typeck2.c revision 146895
1259698Sdim/* Report error messages, build initializers, and perform 2259698Sdim some front-end optimizations for C++ compiler. 3259698Sdim Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 4259698Sdim 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc. 5259698Sdim Hacked by Michael Tiemann (tiemann@cygnus.com) 6259698Sdim 7259698SdimThis file is part of GCC. 8259698Sdim 9259698SdimGCC is free software; you can redistribute it and/or modify 10259698Sdimit under the terms of the GNU General Public License as published by 11259698Sdimthe Free Software Foundation; either version 2, or (at your option) 12259698Sdimany later version. 13259698Sdim 14259698SdimGCC is distributed in the hope that it will be useful, 15259698Sdimbut WITHOUT ANY WARRANTY; without even the implied warranty of 16259698SdimMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17259698SdimGNU General Public License for more details. 18259698Sdim 19259698SdimYou should have received a copy of the GNU General Public License 20259698Sdimalong with GCC; see the file COPYING. If not, write to 21259698Sdimthe Free Software Foundation, 59 Temple Place - Suite 330, 22259698SdimBoston, MA 02111-1307, USA. */ 23259698Sdim 24259698Sdim 25259698Sdim/* This file is part of the C++ front end. 26259698Sdim It contains routines to build C++ expressions given their operands, 27259698Sdim including computing the types of the result, C and C++ specific error 28259698Sdim checks, and some optimization. 29259698Sdim 30259698Sdim There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, 31259698Sdim and to process initializations in declarations (since they work 32259698Sdim like a strange sort of assignment). */ 33259698Sdim 34259698Sdim#include "config.h" 35259698Sdim#include "system.h" 36259698Sdim#include "coretypes.h" 37259698Sdim#include "tm.h" 38259698Sdim#include "tree.h" 39259698Sdim#include "cp-tree.h" 40259698Sdim#include "flags.h" 41259698Sdim#include "toplev.h" 42259698Sdim#include "output.h" 43259698Sdim#include "diagnostic.h" 44259698Sdim 45259698Sdimstatic tree process_init_constructor (tree, tree, tree *); 46259698Sdim 47259698Sdim/* Print an error message stemming from an attempt to use 48259698Sdim BASETYPE as a base class for TYPE. */ 49259698Sdim 50259698Sdimtree 51259698Sdimerror_not_base_type (tree basetype, tree type) 52259698Sdim{ 53259698Sdim if (TREE_CODE (basetype) == FUNCTION_DECL) 54259698Sdim basetype = DECL_CONTEXT (basetype); 55259698Sdim error ("type `%T' is not a base type for type `%T'", basetype, type); 56259698Sdim return error_mark_node; 57259698Sdim} 58259698Sdim 59259698Sdimtree 60259698Sdimbinfo_or_else (tree base, tree type) 61259698Sdim{ 62259698Sdim tree binfo = lookup_base (type, base, ba_ignore, NULL); 63259698Sdim 64259698Sdim if (binfo == error_mark_node) 65259698Sdim return NULL_TREE; 66259698Sdim else if (!binfo) 67259698Sdim error_not_base_type (base, type); 68259698Sdim return binfo; 69259698Sdim} 70259698Sdim 71259698Sdim/* According to ARM $7.1.6, "A `const' object may be initialized, but its 72259698Sdim value may not be changed thereafter. Thus, we emit hard errors for these, 73259698Sdim rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For 74259698Sdim example, conversions to references.) */ 75259698Sdim 76259698Sdimvoid 77259698Sdimreadonly_error (tree arg, const char* string, int soft) 78259698Sdim{ 79259698Sdim const char *fmt; 80259698Sdim void (*fn) (const char *, ...); 81259698Sdim 82259698Sdim if (soft) 83259698Sdim fn = pedwarn; 84259698Sdim else 85259698Sdim fn = error; 86259698Sdim 87259698Sdim if (TREE_CODE (arg) == COMPONENT_REF) 88259698Sdim { 89259698Sdim if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) 90259698Sdim fmt = "%s of data-member `%D' in read-only structure"; 91259698Sdim else 92259698Sdim fmt = "%s of read-only data-member `%D'"; 93259698Sdim (*fn) (fmt, string, TREE_OPERAND (arg, 1)); 94259698Sdim } 95259698Sdim else if (TREE_CODE (arg) == VAR_DECL) 96259698Sdim { 97259698Sdim if (DECL_LANG_SPECIFIC (arg) 98259698Sdim && DECL_IN_AGGR_P (arg) 99259698Sdim && !TREE_STATIC (arg)) 100259698Sdim fmt = "%s of constant field `%D'"; 101259698Sdim else 102259698Sdim fmt = "%s of read-only variable `%D'"; 103259698Sdim (*fn) (fmt, string, arg); 104259698Sdim } 105259698Sdim else if (TREE_CODE (arg) == PARM_DECL) 106259698Sdim (*fn) ("%s of read-only parameter `%D'", string, arg); 107259698Sdim else if (TREE_CODE (arg) == INDIRECT_REF 108259698Sdim && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE 109259698Sdim && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL 110259698Sdim || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) 111259698Sdim (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0)); 112259698Sdim else if (TREE_CODE (arg) == RESULT_DECL) 113259698Sdim (*fn) ("%s of read-only named return value `%D'", string, arg); 114259698Sdim else if (TREE_CODE (arg) == FUNCTION_DECL) 115259698Sdim (*fn) ("%s of function `%D'", string, arg); 116259698Sdim else 117259698Sdim (*fn) ("%s of read-only location", string); 118259698Sdim} 119259698Sdim 120259698Sdim/* If TYPE has abstract virtual functions, issue an error about trying 121259698Sdim to create an object of that type. DECL is the object declared, or 122259698Sdim NULL_TREE if the declaration is unavailable. Returns 1 if an error 123259698Sdim occurred; zero if all was well. */ 124259698Sdim 125259698Sdimint 126259698Sdimabstract_virtuals_error (tree decl, tree type) 127259698Sdim{ 128259698Sdim tree u; 129259698Sdim tree tu; 130259698Sdim 131259698Sdim if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type)) 132259698Sdim return 0; 133259698Sdim 134259698Sdim if (!TYPE_SIZE (type)) 135259698Sdim /* TYPE is being defined, and during that time 136259698Sdim CLASSTYPE_PURE_VIRTUALS holds the inline friends. */ 137259698Sdim return 0; 138259698Sdim 139259698Sdim if (dependent_type_p (type)) 140259698Sdim /* For a dependent type, we do not yet know which functions are pure 141259698Sdim virtuals. */ 142259698Sdim return 0; 143259698Sdim 144259698Sdim u = CLASSTYPE_PURE_VIRTUALS (type); 145259698Sdim if (decl) 146259698Sdim { 147259698Sdim if (TREE_CODE (decl) == RESULT_DECL) 148259698Sdim return 0; 149259698Sdim 150259698Sdim if (TREE_CODE (decl) == VAR_DECL) 151259698Sdim error ("cannot declare variable `%D' to be of type `%T'", 152259698Sdim decl, type); 153259698Sdim else if (TREE_CODE (decl) == PARM_DECL) 154259698Sdim error ("cannot declare parameter `%D' to be of type `%T'", 155259698Sdim decl, type); 156259698Sdim else if (TREE_CODE (decl) == FIELD_DECL) 157259698Sdim error ("cannot declare field `%D' to be of type `%T'", 158259698Sdim decl, type); 159259698Sdim else if (TREE_CODE (decl) == FUNCTION_DECL 160259698Sdim && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 161259698Sdim error ("invalid return type for member function `%#D'", decl); 162259698Sdim else if (TREE_CODE (decl) == FUNCTION_DECL) 163259698Sdim error ("invalid return type for function `%#D'", decl); 164259698Sdim } 165259698Sdim else 166259698Sdim error ("cannot allocate an object of type `%T'", type); 167259698Sdim 168259698Sdim /* Only go through this once. */ 169259698Sdim if (TREE_PURPOSE (u) == NULL_TREE) 170259698Sdim { 171259698Sdim TREE_PURPOSE (u) = error_mark_node; 172259698Sdim 173259698Sdim error (" because the following virtual functions are abstract:"); 174259698Sdim for (tu = u; tu; tu = TREE_CHAIN (tu)) 175259698Sdim cp_error_at ("\t%#D", TREE_VALUE (tu)); 176259698Sdim } 177259698Sdim else 178259698Sdim error (" since type `%T' has abstract virtual functions", type); 179259698Sdim 180259698Sdim return 1; 181259698Sdim} 182259698Sdim 183259698Sdim/* Print an error message for invalid use of an incomplete type. 184259698Sdim VALUE is the expression that was used (or 0 if that isn't known) 185259698Sdim and TYPE is the type that was invalid. DIAG_TYPE indicates the 186259698Sdim type of diagnostic: 0 for an error, 1 for a warning, 2 for a 187259698Sdim pedwarn. */ 188259698Sdim 189259698Sdimvoid 190259698Sdimcxx_incomplete_type_diagnostic (tree value, tree type, int diag_type) 191259698Sdim{ 192259698Sdim int decl = 0; 193259698Sdim void (*p_msg) (const char *, ...); 194259698Sdim void (*p_msg_at) (const char *, ...); 195259698Sdim 196259698Sdim if (diag_type == 1) 197259698Sdim { 198259698Sdim p_msg = warning; 199259698Sdim p_msg_at = cp_warning_at; 200259698Sdim } 201259698Sdim else if (diag_type == 2) 202259698Sdim { 203259698Sdim p_msg = pedwarn; 204259698Sdim p_msg_at = cp_pedwarn_at; 205 } 206 else 207 { 208 p_msg = error; 209 p_msg_at = cp_error_at; 210 } 211 212 /* Avoid duplicate error message. */ 213 if (TREE_CODE (type) == ERROR_MARK) 214 return; 215 216 if (value != 0 && (TREE_CODE (value) == VAR_DECL 217 || TREE_CODE (value) == PARM_DECL 218 || TREE_CODE (value) == FIELD_DECL)) 219 { 220 (*p_msg_at) ("`%D' has incomplete type", value); 221 decl = 1; 222 } 223retry: 224 /* We must print an error message. Be clever about what it says. */ 225 226 switch (TREE_CODE (type)) 227 { 228 case RECORD_TYPE: 229 case UNION_TYPE: 230 case ENUMERAL_TYPE: 231 if (!decl) 232 (*p_msg) ("invalid use of undefined type `%#T'", type); 233 if (!TYPE_TEMPLATE_INFO (type)) 234 (*p_msg_at) ("forward declaration of `%#T'", type); 235 else 236 (*p_msg_at) ("declaration of `%#T'", type); 237 break; 238 239 case VOID_TYPE: 240 (*p_msg) ("invalid use of `%T'", type); 241 break; 242 243 case ARRAY_TYPE: 244 if (TYPE_DOMAIN (type)) 245 { 246 type = TREE_TYPE (type); 247 goto retry; 248 } 249 (*p_msg) ("invalid use of array with unspecified bounds"); 250 break; 251 252 case OFFSET_TYPE: 253 bad_member: 254 (*p_msg) ("invalid use of member (did you forget the `&' ?)"); 255 break; 256 257 case TEMPLATE_TYPE_PARM: 258 (*p_msg) ("invalid use of template type parameter"); 259 break; 260 261 case UNKNOWN_TYPE: 262 if (value && TREE_CODE (value) == COMPONENT_REF) 263 goto bad_member; 264 else if (value && TREE_CODE (value) == ADDR_EXPR) 265 (*p_msg) ("address of overloaded function with no contextual type information"); 266 else if (value && TREE_CODE (value) == OVERLOAD) 267 (*p_msg) ("overloaded function with no contextual type information"); 268 else 269 (*p_msg) ("insufficient contextual information to determine type"); 270 break; 271 272 default: 273 abort (); 274 } 275} 276 277/* Backward-compatibility interface to incomplete_type_diagnostic; 278 required by ../tree.c. */ 279#undef cxx_incomplete_type_error 280void 281cxx_incomplete_type_error (tree value, tree type) 282{ 283 cxx_incomplete_type_diagnostic (value, type, 0); 284} 285 286 287/* The recursive part of split_nonconstant_init. DEST is an lvalue 288 expression to which INIT should be assigned. INIT is a CONSTRUCTOR. 289 PCODE is a pointer to the tail of a chain of statements being emitted. 290 The return value is the new tail of that chain after new statements 291 are generated. */ 292 293static tree * 294split_nonconstant_init_1 (tree dest, tree init, tree *pcode) 295{ 296 tree *pelt, elt, type = TREE_TYPE (dest); 297 tree sub, code, inner_type = NULL; 298 bool array_type_p = false; 299 300 pelt = &CONSTRUCTOR_ELTS (init); 301 switch (TREE_CODE (type)) 302 { 303 case ARRAY_TYPE: 304 inner_type = TREE_TYPE (type); 305 array_type_p = true; 306 /* FALLTHRU */ 307 308 case RECORD_TYPE: 309 case UNION_TYPE: 310 case QUAL_UNION_TYPE: 311 while ((elt = *pelt)) 312 { 313 tree field_index = TREE_PURPOSE (elt); 314 tree value = TREE_VALUE (elt); 315 316 if (!array_type_p) 317 inner_type = TREE_TYPE (field_index); 318 319 if (TREE_CODE (value) == CONSTRUCTOR) 320 { 321 if (array_type_p) 322 sub = build (ARRAY_REF, inner_type, dest, field_index); 323 else 324 sub = build (COMPONENT_REF, inner_type, dest, field_index); 325 326 pcode = split_nonconstant_init_1 (sub, value, pcode); 327 } 328 else if (!initializer_constant_valid_p (value, inner_type)) 329 { 330 *pelt = TREE_CHAIN (elt); 331 332 if (array_type_p) 333 sub = build (ARRAY_REF, inner_type, dest, field_index); 334 else 335 sub = build (COMPONENT_REF, inner_type, dest, field_index); 336 337 code = build (MODIFY_EXPR, inner_type, sub, value); 338 code = build_stmt (EXPR_STMT, code); 339 340 *pcode = code; 341 pcode = &TREE_CHAIN (code); 342 continue; 343 } 344 pelt = &TREE_CHAIN (elt); 345 } 346 break; 347 348 case VECTOR_TYPE: 349 if (!initializer_constant_valid_p (init, type)) 350 { 351 tree cons = copy_node (init); 352 CONSTRUCTOR_ELTS (init) = NULL; 353 code = build (MODIFY_EXPR, type, dest, cons); 354 code = build_stmt (EXPR_STMT, code); 355 *pcode = code; 356 pcode = &TREE_CHAIN (code); 357 } 358 break; 359 360 default: 361 abort (); 362 } 363 364 return pcode; 365} 366 367/* A subroutine of store_init_value. Splits non-constant static 368 initializer INIT into a constant part and generates code to 369 perform the non-constant part of the initialization to DEST. 370 Returns the code for the runtime init. */ 371 372static tree 373split_nonconstant_init (tree dest, tree init) 374{ 375 tree code; 376 377 if (TREE_CODE (init) == CONSTRUCTOR) 378 { 379 code = build_stmt (COMPOUND_STMT, NULL_TREE); 380 split_nonconstant_init_1 (dest, init, &COMPOUND_BODY (code)); 381 code = build1 (STMT_EXPR, void_type_node, code); 382 TREE_SIDE_EFFECTS (code) = 1; 383 DECL_INITIAL (dest) = init; 384 TREE_READONLY (dest) = 0; 385 } 386 else 387 code = build (INIT_EXPR, TREE_TYPE (dest), dest, init); 388 389 return code; 390} 391 392/* Perform appropriate conversions on the initial value of a variable, 393 store it in the declaration DECL, 394 and print any error messages that are appropriate. 395 If the init is invalid, store an ERROR_MARK. 396 397 C++: Note that INIT might be a TREE_LIST, which would mean that it is 398 a base class initializer for some aggregate type, hopefully compatible 399 with DECL. If INIT is a single element, and DECL is an aggregate 400 type, we silently convert INIT into a TREE_LIST, allowing a constructor 401 to be called. 402 403 If INIT is a TREE_LIST and there is no constructor, turn INIT 404 into a CONSTRUCTOR and use standard initialization techniques. 405 Perhaps a warning should be generated? 406 407 Returns code to be executed if initialization could not be performed 408 for static variable. In that case, caller must emit the code. */ 409 410tree 411store_init_value (tree decl, tree init) 412{ 413 tree value, type; 414 415 /* If variable's type was invalidly declared, just ignore it. */ 416 417 type = TREE_TYPE (decl); 418 if (TREE_CODE (type) == ERROR_MARK) 419 return NULL_TREE; 420 421 if (IS_AGGR_TYPE (type)) 422 { 423 if (! TYPE_HAS_TRIVIAL_INIT_REF (type) 424 && TREE_CODE (init) != CONSTRUCTOR) 425 abort (); 426 427 if (TREE_CODE (init) == TREE_LIST) 428 { 429 error ("constructor syntax used, but no constructor declared for type `%T'", type); 430 init = build_constructor (NULL_TREE, nreverse (init)); 431 } 432 } 433 else if (TREE_CODE (init) == TREE_LIST 434 && TREE_TYPE (init) != unknown_type_node) 435 { 436 if (TREE_CODE (decl) == RESULT_DECL) 437 init = build_x_compound_expr_from_list (init, 438 "return value initializer"); 439 else if (TREE_CODE (init) == TREE_LIST 440 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 441 { 442 error ("cannot initialize arrays using this syntax"); 443 return NULL_TREE; 444 } 445 else 446 /* We get here with code like `int a (2);' */ 447 init = build_x_compound_expr_from_list (init, "initializer"); 448 } 449 450 /* Digest the specified initializer into an expression. */ 451 value = digest_init (type, init, (tree *) 0); 452 /* If the initializer is not a constant, fill in DECL_INITIAL with 453 the bits that are constant, and then return an expression that 454 will perform the dynamic initialization. */ 455 if (value != error_mark_node 456 && (! TREE_CONSTANT (value) 457 || ! initializer_constant_valid_p (value, TREE_TYPE (value)))) 458 return split_nonconstant_init (decl, value); 459 /* If the value is a constant, just put it in DECL_INITIAL. If DECL 460 is an automatic variable, the middle end will turn this into a 461 dynamic initialization later. */ 462 DECL_INITIAL (decl) = value; 463 return NULL_TREE; 464} 465 466 467/* Digest the parser output INIT as an initializer for type TYPE. 468 Return a C expression of type TYPE to represent the initial value. 469 470 If TAIL is nonzero, it points to a variable holding a list of elements 471 of which INIT is the first. We update the list stored there by 472 removing from the head all the elements that we use. 473 Normally this is only one; we use more than one element only if 474 TYPE is an aggregate and INIT is not a constructor. */ 475 476tree 477digest_init (tree type, tree init, tree* tail) 478{ 479 enum tree_code code = TREE_CODE (type); 480 tree element = NULL_TREE; 481 tree old_tail_contents = NULL_TREE; 482 /* Nonzero if INIT is a braced grouping. */ 483 int raw_constructor; 484 485 /* By default, assume we use one element from a list. 486 We correct this later in the sole case where it is not true. */ 487 488 if (tail) 489 { 490 old_tail_contents = *tail; 491 *tail = TREE_CHAIN (*tail); 492 } 493 494 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST 495 && TREE_VALUE (init) == error_mark_node)) 496 return error_mark_node; 497 498 if (TREE_CODE (init) == ERROR_MARK) 499 /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside 500 a template function. This gets substituted during instantiation. */ 501 return init; 502 503 /* We must strip the outermost array type when completing the type, 504 because the its bounds might be incomplete at the moment. */ 505 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE 506 ? TREE_TYPE (type) : type, NULL_TREE)) 507 return error_mark_node; 508 509 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 510 if (TREE_CODE (init) == NON_LVALUE_EXPR) 511 init = TREE_OPERAND (init, 0); 512 513 raw_constructor = (TREE_CODE (init) == CONSTRUCTOR 514 && TREE_HAS_CONSTRUCTOR (init)); 515 516 if (raw_constructor 517 && CONSTRUCTOR_ELTS (init) != 0 518 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0) 519 { 520 element = TREE_VALUE (CONSTRUCTOR_ELTS (init)); 521 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 522 if (element && TREE_CODE (element) == NON_LVALUE_EXPR) 523 element = TREE_OPERAND (element, 0); 524 if (element == error_mark_node) 525 return element; 526 } 527 528 /* Initialization of an array of chars from a string constant 529 optionally enclosed in braces. */ 530 531 if (code == ARRAY_TYPE) 532 { 533 tree typ1; 534 535 if (TREE_CODE (init) == TREE_LIST) 536 { 537 error ("initializing array with parameter list"); 538 return error_mark_node; 539 } 540 541 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 542 if (char_type_p (typ1) 543 && ((init && TREE_CODE (init) == STRING_CST) 544 || (element && TREE_CODE (element) == STRING_CST))) 545 { 546 tree string = element ? element : init; 547 548 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) 549 != char_type_node) 550 && TYPE_PRECISION (typ1) == BITS_PER_UNIT) 551 { 552 error ("char-array initialized from wide string"); 553 return error_mark_node; 554 } 555 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) 556 == char_type_node) 557 && TYPE_PRECISION (typ1) != BITS_PER_UNIT) 558 { 559 error ("int-array initialized from non-wide string"); 560 return error_mark_node; 561 } 562 563 TREE_TYPE (string) = type; 564 if (TYPE_DOMAIN (type) != 0 565 && TREE_CONSTANT (TYPE_SIZE (type))) 566 { 567 int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); 568 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; 569 /* In C it is ok to subtract 1 from the length of the string 570 because it's ok to ignore the terminating null char that is 571 counted in the length of the constant, but in C++ this would 572 be invalid. */ 573 if (size < TREE_STRING_LENGTH (string)) 574 pedwarn ("initializer-string for array of chars is too long"); 575 } 576 return string; 577 } 578 } 579 580 /* Handle scalar types, including conversions, 581 and signature pointers and references. */ 582 583 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE 584 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE 585 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE 586 || TYPE_PTR_TO_MEMBER_P (type)) 587 { 588 if (raw_constructor) 589 { 590 if (element == 0) 591 { 592 error ("initializer for scalar variable requires one element"); 593 return error_mark_node; 594 } 595 init = element; 596 } 597 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)) 598 { 599 pedwarn ("braces around scalar initializer for `%T'", type); 600 init = CONSTRUCTOR_ELTS (init); 601 if (TREE_CHAIN (init)) 602 pedwarn ("ignoring extra initializers for `%T'", type); 603 init = TREE_VALUE (init); 604 } 605 606 return convert_for_initialization (0, type, init, LOOKUP_NORMAL, 607 "initialization", NULL_TREE, 0); 608 } 609 610 /* Come here only for records and arrays (and unions with constructors). */ 611 612 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type))) 613 { 614 error ("variable-sized object of type `%T' may not be initialized", 615 type); 616 return error_mark_node; 617 } 618 619 if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code)) 620 { 621 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type) 622 && TREE_HAS_CONSTRUCTOR (init)) 623 { 624 error ("subobject of type `%T' must be initialized by constructor, not by `%E'", 625 type, init); 626 return error_mark_node; 627 } 628 else if (raw_constructor) 629 return process_init_constructor (type, init, (tree *)0); 630 else if (can_convert_arg (type, TREE_TYPE (init), init) 631 || TYPE_NON_AGGREGATE_CLASS (type)) 632 /* These are never initialized from multiple constructor elements. */; 633 else if (tail != 0) 634 { 635 *tail = old_tail_contents; 636 return process_init_constructor (type, 0, tail); 637 } 638 639 if (code != ARRAY_TYPE) 640 { 641 int flags = LOOKUP_NORMAL; 642 /* Initialization from { } is copy-initialization. */ 643 if (tail) 644 flags |= LOOKUP_ONLYCONVERTING; 645 646 return convert_for_initialization (NULL_TREE, type, init, flags, 647 "initialization", NULL_TREE, 0); 648 } 649 } 650 651 error ("invalid initializer"); 652 return error_mark_node; 653} 654 655/* Process a constructor for a variable of type TYPE. 656 The constructor elements may be specified either with INIT or with ELTS, 657 only one of which should be non-null. 658 659 If INIT is specified, it is a CONSTRUCTOR node which is specifically 660 and solely for initializing this datum. 661 662 If ELTS is specified, it is the address of a variable containing 663 a list of expressions. We take as many elements as we need 664 from the head of the list and update the list. 665 666 In the resulting constructor, TREE_CONSTANT is set if all elts are 667 constant, and TREE_STATIC is set if, in addition, all elts are simple enough 668 constants that the assembler and linker can compute them. */ 669 670static tree 671process_init_constructor (tree type, tree init, tree* elts) 672{ 673 tree tail; 674 /* List of the elements of the result constructor, 675 in reverse order. */ 676 tree members = NULL; 677 tree next1; 678 tree result; 679 int allconstant = 1; 680 int allsimple = 1; 681 int erroneous = 0; 682 683 /* Make TAIL be the list of elements to use for the initialization, 684 no matter how the data was given to us. */ 685 686 if (elts) 687 { 688 if (warn_missing_braces) 689 warning ("aggregate has a partly bracketed initializer"); 690 tail = *elts; 691 } 692 else 693 tail = CONSTRUCTOR_ELTS (init); 694 695 /* Gobble as many elements as needed, and make a constructor or initial value 696 for each element of this aggregate. Chain them together in result. 697 If there are too few, use 0 for each scalar ultimate component. */ 698 699 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) 700 { 701 long len; 702 int i; 703 704 if (TREE_CODE (type) == ARRAY_TYPE) 705 { 706 tree domain = TYPE_DOMAIN (type); 707 if (domain) 708 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain)) 709 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain)) 710 + 1); 711 else 712 len = -1; /* Take as many as there are. */ 713 } 714 else 715 { 716 /* Vectors are like simple fixed-size arrays. */ 717 len = TYPE_VECTOR_SUBPARTS (type); 718 } 719 720 for (i = 0; len < 0 || i < len; i++) 721 { 722 if (tail) 723 { 724 if (TREE_PURPOSE (tail) 725 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST 726 || compare_tree_int (TREE_PURPOSE (tail), i) != 0)) 727 sorry ("non-trivial labeled initializers"); 728 729 if (TREE_VALUE (tail) != 0) 730 { 731 tree tail1 = tail; 732 next1 = digest_init (TREE_TYPE (type), 733 TREE_VALUE (tail), &tail1); 734 if (next1 == error_mark_node) 735 return next1; 736 my_friendly_assert 737 (same_type_ignoring_top_level_qualifiers_p 738 (TREE_TYPE (type), TREE_TYPE (next1)), 739 981123); 740 my_friendly_assert (tail1 == 0 741 || TREE_CODE (tail1) == TREE_LIST, 319); 742 if (tail == tail1 && len < 0) 743 { 744 error ("non-empty initializer for array of empty elements"); 745 /* Just ignore what we were supposed to use. */ 746 tail1 = NULL_TREE; 747 } 748 tail = tail1; 749 } 750 else 751 { 752 next1 = error_mark_node; 753 tail = TREE_CHAIN (tail); 754 } 755 } 756 else if (len < 0) 757 /* We're done. */ 758 break; 759 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))) 760 { 761 /* If this type needs constructors run for 762 default-initialization, we can't rely on the backend to do it 763 for us, so build up TARGET_EXPRs. If the type in question is 764 a class, just build one up; if it's an array, recurse. */ 765 766 if (IS_AGGR_TYPE (TREE_TYPE (type))) 767 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE); 768 else 769 next1 = build_constructor (NULL_TREE, NULL_TREE); 770 next1 = digest_init (TREE_TYPE (type), next1, 0); 771 } 772 else if (! zero_init_p (TREE_TYPE (type))) 773 next1 = build_zero_init (TREE_TYPE (type), 774 /*nelts=*/NULL_TREE, 775 /*static_storage_p=*/false); 776 else 777 /* The default zero-initialization is fine for us; don't 778 add anything to the CONSTRUCTOR. */ 779 break; 780 781 if (next1 == error_mark_node) 782 erroneous = 1; 783 else if (!TREE_CONSTANT (next1)) 784 allconstant = 0; 785 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) 786 allsimple = 0; 787 members = tree_cons (size_int (i), next1, members); 788 } 789 } 790 else if (TREE_CODE (type) == RECORD_TYPE) 791 { 792 tree field; 793 794 if (tail) 795 { 796 if (TYPE_USES_VIRTUAL_BASECLASSES (type)) 797 { 798 sorry ("initializer list for object of class with virtual base classes"); 799 return error_mark_node; 800 } 801 802 if (TYPE_BINFO_BASETYPES (type)) 803 { 804 sorry ("initializer list for object of class with base classes"); 805 return error_mark_node; 806 } 807 808 if (TYPE_POLYMORPHIC_P (type)) 809 { 810 sorry ("initializer list for object using virtual functions"); 811 return error_mark_node; 812 } 813 } 814 815 for (field = TYPE_FIELDS (type); field; 816 field = TREE_CHAIN (field)) 817 { 818 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field)) 819 { 820 members = tree_cons (field, integer_zero_node, members); 821 continue; 822 } 823 824 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) 825 continue; 826 827 if (tail) 828 { 829 if (TREE_PURPOSE (tail) 830 && TREE_PURPOSE (tail) != field 831 && TREE_PURPOSE (tail) != DECL_NAME (field)) 832 sorry ("non-trivial labeled initializers"); 833 834 if (TREE_VALUE (tail) != 0) 835 { 836 tree tail1 = tail; 837 838 next1 = digest_init (TREE_TYPE (field), 839 TREE_VALUE (tail), &tail1); 840 my_friendly_assert (tail1 == 0 841 || TREE_CODE (tail1) == TREE_LIST, 320); 842 tail = tail1; 843 } 844 else 845 { 846 next1 = error_mark_node; 847 tail = TREE_CHAIN (tail); 848 } 849 } 850 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field))) 851 { 852 /* If this type needs constructors run for 853 default-initialization, we can't rely on the backend to do it 854 for us, so build up TARGET_EXPRs. If the type in question is 855 a class, just build one up; if it's an array, recurse. */ 856 857 if (IS_AGGR_TYPE (TREE_TYPE (field))) 858 next1 = build_functional_cast (TREE_TYPE (field), 859 NULL_TREE); 860 else 861 { 862 next1 = build_constructor (NULL_TREE, NULL_TREE); 863 if (init) 864 TREE_HAS_CONSTRUCTOR (next1) 865 = TREE_HAS_CONSTRUCTOR (init); 866 } 867 next1 = digest_init (TREE_TYPE (field), next1, 0); 868 869 /* Warn when some struct elements are implicitly initialized. */ 870 if (extra_warnings 871 && (!init || TREE_HAS_CONSTRUCTOR (init))) 872 warning ("missing initializer for member `%D'", field); 873 } 874 else 875 { 876 if (TREE_READONLY (field)) 877 error ("uninitialized const member `%D'", field); 878 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field))) 879 error ("member `%D' with uninitialized const fields", 880 field); 881 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE) 882 error ("member `%D' is uninitialized reference", field); 883 884 /* Warn when some struct elements are implicitly initialized 885 to zero. */ 886 if (extra_warnings 887 && (!init || TREE_HAS_CONSTRUCTOR (init))) 888 warning ("missing initializer for member `%D'", field); 889 890 if (! zero_init_p (TREE_TYPE (field))) 891 next1 = build_zero_init (TREE_TYPE (field), 892 /*nelts=*/NULL_TREE, 893 /*static_storage_p=*/false); 894 else 895 /* The default zero-initialization is fine for us; don't 896 add anything to the CONSTRUCTOR. */ 897 continue; 898 } 899 900 if (next1 == error_mark_node) 901 erroneous = 1; 902 else if (!TREE_CONSTANT (next1)) 903 allconstant = 0; 904 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) 905 allsimple = 0; 906 members = tree_cons (field, next1, members); 907 } 908 } 909 else if (TREE_CODE (type) == UNION_TYPE 910 /* If the initializer was empty, use default zero initialization. */ 911 && tail) 912 { 913 tree field = TYPE_FIELDS (type); 914 915 /* Find the first named field. ANSI decided in September 1990 916 that only named fields count here. */ 917 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL)) 918 field = TREE_CHAIN (field); 919 920 /* If this element specifies a field, initialize via that field. */ 921 if (TREE_PURPOSE (tail) != NULL_TREE) 922 { 923 int win = 0; 924 925 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL) 926 /* Handle the case of a call by build_c_cast. */ 927 field = TREE_PURPOSE (tail), win = 1; 928 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE) 929 error ("index value instead of field name in union initializer"); 930 else 931 { 932 tree temp; 933 for (temp = TYPE_FIELDS (type); 934 temp; 935 temp = TREE_CHAIN (temp)) 936 if (DECL_NAME (temp) == TREE_PURPOSE (tail)) 937 break; 938 if (temp) 939 field = temp, win = 1; 940 else 941 error ("no field `%D' in union being initialized", 942 TREE_PURPOSE (tail)); 943 } 944 if (!win) 945 TREE_VALUE (tail) = error_mark_node; 946 } 947 else if (field == 0) 948 { 949 error ("union `%T' with no named members cannot be initialized", 950 type); 951 TREE_VALUE (tail) = error_mark_node; 952 } 953 954 if (TREE_VALUE (tail) != 0) 955 { 956 tree tail1 = tail; 957 958 next1 = digest_init (TREE_TYPE (field), 959 TREE_VALUE (tail), &tail1); 960 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST) 961 abort (); 962 tail = tail1; 963 } 964 else 965 { 966 next1 = error_mark_node; 967 tail = TREE_CHAIN (tail); 968 } 969 970 if (next1 == error_mark_node) 971 erroneous = 1; 972 else if (!TREE_CONSTANT (next1)) 973 allconstant = 0; 974 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0) 975 allsimple = 0; 976 members = tree_cons (field, next1, members); 977 } 978 979 /* If arguments were specified as a list, just remove the ones we used. */ 980 if (elts) 981 *elts = tail; 982 /* If arguments were specified as a constructor, 983 complain unless we used all the elements of the constructor. */ 984 else if (tail) 985 pedwarn ("excess elements in aggregate initializer"); 986 987 if (erroneous) 988 return error_mark_node; 989 990 result = build_constructor (type, nreverse (members)); 991 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE) 992 complete_array_type (type, result, /*do_default=*/0); 993 if (init) 994 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init); 995 if (allconstant) TREE_CONSTANT (result) = 1; 996 if (allconstant && allsimple) TREE_STATIC (result) = 1; 997 return result; 998} 999 1000/* Given a structure or union value DATUM, construct and return 1001 the structure or union component which results from narrowing 1002 that value to the base specified in BASETYPE. For example, given the 1003 hierarchy 1004 1005 class L { int ii; }; 1006 class A : L { ... }; 1007 class B : L { ... }; 1008 class C : A, B { ... }; 1009 1010 and the declaration 1011 1012 C x; 1013 1014 then the expression 1015 1016 x.A::ii refers to the ii member of the L part of 1017 the A part of the C object named by X. In this case, 1018 DATUM would be x, and BASETYPE would be A. 1019 1020 I used to think that this was nonconformant, that the standard specified 1021 that first we look up ii in A, then convert x to an L& and pull out the 1022 ii part. But in fact, it does say that we convert x to an A&; A here 1023 is known as the "naming class". (jason 2000-12-19) 1024 1025 BINFO_P points to a variable initialized either to NULL_TREE or to the 1026 binfo for the specific base subobject we want to convert to. */ 1027 1028tree 1029build_scoped_ref (tree datum, tree basetype, tree* binfo_p) 1030{ 1031 tree binfo; 1032 1033 if (datum == error_mark_node) 1034 return error_mark_node; 1035 if (*binfo_p) 1036 binfo = *binfo_p; 1037 else 1038 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL); 1039 1040 if (!binfo || binfo == error_mark_node) 1041 { 1042 *binfo_p = NULL_TREE; 1043 if (!binfo) 1044 error_not_base_type (basetype, TREE_TYPE (datum)); 1045 return error_mark_node; 1046 } 1047 1048 *binfo_p = binfo; 1049 return build_base_path (PLUS_EXPR, datum, binfo, 1); 1050} 1051 1052/* Build a reference to an object specified by the C++ `->' operator. 1053 Usually this just involves dereferencing the object, but if the 1054 `->' operator is overloaded, then such overloads must be 1055 performed until an object which does not have the `->' operator 1056 overloaded is found. An error is reported when circular pointer 1057 delegation is detected. */ 1058 1059tree 1060build_x_arrow (tree expr) 1061{ 1062 tree orig_expr = expr; 1063 tree types_memoized = NULL_TREE; 1064 tree type = TREE_TYPE (expr); 1065 tree last_rval = NULL_TREE; 1066 1067 if (type == error_mark_node) 1068 return error_mark_node; 1069 1070 if (processing_template_decl) 1071 { 1072 if (type_dependent_expression_p (expr)) 1073 return build_min_nt (ARROW_EXPR, expr); 1074 expr = build_non_dependent_expr (expr); 1075 } 1076 1077 if (TREE_CODE (type) == REFERENCE_TYPE) 1078 { 1079 expr = convert_from_reference (expr); 1080 type = TREE_TYPE (expr); 1081 } 1082 1083 if (IS_AGGR_TYPE (type)) 1084 { 1085 while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr, 1086 NULL_TREE, NULL_TREE, 1087 /*overloaded_p=*/NULL))) 1088 { 1089 if (expr == error_mark_node) 1090 return error_mark_node; 1091 1092 if (value_member (TREE_TYPE (expr), types_memoized)) 1093 { 1094 error ("circular pointer delegation detected"); 1095 return error_mark_node; 1096 } 1097 else 1098 { 1099 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (expr), 1100 types_memoized); 1101 } 1102 last_rval = expr; 1103 } 1104 1105 if (last_rval == NULL_TREE) 1106 { 1107 error ("base operand of `->' has non-pointer type `%T'", type); 1108 return error_mark_node; 1109 } 1110 1111 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE) 1112 last_rval = convert_from_reference (last_rval); 1113 } 1114 else 1115 last_rval = decay_conversion (expr); 1116 1117 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE) 1118 { 1119 if (processing_template_decl) 1120 { 1121 expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr); 1122 /* It will be dereferenced. */ 1123 TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval)); 1124 return expr; 1125 } 1126 1127 return build_indirect_ref (last_rval, NULL); 1128 } 1129 1130 if (types_memoized) 1131 error ("result of `operator->()' yields non-pointer result"); 1132 else 1133 error ("base operand of `->' is not a pointer"); 1134 return error_mark_node; 1135} 1136 1137/* Return an expression for "DATUM .* COMPONENT". DATUM has not 1138 already been checked out to be of aggregate type. */ 1139 1140tree 1141build_m_component_ref (tree datum, tree component) 1142{ 1143 tree ptrmem_type; 1144 tree objtype; 1145 tree type; 1146 tree binfo; 1147 1148 datum = decay_conversion (datum); 1149 1150 if (datum == error_mark_node || component == error_mark_node) 1151 return error_mark_node; 1152 1153 ptrmem_type = TREE_TYPE (component); 1154 if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type)) 1155 { 1156 error ("`%E' cannot be used as a member pointer, since it is of type `%T'", 1157 component, ptrmem_type); 1158 return error_mark_node; 1159 } 1160 1161 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum)); 1162 if (! IS_AGGR_TYPE (objtype)) 1163 { 1164 error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'", 1165 component, datum, objtype); 1166 return error_mark_node; 1167 } 1168 1169 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type); 1170 binfo = lookup_base (objtype, TYPE_PTRMEM_CLASS_TYPE (ptrmem_type), 1171 ba_check, NULL); 1172 if (!binfo) 1173 { 1174 error ("member type `%T::' incompatible with object type `%T'", 1175 type, objtype); 1176 return error_mark_node; 1177 } 1178 else if (binfo == error_mark_node) 1179 return error_mark_node; 1180 1181 if (TYPE_PTRMEM_P (ptrmem_type)) 1182 { 1183 /* Compute the type of the field, as described in [expr.ref]. 1184 There's no such thing as a mutable pointer-to-member, so 1185 things are not as complex as they are for references to 1186 non-static data members. */ 1187 type = cp_build_qualified_type (type, 1188 (cp_type_quals (type) 1189 | cp_type_quals (TREE_TYPE (datum)))); 1190 /* Build an expression for "object + offset" where offset is the 1191 value stored in the pointer-to-data-member. */ 1192 datum = build (PLUS_EXPR, build_pointer_type (type), 1193 build_base_path (PLUS_EXPR, build_address (datum), 1194 binfo, 1), 1195 build_nop (ptrdiff_type_node, component)); 1196 return build_indirect_ref (datum, 0); 1197 } 1198 else 1199 return build (OFFSET_REF, type, datum, component); 1200} 1201 1202/* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ 1203 1204tree 1205build_functional_cast (tree exp, tree parms) 1206{ 1207 /* This is either a call to a constructor, 1208 or a C cast in C++'s `functional' notation. */ 1209 tree type; 1210 1211 if (exp == error_mark_node || parms == error_mark_node) 1212 return error_mark_node; 1213 1214 if (TREE_CODE (exp) == TYPE_DECL) 1215 type = TREE_TYPE (exp); 1216 else 1217 type = exp; 1218 1219 if (processing_template_decl) 1220 { 1221 tree t = build_min (CAST_EXPR, type, parms); 1222 /* We don't know if it will or will not have side effects. */ 1223 TREE_SIDE_EFFECTS (t) = 1; 1224 return t; 1225 } 1226 1227 if (! IS_AGGR_TYPE (type)) 1228 { 1229 /* This must build a C cast. */ 1230 if (parms == NULL_TREE) 1231 parms = integer_zero_node; 1232 else 1233 parms = build_x_compound_expr_from_list (parms, "functional cast"); 1234 1235 return build_c_cast (type, parms); 1236 } 1237 1238 /* Prepare to evaluate as a call to a constructor. If this expression 1239 is actually used, for example, 1240 1241 return X (arg1, arg2, ...); 1242 1243 then the slot being initialized will be filled in. */ 1244 1245 if (!complete_type_or_else (type, NULL_TREE)) 1246 return error_mark_node; 1247 if (abstract_virtuals_error (NULL_TREE, type)) 1248 return error_mark_node; 1249 1250 if (parms && TREE_CHAIN (parms) == NULL_TREE) 1251 return build_c_cast (type, TREE_VALUE (parms)); 1252 1253 /* We need to zero-initialize POD types. Let's do that for everything 1254 that doesn't need a constructor. */ 1255 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type) 1256 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) 1257 { 1258 exp = build_constructor (type, NULL_TREE); 1259 return get_target_expr (exp); 1260 } 1261 1262 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms, 1263 TYPE_BINFO (type), LOOKUP_NORMAL); 1264 1265 if (exp == error_mark_node) 1266 return error_mark_node; 1267 1268 return build_cplus_new (type, exp); 1269} 1270 1271 1272/* Add new exception specifier SPEC, to the LIST we currently have. 1273 If it's already in LIST then do nothing. 1274 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we 1275 know what we're doing. */ 1276 1277tree 1278add_exception_specifier (tree list, tree spec, int complain) 1279{ 1280 bool ok; 1281 tree core = spec; 1282 bool is_ptr; 1283 int diag_type = -1; /* none */ 1284 1285 if (spec == error_mark_node) 1286 return list; 1287 1288 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317); 1289 1290 /* [except.spec] 1, type in an exception specifier shall not be 1291 incomplete, or pointer or ref to incomplete other than pointer 1292 to cv void. */ 1293 is_ptr = TREE_CODE (core) == POINTER_TYPE; 1294 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE) 1295 core = TREE_TYPE (core); 1296 if (complain < 0) 1297 ok = true; 1298 else if (VOID_TYPE_P (core)) 1299 ok = is_ptr; 1300 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM) 1301 ok = true; 1302 else if (processing_template_decl) 1303 ok = true; 1304 else 1305 { 1306 ok = true; 1307 /* 15.4/1 says that types in an exception specifier must be complete, 1308 but it seems more reasonable to only require this on definitions 1309 and calls. So just give a pedwarn at this point; we will give an 1310 error later if we hit one of those two cases. */ 1311 if (!COMPLETE_TYPE_P (complete_type (core))) 1312 diag_type = 2; /* pedwarn */ 1313 } 1314 1315 if (ok) 1316 { 1317 tree probe; 1318 1319 for (probe = list; probe; probe = TREE_CHAIN (probe)) 1320 if (same_type_p (TREE_VALUE (probe), spec)) 1321 break; 1322 if (!probe) 1323 list = tree_cons (NULL_TREE, spec, list); 1324 } 1325 else 1326 diag_type = 0; /* error */ 1327 1328 if (diag_type >= 0 && complain) 1329 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type); 1330 1331 return list; 1332} 1333 1334/* Combine the two exceptions specifier lists LIST and ADD, and return 1335 their union. */ 1336 1337tree 1338merge_exception_specifiers (tree list, tree add) 1339{ 1340 if (!list || !add) 1341 return NULL_TREE; 1342 else if (!TREE_VALUE (list)) 1343 return add; 1344 else if (!TREE_VALUE (add)) 1345 return list; 1346 else 1347 { 1348 tree orig_list = list; 1349 1350 for (; add; add = TREE_CHAIN (add)) 1351 { 1352 tree spec = TREE_VALUE (add); 1353 tree probe; 1354 1355 for (probe = orig_list; probe; probe = TREE_CHAIN (probe)) 1356 if (same_type_p (TREE_VALUE (probe), spec)) 1357 break; 1358 if (!probe) 1359 { 1360 spec = build_tree_list (NULL_TREE, spec); 1361 TREE_CHAIN (spec) = list; 1362 list = spec; 1363 } 1364 } 1365 } 1366 return list; 1367} 1368 1369/* Subroutine of build_call. Ensure that each of the types in the 1370 exception specification is complete. Technically, 15.4/1 says that 1371 they need to be complete when we see a declaration of the function, 1372 but we should be able to get away with only requiring this when the 1373 function is defined or called. See also add_exception_specifier. */ 1374 1375void 1376require_complete_eh_spec_types (tree fntype, tree decl) 1377{ 1378 tree raises; 1379 /* Don't complain about calls to op new. */ 1380 if (decl && DECL_ARTIFICIAL (decl)) 1381 return; 1382 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises; 1383 raises = TREE_CHAIN (raises)) 1384 { 1385 tree type = TREE_VALUE (raises); 1386 if (type && !COMPLETE_TYPE_P (type)) 1387 { 1388 if (decl) 1389 error 1390 ("call to function `%D' which throws incomplete type `%#T'", 1391 decl, type); 1392 else 1393 error ("call to function which throws incomplete type `%#T'", 1394 decl); 1395 } 1396 } 1397} 1398