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);
2569 : /* empty */ { $$ = null; }
2576 ArrayList list = new ArrayList (4);
2580 | argument_list COMMA argument
2582 ArrayList list = (ArrayList) $1;
2586 | argument_list error {
2587 CheckToken (1026, yyToken, ", or ) expected");
2594 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2596 | REF variable_reference
2598 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2600 | OUT variable_reference
2602 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2604 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2606 ArrayList list = (ArrayList) $3;
2607 Argument[] args = new Argument [list.Count];
2608 list.CopyTo (args, 0);
2610 Expression expr = new Arglist (args, lexer.Location);
2611 $$ = new Argument (expr, Argument.AType.Expression);
2615 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2620 : expression { note ("section 5.4"); $$ = $1; }
2624 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2626 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2628 | primary_expression rank_specifiers
2630 // So the super-trick is that primary_expression
2631 // can only be either a SimpleName or a MemberAccess.
2632 // The MemberAccess case arises when you have a fully qualified type-name like :
2634 // SimpleName is when you have
2637 Expression expr = (Expression) $1;
2638 if (expr is ComposedCast){
2639 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2640 } else if (!(expr is SimpleName || expr is MemberAccess)){
2641 Error_ExpectingTypeName (lexer.Location, expr);
2642 $$ = TypeManager.system_object_expr;
2645 // So we extract the string corresponding to the SimpleName
2648 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2656 ArrayList list = new ArrayList (4);
2660 | expression_list COMMA expression
2662 ArrayList list = (ArrayList) $1;
2671 $$ = new This (current_block, lexer.Location);
2676 : BASE DOT IDENTIFIER
2678 $$ = new BaseAccess ((string) $3, lexer.Location);
2680 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2682 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2685 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2690 post_increment_expression
2691 : primary_expression OP_INC
2693 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2694 (Expression) $1, lexer.Location);
2698 post_decrement_expression
2699 : primary_expression OP_DEC
2701 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2702 (Expression) $1, lexer.Location);
2707 : object_or_delegate_creation_expression
2708 | array_creation_expression
2711 object_or_delegate_creation_expression
2712 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2714 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2718 array_creation_expression
2719 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2721 opt_array_initializer
2723 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2725 | NEW type rank_specifiers array_initializer
2727 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2731 Report.Error (1031, lexer.Location, "Type expected");
2736 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2752 : rank_specifier opt_rank_specifier
2754 $$ = (string) $2 + (string) $1;
2759 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2761 $$ = "[" + (string) $2 + "]";
2781 | dim_separators COMMA
2783 $$ = (string) $1 + ",";
2787 opt_array_initializer
2799 : OPEN_BRACE CLOSE_BRACE
2801 ArrayList list = new ArrayList (4);
2804 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2806 $$ = (ArrayList) $2;
2810 variable_initializer_list
2811 : variable_initializer
2813 ArrayList list = new ArrayList (4);
2817 | variable_initializer_list COMMA variable_initializer
2819 ArrayList list = (ArrayList) $1;
2826 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2828 $$ = new TypeOfVoid (lexer.Location);
2830 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2832 $$ = new TypeOf ((Expression) $3, lexer.Location);
2837 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2838 $$ = new SizeOf ((Expression) $3, lexer.Location);
2843 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2845 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2849 unchecked_expression
2850 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2852 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2856 pointer_member_access
2857 : primary_expression OP_PTR IDENTIFIER
2861 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2862 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2866 anonymous_method_expression
2867 : DELEGATE opt_anonymous_method_signature {
2868 oob_stack.Push (current_local_parameters);
2869 current_local_parameters = (Parameters)$2;
2871 // Force the next block to be created as a ToplevelBlock
2872 oob_stack.Push (current_block);
2873 oob_stack.Push (top_current_block);
2874 oob_stack.Push (lexer.Location);
2875 current_block = null;
2877 Location loc = (Location) oob_stack.Pop ();
2878 top_current_block = (Block) oob_stack.Pop ();
2879 current_block = (Block) oob_stack.Pop ();
2880 if (RootContext.Version == LanguageVersion.ISO_1){
2881 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
2884 ToplevelBlock anon_block = (ToplevelBlock) $4;
2886 anon_block.Parent = current_block;
2887 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
2890 current_local_parameters = (Parameters) oob_stack.Pop ();
2894 opt_anonymous_method_signature
2895 : /* empty */ { $$ = null; }
2896 | anonymous_method_signature
2899 anonymous_method_signature
2900 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
2903 $$ = Parameters.EmptyReadOnlyParameters;
2905 ArrayList par_list = (ArrayList) $2;
2906 Parameter [] pars = new Parameter [par_list.Count];
2907 par_list.CopyTo (pars);
2908 $$ = new Parameters (pars, null, lexer.Location);
2913 opt_anonymous_method_parameter_list
2914 : /* empty */ { $$ = null; }
2915 | anonymous_method_parameter_list { $$ = $1; }
2918 anonymous_method_parameter_list
2919 : anonymous_method_parameter
2921 ArrayList a = new ArrayList (4);
2925 | anonymous_method_parameter_list COMMA anonymous_method_parameter
2927 ArrayList a = (ArrayList) $1;
2933 anonymous_method_parameter
2934 : opt_parameter_modifier type IDENTIFIER {
2935 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
2937 | PARAMS type IDENTIFIER {
2938 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
2944 : primary_expression
2945 | BANG prefixed_unary_expression
2947 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
2949 | TILDE prefixed_unary_expression
2951 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
2957 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
2959 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2961 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
2963 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
2969 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
2971 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
2976 // The idea to split this out is from Rhys' grammar
2977 // to solve the problem with casts.
2979 prefixed_unary_expression
2981 | PLUS prefixed_unary_expression
2983 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
2985 | MINUS prefixed_unary_expression
2987 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
2989 | OP_INC prefixed_unary_expression
2991 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
2992 (Expression) $2, lexer.Location);
2994 | OP_DEC prefixed_unary_expression
2996 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
2997 (Expression) $2, lexer.Location);
2999 | STAR prefixed_unary_expression
3001 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3003 | BITWISE_AND prefixed_unary_expression
3005 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3009 pre_increment_expression
3010 : OP_INC prefixed_unary_expression
3012 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3013 (Expression) $2, lexer.Location);
3017 pre_decrement_expression
3018 : OP_DEC prefixed_unary_expression
3020 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3021 (Expression) $2, lexer.Location);
3025 multiplicative_expression
3026 : prefixed_unary_expression
3027 | multiplicative_expression STAR prefixed_unary_expression
3029 $$ = new Binary (Binary.Operator.Multiply,
3030 (Expression) $1, (Expression) $3, lexer.Location);
3032 | multiplicative_expression DIV prefixed_unary_expression
3034 $$ = new Binary (Binary.Operator.Division,
3035 (Expression) $1, (Expression) $3, lexer.Location);
3037 | multiplicative_expression PERCENT prefixed_unary_expression
3039 $$ = new Binary (Binary.Operator.Modulus,
3040 (Expression) $1, (Expression) $3, lexer.Location);
3045 : multiplicative_expression
3046 | additive_expression PLUS multiplicative_expression
3048 $$ = new Binary (Binary.Operator.Addition,
3049 (Expression) $1, (Expression) $3, lexer.Location);
3051 | additive_expression MINUS multiplicative_expression
3053 $$ = new Binary (Binary.Operator.Subtraction,
3054 (Expression) $1, (Expression) $3, lexer.Location);
3059 : additive_expression
3060 | shift_expression OP_SHIFT_LEFT additive_expression
3062 $$ = new Binary (Binary.Operator.LeftShift,
3063 (Expression) $1, (Expression) $3, lexer.Location);
3065 | shift_expression OP_SHIFT_RIGHT additive_expression
3067 $$ = new Binary (Binary.Operator.RightShift,
3068 (Expression) $1, (Expression) $3, lexer.Location);
3072 relational_expression
3074 | relational_expression OP_LT shift_expression
3076 $$ = new Binary (Binary.Operator.LessThan,
3077 (Expression) $1, (Expression) $3, lexer.Location);
3079 | relational_expression OP_GT shift_expression
3081 $$ = new Binary (Binary.Operator.GreaterThan,
3082 (Expression) $1, (Expression) $3, lexer.Location);
3084 | relational_expression OP_LE shift_expression
3086 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3087 (Expression) $1, (Expression) $3, lexer.Location);
3089 | relational_expression OP_GE shift_expression
3091 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3092 (Expression) $1, (Expression) $3, lexer.Location);
3094 | relational_expression IS type
3096 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3098 | relational_expression AS type
3100 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3105 : relational_expression
3106 | equality_expression OP_EQ relational_expression
3108 $$ = new Binary (Binary.Operator.Equality,
3109 (Expression) $1, (Expression) $3, lexer.Location);
3111 | equality_expression OP_NE relational_expression
3113 $$ = new Binary (Binary.Operator.Inequality,
3114 (Expression) $1, (Expression) $3, lexer.Location);
3119 : equality_expression
3120 | and_expression BITWISE_AND equality_expression
3122 $$ = new Binary (Binary.Operator.BitwiseAnd,
3123 (Expression) $1, (Expression) $3, lexer.Location);
3127 exclusive_or_expression
3129 | exclusive_or_expression CARRET and_expression
3131 $$ = new Binary (Binary.Operator.ExclusiveOr,
3132 (Expression) $1, (Expression) $3, lexer.Location);
3136 inclusive_or_expression
3137 : exclusive_or_expression
3138 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3140 $$ = new Binary (Binary.Operator.BitwiseOr,
3141 (Expression) $1, (Expression) $3, lexer.Location);
3145 conditional_and_expression
3146 : inclusive_or_expression
3147 | conditional_and_expression OP_AND inclusive_or_expression
3149 $$ = new Binary (Binary.Operator.LogicalAnd,
3150 (Expression) $1, (Expression) $3, lexer.Location);
3154 conditional_or_expression
3155 : conditional_and_expression
3156 | conditional_or_expression OP_OR conditional_and_expression
3158 $$ = new Binary (Binary.Operator.LogicalOr,
3159 (Expression) $1, (Expression) $3, lexer.Location);
3163 conditional_expression
3164 : conditional_or_expression
3165 | conditional_or_expression INTERR expression COLON expression
3167 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3171 assignment_expression
3172 : prefixed_unary_expression ASSIGN expression
3174 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3176 | prefixed_unary_expression OP_MULT_ASSIGN expression
3178 Location l = lexer.Location;
3180 $$ = new CompoundAssign (
3181 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3183 | prefixed_unary_expression OP_DIV_ASSIGN expression
3185 Location l = lexer.Location;
3187 $$ = new CompoundAssign (
3188 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3190 | prefixed_unary_expression OP_MOD_ASSIGN expression
3192 Location l = lexer.Location;
3194 $$ = new CompoundAssign (
3195 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3197 | prefixed_unary_expression OP_ADD_ASSIGN expression
3199 Location l = lexer.Location;
3201 $$ = new CompoundAssign (
3202 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3204 | prefixed_unary_expression OP_SUB_ASSIGN expression
3206 Location l = lexer.Location;
3208 $$ = new CompoundAssign (
3209 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3211 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3213 Location l = lexer.Location;
3215 $$ = new CompoundAssign (
3216 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3218 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3220 Location l = lexer.Location;
3222 $$ = new CompoundAssign (
3223 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3225 | prefixed_unary_expression OP_AND_ASSIGN expression
3227 Location l = lexer.Location;
3229 $$ = new CompoundAssign (
3230 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3232 | prefixed_unary_expression OP_OR_ASSIGN expression
3234 Location l = lexer.Location;
3236 $$ = new CompoundAssign (
3237 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3239 | prefixed_unary_expression OP_XOR_ASSIGN expression
3241 Location l = lexer.Location;
3243 $$ = new CompoundAssign (
3244 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3249 : conditional_expression
3250 | assignment_expression
3270 MemberName name = MakeName ((MemberName) $5);
3271 bool partial = (bool) $3;
3272 int mod_flags = (int) $2;
3275 ClassPart part = PartialContainer.CreatePart (
3276 current_namespace, current_container, name, mod_flags,
3277 (Attributes) $1, Kind.Class, lexer.Location);
3279 current_container = part.PartialContainer;
3280 current_class = part;
3282 if ((mod_flags & Modifiers.STATIC) != 0) {
3283 current_class = new StaticClass (
3284 current_namespace, current_container, name,
3285 mod_flags, (Attributes) $1, lexer.Location);
3287 current_class = new Class (
3288 current_namespace, current_container, name,
3289 mod_flags, (Attributes) $1, lexer.Location);
3292 current_container = current_class;
3293 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3299 if (current_class.Name == "System.Object") {
3300 Report.Error (537, current_class.Location,
3301 "The class System.Object cannot have a base " +
3302 "class or implement an interface.");
3304 current_class.Bases = (ArrayList) $7;
3307 if (RootContext.Documentation != null) {
3308 current_class.DocComment = Lexer.consume_doc_comment ();
3309 Lexer.doc_state = XmlCommentState.Allowed;
3312 current_class.Register ();
3316 if (RootContext.Documentation != null)
3317 Lexer.doc_state = XmlCommentState.Allowed;
3323 current_container = current_container.Parent;
3324 current_class = current_container;
3330 { $$ = (bool) false; }
3332 { $$ = (bool) true; }
3336 : /* empty */ { $$ = (int) 0; }
3342 | modifiers modifier
3347 if ((m1 & m2) != 0) {
3348 Location l = lexer.Location;
3349 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3351 $$ = (int) (m1 | m2);
3356 : NEW { $$ = Modifiers.NEW; }
3357 | PUBLIC { $$ = Modifiers.PUBLIC; }
3358 | PROTECTED { $$ = Modifiers.PROTECTED; }
3359 | INTERNAL { $$ = Modifiers.INTERNAL; }
3360 | PRIVATE { $$ = Modifiers.PRIVATE; }
3361 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3362 | SEALED { $$ = Modifiers.SEALED; }
3363 | STATIC { $$ = Modifiers.STATIC; }
3364 | READONLY { $$ = Modifiers.READONLY; }
3365 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3366 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3367 | EXTERN { $$ = Modifiers.EXTERN; }
3368 | VOLATILE { $$ = Modifiers.VOLATILE; }
3369 | UNSAFE { $$ = Modifiers.UNSAFE; }
3373 : /* empty */ { $$ = null; }
3374 | class_base { $$ = $1; }
3378 : COLON type_list { $$ = $2; }
3386 // A block is "contained" on the following places:
3388 // property_declaration as part of the accessor body (get/set)
3389 // operator_declaration
3390 // constructor_declaration
3391 // destructor_declaration
3392 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3397 if (current_block == null){
3398 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3399 top_current_block = current_block;
3401 current_block = new Block (current_block, current_local_parameters,
3402 lexer.Location, Location.Null);
3405 opt_statement_list CLOSE_BRACE
3407 while (current_block.Implicit)
3408 current_block = current_block.Parent;
3410 current_block.SetEndLocation (lexer.Location);
3411 current_block = current_block.Parent;
3412 if (current_block == null)
3413 top_current_block = null;
3424 | statement_list statement
3428 : declaration_statement
3430 if ($1 != null && (Block) $1 != current_block){
3431 current_block.AddStatement ((Statement) $1);
3432 current_block = (Block) $1;
3435 | valid_declaration_statement
3437 current_block.AddStatement ((Statement) $1);
3442 valid_declaration_statement
3445 | expression_statement
3446 | selection_statement
3447 | iteration_statement
3451 | unchecked_statement
3459 : valid_declaration_statement
3460 | declaration_statement
3462 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3467 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3475 $$ = EmptyStatement.Value;
3482 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3484 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3485 current_block.AddStatement (labeled);
3490 declaration_statement
3491 : local_variable_declaration SEMICOLON
3494 DictionaryEntry de = (DictionaryEntry) $1;
3496 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3500 | local_constant_declaration SEMICOLON
3503 DictionaryEntry de = (DictionaryEntry) $1;
3505 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3511 * The following is from Rhys' grammar:
3512 * > Types in local variable declarations must be recognized as
3513 * > expressions to prevent reduce/reduce errors in the grammar.
3514 * > The expressions are converted into types during semantic analysis.
3517 : primary_expression opt_rank_specifier
3519 // FIXME: Do something smart here regarding the composition of the type.
3521 // Ok, the above "primary_expression" is there to get rid of
3522 // both reduce/reduce and shift/reduces in the grammar, it should
3523 // really just be "type_name". If you use type_name, a reduce/reduce
3524 // creeps up. If you use namespace_or_type_name (which is all we need
3525 // really) two shift/reduces appear.
3528 // So the super-trick is that primary_expression
3529 // can only be either a SimpleName or a MemberAccess.
3530 // The MemberAccess case arises when you have a fully qualified type-name like :
3532 // SimpleName is when you have
3535 Expression expr = (Expression) $1;
3536 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3537 Error_ExpectingTypeName (lexer.Location, expr);
3541 // So we extract the string corresponding to the SimpleName
3545 if ((string) $2 == "")
3548 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3551 | builtin_types opt_rank_specifier
3553 if ((string) $2 == "")
3556 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3560 local_variable_pointer_type
3561 : primary_expression STAR
3563 Expression expr = (Expression) $1;
3564 Location l = lexer.Location;
3566 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3567 Error_ExpectingTypeName (l, expr);
3571 $$ = new ComposedCast ((Expression) $1, "*", l);
3573 | builtin_types STAR
3575 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3579 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3581 | local_variable_pointer_type STAR
3583 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3587 local_variable_declaration
3588 : local_variable_type variable_declarators
3591 $$ = new DictionaryEntry ($1, $2);
3595 | local_variable_pointer_type opt_rank_specifier variable_declarators
3600 if ((string) $2 == "")
3601 t = (Expression) $1;
3603 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3604 $$ = new DictionaryEntry (t, $3);
3610 local_constant_declaration
3611 : CONST local_variable_type constant_declarators
3614 $$ = new DictionaryEntry ($2, $3);
3620 expression_statement
3621 : statement_expression SEMICOLON
3628 // We have to do the wrapping here and not in the case above,
3629 // because statement_expression is used for example in for_statement
3631 statement_expression
3632 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3633 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3634 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3635 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3636 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3637 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3638 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3640 Report.Error (1002, lexer.Location, "Expecting `;'");
3645 object_creation_expression
3646 : object_or_delegate_creation_expression
3647 { note ("complain if this is a delegate maybe?"); }
3656 : if_statement_open if_statement_rest
3665 oob_stack.Push (lexer.Location);
3670 : boolean_expression CLOSE_PARENS
3673 Location l = (Location) oob_stack.Pop ();
3675 $$ = new If ((Expression) $1, (Statement) $3, l);
3677 if (RootContext.WarningLevel >= 4){
3678 if ($3 == EmptyStatement.Value)
3679 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3683 | boolean_expression CLOSE_PARENS
3684 embedded_statement ELSE embedded_statement
3686 Location l = (Location) oob_stack.Pop ();
3688 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3693 : SWITCH OPEN_PARENS
3695 oob_stack.Push (lexer.Location);
3696 switch_stack.Push (current_block);
3698 expression CLOSE_PARENS
3701 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3702 current_block = (Block) switch_stack.Pop ();
3718 Report.Error (1522, lexer.Location, "Empty switch block");
3726 ArrayList sections = new ArrayList (4);
3731 | switch_sections switch_section
3733 ArrayList sections = (ArrayList) $1;
3743 current_block = current_block.CreateSwitchBlock (lexer.Location);
3747 Block topmost = current_block;
3749 while (topmost.Implicit)
3750 topmost = topmost.Parent;
3751 $$ = new SwitchSection ((ArrayList) $1, topmost);
3758 ArrayList labels = new ArrayList (4);
3763 | switch_labels switch_label
3765 ArrayList labels = (ArrayList) ($1);
3773 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3774 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3777 1523, lexer.Location,
3778 "The keyword case or default must precede code in switch block");
3792 oob_stack.Push (lexer.Location);
3794 boolean_expression CLOSE_PARENS embedded_statement
3796 Location l = (Location) oob_stack.Pop ();
3797 $$ = new While ((Expression) $4, (Statement) $6, l);
3799 if (RootContext.WarningLevel >= 4){
3800 if ($6 == EmptyStatement.Value)
3801 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3807 : DO embedded_statement
3809 oob_stack.Push (lexer.Location);
3811 boolean_expression CLOSE_PARENS SEMICOLON
3813 Location l = (Location) oob_stack.Pop ();
3815 $$ = new Do ((Statement) $2, (Expression) $6, l);
3821 opt_for_initializer SEMICOLON
3823 Block assign_block = new Block (current_block);
3824 current_block = assign_block;
3826 if ($3 is DictionaryEntry){
3827 DictionaryEntry de = (DictionaryEntry) $3;
3829 Expression type = (Expression) de.Key;
3830 ArrayList var_declarators = (ArrayList) de.Value;
3832 foreach (VariableDeclaration decl in var_declarators){
3836 vi = current_block.AddVariable (
3837 type, decl.identifier, current_local_parameters, decl.Location);
3841 Location l = lexer.Location;
3843 if (decl.expression_or_array_initializer is Expression){
3844 expr = (Expression) decl.expression_or_array_initializer;
3845 } else if (decl.expression_or_array_initializer == null) {
3848 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3849 expr = new ArrayCreation (type, "", init, decl.Location);
3852 LocalVariableReference var;
3853 var = new LocalVariableReference (assign_block, decl.identifier, l);
3856 Assign a = new Assign (var, expr, decl.Location);
3858 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3864 oob_stack.Push (lexer.Location);
3866 opt_for_condition SEMICOLON
3867 opt_for_iterator CLOSE_PARENS
3870 Location l = (Location) oob_stack.Pop ();
3872 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
3874 if (RootContext.WarningLevel >= 4){
3875 if ($10 == EmptyStatement.Value)
3876 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3879 current_block.AddStatement (f);
3880 while (current_block.Implicit)
3881 current_block = current_block.Parent;
3883 current_block = current_block.Parent;
3888 : /* empty */ { $$ = EmptyStatement.Value; }
3893 : local_variable_declaration
3894 | statement_expression_list
3898 : /* empty */ { $$ = null; }
3899 | boolean_expression
3903 : /* empty */ { $$ = EmptyStatement.Value; }
3908 : statement_expression_list
3911 statement_expression_list
3912 : statement_expression
3914 // CHANGE: was `null'
3915 Block b = new Block (current_block, Block.Flags.Implicit);
3917 b.AddStatement ((Statement) $1);
3920 | statement_expression_list COMMA statement_expression
3922 Block b = (Block) $1;
3924 b.AddStatement ((Statement) $3);
3930 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
3932 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
3935 | FOREACH OPEN_PARENS type IDENTIFIER IN
3937 oob_stack.Push (lexer.Location);
3939 expression CLOSE_PARENS
3941 oob_stack.Push (current_block);
3943 Block foreach_block = new Block (current_block);
3944 LocalVariableReference v = null;
3945 Location l = lexer.Location;
3948 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
3952 // Get a writable reference to this read-only variable.
3953 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
3955 current_block = foreach_block;
3958 oob_stack.Push (current_block);
3962 Block foreach_block = (Block) oob_stack.Pop ();
3963 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
3964 Block prev_block = (Block) oob_stack.Pop ();
3965 Location l = (Location) oob_stack.Pop ();
3967 current_block = prev_block;
3970 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
3971 foreach_block.AddStatement (f);
3980 | continue_statement
3990 $$ = new Break (lexer.Location);
3995 : CONTINUE SEMICOLON
3997 $$ = new Continue (lexer.Location);
4002 : GOTO IDENTIFIER SEMICOLON
4004 $$ = new Goto (current_block, (string) $2, lexer.Location);
4006 | GOTO CASE constant_expression SEMICOLON
4008 $$ = new GotoCase ((Expression) $3, lexer.Location);
4010 | GOTO DEFAULT SEMICOLON
4012 $$ = new GotoDefault (lexer.Location);
4017 : RETURN opt_expression SEMICOLON
4019 $$ = new Return ((Expression) $2, lexer.Location);
4024 : THROW opt_expression SEMICOLON
4026 $$ = new Throw ((Expression) $2, lexer.Location);
4031 : IDENTIFIER RETURN expression SEMICOLON
4033 string s = (string) $1;
4035 Report.Error (1003, lexer.Location, "; expected");
4038 if (RootContext.Version == LanguageVersion.ISO_1){
4039 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4042 if (iterator_container == null){
4043 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4046 iterator_container.SetYields ();
4047 $$ = new Yield ((Expression) $3, lexer.Location);
4050 | IDENTIFIER BREAK 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 YieldBreak (lexer.Location);
4077 : TRY block catch_clauses
4081 ArrayList c = (ArrayList)$3;
4082 for (int i = 0; i < c.Count; ++i) {
4083 Catch cc = (Catch) c [i];
4085 if (i != c.Count - 1)
4086 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4093 // Now s contains the list of specific catch clauses
4094 // and g contains the general one.
4096 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4098 | TRY block opt_catch_clauses FINALLY block
4101 ArrayList s = new ArrayList (4);
4102 ArrayList catch_list = (ArrayList) $3;
4104 if (catch_list != null){
4105 foreach (Catch cc in catch_list) {
4113 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4117 Report.Error (1524, lexer.Location, "Expected catch or finally");
4122 : /* empty */ { $$ = null; }
4129 ArrayList l = new ArrayList (4);
4134 | catch_clauses catch_clause
4136 ArrayList l = (ArrayList) $1;
4144 : /* empty */ { $$ = null; }
4149 : CATCH opt_catch_args
4151 Expression type = null;
4155 DictionaryEntry cc = (DictionaryEntry) $2;
4156 type = (Expression) cc.Key;
4157 id = (string) cc.Value;
4160 ArrayList one = new ArrayList (4);
4161 Location loc = lexer.Location;
4163 one.Add (new VariableDeclaration (id, null, loc));
4166 current_block = new Block (current_block);
4167 Block b = declare_local_variables (type, one, loc);
4172 Expression type = null;
4176 DictionaryEntry cc = (DictionaryEntry) $2;
4177 type = (Expression) cc.Key;
4178 id = (string) cc.Value;
4182 // FIXME: I can change this for an assignment.
4184 while (current_block != (Block) $1)
4185 current_block = current_block.Parent;
4190 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
4195 : /* empty */ { $$ = null; }
4200 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4202 $$ = new DictionaryEntry ($2, $3);
4209 $$ = new Checked ((Block) $2);
4216 $$ = new Unchecked ((Block) $2);
4223 if (!RootContext.Unsafe){
4224 Report.Error (227, lexer.Location,
4225 "Unsafe code can only be used if --unsafe is used");
4228 $$ = new Unsafe ((Block) $3);
4234 type fixed_pointer_declarators
4237 ArrayList list = (ArrayList) $4;
4238 Expression type = (Expression) $3;
4239 Location l = lexer.Location;
4240 int top = list.Count;
4242 Block assign_block = new Block (current_block);
4243 current_block = assign_block;
4245 for (int i = 0; i < top; i++){
4246 Pair p = (Pair) list [i];
4249 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4263 Location l = (Location) oob_stack.Pop ();
4265 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4267 if (RootContext.WarningLevel >= 4){
4268 if ($7 == EmptyStatement.Value)
4269 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4272 current_block.AddStatement (f);
4273 while (current_block.Implicit)
4274 current_block = current_block.Parent;
4276 current_block = current_block.Parent;
4280 fixed_pointer_declarators
4281 : fixed_pointer_declarator {
4282 ArrayList declarators = new ArrayList (4);
4284 declarators.Add ($1);
4287 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4289 ArrayList declarators = (ArrayList) $1;
4291 declarators.Add ($3);
4296 fixed_pointer_declarator
4297 : IDENTIFIER ASSIGN expression
4299 $$ = new Pair ($1, $3);
4303 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4309 : LOCK OPEN_PARENS expression CLOSE_PARENS
4315 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4320 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4322 Block assign_block = new Block (current_block);
4323 current_block = assign_block;
4325 oob_stack.Push (lexer.Location);
4327 if ($3 is DictionaryEntry){
4328 DictionaryEntry de = (DictionaryEntry) $3;
4329 Location l = lexer.Location;
4331 Expression type = (Expression) de.Key;
4332 ArrayList var_declarators = (ArrayList) de.Value;
4334 ArrayList vars = new ArrayList (4);
4336 foreach (VariableDeclaration decl in var_declarators){
4338 LocalInfo vi = current_block.AddVariable (
4339 type, decl.identifier,
4340 current_local_parameters, decl.Location);
4346 if (decl.expression_or_array_initializer is Expression){
4347 expr = (Expression) decl.expression_or_array_initializer;
4349 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4351 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4354 expr = new ArrayCreation (type, "", init, decl.Location);
4357 LocalVariableReference var;
4359 // Get a writable reference to this read-only variable.
4360 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4362 // This is so that it is not a warning on using variables
4365 vars.Add (new DictionaryEntry (var, expr));
4367 // Assign a = new Assign (var, expr, decl.Location);
4368 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4370 $3 = new DictionaryEntry (type, vars);
4375 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4376 current_block.AddStatement (u);
4377 while (current_block.Implicit)
4378 current_block = current_block.Parent;
4380 current_block = current_block.Parent;
4384 resource_acquisition
4385 : local_variable_declaration
4392 // A class used to pass around variable declarations and constants
4394 public class VariableDeclaration {
4395 public string identifier;
4396 public object expression_or_array_initializer;
4397 public Location Location;
4398 public Attributes OptAttributes;
4399 public string DocComment;
4401 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4403 this.identifier = id;
4404 this.expression_or_array_initializer = eoai;
4406 this.OptAttributes = opt_attrs;
4409 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4415 /// Used to pass around interface property information
4417 public class InterfaceAccessorInfo {
4419 public readonly Accessor Get, Set;
4421 public InterfaceAccessorInfo (bool has_get, bool has_set,
4422 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4425 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4427 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4430 Get = new Accessor (null, 0, get_attrs, get_loc);
4432 Set = new Accessor (null, 0, set_attrs, set_loc);
4439 // A class used to hold info about an indexer declarator
4441 public class IndexerDeclaration {
4442 public Expression type;
4443 public MemberName interface_type;
4444 public Parameters param_list;
4446 public IndexerDeclaration (Expression type, MemberName interface_type,
4447 Parameters param_list)
4450 this.interface_type = interface_type;
4451 this.param_list = param_list;
4456 // We use this when we do not have an object in advance that is an IIteratorContainer
4458 public class SimpleIteratorContainer : IIteratorContainer {
4461 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4466 public static SimpleIteratorContainer GetSimple () {
4467 Simple.Yields = false;
4471 public void SetYields () { Yields = true; }
4475 // A class used to hold info about an operator declarator
4477 public class OperatorDeclaration {
4478 public Operator.OpType optype;
4479 public Expression ret_type, arg1type, arg2type;
4480 public string arg1name, arg2name;
4481 public Location location;
4483 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4484 Expression arg1type, string arg1name,
4485 Expression arg2type, string arg2name, Location location)
4488 this.ret_type = ret_type;
4489 this.arg1type = arg1type;
4490 this.arg1name = arg1name;
4491 this.arg2type = arg2type;
4492 this.arg2name = arg2name;
4493 this.location = location;
4498 void Error_ExpectingTypeName (Location l, Expression expr)
4500 if (expr is Invocation){
4501 Report.Error (1002, l, "; expected");
4503 Report.Error (-1, l, "Invalid Type definition");
4508 // Given the @class_name name, it creates a fully qualified name
4509 // based on the containing declaration space
4512 MakeName (MemberName class_name)
4514 string ns = current_namespace.FullName;
4516 if (current_container.Name == ""){
4518 return new MemberName (new MemberName (ns), class_name);
4522 return new MemberName (current_container.MemberName, class_name);
4526 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4528 Block implicit_block;
4529 ArrayList inits = null;
4532 // We use the `Used' property to check whether statements
4533 // have been added to the current block. If so, we need
4534 // to create another block to contain the new declaration
4535 // otherwise, as an optimization, we use the same block to
4536 // add the declaration.
4538 // FIXME: A further optimization is to check if the statements
4539 // that were added were added as part of the initialization
4540 // below. In which case, no other statements have been executed
4541 // and we might be able to reduce the number of blocks for
4542 // situations like this:
4544 // int j = 1; int k = j + 1;
4546 if (current_block.Used)
4547 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4549 implicit_block = current_block;
4551 foreach (VariableDeclaration decl in variable_declarators){
4553 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4554 if (decl.expression_or_array_initializer != null){
4556 inits = new ArrayList (4);
4563 return implicit_block;
4565 foreach (VariableDeclaration decl in inits){
4569 if (decl.expression_or_array_initializer is Expression){
4570 expr = (Expression) decl.expression_or_array_initializer;
4573 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4575 expr = new ArrayCreation (type, "", init, decl.Location);
4578 LocalVariableReference var;
4579 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4581 assign = new Assign (var, expr, decl.Location);
4583 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4586 return implicit_block;
4589 Block declare_local_constants (Expression type, ArrayList declarators)
4591 Block implicit_block;
4593 if (current_block.Used)
4594 implicit_block = new Block (current_block, Block.Flags.Implicit);
4596 implicit_block = current_block;
4598 foreach (VariableDeclaration decl in declarators){
4599 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4600 current_local_parameters, decl.Location);
4603 return implicit_block;
4606 void CheckAttributeTarget (string a)
4610 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4614 Location l = lexer.Location;
4615 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4621 void CheckUnaryOperator (Operator.OpType op)
4625 case Operator.OpType.LogicalNot:
4626 case Operator.OpType.OnesComplement:
4627 case Operator.OpType.Increment:
4628 case Operator.OpType.Decrement:
4629 case Operator.OpType.True:
4630 case Operator.OpType.False:
4631 case Operator.OpType.Addition:
4632 case Operator.OpType.Subtraction:
4637 Location l = lexer.Location;
4638 Report.Error (1019, l, "Overloadable unary operator expected");
4644 void CheckBinaryOperator (Operator.OpType op)
4648 case Operator.OpType.Addition:
4649 case Operator.OpType.Subtraction:
4650 case Operator.OpType.Multiply:
4651 case Operator.OpType.Division:
4652 case Operator.OpType.Modulus:
4653 case Operator.OpType.BitwiseAnd:
4654 case Operator.OpType.BitwiseOr:
4655 case Operator.OpType.ExclusiveOr:
4656 case Operator.OpType.LeftShift:
4657 case Operator.OpType.RightShift:
4658 case Operator.OpType.Equality:
4659 case Operator.OpType.Inequality:
4660 case Operator.OpType.GreaterThan:
4661 case Operator.OpType.LessThan:
4662 case Operator.OpType.GreaterThanOrEqual:
4663 case Operator.OpType.LessThanOrEqual:
4667 Location l = lexer.Location;
4668 Report.Error (1020, l, "Overloadable binary operator expected");
4674 void syntax_error (Location l, string msg)
4676 Report.Error (1003, l, "Syntax error, " + msg);
4679 void output (string s)
4681 Console.WriteLine (s);
4684 void note (string s)
4686 // Used to put annotations
4691 public Tokenizer Lexer {
4697 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4699 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4700 this.name = file.Name;
4702 current_container = RootContext.Tree.Types;
4703 current_container.NamespaceEntry = current_namespace;
4704 oob_stack = new Stack ();
4705 switch_stack = new Stack ();
4707 lexer = new Tokenizer (reader, file, defines);
4710 public void parse ()
4713 if (yacc_verbose_flag > 1)
4714 yyparse (lexer, new yydebug.yyDebugSimple ());
4717 Tokenizer tokenizer = lexer as Tokenizer;
4718 tokenizer.cleanup ();
4719 } catch (Exception e){
4721 // Removed for production use, use parser verbose to get the output.
4723 // Console.WriteLine (e);
4724 Report.Error (-25, lexer.Location, "Parsing error");
4725 if (yacc_verbose_flag > 0)
4726 Console.WriteLine (e);
4730 void CheckToken (int error, int yyToken, string msg)
4732 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4733 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4736 Report.Error (error, lexer.Location, msg);
4739 void CheckIdentifierToken (int yyToken)
4741 CheckToken (1041, yyToken, "Identifier expected");
4744 string ConsumeStoredComment ()
4746 string s = tmpComment;
4748 Lexer.doc_state = XmlCommentState.Allowed;