typeck2.c revision 107590
1/* Report error messages, build initializers, and perform 2 some front-end optimizations for C++ compiler. 3 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 4 1999, 2000, 2001, 2002 Free Software Foundation, Inc. 5 Hacked by Michael Tiemann (tiemann@cygnus.com) 6 7This file is part of GNU CC. 8 9GNU CC is free software; you can redistribute it and/or modify 10it under the terms of the GNU General Public License as published by 11the Free Software Foundation; either version 2, or (at your option) 12any later version. 13 14GNU CC is distributed in the hope that it will be useful, 15but WITHOUT ANY WARRANTY; without even the implied warranty of 16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17GNU General Public License for more details. 18 19You should have received a copy of the GNU General Public License 20along with GNU CC; see the file COPYING. If not, write to 21the Free Software Foundation, 59 Temple Place - Suite 330, 22Boston, MA 02111-1307, USA. */ 23 24 25/* This file is part of the C++ front end. 26 It contains routines to build C++ expressions given their operands, 27 including computing the types of the result, C and C++ specific error 28 checks, and some optimization. 29 30 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, 31 and to process initializations in declarations (since they work 32 like a strange sort of assignment). */ 33 34#include "config.h" 35#include "system.h" 36#include "tree.h" 37#include "cp-tree.h" 38#include "flags.h" 39#include "toplev.h" 40#include "output.h" 41#include "diagnostic.h" 42 43static tree process_init_constructor PARAMS ((tree, tree, tree *)); 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_CONTEXT (basetype); 54 error ("type `%T' is not a base type for type `%T'", basetype, type); 55 return error_mark_node; 56} 57 58tree 59binfo_or_else (base, type) 60 tree base, type; 61{ 62 tree binfo = lookup_base (type, base, ba_ignore, NULL); 63 64 if (binfo == error_mark_node) 65 return NULL_TREE; 66 else if (!binfo) 67 error_not_base_type (base, type); 68 return binfo; 69} 70 71/* According to ARM $7.1.6, "A `const' object may be initialized, but its 72 value may not be changed thereafter. Thus, we emit hard errors for these, 73 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For 74 example, conversions to references.) */ 75 76void 77readonly_error (arg, string, soft) 78 tree arg; 79 const char *string; 80 int soft; 81{ 82 const char *fmt; 83 void (*fn) PARAMS ((const char *, ...)); 84 85 if (soft) 86 fn = pedwarn; 87 else 88 fn = error; 89 90 if (TREE_CODE (arg) == COMPONENT_REF) 91 { 92 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) 93 fmt = "%s of data-member `%D' in read-only structure"; 94 else 95 fmt = "%s of read-only data-member `%D'"; 96 (*fn) (fmt, string, TREE_OPERAND (arg, 1)); 97 } 98 else if (TREE_CODE (arg) == VAR_DECL) 99 { 100 if (DECL_LANG_SPECIFIC (arg) 101 && DECL_IN_AGGR_P (arg) 102 && !TREE_STATIC (arg)) 103 fmt = "%s of constant field `%D'"; 104 else 105 fmt = "%s of read-only variable `%D'"; 106 (*fn) (fmt, string, arg); 107 } 108 else if (TREE_CODE (arg) == PARM_DECL) 109 (*fn) ("%s of read-only parameter `%D'", string, arg); 110 else if (TREE_CODE (arg) == INDIRECT_REF 111 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE 112 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL 113 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) 114 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0)); 115 else if (TREE_CODE (arg) == RESULT_DECL) 116 (*fn) ("%s of read-only named return value `%D'", string, arg); 117 else if (TREE_CODE (arg) == FUNCTION_DECL) 118 (*fn) ("%s of function `%D'", string, arg); 119 else 120 (*fn) ("%s of read-only location", string); 121} 122 123/* If TYPE has abstract virtual functions, issue an error about trying 124 to create an object of that type. DECL is the object declared, or 125 NULL_TREE if the declaration is unavailable. Returns 1 if an error 126 occurred; zero if all was well. */ 127 128int 129abstract_virtuals_error (decl, type) 130 tree decl; 131 tree type; 132{ 133 tree u; 134 tree tu; 135 136 if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type)) 137 return 0; 138 139 if (!TYPE_SIZE (type)) 140 /* TYPE is being defined, and during that time 141 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */ 142 return 0; 143 144 u = CLASSTYPE_PURE_VIRTUALS (type); 145 if (decl) 146 { 147 if (TREE_CODE (decl) == RESULT_DECL) 148 return 0; 149 150 if (TREE_CODE (decl) == VAR_DECL) 151 error ("cannot declare variable `%D' to be of type `%T'", 152 decl, type); 153 else if (TREE_CODE (decl) == PARM_DECL) 154 error ("cannot declare parameter `%D' to be of type `%T'", 155 decl, type); 156 else if (TREE_CODE (decl) == FIELD_DECL) 157 error ("cannot declare field `%D' to be of type `%T'", 158 decl, type); 159 else if (TREE_CODE (decl) == FUNCTION_DECL 160 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 161 error ("invalid return type for member function `%#D'", decl); 162 else if (TREE_CODE (decl) == FUNCTION_DECL) 163 error ("invalid return type for function `%#D'", decl); 164 } 165 else 166 error ("cannot allocate an object of type `%T'", type); 167 168 /* Only go through this once. */ 169 if (TREE_PURPOSE (u) == NULL_TREE) 170 { 171 TREE_PURPOSE (u) = error_mark_node; 172 173 error (" because the following virtual functions are abstract:"); 174 for (tu = u; tu; tu = TREE_CHAIN (tu)) 175 cp_error_at ("\t%#D", TREE_VALUE (tu)); 176 } 177 else 178 error (" since type `%T' has abstract virtual functions", type); 179 180 return 1; 181} 182 183/* Print an error message for invalid use of an incomplete type. 184 VALUE is the expression that was used (or 0 if that isn't known) 185 and TYPE is the type that was invalid. */ 186 187void 188incomplete_type_error (value, type) 189 tree value; 190 tree type; 191{ 192 int decl = 0; 193 194 /* Avoid duplicate error message. */ 195 if (TREE_CODE (type) == ERROR_MARK) 196 return; 197 198 if (value != 0 && (TREE_CODE (value) == VAR_DECL 199 || TREE_CODE (value) == PARM_DECL 200 || TREE_CODE (value) == FIELD_DECL)) 201 { 202 cp_error_at ("`%D' has incomplete type", value); 203 decl = 1; 204 } 205retry: 206 /* We must print an error message. Be clever about what it says. */ 207 208 switch (TREE_CODE (type)) 209 { 210 case RECORD_TYPE: 211 case UNION_TYPE: 212 case ENUMERAL_TYPE: 213 if (!decl) 214 error ("invalid use of undefined type `%#T'", type); 215 if (!TYPE_TEMPLATE_INFO (type)) 216 cp_error_at ("forward declaration of `%#T'", type); 217 else 218 cp_error_at ("declaration of `%#T'", type); 219 break; 220 221 case VOID_TYPE: 222 error ("invalid use of `%T'", type); 223 break; 224 225 case ARRAY_TYPE: 226 if (TYPE_DOMAIN (type)) 227 { 228 type = TREE_TYPE (type); 229 goto retry; 230 } 231 error ("invalid use of array with unspecified bounds"); 232 break; 233 234 case OFFSET_TYPE: 235 bad_member: 236 error ("invalid use of member (did you forget the `&' ?)"); 237 break; 238 239 case TEMPLATE_TYPE_PARM: 240 error ("invalid use of template type parameter"); 241 break; 242 243 case UNKNOWN_TYPE: 244 if (value && TREE_CODE (value) == COMPONENT_REF) 245 goto bad_member; 246 else if (value && TREE_CODE (value) == ADDR_EXPR) 247 error ("address of overloaded function with no contextual type information"); 248 else if (value && TREE_CODE (value) == OVERLOAD) 249 error ("overloaded function with no contextual type information"); 250 else 251 error ("insufficient contextual information to determine type"); 252 break; 253 254 default: 255 abort (); 256 } 257} 258 259 260/* Perform appropriate conversions on the initial value of a variable, 261 store it in the declaration DECL, 262 and print any error messages that are appropriate. 263 If the init is invalid, store an ERROR_MARK. 264 265 C++: Note that INIT might be a TREE_LIST, which would mean that it is 266 a base class initializer for some aggregate type, hopefully compatible 267 with DECL. If INIT is a single element, and DECL is an aggregate 268 type, we silently convert INIT into a TREE_LIST, allowing a constructor 269 to be called. 270 271 If INIT is a TREE_LIST and there is no constructor, turn INIT 272 into a CONSTRUCTOR and use standard initialization techniques. 273 Perhaps a warning should be generated? 274 275 Returns value of initializer if initialization could not be 276 performed for static variable. In that case, caller must do 277 the storing. */ 278 279tree 280store_init_value (decl, init) 281 tree decl, init; 282{ 283 register tree value, type; 284 285 /* If variable's type was invalidly declared, just ignore it. */ 286 287 type = TREE_TYPE (decl); 288 if (TREE_CODE (type) == ERROR_MARK) 289 return NULL_TREE; 290 291#if 0 292 /* This breaks arrays, and should not have any effect for other decls. */ 293 /* Take care of C++ business up here. */ 294 type = TYPE_MAIN_VARIANT (type); 295#endif 296 297 if (IS_AGGR_TYPE (type)) 298 { 299 if (! TYPE_HAS_TRIVIAL_INIT_REF (type) 300 && TREE_CODE (init) != CONSTRUCTOR) 301 abort (); 302 303 if (TREE_CODE (init) == TREE_LIST) 304 { 305 error ("constructor syntax used, but no constructor declared for type `%T'", type); 306 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init)); 307 } 308#if 0 309 if (TREE_CODE (init) == CONSTRUCTOR) 310 { 311 tree field; 312 313 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */ 314 if (CLASSTYPE_N_BASECLASSES (type)) 315 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl); 316 if (CLASSTYPE_VTBL_PTR (type)) 317 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl); 318 if (TYPE_NEEDS_CONSTRUCTING (type)) 319 { 320 cp_error_at ("initializer list construction invalid for `%D'", decl); 321 error ("due to the presence of a constructor"); 322 } 323 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 324 if (TREE_PRIVATE (field) || TREE_PROTECTED (field)) 325 { 326 cp_error_at ("initializer list construction invalid for `%D'", decl); 327 cp_error_at ("due to non-public access of member `%D'", field); 328 } 329 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field)) 330 if (TREE_PRIVATE (field) || TREE_PROTECTED (field)) 331 { 332 cp_error_at ("initializer list construction invalid for `%D'", decl); 333 cp_error_at ("due to non-public access of member `%D'", field); 334 } 335 } 336#endif 337 } 338 else if (TREE_CODE (init) == TREE_LIST 339 && TREE_TYPE (init) != unknown_type_node) 340 { 341 if (TREE_CODE (decl) == RESULT_DECL) 342 { 343 if (TREE_CHAIN (init)) 344 { 345 warning ("comma expression used to initialize return value"); 346 init = build_compound_expr (init); 347 } 348 else 349 init = TREE_VALUE (init); 350 } 351 else if (TREE_CODE (init) == TREE_LIST 352 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 353 { 354 error ("cannot initialize arrays using this syntax"); 355 return NULL_TREE; 356 } 357 else 358 { 359 /* We get here with code like `int a (2);' */ 360 361 if (TREE_CHAIN (init) != NULL_TREE) 362 { 363 pedwarn ("initializer list being treated as compound expression"); 364 init = build_compound_expr (init); 365 } 366 else 367 init = TREE_VALUE (init); 368 } 369 } 370 371 /* End of special C++ code. */ 372 373 /* We might have already run this bracketed initializer through 374 digest_init. Don't do so again. */ 375 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init) 376 && TREE_TYPE (init) 377 && TYPE_MAIN_VARIANT (TREE_TYPE (init)) == TYPE_MAIN_VARIANT (type)) 378 value = init; 379 else 380 /* Digest the specified initializer into an expression. */ 381 value = digest_init (type, init, (tree *) 0); 382 383 /* Store the expression if valid; else report error. */ 384 385 if (TREE_CODE (value) == ERROR_MARK) 386 ; 387 /* Other code expects that initializers for objects of types that need 388 constructing never make it into DECL_INITIAL, and passes 'init' to 389 build_aggr_init without checking DECL_INITIAL. So just return. */ 390 else if (TYPE_NEEDS_CONSTRUCTING (type)) 391 return value; 392 else if (TREE_STATIC (decl) 393 && (! TREE_CONSTANT (value) 394 || ! initializer_constant_valid_p (value, TREE_TYPE (value)) 395#if 0 396 /* A STATIC PUBLIC int variable doesn't have to be 397 run time inited when doing pic. (mrs) */ 398 /* Since ctors and dtors are the only things that can 399 reference vtables, and they are always written down 400 the vtable definition, we can leave the 401 vtables in initialized data space. 402 However, other initialized data cannot be initialized 403 this way. Instead a global file-level initializer 404 must do the job. */ 405 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl)) 406#endif 407 )) 408 409 return value; 410#if 0 /* No, that's C. jason 9/19/94 */ 411 else 412 { 413 if (pedantic && TREE_CODE (value) == CONSTRUCTOR) 414 { 415 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value)) 416 pedwarn ("ISO C++ forbids non-constant aggregate initializer expressions"); 417 } 418 } 419#endif 420 421 /* Store the VALUE in DECL_INITIAL. If we're building a 422 statement-tree we will actually expand the initialization later 423 when we output this function. */ 424 DECL_INITIAL (decl) = value; 425 return NULL_TREE; 426} 427 428/* Same as store_init_value, but used for known-to-be-valid static 429 initializers. Used to introduce a static initializer even in data 430 structures that may require dynamic initialization. */ 431 432tree 433force_store_init_value (decl, init) 434 tree decl, init; 435{ 436 tree type = TREE_TYPE (decl); 437 int needs_constructing = TYPE_NEEDS_CONSTRUCTING (type); 438 439 TYPE_NEEDS_CONSTRUCTING (type) = 0; 440 441 init = store_init_value (decl, init); 442 if (init) 443 abort (); 444 445 TYPE_NEEDS_CONSTRUCTING (type) = needs_constructing; 446 447 return init; 448} 449 450/* Digest the parser output INIT as an initializer for type TYPE. 451 Return a C expression of type TYPE to represent the initial value. 452 453 If TAIL is nonzero, it points to a variable holding a list of elements 454 of which INIT is the first. We update the list stored there by 455 removing from the head all the elements that we use. 456 Normally this is only one; we use more than one element only if 457 TYPE is an aggregate and INIT is not a constructor. */ 458 459tree 460digest_init (type, init, tail) 461 tree type, init, *tail; 462{ 463 enum tree_code code = TREE_CODE (type); 464 tree element = NULL_TREE; 465 tree old_tail_contents = NULL_TREE; 466 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR 467 tree node which has no TREE_TYPE. */ 468 int raw_constructor; 469 470 /* By default, assume we use one element from a list. 471 We correct this later in the sole case where it is not true. */ 472 473 if (tail) 474 { 475 old_tail_contents = *tail; 476 *tail = TREE_CHAIN (*tail); 477 } 478 479 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST 480 && TREE_VALUE (init) == error_mark_node)) 481 return error_mark_node; 482 483 if (TREE_CODE (init) == ERROR_MARK) 484 /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside 485 a template function. This gets substituted during instantiation. */ 486 return init; 487 488 /* We must strip the outermost array type when completing the type, 489 because the its bounds might be incomplete at the moment. */ 490 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE 491 ? TREE_TYPE (type) : type, NULL_TREE)) 492 return error_mark_node; 493 494 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 495 if (TREE_CODE (init) == NON_LVALUE_EXPR) 496 init = TREE_OPERAND (init, 0); 497 498 if (TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == type) 499 return init; 500 501 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0; 502 503 if (raw_constructor 504 && CONSTRUCTOR_ELTS (init) != 0 505 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0) 506 { 507 element = TREE_VALUE (CONSTRUCTOR_ELTS (init)); 508 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 509 if (element && TREE_CODE (element) == NON_LVALUE_EXPR) 510 element = TREE_OPERAND (element, 0); 511 if (element == error_mark_node) 512 return element; 513 } 514 515 /* Initialization of an array of chars from a string constant 516 optionally enclosed in braces. */ 517 518 if (code == ARRAY_TYPE) 519 { 520 tree typ1; 521 522 if (TREE_CODE (init) == TREE_LIST) 523 { 524 error ("initializing array with parameter list"); 525 return error_mark_node; 526 } 527 528 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 529 if (char_type_p (typ1) 530 && ((init && TREE_CODE (init) == STRING_CST) 531 || (element && TREE_CODE (element) == STRING_CST))) 532 { 533 tree string = element ? element : init; 534 535 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) 536 != char_type_node) 537 && TYPE_PRECISION (typ1) == BITS_PER_UNIT) 538 { 539 error ("char-array initialized from wide string"); 540 return error_mark_node; 541 } 542 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) 543 == char_type_node) 544 && TYPE_PRECISION (typ1) != BITS_PER_UNIT) 545 { 546 error ("int-array initialized from non-wide string"); 547 return error_mark_node; 548 } 549 550 TREE_TYPE (string) = type; 551 if (TYPE_DOMAIN (type) != 0 552 && TREE_CONSTANT (TYPE_SIZE (type))) 553 { 554 register int size 555 = TREE_INT_CST_LOW (TYPE_SIZE (type)); 556 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; 557 /* In C it is ok to subtract 1 from the length of the string 558 because it's ok to ignore the terminating null char that is 559 counted in the length of the constant, but in C++ this would 560 be invalid. */ 561 if (size < TREE_STRING_LENGTH (string)) 562 pedwarn ("initializer-string for array of chars is too long"); 563 } 564 return string; 565 } 566 } 567 568 /* Handle scalar types, including conversions, 569 and signature pointers and references. */ 570 571 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE 572 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE 573 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE 574 || TYPE_PTRMEMFUNC_P (type)) 575 { 576 if (raw_constructor) 577 { 578 if (element == 0) 579 { 580 error ("initializer for scalar variable requires one element"); 581 return error_mark_node; 582 } 583 init = element; 584 } 585 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)) 586 { 587 pedwarn ("braces around scalar initializer for `%T'", type); 588 init = CONSTRUCTOR_ELTS (init); 589 if (TREE_CHAIN (init)) 590 pedwarn ("ignoring extra initializers for `%T'", type); 591 init = TREE_VALUE (init); 592 } 593 594 return convert_for_initialization (0, type, init, LOOKUP_NORMAL, 595 "initialization", NULL_TREE, 0); 596 } 597 598 /* Come here only for records and arrays (and unions with constructors). */ 599 600 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type))) 601 { 602 error ("variable-sized object of type `%T' may not be initialized", 603 type); 604 return error_mark_node; 605 } 606 607 if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code)) 608 { 609 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type) 610 && TREE_HAS_CONSTRUCTOR (init)) 611 { 612 error ("subobject of type `%T' must be initialized by constructor, not by `%E'", 613 type, init); 614 return error_mark_node; 615 } 616 else if (raw_constructor) 617 return process_init_constructor (type, init, (tree *)0); 618 else if (can_convert_arg (type, TREE_TYPE (init), init) 619 || TYPE_NON_AGGREGATE_CLASS (type)) 620 /* These are never initialized from multiple constructor elements. */; 621 else if (tail != 0) 622 { 623 *tail = old_tail_contents; 624 return process_init_constructor (type, 0, tail); 625 } 626 627 if (code != ARRAY_TYPE) 628 { 629 int flags = LOOKUP_NORMAL; 630 /* Initialization from { } is copy-initialization. */ 631 if (tail) 632 flags |= LOOKUP_ONLYCONVERTING; 633 634 return convert_for_initialization (NULL_TREE, type, init, flags, 635 "initialization", NULL_TREE, 0); 636 } 637 } 638 639 error ("invalid initializer"); 640 return error_mark_node; 641} 642 643/* Process a constructor for a variable of type TYPE. 644 The constructor elements may be specified either with INIT or with ELTS, 645 only one of which should be non-null. 646 647 If INIT is specified, it is a CONSTRUCTOR node which is specifically 648 and solely for initializing this datum. 649 650 If ELTS is specified, it is the address of a variable containing 651 a list of expressions. We take as many elements as we need 652 from the head of the list and update the list. 653 654 In the resulting constructor, TREE_CONSTANT is set if all elts are 655 constant, and TREE_STATIC is set if, in addition, all elts are simple enough 656 constants that the assembler and linker can compute them. */ 657 658static tree 659process_init_constructor (type, init, elts) 660 tree type, init, *elts; 661{ 662 register tree tail; 663 /* List of the elements of the result constructor, 664 in reverse order. */ 665 register tree members = NULL; 666 register tree next1; 667 tree result; 668 int allconstant = 1; 669 int allsimple = 1; 670 int erroneous = 0; 671 672 /* Make TAIL be the list of elements to use for the initialization, 673 no matter how the data was given to us. */ 674 675 if (elts) 676 { 677 if (warn_missing_braces) 678 warning ("aggregate has a partly bracketed initializer"); 679 tail = *elts; 680 } 681 else 682 tail = CONSTRUCTOR_ELTS (init); 683 684 /* Gobble as many elements as needed, and make a constructor or initial value 685 for each element of this aggregate. Chain them together in result. 686 If there are too few, use 0 for each scalar ultimate component. */ 687 688 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) 689 { 690 register long len; 691 register int i; 692 693 if (TREE_CODE (type) == ARRAY_TYPE) 694 { 695 tree domain = TYPE_DOMAIN (type); 696 if (domain) 697 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain)) 698 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain)) 699 + 1); 700 else 701 len = -1; /* Take as many as there are */ 702 } 703 else 704 { 705 /* Vectors are like simple fixed-size arrays. */ 706 len = TYPE_VECTOR_SUBPARTS (type); 707 } 708 709 for (i = 0; len < 0 || i < len; i++) 710 { 711 if (tail) 712 { 713 if (TREE_PURPOSE (tail) 714 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST 715 || compare_tree_int (TREE_PURPOSE (tail), i) != 0)) 716 sorry ("non-trivial labeled initializers"); 717 718 if (TREE_VALUE (tail) != 0) 719 { 720 tree tail1 = tail; 721 next1 = digest_init (TREE_TYPE (type), 722 TREE_VALUE (tail), &tail1); 723 if (next1 == error_mark_node) 724 return next1; 725 my_friendly_assert 726 (same_type_ignoring_top_level_qualifiers_p 727 (TREE_TYPE (type), TREE_TYPE (next1)), 728 981123); 729 my_friendly_assert (tail1 == 0 730 || TREE_CODE (tail1) == TREE_LIST, 319); 731 if (tail == tail1 && len < 0) 732 { 733 error ("non-empty initializer for array of empty elements"); 734 /* Just ignore what we were supposed to use. */ 735 tail1 = NULL_TREE; 736 } 737 tail = tail1; 738 } 739 else 740 { 741 next1 = error_mark_node; 742 tail = TREE_CHAIN (tail); 743 } 744 } 745 else if (len < 0) 746 /* We're done. */ 747 break; 748 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))) 749 { 750 /* If this type needs constructors run for 751 default-initialization, we can't rely on the backend to do it 752 for us, so build up TARGET_EXPRs. If the type in question is 753 a class, just build one up; if it's an array, recurse. */ 754 755 if (IS_AGGR_TYPE (TREE_TYPE (type))) 756 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE); 757 else 758 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE); 759 next1 = digest_init (TREE_TYPE (type), next1, 0); 760 } 761 else if (! zero_init_p (TREE_TYPE (type))) 762 next1 = build_forced_zero_init (TREE_TYPE (type)); 763 else 764 /* The default zero-initialization is fine for us; don't 765 add anything to the CONSTRUCTOR. */ 766 break; 767 768 if (next1 == error_mark_node) 769 erroneous = 1; 770 else if (!TREE_CONSTANT (next1)) 771 allconstant = 0; 772 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) 773 allsimple = 0; 774 members = tree_cons (size_int (i), next1, members); 775 } 776 } 777 else if (TREE_CODE (type) == RECORD_TYPE) 778 { 779 register tree field; 780 781 if (tail) 782 { 783 if (TYPE_USES_VIRTUAL_BASECLASSES (type)) 784 { 785 sorry ("initializer list for object of class with virtual base classes"); 786 return error_mark_node; 787 } 788 789 if (TYPE_BINFO_BASETYPES (type)) 790 { 791 sorry ("initializer list for object of class with base classes"); 792 return error_mark_node; 793 } 794 795 if (TYPE_POLYMORPHIC_P (type)) 796 { 797 sorry ("initializer list for object using virtual functions"); 798 return error_mark_node; 799 } 800 } 801 802 for (field = TYPE_FIELDS (type); field; 803 field = TREE_CHAIN (field)) 804 { 805 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field)) 806 { 807 members = tree_cons (field, integer_zero_node, members); 808 continue; 809 } 810 811 if (TREE_CODE (field) != FIELD_DECL) 812 continue; 813 814 if (tail) 815 { 816 if (TREE_PURPOSE (tail) 817 && TREE_PURPOSE (tail) != field 818 && TREE_PURPOSE (tail) != DECL_NAME (field)) 819 sorry ("non-trivial labeled initializers"); 820 821 if (TREE_VALUE (tail) != 0) 822 { 823 tree tail1 = tail; 824 825 next1 = digest_init (TREE_TYPE (field), 826 TREE_VALUE (tail), &tail1); 827 my_friendly_assert (tail1 == 0 828 || TREE_CODE (tail1) == TREE_LIST, 320); 829 tail = tail1; 830 } 831 else 832 { 833 next1 = error_mark_node; 834 tail = TREE_CHAIN (tail); 835 } 836 } 837 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field))) 838 { 839 /* If this type needs constructors run for 840 default-initialization, we can't rely on the backend to do it 841 for us, so build up TARGET_EXPRs. If the type in question is 842 a class, just build one up; if it's an array, recurse. */ 843 844 if (IS_AGGR_TYPE (TREE_TYPE (field))) 845 next1 = build_functional_cast (TREE_TYPE (field), 846 NULL_TREE); 847 else 848 { 849 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, 850 NULL_TREE); 851 if (init) 852 TREE_HAS_CONSTRUCTOR (next1) 853 = TREE_HAS_CONSTRUCTOR (init); 854 } 855 next1 = digest_init (TREE_TYPE (field), next1, 0); 856 857 /* Warn when some struct elements are implicitly initialized. */ 858 if (extra_warnings 859 && (!init || TREE_HAS_CONSTRUCTOR (init))) 860 warning ("missing initializer for member `%D'", field); 861 } 862 else 863 { 864 if (TREE_READONLY (field)) 865 error ("uninitialized const member `%D'", field); 866 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field))) 867 error ("member `%D' with uninitialized const fields", 868 field); 869 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE) 870 error ("member `%D' is uninitialized reference", field); 871 872 /* Warn when some struct elements are implicitly initialized 873 to zero. */ 874 if (extra_warnings 875 && (!init || TREE_HAS_CONSTRUCTOR (init))) 876 warning ("missing initializer for member `%D'", field); 877 878 if (! zero_init_p (TREE_TYPE (field))) 879 next1 = build_forced_zero_init (TREE_TYPE (field)); 880 else 881 /* The default zero-initialization is fine for us; don't 882 add anything to the CONSTRUCTOR. */ 883 continue; 884 } 885 886 if (next1 == error_mark_node) 887 erroneous = 1; 888 else if (!TREE_CONSTANT (next1)) 889 allconstant = 0; 890 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) 891 allsimple = 0; 892 members = tree_cons (field, next1, members); 893 } 894 } 895 else if (TREE_CODE (type) == UNION_TYPE 896 /* If the initializer was empty, use default zero initialization. */ 897 && tail) 898 { 899 register tree field = TYPE_FIELDS (type); 900 901 /* Find the first named field. ANSI decided in September 1990 902 that only named fields count here. */ 903 while (field && (DECL_NAME (field) == 0 904 || TREE_CODE (field) != FIELD_DECL)) 905 field = TREE_CHAIN (field); 906 907 /* If this element specifies a field, initialize via that field. */ 908 if (TREE_PURPOSE (tail) != NULL_TREE) 909 { 910 int win = 0; 911 912 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL) 913 /* Handle the case of a call by build_c_cast. */ 914 field = TREE_PURPOSE (tail), win = 1; 915 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE) 916 error ("index value instead of field name in union initializer"); 917 else 918 { 919 tree temp; 920 for (temp = TYPE_FIELDS (type); 921 temp; 922 temp = TREE_CHAIN (temp)) 923 if (DECL_NAME (temp) == TREE_PURPOSE (tail)) 924 break; 925 if (temp) 926 field = temp, win = 1; 927 else 928 error ("no field `%D' in union being initialized", 929 TREE_PURPOSE (tail)); 930 } 931 if (!win) 932 TREE_VALUE (tail) = error_mark_node; 933 } 934 else if (field == 0) 935 { 936 error ("union `%T' with no named members cannot be initialized", 937 type); 938 TREE_VALUE (tail) = error_mark_node; 939 } 940 941 if (TREE_VALUE (tail) != 0) 942 { 943 tree tail1 = tail; 944 945 next1 = digest_init (TREE_TYPE (field), 946 TREE_VALUE (tail), &tail1); 947 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST) 948 abort (); 949 tail = tail1; 950 } 951 else 952 { 953 next1 = error_mark_node; 954 tail = TREE_CHAIN (tail); 955 } 956 957 if (next1 == error_mark_node) 958 erroneous = 1; 959 else if (!TREE_CONSTANT (next1)) 960 allconstant = 0; 961 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0) 962 allsimple = 0; 963 members = tree_cons (field, next1, members); 964 } 965 966 /* If arguments were specified as a list, just remove the ones we used. */ 967 if (elts) 968 *elts = tail; 969 /* If arguments were specified as a constructor, 970 complain unless we used all the elements of the constructor. */ 971 else if (tail) 972 pedwarn ("excess elements in aggregate initializer"); 973 974 if (erroneous) 975 return error_mark_node; 976 977 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members)); 978 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE) 979 complete_array_type (type, result, /*do_default=*/0); 980 if (init) 981 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init); 982 if (allconstant) TREE_CONSTANT (result) = 1; 983 if (allconstant && allsimple) TREE_STATIC (result) = 1; 984 return result; 985} 986 987/* Given a structure or union value DATUM, construct and return 988 the structure or union component which results from narrowing 989 that value by the type specified in BASETYPE. For example, given the 990 hierarchy 991 992 class L { int ii; }; 993 class A : L { ... }; 994 class B : L { ... }; 995 class C : A, B { ... }; 996 997 and the declaration 998 999 C x; 1000 1001 then the expression 1002 1003 x.A::ii refers to the ii member of the L part of 1004 the A part of the C object named by X. In this case, 1005 DATUM would be x, and BASETYPE would be A. 1006 1007 I used to think that this was nonconformant, that the standard specified 1008 that first we look up ii in A, then convert x to an L& and pull out the 1009 ii part. But in fact, it does say that we convert x to an A&; A here 1010 is known as the "naming class". (jason 2000-12-19) */ 1011 1012tree 1013build_scoped_ref (datum, basetype) 1014 tree datum; 1015 tree basetype; 1016{ 1017 tree ref; 1018 tree binfo; 1019 1020 if (datum == error_mark_node) 1021 return error_mark_node; 1022 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL); 1023 1024 if (binfo == error_mark_node) 1025 return error_mark_node; 1026 if (!binfo) 1027 return error_not_base_type (TREE_TYPE (datum), basetype); 1028 1029 ref = build_unary_op (ADDR_EXPR, datum, 0); 1030 ref = build_base_path (PLUS_EXPR, ref, binfo, 1); 1031 1032 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)"); 1033} 1034 1035/* Build a reference to an object specified by the C++ `->' operator. 1036 Usually this just involves dereferencing the object, but if the 1037 `->' operator is overloaded, then such overloads must be 1038 performed until an object which does not have the `->' operator 1039 overloaded is found. An error is reported when circular pointer 1040 delegation is detected. */ 1041 1042tree 1043build_x_arrow (datum) 1044 tree datum; 1045{ 1046 tree types_memoized = NULL_TREE; 1047 register tree rval = datum; 1048 tree type = TREE_TYPE (rval); 1049 tree last_rval = NULL_TREE; 1050 1051 if (type == error_mark_node) 1052 return error_mark_node; 1053 1054 if (processing_template_decl) 1055 return build_min_nt (ARROW_EXPR, rval); 1056 1057 if (TREE_CODE (rval) == OFFSET_REF) 1058 { 1059 rval = resolve_offset_ref (datum); 1060 type = TREE_TYPE (rval); 1061 } 1062 1063 if (TREE_CODE (type) == REFERENCE_TYPE) 1064 { 1065 rval = convert_from_reference (rval); 1066 type = TREE_TYPE (rval); 1067 } 1068 1069 if (IS_AGGR_TYPE (type)) 1070 { 1071 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval, 1072 NULL_TREE, NULL_TREE))) 1073 { 1074 if (rval == error_mark_node) 1075 return error_mark_node; 1076 1077 if (value_member (TREE_TYPE (rval), types_memoized)) 1078 { 1079 error ("circular pointer delegation detected"); 1080 return error_mark_node; 1081 } 1082 else 1083 { 1084 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval), 1085 types_memoized); 1086 } 1087 last_rval = rval; 1088 } 1089 1090 if (last_rval == NULL_TREE) 1091 { 1092 error ("base operand of `->' has non-pointer type `%T'", type); 1093 return error_mark_node; 1094 } 1095 1096 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE) 1097 last_rval = convert_from_reference (last_rval); 1098 } 1099 else 1100 last_rval = default_conversion (rval); 1101 1102 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE) 1103 return build_indirect_ref (last_rval, NULL); 1104 1105 if (types_memoized) 1106 error ("result of `operator->()' yields non-pointer result"); 1107 else 1108 error ("base operand of `->' is not a pointer"); 1109 return error_mark_node; 1110} 1111 1112/* Make an expression to refer to the COMPONENT field of 1113 structure or union value DATUM. COMPONENT is an arbitrary 1114 expression. DATUM has not already been checked out to be of 1115 aggregate type. 1116 1117 For C++, COMPONENT may be a TREE_LIST. This happens when we must 1118 return an object of member type to a method of the current class, 1119 but there is not yet enough typing information to know which one. 1120 As a special case, if there is only one method by that name, 1121 it is returned. Otherwise we return an expression which other 1122 routines will have to know how to deal with later. */ 1123 1124tree 1125build_m_component_ref (datum, component) 1126 tree datum, component; 1127{ 1128 tree type; 1129 tree objtype; 1130 tree field_type; 1131 int type_quals; 1132 tree binfo; 1133 1134 if (processing_template_decl) 1135 return build_min_nt (DOTSTAR_EXPR, datum, component); 1136 1137 datum = decay_conversion (datum); 1138 1139 if (datum == error_mark_node || component == error_mark_node) 1140 return error_mark_node; 1141 1142 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum)); 1143 1144 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component))) 1145 { 1146 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component))); 1147 field_type = type; 1148 } 1149 else if (TYPE_PTRMEM_P (TREE_TYPE (component))) 1150 { 1151 type = TREE_TYPE (TREE_TYPE (component)); 1152 field_type = TREE_TYPE (type); 1153 1154 /* Compute the type of the field, as described in [expr.ref]. */ 1155 type_quals = TYPE_UNQUALIFIED; 1156 if (TREE_CODE (field_type) == REFERENCE_TYPE) 1157 /* The standard says that the type of the result should be the 1158 type referred to by the reference. But for now, at least, 1159 we do the conversion from reference type later. */ 1160 ; 1161 else 1162 { 1163 type_quals = (cp_type_quals (field_type) 1164 | cp_type_quals (TREE_TYPE (datum))); 1165 1166 /* There's no such thing as a mutable pointer-to-member, so 1167 we don't need to deal with that here like we do in 1168 build_component_ref. */ 1169 field_type = cp_build_qualified_type (field_type, type_quals); 1170 } 1171 } 1172 else 1173 { 1174 error ("`%E' cannot be used as a member pointer, since it is of type `%T'", 1175 component, TREE_TYPE (component)); 1176 return error_mark_node; 1177 } 1178 1179 if (! IS_AGGR_TYPE (objtype)) 1180 { 1181 error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'", 1182 component, datum, objtype); 1183 return error_mark_node; 1184 } 1185 1186 binfo = lookup_base (objtype, TYPE_METHOD_BASETYPE (type), 1187 ba_check, NULL); 1188 if (!binfo) 1189 { 1190 error ("member type `%T::' incompatible with object type `%T'", 1191 TYPE_METHOD_BASETYPE (type), objtype); 1192 return error_mark_node; 1193 } 1194 else if (binfo == error_mark_node) 1195 return error_mark_node; 1196 1197 component = build (OFFSET_REF, field_type, datum, component); 1198 if (TREE_CODE (type) == OFFSET_TYPE) 1199 component = resolve_offset_ref (component); 1200 return component; 1201} 1202 1203/* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ 1204 1205tree 1206build_functional_cast (exp, parms) 1207 tree exp; 1208 tree parms; 1209{ 1210 /* This is either a call to a constructor, 1211 or a C cast in C++'s `functional' notation. */ 1212 tree type; 1213 1214 if (exp == error_mark_node || parms == error_mark_node) 1215 return error_mark_node; 1216 1217 if (TREE_CODE (exp) == IDENTIFIER_NODE) 1218 { 1219 if (IDENTIFIER_HAS_TYPE_VALUE (exp)) 1220 /* Either an enum or an aggregate type. */ 1221 type = IDENTIFIER_TYPE_VALUE (exp); 1222 else 1223 { 1224 type = lookup_name (exp, 1); 1225 if (!type || TREE_CODE (type) != TYPE_DECL) 1226 { 1227 error ("`%T' fails to be a typedef or built-in type", exp); 1228 return error_mark_node; 1229 } 1230 type = TREE_TYPE (type); 1231 } 1232 } 1233 else if (TREE_CODE (exp) == TYPE_DECL) 1234 type = TREE_TYPE (exp); 1235 else 1236 type = exp; 1237 1238 if (processing_template_decl) 1239 return build_min (CAST_EXPR, type, parms); 1240 1241 if (! IS_AGGR_TYPE (type)) 1242 { 1243 /* this must build a C cast */ 1244 if (parms == NULL_TREE) 1245 parms = integer_zero_node; 1246 else 1247 { 1248 if (TREE_CHAIN (parms) != NULL_TREE) 1249 pedwarn ("initializer list being treated as compound expression"); 1250 parms = build_compound_expr (parms); 1251 } 1252 1253 return build_c_cast (type, parms); 1254 } 1255 1256 /* Prepare to evaluate as a call to a constructor. If this expression 1257 is actually used, for example, 1258 1259 return X (arg1, arg2, ...); 1260 1261 then the slot being initialized will be filled in. */ 1262 1263 if (!complete_type_or_else (type, NULL_TREE)) 1264 return error_mark_node; 1265 if (abstract_virtuals_error (NULL_TREE, type)) 1266 return error_mark_node; 1267 1268 if (parms && TREE_CHAIN (parms) == NULL_TREE) 1269 return build_c_cast (type, TREE_VALUE (parms)); 1270 1271 /* We need to zero-initialize POD types. Let's do that for everything 1272 that doesn't need a constructor. */ 1273 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type) 1274 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) 1275 { 1276 exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); 1277 return get_target_expr (exp); 1278 } 1279 1280 exp = build_method_call (NULL_TREE, complete_ctor_identifier, parms, 1281 TYPE_BINFO (type), LOOKUP_NORMAL); 1282 1283 if (exp == error_mark_node) 1284 return error_mark_node; 1285 1286 return build_cplus_new (type, exp); 1287} 1288 1289 1290/* Complain about defining new types in inappropriate places. We give an 1291 exception for C-style casts, to accommodate GNU C stylings. */ 1292 1293void 1294check_for_new_type (string, inptree) 1295 const char *string; 1296 flagged_type_tree inptree; 1297{ 1298 if (inptree.new_type_flag 1299 && (pedantic || strcmp (string, "cast") != 0)) 1300 pedwarn ("ISO C++ forbids defining types within %s", string); 1301} 1302 1303/* Add new exception specifier SPEC, to the LIST we currently have. 1304 If it's already in LIST then do nothing. 1305 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we 1306 know what we're doing. */ 1307 1308tree 1309add_exception_specifier (list, spec, complain) 1310 tree list, spec; 1311 int complain; 1312{ 1313 int ok; 1314 tree core = spec; 1315 int is_ptr; 1316 1317 if (spec == error_mark_node) 1318 return list; 1319 1320 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317); 1321 1322 /* [except.spec] 1, type in an exception specifier shall not be 1323 incomplete, or pointer or ref to incomplete other than pointer 1324 to cv void. */ 1325 is_ptr = TREE_CODE (core) == POINTER_TYPE; 1326 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE) 1327 core = TREE_TYPE (core); 1328 if (complain < 0) 1329 ok = 1; 1330 else if (VOID_TYPE_P (core)) 1331 ok = is_ptr; 1332 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM) 1333 ok = 1; 1334 else if (processing_template_decl) 1335 ok = 1; 1336 else 1337 ok = COMPLETE_TYPE_P (complete_type (core)); 1338 1339 if (ok) 1340 { 1341 tree probe; 1342 1343 for (probe = list; probe; probe = TREE_CHAIN (probe)) 1344 if (same_type_p (TREE_VALUE (probe), spec)) 1345 break; 1346 if (!probe) 1347 { 1348 spec = build_tree_list (NULL_TREE, spec); 1349 TREE_CHAIN (spec) = list; 1350 list = spec; 1351 } 1352 } 1353 else if (complain) 1354 incomplete_type_error (NULL_TREE, core); 1355 return list; 1356} 1357 1358/* Combine the two exceptions specifier lists LIST and ADD, and return 1359 their union. */ 1360 1361tree 1362merge_exception_specifiers (list, add) 1363 tree list, add; 1364{ 1365 if (!list || !add) 1366 return NULL_TREE; 1367 else if (!TREE_VALUE (list)) 1368 return add; 1369 else if (!TREE_VALUE (add)) 1370 return list; 1371 else 1372 { 1373 tree orig_list = list; 1374 1375 for (; add; add = TREE_CHAIN (add)) 1376 { 1377 tree spec = TREE_VALUE (add); 1378 tree probe; 1379 1380 for (probe = orig_list; probe; probe = TREE_CHAIN (probe)) 1381 if (same_type_p (TREE_VALUE (probe), spec)) 1382 break; 1383 if (!probe) 1384 { 1385 spec = build_tree_list (NULL_TREE, spec); 1386 TREE_CHAIN (spec) = list; 1387 list = spec; 1388 } 1389 } 1390 } 1391 return list; 1392} 1393