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