3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
47 /// This is used by the unary_expression code to resolve
48 /// a name against a parameter.
50 Parameters current_local_parameters;
53 /// Using during property parsing to describe the implicit
54 /// value parameter that is passed to the "set" and "get"accesor
55 /// methods (properties and indexers).
57 Expression implicit_value_parameter_type;
58 Parameters indexer_parameters;
61 /// Used to determine if we are parsing the get/set pair
62 /// of an indexer or a property
67 /// An out-of-band stack.
76 static public int yacc_verbose_flag;
78 // Name of the file we are parsing
87 /// Temporary Xml documentation cache.
88 /// For enum types, we need one more temporary store.
91 string enumTypeComment;
93 /// Current attribute target
94 string current_attr_target;
96 /// assembly and module attribute definition is enabled
97 bool global_attrs_enabled = true;
99 bool has_get, has_set;
104 %token NONE /* This token is never returned by our lexer */
105 %token ERROR // This is used not by the parser, but by the tokenizer.
109 *These are the C# keywords
195 /* C# keywords which are not really keywords */
201 /* C# single character operators/punctuation. */
202 %token OPEN_BRACE "{"
203 %token CLOSE_BRACE "}"
204 %token OPEN_BRACKET "["
205 %token CLOSE_BRACKET "]"
206 %token OPEN_PARENS "("
207 %token CLOSE_PARENS ")"
220 %token BITWISE_AND "&"
221 %token BITWISE_OR "|"
228 /* C# multi-character operators. */
231 %token OP_SHIFT_LEFT "<<"
232 %token OP_SHIFT_RIGHT ">>"
239 %token OP_MULT_ASSIGN "*="
240 %token OP_DIV_ASSIGN "/="
241 %token OP_MOD_ASSIGN "%="
242 %token OP_ADD_ASSIGN "+="
243 %token OP_SUB_ASSIGN "-="
244 %token OP_SHIFT_LEFT_ASSIGN "<<="
245 %token OP_SHIFT_RIGHT_ASSIGN ">>="
246 %token OP_AND_ASSIGN "&="
247 %token OP_XOR_ASSIGN "^="
248 %token OP_OR_ASSIGN "|="
252 %token LITERAL_INTEGER "int literal"
253 %token LITERAL_FLOAT "float literal"
254 %token LITERAL_DOUBLE "double literal"
255 %token LITERAL_DECIMAL "decimal literal"
256 %token LITERAL_CHARACTER "character literal"
257 %token LITERAL_STRING "string literal"
260 %token CLOSE_PARENS_CAST
261 %token CLOSE_PARENS_NO_CAST
262 %token CLOSE_PARENS_OPEN_PARENS
263 %token CLOSE_PARENS_MINUS
265 /* Add precedence rules to solve dangling else s/r conflict */
274 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
276 %left STAR DIV PERCENT
277 %right BANG CARRET UMINUS
278 %nonassoc OP_INC OP_DEC
280 %left OPEN_BRACKET OPEN_BRACE
284 %start compilation_unit
288 : outer_declarations opt_EOF
289 | outer_declarations global_attributes opt_EOF
290 | global_attributes opt_EOF
291 | opt_EOF /* allow empty files */
297 Lexer.check_incorrect_doc_comment ();
301 Lexer.check_incorrect_doc_comment ();
307 | outer_declarations outer_declaration
312 | namespace_member_declaration
317 | using_directives using_directive
321 : using_alias_directive
323 if (RootContext.Documentation != null)
324 Lexer.doc_state = XmlCommentState.Allowed;
326 | using_namespace_directive
328 if (RootContext.Documentation != null)
329 Lexer.doc_state = XmlCommentState.Allowed;
333 using_alias_directive
334 : USING IDENTIFIER ASSIGN
335 namespace_or_type_name SEMICOLON
337 MemberName name = (MemberName) $4;
338 current_namespace.UsingAlias ((string) $2, name.GetTypeExpression (lexer.Location), lexer.Location);
341 CheckIdentifierToken (yyToken);
345 using_namespace_directive
346 : USING namespace_name SEMICOLON
348 MemberName ns_name = (MemberName) $2;
349 current_namespace.Using (ns_name.GetTypeExpression (lexer.Location), lexer.Location);
354 // Strictly speaking, namespaces don't have attributes but
355 // we parse global attributes along with namespace declarations and then
358 namespace_declaration
359 : opt_attributes NAMESPACE namespace_or_type_name
362 Report.Error(1671, Lexer.Location, "A namespace declaration cannot have modifiers or attributes");
365 MemberName name = (MemberName) $3;
367 current_namespace = new NamespaceEntry (
368 current_namespace, file, name.GetName (), lexer.Location);
370 namespace_body opt_semicolon
372 current_namespace = current_namespace.Parent;
387 : namespace_or_type_name
393 if (RootContext.Documentation != null)
394 Lexer.doc_state = XmlCommentState.Allowed;
397 opt_namespace_member_declarations
408 opt_namespace_member_declarations
410 | namespace_member_declarations
413 namespace_member_declarations
414 : namespace_member_declaration
415 | namespace_member_declarations namespace_member_declaration
418 namespace_member_declaration
422 DeclSpace ds = (DeclSpace)$1;
424 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
425 Report.Error (1527, lexer.Location,
426 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
429 current_namespace.DeclarationFound = true;
431 | namespace_declaration {
432 current_namespace.DeclarationFound = true;
435 | field_declaration {
436 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
438 | method_declaration {
439 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
446 | interface_declaration
448 | delegate_declaration
450 // Enable this when we have handled all errors, because this acts as a generic fallback
453 // Console.WriteLine ("Token=" + yyToken);
454 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
466 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
474 global_attrs_enabled = false;
479 global_attrs_enabled = false;
488 ArrayList sect = (ArrayList) $1;
490 if (global_attrs_enabled) {
491 if (current_attr_target == "module") {
492 CodeGen.Module.AddAttributes (sect);
494 } else if (current_attr_target == "assembly") {
495 CodeGen.Assembly.AddAttributes (sect);
498 $$ = new Attributes (sect);
501 if (RootContext.Documentation != null) {
502 Lexer.check_incorrect_doc_comment ();
504 XmlCommentState.Allowed;
508 $$ = new Attributes (sect);
510 current_attr_target = null;
512 | attribute_sections attribute_section
514 Attributes attrs = $1 as Attributes;
515 ArrayList sect = (ArrayList) $2;
517 if (global_attrs_enabled) {
518 if (current_attr_target == "module") {
519 CodeGen.Module.AddAttributes (sect);
521 } else if (current_attr_target == "assembly") {
522 CodeGen.Assembly.AddAttributes (sect);
526 attrs = new Attributes (sect);
528 attrs.AddAttributes (sect);
532 attrs = new Attributes (sect);
534 attrs.AddAttributes (sect);
537 current_attr_target = null;
542 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
546 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
552 attribute_target_specifier
553 : attribute_target COLON
555 current_attr_target = (string)$1;
563 CheckAttributeTarget ((string) $1);
566 | EVENT { $$ = "event"; }
567 | RETURN { $$ = "return"; }
573 ArrayList attrs = new ArrayList (4);
579 | attribute_list COMMA attribute
581 ArrayList attrs = (ArrayList) $1;
593 opt_attribute_arguments
595 Location loc = (Location) $2;
596 MemberName mname = (MemberName) $1;
597 MemberName left = mname.Left;
598 string identifier = mname.Name;
600 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
602 if (current_attr_target == "assembly" || current_attr_target == "module")
603 $$ = new GlobalAttribute (current_container, current_attr_target,
604 left_expr, identifier, (ArrayList) $3, loc);
606 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
611 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
614 opt_attribute_arguments
615 : /* empty */ { $$ = null; }
616 | OPEN_PARENS attribute_arguments CLOSE_PARENS
624 : opt_positional_argument_list
629 ArrayList args = new ArrayList (4);
635 | positional_argument_list COMMA named_argument_list
637 ArrayList args = new ArrayList (4);
643 | named_argument_list
645 ArrayList args = new ArrayList (4);
654 opt_positional_argument_list
655 : /* empty */ { $$ = null; }
656 | positional_argument_list
659 positional_argument_list
662 ArrayList args = new ArrayList (4);
663 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
667 | positional_argument_list COMMA expression
669 ArrayList args = (ArrayList) $1;
670 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
679 ArrayList args = new ArrayList (4);
684 | named_argument_list COMMA named_argument
686 ArrayList args = (ArrayList) $1;
691 | named_argument_list COMMA expression
693 Report.Error (1016, lexer.Location, "Named attribute argument expected");
699 : IDENTIFIER ASSIGN expression
701 $$ = new DictionaryEntry (
703 new Argument ((Expression) $3, Argument.AType.Expression));
709 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
712 opt_class_member_declarations
714 | class_member_declarations
717 class_member_declarations
718 : class_member_declaration
719 | class_member_declarations
720 class_member_declaration
723 class_member_declaration
724 : constant_declaration // done
725 | field_declaration // done
726 | method_declaration // done
727 | property_declaration // done
728 | event_declaration // done
729 | indexer_declaration // done
730 | operator_declaration // done
731 | constructor_declaration // done
732 | destructor_declaration // done
742 MemberName name = MakeName ((MemberName) $5);
743 bool partial = (bool) $3;
746 ClassPart part = PartialContainer.CreatePart (
747 current_namespace, current_class, name, (int) $2,
748 (Attributes) $1, Kind.Struct, lexer.Location);
750 current_container = part.PartialContainer;
751 current_class = part;
753 current_class = new Struct (
754 current_namespace, current_class, name, (int) $2,
755 (Attributes) $1, lexer.Location);
757 current_container.AddClassOrStruct (current_class);
758 current_container = current_class;
759 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
765 current_class.Bases = (ArrayList) $7;
767 if (RootContext.Documentation != null)
768 current_class.DocComment = Lexer.consume_doc_comment ();
772 if (RootContext.Documentation != null)
773 Lexer.doc_state = XmlCommentState.Allowed;
777 $$ = pop_current_class ();
779 | opt_attributes opt_modifiers opt_partial STRUCT error {
780 CheckIdentifierToken (yyToken);
787 if (RootContext.Documentation != null)
788 Lexer.doc_state = XmlCommentState.Allowed;
790 opt_struct_member_declarations CLOSE_BRACE
793 opt_struct_member_declarations
795 | struct_member_declarations
798 struct_member_declarations
799 : struct_member_declaration
800 | struct_member_declarations struct_member_declaration
803 struct_member_declaration
804 : constant_declaration
807 | property_declaration
809 | indexer_declaration
810 | operator_declaration
811 | constructor_declaration
815 * This is only included so we can flag error 575:
816 * destructors only allowed on class types
818 | destructor_declaration
829 int modflags = (int) $2;
830 foreach (VariableDeclaration constant in (ArrayList) $5){
831 Location l = constant.Location;
832 if ((modflags & Modifiers.STATIC) != 0) {
833 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
837 Const c = new Const (
838 current_class, (Expression) $4, (string) constant.identifier,
839 (Expression) constant.expression_or_array_initializer, modflags,
842 if (RootContext.Documentation != null) {
843 c.DocComment = Lexer.consume_doc_comment ();
844 Lexer.doc_state = XmlCommentState.Allowed;
846 current_container.AddConstant (c);
852 : constant_declarator
854 ArrayList constants = new ArrayList (4);
859 | constant_declarators COMMA constant_declarator
862 ArrayList constants = (ArrayList) $1;
869 : IDENTIFIER ASSIGN constant_expression
871 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
875 // A const field requires a value to be provided
876 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
888 Expression type = (Expression) $3;
891 foreach (VariableDeclaration var in (ArrayList) $4){
892 Field field = new Field (current_class, type, mod, var.identifier,
893 var.expression_or_array_initializer,
894 (Attributes) $1, var.Location);
896 if (RootContext.Documentation != null) {
897 field.DocComment = Lexer.consume_doc_comment ();
898 Lexer.doc_state = XmlCommentState.Allowed;
900 current_container.AddField (field);
907 fixed_variable_declarators
910 Expression type = (Expression) $4;
913 foreach (VariableDeclaration var in (ArrayList) $5) {
914 FixedField field = new FixedField (current_class, type, mod, var.identifier,
915 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
917 if (RootContext.Documentation != null) {
918 field.DocComment = Lexer.consume_doc_comment ();
919 Lexer.doc_state = XmlCommentState.Allowed;
921 current_container.AddField (field);
929 Report.Error (670, lexer.Location, "void type is not allowed for fields");
933 fixed_variable_declarators
934 : fixed_variable_declarator
936 ArrayList decl = new ArrayList (2);
940 | fixed_variable_declarators COMMA fixed_variable_declarator
942 ArrayList decls = (ArrayList) $1;
948 fixed_variable_declarator
949 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
951 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
956 : variable_declarator
958 ArrayList decl = new ArrayList (4);
962 | variable_declarators COMMA variable_declarator
964 ArrayList decls = (ArrayList) $1;
971 : IDENTIFIER ASSIGN variable_initializer
973 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
977 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
979 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
981 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
982 "To declare a fixed buffer field, use the fixed keyword before the field type");
995 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
997 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
1001 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1008 iterator_container = (IIteratorContainer) $1;
1009 if (RootContext.Documentation != null)
1010 Lexer.doc_state = XmlCommentState.NotAllowed;
1014 Method method = (Method) $1;
1015 Block b = (Block) $3;
1016 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1019 if ((method.ModFlags & extern_abstract) == 0){
1021 501, lexer.Location, current_container.MakeName (method.Name) +
1022 "must declare a body because it is not marked abstract or extern");
1025 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1027 179, lexer.Location, current_container.MakeName (method.Name) +
1028 " is declared extern, but has a body");
1032 method.Block = (ToplevelBlock) $3;
1033 current_container.AddMethod (method);
1035 current_local_parameters = null;
1036 iterator_container = null;
1038 if (RootContext.Documentation != null)
1039 Lexer.doc_state = XmlCommentState.Allowed;
1053 1585, lexer.Location, "Member modifier `" +
1054 Modifiers.Name (i) + "' must precede member type and name");
1065 type namespace_or_type_name
1066 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1068 MemberName name = (MemberName) $4;
1070 Method method = new Method (current_class, (Expression) $3, (int) $2,
1071 false, name, (Parameters) $6, (Attributes) $1,
1074 current_local_parameters = (Parameters) $6;
1076 if (RootContext.Documentation != null)
1077 method.DocComment = Lexer.consume_doc_comment ();
1083 VOID namespace_or_type_name
1084 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1086 MemberName name = (MemberName) $4;
1088 Method method = new Method (current_class, TypeManager.system_void_expr,
1089 (int) $2, false, name, (Parameters) $6,
1090 (Attributes) $1, lexer.Location);
1092 current_local_parameters = (Parameters) $6;
1094 if (RootContext.Documentation != null)
1095 method.DocComment = Lexer.consume_doc_comment ();
1102 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1104 Report.Error (1585, lexer.Location,
1105 String.Format ("Modifier {0} should appear before type",
1106 Modifiers.Name ((int) $4)));
1107 MemberName name = (MemberName) $4;
1109 Method method = new Method (current_class, TypeManager.system_void_expr,
1110 0, false, name, (Parameters) $7, (Attributes) $1,
1113 current_local_parameters = (Parameters) $7;
1115 if (RootContext.Documentation != null)
1116 method.DocComment = Lexer.consume_doc_comment ();
1124 | SEMICOLON { $$ = null; }
1127 opt_formal_parameter_list
1128 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1129 | formal_parameter_list
1132 formal_parameter_list
1135 ArrayList pars_list = (ArrayList) $1;
1137 Parameter [] pars = new Parameter [pars_list.Count];
1138 pars_list.CopyTo (pars);
1140 $$ = new Parameters (pars, null, lexer.Location);
1142 | fixed_parameters COMMA parameter_array
1144 ArrayList pars_list = (ArrayList) $1;
1146 Parameter [] pars = new Parameter [pars_list.Count];
1147 pars_list.CopyTo (pars);
1149 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1151 | fixed_parameters COMMA ARGLIST
1153 ArrayList pars_list = (ArrayList) $1;
1155 Parameter [] pars = new Parameter [pars_list.Count];
1156 pars_list.CopyTo (pars);
1158 $$ = new Parameters (pars, true, lexer.Location);
1160 | parameter_array COMMA fixed_parameters
1162 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1165 | ARGLIST COMMA fixed_parameters
1167 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1172 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1176 $$ = new Parameters (null, true, lexer.Location);
1183 ArrayList pars = new ArrayList (4);
1188 | fixed_parameters COMMA fixed_parameter
1190 ArrayList pars = (ArrayList) $1;
1199 opt_parameter_modifier
1203 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1206 opt_parameter_modifier
1209 Report.Error (1001, lexer.Location, "Identifier expected");
1213 opt_parameter_modifier
1216 CheckIdentifierToken (yyToken);
1220 opt_parameter_modifier
1226 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1231 opt_parameter_modifier
1232 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1233 | parameter_modifier
1237 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1238 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1242 : opt_attributes PARAMS type IDENTIFIER
1244 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1245 note ("type must be a single-dimension array type");
1247 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1249 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1252 | opt_attributes PARAMS type error {
1253 CheckIdentifierToken (yyToken);
1258 property_declaration
1262 namespace_or_type_name
1264 if (RootContext.Documentation != null)
1265 tmpComment = Lexer.consume_doc_comment ();
1269 implicit_value_parameter_type = (Expression) $3;
1271 lexer.PropertyParsing = true;
1273 $$ = lexer.Location;
1275 iterator_container = SimpleIteratorContainer.GetSimple ();
1277 accessor_declarations
1279 lexer.PropertyParsing = false;
1280 has_get = has_set = false;
1288 Pair pair = (Pair) $8;
1289 Accessor get_block = (Accessor) pair.First;
1290 Accessor set_block = (Accessor) pair.Second;
1292 Location loc = (Location) $7;
1293 MemberName name = (MemberName) $4;
1295 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1296 name, (Attributes) $1, get_block, set_block, loc);
1297 if (SimpleIteratorContainer.Simple.Yields)
1300 current_container.AddProperty (prop);
1301 implicit_value_parameter_type = null;
1302 iterator_container = null;
1304 if (RootContext.Documentation != null)
1305 prop.DocComment = ConsumeStoredComment ();
1310 accessor_declarations
1311 : get_accessor_declaration
1313 $$ = new Pair ($1, null);
1315 | get_accessor_declaration accessor_declarations
1317 Pair pair = (Pair) $2;
1321 | set_accessor_declaration
1323 $$ = new Pair (null, $1);
1325 | set_accessor_declaration accessor_declarations
1327 Pair pair = (Pair) $2;
1333 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1338 get_accessor_declaration
1339 : opt_attributes opt_modifiers GET
1341 // If this is not the case, then current_local_parameters has already
1342 // been set in indexer_declaration
1343 if (parsing_indexer == false)
1344 current_local_parameters = null;
1346 current_local_parameters = indexer_parameters;
1347 lexer.PropertyParsing = false;
1352 Report.Error (1007, lexer.Location, "Property accessor already defined");
1355 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1357 current_local_parameters = null;
1358 lexer.PropertyParsing = true;
1360 if (RootContext.Documentation != null)
1361 if (Lexer.doc_state == XmlCommentState.Error)
1362 Lexer.doc_state = XmlCommentState.NotAllowed;
1366 set_accessor_declaration
1367 : opt_attributes opt_modifiers SET
1370 Parameter implicit_value_parameter = new Parameter (
1371 implicit_value_parameter_type, "value",
1372 Parameter.Modifier.NONE, null);
1374 if (parsing_indexer == false) {
1375 args = new Parameter [1];
1376 args [0] = implicit_value_parameter;
1377 current_local_parameters = new Parameters (args, null, lexer.Location);
1379 Parameter [] fpars = indexer_parameters.FixedParameters;
1382 int count = fpars.Length;
1384 args = new Parameter [count + 1];
1385 fpars.CopyTo (args, 0);
1386 args [count] = implicit_value_parameter;
1389 current_local_parameters = new Parameters (
1390 args, indexer_parameters.ArrayParameter, lexer.Location);
1393 lexer.PropertyParsing = false;
1398 Report.Error (1007, lexer.Location, "Property accessor already defined");
1401 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1403 current_local_parameters = null;
1404 lexer.PropertyParsing = true;
1406 if (RootContext.Documentation != null
1407 && Lexer.doc_state == XmlCommentState.Error)
1408 Lexer.doc_state = XmlCommentState.NotAllowed;
1414 | SEMICOLON { $$ = null; }
1417 interface_declaration
1421 INTERFACE member_name
1423 MemberName name = MakeName ((MemberName) $5);
1424 bool partial = (bool) $3;
1427 ClassPart part = PartialContainer.CreatePart (
1428 current_namespace, current_class, name, (int) $2,
1429 (Attributes) $1, Kind.Interface, lexer.Location);
1431 current_container = part.PartialContainer;
1432 current_class = part;
1434 current_class = new Interface (
1435 current_namespace, current_class, name, (int) $2,
1436 (Attributes) $1, lexer.Location);
1438 current_container.AddInterface (current_class);
1439 current_container = current_class;
1440 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1445 current_class.Bases = (ArrayList) $7;
1447 if (RootContext.Documentation != null) {
1448 current_class.DocComment = Lexer.consume_doc_comment ();
1449 Lexer.doc_state = XmlCommentState.Allowed;
1454 if (RootContext.Documentation != null)
1455 Lexer.doc_state = XmlCommentState.Allowed;
1459 $$ = pop_current_class ();
1461 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1462 CheckIdentifierToken (yyToken);
1468 opt_interface_member_declarations
1472 opt_interface_member_declarations
1474 | interface_member_declarations
1477 interface_member_declarations
1478 : interface_member_declaration
1479 | interface_member_declarations interface_member_declaration
1482 interface_member_declaration
1483 : interface_method_declaration
1488 Method m = (Method) $1;
1490 if (m.IsExplicitImpl)
1491 Report.Error (541, lexer.Location,
1492 "Explicit interface declaration can only be declared in a class or struct");
1494 current_container.AddMethod (m);
1496 if (RootContext.Documentation != null)
1497 Lexer.doc_state = XmlCommentState.Allowed;
1499 | interface_property_declaration
1504 Property p = (Property) $1;
1506 if (p.IsExplicitImpl)
1507 Report.Error (541, lexer.Location,
1508 "Explicit interface declaration can only be declared in a class or struct");
1510 current_container.AddProperty (p);
1512 if (RootContext.Documentation != null)
1513 Lexer.doc_state = XmlCommentState.Allowed;
1515 | interface_event_declaration
1518 Event e = (Event) $1;
1520 if (e.IsExplicitImpl)
1521 Report.Error (541, lexer.Location,
1522 "Explicit interface declaration can only be declared in a class or struct");
1524 current_container.AddEvent (e);
1527 if (RootContext.Documentation != null)
1528 Lexer.doc_state = XmlCommentState.Allowed;
1530 | interface_indexer_declaration
1535 Indexer i = (Indexer) $1;
1537 if (i.IsExplicitImpl)
1538 Report.Error (541, lexer.Location,
1539 "Explicit interface declaration can only be declared in a class or struct");
1541 current_container.AddIndexer (i);
1543 if (RootContext.Documentation != null)
1544 Lexer.doc_state = XmlCommentState.Allowed;
1546 | delegate_declaration
1548 Report.Error (524, lexer.Location, "Interfaces can not declare delegates");
1552 Report.Error (524, lexer.Location, "Interfaces can not declare classes");
1554 | struct_declaration
1556 Report.Error (524, lexer.Location, "Interfaces can not declare structures");
1560 Report.Error (524, lexer.Location, "Interfaces can not declare enumerations");
1562 | interface_declaration
1564 Report.Error (524, lexer.Location, "Interfaces can not declare interfaces");
1566 | constant_declaration
1568 Report.Error (525, lexer.Location, "Interfaces cannot contain constants");
1576 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1581 interface_method_declaration
1582 : opt_attributes opt_new type namespace_or_type_name
1583 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1586 MemberName name = (MemberName) $4;
1588 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1589 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1590 if (RootContext.Documentation != null)
1591 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1593 | opt_attributes opt_new type namespace_or_type_name
1594 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1595 OPEN_BRACE opt_statement_list CLOSE_BRACE
1597 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", $4);
1602 interface_property_declaration
1607 { lexer.PropertyParsing = true; }
1609 { lexer.PropertyParsing = false; }
1612 if ($3 == TypeManager.system_void_expr) {
1613 Report.Error (547, lexer.Location, "'{0}': property cannot have void type", $4);
1620 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1622 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1623 new MemberName ((string) $4), (Attributes) $1,
1624 pinfo.Get, pinfo.Set, lexer.Location);
1625 if (RootContext.Documentation != null)
1626 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1631 CheckIdentifierToken (yyToken);
1637 : opt_attributes opt_modifiers GET SEMICOLON
1638 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1639 | opt_attributes opt_modifiers GET OPEN_BRACE
1641 Report.Error (531, lexer.Location, "'{0}': interface members cannot have a definition", ".get");
1644 | opt_attributes opt_modifiers SET SEMICOLON
1645 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1646 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1647 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1648 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1649 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1652 Report.Error (548, lexer.Location, "'{0}' : property or indexer must have at least one accessor", "");
1656 interface_event_declaration
1657 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1659 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1660 new MemberName ((string) $5), null,
1661 (Attributes) $1, lexer.Location);
1662 if (RootContext.Documentation != null)
1663 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1665 | opt_attributes opt_new EVENT type error {
1666 CheckIdentifierToken (yyToken);
1669 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1670 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1673 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1675 lexer.EventParsing = true;
1677 event_accessor_declarations
1679 lexer.EventParsing = false;
1682 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1687 interface_indexer_declaration
1688 : opt_attributes opt_new type THIS
1689 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1691 { lexer.PropertyParsing = true; }
1693 { lexer.PropertyParsing = false; }
1699 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1701 $$ = new Indexer (current_class, (Expression) $3,
1702 new MemberName (TypeContainer.DefaultIndexerName),
1703 (int) $2, true, (Parameters) $6, (Attributes) $1,
1704 info.Get, info.Set, lexer.Location);
1705 if (RootContext.Documentation != null)
1706 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1710 operator_declaration
1711 : opt_attributes opt_modifiers operator_declarator
1713 iterator_container = SimpleIteratorContainer.GetSimple ();
1720 OperatorDeclaration decl = (OperatorDeclaration) $3;
1722 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1724 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1725 if (decl.arg2type != null)
1726 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1728 Operator op = new Operator (
1729 current_class, decl.optype, decl.ret_type, (int) $2,
1730 new Parameters (param_list, null, decl.location),
1731 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1733 if (RootContext.Documentation != null) {
1734 op.DocComment = tmpComment;
1735 Lexer.doc_state = XmlCommentState.Allowed;
1738 if (SimpleIteratorContainer.Simple.Yields)
1741 // Note again, checking is done in semantic analysis
1742 current_container.AddOperator (op);
1744 current_local_parameters = null;
1745 iterator_container = null;
1751 | SEMICOLON { $$ = null; }
1754 : type OPERATOR overloadable_operator
1755 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1757 Operator.OpType op = (Operator.OpType) $3;
1758 CheckUnaryOperator (op);
1760 if (op == Operator.OpType.Addition)
1761 op = Operator.OpType.UnaryPlus;
1763 if (op == Operator.OpType.Subtraction)
1764 op = Operator.OpType.UnaryNegation;
1766 Parameter [] pars = new Parameter [1];
1767 Expression type = (Expression) $5;
1769 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1771 current_local_parameters = new Parameters (pars, null, lexer.Location);
1773 if (RootContext.Documentation != null) {
1774 tmpComment = Lexer.consume_doc_comment ();
1775 Lexer.doc_state = XmlCommentState.NotAllowed;
1778 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1779 null, null, lexer.Location);
1781 | type OPERATOR overloadable_operator
1783 type IDENTIFIER COMMA
1787 CheckBinaryOperator ((Operator.OpType) $3);
1789 Parameter [] pars = new Parameter [2];
1791 Expression typeL = (Expression) $5;
1792 Expression typeR = (Expression) $8;
1794 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1795 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1797 current_local_parameters = new Parameters (pars, null, lexer.Location);
1799 if (RootContext.Documentation != null) {
1800 tmpComment = Lexer.consume_doc_comment ();
1801 Lexer.doc_state = XmlCommentState.NotAllowed;
1804 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1806 typeR, (string) $9, lexer.Location);
1808 | conversion_operator_declarator
1809 | type OPERATOR overloadable_operator
1811 type IDENTIFIER COMMA
1812 type IDENTIFIER COMMA
1816 Report.Error (1534, lexer.Location, "Overloaded binary operator '{0}' takes two parameters", $3);
1822 overloadable_operator
1824 : BANG { $$ = Operator.OpType.LogicalNot; }
1825 | TILDE { $$ = Operator.OpType.OnesComplement; }
1826 | OP_INC { $$ = Operator.OpType.Increment; }
1827 | OP_DEC { $$ = Operator.OpType.Decrement; }
1828 | TRUE { $$ = Operator.OpType.True; }
1829 | FALSE { $$ = Operator.OpType.False; }
1830 // Unary and binary:
1831 | PLUS { $$ = Operator.OpType.Addition; }
1832 | MINUS { $$ = Operator.OpType.Subtraction; }
1834 | STAR { $$ = Operator.OpType.Multiply; }
1835 | DIV { $$ = Operator.OpType.Division; }
1836 | PERCENT { $$ = Operator.OpType.Modulus; }
1837 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1838 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1839 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1840 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1841 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1842 | OP_EQ { $$ = Operator.OpType.Equality; }
1843 | OP_NE { $$ = Operator.OpType.Inequality; }
1844 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1845 | OP_LT { $$ = Operator.OpType.LessThan; }
1846 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1847 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1850 conversion_operator_declarator
1851 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1853 Parameter [] pars = new Parameter [1];
1855 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1857 current_local_parameters = new Parameters (pars, null, lexer.Location);
1859 if (RootContext.Documentation != null) {
1860 tmpComment = Lexer.consume_doc_comment ();
1861 Lexer.doc_state = XmlCommentState.NotAllowed;
1864 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1865 null, null, lexer.Location);
1867 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1869 Parameter [] pars = new Parameter [1];
1871 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1873 current_local_parameters = new Parameters (pars, null, lexer.Location);
1875 if (RootContext.Documentation != null) {
1876 tmpComment = Lexer.consume_doc_comment ();
1877 Lexer.doc_state = XmlCommentState.NotAllowed;
1880 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1881 null, null, lexer.Location);
1885 syntax_error (lexer.Location, "'operator' expected");
1889 syntax_error (lexer.Location, "'operator' expected");
1893 constructor_declaration
1896 constructor_declarator
1899 Constructor c = (Constructor) $3;
1900 c.Block = (ToplevelBlock) $4;
1901 c.OptAttributes = (Attributes) $1;
1902 c.ModFlags = (int) $2;
1904 if (RootContext.Documentation != null)
1905 c.DocComment = ConsumeStoredComment ();
1907 if (c.Name == current_container.Basename){
1908 if ((c.ModFlags & Modifiers.STATIC) != 0){
1909 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1911 515, c.Location, String.Format (
1912 "`{0}.{1}': static constructor can not have access modifiers",
1913 c.Name, current_container.Name));
1916 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1918 if (c.Initializer != null){
1921 "Static constructors can not have an explicit this or base " +
1922 "constructor invocations");
1925 if (!c.Parameters.Empty){
1927 132, c.Location, "Static constructors should not have parameters");
1930 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1933 // We let another layer check the validity of the constructor.
1934 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1937 current_container.AddConstructor (c);
1939 current_local_parameters = null;
1940 if (RootContext.Documentation != null)
1941 Lexer.doc_state = XmlCommentState.Allowed;
1945 constructor_declarator
1948 if (RootContext.Documentation != null) {
1949 tmpComment = Lexer.consume_doc_comment ();
1950 Lexer.doc_state = XmlCommentState.Allowed;
1953 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1955 current_local_parameters = (Parameters) $4;
1957 opt_constructor_initializer
1959 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1960 (ConstructorInitializer) $8, (Location) $6);
1966 | SEMICOLON { $$ = null; }
1969 opt_constructor_initializer
1970 : /* empty */ { $$ = null; }
1971 | constructor_initializer
1974 constructor_initializer
1975 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1977 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1979 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1981 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1984 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1990 : /* EMPTY */ { $$ = 0; }
1991 | UNSAFE { $$ = Modifiers.UNSAFE; }
1992 | EXTERN { $$ = Modifiers.EXTERN; }
1995 destructor_declaration
1996 : opt_attributes opt_finalizer TILDE
1998 if (RootContext.Documentation != null) {
1999 tmpComment = Lexer.consume_doc_comment ();
2000 Lexer.doc_state = XmlCommentState.NotAllowed;
2003 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2005 if ((string) $5 != current_container.Basename){
2006 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2007 } else if (current_container.Kind != Kind.Class){
2008 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
2010 Location l = lexer.Location;
2013 if (!RootContext.StdLib && current_container.Name == "System.Object")
2014 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2016 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2018 if ((m & Modifiers.UNSAFE) != 0){
2019 if (!RootContext.Unsafe){
2020 Report.Error (227, l,
2021 "Unsafe code requires the -unsafe command " +
2022 "line option to be specified");
2026 Method d = new Destructor (
2027 current_class, TypeManager.system_void_expr, m, "Finalize",
2028 new Parameters (null, null, l), (Attributes) $1, l);
2029 if (RootContext.Documentation != null)
2030 d.DocComment = ConsumeStoredComment ();
2032 d.Block = (ToplevelBlock) $8;
2033 current_container.AddMethod (d);
2041 EVENT type variable_declarators SEMICOLON
2043 foreach (VariableDeclaration var in (ArrayList) $5) {
2045 MemberName name = new MemberName (var.identifier);
2047 Event e = new EventField (
2048 current_class, (Expression) $4, (int) $2, false, name,
2049 var.expression_or_array_initializer, (Attributes) $1,
2052 current_container.AddEvent (e);
2054 if (RootContext.Documentation != null) {
2055 e.DocComment = Lexer.consume_doc_comment ();
2056 Lexer.doc_state = XmlCommentState.Allowed;
2062 EVENT type namespace_or_type_name
2065 implicit_value_parameter_type = (Expression) $4;
2066 lexer.EventParsing = true;
2068 event_accessor_declarations
2070 lexer.EventParsing = false;
2074 Location loc = (Location) $7;
2077 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2080 Pair pair = (Pair) $9;
2082 MemberName name = (MemberName) $5;
2084 Event e = new EventProperty (
2085 current_class, (Expression) $4, (int) $2, false, name, null,
2086 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2088 if (RootContext.Documentation != null) {
2089 e.DocComment = Lexer.consume_doc_comment ();
2090 Lexer.doc_state = XmlCommentState.Allowed;
2093 current_container.AddEvent (e);
2094 implicit_value_parameter_type = null;
2097 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2098 MemberName mn = (MemberName) $5;
2100 if (mn.Left != null)
2101 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2103 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2105 if (RootContext.Documentation != null)
2106 Lexer.doc_state = XmlCommentState.Allowed;
2110 event_accessor_declarations
2111 : add_accessor_declaration remove_accessor_declaration
2113 $$ = new Pair ($1, $2);
2115 | remove_accessor_declaration add_accessor_declaration
2117 $$ = new Pair ($2, $1);
2119 | add_accessor_declaration { $$ = null; }
2120 | remove_accessor_declaration { $$ = null; }
2123 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2129 add_accessor_declaration
2130 : opt_attributes ADD
2132 Parameter [] args = new Parameter [1];
2133 Parameter implicit_value_parameter = new Parameter (
2134 implicit_value_parameter_type, "value",
2135 Parameter.Modifier.NONE, null);
2137 args [0] = implicit_value_parameter;
2139 current_local_parameters = new Parameters (args, null, lexer.Location);
2140 lexer.EventParsing = false;
2144 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2145 lexer.EventParsing = true;
2147 | opt_attributes ADD error {
2148 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2151 | opt_attributes modifiers ADD {
2152 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2157 remove_accessor_declaration
2158 : opt_attributes REMOVE
2160 Parameter [] args = new Parameter [1];
2161 Parameter implicit_value_parameter = new Parameter (
2162 implicit_value_parameter_type, "value",
2163 Parameter.Modifier.NONE, null);
2165 args [0] = implicit_value_parameter;
2167 current_local_parameters = new Parameters (args, null, lexer.Location);
2168 lexer.EventParsing = false;
2172 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2173 lexer.EventParsing = true;
2175 | opt_attributes REMOVE error {
2176 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2179 | opt_attributes modifiers REMOVE {
2180 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2186 : opt_attributes opt_modifiers indexer_declarator
2189 IndexerDeclaration decl = (IndexerDeclaration) $3;
2191 implicit_value_parameter_type = decl.type;
2193 lexer.PropertyParsing = true;
2194 parsing_indexer = true;
2196 indexer_parameters = decl.param_list;
2198 accessor_declarations
2200 lexer.PropertyParsing = false;
2201 has_get = has_set = false;
2202 parsing_indexer = false;
2209 // The signature is computed from the signature of the indexer. Look
2210 // at section 3.6 on the spec
2211 Location loc = (Location) $5;
2213 IndexerDeclaration decl = (IndexerDeclaration) $3;
2214 Pair pair = (Pair) $7;
2215 Accessor get_block = (Accessor) pair.First;
2216 Accessor set_block = (Accessor) pair.Second;
2219 if (decl.interface_type != null)
2220 name = new MemberName (decl.interface_type,
2221 TypeContainer.DefaultIndexerName);
2223 name = new MemberName (TypeContainer.DefaultIndexerName);
2225 indexer = new Indexer (current_class, decl.type, name,
2226 (int) $2, false, decl.param_list, (Attributes) $1,
2227 get_block, set_block, loc);
2228 if (RootContext.Documentation != null)
2229 indexer.DocComment = ConsumeStoredComment ();
2231 current_container.AddIndexer (indexer);
2233 current_local_parameters = null;
2234 implicit_value_parameter_type = null;
2235 indexer_parameters = null;
2240 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2242 Parameters pars = (Parameters) $4;
2243 if (pars.HasArglist) {
2244 // "__arglist is not valid in this context"
2245 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2246 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2247 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2249 if (RootContext.Documentation != null) {
2250 tmpComment = Lexer.consume_doc_comment ();
2251 Lexer.doc_state = XmlCommentState.Allowed;
2254 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2256 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2258 Parameters pars = (Parameters) $6;
2260 if (pars.HasArglist) {
2261 // "__arglist is not valid in this context"
2262 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2263 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2264 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2267 MemberName name = (MemberName) $2;
2268 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2270 if (RootContext.Documentation != null) {
2271 tmpComment = Lexer.consume_doc_comment ();
2272 Lexer.doc_state = XmlCommentState.Allowed;
2282 if (RootContext.Documentation != null)
2283 enumTypeComment = Lexer.consume_doc_comment ();
2288 Location enum_location = lexer.Location;
2290 MemberName full_name = MakeName (new MemberName ((string) $4));
2291 Enum e = new Enum (current_namespace, current_class, (Expression) $5, (int) $2,
2292 full_name, (Attributes) $1, enum_location);
2294 if (RootContext.Documentation != null)
2295 e.DocComment = enumTypeComment;
2297 foreach (VariableDeclaration ev in (ArrayList) $7) {
2298 e.AddEnumMember (ev.identifier,
2299 (Expression) ev.expression_or_array_initializer,
2300 ev.Location, ev.OptAttributes,
2304 string name = full_name.GetName ();
2305 current_container.AddEnum (e);
2306 RootContext.Tree.RecordDecl (name, e);
2313 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2314 | COLON type { $$ = $2; }
2320 if (RootContext.Documentation != null)
2321 Lexer.doc_state = XmlCommentState.Allowed;
2323 opt_enum_member_declarations
2325 // here will be evaluated after CLOSE_BLACE is consumed.
2326 if (RootContext.Documentation != null)
2327 Lexer.doc_state = XmlCommentState.Allowed;
2335 opt_enum_member_declarations
2336 : /* empty */ { $$ = new ArrayList (4); }
2337 | enum_member_declarations opt_comma { $$ = $1; }
2340 enum_member_declarations
2341 : enum_member_declaration
2343 ArrayList l = new ArrayList (4);
2348 | enum_member_declarations COMMA enum_member_declaration
2350 ArrayList l = (ArrayList) $1;
2358 enum_member_declaration
2359 : opt_attributes IDENTIFIER
2361 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2363 if (RootContext.Documentation != null) {
2364 vd.DocComment = Lexer.consume_doc_comment ();
2365 Lexer.doc_state = XmlCommentState.Allowed;
2370 | opt_attributes IDENTIFIER
2372 $$ = lexer.Location;
2373 if (RootContext.Documentation != null) {
2374 tmpComment = Lexer.consume_doc_comment ();
2375 Lexer.doc_state = XmlCommentState.NotAllowed;
2380 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2382 if (RootContext.Documentation != null)
2383 vd.DocComment = ConsumeStoredComment ();
2389 delegate_declaration
2392 DELEGATE type member_name
2393 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2396 Location l = lexer.Location;
2397 MemberName name = MakeName ((MemberName) $5);
2398 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2399 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2401 if (RootContext.Documentation != null) {
2402 del.DocComment = Lexer.consume_doc_comment ();
2403 Lexer.doc_state = XmlCommentState.Allowed;
2406 current_container.AddDelegate (del);
2407 RootContext.Tree.RecordDecl (name.GetName (true), del);
2412 namespace_or_type_name
2414 | namespace_or_type_name DOT IDENTIFIER {
2415 $$ = new MemberName ((MemberName) $1, (string) $3);
2421 $$ = new MemberName ((string) $1);
2426 * Before you think of adding a return_type, notice that we have been
2427 * using two rules in the places where it matters (one rule using type
2428 * and another identical one that uses VOID as the return type). This
2429 * gets rid of a shift/reduce couple
2432 : namespace_or_type_name
2434 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2446 // Note that here only unmanaged types are allowed but we
2447 // can't perform checks during this phase - we do it during
2448 // semantic analysis.
2450 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2454 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2460 | non_expression_type rank_specifier
2462 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2464 | non_expression_type STAR
2466 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2468 | expression rank_specifiers
2470 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2474 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2478 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2479 // through this different path
2481 | multiplicative_expression STAR
2483 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2490 ArrayList types = new ArrayList (4);
2495 | type_list COMMA type
2497 ArrayList types = (ArrayList) $1;
2505 * replaces all the productions for isolating the various
2506 * simple types, but we need this to reuse it easily in local_variable_type
2509 : OBJECT { $$ = TypeManager.system_object_expr; }
2510 | STRING { $$ = TypeManager.system_string_expr; }
2511 | BOOL { $$ = TypeManager.system_boolean_expr; }
2512 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2513 | FLOAT { $$ = TypeManager.system_single_expr; }
2514 | DOUBLE { $$ = TypeManager.system_double_expr; }
2519 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2520 | BYTE { $$ = TypeManager.system_byte_expr; }
2521 | SHORT { $$ = TypeManager.system_int16_expr; }
2522 | USHORT { $$ = TypeManager.system_uint16_expr; }
2523 | INT { $$ = TypeManager.system_int32_expr; }
2524 | UINT { $$ = TypeManager.system_uint32_expr; }
2525 | LONG { $$ = TypeManager.system_int64_expr; }
2526 | ULONG { $$ = TypeManager.system_uint64_expr; }
2527 | CHAR { $$ = TypeManager.system_char_expr; }
2528 | VOID { $$ = TypeManager.system_void_expr; }
2532 : type rank_specifiers
2534 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2539 // Expressions, section 7.5
2549 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2551 | parenthesized_expression
2553 | invocation_expression
2557 | post_increment_expression
2558 | post_decrement_expression
2562 | checked_expression
2563 | unchecked_expression
2564 | pointer_member_access
2565 | anonymous_method_expression
2572 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2573 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2574 | NULL { $$ = NullLiteral.Null; }
2578 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2579 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2580 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2585 object v = lexer.Value;
2591 $$ = IntLiteral.Zero;
2593 $$ = IntLiteral.One;
2595 $$ = new IntLiteral (i);
2596 } else if (v is uint)
2597 $$ = new UIntLiteral ((UInt32) v);
2599 $$ = new LongLiteral ((Int64) v);
2600 else if (v is ulong)
2601 $$ = new ULongLiteral ((UInt64) v);
2603 Console.WriteLine ("OOPS. Unexpected result from scanner");
2608 : TRUE { $$ = new BoolLiteral (true); }
2609 | FALSE { $$ = new BoolLiteral (false); }
2612 parenthesized_expression_0
2613 : OPEN_PARENS expression CLOSE_PARENS
2616 lexer.Deambiguate_CloseParens ();
2617 // After this, the next token returned is one of
2618 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2619 // or CLOSE_PARENS_MINUS.
2623 parenthesized_expression
2624 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2628 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2630 // If a parenthesized expression is followed by a minus, we need to wrap
2631 // the expression inside a ParenthesizedExpression for the CS0075 check
2632 // in Binary.DoResolve().
2633 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2638 : primary_expression DOT IDENTIFIER
2640 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2642 | predefined_type DOT IDENTIFIER
2644 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2652 invocation_expression
2653 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2656 Location l = lexer.Location;
2657 Report.Error (1, l, "Parse error");
2659 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2661 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2663 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2665 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2667 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2669 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2671 ArrayList args = new ArrayList (1);
2673 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2675 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2677 ArrayList args = ((ArrayList) $4);
2679 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2684 : /* empty */ { $$ = null; }
2691 ArrayList list = new ArrayList (4);
2695 | argument_list COMMA argument
2697 ArrayList list = (ArrayList) $1;
2701 | argument_list error {
2702 CheckToken (1026, yyToken, ", or ) expected");
2709 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2711 | non_simple_argument
2718 : REF variable_reference
2720 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2722 | OUT variable_reference
2724 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2726 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2728 ArrayList list = (ArrayList) $3;
2729 Argument[] args = new Argument [list.Count];
2730 list.CopyTo (args, 0);
2732 Expression expr = new Arglist (args, lexer.Location);
2733 $$ = new Argument (expr, Argument.AType.Expression);
2737 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2742 : expression { note ("section 5.4"); $$ = $1; }
2746 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2748 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2750 | primary_expression rank_specifiers
2752 // So the super-trick is that primary_expression
2753 // can only be either a SimpleName or a MemberAccess.
2754 // The MemberAccess case arises when you have a fully qualified type-name like :
2756 // SimpleName is when you have
2759 Expression expr = (Expression) $1;
2760 if (expr is ComposedCast){
2761 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2762 } else if (!(expr is SimpleName || expr is MemberAccess)){
2763 Error_ExpectingTypeName (lexer.Location, expr);
2764 $$ = TypeManager.system_object_expr;
2767 // So we extract the string corresponding to the SimpleName
2770 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2778 ArrayList list = new ArrayList (4);
2782 | expression_list COMMA expression
2784 ArrayList list = (ArrayList) $1;
2793 $$ = new This (current_block, lexer.Location);
2798 : BASE DOT IDENTIFIER
2800 $$ = new BaseAccess ((string) $3, lexer.Location);
2802 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2804 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2807 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2812 post_increment_expression
2813 : primary_expression OP_INC
2815 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2816 (Expression) $1, lexer.Location);
2820 post_decrement_expression
2821 : primary_expression OP_DEC
2823 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2824 (Expression) $1, lexer.Location);
2829 : object_or_delegate_creation_expression
2830 | array_creation_expression
2833 object_or_delegate_creation_expression
2834 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2836 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2840 array_creation_expression
2841 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2843 opt_array_initializer
2845 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2847 | NEW type rank_specifiers array_initializer
2849 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2853 Report.Error (1031, lexer.Location, "Type expected");
2858 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2874 : rank_specifier opt_rank_specifier
2876 $$ = (string) $2 + (string) $1;
2881 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2883 $$ = "[" + (string) $2 + "]";
2903 | dim_separators COMMA
2905 $$ = (string) $1 + ",";
2909 opt_array_initializer
2921 : OPEN_BRACE CLOSE_BRACE
2923 ArrayList list = new ArrayList (4);
2926 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2928 $$ = (ArrayList) $2;
2932 variable_initializer_list
2933 : variable_initializer
2935 ArrayList list = new ArrayList (4);
2939 | variable_initializer_list COMMA variable_initializer
2941 ArrayList list = (ArrayList) $1;
2948 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2950 $$ = new TypeOfVoid (lexer.Location);
2952 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2954 $$ = new TypeOf ((Expression) $3, lexer.Location);
2959 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2960 $$ = new SizeOf ((Expression) $3, lexer.Location);
2965 : CHECKED OPEN_PARENS expression CLOSE_PARENS
2967 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
2971 unchecked_expression
2972 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
2974 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
2978 pointer_member_access
2979 : primary_expression OP_PTR IDENTIFIER
2983 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
2984 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
2988 anonymous_method_expression
2989 : DELEGATE opt_anonymous_method_signature _mark_
2991 oob_stack.Push (current_local_parameters);
2992 current_local_parameters = (Parameters)$2;
2994 // Force the next block to be created as a ToplevelBlock
2995 oob_stack.Push (current_block);
2996 oob_stack.Push (top_current_block);
2997 current_block = null;
3001 Location loc = (Location) $3;
3002 top_current_block = (Block) oob_stack.Pop ();
3003 current_block = (Block) oob_stack.Pop ();
3004 if (RootContext.Version == LanguageVersion.ISO_1){
3005 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3008 ToplevelBlock anon_block = (ToplevelBlock) $5;
3010 anon_block.Parent = current_block;
3011 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3014 current_local_parameters = (Parameters) oob_stack.Pop ();
3018 opt_anonymous_method_signature
3019 : /* empty */ { $$ = null; }
3020 | anonymous_method_signature
3023 anonymous_method_signature
3024 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3027 $$ = Parameters.EmptyReadOnlyParameters;
3029 ArrayList par_list = (ArrayList) $2;
3030 Parameter [] pars = new Parameter [par_list.Count];
3031 par_list.CopyTo (pars);
3032 $$ = new Parameters (pars, null, lexer.Location);
3037 opt_anonymous_method_parameter_list
3038 : /* empty */ { $$ = null; }
3039 | anonymous_method_parameter_list { $$ = $1; }
3042 anonymous_method_parameter_list
3043 : anonymous_method_parameter
3045 ArrayList a = new ArrayList (4);
3049 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3051 ArrayList a = (ArrayList) $1;
3057 anonymous_method_parameter
3058 : opt_parameter_modifier type IDENTIFIER {
3059 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3061 | PARAMS type IDENTIFIER {
3062 Report.Error (1670, lexer.Location, "params modifier not allowed in anonymous method declaration");
3068 : primary_expression
3069 | BANG prefixed_unary_expression
3071 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3073 | TILDE prefixed_unary_expression
3075 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3081 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3083 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3085 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3087 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3093 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3095 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3100 // The idea to split this out is from Rhys' grammar
3101 // to solve the problem with casts.
3103 prefixed_unary_expression
3105 | PLUS prefixed_unary_expression
3107 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3109 | MINUS prefixed_unary_expression
3111 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3113 | OP_INC prefixed_unary_expression
3115 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3116 (Expression) $2, lexer.Location);
3118 | OP_DEC prefixed_unary_expression
3120 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3121 (Expression) $2, lexer.Location);
3123 | STAR prefixed_unary_expression
3125 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3127 | BITWISE_AND prefixed_unary_expression
3129 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3133 pre_increment_expression
3134 : OP_INC prefixed_unary_expression
3136 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3137 (Expression) $2, lexer.Location);
3141 pre_decrement_expression
3142 : OP_DEC prefixed_unary_expression
3144 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3145 (Expression) $2, lexer.Location);
3149 multiplicative_expression
3150 : prefixed_unary_expression
3151 | multiplicative_expression STAR prefixed_unary_expression
3153 $$ = new Binary (Binary.Operator.Multiply,
3154 (Expression) $1, (Expression) $3, lexer.Location);
3156 | multiplicative_expression DIV prefixed_unary_expression
3158 $$ = new Binary (Binary.Operator.Division,
3159 (Expression) $1, (Expression) $3, lexer.Location);
3161 | multiplicative_expression PERCENT prefixed_unary_expression
3163 $$ = new Binary (Binary.Operator.Modulus,
3164 (Expression) $1, (Expression) $3, lexer.Location);
3169 : multiplicative_expression
3170 | additive_expression PLUS multiplicative_expression
3172 $$ = new Binary (Binary.Operator.Addition,
3173 (Expression) $1, (Expression) $3, lexer.Location);
3175 | additive_expression MINUS multiplicative_expression
3177 $$ = new Binary (Binary.Operator.Subtraction,
3178 (Expression) $1, (Expression) $3, lexer.Location);
3183 : additive_expression
3184 | shift_expression OP_SHIFT_LEFT additive_expression
3186 $$ = new Binary (Binary.Operator.LeftShift,
3187 (Expression) $1, (Expression) $3, lexer.Location);
3189 | shift_expression OP_SHIFT_RIGHT additive_expression
3191 $$ = new Binary (Binary.Operator.RightShift,
3192 (Expression) $1, (Expression) $3, lexer.Location);
3196 relational_expression
3198 | relational_expression OP_LT shift_expression
3200 $$ = new Binary (Binary.Operator.LessThan,
3201 (Expression) $1, (Expression) $3, lexer.Location);
3203 | relational_expression OP_GT shift_expression
3205 $$ = new Binary (Binary.Operator.GreaterThan,
3206 (Expression) $1, (Expression) $3, lexer.Location);
3208 | relational_expression OP_LE shift_expression
3210 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3211 (Expression) $1, (Expression) $3, lexer.Location);
3213 | relational_expression OP_GE shift_expression
3215 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3216 (Expression) $1, (Expression) $3, lexer.Location);
3218 | relational_expression IS type
3220 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3222 | relational_expression AS type
3224 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3229 : relational_expression
3230 | equality_expression OP_EQ relational_expression
3232 $$ = new Binary (Binary.Operator.Equality,
3233 (Expression) $1, (Expression) $3, lexer.Location);
3235 | equality_expression OP_NE relational_expression
3237 $$ = new Binary (Binary.Operator.Inequality,
3238 (Expression) $1, (Expression) $3, lexer.Location);
3243 : equality_expression
3244 | and_expression BITWISE_AND equality_expression
3246 $$ = new Binary (Binary.Operator.BitwiseAnd,
3247 (Expression) $1, (Expression) $3, lexer.Location);
3251 exclusive_or_expression
3253 | exclusive_or_expression CARRET and_expression
3255 $$ = new Binary (Binary.Operator.ExclusiveOr,
3256 (Expression) $1, (Expression) $3, lexer.Location);
3260 inclusive_or_expression
3261 : exclusive_or_expression
3262 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3264 $$ = new Binary (Binary.Operator.BitwiseOr,
3265 (Expression) $1, (Expression) $3, lexer.Location);
3269 conditional_and_expression
3270 : inclusive_or_expression
3271 | conditional_and_expression OP_AND inclusive_or_expression
3273 $$ = new Binary (Binary.Operator.LogicalAnd,
3274 (Expression) $1, (Expression) $3, lexer.Location);
3278 conditional_or_expression
3279 : conditional_and_expression
3280 | conditional_or_expression OP_OR conditional_and_expression
3282 $$ = new Binary (Binary.Operator.LogicalOr,
3283 (Expression) $1, (Expression) $3, lexer.Location);
3287 conditional_expression
3288 : conditional_or_expression
3289 | conditional_or_expression INTERR expression COLON expression
3291 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3295 assignment_expression
3296 : prefixed_unary_expression ASSIGN expression
3298 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3300 | prefixed_unary_expression OP_MULT_ASSIGN expression
3302 Location l = lexer.Location;
3304 $$ = new CompoundAssign (
3305 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3307 | prefixed_unary_expression OP_DIV_ASSIGN expression
3309 Location l = lexer.Location;
3311 $$ = new CompoundAssign (
3312 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3314 | prefixed_unary_expression OP_MOD_ASSIGN expression
3316 Location l = lexer.Location;
3318 $$ = new CompoundAssign (
3319 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3321 | prefixed_unary_expression OP_ADD_ASSIGN expression
3323 Location l = lexer.Location;
3325 $$ = new CompoundAssign (
3326 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3328 | prefixed_unary_expression OP_SUB_ASSIGN expression
3330 Location l = lexer.Location;
3332 $$ = new CompoundAssign (
3333 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3335 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3337 Location l = lexer.Location;
3339 $$ = new CompoundAssign (
3340 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3342 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3344 Location l = lexer.Location;
3346 $$ = new CompoundAssign (
3347 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3349 | prefixed_unary_expression OP_AND_ASSIGN expression
3351 Location l = lexer.Location;
3353 $$ = new CompoundAssign (
3354 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3356 | prefixed_unary_expression OP_OR_ASSIGN expression
3358 Location l = lexer.Location;
3360 $$ = new CompoundAssign (
3361 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3363 | prefixed_unary_expression OP_XOR_ASSIGN expression
3365 Location l = lexer.Location;
3367 $$ = new CompoundAssign (
3368 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3373 : conditional_expression
3374 | assignment_expression
3394 MemberName name = MakeName ((MemberName) $5);
3395 bool partial = (bool) $3;
3396 int mod_flags = (int) $2;
3399 ClassPart part = PartialContainer.CreatePart (
3400 current_namespace, current_class, name, mod_flags,
3401 (Attributes) $1, Kind.Class, lexer.Location);
3403 current_container = part.PartialContainer;
3404 current_class = part;
3406 if ((mod_flags & Modifiers.STATIC) != 0) {
3407 current_class = new StaticClass (
3408 current_namespace, current_class, name,
3409 mod_flags, (Attributes) $1, lexer.Location);
3411 current_class = new Class (
3412 current_namespace, current_class, name,
3413 mod_flags, (Attributes) $1, lexer.Location);
3416 current_container.AddClassOrStruct (current_class);
3417 current_container = current_class;
3418 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3424 if (current_class.Name == "System.Object") {
3425 Report.Error (537, current_class.Location,
3426 "The class System.Object cannot have a base " +
3427 "class or implement an interface.");
3429 current_class.Bases = (ArrayList) $7;
3432 if (RootContext.Documentation != null) {
3433 current_class.DocComment = Lexer.consume_doc_comment ();
3434 Lexer.doc_state = XmlCommentState.Allowed;
3439 if (RootContext.Documentation != null)
3440 Lexer.doc_state = XmlCommentState.Allowed;
3444 $$ = pop_current_class ();
3450 { $$ = (bool) false; }
3452 { $$ = (bool) true; }
3456 : /* empty */ { $$ = (int) 0; }
3462 | modifiers modifier
3467 if ((m1 & m2) != 0) {
3468 Location l = lexer.Location;
3469 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3471 $$ = (int) (m1 | m2);
3476 : NEW { $$ = Modifiers.NEW; }
3477 | PUBLIC { $$ = Modifiers.PUBLIC; }
3478 | PROTECTED { $$ = Modifiers.PROTECTED; }
3479 | INTERNAL { $$ = Modifiers.INTERNAL; }
3480 | PRIVATE { $$ = Modifiers.PRIVATE; }
3481 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3482 | SEALED { $$ = Modifiers.SEALED; }
3483 | STATIC { $$ = Modifiers.STATIC; }
3484 | READONLY { $$ = Modifiers.READONLY; }
3485 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3486 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3487 | EXTERN { $$ = Modifiers.EXTERN; }
3488 | VOLATILE { $$ = Modifiers.VOLATILE; }
3489 | UNSAFE { $$ = Modifiers.UNSAFE; }
3493 : /* empty */ { $$ = null; }
3494 | class_base { $$ = $1; }
3498 : COLON type_list { $$ = $2; }
3506 // A block is "contained" on the following places:
3508 // property_declaration as part of the accessor body (get/set)
3509 // operator_declaration
3510 // constructor_declaration
3511 // destructor_declaration
3512 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3517 if (current_block == null){
3518 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3519 top_current_block = current_block;
3521 current_block = new Block (current_block, current_local_parameters,
3522 lexer.Location, Location.Null);
3525 opt_statement_list CLOSE_BRACE
3527 while (current_block.Implicit)
3528 current_block = current_block.Parent;
3530 current_block.SetEndLocation (lexer.Location);
3531 current_block = current_block.Parent;
3532 if (current_block == null)
3533 top_current_block = null;
3544 | statement_list statement
3548 : declaration_statement
3550 if ($1 != null && (Block) $1 != current_block){
3551 current_block.AddStatement ((Statement) $1);
3552 current_block = (Block) $1;
3555 | valid_declaration_statement
3557 current_block.AddStatement ((Statement) $1);
3562 valid_declaration_statement
3565 | expression_statement
3566 | selection_statement
3567 | iteration_statement
3571 | unchecked_statement
3579 : valid_declaration_statement
3580 | declaration_statement
3582 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3587 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3595 $$ = EmptyStatement.Value;
3602 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3604 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3605 current_block.AddStatement (labeled);
3610 declaration_statement
3611 : local_variable_declaration SEMICOLON
3614 DictionaryEntry de = (DictionaryEntry) $1;
3616 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3620 | local_constant_declaration SEMICOLON
3623 DictionaryEntry de = (DictionaryEntry) $1;
3625 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3631 * The following is from Rhys' grammar:
3632 * > Types in local variable declarations must be recognized as
3633 * > expressions to prevent reduce/reduce errors in the grammar.
3634 * > The expressions are converted into types during semantic analysis.
3637 : primary_expression opt_rank_specifier
3639 // FIXME: Do something smart here regarding the composition of the type.
3641 // Ok, the above "primary_expression" is there to get rid of
3642 // both reduce/reduce and shift/reduces in the grammar, it should
3643 // really just be "type_name". If you use type_name, a reduce/reduce
3644 // creeps up. If you use namespace_or_type_name (which is all we need
3645 // really) two shift/reduces appear.
3648 // So the super-trick is that primary_expression
3649 // can only be either a SimpleName or a MemberAccess.
3650 // The MemberAccess case arises when you have a fully qualified type-name like :
3652 // SimpleName is when you have
3655 Expression expr = (Expression) $1;
3656 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3657 Error_ExpectingTypeName (lexer.Location, expr);
3661 // So we extract the string corresponding to the SimpleName
3665 if ((string) $2 == "")
3668 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3671 | builtin_types opt_rank_specifier
3673 if ((string) $2 == "")
3676 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3680 local_variable_pointer_type
3681 : primary_expression STAR
3683 Expression expr = (Expression) $1;
3684 Location l = lexer.Location;
3686 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3687 Error_ExpectingTypeName (l, expr);
3691 $$ = new ComposedCast ((Expression) $1, "*", l);
3693 | builtin_types STAR
3695 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3699 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3701 | local_variable_pointer_type STAR
3703 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3707 local_variable_declaration
3708 : local_variable_type variable_declarators
3711 $$ = new DictionaryEntry ($1, $2);
3715 | local_variable_pointer_type opt_rank_specifier variable_declarators
3720 if ((string) $2 == "")
3721 t = (Expression) $1;
3723 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3724 $$ = new DictionaryEntry (t, $3);
3730 local_constant_declaration
3731 : CONST local_variable_type constant_declarators
3734 $$ = new DictionaryEntry ($2, $3);
3740 expression_statement
3741 : statement_expression SEMICOLON
3748 // We have to do the wrapping here and not in the case above,
3749 // because statement_expression is used for example in for_statement
3751 statement_expression
3752 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3753 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3754 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3755 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3756 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3757 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3758 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3760 Report.Error (1002, lexer.Location, "Expecting `;'");
3765 object_creation_expression
3766 : object_or_delegate_creation_expression
3767 { note ("complain if this is a delegate maybe?"); }
3776 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3779 Location l = (Location) $3;
3781 $$ = new If ((Expression) $4, (Statement) $6, l);
3783 if (RootContext.WarningLevel >= 4){
3784 if ($6 == EmptyStatement.Value)
3785 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3789 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3790 embedded_statement ELSE embedded_statement
3792 Location l = (Location) $3;
3794 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3799 : SWITCH OPEN_PARENS _mark_
3801 switch_stack.Push (current_block);
3803 expression CLOSE_PARENS
3806 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3807 current_block = (Block) switch_stack.Pop ();
3823 Report.Error (1522, lexer.Location, "Empty switch block");
3831 ArrayList sections = new ArrayList (4);
3836 | switch_sections switch_section
3838 ArrayList sections = (ArrayList) $1;
3848 current_block = current_block.CreateSwitchBlock (lexer.Location);
3852 Block topmost = current_block;
3854 while (topmost.Implicit)
3855 topmost = topmost.Parent;
3856 $$ = new SwitchSection ((ArrayList) $1, topmost);
3863 ArrayList labels = new ArrayList (4);
3868 | switch_labels switch_label
3870 ArrayList labels = (ArrayList) ($1);
3878 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3879 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3882 1523, lexer.Location,
3883 "The keyword case or default must precede code in switch block");
3895 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
3897 Location l = (Location) $3;
3898 $$ = new While ((Expression) $4, (Statement) $6, l);
3900 if (RootContext.WarningLevel >= 4){
3901 if ($6 == EmptyStatement.Value)
3902 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3908 : DO embedded_statement
3909 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
3911 Location l = (Location) $5;
3913 $$ = new Do ((Statement) $2, (Expression) $6, l);
3919 opt_for_initializer SEMICOLON _mark_
3921 Block assign_block = new Block (current_block);
3922 current_block = assign_block;
3924 if ($3 is DictionaryEntry){
3925 DictionaryEntry de = (DictionaryEntry) $3;
3927 Expression type = (Expression) de.Key;
3928 ArrayList var_declarators = (ArrayList) de.Value;
3930 foreach (VariableDeclaration decl in var_declarators){
3934 vi = current_block.AddVariable (
3935 type, decl.identifier, current_local_parameters, decl.Location);
3939 Location l = lexer.Location;
3941 if (decl.expression_or_array_initializer is Expression){
3942 expr = (Expression) decl.expression_or_array_initializer;
3943 } else if (decl.expression_or_array_initializer == null) {
3946 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3947 expr = new ArrayCreation (type, "", init, decl.Location);
3950 LocalVariableReference var;
3951 var = new LocalVariableReference (assign_block, decl.identifier, l);
3954 Assign a = new Assign (var, expr, decl.Location);
3956 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3960 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
3961 // This can be referred to as $6 below.
3967 opt_for_condition SEMICOLON
3968 opt_for_iterator CLOSE_PARENS
3971 Location l = (Location) $5;
3973 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
3975 if (RootContext.WarningLevel >= 4){
3976 if ($11 == EmptyStatement.Value)
3977 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3980 current_block.AddStatement (f);
3981 while (current_block.Implicit)
3982 current_block = current_block.Parent;
3984 current_block = current_block.Parent;
3989 : /* empty */ { $$ = EmptyStatement.Value; }
3994 : local_variable_declaration
3995 | statement_expression_list
3999 : /* empty */ { $$ = null; }
4000 | boolean_expression
4004 : /* empty */ { $$ = EmptyStatement.Value; }
4009 : statement_expression_list
4012 statement_expression_list
4013 : statement_expression
4015 // CHANGE: was `null'
4016 Block b = new Block (current_block, Block.Flags.Implicit);
4018 b.AddStatement ((Statement) $1);
4021 | statement_expression_list COMMA statement_expression
4023 Block b = (Block) $1;
4025 b.AddStatement ((Statement) $3);
4031 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4033 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4036 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4037 expression CLOSE_PARENS
4039 Block foreach_block = new Block (current_block);
4040 current_block = foreach_block;
4042 Location l = lexer.Location;
4045 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4049 // Get a writable reference to this read-only variable.
4051 // Note that the $$ here refers to the value of _this_ code block,
4052 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4053 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4060 LocalVariableReference v = (LocalVariableReference) $9;
4061 Location l = (Location) $6;
4064 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4065 current_block.AddStatement (f);
4068 while (current_block.Implicit)
4069 current_block = current_block.Parent;
4071 current_block = current_block.Parent;
4077 | continue_statement
4087 $$ = new Break (lexer.Location);
4092 : CONTINUE SEMICOLON
4094 $$ = new Continue (lexer.Location);
4099 : GOTO IDENTIFIER SEMICOLON
4101 $$ = new Goto (current_block, (string) $2, lexer.Location);
4103 | GOTO CASE constant_expression SEMICOLON
4105 $$ = new GotoCase ((Expression) $3, lexer.Location);
4107 | GOTO DEFAULT SEMICOLON
4109 $$ = new GotoDefault (lexer.Location);
4114 : RETURN opt_expression SEMICOLON
4116 $$ = new Return ((Expression) $2, lexer.Location);
4121 : THROW opt_expression SEMICOLON
4123 $$ = new Throw ((Expression) $2, lexer.Location);
4128 : IDENTIFIER RETURN expression SEMICOLON
4130 string s = (string) $1;
4132 Report.Error (1003, lexer.Location, "; expected");
4135 if (RootContext.Version == LanguageVersion.ISO_1){
4136 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4139 if (iterator_container == null){
4140 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4143 iterator_container.SetYields ();
4144 $$ = new Yield ((Expression) $3, lexer.Location);
4147 | IDENTIFIER RETURN SEMICOLON
4149 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4152 | IDENTIFIER BREAK SEMICOLON
4154 string s = (string) $1;
4156 Report.Error (1003, lexer.Location, "; expected");
4159 if (RootContext.Version == LanguageVersion.ISO_1){
4160 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4163 if (iterator_container == null){
4164 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4167 iterator_container.SetYields ();
4168 $$ = new YieldBreak (lexer.Location);
4179 : TRY block catch_clauses
4183 ArrayList c = (ArrayList)$3;
4184 for (int i = 0; i < c.Count; ++i) {
4185 Catch cc = (Catch) c [i];
4187 if (i != c.Count - 1)
4188 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4195 // Now s contains the list of specific catch clauses
4196 // and g contains the general one.
4198 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4200 | TRY block opt_catch_clauses FINALLY block
4203 ArrayList s = new ArrayList (4);
4204 ArrayList catch_list = (ArrayList) $3;
4206 if (catch_list != null){
4207 foreach (Catch cc in catch_list) {
4215 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4219 Report.Error (1524, lexer.Location, "Expected catch or finally");
4224 : /* empty */ { $$ = null; }
4231 ArrayList l = new ArrayList (4);
4236 | catch_clauses catch_clause
4238 ArrayList l = (ArrayList) $1;
4246 : /* empty */ { $$ = null; }
4251 : CATCH opt_catch_args
4253 Expression type = null;
4257 DictionaryEntry cc = (DictionaryEntry) $2;
4258 type = (Expression) cc.Key;
4259 id = (string) cc.Value;
4262 ArrayList one = new ArrayList (4);
4263 Location loc = lexer.Location;
4265 one.Add (new VariableDeclaration (id, null, loc));
4267 current_block = new Block (current_block);
4268 Block b = declare_local_variables (type, one, loc);
4273 Expression type = null;
4277 DictionaryEntry cc = (DictionaryEntry) $2;
4278 type = (Expression) cc.Key;
4279 id = (string) cc.Value;
4282 while (current_block.Implicit)
4283 current_block = current_block.Parent;
4284 current_block = current_block.Parent;
4288 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4293 : /* empty */ { $$ = null; }
4298 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4300 $$ = new DictionaryEntry ($2, $3);
4307 $$ = new Checked ((Block) $2);
4314 $$ = new Unchecked ((Block) $2);
4321 if (!RootContext.Unsafe){
4322 Report.Error (227, lexer.Location,
4323 "Unsafe code can only be used if --unsafe is used");
4326 $$ = new Unsafe ((Block) $3);
4332 type fixed_pointer_declarators
4335 ArrayList list = (ArrayList) $4;
4336 Expression type = (Expression) $3;
4337 Location l = lexer.Location;
4338 int top = list.Count;
4340 Block assign_block = new Block (current_block);
4341 current_block = assign_block;
4343 for (int i = 0; i < top; i++){
4344 Pair p = (Pair) list [i];
4347 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4359 Location l = (Location) $6;
4361 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4363 if (RootContext.WarningLevel >= 4){
4364 if ($8 == EmptyStatement.Value)
4365 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4368 current_block.AddStatement (f);
4369 while (current_block.Implicit)
4370 current_block = current_block.Parent;
4372 current_block = current_block.Parent;
4376 fixed_pointer_declarators
4377 : fixed_pointer_declarator {
4378 ArrayList declarators = new ArrayList (4);
4380 declarators.Add ($1);
4383 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4385 ArrayList declarators = (ArrayList) $1;
4387 declarators.Add ($3);
4392 fixed_pointer_declarator
4393 : IDENTIFIER ASSIGN expression
4395 $$ = new Pair ($1, $3);
4399 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4405 : LOCK OPEN_PARENS expression CLOSE_PARENS
4411 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4416 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4418 Block assign_block = new Block (current_block);
4419 current_block = assign_block;
4421 if ($3 is DictionaryEntry){
4422 DictionaryEntry de = (DictionaryEntry) $3;
4423 Location l = lexer.Location;
4425 Expression type = (Expression) de.Key;
4426 ArrayList var_declarators = (ArrayList) de.Value;
4428 ArrayList vars = new ArrayList (4);
4430 foreach (VariableDeclaration decl in var_declarators){
4432 LocalInfo vi = current_block.AddVariable (
4433 type, decl.identifier,
4434 current_local_parameters, decl.Location);
4440 if (decl.expression_or_array_initializer is Expression){
4441 expr = (Expression) decl.expression_or_array_initializer;
4443 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4445 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4448 expr = new ArrayCreation (type, "", init, decl.Location);
4451 LocalVariableReference var;
4453 // Get a writable reference to this read-only variable.
4454 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4456 // This is so that it is not a warning on using variables
4459 vars.Add (new DictionaryEntry (var, expr));
4461 // Assign a = new Assign (var, expr, decl.Location);
4462 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4465 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4466 // It can be referred to as $6 below.
4467 $$ = new DictionaryEntry (type, vars);
4474 Using u = new Using ($6, (Statement) $7, (Location) $5);
4475 current_block.AddStatement (u);
4476 while (current_block.Implicit)
4477 current_block = current_block.Parent;
4479 current_block = current_block.Parent;
4483 resource_acquisition
4484 : local_variable_declaration
4488 // Utility rule to save location information
4491 { $$ = lexer.Location; }
4496 // A class used to pass around variable declarations and constants
4498 public class VariableDeclaration {
4499 public string identifier;
4500 public object expression_or_array_initializer;
4501 public Location Location;
4502 public Attributes OptAttributes;
4503 public string DocComment;
4505 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4507 this.identifier = id;
4508 this.expression_or_array_initializer = eoai;
4510 this.OptAttributes = opt_attrs;
4513 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4519 /// Used to pass around interface property information
4521 public class InterfaceAccessorInfo {
4523 public readonly Accessor Get, Set;
4525 public InterfaceAccessorInfo (bool has_get, bool has_set,
4526 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4529 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4531 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4534 Get = new Accessor (null, 0, get_attrs, get_loc);
4536 Set = new Accessor (null, 0, set_attrs, set_loc);
4543 // A class used to hold info about an indexer declarator
4545 public class IndexerDeclaration {
4546 public Expression type;
4547 public MemberName interface_type;
4548 public Parameters param_list;
4550 public IndexerDeclaration (Expression type, MemberName interface_type,
4551 Parameters param_list)
4554 this.interface_type = interface_type;
4555 this.param_list = param_list;
4560 // We use this when we do not have an object in advance that is an IIteratorContainer
4562 public class SimpleIteratorContainer : IIteratorContainer {
4565 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4570 public static SimpleIteratorContainer GetSimple () {
4571 Simple.Yields = false;
4575 public void SetYields () { Yields = true; }
4579 // A class used to hold info about an operator declarator
4581 public class OperatorDeclaration {
4582 public Operator.OpType optype;
4583 public Expression ret_type, arg1type, arg2type;
4584 public string arg1name, arg2name;
4585 public Location location;
4587 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4588 Expression arg1type, string arg1name,
4589 Expression arg2type, string arg2name, Location location)
4592 this.ret_type = ret_type;
4593 this.arg1type = arg1type;
4594 this.arg1name = arg1name;
4595 this.arg2type = arg2type;
4596 this.arg2name = arg2name;
4597 this.location = location;
4602 void Error_ExpectingTypeName (Location l, Expression expr)
4604 if (expr is Invocation){
4605 Report.Error (1002, l, "; expected");
4607 Report.Error (-1, l, "Invalid Type definition");
4611 TypeContainer pop_current_class ()
4613 TypeContainer retval = current_class;
4615 current_class = current_class.Parent;
4616 current_container = current_container.Parent;
4618 if (current_class != current_container) {
4619 if (!(current_class is ClassPart) ||
4620 ((ClassPart) current_class).PartialContainer != current_container)
4621 throw new InternalErrorException ();
4622 } else if (current_container is ClassPart)
4623 current_container = ((ClassPart) current_class).PartialContainer;
4629 // Given the @class_name name, it creates a fully qualified name
4630 // based on the containing declaration space
4633 MakeName (MemberName class_name)
4635 string ns = current_namespace.FullName;
4637 if (current_container.Name == ""){
4639 return new MemberName (new MemberName (ns), class_name);
4643 return new MemberName (current_container.MemberName, class_name);
4647 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4649 Block implicit_block;
4650 ArrayList inits = null;
4653 // We use the `Used' property to check whether statements
4654 // have been added to the current block. If so, we need
4655 // to create another block to contain the new declaration
4656 // otherwise, as an optimization, we use the same block to
4657 // add the declaration.
4659 // FIXME: A further optimization is to check if the statements
4660 // that were added were added as part of the initialization
4661 // below. In which case, no other statements have been executed
4662 // and we might be able to reduce the number of blocks for
4663 // situations like this:
4665 // int j = 1; int k = j + 1;
4667 if (current_block.Used)
4668 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4670 implicit_block = current_block;
4672 foreach (VariableDeclaration decl in variable_declarators){
4674 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4675 if (decl.expression_or_array_initializer != null){
4677 inits = new ArrayList (4);
4684 return implicit_block;
4686 foreach (VariableDeclaration decl in inits){
4690 if (decl.expression_or_array_initializer is Expression){
4691 expr = (Expression) decl.expression_or_array_initializer;
4694 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4696 expr = new ArrayCreation (type, "", init, decl.Location);
4699 LocalVariableReference var;
4700 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4702 assign = new Assign (var, expr, decl.Location);
4704 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4707 return implicit_block;
4710 Block declare_local_constants (Expression type, ArrayList declarators)
4712 Block implicit_block;
4714 if (current_block.Used)
4715 implicit_block = new Block (current_block, Block.Flags.Implicit);
4717 implicit_block = current_block;
4719 foreach (VariableDeclaration decl in declarators){
4720 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4721 current_local_parameters, decl.Location);
4724 return implicit_block;
4727 void CheckAttributeTarget (string a)
4731 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4735 Location l = lexer.Location;
4736 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4742 void CheckUnaryOperator (Operator.OpType op)
4746 case Operator.OpType.LogicalNot:
4747 case Operator.OpType.OnesComplement:
4748 case Operator.OpType.Increment:
4749 case Operator.OpType.Decrement:
4750 case Operator.OpType.True:
4751 case Operator.OpType.False:
4752 case Operator.OpType.Addition:
4753 case Operator.OpType.Subtraction:
4758 Location l = lexer.Location;
4759 Report.Error (1019, l, "Overloadable unary operator expected");
4765 void CheckBinaryOperator (Operator.OpType op)
4769 case Operator.OpType.Addition:
4770 case Operator.OpType.Subtraction:
4771 case Operator.OpType.Multiply:
4772 case Operator.OpType.Division:
4773 case Operator.OpType.Modulus:
4774 case Operator.OpType.BitwiseAnd:
4775 case Operator.OpType.BitwiseOr:
4776 case Operator.OpType.ExclusiveOr:
4777 case Operator.OpType.LeftShift:
4778 case Operator.OpType.RightShift:
4779 case Operator.OpType.Equality:
4780 case Operator.OpType.Inequality:
4781 case Operator.OpType.GreaterThan:
4782 case Operator.OpType.LessThan:
4783 case Operator.OpType.GreaterThanOrEqual:
4784 case Operator.OpType.LessThanOrEqual:
4788 Location l = lexer.Location;
4789 Report.Error (1020, l, "Overloadable binary operator expected");
4795 void syntax_error (Location l, string msg)
4797 Report.Error (1003, l, "Syntax error, " + msg);
4800 void output (string s)
4802 Console.WriteLine (s);
4805 void note (string s)
4807 // Used to put annotations
4812 public Tokenizer Lexer {
4818 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4820 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4821 this.name = file.Name;
4823 current_container = RootContext.Tree.Types;
4824 // TODO: Make RootContext.Tree.Types a PartialContainer.
4825 current_class = current_container;
4826 current_container.NamespaceEntry = current_namespace;
4827 oob_stack = new Stack ();
4828 switch_stack = new Stack ();
4830 lexer = new Tokenizer (reader, file, defines);
4833 public void parse ()
4836 if (yacc_verbose_flag > 1)
4837 yyparse (lexer, new yydebug.yyDebugSimple ());
4840 Tokenizer tokenizer = lexer as Tokenizer;
4841 tokenizer.cleanup ();
4842 } catch (Exception e){
4844 // Removed for production use, use parser verbose to get the output.
4846 // Console.WriteLine (e);
4847 Report.Error (-25, lexer.Location, "Parsing error");
4848 if (yacc_verbose_flag > 0)
4849 Console.WriteLine (e);
4853 void CheckToken (int error, int yyToken, string msg)
4855 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4856 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4859 Report.Error (error, lexer.Location, msg);
4862 void CheckIdentifierToken (int yyToken)
4864 CheckToken (1041, yyToken, "Identifier expected");
4867 string ConsumeStoredComment ()
4869 string s = tmpComment;
4871 Lexer.doc_state = XmlCommentState.Allowed;