c-typeck.c revision 18336
1174604Sscottl/* Build expressions with type checking for C compiler. 2174604Sscottl Copyright (C) 1987, 88, 91, 92, 93, 94, 1995 Free Software Foundation, Inc. 3174604Sscottl 4174604SscottlThis file is part of GNU CC. 5174604Sscottl 6174604SscottlGNU CC is free software; you can redistribute it and/or modify 7174604Sscottlit under the terms of the GNU General Public License as published by 8174604Sscottlthe Free Software Foundation; either version 2, or (at your option) 9174604Sscottlany later version. 10174604Sscottl 11174604SscottlGNU CC is distributed in the hope that it will be useful, 12174604Sscottlbut WITHOUT ANY WARRANTY; without even the implied warranty of 13174604SscottlMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14174604SscottlGNU General Public License for more details. 15174604Sscottl 16174604SscottlYou should have received a copy of the GNU General Public License 17174604Sscottlalong with GNU CC; see the file COPYING. If not, write to 18174604Sscottlthe Free Software Foundation, 59 Temple Place - Suite 330, 19174604SscottlBoston, MA 02111-1307, USA. */ 20174604Sscottl 21174604Sscottl 22174604Sscottl/* This file is part of the C front end. 23174604Sscottl It contains routines to build C expressions given their operands, 24174604Sscottl including computing the types of the result, C-specific error checks, 25174604Sscottl and some optimization. 26174604Sscottl 27237180Seadler There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, 28174604Sscottl and to process initializations in declarations (since they work 29174604Sscottl like a strange sort of assignment). */ 30174604Sscottl 31174604Sscottl#include "config.h" 32174604Sscottl#include <stdio.h> 33174604Sscottl#include "tree.h" 34174604Sscottl#include "c-tree.h" 35174604Sscottl#include "flags.h" 36174604Sscottl#include "output.h" 37174604Sscottl 38174604Sscottl/* Nonzero if we've already printed a "missing braces around initializer" 39174604Sscottl message within this initializer. */ 40174604Sscottlstatic int missing_braces_mentioned; 41174604Sscottl 42174604Sscottlextern char *index (); 43174604Sscottlextern char *rindex (); 44174604Sscottl 45174604Sscottlstatic tree quality_type PROTO((tree, tree)); 46174604Sscottlstatic int comp_target_types PROTO((tree, tree)); 47174604Sscottlstatic int function_types_compatible_p PROTO((tree, tree)); 48174604Sscottlstatic int type_lists_compatible_p PROTO((tree, tree)); 49199043Smavstatic int self_promoting_type_p PROTO((tree)); 50199043Smavstatic tree decl_constant_value PROTO((tree)); 51199043Smavstatic tree lookup_field PROTO((tree, tree, tree *)); 52199043Smavstatic tree convert_arguments PROTO((tree, tree, tree, tree)); 53237178Seadlerstatic tree pointer_int_sum PROTO((enum tree_code, tree, tree)); 54301589Straszstatic tree pointer_diff PROTO((tree, tree)); 55301589Straszstatic tree unary_complex_lvalue PROTO((enum tree_code, tree)); 56237180Seadlerstatic void pedantic_lvalue_warning PROTO((enum tree_code)); 57237180Seadlerstatic tree internal_build_compound_expr PROTO((tree, int)); 58237180Seadlerstatic tree convert_for_assignment PROTO((tree, tree, char *, tree, 59199043Smav tree, int)); 60199043Smavstatic void warn_for_assignment PROTO((char *, char *, tree, int)); 61174604Sscottlstatic tree valid_compound_expr_initializer PROTO((tree, tree)); 62174604Sscottlstatic void push_string PROTO((char *)); 63174604Sscottlstatic void push_member_name PROTO((tree)); 64174604Sscottlstatic void push_array_bounds PROTO((int)); 65174604Sscottlstatic int spelling_length PROTO((void)); 66174604Sscottlstatic char *print_spelling PROTO((char *)); 67174604Sscottlstatic char *get_spelling PROTO((char *)); 68174604Sscottlstatic void warning_init PROTO((char *, char *, 69174604Sscottl char *)); 70174604Sscottlstatic tree digest_init PROTO((tree, tree, int, int)); 71174604Sscottlstatic void check_init_type_bitfields PROTO((tree)); 72174604Sscottlstatic void output_init_element PROTO((tree, tree, tree, int)); 73174604Sscottlstatic void output_pending_init_elements PROTO((int)); 74174604Sscottl 75174604Sscottl/* Do `exp = require_complete_type (exp);' to make sure exp 76174604Sscottl does not have an incomplete type. (That includes void types.) */ 77174604Sscottl 78174604Sscottltree 79174604Sscottlrequire_complete_type (value) 80174604Sscottl tree value; 81174604Sscottl{ 82174604Sscottl tree type = TREE_TYPE (value); 83174604Sscottl 84174604Sscottl /* First, detect a valid value with a complete type. */ 85174604Sscottl if (TYPE_SIZE (type) != 0 86174604Sscottl && type != void_type_node) 87174604Sscottl return value; 88174604Sscottl 89174604Sscottl incomplete_type_error (value, type); 90174604Sscottl return error_mark_node; 91174604Sscottl} 92174604Sscottl 93174604Sscottl/* Print an error message for invalid use of an incomplete type. 94174604Sscottl VALUE is the expression that was used (or 0 if that isn't known) 95174604Sscottl and TYPE is the type that was invalid. */ 96174604Sscottl 97174604Sscottlvoid 98174604Sscottlincomplete_type_error (value, type) 99174604Sscottl tree value; 100174604Sscottl tree type; 101174604Sscottl{ 102174604Sscottl char *errmsg; 103174604Sscottl 104174604Sscottl /* Avoid duplicate error message. */ 105174604Sscottl if (TREE_CODE (type) == ERROR_MARK) 106174604Sscottl return; 107174604Sscottl 108174604Sscottl if (value != 0 && (TREE_CODE (value) == VAR_DECL 109174604Sscottl || TREE_CODE (value) == PARM_DECL)) 110174604Sscottl error ("`%s' has an incomplete type", 111174604Sscottl IDENTIFIER_POINTER (DECL_NAME (value))); 112174604Sscottl else 113176024Sscottl { 114176024Sscottl retry: 115174604Sscottl /* We must print an error message. Be clever about what it says. */ 116199043Smav 117174604Sscottl switch (TREE_CODE (type)) 118174604Sscottl { 119237180Seadler case RECORD_TYPE: 120237180Seadler errmsg = "invalid use of undefined type `struct %s'"; 121174604Sscottl break; 122174604Sscottl 123174604Sscottl case UNION_TYPE: 124174604Sscottl errmsg = "invalid use of undefined type `union %s'"; 125176247Sbrueffer break; 126174604Sscottl 127174604Sscottl case ENUMERAL_TYPE: 128174604Sscottl errmsg = "invalid use of undefined type `enum %s'"; 129174604Sscottl break; 130174604Sscottl 131174604Sscottl case VOID_TYPE: 132174604Sscottl error ("invalid use of void expression"); 133174604Sscottl return; 134174604Sscottl 135174604Sscottl case ARRAY_TYPE: 136174604Sscottl if (TYPE_DOMAIN (type)) 137174604Sscottl { 138174604Sscottl type = TREE_TYPE (type); 139174604Sscottl goto retry; 140174604Sscottl } 141174604Sscottl error ("invalid use of array with unspecified bounds"); 142174604Sscottl return; 143 144 default: 145 abort (); 146 } 147 148 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) 149 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type))); 150 else 151 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ 152 error ("invalid use of incomplete typedef `%s'", 153 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); 154 } 155} 156 157/* Return a variant of TYPE which has all the type qualifiers of LIKE 158 as well as those of TYPE. */ 159 160static tree 161qualify_type (type, like) 162 tree type, like; 163{ 164 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like); 165 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like); 166 return c_build_type_variant (type, constflag, volflag); 167} 168 169/* Return the common type of two types. 170 We assume that comptypes has already been done and returned 1; 171 if that isn't so, this may crash. In particular, we assume that qualifiers 172 match. 173 174 This is the type for the result of most arithmetic operations 175 if the operands have the given two types. */ 176 177tree 178common_type (t1, t2) 179 tree t1, t2; 180{ 181 register enum tree_code code1; 182 register enum tree_code code2; 183 tree attributes; 184 185 /* Save time if the two types are the same. */ 186 187 if (t1 == t2) return t1; 188 189 /* If one type is nonsense, use the other. */ 190 if (t1 == error_mark_node) 191 return t2; 192 if (t2 == error_mark_node) 193 return t1; 194 195 /* Merge the attributes */ 196 197 { register tree a1, a2; 198 a1 = TYPE_ATTRIBUTES (t1); 199 a2 = TYPE_ATTRIBUTES (t2); 200 201 /* Either one unset? Take the set one. */ 202 203 if (!(attributes = a1)) 204 attributes = a2; 205 206 /* One that completely contains the other? Take it. */ 207 208 else if (a2 && !attribute_list_contained (a1, a2)) 209 if (attribute_list_contained (a2, a1)) 210 attributes = a2; 211 else 212 { 213 /* Pick the longest list, and hang on the other list. */ 214 /* ??? For the moment we punt on the issue of attrs with args. */ 215 216 if (list_length (a1) < list_length (a2)) 217 attributes = a2, a2 = a1; 218 219 for (; a2; a2 = TREE_CHAIN (a2)) 220 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)), 221 attributes) == NULL_TREE) 222 { 223 a1 = copy_node (a2); 224 TREE_CHAIN (a1) = attributes; 225 attributes = a1; 226 } 227 } 228 } 229 230 /* Treat an enum type as the unsigned integer type of the same width. */ 231 232 if (TREE_CODE (t1) == ENUMERAL_TYPE) 233 t1 = type_for_size (TYPE_PRECISION (t1), 1); 234 if (TREE_CODE (t2) == ENUMERAL_TYPE) 235 t2 = type_for_size (TYPE_PRECISION (t2), 1); 236 237 code1 = TREE_CODE (t1); 238 code2 = TREE_CODE (t2); 239 240 /* If one type is complex, form the common type of the non-complex 241 components, then make that complex. Use T1 or T2 if it is the 242 required type. */ 243 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) 244 { 245 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; 246 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; 247 tree subtype = common_type (subtype1, subtype2); 248 249 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) 250 return build_type_attribute_variant (t1, attributes); 251 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) 252 return build_type_attribute_variant (t2, attributes); 253 else 254 return build_type_attribute_variant (build_complex_type (subtype), 255 attributes); 256 } 257 258 switch (code1) 259 { 260 case INTEGER_TYPE: 261 case REAL_TYPE: 262 /* If only one is real, use it as the result. */ 263 264 if (code1 == REAL_TYPE && code2 != REAL_TYPE) 265 return build_type_attribute_variant (t1, attributes); 266 267 if (code2 == REAL_TYPE && code1 != REAL_TYPE) 268 return build_type_attribute_variant (t2, attributes); 269 270 /* Both real or both integers; use the one with greater precision. */ 271 272 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) 273 return build_type_attribute_variant (t1, attributes); 274 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) 275 return build_type_attribute_variant (t2, attributes); 276 277 /* Same precision. Prefer longs to ints even when same size. */ 278 279 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node 280 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) 281 return build_type_attribute_variant (long_unsigned_type_node, 282 attributes); 283 284 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node 285 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) 286 { 287 /* But preserve unsignedness from the other type, 288 since long cannot hold all the values of an unsigned int. */ 289 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2)) 290 t1 = long_unsigned_type_node; 291 else 292 t1 = long_integer_type_node; 293 return build_type_attribute_variant (t1, attributes); 294 } 295 296 /* Otherwise prefer the unsigned one. */ 297 298 if (TREE_UNSIGNED (t1)) 299 return build_type_attribute_variant (t1, attributes); 300 else 301 return build_type_attribute_variant (t2, attributes); 302 303 case POINTER_TYPE: 304 /* For two pointers, do this recursively on the target type, 305 and combine the qualifiers of the two types' targets. */ 306 /* This code was turned off; I don't know why. 307 But ANSI C specifies doing this with the qualifiers. 308 So I turned it on again. */ 309 { 310 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)), 311 TYPE_MAIN_VARIANT (TREE_TYPE (t2))); 312 int constp 313 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2)); 314 int volatilep 315 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2)); 316 t1 = build_pointer_type (c_build_type_variant (target, constp, 317 volatilep)); 318 return build_type_attribute_variant (t1, attributes); 319 } 320#if 0 321 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2))); 322 return build_type_attribute_variant (t1, attributes); 323#endif 324 325 case ARRAY_TYPE: 326 { 327 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2)); 328 /* Save space: see if the result is identical to one of the args. */ 329 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) 330 return build_type_attribute_variant (t1, attributes); 331 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) 332 return build_type_attribute_variant (t2, attributes); 333 /* Merge the element types, and have a size if either arg has one. */ 334 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); 335 return build_type_attribute_variant (t1, attributes); 336 } 337 338 case FUNCTION_TYPE: 339 /* Function types: prefer the one that specified arg types. 340 If both do, merge the arg types. Also merge the return types. */ 341 { 342 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2)); 343 tree p1 = TYPE_ARG_TYPES (t1); 344 tree p2 = TYPE_ARG_TYPES (t2); 345 int len; 346 tree newargs, n; 347 int i; 348 349 /* Save space: see if the result is identical to one of the args. */ 350 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2)) 351 return build_type_attribute_variant (t1, attributes); 352 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1)) 353 return build_type_attribute_variant (t2, attributes); 354 355 /* Simple way if one arg fails to specify argument types. */ 356 if (TYPE_ARG_TYPES (t1) == 0) 357 { 358 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); 359 return build_type_attribute_variant (t1, attributes); 360 } 361 if (TYPE_ARG_TYPES (t2) == 0) 362 { 363 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); 364 return build_type_attribute_variant (t1, attributes); 365 } 366 367 /* If both args specify argument types, we must merge the two 368 lists, argument by argument. */ 369 370 len = list_length (p1); 371 newargs = 0; 372 373 for (i = 0; i < len; i++) 374 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); 375 376 n = newargs; 377 378 for (; p1; 379 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) 380 { 381 /* A null type means arg type is not specified. 382 Take whatever the other function type has. */ 383 if (TREE_VALUE (p1) == 0) 384 { 385 TREE_VALUE (n) = TREE_VALUE (p2); 386 goto parm_done; 387 } 388 if (TREE_VALUE (p2) == 0) 389 { 390 TREE_VALUE (n) = TREE_VALUE (p1); 391 goto parm_done; 392 } 393 394 /* Given wait (union {union wait *u; int *i} *) 395 and wait (union wait *), 396 prefer union wait * as type of parm. */ 397 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE 398 && TREE_VALUE (p1) != TREE_VALUE (p2)) 399 { 400 tree memb; 401 for (memb = TYPE_FIELDS (TREE_VALUE (p1)); 402 memb; memb = TREE_CHAIN (memb)) 403 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2))) 404 { 405 TREE_VALUE (n) = TREE_VALUE (p2); 406 if (pedantic) 407 pedwarn ("function types not truly compatible in ANSI C"); 408 goto parm_done; 409 } 410 } 411 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE 412 && TREE_VALUE (p2) != TREE_VALUE (p1)) 413 { 414 tree memb; 415 for (memb = TYPE_FIELDS (TREE_VALUE (p2)); 416 memb; memb = TREE_CHAIN (memb)) 417 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1))) 418 { 419 TREE_VALUE (n) = TREE_VALUE (p1); 420 if (pedantic) 421 pedwarn ("function types not truly compatible in ANSI C"); 422 goto parm_done; 423 } 424 } 425 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2)); 426 parm_done: ; 427 } 428 429 t1 = build_function_type (valtype, newargs); 430 /* ... falls through ... */ 431 } 432 433 default: 434 return build_type_attribute_variant (t1, attributes); 435 } 436 437} 438 439/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment 440 or various other operations. Return 2 if they are compatible 441 but a warning may be needed if you use them together. */ 442 443int 444comptypes (type1, type2) 445 tree type1, type2; 446{ 447 register tree t1 = type1; 448 register tree t2 = type2; 449 int attrval, val; 450 451 /* Suppress errors caused by previously reported errors. */ 452 453 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) 454 return 1; 455 456 /* Treat an enum type as the integer type of the same width and 457 signedness. */ 458 459 if (TREE_CODE (t1) == ENUMERAL_TYPE) 460 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1)); 461 if (TREE_CODE (t2) == ENUMERAL_TYPE) 462 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2)); 463 464 if (t1 == t2) 465 return 1; 466 467 /* Different classes of types can't be compatible. */ 468 469 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0; 470 471 /* Qualifiers must match. */ 472 473 if (TYPE_READONLY (t1) != TYPE_READONLY (t2)) 474 return 0; 475 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2)) 476 return 0; 477 478 /* Allow for two different type nodes which have essentially the same 479 definition. Note that we already checked for equality of the type 480 type qualifiers (just above). */ 481 482 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) 483 return 1; 484 485#ifndef COMP_TYPE_ATTRIBUTES 486#define COMP_TYPE_ATTRIBUTES(t1,t2) 1 487#endif 488 489 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 490 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2))) 491 return 0; 492 493 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 494 val = 0; 495 496 switch (TREE_CODE (t1)) 497 { 498 case POINTER_TYPE: 499 val = (TREE_TYPE (t1) == TREE_TYPE (t2) 500 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2))); 501 break; 502 503 case FUNCTION_TYPE: 504 val = function_types_compatible_p (t1, t2); 505 break; 506 507 case ARRAY_TYPE: 508 { 509 tree d1 = TYPE_DOMAIN (t1); 510 tree d2 = TYPE_DOMAIN (t2); 511 val = 1; 512 513 /* Target types must match incl. qualifiers. */ 514 if (TREE_TYPE (t1) != TREE_TYPE (t2) 515 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))) 516 return 0; 517 518 /* Sizes must match unless one is missing or variable. */ 519 if (d1 == 0 || d2 == 0 || d1 == d2 520 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST 521 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST 522 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST 523 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST) 524 break; 525 526 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1)) 527 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2))) 528 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1)) 529 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2))) 530 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1)) 531 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2))) 532 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1)) 533 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))))) 534 val = 0; 535 break; 536 } 537 538 case RECORD_TYPE: 539 if (maybe_objc_comptypes (t1, t2, 0) == 1) 540 val = 1; 541 break; 542 } 543 return attrval == 2 && val == 1 ? 2 : val; 544} 545 546/* Return 1 if TTL and TTR are pointers to types that are equivalent, 547 ignoring their qualifiers. */ 548 549static int 550comp_target_types (ttl, ttr) 551 tree ttl, ttr; 552{ 553 int val; 554 555 /* Give maybe_objc_comptypes a crack at letting these types through. */ 556 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0) 557 return val; 558 559 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)), 560 TYPE_MAIN_VARIANT (TREE_TYPE (ttr))); 561 562 if (val == 2 && pedantic) 563 pedwarn ("types are not quite compatible"); 564 return val; 565} 566 567/* Subroutines of `comptypes'. */ 568 569/* Return 1 if two function types F1 and F2 are compatible. 570 If either type specifies no argument types, 571 the other must specify a fixed number of self-promoting arg types. 572 Otherwise, if one type specifies only the number of arguments, 573 the other must specify that number of self-promoting arg types. 574 Otherwise, the argument types must match. */ 575 576static int 577function_types_compatible_p (f1, f2) 578 tree f1, f2; 579{ 580 tree args1, args2; 581 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 582 int val = 1; 583 int val1; 584 585 if (!(TREE_TYPE (f1) == TREE_TYPE (f2) 586 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2))))) 587 return 0; 588 589 args1 = TYPE_ARG_TYPES (f1); 590 args2 = TYPE_ARG_TYPES (f2); 591 592 /* An unspecified parmlist matches any specified parmlist 593 whose argument types don't need default promotions. */ 594 595 if (args1 == 0) 596 { 597 if (!self_promoting_args_p (args2)) 598 return 0; 599 /* If one of these types comes from a non-prototype fn definition, 600 compare that with the other type's arglist. 601 If they don't match, ask for a warning (but no error). */ 602 if (TYPE_ACTUAL_ARG_TYPES (f1) 603 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1))) 604 val = 2; 605 return val; 606 } 607 if (args2 == 0) 608 { 609 if (!self_promoting_args_p (args1)) 610 return 0; 611 if (TYPE_ACTUAL_ARG_TYPES (f2) 612 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2))) 613 val = 2; 614 return val; 615 } 616 617 /* Both types have argument lists: compare them and propagate results. */ 618 val1 = type_lists_compatible_p (args1, args2); 619 return val1 != 1 ? val1 : val; 620} 621 622/* Check two lists of types for compatibility, 623 returning 0 for incompatible, 1 for compatible, 624 or 2 for compatible with warning. */ 625 626static int 627type_lists_compatible_p (args1, args2) 628 tree args1, args2; 629{ 630 /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 631 int val = 1; 632 int newval = 0; 633 634 while (1) 635 { 636 if (args1 == 0 && args2 == 0) 637 return val; 638 /* If one list is shorter than the other, 639 they fail to match. */ 640 if (args1 == 0 || args2 == 0) 641 return 0; 642 /* A null pointer instead of a type 643 means there is supposed to be an argument 644 but nothing is specified about what type it has. 645 So match anything that self-promotes. */ 646 if (TREE_VALUE (args1) == 0) 647 { 648 if (! self_promoting_type_p (TREE_VALUE (args2))) 649 return 0; 650 } 651 else if (TREE_VALUE (args2) == 0) 652 { 653 if (! self_promoting_type_p (TREE_VALUE (args1))) 654 return 0; 655 } 656 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2)))) 657 { 658 /* Allow wait (union {union wait *u; int *i} *) 659 and wait (union wait *) to be compatible. */ 660 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE 661 && (TYPE_NAME (TREE_VALUE (args1)) == 0 662 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1))) 663 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST 664 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)), 665 TYPE_SIZE (TREE_VALUE (args2)))) 666 { 667 tree memb; 668 for (memb = TYPE_FIELDS (TREE_VALUE (args1)); 669 memb; memb = TREE_CHAIN (memb)) 670 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2))) 671 break; 672 if (memb == 0) 673 return 0; 674 } 675 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE 676 && (TYPE_NAME (TREE_VALUE (args2)) == 0 677 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2))) 678 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST 679 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)), 680 TYPE_SIZE (TREE_VALUE (args1)))) 681 { 682 tree memb; 683 for (memb = TYPE_FIELDS (TREE_VALUE (args2)); 684 memb; memb = TREE_CHAIN (memb)) 685 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1))) 686 break; 687 if (memb == 0) 688 return 0; 689 } 690 else 691 return 0; 692 } 693 694 /* comptypes said ok, but record if it said to warn. */ 695 if (newval > val) 696 val = newval; 697 698 args1 = TREE_CHAIN (args1); 699 args2 = TREE_CHAIN (args2); 700 } 701} 702 703/* Return 1 if PARMS specifies a fixed number of parameters 704 and none of their types is affected by default promotions. */ 705 706int 707self_promoting_args_p (parms) 708 tree parms; 709{ 710 register tree t; 711 for (t = parms; t; t = TREE_CHAIN (t)) 712 { 713 register tree type = TREE_VALUE (t); 714 715 if (TREE_CHAIN (t) == 0 && type != void_type_node) 716 return 0; 717 718 if (type == 0) 719 return 0; 720 721 if (TYPE_MAIN_VARIANT (type) == float_type_node) 722 return 0; 723 724 if (C_PROMOTING_INTEGER_TYPE_P (type)) 725 return 0; 726 } 727 return 1; 728} 729 730/* Return 1 if TYPE is not affected by default promotions. */ 731 732static int 733self_promoting_type_p (type) 734 tree type; 735{ 736 if (TYPE_MAIN_VARIANT (type) == float_type_node) 737 return 0; 738 739 if (C_PROMOTING_INTEGER_TYPE_P (type)) 740 return 0; 741 742 return 1; 743} 744 745/* Return an unsigned type the same as TYPE in other respects. */ 746 747tree 748unsigned_type (type) 749 tree type; 750{ 751 tree type1 = TYPE_MAIN_VARIANT (type); 752 if (type1 == signed_char_type_node || type1 == char_type_node) 753 return unsigned_char_type_node; 754 if (type1 == integer_type_node) 755 return unsigned_type_node; 756 if (type1 == short_integer_type_node) 757 return short_unsigned_type_node; 758 if (type1 == long_integer_type_node) 759 return long_unsigned_type_node; 760 if (type1 == long_long_integer_type_node) 761 return long_long_unsigned_type_node; 762 if (type1 == intDI_type_node) 763 return unsigned_intDI_type_node; 764 if (type1 == intSI_type_node) 765 return unsigned_intSI_type_node; 766 if (type1 == intHI_type_node) 767 return unsigned_intHI_type_node; 768 if (type1 == intQI_type_node) 769 return unsigned_intQI_type_node; 770 return type; 771} 772 773/* Return a signed type the same as TYPE in other respects. */ 774 775tree 776signed_type (type) 777 tree type; 778{ 779 tree type1 = TYPE_MAIN_VARIANT (type); 780 if (type1 == unsigned_char_type_node || type1 == char_type_node) 781 return signed_char_type_node; 782 if (type1 == unsigned_type_node) 783 return integer_type_node; 784 if (type1 == short_unsigned_type_node) 785 return short_integer_type_node; 786 if (type1 == long_unsigned_type_node) 787 return long_integer_type_node; 788 if (type1 == long_long_unsigned_type_node) 789 return long_long_integer_type_node; 790 if (type1 == unsigned_intDI_type_node) 791 return intDI_type_node; 792 if (type1 == unsigned_intSI_type_node) 793 return intSI_type_node; 794 if (type1 == unsigned_intHI_type_node) 795 return intHI_type_node; 796 if (type1 == unsigned_intQI_type_node) 797 return intQI_type_node; 798 return type; 799} 800 801/* Return a type the same as TYPE except unsigned or 802 signed according to UNSIGNEDP. */ 803 804tree 805signed_or_unsigned_type (unsignedp, type) 806 int unsignedp; 807 tree type; 808{ 809 if (! INTEGRAL_TYPE_P (type)) 810 return type; 811 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)) 812 return unsignedp ? unsigned_char_type_node : signed_char_type_node; 813 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) 814 return unsignedp ? unsigned_type_node : integer_type_node; 815 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node)) 816 return unsignedp ? short_unsigned_type_node : short_integer_type_node; 817 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node)) 818 return unsignedp ? long_unsigned_type_node : long_integer_type_node; 819 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node)) 820 return (unsignedp ? long_long_unsigned_type_node 821 : long_long_integer_type_node); 822 return type; 823} 824 825/* Compute the value of the `sizeof' operator. */ 826 827tree 828c_sizeof (type) 829 tree type; 830{ 831 enum tree_code code = TREE_CODE (type); 832 tree t; 833 834 if (code == FUNCTION_TYPE) 835 { 836 if (pedantic || warn_pointer_arith) 837 pedwarn ("sizeof applied to a function type"); 838 return size_int (1); 839 } 840 if (code == VOID_TYPE) 841 { 842 if (pedantic || warn_pointer_arith) 843 pedwarn ("sizeof applied to a void type"); 844 return size_int (1); 845 } 846 if (code == ERROR_MARK) 847 return size_int (1); 848 if (TYPE_SIZE (type) == 0) 849 { 850 error ("sizeof applied to an incomplete type"); 851 return size_int (0); 852 } 853 854 /* Convert in case a char is more than one unit. */ 855 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), 856 size_int (TYPE_PRECISION (char_type_node))); 857 /* size_binop does not put the constant in range, so do it now. */ 858 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0)) 859 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1; 860 return t; 861} 862 863tree 864c_sizeof_nowarn (type) 865 tree type; 866{ 867 enum tree_code code = TREE_CODE (type); 868 tree t; 869 870 if (code == FUNCTION_TYPE 871 || code == VOID_TYPE 872 || code == ERROR_MARK) 873 return size_int (1); 874 if (TYPE_SIZE (type) == 0) 875 return size_int (0); 876 877 /* Convert in case a char is more than one unit. */ 878 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), 879 size_int (TYPE_PRECISION (char_type_node))); 880 force_fit_type (t, 0); 881 return t; 882} 883 884/* Compute the size to increment a pointer by. */ 885 886tree 887c_size_in_bytes (type) 888 tree type; 889{ 890 enum tree_code code = TREE_CODE (type); 891 tree t; 892 893 if (code == FUNCTION_TYPE) 894 return size_int (1); 895 if (code == VOID_TYPE) 896 return size_int (1); 897 if (code == ERROR_MARK) 898 return size_int (1); 899 if (TYPE_SIZE (type) == 0) 900 { 901 error ("arithmetic on pointer to an incomplete type"); 902 return size_int (1); 903 } 904 905 /* Convert in case a char is more than one unit. */ 906 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), 907 size_int (BITS_PER_UNIT)); 908 force_fit_type (t, 0); 909 return t; 910} 911 912/* Implement the __alignof keyword: Return the minimum required 913 alignment of TYPE, measured in bytes. */ 914 915tree 916c_alignof (type) 917 tree type; 918{ 919 enum tree_code code = TREE_CODE (type); 920 921 if (code == FUNCTION_TYPE) 922 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); 923 924 if (code == VOID_TYPE || code == ERROR_MARK) 925 return size_int (1); 926 927 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT); 928} 929 930/* Implement the __alignof keyword: Return the minimum required 931 alignment of EXPR, measured in bytes. For VAR_DECL's and 932 FIELD_DECL's return DECL_ALIGN (which can be set from an 933 "aligned" __attribute__ specification). */ 934 935tree 936c_alignof_expr (expr) 937 tree expr; 938{ 939 if (TREE_CODE (expr) == VAR_DECL) 940 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT); 941 942 if (TREE_CODE (expr) == COMPONENT_REF 943 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1))) 944 { 945 error ("`__alignof' applied to a bit-field"); 946 return size_int (1); 947 } 948 else if (TREE_CODE (expr) == COMPONENT_REF 949 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) 950 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT); 951 952 if (TREE_CODE (expr) == INDIRECT_REF) 953 { 954 tree t = TREE_OPERAND (expr, 0); 955 tree best = t; 956 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); 957 958 while (TREE_CODE (t) == NOP_EXPR 959 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) 960 { 961 int thisalign; 962 963 t = TREE_OPERAND (t, 0); 964 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); 965 if (thisalign > bestalign) 966 best = t, bestalign = thisalign; 967 } 968 return c_alignof (TREE_TYPE (TREE_TYPE (best))); 969 } 970 else 971 return c_alignof (TREE_TYPE (expr)); 972} 973/* Return either DECL or its known constant value (if it has one). */ 974 975static tree 976decl_constant_value (decl) 977 tree decl; 978{ 979 if (! TREE_PUBLIC (decl) 980 /* Don't change a variable array bound or initial value to a constant 981 in a place where a variable is invalid. */ 982 && current_function_decl != 0 983 && ! pedantic 984 && ! TREE_THIS_VOLATILE (decl) 985 && TREE_READONLY (decl) && ! ITERATOR_P (decl) 986 && DECL_INITIAL (decl) != 0 987 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK 988 /* This is invalid if initial value is not constant. 989 If it has either a function call, a memory reference, 990 or a variable, then re-evaluating it could give different results. */ 991 && TREE_CONSTANT (DECL_INITIAL (decl)) 992 /* Check for cases where this is sub-optimal, even though valid. */ 993 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR 994 && DECL_MODE (decl) != BLKmode) 995 return DECL_INITIAL (decl); 996 return decl; 997} 998 999/* Perform default promotions for C data used in expressions. 1000 Arrays and functions are converted to pointers; 1001 enumeral types or short or char, to int. 1002 In addition, manifest constants symbols are replaced by their values. */ 1003 1004tree 1005default_conversion (exp) 1006 tree exp; 1007{ 1008 register tree type = TREE_TYPE (exp); 1009 register enum tree_code code = TREE_CODE (type); 1010 1011 /* Constants can be used directly unless they're not loadable. */ 1012 if (TREE_CODE (exp) == CONST_DECL) 1013 exp = DECL_INITIAL (exp); 1014 1015 /* Replace a nonvolatile const static variable with its value unless 1016 it is an array, in which case we must be sure that taking the 1017 address of the array produces consistent results. */ 1018 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE) 1019 { 1020 exp = decl_constant_value (exp); 1021 type = TREE_TYPE (exp); 1022 } 1023 1024 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as 1025 an lvalue. */ 1026 /* Do not use STRIP_NOPS here! It will remove conversions from pointer 1027 to integer and cause infinite recursion. */ 1028 while (TREE_CODE (exp) == NON_LVALUE_EXPR 1029 || (TREE_CODE (exp) == NOP_EXPR 1030 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp))) 1031 exp = TREE_OPERAND (exp, 0); 1032 1033 /* Normally convert enums to int, 1034 but convert wide enums to something wider. */ 1035 if (code == ENUMERAL_TYPE) 1036 { 1037 type = type_for_size (MAX (TYPE_PRECISION (type), 1038 TYPE_PRECISION (integer_type_node)), 1039 ((flag_traditional 1040 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node)) 1041 && TREE_UNSIGNED (type))); 1042 return convert (type, exp); 1043 } 1044 1045 if (C_PROMOTING_INTEGER_TYPE_P (type)) 1046 { 1047 /* Traditionally, unsignedness is preserved in default promotions. 1048 Also preserve unsignedness if not really getting any wider. */ 1049 if (TREE_UNSIGNED (type) 1050 && (flag_traditional 1051 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) 1052 return convert (unsigned_type_node, exp); 1053 return convert (integer_type_node, exp); 1054 } 1055 if (flag_traditional && !flag_allow_single_precision 1056 && TYPE_MAIN_VARIANT (type) == float_type_node) 1057 return convert (double_type_node, exp); 1058 if (code == VOID_TYPE) 1059 { 1060 error ("void value not ignored as it ought to be"); 1061 return error_mark_node; 1062 } 1063 if (code == FUNCTION_TYPE) 1064 { 1065 return build_unary_op (ADDR_EXPR, exp, 0); 1066 } 1067 if (code == ARRAY_TYPE) 1068 { 1069 register tree adr; 1070 tree restype = TREE_TYPE (type); 1071 tree ptrtype; 1072 int constp = 0; 1073 int volatilep = 0; 1074 1075 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' 1076 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd') 1077 { 1078 constp = TREE_READONLY (exp); 1079 volatilep = TREE_THIS_VOLATILE (exp); 1080 } 1081 1082 if (TYPE_READONLY (type) || TYPE_VOLATILE (type) 1083 || constp || volatilep) 1084 restype = c_build_type_variant (restype, 1085 TYPE_READONLY (type) || constp, 1086 TYPE_VOLATILE (type) || volatilep); 1087 1088 if (TREE_CODE (exp) == INDIRECT_REF) 1089 return convert (TYPE_POINTER_TO (restype), 1090 TREE_OPERAND (exp, 0)); 1091 1092 if (TREE_CODE (exp) == COMPOUND_EXPR) 1093 { 1094 tree op1 = default_conversion (TREE_OPERAND (exp, 1)); 1095 return build (COMPOUND_EXPR, TREE_TYPE (op1), 1096 TREE_OPERAND (exp, 0), op1); 1097 } 1098 1099 if (!lvalue_p (exp) 1100 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) 1101 { 1102 error ("invalid use of non-lvalue array"); 1103 return error_mark_node; 1104 } 1105 1106 ptrtype = build_pointer_type (restype); 1107 1108 if (TREE_CODE (exp) == VAR_DECL) 1109 { 1110 /* ??? This is not really quite correct 1111 in that the type of the operand of ADDR_EXPR 1112 is not the target type of the type of the ADDR_EXPR itself. 1113 Question is, can this lossage be avoided? */ 1114 adr = build1 (ADDR_EXPR, ptrtype, exp); 1115 if (mark_addressable (exp) == 0) 1116 return error_mark_node; 1117 TREE_CONSTANT (adr) = staticp (exp); 1118 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */ 1119 return adr; 1120 } 1121 /* This way is better for a COMPONENT_REF since it can 1122 simplify the offset for a component. */ 1123 adr = build_unary_op (ADDR_EXPR, exp, 1); 1124 return convert (ptrtype, adr); 1125 } 1126 return exp; 1127} 1128 1129/* Look up component name in the structure type definition. 1130 1131 If this component name is found indirectly within an anonymous union, 1132 store in *INDIRECT the component which directly contains 1133 that anonymous union. Otherwise, set *INDIRECT to 0. */ 1134 1135static tree 1136lookup_field (type, component, indirect) 1137 tree type, component; 1138 tree *indirect; 1139{ 1140 tree field; 1141 1142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers 1143 to the field elements. Use a binary search on this array to quickly 1144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC 1145 will always be set for structures which have many elements. */ 1146 1147 if (TYPE_LANG_SPECIFIC (type)) 1148 { 1149 int bot, top, half; 1150 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0]; 1151 1152 field = TYPE_FIELDS (type); 1153 bot = 0; 1154 top = TYPE_LANG_SPECIFIC (type)->len; 1155 while (top - bot > 1) 1156 { 1157 half = (top - bot + 1) >> 1; 1158 field = field_array[bot+half]; 1159 1160 if (DECL_NAME (field) == NULL_TREE) 1161 { 1162 /* Step through all anon unions in linear fashion. */ 1163 while (DECL_NAME (field_array[bot]) == NULL_TREE) 1164 { 1165 tree anon, junk; 1166 1167 field = field_array[bot++]; 1168 anon = lookup_field (TREE_TYPE (field), component, &junk); 1169 if (anon != NULL_TREE) 1170 { 1171 *indirect = field; 1172 return anon; 1173 } 1174 } 1175 1176 /* Entire record is only anon unions. */ 1177 if (bot > top) 1178 return NULL_TREE; 1179 1180 /* Restart the binary search, with new lower bound. */ 1181 continue; 1182 } 1183 1184 if (DECL_NAME (field) == component) 1185 break; 1186 if (DECL_NAME (field) < component) 1187 bot += half; 1188 else 1189 top = bot + half; 1190 } 1191 1192 if (DECL_NAME (field_array[bot]) == component) 1193 field = field_array[bot]; 1194 else if (DECL_NAME (field) != component) 1195 field = 0; 1196 } 1197 else 1198 { 1199 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 1200 { 1201 if (DECL_NAME (field) == NULL_TREE) 1202 { 1203 tree junk; 1204 tree anon = lookup_field (TREE_TYPE (field), component, &junk); 1205 if (anon != NULL_TREE) 1206 { 1207 *indirect = field; 1208 return anon; 1209 } 1210 } 1211 1212 if (DECL_NAME (field) == component) 1213 break; 1214 } 1215 } 1216 1217 *indirect = NULL_TREE; 1218 return field; 1219} 1220 1221/* Make an expression to refer to the COMPONENT field of 1222 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */ 1223 1224tree 1225build_component_ref (datum, component) 1226 tree datum, component; 1227{ 1228 register tree type = TREE_TYPE (datum); 1229 register enum tree_code code = TREE_CODE (type); 1230 register tree field = NULL; 1231 register tree ref; 1232 1233 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it 1234 unless we are not to support things not strictly ANSI. */ 1235 switch (TREE_CODE (datum)) 1236 { 1237 case COMPOUND_EXPR: 1238 { 1239 tree value = build_component_ref (TREE_OPERAND (datum, 1), component); 1240 return build (COMPOUND_EXPR, TREE_TYPE (value), 1241 TREE_OPERAND (datum, 0), value); 1242 } 1243 case COND_EXPR: 1244 return build_conditional_expr 1245 (TREE_OPERAND (datum, 0), 1246 build_component_ref (TREE_OPERAND (datum, 1), component), 1247 build_component_ref (TREE_OPERAND (datum, 2), component)); 1248 } 1249 1250 /* See if there is a field or component with name COMPONENT. */ 1251 1252 if (code == RECORD_TYPE || code == UNION_TYPE) 1253 { 1254 tree indirect = 0; 1255 1256 if (TYPE_SIZE (type) == 0) 1257 { 1258 incomplete_type_error (NULL_TREE, type); 1259 return error_mark_node; 1260 } 1261 1262 field = lookup_field (type, component, &indirect); 1263 1264 if (!field) 1265 { 1266 error (code == RECORD_TYPE 1267 ? "structure has no member named `%s'" 1268 : "union has no member named `%s'", 1269 IDENTIFIER_POINTER (component)); 1270 return error_mark_node; 1271 } 1272 if (TREE_TYPE (field) == error_mark_node) 1273 return error_mark_node; 1274 1275 /* If FIELD was found buried within an anonymous union, 1276 make one COMPONENT_REF to get that anonymous union, 1277 then fall thru to make a second COMPONENT_REF to get FIELD. */ 1278 if (indirect != 0) 1279 { 1280 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect); 1281 if (TREE_READONLY (datum) || TREE_READONLY (indirect)) 1282 TREE_READONLY (ref) = 1; 1283 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect)) 1284 TREE_THIS_VOLATILE (ref) = 1; 1285 datum = ref; 1286 } 1287 1288 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field); 1289 1290 if (TREE_READONLY (datum) || TREE_READONLY (field)) 1291 TREE_READONLY (ref) = 1; 1292 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field)) 1293 TREE_THIS_VOLATILE (ref) = 1; 1294 1295 return ref; 1296 } 1297 else if (code != ERROR_MARK) 1298 error ("request for member `%s' in something not a structure or union", 1299 IDENTIFIER_POINTER (component)); 1300 1301 return error_mark_node; 1302} 1303 1304/* Given an expression PTR for a pointer, return an expression 1305 for the value pointed to. 1306 ERRORSTRING is the name of the operator to appear in error messages. */ 1307 1308tree 1309build_indirect_ref (ptr, errorstring) 1310 tree ptr; 1311 char *errorstring; 1312{ 1313 register tree pointer = default_conversion (ptr); 1314 register tree type = TREE_TYPE (pointer); 1315 1316 if (TREE_CODE (type) == POINTER_TYPE) 1317 { 1318 if (TREE_CODE (pointer) == ADDR_EXPR 1319 && !flag_volatile 1320 && (TREE_TYPE (TREE_OPERAND (pointer, 0)) 1321 == TREE_TYPE (type))) 1322 return TREE_OPERAND (pointer, 0); 1323 else 1324 { 1325 tree t = TREE_TYPE (type); 1326 register tree ref = build1 (INDIRECT_REF, 1327 TYPE_MAIN_VARIANT (t), pointer); 1328 1329 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE) 1330 { 1331 error ("dereferencing pointer to incomplete type"); 1332 return error_mark_node; 1333 } 1334 if (TREE_CODE (t) == VOID_TYPE) 1335 warning ("dereferencing `void *' pointer"); 1336 1337 /* We *must* set TREE_READONLY when dereferencing a pointer to const, 1338 so that we get the proper error message if the result is used 1339 to assign to. Also, &* is supposed to be a no-op. 1340 And ANSI C seems to specify that the type of the result 1341 should be the const type. */ 1342 /* A de-reference of a pointer to const is not a const. It is valid 1343 to change it via some other pointer. */ 1344 TREE_READONLY (ref) = TYPE_READONLY (t); 1345 TREE_SIDE_EFFECTS (ref) 1346 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile; 1347 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); 1348 return ref; 1349 } 1350 } 1351 else if (TREE_CODE (pointer) != ERROR_MARK) 1352 error ("invalid type argument of `%s'", errorstring); 1353 return error_mark_node; 1354} 1355 1356/* This handles expressions of the form "a[i]", which denotes 1357 an array reference. 1358 1359 This is logically equivalent in C to *(a+i), but we may do it differently. 1360 If A is a variable or a member, we generate a primitive ARRAY_REF. 1361 This avoids forcing the array out of registers, and can work on 1362 arrays that are not lvalues (for example, members of structures returned 1363 by functions). */ 1364 1365tree 1366build_array_ref (array, index) 1367 tree array, index; 1368{ 1369 if (index == 0) 1370 { 1371 error ("subscript missing in array reference"); 1372 return error_mark_node; 1373 } 1374 1375 if (TREE_TYPE (array) == error_mark_node 1376 || TREE_TYPE (index) == error_mark_node) 1377 return error_mark_node; 1378 1379 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE 1380 && TREE_CODE (array) != INDIRECT_REF) 1381 { 1382 tree rval, type; 1383 1384 /* Subscripting with type char is likely to lose 1385 on a machine where chars are signed. 1386 So warn on any machine, but optionally. 1387 Don't warn for unsigned char since that type is safe. 1388 Don't warn for signed char because anyone who uses that 1389 must have done so deliberately. */ 1390 if (warn_char_subscripts 1391 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) 1392 warning ("array subscript has type `char'"); 1393 1394 /* Apply default promotions *after* noticing character types. */ 1395 index = default_conversion (index); 1396 1397 /* Require integer *after* promotion, for sake of enums. */ 1398 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE) 1399 { 1400 error ("array subscript is not an integer"); 1401 return error_mark_node; 1402 } 1403 1404 /* An array that is indexed by a non-constant 1405 cannot be stored in a register; we must be able to do 1406 address arithmetic on its address. 1407 Likewise an array of elements of variable size. */ 1408 if (TREE_CODE (index) != INTEGER_CST 1409 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0 1410 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) 1411 { 1412 if (mark_addressable (array) == 0) 1413 return error_mark_node; 1414 } 1415 /* An array that is indexed by a constant value which is not within 1416 the array bounds cannot be stored in a register either; because we 1417 would get a crash in store_bit_field/extract_bit_field when trying 1418 to access a non-existent part of the register. */ 1419 if (TREE_CODE (index) == INTEGER_CST 1420 && TYPE_VALUES (TREE_TYPE (array)) 1421 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array)))) 1422 { 1423 if (mark_addressable (array) == 0) 1424 return error_mark_node; 1425 } 1426 1427 if (pedantic && !lvalue_p (array)) 1428 { 1429 if (DECL_REGISTER (array)) 1430 pedwarn ("ANSI C forbids subscripting `register' array"); 1431 else 1432 pedwarn ("ANSI C forbids subscripting non-lvalue array"); 1433 } 1434 1435 if (pedantic) 1436 { 1437 tree foo = array; 1438 while (TREE_CODE (foo) == COMPONENT_REF) 1439 foo = TREE_OPERAND (foo, 0); 1440 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)) 1441 pedwarn ("ANSI C forbids subscripting non-lvalue array"); 1442 } 1443 1444 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array))); 1445 rval = build (ARRAY_REF, type, array, index); 1446 /* Array ref is const/volatile if the array elements are 1447 or if the array is. */ 1448 TREE_READONLY (rval) 1449 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) 1450 | TREE_READONLY (array)); 1451 TREE_SIDE_EFFECTS (rval) 1452 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) 1453 | TREE_SIDE_EFFECTS (array)); 1454 TREE_THIS_VOLATILE (rval) 1455 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) 1456 /* This was added by rms on 16 Nov 91. 1457 It fixes vol struct foo *a; a->elts[1] 1458 in an inline function. 1459 Hope it doesn't break something else. */ 1460 | TREE_THIS_VOLATILE (array)); 1461 return require_complete_type (fold (rval)); 1462 } 1463 1464 { 1465 tree ar = default_conversion (array); 1466 tree ind = default_conversion (index); 1467 1468 /* Put the integer in IND to simplify error checking. */ 1469 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) 1470 { 1471 tree temp = ar; 1472 ar = ind; 1473 ind = temp; 1474 } 1475 1476 if (ar == error_mark_node) 1477 return ar; 1478 1479 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE) 1480 { 1481 error ("subscripted value is neither array nor pointer"); 1482 return error_mark_node; 1483 } 1484 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) 1485 { 1486 error ("array subscript is not an integer"); 1487 return error_mark_node; 1488 } 1489 1490 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0), 1491 "array indexing"); 1492 } 1493} 1494 1495/* Build a function call to function FUNCTION with parameters PARAMS. 1496 PARAMS is a list--a chain of TREE_LIST nodes--in which the 1497 TREE_VALUE of each node is a parameter-expression. 1498 FUNCTION's data type may be a function type or a pointer-to-function. */ 1499 1500tree 1501build_function_call (function, params) 1502 tree function, params; 1503{ 1504 register tree fntype, fundecl = 0; 1505 register tree coerced_params; 1506 tree name = NULL_TREE, assembler_name = NULL_TREE; 1507 1508 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 1509 STRIP_TYPE_NOPS (function); 1510 1511 /* Convert anything with function type to a pointer-to-function. */ 1512 if (TREE_CODE (function) == FUNCTION_DECL) 1513 { 1514 name = DECL_NAME (function); 1515 assembler_name = DECL_ASSEMBLER_NAME (function); 1516 1517 /* Differs from default_conversion by not setting TREE_ADDRESSABLE 1518 (because calling an inline function does not mean the function 1519 needs to be separately compiled). */ 1520 fntype = build_type_variant (TREE_TYPE (function), 1521 TREE_READONLY (function), 1522 TREE_THIS_VOLATILE (function)); 1523 fundecl = function; 1524 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function); 1525 } 1526 else 1527 function = default_conversion (function); 1528 1529 fntype = TREE_TYPE (function); 1530 1531 if (TREE_CODE (fntype) == ERROR_MARK) 1532 return error_mark_node; 1533 1534 if (!(TREE_CODE (fntype) == POINTER_TYPE 1535 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) 1536 { 1537 error ("called object is not a function"); 1538 return error_mark_node; 1539 } 1540 1541 /* fntype now gets the type of function pointed to. */ 1542 fntype = TREE_TYPE (fntype); 1543 1544 /* Convert the parameters to the types declared in the 1545 function prototype, or apply default promotions. */ 1546 1547 coerced_params 1548 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl); 1549 1550 /* Check for errors in format strings. */ 1551 1552 if (warn_format && (name || assembler_name)) 1553 check_function_format (name, assembler_name, coerced_params); 1554 1555 /* Recognize certain built-in functions so we can make tree-codes 1556 other than CALL_EXPR. We do this when it enables fold-const.c 1557 to do something useful. */ 1558 1559 if (TREE_CODE (function) == ADDR_EXPR 1560 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL 1561 && DECL_BUILT_IN (TREE_OPERAND (function, 0))) 1562 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0))) 1563 { 1564 case BUILT_IN_ABS: 1565 case BUILT_IN_LABS: 1566 case BUILT_IN_FABS: 1567 if (coerced_params == 0) 1568 return integer_zero_node; 1569 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0); 1570 } 1571 1572 { 1573 register tree result 1574 = build (CALL_EXPR, TREE_TYPE (fntype), 1575 function, coerced_params, NULL_TREE); 1576 1577 TREE_SIDE_EFFECTS (result) = 1; 1578 if (TREE_TYPE (result) == void_type_node) 1579 return result; 1580 return require_complete_type (result); 1581 } 1582} 1583 1584/* Convert the argument expressions in the list VALUES 1585 to the types in the list TYPELIST. The result is a list of converted 1586 argument expressions. 1587 1588 If TYPELIST is exhausted, or when an element has NULL as its type, 1589 perform the default conversions. 1590 1591 PARMLIST is the chain of parm decls for the function being called. 1592 It may be 0, if that info is not available. 1593 It is used only for generating error messages. 1594 1595 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. 1596 1597 This is also where warnings about wrong number of args are generated. 1598 1599 Both VALUES and the returned value are chains of TREE_LIST nodes 1600 with the elements of the list in the TREE_VALUE slots of those nodes. */ 1601 1602static tree 1603convert_arguments (typelist, values, name, fundecl) 1604 tree typelist, values, name, fundecl; 1605{ 1606 register tree typetail, valtail; 1607 register tree result = NULL; 1608 int parmnum; 1609 1610 /* Scan the given expressions and types, producing individual 1611 converted arguments and pushing them on RESULT in reverse order. */ 1612 1613 for (valtail = values, typetail = typelist, parmnum = 0; 1614 valtail; 1615 valtail = TREE_CHAIN (valtail), parmnum++) 1616 { 1617 register tree type = typetail ? TREE_VALUE (typetail) : 0; 1618 register tree val = TREE_VALUE (valtail); 1619 1620 if (type == void_type_node) 1621 { 1622 if (name) 1623 error ("too many arguments to function `%s'", 1624 IDENTIFIER_POINTER (name)); 1625 else 1626 error ("too many arguments to function"); 1627 break; 1628 } 1629 1630 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 1631 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0 1632 to convert automatically to a pointer. */ 1633 if (TREE_CODE (val) == NON_LVALUE_EXPR) 1634 val = TREE_OPERAND (val, 0); 1635 1636 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE 1637 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE) 1638 val = default_conversion (val); 1639 1640 val = require_complete_type (val); 1641 1642 if (type != 0) 1643 { 1644 /* Formal parm type is specified by a function prototype. */ 1645 tree parmval; 1646 1647 if (TYPE_SIZE (type) == 0) 1648 { 1649 error ("type of formal parameter %d is incomplete", parmnum + 1); 1650 parmval = val; 1651 } 1652 else 1653 { 1654 /* Optionally warn about conversions that 1655 differ from the default conversions. */ 1656 if (warn_conversion) 1657 { 1658 int formal_prec = TYPE_PRECISION (type); 1659 1660 if (INTEGRAL_TYPE_P (type) 1661 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) 1662 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1); 1663 else if (TREE_CODE (type) == COMPLEX_TYPE 1664 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) 1665 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1); 1666 else if (TREE_CODE (type) == REAL_TYPE 1667 && INTEGRAL_TYPE_P (TREE_TYPE (val))) 1668 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1); 1669 else if (TREE_CODE (type) == REAL_TYPE 1670 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE) 1671 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1); 1672 /* ??? At some point, messages should be written about 1673 conversions between complex types, but that's too messy 1674 to do now. */ 1675 else if (TREE_CODE (type) == REAL_TYPE 1676 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) 1677 { 1678 /* Warn if any argument is passed as `float', 1679 since without a prototype it would be `double'. */ 1680 if (formal_prec == TYPE_PRECISION (float_type_node)) 1681 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1); 1682 } 1683 /* Detect integer changing in width or signedness. */ 1684 else if (INTEGRAL_TYPE_P (type) 1685 && INTEGRAL_TYPE_P (TREE_TYPE (val))) 1686 { 1687 tree would_have_been = default_conversion (val); 1688 tree type1 = TREE_TYPE (would_have_been); 1689 1690 if (TREE_CODE (type) == ENUMERAL_TYPE 1691 && type == TREE_TYPE (val)) 1692 /* No warning if function asks for enum 1693 and the actual arg is that enum type. */ 1694 ; 1695 else if (formal_prec != TYPE_PRECISION (type1)) 1696 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1); 1697 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1)) 1698 ; 1699 /* Don't complain if the formal parameter type 1700 is an enum, because we can't tell now whether 1701 the value was an enum--even the same enum. */ 1702 else if (TREE_CODE (type) == ENUMERAL_TYPE) 1703 ; 1704 else if (TREE_CODE (val) == INTEGER_CST 1705 && int_fits_type_p (val, type)) 1706 /* Change in signedness doesn't matter 1707 if a constant value is unaffected. */ 1708 ; 1709 /* Likewise for a constant in a NOP_EXPR. */ 1710 else if (TREE_CODE (val) == NOP_EXPR 1711 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST 1712 && int_fits_type_p (TREE_OPERAND (val, 0), type)) 1713 ; 1714#if 0 /* We never get such tree structure here. */ 1715 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE 1716 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type) 1717 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type)) 1718 /* Change in signedness doesn't matter 1719 if an enum value is unaffected. */ 1720 ; 1721#endif 1722 /* If the value is extended from a narrower 1723 unsigned type, it doesn't matter whether we 1724 pass it as signed or unsigned; the value 1725 certainly is the same either way. */ 1726 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type) 1727 && TREE_UNSIGNED (TREE_TYPE (val))) 1728 ; 1729 else if (TREE_UNSIGNED (type)) 1730 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1); 1731 else 1732 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1); 1733 } 1734 } 1735 1736 parmval = convert_for_assignment (type, val, 1737 (char *)0, /* arg passing */ 1738 fundecl, name, parmnum + 1); 1739 1740#ifdef PROMOTE_PROTOTYPES 1741 if ((TREE_CODE (type) == INTEGER_TYPE 1742 || TREE_CODE (type) == ENUMERAL_TYPE) 1743 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) 1744 parmval = default_conversion (parmval); 1745#endif 1746 } 1747 result = tree_cons (NULL_TREE, parmval, result); 1748 } 1749 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE 1750 && (TYPE_PRECISION (TREE_TYPE (val)) 1751 < TYPE_PRECISION (double_type_node))) 1752 /* Convert `float' to `double'. */ 1753 result = tree_cons (NULL_TREE, convert (double_type_node, val), result); 1754 else 1755 /* Convert `short' and `char' to full-size `int'. */ 1756 result = tree_cons (NULL_TREE, default_conversion (val), result); 1757 1758 if (typetail) 1759 typetail = TREE_CHAIN (typetail); 1760 } 1761 1762 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) 1763 { 1764 if (name) 1765 error ("too few arguments to function `%s'", 1766 IDENTIFIER_POINTER (name)); 1767 else 1768 error ("too few arguments to function"); 1769 } 1770 1771 return nreverse (result); 1772} 1773 1774/* This is the entry point used by the parser 1775 for binary operators in the input. 1776 In addition to constructing the expression, 1777 we check for operands that were written with other binary operators 1778 in a way that is likely to confuse the user. */ 1779 1780tree 1781parser_build_binary_op (code, arg1, arg2) 1782 enum tree_code code; 1783 tree arg1, arg2; 1784{ 1785 tree result = build_binary_op (code, arg1, arg2, 1); 1786 1787 char class; 1788 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1)); 1789 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2)); 1790 enum tree_code code1 = ERROR_MARK; 1791 enum tree_code code2 = ERROR_MARK; 1792 1793 if (class1 == 'e' || class1 == '1' 1794 || class1 == '2' || class1 == '<') 1795 code1 = C_EXP_ORIGINAL_CODE (arg1); 1796 if (class2 == 'e' || class2 == '1' 1797 || class2 == '2' || class2 == '<') 1798 code2 = C_EXP_ORIGINAL_CODE (arg2); 1799 1800 /* Check for cases such as x+y<<z which users are likely 1801 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE 1802 is cleared to prevent these warnings. */ 1803 if (warn_parentheses) 1804 { 1805 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR) 1806 { 1807 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR 1808 || code2 == PLUS_EXPR || code2 == MINUS_EXPR) 1809 warning ("suggest parentheses around + or - inside shift"); 1810 } 1811 1812 if (code == TRUTH_ORIF_EXPR) 1813 { 1814 if (code1 == TRUTH_ANDIF_EXPR 1815 || code2 == TRUTH_ANDIF_EXPR) 1816 warning ("suggest parentheses around && within ||"); 1817 } 1818 1819 if (code == BIT_IOR_EXPR) 1820 { 1821 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR 1822 || code1 == PLUS_EXPR || code1 == MINUS_EXPR 1823 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR 1824 || code2 == PLUS_EXPR || code2 == MINUS_EXPR) 1825 warning ("suggest parentheses around arithmetic in operand of |"); 1826 /* Check cases like x|y==z */ 1827 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') 1828 warning ("suggest parentheses around comparison in operand of |"); 1829 } 1830 1831 if (code == BIT_XOR_EXPR) 1832 { 1833 if (code1 == BIT_AND_EXPR 1834 || code1 == PLUS_EXPR || code1 == MINUS_EXPR 1835 || code2 == BIT_AND_EXPR 1836 || code2 == PLUS_EXPR || code2 == MINUS_EXPR) 1837 warning ("suggest parentheses around arithmetic in operand of ^"); 1838 /* Check cases like x^y==z */ 1839 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') 1840 warning ("suggest parentheses around comparison in operand of ^"); 1841 } 1842 1843 if (code == BIT_AND_EXPR) 1844 { 1845 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR 1846 || code2 == PLUS_EXPR || code2 == MINUS_EXPR) 1847 warning ("suggest parentheses around + or - in operand of &"); 1848 /* Check cases like x&y==z */ 1849 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') 1850 warning ("suggest parentheses around comparison in operand of &"); 1851 } 1852 } 1853 1854 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */ 1855 if (TREE_CODE_CLASS (code) == '<' && extra_warnings 1856 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')) 1857 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning"); 1858 1859 unsigned_conversion_warning (result, arg1); 1860 unsigned_conversion_warning (result, arg2); 1861 overflow_warning (result); 1862 1863 class = TREE_CODE_CLASS (TREE_CODE (result)); 1864 1865 /* Record the code that was specified in the source, 1866 for the sake of warnings about confusing nesting. */ 1867 if (class == 'e' || class == '1' 1868 || class == '2' || class == '<') 1869 C_SET_EXP_ORIGINAL_CODE (result, code); 1870 else 1871 { 1872 int flag = TREE_CONSTANT (result); 1873 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR 1874 so that convert_for_assignment wouldn't strip it. 1875 That way, we got warnings for things like p = (1 - 1). 1876 But it turns out we should not get those warnings. */ 1877 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result); 1878 C_SET_EXP_ORIGINAL_CODE (result, code); 1879 TREE_CONSTANT (result) = flag; 1880 } 1881 1882 return result; 1883} 1884 1885/* Build a binary-operation expression without default conversions. 1886 CODE is the kind of expression to build. 1887 This function differs from `build' in several ways: 1888 the data type of the result is computed and recorded in it, 1889 warnings are generated if arg data types are invalid, 1890 special handling for addition and subtraction of pointers is known, 1891 and some optimization is done (operations on narrow ints 1892 are done in the narrower type when that gives the same result). 1893 Constant folding is also done before the result is returned. 1894 1895 Note that the operands will never have enumeral types, or function 1896 or array types, because either they will have the default conversions 1897 performed or they have both just been converted to some other type in which 1898 the arithmetic is to be done. */ 1899 1900tree 1901build_binary_op (code, orig_op0, orig_op1, convert_p) 1902 enum tree_code code; 1903 tree orig_op0, orig_op1; 1904 int convert_p; 1905{ 1906 tree type0, type1; 1907 register enum tree_code code0, code1; 1908 tree op0, op1; 1909 1910 /* Expression code to give to the expression when it is built. 1911 Normally this is CODE, which is what the caller asked for, 1912 but in some special cases we change it. */ 1913 register enum tree_code resultcode = code; 1914 1915 /* Data type in which the computation is to be performed. 1916 In the simplest cases this is the common type of the arguments. */ 1917 register tree result_type = NULL; 1918 1919 /* Nonzero means operands have already been type-converted 1920 in whatever way is necessary. 1921 Zero means they need to be converted to RESULT_TYPE. */ 1922 int converted = 0; 1923 1924 /* Nonzero means create the expression with this type, rather than 1925 RESULT_TYPE. */ 1926 tree build_type = 0; 1927 1928 /* Nonzero means after finally constructing the expression 1929 convert it to this type. */ 1930 tree final_type = 0; 1931 1932 /* Nonzero if this is an operation like MIN or MAX which can 1933 safely be computed in short if both args are promoted shorts. 1934 Also implies COMMON. 1935 -1 indicates a bitwise operation; this makes a difference 1936 in the exact conditions for when it is safe to do the operation 1937 in a narrower mode. */ 1938 int shorten = 0; 1939 1940 /* Nonzero if this is a comparison operation; 1941 if both args are promoted shorts, compare the original shorts. 1942 Also implies COMMON. */ 1943 int short_compare = 0; 1944 1945 /* Nonzero if this is a right-shift operation, which can be computed on the 1946 original short and then promoted if the operand is a promoted short. */ 1947 int short_shift = 0; 1948 1949 /* Nonzero means set RESULT_TYPE to the common type of the args. */ 1950 int common = 0; 1951 1952 if (convert_p) 1953 { 1954 op0 = default_conversion (orig_op0); 1955 op1 = default_conversion (orig_op1); 1956 } 1957 else 1958 { 1959 op0 = orig_op0; 1960 op1 = orig_op1; 1961 } 1962 1963 type0 = TREE_TYPE (op0); 1964 type1 = TREE_TYPE (op1); 1965 1966 /* The expression codes of the data types of the arguments tell us 1967 whether the arguments are integers, floating, pointers, etc. */ 1968 code0 = TREE_CODE (type0); 1969 code1 = TREE_CODE (type1); 1970 1971 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 1972 STRIP_TYPE_NOPS (op0); 1973 STRIP_TYPE_NOPS (op1); 1974 1975 /* If an error was already reported for one of the arguments, 1976 avoid reporting another error. */ 1977 1978 if (code0 == ERROR_MARK || code1 == ERROR_MARK) 1979 return error_mark_node; 1980 1981 switch (code) 1982 { 1983 case PLUS_EXPR: 1984 /* Handle the pointer + int case. */ 1985 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 1986 return pointer_int_sum (PLUS_EXPR, op0, op1); 1987 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) 1988 return pointer_int_sum (PLUS_EXPR, op1, op0); 1989 else 1990 common = 1; 1991 break; 1992 1993 case MINUS_EXPR: 1994 /* Subtraction of two similar pointers. 1995 We must subtract them as integers, then divide by object size. */ 1996 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE 1997 && comp_target_types (type0, type1)) 1998 return pointer_diff (op0, op1); 1999 /* Handle pointer minus int. Just like pointer plus int. */ 2000 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2001 return pointer_int_sum (MINUS_EXPR, op0, op1); 2002 else 2003 common = 1; 2004 break; 2005 2006 case MULT_EXPR: 2007 common = 1; 2008 break; 2009 2010 case TRUNC_DIV_EXPR: 2011 case CEIL_DIV_EXPR: 2012 case FLOOR_DIV_EXPR: 2013 case ROUND_DIV_EXPR: 2014 case EXACT_DIV_EXPR: 2015 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 2016 || code0 == COMPLEX_TYPE) 2017 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 2018 || code1 == COMPLEX_TYPE)) 2019 { 2020 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)) 2021 resultcode = RDIV_EXPR; 2022 else 2023 { 2024 /* Although it would be tempting to shorten always here, that 2025 loses on some targets, since the modulo instruction is 2026 undefined if the quotient can't be represented in the 2027 computation mode. We shorten only if unsigned or if 2028 dividing by something we know != -1. */ 2029 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0)) 2030 || (TREE_CODE (op1) == INTEGER_CST 2031 && (TREE_INT_CST_LOW (op1) != -1 2032 || TREE_INT_CST_HIGH (op1) != -1))); 2033 } 2034 common = 1; 2035 } 2036 break; 2037 2038 case BIT_AND_EXPR: 2039 case BIT_ANDTC_EXPR: 2040 case BIT_IOR_EXPR: 2041 case BIT_XOR_EXPR: 2042 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2043 shorten = -1; 2044 /* If one operand is a constant, and the other is a short type 2045 that has been converted to an int, 2046 really do the work in the short type and then convert the 2047 result to int. If we are lucky, the constant will be 0 or 1 2048 in the short type, making the entire operation go away. */ 2049 if (TREE_CODE (op0) == INTEGER_CST 2050 && TREE_CODE (op1) == NOP_EXPR 2051 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))) 2052 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0)))) 2053 { 2054 final_type = result_type; 2055 op1 = TREE_OPERAND (op1, 0); 2056 result_type = TREE_TYPE (op1); 2057 } 2058 if (TREE_CODE (op1) == INTEGER_CST 2059 && TREE_CODE (op0) == NOP_EXPR 2060 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))) 2061 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) 2062 { 2063 final_type = result_type; 2064 op0 = TREE_OPERAND (op0, 0); 2065 result_type = TREE_TYPE (op0); 2066 } 2067 break; 2068 2069 case TRUNC_MOD_EXPR: 2070 case FLOOR_MOD_EXPR: 2071 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2072 { 2073 /* Although it would be tempting to shorten always here, that loses 2074 on some targets, since the modulo instruction is undefined if the 2075 quotient can't be represented in the computation mode. We shorten 2076 only if unsigned or if dividing by something we know != -1. */ 2077 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0)) 2078 || (TREE_CODE (op1) == INTEGER_CST 2079 && (TREE_INT_CST_LOW (op1) != -1 2080 || TREE_INT_CST_HIGH (op1) != -1))); 2081 common = 1; 2082 } 2083 break; 2084 2085 case TRUTH_ANDIF_EXPR: 2086 case TRUTH_ORIF_EXPR: 2087 case TRUTH_AND_EXPR: 2088 case TRUTH_OR_EXPR: 2089 case TRUTH_XOR_EXPR: 2090 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE 2091 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) 2092 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE 2093 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) 2094 { 2095 /* Result of these operations is always an int, 2096 but that does not mean the operands should be 2097 converted to ints! */ 2098 result_type = integer_type_node; 2099 op0 = truthvalue_conversion (op0); 2100 op1 = truthvalue_conversion (op1); 2101 converted = 1; 2102 } 2103 break; 2104 2105 /* Shift operations: result has same type as first operand; 2106 always convert second operand to int. 2107 Also set SHORT_SHIFT if shifting rightward. */ 2108 2109 case RSHIFT_EXPR: 2110 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2111 { 2112 if (TREE_CODE (op1) == INTEGER_CST) 2113 { 2114 if (tree_int_cst_sgn (op1) < 0) 2115 warning ("right shift count is negative"); 2116 else 2117 { 2118 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1)) 2119 short_shift = 1; 2120 if (TREE_INT_CST_HIGH (op1) != 0 2121 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) 2122 >= TYPE_PRECISION (type0))) 2123 warning ("right shift count >= width of type"); 2124 } 2125 } 2126 /* Use the type of the value to be shifted. 2127 This is what most traditional C compilers do. */ 2128 result_type = type0; 2129 /* Unless traditional, convert the shift-count to an integer, 2130 regardless of size of value being shifted. */ 2131 if (! flag_traditional) 2132 { 2133 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 2134 op1 = convert (integer_type_node, op1); 2135 /* Avoid converting op1 to result_type later. */ 2136 converted = 1; 2137 } 2138 } 2139 break; 2140 2141 case LSHIFT_EXPR: 2142 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2143 { 2144 if (TREE_CODE (op1) == INTEGER_CST) 2145 { 2146 if (tree_int_cst_sgn (op1) < 0) 2147 warning ("left shift count is negative"); 2148 else if (TREE_INT_CST_HIGH (op1) != 0 2149 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) 2150 >= TYPE_PRECISION (type0))) 2151 warning ("left shift count >= width of type"); 2152 } 2153 /* Use the type of the value to be shifted. 2154 This is what most traditional C compilers do. */ 2155 result_type = type0; 2156 /* Unless traditional, convert the shift-count to an integer, 2157 regardless of size of value being shifted. */ 2158 if (! flag_traditional) 2159 { 2160 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 2161 op1 = convert (integer_type_node, op1); 2162 /* Avoid converting op1 to result_type later. */ 2163 converted = 1; 2164 } 2165 } 2166 break; 2167 2168 case RROTATE_EXPR: 2169 case LROTATE_EXPR: 2170 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2171 { 2172 if (TREE_CODE (op1) == INTEGER_CST) 2173 { 2174 if (tree_int_cst_sgn (op1) < 0) 2175 warning ("shift count is negative"); 2176 else if (TREE_INT_CST_HIGH (op1) != 0 2177 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) 2178 >= TYPE_PRECISION (type0))) 2179 warning ("shift count >= width of type"); 2180 } 2181 /* Use the type of the value to be shifted. 2182 This is what most traditional C compilers do. */ 2183 result_type = type0; 2184 /* Unless traditional, convert the shift-count to an integer, 2185 regardless of size of value being shifted. */ 2186 if (! flag_traditional) 2187 { 2188 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 2189 op1 = convert (integer_type_node, op1); 2190 /* Avoid converting op1 to result_type later. */ 2191 converted = 1; 2192 } 2193 } 2194 break; 2195 2196 case EQ_EXPR: 2197 case NE_EXPR: 2198 /* Result of comparison is always int, 2199 but don't convert the args to int! */ 2200 build_type = integer_type_node; 2201 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 2202 || code0 == COMPLEX_TYPE) 2203 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 2204 || code1 == COMPLEX_TYPE)) 2205 short_compare = 1; 2206 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 2207 { 2208 register tree tt0 = TREE_TYPE (type0); 2209 register tree tt1 = TREE_TYPE (type1); 2210 /* Anything compares with void *. void * compares with anything. 2211 Otherwise, the targets must be compatible 2212 and both must be object or both incomplete. */ 2213 if (comp_target_types (type0, type1)) 2214 result_type = common_type (type0, type1); 2215 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node) 2216 { 2217 /* op0 != orig_op0 detects the case of something 2218 whose value is 0 but which isn't a valid null ptr const. */ 2219 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0) 2220 && TREE_CODE (tt1) == FUNCTION_TYPE) 2221 pedwarn ("ANSI C forbids comparison of `void *' with function pointer"); 2222 } 2223 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node) 2224 { 2225 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1) 2226 && TREE_CODE (tt0) == FUNCTION_TYPE) 2227 pedwarn ("ANSI C forbids comparison of `void *' with function pointer"); 2228 } 2229 else 2230 pedwarn ("comparison of distinct pointer types lacks a cast"); 2231 2232 if (result_type == NULL_TREE) 2233 result_type = ptr_type_node; 2234 } 2235 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST 2236 && integer_zerop (op1)) 2237 result_type = type0; 2238 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST 2239 && integer_zerop (op0)) 2240 result_type = type1; 2241 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2242 { 2243 result_type = type0; 2244 if (! flag_traditional) 2245 pedwarn ("comparison between pointer and integer"); 2246 } 2247 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) 2248 { 2249 result_type = type1; 2250 if (! flag_traditional) 2251 pedwarn ("comparison between pointer and integer"); 2252 } 2253 break; 2254 2255 case MAX_EXPR: 2256 case MIN_EXPR: 2257 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) 2258 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) 2259 shorten = 1; 2260 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 2261 { 2262 if (comp_target_types (type0, type1)) 2263 { 2264 result_type = common_type (type0, type1); 2265 if (pedantic 2266 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) 2267 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions"); 2268 } 2269 else 2270 { 2271 result_type = ptr_type_node; 2272 pedwarn ("comparison of distinct pointer types lacks a cast"); 2273 } 2274 } 2275 break; 2276 2277 case LE_EXPR: 2278 case GE_EXPR: 2279 case LT_EXPR: 2280 case GT_EXPR: 2281 build_type = integer_type_node; 2282 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) 2283 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) 2284 short_compare = 1; 2285 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 2286 { 2287 if (comp_target_types (type0, type1)) 2288 { 2289 result_type = common_type (type0, type1); 2290 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0) 2291 != (TYPE_SIZE (TREE_TYPE (type1)) != 0)) 2292 pedwarn ("comparison of complete and incomplete pointers"); 2293 else if (pedantic 2294 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) 2295 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions"); 2296 } 2297 else 2298 { 2299 result_type = ptr_type_node; 2300 pedwarn ("comparison of distinct pointer types lacks a cast"); 2301 } 2302 } 2303 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST 2304 && integer_zerop (op1)) 2305 { 2306 result_type = type0; 2307 if (pedantic || extra_warnings) 2308 pedwarn ("ordered comparison of pointer with integer zero"); 2309 } 2310 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST 2311 && integer_zerop (op0)) 2312 { 2313 result_type = type1; 2314 if (pedantic) 2315 pedwarn ("ordered comparison of pointer with integer zero"); 2316 } 2317 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2318 { 2319 result_type = type0; 2320 if (! flag_traditional) 2321 pedwarn ("comparison between pointer and integer"); 2322 } 2323 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) 2324 { 2325 result_type = type1; 2326 if (! flag_traditional) 2327 pedwarn ("comparison between pointer and integer"); 2328 } 2329 break; 2330 } 2331 2332 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) 2333 && 2334 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) 2335 { 2336 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE); 2337 2338 if (shorten || common || short_compare) 2339 result_type = common_type (type0, type1); 2340 2341 /* For certain operations (which identify themselves by shorten != 0) 2342 if both args were extended from the same smaller type, 2343 do the arithmetic in that type and then extend. 2344 2345 shorten !=0 and !=1 indicates a bitwise operation. 2346 For them, this optimization is safe only if 2347 both args are zero-extended or both are sign-extended. 2348 Otherwise, we might change the result. 2349 Eg, (short)-1 | (unsigned short)-1 is (int)-1 2350 but calculated in (unsigned short) it would be (unsigned short)-1. */ 2351 2352 if (shorten && none_complex) 2353 { 2354 int unsigned0, unsigned1; 2355 tree arg0 = get_narrower (op0, &unsigned0); 2356 tree arg1 = get_narrower (op1, &unsigned1); 2357 /* UNS is 1 if the operation to be done is an unsigned one. */ 2358 int uns = TREE_UNSIGNED (result_type); 2359 tree type; 2360 2361 final_type = result_type; 2362 2363 /* Handle the case that OP0 (or OP1) does not *contain* a conversion 2364 but it *requires* conversion to FINAL_TYPE. */ 2365 2366 if ((TYPE_PRECISION (TREE_TYPE (op0)) 2367 == TYPE_PRECISION (TREE_TYPE (arg0))) 2368 && TREE_TYPE (op0) != final_type) 2369 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0)); 2370 if ((TYPE_PRECISION (TREE_TYPE (op1)) 2371 == TYPE_PRECISION (TREE_TYPE (arg1))) 2372 && TREE_TYPE (op1) != final_type) 2373 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1)); 2374 2375 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ 2376 2377 /* For bitwise operations, signedness of nominal type 2378 does not matter. Consider only how operands were extended. */ 2379 if (shorten == -1) 2380 uns = unsigned0; 2381 2382 /* Note that in all three cases below we refrain from optimizing 2383 an unsigned operation on sign-extended args. 2384 That would not be valid. */ 2385 2386 /* Both args variable: if both extended in same way 2387 from same width, do it in that width. 2388 Do it unsigned if args were zero-extended. */ 2389 if ((TYPE_PRECISION (TREE_TYPE (arg0)) 2390 < TYPE_PRECISION (result_type)) 2391 && (TYPE_PRECISION (TREE_TYPE (arg1)) 2392 == TYPE_PRECISION (TREE_TYPE (arg0))) 2393 && unsigned0 == unsigned1 2394 && (unsigned0 || !uns)) 2395 result_type 2396 = signed_or_unsigned_type (unsigned0, 2397 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); 2398 else if (TREE_CODE (arg0) == INTEGER_CST 2399 && (unsigned1 || !uns) 2400 && (TYPE_PRECISION (TREE_TYPE (arg1)) 2401 < TYPE_PRECISION (result_type)) 2402 && (type = signed_or_unsigned_type (unsigned1, 2403 TREE_TYPE (arg1)), 2404 int_fits_type_p (arg0, type))) 2405 result_type = type; 2406 else if (TREE_CODE (arg1) == INTEGER_CST 2407 && (unsigned0 || !uns) 2408 && (TYPE_PRECISION (TREE_TYPE (arg0)) 2409 < TYPE_PRECISION (result_type)) 2410 && (type = signed_or_unsigned_type (unsigned0, 2411 TREE_TYPE (arg0)), 2412 int_fits_type_p (arg1, type))) 2413 result_type = type; 2414 } 2415 2416 /* Shifts can be shortened if shifting right. */ 2417 2418 if (short_shift) 2419 { 2420 int unsigned_arg; 2421 tree arg0 = get_narrower (op0, &unsigned_arg); 2422 2423 final_type = result_type; 2424 2425 if (arg0 == op0 && final_type == TREE_TYPE (op0)) 2426 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0)); 2427 2428 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) 2429 /* We can shorten only if the shift count is less than the 2430 number of bits in the smaller type size. */ 2431 && TREE_INT_CST_HIGH (op1) == 0 2432 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1) 2433 /* If arg is sign-extended and then unsigned-shifted, 2434 we can simulate this with a signed shift in arg's type 2435 only if the extended result is at least twice as wide 2436 as the arg. Otherwise, the shift could use up all the 2437 ones made by sign-extension and bring in zeros. 2438 We can't optimize that case at all, but in most machines 2439 it never happens because available widths are 2**N. */ 2440 && (!TREE_UNSIGNED (final_type) 2441 || unsigned_arg 2442 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type))) 2443 { 2444 /* Do an unsigned shift if the operand was zero-extended. */ 2445 result_type 2446 = signed_or_unsigned_type (unsigned_arg, 2447 TREE_TYPE (arg0)); 2448 /* Convert value-to-be-shifted to that type. */ 2449 if (TREE_TYPE (op0) != result_type) 2450 op0 = convert (result_type, op0); 2451 converted = 1; 2452 } 2453 } 2454 2455 /* Comparison operations are shortened too but differently. 2456 They identify themselves by setting short_compare = 1. */ 2457 2458 if (short_compare) 2459 { 2460 /* Don't write &op0, etc., because that would prevent op0 2461 from being kept in a register. 2462 Instead, make copies of the our local variables and 2463 pass the copies by reference, then copy them back afterward. */ 2464 tree xop0 = op0, xop1 = op1, xresult_type = result_type; 2465 enum tree_code xresultcode = resultcode; 2466 tree val 2467 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); 2468 if (val != 0) 2469 return val; 2470 op0 = xop0, op1 = xop1; 2471 converted = 1; 2472 resultcode = xresultcode; 2473 2474 if (extra_warnings) 2475 { 2476 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0)); 2477 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1)); 2478 2479 int unsignedp0, unsignedp1; 2480 tree primop0 = get_narrower (op0, &unsignedp0); 2481 tree primop1 = get_narrower (op1, &unsignedp1); 2482 2483 /* Avoid spurious warnings for comparison with enumerators. */ 2484 2485 xop0 = orig_op0; 2486 xop1 = orig_op1; 2487 STRIP_TYPE_NOPS (xop0); 2488 STRIP_TYPE_NOPS (xop1); 2489 2490 /* Give warnings for comparisons between signed and unsigned 2491 quantities that may fail. */ 2492 /* Do the checking based on the original operand trees, so that 2493 casts will be considered, but default promotions won't be. */ 2494 2495 /* Do not warn if the comparison is being done in a signed type, 2496 since the signed type will only be chosen if it can represent 2497 all the values of the unsigned type. */ 2498 if (! TREE_UNSIGNED (result_type)) 2499 /* OK */; 2500 /* Do not warn if both operands are unsigned. */ 2501 else if (op0_signed == op1_signed) 2502 /* OK */; 2503 /* Do not warn if the signed quantity is an unsuffixed 2504 integer literal (or some static constant expression 2505 involving such literals) and it is non-negative. */ 2506 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST 2507 && tree_int_cst_sgn (xop0) >= 0) 2508 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST 2509 && tree_int_cst_sgn (xop1) >= 0)) 2510 /* OK */; 2511 /* Do not warn if the comparison is an equality operation, 2512 the unsigned quantity is an integral constant and it does 2513 not use the most significant bit of result_type. */ 2514 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR) 2515 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST 2516 && int_fits_type_p (xop1, signed_type (result_type)) 2517 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST 2518 && int_fits_type_p (xop0, signed_type (result_type)))))) 2519 /* OK */; 2520 else 2521 warning ("comparison between signed and unsigned"); 2522 2523 /* Warn if two unsigned values are being compared in a size 2524 larger than their original size, and one (and only one) is the 2525 result of a `~' operator. This comparison will always fail. 2526 2527 Also warn if one operand is a constant, and the constant 2528 does not have all bits set that are set in the ~ operand 2529 when it is extended. */ 2530 2531 if ((TREE_CODE (primop0) == BIT_NOT_EXPR) 2532 != (TREE_CODE (primop1) == BIT_NOT_EXPR)) 2533 { 2534 if (TREE_CODE (primop0) == BIT_NOT_EXPR) 2535 primop0 = get_narrower (TREE_OPERAND (primop0, 0), 2536 &unsignedp0); 2537 else 2538 primop1 = get_narrower (TREE_OPERAND (primop1, 0), 2539 &unsignedp1); 2540 2541 if (TREE_CODE (primop0) == INTEGER_CST 2542 || TREE_CODE (primop1) == INTEGER_CST) 2543 { 2544 tree primop; 2545 long constant, mask; 2546 int unsignedp, bits; 2547 2548 if (TREE_CODE (primop0) == INTEGER_CST) 2549 { 2550 primop = primop1; 2551 unsignedp = unsignedp1; 2552 constant = TREE_INT_CST_LOW (primop0); 2553 } 2554 else 2555 { 2556 primop = primop0; 2557 unsignedp = unsignedp0; 2558 constant = TREE_INT_CST_LOW (primop1); 2559 } 2560 2561 bits = TYPE_PRECISION (TREE_TYPE (primop)); 2562 if (bits < TYPE_PRECISION (result_type) 2563 && bits < HOST_BITS_PER_LONG && unsignedp) 2564 { 2565 mask = (~0L) << bits; 2566 if ((mask & constant) != mask) 2567 warning ("comparison of promoted ~unsigned with constant"); 2568 } 2569 } 2570 else if (unsignedp0 && unsignedp1 2571 && (TYPE_PRECISION (TREE_TYPE (primop0)) 2572 < TYPE_PRECISION (result_type)) 2573 && (TYPE_PRECISION (TREE_TYPE (primop1)) 2574 < TYPE_PRECISION (result_type))) 2575 warning ("comparison of promoted ~unsigned with unsigned"); 2576 } 2577 } 2578 } 2579 } 2580 2581 /* At this point, RESULT_TYPE must be nonzero to avoid an error message. 2582 If CONVERTED is zero, both args will be converted to type RESULT_TYPE. 2583 Then the expression will be built. 2584 It will be given type FINAL_TYPE if that is nonzero; 2585 otherwise, it will be given type RESULT_TYPE. */ 2586 2587 if (!result_type) 2588 { 2589 binary_op_error (code); 2590 return error_mark_node; 2591 } 2592 2593 if (! converted) 2594 { 2595 if (TREE_TYPE (op0) != result_type) 2596 op0 = convert (result_type, op0); 2597 if (TREE_TYPE (op1) != result_type) 2598 op1 = convert (result_type, op1); 2599 } 2600 2601 if (build_type == NULL_TREE) 2602 build_type = result_type; 2603 2604 { 2605 register tree result = build (resultcode, build_type, op0, op1); 2606 register tree folded; 2607 2608 folded = fold (result); 2609 if (folded == result) 2610 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); 2611 if (final_type != 0) 2612 return convert (final_type, folded); 2613 return folded; 2614 } 2615} 2616 2617/* Return a tree for the sum or difference (RESULTCODE says which) 2618 of pointer PTROP and integer INTOP. */ 2619 2620static tree 2621pointer_int_sum (resultcode, ptrop, intop) 2622 enum tree_code resultcode; 2623 register tree ptrop, intop; 2624{ 2625 tree size_exp; 2626 2627 register tree result; 2628 register tree folded; 2629 2630 /* The result is a pointer of the same type that is being added. */ 2631 2632 register tree result_type = TREE_TYPE (ptrop); 2633 2634 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) 2635 { 2636 if (pedantic || warn_pointer_arith) 2637 pedwarn ("pointer of type `void *' used in arithmetic"); 2638 size_exp = integer_one_node; 2639 } 2640 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) 2641 { 2642 if (pedantic || warn_pointer_arith) 2643 pedwarn ("pointer to a function used in arithmetic"); 2644 size_exp = integer_one_node; 2645 } 2646 else 2647 size_exp = c_size_in_bytes (TREE_TYPE (result_type)); 2648 2649 /* If what we are about to multiply by the size of the elements 2650 contains a constant term, apply distributive law 2651 and multiply that constant term separately. 2652 This helps produce common subexpressions. */ 2653 2654 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) 2655 && ! TREE_CONSTANT (intop) 2656 && TREE_CONSTANT (TREE_OPERAND (intop, 1)) 2657 && TREE_CONSTANT (size_exp) 2658 /* If the constant comes from pointer subtraction, 2659 skip this optimization--it would cause an error. */ 2660 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE 2661 /* If the constant is unsigned, and smaller than the pointer size, 2662 then we must skip this optimization. This is because it could cause 2663 an overflow error if the constant is negative but INTOP is not. */ 2664 && (! TREE_UNSIGNED (TREE_TYPE (intop)) 2665 || (TYPE_PRECISION (TREE_TYPE (intop)) 2666 == TYPE_PRECISION (TREE_TYPE (ptrop))))) 2667 { 2668 enum tree_code subcode = resultcode; 2669 tree int_type = TREE_TYPE (intop); 2670 if (TREE_CODE (intop) == MINUS_EXPR) 2671 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); 2672 /* Convert both subexpression types to the type of intop, 2673 because weird cases involving pointer arithmetic 2674 can result in a sum or difference with different type args. */ 2675 ptrop = build_binary_op (subcode, ptrop, 2676 convert (int_type, TREE_OPERAND (intop, 1)), 1); 2677 intop = convert (int_type, TREE_OPERAND (intop, 0)); 2678 } 2679 2680 /* Convert the integer argument to a type the same size as a pointer 2681 so the multiply won't overflow spuriously. */ 2682 2683 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE) 2684 intop = convert (type_for_size (POINTER_SIZE, 0), intop); 2685 2686 /* Replace the integer argument with a suitable product by the object size. 2687 Do this multiplication as signed, then convert to the appropriate 2688 pointer type (actually unsigned integral). */ 2689 2690 intop = convert (result_type, 2691 build_binary_op (MULT_EXPR, intop, 2692 convert (TREE_TYPE (intop), size_exp), 1)); 2693 2694 /* Create the sum or difference. */ 2695 2696 result = build (resultcode, result_type, ptrop, intop); 2697 2698 folded = fold (result); 2699 if (folded == result) 2700 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop); 2701 return folded; 2702} 2703 2704/* Return a tree for the difference of pointers OP0 and OP1. 2705 The resulting tree has type int. */ 2706 2707static tree 2708pointer_diff (op0, op1) 2709 register tree op0, op1; 2710{ 2711 register tree result, folded; 2712 tree restype = ptrdiff_type_node; 2713 2714 tree target_type = TREE_TYPE (TREE_TYPE (op0)); 2715 2716 if (pedantic || warn_pointer_arith) 2717 { 2718 if (TREE_CODE (target_type) == VOID_TYPE) 2719 pedwarn ("pointer of type `void *' used in subtraction"); 2720 if (TREE_CODE (target_type) == FUNCTION_TYPE) 2721 pedwarn ("pointer to a function used in subtraction"); 2722 } 2723 2724 /* First do the subtraction as integers; 2725 then drop through to build the divide operator. */ 2726 2727 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0), 2728 convert (restype, op1), 1); 2729 /* This generates an error if op1 is pointer to incomplete type. */ 2730 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0) 2731 error ("arithmetic on pointer to an incomplete type"); 2732 2733 /* This generates an error if op0 is pointer to incomplete type. */ 2734 op1 = c_size_in_bytes (target_type); 2735 2736 /* Divide by the size, in easiest possible way. */ 2737 2738 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)); 2739 2740 folded = fold (result); 2741 if (folded == result) 2742 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); 2743 return folded; 2744} 2745 2746/* Construct and perhaps optimize a tree representation 2747 for a unary operation. CODE, a tree_code, specifies the operation 2748 and XARG is the operand. NOCONVERT nonzero suppresses 2749 the default promotions (such as from short to int). */ 2750 2751tree 2752build_unary_op (code, xarg, noconvert) 2753 enum tree_code code; 2754 tree xarg; 2755 int noconvert; 2756{ 2757 /* No default_conversion here. It causes trouble for ADDR_EXPR. */ 2758 register tree arg = xarg; 2759 register tree argtype = 0; 2760 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg)); 2761 char *errstring = NULL; 2762 tree val; 2763 2764 if (typecode == ERROR_MARK) 2765 return error_mark_node; 2766 if (typecode == ENUMERAL_TYPE) 2767 typecode = INTEGER_TYPE; 2768 2769 switch (code) 2770 { 2771 case CONVERT_EXPR: 2772 /* This is used for unary plus, because a CONVERT_EXPR 2773 is enough to prevent anybody from looking inside for 2774 associativity, but won't generate any code. */ 2775 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 2776 || typecode == COMPLEX_TYPE)) 2777 errstring = "wrong type argument to unary plus"; 2778 else if (!noconvert) 2779 arg = default_conversion (arg); 2780 break; 2781 2782 case NEGATE_EXPR: 2783 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 2784 || typecode == COMPLEX_TYPE)) 2785 errstring = "wrong type argument to unary minus"; 2786 else if (!noconvert) 2787 arg = default_conversion (arg); 2788 break; 2789 2790 case BIT_NOT_EXPR: 2791 if (typecode == COMPLEX_TYPE) 2792 { 2793 code = CONJ_EXPR; 2794 if (!noconvert) 2795 arg = default_conversion (arg); 2796 } 2797 else if (typecode != INTEGER_TYPE) 2798 errstring = "wrong type argument to bit-complement"; 2799 else if (!noconvert) 2800 arg = default_conversion (arg); 2801 break; 2802 2803 case ABS_EXPR: 2804 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 2805 || typecode == COMPLEX_TYPE)) 2806 errstring = "wrong type argument to abs"; 2807 else if (!noconvert) 2808 arg = default_conversion (arg); 2809 break; 2810 2811 case CONJ_EXPR: 2812 /* Conjugating a real value is a no-op, but allow it anyway. */ 2813 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 2814 || typecode == COMPLEX_TYPE)) 2815 errstring = "wrong type argument to conjugation"; 2816 else if (!noconvert) 2817 arg = default_conversion (arg); 2818 break; 2819 2820 case TRUTH_NOT_EXPR: 2821 if (typecode != INTEGER_TYPE 2822 && typecode != REAL_TYPE && typecode != POINTER_TYPE 2823 && typecode != COMPLEX_TYPE 2824 /* These will convert to a pointer. */ 2825 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE) 2826 { 2827 errstring = "wrong type argument to unary exclamation mark"; 2828 break; 2829 } 2830 arg = truthvalue_conversion (arg); 2831 return invert_truthvalue (arg); 2832 2833 case NOP_EXPR: 2834 break; 2835 2836 case REALPART_EXPR: 2837 if (TREE_CODE (arg) == COMPLEX_CST) 2838 return TREE_REALPART (arg); 2839 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) 2840 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); 2841 else 2842 return arg; 2843 2844 case IMAGPART_EXPR: 2845 if (TREE_CODE (arg) == COMPLEX_CST) 2846 return TREE_IMAGPART (arg); 2847 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) 2848 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); 2849 else 2850 return convert (TREE_TYPE (arg), integer_zero_node); 2851 2852 case PREINCREMENT_EXPR: 2853 case POSTINCREMENT_EXPR: 2854 case PREDECREMENT_EXPR: 2855 case POSTDECREMENT_EXPR: 2856 /* Handle complex lvalues (when permitted) 2857 by reduction to simpler cases. */ 2858 2859 val = unary_complex_lvalue (code, arg); 2860 if (val != 0) 2861 return val; 2862 2863 /* Increment or decrement the real part of the value, 2864 and don't change the imaginary part. */ 2865 if (typecode == COMPLEX_TYPE) 2866 { 2867 tree real, imag; 2868 2869 arg = stabilize_reference (arg); 2870 real = build_unary_op (REALPART_EXPR, arg, 1); 2871 imag = build_unary_op (IMAGPART_EXPR, arg, 1); 2872 return build (COMPLEX_EXPR, TREE_TYPE (arg), 2873 build_unary_op (code, real, 1), imag); 2874 } 2875 2876 /* Report invalid types. */ 2877 2878 if (typecode != POINTER_TYPE 2879 && typecode != INTEGER_TYPE && typecode != REAL_TYPE) 2880 { 2881 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 2882 errstring ="wrong type argument to increment"; 2883 else 2884 errstring ="wrong type argument to decrement"; 2885 break; 2886 } 2887 2888 { 2889 register tree inc; 2890 tree result_type = TREE_TYPE (arg); 2891 2892 arg = get_unwidened (arg, 0); 2893 argtype = TREE_TYPE (arg); 2894 2895 /* Compute the increment. */ 2896 2897 if (typecode == POINTER_TYPE) 2898 { 2899 /* If pointer target is an undefined struct, 2900 we just cannot know how to do the arithmetic. */ 2901 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0) 2902 error ("%s of pointer to unknown structure", 2903 ((code == PREINCREMENT_EXPR 2904 || code == POSTINCREMENT_EXPR) 2905 ? "increment" : "decrement")); 2906 else if ((pedantic || warn_pointer_arith) 2907 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE 2908 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)) 2909 pedwarn ("wrong type argument to %s", 2910 ((code == PREINCREMENT_EXPR 2911 || code == POSTINCREMENT_EXPR) 2912 ? "increment" : "decrement")); 2913 inc = c_size_in_bytes (TREE_TYPE (result_type)); 2914 } 2915 else 2916 inc = integer_one_node; 2917 2918 inc = convert (argtype, inc); 2919 2920 /* Handle incrementing a cast-expression. */ 2921 2922 while (1) 2923 switch (TREE_CODE (arg)) 2924 { 2925 case NOP_EXPR: 2926 case CONVERT_EXPR: 2927 case FLOAT_EXPR: 2928 case FIX_TRUNC_EXPR: 2929 case FIX_FLOOR_EXPR: 2930 case FIX_ROUND_EXPR: 2931 case FIX_CEIL_EXPR: 2932 pedantic_lvalue_warning (CONVERT_EXPR); 2933 /* If the real type has the same machine representation 2934 as the type it is cast to, we can make better output 2935 by adding directly to the inside of the cast. */ 2936 if ((TREE_CODE (TREE_TYPE (arg)) 2937 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0)))) 2938 && (TYPE_MODE (TREE_TYPE (arg)) 2939 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0))))) 2940 arg = TREE_OPERAND (arg, 0); 2941 else 2942 { 2943 tree incremented, modify, value; 2944 arg = stabilize_reference (arg); 2945 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) 2946 value = arg; 2947 else 2948 value = save_expr (arg); 2949 incremented = build (((code == PREINCREMENT_EXPR 2950 || code == POSTINCREMENT_EXPR) 2951 ? PLUS_EXPR : MINUS_EXPR), 2952 argtype, value, inc); 2953 TREE_SIDE_EFFECTS (incremented) = 1; 2954 modify = build_modify_expr (arg, NOP_EXPR, incremented); 2955 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value); 2956 TREE_USED (value) = 1; 2957 return value; 2958 } 2959 break; 2960 2961 default: 2962 goto give_up; 2963 } 2964 give_up: 2965 2966 /* Complain about anything else that is not a true lvalue. */ 2967 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR 2968 || code == POSTINCREMENT_EXPR) 2969 ? "increment" : "decrement"))) 2970 return error_mark_node; 2971 2972 /* Report a read-only lvalue. */ 2973 if (TREE_READONLY (arg)) 2974 readonly_warning (arg, 2975 ((code == PREINCREMENT_EXPR 2976 || code == POSTINCREMENT_EXPR) 2977 ? "increment" : "decrement")); 2978 2979 val = build (code, TREE_TYPE (arg), arg, inc); 2980 TREE_SIDE_EFFECTS (val) = 1; 2981 val = convert (result_type, val); 2982 if (TREE_CODE (val) != code) 2983 TREE_NO_UNUSED_WARNING (val) = 1; 2984 return val; 2985 } 2986 2987 case ADDR_EXPR: 2988 /* Note that this operation never does default_conversion 2989 regardless of NOCONVERT. */ 2990 2991 /* Let &* cancel out to simplify resulting code. */ 2992 if (TREE_CODE (arg) == INDIRECT_REF) 2993 { 2994 /* Don't let this be an lvalue. */ 2995 if (lvalue_p (TREE_OPERAND (arg, 0))) 2996 return non_lvalue (TREE_OPERAND (arg, 0)); 2997 return TREE_OPERAND (arg, 0); 2998 } 2999 3000 /* For &x[y], return x+y */ 3001 if (TREE_CODE (arg) == ARRAY_REF) 3002 { 3003 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0) 3004 return error_mark_node; 3005 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0), 3006 TREE_OPERAND (arg, 1), 1); 3007 } 3008 3009 /* Handle complex lvalues (when permitted) 3010 by reduction to simpler cases. */ 3011 val = unary_complex_lvalue (code, arg); 3012 if (val != 0) 3013 return val; 3014 3015#if 0 /* Turned off because inconsistent; 3016 float f; *&(int)f = 3.4 stores in int format 3017 whereas (int)f = 3.4 stores in float format. */ 3018 /* Address of a cast is just a cast of the address 3019 of the operand of the cast. */ 3020 switch (TREE_CODE (arg)) 3021 { 3022 case NOP_EXPR: 3023 case CONVERT_EXPR: 3024 case FLOAT_EXPR: 3025 case FIX_TRUNC_EXPR: 3026 case FIX_FLOOR_EXPR: 3027 case FIX_ROUND_EXPR: 3028 case FIX_CEIL_EXPR: 3029 if (pedantic) 3030 pedwarn ("ANSI C forbids the address of a cast expression"); 3031 return convert (build_pointer_type (TREE_TYPE (arg)), 3032 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 3033 0)); 3034 } 3035#endif 3036 3037 /* Allow the address of a constructor if all the elements 3038 are constant. */ 3039 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg)) 3040 ; 3041 /* Anything not already handled and not a true memory reference 3042 is an error. */ 3043 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'")) 3044 return error_mark_node; 3045 3046 /* Ordinary case; arg is a COMPONENT_REF or a decl. */ 3047 argtype = TREE_TYPE (arg); 3048 /* If the lvalue is const or volatile, 3049 merge that into the type that the address will point to. */ 3050 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd' 3051 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r') 3052 { 3053 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) 3054 argtype = c_build_type_variant (argtype, 3055 TREE_READONLY (arg), 3056 TREE_THIS_VOLATILE (arg)); 3057 } 3058 3059 argtype = build_pointer_type (argtype); 3060 3061 if (mark_addressable (arg) == 0) 3062 return error_mark_node; 3063 3064 { 3065 tree addr; 3066 3067 if (TREE_CODE (arg) == COMPONENT_REF) 3068 { 3069 tree field = TREE_OPERAND (arg, 1); 3070 3071 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0); 3072 3073 if (DECL_BIT_FIELD (field)) 3074 { 3075 error ("attempt to take address of bit-field structure member `%s'", 3076 IDENTIFIER_POINTER (DECL_NAME (field))); 3077 return error_mark_node; 3078 } 3079 3080 addr = convert (argtype, addr); 3081 3082 if (! integer_zerop (DECL_FIELD_BITPOS (field))) 3083 { 3084 tree offset 3085 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field), 3086 size_int (BITS_PER_UNIT)); 3087 int flag = TREE_CONSTANT (addr); 3088 addr = fold (build (PLUS_EXPR, argtype, 3089 addr, convert (argtype, offset))); 3090 TREE_CONSTANT (addr) = flag; 3091 } 3092 } 3093 else 3094 addr = build1 (code, argtype, arg); 3095 3096 /* Address of a static or external variable or 3097 file-scope function counts as a constant. */ 3098 if (staticp (arg) 3099 && ! (TREE_CODE (arg) == FUNCTION_DECL 3100 && DECL_CONTEXT (arg) != 0)) 3101 TREE_CONSTANT (addr) = 1; 3102 return addr; 3103 } 3104 } 3105 3106 if (!errstring) 3107 { 3108 if (argtype == 0) 3109 argtype = TREE_TYPE (arg); 3110 return fold (build1 (code, argtype, arg)); 3111 } 3112 3113 error (errstring); 3114 return error_mark_node; 3115} 3116 3117#if 0 3118/* If CONVERSIONS is a conversion expression or a nested sequence of such, 3119 convert ARG with the same conversions in the same order 3120 and return the result. */ 3121 3122static tree 3123convert_sequence (conversions, arg) 3124 tree conversions; 3125 tree arg; 3126{ 3127 switch (TREE_CODE (conversions)) 3128 { 3129 case NOP_EXPR: 3130 case CONVERT_EXPR: 3131 case FLOAT_EXPR: 3132 case FIX_TRUNC_EXPR: 3133 case FIX_FLOOR_EXPR: 3134 case FIX_ROUND_EXPR: 3135 case FIX_CEIL_EXPR: 3136 return convert (TREE_TYPE (conversions), 3137 convert_sequence (TREE_OPERAND (conversions, 0), 3138 arg)); 3139 3140 default: 3141 return arg; 3142 } 3143} 3144#endif /* 0 */ 3145 3146/* Return nonzero if REF is an lvalue valid for this language. 3147 Lvalues can be assigned, unless their type has TYPE_READONLY. 3148 Lvalues can have their address taken, unless they have DECL_REGISTER. */ 3149 3150int 3151lvalue_p (ref) 3152 tree ref; 3153{ 3154 register enum tree_code code = TREE_CODE (ref); 3155 3156 switch (code) 3157 { 3158 case REALPART_EXPR: 3159 case IMAGPART_EXPR: 3160 case COMPONENT_REF: 3161 return lvalue_p (TREE_OPERAND (ref, 0)); 3162 3163 case STRING_CST: 3164 return 1; 3165 3166 case INDIRECT_REF: 3167 case ARRAY_REF: 3168 case VAR_DECL: 3169 case PARM_DECL: 3170 case RESULT_DECL: 3171 case ERROR_MARK: 3172 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE 3173 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE) 3174 return 1; 3175 break; 3176 } 3177 return 0; 3178} 3179 3180/* Return nonzero if REF is an lvalue valid for this language; 3181 otherwise, print an error message and return zero. */ 3182 3183int 3184lvalue_or_else (ref, string) 3185 tree ref; 3186 char *string; 3187{ 3188 int win = lvalue_p (ref); 3189 if (! win) 3190 error ("invalid lvalue in %s", string); 3191 return win; 3192} 3193 3194/* Apply unary lvalue-demanding operator CODE to the expression ARG 3195 for certain kinds of expressions which are not really lvalues 3196 but which we can accept as lvalues. 3197 3198 If ARG is not a kind of expression we can handle, return zero. */ 3199 3200static tree 3201unary_complex_lvalue (code, arg) 3202 enum tree_code code; 3203 tree arg; 3204{ 3205 /* Handle (a, b) used as an "lvalue". */ 3206 if (TREE_CODE (arg) == COMPOUND_EXPR) 3207 { 3208 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0); 3209 pedantic_lvalue_warning (COMPOUND_EXPR); 3210 return build (COMPOUND_EXPR, TREE_TYPE (real_result), 3211 TREE_OPERAND (arg, 0), real_result); 3212 } 3213 3214 /* Handle (a ? b : c) used as an "lvalue". */ 3215 if (TREE_CODE (arg) == COND_EXPR) 3216 { 3217 pedantic_lvalue_warning (COND_EXPR); 3218 return (build_conditional_expr 3219 (TREE_OPERAND (arg, 0), 3220 build_unary_op (code, TREE_OPERAND (arg, 1), 0), 3221 build_unary_op (code, TREE_OPERAND (arg, 2), 0))); 3222 } 3223 3224 return 0; 3225} 3226 3227/* If pedantic, warn about improper lvalue. CODE is either COND_EXPR 3228 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */ 3229 3230static void 3231pedantic_lvalue_warning (code) 3232 enum tree_code code; 3233{ 3234 if (pedantic) 3235 pedwarn ("ANSI C forbids use of %s expressions as lvalues", 3236 code == COND_EXPR ? "conditional" 3237 : code == COMPOUND_EXPR ? "compound" : "cast"); 3238} 3239 3240/* Warn about storing in something that is `const'. */ 3241 3242void 3243readonly_warning (arg, string) 3244 tree arg; 3245 char *string; 3246{ 3247 char buf[80]; 3248 strcpy (buf, string); 3249 3250 /* Forbid assignments to iterators. */ 3251 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg)) 3252 { 3253 strcat (buf, " of iterator `%s'"); 3254 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg))); 3255 } 3256 3257 if (TREE_CODE (arg) == COMPONENT_REF) 3258 { 3259 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) 3260 readonly_warning (TREE_OPERAND (arg, 0), string); 3261 else 3262 { 3263 strcat (buf, " of read-only member `%s'"); 3264 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1)))); 3265 } 3266 } 3267 else if (TREE_CODE (arg) == VAR_DECL) 3268 { 3269 strcat (buf, " of read-only variable `%s'"); 3270 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg))); 3271 } 3272 else 3273 { 3274 pedwarn ("%s of read-only location", buf); 3275 } 3276} 3277 3278/* Mark EXP saying that we need to be able to take the 3279 address of it; it should not be allocated in a register. 3280 Value is 1 if successful. */ 3281 3282int 3283mark_addressable (exp) 3284 tree exp; 3285{ 3286 register tree x = exp; 3287 while (1) 3288 switch (TREE_CODE (x)) 3289 { 3290 case ADDR_EXPR: 3291 case COMPONENT_REF: 3292 case ARRAY_REF: 3293 case REALPART_EXPR: 3294 case IMAGPART_EXPR: 3295 x = TREE_OPERAND (x, 0); 3296 break; 3297 3298 case CONSTRUCTOR: 3299 TREE_ADDRESSABLE (x) = 1; 3300 return 1; 3301 3302 case VAR_DECL: 3303 case CONST_DECL: 3304 case PARM_DECL: 3305 case RESULT_DECL: 3306 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) 3307 && DECL_NONLOCAL (x)) 3308 { 3309 if (TREE_PUBLIC (x)) 3310 { 3311 error ("global register variable `%s' used in nested function", 3312 IDENTIFIER_POINTER (DECL_NAME (x))); 3313 return 0; 3314 } 3315 pedwarn ("register variable `%s' used in nested function", 3316 IDENTIFIER_POINTER (DECL_NAME (x))); 3317 } 3318 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)) 3319 { 3320 if (TREE_PUBLIC (x)) 3321 { 3322 error ("address of global register variable `%s' requested", 3323 IDENTIFIER_POINTER (DECL_NAME (x))); 3324 return 0; 3325 } 3326 3327 /* If we are making this addressable due to its having 3328 volatile components, give a different error message. Also 3329 handle the case of an unnamed parameter by not trying 3330 to give the name. */ 3331 3332 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x))) 3333 { 3334 error ("cannot put object with volatile field into register"); 3335 return 0; 3336 } 3337 3338 pedwarn ("address of register variable `%s' requested", 3339 IDENTIFIER_POINTER (DECL_NAME (x))); 3340 } 3341 put_var_into_stack (x); 3342 3343 /* drops in */ 3344 case FUNCTION_DECL: 3345 TREE_ADDRESSABLE (x) = 1; 3346#if 0 /* poplevel deals with this now. */ 3347 if (DECL_CONTEXT (x) == 0) 3348 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1; 3349#endif 3350 3351 default: 3352 return 1; 3353 } 3354} 3355 3356/* Build and return a conditional expression IFEXP ? OP1 : OP2. */ 3357 3358tree 3359build_conditional_expr (ifexp, op1, op2) 3360 tree ifexp, op1, op2; 3361{ 3362 register tree type1; 3363 register tree type2; 3364 register enum tree_code code1; 3365 register enum tree_code code2; 3366 register tree result_type = NULL; 3367 tree orig_op1 = op1, orig_op2 = op2; 3368 3369 /* If second operand is omitted, it is the same as the first one; 3370 make sure it is calculated only once. */ 3371 if (op1 == 0) 3372 { 3373 if (pedantic) 3374 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression"); 3375 ifexp = op1 = save_expr (ifexp); 3376 } 3377 3378 ifexp = truthvalue_conversion (default_conversion (ifexp)); 3379 3380#if 0 /* Produces wrong result if within sizeof. */ 3381 /* Don't promote the operands separately if they promote 3382 the same way. Return the unpromoted type and let the combined 3383 value get promoted if necessary. */ 3384 3385 if (TREE_TYPE (op1) == TREE_TYPE (op2) 3386 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE 3387 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE 3388 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE) 3389 { 3390 if (TREE_CODE (ifexp) == INTEGER_CST) 3391 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); 3392 3393 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2)); 3394 } 3395#endif 3396 3397 /* Promote both alternatives. */ 3398 3399 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) 3400 op1 = default_conversion (op1); 3401 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) 3402 op2 = default_conversion (op2); 3403 3404 if (TREE_CODE (ifexp) == ERROR_MARK 3405 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK 3406 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) 3407 return error_mark_node; 3408 3409 type1 = TREE_TYPE (op1); 3410 code1 = TREE_CODE (type1); 3411 type2 = TREE_TYPE (op2); 3412 code2 = TREE_CODE (type2); 3413 3414 /* Quickly detect the usual case where op1 and op2 have the same type 3415 after promotion. */ 3416 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) 3417 { 3418 if (type1 == type2) 3419 result_type = type1; 3420 else 3421 result_type = TYPE_MAIN_VARIANT (type1); 3422 } 3423 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE) 3424 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE)) 3425 { 3426 result_type = common_type (type1, type2); 3427 } 3428 else if (code1 == VOID_TYPE || code2 == VOID_TYPE) 3429 { 3430 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE)) 3431 pedwarn ("ANSI C forbids conditional expr with only one void side"); 3432 result_type = void_type_node; 3433 } 3434 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) 3435 { 3436 if (comp_target_types (type1, type2)) 3437 result_type = common_type (type1, type2); 3438 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node 3439 && TREE_CODE (orig_op1) != NOP_EXPR) 3440 result_type = qualify_type (type2, type1); 3441 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node 3442 && TREE_CODE (orig_op2) != NOP_EXPR) 3443 result_type = qualify_type (type1, type2); 3444 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node) 3445 { 3446 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) 3447 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer"); 3448 result_type = qualify_type (type1, type2); 3449 } 3450 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node) 3451 { 3452 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) 3453 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer"); 3454 result_type = qualify_type (type2, type1); 3455 } 3456 else 3457 { 3458 pedwarn ("pointer type mismatch in conditional expression"); 3459 result_type = build_pointer_type (void_type_node); 3460 } 3461 } 3462 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) 3463 { 3464 if (! integer_zerop (op2)) 3465 pedwarn ("pointer/integer type mismatch in conditional expression"); 3466 else 3467 { 3468 op2 = null_pointer_node; 3469#if 0 /* The spec seems to say this is permitted. */ 3470 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE) 3471 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer"); 3472#endif 3473 } 3474 result_type = type1; 3475 } 3476 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) 3477 { 3478 if (!integer_zerop (op1)) 3479 pedwarn ("pointer/integer type mismatch in conditional expression"); 3480 else 3481 { 3482 op1 = null_pointer_node; 3483#if 0 /* The spec seems to say this is permitted. */ 3484 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE) 3485 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer"); 3486#endif 3487 } 3488 result_type = type2; 3489 } 3490 3491 if (!result_type) 3492 { 3493 if (flag_cond_mismatch) 3494 result_type = void_type_node; 3495 else 3496 { 3497 error ("type mismatch in conditional expression"); 3498 return error_mark_node; 3499 } 3500 } 3501 3502 /* Merge const and volatile flags of the incoming types. */ 3503 result_type 3504 = build_type_variant (result_type, 3505 TREE_READONLY (op1) || TREE_READONLY (op2), 3506 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2)); 3507 3508 if (result_type != TREE_TYPE (op1)) 3509 op1 = convert_and_check (result_type, op1); 3510 if (result_type != TREE_TYPE (op2)) 3511 op2 = convert_and_check (result_type, op2); 3512 3513#if 0 3514 if (code1 == RECORD_TYPE || code1 == UNION_TYPE) 3515 { 3516 result_type = TREE_TYPE (op1); 3517 if (TREE_CONSTANT (ifexp)) 3518 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); 3519 3520 if (TYPE_MODE (result_type) == BLKmode) 3521 { 3522 register tree tempvar 3523 = build_decl (VAR_DECL, NULL_TREE, result_type); 3524 register tree xop1 = build_modify_expr (tempvar, op1); 3525 register tree xop2 = build_modify_expr (tempvar, op2); 3526 register tree result = fold (build (COND_EXPR, result_type, 3527 ifexp, xop1, xop2)); 3528 3529 layout_decl (tempvar, TYPE_ALIGN (result_type)); 3530 /* No way to handle variable-sized objects here. 3531 I fear that the entire handling of BLKmode conditional exprs 3532 needs to be redone. */ 3533 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST) 3534 abort (); 3535 DECL_RTL (tempvar) 3536 = assign_stack_local (DECL_MODE (tempvar), 3537 (TREE_INT_CST_LOW (DECL_SIZE (tempvar)) 3538 + BITS_PER_UNIT - 1) 3539 / BITS_PER_UNIT, 3540 0); 3541 3542 TREE_SIDE_EFFECTS (result) 3543 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1) 3544 | TREE_SIDE_EFFECTS (op2); 3545 return build (COMPOUND_EXPR, result_type, result, tempvar); 3546 } 3547 } 3548#endif /* 0 */ 3549 3550 if (TREE_CODE (ifexp) == INTEGER_CST) 3551 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); 3552 3553 return fold (build (COND_EXPR, result_type, ifexp, op1, op2)); 3554} 3555 3556/* Given a list of expressions, return a compound expression 3557 that performs them all and returns the value of the last of them. */ 3558 3559tree 3560build_compound_expr (list) 3561 tree list; 3562{ 3563 return internal_build_compound_expr (list, TRUE); 3564} 3565 3566static tree 3567internal_build_compound_expr (list, first_p) 3568 tree list; 3569 int first_p; 3570{ 3571 register tree rest; 3572 3573 if (TREE_CHAIN (list) == 0) 3574 { 3575#if 0 /* If something inside inhibited lvalueness, we should not override. */ 3576 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */ 3577 3578 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 3579 if (TREE_CODE (list) == NON_LVALUE_EXPR) 3580 list = TREE_OPERAND (list, 0); 3581#endif 3582 3583 /* Don't let (0, 0) be null pointer constant. */ 3584 if (!first_p && integer_zerop (TREE_VALUE (list))) 3585 return non_lvalue (TREE_VALUE (list)); 3586 return TREE_VALUE (list); 3587 } 3588 3589 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0) 3590 { 3591 /* Convert arrays to pointers when there really is a comma operator. */ 3592 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE) 3593 TREE_VALUE (TREE_CHAIN (list)) 3594 = default_conversion (TREE_VALUE (TREE_CHAIN (list))); 3595 } 3596 3597 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE); 3598 3599 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list))) 3600 { 3601 /* The left-hand operand of a comma expression is like an expression 3602 statement: with -W or -Wunused, we should warn if it doesn't have 3603 any side-effects, unless it was explicitly cast to (void). */ 3604 if ((extra_warnings || warn_unused) 3605 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR 3606 && TREE_TYPE (TREE_VALUE (list)) == void_type_node)) 3607 warning ("left-hand operand of comma expression has no effect"); 3608 3609 /* When pedantic, a compound expression can be neither an lvalue 3610 nor an integer constant expression. */ 3611 if (! pedantic) 3612 return rest; 3613 } 3614 3615 /* With -Wunused, we should also warn if the left-hand operand does have 3616 side-effects, but computes a value which is not used. For example, in 3617 `foo() + bar(), baz()' the result of the `+' operator is not used, 3618 so we should issue a warning. */ 3619 else if (warn_unused) 3620 warn_if_unused_value (TREE_VALUE (list)); 3621 3622 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest); 3623} 3624 3625/* Build an expression representing a cast to type TYPE of expression EXPR. */ 3626 3627tree 3628build_c_cast (type, expr) 3629 register tree type; 3630 tree expr; 3631{ 3632 register tree value = expr; 3633 3634 if (type == error_mark_node || expr == error_mark_node) 3635 return error_mark_node; 3636 type = TYPE_MAIN_VARIANT (type); 3637 3638#if 0 3639 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 3640 if (TREE_CODE (value) == NON_LVALUE_EXPR) 3641 value = TREE_OPERAND (value, 0); 3642#endif 3643 3644 if (TREE_CODE (type) == ARRAY_TYPE) 3645 { 3646 error ("cast specifies array type"); 3647 return error_mark_node; 3648 } 3649 3650 if (TREE_CODE (type) == FUNCTION_TYPE) 3651 { 3652 error ("cast specifies function type"); 3653 return error_mark_node; 3654 } 3655 3656 if (type == TREE_TYPE (value)) 3657 { 3658 if (pedantic) 3659 { 3660 if (TREE_CODE (type) == RECORD_TYPE 3661 || TREE_CODE (type) == UNION_TYPE) 3662 pedwarn ("ANSI C forbids casting nonscalar to the same type"); 3663 } 3664 } 3665 else if (TREE_CODE (type) == UNION_TYPE) 3666 { 3667 tree field; 3668 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE 3669 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE) 3670 value = default_conversion (value); 3671 3672 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 3673 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), 3674 TYPE_MAIN_VARIANT (TREE_TYPE (value)))) 3675 break; 3676 3677 if (field) 3678 { 3679 char *name; 3680 tree t; 3681 3682 if (pedantic) 3683 pedwarn ("ANSI C forbids casts to union type"); 3684 if (TYPE_NAME (type) != 0) 3685 { 3686 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) 3687 name = IDENTIFIER_POINTER (TYPE_NAME (type)); 3688 else 3689 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))); 3690 } 3691 else 3692 name = ""; 3693 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE, 3694 build_tree_list (field, value)), 3695 0, 0); 3696 TREE_CONSTANT (t) = TREE_CONSTANT (value); 3697 return t; 3698 } 3699 error ("cast to union type from type not present in union"); 3700 return error_mark_node; 3701 } 3702 else 3703 { 3704 tree otype, ovalue; 3705 3706 /* If casting to void, avoid the error that would come 3707 from default_conversion in the case of a non-lvalue array. */ 3708 if (type == void_type_node) 3709 return build1 (CONVERT_EXPR, type, value); 3710 3711 /* Convert functions and arrays to pointers, 3712 but don't convert any other types. */ 3713 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE 3714 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE) 3715 value = default_conversion (value); 3716 otype = TREE_TYPE (value); 3717 3718 /* Optionally warn about potentially worrisome casts. */ 3719 3720 if (warn_cast_qual 3721 && TREE_CODE (type) == POINTER_TYPE 3722 && TREE_CODE (otype) == POINTER_TYPE) 3723 { 3724 if (TYPE_VOLATILE (TREE_TYPE (otype)) 3725 && ! TYPE_VOLATILE (TREE_TYPE (type))) 3726 pedwarn ("cast discards `volatile' from pointer target type"); 3727 if (TYPE_READONLY (TREE_TYPE (otype)) 3728 && ! TYPE_READONLY (TREE_TYPE (type))) 3729 pedwarn ("cast discards `const' from pointer target type"); 3730 } 3731 3732 /* Warn about possible alignment problems. */ 3733 if (STRICT_ALIGNMENT && warn_cast_align 3734 && TREE_CODE (type) == POINTER_TYPE 3735 && TREE_CODE (otype) == POINTER_TYPE 3736 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE 3737 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE 3738 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) 3739 warning ("cast increases required alignment of target type"); 3740 3741 if (TREE_CODE (type) == INTEGER_TYPE 3742 && TREE_CODE (otype) == POINTER_TYPE 3743 && TYPE_PRECISION (type) != TYPE_PRECISION (otype) 3744 && !TREE_CONSTANT (value)) 3745 warning ("cast from pointer to integer of different size"); 3746 3747 if (warn_bad_function_cast 3748 && TREE_CODE (value) == CALL_EXPR 3749 && TREE_CODE (type) != TREE_CODE (otype)) 3750 warning ("cast does not match function type"); 3751 3752 if (TREE_CODE (type) == POINTER_TYPE 3753 && TREE_CODE (otype) == INTEGER_TYPE 3754 && TYPE_PRECISION (type) != TYPE_PRECISION (otype) 3755#if 0 3756 /* Don't warn about converting 0 to pointer, 3757 provided the 0 was explicit--not cast or made by folding. */ 3758 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)) 3759#endif 3760 /* Don't warn about converting any constant. */ 3761 && !TREE_CONSTANT (value)) 3762 warning ("cast to pointer from integer of different size"); 3763 3764 ovalue = value; 3765 value = convert (type, value); 3766 3767 /* Ignore any integer overflow caused by the cast. */ 3768 if (TREE_CODE (value) == INTEGER_CST) 3769 { 3770 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); 3771 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue); 3772 } 3773 } 3774 3775 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */ 3776 if (pedantic && TREE_CODE (value) == INTEGER_CST 3777 && TREE_CODE (expr) == INTEGER_CST 3778 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE) 3779 value = non_lvalue (value); 3780 3781 /* If pedantic, don't let a cast be an lvalue. */ 3782 if (value == expr && pedantic) 3783 value = non_lvalue (value); 3784 3785 return value; 3786} 3787 3788/* Build an assignment expression of lvalue LHS from value RHS. 3789 MODIFYCODE is the code for a binary operator that we use 3790 to combine the old value of LHS with RHS to get the new value. 3791 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */ 3792 3793tree 3794build_modify_expr (lhs, modifycode, rhs) 3795 tree lhs, rhs; 3796 enum tree_code modifycode; 3797{ 3798 register tree result; 3799 tree newrhs; 3800 tree lhstype = TREE_TYPE (lhs); 3801 tree olhstype = lhstype; 3802 3803 /* Types that aren't fully specified cannot be used in assignments. */ 3804 lhs = require_complete_type (lhs); 3805 3806 /* Avoid duplicate error messages from operands that had errors. */ 3807 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) 3808 return error_mark_node; 3809 3810 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 3811 /* Do not use STRIP_NOPS here. We do not want an enumerator 3812 whose value is 0 to count as a null pointer constant. */ 3813 if (TREE_CODE (rhs) == NON_LVALUE_EXPR) 3814 rhs = TREE_OPERAND (rhs, 0); 3815 3816 newrhs = rhs; 3817 3818 /* Handle control structure constructs used as "lvalues". */ 3819 3820 switch (TREE_CODE (lhs)) 3821 { 3822 /* Handle (a, b) used as an "lvalue". */ 3823 case COMPOUND_EXPR: 3824 pedantic_lvalue_warning (COMPOUND_EXPR); 3825 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), 3826 modifycode, rhs); 3827 if (TREE_CODE (newrhs) == ERROR_MARK) 3828 return error_mark_node; 3829 return build (COMPOUND_EXPR, lhstype, 3830 TREE_OPERAND (lhs, 0), newrhs); 3831 3832 /* Handle (a ? b : c) used as an "lvalue". */ 3833 case COND_EXPR: 3834 pedantic_lvalue_warning (COND_EXPR); 3835 rhs = save_expr (rhs); 3836 { 3837 /* Produce (a ? (b = rhs) : (c = rhs)) 3838 except that the RHS goes through a save-expr 3839 so the code to compute it is only emitted once. */ 3840 tree cond 3841 = build_conditional_expr (TREE_OPERAND (lhs, 0), 3842 build_modify_expr (TREE_OPERAND (lhs, 1), 3843 modifycode, rhs), 3844 build_modify_expr (TREE_OPERAND (lhs, 2), 3845 modifycode, rhs)); 3846 if (TREE_CODE (cond) == ERROR_MARK) 3847 return cond; 3848 /* Make sure the code to compute the rhs comes out 3849 before the split. */ 3850 return build (COMPOUND_EXPR, TREE_TYPE (lhs), 3851 /* But cast it to void to avoid an "unused" error. */ 3852 convert (void_type_node, rhs), cond); 3853 } 3854 } 3855 3856 /* If a binary op has been requested, combine the old LHS value with the RHS 3857 producing the value we should actually store into the LHS. */ 3858 3859 if (modifycode != NOP_EXPR) 3860 { 3861 lhs = stabilize_reference (lhs); 3862 newrhs = build_binary_op (modifycode, lhs, rhs, 1); 3863 } 3864 3865 /* Handle a cast used as an "lvalue". 3866 We have already performed any binary operator using the value as cast. 3867 Now convert the result to the cast type of the lhs, 3868 and then true type of the lhs and store it there; 3869 then convert result back to the cast type to be the value 3870 of the assignment. */ 3871 3872 switch (TREE_CODE (lhs)) 3873 { 3874 case NOP_EXPR: 3875 case CONVERT_EXPR: 3876 case FLOAT_EXPR: 3877 case FIX_TRUNC_EXPR: 3878 case FIX_FLOOR_EXPR: 3879 case FIX_ROUND_EXPR: 3880 case FIX_CEIL_EXPR: 3881 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE 3882 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE) 3883 newrhs = default_conversion (newrhs); 3884 { 3885 tree inner_lhs = TREE_OPERAND (lhs, 0); 3886 tree result; 3887 result = build_modify_expr (inner_lhs, NOP_EXPR, 3888 convert (TREE_TYPE (inner_lhs), 3889 convert (lhstype, newrhs))); 3890 if (TREE_CODE (result) == ERROR_MARK) 3891 return result; 3892 pedantic_lvalue_warning (CONVERT_EXPR); 3893 return convert (TREE_TYPE (lhs), result); 3894 } 3895 } 3896 3897 /* Now we have handled acceptable kinds of LHS that are not truly lvalues. 3898 Reject anything strange now. */ 3899 3900 if (!lvalue_or_else (lhs, "assignment")) 3901 return error_mark_node; 3902 3903 /* Warn about storing in something that is `const'. */ 3904 3905 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype) 3906 || ((TREE_CODE (lhstype) == RECORD_TYPE 3907 || TREE_CODE (lhstype) == UNION_TYPE) 3908 && C_TYPE_FIELDS_READONLY (lhstype))) 3909 readonly_warning (lhs, "assignment"); 3910 3911 /* If storing into a structure or union member, 3912 it has probably been given type `int'. 3913 Compute the type that would go with 3914 the actual amount of storage the member occupies. */ 3915 3916 if (TREE_CODE (lhs) == COMPONENT_REF 3917 && (TREE_CODE (lhstype) == INTEGER_TYPE 3918 || TREE_CODE (lhstype) == REAL_TYPE 3919 || TREE_CODE (lhstype) == ENUMERAL_TYPE)) 3920 lhstype = TREE_TYPE (get_unwidened (lhs, 0)); 3921 3922 /* If storing in a field that is in actuality a short or narrower than one, 3923 we must store in the field in its actual type. */ 3924 3925 if (lhstype != TREE_TYPE (lhs)) 3926 { 3927 lhs = copy_node (lhs); 3928 TREE_TYPE (lhs) = lhstype; 3929 } 3930 3931 /* Convert new value to destination type. */ 3932 3933 newrhs = convert_for_assignment (lhstype, newrhs, "assignment", 3934 NULL_TREE, NULL_TREE, 0); 3935 if (TREE_CODE (newrhs) == ERROR_MARK) 3936 return error_mark_node; 3937 3938 result = build (MODIFY_EXPR, lhstype, lhs, newrhs); 3939 TREE_SIDE_EFFECTS (result) = 1; 3940 3941 /* If we got the LHS in a different type for storing in, 3942 convert the result back to the nominal type of LHS 3943 so that the value we return always has the same type 3944 as the LHS argument. */ 3945 3946 if (olhstype == TREE_TYPE (result)) 3947 return result; 3948 return convert_for_assignment (olhstype, result, "assignment", 3949 NULL_TREE, NULL_TREE, 0); 3950} 3951 3952/* Convert value RHS to type TYPE as preparation for an assignment 3953 to an lvalue of type TYPE. 3954 The real work of conversion is done by `convert'. 3955 The purpose of this function is to generate error messages 3956 for assignments that are not allowed in C. 3957 ERRTYPE is a string to use in error messages: 3958 "assignment", "return", etc. If it is null, this is parameter passing 3959 for a function call (and different error messages are output). Otherwise, 3960 it may be a name stored in the spelling stack and interpreted by 3961 get_spelling. 3962 3963 FUNNAME is the name of the function being called, 3964 as an IDENTIFIER_NODE, or null. 3965 PARMNUM is the number of the argument, for printing in error messages. */ 3966 3967static tree 3968convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum) 3969 tree type, rhs; 3970 char *errtype; 3971 tree fundecl, funname; 3972 int parmnum; 3973{ 3974 register enum tree_code codel = TREE_CODE (type); 3975 register tree rhstype; 3976 register enum tree_code coder; 3977 3978 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 3979 /* Do not use STRIP_NOPS here. We do not want an enumerator 3980 whose value is 0 to count as a null pointer constant. */ 3981 if (TREE_CODE (rhs) == NON_LVALUE_EXPR) 3982 rhs = TREE_OPERAND (rhs, 0); 3983 3984 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE 3985 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE) 3986 rhs = default_conversion (rhs); 3987 else if (optimize && TREE_CODE (rhs) == VAR_DECL) 3988 rhs = decl_constant_value (rhs); 3989 3990 rhstype = TREE_TYPE (rhs); 3991 coder = TREE_CODE (rhstype); 3992 3993 if (coder == ERROR_MARK) 3994 return error_mark_node; 3995 3996 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) 3997 { 3998 overflow_warning (rhs); 3999 /* Check for Objective-C protocols. This will issue a warning if 4000 there are protocol violations. No need to use the return value. */ 4001 maybe_objc_comptypes (type, rhstype, 0); 4002 return rhs; 4003 } 4004 4005 if (coder == VOID_TYPE) 4006 { 4007 error ("void value not ignored as it ought to be"); 4008 return error_mark_node; 4009 } 4010 /* Arithmetic types all interconvert, and enum is treated like int. */ 4011 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE 4012 || codel == COMPLEX_TYPE) 4013 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE 4014 || coder == COMPLEX_TYPE)) 4015 return convert_and_check (type, rhs); 4016 4017 /* Conversion to a union from its member types. */ 4018 else if (codel == UNION_TYPE) 4019 { 4020 tree memb_types; 4021 4022 for (memb_types = TYPE_FIELDS (type); memb_types; 4023 memb_types = TREE_CHAIN (memb_types)) 4024 { 4025 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs))) 4026 { 4027 if (pedantic 4028 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl))) 4029 pedwarn ("ANSI C prohibits argument conversion to union type"); 4030 return build1 (NOP_EXPR, type, rhs); 4031 } 4032 4033 else if (coder == POINTER_TYPE 4034 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE) 4035 { 4036 tree memb_type = TREE_TYPE (memb_types); 4037 register tree ttl = TREE_TYPE (memb_type); 4038 register tree ttr = TREE_TYPE (rhstype); 4039 4040 /* Any non-function converts to a [const][volatile] void * 4041 and vice versa; otherwise, targets must be the same. 4042 Meanwhile, the lhs target must have all the qualifiers of 4043 the rhs. */ 4044 if (TYPE_MAIN_VARIANT (ttl) == void_type_node 4045 || TYPE_MAIN_VARIANT (ttr) == void_type_node 4046 || comp_target_types (memb_type, rhstype)) 4047 { 4048 /* Const and volatile mean something different for function 4049 types, so the usual warnings are not appropriate. */ 4050 if (TREE_CODE (ttr) != FUNCTION_TYPE 4051 || TREE_CODE (ttl) != FUNCTION_TYPE) 4052 { 4053 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) 4054 warn_for_assignment ("%s discards `const' from pointer target type", 4055 get_spelling (errtype), funname, 4056 parmnum); 4057 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) 4058 warn_for_assignment ("%s discards `volatile' from pointer target type", 4059 get_spelling (errtype), funname, 4060 parmnum); 4061 } 4062 else 4063 { 4064 /* Because const and volatile on functions are 4065 restrictions that say the function will not do 4066 certain things, it is okay to use a const or volatile 4067 function where an ordinary one is wanted, but not 4068 vice-versa. */ 4069 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr)) 4070 warn_for_assignment ("%s makes `const *' function pointer from non-const", 4071 get_spelling (errtype), funname, 4072 parmnum); 4073 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr)) 4074 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile", 4075 get_spelling (errtype), funname, 4076 parmnum); 4077 } 4078 4079 if (pedantic 4080 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl))) 4081 pedwarn ("ANSI C prohibits argument conversion to union type"); 4082 return build1 (NOP_EXPR, type, rhs); 4083 } 4084 } 4085 4086 /* Can convert integer zero to any pointer type. */ 4087 else if (TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE 4088 && (integer_zerop (rhs) 4089 || (TREE_CODE (rhs) == NOP_EXPR 4090 && integer_zerop (TREE_OPERAND (rhs, 0))))) 4091 return build1 (NOP_EXPR, type, null_pointer_node); 4092 } 4093 } 4094 4095 /* Conversions among pointers */ 4096 else if (codel == POINTER_TYPE && coder == POINTER_TYPE) 4097 { 4098 register tree ttl = TREE_TYPE (type); 4099 register tree ttr = TREE_TYPE (rhstype); 4100 4101 /* Any non-function converts to a [const][volatile] void * 4102 and vice versa; otherwise, targets must be the same. 4103 Meanwhile, the lhs target must have all the qualifiers of the rhs. */ 4104 if (TYPE_MAIN_VARIANT (ttl) == void_type_node 4105 || TYPE_MAIN_VARIANT (ttr) == void_type_node 4106 || comp_target_types (type, rhstype) 4107 || (unsigned_type (TYPE_MAIN_VARIANT (ttl)) 4108 == unsigned_type (TYPE_MAIN_VARIANT (ttr)))) 4109 { 4110 if (pedantic 4111 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node 4112 && TREE_CODE (ttr) == FUNCTION_TYPE) 4113 || 4114 (TYPE_MAIN_VARIANT (ttr) == void_type_node 4115 /* Check TREE_CODE to catch cases like (void *) (char *) 0 4116 which are not ANSI null ptr constants. */ 4117 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR) 4118 && TREE_CODE (ttl) == FUNCTION_TYPE))) 4119 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'", 4120 get_spelling (errtype), funname, parmnum); 4121 /* Const and volatile mean something different for function types, 4122 so the usual warnings are not appropriate. */ 4123 else if (TREE_CODE (ttr) != FUNCTION_TYPE 4124 && TREE_CODE (ttl) != FUNCTION_TYPE) 4125 { 4126 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) 4127 warn_for_assignment ("%s discards `const' from pointer target type", 4128 get_spelling (errtype), funname, parmnum); 4129 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) 4130 warn_for_assignment ("%s discards `volatile' from pointer target type", 4131 get_spelling (errtype), funname, parmnum); 4132 /* If this is not a case of ignoring a mismatch in signedness, 4133 no warning. */ 4134 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node 4135 || TYPE_MAIN_VARIANT (ttr) == void_type_node 4136 || comp_target_types (type, rhstype)) 4137 ; 4138 /* If there is a mismatch, do warn. */ 4139 else if (pedantic) 4140 warn_for_assignment ("pointer targets in %s differ in signedness", 4141 get_spelling (errtype), funname, parmnum); 4142 } 4143 else if (TREE_CODE (ttl) == FUNCTION_TYPE 4144 && TREE_CODE (ttr) == FUNCTION_TYPE) 4145 { 4146 /* Because const and volatile on functions are restrictions 4147 that say the function will not do certain things, 4148 it is okay to use a const or volatile function 4149 where an ordinary one is wanted, but not vice-versa. */ 4150 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr)) 4151 warn_for_assignment ("%s makes `const *' function pointer from non-const", 4152 get_spelling (errtype), funname, parmnum); 4153 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr)) 4154 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile", 4155 get_spelling (errtype), funname, parmnum); 4156 } 4157 } 4158 else 4159 warn_for_assignment ("%s from incompatible pointer type", 4160 get_spelling (errtype), funname, parmnum); 4161 return convert (type, rhs); 4162 } 4163 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) 4164 { 4165 /* An explicit constant 0 can convert to a pointer, 4166 or one that results from arithmetic, even including 4167 a cast to integer type. */ 4168 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)) 4169 && 4170 ! (TREE_CODE (rhs) == NOP_EXPR 4171 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE 4172 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST 4173 && integer_zerop (TREE_OPERAND (rhs, 0)))) 4174 { 4175 warn_for_assignment ("%s makes pointer from integer without a cast", 4176 get_spelling (errtype), funname, parmnum); 4177 return convert (type, rhs); 4178 } 4179 return null_pointer_node; 4180 } 4181 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) 4182 { 4183 warn_for_assignment ("%s makes integer from pointer without a cast", 4184 get_spelling (errtype), funname, parmnum); 4185 return convert (type, rhs); 4186 } 4187 4188 if (!errtype) 4189 { 4190 if (funname) 4191 { 4192 tree selector = maybe_building_objc_message_expr (); 4193 4194 if (selector && parmnum > 2) 4195 error ("incompatible type for argument %d of `%s'", 4196 parmnum - 2, IDENTIFIER_POINTER (selector)); 4197 else 4198 error ("incompatible type for argument %d of `%s'", 4199 parmnum, IDENTIFIER_POINTER (funname)); 4200 } 4201 else 4202 error ("incompatible type for argument %d of indirect function call", 4203 parmnum); 4204 } 4205 else 4206 error ("incompatible types in %s", get_spelling (errtype)); 4207 4208 return error_mark_node; 4209} 4210 4211/* Print a warning using MSG. 4212 It gets OPNAME as its one parameter. 4213 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'". 4214 FUNCTION and ARGNUM are handled specially if we are building an 4215 Objective-C selector. */ 4216 4217static void 4218warn_for_assignment (msg, opname, function, argnum) 4219 char *msg; 4220 char *opname; 4221 tree function; 4222 int argnum; 4223{ 4224 static char argstring[] = "passing arg %d of `%s'"; 4225 static char argnofun[] = "passing arg %d"; 4226 4227 if (opname == 0) 4228 { 4229 tree selector = maybe_building_objc_message_expr (); 4230 4231 if (selector && argnum > 2) 4232 { 4233 function = selector; 4234 argnum -= 2; 4235 } 4236 if (function) 4237 { 4238 /* Function name is known; supply it. */ 4239 opname = (char *) alloca (IDENTIFIER_LENGTH (function) 4240 + sizeof (argstring) + 25 /*%d*/ + 1); 4241 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function)); 4242 } 4243 else 4244 { 4245 /* Function name unknown (call through ptr); just give arg number. */ 4246 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1); 4247 sprintf (opname, argnofun, argnum); 4248 } 4249 } 4250 pedwarn (msg, opname); 4251} 4252 4253/* Return nonzero if VALUE is a valid constant-valued expression 4254 for use in initializing a static variable; one that can be an 4255 element of a "constant" initializer. 4256 4257 Return null_pointer_node if the value is absolute; 4258 if it is relocatable, return the variable that determines the relocation. 4259 We assume that VALUE has been folded as much as possible; 4260 therefore, we do not need to check for such things as 4261 arithmetic-combinations of integers. */ 4262 4263tree 4264initializer_constant_valid_p (value, endtype) 4265 tree value; 4266 tree endtype; 4267{ 4268 switch (TREE_CODE (value)) 4269 { 4270 case CONSTRUCTOR: 4271 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE 4272 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE) 4273 && TREE_CONSTANT (value)) 4274 return 4275 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)), 4276 endtype); 4277 4278 return TREE_STATIC (value) ? null_pointer_node : 0; 4279 4280 case INTEGER_CST: 4281 case REAL_CST: 4282 case STRING_CST: 4283 case COMPLEX_CST: 4284 return null_pointer_node; 4285 4286 case ADDR_EXPR: 4287 return TREE_OPERAND (value, 0); 4288 4289 case NON_LVALUE_EXPR: 4290 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 4291 4292 case CONVERT_EXPR: 4293 case NOP_EXPR: 4294 /* Allow conversions between pointer types. */ 4295 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE 4296 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE) 4297 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 4298 4299 /* Allow conversions between real types. */ 4300 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE 4301 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE) 4302 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 4303 4304 /* Allow length-preserving conversions between integer types. */ 4305 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 4306 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE 4307 && (TYPE_PRECISION (TREE_TYPE (value)) 4308 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))) 4309 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 4310 4311 /* Allow conversions between other integer types only if 4312 explicit value. */ 4313 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 4314 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE) 4315 { 4316 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0), 4317 endtype); 4318 if (inner == null_pointer_node) 4319 return null_pointer_node; 4320 return 0; 4321 } 4322 4323 /* Allow (int) &foo provided int is as wide as a pointer. */ 4324 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 4325 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE 4326 && (TYPE_PRECISION (TREE_TYPE (value)) 4327 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))) 4328 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 4329 endtype); 4330 4331 /* Likewise conversions from int to pointers. */ 4332 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE 4333 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE 4334 && (TYPE_PRECISION (TREE_TYPE (value)) 4335 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))) 4336 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 4337 endtype); 4338 4339 /* Allow conversions to union types if the value inside is okay. */ 4340 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE) 4341 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 4342 endtype); 4343 return 0; 4344 4345 case PLUS_EXPR: 4346 if (TREE_CODE (endtype) == INTEGER_TYPE 4347 && TYPE_PRECISION (endtype) < POINTER_SIZE) 4348 return 0; 4349 { 4350 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0), 4351 endtype); 4352 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1), 4353 endtype); 4354 /* If either term is absolute, use the other terms relocation. */ 4355 if (valid0 == null_pointer_node) 4356 return valid1; 4357 if (valid1 == null_pointer_node) 4358 return valid0; 4359 return 0; 4360 } 4361 4362 case MINUS_EXPR: 4363 if (TREE_CODE (endtype) == INTEGER_TYPE 4364 && TYPE_PRECISION (endtype) < POINTER_SIZE) 4365 return 0; 4366 { 4367 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0), 4368 endtype); 4369 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1), 4370 endtype); 4371 /* Win if second argument is absolute. */ 4372 if (valid1 == null_pointer_node) 4373 return valid0; 4374 /* Win if both arguments have the same relocation. 4375 Then the value is absolute. */ 4376 if (valid0 == valid1) 4377 return null_pointer_node; 4378 return 0; 4379 } 4380 } 4381 4382 return 0; 4383} 4384 4385/* If VALUE is a compound expr all of whose expressions are constant, then 4386 return its value. Otherwise, return error_mark_node. 4387 4388 This is for handling COMPOUND_EXPRs as initializer elements 4389 which is allowed with a warning when -pedantic is specified. */ 4390 4391static tree 4392valid_compound_expr_initializer (value, endtype) 4393 tree value; 4394 tree endtype; 4395{ 4396 if (TREE_CODE (value) == COMPOUND_EXPR) 4397 { 4398 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) 4399 == error_mark_node) 4400 return error_mark_node; 4401 return valid_compound_expr_initializer (TREE_OPERAND (value, 1), 4402 endtype); 4403 } 4404 else if (! TREE_CONSTANT (value) 4405 && ! initializer_constant_valid_p (value, endtype)) 4406 return error_mark_node; 4407 else 4408 return value; 4409} 4410 4411/* Perform appropriate conversions on the initial value of a variable, 4412 store it in the declaration DECL, 4413 and print any error messages that are appropriate. 4414 If the init is invalid, store an ERROR_MARK. */ 4415 4416void 4417store_init_value (decl, init) 4418 tree decl, init; 4419{ 4420 register tree value, type; 4421 4422 /* If variable's type was invalidly declared, just ignore it. */ 4423 4424 type = TREE_TYPE (decl); 4425 if (TREE_CODE (type) == ERROR_MARK) 4426 return; 4427 4428 /* Digest the specified initializer into an expression. */ 4429 4430 value = digest_init (type, init, TREE_STATIC (decl), 4431 TREE_STATIC (decl) || pedantic); 4432 4433 /* Store the expression if valid; else report error. */ 4434 4435#if 0 4436 /* Note that this is the only place we can detect the error 4437 in a case such as struct foo bar = (struct foo) { x, y }; 4438 where there is one initial value which is a constructor expression. */ 4439 if (value == error_mark_node) 4440 ; 4441 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value)) 4442 { 4443 error ("initializer for static variable is not constant"); 4444 value = error_mark_node; 4445 } 4446 else if (TREE_STATIC (decl) 4447 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) 4448 { 4449 error ("initializer for static variable uses complicated arithmetic"); 4450 value = error_mark_node; 4451 } 4452 else 4453 { 4454 if (pedantic && TREE_CODE (value) == CONSTRUCTOR) 4455 { 4456 if (! TREE_CONSTANT (value)) 4457 pedwarn ("aggregate initializer is not constant"); 4458 else if (! TREE_STATIC (value)) 4459 pedwarn ("aggregate initializer uses complicated arithmetic"); 4460 } 4461 } 4462#endif 4463 4464 DECL_INITIAL (decl) = value; 4465 4466 /* ANSI wants warnings about out-of-range constant initializers. */ 4467 STRIP_TYPE_NOPS (value); 4468 constant_expression_warning (value); 4469} 4470 4471/* Methods for storing and printing names for error messages. */ 4472 4473/* Implement a spelling stack that allows components of a name to be pushed 4474 and popped. Each element on the stack is this structure. */ 4475 4476struct spelling 4477{ 4478 int kind; 4479 union 4480 { 4481 int i; 4482 char *s; 4483 } u; 4484}; 4485 4486#define SPELLING_STRING 1 4487#define SPELLING_MEMBER 2 4488#define SPELLING_BOUNDS 3 4489 4490static struct spelling *spelling; /* Next stack element (unused). */ 4491static struct spelling *spelling_base; /* Spelling stack base. */ 4492static int spelling_size; /* Size of the spelling stack. */ 4493 4494/* Macros to save and restore the spelling stack around push_... functions. 4495 Alternative to SAVE_SPELLING_STACK. */ 4496 4497#define SPELLING_DEPTH() (spelling - spelling_base) 4498#define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth) 4499 4500/* Save and restore the spelling stack around arbitrary C code. */ 4501 4502#define SAVE_SPELLING_DEPTH(code) \ 4503{ \ 4504 int __depth = SPELLING_DEPTH (); \ 4505 code; \ 4506 RESTORE_SPELLING_DEPTH (__depth); \ 4507} 4508 4509/* Push an element on the spelling stack with type KIND and assign VALUE 4510 to MEMBER. */ 4511 4512#define PUSH_SPELLING(KIND, VALUE, MEMBER) \ 4513{ \ 4514 int depth = SPELLING_DEPTH (); \ 4515 \ 4516 if (depth >= spelling_size) \ 4517 { \ 4518 spelling_size += 10; \ 4519 if (spelling_base == 0) \ 4520 spelling_base \ 4521 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \ 4522 else \ 4523 spelling_base \ 4524 = (struct spelling *) xrealloc (spelling_base, \ 4525 spelling_size * sizeof (struct spelling)); \ 4526 RESTORE_SPELLING_DEPTH (depth); \ 4527 } \ 4528 \ 4529 spelling->kind = (KIND); \ 4530 spelling->MEMBER = (VALUE); \ 4531 spelling++; \ 4532} 4533 4534/* Push STRING on the stack. Printed literally. */ 4535 4536static void 4537push_string (string) 4538 char *string; 4539{ 4540 PUSH_SPELLING (SPELLING_STRING, string, u.s); 4541} 4542 4543/* Push a member name on the stack. Printed as '.' STRING. */ 4544 4545static void 4546push_member_name (decl) 4547 tree decl; 4548 4549{ 4550 char *string 4551 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>"; 4552 PUSH_SPELLING (SPELLING_MEMBER, string, u.s); 4553} 4554 4555/* Push an array bounds on the stack. Printed as [BOUNDS]. */ 4556 4557static void 4558push_array_bounds (bounds) 4559 int bounds; 4560{ 4561 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); 4562} 4563 4564/* Compute the maximum size in bytes of the printed spelling. */ 4565 4566static int 4567spelling_length () 4568{ 4569 register int size = 0; 4570 register struct spelling *p; 4571 4572 for (p = spelling_base; p < spelling; p++) 4573 { 4574 if (p->kind == SPELLING_BOUNDS) 4575 size += 25; 4576 else 4577 size += strlen (p->u.s) + 1; 4578 } 4579 4580 return size; 4581} 4582 4583/* Print the spelling to BUFFER and return it. */ 4584 4585static char * 4586print_spelling (buffer) 4587 register char *buffer; 4588{ 4589 register char *d = buffer; 4590 register char *s; 4591 register struct spelling *p; 4592 4593 for (p = spelling_base; p < spelling; p++) 4594 if (p->kind == SPELLING_BOUNDS) 4595 { 4596 sprintf (d, "[%d]", p->u.i); 4597 d += strlen (d); 4598 } 4599 else 4600 { 4601 if (p->kind == SPELLING_MEMBER) 4602 *d++ = '.'; 4603 for (s = p->u.s; *d = *s++; d++) 4604 ; 4605 } 4606 *d++ = '\0'; 4607 return buffer; 4608} 4609 4610/* Provide a means to pass component names derived from the spelling stack. */ 4611 4612char initialization_message; 4613 4614/* Interpret the spelling of the given ERRTYPE message. */ 4615 4616static char * 4617get_spelling (errtype) 4618 char *errtype; 4619{ 4620 static char *buffer; 4621 static int size = -1; 4622 4623 if (errtype == &initialization_message) 4624 { 4625 /* Avoid counting chars */ 4626 static char message[] = "initialization of `%s'"; 4627 register int needed = sizeof (message) + spelling_length () + 1; 4628 char *temp; 4629 4630 if (size < 0) 4631 buffer = (char *) xmalloc (size = needed); 4632 if (needed > size) 4633 buffer = (char *) xrealloc (buffer, size = needed); 4634 4635 temp = (char *) alloca (needed); 4636 sprintf (buffer, message, print_spelling (temp)); 4637 return buffer; 4638 } 4639 4640 return errtype; 4641} 4642 4643/* Issue an error message for a bad initializer component. 4644 FORMAT describes the message. OFWHAT is the name for the component. 4645 LOCAL is a format string for formatting the insertion of the name 4646 into the message. 4647 4648 If OFWHAT is null, the component name is stored on the spelling stack. 4649 If the component name is a null string, then LOCAL is omitted entirely. */ 4650 4651void 4652error_init (format, local, ofwhat) 4653 char *format, *local, *ofwhat; 4654{ 4655 char *buffer; 4656 4657 if (ofwhat == 0) 4658 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 4659 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); 4660 4661 if (*ofwhat) 4662 sprintf (buffer, local, ofwhat); 4663 else 4664 buffer[0] = 0; 4665 4666 error (format, buffer); 4667} 4668 4669/* Issue a pedantic warning for a bad initializer component. 4670 FORMAT describes the message. OFWHAT is the name for the component. 4671 LOCAL is a format string for formatting the insertion of the name 4672 into the message. 4673 4674 If OFWHAT is null, the component name is stored on the spelling stack. 4675 If the component name is a null string, then LOCAL is omitted entirely. */ 4676 4677void 4678pedwarn_init (format, local, ofwhat) 4679 char *format, *local, *ofwhat; 4680{ 4681 char *buffer; 4682 4683 if (ofwhat == 0) 4684 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 4685 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); 4686 4687 if (*ofwhat) 4688 sprintf (buffer, local, ofwhat); 4689 else 4690 buffer[0] = 0; 4691 4692 pedwarn (format, buffer); 4693} 4694 4695/* Issue a warning for a bad initializer component. 4696 FORMAT describes the message. OFWHAT is the name for the component. 4697 LOCAL is a format string for formatting the insertion of the name 4698 into the message. 4699 4700 If OFWHAT is null, the component name is stored on the spelling stack. 4701 If the component name is a null string, then LOCAL is omitted entirely. */ 4702 4703static void 4704warning_init (format, local, ofwhat) 4705 char *format, *local, *ofwhat; 4706{ 4707 char *buffer; 4708 4709 if (ofwhat == 0) 4710 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 4711 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); 4712 4713 if (*ofwhat) 4714 sprintf (buffer, local, ofwhat); 4715 else 4716 buffer[0] = 0; 4717 4718 warning (format, buffer); 4719} 4720 4721/* Digest the parser output INIT as an initializer for type TYPE. 4722 Return a C expression of type TYPE to represent the initial value. 4723 4724 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors 4725 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT 4726 applies only to elements of constructors. */ 4727 4728static tree 4729digest_init (type, init, require_constant, constructor_constant) 4730 tree type, init; 4731 int require_constant, constructor_constant; 4732{ 4733 enum tree_code code = TREE_CODE (type); 4734 tree inside_init = init; 4735 4736 if (init == error_mark_node) 4737 return init; 4738 4739 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 4740 /* Do not use STRIP_NOPS here. We do not want an enumerator 4741 whose value is 0 to count as a null pointer constant. */ 4742 if (TREE_CODE (init) == NON_LVALUE_EXPR) 4743 inside_init = TREE_OPERAND (init, 0); 4744 4745 /* Initialization of an array of chars from a string constant 4746 optionally enclosed in braces. */ 4747 4748 if (code == ARRAY_TYPE) 4749 { 4750 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 4751 if ((typ1 == char_type_node 4752 || typ1 == signed_char_type_node 4753 || typ1 == unsigned_char_type_node 4754 || typ1 == unsigned_wchar_type_node 4755 || typ1 == signed_wchar_type_node) 4756 && ((inside_init && TREE_CODE (inside_init) == STRING_CST))) 4757 { 4758 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 4759 TYPE_MAIN_VARIANT (type))) 4760 return inside_init; 4761 4762 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) 4763 != char_type_node) 4764 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node)) 4765 { 4766 error_init ("char-array%s initialized from wide string", 4767 " `%s'", NULL); 4768 return error_mark_node; 4769 } 4770 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) 4771 == char_type_node) 4772 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)) 4773 { 4774 error_init ("int-array%s initialized from non-wide string", 4775 " `%s'", NULL); 4776 return error_mark_node; 4777 } 4778 4779 TREE_TYPE (inside_init) = type; 4780 if (TYPE_DOMAIN (type) != 0 4781 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) 4782 { 4783 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); 4784 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; 4785 /* Subtract 1 (or sizeof (wchar_t)) 4786 because it's ok to ignore the terminating null char 4787 that is counted in the length of the constant. */ 4788 if (size < TREE_STRING_LENGTH (inside_init) 4789 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node) 4790 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT 4791 : 1)) 4792 pedwarn_init ( 4793 "initializer-string for array of chars%s is too long", 4794 " `%s'", NULL); 4795 } 4796 return inside_init; 4797 } 4798 } 4799 4800 /* Any type can be initialized 4801 from an expression of the same type, optionally with braces. */ 4802 4803 if (inside_init && TREE_TYPE (inside_init) != 0 4804 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 4805 TYPE_MAIN_VARIANT (type)) 4806 || (code == ARRAY_TYPE 4807 && comptypes (TREE_TYPE (inside_init), type)) 4808 || (code == POINTER_TYPE 4809 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE 4810 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE) 4811 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), 4812 TREE_TYPE (type))))) 4813 { 4814 if (code == POINTER_TYPE 4815 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE 4816 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)) 4817 inside_init = default_conversion (inside_init); 4818 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST 4819 && TREE_CODE (inside_init) != CONSTRUCTOR) 4820 { 4821 error_init ("array%s initialized from non-constant array expression", 4822 " `%s'", NULL); 4823 return error_mark_node; 4824 } 4825 4826 if (optimize && TREE_CODE (inside_init) == VAR_DECL) 4827 inside_init = decl_constant_value (inside_init); 4828 4829 /* Compound expressions can only occur here if -pedantic or 4830 -pedantic-errors is specified. In the later case, we always want 4831 an error. In the former case, we simply want a warning. */ 4832 if (require_constant && pedantic 4833 && TREE_CODE (inside_init) == COMPOUND_EXPR) 4834 { 4835 inside_init 4836 = valid_compound_expr_initializer (inside_init, 4837 TREE_TYPE (inside_init)); 4838 if (inside_init == error_mark_node) 4839 error_init ("initializer element%s is not constant", 4840 " for `%s'", NULL); 4841 else 4842 pedwarn_init ("initializer element%s is not constant", 4843 " for `%s'", NULL); 4844 if (flag_pedantic_errors) 4845 inside_init = error_mark_node; 4846 } 4847 else if (require_constant && ! TREE_CONSTANT (inside_init)) 4848 { 4849 error_init ("initializer element%s is not constant", 4850 " for `%s'", NULL); 4851 inside_init = error_mark_node; 4852 } 4853 else if (require_constant 4854 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) 4855 { 4856 error_init ("initializer element%s is not computable at load time", 4857 " for `%s'", NULL); 4858 inside_init = error_mark_node; 4859 } 4860 4861 return inside_init; 4862 } 4863 4864 /* Handle scalar types, including conversions. */ 4865 4866 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE 4867 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE) 4868 { 4869 /* Note that convert_for_assignment calls default_conversion 4870 for arrays and functions. We must not call it in the 4871 case where inside_init is a null pointer constant. */ 4872 inside_init 4873 = convert_for_assignment (type, init, "initialization", 4874 NULL_TREE, NULL_TREE, 0); 4875 4876 if (require_constant && ! TREE_CONSTANT (inside_init)) 4877 { 4878 error_init ("initializer element%s is not constant", 4879 " for `%s'", NULL); 4880 inside_init = error_mark_node; 4881 } 4882 else if (require_constant 4883 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) 4884 { 4885 error_init ("initializer element%s is not computable at load time", 4886 " for `%s'", NULL); 4887 inside_init = error_mark_node; 4888 } 4889 4890 return inside_init; 4891 } 4892 4893 /* Come here only for records and arrays. */ 4894 4895 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) 4896 { 4897 error_init ("variable-sized object%s may not be initialized", 4898 " `%s'", NULL); 4899 return error_mark_node; 4900 } 4901 4902 /* Traditionally, you can write struct foo x = 0; 4903 and it initializes the first element of x to 0. */ 4904 if (flag_traditional) 4905 { 4906 tree top = 0, prev = 0, otype = type; 4907 while (TREE_CODE (type) == RECORD_TYPE 4908 || TREE_CODE (type) == ARRAY_TYPE 4909 || TREE_CODE (type) == QUAL_UNION_TYPE 4910 || TREE_CODE (type) == UNION_TYPE) 4911 { 4912 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); 4913 if (prev == 0) 4914 top = temp; 4915 else 4916 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp); 4917 prev = temp; 4918 if (TREE_CODE (type) == ARRAY_TYPE) 4919 type = TREE_TYPE (type); 4920 else if (TYPE_FIELDS (type)) 4921 type = TREE_TYPE (TYPE_FIELDS (type)); 4922 else 4923 { 4924 error_init ("invalid initializer%s", " for `%s'", NULL); 4925 return error_mark_node; 4926 } 4927 } 4928 4929 if (otype != type) 4930 { 4931 TREE_OPERAND (prev, 1) 4932 = build_tree_list (NULL_TREE, 4933 digest_init (type, init, require_constant, 4934 constructor_constant)); 4935 return top; 4936 } 4937 else 4938 return error_mark_node; 4939 } 4940 error_init ("invalid initializer%s", " for `%s'", NULL); 4941 return error_mark_node; 4942} 4943 4944/* Handle initializers that use braces. */ 4945 4946/* Type of object we are accumulating a constructor for. 4947 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ 4948static tree constructor_type; 4949 4950/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields 4951 left to fill. */ 4952static tree constructor_fields; 4953 4954/* For an ARRAY_TYPE, this is the specified index 4955 at which to store the next element we get. 4956 This is a special INTEGER_CST node that we modify in place. */ 4957static tree constructor_index; 4958 4959/* For an ARRAY_TYPE, this is the end index of the range 4960 to initialize with the next element, or NULL in the ordinary case 4961 where the element is used just once. */ 4962static tree constructor_range_end; 4963 4964/* For an ARRAY_TYPE, this is the maximum index. */ 4965static tree constructor_max_index; 4966 4967/* For a RECORD_TYPE, this is the first field not yet written out. */ 4968static tree constructor_unfilled_fields; 4969 4970/* For an ARRAY_TYPE, this is the index of the first element 4971 not yet written out. 4972 This is a special INTEGER_CST node that we modify in place. */ 4973static tree constructor_unfilled_index; 4974 4975/* In a RECORD_TYPE, the byte index of the next consecutive field. 4976 This is so we can generate gaps between fields, when appropriate. 4977 This is a special INTEGER_CST node that we modify in place. */ 4978static tree constructor_bit_index; 4979 4980/* If we are saving up the elements rather than allocating them, 4981 this is the list of elements so far (in reverse order, 4982 most recent first). */ 4983static tree constructor_elements; 4984 4985/* 1 if so far this constructor's elements are all compile-time constants. */ 4986static int constructor_constant; 4987 4988/* 1 if so far this constructor's elements are all valid address constants. */ 4989static int constructor_simple; 4990 4991/* 1 if this constructor is erroneous so far. */ 4992static int constructor_erroneous; 4993 4994/* 1 if have called defer_addressed_constants. */ 4995static int constructor_subconstants_deferred; 4996 4997/* List of pending elements at this constructor level. 4998 These are elements encountered out of order 4999 which belong at places we haven't reached yet in actually 5000 writing the output. */ 5001static tree constructor_pending_elts; 5002 5003/* The SPELLING_DEPTH of this constructor. */ 5004static int constructor_depth; 5005 5006/* 0 if implicitly pushing constructor levels is allowed. */ 5007int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */ 5008 5009/* 1 if this constructor level was entered implicitly. */ 5010static int constructor_implicit; 5011 5012static int require_constant_value; 5013static int require_constant_elements; 5014 5015/* 1 if it is ok to output this constructor as we read it. 5016 0 means must accumulate a CONSTRUCTOR expression. */ 5017static int constructor_incremental; 5018 5019/* DECL node for which an initializer is being read. 5020 0 means we are reading a constructor expression 5021 such as (struct foo) {...}. */ 5022static tree constructor_decl; 5023 5024/* start_init saves the ASMSPEC arg here for really_start_incremental_init. */ 5025static char *constructor_asmspec; 5026 5027/* Nonzero if this is an initializer for a top-level decl. */ 5028static int constructor_top_level; 5029 5030/* When we finish reading a constructor expression 5031 (constructor_decl is 0), the CONSTRUCTOR goes here. */ 5032static tree constructor_result; 5033 5034/* This stack has a level for each implicit or explicit level of 5035 structuring in the initializer, including the outermost one. It 5036 saves the values of most of the variables above. */ 5037 5038struct constructor_stack 5039{ 5040 struct constructor_stack *next; 5041 tree type; 5042 tree fields; 5043 tree index; 5044 tree range_end; 5045 tree max_index; 5046 tree unfilled_index; 5047 tree unfilled_fields; 5048 tree bit_index; 5049 tree elements; 5050 int offset; 5051 tree pending_elts; 5052 int depth; 5053 /* If nonzero, this value should replace the entire 5054 constructor at this level. */ 5055 tree replacement_value; 5056 char constant; 5057 char simple; 5058 char implicit; 5059 char incremental; 5060 char erroneous; 5061 char outer; 5062}; 5063 5064struct constructor_stack *constructor_stack; 5065 5066/* This stack records separate initializers that are nested. 5067 Nested initializers can't happen in ANSI C, but GNU C allows them 5068 in cases like { ... (struct foo) { ... } ... }. */ 5069 5070struct initializer_stack 5071{ 5072 struct initializer_stack *next; 5073 tree decl; 5074 char *asmspec; 5075 struct constructor_stack *constructor_stack; 5076 tree elements; 5077 struct spelling *spelling; 5078 struct spelling *spelling_base; 5079 int spelling_size; 5080 char top_level; 5081 char incremental; 5082 char require_constant_value; 5083 char require_constant_elements; 5084 char deferred; 5085}; 5086 5087struct initializer_stack *initializer_stack; 5088 5089/* Prepare to parse and output the initializer for variable DECL. */ 5090 5091void 5092start_init (decl, asmspec_tree, top_level) 5093 tree decl; 5094 tree asmspec_tree; 5095 int top_level; 5096{ 5097 char *locus; 5098 struct initializer_stack *p 5099 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack)); 5100 char *asmspec = 0; 5101 5102 if (asmspec_tree) 5103 asmspec = TREE_STRING_POINTER (asmspec_tree); 5104 5105 p->decl = constructor_decl; 5106 p->asmspec = constructor_asmspec; 5107 p->incremental = constructor_incremental; 5108 p->require_constant_value = require_constant_value; 5109 p->require_constant_elements = require_constant_elements; 5110 p->constructor_stack = constructor_stack; 5111 p->elements = constructor_elements; 5112 p->spelling = spelling; 5113 p->spelling_base = spelling_base; 5114 p->spelling_size = spelling_size; 5115 p->deferred = constructor_subconstants_deferred; 5116 p->top_level = constructor_top_level; 5117 p->next = initializer_stack; 5118 initializer_stack = p; 5119 5120 constructor_decl = decl; 5121 constructor_incremental = top_level; 5122 constructor_asmspec = asmspec; 5123 constructor_subconstants_deferred = 0; 5124 constructor_top_level = top_level; 5125 5126 if (decl != 0) 5127 { 5128 require_constant_value = TREE_STATIC (decl); 5129 require_constant_elements 5130 = ((TREE_STATIC (decl) || pedantic) 5131 /* For a scalar, you can always use any value to initialize, 5132 even within braces. */ 5133 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE 5134 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE 5135 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE 5136 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); 5137 locus = IDENTIFIER_POINTER (DECL_NAME (decl)); 5138 constructor_incremental |= TREE_STATIC (decl); 5139 } 5140 else 5141 { 5142 require_constant_value = 0; 5143 require_constant_elements = 0; 5144 locus = "(anonymous)"; 5145 } 5146 5147 constructor_stack = 0; 5148 5149 missing_braces_mentioned = 0; 5150 5151 spelling_base = 0; 5152 spelling_size = 0; 5153 RESTORE_SPELLING_DEPTH (0); 5154 5155 if (locus) 5156 push_string (locus); 5157} 5158 5159void 5160finish_init () 5161{ 5162 struct initializer_stack *p = initializer_stack; 5163 5164 /* Output subconstants (string constants, usually) 5165 that were referenced within this initializer and saved up. 5166 Must do this if and only if we called defer_addressed_constants. */ 5167 if (constructor_subconstants_deferred) 5168 output_deferred_addressed_constants (); 5169 5170 /* Free the whole constructor stack of this initializer. */ 5171 while (constructor_stack) 5172 { 5173 struct constructor_stack *q = constructor_stack; 5174 constructor_stack = q->next; 5175 free (q); 5176 } 5177 5178 /* Pop back to the data of the outer initializer (if any). */ 5179 constructor_decl = p->decl; 5180 constructor_asmspec = p->asmspec; 5181 constructor_incremental = p->incremental; 5182 require_constant_value = p->require_constant_value; 5183 require_constant_elements = p->require_constant_elements; 5184 constructor_stack = p->constructor_stack; 5185 constructor_elements = p->elements; 5186 spelling = p->spelling; 5187 spelling_base = p->spelling_base; 5188 spelling_size = p->spelling_size; 5189 constructor_subconstants_deferred = p->deferred; 5190 constructor_top_level = p->top_level; 5191 initializer_stack = p->next; 5192 free (p); 5193} 5194 5195/* Call here when we see the initializer is surrounded by braces. 5196 This is instead of a call to push_init_level; 5197 it is matched by a call to pop_init_level. 5198 5199 TYPE is the type to initialize, for a constructor expression. 5200 For an initializer for a decl, TYPE is zero. */ 5201 5202void 5203really_start_incremental_init (type) 5204 tree type; 5205{ 5206 struct constructor_stack *p 5207 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack)); 5208 5209 if (type == 0) 5210 type = TREE_TYPE (constructor_decl); 5211 5212 /* Turn off constructor_incremental if type is a struct with bitfields. 5213 Do this before the first push, so that the corrected value 5214 is available in finish_init. */ 5215 check_init_type_bitfields (type); 5216 5217 p->type = constructor_type; 5218 p->fields = constructor_fields; 5219 p->index = constructor_index; 5220 p->range_end = constructor_range_end; 5221 p->max_index = constructor_max_index; 5222 p->unfilled_index = constructor_unfilled_index; 5223 p->unfilled_fields = constructor_unfilled_fields; 5224 p->bit_index = constructor_bit_index; 5225 p->elements = constructor_elements; 5226 p->constant = constructor_constant; 5227 p->simple = constructor_simple; 5228 p->erroneous = constructor_erroneous; 5229 p->pending_elts = constructor_pending_elts; 5230 p->depth = constructor_depth; 5231 p->replacement_value = 0; 5232 p->implicit = 0; 5233 p->incremental = constructor_incremental; 5234 p->outer = 0; 5235 p->next = 0; 5236 constructor_stack = p; 5237 5238 constructor_constant = 1; 5239 constructor_simple = 1; 5240 constructor_depth = SPELLING_DEPTH (); 5241 constructor_elements = 0; 5242 constructor_pending_elts = 0; 5243 constructor_type = type; 5244 5245 if (TREE_CODE (constructor_type) == RECORD_TYPE 5246 || TREE_CODE (constructor_type) == UNION_TYPE) 5247 { 5248 constructor_fields = TYPE_FIELDS (constructor_type); 5249 /* Skip any nameless bit fields at the beginning. */ 5250 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields) 5251 && DECL_NAME (constructor_fields) == 0) 5252 constructor_fields = TREE_CHAIN (constructor_fields); 5253 constructor_unfilled_fields = constructor_fields; 5254 constructor_bit_index = copy_node (integer_zero_node); 5255 } 5256 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5257 { 5258 constructor_range_end = 0; 5259 if (TYPE_DOMAIN (constructor_type)) 5260 { 5261 constructor_max_index 5262 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); 5263 constructor_index 5264 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 5265 } 5266 else 5267 constructor_index = copy_node (integer_zero_node); 5268 constructor_unfilled_index = copy_node (constructor_index); 5269 } 5270 else 5271 { 5272 /* Handle the case of int x = {5}; */ 5273 constructor_fields = constructor_type; 5274 constructor_unfilled_fields = constructor_type; 5275 } 5276 5277 if (constructor_incremental) 5278 { 5279 int momentary = suspend_momentary (); 5280 push_obstacks_nochange (); 5281 if (TREE_PERMANENT (constructor_decl)) 5282 end_temporary_allocation (); 5283 make_decl_rtl (constructor_decl, constructor_asmspec, 5284 constructor_top_level); 5285 assemble_variable (constructor_decl, constructor_top_level, 0, 1); 5286 pop_obstacks (); 5287 resume_momentary (momentary); 5288 } 5289 5290 if (constructor_incremental) 5291 { 5292 defer_addressed_constants (); 5293 constructor_subconstants_deferred = 1; 5294 } 5295} 5296 5297/* Push down into a subobject, for initialization. 5298 If this is for an explicit set of braces, IMPLICIT is 0. 5299 If it is because the next element belongs at a lower level, 5300 IMPLICIT is 1. */ 5301 5302void 5303push_init_level (implicit) 5304 int implicit; 5305{ 5306 struct constructor_stack *p; 5307 5308 /* If we've exhausted any levels that didn't have braces, 5309 pop them now. */ 5310 while (constructor_stack->implicit) 5311 { 5312 if ((TREE_CODE (constructor_type) == RECORD_TYPE 5313 || TREE_CODE (constructor_type) == UNION_TYPE) 5314 && constructor_fields == 0) 5315 process_init_element (pop_init_level (1)); 5316 else if (TREE_CODE (constructor_type) == ARRAY_TYPE 5317 && tree_int_cst_lt (constructor_max_index, constructor_index)) 5318 process_init_element (pop_init_level (1)); 5319 else 5320 break; 5321 } 5322 5323 /* Structure elements may require alignment. Do this now 5324 if necessary for the subaggregate. */ 5325 if (constructor_incremental && constructor_type != 0 5326 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields) 5327 { 5328 /* Advance to offset of this element. */ 5329 if (! tree_int_cst_equal (constructor_bit_index, 5330 DECL_FIELD_BITPOS (constructor_fields))) 5331 { 5332 int next = (TREE_INT_CST_LOW 5333 (DECL_FIELD_BITPOS (constructor_fields)) 5334 / BITS_PER_UNIT); 5335 int here = (TREE_INT_CST_LOW (constructor_bit_index) 5336 / BITS_PER_UNIT); 5337 5338 assemble_zeros (next - here); 5339 } 5340 } 5341 5342 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack)); 5343 p->type = constructor_type; 5344 p->fields = constructor_fields; 5345 p->index = constructor_index; 5346 p->range_end = constructor_range_end; 5347 p->max_index = constructor_max_index; 5348 p->unfilled_index = constructor_unfilled_index; 5349 p->unfilled_fields = constructor_unfilled_fields; 5350 p->bit_index = constructor_bit_index; 5351 p->elements = constructor_elements; 5352 p->constant = constructor_constant; 5353 p->simple = constructor_simple; 5354 p->erroneous = constructor_erroneous; 5355 p->pending_elts = constructor_pending_elts; 5356 p->depth = constructor_depth; 5357 p->replacement_value = 0; 5358 p->implicit = implicit; 5359 p->incremental = constructor_incremental; 5360 p->outer = 0; 5361 p->next = constructor_stack; 5362 constructor_stack = p; 5363 5364 constructor_constant = 1; 5365 constructor_simple = 1; 5366 constructor_depth = SPELLING_DEPTH (); 5367 constructor_elements = 0; 5368 constructor_pending_elts = 0; 5369 5370 /* Don't die if an entire brace-pair level is superfluous 5371 in the containing level. */ 5372 if (constructor_type == 0) 5373 ; 5374 else if (TREE_CODE (constructor_type) == RECORD_TYPE 5375 || TREE_CODE (constructor_type) == UNION_TYPE) 5376 { 5377 /* Don't die if there are extra init elts at the end. */ 5378 if (constructor_fields == 0) 5379 constructor_type = 0; 5380 else 5381 { 5382 constructor_type = TREE_TYPE (constructor_fields); 5383 push_member_name (constructor_fields); 5384 constructor_depth++; 5385 if (constructor_fields != constructor_unfilled_fields) 5386 constructor_incremental = 0; 5387 } 5388 } 5389 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5390 { 5391 constructor_type = TREE_TYPE (constructor_type); 5392 push_array_bounds (TREE_INT_CST_LOW (constructor_index)); 5393 constructor_depth++; 5394 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index) 5395 || constructor_range_end != 0) 5396 constructor_incremental = 0; 5397 } 5398 5399 if (constructor_type == 0) 5400 { 5401 error_init ("extra brace group at end of initializer%s", 5402 " for `%s'", NULL); 5403 constructor_fields = 0; 5404 constructor_unfilled_fields = 0; 5405 return; 5406 } 5407 5408 /* Turn off constructor_incremental if type is a struct with bitfields. */ 5409 check_init_type_bitfields (constructor_type); 5410 5411 if (implicit && warn_missing_braces && !missing_braces_mentioned) 5412 { 5413 missing_braces_mentioned = 1; 5414 warning_init ("missing braces around initializer%s", " for `%s'", NULL); 5415 } 5416 5417 if (TREE_CODE (constructor_type) == RECORD_TYPE 5418 || TREE_CODE (constructor_type) == UNION_TYPE) 5419 { 5420 constructor_fields = TYPE_FIELDS (constructor_type); 5421 /* Skip any nameless bit fields at the beginning. */ 5422 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields) 5423 && DECL_NAME (constructor_fields) == 0) 5424 constructor_fields = TREE_CHAIN (constructor_fields); 5425 constructor_unfilled_fields = constructor_fields; 5426 constructor_bit_index = copy_node (integer_zero_node); 5427 } 5428 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5429 { 5430 constructor_range_end = 0; 5431 if (TYPE_DOMAIN (constructor_type)) 5432 { 5433 constructor_max_index 5434 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); 5435 constructor_index 5436 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 5437 } 5438 else 5439 constructor_index = copy_node (integer_zero_node); 5440 constructor_unfilled_index = copy_node (constructor_index); 5441 } 5442 else 5443 { 5444 warning_init ("braces around scalar initializer%s", " for `%s'", NULL); 5445 constructor_fields = constructor_type; 5446 constructor_unfilled_fields = constructor_type; 5447 } 5448} 5449 5450/* Don't read a struct incrementally if it has any bitfields, 5451 because the incremental reading code doesn't know how to 5452 handle bitfields yet. */ 5453 5454static void 5455check_init_type_bitfields (type) 5456 tree type; 5457{ 5458 if (TREE_CODE (type) == RECORD_TYPE) 5459 { 5460 tree tail; 5461 for (tail = TYPE_FIELDS (type); tail; 5462 tail = TREE_CHAIN (tail)) 5463 { 5464 if (DECL_BIT_FIELD (tail) 5465 /* This catches cases like `int foo : 8;'. */ 5466 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail))) 5467 { 5468 constructor_incremental = 0; 5469 break; 5470 } 5471 5472 check_init_type_bitfields (TREE_TYPE (tail)); 5473 } 5474 } 5475 5476 else if (TREE_CODE (type) == ARRAY_TYPE) 5477 check_init_type_bitfields (TREE_TYPE (type)); 5478} 5479 5480/* At the end of an implicit or explicit brace level, 5481 finish up that level of constructor. 5482 If we were outputting the elements as they are read, return 0 5483 from inner levels (process_init_element ignores that), 5484 but return error_mark_node from the outermost level 5485 (that's what we want to put in DECL_INITIAL). 5486 Otherwise, return a CONSTRUCTOR expression. */ 5487 5488tree 5489pop_init_level (implicit) 5490 int implicit; 5491{ 5492 struct constructor_stack *p; 5493 int size = 0; 5494 tree constructor = 0; 5495 5496 if (implicit == 0) 5497 { 5498 /* When we come to an explicit close brace, 5499 pop any inner levels that didn't have explicit braces. */ 5500 while (constructor_stack->implicit) 5501 process_init_element (pop_init_level (1)); 5502 } 5503 5504 p = constructor_stack; 5505 5506 if (constructor_type != 0) 5507 size = int_size_in_bytes (constructor_type); 5508 5509 /* Now output all pending elements. */ 5510 output_pending_init_elements (1); 5511 5512#if 0 /* c-parse.in warns about {}. */ 5513 /* In ANSI, each brace level must have at least one element. */ 5514 if (! implicit && pedantic 5515 && (TREE_CODE (constructor_type) == ARRAY_TYPE 5516 ? integer_zerop (constructor_unfilled_index) 5517 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type))) 5518 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL); 5519#endif 5520 5521 /* Pad out the end of the structure. */ 5522 5523 if (p->replacement_value) 5524 { 5525 /* If this closes a superfluous brace pair, 5526 just pass out the element between them. */ 5527 constructor = p->replacement_value; 5528 /* If this is the top level thing within the initializer, 5529 and it's for a variable, then since we already called 5530 assemble_variable, we must output the value now. */ 5531 if (p->next == 0 && constructor_decl != 0 5532 && constructor_incremental) 5533 { 5534 constructor = digest_init (constructor_type, constructor, 5535 require_constant_value, 5536 require_constant_elements); 5537 5538 /* If initializing an array of unknown size, 5539 determine the size now. */ 5540 if (TREE_CODE (constructor_type) == ARRAY_TYPE 5541 && TYPE_DOMAIN (constructor_type) == 0) 5542 { 5543 int failure; 5544 int momentary_p; 5545 5546 push_obstacks_nochange (); 5547 if (TREE_PERMANENT (constructor_type)) 5548 end_temporary_allocation (); 5549 5550 momentary_p = suspend_momentary (); 5551 5552 /* We shouldn't have an incomplete array type within 5553 some other type. */ 5554 if (constructor_stack->next) 5555 abort (); 5556 5557 failure 5558 = complete_array_type (constructor_type, 5559 constructor, 0); 5560 if (failure) 5561 abort (); 5562 5563 size = int_size_in_bytes (constructor_type); 5564 resume_momentary (momentary_p); 5565 pop_obstacks (); 5566 } 5567 5568 output_constant (constructor, size); 5569 } 5570 } 5571 else if (constructor_type == 0) 5572 ; 5573 else if (TREE_CODE (constructor_type) != RECORD_TYPE 5574 && TREE_CODE (constructor_type) != UNION_TYPE 5575 && TREE_CODE (constructor_type) != ARRAY_TYPE 5576 && ! constructor_incremental) 5577 { 5578 /* A nonincremental scalar initializer--just return 5579 the element, after verifying there is just one. */ 5580 if (constructor_elements == 0) 5581 { 5582 error_init ("empty scalar initializer%s", 5583 " for `%s'", NULL); 5584 constructor = error_mark_node; 5585 } 5586 else if (TREE_CHAIN (constructor_elements) != 0) 5587 { 5588 error_init ("extra elements in scalar initializer%s", 5589 " for `%s'", NULL); 5590 constructor = TREE_VALUE (constructor_elements); 5591 } 5592 else 5593 constructor = TREE_VALUE (constructor_elements); 5594 } 5595 else if (! constructor_incremental) 5596 { 5597 if (constructor_erroneous) 5598 constructor = error_mark_node; 5599 else 5600 { 5601 int momentary = suspend_momentary (); 5602 5603 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE, 5604 nreverse (constructor_elements)); 5605 if (constructor_constant) 5606 TREE_CONSTANT (constructor) = 1; 5607 if (constructor_constant && constructor_simple) 5608 TREE_STATIC (constructor) = 1; 5609 5610 resume_momentary (momentary); 5611 } 5612 } 5613 else 5614 { 5615 tree filled; 5616 int momentary = suspend_momentary (); 5617 5618 if (TREE_CODE (constructor_type) == RECORD_TYPE 5619 || TREE_CODE (constructor_type) == UNION_TYPE) 5620 { 5621 /* Find the offset of the end of that field. */ 5622 filled = size_binop (CEIL_DIV_EXPR, 5623 constructor_bit_index, 5624 size_int (BITS_PER_UNIT)); 5625 } 5626 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5627 { 5628 /* If initializing an array of unknown size, 5629 determine the size now. */ 5630 if (TREE_CODE (constructor_type) == ARRAY_TYPE 5631 && TYPE_DOMAIN (constructor_type) == 0) 5632 { 5633 tree maxindex 5634 = size_binop (MINUS_EXPR, 5635 constructor_unfilled_index, 5636 integer_one_node); 5637 5638 push_obstacks_nochange (); 5639 if (TREE_PERMANENT (constructor_type)) 5640 end_temporary_allocation (); 5641 maxindex = copy_node (maxindex); 5642 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex); 5643 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type); 5644 5645 /* TYPE_MAX_VALUE is always one less than the number of elements 5646 in the array, because we start counting at zero. Therefore, 5647 warn only if the value is less than zero. */ 5648 if (pedantic 5649 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) 5650 < 0)) 5651 error_with_decl (constructor_decl, 5652 "zero or negative array size `%s'"); 5653 layout_type (constructor_type); 5654 size = int_size_in_bytes (constructor_type); 5655 pop_obstacks (); 5656 } 5657 5658 filled = size_binop (MULT_EXPR, constructor_unfilled_index, 5659 size_in_bytes (TREE_TYPE (constructor_type))); 5660 } 5661 else 5662 filled = 0; 5663 5664 if (filled != 0) 5665 assemble_zeros (size - TREE_INT_CST_LOW (filled)); 5666 5667 resume_momentary (momentary); 5668 } 5669 5670 5671 constructor_type = p->type; 5672 constructor_fields = p->fields; 5673 constructor_index = p->index; 5674 constructor_range_end = p->range_end; 5675 constructor_max_index = p->max_index; 5676 constructor_unfilled_index = p->unfilled_index; 5677 constructor_unfilled_fields = p->unfilled_fields; 5678 constructor_bit_index = p->bit_index; 5679 constructor_elements = p->elements; 5680 constructor_constant = p->constant; 5681 constructor_simple = p->simple; 5682 constructor_erroneous = p->erroneous; 5683 constructor_pending_elts = p->pending_elts; 5684 constructor_depth = p->depth; 5685 constructor_incremental = p->incremental; 5686 RESTORE_SPELLING_DEPTH (constructor_depth); 5687 5688 constructor_stack = p->next; 5689 free (p); 5690 5691 if (constructor == 0) 5692 { 5693 if (constructor_stack == 0) 5694 return error_mark_node; 5695 return NULL_TREE; 5696 } 5697 return constructor; 5698} 5699 5700/* Within an array initializer, specify the next index to be initialized. 5701 FIRST is that index. If LAST is nonzero, then initialize a range 5702 of indices, running from FIRST through LAST. */ 5703 5704void 5705set_init_index (first, last) 5706 tree first, last; 5707{ 5708 while ((TREE_CODE (first) == NOP_EXPR 5709 || TREE_CODE (first) == CONVERT_EXPR 5710 || TREE_CODE (first) == NON_LVALUE_EXPR) 5711 && (TYPE_MODE (TREE_TYPE (first)) 5712 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0))))) 5713 (first) = TREE_OPERAND (first, 0); 5714 if (last) 5715 while ((TREE_CODE (last) == NOP_EXPR 5716 || TREE_CODE (last) == CONVERT_EXPR 5717 || TREE_CODE (last) == NON_LVALUE_EXPR) 5718 && (TYPE_MODE (TREE_TYPE (last)) 5719 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0))))) 5720 (last) = TREE_OPERAND (last, 0); 5721 5722 if (TREE_CODE (first) != INTEGER_CST) 5723 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL); 5724 else if (last != 0 && TREE_CODE (last) != INTEGER_CST) 5725 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL); 5726 else if (! constructor_unfilled_index) 5727 error_init ("array index in non-array initializer%s", " for `%s'", NULL); 5728 else if (tree_int_cst_lt (first, constructor_unfilled_index)) 5729 error_init ("duplicate array index in initializer%s", " for `%s'", NULL); 5730 else 5731 { 5732 TREE_INT_CST_LOW (constructor_index) 5733 = TREE_INT_CST_LOW (first); 5734 TREE_INT_CST_HIGH (constructor_index) 5735 = TREE_INT_CST_HIGH (first); 5736 5737 if (last != 0 && tree_int_cst_lt (last, first)) 5738 error_init ("empty index range in initializer%s", " for `%s'", NULL); 5739 else 5740 { 5741 if (pedantic) 5742 pedwarn ("ANSI C forbids specifying element to initialize"); 5743 constructor_range_end = last; 5744 } 5745 } 5746} 5747 5748/* Within a struct initializer, specify the next field to be initialized. */ 5749 5750void 5751set_init_label (fieldname) 5752 tree fieldname; 5753{ 5754 tree tail; 5755 int passed = 0; 5756 5757 /* Don't die if an entire brace-pair level is superfluous 5758 in the containing level. */ 5759 if (constructor_type == 0) 5760 return; 5761 5762 for (tail = TYPE_FIELDS (constructor_type); tail; 5763 tail = TREE_CHAIN (tail)) 5764 { 5765 if (tail == constructor_unfilled_fields) 5766 passed = 1; 5767 if (DECL_NAME (tail) == fieldname) 5768 break; 5769 } 5770 5771 if (tail == 0) 5772 error ("unknown field `%s' specified in initializer", 5773 IDENTIFIER_POINTER (fieldname)); 5774 else if (!passed) 5775 error ("field `%s' already initialized", 5776 IDENTIFIER_POINTER (fieldname)); 5777 else 5778 { 5779 constructor_fields = tail; 5780 if (pedantic) 5781 pedwarn ("ANSI C forbids specifying structure member to initialize"); 5782 } 5783} 5784 5785/* "Output" the next constructor element. 5786 At top level, really output it to assembler code now. 5787 Otherwise, collect it in a list from which we will make a CONSTRUCTOR. 5788 TYPE is the data type that the containing data type wants here. 5789 FIELD is the field (a FIELD_DECL) or the index that this element fills. 5790 5791 PENDING if non-nil means output pending elements that belong 5792 right after this element. (PENDING is normally 1; 5793 it is 0 while outputting pending elements, to avoid recursion.) */ 5794 5795static void 5796output_init_element (value, type, field, pending) 5797 tree value, type, field; 5798 int pending; 5799{ 5800 int duplicate = 0; 5801 5802 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE 5803 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE 5804 && !(TREE_CODE (value) == STRING_CST 5805 && TREE_CODE (type) == ARRAY_TYPE 5806 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE) 5807 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), 5808 TYPE_MAIN_VARIANT (type)))) 5809 value = default_conversion (value); 5810 5811 if (value == error_mark_node) 5812 constructor_erroneous = 1; 5813 else if (!TREE_CONSTANT (value)) 5814 constructor_constant = 0; 5815 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0 5816 || ((TREE_CODE (constructor_type) == RECORD_TYPE 5817 || TREE_CODE (constructor_type) == UNION_TYPE) 5818 && DECL_BIT_FIELD (field) && TREE_CODE (value) != INTEGER_CST)) 5819 constructor_simple = 0; 5820 5821 if (require_constant_value && ! TREE_CONSTANT (value)) 5822 { 5823 error_init ("initializer element%s is not constant", 5824 " for `%s'", NULL); 5825 value = error_mark_node; 5826 } 5827 else if (require_constant_elements 5828 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) 5829 { 5830 error_init ("initializer element%s is not computable at load time", 5831 " for `%s'", NULL); 5832 value = error_mark_node; 5833 } 5834 5835 /* If this element duplicates one on constructor_pending_elts, 5836 print a message and ignore it. Don't do this when we're 5837 processing elements taken off constructor_pending_elts, 5838 because we'd always get spurious errors. */ 5839 if (pending) 5840 { 5841 if (TREE_CODE (constructor_type) == RECORD_TYPE 5842 || TREE_CODE (constructor_type) == UNION_TYPE) 5843 { 5844 if (purpose_member (field, constructor_pending_elts)) 5845 { 5846 error_init ("duplicate initializer%s", " for `%s'", NULL); 5847 duplicate = 1; 5848 } 5849 } 5850 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5851 { 5852 tree tail; 5853 for (tail = constructor_pending_elts; tail; 5854 tail = TREE_CHAIN (tail)) 5855 if (TREE_PURPOSE (tail) != 0 5856 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST 5857 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index)) 5858 break; 5859 5860 if (tail != 0) 5861 { 5862 error_init ("duplicate initializer%s", " for `%s'", NULL); 5863 duplicate = 1; 5864 } 5865 } 5866 } 5867 5868 /* If this element doesn't come next in sequence, 5869 put it on constructor_pending_elts. */ 5870 if (TREE_CODE (constructor_type) == ARRAY_TYPE 5871 && !tree_int_cst_equal (field, constructor_unfilled_index)) 5872 { 5873 if (! duplicate) 5874 /* The copy_node is needed in case field is actually 5875 constructor_index, which is modified in place. */ 5876 constructor_pending_elts 5877 = tree_cons (copy_node (field), 5878 digest_init (type, value, require_constant_value, 5879 require_constant_elements), 5880 constructor_pending_elts); 5881 } 5882 else if (TREE_CODE (constructor_type) == RECORD_TYPE 5883 && field != constructor_unfilled_fields) 5884 { 5885 /* We do this for records but not for unions. In a union, 5886 no matter which field is specified, it can be initialized 5887 right away since it starts at the beginning of the union. */ 5888 if (!duplicate) 5889 constructor_pending_elts 5890 = tree_cons (field, 5891 digest_init (type, value, require_constant_value, 5892 require_constant_elements), 5893 constructor_pending_elts); 5894 } 5895 else 5896 { 5897 /* Otherwise, output this element either to 5898 constructor_elements or to the assembler file. */ 5899 5900 if (!duplicate) 5901 { 5902 if (! constructor_incremental) 5903 { 5904 if (field && TREE_CODE (field) == INTEGER_CST) 5905 field = copy_node (field); 5906 constructor_elements 5907 = tree_cons (field, digest_init (type, value, 5908 require_constant_value, 5909 require_constant_elements), 5910 constructor_elements); 5911 } 5912 else 5913 { 5914 /* Structure elements may require alignment. 5915 Do this, if necessary. */ 5916 if (TREE_CODE (constructor_type) == RECORD_TYPE) 5917 { 5918 /* Advance to offset of this element. */ 5919 if (! tree_int_cst_equal (constructor_bit_index, 5920 DECL_FIELD_BITPOS (field))) 5921 { 5922 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field)) 5923 / BITS_PER_UNIT); 5924 int here = (TREE_INT_CST_LOW (constructor_bit_index) 5925 / BITS_PER_UNIT); 5926 5927 assemble_zeros (next - here); 5928 } 5929 } 5930 output_constant (digest_init (type, value, 5931 require_constant_value, 5932 require_constant_elements), 5933 int_size_in_bytes (type)); 5934 5935 /* For a record or union, 5936 keep track of end position of last field. */ 5937 if (TREE_CODE (constructor_type) == RECORD_TYPE 5938 || TREE_CODE (constructor_type) == UNION_TYPE) 5939 { 5940 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field), 5941 DECL_SIZE (field)); 5942 TREE_INT_CST_LOW (constructor_bit_index) 5943 = TREE_INT_CST_LOW (temp); 5944 TREE_INT_CST_HIGH (constructor_bit_index) 5945 = TREE_INT_CST_HIGH (temp); 5946 } 5947 } 5948 } 5949 5950 /* Advance the variable that indicates sequential elements output. */ 5951 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5952 { 5953 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index, 5954 integer_one_node); 5955 TREE_INT_CST_LOW (constructor_unfilled_index) 5956 = TREE_INT_CST_LOW (tem); 5957 TREE_INT_CST_HIGH (constructor_unfilled_index) 5958 = TREE_INT_CST_HIGH (tem); 5959 } 5960 else if (TREE_CODE (constructor_type) == RECORD_TYPE) 5961 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); 5962 else if (TREE_CODE (constructor_type) == UNION_TYPE) 5963 constructor_unfilled_fields = 0; 5964 5965 /* Now output any pending elements which have become next. */ 5966 if (pending) 5967 output_pending_init_elements (0); 5968 } 5969} 5970 5971/* Output any pending elements which have become next. 5972 As we output elements, constructor_unfilled_{fields,index} 5973 advances, which may cause other elements to become next; 5974 if so, they too are output. 5975 5976 If ALL is 0, we return when there are 5977 no more pending elements to output now. 5978 5979 If ALL is 1, we output space as necessary so that 5980 we can output all the pending elements. */ 5981 5982static void 5983output_pending_init_elements (all) 5984 int all; 5985{ 5986 tree tail; 5987 tree next; 5988 5989 retry: 5990 5991 /* Look thru the whole pending list. 5992 If we find an element that should be output now, 5993 output it. Otherwise, set NEXT to the element 5994 that comes first among those still pending. */ 5995 5996 next = 0; 5997 for (tail = constructor_pending_elts; tail; 5998 tail = TREE_CHAIN (tail)) 5999 { 6000 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6001 { 6002 if (tree_int_cst_equal (TREE_PURPOSE (tail), 6003 constructor_unfilled_index)) 6004 { 6005 output_init_element (TREE_VALUE (tail), 6006 TREE_TYPE (constructor_type), 6007 constructor_unfilled_index, 0); 6008 goto retry; 6009 } 6010 else if (tree_int_cst_lt (TREE_PURPOSE (tail), 6011 constructor_unfilled_index)) 6012 ; 6013 else if (next == 0 6014 || tree_int_cst_lt (TREE_PURPOSE (tail), next)) 6015 next = TREE_PURPOSE (tail); 6016 } 6017 else if (TREE_CODE (constructor_type) == RECORD_TYPE 6018 || TREE_CODE (constructor_type) == UNION_TYPE) 6019 { 6020 if (TREE_PURPOSE (tail) == constructor_unfilled_fields) 6021 { 6022 output_init_element (TREE_VALUE (tail), 6023 TREE_TYPE (constructor_unfilled_fields), 6024 constructor_unfilled_fields, 6025 0); 6026 goto retry; 6027 } 6028 else if (constructor_unfilled_fields == 0 6029 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)), 6030 DECL_FIELD_BITPOS (constructor_unfilled_fields))) 6031 ; 6032 else if (next == 0 6033 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)), 6034 DECL_FIELD_BITPOS (next))) 6035 next = TREE_PURPOSE (tail); 6036 } 6037 } 6038 6039 /* Ordinarily return, but not if we want to output all 6040 and there are elements left. */ 6041 if (! (all && next != 0)) 6042 return; 6043 6044 /* Generate space up to the position of NEXT. */ 6045 if (constructor_incremental) 6046 { 6047 tree filled; 6048 tree nextpos_tree = size_int (0); 6049 6050 if (TREE_CODE (constructor_type) == RECORD_TYPE 6051 || TREE_CODE (constructor_type) == UNION_TYPE) 6052 { 6053 /* Find the last field written out, if any. */ 6054 for (tail = TYPE_FIELDS (constructor_type); tail; 6055 tail = TREE_CHAIN (tail)) 6056 if (TREE_CHAIN (tail) == constructor_unfilled_fields) 6057 break; 6058 6059 if (tail) 6060 /* Find the offset of the end of that field. */ 6061 filled = size_binop (CEIL_DIV_EXPR, 6062 size_binop (PLUS_EXPR, 6063 DECL_FIELD_BITPOS (tail), 6064 DECL_SIZE (tail)), 6065 size_int (BITS_PER_UNIT)); 6066 else 6067 filled = size_int (0); 6068 6069 nextpos_tree = size_binop (CEIL_DIV_EXPR, 6070 DECL_FIELD_BITPOS (next), 6071 size_int (BITS_PER_UNIT)); 6072 6073 TREE_INT_CST_HIGH (constructor_bit_index) 6074 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next)); 6075 TREE_INT_CST_LOW (constructor_bit_index) 6076 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next)); 6077 constructor_unfilled_fields = next; 6078 } 6079 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6080 { 6081 filled = size_binop (MULT_EXPR, constructor_unfilled_index, 6082 size_in_bytes (TREE_TYPE (constructor_type))); 6083 nextpos_tree 6084 = size_binop (MULT_EXPR, next, 6085 size_in_bytes (TREE_TYPE (constructor_type))); 6086 TREE_INT_CST_LOW (constructor_unfilled_index) 6087 = TREE_INT_CST_LOW (next); 6088 TREE_INT_CST_HIGH (constructor_unfilled_index) 6089 = TREE_INT_CST_HIGH (next); 6090 } 6091 else 6092 filled = 0; 6093 6094 if (filled) 6095 { 6096 int nextpos = TREE_INT_CST_LOW (nextpos_tree); 6097 6098 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled)); 6099 } 6100 } 6101 else 6102 { 6103 /* If it's not incremental, just skip over the gap, 6104 so that after jumping to retry we will output the next 6105 successive element. */ 6106 if (TREE_CODE (constructor_type) == RECORD_TYPE 6107 || TREE_CODE (constructor_type) == UNION_TYPE) 6108 constructor_unfilled_fields = next; 6109 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6110 { 6111 TREE_INT_CST_LOW (constructor_unfilled_index) 6112 = TREE_INT_CST_LOW (next); 6113 TREE_INT_CST_HIGH (constructor_unfilled_index) 6114 = TREE_INT_CST_HIGH (next); 6115 } 6116 } 6117 6118 goto retry; 6119} 6120 6121/* Add one non-braced element to the current constructor level. 6122 This adjusts the current position within the constructor's type. 6123 This may also start or terminate implicit levels 6124 to handle a partly-braced initializer. 6125 6126 Once this has found the correct level for the new element, 6127 it calls output_init_element. 6128 6129 Note: if we are incrementally outputting this constructor, 6130 this function may be called with a null argument 6131 representing a sub-constructor that was already incrementally output. 6132 When that happens, we output nothing, but we do the bookkeeping 6133 to skip past that element of the current constructor. */ 6134 6135void 6136process_init_element (value) 6137 tree value; 6138{ 6139 tree orig_value = value; 6140 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST; 6141 6142 /* Handle superfluous braces around string cst as in 6143 char x[] = {"foo"}; */ 6144 if (string_flag 6145 && constructor_type 6146 && TREE_CODE (constructor_type) == ARRAY_TYPE 6147 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE 6148 && integer_zerop (constructor_unfilled_index)) 6149 { 6150 constructor_stack->replacement_value = value; 6151 return; 6152 } 6153 6154 if (constructor_stack->replacement_value != 0) 6155 { 6156 error_init ("excess elements in struct initializer%s", 6157 " after `%s'", NULL_PTR); 6158 return; 6159 } 6160 6161 /* Ignore elements of a brace group if it is entirely superfluous 6162 and has already been diagnosed. */ 6163 if (constructor_type == 0) 6164 return; 6165 6166 /* If we've exhausted any levels that didn't have braces, 6167 pop them now. */ 6168 while (constructor_stack->implicit) 6169 { 6170 if ((TREE_CODE (constructor_type) == RECORD_TYPE 6171 || TREE_CODE (constructor_type) == UNION_TYPE) 6172 && constructor_fields == 0) 6173 process_init_element (pop_init_level (1)); 6174 else if (TREE_CODE (constructor_type) == ARRAY_TYPE 6175 && tree_int_cst_lt (constructor_max_index, constructor_index)) 6176 process_init_element (pop_init_level (1)); 6177 else 6178 break; 6179 } 6180 6181 while (1) 6182 { 6183 if (TREE_CODE (constructor_type) == RECORD_TYPE) 6184 { 6185 tree fieldtype; 6186 enum tree_code fieldcode; 6187 6188 if (constructor_fields == 0) 6189 { 6190 pedwarn_init ("excess elements in struct initializer%s", 6191 " after `%s'", NULL_PTR); 6192 break; 6193 } 6194 6195 fieldtype = TREE_TYPE (constructor_fields); 6196 if (fieldtype != error_mark_node) 6197 fieldtype = TYPE_MAIN_VARIANT (fieldtype); 6198 fieldcode = TREE_CODE (fieldtype); 6199 6200 /* Accept a string constant to initialize a subarray. */ 6201 if (value != 0 6202 && fieldcode == ARRAY_TYPE 6203 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE 6204 && string_flag) 6205 value = orig_value; 6206 /* Otherwise, if we have come to a subaggregate, 6207 and we don't have an element of its type, push into it. */ 6208 else if (value != 0 && !constructor_no_implicit 6209 && value != error_mark_node 6210 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype 6211 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE 6212 || fieldcode == UNION_TYPE)) 6213 { 6214 push_init_level (1); 6215 continue; 6216 } 6217 6218 if (value) 6219 { 6220 push_member_name (constructor_fields); 6221 output_init_element (value, fieldtype, constructor_fields, 1); 6222 RESTORE_SPELLING_DEPTH (constructor_depth); 6223 } 6224 else 6225 /* Do the bookkeeping for an element that was 6226 directly output as a constructor. */ 6227 { 6228 /* For a record, keep track of end position of last field. */ 6229 tree temp = size_binop (PLUS_EXPR, 6230 DECL_FIELD_BITPOS (constructor_fields), 6231 DECL_SIZE (constructor_fields)); 6232 TREE_INT_CST_LOW (constructor_bit_index) 6233 = TREE_INT_CST_LOW (temp); 6234 TREE_INT_CST_HIGH (constructor_bit_index) 6235 = TREE_INT_CST_HIGH (temp); 6236 6237 constructor_unfilled_fields = TREE_CHAIN (constructor_fields); 6238 } 6239 6240 constructor_fields = TREE_CHAIN (constructor_fields); 6241 /* Skip any nameless bit fields at the beginning. */ 6242 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields) 6243 && DECL_NAME (constructor_fields) == 0) 6244 constructor_fields = TREE_CHAIN (constructor_fields); 6245 break; 6246 } 6247 if (TREE_CODE (constructor_type) == UNION_TYPE) 6248 { 6249 tree fieldtype; 6250 enum tree_code fieldcode; 6251 6252 if (constructor_fields == 0) 6253 { 6254 pedwarn_init ("excess elements in union initializer%s", 6255 " after `%s'", NULL_PTR); 6256 break; 6257 } 6258 6259 fieldtype = TREE_TYPE (constructor_fields); 6260 if (fieldtype != error_mark_node) 6261 fieldtype = TYPE_MAIN_VARIANT (fieldtype); 6262 fieldcode = TREE_CODE (fieldtype); 6263 6264 /* Accept a string constant to initialize a subarray. */ 6265 if (value != 0 6266 && fieldcode == ARRAY_TYPE 6267 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE 6268 && string_flag) 6269 value = orig_value; 6270 /* Otherwise, if we have come to a subaggregate, 6271 and we don't have an element of its type, push into it. */ 6272 else if (value != 0 && !constructor_no_implicit 6273 && value != error_mark_node 6274 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype 6275 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE 6276 || fieldcode == UNION_TYPE)) 6277 { 6278 push_init_level (1); 6279 continue; 6280 } 6281 6282 if (value) 6283 { 6284 push_member_name (constructor_fields); 6285 output_init_element (value, fieldtype, constructor_fields, 1); 6286 RESTORE_SPELLING_DEPTH (constructor_depth); 6287 } 6288 else 6289 /* Do the bookkeeping for an element that was 6290 directly output as a constructor. */ 6291 { 6292 TREE_INT_CST_LOW (constructor_bit_index) 6293 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields)); 6294 TREE_INT_CST_HIGH (constructor_bit_index) 6295 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields)); 6296 6297 constructor_unfilled_fields = TREE_CHAIN (constructor_fields); 6298 } 6299 6300 constructor_fields = 0; 6301 break; 6302 } 6303 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6304 { 6305 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); 6306 enum tree_code eltcode = TREE_CODE (elttype); 6307 6308 /* Accept a string constant to initialize a subarray. */ 6309 if (value != 0 6310 && eltcode == ARRAY_TYPE 6311 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE 6312 && string_flag) 6313 value = orig_value; 6314 /* Otherwise, if we have come to a subaggregate, 6315 and we don't have an element of its type, push into it. */ 6316 else if (value != 0 && !constructor_no_implicit 6317 && value != error_mark_node 6318 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype 6319 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE 6320 || eltcode == UNION_TYPE)) 6321 { 6322 push_init_level (1); 6323 continue; 6324 } 6325 6326 if (constructor_max_index != 0 6327 && tree_int_cst_lt (constructor_max_index, constructor_index)) 6328 { 6329 pedwarn_init ("excess elements in array initializer%s", 6330 " after `%s'", NULL_PTR); 6331 break; 6332 } 6333 6334 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */ 6335 if (constructor_range_end) 6336 value = save_expr (value); 6337 6338 /* Now output the actual element. 6339 Ordinarily, output once. 6340 If there is a range, repeat it till we advance past the range. */ 6341 do 6342 { 6343 tree tem; 6344 6345 if (value) 6346 { 6347 push_array_bounds (TREE_INT_CST_LOW (constructor_index)); 6348 output_init_element (value, elttype, constructor_index, 1); 6349 RESTORE_SPELLING_DEPTH (constructor_depth); 6350 } 6351 6352 tem = size_binop (PLUS_EXPR, constructor_index, 6353 integer_one_node); 6354 TREE_INT_CST_LOW (constructor_index) 6355 = TREE_INT_CST_LOW (tem); 6356 TREE_INT_CST_HIGH (constructor_index) 6357 = TREE_INT_CST_HIGH (tem); 6358 6359 if (!value) 6360 /* If we are doing the bookkeeping for an element that was 6361 directly output as a constructor, 6362 we must update constructor_unfilled_index. */ 6363 { 6364 TREE_INT_CST_LOW (constructor_unfilled_index) 6365 = TREE_INT_CST_LOW (constructor_index); 6366 TREE_INT_CST_HIGH (constructor_unfilled_index) 6367 = TREE_INT_CST_HIGH (constructor_index); 6368 } 6369 } 6370 while (! (constructor_range_end == 0 6371 || tree_int_cst_lt (constructor_range_end, 6372 constructor_index))); 6373 6374 break; 6375 } 6376 6377 /* Handle the sole element allowed in a braced initializer 6378 for a scalar variable. */ 6379 if (constructor_fields == 0) 6380 { 6381 pedwarn_init ("excess elements in scalar initializer%s", 6382 " after `%s'", NULL_PTR); 6383 break; 6384 } 6385 6386 if (value) 6387 output_init_element (value, constructor_type, NULL_TREE, 1); 6388 constructor_fields = 0; 6389 break; 6390 } 6391 6392 /* If the (lexically) previous elments are not now saved, 6393 we can discard the storage for them. */ 6394 if (constructor_incremental && constructor_pending_elts == 0 && value != 0 6395 && constructor_stack == 0) 6396 clear_momentary (); 6397} 6398 6399/* Expand an ASM statement with operands, handling output operands 6400 that are not variables or INDIRECT_REFS by transforming such 6401 cases into cases that expand_asm_operands can handle. 6402 6403 Arguments are same as for expand_asm_operands. */ 6404 6405void 6406c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line) 6407 tree string, outputs, inputs, clobbers; 6408 int vol; 6409 char *filename; 6410 int line; 6411{ 6412 int noutputs = list_length (outputs); 6413 register int i; 6414 /* o[I] is the place that output number I should be written. */ 6415 register tree *o = (tree *) alloca (noutputs * sizeof (tree)); 6416 register tree tail; 6417 6418 if (TREE_CODE (string) == ADDR_EXPR) 6419 string = TREE_OPERAND (string, 0); 6420 if (TREE_CODE (string) != STRING_CST) 6421 { 6422 error ("asm template is not a string constant"); 6423 return; 6424 } 6425 6426 /* Record the contents of OUTPUTS before it is modified. */ 6427 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) 6428 o[i] = TREE_VALUE (tail); 6429 6430 /* Perform default conversions on array and function inputs. */ 6431 /* Don't do this for other types-- 6432 it would screw up operands expected to be in memory. */ 6433 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++) 6434 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE 6435 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE) 6436 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail)); 6437 6438 /* Generate the ASM_OPERANDS insn; 6439 store into the TREE_VALUEs of OUTPUTS some trees for 6440 where the values were actually stored. */ 6441 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line); 6442 6443 /* Copy all the intermediate outputs into the specified outputs. */ 6444 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) 6445 { 6446 if (o[i] != TREE_VALUE (tail)) 6447 { 6448 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)), 6449 0, VOIDmode, 0); 6450 free_temp_slots (); 6451 } 6452 /* Detect modification of read-only values. 6453 (Otherwise done by build_modify_expr.) */ 6454 else 6455 { 6456 tree type = TREE_TYPE (o[i]); 6457 if (TREE_READONLY (o[i]) 6458 || TYPE_READONLY (type) 6459 || ((TREE_CODE (type) == RECORD_TYPE 6460 || TREE_CODE (type) == UNION_TYPE) 6461 && C_TYPE_FIELDS_READONLY (type))) 6462 readonly_warning (o[i], "modification by `asm'"); 6463 } 6464 } 6465 6466 /* Those MODIFY_EXPRs could do autoincrements. */ 6467 emit_queue (); 6468} 6469 6470/* Expand a C `return' statement. 6471 RETVAL is the expression for what to return, 6472 or a null pointer for `return;' with no value. */ 6473 6474void 6475c_expand_return (retval) 6476 tree retval; 6477{ 6478 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)); 6479 6480 if (TREE_THIS_VOLATILE (current_function_decl)) 6481 warning ("function declared `noreturn' has a `return' statement"); 6482 6483 if (!retval) 6484 { 6485 current_function_returns_null = 1; 6486 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) 6487 warning ("`return' with no value, in function returning non-void"); 6488 expand_null_return (); 6489 } 6490 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) 6491 { 6492 current_function_returns_null = 1; 6493 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) 6494 pedwarn ("`return' with a value, in function returning void"); 6495 expand_return (retval); 6496 } 6497 else 6498 { 6499 tree t = convert_for_assignment (valtype, retval, "return", 6500 NULL_TREE, NULL_TREE, 0); 6501 tree res = DECL_RESULT (current_function_decl); 6502 tree inner; 6503 6504 if (t == error_mark_node) 6505 return; 6506 6507 inner = t = convert (TREE_TYPE (res), t); 6508 6509 /* Strip any conversions, additions, and subtractions, and see if 6510 we are returning the address of a local variable. Warn if so. */ 6511 while (1) 6512 { 6513 switch (TREE_CODE (inner)) 6514 { 6515 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR: 6516 case PLUS_EXPR: 6517 inner = TREE_OPERAND (inner, 0); 6518 continue; 6519 6520 case MINUS_EXPR: 6521 /* If the second operand of the MINUS_EXPR has a pointer 6522 type (or is converted from it), this may be valid, so 6523 don't give a warning. */ 6524 { 6525 tree op1 = TREE_OPERAND (inner, 1); 6526 6527 while (! POINTER_TYPE_P (TREE_TYPE (op1)) 6528 && (TREE_CODE (op1) == NOP_EXPR 6529 || TREE_CODE (op1) == NON_LVALUE_EXPR 6530 || TREE_CODE (op1) == CONVERT_EXPR)) 6531 op1 = TREE_OPERAND (op1, 0); 6532 6533 if (POINTER_TYPE_P (TREE_TYPE (op1))) 6534 break; 6535 6536 inner = TREE_OPERAND (inner, 0); 6537 continue; 6538 } 6539 6540 case ADDR_EXPR: 6541 inner = TREE_OPERAND (inner, 0); 6542 6543 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r') 6544 inner = TREE_OPERAND (inner, 0); 6545 6546 if (TREE_CODE (inner) == VAR_DECL 6547 && ! DECL_EXTERNAL (inner) 6548 && ! TREE_STATIC (inner) 6549 && DECL_CONTEXT (inner) == current_function_decl) 6550 warning ("function returns address of local variable"); 6551 break; 6552 } 6553 6554 break; 6555 } 6556 6557 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t); 6558 TREE_SIDE_EFFECTS (t) = 1; 6559 expand_return (t); 6560 current_function_returns_value = 1; 6561 } 6562} 6563 6564/* Start a C switch statement, testing expression EXP. 6565 Return EXP if it is valid, an error node otherwise. */ 6566 6567tree 6568c_expand_start_case (exp) 6569 tree exp; 6570{ 6571 register enum tree_code code = TREE_CODE (TREE_TYPE (exp)); 6572 tree type = TREE_TYPE (exp); 6573 6574 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK) 6575 { 6576 error ("switch quantity not an integer"); 6577 exp = error_mark_node; 6578 } 6579 else 6580 { 6581 tree index; 6582 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); 6583 6584 if (warn_traditional 6585 && (type == long_integer_type_node 6586 || type == long_unsigned_type_node)) 6587 pedwarn ("`long' switch expression not converted to `int' in ANSI C"); 6588 6589 exp = default_conversion (exp); 6590 type = TREE_TYPE (exp); 6591 index = get_unwidened (exp, NULL_TREE); 6592 /* We can't strip a conversion from a signed type to an unsigned, 6593 because if we did, int_fits_type_p would do the wrong thing 6594 when checking case values for being in range, 6595 and it's too hard to do the right thing. */ 6596 if (TREE_UNSIGNED (TREE_TYPE (exp)) 6597 == TREE_UNSIGNED (TREE_TYPE (index))) 6598 exp = index; 6599 } 6600 6601 expand_start_case (1, exp, type, "switch statement"); 6602 6603 return exp; 6604} 6605