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;
47 /// This is used by the unary_expression code to resolve
48 /// a name against a parameter.
50 Parameters current_local_parameters;
53 /// Using during property parsing to describe the implicit
54 /// value parameter that is passed to the "set" and "get"accesor
55 /// methods (properties and indexers).
57 Expression implicit_value_parameter_type;
58 Parameters indexer_parameters;
61 /// Used to determine if we are parsing the get/set pair
62 /// of an indexer or a property
67 /// An out-of-band stack.
76 static public int yacc_verbose_flag;
78 // Name of the file we are parsing
87 /// Temporary Xml documentation cache.
88 /// For enum types, we need one more temporary store.
91 string enumTypeComment;
93 /// Current attribute target
94 string current_attr_target;
96 /// assembly and module attribute definition is enabled
97 bool global_attrs_enabled = true;
102 %token NONE /* This token is never returned by our lexer */
103 %token ERROR // This is used not by the parser, but by the tokenizer.
107 *These are the C# keywords
193 /* C# keywords which are not really keywords */
199 /* C# single character operators/punctuation. */
200 %token OPEN_BRACE "{"
201 %token CLOSE_BRACE "}"
202 %token OPEN_BRACKET "["
203 %token CLOSE_BRACKET "]"
204 %token OPEN_PARENS "("
205 %token CLOSE_PARENS ")"
218 %token BITWISE_AND "&"
219 %token BITWISE_OR "|"
226 /* C# multi-character operators. */
229 %token OP_SHIFT_LEFT "<<"
230 %token OP_SHIFT_RIGHT ">>"
237 %token OP_MULT_ASSIGN "*="
238 %token OP_DIV_ASSIGN "/="
239 %token OP_MOD_ASSIGN "%="
240 %token OP_ADD_ASSIGN "+="
241 %token OP_SUB_ASSIGN "-="
242 %token OP_SHIFT_LEFT_ASSIGN "<<="
243 %token OP_SHIFT_RIGHT_ASSIGN ">>="
244 %token OP_AND_ASSIGN "&="
245 %token OP_XOR_ASSIGN "^="
246 %token OP_OR_ASSIGN "|="
250 %token LITERAL_INTEGER "int literal"
251 %token LITERAL_FLOAT "float literal"
252 %token LITERAL_DOUBLE "double literal"
253 %token LITERAL_DECIMAL "decimal literal"
254 %token LITERAL_CHARACTER "character literal"
255 %token LITERAL_STRING "string literal"
258 %token CLOSE_PARENS_CAST
259 %token CLOSE_PARENS_NO_CAST
260 %token CLOSE_PARENS_OPEN_PARENS
261 %token CLOSE_PARENS_MINUS
263 /* Add precedence rules to solve dangling else s/r conflict */
272 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
274 %left STAR DIV PERCENT
275 %right BANG CARRET UMINUS
276 %nonassoc OP_INC OP_DEC
278 %left OPEN_BRACKET OPEN_BRACE
282 %start compilation_unit
286 : outer_declarations opt_EOF
287 | outer_declarations global_attributes opt_EOF
288 | global_attributes opt_EOF
289 | opt_EOF /* allow empty files */
295 Lexer.check_incorrect_doc_comment ();
299 Lexer.check_incorrect_doc_comment ();
305 | outer_declarations outer_declaration
310 | namespace_member_declaration
315 | using_directives using_directive
319 : using_alias_directive
321 if (RootContext.Documentation != null)
322 Lexer.doc_state = XmlCommentState.Allowed;
324 | using_namespace_directive
326 if (RootContext.Documentation != null)
327 Lexer.doc_state = XmlCommentState.Allowed;
331 using_alias_directive
332 : USING IDENTIFIER ASSIGN
333 namespace_or_type_name SEMICOLON
335 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
338 CheckIdentifierToken (yyToken);
342 using_namespace_directive
343 : USING namespace_name SEMICOLON
345 current_namespace.Using ((string) $2, lexer.Location);
350 // Strictly speaking, namespaces don't have attributes but
351 // we parse global attributes along with namespace declarations and then
354 namespace_declaration
355 : opt_attributes NAMESPACE namespace_or_type_name
358 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
359 + " Expected class, delegate, enum, interface, or struct");
362 MemberName name = (MemberName) $3;
364 if ((current_namespace.Parent != null) && (name.Left != null)) {
365 Report.Error (134, lexer.Location,
366 "Cannot use qualified namespace names in nested " +
367 "namespace declarations");
370 current_namespace = new NamespaceEntry (
371 current_namespace, file, name.GetName (), lexer.Location);
373 namespace_body opt_semicolon
375 current_namespace = current_namespace.Parent;
390 : namespace_or_type_name {
391 MemberName name = (MemberName) $1;
393 $$ = name.GetName ();
400 if (RootContext.Documentation != null)
401 Lexer.doc_state = XmlCommentState.Allowed;
404 opt_namespace_member_declarations
415 opt_namespace_member_declarations
417 | namespace_member_declarations
420 namespace_member_declarations
421 : namespace_member_declaration
422 | namespace_member_declarations namespace_member_declaration
425 namespace_member_declaration
432 Class c = (Class) $1;
433 mod_flags = c.ModFlags;
435 } else if ($1 is Struct){
436 Struct s = (Struct) $1;
437 mod_flags = s.ModFlags;
442 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
444 1527, lexer.Location,
445 "Namespace elements cant be explicitly " +
446 "declared private or protected in `" + name + "'");
448 current_namespace.DeclarationFound = true;
450 | namespace_declaration {
451 current_namespace.DeclarationFound = true;
454 | field_declaration {
455 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
457 | method_declaration {
458 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
465 | interface_declaration
467 | delegate_declaration
469 // Enable this when we have handled all errors, because this acts as a generic fallback
472 // Console.WriteLine ("Token=" + yyToken);
473 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
485 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
493 global_attrs_enabled = false;
498 global_attrs_enabled = false;
507 ArrayList sect = (ArrayList) $1;
509 if (global_attrs_enabled) {
510 if (current_attr_target == "module") {
511 CodeGen.Module.AddAttributes (sect);
513 } else if (current_attr_target == "assembly") {
514 CodeGen.Assembly.AddAttributes (sect);
517 $$ = new Attributes (sect);
520 if (RootContext.Documentation != null) {
521 Lexer.check_incorrect_doc_comment ();
523 XmlCommentState.Allowed;
527 $$ = new Attributes (sect);
529 current_attr_target = null;
531 | attribute_sections attribute_section
533 Attributes attrs = $1 as Attributes;
534 ArrayList sect = (ArrayList) $2;
536 if (global_attrs_enabled) {
537 if (current_attr_target == "module") {
538 CodeGen.Module.AddAttributes (sect);
540 } else if (current_attr_target == "assembly") {
541 CodeGen.Assembly.AddAttributes (sect);
545 attrs = new Attributes (sect);
547 attrs.AddAttributes (sect);
551 attrs = new Attributes (sect);
553 attrs.AddAttributes (sect);
556 current_attr_target = null;
561 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
565 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
571 attribute_target_specifier
572 : attribute_target COLON
574 current_attr_target = (string)$1;
582 CheckAttributeTarget ((string) $1);
585 | EVENT { $$ = "event"; }
586 | RETURN { $$ = "return"; }
592 ArrayList attrs = new ArrayList (4);
598 | attribute_list COMMA attribute
600 ArrayList attrs = (ArrayList) $1;
612 opt_attribute_arguments
614 Location loc = (Location) $2;
615 MemberName mname = (MemberName) $1;
616 MemberName left = mname.Left;
617 string identifier = mname.Name;
619 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
621 if (current_attr_target == "assembly" || current_attr_target == "module")
622 $$ = new GlobalAttribute (current_container, current_attr_target,
623 left_expr, identifier, (ArrayList) $3, loc);
625 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
630 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
633 opt_attribute_arguments
634 : /* empty */ { $$ = null; }
635 | OPEN_PARENS attribute_arguments CLOSE_PARENS
643 : opt_positional_argument_list
648 ArrayList args = new ArrayList (4);
654 | positional_argument_list COMMA named_argument_list
656 ArrayList args = new ArrayList (4);
662 | named_argument_list
664 ArrayList args = new ArrayList (4);
673 opt_positional_argument_list
674 : /* empty */ { $$ = null; }
675 | positional_argument_list
678 positional_argument_list
681 ArrayList args = new ArrayList (4);
682 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
686 | positional_argument_list COMMA expression
688 ArrayList args = (ArrayList) $1;
689 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
698 ArrayList args = new ArrayList (4);
703 | named_argument_list COMMA named_argument
705 ArrayList args = (ArrayList) $1;
710 | named_argument_list COMMA expression
712 Report.Error (1016, lexer.Location, "Named attribute argument expected");
718 : IDENTIFIER ASSIGN expression
720 $$ = new DictionaryEntry (
722 new Argument ((Expression) $3, Argument.AType.Expression));
728 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
731 opt_class_member_declarations
733 | class_member_declarations
736 class_member_declarations
737 : class_member_declaration
738 | class_member_declarations
739 class_member_declaration
742 class_member_declaration
743 : constant_declaration // done
744 | field_declaration // done
745 | method_declaration // done
746 | property_declaration // done
747 | event_declaration // done
748 | indexer_declaration // done
749 | operator_declaration // done
750 | constructor_declaration // done
751 | destructor_declaration // done
761 MemberName name = MakeName ((MemberName) $5);
762 bool partial = (bool) $3;
765 ClassPart part = PartialContainer.CreatePart (
766 current_namespace, current_container, name, (int) $2,
767 (Attributes) $1, Kind.Struct, lexer.Location);
769 current_container = part.PartialContainer;
770 current_class = part;
772 current_class = new Struct (
773 current_namespace, current_container, name, (int) $2,
774 (Attributes) $1, lexer.Location);
776 current_container = current_class;
777 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
783 current_class.Bases = (ArrayList) $7;
785 if (RootContext.Documentation != null)
786 current_class.DocComment = Lexer.consume_doc_comment ();
788 current_class.Register ();
792 if (RootContext.Documentation != null)
793 Lexer.doc_state = XmlCommentState.Allowed;
799 current_container = current_container.Parent;
800 current_class = current_container;
802 | opt_attributes opt_modifiers opt_partial STRUCT error {
803 CheckIdentifierToken (yyToken);
810 if (RootContext.Documentation != null)
811 Lexer.doc_state = XmlCommentState.Allowed;
813 opt_struct_member_declarations CLOSE_BRACE
816 opt_struct_member_declarations
818 | struct_member_declarations
821 struct_member_declarations
822 : struct_member_declaration
823 | struct_member_declarations struct_member_declaration
826 struct_member_declaration
827 : constant_declaration
830 | property_declaration
832 | indexer_declaration
833 | operator_declaration
834 | constructor_declaration
838 * This is only included so we can flag error 575:
839 * destructors only allowed on class types
841 | destructor_declaration
852 int modflags = (int) $2;
853 foreach (VariableDeclaration constant in (ArrayList) $5){
854 Location l = constant.Location;
855 if ((modflags & Modifiers.STATIC) != 0) {
856 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
860 Const c = new Const (
861 current_class, (Expression) $4, (string) constant.identifier,
862 (Expression) constant.expression_or_array_initializer, modflags,
865 if (RootContext.Documentation != null) {
866 c.DocComment = Lexer.consume_doc_comment ();
867 Lexer.doc_state = XmlCommentState.Allowed;
869 current_container.AddConstant (c);
875 : constant_declarator
877 ArrayList constants = new ArrayList (4);
882 | constant_declarators COMMA constant_declarator
885 ArrayList constants = (ArrayList) $1;
892 : IDENTIFIER ASSIGN constant_expression
894 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
898 // A const field requires a value to be provided
899 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
911 Expression type = (Expression) $3;
914 foreach (VariableDeclaration var in (ArrayList) $4){
915 Location l = var.Location;
917 Field field = new Field (current_class, type, mod, var.identifier,
918 var.expression_or_array_initializer,
921 if (RootContext.Documentation != null) {
922 field.DocComment = Lexer.consume_doc_comment ();
923 Lexer.doc_state = XmlCommentState.Allowed;
925 current_container.AddField (field);
933 Report.Error (670, lexer.Location, "void type is not allowed for fields");
938 : variable_declarator
940 ArrayList decl = new ArrayList (4);
944 | variable_declarators COMMA variable_declarator
946 ArrayList decls = (ArrayList) $1;
953 : IDENTIFIER ASSIGN variable_initializer
955 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
959 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
972 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
974 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
978 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
985 iterator_container = (IIteratorContainer) $1;
986 if (RootContext.Documentation != null)
987 Lexer.doc_state = XmlCommentState.NotAllowed;
991 Method method = (Method) $1;
992 Block b = (Block) $3;
993 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
996 if ((method.ModFlags & extern_abstract) == 0){
998 501, lexer.Location, current_container.MakeName (method.Name) +
999 "must declare a body because it is not marked abstract or extern");
1002 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1004 179, lexer.Location, current_container.MakeName (method.Name) +
1005 " is declared extern, but has a body");
1009 method.Block = (ToplevelBlock) $3;
1010 current_container.AddMethod (method);
1012 current_local_parameters = null;
1013 iterator_container = null;
1015 if (RootContext.Documentation != null)
1016 Lexer.doc_state = XmlCommentState.Allowed;
1030 1585, lexer.Location, "Member modifier `" +
1031 Modifiers.Name (i) + "' must precede member type and name");
1042 type namespace_or_type_name
1043 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1045 MemberName name = (MemberName) $4;
1047 Method method = new Method (current_class, (Expression) $3, (int) $2,
1048 false, name, (Parameters) $6, (Attributes) $1,
1051 current_local_parameters = (Parameters) $6;
1053 if (RootContext.Documentation != null)
1054 method.DocComment = Lexer.consume_doc_comment ();
1060 VOID namespace_or_type_name
1061 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1063 MemberName name = (MemberName) $4;
1065 Method method = new Method (current_class, TypeManager.system_void_expr,
1066 (int) $2, false, name, (Parameters) $6,
1067 (Attributes) $1, lexer.Location);
1069 current_local_parameters = (Parameters) $6;
1071 if (RootContext.Documentation != null)
1072 method.DocComment = Lexer.consume_doc_comment ();
1079 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1081 Report.Error (1585, lexer.Location,
1082 String.Format ("Modifier {0} should appear before type",
1083 Modifiers.Name ((int) $4)));
1084 MemberName name = (MemberName) $4;
1086 Method method = new Method (current_class, TypeManager.system_void_expr,
1087 0, false, name, (Parameters) $7, (Attributes) $1,
1090 current_local_parameters = (Parameters) $7;
1092 if (RootContext.Documentation != null)
1093 method.DocComment = Lexer.consume_doc_comment ();
1101 | SEMICOLON { $$ = null; }
1104 opt_formal_parameter_list
1105 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1106 | formal_parameter_list
1109 formal_parameter_list
1112 ArrayList pars_list = (ArrayList) $1;
1114 Parameter [] pars = new Parameter [pars_list.Count];
1115 pars_list.CopyTo (pars);
1117 $$ = new Parameters (pars, null, lexer.Location);
1119 | fixed_parameters COMMA parameter_array
1121 ArrayList pars_list = (ArrayList) $1;
1123 Parameter [] pars = new Parameter [pars_list.Count];
1124 pars_list.CopyTo (pars);
1126 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1128 | fixed_parameters COMMA ARGLIST
1130 ArrayList pars_list = (ArrayList) $1;
1132 Parameter [] pars = new Parameter [pars_list.Count];
1133 pars_list.CopyTo (pars);
1135 $$ = new Parameters (pars, true, lexer.Location);
1137 | parameter_array COMMA fixed_parameters
1139 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1142 | ARGLIST COMMA fixed_parameters
1144 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1149 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1153 $$ = new Parameters (null, true, lexer.Location);
1160 ArrayList pars = new ArrayList (4);
1165 | fixed_parameters COMMA fixed_parameter
1167 ArrayList pars = (ArrayList) $1;
1176 opt_parameter_modifier
1180 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1183 opt_parameter_modifier
1186 Report.Error (1001, lexer.Location, "Identifier expected");
1190 opt_parameter_modifier
1193 CheckIdentifierToken (yyToken);
1197 opt_parameter_modifier
1203 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1208 opt_parameter_modifier
1209 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1210 | parameter_modifier
1214 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1215 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1219 : opt_attributes PARAMS type IDENTIFIER
1221 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1222 note ("type must be a single-dimension array type");
1224 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1226 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1229 | opt_attributes PARAMS type error {
1230 CheckIdentifierToken (yyToken);
1235 property_declaration
1239 namespace_or_type_name
1241 if (RootContext.Documentation != null)
1242 tmpComment = Lexer.consume_doc_comment ();
1246 implicit_value_parameter_type = (Expression) $3;
1248 lexer.PropertyParsing = true;
1250 $$ = lexer.Location;
1252 iterator_container = SimpleIteratorContainer.GetSimple ();
1254 accessor_declarations
1256 lexer.PropertyParsing = false;
1261 Pair pair = (Pair) $8;
1262 Accessor get_block = (Accessor) pair.First;
1263 Accessor set_block = (Accessor) pair.Second;
1265 Location loc = (Location) $7;
1266 MemberName name = (MemberName) $4;
1268 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1269 name, (Attributes) $1, get_block, set_block, loc);
1270 if (SimpleIteratorContainer.Simple.Yields)
1273 current_container.AddProperty (prop);
1274 implicit_value_parameter_type = null;
1275 iterator_container = null;
1277 if (RootContext.Documentation != null)
1278 prop.DocComment = ConsumeStoredComment ();
1283 accessor_declarations
1284 : get_accessor_declaration opt_set_accessor_declaration
1286 $$ = new Pair ($1, $2);
1288 | set_accessor_declaration opt_get_accessor_declaration
1290 $$ = new Pair ($2, $1);
1294 opt_get_accessor_declaration
1295 : /* empty */ { $$ = null; }
1296 | get_accessor_declaration
1299 opt_set_accessor_declaration
1300 : /* empty */ { $$ = null; }
1301 | set_accessor_declaration
1304 get_accessor_declaration
1305 : opt_attributes opt_modifiers GET
1307 // If this is not the case, then current_local_parameters has already
1308 // been set in indexer_declaration
1309 if (parsing_indexer == false)
1310 current_local_parameters = null;
1312 current_local_parameters = indexer_parameters;
1313 lexer.PropertyParsing = false;
1317 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1318 current_local_parameters = null;
1319 lexer.PropertyParsing = true;
1321 if (RootContext.Documentation != null)
1322 if (Lexer.doc_state == XmlCommentState.Error)
1323 Lexer.doc_state = XmlCommentState.NotAllowed;
1327 set_accessor_declaration
1328 : opt_attributes opt_modifiers SET
1331 Parameter implicit_value_parameter = new Parameter (
1332 implicit_value_parameter_type, "value",
1333 Parameter.Modifier.NONE, null);
1335 if (parsing_indexer == false) {
1336 args = new Parameter [1];
1337 args [0] = implicit_value_parameter;
1338 current_local_parameters = new Parameters (args, null, lexer.Location);
1340 Parameter [] fpars = indexer_parameters.FixedParameters;
1343 int count = fpars.Length;
1345 args = new Parameter [count + 1];
1346 fpars.CopyTo (args, 0);
1347 args [count] = implicit_value_parameter;
1350 current_local_parameters = new Parameters (
1351 args, indexer_parameters.ArrayParameter, lexer.Location);
1354 lexer.PropertyParsing = false;
1358 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1359 current_local_parameters = null;
1360 lexer.PropertyParsing = true;
1362 if (RootContext.Documentation != null
1363 && Lexer.doc_state == XmlCommentState.Error)
1364 Lexer.doc_state = XmlCommentState.NotAllowed;
1370 | SEMICOLON { $$ = null; }
1373 interface_declaration
1377 INTERFACE member_name
1379 MemberName name = MakeName ((MemberName) $5);
1380 bool partial = (bool) $3;
1383 ClassPart part = PartialContainer.CreatePart (
1384 current_namespace, current_container, name, (int) $2,
1385 (Attributes) $1, Kind.Interface, lexer.Location);
1387 current_container = part.PartialContainer;
1388 current_class = part;
1390 current_class = new Interface (
1391 current_namespace, current_container, name, (int) $2,
1392 (Attributes) $1, lexer.Location);
1394 current_container = current_class;
1395 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1400 current_class.Bases = (ArrayList) $7;
1402 if (RootContext.Documentation != null) {
1403 current_class.DocComment = Lexer.consume_doc_comment ();
1404 Lexer.doc_state = XmlCommentState.Allowed;
1407 current_class.Register ();
1411 if (RootContext.Documentation != null)
1412 Lexer.doc_state = XmlCommentState.Allowed;
1418 current_container = current_container.Parent;
1419 current_class = current_container;
1421 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1422 CheckIdentifierToken (yyToken);
1428 opt_interface_member_declarations
1432 opt_interface_member_declarations
1434 | interface_member_declarations
1437 interface_member_declarations
1438 : interface_member_declaration
1439 | interface_member_declarations interface_member_declaration
1442 interface_member_declaration
1443 : interface_method_declaration
1445 Method m = (Method) $1;
1447 if (m.IsExplicitImpl)
1448 Report.Error (541, lexer.Location,
1449 "Explicit interface declaration can only be declared in a class or struct");
1451 current_container.AddMethod (m);
1453 if (RootContext.Documentation != null)
1454 Lexer.doc_state = XmlCommentState.Allowed;
1456 | interface_property_declaration
1458 Property p = (Property) $1;
1460 if (p.IsExplicitImpl)
1461 Report.Error (541, lexer.Location,
1462 "Explicit interface declaration can only be declared in a class or struct");
1464 current_container.AddProperty (p);
1466 if (RootContext.Documentation != null)
1467 Lexer.doc_state = XmlCommentState.Allowed;
1469 | interface_event_declaration
1472 Event e = (Event) $1;
1474 if (e.IsExplicitImpl)
1475 Report.Error (541, lexer.Location,
1476 "Explicit interface declaration can only be declared in a class or struct");
1478 current_container.AddEvent (e);
1481 if (RootContext.Documentation != null)
1482 Lexer.doc_state = XmlCommentState.Allowed;
1484 | interface_indexer_declaration
1486 Indexer i = (Indexer) $1;
1488 if (i.IsExplicitImpl)
1489 Report.Error (541, lexer.Location,
1490 "Explicit interface declaration can only be declared in a class or struct");
1492 current_container.AddIndexer (i);
1494 if (RootContext.Documentation != null)
1495 Lexer.doc_state = XmlCommentState.Allowed;
1497 | delegate_declaration
1499 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1503 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1505 | struct_declaration
1507 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1511 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1513 | interface_declaration
1515 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1523 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1528 interface_method_declaration
1529 : opt_attributes opt_new type namespace_or_type_name
1530 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1533 MemberName name = (MemberName) $4;
1535 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1536 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1537 if (RootContext.Documentation != null)
1538 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1540 | opt_attributes opt_new VOID namespace_or_type_name
1541 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1544 MemberName name = (MemberName) $4;
1546 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1547 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1548 if (RootContext.Documentation != null)
1549 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1553 interface_property_declaration
1558 { lexer.PropertyParsing = true; }
1560 { lexer.PropertyParsing = false; }
1563 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1565 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1566 new MemberName ((string) $4), (Attributes) $1,
1567 pinfo.Get, pinfo.Set, lexer.Location);
1568 if (RootContext.Documentation != null)
1569 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1574 CheckIdentifierToken (yyToken);
1580 : opt_attributes opt_modifiers GET SEMICOLON
1581 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1582 | opt_attributes opt_modifiers SET SEMICOLON
1583 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1584 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1585 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1586 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1587 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1590 interface_event_declaration
1591 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1593 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1594 new MemberName ((string) $5), null,
1595 (Attributes) $1, lexer.Location);
1596 if (RootContext.Documentation != null)
1597 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1599 | opt_attributes opt_new EVENT type error {
1600 CheckIdentifierToken (yyToken);
1603 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1604 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1607 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1608 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1613 interface_indexer_declaration
1614 : opt_attributes opt_new type THIS
1615 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1617 { lexer.PropertyParsing = true; }
1619 { lexer.PropertyParsing = false; }
1622 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1624 $$ = new Indexer (current_class, (Expression) $3,
1625 new MemberName (TypeContainer.DefaultIndexerName),
1626 (int) $2, true, (Parameters) $6, (Attributes) $1,
1627 info.Get, info.Set, lexer.Location);
1628 if (RootContext.Documentation != null)
1629 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1633 operator_declaration
1634 : opt_attributes opt_modifiers operator_declarator
1636 iterator_container = SimpleIteratorContainer.GetSimple ();
1640 OperatorDeclaration decl = (OperatorDeclaration) $3;
1642 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1644 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1645 if (decl.arg2type != null)
1646 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1648 Operator op = new Operator (
1649 current_class, decl.optype, decl.ret_type, (int) $2,
1650 new Parameters (param_list, null, decl.location),
1651 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1653 if (RootContext.Documentation != null) {
1654 op.DocComment = tmpComment;
1655 Lexer.doc_state = XmlCommentState.Allowed;
1658 if (SimpleIteratorContainer.Simple.Yields)
1661 // Note again, checking is done in semantic analysis
1662 current_container.AddOperator (op);
1664 current_local_parameters = null;
1665 iterator_container = null;
1671 | SEMICOLON { $$ = null; }
1674 : type OPERATOR overloadable_operator
1675 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1677 Operator.OpType op = (Operator.OpType) $3;
1678 CheckUnaryOperator (op);
1680 if (op == Operator.OpType.Addition)
1681 op = Operator.OpType.UnaryPlus;
1683 if (op == Operator.OpType.Subtraction)
1684 op = Operator.OpType.UnaryNegation;
1686 Parameter [] pars = new Parameter [1];
1687 Expression type = (Expression) $5;
1689 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1691 current_local_parameters = new Parameters (pars, null, lexer.Location);
1693 if (RootContext.Documentation != null) {
1694 tmpComment = Lexer.consume_doc_comment ();
1695 Lexer.doc_state = XmlCommentState.NotAllowed;
1698 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1699 null, null, lexer.Location);
1701 | type OPERATOR overloadable_operator
1703 type IDENTIFIER COMMA
1707 CheckBinaryOperator ((Operator.OpType) $3);
1709 Parameter [] pars = new Parameter [2];
1711 Expression typeL = (Expression) $5;
1712 Expression typeR = (Expression) $8;
1714 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1715 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1717 current_local_parameters = new Parameters (pars, null, lexer.Location);
1719 if (RootContext.Documentation != null) {
1720 tmpComment = Lexer.consume_doc_comment ();
1721 Lexer.doc_state = XmlCommentState.NotAllowed;
1724 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1726 typeR, (string) $9, lexer.Location);
1728 | conversion_operator_declarator
1731 overloadable_operator
1733 : BANG { $$ = Operator.OpType.LogicalNot; }
1734 | TILDE { $$ = Operator.OpType.OnesComplement; }
1735 | OP_INC { $$ = Operator.OpType.Increment; }
1736 | OP_DEC { $$ = Operator.OpType.Decrement; }
1737 | TRUE { $$ = Operator.OpType.True; }
1738 | FALSE { $$ = Operator.OpType.False; }
1739 // Unary and binary:
1740 | PLUS { $$ = Operator.OpType.Addition; }
1741 | MINUS { $$ = Operator.OpType.Subtraction; }
1743 | STAR { $$ = Operator.OpType.Multiply; }
1744 | DIV { $$ = Operator.OpType.Division; }
1745 | PERCENT { $$ = Operator.OpType.Modulus; }
1746 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1747 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1748 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1749 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1750 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1751 | OP_EQ { $$ = Operator.OpType.Equality; }
1752 | OP_NE { $$ = Operator.OpType.Inequality; }
1753 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1754 | OP_LT { $$ = Operator.OpType.LessThan; }
1755 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1756 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1759 conversion_operator_declarator
1760 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1762 Parameter [] pars = new Parameter [1];
1764 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1766 current_local_parameters = new Parameters (pars, null, lexer.Location);
1768 if (RootContext.Documentation != null) {
1769 tmpComment = Lexer.consume_doc_comment ();
1770 Lexer.doc_state = XmlCommentState.NotAllowed;
1773 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1774 null, null, lexer.Location);
1776 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1778 Parameter [] pars = new Parameter [1];
1780 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1782 current_local_parameters = new Parameters (pars, null, lexer.Location);
1784 if (RootContext.Documentation != null) {
1785 tmpComment = Lexer.consume_doc_comment ();
1786 Lexer.doc_state = XmlCommentState.NotAllowed;
1789 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1790 null, null, lexer.Location);
1794 syntax_error (lexer.Location, "'operator' expected");
1798 syntax_error (lexer.Location, "'operator' expected");
1802 constructor_declaration
1805 constructor_declarator
1808 Constructor c = (Constructor) $3;
1809 c.Block = (ToplevelBlock) $4;
1810 c.OptAttributes = (Attributes) $1;
1811 c.ModFlags = (int) $2;
1813 if (RootContext.Documentation != null)
1814 c.DocComment = ConsumeStoredComment ();
1816 if (c.Name == current_container.Basename){
1817 if ((c.ModFlags & Modifiers.STATIC) != 0){
1818 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1820 515, c.Location, String.Format (
1821 "`{0}.{1}': static constructor can not have access modifiers",
1822 c.Name, current_container.Name));
1825 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1827 if (c.Initializer != null){
1830 "Static constructors can not have an explicit this or base " +
1831 "constructor invocations");
1834 if (!c.Parameters.Empty){
1836 132, c.Location, "Static constructors should not have parameters");
1839 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1842 // We let another layer check the validity of the constructor.
1843 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1846 current_container.AddConstructor (c);
1848 current_local_parameters = null;
1849 if (RootContext.Documentation != null)
1850 Lexer.doc_state = XmlCommentState.Allowed;
1854 constructor_declarator
1857 if (RootContext.Documentation != null) {
1858 tmpComment = Lexer.consume_doc_comment ();
1859 Lexer.doc_state = XmlCommentState.Allowed;
1862 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1864 current_local_parameters = (Parameters) $4;
1866 opt_constructor_initializer
1868 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1869 (ConstructorInitializer) $8, (Location) $6);
1875 | SEMICOLON { $$ = null; }
1878 opt_constructor_initializer
1879 : /* empty */ { $$ = null; }
1880 | constructor_initializer
1883 constructor_initializer
1884 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1886 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1888 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1890 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1893 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1899 : /* EMPTY */ { $$ = 0; }
1900 | UNSAFE { $$ = Modifiers.UNSAFE; }
1901 | EXTERN { $$ = Modifiers.EXTERN; }
1904 destructor_declaration
1905 : opt_attributes opt_finalizer TILDE
1907 if (RootContext.Documentation != null) {
1908 tmpComment = Lexer.consume_doc_comment ();
1909 Lexer.doc_state = XmlCommentState.NotAllowed;
1912 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1914 if ((string) $5 != current_container.Basename){
1915 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1916 } else if (current_container.Kind != Kind.Class){
1917 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1919 Location l = lexer.Location;
1922 if (!RootContext.StdLib && current_container.Name == "System.Object")
1923 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1925 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1927 if ((m & Modifiers.UNSAFE) != 0){
1928 if (!RootContext.Unsafe){
1929 Report.Error (227, l,
1930 "Unsafe code requires the -unsafe command " +
1931 "line option to be specified");
1935 Method d = new Destructor (
1936 current_class, TypeManager.system_void_expr, m, "Finalize",
1937 new Parameters (null, null, l), (Attributes) $1, l);
1938 if (RootContext.Documentation != null)
1939 d.DocComment = ConsumeStoredComment ();
1941 d.Block = (ToplevelBlock) $8;
1942 current_container.AddMethod (d);
1950 EVENT type variable_declarators SEMICOLON
1952 foreach (VariableDeclaration var in (ArrayList) $5) {
1954 MemberName name = new MemberName (var.identifier);
1956 Event e = new EventField (
1957 current_class, (Expression) $4, (int) $2, false, name,
1958 var.expression_or_array_initializer, (Attributes) $1,
1961 current_container.AddEvent (e);
1963 if (RootContext.Documentation != null) {
1964 e.DocComment = Lexer.consume_doc_comment ();
1965 Lexer.doc_state = XmlCommentState.Allowed;
1971 EVENT type namespace_or_type_name
1974 implicit_value_parameter_type = (Expression) $4;
1975 lexer.EventParsing = true;
1977 event_accessor_declarations
1979 lexer.EventParsing = false;
1983 Location loc = (Location) $7;
1986 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1989 Pair pair = (Pair) $9;
1991 MemberName name = (MemberName) $5;
1993 Event e = new EventProperty (
1994 current_class, (Expression) $4, (int) $2, false, name, null,
1995 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1997 if (RootContext.Documentation != null) {
1998 e.DocComment = Lexer.consume_doc_comment ();
1999 Lexer.doc_state = XmlCommentState.Allowed;
2002 current_container.AddEvent (e);
2003 implicit_value_parameter_type = null;
2006 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2007 MemberName mn = (MemberName) $5;
2009 if (mn.Left != null)
2010 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2012 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2014 if (RootContext.Documentation != null)
2015 Lexer.doc_state = XmlCommentState.Allowed;
2019 event_accessor_declarations
2020 : add_accessor_declaration remove_accessor_declaration
2022 $$ = new Pair ($1, $2);
2024 | remove_accessor_declaration add_accessor_declaration
2026 $$ = new Pair ($2, $1);
2028 | add_accessor_declaration { $$ = null; }
2029 | remove_accessor_declaration { $$ = null; }
2032 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2038 add_accessor_declaration
2039 : opt_attributes ADD
2041 Parameter [] args = new Parameter [1];
2042 Parameter implicit_value_parameter = new Parameter (
2043 implicit_value_parameter_type, "value",
2044 Parameter.Modifier.NONE, null);
2046 args [0] = implicit_value_parameter;
2048 current_local_parameters = new Parameters (args, null, lexer.Location);
2049 lexer.EventParsing = false;
2053 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2054 lexer.EventParsing = true;
2056 | opt_attributes ADD error {
2057 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2062 remove_accessor_declaration
2063 : opt_attributes REMOVE
2065 Parameter [] args = new Parameter [1];
2066 Parameter implicit_value_parameter = new Parameter (
2067 implicit_value_parameter_type, "value",
2068 Parameter.Modifier.NONE, null);
2070 args [0] = implicit_value_parameter;
2072 current_local_parameters = new Parameters (args, null, lexer.Location);
2073 lexer.EventParsing = false;
2077 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2078 lexer.EventParsing = true;
2080 | opt_attributes REMOVE error {
2081 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2087 : opt_attributes opt_modifiers indexer_declarator
2090 IndexerDeclaration decl = (IndexerDeclaration) $3;
2092 implicit_value_parameter_type = decl.type;
2094 lexer.PropertyParsing = true;
2095 parsing_indexer = true;
2097 indexer_parameters = decl.param_list;
2099 accessor_declarations
2101 lexer.PropertyParsing = false;
2102 parsing_indexer = false;
2106 // The signature is computed from the signature of the indexer. Look
2107 // at section 3.6 on the spec
2108 Location loc = (Location) $5;
2110 IndexerDeclaration decl = (IndexerDeclaration) $3;
2111 Pair pair = (Pair) $7;
2112 Accessor get_block = (Accessor) pair.First;
2113 Accessor set_block = (Accessor) pair.Second;
2116 if (decl.interface_type != null)
2117 name = new MemberName (decl.interface_type,
2118 TypeContainer.DefaultIndexerName);
2120 name = new MemberName (TypeContainer.DefaultIndexerName);
2122 indexer = new Indexer (current_class, decl.type, name,
2123 (int) $2, false, decl.param_list, (Attributes) $1,
2124 get_block, set_block, loc);
2125 if (RootContext.Documentation != null)
2126 indexer.DocComment = ConsumeStoredComment ();
2128 current_container.AddIndexer (indexer);
2130 current_local_parameters = null;
2131 implicit_value_parameter_type = null;
2132 indexer_parameters = null;
2137 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2139 Parameters pars = (Parameters) $4;
2140 if (pars.HasArglist) {
2141 // "__arglist is not valid in this context"
2142 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2143 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2144 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2146 if (RootContext.Documentation != null) {
2147 tmpComment = Lexer.consume_doc_comment ();
2148 Lexer.doc_state = XmlCommentState.Allowed;
2151 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2153 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2155 Parameters pars = (Parameters) $6;
2157 if (pars.HasArglist) {
2158 // "__arglist is not valid in this context"
2159 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2160 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2161 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2164 MemberName name = (MemberName) $2;
2165 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2167 if (RootContext.Documentation != null) {
2168 tmpComment = Lexer.consume_doc_comment ();
2169 Lexer.doc_state = XmlCommentState.Allowed;
2179 if (RootContext.Documentation != null)
2180 enumTypeComment = Lexer.consume_doc_comment ();
2185 Location enum_location = lexer.Location;
2187 MemberName full_name = MakeName (new MemberName ((string) $4));
2188 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2189 full_name, (Attributes) $1, enum_location);
2191 if (RootContext.Documentation != null)
2192 e.DocComment = enumTypeComment;
2194 foreach (VariableDeclaration ev in (ArrayList) $7) {
2195 e.AddEnumMember (ev.identifier,
2196 (Expression) ev.expression_or_array_initializer,
2197 ev.Location, ev.OptAttributes,
2201 string name = full_name.GetName ();
2202 current_container.AddEnum (e);
2203 RootContext.Tree.RecordDecl (name, e);
2209 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2210 | COLON type { $$ = $2; }
2216 if (RootContext.Documentation != null)
2217 Lexer.doc_state = XmlCommentState.Allowed;
2219 opt_enum_member_declarations
2221 // here will be evaluated after CLOSE_BLACE is consumed.
2222 if (RootContext.Documentation != null)
2223 Lexer.doc_state = XmlCommentState.Allowed;
2231 opt_enum_member_declarations
2232 : /* empty */ { $$ = new ArrayList (4); }
2233 | enum_member_declarations opt_comma { $$ = $1; }
2236 enum_member_declarations
2237 : enum_member_declaration
2239 ArrayList l = new ArrayList (4);
2244 | enum_member_declarations COMMA enum_member_declaration
2246 ArrayList l = (ArrayList) $1;
2254 enum_member_declaration
2255 : opt_attributes IDENTIFIER
2257 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2259 if (RootContext.Documentation != null) {
2260 vd.DocComment = Lexer.consume_doc_comment ();
2261 Lexer.doc_state = XmlCommentState.Allowed;
2266 | opt_attributes IDENTIFIER
2268 $$ = lexer.Location;
2269 if (RootContext.Documentation != null) {
2270 tmpComment = Lexer.consume_doc_comment ();
2271 Lexer.doc_state = XmlCommentState.NotAllowed;
2276 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2278 if (RootContext.Documentation != null)
2279 vd.DocComment = ConsumeStoredComment ();
2285 delegate_declaration
2288 DELEGATE type member_name
2289 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2292 Location l = lexer.Location;
2293 MemberName name = MakeName ((MemberName) $5);
2294 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2295 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2297 if (RootContext.Documentation != null) {
2298 del.DocComment = Lexer.consume_doc_comment ();
2299 Lexer.doc_state = XmlCommentState.Allowed;
2302 current_container.AddDelegate (del);
2303 RootContext.Tree.RecordDecl (name.GetName (true), del);
2307 DELEGATE VOID member_name
2308 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2311 Location l = lexer.Location;
2312 MemberName name = MakeName ((MemberName) $5);
2313 Delegate del = new Delegate (
2314 current_namespace, current_container,
2315 TypeManager.system_void_expr, (int) $2, name,
2316 (Parameters) $7, (Attributes) $1, l);
2318 if (RootContext.Documentation != null) {
2319 del.DocComment = Lexer.consume_doc_comment ();
2320 Lexer.doc_state = XmlCommentState.Allowed;
2323 current_container.AddDelegate (del);
2324 RootContext.Tree.RecordDecl (name.GetName (true), del);
2328 namespace_or_type_name
2330 | namespace_or_type_name DOT IDENTIFIER {
2331 $$ = new MemberName ((MemberName) $1, (string) $3);
2337 $$ = new MemberName ((string) $1);
2342 * Before you think of adding a return_type, notice that we have been
2343 * using two rules in the places where it matters (one rule using type
2344 * and another identical one that uses VOID as the return type). This
2345 * gets rid of a shift/reduce couple
2348 : namespace_or_type_name
2350 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2362 // Note that here only unmanaged types are allowed but we
2363 // can't perform checks during this phase - we do it during
2364 // semantic analysis.
2366 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2370 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2376 | non_expression_type rank_specifier
2378 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2380 | non_expression_type STAR
2382 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2384 | expression rank_specifiers
2386 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2390 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2394 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2395 // through this different path
2397 | multiplicative_expression STAR
2399 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2406 ArrayList types = new ArrayList (4);
2411 | type_list COMMA type
2413 ArrayList types = (ArrayList) $1;
2421 * replaces all the productions for isolating the various
2422 * simple types, but we need this to reuse it easily in local_variable_type
2425 : OBJECT { $$ = TypeManager.system_object_expr; }
2426 | STRING { $$ = TypeManager.system_string_expr; }
2427 | BOOL { $$ = TypeManager.system_boolean_expr; }
2428 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2429 | FLOAT { $$ = TypeManager.system_single_expr; }
2430 | DOUBLE { $$ = TypeManager.system_double_expr; }
2435 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2436 | BYTE { $$ = TypeManager.system_byte_expr; }
2437 | SHORT { $$ = TypeManager.system_int16_expr; }
2438 | USHORT { $$ = TypeManager.system_uint16_expr; }
2439 | INT { $$ = TypeManager.system_int32_expr; }
2440 | UINT { $$ = TypeManager.system_uint32_expr; }
2441 | LONG { $$ = TypeManager.system_int64_expr; }
2442 | ULONG { $$ = TypeManager.system_uint64_expr; }
2443 | CHAR { $$ = TypeManager.system_char_expr; }
2444 | VOID { $$ = TypeManager.system_void_expr; }
2448 : type rank_specifiers
2450 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2455 // Expressions, section 7.5
2465 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2467 | parenthesized_expression
2469 | invocation_expression
2473 | post_increment_expression
2474 | post_decrement_expression
2478 | checked_expression
2479 | unchecked_expression
2480 | pointer_member_access
2481 | anonymous_method_expression
2488 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2489 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2490 | NULL { $$ = NullLiteral.Null; }
2494 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2495 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2496 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2501 object v = lexer.Value;
2507 $$ = IntLiteral.Zero;
2509 $$ = IntLiteral.One;
2511 $$ = new IntLiteral (i);
2512 } else if (v is uint)
2513 $$ = new UIntLiteral ((UInt32) v);
2515 $$ = new LongLiteral ((Int64) v);
2516 else if (v is ulong)
2517 $$ = new ULongLiteral ((UInt64) v);
2519 Console.WriteLine ("OOPS. Unexpected result from scanner");
2524 : TRUE { $$ = new BoolLiteral (true); }
2525 | FALSE { $$ = new BoolLiteral (false); }
2528 parenthesized_expression_0
2529 : OPEN_PARENS expression CLOSE_PARENS
2532 lexer.Deambiguate_CloseParens ();
2533 // After this, the next token returned is one of
2534 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2535 // or CLOSE_PARENS_MINUS.
2539 parenthesized_expression
2540 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2544 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2546 // If a parenthesized expression is followed by a minus, we need to wrap
2547 // the expression inside a ParenthesizedExpression for the CS0075 check
2548 // in Binary.DoResolve().
2549 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2554 : primary_expression DOT IDENTIFIER
2556 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2558 | predefined_type DOT IDENTIFIER
2560 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2568 invocation_expression
2569 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2572 Location l = lexer.Location;
2573 Report.Error (1, l, "Parse error");
2575 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2577 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2579 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2581 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2583 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2585 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2587 ArrayList args = new ArrayList (1);
2589 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2591 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2593 ArrayList args = ((ArrayList) $4);
2595 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2600 : /* empty */ { $$ = null; }
2607 ArrayList list = new ArrayList (4);
2611 | argument_list COMMA argument
2613 ArrayList list = (ArrayList) $1;
2617 | argument_list error {
2618 CheckToken (1026, yyToken, ", or ) expected");
2625 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2627 | non_simple_argument
2634 : REF variable_reference
2636 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2638 | OUT variable_reference
2640 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2642 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2644 ArrayList list = (ArrayList) $3;
2645 Argument[] args = new Argument [list.Count];
2646 list.CopyTo (args, 0);
2648 Expression expr = new Arglist (args, lexer.Location);
2649 $$ = new Argument (expr, Argument.AType.Expression);
2653 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2658 : expression { note ("section 5.4"); $$ = $1; }
2662 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2664 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2666 | primary_expression rank_specifiers
2668 // So the super-trick is that primary_expression
2669 // can only be either a SimpleName or a MemberAccess.
2670 // The MemberAccess case arises when you have a fully qualified type-name like :
2672 // SimpleName is when you have
2675 Expression expr = (Expression) $1;
2676 if (expr is ComposedCast){
2677 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2678 } else if (!(expr is SimpleName || expr is MemberAccess)){
2679 Error_ExpectingTypeName (lexer.Location, expr);
2680 $$ = TypeManager.system_object_expr;
2683 // So we extract the string corresponding to the SimpleName
2686 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2694 ArrayList list = new ArrayList (4);
2698 | expression_list COMMA expression
2700 ArrayList list = (ArrayList) $1;
2709 $$ = new This (current_block, lexer.Location);
2714 : BASE DOT IDENTIFIER
2716 $$ = new BaseAccess ((string) $3, lexer.Location);
2718 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2720 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2723 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2728 post_increment_expression
2729 : primary_expression OP_INC
2731 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2732 (Expression) $1, lexer.Location);
2736 post_decrement_expression
2737 : primary_expression OP_DEC
2739 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2740 (Expression) $1, lexer.Location);
2745 : object_or_delegate_creation_expression
2746 | array_creation_expression
2749 object_or_delegate_creation_expression
2750 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2752 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2756 array_creation_expression
2757 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2759 opt_array_initializer
2761 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2763 | NEW type rank_specifiers array_initializer
2765 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2769 Report.Error (1031, lexer.Location, "Type expected");
2774 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2790 : rank_specifier opt_rank_specifier
2792 $$ = (string) $2 + (string) $1;
2797 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2799 $$ = "[" + (string) $2 + "]";
2819 | dim_separators COMMA
2821 $$ = (string) $1 + ",";
2825 opt_array_initializer
2837 : OPEN_BRACE CLOSE_BRACE
2839 ArrayList list = new ArrayList (4);
2842 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2844 $$ = (ArrayList) $2;
2848 variable_initializer_list
2849 : variable_initializer
2851 ArrayList list = new ArrayList (4);
2855 | variable_initializer_list COMMA variable_initializer
2857 ArrayList list = (ArrayList) $1;
2864 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2866 $$ = new TypeOfVoid (lexer.Location);
2868 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2870 $$ = new TypeOf ((Expression) $3, lexer.Location);
2875 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2876 $$ = new SizeOf ((Expression) $3, lexer.Location);
2881 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2883 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2887 unchecked_expression
2888 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2890 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2894 pointer_member_access
2895 : primary_expression OP_PTR IDENTIFIER
2899 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2900 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2904 anonymous_method_expression
2905 : DELEGATE opt_anonymous_method_signature _mark_
2907 oob_stack.Push (current_local_parameters);
2908 current_local_parameters = (Parameters)$2;
2910 // Force the next block to be created as a ToplevelBlock
2911 oob_stack.Push (current_block);
2912 oob_stack.Push (top_current_block);
2913 current_block = null;
2917 Location loc = (Location) $3;
2918 top_current_block = (Block) oob_stack.Pop ();
2919 current_block = (Block) oob_stack.Pop ();
2920 if (RootContext.Version == LanguageVersion.ISO_1){
2921 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2924 ToplevelBlock anon_block = (ToplevelBlock) $5;
2926 anon_block.Parent = current_block;
2927 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
2930 current_local_parameters = (Parameters) oob_stack.Pop ();
2934 opt_anonymous_method_signature
2935 : /* empty */ { $$ = null; }
2936 | anonymous_method_signature
2939 anonymous_method_signature
2940 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2943 $$ = Parameters.EmptyReadOnlyParameters;
2945 ArrayList par_list = (ArrayList) $2;
2946 Parameter [] pars = new Parameter [par_list.Count];
2947 par_list.CopyTo (pars);
2948 $$ = new Parameters (pars, null, lexer.Location);
2953 opt_anonymous_method_parameter_list
2954 : /* empty */ { $$ = null; }
2955 | anonymous_method_parameter_list { $$ = $1; }
2958 anonymous_method_parameter_list
2959 : anonymous_method_parameter
2961 ArrayList a = new ArrayList (4);
2965 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2967 ArrayList a = (ArrayList) $1;
2973 anonymous_method_parameter
2974 : opt_parameter_modifier type IDENTIFIER {
2975 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
2977 | PARAMS type IDENTIFIER {
2978 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
2984 : primary_expression
2985 | BANG prefixed_unary_expression
2987 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2989 | TILDE prefixed_unary_expression
2991 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2997 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2999 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3001 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3003 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3009 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3011 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3016 // The idea to split this out is from Rhys' grammar
3017 // to solve the problem with casts.
3019 prefixed_unary_expression
3021 | PLUS prefixed_unary_expression
3023 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3025 | MINUS prefixed_unary_expression
3027 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3029 | OP_INC prefixed_unary_expression
3031 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3032 (Expression) $2, lexer.Location);
3034 | OP_DEC prefixed_unary_expression
3036 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3037 (Expression) $2, lexer.Location);
3039 | STAR prefixed_unary_expression
3041 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3043 | BITWISE_AND prefixed_unary_expression
3045 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3049 pre_increment_expression
3050 : OP_INC prefixed_unary_expression
3052 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3053 (Expression) $2, lexer.Location);
3057 pre_decrement_expression
3058 : OP_DEC prefixed_unary_expression
3060 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3061 (Expression) $2, lexer.Location);
3065 multiplicative_expression
3066 : prefixed_unary_expression
3067 | multiplicative_expression STAR prefixed_unary_expression
3069 $$ = new Binary (Binary.Operator.Multiply,
3070 (Expression) $1, (Expression) $3, lexer.Location);
3072 | multiplicative_expression DIV prefixed_unary_expression
3074 $$ = new Binary (Binary.Operator.Division,
3075 (Expression) $1, (Expression) $3, lexer.Location);
3077 | multiplicative_expression PERCENT prefixed_unary_expression
3079 $$ = new Binary (Binary.Operator.Modulus,
3080 (Expression) $1, (Expression) $3, lexer.Location);
3085 : multiplicative_expression
3086 | additive_expression PLUS multiplicative_expression
3088 $$ = new Binary (Binary.Operator.Addition,
3089 (Expression) $1, (Expression) $3, lexer.Location);
3091 | additive_expression MINUS multiplicative_expression
3093 $$ = new Binary (Binary.Operator.Subtraction,
3094 (Expression) $1, (Expression) $3, lexer.Location);
3099 : additive_expression
3100 | shift_expression OP_SHIFT_LEFT additive_expression
3102 $$ = new Binary (Binary.Operator.LeftShift,
3103 (Expression) $1, (Expression) $3, lexer.Location);
3105 | shift_expression OP_SHIFT_RIGHT additive_expression
3107 $$ = new Binary (Binary.Operator.RightShift,
3108 (Expression) $1, (Expression) $3, lexer.Location);
3112 relational_expression
3114 | relational_expression OP_LT shift_expression
3116 $$ = new Binary (Binary.Operator.LessThan,
3117 (Expression) $1, (Expression) $3, lexer.Location);
3119 | relational_expression OP_GT shift_expression
3121 $$ = new Binary (Binary.Operator.GreaterThan,
3122 (Expression) $1, (Expression) $3, lexer.Location);
3124 | relational_expression OP_LE shift_expression
3126 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3127 (Expression) $1, (Expression) $3, lexer.Location);
3129 | relational_expression OP_GE shift_expression
3131 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3132 (Expression) $1, (Expression) $3, lexer.Location);
3134 | relational_expression IS type
3136 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3138 | relational_expression AS type
3140 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3145 : relational_expression
3146 | equality_expression OP_EQ relational_expression
3148 $$ = new Binary (Binary.Operator.Equality,
3149 (Expression) $1, (Expression) $3, lexer.Location);
3151 | equality_expression OP_NE relational_expression
3153 $$ = new Binary (Binary.Operator.Inequality,
3154 (Expression) $1, (Expression) $3, lexer.Location);
3159 : equality_expression
3160 | and_expression BITWISE_AND equality_expression
3162 $$ = new Binary (Binary.Operator.BitwiseAnd,
3163 (Expression) $1, (Expression) $3, lexer.Location);
3167 exclusive_or_expression
3169 | exclusive_or_expression CARRET and_expression
3171 $$ = new Binary (Binary.Operator.ExclusiveOr,
3172 (Expression) $1, (Expression) $3, lexer.Location);
3176 inclusive_or_expression
3177 : exclusive_or_expression
3178 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3180 $$ = new Binary (Binary.Operator.BitwiseOr,
3181 (Expression) $1, (Expression) $3, lexer.Location);
3185 conditional_and_expression
3186 : inclusive_or_expression
3187 | conditional_and_expression OP_AND inclusive_or_expression
3189 $$ = new Binary (Binary.Operator.LogicalAnd,
3190 (Expression) $1, (Expression) $3, lexer.Location);
3194 conditional_or_expression
3195 : conditional_and_expression
3196 | conditional_or_expression OP_OR conditional_and_expression
3198 $$ = new Binary (Binary.Operator.LogicalOr,
3199 (Expression) $1, (Expression) $3, lexer.Location);
3203 conditional_expression
3204 : conditional_or_expression
3205 | conditional_or_expression INTERR expression COLON expression
3207 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3211 assignment_expression
3212 : prefixed_unary_expression ASSIGN expression
3214 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3216 | prefixed_unary_expression OP_MULT_ASSIGN expression
3218 Location l = lexer.Location;
3220 $$ = new CompoundAssign (
3221 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3223 | prefixed_unary_expression OP_DIV_ASSIGN expression
3225 Location l = lexer.Location;
3227 $$ = new CompoundAssign (
3228 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3230 | prefixed_unary_expression OP_MOD_ASSIGN expression
3232 Location l = lexer.Location;
3234 $$ = new CompoundAssign (
3235 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3237 | prefixed_unary_expression OP_ADD_ASSIGN expression
3239 Location l = lexer.Location;
3241 $$ = new CompoundAssign (
3242 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3244 | prefixed_unary_expression OP_SUB_ASSIGN expression
3246 Location l = lexer.Location;
3248 $$ = new CompoundAssign (
3249 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3251 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3253 Location l = lexer.Location;
3255 $$ = new CompoundAssign (
3256 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3258 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3260 Location l = lexer.Location;
3262 $$ = new CompoundAssign (
3263 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3265 | prefixed_unary_expression OP_AND_ASSIGN expression
3267 Location l = lexer.Location;
3269 $$ = new CompoundAssign (
3270 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3272 | prefixed_unary_expression OP_OR_ASSIGN expression
3274 Location l = lexer.Location;
3276 $$ = new CompoundAssign (
3277 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3279 | prefixed_unary_expression OP_XOR_ASSIGN expression
3281 Location l = lexer.Location;
3283 $$ = new CompoundAssign (
3284 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3289 : conditional_expression
3290 | assignment_expression
3310 MemberName name = MakeName ((MemberName) $5);
3311 bool partial = (bool) $3;
3312 int mod_flags = (int) $2;
3315 ClassPart part = PartialContainer.CreatePart (
3316 current_namespace, current_container, name, mod_flags,
3317 (Attributes) $1, Kind.Class, lexer.Location);
3319 current_container = part.PartialContainer;
3320 current_class = part;
3322 if ((mod_flags & Modifiers.STATIC) != 0) {
3323 current_class = new StaticClass (
3324 current_namespace, current_container, name,
3325 mod_flags, (Attributes) $1, lexer.Location);
3327 current_class = new Class (
3328 current_namespace, current_container, name,
3329 mod_flags, (Attributes) $1, lexer.Location);
3332 current_container = current_class;
3333 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3339 if (current_class.Name == "System.Object") {
3340 Report.Error (537, current_class.Location,
3341 "The class System.Object cannot have a base " +
3342 "class or implement an interface.");
3344 current_class.Bases = (ArrayList) $7;
3347 if (RootContext.Documentation != null) {
3348 current_class.DocComment = Lexer.consume_doc_comment ();
3349 Lexer.doc_state = XmlCommentState.Allowed;
3352 current_class.Register ();
3356 if (RootContext.Documentation != null)
3357 Lexer.doc_state = XmlCommentState.Allowed;
3363 current_container = current_container.Parent;
3364 current_class = current_container;
3370 { $$ = (bool) false; }
3372 { $$ = (bool) true; }
3376 : /* empty */ { $$ = (int) 0; }
3382 | modifiers modifier
3387 if ((m1 & m2) != 0) {
3388 Location l = lexer.Location;
3389 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3391 $$ = (int) (m1 | m2);
3396 : NEW { $$ = Modifiers.NEW; }
3397 | PUBLIC { $$ = Modifiers.PUBLIC; }
3398 | PROTECTED { $$ = Modifiers.PROTECTED; }
3399 | INTERNAL { $$ = Modifiers.INTERNAL; }
3400 | PRIVATE { $$ = Modifiers.PRIVATE; }
3401 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3402 | SEALED { $$ = Modifiers.SEALED; }
3403 | STATIC { $$ = Modifiers.STATIC; }
3404 | READONLY { $$ = Modifiers.READONLY; }
3405 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3406 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3407 | EXTERN { $$ = Modifiers.EXTERN; }
3408 | VOLATILE { $$ = Modifiers.VOLATILE; }
3409 | UNSAFE { $$ = Modifiers.UNSAFE; }
3413 : /* empty */ { $$ = null; }
3414 | class_base { $$ = $1; }
3418 : COLON type_list { $$ = $2; }
3426 // A block is "contained" on the following places:
3428 // property_declaration as part of the accessor body (get/set)
3429 // operator_declaration
3430 // constructor_declaration
3431 // destructor_declaration
3432 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3437 if (current_block == null){
3438 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3439 top_current_block = current_block;
3441 current_block = new Block (current_block, current_local_parameters,
3442 lexer.Location, Location.Null);
3445 opt_statement_list CLOSE_BRACE
3447 while (current_block.Implicit)
3448 current_block = current_block.Parent;
3450 current_block.SetEndLocation (lexer.Location);
3451 current_block = current_block.Parent;
3452 if (current_block == null)
3453 top_current_block = null;
3464 | statement_list statement
3468 : declaration_statement
3470 if ($1 != null && (Block) $1 != current_block){
3471 current_block.AddStatement ((Statement) $1);
3472 current_block = (Block) $1;
3475 | valid_declaration_statement
3477 current_block.AddStatement ((Statement) $1);
3482 valid_declaration_statement
3485 | expression_statement
3486 | selection_statement
3487 | iteration_statement
3491 | unchecked_statement
3499 : valid_declaration_statement
3500 | declaration_statement
3502 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3507 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3515 $$ = EmptyStatement.Value;
3522 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3524 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3525 current_block.AddStatement (labeled);
3530 declaration_statement
3531 : local_variable_declaration SEMICOLON
3534 DictionaryEntry de = (DictionaryEntry) $1;
3536 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3540 | local_constant_declaration SEMICOLON
3543 DictionaryEntry de = (DictionaryEntry) $1;
3545 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3551 * The following is from Rhys' grammar:
3552 * > Types in local variable declarations must be recognized as
3553 * > expressions to prevent reduce/reduce errors in the grammar.
3554 * > The expressions are converted into types during semantic analysis.
3557 : primary_expression opt_rank_specifier
3559 // FIXME: Do something smart here regarding the composition of the type.
3561 // Ok, the above "primary_expression" is there to get rid of
3562 // both reduce/reduce and shift/reduces in the grammar, it should
3563 // really just be "type_name". If you use type_name, a reduce/reduce
3564 // creeps up. If you use namespace_or_type_name (which is all we need
3565 // really) two shift/reduces appear.
3568 // So the super-trick is that primary_expression
3569 // can only be either a SimpleName or a MemberAccess.
3570 // The MemberAccess case arises when you have a fully qualified type-name like :
3572 // SimpleName is when you have
3575 Expression expr = (Expression) $1;
3576 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3577 Error_ExpectingTypeName (lexer.Location, expr);
3581 // So we extract the string corresponding to the SimpleName
3585 if ((string) $2 == "")
3588 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3591 | builtin_types opt_rank_specifier
3593 if ((string) $2 == "")
3596 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3600 local_variable_pointer_type
3601 : primary_expression STAR
3603 Expression expr = (Expression) $1;
3604 Location l = lexer.Location;
3606 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3607 Error_ExpectingTypeName (l, expr);
3611 $$ = new ComposedCast ((Expression) $1, "*", l);
3613 | builtin_types STAR
3615 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3619 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3621 | local_variable_pointer_type STAR
3623 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3627 local_variable_declaration
3628 : local_variable_type variable_declarators
3631 $$ = new DictionaryEntry ($1, $2);
3635 | local_variable_pointer_type opt_rank_specifier variable_declarators
3640 if ((string) $2 == "")
3641 t = (Expression) $1;
3643 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3644 $$ = new DictionaryEntry (t, $3);
3650 local_constant_declaration
3651 : CONST local_variable_type constant_declarators
3654 $$ = new DictionaryEntry ($2, $3);
3660 expression_statement
3661 : statement_expression SEMICOLON
3668 // We have to do the wrapping here and not in the case above,
3669 // because statement_expression is used for example in for_statement
3671 statement_expression
3672 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3673 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3674 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3675 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3676 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3677 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3678 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3680 Report.Error (1002, lexer.Location, "Expecting `;'");
3685 object_creation_expression
3686 : object_or_delegate_creation_expression
3687 { note ("complain if this is a delegate maybe?"); }
3696 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3699 Location l = (Location) $3;
3701 $$ = new If ((Expression) $4, (Statement) $6, l);
3703 if (RootContext.WarningLevel >= 4){
3704 if ($6 == EmptyStatement.Value)
3705 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3709 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3710 embedded_statement ELSE embedded_statement
3712 Location l = (Location) $3;
3714 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3719 : SWITCH OPEN_PARENS _mark_
3721 switch_stack.Push (current_block);
3723 expression CLOSE_PARENS
3726 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3727 current_block = (Block) switch_stack.Pop ();
3743 Report.Error (1522, lexer.Location, "Empty switch block");
3751 ArrayList sections = new ArrayList (4);
3756 | switch_sections switch_section
3758 ArrayList sections = (ArrayList) $1;
3768 current_block = current_block.CreateSwitchBlock (lexer.Location);
3772 Block topmost = current_block;
3774 while (topmost.Implicit)
3775 topmost = topmost.Parent;
3776 $$ = new SwitchSection ((ArrayList) $1, topmost);
3783 ArrayList labels = new ArrayList (4);
3788 | switch_labels switch_label
3790 ArrayList labels = (ArrayList) ($1);
3798 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3799 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3802 1523, lexer.Location,
3803 "The keyword case or default must precede code in switch block");
3815 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
3817 Location l = (Location) $3;
3818 $$ = new While ((Expression) $4, (Statement) $6, l);
3820 if (RootContext.WarningLevel >= 4){
3821 if ($6 == EmptyStatement.Value)
3822 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3828 : DO embedded_statement
3829 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
3831 Location l = (Location) $5;
3833 $$ = new Do ((Statement) $2, (Expression) $6, l);
3839 opt_for_initializer SEMICOLON _mark_
3841 Block assign_block = new Block (current_block);
3842 current_block = assign_block;
3844 if ($3 is DictionaryEntry){
3845 DictionaryEntry de = (DictionaryEntry) $3;
3847 Expression type = (Expression) de.Key;
3848 ArrayList var_declarators = (ArrayList) de.Value;
3850 foreach (VariableDeclaration decl in var_declarators){
3854 vi = current_block.AddVariable (
3855 type, decl.identifier, current_local_parameters, decl.Location);
3859 Location l = lexer.Location;
3861 if (decl.expression_or_array_initializer is Expression){
3862 expr = (Expression) decl.expression_or_array_initializer;
3863 } else if (decl.expression_or_array_initializer == null) {
3866 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3867 expr = new ArrayCreation (type, "", init, decl.Location);
3870 LocalVariableReference var;
3871 var = new LocalVariableReference (assign_block, decl.identifier, l);
3874 Assign a = new Assign (var, expr, decl.Location);
3876 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3880 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
3881 // This can be referred to as $6 below.
3887 opt_for_condition SEMICOLON
3888 opt_for_iterator CLOSE_PARENS
3891 Location l = (Location) $5;
3893 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
3895 if (RootContext.WarningLevel >= 4){
3896 if ($11 == EmptyStatement.Value)
3897 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3900 current_block.AddStatement (f);
3901 while (current_block.Implicit)
3902 current_block = current_block.Parent;
3904 current_block = current_block.Parent;
3909 : /* empty */ { $$ = EmptyStatement.Value; }
3914 : local_variable_declaration
3915 | statement_expression_list
3919 : /* empty */ { $$ = null; }
3920 | boolean_expression
3924 : /* empty */ { $$ = EmptyStatement.Value; }
3929 : statement_expression_list
3932 statement_expression_list
3933 : statement_expression
3935 // CHANGE: was `null'
3936 Block b = new Block (current_block, Block.Flags.Implicit);
3938 b.AddStatement ((Statement) $1);
3941 | statement_expression_list COMMA statement_expression
3943 Block b = (Block) $1;
3945 b.AddStatement ((Statement) $3);
3951 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3953 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3956 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
3957 expression CLOSE_PARENS
3959 Block foreach_block = new Block (current_block);
3960 current_block = foreach_block;
3962 Location l = lexer.Location;
3965 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3969 // Get a writable reference to this read-only variable.
3971 // Note that the $$ here refers to the value of _this_ code block,
3972 // not the value of the LHS non-terminal. This can be referred to as $9 below.
3973 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3980 LocalVariableReference v = (LocalVariableReference) $9;
3981 Location l = (Location) $6;
3984 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3985 current_block.AddStatement (f);
3988 while (current_block.Implicit)
3989 current_block = current_block.Parent;
3991 current_block = current_block.Parent;
3997 | continue_statement
4007 $$ = new Break (lexer.Location);
4012 : CONTINUE SEMICOLON
4014 $$ = new Continue (lexer.Location);
4019 : GOTO IDENTIFIER SEMICOLON
4021 $$ = new Goto (current_block, (string) $2, lexer.Location);
4023 | GOTO CASE constant_expression SEMICOLON
4025 $$ = new GotoCase ((Expression) $3, lexer.Location);
4027 | GOTO DEFAULT SEMICOLON
4029 $$ = new GotoDefault (lexer.Location);
4034 : RETURN opt_expression SEMICOLON
4036 $$ = new Return ((Expression) $2, lexer.Location);
4041 : THROW opt_expression SEMICOLON
4043 $$ = new Throw ((Expression) $2, lexer.Location);
4048 : IDENTIFIER RETURN expression SEMICOLON
4050 string s = (string) $1;
4052 Report.Error (1003, lexer.Location, "; expected");
4055 if (RootContext.Version == LanguageVersion.ISO_1){
4056 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4059 if (iterator_container == null){
4060 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4063 iterator_container.SetYields ();
4064 $$ = new Yield ((Expression) $3, lexer.Location);
4067 | IDENTIFIER BREAK SEMICOLON
4069 string s = (string) $1;
4071 Report.Error (1003, lexer.Location, "; expected");
4074 if (RootContext.Version == LanguageVersion.ISO_1){
4075 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4078 if (iterator_container == null){
4079 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4082 iterator_container.SetYields ();
4083 $$ = new YieldBreak (lexer.Location);
4094 : TRY block catch_clauses
4098 ArrayList c = (ArrayList)$3;
4099 for (int i = 0; i < c.Count; ++i) {
4100 Catch cc = (Catch) c [i];
4102 if (i != c.Count - 1)
4103 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4110 // Now s contains the list of specific catch clauses
4111 // and g contains the general one.
4113 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4115 | TRY block opt_catch_clauses FINALLY block
4118 ArrayList s = new ArrayList (4);
4119 ArrayList catch_list = (ArrayList) $3;
4121 if (catch_list != null){
4122 foreach (Catch cc in catch_list) {
4130 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4134 Report.Error (1524, lexer.Location, "Expected catch or finally");
4139 : /* empty */ { $$ = null; }
4146 ArrayList l = new ArrayList (4);
4151 | catch_clauses catch_clause
4153 ArrayList l = (ArrayList) $1;
4161 : /* empty */ { $$ = null; }
4166 : CATCH opt_catch_args
4168 Expression type = null;
4172 DictionaryEntry cc = (DictionaryEntry) $2;
4173 type = (Expression) cc.Key;
4174 id = (string) cc.Value;
4177 ArrayList one = new ArrayList (4);
4178 Location loc = lexer.Location;
4180 one.Add (new VariableDeclaration (id, null, loc));
4182 current_block = new Block (current_block);
4183 Block b = declare_local_variables (type, one, loc);
4188 Expression type = null;
4192 DictionaryEntry cc = (DictionaryEntry) $2;
4193 type = (Expression) cc.Key;
4194 id = (string) cc.Value;
4197 while (current_block.Implicit)
4198 current_block = current_block.Parent;
4199 current_block = current_block.Parent;
4203 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4208 : /* empty */ { $$ = null; }
4213 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4215 $$ = new DictionaryEntry ($2, $3);
4222 $$ = new Checked ((Block) $2);
4229 $$ = new Unchecked ((Block) $2);
4236 if (!RootContext.Unsafe){
4237 Report.Error (227, lexer.Location,
4238 "Unsafe code can only be used if --unsafe is used");
4241 $$ = new Unsafe ((Block) $3);
4247 type fixed_pointer_declarators
4250 ArrayList list = (ArrayList) $4;
4251 Expression type = (Expression) $3;
4252 Location l = lexer.Location;
4253 int top = list.Count;
4255 Block assign_block = new Block (current_block);
4256 current_block = assign_block;
4258 for (int i = 0; i < top; i++){
4259 Pair p = (Pair) list [i];
4262 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4274 Location l = (Location) $6;
4276 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4278 if (RootContext.WarningLevel >= 4){
4279 if ($8 == EmptyStatement.Value)
4280 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4283 current_block.AddStatement (f);
4284 while (current_block.Implicit)
4285 current_block = current_block.Parent;
4287 current_block = current_block.Parent;
4291 fixed_pointer_declarators
4292 : fixed_pointer_declarator {
4293 ArrayList declarators = new ArrayList (4);
4295 declarators.Add ($1);
4298 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4300 ArrayList declarators = (ArrayList) $1;
4302 declarators.Add ($3);
4307 fixed_pointer_declarator
4308 : IDENTIFIER ASSIGN expression
4310 $$ = new Pair ($1, $3);
4314 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4320 : LOCK OPEN_PARENS expression CLOSE_PARENS
4326 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4331 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4333 Block assign_block = new Block (current_block);
4334 current_block = assign_block;
4336 if ($3 is DictionaryEntry){
4337 DictionaryEntry de = (DictionaryEntry) $3;
4338 Location l = lexer.Location;
4340 Expression type = (Expression) de.Key;
4341 ArrayList var_declarators = (ArrayList) de.Value;
4343 ArrayList vars = new ArrayList (4);
4345 foreach (VariableDeclaration decl in var_declarators){
4347 LocalInfo vi = current_block.AddVariable (
4348 type, decl.identifier,
4349 current_local_parameters, decl.Location);
4355 if (decl.expression_or_array_initializer is Expression){
4356 expr = (Expression) decl.expression_or_array_initializer;
4358 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4360 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4363 expr = new ArrayCreation (type, "", init, decl.Location);
4366 LocalVariableReference var;
4368 // Get a writable reference to this read-only variable.
4369 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4371 // This is so that it is not a warning on using variables
4374 vars.Add (new DictionaryEntry (var, expr));
4376 // Assign a = new Assign (var, expr, decl.Location);
4377 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4380 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4381 // It can be referred to as $6 below.
4382 $$ = new DictionaryEntry (type, vars);
4389 Using u = new Using ($6, (Statement) $7, (Location) $5);
4390 current_block.AddStatement (u);
4391 while (current_block.Implicit)
4392 current_block = current_block.Parent;
4394 current_block = current_block.Parent;
4398 resource_acquisition
4399 : local_variable_declaration
4403 // Utility rule to save location information
4406 { $$ = lexer.Location; }
4411 // A class used to pass around variable declarations and constants
4413 public class VariableDeclaration {
4414 public string identifier;
4415 public object expression_or_array_initializer;
4416 public Location Location;
4417 public Attributes OptAttributes;
4418 public string DocComment;
4420 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4422 this.identifier = id;
4423 this.expression_or_array_initializer = eoai;
4425 this.OptAttributes = opt_attrs;
4428 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4434 /// Used to pass around interface property information
4436 public class InterfaceAccessorInfo {
4438 public readonly Accessor Get, Set;
4440 public InterfaceAccessorInfo (bool has_get, bool has_set,
4441 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4444 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4446 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4449 Get = new Accessor (null, 0, get_attrs, get_loc);
4451 Set = new Accessor (null, 0, set_attrs, set_loc);
4458 // A class used to hold info about an indexer declarator
4460 public class IndexerDeclaration {
4461 public Expression type;
4462 public MemberName interface_type;
4463 public Parameters param_list;
4465 public IndexerDeclaration (Expression type, MemberName interface_type,
4466 Parameters param_list)
4469 this.interface_type = interface_type;
4470 this.param_list = param_list;
4475 // We use this when we do not have an object in advance that is an IIteratorContainer
4477 public class SimpleIteratorContainer : IIteratorContainer {
4480 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4485 public static SimpleIteratorContainer GetSimple () {
4486 Simple.Yields = false;
4490 public void SetYields () { Yields = true; }
4494 // A class used to hold info about an operator declarator
4496 public class OperatorDeclaration {
4497 public Operator.OpType optype;
4498 public Expression ret_type, arg1type, arg2type;
4499 public string arg1name, arg2name;
4500 public Location location;
4502 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4503 Expression arg1type, string arg1name,
4504 Expression arg2type, string arg2name, Location location)
4507 this.ret_type = ret_type;
4508 this.arg1type = arg1type;
4509 this.arg1name = arg1name;
4510 this.arg2type = arg2type;
4511 this.arg2name = arg2name;
4512 this.location = location;
4517 void Error_ExpectingTypeName (Location l, Expression expr)
4519 if (expr is Invocation){
4520 Report.Error (1002, l, "; expected");
4522 Report.Error (-1, l, "Invalid Type definition");
4527 // Given the @class_name name, it creates a fully qualified name
4528 // based on the containing declaration space
4531 MakeName (MemberName class_name)
4533 string ns = current_namespace.FullName;
4535 if (current_container.Name == ""){
4537 return new MemberName (new MemberName (ns), class_name);
4541 return new MemberName (current_container.MemberName, class_name);
4545 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4547 Block implicit_block;
4548 ArrayList inits = null;
4551 // We use the `Used' property to check whether statements
4552 // have been added to the current block. If so, we need
4553 // to create another block to contain the new declaration
4554 // otherwise, as an optimization, we use the same block to
4555 // add the declaration.
4557 // FIXME: A further optimization is to check if the statements
4558 // that were added were added as part of the initialization
4559 // below. In which case, no other statements have been executed
4560 // and we might be able to reduce the number of blocks for
4561 // situations like this:
4563 // int j = 1; int k = j + 1;
4565 if (current_block.Used)
4566 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4568 implicit_block = current_block;
4570 foreach (VariableDeclaration decl in variable_declarators){
4572 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4573 if (decl.expression_or_array_initializer != null){
4575 inits = new ArrayList (4);
4582 return implicit_block;
4584 foreach (VariableDeclaration decl in inits){
4588 if (decl.expression_or_array_initializer is Expression){
4589 expr = (Expression) decl.expression_or_array_initializer;
4592 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4594 expr = new ArrayCreation (type, "", init, decl.Location);
4597 LocalVariableReference var;
4598 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4600 assign = new Assign (var, expr, decl.Location);
4602 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4605 return implicit_block;
4608 Block declare_local_constants (Expression type, ArrayList declarators)
4610 Block implicit_block;
4612 if (current_block.Used)
4613 implicit_block = new Block (current_block, Block.Flags.Implicit);
4615 implicit_block = current_block;
4617 foreach (VariableDeclaration decl in declarators){
4618 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4619 current_local_parameters, decl.Location);
4622 return implicit_block;
4625 void CheckAttributeTarget (string a)
4629 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4633 Location l = lexer.Location;
4634 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4640 void CheckUnaryOperator (Operator.OpType op)
4644 case Operator.OpType.LogicalNot:
4645 case Operator.OpType.OnesComplement:
4646 case Operator.OpType.Increment:
4647 case Operator.OpType.Decrement:
4648 case Operator.OpType.True:
4649 case Operator.OpType.False:
4650 case Operator.OpType.Addition:
4651 case Operator.OpType.Subtraction:
4656 Location l = lexer.Location;
4657 Report.Error (1019, l, "Overloadable unary operator expected");
4663 void CheckBinaryOperator (Operator.OpType op)
4667 case Operator.OpType.Addition:
4668 case Operator.OpType.Subtraction:
4669 case Operator.OpType.Multiply:
4670 case Operator.OpType.Division:
4671 case Operator.OpType.Modulus:
4672 case Operator.OpType.BitwiseAnd:
4673 case Operator.OpType.BitwiseOr:
4674 case Operator.OpType.ExclusiveOr:
4675 case Operator.OpType.LeftShift:
4676 case Operator.OpType.RightShift:
4677 case Operator.OpType.Equality:
4678 case Operator.OpType.Inequality:
4679 case Operator.OpType.GreaterThan:
4680 case Operator.OpType.LessThan:
4681 case Operator.OpType.GreaterThanOrEqual:
4682 case Operator.OpType.LessThanOrEqual:
4686 Location l = lexer.Location;
4687 Report.Error (1020, l, "Overloadable binary operator expected");
4693 void syntax_error (Location l, string msg)
4695 Report.Error (1003, l, "Syntax error, " + msg);
4698 void output (string s)
4700 Console.WriteLine (s);
4703 void note (string s)
4705 // Used to put annotations
4710 public Tokenizer Lexer {
4716 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4718 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4719 this.name = file.Name;
4721 current_container = RootContext.Tree.Types;
4722 current_container.NamespaceEntry = current_namespace;
4723 oob_stack = new Stack ();
4724 switch_stack = new Stack ();
4726 lexer = new Tokenizer (reader, file, defines);
4729 public void parse ()
4732 if (yacc_verbose_flag > 1)
4733 yyparse (lexer, new yydebug.yyDebugSimple ());
4736 Tokenizer tokenizer = lexer as Tokenizer;
4737 tokenizer.cleanup ();
4738 } catch (Exception e){
4740 // Removed for production use, use parser verbose to get the output.
4742 // Console.WriteLine (e);
4743 Report.Error (-25, lexer.Location, "Parsing error");
4744 if (yacc_verbose_flag > 0)
4745 Console.WriteLine (e);
4749 void CheckToken (int error, int yyToken, string msg)
4751 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4752 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4755 Report.Error (error, lexer.Location, msg);
4758 void CheckIdentifierToken (int yyToken)
4760 CheckToken (1041, yyToken, "Identifier expected");
4763 string ConsumeStoredComment ()
4765 string s = tmpComment;
4767 Lexer.doc_state = XmlCommentState.Allowed;