typeck2.c revision 50397
1/* Report error messages, build initializers, and perform 2 some front-end optimizations for C++ compiler. 3 Copyright (C) 1987, 88, 89, 92, 93, 94, 1995 Free Software Foundation, Inc. 4 Hacked by Michael Tiemann (tiemann@cygnus.com) 5 6This file is part of GNU CC. 7 8GNU CC is free software; you can redistribute it and/or modify 9it under the terms of the GNU General Public License as published by 10the Free Software Foundation; either version 2, or (at your option) 11any later version. 12 13GNU CC is distributed in the hope that it will be useful, 14but WITHOUT ANY WARRANTY; without even the implied warranty of 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16GNU General Public License for more details. 17 18You should have received a copy of the GNU General Public License 19along with GNU CC; see the file COPYING. If not, write to 20the Free Software Foundation, 59 Temple Place - Suite 330, 21Boston, MA 02111-1307, USA. */ 22 23 24/* This file is part of the C++ front end. 25 It contains routines to build C++ expressions given their operands, 26 including computing the types of the result, C and C++ specific error 27 checks, and some optimization. 28 29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, 30 and to process initializations in declarations (since they work 31 like a strange sort of assignment). */ 32 33#include "config.h" 34#include "system.h" 35#include "tree.h" 36#include "cp-tree.h" 37#include "flags.h" 38#include "toplev.h" 39 40static tree process_init_constructor PROTO((tree, tree, tree *)); 41 42extern int errorcount; 43extern int sorrycount; 44 45/* Print an error message stemming from an attempt to use 46 BASETYPE as a base class for TYPE. */ 47 48tree 49error_not_base_type (basetype, type) 50 tree basetype, type; 51{ 52 if (TREE_CODE (basetype) == FUNCTION_DECL) 53 basetype = DECL_CLASS_CONTEXT (basetype); 54 cp_error ("type `%T' is not a base type for type `%T'", basetype, type); 55 return error_mark_node; 56} 57 58tree 59binfo_or_else (parent_or_type, type) 60 tree parent_or_type, type; 61{ 62 tree binfo; 63 if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type)) 64 return TYPE_BINFO (parent_or_type); 65 if ((binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0))) 66 { 67 if (binfo == error_mark_node) 68 return NULL_TREE; 69 return binfo; 70 } 71 error_not_base_type (parent_or_type, type); 72 return NULL_TREE; 73} 74 75/* According to ARM $7.1.6, "A `const' object may be initialized, but its 76 value may not be changed thereafter. Thus, we emit hard errors for these, 77 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For 78 example, conversions to references.) */ 79 80void 81readonly_error (arg, string, soft) 82 tree arg; 83 char *string; 84 int soft; 85{ 86 char *fmt; 87 void (*fn)(); 88 89 if (soft) 90 fn = cp_pedwarn; 91 else 92 fn = cp_error; 93 94 if (TREE_CODE (arg) == COMPONENT_REF) 95 { 96 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) 97 fmt = "%s of member `%D' in read-only structure"; 98 else 99 fmt = "%s of read-only member `%D'"; 100 (*fn) (fmt, string, TREE_OPERAND (arg, 1)); 101 } 102 else if (TREE_CODE (arg) == VAR_DECL) 103 { 104 if (DECL_LANG_SPECIFIC (arg) 105 && DECL_IN_AGGR_P (arg) 106 && !TREE_STATIC (arg)) 107 fmt = "%s of constant field `%D'"; 108 else 109 fmt = "%s of read-only variable `%D'"; 110 (*fn) (fmt, string, arg); 111 } 112 else if (TREE_CODE (arg) == PARM_DECL) 113 (*fn) ("%s of read-only parameter `%D'", string, arg); 114 else if (TREE_CODE (arg) == INDIRECT_REF 115 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE 116 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL 117 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) 118 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0)); 119 else if (TREE_CODE (arg) == RESULT_DECL) 120 (*fn) ("%s of read-only named return value `%D'", string, arg); 121 else 122 (*fn) ("%s of read-only location", string); 123} 124 125/* Print an error message for invalid use of a type which declares 126 virtual functions which are not inheritable. */ 127 128void 129abstract_virtuals_error (decl, type) 130 tree decl; 131 tree type; 132{ 133 tree u = CLASSTYPE_ABSTRACT_VIRTUALS (type); 134 int has_abstract_virtuals, needs_final_overriders; 135 tree tu; 136 137 /* Count how many abstract methods need to be defined. */ 138 for (has_abstract_virtuals = 0, tu = u; tu; tu = TREE_CHAIN (tu)) 139 { 140 if (DECL_ABSTRACT_VIRTUAL_P (TREE_VALUE (tu)) 141 && ! DECL_NEEDS_FINAL_OVERRIDER_P (TREE_VALUE (tu))) 142 { 143 has_abstract_virtuals = 1; 144 break; 145 } 146 } 147 148 /* Count how many virtual methods need a final overrider. */ 149 for (needs_final_overriders = 0, tu = u; tu; tu = TREE_CHAIN (tu)) 150 { 151 if (DECL_NEEDS_FINAL_OVERRIDER_P (TREE_VALUE (tu))) 152 { 153 needs_final_overriders = 1; 154 break; 155 } 156 } 157 158 if (decl) 159 { 160 if (TREE_CODE (decl) == RESULT_DECL) 161 return; 162 163 if (TREE_CODE (decl) == VAR_DECL) 164 cp_error ("cannot declare variable `%D' to be of type `%T'", 165 decl, type); 166 else if (TREE_CODE (decl) == PARM_DECL) 167 cp_error ("cannot declare parameter `%D' to be of type `%T'", 168 decl, type); 169 else if (TREE_CODE (decl) == FIELD_DECL) 170 cp_error ("cannot declare field `%D' to be of type `%T'", 171 decl, type); 172 else if (TREE_CODE (decl) == FUNCTION_DECL 173 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 174 cp_error ("invalid return type for method `%#D'", decl); 175 else if (TREE_CODE (decl) == FUNCTION_DECL) 176 cp_error ("invalid return type for function `%#D'", decl); 177 } 178 else 179 cp_error ("cannot allocate an object of type `%T'", type); 180 181 /* Only go through this once. */ 182 if (TREE_PURPOSE (u) == NULL_TREE) 183 { 184 TREE_PURPOSE (u) = error_mark_node; 185 186 if (has_abstract_virtuals) 187 error (" since the following virtual functions are abstract:"); 188 tu = u; 189 while (tu) 190 { 191 if (DECL_ABSTRACT_VIRTUAL_P (TREE_VALUE (tu)) 192 && ! DECL_NEEDS_FINAL_OVERRIDER_P (TREE_VALUE (tu))) 193 cp_error ("\t%#D", TREE_VALUE (tu)); 194 tu = TREE_CHAIN (tu); 195 } 196 197 if (needs_final_overriders) 198 { 199 if (has_abstract_virtuals) 200 error (" and the following virtual functions need a final overrider:"); 201 else 202 error (" since the following virtual functions need a final overrider:"); 203 } 204 tu = u; 205 while (tu) 206 { 207 if (DECL_NEEDS_FINAL_OVERRIDER_P (TREE_VALUE (tu))) 208 cp_error ("\t%#D", TREE_VALUE (tu)); 209 tu = TREE_CHAIN (tu); 210 } 211 } 212 else 213 { 214 if (has_abstract_virtuals) 215 { 216 if (needs_final_overriders) 217 cp_error (" since type `%T' has abstract virtual functions and must override virtual functions", type); 218 else 219 cp_error (" since type `%T' has abstract virtual functions", type); 220 } 221 else 222 cp_error (" since type `%T' must override virtual functions", type); 223 } 224} 225 226/* Print an error message for invalid use of a signature type. 227 Signatures are treated similar to abstract classes here, they 228 cannot be instantiated. */ 229 230void 231signature_error (decl, type) 232 tree decl; 233 tree type; 234{ 235 if (decl) 236 { 237 if (TREE_CODE (decl) == RESULT_DECL) 238 return; 239 240 if (TREE_CODE (decl) == VAR_DECL) 241 cp_error ("cannot declare variable `%D' to be of signature type `%T'", 242 decl, type); 243 else if (TREE_CODE (decl) == PARM_DECL) 244 cp_error ("cannot declare parameter `%D' to be of signature type `%T'", 245 decl, type); 246 else if (TREE_CODE (decl) == FIELD_DECL) 247 cp_error ("cannot declare field `%D' to be of signature type `%T'", 248 decl, type); 249 else if (TREE_CODE (decl) == FUNCTION_DECL 250 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 251 cp_error ("invalid return type for method `%#D'", decl); 252 else if (TREE_CODE (decl) == FUNCTION_DECL) 253 cp_error ("invalid return type for function `%#D'", decl); 254 } 255 else 256 cp_error ("cannot allocate an object of signature type `%T'", type); 257} 258 259/* Print an error message for invalid use of an incomplete type. 260 VALUE is the expression that was used (or 0 if that isn't known) 261 and TYPE is the type that was invalid. */ 262 263void 264incomplete_type_error (value, type) 265 tree value; 266 tree type; 267{ 268 char *errmsg = 0; 269 270 /* Avoid duplicate error message. */ 271 if (TREE_CODE (type) == ERROR_MARK) 272 return; 273 274 if (value != 0 && (TREE_CODE (value) == VAR_DECL 275 || TREE_CODE (value) == PARM_DECL)) 276 cp_error ("`%D' has incomplete type", value); 277 else 278 { 279 retry: 280 /* We must print an error message. Be clever about what it says. */ 281 282 switch (TREE_CODE (type)) 283 { 284 case RECORD_TYPE: 285 case UNION_TYPE: 286 case ENUMERAL_TYPE: 287 errmsg = "invalid use of undefined type `%#T'"; 288 break; 289 290 case VOID_TYPE: 291 error ("invalid use of void expression"); 292 return; 293 294 case ARRAY_TYPE: 295 if (TYPE_DOMAIN (type)) 296 { 297 type = TREE_TYPE (type); 298 goto retry; 299 } 300 error ("invalid use of array with unspecified bounds"); 301 return; 302 303 case OFFSET_TYPE: 304 error ("invalid use of member type (did you forget the `&' ?)"); 305 return; 306 307 case TEMPLATE_TYPE_PARM: 308 error ("invalid use of template type parameter"); 309 return; 310 311 default: 312 my_friendly_abort (108); 313 } 314 315 cp_error (errmsg, type); 316 } 317} 318 319/* Like error(), but don't call report_error_function(). */ 320 321static void 322ack (s, v, v2) 323 char *s; 324 HOST_WIDE_INT v; 325 HOST_WIDE_INT v2; 326{ 327 extern char * progname; 328 329 if (input_filename) 330 fprintf (stderr, "%s:%d: ", input_filename, lineno); 331 else 332 fprintf (stderr, "%s: ", progname); 333 334 fprintf (stderr, s, v, v2); 335 fprintf (stderr, "\n"); 336} 337 338/* There are times when the compiler can get very confused, confused 339 to the point of giving up by aborting, simply because of previous 340 input errors. It is much better to have the user go back and 341 correct those errors first, and see if it makes us happier, than it 342 is to abort on him. This is because when one has a 10,000 line 343 program, and the compiler comes back with ``core dump'', the user 344 is left not knowing even where to begin to fix things and no place 345 to even try and work around things. 346 347 The parameter is to uniquely identify the problem to the user, so 348 that they can say, I am having problem 59, and know that fix 7 will 349 probably solve their problem. Or, we can document what problem 350 59 is, so they can understand how to work around it, should they 351 ever run into it. 352 353 Note, there will be no more calls in the C++ front end to abort, 354 because the C++ front end is so unreliable still. The C front end 355 can get away with calling abort, because for most of the calls to 356 abort on most machines, it, I suspect, can be proven that it is 357 impossible to ever call abort. The same is not yet true for C++, 358 one day, maybe it will be. 359 360 We used to tell people to "fix the above error[s] and try recompiling 361 the program" via a call to fatal, but that message tended to look 362 silly. So instead, we just do the equivalent of a call to fatal in the 363 same situation (call exit). */ 364 365/* First used: 0 (reserved), Last used: 369. Free: */ 366 367static int abortcount = 0; 368 369void 370my_friendly_abort (i) 371 int i; 372{ 373 /* if the previous error came through here, i.e. report_error_function 374 ended up calling us again, don't just exit; we want a diagnostic of 375 some kind. */ 376 if (abortcount == 1) 377 current_function_decl = NULL_TREE; 378 else if (errorcount > 0 || sorrycount > 0) 379 { 380 if (abortcount > 1) 381 { 382 if (i == 0) 383 ack ("Internal compiler error."); 384 else 385 ack ("Internal compiler error %d.", i); 386 ack ("Please submit a full bug report to `egcs-bugs@egcs.cygnus.com'."); 387 ack ("See <URL:http://egcs.cygnus.com/faq.html#bugreport> for details."); 388 } 389 else 390 error ("confused by earlier errors, bailing out"); 391 392 exit (34); 393 } 394 ++abortcount; 395 396 if (i == 0) 397 error ("Internal compiler error."); 398 else 399 error ("Internal compiler error %d.", i); 400 401 error ("Please submit a full bug report to `egcs-bugs@egcs.cygnus.com'."); 402 fatal ("See <URL:http://egcs.cygnus.com/faq.html#bugreport> for details."); 403} 404 405void 406my_friendly_assert (cond, where) 407 int cond, where; 408{ 409 if (cond == 0) 410 my_friendly_abort (where); 411} 412 413/* Return nonzero if VALUE is a valid constant-valued expression 414 for use in initializing a static variable; one that can be an 415 element of a "constant" initializer. 416 417 Return null_pointer_node if the value is absolute; 418 if it is relocatable, return the variable that determines the relocation. 419 We assume that VALUE has been folded as much as possible; 420 therefore, we do not need to check for such things as 421 arithmetic-combinations of integers. */ 422 423tree 424initializer_constant_valid_p (value, endtype) 425 tree value; 426 tree endtype; 427{ 428 switch (TREE_CODE (value)) 429 { 430 case CONSTRUCTOR: 431 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE 432 && TREE_CONSTANT (value)) 433 return 434 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)), 435 endtype); 436 437 return TREE_STATIC (value) ? null_pointer_node : 0; 438 439 case INTEGER_CST: 440 case REAL_CST: 441 case STRING_CST: 442 case COMPLEX_CST: 443 return null_pointer_node; 444 445 case ADDR_EXPR: 446 return TREE_OPERAND (value, 0); 447 448 case NON_LVALUE_EXPR: 449 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 450 451 case CONVERT_EXPR: 452 case NOP_EXPR: 453 /* Allow conversions between pointer types. */ 454 if (POINTER_TYPE_P (TREE_TYPE (value)) 455 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))) 456 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 457 458 /* Allow conversions between real types. */ 459 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE 460 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE) 461 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 462 463 /* Allow length-preserving conversions between integer types. */ 464 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 465 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE 466 && (TYPE_PRECISION (TREE_TYPE (value)) 467 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))) 468 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 469 470 /* Allow conversions between other integer types only if 471 explicit value. */ 472 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 473 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE) 474 { 475 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0), 476 endtype); 477 if (inner == null_pointer_node) 478 return null_pointer_node; 479 return 0; 480 } 481 482 /* Allow (int) &foo provided int is as wide as a pointer. */ 483 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 484 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE 485 && (TYPE_PRECISION (TREE_TYPE (value)) 486 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))) 487 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 488 endtype); 489 490 /* Likewise conversions from int to pointers, but also allow 491 conversions from 0. */ 492 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE 493 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE) 494 { 495 if (integer_zerop (TREE_OPERAND (value, 0))) 496 return null_pointer_node; 497 else if (TYPE_PRECISION (TREE_TYPE (value)) 498 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))) 499 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 500 endtype); 501 } 502 503 /* Allow conversions to union types if the value inside is okay. */ 504 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE) 505 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 506 endtype); 507 return 0; 508 509 case PLUS_EXPR: 510 if ((TREE_CODE (endtype) == INTEGER_TYPE) 511 && (TYPE_PRECISION (endtype) < POINTER_SIZE)) 512 return 0; 513 { 514 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0), 515 endtype); 516 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1), 517 endtype); 518 /* If either term is absolute, use the other terms relocation. */ 519 if (valid0 == null_pointer_node) 520 return valid1; 521 if (valid1 == null_pointer_node) 522 return valid0; 523 return 0; 524 } 525 526 case MINUS_EXPR: 527 if ((TREE_CODE (endtype) == INTEGER_TYPE) 528 && (TYPE_PRECISION (endtype) < POINTER_SIZE)) 529 return 0; 530 { 531 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0), 532 endtype); 533 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1), 534 endtype); 535 /* Win if second argument is absolute. */ 536 if (valid1 == null_pointer_node) 537 return valid0; 538 /* Win if both arguments have the same relocation. 539 Then the value is absolute. */ 540 if (valid0 == valid1) 541 return null_pointer_node; 542 return 0; 543 } 544 545 default: 546 break; 547 } 548 549 return 0; 550} 551 552/* Perform appropriate conversions on the initial value of a variable, 553 store it in the declaration DECL, 554 and print any error messages that are appropriate. 555 If the init is invalid, store an ERROR_MARK. 556 557 C++: Note that INIT might be a TREE_LIST, which would mean that it is 558 a base class initializer for some aggregate type, hopefully compatible 559 with DECL. If INIT is a single element, and DECL is an aggregate 560 type, we silently convert INIT into a TREE_LIST, allowing a constructor 561 to be called. 562 563 If INIT is a TREE_LIST and there is no constructor, turn INIT 564 into a CONSTRUCTOR and use standard initialization techniques. 565 Perhaps a warning should be generated? 566 567 Returns value of initializer if initialization could not be 568 performed for static variable. In that case, caller must do 569 the storing. */ 570 571tree 572store_init_value (decl, init) 573 tree decl, init; 574{ 575 register tree value, type; 576 577 /* If variable's type was invalidly declared, just ignore it. */ 578 579 type = TREE_TYPE (decl); 580 if (TREE_CODE (type) == ERROR_MARK) 581 return NULL_TREE; 582 583#if 0 584 /* This breaks arrays, and should not have any effect for other decls. */ 585 /* Take care of C++ business up here. */ 586 type = TYPE_MAIN_VARIANT (type); 587#endif 588 589 if (IS_AGGR_TYPE (type)) 590 { 591 if (! TYPE_HAS_TRIVIAL_INIT_REF (type) 592 && TREE_CODE (init) != CONSTRUCTOR) 593 my_friendly_abort (109); 594 595 /* Although we are not allowed to declare variables of signature 596 type, we complain about a possible constructor call in such a 597 declaration as well. */ 598 if (TREE_CODE (init) == TREE_LIST 599 && IS_SIGNATURE (type)) 600 { 601 cp_error ("constructor syntax cannot be used with signature type `%T'", 602 type); 603 init = error_mark_node; 604 } 605 else if (TREE_CODE (init) == TREE_LIST) 606 { 607 cp_error ("constructor syntax used, but no constructor declared for type `%T'", type); 608 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init)); 609 } 610#if 0 611 if (TREE_CODE (init) == CONSTRUCTOR) 612 { 613 tree field; 614 615 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */ 616 if (CLASSTYPE_N_BASECLASSES (type)) 617 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl); 618 if (CLASSTYPE_VTBL_PTR (type)) 619 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl); 620 if (TYPE_NEEDS_CONSTRUCTING (type)) 621 { 622 cp_error_at ("initializer list construction invalid for `%D'", decl); 623 error ("due to the presence of a constructor"); 624 } 625 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 626 if (TREE_PRIVATE (field) || TREE_PROTECTED (field)) 627 { 628 cp_error_at ("initializer list construction invalid for `%D'", decl); 629 cp_error_at ("due to non-public access of member `%D'", field); 630 } 631 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field)) 632 if (TREE_PRIVATE (field) || TREE_PROTECTED (field)) 633 { 634 cp_error_at ("initializer list construction invalid for `%D'", decl); 635 cp_error_at ("due to non-public access of member `%D'", field); 636 } 637 } 638#endif 639 } 640 else if (TREE_CODE (init) == TREE_LIST 641 && TREE_TYPE (init) != unknown_type_node) 642 { 643 if (TREE_CODE (decl) == RESULT_DECL) 644 { 645 if (TREE_CHAIN (init)) 646 { 647 warning ("comma expression used to initialize return value"); 648 init = build_compound_expr (init); 649 } 650 else 651 init = TREE_VALUE (init); 652 } 653 else if (TREE_TYPE (init) != 0 654 && TREE_CODE (TREE_TYPE (init)) == OFFSET_TYPE) 655 { 656 /* Use the type of our variable to instantiate 657 the type of our initializer. */ 658 init = instantiate_type (type, init, 1); 659 } 660 else if (TREE_CODE (init) == TREE_LIST 661 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 662 { 663 error ("cannot initialize arrays using this syntax"); 664 return NULL_TREE; 665 } 666 else 667 { 668 /* We get here with code like `int a (2);' */ 669 670 if (TREE_CHAIN (init) != NULL_TREE) 671 { 672 pedwarn ("initializer list being treated as compound expression"); 673 init = build_compound_expr (init); 674 } 675 else 676 init = TREE_VALUE (init); 677 } 678 } 679 680 /* End of special C++ code. */ 681 682 /* Digest the specified initializer into an expression. */ 683 684 value = digest_init (type, init, (tree *) 0); 685 686 /* Store the expression if valid; else report error. */ 687 688 if (TREE_CODE (value) == ERROR_MARK) 689 ; 690 /* Other code expects that initializers for objects of types that need 691 constructing never make it into DECL_INITIAL, and passes 'init' to 692 expand_aggr_init without checking DECL_INITIAL. So just return. */ 693 else if (TYPE_NEEDS_CONSTRUCTING (type)) 694 return value; 695 else if (TREE_STATIC (decl) 696 && (! TREE_CONSTANT (value) 697 || ! initializer_constant_valid_p (value, TREE_TYPE (value)) 698#if 0 699 /* A STATIC PUBLIC int variable doesn't have to be 700 run time inited when doing pic. (mrs) */ 701 /* Since ctors and dtors are the only things that can 702 reference vtables, and they are always written down 703 the vtable definition, we can leave the 704 vtables in initialized data space. 705 However, other initialized data cannot be initialized 706 this way. Instead a global file-level initializer 707 must do the job. */ 708 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl)) 709#endif 710 )) 711 712 return value; 713#if 0 /* No, that's C. jason 9/19/94 */ 714 else 715 { 716 if (pedantic && TREE_CODE (value) == CONSTRUCTOR 717 /* Don't complain about non-constant initializers of 718 signature tables and signature pointers/references. */ 719 && ! (TYPE_LANG_SPECIFIC (type) 720 && (IS_SIGNATURE (type) 721 || IS_SIGNATURE_POINTER (type) 722 || IS_SIGNATURE_REFERENCE (type)))) 723 { 724 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value)) 725 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions"); 726 } 727 } 728#endif 729 DECL_INITIAL (decl) = value; 730 return NULL_TREE; 731} 732 733/* Digest the parser output INIT as an initializer for type TYPE. 734 Return a C expression of type TYPE to represent the initial value. 735 736 If TAIL is nonzero, it points to a variable holding a list of elements 737 of which INIT is the first. We update the list stored there by 738 removing from the head all the elements that we use. 739 Normally this is only one; we use more than one element only if 740 TYPE is an aggregate and INIT is not a constructor. */ 741 742tree 743digest_init (type, init, tail) 744 tree type, init, *tail; 745{ 746 enum tree_code code = TREE_CODE (type); 747 tree element = NULL_TREE; 748 tree old_tail_contents = NULL_TREE; 749 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR 750 tree node which has no TREE_TYPE. */ 751 int raw_constructor; 752 753 /* By default, assume we use one element from a list. 754 We correct this later in the sole case where it is not true. */ 755 756 if (tail) 757 { 758 old_tail_contents = *tail; 759 *tail = TREE_CHAIN (*tail); 760 } 761 762 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST 763 && TREE_VALUE (init) == error_mark_node)) 764 return error_mark_node; 765 766 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 767 if (TREE_CODE (init) == NON_LVALUE_EXPR) 768 init = TREE_OPERAND (init, 0); 769 770 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0; 771 772 if (raw_constructor 773 && CONSTRUCTOR_ELTS (init) != 0 774 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0) 775 { 776 element = TREE_VALUE (CONSTRUCTOR_ELTS (init)); 777 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 778 if (element && TREE_CODE (element) == NON_LVALUE_EXPR) 779 element = TREE_OPERAND (element, 0); 780 if (element == error_mark_node) 781 return element; 782 } 783 784 /* Initialization of an array of chars from a string constant 785 optionally enclosed in braces. */ 786 787 if (code == ARRAY_TYPE) 788 { 789 tree typ1; 790 791 if (TREE_CODE (init) == TREE_LIST) 792 { 793 error ("initializing array with parameter list"); 794 return error_mark_node; 795 } 796 797 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 798 if ((typ1 == char_type_node 799 || typ1 == signed_char_type_node 800 || typ1 == unsigned_char_type_node 801 || typ1 == unsigned_wchar_type_node 802 || typ1 == signed_wchar_type_node) 803 && ((init && TREE_CODE (init) == STRING_CST) 804 || (element && TREE_CODE (element) == STRING_CST))) 805 { 806 tree string = element ? element : init; 807 808 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) 809 != char_type_node) 810 && TYPE_PRECISION (typ1) == BITS_PER_UNIT) 811 { 812 error ("char-array initialized from wide string"); 813 return error_mark_node; 814 } 815 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) 816 == char_type_node) 817 && TYPE_PRECISION (typ1) != BITS_PER_UNIT) 818 { 819 error ("int-array initialized from non-wide string"); 820 return error_mark_node; 821 } 822 823 TREE_TYPE (string) = type; 824 if (TYPE_DOMAIN (type) != 0 825 && TREE_CONSTANT (TYPE_SIZE (type))) 826 { 827 register int size 828 = TREE_INT_CST_LOW (TYPE_SIZE (type)); 829 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; 830 /* In C it is ok to subtract 1 from the length of the string 831 because it's ok to ignore the terminating null char that is 832 counted in the length of the constant, but in C++ this would 833 be invalid. */ 834 if (size < TREE_STRING_LENGTH (string)) 835 pedwarn ("initializer-string for array of chars is too long"); 836 } 837 return string; 838 } 839 } 840 841 /* Handle scalar types, including conversions, 842 and signature pointers and references. */ 843 844 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE 845 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE 846 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE 847 || TYPE_PTRMEMFUNC_P (type) 848 || (code == RECORD_TYPE && ! raw_constructor 849 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))) 850 { 851 if (raw_constructor) 852 { 853 if (element == 0) 854 { 855 error ("initializer for scalar variable requires one element"); 856 return error_mark_node; 857 } 858 init = element; 859 } 860 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)) 861 { 862 cp_pedwarn ("braces around scalar initializer for `%T'", type); 863 init = CONSTRUCTOR_ELTS (init); 864 if (TREE_CHAIN (init)) 865 cp_pedwarn ("ignoring extra initializers for `%T'", type); 866 init = TREE_VALUE (init); 867 } 868 869 return convert_for_initialization (0, type, init, LOOKUP_NORMAL, 870 "initialization", NULL_TREE, 0); 871 } 872 873 /* Come here only for records and arrays (and unions with constructors). */ 874 875 if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type))) 876 { 877 cp_error ("variable-sized object of type `%T' may not be initialized", 878 type); 879 return error_mark_node; 880 } 881 882 if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE) 883 { 884 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type)) 885 { 886 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'", 887 type, init); 888 return error_mark_node; 889 } 890 else if (raw_constructor) 891 return process_init_constructor (type, init, (tree *)0); 892 else if (can_convert_arg (type, TREE_TYPE (init), init) 893 || TYPE_NON_AGGREGATE_CLASS (type)) 894 /* These are never initialized from multiple constructor elements. */; 895 else if (tail != 0) 896 { 897 *tail = old_tail_contents; 898 return process_init_constructor (type, 0, tail); 899 } 900 901 if (code != ARRAY_TYPE) 902 { 903 int flags = LOOKUP_NORMAL; 904 /* Initialization from { } is copy-initialization. */ 905 if (tail) 906 flags |= LOOKUP_ONLYCONVERTING; 907 908 return convert_for_initialization (NULL_TREE, type, init, flags, 909 "initialization", NULL_TREE, 0); 910 } 911 } 912 913 error ("invalid initializer"); 914 return error_mark_node; 915} 916 917/* Process a constructor for a variable of type TYPE. 918 The constructor elements may be specified either with INIT or with ELTS, 919 only one of which should be non-null. 920 921 If INIT is specified, it is a CONSTRUCTOR node which is specifically 922 and solely for initializing this datum. 923 924 If ELTS is specified, it is the address of a variable containing 925 a list of expressions. We take as many elements as we need 926 from the head of the list and update the list. 927 928 In the resulting constructor, TREE_CONSTANT is set if all elts are 929 constant, and TREE_STATIC is set if, in addition, all elts are simple enough 930 constants that the assembler and linker can compute them. */ 931 932static tree 933process_init_constructor (type, init, elts) 934 tree type, init, *elts; 935{ 936 register tree tail; 937 /* List of the elements of the result constructor, 938 in reverse order. */ 939 register tree members = NULL; 940 tree result; 941 int allconstant = 1; 942 int allsimple = 1; 943 int erroneous = 0; 944 945 /* Make TAIL be the list of elements to use for the initialization, 946 no matter how the data was given to us. */ 947 948 if (elts) 949 { 950 if (warn_missing_braces) 951 warning ("aggregate has a partly bracketed initializer"); 952 tail = *elts; 953 } 954 else 955 tail = CONSTRUCTOR_ELTS (init); 956 957 /* Gobble as many elements as needed, and make a constructor or initial value 958 for each element of this aggregate. Chain them together in result. 959 If there are too few, use 0 for each scalar ultimate component. */ 960 961 if (TREE_CODE (type) == ARRAY_TYPE) 962 { 963 tree domain = TYPE_DOMAIN (type); 964 register long len; 965 register int i; 966 967 if (domain) 968 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain)) 969 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain)) 970 + 1); 971 else 972 len = -1; /* Take as many as there are */ 973 974 for (i = 0; (len < 0 || i < len) && tail != 0; i++) 975 { 976 register tree next1; 977 978 if (TREE_PURPOSE (tail) 979 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST 980 || TREE_INT_CST_LOW (TREE_PURPOSE (tail)) != i)) 981 sorry ("non-trivial labeled initializers"); 982 983 if (TREE_VALUE (tail) != 0) 984 { 985 tree tail1 = tail; 986 next1 = digest_init (TREE_TYPE (type), 987 TREE_VALUE (tail), &tail1); 988 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)) 989 && TYPE_MAIN_VARIANT (TREE_TYPE (type)) != TYPE_MAIN_VARIANT (TREE_TYPE (next1))) 990 { 991 /* The fact this needs to be done suggests this code needs 992 to be totally rewritten. */ 993 next1 = convert_for_initialization (NULL_TREE, TREE_TYPE (type), next1, LOOKUP_NORMAL, "initialization", NULL_TREE, 0); 994 } 995 my_friendly_assert (tail1 == 0 996 || TREE_CODE (tail1) == TREE_LIST, 319); 997 if (tail == tail1 && len < 0) 998 { 999 error ("non-empty initializer for array of empty elements"); 1000 /* Just ignore what we were supposed to use. */ 1001 tail1 = NULL_TREE; 1002 } 1003 tail = tail1; 1004 } 1005 else 1006 { 1007 next1 = error_mark_node; 1008 tail = TREE_CHAIN (tail); 1009 } 1010 1011 if (next1 == error_mark_node) 1012 erroneous = 1; 1013 else if (!TREE_CONSTANT (next1)) 1014 allconstant = 0; 1015 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) 1016 allsimple = 0; 1017 members = expr_tree_cons (NULL_TREE, next1, members); 1018 } 1019 } 1020 if (TREE_CODE (type) == RECORD_TYPE) 1021 { 1022 register tree field; 1023 1024 if (tail) 1025 { 1026 if (TYPE_USES_VIRTUAL_BASECLASSES (type)) 1027 { 1028 sorry ("initializer list for object of class with virtual baseclasses"); 1029 return error_mark_node; 1030 } 1031 1032 if (TYPE_BINFO_BASETYPES (type)) 1033 { 1034 sorry ("initializer list for object of class with baseclasses"); 1035 return error_mark_node; 1036 } 1037 1038 if (TYPE_VIRTUAL_P (type)) 1039 { 1040 sorry ("initializer list for object using virtual functions"); 1041 return error_mark_node; 1042 } 1043 } 1044 1045 for (field = TYPE_FIELDS (type); field && tail; 1046 field = TREE_CHAIN (field)) 1047 { 1048 register tree next1; 1049 1050 if (! DECL_NAME (field)) 1051 { 1052 members = expr_tree_cons (field, integer_zero_node, members); 1053 continue; 1054 } 1055 1056 if (TREE_CODE (field) != FIELD_DECL) 1057 continue; 1058 1059 if (TREE_PURPOSE (tail) 1060 && TREE_PURPOSE (tail) != field 1061 && TREE_PURPOSE (tail) != DECL_NAME (field)) 1062 sorry ("non-trivial labeled initializers"); 1063 1064 if (TREE_VALUE (tail) != 0) 1065 { 1066 tree tail1 = tail; 1067 1068 next1 = digest_init (TREE_TYPE (field), 1069 TREE_VALUE (tail), &tail1); 1070 my_friendly_assert (tail1 == 0 1071 || TREE_CODE (tail1) == TREE_LIST, 320); 1072 tail = tail1; 1073 } 1074 else 1075 { 1076 next1 = error_mark_node; 1077 tail = TREE_CHAIN (tail); 1078 } 1079 1080 if (next1 == error_mark_node) 1081 erroneous = 1; 1082 else if (!TREE_CONSTANT (next1)) 1083 allconstant = 0; 1084 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) 1085 allsimple = 0; 1086 members = expr_tree_cons (field, next1, members); 1087 } 1088 for (; field; field = TREE_CHAIN (field)) 1089 { 1090 if (TREE_CODE (field) != FIELD_DECL) 1091 continue; 1092 1093 /* Does this field have a default initialization? */ 1094 if (DECL_INITIAL (field)) 1095 { 1096 register tree next1 = DECL_INITIAL (field); 1097 if (TREE_CODE (next1) == ERROR_MARK) 1098 erroneous = 1; 1099 else if (!TREE_CONSTANT (next1)) 1100 allconstant = 0; 1101 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) 1102 allsimple = 0; 1103 members = expr_tree_cons (field, next1, members); 1104 } 1105 else if (TREE_READONLY (field)) 1106 error ("uninitialized const member `%s'", 1107 IDENTIFIER_POINTER (DECL_NAME (field))); 1108 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field)) 1109 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field))) 1110 error ("member `%s' with uninitialized const fields", 1111 IDENTIFIER_POINTER (DECL_NAME (field))); 1112 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE) 1113 error ("member `%s' is uninitialized reference", 1114 IDENTIFIER_POINTER (DECL_NAME (field))); 1115 /* Warn when some struct elements are implicitly initialized 1116 to zero. */ 1117 else if (extra_warnings) 1118 warning ("missing initializer for member `%s'", 1119 IDENTIFIER_POINTER (DECL_NAME (field))); 1120 } 1121 } 1122 1123 if (TREE_CODE (type) == UNION_TYPE) 1124 { 1125 register tree field = TYPE_FIELDS (type); 1126 register tree next1; 1127 1128 /* Find the first named field. ANSI decided in September 1990 1129 that only named fields count here. */ 1130 while (field && (DECL_NAME (field) == 0 1131 || TREE_CODE (field) != FIELD_DECL)) 1132 field = TREE_CHAIN (field); 1133 1134 /* If this element specifies a field, initialize via that field. */ 1135 if (TREE_PURPOSE (tail) != NULL_TREE) 1136 { 1137 int win = 0; 1138 1139 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL) 1140 /* Handle the case of a call by build_c_cast. */ 1141 field = TREE_PURPOSE (tail), win = 1; 1142 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE) 1143 error ("index value instead of field name in union initializer"); 1144 else 1145 { 1146 tree temp; 1147 for (temp = TYPE_FIELDS (type); 1148 temp; 1149 temp = TREE_CHAIN (temp)) 1150 if (DECL_NAME (temp) == TREE_PURPOSE (tail)) 1151 break; 1152 if (temp) 1153 field = temp, win = 1; 1154 else 1155 error ("no field `%s' in union being initialized", 1156 IDENTIFIER_POINTER (TREE_PURPOSE (tail))); 1157 } 1158 if (!win) 1159 TREE_VALUE (tail) = error_mark_node; 1160 } 1161 else if (field == 0) 1162 { 1163 cp_error ("union `%T' with no named members cannot be initialized", 1164 type); 1165 TREE_VALUE (tail) = error_mark_node; 1166 } 1167 1168 if (TREE_VALUE (tail) != 0) 1169 { 1170 tree tail1 = tail; 1171 1172 next1 = digest_init (TREE_TYPE (field), 1173 TREE_VALUE (tail), &tail1); 1174 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST) 1175 my_friendly_abort (357); 1176 tail = tail1; 1177 } 1178 else 1179 { 1180 next1 = error_mark_node; 1181 tail = TREE_CHAIN (tail); 1182 } 1183 1184 if (next1 == error_mark_node) 1185 erroneous = 1; 1186 else if (!TREE_CONSTANT (next1)) 1187 allconstant = 0; 1188 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0) 1189 allsimple = 0; 1190 members = expr_tree_cons (field, next1, members); 1191 } 1192 1193 /* If arguments were specified as a list, just remove the ones we used. */ 1194 if (elts) 1195 *elts = tail; 1196 /* If arguments were specified as a constructor, 1197 complain unless we used all the elements of the constructor. */ 1198 else if (tail) 1199 pedwarn ("excess elements in aggregate initializer"); 1200 1201 if (erroneous) 1202 return error_mark_node; 1203 1204 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members)); 1205 if (init) 1206 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init); 1207 if (allconstant) TREE_CONSTANT (result) = 1; 1208 if (allconstant && allsimple) TREE_STATIC (result) = 1; 1209 return result; 1210} 1211 1212/* Given a structure or union value DATUM, construct and return 1213 the structure or union component which results from narrowing 1214 that value by the type specified in BASETYPE. For example, given the 1215 hierarchy 1216 1217 class L { int ii; }; 1218 class A : L { ... }; 1219 class B : L { ... }; 1220 class C : A, B { ... }; 1221 1222 and the declaration 1223 1224 C x; 1225 1226 then the expression 1227 1228 x.A::ii refers to the ii member of the L part of 1229 the A part of the C object named by X. In this case, 1230 DATUM would be x, and BASETYPE would be A. */ 1231 1232tree 1233build_scoped_ref (datum, basetype) 1234 tree datum; 1235 tree basetype; 1236{ 1237 tree ref; 1238 tree type = TREE_TYPE (datum); 1239 1240 if (datum == error_mark_node) 1241 return error_mark_node; 1242 1243 if (TREE_CODE (type) == REFERENCE_TYPE) 1244 type = TREE_TYPE (type); 1245 1246 type = TYPE_MAIN_VARIANT (type); 1247 1248 /* This is an easy conversion. */ 1249 if (is_aggr_type (basetype, 1)) 1250 { 1251 tree binfo = TYPE_BINFO (basetype); 1252 if (binfo != TYPE_BINFO (type)) 1253 { 1254 binfo = get_binfo (binfo, type, 1); 1255 if (binfo == error_mark_node) 1256 return error_mark_node; 1257 if (binfo == 0) 1258 return error_not_base_type (basetype, type); 1259 } 1260 1261 switch (TREE_CODE (datum)) 1262 { 1263 case NOP_EXPR: 1264 case CONVERT_EXPR: 1265 case FLOAT_EXPR: 1266 case FIX_TRUNC_EXPR: 1267 case FIX_FLOOR_EXPR: 1268 case FIX_ROUND_EXPR: 1269 case FIX_CEIL_EXPR: 1270 ref = convert_pointer_to (binfo, 1271 build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0)); 1272 break; 1273 default: 1274 ref = convert_pointer_to (binfo, 1275 build_unary_op (ADDR_EXPR, datum, 0)); 1276 } 1277 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)"); 1278 } 1279 return error_mark_node; 1280} 1281 1282/* Build a reference to an object specified by the C++ `->' operator. 1283 Usually this just involves dereferencing the object, but if the 1284 `->' operator is overloaded, then such overloads must be 1285 performed until an object which does not have the `->' operator 1286 overloaded is found. An error is reported when circular pointer 1287 delegation is detected. */ 1288 1289tree 1290build_x_arrow (datum) 1291 tree datum; 1292{ 1293 tree types_memoized = NULL_TREE; 1294 register tree rval = datum; 1295 tree type = TREE_TYPE (rval); 1296 tree last_rval = NULL_TREE; 1297 1298 if (type == error_mark_node) 1299 return error_mark_node; 1300 1301 if (processing_template_decl) 1302 return build_min_nt (ARROW_EXPR, rval); 1303 1304 if (TREE_CODE (rval) == OFFSET_REF) 1305 { 1306 rval = resolve_offset_ref (datum); 1307 type = TREE_TYPE (rval); 1308 } 1309 1310 if (TREE_CODE (type) == REFERENCE_TYPE) 1311 { 1312 rval = convert_from_reference (rval); 1313 type = TREE_TYPE (rval); 1314 } 1315 1316 if (IS_AGGR_TYPE (type)) 1317 { 1318 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval, 1319 NULL_TREE, NULL_TREE))) 1320 { 1321 if (rval == error_mark_node) 1322 return error_mark_node; 1323 1324 if (value_member (TREE_TYPE (rval), types_memoized)) 1325 { 1326 error ("circular pointer delegation detected"); 1327 return error_mark_node; 1328 } 1329 else 1330 { 1331 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval), 1332 types_memoized); 1333 } 1334 last_rval = rval; 1335 } 1336 1337 if (last_rval == NULL_TREE) 1338 { 1339 cp_error ("base operand of `->' has non-pointer type `%T'", type); 1340 return error_mark_node; 1341 } 1342 1343 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE) 1344 last_rval = convert_from_reference (last_rval); 1345 } 1346 else 1347 last_rval = default_conversion (rval); 1348 1349 /* Signature pointers are not dereferenced. */ 1350 if (TYPE_LANG_SPECIFIC (TREE_TYPE (last_rval)) 1351 && IS_SIGNATURE_POINTER (TREE_TYPE (last_rval))) 1352 return last_rval; 1353 1354 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE) 1355 return build_indirect_ref (last_rval, NULL_PTR); 1356 1357 if (types_memoized) 1358 error ("result of `operator->()' yields non-pointer result"); 1359 else 1360 error ("base operand of `->' is not a pointer"); 1361 return error_mark_node; 1362} 1363 1364/* Make an expression to refer to the COMPONENT field of 1365 structure or union value DATUM. COMPONENT is an arbitrary 1366 expression. DATUM has not already been checked out to be of 1367 aggregate type. 1368 1369 For C++, COMPONENT may be a TREE_LIST. This happens when we must 1370 return an object of member type to a method of the current class, 1371 but there is not yet enough typing information to know which one. 1372 As a special case, if there is only one method by that name, 1373 it is returned. Otherwise we return an expression which other 1374 routines will have to know how to deal with later. */ 1375 1376tree 1377build_m_component_ref (datum, component) 1378 tree datum, component; 1379{ 1380 tree type; 1381 tree objtype = TREE_TYPE (datum); 1382 tree rettype; 1383 tree binfo; 1384 1385 if (processing_template_decl) 1386 return build_min_nt (DOTSTAR_EXPR, datum, component); 1387 1388 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component))) 1389 { 1390 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component))); 1391 rettype = type; 1392 } 1393 else 1394 { 1395 component = build_indirect_ref (component, NULL_PTR); 1396 type = TREE_TYPE (component); 1397 rettype = TREE_TYPE (type); 1398 } 1399 1400 if (datum == error_mark_node || component == error_mark_node) 1401 return error_mark_node; 1402 1403 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE) 1404 { 1405 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type); 1406 return error_mark_node; 1407 } 1408 1409 if (TREE_CODE (objtype) == REFERENCE_TYPE) 1410 objtype = TREE_TYPE (objtype); 1411 objtype = TYPE_MAIN_VARIANT (objtype); 1412 1413 if (! IS_AGGR_TYPE (objtype)) 1414 { 1415 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum); 1416 cp_error ("which is of non-aggregate type `%T'", objtype); 1417 return error_mark_node; 1418 } 1419 1420 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1); 1421 if (binfo == NULL_TREE) 1422 { 1423 cp_error ("member type `%T::' incompatible with object type `%T'", 1424 TYPE_METHOD_BASETYPE (type), objtype); 1425 return error_mark_node; 1426 } 1427 else if (binfo == error_mark_node) 1428 return error_mark_node; 1429 1430 component = build (OFFSET_REF, rettype, datum, component); 1431 if (TREE_CODE (type) == OFFSET_TYPE) 1432 component = resolve_offset_ref (component); 1433 return component; 1434} 1435 1436/* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ 1437 1438tree 1439build_functional_cast (exp, parms) 1440 tree exp; 1441 tree parms; 1442{ 1443 /* This is either a call to a constructor, 1444 or a C cast in C++'s `functional' notation. */ 1445 tree type; 1446 1447 if (exp == error_mark_node || parms == error_mark_node) 1448 return error_mark_node; 1449 1450 if (TREE_CODE (exp) == IDENTIFIER_NODE) 1451 { 1452 if (IDENTIFIER_HAS_TYPE_VALUE (exp)) 1453 /* Either an enum or an aggregate type. */ 1454 type = IDENTIFIER_TYPE_VALUE (exp); 1455 else 1456 { 1457 type = lookup_name (exp, 1); 1458 if (!type || TREE_CODE (type) != TYPE_DECL) 1459 { 1460 cp_error ("`%T' fails to be a typedef or built-in type", exp); 1461 return error_mark_node; 1462 } 1463 type = TREE_TYPE (type); 1464 } 1465 } 1466 else if (TREE_CODE (exp) == TYPE_DECL) 1467 type = TREE_TYPE (exp); 1468 else 1469 type = exp; 1470 1471 if (processing_template_decl) 1472 return build_min (CAST_EXPR, type, parms); 1473 1474 if (IS_SIGNATURE (type)) 1475 { 1476 error ("signature type not allowed in cast or constructor expression"); 1477 return error_mark_node; 1478 } 1479 1480 if (! IS_AGGR_TYPE (type)) 1481 { 1482 /* this must build a C cast */ 1483 if (parms == NULL_TREE) 1484 parms = integer_zero_node; 1485 else 1486 { 1487 if (TREE_CHAIN (parms) != NULL_TREE) 1488 pedwarn ("initializer list being treated as compound expression"); 1489 parms = build_compound_expr (parms); 1490 } 1491 1492 return build_c_cast (type, parms); 1493 } 1494 1495 /* Prepare to evaluate as a call to a constructor. If this expression 1496 is actually used, for example, 1497 1498 return X (arg1, arg2, ...); 1499 1500 then the slot being initialized will be filled in. */ 1501 1502 if (TYPE_SIZE (complete_type (type)) == NULL_TREE) 1503 { 1504 cp_error ("type `%T' is not yet defined", type); 1505 return error_mark_node; 1506 } 1507 1508 if (parms && TREE_CHAIN (parms) == NULL_TREE) 1509 return build_c_cast (type, TREE_VALUE (parms)); 1510 1511 /* We need to zero-initialize POD types. Let's do that for everything 1512 that doesn't need a constructor. */ 1513 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type) 1514 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) 1515 { 1516 exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); 1517 return get_target_expr (exp); 1518 } 1519 1520 exp = build_method_call (NULL_TREE, ctor_identifier, parms, 1521 TYPE_BINFO (type), LOOKUP_NORMAL); 1522 1523 if (exp == error_mark_node) 1524 return error_mark_node; 1525 1526 return build_cplus_new (type, exp); 1527} 1528 1529/* Return the character string for the name that encodes the 1530 enumeral value VALUE in the domain TYPE. */ 1531 1532char * 1533enum_name_string (value, type) 1534 tree value; 1535 tree type; 1536{ 1537 register tree values = TYPE_VALUES (type); 1538 register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value); 1539 1540 my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324); 1541 while (values 1542 && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval) 1543 values = TREE_CHAIN (values); 1544 if (values == NULL_TREE) 1545 { 1546 char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type)); 1547 1548 /* Value must have been cast. */ 1549 sprintf (buf, "(enum %s)%ld", 1550 TYPE_NAME_STRING (type), (long) intval); 1551 return buf; 1552 } 1553 return IDENTIFIER_POINTER (TREE_PURPOSE (values)); 1554} 1555 1556#if 0 1557/* Print out a language-specific error message for 1558 (Pascal) case or (C) switch statements. 1559 CODE tells what sort of message to print. 1560 TYPE is the type of the switch index expression. 1561 NEW is the new value that we were trying to add. 1562 OLD is the old value that stopped us from adding it. */ 1563 1564void 1565report_case_error (code, type, new_value, old_value) 1566 int code; 1567 tree type; 1568 tree new_value, old_value; 1569{ 1570 if (code == 1) 1571 { 1572 if (new_value) 1573 error ("case label not within a switch statement"); 1574 else 1575 error ("default label not within a switch statement"); 1576 } 1577 else if (code == 2) 1578 { 1579 if (new_value == 0) 1580 { 1581 error ("multiple default labels in one switch"); 1582 return; 1583 } 1584 if (TREE_CODE (new_value) == RANGE_EXPR) 1585 if (TREE_CODE (old_value) == RANGE_EXPR) 1586 { 1587 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type))); 1588 if (TREE_CODE (type) == ENUMERAL_TYPE) 1589 sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression", 1590 enum_name_string (TREE_OPERAND (new_value, 0), type), 1591 enum_name_string (TREE_OPERAND (new_value, 1), type), 1592 enum_name_string (TREE_OPERAND (old_value, 0), type), 1593 enum_name_string (TREE_OPERAND (old_value, 1), type)); 1594 else 1595 sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression", 1596 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)), 1597 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)), 1598 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)), 1599 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1))); 1600 error (buf); 1601 } 1602 else 1603 { 1604 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type))); 1605 if (TREE_CODE (type) == ENUMERAL_TYPE) 1606 sprintf (buf, "range [%s..%s] includes element `%s' in case expression", 1607 enum_name_string (TREE_OPERAND (new_value, 0), type), 1608 enum_name_string (TREE_OPERAND (new_value, 1), type), 1609 enum_name_string (old_value, type)); 1610 else 1611 sprintf (buf, "range [%d..%d] includes (%d) in case expression", 1612 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)), 1613 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)), 1614 TREE_INT_CST_LOW (old_value)); 1615 error (buf); 1616 } 1617 else if (TREE_CODE (old_value) == RANGE_EXPR) 1618 { 1619 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type))); 1620 if (TREE_CODE (type) == ENUMERAL_TYPE) 1621 sprintf (buf, "range [%s..%s] includes element `%s' in case expression", 1622 enum_name_string (TREE_OPERAND (old_value, 0), type), 1623 enum_name_string (TREE_OPERAND (old_value, 1), type), 1624 enum_name_string (new_value, type)); 1625 else 1626 sprintf (buf, "range [%d..%d] includes (%d) in case expression", 1627 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)), 1628 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)), 1629 TREE_INT_CST_LOW (new_value)); 1630 error (buf); 1631 } 1632 else 1633 { 1634 if (TREE_CODE (type) == ENUMERAL_TYPE) 1635 error ("duplicate label `%s' in switch statement", 1636 enum_name_string (new_value, type)); 1637 else 1638 error ("duplicate label (%d) in switch statement", 1639 TREE_INT_CST_LOW (new_value)); 1640 } 1641 } 1642 else if (code == 3) 1643 { 1644 if (TREE_CODE (type) == ENUMERAL_TYPE) 1645 warning ("case value out of range for enum %s", 1646 TYPE_NAME_STRING (type)); 1647 else 1648 warning ("case value out of range"); 1649 } 1650 else if (code == 4) 1651 { 1652 if (TREE_CODE (type) == ENUMERAL_TYPE) 1653 error ("range values `%s' and `%s' reversed", 1654 enum_name_string (new_value, type), 1655 enum_name_string (old_value, type)); 1656 else 1657 error ("range values reversed"); 1658 } 1659} 1660#endif 1661 1662void 1663check_for_new_type (string, inptree) 1664 char *string; 1665 flagged_type_tree inptree; 1666{ 1667 if (pedantic && inptree.new_type_flag) 1668 pedwarn ("ANSI C++ forbids defining types within %s",string); 1669} 1670