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 MemberName mname = (MemberName) $1;
615 string name = mname.GetName ();
616 if (current_attr_target == "assembly" || current_attr_target == "module")
617 $$ = new GlobalAttribute (current_container, current_attr_target,
618 name, (ArrayList) $3, (Location) $2);
620 $$ = new Attribute (current_attr_target, name, (ArrayList) $3,
626 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
629 opt_attribute_arguments
630 : /* empty */ { $$ = null; }
631 | OPEN_PARENS attribute_arguments CLOSE_PARENS
639 : opt_positional_argument_list
644 ArrayList args = new ArrayList (4);
650 | positional_argument_list COMMA named_argument_list
652 ArrayList args = new ArrayList (4);
658 | named_argument_list
660 ArrayList args = new ArrayList (4);
669 opt_positional_argument_list
670 : /* empty */ { $$ = null; }
671 | positional_argument_list
674 positional_argument_list
677 ArrayList args = new ArrayList (4);
678 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
682 | positional_argument_list COMMA expression
684 ArrayList args = (ArrayList) $1;
685 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
694 ArrayList args = new ArrayList (4);
699 | named_argument_list COMMA named_argument
701 ArrayList args = (ArrayList) $1;
706 | named_argument_list COMMA expression
708 Report.Error (1016, lexer.Location, "Named attribute argument expected");
714 : IDENTIFIER ASSIGN expression
716 $$ = new DictionaryEntry (
718 new Argument ((Expression) $3, Argument.AType.Expression));
724 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
727 opt_class_member_declarations
729 | class_member_declarations
732 class_member_declarations
733 : class_member_declaration
734 | class_member_declarations
735 class_member_declaration
738 class_member_declaration
739 : constant_declaration // done
740 | field_declaration // done
741 | method_declaration // done
742 | property_declaration // done
743 | event_declaration // done
744 | indexer_declaration // done
745 | operator_declaration // done
746 | constructor_declaration // done
747 | destructor_declaration // done
757 MemberName name = MakeName ((MemberName) $5);
758 bool partial = (bool) $3;
761 ClassPart part = PartialContainer.CreatePart (
762 current_namespace, current_container, name, (int) $2,
763 (Attributes) $1, Kind.Struct, lexer.Location);
765 current_container = part.PartialContainer;
766 current_class = part;
768 current_class = new Struct (
769 current_namespace, current_container, name, (int) $2,
770 (Attributes) $1, lexer.Location);
772 current_container = current_class;
773 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
779 current_class.Bases = (ArrayList) $7;
781 if (RootContext.Documentation != null)
782 current_class.DocComment = Lexer.consume_doc_comment ();
784 current_class.Register ();
788 if (RootContext.Documentation != null)
789 Lexer.doc_state = XmlCommentState.Allowed;
795 current_container = current_container.Parent;
796 current_class = current_container;
798 | opt_attributes opt_modifiers opt_partial STRUCT error {
799 CheckIdentifierToken (yyToken);
806 if (RootContext.Documentation != null)
807 Lexer.doc_state = XmlCommentState.Allowed;
809 opt_struct_member_declarations CLOSE_BRACE
812 opt_struct_member_declarations
814 | struct_member_declarations
817 struct_member_declarations
818 : struct_member_declaration
819 | struct_member_declarations struct_member_declaration
822 struct_member_declaration
823 : constant_declaration
826 | property_declaration
828 | indexer_declaration
829 | operator_declaration
830 | constructor_declaration
834 * This is only included so we can flag error 575:
835 * destructors only allowed on class types
837 | destructor_declaration
848 int modflags = (int) $2;
849 foreach (VariableDeclaration constant in (ArrayList) $5){
850 Location l = constant.Location;
851 if ((modflags & Modifiers.STATIC) != 0) {
852 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
856 Const c = new Const (
857 current_class, (Expression) $4, (string) constant.identifier,
858 (Expression) constant.expression_or_array_initializer, modflags,
861 if (RootContext.Documentation != null) {
862 c.DocComment = Lexer.consume_doc_comment ();
863 Lexer.doc_state = XmlCommentState.Allowed;
865 current_container.AddConstant (c);
871 : constant_declarator
873 ArrayList constants = new ArrayList (4);
878 | constant_declarators COMMA constant_declarator
881 ArrayList constants = (ArrayList) $1;
888 : IDENTIFIER ASSIGN constant_expression
890 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
894 // A const field requires a value to be provided
895 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
907 Expression type = (Expression) $3;
910 foreach (VariableDeclaration var in (ArrayList) $4){
911 Location l = var.Location;
913 Field field = new Field (current_class, type, mod, var.identifier,
914 var.expression_or_array_initializer,
917 if (RootContext.Documentation != null) {
918 field.DocComment = Lexer.consume_doc_comment ();
919 Lexer.doc_state = XmlCommentState.Allowed;
921 current_container.AddField (field);
929 Report.Error (670, lexer.Location, "void type is not allowed for fields");
934 : variable_declarator
936 ArrayList decl = new ArrayList (4);
940 | variable_declarators COMMA variable_declarator
942 ArrayList decls = (ArrayList) $1;
949 : IDENTIFIER ASSIGN variable_initializer
951 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
955 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
968 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
970 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
974 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
981 iterator_container = (IIteratorContainer) $1;
982 if (RootContext.Documentation != null)
983 Lexer.doc_state = XmlCommentState.NotAllowed;
987 Method method = (Method) $1;
988 Block b = (Block) $3;
989 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
992 if ((method.ModFlags & extern_abstract) == 0){
994 501, lexer.Location, current_container.MakeName (method.Name) +
995 "must declare a body because it is not marked abstract or extern");
998 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1000 179, lexer.Location, current_container.MakeName (method.Name) +
1001 " is declared extern, but has a body");
1005 method.Block = (ToplevelBlock) $3;
1006 current_container.AddMethod (method);
1008 current_local_parameters = null;
1009 iterator_container = null;
1011 if (RootContext.Documentation != null)
1012 Lexer.doc_state = XmlCommentState.Allowed;
1026 1585, lexer.Location, "Member modifier `" +
1027 Modifiers.Name (i) + "' must precede member type and name");
1038 type namespace_or_type_name
1039 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1041 MemberName name = (MemberName) $4;
1043 Method method = new Method (current_class, (Expression) $3, (int) $2,
1044 false, name, (Parameters) $6, (Attributes) $1,
1047 current_local_parameters = (Parameters) $6;
1049 if (RootContext.Documentation != null)
1050 method.DocComment = Lexer.consume_doc_comment ();
1056 VOID namespace_or_type_name
1057 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1059 MemberName name = (MemberName) $4;
1061 Method method = new Method (current_class, TypeManager.system_void_expr,
1062 (int) $2, false, name, (Parameters) $6,
1063 (Attributes) $1, lexer.Location);
1065 current_local_parameters = (Parameters) $6;
1067 if (RootContext.Documentation != null)
1068 method.DocComment = Lexer.consume_doc_comment ();
1075 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1077 Report.Error (1585, lexer.Location,
1078 String.Format ("Modifier {0} should appear before type",
1079 Modifiers.Name ((int) $4)));
1080 MemberName name = (MemberName) $4;
1082 Method method = new Method (current_class, TypeManager.system_void_expr,
1083 0, false, name, (Parameters) $7, (Attributes) $1,
1086 current_local_parameters = (Parameters) $7;
1088 if (RootContext.Documentation != null)
1089 method.DocComment = Lexer.consume_doc_comment ();
1097 | SEMICOLON { $$ = null; }
1100 opt_formal_parameter_list
1101 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1102 | formal_parameter_list
1105 formal_parameter_list
1108 ArrayList pars_list = (ArrayList) $1;
1110 Parameter [] pars = new Parameter [pars_list.Count];
1111 pars_list.CopyTo (pars);
1113 $$ = new Parameters (pars, null, lexer.Location);
1115 | fixed_parameters COMMA parameter_array
1117 ArrayList pars_list = (ArrayList) $1;
1119 Parameter [] pars = new Parameter [pars_list.Count];
1120 pars_list.CopyTo (pars);
1122 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1124 | fixed_parameters COMMA ARGLIST
1126 ArrayList pars_list = (ArrayList) $1;
1128 Parameter [] pars = new Parameter [pars_list.Count];
1129 pars_list.CopyTo (pars);
1131 $$ = new Parameters (pars, true, lexer.Location);
1133 | parameter_array COMMA fixed_parameters
1135 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1138 | ARGLIST COMMA fixed_parameters
1140 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1145 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1149 $$ = new Parameters (null, true, lexer.Location);
1156 ArrayList pars = new ArrayList (4);
1161 | fixed_parameters COMMA fixed_parameter
1163 ArrayList pars = (ArrayList) $1;
1172 opt_parameter_modifier
1176 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1179 opt_parameter_modifier
1182 Report.Error (1001, lexer.Location, "Identifier expected");
1186 opt_parameter_modifier
1189 CheckIdentifierToken (yyToken);
1193 opt_parameter_modifier
1199 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1204 opt_parameter_modifier
1205 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1206 | parameter_modifier
1210 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1211 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1215 : opt_attributes PARAMS type IDENTIFIER
1217 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1218 note ("type must be a single-dimension array type");
1220 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1222 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1225 | opt_attributes PARAMS type error {
1226 CheckIdentifierToken (yyToken);
1231 property_declaration
1235 namespace_or_type_name
1237 if (RootContext.Documentation != null)
1238 tmpComment = Lexer.consume_doc_comment ();
1242 implicit_value_parameter_type = (Expression) $3;
1244 lexer.PropertyParsing = true;
1246 $$ = lexer.Location;
1248 iterator_container = SimpleIteratorContainer.GetSimple ();
1250 accessor_declarations
1252 lexer.PropertyParsing = false;
1257 Pair pair = (Pair) $8;
1258 Accessor get_block = (Accessor) pair.First;
1259 Accessor set_block = (Accessor) pair.Second;
1261 Location loc = (Location) $7;
1262 MemberName name = (MemberName) $4;
1264 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1265 name, (Attributes) $1, get_block, set_block, loc);
1266 if (SimpleIteratorContainer.Simple.Yields)
1269 current_container.AddProperty (prop);
1270 implicit_value_parameter_type = null;
1271 iterator_container = null;
1273 if (RootContext.Documentation != null)
1274 prop.DocComment = ConsumeStoredComment ();
1279 accessor_declarations
1280 : get_accessor_declaration opt_set_accessor_declaration
1282 $$ = new Pair ($1, $2);
1284 | set_accessor_declaration opt_get_accessor_declaration
1286 $$ = new Pair ($2, $1);
1290 opt_get_accessor_declaration
1291 : /* empty */ { $$ = null; }
1292 | get_accessor_declaration
1295 opt_set_accessor_declaration
1296 : /* empty */ { $$ = null; }
1297 | set_accessor_declaration
1300 get_accessor_declaration
1301 : opt_attributes opt_modifiers GET
1303 // If this is not the case, then current_local_parameters has already
1304 // been set in indexer_declaration
1305 if (parsing_indexer == false)
1306 current_local_parameters = null;
1308 current_local_parameters = indexer_parameters;
1309 lexer.PropertyParsing = false;
1313 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1314 current_local_parameters = null;
1315 lexer.PropertyParsing = true;
1317 if (RootContext.Documentation != null)
1318 if (Lexer.doc_state == XmlCommentState.Error)
1319 Lexer.doc_state = XmlCommentState.NotAllowed;
1323 set_accessor_declaration
1324 : opt_attributes opt_modifiers SET
1327 Parameter implicit_value_parameter = new Parameter (
1328 implicit_value_parameter_type, "value",
1329 Parameter.Modifier.NONE, null);
1331 if (parsing_indexer == false) {
1332 args = new Parameter [1];
1333 args [0] = implicit_value_parameter;
1334 current_local_parameters = new Parameters (args, null, lexer.Location);
1336 Parameter [] fpars = indexer_parameters.FixedParameters;
1339 int count = fpars.Length;
1341 args = new Parameter [count + 1];
1342 fpars.CopyTo (args, 0);
1343 args [count] = implicit_value_parameter;
1346 current_local_parameters = new Parameters (
1347 args, indexer_parameters.ArrayParameter, lexer.Location);
1350 lexer.PropertyParsing = false;
1354 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1355 current_local_parameters = null;
1356 lexer.PropertyParsing = true;
1358 if (RootContext.Documentation != null
1359 && Lexer.doc_state == XmlCommentState.Error)
1360 Lexer.doc_state = XmlCommentState.NotAllowed;
1366 | SEMICOLON { $$ = null; }
1369 interface_declaration
1373 INTERFACE member_name
1375 MemberName name = MakeName ((MemberName) $5);
1376 bool partial = (bool) $3;
1379 ClassPart part = PartialContainer.CreatePart (
1380 current_namespace, current_container, name, (int) $2,
1381 (Attributes) $1, Kind.Interface, lexer.Location);
1383 current_container = part.PartialContainer;
1384 current_class = part;
1386 current_class = new Interface (
1387 current_namespace, current_container, name, (int) $2,
1388 (Attributes) $1, lexer.Location);
1390 current_container = current_class;
1391 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1396 current_class.Bases = (ArrayList) $7;
1398 if (RootContext.Documentation != null) {
1399 current_class.DocComment = Lexer.consume_doc_comment ();
1400 Lexer.doc_state = XmlCommentState.Allowed;
1403 current_class.Register ();
1407 if (RootContext.Documentation != null)
1408 Lexer.doc_state = XmlCommentState.Allowed;
1414 current_container = current_container.Parent;
1415 current_class = current_container;
1417 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1418 CheckIdentifierToken (yyToken);
1424 opt_interface_member_declarations
1428 opt_interface_member_declarations
1430 | interface_member_declarations
1433 interface_member_declarations
1434 : interface_member_declaration
1435 | interface_member_declarations interface_member_declaration
1438 interface_member_declaration
1439 : interface_method_declaration
1441 Method m = (Method) $1;
1443 if (m.IsExplicitImpl)
1444 Report.Error (541, lexer.Location,
1445 "Explicit interface declaration can only be declared in a class or struct");
1447 current_container.AddMethod (m);
1449 if (RootContext.Documentation != null)
1450 Lexer.doc_state = XmlCommentState.Allowed;
1452 | interface_property_declaration
1454 Property p = (Property) $1;
1456 if (p.IsExplicitImpl)
1457 Report.Error (541, lexer.Location,
1458 "Explicit interface declaration can only be declared in a class or struct");
1460 current_container.AddProperty (p);
1462 if (RootContext.Documentation != null)
1463 Lexer.doc_state = XmlCommentState.Allowed;
1465 | interface_event_declaration
1468 Event e = (Event) $1;
1470 if (e.IsExplicitImpl)
1471 Report.Error (541, lexer.Location,
1472 "Explicit interface declaration can only be declared in a class or struct");
1474 current_container.AddEvent (e);
1477 if (RootContext.Documentation != null)
1478 Lexer.doc_state = XmlCommentState.Allowed;
1480 | interface_indexer_declaration
1482 Indexer i = (Indexer) $1;
1484 if (i.IsExplicitImpl)
1485 Report.Error (541, lexer.Location,
1486 "Explicit interface declaration can only be declared in a class or struct");
1488 current_container.AddIndexer (i);
1490 if (RootContext.Documentation != null)
1491 Lexer.doc_state = XmlCommentState.Allowed;
1499 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1504 interface_method_declaration
1505 : opt_attributes opt_new type namespace_or_type_name
1506 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1509 MemberName name = (MemberName) $4;
1511 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1512 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1513 if (RootContext.Documentation != null)
1514 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1516 | opt_attributes opt_new VOID namespace_or_type_name
1517 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1520 MemberName name = (MemberName) $4;
1522 $$ = new Method (current_class, TypeManager.system_void_expr, (int) $2,
1523 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1524 if (RootContext.Documentation != null)
1525 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1529 interface_property_declaration
1534 { lexer.PropertyParsing = true; }
1536 { lexer.PropertyParsing = false; }
1539 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1541 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1542 new MemberName ((string) $4), (Attributes) $1,
1543 pinfo.Get, pinfo.Set, lexer.Location);
1544 if (RootContext.Documentation != null)
1545 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1550 CheckIdentifierToken (yyToken);
1556 : opt_attributes opt_modifiers GET SEMICOLON
1557 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1558 | opt_attributes opt_modifiers SET SEMICOLON
1559 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1560 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1561 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1562 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1563 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1566 interface_event_declaration
1567 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1569 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1570 new MemberName ((string) $5), null,
1571 (Attributes) $1, lexer.Location);
1572 if (RootContext.Documentation != null)
1573 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1575 | opt_attributes opt_new EVENT type error {
1576 CheckIdentifierToken (yyToken);
1579 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1580 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1583 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1584 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1589 interface_indexer_declaration
1590 : opt_attributes opt_new type THIS
1591 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1593 { lexer.PropertyParsing = true; }
1595 { lexer.PropertyParsing = false; }
1598 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1600 $$ = new Indexer (current_class, (Expression) $3,
1601 new MemberName (TypeContainer.DefaultIndexerName),
1602 (int) $2, true, (Parameters) $6, (Attributes) $1,
1603 info.Get, info.Set, lexer.Location);
1604 if (RootContext.Documentation != null)
1605 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1609 operator_declaration
1610 : opt_attributes opt_modifiers operator_declarator
1612 iterator_container = SimpleIteratorContainer.GetSimple ();
1616 OperatorDeclaration decl = (OperatorDeclaration) $3;
1618 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1620 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1621 if (decl.arg2type != null)
1622 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1624 Operator op = new Operator (
1625 current_class, decl.optype, decl.ret_type, (int) $2,
1626 new Parameters (param_list, null, decl.location),
1627 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1629 if (RootContext.Documentation != null) {
1630 op.DocComment = tmpComment;
1631 Lexer.doc_state = XmlCommentState.Allowed;
1634 if (SimpleIteratorContainer.Simple.Yields)
1637 // Note again, checking is done in semantic analysis
1638 current_container.AddOperator (op);
1640 current_local_parameters = null;
1641 iterator_container = null;
1647 | SEMICOLON { $$ = null; }
1650 : type OPERATOR overloadable_operator
1651 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1653 Operator.OpType op = (Operator.OpType) $3;
1654 CheckUnaryOperator (op);
1656 if (op == Operator.OpType.Addition)
1657 op = Operator.OpType.UnaryPlus;
1659 if (op == Operator.OpType.Subtraction)
1660 op = Operator.OpType.UnaryNegation;
1662 Parameter [] pars = new Parameter [1];
1663 Expression type = (Expression) $5;
1665 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1667 current_local_parameters = new Parameters (pars, null, lexer.Location);
1669 if (RootContext.Documentation != null) {
1670 tmpComment = Lexer.consume_doc_comment ();
1671 Lexer.doc_state = XmlCommentState.NotAllowed;
1674 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1675 null, null, lexer.Location);
1677 | type OPERATOR overloadable_operator
1679 type IDENTIFIER COMMA
1683 CheckBinaryOperator ((Operator.OpType) $3);
1685 Parameter [] pars = new Parameter [2];
1687 Expression typeL = (Expression) $5;
1688 Expression typeR = (Expression) $8;
1690 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1691 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1693 current_local_parameters = new Parameters (pars, null, lexer.Location);
1695 if (RootContext.Documentation != null) {
1696 tmpComment = Lexer.consume_doc_comment ();
1697 Lexer.doc_state = XmlCommentState.NotAllowed;
1700 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1702 typeR, (string) $9, lexer.Location);
1704 | conversion_operator_declarator
1707 overloadable_operator
1709 : BANG { $$ = Operator.OpType.LogicalNot; }
1710 | TILDE { $$ = Operator.OpType.OnesComplement; }
1711 | OP_INC { $$ = Operator.OpType.Increment; }
1712 | OP_DEC { $$ = Operator.OpType.Decrement; }
1713 | TRUE { $$ = Operator.OpType.True; }
1714 | FALSE { $$ = Operator.OpType.False; }
1715 // Unary and binary:
1716 | PLUS { $$ = Operator.OpType.Addition; }
1717 | MINUS { $$ = Operator.OpType.Subtraction; }
1719 | STAR { $$ = Operator.OpType.Multiply; }
1720 | DIV { $$ = Operator.OpType.Division; }
1721 | PERCENT { $$ = Operator.OpType.Modulus; }
1722 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1723 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1724 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1725 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1726 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1727 | OP_EQ { $$ = Operator.OpType.Equality; }
1728 | OP_NE { $$ = Operator.OpType.Inequality; }
1729 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1730 | OP_LT { $$ = Operator.OpType.LessThan; }
1731 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1732 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1735 conversion_operator_declarator
1736 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1738 Parameter [] pars = new Parameter [1];
1740 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1742 current_local_parameters = new Parameters (pars, null, lexer.Location);
1744 if (RootContext.Documentation != null) {
1745 tmpComment = Lexer.consume_doc_comment ();
1746 Lexer.doc_state = XmlCommentState.NotAllowed;
1749 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1750 null, null, lexer.Location);
1752 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1754 Parameter [] pars = new Parameter [1];
1756 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1758 current_local_parameters = new Parameters (pars, null, lexer.Location);
1760 if (RootContext.Documentation != null) {
1761 tmpComment = Lexer.consume_doc_comment ();
1762 Lexer.doc_state = XmlCommentState.NotAllowed;
1765 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1766 null, null, lexer.Location);
1770 syntax_error (lexer.Location, "'operator' expected");
1774 syntax_error (lexer.Location, "'operator' expected");
1778 constructor_declaration
1781 constructor_declarator
1784 Constructor c = (Constructor) $3;
1785 c.Block = (ToplevelBlock) $4;
1786 c.OptAttributes = (Attributes) $1;
1787 c.ModFlags = (int) $2;
1789 if (RootContext.Documentation != null)
1790 c.DocComment = ConsumeStoredComment ();
1792 if (c.Name == current_container.Basename){
1793 if ((c.ModFlags & Modifiers.STATIC) != 0){
1794 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1796 515, c.Location, String.Format (
1797 "`{0}.{1}': static constructor can not have access modifiers",
1798 c.Name, current_container.Name));
1801 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1803 if (c.Initializer != null){
1806 "Static constructors can not have an explicit this or base " +
1807 "constructor invocations");
1810 if (!c.Parameters.Empty){
1812 132, c.Location, "Static constructors should not have parameters");
1815 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1818 // We let another layer check the validity of the constructor.
1819 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1822 current_container.AddConstructor (c);
1824 current_local_parameters = null;
1825 if (RootContext.Documentation != null)
1826 Lexer.doc_state = XmlCommentState.Allowed;
1830 constructor_declarator
1833 if (RootContext.Documentation != null) {
1834 tmpComment = Lexer.consume_doc_comment ();
1835 Lexer.doc_state = XmlCommentState.Allowed;
1838 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1840 oob_stack.Push (lexer.Location);
1842 current_local_parameters = (Parameters) $4;
1844 opt_constructor_initializer
1846 Location l = (Location) oob_stack.Pop ();
1847 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1848 (ConstructorInitializer) $7, l);
1854 | SEMICOLON { $$ = null; }
1857 opt_constructor_initializer
1858 : /* empty */ { $$ = null; }
1859 | constructor_initializer
1862 constructor_initializer
1863 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1865 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1867 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1869 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1872 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1878 : /* EMPTY */ { $$ = 0; }
1879 | UNSAFE { $$ = Modifiers.UNSAFE; }
1880 | EXTERN { $$ = Modifiers.EXTERN; }
1883 destructor_declaration
1884 : opt_attributes opt_finalizer TILDE
1886 if (RootContext.Documentation != null) {
1887 tmpComment = Lexer.consume_doc_comment ();
1888 Lexer.doc_state = XmlCommentState.NotAllowed;
1891 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1893 if ((string) $5 != current_container.Basename){
1894 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1895 } else if (!(current_container is Class)){
1896 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1898 Location l = lexer.Location;
1901 if (!RootContext.StdLib && current_container.Name == "System.Object")
1902 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
1904 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1906 if ((m & Modifiers.UNSAFE) != 0){
1907 if (!RootContext.Unsafe){
1908 Report.Error (227, l,
1909 "Unsafe code requires the -unsafe command " +
1910 "line option to be specified");
1914 Method d = new Destructor (
1915 current_class, TypeManager.system_void_expr, m, "Finalize",
1916 new Parameters (null, null, l), (Attributes) $1, l);
1917 if (RootContext.Documentation != null)
1918 d.DocComment = ConsumeStoredComment ();
1920 d.Block = (ToplevelBlock) $8;
1921 current_container.AddMethod (d);
1929 EVENT type variable_declarators SEMICOLON
1931 foreach (VariableDeclaration var in (ArrayList) $5) {
1933 MemberName name = new MemberName (var.identifier);
1935 Event e = new EventField (
1936 current_class, (Expression) $4, (int) $2, false, name,
1937 var.expression_or_array_initializer, (Attributes) $1,
1940 current_container.AddEvent (e);
1942 if (RootContext.Documentation != null) {
1943 e.DocComment = Lexer.consume_doc_comment ();
1944 Lexer.doc_state = XmlCommentState.Allowed;
1950 EVENT type namespace_or_type_name
1953 implicit_value_parameter_type = (Expression) $4;
1954 lexer.EventParsing = true;
1955 oob_stack.Push (lexer.Location);
1957 event_accessor_declarations
1959 lexer.EventParsing = false;
1963 Location loc = (Location) oob_stack.Pop ();
1966 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
1969 Pair pair = (Pair) $8;
1971 MemberName name = (MemberName) $5;
1973 Event e = new EventProperty (
1974 current_class, (Expression) $4, (int) $2, false, name, null,
1975 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
1977 if (RootContext.Documentation != null) {
1978 e.DocComment = Lexer.consume_doc_comment ();
1979 Lexer.doc_state = XmlCommentState.Allowed;
1982 current_container.AddEvent (e);
1983 implicit_value_parameter_type = null;
1986 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
1987 MemberName mn = (MemberName) $5;
1989 if (mn.Left != null)
1990 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
1992 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
1994 if (RootContext.Documentation != null)
1995 Lexer.doc_state = XmlCommentState.Allowed;
1999 event_accessor_declarations
2000 : add_accessor_declaration remove_accessor_declaration
2002 $$ = new Pair ($1, $2);
2004 | remove_accessor_declaration add_accessor_declaration
2006 $$ = new Pair ($2, $1);
2008 | add_accessor_declaration { $$ = null; }
2009 | remove_accessor_declaration { $$ = null; }
2012 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2018 add_accessor_declaration
2019 : opt_attributes ADD
2021 Parameter [] args = new Parameter [1];
2022 Parameter implicit_value_parameter = new Parameter (
2023 implicit_value_parameter_type, "value",
2024 Parameter.Modifier.NONE, null);
2026 args [0] = implicit_value_parameter;
2028 current_local_parameters = new Parameters (args, null, lexer.Location);
2029 lexer.EventParsing = false;
2033 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2034 lexer.EventParsing = true;
2036 | opt_attributes ADD error {
2037 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2042 remove_accessor_declaration
2043 : opt_attributes REMOVE
2045 Parameter [] args = new Parameter [1];
2046 Parameter implicit_value_parameter = new Parameter (
2047 implicit_value_parameter_type, "value",
2048 Parameter.Modifier.NONE, null);
2050 args [0] = implicit_value_parameter;
2052 current_local_parameters = new Parameters (args, null, lexer.Location);
2053 lexer.EventParsing = false;
2057 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2058 lexer.EventParsing = true;
2060 | opt_attributes REMOVE error {
2061 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2067 : opt_attributes opt_modifiers indexer_declarator
2070 IndexerDeclaration decl = (IndexerDeclaration) $3;
2072 implicit_value_parameter_type = decl.type;
2074 lexer.PropertyParsing = true;
2075 parsing_indexer = true;
2077 indexer_parameters = decl.param_list;
2078 oob_stack.Push (lexer.Location);
2080 accessor_declarations
2082 lexer.PropertyParsing = false;
2083 parsing_indexer = false;
2087 // The signature is computed from the signature of the indexer. Look
2088 // at section 3.6 on the spec
2089 Location loc = (Location) oob_stack.Pop ();
2091 IndexerDeclaration decl = (IndexerDeclaration) $3;
2092 Pair pair = (Pair) $6;
2093 Accessor get_block = (Accessor) pair.First;
2094 Accessor set_block = (Accessor) pair.Second;
2097 if (decl.interface_type != null)
2098 name = new MemberName (decl.interface_type,
2099 TypeContainer.DefaultIndexerName);
2101 name = new MemberName (TypeContainer.DefaultIndexerName);
2103 indexer = new Indexer (current_class, decl.type, name,
2104 (int) $2, false, decl.param_list, (Attributes) $1,
2105 get_block, set_block, loc);
2106 if (RootContext.Documentation != null)
2107 indexer.DocComment = ConsumeStoredComment ();
2109 current_container.AddIndexer (indexer);
2111 current_local_parameters = null;
2112 implicit_value_parameter_type = null;
2113 indexer_parameters = null;
2118 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2120 Parameters pars = (Parameters) $4;
2121 if (pars.HasArglist) {
2122 // "__arglist is not valid in this context"
2123 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2124 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2125 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2127 if (RootContext.Documentation != null) {
2128 tmpComment = Lexer.consume_doc_comment ();
2129 Lexer.doc_state = XmlCommentState.Allowed;
2132 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2134 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2136 Parameters pars = (Parameters) $6;
2138 if (pars.HasArglist) {
2139 // "__arglist is not valid in this context"
2140 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2141 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2142 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2145 MemberName name = (MemberName) $2;
2146 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2148 if (RootContext.Documentation != null) {
2149 tmpComment = Lexer.consume_doc_comment ();
2150 Lexer.doc_state = XmlCommentState.Allowed;
2160 if (RootContext.Documentation != null)
2161 enumTypeComment = Lexer.consume_doc_comment ();
2166 Location enum_location = lexer.Location;
2168 MemberName full_name = MakeName (new MemberName ((string) $4));
2169 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2170 full_name, (Attributes) $1, enum_location);
2172 if (RootContext.Documentation != null)
2173 e.DocComment = enumTypeComment;
2175 foreach (VariableDeclaration ev in (ArrayList) $7) {
2176 e.AddEnumMember (ev.identifier,
2177 (Expression) ev.expression_or_array_initializer,
2178 ev.Location, ev.OptAttributes,
2182 string name = full_name.GetName ();
2183 current_container.AddEnum (e);
2184 RootContext.Tree.RecordDecl (name, e);
2190 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2191 | COLON type { $$ = $2; }
2197 if (RootContext.Documentation != null)
2198 Lexer.doc_state = XmlCommentState.Allowed;
2200 opt_enum_member_declarations
2202 // here will be evaluated after CLOSE_BLACE is consumed.
2203 if (RootContext.Documentation != null)
2204 Lexer.doc_state = XmlCommentState.Allowed;
2212 opt_enum_member_declarations
2213 : /* empty */ { $$ = new ArrayList (4); }
2214 | enum_member_declarations opt_comma { $$ = $1; }
2217 enum_member_declarations
2218 : enum_member_declaration
2220 ArrayList l = new ArrayList (4);
2225 | enum_member_declarations COMMA enum_member_declaration
2227 ArrayList l = (ArrayList) $1;
2235 enum_member_declaration
2236 : opt_attributes IDENTIFIER
2238 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2240 if (RootContext.Documentation != null) {
2241 vd.DocComment = Lexer.consume_doc_comment ();
2242 Lexer.doc_state = XmlCommentState.Allowed;
2247 | opt_attributes IDENTIFIER
2249 $$ = lexer.Location;
2250 if (RootContext.Documentation != null) {
2251 tmpComment = Lexer.consume_doc_comment ();
2252 Lexer.doc_state = XmlCommentState.NotAllowed;
2257 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2259 if (RootContext.Documentation != null)
2260 vd.DocComment = ConsumeStoredComment ();
2266 delegate_declaration
2269 DELEGATE type member_name
2270 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2273 Location l = lexer.Location;
2274 MemberName name = MakeName ((MemberName) $5);
2275 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2276 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2278 if (RootContext.Documentation != null) {
2279 del.DocComment = Lexer.consume_doc_comment ();
2280 Lexer.doc_state = XmlCommentState.Allowed;
2283 current_container.AddDelegate (del);
2284 RootContext.Tree.RecordDecl (name.GetName (true), del);
2288 DELEGATE VOID 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 (
2295 current_namespace, current_container,
2296 TypeManager.system_void_expr, (int) $2, name,
2297 (Parameters) $7, (Attributes) $1, l);
2299 if (RootContext.Documentation != null) {
2300 del.DocComment = Lexer.consume_doc_comment ();
2301 Lexer.doc_state = XmlCommentState.Allowed;
2304 current_container.AddDelegate (del);
2305 RootContext.Tree.RecordDecl (name.GetName (true), del);
2309 namespace_or_type_name
2311 | namespace_or_type_name DOT IDENTIFIER {
2312 $$ = new MemberName ((MemberName) $1, (string) $3);
2318 $$ = new MemberName ((string) $1);
2323 * Before you think of adding a return_type, notice that we have been
2324 * using two rules in the places where it matters (one rule using type
2325 * and another identical one that uses VOID as the return type). This
2326 * gets rid of a shift/reduce couple
2329 : namespace_or_type_name
2331 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2343 // Note that here only unmanaged types are allowed but we
2344 // can't perform checks during this phase - we do it during
2345 // semantic analysis.
2347 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2351 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2357 | non_expression_type rank_specifier
2359 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2361 | non_expression_type STAR
2363 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2365 | expression rank_specifiers
2367 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2371 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2375 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2376 // through this different path
2378 | multiplicative_expression STAR
2380 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2387 ArrayList types = new ArrayList (4);
2392 | type_list COMMA type
2394 ArrayList types = (ArrayList) $1;
2402 * replaces all the productions for isolating the various
2403 * simple types, but we need this to reuse it easily in local_variable_type
2406 : OBJECT { $$ = TypeManager.system_object_expr; }
2407 | STRING { $$ = TypeManager.system_string_expr; }
2408 | BOOL { $$ = TypeManager.system_boolean_expr; }
2409 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2410 | FLOAT { $$ = TypeManager.system_single_expr; }
2411 | DOUBLE { $$ = TypeManager.system_double_expr; }
2416 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2417 | BYTE { $$ = TypeManager.system_byte_expr; }
2418 | SHORT { $$ = TypeManager.system_int16_expr; }
2419 | USHORT { $$ = TypeManager.system_uint16_expr; }
2420 | INT { $$ = TypeManager.system_int32_expr; }
2421 | UINT { $$ = TypeManager.system_uint32_expr; }
2422 | LONG { $$ = TypeManager.system_int64_expr; }
2423 | ULONG { $$ = TypeManager.system_uint64_expr; }
2424 | CHAR { $$ = TypeManager.system_char_expr; }
2425 | VOID { $$ = TypeManager.system_void_expr; }
2429 : type rank_specifiers
2431 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2436 // Expressions, section 7.5
2446 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2448 | parenthesized_expression
2450 | invocation_expression
2454 | post_increment_expression
2455 | post_decrement_expression
2459 | checked_expression
2460 | unchecked_expression
2461 | pointer_member_access
2462 | anonymous_method_expression
2469 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2470 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2471 | NULL { $$ = NullLiteral.Null; }
2475 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2476 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2477 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2482 object v = lexer.Value;
2488 $$ = IntLiteral.Zero;
2490 $$ = IntLiteral.One;
2492 $$ = new IntLiteral (i);
2493 } else if (v is uint)
2494 $$ = new UIntLiteral ((UInt32) v);
2496 $$ = new LongLiteral ((Int64) v);
2497 else if (v is ulong)
2498 $$ = new ULongLiteral ((UInt64) v);
2500 Console.WriteLine ("OOPS. Unexpected result from scanner");
2505 : TRUE { $$ = new BoolLiteral (true); }
2506 | FALSE { $$ = new BoolLiteral (false); }
2509 parenthesized_expression_0
2510 : OPEN_PARENS expression CLOSE_PARENS
2513 lexer.Deambiguate_CloseParens ();
2514 // After this, the next token returned is one of
2515 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2516 // or CLOSE_PARENS_MINUS.
2520 parenthesized_expression
2521 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2525 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2527 // If a parenthesized expression is followed by a minus, we need to wrap
2528 // the expression inside a ParenthesizedExpression for the CS0075 check
2529 // in Binary.DoResolve().
2530 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2535 : primary_expression DOT IDENTIFIER
2537 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2539 | predefined_type DOT IDENTIFIER
2541 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2549 invocation_expression
2550 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2553 Location l = lexer.Location;
2554 Report.Error (1, l, "Parse error");
2556 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2558 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2560 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2562 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2564 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2566 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2568 ArrayList args = new ArrayList (1);
2570 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2572 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2574 ArrayList args = ((ArrayList) $4);
2576 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2581 : /* empty */ { $$ = null; }
2588 ArrayList list = new ArrayList (4);
2592 | argument_list COMMA argument
2594 ArrayList list = (ArrayList) $1;
2598 | argument_list error {
2599 CheckToken (1026, yyToken, ", or ) expected");
2606 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2608 | non_simple_argument
2615 : REF variable_reference
2617 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2619 | OUT variable_reference
2621 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2623 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2625 ArrayList list = (ArrayList) $3;
2626 Argument[] args = new Argument [list.Count];
2627 list.CopyTo (args, 0);
2629 Expression expr = new Arglist (args, lexer.Location);
2630 $$ = new Argument (expr, Argument.AType.Expression);
2634 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2639 : expression { note ("section 5.4"); $$ = $1; }
2643 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2645 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2647 | primary_expression rank_specifiers
2649 // So the super-trick is that primary_expression
2650 // can only be either a SimpleName or a MemberAccess.
2651 // The MemberAccess case arises when you have a fully qualified type-name like :
2653 // SimpleName is when you have
2656 Expression expr = (Expression) $1;
2657 if (expr is ComposedCast){
2658 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2659 } else if (!(expr is SimpleName || expr is MemberAccess)){
2660 Error_ExpectingTypeName (lexer.Location, expr);
2661 $$ = TypeManager.system_object_expr;
2664 // So we extract the string corresponding to the SimpleName
2667 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2675 ArrayList list = new ArrayList (4);
2679 | expression_list COMMA expression
2681 ArrayList list = (ArrayList) $1;
2690 $$ = new This (current_block, lexer.Location);
2695 : BASE DOT IDENTIFIER
2697 $$ = new BaseAccess ((string) $3, lexer.Location);
2699 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2701 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2704 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2709 post_increment_expression
2710 : primary_expression OP_INC
2712 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2713 (Expression) $1, lexer.Location);
2717 post_decrement_expression
2718 : primary_expression OP_DEC
2720 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2721 (Expression) $1, lexer.Location);
2726 : object_or_delegate_creation_expression
2727 | array_creation_expression
2730 object_or_delegate_creation_expression
2731 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2733 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2737 array_creation_expression
2738 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2740 opt_array_initializer
2742 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2744 | NEW type rank_specifiers array_initializer
2746 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2750 Report.Error (1031, lexer.Location, "Type expected");
2755 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2771 : rank_specifier opt_rank_specifier
2773 $$ = (string) $2 + (string) $1;
2778 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2780 $$ = "[" + (string) $2 + "]";
2800 | dim_separators COMMA
2802 $$ = (string) $1 + ",";
2806 opt_array_initializer
2818 : OPEN_BRACE CLOSE_BRACE
2820 ArrayList list = new ArrayList (4);
2823 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2825 $$ = (ArrayList) $2;
2829 variable_initializer_list
2830 : variable_initializer
2832 ArrayList list = new ArrayList (4);
2836 | variable_initializer_list COMMA variable_initializer
2838 ArrayList list = (ArrayList) $1;
2845 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2847 $$ = new TypeOfVoid (lexer.Location);
2849 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2851 $$ = new TypeOf ((Expression) $3, lexer.Location);
2856 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2857 $$ = new SizeOf ((Expression) $3, lexer.Location);
2862 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2864 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2868 unchecked_expression
2869 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2871 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2875 pointer_member_access
2876 : primary_expression OP_PTR IDENTIFIER
2880 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2881 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2885 anonymous_method_expression
2886 : DELEGATE opt_anonymous_method_signature {
2887 oob_stack.Push (current_local_parameters);
2888 current_local_parameters = (Parameters)$2;
2890 // Force the next block to be created as a ToplevelBlock
2891 oob_stack.Push (current_block);
2892 oob_stack.Push (top_current_block);
2893 oob_stack.Push (lexer.Location);
2894 current_block = null;
2896 Location loc = (Location) oob_stack.Pop ();
2897 top_current_block = (Block) oob_stack.Pop ();
2898 current_block = (Block) oob_stack.Pop ();
2899 if (RootContext.Version == LanguageVersion.ISO_1){
2900 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2903 ToplevelBlock anon_block = (ToplevelBlock) $4;
2905 anon_block.Parent = current_block;
2906 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
2909 current_local_parameters = (Parameters) oob_stack.Pop ();
2913 opt_anonymous_method_signature
2914 : /* empty */ { $$ = null; }
2915 | anonymous_method_signature
2918 anonymous_method_signature
2919 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2922 $$ = Parameters.EmptyReadOnlyParameters;
2924 ArrayList par_list = (ArrayList) $2;
2925 Parameter [] pars = new Parameter [par_list.Count];
2926 par_list.CopyTo (pars);
2927 $$ = new Parameters (pars, null, lexer.Location);
2932 opt_anonymous_method_parameter_list
2933 : /* empty */ { $$ = null; }
2934 | anonymous_method_parameter_list { $$ = $1; }
2937 anonymous_method_parameter_list
2938 : anonymous_method_parameter
2940 ArrayList a = new ArrayList (4);
2944 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2946 ArrayList a = (ArrayList) $1;
2952 anonymous_method_parameter
2953 : opt_parameter_modifier type IDENTIFIER {
2954 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
2956 | PARAMS type IDENTIFIER {
2957 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
2963 : primary_expression
2964 | BANG prefixed_unary_expression
2966 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2968 | TILDE prefixed_unary_expression
2970 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2976 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2978 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2980 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2982 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2988 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2990 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2995 // The idea to split this out is from Rhys' grammar
2996 // to solve the problem with casts.
2998 prefixed_unary_expression
3000 | PLUS prefixed_unary_expression
3002 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3004 | MINUS prefixed_unary_expression
3006 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3008 | OP_INC prefixed_unary_expression
3010 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3011 (Expression) $2, lexer.Location);
3013 | OP_DEC prefixed_unary_expression
3015 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3016 (Expression) $2, lexer.Location);
3018 | STAR prefixed_unary_expression
3020 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3022 | BITWISE_AND prefixed_unary_expression
3024 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3028 pre_increment_expression
3029 : OP_INC prefixed_unary_expression
3031 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3032 (Expression) $2, lexer.Location);
3036 pre_decrement_expression
3037 : OP_DEC prefixed_unary_expression
3039 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3040 (Expression) $2, lexer.Location);
3044 multiplicative_expression
3045 : prefixed_unary_expression
3046 | multiplicative_expression STAR prefixed_unary_expression
3048 $$ = new Binary (Binary.Operator.Multiply,
3049 (Expression) $1, (Expression) $3, lexer.Location);
3051 | multiplicative_expression DIV prefixed_unary_expression
3053 $$ = new Binary (Binary.Operator.Division,
3054 (Expression) $1, (Expression) $3, lexer.Location);
3056 | multiplicative_expression PERCENT prefixed_unary_expression
3058 $$ = new Binary (Binary.Operator.Modulus,
3059 (Expression) $1, (Expression) $3, lexer.Location);
3064 : multiplicative_expression
3065 | additive_expression PLUS multiplicative_expression
3067 $$ = new Binary (Binary.Operator.Addition,
3068 (Expression) $1, (Expression) $3, lexer.Location);
3070 | additive_expression MINUS multiplicative_expression
3072 $$ = new Binary (Binary.Operator.Subtraction,
3073 (Expression) $1, (Expression) $3, lexer.Location);
3078 : additive_expression
3079 | shift_expression OP_SHIFT_LEFT additive_expression
3081 $$ = new Binary (Binary.Operator.LeftShift,
3082 (Expression) $1, (Expression) $3, lexer.Location);
3084 | shift_expression OP_SHIFT_RIGHT additive_expression
3086 $$ = new Binary (Binary.Operator.RightShift,
3087 (Expression) $1, (Expression) $3, lexer.Location);
3091 relational_expression
3093 | relational_expression OP_LT shift_expression
3095 $$ = new Binary (Binary.Operator.LessThan,
3096 (Expression) $1, (Expression) $3, lexer.Location);
3098 | relational_expression OP_GT shift_expression
3100 $$ = new Binary (Binary.Operator.GreaterThan,
3101 (Expression) $1, (Expression) $3, lexer.Location);
3103 | relational_expression OP_LE shift_expression
3105 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3106 (Expression) $1, (Expression) $3, lexer.Location);
3108 | relational_expression OP_GE shift_expression
3110 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3111 (Expression) $1, (Expression) $3, lexer.Location);
3113 | relational_expression IS type
3115 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3117 | relational_expression AS type
3119 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3124 : relational_expression
3125 | equality_expression OP_EQ relational_expression
3127 $$ = new Binary (Binary.Operator.Equality,
3128 (Expression) $1, (Expression) $3, lexer.Location);
3130 | equality_expression OP_NE relational_expression
3132 $$ = new Binary (Binary.Operator.Inequality,
3133 (Expression) $1, (Expression) $3, lexer.Location);
3138 : equality_expression
3139 | and_expression BITWISE_AND equality_expression
3141 $$ = new Binary (Binary.Operator.BitwiseAnd,
3142 (Expression) $1, (Expression) $3, lexer.Location);
3146 exclusive_or_expression
3148 | exclusive_or_expression CARRET and_expression
3150 $$ = new Binary (Binary.Operator.ExclusiveOr,
3151 (Expression) $1, (Expression) $3, lexer.Location);
3155 inclusive_or_expression
3156 : exclusive_or_expression
3157 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3159 $$ = new Binary (Binary.Operator.BitwiseOr,
3160 (Expression) $1, (Expression) $3, lexer.Location);
3164 conditional_and_expression
3165 : inclusive_or_expression
3166 | conditional_and_expression OP_AND inclusive_or_expression
3168 $$ = new Binary (Binary.Operator.LogicalAnd,
3169 (Expression) $1, (Expression) $3, lexer.Location);
3173 conditional_or_expression
3174 : conditional_and_expression
3175 | conditional_or_expression OP_OR conditional_and_expression
3177 $$ = new Binary (Binary.Operator.LogicalOr,
3178 (Expression) $1, (Expression) $3, lexer.Location);
3182 conditional_expression
3183 : conditional_or_expression
3184 | conditional_or_expression INTERR expression COLON expression
3186 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3190 assignment_expression
3191 : prefixed_unary_expression ASSIGN expression
3193 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3195 | prefixed_unary_expression OP_MULT_ASSIGN expression
3197 Location l = lexer.Location;
3199 $$ = new CompoundAssign (
3200 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3202 | prefixed_unary_expression OP_DIV_ASSIGN expression
3204 Location l = lexer.Location;
3206 $$ = new CompoundAssign (
3207 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3209 | prefixed_unary_expression OP_MOD_ASSIGN expression
3211 Location l = lexer.Location;
3213 $$ = new CompoundAssign (
3214 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3216 | prefixed_unary_expression OP_ADD_ASSIGN expression
3218 Location l = lexer.Location;
3220 $$ = new CompoundAssign (
3221 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3223 | prefixed_unary_expression OP_SUB_ASSIGN expression
3225 Location l = lexer.Location;
3227 $$ = new CompoundAssign (
3228 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3230 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3232 Location l = lexer.Location;
3234 $$ = new CompoundAssign (
3235 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3237 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3239 Location l = lexer.Location;
3241 $$ = new CompoundAssign (
3242 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3244 | prefixed_unary_expression OP_AND_ASSIGN expression
3246 Location l = lexer.Location;
3248 $$ = new CompoundAssign (
3249 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3251 | prefixed_unary_expression OP_OR_ASSIGN expression
3253 Location l = lexer.Location;
3255 $$ = new CompoundAssign (
3256 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3258 | prefixed_unary_expression OP_XOR_ASSIGN expression
3260 Location l = lexer.Location;
3262 $$ = new CompoundAssign (
3263 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3268 : conditional_expression
3269 | assignment_expression
3289 MemberName name = MakeName ((MemberName) $5);
3290 bool partial = (bool) $3;
3291 int mod_flags = (int) $2;
3294 ClassPart part = PartialContainer.CreatePart (
3295 current_namespace, current_container, name, mod_flags,
3296 (Attributes) $1, Kind.Class, lexer.Location);
3298 current_container = part.PartialContainer;
3299 current_class = part;
3301 if ((mod_flags & Modifiers.STATIC) != 0) {
3302 current_class = new StaticClass (
3303 current_namespace, current_container, name,
3304 mod_flags, (Attributes) $1, lexer.Location);
3306 current_class = new Class (
3307 current_namespace, current_container, name,
3308 mod_flags, (Attributes) $1, lexer.Location);
3311 current_container = current_class;
3312 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3318 if (current_class.Name == "System.Object") {
3319 Report.Error (537, current_class.Location,
3320 "The class System.Object cannot have a base " +
3321 "class or implement an interface.");
3323 current_class.Bases = (ArrayList) $7;
3326 if (RootContext.Documentation != null) {
3327 current_class.DocComment = Lexer.consume_doc_comment ();
3328 Lexer.doc_state = XmlCommentState.Allowed;
3331 current_class.Register ();
3335 if (RootContext.Documentation != null)
3336 Lexer.doc_state = XmlCommentState.Allowed;
3342 current_container = current_container.Parent;
3343 current_class = current_container;
3349 { $$ = (bool) false; }
3351 { $$ = (bool) true; }
3355 : /* empty */ { $$ = (int) 0; }
3361 | modifiers modifier
3366 if ((m1 & m2) != 0) {
3367 Location l = lexer.Location;
3368 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3370 $$ = (int) (m1 | m2);
3375 : NEW { $$ = Modifiers.NEW; }
3376 | PUBLIC { $$ = Modifiers.PUBLIC; }
3377 | PROTECTED { $$ = Modifiers.PROTECTED; }
3378 | INTERNAL { $$ = Modifiers.INTERNAL; }
3379 | PRIVATE { $$ = Modifiers.PRIVATE; }
3380 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3381 | SEALED { $$ = Modifiers.SEALED; }
3382 | STATIC { $$ = Modifiers.STATIC; }
3383 | READONLY { $$ = Modifiers.READONLY; }
3384 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3385 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3386 | EXTERN { $$ = Modifiers.EXTERN; }
3387 | VOLATILE { $$ = Modifiers.VOLATILE; }
3388 | UNSAFE { $$ = Modifiers.UNSAFE; }
3392 : /* empty */ { $$ = null; }
3393 | class_base { $$ = $1; }
3397 : COLON type_list { $$ = $2; }
3405 // A block is "contained" on the following places:
3407 // property_declaration as part of the accessor body (get/set)
3408 // operator_declaration
3409 // constructor_declaration
3410 // destructor_declaration
3411 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3416 if (current_block == null){
3417 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3418 top_current_block = current_block;
3420 current_block = new Block (current_block, current_local_parameters,
3421 lexer.Location, Location.Null);
3424 opt_statement_list CLOSE_BRACE
3426 while (current_block.Implicit)
3427 current_block = current_block.Parent;
3429 current_block.SetEndLocation (lexer.Location);
3430 current_block = current_block.Parent;
3431 if (current_block == null)
3432 top_current_block = null;
3443 | statement_list statement
3447 : declaration_statement
3449 if ($1 != null && (Block) $1 != current_block){
3450 current_block.AddStatement ((Statement) $1);
3451 current_block = (Block) $1;
3454 | valid_declaration_statement
3456 current_block.AddStatement ((Statement) $1);
3461 valid_declaration_statement
3464 | expression_statement
3465 | selection_statement
3466 | iteration_statement
3470 | unchecked_statement
3478 : valid_declaration_statement
3479 | declaration_statement
3481 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3486 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3494 $$ = EmptyStatement.Value;
3501 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3503 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3504 current_block.AddStatement (labeled);
3509 declaration_statement
3510 : local_variable_declaration SEMICOLON
3513 DictionaryEntry de = (DictionaryEntry) $1;
3515 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3519 | local_constant_declaration SEMICOLON
3522 DictionaryEntry de = (DictionaryEntry) $1;
3524 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3530 * The following is from Rhys' grammar:
3531 * > Types in local variable declarations must be recognized as
3532 * > expressions to prevent reduce/reduce errors in the grammar.
3533 * > The expressions are converted into types during semantic analysis.
3536 : primary_expression opt_rank_specifier
3538 // FIXME: Do something smart here regarding the composition of the type.
3540 // Ok, the above "primary_expression" is there to get rid of
3541 // both reduce/reduce and shift/reduces in the grammar, it should
3542 // really just be "type_name". If you use type_name, a reduce/reduce
3543 // creeps up. If you use namespace_or_type_name (which is all we need
3544 // really) two shift/reduces appear.
3547 // So the super-trick is that primary_expression
3548 // can only be either a SimpleName or a MemberAccess.
3549 // The MemberAccess case arises when you have a fully qualified type-name like :
3551 // SimpleName is when you have
3554 Expression expr = (Expression) $1;
3555 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3556 Error_ExpectingTypeName (lexer.Location, expr);
3560 // So we extract the string corresponding to the SimpleName
3564 if ((string) $2 == "")
3567 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3570 | builtin_types opt_rank_specifier
3572 if ((string) $2 == "")
3575 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3579 local_variable_pointer_type
3580 : primary_expression STAR
3582 Expression expr = (Expression) $1;
3583 Location l = lexer.Location;
3585 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3586 Error_ExpectingTypeName (l, expr);
3590 $$ = new ComposedCast ((Expression) $1, "*", l);
3592 | builtin_types STAR
3594 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3598 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3600 | local_variable_pointer_type STAR
3602 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3606 local_variable_declaration
3607 : local_variable_type variable_declarators
3610 $$ = new DictionaryEntry ($1, $2);
3614 | local_variable_pointer_type opt_rank_specifier variable_declarators
3619 if ((string) $2 == "")
3620 t = (Expression) $1;
3622 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3623 $$ = new DictionaryEntry (t, $3);
3629 local_constant_declaration
3630 : CONST local_variable_type constant_declarators
3633 $$ = new DictionaryEntry ($2, $3);
3639 expression_statement
3640 : statement_expression SEMICOLON
3647 // We have to do the wrapping here and not in the case above,
3648 // because statement_expression is used for example in for_statement
3650 statement_expression
3651 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3652 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3653 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3654 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3655 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3656 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3657 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3659 Report.Error (1002, lexer.Location, "Expecting `;'");
3664 object_creation_expression
3665 : object_or_delegate_creation_expression
3666 { note ("complain if this is a delegate maybe?"); }
3675 : if_statement_open if_statement_rest
3684 oob_stack.Push (lexer.Location);
3689 : boolean_expression CLOSE_PARENS
3692 Location l = (Location) oob_stack.Pop ();
3694 $$ = new If ((Expression) $1, (Statement) $3, l);
3696 if (RootContext.WarningLevel >= 4){
3697 if ($3 == EmptyStatement.Value)
3698 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3702 | boolean_expression CLOSE_PARENS
3703 embedded_statement ELSE embedded_statement
3705 Location l = (Location) oob_stack.Pop ();
3707 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3712 : SWITCH OPEN_PARENS
3714 oob_stack.Push (lexer.Location);
3715 switch_stack.Push (current_block);
3717 expression CLOSE_PARENS
3720 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3721 current_block = (Block) switch_stack.Pop ();
3737 Report.Error (1522, lexer.Location, "Empty switch block");
3745 ArrayList sections = new ArrayList (4);
3750 | switch_sections switch_section
3752 ArrayList sections = (ArrayList) $1;
3762 current_block = current_block.CreateSwitchBlock (lexer.Location);
3766 Block topmost = current_block;
3768 while (topmost.Implicit)
3769 topmost = topmost.Parent;
3770 $$ = new SwitchSection ((ArrayList) $1, topmost);
3777 ArrayList labels = new ArrayList (4);
3782 | switch_labels switch_label
3784 ArrayList labels = (ArrayList) ($1);
3792 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3793 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3796 1523, lexer.Location,
3797 "The keyword case or default must precede code in switch block");
3811 oob_stack.Push (lexer.Location);
3813 boolean_expression CLOSE_PARENS embedded_statement
3815 Location l = (Location) oob_stack.Pop ();
3816 $$ = new While ((Expression) $4, (Statement) $6, l);
3818 if (RootContext.WarningLevel >= 4){
3819 if ($6 == EmptyStatement.Value)
3820 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3826 : DO embedded_statement
3828 oob_stack.Push (lexer.Location);
3830 boolean_expression CLOSE_PARENS SEMICOLON
3832 Location l = (Location) oob_stack.Pop ();
3834 $$ = new Do ((Statement) $2, (Expression) $6, l);
3840 opt_for_initializer SEMICOLON
3842 Block assign_block = new Block (current_block);
3843 current_block = assign_block;
3845 if ($3 is DictionaryEntry){
3846 DictionaryEntry de = (DictionaryEntry) $3;
3848 Expression type = (Expression) de.Key;
3849 ArrayList var_declarators = (ArrayList) de.Value;
3851 foreach (VariableDeclaration decl in var_declarators){
3855 vi = current_block.AddVariable (
3856 type, decl.identifier, current_local_parameters, decl.Location);
3860 Location l = lexer.Location;
3862 if (decl.expression_or_array_initializer is Expression){
3863 expr = (Expression) decl.expression_or_array_initializer;
3864 } else if (decl.expression_or_array_initializer == null) {
3867 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3868 expr = new ArrayCreation (type, "", init, decl.Location);
3871 LocalVariableReference var;
3872 var = new LocalVariableReference (assign_block, decl.identifier, l);
3875 Assign a = new Assign (var, expr, decl.Location);
3877 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3883 oob_stack.Push (lexer.Location);
3885 opt_for_condition SEMICOLON
3886 opt_for_iterator CLOSE_PARENS
3889 Location l = (Location) oob_stack.Pop ();
3891 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3893 if (RootContext.WarningLevel >= 4){
3894 if ($10 == EmptyStatement.Value)
3895 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3898 current_block.AddStatement (f);
3899 while (current_block.Implicit)
3900 current_block = current_block.Parent;
3902 current_block = current_block.Parent;
3907 : /* empty */ { $$ = EmptyStatement.Value; }
3912 : local_variable_declaration
3913 | statement_expression_list
3917 : /* empty */ { $$ = null; }
3918 | boolean_expression
3922 : /* empty */ { $$ = EmptyStatement.Value; }
3927 : statement_expression_list
3930 statement_expression_list
3931 : statement_expression
3933 // CHANGE: was `null'
3934 Block b = new Block (current_block, Block.Flags.Implicit);
3936 b.AddStatement ((Statement) $1);
3939 | statement_expression_list COMMA statement_expression
3941 Block b = (Block) $1;
3943 b.AddStatement ((Statement) $3);
3949 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3951 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3954 | FOREACH OPEN_PARENS type IDENTIFIER IN
3956 oob_stack.Push (lexer.Location);
3958 expression CLOSE_PARENS
3960 oob_stack.Push (current_block);
3962 Block foreach_block = new Block (current_block);
3963 LocalVariableReference v = null;
3964 Location l = lexer.Location;
3967 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3971 // Get a writable reference to this read-only variable.
3972 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3974 current_block = foreach_block;
3977 oob_stack.Push (current_block);
3981 Block foreach_block = (Block) oob_stack.Pop ();
3982 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3983 Block prev_block = (Block) oob_stack.Pop ();
3984 Location l = (Location) oob_stack.Pop ();
3986 current_block = prev_block;
3989 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3990 foreach_block.AddStatement (f);
3999 | continue_statement
4009 $$ = new Break (lexer.Location);
4014 : CONTINUE SEMICOLON
4016 $$ = new Continue (lexer.Location);
4021 : GOTO IDENTIFIER SEMICOLON
4023 $$ = new Goto (current_block, (string) $2, lexer.Location);
4025 | GOTO CASE constant_expression SEMICOLON
4027 $$ = new GotoCase ((Expression) $3, lexer.Location);
4029 | GOTO DEFAULT SEMICOLON
4031 $$ = new GotoDefault (lexer.Location);
4036 : RETURN opt_expression SEMICOLON
4038 $$ = new Return ((Expression) $2, lexer.Location);
4043 : THROW opt_expression SEMICOLON
4045 $$ = new Throw ((Expression) $2, lexer.Location);
4050 : IDENTIFIER RETURN expression SEMICOLON
4052 string s = (string) $1;
4054 Report.Error (1003, lexer.Location, "; expected");
4057 if (RootContext.Version == LanguageVersion.ISO_1){
4058 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4061 if (iterator_container == null){
4062 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4065 iterator_container.SetYields ();
4066 $$ = new Yield ((Expression) $3, lexer.Location);
4069 | IDENTIFIER BREAK SEMICOLON
4071 string s = (string) $1;
4073 Report.Error (1003, lexer.Location, "; expected");
4076 if (RootContext.Version == LanguageVersion.ISO_1){
4077 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4080 if (iterator_container == null){
4081 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4084 iterator_container.SetYields ();
4085 $$ = new YieldBreak (lexer.Location);
4096 : TRY block catch_clauses
4100 ArrayList c = (ArrayList)$3;
4101 for (int i = 0; i < c.Count; ++i) {
4102 Catch cc = (Catch) c [i];
4104 if (i != c.Count - 1)
4105 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4112 // Now s contains the list of specific catch clauses
4113 // and g contains the general one.
4115 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4117 | TRY block opt_catch_clauses FINALLY block
4120 ArrayList s = new ArrayList (4);
4121 ArrayList catch_list = (ArrayList) $3;
4123 if (catch_list != null){
4124 foreach (Catch cc in catch_list) {
4132 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4136 Report.Error (1524, lexer.Location, "Expected catch or finally");
4141 : /* empty */ { $$ = null; }
4148 ArrayList l = new ArrayList (4);
4153 | catch_clauses catch_clause
4155 ArrayList l = (ArrayList) $1;
4163 : /* empty */ { $$ = null; }
4168 : CATCH opt_catch_args
4170 Expression type = null;
4174 DictionaryEntry cc = (DictionaryEntry) $2;
4175 type = (Expression) cc.Key;
4176 id = (string) cc.Value;
4179 ArrayList one = new ArrayList (4);
4180 Location loc = lexer.Location;
4182 one.Add (new VariableDeclaration (id, null, loc));
4185 current_block = new Block (current_block);
4186 Block b = declare_local_variables (type, one, loc);
4191 Expression type = null;
4195 DictionaryEntry cc = (DictionaryEntry) $2;
4196 type = (Expression) cc.Key;
4197 id = (string) cc.Value;
4201 // FIXME: I can change this for an assignment.
4203 while (current_block != (Block) $1)
4204 current_block = current_block.Parent;
4209 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
4214 : /* empty */ { $$ = null; }
4219 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4221 $$ = new DictionaryEntry ($2, $3);
4228 $$ = new Checked ((Block) $2);
4235 $$ = new Unchecked ((Block) $2);
4242 if (!RootContext.Unsafe){
4243 Report.Error (227, lexer.Location,
4244 "Unsafe code can only be used if --unsafe is used");
4247 $$ = new Unsafe ((Block) $3);
4253 type fixed_pointer_declarators
4256 ArrayList list = (ArrayList) $4;
4257 Expression type = (Expression) $3;
4258 Location l = lexer.Location;
4259 int top = list.Count;
4261 Block assign_block = new Block (current_block);
4262 current_block = assign_block;
4264 for (int i = 0; i < top; i++){
4265 Pair p = (Pair) list [i];
4268 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4282 Location l = (Location) oob_stack.Pop ();
4284 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4286 if (RootContext.WarningLevel >= 4){
4287 if ($7 == EmptyStatement.Value)
4288 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4291 current_block.AddStatement (f);
4292 while (current_block.Implicit)
4293 current_block = current_block.Parent;
4295 current_block = current_block.Parent;
4299 fixed_pointer_declarators
4300 : fixed_pointer_declarator {
4301 ArrayList declarators = new ArrayList (4);
4303 declarators.Add ($1);
4306 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4308 ArrayList declarators = (ArrayList) $1;
4310 declarators.Add ($3);
4315 fixed_pointer_declarator
4316 : IDENTIFIER ASSIGN expression
4318 $$ = new Pair ($1, $3);
4322 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4328 : LOCK OPEN_PARENS expression CLOSE_PARENS
4334 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4339 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4341 Block assign_block = new Block (current_block);
4342 current_block = assign_block;
4344 oob_stack.Push (lexer.Location);
4346 if ($3 is DictionaryEntry){
4347 DictionaryEntry de = (DictionaryEntry) $3;
4348 Location l = lexer.Location;
4350 Expression type = (Expression) de.Key;
4351 ArrayList var_declarators = (ArrayList) de.Value;
4353 ArrayList vars = new ArrayList (4);
4355 foreach (VariableDeclaration decl in var_declarators){
4357 LocalInfo vi = current_block.AddVariable (
4358 type, decl.identifier,
4359 current_local_parameters, decl.Location);
4365 if (decl.expression_or_array_initializer is Expression){
4366 expr = (Expression) decl.expression_or_array_initializer;
4368 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4370 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4373 expr = new ArrayCreation (type, "", init, decl.Location);
4376 LocalVariableReference var;
4378 // Get a writable reference to this read-only variable.
4379 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4381 // This is so that it is not a warning on using variables
4384 vars.Add (new DictionaryEntry (var, expr));
4386 // Assign a = new Assign (var, expr, decl.Location);
4387 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4389 $3 = new DictionaryEntry (type, vars);
4394 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4395 current_block.AddStatement (u);
4396 while (current_block.Implicit)
4397 current_block = current_block.Parent;
4399 current_block = current_block.Parent;
4403 resource_acquisition
4404 : local_variable_declaration
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;