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 /// Hack to help create non-typed array initializer
63 public static Expression current_array_type;
66 /// Used to determine if we are parsing the get/set pair
67 /// of an indexer or a property
72 /// An out-of-band stack.
81 static public int yacc_verbose_flag;
83 // Name of the file we are parsing
92 /// Temporary Xml documentation cache.
93 /// For enum types, we need one more temporary store.
96 string enumTypeComment;
98 /// Current attribute target
99 string current_attr_target;
101 /// assembly and module attribute definitions are enabled
102 bool global_attrs_enabled = true;
104 bool has_get, has_set;
109 %token NONE /* This token is never returned by our lexer */
110 %token ERROR // This is used not by the parser, but by the tokenizer.
114 *These are the C# keywords
200 /* C# keywords which are not really keywords */
206 /* C# single character operators/punctuation. */
207 %token OPEN_BRACE "{"
208 %token CLOSE_BRACE "}"
209 %token OPEN_BRACKET "["
210 %token CLOSE_BRACKET "]"
211 %token OPEN_PARENS "("
212 %token CLOSE_PARENS ")"
225 %token BITWISE_AND "&"
226 %token BITWISE_OR "|"
233 /* C# multi-character operators. */
234 %token DOUBLE_COLON "::"
237 %token OP_SHIFT_LEFT "<<"
238 %token OP_SHIFT_RIGHT ">>"
245 %token OP_MULT_ASSIGN "*="
246 %token OP_DIV_ASSIGN "/="
247 %token OP_MOD_ASSIGN "%="
248 %token OP_ADD_ASSIGN "+="
249 %token OP_SUB_ASSIGN "-="
250 %token OP_SHIFT_LEFT_ASSIGN "<<="
251 %token OP_SHIFT_RIGHT_ASSIGN ">>="
252 %token OP_AND_ASSIGN "&="
253 %token OP_XOR_ASSIGN "^="
254 %token OP_OR_ASSIGN "|="
258 %token LITERAL_INTEGER "int literal"
259 %token LITERAL_FLOAT "float literal"
260 %token LITERAL_DOUBLE "double literal"
261 %token LITERAL_DECIMAL "decimal literal"
262 %token LITERAL_CHARACTER "character literal"
263 %token LITERAL_STRING "string literal"
266 %token CLOSE_PARENS_CAST
267 %token CLOSE_PARENS_NO_CAST
268 %token CLOSE_PARENS_OPEN_PARENS
269 %token CLOSE_PARENS_MINUS
271 /* Add precedence rules to solve dangling else s/r conflict */
280 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
282 %left STAR DIV PERCENT
283 %right BANG CARRET UMINUS
284 %nonassoc OP_INC OP_DEC
286 %left OPEN_BRACKET OPEN_BRACE
290 %start compilation_unit
294 : outer_declarations opt_EOF
295 | outer_declarations global_attributes opt_EOF
296 | global_attributes opt_EOF
297 | opt_EOF /* allow empty files */
303 Lexer.check_incorrect_doc_comment ();
307 Lexer.check_incorrect_doc_comment ();
313 | outer_declarations outer_declaration
317 : extern_alias_directive
319 | namespace_member_declaration
322 extern_alias_directives
323 : extern_alias_directive
324 | extern_alias_directives extern_alias_directive;
326 extern_alias_directive
327 : EXTERN IDENTIFIER IDENTIFIER SEMICOLON
329 LocatedToken lt = (LocatedToken) $2;
332 Report.Error (1003, lt.Location, "'alias' expected");
333 } else if (RootContext.Version == LanguageVersion.ISO_1) {
334 Report.FeatureIsNotStandardized (lt.Location, "external alias");
336 lt = (LocatedToken) $3;
337 current_namespace.UsingExternalAlias (lt.Value, lt.Location);
344 | using_directives using_directive
348 : using_alias_directive
350 if (RootContext.Documentation != null)
351 Lexer.doc_state = XmlCommentState.Allowed;
353 | using_namespace_directive
355 if (RootContext.Documentation != null)
356 Lexer.doc_state = XmlCommentState.Allowed;
360 using_alias_directive
361 : USING IDENTIFIER ASSIGN
362 namespace_or_type_name SEMICOLON
364 LocatedToken lt = (LocatedToken) $2;
365 current_namespace.UsingAlias (lt.Value, (MemberName) $4, (Location) $1);
368 CheckIdentifierToken (yyToken, GetLocation ($2));
372 using_namespace_directive
373 : USING namespace_name SEMICOLON
375 current_namespace.Using ((MemberName) $2, (Location) $1);
380 // Strictly speaking, namespaces don't have attributes but
381 // we parse global attributes along with namespace declarations and then
384 namespace_declaration
385 : opt_attributes NAMESPACE namespace_or_type_name
387 MemberName name = (MemberName) $3;
390 Report.Error(1671, name.Location, "A namespace declaration cannot have modifiers or attributes");
393 current_namespace = new NamespaceEntry (
394 current_namespace, file, name.GetName ());
396 namespace_body opt_semicolon
398 current_namespace = current_namespace.Parent;
413 : namespace_or_type_name
419 if (RootContext.Documentation != null)
420 Lexer.doc_state = XmlCommentState.Allowed;
422 opt_extern_alias_directives
424 opt_namespace_member_declarations
433 opt_extern_alias_directives
435 | extern_alias_directives
438 opt_namespace_member_declarations
440 | namespace_member_declarations
443 namespace_member_declarations
444 : namespace_member_declaration
445 | namespace_member_declarations namespace_member_declaration
448 namespace_member_declaration
452 DeclSpace ds = (DeclSpace)$1;
454 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
455 Report.Error (1527, ds.Location,
456 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
459 current_namespace.DeclarationFound = true;
461 | namespace_declaration {
462 current_namespace.DeclarationFound = true;
465 | field_declaration {
466 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
468 | method_declaration {
469 Report.Error (116, ((MemberCore) $1).Location, "A namespace can only contain types and namespace declarations");
476 | interface_declaration
478 | delegate_declaration
480 // Enable this when we have handled all errors, because this acts as a generic fallback
483 // Console.WriteLine ("Token=" + yyToken);
484 // Report.Error (1518, GetLocation ($1), "Expected class, struct, interface, enum or delegate");
496 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
504 global_attrs_enabled = false;
509 global_attrs_enabled = false;
518 ArrayList sect = (ArrayList) $1;
520 if (global_attrs_enabled) {
521 if (current_attr_target == "module") {
522 CodeGen.Module.AddAttributes (sect);
524 } else if (current_attr_target != null && current_attr_target.Length > 0) {
525 CodeGen.Assembly.AddAttributes (sect);
528 $$ = new Attributes (sect);
531 if (RootContext.Documentation != null) {
532 Lexer.check_incorrect_doc_comment ();
534 XmlCommentState.Allowed;
538 $$ = new Attributes (sect);
540 current_attr_target = null;
542 | attribute_sections attribute_section
544 Attributes attrs = $1 as Attributes;
545 ArrayList sect = (ArrayList) $2;
547 if (global_attrs_enabled) {
548 if (current_attr_target == "module") {
549 CodeGen.Module.AddAttributes (sect);
551 } else if (current_attr_target == "assembly") {
552 CodeGen.Assembly.AddAttributes (sect);
556 attrs = new Attributes (sect);
558 attrs.AddAttributes (sect);
562 attrs = new Attributes (sect);
564 attrs.AddAttributes (sect);
567 current_attr_target = null;
572 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
576 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
582 attribute_target_specifier
583 : attribute_target COLON
585 current_attr_target = (string)$1;
593 LocatedToken lt = (LocatedToken) $1;
594 CheckAttributeTarget (lt.Value, lt.Location);
595 $$ = lt.Value; // Location won't be required anymore.
597 | EVENT { $$ = "event"; }
598 | RETURN { $$ = "return"; }
604 ArrayList attrs = new ArrayList (4);
610 | attribute_list COMMA attribute
612 ArrayList attrs = (ArrayList) $1;
620 : attribute_name opt_attribute_arguments
622 MemberName mname = (MemberName) $1;
623 object[] arguments = (object[]) $2;
624 MemberName left = mname.Left;
625 string identifier = mname.Name;
627 Expression left_expr = left == null ? null : left.GetTypeExpression ();
629 if (current_attr_target == "assembly" || current_attr_target == "module")
630 // FIXME: supply "nameEscaped" parameter here.
631 $$ = new GlobalAttribute (current_namespace, current_attr_target,
632 left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
634 $$ = new Attribute (current_attr_target, left_expr, identifier, arguments, mname.Location, lexer.IsEscapedIdentifier (mname.Location));
639 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
642 opt_attribute_arguments
643 : /* empty */ { $$ = null; }
644 | OPEN_PARENS attribute_arguments CLOSE_PARENS
652 : opt_positional_argument_list
657 $$ = new object [] { $1, null };
660 | positional_argument_list COMMA named_argument_list
662 $$ = new object[] { $1, $3 };
664 | named_argument_list
666 $$ = new object [] { null, $1 };
671 opt_positional_argument_list
672 : /* empty */ { $$ = null; }
673 | positional_argument_list
676 positional_argument_list
679 ArrayList args = new ArrayList (4);
680 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
684 | positional_argument_list COMMA expression
686 ArrayList args = (ArrayList) $1;
687 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
696 ArrayList args = new ArrayList (4);
701 | named_argument_list COMMA named_argument
703 ArrayList args = (ArrayList) $1;
708 | named_argument_list COMMA expression
710 Report.Error (1016, ((Expression) $3).Location, "Named attribute argument expected");
716 : IDENTIFIER ASSIGN expression
718 // FIXME: keep location
719 $$ = new DictionaryEntry (
720 ((LocatedToken) $1).Value,
721 new Argument ((Expression) $3, Argument.AType.Expression));
727 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
730 opt_class_member_declarations
732 | class_member_declarations
735 class_member_declarations
736 : class_member_declaration
737 | class_member_declarations
738 class_member_declaration
741 class_member_declaration
742 : constant_declaration // done
743 | field_declaration // done
744 | method_declaration // done
745 | property_declaration // done
746 | event_declaration // done
747 | indexer_declaration // done
748 | operator_declaration // done
749 | constructor_declaration // done
750 | destructor_declaration // done
760 MemberName name = MakeName ((MemberName) $5);
761 current_class = new Struct (
762 current_namespace, current_class, name, (int) $2,
766 current_container = current_container.AddPartial (current_class);
768 current_container.AddClassOrStruct (current_class);
769 current_container = current_class;
775 current_class.Bases = (ArrayList) $7;
777 if (RootContext.Documentation != null)
778 current_container.DocComment = Lexer.consume_doc_comment ();
782 if (RootContext.Documentation != null)
783 Lexer.doc_state = XmlCommentState.Allowed;
787 $$ = pop_current_class ();
789 | opt_attributes opt_modifiers opt_partial STRUCT error {
790 CheckIdentifierToken (yyToken, GetLocation ($5));
797 if (RootContext.Documentation != null)
798 Lexer.doc_state = XmlCommentState.Allowed;
800 opt_struct_member_declarations CLOSE_BRACE
803 opt_struct_member_declarations
805 | struct_member_declarations
808 struct_member_declarations
809 : struct_member_declaration
810 | struct_member_declarations struct_member_declaration
813 struct_member_declaration
814 : constant_declaration
817 | property_declaration
819 | indexer_declaration
820 | operator_declaration
821 | constructor_declaration
825 * This is only included so we can flag error 575:
826 * destructors only allowed on class types
828 | destructor_declaration
839 int modflags = (int) $2;
840 foreach (VariableDeclaration constant in (ArrayList) $5){
841 Location l = constant.Location;
842 if ((modflags & Modifiers.STATIC) != 0) {
843 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
847 Const c = new Const (
848 current_class, (Expression) $4, (string) constant.identifier,
849 (Expression) constant.expression_or_array_initializer, modflags,
852 if (RootContext.Documentation != null) {
853 c.DocComment = Lexer.consume_doc_comment ();
854 Lexer.doc_state = XmlCommentState.Allowed;
856 current_container.AddConstant (c);
862 : constant_declarator
864 ArrayList constants = new ArrayList (4);
869 | constant_declarators COMMA constant_declarator
872 ArrayList constants = (ArrayList) $1;
879 : IDENTIFIER ASSIGN constant_expression
881 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
885 // A const field requires a value to be provided
886 Report.Error (145, ((LocatedToken) $1).Location, "A const field requires a value to be provided");
898 Expression type = (Expression) $3;
901 current_array_type = null;
903 foreach (VariableDeclaration var in (ArrayList) $4){
904 Field field = new Field (current_class, type, mod, var.identifier,
905 (Attributes) $1, var.Location);
907 field.Initializer = var.expression_or_array_initializer;
909 if (RootContext.Documentation != null) {
910 field.DocComment = Lexer.consume_doc_comment ();
911 Lexer.doc_state = XmlCommentState.Allowed;
913 current_container.AddField (field);
914 $$ = field; // FIXME: might be better if it points to the top item
921 fixed_variable_declarators
924 Expression type = (Expression) $4;
927 current_array_type = null;
929 foreach (VariableDeclaration var in (ArrayList) $5) {
930 FixedField field = new FixedField (current_class, type, mod, var.identifier,
931 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
933 if (RootContext.Documentation != null) {
934 field.DocComment = Lexer.consume_doc_comment ();
935 Lexer.doc_state = XmlCommentState.Allowed;
937 current_container.AddField (field);
938 $$ = field; // FIXME: might be better if it points to the top item
946 current_array_type = null;
947 Report.Error (670, (Location) $3, "Fields cannot have void type");
951 fixed_variable_declarators
952 : fixed_variable_declarator
954 ArrayList decl = new ArrayList (2);
958 | fixed_variable_declarators COMMA fixed_variable_declarator
960 ArrayList decls = (ArrayList) $1;
966 fixed_variable_declarator
967 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
969 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
971 | IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
973 Report.Error (443, lexer.Location, "Value or constant expected");
974 $$ = new VariableDeclaration ((LocatedToken) $1, null);
979 : variable_declarator
981 ArrayList decl = new ArrayList (4);
986 | variable_declarators COMMA variable_declarator
988 ArrayList decls = (ArrayList) $1;
995 : IDENTIFIER ASSIGN variable_initializer
997 $$ = new VariableDeclaration ((LocatedToken) $1, $3);
1001 $$ = new VariableDeclaration ((LocatedToken) $1, null);
1003 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
1005 Report.Error (650, ((LocatedToken) $1).Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
1006 "To declare a fixed size buffer field, use the fixed keyword before the field type");
1011 variable_initializer
1020 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
1022 $$ = new StackAlloc ((Expression) $2, (Expression) $4, (Location) $1);
1026 $$ = new ArglistAccess ((Location) $1);
1030 Report.Error (1575, (Location) $1, "A stackalloc expression requires [] after type");
1037 iterator_container = (IIteratorContainer) $1;
1038 if (RootContext.Documentation != null)
1039 Lexer.doc_state = XmlCommentState.NotAllowed;
1043 Method method = (Method) $1;
1044 method.Block = (ToplevelBlock) $3;
1045 current_container.AddMethod (method);
1047 current_local_parameters = null;
1048 iterator_container = null;
1050 if (RootContext.Documentation != null)
1051 Lexer.doc_state = XmlCommentState.Allowed;
1064 Report.Error (1585, lexer.Location,
1065 "Member modifier `{0}' must precede the member type and name",
1066 Modifiers.Name (i));
1077 type namespace_or_type_name
1078 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1080 MemberName name = (MemberName) $4;
1082 Method method = new Method (current_class, (Expression) $3, (int) $2,
1083 false, name, (Parameters) $6, (Attributes) $1);
1085 current_local_parameters = (Parameters) $6;
1087 if (RootContext.Documentation != null)
1088 method.DocComment = Lexer.consume_doc_comment ();
1094 VOID namespace_or_type_name
1095 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1097 MemberName name = (MemberName) $4;
1099 Method method = new Method (current_class, TypeManager.system_void_expr,
1100 (int) $2, false, name, (Parameters) $6,
1103 current_local_parameters = (Parameters) $6;
1105 if (RootContext.Documentation != null)
1106 method.DocComment = Lexer.consume_doc_comment ();
1113 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1115 MemberName name = (MemberName) $5;
1116 Report.Error (1585, name.Location,
1117 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1119 Method method = new Method (current_class, TypeManager.system_void_expr,
1120 0, false, name, (Parameters) $7, (Attributes) $1);
1122 current_local_parameters = (Parameters) $7;
1124 if (RootContext.Documentation != null)
1125 method.DocComment = Lexer.consume_doc_comment ();
1133 | SEMICOLON { $$ = null; }
1136 opt_formal_parameter_list
1137 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1138 | formal_parameter_list
1141 formal_parameter_list
1144 ArrayList pars_list = (ArrayList) $1;
1146 Parameter [] pars = new Parameter [pars_list.Count];
1147 pars_list.CopyTo (pars);
1149 $$ = new Parameters (pars);
1151 | fixed_parameters COMMA parameter_array
1153 ArrayList pars_list = (ArrayList) $1;
1156 Parameter [] pars = new Parameter [pars_list.Count];
1157 pars_list.CopyTo (pars);
1159 $$ = new Parameters (pars);
1161 | fixed_parameters COMMA ARGLIST
1163 ArrayList pars_list = (ArrayList) $1;
1164 //pars_list.Add (new ArglistParameter (GetLocation ($3)));
1166 Parameter [] pars = new Parameter [pars_list.Count];
1167 pars_list.CopyTo (pars);
1169 $$ = new Parameters (pars, true);
1171 | parameter_array COMMA error
1174 Report.Error (231, ((Parameter) $1).Location, "A params parameter must be the last parameter in a formal parameter list");
1177 | ARGLIST COMMA error
1179 Report.Error (257, (Location) $1, "An __arglist parameter must be the last parameter in a formal parameter list");
1184 $$ = new Parameters (new Parameter[] { (Parameter) $1 } );
1188 $$ = new Parameters (new Parameter[0], true);
1195 ArrayList pars = new ArrayList (4);
1200 | fixed_parameters COMMA fixed_parameter
1202 ArrayList pars = (ArrayList) $1;
1211 opt_parameter_modifier
1215 LocatedToken lt = (LocatedToken) $4;
1216 $$ = new Parameter ((Expression) $3, lt.Value, (Parameter.Modifier) $2, (Attributes) $1, lt.Location);
1219 opt_parameter_modifier
1221 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1223 LocatedToken lt = (LocatedToken) $4;
1224 Report.Error (1552, lt.Location, "Array type specifier, [], must appear before parameter name");
1228 opt_parameter_modifier
1231 Report.Error (1001, GetLocation ($3), "Identifier expected");
1235 opt_parameter_modifier
1238 CheckIdentifierToken (yyToken, GetLocation ($4));
1242 opt_parameter_modifier
1248 LocatedToken lt = (LocatedToken) $4;
1249 Report.Error (241, lt.Location, "Default parameter specifiers are not permitted");
1254 opt_parameter_modifier
1255 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1256 | parameter_modifier
1260 : REF { $$ = Parameter.Modifier.REF; }
1261 | OUT { $$ = Parameter.Modifier.OUT; }
1265 : opt_attributes PARAMS type IDENTIFIER
1267 LocatedToken lt = (LocatedToken) $4;
1268 $$ = new ParamsParameter ((Expression) $3, lt.Value, (Attributes) $1, lt.Location);
1269 note ("type must be a single-dimension array type");
1271 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1273 Report.Error (1611, (Location) $2, "The params parameter cannot be declared as ref or out");
1276 | opt_attributes PARAMS type error {
1277 CheckIdentifierToken (yyToken, GetLocation ($4));
1282 property_declaration
1286 namespace_or_type_name
1288 if (RootContext.Documentation != null)
1289 tmpComment = Lexer.consume_doc_comment ();
1293 implicit_value_parameter_type = (Expression) $3;
1295 lexer.PropertyParsing = true;
1297 accessor_declarations
1299 lexer.PropertyParsing = false;
1300 has_get = has_set = false;
1308 Pair pair = (Pair) $8;
1309 Accessor get_block = (Accessor) pair.First;
1310 Accessor set_block = (Accessor) pair.Second;
1312 MemberName name = (MemberName) $4;
1314 prop = new Property (current_class, (Expression) $3, (int) $2,
1315 false, name, (Attributes) $1, get_block, set_block);
1317 current_container.AddProperty (prop);
1318 implicit_value_parameter_type = null;
1320 if (RootContext.Documentation != null)
1321 prop.DocComment = ConsumeStoredComment ();
1326 accessor_declarations
1327 : get_accessor_declaration
1329 $$ = new Pair ($1, null);
1331 | get_accessor_declaration accessor_declarations
1333 Pair pair = (Pair) $2;
1337 | set_accessor_declaration
1339 $$ = new Pair (null, $1);
1341 | set_accessor_declaration accessor_declarations
1343 Pair pair = (Pair) $2;
1349 Report.Error (1014, GetLocation ($1), "A get or set accessor expected");
1354 get_accessor_declaration
1355 : opt_attributes opt_modifiers GET
1357 // If this is not the case, then current_local_parameters has already
1358 // been set in indexer_declaration
1359 if (parsing_indexer == false)
1360 current_local_parameters = null;
1362 current_local_parameters = indexer_parameters;
1363 lexer.PropertyParsing = false;
1365 iterator_container = SimpleIteratorContainer.GetSimple ();
1370 Report.Error (1007, (Location) $3, "Property accessor already defined");
1373 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1375 current_local_parameters = null;
1376 lexer.PropertyParsing = true;
1378 if (SimpleIteratorContainer.Simple.Yields)
1379 accessor.SetYields ();
1381 iterator_container = null;
1383 if (RootContext.Documentation != null)
1384 if (Lexer.doc_state == XmlCommentState.Error)
1385 Lexer.doc_state = XmlCommentState.NotAllowed;
1391 set_accessor_declaration
1392 : opt_attributes opt_modifiers SET
1395 Parameter implicit_value_parameter = new Parameter (
1396 implicit_value_parameter_type, "value",
1397 Parameter.Modifier.NONE, null, (Location) $3);
1399 if (parsing_indexer == false) {
1400 args = new Parameter [1];
1401 args [0] = implicit_value_parameter;
1402 current_local_parameters = new Parameters (args);
1404 Parameter [] fpars = indexer_parameters.FixedParameters;
1407 int count = fpars.Length;
1409 args = new Parameter [count + 1];
1410 fpars.CopyTo (args, 0);
1411 args [count] = implicit_value_parameter;
1414 current_local_parameters = new Parameters (
1418 lexer.PropertyParsing = false;
1420 iterator_container = SimpleIteratorContainer.GetSimple ();
1425 Report.Error (1007, ((LocatedToken) $3).Location, "Property accessor already defined");
1428 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, (Location) $3);
1430 current_local_parameters = null;
1431 lexer.PropertyParsing = true;
1433 if (SimpleIteratorContainer.Simple.Yields)
1434 accessor.SetYields ();
1436 iterator_container = null;
1438 if (RootContext.Documentation != null
1439 && Lexer.doc_state == XmlCommentState.Error)
1440 Lexer.doc_state = XmlCommentState.NotAllowed;
1448 | SEMICOLON { $$ = null; }
1451 interface_declaration
1455 INTERFACE member_name
1457 MemberName name = MakeName ((MemberName) $5);
1459 current_class = new Interface (
1460 current_namespace, current_class, name, (int) $2,
1464 current_container = current_container.AddPartial (current_class);
1466 current_container.AddInterface (current_class);
1467 current_container = current_class;
1472 current_class.Bases = (ArrayList) $7;
1474 if (RootContext.Documentation != null) {
1475 current_container.DocComment = Lexer.consume_doc_comment ();
1476 Lexer.doc_state = XmlCommentState.Allowed;
1481 if (RootContext.Documentation != null)
1482 Lexer.doc_state = XmlCommentState.Allowed;
1486 $$ = pop_current_class ();
1488 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1489 CheckIdentifierToken (yyToken, GetLocation ($5));
1495 opt_interface_member_declarations
1499 opt_interface_member_declarations
1501 | interface_member_declarations
1504 interface_member_declarations
1505 : interface_member_declaration
1506 | interface_member_declarations interface_member_declaration
1509 interface_member_declaration
1510 : interface_method_declaration
1515 Method m = (Method) $1;
1517 if (m.IsExplicitImpl)
1518 Report.Error (541, m.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1519 m.GetSignatureForError ());
1521 current_container.AddMethod (m);
1523 if (RootContext.Documentation != null)
1524 Lexer.doc_state = XmlCommentState.Allowed;
1526 | interface_property_declaration
1531 Property p = (Property) $1;
1533 if (p.IsExplicitImpl)
1534 Report.Error (541, p.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1535 p.GetSignatureForError ());
1537 current_container.AddProperty (p);
1539 if (RootContext.Documentation != null)
1540 Lexer.doc_state = XmlCommentState.Allowed;
1542 | interface_event_declaration
1545 Event e = (Event) $1;
1547 if (e.IsExplicitImpl)
1548 Report.Error (541, e.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1549 e.GetSignatureForError ());
1551 current_container.AddEvent (e);
1554 if (RootContext.Documentation != null)
1555 Lexer.doc_state = XmlCommentState.Allowed;
1557 | interface_indexer_declaration
1562 Indexer i = (Indexer) $1;
1564 if (i.IsExplicitImpl)
1565 Report.Error (541, i.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1566 i.GetSignatureForError ());
1568 current_container.AddIndexer (i);
1570 if (RootContext.Documentation != null)
1571 Lexer.doc_state = XmlCommentState.Allowed;
1573 | delegate_declaration
1576 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1577 ((MemberCore)$1).GetSignatureForError ());
1583 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1584 ((MemberCore)$1).GetSignatureForError ());
1587 | struct_declaration
1590 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1591 ((MemberCore)$1).GetSignatureForError ());
1597 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1598 ((MemberCore)$1).GetSignatureForError ());
1601 | interface_declaration
1604 Report.Error (524, GetLocation ($1), "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1605 ((MemberCore)$1).GetSignatureForError ());
1608 | constant_declaration
1610 Report.Error (525, GetLocation ($1), "Interfaces cannot contain fields or constants");
1618 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, GetLocation ($1));
1623 interface_method_declaration
1624 : opt_attributes opt_new type namespace_or_type_name
1625 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1628 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1629 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1630 if (RootContext.Documentation != null)
1631 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1633 | opt_attributes opt_new type namespace_or_type_name
1634 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1635 OPEN_BRACE opt_statement_list CLOSE_BRACE
1637 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1638 (MemberName) $4, (Parameters) $6, (Attributes) $1);
1640 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1641 ((Method)$$).GetSignatureForError ());
1645 interface_property_declaration
1650 { lexer.PropertyParsing = true; }
1651 accessor_declarations
1653 has_get = has_set = false;
1654 lexer.PropertyParsing = false;
1658 LocatedToken lt = (LocatedToken) $4;
1659 MemberName name = new MemberName (lt.Value, lt.Location);
1661 if ($3 == TypeManager.system_void_expr) {
1662 Report.Error (547, lt.Location, "`{0}': property or indexer cannot have void type", lt.Value);
1668 p = new Property (current_class, (Expression) $3, (int) $2, true,
1669 name, (Attributes) $1,
1672 Report.Error (548, p.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1676 Pair pair = (Pair) $7;
1677 p = new Property (current_class, (Expression) $3, (int) $2, true,
1678 name, (Attributes) $1,
1679 (Accessor)pair.First, (Accessor)pair.Second);
1681 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1682 Report.Error (531, p.Location, "`{0}.get': interface members cannot have a definition", p.GetSignatureForError ());
1687 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1688 Report.Error (531, p.Location, "`{0}.set': interface members cannot have a definition", p.GetSignatureForError ());
1693 if (RootContext.Documentation != null)
1694 p.DocComment = Lexer.consume_doc_comment ();
1701 CheckIdentifierToken (yyToken, GetLocation ($4));
1707 interface_event_declaration
1708 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1710 LocatedToken lt = (LocatedToken) $5;
1711 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1712 new MemberName (lt.Value, lt.Location),
1714 if (RootContext.Documentation != null)
1715 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1717 | opt_attributes opt_new EVENT type error {
1718 CheckIdentifierToken (yyToken, GetLocation ($5));
1721 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1722 LocatedToken lt = (LocatedToken) $5;
1723 Report.Error (68, lt.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, lt.Value);
1726 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1728 lexer.EventParsing = true;
1730 event_accessor_declarations
1732 lexer.EventParsing = false;
1735 Report.Error (69, (Location) $3, "Event in interface cannot have add or remove accessors");
1740 interface_indexer_declaration
1741 : opt_attributes opt_new type THIS
1742 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1744 { lexer.PropertyParsing = true; }
1745 accessor_declarations
1747 has_get = has_set = false;
1748 lexer.PropertyParsing = false;
1754 i = new Indexer (current_class, (Expression) $3,
1755 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1756 (int) $2, true, (Parameters) $6, (Attributes) $1,
1759 Report.Error (548, i.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1763 Pair pair = (Pair) $10;
1764 i = new Indexer (current_class, (Expression) $3,
1765 new MemberName (TypeContainer.DefaultIndexerName, (Location) $4),
1766 (int) $2, true, (Parameters) $6, (Attributes) $1,
1767 (Accessor)pair.First, (Accessor)pair.Second);
1769 if (pair.First != null && ((Accessor)(pair.First)).Block != null) {
1770 Report.Error (531, i.Location, "`{0}.get': interface members cannot have a definition", i.GetSignatureForError ());
1775 if (pair.Second != null && ((Accessor)(pair.Second)).Block != null) {
1776 Report.Error (531, i.Location, "`{0}.set': interface members cannot have a definition", i.GetSignatureForError ());
1781 if (RootContext.Documentation != null)
1782 i.DocComment = ConsumeStoredComment ();
1788 operator_declaration
1789 : opt_attributes opt_modifiers operator_declarator
1791 iterator_container = SimpleIteratorContainer.GetSimple ();
1798 OperatorDeclaration decl = (OperatorDeclaration) $3;
1800 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1802 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1803 if (decl.arg2type != null)
1804 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1806 Operator op = new Operator (
1807 current_class, decl.optype, decl.ret_type, (int) $2,
1808 new Parameters (param_list),
1809 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1811 if (RootContext.Documentation != null) {
1812 op.DocComment = tmpComment;
1813 Lexer.doc_state = XmlCommentState.Allowed;
1816 if (SimpleIteratorContainer.Simple.Yields)
1819 // Note again, checking is done in semantic analysis
1820 current_container.AddOperator (op);
1822 current_local_parameters = null;
1823 iterator_container = null;
1829 | SEMICOLON { $$ = null; }
1832 : type OPERATOR overloadable_operator
1833 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1835 LocatedToken lt = (LocatedToken) $6;
1836 Operator.OpType op = (Operator.OpType) $3;
1837 CheckUnaryOperator (op, lt.Location);
1839 if (op == Operator.OpType.Addition)
1840 op = Operator.OpType.UnaryPlus;
1842 if (op == Operator.OpType.Subtraction)
1843 op = Operator.OpType.UnaryNegation;
1845 Parameter [] pars = new Parameter [1];
1846 Expression type = (Expression) $5;
1848 pars [0] = new Parameter (type, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1850 current_local_parameters = new Parameters (pars);
1852 if (RootContext.Documentation != null) {
1853 tmpComment = Lexer.consume_doc_comment ();
1854 Lexer.doc_state = XmlCommentState.NotAllowed;
1857 $$ = new OperatorDeclaration (op, (Expression) $1, type, lt.Value,
1858 null, null, (Location) $2);
1860 | type OPERATOR overloadable_operator
1862 type IDENTIFIER COMMA
1866 LocatedToken ltParam1 = (LocatedToken) $6;
1867 LocatedToken ltParam2 = (LocatedToken) $9;
1868 CheckBinaryOperator ((Operator.OpType) $3, (Location) $2);
1870 Parameter [] pars = new Parameter [2];
1872 Expression typeL = (Expression) $5;
1873 Expression typeR = (Expression) $8;
1875 pars [0] = new Parameter (typeL, ltParam1.Value, Parameter.Modifier.NONE, null, ltParam1.Location);
1876 pars [1] = new Parameter (typeR, ltParam2.Value, Parameter.Modifier.NONE, null, ltParam2.Location);
1878 current_local_parameters = new Parameters (pars);
1880 if (RootContext.Documentation != null) {
1881 tmpComment = Lexer.consume_doc_comment ();
1882 Lexer.doc_state = XmlCommentState.NotAllowed;
1885 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1886 typeL, ltParam1.Value,
1887 typeR, ltParam2.Value, (Location) $2);
1889 | conversion_operator_declarator
1890 | type OPERATOR overloadable_operator
1892 type IDENTIFIER COMMA
1893 type IDENTIFIER COMMA
1897 Report.Error (1534, (Location) $2, "Overloaded binary operator `{0}' takes two parameters",
1898 Operator.GetName ((Operator.OpType) $3));
1901 | type OPERATOR overloadable_operator
1902 OPEN_PARENS CLOSE_PARENS
1904 Report.Error (1535, (Location) $2, "Overloaded unary operator `{0}' takes one parameter",
1905 Operator.GetName ((Operator.OpType) $3));
1910 overloadable_operator
1912 : BANG { $$ = Operator.OpType.LogicalNot; }
1913 | TILDE { $$ = Operator.OpType.OnesComplement; }
1914 | OP_INC { $$ = Operator.OpType.Increment; }
1915 | OP_DEC { $$ = Operator.OpType.Decrement; }
1916 | TRUE { $$ = Operator.OpType.True; }
1917 | FALSE { $$ = Operator.OpType.False; }
1918 // Unary and binary:
1919 | PLUS { $$ = Operator.OpType.Addition; }
1920 | MINUS { $$ = Operator.OpType.Subtraction; }
1922 | STAR { $$ = Operator.OpType.Multiply; }
1923 | DIV { $$ = Operator.OpType.Division; }
1924 | PERCENT { $$ = Operator.OpType.Modulus; }
1925 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1926 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1927 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1928 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1929 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1930 | OP_EQ { $$ = Operator.OpType.Equality; }
1931 | OP_NE { $$ = Operator.OpType.Inequality; }
1932 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1933 | OP_LT { $$ = Operator.OpType.LessThan; }
1934 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1935 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1938 conversion_operator_declarator
1939 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1941 LocatedToken lt = (LocatedToken) $6;
1942 Parameter [] pars = new Parameter [1];
1944 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1946 current_local_parameters = new Parameters (pars);
1948 if (RootContext.Documentation != null) {
1949 tmpComment = Lexer.consume_doc_comment ();
1950 Lexer.doc_state = XmlCommentState.NotAllowed;
1953 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, lt.Value,
1954 null, null, (Location) $2);
1956 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1958 LocatedToken lt = (LocatedToken) $6;
1959 Parameter [] pars = new Parameter [1];
1961 pars [0] = new Parameter ((Expression) $5, lt.Value, Parameter.Modifier.NONE, null, lt.Location);
1963 current_local_parameters = new Parameters (pars);
1965 if (RootContext.Documentation != null) {
1966 tmpComment = Lexer.consume_doc_comment ();
1967 Lexer.doc_state = XmlCommentState.NotAllowed;
1970 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, lt.Value,
1971 null, null, (Location) $2);
1975 syntax_error ((Location) $1, "'operator' expected");
1979 syntax_error ((Location) $1, "'operator' expected");
1983 constructor_declaration
1986 constructor_declarator
1989 Constructor c = (Constructor) $3;
1990 c.Block = (ToplevelBlock) $4;
1991 c.OptAttributes = (Attributes) $1;
1992 c.ModFlags = (int) $2;
1994 if (RootContext.Documentation != null)
1995 c.DocComment = ConsumeStoredComment ();
1997 if (c.Name == current_container.Basename){
1998 if ((c.ModFlags & Modifiers.STATIC) != 0){
1999 if ((c.ModFlags & Modifiers.Accessibility) != 0){
2000 Report.Error (515, c.Location,
2001 "`{0}': access modifiers are not allowed on static constructors",
2002 c.GetSignatureForError ());
2005 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2007 if (c.Initializer != null){
2008 Report.Error (514, c.Location,
2009 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
2010 c.GetSignatureForError ());
2013 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
2016 // We let another layer check the validity of the constructor.
2017 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
2020 current_container.AddConstructor (c);
2022 current_local_parameters = null;
2023 if (RootContext.Documentation != null)
2024 Lexer.doc_state = XmlCommentState.Allowed;
2028 constructor_declarator
2031 if (RootContext.Documentation != null) {
2032 tmpComment = Lexer.consume_doc_comment ();
2033 Lexer.doc_state = XmlCommentState.Allowed;
2036 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2038 current_local_parameters = (Parameters) $4;
2040 opt_constructor_initializer
2042 LocatedToken lt = (LocatedToken) $1;
2043 $$ = new Constructor (current_class, lt.Value, 0, (Parameters) $4,
2044 (ConstructorInitializer) $7, lt.Location);
2050 | SEMICOLON { $$ = null; }
2053 opt_constructor_initializer
2054 : /* empty */ { $$ = null; }
2055 | constructor_initializer
2058 constructor_initializer
2059 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2061 $$ = new ConstructorBaseInitializer ((ArrayList) $4, (Location) $2);
2063 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2065 $$ = new ConstructorThisInitializer ((ArrayList) $4, (Location) $2);
2068 Report.Error (1018, (Location) $1, "Keyword this or base expected");
2074 : /* EMPTY */ { $$ = 0; }
2075 | UNSAFE { $$ = Modifiers.UNSAFE; }
2076 | EXTERN { $$ = Modifiers.EXTERN; }
2079 destructor_declaration
2080 : opt_attributes opt_finalizer TILDE
2082 if (RootContext.Documentation != null) {
2083 tmpComment = Lexer.consume_doc_comment ();
2084 Lexer.doc_state = XmlCommentState.NotAllowed;
2087 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2089 LocatedToken lt = (LocatedToken) $5;
2090 if (lt.Value != current_container.Basename){
2091 Report.Error (574, lt.Location, "Name of destructor must match name of class");
2092 } else if (current_container.Kind != Kind.Class){
2093 Report.Error (575, lt.Location, "Only class types can contain destructor");
2095 Location l = lt.Location;
2098 if (!RootContext.StdLib && current_container.Name == "System.Object")
2099 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2101 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2103 Method d = new Destructor (
2104 current_class, TypeManager.system_void_expr, m, "Finalize",
2105 Parameters.EmptyReadOnlyParameters, (Attributes) $1, l);
2106 if (RootContext.Documentation != null)
2107 d.DocComment = ConsumeStoredComment ();
2109 d.Block = (ToplevelBlock) $8;
2110 current_container.AddMethod (d);
2118 EVENT type variable_declarators SEMICOLON
2120 current_array_type = null;
2121 foreach (VariableDeclaration var in (ArrayList) $5) {
2123 MemberName name = new MemberName (var.identifier,
2126 EventField e = new EventField (
2127 current_class, (Expression) $4, (int) $2, false, name,
2130 e.Initializer = var.expression_or_array_initializer;
2132 current_container.AddEvent (e);
2134 if (RootContext.Documentation != null) {
2135 e.DocComment = Lexer.consume_doc_comment ();
2136 Lexer.doc_state = XmlCommentState.Allowed;
2142 EVENT type namespace_or_type_name
2145 implicit_value_parameter_type = (Expression) $4;
2146 lexer.EventParsing = true;
2148 event_accessor_declarations
2150 lexer.EventParsing = false;
2154 MemberName name = (MemberName) $5;
2157 Report.Error (65, (Location) $3, "`{0}.{1}': event property must have both add and remove accessors",
2158 current_container.Name, name.ToString ());
2161 Pair pair = (Pair) $8;
2162 if (pair.First == null || pair.Second == null)
2163 // CS0073 is already reported, so no CS0065 here.
2166 Event e = new EventProperty (
2167 current_class, (Expression) $4, (int) $2, false, name,
2168 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second);
2169 if (RootContext.Documentation != null) {
2170 e.DocComment = Lexer.consume_doc_comment ();
2171 Lexer.doc_state = XmlCommentState.Allowed;
2174 current_container.AddEvent (e);
2175 implicit_value_parameter_type = null;
2179 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2180 MemberName mn = (MemberName) $5;
2182 if (mn.Left != null)
2183 Report.Error (71, mn.Location, "An explicit interface implementation of an event must use property syntax");
2185 Report.Error (71, mn.Location, "Event declaration should use property syntax");
2187 if (RootContext.Documentation != null)
2188 Lexer.doc_state = XmlCommentState.Allowed;
2192 event_accessor_declarations
2193 : add_accessor_declaration remove_accessor_declaration
2195 $$ = new Pair ($1, $2);
2197 | remove_accessor_declaration add_accessor_declaration
2199 $$ = new Pair ($2, $1);
2201 | add_accessor_declaration { $$ = null; }
2202 | remove_accessor_declaration { $$ = null; }
2205 Report.Error (1055, GetLocation ($1), "An add or remove accessor expected");
2211 add_accessor_declaration
2212 : opt_attributes ADD
2214 Parameter [] args = new Parameter [1];
2215 Parameter implicit_value_parameter = new Parameter (
2216 implicit_value_parameter_type, "value",
2217 Parameter.Modifier.NONE, null, (Location) $2);
2219 args [0] = implicit_value_parameter;
2221 current_local_parameters = new Parameters (args);
2222 lexer.EventParsing = false;
2226 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2227 lexer.EventParsing = true;
2229 | opt_attributes ADD error {
2230 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2233 | opt_attributes modifiers ADD {
2234 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2239 remove_accessor_declaration
2240 : opt_attributes REMOVE
2242 Parameter [] args = new Parameter [1];
2243 Parameter implicit_value_parameter = new Parameter (
2244 implicit_value_parameter_type, "value",
2245 Parameter.Modifier.NONE, null, (Location) $2);
2247 args [0] = implicit_value_parameter;
2249 current_local_parameters = new Parameters (args);
2250 lexer.EventParsing = false;
2254 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, (Location) $2);
2255 lexer.EventParsing = true;
2257 | opt_attributes REMOVE error {
2258 Report.Error (73, (Location) $2, "An add or remove accessor must have a body");
2261 | opt_attributes modifiers REMOVE {
2262 Report.Error (1609, (Location) $3, "Modifiers cannot be placed on event accessor declarations");
2268 : opt_attributes opt_modifiers indexer_declarator
2271 IndexerDeclaration decl = (IndexerDeclaration) $3;
2273 implicit_value_parameter_type = decl.type;
2275 lexer.PropertyParsing = true;
2276 parsing_indexer = true;
2278 indexer_parameters = decl.param_list;
2280 accessor_declarations
2282 lexer.PropertyParsing = false;
2283 has_get = has_set = false;
2284 parsing_indexer = false;
2291 // The signature is computed from the signature of the indexer. Look
2292 // at section 3.6 on the spec
2294 IndexerDeclaration decl = (IndexerDeclaration) $3;
2295 Location loc = decl.location;
2296 Pair pair = (Pair) $6;
2297 Accessor get_block = (Accessor) pair.First;
2298 Accessor set_block = (Accessor) pair.Second;
2301 if (decl.interface_type != null)
2302 name = new MemberName (decl.interface_type, TypeContainer.DefaultIndexerName, loc);
2304 name = new MemberName (TypeContainer.DefaultIndexerName, loc);
2306 indexer = new Indexer (current_class, decl.type, name,
2307 (int) $2, false, decl.param_list, (Attributes) $1,
2308 get_block, set_block);
2310 if (RootContext.Documentation != null)
2311 indexer.DocComment = ConsumeStoredComment ();
2313 current_container.AddIndexer (indexer);
2315 current_local_parameters = null;
2316 implicit_value_parameter_type = null;
2317 indexer_parameters = null;
2322 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2324 Parameters pars = (Parameters) $4;
2325 if (pars.HasArglist) {
2326 // "__arglist is not valid in this context"
2327 Report.Error (1669, (Location) $2, "__arglist is not valid in this context");
2328 } else if (pars.Empty){
2329 Report.Error (1551, (Location) $2, "Indexers must have at least one parameter");
2331 if (RootContext.Documentation != null) {
2332 tmpComment = Lexer.consume_doc_comment ();
2333 Lexer.doc_state = XmlCommentState.Allowed;
2336 $$ = new IndexerDeclaration ((Expression) $1, null, pars, (Location) $2);
2338 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2340 Parameters pars = (Parameters) $6;
2342 if (pars.HasArglist) {
2343 // "__arglist is not valid in this context"
2344 Report.Error (1669, (Location) $4, "__arglist is not valid in this context");
2345 } else if (pars.Empty){
2346 Report.Error (1551, (Location) $4, "Indexers must have at least one parameter");
2349 MemberName name = (MemberName) $2;
2350 $$ = new IndexerDeclaration ((Expression) $1, name, pars, (Location) $4);
2352 if (RootContext.Documentation != null) {
2353 tmpComment = Lexer.consume_doc_comment ();
2354 Lexer.doc_state = XmlCommentState.Allowed;
2365 if (RootContext.Documentation != null)
2366 enumTypeComment = Lexer.consume_doc_comment ();
2371 LocatedToken lt = (LocatedToken) $5;
2372 Location enum_location = lt.Location;
2375 Report.Error (267, lt.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2376 break; // assumes that the parser put us in a switch
2379 MemberName name = MakeName (new MemberName (lt.Value, enum_location));
2380 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2381 name, (Attributes) $1);
2383 if (RootContext.Documentation != null)
2384 e.DocComment = enumTypeComment;
2387 EnumMember em = null;
2388 foreach (VariableDeclaration ev in (ArrayList) $8) {
2389 em = new EnumMember (e, em, (Expression) ev.expression_or_array_initializer,
2390 new MemberName (ev.identifier, ev.Location), ev.OptAttributes);
2392 // if (RootContext.Documentation != null)
2393 em.DocComment = ev.DocComment;
2395 e.AddEnumMember (em);
2398 current_container.AddEnum (e);
2399 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2406 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2407 | COLON type { $$ = $2; }
2413 if (RootContext.Documentation != null)
2414 Lexer.doc_state = XmlCommentState.Allowed;
2416 opt_enum_member_declarations
2418 // here will be evaluated after CLOSE_BLACE is consumed.
2419 if (RootContext.Documentation != null)
2420 Lexer.doc_state = XmlCommentState.Allowed;
2428 opt_enum_member_declarations
2429 : /* empty */ { $$ = new ArrayList (4); }
2430 | enum_member_declarations opt_comma { $$ = $1; }
2433 enum_member_declarations
2434 : enum_member_declaration
2436 ArrayList l = new ArrayList (4);
2441 | enum_member_declarations COMMA enum_member_declaration
2443 ArrayList l = (ArrayList) $1;
2451 enum_member_declaration
2452 : opt_attributes IDENTIFIER
2454 VariableDeclaration vd = new VariableDeclaration (
2455 (LocatedToken) $2, null, (Attributes) $1);
2457 if (RootContext.Documentation != null) {
2458 vd.DocComment = Lexer.consume_doc_comment ();
2459 Lexer.doc_state = XmlCommentState.Allowed;
2464 | opt_attributes IDENTIFIER
2466 if (RootContext.Documentation != null) {
2467 tmpComment = Lexer.consume_doc_comment ();
2468 Lexer.doc_state = XmlCommentState.NotAllowed;
2473 VariableDeclaration vd = new VariableDeclaration (
2474 (LocatedToken) $2, $5, (Attributes) $1);
2476 if (RootContext.Documentation != null)
2477 vd.DocComment = ConsumeStoredComment ();
2483 delegate_declaration
2486 DELEGATE type member_name
2487 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2490 MemberName name = MakeName ((MemberName) $5);
2491 Parameters p = (Parameters) $7;
2493 // TODO: wrong location
2494 Report.Error (1669, name.Location, "__arglist is not valid in this context");
2497 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2498 (int) $2, name, p, (Attributes) $1);
2500 if (RootContext.Documentation != null) {
2501 del.DocComment = Lexer.consume_doc_comment ();
2502 Lexer.doc_state = XmlCommentState.Allowed;
2505 current_container.AddDelegate (del);
2506 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2511 namespace_or_type_name
2513 | IDENTIFIER DOUBLE_COLON IDENTIFIER {
2514 LocatedToken lt1 = (LocatedToken) $1;
2515 LocatedToken lt2 = (LocatedToken) $3;
2516 $$ = new MemberName (lt1.Value, lt2.Value, lt2.Location);
2518 | namespace_or_type_name DOT IDENTIFIER {
2519 LocatedToken lt = (LocatedToken) $3;
2520 $$ = new MemberName ((MemberName) $1, lt.Value);
2526 LocatedToken lt = (LocatedToken) $1;
2527 $$ = new MemberName (lt.Value, lt.Location);
2532 * Before you think of adding a return_type, notice that we have been
2533 * using two rules in the places where it matters (one rule using type
2534 * and another identical one that uses VOID as the return type). This
2535 * gets rid of a shift/reduce couple
2538 : namespace_or_type_name
2540 MemberName name = (MemberName) $1;
2541 $$ = name.GetTypeExpression ();
2553 // Note that here only unmanaged types are allowed but we
2554 // can't perform checks during this phase - we do it during
2555 // semantic analysis.
2557 $$ = new ComposedCast ((Expression) $1, "*", Lexer.Location);
2561 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
2567 | non_expression_type rank_specifier
2569 Location loc = GetLocation ($1);
2571 loc = lexer.Location;
2572 $$ = new ComposedCast ((Expression) $1, (string) $2, loc);
2574 | non_expression_type STAR
2576 Location loc = GetLocation ($1);
2578 loc = lexer.Location;
2579 $$ = new ComposedCast ((Expression) $1, "*", loc);
2581 | expression rank_specifiers
2583 $$ = new ComposedCast ((Expression) $1, (string) $2);
2587 $$ = new ComposedCast ((Expression) $1, "*");
2591 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2592 // through this different path
2594 | multiplicative_expression STAR
2596 $$ = new ComposedCast ((Expression) $1, "*");
2603 ArrayList types = new ArrayList (4);
2608 | type_list COMMA type
2610 ArrayList types = (ArrayList) $1;
2618 * replaces all the productions for isolating the various
2619 * simple types, but we need this to reuse it easily in local_variable_type
2622 : OBJECT { $$ = TypeManager.system_object_expr; }
2623 | STRING { $$ = TypeManager.system_string_expr; }
2624 | BOOL { $$ = TypeManager.system_boolean_expr; }
2625 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2626 | FLOAT { $$ = TypeManager.system_single_expr; }
2627 | DOUBLE { $$ = TypeManager.system_double_expr; }
2632 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2633 | BYTE { $$ = TypeManager.system_byte_expr; }
2634 | SHORT { $$ = TypeManager.system_int16_expr; }
2635 | USHORT { $$ = TypeManager.system_uint16_expr; }
2636 | INT { $$ = TypeManager.system_int32_expr; }
2637 | UINT { $$ = TypeManager.system_uint32_expr; }
2638 | LONG { $$ = TypeManager.system_int64_expr; }
2639 | ULONG { $$ = TypeManager.system_uint64_expr; }
2640 | CHAR { $$ = TypeManager.system_char_expr; }
2641 | VOID { $$ = TypeManager.system_void_expr; }
2645 : type rank_specifiers
2647 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2);
2652 // Expressions, section 7.5
2662 MemberName mn = (MemberName) $1;
2663 $$ = mn.GetTypeExpression ();
2665 | IDENTIFIER DOUBLE_COLON IDENTIFIER
2667 LocatedToken lt1 = (LocatedToken) $1;
2668 LocatedToken lt2 = (LocatedToken) $3;
2669 $$ = new QualifiedAliasMember (lt1.Value, lt2.Value, lt2.Location);
2671 | parenthesized_expression
2673 | invocation_expression
2677 | post_increment_expression
2678 | post_decrement_expression
2682 | checked_expression
2683 | unchecked_expression
2684 | pointer_member_access
2685 | anonymous_method_expression
2692 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value, lexer.Location); }
2693 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value, lexer.Location); }
2694 | NULL { $$ = new NullLiteral (lexer.Location); }
2698 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value, lexer.Location); }
2699 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value, lexer.Location); }
2700 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value, lexer.Location); }
2705 object v = lexer.Value;
2708 $$ = new IntLiteral ((int) v, lexer.Location);
2709 } else if (v is uint)
2710 $$ = new UIntLiteral ((UInt32) v, lexer.Location);
2712 $$ = new LongLiteral ((Int64) v, lexer.Location);
2713 else if (v is ulong)
2714 $$ = new ULongLiteral ((UInt64) v, lexer.Location);
2716 Console.WriteLine ("OOPS. Unexpected result from scanner");
2721 : TRUE { $$ = new BoolLiteral (true, lexer.Location); }
2722 | FALSE { $$ = new BoolLiteral (false, lexer.Location); }
2725 parenthesized_expression_0
2726 : OPEN_PARENS expression CLOSE_PARENS
2729 lexer.Deambiguate_CloseParens ();
2730 // After this, the next token returned is one of
2731 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2732 // or CLOSE_PARENS_MINUS.
2734 | OPEN_PARENS expression error { CheckToken (1026, yyToken, "Expecting ')'", lexer.Location); }
2737 parenthesized_expression
2738 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2742 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2744 // If a parenthesized expression is followed by a minus, we need to wrap
2745 // the expression inside a ParenthesizedExpression for the CS0075 check
2746 // in Binary.DoResolve().
2747 $$ = new ParenthesizedExpression ((Expression) $1);
2752 : primary_expression DOT IDENTIFIER
2754 LocatedToken lt = (LocatedToken) $3;
2755 $$ = new MemberAccess ((Expression) $1, lt.Value);
2757 | predefined_type DOT IDENTIFIER
2759 LocatedToken lt = (LocatedToken) $3;
2760 $$ = new MemberAccess ((Expression) $1, lt.Value);
2768 invocation_expression
2769 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2772 Report.Error (1, (Location) $2, "Parse error");
2774 $$ = new Invocation ((Expression) $1, (ArrayList) $3);
2776 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2778 $$ = new Invocation ((Expression) $1, new ArrayList ());
2780 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2782 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3);
2784 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2786 ArrayList args = new ArrayList (1);
2788 $$ = new Invocation ((Expression) $1, args);
2790 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2792 ArrayList args = ((ArrayList) $4);
2794 $$ = new Invocation ((Expression) $1, args);
2799 : /* empty */ { $$ = null; }
2806 ArrayList list = new ArrayList (4);
2810 | argument_list COMMA argument
2812 ArrayList list = (ArrayList) $1;
2816 | argument_list error {
2817 CheckToken (1026, yyToken, "Expected `,' or `)'", GetLocation ($2));
2825 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2827 | non_simple_argument
2834 : REF variable_reference
2836 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2838 | OUT variable_reference
2840 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2842 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2844 ArrayList list = (ArrayList) $3;
2845 Argument[] args = new Argument [list.Count];
2846 list.CopyTo (args, 0);
2848 Expression expr = new Arglist (args, (Location) $1);
2849 $$ = new Argument (expr, Argument.AType.Expression);
2853 $$ = new Argument (new ArglistAccess ((Location) $1), Argument.AType.ArgList);
2858 : expression { note ("section 5.4"); $$ = $1; }
2862 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2864 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3);
2866 | primary_expression rank_specifiers
2868 // So the super-trick is that primary_expression
2869 // can only be either a SimpleName or a MemberAccess.
2870 // The MemberAccess case arises when you have a fully qualified type-name like :
2872 // SimpleName is when you have
2875 Expression expr = (Expression) $1;
2876 if (expr is ComposedCast){
2877 $$ = new ComposedCast (expr, (string) $2);
2878 } else if (!(expr is SimpleName || expr is MemberAccess || expr is QualifiedAliasMember)){
2879 Error_ExpectingTypeName (expr);
2880 $$ = TypeManager.system_object_expr;
2883 // So we extract the string corresponding to the SimpleName
2886 $$ = new ComposedCast (expr, (string) $2);
2888 current_array_type = (Expression)$$;
2895 ArrayList list = new ArrayList (4);
2899 | expression_list COMMA expression
2901 ArrayList list = (ArrayList) $1;
2910 $$ = new This (current_block, (Location) $1);
2915 : BASE DOT IDENTIFIER
2917 LocatedToken lt = (LocatedToken) $3;
2918 $$ = new BaseAccess (lt.Value, lt.Location);
2920 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2922 $$ = new BaseIndexerAccess ((ArrayList) $3, (Location) $1);
2925 Report.Error (175, (Location) $1, "Use of keyword `base' is not valid in this context");
2930 post_increment_expression
2931 : primary_expression OP_INC
2933 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2934 (Expression) $1, (Location) $2);
2938 post_decrement_expression
2939 : primary_expression OP_DEC
2941 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2942 (Expression) $1, (Location) $2);
2947 : object_or_delegate_creation_expression
2948 | array_creation_expression
2951 object_or_delegate_creation_expression
2952 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2954 $$ = new New ((Expression) $2, (ArrayList) $4, (Location) $1);
2958 array_creation_expression
2959 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2961 opt_array_initializer
2963 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, (Location) $1);
2965 | NEW type rank_specifiers array_initializer
2967 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, (Location) $1);
2971 Report.Error (1031, (Location) $1, "Type expected");
2976 Report.Error (1526, (Location) $1, "A new expression requires () or [] after type");
2993 : rank_specifier opt_rank_specifier
2995 $$ = (string) $2 + (string) $1;
3000 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
3002 $$ = "[" + (string) $2 + "]";
3022 | dim_separators COMMA
3024 $$ = (string) $1 + ",";
3028 opt_array_initializer
3040 : OPEN_BRACE CLOSE_BRACE
3042 ArrayList list = new ArrayList (4);
3045 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
3047 $$ = (ArrayList) $2;
3051 variable_initializer_list
3052 : variable_initializer
3054 ArrayList list = new ArrayList (4);
3058 | variable_initializer_list COMMA variable_initializer
3060 ArrayList list = (ArrayList) $1;
3067 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
3069 $$ = new TypeOfVoid ((Location) $1);
3071 | TYPEOF OPEN_PARENS type CLOSE_PARENS
3073 $$ = new TypeOf ((Expression) $3, (Location) $1);
3078 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3079 $$ = new SizeOf ((Expression) $3, (Location) $1);
3084 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3086 $$ = new CheckedExpr ((Expression) $3, (Location) $1);
3090 unchecked_expression
3091 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3093 $$ = new UnCheckedExpr ((Expression) $3, (Location) $1);
3097 pointer_member_access
3098 : primary_expression OP_PTR IDENTIFIER
3101 LocatedToken lt = (LocatedToken) $3;
3103 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lt.Location);
3104 $$ = new MemberAccess (deref, lt.Value);
3108 anonymous_method_expression
3109 : DELEGATE opt_anonymous_method_signature
3111 if (oob_stack == null)
3112 oob_stack = new Stack (6);
3114 oob_stack.Push (current_local_parameters);
3115 current_local_parameters = (Parameters)$2;
3117 // Force the next block to be created as a ToplevelBlock
3118 oob_stack.Push (current_block);
3119 oob_stack.Push (top_current_block);
3120 current_block = null;
3124 Location loc = (Location) $1;
3125 top_current_block = (Block) oob_stack.Pop ();
3126 current_block = (Block) oob_stack.Pop ();
3127 if (RootContext.Version == LanguageVersion.ISO_1){
3128 Report.FeatureIsNotStandardized (loc, "anonymous methods");
3131 ToplevelBlock anon_block = (ToplevelBlock) $4;
3133 anon_block.Parent = current_block;
3134 $$ = new AnonymousMethod (current_container, (Parameters) $2, (ToplevelBlock) top_current_block,
3137 current_local_parameters = (Parameters) oob_stack.Pop ();
3141 opt_anonymous_method_signature
3142 : /* empty */ { $$ = null; }
3143 | anonymous_method_signature
3146 anonymous_method_signature
3147 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3150 $$ = Parameters.EmptyReadOnlyParameters;
3152 ArrayList par_list = (ArrayList) $2;
3153 Parameter [] pars = new Parameter [par_list.Count];
3154 par_list.CopyTo (pars);
3155 $$ = new Parameters (pars);
3160 opt_anonymous_method_parameter_list
3161 : /* empty */ { $$ = null; }
3162 | anonymous_method_parameter_list { $$ = $1; }
3165 anonymous_method_parameter_list
3166 : anonymous_method_parameter
3168 ArrayList a = new ArrayList (4);
3172 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3174 ArrayList a = (ArrayList) $1;
3180 anonymous_method_parameter
3181 : opt_parameter_modifier type IDENTIFIER {
3182 LocatedToken lt = (LocatedToken) $3;
3183 $$ = new Parameter ((Expression) $2, lt.Value, (Parameter.Modifier) $1, null, lt.Location);
3185 | PARAMS type IDENTIFIER {
3186 Report.Error (1670, ((LocatedToken) $3).Location, "The `params' modifier is not allowed in anonymous method declaration");
3192 : primary_expression
3193 | BANG prefixed_unary_expression
3195 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, (Location) $1);
3197 | TILDE prefixed_unary_expression
3199 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, (Location) $1);
3205 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3207 $$ = new Cast ((Expression) $1, (Expression) $3);
3209 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3211 $$ = new Cast ((Expression) $1, (Expression) $3);
3217 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3219 // TODO: wrong location
3220 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3225 // The idea to split this out is from Rhys' grammar
3226 // to solve the problem with casts.
3228 prefixed_unary_expression
3230 | PLUS prefixed_unary_expression
3232 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, (Location) $1);
3234 | MINUS prefixed_unary_expression
3236 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, (Location) $1);
3238 | OP_INC prefixed_unary_expression
3240 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3241 (Expression) $2, (Location) $1);
3243 | OP_DEC prefixed_unary_expression
3245 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3246 (Expression) $2, (Location) $1);
3248 | STAR prefixed_unary_expression
3250 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, (Location) $1);
3252 | BITWISE_AND prefixed_unary_expression
3254 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, (Location) $1);
3258 pre_increment_expression
3259 : OP_INC prefixed_unary_expression
3261 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3262 (Expression) $2, (Location) $1);
3266 pre_decrement_expression
3267 : OP_DEC prefixed_unary_expression
3269 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3270 (Expression) $2, (Location) $1);
3274 multiplicative_expression
3275 : prefixed_unary_expression
3276 | multiplicative_expression STAR prefixed_unary_expression
3278 $$ = new Binary (Binary.Operator.Multiply,
3279 (Expression) $1, (Expression) $3);
3281 | multiplicative_expression DIV prefixed_unary_expression
3283 $$ = new Binary (Binary.Operator.Division,
3284 (Expression) $1, (Expression) $3);
3286 | multiplicative_expression PERCENT prefixed_unary_expression
3288 $$ = new Binary (Binary.Operator.Modulus,
3289 (Expression) $1, (Expression) $3);
3294 : multiplicative_expression
3295 | additive_expression PLUS multiplicative_expression
3297 $$ = new Binary (Binary.Operator.Addition,
3298 (Expression) $1, (Expression) $3);
3300 | additive_expression MINUS multiplicative_expression
3302 $$ = new Binary (Binary.Operator.Subtraction,
3303 (Expression) $1, (Expression) $3);
3308 : additive_expression
3309 | shift_expression OP_SHIFT_LEFT additive_expression
3311 $$ = new Binary (Binary.Operator.LeftShift,
3312 (Expression) $1, (Expression) $3);
3314 | shift_expression OP_SHIFT_RIGHT additive_expression
3316 $$ = new Binary (Binary.Operator.RightShift,
3317 (Expression) $1, (Expression) $3);
3321 relational_expression
3323 | relational_expression OP_LT shift_expression
3325 $$ = new Binary (Binary.Operator.LessThan,
3326 (Expression) $1, (Expression) $3);
3328 | relational_expression OP_GT shift_expression
3330 $$ = new Binary (Binary.Operator.GreaterThan,
3331 (Expression) $1, (Expression) $3);
3333 | relational_expression OP_LE shift_expression
3335 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3336 (Expression) $1, (Expression) $3);
3338 | relational_expression OP_GE shift_expression
3340 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3341 (Expression) $1, (Expression) $3);
3343 | relational_expression IS type
3345 $$ = new Is ((Expression) $1, (Expression) $3, (Location) $2);
3347 | relational_expression AS type
3349 $$ = new As ((Expression) $1, (Expression) $3, (Location) $2);
3354 : relational_expression
3355 | equality_expression OP_EQ relational_expression
3357 $$ = new Binary (Binary.Operator.Equality,
3358 (Expression) $1, (Expression) $3);
3360 | equality_expression OP_NE relational_expression
3362 $$ = new Binary (Binary.Operator.Inequality,
3363 (Expression) $1, (Expression) $3);
3368 : equality_expression
3369 | and_expression BITWISE_AND equality_expression
3371 $$ = new Binary (Binary.Operator.BitwiseAnd,
3372 (Expression) $1, (Expression) $3);
3376 exclusive_or_expression
3378 | exclusive_or_expression CARRET and_expression
3380 $$ = new Binary (Binary.Operator.ExclusiveOr,
3381 (Expression) $1, (Expression) $3);
3385 inclusive_or_expression
3386 : exclusive_or_expression
3387 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3389 $$ = new Binary (Binary.Operator.BitwiseOr,
3390 (Expression) $1, (Expression) $3);
3394 conditional_and_expression
3395 : inclusive_or_expression
3396 | conditional_and_expression OP_AND inclusive_or_expression
3398 $$ = new Binary (Binary.Operator.LogicalAnd,
3399 (Expression) $1, (Expression) $3);
3403 conditional_or_expression
3404 : conditional_and_expression
3405 | conditional_or_expression OP_OR conditional_and_expression
3407 $$ = new Binary (Binary.Operator.LogicalOr,
3408 (Expression) $1, (Expression) $3);
3412 conditional_expression
3413 : conditional_or_expression
3414 | conditional_or_expression INTERR expression COLON expression
3416 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5);
3420 assignment_expression
3421 : prefixed_unary_expression ASSIGN expression
3423 $$ = new Assign ((Expression) $1, (Expression) $3);
3425 | prefixed_unary_expression OP_MULT_ASSIGN expression
3427 $$ = new CompoundAssign (
3428 Binary.Operator.Multiply, (Expression) $1, (Expression) $3);
3430 | prefixed_unary_expression OP_DIV_ASSIGN expression
3432 $$ = new CompoundAssign (
3433 Binary.Operator.Division, (Expression) $1, (Expression) $3);
3435 | prefixed_unary_expression OP_MOD_ASSIGN expression
3437 $$ = new CompoundAssign (
3438 Binary.Operator.Modulus, (Expression) $1, (Expression) $3);
3440 | prefixed_unary_expression OP_ADD_ASSIGN expression
3442 $$ = new CompoundAssign (
3443 Binary.Operator.Addition, (Expression) $1, (Expression) $3);
3445 | prefixed_unary_expression OP_SUB_ASSIGN expression
3447 $$ = new CompoundAssign (
3448 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3);
3450 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3452 $$ = new CompoundAssign (
3453 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3);
3455 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3457 $$ = new CompoundAssign (
3458 Binary.Operator.RightShift, (Expression) $1, (Expression) $3);
3460 | prefixed_unary_expression OP_AND_ASSIGN expression
3462 $$ = new CompoundAssign (
3463 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3);
3465 | prefixed_unary_expression OP_OR_ASSIGN expression
3467 $$ = new CompoundAssign (
3468 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3);
3470 | prefixed_unary_expression OP_XOR_ASSIGN expression
3472 $$ = new CompoundAssign (
3473 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3);
3478 : conditional_expression
3479 | assignment_expression
3499 MemberName name = MakeName ((MemberName) $5);
3500 int mod_flags = (int) $2;
3502 current_class = new Class (
3503 current_namespace, current_class, name,
3504 mod_flags, (Attributes) $1);
3507 current_container = current_container.AddPartial (current_class);
3509 current_container.AddClassOrStruct (current_class);
3510 current_container = current_class;
3516 if (current_class.Name == "System.Object") {
3517 Report.Error (537, current_class.Location,
3518 "The class System.Object cannot have a base " +
3519 "class or implement an interface.");
3521 current_class.Bases = (ArrayList) $7;
3524 if (RootContext.Documentation != null) {
3525 current_container.DocComment = Lexer.consume_doc_comment ();
3526 Lexer.doc_state = XmlCommentState.Allowed;
3531 if (RootContext.Documentation != null)
3532 Lexer.doc_state = XmlCommentState.Allowed;
3536 $$ = pop_current_class ();
3544 { $$ = $1; } // location
3548 : /* empty */ { $$ = (int) 0; }
3554 | modifiers modifier
3559 if ((m1 & m2) != 0) {
3560 Location l = lexer.Location;
3561 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3563 $$ = (int) (m1 | m2);
3568 : NEW { $$ = Modifiers.NEW; }
3569 | PUBLIC { $$ = Modifiers.PUBLIC; }
3570 | PROTECTED { $$ = Modifiers.PROTECTED; }
3571 | INTERNAL { $$ = Modifiers.INTERNAL; }
3572 | PRIVATE { $$ = Modifiers.PRIVATE; }
3573 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3574 | SEALED { $$ = Modifiers.SEALED; }
3575 | STATIC { $$ = Modifiers.STATIC; }
3576 | READONLY { $$ = Modifiers.READONLY; }
3577 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3578 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3579 | EXTERN { $$ = Modifiers.EXTERN; }
3580 | VOLATILE { $$ = Modifiers.VOLATILE; }
3581 | UNSAFE { $$ = Modifiers.UNSAFE; }
3585 : /* empty */ { $$ = null; }
3586 | class_base { $$ = $1; }
3590 : COLON type_list { $$ = $2; }
3598 // A block is "contained" on the following places:
3600 // property_declaration as part of the accessor body (get/set)
3601 // operator_declaration
3602 // constructor_declaration
3603 // destructor_declaration
3604 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3609 if (current_block == null){
3610 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, (Location) $1);
3611 top_current_block = current_block;
3613 current_block = new Block (current_block, (Location) $1, Location.Null);
3616 opt_statement_list CLOSE_BRACE
3618 while (current_block.Implicit)
3619 current_block = current_block.Parent;
3621 current_block.SetEndLocation ((Location) $4);
3622 current_block = current_block.Parent;
3623 if (current_block == null)
3624 top_current_block = null;
3635 | statement_list statement
3639 : declaration_statement
3641 if ($1 != null && (Block) $1 != current_block){
3642 current_block.AddStatement ((Statement) $1);
3643 current_block = (Block) $1;
3646 | valid_declaration_statement
3648 current_block.AddStatement ((Statement) $1);
3653 valid_declaration_statement
3656 | expression_statement
3657 | selection_statement
3658 | iteration_statement
3662 | unchecked_statement
3670 : valid_declaration_statement
3671 | declaration_statement
3673 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3678 Report.Error (1023, GetLocation ($1), "An embedded statement may not be a declaration or labeled statement");
3686 $$ = EmptyStatement.Value;
3693 LocatedToken lt = (LocatedToken) $1;
3694 LabeledStatement labeled = new LabeledStatement (lt.Value, lt.Location);
3696 if (current_block.AddLabel (labeled))
3697 current_block.AddStatement (labeled);
3702 declaration_statement
3703 : local_variable_declaration SEMICOLON
3705 current_array_type = null;
3707 DictionaryEntry de = (DictionaryEntry) $1;
3708 Expression e = (Expression) de.Key;
3710 $$ = declare_local_variables (e, (ArrayList) de.Value, e.Location);
3714 | local_constant_declaration SEMICOLON
3716 current_array_type = null;
3718 DictionaryEntry de = (DictionaryEntry) $1;
3720 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3726 * The following is from Rhys' grammar:
3727 * > Types in local variable declarations must be recognized as
3728 * > expressions to prevent reduce/reduce errors in the grammar.
3729 * > The expressions are converted into types during semantic analysis.
3732 : primary_expression opt_rank_specifier
3734 // FIXME: Do something smart here regarding the composition of the type.
3736 // Ok, the above "primary_expression" is there to get rid of
3737 // both reduce/reduce and shift/reduces in the grammar, it should
3738 // really just be "type_name". If you use type_name, a reduce/reduce
3739 // creeps up. If you use namespace_or_type_name (which is all we need
3740 // really) two shift/reduces appear.
3743 // So the super-trick is that primary_expression
3744 // can only be either a SimpleName or a MemberAccess.
3745 // The MemberAccess case arises when you have a fully qualified type-name like :
3747 // SimpleName is when you have
3750 Expression expr = (Expression) $1;
3751 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3752 Error_ExpectingTypeName (expr);
3756 // So we extract the string corresponding to the SimpleName
3760 if ((string) $2 == "")
3763 $$ = new ComposedCast ((Expression) $1, (string) $2);
3766 | builtin_types opt_rank_specifier
3768 if ((string) $2 == "")
3771 $$ = current_array_type = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3775 local_variable_pointer_type
3776 : primary_expression STAR
3778 Expression expr = (Expression) $1;
3780 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is QualifiedAliasMember)) {
3781 Error_ExpectingTypeName (expr);
3785 $$ = new ComposedCast ((Expression) $1, "*");
3787 | builtin_types STAR
3789 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3793 $$ = new ComposedCast (TypeManager.system_void_expr, "*", (Location) $1);
3795 | local_variable_pointer_type STAR
3797 $$ = new ComposedCast ((Expression) $1, "*");
3801 local_variable_declaration
3802 : local_variable_type variable_declarators
3805 $$ = new DictionaryEntry ($1, $2);
3809 | local_variable_pointer_type opt_rank_specifier variable_declarators
3814 if ((string) $2 == "")
3815 t = (Expression) $1;
3817 t = new ComposedCast ((Expression) $1, (string) $2);
3818 $$ = new DictionaryEntry (t, $3);
3824 local_constant_declaration
3825 : CONST local_variable_type constant_declarators
3828 $$ = new DictionaryEntry ($2, $3);
3834 expression_statement
3835 : statement_expression SEMICOLON { $$ = $1; }
3839 // We have to do the wrapping here and not in the case above,
3840 // because statement_expression is used for example in for_statement
3842 statement_expression
3845 Expression expr = (Expression) $1;
3846 ExpressionStatement s = expr as ExpressionStatement;
3848 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
3851 $$ = new StatementExpression (s);
3855 Report.Error (1002, GetLocation ($1), "Expecting `;'");
3860 object_creation_expression
3861 : object_or_delegate_creation_expression
3862 { note ("complain if this is a delegate maybe?"); }
3871 : IF OPEN_PARENS boolean_expression CLOSE_PARENS
3874 Location l = (Location) $1;
3876 $$ = new If ((Expression) $3, (Statement) $5, l);
3878 // FIXME: location for warning should be loc property of $5.
3879 if ($5 == EmptyStatement.Value)
3880 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3883 | IF OPEN_PARENS boolean_expression CLOSE_PARENS
3884 embedded_statement ELSE embedded_statement
3886 Location l = (Location) $1;
3888 $$ = new If ((Expression) $3, (Statement) $5, (Statement) $7, l);
3890 // FIXME: location for warning should be loc property of $5 and $7.
3891 if ($5 == EmptyStatement.Value)
3892 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3893 if ($7 == EmptyStatement.Value)
3894 Report.Warning (642, 3, l, "Possible mistaken empty statement");
3899 : SWITCH OPEN_PARENS
3901 if (switch_stack == null)
3902 switch_stack = new Stack (2);
3903 switch_stack.Push (current_block);
3905 expression CLOSE_PARENS
3908 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) $1);
3909 current_block = (Block) switch_stack.Pop ();
3925 Report.Error (1522, lexer.Location, "Empty switch block");
3933 ArrayList sections = new ArrayList (4);
3938 | switch_sections switch_section
3940 ArrayList sections = (ArrayList) $1;
3950 current_block = current_block.CreateSwitchBlock (lexer.Location);
3954 Block topmost = current_block;
3956 while (topmost.Implicit)
3957 topmost = topmost.Parent;
3958 $$ = new SwitchSection ((ArrayList) $1, topmost);
3965 ArrayList labels = new ArrayList (4);
3970 | switch_labels switch_label
3972 ArrayList labels = (ArrayList) ($1);
3980 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, (Location) $1); }
3981 | DEFAULT COLON { $$ = new SwitchLabel (null, (Location) $1); }
3984 1523, GetLocation ($1),
3985 "The keyword case or default must precede code in switch block");
3997 : WHILE OPEN_PARENS boolean_expression CLOSE_PARENS embedded_statement
3999 Location l = (Location) $1;
4000 $$ = new While ((Expression) $3, (Statement) $5, l);
4005 : DO embedded_statement
4006 WHILE OPEN_PARENS boolean_expression CLOSE_PARENS SEMICOLON
4008 Location l = (Location) $1;
4010 $$ = new Do ((Statement) $2, (Expression) $5, l);
4016 opt_for_initializer SEMICOLON
4018 Block assign_block = new Block (current_block);
4019 current_block = assign_block;
4021 if ($3 is DictionaryEntry){
4022 DictionaryEntry de = (DictionaryEntry) $3;
4024 Expression type = (Expression) de.Key;
4025 ArrayList var_declarators = (ArrayList) de.Value;
4027 foreach (VariableDeclaration decl in var_declarators){
4031 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4035 Location l = lexer.Location;
4036 Expression expr = decl.expression_or_array_initializer;
4038 LocalVariableReference var;
4039 var = new LocalVariableReference (assign_block, decl.identifier, l);
4042 Assign a = new Assign (var, expr, decl.Location);
4044 assign_block.AddStatement (new StatementExpression (a));
4048 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
4049 // This can be referred to as $5 below.
4055 opt_for_condition SEMICOLON
4056 opt_for_iterator CLOSE_PARENS
4059 Location l = (Location) $1;
4061 For f = new For ((Statement) $5, (Expression) $6, (Statement) $8, (Statement) $10, l);
4063 current_block.AddStatement (f);
4064 while (current_block.Implicit)
4065 current_block = current_block.Parent;
4067 current_block = current_block.Parent;
4072 : /* empty */ { $$ = EmptyStatement.Value; }
4077 : local_variable_declaration
4078 | statement_expression_list
4082 : /* empty */ { $$ = null; }
4083 | boolean_expression
4087 : /* empty */ { $$ = EmptyStatement.Value; }
4092 : statement_expression_list
4095 statement_expression_list
4096 : statement_expression
4098 // CHANGE: was `null'
4099 Statement s = (Statement) $1;
4100 Block b = new Block (current_block, Block.Flags.Implicit, s.loc, lexer.Location);
4105 | statement_expression_list COMMA statement_expression
4107 Block b = (Block) $1;
4109 b.AddStatement ((Statement) $3);
4115 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4117 Report.Error (230, (Location) $1, "Type and identifier are both required in a foreach statement");
4120 | FOREACH OPEN_PARENS type IDENTIFIER IN
4121 expression CLOSE_PARENS
4123 Block foreach_block = new Block (current_block);
4124 current_block = foreach_block;
4126 LocatedToken lt = (LocatedToken) $4;
4127 Location l = lt.Location;
4130 vi = foreach_block.AddVariable ((Expression) $3, lt.Value, l);
4132 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4134 // Get a writable reference to this read-only variable.
4136 // Note that the $$ here refers to the value of _this_ code block,
4137 // not the value of the LHS non-terminal. This can be referred to as $8 below.
4138 $$ = new LocalVariableReference (foreach_block, lt.Value, l, vi, false);
4145 LocalVariableReference v = (LocalVariableReference) $8;
4146 Location l = (Location) $1;
4149 Foreach f = new Foreach ((Expression) $3, v, (Expression) $6, (Statement) $9, l);
4150 current_block.AddStatement (f);
4153 while (current_block.Implicit)
4154 current_block = current_block.Parent;
4156 current_block = current_block.Parent;
4162 | continue_statement
4172 $$ = new Break ((Location) $1);
4177 : CONTINUE SEMICOLON
4179 $$ = new Continue ((Location) $1);
4184 : GOTO IDENTIFIER SEMICOLON
4186 LocatedToken lt = (LocatedToken) $2;
4187 $$ = new Goto (lt.Value, lt.Location);
4189 | GOTO CASE constant_expression SEMICOLON
4191 $$ = new GotoCase ((Expression) $3, (Location) $1);
4193 | GOTO DEFAULT SEMICOLON
4195 $$ = new GotoDefault ((Location) $1);
4200 : RETURN opt_expression SEMICOLON
4202 $$ = new Return ((Expression) $2, (Location) $1);
4207 : THROW opt_expression SEMICOLON
4209 $$ = new Throw ((Expression) $2, (Location) $1);
4214 : IDENTIFIER RETURN expression SEMICOLON
4216 LocatedToken lt = (LocatedToken) $1;
4217 string s = lt.Value;
4219 Report.Error (1003, lt.Location, "; expected");
4222 if (RootContext.Version == LanguageVersion.ISO_1){
4223 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4226 if (iterator_container == null){
4227 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4230 iterator_container.SetYields ();
4231 $$ = new Yield ((Expression) $3, lt.Location);
4234 | IDENTIFIER RETURN SEMICOLON
4236 Report.Error (1627, (Location) $2, "Expression expected after yield return");
4239 | IDENTIFIER BREAK SEMICOLON
4241 LocatedToken lt = (LocatedToken) $1;
4242 string s = lt.Value;
4244 Report.Error (1003, lt.Location, "; expected");
4247 if (RootContext.Version == LanguageVersion.ISO_1){
4248 Report.FeatureIsNotStandardized (lt.Location, "yield statement");
4251 if (iterator_container == null){
4252 Report.Error (204, lt.Location, "yield statement can only be used within a method, operator or property");
4255 iterator_container.SetYields ();
4256 $$ = new YieldBreak (lt.Location);
4267 : TRY block catch_clauses
4271 ArrayList c = (ArrayList)$3;
4272 for (int i = 0; i < c.Count; ++i) {
4273 Catch cc = (Catch) c [i];
4275 if (i != c.Count - 1)
4276 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4283 // Now s contains the list of specific catch clauses
4284 // and g contains the general one.
4286 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4288 | TRY block opt_catch_clauses FINALLY block
4291 ArrayList s = new ArrayList (4);
4292 ArrayList catch_list = (ArrayList) $3;
4294 if (catch_list != null){
4295 foreach (Catch cc in catch_list) {
4303 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4307 Report.Error (1524, (Location) $1, "Expected catch or finally");
4313 : /* empty */ { $$ = null; }
4320 ArrayList l = new ArrayList (4);
4325 | catch_clauses catch_clause
4327 ArrayList l = (ArrayList) $1;
4335 : /* empty */ { $$ = null; }
4340 : CATCH opt_catch_args
4342 Expression type = null;
4345 DictionaryEntry cc = (DictionaryEntry) $2;
4346 type = (Expression) cc.Key;
4347 LocatedToken lt = (LocatedToken) cc.Value;
4350 ArrayList one = new ArrayList (4);
4352 one.Add (new VariableDeclaration (lt, null));
4354 current_block = new Block (current_block);
4355 Block b = declare_local_variables (type, one, lt.Location);
4360 Expression type = null;
4362 Block var_block = null;
4365 DictionaryEntry cc = (DictionaryEntry) $2;
4366 type = (Expression) cc.Key;
4367 LocatedToken lt = (LocatedToken) cc.Value;
4371 while (current_block.Implicit)
4372 current_block = current_block.Parent;
4373 var_block = current_block;
4374 current_block = current_block.Parent;
4378 $$ = new Catch (type, id, (Block) $4, var_block, ((Block) $4).loc);
4383 : /* empty */ { $$ = null; }
4388 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4390 $$ = new DictionaryEntry ($2, $3);
4398 $$ = new Checked ((Block) $2);
4405 $$ = new Unchecked ((Block) $2);
4412 RootContext.CheckUnsafeOption ((Location) $1);
4414 $$ = new Unsafe ((Block) $3);
4420 type fixed_pointer_declarators
4423 ArrayList list = (ArrayList) $4;
4424 Expression type = (Expression) $3;
4425 Location l = (Location) $1;
4426 int top = list.Count;
4428 Block assign_block = new Block (current_block);
4429 current_block = assign_block;
4431 for (int i = 0; i < top; i++){
4432 Pair p = (Pair) list [i];
4435 v = current_block.AddVariable (type, (string) p.First, l);
4439 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4447 Location l = (Location) $1;
4449 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4451 current_block.AddStatement (f);
4452 while (current_block.Implicit)
4453 current_block = current_block.Parent;
4455 current_block = current_block.Parent;
4459 fixed_pointer_declarators
4460 : fixed_pointer_declarator {
4461 ArrayList declarators = new ArrayList (4);
4463 declarators.Add ($1);
4466 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4468 ArrayList declarators = (ArrayList) $1;
4470 declarators.Add ($3);
4475 fixed_pointer_declarator
4476 : IDENTIFIER ASSIGN expression
4478 LocatedToken lt = (LocatedToken) $1;
4479 // FIXME: keep location
4480 $$ = new Pair (lt.Value, $3);
4484 Report.Error (210, ((LocatedToken) $1).Location, "You must provide an initializer in a fixed or using statement declaration");
4490 : LOCK OPEN_PARENS expression CLOSE_PARENS
4496 $$ = new Lock ((Expression) $3, (Statement) $6, (Location) $1);
4501 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4503 Block assign_block = new Block (current_block);
4504 current_block = assign_block;
4506 if ($3 is DictionaryEntry){
4507 DictionaryEntry de = (DictionaryEntry) $3;
4508 Location l = (Location) $1;
4510 Expression type = (Expression) de.Key;
4511 ArrayList var_declarators = (ArrayList) de.Value;
4513 ArrayList vars = new ArrayList (4);
4515 foreach (VariableDeclaration decl in var_declarators){
4517 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4520 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4522 Expression expr = decl.expression_or_array_initializer;
4524 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4527 LocalVariableReference var;
4529 // Get a writable reference to this read-only variable.
4530 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4532 // This is so that it is not a warning on using variables
4535 vars.Add (new DictionaryEntry (var, expr));
4537 // Assign a = new Assign (var, expr, decl.Location);
4538 // assign_block.AddStatement (new StatementExpression (a));
4541 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4542 // It can be referred to as $5 below.
4543 $$ = new DictionaryEntry (type, vars);
4550 Using u = new Using ($5, (Statement) $6, (Location) $1);
4551 current_block.AddStatement (u);
4552 while (current_block.Implicit)
4553 current_block = current_block.Parent;
4555 current_block = current_block.Parent;
4559 resource_acquisition
4560 : local_variable_declaration
4567 // A class used to pass around variable declarations and constants
4569 public class VariableDeclaration {
4570 public string identifier;
4571 public Expression expression_or_array_initializer;
4572 public Location Location;
4573 public Attributes OptAttributes;
4574 public string DocComment;
4576 public VariableDeclaration (LocatedToken lt, object eoai, Attributes opt_attrs)
4578 this.identifier = lt.Value;
4579 if (eoai is ArrayList) {
4580 if (CSharpParser.current_array_type == null)
4581 Report.Error (622, lt.Location,
4582 "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
4583 this.expression_or_array_initializer = new ArrayCreation (CSharpParser.current_array_type, "", (ArrayList)eoai, lt.Location);
4585 this.expression_or_array_initializer = (Expression)eoai;
4587 this.Location = lt.Location;
4588 this.OptAttributes = opt_attrs;
4591 public VariableDeclaration (LocatedToken lt, object eoai) : this (lt, eoai, null)
4597 // A class used to hold info about an indexer declarator
4599 public class IndexerDeclaration {
4600 public Expression type;
4601 public MemberName interface_type;
4602 public Parameters param_list;
4603 public Location location;
4605 public IndexerDeclaration (Expression type, MemberName interface_type,
4606 Parameters param_list, Location loc)
4609 this.interface_type = interface_type;
4610 this.param_list = param_list;
4611 this.location = loc;
4616 // We use this when we do not have an object in advance that is an IIteratorContainer
4618 public class SimpleIteratorContainer : IIteratorContainer {
4621 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4626 public static SimpleIteratorContainer GetSimple () {
4627 Simple.Yields = false;
4631 public void SetYields () { Yields = true; }
4635 // A class used to hold info about an operator declarator
4637 public class OperatorDeclaration {
4638 public Operator.OpType optype;
4639 public Expression ret_type, arg1type, arg2type;
4640 public string arg1name, arg2name;
4641 public Location location;
4643 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4644 Expression arg1type, string arg1name,
4645 Expression arg2type, string arg2name, Location location)
4648 this.ret_type = ret_type;
4649 this.arg1type = arg1type;
4650 this.arg1name = arg1name;
4651 this.arg2type = arg2type;
4652 this.arg2name = arg2name;
4653 this.location = location;
4658 void Error_ExpectingTypeName (Expression expr)
4660 if (expr is Invocation){
4661 Report.Error (1002, expr.Location, "Expecting `;'");
4663 Report.Error (201, expr.Location, "Only assignment, call, increment, decrement, and new object expressions can be used as a statement");
4667 TypeContainer pop_current_class ()
4669 TypeContainer retval = current_class;
4671 current_class = (TypeContainer) current_class.Parent;
4672 current_container = current_class.PartialContainer;
4678 // Given the @class_name name, it creates a fully qualified name
4679 // based on the containing declaration space
4682 MakeName (MemberName class_name)
4684 Namespace ns = current_namespace.NS;
4686 if (current_container.Name.Length == 0){
4687 if (ns.Name.Length != 0)
4688 return new MemberName (ns.MemberName, class_name);
4692 return new MemberName (current_container.MemberName, class_name);
4696 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4698 Block implicit_block;
4699 ArrayList inits = null;
4702 // We use the `Used' property to check whether statements
4703 // have been added to the current block. If so, we need
4704 // to create another block to contain the new declaration
4705 // otherwise, as an optimization, we use the same block to
4706 // add the declaration.
4708 // FIXME: A further optimization is to check if the statements
4709 // that were added were added as part of the initialization
4710 // below. In which case, no other statements have been executed
4711 // and we might be able to reduce the number of blocks for
4712 // situations like this:
4714 // int j = 1; int k = j + 1;
4716 if (current_block.Used)
4717 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4719 implicit_block = current_block;
4721 foreach (VariableDeclaration decl in variable_declarators){
4723 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4724 if (decl.expression_or_array_initializer != null){
4726 inits = new ArrayList (4);
4733 return implicit_block;
4735 foreach (VariableDeclaration decl in inits){
4737 Expression expr = decl.expression_or_array_initializer;
4739 LocalVariableReference var;
4740 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4742 assign = new Assign (var, expr, decl.Location);
4744 implicit_block.AddStatement (new StatementExpression (assign));
4747 return implicit_block;
4750 Block declare_local_constants (Expression type, ArrayList declarators)
4752 Block implicit_block;
4754 if (current_block.Used)
4755 implicit_block = new Block (current_block, Block.Flags.Implicit);
4757 implicit_block = current_block;
4759 foreach (VariableDeclaration decl in declarators){
4760 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4763 return implicit_block;
4766 void CheckAttributeTarget (string a, Location l)
4770 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4774 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4780 void CheckUnaryOperator (Operator.OpType op, Location l)
4784 case Operator.OpType.LogicalNot:
4785 case Operator.OpType.OnesComplement:
4786 case Operator.OpType.Increment:
4787 case Operator.OpType.Decrement:
4788 case Operator.OpType.True:
4789 case Operator.OpType.False:
4790 case Operator.OpType.Addition:
4791 case Operator.OpType.Subtraction:
4796 Report.Error (1019, l, "Overloadable unary operator expected");
4802 void CheckBinaryOperator (Operator.OpType op, Location l)
4806 case Operator.OpType.Addition:
4807 case Operator.OpType.Subtraction:
4808 case Operator.OpType.Multiply:
4809 case Operator.OpType.Division:
4810 case Operator.OpType.Modulus:
4811 case Operator.OpType.BitwiseAnd:
4812 case Operator.OpType.BitwiseOr:
4813 case Operator.OpType.ExclusiveOr:
4814 case Operator.OpType.LeftShift:
4815 case Operator.OpType.RightShift:
4816 case Operator.OpType.Equality:
4817 case Operator.OpType.Inequality:
4818 case Operator.OpType.GreaterThan:
4819 case Operator.OpType.LessThan:
4820 case Operator.OpType.GreaterThanOrEqual:
4821 case Operator.OpType.LessThanOrEqual:
4825 Report.Error (1020, l, "Overloadable binary operator expected");
4831 void syntax_error (Location l, string msg)
4833 Report.Error (1003, l, "Syntax error, " + msg);
4836 void note (string s)
4838 // Used to put annotations
4843 public Tokenizer Lexer {
4849 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4851 current_namespace = new NamespaceEntry (null, file, null);
4852 this.name = file.Name;
4854 current_container = RootContext.Tree.Types;
4855 // TODO: Make RootContext.Tree.Types a PartialContainer.
4856 current_class = current_container;
4857 current_container.NamespaceEntry = current_namespace;
4859 lexer = new Tokenizer (reader, file, defines);
4862 public void parse ()
4864 int errors = Report.Errors;
4866 if (yacc_verbose_flag > 1)
4867 yyparse (lexer, new yydebug.yyDebugSimple ());
4870 Tokenizer tokenizer = lexer as Tokenizer;
4871 tokenizer.cleanup ();
4872 } catch (Exception e){
4874 // Removed for production use, use parser verbose to get the output.
4876 // Console.WriteLine (e);
4877 if (Report.Errors == errors)
4878 Report.Error (-25, lexer.Location, "Parsing error");
4879 if (yacc_verbose_flag > 0)
4880 Console.WriteLine (e);
4883 RootContext.Tree.Types.NamespaceEntry = null;
4886 static void CheckToken (int error, int yyToken, string msg, Location loc)
4888 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD)
4889 Report.Error (error, loc, "{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ());
4891 Report.Error (error, loc, msg);
4894 void CheckIdentifierToken (int yyToken, Location loc)
4896 CheckToken (1041, yyToken, "Identifier expected", loc);
4899 string ConsumeStoredComment ()
4901 string s = tmpComment;
4903 Lexer.doc_state = XmlCommentState.Allowed;
4907 Location GetLocation (object obj)
4909 if (obj is MemberCore)
4910 return ((MemberCore) obj).Location;
4911 if (obj is MemberName)
4912 return ((MemberName) obj).Location;
4913 if (obj is LocatedToken)
4914 return ((LocatedToken) obj).Location;
4915 if (obj is Location)
4916 return (Location) obj;
4917 return lexer.Location;