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;
99 bool has_get, has_set;
104 %token NONE /* This token is never returned by our lexer */
105 %token ERROR // This is used not by the parser, but by the tokenizer.
109 *These are the C# keywords
195 /* C# keywords which are not really keywords */
201 /* C# single character operators/punctuation. */
202 %token OPEN_BRACE "{"
203 %token CLOSE_BRACE "}"
204 %token OPEN_BRACKET "["
205 %token CLOSE_BRACKET "]"
206 %token OPEN_PARENS "("
207 %token CLOSE_PARENS ")"
220 %token BITWISE_AND "&"
221 %token BITWISE_OR "|"
228 /* C# multi-character operators. */
231 %token OP_SHIFT_LEFT "<<"
232 %token OP_SHIFT_RIGHT ">>"
239 %token OP_MULT_ASSIGN "*="
240 %token OP_DIV_ASSIGN "/="
241 %token OP_MOD_ASSIGN "%="
242 %token OP_ADD_ASSIGN "+="
243 %token OP_SUB_ASSIGN "-="
244 %token OP_SHIFT_LEFT_ASSIGN "<<="
245 %token OP_SHIFT_RIGHT_ASSIGN ">>="
246 %token OP_AND_ASSIGN "&="
247 %token OP_XOR_ASSIGN "^="
248 %token OP_OR_ASSIGN "|="
252 %token LITERAL_INTEGER "int literal"
253 %token LITERAL_FLOAT "float literal"
254 %token LITERAL_DOUBLE "double literal"
255 %token LITERAL_DECIMAL "decimal literal"
256 %token LITERAL_CHARACTER "character literal"
257 %token LITERAL_STRING "string literal"
260 %token CLOSE_PARENS_CAST
261 %token CLOSE_PARENS_NO_CAST
262 %token CLOSE_PARENS_OPEN_PARENS
263 %token CLOSE_PARENS_MINUS
265 /* Add precedence rules to solve dangling else s/r conflict */
274 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
276 %left STAR DIV PERCENT
277 %right BANG CARRET UMINUS
278 %nonassoc OP_INC OP_DEC
280 %left OPEN_BRACKET OPEN_BRACE
284 %start compilation_unit
288 : outer_declarations opt_EOF
289 | outer_declarations global_attributes opt_EOF
290 | global_attributes opt_EOF
291 | opt_EOF /* allow empty files */
297 Lexer.check_incorrect_doc_comment ();
301 Lexer.check_incorrect_doc_comment ();
307 | outer_declarations outer_declaration
312 | namespace_member_declaration
317 | using_directives using_directive
321 : using_alias_directive
323 if (RootContext.Documentation != null)
324 Lexer.doc_state = XmlCommentState.Allowed;
326 | using_namespace_directive
328 if (RootContext.Documentation != null)
329 Lexer.doc_state = XmlCommentState.Allowed;
333 using_alias_directive
334 : USING IDENTIFIER ASSIGN
335 namespace_or_type_name SEMICOLON
337 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
340 CheckIdentifierToken (yyToken);
344 using_namespace_directive
345 : USING namespace_name SEMICOLON
347 current_namespace.Using ((MemberName) $2, lexer.Location);
352 // Strictly speaking, namespaces don't have attributes but
353 // we parse global attributes along with namespace declarations and then
356 namespace_declaration
357 : opt_attributes NAMESPACE namespace_or_type_name
360 Report.Error(1671, Lexer.Location, "A namespace declaration cannot have modifiers or attributes");
363 MemberName name = (MemberName) $3;
365 current_namespace = new NamespaceEntry (
366 current_namespace, file, name.GetName (), lexer.Location);
368 namespace_body opt_semicolon
370 current_namespace = current_namespace.Parent;
385 : namespace_or_type_name
391 if (RootContext.Documentation != null)
392 Lexer.doc_state = XmlCommentState.Allowed;
395 opt_namespace_member_declarations
404 opt_namespace_member_declarations
406 | namespace_member_declarations
409 namespace_member_declarations
410 : namespace_member_declaration
411 | namespace_member_declarations namespace_member_declaration
414 namespace_member_declaration
418 DeclSpace ds = (DeclSpace)$1;
420 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
421 Report.Error (1527, lexer.Location,
422 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
425 current_namespace.DeclarationFound = true;
427 | namespace_declaration {
428 current_namespace.DeclarationFound = true;
431 | field_declaration {
432 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
434 | method_declaration {
435 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
442 | interface_declaration
444 | delegate_declaration
446 // Enable this when we have handled all errors, because this acts as a generic fallback
449 // Console.WriteLine ("Token=" + yyToken);
450 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
462 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
470 global_attrs_enabled = false;
475 global_attrs_enabled = false;
484 ArrayList sect = (ArrayList) $1;
486 if (global_attrs_enabled) {
487 if (current_attr_target == "module") {
488 CodeGen.Module.AddAttributes (sect);
490 } else if (current_attr_target == "assembly") {
491 CodeGen.Assembly.AddAttributes (sect);
494 $$ = new Attributes (sect);
497 if (RootContext.Documentation != null) {
498 Lexer.check_incorrect_doc_comment ();
500 XmlCommentState.Allowed;
504 $$ = new Attributes (sect);
506 current_attr_target = null;
508 | attribute_sections attribute_section
510 Attributes attrs = $1 as Attributes;
511 ArrayList sect = (ArrayList) $2;
513 if (global_attrs_enabled) {
514 if (current_attr_target == "module") {
515 CodeGen.Module.AddAttributes (sect);
517 } else if (current_attr_target == "assembly") {
518 CodeGen.Assembly.AddAttributes (sect);
522 attrs = new Attributes (sect);
524 attrs.AddAttributes (sect);
528 attrs = new Attributes (sect);
530 attrs.AddAttributes (sect);
533 current_attr_target = null;
538 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
542 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
548 attribute_target_specifier
549 : attribute_target COLON
551 current_attr_target = (string)$1;
559 CheckAttributeTarget ((string) $1);
562 | EVENT { $$ = "event"; }
563 | RETURN { $$ = "return"; }
569 ArrayList attrs = new ArrayList (4);
575 | attribute_list COMMA attribute
577 ArrayList attrs = (ArrayList) $1;
589 opt_attribute_arguments
591 Location loc = (Location) $2;
592 MemberName mname = (MemberName) $1;
593 MemberName left = mname.Left;
594 string identifier = mname.Name;
596 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
598 if (current_attr_target == "assembly" || current_attr_target == "module")
599 $$ = new GlobalAttribute (current_class, current_attr_target,
600 left_expr, identifier, (ArrayList) $3, loc);
602 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
607 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
610 opt_attribute_arguments
611 : /* empty */ { $$ = null; }
612 | OPEN_PARENS attribute_arguments CLOSE_PARENS
620 : opt_positional_argument_list
625 ArrayList args = new ArrayList (4);
631 | positional_argument_list COMMA named_argument_list
633 ArrayList args = new ArrayList (4);
639 | named_argument_list
641 ArrayList args = new ArrayList (4);
650 opt_positional_argument_list
651 : /* empty */ { $$ = null; }
652 | positional_argument_list
655 positional_argument_list
658 ArrayList args = new ArrayList (4);
659 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
663 | positional_argument_list COMMA expression
665 ArrayList args = (ArrayList) $1;
666 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
675 ArrayList args = new ArrayList (4);
680 | named_argument_list COMMA named_argument
682 ArrayList args = (ArrayList) $1;
687 | named_argument_list COMMA expression
689 Report.Error (1016, lexer.Location, "Named attribute argument expected");
695 : IDENTIFIER ASSIGN expression
697 $$ = new DictionaryEntry (
699 new Argument ((Expression) $3, Argument.AType.Expression));
705 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
708 opt_class_member_declarations
710 | class_member_declarations
713 class_member_declarations
714 : class_member_declaration
715 | class_member_declarations
716 class_member_declaration
719 class_member_declaration
720 : constant_declaration // done
721 | field_declaration // done
722 | method_declaration // done
723 | property_declaration // done
724 | event_declaration // done
725 | indexer_declaration // done
726 | operator_declaration // done
727 | constructor_declaration // done
728 | destructor_declaration // done
738 MemberName name = MakeName ((MemberName) $5);
739 bool partial = (bool) $3;
742 ClassPart part = PartialContainer.CreatePart (
743 current_namespace, current_class, name, (int) $2,
744 (Attributes) $1, Kind.Struct, lexer.Location);
746 current_container = part.PartialContainer;
747 current_class = part;
749 current_class = new Struct (
750 current_namespace, current_class, name, (int) $2,
751 (Attributes) $1, lexer.Location);
753 current_container.AddClassOrStruct (current_class);
754 current_container = current_class;
755 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
761 current_class.Bases = (ArrayList) $7;
763 if (RootContext.Documentation != null)
764 current_class.DocComment = Lexer.consume_doc_comment ();
768 if (RootContext.Documentation != null)
769 Lexer.doc_state = XmlCommentState.Allowed;
773 $$ = pop_current_class ();
775 | opt_attributes opt_modifiers opt_partial STRUCT error {
776 CheckIdentifierToken (yyToken);
783 if (RootContext.Documentation != null)
784 Lexer.doc_state = XmlCommentState.Allowed;
786 opt_struct_member_declarations CLOSE_BRACE
789 opt_struct_member_declarations
791 | struct_member_declarations
794 struct_member_declarations
795 : struct_member_declaration
796 | struct_member_declarations struct_member_declaration
799 struct_member_declaration
800 : constant_declaration
803 | property_declaration
805 | indexer_declaration
806 | operator_declaration
807 | constructor_declaration
811 * This is only included so we can flag error 575:
812 * destructors only allowed on class types
814 | destructor_declaration
825 int modflags = (int) $2;
826 foreach (VariableDeclaration constant in (ArrayList) $5){
827 Location l = constant.Location;
828 if ((modflags & Modifiers.STATIC) != 0) {
829 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
833 Const c = new Const (
834 current_class, (Expression) $4, (string) constant.identifier,
835 (Expression) constant.expression_or_array_initializer, modflags,
838 if (RootContext.Documentation != null) {
839 c.DocComment = Lexer.consume_doc_comment ();
840 Lexer.doc_state = XmlCommentState.Allowed;
842 current_container.AddConstant (c);
848 : constant_declarator
850 ArrayList constants = new ArrayList (4);
855 | constant_declarators COMMA constant_declarator
858 ArrayList constants = (ArrayList) $1;
865 : IDENTIFIER ASSIGN constant_expression
867 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
871 // A const field requires a value to be provided
872 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
884 Expression type = (Expression) $3;
887 foreach (VariableDeclaration var in (ArrayList) $4){
888 Field field = new Field (current_class, type, mod, var.identifier,
889 var.expression_or_array_initializer,
890 (Attributes) $1, var.Location);
892 if (RootContext.Documentation != null) {
893 field.DocComment = Lexer.consume_doc_comment ();
894 Lexer.doc_state = XmlCommentState.Allowed;
896 current_container.AddField (field);
903 fixed_variable_declarators
906 Expression type = (Expression) $4;
909 foreach (VariableDeclaration var in (ArrayList) $5) {
910 FixedField field = new FixedField (current_class, type, mod, var.identifier,
911 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
913 if (RootContext.Documentation != null) {
914 field.DocComment = Lexer.consume_doc_comment ();
915 Lexer.doc_state = XmlCommentState.Allowed;
917 current_container.AddField (field);
925 Report.Error (670, lexer.Location, "void type is not allowed for fields");
929 fixed_variable_declarators
930 : fixed_variable_declarator
932 ArrayList decl = new ArrayList (2);
936 | fixed_variable_declarators COMMA fixed_variable_declarator
938 ArrayList decls = (ArrayList) $1;
944 fixed_variable_declarator
945 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
947 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
952 : variable_declarator
954 ArrayList decl = new ArrayList (4);
959 | variable_declarators COMMA variable_declarator
961 ArrayList decls = (ArrayList) $1;
968 : IDENTIFIER ASSIGN variable_initializer
970 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
974 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
976 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
978 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
979 "To declare a fixed size buffer field, use the fixed keyword before the field type");
993 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
995 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
999 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1006 iterator_container = (IIteratorContainer) $1;
1007 if (RootContext.Documentation != null)
1008 Lexer.doc_state = XmlCommentState.NotAllowed;
1012 Method method = (Method) $1;
1013 Block b = (Block) $3;
1014 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1017 if ((method.ModFlags & extern_abstract) == 0){
1019 501, lexer.Location, current_container.MakeName (method.Name) +
1020 "must declare a body because it is not marked abstract or extern");
1023 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1025 179, lexer.Location, current_container.MakeName (method.Name) +
1026 " is declared extern, but has a body");
1030 method.Block = (ToplevelBlock) $3;
1031 current_container.AddMethod (method);
1033 current_local_parameters = null;
1034 iterator_container = null;
1036 if (RootContext.Documentation != null)
1037 Lexer.doc_state = XmlCommentState.Allowed;
1051 1585, lexer.Location, "Member modifier `" +
1052 Modifiers.Name (i) + "' must precede member type and name");
1063 type namespace_or_type_name
1064 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1066 MemberName name = (MemberName) $4;
1068 Method method = new Method (current_class, (Expression) $3, (int) $2,
1069 false, name, (Parameters) $6, (Attributes) $1,
1072 current_local_parameters = (Parameters) $6;
1074 if (RootContext.Documentation != null)
1075 method.DocComment = Lexer.consume_doc_comment ();
1081 VOID namespace_or_type_name
1082 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1084 MemberName name = (MemberName) $4;
1086 Method method = new Method (current_class, TypeManager.system_void_expr,
1087 (int) $2, false, name, (Parameters) $6,
1088 (Attributes) $1, lexer.Location);
1090 current_local_parameters = (Parameters) $6;
1092 if (RootContext.Documentation != null)
1093 method.DocComment = Lexer.consume_doc_comment ();
1100 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1102 Report.Error (1585, lexer.Location,
1103 String.Format ("Modifier {0} should appear before type",
1104 Modifiers.Name ((int) $4)));
1105 MemberName name = (MemberName) $4;
1107 Method method = new Method (current_class, TypeManager.system_void_expr,
1108 0, false, name, (Parameters) $7, (Attributes) $1,
1111 current_local_parameters = (Parameters) $7;
1113 if (RootContext.Documentation != null)
1114 method.DocComment = Lexer.consume_doc_comment ();
1122 | SEMICOLON { $$ = null; }
1125 opt_formal_parameter_list
1126 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1127 | formal_parameter_list
1130 formal_parameter_list
1133 ArrayList pars_list = (ArrayList) $1;
1135 Parameter [] pars = new Parameter [pars_list.Count];
1136 pars_list.CopyTo (pars);
1138 $$ = new Parameters (pars, null);
1140 | fixed_parameters COMMA parameter_array
1142 ArrayList pars_list = (ArrayList) $1;
1144 Parameter [] pars = new Parameter [pars_list.Count];
1145 pars_list.CopyTo (pars);
1147 $$ = new Parameters (pars, (Parameter) $3);
1149 | fixed_parameters COMMA ARGLIST
1151 ArrayList pars_list = (ArrayList) $1;
1153 Parameter [] pars = new Parameter [pars_list.Count];
1154 pars_list.CopyTo (pars);
1156 $$ = new Parameters (pars, true);
1158 | parameter_array COMMA fixed_parameters
1160 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1163 | ARGLIST COMMA fixed_parameters
1165 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1170 $$ = new Parameters (null, (Parameter) $1);
1174 $$ = new Parameters (null, true);
1181 ArrayList pars = new ArrayList (4);
1186 | fixed_parameters COMMA fixed_parameter
1188 ArrayList pars = (ArrayList) $1;
1197 opt_parameter_modifier
1201 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1, lexer.Location);
1204 opt_parameter_modifier
1206 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1208 Report.Error (1552, lexer.Location, "Array type specifier, [], must appear before parameter name");
1212 opt_parameter_modifier
1215 Report.Error (1001, lexer.Location, "Identifier expected");
1219 opt_parameter_modifier
1222 CheckIdentifierToken (yyToken);
1226 opt_parameter_modifier
1232 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1237 opt_parameter_modifier
1238 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1239 | parameter_modifier
1243 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1244 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1248 : opt_attributes PARAMS type IDENTIFIER
1250 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1, lexer.Location);
1251 note ("type must be a single-dimension array type");
1253 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1255 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1258 | opt_attributes PARAMS type error {
1259 CheckIdentifierToken (yyToken);
1264 property_declaration
1268 namespace_or_type_name
1270 if (RootContext.Documentation != null)
1271 tmpComment = Lexer.consume_doc_comment ();
1275 implicit_value_parameter_type = (Expression) $3;
1277 lexer.PropertyParsing = true;
1279 $$ = lexer.Location;
1281 iterator_container = SimpleIteratorContainer.GetSimple ();
1283 accessor_declarations
1285 lexer.PropertyParsing = false;
1286 has_get = has_set = false;
1294 Pair pair = (Pair) $8;
1295 Accessor get_block = (Accessor) pair.First;
1296 Accessor set_block = (Accessor) pair.Second;
1298 Location loc = (Location) $7;
1299 MemberName name = (MemberName) $4;
1301 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1302 name, (Attributes) $1, get_block, set_block, loc);
1303 if (SimpleIteratorContainer.Simple.Yields)
1306 current_container.AddProperty (prop);
1307 implicit_value_parameter_type = null;
1308 iterator_container = null;
1310 if (RootContext.Documentation != null)
1311 prop.DocComment = ConsumeStoredComment ();
1316 accessor_declarations
1317 : get_accessor_declaration
1319 $$ = new Pair ($1, null);
1321 | get_accessor_declaration accessor_declarations
1323 Pair pair = (Pair) $2;
1327 | set_accessor_declaration
1329 $$ = new Pair (null, $1);
1331 | set_accessor_declaration accessor_declarations
1333 Pair pair = (Pair) $2;
1339 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1344 get_accessor_declaration
1345 : opt_attributes opt_modifiers GET
1347 // If this is not the case, then current_local_parameters has already
1348 // been set in indexer_declaration
1349 if (parsing_indexer == false)
1350 current_local_parameters = null;
1352 current_local_parameters = indexer_parameters;
1353 lexer.PropertyParsing = false;
1358 Report.Error (1007, lexer.Location, "Property accessor already defined");
1361 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1363 current_local_parameters = null;
1364 lexer.PropertyParsing = true;
1366 if (RootContext.Documentation != null)
1367 if (Lexer.doc_state == XmlCommentState.Error)
1368 Lexer.doc_state = XmlCommentState.NotAllowed;
1372 set_accessor_declaration
1373 : opt_attributes opt_modifiers SET
1376 Parameter implicit_value_parameter = new Parameter (
1377 implicit_value_parameter_type, "value",
1378 Parameter.Modifier.NONE, null, lexer.Location);
1380 if (parsing_indexer == false) {
1381 args = new Parameter [1];
1382 args [0] = implicit_value_parameter;
1383 current_local_parameters = new Parameters (args, null);
1385 Parameter [] fpars = indexer_parameters.FixedParameters;
1388 int count = fpars.Length;
1390 args = new Parameter [count + 1];
1391 fpars.CopyTo (args, 0);
1392 args [count] = implicit_value_parameter;
1395 current_local_parameters = new Parameters (
1396 args, indexer_parameters.ArrayParameter);
1399 lexer.PropertyParsing = false;
1404 Report.Error (1007, lexer.Location, "Property accessor already defined");
1407 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1409 current_local_parameters = null;
1410 lexer.PropertyParsing = true;
1412 if (RootContext.Documentation != null
1413 && Lexer.doc_state == XmlCommentState.Error)
1414 Lexer.doc_state = XmlCommentState.NotAllowed;
1420 | SEMICOLON { $$ = null; }
1423 interface_declaration
1427 INTERFACE member_name
1429 MemberName name = MakeName ((MemberName) $5);
1430 bool partial = (bool) $3;
1433 ClassPart part = PartialContainer.CreatePart (
1434 current_namespace, current_class, name, (int) $2,
1435 (Attributes) $1, Kind.Interface, lexer.Location);
1437 current_container = part.PartialContainer;
1438 current_class = part;
1440 current_class = new Interface (
1441 current_namespace, current_class, name, (int) $2,
1442 (Attributes) $1, lexer.Location);
1444 current_container.AddInterface (current_class);
1445 current_container = current_class;
1446 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
1451 current_class.Bases = (ArrayList) $7;
1453 if (RootContext.Documentation != null) {
1454 current_class.DocComment = Lexer.consume_doc_comment ();
1455 Lexer.doc_state = XmlCommentState.Allowed;
1460 if (RootContext.Documentation != null)
1461 Lexer.doc_state = XmlCommentState.Allowed;
1465 $$ = pop_current_class ();
1467 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1468 CheckIdentifierToken (yyToken);
1474 opt_interface_member_declarations
1478 opt_interface_member_declarations
1480 | interface_member_declarations
1483 interface_member_declarations
1484 : interface_member_declaration
1485 | interface_member_declarations interface_member_declaration
1488 interface_member_declaration
1489 : interface_method_declaration
1494 Method m = (Method) $1;
1496 if (m.IsExplicitImpl)
1497 Report.Error (541, lexer.Location,
1498 "Explicit interface declaration can only be declared in a class or struct");
1500 current_container.AddMethod (m);
1502 if (RootContext.Documentation != null)
1503 Lexer.doc_state = XmlCommentState.Allowed;
1505 | interface_property_declaration
1510 Property p = (Property) $1;
1512 if (p.IsExplicitImpl)
1513 Report.Error (541, lexer.Location,
1514 "Explicit interface declaration can only be declared in a class or struct");
1516 current_container.AddProperty (p);
1518 if (RootContext.Documentation != null)
1519 Lexer.doc_state = XmlCommentState.Allowed;
1521 | interface_event_declaration
1524 Event e = (Event) $1;
1526 if (e.IsExplicitImpl)
1527 Report.Error (541, lexer.Location,
1528 "Explicit interface declaration can only be declared in a class or struct");
1530 current_container.AddEvent (e);
1533 if (RootContext.Documentation != null)
1534 Lexer.doc_state = XmlCommentState.Allowed;
1536 | interface_indexer_declaration
1541 Indexer i = (Indexer) $1;
1543 if (i.IsExplicitImpl)
1544 Report.Error (541, lexer.Location,
1545 "Explicit interface declaration can only be declared in a class or struct");
1547 current_container.AddIndexer (i);
1549 if (RootContext.Documentation != null)
1550 Lexer.doc_state = XmlCommentState.Allowed;
1552 | delegate_declaration
1554 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1558 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1560 | struct_declaration
1562 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1566 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1568 | interface_declaration
1570 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1572 | constant_declaration
1574 Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
1582 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1587 interface_method_declaration
1588 : opt_attributes opt_new type namespace_or_type_name
1589 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1592 MemberName name = (MemberName) $4;
1594 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1595 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1596 if (RootContext.Documentation != null)
1597 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1599 | opt_attributes opt_new type namespace_or_type_name
1600 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1601 OPEN_BRACE opt_statement_list CLOSE_BRACE
1603 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
1608 interface_property_declaration
1613 { lexer.PropertyParsing = true; }
1615 { lexer.PropertyParsing = false; }
1618 if ($3 == TypeManager.system_void_expr) {
1619 Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
1626 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1628 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1629 new MemberName ((string) $4), (Attributes) $1,
1630 pinfo.Get, pinfo.Set, lexer.Location);
1631 if (RootContext.Documentation != null)
1632 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1637 CheckIdentifierToken (yyToken);
1643 : opt_attributes opt_modifiers GET SEMICOLON
1644 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1645 | opt_attributes opt_modifiers GET OPEN_BRACE
1647 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
1650 | opt_attributes opt_modifiers SET SEMICOLON
1651 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1652 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1653 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1654 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1655 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1658 Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
1662 interface_event_declaration
1663 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1665 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1666 new MemberName ((string) $5), null,
1667 (Attributes) $1, lexer.Location);
1668 if (RootContext.Documentation != null)
1669 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1671 | opt_attributes opt_new EVENT type error {
1672 CheckIdentifierToken (yyToken);
1675 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1676 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1679 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1681 lexer.EventParsing = true;
1683 event_accessor_declarations
1685 lexer.EventParsing = false;
1688 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1693 interface_indexer_declaration
1694 : opt_attributes opt_new type THIS
1695 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1697 { lexer.PropertyParsing = true; }
1699 { lexer.PropertyParsing = false; }
1705 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1707 $$ = new Indexer (current_class, (Expression) $3,
1708 new MemberName (TypeContainer.DefaultIndexerName),
1709 (int) $2, true, (Parameters) $6, (Attributes) $1,
1710 info.Get, info.Set, lexer.Location);
1711 if (RootContext.Documentation != null)
1712 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1716 operator_declaration
1717 : opt_attributes opt_modifiers operator_declarator
1719 iterator_container = SimpleIteratorContainer.GetSimple ();
1726 OperatorDeclaration decl = (OperatorDeclaration) $3;
1728 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1730 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1731 if (decl.arg2type != null)
1732 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1734 Operator op = new Operator (
1735 current_class, decl.optype, decl.ret_type, (int) $2,
1736 new Parameters (param_list, null),
1737 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1739 if (RootContext.Documentation != null) {
1740 op.DocComment = tmpComment;
1741 Lexer.doc_state = XmlCommentState.Allowed;
1744 if (SimpleIteratorContainer.Simple.Yields)
1747 // Note again, checking is done in semantic analysis
1748 current_container.AddOperator (op);
1750 current_local_parameters = null;
1751 iterator_container = null;
1757 | SEMICOLON { $$ = null; }
1760 : type OPERATOR overloadable_operator
1761 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1763 Operator.OpType op = (Operator.OpType) $3;
1764 CheckUnaryOperator (op);
1766 if (op == Operator.OpType.Addition)
1767 op = Operator.OpType.UnaryPlus;
1769 if (op == Operator.OpType.Subtraction)
1770 op = Operator.OpType.UnaryNegation;
1772 Parameter [] pars = new Parameter [1];
1773 Expression type = (Expression) $5;
1775 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null, lexer.Location);
1777 current_local_parameters = new Parameters (pars, null);
1779 if (RootContext.Documentation != null) {
1780 tmpComment = Lexer.consume_doc_comment ();
1781 Lexer.doc_state = XmlCommentState.NotAllowed;
1784 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1785 null, null, lexer.Location);
1787 | type OPERATOR overloadable_operator
1789 type IDENTIFIER COMMA
1793 CheckBinaryOperator ((Operator.OpType) $3);
1795 Parameter [] pars = new Parameter [2];
1797 Expression typeL = (Expression) $5;
1798 Expression typeR = (Expression) $8;
1800 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null, lexer.Location);
1801 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null, lexer.Location);
1803 current_local_parameters = new Parameters (pars, null);
1805 if (RootContext.Documentation != null) {
1806 tmpComment = Lexer.consume_doc_comment ();
1807 Lexer.doc_state = XmlCommentState.NotAllowed;
1810 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1812 typeR, (string) $9, lexer.Location);
1814 | conversion_operator_declarator
1815 | type OPERATOR overloadable_operator
1817 type IDENTIFIER COMMA
1818 type IDENTIFIER COMMA
1822 Report.Error (1534, lexer.Location, "Overloaded binary operator '{0}' takes two parameters", $3);
1825 | type OPERATOR overloadable_operator
1826 OPEN_PARENS CLOSE_PARENS
1828 Report.Error (1535, lexer.Location, "Overloaded unary operator '{0}' takes one parameter", $3);
1833 overloadable_operator
1835 : BANG { $$ = Operator.OpType.LogicalNot; }
1836 | TILDE { $$ = Operator.OpType.OnesComplement; }
1837 | OP_INC { $$ = Operator.OpType.Increment; }
1838 | OP_DEC { $$ = Operator.OpType.Decrement; }
1839 | TRUE { $$ = Operator.OpType.True; }
1840 | FALSE { $$ = Operator.OpType.False; }
1841 // Unary and binary:
1842 | PLUS { $$ = Operator.OpType.Addition; }
1843 | MINUS { $$ = Operator.OpType.Subtraction; }
1845 | STAR { $$ = Operator.OpType.Multiply; }
1846 | DIV { $$ = Operator.OpType.Division; }
1847 | PERCENT { $$ = Operator.OpType.Modulus; }
1848 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1849 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1850 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1851 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1852 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1853 | OP_EQ { $$ = Operator.OpType.Equality; }
1854 | OP_NE { $$ = Operator.OpType.Inequality; }
1855 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1856 | OP_LT { $$ = Operator.OpType.LessThan; }
1857 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1858 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1861 conversion_operator_declarator
1862 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1864 Parameter [] pars = new Parameter [1];
1866 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null, lexer.Location);
1868 current_local_parameters = new Parameters (pars, null);
1870 if (RootContext.Documentation != null) {
1871 tmpComment = Lexer.consume_doc_comment ();
1872 Lexer.doc_state = XmlCommentState.NotAllowed;
1875 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1876 null, null, lexer.Location);
1878 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1880 Parameter [] pars = new Parameter [1];
1882 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null, lexer.Location);
1884 current_local_parameters = new Parameters (pars, null);
1886 if (RootContext.Documentation != null) {
1887 tmpComment = Lexer.consume_doc_comment ();
1888 Lexer.doc_state = XmlCommentState.NotAllowed;
1891 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1892 null, null, lexer.Location);
1896 syntax_error (lexer.Location, "'operator' expected");
1900 syntax_error (lexer.Location, "'operator' expected");
1904 constructor_declaration
1907 constructor_declarator
1910 Constructor c = (Constructor) $3;
1911 c.Block = (ToplevelBlock) $4;
1912 c.OptAttributes = (Attributes) $1;
1913 c.ModFlags = (int) $2;
1915 if (RootContext.Documentation != null)
1916 c.DocComment = ConsumeStoredComment ();
1918 if (c.Name == current_container.Basename){
1919 if ((c.ModFlags & Modifiers.STATIC) != 0){
1920 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1922 515, c.Location, String.Format (
1923 "`{0}.{1}': static constructor can not have access modifiers",
1924 c.Name, current_container.Name));
1927 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1929 if (c.Initializer != null){
1932 "Static constructors can not have an explicit this or base " +
1933 "constructor invocations");
1936 if (!c.Parameters.Empty){
1938 132, c.Location, "Static constructors should not have parameters");
1941 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1944 // We let another layer check the validity of the constructor.
1945 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1948 current_container.AddConstructor (c);
1950 current_local_parameters = null;
1951 if (RootContext.Documentation != null)
1952 Lexer.doc_state = XmlCommentState.Allowed;
1956 constructor_declarator
1959 if (RootContext.Documentation != null) {
1960 tmpComment = Lexer.consume_doc_comment ();
1961 Lexer.doc_state = XmlCommentState.Allowed;
1964 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1966 current_local_parameters = (Parameters) $4;
1968 opt_constructor_initializer
1970 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1971 (ConstructorInitializer) $8, (Location) $6);
1977 | SEMICOLON { $$ = null; }
1980 opt_constructor_initializer
1981 : /* empty */ { $$ = null; }
1982 | constructor_initializer
1985 constructor_initializer
1986 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1988 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1990 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1992 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1995 Report.Error (1018, lexer.Location, "Keyword this or base expected");
2001 : /* EMPTY */ { $$ = 0; }
2002 | UNSAFE { $$ = Modifiers.UNSAFE; }
2003 | EXTERN { $$ = Modifiers.EXTERN; }
2006 destructor_declaration
2007 : opt_attributes opt_finalizer TILDE
2009 if (RootContext.Documentation != null) {
2010 tmpComment = Lexer.consume_doc_comment ();
2011 Lexer.doc_state = XmlCommentState.NotAllowed;
2014 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2016 if ((string) $5 != current_container.Basename){
2017 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2018 } else if (current_container.Kind != Kind.Class){
2019 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2021 Location l = lexer.Location;
2024 if (!RootContext.StdLib && current_container.Name == "System.Object")
2025 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2027 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2029 if ((m & Modifiers.UNSAFE) != 0){
2030 if (!RootContext.Unsafe){
2031 Report.Error (227, l,
2032 "Unsafe code requires the -unsafe command " +
2033 "line option to be specified");
2037 Method d = new Destructor (
2038 current_class, TypeManager.system_void_expr, m, "Finalize",
2039 new Parameters (null, null), (Attributes) $1, l);
2040 if (RootContext.Documentation != null)
2041 d.DocComment = ConsumeStoredComment ();
2043 d.Block = (ToplevelBlock) $8;
2044 current_container.AddMethod (d);
2052 EVENT type variable_declarators SEMICOLON
2054 foreach (VariableDeclaration var in (ArrayList) $5) {
2056 MemberName name = new MemberName (var.identifier);
2058 Event e = new EventField (
2059 current_class, (Expression) $4, (int) $2, false, name,
2060 var.expression_or_array_initializer, (Attributes) $1,
2063 current_container.AddEvent (e);
2065 if (RootContext.Documentation != null) {
2066 e.DocComment = Lexer.consume_doc_comment ();
2067 Lexer.doc_state = XmlCommentState.Allowed;
2073 EVENT type namespace_or_type_name
2076 implicit_value_parameter_type = (Expression) $4;
2077 lexer.EventParsing = true;
2079 event_accessor_declarations
2081 lexer.EventParsing = false;
2085 Location loc = (Location) $7;
2088 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2091 Pair pair = (Pair) $9;
2093 MemberName name = (MemberName) $5;
2095 Event e = new EventProperty (
2096 current_class, (Expression) $4, (int) $2, false, name, null,
2097 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2099 if (RootContext.Documentation != null) {
2100 e.DocComment = Lexer.consume_doc_comment ();
2101 Lexer.doc_state = XmlCommentState.Allowed;
2104 current_container.AddEvent (e);
2105 implicit_value_parameter_type = null;
2108 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2109 MemberName mn = (MemberName) $5;
2111 if (mn.Left != null)
2112 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2114 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2116 if (RootContext.Documentation != null)
2117 Lexer.doc_state = XmlCommentState.Allowed;
2121 event_accessor_declarations
2122 : add_accessor_declaration remove_accessor_declaration
2124 $$ = new Pair ($1, $2);
2126 | remove_accessor_declaration add_accessor_declaration
2128 $$ = new Pair ($2, $1);
2130 | add_accessor_declaration { $$ = null; }
2131 | remove_accessor_declaration { $$ = null; }
2134 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2140 add_accessor_declaration
2141 : opt_attributes ADD
2143 Parameter [] args = new Parameter [1];
2144 Parameter implicit_value_parameter = new Parameter (
2145 implicit_value_parameter_type, "value",
2146 Parameter.Modifier.NONE, null, lexer.Location);
2148 args [0] = implicit_value_parameter;
2150 current_local_parameters = new Parameters (args, null);
2151 lexer.EventParsing = false;
2155 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2156 lexer.EventParsing = true;
2158 | opt_attributes ADD error {
2159 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2162 | opt_attributes modifiers ADD {
2163 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2168 remove_accessor_declaration
2169 : opt_attributes REMOVE
2171 Parameter [] args = new Parameter [1];
2172 Parameter implicit_value_parameter = new Parameter (
2173 implicit_value_parameter_type, "value",
2174 Parameter.Modifier.NONE, null, lexer.Location);
2176 args [0] = implicit_value_parameter;
2178 current_local_parameters = new Parameters (args, null);
2179 lexer.EventParsing = false;
2183 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2184 lexer.EventParsing = true;
2186 | opt_attributes REMOVE error {
2187 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2190 | opt_attributes modifiers REMOVE {
2191 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2197 : opt_attributes opt_modifiers indexer_declarator
2200 IndexerDeclaration decl = (IndexerDeclaration) $3;
2202 implicit_value_parameter_type = decl.type;
2204 lexer.PropertyParsing = true;
2205 parsing_indexer = true;
2207 indexer_parameters = decl.param_list;
2209 accessor_declarations
2211 lexer.PropertyParsing = false;
2212 has_get = has_set = false;
2213 parsing_indexer = false;
2220 // The signature is computed from the signature of the indexer. Look
2221 // at section 3.6 on the spec
2222 Location loc = (Location) $5;
2224 IndexerDeclaration decl = (IndexerDeclaration) $3;
2225 Pair pair = (Pair) $7;
2226 Accessor get_block = (Accessor) pair.First;
2227 Accessor set_block = (Accessor) pair.Second;
2230 if (decl.interface_type != null)
2231 name = new MemberName (decl.interface_type,
2232 TypeContainer.DefaultIndexerName);
2234 name = new MemberName (TypeContainer.DefaultIndexerName);
2236 indexer = new Indexer (current_class, decl.type, name,
2237 (int) $2, false, decl.param_list, (Attributes) $1,
2238 get_block, set_block, loc);
2239 if (RootContext.Documentation != null)
2240 indexer.DocComment = ConsumeStoredComment ();
2242 current_container.AddIndexer (indexer);
2244 current_local_parameters = null;
2245 implicit_value_parameter_type = null;
2246 indexer_parameters = null;
2251 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2253 Parameters pars = (Parameters) $4;
2254 if (pars.HasArglist) {
2255 // "__arglist is not valid in this context"
2256 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2257 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2258 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2260 if (RootContext.Documentation != null) {
2261 tmpComment = Lexer.consume_doc_comment ();
2262 Lexer.doc_state = XmlCommentState.Allowed;
2265 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2267 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2269 Parameters pars = (Parameters) $6;
2271 if (pars.HasArglist) {
2272 // "__arglist is not valid in this context"
2273 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2274 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2275 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2278 MemberName name = (MemberName) $2;
2279 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2281 if (RootContext.Documentation != null) {
2282 tmpComment = Lexer.consume_doc_comment ();
2283 Lexer.doc_state = XmlCommentState.Allowed;
2294 if (RootContext.Documentation != null)
2295 enumTypeComment = Lexer.consume_doc_comment ();
2300 bool partial = (bool) $3;
2303 Report.Error (267, lexer.Location, "The partial modifier can only appear before a 'class', 'struct', or 'interface'");
2304 break; // assumes that the parser put us in a switch
2307 Location enum_location = lexer.Location;
2309 MemberName name = MakeName (new MemberName ((string) $5));
2310 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2311 name, (Attributes) $1, enum_location);
2313 if (RootContext.Documentation != null)
2314 e.DocComment = enumTypeComment;
2316 foreach (VariableDeclaration ev in (ArrayList) $8) {
2317 e.AddEnumMember (ev.identifier,
2318 (Expression) ev.expression_or_array_initializer,
2319 ev.Location, ev.OptAttributes,
2323 current_container.AddEnum (e);
2324 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2331 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2332 | COLON type { $$ = $2; }
2338 if (RootContext.Documentation != null)
2339 Lexer.doc_state = XmlCommentState.Allowed;
2341 opt_enum_member_declarations
2343 // here will be evaluated after CLOSE_BLACE is consumed.
2344 if (RootContext.Documentation != null)
2345 Lexer.doc_state = XmlCommentState.Allowed;
2353 opt_enum_member_declarations
2354 : /* empty */ { $$ = new ArrayList (4); }
2355 | enum_member_declarations opt_comma { $$ = $1; }
2358 enum_member_declarations
2359 : enum_member_declaration
2361 ArrayList l = new ArrayList (4);
2366 | enum_member_declarations COMMA enum_member_declaration
2368 ArrayList l = (ArrayList) $1;
2376 enum_member_declaration
2377 : opt_attributes IDENTIFIER
2379 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2381 if (RootContext.Documentation != null) {
2382 vd.DocComment = Lexer.consume_doc_comment ();
2383 Lexer.doc_state = XmlCommentState.Allowed;
2388 | opt_attributes IDENTIFIER
2390 $$ = lexer.Location;
2391 if (RootContext.Documentation != null) {
2392 tmpComment = Lexer.consume_doc_comment ();
2393 Lexer.doc_state = XmlCommentState.NotAllowed;
2398 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2400 if (RootContext.Documentation != null)
2401 vd.DocComment = ConsumeStoredComment ();
2407 delegate_declaration
2410 DELEGATE type member_name
2411 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2414 Location l = lexer.Location;
2415 MemberName name = MakeName ((MemberName) $5);
2416 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2417 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2419 if (RootContext.Documentation != null) {
2420 del.DocComment = Lexer.consume_doc_comment ();
2421 Lexer.doc_state = XmlCommentState.Allowed;
2424 current_container.AddDelegate (del);
2425 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2430 namespace_or_type_name
2432 | namespace_or_type_name DOT IDENTIFIER {
2433 $$ = new MemberName ((MemberName) $1, (string) $3);
2439 $$ = new MemberName ((string) $1);
2444 * Before you think of adding a return_type, notice that we have been
2445 * using two rules in the places where it matters (one rule using type
2446 * and another identical one that uses VOID as the return type). This
2447 * gets rid of a shift/reduce couple
2450 : namespace_or_type_name
2452 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2464 // Note that here only unmanaged types are allowed but we
2465 // can't perform checks during this phase - we do it during
2466 // semantic analysis.
2468 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2472 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2478 | non_expression_type rank_specifier
2480 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2482 | non_expression_type STAR
2484 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2486 | expression rank_specifiers
2488 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2492 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2496 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2497 // through this different path
2499 | multiplicative_expression STAR
2501 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2508 ArrayList types = new ArrayList (4);
2513 | type_list COMMA type
2515 ArrayList types = (ArrayList) $1;
2523 * replaces all the productions for isolating the various
2524 * simple types, but we need this to reuse it easily in local_variable_type
2527 : OBJECT { $$ = TypeManager.system_object_expr; }
2528 | STRING { $$ = TypeManager.system_string_expr; }
2529 | BOOL { $$ = TypeManager.system_boolean_expr; }
2530 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2531 | FLOAT { $$ = TypeManager.system_single_expr; }
2532 | DOUBLE { $$ = TypeManager.system_double_expr; }
2537 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2538 | BYTE { $$ = TypeManager.system_byte_expr; }
2539 | SHORT { $$ = TypeManager.system_int16_expr; }
2540 | USHORT { $$ = TypeManager.system_uint16_expr; }
2541 | INT { $$ = TypeManager.system_int32_expr; }
2542 | UINT { $$ = TypeManager.system_uint32_expr; }
2543 | LONG { $$ = TypeManager.system_int64_expr; }
2544 | ULONG { $$ = TypeManager.system_uint64_expr; }
2545 | CHAR { $$ = TypeManager.system_char_expr; }
2546 | VOID { $$ = TypeManager.system_void_expr; }
2550 : type rank_specifiers
2552 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2557 // Expressions, section 7.5
2567 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2569 | parenthesized_expression
2571 | invocation_expression
2575 | post_increment_expression
2576 | post_decrement_expression
2580 | checked_expression
2581 | unchecked_expression
2582 | pointer_member_access
2583 | anonymous_method_expression
2590 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2591 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2592 | NULL { $$ = NullLiteral.Null; }
2596 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2597 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2598 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2603 object v = lexer.Value;
2609 $$ = IntLiteral.Zero;
2611 $$ = IntLiteral.One;
2613 $$ = new IntLiteral (i);
2614 } else if (v is uint)
2615 $$ = new UIntLiteral ((UInt32) v);
2617 $$ = new LongLiteral ((Int64) v);
2618 else if (v is ulong)
2619 $$ = new ULongLiteral ((UInt64) v);
2621 Console.WriteLine ("OOPS. Unexpected result from scanner");
2626 : TRUE { $$ = new BoolLiteral (true); }
2627 | FALSE { $$ = new BoolLiteral (false); }
2630 parenthesized_expression_0
2631 : OPEN_PARENS expression CLOSE_PARENS
2634 lexer.Deambiguate_CloseParens ();
2635 // After this, the next token returned is one of
2636 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2637 // or CLOSE_PARENS_MINUS.
2641 parenthesized_expression
2642 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2646 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2648 // If a parenthesized expression is followed by a minus, we need to wrap
2649 // the expression inside a ParenthesizedExpression for the CS0075 check
2650 // in Binary.DoResolve().
2651 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2656 : primary_expression DOT IDENTIFIER
2658 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2660 | predefined_type DOT IDENTIFIER
2662 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2670 invocation_expression
2671 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2674 Location l = lexer.Location;
2675 Report.Error (1, l, "Parse error");
2677 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2679 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2681 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2683 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2685 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2687 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2689 ArrayList args = new ArrayList (1);
2691 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2693 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2695 ArrayList args = ((ArrayList) $4);
2697 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2702 : /* empty */ { $$ = null; }
2709 ArrayList list = new ArrayList (4);
2713 | argument_list COMMA argument
2715 ArrayList list = (ArrayList) $1;
2719 | argument_list error {
2720 CheckToken (1026, yyToken, ", or ) expected");
2727 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2729 | non_simple_argument
2736 : REF variable_reference
2738 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2740 | OUT variable_reference
2742 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2744 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2746 ArrayList list = (ArrayList) $3;
2747 Argument[] args = new Argument [list.Count];
2748 list.CopyTo (args, 0);
2750 Expression expr = new Arglist (args, lexer.Location);
2751 $$ = new Argument (expr, Argument.AType.Expression);
2755 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2760 : expression { note ("section 5.4"); $$ = $1; }
2764 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2766 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2768 | primary_expression rank_specifiers
2770 // So the super-trick is that primary_expression
2771 // can only be either a SimpleName or a MemberAccess.
2772 // The MemberAccess case arises when you have a fully qualified type-name like :
2774 // SimpleName is when you have
2777 Expression expr = (Expression) $1;
2778 if (expr is ComposedCast){
2779 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2780 } else if (!(expr is SimpleName || expr is MemberAccess)){
2781 Error_ExpectingTypeName (lexer.Location, expr);
2782 $$ = TypeManager.system_object_expr;
2785 // So we extract the string corresponding to the SimpleName
2788 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2796 ArrayList list = new ArrayList (4);
2800 | expression_list COMMA expression
2802 ArrayList list = (ArrayList) $1;
2811 $$ = new This (current_block, lexer.Location);
2816 : BASE DOT IDENTIFIER
2818 $$ = new BaseAccess ((string) $3, lexer.Location);
2820 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2822 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2825 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2830 post_increment_expression
2831 : primary_expression OP_INC
2833 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2834 (Expression) $1, lexer.Location);
2838 post_decrement_expression
2839 : primary_expression OP_DEC
2841 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2842 (Expression) $1, lexer.Location);
2847 : object_or_delegate_creation_expression
2848 | array_creation_expression
2851 object_or_delegate_creation_expression
2852 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2854 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2858 array_creation_expression
2859 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2861 opt_array_initializer
2863 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2865 | NEW type rank_specifiers array_initializer
2867 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2871 Report.Error (1031, lexer.Location, "Type expected");
2876 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2892 : rank_specifier opt_rank_specifier
2894 $$ = (string) $2 + (string) $1;
2899 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2901 $$ = "[" + (string) $2 + "]";
2921 | dim_separators COMMA
2923 $$ = (string) $1 + ",";
2927 opt_array_initializer
2939 : OPEN_BRACE CLOSE_BRACE
2941 ArrayList list = new ArrayList (4);
2944 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2946 $$ = (ArrayList) $2;
2950 variable_initializer_list
2951 : variable_initializer
2953 ArrayList list = new ArrayList (4);
2957 | variable_initializer_list COMMA variable_initializer
2959 ArrayList list = (ArrayList) $1;
2966 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2968 $$ = new TypeOfVoid (lexer.Location);
2970 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2972 $$ = new TypeOf ((Expression) $3, lexer.Location);
2977 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2978 $$ = new SizeOf ((Expression) $3, lexer.Location);
2983 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2985 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2989 unchecked_expression
2990 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2992 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2996 pointer_member_access
2997 : primary_expression OP_PTR IDENTIFIER
3001 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3002 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3006 anonymous_method_expression
3007 : DELEGATE opt_anonymous_method_signature _mark_
3009 oob_stack.Push (current_local_parameters);
3010 current_local_parameters = (Parameters)$2;
3012 // Force the next block to be created as a ToplevelBlock
3013 oob_stack.Push (current_block);
3014 oob_stack.Push (top_current_block);
3015 current_block = null;
3019 Location loc = (Location) $3;
3020 top_current_block = (Block) oob_stack.Pop ();
3021 current_block = (Block) oob_stack.Pop ();
3022 if (RootContext.Version == LanguageVersion.ISO_1){
3023 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3026 ToplevelBlock anon_block = (ToplevelBlock) $5;
3028 anon_block.Parent = current_block;
3029 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3032 current_local_parameters = (Parameters) oob_stack.Pop ();
3036 opt_anonymous_method_signature
3037 : /* empty */ { $$ = null; }
3038 | anonymous_method_signature
3041 anonymous_method_signature
3042 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3045 $$ = Parameters.EmptyReadOnlyParameters;
3047 ArrayList par_list = (ArrayList) $2;
3048 Parameter [] pars = new Parameter [par_list.Count];
3049 par_list.CopyTo (pars);
3050 $$ = new Parameters (pars, null);
3055 opt_anonymous_method_parameter_list
3056 : /* empty */ { $$ = null; }
3057 | anonymous_method_parameter_list { $$ = $1; }
3060 anonymous_method_parameter_list
3061 : anonymous_method_parameter
3063 ArrayList a = new ArrayList (4);
3067 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3069 ArrayList a = (ArrayList) $1;
3075 anonymous_method_parameter
3076 : opt_parameter_modifier type IDENTIFIER {
3077 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null, lexer.Location);
3079 | PARAMS type IDENTIFIER {
3080 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3086 : primary_expression
3087 | BANG prefixed_unary_expression
3089 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3091 | TILDE prefixed_unary_expression
3093 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3099 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3101 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3103 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3105 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3111 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3113 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3118 // The idea to split this out is from Rhys' grammar
3119 // to solve the problem with casts.
3121 prefixed_unary_expression
3123 | PLUS prefixed_unary_expression
3125 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3127 | MINUS prefixed_unary_expression
3129 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3131 | OP_INC prefixed_unary_expression
3133 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3134 (Expression) $2, lexer.Location);
3136 | OP_DEC prefixed_unary_expression
3138 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3139 (Expression) $2, lexer.Location);
3141 | STAR prefixed_unary_expression
3143 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3145 | BITWISE_AND prefixed_unary_expression
3147 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3151 pre_increment_expression
3152 : OP_INC prefixed_unary_expression
3154 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3155 (Expression) $2, lexer.Location);
3159 pre_decrement_expression
3160 : OP_DEC prefixed_unary_expression
3162 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3163 (Expression) $2, lexer.Location);
3167 multiplicative_expression
3168 : prefixed_unary_expression
3169 | multiplicative_expression STAR prefixed_unary_expression
3171 $$ = new Binary (Binary.Operator.Multiply,
3172 (Expression) $1, (Expression) $3, lexer.Location);
3174 | multiplicative_expression DIV prefixed_unary_expression
3176 $$ = new Binary (Binary.Operator.Division,
3177 (Expression) $1, (Expression) $3, lexer.Location);
3179 | multiplicative_expression PERCENT prefixed_unary_expression
3181 $$ = new Binary (Binary.Operator.Modulus,
3182 (Expression) $1, (Expression) $3, lexer.Location);
3187 : multiplicative_expression
3188 | additive_expression PLUS multiplicative_expression
3190 $$ = new Binary (Binary.Operator.Addition,
3191 (Expression) $1, (Expression) $3, lexer.Location);
3193 | additive_expression MINUS multiplicative_expression
3195 $$ = new Binary (Binary.Operator.Subtraction,
3196 (Expression) $1, (Expression) $3, lexer.Location);
3201 : additive_expression
3202 | shift_expression OP_SHIFT_LEFT additive_expression
3204 $$ = new Binary (Binary.Operator.LeftShift,
3205 (Expression) $1, (Expression) $3, lexer.Location);
3207 | shift_expression OP_SHIFT_RIGHT additive_expression
3209 $$ = new Binary (Binary.Operator.RightShift,
3210 (Expression) $1, (Expression) $3, lexer.Location);
3214 relational_expression
3216 | relational_expression OP_LT shift_expression
3218 $$ = new Binary (Binary.Operator.LessThan,
3219 (Expression) $1, (Expression) $3, lexer.Location);
3221 | relational_expression OP_GT shift_expression
3223 $$ = new Binary (Binary.Operator.GreaterThan,
3224 (Expression) $1, (Expression) $3, lexer.Location);
3226 | relational_expression OP_LE shift_expression
3228 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3229 (Expression) $1, (Expression) $3, lexer.Location);
3231 | relational_expression OP_GE shift_expression
3233 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3234 (Expression) $1, (Expression) $3, lexer.Location);
3236 | relational_expression IS type
3238 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3240 | relational_expression AS type
3242 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3247 : relational_expression
3248 | equality_expression OP_EQ relational_expression
3250 $$ = new Binary (Binary.Operator.Equality,
3251 (Expression) $1, (Expression) $3, lexer.Location);
3253 | equality_expression OP_NE relational_expression
3255 $$ = new Binary (Binary.Operator.Inequality,
3256 (Expression) $1, (Expression) $3, lexer.Location);
3261 : equality_expression
3262 | and_expression BITWISE_AND equality_expression
3264 $$ = new Binary (Binary.Operator.BitwiseAnd,
3265 (Expression) $1, (Expression) $3, lexer.Location);
3269 exclusive_or_expression
3271 | exclusive_or_expression CARRET and_expression
3273 $$ = new Binary (Binary.Operator.ExclusiveOr,
3274 (Expression) $1, (Expression) $3, lexer.Location);
3278 inclusive_or_expression
3279 : exclusive_or_expression
3280 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3282 $$ = new Binary (Binary.Operator.BitwiseOr,
3283 (Expression) $1, (Expression) $3, lexer.Location);
3287 conditional_and_expression
3288 : inclusive_or_expression
3289 | conditional_and_expression OP_AND inclusive_or_expression
3291 $$ = new Binary (Binary.Operator.LogicalAnd,
3292 (Expression) $1, (Expression) $3, lexer.Location);
3296 conditional_or_expression
3297 : conditional_and_expression
3298 | conditional_or_expression OP_OR conditional_and_expression
3300 $$ = new Binary (Binary.Operator.LogicalOr,
3301 (Expression) $1, (Expression) $3, lexer.Location);
3305 conditional_expression
3306 : conditional_or_expression
3307 | conditional_or_expression INTERR expression COLON expression
3309 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3313 assignment_expression
3314 : prefixed_unary_expression ASSIGN expression
3316 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3318 | prefixed_unary_expression OP_MULT_ASSIGN expression
3320 Location l = lexer.Location;
3322 $$ = new CompoundAssign (
3323 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3325 | prefixed_unary_expression OP_DIV_ASSIGN expression
3327 Location l = lexer.Location;
3329 $$ = new CompoundAssign (
3330 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3332 | prefixed_unary_expression OP_MOD_ASSIGN expression
3334 Location l = lexer.Location;
3336 $$ = new CompoundAssign (
3337 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3339 | prefixed_unary_expression OP_ADD_ASSIGN expression
3341 Location l = lexer.Location;
3343 $$ = new CompoundAssign (
3344 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3346 | prefixed_unary_expression OP_SUB_ASSIGN expression
3348 Location l = lexer.Location;
3350 $$ = new CompoundAssign (
3351 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3353 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3355 Location l = lexer.Location;
3357 $$ = new CompoundAssign (
3358 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3360 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3362 Location l = lexer.Location;
3364 $$ = new CompoundAssign (
3365 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3367 | prefixed_unary_expression OP_AND_ASSIGN expression
3369 Location l = lexer.Location;
3371 $$ = new CompoundAssign (
3372 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3374 | prefixed_unary_expression OP_OR_ASSIGN expression
3376 Location l = lexer.Location;
3378 $$ = new CompoundAssign (
3379 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3381 | prefixed_unary_expression OP_XOR_ASSIGN expression
3383 Location l = lexer.Location;
3385 $$ = new CompoundAssign (
3386 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3391 : conditional_expression
3392 | assignment_expression
3412 MemberName name = MakeName ((MemberName) $5);
3413 bool partial = (bool) $3;
3414 int mod_flags = (int) $2;
3417 ClassPart part = PartialContainer.CreatePart (
3418 current_namespace, current_class, name, mod_flags,
3419 (Attributes) $1, Kind.Class, lexer.Location);
3421 current_container = part.PartialContainer;
3422 current_class = part;
3424 if ((mod_flags & Modifiers.STATIC) != 0) {
3425 current_class = new StaticClass (
3426 current_namespace, current_class, name,
3427 mod_flags, (Attributes) $1, lexer.Location);
3429 current_class = new Class (
3430 current_namespace, current_class, name,
3431 mod_flags, (Attributes) $1, lexer.Location);
3434 current_container.AddClassOrStruct (current_class);
3435 current_container = current_class;
3436 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3442 if (current_class.Name == "System.Object") {
3443 Report.Error (537, current_class.Location,
3444 "The class System.Object cannot have a base " +
3445 "class or implement an interface.");
3447 current_class.Bases = (ArrayList) $7;
3450 if (RootContext.Documentation != null) {
3451 current_class.DocComment = Lexer.consume_doc_comment ();
3452 Lexer.doc_state = XmlCommentState.Allowed;
3457 if (RootContext.Documentation != null)
3458 Lexer.doc_state = XmlCommentState.Allowed;
3462 $$ = pop_current_class ();
3468 { $$ = (bool) false; }
3470 { $$ = (bool) true; }
3474 : /* empty */ { $$ = (int) 0; }
3480 | modifiers modifier
3485 if ((m1 & m2) != 0) {
3486 Location l = lexer.Location;
3487 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3489 $$ = (int) (m1 | m2);
3494 : NEW { $$ = Modifiers.NEW; }
3495 | PUBLIC { $$ = Modifiers.PUBLIC; }
3496 | PROTECTED { $$ = Modifiers.PROTECTED; }
3497 | INTERNAL { $$ = Modifiers.INTERNAL; }
3498 | PRIVATE { $$ = Modifiers.PRIVATE; }
3499 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3500 | SEALED { $$ = Modifiers.SEALED; }
3501 | STATIC { $$ = Modifiers.STATIC; }
3502 | READONLY { $$ = Modifiers.READONLY; }
3503 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3504 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3505 | EXTERN { $$ = Modifiers.EXTERN; }
3506 | VOLATILE { $$ = Modifiers.VOLATILE; }
3507 | UNSAFE { $$ = Modifiers.UNSAFE; }
3511 : /* empty */ { $$ = null; }
3512 | class_base { $$ = $1; }
3516 : COLON type_list { $$ = $2; }
3524 // A block is "contained" on the following places:
3526 // property_declaration as part of the accessor body (get/set)
3527 // operator_declaration
3528 // constructor_declaration
3529 // destructor_declaration
3530 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3535 if (current_block == null){
3536 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3537 top_current_block = current_block;
3539 current_block = new Block (current_block, lexer.Location, Location.Null);
3542 opt_statement_list CLOSE_BRACE
3544 while (current_block.Implicit)
3545 current_block = current_block.Parent;
3547 current_block.SetEndLocation (lexer.Location);
3548 current_block = current_block.Parent;
3549 if (current_block == null)
3550 top_current_block = null;
3561 | statement_list statement
3565 : declaration_statement
3567 if ($1 != null && (Block) $1 != current_block){
3568 current_block.AddStatement ((Statement) $1);
3569 current_block = (Block) $1;
3572 | valid_declaration_statement
3574 current_block.AddStatement ((Statement) $1);
3579 valid_declaration_statement
3582 | expression_statement
3583 | selection_statement
3584 | iteration_statement
3588 | unchecked_statement
3596 : valid_declaration_statement
3597 | declaration_statement
3599 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3604 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3612 $$ = EmptyStatement.Value;
3619 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3621 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3622 current_block.AddStatement (labeled);
3627 declaration_statement
3628 : local_variable_declaration SEMICOLON
3631 DictionaryEntry de = (DictionaryEntry) $1;
3633 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3637 | local_constant_declaration SEMICOLON
3640 DictionaryEntry de = (DictionaryEntry) $1;
3642 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3648 * The following is from Rhys' grammar:
3649 * > Types in local variable declarations must be recognized as
3650 * > expressions to prevent reduce/reduce errors in the grammar.
3651 * > The expressions are converted into types during semantic analysis.
3654 : primary_expression opt_rank_specifier
3656 // FIXME: Do something smart here regarding the composition of the type.
3658 // Ok, the above "primary_expression" is there to get rid of
3659 // both reduce/reduce and shift/reduces in the grammar, it should
3660 // really just be "type_name". If you use type_name, a reduce/reduce
3661 // creeps up. If you use namespace_or_type_name (which is all we need
3662 // really) two shift/reduces appear.
3665 // So the super-trick is that primary_expression
3666 // can only be either a SimpleName or a MemberAccess.
3667 // The MemberAccess case arises when you have a fully qualified type-name like :
3669 // SimpleName is when you have
3672 Expression expr = (Expression) $1;
3673 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3674 Error_ExpectingTypeName (lexer.Location, expr);
3678 // So we extract the string corresponding to the SimpleName
3682 if ((string) $2 == "")
3685 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3688 | builtin_types opt_rank_specifier
3690 if ((string) $2 == "")
3693 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3697 local_variable_pointer_type
3698 : primary_expression STAR
3700 Expression expr = (Expression) $1;
3701 Location l = lexer.Location;
3703 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3704 Error_ExpectingTypeName (l, expr);
3708 $$ = new ComposedCast ((Expression) $1, "*", l);
3710 | builtin_types STAR
3712 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3716 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3718 | local_variable_pointer_type STAR
3720 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3724 local_variable_declaration
3725 : local_variable_type variable_declarators
3728 $$ = new DictionaryEntry ($1, $2);
3732 | local_variable_pointer_type opt_rank_specifier variable_declarators
3737 if ((string) $2 == "")
3738 t = (Expression) $1;
3740 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3741 $$ = new DictionaryEntry (t, $3);
3747 local_constant_declaration
3748 : CONST local_variable_type constant_declarators
3751 $$ = new DictionaryEntry ($2, $3);
3757 expression_statement
3758 : statement_expression SEMICOLON
3765 // We have to do the wrapping here and not in the case above,
3766 // because statement_expression is used for example in for_statement
3768 statement_expression
3769 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3770 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3771 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3772 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3773 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3774 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3775 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3777 Report.Error (1002, lexer.Location, "Expecting `;'");
3782 object_creation_expression
3783 : object_or_delegate_creation_expression
3784 { note ("complain if this is a delegate maybe?"); }
3793 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3796 Location l = (Location) $3;
3798 $$ = new If ((Expression) $4, (Statement) $6, l);
3800 if (RootContext.WarningLevel >= 4){
3801 if ($6 == EmptyStatement.Value)
3802 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3806 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3807 embedded_statement ELSE embedded_statement
3809 Location l = (Location) $3;
3811 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3816 : SWITCH OPEN_PARENS _mark_
3818 switch_stack.Push (current_block);
3820 expression CLOSE_PARENS
3823 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3824 current_block = (Block) switch_stack.Pop ();
3840 Report.Error (1522, lexer.Location, "Empty switch block");
3848 ArrayList sections = new ArrayList (4);
3853 | switch_sections switch_section
3855 ArrayList sections = (ArrayList) $1;
3865 current_block = current_block.CreateSwitchBlock (lexer.Location);
3869 Block topmost = current_block;
3871 while (topmost.Implicit)
3872 topmost = topmost.Parent;
3873 $$ = new SwitchSection ((ArrayList) $1, topmost);
3880 ArrayList labels = new ArrayList (4);
3885 | switch_labels switch_label
3887 ArrayList labels = (ArrayList) ($1);
3895 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3896 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3899 1523, lexer.Location,
3900 "The keyword case or default must precede code in switch block");
3912 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
3914 Location l = (Location) $3;
3915 $$ = new While ((Expression) $4, (Statement) $6, l);
3917 if (RootContext.WarningLevel >= 4){
3918 if ($6 == EmptyStatement.Value)
3919 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3925 : DO embedded_statement
3926 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
3928 Location l = (Location) $5;
3930 $$ = new Do ((Statement) $2, (Expression) $6, l);
3936 opt_for_initializer SEMICOLON _mark_
3938 Block assign_block = new Block (current_block);
3939 current_block = assign_block;
3941 if ($3 is DictionaryEntry){
3942 DictionaryEntry de = (DictionaryEntry) $3;
3944 Expression type = (Expression) de.Key;
3945 ArrayList var_declarators = (ArrayList) de.Value;
3947 foreach (VariableDeclaration decl in var_declarators){
3951 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
3955 Location l = lexer.Location;
3957 if (decl.expression_or_array_initializer is Expression){
3958 expr = (Expression) decl.expression_or_array_initializer;
3959 } else if (decl.expression_or_array_initializer == null) {
3962 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3963 expr = new ArrayCreation (type, "", init, decl.Location);
3966 LocalVariableReference var;
3967 var = new LocalVariableReference (assign_block, decl.identifier, l);
3970 Assign a = new Assign (var, expr, decl.Location);
3972 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3976 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
3977 // This can be referred to as $6 below.
3983 opt_for_condition SEMICOLON
3984 opt_for_iterator CLOSE_PARENS
3987 Location l = (Location) $5;
3989 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
3991 if (RootContext.WarningLevel >= 4){
3992 if ($11 == EmptyStatement.Value)
3993 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3996 current_block.AddStatement (f);
3997 while (current_block.Implicit)
3998 current_block = current_block.Parent;
4000 current_block = current_block.Parent;
4005 : /* empty */ { $$ = EmptyStatement.Value; }
4010 : local_variable_declaration
4011 | statement_expression_list
4015 : /* empty */ { $$ = null; }
4016 | boolean_expression
4020 : /* empty */ { $$ = EmptyStatement.Value; }
4025 : statement_expression_list
4028 statement_expression_list
4029 : statement_expression
4031 // CHANGE: was `null'
4032 Block b = new Block (current_block, Block.Flags.Implicit);
4034 b.AddStatement ((Statement) $1);
4037 | statement_expression_list COMMA statement_expression
4039 Block b = (Block) $1;
4041 b.AddStatement ((Statement) $3);
4047 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4049 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4052 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4053 expression CLOSE_PARENS
4055 Block foreach_block = new Block (current_block);
4056 current_block = foreach_block;
4058 Location l = lexer.Location;
4061 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, l);
4063 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4065 // Get a writable reference to this read-only variable.
4067 // Note that the $$ here refers to the value of _this_ code block,
4068 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4069 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4076 LocalVariableReference v = (LocalVariableReference) $9;
4077 Location l = (Location) $6;
4080 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4081 current_block.AddStatement (f);
4084 while (current_block.Implicit)
4085 current_block = current_block.Parent;
4087 current_block = current_block.Parent;
4093 | continue_statement
4103 $$ = new Break (lexer.Location);
4108 : CONTINUE SEMICOLON
4110 $$ = new Continue (lexer.Location);
4115 : GOTO IDENTIFIER SEMICOLON
4117 $$ = new Goto ((string) $2, lexer.Location);
4119 | GOTO CASE constant_expression SEMICOLON
4121 $$ = new GotoCase ((Expression) $3, lexer.Location);
4123 | GOTO DEFAULT SEMICOLON
4125 $$ = new GotoDefault (lexer.Location);
4130 : RETURN opt_expression SEMICOLON
4132 $$ = new Return ((Expression) $2, lexer.Location);
4137 : THROW opt_expression SEMICOLON
4139 $$ = new Throw ((Expression) $2, lexer.Location);
4144 : IDENTIFIER RETURN expression SEMICOLON
4146 string s = (string) $1;
4148 Report.Error (1003, lexer.Location, "; expected");
4151 if (RootContext.Version == LanguageVersion.ISO_1){
4152 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4155 if (iterator_container == null){
4156 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4159 iterator_container.SetYields ();
4160 $$ = new Yield ((Expression) $3, lexer.Location);
4163 | IDENTIFIER RETURN SEMICOLON
4165 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4168 | IDENTIFIER BREAK SEMICOLON
4170 string s = (string) $1;
4172 Report.Error (1003, lexer.Location, "; expected");
4175 if (RootContext.Version == LanguageVersion.ISO_1){
4176 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4179 if (iterator_container == null){
4180 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4183 iterator_container.SetYields ();
4184 $$ = new YieldBreak (lexer.Location);
4195 : TRY block catch_clauses
4199 ArrayList c = (ArrayList)$3;
4200 for (int i = 0; i < c.Count; ++i) {
4201 Catch cc = (Catch) c [i];
4203 if (i != c.Count - 1)
4204 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4211 // Now s contains the list of specific catch clauses
4212 // and g contains the general one.
4214 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4216 | TRY block opt_catch_clauses FINALLY block
4219 ArrayList s = new ArrayList (4);
4220 ArrayList catch_list = (ArrayList) $3;
4222 if (catch_list != null){
4223 foreach (Catch cc in catch_list) {
4231 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4235 Report.Error (1524, lexer.Location, "Expected catch or finally");
4240 : /* empty */ { $$ = null; }
4247 ArrayList l = new ArrayList (4);
4252 | catch_clauses catch_clause
4254 ArrayList l = (ArrayList) $1;
4262 : /* empty */ { $$ = null; }
4267 : CATCH opt_catch_args
4269 Expression type = null;
4273 DictionaryEntry cc = (DictionaryEntry) $2;
4274 type = (Expression) cc.Key;
4275 id = (string) cc.Value;
4278 ArrayList one = new ArrayList (4);
4279 Location loc = lexer.Location;
4281 one.Add (new VariableDeclaration (id, null, loc));
4283 current_block = new Block (current_block);
4284 Block b = declare_local_variables (type, one, loc);
4289 Expression type = null;
4293 DictionaryEntry cc = (DictionaryEntry) $2;
4294 type = (Expression) cc.Key;
4295 id = (string) cc.Value;
4298 while (current_block.Implicit)
4299 current_block = current_block.Parent;
4300 current_block = current_block.Parent;
4304 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4309 : /* empty */ { $$ = null; }
4314 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4316 $$ = new DictionaryEntry ($2, $3);
4323 $$ = new Checked ((Block) $2);
4330 $$ = new Unchecked ((Block) $2);
4337 if (!RootContext.Unsafe){
4338 Report.Error (227, lexer.Location,
4339 "Unsafe code can only be used if -unsafe is used");
4342 $$ = new Unsafe ((Block) $3);
4348 type fixed_pointer_declarators
4351 ArrayList list = (ArrayList) $4;
4352 Expression type = (Expression) $3;
4353 Location l = lexer.Location;
4354 int top = list.Count;
4356 Block assign_block = new Block (current_block);
4357 current_block = assign_block;
4359 for (int i = 0; i < top; i++){
4360 Pair p = (Pair) list [i];
4363 v = current_block.AddVariable (type, (string) p.First, l);
4367 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4375 Location l = (Location) $6;
4377 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4379 if (RootContext.WarningLevel >= 4){
4380 if ($8 == EmptyStatement.Value)
4381 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4384 current_block.AddStatement (f);
4385 while (current_block.Implicit)
4386 current_block = current_block.Parent;
4388 current_block = current_block.Parent;
4392 fixed_pointer_declarators
4393 : fixed_pointer_declarator {
4394 ArrayList declarators = new ArrayList (4);
4396 declarators.Add ($1);
4399 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4401 ArrayList declarators = (ArrayList) $1;
4403 declarators.Add ($3);
4408 fixed_pointer_declarator
4409 : IDENTIFIER ASSIGN expression
4411 $$ = new Pair ($1, $3);
4415 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4421 : LOCK OPEN_PARENS expression CLOSE_PARENS
4427 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4432 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4434 Block assign_block = new Block (current_block);
4435 current_block = assign_block;
4437 if ($3 is DictionaryEntry){
4438 DictionaryEntry de = (DictionaryEntry) $3;
4439 Location l = lexer.Location;
4441 Expression type = (Expression) de.Key;
4442 ArrayList var_declarators = (ArrayList) de.Value;
4444 ArrayList vars = new ArrayList (4);
4446 foreach (VariableDeclaration decl in var_declarators){
4448 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4451 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4454 if (decl.expression_or_array_initializer is Expression){
4455 expr = (Expression) decl.expression_or_array_initializer;
4457 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4459 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4462 expr = new ArrayCreation (type, "", init, decl.Location);
4465 LocalVariableReference var;
4467 // Get a writable reference to this read-only variable.
4468 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4470 // This is so that it is not a warning on using variables
4473 vars.Add (new DictionaryEntry (var, expr));
4475 // Assign a = new Assign (var, expr, decl.Location);
4476 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4479 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4480 // It can be referred to as $6 below.
4481 $$ = new DictionaryEntry (type, vars);
4488 Using u = new Using ($6, (Statement) $7, (Location) $5);
4489 current_block.AddStatement (u);
4490 while (current_block.Implicit)
4491 current_block = current_block.Parent;
4493 current_block = current_block.Parent;
4497 resource_acquisition
4498 : local_variable_declaration
4502 // Utility rule to save location information
4505 { $$ = lexer.Location; }
4510 // A class used to pass around variable declarations and constants
4512 public class VariableDeclaration {
4513 public string identifier;
4514 public object expression_or_array_initializer;
4515 public Location Location;
4516 public Attributes OptAttributes;
4517 public string DocComment;
4519 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4521 this.identifier = id;
4522 this.expression_or_array_initializer = eoai;
4524 this.OptAttributes = opt_attrs;
4527 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4533 /// Used to pass around interface property information
4535 public class InterfaceAccessorInfo {
4537 public readonly Accessor Get, Set;
4539 public InterfaceAccessorInfo (bool has_get, bool has_set,
4540 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4543 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4545 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4548 Get = new Accessor (null, 0, get_attrs, get_loc);
4550 Set = new Accessor (null, 0, set_attrs, set_loc);
4557 // A class used to hold info about an indexer declarator
4559 public class IndexerDeclaration {
4560 public Expression type;
4561 public MemberName interface_type;
4562 public Parameters param_list;
4564 public IndexerDeclaration (Expression type, MemberName interface_type,
4565 Parameters param_list)
4568 this.interface_type = interface_type;
4569 this.param_list = param_list;
4574 // We use this when we do not have an object in advance that is an IIteratorContainer
4576 public class SimpleIteratorContainer : IIteratorContainer {
4579 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4584 public static SimpleIteratorContainer GetSimple () {
4585 Simple.Yields = false;
4589 public void SetYields () { Yields = true; }
4593 // A class used to hold info about an operator declarator
4595 public class OperatorDeclaration {
4596 public Operator.OpType optype;
4597 public Expression ret_type, arg1type, arg2type;
4598 public string arg1name, arg2name;
4599 public Location location;
4601 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4602 Expression arg1type, string arg1name,
4603 Expression arg2type, string arg2name, Location location)
4606 this.ret_type = ret_type;
4607 this.arg1type = arg1type;
4608 this.arg1name = arg1name;
4609 this.arg2type = arg2type;
4610 this.arg2name = arg2name;
4611 this.location = location;
4616 void Error_ExpectingTypeName (Location l, Expression expr)
4618 if (expr is Invocation){
4619 Report.Error (1002, l, "; expected");
4621 Report.Error (-1, l, "Invalid Type definition");
4625 TypeContainer pop_current_class ()
4627 TypeContainer retval = current_class;
4629 current_class = current_class.Parent;
4630 current_container = current_container.Parent;
4632 if (current_class != current_container) {
4633 if (!(current_class is ClassPart) ||
4634 ((ClassPart) current_class).PartialContainer != current_container)
4635 throw new InternalErrorException ();
4636 } else if (current_container is ClassPart)
4637 current_container = ((ClassPart) current_class).PartialContainer;
4643 // Given the @class_name name, it creates a fully qualified name
4644 // based on the containing declaration space
4647 MakeName (MemberName class_name)
4649 Namespace ns = current_namespace.NS;
4651 if (current_container.Name == ""){
4653 return new MemberName (ns.MemberName, class_name);
4657 return new MemberName (current_container.MemberName, class_name);
4661 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4663 Block implicit_block;
4664 ArrayList inits = null;
4667 // We use the `Used' property to check whether statements
4668 // have been added to the current block. If so, we need
4669 // to create another block to contain the new declaration
4670 // otherwise, as an optimization, we use the same block to
4671 // add the declaration.
4673 // FIXME: A further optimization is to check if the statements
4674 // that were added were added as part of the initialization
4675 // below. In which case, no other statements have been executed
4676 // and we might be able to reduce the number of blocks for
4677 // situations like this:
4679 // int j = 1; int k = j + 1;
4681 if (current_block.Used)
4682 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4684 implicit_block = current_block;
4686 foreach (VariableDeclaration decl in variable_declarators){
4688 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4689 if (decl.expression_or_array_initializer != null){
4691 inits = new ArrayList (4);
4698 return implicit_block;
4700 foreach (VariableDeclaration decl in inits){
4704 if (decl.expression_or_array_initializer is Expression){
4705 expr = (Expression) decl.expression_or_array_initializer;
4708 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4710 expr = new ArrayCreation (type, "", init, decl.Location);
4713 LocalVariableReference var;
4714 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4716 assign = new Assign (var, expr, decl.Location);
4718 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4721 return implicit_block;
4724 Block declare_local_constants (Expression type, ArrayList declarators)
4726 Block implicit_block;
4728 if (current_block.Used)
4729 implicit_block = new Block (current_block, Block.Flags.Implicit);
4731 implicit_block = current_block;
4733 foreach (VariableDeclaration decl in declarators){
4734 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4737 return implicit_block;
4740 void CheckAttributeTarget (string a)
4744 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4748 Location l = lexer.Location;
4749 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4755 void CheckUnaryOperator (Operator.OpType op)
4759 case Operator.OpType.LogicalNot:
4760 case Operator.OpType.OnesComplement:
4761 case Operator.OpType.Increment:
4762 case Operator.OpType.Decrement:
4763 case Operator.OpType.True:
4764 case Operator.OpType.False:
4765 case Operator.OpType.Addition:
4766 case Operator.OpType.Subtraction:
4771 Location l = lexer.Location;
4772 Report.Error (1019, l, "Overloadable unary operator expected");
4778 void CheckBinaryOperator (Operator.OpType op)
4782 case Operator.OpType.Addition:
4783 case Operator.OpType.Subtraction:
4784 case Operator.OpType.Multiply:
4785 case Operator.OpType.Division:
4786 case Operator.OpType.Modulus:
4787 case Operator.OpType.BitwiseAnd:
4788 case Operator.OpType.BitwiseOr:
4789 case Operator.OpType.ExclusiveOr:
4790 case Operator.OpType.LeftShift:
4791 case Operator.OpType.RightShift:
4792 case Operator.OpType.Equality:
4793 case Operator.OpType.Inequality:
4794 case Operator.OpType.GreaterThan:
4795 case Operator.OpType.LessThan:
4796 case Operator.OpType.GreaterThanOrEqual:
4797 case Operator.OpType.LessThanOrEqual:
4801 Location l = lexer.Location;
4802 Report.Error (1020, l, "Overloadable binary operator expected");
4808 void syntax_error (Location l, string msg)
4810 Report.Error (1003, l, "Syntax error, " + msg);
4813 void note (string s)
4815 // Used to put annotations
4820 public Tokenizer Lexer {
4826 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4828 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4829 this.name = file.Name;
4831 current_container = RootContext.Tree.Types;
4832 // TODO: Make RootContext.Tree.Types a PartialContainer.
4833 current_class = current_container;
4834 current_container.NamespaceEntry = current_namespace;
4835 oob_stack = new Stack ();
4836 switch_stack = new Stack ();
4838 lexer = new Tokenizer (reader, file, defines);
4841 public void parse ()
4844 if (yacc_verbose_flag > 1)
4845 yyparse (lexer, new yydebug.yyDebugSimple ());
4848 Tokenizer tokenizer = lexer as Tokenizer;
4849 tokenizer.cleanup ();
4850 } catch (Exception e){
4852 // Removed for production use, use parser verbose to get the output.
4854 // Console.WriteLine (e);
4855 Report.Error (-25, lexer.Location, "Parsing error");
4856 if (yacc_verbose_flag > 0)
4857 Console.WriteLine (e);
4860 RootContext.Tree.Types.NamespaceEntry = null;
4863 void CheckToken (int error, int yyToken, string msg)
4865 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4866 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4869 Report.Error (error, lexer.Location, msg);
4872 void CheckIdentifierToken (int yyToken)
4874 CheckToken (1041, yyToken, "Identifier expected");
4877 string ConsumeStoredComment ()
4879 string s = tmpComment;
4881 Lexer.doc_state = XmlCommentState.Allowed;