c-typeck.c revision 50397
1145247Sdamien/* Build expressions with type checking for C compiler. 2156599Sdamien Copyright (C) 1987, 88, 91-97, 1998 Free Software Foundation, Inc. 3145247Sdamien 4172567SthompsaThis file is part of GNU CC. 5172567Sthompsa 6145247SdamienGNU CC is free software; you can redistribute it and/or modify 7145247Sdamienit under the terms of the GNU General Public License as published by 8145247Sdamienthe Free Software Foundation; either version 2, or (at your option) 9145247Sdamienany later version. 10145247Sdamien 11145247SdamienGNU CC is distributed in the hope that it will be useful, 12145247Sdamienbut WITHOUT ANY WARRANTY; without even the implied warranty of 13145247SdamienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14145247SdamienGNU General Public License for more details. 15145247Sdamien 16145247SdamienYou should have received a copy of the GNU General Public License 17145247Sdamienalong with GNU CC; see the file COPYING. If not, write to 18145247Sdamienthe Free Software Foundation, 59 Temple Place - Suite 330, 19145247SdamienBoston, MA 02111-1307, USA. */ 20145247Sdamien 21145247Sdamien 22145247Sdamien/* This file is part of the C front end. 23145247Sdamien It contains routines to build C expressions given their operands, 24145247Sdamien including computing the types of the result, C-specific error checks, 25145247Sdamien and some optimization. 26145247Sdamien 27145247Sdamien There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, 28145247Sdamien and to process initializations in declarations (since they work 29145247Sdamien like a strange sort of assignment). */ 30145247Sdamien 31145247Sdamien#include "config.h" 32145247Sdamien#include "system.h" 33145247Sdamien#include "tree.h" 34145247Sdamien#include "c-tree.h" 35145247Sdamien#include "flags.h" 36145247Sdamien#include "output.h" 37145247Sdamien#include "rtl.h" 38145247Sdamien#include "expr.h" 39145247Sdamien#include "toplev.h" 40145247Sdamien 41145247Sdamien/* Nonzero if we've already printed a "missing braces around initializer" 42145247Sdamien message within this initializer. */ 43145247Sdamienstatic int missing_braces_mentioned; 44145247Sdamien 45145247Sdamienstatic tree qualify_type PROTO((tree, tree)); 46156599Sdamienstatic int comp_target_types PROTO((tree, tree)); 47156599Sdamienstatic int function_types_compatible_p PROTO((tree, tree)); 48145247Sdamienstatic int type_lists_compatible_p PROTO((tree, tree)); 49145247Sdamienstatic int self_promoting_type_p PROTO((tree)); 50145247Sdamienstatic tree decl_constant_value PROTO((tree)); 51156599Sdamienstatic tree lookup_field PROTO((tree, tree, tree *)); 52156599Sdamienstatic tree convert_arguments PROTO((tree, tree, tree, tree)); 53145247Sdamienstatic tree pointer_int_sum PROTO((enum tree_code, tree, tree)); 54145247Sdamienstatic tree pointer_diff PROTO((tree, tree)); 55145247Sdamienstatic tree unary_complex_lvalue PROTO((enum tree_code, tree)); 56145247Sdamienstatic void pedantic_lvalue_warning PROTO((enum tree_code)); 57145247Sdamienstatic tree internal_build_compound_expr PROTO((tree, int)); 58145247Sdamienstatic tree convert_for_assignment PROTO((tree, tree, char *, tree, 59145247Sdamien tree, int)); 60145247Sdamienstatic void warn_for_assignment PROTO((char *, char *, tree, int)); 61145247Sdamienstatic tree valid_compound_expr_initializer PROTO((tree, tree)); 62145247Sdamienstatic void push_string PROTO((char *)); 63257176Sglebiusstatic void push_member_name PROTO((tree)); 64145247Sdamienstatic void push_array_bounds PROTO((int)); 65145247Sdamienstatic int spelling_length PROTO((void)); 66145247Sdamienstatic char *print_spelling PROTO((char *)); 67145247Sdamienstatic char *get_spelling PROTO((char *)); 68145247Sdamienstatic void warning_init PROTO((char *, char *, 69145247Sdamien char *)); 70156599Sdamienstatic tree digest_init PROTO((tree, tree, int, int)); 71156599Sdamienstatic void check_init_type_bitfields PROTO((tree)); 72156599Sdamienstatic void output_init_element PROTO((tree, tree, tree, int)); 73145247Sdamienstatic void output_pending_init_elements PROTO((int)); 74145247Sdamienstatic void add_pending_init PROTO((tree, tree)); 75145247Sdamienstatic int pending_init_member PROTO((tree)); 76145247Sdamien 77145247Sdamien/* Do `exp = require_complete_type (exp);' to make sure exp 78145247Sdamien does not have an incomplete type. (That includes void types.) */ 79145247Sdamien 80145247Sdamientree 81145247Sdamienrequire_complete_type (value) 82172567Sthompsa tree value; 83145247Sdamien{ 84145247Sdamien tree type = TREE_TYPE (value); 85145247Sdamien 86145247Sdamien /* First, detect a valid value with a complete type. */ 87145247Sdamien if (TYPE_SIZE (type) != 0 88145247Sdamien && type != void_type_node) 89145247Sdamien return value; 90145247Sdamien 91145247Sdamien incomplete_type_error (value, type); 92145247Sdamien return error_mark_node; 93145247Sdamien} 94145247Sdamien 95156599Sdamien/* Print an error message for invalid use of an incomplete type. 96145247Sdamien VALUE is the expression that was used (or 0 if that isn't known) 97145247Sdamien and TYPE is the type that was invalid. */ 98145247Sdamien 99145247Sdamienvoid 100145247Sdamienincomplete_type_error (value, type) 101145247Sdamien tree value; 102145247Sdamien tree type; 103145247Sdamien{ 104145247Sdamien char *errmsg; 105145247Sdamien 106145247Sdamien /* Avoid duplicate error message. */ 107145247Sdamien if (TREE_CODE (type) == ERROR_MARK) 108145247Sdamien return; 109178354Ssam 110228621Sbschmidt if (value != 0 && (TREE_CODE (value) == VAR_DECL 111228621Sbschmidt || TREE_CODE (value) == PARM_DECL)) 112228621Sbschmidt error ("`%s' has an incomplete type", 113178354Ssam IDENTIFIER_POINTER (DECL_NAME (value))); 114145247Sdamien else 115145247Sdamien { 116145247Sdamien retry: 117178354Ssam /* We must print an error message. Be clever about what it says. */ 118145247Sdamien 119300239Savos switch (TREE_CODE (type)) 120172567Sthompsa { 121172567Sthompsa case RECORD_TYPE: 122172567Sthompsa errmsg = "invalid use of undefined type `struct %s'"; 123145247Sdamien break; 124145247Sdamien 125145247Sdamien case UNION_TYPE: 126145247Sdamien errmsg = "invalid use of undefined type `union %s'"; 127145247Sdamien break; 128145247Sdamien 129172567Sthompsa case ENUMERAL_TYPE: 130145247Sdamien errmsg = "invalid use of undefined type `enum %s'"; 131287197Sglebius break; 132145247Sdamien 133178354Ssam case VOID_TYPE: 134178354Ssam error ("invalid use of void expression"); 135287197Sglebius return; 136287197Sglebius 137172567Sthompsa case ARRAY_TYPE: 138287197Sglebius if (TYPE_DOMAIN (type)) 139145247Sdamien { 140172567Sthompsa type = TREE_TYPE (type); 141172567Sthompsa goto retry; 142145247Sdamien } 143156599Sdamien error ("invalid use of array with unspecified bounds"); 144156599Sdamien return; 145145247Sdamien 146191746Sthompsa default: 147191746Sthompsa abort (); 148156599Sdamien } 149145247Sdamien 150178354Ssam if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) 151145247Sdamien error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type))); 152172567Sthompsa else 153145247Sdamien /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ 154145247Sdamien error ("invalid use of incomplete typedef `%s'", 155145247Sdamien IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); 156145247Sdamien } 157172567Sthompsa} 158145247Sdamien 159145247Sdamien/* Return a variant of TYPE which has all the type qualifiers of LIKE 160145247Sdamien as well as those of TYPE. */ 161145247Sdamien 162172567Sthompsastatic tree 163156599Sdamienqualify_type (type, like) 164156599Sdamien tree type, like; 165172567Sthompsa{ 166172567Sthompsa int constflag = TYPE_READONLY (type) || TYPE_READONLY (like); 167172567Sthompsa int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like); 168300239Savos return c_build_type_variant (type, constflag, volflag); 169300239Savos} 170172567Sthompsa 171178354Ssam/* Return the common type of two types. 172178354Ssam We assume that comptypes has already been done and returned 1; 173178354Ssam if that isn't so, this may crash. In particular, we assume that qualifiers 174145247Sdamien match. 175145247Sdamien 176145247Sdamien This is the type for the result of most arithmetic operations 177145247Sdamien if the operands have the given two types. */ 178145247Sdamien 179145247Sdamientree 180145247Sdamiencommon_type (t1, t2) 181145247Sdamien tree t1, t2; 182145247Sdamien{ 183145247Sdamien register enum tree_code code1; 184145247Sdamien register enum tree_code code2; 185145247Sdamien tree attributes; 186145247Sdamien 187145247Sdamien /* Save time if the two types are the same. */ 188145247Sdamien 189145247Sdamien if (t1 == t2) return t1; 190145247Sdamien 191260062Smarius /* If one type is nonsense, use the other. */ 192145247Sdamien if (t1 == error_mark_node) 193145247Sdamien return t2; 194145247Sdamien if (t2 == error_mark_node) 195145247Sdamien return t1; 196145247Sdamien 197145247Sdamien /* Merge the attributes. */ 198145247Sdamien attributes = merge_machine_type_attributes (t1, t2); 199145247Sdamien 200145247Sdamien /* Treat an enum type as the unsigned integer type of the same width. */ 201145247Sdamien 202260062Smarius if (TREE_CODE (t1) == ENUMERAL_TYPE) 203145247Sdamien t1 = type_for_size (TYPE_PRECISION (t1), 1); 204222543Sbschmidt if (TREE_CODE (t2) == ENUMERAL_TYPE) 205222543Sbschmidt t2 = type_for_size (TYPE_PRECISION (t2), 1); 206145247Sdamien 207145247Sdamien code1 = TREE_CODE (t1); 208145247Sdamien code2 = TREE_CODE (t2); 209145247Sdamien 210145247Sdamien /* If one type is complex, form the common type of the non-complex 211145247Sdamien components, then make that complex. Use T1 or T2 if it is the 212145247Sdamien required type. */ 213145247Sdamien if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) 214145247Sdamien { 215260062Smarius tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; 216145247Sdamien tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; 217145247Sdamien tree subtype = common_type (subtype1, subtype2); 218145247Sdamien 219145247Sdamien if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) 220145247Sdamien return build_type_attribute_variant (t1, attributes); 221145247Sdamien else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) 222145247Sdamien return build_type_attribute_variant (t2, attributes); 223145247Sdamien else 224145247Sdamien return build_type_attribute_variant (build_complex_type (subtype), 225145247Sdamien attributes); 226287197Sglebius } 227145247Sdamien 228145247Sdamien switch (code1) 229145247Sdamien { 230145247Sdamien case INTEGER_TYPE: 231145247Sdamien case REAL_TYPE: 232145247Sdamien /* If only one is real, use it as the result. */ 233145247Sdamien 234287197Sglebius if (code1 == REAL_TYPE && code2 != REAL_TYPE) 235156599Sdamien return build_type_attribute_variant (t1, attributes); 236172567Sthompsa 237156599Sdamien if (code2 == REAL_TYPE && code1 != REAL_TYPE) 238146498Sdamien return build_type_attribute_variant (t2, attributes); 239146498Sdamien 240145247Sdamien /* Both real or both integers; use the one with greater precision. */ 241145247Sdamien 242145247Sdamien if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) 243260062Smarius return build_type_attribute_variant (t1, attributes); 244260062Smarius else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) 245145247Sdamien return build_type_attribute_variant (t2, attributes); 246145247Sdamien 247145247Sdamien /* Same precision. Prefer longs to ints even when same size. */ 248145247Sdamien 249145247Sdamien if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node 250145247Sdamien || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) 251145247Sdamien return build_type_attribute_variant (long_unsigned_type_node, 252145247Sdamien attributes); 253260062Smarius 254260062Smarius if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node 255145247Sdamien || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) 256145247Sdamien { 257145247Sdamien /* But preserve unsignedness from the other type, 258178354Ssam since long cannot hold all the values of an unsigned int. */ 259145247Sdamien if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2)) 260145247Sdamien t1 = long_unsigned_type_node; 261145247Sdamien else 262145247Sdamien t1 = long_integer_type_node; 263178354Ssam return build_type_attribute_variant (t1, attributes); 264145247Sdamien } 265145247Sdamien 266145247Sdamien /* Likewise, prefer long double to double even if same size. */ 267145247Sdamien if (TYPE_MAIN_VARIANT (t1) == long_double_type_node 268178354Ssam || TYPE_MAIN_VARIANT (t2) == long_double_type_node) 269145247Sdamien return build_type_attribute_variant (long_double_type_node, 270147757Sdamien attributes); 271283537Sglebius 272283527Sglebius /* Otherwise prefer the unsigned one. */ 273178354Ssam 274145247Sdamien if (TREE_UNSIGNED (t1)) 275145247Sdamien return build_type_attribute_variant (t1, attributes); 276145247Sdamien else 277178957Ssam return build_type_attribute_variant (t2, attributes); 278178957Ssam 279178957Ssam case POINTER_TYPE: 280172567Sthompsa /* For two pointers, do this recursively on the target type, 281172567Sthompsa and combine the qualifiers of the two types' targets. */ 282172567Sthompsa /* This code was turned off; I don't know why. 283172567Sthompsa But ANSI C specifies doing this with the qualifiers. 284172567Sthompsa So I turned it on again. */ 285145247Sdamien { 286145247Sdamien tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)), 287145247Sdamien TYPE_MAIN_VARIANT (TREE_TYPE (t2))); 288287197Sglebius int constp 289287197Sglebius = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2)); 290145247Sdamien int volatilep 291287197Sglebius = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2)); 292287197Sglebius t1 = build_pointer_type (c_build_type_variant (target, constp, 293145247Sdamien volatilep)); 294287197Sglebius return build_type_attribute_variant (t1, attributes); 295287197Sglebius } 296145247Sdamien#if 0 297300239Savos t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2))); 298300239Savos return build_type_attribute_variant (t1, attributes); 299300239Savos#endif 300145247Sdamien 301145247Sdamien case ARRAY_TYPE: 302145247Sdamien { 303145247Sdamien tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2)); 304145247Sdamien /* Save space: see if the result is identical to one of the args. */ 305287197Sglebius if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)) 306172567Sthompsa return build_type_attribute_variant (t1, attributes); 307172567Sthompsa if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)) 308300239Savos return build_type_attribute_variant (t2, attributes); 309172567Sthompsa /* Merge the element types, and have a size if either arg has one. */ 310172567Sthompsa t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2)); 311172567Sthompsa return build_type_attribute_variant (t1, attributes); 312178354Ssam } 313178354Ssam 314178354Ssam case FUNCTION_TYPE: 315287197Sglebius /* Function types: prefer the one that specified arg types. 316287197Sglebius If both do, merge the arg types. Also merge the return types. */ 317145247Sdamien { 318192468Ssam tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2)); 319192468Ssam tree p1 = TYPE_ARG_TYPES (t1); 320192468Ssam tree p2 = TYPE_ARG_TYPES (t2); 321192468Ssam int len; 322192468Ssam tree newargs, n; 323178354Ssam int i; 324145247Sdamien 325145247Sdamien /* Save space: see if the result is identical to one of the args. */ 326145247Sdamien if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2)) 327145247Sdamien return build_type_attribute_variant (t1, attributes); 328145247Sdamien if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1)) 329145247Sdamien return build_type_attribute_variant (t2, attributes); 330145247Sdamien 331145247Sdamien /* Simple way if one arg fails to specify argument types. */ 332145247Sdamien if (TYPE_ARG_TYPES (t1) == 0) 333145247Sdamien { 334145247Sdamien t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); 335145247Sdamien return build_type_attribute_variant (t1, attributes); 336145247Sdamien } 337145247Sdamien if (TYPE_ARG_TYPES (t2) == 0) 338145247Sdamien { 339145247Sdamien t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); 340145247Sdamien return build_type_attribute_variant (t1, attributes); 341166901Spiso } 342145247Sdamien 343145247Sdamien /* If both args specify argument types, we must merge the two 344287197Sglebius lists, argument by argument. */ 345145247Sdamien 346145247Sdamien len = list_length (p1); 347145247Sdamien newargs = 0; 348145247Sdamien 349145247Sdamien for (i = 0; i < len; i++) 350145247Sdamien newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); 351178354Ssam 352178354Ssam n = newargs; 353178354Ssam 354260062Smarius for (; p1; 355178354Ssam p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) 356260062Smarius { 357260062Smarius /* A null type means arg type is not specified. 358178354Ssam Take whatever the other function type has. */ 359178354Ssam if (TREE_VALUE (p1) == 0) 360145247Sdamien { 361145247Sdamien TREE_VALUE (n) = TREE_VALUE (p2); 362145247Sdamien goto parm_done; 363145247Sdamien } 364145247Sdamien if (TREE_VALUE (p2) == 0) 365145247Sdamien { 366145247Sdamien TREE_VALUE (n) = TREE_VALUE (p1); 367287197Sglebius goto parm_done; 368145247Sdamien } 369260062Smarius 370260062Smarius /* Given wait (union {union wait *u; int *i} *) 371191912Sthompsa and wait (union wait *), 372145247Sdamien prefer union wait * as type of parm. */ 373178354Ssam if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE 374178354Ssam && TREE_VALUE (p1) != TREE_VALUE (p2)) 375178354Ssam { 376172567Sthompsa tree memb; 377287197Sglebius for (memb = TYPE_FIELDS (TREE_VALUE (p1)); 378145247Sdamien memb; memb = TREE_CHAIN (memb)) 379145247Sdamien if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2))) 380145247Sdamien { 381260062Smarius TREE_VALUE (n) = TREE_VALUE (p2); 382145247Sdamien if (pedantic) 383260062Smarius pedwarn ("function types not truly compatible in ANSI C"); 384260062Smarius goto parm_done; 385156599Sdamien } 386159487Siedowse } 387159487Siedowse if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE 388159487Siedowse && TREE_VALUE (p2) != TREE_VALUE (p1)) 389159487Siedowse { 390159487Siedowse tree memb; 391145247Sdamien for (memb = TYPE_FIELDS (TREE_VALUE (p2)); 392145247Sdamien memb; memb = TREE_CHAIN (memb)) 393145247Sdamien if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1))) 394145247Sdamien { 395145247Sdamien TREE_VALUE (n) = TREE_VALUE (p1); 396178354Ssam if (pedantic) 397228621Sbschmidt pedwarn ("function types not truly compatible in ANSI C"); 398228621Sbschmidt goto parm_done; 399228621Sbschmidt } 400228621Sbschmidt } 401178354Ssam TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2)); 402286865Sadrian parm_done: ; 403178354Ssam } 404178354Ssam 405178354Ssam t1 = build_function_type (valtype, newargs); 406178354Ssam /* ... falls through ... */ 407178354Ssam } 408178354Ssam 409178354Ssam default: 410178354Ssam return build_type_attribute_variant (t1, attributes); 411178354Ssam } 412178354Ssam 413178354Ssam} 414178354Ssam 415178354Ssam/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment 416178354Ssam or various other operations. Return 2 if they are compatible 417178354Ssam but a warning may be needed if you use them together. */ 418178354Ssam 419178354Ssamint 420178354Ssamcomptypes (type1, type2) 421178354Ssam tree type1, type2; 422178354Ssam{ 423178354Ssam register tree t1 = type1; 424178354Ssam register tree t2 = type2; 425178354Ssam int attrval, val; 426178354Ssam 427178354Ssam /* Suppress errors caused by previously reported errors. */ 428178354Ssam 429178354Ssam if (t1 == t2 || !t1 || !t2 430178354Ssam || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) 431178354Ssam return 1; 432178354Ssam 433178354Ssam /* Treat an enum type as the integer type of the same width and 434178354Ssam signedness. */ 435178354Ssam 436178354Ssam if (TREE_CODE (t1) == ENUMERAL_TYPE) 437178354Ssam t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1)); 438178354Ssam if (TREE_CODE (t2) == ENUMERAL_TYPE) 439178354Ssam t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2)); 440178354Ssam 441178354Ssam if (t1 == t2) 442178354Ssam return 1; 443178354Ssam 444178354Ssam /* Different classes of types can't be compatible. */ 445178354Ssam 446178354Ssam if (TREE_CODE (t1) != TREE_CODE (t2)) return 0; 447178354Ssam 448178354Ssam /* Qualifiers must match. */ 449178354Ssam 450178354Ssam if (TYPE_READONLY (t1) != TYPE_READONLY (t2)) 451178354Ssam return 0; 452178354Ssam if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2)) 453178354Ssam return 0; 454178354Ssam 455178354Ssam /* Allow for two different type nodes which have essentially the same 456178354Ssam definition. Note that we already checked for equality of the type 457178354Ssam qualifiers (just above). */ 458178354Ssam 459178354Ssam if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) 460287197Sglebius return 1; 461178354Ssam 462178354Ssam#ifndef COMP_TYPE_ATTRIBUTES 463287197Sglebius#define COMP_TYPE_ATTRIBUTES(t1,t2) 1 464178354Ssam#endif 465178354Ssam 466178354Ssam /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 467178354Ssam if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2))) 468178354Ssam return 0; 469287197Sglebius 470287197Sglebius /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 471178354Ssam val = 0; 472178354Ssam 473178354Ssam switch (TREE_CODE (t1)) 474178354Ssam { 475178354Ssam case POINTER_TYPE: 476178354Ssam val = (TREE_TYPE (t1) == TREE_TYPE (t2) 477178354Ssam ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2))); 478178354Ssam break; 479178354Ssam 480178354Ssam case FUNCTION_TYPE: 481178354Ssam val = function_types_compatible_p (t1, t2); 482178354Ssam break; 483178354Ssam 484145247Sdamien case ARRAY_TYPE: 485145247Sdamien { 486145247Sdamien tree d1 = TYPE_DOMAIN (t1); 487145247Sdamien tree d2 = TYPE_DOMAIN (t2); 488145247Sdamien val = 1; 489145247Sdamien 490145247Sdamien /* Target types must match incl. qualifiers. */ 491145247Sdamien if (TREE_TYPE (t1) != TREE_TYPE (t2) 492145247Sdamien && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))) 493145247Sdamien return 0; 494232874Sscottl 495232874Sscottl /* Sizes must match unless one is missing or variable. */ 496232874Sscottl if (d1 == 0 || d2 == 0 || d1 == d2 497232874Sscottl || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST 498232874Sscottl || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST 499232874Sscottl || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST 500232874Sscottl || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST) 501232874Sscottl break; 502232874Sscottl 503232874Sscottl if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1)) 504232874Sscottl == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2))) 505232874Sscottl && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1)) 506145247Sdamien == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2))) 507145247Sdamien && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1)) 508232874Sscottl == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2))) 509145247Sdamien && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1)) 510145247Sdamien == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))))) 511145247Sdamien val = 0; 512145247Sdamien break; 513145247Sdamien } 514145247Sdamien 515145247Sdamien case RECORD_TYPE: 516145247Sdamien if (maybe_objc_comptypes (t1, t2, 0) == 1) 517145247Sdamien val = 1; 518145247Sdamien break; 519145247Sdamien 520145247Sdamien default: 521145247Sdamien break; 522145247Sdamien } 523145247Sdamien return attrval == 2 && val == 1 ? 2 : val; 524145247Sdamien} 525145247Sdamien 526145247Sdamien/* Return 1 if TTL and TTR are pointers to types that are equivalent, 527145247Sdamien ignoring their qualifiers. */ 528145247Sdamien 529145247Sdamienstatic int 530145247Sdamiencomp_target_types (ttl, ttr) 531145247Sdamien tree ttl, ttr; 532145247Sdamien{ 533145247Sdamien int val; 534232874Sscottl 535145247Sdamien /* Give maybe_objc_comptypes a crack at letting these types through. */ 536145247Sdamien if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0) 537145247Sdamien return val; 538145247Sdamien 539145247Sdamien val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)), 540145247Sdamien TYPE_MAIN_VARIANT (TREE_TYPE (ttr))); 541145247Sdamien 542145247Sdamien if (val == 2 && pedantic) 543145247Sdamien pedwarn ("types are not quite compatible"); 544145247Sdamien return val; 545145247Sdamien} 546145247Sdamien 547145247Sdamien/* Subroutines of `comptypes'. */ 548145247Sdamien 549145247Sdamien/* Return 1 if two function types F1 and F2 are compatible. 550145247Sdamien If either type specifies no argument types, 551145247Sdamien the other must specify a fixed number of self-promoting arg types. 552145247Sdamien Otherwise, if one type specifies only the number of arguments, 553145247Sdamien the other must specify that number of self-promoting arg types. 554145247Sdamien Otherwise, the argument types must match. */ 555145247Sdamien 556145247Sdamienstatic int 557145247Sdamienfunction_types_compatible_p (f1, f2) 558145247Sdamien tree f1, f2; 559145247Sdamien{ 560232874Sscottl tree args1, args2; 561145247Sdamien /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 562145247Sdamien int val = 1; 563145247Sdamien int val1; 564145247Sdamien 565145247Sdamien if (!(TREE_TYPE (f1) == TREE_TYPE (f2) 566145247Sdamien || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2))))) 567145247Sdamien return 0; 568145247Sdamien 569145247Sdamien args1 = TYPE_ARG_TYPES (f1); 570145247Sdamien args2 = TYPE_ARG_TYPES (f2); 571145247Sdamien 572145247Sdamien /* An unspecified parmlist matches any specified parmlist 573145247Sdamien whose argument types don't need default promotions. */ 574145247Sdamien 575145247Sdamien if (args1 == 0) 576145247Sdamien { 577145247Sdamien if (!self_promoting_args_p (args2)) 578145247Sdamien return 0; 579145247Sdamien /* If one of these types comes from a non-prototype fn definition, 580145247Sdamien compare that with the other type's arglist. 581145247Sdamien If they don't match, ask for a warning (but no error). */ 582145247Sdamien if (TYPE_ACTUAL_ARG_TYPES (f1) 583145247Sdamien && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1))) 584145247Sdamien val = 2; 585145247Sdamien return val; 586145247Sdamien } 587145247Sdamien if (args2 == 0) 588145247Sdamien { 589232874Sscottl if (!self_promoting_args_p (args1)) 590145247Sdamien return 0; 591145247Sdamien if (TYPE_ACTUAL_ARG_TYPES (f2) 592145247Sdamien && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2))) 593145247Sdamien val = 2; 594145247Sdamien return val; 595145247Sdamien } 596145247Sdamien 597145247Sdamien /* Both types have argument lists: compare them and propagate results. */ 598145247Sdamien val1 = type_lists_compatible_p (args1, args2); 599145247Sdamien return val1 != 1 ? val1 : val; 600145247Sdamien} 601145247Sdamien 602145247Sdamien/* Check two lists of types for compatibility, 603145247Sdamien returning 0 for incompatible, 1 for compatible, 604145247Sdamien or 2 for compatible with warning. */ 605145247Sdamien 606145247Sdamienstatic int 607232874Sscottltype_lists_compatible_p (args1, args2) 608145247Sdamien tree args1, args2; 609145247Sdamien{ 610145247Sdamien /* 1 if no need for warning yet, 2 if warning cause has been seen. */ 611145247Sdamien int val = 1; 612145247Sdamien int newval = 0; 613145247Sdamien 614145247Sdamien while (1) 615145247Sdamien { 616145247Sdamien if (args1 == 0 && args2 == 0) 617145247Sdamien return val; 618145247Sdamien /* If one list is shorter than the other, 619145247Sdamien they fail to match. */ 620145247Sdamien if (args1 == 0 || args2 == 0) 621145247Sdamien return 0; 622145247Sdamien /* A null pointer instead of a type 623145247Sdamien means there is supposed to be an argument 624145247Sdamien but nothing is specified about what type it has. 625145247Sdamien So match anything that self-promotes. */ 626145247Sdamien if (TREE_VALUE (args1) == 0) 627145247Sdamien { 628145247Sdamien if (! self_promoting_type_p (TREE_VALUE (args2))) 629145247Sdamien return 0; 630232874Sscottl } 631145247Sdamien else if (TREE_VALUE (args2) == 0) 632145247Sdamien { 633145247Sdamien if (! self_promoting_type_p (TREE_VALUE (args1))) 634145247Sdamien return 0; 635145247Sdamien } 636145247Sdamien else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2)))) 637145247Sdamien { 638145247Sdamien /* Allow wait (union {union wait *u; int *i} *) 639145247Sdamien and wait (union wait *) to be compatible. */ 640145247Sdamien if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE 641145247Sdamien && (TYPE_NAME (TREE_VALUE (args1)) == 0 642145247Sdamien || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1))) 643145247Sdamien && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST 644145247Sdamien && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)), 645145247Sdamien TYPE_SIZE (TREE_VALUE (args2)))) 646145247Sdamien { 647145247Sdamien tree memb; 648145247Sdamien for (memb = TYPE_FIELDS (TREE_VALUE (args1)); 649145247Sdamien memb; memb = TREE_CHAIN (memb)) 650145247Sdamien if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2))) 651145247Sdamien break; 652145247Sdamien if (memb == 0) 653145247Sdamien return 0; 654145247Sdamien } 655145247Sdamien else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE 656145247Sdamien && (TYPE_NAME (TREE_VALUE (args2)) == 0 657145247Sdamien || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2))) 658145247Sdamien && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST 659145247Sdamien && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)), 660145247Sdamien TYPE_SIZE (TREE_VALUE (args1)))) 661145247Sdamien { 662232874Sscottl tree memb; 663147841Sdamien for (memb = TYPE_FIELDS (TREE_VALUE (args2)); 664147841Sdamien memb; memb = TREE_CHAIN (memb)) 665145247Sdamien if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1))) 666145247Sdamien break; 667145247Sdamien if (memb == 0) 668145247Sdamien return 0; 669145247Sdamien } 670145247Sdamien else 671145247Sdamien return 0; 672145247Sdamien } 673145247Sdamien 674145247Sdamien /* comptypes said ok, but record if it said to warn. */ 675243857Sglebius if (newval > val) 676145247Sdamien val = newval; 677145247Sdamien 678145247Sdamien args1 = TREE_CHAIN (args1); 679145247Sdamien args2 = TREE_CHAIN (args2); 680145247Sdamien } 681145247Sdamien} 682145247Sdamien 683145247Sdamien/* Return 1 if PARMS specifies a fixed number of parameters 684145247Sdamien and none of their types is affected by default promotions. */ 685145247Sdamien 686145247Sdamienint 687145247Sdamienself_promoting_args_p (parms) 688145247Sdamien tree parms; 689145247Sdamien{ 690145247Sdamien register tree t; 691145247Sdamien for (t = parms; t; t = TREE_CHAIN (t)) 692145247Sdamien { 693145247Sdamien register tree type = TREE_VALUE (t); 694145247Sdamien 695145247Sdamien if (TREE_CHAIN (t) == 0 && type != void_type_node) 696145247Sdamien return 0; 697145247Sdamien 698145247Sdamien if (type == 0) 699145247Sdamien return 0; 700145247Sdamien 701145247Sdamien if (TYPE_MAIN_VARIANT (type) == float_type_node) 702145247Sdamien return 0; 703145247Sdamien 704145247Sdamien if (C_PROMOTING_INTEGER_TYPE_P (type)) 705145247Sdamien return 0; 706145247Sdamien } 707145247Sdamien return 1; 708145247Sdamien} 709145247Sdamien 710145247Sdamien/* Return 1 if TYPE is not affected by default promotions. */ 711145247Sdamien 712145247Sdamienstatic int 713145247Sdamienself_promoting_type_p (type) 714145247Sdamien tree type; 715145247Sdamien{ 716145247Sdamien if (TYPE_MAIN_VARIANT (type) == float_type_node) 717145247Sdamien return 0; 718145247Sdamien 719232874Sscottl if (C_PROMOTING_INTEGER_TYPE_P (type)) 720232874Sscottl return 0; 721232874Sscottl 722232874Sscottl return 1; 723145247Sdamien} 724283528Sglebius 725283528Sglebius/* Return an unsigned type the same as TYPE in other respects. */ 726145247Sdamien 727145247Sdamientree 728145247Sdamienunsigned_type (type) 729145247Sdamien tree type; 730145247Sdamien{ 731145247Sdamien tree type1 = TYPE_MAIN_VARIANT (type); 732145247Sdamien if (type1 == signed_char_type_node || type1 == char_type_node) 733145247Sdamien return unsigned_char_type_node; 734145247Sdamien if (type1 == integer_type_node) 735145247Sdamien return unsigned_type_node; 736145247Sdamien if (type1 == short_integer_type_node) 737145247Sdamien return short_unsigned_type_node; 738145247Sdamien if (type1 == long_integer_type_node) 739145247Sdamien return long_unsigned_type_node; 740145247Sdamien if (type1 == long_long_integer_type_node) 741145247Sdamien return long_long_unsigned_type_node; 742145247Sdamien if (type1 == intDI_type_node) 743145247Sdamien return unsigned_intDI_type_node; 744145247Sdamien if (type1 == intSI_type_node) 745145247Sdamien return unsigned_intSI_type_node; 746145247Sdamien if (type1 == intHI_type_node) 747145247Sdamien return unsigned_intHI_type_node; 748145247Sdamien if (type1 == intQI_type_node) 749145247Sdamien return unsigned_intQI_type_node; 750145247Sdamien 751145247Sdamien return signed_or_unsigned_type (1, type); 752145247Sdamien} 753145247Sdamien 754145247Sdamien/* Return a signed type the same as TYPE in other respects. */ 755145247Sdamien 756145247Sdamientree 757145247Sdamiensigned_type (type) 758145247Sdamien tree type; 759145247Sdamien{ 760145247Sdamien tree type1 = TYPE_MAIN_VARIANT (type); 761145247Sdamien if (type1 == unsigned_char_type_node || type1 == char_type_node) 762145247Sdamien return signed_char_type_node; 763145247Sdamien if (type1 == unsigned_type_node) 764145247Sdamien return integer_type_node; 765145247Sdamien if (type1 == short_unsigned_type_node) 766145247Sdamien return short_integer_type_node; 767145247Sdamien if (type1 == long_unsigned_type_node) 768145247Sdamien return long_integer_type_node; 769145247Sdamien if (type1 == long_long_unsigned_type_node) 770145247Sdamien return long_long_integer_type_node; 771145247Sdamien if (type1 == unsigned_intDI_type_node) 772145247Sdamien return intDI_type_node; 773145247Sdamien if (type1 == unsigned_intSI_type_node) 774145247Sdamien return intSI_type_node; 775145247Sdamien if (type1 == unsigned_intHI_type_node) 776145247Sdamien return intHI_type_node; 777145247Sdamien if (type1 == unsigned_intQI_type_node) 778145247Sdamien return intQI_type_node; 779145247Sdamien 780145247Sdamien return signed_or_unsigned_type (0, type); 781145247Sdamien} 782145247Sdamien 783145247Sdamien/* Return a type the same as TYPE except unsigned or 784145247Sdamien signed according to UNSIGNEDP. */ 785145247Sdamien 786145247Sdamientree 787145247Sdamiensigned_or_unsigned_type (unsignedp, type) 788145247Sdamien int unsignedp; 789145247Sdamien tree type; 790145247Sdamien{ 791145247Sdamien if ((! INTEGRAL_TYPE_P (type) && ! POINTER_TYPE_P (type)) 792145247Sdamien || TREE_UNSIGNED (type) == unsignedp) 793145247Sdamien return type; 794145247Sdamien if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)) 795145247Sdamien return unsignedp ? unsigned_char_type_node : signed_char_type_node; 796145247Sdamien if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) 797145247Sdamien return unsignedp ? unsigned_type_node : integer_type_node; 798287197Sglebius if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node)) 799145247Sdamien return unsignedp ? short_unsigned_type_node : short_integer_type_node; 800233387Sbschmidt if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node)) 801145247Sdamien return unsignedp ? long_unsigned_type_node : long_integer_type_node; 802145247Sdamien if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node)) 803145247Sdamien return (unsignedp ? long_long_unsigned_type_node 804145247Sdamien : long_long_integer_type_node); 805145247Sdamien return type; 806145247Sdamien} 807145247Sdamien 808287197Sglebius/* Compute the value of the `sizeof' operator. */ 809145247Sdamien 810146498Sdamientree 811146498Sdamienc_sizeof (type) 812233387Sbschmidt tree type; 813145247Sdamien{ 814145247Sdamien enum tree_code code = TREE_CODE (type); 815145247Sdamien tree t; 816145247Sdamien 817172567Sthompsa if (code == FUNCTION_TYPE) 818172567Sthompsa { 819172567Sthompsa if (pedantic || warn_pointer_arith) 820172567Sthompsa pedwarn ("sizeof applied to a function type"); 821172567Sthompsa return size_int (1); 822172567Sthompsa } 823172567Sthompsa if (code == VOID_TYPE) 824172567Sthompsa { 825145247Sdamien if (pedantic || warn_pointer_arith) 826145247Sdamien pedwarn ("sizeof applied to a void type"); 827145247Sdamien return size_int (1); 828145247Sdamien } 829156599Sdamien if (code == ERROR_MARK) 830156599Sdamien return size_int (1); 831145247Sdamien if (TYPE_SIZE (type) == 0) 832145247Sdamien { 833145247Sdamien error ("sizeof applied to an incomplete type"); 834145247Sdamien return size_int (0); 835178354Ssam } 836178354Ssam 837286865Sadrian /* Convert in case a char is more than one unit. */ 838145247Sdamien t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), 839145247Sdamien size_int (TYPE_PRECISION (char_type_node))); 840178354Ssam t = convert (sizetype, t); 841178354Ssam /* size_binop does not put the constant in range, so do it now. */ 842178354Ssam if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0)) 843145247Sdamien TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1; 844145247Sdamien return t; 845145247Sdamien} 846178354Ssam 847145247Sdamientree 848178354Ssamc_sizeof_nowarn (type) 849178354Ssam tree type; 850286865Sadrian{ 851191746Sthompsa enum tree_code code = TREE_CODE (type); 852145247Sdamien tree t; 853172567Sthompsa 854178354Ssam if (code == FUNCTION_TYPE 855172567Sthompsa || code == VOID_TYPE 856172567Sthompsa || code == ERROR_MARK) 857191746Sthompsa return size_int (1); 858191746Sthompsa if (TYPE_SIZE (type) == 0) 859191746Sthompsa return size_int (0); 860145247Sdamien 861145247Sdamien /* Convert in case a char is more than one unit. */ 862172567Sthompsa t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), 863172567Sthompsa size_int (TYPE_PRECISION (char_type_node))); 864172567Sthompsa t = convert (sizetype, t); 865172567Sthompsa force_fit_type (t, 0); 866206763Sbschmidt return t; 867206763Sbschmidt} 868206763Sbschmidt 869206763Sbschmidt/* Compute the size to increment a pointer by. */ 870172567Sthompsa 871191746Sthompsatree 872191746Sthompsac_size_in_bytes (type) 873172567Sthompsa tree type; 874172567Sthompsa{ 875145247Sdamien enum tree_code code = TREE_CODE (type); 876172567Sthompsa tree t; 877172567Sthompsa 878191746Sthompsa if (code == FUNCTION_TYPE) 879172567Sthompsa return size_int (1); 880145247Sdamien if (code == VOID_TYPE) 881172567Sthompsa return size_int (1); 882206765Sbschmidt if (code == ERROR_MARK) 883206765Sbschmidt return size_int (1); 884206765Sbschmidt if (TYPE_SIZE (type) == 0) 885206765Sbschmidt { 886206765Sbschmidt error ("arithmetic on pointer to an incomplete type"); 887206765Sbschmidt return size_int (1); 888206765Sbschmidt } 889191746Sthompsa 890191746Sthompsa /* Convert in case a char is more than one unit. */ 891145247Sdamien t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), 892172567Sthompsa size_int (BITS_PER_UNIT)); 893172567Sthompsa t = convert (sizetype, t); 894206763Sbschmidt force_fit_type (t, 0); 895172567Sthompsa return t; 896172567Sthompsa} 897191746Sthompsa 898191746Sthompsa/* Implement the __alignof keyword: Return the minimum required 899145247Sdamien alignment of TYPE, measured in bytes. */ 900145247Sdamien 901172058Ssamtree 902145247Sdamienc_alignof (type) 903145247Sdamien tree type; 904191746Sthompsa{ 905178354Ssam enum tree_code code = TREE_CODE (type); 906145247Sdamien 907145247Sdamien if (code == FUNCTION_TYPE) 908145247Sdamien return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); 909145247Sdamien 910145247Sdamien if (code == VOID_TYPE || code == ERROR_MARK) 911145247Sdamien return size_int (1); 912145247Sdamien 913145247Sdamien return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT); 914145247Sdamien} 915145247Sdamien 916145247Sdamien/* Implement the __alignof keyword: Return the minimum required 917145247Sdamien alignment of EXPR, measured in bytes. For VAR_DECL's and 918145247Sdamien FIELD_DECL's return DECL_ALIGN (which can be set from an 919145247Sdamien "aligned" __attribute__ specification). */ 920145247Sdamien 921145247Sdamientree 922145247Sdamienc_alignof_expr (expr) 923145247Sdamien tree expr; 924145247Sdamien{ 925145247Sdamien if (TREE_CODE (expr) == VAR_DECL) 926145247Sdamien return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT); 927145247Sdamien 928145247Sdamien if (TREE_CODE (expr) == COMPONENT_REF 929145247Sdamien && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) 930145247Sdamien { 931145247Sdamien error ("`__alignof' applied to a bit-field"); 932145247Sdamien return size_int (1); 933145247Sdamien } 934145247Sdamien else if (TREE_CODE (expr) == COMPONENT_REF 935145247Sdamien && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) 936145247Sdamien return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT); 937145247Sdamien 938145247Sdamien if (TREE_CODE (expr) == INDIRECT_REF) 939145247Sdamien { 940145247Sdamien tree t = TREE_OPERAND (expr, 0); 941145247Sdamien tree best = t; 942145247Sdamien int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); 943145247Sdamien 944145247Sdamien while (TREE_CODE (t) == NOP_EXPR 945145247Sdamien && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) 946145247Sdamien { 947145247Sdamien int thisalign; 948145247Sdamien 949145247Sdamien t = TREE_OPERAND (t, 0); 950145247Sdamien thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); 951145247Sdamien if (thisalign > bestalign) 952145247Sdamien best = t, bestalign = thisalign; 953145247Sdamien } 954145247Sdamien return c_alignof (TREE_TYPE (TREE_TYPE (best))); 955145247Sdamien } 956145247Sdamien else 957145247Sdamien return c_alignof (TREE_TYPE (expr)); 958145247Sdamien} 959145247Sdamien 960145247Sdamien/* Return either DECL or its known constant value (if it has one). */ 961145247Sdamien 962145247Sdamienstatic tree 963300239Savosdecl_constant_value (decl) 964300239Savos tree decl; 965300239Savos{ 966300239Savos if (/* Don't change a variable array bound or initial value to a constant 967300239Savos in a place where a variable is invalid. */ 968300239Savos current_function_decl != 0 969300239Savos && ! pedantic 970300239Savos && ! TREE_THIS_VOLATILE (decl) 971300239Savos && TREE_READONLY (decl) && ! ITERATOR_P (decl) 972300239Savos && DECL_INITIAL (decl) != 0 973300239Savos && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK 974300239Savos /* This is invalid if initial value is not constant. 975300239Savos If it has either a function call, a memory reference, 976145247Sdamien or a variable, then re-evaluating it could give different results. */ 977172567Sthompsa && TREE_CONSTANT (DECL_INITIAL (decl)) 978145247Sdamien /* Check for cases where this is sub-optimal, even though valid. */ 979145247Sdamien && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR 980145247Sdamien && DECL_MODE (decl) != BLKmode) 981145247Sdamien return DECL_INITIAL (decl); 982145247Sdamien return decl; 983145247Sdamien} 984145247Sdamien 985172567Sthompsa/* Perform default promotions for C data used in expressions. 986172567Sthompsa Arrays and functions are converted to pointers; 987145247Sdamien enumeral types or short or char, to int. 988145247Sdamien In addition, manifest constants symbols are replaced by their values. */ 989145247Sdamien 990172567Sthompsatree 991145247Sdamiendefault_conversion (exp) 992145247Sdamien tree exp; 993145247Sdamien{ 994145247Sdamien register tree type = TREE_TYPE (exp); 995172567Sthompsa register enum tree_code code = TREE_CODE (type); 996145247Sdamien 997178354Ssam /* Constants can be used directly unless they're not loadable. */ 998287197Sglebius if (TREE_CODE (exp) == CONST_DECL) 999178354Ssam exp = DECL_INITIAL (exp); 1000145247Sdamien 1001145247Sdamien /* Replace a nonvolatile const static variable with its value unless 1002145247Sdamien it is an array, in which case we must be sure that taking the 1003145247Sdamien address of the array produces consistent results. */ 1004145247Sdamien else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE) 1005145247Sdamien { 1006145247Sdamien exp = decl_constant_value (exp); 1007145247Sdamien type = TREE_TYPE (exp); 1008172567Sthompsa } 1009178354Ssam 1010178354Ssam /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as 1011178354Ssam an lvalue. */ 1012178354Ssam /* Do not use STRIP_NOPS here! It will remove conversions from pointer 1013178354Ssam to integer and cause infinite recursion. */ 1014178354Ssam while (TREE_CODE (exp) == NON_LVALUE_EXPR 1015178354Ssam || (TREE_CODE (exp) == NOP_EXPR 1016178354Ssam && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp))) 1017172567Sthompsa exp = TREE_OPERAND (exp, 0); 1018145247Sdamien 1019145247Sdamien /* Normally convert enums to int, 1020145247Sdamien but convert wide enums to something wider. */ 1021172567Sthompsa if (code == ENUMERAL_TYPE) 1022178354Ssam { 1023172567Sthompsa type = type_for_size (MAX (TYPE_PRECISION (type), 1024172567Sthompsa TYPE_PRECISION (integer_type_node)), 1025172567Sthompsa ((flag_traditional 1026172567Sthompsa || (TYPE_PRECISION (type) 1027172567Sthompsa >= TYPE_PRECISION (integer_type_node))) 1028172567Sthompsa && TREE_UNSIGNED (type))); 1029178354Ssam return convert (type, exp); 1030206767Sbschmidt } 1031178354Ssam 1032191746Sthompsa if (TREE_CODE (exp) == COMPONENT_REF 1033206767Sbschmidt && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))) 1034178354Ssam { 1035145247Sdamien tree width = DECL_SIZE (TREE_OPERAND (exp, 1)); 1036145247Sdamien HOST_WIDE_INT low = TREE_INT_CST_LOW (width); 1037145247Sdamien 1038172567Sthompsa /* If it's thinner than an int, promote it like a 1039172567Sthompsa C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */ 1040172567Sthompsa 1041172567Sthompsa if (low < TYPE_PRECISION (integer_type_node)) 1042172567Sthompsa { 1043172567Sthompsa if (flag_traditional && TREE_UNSIGNED (type)) 1044172567Sthompsa return convert (unsigned_type_node, exp); 1045178354Ssam else 1046178354Ssam return convert (integer_type_node, exp); 1047172567Sthompsa } 1048172567Sthompsa } 1049172567Sthompsa 1050172567Sthompsa if (C_PROMOTING_INTEGER_TYPE_P (type)) 1051172567Sthompsa { 1052206767Sbschmidt /* Traditionally, unsignedness is preserved in default promotions. 1053191746Sthompsa Also preserve unsignedness if not really getting any wider. */ 1054206767Sbschmidt if (TREE_UNSIGNED (type) 1055172567Sthompsa && (flag_traditional 1056172567Sthompsa || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) 1057172567Sthompsa return convert (unsigned_type_node, exp); 1058145247Sdamien return convert (integer_type_node, exp); 1059145247Sdamien } 1060145247Sdamien if (flag_traditional && !flag_allow_single_precision 1061172567Sthompsa && TYPE_MAIN_VARIANT (type) == float_type_node) 1062178354Ssam return convert (double_type_node, exp); 1063178354Ssam if (code == VOID_TYPE) 1064206767Sbschmidt { 1065206767Sbschmidt error ("void value not ignored as it ought to be"); 1066191746Sthompsa return error_mark_node; 1067206767Sbschmidt } 1068206767Sbschmidt if (code == FUNCTION_TYPE) 1069145247Sdamien { 1070145247Sdamien return build_unary_op (ADDR_EXPR, exp, 0); 1071172567Sthompsa } 1072178354Ssam if (code == ARRAY_TYPE) 1073206763Sbschmidt { 1074178354Ssam register tree adr; 1075172567Sthompsa tree restype = TREE_TYPE (type); 1076178354Ssam tree ptrtype; 1077172567Sthompsa int constp = 0; 1078172567Sthompsa int volatilep = 0; 1079145247Sdamien 1080178354Ssam if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' 1081178354Ssam || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd') 1082172567Sthompsa { 1083178354Ssam constp = TREE_READONLY (exp); 1084145247Sdamien volatilep = TREE_THIS_VOLATILE (exp); 1085172567Sthompsa } 1086172567Sthompsa 1087172567Sthompsa if (TYPE_READONLY (type) || TYPE_VOLATILE (type) 1088178354Ssam || constp || volatilep) 1089172567Sthompsa restype = c_build_type_variant (restype, 1090145247Sdamien TYPE_READONLY (type) || constp, 1091172567Sthompsa TYPE_VOLATILE (type) || volatilep); 1092145247Sdamien 1093145247Sdamien if (TREE_CODE (exp) == INDIRECT_REF) 1094145247Sdamien return convert (TYPE_POINTER_TO (restype), 1095172567Sthompsa TREE_OPERAND (exp, 0)); 1096172567Sthompsa 1097172567Sthompsa if (TREE_CODE (exp) == COMPOUND_EXPR) 1098172567Sthompsa { 1099172567Sthompsa tree op1 = default_conversion (TREE_OPERAND (exp, 1)); 1100287197Sglebius return build (COMPOUND_EXPR, TREE_TYPE (op1), 1101172567Sthompsa TREE_OPERAND (exp, 0), op1); 1102172567Sthompsa } 1103192468Ssam 1104172567Sthompsa if (! lvalue_p (exp) 1105172567Sthompsa && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp))) 1106172567Sthompsa { 1107145247Sdamien error ("invalid use of non-lvalue array"); 1108145247Sdamien return error_mark_node; 1109145247Sdamien } 1110145247Sdamien 1111172567Sthompsa ptrtype = build_pointer_type (restype); 1112145247Sdamien 1113287197Sglebius if (TREE_CODE (exp) == VAR_DECL) 1114172567Sthompsa { 1115145247Sdamien /* ??? This is not really quite correct 1116145247Sdamien in that the type of the operand of ADDR_EXPR 1117145247Sdamien is not the target type of the type of the ADDR_EXPR itself. 1118145247Sdamien Question is, can this lossage be avoided? */ 1119145247Sdamien adr = build1 (ADDR_EXPR, ptrtype, exp); 1120145247Sdamien if (mark_addressable (exp) == 0) 1121145247Sdamien return error_mark_node; 1122145247Sdamien TREE_CONSTANT (adr) = staticp (exp); 1123145247Sdamien TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */ 1124145247Sdamien return adr; 1125145247Sdamien } 1126145247Sdamien /* This way is better for a COMPONENT_REF since it can 1127145247Sdamien simplify the offset for a component. */ 1128145247Sdamien adr = build_unary_op (ADDR_EXPR, exp, 1); 1129145247Sdamien return convert (ptrtype, adr); 1130178354Ssam } 1131145247Sdamien return exp; 1132145247Sdamien} 1133145247Sdamien 1134145247Sdamien/* Look up component name in the structure type definition. 1135145247Sdamien 1136145247Sdamien If this component name is found indirectly within an anonymous union, 1137145247Sdamien store in *INDIRECT the component which directly contains 1138145247Sdamien that anonymous union. Otherwise, set *INDIRECT to 0. */ 1139145247Sdamien 1140172567Sthompsastatic tree 1141172567Sthompsalookup_field (type, component, indirect) 1142172567Sthompsa tree type, component; 1143170530Ssam tree *indirect; 1144172567Sthompsa{ 1145172567Sthompsa tree field; 1146172567Sthompsa 1147172567Sthompsa /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers 1148172567Sthompsa to the field elements. Use a binary search on this array to quickly 1149145247Sdamien find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC 1150145247Sdamien will always be set for structures which have many elements. */ 1151145247Sdamien 1152145247Sdamien if (TYPE_LANG_SPECIFIC (type)) 1153145247Sdamien { 1154145247Sdamien int bot, top, half; 1155172567Sthompsa tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0]; 1156145247Sdamien 1157145247Sdamien field = TYPE_FIELDS (type); 1158287197Sglebius bot = 0; 1159152385Sdamien top = TYPE_LANG_SPECIFIC (type)->len; 1160145247Sdamien while (top - bot > 1) 1161145247Sdamien { 1162145247Sdamien half = (top - bot + 1) >> 1; 1163192468Ssam field = field_array[bot+half]; 1164145247Sdamien 1165152385Sdamien if (DECL_NAME (field) == NULL_TREE) 1166152385Sdamien { 1167152385Sdamien /* Step through all anon unions in linear fashion. */ 1168147757Sdamien while (DECL_NAME (field_array[bot]) == NULL_TREE) 1169147757Sdamien { 1170147757Sdamien tree anon = 0, junk; 1171147757Sdamien 1172152385Sdamien field = field_array[bot++]; 1173152385Sdamien if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE 1174152385Sdamien || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) 1175152385Sdamien anon = lookup_field (TREE_TYPE (field), component, &junk); 1176152385Sdamien 1177152385Sdamien if (anon != NULL_TREE) 1178243857Sglebius { 1179152385Sdamien *indirect = field; 1180287197Sglebius return anon; 1181152385Sdamien } 1182152385Sdamien } 1183152385Sdamien 1184145247Sdamien /* Entire record is only anon unions. */ 1185145247Sdamien if (bot > top) 1186145247Sdamien return NULL_TREE; 1187152385Sdamien 1188152385Sdamien /* Restart the binary search, with new lower bound. */ 1189152385Sdamien continue; 1190152385Sdamien } 1191152385Sdamien 1192152385Sdamien if (DECL_NAME (field) == component) 1193152385Sdamien break; 1194152385Sdamien if (DECL_NAME (field) < component) 1195152385Sdamien bot += half; 1196152385Sdamien else 1197152385Sdamien top = bot + half; 1198152385Sdamien } 1199152385Sdamien 1200152385Sdamien if (DECL_NAME (field_array[bot]) == component) 1201287197Sglebius field = field_array[bot]; 1202152385Sdamien else if (DECL_NAME (field) != component) 1203152385Sdamien field = 0; 1204152385Sdamien } 1205152385Sdamien else 1206152385Sdamien { 1207152385Sdamien for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 1208152385Sdamien { 1209152385Sdamien if (DECL_NAME (field) == NULL_TREE) 1210152385Sdamien { 1211152385Sdamien tree junk; 1212145247Sdamien tree anon = 0; 1213145247Sdamien 1214192468Ssam if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE 1215192468Ssam || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) 1216192468Ssam anon = lookup_field (TREE_TYPE (field), component, &junk); 1217145247Sdamien 1218145247Sdamien if (anon != NULL_TREE) 1219145247Sdamien { 1220192468Ssam *indirect = field; 1221192468Ssam return anon; 1222145247Sdamien } 1223145247Sdamien } 1224172567Sthompsa 1225172567Sthompsa if (DECL_NAME (field) == component) 1226145247Sdamien break; 1227172567Sthompsa } 1228178354Ssam } 1229178354Ssam 1230206764Sbschmidt *indirect = NULL_TREE; 1231178354Ssam return field; 1232178354Ssam} 1233206764Sbschmidt 1234172567Sthompsa/* Make an expression to refer to the COMPONENT field of 1235145247Sdamien structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */ 1236145247Sdamien 1237145247Sdamientree 1238145247Sdamienbuild_component_ref (datum, component) 1239145247Sdamien tree datum, component; 1240145247Sdamien{ 1241145247Sdamien register tree type = TREE_TYPE (datum); 1242145247Sdamien register enum tree_code code = TREE_CODE (type); 1243145247Sdamien register tree field = NULL; 1244145247Sdamien register tree ref; 1245145247Sdamien 1246145247Sdamien /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it 1247145247Sdamien unless we are not to support things not strictly ANSI. */ 1248145247Sdamien switch (TREE_CODE (datum)) 1249145247Sdamien { 1250145247Sdamien case COMPOUND_EXPR: 1251145247Sdamien { 1252145247Sdamien tree value = build_component_ref (TREE_OPERAND (datum, 1), component); 1253145247Sdamien return build (COMPOUND_EXPR, TREE_TYPE (value), 1254145247Sdamien TREE_OPERAND (datum, 0), value); 1255145247Sdamien } 1256145247Sdamien case COND_EXPR: 1257145247Sdamien return build_conditional_expr 1258145247Sdamien (TREE_OPERAND (datum, 0), 1259145247Sdamien build_component_ref (TREE_OPERAND (datum, 1), component), 1260145247Sdamien build_component_ref (TREE_OPERAND (datum, 2), component)); 1261172567Sthompsa 1262145247Sdamien default: 1263145247Sdamien break; 1264145247Sdamien } 1265172567Sthompsa 1266145247Sdamien /* See if there is a field or component with name COMPONENT. */ 1267145247Sdamien 1268145247Sdamien if (code == RECORD_TYPE || code == UNION_TYPE) 1269145247Sdamien { 1270172567Sthompsa tree indirect = 0; 1271145247Sdamien 1272145247Sdamien if (TYPE_SIZE (type) == 0) 1273145247Sdamien { 1274172567Sthompsa incomplete_type_error (NULL_TREE, type); 1275172567Sthompsa return error_mark_node; 1276178354Ssam } 1277145247Sdamien 1278145247Sdamien field = lookup_field (type, component, &indirect); 1279145247Sdamien 1280172567Sthompsa if (!field) 1281145247Sdamien { 1282145247Sdamien error (code == RECORD_TYPE 1283145247Sdamien ? "structure has no member named `%s'" 1284145247Sdamien : "union has no member named `%s'", 1285145247Sdamien IDENTIFIER_POINTER (component)); 1286145247Sdamien return error_mark_node; 1287145247Sdamien } 1288145247Sdamien if (TREE_TYPE (field) == error_mark_node) 1289145247Sdamien return error_mark_node; 1290145247Sdamien 1291145247Sdamien /* If FIELD was found buried within an anonymous union, 1292145247Sdamien make one COMPONENT_REF to get that anonymous union, 1293145247Sdamien then fall thru to make a second COMPONENT_REF to get FIELD. */ 1294145247Sdamien if (indirect != 0) 1295145247Sdamien { 1296145247Sdamien ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect); 1297145247Sdamien if (TREE_READONLY (datum) || TREE_READONLY (indirect)) 1298145247Sdamien TREE_READONLY (ref) = 1; 1299145247Sdamien if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect)) 1300145247Sdamien TREE_THIS_VOLATILE (ref) = 1; 1301145247Sdamien datum = ref; 1302145247Sdamien } 1303145247Sdamien 1304145247Sdamien ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field); 1305145247Sdamien 1306145247Sdamien if (TREE_READONLY (datum) || TREE_READONLY (field)) 1307145247Sdamien TREE_READONLY (ref) = 1; 1308145247Sdamien if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field)) 1309145247Sdamien TREE_THIS_VOLATILE (ref) = 1; 1310145247Sdamien 1311145247Sdamien return ref; 1312145247Sdamien } 1313145247Sdamien else if (code != ERROR_MARK) 1314145247Sdamien error ("request for member `%s' in something not a structure or union", 1315145247Sdamien IDENTIFIER_POINTER (component)); 1316145247Sdamien 1317145247Sdamien return error_mark_node; 1318145247Sdamien} 1319145247Sdamien 1320145247Sdamien/* Given an expression PTR for a pointer, return an expression 1321145247Sdamien for the value pointed to. 1322145247Sdamien ERRORSTRING is the name of the operator to appear in error messages. */ 1323145247Sdamien 1324145247Sdamientree 1325343907Savosbuild_indirect_ref (ptr, errorstring) 1326145247Sdamien tree ptr; 1327145247Sdamien char *errorstring; 1328145247Sdamien{ 1329145247Sdamien register tree pointer = default_conversion (ptr); 1330145247Sdamien register tree type = TREE_TYPE (pointer); 1331145247Sdamien 1332145247Sdamien if (TREE_CODE (type) == POINTER_TYPE) 1333145247Sdamien { 1334145247Sdamien if (TREE_CODE (pointer) == ADDR_EXPR 1335145247Sdamien && !flag_volatile 1336145247Sdamien && (TREE_TYPE (TREE_OPERAND (pointer, 0)) 1337145247Sdamien == TREE_TYPE (type))) 1338145247Sdamien return TREE_OPERAND (pointer, 0); 1339145247Sdamien else 1340145247Sdamien { 1341145247Sdamien tree t = TREE_TYPE (type); 1342145247Sdamien register tree ref = build1 (INDIRECT_REF, 1343145247Sdamien TYPE_MAIN_VARIANT (t), pointer); 1344145247Sdamien 1345145247Sdamien if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE) 1346145247Sdamien { 1347145247Sdamien error ("dereferencing pointer to incomplete type"); 1348145247Sdamien return error_mark_node; 1349145247Sdamien } 1350145247Sdamien if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0) 1351145247Sdamien warning ("dereferencing `void *' pointer"); 1352145247Sdamien 1353145247Sdamien /* We *must* set TREE_READONLY when dereferencing a pointer to const, 1354287197Sglebius so that we get the proper error message if the result is used 1355145247Sdamien to assign to. Also, &* is supposed to be a no-op. 1356145247Sdamien And ANSI C seems to specify that the type of the result 1357145247Sdamien should be the const type. */ 1358191746Sthompsa /* A de-reference of a pointer to const is not a const. It is valid 1359191746Sthompsa to change it via some other pointer. */ 1360287197Sglebius TREE_READONLY (ref) = TYPE_READONLY (t); 1361191956Sthompsa TREE_SIDE_EFFECTS (ref) 1362191746Sthompsa = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile; 1363191746Sthompsa TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); 1364206767Sbschmidt return ref; 1365206767Sbschmidt } 1366191956Sthompsa } 1367206767Sbschmidt else if (TREE_CODE (pointer) != ERROR_MARK) 1368206767Sbschmidt error ("invalid type argument of `%s'", errorstring); 1369191746Sthompsa return error_mark_node; 1370191746Sthompsa} 1371191746Sthompsa 1372191746Sthompsa/* This handles expressions of the form "a[i]", which denotes 1373145247Sdamien an array reference. 1374145247Sdamien 1375145247Sdamien This is logically equivalent in C to *(a+i), but we may do it differently. 1376145247Sdamien If A is a variable or a member, we generate a primitive ARRAY_REF. 1377145247Sdamien This avoids forcing the array out of registers, and can work on 1378172567Sthompsa arrays that are not lvalues (for example, members of structures returned 1379145247Sdamien by functions). */ 1380191746Sthompsa 1381191746Sthompsatree 1382191746Sthompsabuild_array_ref (array, index) 1383145247Sdamien tree array, index; 1384145247Sdamien{ 1385145247Sdamien if (index == 0) 1386145247Sdamien { 1387156599Sdamien error ("subscript missing in array reference"); 1388156599Sdamien return error_mark_node; 1389156599Sdamien } 1390145247Sdamien 1391191746Sthompsa if (TREE_TYPE (array) == error_mark_node 1392191746Sthompsa || TREE_TYPE (index) == error_mark_node) 1393145247Sdamien return error_mark_node; 1394145247Sdamien 1395156599Sdamien if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE 1396156599Sdamien && TREE_CODE (array) != INDIRECT_REF) 1397145247Sdamien { 1398145247Sdamien tree rval, type; 1399145247Sdamien 1400145247Sdamien /* Subscripting with type char is likely to lose 1401145247Sdamien on a machine where chars are signed. 1402145247Sdamien So warn on any machine, but optionally. 1403145247Sdamien Don't warn for unsigned char since that type is safe. 1404145247Sdamien Don't warn for signed char because anyone who uses that 1405145247Sdamien must have done so deliberately. */ 1406191746Sthompsa if (warn_char_subscripts 1407172567Sthompsa && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) 1408145247Sdamien warning ("array subscript has type `char'"); 1409145247Sdamien 1410145247Sdamien /* Apply default promotions *after* noticing character types. */ 1411145247Sdamien index = default_conversion (index); 1412145247Sdamien 1413145247Sdamien /* Require integer *after* promotion, for sake of enums. */ 1414145247Sdamien if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE) 1415145247Sdamien { 1416145247Sdamien error ("array subscript is not an integer"); 1417145247Sdamien return error_mark_node; 1418145247Sdamien } 1419145247Sdamien 1420145247Sdamien /* An array that is indexed by a non-constant 1421172567Sthompsa cannot be stored in a register; we must be able to do 1422172567Sthompsa address arithmetic on its address. 1423172567Sthompsa Likewise an array of elements of variable size. */ 1424172567Sthompsa if (TREE_CODE (index) != INTEGER_CST 1425172567Sthompsa || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0 1426172567Sthompsa && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) 1427172567Sthompsa { 1428172567Sthompsa if (mark_addressable (array) == 0) 1429172567Sthompsa return error_mark_node; 1430172567Sthompsa } 1431172567Sthompsa /* An array that is indexed by a constant value which is not within 1432172567Sthompsa the array bounds cannot be stored in a register either; because we 1433172567Sthompsa would get a crash in store_bit_field/extract_bit_field when trying 1434172567Sthompsa to access a non-existent part of the register. */ 1435172567Sthompsa if (TREE_CODE (index) == INTEGER_CST 1436172567Sthompsa && TYPE_VALUES (TREE_TYPE (array)) 1437172567Sthompsa && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array)))) 1438172567Sthompsa { 1439172567Sthompsa if (mark_addressable (array) == 0) 1440172567Sthompsa return error_mark_node; 1441172567Sthompsa } 1442172567Sthompsa 1443172567Sthompsa if (pedantic && !lvalue_p (array)) 1444172567Sthompsa { 1445172567Sthompsa if (DECL_REGISTER (array)) 1446172567Sthompsa pedwarn ("ANSI C forbids subscripting `register' array"); 1447172567Sthompsa else 1448172567Sthompsa pedwarn ("ANSI C forbids subscripting non-lvalue array"); 1449172567Sthompsa } 1450172567Sthompsa 1451172567Sthompsa if (pedantic) 1452172567Sthompsa { 1453172567Sthompsa tree foo = array; 1454172567Sthompsa while (TREE_CODE (foo) == COMPONENT_REF) 1455172567Sthompsa foo = TREE_OPERAND (foo, 0); 1456172567Sthompsa if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)) 1457172567Sthompsa pedwarn ("ANSI C forbids subscripting non-lvalue array"); 1458172567Sthompsa } 1459172567Sthompsa 1460288087Sadrian type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array))); 1461172567Sthompsa rval = build (ARRAY_REF, type, array, index); 1462172567Sthompsa /* Array ref is const/volatile if the array elements are 1463172567Sthompsa or if the array is. */ 1464172567Sthompsa TREE_READONLY (rval) 1465172567Sthompsa |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) 1466172567Sthompsa | TREE_READONLY (array)); 1467145247Sdamien TREE_SIDE_EFFECTS (rval) 1468145247Sdamien |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) 1469145247Sdamien | TREE_SIDE_EFFECTS (array)); 1470145247Sdamien TREE_THIS_VOLATILE (rval) 1471145247Sdamien |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) 1472145247Sdamien /* This was added by rms on 16 Nov 91. 1473145247Sdamien It fixes vol struct foo *a; a->elts[1] 1474145247Sdamien in an inline function. 1475145247Sdamien Hope it doesn't break something else. */ 1476145247Sdamien | TREE_THIS_VOLATILE (array)); 1477178354Ssam return require_complete_type (fold (rval)); 1478178354Ssam } 1479172567Sthompsa 1480172567Sthompsa { 1481172567Sthompsa tree ar = default_conversion (array); 1482172567Sthompsa tree ind = default_conversion (index); 1483172567Sthompsa 1484172567Sthompsa /* Do the same warning check as above, but only on the part that's 1485172567Sthompsa syntactically the index and only if it is also semantically 1486145247Sdamien the index. */ 1487145247Sdamien if (warn_char_subscripts 1488145247Sdamien && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE 1489145247Sdamien && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node) 1490145247Sdamien warning ("subscript has type `char'"); 1491145247Sdamien 1492172567Sthompsa /* Put the integer in IND to simplify error checking. */ 1493145247Sdamien if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE) 1494145247Sdamien { 1495145247Sdamien tree temp = ar; 1496145247Sdamien ar = ind; 1497145247Sdamien ind = temp; 1498145247Sdamien } 1499145247Sdamien 1500152637Sdamien if (ar == error_mark_node) 1501145247Sdamien return ar; 1502145247Sdamien 1503145247Sdamien if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE 1504145247Sdamien || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE) 1505145247Sdamien { 1506145247Sdamien error ("subscripted value is neither array nor pointer"); 1507145247Sdamien return error_mark_node; 1508145247Sdamien } 1509145247Sdamien if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE) 1510145247Sdamien { 1511145247Sdamien error ("array subscript is not an integer"); 1512172567Sthompsa return error_mark_node; 1513172567Sthompsa } 1514172567Sthompsa 1515172567Sthompsa return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0), 1516172567Sthompsa "array indexing"); 1517172567Sthompsa } 1518172567Sthompsa} 1519172567Sthompsa 1520174269Swkoszek/* Build a function call to function FUNCTION with parameters PARAMS. 1521172567Sthompsa PARAMS is a list--a chain of TREE_LIST nodes--in which the 1522172567Sthompsa TREE_VALUE of each node is a parameter-expression. 1523172567Sthompsa FUNCTION's data type may be a function type or a pointer-to-function. */ 1524172567Sthompsa 1525145247Sdamientree 1526145247Sdamienbuild_function_call (function, params) 1527145247Sdamien tree function, params; 1528145247Sdamien{ 1529145247Sdamien register tree fntype, fundecl = 0; 1530145247Sdamien register tree coerced_params; 1531145247Sdamien tree name = NULL_TREE, assembler_name = NULL_TREE; 1532172567Sthompsa 1533172567Sthompsa /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 1534172567Sthompsa STRIP_TYPE_NOPS (function); 1535172567Sthompsa 1536172567Sthompsa /* Convert anything with function type to a pointer-to-function. */ 1537172567Sthompsa if (TREE_CODE (function) == FUNCTION_DECL) 1538172567Sthompsa { 1539172567Sthompsa name = DECL_NAME (function); 1540145247Sdamien assembler_name = DECL_ASSEMBLER_NAME (function); 1541145247Sdamien 1542145247Sdamien /* Differs from default_conversion by not setting TREE_ADDRESSABLE 1543287197Sglebius (because calling an inline function does not mean the function 1544145247Sdamien needs to be separately compiled). */ 1545287197Sglebius fntype = build_type_variant (TREE_TYPE (function), 1546192468Ssam TREE_READONLY (function), 1547145247Sdamien TREE_THIS_VOLATILE (function)); 1548145247Sdamien fundecl = function; 1549145247Sdamien function = build1 (ADDR_EXPR, build_pointer_type (fntype), function); 1550145247Sdamien } 1551145247Sdamien else 1552145247Sdamien function = default_conversion (function); 1553145247Sdamien 1554145247Sdamien fntype = TREE_TYPE (function); 1555145247Sdamien 1556145247Sdamien if (TREE_CODE (fntype) == ERROR_MARK) 1557145247Sdamien return error_mark_node; 1558145247Sdamien 1559260444Skevlo if (!(TREE_CODE (fntype) == POINTER_TYPE 1560178354Ssam && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) 1561147806Ssam { 1562147806Ssam error ("called object is not a function"); 1563145247Sdamien return error_mark_node; 1564147806Ssam } 1565145247Sdamien 1566145247Sdamien /* fntype now gets the type of function pointed to. */ 1567145247Sdamien fntype = TREE_TYPE (fntype); 1568145247Sdamien 1569192468Ssam /* Convert the parameters to the types declared in the 1570145247Sdamien function prototype, or apply default promotions. */ 1571145247Sdamien 1572145247Sdamien coerced_params 1573145247Sdamien = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl); 1574192468Ssam 1575145247Sdamien /* Check for errors in format strings. */ 1576145247Sdamien 1577145247Sdamien if (warn_format && (name || assembler_name)) 1578145247Sdamien check_function_format (name, assembler_name, coerced_params); 1579145247Sdamien 1580145247Sdamien /* Recognize certain built-in functions so we can make tree-codes 1581145247Sdamien other than CALL_EXPR. We do this when it enables fold-const.c 1582145247Sdamien to do something useful. */ 1583260444Skevlo 1584145247Sdamien if (TREE_CODE (function) == ADDR_EXPR 1585145247Sdamien && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL 1586145247Sdamien && DECL_BUILT_IN (TREE_OPERAND (function, 0))) 1587145247Sdamien switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0))) 1588145247Sdamien { 1589145247Sdamien case BUILT_IN_ABS: 1590145247Sdamien case BUILT_IN_LABS: 1591145247Sdamien case BUILT_IN_FABS: 1592145247Sdamien if (coerced_params == 0) 1593145247Sdamien return integer_zero_node; 1594145247Sdamien return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0); 1595145247Sdamien default: 1596145247Sdamien break; 1597145247Sdamien } 1598145247Sdamien 1599145247Sdamien { 1600145247Sdamien register tree result 1601145247Sdamien = build (CALL_EXPR, TREE_TYPE (fntype), 1602145247Sdamien function, coerced_params, NULL_TREE); 1603145247Sdamien 1604145247Sdamien TREE_SIDE_EFFECTS (result) = 1; 1605145247Sdamien if (TREE_TYPE (result) == void_type_node) 1606243857Sglebius return result; 1607145247Sdamien return require_complete_type (result); 1608145247Sdamien } 1609145247Sdamien} 1610145247Sdamien 1611145247Sdamien/* Convert the argument expressions in the list VALUES 1612145247Sdamien to the types in the list TYPELIST. The result is a list of converted 1613145247Sdamien argument expressions. 1614145247Sdamien 1615145247Sdamien If TYPELIST is exhausted, or when an element has NULL as its type, 1616145247Sdamien perform the default conversions. 1617145247Sdamien 1618145247Sdamien PARMLIST is the chain of parm decls for the function being called. 1619145247Sdamien It may be 0, if that info is not available. 1620145247Sdamien It is used only for generating error messages. 1621145247Sdamien 1622145247Sdamien NAME is an IDENTIFIER_NODE or 0. It is used only for error messages. 1623145247Sdamien 1624145247Sdamien This is also where warnings about wrong number of args are generated. 1625145247Sdamien 1626145247Sdamien Both VALUES and the returned value are chains of TREE_LIST nodes 1627145247Sdamien with the elements of the list in the TREE_VALUE slots of those nodes. */ 1628145247Sdamien 1629145247Sdamienstatic tree 1630145247Sdamienconvert_arguments (typelist, values, name, fundecl) 1631145247Sdamien tree typelist, values, name, fundecl; 1632145247Sdamien{ 1633145247Sdamien register tree typetail, valtail; 1634145247Sdamien register tree result = NULL; 1635145247Sdamien int parmnum; 1636145247Sdamien 1637145247Sdamien /* Scan the given expressions and types, producing individual 1638145247Sdamien converted arguments and pushing them on RESULT in reverse order. */ 1639145247Sdamien 1640145247Sdamien for (valtail = values, typetail = typelist, parmnum = 0; 1641145247Sdamien valtail; 1642145247Sdamien valtail = TREE_CHAIN (valtail), parmnum++) 1643145247Sdamien { 1644145247Sdamien register tree type = typetail ? TREE_VALUE (typetail) : 0; 1645145247Sdamien register tree val = TREE_VALUE (valtail); 1646145247Sdamien 1647145247Sdamien if (type == void_type_node) 1648145247Sdamien { 1649145247Sdamien if (name) 1650145247Sdamien error ("too many arguments to function `%s'", 1651145247Sdamien IDENTIFIER_POINTER (name)); 1652145247Sdamien else 1653145247Sdamien error ("too many arguments to function"); 1654145247Sdamien break; 1655145247Sdamien } 1656145247Sdamien 1657145247Sdamien /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 1658145247Sdamien /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0 1659145247Sdamien to convert automatically to a pointer. */ 1660145247Sdamien if (TREE_CODE (val) == NON_LVALUE_EXPR) 1661145247Sdamien val = TREE_OPERAND (val, 0); 1662145247Sdamien 1663145247Sdamien if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE 1664145247Sdamien || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE) 1665145247Sdamien val = default_conversion (val); 1666145247Sdamien 1667145247Sdamien val = require_complete_type (val); 1668145247Sdamien 1669145247Sdamien if (type != 0) 1670145247Sdamien { 1671145247Sdamien /* Formal parm type is specified by a function prototype. */ 1672145247Sdamien tree parmval; 1673172574Sthompsa 1674145247Sdamien if (TYPE_SIZE (type) == 0) 1675145247Sdamien { 1676145247Sdamien error ("type of formal parameter %d is incomplete", parmnum + 1); 1677145247Sdamien parmval = val; 1678145247Sdamien } 1679145247Sdamien else 1680145247Sdamien { 1681145247Sdamien /* Optionally warn about conversions that 1682145247Sdamien differ from the default conversions. */ 1683145247Sdamien if (warn_conversion) 1684145247Sdamien { 1685145247Sdamien int formal_prec = TYPE_PRECISION (type); 1686145247Sdamien 1687145247Sdamien if (INTEGRAL_TYPE_P (type) 1688145247Sdamien && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) 1689178354Ssam warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1); 1690178354Ssam else if (TREE_CODE (type) == COMPLEX_TYPE 1691178354Ssam && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) 1692178354Ssam warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1); 1693178354Ssam else if (TREE_CODE (type) == REAL_TYPE 1694178354Ssam && INTEGRAL_TYPE_P (TREE_TYPE (val))) 1695178354Ssam warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1); 1696178354Ssam else if (TREE_CODE (type) == REAL_TYPE 1697178354Ssam && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE) 1698178354Ssam warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1); 1699287197Sglebius /* ??? At some point, messages should be written about 1700287197Sglebius conversions between complex types, but that's too messy 1701145247Sdamien to do now. */ 1702287197Sglebius else if (TREE_CODE (type) == REAL_TYPE 1703287197Sglebius && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE) 1704172567Sthompsa { 1705172567Sthompsa /* Warn if any argument is passed as `float', 1706287197Sglebius since without a prototype it would be `double'. */ 1707287197Sglebius if (formal_prec == TYPE_PRECISION (float_type_node)) 1708287197Sglebius warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1); 1709287197Sglebius } 1710287197Sglebius /* Detect integer changing in width or signedness. */ 1711287197Sglebius else if (INTEGRAL_TYPE_P (type) 1712287197Sglebius && INTEGRAL_TYPE_P (TREE_TYPE (val))) 1713287197Sglebius { 1714287197Sglebius tree would_have_been = default_conversion (val); 1715287197Sglebius tree type1 = TREE_TYPE (would_have_been); 1716172567Sthompsa 1717287197Sglebius if (TREE_CODE (type) == ENUMERAL_TYPE 1718172567Sthompsa && type == TREE_TYPE (val)) 1719172567Sthompsa /* No warning if function asks for enum 1720172567Sthompsa and the actual arg is that enum type. */ 1721287197Sglebius ; 1722172567Sthompsa else if (formal_prec != TYPE_PRECISION (type1)) 1723145247Sdamien warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1); 1724178354Ssam else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1)) 1725145247Sdamien ; 1726172567Sthompsa /* Don't complain if the formal parameter type 1727145247Sdamien is an enum, because we can't tell now whether 1728339976Sglebius the value was an enum--even the same enum. */ 1729287197Sglebius else if (TREE_CODE (type) == ENUMERAL_TYPE) 1730178354Ssam ; 1731287197Sglebius else if (TREE_CODE (val) == INTEGER_CST 1732287197Sglebius && int_fits_type_p (val, type)) 1733287197Sglebius /* Change in signedness doesn't matter 1734145247Sdamien if a constant value is unaffected. */ 1735145247Sdamien ; 1736145247Sdamien /* Likewise for a constant in a NOP_EXPR. */ 1737145247Sdamien else if (TREE_CODE (val) == NOP_EXPR 1738145247Sdamien && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST 1739145247Sdamien && int_fits_type_p (TREE_OPERAND (val, 0), type)) 1740145247Sdamien ; 1741145247Sdamien#if 0 /* We never get such tree structure here. */ 1742145247Sdamien else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE 1743172567Sthompsa && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type) 1744145247Sdamien && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type)) 1745172567Sthompsa /* Change in signedness doesn't matter 1746287197Sglebius if an enum value is unaffected. */ 1747145247Sdamien ; 1748172567Sthompsa#endif 1749156599Sdamien /* If the value is extended from a narrower 1750145247Sdamien unsigned type, it doesn't matter whether we 1751145247Sdamien pass it as signed or unsigned; the value 1752287197Sglebius certainly is the same either way. */ 1753287197Sglebius else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type) 1754178354Ssam && TREE_UNSIGNED (TREE_TYPE (val))) 1755145247Sdamien ; 1756145247Sdamien else if (TREE_UNSIGNED (type)) 1757172567Sthompsa warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1); 1758172567Sthompsa else 1759172567Sthompsa warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1); 1760172567Sthompsa } 1761172567Sthompsa } 1762206767Sbschmidt 1763178354Ssam parmval = convert_for_assignment (type, val, 1764206767Sbschmidt (char *) 0, /* arg passing */ 1765172567Sthompsa fundecl, name, parmnum + 1); 1766172567Sthompsa 1767172567Sthompsa#ifdef PROMOTE_PROTOTYPES 1768172567Sthompsa if ((TREE_CODE (type) == INTEGER_TYPE 1769287197Sglebius || TREE_CODE (type) == ENUMERAL_TYPE) 1770172567Sthompsa && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) 1771145247Sdamien parmval = default_conversion (parmval); 1772145247Sdamien#endif 1773287197Sglebius } 1774287197Sglebius result = tree_cons (NULL_TREE, parmval, result); 1775145247Sdamien } 1776287197Sglebius else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE 1777287197Sglebius && (TYPE_PRECISION (TREE_TYPE (val)) 1778145247Sdamien < TYPE_PRECISION (double_type_node))) 1779287197Sglebius /* Convert `float' to `double'. */ 1780287197Sglebius result = tree_cons (NULL_TREE, convert (double_type_node, val), result); 1781287197Sglebius else 1782287197Sglebius /* Convert `short' and `char' to full-size `int'. */ 1783287197Sglebius result = tree_cons (NULL_TREE, default_conversion (val), result); 1784145247Sdamien 1785287197Sglebius if (typetail) 1786287197Sglebius typetail = TREE_CHAIN (typetail); 1787287197Sglebius } 1788287197Sglebius 1789287197Sglebius if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) 1790145247Sdamien { 1791145247Sdamien if (name) 1792145247Sdamien error ("too few arguments to function `%s'", 1793145247Sdamien IDENTIFIER_POINTER (name)); 1794145247Sdamien else 1795156599Sdamien error ("too few arguments to function"); 1796145247Sdamien } 1797145247Sdamien 1798145247Sdamien return nreverse (result); 1799145247Sdamien} 1800145247Sdamien 1801145247Sdamien/* This is the entry point used by the parser 1802145247Sdamien for binary operators in the input. 1803145247Sdamien In addition to constructing the expression, 1804145247Sdamien we check for operands that were written with other binary operators 1805145247Sdamien in a way that is likely to confuse the user. */ 1806145247Sdamien 1807145247Sdamientree 1808145247Sdamienparser_build_binary_op (code, arg1, arg2) 1809145247Sdamien enum tree_code code; 1810156599Sdamien tree arg1, arg2; 1811156599Sdamien{ 1812145247Sdamien tree result = build_binary_op (code, arg1, arg2, 1); 1813172567Sthompsa 1814172567Sthompsa char class; 1815145247Sdamien char class1 = TREE_CODE_CLASS (TREE_CODE (arg1)); 1816145247Sdamien char class2 = TREE_CODE_CLASS (TREE_CODE (arg2)); 1817145247Sdamien enum tree_code code1 = ERROR_MARK; 1818145247Sdamien enum tree_code code2 = ERROR_MARK; 1819145247Sdamien 1820156599Sdamien if (class1 == 'e' || class1 == '1' 1821145247Sdamien || class1 == '2' || class1 == '<') 1822145247Sdamien code1 = C_EXP_ORIGINAL_CODE (arg1); 1823145247Sdamien if (class2 == 'e' || class2 == '1' 1824145247Sdamien || class2 == '2' || class2 == '<') 1825145247Sdamien code2 = C_EXP_ORIGINAL_CODE (arg2); 1826156599Sdamien 1827156599Sdamien /* Check for cases such as x+y<<z which users are likely 1828145247Sdamien to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE 1829145247Sdamien is cleared to prevent these warnings. */ 1830145247Sdamien if (warn_parentheses) 1831145247Sdamien { 1832145247Sdamien if (code == LSHIFT_EXPR || code == RSHIFT_EXPR) 1833145247Sdamien { 1834145247Sdamien if (code1 == PLUS_EXPR || code1 == MINUS_EXPR 1835145247Sdamien || code2 == PLUS_EXPR || code2 == MINUS_EXPR) 1836145247Sdamien warning ("suggest parentheses around + or - inside shift"); 1837145247Sdamien } 1838156599Sdamien 1839156599Sdamien if (code == TRUTH_ORIF_EXPR) 1840145247Sdamien { 1841145247Sdamien if (code1 == TRUTH_ANDIF_EXPR 1842145247Sdamien || code2 == TRUTH_ANDIF_EXPR) 1843156599Sdamien warning ("suggest parentheses around && within ||"); 1844156599Sdamien } 1845145247Sdamien 1846145247Sdamien if (code == BIT_IOR_EXPR) 1847145247Sdamien { 1848145247Sdamien if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR 1849172567Sthompsa || code1 == PLUS_EXPR || code1 == MINUS_EXPR 1850172567Sthompsa || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR 1851172567Sthompsa || code2 == PLUS_EXPR || code2 == MINUS_EXPR) 1852172567Sthompsa warning ("suggest parentheses around arithmetic in operand of |"); 1853172567Sthompsa /* Check cases like x|y==z */ 1854172567Sthompsa if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') 1855172567Sthompsa warning ("suggest parentheses around comparison in operand of |"); 1856172567Sthompsa } 1857172567Sthompsa 1858172567Sthompsa if (code == BIT_XOR_EXPR) 1859172567Sthompsa { 1860172567Sthompsa if (code1 == BIT_AND_EXPR 1861172567Sthompsa || code1 == PLUS_EXPR || code1 == MINUS_EXPR 1862172567Sthompsa || code2 == BIT_AND_EXPR 1863172567Sthompsa || code2 == PLUS_EXPR || code2 == MINUS_EXPR) 1864172567Sthompsa warning ("suggest parentheses around arithmetic in operand of ^"); 1865172567Sthompsa /* Check cases like x^y==z */ 1866172567Sthompsa if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') 1867172567Sthompsa warning ("suggest parentheses around comparison in operand of ^"); 1868172567Sthompsa } 1869172567Sthompsa 1870172567Sthompsa if (code == BIT_AND_EXPR) 1871172567Sthompsa { 1872172567Sthompsa if (code1 == PLUS_EXPR || code1 == MINUS_EXPR 1873172567Sthompsa || code2 == PLUS_EXPR || code2 == MINUS_EXPR) 1874172567Sthompsa warning ("suggest parentheses around + or - in operand of &"); 1875172567Sthompsa /* Check cases like x&y==z */ 1876172567Sthompsa if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') 1877172567Sthompsa warning ("suggest parentheses around comparison in operand of &"); 1878172567Sthompsa } 1879172567Sthompsa } 1880172567Sthompsa 1881172567Sthompsa /* Similarly, check for cases like 1<=i<=10 that are probably errors. */ 1882172567Sthompsa if (TREE_CODE_CLASS (code) == '<' && extra_warnings 1883172567Sthompsa && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')) 1884172567Sthompsa warning ("comparisons like X<=Y<=Z do not have their mathematical meaning"); 1885172567Sthompsa 1886172567Sthompsa unsigned_conversion_warning (result, arg1); 1887172567Sthompsa unsigned_conversion_warning (result, arg2); 1888172567Sthompsa overflow_warning (result); 1889172567Sthompsa 1890172567Sthompsa class = TREE_CODE_CLASS (TREE_CODE (result)); 1891172567Sthompsa 1892172567Sthompsa /* Record the code that was specified in the source, 1893172567Sthompsa for the sake of warnings about confusing nesting. */ 1894172567Sthompsa if (class == 'e' || class == '1' 1895172567Sthompsa || class == '2' || class == '<') 1896172567Sthompsa C_SET_EXP_ORIGINAL_CODE (result, code); 1897172567Sthompsa else 1898172567Sthompsa { 1899172567Sthompsa int flag = TREE_CONSTANT (result); 1900172567Sthompsa /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR 1901172567Sthompsa so that convert_for_assignment wouldn't strip it. 1902172567Sthompsa That way, we got warnings for things like p = (1 - 1). 1903145247Sdamien But it turns out we should not get those warnings. */ 1904145247Sdamien result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result); 1905145247Sdamien C_SET_EXP_ORIGINAL_CODE (result, code); 1906145247Sdamien TREE_CONSTANT (result) = flag; 1907156599Sdamien } 1908145247Sdamien 1909145247Sdamien return result; 1910145247Sdamien} 1911145247Sdamien 1912145247Sdamien/* Build a binary-operation expression without default conversions. 1913145247Sdamien CODE is the kind of expression to build. 1914145247Sdamien This function differs from `build' in several ways: 1915145247Sdamien the data type of the result is computed and recorded in it, 1916145247Sdamien warnings are generated if arg data types are invalid, 1917145247Sdamien special handling for addition and subtraction of pointers is known, 1918145247Sdamien and some optimization is done (operations on narrow ints 1919145247Sdamien are done in the narrower type when that gives the same result). 1920145247Sdamien Constant folding is also done before the result is returned. 1921145247Sdamien 1922145247Sdamien Note that the operands will never have enumeral types, or function 1923145247Sdamien or array types, because either they will have the default conversions 1924145247Sdamien performed or they have both just been converted to some other type in which 1925145247Sdamien the arithmetic is to be done. */ 1926145247Sdamien 1927145247Sdamientree 1928145247Sdamienbuild_binary_op (code, orig_op0, orig_op1, convert_p) 1929145247Sdamien enum tree_code code; 1930145247Sdamien tree orig_op0, orig_op1; 1931145247Sdamien int convert_p; 1932145247Sdamien{ 1933145247Sdamien tree type0, type1; 1934145247Sdamien register enum tree_code code0, code1; 1935145247Sdamien tree op0, op1; 1936145247Sdamien 1937145247Sdamien /* Expression code to give to the expression when it is built. 1938145247Sdamien Normally this is CODE, which is what the caller asked for, 1939145247Sdamien but in some special cases we change it. */ 1940145247Sdamien register enum tree_code resultcode = code; 1941145247Sdamien 1942145247Sdamien /* Data type in which the computation is to be performed. 1943145247Sdamien In the simplest cases this is the common type of the arguments. */ 1944145247Sdamien register tree result_type = NULL; 1945145247Sdamien 1946145247Sdamien /* Nonzero means operands have already been type-converted 1947145247Sdamien in whatever way is necessary. 1948145247Sdamien Zero means they need to be converted to RESULT_TYPE. */ 1949145247Sdamien int converted = 0; 1950145247Sdamien 1951145247Sdamien /* Nonzero means create the expression with this type, rather than 1952145247Sdamien RESULT_TYPE. */ 1953145247Sdamien tree build_type = 0; 1954145247Sdamien 1955145247Sdamien /* Nonzero means after finally constructing the expression 1956145247Sdamien convert it to this type. */ 1957145247Sdamien tree final_type = 0; 1958145247Sdamien 1959156599Sdamien /* Nonzero if this is an operation like MIN or MAX which can 1960145247Sdamien safely be computed in short if both args are promoted shorts. 1961156599Sdamien Also implies COMMON. 1962156599Sdamien -1 indicates a bitwise operation; this makes a difference 1963145247Sdamien in the exact conditions for when it is safe to do the operation 1964145247Sdamien in a narrower mode. */ 1965145247Sdamien int shorten = 0; 1966145247Sdamien 1967145247Sdamien /* Nonzero if this is a comparison operation; 1968145247Sdamien if both args are promoted shorts, compare the original shorts. 1969145247Sdamien Also implies COMMON. */ 1970145247Sdamien int short_compare = 0; 1971145247Sdamien 1972145247Sdamien /* Nonzero if this is a right-shift operation, which can be computed on the 1973145247Sdamien original short and then promoted if the operand is a promoted short. */ 1974145247Sdamien int short_shift = 0; 1975145247Sdamien 1976145247Sdamien /* Nonzero means set RESULT_TYPE to the common type of the args. */ 1977145247Sdamien int common = 0; 1978145247Sdamien 1979145247Sdamien if (convert_p) 1980145247Sdamien { 1981145247Sdamien op0 = default_conversion (orig_op0); 1982145247Sdamien op1 = default_conversion (orig_op1); 1983145247Sdamien } 1984145247Sdamien else 1985156599Sdamien { 1986156599Sdamien op0 = orig_op0; 1987145247Sdamien op1 = orig_op1; 1988145247Sdamien } 1989145247Sdamien 1990145247Sdamien type0 = TREE_TYPE (op0); 1991145247Sdamien type1 = TREE_TYPE (op1); 1992145247Sdamien 1993145247Sdamien /* The expression codes of the data types of the arguments tell us 1994145247Sdamien whether the arguments are integers, floating, pointers, etc. */ 1995156599Sdamien code0 = TREE_CODE (type0); 1996156599Sdamien code1 = TREE_CODE (type1); 1997156599Sdamien 1998145247Sdamien /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ 1999145247Sdamien STRIP_TYPE_NOPS (op0); 2000145247Sdamien STRIP_TYPE_NOPS (op1); 2001145247Sdamien 2002145247Sdamien /* If an error was already reported for one of the arguments, 2003172567Sthompsa avoid reporting another error. */ 2004172567Sthompsa 2005287197Sglebius if (code0 == ERROR_MARK || code1 == ERROR_MARK) 2006178354Ssam return error_mark_node; 2007172567Sthompsa 2008172567Sthompsa switch (code) 2009172567Sthompsa { 2010172567Sthompsa case PLUS_EXPR: 2011172567Sthompsa /* Handle the pointer + int case. */ 2012178354Ssam if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2013172567Sthompsa return pointer_int_sum (PLUS_EXPR, op0, op1); 2014172567Sthompsa else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) 2015172567Sthompsa return pointer_int_sum (PLUS_EXPR, op1, op0); 2016172567Sthompsa else 2017172567Sthompsa common = 1; 2018172567Sthompsa break; 2019172567Sthompsa 2020172567Sthompsa case MINUS_EXPR: 2021172567Sthompsa /* Subtraction of two similar pointers. 2022172567Sthompsa We must subtract them as integers, then divide by object size. */ 2023172567Sthompsa if (code0 == POINTER_TYPE && code1 == POINTER_TYPE 2024172567Sthompsa && comp_target_types (type0, type1)) 2025172567Sthompsa return pointer_diff (op0, op1); 2026172567Sthompsa /* Handle pointer minus int. Just like pointer plus int. */ 2027172567Sthompsa else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2028172567Sthompsa return pointer_int_sum (MINUS_EXPR, op0, op1); 2029172567Sthompsa else 2030172567Sthompsa common = 1; 2031172567Sthompsa break; 2032172567Sthompsa 2033172567Sthompsa case MULT_EXPR: 2034172567Sthompsa common = 1; 2035172567Sthompsa break; 2036172567Sthompsa 2037172567Sthompsa case TRUNC_DIV_EXPR: 2038172567Sthompsa case CEIL_DIV_EXPR: 2039172567Sthompsa case FLOOR_DIV_EXPR: 2040172567Sthompsa case ROUND_DIV_EXPR: 2041172567Sthompsa case EXACT_DIV_EXPR: 2042172567Sthompsa if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 2043172567Sthompsa || code0 == COMPLEX_TYPE) 2044172567Sthompsa && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 2045172567Sthompsa || code1 == COMPLEX_TYPE)) 2046172567Sthompsa { 2047172567Sthompsa if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)) 2048172567Sthompsa resultcode = RDIV_EXPR; 2049172567Sthompsa else 2050172567Sthompsa { 2051172567Sthompsa /* Although it would be tempting to shorten always here, that 2052172567Sthompsa loses on some targets, since the modulo instruction is 2053172567Sthompsa undefined if the quotient can't be represented in the 2054172567Sthompsa computation mode. We shorten only if unsigned or if 2055172567Sthompsa dividing by something we know != -1. */ 2056172567Sthompsa shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0)) 2057172567Sthompsa || (TREE_CODE (op1) == INTEGER_CST 2058172567Sthompsa && (TREE_INT_CST_LOW (op1) != -1 2059172567Sthompsa || TREE_INT_CST_HIGH (op1) != -1))); 2060172567Sthompsa } 2061172567Sthompsa common = 1; 2062172567Sthompsa } 2063172567Sthompsa break; 2064172567Sthompsa 2065172567Sthompsa case BIT_AND_EXPR: 2066172567Sthompsa case BIT_ANDTC_EXPR: 2067172567Sthompsa case BIT_IOR_EXPR: 2068172567Sthompsa case BIT_XOR_EXPR: 2069172567Sthompsa if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2070172567Sthompsa shorten = -1; 2071172567Sthompsa /* If one operand is a constant, and the other is a short type 2072172567Sthompsa that has been converted to an int, 2073172567Sthompsa really do the work in the short type and then convert the 2074172567Sthompsa result to int. If we are lucky, the constant will be 0 or 1 2075172567Sthompsa in the short type, making the entire operation go away. */ 2076172567Sthompsa if (TREE_CODE (op0) == INTEGER_CST 2077172567Sthompsa && TREE_CODE (op1) == NOP_EXPR 2078172567Sthompsa && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))) 2079172567Sthompsa && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0)))) 2080172567Sthompsa { 2081172567Sthompsa final_type = result_type; 2082172567Sthompsa op1 = TREE_OPERAND (op1, 0); 2083172567Sthompsa result_type = TREE_TYPE (op1); 2084172567Sthompsa } 2085172567Sthompsa if (TREE_CODE (op1) == INTEGER_CST 2086172567Sthompsa && TREE_CODE (op0) == NOP_EXPR 2087172567Sthompsa && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))) 2088172567Sthompsa && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)))) 2089172567Sthompsa { 2090172567Sthompsa final_type = result_type; 2091172567Sthompsa op0 = TREE_OPERAND (op0, 0); 2092172567Sthompsa result_type = TREE_TYPE (op0); 2093172567Sthompsa } 2094172567Sthompsa break; 2095172567Sthompsa 2096172567Sthompsa case TRUNC_MOD_EXPR: 2097172567Sthompsa case FLOOR_MOD_EXPR: 2098172567Sthompsa if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2099172567Sthompsa { 2100172567Sthompsa /* Although it would be tempting to shorten always here, that loses 2101172567Sthompsa on some targets, since the modulo instruction is undefined if the 2102172567Sthompsa quotient can't be represented in the computation mode. We shorten 2103172567Sthompsa only if unsigned or if dividing by something we know != -1. */ 2104172567Sthompsa shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0)) 2105172567Sthompsa || (TREE_CODE (op1) == INTEGER_CST 2106172567Sthompsa && (TREE_INT_CST_LOW (op1) != -1 2107172567Sthompsa || TREE_INT_CST_HIGH (op1) != -1))); 2108172567Sthompsa common = 1; 2109172567Sthompsa } 2110172567Sthompsa break; 2111172567Sthompsa 2112172567Sthompsa case TRUTH_ANDIF_EXPR: 2113172567Sthompsa case TRUTH_ORIF_EXPR: 2114172567Sthompsa case TRUTH_AND_EXPR: 2115172567Sthompsa case TRUTH_OR_EXPR: 2116172567Sthompsa case TRUTH_XOR_EXPR: 2117172567Sthompsa if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE 2118172567Sthompsa || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) 2119172567Sthompsa && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE 2120172567Sthompsa || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) 2121172567Sthompsa { 2122172567Sthompsa /* Result of these operations is always an int, 2123172567Sthompsa but that does not mean the operands should be 2124172567Sthompsa converted to ints! */ 2125172567Sthompsa result_type = integer_type_node; 2126172567Sthompsa op0 = truthvalue_conversion (op0); 2127172567Sthompsa op1 = truthvalue_conversion (op1); 2128172567Sthompsa converted = 1; 2129172567Sthompsa } 2130172567Sthompsa break; 2131172567Sthompsa 2132172567Sthompsa /* Shift operations: result has same type as first operand; 2133172567Sthompsa always convert second operand to int. 2134172567Sthompsa Also set SHORT_SHIFT if shifting rightward. */ 2135172567Sthompsa 2136172567Sthompsa case RSHIFT_EXPR: 2137172567Sthompsa if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2138172567Sthompsa { 2139172567Sthompsa if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) 2140172567Sthompsa { 2141172567Sthompsa if (tree_int_cst_sgn (op1) < 0) 2142172567Sthompsa warning ("right shift count is negative"); 2143172567Sthompsa else 2144172567Sthompsa { 2145172567Sthompsa if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1)) 2146172567Sthompsa short_shift = 1; 2147172567Sthompsa if (TREE_INT_CST_HIGH (op1) != 0 2148287197Sglebius || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) 2149172567Sthompsa >= TYPE_PRECISION (type0))) 2150172567Sthompsa warning ("right shift count >= width of type"); 2151172567Sthompsa } 2152172567Sthompsa } 2153172567Sthompsa /* Use the type of the value to be shifted. 2154172567Sthompsa This is what most traditional C compilers do. */ 2155172567Sthompsa result_type = type0; 2156172567Sthompsa /* Unless traditional, convert the shift-count to an integer, 2157172567Sthompsa regardless of size of value being shifted. */ 2158172567Sthompsa if (! flag_traditional) 2159172567Sthompsa { 2160178354Ssam if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 2161191746Sthompsa op1 = convert (integer_type_node, op1); 2162145247Sdamien /* Avoid converting op1 to result_type later. */ 2163286865Sadrian converted = 1; 2164178354Ssam } 2165145247Sdamien } 2166145247Sdamien break; 2167172567Sthompsa 2168145247Sdamien case LSHIFT_EXPR: 2169178354Ssam if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2170172567Sthompsa { 2171172567Sthompsa if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) 2172178354Ssam { 2173172567Sthompsa if (tree_int_cst_sgn (op1) < 0) 2174152637Sdamien warning ("left shift count is negative"); 2175178354Ssam else if (TREE_INT_CST_HIGH (op1) != 0 2176145247Sdamien || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) 2177145247Sdamien >= TYPE_PRECISION (type0))) 2178145247Sdamien warning ("left shift count >= width of type"); 2179172567Sthompsa } 2180145247Sdamien /* Use the type of the value to be shifted. 2181145247Sdamien This is what most traditional C compilers do. */ 2182178354Ssam result_type = type0; 2183145247Sdamien /* Unless traditional, convert the shift-count to an integer, 2184178354Ssam regardless of size of value being shifted. */ 2185178354Ssam if (! flag_traditional) 2186178354Ssam { 2187145247Sdamien if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 2188178354Ssam op1 = convert (integer_type_node, op1); 2189145247Sdamien /* Avoid converting op1 to result_type later. */ 2190178354Ssam converted = 1; 2191178354Ssam } 2192178354Ssam } 2193145247Sdamien break; 2194178354Ssam 2195145247Sdamien case RROTATE_EXPR: 2196178354Ssam case LROTATE_EXPR: 2197172567Sthompsa if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) 2198172567Sthompsa { 2199178354Ssam if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0) 2200172567Sthompsa { 2201178354Ssam if (tree_int_cst_sgn (op1) < 0) 2202178354Ssam warning ("shift count is negative"); 2203172567Sthompsa else if (TREE_INT_CST_HIGH (op1) != 0 2204172567Sthompsa || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1) 2205172567Sthompsa >= TYPE_PRECISION (type0))) 2206172567Sthompsa warning ("shift count >= width of type"); 2207172567Sthompsa } 2208178354Ssam /* Use the type of the value to be shifted. 2209172567Sthompsa This is what most traditional C compilers do. */ 2210172567Sthompsa result_type = type0; 2211172567Sthompsa /* Unless traditional, convert the shift-count to an integer, 2212178354Ssam regardless of size of value being shifted. */ 2213172567Sthompsa if (! flag_traditional) 2214172567Sthompsa { 2215172567Sthompsa if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) 2216178354Ssam op1 = convert (integer_type_node, op1); 2217172567Sthompsa /* Avoid converting op1 to result_type later. */ 2218172567Sthompsa converted = 1; 2219172567Sthompsa } 2220178354Ssam } 2221172567Sthompsa break; 2222172567Sthompsa 2223172567Sthompsa case EQ_EXPR: 2224178354Ssam case NE_EXPR: 2225172567Sthompsa /* Result of comparison is always int, 2226206766Sbschmidt but don't convert the args to int! */ 2227206766Sbschmidt build_type = integer_type_node; 2228178354Ssam if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE 2229172567Sthompsa || code0 == COMPLEX_TYPE) 2230178354Ssam && (code1 == INTEGER_TYPE || code1 == REAL_TYPE 2231172567Sthompsa || code1 == COMPLEX_TYPE)) 2232172567Sthompsa short_compare = 1; 2233172567Sthompsa else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 2234172567Sthompsa { 2235178354Ssam register tree tt0 = TREE_TYPE (type0); 2236172567Sthompsa register tree tt1 = TREE_TYPE (type1); 2237172567Sthompsa /* Anything compares with void *. void * compares with anything. 2238172567Sthompsa Otherwise, the targets must be compatible 2239172567Sthompsa and both must be object or both incomplete. */ 2240178354Ssam if (comp_target_types (type0, type1)) 2241178354Ssam result_type = common_type (type0, type1); 2242178354Ssam else if (TYPE_MAIN_VARIANT (tt0) == void_type_node) 2243172567Sthompsa { 2244178354Ssam /* op0 != orig_op0 detects the case of something 2245178354Ssam whose value is 0 but which isn't a valid null ptr const. */ 2246178354Ssam if (pedantic && (!integer_zerop (op0) || op0 != orig_op0) 2247178354Ssam && TREE_CODE (tt1) == FUNCTION_TYPE) 2248178354Ssam pedwarn ("ANSI C forbids comparison of `void *' with function pointer"); 2249172567Sthompsa } 2250172567Sthompsa else if (TYPE_MAIN_VARIANT (tt1) == void_type_node) 2251172567Sthompsa { 2252191746Sthompsa if (pedantic && (!integer_zerop (op1) || op1 != orig_op1) 2253172567Sthompsa && TREE_CODE (tt0) == FUNCTION_TYPE) 2254178354Ssam pedwarn ("ANSI C forbids comparison of `void *' with function pointer"); 2255286865Sadrian } 2256172567Sthompsa else 2257172567Sthompsa pedwarn ("comparison of distinct pointer types lacks a cast"); 2258172567Sthompsa 2259172567Sthompsa if (result_type == NULL_TREE) 2260172567Sthompsa result_type = ptr_type_node; 2261172567Sthompsa } 2262172567Sthompsa else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST 2263178354Ssam && integer_zerop (op1)) 2264178354Ssam result_type = type0; 2265178354Ssam else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST 2266178354Ssam && integer_zerop (op0)) 2267178354Ssam result_type = type1; 2268178354Ssam else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2269178354Ssam { 2270178354Ssam result_type = type0; 2271178354Ssam if (! flag_traditional) 2272178354Ssam pedwarn ("comparison between pointer and integer"); 2273172567Sthompsa } 2274172567Sthompsa else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) 2275172567Sthompsa { 2276156599Sdamien result_type = type1; 2277156599Sdamien if (! flag_traditional) 2278156599Sdamien pedwarn ("comparison between pointer and integer"); 2279145247Sdamien } 2280156599Sdamien break; 2281156599Sdamien 2282156599Sdamien case MAX_EXPR: 2283156599Sdamien case MIN_EXPR: 2284156599Sdamien if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) 2285156599Sdamien && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) 2286145247Sdamien shorten = 1; 2287145247Sdamien else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 2288145247Sdamien { 2289287197Sglebius if (comp_target_types (type0, type1)) 2290172567Sthompsa { 2291172567Sthompsa result_type = common_type (type0, type1); 2292178354Ssam if (pedantic 2293172567Sthompsa && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) 2294178354Ssam pedwarn ("ANSI C forbids ordered comparisons of pointers to functions"); 2295287197Sglebius } 2296178931Sthompsa else 2297172567Sthompsa { 2298172567Sthompsa result_type = ptr_type_node; 2299172567Sthompsa pedwarn ("comparison of distinct pointer types lacks a cast"); 2300178354Ssam } 2301172567Sthompsa } 2302287197Sglebius break; 2303178354Ssam 2304166756Sluigi case LE_EXPR: 2305156599Sdamien case GE_EXPR: 2306178354Ssam case LT_EXPR: 2307145247Sdamien case GT_EXPR: 2308172567Sthompsa build_type = integer_type_node; 2309156599Sdamien if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE) 2310172567Sthompsa && (code1 == INTEGER_TYPE || code1 == REAL_TYPE)) 2311178354Ssam short_compare = 1; 2312172567Sthompsa else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) 2313156599Sdamien { 2314156599Sdamien if (comp_target_types (type0, type1)) 2315156599Sdamien { 2316156599Sdamien result_type = common_type (type0, type1); 2317156599Sdamien if ((TYPE_SIZE (TREE_TYPE (type0)) != 0) 2318172567Sthompsa != (TYPE_SIZE (TREE_TYPE (type1)) != 0)) 2319145247Sdamien pedwarn ("comparison of complete and incomplete pointers"); 2320156599Sdamien else if (pedantic 2321145247Sdamien && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) 2322172567Sthompsa pedwarn ("ANSI C forbids ordered comparisons of pointers to functions"); 2323145247Sdamien } 2324145247Sdamien else 2325145247Sdamien { 2326178354Ssam result_type = ptr_type_node; 2327145247Sdamien pedwarn ("comparison of distinct pointer types lacks a cast"); 2328145247Sdamien } 2329159487Siedowse } 2330178354Ssam else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST 2331178354Ssam && integer_zerop (op1)) 2332156599Sdamien { 2333178354Ssam result_type = type0; 2334159487Siedowse if (pedantic || extra_warnings) 2335156599Sdamien pedwarn ("ordered comparison of pointer with integer zero"); 2336156599Sdamien } 2337178354Ssam else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST 2338156599Sdamien && integer_zerop (op0)) 2339156599Sdamien { 2340145247Sdamien result_type = type1; 2341178354Ssam if (pedantic) 2342145247Sdamien pedwarn ("ordered comparison of pointer with integer zero"); 2343145247Sdamien } 2344145247Sdamien else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) 2345145247Sdamien { 2346145247Sdamien result_type = type0; 2347145247Sdamien if (! flag_traditional) 2348145247Sdamien pedwarn ("comparison between pointer and integer"); 2349145247Sdamien } 2350145247Sdamien else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) 2351145247Sdamien { 2352145247Sdamien result_type = type1; 2353145247Sdamien if (! flag_traditional) 2354145247Sdamien pedwarn ("comparison between pointer and integer"); 2355145247Sdamien } 2356145247Sdamien break; 2357145247Sdamien 2358145247Sdamien default: 2359145247Sdamien break; 2360145247Sdamien } 2361145247Sdamien 2362145247Sdamien if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE) 2363145247Sdamien && 2364145247Sdamien (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE)) 2365145247Sdamien { 2366156599Sdamien int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE); 2367156599Sdamien 2368145247Sdamien if (shorten || common || short_compare) 2369178354Ssam result_type = common_type (type0, type1); 2370145247Sdamien 2371145247Sdamien /* For certain operations (which identify themselves by shorten != 0) 2372145247Sdamien if both args were extended from the same smaller type, 2373145247Sdamien do the arithmetic in that type and then extend. 2374145247Sdamien 2375145247Sdamien shorten !=0 and !=1 indicates a bitwise operation. 2376145247Sdamien For them, this optimization is safe only if 2377145247Sdamien both args are zero-extended or both are sign-extended. 2378145247Sdamien Otherwise, we might change the result. 2379145247Sdamien Eg, (short)-1 | (unsigned short)-1 is (int)-1 2380145247Sdamien but calculated in (unsigned short) it would be (unsigned short)-1. */ 2381145247Sdamien 2382178354Ssam if (shorten && none_complex) 2383145247Sdamien { 2384145247Sdamien int unsigned0, unsigned1; 2385172567Sthompsa tree arg0 = get_narrower (op0, &unsigned0); 2386287197Sglebius tree arg1 = get_narrower (op1, &unsigned1); 2387287197Sglebius /* UNS is 1 if the operation to be done is an unsigned one. */ 2388145247Sdamien int uns = TREE_UNSIGNED (result_type); 2389145247Sdamien tree type; 2390178354Ssam 2391172567Sthompsa final_type = result_type; 2392287197Sglebius 2393145247Sdamien /* Handle the case that OP0 (or OP1) does not *contain* a conversion 2394145247Sdamien but it *requires* conversion to FINAL_TYPE. */ 2395178354Ssam 2396178354Ssam if ((TYPE_PRECISION (TREE_TYPE (op0)) 2397178354Ssam == TYPE_PRECISION (TREE_TYPE (arg0))) 2398287197Sglebius && TREE_TYPE (op0) != final_type) 2399178354Ssam unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0)); 2400178354Ssam if ((TYPE_PRECISION (TREE_TYPE (op1)) 2401178354Ssam == TYPE_PRECISION (TREE_TYPE (arg1))) 2402178354Ssam && TREE_TYPE (op1) != final_type) 2403178354Ssam unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1)); 2404178354Ssam 2405178354Ssam /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ 2406178354Ssam 2407178354Ssam /* For bitwise operations, signedness of nominal type 2408178354Ssam does not matter. Consider only how operands were extended. */ 2409178354Ssam if (shorten == -1) 2410178354Ssam uns = unsigned0; 2411178354Ssam 2412178354Ssam /* Note that in all three cases below we refrain from optimizing 2413178354Ssam an unsigned operation on sign-extended args. 2414178354Ssam That would not be valid. */ 2415178354Ssam 2416178354Ssam /* Both args variable: if both extended in same way 2417178354Ssam from same width, do it in that width. 2418178354Ssam Do it unsigned if args were zero-extended. */ 2419178354Ssam if ((TYPE_PRECISION (TREE_TYPE (arg0)) 2420195562Srpaulo < TYPE_PRECISION (result_type)) 2421195562Srpaulo && (TYPE_PRECISION (TREE_TYPE (arg1)) 2422195562Srpaulo == TYPE_PRECISION (TREE_TYPE (arg0))) 2423178354Ssam && unsigned0 == unsigned1 2424178354Ssam && (unsigned0 || !uns)) 2425178354Ssam result_type 2426178354Ssam = signed_or_unsigned_type (unsigned0, 2427178354Ssam common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); 2428178354Ssam else if (TREE_CODE (arg0) == INTEGER_CST 2429178354Ssam && (unsigned1 || !uns) 2430178354Ssam && (TYPE_PRECISION (TREE_TYPE (arg1)) 2431178354Ssam < TYPE_PRECISION (result_type)) 2432178354Ssam && (type = signed_or_unsigned_type (unsigned1, 2433178354Ssam TREE_TYPE (arg1)), 2434178354Ssam int_fits_type_p (arg0, type))) 2435178354Ssam result_type = type; 2436178354Ssam else if (TREE_CODE (arg1) == INTEGER_CST 2437178354Ssam && (unsigned0 || !uns) 2438178354Ssam && (TYPE_PRECISION (TREE_TYPE (arg0)) 2439178354Ssam < TYPE_PRECISION (result_type)) 2440178354Ssam && (type = signed_or_unsigned_type (unsigned0, 2441178354Ssam TREE_TYPE (arg0)), 2442178354Ssam int_fits_type_p (arg1, type))) 2443287197Sglebius result_type = type; 2444178354Ssam } 2445178354Ssam 2446178354Ssam /* Shifts can be shortened if shifting right. */ 2447178354Ssam 2448178354Ssam if (short_shift) 2449178354Ssam { 2450178354Ssam int unsigned_arg; 2451178354Ssam tree arg0 = get_narrower (op0, &unsigned_arg); 2452207926Sbschmidt 2453178354Ssam final_type = result_type; 2454178354Ssam 2455178354Ssam if (arg0 == op0 && final_type == TREE_TYPE (op0)) 2456178354Ssam unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0)); 2457178354Ssam 2458207926Sbschmidt if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) 2459178354Ssam /* We can shorten only if the shift count is less than the 2460178354Ssam number of bits in the smaller type size. */ 2461178354Ssam && TREE_INT_CST_HIGH (op1) == 0 2462178354Ssam && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1) 2463178354Ssam /* If arg is sign-extended and then unsigned-shifted, 2464207926Sbschmidt we can simulate this with a signed shift in arg's type 2465178354Ssam only if the extended result is at least twice as wide 2466178354Ssam as the arg. Otherwise, the shift could use up all the 2467178354Ssam ones made by sign-extension and bring in zeros. 2468178354Ssam We can't optimize that case at all, but in most machines 2469178354Ssam it never happens because available widths are 2**N. */ 2470178354Ssam && (!TREE_UNSIGNED (final_type) 2471178354Ssam || unsigned_arg 2472178354Ssam || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type))) 2473178354Ssam { 2474178354Ssam /* Do an unsigned shift if the operand was zero-extended. */ 2475178354Ssam result_type 2476178354Ssam = signed_or_unsigned_type (unsigned_arg, 2477178354Ssam TREE_TYPE (arg0)); 2478178354Ssam /* Convert value-to-be-shifted to that type. */ 2479178354Ssam if (TREE_TYPE (op0) != result_type) 2480178354Ssam op0 = convert (result_type, op0); 2481178354Ssam converted = 1; 2482178354Ssam } 2483178354Ssam } 2484178354Ssam 2485178354Ssam /* Comparison operations are shortened too but differently. 2486178354Ssam They identify themselves by setting short_compare = 1. */ 2487178354Ssam 2488145247Sdamien if (short_compare) 2489145247Sdamien { 2490145247Sdamien /* Don't write &op0, etc., because that would prevent op0 2491145247Sdamien from being kept in a register. 2492172567Sthompsa Instead, make copies of the our local variables and 2493172567Sthompsa pass the copies by reference, then copy them back afterward. */ 2494172567Sthompsa tree xop0 = op0, xop1 = op1, xresult_type = result_type; 2495172567Sthompsa enum tree_code xresultcode = resultcode; 2496172567Sthompsa tree val 2497172567Sthompsa = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); 2498172567Sthompsa if (val != 0) 2499172567Sthompsa return val; 2500172567Sthompsa op0 = xop0, op1 = xop1; 2501145247Sdamien converted = 1; 2502145247Sdamien resultcode = xresultcode; 2503172567Sthompsa 2504156599Sdamien if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0) 2505172567Sthompsa && skip_evaluation == 0) 2506145247Sdamien { 2507145247Sdamien int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0)); 2508145247Sdamien int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1)); 2509145247Sdamien 2510145247Sdamien int unsignedp0, unsignedp1; 2511145247Sdamien tree primop0 = get_narrower (op0, &unsignedp0); 2512145247Sdamien tree primop1 = get_narrower (op1, &unsignedp1); 2513145247Sdamien 2514145247Sdamien /* Avoid spurious warnings for comparison with enumerators. */ 2515145247Sdamien 2516145247Sdamien xop0 = orig_op0; 2517287197Sglebius xop1 = orig_op1; 2518145247Sdamien STRIP_TYPE_NOPS (xop0); 2519145247Sdamien STRIP_TYPE_NOPS (xop1); 2520145247Sdamien 2521145247Sdamien /* Give warnings for comparisons between signed and unsigned 2522145247Sdamien quantities that may fail. */ 2523145247Sdamien /* Do the checking based on the original operand trees, so that 2524145247Sdamien casts will be considered, but default promotions won't be. */ 2525145247Sdamien 2526174317Sphilip /* Do not warn if the comparison is being done in a signed type, 2527174317Sphilip since the signed type will only be chosen if it can represent 2528174317Sphilip all the values of the unsigned type. */ 2529145247Sdamien if (! TREE_UNSIGNED (result_type)) 2530145247Sdamien /* OK */; 2531145247Sdamien /* Do not warn if both operands are unsigned. */ 2532145247Sdamien else if (op0_signed == op1_signed) 2533145247Sdamien /* OK */; 2534145247Sdamien /* Do not warn if the signed quantity is an unsuffixed 2535145247Sdamien integer literal (or some static constant expression 2536145247Sdamien involving such literals) and it is non-negative. */ 2537174317Sphilip else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST 2538145247Sdamien && tree_int_cst_sgn (xop0) >= 0) 2539145247Sdamien || (op1_signed && TREE_CODE (xop1) == INTEGER_CST 2540145247Sdamien && tree_int_cst_sgn (xop1) >= 0)) 2541145247Sdamien /* OK */; 2542145247Sdamien /* Do not warn if the comparison is an equality operation, 2543145247Sdamien the unsigned quantity is an integral constant and it does 2544145247Sdamien not use the most significant bit of result_type. */ 2545145247Sdamien else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR) 2546145247Sdamien && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST 2547145247Sdamien && int_fits_type_p (xop1, signed_type (result_type))) 2548145247Sdamien || (op1_signed && TREE_CODE (xop0) == INTEGER_CST 2549145247Sdamien && int_fits_type_p (xop0, signed_type (result_type))))) 2550145247Sdamien /* OK */; 2551145247Sdamien else 2552145247Sdamien warning ("comparison between signed and unsigned"); 2553145247Sdamien 2554145247Sdamien /* Warn if two unsigned values are being compared in a size 2555145247Sdamien larger than their original size, and one (and only one) is the 2556145247Sdamien result of a `~' operator. This comparison will always fail. 2557145247Sdamien 2558145247Sdamien Also warn if one operand is a constant, and the constant 2559145247Sdamien does not have all bits set that are set in the ~ operand 2560145247Sdamien when it is extended. */ 2561145247Sdamien 2562145247Sdamien if ((TREE_CODE (primop0) == BIT_NOT_EXPR) 2563145247Sdamien != (TREE_CODE (primop1) == BIT_NOT_EXPR)) 2564172567Sthompsa { 2565145247Sdamien if (TREE_CODE (primop0) == BIT_NOT_EXPR) 2566145247Sdamien primop0 = get_narrower (TREE_OPERAND (primop0, 0), 2567145247Sdamien &unsignedp0); 2568145247Sdamien else 2569145247Sdamien primop1 = get_narrower (TREE_OPERAND (primop1, 0), 2570145247Sdamien &unsignedp1); 2571145247Sdamien 2572145247Sdamien if (TREE_CODE (primop0) == INTEGER_CST 2573145247Sdamien || TREE_CODE (primop1) == INTEGER_CST) 2574145247Sdamien { 2575145247Sdamien tree primop; 2576145247Sdamien long constant, mask; 2577145247Sdamien int unsignedp, bits; 2578145247Sdamien 2579145247Sdamien if (TREE_CODE (primop0) == INTEGER_CST) 2580145247Sdamien { 2581145247Sdamien primop = primop1; 2582145247Sdamien unsignedp = unsignedp1; 2583145247Sdamien constant = TREE_INT_CST_LOW (primop0); 2584145247Sdamien } 2585145247Sdamien else 2586145247Sdamien { 2587145247Sdamien primop = primop0; 2588145247Sdamien unsignedp = unsignedp0; 2589145247Sdamien constant = TREE_INT_CST_LOW (primop1); 2590145247Sdamien } 2591145247Sdamien 2592145247Sdamien bits = TYPE_PRECISION (TREE_TYPE (primop)); 2593145247Sdamien if (bits < TYPE_PRECISION (result_type) 2594145247Sdamien && bits < HOST_BITS_PER_LONG && unsignedp) 2595145247Sdamien { 2596145247Sdamien mask = (~0L) << bits; 2597145247Sdamien if ((mask & constant) != mask) 2598145247Sdamien warning ("comparison of promoted ~unsigned with constant"); 2599145247Sdamien } 2600172567Sthompsa } 2601145247Sdamien else if (unsignedp0 && unsignedp1 2602145247Sdamien && (TYPE_PRECISION (TREE_TYPE (primop0)) 2603156599Sdamien < TYPE_PRECISION (result_type)) 2604145247Sdamien && (TYPE_PRECISION (TREE_TYPE (primop1)) 2605145247Sdamien < TYPE_PRECISION (result_type))) 2606145247Sdamien warning ("comparison of promoted ~unsigned with unsigned"); 2607145247Sdamien } 2608145247Sdamien } 2609145247Sdamien } 2610145247Sdamien } 2611172567Sthompsa 2612172567Sthompsa /* At this point, RESULT_TYPE must be nonzero to avoid an error message. 2613172567Sthompsa If CONVERTED is zero, both args will be converted to type RESULT_TYPE. 2614172567Sthompsa Then the expression will be built. 2615286865Sadrian It will be given type FINAL_TYPE if that is nonzero; 2616172567Sthompsa otherwise, it will be given type RESULT_TYPE. */ 2617172567Sthompsa 2618191746Sthompsa if (!result_type) 2619172567Sthompsa { 2620172567Sthompsa binary_op_error (code); 2621172567Sthompsa return error_mark_node; 2622172567Sthompsa } 2623300239Savos 2624300239Savos if (! converted) 2625300239Savos { 2626300239Savos if (TREE_TYPE (op0) != result_type) 2627300239Savos op0 = convert (result_type, op0); 2628300239Savos if (TREE_TYPE (op1) != result_type) 2629300239Savos op1 = convert (result_type, op1); 2630300239Savos } 2631300239Savos 2632300239Savos if (build_type == NULL_TREE) 2633300239Savos build_type = result_type; 2634300239Savos 2635300239Savos { 2636300239Savos register tree result = build (resultcode, build_type, op0, op1); 2637300239Savos register tree folded; 2638300239Savos 2639300239Savos folded = fold (result); 2640300239Savos if (folded == result) 2641300239Savos TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); 2642300239Savos if (final_type != 0) 2643172567Sthompsa return convert (final_type, folded); 2644172567Sthompsa return folded; 2645286865Sadrian } 2646172567Sthompsa} 2647172567Sthompsa 2648172567Sthompsa/* Return a tree for the sum or difference (RESULTCODE says which) 2649172567Sthompsa of pointer PTROP and integer INTOP. */ 2650172567Sthompsa 2651172567Sthompsastatic tree 2652172567Sthompsapointer_int_sum (resultcode, ptrop, intop) 2653172567Sthompsa enum tree_code resultcode; 2654172567Sthompsa register tree ptrop, intop; 2655172567Sthompsa{ 2656172567Sthompsa tree size_exp; 2657178354Ssam 2658172567Sthompsa register tree result; 2659172567Sthompsa register tree folded; 2660172567Sthompsa 2661172567Sthompsa /* The result is a pointer of the same type that is being added. */ 2662172567Sthompsa 2663178354Ssam register tree result_type = TREE_TYPE (ptrop); 2664172567Sthompsa 2665172567Sthompsa if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) 2666172567Sthompsa { 2667172567Sthompsa if (pedantic || warn_pointer_arith) 2668172567Sthompsa pedwarn ("pointer of type `void *' used in arithmetic"); 2669172567Sthompsa size_exp = integer_one_node; 2670172567Sthompsa } 2671286865Sadrian else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) 2672172567Sthompsa { 2673172567Sthompsa if (pedantic || warn_pointer_arith) 2674172567Sthompsa pedwarn ("pointer to a function used in arithmetic"); 2675172567Sthompsa size_exp = integer_one_node; 2676172567Sthompsa } 2677 else 2678 size_exp = c_size_in_bytes (TREE_TYPE (result_type)); 2679 2680 /* If what we are about to multiply by the size of the elements 2681 contains a constant term, apply distributive law 2682 and multiply that constant term separately. 2683 This helps produce common subexpressions. */ 2684 2685 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) 2686 && ! TREE_CONSTANT (intop) 2687 && TREE_CONSTANT (TREE_OPERAND (intop, 1)) 2688 && TREE_CONSTANT (size_exp) 2689 /* If the constant comes from pointer subtraction, 2690 skip this optimization--it would cause an error. */ 2691 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE 2692 /* If the constant is unsigned, and smaller than the pointer size, 2693 then we must skip this optimization. This is because it could cause 2694 an overflow error if the constant is negative but INTOP is not. */ 2695 && (! TREE_UNSIGNED (TREE_TYPE (intop)) 2696 || (TYPE_PRECISION (TREE_TYPE (intop)) 2697 == TYPE_PRECISION (TREE_TYPE (ptrop))))) 2698 { 2699 enum tree_code subcode = resultcode; 2700 tree int_type = TREE_TYPE (intop); 2701 if (TREE_CODE (intop) == MINUS_EXPR) 2702 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); 2703 /* Convert both subexpression types to the type of intop, 2704 because weird cases involving pointer arithmetic 2705 can result in a sum or difference with different type args. */ 2706 ptrop = build_binary_op (subcode, ptrop, 2707 convert (int_type, TREE_OPERAND (intop, 1)), 1); 2708 intop = convert (int_type, TREE_OPERAND (intop, 0)); 2709 } 2710 2711 /* Convert the integer argument to a type the same size as sizetype 2712 so the multiply won't overflow spuriously. */ 2713 2714 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) 2715 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype)) 2716 intop = convert (type_for_size (TYPE_PRECISION (sizetype), 2717 TREE_UNSIGNED (sizetype)), intop); 2718 2719 /* Replace the integer argument with a suitable product by the object size. 2720 Do this multiplication as signed, then convert to the appropriate 2721 pointer type (actually unsigned integral). */ 2722 2723 intop = convert (result_type, 2724 build_binary_op (MULT_EXPR, intop, 2725 convert (TREE_TYPE (intop), size_exp), 1)); 2726 2727 /* Create the sum or difference. */ 2728 2729 result = build (resultcode, result_type, ptrop, intop); 2730 2731 folded = fold (result); 2732 if (folded == result) 2733 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop); 2734 return folded; 2735} 2736 2737/* Return a tree for the difference of pointers OP0 and OP1. 2738 The resulting tree has type int. */ 2739 2740static tree 2741pointer_diff (op0, op1) 2742 register tree op0, op1; 2743{ 2744 register tree result, folded; 2745 tree restype = ptrdiff_type_node; 2746 2747 tree target_type = TREE_TYPE (TREE_TYPE (op0)); 2748 2749 if (pedantic || warn_pointer_arith) 2750 { 2751 if (TREE_CODE (target_type) == VOID_TYPE) 2752 pedwarn ("pointer of type `void *' used in subtraction"); 2753 if (TREE_CODE (target_type) == FUNCTION_TYPE) 2754 pedwarn ("pointer to a function used in subtraction"); 2755 } 2756 2757 /* First do the subtraction as integers; 2758 then drop through to build the divide operator. 2759 Do not do default conversions on the minus operator 2760 in case restype is a short type. */ 2761 2762 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0), 2763 convert (restype, op1), 0); 2764 /* This generates an error if op1 is pointer to incomplete type. */ 2765 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0) 2766 error ("arithmetic on pointer to an incomplete type"); 2767 2768 /* This generates an error if op0 is pointer to incomplete type. */ 2769 op1 = c_size_in_bytes (target_type); 2770 2771 /* Divide by the size, in easiest possible way. */ 2772 2773 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)); 2774 2775 folded = fold (result); 2776 if (folded == result) 2777 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1); 2778 return folded; 2779} 2780 2781/* Construct and perhaps optimize a tree representation 2782 for a unary operation. CODE, a tree_code, specifies the operation 2783 and XARG is the operand. NOCONVERT nonzero suppresses 2784 the default promotions (such as from short to int). */ 2785 2786tree 2787build_unary_op (code, xarg, noconvert) 2788 enum tree_code code; 2789 tree xarg; 2790 int noconvert; 2791{ 2792 /* No default_conversion here. It causes trouble for ADDR_EXPR. */ 2793 register tree arg = xarg; 2794 register tree argtype = 0; 2795 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg)); 2796 char *errstring = NULL; 2797 tree val; 2798 2799 if (typecode == ERROR_MARK) 2800 return error_mark_node; 2801 if (typecode == ENUMERAL_TYPE) 2802 typecode = INTEGER_TYPE; 2803 2804 switch (code) 2805 { 2806 case CONVERT_EXPR: 2807 /* This is used for unary plus, because a CONVERT_EXPR 2808 is enough to prevent anybody from looking inside for 2809 associativity, but won't generate any code. */ 2810 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 2811 || typecode == COMPLEX_TYPE)) 2812 errstring = "wrong type argument to unary plus"; 2813 else if (!noconvert) 2814 arg = default_conversion (arg); 2815 break; 2816 2817 case NEGATE_EXPR: 2818 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 2819 || typecode == COMPLEX_TYPE)) 2820 errstring = "wrong type argument to unary minus"; 2821 else if (!noconvert) 2822 arg = default_conversion (arg); 2823 break; 2824 2825 case BIT_NOT_EXPR: 2826 if (typecode == COMPLEX_TYPE) 2827 { 2828 code = CONJ_EXPR; 2829 if (!noconvert) 2830 arg = default_conversion (arg); 2831 } 2832 else if (typecode != INTEGER_TYPE) 2833 errstring = "wrong type argument to bit-complement"; 2834 else if (!noconvert) 2835 arg = default_conversion (arg); 2836 break; 2837 2838 case ABS_EXPR: 2839 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 2840 || typecode == COMPLEX_TYPE)) 2841 errstring = "wrong type argument to abs"; 2842 else if (!noconvert) 2843 arg = default_conversion (arg); 2844 break; 2845 2846 case CONJ_EXPR: 2847 /* Conjugating a real value is a no-op, but allow it anyway. */ 2848 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE 2849 || typecode == COMPLEX_TYPE)) 2850 errstring = "wrong type argument to conjugation"; 2851 else if (!noconvert) 2852 arg = default_conversion (arg); 2853 break; 2854 2855 case TRUTH_NOT_EXPR: 2856 if (typecode != INTEGER_TYPE 2857 && typecode != REAL_TYPE && typecode != POINTER_TYPE 2858 && typecode != COMPLEX_TYPE 2859 /* These will convert to a pointer. */ 2860 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE) 2861 { 2862 errstring = "wrong type argument to unary exclamation mark"; 2863 break; 2864 } 2865 arg = truthvalue_conversion (arg); 2866 return invert_truthvalue (arg); 2867 2868 case NOP_EXPR: 2869 break; 2870 2871 case REALPART_EXPR: 2872 if (TREE_CODE (arg) == COMPLEX_CST) 2873 return TREE_REALPART (arg); 2874 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) 2875 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); 2876 else 2877 return arg; 2878 2879 case IMAGPART_EXPR: 2880 if (TREE_CODE (arg) == COMPLEX_CST) 2881 return TREE_IMAGPART (arg); 2882 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) 2883 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg)); 2884 else 2885 return convert (TREE_TYPE (arg), integer_zero_node); 2886 2887 case PREINCREMENT_EXPR: 2888 case POSTINCREMENT_EXPR: 2889 case PREDECREMENT_EXPR: 2890 case POSTDECREMENT_EXPR: 2891 /* Handle complex lvalues (when permitted) 2892 by reduction to simpler cases. */ 2893 2894 val = unary_complex_lvalue (code, arg); 2895 if (val != 0) 2896 return val; 2897 2898 /* Increment or decrement the real part of the value, 2899 and don't change the imaginary part. */ 2900 if (typecode == COMPLEX_TYPE) 2901 { 2902 tree real, imag; 2903 2904 arg = stabilize_reference (arg); 2905 real = build_unary_op (REALPART_EXPR, arg, 1); 2906 imag = build_unary_op (IMAGPART_EXPR, arg, 1); 2907 return build (COMPLEX_EXPR, TREE_TYPE (arg), 2908 build_unary_op (code, real, 1), imag); 2909 } 2910 2911 /* Report invalid types. */ 2912 2913 if (typecode != POINTER_TYPE 2914 && typecode != INTEGER_TYPE && typecode != REAL_TYPE) 2915 { 2916 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) 2917 errstring ="wrong type argument to increment"; 2918 else 2919 errstring ="wrong type argument to decrement"; 2920 break; 2921 } 2922 2923 { 2924 register tree inc; 2925 tree result_type = TREE_TYPE (arg); 2926 2927 arg = get_unwidened (arg, 0); 2928 argtype = TREE_TYPE (arg); 2929 2930 /* Compute the increment. */ 2931 2932 if (typecode == POINTER_TYPE) 2933 { 2934 /* If pointer target is an undefined struct, 2935 we just cannot know how to do the arithmetic. */ 2936 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0) 2937 error ("%s of pointer to unknown structure", 2938 ((code == PREINCREMENT_EXPR 2939 || code == POSTINCREMENT_EXPR) 2940 ? "increment" : "decrement")); 2941 else if ((pedantic || warn_pointer_arith) 2942 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE 2943 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)) 2944 pedwarn ("wrong type argument to %s", 2945 ((code == PREINCREMENT_EXPR 2946 || code == POSTINCREMENT_EXPR) 2947 ? "increment" : "decrement")); 2948 inc = c_size_in_bytes (TREE_TYPE (result_type)); 2949 } 2950 else 2951 inc = integer_one_node; 2952 2953 inc = convert (argtype, inc); 2954 2955 /* Handle incrementing a cast-expression. */ 2956 2957 while (1) 2958 switch (TREE_CODE (arg)) 2959 { 2960 case NOP_EXPR: 2961 case CONVERT_EXPR: 2962 case FLOAT_EXPR: 2963 case FIX_TRUNC_EXPR: 2964 case FIX_FLOOR_EXPR: 2965 case FIX_ROUND_EXPR: 2966 case FIX_CEIL_EXPR: 2967 pedantic_lvalue_warning (CONVERT_EXPR); 2968 /* If the real type has the same machine representation 2969 as the type it is cast to, we can make better output 2970 by adding directly to the inside of the cast. */ 2971 if ((TREE_CODE (TREE_TYPE (arg)) 2972 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0)))) 2973 && (TYPE_MODE (TREE_TYPE (arg)) 2974 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0))))) 2975 arg = TREE_OPERAND (arg, 0); 2976 else 2977 { 2978 tree incremented, modify, value; 2979 arg = stabilize_reference (arg); 2980 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) 2981 value = arg; 2982 else 2983 value = save_expr (arg); 2984 incremented = build (((code == PREINCREMENT_EXPR 2985 || code == POSTINCREMENT_EXPR) 2986 ? PLUS_EXPR : MINUS_EXPR), 2987 argtype, value, inc); 2988 TREE_SIDE_EFFECTS (incremented) = 1; 2989 modify = build_modify_expr (arg, NOP_EXPR, incremented); 2990 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value); 2991 TREE_USED (value) = 1; 2992 return value; 2993 } 2994 break; 2995 2996 default: 2997 goto give_up; 2998 } 2999 give_up: 3000 3001 /* Complain about anything else that is not a true lvalue. */ 3002 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR 3003 || code == POSTINCREMENT_EXPR) 3004 ? "increment" : "decrement"))) 3005 return error_mark_node; 3006 3007 /* Report a read-only lvalue. */ 3008 if (TREE_READONLY (arg)) 3009 readonly_warning (arg, 3010 ((code == PREINCREMENT_EXPR 3011 || code == POSTINCREMENT_EXPR) 3012 ? "increment" : "decrement")); 3013 3014 val = build (code, TREE_TYPE (arg), arg, inc); 3015 TREE_SIDE_EFFECTS (val) = 1; 3016 val = convert (result_type, val); 3017 if (TREE_CODE (val) != code) 3018 TREE_NO_UNUSED_WARNING (val) = 1; 3019 return val; 3020 } 3021 3022 case ADDR_EXPR: 3023 /* Note that this operation never does default_conversion 3024 regardless of NOCONVERT. */ 3025 3026 /* Let &* cancel out to simplify resulting code. */ 3027 if (TREE_CODE (arg) == INDIRECT_REF) 3028 { 3029 /* Don't let this be an lvalue. */ 3030 if (lvalue_p (TREE_OPERAND (arg, 0))) 3031 return non_lvalue (TREE_OPERAND (arg, 0)); 3032 return TREE_OPERAND (arg, 0); 3033 } 3034 3035 /* For &x[y], return x+y */ 3036 if (TREE_CODE (arg) == ARRAY_REF) 3037 { 3038 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0) 3039 return error_mark_node; 3040 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0), 3041 TREE_OPERAND (arg, 1), 1); 3042 } 3043 3044 /* Handle complex lvalues (when permitted) 3045 by reduction to simpler cases. */ 3046 val = unary_complex_lvalue (code, arg); 3047 if (val != 0) 3048 return val; 3049 3050#if 0 /* Turned off because inconsistent; 3051 float f; *&(int)f = 3.4 stores in int format 3052 whereas (int)f = 3.4 stores in float format. */ 3053 /* Address of a cast is just a cast of the address 3054 of the operand of the cast. */ 3055 switch (TREE_CODE (arg)) 3056 { 3057 case NOP_EXPR: 3058 case CONVERT_EXPR: 3059 case FLOAT_EXPR: 3060 case FIX_TRUNC_EXPR: 3061 case FIX_FLOOR_EXPR: 3062 case FIX_ROUND_EXPR: 3063 case FIX_CEIL_EXPR: 3064 if (pedantic) 3065 pedwarn ("ANSI C forbids the address of a cast expression"); 3066 return convert (build_pointer_type (TREE_TYPE (arg)), 3067 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 3068 0)); 3069 } 3070#endif 3071 3072 /* Allow the address of a constructor if all the elements 3073 are constant. */ 3074 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg)) 3075 ; 3076 /* Anything not already handled and not a true memory reference 3077 is an error. */ 3078 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'")) 3079 return error_mark_node; 3080 3081 /* Ordinary case; arg is a COMPONENT_REF or a decl. */ 3082 argtype = TREE_TYPE (arg); 3083 /* If the lvalue is const or volatile, 3084 merge that into the type that the address will point to. */ 3085 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd' 3086 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r') 3087 { 3088 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) 3089 argtype = c_build_type_variant (argtype, 3090 TREE_READONLY (arg), 3091 TREE_THIS_VOLATILE (arg)); 3092 } 3093 3094 argtype = build_pointer_type (argtype); 3095 3096 if (mark_addressable (arg) == 0) 3097 return error_mark_node; 3098 3099 { 3100 tree addr; 3101 3102 if (TREE_CODE (arg) == COMPONENT_REF) 3103 { 3104 tree field = TREE_OPERAND (arg, 1); 3105 3106 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0); 3107 3108 if (DECL_C_BIT_FIELD (field)) 3109 { 3110 error ("attempt to take address of bit-field structure member `%s'", 3111 IDENTIFIER_POINTER (DECL_NAME (field))); 3112 return error_mark_node; 3113 } 3114 3115 addr = convert (argtype, addr); 3116 3117 if (! integer_zerop (DECL_FIELD_BITPOS (field))) 3118 { 3119 tree offset 3120 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field), 3121 size_int (BITS_PER_UNIT)); 3122 int flag = TREE_CONSTANT (addr); 3123 addr = fold (build (PLUS_EXPR, argtype, 3124 addr, convert (argtype, offset))); 3125 TREE_CONSTANT (addr) = flag; 3126 } 3127 } 3128 else 3129 addr = build1 (code, argtype, arg); 3130 3131 /* Address of a static or external variable or 3132 file-scope function counts as a constant. */ 3133 if (staticp (arg) 3134 && ! (TREE_CODE (arg) == FUNCTION_DECL 3135 && DECL_CONTEXT (arg) != 0)) 3136 TREE_CONSTANT (addr) = 1; 3137 return addr; 3138 } 3139 3140 default: 3141 break; 3142 } 3143 3144 if (!errstring) 3145 { 3146 if (argtype == 0) 3147 argtype = TREE_TYPE (arg); 3148 return fold (build1 (code, argtype, arg)); 3149 } 3150 3151 error (errstring); 3152 return error_mark_node; 3153} 3154 3155#if 0 3156/* If CONVERSIONS is a conversion expression or a nested sequence of such, 3157 convert ARG with the same conversions in the same order 3158 and return the result. */ 3159 3160static tree 3161convert_sequence (conversions, arg) 3162 tree conversions; 3163 tree arg; 3164{ 3165 switch (TREE_CODE (conversions)) 3166 { 3167 case NOP_EXPR: 3168 case CONVERT_EXPR: 3169 case FLOAT_EXPR: 3170 case FIX_TRUNC_EXPR: 3171 case FIX_FLOOR_EXPR: 3172 case FIX_ROUND_EXPR: 3173 case FIX_CEIL_EXPR: 3174 return convert (TREE_TYPE (conversions), 3175 convert_sequence (TREE_OPERAND (conversions, 0), 3176 arg)); 3177 3178 default: 3179 return arg; 3180 } 3181} 3182#endif /* 0 */ 3183 3184/* Return nonzero if REF is an lvalue valid for this language. 3185 Lvalues can be assigned, unless their type has TYPE_READONLY. 3186 Lvalues can have their address taken, unless they have DECL_REGISTER. */ 3187 3188int 3189lvalue_p (ref) 3190 tree ref; 3191{ 3192 register enum tree_code code = TREE_CODE (ref); 3193 3194 switch (code) 3195 { 3196 case REALPART_EXPR: 3197 case IMAGPART_EXPR: 3198 case COMPONENT_REF: 3199 return lvalue_p (TREE_OPERAND (ref, 0)); 3200 3201 case STRING_CST: 3202 return 1; 3203 3204 case INDIRECT_REF: 3205 case ARRAY_REF: 3206 case VAR_DECL: 3207 case PARM_DECL: 3208 case RESULT_DECL: 3209 case ERROR_MARK: 3210 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE 3211 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); 3212 3213 case BIND_EXPR: 3214 case RTL_EXPR: 3215 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; 3216 3217 default: 3218 return 0; 3219 } 3220} 3221 3222/* Return nonzero if REF is an lvalue valid for this language; 3223 otherwise, print an error message and return zero. */ 3224 3225int 3226lvalue_or_else (ref, string) 3227 tree ref; 3228 char *string; 3229{ 3230 int win = lvalue_p (ref); 3231 if (! win) 3232 error ("invalid lvalue in %s", string); 3233 return win; 3234} 3235 3236/* Apply unary lvalue-demanding operator CODE to the expression ARG 3237 for certain kinds of expressions which are not really lvalues 3238 but which we can accept as lvalues. 3239 3240 If ARG is not a kind of expression we can handle, return zero. */ 3241 3242static tree 3243unary_complex_lvalue (code, arg) 3244 enum tree_code code; 3245 tree arg; 3246{ 3247 /* Handle (a, b) used as an "lvalue". */ 3248 if (TREE_CODE (arg) == COMPOUND_EXPR) 3249 { 3250 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0); 3251 3252 /* If this returns a function type, it isn't really being used as 3253 an lvalue, so don't issue a warning about it. */ 3254 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE) 3255 pedantic_lvalue_warning (COMPOUND_EXPR); 3256 3257 return build (COMPOUND_EXPR, TREE_TYPE (real_result), 3258 TREE_OPERAND (arg, 0), real_result); 3259 } 3260 3261 /* Handle (a ? b : c) used as an "lvalue". */ 3262 if (TREE_CODE (arg) == COND_EXPR) 3263 { 3264 pedantic_lvalue_warning (COND_EXPR); 3265 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE) 3266 pedantic_lvalue_warning (COMPOUND_EXPR); 3267 3268 return (build_conditional_expr 3269 (TREE_OPERAND (arg, 0), 3270 build_unary_op (code, TREE_OPERAND (arg, 1), 0), 3271 build_unary_op (code, TREE_OPERAND (arg, 2), 0))); 3272 } 3273 3274 return 0; 3275} 3276 3277/* If pedantic, warn about improper lvalue. CODE is either COND_EXPR 3278 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */ 3279 3280static void 3281pedantic_lvalue_warning (code) 3282 enum tree_code code; 3283{ 3284 if (pedantic) 3285 pedwarn ("ANSI C forbids use of %s expressions as lvalues", 3286 code == COND_EXPR ? "conditional" 3287 : code == COMPOUND_EXPR ? "compound" : "cast"); 3288} 3289 3290/* Warn about storing in something that is `const'. */ 3291 3292void 3293readonly_warning (arg, string) 3294 tree arg; 3295 char *string; 3296{ 3297 char buf[80]; 3298 strcpy (buf, string); 3299 3300 /* Forbid assignments to iterators. */ 3301 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg)) 3302 { 3303 strcat (buf, " of iterator `%s'"); 3304 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg))); 3305 } 3306 3307 if (TREE_CODE (arg) == COMPONENT_REF) 3308 { 3309 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) 3310 readonly_warning (TREE_OPERAND (arg, 0), string); 3311 else 3312 { 3313 strcat (buf, " of read-only member `%s'"); 3314 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1)))); 3315 } 3316 } 3317 else if (TREE_CODE (arg) == VAR_DECL) 3318 { 3319 strcat (buf, " of read-only variable `%s'"); 3320 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg))); 3321 } 3322 else 3323 { 3324 pedwarn ("%s of read-only location", buf); 3325 } 3326} 3327 3328/* Mark EXP saying that we need to be able to take the 3329 address of it; it should not be allocated in a register. 3330 Value is 1 if successful. */ 3331 3332int 3333mark_addressable (exp) 3334 tree exp; 3335{ 3336 register tree x = exp; 3337 while (1) 3338 switch (TREE_CODE (x)) 3339 { 3340 case COMPONENT_REF: 3341 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) 3342 { 3343 error ("cannot take address of bitfield `%s'", 3344 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1)))); 3345 return 0; 3346 } 3347 3348 /* ... fall through ... */ 3349 3350 case ADDR_EXPR: 3351 case ARRAY_REF: 3352 case REALPART_EXPR: 3353 case IMAGPART_EXPR: 3354 x = TREE_OPERAND (x, 0); 3355 break; 3356 3357 case CONSTRUCTOR: 3358 TREE_ADDRESSABLE (x) = 1; 3359 return 1; 3360 3361 case VAR_DECL: 3362 case CONST_DECL: 3363 case PARM_DECL: 3364 case RESULT_DECL: 3365 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) 3366 && DECL_NONLOCAL (x)) 3367 { 3368 if (TREE_PUBLIC (x)) 3369 { 3370 error ("global register variable `%s' used in nested function", 3371 IDENTIFIER_POINTER (DECL_NAME (x))); 3372 return 0; 3373 } 3374 pedwarn ("register variable `%s' used in nested function", 3375 IDENTIFIER_POINTER (DECL_NAME (x))); 3376 } 3377 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)) 3378 { 3379 if (TREE_PUBLIC (x)) 3380 { 3381 error ("address of global register variable `%s' requested", 3382 IDENTIFIER_POINTER (DECL_NAME (x))); 3383 return 0; 3384 } 3385 3386 /* If we are making this addressable due to its having 3387 volatile components, give a different error message. Also 3388 handle the case of an unnamed parameter by not trying 3389 to give the name. */ 3390 3391 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x))) 3392 { 3393 error ("cannot put object with volatile field into register"); 3394 return 0; 3395 } 3396 3397 pedwarn ("address of register variable `%s' requested", 3398 IDENTIFIER_POINTER (DECL_NAME (x))); 3399 } 3400 put_var_into_stack (x); 3401 3402 /* drops in */ 3403 case FUNCTION_DECL: 3404 TREE_ADDRESSABLE (x) = 1; 3405#if 0 /* poplevel deals with this now. */ 3406 if (DECL_CONTEXT (x) == 0) 3407 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1; 3408#endif 3409 3410 default: 3411 return 1; 3412 } 3413} 3414 3415/* Build and return a conditional expression IFEXP ? OP1 : OP2. */ 3416 3417tree 3418build_conditional_expr (ifexp, op1, op2) 3419 tree ifexp, op1, op2; 3420{ 3421 register tree type1; 3422 register tree type2; 3423 register enum tree_code code1; 3424 register enum tree_code code2; 3425 register tree result_type = NULL; 3426 tree orig_op1 = op1, orig_op2 = op2; 3427 3428 ifexp = truthvalue_conversion (default_conversion (ifexp)); 3429 3430#if 0 /* Produces wrong result if within sizeof. */ 3431 /* Don't promote the operands separately if they promote 3432 the same way. Return the unpromoted type and let the combined 3433 value get promoted if necessary. */ 3434 3435 if (TREE_TYPE (op1) == TREE_TYPE (op2) 3436 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE 3437 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE 3438 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE) 3439 { 3440 if (TREE_CODE (ifexp) == INTEGER_CST) 3441 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); 3442 3443 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2)); 3444 } 3445#endif 3446 3447 /* Promote both alternatives. */ 3448 3449 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) 3450 op1 = default_conversion (op1); 3451 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) 3452 op2 = default_conversion (op2); 3453 3454 if (TREE_CODE (ifexp) == ERROR_MARK 3455 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK 3456 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) 3457 return error_mark_node; 3458 3459 type1 = TREE_TYPE (op1); 3460 code1 = TREE_CODE (type1); 3461 type2 = TREE_TYPE (op2); 3462 code2 = TREE_CODE (type2); 3463 3464 /* Quickly detect the usual case where op1 and op2 have the same type 3465 after promotion. */ 3466 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) 3467 { 3468 if (type1 == type2) 3469 result_type = type1; 3470 else 3471 result_type = TYPE_MAIN_VARIANT (type1); 3472 } 3473 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE) 3474 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE)) 3475 { 3476 result_type = common_type (type1, type2); 3477 } 3478 else if (code1 == VOID_TYPE || code2 == VOID_TYPE) 3479 { 3480 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE)) 3481 pedwarn ("ANSI C forbids conditional expr with only one void side"); 3482 result_type = void_type_node; 3483 } 3484 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) 3485 { 3486 if (comp_target_types (type1, type2)) 3487 result_type = common_type (type1, type2); 3488 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node 3489 && TREE_CODE (orig_op1) != NOP_EXPR) 3490 result_type = qualify_type (type2, type1); 3491 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node 3492 && TREE_CODE (orig_op2) != NOP_EXPR) 3493 result_type = qualify_type (type1, type2); 3494 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node) 3495 { 3496 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) 3497 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer"); 3498 result_type = qualify_type (type1, type2); 3499 } 3500 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node) 3501 { 3502 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) 3503 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer"); 3504 result_type = qualify_type (type2, type1); 3505 } 3506 else 3507 { 3508 pedwarn ("pointer type mismatch in conditional expression"); 3509 result_type = build_pointer_type (void_type_node); 3510 } 3511 } 3512 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) 3513 { 3514 if (! integer_zerop (op2)) 3515 pedwarn ("pointer/integer type mismatch in conditional expression"); 3516 else 3517 { 3518 op2 = null_pointer_node; 3519#if 0 /* The spec seems to say this is permitted. */ 3520 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE) 3521 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer"); 3522#endif 3523 } 3524 result_type = type1; 3525 } 3526 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) 3527 { 3528 if (!integer_zerop (op1)) 3529 pedwarn ("pointer/integer type mismatch in conditional expression"); 3530 else 3531 { 3532 op1 = null_pointer_node; 3533#if 0 /* The spec seems to say this is permitted. */ 3534 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE) 3535 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer"); 3536#endif 3537 } 3538 result_type = type2; 3539 } 3540 3541 if (!result_type) 3542 { 3543 if (flag_cond_mismatch) 3544 result_type = void_type_node; 3545 else 3546 { 3547 error ("type mismatch in conditional expression"); 3548 return error_mark_node; 3549 } 3550 } 3551 3552 /* Merge const and volatile flags of the incoming types. */ 3553 result_type 3554 = build_type_variant (result_type, 3555 TREE_READONLY (op1) || TREE_READONLY (op2), 3556 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2)); 3557 3558 if (result_type != TREE_TYPE (op1)) 3559 op1 = convert_and_check (result_type, op1); 3560 if (result_type != TREE_TYPE (op2)) 3561 op2 = convert_and_check (result_type, op2); 3562 3563#if 0 3564 if (code1 == RECORD_TYPE || code1 == UNION_TYPE) 3565 { 3566 result_type = TREE_TYPE (op1); 3567 if (TREE_CONSTANT (ifexp)) 3568 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); 3569 3570 if (TYPE_MODE (result_type) == BLKmode) 3571 { 3572 register tree tempvar 3573 = build_decl (VAR_DECL, NULL_TREE, result_type); 3574 register tree xop1 = build_modify_expr (tempvar, op1); 3575 register tree xop2 = build_modify_expr (tempvar, op2); 3576 register tree result = fold (build (COND_EXPR, result_type, 3577 ifexp, xop1, xop2)); 3578 3579 layout_decl (tempvar, TYPE_ALIGN (result_type)); 3580 /* No way to handle variable-sized objects here. 3581 I fear that the entire handling of BLKmode conditional exprs 3582 needs to be redone. */ 3583 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST) 3584 abort (); 3585 DECL_RTL (tempvar) 3586 = assign_stack_local (DECL_MODE (tempvar), 3587 (TREE_INT_CST_LOW (DECL_SIZE (tempvar)) 3588 + BITS_PER_UNIT - 1) 3589 / BITS_PER_UNIT, 3590 0); 3591 3592 TREE_SIDE_EFFECTS (result) 3593 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1) 3594 | TREE_SIDE_EFFECTS (op2); 3595 return build (COMPOUND_EXPR, result_type, result, tempvar); 3596 } 3597 } 3598#endif /* 0 */ 3599 3600 if (TREE_CODE (ifexp) == INTEGER_CST) 3601 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1); 3602 3603 return fold (build (COND_EXPR, result_type, ifexp, op1, op2)); 3604} 3605 3606/* Given a list of expressions, return a compound expression 3607 that performs them all and returns the value of the last of them. */ 3608 3609tree 3610build_compound_expr (list) 3611 tree list; 3612{ 3613 return internal_build_compound_expr (list, TRUE); 3614} 3615 3616static tree 3617internal_build_compound_expr (list, first_p) 3618 tree list; 3619 int first_p; 3620{ 3621 register tree rest; 3622 3623 if (TREE_CHAIN (list) == 0) 3624 { 3625#if 0 /* If something inside inhibited lvalueness, we should not override. */ 3626 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */ 3627 3628 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 3629 if (TREE_CODE (list) == NON_LVALUE_EXPR) 3630 list = TREE_OPERAND (list, 0); 3631#endif 3632 3633 /* Don't let (0, 0) be null pointer constant. */ 3634 if (!first_p && integer_zerop (TREE_VALUE (list))) 3635 return non_lvalue (TREE_VALUE (list)); 3636 return TREE_VALUE (list); 3637 } 3638 3639 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0) 3640 { 3641 /* Convert arrays to pointers when there really is a comma operator. */ 3642 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE) 3643 TREE_VALUE (TREE_CHAIN (list)) 3644 = default_conversion (TREE_VALUE (TREE_CHAIN (list))); 3645 } 3646 3647 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE); 3648 3649 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list))) 3650 { 3651 /* The left-hand operand of a comma expression is like an expression 3652 statement: with -W or -Wunused, we should warn if it doesn't have 3653 any side-effects, unless it was explicitly cast to (void). */ 3654 if ((extra_warnings || warn_unused) 3655 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR 3656 && TREE_TYPE (TREE_VALUE (list)) == void_type_node)) 3657 warning ("left-hand operand of comma expression has no effect"); 3658 3659 /* When pedantic, a compound expression can be neither an lvalue 3660 nor an integer constant expression. */ 3661 if (! pedantic) 3662 return rest; 3663 } 3664 3665 /* With -Wunused, we should also warn if the left-hand operand does have 3666 side-effects, but computes a value which is not used. For example, in 3667 `foo() + bar(), baz()' the result of the `+' operator is not used, 3668 so we should issue a warning. */ 3669 else if (warn_unused) 3670 warn_if_unused_value (TREE_VALUE (list)); 3671 3672 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest); 3673} 3674 3675/* Build an expression representing a cast to type TYPE of expression EXPR. */ 3676 3677tree 3678build_c_cast (type, expr) 3679 register tree type; 3680 tree expr; 3681{ 3682 register tree value = expr; 3683 3684 if (type == error_mark_node || expr == error_mark_node) 3685 return error_mark_node; 3686 type = TYPE_MAIN_VARIANT (type); 3687 3688#if 0 3689 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 3690 if (TREE_CODE (value) == NON_LVALUE_EXPR) 3691 value = TREE_OPERAND (value, 0); 3692#endif 3693 3694 if (TREE_CODE (type) == ARRAY_TYPE) 3695 { 3696 error ("cast specifies array type"); 3697 return error_mark_node; 3698 } 3699 3700 if (TREE_CODE (type) == FUNCTION_TYPE) 3701 { 3702 error ("cast specifies function type"); 3703 return error_mark_node; 3704 } 3705 3706 if (type == TREE_TYPE (value)) 3707 { 3708 if (pedantic) 3709 { 3710 if (TREE_CODE (type) == RECORD_TYPE 3711 || TREE_CODE (type) == UNION_TYPE) 3712 pedwarn ("ANSI C forbids casting nonscalar to the same type"); 3713 } 3714 } 3715 else if (TREE_CODE (type) == UNION_TYPE) 3716 { 3717 tree field; 3718 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE 3719 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE) 3720 value = default_conversion (value); 3721 3722 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 3723 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), 3724 TYPE_MAIN_VARIANT (TREE_TYPE (value)))) 3725 break; 3726 3727 if (field) 3728 { 3729 char *name; 3730 tree t; 3731 3732 if (pedantic) 3733 pedwarn ("ANSI C forbids casts to union type"); 3734 if (TYPE_NAME (type) != 0) 3735 { 3736 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) 3737 name = IDENTIFIER_POINTER (TYPE_NAME (type)); 3738 else 3739 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))); 3740 } 3741 else 3742 name = ""; 3743 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE, 3744 build_tree_list (field, value)), 3745 0, 0); 3746 TREE_CONSTANT (t) = TREE_CONSTANT (value); 3747 return t; 3748 } 3749 error ("cast to union type from type not present in union"); 3750 return error_mark_node; 3751 } 3752 else 3753 { 3754 tree otype, ovalue; 3755 3756 /* If casting to void, avoid the error that would come 3757 from default_conversion in the case of a non-lvalue array. */ 3758 if (type == void_type_node) 3759 return build1 (CONVERT_EXPR, type, value); 3760 3761 /* Convert functions and arrays to pointers, 3762 but don't convert any other types. */ 3763 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE 3764 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE) 3765 value = default_conversion (value); 3766 otype = TREE_TYPE (value); 3767 3768 /* Optionally warn about potentially worrisome casts. */ 3769 3770 if (warn_cast_qual 3771 && TREE_CODE (type) == POINTER_TYPE 3772 && TREE_CODE (otype) == POINTER_TYPE) 3773 { 3774 /* Go to the innermost object being pointed to. */ 3775 tree in_type = type; 3776 tree in_otype = otype; 3777 3778 while (TREE_CODE (in_type) == POINTER_TYPE) 3779 in_type = TREE_TYPE (in_type); 3780 while (TREE_CODE (in_otype) == POINTER_TYPE) 3781 in_otype = TREE_TYPE (in_otype); 3782 3783 if (TYPE_VOLATILE (in_otype) && ! TYPE_VOLATILE (in_type)) 3784 pedwarn ("cast discards `volatile' from pointer target type"); 3785 if (TYPE_READONLY (in_otype) && ! TYPE_READONLY (in_type)) 3786 pedwarn ("cast discards `const' from pointer target type"); 3787 } 3788 3789 /* Warn about possible alignment problems. */ 3790 if (STRICT_ALIGNMENT && warn_cast_align 3791 && TREE_CODE (type) == POINTER_TYPE 3792 && TREE_CODE (otype) == POINTER_TYPE 3793 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE 3794 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE 3795 /* Don't warn about opaque types, where the actual alignment 3796 restriction is unknown. */ 3797 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE 3798 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) 3799 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) 3800 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) 3801 warning ("cast increases required alignment of target type"); 3802 3803 if (TREE_CODE (type) == INTEGER_TYPE 3804 && TREE_CODE (otype) == POINTER_TYPE 3805 && TYPE_PRECISION (type) != TYPE_PRECISION (otype) 3806 && !TREE_CONSTANT (value)) 3807 warning ("cast from pointer to integer of different size"); 3808 3809 if (warn_bad_function_cast 3810 && TREE_CODE (value) == CALL_EXPR 3811 && TREE_CODE (type) != TREE_CODE (otype)) 3812 warning ("cast does not match function type"); 3813 3814 if (TREE_CODE (type) == POINTER_TYPE 3815 && TREE_CODE (otype) == INTEGER_TYPE 3816 && TYPE_PRECISION (type) != TYPE_PRECISION (otype) 3817#if 0 3818 /* Don't warn about converting 0 to pointer, 3819 provided the 0 was explicit--not cast or made by folding. */ 3820 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)) 3821#endif 3822 /* Don't warn about converting any constant. */ 3823 && !TREE_CONSTANT (value)) 3824 warning ("cast to pointer from integer of different size"); 3825 3826 ovalue = value; 3827 value = convert (type, value); 3828 3829 /* Ignore any integer overflow caused by the cast. */ 3830 if (TREE_CODE (value) == INTEGER_CST) 3831 { 3832 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); 3833 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue); 3834 } 3835 } 3836 3837 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */ 3838 if (pedantic && TREE_CODE (value) == INTEGER_CST 3839 && TREE_CODE (expr) == INTEGER_CST 3840 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE) 3841 value = non_lvalue (value); 3842 3843 /* If pedantic, don't let a cast be an lvalue. */ 3844 if (value == expr && pedantic) 3845 value = non_lvalue (value); 3846 3847 return value; 3848} 3849 3850/* Build an assignment expression of lvalue LHS from value RHS. 3851 MODIFYCODE is the code for a binary operator that we use 3852 to combine the old value of LHS with RHS to get the new value. 3853 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */ 3854 3855tree 3856build_modify_expr (lhs, modifycode, rhs) 3857 tree lhs, rhs; 3858 enum tree_code modifycode; 3859{ 3860 register tree result; 3861 tree newrhs; 3862 tree lhstype = TREE_TYPE (lhs); 3863 tree olhstype = lhstype; 3864 3865 /* Types that aren't fully specified cannot be used in assignments. */ 3866 lhs = require_complete_type (lhs); 3867 3868 /* Avoid duplicate error messages from operands that had errors. */ 3869 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) 3870 return error_mark_node; 3871 3872 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 3873 /* Do not use STRIP_NOPS here. We do not want an enumerator 3874 whose value is 0 to count as a null pointer constant. */ 3875 if (TREE_CODE (rhs) == NON_LVALUE_EXPR) 3876 rhs = TREE_OPERAND (rhs, 0); 3877 3878 newrhs = rhs; 3879 3880 /* Handle control structure constructs used as "lvalues". */ 3881 3882 switch (TREE_CODE (lhs)) 3883 { 3884 /* Handle (a, b) used as an "lvalue". */ 3885 case COMPOUND_EXPR: 3886 pedantic_lvalue_warning (COMPOUND_EXPR); 3887 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), 3888 modifycode, rhs); 3889 if (TREE_CODE (newrhs) == ERROR_MARK) 3890 return error_mark_node; 3891 return build (COMPOUND_EXPR, lhstype, 3892 TREE_OPERAND (lhs, 0), newrhs); 3893 3894 /* Handle (a ? b : c) used as an "lvalue". */ 3895 case COND_EXPR: 3896 pedantic_lvalue_warning (COND_EXPR); 3897 rhs = save_expr (rhs); 3898 { 3899 /* Produce (a ? (b = rhs) : (c = rhs)) 3900 except that the RHS goes through a save-expr 3901 so the code to compute it is only emitted once. */ 3902 tree cond 3903 = build_conditional_expr (TREE_OPERAND (lhs, 0), 3904 build_modify_expr (TREE_OPERAND (lhs, 1), 3905 modifycode, rhs), 3906 build_modify_expr (TREE_OPERAND (lhs, 2), 3907 modifycode, rhs)); 3908 if (TREE_CODE (cond) == ERROR_MARK) 3909 return cond; 3910 /* Make sure the code to compute the rhs comes out 3911 before the split. */ 3912 return build (COMPOUND_EXPR, TREE_TYPE (lhs), 3913 /* But cast it to void to avoid an "unused" error. */ 3914 convert (void_type_node, rhs), cond); 3915 } 3916 default: 3917 break; 3918 } 3919 3920 /* If a binary op has been requested, combine the old LHS value with the RHS 3921 producing the value we should actually store into the LHS. */ 3922 3923 if (modifycode != NOP_EXPR) 3924 { 3925 lhs = stabilize_reference (lhs); 3926 newrhs = build_binary_op (modifycode, lhs, rhs, 1); 3927 } 3928 3929 /* Handle a cast used as an "lvalue". 3930 We have already performed any binary operator using the value as cast. 3931 Now convert the result to the cast type of the lhs, 3932 and then true type of the lhs and store it there; 3933 then convert result back to the cast type to be the value 3934 of the assignment. */ 3935 3936 switch (TREE_CODE (lhs)) 3937 { 3938 case NOP_EXPR: 3939 case CONVERT_EXPR: 3940 case FLOAT_EXPR: 3941 case FIX_TRUNC_EXPR: 3942 case FIX_FLOOR_EXPR: 3943 case FIX_ROUND_EXPR: 3944 case FIX_CEIL_EXPR: 3945 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE 3946 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE) 3947 newrhs = default_conversion (newrhs); 3948 { 3949 tree inner_lhs = TREE_OPERAND (lhs, 0); 3950 tree result; 3951 result = build_modify_expr (inner_lhs, NOP_EXPR, 3952 convert (TREE_TYPE (inner_lhs), 3953 convert (lhstype, newrhs))); 3954 if (TREE_CODE (result) == ERROR_MARK) 3955 return result; 3956 pedantic_lvalue_warning (CONVERT_EXPR); 3957 return convert (TREE_TYPE (lhs), result); 3958 } 3959 3960 default: 3961 break; 3962 } 3963 3964 /* Now we have handled acceptable kinds of LHS that are not truly lvalues. 3965 Reject anything strange now. */ 3966 3967 if (!lvalue_or_else (lhs, "assignment")) 3968 return error_mark_node; 3969 3970 /* Warn about storing in something that is `const'. */ 3971 3972 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype) 3973 || ((TREE_CODE (lhstype) == RECORD_TYPE 3974 || TREE_CODE (lhstype) == UNION_TYPE) 3975 && C_TYPE_FIELDS_READONLY (lhstype))) 3976 readonly_warning (lhs, "assignment"); 3977 3978 /* If storing into a structure or union member, 3979 it has probably been given type `int'. 3980 Compute the type that would go with 3981 the actual amount of storage the member occupies. */ 3982 3983 if (TREE_CODE (lhs) == COMPONENT_REF 3984 && (TREE_CODE (lhstype) == INTEGER_TYPE 3985 || TREE_CODE (lhstype) == REAL_TYPE 3986 || TREE_CODE (lhstype) == ENUMERAL_TYPE)) 3987 lhstype = TREE_TYPE (get_unwidened (lhs, 0)); 3988 3989 /* If storing in a field that is in actuality a short or narrower than one, 3990 we must store in the field in its actual type. */ 3991 3992 if (lhstype != TREE_TYPE (lhs)) 3993 { 3994 lhs = copy_node (lhs); 3995 TREE_TYPE (lhs) = lhstype; 3996 } 3997 3998 /* Convert new value to destination type. */ 3999 4000 newrhs = convert_for_assignment (lhstype, newrhs, "assignment", 4001 NULL_TREE, NULL_TREE, 0); 4002 if (TREE_CODE (newrhs) == ERROR_MARK) 4003 return error_mark_node; 4004 4005 result = build (MODIFY_EXPR, lhstype, lhs, newrhs); 4006 TREE_SIDE_EFFECTS (result) = 1; 4007 4008 /* If we got the LHS in a different type for storing in, 4009 convert the result back to the nominal type of LHS 4010 so that the value we return always has the same type 4011 as the LHS argument. */ 4012 4013 if (olhstype == TREE_TYPE (result)) 4014 return result; 4015 return convert_for_assignment (olhstype, result, "assignment", 4016 NULL_TREE, NULL_TREE, 0); 4017} 4018 4019/* Convert value RHS to type TYPE as preparation for an assignment 4020 to an lvalue of type TYPE. 4021 The real work of conversion is done by `convert'. 4022 The purpose of this function is to generate error messages 4023 for assignments that are not allowed in C. 4024 ERRTYPE is a string to use in error messages: 4025 "assignment", "return", etc. If it is null, this is parameter passing 4026 for a function call (and different error messages are output). Otherwise, 4027 it may be a name stored in the spelling stack and interpreted by 4028 get_spelling. 4029 4030 FUNNAME is the name of the function being called, 4031 as an IDENTIFIER_NODE, or null. 4032 PARMNUM is the number of the argument, for printing in error messages. */ 4033 4034static tree 4035convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum) 4036 tree type, rhs; 4037 char *errtype; 4038 tree fundecl, funname; 4039 int parmnum; 4040{ 4041 register enum tree_code codel = TREE_CODE (type); 4042 register tree rhstype; 4043 register enum tree_code coder; 4044 4045 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 4046 /* Do not use STRIP_NOPS here. We do not want an enumerator 4047 whose value is 0 to count as a null pointer constant. */ 4048 if (TREE_CODE (rhs) == NON_LVALUE_EXPR) 4049 rhs = TREE_OPERAND (rhs, 0); 4050 4051 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE 4052 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE) 4053 rhs = default_conversion (rhs); 4054 else if (optimize && TREE_CODE (rhs) == VAR_DECL) 4055 rhs = decl_constant_value (rhs); 4056 4057 rhstype = TREE_TYPE (rhs); 4058 coder = TREE_CODE (rhstype); 4059 4060 if (coder == ERROR_MARK) 4061 return error_mark_node; 4062 4063 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) 4064 { 4065 overflow_warning (rhs); 4066 /* Check for Objective-C protocols. This will issue a warning if 4067 there are protocol violations. No need to use the return value. */ 4068 maybe_objc_comptypes (type, rhstype, 0); 4069 return rhs; 4070 } 4071 4072 if (coder == VOID_TYPE) 4073 { 4074 error ("void value not ignored as it ought to be"); 4075 return error_mark_node; 4076 } 4077 /* Arithmetic types all interconvert, and enum is treated like int. */ 4078 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE 4079 || codel == COMPLEX_TYPE) 4080 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE 4081 || coder == COMPLEX_TYPE)) 4082 return convert_and_check (type, rhs); 4083 4084 /* Conversion to a transparent union from its member types. 4085 This applies only to function arguments. */ 4086 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype) 4087 { 4088 tree memb_types; 4089 tree marginal_memb_type = 0; 4090 4091 for (memb_types = TYPE_FIELDS (type); memb_types; 4092 memb_types = TREE_CHAIN (memb_types)) 4093 { 4094 tree memb_type = TREE_TYPE (memb_types); 4095 4096 if (comptypes (TYPE_MAIN_VARIANT (memb_type), 4097 TYPE_MAIN_VARIANT (rhstype))) 4098 break; 4099 4100 if (TREE_CODE (memb_type) != POINTER_TYPE) 4101 continue; 4102 4103 if (coder == POINTER_TYPE) 4104 { 4105 register tree ttl = TREE_TYPE (memb_type); 4106 register tree ttr = TREE_TYPE (rhstype); 4107 4108 /* Any non-function converts to a [const][volatile] void * 4109 and vice versa; otherwise, targets must be the same. 4110 Meanwhile, the lhs target must have all the qualifiers of 4111 the rhs. */ 4112 if (TYPE_MAIN_VARIANT (ttl) == void_type_node 4113 || TYPE_MAIN_VARIANT (ttr) == void_type_node 4114 || comp_target_types (memb_type, rhstype)) 4115 { 4116 /* If this type won't generate any warnings, use it. */ 4117 if ((TREE_CODE (ttr) == FUNCTION_TYPE 4118 && TREE_CODE (ttl) == FUNCTION_TYPE) 4119 ? ((! TYPE_READONLY (ttl) | TYPE_READONLY (ttr)) 4120 & (! TYPE_VOLATILE (ttl) | TYPE_VOLATILE (ttr))) 4121 : ((TYPE_READONLY (ttl) | ! TYPE_READONLY (ttr)) 4122 & (TYPE_VOLATILE (ttl) | ! TYPE_VOLATILE (ttr)))) 4123 break; 4124 4125 /* Keep looking for a better type, but remember this one. */ 4126 if (! marginal_memb_type) 4127 marginal_memb_type = memb_type; 4128 } 4129 } 4130 4131 /* Can convert integer zero to any pointer type. */ 4132 if (integer_zerop (rhs) 4133 || (TREE_CODE (rhs) == NOP_EXPR 4134 && integer_zerop (TREE_OPERAND (rhs, 0)))) 4135 { 4136 rhs = null_pointer_node; 4137 break; 4138 } 4139 } 4140 4141 if (memb_types || marginal_memb_type) 4142 { 4143 if (! memb_types) 4144 { 4145 /* We have only a marginally acceptable member type; 4146 it needs a warning. */ 4147 register tree ttl = TREE_TYPE (marginal_memb_type); 4148 register tree ttr = TREE_TYPE (rhstype); 4149 4150 /* Const and volatile mean something different for function 4151 types, so the usual warnings are not appropriate. */ 4152 if (TREE_CODE (ttr) == FUNCTION_TYPE 4153 && TREE_CODE (ttl) == FUNCTION_TYPE) 4154 { 4155 /* Because const and volatile on functions are 4156 restrictions that say the function will not do 4157 certain things, it is okay to use a const or volatile 4158 function where an ordinary one is wanted, but not 4159 vice-versa. */ 4160 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr)) 4161 warn_for_assignment ("%s makes `const *' function pointer from non-const", 4162 get_spelling (errtype), funname, 4163 parmnum); 4164 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr)) 4165 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile", 4166 get_spelling (errtype), funname, 4167 parmnum); 4168 } 4169 else 4170 { 4171 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) 4172 warn_for_assignment ("%s discards `const' from pointer target type", 4173 get_spelling (errtype), funname, 4174 parmnum); 4175 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) 4176 warn_for_assignment ("%s discards `volatile' from pointer target type", 4177 get_spelling (errtype), funname, 4178 parmnum); 4179 } 4180 } 4181 4182 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl)) 4183 pedwarn ("ANSI C prohibits argument conversion to union type"); 4184 4185 return build1 (NOP_EXPR, type, rhs); 4186 } 4187 } 4188 4189 /* Conversions among pointers */ 4190 else if (codel == POINTER_TYPE && coder == POINTER_TYPE) 4191 { 4192 register tree ttl = TREE_TYPE (type); 4193 register tree ttr = TREE_TYPE (rhstype); 4194 4195 /* Any non-function converts to a [const][volatile] void * 4196 and vice versa; otherwise, targets must be the same. 4197 Meanwhile, the lhs target must have all the qualifiers of the rhs. */ 4198 if (TYPE_MAIN_VARIANT (ttl) == void_type_node 4199 || TYPE_MAIN_VARIANT (ttr) == void_type_node 4200 || comp_target_types (type, rhstype) 4201 || (unsigned_type (TYPE_MAIN_VARIANT (ttl)) 4202 == unsigned_type (TYPE_MAIN_VARIANT (ttr)))) 4203 { 4204 if (pedantic 4205 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node 4206 && TREE_CODE (ttr) == FUNCTION_TYPE) 4207 || 4208 (TYPE_MAIN_VARIANT (ttr) == void_type_node 4209 /* Check TREE_CODE to catch cases like (void *) (char *) 0 4210 which are not ANSI null ptr constants. */ 4211 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR) 4212 && TREE_CODE (ttl) == FUNCTION_TYPE))) 4213 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'", 4214 get_spelling (errtype), funname, parmnum); 4215 /* Const and volatile mean something different for function types, 4216 so the usual warnings are not appropriate. */ 4217 else if (TREE_CODE (ttr) != FUNCTION_TYPE 4218 && TREE_CODE (ttl) != FUNCTION_TYPE) 4219 { 4220 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr)) 4221 warn_for_assignment ("%s discards `const' from pointer target type", 4222 get_spelling (errtype), funname, parmnum); 4223 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)) 4224 warn_for_assignment ("%s discards `volatile' from pointer target type", 4225 get_spelling (errtype), funname, parmnum); 4226 /* If this is not a case of ignoring a mismatch in signedness, 4227 no warning. */ 4228 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node 4229 || TYPE_MAIN_VARIANT (ttr) == void_type_node 4230 || comp_target_types (type, rhstype)) 4231 ; 4232 /* If there is a mismatch, do warn. */ 4233 else if (pedantic) 4234 warn_for_assignment ("pointer targets in %s differ in signedness", 4235 get_spelling (errtype), funname, parmnum); 4236 } 4237 else if (TREE_CODE (ttl) == FUNCTION_TYPE 4238 && TREE_CODE (ttr) == FUNCTION_TYPE) 4239 { 4240 /* Because const and volatile on functions are restrictions 4241 that say the function will not do certain things, 4242 it is okay to use a const or volatile function 4243 where an ordinary one is wanted, but not vice-versa. */ 4244 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr)) 4245 warn_for_assignment ("%s makes `const *' function pointer from non-const", 4246 get_spelling (errtype), funname, parmnum); 4247 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr)) 4248 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile", 4249 get_spelling (errtype), funname, parmnum); 4250 } 4251 } 4252 else 4253 warn_for_assignment ("%s from incompatible pointer type", 4254 get_spelling (errtype), funname, parmnum); 4255 return convert (type, rhs); 4256 } 4257 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) 4258 { 4259 /* An explicit constant 0 can convert to a pointer, 4260 or one that results from arithmetic, even including 4261 a cast to integer type. */ 4262 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)) 4263 && 4264 ! (TREE_CODE (rhs) == NOP_EXPR 4265 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE 4266 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST 4267 && integer_zerop (TREE_OPERAND (rhs, 0)))) 4268 { 4269 warn_for_assignment ("%s makes pointer from integer without a cast", 4270 get_spelling (errtype), funname, parmnum); 4271 return convert (type, rhs); 4272 } 4273 return null_pointer_node; 4274 } 4275 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) 4276 { 4277 warn_for_assignment ("%s makes integer from pointer without a cast", 4278 get_spelling (errtype), funname, parmnum); 4279 return convert (type, rhs); 4280 } 4281 4282 if (!errtype) 4283 { 4284 if (funname) 4285 { 4286 tree selector = maybe_building_objc_message_expr (); 4287 4288 if (selector && parmnum > 2) 4289 error ("incompatible type for argument %d of `%s'", 4290 parmnum - 2, IDENTIFIER_POINTER (selector)); 4291 else 4292 error ("incompatible type for argument %d of `%s'", 4293 parmnum, IDENTIFIER_POINTER (funname)); 4294 } 4295 else 4296 error ("incompatible type for argument %d of indirect function call", 4297 parmnum); 4298 } 4299 else 4300 error ("incompatible types in %s", get_spelling (errtype)); 4301 4302 return error_mark_node; 4303} 4304 4305/* Print a warning using MSG. 4306 It gets OPNAME as its one parameter. 4307 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'". 4308 FUNCTION and ARGNUM are handled specially if we are building an 4309 Objective-C selector. */ 4310 4311static void 4312warn_for_assignment (msg, opname, function, argnum) 4313 char *msg; 4314 char *opname; 4315 tree function; 4316 int argnum; 4317{ 4318 static char argstring[] = "passing arg %d of `%s'"; 4319 static char argnofun[] = "passing arg %d"; 4320 4321 if (opname == 0) 4322 { 4323 tree selector = maybe_building_objc_message_expr (); 4324 4325 if (selector && argnum > 2) 4326 { 4327 function = selector; 4328 argnum -= 2; 4329 } 4330 if (function) 4331 { 4332 /* Function name is known; supply it. */ 4333 opname = (char *) alloca (IDENTIFIER_LENGTH (function) 4334 + sizeof (argstring) + 25 /*%d*/ + 1); 4335 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function)); 4336 } 4337 else 4338 { 4339 /* Function name unknown (call through ptr); just give arg number. */ 4340 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1); 4341 sprintf (opname, argnofun, argnum); 4342 } 4343 } 4344 pedwarn (msg, opname); 4345} 4346 4347/* Return nonzero if VALUE is a valid constant-valued expression 4348 for use in initializing a static variable; one that can be an 4349 element of a "constant" initializer. 4350 4351 Return null_pointer_node if the value is absolute; 4352 if it is relocatable, return the variable that determines the relocation. 4353 We assume that VALUE has been folded as much as possible; 4354 therefore, we do not need to check for such things as 4355 arithmetic-combinations of integers. */ 4356 4357tree 4358initializer_constant_valid_p (value, endtype) 4359 tree value; 4360 tree endtype; 4361{ 4362 switch (TREE_CODE (value)) 4363 { 4364 case CONSTRUCTOR: 4365 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE 4366 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE) 4367 && TREE_CONSTANT (value) 4368 && CONSTRUCTOR_ELTS (value)) 4369 return 4370 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)), 4371 endtype); 4372 4373 return TREE_STATIC (value) ? null_pointer_node : 0; 4374 4375 case INTEGER_CST: 4376 case REAL_CST: 4377 case STRING_CST: 4378 case COMPLEX_CST: 4379 return null_pointer_node; 4380 4381 case ADDR_EXPR: 4382 return TREE_OPERAND (value, 0); 4383 4384 case NON_LVALUE_EXPR: 4385 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 4386 4387 case CONVERT_EXPR: 4388 case NOP_EXPR: 4389 /* Allow conversions between pointer types. */ 4390 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE 4391 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE) 4392 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 4393 4394 /* Allow conversions between real types. */ 4395 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE 4396 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE) 4397 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 4398 4399 /* Allow length-preserving conversions between integer types. */ 4400 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 4401 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE 4402 && (TYPE_PRECISION (TREE_TYPE (value)) 4403 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))) 4404 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype); 4405 4406 /* Allow conversions between other integer types only if 4407 explicit value. */ 4408 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 4409 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE) 4410 { 4411 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0), 4412 endtype); 4413 if (inner == null_pointer_node) 4414 return null_pointer_node; 4415 return 0; 4416 } 4417 4418 /* Allow (int) &foo provided int is as wide as a pointer. */ 4419 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 4420 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE 4421 && (TYPE_PRECISION (TREE_TYPE (value)) 4422 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))) 4423 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 4424 endtype); 4425 4426 /* Likewise conversions from int to pointers. */ 4427 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE 4428 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE 4429 && (TYPE_PRECISION (TREE_TYPE (value)) 4430 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))) 4431 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 4432 endtype); 4433 4434 /* Allow conversions to union types if the value inside is okay. */ 4435 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE) 4436 return initializer_constant_valid_p (TREE_OPERAND (value, 0), 4437 endtype); 4438 return 0; 4439 4440 case PLUS_EXPR: 4441 if (TREE_CODE (endtype) == INTEGER_TYPE 4442 && TYPE_PRECISION (endtype) < POINTER_SIZE) 4443 return 0; 4444 { 4445 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0), 4446 endtype); 4447 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1), 4448 endtype); 4449 /* If either term is absolute, use the other terms relocation. */ 4450 if (valid0 == null_pointer_node) 4451 return valid1; 4452 if (valid1 == null_pointer_node) 4453 return valid0; 4454 return 0; 4455 } 4456 4457 case MINUS_EXPR: 4458 if (TREE_CODE (endtype) == INTEGER_TYPE 4459 && TYPE_PRECISION (endtype) < POINTER_SIZE) 4460 return 0; 4461 { 4462 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0), 4463 endtype); 4464 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1), 4465 endtype); 4466 /* Win if second argument is absolute. */ 4467 if (valid1 == null_pointer_node) 4468 return valid0; 4469 /* Win if both arguments have the same relocation. 4470 Then the value is absolute. */ 4471 if (valid0 == valid1) 4472 return null_pointer_node; 4473 return 0; 4474 } 4475 4476 default: 4477 return 0; 4478 } 4479} 4480 4481/* If VALUE is a compound expr all of whose expressions are constant, then 4482 return its value. Otherwise, return error_mark_node. 4483 4484 This is for handling COMPOUND_EXPRs as initializer elements 4485 which is allowed with a warning when -pedantic is specified. */ 4486 4487static tree 4488valid_compound_expr_initializer (value, endtype) 4489 tree value; 4490 tree endtype; 4491{ 4492 if (TREE_CODE (value) == COMPOUND_EXPR) 4493 { 4494 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) 4495 == error_mark_node) 4496 return error_mark_node; 4497 return valid_compound_expr_initializer (TREE_OPERAND (value, 1), 4498 endtype); 4499 } 4500 else if (! TREE_CONSTANT (value) 4501 && ! initializer_constant_valid_p (value, endtype)) 4502 return error_mark_node; 4503 else 4504 return value; 4505} 4506 4507/* Perform appropriate conversions on the initial value of a variable, 4508 store it in the declaration DECL, 4509 and print any error messages that are appropriate. 4510 If the init is invalid, store an ERROR_MARK. */ 4511 4512void 4513store_init_value (decl, init) 4514 tree decl, init; 4515{ 4516 register tree value, type; 4517 4518 /* If variable's type was invalidly declared, just ignore it. */ 4519 4520 type = TREE_TYPE (decl); 4521 if (TREE_CODE (type) == ERROR_MARK) 4522 return; 4523 4524 /* Digest the specified initializer into an expression. */ 4525 4526 value = digest_init (type, init, TREE_STATIC (decl), 4527 TREE_STATIC (decl) || pedantic); 4528 4529 /* Store the expression if valid; else report error. */ 4530 4531#if 0 4532 /* Note that this is the only place we can detect the error 4533 in a case such as struct foo bar = (struct foo) { x, y }; 4534 where there is one initial value which is a constructor expression. */ 4535 if (value == error_mark_node) 4536 ; 4537 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value)) 4538 { 4539 error ("initializer for static variable is not constant"); 4540 value = error_mark_node; 4541 } 4542 else if (TREE_STATIC (decl) 4543 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) 4544 { 4545 error ("initializer for static variable uses complicated arithmetic"); 4546 value = error_mark_node; 4547 } 4548 else 4549 { 4550 if (pedantic && TREE_CODE (value) == CONSTRUCTOR) 4551 { 4552 if (! TREE_CONSTANT (value)) 4553 pedwarn ("aggregate initializer is not constant"); 4554 else if (! TREE_STATIC (value)) 4555 pedwarn ("aggregate initializer uses complicated arithmetic"); 4556 } 4557 } 4558#endif 4559 4560 DECL_INITIAL (decl) = value; 4561 4562 /* ANSI wants warnings about out-of-range constant initializers. */ 4563 STRIP_TYPE_NOPS (value); 4564 constant_expression_warning (value); 4565} 4566 4567/* Methods for storing and printing names for error messages. */ 4568 4569/* Implement a spelling stack that allows components of a name to be pushed 4570 and popped. Each element on the stack is this structure. */ 4571 4572struct spelling 4573{ 4574 int kind; 4575 union 4576 { 4577 int i; 4578 char *s; 4579 } u; 4580}; 4581 4582#define SPELLING_STRING 1 4583#define SPELLING_MEMBER 2 4584#define SPELLING_BOUNDS 3 4585 4586static struct spelling *spelling; /* Next stack element (unused). */ 4587static struct spelling *spelling_base; /* Spelling stack base. */ 4588static int spelling_size; /* Size of the spelling stack. */ 4589 4590/* Macros to save and restore the spelling stack around push_... functions. 4591 Alternative to SAVE_SPELLING_STACK. */ 4592 4593#define SPELLING_DEPTH() (spelling - spelling_base) 4594#define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth) 4595 4596/* Save and restore the spelling stack around arbitrary C code. */ 4597 4598#define SAVE_SPELLING_DEPTH(code) \ 4599{ \ 4600 int __depth = SPELLING_DEPTH (); \ 4601 code; \ 4602 RESTORE_SPELLING_DEPTH (__depth); \ 4603} 4604 4605/* Push an element on the spelling stack with type KIND and assign VALUE 4606 to MEMBER. */ 4607 4608#define PUSH_SPELLING(KIND, VALUE, MEMBER) \ 4609{ \ 4610 int depth = SPELLING_DEPTH (); \ 4611 \ 4612 if (depth >= spelling_size) \ 4613 { \ 4614 spelling_size += 10; \ 4615 if (spelling_base == 0) \ 4616 spelling_base \ 4617 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \ 4618 else \ 4619 spelling_base \ 4620 = (struct spelling *) xrealloc (spelling_base, \ 4621 spelling_size * sizeof (struct spelling)); \ 4622 RESTORE_SPELLING_DEPTH (depth); \ 4623 } \ 4624 \ 4625 spelling->kind = (KIND); \ 4626 spelling->MEMBER = (VALUE); \ 4627 spelling++; \ 4628} 4629 4630/* Push STRING on the stack. Printed literally. */ 4631 4632static void 4633push_string (string) 4634 char *string; 4635{ 4636 PUSH_SPELLING (SPELLING_STRING, string, u.s); 4637} 4638 4639/* Push a member name on the stack. Printed as '.' STRING. */ 4640 4641static void 4642push_member_name (decl) 4643 tree decl; 4644 4645{ 4646 char *string 4647 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>"; 4648 PUSH_SPELLING (SPELLING_MEMBER, string, u.s); 4649} 4650 4651/* Push an array bounds on the stack. Printed as [BOUNDS]. */ 4652 4653static void 4654push_array_bounds (bounds) 4655 int bounds; 4656{ 4657 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); 4658} 4659 4660/* Compute the maximum size in bytes of the printed spelling. */ 4661 4662static int 4663spelling_length () 4664{ 4665 register int size = 0; 4666 register struct spelling *p; 4667 4668 for (p = spelling_base; p < spelling; p++) 4669 { 4670 if (p->kind == SPELLING_BOUNDS) 4671 size += 25; 4672 else 4673 size += strlen (p->u.s) + 1; 4674 } 4675 4676 return size; 4677} 4678 4679/* Print the spelling to BUFFER and return it. */ 4680 4681static char * 4682print_spelling (buffer) 4683 register char *buffer; 4684{ 4685 register char *d = buffer; 4686 register char *s; 4687 register struct spelling *p; 4688 4689 for (p = spelling_base; p < spelling; p++) 4690 if (p->kind == SPELLING_BOUNDS) 4691 { 4692 sprintf (d, "[%d]", p->u.i); 4693 d += strlen (d); 4694 } 4695 else 4696 { 4697 if (p->kind == SPELLING_MEMBER) 4698 *d++ = '.'; 4699 for (s = p->u.s; (*d = *s++); d++) 4700 ; 4701 } 4702 *d++ = '\0'; 4703 return buffer; 4704} 4705 4706/* Provide a means to pass component names derived from the spelling stack. */ 4707 4708char initialization_message; 4709 4710/* Interpret the spelling of the given ERRTYPE message. */ 4711 4712static char * 4713get_spelling (errtype) 4714 char *errtype; 4715{ 4716 static char *buffer; 4717 static int size = -1; 4718 4719 if (errtype == &initialization_message) 4720 { 4721 /* Avoid counting chars */ 4722 static char message[] = "initialization of `%s'"; 4723 register int needed = sizeof (message) + spelling_length () + 1; 4724 char *temp; 4725 4726 if (size < 0) 4727 buffer = (char *) xmalloc (size = needed); 4728 if (needed > size) 4729 buffer = (char *) xrealloc (buffer, size = needed); 4730 4731 temp = (char *) alloca (needed); 4732 sprintf (buffer, message, print_spelling (temp)); 4733 return buffer; 4734 } 4735 4736 return errtype; 4737} 4738 4739/* Issue an error message for a bad initializer component. 4740 FORMAT describes the message. OFWHAT is the name for the component. 4741 LOCAL is a format string for formatting the insertion of the name 4742 into the message. 4743 4744 If OFWHAT is null, the component name is stored on the spelling stack. 4745 If the component name is a null string, then LOCAL is omitted entirely. */ 4746 4747void 4748error_init (format, local, ofwhat) 4749 char *format, *local, *ofwhat; 4750{ 4751 char *buffer; 4752 4753 if (ofwhat == 0) 4754 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 4755 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); 4756 4757 if (*ofwhat) 4758 sprintf (buffer, local, ofwhat); 4759 else 4760 buffer[0] = 0; 4761 4762 error (format, buffer); 4763} 4764 4765/* Issue a pedantic warning for a bad initializer component. 4766 FORMAT describes the message. OFWHAT is the name for the component. 4767 LOCAL is a format string for formatting the insertion of the name 4768 into the message. 4769 4770 If OFWHAT is null, the component name is stored on the spelling stack. 4771 If the component name is a null string, then LOCAL is omitted entirely. */ 4772 4773void 4774pedwarn_init (format, local, ofwhat) 4775 char *format, *local, *ofwhat; 4776{ 4777 char *buffer; 4778 4779 if (ofwhat == 0) 4780 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 4781 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); 4782 4783 if (*ofwhat) 4784 sprintf (buffer, local, ofwhat); 4785 else 4786 buffer[0] = 0; 4787 4788 pedwarn (format, buffer); 4789} 4790 4791/* Issue a warning for a bad initializer component. 4792 FORMAT describes the message. OFWHAT is the name for the component. 4793 LOCAL is a format string for formatting the insertion of the name 4794 into the message. 4795 4796 If OFWHAT is null, the component name is stored on the spelling stack. 4797 If the component name is a null string, then LOCAL is omitted entirely. */ 4798 4799static void 4800warning_init (format, local, ofwhat) 4801 char *format, *local, *ofwhat; 4802{ 4803 char *buffer; 4804 4805 if (ofwhat == 0) 4806 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); 4807 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2); 4808 4809 if (*ofwhat) 4810 sprintf (buffer, local, ofwhat); 4811 else 4812 buffer[0] = 0; 4813 4814 warning (format, buffer); 4815} 4816 4817/* Digest the parser output INIT as an initializer for type TYPE. 4818 Return a C expression of type TYPE to represent the initial value. 4819 4820 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors 4821 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT 4822 applies only to elements of constructors. */ 4823 4824static tree 4825digest_init (type, init, require_constant, constructor_constant) 4826 tree type, init; 4827 int require_constant, constructor_constant; 4828{ 4829 enum tree_code code = TREE_CODE (type); 4830 tree inside_init = init; 4831 4832 if (init == error_mark_node) 4833 return init; 4834 4835 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ 4836 /* Do not use STRIP_NOPS here. We do not want an enumerator 4837 whose value is 0 to count as a null pointer constant. */ 4838 if (TREE_CODE (init) == NON_LVALUE_EXPR) 4839 inside_init = TREE_OPERAND (init, 0); 4840 4841 /* Initialization of an array of chars from a string constant 4842 optionally enclosed in braces. */ 4843 4844 if (code == ARRAY_TYPE) 4845 { 4846 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); 4847 if ((typ1 == char_type_node 4848 || typ1 == signed_char_type_node 4849 || typ1 == unsigned_char_type_node 4850 || typ1 == unsigned_wchar_type_node 4851 || typ1 == signed_wchar_type_node) 4852 && ((inside_init && TREE_CODE (inside_init) == STRING_CST))) 4853 { 4854 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 4855 TYPE_MAIN_VARIANT (type))) 4856 return inside_init; 4857 4858 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) 4859 != char_type_node) 4860 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node)) 4861 { 4862 error_init ("char-array%s initialized from wide string", 4863 " `%s'", NULL); 4864 return error_mark_node; 4865 } 4866 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))) 4867 == char_type_node) 4868 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)) 4869 { 4870 error_init ("int-array%s initialized from non-wide string", 4871 " `%s'", NULL); 4872 return error_mark_node; 4873 } 4874 4875 TREE_TYPE (inside_init) = type; 4876 if (TYPE_DOMAIN (type) != 0 4877 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) 4878 { 4879 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); 4880 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; 4881 /* Subtract 1 (or sizeof (wchar_t)) 4882 because it's ok to ignore the terminating null char 4883 that is counted in the length of the constant. */ 4884 if (size < TREE_STRING_LENGTH (inside_init) 4885 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node) 4886 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT 4887 : 1)) 4888 pedwarn_init ( 4889 "initializer-string for array of chars%s is too long", 4890 " `%s'", NULL); 4891 } 4892 return inside_init; 4893 } 4894 } 4895 4896 /* Any type can be initialized 4897 from an expression of the same type, optionally with braces. */ 4898 4899 if (inside_init && TREE_TYPE (inside_init) != 0 4900 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), 4901 TYPE_MAIN_VARIANT (type)) 4902 || (code == ARRAY_TYPE 4903 && comptypes (TREE_TYPE (inside_init), type)) 4904 || (code == POINTER_TYPE 4905 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE 4906 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE) 4907 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), 4908 TREE_TYPE (type))))) 4909 { 4910 if (code == POINTER_TYPE 4911 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE 4912 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)) 4913 inside_init = default_conversion (inside_init); 4914 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST 4915 && TREE_CODE (inside_init) != CONSTRUCTOR) 4916 { 4917 error_init ("array%s initialized from non-constant array expression", 4918 " `%s'", NULL); 4919 return error_mark_node; 4920 } 4921 4922 if (optimize && TREE_CODE (inside_init) == VAR_DECL) 4923 inside_init = decl_constant_value (inside_init); 4924 4925 /* Compound expressions can only occur here if -pedantic or 4926 -pedantic-errors is specified. In the later case, we always want 4927 an error. In the former case, we simply want a warning. */ 4928 if (require_constant && pedantic 4929 && TREE_CODE (inside_init) == COMPOUND_EXPR) 4930 { 4931 inside_init 4932 = valid_compound_expr_initializer (inside_init, 4933 TREE_TYPE (inside_init)); 4934 if (inside_init == error_mark_node) 4935 error_init ("initializer element%s is not constant", 4936 " for `%s'", NULL); 4937 else 4938 pedwarn_init ("initializer element%s is not constant", 4939 " for `%s'", NULL); 4940 if (flag_pedantic_errors) 4941 inside_init = error_mark_node; 4942 } 4943 else if (require_constant && ! TREE_CONSTANT (inside_init)) 4944 { 4945 error_init ("initializer element%s is not constant", 4946 " for `%s'", NULL); 4947 inside_init = error_mark_node; 4948 } 4949 else if (require_constant 4950 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) 4951 { 4952 error_init ("initializer element%s is not computable at load time", 4953 " for `%s'", NULL); 4954 inside_init = error_mark_node; 4955 } 4956 4957 return inside_init; 4958 } 4959 4960 /* Handle scalar types, including conversions. */ 4961 4962 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE 4963 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE) 4964 { 4965 /* Note that convert_for_assignment calls default_conversion 4966 for arrays and functions. We must not call it in the 4967 case where inside_init is a null pointer constant. */ 4968 inside_init 4969 = convert_for_assignment (type, init, "initialization", 4970 NULL_TREE, NULL_TREE, 0); 4971 4972 if (require_constant && ! TREE_CONSTANT (inside_init)) 4973 { 4974 error_init ("initializer element%s is not constant", 4975 " for `%s'", NULL); 4976 inside_init = error_mark_node; 4977 } 4978 else if (require_constant 4979 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0) 4980 { 4981 error_init ("initializer element%s is not computable at load time", 4982 " for `%s'", NULL); 4983 inside_init = error_mark_node; 4984 } 4985 4986 return inside_init; 4987 } 4988 4989 /* Come here only for records and arrays. */ 4990 4991 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) 4992 { 4993 error_init ("variable-sized object%s may not be initialized", 4994 " `%s'", NULL); 4995 return error_mark_node; 4996 } 4997 4998 /* Traditionally, you can write struct foo x = 0; 4999 and it initializes the first element of x to 0. */ 5000 if (flag_traditional) 5001 { 5002 tree top = 0, prev = 0, otype = type; 5003 while (TREE_CODE (type) == RECORD_TYPE 5004 || TREE_CODE (type) == ARRAY_TYPE 5005 || TREE_CODE (type) == QUAL_UNION_TYPE 5006 || TREE_CODE (type) == UNION_TYPE) 5007 { 5008 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); 5009 if (prev == 0) 5010 top = temp; 5011 else 5012 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp); 5013 prev = temp; 5014 if (TREE_CODE (type) == ARRAY_TYPE) 5015 type = TREE_TYPE (type); 5016 else if (TYPE_FIELDS (type)) 5017 type = TREE_TYPE (TYPE_FIELDS (type)); 5018 else 5019 { 5020 error_init ("invalid initializer%s", " for `%s'", NULL); 5021 return error_mark_node; 5022 } 5023 } 5024 5025 if (otype != type) 5026 { 5027 TREE_OPERAND (prev, 1) 5028 = build_tree_list (NULL_TREE, 5029 digest_init (type, init, require_constant, 5030 constructor_constant)); 5031 return top; 5032 } 5033 else 5034 return error_mark_node; 5035 } 5036 error_init ("invalid initializer%s", " for `%s'", NULL); 5037 return error_mark_node; 5038} 5039 5040/* Handle initializers that use braces. */ 5041 5042/* Type of object we are accumulating a constructor for. 5043 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ 5044static tree constructor_type; 5045 5046/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields 5047 left to fill. */ 5048static tree constructor_fields; 5049 5050/* For an ARRAY_TYPE, this is the specified index 5051 at which to store the next element we get. 5052 This is a special INTEGER_CST node that we modify in place. */ 5053static tree constructor_index; 5054 5055/* For an ARRAY_TYPE, this is the end index of the range 5056 to initialize with the next element, or NULL in the ordinary case 5057 where the element is used just once. */ 5058static tree constructor_range_end; 5059 5060/* For an ARRAY_TYPE, this is the maximum index. */ 5061static tree constructor_max_index; 5062 5063/* For a RECORD_TYPE, this is the first field not yet written out. */ 5064static tree constructor_unfilled_fields; 5065 5066/* For an ARRAY_TYPE, this is the index of the first element 5067 not yet written out. 5068 This is a special INTEGER_CST node that we modify in place. */ 5069static tree constructor_unfilled_index; 5070 5071/* In a RECORD_TYPE, the byte index of the next consecutive field. 5072 This is so we can generate gaps between fields, when appropriate. 5073 This is a special INTEGER_CST node that we modify in place. */ 5074static tree constructor_bit_index; 5075 5076/* If we are saving up the elements rather than allocating them, 5077 this is the list of elements so far (in reverse order, 5078 most recent first). */ 5079static tree constructor_elements; 5080 5081/* 1 if so far this constructor's elements are all compile-time constants. */ 5082static int constructor_constant; 5083 5084/* 1 if so far this constructor's elements are all valid address constants. */ 5085static int constructor_simple; 5086 5087/* 1 if this constructor is erroneous so far. */ 5088static int constructor_erroneous; 5089 5090/* 1 if have called defer_addressed_constants. */ 5091static int constructor_subconstants_deferred; 5092 5093/* Structure for managing pending initializer elements, organized as an 5094 AVL tree. */ 5095 5096struct init_node 5097{ 5098 struct init_node *left, *right; 5099 struct init_node *parent; 5100 int balance; 5101 tree purpose; 5102 tree value; 5103}; 5104 5105/* Tree of pending elements at this constructor level. 5106 These are elements encountered out of order 5107 which belong at places we haven't reached yet in actually 5108 writing the output. */ 5109static struct init_node *constructor_pending_elts; 5110 5111/* The SPELLING_DEPTH of this constructor. */ 5112static int constructor_depth; 5113 5114/* 0 if implicitly pushing constructor levels is allowed. */ 5115int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */ 5116 5117static int require_constant_value; 5118static int require_constant_elements; 5119 5120/* 1 if it is ok to output this constructor as we read it. 5121 0 means must accumulate a CONSTRUCTOR expression. */ 5122static int constructor_incremental; 5123 5124/* DECL node for which an initializer is being read. 5125 0 means we are reading a constructor expression 5126 such as (struct foo) {...}. */ 5127static tree constructor_decl; 5128 5129/* start_init saves the ASMSPEC arg here for really_start_incremental_init. */ 5130static char *constructor_asmspec; 5131 5132/* Nonzero if this is an initializer for a top-level decl. */ 5133static int constructor_top_level; 5134 5135 5136/* This stack has a level for each implicit or explicit level of 5137 structuring in the initializer, including the outermost one. It 5138 saves the values of most of the variables above. */ 5139 5140struct constructor_stack 5141{ 5142 struct constructor_stack *next; 5143 tree type; 5144 tree fields; 5145 tree index; 5146 tree range_end; 5147 tree max_index; 5148 tree unfilled_index; 5149 tree unfilled_fields; 5150 tree bit_index; 5151 tree elements; 5152 int offset; 5153 struct init_node *pending_elts; 5154 int depth; 5155 /* If nonzero, this value should replace the entire 5156 constructor at this level. */ 5157 tree replacement_value; 5158 char constant; 5159 char simple; 5160 char implicit; 5161 char incremental; 5162 char erroneous; 5163 char outer; 5164}; 5165 5166struct constructor_stack *constructor_stack; 5167 5168/* This stack records separate initializers that are nested. 5169 Nested initializers can't happen in ANSI C, but GNU C allows them 5170 in cases like { ... (struct foo) { ... } ... }. */ 5171 5172struct initializer_stack 5173{ 5174 struct initializer_stack *next; 5175 tree decl; 5176 char *asmspec; 5177 struct constructor_stack *constructor_stack; 5178 tree elements; 5179 struct spelling *spelling; 5180 struct spelling *spelling_base; 5181 int spelling_size; 5182 char top_level; 5183 char incremental; 5184 char require_constant_value; 5185 char require_constant_elements; 5186 char deferred; 5187}; 5188 5189struct initializer_stack *initializer_stack; 5190 5191/* Prepare to parse and output the initializer for variable DECL. */ 5192 5193void 5194start_init (decl, asmspec_tree, top_level) 5195 tree decl; 5196 tree asmspec_tree; 5197 int top_level; 5198{ 5199 char *locus; 5200 struct initializer_stack *p 5201 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack)); 5202 char *asmspec = 0; 5203 5204 if (asmspec_tree) 5205 asmspec = TREE_STRING_POINTER (asmspec_tree); 5206 5207 p->decl = constructor_decl; 5208 p->asmspec = constructor_asmspec; 5209 p->incremental = constructor_incremental; 5210 p->require_constant_value = require_constant_value; 5211 p->require_constant_elements = require_constant_elements; 5212 p->constructor_stack = constructor_stack; 5213 p->elements = constructor_elements; 5214 p->spelling = spelling; 5215 p->spelling_base = spelling_base; 5216 p->spelling_size = spelling_size; 5217 p->deferred = constructor_subconstants_deferred; 5218 p->top_level = constructor_top_level; 5219 p->next = initializer_stack; 5220 initializer_stack = p; 5221 5222 constructor_decl = decl; 5223 constructor_incremental = top_level; 5224 constructor_asmspec = asmspec; 5225 constructor_subconstants_deferred = 0; 5226 constructor_top_level = top_level; 5227 5228 if (decl != 0) 5229 { 5230 require_constant_value = TREE_STATIC (decl); 5231 require_constant_elements 5232 = ((TREE_STATIC (decl) || pedantic) 5233 /* For a scalar, you can always use any value to initialize, 5234 even within braces. */ 5235 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE 5236 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE 5237 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE 5238 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); 5239 locus = IDENTIFIER_POINTER (DECL_NAME (decl)); 5240 constructor_incremental |= TREE_STATIC (decl); 5241 } 5242 else 5243 { 5244 require_constant_value = 0; 5245 require_constant_elements = 0; 5246 locus = "(anonymous)"; 5247 } 5248 5249 constructor_stack = 0; 5250 5251 missing_braces_mentioned = 0; 5252 5253 spelling_base = 0; 5254 spelling_size = 0; 5255 RESTORE_SPELLING_DEPTH (0); 5256 5257 if (locus) 5258 push_string (locus); 5259} 5260 5261void 5262finish_init () 5263{ 5264 struct initializer_stack *p = initializer_stack; 5265 5266 /* Output subconstants (string constants, usually) 5267 that were referenced within this initializer and saved up. 5268 Must do this if and only if we called defer_addressed_constants. */ 5269 if (constructor_subconstants_deferred) 5270 output_deferred_addressed_constants (); 5271 5272 /* Free the whole constructor stack of this initializer. */ 5273 while (constructor_stack) 5274 { 5275 struct constructor_stack *q = constructor_stack; 5276 constructor_stack = q->next; 5277 free (q); 5278 } 5279 5280 /* Pop back to the data of the outer initializer (if any). */ 5281 constructor_decl = p->decl; 5282 constructor_asmspec = p->asmspec; 5283 constructor_incremental = p->incremental; 5284 require_constant_value = p->require_constant_value; 5285 require_constant_elements = p->require_constant_elements; 5286 constructor_stack = p->constructor_stack; 5287 constructor_elements = p->elements; 5288 spelling = p->spelling; 5289 spelling_base = p->spelling_base; 5290 spelling_size = p->spelling_size; 5291 constructor_subconstants_deferred = p->deferred; 5292 constructor_top_level = p->top_level; 5293 initializer_stack = p->next; 5294 free (p); 5295} 5296 5297/* Call here when we see the initializer is surrounded by braces. 5298 This is instead of a call to push_init_level; 5299 it is matched by a call to pop_init_level. 5300 5301 TYPE is the type to initialize, for a constructor expression. 5302 For an initializer for a decl, TYPE is zero. */ 5303 5304void 5305really_start_incremental_init (type) 5306 tree type; 5307{ 5308 struct constructor_stack *p 5309 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack)); 5310 5311 if (type == 0) 5312 type = TREE_TYPE (constructor_decl); 5313 5314 /* Turn off constructor_incremental if type is a struct with bitfields. 5315 Do this before the first push, so that the corrected value 5316 is available in finish_init. */ 5317 check_init_type_bitfields (type); 5318 5319 p->type = constructor_type; 5320 p->fields = constructor_fields; 5321 p->index = constructor_index; 5322 p->range_end = constructor_range_end; 5323 p->max_index = constructor_max_index; 5324 p->unfilled_index = constructor_unfilled_index; 5325 p->unfilled_fields = constructor_unfilled_fields; 5326 p->bit_index = constructor_bit_index; 5327 p->elements = constructor_elements; 5328 p->constant = constructor_constant; 5329 p->simple = constructor_simple; 5330 p->erroneous = constructor_erroneous; 5331 p->pending_elts = constructor_pending_elts; 5332 p->depth = constructor_depth; 5333 p->replacement_value = 0; 5334 p->implicit = 0; 5335 p->incremental = constructor_incremental; 5336 p->outer = 0; 5337 p->next = 0; 5338 constructor_stack = p; 5339 5340 constructor_constant = 1; 5341 constructor_simple = 1; 5342 constructor_depth = SPELLING_DEPTH (); 5343 constructor_elements = 0; 5344 constructor_pending_elts = 0; 5345 constructor_type = type; 5346 5347 if (TREE_CODE (constructor_type) == RECORD_TYPE 5348 || TREE_CODE (constructor_type) == UNION_TYPE) 5349 { 5350 constructor_fields = TYPE_FIELDS (constructor_type); 5351 /* Skip any nameless bit fields at the beginning. */ 5352 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) 5353 && DECL_NAME (constructor_fields) == 0) 5354 constructor_fields = TREE_CHAIN (constructor_fields); 5355 constructor_unfilled_fields = constructor_fields; 5356 constructor_bit_index = copy_node (integer_zero_node); 5357 TREE_TYPE (constructor_bit_index) = sbitsizetype; 5358 } 5359 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5360 { 5361 constructor_range_end = 0; 5362 if (TYPE_DOMAIN (constructor_type)) 5363 { 5364 constructor_max_index 5365 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); 5366 constructor_index 5367 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 5368 } 5369 else 5370 constructor_index = copy_node (integer_zero_node); 5371 constructor_unfilled_index = copy_node (constructor_index); 5372 } 5373 else 5374 { 5375 /* Handle the case of int x = {5}; */ 5376 constructor_fields = constructor_type; 5377 constructor_unfilled_fields = constructor_type; 5378 } 5379 5380 if (constructor_incremental) 5381 { 5382 int momentary = suspend_momentary (); 5383 push_obstacks_nochange (); 5384 if (TREE_PERMANENT (constructor_decl)) 5385 end_temporary_allocation (); 5386 make_decl_rtl (constructor_decl, constructor_asmspec, 5387 constructor_top_level); 5388 assemble_variable (constructor_decl, constructor_top_level, 0, 1); 5389 pop_obstacks (); 5390 resume_momentary (momentary); 5391 } 5392 5393 if (constructor_incremental) 5394 { 5395 defer_addressed_constants (); 5396 constructor_subconstants_deferred = 1; 5397 } 5398} 5399 5400/* Push down into a subobject, for initialization. 5401 If this is for an explicit set of braces, IMPLICIT is 0. 5402 If it is because the next element belongs at a lower level, 5403 IMPLICIT is 1. */ 5404 5405void 5406push_init_level (implicit) 5407 int implicit; 5408{ 5409 struct constructor_stack *p; 5410 5411 /* If we've exhausted any levels that didn't have braces, 5412 pop them now. */ 5413 while (constructor_stack->implicit) 5414 { 5415 if ((TREE_CODE (constructor_type) == RECORD_TYPE 5416 || TREE_CODE (constructor_type) == UNION_TYPE) 5417 && constructor_fields == 0) 5418 process_init_element (pop_init_level (1)); 5419 else if (TREE_CODE (constructor_type) == ARRAY_TYPE 5420 && tree_int_cst_lt (constructor_max_index, constructor_index)) 5421 process_init_element (pop_init_level (1)); 5422 else 5423 break; 5424 } 5425 5426 /* Structure elements may require alignment. Do this now if necessary 5427 for the subaggregate, and if it comes next in sequence. Don't do 5428 this for subaggregates that will go on the pending list. */ 5429 if (constructor_incremental && constructor_type != 0 5430 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields 5431 && constructor_fields == constructor_unfilled_fields) 5432 { 5433 /* Advance to offset of this element. */ 5434 if (! tree_int_cst_equal (constructor_bit_index, 5435 DECL_FIELD_BITPOS (constructor_fields))) 5436 { 5437 /* By using unsigned arithmetic, the result will be correct even 5438 in case of overflows, if BITS_PER_UNIT is a power of two. */ 5439 unsigned next = (TREE_INT_CST_LOW 5440 (DECL_FIELD_BITPOS (constructor_fields)) 5441 / (unsigned)BITS_PER_UNIT); 5442 unsigned here = (TREE_INT_CST_LOW (constructor_bit_index) 5443 / (unsigned)BITS_PER_UNIT); 5444 5445 assemble_zeros ((next - here) 5446 * (unsigned)BITS_PER_UNIT 5447 / (unsigned)BITS_PER_UNIT); 5448 } 5449 /* Indicate that we have now filled the structure up to the current 5450 field. */ 5451 constructor_unfilled_fields = constructor_fields; 5452 } 5453 5454 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack)); 5455 p->type = constructor_type; 5456 p->fields = constructor_fields; 5457 p->index = constructor_index; 5458 p->range_end = constructor_range_end; 5459 p->max_index = constructor_max_index; 5460 p->unfilled_index = constructor_unfilled_index; 5461 p->unfilled_fields = constructor_unfilled_fields; 5462 p->bit_index = constructor_bit_index; 5463 p->elements = constructor_elements; 5464 p->constant = constructor_constant; 5465 p->simple = constructor_simple; 5466 p->erroneous = constructor_erroneous; 5467 p->pending_elts = constructor_pending_elts; 5468 p->depth = constructor_depth; 5469 p->replacement_value = 0; 5470 p->implicit = implicit; 5471 p->incremental = constructor_incremental; 5472 p->outer = 0; 5473 p->next = constructor_stack; 5474 constructor_stack = p; 5475 5476 constructor_constant = 1; 5477 constructor_simple = 1; 5478 constructor_depth = SPELLING_DEPTH (); 5479 constructor_elements = 0; 5480 constructor_pending_elts = 0; 5481 5482 /* Don't die if an entire brace-pair level is superfluous 5483 in the containing level. */ 5484 if (constructor_type == 0) 5485 ; 5486 else if (TREE_CODE (constructor_type) == RECORD_TYPE 5487 || TREE_CODE (constructor_type) == UNION_TYPE) 5488 { 5489 /* Don't die if there are extra init elts at the end. */ 5490 if (constructor_fields == 0) 5491 constructor_type = 0; 5492 else 5493 { 5494 constructor_type = TREE_TYPE (constructor_fields); 5495 push_member_name (constructor_fields); 5496 constructor_depth++; 5497 if (constructor_fields != constructor_unfilled_fields) 5498 constructor_incremental = 0; 5499 } 5500 } 5501 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5502 { 5503 constructor_type = TREE_TYPE (constructor_type); 5504 push_array_bounds (TREE_INT_CST_LOW (constructor_index)); 5505 constructor_depth++; 5506 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index) 5507 || constructor_range_end != 0) 5508 constructor_incremental = 0; 5509 } 5510 5511 if (constructor_type == 0) 5512 { 5513 error_init ("extra brace group at end of initializer%s", 5514 " for `%s'", NULL); 5515 constructor_fields = 0; 5516 constructor_unfilled_fields = 0; 5517 return; 5518 } 5519 5520 /* Turn off constructor_incremental if type is a struct with bitfields. */ 5521 check_init_type_bitfields (constructor_type); 5522 5523 if (implicit && warn_missing_braces && !missing_braces_mentioned) 5524 { 5525 missing_braces_mentioned = 1; 5526 warning_init ("missing braces around initializer%s", " for `%s'", NULL); 5527 } 5528 5529 if (TREE_CODE (constructor_type) == RECORD_TYPE 5530 || TREE_CODE (constructor_type) == UNION_TYPE) 5531 { 5532 constructor_fields = TYPE_FIELDS (constructor_type); 5533 /* Skip any nameless bit fields at the beginning. */ 5534 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) 5535 && DECL_NAME (constructor_fields) == 0) 5536 constructor_fields = TREE_CHAIN (constructor_fields); 5537 constructor_unfilled_fields = constructor_fields; 5538 constructor_bit_index = copy_node (integer_zero_node); 5539 TREE_TYPE (constructor_bit_index) = sbitsizetype; 5540 } 5541 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5542 { 5543 constructor_range_end = 0; 5544 if (TYPE_DOMAIN (constructor_type)) 5545 { 5546 constructor_max_index 5547 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); 5548 constructor_index 5549 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); 5550 } 5551 else 5552 constructor_index = copy_node (integer_zero_node); 5553 constructor_unfilled_index = copy_node (constructor_index); 5554 } 5555 else 5556 { 5557 warning_init ("braces around scalar initializer%s", " for `%s'", NULL); 5558 constructor_fields = constructor_type; 5559 constructor_unfilled_fields = constructor_type; 5560 } 5561} 5562 5563/* Don't read a struct incrementally if it has any bitfields, 5564 because the incremental reading code doesn't know how to 5565 handle bitfields yet. */ 5566 5567static void 5568check_init_type_bitfields (type) 5569 tree type; 5570{ 5571 if (TREE_CODE (type) == RECORD_TYPE) 5572 { 5573 tree tail; 5574 for (tail = TYPE_FIELDS (type); tail; 5575 tail = TREE_CHAIN (tail)) 5576 { 5577 if (DECL_C_BIT_FIELD (tail) 5578 /* This catches cases like `int foo : 8;'. */ 5579 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail))) 5580 { 5581 constructor_incremental = 0; 5582 break; 5583 } 5584 5585 check_init_type_bitfields (TREE_TYPE (tail)); 5586 } 5587 } 5588 5589 else if (TREE_CODE (type) == ARRAY_TYPE) 5590 check_init_type_bitfields (TREE_TYPE (type)); 5591} 5592 5593/* At the end of an implicit or explicit brace level, 5594 finish up that level of constructor. 5595 If we were outputting the elements as they are read, return 0 5596 from inner levels (process_init_element ignores that), 5597 but return error_mark_node from the outermost level 5598 (that's what we want to put in DECL_INITIAL). 5599 Otherwise, return a CONSTRUCTOR expression. */ 5600 5601tree 5602pop_init_level (implicit) 5603 int implicit; 5604{ 5605 struct constructor_stack *p; 5606 int size = 0; 5607 tree constructor = 0; 5608 5609 if (implicit == 0) 5610 { 5611 /* When we come to an explicit close brace, 5612 pop any inner levels that didn't have explicit braces. */ 5613 while (constructor_stack->implicit) 5614 process_init_element (pop_init_level (1)); 5615 } 5616 5617 p = constructor_stack; 5618 5619 if (constructor_type != 0) 5620 size = int_size_in_bytes (constructor_type); 5621 5622 /* Warn when some struct elements are implicitly initialized to zero. */ 5623 if (extra_warnings 5624 && constructor_type 5625 && TREE_CODE (constructor_type) == RECORD_TYPE 5626 && constructor_unfilled_fields) 5627 { 5628 push_member_name (constructor_unfilled_fields); 5629 warning_init ("missing initializer%s", " for `%s'", NULL); 5630 RESTORE_SPELLING_DEPTH (constructor_depth); 5631 } 5632 5633 /* Now output all pending elements. */ 5634 output_pending_init_elements (1); 5635 5636#if 0 /* c-parse.in warns about {}. */ 5637 /* In ANSI, each brace level must have at least one element. */ 5638 if (! implicit && pedantic 5639 && (TREE_CODE (constructor_type) == ARRAY_TYPE 5640 ? integer_zerop (constructor_unfilled_index) 5641 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type))) 5642 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL); 5643#endif 5644 5645 /* Pad out the end of the structure. */ 5646 5647 if (p->replacement_value) 5648 { 5649 /* If this closes a superfluous brace pair, 5650 just pass out the element between them. */ 5651 constructor = p->replacement_value; 5652 /* If this is the top level thing within the initializer, 5653 and it's for a variable, then since we already called 5654 assemble_variable, we must output the value now. */ 5655 if (p->next == 0 && constructor_decl != 0 5656 && constructor_incremental) 5657 { 5658 constructor = digest_init (constructor_type, constructor, 5659 require_constant_value, 5660 require_constant_elements); 5661 5662 /* If initializing an array of unknown size, 5663 determine the size now. */ 5664 if (TREE_CODE (constructor_type) == ARRAY_TYPE 5665 && TYPE_DOMAIN (constructor_type) == 0) 5666 { 5667 int failure; 5668 int momentary_p; 5669 5670 push_obstacks_nochange (); 5671 if (TREE_PERMANENT (constructor_type)) 5672 end_temporary_allocation (); 5673 5674 momentary_p = suspend_momentary (); 5675 5676 /* We shouldn't have an incomplete array type within 5677 some other type. */ 5678 if (constructor_stack->next) 5679 abort (); 5680 5681 failure 5682 = complete_array_type (constructor_type, 5683 constructor, 0); 5684 if (failure) 5685 abort (); 5686 5687 size = int_size_in_bytes (constructor_type); 5688 resume_momentary (momentary_p); 5689 pop_obstacks (); 5690 } 5691 5692 output_constant (constructor, size); 5693 } 5694 } 5695 else if (constructor_type == 0) 5696 ; 5697 else if (TREE_CODE (constructor_type) != RECORD_TYPE 5698 && TREE_CODE (constructor_type) != UNION_TYPE 5699 && TREE_CODE (constructor_type) != ARRAY_TYPE 5700 && ! constructor_incremental) 5701 { 5702 /* A nonincremental scalar initializer--just return 5703 the element, after verifying there is just one. */ 5704 if (constructor_elements == 0) 5705 { 5706 error_init ("empty scalar initializer%s", 5707 " for `%s'", NULL); 5708 constructor = error_mark_node; 5709 } 5710 else if (TREE_CHAIN (constructor_elements) != 0) 5711 { 5712 error_init ("extra elements in scalar initializer%s", 5713 " for `%s'", NULL); 5714 constructor = TREE_VALUE (constructor_elements); 5715 } 5716 else 5717 constructor = TREE_VALUE (constructor_elements); 5718 } 5719 else if (! constructor_incremental) 5720 { 5721 if (constructor_erroneous) 5722 constructor = error_mark_node; 5723 else 5724 { 5725 int momentary = suspend_momentary (); 5726 5727 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE, 5728 nreverse (constructor_elements)); 5729 if (constructor_constant) 5730 TREE_CONSTANT (constructor) = 1; 5731 if (constructor_constant && constructor_simple) 5732 TREE_STATIC (constructor) = 1; 5733 5734 resume_momentary (momentary); 5735 } 5736 } 5737 else 5738 { 5739 tree filled; 5740 int momentary = suspend_momentary (); 5741 5742 if (TREE_CODE (constructor_type) == RECORD_TYPE 5743 || TREE_CODE (constructor_type) == UNION_TYPE) 5744 { 5745 /* Find the offset of the end of that field. */ 5746 filled = size_binop (CEIL_DIV_EXPR, 5747 constructor_bit_index, 5748 size_int (BITS_PER_UNIT)); 5749 } 5750 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5751 { 5752 /* If initializing an array of unknown size, 5753 determine the size now. */ 5754 if (TREE_CODE (constructor_type) == ARRAY_TYPE 5755 && TYPE_DOMAIN (constructor_type) == 0) 5756 { 5757 tree maxindex 5758 = size_binop (MINUS_EXPR, 5759 constructor_unfilled_index, 5760 integer_one_node); 5761 5762 push_obstacks_nochange (); 5763 if (TREE_PERMANENT (constructor_type)) 5764 end_temporary_allocation (); 5765 maxindex = copy_node (maxindex); 5766 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex); 5767 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type); 5768 5769 /* TYPE_MAX_VALUE is always one less than the number of elements 5770 in the array, because we start counting at zero. Therefore, 5771 warn only if the value is less than zero. */ 5772 if (pedantic 5773 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) 5774 < 0)) 5775 error_with_decl (constructor_decl, 5776 "zero or negative array size `%s'"); 5777 layout_type (constructor_type); 5778 size = int_size_in_bytes (constructor_type); 5779 pop_obstacks (); 5780 } 5781 5782 filled = size_binop (MULT_EXPR, constructor_unfilled_index, 5783 size_in_bytes (TREE_TYPE (constructor_type))); 5784 } 5785 else 5786 filled = 0; 5787 5788 if (filled != 0) 5789 assemble_zeros (size - TREE_INT_CST_LOW (filled)); 5790 5791 resume_momentary (momentary); 5792 } 5793 5794 5795 constructor_type = p->type; 5796 constructor_fields = p->fields; 5797 constructor_index = p->index; 5798 constructor_range_end = p->range_end; 5799 constructor_max_index = p->max_index; 5800 constructor_unfilled_index = p->unfilled_index; 5801 constructor_unfilled_fields = p->unfilled_fields; 5802 constructor_bit_index = p->bit_index; 5803 constructor_elements = p->elements; 5804 constructor_constant = p->constant; 5805 constructor_simple = p->simple; 5806 constructor_erroneous = p->erroneous; 5807 constructor_pending_elts = p->pending_elts; 5808 constructor_depth = p->depth; 5809 constructor_incremental = p->incremental; 5810 RESTORE_SPELLING_DEPTH (constructor_depth); 5811 5812 constructor_stack = p->next; 5813 free (p); 5814 5815 if (constructor == 0) 5816 { 5817 if (constructor_stack == 0) 5818 return error_mark_node; 5819 return NULL_TREE; 5820 } 5821 return constructor; 5822} 5823 5824/* Within an array initializer, specify the next index to be initialized. 5825 FIRST is that index. If LAST is nonzero, then initialize a range 5826 of indices, running from FIRST through LAST. */ 5827 5828void 5829set_init_index (first, last) 5830 tree first, last; 5831{ 5832 while ((TREE_CODE (first) == NOP_EXPR 5833 || TREE_CODE (first) == CONVERT_EXPR 5834 || TREE_CODE (first) == NON_LVALUE_EXPR) 5835 && (TYPE_MODE (TREE_TYPE (first)) 5836 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0))))) 5837 (first) = TREE_OPERAND (first, 0); 5838 if (last) 5839 while ((TREE_CODE (last) == NOP_EXPR 5840 || TREE_CODE (last) == CONVERT_EXPR 5841 || TREE_CODE (last) == NON_LVALUE_EXPR) 5842 && (TYPE_MODE (TREE_TYPE (last)) 5843 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0))))) 5844 (last) = TREE_OPERAND (last, 0); 5845 5846 if (TREE_CODE (first) != INTEGER_CST) 5847 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL); 5848 else if (last != 0 && TREE_CODE (last) != INTEGER_CST) 5849 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL); 5850 else if (! constructor_unfilled_index) 5851 error_init ("array index in non-array initializer%s", " for `%s'", NULL); 5852 else if (tree_int_cst_lt (first, constructor_unfilled_index)) 5853 error_init ("duplicate array index in initializer%s", " for `%s'", NULL); 5854 else 5855 { 5856 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first); 5857 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first); 5858 5859 if (last != 0 && tree_int_cst_lt (last, first)) 5860 error_init ("empty index range in initializer%s", " for `%s'", NULL); 5861 else 5862 { 5863 if (pedantic) 5864 pedwarn ("ANSI C forbids specifying element to initialize"); 5865 constructor_range_end = last; 5866 } 5867 } 5868} 5869 5870/* Within a struct initializer, specify the next field to be initialized. */ 5871 5872void 5873set_init_label (fieldname) 5874 tree fieldname; 5875{ 5876 tree tail; 5877 int passed = 0; 5878 5879 /* Don't die if an entire brace-pair level is superfluous 5880 in the containing level. */ 5881 if (constructor_type == 0) 5882 return; 5883 5884 for (tail = TYPE_FIELDS (constructor_type); tail; 5885 tail = TREE_CHAIN (tail)) 5886 { 5887 if (tail == constructor_unfilled_fields) 5888 passed = 1; 5889 if (DECL_NAME (tail) == fieldname) 5890 break; 5891 } 5892 5893 if (tail == 0) 5894 error ("unknown field `%s' specified in initializer", 5895 IDENTIFIER_POINTER (fieldname)); 5896 else if (!passed) 5897 error ("field `%s' already initialized", 5898 IDENTIFIER_POINTER (fieldname)); 5899 else 5900 { 5901 constructor_fields = tail; 5902 if (pedantic) 5903 pedwarn ("ANSI C forbids specifying structure member to initialize"); 5904 } 5905} 5906 5907/* Add a new initializer to the tree of pending initializers. PURPOSE 5908 indentifies the initializer, either array index or field in a structure. 5909 VALUE is the value of that index or field. */ 5910 5911static void 5912add_pending_init (purpose, value) 5913 tree purpose, value; 5914{ 5915 struct init_node *p, **q, *r; 5916 5917 q = &constructor_pending_elts; 5918 p = 0; 5919 5920 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 5921 { 5922 while (*q != 0) 5923 { 5924 p = *q; 5925 if (tree_int_cst_lt (purpose, p->purpose)) 5926 q = &p->left; 5927 else if (tree_int_cst_lt (p->purpose, purpose)) 5928 q = &p->right; 5929 else 5930 abort (); 5931 } 5932 } 5933 else 5934 { 5935 while (*q != NULL) 5936 { 5937 p = *q; 5938 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose), 5939 DECL_FIELD_BITPOS (p->purpose))) 5940 q = &p->left; 5941 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (p->purpose), 5942 DECL_FIELD_BITPOS (purpose))) 5943 q = &p->right; 5944 else 5945 abort (); 5946 } 5947 } 5948 5949 r = (struct init_node *) oballoc (sizeof (struct init_node)); 5950 r->purpose = purpose; 5951 r->value = value; 5952 5953 *q = r; 5954 r->parent = p; 5955 r->left = 0; 5956 r->right = 0; 5957 r->balance = 0; 5958 5959 while (p) 5960 { 5961 struct init_node *s; 5962 5963 if (r == p->left) 5964 { 5965 if (p->balance == 0) 5966 p->balance = -1; 5967 else if (p->balance < 0) 5968 { 5969 if (r->balance < 0) 5970 { 5971 /* L rotation. */ 5972 p->left = r->right; 5973 if (p->left) 5974 p->left->parent = p; 5975 r->right = p; 5976 5977 p->balance = 0; 5978 r->balance = 0; 5979 5980 s = p->parent; 5981 p->parent = r; 5982 r->parent = s; 5983 if (s) 5984 { 5985 if (s->left == p) 5986 s->left = r; 5987 else 5988 s->right = r; 5989 } 5990 else 5991 constructor_pending_elts = r; 5992 } 5993 else 5994 { 5995 /* LR rotation. */ 5996 struct init_node *t = r->right; 5997 5998 r->right = t->left; 5999 if (r->right) 6000 r->right->parent = r; 6001 t->left = r; 6002 6003 p->left = t->right; 6004 if (p->left) 6005 p->left->parent = p; 6006 t->right = p; 6007 6008 p->balance = t->balance < 0; 6009 r->balance = -(t->balance > 0); 6010 t->balance = 0; 6011 6012 s = p->parent; 6013 p->parent = t; 6014 r->parent = t; 6015 t->parent = s; 6016 if (s) 6017 { 6018 if (s->left == p) 6019 s->left = t; 6020 else 6021 s->right = t; 6022 } 6023 else 6024 constructor_pending_elts = t; 6025 } 6026 break; 6027 } 6028 else 6029 { 6030 /* p->balance == +1; growth of left side balances the node. */ 6031 p->balance = 0; 6032 break; 6033 } 6034 } 6035 else /* r == p->right */ 6036 { 6037 if (p->balance == 0) 6038 /* Growth propagation from right side. */ 6039 p->balance++; 6040 else if (p->balance > 0) 6041 { 6042 if (r->balance > 0) 6043 { 6044 /* R rotation. */ 6045 p->right = r->left; 6046 if (p->right) 6047 p->right->parent = p; 6048 r->left = p; 6049 6050 p->balance = 0; 6051 r->balance = 0; 6052 6053 s = p->parent; 6054 p->parent = r; 6055 r->parent = s; 6056 if (s) 6057 { 6058 if (s->left == p) 6059 s->left = r; 6060 else 6061 s->right = r; 6062 } 6063 else 6064 constructor_pending_elts = r; 6065 } 6066 else /* r->balance == -1 */ 6067 { 6068 /* RL rotation */ 6069 struct init_node *t = r->left; 6070 6071 r->left = t->right; 6072 if (r->left) 6073 r->left->parent = r; 6074 t->right = r; 6075 6076 p->right = t->left; 6077 if (p->right) 6078 p->right->parent = p; 6079 t->left = p; 6080 6081 r->balance = (t->balance < 0); 6082 p->balance = -(t->balance > 0); 6083 t->balance = 0; 6084 6085 s = p->parent; 6086 p->parent = t; 6087 r->parent = t; 6088 t->parent = s; 6089 if (s) 6090 { 6091 if (s->left == p) 6092 s->left = t; 6093 else 6094 s->right = t; 6095 } 6096 else 6097 constructor_pending_elts = t; 6098 } 6099 break; 6100 } 6101 else 6102 { 6103 /* p->balance == -1; growth of right side balances the node. */ 6104 p->balance = 0; 6105 break; 6106 } 6107 } 6108 6109 r = p; 6110 p = p->parent; 6111 } 6112} 6113 6114/* Return nonzero if FIELD is equal to the index of a pending initializer. */ 6115 6116static int 6117pending_init_member (field) 6118 tree field; 6119{ 6120 struct init_node *p; 6121 6122 p = constructor_pending_elts; 6123 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6124 { 6125 while (p) 6126 { 6127 if (tree_int_cst_equal (field, p->purpose)) 6128 return 1; 6129 else if (tree_int_cst_lt (field, p->purpose)) 6130 p = p->left; 6131 else 6132 p = p->right; 6133 } 6134 } 6135 else 6136 { 6137 while (p) 6138 { 6139 if (field == p->purpose) 6140 return 1; 6141 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field), 6142 DECL_FIELD_BITPOS (p->purpose))) 6143 p = p->left; 6144 else 6145 p = p->right; 6146 } 6147 } 6148 6149 return 0; 6150} 6151 6152/* "Output" the next constructor element. 6153 At top level, really output it to assembler code now. 6154 Otherwise, collect it in a list from which we will make a CONSTRUCTOR. 6155 TYPE is the data type that the containing data type wants here. 6156 FIELD is the field (a FIELD_DECL) or the index that this element fills. 6157 6158 PENDING if non-nil means output pending elements that belong 6159 right after this element. (PENDING is normally 1; 6160 it is 0 while outputting pending elements, to avoid recursion.) */ 6161 6162static void 6163output_init_element (value, type, field, pending) 6164 tree value, type, field; 6165 int pending; 6166{ 6167 int duplicate = 0; 6168 6169 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE 6170 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE 6171 && !(TREE_CODE (value) == STRING_CST 6172 && TREE_CODE (type) == ARRAY_TYPE 6173 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE) 6174 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), 6175 TYPE_MAIN_VARIANT (type)))) 6176 value = default_conversion (value); 6177 6178 if (value == error_mark_node) 6179 constructor_erroneous = 1; 6180 else if (!TREE_CONSTANT (value)) 6181 constructor_constant = 0; 6182 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0 6183 || ((TREE_CODE (constructor_type) == RECORD_TYPE 6184 || TREE_CODE (constructor_type) == UNION_TYPE) 6185 && DECL_C_BIT_FIELD (field) 6186 && TREE_CODE (value) != INTEGER_CST)) 6187 constructor_simple = 0; 6188 6189 if (require_constant_value && ! TREE_CONSTANT (value)) 6190 { 6191 error_init ("initializer element%s is not constant", 6192 " for `%s'", NULL); 6193 value = error_mark_node; 6194 } 6195 else if (require_constant_elements 6196 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0) 6197 { 6198 error_init ("initializer element%s is not computable at load time", 6199 " for `%s'", NULL); 6200 value = error_mark_node; 6201 } 6202 6203 /* If this element duplicates one on constructor_pending_elts, 6204 print a message and ignore it. Don't do this when we're 6205 processing elements taken off constructor_pending_elts, 6206 because we'd always get spurious errors. */ 6207 if (pending) 6208 { 6209 if (TREE_CODE (constructor_type) == RECORD_TYPE 6210 || TREE_CODE (constructor_type) == UNION_TYPE 6211 || TREE_CODE (constructor_type) == ARRAY_TYPE) 6212 { 6213 if (pending_init_member (field)) 6214 { 6215 error_init ("duplicate initializer%s", " for `%s'", NULL); 6216 duplicate = 1; 6217 } 6218 } 6219 } 6220 6221 /* If this element doesn't come next in sequence, 6222 put it on constructor_pending_elts. */ 6223 if (TREE_CODE (constructor_type) == ARRAY_TYPE 6224 && !tree_int_cst_equal (field, constructor_unfilled_index)) 6225 { 6226 if (! duplicate) 6227 /* The copy_node is needed in case field is actually 6228 constructor_index, which is modified in place. */ 6229 add_pending_init (copy_node (field), 6230 digest_init (type, value, require_constant_value, 6231 require_constant_elements)); 6232 } 6233 else if (TREE_CODE (constructor_type) == RECORD_TYPE 6234 && field != constructor_unfilled_fields) 6235 { 6236 /* We do this for records but not for unions. In a union, 6237 no matter which field is specified, it can be initialized 6238 right away since it starts at the beginning of the union. */ 6239 if (!duplicate) 6240 add_pending_init (field, 6241 digest_init (type, value, require_constant_value, 6242 require_constant_elements)); 6243 } 6244 else 6245 { 6246 /* Otherwise, output this element either to 6247 constructor_elements or to the assembler file. */ 6248 6249 if (!duplicate) 6250 { 6251 if (! constructor_incremental) 6252 { 6253 if (field && TREE_CODE (field) == INTEGER_CST) 6254 field = copy_node (field); 6255 constructor_elements 6256 = tree_cons (field, digest_init (type, value, 6257 require_constant_value, 6258 require_constant_elements), 6259 constructor_elements); 6260 } 6261 else 6262 { 6263 /* Structure elements may require alignment. 6264 Do this, if necessary. */ 6265 if (TREE_CODE (constructor_type) == RECORD_TYPE) 6266 { 6267 /* Advance to offset of this element. */ 6268 if (! tree_int_cst_equal (constructor_bit_index, 6269 DECL_FIELD_BITPOS (field))) 6270 { 6271 /* By using unsigned arithmetic, the result will be 6272 correct even in case of overflows, if BITS_PER_UNIT 6273 is a power of two. */ 6274 unsigned next = (TREE_INT_CST_LOW 6275 (DECL_FIELD_BITPOS (field)) 6276 / (unsigned)BITS_PER_UNIT); 6277 unsigned here = (TREE_INT_CST_LOW 6278 (constructor_bit_index) 6279 / (unsigned)BITS_PER_UNIT); 6280 6281 assemble_zeros ((next - here) 6282 * (unsigned)BITS_PER_UNIT 6283 / (unsigned)BITS_PER_UNIT); 6284 } 6285 } 6286 output_constant (digest_init (type, value, 6287 require_constant_value, 6288 require_constant_elements), 6289 int_size_in_bytes (type)); 6290 6291 /* For a record or union, 6292 keep track of end position of last field. */ 6293 if (TREE_CODE (constructor_type) == RECORD_TYPE 6294 || TREE_CODE (constructor_type) == UNION_TYPE) 6295 { 6296 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field), 6297 DECL_SIZE (field)); 6298 TREE_INT_CST_LOW (constructor_bit_index) 6299 = TREE_INT_CST_LOW (temp); 6300 TREE_INT_CST_HIGH (constructor_bit_index) 6301 = TREE_INT_CST_HIGH (temp); 6302 } 6303 } 6304 } 6305 6306 /* Advance the variable that indicates sequential elements output. */ 6307 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6308 { 6309 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index, 6310 integer_one_node); 6311 TREE_INT_CST_LOW (constructor_unfilled_index) 6312 = TREE_INT_CST_LOW (tem); 6313 TREE_INT_CST_HIGH (constructor_unfilled_index) 6314 = TREE_INT_CST_HIGH (tem); 6315 } 6316 else if (TREE_CODE (constructor_type) == RECORD_TYPE) 6317 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); 6318 else if (TREE_CODE (constructor_type) == UNION_TYPE) 6319 constructor_unfilled_fields = 0; 6320 6321 /* Now output any pending elements which have become next. */ 6322 if (pending) 6323 output_pending_init_elements (0); 6324 } 6325} 6326 6327/* Output any pending elements which have become next. 6328 As we output elements, constructor_unfilled_{fields,index} 6329 advances, which may cause other elements to become next; 6330 if so, they too are output. 6331 6332 If ALL is 0, we return when there are 6333 no more pending elements to output now. 6334 6335 If ALL is 1, we output space as necessary so that 6336 we can output all the pending elements. */ 6337 6338static void 6339output_pending_init_elements (all) 6340 int all; 6341{ 6342 struct init_node *elt = constructor_pending_elts; 6343 tree next; 6344 6345 retry: 6346 6347 /* Look thru the whole pending tree. 6348 If we find an element that should be output now, 6349 output it. Otherwise, set NEXT to the element 6350 that comes first among those still pending. */ 6351 6352 next = 0; 6353 while (elt) 6354 { 6355 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6356 { 6357 if (tree_int_cst_equal (elt->purpose, 6358 constructor_unfilled_index)) 6359 output_init_element (elt->value, 6360 TREE_TYPE (constructor_type), 6361 constructor_unfilled_index, 0); 6362 else if (tree_int_cst_lt (constructor_unfilled_index, 6363 elt->purpose)) 6364 { 6365 /* Advance to the next smaller node. */ 6366 if (elt->left) 6367 elt = elt->left; 6368 else 6369 { 6370 /* We have reached the smallest node bigger than the 6371 current unfilled index. Fill the space first. */ 6372 next = elt->purpose; 6373 break; 6374 } 6375 } 6376 else 6377 { 6378 /* Advance to the next bigger node. */ 6379 if (elt->right) 6380 elt = elt->right; 6381 else 6382 { 6383 /* We have reached the biggest node in a subtree. Find 6384 the parent of it, which is the next bigger node. */ 6385 while (elt->parent && elt->parent->right == elt) 6386 elt = elt->parent; 6387 elt = elt->parent; 6388 if (elt && tree_int_cst_lt (constructor_unfilled_index, 6389 elt->purpose)) 6390 { 6391 next = elt->purpose; 6392 break; 6393 } 6394 } 6395 } 6396 } 6397 else if (TREE_CODE (constructor_type) == RECORD_TYPE 6398 || TREE_CODE (constructor_type) == UNION_TYPE) 6399 { 6400 /* If the current record is complete we are done. */ 6401 if (constructor_unfilled_fields == 0) 6402 break; 6403 if (elt->purpose == constructor_unfilled_fields) 6404 { 6405 output_init_element (elt->value, 6406 TREE_TYPE (constructor_unfilled_fields), 6407 constructor_unfilled_fields, 6408 0); 6409 } 6410 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields), 6411 DECL_FIELD_BITPOS (elt->purpose))) 6412 { 6413 /* Advance to the next smaller node. */ 6414 if (elt->left) 6415 elt = elt->left; 6416 else 6417 { 6418 /* We have reached the smallest node bigger than the 6419 current unfilled field. Fill the space first. */ 6420 next = elt->purpose; 6421 break; 6422 } 6423 } 6424 else 6425 { 6426 /* Advance to the next bigger node. */ 6427 if (elt->right) 6428 elt = elt->right; 6429 else 6430 { 6431 /* We have reached the biggest node in a subtree. Find 6432 the parent of it, which is the next bigger node. */ 6433 while (elt->parent && elt->parent->right == elt) 6434 elt = elt->parent; 6435 elt = elt->parent; 6436 if (elt 6437 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields), 6438 DECL_FIELD_BITPOS (elt->purpose))) 6439 { 6440 next = elt->purpose; 6441 break; 6442 } 6443 } 6444 } 6445 } 6446 } 6447 6448 /* Ordinarily return, but not if we want to output all 6449 and there are elements left. */ 6450 if (! (all && next != 0)) 6451 return; 6452 6453 /* Generate space up to the position of NEXT. */ 6454 if (constructor_incremental) 6455 { 6456 tree filled; 6457 tree nextpos_tree = size_int (0); 6458 6459 if (TREE_CODE (constructor_type) == RECORD_TYPE 6460 || TREE_CODE (constructor_type) == UNION_TYPE) 6461 { 6462 tree tail; 6463 /* Find the last field written out, if any. */ 6464 for (tail = TYPE_FIELDS (constructor_type); tail; 6465 tail = TREE_CHAIN (tail)) 6466 if (TREE_CHAIN (tail) == constructor_unfilled_fields) 6467 break; 6468 6469 if (tail) 6470 /* Find the offset of the end of that field. */ 6471 filled = size_binop (CEIL_DIV_EXPR, 6472 size_binop (PLUS_EXPR, 6473 DECL_FIELD_BITPOS (tail), 6474 DECL_SIZE (tail)), 6475 size_int (BITS_PER_UNIT)); 6476 else 6477 filled = size_int (0); 6478 6479 nextpos_tree = size_binop (CEIL_DIV_EXPR, 6480 DECL_FIELD_BITPOS (next), 6481 size_int (BITS_PER_UNIT)); 6482 6483 TREE_INT_CST_HIGH (constructor_bit_index) 6484 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next)); 6485 TREE_INT_CST_LOW (constructor_bit_index) 6486 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next)); 6487 constructor_unfilled_fields = next; 6488 } 6489 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6490 { 6491 filled = size_binop (MULT_EXPR, constructor_unfilled_index, 6492 size_in_bytes (TREE_TYPE (constructor_type))); 6493 nextpos_tree 6494 = size_binop (MULT_EXPR, next, 6495 size_in_bytes (TREE_TYPE (constructor_type))); 6496 TREE_INT_CST_LOW (constructor_unfilled_index) 6497 = TREE_INT_CST_LOW (next); 6498 TREE_INT_CST_HIGH (constructor_unfilled_index) 6499 = TREE_INT_CST_HIGH (next); 6500 } 6501 else 6502 filled = 0; 6503 6504 if (filled) 6505 { 6506 int nextpos = TREE_INT_CST_LOW (nextpos_tree); 6507 6508 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled)); 6509 } 6510 } 6511 else 6512 { 6513 /* If it's not incremental, just skip over the gap, 6514 so that after jumping to retry we will output the next 6515 successive element. */ 6516 if (TREE_CODE (constructor_type) == RECORD_TYPE 6517 || TREE_CODE (constructor_type) == UNION_TYPE) 6518 constructor_unfilled_fields = next; 6519 else if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6520 { 6521 TREE_INT_CST_LOW (constructor_unfilled_index) 6522 = TREE_INT_CST_LOW (next); 6523 TREE_INT_CST_HIGH (constructor_unfilled_index) 6524 = TREE_INT_CST_HIGH (next); 6525 } 6526 } 6527 6528 /* ELT now points to the node in the pending tree with the next 6529 initializer to output. */ 6530 goto retry; 6531} 6532 6533/* Add one non-braced element to the current constructor level. 6534 This adjusts the current position within the constructor's type. 6535 This may also start or terminate implicit levels 6536 to handle a partly-braced initializer. 6537 6538 Once this has found the correct level for the new element, 6539 it calls output_init_element. 6540 6541 Note: if we are incrementally outputting this constructor, 6542 this function may be called with a null argument 6543 representing a sub-constructor that was already incrementally output. 6544 When that happens, we output nothing, but we do the bookkeeping 6545 to skip past that element of the current constructor. */ 6546 6547void 6548process_init_element (value) 6549 tree value; 6550{ 6551 tree orig_value = value; 6552 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST; 6553 6554 /* Handle superfluous braces around string cst as in 6555 char x[] = {"foo"}; */ 6556 if (string_flag 6557 && constructor_type 6558 && TREE_CODE (constructor_type) == ARRAY_TYPE 6559 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE 6560 && integer_zerop (constructor_unfilled_index)) 6561 { 6562 constructor_stack->replacement_value = value; 6563 return; 6564 } 6565 6566 if (constructor_stack->replacement_value != 0) 6567 { 6568 error_init ("excess elements in struct initializer%s", 6569 " after `%s'", NULL_PTR); 6570 return; 6571 } 6572 6573 /* Ignore elements of a brace group if it is entirely superfluous 6574 and has already been diagnosed. */ 6575 if (constructor_type == 0) 6576 return; 6577 6578 /* If we've exhausted any levels that didn't have braces, 6579 pop them now. */ 6580 while (constructor_stack->implicit) 6581 { 6582 if ((TREE_CODE (constructor_type) == RECORD_TYPE 6583 || TREE_CODE (constructor_type) == UNION_TYPE) 6584 && constructor_fields == 0) 6585 process_init_element (pop_init_level (1)); 6586 else if (TREE_CODE (constructor_type) == ARRAY_TYPE 6587 && (constructor_max_index == 0 6588 || tree_int_cst_lt (constructor_max_index, 6589 constructor_index))) 6590 process_init_element (pop_init_level (1)); 6591 else 6592 break; 6593 } 6594 6595 while (1) 6596 { 6597 if (TREE_CODE (constructor_type) == RECORD_TYPE) 6598 { 6599 tree fieldtype; 6600 enum tree_code fieldcode; 6601 6602 if (constructor_fields == 0) 6603 { 6604 pedwarn_init ("excess elements in struct initializer%s", 6605 " after `%s'", NULL_PTR); 6606 break; 6607 } 6608 6609 fieldtype = TREE_TYPE (constructor_fields); 6610 if (fieldtype != error_mark_node) 6611 fieldtype = TYPE_MAIN_VARIANT (fieldtype); 6612 fieldcode = TREE_CODE (fieldtype); 6613 6614 /* Accept a string constant to initialize a subarray. */ 6615 if (value != 0 6616 && fieldcode == ARRAY_TYPE 6617 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE 6618 && string_flag) 6619 value = orig_value; 6620 /* Otherwise, if we have come to a subaggregate, 6621 and we don't have an element of its type, push into it. */ 6622 else if (value != 0 && !constructor_no_implicit 6623 && value != error_mark_node 6624 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype 6625 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE 6626 || fieldcode == UNION_TYPE)) 6627 { 6628 push_init_level (1); 6629 continue; 6630 } 6631 6632 if (value) 6633 { 6634 push_member_name (constructor_fields); 6635 output_init_element (value, fieldtype, constructor_fields, 1); 6636 RESTORE_SPELLING_DEPTH (constructor_depth); 6637 } 6638 else 6639 /* Do the bookkeeping for an element that was 6640 directly output as a constructor. */ 6641 { 6642 /* For a record, keep track of end position of last field. */ 6643 tree temp = size_binop (PLUS_EXPR, 6644 DECL_FIELD_BITPOS (constructor_fields), 6645 DECL_SIZE (constructor_fields)); 6646 TREE_INT_CST_LOW (constructor_bit_index) 6647 = TREE_INT_CST_LOW (temp); 6648 TREE_INT_CST_HIGH (constructor_bit_index) 6649 = TREE_INT_CST_HIGH (temp); 6650 6651 constructor_unfilled_fields = TREE_CHAIN (constructor_fields); 6652 } 6653 6654 constructor_fields = TREE_CHAIN (constructor_fields); 6655 /* Skip any nameless bit fields at the beginning. */ 6656 while (constructor_fields != 0 6657 && DECL_C_BIT_FIELD (constructor_fields) 6658 && DECL_NAME (constructor_fields) == 0) 6659 constructor_fields = TREE_CHAIN (constructor_fields); 6660 break; 6661 } 6662 if (TREE_CODE (constructor_type) == UNION_TYPE) 6663 { 6664 tree fieldtype; 6665 enum tree_code fieldcode; 6666 6667 if (constructor_fields == 0) 6668 { 6669 pedwarn_init ("excess elements in union initializer%s", 6670 " after `%s'", NULL_PTR); 6671 break; 6672 } 6673 6674 fieldtype = TREE_TYPE (constructor_fields); 6675 if (fieldtype != error_mark_node) 6676 fieldtype = TYPE_MAIN_VARIANT (fieldtype); 6677 fieldcode = TREE_CODE (fieldtype); 6678 6679 /* Accept a string constant to initialize a subarray. */ 6680 if (value != 0 6681 && fieldcode == ARRAY_TYPE 6682 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE 6683 && string_flag) 6684 value = orig_value; 6685 /* Otherwise, if we have come to a subaggregate, 6686 and we don't have an element of its type, push into it. */ 6687 else if (value != 0 && !constructor_no_implicit 6688 && value != error_mark_node 6689 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype 6690 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE 6691 || fieldcode == UNION_TYPE)) 6692 { 6693 push_init_level (1); 6694 continue; 6695 } 6696 6697 if (value) 6698 { 6699 push_member_name (constructor_fields); 6700 output_init_element (value, fieldtype, constructor_fields, 1); 6701 RESTORE_SPELLING_DEPTH (constructor_depth); 6702 } 6703 else 6704 /* Do the bookkeeping for an element that was 6705 directly output as a constructor. */ 6706 { 6707 TREE_INT_CST_LOW (constructor_bit_index) 6708 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields)); 6709 TREE_INT_CST_HIGH (constructor_bit_index) 6710 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields)); 6711 6712 constructor_unfilled_fields = TREE_CHAIN (constructor_fields); 6713 } 6714 6715 constructor_fields = 0; 6716 break; 6717 } 6718 if (TREE_CODE (constructor_type) == ARRAY_TYPE) 6719 { 6720 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); 6721 enum tree_code eltcode = TREE_CODE (elttype); 6722 6723 /* Accept a string constant to initialize a subarray. */ 6724 if (value != 0 6725 && eltcode == ARRAY_TYPE 6726 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE 6727 && string_flag) 6728 value = orig_value; 6729 /* Otherwise, if we have come to a subaggregate, 6730 and we don't have an element of its type, push into it. */ 6731 else if (value != 0 && !constructor_no_implicit 6732 && value != error_mark_node 6733 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype 6734 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE 6735 || eltcode == UNION_TYPE)) 6736 { 6737 push_init_level (1); 6738 continue; 6739 } 6740 6741 if (constructor_max_index != 0 6742 && tree_int_cst_lt (constructor_max_index, constructor_index)) 6743 { 6744 pedwarn_init ("excess elements in array initializer%s", 6745 " after `%s'", NULL_PTR); 6746 break; 6747 } 6748 6749 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */ 6750 if (constructor_range_end) 6751 { 6752 if (constructor_max_index != 0 6753 && tree_int_cst_lt (constructor_max_index, 6754 constructor_range_end)) 6755 { 6756 pedwarn_init ("excess elements in array initializer%s", 6757 " after `%s'", NULL_PTR); 6758 TREE_INT_CST_HIGH (constructor_range_end) 6759 = TREE_INT_CST_HIGH (constructor_max_index); 6760 TREE_INT_CST_LOW (constructor_range_end) 6761 = TREE_INT_CST_LOW (constructor_max_index); 6762 } 6763 6764 value = save_expr (value); 6765 } 6766 6767 /* Now output the actual element. 6768 Ordinarily, output once. 6769 If there is a range, repeat it till we advance past the range. */ 6770 do 6771 { 6772 tree tem; 6773 6774 if (value) 6775 { 6776 push_array_bounds (TREE_INT_CST_LOW (constructor_index)); 6777 output_init_element (value, elttype, constructor_index, 1); 6778 RESTORE_SPELLING_DEPTH (constructor_depth); 6779 } 6780 6781 tem = size_binop (PLUS_EXPR, constructor_index, 6782 integer_one_node); 6783 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem); 6784 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem); 6785 6786 if (!value) 6787 /* If we are doing the bookkeeping for an element that was 6788 directly output as a constructor, 6789 we must update constructor_unfilled_index. */ 6790 { 6791 TREE_INT_CST_LOW (constructor_unfilled_index) 6792 = TREE_INT_CST_LOW (constructor_index); 6793 TREE_INT_CST_HIGH (constructor_unfilled_index) 6794 = TREE_INT_CST_HIGH (constructor_index); 6795 } 6796 } 6797 while (! (constructor_range_end == 0 6798 || tree_int_cst_lt (constructor_range_end, 6799 constructor_index))); 6800 6801 break; 6802 } 6803 6804 /* Handle the sole element allowed in a braced initializer 6805 for a scalar variable. */ 6806 if (constructor_fields == 0) 6807 { 6808 pedwarn_init ("excess elements in scalar initializer%s", 6809 " after `%s'", NULL_PTR); 6810 break; 6811 } 6812 6813 if (value) 6814 output_init_element (value, constructor_type, NULL_TREE, 1); 6815 constructor_fields = 0; 6816 break; 6817 } 6818 6819 /* If the (lexically) previous elments are not now saved, 6820 we can discard the storage for them. */ 6821 if (constructor_incremental && constructor_pending_elts == 0 && value != 0 6822 && constructor_stack == 0) 6823 clear_momentary (); 6824} 6825 6826/* Expand an ASM statement with operands, handling output operands 6827 that are not variables or INDIRECT_REFS by transforming such 6828 cases into cases that expand_asm_operands can handle. 6829 6830 Arguments are same as for expand_asm_operands. */ 6831 6832void 6833c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line) 6834 tree string, outputs, inputs, clobbers; 6835 int vol; 6836 char *filename; 6837 int line; 6838{ 6839 int noutputs = list_length (outputs); 6840 register int i; 6841 /* o[I] is the place that output number I should be written. */ 6842 register tree *o = (tree *) alloca (noutputs * sizeof (tree)); 6843 register tree tail; 6844 6845 if (TREE_CODE (string) == ADDR_EXPR) 6846 string = TREE_OPERAND (string, 0); 6847 if (TREE_CODE (string) != STRING_CST) 6848 { 6849 error ("asm template is not a string constant"); 6850 return; 6851 } 6852 6853 /* Record the contents of OUTPUTS before it is modified. */ 6854 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) 6855 o[i] = TREE_VALUE (tail); 6856 6857 /* Perform default conversions on array and function inputs. */ 6858 /* Don't do this for other types-- 6859 it would screw up operands expected to be in memory. */ 6860 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++) 6861 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE 6862 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE) 6863 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail)); 6864 6865 /* Generate the ASM_OPERANDS insn; 6866 store into the TREE_VALUEs of OUTPUTS some trees for 6867 where the values were actually stored. */ 6868 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line); 6869 6870 /* Copy all the intermediate outputs into the specified outputs. */ 6871 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++) 6872 { 6873 if (o[i] != TREE_VALUE (tail)) 6874 { 6875 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)), 6876 NULL_RTX, VOIDmode, EXPAND_NORMAL); 6877 free_temp_slots (); 6878 } 6879 /* Detect modification of read-only values. 6880 (Otherwise done by build_modify_expr.) */ 6881 else 6882 { 6883 tree type = TREE_TYPE (o[i]); 6884 if (TREE_READONLY (o[i]) 6885 || TYPE_READONLY (type) 6886 || ((TREE_CODE (type) == RECORD_TYPE 6887 || TREE_CODE (type) == UNION_TYPE) 6888 && C_TYPE_FIELDS_READONLY (type))) 6889 readonly_warning (o[i], "modification by `asm'"); 6890 } 6891 } 6892 6893 /* Those MODIFY_EXPRs could do autoincrements. */ 6894 emit_queue (); 6895} 6896 6897/* Expand a C `return' statement. 6898 RETVAL is the expression for what to return, 6899 or a null pointer for `return;' with no value. */ 6900 6901void 6902c_expand_return (retval) 6903 tree retval; 6904{ 6905 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)); 6906 6907 if (TREE_THIS_VOLATILE (current_function_decl)) 6908 warning ("function declared `noreturn' has a `return' statement"); 6909 6910 if (!retval) 6911 { 6912 current_function_returns_null = 1; 6913 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) 6914 warning ("`return' with no value, in function returning non-void"); 6915 expand_null_return (); 6916 } 6917 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) 6918 { 6919 current_function_returns_null = 1; 6920 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) 6921 pedwarn ("`return' with a value, in function returning void"); 6922 expand_return (retval); 6923 } 6924 else 6925 { 6926 tree t = convert_for_assignment (valtype, retval, "return", 6927 NULL_TREE, NULL_TREE, 0); 6928 tree res = DECL_RESULT (current_function_decl); 6929 tree inner; 6930 6931 if (t == error_mark_node) 6932 return; 6933 6934 inner = t = convert (TREE_TYPE (res), t); 6935 6936 /* Strip any conversions, additions, and subtractions, and see if 6937 we are returning the address of a local variable. Warn if so. */ 6938 while (1) 6939 { 6940 switch (TREE_CODE (inner)) 6941 { 6942 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR: 6943 case PLUS_EXPR: 6944 inner = TREE_OPERAND (inner, 0); 6945 continue; 6946 6947 case MINUS_EXPR: 6948 /* If the second operand of the MINUS_EXPR has a pointer 6949 type (or is converted from it), this may be valid, so 6950 don't give a warning. */ 6951 { 6952 tree op1 = TREE_OPERAND (inner, 1); 6953 6954 while (! POINTER_TYPE_P (TREE_TYPE (op1)) 6955 && (TREE_CODE (op1) == NOP_EXPR 6956 || TREE_CODE (op1) == NON_LVALUE_EXPR 6957 || TREE_CODE (op1) == CONVERT_EXPR)) 6958 op1 = TREE_OPERAND (op1, 0); 6959 6960 if (POINTER_TYPE_P (TREE_TYPE (op1))) 6961 break; 6962 6963 inner = TREE_OPERAND (inner, 0); 6964 continue; 6965 } 6966 6967 case ADDR_EXPR: 6968 inner = TREE_OPERAND (inner, 0); 6969 6970 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r') 6971 inner = TREE_OPERAND (inner, 0); 6972 6973 if (TREE_CODE (inner) == VAR_DECL 6974 && ! DECL_EXTERNAL (inner) 6975 && ! TREE_STATIC (inner) 6976 && DECL_CONTEXT (inner) == current_function_decl) 6977 warning ("function returns address of local variable"); 6978 break; 6979 6980 default: 6981 break; 6982 } 6983 6984 break; 6985 } 6986 6987 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t); 6988 TREE_SIDE_EFFECTS (t) = 1; 6989 expand_return (t); 6990 current_function_returns_value = 1; 6991 } 6992} 6993 6994/* Start a C switch statement, testing expression EXP. 6995 Return EXP if it is valid, an error node otherwise. */ 6996 6997tree 6998c_expand_start_case (exp) 6999 tree exp; 7000{ 7001 register enum tree_code code = TREE_CODE (TREE_TYPE (exp)); 7002 tree type = TREE_TYPE (exp); 7003 7004 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK) 7005 { 7006 error ("switch quantity not an integer"); 7007 exp = error_mark_node; 7008 } 7009 else 7010 { 7011 tree index; 7012 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); 7013 7014 if (warn_traditional 7015 && (type == long_integer_type_node 7016 || type == long_unsigned_type_node)) 7017 pedwarn ("`long' switch expression not converted to `int' in ANSI C"); 7018 7019 exp = default_conversion (exp); 7020 type = TREE_TYPE (exp); 7021 index = get_unwidened (exp, NULL_TREE); 7022 /* We can't strip a conversion from a signed type to an unsigned, 7023 because if we did, int_fits_type_p would do the wrong thing 7024 when checking case values for being in range, 7025 and it's too hard to do the right thing. */ 7026 if (TREE_UNSIGNED (TREE_TYPE (exp)) 7027 == TREE_UNSIGNED (TREE_TYPE (index))) 7028 exp = index; 7029 } 7030 7031 expand_start_case (1, exp, type, "switch statement"); 7032 7033 return exp; 7034} 7035