]>
Commit | Line | Data |
---|---|---|
628fa4f8 | 1 | /* Build expressions with type checking for C compiler. |
d585e4c9 | 2 | Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
fdd84b77 | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
babf52eb | 4 | Free Software Foundation, Inc. |
628fa4f8 | 5 | |
f12b58b3 | 6 | This file is part of GCC. |
628fa4f8 | 7 | |
f12b58b3 | 8 | GCC is free software; you can redistribute it and/or modify it under |
9 | the terms of the GNU General Public License as published by the Free | |
8c4c00c1 | 10 | Software Foundation; either version 3, or (at your option) any later |
f12b58b3 | 11 | version. |
628fa4f8 | 12 | |
f12b58b3 | 13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
628fa4f8 | 17 | |
18 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
628fa4f8 | 21 | |
22 | ||
23 | /* This file is part of the C front end. | |
24 | It contains routines to build C expressions given their operands, | |
25 | including computing the types of the result, C-specific error checks, | |
c857cd60 | 26 | and some optimization. */ |
628fa4f8 | 27 | |
28 | #include "config.h" | |
405711de | 29 | #include "system.h" |
805e22b2 | 30 | #include "coretypes.h" |
31 | #include "tm.h" | |
628fa4f8 | 32 | #include "tree.h" |
d1b92730 | 33 | #include "langhooks.h" |
628fa4f8 | 34 | #include "c-tree.h" |
874993a5 | 35 | #include "c-lang.h" |
628fa4f8 | 36 | #include "flags.h" |
be2828ce | 37 | #include "intl.h" |
a767736d | 38 | #include "target.h" |
2363ef00 | 39 | #include "tree-iterator.h" |
9b40bfbf | 40 | #include "bitmap.h" |
41 | #include "gimple.h" | |
6c536c4f | 42 | #include "c-family/c-objc.h" |
2363ef00 | 43 | |
245d6740 | 44 | /* Possible cases of implicit bad conversions. Used to select |
45 | diagnostic messages in convert_for_assignment. */ | |
46 | enum impl_conv { | |
47 | ic_argpass, | |
48 | ic_assign, | |
49 | ic_init, | |
50 | ic_return | |
51 | }; | |
52 | ||
7dfa155b | 53 | /* Possibe cases of scalar_to_vector conversion. */ |
54 | enum stv_conv { | |
55 | stv_error, /* Error occured. */ | |
56 | stv_nothing, /* Nothing happened. */ | |
57 | stv_firstarg, /* First argument must be expanded. */ | |
58 | stv_secondarg /* Second argument must be expanded. */ | |
59 | }; | |
60 | ||
9823d3a9 | 61 | /* The level of nesting inside "__alignof__". */ |
62 | int in_alignof; | |
63 | ||
64 | /* The level of nesting inside "sizeof". */ | |
65 | int in_sizeof; | |
66 | ||
67 | /* The level of nesting inside "typeof". */ | |
68 | int in_typeof; | |
628fa4f8 | 69 | |
89a5f6d1 | 70 | /* Nonzero if we've already printed a "missing braces around initializer" |
7de1d3ba | 71 | message within this initializer. */ |
89a5f6d1 | 72 | static int missing_braces_mentioned; |
7de1d3ba | 73 | |
16cb78b3 | 74 | static int require_constant_value; |
75 | static int require_constant_elements; | |
76 | ||
9f627b1a | 77 | static bool null_pointer_constant_p (const_tree); |
5a2784f8 | 78 | static tree qualify_type (tree, tree); |
ce3765bf | 79 | static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *, |
80 | bool *); | |
d5b637fa | 81 | static int comp_target_types (location_t, tree, tree); |
ce3765bf | 82 | static int function_types_compatible_p (const_tree, const_tree, bool *, |
83 | bool *); | |
84 | static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *); | |
5a2784f8 | 85 | static tree lookup_field (tree, tree); |
b9c74b4d | 86 | static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree, |
87 | tree); | |
389dd41b | 88 | static tree pointer_diff (location_t, tree, tree); |
d5b637fa | 89 | static tree convert_for_assignment (location_t, tree, tree, tree, |
90 | enum impl_conv, bool, tree, tree, int); | |
5a2784f8 | 91 | static tree valid_compound_expr_initializer (tree, tree); |
92 | static void push_string (const char *); | |
93 | static void push_member_name (tree); | |
5a2784f8 | 94 | static int spelling_length (void); |
95 | static char *print_spelling (char *); | |
6cbbbc89 | 96 | static void warning_init (int, const char *); |
e60a6f7b | 97 | static tree digest_init (location_t, tree, tree, tree, bool, bool, int); |
759791ee | 98 | static void output_init_element (tree, tree, bool, tree, tree, int, bool, |
99 | struct obstack *); | |
100 | static void output_pending_init_elements (int, struct obstack *); | |
101 | static int set_designator (int, struct obstack *); | |
102 | static void push_range_stack (tree, struct obstack *); | |
103 | static void add_pending_init (tree, tree, tree, bool, struct obstack *); | |
104 | static void set_nonincremental_init (struct obstack *); | |
105 | static void set_nonincremental_init_from_string (tree, struct obstack *); | |
106 | static tree find_init_member (tree, struct obstack *); | |
d4e60318 | 107 | static void readonly_warning (tree, enum lvalue_use); |
fdd84b77 | 108 | static int lvalue_or_else (location_t, const_tree, enum lvalue_use); |
91ffb709 | 109 | static void record_maybe_used_decl (tree); |
ce3765bf | 110 | static int comptypes_internal (const_tree, const_tree, bool *, bool *); |
79f925ed | 111 | \f |
112 | /* Return true if EXP is a null pointer constant, false otherwise. */ | |
113 | ||
114 | static bool | |
9f627b1a | 115 | null_pointer_constant_p (const_tree expr) |
79f925ed | 116 | { |
117 | /* This should really operate on c_expr structures, but they aren't | |
118 | yet available everywhere required. */ | |
119 | tree type = TREE_TYPE (expr); | |
120 | return (TREE_CODE (expr) == INTEGER_CST | |
ca24357c | 121 | && !TREE_OVERFLOW (expr) |
79f925ed | 122 | && integer_zerop (expr) |
123 | && (INTEGRAL_TYPE_P (type) | |
124 | || (TREE_CODE (type) == POINTER_TYPE | |
125 | && VOID_TYPE_P (TREE_TYPE (type)) | |
126 | && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); | |
127 | } | |
a75b1c71 | 128 | |
129 | /* EXPR may appear in an unevaluated part of an integer constant | |
130 | expression, but not in an evaluated part. Wrap it in a | |
131 | C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an | |
132 | INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ | |
133 | ||
134 | static tree | |
135 | note_integer_operands (tree expr) | |
136 | { | |
137 | tree ret; | |
138 | if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) | |
139 | { | |
140 | ret = copy_node (expr); | |
141 | TREE_OVERFLOW (ret) = 1; | |
142 | } | |
143 | else | |
144 | { | |
145 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); | |
146 | C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; | |
147 | } | |
148 | return ret; | |
149 | } | |
150 | ||
9a8bed72 | 151 | /* Having checked whether EXPR may appear in an unevaluated part of an |
152 | integer constant expression and found that it may, remove any | |
153 | C_MAYBE_CONST_EXPR noting this fact and return the resulting | |
154 | expression. */ | |
155 | ||
156 | static inline tree | |
157 | remove_c_maybe_const_expr (tree expr) | |
158 | { | |
159 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
160 | return C_MAYBE_CONST_EXPR_EXPR (expr); | |
161 | else | |
162 | return expr; | |
163 | } | |
164 | ||
7e58afa1 | 165 | \f/* This is a cache to hold if two types are compatible or not. */ |
166 | ||
167 | struct tagged_tu_seen_cache { | |
168 | const struct tagged_tu_seen_cache * next; | |
9f627b1a | 169 | const_tree t1; |
170 | const_tree t2; | |
7e58afa1 | 171 | /* The return value of tagged_types_tu_compatible_p if we had seen |
172 | these two types already. */ | |
173 | int val; | |
174 | }; | |
175 | ||
176 | static const struct tagged_tu_seen_cache * tagged_tu_seen_base; | |
177 | static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); | |
178 | ||
628fa4f8 | 179 | /* Do `exp = require_complete_type (exp);' to make sure exp |
180 | does not have an incomplete type. (That includes void types.) */ | |
181 | ||
182 | tree | |
2b30d46c | 183 | require_complete_type (tree value) |
628fa4f8 | 184 | { |
185 | tree type = TREE_TYPE (value); | |
186 | ||
5601fc98 | 187 | if (value == error_mark_node || type == error_mark_node) |
1e4f99e8 | 188 | return error_mark_node; |
189 | ||
628fa4f8 | 190 | /* First, detect a valid value with a complete type. */ |
4b72716d | 191 | if (COMPLETE_TYPE_P (type)) |
628fa4f8 | 192 | return value; |
193 | ||
1dd25100 | 194 | c_incomplete_type_error (value, type); |
628fa4f8 | 195 | return error_mark_node; |
196 | } | |
197 | ||
198 | /* Print an error message for invalid use of an incomplete type. | |
199 | VALUE is the expression that was used (or 0 if that isn't known) | |
200 | and TYPE is the type that was invalid. */ | |
201 | ||
202 | void | |
f8fd23c0 | 203 | c_incomplete_type_error (const_tree value, const_tree type) |
628fa4f8 | 204 | { |
ba1d8f67 | 205 | const char *type_code_string; |
628fa4f8 | 206 | |
207 | /* Avoid duplicate error message. */ | |
208 | if (TREE_CODE (type) == ERROR_MARK) | |
209 | return; | |
210 | ||
211 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
212 | || TREE_CODE (value) == PARM_DECL)) | |
782858b8 | 213 | error ("%qD has an incomplete type", value); |
628fa4f8 | 214 | else |
215 | { | |
216 | retry: | |
217 | /* We must print an error message. Be clever about what it says. */ | |
218 | ||
219 | switch (TREE_CODE (type)) | |
220 | { | |
221 | case RECORD_TYPE: | |
be2828ce | 222 | type_code_string = "struct"; |
628fa4f8 | 223 | break; |
224 | ||
225 | case UNION_TYPE: | |
be2828ce | 226 | type_code_string = "union"; |
628fa4f8 | 227 | break; |
228 | ||
229 | case ENUMERAL_TYPE: | |
be2828ce | 230 | type_code_string = "enum"; |
628fa4f8 | 231 | break; |
232 | ||
233 | case VOID_TYPE: | |
234 | error ("invalid use of void expression"); | |
235 | return; | |
236 | ||
237 | case ARRAY_TYPE: | |
238 | if (TYPE_DOMAIN (type)) | |
239 | { | |
e40a1d5c | 240 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) |
241 | { | |
242 | error ("invalid use of flexible array member"); | |
243 | return; | |
244 | } | |
628fa4f8 | 245 | type = TREE_TYPE (type); |
246 | goto retry; | |
247 | } | |
248 | error ("invalid use of array with unspecified bounds"); | |
249 | return; | |
250 | ||
251 | default: | |
231bd014 | 252 | gcc_unreachable (); |
628fa4f8 | 253 | } |
254 | ||
255 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
782858b8 | 256 | error ("invalid use of undefined type %<%s %E%>", |
257 | type_code_string, TYPE_NAME (type)); | |
628fa4f8 | 258 | else |
259 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
782858b8 | 260 | error ("invalid use of incomplete typedef %qD", TYPE_NAME (type)); |
628fa4f8 | 261 | } |
262 | } | |
263 | ||
63c62881 | 264 | /* Given a type, apply default promotions wrt unnamed function |
265 | arguments and return the new type. */ | |
266 | ||
267 | tree | |
2b30d46c | 268 | c_type_promotes_to (tree type) |
63c62881 | 269 | { |
270 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
271 | return double_type_node; | |
272 | ||
273 | if (c_promoting_integer_type_p (type)) | |
274 | { | |
275 | /* Preserve unsignedness if not really getting any wider. */ | |
78a8ed03 | 276 | if (TYPE_UNSIGNED (type) |
a0c938f0 | 277 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) |
278 | return unsigned_type_node; | |
63c62881 | 279 | return integer_type_node; |
280 | } | |
281 | ||
282 | return type; | |
283 | } | |
284 | ||
6d5d708e | 285 | /* Return true if between two named address spaces, whether there is a superset |
286 | named address space that encompasses both address spaces. If there is a | |
287 | superset, return which address space is the superset. */ | |
288 | ||
289 | static bool | |
290 | addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) | |
291 | { | |
292 | if (as1 == as2) | |
293 | { | |
294 | *common = as1; | |
295 | return true; | |
296 | } | |
297 | else if (targetm.addr_space.subset_p (as1, as2)) | |
298 | { | |
299 | *common = as2; | |
300 | return true; | |
301 | } | |
302 | else if (targetm.addr_space.subset_p (as2, as1)) | |
303 | { | |
304 | *common = as1; | |
305 | return true; | |
306 | } | |
307 | else | |
308 | return false; | |
309 | } | |
310 | ||
628fa4f8 | 311 | /* Return a variant of TYPE which has all the type qualifiers of LIKE |
312 | as well as those of TYPE. */ | |
313 | ||
314 | static tree | |
2b30d46c | 315 | qualify_type (tree type, tree like) |
628fa4f8 | 316 | { |
6d5d708e | 317 | addr_space_t as_type = TYPE_ADDR_SPACE (type); |
318 | addr_space_t as_like = TYPE_ADDR_SPACE (like); | |
319 | addr_space_t as_common; | |
320 | ||
321 | /* If the two named address spaces are different, determine the common | |
322 | superset address space. If there isn't one, raise an error. */ | |
323 | if (!addr_space_superset (as_type, as_like, &as_common)) | |
324 | { | |
325 | as_common = as_type; | |
326 | error ("%qT and %qT are in disjoint named address spaces", | |
327 | type, like); | |
328 | } | |
329 | ||
2b30d46c | 330 | return c_build_qualified_type (type, |
6d5d708e | 331 | TYPE_QUALS_NO_ADDR_SPACE (type) |
332 | | TYPE_QUALS_NO_ADDR_SPACE (like) | |
333 | | ENCODE_QUAL_ADDR_SPACE (as_common)); | |
628fa4f8 | 334 | } |
32d33ab2 | 335 | |
336 | /* Return true iff the given tree T is a variable length array. */ | |
337 | ||
338 | bool | |
f8fd23c0 | 339 | c_vla_type_p (const_tree t) |
32d33ab2 | 340 | { |
341 | if (TREE_CODE (t) == ARRAY_TYPE | |
342 | && C_TYPE_VARIABLE_SIZE (t)) | |
343 | return true; | |
344 | return false; | |
345 | } | |
628fa4f8 | 346 | \f |
1ea7fa60 | 347 | /* Return the composite type of two compatible types. |
3ac2c7b8 | 348 | |
1ea7fa60 | 349 | We assume that comptypes has already been done and returned |
350 | nonzero; if that isn't so, this may crash. In particular, we | |
351 | assume that qualifiers match. */ | |
628fa4f8 | 352 | |
353 | tree | |
1ea7fa60 | 354 | composite_type (tree t1, tree t2) |
628fa4f8 | 355 | { |
19cb6b50 | 356 | enum tree_code code1; |
357 | enum tree_code code2; | |
8fc98180 | 358 | tree attributes; |
628fa4f8 | 359 | |
360 | /* Save time if the two types are the same. */ | |
361 | ||
362 | if (t1 == t2) return t1; | |
363 | ||
364 | /* If one type is nonsense, use the other. */ | |
365 | if (t1 == error_mark_node) | |
366 | return t2; | |
367 | if (t2 == error_mark_node) | |
368 | return t1; | |
369 | ||
1ea7fa60 | 370 | code1 = TREE_CODE (t1); |
371 | code2 = TREE_CODE (t2); | |
372 | ||
0bf60c2b | 373 | /* Merge the attributes. */ |
883b2e73 | 374 | attributes = targetm.merge_type_attributes (t1, t2); |
8fc98180 | 375 | |
1ea7fa60 | 376 | /* If one is an enumerated type and the other is the compatible |
377 | integer type, the composite type might be either of the two | |
378 | (DR#013 question 3). For consistency, use the enumerated type as | |
379 | the composite type. */ | |
628fa4f8 | 380 | |
1ea7fa60 | 381 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) |
382 | return t1; | |
383 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
384 | return t2; | |
1d790105 | 385 | |
231bd014 | 386 | gcc_assert (code1 == code2); |
6b854be5 | 387 | |
628fa4f8 | 388 | switch (code1) |
389 | { | |
628fa4f8 | 390 | case POINTER_TYPE: |
1ea7fa60 | 391 | /* For two pointers, do this recursively on the target type. */ |
628fa4f8 | 392 | { |
a5b1863e | 393 | tree pointed_to_1 = TREE_TYPE (t1); |
394 | tree pointed_to_2 = TREE_TYPE (t2); | |
1ea7fa60 | 395 | tree target = composite_type (pointed_to_1, pointed_to_2); |
b589b3b4 | 396 | t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false); |
5be41d48 | 397 | t1 = build_type_attribute_variant (t1, attributes); |
398 | return qualify_type (t1, t2); | |
628fa4f8 | 399 | } |
628fa4f8 | 400 | |
401 | case ARRAY_TYPE: | |
402 | { | |
1ea7fa60 | 403 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
aebc8537 | 404 | int quals; |
405 | tree unqual_elt; | |
a2f96aea | 406 | tree d1 = TYPE_DOMAIN (t1); |
407 | tree d2 = TYPE_DOMAIN (t2); | |
408 | bool d1_variable, d2_variable; | |
409 | bool d1_zero, d2_zero; | |
e5d71db4 | 410 | bool t1_complete, t2_complete; |
aebc8537 | 411 | |
4e7c4787 | 412 | /* We should not have any type quals on arrays at all. */ |
6d5d708e | 413 | gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) |
414 | && !TYPE_QUALS_NO_ADDR_SPACE (t2)); | |
a0c938f0 | 415 | |
e5d71db4 | 416 | t1_complete = COMPLETE_TYPE_P (t1); |
417 | t2_complete = COMPLETE_TYPE_P (t2); | |
418 | ||
a2f96aea | 419 | d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); |
420 | d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); | |
421 | ||
422 | d1_variable = (!d1_zero | |
423 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
424 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
425 | d2_variable = (!d2_zero | |
426 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
427 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
32d33ab2 | 428 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
429 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
a2f96aea | 430 | |
628fa4f8 | 431 | /* Save space: see if the result is identical to one of the args. */ |
a2f96aea | 432 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) |
433 | && (d2_variable || d2_zero || !d1_variable)) | |
8fc98180 | 434 | return build_type_attribute_variant (t1, attributes); |
a2f96aea | 435 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) |
436 | && (d1_variable || d1_zero || !d2_variable)) | |
8fc98180 | 437 | return build_type_attribute_variant (t2, attributes); |
a0c938f0 | 438 | |
4e7c4787 | 439 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) |
440 | return build_type_attribute_variant (t1, attributes); | |
441 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
442 | return build_type_attribute_variant (t2, attributes); | |
a0c938f0 | 443 | |
aebc8537 | 444 | /* Merge the element types, and have a size if either arg has |
445 | one. We may have qualifiers on the element types. To set | |
446 | up TYPE_MAIN_VARIANT correctly, we need to form the | |
447 | composite of the unqualified types and add the qualifiers | |
448 | back at the end. */ | |
449 | quals = TYPE_QUALS (strip_array_types (elt)); | |
450 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
451 | t1 = build_array_type (unqual_elt, | |
a2f96aea | 452 | TYPE_DOMAIN ((TYPE_DOMAIN (t1) |
453 | && (d2_variable | |
454 | || d2_zero | |
455 | || !d1_variable)) | |
456 | ? t1 | |
457 | : t2)); | |
e5d71db4 | 458 | /* Ensure a composite type involving a zero-length array type |
459 | is a zero-length type not an incomplete type. */ | |
460 | if (d1_zero && d2_zero | |
461 | && (t1_complete || t2_complete) | |
462 | && !COMPLETE_TYPE_P (t1)) | |
463 | { | |
464 | TYPE_SIZE (t1) = bitsize_zero_node; | |
465 | TYPE_SIZE_UNIT (t1) = size_zero_node; | |
466 | } | |
aebc8537 | 467 | t1 = c_build_qualified_type (t1, quals); |
4e7c4787 | 468 | return build_type_attribute_variant (t1, attributes); |
628fa4f8 | 469 | } |
470 | ||
9369a09f | 471 | case ENUMERAL_TYPE: |
472 | case RECORD_TYPE: | |
473 | case UNION_TYPE: | |
474 | if (attributes != NULL) | |
475 | { | |
476 | /* Try harder not to create a new aggregate type. */ | |
477 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
478 | return t1; | |
479 | if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
480 | return t2; | |
481 | } | |
482 | return build_type_attribute_variant (t1, attributes); | |
483 | ||
628fa4f8 | 484 | case FUNCTION_TYPE: |
485 | /* Function types: prefer the one that specified arg types. | |
486 | If both do, merge the arg types. Also merge the return types. */ | |
487 | { | |
1ea7fa60 | 488 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
628fa4f8 | 489 | tree p1 = TYPE_ARG_TYPES (t1); |
490 | tree p2 = TYPE_ARG_TYPES (t2); | |
491 | int len; | |
492 | tree newargs, n; | |
493 | int i; | |
494 | ||
495 | /* Save space: see if the result is identical to one of the args. */ | |
84166705 | 496 | if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) |
8fc98180 | 497 | return build_type_attribute_variant (t1, attributes); |
84166705 | 498 | if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) |
8fc98180 | 499 | return build_type_attribute_variant (t2, attributes); |
628fa4f8 | 500 | |
501 | /* Simple way if one arg fails to specify argument types. */ | |
502 | if (TYPE_ARG_TYPES (t1) == 0) | |
8fc98180 | 503 | { |
5be41d48 | 504 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); |
505 | t1 = build_type_attribute_variant (t1, attributes); | |
506 | return qualify_type (t1, t2); | |
8fc98180 | 507 | } |
628fa4f8 | 508 | if (TYPE_ARG_TYPES (t2) == 0) |
8fc98180 | 509 | { |
510 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
5be41d48 | 511 | t1 = build_type_attribute_variant (t1, attributes); |
512 | return qualify_type (t1, t2); | |
8fc98180 | 513 | } |
628fa4f8 | 514 | |
515 | /* If both args specify argument types, we must merge the two | |
516 | lists, argument by argument. */ | |
6e11a41e | 517 | |
628fa4f8 | 518 | len = list_length (p1); |
519 | newargs = 0; | |
520 | ||
521 | for (i = 0; i < len; i++) | |
7224cf20 | 522 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
628fa4f8 | 523 | |
524 | n = newargs; | |
525 | ||
526 | for (; p1; | |
527 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
528 | { | |
529 | /* A null type means arg type is not specified. | |
530 | Take whatever the other function type has. */ | |
531 | if (TREE_VALUE (p1) == 0) | |
532 | { | |
533 | TREE_VALUE (n) = TREE_VALUE (p2); | |
534 | goto parm_done; | |
535 | } | |
536 | if (TREE_VALUE (p2) == 0) | |
537 | { | |
538 | TREE_VALUE (n) = TREE_VALUE (p1); | |
539 | goto parm_done; | |
540 | } | |
2b30d46c | 541 | |
628fa4f8 | 542 | /* Given wait (union {union wait *u; int *i} *) |
543 | and wait (union wait *), | |
544 | prefer union wait * as type of parm. */ | |
545 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
546 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
547 | { | |
548 | tree memb; | |
c75da5d5 | 549 | tree mv2 = TREE_VALUE (p2); |
550 | if (mv2 && mv2 != error_mark_node | |
551 | && TREE_CODE (mv2) != ARRAY_TYPE) | |
552 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
628fa4f8 | 553 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); |
1767a056 | 554 | memb; memb = DECL_CHAIN (memb)) |
c75da5d5 | 555 | { |
556 | tree mv3 = TREE_TYPE (memb); | |
557 | if (mv3 && mv3 != error_mark_node | |
558 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
559 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
560 | if (comptypes (mv3, mv2)) | |
561 | { | |
562 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
563 | TREE_VALUE (p2)); | |
29438999 | 564 | pedwarn (input_location, OPT_Wpedantic, |
8864917d | 565 | "function types not truly compatible in ISO C"); |
c75da5d5 | 566 | goto parm_done; |
567 | } | |
568 | } | |
628fa4f8 | 569 | } |
570 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
571 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
572 | { | |
573 | tree memb; | |
c75da5d5 | 574 | tree mv1 = TREE_VALUE (p1); |
575 | if (mv1 && mv1 != error_mark_node | |
576 | && TREE_CODE (mv1) != ARRAY_TYPE) | |
577 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
628fa4f8 | 578 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); |
1767a056 | 579 | memb; memb = DECL_CHAIN (memb)) |
c75da5d5 | 580 | { |
581 | tree mv3 = TREE_TYPE (memb); | |
582 | if (mv3 && mv3 != error_mark_node | |
583 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
584 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
585 | if (comptypes (mv3, mv1)) | |
586 | { | |
587 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
588 | TREE_VALUE (p1)); | |
29438999 | 589 | pedwarn (input_location, OPT_Wpedantic, |
8864917d | 590 | "function types not truly compatible in ISO C"); |
c75da5d5 | 591 | goto parm_done; |
592 | } | |
593 | } | |
628fa4f8 | 594 | } |
1ea7fa60 | 595 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); |
628fa4f8 | 596 | parm_done: ; |
597 | } | |
598 | ||
8fc98180 | 599 | t1 = build_function_type (valtype, newargs); |
5be41d48 | 600 | t1 = qualify_type (t1, t2); |
a92771b8 | 601 | /* ... falls through ... */ |
628fa4f8 | 602 | } |
603 | ||
604 | default: | |
8fc98180 | 605 | return build_type_attribute_variant (t1, attributes); |
628fa4f8 | 606 | } |
607 | ||
608 | } | |
1ea7fa60 | 609 | |
610 | /* Return the type of a conditional expression between pointers to | |
611 | possibly differently qualified versions of compatible types. | |
612 | ||
613 | We assume that comp_target_types has already been done and returned | |
614 | nonzero; if that isn't so, this may crash. */ | |
615 | ||
616 | static tree | |
617 | common_pointer_type (tree t1, tree t2) | |
618 | { | |
619 | tree attributes; | |
aebc8537 | 620 | tree pointed_to_1, mv1; |
621 | tree pointed_to_2, mv2; | |
1ea7fa60 | 622 | tree target; |
36d43c4a | 623 | unsigned target_quals; |
6d5d708e | 624 | addr_space_t as1, as2, as_common; |
625 | int quals1, quals2; | |
1ea7fa60 | 626 | |
627 | /* Save time if the two types are the same. */ | |
628 | ||
629 | if (t1 == t2) return t1; | |
630 | ||
631 | /* If one type is nonsense, use the other. */ | |
632 | if (t1 == error_mark_node) | |
633 | return t2; | |
634 | if (t2 == error_mark_node) | |
635 | return t1; | |
636 | ||
231bd014 | 637 | gcc_assert (TREE_CODE (t1) == POINTER_TYPE |
a0c938f0 | 638 | && TREE_CODE (t2) == POINTER_TYPE); |
1ea7fa60 | 639 | |
640 | /* Merge the attributes. */ | |
641 | attributes = targetm.merge_type_attributes (t1, t2); | |
642 | ||
643 | /* Find the composite type of the target types, and combine the | |
aebc8537 | 644 | qualifiers of the two types' targets. Do not lose qualifiers on |
645 | array element types by taking the TYPE_MAIN_VARIANT. */ | |
646 | mv1 = pointed_to_1 = TREE_TYPE (t1); | |
647 | mv2 = pointed_to_2 = TREE_TYPE (t2); | |
648 | if (TREE_CODE (mv1) != ARRAY_TYPE) | |
649 | mv1 = TYPE_MAIN_VARIANT (pointed_to_1); | |
650 | if (TREE_CODE (mv2) != ARRAY_TYPE) | |
651 | mv2 = TYPE_MAIN_VARIANT (pointed_to_2); | |
652 | target = composite_type (mv1, mv2); | |
36d43c4a | 653 | |
654 | /* For function types do not merge const qualifiers, but drop them | |
655 | if used inconsistently. The middle-end uses these to mark const | |
656 | and noreturn functions. */ | |
6d5d708e | 657 | quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1); |
658 | quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2); | |
659 | ||
36d43c4a | 660 | if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) |
6d5d708e | 661 | target_quals = (quals1 & quals2); |
36d43c4a | 662 | else |
6d5d708e | 663 | target_quals = (quals1 | quals2); |
664 | ||
665 | /* If the two named address spaces are different, determine the common | |
666 | superset address space. This is guaranteed to exist due to the | |
667 | assumption that comp_target_type returned non-zero. */ | |
668 | as1 = TYPE_ADDR_SPACE (pointed_to_1); | |
669 | as2 = TYPE_ADDR_SPACE (pointed_to_2); | |
670 | if (!addr_space_superset (as1, as2, &as_common)) | |
671 | gcc_unreachable (); | |
672 | ||
673 | target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); | |
674 | ||
36d43c4a | 675 | t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); |
1ea7fa60 | 676 | return build_type_attribute_variant (t1, attributes); |
677 | } | |
678 | ||
679 | /* Return the common type for two arithmetic types under the usual | |
680 | arithmetic conversions. The default conversions have already been | |
681 | applied, and enumerated types converted to their compatible integer | |
682 | types. The resulting type is unqualified and has no attributes. | |
683 | ||
684 | This is the type for the result of most arithmetic operations | |
685 | if the operands have the given two types. */ | |
686 | ||
79d37242 | 687 | static tree |
688 | c_common_type (tree t1, tree t2) | |
1ea7fa60 | 689 | { |
690 | enum tree_code code1; | |
691 | enum tree_code code2; | |
692 | ||
693 | /* If one type is nonsense, use the other. */ | |
694 | if (t1 == error_mark_node) | |
695 | return t2; | |
696 | if (t2 == error_mark_node) | |
697 | return t1; | |
698 | ||
699 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
700 | t1 = TYPE_MAIN_VARIANT (t1); | |
701 | ||
702 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
703 | t2 = TYPE_MAIN_VARIANT (t2); | |
704 | ||
705 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
706 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
707 | ||
708 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
709 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
710 | ||
711 | /* Save time if the two types are the same. */ | |
712 | ||
713 | if (t1 == t2) return t1; | |
714 | ||
715 | code1 = TREE_CODE (t1); | |
716 | code2 = TREE_CODE (t2); | |
717 | ||
231bd014 | 718 | gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE |
9421ebb9 | 719 | || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE |
720 | || code1 == INTEGER_TYPE); | |
231bd014 | 721 | gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE |
9421ebb9 | 722 | || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE |
723 | || code2 == INTEGER_TYPE); | |
1ea7fa60 | 724 | |
e5d0ba06 | 725 | /* When one operand is a decimal float type, the other operand cannot be |
726 | a generic float type or a complex type. We also disallow vector types | |
727 | here. */ | |
728 | if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) | |
729 | && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) | |
730 | { | |
731 | if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) | |
732 | { | |
733 | error ("can%'t mix operands of decimal float and vector types"); | |
734 | return error_mark_node; | |
735 | } | |
736 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
737 | { | |
738 | error ("can%'t mix operands of decimal float and complex types"); | |
739 | return error_mark_node; | |
740 | } | |
741 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
742 | { | |
743 | error ("can%'t mix operands of decimal float and other float types"); | |
744 | return error_mark_node; | |
745 | } | |
746 | } | |
747 | ||
1ea7fa60 | 748 | /* If one type is a vector type, return that type. (How the usual |
749 | arithmetic conversions apply to the vector types extension is not | |
750 | precisely specified.) */ | |
751 | if (code1 == VECTOR_TYPE) | |
752 | return t1; | |
753 | ||
754 | if (code2 == VECTOR_TYPE) | |
755 | return t2; | |
756 | ||
757 | /* If one type is complex, form the common type of the non-complex | |
758 | components, then make that complex. Use T1 or T2 if it is the | |
759 | required type. */ | |
760 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
761 | { | |
762 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
763 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
79d37242 | 764 | tree subtype = c_common_type (subtype1, subtype2); |
1ea7fa60 | 765 | |
766 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
767 | return t1; | |
768 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
769 | return t2; | |
770 | else | |
771 | return build_complex_type (subtype); | |
772 | } | |
773 | ||
774 | /* If only one is real, use it as the result. */ | |
775 | ||
776 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
777 | return t1; | |
778 | ||
779 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
780 | return t2; | |
781 | ||
c4503c0a | 782 | /* If both are real and either are decimal floating point types, use |
783 | the decimal floating point type with the greater precision. */ | |
784 | ||
785 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
786 | { | |
787 | if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node | |
788 | || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) | |
789 | return dfloat128_type_node; | |
790 | else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node | |
791 | || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) | |
792 | return dfloat64_type_node; | |
793 | else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node | |
794 | || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) | |
795 | return dfloat32_type_node; | |
796 | } | |
797 | ||
9421ebb9 | 798 | /* Deal with fixed-point types. */ |
799 | if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) | |
800 | { | |
801 | unsigned int unsignedp = 0, satp = 0; | |
802 | enum machine_mode m1, m2; | |
803 | unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; | |
804 | ||
805 | m1 = TYPE_MODE (t1); | |
806 | m2 = TYPE_MODE (t2); | |
807 | ||
808 | /* If one input type is saturating, the result type is saturating. */ | |
809 | if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) | |
810 | satp = 1; | |
811 | ||
812 | /* If both fixed-point types are unsigned, the result type is unsigned. | |
813 | When mixing fixed-point and integer types, follow the sign of the | |
814 | fixed-point type. | |
815 | Otherwise, the result type is signed. */ | |
816 | if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) | |
817 | && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) | |
818 | || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE | |
819 | && TYPE_UNSIGNED (t1)) | |
820 | || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE | |
821 | && TYPE_UNSIGNED (t2))) | |
822 | unsignedp = 1; | |
823 | ||
824 | /* The result type is signed. */ | |
825 | if (unsignedp == 0) | |
826 | { | |
827 | /* If the input type is unsigned, we need to convert to the | |
828 | signed type. */ | |
829 | if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) | |
830 | { | |
7d339f93 | 831 | enum mode_class mclass = (enum mode_class) 0; |
9421ebb9 | 832 | if (GET_MODE_CLASS (m1) == MODE_UFRACT) |
833 | mclass = MODE_FRACT; | |
834 | else if (GET_MODE_CLASS (m1) == MODE_UACCUM) | |
835 | mclass = MODE_ACCUM; | |
836 | else | |
837 | gcc_unreachable (); | |
838 | m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); | |
839 | } | |
840 | if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) | |
841 | { | |
7d339f93 | 842 | enum mode_class mclass = (enum mode_class) 0; |
9421ebb9 | 843 | if (GET_MODE_CLASS (m2) == MODE_UFRACT) |
844 | mclass = MODE_FRACT; | |
845 | else if (GET_MODE_CLASS (m2) == MODE_UACCUM) | |
846 | mclass = MODE_ACCUM; | |
847 | else | |
848 | gcc_unreachable (); | |
849 | m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); | |
850 | } | |
851 | } | |
852 | ||
853 | if (code1 == FIXED_POINT_TYPE) | |
854 | { | |
855 | fbit1 = GET_MODE_FBIT (m1); | |
856 | ibit1 = GET_MODE_IBIT (m1); | |
857 | } | |
858 | else | |
859 | { | |
860 | fbit1 = 0; | |
861 | /* Signed integers need to subtract one sign bit. */ | |
862 | ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); | |
863 | } | |
864 | ||
865 | if (code2 == FIXED_POINT_TYPE) | |
866 | { | |
867 | fbit2 = GET_MODE_FBIT (m2); | |
868 | ibit2 = GET_MODE_IBIT (m2); | |
869 | } | |
870 | else | |
871 | { | |
872 | fbit2 = 0; | |
873 | /* Signed integers need to subtract one sign bit. */ | |
874 | ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); | |
875 | } | |
876 | ||
877 | max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; | |
878 | max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; | |
879 | return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, | |
880 | satp); | |
881 | } | |
882 | ||
1ea7fa60 | 883 | /* Both real or both integers; use the one with greater precision. */ |
884 | ||
885 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
886 | return t1; | |
887 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
888 | return t2; | |
889 | ||
890 | /* Same precision. Prefer long longs to longs to ints when the | |
891 | same precision, following the C99 rules on integer type rank | |
892 | (which are equivalent to the C90 rules for C90 types). */ | |
893 | ||
894 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
895 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
896 | return long_long_unsigned_type_node; | |
897 | ||
898 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
899 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
900 | { | |
901 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
902 | return long_long_unsigned_type_node; | |
903 | else | |
a0c938f0 | 904 | return long_long_integer_type_node; |
1ea7fa60 | 905 | } |
906 | ||
907 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
908 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
909 | return long_unsigned_type_node; | |
910 | ||
911 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
912 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
913 | { | |
914 | /* But preserve unsignedness from the other type, | |
915 | since long cannot hold all the values of an unsigned int. */ | |
916 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
917 | return long_unsigned_type_node; | |
918 | else | |
919 | return long_integer_type_node; | |
920 | } | |
921 | ||
922 | /* Likewise, prefer long double to double even if same size. */ | |
923 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
924 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
925 | return long_double_type_node; | |
926 | ||
927 | /* Otherwise prefer the unsigned one. */ | |
928 | ||
929 | if (TYPE_UNSIGNED (t1)) | |
930 | return t1; | |
931 | else | |
932 | return t2; | |
933 | } | |
628fa4f8 | 934 | \f |
7422cd7b | 935 | /* Wrapper around c_common_type that is used by c-common.c and other |
936 | front end optimizations that remove promotions. ENUMERAL_TYPEs | |
c3a8afb5 | 937 | are allowed here and are converted to their compatible integer types. |
938 | BOOLEAN_TYPEs are allowed here and return either boolean_type_node or | |
939 | preferably a non-Boolean type as the common type. */ | |
79d37242 | 940 | tree |
941 | common_type (tree t1, tree t2) | |
942 | { | |
943 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
944 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); | |
945 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
946 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); | |
c3a8afb5 | 947 | |
948 | /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ | |
949 | if (TREE_CODE (t1) == BOOLEAN_TYPE | |
950 | && TREE_CODE (t2) == BOOLEAN_TYPE) | |
951 | return boolean_type_node; | |
952 | ||
953 | /* If either type is BOOLEAN_TYPE, then return the other. */ | |
954 | if (TREE_CODE (t1) == BOOLEAN_TYPE) | |
955 | return t2; | |
956 | if (TREE_CODE (t2) == BOOLEAN_TYPE) | |
957 | return t1; | |
958 | ||
79d37242 | 959 | return c_common_type (t1, t2); |
960 | } | |
7e58afa1 | 961 | |
628fa4f8 | 962 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
963 | or various other operations. Return 2 if they are compatible | |
964 | but a warning may be needed if you use them together. */ | |
965 | ||
966 | int | |
3ed275a6 | 967 | comptypes (tree type1, tree type2) |
7e58afa1 | 968 | { |
969 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
970 | int val; | |
971 | ||
ce3765bf | 972 | val = comptypes_internal (type1, type2, NULL, NULL); |
d5b637fa | 973 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
974 | ||
975 | return val; | |
976 | } | |
977 | ||
978 | /* Like comptypes, but if it returns non-zero because enum and int are | |
979 | compatible, it sets *ENUM_AND_INT_P to true. */ | |
980 | ||
981 | static int | |
982 | comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) | |
983 | { | |
984 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
985 | int val; | |
986 | ||
ce3765bf | 987 | val = comptypes_internal (type1, type2, enum_and_int_p, NULL); |
988 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
989 | ||
990 | return val; | |
991 | } | |
992 | ||
993 | /* Like comptypes, but if it returns nonzero for different types, it | |
994 | sets *DIFFERENT_TYPES_P to true. */ | |
995 | ||
996 | int | |
997 | comptypes_check_different_types (tree type1, tree type2, | |
998 | bool *different_types_p) | |
999 | { | |
1000 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1001 | int val; | |
1002 | ||
1003 | val = comptypes_internal (type1, type2, NULL, different_types_p); | |
7e58afa1 | 1004 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
a0c938f0 | 1005 | |
7e58afa1 | 1006 | return val; |
a0c938f0 | 1007 | } |
1008 | \f | |
7e58afa1 | 1009 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
1010 | or various other operations. Return 2 if they are compatible | |
d5b637fa | 1011 | but a warning may be needed if you use them together. If |
1012 | ENUM_AND_INT_P is not NULL, and one type is an enum and the other a | |
1013 | compatible integer type, then this sets *ENUM_AND_INT_P to true; | |
ce3765bf | 1014 | *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not |
1015 | NULL, and the types are compatible but different enough not to be | |
32074525 | 1016 | permitted in C11 typedef redeclarations, then this sets |
ce3765bf | 1017 | *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to |
1018 | false, but may or may not be set if the types are incompatible. | |
1019 | This differs from comptypes, in that we don't free the seen | |
1020 | types. */ | |
7e58afa1 | 1021 | |
1022 | static int | |
ce3765bf | 1023 | comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, |
1024 | bool *different_types_p) | |
628fa4f8 | 1025 | { |
9f627b1a | 1026 | const_tree t1 = type1; |
1027 | const_tree t2 = type2; | |
8fc98180 | 1028 | int attrval, val; |
628fa4f8 | 1029 | |
1030 | /* Suppress errors caused by previously reported errors. */ | |
1031 | ||
6b4e4ff6 | 1032 | if (t1 == t2 || !t1 || !t2 |
1033 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
628fa4f8 | 1034 | return 1; |
1035 | ||
09e0a2a1 | 1036 | /* Enumerated types are compatible with integer types, but this is |
1037 | not transitive: two enumerated types in the same translation unit | |
1038 | are compatible with each other only if they are the same type. */ | |
628fa4f8 | 1039 | |
09e0a2a1 | 1040 | if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) |
d5b637fa | 1041 | { |
1042 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); | |
ce3765bf | 1043 | if (TREE_CODE (t2) != VOID_TYPE) |
1044 | { | |
1045 | if (enum_and_int_p != NULL) | |
1046 | *enum_and_int_p = true; | |
1047 | if (different_types_p != NULL) | |
1048 | *different_types_p = true; | |
1049 | } | |
d5b637fa | 1050 | } |
09e0a2a1 | 1051 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) |
d5b637fa | 1052 | { |
1053 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); | |
ce3765bf | 1054 | if (TREE_CODE (t1) != VOID_TYPE) |
1055 | { | |
1056 | if (enum_and_int_p != NULL) | |
1057 | *enum_and_int_p = true; | |
1058 | if (different_types_p != NULL) | |
1059 | *different_types_p = true; | |
1060 | } | |
d5b637fa | 1061 | } |
628fa4f8 | 1062 | |
1063 | if (t1 == t2) | |
1064 | return 1; | |
1065 | ||
1066 | /* Different classes of types can't be compatible. */ | |
1067 | ||
a5d9b222 | 1068 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
1069 | return 0; | |
628fa4f8 | 1070 | |
14338e9a | 1071 | /* Qualifiers must match. C99 6.7.3p9 */ |
628fa4f8 | 1072 | |
a5b1863e | 1073 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
628fa4f8 | 1074 | return 0; |
1075 | ||
2201c2d1 | 1076 | /* Allow for two different type nodes which have essentially the same |
1077 | definition. Note that we already checked for equality of the type | |
3398e91d | 1078 | qualifiers (just above). */ |
628fa4f8 | 1079 | |
aebc8537 | 1080 | if (TREE_CODE (t1) != ARRAY_TYPE |
1081 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
628fa4f8 | 1082 | return 1; |
1083 | ||
8fc98180 | 1084 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ |
309303cf | 1085 | if (!(attrval = comp_type_attributes (t1, t2))) |
8fc98180 | 1086 | return 0; |
1087 | ||
1088 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1089 | val = 0; | |
1090 | ||
628fa4f8 | 1091 | switch (TREE_CODE (t1)) |
1092 | { | |
1093 | case POINTER_TYPE: | |
4cb9d5c8 | 1094 | /* Do not remove mode or aliasing information. */ |
1095 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1096 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) | |
1097 | break; | |
8fc98180 | 1098 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
d5b637fa | 1099 | ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
ce3765bf | 1100 | enum_and_int_p, different_types_p)); |
8fc98180 | 1101 | break; |
628fa4f8 | 1102 | |
1103 | case FUNCTION_TYPE: | |
ce3765bf | 1104 | val = function_types_compatible_p (t1, t2, enum_and_int_p, |
1105 | different_types_p); | |
8fc98180 | 1106 | break; |
628fa4f8 | 1107 | |
1108 | case ARRAY_TYPE: | |
1109 | { | |
628fa4f8 | 1110 | tree d1 = TYPE_DOMAIN (t1); |
1111 | tree d2 = TYPE_DOMAIN (t2); | |
2af9221e | 1112 | bool d1_variable, d2_variable; |
1113 | bool d1_zero, d2_zero; | |
8fc98180 | 1114 | val = 1; |
628fa4f8 | 1115 | |
1116 | /* Target types must match incl. qualifiers. */ | |
1117 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
d5b637fa | 1118 | && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
ce3765bf | 1119 | enum_and_int_p, |
1120 | different_types_p))) | |
628fa4f8 | 1121 | return 0; |
1122 | ||
ce3765bf | 1123 | if (different_types_p != NULL |
1124 | && (d1 == 0) != (d2 == 0)) | |
1125 | *different_types_p = true; | |
628fa4f8 | 1126 | /* Sizes must match unless one is missing or variable. */ |
2af9221e | 1127 | if (d1 == 0 || d2 == 0 || d1 == d2) |
8fc98180 | 1128 | break; |
628fa4f8 | 1129 | |
84166705 | 1130 | d1_zero = !TYPE_MAX_VALUE (d1); |
1131 | d2_zero = !TYPE_MAX_VALUE (d2); | |
2af9221e | 1132 | |
84166705 | 1133 | d1_variable = (!d1_zero |
2af9221e | 1134 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST |
1135 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
84166705 | 1136 | d2_variable = (!d2_zero |
2af9221e | 1137 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST |
1138 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
32d33ab2 | 1139 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
1140 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
2af9221e | 1141 | |
ce3765bf | 1142 | if (different_types_p != NULL |
1143 | && d1_variable != d2_variable) | |
1144 | *different_types_p = true; | |
2af9221e | 1145 | if (d1_variable || d2_variable) |
1146 | break; | |
1147 | if (d1_zero && d2_zero) | |
1148 | break; | |
1149 | if (d1_zero || d2_zero | |
84166705 | 1150 | || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) |
1151 | || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) | |
a0c2c45b | 1152 | val = 0; |
1153 | ||
a0c938f0 | 1154 | break; |
628fa4f8 | 1155 | } |
1156 | ||
40109983 | 1157 | case ENUMERAL_TYPE: |
34d3c5de | 1158 | case RECORD_TYPE: |
40109983 | 1159 | case UNION_TYPE: |
76c16586 | 1160 | if (val != 1 && !same_translation_unit_p (t1, t2)) |
a0c938f0 | 1161 | { |
9369a09f | 1162 | tree a1 = TYPE_ATTRIBUTES (t1); |
1163 | tree a2 = TYPE_ATTRIBUTES (t2); | |
1164 | ||
1165 | if (! attribute_list_contained (a1, a2) | |
1166 | && ! attribute_list_contained (a2, a1)) | |
1167 | break; | |
1168 | ||
7e58afa1 | 1169 | if (attrval != 2) |
ce3765bf | 1170 | return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, |
1171 | different_types_p); | |
1172 | val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, | |
1173 | different_types_p); | |
7e58afa1 | 1174 | } |
8fc98180 | 1175 | break; |
0dbd1c74 | 1176 | |
5050b2f7 | 1177 | case VECTOR_TYPE: |
d5b637fa | 1178 | val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) |
1179 | && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
ce3765bf | 1180 | enum_and_int_p, different_types_p)); |
5050b2f7 | 1181 | break; |
1182 | ||
0dbd1c74 | 1183 | default: |
1184 | break; | |
628fa4f8 | 1185 | } |
8fc98180 | 1186 | return attrval == 2 && val == 1 ? 2 : val; |
628fa4f8 | 1187 | } |
1188 | ||
6d5d708e | 1189 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring |
1190 | their qualifiers, except for named address spaces. If the pointers point to | |
1191 | different named addresses, then we must determine if one address space is a | |
1192 | subset of the other. */ | |
628fa4f8 | 1193 | |
1194 | static int | |
d5b637fa | 1195 | comp_target_types (location_t location, tree ttl, tree ttr) |
628fa4f8 | 1196 | { |
fadbd30d | 1197 | int val; |
6d5d708e | 1198 | tree mvl = TREE_TYPE (ttl); |
1199 | tree mvr = TREE_TYPE (ttr); | |
1200 | addr_space_t asl = TYPE_ADDR_SPACE (mvl); | |
1201 | addr_space_t asr = TYPE_ADDR_SPACE (mvr); | |
1202 | addr_space_t as_common; | |
d5b637fa | 1203 | bool enum_and_int_p; |
ea5fb8f9 | 1204 | |
6d5d708e | 1205 | /* Fail if pointers point to incompatible address spaces. */ |
1206 | if (!addr_space_superset (asl, asr, &as_common)) | |
1207 | return 0; | |
1208 | ||
aebc8537 | 1209 | /* Do not lose qualifiers on element types of array types that are |
1210 | pointer targets by taking their TYPE_MAIN_VARIANT. */ | |
aebc8537 | 1211 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
1212 | mvl = TYPE_MAIN_VARIANT (mvl); | |
1213 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
1214 | mvr = TYPE_MAIN_VARIANT (mvr); | |
d5b637fa | 1215 | enum_and_int_p = false; |
1216 | val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); | |
ea5fb8f9 | 1217 | |
8864917d | 1218 | if (val == 2) |
29438999 | 1219 | pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); |
d5b637fa | 1220 | |
1221 | if (val == 1 && enum_and_int_p && warn_cxx_compat) | |
1222 | warning_at (location, OPT_Wc___compat, | |
1223 | "pointer target types incompatible in C++"); | |
1224 | ||
628fa4f8 | 1225 | return val; |
1226 | } | |
1227 | \f | |
1228 | /* Subroutines of `comptypes'. */ | |
1229 | ||
393b349a | 1230 | /* Determine whether two trees derive from the same translation unit. |
1231 | If the CONTEXT chain ends in a null, that tree's context is still | |
1232 | being parsed, so if two trees have context chains ending in null, | |
76c16586 | 1233 | they're in the same translation unit. */ |
393b349a | 1234 | int |
9f627b1a | 1235 | same_translation_unit_p (const_tree t1, const_tree t2) |
76c16586 | 1236 | { |
1237 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
1238 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
1239 | { | |
ce45a448 | 1240 | case tcc_declaration: |
1241 | t1 = DECL_CONTEXT (t1); break; | |
1242 | case tcc_type: | |
1243 | t1 = TYPE_CONTEXT (t1); break; | |
1244 | case tcc_exceptional: | |
1245 | t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ | |
231bd014 | 1246 | default: gcc_unreachable (); |
76c16586 | 1247 | } |
1248 | ||
1249 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
1250 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
1251 | { | |
ce45a448 | 1252 | case tcc_declaration: |
1253 | t2 = DECL_CONTEXT (t2); break; | |
1254 | case tcc_type: | |
1255 | t2 = TYPE_CONTEXT (t2); break; | |
1256 | case tcc_exceptional: | |
1257 | t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ | |
231bd014 | 1258 | default: gcc_unreachable (); |
76c16586 | 1259 | } |
1260 | ||
1261 | return t1 == t2; | |
1262 | } | |
1263 | ||
7e58afa1 | 1264 | /* Allocate the seen two types, assuming that they are compatible. */ |
40109983 | 1265 | |
7e58afa1 | 1266 | static struct tagged_tu_seen_cache * |
9f627b1a | 1267 | alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) |
7e58afa1 | 1268 | { |
a9c6c0e3 | 1269 | struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); |
7e58afa1 | 1270 | tu->next = tagged_tu_seen_base; |
1271 | tu->t1 = t1; | |
1272 | tu->t2 = t2; | |
a0c938f0 | 1273 | |
7e58afa1 | 1274 | tagged_tu_seen_base = tu; |
a0c938f0 | 1275 | |
7e58afa1 | 1276 | /* The C standard says that two structures in different translation |
1277 | units are compatible with each other only if the types of their | |
1278 | fields are compatible (among other things). We assume that they | |
1279 | are compatible until proven otherwise when building the cache. | |
1280 | An example where this can occur is: | |
1281 | struct a | |
1282 | { | |
1283 | struct a *next; | |
1284 | }; | |
1285 | If we are comparing this against a similar struct in another TU, | |
7063afc3 | 1286 | and did not assume they were compatible, we end up with an infinite |
7e58afa1 | 1287 | loop. */ |
1288 | tu->val = 1; | |
1289 | return tu; | |
1290 | } | |
40109983 | 1291 | |
7e58afa1 | 1292 | /* Free the seen types until we get to TU_TIL. */ |
40109983 | 1293 | |
7e58afa1 | 1294 | static void |
1295 | free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) | |
1296 | { | |
1297 | const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; | |
1298 | while (tu != tu_til) | |
1299 | { | |
aae87fc3 | 1300 | const struct tagged_tu_seen_cache *const tu1 |
1301 | = (const struct tagged_tu_seen_cache *) tu; | |
7e58afa1 | 1302 | tu = tu1->next; |
e47a6f81 | 1303 | free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); |
7e58afa1 | 1304 | } |
1305 | tagged_tu_seen_base = tu_til; | |
1306 | } | |
40109983 | 1307 | |
1308 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
1309 | compatible. If the two types are not the same (which has been | |
1310 | checked earlier), this can only happen when multiple translation | |
1311 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
ce3765bf | 1312 | rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1313 | comptypes_internal. */ | |
40109983 | 1314 | |
1315 | static int | |
d5b637fa | 1316 | tagged_types_tu_compatible_p (const_tree t1, const_tree t2, |
ce3765bf | 1317 | bool *enum_and_int_p, bool *different_types_p) |
40109983 | 1318 | { |
1319 | tree s1, s2; | |
1320 | bool needs_warning = false; | |
a5d9b222 | 1321 | |
40109983 | 1322 | /* We have to verify that the tags of the types are the same. This |
1323 | is harder than it looks because this may be a typedef, so we have | |
1324 | to go look at the original type. It may even be a typedef of a | |
4ee9c684 | 1325 | typedef... |
1326 | In the case of compiler-created builtin structs the TYPE_DECL | |
1327 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
dba94932 | 1328 | while (TYPE_NAME (t1) |
1329 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
1330 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
40109983 | 1331 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); |
1332 | ||
dba94932 | 1333 | while (TYPE_NAME (t2) |
1334 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
1335 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
40109983 | 1336 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); |
1337 | ||
1338 | /* C90 didn't have the requirement that the two tags be the same. */ | |
1339 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
1340 | return 0; | |
a5d9b222 | 1341 | |
40109983 | 1342 | /* C90 didn't say what happened if one or both of the types were |
1343 | incomplete; we choose to follow C99 rules here, which is that they | |
1344 | are compatible. */ | |
1345 | if (TYPE_SIZE (t1) == NULL | |
1346 | || TYPE_SIZE (t2) == NULL) | |
1347 | return 1; | |
a5d9b222 | 1348 | |
40109983 | 1349 | { |
7e58afa1 | 1350 | const struct tagged_tu_seen_cache * tts_i; |
40109983 | 1351 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) |
1352 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
7e58afa1 | 1353 | return tts_i->val; |
40109983 | 1354 | } |
a5d9b222 | 1355 | |
40109983 | 1356 | switch (TREE_CODE (t1)) |
1357 | { | |
1358 | case ENUMERAL_TYPE: | |
1359 | { | |
7e58afa1 | 1360 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
a0c938f0 | 1361 | /* Speed up the case where the type values are in the same order. */ |
1362 | tree tv1 = TYPE_VALUES (t1); | |
1363 | tree tv2 = TYPE_VALUES (t2); | |
a5d9b222 | 1364 | |
a0c938f0 | 1365 | if (tv1 == tv2) |
7e58afa1 | 1366 | { |
1367 | return 1; | |
1368 | } | |
a5d9b222 | 1369 | |
a0c938f0 | 1370 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) |
1371 | { | |
1372 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) | |
1373 | break; | |
1374 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
7e58afa1 | 1375 | { |
a0c938f0 | 1376 | tu->val = 0; |
7e58afa1 | 1377 | return 0; |
1378 | } | |
a0c938f0 | 1379 | } |
a5d9b222 | 1380 | |
a0c938f0 | 1381 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) |
7e58afa1 | 1382 | { |
1383 | return 1; | |
1384 | } | |
a0c938f0 | 1385 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) |
7e58afa1 | 1386 | { |
1387 | tu->val = 0; | |
1388 | return 0; | |
1389 | } | |
a5d9b222 | 1390 | |
40109983 | 1391 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) |
7e58afa1 | 1392 | { |
1393 | tu->val = 0; | |
1394 | return 0; | |
1395 | } | |
a5d9b222 | 1396 | |
40109983 | 1397 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) |
1398 | { | |
1399 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
1400 | if (s2 == NULL | |
1401 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
7e58afa1 | 1402 | { |
1403 | tu->val = 0; | |
1404 | return 0; | |
1405 | } | |
40109983 | 1406 | } |
1407 | return 1; | |
1408 | } | |
1409 | ||
1410 | case UNION_TYPE: | |
1411 | { | |
7e58afa1 | 1412 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
40109983 | 1413 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) |
7e58afa1 | 1414 | { |
1415 | tu->val = 0; | |
1416 | return 0; | |
1417 | } | |
a0c938f0 | 1418 | |
7e58afa1 | 1419 | /* Speed up the common case where the fields are in the same order. */ |
1420 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; | |
1767a056 | 1421 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
7e58afa1 | 1422 | { |
1423 | int result; | |
a0c938f0 | 1424 | |
7a07cc13 | 1425 | if (DECL_NAME (s1) != DECL_NAME (s2)) |
7e58afa1 | 1426 | break; |
d5b637fa | 1427 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
ce3765bf | 1428 | enum_and_int_p, different_types_p); |
7a07cc13 | 1429 | |
1430 | if (result != 1 && !DECL_NAME (s1)) | |
1431 | break; | |
7e58afa1 | 1432 | if (result == 0) |
1433 | { | |
1434 | tu->val = 0; | |
1435 | return 0; | |
1436 | } | |
1437 | if (result == 2) | |
1438 | needs_warning = true; | |
1439 | ||
1440 | if (TREE_CODE (s1) == FIELD_DECL | |
1441 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1442 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1443 | { | |
1444 | tu->val = 0; | |
1445 | return 0; | |
1446 | } | |
1447 | } | |
1448 | if (!s1 && !s2) | |
1449 | { | |
1450 | tu->val = needs_warning ? 2 : 1; | |
1451 | return tu->val; | |
1452 | } | |
40109983 | 1453 | |
1767a056 | 1454 | for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1)) |
40109983 | 1455 | { |
1456 | bool ok = false; | |
a5d9b222 | 1457 | |
1767a056 | 1458 | for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2)) |
7a07cc13 | 1459 | if (DECL_NAME (s1) == DECL_NAME (s2)) |
1460 | { | |
1461 | int result; | |
1462 | ||
d5b637fa | 1463 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
ce3765bf | 1464 | enum_and_int_p, |
1465 | different_types_p); | |
7a07cc13 | 1466 | |
1467 | if (result != 1 && !DECL_NAME (s1)) | |
1468 | continue; | |
1469 | if (result == 0) | |
1470 | { | |
1471 | tu->val = 0; | |
1472 | return 0; | |
1473 | } | |
1474 | if (result == 2) | |
1475 | needs_warning = true; | |
1476 | ||
1477 | if (TREE_CODE (s1) == FIELD_DECL | |
1478 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1479 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
40109983 | 1480 | break; |
7a07cc13 | 1481 | |
1482 | ok = true; | |
1483 | break; | |
1484 | } | |
84166705 | 1485 | if (!ok) |
7e58afa1 | 1486 | { |
1487 | tu->val = 0; | |
1488 | return 0; | |
1489 | } | |
40109983 | 1490 | } |
7e58afa1 | 1491 | tu->val = needs_warning ? 2 : 10; |
1492 | return tu->val; | |
40109983 | 1493 | } |
1494 | ||
1495 | case RECORD_TYPE: | |
1496 | { | |
a0c938f0 | 1497 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
a5d9b222 | 1498 | |
1499 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
40109983 | 1500 | s1 && s2; |
1767a056 | 1501 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
40109983 | 1502 | { |
1503 | int result; | |
1504 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
1505 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
1506 | break; | |
d5b637fa | 1507 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
ce3765bf | 1508 | enum_and_int_p, different_types_p); |
40109983 | 1509 | if (result == 0) |
1510 | break; | |
1511 | if (result == 2) | |
1512 | needs_warning = true; | |
a5d9b222 | 1513 | |
40109983 | 1514 | if (TREE_CODE (s1) == FIELD_DECL |
1515 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1516 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1517 | break; | |
1518 | } | |
40109983 | 1519 | if (s1 && s2) |
7e58afa1 | 1520 | tu->val = 0; |
1521 | else | |
1522 | tu->val = needs_warning ? 2 : 1; | |
1523 | return tu->val; | |
40109983 | 1524 | } |
1525 | ||
1526 | default: | |
231bd014 | 1527 | gcc_unreachable (); |
40109983 | 1528 | } |
1529 | } | |
1530 | ||
628fa4f8 | 1531 | /* Return 1 if two function types F1 and F2 are compatible. |
1532 | If either type specifies no argument types, | |
1533 | the other must specify a fixed number of self-promoting arg types. | |
2b30d46c | 1534 | Otherwise, if one type specifies only the number of arguments, |
628fa4f8 | 1535 | the other must specify that number of self-promoting arg types. |
d5b637fa | 1536 | Otherwise, the argument types must match. |
ce3765bf | 1537 | ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */ |
628fa4f8 | 1538 | |
1539 | static int | |
d5b637fa | 1540 | function_types_compatible_p (const_tree f1, const_tree f2, |
ce3765bf | 1541 | bool *enum_and_int_p, bool *different_types_p) |
628fa4f8 | 1542 | { |
1543 | tree args1, args2; | |
1544 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1545 | int val = 1; | |
1546 | int val1; | |
fd49c655 | 1547 | tree ret1, ret2; |
1548 | ||
1549 | ret1 = TREE_TYPE (f1); | |
1550 | ret2 = TREE_TYPE (f2); | |
1551 | ||
0bfe2579 | 1552 | /* 'volatile' qualifiers on a function's return type used to mean |
1553 | the function is noreturn. */ | |
1554 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
21ca8540 | 1555 | pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); |
fd49c655 | 1556 | if (TYPE_VOLATILE (ret1)) |
1557 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
1558 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
1559 | if (TYPE_VOLATILE (ret2)) | |
1560 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
1561 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
ce3765bf | 1562 | val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p); |
fd49c655 | 1563 | if (val == 0) |
628fa4f8 | 1564 | return 0; |
1565 | ||
1566 | args1 = TYPE_ARG_TYPES (f1); | |
1567 | args2 = TYPE_ARG_TYPES (f2); | |
1568 | ||
ce3765bf | 1569 | if (different_types_p != NULL |
1570 | && (args1 == 0) != (args2 == 0)) | |
1571 | *different_types_p = true; | |
1572 | ||
628fa4f8 | 1573 | /* An unspecified parmlist matches any specified parmlist |
1574 | whose argument types don't need default promotions. */ | |
1575 | ||
1576 | if (args1 == 0) | |
1577 | { | |
1578 | if (!self_promoting_args_p (args2)) | |
1579 | return 0; | |
1580 | /* If one of these types comes from a non-prototype fn definition, | |
1581 | compare that with the other type's arglist. | |
6bf97f82 | 1582 | If they don't match, ask for a warning (but no error). */ |
628fa4f8 | 1583 | if (TYPE_ACTUAL_ARG_TYPES (f1) |
d5b637fa | 1584 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), |
ce3765bf | 1585 | enum_and_int_p, different_types_p)) |
628fa4f8 | 1586 | val = 2; |
1587 | return val; | |
1588 | } | |
1589 | if (args2 == 0) | |
1590 | { | |
1591 | if (!self_promoting_args_p (args1)) | |
1592 | return 0; | |
1593 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
d5b637fa | 1594 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), |
ce3765bf | 1595 | enum_and_int_p, different_types_p)) |
628fa4f8 | 1596 | val = 2; |
1597 | return val; | |
1598 | } | |
1599 | ||
1600 | /* Both types have argument lists: compare them and propagate results. */ | |
ce3765bf | 1601 | val1 = type_lists_compatible_p (args1, args2, enum_and_int_p, |
1602 | different_types_p); | |
628fa4f8 | 1603 | return val1 != 1 ? val1 : val; |
1604 | } | |
1605 | ||
d5b637fa | 1606 | /* Check two lists of types for compatibility, returning 0 for |
1607 | incompatible, 1 for compatible, or 2 for compatible with | |
ce3765bf | 1608 | warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1609 | comptypes_internal. */ | |
628fa4f8 | 1610 | |
1611 | static int | |
d5b637fa | 1612 | type_lists_compatible_p (const_tree args1, const_tree args2, |
ce3765bf | 1613 | bool *enum_and_int_p, bool *different_types_p) |
628fa4f8 | 1614 | { |
1615 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1616 | int val = 1; | |
c2879dd7 | 1617 | int newval = 0; |
628fa4f8 | 1618 | |
1619 | while (1) | |
1620 | { | |
aebc8537 | 1621 | tree a1, mv1, a2, mv2; |
628fa4f8 | 1622 | if (args1 == 0 && args2 == 0) |
1623 | return val; | |
1624 | /* If one list is shorter than the other, | |
1625 | they fail to match. */ | |
1626 | if (args1 == 0 || args2 == 0) | |
1627 | return 0; | |
aebc8537 | 1628 | mv1 = a1 = TREE_VALUE (args1); |
1629 | mv2 = a2 = TREE_VALUE (args2); | |
1630 | if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) | |
1631 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
1632 | if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) | |
1633 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
628fa4f8 | 1634 | /* A null pointer instead of a type |
1635 | means there is supposed to be an argument | |
1636 | but nothing is specified about what type it has. | |
1637 | So match anything that self-promotes. */ | |
ce3765bf | 1638 | if (different_types_p != NULL |
1639 | && (a1 == 0) != (a2 == 0)) | |
1640 | *different_types_p = true; | |
aebc8537 | 1641 | if (a1 == 0) |
628fa4f8 | 1642 | { |
aebc8537 | 1643 | if (c_type_promotes_to (a2) != a2) |
628fa4f8 | 1644 | return 0; |
1645 | } | |
aebc8537 | 1646 | else if (a2 == 0) |
628fa4f8 | 1647 | { |
aebc8537 | 1648 | if (c_type_promotes_to (a1) != a1) |
628fa4f8 | 1649 | return 0; |
1650 | } | |
aa76d997 | 1651 | /* If one of the lists has an error marker, ignore this arg. */ |
aebc8537 | 1652 | else if (TREE_CODE (a1) == ERROR_MARK |
1653 | || TREE_CODE (a2) == ERROR_MARK) | |
aa76d997 | 1654 | ; |
ce3765bf | 1655 | else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p, |
1656 | different_types_p))) | |
628fa4f8 | 1657 | { |
ce3765bf | 1658 | if (different_types_p != NULL) |
1659 | *different_types_p = true; | |
628fa4f8 | 1660 | /* Allow wait (union {union wait *u; int *i} *) |
1661 | and wait (union wait *) to be compatible. */ | |
aebc8537 | 1662 | if (TREE_CODE (a1) == UNION_TYPE |
1663 | && (TYPE_NAME (a1) == 0 | |
8df5a43d | 1664 | || TYPE_TRANSPARENT_AGGR (a1)) |
aebc8537 | 1665 | && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST |
1666 | && tree_int_cst_equal (TYPE_SIZE (a1), | |
1667 | TYPE_SIZE (a2))) | |
628fa4f8 | 1668 | { |
1669 | tree memb; | |
aebc8537 | 1670 | for (memb = TYPE_FIELDS (a1); |
1767a056 | 1671 | memb; memb = DECL_CHAIN (memb)) |
c75da5d5 | 1672 | { |
1673 | tree mv3 = TREE_TYPE (memb); | |
1674 | if (mv3 && mv3 != error_mark_node | |
1675 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
1676 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
ce3765bf | 1677 | if (comptypes_internal (mv3, mv2, enum_and_int_p, |
1678 | different_types_p)) | |
c75da5d5 | 1679 | break; |
1680 | } | |
628fa4f8 | 1681 | if (memb == 0) |
1682 | return 0; | |
1683 | } | |
aebc8537 | 1684 | else if (TREE_CODE (a2) == UNION_TYPE |
1685 | && (TYPE_NAME (a2) == 0 | |
8df5a43d | 1686 | || TYPE_TRANSPARENT_AGGR (a2)) |
aebc8537 | 1687 | && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST |
1688 | && tree_int_cst_equal (TYPE_SIZE (a2), | |
1689 | TYPE_SIZE (a1))) | |
628fa4f8 | 1690 | { |
1691 | tree memb; | |
aebc8537 | 1692 | for (memb = TYPE_FIELDS (a2); |
1767a056 | 1693 | memb; memb = DECL_CHAIN (memb)) |
c75da5d5 | 1694 | { |
1695 | tree mv3 = TREE_TYPE (memb); | |
1696 | if (mv3 && mv3 != error_mark_node | |
1697 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
1698 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
ce3765bf | 1699 | if (comptypes_internal (mv3, mv1, enum_and_int_p, |
1700 | different_types_p)) | |
c75da5d5 | 1701 | break; |
1702 | } | |
628fa4f8 | 1703 | if (memb == 0) |
1704 | return 0; | |
1705 | } | |
1706 | else | |
1707 | return 0; | |
1708 | } | |
1709 | ||
1710 | /* comptypes said ok, but record if it said to warn. */ | |
1711 | if (newval > val) | |
1712 | val = newval; | |
1713 | ||
1714 | args1 = TREE_CHAIN (args1); | |
1715 | args2 = TREE_CHAIN (args2); | |
1716 | } | |
1717 | } | |
628fa4f8 | 1718 | \f |
628fa4f8 | 1719 | /* Compute the size to increment a pointer by. */ |
1720 | ||
91ffb709 | 1721 | static tree |
9f627b1a | 1722 | c_size_in_bytes (const_tree type) |
628fa4f8 | 1723 | { |
1724 | enum tree_code code = TREE_CODE (type); | |
1725 | ||
902de8ed | 1726 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) |
1727 | return size_one_node; | |
1728 | ||
4b72716d | 1729 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
628fa4f8 | 1730 | { |
1731 | error ("arithmetic on pointer to an incomplete type"); | |
902de8ed | 1732 | return size_one_node; |
628fa4f8 | 1733 | } |
1734 | ||
1735 | /* Convert in case a char is more than one unit. */ | |
389dd41b | 1736 | return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
1737 | size_int (TYPE_PRECISION (char_type_node) | |
1738 | / BITS_PER_UNIT)); | |
628fa4f8 | 1739 | } |
628fa4f8 | 1740 | \f |
628fa4f8 | 1741 | /* Return either DECL or its known constant value (if it has one). */ |
1742 | ||
225ec6aa | 1743 | tree |
2b30d46c | 1744 | decl_constant_value (tree decl) |
628fa4f8 | 1745 | { |
6cd7ce61 | 1746 | if (/* Don't change a variable array bound or initial value to a constant |
66d12a6c | 1747 | in a place where a variable is invalid. Note that DECL_INITIAL |
1748 | isn't valid for a PARM_DECL. */ | |
6cd7ce61 | 1749 | current_function_decl != 0 |
66d12a6c | 1750 | && TREE_CODE (decl) != PARM_DECL |
84166705 | 1751 | && !TREE_THIS_VOLATILE (decl) |
57913890 | 1752 | && TREE_READONLY (decl) |
628fa4f8 | 1753 | && DECL_INITIAL (decl) != 0 |
1754 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1755 | /* This is invalid if initial value is not constant. | |
1756 | If it has either a function call, a memory reference, | |
1757 | or a variable, then re-evaluating it could give different results. */ | |
1758 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1759 | /* Check for cases where this is sub-optimal, even though valid. */ | |
629d8b08 | 1760 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
628fa4f8 | 1761 | return DECL_INITIAL (decl); |
1762 | return decl; | |
1763 | } | |
1764 | ||
e78cf9a7 | 1765 | /* Convert the array expression EXP to a pointer. */ |
1766 | static tree | |
e60a6f7b | 1767 | array_to_pointer_conversion (location_t loc, tree exp) |
62827f44 | 1768 | { |
e78cf9a7 | 1769 | tree orig_exp = exp; |
62827f44 | 1770 | tree type = TREE_TYPE (exp); |
e78cf9a7 | 1771 | tree adr; |
1772 | tree restype = TREE_TYPE (type); | |
1773 | tree ptrtype; | |
62827f44 | 1774 | |
e78cf9a7 | 1775 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
62827f44 | 1776 | |
e78cf9a7 | 1777 | STRIP_TYPE_NOPS (exp); |
62827f44 | 1778 | |
066f26bf | 1779 | if (TREE_NO_WARNING (orig_exp)) |
1780 | TREE_NO_WARNING (exp) = 1; | |
62827f44 | 1781 | |
e78cf9a7 | 1782 | ptrtype = build_pointer_type (restype); |
1783 | ||
1784 | if (TREE_CODE (exp) == INDIRECT_REF) | |
1785 | return convert (ptrtype, TREE_OPERAND (exp, 0)); | |
1786 | ||
eb5d9b7b | 1787 | /* In C++ array compound literals are temporary objects unless they are |
1788 | const or appear in namespace scope, so they are destroyed too soon | |
1789 | to use them for much of anything (c++/53220). */ | |
1790 | if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) | |
1791 | { | |
1792 | tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
1793 | if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) | |
1794 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, | |
1795 | "converting an array compound literal to a pointer " | |
1796 | "is ill-formed in C++"); | |
1797 | } | |
1798 | ||
e60a6f7b | 1799 | adr = build_unary_op (loc, ADDR_EXPR, exp, 1); |
e78cf9a7 | 1800 | return convert (ptrtype, adr); |
1801 | } | |
62827f44 | 1802 | |
e78cf9a7 | 1803 | /* Convert the function expression EXP to a pointer. */ |
1804 | static tree | |
e60a6f7b | 1805 | function_to_pointer_conversion (location_t loc, tree exp) |
e78cf9a7 | 1806 | { |
1807 | tree orig_exp = exp; | |
62827f44 | 1808 | |
e78cf9a7 | 1809 | gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); |
62827f44 | 1810 | |
e78cf9a7 | 1811 | STRIP_TYPE_NOPS (exp); |
62827f44 | 1812 | |
e78cf9a7 | 1813 | if (TREE_NO_WARNING (orig_exp)) |
1814 | TREE_NO_WARNING (exp) = 1; | |
62827f44 | 1815 | |
e60a6f7b | 1816 | return build_unary_op (loc, ADDR_EXPR, exp, 0); |
e78cf9a7 | 1817 | } |
62827f44 | 1818 | |
abc6c64f | 1819 | /* Mark EXP as read, not just set, for set but not used -Wunused |
1820 | warning purposes. */ | |
1821 | ||
1822 | void | |
1823 | mark_exp_read (tree exp) | |
1824 | { | |
1825 | switch (TREE_CODE (exp)) | |
1826 | { | |
1827 | case VAR_DECL: | |
1828 | case PARM_DECL: | |
1829 | DECL_READ_P (exp) = 1; | |
1830 | break; | |
1831 | case ARRAY_REF: | |
1832 | case COMPONENT_REF: | |
1833 | case MODIFY_EXPR: | |
1834 | case REALPART_EXPR: | |
1835 | case IMAGPART_EXPR: | |
1836 | CASE_CONVERT: | |
1837 | case ADDR_EXPR: | |
1838 | mark_exp_read (TREE_OPERAND (exp, 0)); | |
1839 | break; | |
1840 | case COMPOUND_EXPR: | |
062966c1 | 1841 | case C_MAYBE_CONST_EXPR: |
abc6c64f | 1842 | mark_exp_read (TREE_OPERAND (exp, 1)); |
1843 | break; | |
1844 | default: | |
1845 | break; | |
1846 | } | |
1847 | } | |
1848 | ||
e78cf9a7 | 1849 | /* Perform the default conversion of arrays and functions to pointers. |
1850 | Return the result of converting EXP. For any other expression, just | |
e60a6f7b | 1851 | return EXP. |
1852 | ||
1853 | LOC is the location of the expression. */ | |
e78cf9a7 | 1854 | |
1855 | struct c_expr | |
e60a6f7b | 1856 | default_function_array_conversion (location_t loc, struct c_expr exp) |
e78cf9a7 | 1857 | { |
1858 | tree orig_exp = exp.value; | |
1859 | tree type = TREE_TYPE (exp.value); | |
1860 | enum tree_code code = TREE_CODE (type); | |
1861 | ||
1862 | switch (code) | |
1863 | { | |
1864 | case ARRAY_TYPE: | |
1865 | { | |
1866 | bool not_lvalue = false; | |
1867 | bool lvalue_array_p; | |
1868 | ||
1869 | while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR | |
72dd6141 | 1870 | || CONVERT_EXPR_P (exp.value)) |
e78cf9a7 | 1871 | && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) |
1872 | { | |
1873 | if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) | |
1874 | not_lvalue = true; | |
1875 | exp.value = TREE_OPERAND (exp.value, 0); | |
1876 | } | |
1877 | ||
1878 | if (TREE_NO_WARNING (orig_exp)) | |
1879 | TREE_NO_WARNING (exp.value) = 1; | |
1880 | ||
1881 | lvalue_array_p = !not_lvalue && lvalue_p (exp.value); | |
1882 | if (!flag_isoc99 && !lvalue_array_p) | |
1883 | { | |
1884 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1885 | Normally, using such an array would be invalid; but it can | |
1886 | be used correctly inside sizeof or as a statement expression. | |
1887 | Thus, do not give an error here; an error will result later. */ | |
1888 | return exp; | |
1889 | } | |
1890 | ||
e60a6f7b | 1891 | exp.value = array_to_pointer_conversion (loc, exp.value); |
e78cf9a7 | 1892 | } |
1893 | break; | |
1894 | case FUNCTION_TYPE: | |
e60a6f7b | 1895 | exp.value = function_to_pointer_conversion (loc, exp.value); |
e78cf9a7 | 1896 | break; |
1897 | default: | |
e78cf9a7 | 1898 | break; |
62827f44 | 1899 | } |
e78cf9a7 | 1900 | |
62827f44 | 1901 | return exp; |
1902 | } | |
1903 | ||
abc6c64f | 1904 | struct c_expr |
1905 | default_function_array_read_conversion (location_t loc, struct c_expr exp) | |
1906 | { | |
1907 | mark_exp_read (exp.value); | |
1908 | return default_function_array_conversion (loc, exp); | |
1909 | } | |
b96dc121 | 1910 | |
1911 | /* EXP is an expression of integer type. Apply the integer promotions | |
1912 | to it and return the promoted value. */ | |
628fa4f8 | 1913 | |
1914 | tree | |
b96dc121 | 1915 | perform_integral_promotions (tree exp) |
628fa4f8 | 1916 | { |
19cb6b50 | 1917 | tree type = TREE_TYPE (exp); |
1918 | enum tree_code code = TREE_CODE (type); | |
628fa4f8 | 1919 | |
b96dc121 | 1920 | gcc_assert (INTEGRAL_TYPE_P (type)); |
98821063 | 1921 | |
628fa4f8 | 1922 | /* Normally convert enums to int, |
1923 | but convert wide enums to something wider. */ | |
1924 | if (code == ENUMERAL_TYPE) | |
1925 | { | |
771d21fa | 1926 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
1927 | TYPE_PRECISION (integer_type_node)), | |
1928 | ((TYPE_PRECISION (type) | |
1929 | >= TYPE_PRECISION (integer_type_node)) | |
78a8ed03 | 1930 | && TYPE_UNSIGNED (type))); |
a0c2c45b | 1931 | |
628fa4f8 | 1932 | return convert (type, exp); |
1933 | } | |
1934 | ||
b96dc121 | 1935 | /* ??? This should no longer be needed now bit-fields have their |
1936 | proper types. */ | |
3486c3bf | 1937 | if (TREE_CODE (exp) == COMPONENT_REF |
a0c2c45b | 1938 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) |
ff552418 | 1939 | /* If it's thinner than an int, promote it like a |
d7aeef06 | 1940 | c_promoting_integer_type_p, otherwise leave it alone. */ |
a0c2c45b | 1941 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), |
1942 | TYPE_PRECISION (integer_type_node))) | |
455730ef | 1943 | return convert (integer_type_node, exp); |
3486c3bf | 1944 | |
d7aeef06 | 1945 | if (c_promoting_integer_type_p (type)) |
628fa4f8 | 1946 | { |
455730ef | 1947 | /* Preserve unsignedness if not really getting any wider. */ |
78a8ed03 | 1948 | if (TYPE_UNSIGNED (type) |
455730ef | 1949 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
628fa4f8 | 1950 | return convert (unsigned_type_node, exp); |
a0c2c45b | 1951 | |
628fa4f8 | 1952 | return convert (integer_type_node, exp); |
1953 | } | |
a0c2c45b | 1954 | |
b96dc121 | 1955 | return exp; |
1956 | } | |
1957 | ||
1958 | ||
1959 | /* Perform default promotions for C data used in expressions. | |
f14c8207 | 1960 | Enumeral types or short or char are converted to int. |
b96dc121 | 1961 | In addition, manifest constants symbols are replaced by their values. */ |
1962 | ||
1963 | tree | |
1964 | default_conversion (tree exp) | |
1965 | { | |
1966 | tree orig_exp; | |
1967 | tree type = TREE_TYPE (exp); | |
1968 | enum tree_code code = TREE_CODE (type); | |
2b3c93a3 | 1969 | tree promoted_type; |
b96dc121 | 1970 | |
abc6c64f | 1971 | mark_exp_read (exp); |
1972 | ||
f14c8207 | 1973 | /* Functions and arrays have been converted during parsing. */ |
1974 | gcc_assert (code != FUNCTION_TYPE); | |
1975 | if (code == ARRAY_TYPE) | |
1976 | return exp; | |
b96dc121 | 1977 | |
1978 | /* Constants can be used directly unless they're not loadable. */ | |
1979 | if (TREE_CODE (exp) == CONST_DECL) | |
1980 | exp = DECL_INITIAL (exp); | |
1981 | ||
b96dc121 | 1982 | /* Strip no-op conversions. */ |
1983 | orig_exp = exp; | |
1984 | STRIP_TYPE_NOPS (exp); | |
1985 | ||
1986 | if (TREE_NO_WARNING (orig_exp)) | |
1987 | TREE_NO_WARNING (exp) = 1; | |
1988 | ||
628fa4f8 | 1989 | if (code == VOID_TYPE) |
1990 | { | |
1991 | error ("void value not ignored as it ought to be"); | |
1992 | return error_mark_node; | |
1993 | } | |
f831c98f | 1994 | |
1995 | exp = require_complete_type (exp); | |
1996 | if (exp == error_mark_node) | |
1997 | return error_mark_node; | |
1998 | ||
2b3c93a3 | 1999 | promoted_type = targetm.promoted_type (type); |
2000 | if (promoted_type) | |
2001 | return convert (promoted_type, exp); | |
2002 | ||
f831c98f | 2003 | if (INTEGRAL_TYPE_P (type)) |
2004 | return perform_integral_promotions (exp); | |
2005 | ||
628fa4f8 | 2006 | return exp; |
2007 | } | |
2008 | \f | |
10064fb9 | 2009 | /* Look up COMPONENT in a structure or union TYPE. |
9303bbca | 2010 | |
2011 | If the component name is not found, returns NULL_TREE. Otherwise, | |
2012 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
2013 | stepping down the chain to the component, which is in the last | |
2014 | TREE_VALUE of the list. Normally the list is of length one, but if | |
2015 | the component is embedded within (nested) anonymous structures or | |
2016 | unions, the list steps down the chain to the component. */ | |
2b30d46c | 2017 | |
bdbd2f6d | 2018 | static tree |
10064fb9 | 2019 | lookup_field (tree type, tree component) |
bdbd2f6d | 2020 | { |
2021 | tree field; | |
2022 | ||
2023 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
2024 | to the field elements. Use a binary search on this array to quickly | |
2025 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
2026 | will always be set for structures which have many elements. */ | |
2027 | ||
4bb2a91a | 2028 | if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) |
bdbd2f6d | 2029 | { |
2030 | int bot, top, half; | |
860251be | 2031 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; |
bdbd2f6d | 2032 | |
2033 | field = TYPE_FIELDS (type); | |
2034 | bot = 0; | |
860251be | 2035 | top = TYPE_LANG_SPECIFIC (type)->s->len; |
bdbd2f6d | 2036 | while (top - bot > 1) |
2037 | { | |
bdbd2f6d | 2038 | half = (top - bot + 1) >> 1; |
2039 | field = field_array[bot+half]; | |
2040 | ||
2041 | if (DECL_NAME (field) == NULL_TREE) | |
2042 | { | |
2043 | /* Step through all anon unions in linear fashion. */ | |
2044 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
2045 | { | |
bdbd2f6d | 2046 | field = field_array[bot++]; |
f9d98a50 | 2047 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
2048 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
37c005bb | 2049 | { |
10064fb9 | 2050 | tree anon = lookup_field (TREE_TYPE (field), component); |
9303bbca | 2051 | |
2052 | if (anon) | |
2053 | return tree_cons (NULL_TREE, field, anon); | |
2fdec027 | 2054 | |
2055 | /* The Plan 9 compiler permits referring | |
2056 | directly to an anonymous struct/union field | |
2057 | using a typedef name. */ | |
2058 | if (flag_plan9_extensions | |
2059 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2060 | && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) | |
2061 | == TYPE_DECL) | |
2062 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2063 | == component)) | |
2064 | break; | |
2b30d46c | 2065 | } |
bdbd2f6d | 2066 | } |
2067 | ||
2068 | /* Entire record is only anon unions. */ | |
2069 | if (bot > top) | |
2070 | return NULL_TREE; | |
2071 | ||
2072 | /* Restart the binary search, with new lower bound. */ | |
2073 | continue; | |
2074 | } | |
2075 | ||
d3016716 | 2076 | if (DECL_NAME (field) == component) |
bdbd2f6d | 2077 | break; |
d3016716 | 2078 | if (DECL_NAME (field) < component) |
bdbd2f6d | 2079 | bot += half; |
2080 | else | |
2081 | top = bot + half; | |
2082 | } | |
2083 | ||
2084 | if (DECL_NAME (field_array[bot]) == component) | |
2085 | field = field_array[bot]; | |
2086 | else if (DECL_NAME (field) != component) | |
9303bbca | 2087 | return NULL_TREE; |
bdbd2f6d | 2088 | } |
2089 | else | |
2090 | { | |
1767a056 | 2091 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
bdbd2f6d | 2092 | { |
9303bbca | 2093 | if (DECL_NAME (field) == NULL_TREE |
2094 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
2095 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
bdbd2f6d | 2096 | { |
10064fb9 | 2097 | tree anon = lookup_field (TREE_TYPE (field), component); |
f9d98a50 | 2098 | |
9303bbca | 2099 | if (anon) |
2100 | return tree_cons (NULL_TREE, field, anon); | |
2fdec027 | 2101 | |
2102 | /* The Plan 9 compiler permits referring directly to an | |
2103 | anonymous struct/union field using a typedef | |
2104 | name. */ | |
2105 | if (flag_plan9_extensions | |
2106 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2107 | && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL | |
2108 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2109 | == component)) | |
2110 | break; | |
bdbd2f6d | 2111 | } |
2112 | ||
2113 | if (DECL_NAME (field) == component) | |
2114 | break; | |
2115 | } | |
9303bbca | 2116 | |
2117 | if (field == NULL_TREE) | |
2118 | return NULL_TREE; | |
bdbd2f6d | 2119 | } |
2120 | ||
9303bbca | 2121 | return tree_cons (NULL_TREE, field, NULL_TREE); |
bdbd2f6d | 2122 | } |
2123 | ||
e60a6f7b | 2124 | /* Make an expression to refer to the COMPONENT field of structure or |
2125 | union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the | |
2126 | location of the COMPONENT_REF. */ | |
628fa4f8 | 2127 | |
2128 | tree | |
e60a6f7b | 2129 | build_component_ref (location_t loc, tree datum, tree component) |
628fa4f8 | 2130 | { |
19cb6b50 | 2131 | tree type = TREE_TYPE (datum); |
2132 | enum tree_code code = TREE_CODE (type); | |
2133 | tree field = NULL; | |
2134 | tree ref; | |
7666d572 | 2135 | bool datum_lvalue = lvalue_p (datum); |
628fa4f8 | 2136 | |
43bf5d72 | 2137 | if (!objc_is_public (datum, component)) |
2138 | return error_mark_node; | |
2139 | ||
9d9f5bb3 | 2140 | /* Detect Objective-C property syntax object.property. */ |
86c110ac | 2141 | if (c_dialect_objc () |
9d9f5bb3 | 2142 | && (ref = objc_maybe_build_component_ref (datum, component))) |
86c110ac | 2143 | return ref; |
2144 | ||
628fa4f8 | 2145 | /* See if there is a field or component with name COMPONENT. */ |
2146 | ||
2147 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
2148 | { | |
4b72716d | 2149 | if (!COMPLETE_TYPE_P (type)) |
628fa4f8 | 2150 | { |
1dd25100 | 2151 | c_incomplete_type_error (NULL_TREE, type); |
628fa4f8 | 2152 | return error_mark_node; |
2153 | } | |
2154 | ||
10064fb9 | 2155 | field = lookup_field (type, component); |
628fa4f8 | 2156 | |
2157 | if (!field) | |
2158 | { | |
e60a6f7b | 2159 | error_at (loc, "%qT has no member named %qE", type, component); |
628fa4f8 | 2160 | return error_mark_node; |
2161 | } | |
628fa4f8 | 2162 | |
9303bbca | 2163 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. |
2164 | This might be better solved in future the way the C++ front | |
2165 | end does it - by giving the anonymous entities each a | |
2166 | separate name and type, and then have build_component_ref | |
2167 | recursively call itself. We can't do that here. */ | |
04416275 | 2168 | do |
37c005bb | 2169 | { |
9303bbca | 2170 | tree subdatum = TREE_VALUE (field); |
62e58612 | 2171 | int quals; |
2172 | tree subtype; | |
7666d572 | 2173 | bool use_datum_quals; |
9303bbca | 2174 | |
2175 | if (TREE_TYPE (subdatum) == error_mark_node) | |
2176 | return error_mark_node; | |
2177 | ||
7666d572 | 2178 | /* If this is an rvalue, it does not have qualifiers in C |
2179 | standard terms and we must avoid propagating such | |
2180 | qualifiers down to a non-lvalue array that is then | |
2181 | converted to a pointer. */ | |
2182 | use_datum_quals = (datum_lvalue | |
2183 | || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); | |
2184 | ||
62e58612 | 2185 | quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); |
7666d572 | 2186 | if (use_datum_quals) |
2187 | quals |= TYPE_QUALS (TREE_TYPE (datum)); | |
62e58612 | 2188 | subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); |
2189 | ||
2190 | ref = build3 (COMPONENT_REF, subtype, datum, subdatum, | |
14ae0310 | 2191 | NULL_TREE); |
e60a6f7b | 2192 | SET_EXPR_LOCATION (ref, loc); |
7666d572 | 2193 | if (TREE_READONLY (subdatum) |
2194 | || (use_datum_quals && TREE_READONLY (datum))) | |
37c005bb | 2195 | TREE_READONLY (ref) = 1; |
7666d572 | 2196 | if (TREE_THIS_VOLATILE (subdatum) |
2197 | || (use_datum_quals && TREE_THIS_VOLATILE (datum))) | |
37c005bb | 2198 | TREE_THIS_VOLATILE (ref) = 1; |
88da234d | 2199 | |
2200 | if (TREE_DEPRECATED (subdatum)) | |
45c4e798 | 2201 | warn_deprecated_use (subdatum, NULL_TREE); |
88da234d | 2202 | |
37c005bb | 2203 | datum = ref; |
04416275 | 2204 | |
2205 | field = TREE_CHAIN (field); | |
37c005bb | 2206 | } |
04416275 | 2207 | while (field); |
37c005bb | 2208 | |
628fa4f8 | 2209 | return ref; |
2210 | } | |
2211 | else if (code != ERROR_MARK) | |
e60a6f7b | 2212 | error_at (loc, |
2213 | "request for member %qE in something not a structure or union", | |
2214 | component); | |
628fa4f8 | 2215 | |
2216 | return error_mark_node; | |
2217 | } | |
2218 | \f | |
2219 | /* Given an expression PTR for a pointer, return an expression | |
2220 | for the value pointed to. | |
dda49785 | 2221 | ERRORSTRING is the name of the operator to appear in error messages. |
2222 | ||
2223 | LOC is the location to use for the generated tree. */ | |
628fa4f8 | 2224 | |
2225 | tree | |
f08923b3 | 2226 | build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) |
628fa4f8 | 2227 | { |
19cb6b50 | 2228 | tree pointer = default_conversion (ptr); |
2229 | tree type = TREE_TYPE (pointer); | |
dda49785 | 2230 | tree ref; |
628fa4f8 | 2231 | |
2232 | if (TREE_CODE (type) == POINTER_TYPE) | |
bfbc4d3a | 2233 | { |
72dd6141 | 2234 | if (CONVERT_EXPR_P (pointer) |
e6fa0ea6 | 2235 | || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) |
2236 | { | |
2237 | /* If a warning is issued, mark it to avoid duplicates from | |
2238 | the backend. This only needs to be done at | |
2239 | warn_strict_aliasing > 2. */ | |
2240 | if (warn_strict_aliasing > 2) | |
2241 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), | |
2242 | type, TREE_OPERAND (pointer, 0))) | |
2243 | TREE_NO_WARNING (pointer) = 1; | |
2244 | } | |
2245 | ||
bfbc4d3a | 2246 | if (TREE_CODE (pointer) == ADDR_EXPR |
bfbc4d3a | 2247 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) |
2248 | == TREE_TYPE (type))) | |
dda49785 | 2249 | { |
2250 | ref = TREE_OPERAND (pointer, 0); | |
2251 | protected_set_expr_location (ref, loc); | |
2252 | return ref; | |
2253 | } | |
bfbc4d3a | 2254 | else |
2255 | { | |
2256 | tree t = TREE_TYPE (type); | |
aebc8537 | 2257 | |
ca136233 | 2258 | ref = build1 (INDIRECT_REF, t, pointer); |
628fa4f8 | 2259 | |
fe2c198f | 2260 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) |
bfbc4d3a | 2261 | { |
b6889cb0 | 2262 | error_at (loc, "dereferencing pointer to incomplete type"); |
bfbc4d3a | 2263 | return error_mark_node; |
2264 | } | |
48d94ede | 2265 | if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) |
b6889cb0 | 2266 | warning_at (loc, 0, "dereferencing %<void *%> pointer"); |
bfbc4d3a | 2267 | |
2268 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
2269 | so that we get the proper error message if the result is used | |
2270 | to assign to. Also, &* is supposed to be a no-op. | |
2271 | And ANSI C seems to specify that the type of the result | |
2272 | should be the const type. */ | |
2273 | /* A de-reference of a pointer to const is not a const. It is valid | |
2274 | to change it via some other pointer. */ | |
2275 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
2276 | TREE_SIDE_EFFECTS (ref) | |
c1771f2c | 2277 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); |
618c9975 | 2278 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
dda49785 | 2279 | protected_set_expr_location (ref, loc); |
bfbc4d3a | 2280 | return ref; |
2281 | } | |
2282 | } | |
628fa4f8 | 2283 | else if (TREE_CODE (pointer) != ERROR_MARK) |
b1bbc8e5 | 2284 | invalid_indirection_error (loc, type, errstring); |
2285 | ||
628fa4f8 | 2286 | return error_mark_node; |
2287 | } | |
2288 | ||
2289 | /* This handles expressions of the form "a[i]", which denotes | |
2290 | an array reference. | |
2291 | ||
2292 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2293 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2294 | This avoids forcing the array out of registers, and can work on | |
2295 | arrays that are not lvalues (for example, members of structures returned | |
dda49785 | 2296 | by functions). |
2297 | ||
93426222 | 2298 | For vector types, allow vector[i] but not i[vector], and create |
2299 | *(((type*)&vectortype) + i) for the expression. | |
2300 | ||
dda49785 | 2301 | LOC is the location to use for the returned expression. */ |
628fa4f8 | 2302 | |
2303 | tree | |
e60a6f7b | 2304 | build_array_ref (location_t loc, tree array, tree index) |
628fa4f8 | 2305 | { |
dda49785 | 2306 | tree ret; |
20931c08 | 2307 | bool swapped = false; |
628fa4f8 | 2308 | if (TREE_TYPE (array) == error_mark_node |
2309 | || TREE_TYPE (index) == error_mark_node) | |
2310 | return error_mark_node; | |
2311 | ||
20931c08 | 2312 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE |
93426222 | 2313 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE |
2314 | /* Allow vector[index] but not index[vector]. */ | |
2315 | && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE) | |
628fa4f8 | 2316 | { |
20931c08 | 2317 | tree temp; |
2318 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE | |
2319 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
c4f6b7e5 | 2320 | { |
6cf89e04 | 2321 | error_at (loc, |
93426222 | 2322 | "subscripted value is neither array nor pointer nor vector"); |
2323 | ||
c4f6b7e5 | 2324 | return error_mark_node; |
2325 | } | |
20931c08 | 2326 | temp = array; |
2327 | array = index; | |
2328 | index = temp; | |
2329 | swapped = true; | |
2330 | } | |
2331 | ||
2332 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2333 | { | |
3bc1df45 | 2334 | error_at (loc, "array subscript is not an integer"); |
20931c08 | 2335 | return error_mark_node; |
2336 | } | |
2337 | ||
2338 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2339 | { | |
3bc1df45 | 2340 | error_at (loc, "subscripted value is pointer to function"); |
20931c08 | 2341 | return error_mark_node; |
2342 | } | |
2343 | ||
2840aae4 | 2344 | /* ??? Existing practice has been to warn only when the char |
2345 | index is syntactically the index, not for char[array]. */ | |
2346 | if (!swapped) | |
2347 | warn_array_subscript_with_type_char (index); | |
20931c08 | 2348 | |
2349 | /* Apply default promotions *after* noticing character types. */ | |
2350 | index = default_conversion (index); | |
2351 | ||
2352 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
6cf89e04 | 2353 | |
7059d45d | 2354 | convert_vector_to_pointer_for_subscript (loc, &array, index); |
20931c08 | 2355 | |
2356 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2357 | { | |
8e31b4b2 | 2358 | tree rval, type; |
c4f6b7e5 | 2359 | |
628fa4f8 | 2360 | /* An array that is indexed by a non-constant |
2361 | cannot be stored in a register; we must be able to do | |
2362 | address arithmetic on its address. | |
2363 | Likewise an array of elements of variable size. */ | |
2364 | if (TREE_CODE (index) != INTEGER_CST | |
4b72716d | 2365 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
628fa4f8 | 2366 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
2367 | { | |
9b86eec0 | 2368 | if (!c_mark_addressable (array)) |
628fa4f8 | 2369 | return error_mark_node; |
2370 | } | |
754b3ff5 | 2371 | /* An array that is indexed by a constant value which is not within |
2372 | the array bounds cannot be stored in a register either; because we | |
2373 | would get a crash in store_bit_field/extract_bit_field when trying | |
2374 | to access a non-existent part of the register. */ | |
2375 | if (TREE_CODE (index) == INTEGER_CST | |
82bb2115 | 2376 | && TYPE_DOMAIN (TREE_TYPE (array)) |
84166705 | 2377 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) |
754b3ff5 | 2378 | { |
9b86eec0 | 2379 | if (!c_mark_addressable (array)) |
754b3ff5 | 2380 | return error_mark_node; |
2381 | } | |
628fa4f8 | 2382 | |
628fa4f8 | 2383 | if (pedantic) |
2384 | { | |
2385 | tree foo = array; | |
2386 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2387 | foo = TREE_OPERAND (foo, 0); | |
7746224a | 2388 | if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) |
29438999 | 2389 | pedwarn (loc, OPT_Wpedantic, |
8864917d | 2390 | "ISO C forbids subscripting %<register%> array"); |
84166705 | 2391 | else if (!flag_isoc99 && !lvalue_p (foo)) |
29438999 | 2392 | pedwarn (loc, OPT_Wpedantic, |
8864917d | 2393 | "ISO C90 forbids subscripting non-lvalue array"); |
628fa4f8 | 2394 | } |
2395 | ||
aebc8537 | 2396 | type = TREE_TYPE (TREE_TYPE (array)); |
14ae0310 | 2397 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
628fa4f8 | 2398 | /* Array ref is const/volatile if the array elements are |
a0c938f0 | 2399 | or if the array is. */ |
628fa4f8 | 2400 | TREE_READONLY (rval) |
2401 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2402 | | TREE_READONLY (array)); | |
2403 | TREE_SIDE_EFFECTS (rval) | |
2404 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2405 | | TREE_SIDE_EFFECTS (array)); | |
2406 | TREE_THIS_VOLATILE (rval) | |
2407 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2408 | /* This was added by rms on 16 Nov 91. | |
2b30d46c | 2409 | It fixes vol struct foo *a; a->elts[1] |
628fa4f8 | 2410 | in an inline function. |
2411 | Hope it doesn't break something else. */ | |
2412 | | TREE_THIS_VOLATILE (array)); | |
a75b1c71 | 2413 | ret = require_complete_type (rval); |
dda49785 | 2414 | protected_set_expr_location (ret, loc); |
2415 | return ret; | |
628fa4f8 | 2416 | } |
20931c08 | 2417 | else |
2418 | { | |
2419 | tree ar = default_conversion (array); | |
628fa4f8 | 2420 | |
20931c08 | 2421 | if (ar == error_mark_node) |
2422 | return ar; | |
628fa4f8 | 2423 | |
20931c08 | 2424 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); |
2425 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
628fa4f8 | 2426 | |
8e70fb09 | 2427 | return build_indirect_ref |
b6889cb0 | 2428 | (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0), |
f08923b3 | 2429 | RO_ARRAY_INDEXING); |
20931c08 | 2430 | } |
628fa4f8 | 2431 | } |
2432 | \f | |
1beb61f9 | 2433 | /* Build an external reference to identifier ID. FUN indicates |
8dc938b9 | 2434 | whether this will be used for a function call. LOC is the source |
841fdaa6 | 2435 | location of the identifier. This sets *TYPE to the type of the |
2436 | identifier, which is not the same as the type of the returned value | |
2437 | for CONST_DECLs defined as enum constants. If the type of the | |
2438 | identifier is not available, *TYPE is set to NULL. */ | |
1beb61f9 | 2439 | tree |
e60a6f7b | 2440 | build_external_ref (location_t loc, tree id, int fun, tree *type) |
1beb61f9 | 2441 | { |
2442 | tree ref; | |
2443 | tree decl = lookup_name (id); | |
e913c82d | 2444 | |
2445 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2446 | whatever lookup_name() found. */ | |
2447 | decl = objc_lookup_ivar (decl, id); | |
1beb61f9 | 2448 | |
841fdaa6 | 2449 | *type = NULL; |
31ddae9f | 2450 | if (decl && decl != error_mark_node) |
841fdaa6 | 2451 | { |
2452 | ref = decl; | |
2453 | *type = TREE_TYPE (ref); | |
2454 | } | |
31ddae9f | 2455 | else if (fun) |
2456 | /* Implicit function declaration. */ | |
e60a6f7b | 2457 | ref = implicitly_declare (loc, id); |
31ddae9f | 2458 | else if (decl == error_mark_node) |
2459 | /* Don't complain about something that's already been | |
2460 | complained about. */ | |
2461 | return error_mark_node; | |
2462 | else | |
2463 | { | |
e60a6f7b | 2464 | undeclared_variable (loc, id); |
31ddae9f | 2465 | return error_mark_node; |
2466 | } | |
1beb61f9 | 2467 | |
2468 | if (TREE_TYPE (ref) == error_mark_node) | |
2469 | return error_mark_node; | |
2470 | ||
31ddae9f | 2471 | if (TREE_DEPRECATED (ref)) |
45c4e798 | 2472 | warn_deprecated_use (ref, NULL_TREE); |
31ddae9f | 2473 | |
3d053e06 | 2474 | /* Recursive call does not count as usage. */ |
48e1416a | 2475 | if (ref != current_function_decl) |
3d053e06 | 2476 | { |
3d053e06 | 2477 | TREE_USED (ref) = 1; |
2478 | } | |
1beb61f9 | 2479 | |
9823d3a9 | 2480 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) |
2481 | { | |
2482 | if (!in_sizeof && !in_typeof) | |
2483 | C_DECL_USED (ref) = 1; | |
2484 | else if (DECL_INITIAL (ref) == 0 | |
2485 | && DECL_EXTERNAL (ref) | |
2486 | && !TREE_PUBLIC (ref)) | |
2487 | record_maybe_used_decl (ref); | |
2488 | } | |
2489 | ||
1beb61f9 | 2490 | if (TREE_CODE (ref) == CONST_DECL) |
2491 | { | |
89c30811 | 2492 | used_types_insert (TREE_TYPE (ref)); |
0b09525f | 2493 | |
2494 | if (warn_cxx_compat | |
2495 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2496 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2497 | { | |
2498 | warning_at (loc, OPT_Wc___compat, | |
2499 | ("enum constant defined in struct or union " | |
2500 | "is not visible in C++")); | |
2501 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2502 | } | |
2503 | ||
1beb61f9 | 2504 | ref = DECL_INITIAL (ref); |
2505 | TREE_CONSTANT (ref) = 1; | |
2506 | } | |
f3d3a8e2 | 2507 | else if (current_function_decl != 0 |
3a2bf8d3 | 2508 | && !DECL_FILE_SCOPE_P (current_function_decl) |
f3d3a8e2 | 2509 | && (TREE_CODE (ref) == VAR_DECL |
2510 | || TREE_CODE (ref) == PARM_DECL | |
2511 | || TREE_CODE (ref) == FUNCTION_DECL)) | |
2512 | { | |
2513 | tree context = decl_function_context (ref); | |
2b30d46c | 2514 | |
f3d3a8e2 | 2515 | if (context != 0 && context != current_function_decl) |
2516 | DECL_NONLOCAL (ref) = 1; | |
2517 | } | |
c42cc79e | 2518 | /* C99 6.7.4p3: An inline definition of a function with external |
2519 | linkage ... shall not contain a reference to an identifier with | |
2520 | internal linkage. */ | |
2521 | else if (current_function_decl != 0 | |
2522 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2523 | && DECL_EXTERNAL (current_function_decl) | |
2524 | && VAR_OR_FUNCTION_DECL_P (ref) | |
2525 | && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref)) | |
c55e2943 | 2526 | && ! TREE_PUBLIC (ref) |
2527 | && DECL_CONTEXT (ref) != current_function_decl) | |
551ed71a | 2528 | record_inline_static (loc, current_function_decl, ref, |
2529 | csi_internal); | |
1beb61f9 | 2530 | |
2531 | return ref; | |
2532 | } | |
2533 | ||
9823d3a9 | 2534 | /* Record details of decls possibly used inside sizeof or typeof. */ |
2535 | struct maybe_used_decl | |
2536 | { | |
2537 | /* The decl. */ | |
2538 | tree decl; | |
2539 | /* The level seen at (in_sizeof + in_typeof). */ | |
2540 | int level; | |
2541 | /* The next one at this level or above, or NULL. */ | |
2542 | struct maybe_used_decl *next; | |
2543 | }; | |
2544 | ||
2545 | static struct maybe_used_decl *maybe_used_decls; | |
2546 | ||
2547 | /* Record that DECL, an undefined static function reference seen | |
2548 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2549 | a VLA type or the operand of typeof is a variably modified | |
2550 | type. */ | |
2551 | ||
91ffb709 | 2552 | static void |
9823d3a9 | 2553 | record_maybe_used_decl (tree decl) |
2554 | { | |
2555 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2556 | t->decl = decl; | |
2557 | t->level = in_sizeof + in_typeof; | |
2558 | t->next = maybe_used_decls; | |
2559 | maybe_used_decls = t; | |
2560 | } | |
2561 | ||
2562 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2563 | USED is false, just discard them. If it is true, mark them used | |
2564 | (if no longer inside sizeof or typeof) or move them to the next | |
2565 | level up (if still inside sizeof or typeof). */ | |
2566 | ||
2567 | void | |
2568 | pop_maybe_used (bool used) | |
2569 | { | |
2570 | struct maybe_used_decl *p = maybe_used_decls; | |
2571 | int cur_level = in_sizeof + in_typeof; | |
2572 | while (p && p->level > cur_level) | |
2573 | { | |
2574 | if (used) | |
2575 | { | |
2576 | if (cur_level == 0) | |
2577 | C_DECL_USED (p->decl) = 1; | |
2578 | else | |
2579 | p->level = cur_level; | |
2580 | } | |
2581 | p = p->next; | |
2582 | } | |
2583 | if (!used || cur_level == 0) | |
2584 | maybe_used_decls = p; | |
2585 | } | |
2586 | ||
2587 | /* Return the result of sizeof applied to EXPR. */ | |
2588 | ||
2589 | struct c_expr | |
e60a6f7b | 2590 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) |
9823d3a9 | 2591 | { |
2592 | struct c_expr ret; | |
7d32f35d | 2593 | if (expr.value == error_mark_node) |
2594 | { | |
2595 | ret.value = error_mark_node; | |
2596 | ret.original_code = ERROR_MARK; | |
841fdaa6 | 2597 | ret.original_type = NULL; |
7d32f35d | 2598 | pop_maybe_used (false); |
2599 | } | |
2600 | else | |
2601 | { | |
a75b1c71 | 2602 | bool expr_const_operands = true; |
2603 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, | |
2604 | &expr_const_operands); | |
e60a6f7b | 2605 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); |
7d32f35d | 2606 | ret.original_code = ERROR_MARK; |
841fdaa6 | 2607 | ret.original_type = NULL; |
a75b1c71 | 2608 | if (c_vla_type_p (TREE_TYPE (folded_expr))) |
32d33ab2 | 2609 | { |
2610 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
a75b1c71 | 2611 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2612 | folded_expr, ret.value); | |
2613 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
e60a6f7b | 2614 | SET_EXPR_LOCATION (ret.value, loc); |
32d33ab2 | 2615 | } |
a75b1c71 | 2616 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); |
7d32f35d | 2617 | } |
9823d3a9 | 2618 | return ret; |
2619 | } | |
2620 | ||
2621 | /* Return the result of sizeof applied to T, a structure for the type | |
e60a6f7b | 2622 | name passed to sizeof (rather than the type itself). LOC is the |
2623 | location of the original expression. */ | |
9823d3a9 | 2624 | |
2625 | struct c_expr | |
e60a6f7b | 2626 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) |
9823d3a9 | 2627 | { |
2628 | tree type; | |
2629 | struct c_expr ret; | |
a75b1c71 | 2630 | tree type_expr = NULL_TREE; |
2631 | bool type_expr_const = true; | |
2632 | type = groktypename (t, &type_expr, &type_expr_const); | |
e60a6f7b | 2633 | ret.value = c_sizeof (loc, type); |
9823d3a9 | 2634 | ret.original_code = ERROR_MARK; |
841fdaa6 | 2635 | ret.original_type = NULL; |
e8074982 | 2636 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) |
2637 | && c_vla_type_p (type)) | |
a75b1c71 | 2638 | { |
e8074982 | 2639 | /* If the type is a [*] array, it is a VLA but is represented as |
2640 | having a size of zero. In such a case we must ensure that | |
2641 | the result of sizeof does not get folded to a constant by | |
2642 | c_fully_fold, because if the size is evaluated the result is | |
2643 | not constant and so constraints on zero or negative size | |
2644 | arrays must not be applied when this sizeof call is inside | |
2645 | another array declarator. */ | |
2646 | if (!type_expr) | |
2647 | type_expr = integer_zero_node; | |
a75b1c71 | 2648 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2649 | type_expr, ret.value); | |
2650 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2651 | } | |
8a98ec4a | 2652 | pop_maybe_used (type != error_mark_node |
2653 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
9823d3a9 | 2654 | return ret; |
2655 | } | |
2656 | ||
628fa4f8 | 2657 | /* Build a function call to function FUNCTION with parameters PARAMS. |
e60a6f7b | 2658 | The function call is at LOC. |
628fa4f8 | 2659 | PARAMS is a list--a chain of TREE_LIST nodes--in which the |
2660 | TREE_VALUE of each node is a parameter-expression. | |
2661 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2662 | ||
2663 | tree | |
e60a6f7b | 2664 | build_function_call (location_t loc, tree function, tree params) |
b9c74b4d | 2665 | { |
2666 | VEC(tree,gc) *vec; | |
2667 | tree ret; | |
2668 | ||
2669 | vec = VEC_alloc (tree, gc, list_length (params)); | |
2670 | for (; params; params = TREE_CHAIN (params)) | |
2671 | VEC_quick_push (tree, vec, TREE_VALUE (params)); | |
e60a6f7b | 2672 | ret = build_function_call_vec (loc, function, vec, NULL); |
b9c74b4d | 2673 | VEC_free (tree, gc, vec); |
2674 | return ret; | |
2675 | } | |
2676 | ||
28738b20 | 2677 | /* Give a note about the location of the declaration of DECL. */ |
2678 | ||
2679 | static void inform_declaration (tree decl) | |
2680 | { | |
2681 | if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl))) | |
2682 | inform (DECL_SOURCE_LOCATION (decl), "declared here"); | |
2683 | } | |
2684 | ||
b9c74b4d | 2685 | /* Build a function call to function FUNCTION with parameters PARAMS. |
2686 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2687 | either NULL or the original type of the corresponding element in | |
2688 | PARAMS. The original type may differ from TREE_TYPE of the | |
2689 | parameter for enums. FUNCTION's data type may be a function type | |
2690 | or pointer-to-function. This function changes the elements of | |
2691 | PARAMS. */ | |
2692 | ||
2693 | tree | |
e60a6f7b | 2694 | build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params, |
b9c74b4d | 2695 | VEC(tree,gc) *origtypes) |
628fa4f8 | 2696 | { |
19cb6b50 | 2697 | tree fntype, fundecl = 0; |
805e22b2 | 2698 | tree name = NULL_TREE, result; |
59798ded | 2699 | tree tem; |
d01f58f9 | 2700 | int nargs; |
2701 | tree *argarray; | |
48e1416a | 2702 | |
628fa4f8 | 2703 | |
a390f7f2 | 2704 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
fe4d64a0 | 2705 | STRIP_TYPE_NOPS (function); |
628fa4f8 | 2706 | |
2707 | /* Convert anything with function type to a pointer-to-function. */ | |
2708 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2709 | { | |
65441f6f | 2710 | /* Implement type-directed function overloading for builtins. |
2711 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
2712 | handle all the type checking. The result is a complete expression | |
2713 | that implements this function call. */ | |
e60a6f7b | 2714 | tem = resolve_overloaded_builtin (loc, function, params); |
65441f6f | 2715 | if (tem) |
2716 | return tem; | |
b6a5fc45 | 2717 | |
628fa4f8 | 2718 | name = DECL_NAME (function); |
4c0315d0 | 2719 | |
2720 | if (flag_tm) | |
2721 | tm_malloc_replacement (function); | |
25fd8fda | 2722 | fundecl = function; |
1cd6e20d | 2723 | /* Atomic functions have type checking/casting already done. They are |
2724 | often rewritten and don't match the original parameter list. */ | |
2725 | if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) | |
2726 | origtypes = NULL; | |
628fa4f8 | 2727 | } |
e78cf9a7 | 2728 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) |
e60a6f7b | 2729 | function = function_to_pointer_conversion (loc, function); |
628fa4f8 | 2730 | |
ddb5d39d | 2731 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2732 | expressions, like those used for ObjC messenger dispatches. */ | |
b9c74b4d | 2733 | if (!VEC_empty (tree, params)) |
2734 | function = objc_rewrite_function_call (function, | |
2735 | VEC_index (tree, params, 0)); | |
ddb5d39d | 2736 | |
a75b1c71 | 2737 | function = c_fully_fold (function, false, NULL); |
2738 | ||
628fa4f8 | 2739 | fntype = TREE_TYPE (function); |
2740 | ||
2741 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2742 | return error_mark_node; | |
2743 | ||
2744 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2745 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2746 | { | |
28738b20 | 2747 | if (!flag_diagnostics_show_caret) |
2748 | error_at (loc, | |
2749 | "called object %qE is not a function or function pointer", | |
2750 | function); | |
2751 | else if (DECL_P (function)) | |
2752 | { | |
2753 | error_at (loc, | |
2754 | "called object %qD is not a function or function pointer", | |
2755 | function); | |
2756 | inform_declaration (function); | |
2757 | } | |
2758 | else | |
2759 | error_at (loc, | |
2760 | "called object is not a function or function pointer"); | |
628fa4f8 | 2761 | return error_mark_node; |
2762 | } | |
2763 | ||
af857942 | 2764 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
2765 | current_function_returns_abnormally = 1; | |
2766 | ||
628fa4f8 | 2767 | /* fntype now gets the type of function pointed to. */ |
2768 | fntype = TREE_TYPE (fntype); | |
2769 | ||
ad0e7a06 | 2770 | /* Convert the parameters to the types declared in the |
2771 | function prototype, or apply default promotions. */ | |
2772 | ||
b9c74b4d | 2773 | nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes, |
2774 | function, fundecl); | |
ad0e7a06 | 2775 | if (nargs < 0) |
2776 | return error_mark_node; | |
2777 | ||
59798ded | 2778 | /* Check that the function is called through a compatible prototype. |
2779 | If it is not, replace the call by a trap, wrapped up in a compound | |
2780 | expression if necessary. This has the nice side-effect to prevent | |
2781 | the tree-inliner from generating invalid assignment trees which may | |
34d3c5de | 2782 | blow up in the RTL expander later. */ |
72dd6141 | 2783 | if (CONVERT_EXPR_P (function) |
59798ded | 2784 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR |
2785 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
84166705 | 2786 | && !comptypes (fntype, TREE_TYPE (tem))) |
59798ded | 2787 | { |
2788 | tree return_type = TREE_TYPE (fntype); | |
b9a16870 | 2789 | tree trap = build_function_call (loc, |
2790 | builtin_decl_explicit (BUILT_IN_TRAP), | |
59798ded | 2791 | NULL_TREE); |
ad0e7a06 | 2792 | int i; |
59798ded | 2793 | |
2794 | /* This situation leads to run-time undefined behavior. We can't, | |
2795 | therefore, simply error unless we can prove that all possible | |
2796 | executions of the program must execute the code. */ | |
e60a6f7b | 2797 | if (warning_at (loc, 0, "function called through a non-compatible type")) |
a52d5726 | 2798 | /* We can, however, treat "undefined" any way we please. |
2799 | Call abort to encourage the user to fix the program. */ | |
e60a6f7b | 2800 | inform (loc, "if this code is reached, the program will abort"); |
ad0e7a06 | 2801 | /* Before the abort, allow the function arguments to exit or |
2802 | call longjmp. */ | |
2803 | for (i = 0; i < nargs; i++) | |
b9c74b4d | 2804 | trap = build2 (COMPOUND_EXPR, void_type_node, |
2805 | VEC_index (tree, params, i), trap); | |
82e18817 | 2806 | |
59798ded | 2807 | if (VOID_TYPE_P (return_type)) |
8f24e4bd | 2808 | { |
2809 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
389dd41b | 2810 | pedwarn (loc, 0, |
8f24e4bd | 2811 | "function with qualified void return type called"); |
2812 | return trap; | |
2813 | } | |
59798ded | 2814 | else |
2815 | { | |
2816 | tree rhs; | |
2817 | ||
2818 | if (AGGREGATE_TYPE_P (return_type)) | |
e60a6f7b | 2819 | rhs = build_compound_literal (loc, return_type, |
a75b1c71 | 2820 | build_constructor (return_type, 0), |
2821 | false); | |
59798ded | 2822 | else |
385f3f36 | 2823 | rhs = build_zero_cst (return_type); |
59798ded | 2824 | |
8f24e4bd | 2825 | return require_complete_type (build2 (COMPOUND_EXPR, return_type, |
2826 | trap, rhs)); | |
59798ded | 2827 | } |
2828 | } | |
2829 | ||
b9c74b4d | 2830 | argarray = VEC_address (tree, params); |
2831 | ||
d43cee80 | 2832 | /* Check that arguments to builtin functions match the expectations. */ |
2833 | if (fundecl | |
2834 | && DECL_BUILT_IN (fundecl) | |
2835 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2836 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2837 | return error_mark_node; | |
628fa4f8 | 2838 | |
d43cee80 | 2839 | /* Check that the arguments to the function are valid. */ |
774e9d58 | 2840 | check_function_arguments (fntype, nargs, argarray); |
628fa4f8 | 2841 | |
a75b1c71 | 2842 | if (name != NULL_TREE |
2843 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
16cb78b3 | 2844 | { |
a75b1c71 | 2845 | if (require_constant_value) |
48e1416a | 2846 | result = |
389dd41b | 2847 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), |
2848 | function, nargs, argarray); | |
a75b1c71 | 2849 | else |
389dd41b | 2850 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), |
2851 | function, nargs, argarray); | |
a75b1c71 | 2852 | if (TREE_CODE (result) == NOP_EXPR |
2853 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2854 | STRIP_TYPE_NOPS (result); | |
16cb78b3 | 2855 | } |
2856 | else | |
389dd41b | 2857 | result = build_call_array_loc (loc, TREE_TYPE (fntype), |
2858 | function, nargs, argarray); | |
650e4c94 | 2859 | |
5c803577 | 2860 | if (VOID_TYPE_P (TREE_TYPE (result))) |
8f24e4bd | 2861 | { |
2862 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
389dd41b | 2863 | pedwarn (loc, 0, |
8f24e4bd | 2864 | "function with qualified void return type called"); |
2865 | return result; | |
2866 | } | |
a4110d9a | 2867 | return require_complete_type (result); |
628fa4f8 | 2868 | } |
2869 | \f | |
b9c74b4d | 2870 | /* Convert the argument expressions in the vector VALUES |
2871 | to the types in the list TYPELIST. | |
628fa4f8 | 2872 | |
2873 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
2874 | perform the default conversions. | |
2875 | ||
b9c74b4d | 2876 | ORIGTYPES is the original types of the expressions in VALUES. This |
2877 | holds the type of enum values which have been converted to integral | |
2878 | types. It may be NULL. | |
628fa4f8 | 2879 | |
8ccb0952 | 2880 | FUNCTION is a tree for the called function. It is used only for |
2881 | error messages, where it is formatted with %qE. | |
628fa4f8 | 2882 | |
2883 | This is also where warnings about wrong number of args are generated. | |
2884 | ||
d01f58f9 | 2885 | Returns the actual number of arguments processed (which may be less |
b9c74b4d | 2886 | than the length of VALUES in some error situations), or -1 on |
2887 | failure. */ | |
d01f58f9 | 2888 | |
2889 | static int | |
b9c74b4d | 2890 | convert_arguments (tree typelist, VEC(tree,gc) *values, |
2891 | VEC(tree,gc) *origtypes, tree function, tree fundecl) | |
628fa4f8 | 2892 | { |
b9c74b4d | 2893 | tree typetail, val; |
2894 | unsigned int parmnum; | |
cb60919f | 2895 | bool error_args = false; |
b5c26b42 | 2896 | const bool type_generic = fundecl |
2897 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl))); | |
c6418a4e | 2898 | bool type_generic_remove_excess_precision = false; |
8ccb0952 | 2899 | tree selector; |
8ccb0952 | 2900 | |
245d6740 | 2901 | /* Change pointer to function to the function itself for |
2902 | diagnostics. */ | |
8ccb0952 | 2903 | if (TREE_CODE (function) == ADDR_EXPR |
2904 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
245d6740 | 2905 | function = TREE_OPERAND (function, 0); |
8ccb0952 | 2906 | |
2907 | /* Handle an ObjC selector specially for diagnostics. */ | |
2908 | selector = objc_message_selector (); | |
628fa4f8 | 2909 | |
c6418a4e | 2910 | /* For type-generic built-in functions, determine whether excess |
2911 | precision should be removed (classification) or not | |
2912 | (comparison). */ | |
2913 | if (type_generic | |
2914 | && DECL_BUILT_IN (fundecl) | |
2915 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
2916 | { | |
2917 | switch (DECL_FUNCTION_CODE (fundecl)) | |
2918 | { | |
2919 | case BUILT_IN_ISFINITE: | |
2920 | case BUILT_IN_ISINF: | |
2921 | case BUILT_IN_ISINF_SIGN: | |
2922 | case BUILT_IN_ISNAN: | |
2923 | case BUILT_IN_ISNORMAL: | |
2924 | case BUILT_IN_FPCLASSIFY: | |
2925 | type_generic_remove_excess_precision = true; | |
2926 | break; | |
2927 | ||
2928 | default: | |
2929 | type_generic_remove_excess_precision = false; | |
2930 | break; | |
2931 | } | |
2932 | } | |
2933 | ||
628fa4f8 | 2934 | /* Scan the given expressions and types, producing individual |
b9c74b4d | 2935 | converted arguments. */ |
628fa4f8 | 2936 | |
b9c74b4d | 2937 | for (typetail = typelist, parmnum = 0; |
2938 | VEC_iterate (tree, values, parmnum, val); | |
2939 | ++parmnum) | |
628fa4f8 | 2940 | { |
19cb6b50 | 2941 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
c6418a4e | 2942 | tree valtype = TREE_TYPE (val); |
8ccb0952 | 2943 | tree rname = function; |
2944 | int argnum = parmnum + 1; | |
10fda9eb | 2945 | const char *invalid_func_diag; |
c6418a4e | 2946 | bool excess_precision = false; |
a75b1c71 | 2947 | bool npc; |
b9c74b4d | 2948 | tree parmval; |
628fa4f8 | 2949 | |
2950 | if (type == void_type_node) | |
2951 | { | |
1f3a51d6 | 2952 | if (selector) |
2953 | error_at (input_location, | |
2954 | "too many arguments to method %qE", selector); | |
2955 | else | |
2956 | error_at (input_location, | |
2957 | "too many arguments to function %qE", function); | |
28738b20 | 2958 | inform_declaration (fundecl); |
d01f58f9 | 2959 | return parmnum; |
628fa4f8 | 2960 | } |
2961 | ||
8ccb0952 | 2962 | if (selector && argnum > 2) |
2963 | { | |
2964 | rname = selector; | |
2965 | argnum -= 2; | |
2966 | } | |
2967 | ||
a75b1c71 | 2968 | npc = null_pointer_constant_p (val); |
c6418a4e | 2969 | |
2970 | /* If there is excess precision and a prototype, convert once to | |
2971 | the required type rather than converting via the semantic | |
2972 | type. Likewise without a prototype a float value represented | |
2973 | as long double should be converted once to double. But for | |
2974 | type-generic classification functions excess precision must | |
2975 | be removed here. */ | |
2976 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
2977 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
2978 | { | |
2979 | val = TREE_OPERAND (val, 0); | |
2980 | excess_precision = true; | |
2981 | } | |
a75b1c71 | 2982 | val = c_fully_fold (val, false, NULL); |
a97c952c | 2983 | STRIP_TYPE_NOPS (val); |
628fa4f8 | 2984 | |
628fa4f8 | 2985 | val = require_complete_type (val); |
2986 | ||
2987 | if (type != 0) | |
2988 | { | |
2989 | /* Formal parm type is specified by a function prototype. */ | |
628fa4f8 | 2990 | |
d487ebe8 | 2991 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) |
628fa4f8 | 2992 | { |
2993 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
2994 | parmval = val; | |
2995 | } | |
2996 | else | |
2997 | { | |
b9c74b4d | 2998 | tree origtype; |
2999 | ||
f9e15121 | 3000 | /* Optionally warn about conversions that |
3001 | differ from the default conversions. */ | |
583272a2 | 3002 | if (warn_traditional_conversion || warn_traditional) |
628fa4f8 | 3003 | { |
e916c70c | 3004 | unsigned int formal_prec = TYPE_PRECISION (type); |
628fa4f8 | 3005 | |
054521db | 3006 | if (INTEGRAL_TYPE_P (type) |
c6418a4e | 3007 | && TREE_CODE (valtype) == REAL_TYPE) |
c3ceba8e | 3008 | warning (0, "passing argument %d of %qE as integer " |
8ccb0952 | 3009 | "rather than floating due to prototype", |
3010 | argnum, rname); | |
af324fb3 | 3011 | if (INTEGRAL_TYPE_P (type) |
c6418a4e | 3012 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
c3ceba8e | 3013 | warning (0, "passing argument %d of %qE as integer " |
8ccb0952 | 3014 | "rather than complex due to prototype", |
3015 | argnum, rname); | |
054521db | 3016 | else if (TREE_CODE (type) == COMPLEX_TYPE |
c6418a4e | 3017 | && TREE_CODE (valtype) == REAL_TYPE) |
c3ceba8e | 3018 | warning (0, "passing argument %d of %qE as complex " |
8ccb0952 | 3019 | "rather than floating due to prototype", |
3020 | argnum, rname); | |
628fa4f8 | 3021 | else if (TREE_CODE (type) == REAL_TYPE |
c6418a4e | 3022 | && INTEGRAL_TYPE_P (valtype)) |
c3ceba8e | 3023 | warning (0, "passing argument %d of %qE as floating " |
8ccb0952 | 3024 | "rather than integer due to prototype", |
3025 | argnum, rname); | |
af324fb3 | 3026 | else if (TREE_CODE (type) == COMPLEX_TYPE |
c6418a4e | 3027 | && INTEGRAL_TYPE_P (valtype)) |
c3ceba8e | 3028 | warning (0, "passing argument %d of %qE as complex " |
8ccb0952 | 3029 | "rather than integer due to prototype", |
3030 | argnum, rname); | |
054521db | 3031 | else if (TREE_CODE (type) == REAL_TYPE |
c6418a4e | 3032 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
c3ceba8e | 3033 | warning (0, "passing argument %d of %qE as floating " |
8ccb0952 | 3034 | "rather than complex due to prototype", |
3035 | argnum, rname); | |
054521db | 3036 | /* ??? At some point, messages should be written about |
3037 | conversions between complex types, but that's too messy | |
3038 | to do now. */ | |
f9e15121 | 3039 | else if (TREE_CODE (type) == REAL_TYPE |
c6418a4e | 3040 | && TREE_CODE (valtype) == REAL_TYPE) |
f9e15121 | 3041 | { |
3042 | /* Warn if any argument is passed as `float', | |
66618a1e | 3043 | since without a prototype it would be `double'. */ |
c4503c0a | 3044 | if (formal_prec == TYPE_PRECISION (float_type_node) |
3045 | && type != dfloat32_type_node) | |
c3ceba8e | 3046 | warning (0, "passing argument %d of %qE as %<float%> " |
8ccb0952 | 3047 | "rather than %<double%> due to prototype", |
3048 | argnum, rname); | |
c4503c0a | 3049 | |
3050 | /* Warn if mismatch between argument and prototype | |
3051 | for decimal float types. Warn of conversions with | |
3052 | binary float types and of precision narrowing due to | |
3053 | prototype. */ | |
c6418a4e | 3054 | else if (type != valtype |
c4503c0a | 3055 | && (type == dfloat32_type_node |
3056 | || type == dfloat64_type_node | |
a0c938f0 | 3057 | || type == dfloat128_type_node |
c6418a4e | 3058 | || valtype == dfloat32_type_node |
3059 | || valtype == dfloat64_type_node | |
3060 | || valtype == dfloat128_type_node) | |
a0c938f0 | 3061 | && (formal_prec |
c6418a4e | 3062 | <= TYPE_PRECISION (valtype) |
c4503c0a | 3063 | || (type == dfloat128_type_node |
c6418a4e | 3064 | && (valtype |
a0c938f0 | 3065 | != dfloat64_type_node |
c6418a4e | 3066 | && (valtype |
c4503c0a | 3067 | != dfloat32_type_node))) |
3068 | || (type == dfloat64_type_node | |
c6418a4e | 3069 | && (valtype |
c4503c0a | 3070 | != dfloat32_type_node)))) |
3071 | warning (0, "passing argument %d of %qE as %qT " | |
3072 | "rather than %qT due to prototype", | |
c6418a4e | 3073 | argnum, rname, type, valtype); |
c4503c0a | 3074 | |
f9e15121 | 3075 | } |
261366e3 | 3076 | /* Detect integer changing in width or signedness. |
3077 | These warnings are only activated with | |
583272a2 | 3078 | -Wtraditional-conversion, not with -Wtraditional. */ |
3079 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
c6418a4e | 3080 | && INTEGRAL_TYPE_P (valtype)) |
628fa4f8 | 3081 | { |
f9e15121 | 3082 | tree would_have_been = default_conversion (val); |
3083 | tree type1 = TREE_TYPE (would_have_been); | |
3084 | ||
a14622ee | 3085 | if (TREE_CODE (type) == ENUMERAL_TYPE |
bab5a44d | 3086 | && (TYPE_MAIN_VARIANT (type) |
c6418a4e | 3087 | == TYPE_MAIN_VARIANT (valtype))) |
a14622ee | 3088 | /* No warning if function asks for enum |
3089 | and the actual arg is that enum type. */ | |
3090 | ; | |
3091 | else if (formal_prec != TYPE_PRECISION (type1)) | |
e60a6f7b | 3092 | warning (OPT_Wtraditional_conversion, |
3093 | "passing argument %d of %qE " | |
6bf97f82 | 3094 | "with different width due to prototype", |
3095 | argnum, rname); | |
78a8ed03 | 3096 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
f9e15121 | 3097 | ; |
1ca23b2d | 3098 | /* Don't complain if the formal parameter type |
3099 | is an enum, because we can't tell now whether | |
3100 | the value was an enum--even the same enum. */ | |
3101 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
3102 | ; | |
628fa4f8 | 3103 | else if (TREE_CODE (val) == INTEGER_CST |
3104 | && int_fits_type_p (val, type)) | |
3105 | /* Change in signedness doesn't matter | |
3106 | if a constant value is unaffected. */ | |
3107 | ; | |
c6abd4de | 3108 | /* If the value is extended from a narrower |
3109 | unsigned type, it doesn't matter whether we | |
3110 | pass it as signed or unsigned; the value | |
3111 | certainly is the same either way. */ | |
c6418a4e | 3112 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) |
3113 | && TYPE_UNSIGNED (valtype)) | |
c6abd4de | 3114 | ; |
78a8ed03 | 3115 | else if (TYPE_UNSIGNED (type)) |
e60a6f7b | 3116 | warning (OPT_Wtraditional_conversion, |
3117 | "passing argument %d of %qE " | |
6bf97f82 | 3118 | "as unsigned due to prototype", |
3119 | argnum, rname); | |
261366e3 | 3120 | else |
e60a6f7b | 3121 | warning (OPT_Wtraditional_conversion, |
3122 | "passing argument %d of %qE " | |
6bf97f82 | 3123 | "as signed due to prototype", argnum, rname); |
628fa4f8 | 3124 | } |
3125 | } | |
3126 | ||
c6418a4e | 3127 | /* Possibly restore an EXCESS_PRECISION_EXPR for the |
3128 | sake of better warnings from convert_and_check. */ | |
3129 | if (excess_precision) | |
3130 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
b9c74b4d | 3131 | origtype = (origtypes == NULL |
3132 | ? NULL_TREE | |
3133 | : VEC_index (tree, origtypes, parmnum)); | |
d5b637fa | 3134 | parmval = convert_for_assignment (input_location, type, val, |
3135 | origtype, ic_argpass, npc, | |
245d6740 | 3136 | fundecl, function, |
3137 | parmnum + 1); | |
2b30d46c | 3138 | |
45550790 | 3139 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
a12f4e51 | 3140 | && INTEGRAL_TYPE_P (type) |
628fa4f8 | 3141 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
3142 | parmval = default_conversion (parmval); | |
628fa4f8 | 3143 | } |
628fa4f8 | 3144 | } |
c6418a4e | 3145 | else if (TREE_CODE (valtype) == REAL_TYPE |
3146 | && (TYPE_PRECISION (valtype) | |
a0c938f0 | 3147 | < TYPE_PRECISION (double_type_node)) |
c6418a4e | 3148 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) |
b5c26b42 | 3149 | { |
3150 | if (type_generic) | |
b9c74b4d | 3151 | parmval = val; |
b5c26b42 | 3152 | else |
c920faa3 | 3153 | { |
3154 | /* Convert `float' to `double'. */ | |
3155 | if (warn_double_promotion && !c_inhibit_evaluation_warnings) | |
3156 | warning (OPT_Wdouble_promotion, | |
3157 | "implicit conversion from %qT to %qT when passing " | |
3158 | "argument to function", | |
3159 | valtype, double_type_node); | |
3160 | parmval = convert (double_type_node, val); | |
3161 | } | |
b5c26b42 | 3162 | } |
c6418a4e | 3163 | else if (excess_precision && !type_generic) |
3164 | /* A "double" argument with excess precision being passed | |
3165 | without a prototype or in variable arguments. */ | |
b9c74b4d | 3166 | parmval = convert (valtype, val); |
a0c938f0 | 3167 | else if ((invalid_func_diag = |
3168 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
10fda9eb | 3169 | { |
3170 | error (invalid_func_diag); | |
d01f58f9 | 3171 | return -1; |
10fda9eb | 3172 | } |
628fa4f8 | 3173 | else |
3174 | /* Convert `short' and `char' to full-size `int'. */ | |
b9c74b4d | 3175 | parmval = default_conversion (val); |
3176 | ||
3177 | VEC_replace (tree, values, parmnum, parmval); | |
cb60919f | 3178 | if (parmval == error_mark_node) |
3179 | error_args = true; | |
628fa4f8 | 3180 | |
3181 | if (typetail) | |
3182 | typetail = TREE_CHAIN (typetail); | |
3183 | } | |
3184 | ||
b9c74b4d | 3185 | gcc_assert (parmnum == VEC_length (tree, values)); |
d01f58f9 | 3186 | |
628fa4f8 | 3187 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) |
7b2e6555 | 3188 | { |
6cf89e04 | 3189 | error_at (input_location, |
60cce472 | 3190 | "too few arguments to function %qE", function); |
28738b20 | 3191 | inform_declaration (fundecl); |
d01f58f9 | 3192 | return -1; |
7b2e6555 | 3193 | } |
628fa4f8 | 3194 | |
cb60919f | 3195 | return error_args ? -1 : (int) parmnum; |
628fa4f8 | 3196 | } |
3197 | \f | |
5381eb98 | 3198 | /* This is the entry point used by the parser to build unary operators |
3199 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3200 | ARG is the operand. For unary plus, the C parser currently uses | |
dda49785 | 3201 | CONVERT_EXPR for code. |
3202 | ||
3203 | LOC is the location to use for the tree generated. | |
3204 | */ | |
5381eb98 | 3205 | |
3206 | struct c_expr | |
e60a6f7b | 3207 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) |
5381eb98 | 3208 | { |
3209 | struct c_expr result; | |
3210 | ||
b6889cb0 | 3211 | result.value = build_unary_op (loc, code, arg.value, 0); |
82012ffe | 3212 | result.original_code = code; |
841fdaa6 | 3213 | result.original_type = NULL; |
3214 | ||
f170d67f | 3215 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) |
e60a6f7b | 3216 | overflow_warning (loc, result.value); |
f170d67f | 3217 | |
5381eb98 | 3218 | return result; |
3219 | } | |
3220 | ||
3221 | /* This is the entry point used by the parser to build binary operators | |
3222 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3223 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3224 | expression, we check for operands that were written with other binary | |
8e70fb09 | 3225 | operators in a way that is likely to confuse the user. |
3226 | ||
3227 | LOCATION is the location of the binary operator. */ | |
a9ba5ac4 | 3228 | |
066f26bf | 3229 | struct c_expr |
8e70fb09 | 3230 | parser_build_binary_op (location_t location, enum tree_code code, |
3231 | struct c_expr arg1, struct c_expr arg2) | |
628fa4f8 | 3232 | { |
066f26bf | 3233 | struct c_expr result; |
628fa4f8 | 3234 | |
066f26bf | 3235 | enum tree_code code1 = arg1.original_code; |
3236 | enum tree_code code2 = arg2.original_code; | |
841fdaa6 | 3237 | tree type1 = (arg1.original_type |
3238 | ? arg1.original_type | |
3239 | : TREE_TYPE (arg1.value)); | |
3240 | tree type2 = (arg2.original_type | |
3241 | ? arg2.original_type | |
3242 | : TREE_TYPE (arg2.value)); | |
628fa4f8 | 3243 | |
8e70fb09 | 3244 | result.value = build_binary_op (location, code, |
3245 | arg1.value, arg2.value, 1); | |
066f26bf | 3246 | result.original_code = code; |
841fdaa6 | 3247 | result.original_type = NULL; |
5eaf60b3 | 3248 | |
066f26bf | 3249 | if (TREE_CODE (result.value) == ERROR_MARK) |
3250 | return result; | |
628fa4f8 | 3251 | |
8e70fb09 | 3252 | if (location != UNKNOWN_LOCATION) |
3253 | protected_set_expr_location (result.value, location); | |
3254 | ||
628fa4f8 | 3255 | /* Check for cases such as x+y<<z which users are likely |
066f26bf | 3256 | to misinterpret. */ |
628fa4f8 | 3257 | if (warn_parentheses) |
82012ffe | 3258 | warn_about_parentheses (code, code1, arg1.value, code2, arg2.value); |
8ee1dd9f | 3259 | |
03033af4 | 3260 | if (warn_logical_op) |
9c20c4fc | 3261 | warn_logical_operator (input_location, code, TREE_TYPE (result.value), |
03033af4 | 3262 | code1, arg1.value, code2, arg2.value); |
b13d1547 | 3263 | |
19f0596c | 3264 | /* Warn about comparisons against string literals, with the exception |
3265 | of testing for equality or inequality of a string literal with NULL. */ | |
3266 | if (code == EQ_EXPR || code == NE_EXPR) | |
3267 | { | |
3268 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3269 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
e60a6f7b | 3270 | warning_at (location, OPT_Waddress, |
3271 | "comparison with string literal results in unspecified behavior"); | |
19f0596c | 3272 | } |
3273 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3274 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
e60a6f7b | 3275 | warning_at (location, OPT_Waddress, |
3276 | "comparison with string literal results in unspecified behavior"); | |
19f0596c | 3277 | |
48e1416a | 3278 | if (TREE_OVERFLOW_P (result.value) |
3279 | && !TREE_OVERFLOW_P (arg1.value) | |
f170d67f | 3280 | && !TREE_OVERFLOW_P (arg2.value)) |
e60a6f7b | 3281 | overflow_warning (location, result.value); |
628fa4f8 | 3282 | |
841fdaa6 | 3283 | /* Warn about comparisons of different enum types. */ |
3284 | if (warn_enum_compare | |
3285 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3286 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3287 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3288 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3289 | warning_at (location, OPT_Wenum_compare, | |
3290 | "comparison between %qT and %qT", | |
3291 | type1, type2); | |
3292 | ||
628fa4f8 | 3293 | return result; |
3294 | } | |
276beea2 | 3295 | \f |
276beea2 | 3296 | /* Return a tree for the difference of pointers OP0 and OP1. |
3297 | The resulting tree has type int. */ | |
367d2866 | 3298 | |
276beea2 | 3299 | static tree |
389dd41b | 3300 | pointer_diff (location_t loc, tree op0, tree op1) |
276beea2 | 3301 | { |
276beea2 | 3302 | tree restype = ptrdiff_type_node; |
6d5d708e | 3303 | tree result, inttype; |
628fa4f8 | 3304 | |
6d5d708e | 3305 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); |
3306 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
276beea2 | 3307 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
3308 | tree con0, con1, lit0, lit1; | |
3309 | tree orig_op1 = op1; | |
628fa4f8 | 3310 | |
6d5d708e | 3311 | /* If the operands point into different address spaces, we need to |
3312 | explicitly convert them to pointers into the common address space | |
3313 | before we can subtract the numerical address values. */ | |
3314 | if (as0 != as1) | |
3315 | { | |
3316 | addr_space_t as_common; | |
3317 | tree common_type; | |
3318 | ||
3319 | /* Determine the common superset address space. This is guaranteed | |
3320 | to exist because the caller verified that comp_target_types | |
3321 | returned non-zero. */ | |
3322 | if (!addr_space_superset (as0, as1, &as_common)) | |
3323 | gcc_unreachable (); | |
3324 | ||
3325 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3326 | op0 = convert (common_type, op0); | |
3327 | op1 = convert (common_type, op1); | |
3328 | } | |
3329 | ||
3330 | /* Determine integer type to perform computations in. This will usually | |
3331 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3332 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3333 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
3c9b85aa | 3334 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); |
6d5d708e | 3335 | else |
3336 | inttype = restype; | |
3337 | ||
3338 | ||
8864917d | 3339 | if (TREE_CODE (target_type) == VOID_TYPE) |
29438999 | 3340 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 3341 | "pointer of type %<void *%> used in subtraction"); |
3342 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
29438999 | 3343 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 3344 | "pointer to a function used in subtraction"); |
628fa4f8 | 3345 | |
276beea2 | 3346 | /* If the conversion to ptrdiff_type does anything like widening or |
3347 | converting a partial to an integral mode, we get a convert_expression | |
3348 | that is in the way to do any simplifications. | |
3349 | (fold-const.c doesn't know that the extra bits won't be needed. | |
3350 | split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a | |
3351 | different mode in place.) | |
3352 | So first try to find a common term here 'by hand'; we want to cover | |
3353 | at least the cases that occur in legal static initializers. */ | |
72dd6141 | 3354 | if (CONVERT_EXPR_P (op0) |
c44afe23 | 3355 | && (TYPE_PRECISION (TREE_TYPE (op0)) |
3356 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))) | |
3357 | con0 = TREE_OPERAND (op0, 0); | |
3358 | else | |
3359 | con0 = op0; | |
72dd6141 | 3360 | if (CONVERT_EXPR_P (op1) |
c44afe23 | 3361 | && (TYPE_PRECISION (TREE_TYPE (op1)) |
3362 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))) | |
3363 | con1 = TREE_OPERAND (op1, 0); | |
3364 | else | |
3365 | con1 = op1; | |
628fa4f8 | 3366 | |
555c3766 | 3367 | if (TREE_CODE (con0) == POINTER_PLUS_EXPR) |
276beea2 | 3368 | { |
3369 | lit0 = TREE_OPERAND (con0, 1); | |
3370 | con0 = TREE_OPERAND (con0, 0); | |
3371 | } | |
3372 | else | |
3373 | lit0 = integer_zero_node; | |
628fa4f8 | 3374 | |
555c3766 | 3375 | if (TREE_CODE (con1) == POINTER_PLUS_EXPR) |
628fa4f8 | 3376 | { |
276beea2 | 3377 | lit1 = TREE_OPERAND (con1, 1); |
3378 | con1 = TREE_OPERAND (con1, 0); | |
628fa4f8 | 3379 | } |
3380 | else | |
276beea2 | 3381 | lit1 = integer_zero_node; |
3382 | ||
3383 | if (operand_equal_p (con0, con1, 0)) | |
628fa4f8 | 3384 | { |
276beea2 | 3385 | op0 = lit0; |
3386 | op1 = lit1; | |
628fa4f8 | 3387 | } |
3388 | ||
628fa4f8 | 3389 | |
276beea2 | 3390 | /* First do the subtraction as integers; |
3391 | then drop through to build the divide operator. | |
3392 | Do not do default conversions on the minus operator | |
3393 | in case restype is a short type. */ | |
628fa4f8 | 3394 | |
389dd41b | 3395 | op0 = build_binary_op (loc, |
6d5d708e | 3396 | MINUS_EXPR, convert (inttype, op0), |
3397 | convert (inttype, op1), 0); | |
276beea2 | 3398 | /* This generates an error if op1 is pointer to incomplete type. */ |
3399 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
389dd41b | 3400 | error_at (loc, "arithmetic on pointer to an incomplete type"); |
628fa4f8 | 3401 | |
276beea2 | 3402 | /* This generates an error if op0 is pointer to incomplete type. */ |
3403 | op1 = c_size_in_bytes (target_type); | |
628fa4f8 | 3404 | |
276beea2 | 3405 | /* Divide by the size, in easiest possible way. */ |
6d5d708e | 3406 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, |
3407 | op0, convert (inttype, op1)); | |
3408 | ||
3409 | /* Convert to final result type if necessary. */ | |
3410 | return convert (restype, result); | |
276beea2 | 3411 | } |
3412 | \f | |
3413 | /* Construct and perhaps optimize a tree representation | |
3414 | for a unary operation. CODE, a tree_code, specifies the operation | |
3415 | and XARG is the operand. | |
3416 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3417 | the default promotions (such as from short to int). | |
3418 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3419 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
b6889cb0 | 3420 | arrays to pointers in C99. |
3421 | ||
3422 | LOCATION is the location of the operator. */ | |
628fa4f8 | 3423 | |
276beea2 | 3424 | tree |
b6889cb0 | 3425 | build_unary_op (location_t location, |
3426 | enum tree_code code, tree xarg, int flag) | |
276beea2 | 3427 | { |
3428 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3429 | tree arg = xarg; | |
3430 | tree argtype = 0; | |
f831c98f | 3431 | enum tree_code typecode; |
276beea2 | 3432 | tree val; |
b6889cb0 | 3433 | tree ret = error_mark_node; |
c6418a4e | 3434 | tree eptype = NULL_TREE; |
276beea2 | 3435 | int noconvert = flag; |
7a979707 | 3436 | const char *invalid_op_diag; |
a75b1c71 | 3437 | bool int_operands; |
3438 | ||
3439 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
9a8bed72 | 3440 | if (int_operands) |
3441 | arg = remove_c_maybe_const_expr (arg); | |
628fa4f8 | 3442 | |
f831c98f | 3443 | if (code != ADDR_EXPR) |
3444 | arg = require_complete_type (arg); | |
3445 | ||
3446 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
276beea2 | 3447 | if (typecode == ERROR_MARK) |
3448 | return error_mark_node; | |
3449 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3450 | typecode = INTEGER_TYPE; | |
bd08a4e6 | 3451 | |
7a979707 | 3452 | if ((invalid_op_diag |
3453 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3454 | { | |
b6889cb0 | 3455 | error_at (location, invalid_op_diag); |
7a979707 | 3456 | return error_mark_node; |
3457 | } | |
3458 | ||
c6418a4e | 3459 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
3460 | { | |
3461 | eptype = TREE_TYPE (arg); | |
3462 | arg = TREE_OPERAND (arg, 0); | |
3463 | } | |
3464 | ||
276beea2 | 3465 | switch (code) |
3466 | { | |
3467 | case CONVERT_EXPR: | |
3468 | /* This is used for unary plus, because a CONVERT_EXPR | |
3469 | is enough to prevent anybody from looking inside for | |
3470 | associativity, but won't generate any code. */ | |
3471 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
9421ebb9 | 3472 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
71c777ee | 3473 | || typecode == VECTOR_TYPE)) |
628fa4f8 | 3474 | { |
b6889cb0 | 3475 | error_at (location, "wrong type argument to unary plus"); |
276beea2 | 3476 | return error_mark_node; |
628fa4f8 | 3477 | } |
276beea2 | 3478 | else if (!noconvert) |
3479 | arg = default_conversion (arg); | |
389dd41b | 3480 | arg = non_lvalue_loc (location, arg); |
628fa4f8 | 3481 | break; |
3482 | ||
276beea2 | 3483 | case NEGATE_EXPR: |
3484 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
9421ebb9 | 3485 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
276beea2 | 3486 | || typecode == VECTOR_TYPE)) |
3487 | { | |
b6889cb0 | 3488 | error_at (location, "wrong type argument to unary minus"); |
276beea2 | 3489 | return error_mark_node; |
3490 | } | |
3491 | else if (!noconvert) | |
3492 | arg = default_conversion (arg); | |
628fa4f8 | 3493 | break; |
3494 | ||
276beea2 | 3495 | case BIT_NOT_EXPR: |
0eb14c3d | 3496 | /* ~ works on integer types and non float vectors. */ |
3497 | if (typecode == INTEGER_TYPE | |
3498 | || (typecode == VECTOR_TYPE | |
3499 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
d2c0af2b | 3500 | { |
276beea2 | 3501 | if (!noconvert) |
3502 | arg = default_conversion (arg); | |
d2c0af2b | 3503 | } |
276beea2 | 3504 | else if (typecode == COMPLEX_TYPE) |
628fa4f8 | 3505 | { |
276beea2 | 3506 | code = CONJ_EXPR; |
29438999 | 3507 | pedwarn (location, OPT_Wpedantic, |
8864917d | 3508 | "ISO C does not support %<~%> for complex conjugation"); |
276beea2 | 3509 | if (!noconvert) |
3510 | arg = default_conversion (arg); | |
3511 | } | |
3512 | else | |
3513 | { | |
b6889cb0 | 3514 | error_at (location, "wrong type argument to bit-complement"); |
276beea2 | 3515 | return error_mark_node; |
628fa4f8 | 3516 | } |
3517 | break; | |
3518 | ||
276beea2 | 3519 | case ABS_EXPR: |
71d5a758 | 3520 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
628fa4f8 | 3521 | { |
b6889cb0 | 3522 | error_at (location, "wrong type argument to abs"); |
276beea2 | 3523 | return error_mark_node; |
628fa4f8 | 3524 | } |
276beea2 | 3525 | else if (!noconvert) |
3526 | arg = default_conversion (arg); | |
628fa4f8 | 3527 | break; |
3528 | ||
276beea2 | 3529 | case CONJ_EXPR: |
3530 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3531 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3532 | || typecode == COMPLEX_TYPE)) | |
628fa4f8 | 3533 | { |
b6889cb0 | 3534 | error_at (location, "wrong type argument to conjugation"); |
276beea2 | 3535 | return error_mark_node; |
628fa4f8 | 3536 | } |
276beea2 | 3537 | else if (!noconvert) |
3538 | arg = default_conversion (arg); | |
628fa4f8 | 3539 | break; |
3540 | ||
276beea2 | 3541 | case TRUTH_NOT_EXPR: |
9421ebb9 | 3542 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE |
276beea2 | 3543 | && typecode != REAL_TYPE && typecode != POINTER_TYPE |
f14c8207 | 3544 | && typecode != COMPLEX_TYPE) |
628fa4f8 | 3545 | { |
b6889cb0 | 3546 | error_at (location, |
3547 | "wrong type argument to unary exclamation mark"); | |
276beea2 | 3548 | return error_mark_node; |
628fa4f8 | 3549 | } |
b6889cb0 | 3550 | arg = c_objc_common_truthvalue_conversion (location, arg); |
389dd41b | 3551 | ret = invert_truthvalue_loc (location, arg); |
43158006 | 3552 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ |
3553 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3554 | location = EXPR_LOCATION (ret); | |
b6889cb0 | 3555 | goto return_build_unary_op; |
276beea2 | 3556 | |
276beea2 | 3557 | case REALPART_EXPR: |
276beea2 | 3558 | case IMAGPART_EXPR: |
0b5fc5d6 | 3559 | ret = build_real_imag_expr (location, code, arg); |
3560 | if (ret == error_mark_node) | |
3561 | return error_mark_node; | |
c6418a4e | 3562 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3563 | eptype = TREE_TYPE (eptype); | |
b6889cb0 | 3564 | goto return_build_unary_op; |
276beea2 | 3565 | |
3566 | case PREINCREMENT_EXPR: | |
3567 | case POSTINCREMENT_EXPR: | |
3568 | case PREDECREMENT_EXPR: | |
3569 | case POSTDECREMENT_EXPR: | |
276beea2 | 3570 | |
a75b1c71 | 3571 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) |
3572 | { | |
3573 | tree inner = build_unary_op (location, code, | |
3574 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3575 | if (inner == error_mark_node) | |
3576 | return error_mark_node; | |
3577 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3578 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3579 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3580 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3581 | goto return_build_unary_op; | |
3582 | } | |
3583 | ||
e4a7640a | 3584 | /* Complain about anything that is not a true lvalue. In |
3585 | Objective-C, skip this check for property_refs. */ | |
6cf89e04 | 3586 | if (!objc_is_property_ref (arg) |
fdd84b77 | 3587 | && !lvalue_or_else (location, |
3588 | arg, ((code == PREINCREMENT_EXPR | |
e4a7640a | 3589 | || code == POSTINCREMENT_EXPR) |
3590 | ? lv_increment | |
3591 | : lv_decrement))) | |
a75b1c71 | 3592 | return error_mark_node; |
3593 | ||
9f1b7d17 | 3594 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) |
3595 | { | |
3596 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3597 | warning_at (location, OPT_Wc___compat, | |
3598 | "increment of enumeration value is invalid in C++"); | |
3599 | else | |
3600 | warning_at (location, OPT_Wc___compat, | |
3601 | "decrement of enumeration value is invalid in C++"); | |
3602 | } | |
3603 | ||
a75b1c71 | 3604 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ |
3605 | arg = c_fully_fold (arg, false, NULL); | |
3606 | ||
276beea2 | 3607 | /* Increment or decrement the real part of the value, |
3608 | and don't change the imaginary part. */ | |
3609 | if (typecode == COMPLEX_TYPE) | |
628fa4f8 | 3610 | { |
276beea2 | 3611 | tree real, imag; |
3612 | ||
29438999 | 3613 | pedwarn (location, OPT_Wpedantic, |
21ca8540 | 3614 | "ISO C does not support %<++%> and %<--%> on complex types"); |
276beea2 | 3615 | |
3616 | arg = stabilize_reference (arg); | |
b6889cb0 | 3617 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); |
3618 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
3619 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
3c550432 | 3620 | if (real == error_mark_node || imag == error_mark_node) |
3621 | return error_mark_node; | |
b6889cb0 | 3622 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
3623 | real, imag); | |
3624 | goto return_build_unary_op; | |
628fa4f8 | 3625 | } |
276beea2 | 3626 | |
3627 | /* Report invalid types. */ | |
3628 | ||
9421ebb9 | 3629 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE |
276beea2 | 3630 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE) |
628fa4f8 | 3631 | { |
276beea2 | 3632 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
b6889cb0 | 3633 | error_at (location, "wrong type argument to increment"); |
a0c938f0 | 3634 | else |
b6889cb0 | 3635 | error_at (location, "wrong type argument to decrement"); |
276beea2 | 3636 | |
3637 | return error_mark_node; | |
628fa4f8 | 3638 | } |
628fa4f8 | 3639 | |
276beea2 | 3640 | { |
3641 | tree inc; | |
628fa4f8 | 3642 | |
276beea2 | 3643 | argtype = TREE_TYPE (arg); |
3644 | ||
3645 | /* Compute the increment. */ | |
3646 | ||
3647 | if (typecode == POINTER_TYPE) | |
3648 | { | |
3649 | /* If pointer target is an undefined struct, | |
3650 | we just cannot know how to do the arithmetic. */ | |
20a44a69 | 3651 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) |
276beea2 | 3652 | { |
3653 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
b6889cb0 | 3654 | error_at (location, |
3655 | "increment of pointer to unknown structure"); | |
276beea2 | 3656 | else |
b6889cb0 | 3657 | error_at (location, |
3658 | "decrement of pointer to unknown structure"); | |
276beea2 | 3659 | } |
20a44a69 | 3660 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE |
3661 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
a0c938f0 | 3662 | { |
276beea2 | 3663 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
29438999 | 3664 | pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 3665 | "wrong type argument to increment"); |
276beea2 | 3666 | else |
29438999 | 3667 | pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 3668 | "wrong type argument to decrement"); |
276beea2 | 3669 | } |
3670 | ||
20a44a69 | 3671 | inc = c_size_in_bytes (TREE_TYPE (argtype)); |
a0553bff | 3672 | inc = convert_to_ptrofftype_loc (location, inc); |
276beea2 | 3673 | } |
20a44a69 | 3674 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) |
9421ebb9 | 3675 | { |
3676 | /* For signed fract types, we invert ++ to -- or | |
3677 | -- to ++, and change inc from 1 to -1, because | |
3678 | it is not possible to represent 1 in signed fract constants. | |
3679 | For unsigned fract types, the result always overflows and | |
3680 | we get an undefined (original) or the maximum value. */ | |
3681 | if (code == PREINCREMENT_EXPR) | |
3682 | code = PREDECREMENT_EXPR; | |
3683 | else if (code == PREDECREMENT_EXPR) | |
3684 | code = PREINCREMENT_EXPR; | |
3685 | else if (code == POSTINCREMENT_EXPR) | |
3686 | code = POSTDECREMENT_EXPR; | |
3687 | else /* code == POSTDECREMENT_EXPR */ | |
3688 | code = POSTINCREMENT_EXPR; | |
3689 | ||
3690 | inc = integer_minus_one_node; | |
3691 | inc = convert (argtype, inc); | |
3692 | } | |
276beea2 | 3693 | else |
0de36bdb | 3694 | { |
3695 | inc = integer_one_node; | |
3696 | inc = convert (argtype, inc); | |
3697 | } | |
276beea2 | 3698 | |
e4a7640a | 3699 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
3700 | need to ask Objective-C to build the increment or decrement | |
3701 | expression for it. */ | |
3702 | if (objc_is_property_ref (arg)) | |
6cf89e04 | 3703 | return objc_build_incr_expr_for_property_ref (location, code, |
e4a7640a | 3704 | arg, inc); |
3705 | ||
276beea2 | 3706 | /* Report a read-only lvalue. */ |
d4e60318 | 3707 | if (TYPE_READONLY (argtype)) |
1e8e9920 | 3708 | { |
3709 | readonly_error (arg, | |
3710 | ((code == PREINCREMENT_EXPR | |
3711 | || code == POSTINCREMENT_EXPR) | |
3712 | ? lv_increment : lv_decrement)); | |
3713 | return error_mark_node; | |
3714 | } | |
d4e60318 | 3715 | else if (TREE_READONLY (arg)) |
3716 | readonly_warning (arg, | |
3717 | ((code == PREINCREMENT_EXPR | |
3718 | || code == POSTINCREMENT_EXPR) | |
3719 | ? lv_increment : lv_decrement)); | |
276beea2 | 3720 | |
3721 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) | |
3722 | val = boolean_increment (code, arg); | |
3723 | else | |
14ae0310 | 3724 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
276beea2 | 3725 | TREE_SIDE_EFFECTS (val) = 1; |
276beea2 | 3726 | if (TREE_CODE (val) != code) |
4ee9c684 | 3727 | TREE_NO_WARNING (val) = 1; |
b6889cb0 | 3728 | ret = val; |
3729 | goto return_build_unary_op; | |
276beea2 | 3730 | } |
3731 | ||
3732 | case ADDR_EXPR: | |
3733 | /* Note that this operation never does default_conversion. */ | |
3734 | ||
1b157e43 | 3735 | /* The operand of unary '&' must be an lvalue (which excludes |
3736 | expressions of type void), or, in C99, the result of a [] or | |
3737 | unary '*' operator. */ | |
3738 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
3739 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
3740 | && (TREE_CODE (arg) != INDIRECT_REF | |
3741 | || !flag_isoc99)) | |
3742 | pedwarn (location, 0, "taking address of expression of type %<void%>"); | |
3743 | ||
276beea2 | 3744 | /* Let &* cancel out to simplify resulting code. */ |
3745 | if (TREE_CODE (arg) == INDIRECT_REF) | |
628fa4f8 | 3746 | { |
276beea2 | 3747 | /* Don't let this be an lvalue. */ |
3748 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
389dd41b | 3749 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); |
b6889cb0 | 3750 | ret = TREE_OPERAND (arg, 0); |
3751 | goto return_build_unary_op; | |
628fa4f8 | 3752 | } |
a4110d9a | 3753 | |
638a142a | 3754 | /* For &x[y], return x+y */ |
276beea2 | 3755 | if (TREE_CODE (arg) == ARRAY_REF) |
a4110d9a | 3756 | { |
e78cf9a7 | 3757 | tree op0 = TREE_OPERAND (arg, 0); |
3758 | if (!c_mark_addressable (op0)) | |
276beea2 | 3759 | return error_mark_node; |
a4110d9a | 3760 | } |
a4110d9a | 3761 | |
276beea2 | 3762 | /* Anything not already handled and not a true memory reference |
3763 | or a non-lvalue array is an error. */ | |
3764 | else if (typecode != FUNCTION_TYPE && !flag | |
fdd84b77 | 3765 | && !lvalue_or_else (location, arg, lv_addressof)) |
276beea2 | 3766 | return error_mark_node; |
6b854be5 | 3767 | |
a75b1c71 | 3768 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify |
3769 | folding later. */ | |
3770 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
3771 | { | |
3772 | tree inner = build_unary_op (location, code, | |
3773 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3774 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3775 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3776 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3777 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
3778 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
3779 | goto return_build_unary_op; | |
3780 | } | |
3781 | ||
276beea2 | 3782 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
3783 | argtype = TREE_TYPE (arg); | |
628fa4f8 | 3784 | |
276beea2 | 3785 | /* If the lvalue is const or volatile, merge that into the type |
8ae93b58 | 3786 | to which the address will point. This is only needed |
51e25433 | 3787 | for function types. */ |
ce45a448 | 3788 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) |
8ae93b58 | 3789 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) |
3790 | && TREE_CODE (argtype) == FUNCTION_TYPE) | |
51e25433 | 3791 | { |
3792 | int orig_quals = TYPE_QUALS (strip_array_types (argtype)); | |
3793 | int quals = orig_quals; | |
3794 | ||
3795 | if (TREE_READONLY (arg)) | |
3796 | quals |= TYPE_QUAL_CONST; | |
3797 | if (TREE_THIS_VOLATILE (arg)) | |
3798 | quals |= TYPE_QUAL_VOLATILE; | |
3799 | ||
51e25433 | 3800 | argtype = c_build_qualified_type (argtype, quals); |
3801 | } | |
628fa4f8 | 3802 | |
276beea2 | 3803 | if (!c_mark_addressable (arg)) |
3804 | return error_mark_node; | |
628fa4f8 | 3805 | |
43d08288 | 3806 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF |
3807 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
628fa4f8 | 3808 | |
7da1265c | 3809 | argtype = build_pointer_type (argtype); |
3ce29533 | 3810 | |
3811 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
3812 | when we have proper support for integer constant expressions. */ | |
3813 | val = get_base_address (arg); | |
3814 | if (val && TREE_CODE (val) == INDIRECT_REF | |
120c6697 | 3815 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
3816 | { | |
7549df0d | 3817 | ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg)); |
b6889cb0 | 3818 | goto return_build_unary_op; |
120c6697 | 3819 | } |
3ce29533 | 3820 | |
7da1265c | 3821 | val = build1 (ADDR_EXPR, argtype, arg); |
628fa4f8 | 3822 | |
b6889cb0 | 3823 | ret = val; |
3824 | goto return_build_unary_op; | |
628fa4f8 | 3825 | |
276beea2 | 3826 | default: |
c44afe23 | 3827 | gcc_unreachable (); |
276beea2 | 3828 | } |
628fa4f8 | 3829 | |
276beea2 | 3830 | if (argtype == 0) |
3831 | argtype = TREE_TYPE (arg); | |
a75b1c71 | 3832 | if (TREE_CODE (arg) == INTEGER_CST) |
3833 | ret = (require_constant_value | |
389dd41b | 3834 | ? fold_build1_initializer_loc (location, code, argtype, arg) |
3835 | : fold_build1_loc (location, code, argtype, arg)); | |
a75b1c71 | 3836 | else |
3837 | ret = build1 (code, argtype, arg); | |
b6889cb0 | 3838 | return_build_unary_op: |
3839 | gcc_assert (ret != error_mark_node); | |
a75b1c71 | 3840 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) |
3841 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
3842 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
3843 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
3844 | ret = note_integer_operands (ret); | |
c6418a4e | 3845 | if (eptype) |
3846 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
b6889cb0 | 3847 | protected_set_expr_location (ret, location); |
3848 | return ret; | |
276beea2 | 3849 | } |
628fa4f8 | 3850 | |
276beea2 | 3851 | /* Return nonzero if REF is an lvalue valid for this language. |
3852 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
7746224a | 3853 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
628fa4f8 | 3854 | |
79973b57 | 3855 | bool |
9f627b1a | 3856 | lvalue_p (const_tree ref) |
276beea2 | 3857 | { |
9f627b1a | 3858 | const enum tree_code code = TREE_CODE (ref); |
628fa4f8 | 3859 | |
276beea2 | 3860 | switch (code) |
3861 | { | |
3862 | case REALPART_EXPR: | |
3863 | case IMAGPART_EXPR: | |
3864 | case COMPONENT_REF: | |
3865 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
628fa4f8 | 3866 | |
a75b1c71 | 3867 | case C_MAYBE_CONST_EXPR: |
3868 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
3869 | ||
276beea2 | 3870 | case COMPOUND_LITERAL_EXPR: |
3871 | case STRING_CST: | |
3872 | return 1; | |
628fa4f8 | 3873 | |
276beea2 | 3874 | case INDIRECT_REF: |
3875 | case ARRAY_REF: | |
3876 | case VAR_DECL: | |
3877 | case PARM_DECL: | |
3878 | case RESULT_DECL: | |
3879 | case ERROR_MARK: | |
3880 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
3881 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
5d844ba2 | 3882 | |
276beea2 | 3883 | case BIND_EXPR: |
276beea2 | 3884 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
5d844ba2 | 3885 | |
276beea2 | 3886 | default: |
3887 | return 0; | |
3888 | } | |
3889 | } | |
628fa4f8 | 3890 | \f |
d4e60318 | 3891 | /* Give a warning for storing in something that is read-only in GCC |
3892 | terms but not const in ISO C terms. */ | |
3893 | ||
3894 | static void | |
3895 | readonly_warning (tree arg, enum lvalue_use use) | |
3896 | { | |
3897 | switch (use) | |
3898 | { | |
3899 | case lv_assign: | |
3900 | warning (0, "assignment of read-only location %qE", arg); | |
3901 | break; | |
3902 | case lv_increment: | |
3903 | warning (0, "increment of read-only location %qE", arg); | |
3904 | break; | |
3905 | case lv_decrement: | |
3906 | warning (0, "decrement of read-only location %qE", arg); | |
3907 | break; | |
3908 | default: | |
3909 | gcc_unreachable (); | |
3910 | } | |
3911 | return; | |
3912 | } | |
3913 | ||
e35976b1 | 3914 | |
3915 | /* Return nonzero if REF is an lvalue valid for this language; | |
3916 | otherwise, print an error message and return zero. USE says | |
fdd84b77 | 3917 | how the lvalue is being used and so selects the error message. |
3918 | LOCATION is the location at which any error should be reported. */ | |
e35976b1 | 3919 | |
3920 | static int | |
fdd84b77 | 3921 | lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) |
e35976b1 | 3922 | { |
3923 | int win = lvalue_p (ref); | |
3924 | ||
3925 | if (!win) | |
fdd84b77 | 3926 | lvalue_error (loc, use); |
e35976b1 | 3927 | |
3928 | return win; | |
3929 | } | |
276beea2 | 3930 | \f |
3931 | /* Mark EXP saying that we need to be able to take the | |
3932 | address of it; it should not be allocated in a register. | |
3933 | Returns true if successful. */ | |
e4d4d9ae | 3934 | |
276beea2 | 3935 | bool |
3936 | c_mark_addressable (tree exp) | |
628fa4f8 | 3937 | { |
276beea2 | 3938 | tree x = exp; |
277f8686 | 3939 | |
276beea2 | 3940 | while (1) |
3941 | switch (TREE_CODE (x)) | |
3942 | { | |
3943 | case COMPONENT_REF: | |
3944 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
3945 | { | |
e3ab5fb6 | 3946 | error |
3947 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
276beea2 | 3948 | return false; |
3949 | } | |
277f8686 | 3950 | |
276beea2 | 3951 | /* ... fall through ... */ |
277f8686 | 3952 | |
276beea2 | 3953 | case ADDR_EXPR: |
3954 | case ARRAY_REF: | |
3955 | case REALPART_EXPR: | |
3956 | case IMAGPART_EXPR: | |
3957 | x = TREE_OPERAND (x, 0); | |
3958 | break; | |
277f8686 | 3959 | |
276beea2 | 3960 | case COMPOUND_LITERAL_EXPR: |
3961 | case CONSTRUCTOR: | |
3962 | TREE_ADDRESSABLE (x) = 1; | |
3963 | return true; | |
277f8686 | 3964 | |
276beea2 | 3965 | case VAR_DECL: |
3966 | case CONST_DECL: | |
3967 | case PARM_DECL: | |
3968 | case RESULT_DECL: | |
7746224a | 3969 | if (C_DECL_REGISTER (x) |
276beea2 | 3970 | && DECL_NONLOCAL (x)) |
3971 | { | |
7d282ac0 | 3972 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
276beea2 | 3973 | { |
e3ab5fb6 | 3974 | error |
3975 | ("global register variable %qD used in nested function", x); | |
276beea2 | 3976 | return false; |
3977 | } | |
21ca8540 | 3978 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); |
276beea2 | 3979 | } |
7746224a | 3980 | else if (C_DECL_REGISTER (x)) |
276beea2 | 3981 | { |
7d282ac0 | 3982 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
e3ab5fb6 | 3983 | error ("address of global register variable %qD requested", x); |
3984 | else | |
3985 | error ("address of register variable %qD requested", x); | |
3986 | return false; | |
276beea2 | 3987 | } |
628fa4f8 | 3988 | |
276beea2 | 3989 | /* drops in */ |
3990 | case FUNCTION_DECL: | |
3991 | TREE_ADDRESSABLE (x) = 1; | |
3992 | /* drops out */ | |
3993 | default: | |
3994 | return true; | |
3995 | } | |
3996 | } | |
3997 | \f | |
8f45ab4d | 3998 | /* Convert EXPR to TYPE, warning about conversion problems with |
3999 | constants. SEMANTIC_TYPE is the type this conversion would use | |
4000 | without excess precision. If SEMANTIC_TYPE is NULL, this function | |
4001 | is equivalent to convert_and_check. This function is a wrapper that | |
4002 | handles conversions that may be different than | |
4003 | the usual ones because of excess precision. */ | |
4004 | ||
4005 | static tree | |
4006 | ep_convert_and_check (tree type, tree expr, tree semantic_type) | |
4007 | { | |
4008 | if (TREE_TYPE (expr) == type) | |
4009 | return expr; | |
4010 | ||
4011 | if (!semantic_type) | |
4012 | return convert_and_check (type, expr); | |
4013 | ||
4014 | if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
4015 | && TREE_TYPE (expr) != semantic_type) | |
4016 | { | |
4017 | /* For integers, we need to check the real conversion, not | |
4018 | the conversion to the excess precision type. */ | |
4019 | expr = convert_and_check (semantic_type, expr); | |
4020 | } | |
4021 | /* Result type is the excess precision type, which should be | |
4022 | large enough, so do not check. */ | |
4023 | return convert (type, expr); | |
4024 | } | |
4025 | ||
a75b1c71 | 4026 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If |
4027 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
4028 | if folded to an integer constant then the unselected half may | |
4029 | contain arbitrary operations not normally permitted in constant | |
e60a6f7b | 4030 | expressions. Set the location of the expression to LOC. */ |
628fa4f8 | 4031 | |
4032 | tree | |
d5b637fa | 4033 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, |
6163a125 | 4034 | tree op1, tree op1_original_type, tree op2, |
4035 | tree op2_original_type) | |
628fa4f8 | 4036 | { |
276beea2 | 4037 | tree type1; |
4038 | tree type2; | |
4039 | enum tree_code code1; | |
4040 | enum tree_code code2; | |
4041 | tree result_type = NULL; | |
8f45ab4d | 4042 | tree semantic_result_type = NULL; |
276beea2 | 4043 | tree orig_op1 = op1, orig_op2 = op2; |
a75b1c71 | 4044 | bool int_const, op1_int_operands, op2_int_operands, int_operands; |
9a8bed72 | 4045 | bool ifexp_int_operands; |
a75b1c71 | 4046 | tree ret; |
628fa4f8 | 4047 | |
9a8bed72 | 4048 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); |
4049 | if (op1_int_operands) | |
4050 | op1 = remove_c_maybe_const_expr (op1); | |
4051 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
4052 | if (op2_int_operands) | |
4053 | op2 = remove_c_maybe_const_expr (op2); | |
4054 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
4055 | if (ifexp_int_operands) | |
4056 | ifexp = remove_c_maybe_const_expr (ifexp); | |
4057 | ||
276beea2 | 4058 | /* Promote both alternatives. */ |
4059 | ||
4060 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
4061 | op1 = default_conversion (op1); | |
4062 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
4063 | op2 = default_conversion (op2); | |
4064 | ||
4065 | if (TREE_CODE (ifexp) == ERROR_MARK | |
4066 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
4067 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
628fa4f8 | 4068 | return error_mark_node; |
628fa4f8 | 4069 | |
276beea2 | 4070 | type1 = TREE_TYPE (op1); |
4071 | code1 = TREE_CODE (type1); | |
4072 | type2 = TREE_TYPE (op2); | |
4073 | code2 = TREE_CODE (type2); | |
4074 | ||
9b73f131 | 4075 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
4076 | In C99 they will be pointers by now. */ | |
4077 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
4078 | { | |
d5b637fa | 4079 | error_at (colon_loc, "non-lvalue array in conditional expression"); |
9b73f131 | 4080 | return error_mark_node; |
4081 | } | |
4082 | ||
c6418a4e | 4083 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR |
4084 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4085 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4086 | || code1 == COMPLEX_TYPE) | |
4087 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4088 | || code2 == COMPLEX_TYPE)) | |
4089 | { | |
8f45ab4d | 4090 | semantic_result_type = c_common_type (type1, type2); |
c6418a4e | 4091 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) |
4092 | { | |
4093 | op1 = TREE_OPERAND (op1, 0); | |
4094 | type1 = TREE_TYPE (op1); | |
4095 | gcc_assert (TREE_CODE (type1) == code1); | |
4096 | } | |
4097 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4098 | { | |
4099 | op2 = TREE_OPERAND (op2, 0); | |
4100 | type2 = TREE_TYPE (op2); | |
4101 | gcc_assert (TREE_CODE (type2) == code2); | |
4102 | } | |
4103 | } | |
4104 | ||
6163a125 | 4105 | if (warn_cxx_compat) |
4106 | { | |
4107 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
4108 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
4109 | ||
4110 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
4111 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
4112 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
4113 | warning_at (colon_loc, OPT_Wc___compat, | |
4114 | ("different enum types in conditional is " | |
4115 | "invalid in C++: %qT vs %qT"), | |
4116 | t1, t2); | |
4117 | } | |
4118 | ||
276beea2 | 4119 | /* Quickly detect the usual case where op1 and op2 have the same type |
4120 | after promotion. */ | |
4121 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
628fa4f8 | 4122 | { |
276beea2 | 4123 | if (type1 == type2) |
4124 | result_type = type1; | |
4125 | else | |
4126 | result_type = TYPE_MAIN_VARIANT (type1); | |
4127 | } | |
4128 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
a0c938f0 | 4129 | || code1 == COMPLEX_TYPE) |
4130 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4131 | || code2 == COMPLEX_TYPE)) | |
276beea2 | 4132 | { |
79d37242 | 4133 | result_type = c_common_type (type1, type2); |
c920faa3 | 4134 | do_warn_double_promotion (result_type, type1, type2, |
4135 | "implicit conversion from %qT to %qT to " | |
4136 | "match other result of conditional", | |
4137 | colon_loc); | |
628fa4f8 | 4138 | |
276beea2 | 4139 | /* If -Wsign-compare, warn here if type1 and type2 have |
4140 | different signedness. We'll promote the signed to unsigned | |
4141 | and later code won't know it used to be different. | |
4142 | Do this check on the original types, so that explicit casts | |
4143 | will be considered, but default promotions won't. */ | |
48d94ede | 4144 | if (c_inhibit_evaluation_warnings == 0) |
be2828ce | 4145 | { |
78a8ed03 | 4146 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
4147 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
628fa4f8 | 4148 | |
276beea2 | 4149 | if (unsigned_op1 ^ unsigned_op2) |
4150 | { | |
add6ee5e | 4151 | bool ovf; |
4152 | ||
276beea2 | 4153 | /* Do not warn if the result type is signed, since the |
4154 | signed type will only be chosen if it can represent | |
4155 | all the values of the unsigned type. */ | |
84166705 | 4156 | if (!TYPE_UNSIGNED (result_type)) |
276beea2 | 4157 | /* OK */; |
276beea2 | 4158 | else |
a75b1c71 | 4159 | { |
4160 | bool op1_maybe_const = true; | |
4161 | bool op2_maybe_const = true; | |
4162 | ||
4163 | /* Do not warn if the signed quantity is an | |
4164 | unsuffixed integer literal (or some static | |
4165 | constant expression involving such literals) and | |
4166 | it is non-negative. This warning requires the | |
4167 | operands to be folded for best results, so do | |
4168 | that folding in this case even without | |
4169 | warn_sign_compare to avoid warning options | |
4170 | possibly affecting code generation. */ | |
672d914b | 4171 | c_inhibit_evaluation_warnings |
4172 | += (ifexp == truthvalue_false_node); | |
a75b1c71 | 4173 | op1 = c_fully_fold (op1, require_constant_value, |
4174 | &op1_maybe_const); | |
672d914b | 4175 | c_inhibit_evaluation_warnings |
4176 | -= (ifexp == truthvalue_false_node); | |
4177 | ||
4178 | c_inhibit_evaluation_warnings | |
4179 | += (ifexp == truthvalue_true_node); | |
a75b1c71 | 4180 | op2 = c_fully_fold (op2, require_constant_value, |
4181 | &op2_maybe_const); | |
672d914b | 4182 | c_inhibit_evaluation_warnings |
4183 | -= (ifexp == truthvalue_true_node); | |
a75b1c71 | 4184 | |
4185 | if (warn_sign_compare) | |
4186 | { | |
4187 | if ((unsigned_op2 | |
4188 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4189 | || (unsigned_op1 | |
4190 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4191 | /* OK */; | |
4192 | else | |
d5b637fa | 4193 | warning_at (colon_loc, OPT_Wsign_compare, |
4194 | ("signed and unsigned type in " | |
4195 | "conditional expression")); | |
a75b1c71 | 4196 | } |
4197 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
f59e3889 | 4198 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
a75b1c71 | 4199 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) |
f59e3889 | 4200 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); |
a75b1c71 | 4201 | } |
276beea2 | 4202 | } |
4203 | } | |
4204 | } | |
4205 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4206 | { | |
8864917d | 4207 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) |
29438999 | 4208 | pedwarn (colon_loc, OPT_Wpedantic, |
8864917d | 4209 | "ISO C forbids conditional expr with only one void side"); |
276beea2 | 4210 | result_type = void_type_node; |
4211 | } | |
4212 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4213 | { | |
6d5d708e | 4214 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); |
4215 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4216 | addr_space_t as_common; | |
4217 | ||
d5b637fa | 4218 | if (comp_target_types (colon_loc, type1, type2)) |
1ea7fa60 | 4219 | result_type = common_pointer_type (type1, type2); |
79f925ed | 4220 | else if (null_pointer_constant_p (orig_op1)) |
6d5d708e | 4221 | result_type = type2; |
79f925ed | 4222 | else if (null_pointer_constant_p (orig_op2)) |
6d5d708e | 4223 | result_type = type1; |
4224 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4225 | { | |
4226 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4227 | "used in conditional expression"); | |
4228 | return error_mark_node; | |
4229 | } | |
276beea2 | 4230 | else if (VOID_TYPE_P (TREE_TYPE (type1))) |
20407c42 | 4231 | { |
8864917d | 4232 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
29438999 | 4233 | pedwarn (colon_loc, OPT_Wpedantic, |
21ca8540 | 4234 | "ISO C forbids conditional expr between " |
b0b1af64 | 4235 | "%<void *%> and function pointer"); |
276beea2 | 4236 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), |
4237 | TREE_TYPE (type2))); | |
20407c42 | 4238 | } |
276beea2 | 4239 | else if (VOID_TYPE_P (TREE_TYPE (type2))) |
a5eb6189 | 4240 | { |
8864917d | 4241 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
29438999 | 4242 | pedwarn (colon_loc, OPT_Wpedantic, |
21ca8540 | 4243 | "ISO C forbids conditional expr between " |
b0b1af64 | 4244 | "%<void *%> and function pointer"); |
276beea2 | 4245 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), |
4246 | TREE_TYPE (type1))); | |
a5eb6189 | 4247 | } |
4abfc532 | 4248 | /* Objective-C pointer comparisons are a bit more lenient. */ |
4249 | else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) | |
4250 | result_type = objc_common_type (type1, type2); | |
20407c42 | 4251 | else |
be2828ce | 4252 | { |
6d5d708e | 4253 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
4254 | ||
4abfc532 | 4255 | pedwarn (colon_loc, 0, |
4256 | "pointer type mismatch in conditional expression"); | |
6d5d708e | 4257 | result_type = build_pointer_type |
4258 | (build_qualified_type (void_type_node, qual)); | |
be2828ce | 4259 | } |
276beea2 | 4260 | } |
4261 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4262 | { | |
79f925ed | 4263 | if (!null_pointer_constant_p (orig_op2)) |
d5b637fa | 4264 | pedwarn (colon_loc, 0, |
21ca8540 | 4265 | "pointer/integer type mismatch in conditional expression"); |
276beea2 | 4266 | else |
be2828ce | 4267 | { |
276beea2 | 4268 | op2 = null_pointer_node; |
be2828ce | 4269 | } |
276beea2 | 4270 | result_type = type1; |
4271 | } | |
4272 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4273 | { | |
79f925ed | 4274 | if (!null_pointer_constant_p (orig_op1)) |
d5b637fa | 4275 | pedwarn (colon_loc, 0, |
21ca8540 | 4276 | "pointer/integer type mismatch in conditional expression"); |
276beea2 | 4277 | else |
be2828ce | 4278 | { |
276beea2 | 4279 | op1 = null_pointer_node; |
be2828ce | 4280 | } |
276beea2 | 4281 | result_type = type2; |
4282 | } | |
a5eb6189 | 4283 | |
276beea2 | 4284 | if (!result_type) |
4285 | { | |
4286 | if (flag_cond_mismatch) | |
4287 | result_type = void_type_node; | |
4288 | else | |
628fa4f8 | 4289 | { |
e60a6f7b | 4290 | error_at (colon_loc, "type mismatch in conditional expression"); |
be2828ce | 4291 | return error_mark_node; |
628fa4f8 | 4292 | } |
276beea2 | 4293 | } |
628fa4f8 | 4294 | |
276beea2 | 4295 | /* Merge const and volatile flags of the incoming types. */ |
4296 | result_type | |
4297 | = build_type_variant (result_type, | |
a6b4313a | 4298 | TYPE_READONLY (type1) || TYPE_READONLY (type2), |
4299 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
6b854be5 | 4300 | |
8f45ab4d | 4301 | op1 = ep_convert_and_check (result_type, op1, semantic_result_type); |
4302 | op2 = ep_convert_and_check (result_type, op2, semantic_result_type); | |
6b854be5 | 4303 | |
a75b1c71 | 4304 | if (ifexp_bcp && ifexp == truthvalue_true_node) |
4305 | { | |
4306 | op2_int_operands = true; | |
4307 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4308 | } | |
4309 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4310 | { | |
4311 | op1_int_operands = true; | |
4312 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4313 | } | |
9a8bed72 | 4314 | int_const = int_operands = (ifexp_int_operands |
a75b1c71 | 4315 | && op1_int_operands |
4316 | && op2_int_operands); | |
4317 | if (int_operands) | |
4318 | { | |
4319 | int_const = ((ifexp == truthvalue_true_node | |
4320 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4321 | && !TREE_OVERFLOW (orig_op1)) | |
4322 | || (ifexp == truthvalue_false_node | |
4323 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4324 | && !TREE_OVERFLOW (orig_op2))); | |
4325 | } | |
4326 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) | |
389dd41b | 4327 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); |
a75b1c71 | 4328 | else |
4329 | { | |
7c2df6db | 4330 | if (int_operands) |
4331 | { | |
4332 | op1 = remove_c_maybe_const_expr (op1); | |
4333 | op2 = remove_c_maybe_const_expr (op2); | |
4334 | } | |
a75b1c71 | 4335 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); |
4336 | if (int_operands) | |
4337 | ret = note_integer_operands (ret); | |
4338 | } | |
8f45ab4d | 4339 | if (semantic_result_type) |
4340 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
a75b1c71 | 4341 | |
e60a6f7b | 4342 | protected_set_expr_location (ret, colon_loc); |
a75b1c71 | 4343 | return ret; |
276beea2 | 4344 | } |
4345 | \f | |
066f26bf | 4346 | /* Return a compound expression that performs two expressions and |
e60a6f7b | 4347 | returns the value of the second of them. |
4348 | ||
4349 | LOC is the location of the COMPOUND_EXPR. */ | |
628fa4f8 | 4350 | |
276beea2 | 4351 | tree |
e60a6f7b | 4352 | build_compound_expr (location_t loc, tree expr1, tree expr2) |
276beea2 | 4353 | { |
9a8bed72 | 4354 | bool expr1_int_operands, expr2_int_operands; |
c6418a4e | 4355 | tree eptype = NULL_TREE; |
a75b1c71 | 4356 | tree ret; |
4357 | ||
9a8bed72 | 4358 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); |
4359 | if (expr1_int_operands) | |
4360 | expr1 = remove_c_maybe_const_expr (expr1); | |
4361 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4362 | if (expr2_int_operands) | |
4363 | expr2 = remove_c_maybe_const_expr (expr2); | |
4364 | ||
c6418a4e | 4365 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) |
4366 | expr1 = TREE_OPERAND (expr1, 0); | |
4367 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4368 | { | |
4369 | eptype = TREE_TYPE (expr2); | |
4370 | expr2 = TREE_OPERAND (expr2, 0); | |
4371 | } | |
4372 | ||
84166705 | 4373 | if (!TREE_SIDE_EFFECTS (expr1)) |
276beea2 | 4374 | { |
4375 | /* The left-hand operand of a comma expression is like an expression | |
2006d7dc | 4376 | statement: with -Wunused, we should warn if it doesn't have |
276beea2 | 4377 | any side-effects, unless it was explicitly cast to (void). */ |
aaa8f689 | 4378 | if (warn_unused_value) |
778e3b56 | 4379 | { |
aaa8f689 | 4380 | if (VOID_TYPE_P (TREE_TYPE (expr1)) |
72dd6141 | 4381 | && CONVERT_EXPR_P (expr1)) |
778e3b56 | 4382 | ; /* (void) a, b */ |
aaa8f689 | 4383 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) |
4384 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
72dd6141 | 4385 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) |
778e3b56 | 4386 | ; /* (void) a, (void) b, c */ |
4387 | else | |
48e1416a | 4388 | warning_at (loc, OPT_Wunused_value, |
e60a6f7b | 4389 | "left-hand operand of comma expression has no effect"); |
778e3b56 | 4390 | } |
276beea2 | 4391 | } |
628fa4f8 | 4392 | |
276beea2 | 4393 | /* With -Wunused, we should also warn if the left-hand operand does have |
4394 | side-effects, but computes a value which is not used. For example, in | |
4395 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4396 | so we should issue a warning. */ | |
4397 | else if (warn_unused_value) | |
e60a6f7b | 4398 | warn_if_unused_value (expr1, loc); |
628fa4f8 | 4399 | |
47adf96b | 4400 | if (expr2 == error_mark_node) |
4401 | return error_mark_node; | |
4402 | ||
a75b1c71 | 4403 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
4404 | ||
4405 | if (flag_isoc99 | |
9a8bed72 | 4406 | && expr1_int_operands |
4407 | && expr2_int_operands) | |
a75b1c71 | 4408 | ret = note_integer_operands (ret); |
4409 | ||
c6418a4e | 4410 | if (eptype) |
4411 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4412 | ||
e60a6f7b | 4413 | protected_set_expr_location (ret, loc); |
a75b1c71 | 4414 | return ret; |
276beea2 | 4415 | } |
628fa4f8 | 4416 | |
402ba866 | 4417 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to |
4418 | which we are casting. OTYPE is the type of the expression being | |
80373d81 | 4419 | cast. Both TYPE and OTYPE are pointer types. LOC is the location |
4420 | of the cast. -Wcast-qual appeared on the command line. Named | |
4421 | address space qualifiers are not handled here, because they result | |
4422 | in different warnings. */ | |
402ba866 | 4423 | |
4424 | static void | |
80373d81 | 4425 | handle_warn_cast_qual (location_t loc, tree type, tree otype) |
402ba866 | 4426 | { |
4427 | tree in_type = type; | |
4428 | tree in_otype = otype; | |
4429 | int added = 0; | |
4430 | int discarded = 0; | |
4431 | bool is_const; | |
4432 | ||
4433 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4434 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4435 | nodes is uninteresting and we stop as soon as we hit a | |
4436 | non-POINTER_TYPE node on either type. */ | |
4437 | do | |
4438 | { | |
4439 | in_otype = TREE_TYPE (in_otype); | |
4440 | in_type = TREE_TYPE (in_type); | |
4441 | ||
4442 | /* GNU C allows cv-qualified function types. 'const' means the | |
4443 | function is very pure, 'volatile' means it can't return. We | |
4444 | need to warn when such qualifiers are added, not when they're | |
4445 | taken away. */ | |
4446 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4447 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
6d5d708e | 4448 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) |
4449 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
402ba866 | 4450 | else |
6d5d708e | 4451 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) |
4452 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
402ba866 | 4453 | } |
4454 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4455 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4456 | ||
4457 | if (added) | |
80373d81 | 4458 | warning_at (loc, OPT_Wcast_qual, |
4459 | "cast adds %q#v qualifier to function type", added); | |
402ba866 | 4460 | |
4461 | if (discarded) | |
4462 | /* There are qualifiers present in IN_OTYPE that are not present | |
4463 | in IN_TYPE. */ | |
80373d81 | 4464 | warning_at (loc, OPT_Wcast_qual, |
4465 | "cast discards %q#v qualifier from pointer target type", | |
4466 | discarded); | |
402ba866 | 4467 | |
4468 | if (added || discarded) | |
4469 | return; | |
4470 | ||
4471 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4472 | const value to be changed with no additional warning. We only | |
4473 | issue this warning if T is the same on both sides, and we only | |
4474 | issue the warning if there are the same number of pointers on | |
4475 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4476 | cast is unsafe when a qualifier is added at one level and const | |
4477 | is not present at all outer levels. | |
4478 | ||
4479 | To issue this warning, we check at each level whether the cast | |
4480 | adds new qualifiers not already seen. We don't need to special | |
4481 | case function types, as they won't have the same | |
4482 | TYPE_MAIN_VARIANT. */ | |
4483 | ||
4484 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4485 | return; | |
4486 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4487 | return; | |
4488 | ||
4489 | in_type = type; | |
4490 | in_otype = otype; | |
4491 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4492 | do | |
4493 | { | |
4494 | in_type = TREE_TYPE (in_type); | |
4495 | in_otype = TREE_TYPE (in_otype); | |
4496 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4497 | && !is_const) | |
4498 | { | |
80373d81 | 4499 | warning_at (loc, OPT_Wcast_qual, |
4500 | "to be safe all intermediate pointers in cast from " | |
4501 | "%qT to %qT must be %<const%> qualified", | |
4502 | otype, type); | |
402ba866 | 4503 | break; |
4504 | } | |
4505 | if (is_const) | |
4506 | is_const = TYPE_READONLY (in_type); | |
4507 | } | |
4508 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4509 | } | |
4510 | ||
48e1416a | 4511 | /* Build an expression representing a cast to type TYPE of expression EXPR. |
e60a6f7b | 4512 | LOC is the location of the cast-- typically the open paren of the cast. */ |
628fa4f8 | 4513 | |
276beea2 | 4514 | tree |
e60a6f7b | 4515 | build_c_cast (location_t loc, tree type, tree expr) |
276beea2 | 4516 | { |
c6418a4e | 4517 | tree value; |
4518 | ||
4519 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
4520 | expr = TREE_OPERAND (expr, 0); | |
4521 | ||
4522 | value = expr; | |
628fa4f8 | 4523 | |
276beea2 | 4524 | if (type == error_mark_node || expr == error_mark_node) |
4525 | return error_mark_node; | |
628fa4f8 | 4526 | |
276beea2 | 4527 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
4528 | only in <protocol> qualifications. But when constructing cast expressions, | |
4529 | the protocols do matter and must be kept around. */ | |
e0d844be | 4530 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) |
4531 | return build1 (NOP_EXPR, type, expr); | |
4532 | ||
4533 | type = TYPE_MAIN_VARIANT (type); | |
628fa4f8 | 4534 | |
276beea2 | 4535 | if (TREE_CODE (type) == ARRAY_TYPE) |
4536 | { | |
e60a6f7b | 4537 | error_at (loc, "cast specifies array type"); |
276beea2 | 4538 | return error_mark_node; |
4539 | } | |
628fa4f8 | 4540 | |
276beea2 | 4541 | if (TREE_CODE (type) == FUNCTION_TYPE) |
4542 | { | |
e60a6f7b | 4543 | error_at (loc, "cast specifies function type"); |
276beea2 | 4544 | return error_mark_node; |
4545 | } | |
628fa4f8 | 4546 | |
f831c98f | 4547 | if (!VOID_TYPE_P (type)) |
4548 | { | |
4549 | value = require_complete_type (value); | |
4550 | if (value == error_mark_node) | |
4551 | return error_mark_node; | |
4552 | } | |
4553 | ||
276beea2 | 4554 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
4555 | { | |
8864917d | 4556 | if (TREE_CODE (type) == RECORD_TYPE |
4557 | || TREE_CODE (type) == UNION_TYPE) | |
29438999 | 4558 | pedwarn (loc, OPT_Wpedantic, |
8864917d | 4559 | "ISO C forbids casting nonscalar to the same type"); |
276beea2 | 4560 | } |
4561 | else if (TREE_CODE (type) == UNION_TYPE) | |
4562 | { | |
4563 | tree field; | |
628fa4f8 | 4564 | |
1767a056 | 4565 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
ae4400fc | 4566 | if (TREE_TYPE (field) != error_mark_node |
4567 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
4568 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
276beea2 | 4569 | break; |
4570 | ||
4571 | if (field) | |
628fa4f8 | 4572 | { |
276beea2 | 4573 | tree t; |
8c212779 | 4574 | bool maybe_const = true; |
276beea2 | 4575 | |
29438999 | 4576 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); |
8c212779 | 4577 | t = c_fully_fold (value, false, &maybe_const); |
4578 | t = build_constructor_single (type, field, t); | |
4579 | if (!maybe_const) | |
4580 | t = c_wrap_maybe_const (t, true); | |
4581 | t = digest_init (loc, type, t, | |
b9c74b4d | 4582 | NULL_TREE, false, true, 0); |
276beea2 | 4583 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
4584 | return t; | |
628fa4f8 | 4585 | } |
e60a6f7b | 4586 | error_at (loc, "cast to union type from type not present in union"); |
276beea2 | 4587 | return error_mark_node; |
4588 | } | |
4589 | else | |
4590 | { | |
4591 | tree otype, ovalue; | |
628fa4f8 | 4592 | |
276beea2 | 4593 | if (type == void_type_node) |
e60a6f7b | 4594 | { |
4595 | tree t = build1 (CONVERT_EXPR, type, value); | |
4596 | SET_EXPR_LOCATION (t, loc); | |
4597 | return t; | |
4598 | } | |
628fa4f8 | 4599 | |
276beea2 | 4600 | otype = TREE_TYPE (value); |
628fa4f8 | 4601 | |
276beea2 | 4602 | /* Optionally warn about potentially worrisome casts. */ |
276beea2 | 4603 | if (warn_cast_qual |
4604 | && TREE_CODE (type) == POINTER_TYPE | |
4605 | && TREE_CODE (otype) == POINTER_TYPE) | |
80373d81 | 4606 | handle_warn_cast_qual (loc, type, otype); |
628fa4f8 | 4607 | |
6d5d708e | 4608 | /* Warn about conversions between pointers to disjoint |
4609 | address spaces. */ | |
4610 | if (TREE_CODE (type) == POINTER_TYPE | |
4611 | && TREE_CODE (otype) == POINTER_TYPE | |
4612 | && !null_pointer_constant_p (value)) | |
4613 | { | |
4614 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
4615 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
4616 | addr_space_t as_common; | |
4617 | ||
4618 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
4619 | { | |
4620 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
4621 | warning_at (loc, 0, "cast to %s address space pointer " | |
4622 | "from disjoint generic address space pointer", | |
4623 | c_addr_space_name (as_to)); | |
4624 | ||
4625 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
4626 | warning_at (loc, 0, "cast to generic address space pointer " | |
4627 | "from disjoint %s address space pointer", | |
4628 | c_addr_space_name (as_from)); | |
4629 | ||
4630 | else | |
4631 | warning_at (loc, 0, "cast to %s address space pointer " | |
4632 | "from disjoint %s address space pointer", | |
4633 | c_addr_space_name (as_to), | |
4634 | c_addr_space_name (as_from)); | |
4635 | } | |
4636 | } | |
4637 | ||
276beea2 | 4638 | /* Warn about possible alignment problems. */ |
6bf97f82 | 4639 | if (STRICT_ALIGNMENT |
276beea2 | 4640 | && TREE_CODE (type) == POINTER_TYPE |
4641 | && TREE_CODE (otype) == POINTER_TYPE | |
4642 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
4643 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
4644 | /* Don't warn about opaque types, where the actual alignment | |
4645 | restriction is unknown. */ | |
4646 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
4647 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
4648 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
4649 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
e60a6f7b | 4650 | warning_at (loc, OPT_Wcast_align, |
4651 | "cast increases required alignment of target type"); | |
0dbd1c74 | 4652 | |
6bf97f82 | 4653 | if (TREE_CODE (type) == INTEGER_TYPE |
276beea2 | 4654 | && TREE_CODE (otype) == POINTER_TYPE |
b8656032 | 4655 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) |
a0c938f0 | 4656 | /* Unlike conversion of integers to pointers, where the |
4657 | warning is disabled for converting constants because | |
4658 | of cases such as SIG_*, warn about converting constant | |
4659 | pointers to integers. In some cases it may cause unwanted | |
b8656032 | 4660 | sign extension, and a warning is appropriate. */ |
e60a6f7b | 4661 | warning_at (loc, OPT_Wpointer_to_int_cast, |
4662 | "cast from pointer to integer of different size"); | |
628fa4f8 | 4663 | |
6bf97f82 | 4664 | if (TREE_CODE (value) == CALL_EXPR |
276beea2 | 4665 | && TREE_CODE (type) != TREE_CODE (otype)) |
e60a6f7b | 4666 | warning_at (loc, OPT_Wbad_function_cast, |
4667 | "cast from function call of type %qT " | |
4668 | "to non-matching type %qT", otype, type); | |
628fa4f8 | 4669 | |
6bf97f82 | 4670 | if (TREE_CODE (type) == POINTER_TYPE |
276beea2 | 4671 | && TREE_CODE (otype) == INTEGER_TYPE |
4672 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
4673 | /* Don't warn about converting any constant. */ | |
4674 | && !TREE_CONSTANT (value)) | |
e60a6f7b | 4675 | warning_at (loc, |
4676 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
4677 | "of different size"); | |
628fa4f8 | 4678 | |
e6fa0ea6 | 4679 | if (warn_strict_aliasing <= 2) |
4680 | strict_aliasing_warning (otype, type, expr); | |
628fa4f8 | 4681 | |
48b3d385 | 4682 | /* If pedantic, warn for conversions between function and object |
4683 | pointer types, except for converting a null pointer constant | |
4684 | to function pointer type. */ | |
4685 | if (pedantic | |
4686 | && TREE_CODE (type) == POINTER_TYPE | |
4687 | && TREE_CODE (otype) == POINTER_TYPE | |
4688 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
4689 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
29438999 | 4690 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
8864917d | 4691 | "conversion of function pointer to object pointer type"); |
48b3d385 | 4692 | |
4693 | if (pedantic | |
4694 | && TREE_CODE (type) == POINTER_TYPE | |
4695 | && TREE_CODE (otype) == POINTER_TYPE | |
4696 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
4697 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
79f925ed | 4698 | && !null_pointer_constant_p (value)) |
29438999 | 4699 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
8864917d | 4700 | "conversion of object pointer to function pointer type"); |
48b3d385 | 4701 | |
276beea2 | 4702 | ovalue = value; |
276beea2 | 4703 | value = convert (type, value); |
628fa4f8 | 4704 | |
276beea2 | 4705 | /* Ignore any integer overflow caused by the cast. */ |
a75b1c71 | 4706 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) |
276beea2 | 4707 | { |
ca24357c | 4708 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) |
b468918a | 4709 | { |
ca24357c | 4710 | if (!TREE_OVERFLOW (value)) |
4711 | { | |
4712 | /* Avoid clobbering a shared constant. */ | |
4713 | value = copy_node (value); | |
4714 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
4715 | } | |
b468918a | 4716 | } |
ca24357c | 4717 | else if (TREE_OVERFLOW (value)) |
84086eb0 | 4718 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ |
4719 | value = build_int_cst_wide (TREE_TYPE (value), | |
4720 | TREE_INT_CST_LOW (value), | |
4721 | TREE_INT_CST_HIGH (value)); | |
276beea2 | 4722 | } |
4723 | } | |
628fa4f8 | 4724 | |
a7ea5e81 | 4725 | /* Don't let a cast be an lvalue. */ |
4726 | if (value == expr) | |
389dd41b | 4727 | value = non_lvalue_loc (loc, value); |
0dbd1c74 | 4728 | |
a75b1c71 | 4729 | /* Don't allow the results of casting to floating-point or complex |
4730 | types be confused with actual constants, or casts involving | |
4731 | integer and pointer types other than direct integer-to-integer | |
4732 | and integer-to-pointer be confused with integer constant | |
4733 | expressions and null pointer constants. */ | |
4734 | if (TREE_CODE (value) == REAL_CST | |
4735 | || TREE_CODE (value) == COMPLEX_CST | |
4736 | || (TREE_CODE (value) == INTEGER_CST | |
4737 | && !((TREE_CODE (expr) == INTEGER_CST | |
4738 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
4739 | || TREE_CODE (expr) == REAL_CST | |
4740 | || TREE_CODE (expr) == COMPLEX_CST))) | |
4741 | value = build1 (NOP_EXPR, type, value); | |
4742 | ||
e60a6f7b | 4743 | if (CAN_HAVE_LOCATION_P (value)) |
4744 | SET_EXPR_LOCATION (value, loc); | |
276beea2 | 4745 | return value; |
628fa4f8 | 4746 | } |
4747 | ||
e60a6f7b | 4748 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the |
4749 | location of the open paren of the cast, or the position of the cast | |
4750 | expr. */ | |
276beea2 | 4751 | tree |
e60a6f7b | 4752 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) |
628fa4f8 | 4753 | { |
ac206aff | 4754 | tree type; |
a75b1c71 | 4755 | tree type_expr = NULL_TREE; |
4756 | bool type_expr_const = true; | |
4757 | tree ret; | |
276beea2 | 4758 | int saved_wsp = warn_strict_prototypes; |
7014838c | 4759 | |
276beea2 | 4760 | /* This avoids warnings about unprototyped casts on |
4761 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
4762 | if (TREE_CODE (expr) == INTEGER_CST) | |
4763 | warn_strict_prototypes = 0; | |
a75b1c71 | 4764 | type = groktypename (type_name, &type_expr, &type_expr_const); |
276beea2 | 4765 | warn_strict_prototypes = saved_wsp; |
7014838c | 4766 | |
e60a6f7b | 4767 | ret = build_c_cast (loc, type, expr); |
a75b1c71 | 4768 | if (type_expr) |
4769 | { | |
4770 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); | |
4771 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const; | |
e60a6f7b | 4772 | SET_EXPR_LOCATION (ret, loc); |
a75b1c71 | 4773 | } |
0b09525f | 4774 | |
4775 | if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) | |
4776 | SET_EXPR_LOCATION (ret, loc); | |
4777 | ||
ae5ead32 | 4778 | /* C++ does not permits types to be defined in a cast, but it |
4779 | allows references to incomplete types. */ | |
4780 | if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) | |
0b09525f | 4781 | warning_at (loc, OPT_Wc___compat, |
4782 | "defining a type in a cast is invalid in C++"); | |
4783 | ||
a75b1c71 | 4784 | return ret; |
628fa4f8 | 4785 | } |
276beea2 | 4786 | \f |
4787 | /* Build an assignment expression of lvalue LHS from value RHS. | |
8458f4ca | 4788 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which |
4789 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
276beea2 | 4790 | MODIFYCODE is the code for a binary operator that we use |
4791 | to combine the old value of LHS with RHS to get the new value. | |
b6889cb0 | 4792 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
b9c74b4d | 4793 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, |
4794 | which may differ from TREE_TYPE (RHS) for an enum value. | |
b6889cb0 | 4795 | |
e60a6f7b | 4796 | LOCATION is the location of the MODIFYCODE operator. |
4797 | RHS_LOC is the location of the RHS. */ | |
2b30d46c | 4798 | |
276beea2 | 4799 | tree |
8458f4ca | 4800 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, |
48e1416a | 4801 | enum tree_code modifycode, |
e60a6f7b | 4802 | location_t rhs_loc, tree rhs, tree rhs_origtype) |
628fa4f8 | 4803 | { |
276beea2 | 4804 | tree result; |
4805 | tree newrhs; | |
c6418a4e | 4806 | tree rhs_semantic_type = NULL_TREE; |
276beea2 | 4807 | tree lhstype = TREE_TYPE (lhs); |
4808 | tree olhstype = lhstype; | |
a75b1c71 | 4809 | bool npc; |
0dbd1c74 | 4810 | |
276beea2 | 4811 | /* Types that aren't fully specified cannot be used in assignments. */ |
4812 | lhs = require_complete_type (lhs); | |
0dbd1c74 | 4813 | |
276beea2 | 4814 | /* Avoid duplicate error messages from operands that had errors. */ |
4815 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
4816 | return error_mark_node; | |
628fa4f8 | 4817 | |
9d9f5bb3 | 4818 | /* For ObjC properties, defer this check. */ |
fdd84b77 | 4819 | if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) |
ab774101 | 4820 | return error_mark_node; |
4821 | ||
c6418a4e | 4822 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
4823 | { | |
4824 | rhs_semantic_type = TREE_TYPE (rhs); | |
4825 | rhs = TREE_OPERAND (rhs, 0); | |
4826 | } | |
4827 | ||
276beea2 | 4828 | newrhs = rhs; |
628fa4f8 | 4829 | |
a75b1c71 | 4830 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) |
4831 | { | |
4832 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
e60a6f7b | 4833 | lhs_origtype, modifycode, rhs_loc, rhs, |
8458f4ca | 4834 | rhs_origtype); |
a75b1c71 | 4835 | if (inner == error_mark_node) |
4836 | return error_mark_node; | |
4837 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4838 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
4839 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
4840 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
4841 | protected_set_expr_location (result, location); | |
4842 | return result; | |
4843 | } | |
4844 | ||
276beea2 | 4845 | /* If a binary op has been requested, combine the old LHS value with the RHS |
4846 | producing the value we should actually store into the LHS. */ | |
4847 | ||
4848 | if (modifycode != NOP_EXPR) | |
628fa4f8 | 4849 | { |
a75b1c71 | 4850 | lhs = c_fully_fold (lhs, false, NULL); |
276beea2 | 4851 | lhs = stabilize_reference (lhs); |
b6889cb0 | 4852 | newrhs = build_binary_op (location, |
8e70fb09 | 4853 | modifycode, lhs, rhs, 1); |
b9c74b4d | 4854 | |
4855 | /* The original type of the right hand side is no longer | |
4856 | meaningful. */ | |
4857 | rhs_origtype = NULL_TREE; | |
628fa4f8 | 4858 | } |
628fa4f8 | 4859 | |
86c110ac | 4860 | if (c_dialect_objc ()) |
4861 | { | |
9d9f5bb3 | 4862 | /* Check if we are modifying an Objective-C property reference; |
4863 | if so, we need to generate setter calls. */ | |
4864 | result = objc_maybe_build_modify_expr (lhs, newrhs); | |
86c110ac | 4865 | if (result) |
4866 | return result; | |
9d9f5bb3 | 4867 | |
4868 | /* Else, do the check that we postponed for Objective-C. */ | |
fdd84b77 | 4869 | if (!lvalue_or_else (location, lhs, lv_assign)) |
86c110ac | 4870 | return error_mark_node; |
4871 | } | |
4872 | ||
056a8ee6 | 4873 | /* Give an error for storing in something that is 'const'. */ |
874d597d | 4874 | |
d4e60318 | 4875 | if (TYPE_READONLY (lhstype) |
276beea2 | 4876 | || ((TREE_CODE (lhstype) == RECORD_TYPE |
4877 | || TREE_CODE (lhstype) == UNION_TYPE) | |
4878 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
1e8e9920 | 4879 | { |
4880 | readonly_error (lhs, lv_assign); | |
4881 | return error_mark_node; | |
4882 | } | |
d4e60318 | 4883 | else if (TREE_READONLY (lhs)) |
4884 | readonly_warning (lhs, lv_assign); | |
874d597d | 4885 | |
276beea2 | 4886 | /* If storing into a structure or union member, |
4887 | it has probably been given type `int'. | |
4888 | Compute the type that would go with | |
4889 | the actual amount of storage the member occupies. */ | |
874d597d | 4890 | |
276beea2 | 4891 | if (TREE_CODE (lhs) == COMPONENT_REF |
4892 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
4893 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
4894 | || TREE_CODE (lhstype) == REAL_TYPE | |
4895 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
4896 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
628fa4f8 | 4897 | |
276beea2 | 4898 | /* If storing in a field that is in actuality a short or narrower than one, |
4899 | we must store in the field in its actual type. */ | |
4900 | ||
4901 | if (lhstype != TREE_TYPE (lhs)) | |
4902 | { | |
4903 | lhs = copy_node (lhs); | |
4904 | TREE_TYPE (lhs) = lhstype; | |
628fa4f8 | 4905 | } |
628fa4f8 | 4906 | |
8458f4ca | 4907 | /* Issue -Wc++-compat warnings about an assignment to an enum type |
4908 | when LHS does not have its original type. This happens for, | |
4909 | e.g., an enum bitfield in a struct. */ | |
4910 | if (warn_cxx_compat | |
4911 | && lhs_origtype != NULL_TREE | |
4912 | && lhs_origtype != lhstype | |
4913 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
4914 | { | |
4915 | tree checktype = (rhs_origtype != NULL_TREE | |
4916 | ? rhs_origtype | |
4917 | : TREE_TYPE (rhs)); | |
4918 | if (checktype != error_mark_node | |
4919 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)) | |
4920 | warning_at (location, OPT_Wc___compat, | |
4921 | "enum conversion in assignment is invalid in C++"); | |
4922 | } | |
4923 | ||
c6418a4e | 4924 | /* Convert new value to destination type. Fold it first, then |
4925 | restore any excess precision information, for the sake of | |
4926 | conversion warnings. */ | |
628fa4f8 | 4927 | |
a75b1c71 | 4928 | npc = null_pointer_constant_p (newrhs); |
4929 | newrhs = c_fully_fold (newrhs, false, NULL); | |
c6418a4e | 4930 | if (rhs_semantic_type) |
4931 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
d5b637fa | 4932 | newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype, |
4933 | ic_assign, npc, NULL_TREE, NULL_TREE, 0); | |
276beea2 | 4934 | if (TREE_CODE (newrhs) == ERROR_MARK) |
4935 | return error_mark_node; | |
628fa4f8 | 4936 | |
ddb5d39d | 4937 | /* Emit ObjC write barrier, if necessary. */ |
4938 | if (c_dialect_objc () && flag_objc_gc) | |
4939 | { | |
4940 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
4941 | if (result) | |
b6889cb0 | 4942 | { |
4943 | protected_set_expr_location (result, location); | |
4944 | return result; | |
4945 | } | |
ddb5d39d | 4946 | } |
4947 | ||
778ac06a | 4948 | /* Scan operands. */ |
628fa4f8 | 4949 | |
14ae0310 | 4950 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); |
276beea2 | 4951 | TREE_SIDE_EFFECTS (result) = 1; |
b6889cb0 | 4952 | protected_set_expr_location (result, location); |
628fa4f8 | 4953 | |
276beea2 | 4954 | /* If we got the LHS in a different type for storing in, |
4955 | convert the result back to the nominal type of LHS | |
4956 | so that the value we return always has the same type | |
4957 | as the LHS argument. */ | |
5ada9644 | 4958 | |
276beea2 | 4959 | if (olhstype == TREE_TYPE (result)) |
4960 | return result; | |
b6889cb0 | 4961 | |
d5b637fa | 4962 | result = convert_for_assignment (location, olhstype, result, rhs_origtype, |
4963 | ic_assign, false, NULL_TREE, NULL_TREE, 0); | |
b6889cb0 | 4964 | protected_set_expr_location (result, location); |
4965 | return result; | |
276beea2 | 4966 | } |
4967 | \f | |
2fdec027 | 4968 | /* Return whether STRUCT_TYPE has an anonymous field with type TYPE. |
4969 | This is used to implement -fplan9-extensions. */ | |
4970 | ||
4971 | static bool | |
4972 | find_anonymous_field_with_type (tree struct_type, tree type) | |
4973 | { | |
4974 | tree field; | |
4975 | bool found; | |
4976 | ||
4977 | gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE | |
4978 | || TREE_CODE (struct_type) == UNION_TYPE); | |
4979 | found = false; | |
4980 | for (field = TYPE_FIELDS (struct_type); | |
4981 | field != NULL_TREE; | |
4982 | field = TREE_CHAIN (field)) | |
4983 | { | |
4984 | if (DECL_NAME (field) == NULL | |
4985 | && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field)))) | |
4986 | { | |
4987 | if (found) | |
4988 | return false; | |
4989 | found = true; | |
4990 | } | |
4991 | else if (DECL_NAME (field) == NULL | |
4992 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
4993 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
4994 | && find_anonymous_field_with_type (TREE_TYPE (field), type)) | |
4995 | { | |
4996 | if (found) | |
4997 | return false; | |
4998 | found = true; | |
4999 | } | |
5000 | } | |
5001 | return found; | |
5002 | } | |
5003 | ||
5004 | /* RHS is an expression whose type is pointer to struct. If there is | |
5005 | an anonymous field in RHS with type TYPE, then return a pointer to | |
5006 | that field in RHS. This is used with -fplan9-extensions. This | |
5007 | returns NULL if no conversion could be found. */ | |
5008 | ||
5009 | static tree | |
5010 | convert_to_anonymous_field (location_t location, tree type, tree rhs) | |
5011 | { | |
5012 | tree rhs_struct_type, lhs_main_type; | |
5013 | tree field, found_field; | |
5014 | bool found_sub_field; | |
5015 | tree ret; | |
5016 | ||
5017 | gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); | |
5018 | rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); | |
5019 | gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE | |
5020 | || TREE_CODE (rhs_struct_type) == UNION_TYPE); | |
5021 | ||
5022 | gcc_assert (POINTER_TYPE_P (type)); | |
5023 | lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
5024 | ||
5025 | found_field = NULL_TREE; | |
5026 | found_sub_field = false; | |
5027 | for (field = TYPE_FIELDS (rhs_struct_type); | |
5028 | field != NULL_TREE; | |
5029 | field = TREE_CHAIN (field)) | |
5030 | { | |
5031 | if (DECL_NAME (field) != NULL_TREE | |
5032 | || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE | |
5033 | && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)) | |
5034 | continue; | |
5035 | if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field)))) | |
5036 | { | |
5037 | if (found_field != NULL_TREE) | |
5038 | return NULL_TREE; | |
5039 | found_field = field; | |
5040 | } | |
5041 | else if (find_anonymous_field_with_type (TREE_TYPE (field), | |
5042 | lhs_main_type)) | |
5043 | { | |
5044 | if (found_field != NULL_TREE) | |
5045 | return NULL_TREE; | |
5046 | found_field = field; | |
5047 | found_sub_field = true; | |
5048 | } | |
5049 | } | |
5050 | ||
5051 | if (found_field == NULL_TREE) | |
5052 | return NULL_TREE; | |
5053 | ||
5054 | ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), | |
5055 | build_fold_indirect_ref (rhs), found_field, | |
5056 | NULL_TREE); | |
5057 | ret = build_fold_addr_expr_loc (location, ret); | |
5058 | ||
5059 | if (found_sub_field) | |
5060 | { | |
5061 | ret = convert_to_anonymous_field (location, type, ret); | |
5062 | gcc_assert (ret != NULL_TREE); | |
5063 | } | |
5064 | ||
5065 | return ret; | |
5066 | } | |
5067 | ||
b9c74b4d | 5068 | /* Convert value RHS to type TYPE as preparation for an assignment to |
5069 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
5070 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
5071 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
5072 | constant before any folding. | |
276beea2 | 5073 | The real work of conversion is done by `convert'. |
5074 | The purpose of this function is to generate error messages | |
5075 | for assignments that are not allowed in C. | |
245d6740 | 5076 | ERRTYPE says whether it is argument passing, assignment, |
5077 | initialization or return. | |
2b30d46c | 5078 | |
e60a6f7b | 5079 | LOCATION is the location of the RHS. |
245d6740 | 5080 | FUNCTION is a tree for the function being called. |
276beea2 | 5081 | PARMNUM is the number of the argument, for printing in error messages. */ |
dcf6eb8b | 5082 | |
276beea2 | 5083 | static tree |
d5b637fa | 5084 | convert_for_assignment (location_t location, tree type, tree rhs, |
5085 | tree origtype, enum impl_conv errtype, | |
5086 | bool null_pointer_constant, tree fundecl, | |
5087 | tree function, int parmnum) | |
276beea2 | 5088 | { |
5089 | enum tree_code codel = TREE_CODE (type); | |
c6418a4e | 5090 | tree orig_rhs = rhs; |
276beea2 | 5091 | tree rhstype; |
5092 | enum tree_code coder; | |
245d6740 | 5093 | tree rname = NULL_TREE; |
34d3c5de | 5094 | bool objc_ok = false; |
245d6740 | 5095 | |
5933982f | 5096 | if (errtype == ic_argpass) |
245d6740 | 5097 | { |
5098 | tree selector; | |
5099 | /* Change pointer to function to the function itself for | |
5100 | diagnostics. */ | |
5101 | if (TREE_CODE (function) == ADDR_EXPR | |
5102 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
5103 | function = TREE_OPERAND (function, 0); | |
5104 | ||
5105 | /* Handle an ObjC selector specially for diagnostics. */ | |
5106 | selector = objc_message_selector (); | |
5107 | rname = function; | |
5108 | if (selector && parmnum > 2) | |
5109 | { | |
5110 | rname = selector; | |
5111 | parmnum -= 2; | |
5112 | } | |
5113 | } | |
5114 | ||
5115 | /* This macro is used to emit diagnostics to ensure that all format | |
5116 | strings are complete sentences, visible to gettext and checked at | |
5117 | compile time. */ | |
e60a6f7b | 5118 | #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \ |
3230bfa8 | 5119 | do { \ |
5120 | switch (errtype) \ | |
5121 | { \ | |
5122 | case ic_argpass: \ | |
5123 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \ | |
e60a6f7b | 5124 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5125 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
3230bfa8 | 5126 | "expected %qT but argument is of type %qT", \ |
5127 | type, rhstype); \ | |
5128 | break; \ | |
3230bfa8 | 5129 | case ic_assign: \ |
5130 | pedwarn (LOCATION, OPT, AS); \ | |
5131 | break; \ | |
5132 | case ic_init: \ | |
1183a806 | 5133 | pedwarn_init (LOCATION, OPT, IN); \ |
3230bfa8 | 5134 | break; \ |
5135 | case ic_return: \ | |
e60a6f7b | 5136 | pedwarn (LOCATION, OPT, RE); \ |
3230bfa8 | 5137 | break; \ |
5138 | default: \ | |
5139 | gcc_unreachable (); \ | |
5140 | } \ | |
245d6740 | 5141 | } while (0) |
dcf6eb8b | 5142 | |
754eb2f5 | 5143 | /* This macro is used to emit diagnostics to ensure that all format |
5144 | strings are complete sentences, visible to gettext and checked at | |
5145 | compile time. It is the same as WARN_FOR_ASSIGNMENT but with an | |
5146 | extra parameter to enumerate qualifiers. */ | |
5147 | ||
5148 | #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \ | |
5149 | do { \ | |
5150 | switch (errtype) \ | |
5151 | { \ | |
5152 | case ic_argpass: \ | |
5153 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \ | |
5154 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ | |
5155 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
5156 | "expected %qT but argument is of type %qT", \ | |
5157 | type, rhstype); \ | |
5158 | break; \ | |
5159 | case ic_assign: \ | |
5160 | pedwarn (LOCATION, OPT, AS, QUALS); \ | |
5161 | break; \ | |
5162 | case ic_init: \ | |
5163 | pedwarn (LOCATION, OPT, IN, QUALS); \ | |
5164 | break; \ | |
5165 | case ic_return: \ | |
5166 | pedwarn (LOCATION, OPT, RE, QUALS); \ | |
5167 | break; \ | |
5168 | default: \ | |
5169 | gcc_unreachable (); \ | |
5170 | } \ | |
5171 | } while (0) | |
5172 | ||
c6418a4e | 5173 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5174 | rhs = TREE_OPERAND (rhs, 0); | |
5175 | ||
276beea2 | 5176 | rhstype = TREE_TYPE (rhs); |
5177 | coder = TREE_CODE (rhstype); | |
5178 | ||
5179 | if (coder == ERROR_MARK) | |
5180 | return error_mark_node; | |
5181 | ||
34d3c5de | 5182 | if (c_dialect_objc ()) |
5183 | { | |
5184 | int parmno; | |
5185 | ||
5186 | switch (errtype) | |
5187 | { | |
5188 | case ic_return: | |
5189 | parmno = 0; | |
5190 | break; | |
5191 | ||
5192 | case ic_assign: | |
5193 | parmno = -1; | |
5194 | break; | |
5195 | ||
5196 | case ic_init: | |
5197 | parmno = -2; | |
5198 | break; | |
5199 | ||
5200 | default: | |
5201 | parmno = parmnum; | |
5202 | break; | |
5203 | } | |
5204 | ||
5205 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
5206 | } | |
5207 | ||
b9c74b4d | 5208 | if (warn_cxx_compat) |
5209 | { | |
5210 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
5211 | if (checktype != error_mark_node | |
5212 | && TREE_CODE (type) == ENUMERAL_TYPE | |
5213 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
5214 | { | |
bc620c5c | 5215 | WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat, |
5216 | G_("enum conversion when passing argument " | |
5217 | "%d of %qE is invalid in C++"), | |
5218 | G_("enum conversion in assignment is " | |
5219 | "invalid in C++"), | |
5220 | G_("enum conversion in initialization is " | |
5221 | "invalid in C++"), | |
5222 | G_("enum conversion in return is " | |
5223 | "invalid in C++")); | |
b9c74b4d | 5224 | } |
5225 | } | |
5226 | ||
276beea2 | 5227 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) |
f170d67f | 5228 | return rhs; |
276beea2 | 5229 | |
5230 | if (coder == VOID_TYPE) | |
628fa4f8 | 5231 | { |
050526fe | 5232 | /* Except for passing an argument to an unprototyped function, |
5233 | this is a constraint violation. When passing an argument to | |
5234 | an unprototyped function, it is compile-time undefined; | |
5235 | making it a constraint in that case was rejected in | |
5236 | DR#252. */ | |
e60a6f7b | 5237 | error_at (location, "void value not ignored as it ought to be"); |
276beea2 | 5238 | return error_mark_node; |
628fa4f8 | 5239 | } |
f831c98f | 5240 | rhs = require_complete_type (rhs); |
5241 | if (rhs == error_mark_node) | |
5242 | return error_mark_node; | |
276beea2 | 5243 | /* A type converts to a reference to it. |
5244 | This code doesn't fully support references, it's just for the | |
5245 | special case of va_start and va_copy. */ | |
5246 | if (codel == REFERENCE_TYPE | |
3ed275a6 | 5247 | && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1) |
628fa4f8 | 5248 | { |
276beea2 | 5249 | if (!lvalue_p (rhs)) |
628fa4f8 | 5250 | { |
e60a6f7b | 5251 | error_at (location, "cannot pass rvalue to reference parameter"); |
276beea2 | 5252 | return error_mark_node; |
628fa4f8 | 5253 | } |
276beea2 | 5254 | if (!c_mark_addressable (rhs)) |
5255 | return error_mark_node; | |
5256 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
e60a6f7b | 5257 | SET_EXPR_LOCATION (rhs, location); |
276beea2 | 5258 | |
5259 | /* We already know that these two types are compatible, but they | |
5260 | may not be exactly identical. In fact, `TREE_TYPE (type)' is | |
5261 | likely to be __builtin_va_list and `TREE_TYPE (rhs)' is | |
5262 | likely to be va_list, a typedef to __builtin_va_list, which | |
5263 | is different enough that it will cause problems later. */ | |
5264 | if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type)) | |
e60a6f7b | 5265 | { |
5266 | rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs); | |
5267 | SET_EXPR_LOCATION (rhs, location); | |
5268 | } | |
276beea2 | 5269 | |
5270 | rhs = build1 (NOP_EXPR, type, rhs); | |
e60a6f7b | 5271 | SET_EXPR_LOCATION (rhs, location); |
276beea2 | 5272 | return rhs; |
628fa4f8 | 5273 | } |
276beea2 | 5274 | /* Some types can interconvert without explicit casts. */ |
0d45e76f | 5275 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE |
546c4794 | 5276 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) |
276beea2 | 5277 | return convert (type, rhs); |
5278 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
5279 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
9421ebb9 | 5280 | || codel == FIXED_POINT_TYPE |
276beea2 | 5281 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE |
5282 | || codel == BOOLEAN_TYPE) | |
5283 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
9421ebb9 | 5284 | || coder == FIXED_POINT_TYPE |
276beea2 | 5285 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE |
5286 | || coder == BOOLEAN_TYPE)) | |
a75b1c71 | 5287 | { |
5288 | tree ret; | |
5289 | bool save = in_late_binary_op; | |
a7d4dc67 | 5290 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) |
a75b1c71 | 5291 | in_late_binary_op = true; |
c6418a4e | 5292 | ret = convert_and_check (type, orig_rhs); |
a7d4dc67 | 5293 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) |
a75b1c71 | 5294 | in_late_binary_op = save; |
5295 | return ret; | |
5296 | } | |
628fa4f8 | 5297 | |
aedd6e32 | 5298 | /* Aggregates in different TUs might need conversion. */ |
5299 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
5300 | && codel == coder | |
5301 | && comptypes (type, rhstype)) | |
5302 | return convert_and_check (type, rhs); | |
5303 | ||
8df5a43d | 5304 | /* Conversion to a transparent union or record from its member types. |
276beea2 | 5305 | This applies only to function arguments. */ |
8df5a43d | 5306 | if (((codel == UNION_TYPE || codel == RECORD_TYPE) |
5307 | && TYPE_TRANSPARENT_AGGR (type)) | |
5933982f | 5308 | && errtype == ic_argpass) |
628fa4f8 | 5309 | { |
0f7adc03 | 5310 | tree memb, marginal_memb = NULL_TREE; |
276beea2 | 5311 | |
1767a056 | 5312 | for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) |
628fa4f8 | 5313 | { |
0f7adc03 | 5314 | tree memb_type = TREE_TYPE (memb); |
628fa4f8 | 5315 | |
276beea2 | 5316 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
3ed275a6 | 5317 | TYPE_MAIN_VARIANT (rhstype))) |
276beea2 | 5318 | break; |
8915b00e | 5319 | |
276beea2 | 5320 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
5321 | continue; | |
2b30d46c | 5322 | |
276beea2 | 5323 | if (coder == POINTER_TYPE) |
5324 | { | |
5325 | tree ttl = TREE_TYPE (memb_type); | |
5326 | tree ttr = TREE_TYPE (rhstype); | |
628fa4f8 | 5327 | |
276beea2 | 5328 | /* Any non-function converts to a [const][volatile] void * |
5329 | and vice versa; otherwise, targets must be the same. | |
5330 | Meanwhile, the lhs target must have all the qualifiers of | |
5331 | the rhs. */ | |
5332 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
d5b637fa | 5333 | || comp_target_types (location, memb_type, rhstype)) |
276beea2 | 5334 | { |
5335 | /* If this type won't generate any warnings, use it. */ | |
5336 | if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr) | |
5337 | || ((TREE_CODE (ttr) == FUNCTION_TYPE | |
5338 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5339 | ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5340 | == TYPE_QUALS (ttr)) | |
5341 | : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5342 | == TYPE_QUALS (ttl)))) | |
5343 | break; | |
628fa4f8 | 5344 | |
276beea2 | 5345 | /* Keep looking for a better type, but remember this one. */ |
0f7adc03 | 5346 | if (!marginal_memb) |
5347 | marginal_memb = memb; | |
276beea2 | 5348 | } |
5349 | } | |
448d3ee7 | 5350 | |
276beea2 | 5351 | /* Can convert integer zero to any pointer type. */ |
a75b1c71 | 5352 | if (null_pointer_constant) |
276beea2 | 5353 | { |
5354 | rhs = null_pointer_node; | |
5355 | break; | |
5356 | } | |
5357 | } | |
628fa4f8 | 5358 | |
0f7adc03 | 5359 | if (memb || marginal_memb) |
276beea2 | 5360 | { |
0f7adc03 | 5361 | if (!memb) |
276beea2 | 5362 | { |
5363 | /* We have only a marginally acceptable member type; | |
5364 | it needs a warning. */ | |
0f7adc03 | 5365 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); |
276beea2 | 5366 | tree ttr = TREE_TYPE (rhstype); |
c377133d | 5367 | |
276beea2 | 5368 | /* Const and volatile mean something different for function |
5369 | types, so the usual warnings are not appropriate. */ | |
5370 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
5371 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5372 | { | |
5373 | /* Because const and volatile on functions are | |
5374 | restrictions that say the function will not do | |
5375 | certain things, it is okay to use a const or volatile | |
5376 | function where an ordinary one is wanted, but not | |
5377 | vice-versa. */ | |
6d5d708e | 5378 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5379 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
754eb2f5 | 5380 | WARN_FOR_QUALIFIERS (location, 0, |
21ca8540 | 5381 | G_("passing argument %d of %qE " |
754eb2f5 | 5382 | "makes %q#v qualified function " |
245d6740 | 5383 | "pointer from unqualified"), |
754eb2f5 | 5384 | G_("assignment makes %q#v qualified " |
245d6740 | 5385 | "function pointer from " |
5386 | "unqualified"), | |
754eb2f5 | 5387 | G_("initialization makes %q#v qualified " |
245d6740 | 5388 | "function pointer from " |
5389 | "unqualified"), | |
754eb2f5 | 5390 | G_("return makes %q#v qualified function " |
5391 | "pointer from unqualified"), | |
5392 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
276beea2 | 5393 | } |
6d5d708e | 5394 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5395 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
754eb2f5 | 5396 | WARN_FOR_QUALIFIERS (location, 0, |
21ca8540 | 5397 | G_("passing argument %d of %qE discards " |
754eb2f5 | 5398 | "%qv qualifier from pointer target type"), |
5399 | G_("assignment discards %qv qualifier " | |
245d6740 | 5400 | "from pointer target type"), |
754eb2f5 | 5401 | G_("initialization discards %qv qualifier " |
245d6740 | 5402 | "from pointer target type"), |
754eb2f5 | 5403 | G_("return discards %qv qualifier from " |
5404 | "pointer target type"), | |
5405 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
0f7adc03 | 5406 | |
5407 | memb = marginal_memb; | |
276beea2 | 5408 | } |
628fa4f8 | 5409 | |
8864917d | 5410 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) |
29438999 | 5411 | pedwarn (location, OPT_Wpedantic, |
8864917d | 5412 | "ISO C prohibits argument conversion to union type"); |
5fcbb750 | 5413 | |
389dd41b | 5414 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); |
0f7adc03 | 5415 | return build_constructor_single (type, memb, rhs); |
276beea2 | 5416 | } |
5fcbb750 | 5417 | } |
5418 | ||
276beea2 | 5419 | /* Conversions among pointers */ |
5420 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
5421 | && (coder == codel)) | |
628fa4f8 | 5422 | { |
276beea2 | 5423 | tree ttl = TREE_TYPE (type); |
5424 | tree ttr = TREE_TYPE (rhstype); | |
aebc8537 | 5425 | tree mvl = ttl; |
5426 | tree mvr = ttr; | |
276beea2 | 5427 | bool is_opaque_pointer; |
c17b85ea | 5428 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
6d5d708e | 5429 | addr_space_t asl; |
5430 | addr_space_t asr; | |
628fa4f8 | 5431 | |
aebc8537 | 5432 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
5433 | mvl = TYPE_MAIN_VARIANT (mvl); | |
5434 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
5435 | mvr = TYPE_MAIN_VARIANT (mvr); | |
276beea2 | 5436 | /* Opaque pointers are treated like void pointers. */ |
ed7c4e62 | 5437 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); |
a0c938f0 | 5438 | |
2fdec027 | 5439 | /* The Plan 9 compiler permits a pointer to a struct to be |
5440 | automatically converted into a pointer to an anonymous field | |
5441 | within the struct. */ | |
5442 | if (flag_plan9_extensions | |
5443 | && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE) | |
5444 | && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE) | |
5445 | && mvl != mvr) | |
5446 | { | |
5447 | tree new_rhs = convert_to_anonymous_field (location, type, rhs); | |
5448 | if (new_rhs != NULL_TREE) | |
5449 | { | |
5450 | rhs = new_rhs; | |
5451 | rhstype = TREE_TYPE (rhs); | |
5452 | coder = TREE_CODE (rhstype); | |
5453 | ttr = TREE_TYPE (rhstype); | |
5454 | mvr = TYPE_MAIN_VARIANT (ttr); | |
5455 | } | |
5456 | } | |
5457 | ||
fa2eba0d | 5458 | /* C++ does not allow the implicit conversion void* -> T*. However, |
a0c938f0 | 5459 | for the purpose of reducing the number of false positives, we |
5460 | tolerate the special case of | |
fa2eba0d | 5461 | |
a0c938f0 | 5462 | int *p = NULL; |
fa2eba0d | 5463 | |
a0c938f0 | 5464 | where NULL is typically defined in C to be '(void *) 0'. */ |
bcdafdcb | 5465 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) |
e60a6f7b | 5466 | warning_at (location, OPT_Wc___compat, |
5467 | "request for implicit conversion " | |
5468 | "from %qT to %qT not permitted in C++", rhstype, type); | |
628fa4f8 | 5469 | |
6d5d708e | 5470 | /* See if the pointers point to incompatible address spaces. */ |
5471 | asl = TYPE_ADDR_SPACE (ttl); | |
5472 | asr = TYPE_ADDR_SPACE (ttr); | |
5473 | if (!null_pointer_constant_p (rhs) | |
5474 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
5475 | { | |
5476 | switch (errtype) | |
5477 | { | |
5478 | case ic_argpass: | |
5479 | error_at (location, "passing argument %d of %qE from pointer to " | |
5480 | "non-enclosed address space", parmnum, rname); | |
5481 | break; | |
5482 | case ic_assign: | |
5483 | error_at (location, "assignment from pointer to " | |
5484 | "non-enclosed address space"); | |
5485 | break; | |
5486 | case ic_init: | |
5487 | error_at (location, "initialization from pointer to " | |
5488 | "non-enclosed address space"); | |
5489 | break; | |
5490 | case ic_return: | |
5491 | error_at (location, "return from pointer to " | |
5492 | "non-enclosed address space"); | |
5493 | break; | |
5494 | default: | |
5495 | gcc_unreachable (); | |
5496 | } | |
5497 | return error_mark_node; | |
5498 | } | |
5499 | ||
95c90e04 | 5500 | /* Check if the right-hand side has a format attribute but the |
5501 | left-hand side doesn't. */ | |
b86527d8 | 5502 | if (warn_suggest_attribute_format |
be7350e7 | 5503 | && check_missing_format_attribute (type, rhstype)) |
a0c938f0 | 5504 | { |
be7350e7 | 5505 | switch (errtype) |
5506 | { | |
5507 | case ic_argpass: | |
b86527d8 | 5508 | warning_at (location, OPT_Wsuggest_attribute_format, |
e60a6f7b | 5509 | "argument %d of %qE might be " |
5510 | "a candidate for a format attribute", | |
5511 | parmnum, rname); | |
be7350e7 | 5512 | break; |
5513 | case ic_assign: | |
b86527d8 | 5514 | warning_at (location, OPT_Wsuggest_attribute_format, |
e60a6f7b | 5515 | "assignment left-hand side might be " |
5516 | "a candidate for a format attribute"); | |
be7350e7 | 5517 | break; |
5518 | case ic_init: | |
b86527d8 | 5519 | warning_at (location, OPT_Wsuggest_attribute_format, |
e60a6f7b | 5520 | "initialization left-hand side might be " |
5521 | "a candidate for a format attribute"); | |
be7350e7 | 5522 | break; |
5523 | case ic_return: | |
b86527d8 | 5524 | warning_at (location, OPT_Wsuggest_attribute_format, |
e60a6f7b | 5525 | "return type might be " |
5526 | "a candidate for a format attribute"); | |
be7350e7 | 5527 | break; |
5528 | default: | |
5529 | gcc_unreachable (); | |
5530 | } | |
95c90e04 | 5531 | } |
a0c938f0 | 5532 | |
276beea2 | 5533 | /* Any non-function converts to a [const][volatile] void * |
5534 | and vice versa; otherwise, targets must be the same. | |
5535 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
5536 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
d5b637fa | 5537 | || (target_cmp = comp_target_types (location, type, rhstype)) |
276beea2 | 5538 | || is_opaque_pointer |
11773141 | 5539 | || (c_common_unsigned_type (mvl) |
5540 | == c_common_unsigned_type (mvr))) | |
276beea2 | 5541 | { |
5542 | if (pedantic | |
5543 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5544 | || | |
5545 | (VOID_TYPE_P (ttr) | |
a75b1c71 | 5546 | && !null_pointer_constant |
276beea2 | 5547 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
29438999 | 5548 | WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic, |
21ca8540 | 5549 | G_("ISO C forbids passing argument %d of " |
245d6740 | 5550 | "%qE between function pointer " |
5551 | "and %<void *%>"), | |
380c6697 | 5552 | G_("ISO C forbids assignment between " |
245d6740 | 5553 | "function pointer and %<void *%>"), |
380c6697 | 5554 | G_("ISO C forbids initialization between " |
245d6740 | 5555 | "function pointer and %<void *%>"), |
380c6697 | 5556 | G_("ISO C forbids return between function " |
245d6740 | 5557 | "pointer and %<void *%>")); |
276beea2 | 5558 | /* Const and volatile mean something different for function types, |
5559 | so the usual warnings are not appropriate. */ | |
5560 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
5561 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
5562 | { | |
6d5d708e | 5563 | if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5564 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
34d3c5de | 5565 | { |
d7489d8d | 5566 | WARN_FOR_QUALIFIERS (location, 0, |
5567 | G_("passing argument %d of %qE discards " | |
5568 | "%qv qualifier from pointer target type"), | |
5569 | G_("assignment discards %qv qualifier " | |
5570 | "from pointer target type"), | |
5571 | G_("initialization discards %qv qualifier " | |
5572 | "from pointer target type"), | |
5573 | G_("return discards %qv qualifier from " | |
5574 | "pointer target type"), | |
5575 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
34d3c5de | 5576 | } |
276beea2 | 5577 | /* If this is not a case of ignoring a mismatch in signedness, |
5578 | no warning. */ | |
5579 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
c17b85ea | 5580 | || target_cmp) |
276beea2 | 5581 | ; |
5582 | /* If there is a mismatch, do warn. */ | |
380bf74b | 5583 | else if (warn_pointer_sign) |
e60a6f7b | 5584 | WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign, |
21ca8540 | 5585 | G_("pointer targets in passing argument " |
245d6740 | 5586 | "%d of %qE differ in signedness"), |
380c6697 | 5587 | G_("pointer targets in assignment " |
245d6740 | 5588 | "differ in signedness"), |
380c6697 | 5589 | G_("pointer targets in initialization " |
245d6740 | 5590 | "differ in signedness"), |
380c6697 | 5591 | G_("pointer targets in return differ " |
245d6740 | 5592 | "in signedness")); |
276beea2 | 5593 | } |
5594 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
5595 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5596 | { | |
5597 | /* Because const and volatile on functions are restrictions | |
5598 | that say the function will not do certain things, | |
5599 | it is okay to use a const or volatile function | |
5600 | where an ordinary one is wanted, but not vice-versa. */ | |
6d5d708e | 5601 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5602 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
754eb2f5 | 5603 | WARN_FOR_QUALIFIERS (location, 0, |
21ca8540 | 5604 | G_("passing argument %d of %qE makes " |
754eb2f5 | 5605 | "%q#v qualified function pointer " |
245d6740 | 5606 | "from unqualified"), |
754eb2f5 | 5607 | G_("assignment makes %q#v qualified function " |
245d6740 | 5608 | "pointer from unqualified"), |
754eb2f5 | 5609 | G_("initialization makes %q#v qualified " |
245d6740 | 5610 | "function pointer from unqualified"), |
754eb2f5 | 5611 | G_("return makes %q#v qualified function " |
5612 | "pointer from unqualified"), | |
5613 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
276beea2 | 5614 | } |
5615 | } | |
5616 | else | |
34d3c5de | 5617 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
5618 | if (!objc_ok) | |
e60a6f7b | 5619 | WARN_FOR_ASSIGNMENT (location, 0, |
21ca8540 | 5620 | G_("passing argument %d of %qE from " |
34d3c5de | 5621 | "incompatible pointer type"), |
380c6697 | 5622 | G_("assignment from incompatible pointer type"), |
5623 | G_("initialization from incompatible " | |
34d3c5de | 5624 | "pointer type"), |
380c6697 | 5625 | G_("return from incompatible pointer type")); |
34d3c5de | 5626 | |
276beea2 | 5627 | return convert (type, rhs); |
5628 | } | |
5cd106da | 5629 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
5630 | { | |
050526fe | 5631 | /* ??? This should not be an error when inlining calls to |
5632 | unprototyped functions. */ | |
e60a6f7b | 5633 | error_at (location, "invalid use of non-lvalue array"); |
5cd106da | 5634 | return error_mark_node; |
5635 | } | |
276beea2 | 5636 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
628fa4f8 | 5637 | { |
276beea2 | 5638 | /* An explicit constant 0 can convert to a pointer, |
5639 | or one that results from arithmetic, even including | |
5640 | a cast to integer type. */ | |
a75b1c71 | 5641 | if (!null_pointer_constant) |
e60a6f7b | 5642 | WARN_FOR_ASSIGNMENT (location, 0, |
21ca8540 | 5643 | G_("passing argument %d of %qE makes " |
245d6740 | 5644 | "pointer from integer without a cast"), |
380c6697 | 5645 | G_("assignment makes pointer from integer " |
245d6740 | 5646 | "without a cast"), |
380c6697 | 5647 | G_("initialization makes pointer from " |
245d6740 | 5648 | "integer without a cast"), |
380c6697 | 5649 | G_("return makes pointer from integer " |
245d6740 | 5650 | "without a cast")); |
d318227b | 5651 | |
5652 | return convert (type, rhs); | |
628fa4f8 | 5653 | } |
276beea2 | 5654 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
628fa4f8 | 5655 | { |
e60a6f7b | 5656 | WARN_FOR_ASSIGNMENT (location, 0, |
21ca8540 | 5657 | G_("passing argument %d of %qE makes integer " |
245d6740 | 5658 | "from pointer without a cast"), |
380c6697 | 5659 | G_("assignment makes integer from pointer " |
245d6740 | 5660 | "without a cast"), |
380c6697 | 5661 | G_("initialization makes integer from pointer " |
245d6740 | 5662 | "without a cast"), |
380c6697 | 5663 | G_("return makes integer from pointer " |
245d6740 | 5664 | "without a cast")); |
276beea2 | 5665 | return convert (type, rhs); |
5666 | } | |
5667 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
a75b1c71 | 5668 | { |
5669 | tree ret; | |
5670 | bool save = in_late_binary_op; | |
5671 | in_late_binary_op = true; | |
5672 | ret = convert (type, rhs); | |
5673 | in_late_binary_op = save; | |
5674 | return ret; | |
5675 | } | |
628fa4f8 | 5676 | |
245d6740 | 5677 | switch (errtype) |
276beea2 | 5678 | { |
245d6740 | 5679 | case ic_argpass: |
e60a6f7b | 5680 | error_at (location, "incompatible type for argument %d of %qE", parmnum, rname); |
bd8a0c52 | 5681 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) |
5682 | ? DECL_SOURCE_LOCATION (fundecl) : input_location, | |
5683 | "expected %qT but argument is of type %qT", type, rhstype); | |
245d6740 | 5684 | break; |
5685 | case ic_assign: | |
e60a6f7b | 5686 | error_at (location, "incompatible types when assigning to type %qT from " |
5687 | "type %qT", type, rhstype); | |
245d6740 | 5688 | break; |
5689 | case ic_init: | |
e60a6f7b | 5690 | error_at (location, |
5691 | "incompatible types when initializing type %qT using type %qT", | |
5692 | type, rhstype); | |
245d6740 | 5693 | break; |
5694 | case ic_return: | |
e60a6f7b | 5695 | error_at (location, |
5696 | "incompatible types when returning type %qT but %qT was " | |
5697 | "expected", rhstype, type); | |
245d6740 | 5698 | break; |
5699 | default: | |
5700 | gcc_unreachable (); | |
628fa4f8 | 5701 | } |
8ba8da08 | 5702 | |
276beea2 | 5703 | return error_mark_node; |
5704 | } | |
276beea2 | 5705 | \f |
5706 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
5707 | return its value. Otherwise, return error_mark_node. | |
d0a47c8d | 5708 | |
276beea2 | 5709 | This is for handling COMPOUND_EXPRs as initializer elements |
5710 | which is allowed with a warning when -pedantic is specified. */ | |
d0a47c8d | 5711 | |
276beea2 | 5712 | static tree |
5713 | valid_compound_expr_initializer (tree value, tree endtype) | |
5714 | { | |
5715 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
5716 | { | |
5717 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
5718 | == error_mark_node) | |
5719 | return error_mark_node; | |
5720 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
5721 | endtype); | |
5722 | } | |
fdf95cc9 | 5723 | else if (!initializer_constant_valid_p (value, endtype)) |
276beea2 | 5724 | return error_mark_node; |
5725 | else | |
5726 | return value; | |
d0a47c8d | 5727 | } |
628fa4f8 | 5728 | \f |
276beea2 | 5729 | /* Perform appropriate conversions on the initial value of a variable, |
5730 | store it in the declaration DECL, | |
5731 | and print any error messages that are appropriate. | |
b9c74b4d | 5732 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
e60a6f7b | 5733 | If the init is invalid, store an ERROR_MARK. |
5734 | ||
5735 | INIT_LOC is the location of the initial value. */ | |
628fa4f8 | 5736 | |
276beea2 | 5737 | void |
e60a6f7b | 5738 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) |
628fa4f8 | 5739 | { |
276beea2 | 5740 | tree value, type; |
a75b1c71 | 5741 | bool npc = false; |
628fa4f8 | 5742 | |
276beea2 | 5743 | /* If variable's type was invalidly declared, just ignore it. */ |
628fa4f8 | 5744 | |
276beea2 | 5745 | type = TREE_TYPE (decl); |
5746 | if (TREE_CODE (type) == ERROR_MARK) | |
5747 | return; | |
628fa4f8 | 5748 | |
276beea2 | 5749 | /* Digest the specified initializer into an expression. */ |
628fa4f8 | 5750 | |
a75b1c71 | 5751 | if (init) |
5752 | npc = null_pointer_constant_p (init); | |
e60a6f7b | 5753 | value = digest_init (init_loc, type, init, origtype, npc, |
5754 | true, TREE_STATIC (decl)); | |
628fa4f8 | 5755 | |
276beea2 | 5756 | /* Store the expression if valid; else report error. */ |
628fa4f8 | 5757 | |
6bf97f82 | 5758 | if (!in_system_header |
84166705 | 5759 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) |
6bf97f82 | 5760 | warning (OPT_Wtraditional, "traditional C rejects automatic " |
5761 | "aggregate initialization"); | |
2b30d46c | 5762 | |
276beea2 | 5763 | DECL_INITIAL (decl) = value; |
628fa4f8 | 5764 | |
276beea2 | 5765 | /* ANSI wants warnings about out-of-range constant initializers. */ |
5766 | STRIP_TYPE_NOPS (value); | |
48e1416a | 5767 | if (TREE_STATIC (decl)) |
67e05044 | 5768 | constant_expression_warning (value); |
628fa4f8 | 5769 | |
276beea2 | 5770 | /* Check if we need to set array size from compound literal size. */ |
5771 | if (TREE_CODE (type) == ARRAY_TYPE | |
5772 | && TYPE_DOMAIN (type) == 0 | |
5773 | && value != error_mark_node) | |
628fa4f8 | 5774 | { |
276beea2 | 5775 | tree inside_init = init; |
5776 | ||
a97c952c | 5777 | STRIP_TYPE_NOPS (inside_init); |
276beea2 | 5778 | inside_init = fold (inside_init); |
5779 | ||
5780 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
5781 | { | |
ce675af0 | 5782 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
276beea2 | 5783 | |
ce675af0 | 5784 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) |
276beea2 | 5785 | { |
5786 | /* For int foo[] = (int [3]){1}; we need to set array size | |
5787 | now since later on array initializer will be just the | |
5788 | brace enclosed list of the compound literal. */ | |
9e1f1fbf | 5789 | tree etype = strip_array_types (TREE_TYPE (decl)); |
ce675af0 | 5790 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); |
ce675af0 | 5791 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); |
276beea2 | 5792 | layout_type (type); |
ce675af0 | 5793 | layout_decl (cldecl, 0); |
9e1f1fbf | 5794 | TREE_TYPE (decl) |
5795 | = c_build_qualified_type (type, TYPE_QUALS (etype)); | |
276beea2 | 5796 | } |
5797 | } | |
628fa4f8 | 5798 | } |
276beea2 | 5799 | } |
5800 | \f | |
5801 | /* Methods for storing and printing names for error messages. */ | |
628fa4f8 | 5802 | |
276beea2 | 5803 | /* Implement a spelling stack that allows components of a name to be pushed |
5804 | and popped. Each element on the stack is this structure. */ | |
628fa4f8 | 5805 | |
276beea2 | 5806 | struct spelling |
5807 | { | |
5808 | int kind; | |
5809 | union | |
628fa4f8 | 5810 | { |
4aeeff51 | 5811 | unsigned HOST_WIDE_INT i; |
276beea2 | 5812 | const char *s; |
5813 | } u; | |
5814 | }; | |
2b30d46c | 5815 | |
276beea2 | 5816 | #define SPELLING_STRING 1 |
5817 | #define SPELLING_MEMBER 2 | |
5818 | #define SPELLING_BOUNDS 3 | |
628fa4f8 | 5819 | |
276beea2 | 5820 | static struct spelling *spelling; /* Next stack element (unused). */ |
5821 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
5822 | static int spelling_size; /* Size of the spelling stack. */ | |
628fa4f8 | 5823 | |
276beea2 | 5824 | /* Macros to save and restore the spelling stack around push_... functions. |
5825 | Alternative to SAVE_SPELLING_STACK. */ | |
628fa4f8 | 5826 | |
276beea2 | 5827 | #define SPELLING_DEPTH() (spelling - spelling_base) |
5828 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
628fa4f8 | 5829 | |
276beea2 | 5830 | /* Push an element on the spelling stack with type KIND and assign VALUE |
5831 | to MEMBER. */ | |
628fa4f8 | 5832 | |
276beea2 | 5833 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
5834 | { \ | |
5835 | int depth = SPELLING_DEPTH (); \ | |
5836 | \ | |
5837 | if (depth >= spelling_size) \ | |
5838 | { \ | |
5839 | spelling_size += 10; \ | |
63aa3db6 | 5840 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
5841 | spelling_size); \ | |
276beea2 | 5842 | RESTORE_SPELLING_DEPTH (depth); \ |
5843 | } \ | |
5844 | \ | |
5845 | spelling->kind = (KIND); \ | |
5846 | spelling->MEMBER = (VALUE); \ | |
5847 | spelling++; \ | |
5848 | } | |
628fa4f8 | 5849 | |
276beea2 | 5850 | /* Push STRING on the stack. Printed literally. */ |
628fa4f8 | 5851 | |
276beea2 | 5852 | static void |
5853 | push_string (const char *string) | |
5854 | { | |
5855 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
5856 | } | |
628fa4f8 | 5857 | |
276beea2 | 5858 | /* Push a member name on the stack. Printed as '.' STRING. */ |
628fa4f8 | 5859 | |
276beea2 | 5860 | static void |
5861 | push_member_name (tree decl) | |
5862 | { | |
5863 | const char *const string | |
d1dd9ac0 | 5864 | = (DECL_NAME (decl) |
5865 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
5866 | : _("<anonymous>")); | |
276beea2 | 5867 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
5868 | } | |
628fa4f8 | 5869 | |
276beea2 | 5870 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
628fa4f8 | 5871 | |
276beea2 | 5872 | static void |
4aeeff51 | 5873 | push_array_bounds (unsigned HOST_WIDE_INT bounds) |
276beea2 | 5874 | { |
5875 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
5876 | } | |
cf18ae1f | 5877 | |
276beea2 | 5878 | /* Compute the maximum size in bytes of the printed spelling. */ |
628fa4f8 | 5879 | |
276beea2 | 5880 | static int |
5881 | spelling_length (void) | |
5882 | { | |
5883 | int size = 0; | |
5884 | struct spelling *p; | |
628fa4f8 | 5885 | |
276beea2 | 5886 | for (p = spelling_base; p < spelling; p++) |
5887 | { | |
5888 | if (p->kind == SPELLING_BOUNDS) | |
5889 | size += 25; | |
5890 | else | |
5891 | size += strlen (p->u.s) + 1; | |
5892 | } | |
5893 | ||
5894 | return size; | |
628fa4f8 | 5895 | } |
628fa4f8 | 5896 | |
276beea2 | 5897 | /* Print the spelling to BUFFER and return it. */ |
628fa4f8 | 5898 | |
276beea2 | 5899 | static char * |
5900 | print_spelling (char *buffer) | |
628fa4f8 | 5901 | { |
276beea2 | 5902 | char *d = buffer; |
5903 | struct spelling *p; | |
628fa4f8 | 5904 | |
276beea2 | 5905 | for (p = spelling_base; p < spelling; p++) |
5906 | if (p->kind == SPELLING_BOUNDS) | |
5907 | { | |
4aeeff51 | 5908 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); |
276beea2 | 5909 | d += strlen (d); |
5910 | } | |
5911 | else | |
5912 | { | |
5913 | const char *s; | |
5914 | if (p->kind == SPELLING_MEMBER) | |
5915 | *d++ = '.'; | |
5916 | for (s = p->u.s; (*d = *s++); d++) | |
5917 | ; | |
5918 | } | |
5919 | *d++ = '\0'; | |
5920 | return buffer; | |
5921 | } | |
628fa4f8 | 5922 | |
276beea2 | 5923 | /* Issue an error message for a bad initializer component. |
1183a806 | 5924 | GMSGID identifies the message. |
276beea2 | 5925 | The component name is taken from the spelling stack. */ |
628fa4f8 | 5926 | |
276beea2 | 5927 | void |
1183a806 | 5928 | error_init (const char *gmsgid) |
276beea2 | 5929 | { |
5930 | char *ofwhat; | |
628fa4f8 | 5931 | |
1183a806 | 5932 | /* The gmsgid may be a format string with %< and %>. */ |
5933 | error (gmsgid); | |
4fd61bc6 | 5934 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
276beea2 | 5935 | if (*ofwhat) |
b0b1af64 | 5936 | error ("(near initialization for %qs)", ofwhat); |
276beea2 | 5937 | } |
628fa4f8 | 5938 | |
8864917d | 5939 | /* Issue a pedantic warning for a bad initializer component. OPT is |
5940 | the option OPT_* (from options.h) controlling this warning or 0 if | |
1183a806 | 5941 | it is unconditionally given. GMSGID identifies the message. The |
8864917d | 5942 | component name is taken from the spelling stack. */ |
628fa4f8 | 5943 | |
276beea2 | 5944 | void |
1183a806 | 5945 | pedwarn_init (location_t location, int opt, const char *gmsgid) |
276beea2 | 5946 | { |
5947 | char *ofwhat; | |
6cf89e04 | 5948 | |
1183a806 | 5949 | /* The gmsgid may be a format string with %< and %>. */ |
5950 | pedwarn (location, opt, gmsgid); | |
4fd61bc6 | 5951 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
276beea2 | 5952 | if (*ofwhat) |
21ca8540 | 5953 | pedwarn (location, opt, "(near initialization for %qs)", ofwhat); |
276beea2 | 5954 | } |
2d47cc32 | 5955 | |
48e1416a | 5956 | /* Issue a warning for a bad initializer component. |
6cbbbc89 | 5957 | |
5958 | OPT is the OPT_W* value corresponding to the warning option that | |
1183a806 | 5959 | controls this warning. GMSGID identifies the message. The |
6cbbbc89 | 5960 | component name is taken from the spelling stack. */ |
1afefcf8 | 5961 | |
276beea2 | 5962 | static void |
1183a806 | 5963 | warning_init (int opt, const char *gmsgid) |
276beea2 | 5964 | { |
5965 | char *ofwhat; | |
fdcfba98 | 5966 | |
1183a806 | 5967 | /* The gmsgid may be a format string with %< and %>. */ |
5968 | warning (opt, gmsgid); | |
4fd61bc6 | 5969 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
276beea2 | 5970 | if (*ofwhat) |
6cbbbc89 | 5971 | warning (opt, "(near initialization for %qs)", ofwhat); |
276beea2 | 5972 | } |
5973 | \f | |
3ad5f027 | 5974 | /* If TYPE is an array type and EXPR is a parenthesized string |
5975 | constant, warn if pedantic that EXPR is being used to initialize an | |
5976 | object of type TYPE. */ | |
5977 | ||
5978 | void | |
5979 | maybe_warn_string_init (tree type, struct c_expr expr) | |
5980 | { | |
5981 | if (pedantic | |
5982 | && TREE_CODE (type) == ARRAY_TYPE | |
5983 | && TREE_CODE (expr.value) == STRING_CST | |
5984 | && expr.original_code != STRING_CST) | |
29438999 | 5985 | pedwarn_init (input_location, OPT_Wpedantic, |
8864917d | 5986 | "array initialized from parenthesized string constant"); |
3ad5f027 | 5987 | } |
5988 | ||
276beea2 | 5989 | /* Digest the parser output INIT as an initializer for type TYPE. |
5990 | Return a C expression of type TYPE to represent the initial value. | |
fdcfba98 | 5991 | |
b9c74b4d | 5992 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
5993 | ||
a75b1c71 | 5994 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. |
5995 | ||
3ad5f027 | 5996 | If INIT is a string constant, STRICT_STRING is true if it is |
5997 | unparenthesized or we should not warn here for it being parenthesized. | |
5998 | For other types of INIT, STRICT_STRING is not used. | |
5999 | ||
e60a6f7b | 6000 | INIT_LOC is the location of the INIT. |
6001 | ||
276beea2 | 6002 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
6003 | elements are seen. */ | |
fdcfba98 | 6004 | |
276beea2 | 6005 | static tree |
e60a6f7b | 6006 | digest_init (location_t init_loc, tree type, tree init, tree origtype, |
6007 | bool null_pointer_constant, bool strict_string, | |
6008 | int require_constant) | |
276beea2 | 6009 | { |
6010 | enum tree_code code = TREE_CODE (type); | |
6011 | tree inside_init = init; | |
c6418a4e | 6012 | tree semantic_type = NULL_TREE; |
a75b1c71 | 6013 | bool maybe_const = true; |
fdcfba98 | 6014 | |
276beea2 | 6015 | if (type == error_mark_node |
b5a68e6b | 6016 | || !init |
276beea2 | 6017 | || init == error_mark_node |
6018 | || TREE_TYPE (init) == error_mark_node) | |
6019 | return error_mark_node; | |
fdcfba98 | 6020 | |
a97c952c | 6021 | STRIP_TYPE_NOPS (inside_init); |
fdcfba98 | 6022 | |
c6418a4e | 6023 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) |
6024 | { | |
6025 | semantic_type = TREE_TYPE (inside_init); | |
6026 | inside_init = TREE_OPERAND (inside_init, 0); | |
6027 | } | |
a75b1c71 | 6028 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); |
6029 | inside_init = decl_constant_value_for_optimization (inside_init); | |
fdcfba98 | 6030 | |
276beea2 | 6031 | /* Initialization of an array of chars from a string constant |
6032 | optionally enclosed in braces. */ | |
fdcfba98 | 6033 | |
e91a0d39 | 6034 | if (code == ARRAY_TYPE && inside_init |
6035 | && TREE_CODE (inside_init) == STRING_CST) | |
276beea2 | 6036 | { |
6037 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
e91a0d39 | 6038 | /* Note that an array could be both an array of character type |
6039 | and an array of wchar_t if wchar_t is signed char or unsigned | |
6040 | char. */ | |
6041 | bool char_array = (typ1 == char_type_node | |
6042 | || typ1 == signed_char_type_node | |
6043 | || typ1 == unsigned_char_type_node); | |
6044 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
b0726616 | 6045 | bool char16_array = !!comptypes (typ1, char16_type_node); |
6046 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
6047 | ||
6048 | if (char_array || wchar_array || char16_array || char32_array) | |
fdcfba98 | 6049 | { |
3ad5f027 | 6050 | struct c_expr expr; |
b0726616 | 6051 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); |
3ad5f027 | 6052 | expr.value = inside_init; |
6053 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
841fdaa6 | 6054 | expr.original_type = NULL; |
3ad5f027 | 6055 | maybe_warn_string_init (type, expr); |
6056 | ||
fe49e588 | 6057 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
29438999 | 6058 | pedwarn_init (init_loc, OPT_Wpedantic, |
fe49e588 | 6059 | "initialization of a flexible array member"); |
6060 | ||
276beea2 | 6061 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
3ed275a6 | 6062 | TYPE_MAIN_VARIANT (type))) |
276beea2 | 6063 | return inside_init; |
fdcfba98 | 6064 | |
b0726616 | 6065 | if (char_array) |
276beea2 | 6066 | { |
b0726616 | 6067 | if (typ2 != char_type_node) |
6068 | { | |
6069 | error_init ("char-array initialized from wide string"); | |
6070 | return error_mark_node; | |
6071 | } | |
276beea2 | 6072 | } |
b0726616 | 6073 | else |
276beea2 | 6074 | { |
b0726616 | 6075 | if (typ2 == char_type_node) |
6076 | { | |
6077 | error_init ("wide character array initialized from non-wide " | |
6078 | "string"); | |
6079 | return error_mark_node; | |
6080 | } | |
6081 | else if (!comptypes(typ1, typ2)) | |
6082 | { | |
6083 | error_init ("wide character array initialized from " | |
6084 | "incompatible wide string"); | |
6085 | return error_mark_node; | |
6086 | } | |
fdcfba98 | 6087 | } |
2b30d46c | 6088 | |
276beea2 | 6089 | TREE_TYPE (inside_init) = type; |
6090 | if (TYPE_DOMAIN (type) != 0 | |
6091 | && TYPE_SIZE (type) != 0 | |
019c559b | 6092 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
6093 | { | |
6094 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
6095 | ||
b0726616 | 6096 | /* Subtract the size of a single (possibly wide) character |
276beea2 | 6097 | because it's ok to ignore the terminating null char |
6098 | that is counted in the length of the constant. */ | |
019c559b | 6099 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), |
6100 | (len | |
6101 | - (TYPE_PRECISION (typ1) | |
6102 | / BITS_PER_UNIT)))) | |
6103 | pedwarn_init (init_loc, 0, | |
6104 | ("initializer-string for array of chars " | |
6105 | "is too long")); | |
6106 | else if (warn_cxx_compat | |
6107 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
6108 | warning_at (init_loc, OPT_Wc___compat, | |
6109 | ("initializer-string for array chars " | |
6110 | "is too long for C++")); | |
6111 | } | |
fdcfba98 | 6112 | |
276beea2 | 6113 | return inside_init; |
fdcfba98 | 6114 | } |
e91a0d39 | 6115 | else if (INTEGRAL_TYPE_P (typ1)) |
6116 | { | |
6117 | error_init ("array of inappropriate type initialized " | |
6118 | "from string constant"); | |
6119 | return error_mark_node; | |
6120 | } | |
fdcfba98 | 6121 | } |
6122 | ||
276beea2 | 6123 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
6124 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
6125 | below and handle as a constructor. */ | |
3a3c175c | 6126 | if (code == VECTOR_TYPE |
6127 | && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE | |
546c4794 | 6128 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) |
3a3c175c | 6129 | && TREE_CONSTANT (inside_init)) |
6130 | { | |
6131 | if (TREE_CODE (inside_init) == VECTOR_CST | |
6132 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6133 | TYPE_MAIN_VARIANT (type))) | |
6134 | return inside_init; | |
6135 | ||
6136 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
6137 | { | |
c75b4594 | 6138 | unsigned HOST_WIDE_INT ix; |
6139 | tree value; | |
6140 | bool constant_p = true; | |
3a3c175c | 6141 | |
6142 | /* Iterate through elements and check if all constructor | |
6143 | elements are *_CSTs. */ | |
c75b4594 | 6144 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) |
6145 | if (!CONSTANT_CLASS_P (value)) | |
6146 | { | |
6147 | constant_p = false; | |
6148 | break; | |
6149 | } | |
3a3c175c | 6150 | |
c75b4594 | 6151 | if (constant_p) |
6152 | return build_vector_from_ctor (type, | |
6153 | CONSTRUCTOR_ELTS (inside_init)); | |
3a3c175c | 6154 | } |
6155 | } | |
4b4dcf9c | 6156 | |
6513b50d | 6157 | if (warn_sequence_point) |
6158 | verify_sequence_points (inside_init); | |
6159 | ||
276beea2 | 6160 | /* Any type can be initialized |
6161 | from an expression of the same type, optionally with braces. */ | |
628fa4f8 | 6162 | |
276beea2 | 6163 | if (inside_init && TREE_TYPE (inside_init) != 0 |
6164 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
3ed275a6 | 6165 | TYPE_MAIN_VARIANT (type)) |
276beea2 | 6166 | || (code == ARRAY_TYPE |
3ed275a6 | 6167 | && comptypes (TREE_TYPE (inside_init), type)) |
276beea2 | 6168 | || (code == VECTOR_TYPE |
3ed275a6 | 6169 | && comptypes (TREE_TYPE (inside_init), type)) |
276beea2 | 6170 | || (code == POINTER_TYPE |
48b3d385 | 6171 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
276beea2 | 6172 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
3ed275a6 | 6173 | TREE_TYPE (type))))) |
276beea2 | 6174 | { |
6175 | if (code == POINTER_TYPE) | |
5cd106da | 6176 | { |
5cd106da | 6177 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) |
6178 | { | |
e78cf9a7 | 6179 | if (TREE_CODE (inside_init) == STRING_CST |
6180 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
e60a6f7b | 6181 | inside_init = array_to_pointer_conversion |
6182 | (init_loc, inside_init); | |
e78cf9a7 | 6183 | else |
6184 | { | |
6185 | error_init ("invalid use of non-lvalue array"); | |
6186 | return error_mark_node; | |
6187 | } | |
5cd106da | 6188 | } |
e78cf9a7 | 6189 | } |
5cd106da | 6190 | |
cd81f692 | 6191 | if (code == VECTOR_TYPE) |
6192 | /* Although the types are compatible, we may require a | |
6193 | conversion. */ | |
6194 | inside_init = convert (type, inside_init); | |
276beea2 | 6195 | |
b7d1b569 | 6196 | if (require_constant |
6197 | && (code == VECTOR_TYPE || !flag_isoc99) | |
276beea2 | 6198 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) |
628fa4f8 | 6199 | { |
276beea2 | 6200 | /* As an extension, allow initializing objects with static storage |
6201 | duration with compound literals (which are then treated just as | |
b7d1b569 | 6202 | the brace enclosed list they contain). Also allow this for |
6203 | vectors, as we can only assign them with compound literals. */ | |
276beea2 | 6204 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
6205 | inside_init = DECL_INITIAL (decl); | |
628fa4f8 | 6206 | } |
276beea2 | 6207 | |
6208 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
6209 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
628fa4f8 | 6210 | { |
276beea2 | 6211 | error_init ("array initialized from non-constant array expression"); |
6212 | return error_mark_node; | |
628fa4f8 | 6213 | } |
628fa4f8 | 6214 | |
29438999 | 6215 | /* Compound expressions can only occur here if -Wpedantic or |
276beea2 | 6216 | -pedantic-errors is specified. In the later case, we always want |
6217 | an error. In the former case, we simply want a warning. */ | |
6218 | if (require_constant && pedantic | |
6219 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
6220 | { | |
6221 | inside_init | |
6222 | = valid_compound_expr_initializer (inside_init, | |
6223 | TREE_TYPE (inside_init)); | |
6224 | if (inside_init == error_mark_node) | |
6225 | error_init ("initializer element is not constant"); | |
2b30d46c | 6226 | else |
29438999 | 6227 | pedwarn_init (init_loc, OPT_Wpedantic, |
21ca8540 | 6228 | "initializer element is not constant"); |
276beea2 | 6229 | if (flag_pedantic_errors) |
6230 | inside_init = error_mark_node; | |
6231 | } | |
6232 | else if (require_constant | |
fdf95cc9 | 6233 | && !initializer_constant_valid_p (inside_init, |
6234 | TREE_TYPE (inside_init))) | |
276beea2 | 6235 | { |
6236 | error_init ("initializer element is not constant"); | |
6237 | inside_init = error_mark_node; | |
ea5fb8f9 | 6238 | } |
a75b1c71 | 6239 | else if (require_constant && !maybe_const) |
e60a6f7b | 6240 | pedwarn_init (init_loc, 0, |
a75b1c71 | 6241 | "initializer element is not a constant expression"); |
78b80426 | 6242 | |
b86527d8 | 6243 | /* Added to enable additional -Wsuggest-attribute=format warnings. */ |
c7eca986 | 6244 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) |
e60a6f7b | 6245 | inside_init = convert_for_assignment (init_loc, type, inside_init, |
6246 | origtype, | |
b9c74b4d | 6247 | ic_init, null_pointer_constant, |
a75b1c71 | 6248 | NULL_TREE, NULL_TREE, 0); |
276beea2 | 6249 | return inside_init; |
6250 | } | |
78b80426 | 6251 | |
276beea2 | 6252 | /* Handle scalar types, including conversions. */ |
628fa4f8 | 6253 | |
9421ebb9 | 6254 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE |
6255 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
6256 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
628fa4f8 | 6257 | { |
e78cf9a7 | 6258 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE |
6259 | && (TREE_CODE (init) == STRING_CST | |
6260 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
e60a6f7b | 6261 | inside_init = init = array_to_pointer_conversion (init_loc, init); |
c6418a4e | 6262 | if (semantic_type) |
6263 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
6264 | inside_init); | |
276beea2 | 6265 | inside_init |
e60a6f7b | 6266 | = convert_for_assignment (init_loc, type, inside_init, origtype, |
6267 | ic_init, null_pointer_constant, | |
276beea2 | 6268 | NULL_TREE, NULL_TREE, 0); |
2b30d46c | 6269 | |
0d45e76f | 6270 | /* Check to see if we have already given an error message. */ |
6271 | if (inside_init == error_mark_node) | |
6272 | ; | |
84166705 | 6273 | else if (require_constant && !TREE_CONSTANT (inside_init)) |
628fa4f8 | 6274 | { |
276beea2 | 6275 | error_init ("initializer element is not constant"); |
6276 | inside_init = error_mark_node; | |
628fa4f8 | 6277 | } |
276beea2 | 6278 | else if (require_constant |
fdf95cc9 | 6279 | && !initializer_constant_valid_p (inside_init, |
6280 | TREE_TYPE (inside_init))) | |
628fa4f8 | 6281 | { |
276beea2 | 6282 | error_init ("initializer element is not computable at load time"); |
6283 | inside_init = error_mark_node; | |
628fa4f8 | 6284 | } |
a75b1c71 | 6285 | else if (require_constant && !maybe_const) |
e60a6f7b | 6286 | pedwarn_init (init_loc, 0, |
a75b1c71 | 6287 | "initializer element is not a constant expression"); |
276beea2 | 6288 | |
6289 | return inside_init; | |
628fa4f8 | 6290 | } |
bb94852b | 6291 | |
276beea2 | 6292 | /* Come here only for records and arrays. */ |
bb94852b | 6293 | |
276beea2 | 6294 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
bb94852b | 6295 | { |
276beea2 | 6296 | error_init ("variable-sized object may not be initialized"); |
6297 | return error_mark_node; | |
bb94852b | 6298 | } |
276beea2 | 6299 | |
6300 | error_init ("invalid initializer"); | |
6301 | return error_mark_node; | |
bb94852b | 6302 | } |
628fa4f8 | 6303 | \f |
276beea2 | 6304 | /* Handle initializers that use braces. */ |
628fa4f8 | 6305 | |
276beea2 | 6306 | /* Type of object we are accumulating a constructor for. |
6307 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
6308 | static tree constructor_type; | |
628fa4f8 | 6309 | |
276beea2 | 6310 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
6311 | left to fill. */ | |
6312 | static tree constructor_fields; | |
628fa4f8 | 6313 | |
276beea2 | 6314 | /* For an ARRAY_TYPE, this is the specified index |
6315 | at which to store the next element we get. */ | |
6316 | static tree constructor_index; | |
628fa4f8 | 6317 | |
276beea2 | 6318 | /* For an ARRAY_TYPE, this is the maximum index. */ |
6319 | static tree constructor_max_index; | |
628fa4f8 | 6320 | |
276beea2 | 6321 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
6322 | static tree constructor_unfilled_fields; | |
628fa4f8 | 6323 | |
276beea2 | 6324 | /* For an ARRAY_TYPE, this is the index of the first element |
6325 | not yet written out. */ | |
6326 | static tree constructor_unfilled_index; | |
3a77db2f | 6327 | |
276beea2 | 6328 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
6329 | This is so we can generate gaps between fields, when appropriate. */ | |
6330 | static tree constructor_bit_index; | |
550e465e | 6331 | |
276beea2 | 6332 | /* If we are saving up the elements rather than allocating them, |
6333 | this is the list of elements so far (in reverse order, | |
6334 | most recent first). */ | |
c75b4594 | 6335 | static VEC(constructor_elt,gc) *constructor_elements; |
02643ebc | 6336 | |
276beea2 | 6337 | /* 1 if constructor should be incrementally stored into a constructor chain, |
6338 | 0 if all the elements should be kept in AVL tree. */ | |
6339 | static int constructor_incremental; | |
02643ebc | 6340 | |
276beea2 | 6341 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
6342 | static int constructor_constant; | |
02643ebc | 6343 | |
276beea2 | 6344 | /* 1 if so far this constructor's elements are all valid address constants. */ |
6345 | static int constructor_simple; | |
02643ebc | 6346 | |
a75b1c71 | 6347 | /* 1 if this constructor has an element that cannot be part of a |
6348 | constant expression. */ | |
6349 | static int constructor_nonconst; | |
6350 | ||
276beea2 | 6351 | /* 1 if this constructor is erroneous so far. */ |
6352 | static int constructor_erroneous; | |
f9e15121 | 6353 | |
276beea2 | 6354 | /* Structure for managing pending initializer elements, organized as an |
6355 | AVL tree. */ | |
f9e15121 | 6356 | |
276beea2 | 6357 | struct init_node |
f9e15121 | 6358 | { |
276beea2 | 6359 | struct init_node *left, *right; |
6360 | struct init_node *parent; | |
6361 | int balance; | |
6362 | tree purpose; | |
6363 | tree value; | |
b9c74b4d | 6364 | tree origtype; |
f9e15121 | 6365 | }; |
6366 | ||
276beea2 | 6367 | /* Tree of pending elements at this constructor level. |
6368 | These are elements encountered out of order | |
6369 | which belong at places we haven't reached yet in actually | |
6370 | writing the output. | |
6371 | Will never hold tree nodes across GC runs. */ | |
6372 | static struct init_node *constructor_pending_elts; | |
f9e15121 | 6373 | |
276beea2 | 6374 | /* The SPELLING_DEPTH of this constructor. */ |
6375 | static int constructor_depth; | |
f9e15121 | 6376 | |
276beea2 | 6377 | /* DECL node for which an initializer is being read. |
6378 | 0 means we are reading a constructor expression | |
6379 | such as (struct foo) {...}. */ | |
6380 | static tree constructor_decl; | |
f9e15121 | 6381 | |
276beea2 | 6382 | /* Nonzero if this is an initializer for a top-level decl. */ |
6383 | static int constructor_top_level; | |
f9e15121 | 6384 | |
276beea2 | 6385 | /* Nonzero if there were any member designators in this initializer. */ |
6386 | static int constructor_designated; | |
f9e15121 | 6387 | |
276beea2 | 6388 | /* Nesting depth of designator list. */ |
6389 | static int designator_depth; | |
f9e15121 | 6390 | |
276beea2 | 6391 | /* Nonzero if there were diagnosed errors in this designator list. */ |
ebe39827 | 6392 | static int designator_erroneous; |
f9e15121 | 6393 | |
276beea2 | 6394 | \f |
6395 | /* This stack has a level for each implicit or explicit level of | |
6396 | structuring in the initializer, including the outermost one. It | |
6397 | saves the values of most of the variables above. */ | |
f9e15121 | 6398 | |
276beea2 | 6399 | struct constructor_range_stack; |
6400 | ||
6401 | struct constructor_stack | |
f9e15121 | 6402 | { |
276beea2 | 6403 | struct constructor_stack *next; |
6404 | tree type; | |
6405 | tree fields; | |
6406 | tree index; | |
6407 | tree max_index; | |
6408 | tree unfilled_index; | |
6409 | tree unfilled_fields; | |
6410 | tree bit_index; | |
c75b4594 | 6411 | VEC(constructor_elt,gc) *elements; |
276beea2 | 6412 | struct init_node *pending_elts; |
6413 | int offset; | |
6414 | int depth; | |
3ad5f027 | 6415 | /* If value nonzero, this value should replace the entire |
276beea2 | 6416 | constructor at this level. */ |
3ad5f027 | 6417 | struct c_expr replacement_value; |
276beea2 | 6418 | struct constructor_range_stack *range_stack; |
6419 | char constant; | |
6420 | char simple; | |
a75b1c71 | 6421 | char nonconst; |
276beea2 | 6422 | char implicit; |
6423 | char erroneous; | |
6424 | char outer; | |
6425 | char incremental; | |
6426 | char designated; | |
6427 | }; | |
f9e15121 | 6428 | |
4aa31b78 | 6429 | static struct constructor_stack *constructor_stack; |
f9e15121 | 6430 | |
276beea2 | 6431 | /* This stack represents designators from some range designator up to |
6432 | the last designator in the list. */ | |
f9e15121 | 6433 | |
276beea2 | 6434 | struct constructor_range_stack |
6435 | { | |
6436 | struct constructor_range_stack *next, *prev; | |
6437 | struct constructor_stack *stack; | |
6438 | tree range_start; | |
6439 | tree index; | |
6440 | tree range_end; | |
6441 | tree fields; | |
6442 | }; | |
f9e15121 | 6443 | |
4aa31b78 | 6444 | static struct constructor_range_stack *constructor_range_stack; |
f9e15121 | 6445 | |
276beea2 | 6446 | /* This stack records separate initializers that are nested. |
6447 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
6448 | in cases like { ... (struct foo) { ... } ... }. */ | |
f9e15121 | 6449 | |
276beea2 | 6450 | struct initializer_stack |
f9e15121 | 6451 | { |
276beea2 | 6452 | struct initializer_stack *next; |
6453 | tree decl; | |
276beea2 | 6454 | struct constructor_stack *constructor_stack; |
6455 | struct constructor_range_stack *constructor_range_stack; | |
c75b4594 | 6456 | VEC(constructor_elt,gc) *elements; |
276beea2 | 6457 | struct spelling *spelling; |
6458 | struct spelling *spelling_base; | |
6459 | int spelling_size; | |
6460 | char top_level; | |
6461 | char require_constant_value; | |
6462 | char require_constant_elements; | |
6463 | }; | |
f9e15121 | 6464 | |
4aa31b78 | 6465 | static struct initializer_stack *initializer_stack; |
276beea2 | 6466 | \f |
6467 | /* Prepare to parse and output the initializer for variable DECL. */ | |
628fa4f8 | 6468 | |
6469 | void | |
d5406300 | 6470 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) |
628fa4f8 | 6471 | { |
276beea2 | 6472 | const char *locus; |
a9c6c0e3 | 6473 | struct initializer_stack *p = XNEW (struct initializer_stack); |
628fa4f8 | 6474 | |
276beea2 | 6475 | p->decl = constructor_decl; |
276beea2 | 6476 | p->require_constant_value = require_constant_value; |
6477 | p->require_constant_elements = require_constant_elements; | |
6478 | p->constructor_stack = constructor_stack; | |
6479 | p->constructor_range_stack = constructor_range_stack; | |
6480 | p->elements = constructor_elements; | |
6481 | p->spelling = spelling; | |
6482 | p->spelling_base = spelling_base; | |
6483 | p->spelling_size = spelling_size; | |
6484 | p->top_level = constructor_top_level; | |
6485 | p->next = initializer_stack; | |
6486 | initializer_stack = p; | |
628fa4f8 | 6487 | |
276beea2 | 6488 | constructor_decl = decl; |
276beea2 | 6489 | constructor_designated = 0; |
6490 | constructor_top_level = top_level; | |
628fa4f8 | 6491 | |
61ba37bf | 6492 | if (decl != 0 && decl != error_mark_node) |
276beea2 | 6493 | { |
6494 | require_constant_value = TREE_STATIC (decl); | |
6495 | require_constant_elements | |
6496 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
6497 | /* For a scalar, you can always use any value to initialize, | |
6498 | even within braces. */ | |
6499 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
6500 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
6501 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
6502 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
d1dd9ac0 | 6503 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); |
276beea2 | 6504 | } |
6505 | else | |
6506 | { | |
6507 | require_constant_value = 0; | |
6508 | require_constant_elements = 0; | |
d1dd9ac0 | 6509 | locus = _("(anonymous)"); |
276beea2 | 6510 | } |
89a5f6d1 | 6511 | |
276beea2 | 6512 | constructor_stack = 0; |
6513 | constructor_range_stack = 0; | |
89a5f6d1 | 6514 | |
276beea2 | 6515 | missing_braces_mentioned = 0; |
6516 | ||
6517 | spelling_base = 0; | |
6518 | spelling_size = 0; | |
6519 | RESTORE_SPELLING_DEPTH (0); | |
6520 | ||
6521 | if (locus) | |
6522 | push_string (locus); | |
6523 | } | |
6524 | ||
6525 | void | |
6526 | finish_init (void) | |
89a5f6d1 | 6527 | { |
276beea2 | 6528 | struct initializer_stack *p = initializer_stack; |
89a5f6d1 | 6529 | |
276beea2 | 6530 | /* Free the whole constructor stack of this initializer. */ |
6531 | while (constructor_stack) | |
6532 | { | |
6533 | struct constructor_stack *q = constructor_stack; | |
6534 | constructor_stack = q->next; | |
6535 | free (q); | |
6536 | } | |
6537 | ||
231bd014 | 6538 | gcc_assert (!constructor_range_stack); |
276beea2 | 6539 | |
6540 | /* Pop back to the data of the outer initializer (if any). */ | |
dcd8fd01 | 6541 | free (spelling_base); |
a5d9b222 | 6542 | |
276beea2 | 6543 | constructor_decl = p->decl; |
276beea2 | 6544 | require_constant_value = p->require_constant_value; |
6545 | require_constant_elements = p->require_constant_elements; | |
6546 | constructor_stack = p->constructor_stack; | |
6547 | constructor_range_stack = p->constructor_range_stack; | |
6548 | constructor_elements = p->elements; | |
6549 | spelling = p->spelling; | |
6550 | spelling_base = p->spelling_base; | |
6551 | spelling_size = p->spelling_size; | |
6552 | constructor_top_level = p->top_level; | |
6553 | initializer_stack = p->next; | |
6554 | free (p); | |
89a5f6d1 | 6555 | } |
628fa4f8 | 6556 | \f |
276beea2 | 6557 | /* Call here when we see the initializer is surrounded by braces. |
6558 | This is instead of a call to push_init_level; | |
6559 | it is matched by a call to pop_init_level. | |
628fa4f8 | 6560 | |
276beea2 | 6561 | TYPE is the type to initialize, for a constructor expression. |
6562 | For an initializer for a decl, TYPE is zero. */ | |
628fa4f8 | 6563 | |
276beea2 | 6564 | void |
6565 | really_start_incremental_init (tree type) | |
628fa4f8 | 6566 | { |
9318f22c | 6567 | struct constructor_stack *p = XNEW (struct constructor_stack); |
628fa4f8 | 6568 | |
276beea2 | 6569 | if (type == 0) |
6570 | type = TREE_TYPE (constructor_decl); | |
628fa4f8 | 6571 | |
8d125f7d | 6572 | if (TREE_CODE (type) == VECTOR_TYPE |
6573 | && TYPE_VECTOR_OPAQUE (type)) | |
276beea2 | 6574 | error ("opaque vector types cannot be initialized"); |
628fa4f8 | 6575 | |
276beea2 | 6576 | p->type = constructor_type; |
6577 | p->fields = constructor_fields; | |
6578 | p->index = constructor_index; | |
6579 | p->max_index = constructor_max_index; | |
6580 | p->unfilled_index = constructor_unfilled_index; | |
6581 | p->unfilled_fields = constructor_unfilled_fields; | |
6582 | p->bit_index = constructor_bit_index; | |
6583 | p->elements = constructor_elements; | |
6584 | p->constant = constructor_constant; | |
6585 | p->simple = constructor_simple; | |
a75b1c71 | 6586 | p->nonconst = constructor_nonconst; |
276beea2 | 6587 | p->erroneous = constructor_erroneous; |
6588 | p->pending_elts = constructor_pending_elts; | |
6589 | p->depth = constructor_depth; | |
3ad5f027 | 6590 | p->replacement_value.value = 0; |
6591 | p->replacement_value.original_code = ERROR_MARK; | |
841fdaa6 | 6592 | p->replacement_value.original_type = NULL; |
276beea2 | 6593 | p->implicit = 0; |
6594 | p->range_stack = 0; | |
6595 | p->outer = 0; | |
6596 | p->incremental = constructor_incremental; | |
6597 | p->designated = constructor_designated; | |
6598 | p->next = 0; | |
6599 | constructor_stack = p; | |
189495e1 | 6600 | |
276beea2 | 6601 | constructor_constant = 1; |
6602 | constructor_simple = 1; | |
a75b1c71 | 6603 | constructor_nonconst = 0; |
276beea2 | 6604 | constructor_depth = SPELLING_DEPTH (); |
6605 | constructor_elements = 0; | |
6606 | constructor_pending_elts = 0; | |
6607 | constructor_type = type; | |
6608 | constructor_incremental = 1; | |
6609 | constructor_designated = 0; | |
6610 | designator_depth = 0; | |
ebe39827 | 6611 | designator_erroneous = 0; |
628fa4f8 | 6612 | |
276beea2 | 6613 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6614 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
628fa4f8 | 6615 | { |
276beea2 | 6616 | constructor_fields = TYPE_FIELDS (constructor_type); |
6617 | /* Skip any nameless bit fields at the beginning. */ | |
6618 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6619 | && DECL_NAME (constructor_fields) == 0) | |
1767a056 | 6620 | constructor_fields = DECL_CHAIN (constructor_fields); |
a0c2c45b | 6621 | |
276beea2 | 6622 | constructor_unfilled_fields = constructor_fields; |
6623 | constructor_bit_index = bitsize_zero_node; | |
628fa4f8 | 6624 | } |
276beea2 | 6625 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
6626 | { | |
6627 | if (TYPE_DOMAIN (constructor_type)) | |
6628 | { | |
6629 | constructor_max_index | |
6630 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
628fa4f8 | 6631 | |
276beea2 | 6632 | /* Detect non-empty initializations of zero-length arrays. */ |
6633 | if (constructor_max_index == NULL_TREE | |
6634 | && TYPE_SIZE (constructor_type)) | |
2512209b | 6635 | constructor_max_index = integer_minus_one_node; |
628fa4f8 | 6636 | |
276beea2 | 6637 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
6638 | to initialize VLAs will cause a proper error; avoid tree | |
6639 | checking errors as well by setting a safe value. */ | |
6640 | if (constructor_max_index | |
6641 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
2512209b | 6642 | constructor_max_index = integer_minus_one_node; |
4619fc15 | 6643 | |
276beea2 | 6644 | constructor_index |
6645 | = convert (bitsizetype, | |
6646 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
4619fc15 | 6647 | } |
276beea2 | 6648 | else |
fb213c85 | 6649 | { |
6650 | constructor_index = bitsize_zero_node; | |
6651 | constructor_max_index = NULL_TREE; | |
6652 | } | |
4619fc15 | 6653 | |
276beea2 | 6654 | constructor_unfilled_index = constructor_index; |
6655 | } | |
6656 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6657 | { | |
6658 | /* Vectors are like simple fixed-size arrays. */ | |
6659 | constructor_max_index = | |
ceb7b692 | 6660 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
4aeeff51 | 6661 | constructor_index = bitsize_zero_node; |
276beea2 | 6662 | constructor_unfilled_index = constructor_index; |
6663 | } | |
6664 | else | |
6665 | { | |
6666 | /* Handle the case of int x = {5}; */ | |
6667 | constructor_fields = constructor_type; | |
6668 | constructor_unfilled_fields = constructor_type; | |
6669 | } | |
6670 | } | |
6671 | \f | |
6672 | /* Push down into a subobject, for initialization. | |
6673 | If this is for an explicit set of braces, IMPLICIT is 0. | |
6674 | If it is because the next element belongs at a lower level, | |
6675 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
628fa4f8 | 6676 | |
276beea2 | 6677 | void |
759791ee | 6678 | push_init_level (int implicit, struct obstack * braced_init_obstack) |
276beea2 | 6679 | { |
6680 | struct constructor_stack *p; | |
6681 | tree value = NULL_TREE; | |
628fa4f8 | 6682 | |
276beea2 | 6683 | /* If we've exhausted any levels that didn't have braces, |
411bd122 | 6684 | pop them now. If implicit == 1, this will have been done in |
6685 | process_init_element; do not repeat it here because in the case | |
6686 | of excess initializers for an empty aggregate this leads to an | |
6687 | infinite cycle of popping a level and immediately recreating | |
6688 | it. */ | |
6689 | if (implicit != 1) | |
276beea2 | 6690 | { |
411bd122 | 6691 | while (constructor_stack->implicit) |
6692 | { | |
6693 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6694 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6695 | && constructor_fields == 0) | |
759791ee | 6696 | process_init_element (pop_init_level (1, braced_init_obstack), |
6697 | true, braced_init_obstack); | |
411bd122 | 6698 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE |
6699 | && constructor_max_index | |
6700 | && tree_int_cst_lt (constructor_max_index, | |
6701 | constructor_index)) | |
759791ee | 6702 | process_init_element (pop_init_level (1, braced_init_obstack), |
6703 | true, braced_init_obstack); | |
411bd122 | 6704 | else |
6705 | break; | |
6706 | } | |
276beea2 | 6707 | } |
628fa4f8 | 6708 | |
276beea2 | 6709 | /* Unless this is an explicit brace, we need to preserve previous |
6710 | content if any. */ | |
6711 | if (implicit) | |
6712 | { | |
6713 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6714 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6715 | && constructor_fields) | |
759791ee | 6716 | value = find_init_member (constructor_fields, braced_init_obstack); |
276beea2 | 6717 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
759791ee | 6718 | value = find_init_member (constructor_index, braced_init_obstack); |
628fa4f8 | 6719 | } |
6720 | ||
9318f22c | 6721 | p = XNEW (struct constructor_stack); |
276beea2 | 6722 | p->type = constructor_type; |
6723 | p->fields = constructor_fields; | |
6724 | p->index = constructor_index; | |
6725 | p->max_index = constructor_max_index; | |
6726 | p->unfilled_index = constructor_unfilled_index; | |
6727 | p->unfilled_fields = constructor_unfilled_fields; | |
6728 | p->bit_index = constructor_bit_index; | |
6729 | p->elements = constructor_elements; | |
6730 | p->constant = constructor_constant; | |
6731 | p->simple = constructor_simple; | |
a75b1c71 | 6732 | p->nonconst = constructor_nonconst; |
276beea2 | 6733 | p->erroneous = constructor_erroneous; |
6734 | p->pending_elts = constructor_pending_elts; | |
6735 | p->depth = constructor_depth; | |
3ad5f027 | 6736 | p->replacement_value.value = 0; |
6737 | p->replacement_value.original_code = ERROR_MARK; | |
841fdaa6 | 6738 | p->replacement_value.original_type = NULL; |
276beea2 | 6739 | p->implicit = implicit; |
6740 | p->outer = 0; | |
6741 | p->incremental = constructor_incremental; | |
6742 | p->designated = constructor_designated; | |
6743 | p->next = constructor_stack; | |
6744 | p->range_stack = 0; | |
6745 | constructor_stack = p; | |
628fa4f8 | 6746 | |
276beea2 | 6747 | constructor_constant = 1; |
6748 | constructor_simple = 1; | |
a75b1c71 | 6749 | constructor_nonconst = 0; |
276beea2 | 6750 | constructor_depth = SPELLING_DEPTH (); |
6751 | constructor_elements = 0; | |
6752 | constructor_incremental = 1; | |
6753 | constructor_designated = 0; | |
6754 | constructor_pending_elts = 0; | |
6755 | if (!implicit) | |
628fa4f8 | 6756 | { |
276beea2 | 6757 | p->range_stack = constructor_range_stack; |
6758 | constructor_range_stack = 0; | |
6759 | designator_depth = 0; | |
ebe39827 | 6760 | designator_erroneous = 0; |
276beea2 | 6761 | } |
628fa4f8 | 6762 | |
276beea2 | 6763 | /* Don't die if an entire brace-pair level is superfluous |
6764 | in the containing level. */ | |
6765 | if (constructor_type == 0) | |
6766 | ; | |
6767 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6768 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6769 | { | |
6770 | /* Don't die if there are extra init elts at the end. */ | |
6771 | if (constructor_fields == 0) | |
6772 | constructor_type = 0; | |
6773 | else | |
628fa4f8 | 6774 | { |
276beea2 | 6775 | constructor_type = TREE_TYPE (constructor_fields); |
6776 | push_member_name (constructor_fields); | |
6777 | constructor_depth++; | |
628fa4f8 | 6778 | } |
276beea2 | 6779 | } |
6780 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6781 | { | |
6782 | constructor_type = TREE_TYPE (constructor_type); | |
4aeeff51 | 6783 | push_array_bounds (tree_low_cst (constructor_index, 1)); |
276beea2 | 6784 | constructor_depth++; |
628fa4f8 | 6785 | } |
6786 | ||
276beea2 | 6787 | if (constructor_type == 0) |
628fa4f8 | 6788 | { |
276beea2 | 6789 | error_init ("extra brace group at end of initializer"); |
6790 | constructor_fields = 0; | |
6791 | constructor_unfilled_fields = 0; | |
6792 | return; | |
628fa4f8 | 6793 | } |
6794 | ||
276beea2 | 6795 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
6796 | { | |
6797 | constructor_constant = TREE_CONSTANT (value); | |
6798 | constructor_simple = TREE_STATIC (value); | |
a75b1c71 | 6799 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); |
276beea2 | 6800 | constructor_elements = CONSTRUCTOR_ELTS (value); |
c75b4594 | 6801 | if (!VEC_empty (constructor_elt, constructor_elements) |
276beea2 | 6802 | && (TREE_CODE (constructor_type) == RECORD_TYPE |
6803 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
759791ee | 6804 | set_nonincremental_init (braced_init_obstack); |
276beea2 | 6805 | } |
628fa4f8 | 6806 | |
276beea2 | 6807 | if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) |
6808 | { | |
6809 | missing_braces_mentioned = 1; | |
6cbbbc89 | 6810 | warning_init (OPT_Wmissing_braces, "missing braces around initializer"); |
276beea2 | 6811 | } |
628fa4f8 | 6812 | |
276beea2 | 6813 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6814 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6815 | { | |
6816 | constructor_fields = TYPE_FIELDS (constructor_type); | |
6817 | /* Skip any nameless bit fields at the beginning. */ | |
6818 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6819 | && DECL_NAME (constructor_fields) == 0) | |
d059ef79 | 6820 | constructor_fields = DECL_CHAIN (constructor_fields); |
7de1d3ba | 6821 | |
276beea2 | 6822 | constructor_unfilled_fields = constructor_fields; |
6823 | constructor_bit_index = bitsize_zero_node; | |
6824 | } | |
6825 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6826 | { | |
6827 | /* Vectors are like simple fixed-size arrays. */ | |
6828 | constructor_max_index = | |
ceb7b692 | 6829 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
6830 | constructor_index = bitsize_int (0); | |
276beea2 | 6831 | constructor_unfilled_index = constructor_index; |
6832 | } | |
6833 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6834 | { | |
6835 | if (TYPE_DOMAIN (constructor_type)) | |
6836 | { | |
6837 | constructor_max_index | |
6838 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
628fa4f8 | 6839 | |
276beea2 | 6840 | /* Detect non-empty initializations of zero-length arrays. */ |
6841 | if (constructor_max_index == NULL_TREE | |
6842 | && TYPE_SIZE (constructor_type)) | |
2512209b | 6843 | constructor_max_index = integer_minus_one_node; |
fc698c9d | 6844 | |
276beea2 | 6845 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
6846 | to initialize VLAs will cause a proper error; avoid tree | |
6847 | checking errors as well by setting a safe value. */ | |
6848 | if (constructor_max_index | |
6849 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
2512209b | 6850 | constructor_max_index = integer_minus_one_node; |
554732cd | 6851 | |
276beea2 | 6852 | constructor_index |
6853 | = convert (bitsizetype, | |
6854 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
6855 | } | |
6856 | else | |
6857 | constructor_index = bitsize_zero_node; | |
fc698c9d | 6858 | |
276beea2 | 6859 | constructor_unfilled_index = constructor_index; |
6860 | if (value && TREE_CODE (value) == STRING_CST) | |
6861 | { | |
6862 | /* We need to split the char/wchar array into individual | |
6863 | characters, so that we don't have to special case it | |
6864 | everywhere. */ | |
759791ee | 6865 | set_nonincremental_init_from_string (value, braced_init_obstack); |
276beea2 | 6866 | } |
6867 | } | |
6868 | else | |
6869 | { | |
b765c734 | 6870 | if (constructor_type != error_mark_node) |
6cbbbc89 | 6871 | warning_init (0, "braces around scalar initializer"); |
276beea2 | 6872 | constructor_fields = constructor_type; |
6873 | constructor_unfilled_fields = constructor_type; | |
6874 | } | |
6875 | } | |
989bbcfc | 6876 | |
276beea2 | 6877 | /* At the end of an implicit or explicit brace level, |
3ad5f027 | 6878 | finish up that level of constructor. If a single expression |
6879 | with redundant braces initialized that level, return the | |
6880 | c_expr structure for that expression. Otherwise, the original_code | |
6881 | element is set to ERROR_MARK. | |
6882 | If we were outputting the elements as they are read, return 0 as the value | |
276beea2 | 6883 | from inner levels (process_init_element ignores that), |
3ad5f027 | 6884 | but return error_mark_node as the value from the outermost level |
276beea2 | 6885 | (that's what we want to put in DECL_INITIAL). |
3ad5f027 | 6886 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
fc698c9d | 6887 | |
3ad5f027 | 6888 | struct c_expr |
759791ee | 6889 | pop_init_level (int implicit, struct obstack * braced_init_obstack) |
276beea2 | 6890 | { |
6891 | struct constructor_stack *p; | |
3ad5f027 | 6892 | struct c_expr ret; |
6893 | ret.value = 0; | |
6894 | ret.original_code = ERROR_MARK; | |
841fdaa6 | 6895 | ret.original_type = NULL; |
fc698c9d | 6896 | |
276beea2 | 6897 | if (implicit == 0) |
6898 | { | |
6899 | /* When we come to an explicit close brace, | |
6900 | pop any inner levels that didn't have explicit braces. */ | |
6901 | while (constructor_stack->implicit) | |
759791ee | 6902 | { |
6903 | process_init_element (pop_init_level (1, braced_init_obstack), | |
6904 | true, braced_init_obstack); | |
6905 | } | |
231bd014 | 6906 | gcc_assert (!constructor_range_stack); |
276beea2 | 6907 | } |
997d68fe | 6908 | |
36e0475c | 6909 | /* Now output all pending elements. */ |
6910 | constructor_incremental = 1; | |
759791ee | 6911 | output_pending_init_elements (1, braced_init_obstack); |
36e0475c | 6912 | |
276beea2 | 6913 | p = constructor_stack; |
997d68fe | 6914 | |
276beea2 | 6915 | /* Error for initializing a flexible array member, or a zero-length |
6916 | array member in an inappropriate context. */ | |
6917 | if (constructor_type && constructor_fields | |
6918 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
6919 | && TYPE_DOMAIN (constructor_type) | |
84166705 | 6920 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
276beea2 | 6921 | { |
6922 | /* Silently discard empty initializations. The parser will | |
6923 | already have pedwarned for empty brackets. */ | |
6924 | if (integer_zerop (constructor_unfilled_index)) | |
6925 | constructor_type = NULL_TREE; | |
231bd014 | 6926 | else |
276beea2 | 6927 | { |
231bd014 | 6928 | gcc_assert (!TYPE_SIZE (constructor_type)); |
a0c938f0 | 6929 | |
276beea2 | 6930 | if (constructor_depth > 2) |
6931 | error_init ("initialization of flexible array member in a nested context"); | |
8864917d | 6932 | else |
29438999 | 6933 | pedwarn_init (input_location, OPT_Wpedantic, |
21ca8540 | 6934 | "initialization of a flexible array member"); |
fc698c9d | 6935 | |
276beea2 | 6936 | /* We have already issued an error message for the existence |
6937 | of a flexible array member not at the end of the structure. | |
89f18f73 | 6938 | Discard the initializer so that we do not die later. */ |
1767a056 | 6939 | if (DECL_CHAIN (constructor_fields) != NULL_TREE) |
276beea2 | 6940 | constructor_type = NULL_TREE; |
6941 | } | |
276beea2 | 6942 | } |
fc698c9d | 6943 | |
276beea2 | 6944 | /* Warn when some struct elements are implicitly initialized to zero. */ |
3304fb1c | 6945 | if (warn_missing_field_initializers |
276beea2 | 6946 | && constructor_type |
6947 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
6948 | && constructor_unfilled_fields) | |
6949 | { | |
ac4e675f | 6950 | bool constructor_zeroinit = |
6951 | (VEC_length (constructor_elt, constructor_elements) == 1 | |
6952 | && integer_zerop | |
6953 | (VEC_index (constructor_elt, constructor_elements, 0)->value)); | |
6954 | ||
276beea2 | 6955 | /* Do not warn for flexible array members or zero-length arrays. */ |
6956 | while (constructor_unfilled_fields | |
84166705 | 6957 | && (!DECL_SIZE (constructor_unfilled_fields) |
276beea2 | 6958 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) |
1767a056 | 6959 | constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); |
1955ae11 | 6960 | |
ac4e675f | 6961 | if (constructor_unfilled_fields |
6962 | /* Do not warn if this level of the initializer uses member | |
6963 | designators; it is likely to be deliberate. */ | |
6964 | && !constructor_designated | |
6965 | /* Do not warn about initializing with ` = {0}'. */ | |
6966 | && !constructor_zeroinit) | |
276beea2 | 6967 | { |
292bb106 | 6968 | if (warning_at (input_location, OPT_Wmissing_field_initializers, |
6969 | "missing initializer for field %qD of %qT", | |
6970 | constructor_unfilled_fields, | |
6971 | constructor_type)) | |
6972 | inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), | |
b0ec32b9 | 6973 | "%qD declared here", constructor_unfilled_fields); |
276beea2 | 6974 | } |
6975 | } | |
fc698c9d | 6976 | |
276beea2 | 6977 | /* Pad out the end of the structure. */ |
3ad5f027 | 6978 | if (p->replacement_value.value) |
276beea2 | 6979 | /* If this closes a superfluous brace pair, |
6980 | just pass out the element between them. */ | |
3ad5f027 | 6981 | ret = p->replacement_value; |
276beea2 | 6982 | else if (constructor_type == 0) |
6983 | ; | |
6984 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
6985 | && TREE_CODE (constructor_type) != UNION_TYPE | |
6986 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
6987 | && TREE_CODE (constructor_type) != VECTOR_TYPE) | |
6988 | { | |
6989 | /* A nonincremental scalar initializer--just return | |
6990 | the element, after verifying there is just one. */ | |
c75b4594 | 6991 | if (VEC_empty (constructor_elt,constructor_elements)) |
276beea2 | 6992 | { |
6993 | if (!constructor_erroneous) | |
6994 | error_init ("empty scalar initializer"); | |
3ad5f027 | 6995 | ret.value = error_mark_node; |
276beea2 | 6996 | } |
c75b4594 | 6997 | else if (VEC_length (constructor_elt,constructor_elements) != 1) |
276beea2 | 6998 | { |
6999 | error_init ("extra elements in scalar initializer"); | |
c75b4594 | 7000 | ret.value = VEC_index (constructor_elt,constructor_elements,0)->value; |
276beea2 | 7001 | } |
7002 | else | |
c75b4594 | 7003 | ret.value = VEC_index (constructor_elt,constructor_elements,0)->value; |
276beea2 | 7004 | } |
7005 | else | |
7006 | { | |
7007 | if (constructor_erroneous) | |
3ad5f027 | 7008 | ret.value = error_mark_node; |
276beea2 | 7009 | else |
7010 | { | |
3ad5f027 | 7011 | ret.value = build_constructor (constructor_type, |
c75b4594 | 7012 | constructor_elements); |
276beea2 | 7013 | if (constructor_constant) |
c7d4e749 | 7014 | TREE_CONSTANT (ret.value) = 1; |
276beea2 | 7015 | if (constructor_constant && constructor_simple) |
3ad5f027 | 7016 | TREE_STATIC (ret.value) = 1; |
a75b1c71 | 7017 | if (constructor_nonconst) |
7018 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
276beea2 | 7019 | } |
7020 | } | |
fc698c9d | 7021 | |
a75b1c71 | 7022 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) |
7023 | { | |
7024 | if (constructor_nonconst) | |
7025 | ret.original_code = C_MAYBE_CONST_EXPR; | |
7026 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
7027 | ret.original_code = ERROR_MARK; | |
7028 | } | |
7029 | ||
276beea2 | 7030 | constructor_type = p->type; |
7031 | constructor_fields = p->fields; | |
7032 | constructor_index = p->index; | |
7033 | constructor_max_index = p->max_index; | |
7034 | constructor_unfilled_index = p->unfilled_index; | |
7035 | constructor_unfilled_fields = p->unfilled_fields; | |
7036 | constructor_bit_index = p->bit_index; | |
7037 | constructor_elements = p->elements; | |
7038 | constructor_constant = p->constant; | |
7039 | constructor_simple = p->simple; | |
a75b1c71 | 7040 | constructor_nonconst = p->nonconst; |
276beea2 | 7041 | constructor_erroneous = p->erroneous; |
7042 | constructor_incremental = p->incremental; | |
7043 | constructor_designated = p->designated; | |
7044 | constructor_pending_elts = p->pending_elts; | |
7045 | constructor_depth = p->depth; | |
7046 | if (!p->implicit) | |
7047 | constructor_range_stack = p->range_stack; | |
7048 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
fc698c9d | 7049 | |
276beea2 | 7050 | constructor_stack = p->next; |
7051 | free (p); | |
52136990 | 7052 | |
09d2b71a | 7053 | if (ret.value == 0 && constructor_stack == 0) |
7054 | ret.value = error_mark_node; | |
3ad5f027 | 7055 | return ret; |
276beea2 | 7056 | } |
989bbcfc | 7057 | |
276beea2 | 7058 | /* Common handling for both array range and field name designators. |
7059 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
628fa4f8 | 7060 | |
276beea2 | 7061 | static int |
759791ee | 7062 | set_designator (int array, struct obstack * braced_init_obstack) |
fc698c9d | 7063 | { |
276beea2 | 7064 | tree subtype; |
7065 | enum tree_code subcode; | |
fc698c9d | 7066 | |
276beea2 | 7067 | /* Don't die if an entire brace-pair level is superfluous |
7068 | in the containing level. */ | |
7069 | if (constructor_type == 0) | |
7070 | return 1; | |
fc698c9d | 7071 | |
231bd014 | 7072 | /* If there were errors in this designator list already, bail out |
7073 | silently. */ | |
ebe39827 | 7074 | if (designator_erroneous) |
276beea2 | 7075 | return 1; |
0583de9c | 7076 | |
276beea2 | 7077 | if (!designator_depth) |
7078 | { | |
231bd014 | 7079 | gcc_assert (!constructor_range_stack); |
fc698c9d | 7080 | |
276beea2 | 7081 | /* Designator list starts at the level of closest explicit |
7082 | braces. */ | |
7083 | while (constructor_stack->implicit) | |
759791ee | 7084 | { |
7085 | process_init_element (pop_init_level (1, braced_init_obstack), | |
7086 | true, braced_init_obstack); | |
7087 | } | |
276beea2 | 7088 | constructor_designated = 1; |
7089 | return 0; | |
7090 | } | |
fc698c9d | 7091 | |
231bd014 | 7092 | switch (TREE_CODE (constructor_type)) |
f1c75cb5 | 7093 | { |
231bd014 | 7094 | case RECORD_TYPE: |
7095 | case UNION_TYPE: | |
276beea2 | 7096 | subtype = TREE_TYPE (constructor_fields); |
7097 | if (subtype != error_mark_node) | |
7098 | subtype = TYPE_MAIN_VARIANT (subtype); | |
231bd014 | 7099 | break; |
7100 | case ARRAY_TYPE: | |
276beea2 | 7101 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
231bd014 | 7102 | break; |
7103 | default: | |
7104 | gcc_unreachable (); | |
fc698c9d | 7105 | } |
628fa4f8 | 7106 | |
276beea2 | 7107 | subcode = TREE_CODE (subtype); |
7108 | if (array && subcode != ARRAY_TYPE) | |
7109 | { | |
7110 | error_init ("array index in non-array initializer"); | |
7111 | return 1; | |
7112 | } | |
7113 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
7114 | { | |
7115 | error_init ("field name not in record or union initializer"); | |
7116 | return 1; | |
7117 | } | |
f9e15121 | 7118 | |
276beea2 | 7119 | constructor_designated = 1; |
759791ee | 7120 | push_init_level (2, braced_init_obstack); |
276beea2 | 7121 | return 0; |
fc698c9d | 7122 | } |
628fa4f8 | 7123 | |
276beea2 | 7124 | /* If there are range designators in designator list, push a new designator |
7125 | to constructor_range_stack. RANGE_END is end of such stack range or | |
7126 | NULL_TREE if there is no range designator at this level. */ | |
628fa4f8 | 7127 | |
276beea2 | 7128 | static void |
759791ee | 7129 | push_range_stack (tree range_end, struct obstack * braced_init_obstack) |
276beea2 | 7130 | { |
7131 | struct constructor_range_stack *p; | |
628fa4f8 | 7132 | |
759791ee | 7133 | p = (struct constructor_range_stack *) |
7134 | obstack_alloc (braced_init_obstack, | |
7135 | sizeof (struct constructor_range_stack)); | |
276beea2 | 7136 | p->prev = constructor_range_stack; |
7137 | p->next = 0; | |
7138 | p->fields = constructor_fields; | |
7139 | p->range_start = constructor_index; | |
7140 | p->index = constructor_index; | |
7141 | p->stack = constructor_stack; | |
7142 | p->range_end = range_end; | |
989bbcfc | 7143 | if (constructor_range_stack) |
276beea2 | 7144 | constructor_range_stack->next = p; |
7145 | constructor_range_stack = p; | |
fc698c9d | 7146 | } |
628fa4f8 | 7147 | |
276beea2 | 7148 | /* Within an array initializer, specify the next index to be initialized. |
7149 | FIRST is that index. If LAST is nonzero, then initialize a range | |
7150 | of indices, running from FIRST through LAST. */ | |
ca9c4c86 | 7151 | |
fc698c9d | 7152 | void |
759791ee | 7153 | set_init_index (tree first, tree last, |
7154 | struct obstack * braced_init_obstack) | |
fc698c9d | 7155 | { |
759791ee | 7156 | if (set_designator (1, braced_init_obstack)) |
276beea2 | 7157 | return; |
fc698c9d | 7158 | |
ebe39827 | 7159 | designator_erroneous = 1; |
fc698c9d | 7160 | |
a250ed2e | 7161 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
7162 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
7163 | { | |
7164 | error_init ("array index in initializer not of integer type"); | |
7165 | return; | |
7166 | } | |
7167 | ||
88c32270 | 7168 | if (TREE_CODE (first) != INTEGER_CST) |
7169 | { | |
7170 | first = c_fully_fold (first, false, NULL); | |
7171 | if (TREE_CODE (first) == INTEGER_CST) | |
29438999 | 7172 | pedwarn_init (input_location, OPT_Wpedantic, |
88c32270 | 7173 | "array index in initializer is not " |
7174 | "an integer constant expression"); | |
7175 | } | |
7176 | ||
7177 | if (last && TREE_CODE (last) != INTEGER_CST) | |
7178 | { | |
7179 | last = c_fully_fold (last, false, NULL); | |
7180 | if (TREE_CODE (last) == INTEGER_CST) | |
29438999 | 7181 | pedwarn_init (input_location, OPT_Wpedantic, |
88c32270 | 7182 | "array index in initializer is not " |
7183 | "an integer constant expression"); | |
7184 | } | |
7185 | ||
276beea2 | 7186 | if (TREE_CODE (first) != INTEGER_CST) |
7187 | error_init ("nonconstant array index in initializer"); | |
7188 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) | |
7189 | error_init ("nonconstant array index in initializer"); | |
7190 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7191 | error_init ("array index in non-array initializer"); | |
42b1a257 | 7192 | else if (tree_int_cst_sgn (first) == -1) |
7193 | error_init ("array index in initializer exceeds array bounds"); | |
276beea2 | 7194 | else if (constructor_max_index |
7195 | && tree_int_cst_lt (constructor_max_index, first)) | |
7196 | error_init ("array index in initializer exceeds array bounds"); | |
7197 | else | |
fc698c9d | 7198 | { |
a75b1c71 | 7199 | constant_expression_warning (first); |
7200 | if (last) | |
7201 | constant_expression_warning (last); | |
276beea2 | 7202 | constructor_index = convert (bitsizetype, first); |
5d844ba2 | 7203 | |
276beea2 | 7204 | if (last) |
886de5de | 7205 | { |
276beea2 | 7206 | if (tree_int_cst_equal (first, last)) |
7207 | last = 0; | |
7208 | else if (tree_int_cst_lt (last, first)) | |
7209 | { | |
7210 | error_init ("empty index range in initializer"); | |
7211 | last = 0; | |
7212 | } | |
7213 | else | |
7214 | { | |
7215 | last = convert (bitsizetype, last); | |
7216 | if (constructor_max_index != 0 | |
7217 | && tree_int_cst_lt (constructor_max_index, last)) | |
7218 | { | |
7219 | error_init ("array index range in initializer exceeds array bounds"); | |
7220 | last = 0; | |
7221 | } | |
7222 | } | |
886de5de | 7223 | } |
902de8ed | 7224 | |
276beea2 | 7225 | designator_depth++; |
ebe39827 | 7226 | designator_erroneous = 0; |
276beea2 | 7227 | if (constructor_range_stack || last) |
759791ee | 7228 | push_range_stack (last, braced_init_obstack); |
fc698c9d | 7229 | } |
fc698c9d | 7230 | } |
276beea2 | 7231 | |
7232 | /* Within a struct initializer, specify the next field to be initialized. */ | |
628fa4f8 | 7233 | |
fc698c9d | 7234 | void |
759791ee | 7235 | set_init_label (tree fieldname, struct obstack * braced_init_obstack) |
fc698c9d | 7236 | { |
10064fb9 | 7237 | tree field; |
d354d271 | 7238 | |
759791ee | 7239 | if (set_designator (0, braced_init_obstack)) |
276beea2 | 7240 | return; |
7241 | ||
ebe39827 | 7242 | designator_erroneous = 1; |
276beea2 | 7243 | |
7244 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7245 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
d354d271 | 7246 | { |
276beea2 | 7247 | error_init ("field name not in record or union initializer"); |
7248 | return; | |
d354d271 | 7249 | } |
7250 | ||
10064fb9 | 7251 | field = lookup_field (constructor_type, fieldname); |
989bbcfc | 7252 | |
10064fb9 | 7253 | if (field == 0) |
782858b8 | 7254 | error ("unknown field %qE specified in initializer", fieldname); |
276beea2 | 7255 | else |
10064fb9 | 7256 | do |
7257 | { | |
7258 | constructor_fields = TREE_VALUE (field); | |
7259 | designator_depth++; | |
7260 | designator_erroneous = 0; | |
7261 | if (constructor_range_stack) | |
7262 | push_range_stack (NULL_TREE, braced_init_obstack); | |
7263 | field = TREE_CHAIN (field); | |
7264 | if (field) | |
7265 | { | |
7266 | if (set_designator (0, braced_init_obstack)) | |
7267 | return; | |
7268 | } | |
7269 | } | |
7270 | while (field != NULL_TREE); | |
276beea2 | 7271 | } |
7272 | \f | |
7273 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
7274 | identifies the initializer, either array index or field in a structure. | |
b9c74b4d | 7275 | VALUE is the value of that index or field. If ORIGTYPE is not |
7276 | NULL_TREE, it is the original type of VALUE. | |
d241bf0d | 7277 | |
7278 | IMPLICIT is true if value comes from pop_init_level (1), | |
7279 | the new initializer has been merged with the existing one | |
7280 | and thus no warnings should be emitted about overriding an | |
7281 | existing initializer. */ | |
fc698c9d | 7282 | |
276beea2 | 7283 | static void |
759791ee | 7284 | add_pending_init (tree purpose, tree value, tree origtype, bool implicit, |
7285 | struct obstack * braced_init_obstack) | |
276beea2 | 7286 | { |
7287 | struct init_node *p, **q, *r; | |
7288 | ||
7289 | q = &constructor_pending_elts; | |
7290 | p = 0; | |
7291 | ||
7292 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
fc698c9d | 7293 | { |
276beea2 | 7294 | while (*q != 0) |
812fff1a | 7295 | { |
276beea2 | 7296 | p = *q; |
7297 | if (tree_int_cst_lt (purpose, p->purpose)) | |
7298 | q = &p->left; | |
7299 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
7300 | q = &p->right; | |
7301 | else | |
7302 | { | |
d241bf0d | 7303 | if (!implicit) |
7304 | { | |
7305 | if (TREE_SIDE_EFFECTS (p->value)) | |
7306 | warning_init (0, "initialized field with side-effects overwritten"); | |
7307 | else if (warn_override_init) | |
7308 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7309 | } | |
276beea2 | 7310 | p->value = value; |
b9c74b4d | 7311 | p->origtype = origtype; |
276beea2 | 7312 | return; |
7313 | } | |
812fff1a | 7314 | } |
fc698c9d | 7315 | } |
276beea2 | 7316 | else |
fc698c9d | 7317 | { |
276beea2 | 7318 | tree bitpos; |
628fa4f8 | 7319 | |
276beea2 | 7320 | bitpos = bit_position (purpose); |
7321 | while (*q != NULL) | |
7322 | { | |
7323 | p = *q; | |
7324 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7325 | q = &p->left; | |
7326 | else if (p->purpose != purpose) | |
7327 | q = &p->right; | |
7328 | else | |
7329 | { | |
d241bf0d | 7330 | if (!implicit) |
7331 | { | |
7332 | if (TREE_SIDE_EFFECTS (p->value)) | |
7333 | warning_init (0, "initialized field with side-effects overwritten"); | |
7334 | else if (warn_override_init) | |
7335 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7336 | } | |
276beea2 | 7337 | p->value = value; |
b9c74b4d | 7338 | p->origtype = origtype; |
276beea2 | 7339 | return; |
7340 | } | |
7341 | } | |
812fff1a | 7342 | } |
89a5f6d1 | 7343 | |
759791ee | 7344 | r = (struct init_node *) obstack_alloc (braced_init_obstack, |
7345 | sizeof (struct init_node)); | |
276beea2 | 7346 | r->purpose = purpose; |
7347 | r->value = value; | |
b9c74b4d | 7348 | r->origtype = origtype; |
989bbcfc | 7349 | |
276beea2 | 7350 | *q = r; |
7351 | r->parent = p; | |
7352 | r->left = 0; | |
7353 | r->right = 0; | |
7354 | r->balance = 0; | |
89a5f6d1 | 7355 | |
276beea2 | 7356 | while (p) |
fc698c9d | 7357 | { |
276beea2 | 7358 | struct init_node *s; |
5d844ba2 | 7359 | |
276beea2 | 7360 | if (r == p->left) |
886de5de | 7361 | { |
276beea2 | 7362 | if (p->balance == 0) |
7363 | p->balance = -1; | |
7364 | else if (p->balance < 0) | |
7365 | { | |
7366 | if (r->balance < 0) | |
7367 | { | |
7368 | /* L rotation. */ | |
7369 | p->left = r->right; | |
7370 | if (p->left) | |
7371 | p->left->parent = p; | |
7372 | r->right = p; | |
26877a3f | 7373 | |
276beea2 | 7374 | p->balance = 0; |
7375 | r->balance = 0; | |
c052c0ba | 7376 | |
276beea2 | 7377 | s = p->parent; |
7378 | p->parent = r; | |
7379 | r->parent = s; | |
7380 | if (s) | |
7381 | { | |
7382 | if (s->left == p) | |
7383 | s->left = r; | |
7384 | else | |
7385 | s->right = r; | |
7386 | } | |
7387 | else | |
7388 | constructor_pending_elts = r; | |
7389 | } | |
7390 | else | |
7391 | { | |
7392 | /* LR rotation. */ | |
7393 | struct init_node *t = r->right; | |
26877a3f | 7394 | |
276beea2 | 7395 | r->right = t->left; |
7396 | if (r->right) | |
7397 | r->right->parent = r; | |
7398 | t->left = r; | |
7399 | ||
7400 | p->left = t->right; | |
7401 | if (p->left) | |
7402 | p->left->parent = p; | |
7403 | t->right = p; | |
7404 | ||
7405 | p->balance = t->balance < 0; | |
7406 | r->balance = -(t->balance > 0); | |
7407 | t->balance = 0; | |
7408 | ||
7409 | s = p->parent; | |
7410 | p->parent = t; | |
7411 | r->parent = t; | |
7412 | t->parent = s; | |
7413 | if (s) | |
7414 | { | |
7415 | if (s->left == p) | |
7416 | s->left = t; | |
7417 | else | |
7418 | s->right = t; | |
7419 | } | |
7420 | else | |
7421 | constructor_pending_elts = t; | |
7422 | } | |
7423 | break; | |
7424 | } | |
7425 | else | |
7426 | { | |
7427 | /* p->balance == +1; growth of left side balances the node. */ | |
7428 | p->balance = 0; | |
7429 | break; | |
7430 | } | |
886de5de | 7431 | } |
276beea2 | 7432 | else /* r == p->right */ |
7433 | { | |
7434 | if (p->balance == 0) | |
7435 | /* Growth propagation from right side. */ | |
7436 | p->balance++; | |
7437 | else if (p->balance > 0) | |
7438 | { | |
7439 | if (r->balance > 0) | |
7440 | { | |
7441 | /* R rotation. */ | |
7442 | p->right = r->left; | |
7443 | if (p->right) | |
7444 | p->right->parent = p; | |
7445 | r->left = p; | |
7446 | ||
7447 | p->balance = 0; | |
7448 | r->balance = 0; | |
7449 | ||
7450 | s = p->parent; | |
7451 | p->parent = r; | |
7452 | r->parent = s; | |
7453 | if (s) | |
7454 | { | |
7455 | if (s->left == p) | |
7456 | s->left = r; | |
7457 | else | |
7458 | s->right = r; | |
7459 | } | |
7460 | else | |
7461 | constructor_pending_elts = r; | |
7462 | } | |
7463 | else /* r->balance == -1 */ | |
7464 | { | |
7465 | /* RL rotation */ | |
7466 | struct init_node *t = r->left; | |
7467 | ||
7468 | r->left = t->right; | |
7469 | if (r->left) | |
7470 | r->left->parent = r; | |
7471 | t->right = r; | |
7472 | ||
7473 | p->right = t->left; | |
7474 | if (p->right) | |
7475 | p->right->parent = p; | |
7476 | t->left = p; | |
7477 | ||
7478 | r->balance = (t->balance < 0); | |
7479 | p->balance = -(t->balance > 0); | |
7480 | t->balance = 0; | |
7481 | ||
7482 | s = p->parent; | |
7483 | p->parent = t; | |
7484 | r->parent = t; | |
7485 | t->parent = s; | |
7486 | if (s) | |
7487 | { | |
7488 | if (s->left == p) | |
7489 | s->left = t; | |
7490 | else | |
7491 | s->right = t; | |
7492 | } | |
7493 | else | |
7494 | constructor_pending_elts = t; | |
7495 | } | |
7496 | break; | |
7497 | } | |
7498 | else | |
7499 | { | |
7500 | /* p->balance == -1; growth of right side balances the node. */ | |
7501 | p->balance = 0; | |
7502 | break; | |
7503 | } | |
7504 | } | |
7505 | ||
7506 | r = p; | |
7507 | p = p->parent; | |
7508 | } | |
7509 | } | |
7510 | ||
7511 | /* Build AVL tree from a sorted chain. */ | |
7512 | ||
7513 | static void | |
759791ee | 7514 | set_nonincremental_init (struct obstack * braced_init_obstack) |
276beea2 | 7515 | { |
c75b4594 | 7516 | unsigned HOST_WIDE_INT ix; |
7517 | tree index, value; | |
276beea2 | 7518 | |
7519 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7520 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7521 | return; | |
7522 | ||
c75b4594 | 7523 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) |
759791ee | 7524 | { |
537cbcc2 | 7525 | add_pending_init (index, value, NULL_TREE, true, |
759791ee | 7526 | braced_init_obstack); |
7527 | } | |
276beea2 | 7528 | constructor_elements = 0; |
7529 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
7530 | { | |
7531 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
7532 | /* Skip any nameless bit fields at the beginning. */ | |
7533 | while (constructor_unfilled_fields != 0 | |
7534 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7535 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7536 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
902de8ed | 7537 | |
fc698c9d | 7538 | } |
276beea2 | 7539 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
fc698c9d | 7540 | { |
276beea2 | 7541 | if (TYPE_DOMAIN (constructor_type)) |
7542 | constructor_unfilled_index | |
7543 | = convert (bitsizetype, | |
7544 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7545 | else | |
7546 | constructor_unfilled_index = bitsize_zero_node; | |
fc698c9d | 7547 | } |
276beea2 | 7548 | constructor_incremental = 0; |
fc698c9d | 7549 | } |
628fa4f8 | 7550 | |
276beea2 | 7551 | /* Build AVL tree from a string constant. */ |
fc698c9d | 7552 | |
276beea2 | 7553 | static void |
759791ee | 7554 | set_nonincremental_init_from_string (tree str, |
7555 | struct obstack * braced_init_obstack) | |
fc698c9d | 7556 | { |
276beea2 | 7557 | tree value, purpose, type; |
7558 | HOST_WIDE_INT val[2]; | |
7559 | const char *p, *end; | |
7560 | int byte, wchar_bytes, charwidth, bitpos; | |
fc698c9d | 7561 | |
231bd014 | 7562 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); |
b9fc4354 | 7563 | |
b0726616 | 7564 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; |
276beea2 | 7565 | charwidth = TYPE_PRECISION (char_type_node); |
7566 | type = TREE_TYPE (constructor_type); | |
7567 | p = TREE_STRING_POINTER (str); | |
7568 | end = p + TREE_STRING_LENGTH (str); | |
812fff1a | 7569 | |
276beea2 | 7570 | for (purpose = bitsize_zero_node; |
7571 | p < end && !tree_int_cst_lt (constructor_max_index, purpose); | |
7572 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) | |
15119d83 | 7573 | { |
276beea2 | 7574 | if (wchar_bytes == 1) |
21c8999d | 7575 | { |
276beea2 | 7576 | val[1] = (unsigned char) *p++; |
7577 | val[0] = 0; | |
21c8999d | 7578 | } |
7579 | else | |
276beea2 | 7580 | { |
7581 | val[0] = 0; | |
7582 | val[1] = 0; | |
7583 | for (byte = 0; byte < wchar_bytes; byte++) | |
7584 | { | |
7585 | if (BYTES_BIG_ENDIAN) | |
7586 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
7587 | else | |
7588 | bitpos = byte * charwidth; | |
7589 | val[bitpos < HOST_BITS_PER_WIDE_INT] | |
7590 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) | |
7591 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
7592 | } | |
7593 | } | |
15119d83 | 7594 | |
78a8ed03 | 7595 | if (!TYPE_UNSIGNED (type)) |
276beea2 | 7596 | { |
7597 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
7598 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
7599 | { | |
7600 | if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) | |
7601 | { | |
7602 | val[1] |= ((HOST_WIDE_INT) -1) << bitpos; | |
7603 | val[0] = -1; | |
7604 | } | |
7605 | } | |
7606 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
7607 | { | |
7608 | if (val[1] < 0) | |
a0c938f0 | 7609 | val[0] = -1; |
276beea2 | 7610 | } |
7611 | else if (val[0] & (((HOST_WIDE_INT) 1) | |
7612 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) | |
7613 | val[0] |= ((HOST_WIDE_INT) -1) | |
7614 | << (bitpos - HOST_BITS_PER_WIDE_INT); | |
7615 | } | |
21c8999d | 7616 | |
7016c612 | 7617 | value = build_int_cst_wide (type, val[1], val[0]); |
537cbcc2 | 7618 | add_pending_init (purpose, value, NULL_TREE, true, |
759791ee | 7619 | braced_init_obstack); |
03632119 | 7620 | } |
7621 | ||
276beea2 | 7622 | constructor_incremental = 0; |
7623 | } | |
fc698c9d | 7624 | |
276beea2 | 7625 | /* Return value of FIELD in pending initializer or zero if the field was |
7626 | not initialized yet. */ | |
7627 | ||
7628 | static tree | |
759791ee | 7629 | find_init_member (tree field, struct obstack * braced_init_obstack) |
276beea2 | 7630 | { |
7631 | struct init_node *p; | |
7632 | ||
7633 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
37c005bb | 7634 | { |
276beea2 | 7635 | if (constructor_incremental |
7636 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
759791ee | 7637 | set_nonincremental_init (braced_init_obstack); |
276beea2 | 7638 | |
7639 | p = constructor_pending_elts; | |
7640 | while (p) | |
37c005bb | 7641 | { |
276beea2 | 7642 | if (tree_int_cst_lt (field, p->purpose)) |
7643 | p = p->left; | |
7644 | else if (tree_int_cst_lt (p->purpose, field)) | |
7645 | p = p->right; | |
7646 | else | |
7647 | return p->value; | |
37c005bb | 7648 | } |
37c005bb | 7649 | } |
276beea2 | 7650 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
fc698c9d | 7651 | { |
276beea2 | 7652 | tree bitpos = bit_position (field); |
fc698c9d | 7653 | |
276beea2 | 7654 | if (constructor_incremental |
7655 | && (!constructor_unfilled_fields | |
7656 | || tree_int_cst_lt (bitpos, | |
7657 | bit_position (constructor_unfilled_fields)))) | |
759791ee | 7658 | set_nonincremental_init (braced_init_obstack); |
fc698c9d | 7659 | |
276beea2 | 7660 | p = constructor_pending_elts; |
7661 | while (p) | |
7662 | { | |
7663 | if (field == p->purpose) | |
7664 | return p->value; | |
7665 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7666 | p = p->left; | |
7667 | else | |
7668 | p = p->right; | |
7669 | } | |
7670 | } | |
7671 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
fc698c9d | 7672 | { |
c75b4594 | 7673 | if (!VEC_empty (constructor_elt, constructor_elements) |
7674 | && (VEC_last (constructor_elt, constructor_elements)->index | |
7675 | == field)) | |
7676 | return VEC_last (constructor_elt, constructor_elements)->value; | |
fc698c9d | 7677 | } |
276beea2 | 7678 | return 0; |
fc698c9d | 7679 | } |
7680 | ||
276beea2 | 7681 | /* "Output" the next constructor element. |
7682 | At top level, really output it to assembler code now. | |
7683 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
b9c74b4d | 7684 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. |
276beea2 | 7685 | TYPE is the data type that the containing data type wants here. |
7686 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
3ad5f027 | 7687 | If VALUE is a string constant, STRICT_STRING is true if it is |
7688 | unparenthesized or we should not warn here for it being parenthesized. | |
7689 | For other types of VALUE, STRICT_STRING is not used. | |
989bbcfc | 7690 | |
276beea2 | 7691 | PENDING if non-nil means output pending elements that belong |
7692 | right after this element. (PENDING is normally 1; | |
d241bf0d | 7693 | it is 0 while outputting pending elements, to avoid recursion.) |
7694 | ||
7695 | IMPLICIT is true if value comes from pop_init_level (1), | |
7696 | the new initializer has been merged with the existing one | |
7697 | and thus no warnings should be emitted about overriding an | |
7698 | existing initializer. */ | |
989bbcfc | 7699 | |
276beea2 | 7700 | static void |
b9c74b4d | 7701 | output_init_element (tree value, tree origtype, bool strict_string, tree type, |
759791ee | 7702 | tree field, int pending, bool implicit, |
7703 | struct obstack * braced_init_obstack) | |
276beea2 | 7704 | { |
c6418a4e | 7705 | tree semantic_type = NULL_TREE; |
c75b4594 | 7706 | constructor_elt *celt; |
a75b1c71 | 7707 | bool maybe_const = true; |
7708 | bool npc; | |
c75b4594 | 7709 | |
081b8c23 | 7710 | if (type == error_mark_node || value == error_mark_node) |
989bbcfc | 7711 | { |
276beea2 | 7712 | constructor_erroneous = 1; |
7713 | return; | |
989bbcfc | 7714 | } |
f14c8207 | 7715 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
7716 | && (TREE_CODE (value) == STRING_CST | |
7717 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
7718 | && !(TREE_CODE (value) == STRING_CST | |
7719 | && TREE_CODE (type) == ARRAY_TYPE | |
7720 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
7721 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
7722 | TYPE_MAIN_VARIANT (type))) | |
e60a6f7b | 7723 | value = array_to_pointer_conversion (input_location, value); |
989bbcfc | 7724 | |
276beea2 | 7725 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
7726 | && require_constant_value && !flag_isoc99 && pending) | |
989bbcfc | 7727 | { |
276beea2 | 7728 | /* As an extension, allow initializing objects with static storage |
7729 | duration with compound literals (which are then treated just as | |
7730 | the brace enclosed list they contain). */ | |
7731 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); | |
7732 | value = DECL_INITIAL (decl); | |
989bbcfc | 7733 | } |
7734 | ||
a75b1c71 | 7735 | npc = null_pointer_constant_p (value); |
c6418a4e | 7736 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) |
7737 | { | |
7738 | semantic_type = TREE_TYPE (value); | |
7739 | value = TREE_OPERAND (value, 0); | |
7740 | } | |
a75b1c71 | 7741 | value = c_fully_fold (value, require_constant_value, &maybe_const); |
7742 | ||
276beea2 | 7743 | if (value == error_mark_node) |
7744 | constructor_erroneous = 1; | |
7745 | else if (!TREE_CONSTANT (value)) | |
7746 | constructor_constant = 0; | |
fdf95cc9 | 7747 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) |
276beea2 | 7748 | || ((TREE_CODE (constructor_type) == RECORD_TYPE |
7749 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7750 | && DECL_C_BIT_FIELD (field) | |
7751 | && TREE_CODE (value) != INTEGER_CST)) | |
7752 | constructor_simple = 0; | |
a75b1c71 | 7753 | if (!maybe_const) |
7754 | constructor_nonconst = 1; | |
276beea2 | 7755 | |
fdf95cc9 | 7756 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) |
989bbcfc | 7757 | { |
fdf95cc9 | 7758 | if (require_constant_value) |
7759 | { | |
7760 | error_init ("initializer element is not constant"); | |
7761 | value = error_mark_node; | |
7762 | } | |
7763 | else if (require_constant_elements) | |
21ca8540 | 7764 | pedwarn (input_location, 0, |
7765 | "initializer element is not computable at load time"); | |
989bbcfc | 7766 | } |
a75b1c71 | 7767 | else if (!maybe_const |
7768 | && (require_constant_value || require_constant_elements)) | |
7769 | pedwarn_init (input_location, 0, | |
7770 | "initializer element is not a constant expression"); | |
276beea2 | 7771 | |
bc620c5c | 7772 | /* Issue -Wc++-compat warnings about initializing a bitfield with |
7773 | enum type. */ | |
7774 | if (warn_cxx_compat | |
7775 | && field != NULL_TREE | |
7776 | && TREE_CODE (field) == FIELD_DECL | |
7777 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
7778 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
7779 | != TYPE_MAIN_VARIANT (type)) | |
7780 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
7781 | { | |
7782 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
7783 | if (checktype != error_mark_node | |
7784 | && (TYPE_MAIN_VARIANT (checktype) | |
7785 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
7786 | warning_init (OPT_Wc___compat, | |
7787 | "enum conversion in initialization is invalid in C++"); | |
7788 | } | |
7789 | ||
276beea2 | 7790 | /* If this field is empty (and not at the end of structure), |
7791 | don't do anything other than checking the initializer. */ | |
7792 | if (field | |
7793 | && (TREE_TYPE (field) == error_mark_node | |
7794 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
7795 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
7796 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
1767a056 | 7797 | || DECL_CHAIN (field))))) |
276beea2 | 7798 | return; |
7799 | ||
c6418a4e | 7800 | if (semantic_type) |
7801 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
e60a6f7b | 7802 | value = digest_init (input_location, type, value, origtype, npc, |
7803 | strict_string, require_constant_value); | |
276beea2 | 7804 | if (value == error_mark_node) |
989bbcfc | 7805 | { |
276beea2 | 7806 | constructor_erroneous = 1; |
7807 | return; | |
989bbcfc | 7808 | } |
a75b1c71 | 7809 | if (require_constant_value || require_constant_elements) |
7810 | constant_expression_warning (value); | |
989bbcfc | 7811 | |
276beea2 | 7812 | /* If this element doesn't come next in sequence, |
7813 | put it on constructor_pending_elts. */ | |
7814 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
7815 | && (!constructor_incremental | |
7816 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
989bbcfc | 7817 | { |
276beea2 | 7818 | if (constructor_incremental |
7819 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
759791ee | 7820 | set_nonincremental_init (braced_init_obstack); |
276beea2 | 7821 | |
759791ee | 7822 | add_pending_init (field, value, origtype, implicit, |
7823 | braced_init_obstack); | |
276beea2 | 7824 | return; |
989bbcfc | 7825 | } |
276beea2 | 7826 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
7827 | && (!constructor_incremental | |
7828 | || field != constructor_unfilled_fields)) | |
989bbcfc | 7829 | { |
276beea2 | 7830 | /* We do this for records but not for unions. In a union, |
7831 | no matter which field is specified, it can be initialized | |
7832 | right away since it starts at the beginning of the union. */ | |
7833 | if (constructor_incremental) | |
7834 | { | |
7835 | if (!constructor_unfilled_fields) | |
759791ee | 7836 | set_nonincremental_init (braced_init_obstack); |
276beea2 | 7837 | else |
7838 | { | |
7839 | tree bitpos, unfillpos; | |
7840 | ||
7841 | bitpos = bit_position (field); | |
7842 | unfillpos = bit_position (constructor_unfilled_fields); | |
7843 | ||
7844 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
759791ee | 7845 | set_nonincremental_init (braced_init_obstack); |
276beea2 | 7846 | } |
7847 | } | |
7848 | ||
759791ee | 7849 | add_pending_init (field, value, origtype, implicit, |
7850 | braced_init_obstack); | |
276beea2 | 7851 | return; |
989bbcfc | 7852 | } |
276beea2 | 7853 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
c75b4594 | 7854 | && !VEC_empty (constructor_elt, constructor_elements)) |
276beea2 | 7855 | { |
d241bf0d | 7856 | if (!implicit) |
7857 | { | |
7858 | if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt, | |
7859 | constructor_elements)->value)) | |
7860 | warning_init (0, | |
7861 | "initialized field with side-effects overwritten"); | |
7862 | else if (warn_override_init) | |
7863 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7864 | } | |
989bbcfc | 7865 | |
276beea2 | 7866 | /* We can have just one union field set. */ |
7867 | constructor_elements = 0; | |
7868 | } | |
989bbcfc | 7869 | |
276beea2 | 7870 | /* Otherwise, output this element either to |
7871 | constructor_elements or to the assembler file. */ | |
989bbcfc | 7872 | |
c75b4594 | 7873 | celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL); |
7874 | celt->index = field; | |
7875 | celt->value = value; | |
989bbcfc | 7876 | |
276beea2 | 7877 | /* Advance the variable that indicates sequential elements output. */ |
7878 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7879 | constructor_unfilled_index | |
389dd41b | 7880 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, |
7881 | bitsize_one_node); | |
276beea2 | 7882 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
7883 | { | |
7884 | constructor_unfilled_fields | |
1767a056 | 7885 | = DECL_CHAIN (constructor_unfilled_fields); |
989bbcfc | 7886 | |
276beea2 | 7887 | /* Skip any nameless bit fields. */ |
7888 | while (constructor_unfilled_fields != 0 | |
7889 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7890 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7891 | constructor_unfilled_fields = | |
1767a056 | 7892 | DECL_CHAIN (constructor_unfilled_fields); |
276beea2 | 7893 | } |
7894 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
7895 | constructor_unfilled_fields = 0; | |
fc698c9d | 7896 | |
276beea2 | 7897 | /* Now output any pending elements which have become next. */ |
7898 | if (pending) | |
759791ee | 7899 | output_pending_init_elements (0, braced_init_obstack); |
276beea2 | 7900 | } |
989bbcfc | 7901 | |
276beea2 | 7902 | /* Output any pending elements which have become next. |
7903 | As we output elements, constructor_unfilled_{fields,index} | |
7904 | advances, which may cause other elements to become next; | |
7905 | if so, they too are output. | |
989bbcfc | 7906 | |
276beea2 | 7907 | If ALL is 0, we return when there are |
7908 | no more pending elements to output now. | |
5d844ba2 | 7909 | |
276beea2 | 7910 | If ALL is 1, we output space as necessary so that |
7911 | we can output all the pending elements. */ | |
276beea2 | 7912 | static void |
759791ee | 7913 | output_pending_init_elements (int all, struct obstack * braced_init_obstack) |
276beea2 | 7914 | { |
7915 | struct init_node *elt = constructor_pending_elts; | |
7916 | tree next; | |
fc698c9d | 7917 | |
276beea2 | 7918 | retry: |
7919 | ||
c7bf1374 | 7920 | /* Look through the whole pending tree. |
276beea2 | 7921 | If we find an element that should be output now, |
7922 | output it. Otherwise, set NEXT to the element | |
7923 | that comes first among those still pending. */ | |
7924 | ||
7925 | next = 0; | |
7926 | while (elt) | |
7927 | { | |
7928 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
989bbcfc | 7929 | { |
276beea2 | 7930 | if (tree_int_cst_equal (elt->purpose, |
7931 | constructor_unfilled_index)) | |
b9c74b4d | 7932 | output_init_element (elt->value, elt->origtype, true, |
276beea2 | 7933 | TREE_TYPE (constructor_type), |
759791ee | 7934 | constructor_unfilled_index, 0, false, |
7935 | braced_init_obstack); | |
276beea2 | 7936 | else if (tree_int_cst_lt (constructor_unfilled_index, |
7937 | elt->purpose)) | |
989bbcfc | 7938 | { |
276beea2 | 7939 | /* Advance to the next smaller node. */ |
7940 | if (elt->left) | |
7941 | elt = elt->left; | |
7942 | else | |
7943 | { | |
7944 | /* We have reached the smallest node bigger than the | |
7945 | current unfilled index. Fill the space first. */ | |
7946 | next = elt->purpose; | |
7947 | break; | |
7948 | } | |
989bbcfc | 7949 | } |
b10b344f | 7950 | else |
7951 | { | |
276beea2 | 7952 | /* Advance to the next bigger node. */ |
7953 | if (elt->right) | |
7954 | elt = elt->right; | |
7955 | else | |
b10b344f | 7956 | { |
276beea2 | 7957 | /* We have reached the biggest node in a subtree. Find |
7958 | the parent of it, which is the next bigger node. */ | |
7959 | while (elt->parent && elt->parent->right == elt) | |
7960 | elt = elt->parent; | |
7961 | elt = elt->parent; | |
7962 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
7963 | elt->purpose)) | |
7964 | { | |
7965 | next = elt->purpose; | |
7966 | break; | |
7967 | } | |
b10b344f | 7968 | } |
7969 | } | |
989bbcfc | 7970 | } |
276beea2 | 7971 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
7972 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7973 | { | |
7974 | tree ctor_unfilled_bitpos, elt_bitpos; | |
b10b344f | 7975 | |
276beea2 | 7976 | /* If the current record is complete we are done. */ |
7977 | if (constructor_unfilled_fields == 0) | |
7978 | break; | |
fc698c9d | 7979 | |
276beea2 | 7980 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
7981 | elt_bitpos = bit_position (elt->purpose); | |
7982 | /* We can't compare fields here because there might be empty | |
7983 | fields in between. */ | |
7984 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
7985 | { | |
7986 | constructor_unfilled_fields = elt->purpose; | |
b9c74b4d | 7987 | output_init_element (elt->value, elt->origtype, true, |
7988 | TREE_TYPE (elt->purpose), | |
759791ee | 7989 | elt->purpose, 0, false, |
7990 | braced_init_obstack); | |
276beea2 | 7991 | } |
7992 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
7993 | { | |
7994 | /* Advance to the next smaller node. */ | |
7995 | if (elt->left) | |
7996 | elt = elt->left; | |
7997 | else | |
7998 | { | |
7999 | /* We have reached the smallest node bigger than the | |
8000 | current unfilled field. Fill the space first. */ | |
8001 | next = elt->purpose; | |
8002 | break; | |
8003 | } | |
8004 | } | |
8005 | else | |
8006 | { | |
8007 | /* Advance to the next bigger node. */ | |
8008 | if (elt->right) | |
8009 | elt = elt->right; | |
8010 | else | |
8011 | { | |
8012 | /* We have reached the biggest node in a subtree. Find | |
8013 | the parent of it, which is the next bigger node. */ | |
8014 | while (elt->parent && elt->parent->right == elt) | |
8015 | elt = elt->parent; | |
8016 | elt = elt->parent; | |
8017 | if (elt | |
8018 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
8019 | bit_position (elt->purpose)))) | |
8020 | { | |
8021 | next = elt->purpose; | |
8022 | break; | |
8023 | } | |
8024 | } | |
8025 | } | |
8026 | } | |
8027 | } | |
fc698c9d | 8028 | |
276beea2 | 8029 | /* Ordinarily return, but not if we want to output all |
8030 | and there are elements left. */ | |
84166705 | 8031 | if (!(all && next != 0)) |
c2e053d8 | 8032 | return; |
8033 | ||
276beea2 | 8034 | /* If it's not incremental, just skip over the gap, so that after |
8035 | jumping to retry we will output the next successive element. */ | |
8036 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
8037 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8038 | constructor_unfilled_fields = next; | |
8039 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8040 | constructor_unfilled_index = next; | |
fc698c9d | 8041 | |
276beea2 | 8042 | /* ELT now points to the node in the pending tree with the next |
8043 | initializer to output. */ | |
8044 | goto retry; | |
fc698c9d | 8045 | } |
8046 | \f | |
276beea2 | 8047 | /* Add one non-braced element to the current constructor level. |
8048 | This adjusts the current position within the constructor's type. | |
8049 | This may also start or terminate implicit levels | |
8050 | to handle a partly-braced initializer. | |
997d68fe | 8051 | |
276beea2 | 8052 | Once this has found the correct level for the new element, |
d241bf0d | 8053 | it calls output_init_element. |
8054 | ||
8055 | IMPLICIT is true if value comes from pop_init_level (1), | |
8056 | the new initializer has been merged with the existing one | |
8057 | and thus no warnings should be emitted about overriding an | |
8058 | existing initializer. */ | |
276beea2 | 8059 | |
8060 | void | |
759791ee | 8061 | process_init_element (struct c_expr value, bool implicit, |
8062 | struct obstack * braced_init_obstack) | |
997d68fe | 8063 | { |
3ad5f027 | 8064 | tree orig_value = value.value; |
8065 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
8066 | bool strict_string = value.original_code == STRING_CST; | |
997d68fe | 8067 | |
276beea2 | 8068 | designator_depth = 0; |
ebe39827 | 8069 | designator_erroneous = 0; |
997d68fe | 8070 | |
276beea2 | 8071 | /* Handle superfluous braces around string cst as in |
8072 | char x[] = {"foo"}; */ | |
8073 | if (string_flag | |
8074 | && constructor_type | |
8075 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
e91a0d39 | 8076 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
276beea2 | 8077 | && integer_zerop (constructor_unfilled_index)) |
997d68fe | 8078 | { |
3ad5f027 | 8079 | if (constructor_stack->replacement_value.value) |
a0c938f0 | 8080 | error_init ("excess elements in char array initializer"); |
276beea2 | 8081 | constructor_stack->replacement_value = value; |
8082 | return; | |
997d68fe | 8083 | } |
989bbcfc | 8084 | |
3ad5f027 | 8085 | if (constructor_stack->replacement_value.value != 0) |
276beea2 | 8086 | { |
8087 | error_init ("excess elements in struct initializer"); | |
8088 | return; | |
997d68fe | 8089 | } |
8090 | ||
276beea2 | 8091 | /* Ignore elements of a brace group if it is entirely superfluous |
8092 | and has already been diagnosed. */ | |
8093 | if (constructor_type == 0) | |
8094 | return; | |
997d68fe | 8095 | |
276beea2 | 8096 | /* If we've exhausted any levels that didn't have braces, |
8097 | pop them now. */ | |
8098 | while (constructor_stack->implicit) | |
8099 | { | |
8100 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
8101 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8102 | && constructor_fields == 0) | |
759791ee | 8103 | process_init_element (pop_init_level (1, braced_init_obstack), |
8104 | true, braced_init_obstack); | |
415528ef | 8105 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE |
8106 | || TREE_CODE (constructor_type) == VECTOR_TYPE) | |
276beea2 | 8107 | && (constructor_max_index == 0 |
8108 | || tree_int_cst_lt (constructor_max_index, | |
8109 | constructor_index))) | |
759791ee | 8110 | process_init_element (pop_init_level (1, braced_init_obstack), |
8111 | true, braced_init_obstack); | |
276beea2 | 8112 | else |
8113 | break; | |
8114 | } | |
997d68fe | 8115 | |
276beea2 | 8116 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
8117 | if (constructor_range_stack) | |
997d68fe | 8118 | { |
276beea2 | 8119 | /* If value is a compound literal and we'll be just using its |
8120 | content, don't put it into a SAVE_EXPR. */ | |
3ad5f027 | 8121 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
276beea2 | 8122 | || !require_constant_value |
8123 | || flag_isoc99) | |
c6418a4e | 8124 | { |
8125 | tree semantic_type = NULL_TREE; | |
8126 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
8127 | { | |
8128 | semantic_type = TREE_TYPE (value.value); | |
8129 | value.value = TREE_OPERAND (value.value, 0); | |
8130 | } | |
8131 | value.value = c_save_expr (value.value); | |
8132 | if (semantic_type) | |
8133 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
8134 | value.value); | |
8135 | } | |
276beea2 | 8136 | } |
997d68fe | 8137 | |
276beea2 | 8138 | while (1) |
8139 | { | |
8140 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
997d68fe | 8141 | { |
276beea2 | 8142 | tree fieldtype; |
8143 | enum tree_code fieldcode; | |
997d68fe | 8144 | |
276beea2 | 8145 | if (constructor_fields == 0) |
8146 | { | |
21ca8540 | 8147 | pedwarn_init (input_location, 0, |
8148 | "excess elements in struct initializer"); | |
276beea2 | 8149 | break; |
8150 | } | |
997d68fe | 8151 | |
276beea2 | 8152 | fieldtype = TREE_TYPE (constructor_fields); |
8153 | if (fieldtype != error_mark_node) | |
8154 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8155 | fieldcode = TREE_CODE (fieldtype); | |
997d68fe | 8156 | |
276beea2 | 8157 | /* Error for non-static initialization of a flexible array member. */ |
8158 | if (fieldcode == ARRAY_TYPE | |
8159 | && !require_constant_value | |
8160 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
d059ef79 | 8161 | && DECL_CHAIN (constructor_fields) == NULL_TREE) |
276beea2 | 8162 | { |
8163 | error_init ("non-static initialization of a flexible array member"); | |
8164 | break; | |
8165 | } | |
997d68fe | 8166 | |
276beea2 | 8167 | /* Accept a string constant to initialize a subarray. */ |
3ad5f027 | 8168 | if (value.value != 0 |
276beea2 | 8169 | && fieldcode == ARRAY_TYPE |
e91a0d39 | 8170 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
276beea2 | 8171 | && string_flag) |
3ad5f027 | 8172 | value.value = orig_value; |
276beea2 | 8173 | /* Otherwise, if we have come to a subaggregate, |
8174 | and we don't have an element of its type, push into it. */ | |
d585e4c9 | 8175 | else if (value.value != 0 |
3ad5f027 | 8176 | && value.value != error_mark_node |
8177 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
276beea2 | 8178 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
415528ef | 8179 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
276beea2 | 8180 | { |
759791ee | 8181 | push_init_level (1, braced_init_obstack); |
276beea2 | 8182 | continue; |
8183 | } | |
997d68fe | 8184 | |
3ad5f027 | 8185 | if (value.value) |
276beea2 | 8186 | { |
8187 | push_member_name (constructor_fields); | |
b9c74b4d | 8188 | output_init_element (value.value, value.original_type, |
8189 | strict_string, fieldtype, | |
759791ee | 8190 | constructor_fields, 1, implicit, |
8191 | braced_init_obstack); | |
276beea2 | 8192 | RESTORE_SPELLING_DEPTH (constructor_depth); |
997d68fe | 8193 | } |
8194 | else | |
276beea2 | 8195 | /* Do the bookkeeping for an element that was |
8196 | directly output as a constructor. */ | |
997d68fe | 8197 | { |
276beea2 | 8198 | /* For a record, keep track of end position of last field. */ |
8199 | if (DECL_SIZE (constructor_fields)) | |
a0c938f0 | 8200 | constructor_bit_index |
389dd41b | 8201 | = size_binop_loc (input_location, PLUS_EXPR, |
8202 | bit_position (constructor_fields), | |
8203 | DECL_SIZE (constructor_fields)); | |
276beea2 | 8204 | |
8205 | /* If the current field was the first one not yet written out, | |
8206 | it isn't now, so update. */ | |
8207 | if (constructor_unfilled_fields == constructor_fields) | |
8208 | { | |
1767a056 | 8209 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
276beea2 | 8210 | /* Skip any nameless bit fields. */ |
8211 | while (constructor_unfilled_fields != 0 | |
8212 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8213 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8214 | constructor_unfilled_fields = | |
1767a056 | 8215 | DECL_CHAIN (constructor_unfilled_fields); |
276beea2 | 8216 | } |
997d68fe | 8217 | } |
276beea2 | 8218 | |
1767a056 | 8219 | constructor_fields = DECL_CHAIN (constructor_fields); |
276beea2 | 8220 | /* Skip any nameless bit fields at the beginning. */ |
8221 | while (constructor_fields != 0 | |
8222 | && DECL_C_BIT_FIELD (constructor_fields) | |
8223 | && DECL_NAME (constructor_fields) == 0) | |
1767a056 | 8224 | constructor_fields = DECL_CHAIN (constructor_fields); |
997d68fe | 8225 | } |
276beea2 | 8226 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
997d68fe | 8227 | { |
276beea2 | 8228 | tree fieldtype; |
8229 | enum tree_code fieldcode; | |
997d68fe | 8230 | |
276beea2 | 8231 | if (constructor_fields == 0) |
8232 | { | |
21ca8540 | 8233 | pedwarn_init (input_location, 0, |
8234 | "excess elements in union initializer"); | |
276beea2 | 8235 | break; |
8236 | } | |
997d68fe | 8237 | |
276beea2 | 8238 | fieldtype = TREE_TYPE (constructor_fields); |
8239 | if (fieldtype != error_mark_node) | |
8240 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8241 | fieldcode = TREE_CODE (fieldtype); | |
997d68fe | 8242 | |
276beea2 | 8243 | /* Warn that traditional C rejects initialization of unions. |
8244 | We skip the warning if the value is zero. This is done | |
8245 | under the assumption that the zero initializer in user | |
8246 | code appears conditioned on e.g. __STDC__ to avoid | |
8247 | "missing initializer" warnings and relies on default | |
8248 | initialization to zero in the traditional C case. | |
8249 | We also skip the warning if the initializer is designated, | |
8250 | again on the assumption that this must be conditional on | |
8251 | __STDC__ anyway (and we've already complained about the | |
8252 | member-designator already). */ | |
6bf97f82 | 8253 | if (!in_system_header && !constructor_designated |
3ad5f027 | 8254 | && !(value.value && (integer_zerop (value.value) |
8255 | || real_zerop (value.value)))) | |
6bf97f82 | 8256 | warning (OPT_Wtraditional, "traditional C rejects initialization " |
8257 | "of unions"); | |
997d68fe | 8258 | |
276beea2 | 8259 | /* Accept a string constant to initialize a subarray. */ |
3ad5f027 | 8260 | if (value.value != 0 |
276beea2 | 8261 | && fieldcode == ARRAY_TYPE |
e91a0d39 | 8262 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
276beea2 | 8263 | && string_flag) |
3ad5f027 | 8264 | value.value = orig_value; |
276beea2 | 8265 | /* Otherwise, if we have come to a subaggregate, |
8266 | and we don't have an element of its type, push into it. */ | |
d585e4c9 | 8267 | else if (value.value != 0 |
3ad5f027 | 8268 | && value.value != error_mark_node |
8269 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
276beea2 | 8270 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
415528ef | 8271 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
276beea2 | 8272 | { |
759791ee | 8273 | push_init_level (1, braced_init_obstack); |
276beea2 | 8274 | continue; |
8275 | } | |
997d68fe | 8276 | |
3ad5f027 | 8277 | if (value.value) |
276beea2 | 8278 | { |
8279 | push_member_name (constructor_fields); | |
b9c74b4d | 8280 | output_init_element (value.value, value.original_type, |
8281 | strict_string, fieldtype, | |
759791ee | 8282 | constructor_fields, 1, implicit, |
8283 | braced_init_obstack); | |
276beea2 | 8284 | RESTORE_SPELLING_DEPTH (constructor_depth); |
997d68fe | 8285 | } |
8286 | else | |
276beea2 | 8287 | /* Do the bookkeeping for an element that was |
8288 | directly output as a constructor. */ | |
997d68fe | 8289 | { |
276beea2 | 8290 | constructor_bit_index = DECL_SIZE (constructor_fields); |
d059ef79 | 8291 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
997d68fe | 8292 | } |
997d68fe | 8293 | |
276beea2 | 8294 | constructor_fields = 0; |
8295 | } | |
8296 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8297 | { | |
8298 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8299 | enum tree_code eltcode = TREE_CODE (elttype); | |
997d68fe | 8300 | |
276beea2 | 8301 | /* Accept a string constant to initialize a subarray. */ |
3ad5f027 | 8302 | if (value.value != 0 |
276beea2 | 8303 | && eltcode == ARRAY_TYPE |
e91a0d39 | 8304 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
276beea2 | 8305 | && string_flag) |
3ad5f027 | 8306 | value.value = orig_value; |
276beea2 | 8307 | /* Otherwise, if we have come to a subaggregate, |
8308 | and we don't have an element of its type, push into it. */ | |
d585e4c9 | 8309 | else if (value.value != 0 |
3ad5f027 | 8310 | && value.value != error_mark_node |
8311 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
276beea2 | 8312 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
415528ef | 8313 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) |
276beea2 | 8314 | { |
759791ee | 8315 | push_init_level (1, braced_init_obstack); |
276beea2 | 8316 | continue; |
8317 | } | |
989bbcfc | 8318 | |
276beea2 | 8319 | if (constructor_max_index != 0 |
8320 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
8321 | || integer_all_onesp (constructor_max_index))) | |
8322 | { | |
21ca8540 | 8323 | pedwarn_init (input_location, 0, |
8324 | "excess elements in array initializer"); | |
276beea2 | 8325 | break; |
8326 | } | |
989bbcfc | 8327 | |
276beea2 | 8328 | /* Now output the actual element. */ |
3ad5f027 | 8329 | if (value.value) |
276beea2 | 8330 | { |
4aeeff51 | 8331 | push_array_bounds (tree_low_cst (constructor_index, 1)); |
b9c74b4d | 8332 | output_init_element (value.value, value.original_type, |
8333 | strict_string, elttype, | |
759791ee | 8334 | constructor_index, 1, implicit, |
8335 | braced_init_obstack); | |
276beea2 | 8336 | RESTORE_SPELLING_DEPTH (constructor_depth); |
8337 | } | |
2b30d46c | 8338 | |
276beea2 | 8339 | constructor_index |
389dd41b | 8340 | = size_binop_loc (input_location, PLUS_EXPR, |
8341 | constructor_index, bitsize_one_node); | |
989bbcfc | 8342 | |
3ad5f027 | 8343 | if (!value.value) |
276beea2 | 8344 | /* If we are doing the bookkeeping for an element that was |
8345 | directly output as a constructor, we must update | |
8346 | constructor_unfilled_index. */ | |
8347 | constructor_unfilled_index = constructor_index; | |
8348 | } | |
8349 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8350 | { | |
8351 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
989bbcfc | 8352 | |
a0c938f0 | 8353 | /* Do a basic check of initializer size. Note that vectors |
8354 | always have a fixed size derived from their type. */ | |
276beea2 | 8355 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) |
8356 | { | |
21ca8540 | 8357 | pedwarn_init (input_location, 0, |
8358 | "excess elements in vector initializer"); | |
276beea2 | 8359 | break; |
8360 | } | |
989bbcfc | 8361 | |
276beea2 | 8362 | /* Now output the actual element. */ |
3ad5f027 | 8363 | if (value.value) |
415528ef | 8364 | { |
8365 | if (TREE_CODE (value.value) == VECTOR_CST) | |
8366 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
8367 | output_init_element (value.value, value.original_type, | |
8368 | strict_string, elttype, | |
759791ee | 8369 | constructor_index, 1, implicit, |
8370 | braced_init_obstack); | |
415528ef | 8371 | } |
989bbcfc | 8372 | |
276beea2 | 8373 | constructor_index |
389dd41b | 8374 | = size_binop_loc (input_location, |
8375 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
989bbcfc | 8376 | |
3ad5f027 | 8377 | if (!value.value) |
276beea2 | 8378 | /* If we are doing the bookkeeping for an element that was |
8379 | directly output as a constructor, we must update | |
8380 | constructor_unfilled_index. */ | |
8381 | constructor_unfilled_index = constructor_index; | |
8382 | } | |
989bbcfc | 8383 | |
276beea2 | 8384 | /* Handle the sole element allowed in a braced initializer |
8385 | for a scalar variable. */ | |
b765c734 | 8386 | else if (constructor_type != error_mark_node |
8387 | && constructor_fields == 0) | |
989bbcfc | 8388 | { |
21ca8540 | 8389 | pedwarn_init (input_location, 0, |
8390 | "excess elements in scalar initializer"); | |
276beea2 | 8391 | break; |
989bbcfc | 8392 | } |
8393 | else | |
8394 | { | |
3ad5f027 | 8395 | if (value.value) |
b9c74b4d | 8396 | output_init_element (value.value, value.original_type, |
8397 | strict_string, constructor_type, | |
759791ee | 8398 | NULL_TREE, 1, implicit, |
8399 | braced_init_obstack); | |
276beea2 | 8400 | constructor_fields = 0; |
989bbcfc | 8401 | } |
8402 | ||
276beea2 | 8403 | /* Handle range initializers either at this level or anywhere higher |
8404 | in the designator stack. */ | |
8405 | if (constructor_range_stack) | |
989bbcfc | 8406 | { |
276beea2 | 8407 | struct constructor_range_stack *p, *range_stack; |
8408 | int finish = 0; | |
8409 | ||
8410 | range_stack = constructor_range_stack; | |
8411 | constructor_range_stack = 0; | |
8412 | while (constructor_stack != range_stack->stack) | |
989bbcfc | 8413 | { |
231bd014 | 8414 | gcc_assert (constructor_stack->implicit); |
759791ee | 8415 | process_init_element (pop_init_level (1, |
8416 | braced_init_obstack), | |
8417 | true, braced_init_obstack); | |
989bbcfc | 8418 | } |
276beea2 | 8419 | for (p = range_stack; |
8420 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
8421 | p = p->prev) | |
989bbcfc | 8422 | { |
231bd014 | 8423 | gcc_assert (constructor_stack->implicit); |
759791ee | 8424 | process_init_element (pop_init_level (1, braced_init_obstack), |
8425 | true, braced_init_obstack); | |
989bbcfc | 8426 | } |
276beea2 | 8427 | |
389dd41b | 8428 | p->index = size_binop_loc (input_location, |
8429 | PLUS_EXPR, p->index, bitsize_one_node); | |
276beea2 | 8430 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) |
8431 | finish = 1; | |
8432 | ||
8433 | while (1) | |
8434 | { | |
8435 | constructor_index = p->index; | |
8436 | constructor_fields = p->fields; | |
8437 | if (finish && p->range_end && p->index == p->range_start) | |
8438 | { | |
8439 | finish = 0; | |
8440 | p->prev = 0; | |
8441 | } | |
8442 | p = p->next; | |
8443 | if (!p) | |
8444 | break; | |
759791ee | 8445 | push_init_level (2, braced_init_obstack); |
276beea2 | 8446 | p->stack = constructor_stack; |
8447 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
8448 | p->index = p->range_start; | |
8449 | } | |
8450 | ||
8451 | if (!finish) | |
8452 | constructor_range_stack = range_stack; | |
8453 | continue; | |
989bbcfc | 8454 | } |
8455 | ||
276beea2 | 8456 | break; |
989bbcfc | 8457 | } |
8458 | ||
276beea2 | 8459 | constructor_range_stack = 0; |
8460 | } | |
8461 | \f | |
fae5a040 | 8462 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
8463 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
226c2452 | 8464 | an ASM_EXPR node). */ |
276beea2 | 8465 | tree |
fae5a040 | 8466 | build_asm_stmt (tree cv_qualifier, tree args) |
276beea2 | 8467 | { |
4ee9c684 | 8468 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
8469 | ASM_VOLATILE_P (args) = 1; | |
fae5a040 | 8470 | return add_stmt (args); |
989bbcfc | 8471 | } |
8472 | ||
fae5a040 | 8473 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
8474 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
8475 | SIMPLE indicates whether there was anything at all after the | |
8476 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
226c2452 | 8477 | are subtly different. We use a ASM_EXPR node to represent this. */ |
276beea2 | 8478 | tree |
e60a6f7b | 8479 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, |
78f55ca8 | 8480 | tree clobbers, tree labels, bool simple) |
997d68fe | 8481 | { |
276beea2 | 8482 | tree tail; |
fae5a040 | 8483 | tree args; |
4ee9c684 | 8484 | int i; |
8485 | const char *constraint; | |
8b0d0af6 | 8486 | const char **oconstraints; |
4ee9c684 | 8487 | bool allows_mem, allows_reg, is_inout; |
8b0d0af6 | 8488 | int ninputs, noutputs; |
4ee9c684 | 8489 | |
8490 | ninputs = list_length (inputs); | |
8491 | noutputs = list_length (outputs); | |
8b0d0af6 | 8492 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); |
8493 | ||
78f55ca8 | 8494 | string = resolve_asm_operand_names (string, outputs, inputs, labels); |
276beea2 | 8495 | |
4ee9c684 | 8496 | /* Remove output conversions that change the type but not the mode. */ |
8497 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
997d68fe | 8498 | { |
276beea2 | 8499 | tree output = TREE_VALUE (tail); |
8b0d0af6 | 8500 | |
8501 | /* ??? Really, this should not be here. Users should be using a | |
8502 | proper lvalue, dammit. But there's a long history of using casts | |
8503 | in the output operands. In cases like longlong.h, this becomes a | |
8504 | primitive form of typechecking -- if the cast can be removed, then | |
8505 | the output operand had a type of the proper width; otherwise we'll | |
8506 | get an error. Gross, but ... */ | |
276beea2 | 8507 | STRIP_NOPS (output); |
8b0d0af6 | 8508 | |
fdd84b77 | 8509 | if (!lvalue_or_else (loc, output, lv_asm)) |
8b0d0af6 | 8510 | output = error_mark_node; |
989bbcfc | 8511 | |
89552023 | 8512 | if (output != error_mark_node |
8513 | && (TREE_READONLY (output) | |
8514 | || TYPE_READONLY (TREE_TYPE (output)) | |
8515 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
8516 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
8517 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
8518 | readonly_error (output, lv_asm); | |
8519 | ||
4ee9c684 | 8520 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
8b0d0af6 | 8521 | oconstraints[i] = constraint; |
8522 | ||
8523 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
8524 | &allows_mem, &allows_reg, &is_inout)) | |
8525 | { | |
8526 | /* If the operand is going to end up in memory, | |
8527 | mark it addressable. */ | |
8528 | if (!allows_reg && !c_mark_addressable (output)) | |
8529 | output = error_mark_node; | |
ef31075b | 8530 | if (!(!allows_reg && allows_mem) |
8531 | && output != error_mark_node | |
8532 | && VOID_TYPE_P (TREE_TYPE (output))) | |
8533 | { | |
8534 | error_at (loc, "invalid use of void expression"); | |
8535 | output = error_mark_node; | |
8536 | } | |
8b0d0af6 | 8537 | } |
8538 | else | |
a0c938f0 | 8539 | output = error_mark_node; |
276beea2 | 8540 | |
8b0d0af6 | 8541 | TREE_VALUE (tail) = output; |
989bbcfc | 8542 | } |
276beea2 | 8543 | |
8b0d0af6 | 8544 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) |
8545 | { | |
8546 | tree input; | |
8547 | ||
8548 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8549 | input = TREE_VALUE (tail); | |
8550 | ||
8b0d0af6 | 8551 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
8552 | oconstraints, &allows_mem, &allows_reg)) | |
8553 | { | |
8554 | /* If the operand is going to end up in memory, | |
8555 | mark it addressable. */ | |
c6e95001 | 8556 | if (!allows_reg && allows_mem) |
8557 | { | |
8558 | /* Strip the nops as we allow this case. FIXME, this really | |
8559 | should be rejected or made deprecated. */ | |
8560 | STRIP_NOPS (input); | |
8561 | if (!c_mark_addressable (input)) | |
8562 | input = error_mark_node; | |
ef31075b | 8563 | } |
8564 | else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) | |
8565 | { | |
8566 | error_at (loc, "invalid use of void expression"); | |
8567 | input = error_mark_node; | |
8568 | } | |
8b0d0af6 | 8569 | } |
8570 | else | |
8571 | input = error_mark_node; | |
8572 | ||
8573 | TREE_VALUE (tail) = input; | |
8574 | } | |
276beea2 | 8575 | |
78f55ca8 | 8576 | /* ASMs with labels cannot have outputs. This should have been |
8577 | enforced by the parser. */ | |
8578 | gcc_assert (outputs == NULL || labels == NULL); | |
8579 | ||
8580 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
fae5a040 | 8581 | |
89552023 | 8582 | /* asm statements without outputs, including simple ones, are treated |
8583 | as volatile. */ | |
8584 | ASM_INPUT_P (args) = simple; | |
8585 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
8b0d0af6 | 8586 | |
fae5a040 | 8587 | return args; |
997d68fe | 8588 | } |
276beea2 | 8589 | \f |
e60a6f7b | 8590 | /* Generate a goto statement to LABEL. LOC is the location of the |
8591 | GOTO. */ | |
0375a275 | 8592 | |
8593 | tree | |
e60a6f7b | 8594 | c_finish_goto_label (location_t loc, tree label) |
0375a275 | 8595 | { |
f805d53d | 8596 | tree decl = lookup_label_for_goto (loc, label); |
0375a275 | 8597 | if (!decl) |
8598 | return NULL_TREE; | |
0375a275 | 8599 | TREE_USED (decl) = 1; |
e60a6f7b | 8600 | { |
8601 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
8602 | SET_EXPR_LOCATION (t, loc); | |
8603 | return add_stmt (t); | |
8604 | } | |
0375a275 | 8605 | } |
8606 | ||
e60a6f7b | 8607 | /* Generate a computed goto statement to EXPR. LOC is the location of |
8608 | the GOTO. */ | |
0375a275 | 8609 | |
8610 | tree | |
e60a6f7b | 8611 | c_finish_goto_ptr (location_t loc, tree expr) |
0375a275 | 8612 | { |
e60a6f7b | 8613 | tree t; |
29438999 | 8614 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); |
a75b1c71 | 8615 | expr = c_fully_fold (expr, false, NULL); |
0375a275 | 8616 | expr = convert (ptr_type_node, expr); |
e60a6f7b | 8617 | t = build1 (GOTO_EXPR, void_type_node, expr); |
8618 | SET_EXPR_LOCATION (t, loc); | |
8619 | return add_stmt (t); | |
0375a275 | 8620 | } |
8621 | ||
c857cd60 | 8622 | /* Generate a C `return' statement. RETVAL is the expression for what |
e60a6f7b | 8623 | to return, or a null pointer for `return;' with no value. LOC is |
8624 | the location of the return statement. If ORIGTYPE is not NULL_TREE, it | |
8625 | is the original type of RETVAL. */ | |
fc698c9d | 8626 | |
0375a275 | 8627 | tree |
e60a6f7b | 8628 | c_finish_return (location_t loc, tree retval, tree origtype) |
276beea2 | 8629 | { |
81a3c55b | 8630 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; |
8631 | bool no_warning = false; | |
a75b1c71 | 8632 | bool npc = false; |
276beea2 | 8633 | |
8634 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
e60a6f7b | 8635 | warning_at (loc, 0, |
8636 | "function declared %<noreturn%> has a %<return%> statement"); | |
276beea2 | 8637 | |
a75b1c71 | 8638 | if (retval) |
8639 | { | |
c6418a4e | 8640 | tree semantic_type = NULL_TREE; |
a75b1c71 | 8641 | npc = null_pointer_constant_p (retval); |
c6418a4e | 8642 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) |
8643 | { | |
8644 | semantic_type = TREE_TYPE (retval); | |
8645 | retval = TREE_OPERAND (retval, 0); | |
8646 | } | |
a75b1c71 | 8647 | retval = c_fully_fold (retval, false, NULL); |
c6418a4e | 8648 | if (semantic_type) |
8649 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
a75b1c71 | 8650 | } |
8651 | ||
276beea2 | 8652 | if (!retval) |
fc698c9d | 8653 | { |
276beea2 | 8654 | current_function_returns_null = 1; |
8655 | if ((warn_return_type || flag_isoc99) | |
8656 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
81a3c55b | 8657 | { |
48e1416a | 8658 | pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type, |
8864917d | 8659 | "%<return%> with no value, in " |
81a3c55b | 8660 | "function returning non-void"); |
8661 | no_warning = true; | |
8662 | } | |
628fa4f8 | 8663 | } |
276beea2 | 8664 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
fc698c9d | 8665 | { |
276beea2 | 8666 | current_function_returns_null = 1; |
6e4e3580 | 8667 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) |
48e1416a | 8668 | pedwarn (loc, 0, |
21ca8540 | 8669 | "%<return%> with a value, in function returning void"); |
48e1416a | 8670 | else |
29438999 | 8671 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
8864917d | 8672 | "%<return%> with expression, in function returning void"); |
fc698c9d | 8673 | } |
276beea2 | 8674 | else |
fc698c9d | 8675 | { |
e60a6f7b | 8676 | tree t = convert_for_assignment (loc, valtype, retval, origtype, |
8677 | ic_return, | |
8678 | npc, NULL_TREE, NULL_TREE, 0); | |
276beea2 | 8679 | tree res = DECL_RESULT (current_function_decl); |
8680 | tree inner; | |
8681 | ||
8682 | current_function_returns_value = 1; | |
8683 | if (t == error_mark_node) | |
0375a275 | 8684 | return NULL_TREE; |
276beea2 | 8685 | |
8686 | inner = t = convert (TREE_TYPE (res), t); | |
8687 | ||
8688 | /* Strip any conversions, additions, and subtractions, and see if | |
8689 | we are returning the address of a local variable. Warn if so. */ | |
8690 | while (1) | |
989bbcfc | 8691 | { |
276beea2 | 8692 | switch (TREE_CODE (inner)) |
989bbcfc | 8693 | { |
e7e7c0fe | 8694 | CASE_CONVERT: |
8695 | case NON_LVALUE_EXPR: | |
276beea2 | 8696 | case PLUS_EXPR: |
e7e7c0fe | 8697 | case POINTER_PLUS_EXPR: |
276beea2 | 8698 | inner = TREE_OPERAND (inner, 0); |
8699 | continue; | |
8700 | ||
8701 | case MINUS_EXPR: | |
8702 | /* If the second operand of the MINUS_EXPR has a pointer | |
8703 | type (or is converted from it), this may be valid, so | |
8704 | don't give a warning. */ | |
8705 | { | |
8706 | tree op1 = TREE_OPERAND (inner, 1); | |
989bbcfc | 8707 | |
84166705 | 8708 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) |
72dd6141 | 8709 | && (CONVERT_EXPR_P (op1) |
8710 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
276beea2 | 8711 | op1 = TREE_OPERAND (op1, 0); |
989bbcfc | 8712 | |
276beea2 | 8713 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
8714 | break; | |
989bbcfc | 8715 | |
276beea2 | 8716 | inner = TREE_OPERAND (inner, 0); |
8717 | continue; | |
8718 | } | |
628fa4f8 | 8719 | |
276beea2 | 8720 | case ADDR_EXPR: |
8721 | inner = TREE_OPERAND (inner, 0); | |
8ee1039f | 8722 | |
ce45a448 | 8723 | while (REFERENCE_CLASS_P (inner) |
a0c938f0 | 8724 | && TREE_CODE (inner) != INDIRECT_REF) |
276beea2 | 8725 | inner = TREE_OPERAND (inner, 0); |
989bbcfc | 8726 | |
aaadf915 | 8727 | if (DECL_P (inner) |
84166705 | 8728 | && !DECL_EXTERNAL (inner) |
8729 | && !TREE_STATIC (inner) | |
276beea2 | 8730 | && DECL_CONTEXT (inner) == current_function_decl) |
e60a6f7b | 8731 | warning_at (loc, |
8732 | 0, "function returns address of local variable"); | |
276beea2 | 8733 | break; |
989bbcfc | 8734 | |
276beea2 | 8735 | default: |
8736 | break; | |
8737 | } | |
fc698c9d | 8738 | |
276beea2 | 8739 | break; |
8740 | } | |
8741 | ||
14ae0310 | 8742 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
e60a6f7b | 8743 | SET_EXPR_LOCATION (retval, loc); |
6513b50d | 8744 | |
8745 | if (warn_sequence_point) | |
8746 | verify_sequence_points (retval); | |
fc698c9d | 8747 | } |
989bbcfc | 8748 | |
e60a6f7b | 8749 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); |
81a3c55b | 8750 | TREE_NO_WARNING (ret_stmt) |= no_warning; |
8751 | return add_stmt (ret_stmt); | |
fc698c9d | 8752 | } |
276beea2 | 8753 | \f |
8754 | struct c_switch { | |
197638d4 | 8755 | /* The SWITCH_EXPR being built. */ |
8756 | tree switch_expr; | |
2ca392fd | 8757 | |
5c9dae64 | 8758 | /* The original type of the testing expression, i.e. before the |
2ca392fd | 8759 | default conversion is applied. */ |
8760 | tree orig_type; | |
8761 | ||
276beea2 | 8762 | /* A splay-tree mapping the low element of a case range to the high |
8763 | element, or NULL_TREE if there is no high element. Used to | |
8764 | determine whether or not a new case label duplicates an old case | |
8765 | label. We need a tree, rather than simply a hash table, because | |
8766 | of the GNU case range extension. */ | |
8767 | splay_tree cases; | |
2ca392fd | 8768 | |
f805d53d | 8769 | /* The bindings at the point of the switch. This is used for |
8770 | warnings crossing decls when branching to a case label. */ | |
8771 | struct c_spot_bindings *bindings; | |
f5d156fd | 8772 | |
276beea2 | 8773 | /* The next node on the stack. */ |
8774 | struct c_switch *next; | |
8775 | }; | |
628fa4f8 | 8776 | |
276beea2 | 8777 | /* A stack of the currently active switch statements. The innermost |
8778 | switch statement is on the top of the stack. There is no need to | |
8779 | mark the stack for garbage collection because it is only active | |
8780 | during the processing of the body of a function, and we never | |
8781 | collect at that point. */ | |
fc698c9d | 8782 | |
0375a275 | 8783 | struct c_switch *c_switch_stack; |
fc698c9d | 8784 | |
276beea2 | 8785 | /* Start a C switch statement, testing expression EXP. Return the new |
e60a6f7b | 8786 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. |
8787 | SWITCH_COND_LOC is the location of the switch's condition. */ | |
fc698c9d | 8788 | |
276beea2 | 8789 | tree |
e60a6f7b | 8790 | c_start_case (location_t switch_loc, |
8791 | location_t switch_cond_loc, | |
8792 | tree exp) | |
fc698c9d | 8793 | { |
55d7e60b | 8794 | tree orig_type = error_mark_node; |
276beea2 | 8795 | struct c_switch *cs; |
2b30d46c | 8796 | |
276beea2 | 8797 | if (exp != error_mark_node) |
fc698c9d | 8798 | { |
276beea2 | 8799 | orig_type = TREE_TYPE (exp); |
8800 | ||
55d7e60b | 8801 | if (!INTEGRAL_TYPE_P (orig_type)) |
fc698c9d | 8802 | { |
55d7e60b | 8803 | if (orig_type != error_mark_node) |
8804 | { | |
e60a6f7b | 8805 | error_at (switch_cond_loc, "switch quantity not an integer"); |
55d7e60b | 8806 | orig_type = error_mark_node; |
8807 | } | |
276beea2 | 8808 | exp = integer_zero_node; |
fc698c9d | 8809 | } |
276beea2 | 8810 | else |
fc698c9d | 8811 | { |
55d7e60b | 8812 | tree type = TYPE_MAIN_VARIANT (orig_type); |
989bbcfc | 8813 | |
6bf97f82 | 8814 | if (!in_system_header |
276beea2 | 8815 | && (type == long_integer_type_node |
8816 | || type == long_unsigned_type_node)) | |
e60a6f7b | 8817 | warning_at (switch_cond_loc, |
8818 | OPT_Wtraditional, "%<long%> switch expression not " | |
8819 | "converted to %<int%> in ISO C"); | |
989bbcfc | 8820 | |
a75b1c71 | 8821 | exp = c_fully_fold (exp, false, NULL); |
276beea2 | 8822 | exp = default_conversion (exp); |
6513b50d | 8823 | |
8824 | if (warn_sequence_point) | |
8825 | verify_sequence_points (exp); | |
276beea2 | 8826 | } |
8827 | } | |
8828 | ||
197638d4 | 8829 | /* Add this new SWITCH_EXPR to the stack. */ |
9318f22c | 8830 | cs = XNEW (struct c_switch); |
197638d4 | 8831 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); |
e60a6f7b | 8832 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); |
2ca392fd | 8833 | cs->orig_type = orig_type; |
276beea2 | 8834 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
f805d53d | 8835 | cs->bindings = c_get_switch_bindings (); |
0375a275 | 8836 | cs->next = c_switch_stack; |
8837 | c_switch_stack = cs; | |
276beea2 | 8838 | |
197638d4 | 8839 | return add_stmt (cs->switch_expr); |
276beea2 | 8840 | } |
8841 | ||
e60a6f7b | 8842 | /* Process a case label at location LOC. */ |
276beea2 | 8843 | |
8844 | tree | |
e60a6f7b | 8845 | do_case (location_t loc, tree low_value, tree high_value) |
276beea2 | 8846 | { |
8847 | tree label = NULL_TREE; | |
8848 | ||
482c1ea0 | 8849 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) |
8850 | { | |
8851 | low_value = c_fully_fold (low_value, false, NULL); | |
8852 | if (TREE_CODE (low_value) == INTEGER_CST) | |
29438999 | 8853 | pedwarn (input_location, OPT_Wpedantic, |
482c1ea0 | 8854 | "case label is not an integer constant expression"); |
8855 | } | |
8856 | ||
8857 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
8858 | { | |
8859 | high_value = c_fully_fold (high_value, false, NULL); | |
8860 | if (TREE_CODE (high_value) == INTEGER_CST) | |
29438999 | 8861 | pedwarn (input_location, OPT_Wpedantic, |
482c1ea0 | 8862 | "case label is not an integer constant expression"); |
8863 | } | |
8864 | ||
f805d53d | 8865 | if (c_switch_stack == NULL) |
f5d156fd | 8866 | { |
8867 | if (low_value) | |
f805d53d | 8868 | error_at (loc, "case label not within a switch statement"); |
f5d156fd | 8869 | else |
f805d53d | 8870 | error_at (loc, "%<default%> label not within a switch statement"); |
8871 | return NULL_TREE; | |
f5d156fd | 8872 | } |
fc698c9d | 8873 | |
f805d53d | 8874 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, |
8875 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
8876 | loc)) | |
8877 | return NULL_TREE; | |
8878 | ||
8879 | label = c_add_case_label (loc, c_switch_stack->cases, | |
8880 | SWITCH_COND (c_switch_stack->switch_expr), | |
8881 | c_switch_stack->orig_type, | |
8882 | low_value, high_value); | |
8883 | if (label == error_mark_node) | |
8884 | label = NULL_TREE; | |
276beea2 | 8885 | return label; |
8886 | } | |
fc698c9d | 8887 | |
276beea2 | 8888 | /* Finish the switch statement. */ |
fc698c9d | 8889 | |
276beea2 | 8890 | void |
2363ef00 | 8891 | c_finish_case (tree body) |
276beea2 | 8892 | { |
0375a275 | 8893 | struct c_switch *cs = c_switch_stack; |
e7911019 | 8894 | location_t switch_location; |
276beea2 | 8895 | |
197638d4 | 8896 | SWITCH_BODY (cs->switch_expr) = body; |
2363ef00 | 8897 | |
4ee9c684 | 8898 | /* Emit warnings as needed. */ |
e60a6f7b | 8899 | switch_location = EXPR_LOCATION (cs->switch_expr); |
e7911019 | 8900 | c_do_switch_warnings (cs->cases, switch_location, |
8901 | TREE_TYPE (cs->switch_expr), | |
8902 | SWITCH_COND (cs->switch_expr)); | |
4ee9c684 | 8903 | |
276beea2 | 8904 | /* Pop the stack. */ |
0375a275 | 8905 | c_switch_stack = cs->next; |
276beea2 | 8906 | splay_tree_delete (cs->cases); |
f805d53d | 8907 | c_release_switch_bindings (cs->bindings); |
9318f22c | 8908 | XDELETE (cs); |
fc698c9d | 8909 | } |
2363ef00 | 8910 | \f |
0375a275 | 8911 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
8912 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
8913 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
8914 | statement, and was not surrounded with parenthesis. */ | |
2363ef00 | 8915 | |
b576c0c1 | 8916 | void |
0375a275 | 8917 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
8918 | tree else_block, bool nested_if) | |
2363ef00 | 8919 | { |
0375a275 | 8920 | tree stmt; |
2363ef00 | 8921 | |
0375a275 | 8922 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
8923 | if (warn_parentheses && nested_if && else_block == NULL) | |
2363ef00 | 8924 | { |
0375a275 | 8925 | tree inner_if = then_block; |
b6bbe4a7 | 8926 | |
2c763ed4 | 8927 | /* We know from the grammar productions that there is an IF nested |
0375a275 | 8928 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
8929 | it might not be exactly THEN_BLOCK, but should be the last | |
8930 | non-container statement within. */ | |
8931 | while (1) | |
8932 | switch (TREE_CODE (inner_if)) | |
8933 | { | |
8934 | case COND_EXPR: | |
8935 | goto found; | |
8936 | case BIND_EXPR: | |
8937 | inner_if = BIND_EXPR_BODY (inner_if); | |
8938 | break; | |
8939 | case STATEMENT_LIST: | |
8940 | inner_if = expr_last (then_block); | |
8941 | break; | |
8942 | case TRY_FINALLY_EXPR: | |
8943 | case TRY_CATCH_EXPR: | |
8944 | inner_if = TREE_OPERAND (inner_if, 0); | |
8945 | break; | |
8946 | default: | |
231bd014 | 8947 | gcc_unreachable (); |
0375a275 | 8948 | } |
8949 | found: | |
b6bbe4a7 | 8950 | |
0375a275 | 8951 | if (COND_EXPR_ELSE (inner_if)) |
5fb6a912 | 8952 | warning_at (if_locus, OPT_Wparentheses, |
8953 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
0375a275 | 8954 | } |
b6bbe4a7 | 8955 | |
fee9f7da | 8956 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); |
2ed8b5d0 | 8957 | SET_EXPR_LOCATION (stmt, if_locus); |
0375a275 | 8958 | add_stmt (stmt); |
2363ef00 | 8959 | } |
8960 | ||
0375a275 | 8961 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
8962 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
8963 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
2c763ed4 | 8964 | the statement controlled by the loop. BLAB is the break label. CLAB is |
0375a275 | 8965 | the continue label. Everything is allowed to be NULL. */ |
2363ef00 | 8966 | |
8967 | void | |
0375a275 | 8968 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
8969 | tree blab, tree clab, bool cond_is_first) | |
2363ef00 | 8970 | { |
0375a275 | 8971 | tree entry = NULL, exit = NULL, t; |
8972 | ||
3959c2d9 | 8973 | /* If the condition is zero don't generate a loop construct. */ |
8974 | if (cond && integer_zerop (cond)) | |
8975 | { | |
8976 | if (cond_is_first) | |
8977 | { | |
8978 | t = build_and_jump (&blab); | |
8979 | SET_EXPR_LOCATION (t, start_locus); | |
8980 | add_stmt (t); | |
8981 | } | |
8982 | } | |
8983 | else | |
0375a275 | 8984 | { |
8985 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
a0c938f0 | 8986 | |
0375a275 | 8987 | /* If we have an exit condition, then we build an IF with gotos either |
a0c938f0 | 8988 | out of the loop, or to the top of it. If there's no exit condition, |
8989 | then we just build a jump back to the top. */ | |
0375a275 | 8990 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); |
a0c938f0 | 8991 | |
3959c2d9 | 8992 | if (cond && !integer_nonzerop (cond)) |
a0c938f0 | 8993 | { |
8994 | /* Canonicalize the loop condition to the end. This means | |
8995 | generating a branch to the loop condition. Reuse the | |
8996 | continue label, if possible. */ | |
8997 | if (cond_is_first) | |
8998 | { | |
8999 | if (incr || !clab) | |
9000 | { | |
9001 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
9002 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
9003 | } | |
9004 | else | |
9005 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
2ed8b5d0 | 9006 | SET_EXPR_LOCATION (t, start_locus); |
a0c938f0 | 9007 | add_stmt (t); |
9008 | } | |
9009 | ||
0375a275 | 9010 | t = build_and_jump (&blab); |
0375a275 | 9011 | if (cond_is_first) |
389dd41b | 9012 | exit = fold_build3_loc (start_locus, |
9013 | COND_EXPR, void_type_node, cond, exit, t); | |
0375a275 | 9014 | else |
389dd41b | 9015 | exit = fold_build3_loc (input_location, |
9016 | COND_EXPR, void_type_node, cond, exit, t); | |
a0c938f0 | 9017 | } |
9018 | ||
0375a275 | 9019 | add_stmt (top); |
9020 | } | |
a0c938f0 | 9021 | |
0375a275 | 9022 | if (body) |
9023 | add_stmt (body); | |
9024 | if (clab) | |
9025 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
9026 | if (incr) | |
9027 | add_stmt (incr); | |
9028 | if (entry) | |
9029 | add_stmt (entry); | |
9030 | if (exit) | |
9031 | add_stmt (exit); | |
9032 | if (blab) | |
9033 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
2363ef00 | 9034 | } |
2363ef00 | 9035 | |
9036 | tree | |
e60a6f7b | 9037 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) |
2363ef00 | 9038 | { |
7f0f308d | 9039 | bool skip; |
0375a275 | 9040 | tree label = *label_p; |
2363ef00 | 9041 | |
7f0f308d | 9042 | /* In switch statements break is sometimes stylistically used after |
9043 | a return statement. This can lead to spurious warnings about | |
9044 | control reaching the end of a non-void function when it is | |
9045 | inlined. Note that we are calling block_may_fallthru with | |
9046 | language specific tree nodes; this works because | |
9047 | block_may_fallthru returns true when given something it does not | |
9048 | understand. */ | |
9049 | skip = !block_may_fallthru (cur_stmt_list); | |
9050 | ||
0375a275 | 9051 | if (!label) |
7f0f308d | 9052 | { |
9053 | if (!skip) | |
e60a6f7b | 9054 | *label_p = label = create_artificial_label (loc); |
7f0f308d | 9055 | } |
1e8e9920 | 9056 | else if (TREE_CODE (label) == LABEL_DECL) |
9057 | ; | |
9058 | else switch (TREE_INT_CST_LOW (label)) | |
0375a275 | 9059 | { |
1e8e9920 | 9060 | case 0: |
0375a275 | 9061 | if (is_break) |
e60a6f7b | 9062 | error_at (loc, "break statement not within loop or switch"); |
0375a275 | 9063 | else |
e60a6f7b | 9064 | error_at (loc, "continue statement not within a loop"); |
0375a275 | 9065 | return NULL_TREE; |
1e8e9920 | 9066 | |
9067 | case 1: | |
9068 | gcc_assert (is_break); | |
e60a6f7b | 9069 | error_at (loc, "break statement used with OpenMP for loop"); |
1e8e9920 | 9070 | return NULL_TREE; |
9071 | ||
9072 | default: | |
9073 | gcc_unreachable (); | |
0375a275 | 9074 | } |
2363ef00 | 9075 | |
7f0f308d | 9076 | if (skip) |
9077 | return NULL_TREE; | |
9078 | ||
4a1849e3 | 9079 | if (!is_break) |
9080 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
9081 | ||
14ae0310 | 9082 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
2363ef00 | 9083 | } |
9084 | ||
0375a275 | 9085 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
2569a1be | 9086 | |
9087 | static void | |
e60a6f7b | 9088 | emit_side_effect_warnings (location_t loc, tree expr) |
2569a1be | 9089 | { |
29eda12a | 9090 | if (expr == error_mark_node) |
9091 | ; | |
9092 | else if (!TREE_SIDE_EFFECTS (expr)) | |
2569a1be | 9093 | { |
9094 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
e60a6f7b | 9095 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); |
2569a1be | 9096 | } |
43667bd3 | 9097 | else |
e60a6f7b | 9098 | warn_if_unused_value (expr, loc); |
2569a1be | 9099 | } |
9100 | ||
0375a275 | 9101 | /* Process an expression as if it were a complete statement. Emit |
e60a6f7b | 9102 | diagnostics, but do not call ADD_STMT. LOC is the location of the |
9103 | statement. */ | |
2569a1be | 9104 | |
0375a275 | 9105 | tree |
e60a6f7b | 9106 | c_process_expr_stmt (location_t loc, tree expr) |
2569a1be | 9107 | { |
b2af2a93 | 9108 | tree exprv; |
9109 | ||
2569a1be | 9110 | if (!expr) |
0375a275 | 9111 | return NULL_TREE; |
2569a1be | 9112 | |
a75b1c71 | 9113 | expr = c_fully_fold (expr, false, NULL); |
9114 | ||
2569a1be | 9115 | if (warn_sequence_point) |
9116 | verify_sequence_points (expr); | |
9117 | ||
9118 | if (TREE_TYPE (expr) != error_mark_node | |
9119 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
9120 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
e60a6f7b | 9121 | error_at (loc, "expression statement has incomplete type"); |
2569a1be | 9122 | |
9123 | /* If we're not processing a statement expression, warn about unused values. | |
9124 | Warnings for statement expressions will be emitted later, once we figure | |
9125 | out which is the result. */ | |
9126 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
43667bd3 | 9127 | && warn_unused_value) |
e60a6f7b | 9128 | emit_side_effect_warnings (loc, expr); |
2569a1be | 9129 | |
b2af2a93 | 9130 | exprv = expr; |
9131 | while (TREE_CODE (exprv) == COMPOUND_EXPR) | |
9132 | exprv = TREE_OPERAND (exprv, 1); | |
0902fd73 | 9133 | while (CONVERT_EXPR_P (exprv)) |
9134 | exprv = TREE_OPERAND (exprv, 0); | |
9135 | if (DECL_P (exprv) | |
9136 | || handled_component_p (exprv) | |
9137 | || TREE_CODE (exprv) == ADDR_EXPR) | |
b2af2a93 | 9138 | mark_exp_read (exprv); |
7cb9c999 | 9139 | |
2569a1be | 9140 | /* If the expression is not of a type to which we cannot assign a line |
9141 | number, wrap the thing in a no-op NOP_EXPR. */ | |
ce45a448 | 9142 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) |
e60a6f7b | 9143 | { |
9144 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
9145 | SET_EXPR_LOCATION (expr, loc); | |
9146 | } | |
0375a275 | 9147 | |
9148 | return expr; | |
9149 | } | |
9150 | ||
e60a6f7b | 9151 | /* Emit an expression as a statement. LOC is the location of the |
9152 | expression. */ | |
0375a275 | 9153 | |
9154 | tree | |
e60a6f7b | 9155 | c_finish_expr_stmt (location_t loc, tree expr) |
0375a275 | 9156 | { |
9157 | if (expr) | |
e60a6f7b | 9158 | return add_stmt (c_process_expr_stmt (loc, expr)); |
0375a275 | 9159 | else |
9160 | return NULL; | |
2569a1be | 9161 | } |
9162 | ||
9163 | /* Do the opposite and emit a statement as an expression. To begin, | |
9164 | create a new binding level and return it. */ | |
2363ef00 | 9165 | |
9166 | tree | |
9167 | c_begin_stmt_expr (void) | |
9168 | { | |
9169 | tree ret; | |
9170 | ||
9171 | /* We must force a BLOCK for this level so that, if it is not expanded | |
9172 | later, there is a way to turn off the entire subtree of blocks that | |
9173 | are contained in it. */ | |
9174 | keep_next_level (); | |
9175 | ret = c_begin_compound_stmt (true); | |
f805d53d | 9176 | |
9177 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
9178 | ? NULL | |
9179 | : c_switch_stack->bindings); | |
2363ef00 | 9180 | |
9181 | /* Mark the current statement list as belonging to a statement list. */ | |
9182 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
9183 | ||
9184 | return ret; | |
9185 | } | |
9186 | ||
e60a6f7b | 9187 | /* LOC is the location of the compound statement to which this body |
9188 | belongs. */ | |
9189 | ||
2363ef00 | 9190 | tree |
e60a6f7b | 9191 | c_finish_stmt_expr (location_t loc, tree body) |
2363ef00 | 9192 | { |
2569a1be | 9193 | tree last, type, tmp, val; |
2363ef00 | 9194 | tree *last_p; |
9195 | ||
e60a6f7b | 9196 | body = c_end_compound_stmt (loc, body, true); |
f805d53d | 9197 | |
9198 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
9199 | ? NULL | |
9200 | : c_switch_stack->bindings); | |
2363ef00 | 9201 | |
2569a1be | 9202 | /* Locate the last statement in BODY. See c_end_compound_stmt |
9203 | about always returning a BIND_EXPR. */ | |
9204 | last_p = &BIND_EXPR_BODY (body); | |
9205 | last = BIND_EXPR_BODY (body); | |
9206 | ||
9207 | continue_searching: | |
2363ef00 | 9208 | if (TREE_CODE (last) == STATEMENT_LIST) |
9209 | { | |
2569a1be | 9210 | tree_stmt_iterator i; |
9211 | ||
9212 | /* This can happen with degenerate cases like ({ }). No value. */ | |
9213 | if (!TREE_SIDE_EFFECTS (last)) | |
9214 | return body; | |
9215 | ||
9216 | /* If we're supposed to generate side effects warnings, process | |
9217 | all of the statements except the last. */ | |
43667bd3 | 9218 | if (warn_unused_value) |
2363ef00 | 9219 | { |
2569a1be | 9220 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
e60a6f7b | 9221 | { |
9222 | location_t tloc; | |
9223 | tree t = tsi_stmt (i); | |
9224 | ||
9225 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
9226 | emit_side_effect_warnings (tloc, t); | |
9227 | } | |
2363ef00 | 9228 | } |
9229 | else | |
2569a1be | 9230 | i = tsi_last (last); |
9231 | last_p = tsi_stmt_ptr (i); | |
9232 | last = *last_p; | |
2363ef00 | 9233 | } |
9234 | ||
2569a1be | 9235 | /* If the end of the list is exception related, then the list was split |
9236 | by a call to push_cleanup. Continue searching. */ | |
9237 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
9238 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
9239 | { | |
9240 | last_p = &TREE_OPERAND (last, 0); | |
9241 | last = *last_p; | |
9242 | goto continue_searching; | |
9243 | } | |
9244 | ||
9fa2814c | 9245 | if (last == error_mark_node) |
9246 | return last; | |
9247 | ||
2569a1be | 9248 | /* In the case that the BIND_EXPR is not necessary, return the |
9249 | expression out from inside it. */ | |
9fa2814c | 9250 | if (last == BIND_EXPR_BODY (body) |
9251 | && BIND_EXPR_VARS (body) == NULL) | |
1c3d3e7c | 9252 | { |
a75b1c71 | 9253 | /* Even if this looks constant, do not allow it in a constant |
9254 | expression. */ | |
f59e3889 | 9255 | last = c_wrap_maybe_const (last, true); |
1c3d3e7c | 9256 | /* Do not warn if the return value of a statement expression is |
9257 | unused. */ | |
a75b1c71 | 9258 | TREE_NO_WARNING (last) = 1; |
1c3d3e7c | 9259 | return last; |
9260 | } | |
2363ef00 | 9261 | |
9262 | /* Extract the type of said expression. */ | |
9263 | type = TREE_TYPE (last); | |
2363ef00 | 9264 | |
2569a1be | 9265 | /* If we're not returning a value at all, then the BIND_EXPR that |
9266 | we already have is a fine expression to return. */ | |
9267 | if (!type || VOID_TYPE_P (type)) | |
9268 | return body; | |
9269 | ||
9270 | /* Now that we've located the expression containing the value, it seems | |
9271 | silly to make voidify_wrapper_expr repeat the process. Create a | |
9272 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
9273 | tmp = create_tmp_var_raw (type, NULL); | |
9274 | ||
9275 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
9276 | tree_expr_nonnegative_p giving up immediately. */ | |
9277 | val = last; | |
9278 | if (TREE_CODE (val) == NOP_EXPR | |
9279 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
9280 | val = TREE_OPERAND (val, 0); | |
9281 | ||
14ae0310 | 9282 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
1cf1742e | 9283 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); |
2569a1be | 9284 | |
e60a6f7b | 9285 | { |
9286 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
9287 | SET_EXPR_LOCATION (t, loc); | |
9288 | return t; | |
9289 | } | |
2363ef00 | 9290 | } |
9291 | \f | |
9292 | /* Begin and end compound statements. This is as simple as pushing | |
9293 | and popping new statement lists from the tree. */ | |
9294 | ||
9295 | tree | |
9296 | c_begin_compound_stmt (bool do_scope) | |
9297 | { | |
9298 | tree stmt = push_stmt_list (); | |
9299 | if (do_scope) | |
735f4358 | 9300 | push_scope (); |
2363ef00 | 9301 | return stmt; |
9302 | } | |
9303 | ||
e60a6f7b | 9304 | /* End a compound statement. STMT is the statement. LOC is the |
9305 | location of the compound statement-- this is usually the location | |
9306 | of the opening brace. */ | |
9307 | ||
2363ef00 | 9308 | tree |
e60a6f7b | 9309 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) |
2363ef00 | 9310 | { |
9311 | tree block = NULL; | |
9312 | ||
9313 | if (do_scope) | |
9314 | { | |
9315 | if (c_dialect_objc ()) | |
9316 | objc_clear_super_receiver (); | |
9317 | block = pop_scope (); | |
9318 | } | |
9319 | ||
9320 | stmt = pop_stmt_list (stmt); | |
e60a6f7b | 9321 | stmt = c_build_bind_expr (loc, block, stmt); |
2363ef00 | 9322 | |
9323 | /* If this compound statement is nested immediately inside a statement | |
9324 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
9325 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
9326 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
9327 | was really last. */ | |
cacfdc02 | 9328 | if (building_stmt_list_p () |
2363ef00 | 9329 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) |
9330 | && TREE_CODE (stmt) != BIND_EXPR) | |
9331 | { | |
14ae0310 | 9332 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
2363ef00 | 9333 | TREE_SIDE_EFFECTS (stmt) = 1; |
e60a6f7b | 9334 | SET_EXPR_LOCATION (stmt, loc); |
2363ef00 | 9335 | } |
9336 | ||
9337 | return stmt; | |
9338 | } | |
dddab69e | 9339 | |
9340 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
9341 | when the current scope is exited. EH_ONLY is true when this is not | |
9342 | meant to apply to normal control flow transfer. */ | |
9343 | ||
9344 | void | |
e60a6f7b | 9345 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
dddab69e | 9346 | { |
2569a1be | 9347 | enum tree_code code; |
9348 | tree stmt, list; | |
9349 | bool stmt_expr; | |
9350 | ||
9351 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
e60a6f7b | 9352 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); |
dddab69e | 9353 | add_stmt (stmt); |
2569a1be | 9354 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
9355 | list = push_stmt_list (); | |
9356 | TREE_OPERAND (stmt, 0) = list; | |
9357 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
dddab69e | 9358 | } |
7dfa155b | 9359 | |
9360 | /* Convert scalar to vector for the range of operations. */ | |
9361 | static enum stv_conv | |
9362 | scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1) | |
9363 | { | |
9364 | tree type0 = TREE_TYPE (op0); | |
9365 | tree type1 = TREE_TYPE (op1); | |
9366 | bool integer_only_op = false; | |
9367 | enum stv_conv ret = stv_firstarg; | |
6cf89e04 | 9368 | |
9369 | gcc_assert (TREE_CODE (type0) == VECTOR_TYPE | |
7dfa155b | 9370 | || TREE_CODE (type1) == VECTOR_TYPE); |
9371 | switch (code) | |
9372 | { | |
9373 | case RSHIFT_EXPR: | |
9374 | case LSHIFT_EXPR: | |
9375 | if (TREE_CODE (type0) == INTEGER_TYPE | |
9376 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
9377 | { | |
9378 | if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) | |
9379 | { | |
9380 | error_at (loc, "conversion of scalar to vector " | |
9381 | "involves truncation"); | |
9382 | return stv_error; | |
9383 | } | |
9384 | else | |
9385 | return stv_firstarg; | |
9386 | } | |
9387 | break; | |
9388 | ||
9389 | case BIT_IOR_EXPR: | |
9390 | case BIT_XOR_EXPR: | |
9391 | case BIT_AND_EXPR: | |
9392 | integer_only_op = true; | |
9393 | /* ... fall through ... */ | |
6cf89e04 | 9394 | |
7dfa155b | 9395 | case PLUS_EXPR: |
9396 | case MINUS_EXPR: | |
9397 | case MULT_EXPR: | |
9398 | case TRUNC_DIV_EXPR: | |
9399 | case TRUNC_MOD_EXPR: | |
9400 | case RDIV_EXPR: | |
9401 | if (TREE_CODE (type0) == VECTOR_TYPE) | |
9402 | { | |
9403 | tree tmp; | |
9404 | ret = stv_secondarg; | |
9405 | /* Swap TYPE0 with TYPE1 and OP0 with OP1 */ | |
9406 | tmp = type0; type0 = type1; type1 = tmp; | |
9407 | tmp = op0; op0 = op1; op1 = tmp; | |
9408 | } | |
9409 | ||
9410 | if (TREE_CODE (type0) == INTEGER_TYPE | |
6cf89e04 | 9411 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) |
7dfa155b | 9412 | { |
9413 | if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) | |
9414 | { | |
9415 | error_at (loc, "conversion of scalar to vector " | |
9416 | "involves truncation"); | |
9417 | return stv_error; | |
9418 | } | |
9419 | return ret; | |
9420 | } | |
9421 | else if (!integer_only_op | |
9422 | /* Allow integer --> real conversion if safe. */ | |
6cf89e04 | 9423 | && (TREE_CODE (type0) == REAL_TYPE |
7dfa155b | 9424 | || TREE_CODE (type0) == INTEGER_TYPE) |
9425 | && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1))) | |
9426 | { | |
9427 | if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) | |
9428 | { | |
9429 | error_at (loc, "conversion of scalar to vector " | |
9430 | "involves truncation"); | |
9431 | return stv_error; | |
9432 | } | |
9433 | return ret; | |
9434 | } | |
9435 | default: | |
9436 | break; | |
9437 | } | |
6cf89e04 | 9438 | |
7dfa155b | 9439 | return stv_nothing; |
9440 | } | |
2363ef00 | 9441 | \f |
276beea2 | 9442 | /* Build a binary-operation expression without default conversions. |
9443 | CODE is the kind of expression to build. | |
8e70fb09 | 9444 | LOCATION is the operator's location. |
276beea2 | 9445 | This function differs from `build' in several ways: |
9446 | the data type of the result is computed and recorded in it, | |
9447 | warnings are generated if arg data types are invalid, | |
9448 | special handling for addition and subtraction of pointers is known, | |
9449 | and some optimization is done (operations on narrow ints | |
9450 | are done in the narrower type when that gives the same result). | |
9451 | Constant folding is also done before the result is returned. | |
fc698c9d | 9452 | |
276beea2 | 9453 | Note that the operands will never have enumeral types, or function |
9454 | or array types, because either they will have the default conversions | |
9455 | performed or they have both just been converted to some other type in which | |
9456 | the arithmetic is to be done. */ | |
9457 | ||
9458 | tree | |
8e70fb09 | 9459 | build_binary_op (location_t location, enum tree_code code, |
9460 | tree orig_op0, tree orig_op1, int convert_p) | |
fc698c9d | 9461 | { |
c6418a4e | 9462 | tree type0, type1, orig_type0, orig_type1; |
9463 | tree eptype; | |
276beea2 | 9464 | enum tree_code code0, code1; |
9465 | tree op0, op1; | |
b6889cb0 | 9466 | tree ret = error_mark_node; |
7a979707 | 9467 | const char *invalid_op_diag; |
9a8bed72 | 9468 | bool op0_int_operands, op1_int_operands; |
a75b1c71 | 9469 | bool int_const, int_const_or_overflow, int_operands; |
554732cd | 9470 | |
276beea2 | 9471 | /* Expression code to give to the expression when it is built. |
9472 | Normally this is CODE, which is what the caller asked for, | |
9473 | but in some special cases we change it. */ | |
9474 | enum tree_code resultcode = code; | |
989bbcfc | 9475 | |
276beea2 | 9476 | /* Data type in which the computation is to be performed. |
9477 | In the simplest cases this is the common type of the arguments. */ | |
9478 | tree result_type = NULL; | |
9479 | ||
c6418a4e | 9480 | /* When the computation is in excess precision, the type of the |
9481 | final EXCESS_PRECISION_EXPR. */ | |
8f45ab4d | 9482 | tree semantic_result_type = NULL; |
c6418a4e | 9483 | |
276beea2 | 9484 | /* Nonzero means operands have already been type-converted |
9485 | in whatever way is necessary. | |
9486 | Zero means they need to be converted to RESULT_TYPE. */ | |
9487 | int converted = 0; | |
9488 | ||
9489 | /* Nonzero means create the expression with this type, rather than | |
9490 | RESULT_TYPE. */ | |
9491 | tree build_type = 0; | |
9492 | ||
9493 | /* Nonzero means after finally constructing the expression | |
9494 | convert it to this type. */ | |
9495 | tree final_type = 0; | |
9496 | ||
9497 | /* Nonzero if this is an operation like MIN or MAX which can | |
9498 | safely be computed in short if both args are promoted shorts. | |
9499 | Also implies COMMON. | |
9500 | -1 indicates a bitwise operation; this makes a difference | |
9501 | in the exact conditions for when it is safe to do the operation | |
9502 | in a narrower mode. */ | |
9503 | int shorten = 0; | |
9504 | ||
9505 | /* Nonzero if this is a comparison operation; | |
9506 | if both args are promoted shorts, compare the original shorts. | |
9507 | Also implies COMMON. */ | |
9508 | int short_compare = 0; | |
9509 | ||
9510 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
9511 | original short and then promoted if the operand is a promoted short. */ | |
9512 | int short_shift = 0; | |
9513 | ||
9514 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
9515 | int common = 0; | |
9516 | ||
34d3c5de | 9517 | /* True means types are compatible as far as ObjC is concerned. */ |
9518 | bool objc_ok; | |
9519 | ||
c6418a4e | 9520 | /* True means this is an arithmetic operation that may need excess |
9521 | precision. */ | |
9522 | bool may_need_excess_precision; | |
9523 | ||
16a547ac | 9524 | /* True means this is a boolean operation that converts both its |
9525 | operands to truth-values. */ | |
9526 | bool boolean_op = false; | |
9527 | ||
8e70fb09 | 9528 | if (location == UNKNOWN_LOCATION) |
9529 | location = input_location; | |
9530 | ||
9a8bed72 | 9531 | op0 = orig_op0; |
9532 | op1 = orig_op1; | |
9533 | ||
9534 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
9535 | if (op0_int_operands) | |
9536 | op0 = remove_c_maybe_const_expr (op0); | |
9537 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
9538 | if (op1_int_operands) | |
9539 | op1 = remove_c_maybe_const_expr (op1); | |
9540 | int_operands = (op0_int_operands && op1_int_operands); | |
a75b1c71 | 9541 | if (int_operands) |
9542 | { | |
9543 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
9544 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
9545 | int_const = (int_const_or_overflow | |
9546 | && !TREE_OVERFLOW (orig_op0) | |
9547 | && !TREE_OVERFLOW (orig_op1)); | |
9548 | } | |
9549 | else | |
9550 | int_const = int_const_or_overflow = false; | |
9551 | ||
7dfa155b | 9552 | /* Do not apply default conversion in mixed vector/scalar expression. */ |
6cf89e04 | 9553 | if (convert_p |
9554 | && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE) | |
7dfa155b | 9555 | != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE))) |
fcacf125 | 9556 | { |
9a8bed72 | 9557 | op0 = default_conversion (op0); |
9558 | op1 = default_conversion (op1); | |
fcacf125 | 9559 | } |
9560 | ||
c6418a4e | 9561 | orig_type0 = type0 = TREE_TYPE (op0); |
9562 | orig_type1 = type1 = TREE_TYPE (op1); | |
812fff1a | 9563 | |
276beea2 | 9564 | /* The expression codes of the data types of the arguments tell us |
9565 | whether the arguments are integers, floating, pointers, etc. */ | |
9566 | code0 = TREE_CODE (type0); | |
9567 | code1 = TREE_CODE (type1); | |
9568 | ||
9569 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
9570 | STRIP_TYPE_NOPS (op0); | |
9571 | STRIP_TYPE_NOPS (op1); | |
9572 | ||
9573 | /* If an error was already reported for one of the arguments, | |
9574 | avoid reporting another error. */ | |
9575 | ||
9576 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
9577 | return error_mark_node; | |
9578 | ||
7a979707 | 9579 | if ((invalid_op_diag |
9580 | = targetm.invalid_binary_op (code, type0, type1))) | |
9581 | { | |
8e70fb09 | 9582 | error_at (location, invalid_op_diag); |
7a979707 | 9583 | return error_mark_node; |
9584 | } | |
9585 | ||
c6418a4e | 9586 | switch (code) |
9587 | { | |
9588 | case PLUS_EXPR: | |
9589 | case MINUS_EXPR: | |
9590 | case MULT_EXPR: | |
9591 | case TRUNC_DIV_EXPR: | |
9592 | case CEIL_DIV_EXPR: | |
9593 | case FLOOR_DIV_EXPR: | |
9594 | case ROUND_DIV_EXPR: | |
9595 | case EXACT_DIV_EXPR: | |
9596 | may_need_excess_precision = true; | |
9597 | break; | |
9598 | default: | |
9599 | may_need_excess_precision = false; | |
9600 | break; | |
9601 | } | |
9602 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
9603 | { | |
9604 | op0 = TREE_OPERAND (op0, 0); | |
9605 | type0 = TREE_TYPE (op0); | |
9606 | } | |
9607 | else if (may_need_excess_precision | |
9608 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
9609 | { | |
9610 | type0 = eptype; | |
9611 | op0 = convert (eptype, op0); | |
9612 | } | |
9613 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
9614 | { | |
9615 | op1 = TREE_OPERAND (op1, 0); | |
9616 | type1 = TREE_TYPE (op1); | |
9617 | } | |
9618 | else if (may_need_excess_precision | |
9619 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
9620 | { | |
9621 | type1 = eptype; | |
9622 | op1 = convert (eptype, op1); | |
9623 | } | |
9624 | ||
34d3c5de | 9625 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); |
9626 | ||
7dfa155b | 9627 | /* In case when one of the operands of the binary operation is |
9628 | a vector and another is a scalar -- convert scalar to vector. */ | |
9629 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
9630 | { | |
9631 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1); | |
6cf89e04 | 9632 | |
7dfa155b | 9633 | switch (convert_flag) |
9634 | { | |
9635 | case stv_error: | |
9636 | return error_mark_node; | |
9637 | case stv_firstarg: | |
9638 | { | |
9639 | bool maybe_const = true; | |
9640 | tree sc; | |
9641 | sc = c_fully_fold (op0, false, &maybe_const); | |
9642 | sc = save_expr (sc); | |
9643 | sc = convert (TREE_TYPE (type1), sc); | |
9644 | op0 = build_vector_from_val (type1, sc); | |
9645 | if (!maybe_const) | |
9646 | op0 = c_wrap_maybe_const (op0, true); | |
9647 | orig_type0 = type0 = TREE_TYPE (op0); | |
9648 | code0 = TREE_CODE (type0); | |
9649 | converted = 1; | |
9650 | break; | |
9651 | } | |
9652 | case stv_secondarg: | |
9653 | { | |
9654 | bool maybe_const = true; | |
9655 | tree sc; | |
9656 | sc = c_fully_fold (op1, false, &maybe_const); | |
9657 | sc = save_expr (sc); | |
9658 | sc = convert (TREE_TYPE (type0), sc); | |
9659 | op1 = build_vector_from_val (type0, sc); | |
9660 | if (!maybe_const) | |
104619f5 | 9661 | op1 = c_wrap_maybe_const (op1, true); |
7dfa155b | 9662 | orig_type1 = type1 = TREE_TYPE (op1); |
9663 | code1 = TREE_CODE (type1); | |
9664 | converted = 1; | |
9665 | break; | |
9666 | } | |
9667 | default: | |
9668 | break; | |
9669 | } | |
9670 | } | |
9671 | ||
276beea2 | 9672 | switch (code) |
fc698c9d | 9673 | { |
276beea2 | 9674 | case PLUS_EXPR: |
9675 | /* Handle the pointer + int case. */ | |
9676 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
b6889cb0 | 9677 | { |
389dd41b | 9678 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); |
b6889cb0 | 9679 | goto return_build_binary_op; |
9680 | } | |
276beea2 | 9681 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) |
b6889cb0 | 9682 | { |
389dd41b | 9683 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); |
b6889cb0 | 9684 | goto return_build_binary_op; |
9685 | } | |
16d7533c | 9686 | else |
276beea2 | 9687 | common = 1; |
9688 | break; | |
628fa4f8 | 9689 | |
276beea2 | 9690 | case MINUS_EXPR: |
9691 | /* Subtraction of two similar pointers. | |
9692 | We must subtract them as integers, then divide by object size. */ | |
9693 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
d5b637fa | 9694 | && comp_target_types (location, type0, type1)) |
b6889cb0 | 9695 | { |
389dd41b | 9696 | ret = pointer_diff (location, op0, op1); |
b6889cb0 | 9697 | goto return_build_binary_op; |
9698 | } | |
276beea2 | 9699 | /* Handle pointer minus int. Just like pointer plus int. */ |
9700 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
b6889cb0 | 9701 | { |
389dd41b | 9702 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); |
b6889cb0 | 9703 | goto return_build_binary_op; |
9704 | } | |
276beea2 | 9705 | else |
9706 | common = 1; | |
9707 | break; | |
989bbcfc | 9708 | |
276beea2 | 9709 | case MULT_EXPR: |
9710 | common = 1; | |
9711 | break; | |
9712 | ||
9713 | case TRUNC_DIV_EXPR: | |
9714 | case CEIL_DIV_EXPR: | |
9715 | case FLOOR_DIV_EXPR: | |
9716 | case ROUND_DIV_EXPR: | |
9717 | case EXACT_DIV_EXPR: | |
b6889cb0 | 9718 | warn_for_div_by_zero (location, op1); |
276beea2 | 9719 | |
9720 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
9421ebb9 | 9721 | || code0 == FIXED_POINT_TYPE |
276beea2 | 9722 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
9723 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
9421ebb9 | 9724 | || code1 == FIXED_POINT_TYPE |
276beea2 | 9725 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
628fa4f8 | 9726 | { |
491255f5 | 9727 | enum tree_code tcode0 = code0, tcode1 = code1; |
9728 | ||
4418a1d4 | 9729 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
491255f5 | 9730 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
4418a1d4 | 9731 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) |
491255f5 | 9732 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
4418a1d4 | 9733 | |
9421ebb9 | 9734 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) |
9735 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
276beea2 | 9736 | resultcode = RDIV_EXPR; |
9737 | else | |
9738 | /* Although it would be tempting to shorten always here, that | |
9739 | loses on some targets, since the modulo instruction is | |
9740 | undefined if the quotient can't be represented in the | |
9741 | computation mode. We shorten only if unsigned or if | |
9742 | dividing by something we know != -1. */ | |
78a8ed03 | 9743 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
276beea2 | 9744 | || (TREE_CODE (op1) == INTEGER_CST |
84166705 | 9745 | && !integer_all_onesp (op1))); |
276beea2 | 9746 | common = 1; |
9747 | } | |
9748 | break; | |
fc698c9d | 9749 | |
276beea2 | 9750 | case BIT_AND_EXPR: |
276beea2 | 9751 | case BIT_IOR_EXPR: |
9752 | case BIT_XOR_EXPR: | |
9753 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
9754 | shorten = -1; | |
f8629d35 | 9755 | /* Allow vector types which are not floating point types. */ |
9756 | else if (code0 == VECTOR_TYPE | |
9757 | && code1 == VECTOR_TYPE | |
9758 | && !VECTOR_FLOAT_TYPE_P (type0) | |
9759 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
276beea2 | 9760 | common = 1; |
9761 | break; | |
9762 | ||
9763 | case TRUNC_MOD_EXPR: | |
9764 | case FLOOR_MOD_EXPR: | |
b6889cb0 | 9765 | warn_for_div_by_zero (location, op1); |
fc698c9d | 9766 | |
b6d7101e | 9767 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
9768 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9769 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
9770 | common = 1; | |
9771 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
276beea2 | 9772 | { |
9773 | /* Although it would be tempting to shorten always here, that loses | |
9774 | on some targets, since the modulo instruction is undefined if the | |
9775 | quotient can't be represented in the computation mode. We shorten | |
9776 | only if unsigned or if dividing by something we know != -1. */ | |
78a8ed03 | 9777 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
276beea2 | 9778 | || (TREE_CODE (op1) == INTEGER_CST |
84166705 | 9779 | && !integer_all_onesp (op1))); |
276beea2 | 9780 | common = 1; |
9781 | } | |
9782 | break; | |
fc698c9d | 9783 | |
276beea2 | 9784 | case TRUTH_ANDIF_EXPR: |
9785 | case TRUTH_ORIF_EXPR: | |
9786 | case TRUTH_AND_EXPR: | |
9787 | case TRUTH_OR_EXPR: | |
9788 | case TRUTH_XOR_EXPR: | |
9789 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
9421ebb9 | 9790 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
9791 | || code0 == FIXED_POINT_TYPE) | |
276beea2 | 9792 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE |
9421ebb9 | 9793 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE |
9794 | || code1 == FIXED_POINT_TYPE)) | |
276beea2 | 9795 | { |
9796 | /* Result of these operations is always an int, | |
9797 | but that does not mean the operands should be | |
9798 | converted to ints! */ | |
9799 | result_type = integer_type_node; | |
8e70fb09 | 9800 | op0 = c_common_truthvalue_conversion (location, op0); |
9801 | op1 = c_common_truthvalue_conversion (location, op1); | |
276beea2 | 9802 | converted = 1; |
16a547ac | 9803 | boolean_op = true; |
276beea2 | 9804 | } |
a75b1c71 | 9805 | if (code == TRUTH_ANDIF_EXPR) |
9806 | { | |
9807 | int_const_or_overflow = (int_operands | |
9808 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9809 | && (op0 == truthvalue_false_node | |
9810 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9811 | int_const = (int_const_or_overflow | |
9812 | && !TREE_OVERFLOW (orig_op0) | |
9813 | && (op0 == truthvalue_false_node | |
9814 | || !TREE_OVERFLOW (orig_op1))); | |
9815 | } | |
9816 | else if (code == TRUTH_ORIF_EXPR) | |
9817 | { | |
9818 | int_const_or_overflow = (int_operands | |
9819 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9820 | && (op0 == truthvalue_true_node | |
9821 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9822 | int_const = (int_const_or_overflow | |
9823 | && !TREE_OVERFLOW (orig_op0) | |
9824 | && (op0 == truthvalue_true_node | |
9825 | || !TREE_OVERFLOW (orig_op1))); | |
9826 | } | |
276beea2 | 9827 | break; |
cad0b4b0 | 9828 | |
276beea2 | 9829 | /* Shift operations: result has same type as first operand; |
9830 | always convert second operand to int. | |
9831 | Also set SHORT_SHIFT if shifting rightward. */ | |
fc698c9d | 9832 | |
276beea2 | 9833 | case RSHIFT_EXPR: |
191b2c61 | 9834 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
9835 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
9836 | { | |
9837 | result_type = type0; | |
9838 | converted = 1; | |
9839 | } | |
9840 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
9841 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9842 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
9843 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
9844 | { | |
9845 | result_type = type0; | |
9846 | converted = 1; | |
9847 | } | |
9848 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
9421ebb9 | 9849 | && code1 == INTEGER_TYPE) |
276beea2 | 9850 | { |
a75b1c71 | 9851 | if (TREE_CODE (op1) == INTEGER_CST) |
554732cd | 9852 | { |
276beea2 | 9853 | if (tree_int_cst_sgn (op1) < 0) |
a75b1c71 | 9854 | { |
9855 | int_const = false; | |
48d94ede | 9856 | if (c_inhibit_evaluation_warnings == 0) |
a75b1c71 | 9857 | warning (0, "right shift count is negative"); |
9858 | } | |
276beea2 | 9859 | else |
66fb954a | 9860 | { |
84166705 | 9861 | if (!integer_zerop (op1)) |
276beea2 | 9862 | short_shift = 1; |
9863 | ||
9864 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
a75b1c71 | 9865 | { |
9866 | int_const = false; | |
48d94ede | 9867 | if (c_inhibit_evaluation_warnings == 0) |
a75b1c71 | 9868 | warning (0, "right shift count >= width of type"); |
9869 | } | |
66fb954a | 9870 | } |
554732cd | 9871 | } |
fc698c9d | 9872 | |
276beea2 | 9873 | /* Use the type of the value to be shifted. */ |
9874 | result_type = type0; | |
191b2c61 | 9875 | /* Convert the non vector shift-count to an integer, regardless |
9876 | of size of value being shifted. */ | |
9877 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
9878 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
276beea2 | 9879 | op1 = convert (integer_type_node, op1); |
9880 | /* Avoid converting op1 to result_type later. */ | |
9881 | converted = 1; | |
628fa4f8 | 9882 | } |
276beea2 | 9883 | break; |
fa6ddf23 | 9884 | |
276beea2 | 9885 | case LSHIFT_EXPR: |
191b2c61 | 9886 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
9887 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
9888 | { | |
9889 | result_type = type0; | |
9890 | converted = 1; | |
9891 | } | |
9892 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
9893 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9894 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
9895 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
9896 | { | |
9897 | result_type = type0; | |
9898 | converted = 1; | |
9899 | } | |
9900 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
9421ebb9 | 9901 | && code1 == INTEGER_TYPE) |
276beea2 | 9902 | { |
a75b1c71 | 9903 | if (TREE_CODE (op1) == INTEGER_CST) |
fc698c9d | 9904 | { |
276beea2 | 9905 | if (tree_int_cst_sgn (op1) < 0) |
a75b1c71 | 9906 | { |
9907 | int_const = false; | |
48d94ede | 9908 | if (c_inhibit_evaluation_warnings == 0) |
a75b1c71 | 9909 | warning (0, "left shift count is negative"); |
9910 | } | |
fc698c9d | 9911 | |
276beea2 | 9912 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
a75b1c71 | 9913 | { |
9914 | int_const = false; | |
48d94ede | 9915 | if (c_inhibit_evaluation_warnings == 0) |
a75b1c71 | 9916 | warning (0, "left shift count >= width of type"); |
9917 | } | |
d354d271 | 9918 | } |
fc698c9d | 9919 | |
276beea2 | 9920 | /* Use the type of the value to be shifted. */ |
9921 | result_type = type0; | |
191b2c61 | 9922 | /* Convert the non vector shift-count to an integer, regardless |
9923 | of size of value being shifted. */ | |
9924 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
9925 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
276beea2 | 9926 | op1 = convert (integer_type_node, op1); |
9927 | /* Avoid converting op1 to result_type later. */ | |
9928 | converted = 1; | |
628fa4f8 | 9929 | } |
276beea2 | 9930 | break; |
fc698c9d | 9931 | |
276beea2 | 9932 | case EQ_EXPR: |
9933 | case NE_EXPR: | |
d7ad16c2 | 9934 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
9935 | { | |
9936 | tree intt; | |
9937 | if (TREE_TYPE (type0) != TREE_TYPE (type1)) | |
9938 | { | |
9939 | error_at (location, "comparing vectors with different " | |
9940 | "element types"); | |
9941 | return error_mark_node; | |
9942 | } | |
9943 | ||
9944 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
9945 | { | |
9946 | error_at (location, "comparing vectors with different " | |
9947 | "number of elements"); | |
9948 | return error_mark_node; | |
9949 | } | |
9950 | ||
9951 | /* Always construct signed integer vector type. */ | |
9952 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
9953 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
9954 | result_type = build_opaque_vector_type (intt, | |
9955 | TYPE_VECTOR_SUBPARTS (type0)); | |
9956 | converted = 1; | |
9957 | break; | |
9958 | } | |
067d82a2 | 9959 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) |
8e70fb09 | 9960 | warning_at (location, |
9961 | OPT_Wfloat_equal, | |
9962 | "comparing floating point with == or != is unsafe"); | |
276beea2 | 9963 | /* Result of comparison is always int, |
9964 | but don't convert the args to int! */ | |
9965 | build_type = integer_type_node; | |
9966 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
9421ebb9 | 9967 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) |
276beea2 | 9968 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
9421ebb9 | 9969 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) |
276beea2 | 9970 | short_compare = 1; |
bf8949d1 | 9971 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
9972 | { | |
9973 | if (TREE_CODE (op0) == ADDR_EXPR | |
9974 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) | |
9975 | { | |
9976 | if (code == EQ_EXPR) | |
9977 | warning_at (location, | |
9978 | OPT_Waddress, | |
9979 | "the comparison will always evaluate as %<false%> " | |
9980 | "for the address of %qD will never be NULL", | |
9981 | TREE_OPERAND (op0, 0)); | |
9982 | else | |
9983 | warning_at (location, | |
9984 | OPT_Waddress, | |
9985 | "the comparison will always evaluate as %<true%> " | |
9986 | "for the address of %qD will never be NULL", | |
9987 | TREE_OPERAND (op0, 0)); | |
9988 | } | |
9989 | result_type = type0; | |
9990 | } | |
9991 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) | |
9992 | { | |
9993 | if (TREE_CODE (op1) == ADDR_EXPR | |
9994 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) | |
9995 | { | |
9996 | if (code == EQ_EXPR) | |
9997 | warning_at (location, | |
6cf89e04 | 9998 | OPT_Waddress, |
bf8949d1 | 9999 | "the comparison will always evaluate as %<false%> " |
10000 | "for the address of %qD will never be NULL", | |
10001 | TREE_OPERAND (op1, 0)); | |
10002 | else | |
10003 | warning_at (location, | |
10004 | OPT_Waddress, | |
10005 | "the comparison will always evaluate as %<true%> " | |
10006 | "for the address of %qD will never be NULL", | |
10007 | TREE_OPERAND (op1, 0)); | |
10008 | } | |
10009 | result_type = type1; | |
10010 | } | |
276beea2 | 10011 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
10012 | { | |
10013 | tree tt0 = TREE_TYPE (type0); | |
10014 | tree tt1 = TREE_TYPE (type1); | |
6d5d708e | 10015 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); |
10016 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
10017 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
10018 | ||
276beea2 | 10019 | /* Anything compares with void *. void * compares with anything. |
10020 | Otherwise, the targets must be compatible | |
10021 | and both must be object or both incomplete. */ | |
d5b637fa | 10022 | if (comp_target_types (location, type0, type1)) |
1ea7fa60 | 10023 | result_type = common_pointer_type (type0, type1); |
6d5d708e | 10024 | else if (!addr_space_superset (as0, as1, &as_common)) |
10025 | { | |
10026 | error_at (location, "comparison of pointers to " | |
10027 | "disjoint address spaces"); | |
10028 | return error_mark_node; | |
10029 | } | |
276beea2 | 10030 | else if (VOID_TYPE_P (tt0)) |
5e4a4d15 | 10031 | { |
6d5d708e | 10032 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) |
29438999 | 10033 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
8864917d | 10034 | "comparison of %<void *%> with function pointer"); |
5e4a4d15 | 10035 | } |
276beea2 | 10036 | else if (VOID_TYPE_P (tt1)) |
0ad236c2 | 10037 | { |
6d5d708e | 10038 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) |
29438999 | 10039 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
8864917d | 10040 | "comparison of %<void *%> with function pointer"); |
0ad236c2 | 10041 | } |
276beea2 | 10042 | else |
34d3c5de | 10043 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
10044 | if (!objc_ok) | |
8e70fb09 | 10045 | pedwarn (location, 0, |
21ca8540 | 10046 | "comparison of distinct pointer types lacks a cast"); |
0ad236c2 | 10047 | |
276beea2 | 10048 | if (result_type == NULL_TREE) |
6d5d708e | 10049 | { |
10050 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
10051 | result_type = build_pointer_type | |
10052 | (build_qualified_type (void_type_node, qual)); | |
10053 | } | |
0ad236c2 | 10054 | } |
276beea2 | 10055 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
fc698c9d | 10056 | { |
276beea2 | 10057 | result_type = type0; |
8e70fb09 | 10058 | pedwarn (location, 0, "comparison between pointer and integer"); |
fc698c9d | 10059 | } |
276beea2 | 10060 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
989bbcfc | 10061 | { |
276beea2 | 10062 | result_type = type1; |
8e70fb09 | 10063 | pedwarn (location, 0, "comparison between pointer and integer"); |
989bbcfc | 10064 | } |
276beea2 | 10065 | break; |
989bbcfc | 10066 | |
276beea2 | 10067 | case LE_EXPR: |
10068 | case GE_EXPR: | |
10069 | case LT_EXPR: | |
10070 | case GT_EXPR: | |
d7ad16c2 | 10071 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10072 | { | |
10073 | tree intt; | |
10074 | if (TREE_TYPE (type0) != TREE_TYPE (type1)) | |
10075 | { | |
10076 | error_at (location, "comparing vectors with different " | |
10077 | "element types"); | |
10078 | return error_mark_node; | |
10079 | } | |
10080 | ||
10081 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10082 | { | |
10083 | error_at (location, "comparing vectors with different " | |
10084 | "number of elements"); | |
10085 | return error_mark_node; | |
10086 | } | |
10087 | ||
10088 | /* Always construct signed integer vector type. */ | |
10089 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10090 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10091 | result_type = build_opaque_vector_type (intt, | |
10092 | TYPE_VECTOR_SUBPARTS (type0)); | |
10093 | converted = 1; | |
10094 | break; | |
10095 | } | |
276beea2 | 10096 | build_type = integer_type_node; |
9421ebb9 | 10097 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
10098 | || code0 == FIXED_POINT_TYPE) | |
10099 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
10100 | || code1 == FIXED_POINT_TYPE)) | |
276beea2 | 10101 | short_compare = 1; |
10102 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
10103 | { | |
6d5d708e | 10104 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); |
10105 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
10106 | addr_space_t as_common; | |
10107 | ||
d5b637fa | 10108 | if (comp_target_types (location, type0, type1)) |
276beea2 | 10109 | { |
1ea7fa60 | 10110 | result_type = common_pointer_type (type0, type1); |
276beea2 | 10111 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
10112 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
8e70fb09 | 10113 | pedwarn (location, 0, |
21ca8540 | 10114 | "comparison of complete and incomplete pointers"); |
8864917d | 10115 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) |
29438999 | 10116 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
8864917d | 10117 | "ordered comparisons of pointers to functions"); |
cdd6b2f1 | 10118 | else if (null_pointer_constant_p (orig_op0) |
10119 | || null_pointer_constant_p (orig_op1)) | |
10120 | warning_at (location, OPT_Wextra, | |
10121 | "ordered comparison of pointer with null pointer"); | |
10122 | ||
276beea2 | 10123 | } |
6d5d708e | 10124 | else if (!addr_space_superset (as0, as1, &as_common)) |
10125 | { | |
10126 | error_at (location, "comparison of pointers to " | |
10127 | "disjoint address spaces"); | |
10128 | return error_mark_node; | |
10129 | } | |
276beea2 | 10130 | else |
10131 | { | |
6d5d708e | 10132 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
10133 | result_type = build_pointer_type | |
10134 | (build_qualified_type (void_type_node, qual)); | |
8e70fb09 | 10135 | pedwarn (location, 0, |
21ca8540 | 10136 | "comparison of distinct pointer types lacks a cast"); |
276beea2 | 10137 | } |
10138 | } | |
79f925ed | 10139 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
276beea2 | 10140 | { |
10141 | result_type = type0; | |
8864917d | 10142 | if (pedantic) |
29438999 | 10143 | pedwarn (location, OPT_Wpedantic, |
8864917d | 10144 | "ordered comparison of pointer with integer zero"); |
10145 | else if (extra_warnings) | |
8e70fb09 | 10146 | warning_at (location, OPT_Wextra, |
cdd6b2f1 | 10147 | "ordered comparison of pointer with integer zero"); |
276beea2 | 10148 | } |
79f925ed | 10149 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
276beea2 | 10150 | { |
10151 | result_type = type1; | |
cdd6b2f1 | 10152 | if (pedantic) |
29438999 | 10153 | pedwarn (location, OPT_Wpedantic, |
cdd6b2f1 | 10154 | "ordered comparison of pointer with integer zero"); |
10155 | else if (extra_warnings) | |
10156 | warning_at (location, OPT_Wextra, | |
10157 | "ordered comparison of pointer with integer zero"); | |
276beea2 | 10158 | } |
10159 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
10160 | { | |
10161 | result_type = type0; | |
8e70fb09 | 10162 | pedwarn (location, 0, "comparison between pointer and integer"); |
276beea2 | 10163 | } |
10164 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
10165 | { | |
10166 | result_type = type1; | |
8e70fb09 | 10167 | pedwarn (location, 0, "comparison between pointer and integer"); |
276beea2 | 10168 | } |
10169 | break; | |
7cad36f4 | 10170 | |
276beea2 | 10171 | default: |
315ba355 | 10172 | gcc_unreachable (); |
f1929933 | 10173 | } |
e41f0d80 | 10174 | |
d1b92730 | 10175 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
10176 | return error_mark_node; | |
10177 | ||
491255f5 | 10178 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
10179 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
10180 | || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0), | |
10181 | TREE_TYPE (type1)))) | |
10182 | { | |
8e70fb09 | 10183 | binary_op_error (location, code, type0, type1); |
491255f5 | 10184 | return error_mark_node; |
10185 | } | |
10186 | ||
276beea2 | 10187 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
9421ebb9 | 10188 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) |
276beea2 | 10189 | && |
10190 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
9421ebb9 | 10191 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) |
628fa4f8 | 10192 | { |
0f241d3f | 10193 | bool first_complex = (code0 == COMPLEX_TYPE); |
10194 | bool second_complex = (code1 == COMPLEX_TYPE); | |
10195 | int none_complex = (!first_complex && !second_complex); | |
4ec54c99 | 10196 | |
276beea2 | 10197 | if (shorten || common || short_compare) |
441df341 | 10198 | { |
10199 | result_type = c_common_type (type0, type1); | |
c920faa3 | 10200 | do_warn_double_promotion (result_type, type0, type1, |
10201 | "implicit conversion from %qT to %qT " | |
10202 | "to match other operand of binary " | |
10203 | "expression", | |
10204 | location); | |
441df341 | 10205 | if (result_type == error_mark_node) |
10206 | return error_mark_node; | |
10207 | } | |
628fa4f8 | 10208 | |
0f241d3f | 10209 | if (first_complex != second_complex |
10210 | && (code == PLUS_EXPR | |
10211 | || code == MINUS_EXPR | |
10212 | || code == MULT_EXPR | |
10213 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
10214 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
10215 | && flag_signed_zeros) | |
10216 | { | |
10217 | /* An operation on mixed real/complex operands must be | |
10218 | handled specially, but the language-independent code can | |
10219 | more easily optimize the plain complex arithmetic if | |
10220 | -fno-signed-zeros. */ | |
10221 | tree real_type = TREE_TYPE (result_type); | |
10222 | tree real, imag; | |
10223 | if (type0 != orig_type0 || type1 != orig_type1) | |
10224 | { | |
10225 | gcc_assert (may_need_excess_precision && common); | |
8f45ab4d | 10226 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
0f241d3f | 10227 | } |
10228 | if (first_complex) | |
10229 | { | |
10230 | if (TREE_TYPE (op0) != result_type) | |
10231 | op0 = convert_and_check (result_type, op0); | |
10232 | if (TREE_TYPE (op1) != real_type) | |
10233 | op1 = convert_and_check (real_type, op1); | |
10234 | } | |
10235 | else | |
10236 | { | |
10237 | if (TREE_TYPE (op0) != real_type) | |
10238 | op0 = convert_and_check (real_type, op0); | |
10239 | if (TREE_TYPE (op1) != result_type) | |
10240 | op1 = convert_and_check (result_type, op1); | |
10241 | } | |
10242 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10243 | return error_mark_node; | |
10244 | if (first_complex) | |
10245 | { | |
10246 | op0 = c_save_expr (op0); | |
10247 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
10248 | op0, 1); | |
10249 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
10250 | op0, 1); | |
10251 | switch (code) | |
10252 | { | |
10253 | case MULT_EXPR: | |
10254 | case TRUNC_DIV_EXPR: | |
66031ea7 | 10255 | op1 = c_save_expr (op1); |
0f241d3f | 10256 | imag = build2 (resultcode, real_type, imag, op1); |
10257 | /* Fall through. */ | |
10258 | case PLUS_EXPR: | |
10259 | case MINUS_EXPR: | |
10260 | real = build2 (resultcode, real_type, real, op1); | |
10261 | break; | |
10262 | default: | |
10263 | gcc_unreachable(); | |
10264 | } | |
10265 | } | |
10266 | else | |
10267 | { | |
10268 | op1 = c_save_expr (op1); | |
10269 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
10270 | op1, 1); | |
10271 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
10272 | op1, 1); | |
10273 | switch (code) | |
10274 | { | |
10275 | case MULT_EXPR: | |
66031ea7 | 10276 | op0 = c_save_expr (op0); |
0f241d3f | 10277 | imag = build2 (resultcode, real_type, op0, imag); |
10278 | /* Fall through. */ | |
10279 | case PLUS_EXPR: | |
10280 | real = build2 (resultcode, real_type, op0, real); | |
10281 | break; | |
10282 | case MINUS_EXPR: | |
10283 | real = build2 (resultcode, real_type, op0, real); | |
10284 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
10285 | break; | |
10286 | default: | |
10287 | gcc_unreachable(); | |
10288 | } | |
10289 | } | |
10290 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
10291 | goto return_build_binary_op; | |
10292 | } | |
10293 | ||
276beea2 | 10294 | /* For certain operations (which identify themselves by shorten != 0) |
10295 | if both args were extended from the same smaller type, | |
10296 | do the arithmetic in that type and then extend. | |
628fa4f8 | 10297 | |
276beea2 | 10298 | shorten !=0 and !=1 indicates a bitwise operation. |
10299 | For them, this optimization is safe only if | |
10300 | both args are zero-extended or both are sign-extended. | |
10301 | Otherwise, we might change the result. | |
10302 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
10303 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
628fa4f8 | 10304 | |
276beea2 | 10305 | if (shorten && none_complex) |
10306 | { | |
276beea2 | 10307 | final_type = result_type; |
48e1416a | 10308 | result_type = shorten_binary_op (result_type, op0, op1, |
2561cea2 | 10309 | shorten == -1); |
276beea2 | 10310 | } |
30cd880f | 10311 | |
276beea2 | 10312 | /* Shifts can be shortened if shifting right. */ |
2b30d46c | 10313 | |
276beea2 | 10314 | if (short_shift) |
10315 | { | |
10316 | int unsigned_arg; | |
10317 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
30cd880f | 10318 | |
276beea2 | 10319 | final_type = result_type; |
17677b84 | 10320 | |
276beea2 | 10321 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
78a8ed03 | 10322 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
0dbd1c74 | 10323 | |
276beea2 | 10324 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
9e072631 | 10325 | && tree_int_cst_sgn (op1) > 0 |
276beea2 | 10326 | /* We can shorten only if the shift count is less than the |
10327 | number of bits in the smaller type size. */ | |
10328 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
10329 | /* We cannot drop an unsigned shift after sign-extension. */ | |
78a8ed03 | 10330 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
276beea2 | 10331 | { |
10332 | /* Do an unsigned shift if the operand was zero-extended. */ | |
10333 | result_type | |
10334 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
10335 | TREE_TYPE (arg0)); | |
10336 | /* Convert value-to-be-shifted to that type. */ | |
10337 | if (TREE_TYPE (op0) != result_type) | |
10338 | op0 = convert (result_type, op0); | |
10339 | converted = 1; | |
17677b84 | 10340 | } |
30cd880f | 10341 | } |
10342 | ||
276beea2 | 10343 | /* Comparison operations are shortened too but differently. |
10344 | They identify themselves by setting short_compare = 1. */ | |
225ec6aa | 10345 | |
276beea2 | 10346 | if (short_compare) |
10347 | { | |
10348 | /* Don't write &op0, etc., because that would prevent op0 | |
10349 | from being kept in a register. | |
10350 | Instead, make copies of the our local variables and | |
10351 | pass the copies by reference, then copy them back afterward. */ | |
10352 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
10353 | enum tree_code xresultcode = resultcode; | |
10354 | tree val | |
10355 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
e41f0d80 | 10356 | |
276beea2 | 10357 | if (val != 0) |
b6889cb0 | 10358 | { |
10359 | ret = val; | |
10360 | goto return_build_binary_op; | |
10361 | } | |
e41f0d80 | 10362 | |
276beea2 | 10363 | op0 = xop0, op1 = xop1; |
10364 | converted = 1; | |
10365 | resultcode = xresultcode; | |
e41f0d80 | 10366 | |
48d94ede | 10367 | if (c_inhibit_evaluation_warnings == 0) |
a75b1c71 | 10368 | { |
10369 | bool op0_maybe_const = true; | |
10370 | bool op1_maybe_const = true; | |
10371 | tree orig_op0_folded, orig_op1_folded; | |
10372 | ||
10373 | if (in_late_binary_op) | |
10374 | { | |
10375 | orig_op0_folded = orig_op0; | |
10376 | orig_op1_folded = orig_op1; | |
10377 | } | |
10378 | else | |
10379 | { | |
10380 | /* Fold for the sake of possible warnings, as in | |
10381 | build_conditional_expr. This requires the | |
10382 | "original" values to be folded, not just op0 and | |
10383 | op1. */ | |
672d914b | 10384 | c_inhibit_evaluation_warnings++; |
a75b1c71 | 10385 | op0 = c_fully_fold (op0, require_constant_value, |
10386 | &op0_maybe_const); | |
10387 | op1 = c_fully_fold (op1, require_constant_value, | |
10388 | &op1_maybe_const); | |
672d914b | 10389 | c_inhibit_evaluation_warnings--; |
a75b1c71 | 10390 | orig_op0_folded = c_fully_fold (orig_op0, |
10391 | require_constant_value, | |
10392 | NULL); | |
10393 | orig_op1_folded = c_fully_fold (orig_op1, | |
10394 | require_constant_value, | |
10395 | NULL); | |
10396 | } | |
10397 | ||
10398 | if (warn_sign_compare) | |
10399 | warn_for_sign_compare (location, orig_op0_folded, | |
10400 | orig_op1_folded, op0, op1, | |
10401 | result_type, resultcode); | |
4095a8c8 | 10402 | if (!in_late_binary_op && !int_operands) |
a75b1c71 | 10403 | { |
10404 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
f59e3889 | 10405 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); |
a75b1c71 | 10406 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) |
f59e3889 | 10407 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
a75b1c71 | 10408 | } |
276beea2 | 10409 | } |
736393e5 | 10410 | } |
7cad36f4 | 10411 | } |
7cad36f4 | 10412 | |
276beea2 | 10413 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
10414 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
10415 | Then the expression will be built. | |
10416 | It will be given type FINAL_TYPE if that is nonzero; | |
10417 | otherwise, it will be given type RESULT_TYPE. */ | |
628fa4f8 | 10418 | |
276beea2 | 10419 | if (!result_type) |
10420 | { | |
8e70fb09 | 10421 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); |
276beea2 | 10422 | return error_mark_node; |
10423 | } | |
628fa4f8 | 10424 | |
276beea2 | 10425 | if (build_type == NULL_TREE) |
c6418a4e | 10426 | { |
10427 | build_type = result_type; | |
16a547ac | 10428 | if ((type0 != orig_type0 || type1 != orig_type1) |
10429 | && !boolean_op) | |
c6418a4e | 10430 | { |
10431 | gcc_assert (may_need_excess_precision && common); | |
8f45ab4d | 10432 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
c6418a4e | 10433 | } |
10434 | } | |
628fa4f8 | 10435 | |
8f45ab4d | 10436 | if (!converted) |
10437 | { | |
10438 | op0 = ep_convert_and_check (result_type, op0, semantic_result_type); | |
10439 | op1 = ep_convert_and_check (result_type, op1, semantic_result_type); | |
10440 | ||
10441 | /* This can happen if one operand has a vector type, and the other | |
10442 | has a different type. */ | |
10443 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10444 | return error_mark_node; | |
10445 | } | |
10446 | ||
b6889cb0 | 10447 | /* Treat expressions in initializers specially as they can't trap. */ |
a75b1c71 | 10448 | if (int_const_or_overflow) |
10449 | ret = (require_constant_value | |
389dd41b | 10450 | ? fold_build2_initializer_loc (location, resultcode, build_type, |
10451 | op0, op1) | |
10452 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
a75b1c71 | 10453 | else |
10454 | ret = build2 (resultcode, build_type, op0, op1); | |
b6889cb0 | 10455 | if (final_type != 0) |
10456 | ret = convert (final_type, ret); | |
10457 | ||
10458 | return_build_binary_op: | |
10459 | gcc_assert (ret != error_mark_node); | |
a75b1c71 | 10460 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) |
10461 | ret = (int_operands | |
10462 | ? note_integer_operands (ret) | |
10463 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
10464 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
10465 | && !in_late_binary_op) | |
10466 | ret = note_integer_operands (ret); | |
8f45ab4d | 10467 | if (semantic_result_type) |
10468 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
b6889cb0 | 10469 | protected_set_expr_location (ret, location); |
10470 | return ret; | |
628fa4f8 | 10471 | } |
45a78cc0 | 10472 | |
10473 | ||
10474 | /* Convert EXPR to be a truth-value, validating its type for this | |
8e70fb09 | 10475 | purpose. LOCATION is the source location for the expression. */ |
45a78cc0 | 10476 | |
10477 | tree | |
8e70fb09 | 10478 | c_objc_common_truthvalue_conversion (location_t location, tree expr) |
45a78cc0 | 10479 | { |
a75b1c71 | 10480 | bool int_const, int_operands; |
10481 | ||
45a78cc0 | 10482 | switch (TREE_CODE (TREE_TYPE (expr))) |
10483 | { | |
10484 | case ARRAY_TYPE: | |
8e70fb09 | 10485 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); |
45a78cc0 | 10486 | return error_mark_node; |
10487 | ||
10488 | case RECORD_TYPE: | |
8e70fb09 | 10489 | error_at (location, "used struct type value where scalar is required"); |
45a78cc0 | 10490 | return error_mark_node; |
10491 | ||
10492 | case UNION_TYPE: | |
8e70fb09 | 10493 | error_at (location, "used union type value where scalar is required"); |
45a78cc0 | 10494 | return error_mark_node; |
10495 | ||
a43fc97e | 10496 | case VOID_TYPE: |
10497 | error_at (location, "void value not ignored as it ought to be"); | |
10498 | return error_mark_node; | |
10499 | ||
f14c8207 | 10500 | case FUNCTION_TYPE: |
10501 | gcc_unreachable (); | |
10502 | ||
d7ad16c2 | 10503 | case VECTOR_TYPE: |
10504 | error_at (location, "used vector type where scalar is required"); | |
10505 | return error_mark_node; | |
10506 | ||
45a78cc0 | 10507 | default: |
10508 | break; | |
10509 | } | |
10510 | ||
a75b1c71 | 10511 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); |
10512 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
9a8bed72 | 10513 | if (int_operands) |
10514 | expr = remove_c_maybe_const_expr (expr); | |
a75b1c71 | 10515 | |
a43fc97e | 10516 | /* ??? Should we also give an error for vectors rather than leaving |
10517 | those to give errors later? */ | |
a75b1c71 | 10518 | expr = c_common_truthvalue_conversion (location, expr); |
10519 | ||
10520 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
10521 | { | |
10522 | if (TREE_OVERFLOW (expr)) | |
10523 | return expr; | |
10524 | else | |
10525 | return note_integer_operands (expr); | |
10526 | } | |
10527 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
10528 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
10529 | return expr; | |
45a78cc0 | 10530 | } |
54d7165a | 10531 | \f |
10532 | ||
10533 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
10534 | required. */ | |
10535 | ||
10536 | tree | |
c7d4e749 | 10537 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) |
54d7165a | 10538 | { |
10539 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
10540 | { | |
10541 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
10542 | /* Executing a compound literal inside a function reinitializes | |
10543 | it. */ | |
10544 | if (!TREE_STATIC (decl)) | |
10545 | *se = true; | |
10546 | return decl; | |
10547 | } | |
10548 | else | |
10549 | return expr; | |
10550 | } | |
1e8e9920 | 10551 | \f |
334ec2d8 | 10552 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
1e8e9920 | 10553 | |
10554 | tree | |
10555 | c_begin_omp_parallel (void) | |
10556 | { | |
10557 | tree block; | |
10558 | ||
10559 | keep_next_level (); | |
10560 | block = c_begin_compound_stmt (true); | |
10561 | ||
10562 | return block; | |
10563 | } | |
10564 | ||
e60a6f7b | 10565 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound |
10566 | statement. LOC is the location of the OMP_PARALLEL. */ | |
fd6481cf | 10567 | |
1e8e9920 | 10568 | tree |
e60a6f7b | 10569 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) |
1e8e9920 | 10570 | { |
10571 | tree stmt; | |
10572 | ||
e60a6f7b | 10573 | block = c_end_compound_stmt (loc, block, true); |
1e8e9920 | 10574 | |
10575 | stmt = make_node (OMP_PARALLEL); | |
10576 | TREE_TYPE (stmt) = void_type_node; | |
10577 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
10578 | OMP_PARALLEL_BODY (stmt) = block; | |
e60a6f7b | 10579 | SET_EXPR_LOCATION (stmt, loc); |
1e8e9920 | 10580 | |
10581 | return add_stmt (stmt); | |
10582 | } | |
10583 | ||
fd6481cf | 10584 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
10585 | ||
10586 | tree | |
10587 | c_begin_omp_task (void) | |
10588 | { | |
10589 | tree block; | |
10590 | ||
10591 | keep_next_level (); | |
10592 | block = c_begin_compound_stmt (true); | |
10593 | ||
10594 | return block; | |
10595 | } | |
10596 | ||
e60a6f7b | 10597 | /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound |
10598 | statement. LOC is the location of the #pragma. */ | |
fd6481cf | 10599 | |
10600 | tree | |
e60a6f7b | 10601 | c_finish_omp_task (location_t loc, tree clauses, tree block) |
fd6481cf | 10602 | { |
10603 | tree stmt; | |
10604 | ||
e60a6f7b | 10605 | block = c_end_compound_stmt (loc, block, true); |
fd6481cf | 10606 | |
10607 | stmt = make_node (OMP_TASK); | |
10608 | TREE_TYPE (stmt) = void_type_node; | |
10609 | OMP_TASK_CLAUSES (stmt) = clauses; | |
10610 | OMP_TASK_BODY (stmt) = block; | |
e60a6f7b | 10611 | SET_EXPR_LOCATION (stmt, loc); |
fd6481cf | 10612 | |
10613 | return add_stmt (stmt); | |
10614 | } | |
10615 | ||
1e8e9920 | 10616 | /* For all elements of CLAUSES, validate them vs OpenMP constraints. |
10617 | Remove any elements from the list that are invalid. */ | |
10618 | ||
10619 | tree | |
10620 | c_finish_omp_clauses (tree clauses) | |
10621 | { | |
10622 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
10623 | tree c, t, *pc = &clauses; | |
10624 | const char *name; | |
10625 | ||
10626 | bitmap_obstack_initialize (NULL); | |
10627 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
10628 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
10629 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
10630 | ||
10631 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
10632 | { | |
10633 | bool remove = false; | |
10634 | bool need_complete = false; | |
10635 | bool need_implicitly_determined = false; | |
10636 | ||
55d6e7cd | 10637 | switch (OMP_CLAUSE_CODE (c)) |
1e8e9920 | 10638 | { |
10639 | case OMP_CLAUSE_SHARED: | |
10640 | name = "shared"; | |
10641 | need_implicitly_determined = true; | |
10642 | goto check_dup_generic; | |
10643 | ||
10644 | case OMP_CLAUSE_PRIVATE: | |
10645 | name = "private"; | |
10646 | need_complete = true; | |
10647 | need_implicitly_determined = true; | |
10648 | goto check_dup_generic; | |
10649 | ||
10650 | case OMP_CLAUSE_REDUCTION: | |
10651 | name = "reduction"; | |
10652 | need_implicitly_determined = true; | |
10653 | t = OMP_CLAUSE_DECL (c); | |
10654 | if (AGGREGATE_TYPE_P (TREE_TYPE (t)) | |
10655 | || POINTER_TYPE_P (TREE_TYPE (t))) | |
10656 | { | |
e60a6f7b | 10657 | error_at (OMP_CLAUSE_LOCATION (c), |
10658 | "%qE has invalid type for %<reduction%>", t); | |
1e8e9920 | 10659 | remove = true; |
10660 | } | |
10661 | else if (FLOAT_TYPE_P (TREE_TYPE (t))) | |
10662 | { | |
10663 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
10664 | const char *r_name = NULL; | |
10665 | ||
10666 | switch (r_code) | |
10667 | { | |
10668 | case PLUS_EXPR: | |
10669 | case MULT_EXPR: | |
10670 | case MINUS_EXPR: | |
2169f33b | 10671 | case MIN_EXPR: |
10672 | case MAX_EXPR: | |
1e8e9920 | 10673 | break; |
10674 | case BIT_AND_EXPR: | |
10675 | r_name = "&"; | |
10676 | break; | |
10677 | case BIT_XOR_EXPR: | |
10678 | r_name = "^"; | |
10679 | break; | |
10680 | case BIT_IOR_EXPR: | |
10681 | r_name = "|"; | |
10682 | break; | |
10683 | case TRUTH_ANDIF_EXPR: | |
10684 | r_name = "&&"; | |
10685 | break; | |
10686 | case TRUTH_ORIF_EXPR: | |
10687 | r_name = "||"; | |
10688 | break; | |
10689 | default: | |
10690 | gcc_unreachable (); | |
10691 | } | |
10692 | if (r_name) | |
10693 | { | |
e60a6f7b | 10694 | error_at (OMP_CLAUSE_LOCATION (c), |
10695 | "%qE has invalid type for %<reduction(%s)%>", | |
10696 | t, r_name); | |
1e8e9920 | 10697 | remove = true; |
10698 | } | |
10699 | } | |
10700 | goto check_dup_generic; | |
10701 | ||
10702 | case OMP_CLAUSE_COPYPRIVATE: | |
10703 | name = "copyprivate"; | |
10704 | goto check_dup_generic; | |
10705 | ||
10706 | case OMP_CLAUSE_COPYIN: | |
10707 | name = "copyin"; | |
10708 | t = OMP_CLAUSE_DECL (c); | |
10709 | if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) | |
10710 | { | |
e60a6f7b | 10711 | error_at (OMP_CLAUSE_LOCATION (c), |
10712 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
1e8e9920 | 10713 | remove = true; |
10714 | } | |
10715 | goto check_dup_generic; | |
10716 | ||
10717 | check_dup_generic: | |
10718 | t = OMP_CLAUSE_DECL (c); | |
10719 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10720 | { | |
e60a6f7b | 10721 | error_at (OMP_CLAUSE_LOCATION (c), |
10722 | "%qE is not a variable in clause %qs", t, name); | |
1e8e9920 | 10723 | remove = true; |
10724 | } | |
10725 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10726 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
10727 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10728 | { | |
e60a6f7b | 10729 | error_at (OMP_CLAUSE_LOCATION (c), |
10730 | "%qE appears more than once in data clauses", t); | |
1e8e9920 | 10731 | remove = true; |
10732 | } | |
10733 | else | |
10734 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
10735 | break; | |
10736 | ||
10737 | case OMP_CLAUSE_FIRSTPRIVATE: | |
10738 | name = "firstprivate"; | |
10739 | t = OMP_CLAUSE_DECL (c); | |
10740 | need_complete = true; | |
10741 | need_implicitly_determined = true; | |
10742 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10743 | { | |
e60a6f7b | 10744 | error_at (OMP_CLAUSE_LOCATION (c), |
10745 | "%qE is not a variable in clause %<firstprivate%>", t); | |
1e8e9920 | 10746 | remove = true; |
10747 | } | |
10748 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10749 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
10750 | { | |
e60a6f7b | 10751 | error_at (OMP_CLAUSE_LOCATION (c), |
10752 | "%qE appears more than once in data clauses", t); | |
1e8e9920 | 10753 | remove = true; |
10754 | } | |
10755 | else | |
10756 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
10757 | break; | |
10758 | ||
10759 | case OMP_CLAUSE_LASTPRIVATE: | |
10760 | name = "lastprivate"; | |
10761 | t = OMP_CLAUSE_DECL (c); | |
10762 | need_complete = true; | |
10763 | need_implicitly_determined = true; | |
10764 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10765 | { | |
e60a6f7b | 10766 | error_at (OMP_CLAUSE_LOCATION (c), |
10767 | "%qE is not a variable in clause %<lastprivate%>", t); | |
1e8e9920 | 10768 | remove = true; |
10769 | } | |
10770 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10771 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10772 | { | |
e60a6f7b | 10773 | error_at (OMP_CLAUSE_LOCATION (c), |
10774 | "%qE appears more than once in data clauses", t); | |
1e8e9920 | 10775 | remove = true; |
10776 | } | |
10777 | else | |
10778 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
10779 | break; | |
10780 | ||
10781 | case OMP_CLAUSE_IF: | |
10782 | case OMP_CLAUSE_NUM_THREADS: | |
10783 | case OMP_CLAUSE_SCHEDULE: | |
10784 | case OMP_CLAUSE_NOWAIT: | |
10785 | case OMP_CLAUSE_ORDERED: | |
10786 | case OMP_CLAUSE_DEFAULT: | |
fd6481cf | 10787 | case OMP_CLAUSE_UNTIED: |
10788 | case OMP_CLAUSE_COLLAPSE: | |
2169f33b | 10789 | case OMP_CLAUSE_FINAL: |
10790 | case OMP_CLAUSE_MERGEABLE: | |
1e8e9920 | 10791 | pc = &OMP_CLAUSE_CHAIN (c); |
10792 | continue; | |
10793 | ||
10794 | default: | |
10795 | gcc_unreachable (); | |
10796 | } | |
10797 | ||
10798 | if (!remove) | |
10799 | { | |
10800 | t = OMP_CLAUSE_DECL (c); | |
10801 | ||
10802 | if (need_complete) | |
10803 | { | |
10804 | t = require_complete_type (t); | |
10805 | if (t == error_mark_node) | |
10806 | remove = true; | |
10807 | } | |
10808 | ||
10809 | if (need_implicitly_determined) | |
10810 | { | |
10811 | const char *share_name = NULL; | |
10812 | ||
10813 | if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
10814 | share_name = "threadprivate"; | |
10815 | else switch (c_omp_predetermined_sharing (t)) | |
10816 | { | |
10817 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
10818 | break; | |
10819 | case OMP_CLAUSE_DEFAULT_SHARED: | |
2169f33b | 10820 | /* const vars may be specified in firstprivate clause. */ |
10821 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
10822 | && TREE_READONLY (t)) | |
10823 | break; | |
1e8e9920 | 10824 | share_name = "shared"; |
10825 | break; | |
10826 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
10827 | share_name = "private"; | |
10828 | break; | |
10829 | default: | |
10830 | gcc_unreachable (); | |
10831 | } | |
10832 | if (share_name) | |
10833 | { | |
e60a6f7b | 10834 | error_at (OMP_CLAUSE_LOCATION (c), |
10835 | "%qE is predetermined %qs for %qs", | |
10836 | t, share_name, name); | |
1e8e9920 | 10837 | remove = true; |
10838 | } | |
10839 | } | |
10840 | } | |
10841 | ||
10842 | if (remove) | |
10843 | *pc = OMP_CLAUSE_CHAIN (c); | |
10844 | else | |
10845 | pc = &OMP_CLAUSE_CHAIN (c); | |
10846 | } | |
10847 | ||
10848 | bitmap_obstack_release (NULL); | |
10849 | return clauses; | |
10850 | } | |
796735dc | 10851 | |
4c0315d0 | 10852 | /* Create a transaction node. */ |
10853 | ||
10854 | tree | |
10855 | c_finish_transaction (location_t loc, tree block, int flags) | |
10856 | { | |
10857 | tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); | |
10858 | if (flags & TM_STMT_ATTR_OUTER) | |
10859 | TRANSACTION_EXPR_OUTER (stmt) = 1; | |
10860 | if (flags & TM_STMT_ATTR_RELAXED) | |
10861 | TRANSACTION_EXPR_RELAXED (stmt) = 1; | |
10862 | return add_stmt (stmt); | |
10863 | } | |
10864 | ||
796735dc | 10865 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
10866 | down to the element type of an array. */ | |
10867 | ||
10868 | tree | |
10869 | c_build_qualified_type (tree type, int type_quals) | |
10870 | { | |
10871 | if (type == error_mark_node) | |
10872 | return type; | |
10873 | ||
10874 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10875 | { | |
10876 | tree t; | |
10877 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
10878 | type_quals); | |
10879 | ||
10880 | /* See if we already have an identically qualified type. */ | |
10881 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
10882 | { | |
10883 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
10884 | && TYPE_NAME (t) == TYPE_NAME (type) | |
10885 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
10886 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
10887 | TYPE_ATTRIBUTES (type))) | |
10888 | break; | |
10889 | } | |
10890 | if (!t) | |
10891 | { | |
10892 | tree domain = TYPE_DOMAIN (type); | |
10893 | ||
10894 | t = build_variant_type_copy (type); | |
10895 | TREE_TYPE (t) = element_type; | |
10896 | ||
10897 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
10898 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
10899 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
10900 | else if (TYPE_CANONICAL (element_type) != element_type | |
10901 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
10902 | { | |
48e1416a | 10903 | tree unqualified_canon |
796735dc | 10904 | = build_array_type (TYPE_CANONICAL (element_type), |
48e1416a | 10905 | domain? TYPE_CANONICAL (domain) |
796735dc | 10906 | : NULL_TREE); |
48e1416a | 10907 | TYPE_CANONICAL (t) |
796735dc | 10908 | = c_build_qualified_type (unqualified_canon, type_quals); |
10909 | } | |
10910 | else | |
10911 | TYPE_CANONICAL (t) = t; | |
10912 | } | |
10913 | return t; | |
10914 | } | |
10915 | ||
10916 | /* A restrict-qualified pointer type must be a pointer to object or | |
10917 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
10918 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
10919 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
10920 | && (!POINTER_TYPE_P (type) | |
10921 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
10922 | { | |
10923 | error ("invalid use of %<restrict%>"); | |
10924 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
10925 | } | |
10926 | ||
10927 | return build_qualified_type (type, type_quals); | |
10928 | } | |
d62e827b | 10929 | |
10930 | /* Build a VA_ARG_EXPR for the C parser. */ | |
10931 | ||
10932 | tree | |
e60a6f7b | 10933 | c_build_va_arg (location_t loc, tree expr, tree type) |
d62e827b | 10934 | { |
10935 | if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) | |
10936 | warning_at (loc, OPT_Wc___compat, | |
10937 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
e60a6f7b | 10938 | return build_va_arg (loc, expr, type); |
d62e827b | 10939 | } |