]>
Commit | Line | Data |
---|---|---|
400fbf9f | 1 | /* Build expressions with type checking for C compiler. |
5624e564 | 2 | Copyright (C) 1987-2015 Free Software Foundation, Inc. |
400fbf9f | 3 | |
1322177d | 4 | This file is part of GCC. |
400fbf9f | 5 | |
1322177d LB |
6 | GCC is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 9 | version. |
400fbf9f | 10 | |
1322177d LB |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
400fbf9f JW |
15 | |
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
400fbf9f JW |
19 | |
20 | ||
21 | /* This file is part of the C front end. | |
22 | It contains routines to build C expressions given their operands, | |
23 | including computing the types of the result, C-specific error checks, | |
5088b058 | 24 | and some optimization. */ |
400fbf9f JW |
25 | |
26 | #include "config.h" | |
670ee920 | 27 | #include "system.h" |
4977bab6 | 28 | #include "coretypes.h" |
2adfab87 AM |
29 | #include "target.h" |
30 | #include "function.h" | |
31 | #include "bitmap.h" | |
400fbf9f | 32 | #include "tree.h" |
2adfab87 AM |
33 | #include "c-family/c-common.h" |
34 | #include "c-tree.h" | |
35 | #include "gimple-expr.h" | |
36 | #include "predict.h" | |
d8a2d370 DN |
37 | #include "stor-layout.h" |
38 | #include "trans-mem.h" | |
39 | #include "varasm.h" | |
40 | #include "stmt.h" | |
e57e265b | 41 | #include "langhooks.h" |
29cc57cf | 42 | #include "c-lang.h" |
400fbf9f | 43 | #include "flags.h" |
ab87f8c8 | 44 | #include "intl.h" |
325c3691 | 45 | #include "tree-iterator.h" |
45b0be94 | 46 | #include "gimplify.h" |
acf0174b | 47 | #include "tree-inline.h" |
0645c1a2 | 48 | #include "omp-low.h" |
61d3ce20 | 49 | #include "c-family/c-objc.h" |
de5a5fa1 | 50 | #include "c-family/c-ubsan.h" |
12893402 | 51 | #include "cilk.h" |
41dbbb37 | 52 | #include "gomp-constants.h" |
325c3691 | 53 | |
2ac2f164 JM |
54 | /* Possible cases of implicit bad conversions. Used to select |
55 | diagnostic messages in convert_for_assignment. */ | |
56 | enum impl_conv { | |
57 | ic_argpass, | |
58 | ic_assign, | |
59 | ic_init, | |
60 | ic_return | |
61 | }; | |
62 | ||
bc4b653b JM |
63 | /* The level of nesting inside "__alignof__". */ |
64 | int in_alignof; | |
65 | ||
66 | /* The level of nesting inside "sizeof". */ | |
67 | int in_sizeof; | |
68 | ||
69 | /* The level of nesting inside "typeof". */ | |
70 | int in_typeof; | |
400fbf9f | 71 | |
1a4049e7 JJ |
72 | /* The argument of last parsed sizeof expression, only to be tested |
73 | if expr.original_code == SIZEOF_EXPR. */ | |
74 | tree c_last_sizeof_arg; | |
75 | ||
9bac5cbb G |
76 | /* Nonzero if we might need to print a "missing braces around |
77 | initializer" message within this initializer. */ | |
78 | static int found_missing_braces; | |
103b7b17 | 79 | |
bf730f15 RS |
80 | static int require_constant_value; |
81 | static int require_constant_elements; | |
82 | ||
58f9752a | 83 | static bool null_pointer_constant_p (const_tree); |
f55ade6e | 84 | static tree qualify_type (tree, tree); |
dc5027f4 JM |
85 | static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *, |
86 | bool *); | |
744aa42f | 87 | static int comp_target_types (location_t, tree, tree); |
dc5027f4 JM |
88 | static int function_types_compatible_p (const_tree, const_tree, bool *, |
89 | bool *); | |
90 | static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *); | |
f55ade6e | 91 | static tree lookup_field (tree, tree); |
81e5eca8 MP |
92 | static int convert_arguments (location_t, vec<location_t>, tree, |
93 | vec<tree, va_gc> *, vec<tree, va_gc> *, tree, | |
94 | tree); | |
db3927fb | 95 | static tree pointer_diff (location_t, tree, tree); |
68fca595 | 96 | static tree convert_for_assignment (location_t, location_t, tree, tree, tree, |
744aa42f | 97 | enum impl_conv, bool, tree, tree, int); |
f55ade6e AJ |
98 | static tree valid_compound_expr_initializer (tree, tree); |
99 | static void push_string (const char *); | |
100 | static void push_member_name (tree); | |
f55ade6e AJ |
101 | static int spelling_length (void); |
102 | static char *print_spelling (char *); | |
96b40f8d | 103 | static void warning_init (location_t, int, const char *); |
c2255bc4 | 104 | static tree digest_init (location_t, tree, tree, tree, bool, bool, int); |
34cf811f MP |
105 | static void output_init_element (location_t, tree, tree, bool, tree, tree, int, |
106 | bool, struct obstack *); | |
a1e3b3d9 | 107 | static void output_pending_init_elements (int, struct obstack *); |
ea58ef42 | 108 | static int set_designator (location_t, int, struct obstack *); |
a1e3b3d9 | 109 | static void push_range_stack (tree, struct obstack *); |
96b40f8d MP |
110 | static void add_pending_init (location_t, tree, tree, tree, bool, |
111 | struct obstack *); | |
a1e3b3d9 LB |
112 | static void set_nonincremental_init (struct obstack *); |
113 | static void set_nonincremental_init_from_string (tree, struct obstack *); | |
114 | static tree find_init_member (tree, struct obstack *); | |
f37acdf9 | 115 | static void readonly_warning (tree, enum lvalue_use); |
7bd11157 | 116 | static int lvalue_or_else (location_t, const_tree, enum lvalue_use); |
4e2fb7de | 117 | static void record_maybe_used_decl (tree); |
dc5027f4 | 118 | static int comptypes_internal (const_tree, const_tree, bool *, bool *); |
6aa3c60d JM |
119 | \f |
120 | /* Return true if EXP is a null pointer constant, false otherwise. */ | |
121 | ||
122 | static bool | |
58f9752a | 123 | null_pointer_constant_p (const_tree expr) |
6aa3c60d JM |
124 | { |
125 | /* This should really operate on c_expr structures, but they aren't | |
126 | yet available everywhere required. */ | |
127 | tree type = TREE_TYPE (expr); | |
128 | return (TREE_CODE (expr) == INTEGER_CST | |
8bcd6380 | 129 | && !TREE_OVERFLOW (expr) |
6aa3c60d JM |
130 | && integer_zerop (expr) |
131 | && (INTEGRAL_TYPE_P (type) | |
132 | || (TREE_CODE (type) == POINTER_TYPE | |
133 | && VOID_TYPE_P (TREE_TYPE (type)) | |
134 | && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); | |
135 | } | |
928c19bb JM |
136 | |
137 | /* EXPR may appear in an unevaluated part of an integer constant | |
138 | expression, but not in an evaluated part. Wrap it in a | |
139 | C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an | |
140 | INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ | |
141 | ||
142 | static tree | |
143 | note_integer_operands (tree expr) | |
144 | { | |
145 | tree ret; | |
146 | if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) | |
147 | { | |
148 | ret = copy_node (expr); | |
149 | TREE_OVERFLOW (ret) = 1; | |
150 | } | |
151 | else | |
152 | { | |
153 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); | |
154 | C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; | |
155 | } | |
156 | return ret; | |
157 | } | |
158 | ||
4d84fe7c JM |
159 | /* Having checked whether EXPR may appear in an unevaluated part of an |
160 | integer constant expression and found that it may, remove any | |
161 | C_MAYBE_CONST_EXPR noting this fact and return the resulting | |
162 | expression. */ | |
163 | ||
164 | static inline tree | |
165 | remove_c_maybe_const_expr (tree expr) | |
166 | { | |
167 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
168 | return C_MAYBE_CONST_EXPR_EXPR (expr); | |
169 | else | |
170 | return expr; | |
171 | } | |
172 | ||
f13c9b2c AP |
173 | \f/* This is a cache to hold if two types are compatible or not. */ |
174 | ||
175 | struct tagged_tu_seen_cache { | |
176 | const struct tagged_tu_seen_cache * next; | |
58f9752a KG |
177 | const_tree t1; |
178 | const_tree t2; | |
f13c9b2c AP |
179 | /* The return value of tagged_types_tu_compatible_p if we had seen |
180 | these two types already. */ | |
181 | int val; | |
182 | }; | |
183 | ||
184 | static const struct tagged_tu_seen_cache * tagged_tu_seen_base; | |
185 | static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); | |
186 | ||
400fbf9f JW |
187 | /* Do `exp = require_complete_type (exp);' to make sure exp |
188 | does not have an incomplete type. (That includes void types.) */ | |
189 | ||
190 | tree | |
2f6e4e97 | 191 | require_complete_type (tree value) |
400fbf9f JW |
192 | { |
193 | tree type = TREE_TYPE (value); | |
194 | ||
7a0ca710 | 195 | if (error_operand_p (value)) |
ea0f786b CB |
196 | return error_mark_node; |
197 | ||
400fbf9f | 198 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 199 | if (COMPLETE_TYPE_P (type)) |
400fbf9f JW |
200 | return value; |
201 | ||
7a228918 | 202 | c_incomplete_type_error (value, type); |
400fbf9f JW |
203 | return error_mark_node; |
204 | } | |
205 | ||
206 | /* Print an error message for invalid use of an incomplete type. | |
207 | VALUE is the expression that was used (or 0 if that isn't known) | |
208 | and TYPE is the type that was invalid. */ | |
209 | ||
210 | void | |
ac7d7749 | 211 | c_incomplete_type_error (const_tree value, const_tree type) |
400fbf9f | 212 | { |
400fbf9f JW |
213 | /* Avoid duplicate error message. */ |
214 | if (TREE_CODE (type) == ERROR_MARK) | |
215 | return; | |
216 | ||
0ae9bd27 | 217 | if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL)) |
ac9f18db | 218 | error ("%qD has an incomplete type %qT", value, type); |
400fbf9f JW |
219 | else |
220 | { | |
221 | retry: | |
222 | /* We must print an error message. Be clever about what it says. */ | |
223 | ||
224 | switch (TREE_CODE (type)) | |
225 | { | |
226 | case RECORD_TYPE: | |
400fbf9f | 227 | case UNION_TYPE: |
400fbf9f | 228 | case ENUMERAL_TYPE: |
400fbf9f JW |
229 | break; |
230 | ||
231 | case VOID_TYPE: | |
232 | error ("invalid use of void expression"); | |
233 | return; | |
234 | ||
235 | case ARRAY_TYPE: | |
236 | if (TYPE_DOMAIN (type)) | |
237 | { | |
fba78abb RH |
238 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) |
239 | { | |
240 | error ("invalid use of flexible array member"); | |
241 | return; | |
242 | } | |
400fbf9f JW |
243 | type = TREE_TYPE (type); |
244 | goto retry; | |
245 | } | |
246 | error ("invalid use of array with unspecified bounds"); | |
247 | return; | |
248 | ||
249 | default: | |
366de0ce | 250 | gcc_unreachable (); |
400fbf9f JW |
251 | } |
252 | ||
253 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
ac9f18db | 254 | error ("invalid use of undefined type %qT", type); |
400fbf9f JW |
255 | else |
256 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
ac9f18db | 257 | error ("invalid use of incomplete typedef %qT", type); |
400fbf9f JW |
258 | } |
259 | } | |
260 | ||
ab393bf1 NB |
261 | /* Given a type, apply default promotions wrt unnamed function |
262 | arguments and return the new type. */ | |
263 | ||
264 | tree | |
2f6e4e97 | 265 | c_type_promotes_to (tree type) |
ab393bf1 | 266 | { |
267bac10 | 267 | tree ret = NULL_TREE; |
ab393bf1 | 268 | |
267bac10 JM |
269 | if (TYPE_MAIN_VARIANT (type) == float_type_node) |
270 | ret = double_type_node; | |
271 | else if (c_promoting_integer_type_p (type)) | |
ab393bf1 NB |
272 | { |
273 | /* Preserve unsignedness if not really getting any wider. */ | |
8df83eae | 274 | if (TYPE_UNSIGNED (type) |
c22cacf3 | 275 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) |
267bac10 JM |
276 | ret = unsigned_type_node; |
277 | else | |
278 | ret = integer_type_node; | |
ab393bf1 NB |
279 | } |
280 | ||
267bac10 JM |
281 | if (ret != NULL_TREE) |
282 | return (TYPE_ATOMIC (type) | |
283 | ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC) | |
284 | : ret); | |
285 | ||
ab393bf1 NB |
286 | return type; |
287 | } | |
288 | ||
36c5e70a BE |
289 | /* Return true if between two named address spaces, whether there is a superset |
290 | named address space that encompasses both address spaces. If there is a | |
291 | superset, return which address space is the superset. */ | |
292 | ||
293 | static bool | |
294 | addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) | |
295 | { | |
296 | if (as1 == as2) | |
297 | { | |
298 | *common = as1; | |
299 | return true; | |
300 | } | |
301 | else if (targetm.addr_space.subset_p (as1, as2)) | |
302 | { | |
303 | *common = as2; | |
304 | return true; | |
305 | } | |
306 | else if (targetm.addr_space.subset_p (as2, as1)) | |
307 | { | |
308 | *common = as1; | |
309 | return true; | |
310 | } | |
311 | else | |
312 | return false; | |
313 | } | |
314 | ||
400fbf9f JW |
315 | /* Return a variant of TYPE which has all the type qualifiers of LIKE |
316 | as well as those of TYPE. */ | |
317 | ||
318 | static tree | |
2f6e4e97 | 319 | qualify_type (tree type, tree like) |
400fbf9f | 320 | { |
36c5e70a BE |
321 | addr_space_t as_type = TYPE_ADDR_SPACE (type); |
322 | addr_space_t as_like = TYPE_ADDR_SPACE (like); | |
323 | addr_space_t as_common; | |
324 | ||
325 | /* If the two named address spaces are different, determine the common | |
326 | superset address space. If there isn't one, raise an error. */ | |
327 | if (!addr_space_superset (as_type, as_like, &as_common)) | |
328 | { | |
329 | as_common = as_type; | |
330 | error ("%qT and %qT are in disjoint named address spaces", | |
331 | type, like); | |
332 | } | |
333 | ||
2f6e4e97 | 334 | return c_build_qualified_type (type, |
36c5e70a | 335 | TYPE_QUALS_NO_ADDR_SPACE (type) |
267bac10 | 336 | | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like) |
36c5e70a | 337 | | ENCODE_QUAL_ADDR_SPACE (as_common)); |
400fbf9f | 338 | } |
52ffd86e MS |
339 | |
340 | /* Return true iff the given tree T is a variable length array. */ | |
341 | ||
342 | bool | |
ac7d7749 | 343 | c_vla_type_p (const_tree t) |
52ffd86e MS |
344 | { |
345 | if (TREE_CODE (t) == ARRAY_TYPE | |
346 | && C_TYPE_VARIABLE_SIZE (t)) | |
347 | return true; | |
348 | return false; | |
349 | } | |
400fbf9f | 350 | \f |
10bc1b1b | 351 | /* Return the composite type of two compatible types. |
5305f6d7 | 352 | |
10bc1b1b JM |
353 | We assume that comptypes has already been done and returned |
354 | nonzero; if that isn't so, this may crash. In particular, we | |
355 | assume that qualifiers match. */ | |
400fbf9f JW |
356 | |
357 | tree | |
10bc1b1b | 358 | composite_type (tree t1, tree t2) |
400fbf9f | 359 | { |
b3694847 SS |
360 | enum tree_code code1; |
361 | enum tree_code code2; | |
4b027d16 | 362 | tree attributes; |
400fbf9f JW |
363 | |
364 | /* Save time if the two types are the same. */ | |
365 | ||
366 | if (t1 == t2) return t1; | |
367 | ||
368 | /* If one type is nonsense, use the other. */ | |
369 | if (t1 == error_mark_node) | |
370 | return t2; | |
371 | if (t2 == error_mark_node) | |
372 | return t1; | |
373 | ||
10bc1b1b JM |
374 | code1 = TREE_CODE (t1); |
375 | code2 = TREE_CODE (t2); | |
376 | ||
d9525bec | 377 | /* Merge the attributes. */ |
5fd9b178 | 378 | attributes = targetm.merge_type_attributes (t1, t2); |
4b027d16 | 379 | |
10bc1b1b JM |
380 | /* If one is an enumerated type and the other is the compatible |
381 | integer type, the composite type might be either of the two | |
382 | (DR#013 question 3). For consistency, use the enumerated type as | |
383 | the composite type. */ | |
400fbf9f | 384 | |
10bc1b1b JM |
385 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) |
386 | return t1; | |
387 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
388 | return t2; | |
75326e8c | 389 | |
366de0ce | 390 | gcc_assert (code1 == code2); |
b6a10c9f | 391 | |
400fbf9f JW |
392 | switch (code1) |
393 | { | |
400fbf9f | 394 | case POINTER_TYPE: |
10bc1b1b | 395 | /* For two pointers, do this recursively on the target type. */ |
400fbf9f | 396 | { |
3932261a MM |
397 | tree pointed_to_1 = TREE_TYPE (t1); |
398 | tree pointed_to_2 = TREE_TYPE (t2); | |
10bc1b1b | 399 | tree target = composite_type (pointed_to_1, pointed_to_2); |
3db684fb | 400 | t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false); |
fe7080d2 AP |
401 | t1 = build_type_attribute_variant (t1, attributes); |
402 | return qualify_type (t1, t2); | |
400fbf9f | 403 | } |
400fbf9f JW |
404 | |
405 | case ARRAY_TYPE: | |
406 | { | |
10bc1b1b | 407 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
46df2823 JM |
408 | int quals; |
409 | tree unqual_elt; | |
ca8bdb78 JM |
410 | tree d1 = TYPE_DOMAIN (t1); |
411 | tree d2 = TYPE_DOMAIN (t2); | |
412 | bool d1_variable, d2_variable; | |
413 | bool d1_zero, d2_zero; | |
f6294de7 | 414 | bool t1_complete, t2_complete; |
46df2823 | 415 | |
de46b2fe | 416 | /* We should not have any type quals on arrays at all. */ |
36c5e70a BE |
417 | gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) |
418 | && !TYPE_QUALS_NO_ADDR_SPACE (t2)); | |
c22cacf3 | 419 | |
f6294de7 JM |
420 | t1_complete = COMPLETE_TYPE_P (t1); |
421 | t2_complete = COMPLETE_TYPE_P (t2); | |
422 | ||
ca8bdb78 JM |
423 | d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); |
424 | d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); | |
425 | ||
426 | d1_variable = (!d1_zero | |
427 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
428 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
429 | d2_variable = (!d2_zero | |
430 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
431 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
432 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
433 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
ca8bdb78 | 434 | |
400fbf9f | 435 | /* Save space: see if the result is identical to one of the args. */ |
ca8bdb78 JM |
436 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) |
437 | && (d2_variable || d2_zero || !d1_variable)) | |
4b027d16 | 438 | return build_type_attribute_variant (t1, attributes); |
ca8bdb78 JM |
439 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) |
440 | && (d1_variable || d1_zero || !d2_variable)) | |
4b027d16 | 441 | return build_type_attribute_variant (t2, attributes); |
c22cacf3 | 442 | |
de46b2fe AP |
443 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) |
444 | return build_type_attribute_variant (t1, attributes); | |
445 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
446 | return build_type_attribute_variant (t2, attributes); | |
c22cacf3 | 447 | |
46df2823 JM |
448 | /* Merge the element types, and have a size if either arg has |
449 | one. We may have qualifiers on the element types. To set | |
450 | up TYPE_MAIN_VARIANT correctly, we need to form the | |
451 | composite of the unqualified types and add the qualifiers | |
452 | back at the end. */ | |
453 | quals = TYPE_QUALS (strip_array_types (elt)); | |
454 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
455 | t1 = build_array_type (unqual_elt, | |
ca8bdb78 JM |
456 | TYPE_DOMAIN ((TYPE_DOMAIN (t1) |
457 | && (d2_variable | |
458 | || d2_zero | |
459 | || !d1_variable)) | |
460 | ? t1 | |
461 | : t2)); | |
f6294de7 JM |
462 | /* Ensure a composite type involving a zero-length array type |
463 | is a zero-length type not an incomplete type. */ | |
464 | if (d1_zero && d2_zero | |
465 | && (t1_complete || t2_complete) | |
466 | && !COMPLETE_TYPE_P (t1)) | |
467 | { | |
468 | TYPE_SIZE (t1) = bitsize_zero_node; | |
469 | TYPE_SIZE_UNIT (t1) = size_zero_node; | |
470 | } | |
46df2823 | 471 | t1 = c_build_qualified_type (t1, quals); |
de46b2fe | 472 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
473 | } |
474 | ||
fcb99e7b JJ |
475 | case ENUMERAL_TYPE: |
476 | case RECORD_TYPE: | |
477 | case UNION_TYPE: | |
478 | if (attributes != NULL) | |
479 | { | |
480 | /* Try harder not to create a new aggregate type. */ | |
481 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
482 | return t1; | |
483 | if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
484 | return t2; | |
485 | } | |
486 | return build_type_attribute_variant (t1, attributes); | |
487 | ||
400fbf9f JW |
488 | case FUNCTION_TYPE: |
489 | /* Function types: prefer the one that specified arg types. | |
490 | If both do, merge the arg types. Also merge the return types. */ | |
491 | { | |
10bc1b1b | 492 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
400fbf9f JW |
493 | tree p1 = TYPE_ARG_TYPES (t1); |
494 | tree p2 = TYPE_ARG_TYPES (t2); | |
495 | int len; | |
496 | tree newargs, n; | |
497 | int i; | |
498 | ||
499 | /* Save space: see if the result is identical to one of the args. */ | |
3f75a254 | 500 | if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) |
4b027d16 | 501 | return build_type_attribute_variant (t1, attributes); |
3f75a254 | 502 | if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) |
4b027d16 | 503 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
504 | |
505 | /* Simple way if one arg fails to specify argument types. */ | |
506 | if (TYPE_ARG_TYPES (t1) == 0) | |
4b027d16 | 507 | { |
fe7080d2 AP |
508 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); |
509 | t1 = build_type_attribute_variant (t1, attributes); | |
510 | return qualify_type (t1, t2); | |
4b027d16 | 511 | } |
400fbf9f | 512 | if (TYPE_ARG_TYPES (t2) == 0) |
4b027d16 RK |
513 | { |
514 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
fe7080d2 AP |
515 | t1 = build_type_attribute_variant (t1, attributes); |
516 | return qualify_type (t1, t2); | |
4b027d16 | 517 | } |
400fbf9f JW |
518 | |
519 | /* If both args specify argument types, we must merge the two | |
520 | lists, argument by argument. */ | |
2f4e8f2b | 521 | |
400fbf9f JW |
522 | len = list_length (p1); |
523 | newargs = 0; | |
524 | ||
525 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 526 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
527 | |
528 | n = newargs; | |
529 | ||
530 | for (; p1; | |
531 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
532 | { | |
533 | /* A null type means arg type is not specified. | |
534 | Take whatever the other function type has. */ | |
535 | if (TREE_VALUE (p1) == 0) | |
536 | { | |
537 | TREE_VALUE (n) = TREE_VALUE (p2); | |
538 | goto parm_done; | |
539 | } | |
540 | if (TREE_VALUE (p2) == 0) | |
541 | { | |
542 | TREE_VALUE (n) = TREE_VALUE (p1); | |
543 | goto parm_done; | |
544 | } | |
2f6e4e97 | 545 | |
400fbf9f JW |
546 | /* Given wait (union {union wait *u; int *i} *) |
547 | and wait (union wait *), | |
548 | prefer union wait * as type of parm. */ | |
549 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
550 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
551 | { | |
552 | tree memb; | |
58cb41e6 JJ |
553 | tree mv2 = TREE_VALUE (p2); |
554 | if (mv2 && mv2 != error_mark_node | |
555 | && TREE_CODE (mv2) != ARRAY_TYPE) | |
556 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
400fbf9f | 557 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); |
910ad8de | 558 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
559 | { |
560 | tree mv3 = TREE_TYPE (memb); | |
561 | if (mv3 && mv3 != error_mark_node | |
562 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
563 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
564 | if (comptypes (mv3, mv2)) | |
565 | { | |
566 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
567 | TREE_VALUE (p2)); | |
c1771a20 | 568 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 569 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
570 | goto parm_done; |
571 | } | |
572 | } | |
400fbf9f JW |
573 | } |
574 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
575 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
576 | { | |
577 | tree memb; | |
58cb41e6 JJ |
578 | tree mv1 = TREE_VALUE (p1); |
579 | if (mv1 && mv1 != error_mark_node | |
580 | && TREE_CODE (mv1) != ARRAY_TYPE) | |
581 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
400fbf9f | 582 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); |
910ad8de | 583 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
584 | { |
585 | tree mv3 = TREE_TYPE (memb); | |
586 | if (mv3 && mv3 != error_mark_node | |
587 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
588 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
589 | if (comptypes (mv3, mv1)) | |
590 | { | |
591 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
592 | TREE_VALUE (p1)); | |
c1771a20 | 593 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 594 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
595 | goto parm_done; |
596 | } | |
597 | } | |
400fbf9f | 598 | } |
10bc1b1b | 599 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); |
400fbf9f JW |
600 | parm_done: ; |
601 | } | |
602 | ||
4b027d16 | 603 | t1 = build_function_type (valtype, newargs); |
fe7080d2 | 604 | t1 = qualify_type (t1, t2); |
0f41302f | 605 | /* ... falls through ... */ |
400fbf9f JW |
606 | } |
607 | ||
608 | default: | |
4b027d16 | 609 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
610 | } |
611 | ||
612 | } | |
10bc1b1b JM |
613 | |
614 | /* Return the type of a conditional expression between pointers to | |
615 | possibly differently qualified versions of compatible types. | |
616 | ||
617 | We assume that comp_target_types has already been done and returned | |
618 | nonzero; if that isn't so, this may crash. */ | |
619 | ||
620 | static tree | |
621 | common_pointer_type (tree t1, tree t2) | |
622 | { | |
623 | tree attributes; | |
46df2823 JM |
624 | tree pointed_to_1, mv1; |
625 | tree pointed_to_2, mv2; | |
10bc1b1b | 626 | tree target; |
eb1387a0 | 627 | unsigned target_quals; |
36c5e70a BE |
628 | addr_space_t as1, as2, as_common; |
629 | int quals1, quals2; | |
10bc1b1b JM |
630 | |
631 | /* Save time if the two types are the same. */ | |
632 | ||
633 | if (t1 == t2) return t1; | |
634 | ||
635 | /* If one type is nonsense, use the other. */ | |
636 | if (t1 == error_mark_node) | |
637 | return t2; | |
638 | if (t2 == error_mark_node) | |
639 | return t1; | |
640 | ||
366de0ce | 641 | gcc_assert (TREE_CODE (t1) == POINTER_TYPE |
c22cacf3 | 642 | && TREE_CODE (t2) == POINTER_TYPE); |
10bc1b1b JM |
643 | |
644 | /* Merge the attributes. */ | |
645 | attributes = targetm.merge_type_attributes (t1, t2); | |
646 | ||
647 | /* Find the composite type of the target types, and combine the | |
46df2823 JM |
648 | qualifiers of the two types' targets. Do not lose qualifiers on |
649 | array element types by taking the TYPE_MAIN_VARIANT. */ | |
650 | mv1 = pointed_to_1 = TREE_TYPE (t1); | |
651 | mv2 = pointed_to_2 = TREE_TYPE (t2); | |
652 | if (TREE_CODE (mv1) != ARRAY_TYPE) | |
653 | mv1 = TYPE_MAIN_VARIANT (pointed_to_1); | |
654 | if (TREE_CODE (mv2) != ARRAY_TYPE) | |
655 | mv2 = TYPE_MAIN_VARIANT (pointed_to_2); | |
656 | target = composite_type (mv1, mv2); | |
eb1387a0 | 657 | |
768952be MU |
658 | /* Strip array types to get correct qualifier for pointers to arrays */ |
659 | quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1)); | |
660 | quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2)); | |
661 | ||
eb1387a0 RG |
662 | /* For function types do not merge const qualifiers, but drop them |
663 | if used inconsistently. The middle-end uses these to mark const | |
664 | and noreturn functions. */ | |
665 | if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) | |
36c5e70a | 666 | target_quals = (quals1 & quals2); |
eb1387a0 | 667 | else |
36c5e70a BE |
668 | target_quals = (quals1 | quals2); |
669 | ||
670 | /* If the two named address spaces are different, determine the common | |
671 | superset address space. This is guaranteed to exist due to the | |
672 | assumption that comp_target_type returned non-zero. */ | |
673 | as1 = TYPE_ADDR_SPACE (pointed_to_1); | |
674 | as2 = TYPE_ADDR_SPACE (pointed_to_2); | |
675 | if (!addr_space_superset (as1, as2, &as_common)) | |
676 | gcc_unreachable (); | |
677 | ||
678 | target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); | |
679 | ||
eb1387a0 | 680 | t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); |
10bc1b1b JM |
681 | return build_type_attribute_variant (t1, attributes); |
682 | } | |
683 | ||
684 | /* Return the common type for two arithmetic types under the usual | |
685 | arithmetic conversions. The default conversions have already been | |
686 | applied, and enumerated types converted to their compatible integer | |
687 | types. The resulting type is unqualified and has no attributes. | |
688 | ||
689 | This is the type for the result of most arithmetic operations | |
690 | if the operands have the given two types. */ | |
691 | ||
ccf7f880 JJ |
692 | static tree |
693 | c_common_type (tree t1, tree t2) | |
10bc1b1b JM |
694 | { |
695 | enum tree_code code1; | |
696 | enum tree_code code2; | |
697 | ||
698 | /* If one type is nonsense, use the other. */ | |
699 | if (t1 == error_mark_node) | |
700 | return t2; | |
701 | if (t2 == error_mark_node) | |
702 | return t1; | |
703 | ||
704 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
705 | t1 = TYPE_MAIN_VARIANT (t1); | |
706 | ||
707 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
708 | t2 = TYPE_MAIN_VARIANT (t2); | |
709 | ||
710 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
711 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
712 | ||
713 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
714 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
715 | ||
716 | /* Save time if the two types are the same. */ | |
717 | ||
718 | if (t1 == t2) return t1; | |
719 | ||
720 | code1 = TREE_CODE (t1); | |
721 | code2 = TREE_CODE (t2); | |
722 | ||
366de0ce | 723 | gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE |
ab22c1fa CF |
724 | || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE |
725 | || code1 == INTEGER_TYPE); | |
366de0ce | 726 | gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE |
ab22c1fa CF |
727 | || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE |
728 | || code2 == INTEGER_TYPE); | |
10bc1b1b | 729 | |
5fc89bfd JJ |
730 | /* When one operand is a decimal float type, the other operand cannot be |
731 | a generic float type or a complex type. We also disallow vector types | |
732 | here. */ | |
733 | if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) | |
734 | && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) | |
735 | { | |
736 | if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) | |
737 | { | |
738 | error ("can%'t mix operands of decimal float and vector types"); | |
739 | return error_mark_node; | |
740 | } | |
741 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
742 | { | |
743 | error ("can%'t mix operands of decimal float and complex types"); | |
744 | return error_mark_node; | |
745 | } | |
746 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
747 | { | |
748 | error ("can%'t mix operands of decimal float and other float types"); | |
749 | return error_mark_node; | |
750 | } | |
751 | } | |
752 | ||
10bc1b1b JM |
753 | /* If one type is a vector type, return that type. (How the usual |
754 | arithmetic conversions apply to the vector types extension is not | |
755 | precisely specified.) */ | |
756 | if (code1 == VECTOR_TYPE) | |
757 | return t1; | |
758 | ||
759 | if (code2 == VECTOR_TYPE) | |
760 | return t2; | |
761 | ||
762 | /* If one type is complex, form the common type of the non-complex | |
763 | components, then make that complex. Use T1 or T2 if it is the | |
764 | required type. */ | |
765 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
766 | { | |
767 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
768 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
ccf7f880 | 769 | tree subtype = c_common_type (subtype1, subtype2); |
10bc1b1b JM |
770 | |
771 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
772 | return t1; | |
773 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
774 | return t2; | |
775 | else | |
776 | return build_complex_type (subtype); | |
777 | } | |
778 | ||
779 | /* If only one is real, use it as the result. */ | |
780 | ||
781 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
782 | return t1; | |
783 | ||
784 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
785 | return t2; | |
786 | ||
9a8ce21f JG |
787 | /* If both are real and either are decimal floating point types, use |
788 | the decimal floating point type with the greater precision. */ | |
789 | ||
790 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
791 | { | |
792 | if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node | |
793 | || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) | |
794 | return dfloat128_type_node; | |
795 | else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node | |
796 | || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) | |
797 | return dfloat64_type_node; | |
798 | else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node | |
799 | || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) | |
800 | return dfloat32_type_node; | |
801 | } | |
802 | ||
ab22c1fa CF |
803 | /* Deal with fixed-point types. */ |
804 | if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) | |
805 | { | |
806 | unsigned int unsignedp = 0, satp = 0; | |
ef4bddc2 | 807 | machine_mode m1, m2; |
ab22c1fa CF |
808 | unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; |
809 | ||
810 | m1 = TYPE_MODE (t1); | |
811 | m2 = TYPE_MODE (t2); | |
812 | ||
813 | /* If one input type is saturating, the result type is saturating. */ | |
814 | if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) | |
815 | satp = 1; | |
816 | ||
817 | /* If both fixed-point types are unsigned, the result type is unsigned. | |
818 | When mixing fixed-point and integer types, follow the sign of the | |
819 | fixed-point type. | |
820 | Otherwise, the result type is signed. */ | |
821 | if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) | |
822 | && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) | |
823 | || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE | |
824 | && TYPE_UNSIGNED (t1)) | |
825 | || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE | |
826 | && TYPE_UNSIGNED (t2))) | |
827 | unsignedp = 1; | |
828 | ||
829 | /* The result type is signed. */ | |
830 | if (unsignedp == 0) | |
831 | { | |
832 | /* If the input type is unsigned, we need to convert to the | |
833 | signed type. */ | |
834 | if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) | |
835 | { | |
d75d71e0 | 836 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
837 | if (GET_MODE_CLASS (m1) == MODE_UFRACT) |
838 | mclass = MODE_FRACT; | |
839 | else if (GET_MODE_CLASS (m1) == MODE_UACCUM) | |
840 | mclass = MODE_ACCUM; | |
841 | else | |
842 | gcc_unreachable (); | |
843 | m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); | |
844 | } | |
845 | if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) | |
846 | { | |
d75d71e0 | 847 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
848 | if (GET_MODE_CLASS (m2) == MODE_UFRACT) |
849 | mclass = MODE_FRACT; | |
850 | else if (GET_MODE_CLASS (m2) == MODE_UACCUM) | |
851 | mclass = MODE_ACCUM; | |
852 | else | |
853 | gcc_unreachable (); | |
854 | m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); | |
855 | } | |
856 | } | |
857 | ||
858 | if (code1 == FIXED_POINT_TYPE) | |
859 | { | |
860 | fbit1 = GET_MODE_FBIT (m1); | |
861 | ibit1 = GET_MODE_IBIT (m1); | |
862 | } | |
863 | else | |
864 | { | |
865 | fbit1 = 0; | |
866 | /* Signed integers need to subtract one sign bit. */ | |
867 | ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); | |
868 | } | |
869 | ||
870 | if (code2 == FIXED_POINT_TYPE) | |
871 | { | |
872 | fbit2 = GET_MODE_FBIT (m2); | |
873 | ibit2 = GET_MODE_IBIT (m2); | |
874 | } | |
875 | else | |
876 | { | |
877 | fbit2 = 0; | |
878 | /* Signed integers need to subtract one sign bit. */ | |
879 | ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); | |
880 | } | |
881 | ||
882 | max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; | |
883 | max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; | |
884 | return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, | |
885 | satp); | |
886 | } | |
887 | ||
10bc1b1b JM |
888 | /* Both real or both integers; use the one with greater precision. */ |
889 | ||
890 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
891 | return t1; | |
892 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
893 | return t2; | |
894 | ||
895 | /* Same precision. Prefer long longs to longs to ints when the | |
896 | same precision, following the C99 rules on integer type rank | |
897 | (which are equivalent to the C90 rules for C90 types). */ | |
898 | ||
899 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
900 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
901 | return long_long_unsigned_type_node; | |
902 | ||
903 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
904 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
905 | { | |
906 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
907 | return long_long_unsigned_type_node; | |
908 | else | |
c22cacf3 | 909 | return long_long_integer_type_node; |
10bc1b1b JM |
910 | } |
911 | ||
912 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
913 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
914 | return long_unsigned_type_node; | |
915 | ||
916 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
917 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
918 | { | |
919 | /* But preserve unsignedness from the other type, | |
920 | since long cannot hold all the values of an unsigned int. */ | |
921 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
922 | return long_unsigned_type_node; | |
923 | else | |
924 | return long_integer_type_node; | |
925 | } | |
926 | ||
927 | /* Likewise, prefer long double to double even if same size. */ | |
928 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
929 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
930 | return long_double_type_node; | |
931 | ||
2531a1d9 JR |
932 | /* Likewise, prefer double to float even if same size. |
933 | We got a couple of embedded targets with 32 bit doubles, and the | |
934 | pdp11 might have 64 bit floats. */ | |
935 | if (TYPE_MAIN_VARIANT (t1) == double_type_node | |
936 | || TYPE_MAIN_VARIANT (t2) == double_type_node) | |
937 | return double_type_node; | |
938 | ||
10bc1b1b JM |
939 | /* Otherwise prefer the unsigned one. */ |
940 | ||
941 | if (TYPE_UNSIGNED (t1)) | |
942 | return t1; | |
943 | else | |
944 | return t2; | |
945 | } | |
400fbf9f | 946 | \f |
5922c215 JM |
947 | /* Wrapper around c_common_type that is used by c-common.c and other |
948 | front end optimizations that remove promotions. ENUMERAL_TYPEs | |
b2232745 RS |
949 | are allowed here and are converted to their compatible integer types. |
950 | BOOLEAN_TYPEs are allowed here and return either boolean_type_node or | |
951 | preferably a non-Boolean type as the common type. */ | |
ccf7f880 JJ |
952 | tree |
953 | common_type (tree t1, tree t2) | |
954 | { | |
955 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
956 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); | |
957 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
958 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); | |
b2232745 RS |
959 | |
960 | /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ | |
961 | if (TREE_CODE (t1) == BOOLEAN_TYPE | |
962 | && TREE_CODE (t2) == BOOLEAN_TYPE) | |
963 | return boolean_type_node; | |
964 | ||
965 | /* If either type is BOOLEAN_TYPE, then return the other. */ | |
966 | if (TREE_CODE (t1) == BOOLEAN_TYPE) | |
967 | return t2; | |
968 | if (TREE_CODE (t2) == BOOLEAN_TYPE) | |
969 | return t1; | |
970 | ||
ccf7f880 JJ |
971 | return c_common_type (t1, t2); |
972 | } | |
f13c9b2c | 973 | |
400fbf9f JW |
974 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
975 | or various other operations. Return 2 if they are compatible | |
976 | but a warning may be needed if you use them together. */ | |
977 | ||
978 | int | |
132da1a5 | 979 | comptypes (tree type1, tree type2) |
f13c9b2c AP |
980 | { |
981 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
982 | int val; | |
983 | ||
dc5027f4 | 984 | val = comptypes_internal (type1, type2, NULL, NULL); |
744aa42f ILT |
985 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
986 | ||
987 | return val; | |
988 | } | |
989 | ||
990 | /* Like comptypes, but if it returns non-zero because enum and int are | |
991 | compatible, it sets *ENUM_AND_INT_P to true. */ | |
992 | ||
993 | static int | |
994 | comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) | |
995 | { | |
996 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
997 | int val; | |
998 | ||
dc5027f4 JM |
999 | val = comptypes_internal (type1, type2, enum_and_int_p, NULL); |
1000 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
1001 | ||
1002 | return val; | |
1003 | } | |
1004 | ||
1005 | /* Like comptypes, but if it returns nonzero for different types, it | |
1006 | sets *DIFFERENT_TYPES_P to true. */ | |
1007 | ||
1008 | int | |
1009 | comptypes_check_different_types (tree type1, tree type2, | |
1010 | bool *different_types_p) | |
1011 | { | |
1012 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1013 | int val; | |
1014 | ||
1015 | val = comptypes_internal (type1, type2, NULL, different_types_p); | |
f13c9b2c | 1016 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
c22cacf3 | 1017 | |
f13c9b2c | 1018 | return val; |
c22cacf3 MS |
1019 | } |
1020 | \f | |
f13c9b2c AP |
1021 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
1022 | or various other operations. Return 2 if they are compatible | |
744aa42f ILT |
1023 | but a warning may be needed if you use them together. If |
1024 | ENUM_AND_INT_P is not NULL, and one type is an enum and the other a | |
1025 | compatible integer type, then this sets *ENUM_AND_INT_P to true; | |
dc5027f4 JM |
1026 | *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not |
1027 | NULL, and the types are compatible but different enough not to be | |
48b0b196 | 1028 | permitted in C11 typedef redeclarations, then this sets |
dc5027f4 JM |
1029 | *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to |
1030 | false, but may or may not be set if the types are incompatible. | |
1031 | This differs from comptypes, in that we don't free the seen | |
1032 | types. */ | |
f13c9b2c AP |
1033 | |
1034 | static int | |
dc5027f4 JM |
1035 | comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, |
1036 | bool *different_types_p) | |
400fbf9f | 1037 | { |
58f9752a KG |
1038 | const_tree t1 = type1; |
1039 | const_tree t2 = type2; | |
4b027d16 | 1040 | int attrval, val; |
400fbf9f JW |
1041 | |
1042 | /* Suppress errors caused by previously reported errors. */ | |
1043 | ||
8d47dfc5 RH |
1044 | if (t1 == t2 || !t1 || !t2 |
1045 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
400fbf9f JW |
1046 | return 1; |
1047 | ||
bca63328 JM |
1048 | /* Enumerated types are compatible with integer types, but this is |
1049 | not transitive: two enumerated types in the same translation unit | |
1050 | are compatible with each other only if they are the same type. */ | |
400fbf9f | 1051 | |
bca63328 | 1052 | if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) |
744aa42f ILT |
1053 | { |
1054 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); | |
dc5027f4 JM |
1055 | if (TREE_CODE (t2) != VOID_TYPE) |
1056 | { | |
1057 | if (enum_and_int_p != NULL) | |
1058 | *enum_and_int_p = true; | |
1059 | if (different_types_p != NULL) | |
1060 | *different_types_p = true; | |
1061 | } | |
744aa42f | 1062 | } |
bca63328 | 1063 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) |
744aa42f ILT |
1064 | { |
1065 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); | |
dc5027f4 JM |
1066 | if (TREE_CODE (t1) != VOID_TYPE) |
1067 | { | |
1068 | if (enum_and_int_p != NULL) | |
1069 | *enum_and_int_p = true; | |
1070 | if (different_types_p != NULL) | |
1071 | *different_types_p = true; | |
1072 | } | |
744aa42f | 1073 | } |
400fbf9f JW |
1074 | |
1075 | if (t1 == t2) | |
1076 | return 1; | |
1077 | ||
1078 | /* Different classes of types can't be compatible. */ | |
1079 | ||
3aeb3655 EC |
1080 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
1081 | return 0; | |
400fbf9f | 1082 | |
118a3a8b | 1083 | /* Qualifiers must match. C99 6.7.3p9 */ |
400fbf9f | 1084 | |
3932261a | 1085 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
400fbf9f JW |
1086 | return 0; |
1087 | ||
08632da2 RS |
1088 | /* Allow for two different type nodes which have essentially the same |
1089 | definition. Note that we already checked for equality of the type | |
38e01259 | 1090 | qualifiers (just above). */ |
400fbf9f | 1091 | |
46df2823 JM |
1092 | if (TREE_CODE (t1) != ARRAY_TYPE |
1093 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
400fbf9f JW |
1094 | return 1; |
1095 | ||
4b027d16 | 1096 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ |
ac9a30ae | 1097 | if (!(attrval = comp_type_attributes (t1, t2))) |
4b027d16 RK |
1098 | return 0; |
1099 | ||
1100 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1101 | val = 0; | |
1102 | ||
400fbf9f JW |
1103 | switch (TREE_CODE (t1)) |
1104 | { | |
1105 | case POINTER_TYPE: | |
106f5de5 UW |
1106 | /* Do not remove mode or aliasing information. */ |
1107 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1108 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) | |
1109 | break; | |
4b027d16 | 1110 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
744aa42f | 1111 | ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 | 1112 | enum_and_int_p, different_types_p)); |
4b027d16 | 1113 | break; |
400fbf9f JW |
1114 | |
1115 | case FUNCTION_TYPE: | |
dc5027f4 JM |
1116 | val = function_types_compatible_p (t1, t2, enum_and_int_p, |
1117 | different_types_p); | |
4b027d16 | 1118 | break; |
400fbf9f JW |
1119 | |
1120 | case ARRAY_TYPE: | |
1121 | { | |
400fbf9f JW |
1122 | tree d1 = TYPE_DOMAIN (t1); |
1123 | tree d2 = TYPE_DOMAIN (t2); | |
3f85558f RH |
1124 | bool d1_variable, d2_variable; |
1125 | bool d1_zero, d2_zero; | |
4b027d16 | 1126 | val = 1; |
400fbf9f JW |
1127 | |
1128 | /* Target types must match incl. qualifiers. */ | |
1129 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
744aa42f | 1130 | && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 JM |
1131 | enum_and_int_p, |
1132 | different_types_p))) | |
400fbf9f JW |
1133 | return 0; |
1134 | ||
dc5027f4 JM |
1135 | if (different_types_p != NULL |
1136 | && (d1 == 0) != (d2 == 0)) | |
1137 | *different_types_p = true; | |
400fbf9f | 1138 | /* Sizes must match unless one is missing or variable. */ |
3f85558f | 1139 | if (d1 == 0 || d2 == 0 || d1 == d2) |
4b027d16 | 1140 | break; |
400fbf9f | 1141 | |
3f75a254 JM |
1142 | d1_zero = !TYPE_MAX_VALUE (d1); |
1143 | d2_zero = !TYPE_MAX_VALUE (d2); | |
3f85558f | 1144 | |
3f75a254 | 1145 | d1_variable = (!d1_zero |
3f85558f RH |
1146 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST |
1147 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
3f75a254 | 1148 | d2_variable = (!d2_zero |
3f85558f RH |
1149 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST |
1150 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
1151 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
1152 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
3f85558f | 1153 | |
dc5027f4 JM |
1154 | if (different_types_p != NULL |
1155 | && d1_variable != d2_variable) | |
1156 | *different_types_p = true; | |
3f85558f RH |
1157 | if (d1_variable || d2_variable) |
1158 | break; | |
1159 | if (d1_zero && d2_zero) | |
1160 | break; | |
1161 | if (d1_zero || d2_zero | |
3f75a254 JM |
1162 | || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) |
1163 | || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) | |
05bccae2 RK |
1164 | val = 0; |
1165 | ||
c22cacf3 | 1166 | break; |
400fbf9f JW |
1167 | } |
1168 | ||
d1bd0ded | 1169 | case ENUMERAL_TYPE: |
58393038 | 1170 | case RECORD_TYPE: |
d1bd0ded | 1171 | case UNION_TYPE: |
766beae1 | 1172 | if (val != 1 && !same_translation_unit_p (t1, t2)) |
c22cacf3 | 1173 | { |
fcb99e7b JJ |
1174 | tree a1 = TYPE_ATTRIBUTES (t1); |
1175 | tree a2 = TYPE_ATTRIBUTES (t2); | |
1176 | ||
1177 | if (! attribute_list_contained (a1, a2) | |
1178 | && ! attribute_list_contained (a2, a1)) | |
1179 | break; | |
1180 | ||
f13c9b2c | 1181 | if (attrval != 2) |
dc5027f4 JM |
1182 | return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, |
1183 | different_types_p); | |
1184 | val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, | |
1185 | different_types_p); | |
f13c9b2c | 1186 | } |
4b027d16 | 1187 | break; |
e9a25f70 | 1188 | |
62e1dfcf | 1189 | case VECTOR_TYPE: |
744aa42f ILT |
1190 | val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) |
1191 | && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
dc5027f4 | 1192 | enum_and_int_p, different_types_p)); |
62e1dfcf NC |
1193 | break; |
1194 | ||
e9a25f70 JL |
1195 | default: |
1196 | break; | |
400fbf9f | 1197 | } |
4b027d16 | 1198 | return attrval == 2 && val == 1 ? 2 : val; |
400fbf9f JW |
1199 | } |
1200 | ||
36c5e70a BE |
1201 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring |
1202 | their qualifiers, except for named address spaces. If the pointers point to | |
1203 | different named addresses, then we must determine if one address space is a | |
1204 | subset of the other. */ | |
400fbf9f JW |
1205 | |
1206 | static int | |
744aa42f | 1207 | comp_target_types (location_t location, tree ttl, tree ttr) |
400fbf9f | 1208 | { |
392202b0 | 1209 | int val; |
768952be | 1210 | int val_ped; |
36c5e70a BE |
1211 | tree mvl = TREE_TYPE (ttl); |
1212 | tree mvr = TREE_TYPE (ttr); | |
1213 | addr_space_t asl = TYPE_ADDR_SPACE (mvl); | |
1214 | addr_space_t asr = TYPE_ADDR_SPACE (mvr); | |
1215 | addr_space_t as_common; | |
744aa42f | 1216 | bool enum_and_int_p; |
8b40563c | 1217 | |
36c5e70a BE |
1218 | /* Fail if pointers point to incompatible address spaces. */ |
1219 | if (!addr_space_superset (asl, asr, &as_common)) | |
1220 | return 0; | |
1221 | ||
768952be MU |
1222 | /* For pedantic record result of comptypes on arrays before losing |
1223 | qualifiers on the element type below. */ | |
1224 | val_ped = 1; | |
1225 | ||
1226 | if (TREE_CODE (mvl) == ARRAY_TYPE | |
1227 | && TREE_CODE (mvr) == ARRAY_TYPE) | |
1228 | val_ped = comptypes (mvl, mvr); | |
1229 | ||
1230 | /* Qualifiers on element types of array types that are | |
1231 | pointer targets are lost by taking their TYPE_MAIN_VARIANT. */ | |
1232 | ||
1233 | mvl = (TYPE_ATOMIC (strip_array_types (mvl)) | |
1234 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC) | |
1235 | : TYPE_MAIN_VARIANT (mvl)); | |
1236 | ||
1237 | mvr = (TYPE_ATOMIC (strip_array_types (mvr)) | |
1238 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC) | |
1239 | : TYPE_MAIN_VARIANT (mvr)); | |
1240 | ||
744aa42f ILT |
1241 | enum_and_int_p = false; |
1242 | val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); | |
8b40563c | 1243 | |
768952be MU |
1244 | if (val == 1 && val_ped != 1) |
1245 | pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers " | |
1246 | "are incompatible in ISO C"); | |
1247 | ||
fcf73884 | 1248 | if (val == 2) |
c1771a20 | 1249 | pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); |
744aa42f ILT |
1250 | |
1251 | if (val == 1 && enum_and_int_p && warn_cxx_compat) | |
1252 | warning_at (location, OPT_Wc___compat, | |
1253 | "pointer target types incompatible in C++"); | |
1254 | ||
400fbf9f JW |
1255 | return val; |
1256 | } | |
1257 | \f | |
1258 | /* Subroutines of `comptypes'. */ | |
1259 | ||
f75fbaf7 ZW |
1260 | /* Determine whether two trees derive from the same translation unit. |
1261 | If the CONTEXT chain ends in a null, that tree's context is still | |
1262 | being parsed, so if two trees have context chains ending in null, | |
766beae1 | 1263 | they're in the same translation unit. */ |
f75fbaf7 | 1264 | int |
58f9752a | 1265 | same_translation_unit_p (const_tree t1, const_tree t2) |
766beae1 ZW |
1266 | { |
1267 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
1268 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
1269 | { | |
6615c446 JO |
1270 | case tcc_declaration: |
1271 | t1 = DECL_CONTEXT (t1); break; | |
1272 | case tcc_type: | |
1273 | t1 = TYPE_CONTEXT (t1); break; | |
1274 | case tcc_exceptional: | |
1275 | t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ | |
366de0ce | 1276 | default: gcc_unreachable (); |
766beae1 ZW |
1277 | } |
1278 | ||
1279 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
1280 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
1281 | { | |
6615c446 JO |
1282 | case tcc_declaration: |
1283 | t2 = DECL_CONTEXT (t2); break; | |
1284 | case tcc_type: | |
1285 | t2 = TYPE_CONTEXT (t2); break; | |
1286 | case tcc_exceptional: | |
1287 | t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ | |
366de0ce | 1288 | default: gcc_unreachable (); |
766beae1 ZW |
1289 | } |
1290 | ||
1291 | return t1 == t2; | |
1292 | } | |
1293 | ||
f13c9b2c | 1294 | /* Allocate the seen two types, assuming that they are compatible. */ |
d1bd0ded | 1295 | |
f13c9b2c | 1296 | static struct tagged_tu_seen_cache * |
58f9752a | 1297 | alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) |
f13c9b2c | 1298 | { |
cceb1885 | 1299 | struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); |
f13c9b2c AP |
1300 | tu->next = tagged_tu_seen_base; |
1301 | tu->t1 = t1; | |
1302 | tu->t2 = t2; | |
c22cacf3 | 1303 | |
f13c9b2c | 1304 | tagged_tu_seen_base = tu; |
c22cacf3 | 1305 | |
f13c9b2c AP |
1306 | /* The C standard says that two structures in different translation |
1307 | units are compatible with each other only if the types of their | |
1308 | fields are compatible (among other things). We assume that they | |
1309 | are compatible until proven otherwise when building the cache. | |
1310 | An example where this can occur is: | |
1311 | struct a | |
1312 | { | |
1313 | struct a *next; | |
1314 | }; | |
1315 | If we are comparing this against a similar struct in another TU, | |
c83eecad | 1316 | and did not assume they were compatible, we end up with an infinite |
f13c9b2c AP |
1317 | loop. */ |
1318 | tu->val = 1; | |
1319 | return tu; | |
1320 | } | |
d1bd0ded | 1321 | |
f13c9b2c | 1322 | /* Free the seen types until we get to TU_TIL. */ |
d1bd0ded | 1323 | |
f13c9b2c AP |
1324 | static void |
1325 | free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) | |
1326 | { | |
1327 | const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; | |
1328 | while (tu != tu_til) | |
1329 | { | |
741ac903 KG |
1330 | const struct tagged_tu_seen_cache *const tu1 |
1331 | = (const struct tagged_tu_seen_cache *) tu; | |
f13c9b2c | 1332 | tu = tu1->next; |
b1d5455a | 1333 | free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); |
f13c9b2c AP |
1334 | } |
1335 | tagged_tu_seen_base = tu_til; | |
1336 | } | |
d1bd0ded GK |
1337 | |
1338 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
1339 | compatible. If the two types are not the same (which has been | |
1340 | checked earlier), this can only happen when multiple translation | |
1341 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
dc5027f4 JM |
1342 | rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1343 | comptypes_internal. */ | |
d1bd0ded GK |
1344 | |
1345 | static int | |
744aa42f | 1346 | tagged_types_tu_compatible_p (const_tree t1, const_tree t2, |
dc5027f4 | 1347 | bool *enum_and_int_p, bool *different_types_p) |
d1bd0ded GK |
1348 | { |
1349 | tree s1, s2; | |
1350 | bool needs_warning = false; | |
3aeb3655 | 1351 | |
d1bd0ded GK |
1352 | /* We have to verify that the tags of the types are the same. This |
1353 | is harder than it looks because this may be a typedef, so we have | |
1354 | to go look at the original type. It may even be a typedef of a | |
6de9cd9a DN |
1355 | typedef... |
1356 | In the case of compiler-created builtin structs the TYPE_DECL | |
1357 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
dea984dc ILT |
1358 | while (TYPE_NAME (t1) |
1359 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
1360 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
d1bd0ded GK |
1361 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); |
1362 | ||
dea984dc ILT |
1363 | while (TYPE_NAME (t2) |
1364 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
1365 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
d1bd0ded GK |
1366 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); |
1367 | ||
1368 | /* C90 didn't have the requirement that the two tags be the same. */ | |
1369 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
1370 | return 0; | |
3aeb3655 | 1371 | |
d1bd0ded GK |
1372 | /* C90 didn't say what happened if one or both of the types were |
1373 | incomplete; we choose to follow C99 rules here, which is that they | |
1374 | are compatible. */ | |
1375 | if (TYPE_SIZE (t1) == NULL | |
1376 | || TYPE_SIZE (t2) == NULL) | |
1377 | return 1; | |
3aeb3655 | 1378 | |
d1bd0ded | 1379 | { |
f13c9b2c | 1380 | const struct tagged_tu_seen_cache * tts_i; |
d1bd0ded GK |
1381 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) |
1382 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
f13c9b2c | 1383 | return tts_i->val; |
d1bd0ded | 1384 | } |
3aeb3655 | 1385 | |
d1bd0ded GK |
1386 | switch (TREE_CODE (t1)) |
1387 | { | |
1388 | case ENUMERAL_TYPE: | |
1389 | { | |
f13c9b2c | 1390 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
c22cacf3 MS |
1391 | /* Speed up the case where the type values are in the same order. */ |
1392 | tree tv1 = TYPE_VALUES (t1); | |
1393 | tree tv2 = TYPE_VALUES (t2); | |
3aeb3655 | 1394 | |
c22cacf3 | 1395 | if (tv1 == tv2) |
f13c9b2c AP |
1396 | { |
1397 | return 1; | |
1398 | } | |
3aeb3655 | 1399 | |
c22cacf3 MS |
1400 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) |
1401 | { | |
1402 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) | |
1403 | break; | |
1404 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
f13c9b2c | 1405 | { |
c22cacf3 | 1406 | tu->val = 0; |
f13c9b2c AP |
1407 | return 0; |
1408 | } | |
c22cacf3 | 1409 | } |
3aeb3655 | 1410 | |
c22cacf3 | 1411 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) |
f13c9b2c AP |
1412 | { |
1413 | return 1; | |
1414 | } | |
c22cacf3 | 1415 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) |
f13c9b2c AP |
1416 | { |
1417 | tu->val = 0; | |
1418 | return 0; | |
1419 | } | |
3aeb3655 | 1420 | |
d1bd0ded | 1421 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) |
f13c9b2c AP |
1422 | { |
1423 | tu->val = 0; | |
1424 | return 0; | |
1425 | } | |
3aeb3655 | 1426 | |
d1bd0ded GK |
1427 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) |
1428 | { | |
1429 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
1430 | if (s2 == NULL | |
1431 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
f13c9b2c AP |
1432 | { |
1433 | tu->val = 0; | |
1434 | return 0; | |
1435 | } | |
d1bd0ded GK |
1436 | } |
1437 | return 1; | |
1438 | } | |
1439 | ||
1440 | case UNION_TYPE: | |
1441 | { | |
f13c9b2c | 1442 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
d1bd0ded | 1443 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) |
f13c9b2c AP |
1444 | { |
1445 | tu->val = 0; | |
1446 | return 0; | |
1447 | } | |
c22cacf3 | 1448 | |
f13c9b2c AP |
1449 | /* Speed up the common case where the fields are in the same order. */ |
1450 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; | |
910ad8de | 1451 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
f13c9b2c AP |
1452 | { |
1453 | int result; | |
c22cacf3 | 1454 | |
3ae4d3cc | 1455 | if (DECL_NAME (s1) != DECL_NAME (s2)) |
f13c9b2c | 1456 | break; |
744aa42f | 1457 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1458 | enum_and_int_p, different_types_p); |
3ae4d3cc AO |
1459 | |
1460 | if (result != 1 && !DECL_NAME (s1)) | |
1461 | break; | |
f13c9b2c AP |
1462 | if (result == 0) |
1463 | { | |
1464 | tu->val = 0; | |
1465 | return 0; | |
1466 | } | |
1467 | if (result == 2) | |
1468 | needs_warning = true; | |
1469 | ||
1470 | if (TREE_CODE (s1) == FIELD_DECL | |
1471 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1472 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1473 | { | |
1474 | tu->val = 0; | |
1475 | return 0; | |
1476 | } | |
1477 | } | |
1478 | if (!s1 && !s2) | |
1479 | { | |
1480 | tu->val = needs_warning ? 2 : 1; | |
1481 | return tu->val; | |
1482 | } | |
d1bd0ded | 1483 | |
910ad8de | 1484 | for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1)) |
d1bd0ded GK |
1485 | { |
1486 | bool ok = false; | |
3aeb3655 | 1487 | |
910ad8de | 1488 | for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2)) |
3ae4d3cc AO |
1489 | if (DECL_NAME (s1) == DECL_NAME (s2)) |
1490 | { | |
1491 | int result; | |
1492 | ||
744aa42f | 1493 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 JM |
1494 | enum_and_int_p, |
1495 | different_types_p); | |
3ae4d3cc AO |
1496 | |
1497 | if (result != 1 && !DECL_NAME (s1)) | |
1498 | continue; | |
1499 | if (result == 0) | |
1500 | { | |
1501 | tu->val = 0; | |
1502 | return 0; | |
1503 | } | |
1504 | if (result == 2) | |
1505 | needs_warning = true; | |
1506 | ||
1507 | if (TREE_CODE (s1) == FIELD_DECL | |
1508 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1509 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
d1bd0ded | 1510 | break; |
3ae4d3cc AO |
1511 | |
1512 | ok = true; | |
1513 | break; | |
1514 | } | |
3f75a254 | 1515 | if (!ok) |
f13c9b2c AP |
1516 | { |
1517 | tu->val = 0; | |
1518 | return 0; | |
1519 | } | |
d1bd0ded | 1520 | } |
f13c9b2c AP |
1521 | tu->val = needs_warning ? 2 : 10; |
1522 | return tu->val; | |
d1bd0ded GK |
1523 | } |
1524 | ||
1525 | case RECORD_TYPE: | |
1526 | { | |
c22cacf3 | 1527 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
3aeb3655 EC |
1528 | |
1529 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
d1bd0ded | 1530 | s1 && s2; |
910ad8de | 1531 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
d1bd0ded GK |
1532 | { |
1533 | int result; | |
1534 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
1535 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
1536 | break; | |
744aa42f | 1537 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1538 | enum_and_int_p, different_types_p); |
d1bd0ded GK |
1539 | if (result == 0) |
1540 | break; | |
1541 | if (result == 2) | |
1542 | needs_warning = true; | |
3aeb3655 | 1543 | |
d1bd0ded GK |
1544 | if (TREE_CODE (s1) == FIELD_DECL |
1545 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1546 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1547 | break; | |
1548 | } | |
d1bd0ded | 1549 | if (s1 && s2) |
f13c9b2c AP |
1550 | tu->val = 0; |
1551 | else | |
1552 | tu->val = needs_warning ? 2 : 1; | |
1553 | return tu->val; | |
d1bd0ded GK |
1554 | } |
1555 | ||
1556 | default: | |
366de0ce | 1557 | gcc_unreachable (); |
d1bd0ded GK |
1558 | } |
1559 | } | |
1560 | ||
400fbf9f JW |
1561 | /* Return 1 if two function types F1 and F2 are compatible. |
1562 | If either type specifies no argument types, | |
1563 | the other must specify a fixed number of self-promoting arg types. | |
2f6e4e97 | 1564 | Otherwise, if one type specifies only the number of arguments, |
400fbf9f | 1565 | the other must specify that number of self-promoting arg types. |
744aa42f | 1566 | Otherwise, the argument types must match. |
dc5027f4 | 1567 | ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */ |
400fbf9f JW |
1568 | |
1569 | static int | |
744aa42f | 1570 | function_types_compatible_p (const_tree f1, const_tree f2, |
dc5027f4 | 1571 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1572 | { |
1573 | tree args1, args2; | |
1574 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1575 | int val = 1; | |
1576 | int val1; | |
a6fdc086 GK |
1577 | tree ret1, ret2; |
1578 | ||
1579 | ret1 = TREE_TYPE (f1); | |
1580 | ret2 = TREE_TYPE (f2); | |
1581 | ||
e508a019 JM |
1582 | /* 'volatile' qualifiers on a function's return type used to mean |
1583 | the function is noreturn. */ | |
1584 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
509c9d60 | 1585 | pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); |
a6fdc086 GK |
1586 | if (TYPE_VOLATILE (ret1)) |
1587 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
1588 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
1589 | if (TYPE_VOLATILE (ret2)) | |
1590 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
1591 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
dc5027f4 | 1592 | val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p); |
a6fdc086 | 1593 | if (val == 0) |
400fbf9f JW |
1594 | return 0; |
1595 | ||
1596 | args1 = TYPE_ARG_TYPES (f1); | |
1597 | args2 = TYPE_ARG_TYPES (f2); | |
1598 | ||
dc5027f4 JM |
1599 | if (different_types_p != NULL |
1600 | && (args1 == 0) != (args2 == 0)) | |
1601 | *different_types_p = true; | |
1602 | ||
400fbf9f JW |
1603 | /* An unspecified parmlist matches any specified parmlist |
1604 | whose argument types don't need default promotions. */ | |
1605 | ||
1606 | if (args1 == 0) | |
1607 | { | |
1608 | if (!self_promoting_args_p (args2)) | |
1609 | return 0; | |
1610 | /* If one of these types comes from a non-prototype fn definition, | |
1611 | compare that with the other type's arglist. | |
3176a0c2 | 1612 | If they don't match, ask for a warning (but no error). */ |
400fbf9f | 1613 | if (TYPE_ACTUAL_ARG_TYPES (f1) |
744aa42f | 1614 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), |
dc5027f4 | 1615 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1616 | val = 2; |
1617 | return val; | |
1618 | } | |
1619 | if (args2 == 0) | |
1620 | { | |
1621 | if (!self_promoting_args_p (args1)) | |
1622 | return 0; | |
1623 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
744aa42f | 1624 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), |
dc5027f4 | 1625 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1626 | val = 2; |
1627 | return val; | |
1628 | } | |
1629 | ||
1630 | /* Both types have argument lists: compare them and propagate results. */ | |
dc5027f4 JM |
1631 | val1 = type_lists_compatible_p (args1, args2, enum_and_int_p, |
1632 | different_types_p); | |
400fbf9f JW |
1633 | return val1 != 1 ? val1 : val; |
1634 | } | |
1635 | ||
744aa42f ILT |
1636 | /* Check two lists of types for compatibility, returning 0 for |
1637 | incompatible, 1 for compatible, or 2 for compatible with | |
dc5027f4 JM |
1638 | warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1639 | comptypes_internal. */ | |
400fbf9f JW |
1640 | |
1641 | static int | |
744aa42f | 1642 | type_lists_compatible_p (const_tree args1, const_tree args2, |
dc5027f4 | 1643 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1644 | { |
1645 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1646 | int val = 1; | |
9d5f3e49 | 1647 | int newval = 0; |
400fbf9f JW |
1648 | |
1649 | while (1) | |
1650 | { | |
46df2823 | 1651 | tree a1, mv1, a2, mv2; |
400fbf9f JW |
1652 | if (args1 == 0 && args2 == 0) |
1653 | return val; | |
1654 | /* If one list is shorter than the other, | |
1655 | they fail to match. */ | |
1656 | if (args1 == 0 || args2 == 0) | |
1657 | return 0; | |
46df2823 JM |
1658 | mv1 = a1 = TREE_VALUE (args1); |
1659 | mv2 = a2 = TREE_VALUE (args2); | |
1660 | if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) | |
267bac10 JM |
1661 | mv1 = (TYPE_ATOMIC (mv1) |
1662 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1), | |
1663 | TYPE_QUAL_ATOMIC) | |
1664 | : TYPE_MAIN_VARIANT (mv1)); | |
46df2823 | 1665 | if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) |
267bac10 JM |
1666 | mv2 = (TYPE_ATOMIC (mv2) |
1667 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2), | |
1668 | TYPE_QUAL_ATOMIC) | |
1669 | : TYPE_MAIN_VARIANT (mv2)); | |
400fbf9f JW |
1670 | /* A null pointer instead of a type |
1671 | means there is supposed to be an argument | |
1672 | but nothing is specified about what type it has. | |
1673 | So match anything that self-promotes. */ | |
dc5027f4 JM |
1674 | if (different_types_p != NULL |
1675 | && (a1 == 0) != (a2 == 0)) | |
1676 | *different_types_p = true; | |
46df2823 | 1677 | if (a1 == 0) |
400fbf9f | 1678 | { |
46df2823 | 1679 | if (c_type_promotes_to (a2) != a2) |
400fbf9f JW |
1680 | return 0; |
1681 | } | |
46df2823 | 1682 | else if (a2 == 0) |
400fbf9f | 1683 | { |
46df2823 | 1684 | if (c_type_promotes_to (a1) != a1) |
400fbf9f JW |
1685 | return 0; |
1686 | } | |
8f5b6d29 | 1687 | /* If one of the lists has an error marker, ignore this arg. */ |
46df2823 JM |
1688 | else if (TREE_CODE (a1) == ERROR_MARK |
1689 | || TREE_CODE (a2) == ERROR_MARK) | |
8f5b6d29 | 1690 | ; |
dc5027f4 JM |
1691 | else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p, |
1692 | different_types_p))) | |
400fbf9f | 1693 | { |
dc5027f4 JM |
1694 | if (different_types_p != NULL) |
1695 | *different_types_p = true; | |
400fbf9f JW |
1696 | /* Allow wait (union {union wait *u; int *i} *) |
1697 | and wait (union wait *) to be compatible. */ | |
46df2823 JM |
1698 | if (TREE_CODE (a1) == UNION_TYPE |
1699 | && (TYPE_NAME (a1) == 0 | |
ebf0bf7f | 1700 | || TYPE_TRANSPARENT_AGGR (a1)) |
46df2823 JM |
1701 | && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST |
1702 | && tree_int_cst_equal (TYPE_SIZE (a1), | |
1703 | TYPE_SIZE (a2))) | |
400fbf9f JW |
1704 | { |
1705 | tree memb; | |
46df2823 | 1706 | for (memb = TYPE_FIELDS (a1); |
910ad8de | 1707 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1708 | { |
1709 | tree mv3 = TREE_TYPE (memb); | |
1710 | if (mv3 && mv3 != error_mark_node | |
1711 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
267bac10 JM |
1712 | mv3 = (TYPE_ATOMIC (mv3) |
1713 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), | |
1714 | TYPE_QUAL_ATOMIC) | |
1715 | : TYPE_MAIN_VARIANT (mv3)); | |
dc5027f4 JM |
1716 | if (comptypes_internal (mv3, mv2, enum_and_int_p, |
1717 | different_types_p)) | |
58cb41e6 JJ |
1718 | break; |
1719 | } | |
400fbf9f JW |
1720 | if (memb == 0) |
1721 | return 0; | |
1722 | } | |
46df2823 JM |
1723 | else if (TREE_CODE (a2) == UNION_TYPE |
1724 | && (TYPE_NAME (a2) == 0 | |
ebf0bf7f | 1725 | || TYPE_TRANSPARENT_AGGR (a2)) |
46df2823 JM |
1726 | && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST |
1727 | && tree_int_cst_equal (TYPE_SIZE (a2), | |
1728 | TYPE_SIZE (a1))) | |
400fbf9f JW |
1729 | { |
1730 | tree memb; | |
46df2823 | 1731 | for (memb = TYPE_FIELDS (a2); |
910ad8de | 1732 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1733 | { |
1734 | tree mv3 = TREE_TYPE (memb); | |
1735 | if (mv3 && mv3 != error_mark_node | |
1736 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
267bac10 JM |
1737 | mv3 = (TYPE_ATOMIC (mv3) |
1738 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), | |
1739 | TYPE_QUAL_ATOMIC) | |
1740 | : TYPE_MAIN_VARIANT (mv3)); | |
dc5027f4 JM |
1741 | if (comptypes_internal (mv3, mv1, enum_and_int_p, |
1742 | different_types_p)) | |
58cb41e6 JJ |
1743 | break; |
1744 | } | |
400fbf9f JW |
1745 | if (memb == 0) |
1746 | return 0; | |
1747 | } | |
1748 | else | |
1749 | return 0; | |
1750 | } | |
1751 | ||
1752 | /* comptypes said ok, but record if it said to warn. */ | |
1753 | if (newval > val) | |
1754 | val = newval; | |
1755 | ||
1756 | args1 = TREE_CHAIN (args1); | |
1757 | args2 = TREE_CHAIN (args2); | |
1758 | } | |
1759 | } | |
400fbf9f | 1760 | \f |
a0e24419 MP |
1761 | /* Compute the size to increment a pointer by. When a function type or void |
1762 | type or incomplete type is passed, size_one_node is returned. | |
1763 | This function does not emit any diagnostics; the caller is responsible | |
1764 | for that. */ | |
400fbf9f | 1765 | |
4e2fb7de | 1766 | static tree |
58f9752a | 1767 | c_size_in_bytes (const_tree type) |
400fbf9f JW |
1768 | { |
1769 | enum tree_code code = TREE_CODE (type); | |
1770 | ||
a0e24419 MP |
1771 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK |
1772 | || !COMPLETE_TYPE_P (type)) | |
fed3cef0 RK |
1773 | return size_one_node; |
1774 | ||
400fbf9f | 1775 | /* Convert in case a char is more than one unit. */ |
db3927fb AH |
1776 | return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
1777 | size_int (TYPE_PRECISION (char_type_node) | |
1778 | / BITS_PER_UNIT)); | |
400fbf9f | 1779 | } |
400fbf9f | 1780 | \f |
400fbf9f JW |
1781 | /* Return either DECL or its known constant value (if it has one). */ |
1782 | ||
56cb9733 | 1783 | tree |
2f6e4e97 | 1784 | decl_constant_value (tree decl) |
400fbf9f | 1785 | { |
a7c1916a | 1786 | if (/* Don't change a variable array bound or initial value to a constant |
4f976745 RK |
1787 | in a place where a variable is invalid. Note that DECL_INITIAL |
1788 | isn't valid for a PARM_DECL. */ | |
a7c1916a | 1789 | current_function_decl != 0 |
4f976745 | 1790 | && TREE_CODE (decl) != PARM_DECL |
3f75a254 | 1791 | && !TREE_THIS_VOLATILE (decl) |
83bab8db | 1792 | && TREE_READONLY (decl) |
400fbf9f JW |
1793 | && DECL_INITIAL (decl) != 0 |
1794 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1795 | /* This is invalid if initial value is not constant. | |
1796 | If it has either a function call, a memory reference, | |
1797 | or a variable, then re-evaluating it could give different results. */ | |
1798 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1799 | /* Check for cases where this is sub-optimal, even though valid. */ | |
2f74f7e9 | 1800 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
400fbf9f JW |
1801 | return DECL_INITIAL (decl); |
1802 | return decl; | |
1803 | } | |
1804 | ||
f2a71bbc JM |
1805 | /* Convert the array expression EXP to a pointer. */ |
1806 | static tree | |
c2255bc4 | 1807 | array_to_pointer_conversion (location_t loc, tree exp) |
207bf485 | 1808 | { |
f2a71bbc | 1809 | tree orig_exp = exp; |
207bf485 | 1810 | tree type = TREE_TYPE (exp); |
f2a71bbc JM |
1811 | tree adr; |
1812 | tree restype = TREE_TYPE (type); | |
1813 | tree ptrtype; | |
207bf485 | 1814 | |
f2a71bbc | 1815 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
207bf485 | 1816 | |
f2a71bbc | 1817 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1818 | |
487a92fe JM |
1819 | if (TREE_NO_WARNING (orig_exp)) |
1820 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1821 | |
f2a71bbc JM |
1822 | ptrtype = build_pointer_type (restype); |
1823 | ||
22d03525 | 1824 | if (INDIRECT_REF_P (exp)) |
f2a71bbc JM |
1825 | return convert (ptrtype, TREE_OPERAND (exp, 0)); |
1826 | ||
1f37c583 JM |
1827 | /* In C++ array compound literals are temporary objects unless they are |
1828 | const or appear in namespace scope, so they are destroyed too soon | |
1829 | to use them for much of anything (c++/53220). */ | |
1830 | if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) | |
1831 | { | |
1832 | tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
1833 | if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) | |
1834 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, | |
1835 | "converting an array compound literal to a pointer " | |
1836 | "is ill-formed in C++"); | |
1837 | } | |
1838 | ||
c2255bc4 | 1839 | adr = build_unary_op (loc, ADDR_EXPR, exp, 1); |
f2a71bbc JM |
1840 | return convert (ptrtype, adr); |
1841 | } | |
207bf485 | 1842 | |
f2a71bbc JM |
1843 | /* Convert the function expression EXP to a pointer. */ |
1844 | static tree | |
c2255bc4 | 1845 | function_to_pointer_conversion (location_t loc, tree exp) |
f2a71bbc JM |
1846 | { |
1847 | tree orig_exp = exp; | |
207bf485 | 1848 | |
f2a71bbc | 1849 | gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); |
207bf485 | 1850 | |
f2a71bbc | 1851 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1852 | |
f2a71bbc JM |
1853 | if (TREE_NO_WARNING (orig_exp)) |
1854 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1855 | |
c2255bc4 | 1856 | return build_unary_op (loc, ADDR_EXPR, exp, 0); |
f2a71bbc | 1857 | } |
207bf485 | 1858 | |
ebfbbdc5 JJ |
1859 | /* Mark EXP as read, not just set, for set but not used -Wunused |
1860 | warning purposes. */ | |
1861 | ||
1862 | void | |
1863 | mark_exp_read (tree exp) | |
1864 | { | |
1865 | switch (TREE_CODE (exp)) | |
1866 | { | |
1867 | case VAR_DECL: | |
1868 | case PARM_DECL: | |
1869 | DECL_READ_P (exp) = 1; | |
1870 | break; | |
1871 | case ARRAY_REF: | |
1872 | case COMPONENT_REF: | |
1873 | case MODIFY_EXPR: | |
1874 | case REALPART_EXPR: | |
1875 | case IMAGPART_EXPR: | |
1876 | CASE_CONVERT: | |
1877 | case ADDR_EXPR: | |
1878 | mark_exp_read (TREE_OPERAND (exp, 0)); | |
1879 | break; | |
1880 | case COMPOUND_EXPR: | |
82c3c067 | 1881 | case C_MAYBE_CONST_EXPR: |
ebfbbdc5 JJ |
1882 | mark_exp_read (TREE_OPERAND (exp, 1)); |
1883 | break; | |
1884 | default: | |
1885 | break; | |
1886 | } | |
1887 | } | |
1888 | ||
f2a71bbc JM |
1889 | /* Perform the default conversion of arrays and functions to pointers. |
1890 | Return the result of converting EXP. For any other expression, just | |
c2255bc4 AH |
1891 | return EXP. |
1892 | ||
1893 | LOC is the location of the expression. */ | |
f2a71bbc JM |
1894 | |
1895 | struct c_expr | |
c2255bc4 | 1896 | default_function_array_conversion (location_t loc, struct c_expr exp) |
f2a71bbc JM |
1897 | { |
1898 | tree orig_exp = exp.value; | |
1899 | tree type = TREE_TYPE (exp.value); | |
1900 | enum tree_code code = TREE_CODE (type); | |
1901 | ||
1902 | switch (code) | |
1903 | { | |
1904 | case ARRAY_TYPE: | |
1905 | { | |
1906 | bool not_lvalue = false; | |
1907 | bool lvalue_array_p; | |
1908 | ||
1909 | while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR | |
1043771b | 1910 | || CONVERT_EXPR_P (exp.value)) |
f2a71bbc JM |
1911 | && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) |
1912 | { | |
1913 | if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) | |
1914 | not_lvalue = true; | |
1915 | exp.value = TREE_OPERAND (exp.value, 0); | |
1916 | } | |
1917 | ||
1918 | if (TREE_NO_WARNING (orig_exp)) | |
1919 | TREE_NO_WARNING (exp.value) = 1; | |
1920 | ||
1921 | lvalue_array_p = !not_lvalue && lvalue_p (exp.value); | |
1922 | if (!flag_isoc99 && !lvalue_array_p) | |
1923 | { | |
1924 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1925 | Normally, using such an array would be invalid; but it can | |
1926 | be used correctly inside sizeof or as a statement expression. | |
1927 | Thus, do not give an error here; an error will result later. */ | |
1928 | return exp; | |
1929 | } | |
1930 | ||
c2255bc4 | 1931 | exp.value = array_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1932 | } |
1933 | break; | |
1934 | case FUNCTION_TYPE: | |
c2255bc4 | 1935 | exp.value = function_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1936 | break; |
1937 | default: | |
f2a71bbc | 1938 | break; |
207bf485 | 1939 | } |
f2a71bbc | 1940 | |
207bf485 JM |
1941 | return exp; |
1942 | } | |
1943 | ||
ebfbbdc5 JJ |
1944 | struct c_expr |
1945 | default_function_array_read_conversion (location_t loc, struct c_expr exp) | |
1946 | { | |
1947 | mark_exp_read (exp.value); | |
1948 | return default_function_array_conversion (loc, exp); | |
1949 | } | |
522ddfa2 | 1950 | |
267bac10 JM |
1951 | /* Return whether EXPR should be treated as an atomic lvalue for the |
1952 | purposes of load and store handling. */ | |
1953 | ||
1954 | static bool | |
1955 | really_atomic_lvalue (tree expr) | |
1956 | { | |
7a0ca710 | 1957 | if (error_operand_p (expr)) |
267bac10 JM |
1958 | return false; |
1959 | if (!TYPE_ATOMIC (TREE_TYPE (expr))) | |
1960 | return false; | |
1961 | if (!lvalue_p (expr)) | |
1962 | return false; | |
1963 | ||
1964 | /* Ignore _Atomic on register variables, since their addresses can't | |
1965 | be taken so (a) atomicity is irrelevant and (b) the normal atomic | |
1966 | sequences wouldn't work. Ignore _Atomic on structures containing | |
1967 | bit-fields, since accessing elements of atomic structures or | |
1968 | unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if | |
1969 | it's undefined at translation time or execution time, and the | |
1970 | normal atomic sequences again wouldn't work. */ | |
1971 | while (handled_component_p (expr)) | |
1972 | { | |
1973 | if (TREE_CODE (expr) == COMPONENT_REF | |
1974 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
1975 | return false; | |
1976 | expr = TREE_OPERAND (expr, 0); | |
1977 | } | |
1978 | if (DECL_P (expr) && C_DECL_REGISTER (expr)) | |
1979 | return false; | |
1980 | return true; | |
1981 | } | |
1982 | ||
1983 | /* Convert expression EXP (location LOC) from lvalue to rvalue, | |
1984 | including converting functions and arrays to pointers if CONVERT_P. | |
1985 | If READ_P, also mark the expression as having been read. */ | |
1986 | ||
1987 | struct c_expr | |
1988 | convert_lvalue_to_rvalue (location_t loc, struct c_expr exp, | |
1989 | bool convert_p, bool read_p) | |
1990 | { | |
1991 | if (read_p) | |
1992 | mark_exp_read (exp.value); | |
1993 | if (convert_p) | |
1994 | exp = default_function_array_conversion (loc, exp); | |
1995 | if (really_atomic_lvalue (exp.value)) | |
1996 | { | |
1997 | vec<tree, va_gc> *params; | |
1998 | tree nonatomic_type, tmp, tmp_addr, fndecl, func_call; | |
1999 | tree expr_type = TREE_TYPE (exp.value); | |
2000 | tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0); | |
2001 | tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); | |
2002 | ||
2003 | gcc_assert (TYPE_ATOMIC (expr_type)); | |
2004 | ||
2005 | /* Expansion of a generic atomic load may require an addition | |
2006 | element, so allocate enough to prevent a resize. */ | |
2007 | vec_alloc (params, 4); | |
2008 | ||
2009 | /* Remove the qualifiers for the rest of the expressions and | |
2010 | create the VAL temp variable to hold the RHS. */ | |
2011 | nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED); | |
5c4abbb8 | 2012 | tmp = create_tmp_var_raw (nonatomic_type); |
267bac10 JM |
2013 | tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0); |
2014 | TREE_ADDRESSABLE (tmp) = 1; | |
cc28fc7f | 2015 | TREE_NO_WARNING (tmp) = 1; |
267bac10 JM |
2016 | |
2017 | /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */ | |
2018 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); | |
2019 | params->quick_push (expr_addr); | |
2020 | params->quick_push (tmp_addr); | |
2021 | params->quick_push (seq_cst); | |
8edbfaa6 | 2022 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 | 2023 | |
cc28fc7f MP |
2024 | /* EXPR is always read. */ |
2025 | mark_exp_read (exp.value); | |
2026 | ||
267bac10 | 2027 | /* Return tmp which contains the value loaded. */ |
5c4abbb8 MP |
2028 | exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call, |
2029 | NULL_TREE, NULL_TREE); | |
267bac10 JM |
2030 | } |
2031 | return exp; | |
2032 | } | |
2033 | ||
522ddfa2 JM |
2034 | /* EXP is an expression of integer type. Apply the integer promotions |
2035 | to it and return the promoted value. */ | |
400fbf9f JW |
2036 | |
2037 | tree | |
522ddfa2 | 2038 | perform_integral_promotions (tree exp) |
400fbf9f | 2039 | { |
b3694847 SS |
2040 | tree type = TREE_TYPE (exp); |
2041 | enum tree_code code = TREE_CODE (type); | |
400fbf9f | 2042 | |
522ddfa2 | 2043 | gcc_assert (INTEGRAL_TYPE_P (type)); |
157689c6 | 2044 | |
400fbf9f JW |
2045 | /* Normally convert enums to int, |
2046 | but convert wide enums to something wider. */ | |
2047 | if (code == ENUMERAL_TYPE) | |
2048 | { | |
b0c48229 NB |
2049 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
2050 | TYPE_PRECISION (integer_type_node)), | |
2051 | ((TYPE_PRECISION (type) | |
2052 | >= TYPE_PRECISION (integer_type_node)) | |
8df83eae | 2053 | && TYPE_UNSIGNED (type))); |
05bccae2 | 2054 | |
400fbf9f JW |
2055 | return convert (type, exp); |
2056 | } | |
2057 | ||
522ddfa2 JM |
2058 | /* ??? This should no longer be needed now bit-fields have their |
2059 | proper types. */ | |
9753f113 | 2060 | if (TREE_CODE (exp) == COMPONENT_REF |
05bccae2 | 2061 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) |
cff9c407 | 2062 | /* If it's thinner than an int, promote it like a |
d72040f5 | 2063 | c_promoting_integer_type_p, otherwise leave it alone. */ |
05bccae2 RK |
2064 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), |
2065 | TYPE_PRECISION (integer_type_node))) | |
f458d1d5 | 2066 | return convert (integer_type_node, exp); |
9753f113 | 2067 | |
d72040f5 | 2068 | if (c_promoting_integer_type_p (type)) |
400fbf9f | 2069 | { |
f458d1d5 | 2070 | /* Preserve unsignedness if not really getting any wider. */ |
8df83eae | 2071 | if (TYPE_UNSIGNED (type) |
f458d1d5 | 2072 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
400fbf9f | 2073 | return convert (unsigned_type_node, exp); |
05bccae2 | 2074 | |
400fbf9f JW |
2075 | return convert (integer_type_node, exp); |
2076 | } | |
05bccae2 | 2077 | |
522ddfa2 JM |
2078 | return exp; |
2079 | } | |
2080 | ||
2081 | ||
2082 | /* Perform default promotions for C data used in expressions. | |
46bdb9cf | 2083 | Enumeral types or short or char are converted to int. |
522ddfa2 JM |
2084 | In addition, manifest constants symbols are replaced by their values. */ |
2085 | ||
2086 | tree | |
2087 | default_conversion (tree exp) | |
2088 | { | |
2089 | tree orig_exp; | |
2090 | tree type = TREE_TYPE (exp); | |
2091 | enum tree_code code = TREE_CODE (type); | |
40449a90 | 2092 | tree promoted_type; |
522ddfa2 | 2093 | |
ebfbbdc5 JJ |
2094 | mark_exp_read (exp); |
2095 | ||
46bdb9cf JM |
2096 | /* Functions and arrays have been converted during parsing. */ |
2097 | gcc_assert (code != FUNCTION_TYPE); | |
2098 | if (code == ARRAY_TYPE) | |
2099 | return exp; | |
522ddfa2 JM |
2100 | |
2101 | /* Constants can be used directly unless they're not loadable. */ | |
2102 | if (TREE_CODE (exp) == CONST_DECL) | |
2103 | exp = DECL_INITIAL (exp); | |
2104 | ||
522ddfa2 JM |
2105 | /* Strip no-op conversions. */ |
2106 | orig_exp = exp; | |
2107 | STRIP_TYPE_NOPS (exp); | |
2108 | ||
2109 | if (TREE_NO_WARNING (orig_exp)) | |
2110 | TREE_NO_WARNING (exp) = 1; | |
2111 | ||
400fbf9f JW |
2112 | if (code == VOID_TYPE) |
2113 | { | |
5436fa2e MP |
2114 | error_at (EXPR_LOC_OR_LOC (exp, input_location), |
2115 | "void value not ignored as it ought to be"); | |
400fbf9f JW |
2116 | return error_mark_node; |
2117 | } | |
808d6eaa JM |
2118 | |
2119 | exp = require_complete_type (exp); | |
2120 | if (exp == error_mark_node) | |
2121 | return error_mark_node; | |
2122 | ||
40449a90 SL |
2123 | promoted_type = targetm.promoted_type (type); |
2124 | if (promoted_type) | |
2125 | return convert (promoted_type, exp); | |
2126 | ||
808d6eaa JM |
2127 | if (INTEGRAL_TYPE_P (type)) |
2128 | return perform_integral_promotions (exp); | |
2129 | ||
400fbf9f JW |
2130 | return exp; |
2131 | } | |
2132 | \f | |
0fb96aa4 | 2133 | /* Look up COMPONENT in a structure or union TYPE. |
e9b2c823 NB |
2134 | |
2135 | If the component name is not found, returns NULL_TREE. Otherwise, | |
2136 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
2137 | stepping down the chain to the component, which is in the last | |
2138 | TREE_VALUE of the list. Normally the list is of length one, but if | |
2139 | the component is embedded within (nested) anonymous structures or | |
2140 | unions, the list steps down the chain to the component. */ | |
2f6e4e97 | 2141 | |
2f2d13da | 2142 | static tree |
0fb96aa4 | 2143 | lookup_field (tree type, tree component) |
2f2d13da DE |
2144 | { |
2145 | tree field; | |
2146 | ||
2147 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
2148 | to the field elements. Use a binary search on this array to quickly | |
2149 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
2150 | will always be set for structures which have many elements. */ | |
2151 | ||
22a0b85f | 2152 | if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) |
2f2d13da DE |
2153 | { |
2154 | int bot, top, half; | |
d07605f5 | 2155 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; |
2f2d13da DE |
2156 | |
2157 | field = TYPE_FIELDS (type); | |
2158 | bot = 0; | |
d07605f5 | 2159 | top = TYPE_LANG_SPECIFIC (type)->s->len; |
2f2d13da DE |
2160 | while (top - bot > 1) |
2161 | { | |
2f2d13da DE |
2162 | half = (top - bot + 1) >> 1; |
2163 | field = field_array[bot+half]; | |
2164 | ||
2165 | if (DECL_NAME (field) == NULL_TREE) | |
2166 | { | |
2167 | /* Step through all anon unions in linear fashion. */ | |
2168 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
2169 | { | |
2f2d13da | 2170 | field = field_array[bot++]; |
a68b98cf RK |
2171 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
2172 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
19d76e60 | 2173 | { |
0fb96aa4 | 2174 | tree anon = lookup_field (TREE_TYPE (field), component); |
e9b2c823 NB |
2175 | |
2176 | if (anon) | |
2177 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2178 | |
2179 | /* The Plan 9 compiler permits referring | |
2180 | directly to an anonymous struct/union field | |
2181 | using a typedef name. */ | |
2182 | if (flag_plan9_extensions | |
2183 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2184 | && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) | |
2185 | == TYPE_DECL) | |
2186 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2187 | == component)) | |
2188 | break; | |
2f6e4e97 | 2189 | } |
2f2d13da DE |
2190 | } |
2191 | ||
2192 | /* Entire record is only anon unions. */ | |
2193 | if (bot > top) | |
2194 | return NULL_TREE; | |
2195 | ||
2196 | /* Restart the binary search, with new lower bound. */ | |
2197 | continue; | |
2198 | } | |
2199 | ||
e8b87aac | 2200 | if (DECL_NAME (field) == component) |
2f2d13da | 2201 | break; |
e8b87aac | 2202 | if (DECL_NAME (field) < component) |
2f2d13da DE |
2203 | bot += half; |
2204 | else | |
2205 | top = bot + half; | |
2206 | } | |
2207 | ||
2208 | if (DECL_NAME (field_array[bot]) == component) | |
2209 | field = field_array[bot]; | |
2210 | else if (DECL_NAME (field) != component) | |
e9b2c823 | 2211 | return NULL_TREE; |
2f2d13da DE |
2212 | } |
2213 | else | |
2214 | { | |
910ad8de | 2215 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
2f2d13da | 2216 | { |
e9b2c823 NB |
2217 | if (DECL_NAME (field) == NULL_TREE |
2218 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
2219 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
2f2d13da | 2220 | { |
0fb96aa4 | 2221 | tree anon = lookup_field (TREE_TYPE (field), component); |
a68b98cf | 2222 | |
e9b2c823 NB |
2223 | if (anon) |
2224 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2225 | |
2226 | /* The Plan 9 compiler permits referring directly to an | |
2227 | anonymous struct/union field using a typedef | |
2228 | name. */ | |
2229 | if (flag_plan9_extensions | |
2230 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2231 | && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL | |
2232 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2233 | == component)) | |
2234 | break; | |
2f2d13da DE |
2235 | } |
2236 | ||
2237 | if (DECL_NAME (field) == component) | |
2238 | break; | |
2239 | } | |
e9b2c823 NB |
2240 | |
2241 | if (field == NULL_TREE) | |
2242 | return NULL_TREE; | |
2f2d13da DE |
2243 | } |
2244 | ||
e9b2c823 | 2245 | return tree_cons (NULL_TREE, field, NULL_TREE); |
2f2d13da DE |
2246 | } |
2247 | ||
c2255bc4 AH |
2248 | /* Make an expression to refer to the COMPONENT field of structure or |
2249 | union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the | |
2250 | location of the COMPONENT_REF. */ | |
400fbf9f JW |
2251 | |
2252 | tree | |
c2255bc4 | 2253 | build_component_ref (location_t loc, tree datum, tree component) |
400fbf9f | 2254 | { |
b3694847 SS |
2255 | tree type = TREE_TYPE (datum); |
2256 | enum tree_code code = TREE_CODE (type); | |
2257 | tree field = NULL; | |
2258 | tree ref; | |
1e57bf47 | 2259 | bool datum_lvalue = lvalue_p (datum); |
400fbf9f | 2260 | |
7a3ea201 RH |
2261 | if (!objc_is_public (datum, component)) |
2262 | return error_mark_node; | |
2263 | ||
46a88c12 | 2264 | /* Detect Objective-C property syntax object.property. */ |
668ea4b1 | 2265 | if (c_dialect_objc () |
46a88c12 | 2266 | && (ref = objc_maybe_build_component_ref (datum, component))) |
668ea4b1 IS |
2267 | return ref; |
2268 | ||
400fbf9f JW |
2269 | /* See if there is a field or component with name COMPONENT. */ |
2270 | ||
2271 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
2272 | { | |
d0f062fb | 2273 | if (!COMPLETE_TYPE_P (type)) |
400fbf9f | 2274 | { |
7a228918 | 2275 | c_incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
2276 | return error_mark_node; |
2277 | } | |
2278 | ||
0fb96aa4 | 2279 | field = lookup_field (type, component); |
400fbf9f JW |
2280 | |
2281 | if (!field) | |
2282 | { | |
c2255bc4 | 2283 | error_at (loc, "%qT has no member named %qE", type, component); |
400fbf9f JW |
2284 | return error_mark_node; |
2285 | } | |
400fbf9f | 2286 | |
e9b2c823 NB |
2287 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. |
2288 | This might be better solved in future the way the C++ front | |
2289 | end does it - by giving the anonymous entities each a | |
2290 | separate name and type, and then have build_component_ref | |
2291 | recursively call itself. We can't do that here. */ | |
46ea50cb | 2292 | do |
19d76e60 | 2293 | { |
e9b2c823 | 2294 | tree subdatum = TREE_VALUE (field); |
efed193e JM |
2295 | int quals; |
2296 | tree subtype; | |
1e57bf47 | 2297 | bool use_datum_quals; |
e9b2c823 NB |
2298 | |
2299 | if (TREE_TYPE (subdatum) == error_mark_node) | |
2300 | return error_mark_node; | |
2301 | ||
1e57bf47 JM |
2302 | /* If this is an rvalue, it does not have qualifiers in C |
2303 | standard terms and we must avoid propagating such | |
2304 | qualifiers down to a non-lvalue array that is then | |
2305 | converted to a pointer. */ | |
2306 | use_datum_quals = (datum_lvalue | |
2307 | || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); | |
2308 | ||
efed193e | 2309 | quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); |
1e57bf47 JM |
2310 | if (use_datum_quals) |
2311 | quals |= TYPE_QUALS (TREE_TYPE (datum)); | |
efed193e JM |
2312 | subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); |
2313 | ||
2314 | ref = build3 (COMPONENT_REF, subtype, datum, subdatum, | |
53fb4de3 | 2315 | NULL_TREE); |
c2255bc4 | 2316 | SET_EXPR_LOCATION (ref, loc); |
1e57bf47 JM |
2317 | if (TREE_READONLY (subdatum) |
2318 | || (use_datum_quals && TREE_READONLY (datum))) | |
19d76e60 | 2319 | TREE_READONLY (ref) = 1; |
1e57bf47 JM |
2320 | if (TREE_THIS_VOLATILE (subdatum) |
2321 | || (use_datum_quals && TREE_THIS_VOLATILE (datum))) | |
19d76e60 | 2322 | TREE_THIS_VOLATILE (ref) = 1; |
e23bd218 IR |
2323 | |
2324 | if (TREE_DEPRECATED (subdatum)) | |
9b86d6bb | 2325 | warn_deprecated_use (subdatum, NULL_TREE); |
e23bd218 | 2326 | |
19d76e60 | 2327 | datum = ref; |
46ea50cb RS |
2328 | |
2329 | field = TREE_CHAIN (field); | |
19d76e60 | 2330 | } |
46ea50cb | 2331 | while (field); |
19d76e60 | 2332 | |
400fbf9f JW |
2333 | return ref; |
2334 | } | |
2335 | else if (code != ERROR_MARK) | |
c2255bc4 AH |
2336 | error_at (loc, |
2337 | "request for member %qE in something not a structure or union", | |
2338 | component); | |
400fbf9f JW |
2339 | |
2340 | return error_mark_node; | |
2341 | } | |
2342 | \f | |
2343 | /* Given an expression PTR for a pointer, return an expression | |
2344 | for the value pointed to. | |
6a3799eb AH |
2345 | ERRORSTRING is the name of the operator to appear in error messages. |
2346 | ||
2347 | LOC is the location to use for the generated tree. */ | |
400fbf9f JW |
2348 | |
2349 | tree | |
dd865ef6 | 2350 | build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) |
400fbf9f | 2351 | { |
b3694847 SS |
2352 | tree pointer = default_conversion (ptr); |
2353 | tree type = TREE_TYPE (pointer); | |
6a3799eb | 2354 | tree ref; |
400fbf9f JW |
2355 | |
2356 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b | 2357 | { |
1043771b | 2358 | if (CONVERT_EXPR_P (pointer) |
79bedddc SR |
2359 | || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) |
2360 | { | |
2361 | /* If a warning is issued, mark it to avoid duplicates from | |
2362 | the backend. This only needs to be done at | |
2363 | warn_strict_aliasing > 2. */ | |
2364 | if (warn_strict_aliasing > 2) | |
2365 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), | |
2366 | type, TREE_OPERAND (pointer, 0))) | |
2367 | TREE_NO_WARNING (pointer) = 1; | |
2368 | } | |
2369 | ||
870cc33b | 2370 | if (TREE_CODE (pointer) == ADDR_EXPR |
870cc33b RS |
2371 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) |
2372 | == TREE_TYPE (type))) | |
6a3799eb AH |
2373 | { |
2374 | ref = TREE_OPERAND (pointer, 0); | |
2375 | protected_set_expr_location (ref, loc); | |
2376 | return ref; | |
2377 | } | |
870cc33b RS |
2378 | else |
2379 | { | |
2380 | tree t = TREE_TYPE (type); | |
46df2823 | 2381 | |
984dfd8c | 2382 | ref = build1 (INDIRECT_REF, t, pointer); |
400fbf9f | 2383 | |
baae9b65 | 2384 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) |
870cc33b | 2385 | { |
d9b7be2e MP |
2386 | if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr))) |
2387 | { | |
2388 | error_at (loc, "dereferencing pointer to incomplete type " | |
2389 | "%qT", t); | |
2390 | C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1; | |
2391 | } | |
870cc33b RS |
2392 | return error_mark_node; |
2393 | } | |
7d882b83 | 2394 | if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) |
c9f9eb5d | 2395 | warning_at (loc, 0, "dereferencing %<void *%> pointer"); |
870cc33b RS |
2396 | |
2397 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
2398 | so that we get the proper error message if the result is used | |
2399 | to assign to. Also, &* is supposed to be a no-op. | |
2400 | And ANSI C seems to specify that the type of the result | |
2401 | should be the const type. */ | |
2402 | /* A de-reference of a pointer to const is not a const. It is valid | |
2403 | to change it via some other pointer. */ | |
2404 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
2405 | TREE_SIDE_EFFECTS (ref) | |
271bd540 | 2406 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); |
493692cd | 2407 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
6a3799eb | 2408 | protected_set_expr_location (ref, loc); |
870cc33b RS |
2409 | return ref; |
2410 | } | |
2411 | } | |
400fbf9f | 2412 | else if (TREE_CODE (pointer) != ERROR_MARK) |
7a6daeb0 NF |
2413 | invalid_indirection_error (loc, type, errstring); |
2414 | ||
400fbf9f JW |
2415 | return error_mark_node; |
2416 | } | |
2417 | ||
2418 | /* This handles expressions of the form "a[i]", which denotes | |
2419 | an array reference. | |
2420 | ||
2421 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2422 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2423 | This avoids forcing the array out of registers, and can work on | |
2424 | arrays that are not lvalues (for example, members of structures returned | |
6a3799eb AH |
2425 | by functions). |
2426 | ||
30cd1c5d AS |
2427 | For vector types, allow vector[i] but not i[vector], and create |
2428 | *(((type*)&vectortype) + i) for the expression. | |
2429 | ||
6a3799eb | 2430 | LOC is the location to use for the returned expression. */ |
400fbf9f JW |
2431 | |
2432 | tree | |
c2255bc4 | 2433 | build_array_ref (location_t loc, tree array, tree index) |
400fbf9f | 2434 | { |
6a3799eb | 2435 | tree ret; |
a4ab7973 | 2436 | bool swapped = false; |
400fbf9f JW |
2437 | if (TREE_TYPE (array) == error_mark_node |
2438 | || TREE_TYPE (index) == error_mark_node) | |
2439 | return error_mark_node; | |
2440 | ||
b72271b9 | 2441 | if (flag_cilkplus && contains_array_notation_expr (index)) |
36536d79 BI |
2442 | { |
2443 | size_t rank = 0; | |
2444 | if (!find_rank (loc, index, index, true, &rank)) | |
2445 | return error_mark_node; | |
2446 | if (rank > 1) | |
2447 | { | |
2448 | error_at (loc, "rank of the array's index is greater than 1"); | |
2449 | return error_mark_node; | |
2450 | } | |
2451 | } | |
a4ab7973 | 2452 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE |
30cd1c5d AS |
2453 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE |
2454 | /* Allow vector[index] but not index[vector]. */ | |
31521951 | 2455 | && !VECTOR_TYPE_P (TREE_TYPE (array))) |
400fbf9f | 2456 | { |
a4ab7973 JM |
2457 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE |
2458 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
fdeefd49 | 2459 | { |
f90e8e2e | 2460 | error_at (loc, |
30cd1c5d AS |
2461 | "subscripted value is neither array nor pointer nor vector"); |
2462 | ||
fdeefd49 RS |
2463 | return error_mark_node; |
2464 | } | |
fab27f52 | 2465 | std::swap (array, index); |
a4ab7973 JM |
2466 | swapped = true; |
2467 | } | |
2468 | ||
2469 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2470 | { | |
a63068b6 | 2471 | error_at (loc, "array subscript is not an integer"); |
a4ab7973 JM |
2472 | return error_mark_node; |
2473 | } | |
2474 | ||
2475 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2476 | { | |
a63068b6 | 2477 | error_at (loc, "subscripted value is pointer to function"); |
a4ab7973 JM |
2478 | return error_mark_node; |
2479 | } | |
2480 | ||
ff6b6641 GDR |
2481 | /* ??? Existing practice has been to warn only when the char |
2482 | index is syntactically the index, not for char[array]. */ | |
2483 | if (!swapped) | |
5bd012f8 | 2484 | warn_array_subscript_with_type_char (loc, index); |
a4ab7973 JM |
2485 | |
2486 | /* Apply default promotions *after* noticing character types. */ | |
2487 | index = default_conversion (index); | |
f406ae1f MP |
2488 | if (index == error_mark_node) |
2489 | return error_mark_node; | |
a4ab7973 JM |
2490 | |
2491 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
f90e8e2e | 2492 | |
aa7da51a JJ |
2493 | bool non_lvalue |
2494 | = convert_vector_to_pointer_for_subscript (loc, &array, index); | |
a4ab7973 JM |
2495 | |
2496 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2497 | { | |
e4d35515 | 2498 | tree rval, type; |
fdeefd49 | 2499 | |
400fbf9f JW |
2500 | /* An array that is indexed by a non-constant |
2501 | cannot be stored in a register; we must be able to do | |
2502 | address arithmetic on its address. | |
2503 | Likewise an array of elements of variable size. */ | |
2504 | if (TREE_CODE (index) != INTEGER_CST | |
d0f062fb | 2505 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
400fbf9f JW |
2506 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
2507 | { | |
dffd7eb6 | 2508 | if (!c_mark_addressable (array)) |
400fbf9f JW |
2509 | return error_mark_node; |
2510 | } | |
e6d52559 JW |
2511 | /* An array that is indexed by a constant value which is not within |
2512 | the array bounds cannot be stored in a register either; because we | |
2513 | would get a crash in store_bit_field/extract_bit_field when trying | |
2514 | to access a non-existent part of the register. */ | |
2515 | if (TREE_CODE (index) == INTEGER_CST | |
eb34af89 | 2516 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3f75a254 | 2517 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) |
e6d52559 | 2518 | { |
dffd7eb6 | 2519 | if (!c_mark_addressable (array)) |
e6d52559 JW |
2520 | return error_mark_node; |
2521 | } | |
400fbf9f | 2522 | |
f3bede71 | 2523 | if (pedantic || warn_c90_c99_compat) |
400fbf9f JW |
2524 | { |
2525 | tree foo = array; | |
2526 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2527 | foo = TREE_OPERAND (foo, 0); | |
0ae9bd27 | 2528 | if (VAR_P (foo) && C_DECL_REGISTER (foo)) |
c1771a20 | 2529 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 2530 | "ISO C forbids subscripting %<register%> array"); |
f3bede71 MP |
2531 | else if (!lvalue_p (foo)) |
2532 | pedwarn_c90 (loc, OPT_Wpedantic, | |
2533 | "ISO C90 forbids subscripting non-lvalue " | |
2534 | "array"); | |
400fbf9f JW |
2535 | } |
2536 | ||
46df2823 | 2537 | type = TREE_TYPE (TREE_TYPE (array)); |
53fb4de3 | 2538 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
400fbf9f | 2539 | /* Array ref is const/volatile if the array elements are |
c22cacf3 | 2540 | or if the array is. */ |
400fbf9f JW |
2541 | TREE_READONLY (rval) |
2542 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2543 | | TREE_READONLY (array)); | |
2544 | TREE_SIDE_EFFECTS (rval) | |
2545 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2546 | | TREE_SIDE_EFFECTS (array)); | |
2547 | TREE_THIS_VOLATILE (rval) | |
2548 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2549 | /* This was added by rms on 16 Nov 91. | |
2f6e4e97 | 2550 | It fixes vol struct foo *a; a->elts[1] |
400fbf9f JW |
2551 | in an inline function. |
2552 | Hope it doesn't break something else. */ | |
2553 | | TREE_THIS_VOLATILE (array)); | |
928c19bb | 2554 | ret = require_complete_type (rval); |
6a3799eb | 2555 | protected_set_expr_location (ret, loc); |
aa7da51a JJ |
2556 | if (non_lvalue) |
2557 | ret = non_lvalue_loc (loc, ret); | |
6a3799eb | 2558 | return ret; |
400fbf9f | 2559 | } |
a4ab7973 JM |
2560 | else |
2561 | { | |
2562 | tree ar = default_conversion (array); | |
400fbf9f | 2563 | |
a4ab7973 JM |
2564 | if (ar == error_mark_node) |
2565 | return ar; | |
400fbf9f | 2566 | |
a4ab7973 JM |
2567 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); |
2568 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
400fbf9f | 2569 | |
aa7da51a JJ |
2570 | ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar, |
2571 | index, 0), | |
2572 | RO_ARRAY_INDEXING); | |
2573 | if (non_lvalue) | |
2574 | ret = non_lvalue_loc (loc, ret); | |
2575 | return ret; | |
a4ab7973 | 2576 | } |
400fbf9f JW |
2577 | } |
2578 | \f | |
7e585d16 | 2579 | /* Build an external reference to identifier ID. FUN indicates |
766beb40 | 2580 | whether this will be used for a function call. LOC is the source |
6866c6e8 ILT |
2581 | location of the identifier. This sets *TYPE to the type of the |
2582 | identifier, which is not the same as the type of the returned value | |
2583 | for CONST_DECLs defined as enum constants. If the type of the | |
2584 | identifier is not available, *TYPE is set to NULL. */ | |
7e585d16 | 2585 | tree |
c2255bc4 | 2586 | build_external_ref (location_t loc, tree id, int fun, tree *type) |
7e585d16 ZW |
2587 | { |
2588 | tree ref; | |
2589 | tree decl = lookup_name (id); | |
16b34ad6 ZL |
2590 | |
2591 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2592 | whatever lookup_name() found. */ | |
2593 | decl = objc_lookup_ivar (decl, id); | |
7e585d16 | 2594 | |
6866c6e8 | 2595 | *type = NULL; |
339a28b9 | 2596 | if (decl && decl != error_mark_node) |
6866c6e8 ILT |
2597 | { |
2598 | ref = decl; | |
2599 | *type = TREE_TYPE (ref); | |
2600 | } | |
339a28b9 ZW |
2601 | else if (fun) |
2602 | /* Implicit function declaration. */ | |
c2255bc4 | 2603 | ref = implicitly_declare (loc, id); |
339a28b9 ZW |
2604 | else if (decl == error_mark_node) |
2605 | /* Don't complain about something that's already been | |
2606 | complained about. */ | |
2607 | return error_mark_node; | |
2608 | else | |
2609 | { | |
c2255bc4 | 2610 | undeclared_variable (loc, id); |
339a28b9 ZW |
2611 | return error_mark_node; |
2612 | } | |
7e585d16 ZW |
2613 | |
2614 | if (TREE_TYPE (ref) == error_mark_node) | |
2615 | return error_mark_node; | |
2616 | ||
339a28b9 | 2617 | if (TREE_DEPRECATED (ref)) |
9b86d6bb | 2618 | warn_deprecated_use (ref, NULL_TREE); |
339a28b9 | 2619 | |
ad960f56 | 2620 | /* Recursive call does not count as usage. */ |
b8698a0f | 2621 | if (ref != current_function_decl) |
ad960f56 | 2622 | { |
ad960f56 MLI |
2623 | TREE_USED (ref) = 1; |
2624 | } | |
7e585d16 | 2625 | |
bc4b653b JM |
2626 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) |
2627 | { | |
2628 | if (!in_sizeof && !in_typeof) | |
2629 | C_DECL_USED (ref) = 1; | |
2630 | else if (DECL_INITIAL (ref) == 0 | |
2631 | && DECL_EXTERNAL (ref) | |
2632 | && !TREE_PUBLIC (ref)) | |
2633 | record_maybe_used_decl (ref); | |
2634 | } | |
2635 | ||
7e585d16 ZW |
2636 | if (TREE_CODE (ref) == CONST_DECL) |
2637 | { | |
6193b8b7 | 2638 | used_types_insert (TREE_TYPE (ref)); |
24b97832 ILT |
2639 | |
2640 | if (warn_cxx_compat | |
2641 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2642 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2643 | { | |
2644 | warning_at (loc, OPT_Wc___compat, | |
2645 | ("enum constant defined in struct or union " | |
2646 | "is not visible in C++")); | |
2647 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2648 | } | |
2649 | ||
7e585d16 ZW |
2650 | ref = DECL_INITIAL (ref); |
2651 | TREE_CONSTANT (ref) = 1; | |
2652 | } | |
6a29edea | 2653 | else if (current_function_decl != 0 |
4b1e44be | 2654 | && !DECL_FILE_SCOPE_P (current_function_decl) |
21b634ae MP |
2655 | && (VAR_OR_FUNCTION_DECL_P (ref) |
2656 | || TREE_CODE (ref) == PARM_DECL)) | |
6a29edea EB |
2657 | { |
2658 | tree context = decl_function_context (ref); | |
2f6e4e97 | 2659 | |
6a29edea EB |
2660 | if (context != 0 && context != current_function_decl) |
2661 | DECL_NONLOCAL (ref) = 1; | |
2662 | } | |
71113fcd GK |
2663 | /* C99 6.7.4p3: An inline definition of a function with external |
2664 | linkage ... shall not contain a reference to an identifier with | |
2665 | internal linkage. */ | |
2666 | else if (current_function_decl != 0 | |
2667 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2668 | && DECL_EXTERNAL (current_function_decl) | |
2669 | && VAR_OR_FUNCTION_DECL_P (ref) | |
0ae9bd27 | 2670 | && (!VAR_P (ref) || TREE_STATIC (ref)) |
1033ffa8 JJ |
2671 | && ! TREE_PUBLIC (ref) |
2672 | && DECL_CONTEXT (ref) != current_function_decl) | |
991d6621 JM |
2673 | record_inline_static (loc, current_function_decl, ref, |
2674 | csi_internal); | |
7e585d16 ZW |
2675 | |
2676 | return ref; | |
2677 | } | |
2678 | ||
bc4b653b JM |
2679 | /* Record details of decls possibly used inside sizeof or typeof. */ |
2680 | struct maybe_used_decl | |
2681 | { | |
2682 | /* The decl. */ | |
2683 | tree decl; | |
2684 | /* The level seen at (in_sizeof + in_typeof). */ | |
2685 | int level; | |
2686 | /* The next one at this level or above, or NULL. */ | |
2687 | struct maybe_used_decl *next; | |
2688 | }; | |
2689 | ||
2690 | static struct maybe_used_decl *maybe_used_decls; | |
2691 | ||
2692 | /* Record that DECL, an undefined static function reference seen | |
2693 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2694 | a VLA type or the operand of typeof is a variably modified | |
2695 | type. */ | |
2696 | ||
4e2fb7de | 2697 | static void |
bc4b653b JM |
2698 | record_maybe_used_decl (tree decl) |
2699 | { | |
2700 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2701 | t->decl = decl; | |
2702 | t->level = in_sizeof + in_typeof; | |
2703 | t->next = maybe_used_decls; | |
2704 | maybe_used_decls = t; | |
2705 | } | |
2706 | ||
2707 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2708 | USED is false, just discard them. If it is true, mark them used | |
2709 | (if no longer inside sizeof or typeof) or move them to the next | |
2710 | level up (if still inside sizeof or typeof). */ | |
2711 | ||
2712 | void | |
2713 | pop_maybe_used (bool used) | |
2714 | { | |
2715 | struct maybe_used_decl *p = maybe_used_decls; | |
2716 | int cur_level = in_sizeof + in_typeof; | |
2717 | while (p && p->level > cur_level) | |
2718 | { | |
2719 | if (used) | |
2720 | { | |
2721 | if (cur_level == 0) | |
2722 | C_DECL_USED (p->decl) = 1; | |
2723 | else | |
2724 | p->level = cur_level; | |
2725 | } | |
2726 | p = p->next; | |
2727 | } | |
2728 | if (!used || cur_level == 0) | |
2729 | maybe_used_decls = p; | |
2730 | } | |
2731 | ||
2732 | /* Return the result of sizeof applied to EXPR. */ | |
2733 | ||
2734 | struct c_expr | |
c2255bc4 | 2735 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) |
bc4b653b JM |
2736 | { |
2737 | struct c_expr ret; | |
ad97f4be JM |
2738 | if (expr.value == error_mark_node) |
2739 | { | |
2740 | ret.value = error_mark_node; | |
2741 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 2742 | ret.original_type = NULL; |
ad97f4be JM |
2743 | pop_maybe_used (false); |
2744 | } | |
2745 | else | |
2746 | { | |
928c19bb | 2747 | bool expr_const_operands = true; |
773ec47f MP |
2748 | |
2749 | if (TREE_CODE (expr.value) == PARM_DECL | |
2750 | && C_ARRAY_PARAMETER (expr.value)) | |
2751 | { | |
2752 | if (warning_at (loc, OPT_Wsizeof_array_argument, | |
2753 | "%<sizeof%> on array function parameter %qE will " | |
2754 | "return size of %qT", expr.value, | |
2755 | expr.original_type)) | |
2756 | inform (DECL_SOURCE_LOCATION (expr.value), "declared here"); | |
2757 | } | |
928c19bb JM |
2758 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, |
2759 | &expr_const_operands); | |
c2255bc4 | 2760 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); |
1a4049e7 JJ |
2761 | c_last_sizeof_arg = expr.value; |
2762 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2763 | ret.original_type = NULL; |
928c19bb | 2764 | if (c_vla_type_p (TREE_TYPE (folded_expr))) |
52ffd86e MS |
2765 | { |
2766 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
928c19bb JM |
2767 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2768 | folded_expr, ret.value); | |
2769 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
c2255bc4 | 2770 | SET_EXPR_LOCATION (ret.value, loc); |
52ffd86e | 2771 | } |
928c19bb | 2772 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); |
ad97f4be | 2773 | } |
bc4b653b JM |
2774 | return ret; |
2775 | } | |
2776 | ||
2777 | /* Return the result of sizeof applied to T, a structure for the type | |
c2255bc4 AH |
2778 | name passed to sizeof (rather than the type itself). LOC is the |
2779 | location of the original expression. */ | |
bc4b653b JM |
2780 | |
2781 | struct c_expr | |
c2255bc4 | 2782 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) |
bc4b653b JM |
2783 | { |
2784 | tree type; | |
2785 | struct c_expr ret; | |
928c19bb JM |
2786 | tree type_expr = NULL_TREE; |
2787 | bool type_expr_const = true; | |
2788 | type = groktypename (t, &type_expr, &type_expr_const); | |
c2255bc4 | 2789 | ret.value = c_sizeof (loc, type); |
1a4049e7 JJ |
2790 | c_last_sizeof_arg = type; |
2791 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2792 | ret.original_type = NULL; |
24070fcb JM |
2793 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) |
2794 | && c_vla_type_p (type)) | |
928c19bb | 2795 | { |
24070fcb JM |
2796 | /* If the type is a [*] array, it is a VLA but is represented as |
2797 | having a size of zero. In such a case we must ensure that | |
2798 | the result of sizeof does not get folded to a constant by | |
2799 | c_fully_fold, because if the size is evaluated the result is | |
2800 | not constant and so constraints on zero or negative size | |
2801 | arrays must not be applied when this sizeof call is inside | |
2802 | another array declarator. */ | |
2803 | if (!type_expr) | |
2804 | type_expr = integer_zero_node; | |
928c19bb JM |
2805 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2806 | type_expr, ret.value); | |
2807 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2808 | } | |
16464cc1 VR |
2809 | pop_maybe_used (type != error_mark_node |
2810 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
bc4b653b JM |
2811 | return ret; |
2812 | } | |
2813 | ||
400fbf9f | 2814 | /* Build a function call to function FUNCTION with parameters PARAMS. |
c2255bc4 | 2815 | The function call is at LOC. |
400fbf9f JW |
2816 | PARAMS is a list--a chain of TREE_LIST nodes--in which the |
2817 | TREE_VALUE of each node is a parameter-expression. | |
2818 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2819 | ||
2820 | tree | |
c2255bc4 | 2821 | build_function_call (location_t loc, tree function, tree params) |
bbbbb16a | 2822 | { |
9771b263 | 2823 | vec<tree, va_gc> *v; |
bbbbb16a ILT |
2824 | tree ret; |
2825 | ||
9771b263 | 2826 | vec_alloc (v, list_length (params)); |
bbbbb16a | 2827 | for (; params; params = TREE_CHAIN (params)) |
9771b263 | 2828 | v->quick_push (TREE_VALUE (params)); |
8edbfaa6 | 2829 | ret = c_build_function_call_vec (loc, vNULL, function, v, NULL); |
9771b263 | 2830 | vec_free (v); |
bbbbb16a ILT |
2831 | return ret; |
2832 | } | |
2833 | ||
ae52741c MLI |
2834 | /* Give a note about the location of the declaration of DECL. */ |
2835 | ||
c7b70a3c MP |
2836 | static void |
2837 | inform_declaration (tree decl) | |
ae52741c | 2838 | { |
c7b70a3c | 2839 | if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl))) |
ae52741c MLI |
2840 | inform (DECL_SOURCE_LOCATION (decl), "declared here"); |
2841 | } | |
2842 | ||
bbbbb16a ILT |
2843 | /* Build a function call to function FUNCTION with parameters PARAMS. |
2844 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2845 | either NULL or the original type of the corresponding element in | |
2846 | PARAMS. The original type may differ from TREE_TYPE of the | |
2847 | parameter for enums. FUNCTION's data type may be a function type | |
2848 | or pointer-to-function. This function changes the elements of | |
2849 | PARAMS. */ | |
2850 | ||
2851 | tree | |
81e5eca8 MP |
2852 | build_function_call_vec (location_t loc, vec<location_t> arg_loc, |
2853 | tree function, vec<tree, va_gc> *params, | |
9771b263 | 2854 | vec<tree, va_gc> *origtypes) |
400fbf9f | 2855 | { |
b3694847 | 2856 | tree fntype, fundecl = 0; |
4977bab6 | 2857 | tree name = NULL_TREE, result; |
c96f4f73 | 2858 | tree tem; |
94a0dd7b SL |
2859 | int nargs; |
2860 | tree *argarray; | |
b8698a0f | 2861 | |
400fbf9f | 2862 | |
fc76e425 | 2863 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 2864 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
2865 | |
2866 | /* Convert anything with function type to a pointer-to-function. */ | |
2867 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2868 | { | |
2869 | name = DECL_NAME (function); | |
0a35513e AH |
2870 | |
2871 | if (flag_tm) | |
2872 | tm_malloc_replacement (function); | |
a5eadacc | 2873 | fundecl = function; |
86951993 AM |
2874 | /* Atomic functions have type checking/casting already done. They are |
2875 | often rewritten and don't match the original parameter list. */ | |
2876 | if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) | |
2877 | origtypes = NULL; | |
36536d79 | 2878 | |
b72271b9 | 2879 | if (flag_cilkplus |
36536d79 BI |
2880 | && is_cilkplus_reduce_builtin (function)) |
2881 | origtypes = NULL; | |
400fbf9f | 2882 | } |
f2a71bbc | 2883 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) |
c2255bc4 | 2884 | function = function_to_pointer_conversion (loc, function); |
400fbf9f | 2885 | |
6e955430 ZL |
2886 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2887 | expressions, like those used for ObjC messenger dispatches. */ | |
9771b263 DN |
2888 | if (params && !params->is_empty ()) |
2889 | function = objc_rewrite_function_call (function, (*params)[0]); | |
6e955430 | 2890 | |
928c19bb JM |
2891 | function = c_fully_fold (function, false, NULL); |
2892 | ||
400fbf9f JW |
2893 | fntype = TREE_TYPE (function); |
2894 | ||
2895 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2896 | return error_mark_node; | |
2897 | ||
2898 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2899 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2900 | { | |
ae52741c MLI |
2901 | if (!flag_diagnostics_show_caret) |
2902 | error_at (loc, | |
2903 | "called object %qE is not a function or function pointer", | |
2904 | function); | |
2905 | else if (DECL_P (function)) | |
2906 | { | |
2907 | error_at (loc, | |
2908 | "called object %qD is not a function or function pointer", | |
2909 | function); | |
2910 | inform_declaration (function); | |
2911 | } | |
2912 | else | |
2913 | error_at (loc, | |
2914 | "called object is not a function or function pointer"); | |
400fbf9f JW |
2915 | return error_mark_node; |
2916 | } | |
2917 | ||
5ce89b2e JM |
2918 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
2919 | current_function_returns_abnormally = 1; | |
2920 | ||
400fbf9f JW |
2921 | /* fntype now gets the type of function pointed to. */ |
2922 | fntype = TREE_TYPE (fntype); | |
2923 | ||
ab4194da JM |
2924 | /* Convert the parameters to the types declared in the |
2925 | function prototype, or apply default promotions. */ | |
2926 | ||
81e5eca8 MP |
2927 | nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params, |
2928 | origtypes, function, fundecl); | |
ab4194da JM |
2929 | if (nargs < 0) |
2930 | return error_mark_node; | |
2931 | ||
c96f4f73 | 2932 | /* Check that the function is called through a compatible prototype. |
fa337f3a | 2933 | If it is not, warn. */ |
1043771b | 2934 | if (CONVERT_EXPR_P (function) |
c96f4f73 EB |
2935 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR |
2936 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
3f75a254 | 2937 | && !comptypes (fntype, TREE_TYPE (tem))) |
c96f4f73 EB |
2938 | { |
2939 | tree return_type = TREE_TYPE (fntype); | |
c96f4f73 EB |
2940 | |
2941 | /* This situation leads to run-time undefined behavior. We can't, | |
2942 | therefore, simply error unless we can prove that all possible | |
2943 | executions of the program must execute the code. */ | |
fa337f3a | 2944 | warning_at (loc, 0, "function called through a non-compatible type"); |
c96f4f73 | 2945 | |
fa337f3a RB |
2946 | if (VOID_TYPE_P (return_type) |
2947 | && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
2948 | pedwarn (loc, 0, | |
2949 | "function with qualified void return type called"); | |
2950 | } | |
c96f4f73 | 2951 | |
9771b263 | 2952 | argarray = vec_safe_address (params); |
bbbbb16a | 2953 | |
83322951 RG |
2954 | /* Check that arguments to builtin functions match the expectations. */ |
2955 | if (fundecl | |
2956 | && DECL_BUILT_IN (fundecl) | |
2957 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2958 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2959 | return error_mark_node; | |
400fbf9f | 2960 | |
83322951 | 2961 | /* Check that the arguments to the function are valid. */ |
dde05067 | 2962 | check_function_arguments (fntype, nargs, argarray); |
400fbf9f | 2963 | |
928c19bb JM |
2964 | if (name != NULL_TREE |
2965 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
bf730f15 | 2966 | { |
928c19bb | 2967 | if (require_constant_value) |
b8698a0f | 2968 | result = |
db3927fb AH |
2969 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), |
2970 | function, nargs, argarray); | |
928c19bb | 2971 | else |
db3927fb AH |
2972 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), |
2973 | function, nargs, argarray); | |
928c19bb JM |
2974 | if (TREE_CODE (result) == NOP_EXPR |
2975 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2976 | STRIP_TYPE_NOPS (result); | |
bf730f15 RS |
2977 | } |
2978 | else | |
db3927fb AH |
2979 | result = build_call_array_loc (loc, TREE_TYPE (fntype), |
2980 | function, nargs, argarray); | |
b0b3afb2 | 2981 | |
71653180 | 2982 | if (VOID_TYPE_P (TREE_TYPE (result))) |
3ce62965 JM |
2983 | { |
2984 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
db3927fb | 2985 | pedwarn (loc, 0, |
3ce62965 JM |
2986 | "function with qualified void return type called"); |
2987 | return result; | |
2988 | } | |
1eb8759b | 2989 | return require_complete_type (result); |
400fbf9f | 2990 | } |
8edbfaa6 JJ |
2991 | |
2992 | /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */ | |
2993 | ||
2994 | tree | |
2995 | c_build_function_call_vec (location_t loc, vec<location_t> arg_loc, | |
2996 | tree function, vec<tree, va_gc> *params, | |
2997 | vec<tree, va_gc> *origtypes) | |
2998 | { | |
2999 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
3000 | STRIP_TYPE_NOPS (function); | |
3001 | ||
3002 | /* Convert anything with function type to a pointer-to-function. */ | |
3003 | if (TREE_CODE (function) == FUNCTION_DECL) | |
3004 | { | |
3005 | /* Implement type-directed function overloading for builtins. | |
3006 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
3007 | handle all the type checking. The result is a complete expression | |
3008 | that implements this function call. */ | |
3009 | tree tem = resolve_overloaded_builtin (loc, function, params); | |
3010 | if (tem) | |
3011 | return tem; | |
3012 | } | |
3013 | return build_function_call_vec (loc, arg_loc, function, params, origtypes); | |
3014 | } | |
400fbf9f | 3015 | \f |
bbbbb16a ILT |
3016 | /* Convert the argument expressions in the vector VALUES |
3017 | to the types in the list TYPELIST. | |
400fbf9f JW |
3018 | |
3019 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
3020 | perform the default conversions. | |
3021 | ||
bbbbb16a ILT |
3022 | ORIGTYPES is the original types of the expressions in VALUES. This |
3023 | holds the type of enum values which have been converted to integral | |
3024 | types. It may be NULL. | |
400fbf9f | 3025 | |
03dafa61 JM |
3026 | FUNCTION is a tree for the called function. It is used only for |
3027 | error messages, where it is formatted with %qE. | |
400fbf9f JW |
3028 | |
3029 | This is also where warnings about wrong number of args are generated. | |
3030 | ||
81e5eca8 MP |
3031 | ARG_LOC are locations of function arguments (if any). |
3032 | ||
94a0dd7b | 3033 | Returns the actual number of arguments processed (which may be less |
bbbbb16a ILT |
3034 | than the length of VALUES in some error situations), or -1 on |
3035 | failure. */ | |
94a0dd7b SL |
3036 | |
3037 | static int | |
81e5eca8 MP |
3038 | convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist, |
3039 | vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes, | |
3040 | tree function, tree fundecl) | |
400fbf9f | 3041 | { |
bbbbb16a ILT |
3042 | tree typetail, val; |
3043 | unsigned int parmnum; | |
06302a02 | 3044 | bool error_args = false; |
b5d32c25 | 3045 | const bool type_generic = fundecl |
81e5eca8 | 3046 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl))); |
8ce94e44 | 3047 | bool type_generic_remove_excess_precision = false; |
03dafa61 | 3048 | tree selector; |
03dafa61 | 3049 | |
2ac2f164 JM |
3050 | /* Change pointer to function to the function itself for |
3051 | diagnostics. */ | |
03dafa61 JM |
3052 | if (TREE_CODE (function) == ADDR_EXPR |
3053 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
2ac2f164 | 3054 | function = TREE_OPERAND (function, 0); |
03dafa61 JM |
3055 | |
3056 | /* Handle an ObjC selector specially for diagnostics. */ | |
3057 | selector = objc_message_selector (); | |
400fbf9f | 3058 | |
8ce94e44 JM |
3059 | /* For type-generic built-in functions, determine whether excess |
3060 | precision should be removed (classification) or not | |
3061 | (comparison). */ | |
3062 | if (type_generic | |
3063 | && DECL_BUILT_IN (fundecl) | |
3064 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
3065 | { | |
3066 | switch (DECL_FUNCTION_CODE (fundecl)) | |
3067 | { | |
3068 | case BUILT_IN_ISFINITE: | |
3069 | case BUILT_IN_ISINF: | |
3070 | case BUILT_IN_ISINF_SIGN: | |
3071 | case BUILT_IN_ISNAN: | |
3072 | case BUILT_IN_ISNORMAL: | |
3073 | case BUILT_IN_FPCLASSIFY: | |
3074 | type_generic_remove_excess_precision = true; | |
3075 | break; | |
3076 | ||
3077 | default: | |
3078 | type_generic_remove_excess_precision = false; | |
3079 | break; | |
3080 | } | |
3081 | } | |
b72271b9 | 3082 | if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl)) |
6d6efbb3 | 3083 | return vec_safe_length (values); |
8ce94e44 | 3084 | |
400fbf9f | 3085 | /* Scan the given expressions and types, producing individual |
bbbbb16a | 3086 | converted arguments. */ |
400fbf9f | 3087 | |
bbbbb16a | 3088 | for (typetail = typelist, parmnum = 0; |
9771b263 | 3089 | values && values->iterate (parmnum, &val); |
bbbbb16a | 3090 | ++parmnum) |
400fbf9f | 3091 | { |
b3694847 | 3092 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
8ce94e44 | 3093 | tree valtype = TREE_TYPE (val); |
03dafa61 JM |
3094 | tree rname = function; |
3095 | int argnum = parmnum + 1; | |
4d3e6fae | 3096 | const char *invalid_func_diag; |
8ce94e44 | 3097 | bool excess_precision = false; |
928c19bb | 3098 | bool npc; |
bbbbb16a | 3099 | tree parmval; |
5c1bc275 MP |
3100 | /* Some __atomic_* builtins have additional hidden argument at |
3101 | position 0. */ | |
3102 | location_t ploc | |
3103 | = !arg_loc.is_empty () && values->length () == arg_loc.length () | |
3104 | ? expansion_point_location_if_in_system_header (arg_loc[parmnum]) | |
3105 | : input_location; | |
400fbf9f JW |
3106 | |
3107 | if (type == void_type_node) | |
3108 | { | |
19dc6d01 | 3109 | if (selector) |
68fca595 | 3110 | error_at (loc, "too many arguments to method %qE", selector); |
19dc6d01 | 3111 | else |
68fca595 | 3112 | error_at (loc, "too many arguments to function %qE", function); |
ae52741c | 3113 | inform_declaration (fundecl); |
d38f7dce | 3114 | return error_args ? -1 : (int) parmnum; |
400fbf9f JW |
3115 | } |
3116 | ||
03dafa61 JM |
3117 | if (selector && argnum > 2) |
3118 | { | |
3119 | rname = selector; | |
3120 | argnum -= 2; | |
3121 | } | |
3122 | ||
928c19bb | 3123 | npc = null_pointer_constant_p (val); |
8ce94e44 JM |
3124 | |
3125 | /* If there is excess precision and a prototype, convert once to | |
3126 | the required type rather than converting via the semantic | |
3127 | type. Likewise without a prototype a float value represented | |
3128 | as long double should be converted once to double. But for | |
3129 | type-generic classification functions excess precision must | |
3130 | be removed here. */ | |
3131 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
3132 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
3133 | { | |
3134 | val = TREE_OPERAND (val, 0); | |
3135 | excess_precision = true; | |
3136 | } | |
928c19bb | 3137 | val = c_fully_fold (val, false, NULL); |
ed248cf7 | 3138 | STRIP_TYPE_NOPS (val); |
400fbf9f | 3139 | |
400fbf9f JW |
3140 | val = require_complete_type (val); |
3141 | ||
3142 | if (type != 0) | |
3143 | { | |
3144 | /* Formal parm type is specified by a function prototype. */ | |
400fbf9f | 3145 | |
20913689 | 3146 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) |
400fbf9f | 3147 | { |
5c1bc275 MP |
3148 | error_at (ploc, "type of formal parameter %d is incomplete", |
3149 | parmnum + 1); | |
400fbf9f JW |
3150 | parmval = val; |
3151 | } | |
3152 | else | |
3153 | { | |
bbbbb16a ILT |
3154 | tree origtype; |
3155 | ||
d45cf215 RS |
3156 | /* Optionally warn about conversions that |
3157 | differ from the default conversions. */ | |
05170031 | 3158 | if (warn_traditional_conversion || warn_traditional) |
400fbf9f | 3159 | { |
e3a64162 | 3160 | unsigned int formal_prec = TYPE_PRECISION (type); |
400fbf9f | 3161 | |
aae43c5f | 3162 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3163 | && TREE_CODE (valtype) == REAL_TYPE) |
5c1bc275 MP |
3164 | warning_at (ploc, OPT_Wtraditional_conversion, |
3165 | "passing argument %d of %qE as integer rather " | |
3166 | "than floating due to prototype", | |
3167 | argnum, rname); | |
03829ad2 | 3168 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3169 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
5c1bc275 MP |
3170 | warning_at (ploc, OPT_Wtraditional_conversion, |
3171 | "passing argument %d of %qE as integer rather " | |
3172 | "than complex due to prototype", | |
3173 | argnum, rname); | |
aae43c5f | 3174 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3175 | && TREE_CODE (valtype) == REAL_TYPE) |
5c1bc275 MP |
3176 | warning_at (ploc, OPT_Wtraditional_conversion, |
3177 | "passing argument %d of %qE as complex rather " | |
3178 | "than floating due to prototype", | |
3179 | argnum, rname); | |
400fbf9f | 3180 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3181 | && INTEGRAL_TYPE_P (valtype)) |
5c1bc275 MP |
3182 | warning_at (ploc, OPT_Wtraditional_conversion, |
3183 | "passing argument %d of %qE as floating rather " | |
3184 | "than integer due to prototype", | |
3185 | argnum, rname); | |
03829ad2 | 3186 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3187 | && INTEGRAL_TYPE_P (valtype)) |
5c1bc275 MP |
3188 | warning_at (ploc, OPT_Wtraditional_conversion, |
3189 | "passing argument %d of %qE as complex rather " | |
3190 | "than integer due to prototype", | |
3191 | argnum, rname); | |
aae43c5f | 3192 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3193 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
5c1bc275 MP |
3194 | warning_at (ploc, OPT_Wtraditional_conversion, |
3195 | "passing argument %d of %qE as floating rather " | |
3196 | "than complex due to prototype", | |
3197 | argnum, rname); | |
aae43c5f RK |
3198 | /* ??? At some point, messages should be written about |
3199 | conversions between complex types, but that's too messy | |
3200 | to do now. */ | |
d45cf215 | 3201 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3202 | && TREE_CODE (valtype) == REAL_TYPE) |
d45cf215 RS |
3203 | { |
3204 | /* Warn if any argument is passed as `float', | |
047de90b | 3205 | since without a prototype it would be `double'. */ |
9a8ce21f JG |
3206 | if (formal_prec == TYPE_PRECISION (float_type_node) |
3207 | && type != dfloat32_type_node) | |
5c1bc275 MP |
3208 | warning_at (ploc, 0, |
3209 | "passing argument %d of %qE as %<float%> " | |
3210 | "rather than %<double%> due to prototype", | |
3211 | argnum, rname); | |
9a8ce21f JG |
3212 | |
3213 | /* Warn if mismatch between argument and prototype | |
3214 | for decimal float types. Warn of conversions with | |
3215 | binary float types and of precision narrowing due to | |
3216 | prototype. */ | |
8ce94e44 | 3217 | else if (type != valtype |
9a8ce21f JG |
3218 | && (type == dfloat32_type_node |
3219 | || type == dfloat64_type_node | |
c22cacf3 | 3220 | || type == dfloat128_type_node |
8ce94e44 JM |
3221 | || valtype == dfloat32_type_node |
3222 | || valtype == dfloat64_type_node | |
3223 | || valtype == dfloat128_type_node) | |
c22cacf3 | 3224 | && (formal_prec |
8ce94e44 | 3225 | <= TYPE_PRECISION (valtype) |
9a8ce21f | 3226 | || (type == dfloat128_type_node |
8ce94e44 | 3227 | && (valtype |
c22cacf3 | 3228 | != dfloat64_type_node |
8ce94e44 | 3229 | && (valtype |
9a8ce21f JG |
3230 | != dfloat32_type_node))) |
3231 | || (type == dfloat64_type_node | |
8ce94e44 | 3232 | && (valtype |
9a8ce21f | 3233 | != dfloat32_type_node)))) |
5c1bc275 MP |
3234 | warning_at (ploc, 0, |
3235 | "passing argument %d of %qE as %qT " | |
3236 | "rather than %qT due to prototype", | |
3237 | argnum, rname, type, valtype); | |
9a8ce21f | 3238 | |
d45cf215 | 3239 | } |
3ed56f8a KG |
3240 | /* Detect integer changing in width or signedness. |
3241 | These warnings are only activated with | |
05170031 MLI |
3242 | -Wtraditional-conversion, not with -Wtraditional. */ |
3243 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
8ce94e44 | 3244 | && INTEGRAL_TYPE_P (valtype)) |
400fbf9f | 3245 | { |
d45cf215 RS |
3246 | tree would_have_been = default_conversion (val); |
3247 | tree type1 = TREE_TYPE (would_have_been); | |
3248 | ||
754a4d82 | 3249 | if (TREE_CODE (type) == ENUMERAL_TYPE |
a38b987a | 3250 | && (TYPE_MAIN_VARIANT (type) |
8ce94e44 | 3251 | == TYPE_MAIN_VARIANT (valtype))) |
754a4d82 RS |
3252 | /* No warning if function asks for enum |
3253 | and the actual arg is that enum type. */ | |
3254 | ; | |
3255 | else if (formal_prec != TYPE_PRECISION (type1)) | |
5c1bc275 MP |
3256 | warning_at (ploc, OPT_Wtraditional_conversion, |
3257 | "passing argument %d of %qE " | |
3258 | "with different width due to prototype", | |
3259 | argnum, rname); | |
8df83eae | 3260 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
d45cf215 | 3261 | ; |
800cd3b9 RS |
3262 | /* Don't complain if the formal parameter type |
3263 | is an enum, because we can't tell now whether | |
3264 | the value was an enum--even the same enum. */ | |
3265 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
3266 | ; | |
400fbf9f JW |
3267 | else if (TREE_CODE (val) == INTEGER_CST |
3268 | && int_fits_type_p (val, type)) | |
3269 | /* Change in signedness doesn't matter | |
3270 | if a constant value is unaffected. */ | |
3271 | ; | |
ce9895ae RS |
3272 | /* If the value is extended from a narrower |
3273 | unsigned type, it doesn't matter whether we | |
3274 | pass it as signed or unsigned; the value | |
3275 | certainly is the same either way. */ | |
8ce94e44 JM |
3276 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) |
3277 | && TYPE_UNSIGNED (valtype)) | |
ce9895ae | 3278 | ; |
8df83eae | 3279 | else if (TYPE_UNSIGNED (type)) |
5c1bc275 MP |
3280 | warning_at (ploc, OPT_Wtraditional_conversion, |
3281 | "passing argument %d of %qE " | |
3282 | "as unsigned due to prototype", | |
3283 | argnum, rname); | |
3ed56f8a | 3284 | else |
5c1bc275 MP |
3285 | warning_at (ploc, OPT_Wtraditional_conversion, |
3286 | "passing argument %d of %qE " | |
3287 | "as signed due to prototype", | |
3288 | argnum, rname); | |
400fbf9f JW |
3289 | } |
3290 | } | |
3291 | ||
8ce94e44 JM |
3292 | /* Possibly restore an EXCESS_PRECISION_EXPR for the |
3293 | sake of better warnings from convert_and_check. */ | |
3294 | if (excess_precision) | |
3295 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
9771b263 | 3296 | origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; |
5c1bc275 | 3297 | parmval = convert_for_assignment (loc, ploc, type, |
68fca595 MP |
3298 | val, origtype, ic_argpass, |
3299 | npc, fundecl, function, | |
2ac2f164 | 3300 | parmnum + 1); |
2f6e4e97 | 3301 | |
61f71b34 | 3302 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
b6d6aa84 | 3303 | && INTEGRAL_TYPE_P (type) |
400fbf9f JW |
3304 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
3305 | parmval = default_conversion (parmval); | |
400fbf9f | 3306 | } |
400fbf9f | 3307 | } |
8ce94e44 JM |
3308 | else if (TREE_CODE (valtype) == REAL_TYPE |
3309 | && (TYPE_PRECISION (valtype) | |
2531a1d9 | 3310 | <= TYPE_PRECISION (double_type_node)) |
0fdd4508 JR |
3311 | && TYPE_MAIN_VARIANT (valtype) != double_type_node |
3312 | && TYPE_MAIN_VARIANT (valtype) != long_double_type_node | |
8ce94e44 | 3313 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) |
b5d32c25 KG |
3314 | { |
3315 | if (type_generic) | |
bbbbb16a | 3316 | parmval = val; |
b5d32c25 | 3317 | else |
0a0b3574 MM |
3318 | { |
3319 | /* Convert `float' to `double'. */ | |
3320 | if (warn_double_promotion && !c_inhibit_evaluation_warnings) | |
5c1bc275 MP |
3321 | warning_at (ploc, OPT_Wdouble_promotion, |
3322 | "implicit conversion from %qT to %qT when passing " | |
3323 | "argument to function", | |
3324 | valtype, double_type_node); | |
0a0b3574 MM |
3325 | parmval = convert (double_type_node, val); |
3326 | } | |
b5d32c25 | 3327 | } |
8ce94e44 JM |
3328 | else if (excess_precision && !type_generic) |
3329 | /* A "double" argument with excess precision being passed | |
3330 | without a prototype or in variable arguments. */ | |
bbbbb16a | 3331 | parmval = convert (valtype, val); |
c22cacf3 MS |
3332 | else if ((invalid_func_diag = |
3333 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
4d3e6fae FJ |
3334 | { |
3335 | error (invalid_func_diag); | |
94a0dd7b | 3336 | return -1; |
4d3e6fae | 3337 | } |
1807ffc1 MS |
3338 | else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val)) |
3339 | { | |
3340 | return -1; | |
3341 | } | |
400fbf9f JW |
3342 | else |
3343 | /* Convert `short' and `char' to full-size `int'. */ | |
bbbbb16a ILT |
3344 | parmval = default_conversion (val); |
3345 | ||
9771b263 | 3346 | (*values)[parmnum] = parmval; |
06302a02 JJ |
3347 | if (parmval == error_mark_node) |
3348 | error_args = true; | |
400fbf9f JW |
3349 | |
3350 | if (typetail) | |
3351 | typetail = TREE_CHAIN (typetail); | |
3352 | } | |
3353 | ||
9771b263 | 3354 | gcc_assert (parmnum == vec_safe_length (values)); |
94a0dd7b | 3355 | |
400fbf9f | 3356 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) |
3789b316 | 3357 | { |
68fca595 | 3358 | error_at (loc, "too few arguments to function %qE", function); |
6d6efbb3 BI |
3359 | inform_declaration (fundecl); |
3360 | return -1; | |
3789b316 | 3361 | } |
400fbf9f | 3362 | |
06302a02 | 3363 | return error_args ? -1 : (int) parmnum; |
400fbf9f JW |
3364 | } |
3365 | \f | |
43f6dfd3 RS |
3366 | /* This is the entry point used by the parser to build unary operators |
3367 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3368 | ARG is the operand. For unary plus, the C parser currently uses | |
6a3799eb AH |
3369 | CONVERT_EXPR for code. |
3370 | ||
3371 | LOC is the location to use for the tree generated. | |
3372 | */ | |
43f6dfd3 RS |
3373 | |
3374 | struct c_expr | |
c2255bc4 | 3375 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) |
43f6dfd3 RS |
3376 | { |
3377 | struct c_expr result; | |
3378 | ||
100d537d | 3379 | result.original_code = code; |
6866c6e8 ILT |
3380 | result.original_type = NULL; |
3381 | ||
1807ffc1 MS |
3382 | if (reject_gcc_builtin (arg.value)) |
3383 | { | |
3384 | result.value = error_mark_node; | |
3385 | } | |
3386 | else | |
3387 | { | |
3388 | result.value = build_unary_op (loc, code, arg.value, 0); | |
3389 | ||
59c0753d | 3390 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) |
c2255bc4 | 3391 | overflow_warning (loc, result.value); |
1807ffc1 | 3392 | } |
59c0753d | 3393 | |
43f6dfd3 RS |
3394 | return result; |
3395 | } | |
3396 | ||
3397 | /* This is the entry point used by the parser to build binary operators | |
3398 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3399 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3400 | expression, we check for operands that were written with other binary | |
ba47d38d AH |
3401 | operators in a way that is likely to confuse the user. |
3402 | ||
3403 | LOCATION is the location of the binary operator. */ | |
edc7c4ec | 3404 | |
487a92fe | 3405 | struct c_expr |
ba47d38d AH |
3406 | parser_build_binary_op (location_t location, enum tree_code code, |
3407 | struct c_expr arg1, struct c_expr arg2) | |
400fbf9f | 3408 | { |
487a92fe | 3409 | struct c_expr result; |
400fbf9f | 3410 | |
487a92fe JM |
3411 | enum tree_code code1 = arg1.original_code; |
3412 | enum tree_code code2 = arg2.original_code; | |
6866c6e8 ILT |
3413 | tree type1 = (arg1.original_type |
3414 | ? arg1.original_type | |
3415 | : TREE_TYPE (arg1.value)); | |
3416 | tree type2 = (arg2.original_type | |
3417 | ? arg2.original_type | |
3418 | : TREE_TYPE (arg2.value)); | |
400fbf9f | 3419 | |
ba47d38d AH |
3420 | result.value = build_binary_op (location, code, |
3421 | arg1.value, arg2.value, 1); | |
487a92fe | 3422 | result.original_code = code; |
6866c6e8 | 3423 | result.original_type = NULL; |
58bf601b | 3424 | |
487a92fe JM |
3425 | if (TREE_CODE (result.value) == ERROR_MARK) |
3426 | return result; | |
400fbf9f | 3427 | |
ba47d38d AH |
3428 | if (location != UNKNOWN_LOCATION) |
3429 | protected_set_expr_location (result.value, location); | |
3430 | ||
400fbf9f | 3431 | /* Check for cases such as x+y<<z which users are likely |
487a92fe | 3432 | to misinterpret. */ |
400fbf9f | 3433 | if (warn_parentheses) |
393e8e8b MP |
3434 | warn_about_parentheses (location, code, code1, arg1.value, code2, |
3435 | arg2.value); | |
001af587 | 3436 | |
ca409efd | 3437 | if (warn_logical_op) |
393e8e8b | 3438 | warn_logical_operator (location, code, TREE_TYPE (result.value), |
ca409efd | 3439 | code1, arg1.value, code2, arg2.value); |
63a08740 | 3440 | |
05b28fd6 MP |
3441 | if (warn_tautological_compare) |
3442 | warn_tautological_cmp (location, code, arg1.value, arg2.value); | |
3443 | ||
742938c9 | 3444 | if (warn_logical_not_paren |
7ccb1a11 | 3445 | && TREE_CODE_CLASS (code) == tcc_comparison |
47c2554f | 3446 | && code1 == TRUTH_NOT_EXPR |
01177669 JJ |
3447 | && code2 != TRUTH_NOT_EXPR |
3448 | /* Avoid warning for !!x == y. */ | |
3449 | && (TREE_CODE (arg1.value) != NE_EXPR | |
3450 | || !integer_zerop (TREE_OPERAND (arg1.value, 1)))) | |
3451 | { | |
3452 | /* Avoid warning for !b == y where b has _Bool type. */ | |
3453 | tree t = integer_zero_node; | |
3454 | if (TREE_CODE (arg1.value) == EQ_EXPR | |
3455 | && integer_zerop (TREE_OPERAND (arg1.value, 1)) | |
3456 | && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node) | |
3457 | { | |
3458 | t = TREE_OPERAND (arg1.value, 0); | |
3459 | do | |
3460 | { | |
3461 | if (TREE_TYPE (t) != integer_type_node) | |
3462 | break; | |
3463 | if (TREE_CODE (t) == C_MAYBE_CONST_EXPR) | |
3464 | t = C_MAYBE_CONST_EXPR_EXPR (t); | |
3465 | else if (CONVERT_EXPR_P (t)) | |
3466 | t = TREE_OPERAND (t, 0); | |
3467 | else | |
3468 | break; | |
3469 | } | |
3470 | while (1); | |
3471 | } | |
3472 | if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE) | |
3473 | warn_logical_not_parentheses (location, code, arg2.value); | |
3474 | } | |
742938c9 | 3475 | |
e994a705 RS |
3476 | /* Warn about comparisons against string literals, with the exception |
3477 | of testing for equality or inequality of a string literal with NULL. */ | |
3478 | if (code == EQ_EXPR || code == NE_EXPR) | |
3479 | { | |
3480 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3481 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
c2255bc4 AH |
3482 | warning_at (location, OPT_Waddress, |
3483 | "comparison with string literal results in unspecified behavior"); | |
e994a705 RS |
3484 | } |
3485 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3486 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
c2255bc4 AH |
3487 | warning_at (location, OPT_Waddress, |
3488 | "comparison with string literal results in unspecified behavior"); | |
e994a705 | 3489 | |
b8698a0f L |
3490 | if (TREE_OVERFLOW_P (result.value) |
3491 | && !TREE_OVERFLOW_P (arg1.value) | |
59c0753d | 3492 | && !TREE_OVERFLOW_P (arg2.value)) |
c2255bc4 | 3493 | overflow_warning (location, result.value); |
400fbf9f | 3494 | |
6866c6e8 ILT |
3495 | /* Warn about comparisons of different enum types. */ |
3496 | if (warn_enum_compare | |
3497 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3498 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3499 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3500 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3501 | warning_at (location, OPT_Wenum_compare, | |
3502 | "comparison between %qT and %qT", | |
3503 | type1, type2); | |
3504 | ||
400fbf9f JW |
3505 | return result; |
3506 | } | |
3e4093b6 | 3507 | \f |
3e4093b6 RS |
3508 | /* Return a tree for the difference of pointers OP0 and OP1. |
3509 | The resulting tree has type int. */ | |
293c9fdd | 3510 | |
3e4093b6 | 3511 | static tree |
db3927fb | 3512 | pointer_diff (location_t loc, tree op0, tree op1) |
3e4093b6 | 3513 | { |
3e4093b6 | 3514 | tree restype = ptrdiff_type_node; |
36c5e70a | 3515 | tree result, inttype; |
400fbf9f | 3516 | |
36c5e70a BE |
3517 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); |
3518 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
3e4093b6 | 3519 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
3e4093b6 | 3520 | tree orig_op1 = op1; |
400fbf9f | 3521 | |
36c5e70a BE |
3522 | /* If the operands point into different address spaces, we need to |
3523 | explicitly convert them to pointers into the common address space | |
3524 | before we can subtract the numerical address values. */ | |
3525 | if (as0 != as1) | |
3526 | { | |
3527 | addr_space_t as_common; | |
3528 | tree common_type; | |
3529 | ||
3530 | /* Determine the common superset address space. This is guaranteed | |
3531 | to exist because the caller verified that comp_target_types | |
3532 | returned non-zero. */ | |
3533 | if (!addr_space_superset (as0, as1, &as_common)) | |
3534 | gcc_unreachable (); | |
3535 | ||
3536 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3537 | op0 = convert (common_type, op0); | |
3538 | op1 = convert (common_type, op1); | |
3539 | } | |
3540 | ||
3541 | /* Determine integer type to perform computations in. This will usually | |
3542 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3543 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3544 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
647d4b75 | 3545 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); |
36c5e70a BE |
3546 | else |
3547 | inttype = restype; | |
3548 | ||
fcf73884 | 3549 | if (TREE_CODE (target_type) == VOID_TYPE) |
44d90fe1 | 3550 | pedwarn (loc, OPT_Wpointer_arith, |
fcf73884 MLI |
3551 | "pointer of type %<void *%> used in subtraction"); |
3552 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
44d90fe1 | 3553 | pedwarn (loc, OPT_Wpointer_arith, |
fcf73884 | 3554 | "pointer to a function used in subtraction"); |
400fbf9f | 3555 | |
3e4093b6 RS |
3556 | /* First do the subtraction as integers; |
3557 | then drop through to build the divide operator. | |
3558 | Do not do default conversions on the minus operator | |
3559 | in case restype is a short type. */ | |
400fbf9f | 3560 | |
db3927fb | 3561 | op0 = build_binary_op (loc, |
36c5e70a BE |
3562 | MINUS_EXPR, convert (inttype, op0), |
3563 | convert (inttype, op1), 0); | |
3e4093b6 RS |
3564 | /* This generates an error if op1 is pointer to incomplete type. */ |
3565 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
db3927fb | 3566 | error_at (loc, "arithmetic on pointer to an incomplete type"); |
400fbf9f | 3567 | |
3e4093b6 | 3568 | op1 = c_size_in_bytes (target_type); |
400fbf9f | 3569 | |
f04dda30 MP |
3570 | if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1))) |
3571 | error_at (loc, "arithmetic on pointer to an empty aggregate"); | |
3572 | ||
3e4093b6 | 3573 | /* Divide by the size, in easiest possible way. */ |
36c5e70a BE |
3574 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, |
3575 | op0, convert (inttype, op1)); | |
3576 | ||
3577 | /* Convert to final result type if necessary. */ | |
3578 | return convert (restype, result); | |
3e4093b6 RS |
3579 | } |
3580 | \f | |
267bac10 JM |
3581 | /* Expand atomic compound assignments into an approriate sequence as |
3582 | specified by the C11 standard section 6.5.16.2. | |
3583 | given | |
3584 | _Atomic T1 E1 | |
3585 | T2 E2 | |
3586 | E1 op= E2 | |
3587 | ||
3588 | This sequence is used for all types for which these operations are | |
3589 | supported. | |
3590 | ||
3591 | In addition, built-in versions of the 'fe' prefixed routines may | |
3592 | need to be invoked for floating point (real, complex or vector) when | |
3593 | floating-point exceptions are supported. See 6.5.16.2 footnote 113. | |
3594 | ||
3595 | T1 newval; | |
3596 | T1 old; | |
3597 | T1 *addr | |
3598 | T2 val | |
3599 | fenv_t fenv | |
3600 | ||
3601 | addr = &E1; | |
3602 | val = (E2); | |
3603 | __atomic_load (addr, &old, SEQ_CST); | |
3604 | feholdexcept (&fenv); | |
3605 | loop: | |
3606 | newval = old op val; | |
3607 | if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST, | |
3608 | SEQ_CST)) | |
3609 | goto done; | |
3610 | feclearexcept (FE_ALL_EXCEPT); | |
3611 | goto loop: | |
3612 | done: | |
3613 | feupdateenv (&fenv); | |
3614 | ||
3615 | Also note that the compiler is simply issuing the generic form of | |
3616 | the atomic operations. This requires temp(s) and has their address | |
3617 | taken. The atomic processing is smart enough to figure out when the | |
3618 | size of an object can utilize a lock-free version, and convert the | |
3619 | built-in call to the appropriate lock-free routine. The optimizers | |
3620 | will then dispose of any temps that are no longer required, and | |
3621 | lock-free implementations are utilized as long as there is target | |
3622 | support for the required size. | |
3623 | ||
3624 | If the operator is NOP_EXPR, then this is a simple assignment, and | |
3625 | an __atomic_store is issued to perform the assignment rather than | |
3626 | the above loop. | |
3627 | ||
3628 | */ | |
3629 | ||
3630 | /* Build an atomic assignment at LOC, expanding into the proper | |
3631 | sequence to store LHS MODIFYCODE= RHS. Return a value representing | |
3632 | the result of the operation, unless RETURN_OLD_P in which case | |
3633 | return the old value of LHS (this is only for postincrement and | |
3634 | postdecrement). */ | |
3635 | static tree | |
3636 | build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode, | |
3637 | tree rhs, bool return_old_p) | |
3638 | { | |
3639 | tree fndecl, func_call; | |
3640 | vec<tree, va_gc> *params; | |
3641 | tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr; | |
3642 | tree old, old_addr; | |
3643 | tree compound_stmt; | |
3644 | tree stmt, goto_stmt; | |
3645 | tree loop_label, loop_decl, done_label, done_decl; | |
3646 | ||
3647 | tree lhs_type = TREE_TYPE (lhs); | |
3648 | tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0); | |
3649 | tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); | |
3650 | tree rhs_type = TREE_TYPE (rhs); | |
3651 | ||
3652 | gcc_assert (TYPE_ATOMIC (lhs_type)); | |
3653 | ||
3654 | if (return_old_p) | |
3655 | gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR); | |
3656 | ||
3657 | /* Allocate enough vector items for a compare_exchange. */ | |
3658 | vec_alloc (params, 6); | |
3659 | ||
3660 | /* Create a compound statement to hold the sequence of statements | |
3661 | with a loop. */ | |
3662 | compound_stmt = c_begin_compound_stmt (false); | |
3663 | ||
3664 | /* Fold the RHS if it hasn't already been folded. */ | |
3665 | if (modifycode != NOP_EXPR) | |
3666 | rhs = c_fully_fold (rhs, false, NULL); | |
3667 | ||
3668 | /* Remove the qualifiers for the rest of the expressions and create | |
3669 | the VAL temp variable to hold the RHS. */ | |
3670 | nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED); | |
3671 | nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED); | |
5c4abbb8 | 3672 | val = create_tmp_var_raw (nonatomic_rhs_type); |
267bac10 | 3673 | TREE_ADDRESSABLE (val) = 1; |
cc28fc7f | 3674 | TREE_NO_WARNING (val) = 1; |
5c4abbb8 MP |
3675 | rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE, |
3676 | NULL_TREE); | |
267bac10 JM |
3677 | SET_EXPR_LOCATION (rhs, loc); |
3678 | add_stmt (rhs); | |
3679 | ||
3680 | /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue | |
3681 | an atomic_store. */ | |
3682 | if (modifycode == NOP_EXPR) | |
3683 | { | |
3684 | /* Build __atomic_store (&lhs, &val, SEQ_CST) */ | |
3685 | rhs = build_unary_op (loc, ADDR_EXPR, val, 0); | |
3686 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE); | |
3687 | params->quick_push (lhs_addr); | |
3688 | params->quick_push (rhs); | |
3689 | params->quick_push (seq_cst); | |
8edbfaa6 | 3690 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 JM |
3691 | add_stmt (func_call); |
3692 | ||
3693 | /* Finish the compound statement. */ | |
3694 | compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); | |
3695 | ||
3696 | /* VAL is the value which was stored, return a COMPOUND_STMT of | |
3697 | the statement and that value. */ | |
3698 | return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val); | |
3699 | } | |
3700 | ||
3701 | /* Create the variables and labels required for the op= form. */ | |
5c4abbb8 | 3702 | old = create_tmp_var_raw (nonatomic_lhs_type); |
267bac10 | 3703 | old_addr = build_unary_op (loc, ADDR_EXPR, old, 0); |
cc28fc7f MP |
3704 | TREE_ADDRESSABLE (old) = 1; |
3705 | TREE_NO_WARNING (old) = 1; | |
267bac10 | 3706 | |
5c4abbb8 | 3707 | newval = create_tmp_var_raw (nonatomic_lhs_type); |
267bac10 JM |
3708 | newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0); |
3709 | TREE_ADDRESSABLE (newval) = 1; | |
3710 | ||
3711 | loop_decl = create_artificial_label (loc); | |
3712 | loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl); | |
3713 | ||
3714 | done_decl = create_artificial_label (loc); | |
3715 | done_label = build1 (LABEL_EXPR, void_type_node, done_decl); | |
3716 | ||
3717 | /* __atomic_load (addr, &old, SEQ_CST). */ | |
3718 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); | |
3719 | params->quick_push (lhs_addr); | |
3720 | params->quick_push (old_addr); | |
3721 | params->quick_push (seq_cst); | |
8edbfaa6 | 3722 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
5c4abbb8 MP |
3723 | old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE, |
3724 | NULL_TREE); | |
3725 | add_stmt (old); | |
267bac10 JM |
3726 | params->truncate (0); |
3727 | ||
3728 | /* Create the expressions for floating-point environment | |
3729 | manipulation, if required. */ | |
3730 | bool need_fenv = (flag_trapping_math | |
3731 | && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type))); | |
3732 | tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE; | |
3733 | if (need_fenv) | |
3734 | targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call); | |
3735 | ||
3736 | if (hold_call) | |
3737 | add_stmt (hold_call); | |
3738 | ||
3739 | /* loop: */ | |
3740 | add_stmt (loop_label); | |
3741 | ||
3742 | /* newval = old + val; */ | |
3743 | rhs = build_binary_op (loc, modifycode, old, val, 1); | |
5c4abbb8 | 3744 | rhs = c_fully_fold (rhs, false, NULL); |
68fca595 MP |
3745 | rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type, |
3746 | rhs, NULL_TREE, ic_assign, false, NULL_TREE, | |
267bac10 JM |
3747 | NULL_TREE, 0); |
3748 | if (rhs != error_mark_node) | |
3749 | { | |
5c4abbb8 MP |
3750 | rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE, |
3751 | NULL_TREE); | |
267bac10 JM |
3752 | SET_EXPR_LOCATION (rhs, loc); |
3753 | add_stmt (rhs); | |
3754 | } | |
3755 | ||
3756 | /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST)) | |
3757 | goto done; */ | |
3758 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE); | |
3759 | params->quick_push (lhs_addr); | |
3760 | params->quick_push (old_addr); | |
3761 | params->quick_push (newval_addr); | |
3762 | params->quick_push (integer_zero_node); | |
3763 | params->quick_push (seq_cst); | |
3764 | params->quick_push (seq_cst); | |
8edbfaa6 | 3765 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 JM |
3766 | |
3767 | goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl); | |
3768 | SET_EXPR_LOCATION (goto_stmt, loc); | |
3769 | ||
3770 | stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE); | |
3771 | SET_EXPR_LOCATION (stmt, loc); | |
3772 | add_stmt (stmt); | |
5c4abbb8 | 3773 | |
267bac10 JM |
3774 | if (clear_call) |
3775 | add_stmt (clear_call); | |
3776 | ||
3777 | /* goto loop; */ | |
3778 | goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl); | |
3779 | SET_EXPR_LOCATION (goto_stmt, loc); | |
3780 | add_stmt (goto_stmt); | |
5c4abbb8 | 3781 | |
267bac10 JM |
3782 | /* done: */ |
3783 | add_stmt (done_label); | |
3784 | ||
3785 | if (update_call) | |
3786 | add_stmt (update_call); | |
3787 | ||
3788 | /* Finish the compound statement. */ | |
3789 | compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); | |
3790 | ||
3791 | /* NEWVAL is the value that was successfully stored, return a | |
3792 | COMPOUND_EXPR of the statement and the appropriate value. */ | |
3793 | return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, | |
3794 | return_old_p ? old : newval); | |
3795 | } | |
3796 | ||
3e4093b6 RS |
3797 | /* Construct and perhaps optimize a tree representation |
3798 | for a unary operation. CODE, a tree_code, specifies the operation | |
3799 | and XARG is the operand. | |
3800 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3801 | the default promotions (such as from short to int). | |
3802 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3803 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
c9f9eb5d AH |
3804 | arrays to pointers in C99. |
3805 | ||
3806 | LOCATION is the location of the operator. */ | |
400fbf9f | 3807 | |
3e4093b6 | 3808 | tree |
c9f9eb5d AH |
3809 | build_unary_op (location_t location, |
3810 | enum tree_code code, tree xarg, int flag) | |
3e4093b6 RS |
3811 | { |
3812 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3813 | tree arg = xarg; | |
3814 | tree argtype = 0; | |
808d6eaa | 3815 | enum tree_code typecode; |
3e4093b6 | 3816 | tree val; |
c9f9eb5d | 3817 | tree ret = error_mark_node; |
8ce94e44 | 3818 | tree eptype = NULL_TREE; |
3e4093b6 | 3819 | int noconvert = flag; |
4de67c26 | 3820 | const char *invalid_op_diag; |
928c19bb JM |
3821 | bool int_operands; |
3822 | ||
3823 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
4d84fe7c JM |
3824 | if (int_operands) |
3825 | arg = remove_c_maybe_const_expr (arg); | |
400fbf9f | 3826 | |
808d6eaa JM |
3827 | if (code != ADDR_EXPR) |
3828 | arg = require_complete_type (arg); | |
3829 | ||
3830 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
3e4093b6 RS |
3831 | if (typecode == ERROR_MARK) |
3832 | return error_mark_node; | |
3833 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3834 | typecode = INTEGER_TYPE; | |
6c36d76b | 3835 | |
4de67c26 JM |
3836 | if ((invalid_op_diag |
3837 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3838 | { | |
c9f9eb5d | 3839 | error_at (location, invalid_op_diag); |
4de67c26 JM |
3840 | return error_mark_node; |
3841 | } | |
3842 | ||
8ce94e44 JM |
3843 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
3844 | { | |
3845 | eptype = TREE_TYPE (arg); | |
3846 | arg = TREE_OPERAND (arg, 0); | |
3847 | } | |
3848 | ||
3e4093b6 RS |
3849 | switch (code) |
3850 | { | |
3851 | case CONVERT_EXPR: | |
3852 | /* This is used for unary plus, because a CONVERT_EXPR | |
3853 | is enough to prevent anybody from looking inside for | |
3854 | associativity, but won't generate any code. */ | |
3855 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3856 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
8a2cee38 | 3857 | || typecode == VECTOR_TYPE)) |
400fbf9f | 3858 | { |
c9f9eb5d | 3859 | error_at (location, "wrong type argument to unary plus"); |
3e4093b6 | 3860 | return error_mark_node; |
400fbf9f | 3861 | } |
3e4093b6 RS |
3862 | else if (!noconvert) |
3863 | arg = default_conversion (arg); | |
db3927fb | 3864 | arg = non_lvalue_loc (location, arg); |
400fbf9f JW |
3865 | break; |
3866 | ||
3e4093b6 RS |
3867 | case NEGATE_EXPR: |
3868 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3869 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
3e4093b6 RS |
3870 | || typecode == VECTOR_TYPE)) |
3871 | { | |
c9f9eb5d | 3872 | error_at (location, "wrong type argument to unary minus"); |
3e4093b6 RS |
3873 | return error_mark_node; |
3874 | } | |
3875 | else if (!noconvert) | |
3876 | arg = default_conversion (arg); | |
400fbf9f JW |
3877 | break; |
3878 | ||
3e4093b6 | 3879 | case BIT_NOT_EXPR: |
462643f0 AP |
3880 | /* ~ works on integer types and non float vectors. */ |
3881 | if (typecode == INTEGER_TYPE | |
3882 | || (typecode == VECTOR_TYPE | |
3883 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
03d5b1f5 | 3884 | { |
3e4093b6 RS |
3885 | if (!noconvert) |
3886 | arg = default_conversion (arg); | |
03d5b1f5 | 3887 | } |
3e4093b6 | 3888 | else if (typecode == COMPLEX_TYPE) |
400fbf9f | 3889 | { |
3e4093b6 | 3890 | code = CONJ_EXPR; |
c1771a20 | 3891 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 3892 | "ISO C does not support %<~%> for complex conjugation"); |
3e4093b6 RS |
3893 | if (!noconvert) |
3894 | arg = default_conversion (arg); | |
3895 | } | |
3896 | else | |
3897 | { | |
c9f9eb5d | 3898 | error_at (location, "wrong type argument to bit-complement"); |
3e4093b6 | 3899 | return error_mark_node; |
400fbf9f JW |
3900 | } |
3901 | break; | |
3902 | ||
3e4093b6 | 3903 | case ABS_EXPR: |
11017cc7 | 3904 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
400fbf9f | 3905 | { |
c9f9eb5d | 3906 | error_at (location, "wrong type argument to abs"); |
3e4093b6 | 3907 | return error_mark_node; |
400fbf9f | 3908 | } |
3e4093b6 RS |
3909 | else if (!noconvert) |
3910 | arg = default_conversion (arg); | |
400fbf9f JW |
3911 | break; |
3912 | ||
3e4093b6 RS |
3913 | case CONJ_EXPR: |
3914 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3915 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3916 | || typecode == COMPLEX_TYPE)) | |
400fbf9f | 3917 | { |
c9f9eb5d | 3918 | error_at (location, "wrong type argument to conjugation"); |
3e4093b6 | 3919 | return error_mark_node; |
400fbf9f | 3920 | } |
3e4093b6 RS |
3921 | else if (!noconvert) |
3922 | arg = default_conversion (arg); | |
400fbf9f JW |
3923 | break; |
3924 | ||
3e4093b6 | 3925 | case TRUTH_NOT_EXPR: |
ab22c1fa | 3926 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3927 | && typecode != REAL_TYPE && typecode != POINTER_TYPE |
46bdb9cf | 3928 | && typecode != COMPLEX_TYPE) |
400fbf9f | 3929 | { |
c9f9eb5d AH |
3930 | error_at (location, |
3931 | "wrong type argument to unary exclamation mark"); | |
3e4093b6 | 3932 | return error_mark_node; |
400fbf9f | 3933 | } |
a27d595d JM |
3934 | if (int_operands) |
3935 | { | |
3936 | arg = c_objc_common_truthvalue_conversion (location, xarg); | |
3937 | arg = remove_c_maybe_const_expr (arg); | |
3938 | } | |
3939 | else | |
3940 | arg = c_objc_common_truthvalue_conversion (location, arg); | |
db3927fb | 3941 | ret = invert_truthvalue_loc (location, arg); |
ca80e52b EB |
3942 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ |
3943 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3944 | location = EXPR_LOCATION (ret); | |
c9f9eb5d | 3945 | goto return_build_unary_op; |
3e4093b6 | 3946 | |
3e4093b6 | 3947 | case REALPART_EXPR: |
3e4093b6 | 3948 | case IMAGPART_EXPR: |
fb52b50a NF |
3949 | ret = build_real_imag_expr (location, code, arg); |
3950 | if (ret == error_mark_node) | |
3951 | return error_mark_node; | |
8ce94e44 JM |
3952 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3953 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3954 | goto return_build_unary_op; |
3e4093b6 RS |
3955 | |
3956 | case PREINCREMENT_EXPR: | |
3957 | case POSTINCREMENT_EXPR: | |
3958 | case PREDECREMENT_EXPR: | |
3959 | case POSTDECREMENT_EXPR: | |
3e4093b6 | 3960 | |
928c19bb JM |
3961 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) |
3962 | { | |
3963 | tree inner = build_unary_op (location, code, | |
3964 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3965 | if (inner == error_mark_node) | |
3966 | return error_mark_node; | |
3967 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3968 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3969 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3970 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3971 | goto return_build_unary_op; | |
3972 | } | |
3973 | ||
925e8657 NP |
3974 | /* Complain about anything that is not a true lvalue. In |
3975 | Objective-C, skip this check for property_refs. */ | |
f90e8e2e | 3976 | if (!objc_is_property_ref (arg) |
7bd11157 TT |
3977 | && !lvalue_or_else (location, |
3978 | arg, ((code == PREINCREMENT_EXPR | |
925e8657 NP |
3979 | || code == POSTINCREMENT_EXPR) |
3980 | ? lv_increment | |
3981 | : lv_decrement))) | |
928c19bb JM |
3982 | return error_mark_node; |
3983 | ||
09639a83 ILT |
3984 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) |
3985 | { | |
3986 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3987 | warning_at (location, OPT_Wc___compat, | |
3988 | "increment of enumeration value is invalid in C++"); | |
3989 | else | |
3990 | warning_at (location, OPT_Wc___compat, | |
3991 | "decrement of enumeration value is invalid in C++"); | |
3992 | } | |
3993 | ||
928c19bb JM |
3994 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ |
3995 | arg = c_fully_fold (arg, false, NULL); | |
3996 | ||
267bac10 JM |
3997 | bool atomic_op; |
3998 | atomic_op = really_atomic_lvalue (arg); | |
3999 | ||
3e4093b6 RS |
4000 | /* Increment or decrement the real part of the value, |
4001 | and don't change the imaginary part. */ | |
4002 | if (typecode == COMPLEX_TYPE) | |
400fbf9f | 4003 | { |
3e4093b6 RS |
4004 | tree real, imag; |
4005 | ||
c1771a20 | 4006 | pedwarn (location, OPT_Wpedantic, |
509c9d60 | 4007 | "ISO C does not support %<++%> and %<--%> on complex types"); |
3e4093b6 | 4008 | |
267bac10 JM |
4009 | if (!atomic_op) |
4010 | { | |
4011 | arg = stabilize_reference (arg); | |
4012 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); | |
4013 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
4014 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
4015 | if (real == error_mark_node || imag == error_mark_node) | |
4016 | return error_mark_node; | |
4017 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), | |
4018 | real, imag); | |
4019 | goto return_build_unary_op; | |
4020 | } | |
400fbf9f | 4021 | } |
3e4093b6 RS |
4022 | |
4023 | /* Report invalid types. */ | |
4024 | ||
ab22c1fa | 4025 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE |
267bac10 | 4026 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE |
241b71bb | 4027 | && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE) |
400fbf9f | 4028 | { |
3e4093b6 | 4029 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c9f9eb5d | 4030 | error_at (location, "wrong type argument to increment"); |
c22cacf3 | 4031 | else |
c9f9eb5d | 4032 | error_at (location, "wrong type argument to decrement"); |
3e4093b6 RS |
4033 | |
4034 | return error_mark_node; | |
400fbf9f | 4035 | } |
400fbf9f | 4036 | |
3e4093b6 RS |
4037 | { |
4038 | tree inc; | |
400fbf9f | 4039 | |
3e4093b6 RS |
4040 | argtype = TREE_TYPE (arg); |
4041 | ||
4042 | /* Compute the increment. */ | |
4043 | ||
4044 | if (typecode == POINTER_TYPE) | |
4045 | { | |
a0e24419 | 4046 | /* If pointer target is an incomplete type, |
3e4093b6 | 4047 | we just cannot know how to do the arithmetic. */ |
b70cef5d | 4048 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) |
3e4093b6 RS |
4049 | { |
4050 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
c9f9eb5d | 4051 | error_at (location, |
a0e24419 MP |
4052 | "increment of pointer to an incomplete type %qT", |
4053 | TREE_TYPE (argtype)); | |
3e4093b6 | 4054 | else |
c9f9eb5d | 4055 | error_at (location, |
a0e24419 MP |
4056 | "decrement of pointer to an incomplete type %qT", |
4057 | TREE_TYPE (argtype)); | |
3e4093b6 | 4058 | } |
b70cef5d JJ |
4059 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE |
4060 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
c22cacf3 | 4061 | { |
3e4093b6 | 4062 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
44d90fe1 | 4063 | pedwarn (location, OPT_Wpointer_arith, |
fcf73884 | 4064 | "wrong type argument to increment"); |
3e4093b6 | 4065 | else |
44d90fe1 | 4066 | pedwarn (location, OPT_Wpointer_arith, |
fcf73884 | 4067 | "wrong type argument to decrement"); |
3e4093b6 RS |
4068 | } |
4069 | ||
b70cef5d | 4070 | inc = c_size_in_bytes (TREE_TYPE (argtype)); |
0d82a1c8 | 4071 | inc = convert_to_ptrofftype_loc (location, inc); |
3e4093b6 | 4072 | } |
b70cef5d | 4073 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) |
ab22c1fa CF |
4074 | { |
4075 | /* For signed fract types, we invert ++ to -- or | |
4076 | -- to ++, and change inc from 1 to -1, because | |
4077 | it is not possible to represent 1 in signed fract constants. | |
4078 | For unsigned fract types, the result always overflows and | |
4079 | we get an undefined (original) or the maximum value. */ | |
4080 | if (code == PREINCREMENT_EXPR) | |
4081 | code = PREDECREMENT_EXPR; | |
4082 | else if (code == PREDECREMENT_EXPR) | |
4083 | code = PREINCREMENT_EXPR; | |
4084 | else if (code == POSTINCREMENT_EXPR) | |
4085 | code = POSTDECREMENT_EXPR; | |
4086 | else /* code == POSTDECREMENT_EXPR */ | |
4087 | code = POSTINCREMENT_EXPR; | |
4088 | ||
4089 | inc = integer_minus_one_node; | |
4090 | inc = convert (argtype, inc); | |
4091 | } | |
3e4093b6 | 4092 | else |
5be014d5 | 4093 | { |
241b71bb TV |
4094 | inc = VECTOR_TYPE_P (argtype) |
4095 | ? build_one_cst (argtype) | |
4096 | : integer_one_node; | |
5be014d5 AP |
4097 | inc = convert (argtype, inc); |
4098 | } | |
3e4093b6 | 4099 | |
925e8657 NP |
4100 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
4101 | need to ask Objective-C to build the increment or decrement | |
4102 | expression for it. */ | |
4103 | if (objc_is_property_ref (arg)) | |
f90e8e2e | 4104 | return objc_build_incr_expr_for_property_ref (location, code, |
925e8657 NP |
4105 | arg, inc); |
4106 | ||
3e4093b6 | 4107 | /* Report a read-only lvalue. */ |
f37acdf9 | 4108 | if (TYPE_READONLY (argtype)) |
953ff289 | 4109 | { |
c02065fc | 4110 | readonly_error (location, arg, |
953ff289 DN |
4111 | ((code == PREINCREMENT_EXPR |
4112 | || code == POSTINCREMENT_EXPR) | |
4113 | ? lv_increment : lv_decrement)); | |
4114 | return error_mark_node; | |
4115 | } | |
f37acdf9 JM |
4116 | else if (TREE_READONLY (arg)) |
4117 | readonly_warning (arg, | |
4118 | ((code == PREINCREMENT_EXPR | |
4119 | || code == POSTINCREMENT_EXPR) | |
4120 | ? lv_increment : lv_decrement)); | |
3e4093b6 | 4121 | |
267bac10 JM |
4122 | /* If the argument is atomic, use the special code sequences for |
4123 | atomic compound assignment. */ | |
4124 | if (atomic_op) | |
4125 | { | |
4126 | arg = stabilize_reference (arg); | |
4127 | ret = build_atomic_assign (location, arg, | |
4128 | ((code == PREINCREMENT_EXPR | |
4129 | || code == POSTINCREMENT_EXPR) | |
4130 | ? PLUS_EXPR | |
4131 | : MINUS_EXPR), | |
4132 | (FRACT_MODE_P (TYPE_MODE (argtype)) | |
4133 | ? inc | |
4134 | : integer_one_node), | |
4135 | (code == POSTINCREMENT_EXPR | |
4136 | || code == POSTDECREMENT_EXPR)); | |
4137 | goto return_build_unary_op; | |
4138 | } | |
4139 | ||
3e4093b6 RS |
4140 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) |
4141 | val = boolean_increment (code, arg); | |
4142 | else | |
53fb4de3 | 4143 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
3e4093b6 | 4144 | TREE_SIDE_EFFECTS (val) = 1; |
3e4093b6 | 4145 | if (TREE_CODE (val) != code) |
6de9cd9a | 4146 | TREE_NO_WARNING (val) = 1; |
c9f9eb5d AH |
4147 | ret = val; |
4148 | goto return_build_unary_op; | |
3e4093b6 RS |
4149 | } |
4150 | ||
4151 | case ADDR_EXPR: | |
4152 | /* Note that this operation never does default_conversion. */ | |
4153 | ||
2b4b7036 JM |
4154 | /* The operand of unary '&' must be an lvalue (which excludes |
4155 | expressions of type void), or, in C99, the result of a [] or | |
4156 | unary '*' operator. */ | |
4157 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
4158 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
22d03525 | 4159 | && (!INDIRECT_REF_P (arg) || !flag_isoc99)) |
2b4b7036 JM |
4160 | pedwarn (location, 0, "taking address of expression of type %<void%>"); |
4161 | ||
3e4093b6 | 4162 | /* Let &* cancel out to simplify resulting code. */ |
22d03525 | 4163 | if (INDIRECT_REF_P (arg)) |
400fbf9f | 4164 | { |
3e4093b6 RS |
4165 | /* Don't let this be an lvalue. */ |
4166 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
db3927fb | 4167 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); |
c9f9eb5d AH |
4168 | ret = TREE_OPERAND (arg, 0); |
4169 | goto return_build_unary_op; | |
400fbf9f | 4170 | } |
1eb8759b | 4171 | |
7c672dfc | 4172 | /* For &x[y], return x+y */ |
3e4093b6 | 4173 | if (TREE_CODE (arg) == ARRAY_REF) |
1eb8759b | 4174 | { |
f2a71bbc JM |
4175 | tree op0 = TREE_OPERAND (arg, 0); |
4176 | if (!c_mark_addressable (op0)) | |
3e4093b6 | 4177 | return error_mark_node; |
1eb8759b | 4178 | } |
1eb8759b | 4179 | |
3e4093b6 RS |
4180 | /* Anything not already handled and not a true memory reference |
4181 | or a non-lvalue array is an error. */ | |
4182 | else if (typecode != FUNCTION_TYPE && !flag | |
7bd11157 | 4183 | && !lvalue_or_else (location, arg, lv_addressof)) |
3e4093b6 | 4184 | return error_mark_node; |
b6a10c9f | 4185 | |
928c19bb JM |
4186 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify |
4187 | folding later. */ | |
4188 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
4189 | { | |
4190 | tree inner = build_unary_op (location, code, | |
4191 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
4192 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4193 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
4194 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
4195 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
4196 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
4197 | goto return_build_unary_op; | |
4198 | } | |
4199 | ||
3e4093b6 RS |
4200 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
4201 | argtype = TREE_TYPE (arg); | |
400fbf9f | 4202 | |
3e4093b6 | 4203 | /* If the lvalue is const or volatile, merge that into the type |
e73a83fc | 4204 | to which the address will point. This is only needed |
f2c1da78 | 4205 | for function types. */ |
6615c446 | 4206 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) |
e73a83fc RB |
4207 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) |
4208 | && TREE_CODE (argtype) == FUNCTION_TYPE) | |
f2c1da78 JM |
4209 | { |
4210 | int orig_quals = TYPE_QUALS (strip_array_types (argtype)); | |
4211 | int quals = orig_quals; | |
4212 | ||
4213 | if (TREE_READONLY (arg)) | |
4214 | quals |= TYPE_QUAL_CONST; | |
4215 | if (TREE_THIS_VOLATILE (arg)) | |
4216 | quals |= TYPE_QUAL_VOLATILE; | |
4217 | ||
f2c1da78 JM |
4218 | argtype = c_build_qualified_type (argtype, quals); |
4219 | } | |
400fbf9f | 4220 | |
3e4093b6 RS |
4221 | if (!c_mark_addressable (arg)) |
4222 | return error_mark_node; | |
400fbf9f | 4223 | |
abb54d14 JM |
4224 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF |
4225 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
400fbf9f | 4226 | |
5cc200fc | 4227 | argtype = build_pointer_type (argtype); |
5e55f99d RH |
4228 | |
4229 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
4230 | when we have proper support for integer constant expressions. */ | |
4231 | val = get_base_address (arg); | |
22d03525 | 4232 | if (val && INDIRECT_REF_P (val) |
3aa2ddb8 JJ |
4233 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
4234 | { | |
cf9e9959 | 4235 | ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg)); |
c9f9eb5d | 4236 | goto return_build_unary_op; |
3aa2ddb8 | 4237 | } |
5e55f99d | 4238 | |
5cc200fc | 4239 | val = build1 (ADDR_EXPR, argtype, arg); |
400fbf9f | 4240 | |
c9f9eb5d AH |
4241 | ret = val; |
4242 | goto return_build_unary_op; | |
400fbf9f | 4243 | |
3e4093b6 | 4244 | default: |
1344f9a3 | 4245 | gcc_unreachable (); |
3e4093b6 | 4246 | } |
400fbf9f | 4247 | |
3e4093b6 RS |
4248 | if (argtype == 0) |
4249 | argtype = TREE_TYPE (arg); | |
928c19bb JM |
4250 | if (TREE_CODE (arg) == INTEGER_CST) |
4251 | ret = (require_constant_value | |
db3927fb AH |
4252 | ? fold_build1_initializer_loc (location, code, argtype, arg) |
4253 | : fold_build1_loc (location, code, argtype, arg)); | |
928c19bb JM |
4254 | else |
4255 | ret = build1 (code, argtype, arg); | |
c9f9eb5d AH |
4256 | return_build_unary_op: |
4257 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
4258 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) |
4259 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
4260 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
4261 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
4262 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
4263 | if (eptype) |
4264 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
c9f9eb5d AH |
4265 | protected_set_expr_location (ret, location); |
4266 | return ret; | |
3e4093b6 | 4267 | } |
400fbf9f | 4268 | |
3e4093b6 RS |
4269 | /* Return nonzero if REF is an lvalue valid for this language. |
4270 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
5baeaac0 | 4271 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
400fbf9f | 4272 | |
1e4ae551 | 4273 | bool |
58f9752a | 4274 | lvalue_p (const_tree ref) |
3e4093b6 | 4275 | { |
58f9752a | 4276 | const enum tree_code code = TREE_CODE (ref); |
400fbf9f | 4277 | |
3e4093b6 RS |
4278 | switch (code) |
4279 | { | |
4280 | case REALPART_EXPR: | |
4281 | case IMAGPART_EXPR: | |
4282 | case COMPONENT_REF: | |
4283 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
400fbf9f | 4284 | |
928c19bb JM |
4285 | case C_MAYBE_CONST_EXPR: |
4286 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
4287 | ||
3e4093b6 RS |
4288 | case COMPOUND_LITERAL_EXPR: |
4289 | case STRING_CST: | |
4290 | return 1; | |
400fbf9f | 4291 | |
3e4093b6 RS |
4292 | case INDIRECT_REF: |
4293 | case ARRAY_REF: | |
36536d79 | 4294 | case ARRAY_NOTATION_REF: |
3e4093b6 RS |
4295 | case VAR_DECL: |
4296 | case PARM_DECL: | |
4297 | case RESULT_DECL: | |
4298 | case ERROR_MARK: | |
4299 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
4300 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
665f2503 | 4301 | |
3e4093b6 | 4302 | case BIND_EXPR: |
3e4093b6 | 4303 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
665f2503 | 4304 | |
3e4093b6 RS |
4305 | default: |
4306 | return 0; | |
4307 | } | |
4308 | } | |
400fbf9f | 4309 | \f |
f37acdf9 JM |
4310 | /* Give a warning for storing in something that is read-only in GCC |
4311 | terms but not const in ISO C terms. */ | |
4312 | ||
4313 | static void | |
4314 | readonly_warning (tree arg, enum lvalue_use use) | |
4315 | { | |
4316 | switch (use) | |
4317 | { | |
4318 | case lv_assign: | |
4319 | warning (0, "assignment of read-only location %qE", arg); | |
4320 | break; | |
4321 | case lv_increment: | |
4322 | warning (0, "increment of read-only location %qE", arg); | |
4323 | break; | |
4324 | case lv_decrement: | |
4325 | warning (0, "decrement of read-only location %qE", arg); | |
4326 | break; | |
4327 | default: | |
4328 | gcc_unreachable (); | |
4329 | } | |
4330 | return; | |
4331 | } | |
4332 | ||
37dc0d8d JM |
4333 | |
4334 | /* Return nonzero if REF is an lvalue valid for this language; | |
4335 | otherwise, print an error message and return zero. USE says | |
7bd11157 TT |
4336 | how the lvalue is being used and so selects the error message. |
4337 | LOCATION is the location at which any error should be reported. */ | |
37dc0d8d JM |
4338 | |
4339 | static int | |
7bd11157 | 4340 | lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) |
37dc0d8d JM |
4341 | { |
4342 | int win = lvalue_p (ref); | |
4343 | ||
4344 | if (!win) | |
7bd11157 | 4345 | lvalue_error (loc, use); |
37dc0d8d JM |
4346 | |
4347 | return win; | |
4348 | } | |
3e4093b6 RS |
4349 | \f |
4350 | /* Mark EXP saying that we need to be able to take the | |
4351 | address of it; it should not be allocated in a register. | |
4352 | Returns true if successful. */ | |
54c93c30 | 4353 | |
3e4093b6 RS |
4354 | bool |
4355 | c_mark_addressable (tree exp) | |
400fbf9f | 4356 | { |
3e4093b6 | 4357 | tree x = exp; |
95602da1 | 4358 | |
3e4093b6 RS |
4359 | while (1) |
4360 | switch (TREE_CODE (x)) | |
4361 | { | |
4362 | case COMPONENT_REF: | |
4363 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
4364 | { | |
0039fa55 AN |
4365 | error |
4366 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
3e4093b6 RS |
4367 | return false; |
4368 | } | |
95602da1 | 4369 | |
3e4093b6 | 4370 | /* ... fall through ... */ |
95602da1 | 4371 | |
3e4093b6 RS |
4372 | case ADDR_EXPR: |
4373 | case ARRAY_REF: | |
4374 | case REALPART_EXPR: | |
4375 | case IMAGPART_EXPR: | |
4376 | x = TREE_OPERAND (x, 0); | |
4377 | break; | |
95602da1 | 4378 | |
3e4093b6 RS |
4379 | case COMPOUND_LITERAL_EXPR: |
4380 | case CONSTRUCTOR: | |
4381 | TREE_ADDRESSABLE (x) = 1; | |
4382 | return true; | |
95602da1 | 4383 | |
3e4093b6 RS |
4384 | case VAR_DECL: |
4385 | case CONST_DECL: | |
4386 | case PARM_DECL: | |
4387 | case RESULT_DECL: | |
5baeaac0 | 4388 | if (C_DECL_REGISTER (x) |
3e4093b6 RS |
4389 | && DECL_NONLOCAL (x)) |
4390 | { | |
62f9079a | 4391 | if (TREE_PUBLIC (x) || is_global_var (x)) |
3e4093b6 | 4392 | { |
0039fa55 AN |
4393 | error |
4394 | ("global register variable %qD used in nested function", x); | |
3e4093b6 RS |
4395 | return false; |
4396 | } | |
509c9d60 | 4397 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); |
3e4093b6 | 4398 | } |
5baeaac0 | 4399 | else if (C_DECL_REGISTER (x)) |
3e4093b6 | 4400 | { |
62f9079a | 4401 | if (TREE_PUBLIC (x) || is_global_var (x)) |
0039fa55 AN |
4402 | error ("address of global register variable %qD requested", x); |
4403 | else | |
4404 | error ("address of register variable %qD requested", x); | |
4405 | return false; | |
3e4093b6 | 4406 | } |
400fbf9f | 4407 | |
3e4093b6 RS |
4408 | /* drops in */ |
4409 | case FUNCTION_DECL: | |
4410 | TREE_ADDRESSABLE (x) = 1; | |
4411 | /* drops out */ | |
4412 | default: | |
4413 | return true; | |
4414 | } | |
4415 | } | |
4416 | \f | |
2d2e923f MLI |
4417 | /* Convert EXPR to TYPE, warning about conversion problems with |
4418 | constants. SEMANTIC_TYPE is the type this conversion would use | |
4419 | without excess precision. If SEMANTIC_TYPE is NULL, this function | |
4420 | is equivalent to convert_and_check. This function is a wrapper that | |
4421 | handles conversions that may be different than | |
4422 | the usual ones because of excess precision. */ | |
4423 | ||
4424 | static tree | |
68fca595 MP |
4425 | ep_convert_and_check (location_t loc, tree type, tree expr, |
4426 | tree semantic_type) | |
2d2e923f MLI |
4427 | { |
4428 | if (TREE_TYPE (expr) == type) | |
4429 | return expr; | |
4430 | ||
4431 | if (!semantic_type) | |
68fca595 | 4432 | return convert_and_check (loc, type, expr); |
2d2e923f MLI |
4433 | |
4434 | if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
4435 | && TREE_TYPE (expr) != semantic_type) | |
4436 | { | |
4437 | /* For integers, we need to check the real conversion, not | |
4438 | the conversion to the excess precision type. */ | |
68fca595 | 4439 | expr = convert_and_check (loc, semantic_type, expr); |
2d2e923f MLI |
4440 | } |
4441 | /* Result type is the excess precision type, which should be | |
4442 | large enough, so do not check. */ | |
4443 | return convert (type, expr); | |
4444 | } | |
4445 | ||
928c19bb JM |
4446 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If |
4447 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
4448 | if folded to an integer constant then the unselected half may | |
4449 | contain arbitrary operations not normally permitted in constant | |
c2255bc4 | 4450 | expressions. Set the location of the expression to LOC. */ |
400fbf9f JW |
4451 | |
4452 | tree | |
744aa42f | 4453 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, |
d130ae11 ILT |
4454 | tree op1, tree op1_original_type, tree op2, |
4455 | tree op2_original_type) | |
400fbf9f | 4456 | { |
3e4093b6 RS |
4457 | tree type1; |
4458 | tree type2; | |
4459 | enum tree_code code1; | |
4460 | enum tree_code code2; | |
4461 | tree result_type = NULL; | |
2d2e923f | 4462 | tree semantic_result_type = NULL; |
3e4093b6 | 4463 | tree orig_op1 = op1, orig_op2 = op2; |
928c19bb | 4464 | bool int_const, op1_int_operands, op2_int_operands, int_operands; |
4d84fe7c | 4465 | bool ifexp_int_operands; |
928c19bb | 4466 | tree ret; |
400fbf9f | 4467 | |
4d84fe7c JM |
4468 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); |
4469 | if (op1_int_operands) | |
4470 | op1 = remove_c_maybe_const_expr (op1); | |
4471 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
4472 | if (op2_int_operands) | |
4473 | op2 = remove_c_maybe_const_expr (op2); | |
4474 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
4475 | if (ifexp_int_operands) | |
4476 | ifexp = remove_c_maybe_const_expr (ifexp); | |
4477 | ||
3e4093b6 RS |
4478 | /* Promote both alternatives. */ |
4479 | ||
4480 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
4481 | op1 = default_conversion (op1); | |
4482 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
4483 | op2 = default_conversion (op2); | |
4484 | ||
4485 | if (TREE_CODE (ifexp) == ERROR_MARK | |
4486 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
4487 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
400fbf9f | 4488 | return error_mark_node; |
400fbf9f | 4489 | |
3e4093b6 RS |
4490 | type1 = TREE_TYPE (op1); |
4491 | code1 = TREE_CODE (type1); | |
4492 | type2 = TREE_TYPE (op2); | |
4493 | code2 = TREE_CODE (type2); | |
4494 | ||
1807ffc1 MS |
4495 | if (code1 == POINTER_TYPE && reject_gcc_builtin (op1)) |
4496 | return error_mark_node; | |
4497 | ||
4498 | if (code2 == POINTER_TYPE && reject_gcc_builtin (op2)) | |
4499 | return error_mark_node; | |
4500 | ||
b1adf557 JM |
4501 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
4502 | In C99 they will be pointers by now. */ | |
4503 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
4504 | { | |
744aa42f | 4505 | error_at (colon_loc, "non-lvalue array in conditional expression"); |
b1adf557 JM |
4506 | return error_mark_node; |
4507 | } | |
4508 | ||
8ce94e44 JM |
4509 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR |
4510 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4511 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4512 | || code1 == COMPLEX_TYPE) | |
4513 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4514 | || code2 == COMPLEX_TYPE)) | |
4515 | { | |
2d2e923f | 4516 | semantic_result_type = c_common_type (type1, type2); |
8ce94e44 JM |
4517 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) |
4518 | { | |
4519 | op1 = TREE_OPERAND (op1, 0); | |
4520 | type1 = TREE_TYPE (op1); | |
4521 | gcc_assert (TREE_CODE (type1) == code1); | |
4522 | } | |
4523 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4524 | { | |
4525 | op2 = TREE_OPERAND (op2, 0); | |
4526 | type2 = TREE_TYPE (op2); | |
4527 | gcc_assert (TREE_CODE (type2) == code2); | |
4528 | } | |
4529 | } | |
4530 | ||
d130ae11 ILT |
4531 | if (warn_cxx_compat) |
4532 | { | |
4533 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
4534 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
4535 | ||
4536 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
4537 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
4538 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
4539 | warning_at (colon_loc, OPT_Wc___compat, | |
4540 | ("different enum types in conditional is " | |
4541 | "invalid in C++: %qT vs %qT"), | |
4542 | t1, t2); | |
4543 | } | |
4544 | ||
3e4093b6 RS |
4545 | /* Quickly detect the usual case where op1 and op2 have the same type |
4546 | after promotion. */ | |
4547 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
400fbf9f | 4548 | { |
3e4093b6 RS |
4549 | if (type1 == type2) |
4550 | result_type = type1; | |
4551 | else | |
4552 | result_type = TYPE_MAIN_VARIANT (type1); | |
4553 | } | |
4554 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
c22cacf3 MS |
4555 | || code1 == COMPLEX_TYPE) |
4556 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4557 | || code2 == COMPLEX_TYPE)) | |
3e4093b6 | 4558 | { |
ccf7f880 | 4559 | result_type = c_common_type (type1, type2); |
0a0b3574 MM |
4560 | do_warn_double_promotion (result_type, type1, type2, |
4561 | "implicit conversion from %qT to %qT to " | |
4562 | "match other result of conditional", | |
4563 | colon_loc); | |
400fbf9f | 4564 | |
3e4093b6 RS |
4565 | /* If -Wsign-compare, warn here if type1 and type2 have |
4566 | different signedness. We'll promote the signed to unsigned | |
4567 | and later code won't know it used to be different. | |
4568 | Do this check on the original types, so that explicit casts | |
4569 | will be considered, but default promotions won't. */ | |
7d882b83 | 4570 | if (c_inhibit_evaluation_warnings == 0) |
ab87f8c8 | 4571 | { |
8df83eae RK |
4572 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
4573 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
400fbf9f | 4574 | |
3e4093b6 RS |
4575 | if (unsigned_op1 ^ unsigned_op2) |
4576 | { | |
6ac01510 ILT |
4577 | bool ovf; |
4578 | ||
3e4093b6 RS |
4579 | /* Do not warn if the result type is signed, since the |
4580 | signed type will only be chosen if it can represent | |
4581 | all the values of the unsigned type. */ | |
3f75a254 | 4582 | if (!TYPE_UNSIGNED (result_type)) |
3e4093b6 | 4583 | /* OK */; |
3e4093b6 | 4584 | else |
928c19bb JM |
4585 | { |
4586 | bool op1_maybe_const = true; | |
4587 | bool op2_maybe_const = true; | |
4588 | ||
4589 | /* Do not warn if the signed quantity is an | |
4590 | unsuffixed integer literal (or some static | |
4591 | constant expression involving such literals) and | |
4592 | it is non-negative. This warning requires the | |
4593 | operands to be folded for best results, so do | |
4594 | that folding in this case even without | |
4595 | warn_sign_compare to avoid warning options | |
4596 | possibly affecting code generation. */ | |
f5178456 RS |
4597 | c_inhibit_evaluation_warnings |
4598 | += (ifexp == truthvalue_false_node); | |
928c19bb JM |
4599 | op1 = c_fully_fold (op1, require_constant_value, |
4600 | &op1_maybe_const); | |
f5178456 RS |
4601 | c_inhibit_evaluation_warnings |
4602 | -= (ifexp == truthvalue_false_node); | |
4603 | ||
4604 | c_inhibit_evaluation_warnings | |
4605 | += (ifexp == truthvalue_true_node); | |
928c19bb JM |
4606 | op2 = c_fully_fold (op2, require_constant_value, |
4607 | &op2_maybe_const); | |
f5178456 RS |
4608 | c_inhibit_evaluation_warnings |
4609 | -= (ifexp == truthvalue_true_node); | |
928c19bb JM |
4610 | |
4611 | if (warn_sign_compare) | |
4612 | { | |
4613 | if ((unsigned_op2 | |
4614 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4615 | || (unsigned_op1 | |
4616 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4617 | /* OK */; | |
4618 | else | |
744aa42f ILT |
4619 | warning_at (colon_loc, OPT_Wsign_compare, |
4620 | ("signed and unsigned type in " | |
4621 | "conditional expression")); | |
928c19bb JM |
4622 | } |
4623 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
e5a94231 | 4624 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 4625 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) |
e5a94231 | 4626 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); |
928c19bb | 4627 | } |
3e4093b6 RS |
4628 | } |
4629 | } | |
4630 | } | |
4631 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4632 | { | |
fcf73884 | 4633 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) |
c1771a20 | 4634 | pedwarn (colon_loc, OPT_Wpedantic, |
fcf73884 | 4635 | "ISO C forbids conditional expr with only one void side"); |
3e4093b6 RS |
4636 | result_type = void_type_node; |
4637 | } | |
4638 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4639 | { | |
36c5e70a BE |
4640 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); |
4641 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4642 | addr_space_t as_common; | |
4643 | ||
744aa42f | 4644 | if (comp_target_types (colon_loc, type1, type2)) |
10bc1b1b | 4645 | result_type = common_pointer_type (type1, type2); |
6aa3c60d | 4646 | else if (null_pointer_constant_p (orig_op1)) |
36c5e70a | 4647 | result_type = type2; |
6aa3c60d | 4648 | else if (null_pointer_constant_p (orig_op2)) |
36c5e70a BE |
4649 | result_type = type1; |
4650 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4651 | { | |
4652 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4653 | "used in conditional expression"); | |
4654 | return error_mark_node; | |
4655 | } | |
267bac10 JM |
4656 | else if (VOID_TYPE_P (TREE_TYPE (type1)) |
4657 | && !TYPE_ATOMIC (TREE_TYPE (type1))) | |
34a80643 | 4658 | { |
768952be MU |
4659 | if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE) |
4660 | && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2))) | |
4661 | & ~TYPE_QUALS (TREE_TYPE (type1)))) | |
4662 | warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers, | |
4663 | "pointer to array loses qualifier " | |
4664 | "in conditional expression"); | |
4665 | ||
fcf73884 | 4666 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
c1771a20 | 4667 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4668 | "ISO C forbids conditional expr between " |
bda67431 | 4669 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4670 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), |
4671 | TREE_TYPE (type2))); | |
34a80643 | 4672 | } |
267bac10 JM |
4673 | else if (VOID_TYPE_P (TREE_TYPE (type2)) |
4674 | && !TYPE_ATOMIC (TREE_TYPE (type2))) | |
1c2a9b35 | 4675 | { |
768952be MU |
4676 | if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE) |
4677 | && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1))) | |
4678 | & ~TYPE_QUALS (TREE_TYPE (type2)))) | |
4679 | warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers, | |
4680 | "pointer to array loses qualifier " | |
4681 | "in conditional expression"); | |
4682 | ||
fcf73884 | 4683 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
c1771a20 | 4684 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4685 | "ISO C forbids conditional expr between " |
bda67431 | 4686 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4687 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), |
4688 | TREE_TYPE (type1))); | |
1c2a9b35 | 4689 | } |
b581b85b NP |
4690 | /* Objective-C pointer comparisons are a bit more lenient. */ |
4691 | else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) | |
4692 | result_type = objc_common_type (type1, type2); | |
34a80643 | 4693 | else |
ab87f8c8 | 4694 | { |
36c5e70a BE |
4695 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
4696 | ||
b581b85b NP |
4697 | pedwarn (colon_loc, 0, |
4698 | "pointer type mismatch in conditional expression"); | |
36c5e70a BE |
4699 | result_type = build_pointer_type |
4700 | (build_qualified_type (void_type_node, qual)); | |
ab87f8c8 | 4701 | } |
3e4093b6 RS |
4702 | } |
4703 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4704 | { | |
6aa3c60d | 4705 | if (!null_pointer_constant_p (orig_op2)) |
744aa42f | 4706 | pedwarn (colon_loc, 0, |
509c9d60 | 4707 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4708 | else |
ab87f8c8 | 4709 | { |
3e4093b6 | 4710 | op2 = null_pointer_node; |
ab87f8c8 | 4711 | } |
3e4093b6 RS |
4712 | result_type = type1; |
4713 | } | |
4714 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4715 | { | |
6aa3c60d | 4716 | if (!null_pointer_constant_p (orig_op1)) |
744aa42f | 4717 | pedwarn (colon_loc, 0, |
509c9d60 | 4718 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4719 | else |
ab87f8c8 | 4720 | { |
3e4093b6 | 4721 | op1 = null_pointer_node; |
ab87f8c8 | 4722 | } |
3e4093b6 RS |
4723 | result_type = type2; |
4724 | } | |
1c2a9b35 | 4725 | |
3e4093b6 RS |
4726 | if (!result_type) |
4727 | { | |
4728 | if (flag_cond_mismatch) | |
4729 | result_type = void_type_node; | |
4730 | else | |
400fbf9f | 4731 | { |
c2255bc4 | 4732 | error_at (colon_loc, "type mismatch in conditional expression"); |
ab87f8c8 | 4733 | return error_mark_node; |
400fbf9f | 4734 | } |
3e4093b6 | 4735 | } |
400fbf9f | 4736 | |
3e4093b6 RS |
4737 | /* Merge const and volatile flags of the incoming types. */ |
4738 | result_type | |
4739 | = build_type_variant (result_type, | |
afbd0665 AS |
4740 | TYPE_READONLY (type1) || TYPE_READONLY (type2), |
4741 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
b6a10c9f | 4742 | |
68fca595 MP |
4743 | op1 = ep_convert_and_check (colon_loc, result_type, op1, |
4744 | semantic_result_type); | |
4745 | op2 = ep_convert_and_check (colon_loc, result_type, op2, | |
4746 | semantic_result_type); | |
b6a10c9f | 4747 | |
928c19bb JM |
4748 | if (ifexp_bcp && ifexp == truthvalue_true_node) |
4749 | { | |
4750 | op2_int_operands = true; | |
4751 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4752 | } | |
4753 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4754 | { | |
4755 | op1_int_operands = true; | |
4756 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4757 | } | |
4d84fe7c | 4758 | int_const = int_operands = (ifexp_int_operands |
928c19bb JM |
4759 | && op1_int_operands |
4760 | && op2_int_operands); | |
4761 | if (int_operands) | |
4762 | { | |
4763 | int_const = ((ifexp == truthvalue_true_node | |
4764 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4765 | && !TREE_OVERFLOW (orig_op1)) | |
4766 | || (ifexp == truthvalue_false_node | |
4767 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4768 | && !TREE_OVERFLOW (orig_op2))); | |
4769 | } | |
9f47c7e5 IE |
4770 | |
4771 | /* Need to convert condition operand into a vector mask. */ | |
4772 | if (VECTOR_TYPE_P (TREE_TYPE (ifexp))) | |
4773 | { | |
4774 | tree vectype = TREE_TYPE (ifexp); | |
4775 | tree elem_type = TREE_TYPE (vectype); | |
4776 | tree zero = build_int_cst (elem_type, 0); | |
4777 | tree zero_vec = build_vector_from_val (vectype, zero); | |
4778 | tree cmp_type = build_same_sized_truth_vector_type (vectype); | |
4779 | ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec); | |
4780 | } | |
4781 | ||
928c19bb | 4782 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) |
db3927fb | 4783 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); |
928c19bb JM |
4784 | else |
4785 | { | |
01c7ccbb JM |
4786 | if (int_operands) |
4787 | { | |
f34f1c87 MP |
4788 | /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be |
4789 | nested inside of the expression. */ | |
4790 | op1 = c_fully_fold (op1, false, NULL); | |
4791 | op2 = c_fully_fold (op2, false, NULL); | |
01c7ccbb | 4792 | } |
928c19bb JM |
4793 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); |
4794 | if (int_operands) | |
4795 | ret = note_integer_operands (ret); | |
4796 | } | |
2d2e923f MLI |
4797 | if (semantic_result_type) |
4798 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
928c19bb | 4799 | |
c2255bc4 | 4800 | protected_set_expr_location (ret, colon_loc); |
928c19bb | 4801 | return ret; |
3e4093b6 RS |
4802 | } |
4803 | \f | |
487a92fe | 4804 | /* Return a compound expression that performs two expressions and |
c2255bc4 AH |
4805 | returns the value of the second of them. |
4806 | ||
4807 | LOC is the location of the COMPOUND_EXPR. */ | |
400fbf9f | 4808 | |
3e4093b6 | 4809 | tree |
c2255bc4 | 4810 | build_compound_expr (location_t loc, tree expr1, tree expr2) |
3e4093b6 | 4811 | { |
4d84fe7c | 4812 | bool expr1_int_operands, expr2_int_operands; |
8ce94e44 | 4813 | tree eptype = NULL_TREE; |
928c19bb JM |
4814 | tree ret; |
4815 | ||
b72271b9 | 4816 | if (flag_cilkplus |
939b37da BI |
4817 | && (TREE_CODE (expr1) == CILK_SPAWN_STMT |
4818 | || TREE_CODE (expr2) == CILK_SPAWN_STMT)) | |
4819 | { | |
4820 | error_at (loc, | |
4821 | "spawned function call cannot be part of a comma expression"); | |
4822 | return error_mark_node; | |
4823 | } | |
4d84fe7c JM |
4824 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); |
4825 | if (expr1_int_operands) | |
4826 | expr1 = remove_c_maybe_const_expr (expr1); | |
4827 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4828 | if (expr2_int_operands) | |
4829 | expr2 = remove_c_maybe_const_expr (expr2); | |
4830 | ||
8ce94e44 JM |
4831 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) |
4832 | expr1 = TREE_OPERAND (expr1, 0); | |
4833 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4834 | { | |
4835 | eptype = TREE_TYPE (expr2); | |
4836 | expr2 = TREE_OPERAND (expr2, 0); | |
4837 | } | |
4838 | ||
3f75a254 | 4839 | if (!TREE_SIDE_EFFECTS (expr1)) |
3e4093b6 RS |
4840 | { |
4841 | /* The left-hand operand of a comma expression is like an expression | |
c5409249 | 4842 | statement: with -Wunused, we should warn if it doesn't have |
3e4093b6 | 4843 | any side-effects, unless it was explicitly cast to (void). */ |
e14a6540 | 4844 | if (warn_unused_value) |
47aecf47 | 4845 | { |
e14a6540 | 4846 | if (VOID_TYPE_P (TREE_TYPE (expr1)) |
1043771b | 4847 | && CONVERT_EXPR_P (expr1)) |
47aecf47 | 4848 | ; /* (void) a, b */ |
e14a6540 JM |
4849 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) |
4850 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
1043771b | 4851 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) |
47aecf47 JM |
4852 | ; /* (void) a, (void) b, c */ |
4853 | else | |
b8698a0f | 4854 | warning_at (loc, OPT_Wunused_value, |
c2255bc4 | 4855 | "left-hand operand of comma expression has no effect"); |
47aecf47 | 4856 | } |
3e4093b6 | 4857 | } |
789eadcd MP |
4858 | else if (TREE_CODE (expr1) == COMPOUND_EXPR |
4859 | && warn_unused_value) | |
4860 | { | |
4861 | tree r = expr1; | |
4862 | location_t cloc = loc; | |
4863 | while (TREE_CODE (r) == COMPOUND_EXPR) | |
4864 | { | |
4865 | if (EXPR_HAS_LOCATION (r)) | |
4866 | cloc = EXPR_LOCATION (r); | |
4867 | r = TREE_OPERAND (r, 1); | |
4868 | } | |
4869 | if (!TREE_SIDE_EFFECTS (r) | |
4870 | && !VOID_TYPE_P (TREE_TYPE (r)) | |
4871 | && !CONVERT_EXPR_P (r)) | |
4872 | warning_at (cloc, OPT_Wunused_value, | |
4873 | "right-hand operand of comma expression has no effect"); | |
4874 | } | |
400fbf9f | 4875 | |
3e4093b6 RS |
4876 | /* With -Wunused, we should also warn if the left-hand operand does have |
4877 | side-effects, but computes a value which is not used. For example, in | |
4878 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4879 | so we should issue a warning. */ | |
4880 | else if (warn_unused_value) | |
c2255bc4 | 4881 | warn_if_unused_value (expr1, loc); |
400fbf9f | 4882 | |
e63d6886 AP |
4883 | if (expr2 == error_mark_node) |
4884 | return error_mark_node; | |
4885 | ||
928c19bb JM |
4886 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
4887 | ||
4888 | if (flag_isoc99 | |
4d84fe7c JM |
4889 | && expr1_int_operands |
4890 | && expr2_int_operands) | |
928c19bb JM |
4891 | ret = note_integer_operands (ret); |
4892 | ||
8ce94e44 JM |
4893 | if (eptype) |
4894 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4895 | ||
c2255bc4 | 4896 | protected_set_expr_location (ret, loc); |
928c19bb | 4897 | return ret; |
3e4093b6 | 4898 | } |
400fbf9f | 4899 | |
67165eb3 ILT |
4900 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to |
4901 | which we are casting. OTYPE is the type of the expression being | |
2ee3cb35 MLI |
4902 | cast. Both TYPE and OTYPE are pointer types. LOC is the location |
4903 | of the cast. -Wcast-qual appeared on the command line. Named | |
4904 | address space qualifiers are not handled here, because they result | |
4905 | in different warnings. */ | |
67165eb3 ILT |
4906 | |
4907 | static void | |
2ee3cb35 | 4908 | handle_warn_cast_qual (location_t loc, tree type, tree otype) |
67165eb3 ILT |
4909 | { |
4910 | tree in_type = type; | |
4911 | tree in_otype = otype; | |
4912 | int added = 0; | |
4913 | int discarded = 0; | |
4914 | bool is_const; | |
4915 | ||
4916 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4917 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4918 | nodes is uninteresting and we stop as soon as we hit a | |
4919 | non-POINTER_TYPE node on either type. */ | |
4920 | do | |
4921 | { | |
4922 | in_otype = TREE_TYPE (in_otype); | |
4923 | in_type = TREE_TYPE (in_type); | |
4924 | ||
4925 | /* GNU C allows cv-qualified function types. 'const' means the | |
4926 | function is very pure, 'volatile' means it can't return. We | |
4927 | need to warn when such qualifiers are added, not when they're | |
4928 | taken away. */ | |
4929 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4930 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
36c5e70a BE |
4931 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) |
4932 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
67165eb3 | 4933 | else |
36c5e70a BE |
4934 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) |
4935 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
67165eb3 ILT |
4936 | } |
4937 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4938 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4939 | ||
4940 | if (added) | |
2ee3cb35 MLI |
4941 | warning_at (loc, OPT_Wcast_qual, |
4942 | "cast adds %q#v qualifier to function type", added); | |
67165eb3 ILT |
4943 | |
4944 | if (discarded) | |
4945 | /* There are qualifiers present in IN_OTYPE that are not present | |
4946 | in IN_TYPE. */ | |
2ee3cb35 | 4947 | warning_at (loc, OPT_Wcast_qual, |
7485aeea | 4948 | "cast discards %qv qualifier from pointer target type", |
2ee3cb35 | 4949 | discarded); |
67165eb3 ILT |
4950 | |
4951 | if (added || discarded) | |
4952 | return; | |
4953 | ||
4954 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4955 | const value to be changed with no additional warning. We only | |
4956 | issue this warning if T is the same on both sides, and we only | |
4957 | issue the warning if there are the same number of pointers on | |
4958 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4959 | cast is unsafe when a qualifier is added at one level and const | |
4960 | is not present at all outer levels. | |
4961 | ||
4962 | To issue this warning, we check at each level whether the cast | |
4963 | adds new qualifiers not already seen. We don't need to special | |
4964 | case function types, as they won't have the same | |
4965 | TYPE_MAIN_VARIANT. */ | |
4966 | ||
4967 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4968 | return; | |
4969 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4970 | return; | |
4971 | ||
4972 | in_type = type; | |
4973 | in_otype = otype; | |
4974 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4975 | do | |
4976 | { | |
4977 | in_type = TREE_TYPE (in_type); | |
4978 | in_otype = TREE_TYPE (in_otype); | |
4979 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4980 | && !is_const) | |
4981 | { | |
2ee3cb35 MLI |
4982 | warning_at (loc, OPT_Wcast_qual, |
4983 | "to be safe all intermediate pointers in cast from " | |
4984 | "%qT to %qT must be %<const%> qualified", | |
4985 | otype, type); | |
67165eb3 ILT |
4986 | break; |
4987 | } | |
4988 | if (is_const) | |
4989 | is_const = TYPE_READONLY (in_type); | |
4990 | } | |
4991 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4992 | } | |
4993 | ||
b8698a0f | 4994 | /* Build an expression representing a cast to type TYPE of expression EXPR. |
c2255bc4 | 4995 | LOC is the location of the cast-- typically the open paren of the cast. */ |
400fbf9f | 4996 | |
3e4093b6 | 4997 | tree |
c2255bc4 | 4998 | build_c_cast (location_t loc, tree type, tree expr) |
3e4093b6 | 4999 | { |
8ce94e44 JM |
5000 | tree value; |
5001 | ||
5002 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
5003 | expr = TREE_OPERAND (expr, 0); | |
5004 | ||
5005 | value = expr; | |
400fbf9f | 5006 | |
3e4093b6 RS |
5007 | if (type == error_mark_node || expr == error_mark_node) |
5008 | return error_mark_node; | |
400fbf9f | 5009 | |
3e4093b6 RS |
5010 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
5011 | only in <protocol> qualifications. But when constructing cast expressions, | |
5012 | the protocols do matter and must be kept around. */ | |
700686fa ZL |
5013 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) |
5014 | return build1 (NOP_EXPR, type, expr); | |
5015 | ||
5016 | type = TYPE_MAIN_VARIANT (type); | |
400fbf9f | 5017 | |
3e4093b6 RS |
5018 | if (TREE_CODE (type) == ARRAY_TYPE) |
5019 | { | |
c2255bc4 | 5020 | error_at (loc, "cast specifies array type"); |
3e4093b6 RS |
5021 | return error_mark_node; |
5022 | } | |
400fbf9f | 5023 | |
3e4093b6 RS |
5024 | if (TREE_CODE (type) == FUNCTION_TYPE) |
5025 | { | |
c2255bc4 | 5026 | error_at (loc, "cast specifies function type"); |
3e4093b6 RS |
5027 | return error_mark_node; |
5028 | } | |
400fbf9f | 5029 | |
808d6eaa JM |
5030 | if (!VOID_TYPE_P (type)) |
5031 | { | |
5032 | value = require_complete_type (value); | |
5033 | if (value == error_mark_node) | |
5034 | return error_mark_node; | |
5035 | } | |
5036 | ||
3e4093b6 RS |
5037 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
5038 | { | |
fcf73884 MLI |
5039 | if (TREE_CODE (type) == RECORD_TYPE |
5040 | || TREE_CODE (type) == UNION_TYPE) | |
c1771a20 | 5041 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 5042 | "ISO C forbids casting nonscalar to the same type"); |
c77935ee PP |
5043 | |
5044 | /* Convert to remove any qualifiers from VALUE's type. */ | |
5045 | value = convert (type, value); | |
3e4093b6 RS |
5046 | } |
5047 | else if (TREE_CODE (type) == UNION_TYPE) | |
5048 | { | |
5049 | tree field; | |
400fbf9f | 5050 | |
910ad8de | 5051 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
39ffbac9 VR |
5052 | if (TREE_TYPE (field) != error_mark_node |
5053 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
5054 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3e4093b6 RS |
5055 | break; |
5056 | ||
5057 | if (field) | |
400fbf9f | 5058 | { |
3e4093b6 | 5059 | tree t; |
e616f54d | 5060 | bool maybe_const = true; |
3e4093b6 | 5061 | |
c1771a20 | 5062 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); |
e616f54d JM |
5063 | t = c_fully_fold (value, false, &maybe_const); |
5064 | t = build_constructor_single (type, field, t); | |
5065 | if (!maybe_const) | |
5066 | t = c_wrap_maybe_const (t, true); | |
5067 | t = digest_init (loc, type, t, | |
bbbbb16a | 5068 | NULL_TREE, false, true, 0); |
3e4093b6 RS |
5069 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
5070 | return t; | |
400fbf9f | 5071 | } |
c2255bc4 | 5072 | error_at (loc, "cast to union type from type not present in union"); |
3e4093b6 RS |
5073 | return error_mark_node; |
5074 | } | |
5075 | else | |
5076 | { | |
5077 | tree otype, ovalue; | |
400fbf9f | 5078 | |
3e4093b6 | 5079 | if (type == void_type_node) |
c2255bc4 AH |
5080 | { |
5081 | tree t = build1 (CONVERT_EXPR, type, value); | |
5082 | SET_EXPR_LOCATION (t, loc); | |
5083 | return t; | |
5084 | } | |
400fbf9f | 5085 | |
3e4093b6 | 5086 | otype = TREE_TYPE (value); |
400fbf9f | 5087 | |
3e4093b6 | 5088 | /* Optionally warn about potentially worrisome casts. */ |
3e4093b6 RS |
5089 | if (warn_cast_qual |
5090 | && TREE_CODE (type) == POINTER_TYPE | |
5091 | && TREE_CODE (otype) == POINTER_TYPE) | |
2ee3cb35 | 5092 | handle_warn_cast_qual (loc, type, otype); |
400fbf9f | 5093 | |
36c5e70a BE |
5094 | /* Warn about conversions between pointers to disjoint |
5095 | address spaces. */ | |
5096 | if (TREE_CODE (type) == POINTER_TYPE | |
5097 | && TREE_CODE (otype) == POINTER_TYPE | |
5098 | && !null_pointer_constant_p (value)) | |
5099 | { | |
5100 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
5101 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
5102 | addr_space_t as_common; | |
5103 | ||
5104 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
5105 | { | |
5106 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
5107 | warning_at (loc, 0, "cast to %s address space pointer " | |
5108 | "from disjoint generic address space pointer", | |
5109 | c_addr_space_name (as_to)); | |
5110 | ||
5111 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
5112 | warning_at (loc, 0, "cast to generic address space pointer " | |
5113 | "from disjoint %s address space pointer", | |
5114 | c_addr_space_name (as_from)); | |
5115 | ||
5116 | else | |
5117 | warning_at (loc, 0, "cast to %s address space pointer " | |
5118 | "from disjoint %s address space pointer", | |
5119 | c_addr_space_name (as_to), | |
5120 | c_addr_space_name (as_from)); | |
5121 | } | |
5122 | } | |
5123 | ||
3e4093b6 | 5124 | /* Warn about possible alignment problems. */ |
3176a0c2 | 5125 | if (STRICT_ALIGNMENT |
3e4093b6 RS |
5126 | && TREE_CODE (type) == POINTER_TYPE |
5127 | && TREE_CODE (otype) == POINTER_TYPE | |
5128 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
5129 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
5130 | /* Don't warn about opaque types, where the actual alignment | |
5131 | restriction is unknown. */ | |
5132 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
5133 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
5134 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
5135 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
c2255bc4 AH |
5136 | warning_at (loc, OPT_Wcast_align, |
5137 | "cast increases required alignment of target type"); | |
e9a25f70 | 5138 | |
3176a0c2 | 5139 | if (TREE_CODE (type) == INTEGER_TYPE |
3e4093b6 | 5140 | && TREE_CODE (otype) == POINTER_TYPE |
8d1c7aef | 5141 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) |
c22cacf3 MS |
5142 | /* Unlike conversion of integers to pointers, where the |
5143 | warning is disabled for converting constants because | |
5144 | of cases such as SIG_*, warn about converting constant | |
5145 | pointers to integers. In some cases it may cause unwanted | |
8d1c7aef | 5146 | sign extension, and a warning is appropriate. */ |
c2255bc4 AH |
5147 | warning_at (loc, OPT_Wpointer_to_int_cast, |
5148 | "cast from pointer to integer of different size"); | |
400fbf9f | 5149 | |
3176a0c2 | 5150 | if (TREE_CODE (value) == CALL_EXPR |
3e4093b6 | 5151 | && TREE_CODE (type) != TREE_CODE (otype)) |
c2255bc4 AH |
5152 | warning_at (loc, OPT_Wbad_function_cast, |
5153 | "cast from function call of type %qT " | |
5154 | "to non-matching type %qT", otype, type); | |
400fbf9f | 5155 | |
3176a0c2 | 5156 | if (TREE_CODE (type) == POINTER_TYPE |
3e4093b6 RS |
5157 | && TREE_CODE (otype) == INTEGER_TYPE |
5158 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
5159 | /* Don't warn about converting any constant. */ | |
5160 | && !TREE_CONSTANT (value)) | |
c2255bc4 AH |
5161 | warning_at (loc, |
5162 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
5163 | "of different size"); | |
400fbf9f | 5164 | |
79bedddc SR |
5165 | if (warn_strict_aliasing <= 2) |
5166 | strict_aliasing_warning (otype, type, expr); | |
400fbf9f | 5167 | |
3897f229 JM |
5168 | /* If pedantic, warn for conversions between function and object |
5169 | pointer types, except for converting a null pointer constant | |
5170 | to function pointer type. */ | |
5171 | if (pedantic | |
5172 | && TREE_CODE (type) == POINTER_TYPE | |
5173 | && TREE_CODE (otype) == POINTER_TYPE | |
5174 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
5175 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
c1771a20 | 5176 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 5177 | "conversion of function pointer to object pointer type"); |
3897f229 JM |
5178 | |
5179 | if (pedantic | |
5180 | && TREE_CODE (type) == POINTER_TYPE | |
5181 | && TREE_CODE (otype) == POINTER_TYPE | |
5182 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
5183 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
6aa3c60d | 5184 | && !null_pointer_constant_p (value)) |
c1771a20 | 5185 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 5186 | "conversion of object pointer to function pointer type"); |
3897f229 | 5187 | |
3e4093b6 | 5188 | ovalue = value; |
3e4093b6 | 5189 | value = convert (type, value); |
400fbf9f | 5190 | |
3e4093b6 | 5191 | /* Ignore any integer overflow caused by the cast. */ |
928c19bb | 5192 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) |
3e4093b6 | 5193 | { |
8bcd6380 | 5194 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) |
6414bad6 | 5195 | { |
8bcd6380 RS |
5196 | if (!TREE_OVERFLOW (value)) |
5197 | { | |
5198 | /* Avoid clobbering a shared constant. */ | |
5199 | value = copy_node (value); | |
5200 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
5201 | } | |
6414bad6 | 5202 | } |
8bcd6380 | 5203 | else if (TREE_OVERFLOW (value)) |
d8e1f97b | 5204 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ |
807e902e | 5205 | value = wide_int_to_tree (TREE_TYPE (value), value); |
3e4093b6 RS |
5206 | } |
5207 | } | |
400fbf9f | 5208 | |
53cd18ec | 5209 | /* Don't let a cast be an lvalue. */ |
9482b620 | 5210 | if (lvalue_p (value)) |
db3927fb | 5211 | value = non_lvalue_loc (loc, value); |
e9a25f70 | 5212 | |
928c19bb JM |
5213 | /* Don't allow the results of casting to floating-point or complex |
5214 | types be confused with actual constants, or casts involving | |
5215 | integer and pointer types other than direct integer-to-integer | |
5216 | and integer-to-pointer be confused with integer constant | |
5217 | expressions and null pointer constants. */ | |
5218 | if (TREE_CODE (value) == REAL_CST | |
5219 | || TREE_CODE (value) == COMPLEX_CST | |
5220 | || (TREE_CODE (value) == INTEGER_CST | |
5221 | && !((TREE_CODE (expr) == INTEGER_CST | |
5222 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
5223 | || TREE_CODE (expr) == REAL_CST | |
5224 | || TREE_CODE (expr) == COMPLEX_CST))) | |
5225 | value = build1 (NOP_EXPR, type, value); | |
5226 | ||
21ba0cea | 5227 | protected_set_expr_location (value, loc); |
3e4093b6 | 5228 | return value; |
400fbf9f JW |
5229 | } |
5230 | ||
c2255bc4 AH |
5231 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the |
5232 | location of the open paren of the cast, or the position of the cast | |
5233 | expr. */ | |
3e4093b6 | 5234 | tree |
c2255bc4 | 5235 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) |
400fbf9f | 5236 | { |
f8893e47 | 5237 | tree type; |
928c19bb JM |
5238 | tree type_expr = NULL_TREE; |
5239 | bool type_expr_const = true; | |
5240 | tree ret; | |
3e4093b6 | 5241 | int saved_wsp = warn_strict_prototypes; |
c5c76735 | 5242 | |
3e4093b6 RS |
5243 | /* This avoids warnings about unprototyped casts on |
5244 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
5245 | if (TREE_CODE (expr) == INTEGER_CST) | |
5246 | warn_strict_prototypes = 0; | |
928c19bb | 5247 | type = groktypename (type_name, &type_expr, &type_expr_const); |
3e4093b6 | 5248 | warn_strict_prototypes = saved_wsp; |
c5c76735 | 5249 | |
1807ffc1 MS |
5250 | if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type) |
5251 | && reject_gcc_builtin (expr)) | |
5252 | return error_mark_node; | |
5253 | ||
c2255bc4 | 5254 | ret = build_c_cast (loc, type, expr); |
928c19bb JM |
5255 | if (type_expr) |
5256 | { | |
9f33203d JM |
5257 | bool inner_expr_const = true; |
5258 | ret = c_fully_fold (ret, require_constant_value, &inner_expr_const); | |
928c19bb | 5259 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); |
9f33203d JM |
5260 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const |
5261 | && inner_expr_const); | |
c2255bc4 | 5262 | SET_EXPR_LOCATION (ret, loc); |
928c19bb | 5263 | } |
24b97832 | 5264 | |
21ba0cea MP |
5265 | if (!EXPR_HAS_LOCATION (ret)) |
5266 | protected_set_expr_location (ret, loc); | |
24b97832 | 5267 | |
9e5b2115 PB |
5268 | /* C++ does not permits types to be defined in a cast, but it |
5269 | allows references to incomplete types. */ | |
5270 | if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) | |
24b97832 ILT |
5271 | warning_at (loc, OPT_Wc___compat, |
5272 | "defining a type in a cast is invalid in C++"); | |
5273 | ||
928c19bb | 5274 | return ret; |
400fbf9f | 5275 | } |
3e4093b6 RS |
5276 | \f |
5277 | /* Build an assignment expression of lvalue LHS from value RHS. | |
32e8bb8e ILT |
5278 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which |
5279 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
3e4093b6 RS |
5280 | MODIFYCODE is the code for a binary operator that we use |
5281 | to combine the old value of LHS with RHS to get the new value. | |
c9f9eb5d | 5282 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
bbbbb16a ILT |
5283 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, |
5284 | which may differ from TREE_TYPE (RHS) for an enum value. | |
c9f9eb5d | 5285 | |
c2255bc4 AH |
5286 | LOCATION is the location of the MODIFYCODE operator. |
5287 | RHS_LOC is the location of the RHS. */ | |
2f6e4e97 | 5288 | |
3e4093b6 | 5289 | tree |
32e8bb8e | 5290 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, |
b8698a0f | 5291 | enum tree_code modifycode, |
c2255bc4 | 5292 | location_t rhs_loc, tree rhs, tree rhs_origtype) |
400fbf9f | 5293 | { |
3e4093b6 RS |
5294 | tree result; |
5295 | tree newrhs; | |
241f845a | 5296 | tree rhseval = NULL_TREE; |
8ce94e44 | 5297 | tree rhs_semantic_type = NULL_TREE; |
3e4093b6 RS |
5298 | tree lhstype = TREE_TYPE (lhs); |
5299 | tree olhstype = lhstype; | |
928c19bb | 5300 | bool npc; |
267bac10 | 5301 | bool is_atomic_op; |
e9a25f70 | 5302 | |
3e4093b6 RS |
5303 | /* Types that aren't fully specified cannot be used in assignments. */ |
5304 | lhs = require_complete_type (lhs); | |
e9a25f70 | 5305 | |
3e4093b6 RS |
5306 | /* Avoid duplicate error messages from operands that had errors. */ |
5307 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
5308 | return error_mark_node; | |
400fbf9f | 5309 | |
4c2ecab0 JM |
5310 | /* Ensure an error for assigning a non-lvalue array to an array in |
5311 | C90. */ | |
5312 | if (TREE_CODE (lhstype) == ARRAY_TYPE) | |
5313 | { | |
5314 | error_at (location, "assignment to expression with array type"); | |
5315 | return error_mark_node; | |
5316 | } | |
5317 | ||
46a88c12 | 5318 | /* For ObjC properties, defer this check. */ |
7bd11157 | 5319 | if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) |
c0bcacec VR |
5320 | return error_mark_node; |
5321 | ||
267bac10 JM |
5322 | is_atomic_op = really_atomic_lvalue (lhs); |
5323 | ||
8ce94e44 JM |
5324 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5325 | { | |
5326 | rhs_semantic_type = TREE_TYPE (rhs); | |
5327 | rhs = TREE_OPERAND (rhs, 0); | |
5328 | } | |
5329 | ||
3e4093b6 | 5330 | newrhs = rhs; |
400fbf9f | 5331 | |
928c19bb JM |
5332 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) |
5333 | { | |
5334 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
c2255bc4 | 5335 | lhs_origtype, modifycode, rhs_loc, rhs, |
32e8bb8e | 5336 | rhs_origtype); |
928c19bb JM |
5337 | if (inner == error_mark_node) |
5338 | return error_mark_node; | |
5339 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
5340 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
5341 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
5342 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
5343 | protected_set_expr_location (result, location); | |
5344 | return result; | |
5345 | } | |
5346 | ||
3e4093b6 RS |
5347 | /* If a binary op has been requested, combine the old LHS value with the RHS |
5348 | producing the value we should actually store into the LHS. */ | |
5349 | ||
5350 | if (modifycode != NOP_EXPR) | |
400fbf9f | 5351 | { |
928c19bb | 5352 | lhs = c_fully_fold (lhs, false, NULL); |
3e4093b6 | 5353 | lhs = stabilize_reference (lhs); |
bbbbb16a | 5354 | |
267bac10 JM |
5355 | /* Construct the RHS for any non-atomic compound assignemnt. */ |
5356 | if (!is_atomic_op) | |
5357 | { | |
241f845a JJ |
5358 | /* If in LHS op= RHS the RHS has side-effects, ensure they |
5359 | are preevaluated before the rest of the assignment expression's | |
5360 | side-effects, because RHS could contain e.g. function calls | |
5361 | that modify LHS. */ | |
5362 | if (TREE_SIDE_EFFECTS (rhs)) | |
5363 | { | |
5364 | newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs); | |
5365 | rhseval = newrhs; | |
5366 | } | |
267bac10 | 5367 | newrhs = build_binary_op (location, |
241f845a | 5368 | modifycode, lhs, newrhs, 1); |
267bac10 JM |
5369 | |
5370 | /* The original type of the right hand side is no longer | |
5371 | meaningful. */ | |
5372 | rhs_origtype = NULL_TREE; | |
5373 | } | |
400fbf9f | 5374 | } |
400fbf9f | 5375 | |
668ea4b1 IS |
5376 | if (c_dialect_objc ()) |
5377 | { | |
46a88c12 NP |
5378 | /* Check if we are modifying an Objective-C property reference; |
5379 | if so, we need to generate setter calls. */ | |
5380 | result = objc_maybe_build_modify_expr (lhs, newrhs); | |
668ea4b1 | 5381 | if (result) |
241f845a | 5382 | goto return_result; |
46a88c12 NP |
5383 | |
5384 | /* Else, do the check that we postponed for Objective-C. */ | |
7bd11157 | 5385 | if (!lvalue_or_else (location, lhs, lv_assign)) |
668ea4b1 IS |
5386 | return error_mark_node; |
5387 | } | |
5388 | ||
9bf24266 | 5389 | /* Give an error for storing in something that is 'const'. */ |
bbbd6700 | 5390 | |
f37acdf9 | 5391 | if (TYPE_READONLY (lhstype) |
3e4093b6 RS |
5392 | || ((TREE_CODE (lhstype) == RECORD_TYPE |
5393 | || TREE_CODE (lhstype) == UNION_TYPE) | |
5394 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
953ff289 | 5395 | { |
c02065fc | 5396 | readonly_error (location, lhs, lv_assign); |
953ff289 DN |
5397 | return error_mark_node; |
5398 | } | |
f37acdf9 JM |
5399 | else if (TREE_READONLY (lhs)) |
5400 | readonly_warning (lhs, lv_assign); | |
bbbd6700 | 5401 | |
3e4093b6 RS |
5402 | /* If storing into a structure or union member, |
5403 | it has probably been given type `int'. | |
5404 | Compute the type that would go with | |
5405 | the actual amount of storage the member occupies. */ | |
bbbd6700 | 5406 | |
3e4093b6 RS |
5407 | if (TREE_CODE (lhs) == COMPONENT_REF |
5408 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
5409 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
5410 | || TREE_CODE (lhstype) == REAL_TYPE | |
5411 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
5412 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
400fbf9f | 5413 | |
3e4093b6 RS |
5414 | /* If storing in a field that is in actuality a short or narrower than one, |
5415 | we must store in the field in its actual type. */ | |
5416 | ||
5417 | if (lhstype != TREE_TYPE (lhs)) | |
5418 | { | |
5419 | lhs = copy_node (lhs); | |
5420 | TREE_TYPE (lhs) = lhstype; | |
400fbf9f | 5421 | } |
400fbf9f | 5422 | |
32e8bb8e ILT |
5423 | /* Issue -Wc++-compat warnings about an assignment to an enum type |
5424 | when LHS does not have its original type. This happens for, | |
5425 | e.g., an enum bitfield in a struct. */ | |
5426 | if (warn_cxx_compat | |
5427 | && lhs_origtype != NULL_TREE | |
5428 | && lhs_origtype != lhstype | |
5429 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
5430 | { | |
5431 | tree checktype = (rhs_origtype != NULL_TREE | |
5432 | ? rhs_origtype | |
5433 | : TREE_TYPE (rhs)); | |
5434 | if (checktype != error_mark_node | |
267bac10 JM |
5435 | && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype) |
5436 | || (is_atomic_op && modifycode != NOP_EXPR))) | |
32e8bb8e ILT |
5437 | warning_at (location, OPT_Wc___compat, |
5438 | "enum conversion in assignment is invalid in C++"); | |
5439 | } | |
5440 | ||
267bac10 JM |
5441 | /* If the lhs is atomic, remove that qualifier. */ |
5442 | if (is_atomic_op) | |
5443 | { | |
5444 | lhstype = build_qualified_type (lhstype, | |
5445 | (TYPE_QUALS (lhstype) | |
5446 | & ~TYPE_QUAL_ATOMIC)); | |
5447 | olhstype = build_qualified_type (olhstype, | |
5448 | (TYPE_QUALS (lhstype) | |
5449 | & ~TYPE_QUAL_ATOMIC)); | |
5450 | } | |
5451 | ||
8ce94e44 JM |
5452 | /* Convert new value to destination type. Fold it first, then |
5453 | restore any excess precision information, for the sake of | |
5454 | conversion warnings. */ | |
400fbf9f | 5455 | |
267bac10 JM |
5456 | if (!(is_atomic_op && modifycode != NOP_EXPR)) |
5457 | { | |
5458 | npc = null_pointer_constant_p (newrhs); | |
5459 | newrhs = c_fully_fold (newrhs, false, NULL); | |
5460 | if (rhs_semantic_type) | |
5461 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
68fca595 MP |
5462 | newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs, |
5463 | rhs_origtype, ic_assign, npc, | |
5464 | NULL_TREE, NULL_TREE, 0); | |
267bac10 JM |
5465 | if (TREE_CODE (newrhs) == ERROR_MARK) |
5466 | return error_mark_node; | |
5467 | } | |
400fbf9f | 5468 | |
6e955430 ZL |
5469 | /* Emit ObjC write barrier, if necessary. */ |
5470 | if (c_dialect_objc () && flag_objc_gc) | |
5471 | { | |
5472 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
5473 | if (result) | |
c9f9eb5d AH |
5474 | { |
5475 | protected_set_expr_location (result, location); | |
241f845a | 5476 | goto return_result; |
c9f9eb5d | 5477 | } |
6e955430 ZL |
5478 | } |
5479 | ||
ea4b7848 | 5480 | /* Scan operands. */ |
400fbf9f | 5481 | |
267bac10 JM |
5482 | if (is_atomic_op) |
5483 | result = build_atomic_assign (location, lhs, modifycode, newrhs, false); | |
5484 | else | |
5485 | { | |
5486 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); | |
5487 | TREE_SIDE_EFFECTS (result) = 1; | |
5488 | protected_set_expr_location (result, location); | |
5489 | } | |
400fbf9f | 5490 | |
3e4093b6 RS |
5491 | /* If we got the LHS in a different type for storing in, |
5492 | convert the result back to the nominal type of LHS | |
5493 | so that the value we return always has the same type | |
5494 | as the LHS argument. */ | |
e855c5ce | 5495 | |
3e4093b6 | 5496 | if (olhstype == TREE_TYPE (result)) |
241f845a | 5497 | goto return_result; |
c9f9eb5d | 5498 | |
68fca595 MP |
5499 | result = convert_for_assignment (location, rhs_loc, olhstype, result, |
5500 | rhs_origtype, ic_assign, false, NULL_TREE, | |
5501 | NULL_TREE, 0); | |
c9f9eb5d | 5502 | protected_set_expr_location (result, location); |
241f845a JJ |
5503 | |
5504 | return_result: | |
5505 | if (rhseval) | |
5506 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result); | |
c9f9eb5d | 5507 | return result; |
3e4093b6 RS |
5508 | } |
5509 | \f | |
478a1c5b ILT |
5510 | /* Return whether STRUCT_TYPE has an anonymous field with type TYPE. |
5511 | This is used to implement -fplan9-extensions. */ | |
5512 | ||
5513 | static bool | |
5514 | find_anonymous_field_with_type (tree struct_type, tree type) | |
5515 | { | |
5516 | tree field; | |
5517 | bool found; | |
5518 | ||
5519 | gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE | |
5520 | || TREE_CODE (struct_type) == UNION_TYPE); | |
5521 | found = false; | |
5522 | for (field = TYPE_FIELDS (struct_type); | |
5523 | field != NULL_TREE; | |
5524 | field = TREE_CHAIN (field)) | |
5525 | { | |
267bac10 JM |
5526 | tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) |
5527 | ? c_build_qualified_type (TREE_TYPE (field), | |
5528 | TYPE_QUAL_ATOMIC) | |
5529 | : TYPE_MAIN_VARIANT (TREE_TYPE (field))); | |
478a1c5b | 5530 | if (DECL_NAME (field) == NULL |
267bac10 | 5531 | && comptypes (type, fieldtype)) |
478a1c5b ILT |
5532 | { |
5533 | if (found) | |
5534 | return false; | |
5535 | found = true; | |
5536 | } | |
5537 | else if (DECL_NAME (field) == NULL | |
5538 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
5539 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
5540 | && find_anonymous_field_with_type (TREE_TYPE (field), type)) | |
5541 | { | |
5542 | if (found) | |
5543 | return false; | |
5544 | found = true; | |
5545 | } | |
5546 | } | |
5547 | return found; | |
5548 | } | |
5549 | ||
5550 | /* RHS is an expression whose type is pointer to struct. If there is | |
5551 | an anonymous field in RHS with type TYPE, then return a pointer to | |
5552 | that field in RHS. This is used with -fplan9-extensions. This | |
5553 | returns NULL if no conversion could be found. */ | |
5554 | ||
5555 | static tree | |
5556 | convert_to_anonymous_field (location_t location, tree type, tree rhs) | |
5557 | { | |
5558 | tree rhs_struct_type, lhs_main_type; | |
5559 | tree field, found_field; | |
5560 | bool found_sub_field; | |
5561 | tree ret; | |
5562 | ||
5563 | gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); | |
5564 | rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); | |
5565 | gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE | |
5566 | || TREE_CODE (rhs_struct_type) == UNION_TYPE); | |
5567 | ||
5568 | gcc_assert (POINTER_TYPE_P (type)); | |
267bac10 JM |
5569 | lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type)) |
5570 | ? c_build_qualified_type (TREE_TYPE (type), | |
5571 | TYPE_QUAL_ATOMIC) | |
5572 | : TYPE_MAIN_VARIANT (TREE_TYPE (type))); | |
478a1c5b ILT |
5573 | |
5574 | found_field = NULL_TREE; | |
5575 | found_sub_field = false; | |
5576 | for (field = TYPE_FIELDS (rhs_struct_type); | |
5577 | field != NULL_TREE; | |
5578 | field = TREE_CHAIN (field)) | |
5579 | { | |
5580 | if (DECL_NAME (field) != NULL_TREE | |
5581 | || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE | |
5582 | && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)) | |
5583 | continue; | |
267bac10 JM |
5584 | tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) |
5585 | ? c_build_qualified_type (TREE_TYPE (field), | |
5586 | TYPE_QUAL_ATOMIC) | |
5587 | : TYPE_MAIN_VARIANT (TREE_TYPE (field))); | |
5588 | if (comptypes (lhs_main_type, fieldtype)) | |
478a1c5b ILT |
5589 | { |
5590 | if (found_field != NULL_TREE) | |
5591 | return NULL_TREE; | |
5592 | found_field = field; | |
5593 | } | |
5594 | else if (find_anonymous_field_with_type (TREE_TYPE (field), | |
5595 | lhs_main_type)) | |
5596 | { | |
5597 | if (found_field != NULL_TREE) | |
5598 | return NULL_TREE; | |
5599 | found_field = field; | |
5600 | found_sub_field = true; | |
5601 | } | |
5602 | } | |
5603 | ||
5604 | if (found_field == NULL_TREE) | |
5605 | return NULL_TREE; | |
5606 | ||
5607 | ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), | |
5608 | build_fold_indirect_ref (rhs), found_field, | |
5609 | NULL_TREE); | |
5610 | ret = build_fold_addr_expr_loc (location, ret); | |
5611 | ||
5612 | if (found_sub_field) | |
5613 | { | |
5614 | ret = convert_to_anonymous_field (location, type, ret); | |
5615 | gcc_assert (ret != NULL_TREE); | |
5616 | } | |
5617 | ||
5618 | return ret; | |
5619 | } | |
5620 | ||
63bc4e87 MP |
5621 | /* Issue an error message for a bad initializer component. |
5622 | GMSGID identifies the message. | |
5623 | The component name is taken from the spelling stack. */ | |
5624 | ||
5625 | static void | |
ea58ef42 | 5626 | error_init (location_t loc, const char *gmsgid) |
63bc4e87 MP |
5627 | { |
5628 | char *ofwhat; | |
5629 | ||
5630 | /* The gmsgid may be a format string with %< and %>. */ | |
ea58ef42 | 5631 | error_at (loc, gmsgid); |
63bc4e87 MP |
5632 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
5633 | if (*ofwhat) | |
d7ff7ae5 | 5634 | inform (loc, "(near initialization for %qs)", ofwhat); |
63bc4e87 MP |
5635 | } |
5636 | ||
5637 | /* Issue a pedantic warning for a bad initializer component. OPT is | |
5638 | the option OPT_* (from options.h) controlling this warning or 0 if | |
5639 | it is unconditionally given. GMSGID identifies the message. The | |
5640 | component name is taken from the spelling stack. */ | |
5641 | ||
5642 | static void | |
5643 | pedwarn_init (location_t location, int opt, const char *gmsgid) | |
5644 | { | |
5645 | char *ofwhat; | |
d7ff7ae5 | 5646 | bool warned; |
63bc4e87 MP |
5647 | |
5648 | /* The gmsgid may be a format string with %< and %>. */ | |
d7ff7ae5 | 5649 | warned = pedwarn (location, opt, gmsgid); |
63bc4e87 | 5650 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
d7ff7ae5 MP |
5651 | if (*ofwhat && warned) |
5652 | inform (location, "(near initialization for %qs)", ofwhat); | |
63bc4e87 MP |
5653 | } |
5654 | ||
5655 | /* Issue a warning for a bad initializer component. | |
5656 | ||
5657 | OPT is the OPT_W* value corresponding to the warning option that | |
5658 | controls this warning. GMSGID identifies the message. The | |
5659 | component name is taken from the spelling stack. */ | |
5660 | ||
5661 | static void | |
5662 | warning_init (location_t loc, int opt, const char *gmsgid) | |
5663 | { | |
5664 | char *ofwhat; | |
d7ff7ae5 | 5665 | bool warned; |
63bc4e87 MP |
5666 | |
5667 | /* The gmsgid may be a format string with %< and %>. */ | |
d7ff7ae5 | 5668 | warned = warning_at (loc, opt, gmsgid); |
63bc4e87 | 5669 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
d7ff7ae5 MP |
5670 | if (*ofwhat && warned) |
5671 | inform (loc, "(near initialization for %qs)", ofwhat); | |
63bc4e87 MP |
5672 | } |
5673 | \f | |
5674 | /* If TYPE is an array type and EXPR is a parenthesized string | |
5675 | constant, warn if pedantic that EXPR is being used to initialize an | |
5676 | object of type TYPE. */ | |
5677 | ||
5678 | void | |
d033409e | 5679 | maybe_warn_string_init (location_t loc, tree type, struct c_expr expr) |
63bc4e87 MP |
5680 | { |
5681 | if (pedantic | |
5682 | && TREE_CODE (type) == ARRAY_TYPE | |
5683 | && TREE_CODE (expr.value) == STRING_CST | |
5684 | && expr.original_code != STRING_CST) | |
d033409e | 5685 | pedwarn_init (loc, OPT_Wpedantic, |
63bc4e87 MP |
5686 | "array initialized from parenthesized string constant"); |
5687 | } | |
5688 | ||
bbbbb16a ILT |
5689 | /* Convert value RHS to type TYPE as preparation for an assignment to |
5690 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
5691 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
5692 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
5693 | constant before any folding. | |
3e4093b6 RS |
5694 | The real work of conversion is done by `convert'. |
5695 | The purpose of this function is to generate error messages | |
5696 | for assignments that are not allowed in C. | |
2ac2f164 JM |
5697 | ERRTYPE says whether it is argument passing, assignment, |
5698 | initialization or return. | |
2f6e4e97 | 5699 | |
aabef2de MP |
5700 | In the following example, '~' denotes where EXPR_LOC and '^' where |
5701 | LOCATION point to: | |
5702 | ||
5703 | f (var); [ic_argpass] | |
5704 | ^ ~~~ | |
5705 | x = var; [ic_assign] | |
5706 | ^ ~~~; | |
5707 | int x = var; [ic_init] | |
5708 | ^^^ | |
5709 | return x; [ic_return] | |
5710 | ^ | |
5711 | ||
2ac2f164 | 5712 | FUNCTION is a tree for the function being called. |
3e4093b6 | 5713 | PARMNUM is the number of the argument, for printing in error messages. */ |
cb3ca04e | 5714 | |
3e4093b6 | 5715 | static tree |
81e5eca8 | 5716 | convert_for_assignment (location_t location, location_t expr_loc, tree type, |
68fca595 | 5717 | tree rhs, tree origtype, enum impl_conv errtype, |
744aa42f ILT |
5718 | bool null_pointer_constant, tree fundecl, |
5719 | tree function, int parmnum) | |
3e4093b6 RS |
5720 | { |
5721 | enum tree_code codel = TREE_CODE (type); | |
8ce94e44 | 5722 | tree orig_rhs = rhs; |
3e4093b6 RS |
5723 | tree rhstype; |
5724 | enum tree_code coder; | |
2ac2f164 | 5725 | tree rname = NULL_TREE; |
58393038 | 5726 | bool objc_ok = false; |
2ac2f164 | 5727 | |
1c7485af MP |
5728 | /* Use the expansion point location to handle cases such as user's |
5729 | function returning a wrong-type macro defined in a system header. */ | |
5730 | location = expansion_point_location_if_in_system_header (location); | |
5731 | ||
6b4ef5c1 | 5732 | if (errtype == ic_argpass) |
2ac2f164 JM |
5733 | { |
5734 | tree selector; | |
5735 | /* Change pointer to function to the function itself for | |
5736 | diagnostics. */ | |
5737 | if (TREE_CODE (function) == ADDR_EXPR | |
5738 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
5739 | function = TREE_OPERAND (function, 0); | |
5740 | ||
5741 | /* Handle an ObjC selector specially for diagnostics. */ | |
5742 | selector = objc_message_selector (); | |
5743 | rname = function; | |
5744 | if (selector && parmnum > 2) | |
5745 | { | |
5746 | rname = selector; | |
5747 | parmnum -= 2; | |
5748 | } | |
5749 | } | |
5750 | ||
5751 | /* This macro is used to emit diagnostics to ensure that all format | |
5752 | strings are complete sentences, visible to gettext and checked at | |
5753 | compile time. */ | |
768952be | 5754 | #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \ |
1e053dfe MLI |
5755 | do { \ |
5756 | switch (errtype) \ | |
5757 | { \ | |
5758 | case ic_argpass: \ | |
5c1bc275 | 5759 | if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \ |
c2255bc4 | 5760 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5c1bc275 | 5761 | ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ |
1e053dfe MLI |
5762 | "expected %qT but argument is of type %qT", \ |
5763 | type, rhstype); \ | |
5764 | break; \ | |
1e053dfe MLI |
5765 | case ic_assign: \ |
5766 | pedwarn (LOCATION, OPT, AS); \ | |
5767 | break; \ | |
5768 | case ic_init: \ | |
6a8f4e12 | 5769 | pedwarn_init (LOCATION, OPT, IN); \ |
1e053dfe MLI |
5770 | break; \ |
5771 | case ic_return: \ | |
d033409e | 5772 | pedwarn (LOCATION, OPT, RE); \ |
1e053dfe MLI |
5773 | break; \ |
5774 | default: \ | |
5775 | gcc_unreachable (); \ | |
5776 | } \ | |
2ac2f164 | 5777 | } while (0) |
cb3ca04e | 5778 | |
49706e39 MLI |
5779 | /* This macro is used to emit diagnostics to ensure that all format |
5780 | strings are complete sentences, visible to gettext and checked at | |
768952be | 5781 | compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an |
49706e39 | 5782 | extra parameter to enumerate qualifiers. */ |
768952be | 5783 | #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ |
49706e39 MLI |
5784 | do { \ |
5785 | switch (errtype) \ | |
5786 | { \ | |
5787 | case ic_argpass: \ | |
5c1bc275 | 5788 | if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \ |
49706e39 | 5789 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5c1bc275 | 5790 | ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ |
49706e39 MLI |
5791 | "expected %qT but argument is of type %qT", \ |
5792 | type, rhstype); \ | |
5793 | break; \ | |
5794 | case ic_assign: \ | |
5c1bc275 | 5795 | pedwarn (LOCATION, OPT, AS, QUALS); \ |
49706e39 MLI |
5796 | break; \ |
5797 | case ic_init: \ | |
5c1bc275 | 5798 | pedwarn (LOCATION, OPT, IN, QUALS); \ |
49706e39 MLI |
5799 | break; \ |
5800 | case ic_return: \ | |
5c1bc275 | 5801 | pedwarn (LOCATION, OPT, RE, QUALS); \ |
49706e39 MLI |
5802 | break; \ |
5803 | default: \ | |
5804 | gcc_unreachable (); \ | |
5805 | } \ | |
5806 | } while (0) | |
5807 | ||
768952be MU |
5808 | /* This macro is used to emit diagnostics to ensure that all format |
5809 | strings are complete sentences, visible to gettext and checked at | |
5810 | compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses | |
5811 | warning_at instead of pedwarn. */ | |
5812 | #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ | |
5813 | do { \ | |
5814 | switch (errtype) \ | |
5815 | { \ | |
5816 | case ic_argpass: \ | |
5817 | if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \ | |
5818 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ | |
5819 | ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ | |
5820 | "expected %qT but argument is of type %qT", \ | |
5821 | type, rhstype); \ | |
5822 | break; \ | |
5823 | case ic_assign: \ | |
5824 | warning_at (LOCATION, OPT, AS, QUALS); \ | |
5825 | break; \ | |
5826 | case ic_init: \ | |
5827 | warning_at (LOCATION, OPT, IN, QUALS); \ | |
5828 | break; \ | |
5829 | case ic_return: \ | |
5830 | warning_at (LOCATION, OPT, RE, QUALS); \ | |
5831 | break; \ | |
5832 | default: \ | |
5833 | gcc_unreachable (); \ | |
5834 | } \ | |
5835 | } while (0) | |
5836 | ||
8ce94e44 JM |
5837 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5838 | rhs = TREE_OPERAND (rhs, 0); | |
5839 | ||
3e4093b6 RS |
5840 | rhstype = TREE_TYPE (rhs); |
5841 | coder = TREE_CODE (rhstype); | |
5842 | ||
5843 | if (coder == ERROR_MARK) | |
5844 | return error_mark_node; | |
5845 | ||
58393038 ZL |
5846 | if (c_dialect_objc ()) |
5847 | { | |
5848 | int parmno; | |
5849 | ||
5850 | switch (errtype) | |
5851 | { | |
5852 | case ic_return: | |
5853 | parmno = 0; | |
5854 | break; | |
5855 | ||
5856 | case ic_assign: | |
5857 | parmno = -1; | |
5858 | break; | |
5859 | ||
5860 | case ic_init: | |
5861 | parmno = -2; | |
5862 | break; | |
5863 | ||
5864 | default: | |
5865 | parmno = parmnum; | |
5866 | break; | |
5867 | } | |
5868 | ||
5869 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
5870 | } | |
5871 | ||
bbbbb16a ILT |
5872 | if (warn_cxx_compat) |
5873 | { | |
5874 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
5875 | if (checktype != error_mark_node | |
5876 | && TREE_CODE (type) == ENUMERAL_TYPE | |
5877 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
5878 | { | |
768952be MU |
5879 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat, |
5880 | G_("enum conversion when passing argument " | |
5881 | "%d of %qE is invalid in C++"), | |
5882 | G_("enum conversion in assignment is " | |
5883 | "invalid in C++"), | |
5884 | G_("enum conversion in initialization is " | |
5885 | "invalid in C++"), | |
5886 | G_("enum conversion in return is " | |
5887 | "invalid in C++")); | |
bbbbb16a ILT |
5888 | } |
5889 | } | |
5890 | ||
3e4093b6 | 5891 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) |
59c0753d | 5892 | return rhs; |
3e4093b6 RS |
5893 | |
5894 | if (coder == VOID_TYPE) | |
400fbf9f | 5895 | { |
6dcc04b0 JM |
5896 | /* Except for passing an argument to an unprototyped function, |
5897 | this is a constraint violation. When passing an argument to | |
5898 | an unprototyped function, it is compile-time undefined; | |
5899 | making it a constraint in that case was rejected in | |
5900 | DR#252. */ | |
c2255bc4 | 5901 | error_at (location, "void value not ignored as it ought to be"); |
3e4093b6 | 5902 | return error_mark_node; |
400fbf9f | 5903 | } |
808d6eaa JM |
5904 | rhs = require_complete_type (rhs); |
5905 | if (rhs == error_mark_node) | |
5906 | return error_mark_node; | |
1807ffc1 MS |
5907 | |
5908 | if (coder == POINTER_TYPE && reject_gcc_builtin (rhs)) | |
5909 | return error_mark_node; | |
5910 | ||
cd192ccc MS |
5911 | /* A non-reference type can convert to a reference. This handles |
5912 | va_start, va_copy and possibly port built-ins. */ | |
5913 | if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) | |
400fbf9f | 5914 | { |
3e4093b6 | 5915 | if (!lvalue_p (rhs)) |
400fbf9f | 5916 | { |
c2255bc4 | 5917 | error_at (location, "cannot pass rvalue to reference parameter"); |
3e4093b6 | 5918 | return error_mark_node; |
400fbf9f | 5919 | } |
3e4093b6 RS |
5920 | if (!c_mark_addressable (rhs)) |
5921 | return error_mark_node; | |
5922 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
c2255bc4 | 5923 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5924 | |
81e5eca8 | 5925 | rhs = convert_for_assignment (location, expr_loc, |
68fca595 MP |
5926 | build_pointer_type (TREE_TYPE (type)), |
5927 | rhs, origtype, errtype, | |
5928 | null_pointer_constant, fundecl, function, | |
5929 | parmnum); | |
cd192ccc MS |
5930 | if (rhs == error_mark_node) |
5931 | return error_mark_node; | |
3e4093b6 RS |
5932 | |
5933 | rhs = build1 (NOP_EXPR, type, rhs); | |
c2255bc4 | 5934 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5935 | return rhs; |
400fbf9f | 5936 | } |
3e4093b6 | 5937 | /* Some types can interconvert without explicit casts. */ |
3274deff | 5938 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE |
00c8e9f6 | 5939 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) |
3e4093b6 RS |
5940 | return convert (type, rhs); |
5941 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
5942 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
ab22c1fa | 5943 | || codel == FIXED_POINT_TYPE |
3e4093b6 RS |
5944 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE |
5945 | || codel == BOOLEAN_TYPE) | |
5946 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
ab22c1fa | 5947 | || coder == FIXED_POINT_TYPE |
3e4093b6 RS |
5948 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE |
5949 | || coder == BOOLEAN_TYPE)) | |
928c19bb JM |
5950 | { |
5951 | tree ret; | |
5952 | bool save = in_late_binary_op; | |
e5341100 JJ |
5953 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE |
5954 | || (coder == REAL_TYPE | |
5955 | && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE) | |
5956 | && (flag_sanitize & SANITIZE_FLOAT_CAST))) | |
928c19bb | 5957 | in_late_binary_op = true; |
81e5eca8 MP |
5958 | ret = convert_and_check (expr_loc != UNKNOWN_LOCATION |
5959 | ? expr_loc : location, type, orig_rhs); | |
e5341100 | 5960 | in_late_binary_op = save; |
928c19bb JM |
5961 | return ret; |
5962 | } | |
400fbf9f | 5963 | |
79077aea JJ |
5964 | /* Aggregates in different TUs might need conversion. */ |
5965 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
5966 | && codel == coder | |
5967 | && comptypes (type, rhstype)) | |
81e5eca8 MP |
5968 | return convert_and_check (expr_loc != UNKNOWN_LOCATION |
5969 | ? expr_loc : location, type, rhs); | |
79077aea | 5970 | |
ebf0bf7f | 5971 | /* Conversion to a transparent union or record from its member types. |
3e4093b6 | 5972 | This applies only to function arguments. */ |
ebf0bf7f JJ |
5973 | if (((codel == UNION_TYPE || codel == RECORD_TYPE) |
5974 | && TYPE_TRANSPARENT_AGGR (type)) | |
6b4ef5c1 | 5975 | && errtype == ic_argpass) |
400fbf9f | 5976 | { |
0257e383 | 5977 | tree memb, marginal_memb = NULL_TREE; |
3e4093b6 | 5978 | |
910ad8de | 5979 | for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) |
400fbf9f | 5980 | { |
0257e383 | 5981 | tree memb_type = TREE_TYPE (memb); |
400fbf9f | 5982 | |
3e4093b6 | 5983 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
132da1a5 | 5984 | TYPE_MAIN_VARIANT (rhstype))) |
3e4093b6 | 5985 | break; |
e58cd767 | 5986 | |
3e4093b6 RS |
5987 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
5988 | continue; | |
2f6e4e97 | 5989 | |
3e4093b6 RS |
5990 | if (coder == POINTER_TYPE) |
5991 | { | |
5992 | tree ttl = TREE_TYPE (memb_type); | |
5993 | tree ttr = TREE_TYPE (rhstype); | |
400fbf9f | 5994 | |
3e4093b6 RS |
5995 | /* Any non-function converts to a [const][volatile] void * |
5996 | and vice versa; otherwise, targets must be the same. | |
5997 | Meanwhile, the lhs target must have all the qualifiers of | |
5998 | the rhs. */ | |
267bac10 JM |
5999 | if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) |
6000 | || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) | |
744aa42f | 6001 | || comp_target_types (location, memb_type, rhstype)) |
3e4093b6 | 6002 | { |
267bac10 JM |
6003 | int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC; |
6004 | int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC; | |
3e4093b6 | 6005 | /* If this type won't generate any warnings, use it. */ |
267bac10 | 6006 | if (lquals == rquals |
3e4093b6 RS |
6007 | || ((TREE_CODE (ttr) == FUNCTION_TYPE |
6008 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
267bac10 JM |
6009 | ? ((lquals | rquals) == rquals) |
6010 | : ((lquals | rquals) == lquals))) | |
3e4093b6 | 6011 | break; |
400fbf9f | 6012 | |
3e4093b6 | 6013 | /* Keep looking for a better type, but remember this one. */ |
0257e383 RH |
6014 | if (!marginal_memb) |
6015 | marginal_memb = memb; | |
3e4093b6 RS |
6016 | } |
6017 | } | |
82bde854 | 6018 | |
3e4093b6 | 6019 | /* Can convert integer zero to any pointer type. */ |
928c19bb | 6020 | if (null_pointer_constant) |
3e4093b6 RS |
6021 | { |
6022 | rhs = null_pointer_node; | |
6023 | break; | |
6024 | } | |
6025 | } | |
400fbf9f | 6026 | |
0257e383 | 6027 | if (memb || marginal_memb) |
3e4093b6 | 6028 | { |
0257e383 | 6029 | if (!memb) |
3e4093b6 RS |
6030 | { |
6031 | /* We have only a marginally acceptable member type; | |
6032 | it needs a warning. */ | |
0257e383 | 6033 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); |
3e4093b6 | 6034 | tree ttr = TREE_TYPE (rhstype); |
714a0864 | 6035 | |
3e4093b6 RS |
6036 | /* Const and volatile mean something different for function |
6037 | types, so the usual warnings are not appropriate. */ | |
6038 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
6039 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
6040 | { | |
6041 | /* Because const and volatile on functions are | |
6042 | restrictions that say the function will not do | |
6043 | certain things, it is okay to use a const or volatile | |
6044 | function where an ordinary one is wanted, but not | |
6045 | vice-versa. */ | |
36c5e70a BE |
6046 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
6047 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
768952be MU |
6048 | PEDWARN_FOR_QUALIFIERS (location, expr_loc, |
6049 | OPT_Wdiscarded_qualifiers, | |
6050 | G_("passing argument %d of %qE " | |
6051 | "makes %q#v qualified function " | |
6052 | "pointer from unqualified"), | |
6053 | G_("assignment makes %q#v qualified " | |
6054 | "function pointer from " | |
6055 | "unqualified"), | |
6056 | G_("initialization makes %q#v qualified " | |
6057 | "function pointer from " | |
6058 | "unqualified"), | |
6059 | G_("return makes %q#v qualified function " | |
6060 | "pointer from unqualified"), | |
6061 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 | 6062 | } |
36c5e70a BE |
6063 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
6064 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
768952be MU |
6065 | PEDWARN_FOR_QUALIFIERS (location, expr_loc, |
6066 | OPT_Wdiscarded_qualifiers, | |
6067 | G_("passing argument %d of %qE discards " | |
6068 | "%qv qualifier from pointer target type"), | |
6069 | G_("assignment discards %qv qualifier " | |
6070 | "from pointer target type"), | |
6071 | G_("initialization discards %qv qualifier " | |
6072 | "from pointer target type"), | |
6073 | G_("return discards %qv qualifier from " | |
6074 | "pointer target type"), | |
6075 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
0257e383 RH |
6076 | |
6077 | memb = marginal_memb; | |
3e4093b6 | 6078 | } |
400fbf9f | 6079 | |
fcf73884 | 6080 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) |
c1771a20 | 6081 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 6082 | "ISO C prohibits argument conversion to union type"); |
0e7c47fa | 6083 | |
db3927fb | 6084 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); |
0257e383 | 6085 | return build_constructor_single (type, memb, rhs); |
3e4093b6 | 6086 | } |
0e7c47fa RK |
6087 | } |
6088 | ||
3e4093b6 RS |
6089 | /* Conversions among pointers */ |
6090 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
6091 | && (coder == codel)) | |
400fbf9f | 6092 | { |
3e4093b6 RS |
6093 | tree ttl = TREE_TYPE (type); |
6094 | tree ttr = TREE_TYPE (rhstype); | |
46df2823 JM |
6095 | tree mvl = ttl; |
6096 | tree mvr = ttr; | |
3e4093b6 | 6097 | bool is_opaque_pointer; |
264fa2db | 6098 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
36c5e70a BE |
6099 | addr_space_t asl; |
6100 | addr_space_t asr; | |
400fbf9f | 6101 | |
46df2823 | 6102 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
267bac10 JM |
6103 | mvl = (TYPE_ATOMIC (mvl) |
6104 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), | |
6105 | TYPE_QUAL_ATOMIC) | |
6106 | : TYPE_MAIN_VARIANT (mvl)); | |
46df2823 | 6107 | if (TREE_CODE (mvr) != ARRAY_TYPE) |
267bac10 JM |
6108 | mvr = (TYPE_ATOMIC (mvr) |
6109 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), | |
6110 | TYPE_QUAL_ATOMIC) | |
6111 | : TYPE_MAIN_VARIANT (mvr)); | |
3e4093b6 | 6112 | /* Opaque pointers are treated like void pointers. */ |
f83c7f63 | 6113 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); |
c22cacf3 | 6114 | |
478a1c5b ILT |
6115 | /* The Plan 9 compiler permits a pointer to a struct to be |
6116 | automatically converted into a pointer to an anonymous field | |
6117 | within the struct. */ | |
6118 | if (flag_plan9_extensions | |
6119 | && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE) | |
6120 | && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE) | |
6121 | && mvl != mvr) | |
6122 | { | |
6123 | tree new_rhs = convert_to_anonymous_field (location, type, rhs); | |
6124 | if (new_rhs != NULL_TREE) | |
6125 | { | |
6126 | rhs = new_rhs; | |
6127 | rhstype = TREE_TYPE (rhs); | |
6128 | coder = TREE_CODE (rhstype); | |
6129 | ttr = TREE_TYPE (rhstype); | |
6130 | mvr = TYPE_MAIN_VARIANT (ttr); | |
6131 | } | |
6132 | } | |
6133 | ||
b7e20b53 | 6134 | /* C++ does not allow the implicit conversion void* -> T*. However, |
c22cacf3 MS |
6135 | for the purpose of reducing the number of false positives, we |
6136 | tolerate the special case of | |
b7e20b53 | 6137 | |
c22cacf3 | 6138 | int *p = NULL; |
b7e20b53 | 6139 | |
c22cacf3 | 6140 | where NULL is typically defined in C to be '(void *) 0'. */ |
c6bdf92e | 6141 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) |
8ffcdea8 MP |
6142 | warning_at (errtype == ic_argpass ? expr_loc : location, |
6143 | OPT_Wc___compat, | |
6144 | "request for implicit conversion " | |
c2255bc4 | 6145 | "from %qT to %qT not permitted in C++", rhstype, type); |
400fbf9f | 6146 | |
36c5e70a BE |
6147 | /* See if the pointers point to incompatible address spaces. */ |
6148 | asl = TYPE_ADDR_SPACE (ttl); | |
6149 | asr = TYPE_ADDR_SPACE (ttr); | |
6150 | if (!null_pointer_constant_p (rhs) | |
6151 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
6152 | { | |
6153 | switch (errtype) | |
6154 | { | |
6155 | case ic_argpass: | |
8ffcdea8 | 6156 | error_at (expr_loc, "passing argument %d of %qE from pointer to " |
36c5e70a BE |
6157 | "non-enclosed address space", parmnum, rname); |
6158 | break; | |
6159 | case ic_assign: | |
6160 | error_at (location, "assignment from pointer to " | |
6161 | "non-enclosed address space"); | |
6162 | break; | |
6163 | case ic_init: | |
6164 | error_at (location, "initialization from pointer to " | |
6165 | "non-enclosed address space"); | |
6166 | break; | |
6167 | case ic_return: | |
6168 | error_at (location, "return from pointer to " | |
6169 | "non-enclosed address space"); | |
6170 | break; | |
6171 | default: | |
6172 | gcc_unreachable (); | |
6173 | } | |
6174 | return error_mark_node; | |
6175 | } | |
6176 | ||
7876a414 KG |
6177 | /* Check if the right-hand side has a format attribute but the |
6178 | left-hand side doesn't. */ | |
90137d8f | 6179 | if (warn_suggest_attribute_format |
104f8784 | 6180 | && check_missing_format_attribute (type, rhstype)) |
c22cacf3 | 6181 | { |
104f8784 KG |
6182 | switch (errtype) |
6183 | { | |
6184 | case ic_argpass: | |
8ffcdea8 | 6185 | warning_at (expr_loc, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6186 | "argument %d of %qE might be " |
6187 | "a candidate for a format attribute", | |
6188 | parmnum, rname); | |
104f8784 KG |
6189 | break; |
6190 | case ic_assign: | |
90137d8f | 6191 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6192 | "assignment left-hand side might be " |
6193 | "a candidate for a format attribute"); | |
104f8784 KG |
6194 | break; |
6195 | case ic_init: | |
90137d8f | 6196 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6197 | "initialization left-hand side might be " |
6198 | "a candidate for a format attribute"); | |
104f8784 KG |
6199 | break; |
6200 | case ic_return: | |
90137d8f | 6201 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6202 | "return type might be " |
6203 | "a candidate for a format attribute"); | |
104f8784 KG |
6204 | break; |
6205 | default: | |
6206 | gcc_unreachable (); | |
6207 | } | |
7876a414 | 6208 | } |
c22cacf3 | 6209 | |
3e4093b6 RS |
6210 | /* Any non-function converts to a [const][volatile] void * |
6211 | and vice versa; otherwise, targets must be the same. | |
6212 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
267bac10 JM |
6213 | if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) |
6214 | || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) | |
744aa42f | 6215 | || (target_cmp = comp_target_types (location, type, rhstype)) |
3e4093b6 | 6216 | || is_opaque_pointer |
f8a93a2e JJ |
6217 | || ((c_common_unsigned_type (mvl) |
6218 | == c_common_unsigned_type (mvr)) | |
267bac10 JM |
6219 | && (c_common_signed_type (mvl) |
6220 | == c_common_signed_type (mvr)) | |
6221 | && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr))) | |
3e4093b6 | 6222 | { |
768952be MU |
6223 | /* Warn about loss of qualifers from pointers to arrays with |
6224 | qualifiers on the element type. */ | |
6225 | if (TREE_CODE (ttr) == ARRAY_TYPE) | |
6226 | { | |
6227 | ttr = strip_array_types (ttr); | |
6228 | ttl = strip_array_types (ttl); | |
6229 | ||
6230 | if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) | |
6231 | & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) | |
6232 | WARNING_FOR_QUALIFIERS (location, expr_loc, | |
6233 | OPT_Wdiscarded_array_qualifiers, | |
6234 | G_("passing argument %d of %qE discards " | |
6235 | "%qv qualifier from pointer target type"), | |
6236 | G_("assignment discards %qv qualifier " | |
6237 | "from pointer target type"), | |
6238 | G_("initialization discards %qv qualifier " | |
6239 | "from pointer target type"), | |
6240 | G_("return discards %qv qualifier from " | |
6241 | "pointer target type"), | |
6242 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
6243 | } | |
6244 | else if (pedantic | |
3e4093b6 RS |
6245 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) |
6246 | || | |
6247 | (VOID_TYPE_P (ttr) | |
928c19bb | 6248 | && !null_pointer_constant |
3e4093b6 | 6249 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
768952be MU |
6250 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic, |
6251 | G_("ISO C forbids passing argument %d of " | |
6252 | "%qE between function pointer " | |
6253 | "and %<void *%>"), | |
6254 | G_("ISO C forbids assignment between " | |
6255 | "function pointer and %<void *%>"), | |
6256 | G_("ISO C forbids initialization between " | |
6257 | "function pointer and %<void *%>"), | |
6258 | G_("ISO C forbids return between function " | |
6259 | "pointer and %<void *%>")); | |
3e4093b6 RS |
6260 | /* Const and volatile mean something different for function types, |
6261 | so the usual warnings are not appropriate. */ | |
6262 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
6263 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
6264 | { | |
768952be MU |
6265 | /* Don't warn about loss of qualifier for conversions from |
6266 | qualified void* to pointers to arrays with corresponding | |
6267 | qualifier on the element type. */ | |
6268 | if (!pedantic) | |
6269 | ttl = strip_array_types (ttl); | |
6270 | ||
267bac10 JM |
6271 | /* Assignments between atomic and non-atomic objects are OK. */ |
6272 | if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) | |
6273 | & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) | |
58393038 | 6274 | { |
768952be MU |
6275 | PEDWARN_FOR_QUALIFIERS (location, expr_loc, |
6276 | OPT_Wdiscarded_qualifiers, | |
6277 | G_("passing argument %d of %qE discards " | |
6278 | "%qv qualifier from pointer target type"), | |
6279 | G_("assignment discards %qv qualifier " | |
6280 | "from pointer target type"), | |
6281 | G_("initialization discards %qv qualifier " | |
6282 | "from pointer target type"), | |
6283 | G_("return discards %qv qualifier from " | |
6284 | "pointer target type"), | |
6285 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
58393038 | 6286 | } |
3e4093b6 RS |
6287 | /* If this is not a case of ignoring a mismatch in signedness, |
6288 | no warning. */ | |
6289 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 6290 | || target_cmp) |
3e4093b6 RS |
6291 | ; |
6292 | /* If there is a mismatch, do warn. */ | |
f2fd3821 | 6293 | else if (warn_pointer_sign) |
768952be MU |
6294 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign, |
6295 | G_("pointer targets in passing argument " | |
6296 | "%d of %qE differ in signedness"), | |
6297 | G_("pointer targets in assignment " | |
6298 | "differ in signedness"), | |
6299 | G_("pointer targets in initialization " | |
6300 | "differ in signedness"), | |
6301 | G_("pointer targets in return differ " | |
6302 | "in signedness")); | |
3e4093b6 RS |
6303 | } |
6304 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
6305 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
6306 | { | |
6307 | /* Because const and volatile on functions are restrictions | |
6308 | that say the function will not do certain things, | |
6309 | it is okay to use a const or volatile function | |
6310 | where an ordinary one is wanted, but not vice-versa. */ | |
36c5e70a BE |
6311 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
6312 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
768952be MU |
6313 | PEDWARN_FOR_QUALIFIERS (location, expr_loc, |
6314 | OPT_Wdiscarded_qualifiers, | |
6315 | G_("passing argument %d of %qE makes " | |
6316 | "%q#v qualified function pointer " | |
6317 | "from unqualified"), | |
6318 | G_("assignment makes %q#v qualified function " | |
6319 | "pointer from unqualified"), | |
6320 | G_("initialization makes %q#v qualified " | |
6321 | "function pointer from unqualified"), | |
6322 | G_("return makes %q#v qualified function " | |
6323 | "pointer from unqualified"), | |
6324 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 RS |
6325 | } |
6326 | } | |
6327 | else | |
58393038 ZL |
6328 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
6329 | if (!objc_ok) | |
768952be MU |
6330 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, |
6331 | OPT_Wincompatible_pointer_types, | |
6332 | G_("passing argument %d of %qE from " | |
6333 | "incompatible pointer type"), | |
6334 | G_("assignment from incompatible pointer type"), | |
6335 | G_("initialization from incompatible " | |
6336 | "pointer type"), | |
6337 | G_("return from incompatible pointer type")); | |
58393038 | 6338 | |
3e4093b6 RS |
6339 | return convert (type, rhs); |
6340 | } | |
b494fd98 EB |
6341 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
6342 | { | |
6dcc04b0 JM |
6343 | /* ??? This should not be an error when inlining calls to |
6344 | unprototyped functions. */ | |
c2255bc4 | 6345 | error_at (location, "invalid use of non-lvalue array"); |
b494fd98 EB |
6346 | return error_mark_node; |
6347 | } | |
3e4093b6 | 6348 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
400fbf9f | 6349 | { |
3e4093b6 RS |
6350 | /* An explicit constant 0 can convert to a pointer, |
6351 | or one that results from arithmetic, even including | |
6352 | a cast to integer type. */ | |
928c19bb | 6353 | if (!null_pointer_constant) |
768952be MU |
6354 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, |
6355 | OPT_Wint_conversion, | |
6356 | G_("passing argument %d of %qE makes " | |
6357 | "pointer from integer without a cast"), | |
6358 | G_("assignment makes pointer from integer " | |
6359 | "without a cast"), | |
6360 | G_("initialization makes pointer from " | |
6361 | "integer without a cast"), | |
6362 | G_("return makes pointer from integer " | |
6363 | "without a cast")); | |
b3006337 EB |
6364 | |
6365 | return convert (type, rhs); | |
400fbf9f | 6366 | } |
3e4093b6 | 6367 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
400fbf9f | 6368 | { |
768952be MU |
6369 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, |
6370 | OPT_Wint_conversion, | |
6371 | G_("passing argument %d of %qE makes integer " | |
6372 | "from pointer without a cast"), | |
6373 | G_("assignment makes integer from pointer " | |
6374 | "without a cast"), | |
6375 | G_("initialization makes integer from pointer " | |
6376 | "without a cast"), | |
6377 | G_("return makes integer from pointer " | |
6378 | "without a cast")); | |
3e4093b6 RS |
6379 | return convert (type, rhs); |
6380 | } | |
6381 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
928c19bb JM |
6382 | { |
6383 | tree ret; | |
6384 | bool save = in_late_binary_op; | |
6385 | in_late_binary_op = true; | |
6386 | ret = convert (type, rhs); | |
6387 | in_late_binary_op = save; | |
6388 | return ret; | |
6389 | } | |
400fbf9f | 6390 | |
2ac2f164 | 6391 | switch (errtype) |
3e4093b6 | 6392 | { |
2ac2f164 | 6393 | case ic_argpass: |
8ffcdea8 MP |
6394 | error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum, |
6395 | rname); | |
a7da8b42 | 6396 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) |
8ffcdea8 | 6397 | ? DECL_SOURCE_LOCATION (fundecl) : expr_loc, |
a7da8b42 | 6398 | "expected %qT but argument is of type %qT", type, rhstype); |
2ac2f164 JM |
6399 | break; |
6400 | case ic_assign: | |
c2255bc4 AH |
6401 | error_at (location, "incompatible types when assigning to type %qT from " |
6402 | "type %qT", type, rhstype); | |
2ac2f164 JM |
6403 | break; |
6404 | case ic_init: | |
c2255bc4 | 6405 | error_at (location, |
8ffcdea8 | 6406 | "incompatible types when initializing type %qT using type %qT", |
c2255bc4 | 6407 | type, rhstype); |
2ac2f164 JM |
6408 | break; |
6409 | case ic_return: | |
c2255bc4 | 6410 | error_at (location, |
8ffcdea8 | 6411 | "incompatible types when returning type %qT but %qT was " |
c2255bc4 | 6412 | "expected", rhstype, type); |
2ac2f164 JM |
6413 | break; |
6414 | default: | |
6415 | gcc_unreachable (); | |
400fbf9f | 6416 | } |
53b01f59 | 6417 | |
3e4093b6 RS |
6418 | return error_mark_node; |
6419 | } | |
3e4093b6 RS |
6420 | \f |
6421 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
6422 | return its value. Otherwise, return error_mark_node. | |
15b732b2 | 6423 | |
3e4093b6 RS |
6424 | This is for handling COMPOUND_EXPRs as initializer elements |
6425 | which is allowed with a warning when -pedantic is specified. */ | |
15b732b2 | 6426 | |
3e4093b6 RS |
6427 | static tree |
6428 | valid_compound_expr_initializer (tree value, tree endtype) | |
6429 | { | |
6430 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
6431 | { | |
6432 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
6433 | == error_mark_node) | |
6434 | return error_mark_node; | |
6435 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
6436 | endtype); | |
6437 | } | |
116df786 | 6438 | else if (!initializer_constant_valid_p (value, endtype)) |
3e4093b6 RS |
6439 | return error_mark_node; |
6440 | else | |
6441 | return value; | |
15b732b2 | 6442 | } |
400fbf9f | 6443 | \f |
3e4093b6 RS |
6444 | /* Perform appropriate conversions on the initial value of a variable, |
6445 | store it in the declaration DECL, | |
6446 | and print any error messages that are appropriate. | |
bbbbb16a | 6447 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
c2255bc4 AH |
6448 | If the init is invalid, store an ERROR_MARK. |
6449 | ||
6450 | INIT_LOC is the location of the initial value. */ | |
400fbf9f | 6451 | |
3e4093b6 | 6452 | void |
c2255bc4 | 6453 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) |
400fbf9f | 6454 | { |
3e4093b6 | 6455 | tree value, type; |
928c19bb | 6456 | bool npc = false; |
400fbf9f | 6457 | |
3e4093b6 | 6458 | /* If variable's type was invalidly declared, just ignore it. */ |
400fbf9f | 6459 | |
3e4093b6 RS |
6460 | type = TREE_TYPE (decl); |
6461 | if (TREE_CODE (type) == ERROR_MARK) | |
6462 | return; | |
400fbf9f | 6463 | |
3e4093b6 | 6464 | /* Digest the specified initializer into an expression. */ |
400fbf9f | 6465 | |
928c19bb JM |
6466 | if (init) |
6467 | npc = null_pointer_constant_p (init); | |
c2255bc4 AH |
6468 | value = digest_init (init_loc, type, init, origtype, npc, |
6469 | true, TREE_STATIC (decl)); | |
400fbf9f | 6470 | |
3e4093b6 | 6471 | /* Store the expression if valid; else report error. */ |
400fbf9f | 6472 | |
8400e75e | 6473 | if (!in_system_header_at (input_location) |
3f75a254 | 6474 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) |
3176a0c2 DD |
6475 | warning (OPT_Wtraditional, "traditional C rejects automatic " |
6476 | "aggregate initialization"); | |
2f6e4e97 | 6477 | |
dea63e49 JJ |
6478 | if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL) |
6479 | DECL_INITIAL (decl) = value; | |
400fbf9f | 6480 | |
3e4093b6 RS |
6481 | /* ANSI wants warnings about out-of-range constant initializers. */ |
6482 | STRIP_TYPE_NOPS (value); | |
b8698a0f | 6483 | if (TREE_STATIC (decl)) |
c2658540 | 6484 | constant_expression_warning (value); |
400fbf9f | 6485 | |
3e4093b6 RS |
6486 | /* Check if we need to set array size from compound literal size. */ |
6487 | if (TREE_CODE (type) == ARRAY_TYPE | |
6488 | && TYPE_DOMAIN (type) == 0 | |
6489 | && value != error_mark_node) | |
400fbf9f | 6490 | { |
3e4093b6 RS |
6491 | tree inside_init = init; |
6492 | ||
ed248cf7 | 6493 | STRIP_TYPE_NOPS (inside_init); |
3e4093b6 RS |
6494 | inside_init = fold (inside_init); |
6495 | ||
6496 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
6497 | { | |
8d9f82d5 | 6498 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
3e4093b6 | 6499 | |
8d9f82d5 | 6500 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) |
3e4093b6 RS |
6501 | { |
6502 | /* For int foo[] = (int [3]){1}; we need to set array size | |
6503 | now since later on array initializer will be just the | |
6504 | brace enclosed list of the compound literal. */ | |
e30ecc5d | 6505 | tree etype = strip_array_types (TREE_TYPE (decl)); |
8d9f82d5 | 6506 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); |
8d9f82d5 | 6507 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); |
3e4093b6 | 6508 | layout_type (type); |
8d9f82d5 | 6509 | layout_decl (cldecl, 0); |
e30ecc5d JJ |
6510 | TREE_TYPE (decl) |
6511 | = c_build_qualified_type (type, TYPE_QUALS (etype)); | |
3e4093b6 RS |
6512 | } |
6513 | } | |
400fbf9f | 6514 | } |
3e4093b6 RS |
6515 | } |
6516 | \f | |
6517 | /* Methods for storing and printing names for error messages. */ | |
400fbf9f | 6518 | |
3e4093b6 RS |
6519 | /* Implement a spelling stack that allows components of a name to be pushed |
6520 | and popped. Each element on the stack is this structure. */ | |
400fbf9f | 6521 | |
3e4093b6 RS |
6522 | struct spelling |
6523 | { | |
6524 | int kind; | |
6525 | union | |
400fbf9f | 6526 | { |
a0f0ab9f | 6527 | unsigned HOST_WIDE_INT i; |
3e4093b6 RS |
6528 | const char *s; |
6529 | } u; | |
6530 | }; | |
2f6e4e97 | 6531 | |
3e4093b6 RS |
6532 | #define SPELLING_STRING 1 |
6533 | #define SPELLING_MEMBER 2 | |
6534 | #define SPELLING_BOUNDS 3 | |
400fbf9f | 6535 | |
3e4093b6 RS |
6536 | static struct spelling *spelling; /* Next stack element (unused). */ |
6537 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
6538 | static int spelling_size; /* Size of the spelling stack. */ | |
400fbf9f | 6539 | |
3e4093b6 RS |
6540 | /* Macros to save and restore the spelling stack around push_... functions. |
6541 | Alternative to SAVE_SPELLING_STACK. */ | |
400fbf9f | 6542 | |
3e4093b6 RS |
6543 | #define SPELLING_DEPTH() (spelling - spelling_base) |
6544 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
400fbf9f | 6545 | |
3e4093b6 RS |
6546 | /* Push an element on the spelling stack with type KIND and assign VALUE |
6547 | to MEMBER. */ | |
400fbf9f | 6548 | |
3e4093b6 RS |
6549 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
6550 | { \ | |
6551 | int depth = SPELLING_DEPTH (); \ | |
6552 | \ | |
6553 | if (depth >= spelling_size) \ | |
6554 | { \ | |
6555 | spelling_size += 10; \ | |
cca8ead2 BI |
6556 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
6557 | spelling_size); \ | |
3e4093b6 RS |
6558 | RESTORE_SPELLING_DEPTH (depth); \ |
6559 | } \ | |
6560 | \ | |
6561 | spelling->kind = (KIND); \ | |
6562 | spelling->MEMBER = (VALUE); \ | |
6563 | spelling++; \ | |
6564 | } | |
400fbf9f | 6565 | |
3e4093b6 | 6566 | /* Push STRING on the stack. Printed literally. */ |
400fbf9f | 6567 | |
3e4093b6 RS |
6568 | static void |
6569 | push_string (const char *string) | |
6570 | { | |
6571 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
6572 | } | |
400fbf9f | 6573 | |
3e4093b6 | 6574 | /* Push a member name on the stack. Printed as '.' STRING. */ |
400fbf9f | 6575 | |
3e4093b6 RS |
6576 | static void |
6577 | push_member_name (tree decl) | |
6578 | { | |
6579 | const char *const string | |
88388a52 JM |
6580 | = (DECL_NAME (decl) |
6581 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
6582 | : _("<anonymous>")); | |
3e4093b6 RS |
6583 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
6584 | } | |
400fbf9f | 6585 | |
3e4093b6 | 6586 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
400fbf9f | 6587 | |
3e4093b6 | 6588 | static void |
a0f0ab9f | 6589 | push_array_bounds (unsigned HOST_WIDE_INT bounds) |
3e4093b6 RS |
6590 | { |
6591 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
6592 | } | |
bb58bec5 | 6593 | |
3e4093b6 | 6594 | /* Compute the maximum size in bytes of the printed spelling. */ |
400fbf9f | 6595 | |
3e4093b6 RS |
6596 | static int |
6597 | spelling_length (void) | |
6598 | { | |
6599 | int size = 0; | |
6600 | struct spelling *p; | |
400fbf9f | 6601 | |
3e4093b6 RS |
6602 | for (p = spelling_base; p < spelling; p++) |
6603 | { | |
6604 | if (p->kind == SPELLING_BOUNDS) | |
6605 | size += 25; | |
6606 | else | |
6607 | size += strlen (p->u.s) + 1; | |
6608 | } | |
6609 | ||
6610 | return size; | |
400fbf9f | 6611 | } |
400fbf9f | 6612 | |
3e4093b6 | 6613 | /* Print the spelling to BUFFER and return it. */ |
400fbf9f | 6614 | |
3e4093b6 RS |
6615 | static char * |
6616 | print_spelling (char *buffer) | |
400fbf9f | 6617 | { |
3e4093b6 RS |
6618 | char *d = buffer; |
6619 | struct spelling *p; | |
400fbf9f | 6620 | |
3e4093b6 RS |
6621 | for (p = spelling_base; p < spelling; p++) |
6622 | if (p->kind == SPELLING_BOUNDS) | |
6623 | { | |
a0f0ab9f | 6624 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); |
3e4093b6 RS |
6625 | d += strlen (d); |
6626 | } | |
6627 | else | |
6628 | { | |
6629 | const char *s; | |
6630 | if (p->kind == SPELLING_MEMBER) | |
6631 | *d++ = '.'; | |
6632 | for (s = p->u.s; (*d = *s++); d++) | |
6633 | ; | |
6634 | } | |
6635 | *d++ = '\0'; | |
6636 | return buffer; | |
6637 | } | |
400fbf9f | 6638 | |
3e4093b6 RS |
6639 | /* Digest the parser output INIT as an initializer for type TYPE. |
6640 | Return a C expression of type TYPE to represent the initial value. | |
7e842ef8 | 6641 | |
bbbbb16a ILT |
6642 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
6643 | ||
928c19bb JM |
6644 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. |
6645 | ||
916c5919 JM |
6646 | If INIT is a string constant, STRICT_STRING is true if it is |
6647 | unparenthesized or we should not warn here for it being parenthesized. | |
6648 | For other types of INIT, STRICT_STRING is not used. | |
6649 | ||
c2255bc4 AH |
6650 | INIT_LOC is the location of the INIT. |
6651 | ||
3e4093b6 RS |
6652 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
6653 | elements are seen. */ | |
7e842ef8 | 6654 | |
3e4093b6 | 6655 | static tree |
c2255bc4 AH |
6656 | digest_init (location_t init_loc, tree type, tree init, tree origtype, |
6657 | bool null_pointer_constant, bool strict_string, | |
6658 | int require_constant) | |
3e4093b6 RS |
6659 | { |
6660 | enum tree_code code = TREE_CODE (type); | |
6661 | tree inside_init = init; | |
8ce94e44 | 6662 | tree semantic_type = NULL_TREE; |
928c19bb | 6663 | bool maybe_const = true; |
7e842ef8 | 6664 | |
3e4093b6 | 6665 | if (type == error_mark_node |
f01da1a5 | 6666 | || !init |
7a0ca710 | 6667 | || error_operand_p (init)) |
3e4093b6 | 6668 | return error_mark_node; |
7e842ef8 | 6669 | |
ed248cf7 | 6670 | STRIP_TYPE_NOPS (inside_init); |
7e842ef8 | 6671 | |
8ce94e44 JM |
6672 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) |
6673 | { | |
6674 | semantic_type = TREE_TYPE (inside_init); | |
6675 | inside_init = TREE_OPERAND (inside_init, 0); | |
6676 | } | |
928c19bb JM |
6677 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); |
6678 | inside_init = decl_constant_value_for_optimization (inside_init); | |
7e842ef8 | 6679 | |
3e4093b6 RS |
6680 | /* Initialization of an array of chars from a string constant |
6681 | optionally enclosed in braces. */ | |
7e842ef8 | 6682 | |
197463ae JM |
6683 | if (code == ARRAY_TYPE && inside_init |
6684 | && TREE_CODE (inside_init) == STRING_CST) | |
3e4093b6 | 6685 | { |
267bac10 JM |
6686 | tree typ1 |
6687 | = (TYPE_ATOMIC (TREE_TYPE (type)) | |
6688 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)), | |
6689 | TYPE_QUAL_ATOMIC) | |
6690 | : TYPE_MAIN_VARIANT (TREE_TYPE (type))); | |
197463ae JM |
6691 | /* Note that an array could be both an array of character type |
6692 | and an array of wchar_t if wchar_t is signed char or unsigned | |
6693 | char. */ | |
6694 | bool char_array = (typ1 == char_type_node | |
6695 | || typ1 == signed_char_type_node | |
6696 | || typ1 == unsigned_char_type_node); | |
6697 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
c466b2cd KVH |
6698 | bool char16_array = !!comptypes (typ1, char16_type_node); |
6699 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
6700 | ||
6701 | if (char_array || wchar_array || char16_array || char32_array) | |
7e842ef8 | 6702 | { |
916c5919 | 6703 | struct c_expr expr; |
c466b2cd | 6704 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); |
916c5919 JM |
6705 | expr.value = inside_init; |
6706 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
6866c6e8 | 6707 | expr.original_type = NULL; |
d033409e | 6708 | maybe_warn_string_init (init_loc, type, expr); |
916c5919 | 6709 | |
a45e580b | 6710 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
c1771a20 | 6711 | pedwarn_init (init_loc, OPT_Wpedantic, |
a45e580b JM |
6712 | "initialization of a flexible array member"); |
6713 | ||
3e4093b6 | 6714 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 6715 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 | 6716 | return inside_init; |
7e842ef8 | 6717 | |
c466b2cd | 6718 | if (char_array) |
3e4093b6 | 6719 | { |
c466b2cd KVH |
6720 | if (typ2 != char_type_node) |
6721 | { | |
ea58ef42 MP |
6722 | error_init (init_loc, "char-array initialized from wide " |
6723 | "string"); | |
c466b2cd KVH |
6724 | return error_mark_node; |
6725 | } | |
3e4093b6 | 6726 | } |
c466b2cd | 6727 | else |
3e4093b6 | 6728 | { |
c466b2cd KVH |
6729 | if (typ2 == char_type_node) |
6730 | { | |
ea58ef42 MP |
6731 | error_init (init_loc, "wide character array initialized " |
6732 | "from non-wide string"); | |
c466b2cd KVH |
6733 | return error_mark_node; |
6734 | } | |
6735 | else if (!comptypes(typ1, typ2)) | |
6736 | { | |
ea58ef42 MP |
6737 | error_init (init_loc, "wide character array initialized " |
6738 | "from incompatible wide string"); | |
c466b2cd KVH |
6739 | return error_mark_node; |
6740 | } | |
7e842ef8 | 6741 | } |
2f6e4e97 | 6742 | |
3e4093b6 RS |
6743 | TREE_TYPE (inside_init) = type; |
6744 | if (TYPE_DOMAIN (type) != 0 | |
6745 | && TYPE_SIZE (type) != 0 | |
5eb4df45 ILT |
6746 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
6747 | { | |
6748 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
6749 | ||
c466b2cd | 6750 | /* Subtract the size of a single (possibly wide) character |
3e4093b6 RS |
6751 | because it's ok to ignore the terminating null char |
6752 | that is counted in the length of the constant. */ | |
5eb4df45 ILT |
6753 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), |
6754 | (len | |
6755 | - (TYPE_PRECISION (typ1) | |
6756 | / BITS_PER_UNIT)))) | |
6757 | pedwarn_init (init_loc, 0, | |
6758 | ("initializer-string for array of chars " | |
6759 | "is too long")); | |
6760 | else if (warn_cxx_compat | |
6761 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
6762 | warning_at (init_loc, OPT_Wc___compat, | |
6763 | ("initializer-string for array chars " | |
6764 | "is too long for C++")); | |
6765 | } | |
7e842ef8 | 6766 | |
3e4093b6 | 6767 | return inside_init; |
7e842ef8 | 6768 | } |
197463ae JM |
6769 | else if (INTEGRAL_TYPE_P (typ1)) |
6770 | { | |
ea58ef42 | 6771 | error_init (init_loc, "array of inappropriate type initialized " |
197463ae JM |
6772 | "from string constant"); |
6773 | return error_mark_node; | |
6774 | } | |
7e842ef8 PE |
6775 | } |
6776 | ||
3e4093b6 RS |
6777 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
6778 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
6779 | below and handle as a constructor. */ | |
e89be13b | 6780 | if (code == VECTOR_TYPE |
31521951 | 6781 | && VECTOR_TYPE_P (TREE_TYPE (inside_init)) |
00c8e9f6 | 6782 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) |
e89be13b JJ |
6783 | && TREE_CONSTANT (inside_init)) |
6784 | { | |
6785 | if (TREE_CODE (inside_init) == VECTOR_CST | |
6786 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6787 | TYPE_MAIN_VARIANT (type))) | |
6788 | return inside_init; | |
6789 | ||
6790 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
6791 | { | |
4038c495 GB |
6792 | unsigned HOST_WIDE_INT ix; |
6793 | tree value; | |
6794 | bool constant_p = true; | |
e89be13b JJ |
6795 | |
6796 | /* Iterate through elements and check if all constructor | |
6797 | elements are *_CSTs. */ | |
4038c495 GB |
6798 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) |
6799 | if (!CONSTANT_CLASS_P (value)) | |
6800 | { | |
6801 | constant_p = false; | |
6802 | break; | |
6803 | } | |
e89be13b | 6804 | |
4038c495 GB |
6805 | if (constant_p) |
6806 | return build_vector_from_ctor (type, | |
6807 | CONSTRUCTOR_ELTS (inside_init)); | |
e89be13b JJ |
6808 | } |
6809 | } | |
6035d635 | 6810 | |
ca085fd7 MLI |
6811 | if (warn_sequence_point) |
6812 | verify_sequence_points (inside_init); | |
6813 | ||
3e4093b6 RS |
6814 | /* Any type can be initialized |
6815 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 6816 | |
3e4093b6 RS |
6817 | if (inside_init && TREE_TYPE (inside_init) != 0 |
6818 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
132da1a5 | 6819 | TYPE_MAIN_VARIANT (type)) |
3e4093b6 | 6820 | || (code == ARRAY_TYPE |
132da1a5 | 6821 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6822 | || (code == VECTOR_TYPE |
132da1a5 | 6823 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6824 | || (code == POINTER_TYPE |
3897f229 | 6825 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
3e4093b6 | 6826 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
132da1a5 | 6827 | TREE_TYPE (type))))) |
3e4093b6 RS |
6828 | { |
6829 | if (code == POINTER_TYPE) | |
b494fd98 | 6830 | { |
b494fd98 EB |
6831 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) |
6832 | { | |
f2a71bbc JM |
6833 | if (TREE_CODE (inside_init) == STRING_CST |
6834 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
c2255bc4 AH |
6835 | inside_init = array_to_pointer_conversion |
6836 | (init_loc, inside_init); | |
f2a71bbc JM |
6837 | else |
6838 | { | |
ea58ef42 | 6839 | error_init (init_loc, "invalid use of non-lvalue array"); |
f2a71bbc JM |
6840 | return error_mark_node; |
6841 | } | |
b494fd98 | 6842 | } |
f2a71bbc | 6843 | } |
b494fd98 | 6844 | |
bae39a73 NS |
6845 | if (code == VECTOR_TYPE) |
6846 | /* Although the types are compatible, we may require a | |
6847 | conversion. */ | |
6848 | inside_init = convert (type, inside_init); | |
3e4093b6 | 6849 | |
ca58211b | 6850 | if (require_constant |
3e4093b6 | 6851 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) |
400fbf9f | 6852 | { |
3e4093b6 RS |
6853 | /* As an extension, allow initializing objects with static storage |
6854 | duration with compound literals (which are then treated just as | |
ca58211b PB |
6855 | the brace enclosed list they contain). Also allow this for |
6856 | vectors, as we can only assign them with compound literals. */ | |
7278465e MP |
6857 | if (flag_isoc99 && code != VECTOR_TYPE) |
6858 | pedwarn_init (init_loc, OPT_Wpedantic, "initializer element " | |
6859 | "is not constant"); | |
3e4093b6 RS |
6860 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
6861 | inside_init = DECL_INITIAL (decl); | |
400fbf9f | 6862 | } |
3e4093b6 RS |
6863 | |
6864 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
6865 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 6866 | { |
ea58ef42 MP |
6867 | error_init (init_loc, "array initialized from non-constant array " |
6868 | "expression"); | |
3e4093b6 | 6869 | return error_mark_node; |
400fbf9f | 6870 | } |
400fbf9f | 6871 | |
c1771a20 | 6872 | /* Compound expressions can only occur here if -Wpedantic or |
3e4093b6 RS |
6873 | -pedantic-errors is specified. In the later case, we always want |
6874 | an error. In the former case, we simply want a warning. */ | |
6875 | if (require_constant && pedantic | |
6876 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
6877 | { | |
6878 | inside_init | |
6879 | = valid_compound_expr_initializer (inside_init, | |
6880 | TREE_TYPE (inside_init)); | |
6881 | if (inside_init == error_mark_node) | |
ea58ef42 | 6882 | error_init (init_loc, "initializer element is not constant"); |
2f6e4e97 | 6883 | else |
c1771a20 | 6884 | pedwarn_init (init_loc, OPT_Wpedantic, |
509c9d60 | 6885 | "initializer element is not constant"); |
3e4093b6 RS |
6886 | if (flag_pedantic_errors) |
6887 | inside_init = error_mark_node; | |
6888 | } | |
6889 | else if (require_constant | |
116df786 RH |
6890 | && !initializer_constant_valid_p (inside_init, |
6891 | TREE_TYPE (inside_init))) | |
3e4093b6 | 6892 | { |
ea58ef42 | 6893 | error_init (init_loc, "initializer element is not constant"); |
3e4093b6 | 6894 | inside_init = error_mark_node; |
8b40563c | 6895 | } |
928c19bb | 6896 | else if (require_constant && !maybe_const) |
3aa3c9fc | 6897 | pedwarn_init (init_loc, OPT_Wpedantic, |
928c19bb | 6898 | "initializer element is not a constant expression"); |
f735a153 | 6899 | |
90137d8f | 6900 | /* Added to enable additional -Wsuggest-attribute=format warnings. */ |
8fcef540 | 6901 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) |
68fca595 MP |
6902 | inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION, |
6903 | type, inside_init, origtype, | |
bbbbb16a | 6904 | ic_init, null_pointer_constant, |
928c19bb | 6905 | NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
6906 | return inside_init; |
6907 | } | |
f735a153 | 6908 | |
3e4093b6 | 6909 | /* Handle scalar types, including conversions. */ |
400fbf9f | 6910 | |
ab22c1fa CF |
6911 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE |
6912 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
6913 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
400fbf9f | 6914 | { |
f2a71bbc JM |
6915 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE |
6916 | && (TREE_CODE (init) == STRING_CST | |
6917 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
c2255bc4 | 6918 | inside_init = init = array_to_pointer_conversion (init_loc, init); |
8ce94e44 JM |
6919 | if (semantic_type) |
6920 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
6921 | inside_init); | |
3e4093b6 | 6922 | inside_init |
68fca595 MP |
6923 | = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type, |
6924 | inside_init, origtype, ic_init, | |
6925 | null_pointer_constant, NULL_TREE, NULL_TREE, | |
6926 | 0); | |
2f6e4e97 | 6927 | |
3274deff JW |
6928 | /* Check to see if we have already given an error message. */ |
6929 | if (inside_init == error_mark_node) | |
6930 | ; | |
3f75a254 | 6931 | else if (require_constant && !TREE_CONSTANT (inside_init)) |
400fbf9f | 6932 | { |
ea58ef42 | 6933 | error_init (init_loc, "initializer element is not constant"); |
3e4093b6 | 6934 | inside_init = error_mark_node; |
400fbf9f | 6935 | } |
3e4093b6 | 6936 | else if (require_constant |
116df786 RH |
6937 | && !initializer_constant_valid_p (inside_init, |
6938 | TREE_TYPE (inside_init))) | |
400fbf9f | 6939 | { |
ea58ef42 MP |
6940 | error_init (init_loc, "initializer element is not computable at " |
6941 | "load time"); | |
3e4093b6 | 6942 | inside_init = error_mark_node; |
400fbf9f | 6943 | } |
928c19bb | 6944 | else if (require_constant && !maybe_const) |
451b5e48 | 6945 | pedwarn_init (init_loc, OPT_Wpedantic, |
928c19bb | 6946 | "initializer element is not a constant expression"); |
3e4093b6 RS |
6947 | |
6948 | return inside_init; | |
400fbf9f | 6949 | } |
d9fc6069 | 6950 | |
3e4093b6 | 6951 | /* Come here only for records and arrays. */ |
d9fc6069 | 6952 | |
3e4093b6 | 6953 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
d9fc6069 | 6954 | { |
ea58ef42 | 6955 | error_init (init_loc, "variable-sized object may not be initialized"); |
3e4093b6 | 6956 | return error_mark_node; |
d9fc6069 | 6957 | } |
3e4093b6 | 6958 | |
ea58ef42 | 6959 | error_init (init_loc, "invalid initializer"); |
3e4093b6 | 6960 | return error_mark_node; |
d9fc6069 | 6961 | } |
400fbf9f | 6962 | \f |
3e4093b6 | 6963 | /* Handle initializers that use braces. */ |
400fbf9f | 6964 | |
3e4093b6 RS |
6965 | /* Type of object we are accumulating a constructor for. |
6966 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
6967 | static tree constructor_type; | |
400fbf9f | 6968 | |
3e4093b6 RS |
6969 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
6970 | left to fill. */ | |
6971 | static tree constructor_fields; | |
400fbf9f | 6972 | |
3e4093b6 RS |
6973 | /* For an ARRAY_TYPE, this is the specified index |
6974 | at which to store the next element we get. */ | |
6975 | static tree constructor_index; | |
400fbf9f | 6976 | |
3e4093b6 RS |
6977 | /* For an ARRAY_TYPE, this is the maximum index. */ |
6978 | static tree constructor_max_index; | |
400fbf9f | 6979 | |
3e4093b6 RS |
6980 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
6981 | static tree constructor_unfilled_fields; | |
400fbf9f | 6982 | |
3e4093b6 RS |
6983 | /* For an ARRAY_TYPE, this is the index of the first element |
6984 | not yet written out. */ | |
6985 | static tree constructor_unfilled_index; | |
895ea614 | 6986 | |
3e4093b6 RS |
6987 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
6988 | This is so we can generate gaps between fields, when appropriate. */ | |
6989 | static tree constructor_bit_index; | |
10d5caec | 6990 | |
3e4093b6 RS |
6991 | /* If we are saving up the elements rather than allocating them, |
6992 | this is the list of elements so far (in reverse order, | |
6993 | most recent first). */ | |
9771b263 | 6994 | static vec<constructor_elt, va_gc> *constructor_elements; |
ad47f1e5 | 6995 | |
3e4093b6 RS |
6996 | /* 1 if constructor should be incrementally stored into a constructor chain, |
6997 | 0 if all the elements should be kept in AVL tree. */ | |
6998 | static int constructor_incremental; | |
ad47f1e5 | 6999 | |
3e4093b6 RS |
7000 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
7001 | static int constructor_constant; | |
ad47f1e5 | 7002 | |
3e4093b6 RS |
7003 | /* 1 if so far this constructor's elements are all valid address constants. */ |
7004 | static int constructor_simple; | |
ad47f1e5 | 7005 | |
928c19bb JM |
7006 | /* 1 if this constructor has an element that cannot be part of a |
7007 | constant expression. */ | |
7008 | static int constructor_nonconst; | |
7009 | ||
3e4093b6 RS |
7010 | /* 1 if this constructor is erroneous so far. */ |
7011 | static int constructor_erroneous; | |
d45cf215 | 7012 | |
9bac5cbb G |
7013 | /* 1 if this constructor is the universal zero initializer { 0 }. */ |
7014 | static int constructor_zeroinit; | |
7015 | ||
3e4093b6 RS |
7016 | /* Structure for managing pending initializer elements, organized as an |
7017 | AVL tree. */ | |
d45cf215 | 7018 | |
3e4093b6 | 7019 | struct init_node |
d45cf215 | 7020 | { |
3e4093b6 RS |
7021 | struct init_node *left, *right; |
7022 | struct init_node *parent; | |
7023 | int balance; | |
7024 | tree purpose; | |
7025 | tree value; | |
bbbbb16a | 7026 | tree origtype; |
d45cf215 RS |
7027 | }; |
7028 | ||
3e4093b6 RS |
7029 | /* Tree of pending elements at this constructor level. |
7030 | These are elements encountered out of order | |
7031 | which belong at places we haven't reached yet in actually | |
7032 | writing the output. | |
7033 | Will never hold tree nodes across GC runs. */ | |
7034 | static struct init_node *constructor_pending_elts; | |
d45cf215 | 7035 | |
3e4093b6 RS |
7036 | /* The SPELLING_DEPTH of this constructor. */ |
7037 | static int constructor_depth; | |
d45cf215 | 7038 | |
3e4093b6 RS |
7039 | /* DECL node for which an initializer is being read. |
7040 | 0 means we are reading a constructor expression | |
7041 | such as (struct foo) {...}. */ | |
7042 | static tree constructor_decl; | |
d45cf215 | 7043 | |
3e4093b6 RS |
7044 | /* Nonzero if this is an initializer for a top-level decl. */ |
7045 | static int constructor_top_level; | |
d45cf215 | 7046 | |
3e4093b6 RS |
7047 | /* Nonzero if there were any member designators in this initializer. */ |
7048 | static int constructor_designated; | |
d45cf215 | 7049 | |
3e4093b6 RS |
7050 | /* Nesting depth of designator list. */ |
7051 | static int designator_depth; | |
d45cf215 | 7052 | |
3e4093b6 | 7053 | /* Nonzero if there were diagnosed errors in this designator list. */ |
b06df647 | 7054 | static int designator_erroneous; |
d45cf215 | 7055 | |
3e4093b6 RS |
7056 | \f |
7057 | /* This stack has a level for each implicit or explicit level of | |
7058 | structuring in the initializer, including the outermost one. It | |
7059 | saves the values of most of the variables above. */ | |
d45cf215 | 7060 | |
3e4093b6 RS |
7061 | struct constructor_range_stack; |
7062 | ||
7063 | struct constructor_stack | |
d45cf215 | 7064 | { |
3e4093b6 RS |
7065 | struct constructor_stack *next; |
7066 | tree type; | |
7067 | tree fields; | |
7068 | tree index; | |
7069 | tree max_index; | |
7070 | tree unfilled_index; | |
7071 | tree unfilled_fields; | |
7072 | tree bit_index; | |
9771b263 | 7073 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
7074 | struct init_node *pending_elts; |
7075 | int offset; | |
7076 | int depth; | |
916c5919 | 7077 | /* If value nonzero, this value should replace the entire |
3e4093b6 | 7078 | constructor at this level. */ |
916c5919 | 7079 | struct c_expr replacement_value; |
3e4093b6 RS |
7080 | struct constructor_range_stack *range_stack; |
7081 | char constant; | |
7082 | char simple; | |
928c19bb | 7083 | char nonconst; |
3e4093b6 RS |
7084 | char implicit; |
7085 | char erroneous; | |
7086 | char outer; | |
7087 | char incremental; | |
7088 | char designated; | |
976d5a22 | 7089 | int designator_depth; |
3e4093b6 | 7090 | }; |
d45cf215 | 7091 | |
802415d1 | 7092 | static struct constructor_stack *constructor_stack; |
d45cf215 | 7093 | |
3e4093b6 RS |
7094 | /* This stack represents designators from some range designator up to |
7095 | the last designator in the list. */ | |
d45cf215 | 7096 | |
3e4093b6 RS |
7097 | struct constructor_range_stack |
7098 | { | |
7099 | struct constructor_range_stack *next, *prev; | |
7100 | struct constructor_stack *stack; | |
7101 | tree range_start; | |
7102 | tree index; | |
7103 | tree range_end; | |
7104 | tree fields; | |
7105 | }; | |
d45cf215 | 7106 | |
802415d1 | 7107 | static struct constructor_range_stack *constructor_range_stack; |
d45cf215 | 7108 | |
3e4093b6 RS |
7109 | /* This stack records separate initializers that are nested. |
7110 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
7111 | in cases like { ... (struct foo) { ... } ... }. */ | |
d45cf215 | 7112 | |
3e4093b6 | 7113 | struct initializer_stack |
d45cf215 | 7114 | { |
3e4093b6 RS |
7115 | struct initializer_stack *next; |
7116 | tree decl; | |
3e4093b6 RS |
7117 | struct constructor_stack *constructor_stack; |
7118 | struct constructor_range_stack *constructor_range_stack; | |
9771b263 | 7119 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
7120 | struct spelling *spelling; |
7121 | struct spelling *spelling_base; | |
7122 | int spelling_size; | |
7123 | char top_level; | |
7124 | char require_constant_value; | |
7125 | char require_constant_elements; | |
7126 | }; | |
d45cf215 | 7127 | |
802415d1 | 7128 | static struct initializer_stack *initializer_stack; |
3e4093b6 RS |
7129 | \f |
7130 | /* Prepare to parse and output the initializer for variable DECL. */ | |
400fbf9f JW |
7131 | |
7132 | void | |
a396f8ae | 7133 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) |
400fbf9f | 7134 | { |
3e4093b6 | 7135 | const char *locus; |
cceb1885 | 7136 | struct initializer_stack *p = XNEW (struct initializer_stack); |
400fbf9f | 7137 | |
3e4093b6 | 7138 | p->decl = constructor_decl; |
3e4093b6 RS |
7139 | p->require_constant_value = require_constant_value; |
7140 | p->require_constant_elements = require_constant_elements; | |
7141 | p->constructor_stack = constructor_stack; | |
7142 | p->constructor_range_stack = constructor_range_stack; | |
7143 | p->elements = constructor_elements; | |
7144 | p->spelling = spelling; | |
7145 | p->spelling_base = spelling_base; | |
7146 | p->spelling_size = spelling_size; | |
7147 | p->top_level = constructor_top_level; | |
7148 | p->next = initializer_stack; | |
7149 | initializer_stack = p; | |
400fbf9f | 7150 | |
3e4093b6 | 7151 | constructor_decl = decl; |
3e4093b6 RS |
7152 | constructor_designated = 0; |
7153 | constructor_top_level = top_level; | |
400fbf9f | 7154 | |
6f17bbcf | 7155 | if (decl != 0 && decl != error_mark_node) |
3e4093b6 RS |
7156 | { |
7157 | require_constant_value = TREE_STATIC (decl); | |
7158 | require_constant_elements | |
7159 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
7160 | /* For a scalar, you can always use any value to initialize, | |
7161 | even within braces. */ | |
296a8c2f | 7162 | && AGGREGATE_TYPE_P (TREE_TYPE (decl))); |
88388a52 | 7163 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); |
3e4093b6 RS |
7164 | } |
7165 | else | |
7166 | { | |
7167 | require_constant_value = 0; | |
7168 | require_constant_elements = 0; | |
88388a52 | 7169 | locus = _("(anonymous)"); |
3e4093b6 | 7170 | } |
b71c7f8a | 7171 | |
3e4093b6 RS |
7172 | constructor_stack = 0; |
7173 | constructor_range_stack = 0; | |
b71c7f8a | 7174 | |
9bac5cbb | 7175 | found_missing_braces = 0; |
3e4093b6 RS |
7176 | |
7177 | spelling_base = 0; | |
7178 | spelling_size = 0; | |
7179 | RESTORE_SPELLING_DEPTH (0); | |
7180 | ||
7181 | if (locus) | |
7182 | push_string (locus); | |
7183 | } | |
7184 | ||
7185 | void | |
7186 | finish_init (void) | |
b71c7f8a | 7187 | { |
3e4093b6 | 7188 | struct initializer_stack *p = initializer_stack; |
b71c7f8a | 7189 | |
3e4093b6 RS |
7190 | /* Free the whole constructor stack of this initializer. */ |
7191 | while (constructor_stack) | |
7192 | { | |
7193 | struct constructor_stack *q = constructor_stack; | |
7194 | constructor_stack = q->next; | |
7195 | free (q); | |
7196 | } | |
7197 | ||
366de0ce | 7198 | gcc_assert (!constructor_range_stack); |
3e4093b6 RS |
7199 | |
7200 | /* Pop back to the data of the outer initializer (if any). */ | |
36579663 | 7201 | free (spelling_base); |
3aeb3655 | 7202 | |
3e4093b6 | 7203 | constructor_decl = p->decl; |
3e4093b6 RS |
7204 | require_constant_value = p->require_constant_value; |
7205 | require_constant_elements = p->require_constant_elements; | |
7206 | constructor_stack = p->constructor_stack; | |
7207 | constructor_range_stack = p->constructor_range_stack; | |
7208 | constructor_elements = p->elements; | |
7209 | spelling = p->spelling; | |
7210 | spelling_base = p->spelling_base; | |
7211 | spelling_size = p->spelling_size; | |
7212 | constructor_top_level = p->top_level; | |
7213 | initializer_stack = p->next; | |
7214 | free (p); | |
b71c7f8a | 7215 | } |
400fbf9f | 7216 | \f |
3e4093b6 RS |
7217 | /* Call here when we see the initializer is surrounded by braces. |
7218 | This is instead of a call to push_init_level; | |
7219 | it is matched by a call to pop_init_level. | |
400fbf9f | 7220 | |
3e4093b6 RS |
7221 | TYPE is the type to initialize, for a constructor expression. |
7222 | For an initializer for a decl, TYPE is zero. */ | |
400fbf9f | 7223 | |
3e4093b6 RS |
7224 | void |
7225 | really_start_incremental_init (tree type) | |
400fbf9f | 7226 | { |
5d038c4c | 7227 | struct constructor_stack *p = XNEW (struct constructor_stack); |
400fbf9f | 7228 | |
3e4093b6 RS |
7229 | if (type == 0) |
7230 | type = TREE_TYPE (constructor_decl); | |
400fbf9f | 7231 | |
31521951 | 7232 | if (VECTOR_TYPE_P (type) |
b6fc2cdb | 7233 | && TYPE_VECTOR_OPAQUE (type)) |
3e4093b6 | 7234 | error ("opaque vector types cannot be initialized"); |
400fbf9f | 7235 | |
3e4093b6 RS |
7236 | p->type = constructor_type; |
7237 | p->fields = constructor_fields; | |
7238 | p->index = constructor_index; | |
7239 | p->max_index = constructor_max_index; | |
7240 | p->unfilled_index = constructor_unfilled_index; | |
7241 | p->unfilled_fields = constructor_unfilled_fields; | |
7242 | p->bit_index = constructor_bit_index; | |
7243 | p->elements = constructor_elements; | |
7244 | p->constant = constructor_constant; | |
7245 | p->simple = constructor_simple; | |
928c19bb | 7246 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
7247 | p->erroneous = constructor_erroneous; |
7248 | p->pending_elts = constructor_pending_elts; | |
7249 | p->depth = constructor_depth; | |
916c5919 JM |
7250 | p->replacement_value.value = 0; |
7251 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 7252 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
7253 | p->implicit = 0; |
7254 | p->range_stack = 0; | |
7255 | p->outer = 0; | |
7256 | p->incremental = constructor_incremental; | |
7257 | p->designated = constructor_designated; | |
976d5a22 | 7258 | p->designator_depth = designator_depth; |
3e4093b6 RS |
7259 | p->next = 0; |
7260 | constructor_stack = p; | |
b13aca19 | 7261 | |
3e4093b6 RS |
7262 | constructor_constant = 1; |
7263 | constructor_simple = 1; | |
928c19bb | 7264 | constructor_nonconst = 0; |
3e4093b6 | 7265 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 7266 | constructor_elements = NULL; |
3e4093b6 RS |
7267 | constructor_pending_elts = 0; |
7268 | constructor_type = type; | |
7269 | constructor_incremental = 1; | |
7270 | constructor_designated = 0; | |
9bac5cbb | 7271 | constructor_zeroinit = 1; |
3e4093b6 | 7272 | designator_depth = 0; |
b06df647 | 7273 | designator_erroneous = 0; |
400fbf9f | 7274 | |
3e4093b6 RS |
7275 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
7276 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 7277 | { |
3e4093b6 RS |
7278 | constructor_fields = TYPE_FIELDS (constructor_type); |
7279 | /* Skip any nameless bit fields at the beginning. */ | |
7280 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
7281 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 7282 | constructor_fields = DECL_CHAIN (constructor_fields); |
05bccae2 | 7283 | |
3e4093b6 RS |
7284 | constructor_unfilled_fields = constructor_fields; |
7285 | constructor_bit_index = bitsize_zero_node; | |
400fbf9f | 7286 | } |
3e4093b6 RS |
7287 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
7288 | { | |
7289 | if (TYPE_DOMAIN (constructor_type)) | |
7290 | { | |
7291 | constructor_max_index | |
7292 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 7293 | |
3e4093b6 RS |
7294 | /* Detect non-empty initializations of zero-length arrays. */ |
7295 | if (constructor_max_index == NULL_TREE | |
7296 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 7297 | constructor_max_index = integer_minus_one_node; |
400fbf9f | 7298 | |
3e4093b6 RS |
7299 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
7300 | to initialize VLAs will cause a proper error; avoid tree | |
7301 | checking errors as well by setting a safe value. */ | |
7302 | if (constructor_max_index | |
7303 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 7304 | constructor_max_index = integer_minus_one_node; |
59c83dbf | 7305 | |
3e4093b6 RS |
7306 | constructor_index |
7307 | = convert (bitsizetype, | |
7308 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
59c83dbf | 7309 | } |
3e4093b6 | 7310 | else |
493179da JM |
7311 | { |
7312 | constructor_index = bitsize_zero_node; | |
7313 | constructor_max_index = NULL_TREE; | |
7314 | } | |
59c83dbf | 7315 | |
3e4093b6 RS |
7316 | constructor_unfilled_index = constructor_index; |
7317 | } | |
31521951 | 7318 | else if (VECTOR_TYPE_P (constructor_type)) |
3e4093b6 RS |
7319 | { |
7320 | /* Vectors are like simple fixed-size arrays. */ | |
7321 | constructor_max_index = | |
c62c040f | 7322 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
a0f0ab9f | 7323 | constructor_index = bitsize_zero_node; |
3e4093b6 RS |
7324 | constructor_unfilled_index = constructor_index; |
7325 | } | |
7326 | else | |
7327 | { | |
7328 | /* Handle the case of int x = {5}; */ | |
7329 | constructor_fields = constructor_type; | |
7330 | constructor_unfilled_fields = constructor_type; | |
7331 | } | |
7332 | } | |
7333 | \f | |
7334 | /* Push down into a subobject, for initialization. | |
7335 | If this is for an explicit set of braces, IMPLICIT is 0. | |
7336 | If it is because the next element belongs at a lower level, | |
7337 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
400fbf9f | 7338 | |
3e4093b6 | 7339 | void |
ea58ef42 MP |
7340 | push_init_level (location_t loc, int implicit, |
7341 | struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7342 | { |
7343 | struct constructor_stack *p; | |
7344 | tree value = NULL_TREE; | |
400fbf9f | 7345 | |
3e4093b6 | 7346 | /* If we've exhausted any levels that didn't have braces, |
472d98b4 JM |
7347 | pop them now. If implicit == 1, this will have been done in |
7348 | process_init_element; do not repeat it here because in the case | |
7349 | of excess initializers for an empty aggregate this leads to an | |
7350 | infinite cycle of popping a level and immediately recreating | |
7351 | it. */ | |
7352 | if (implicit != 1) | |
3e4093b6 | 7353 | { |
472d98b4 JM |
7354 | while (constructor_stack->implicit) |
7355 | { | |
7356 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7357 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7358 | && constructor_fields == 0) | |
34cf811f | 7359 | process_init_element (input_location, |
ea58ef42 | 7360 | pop_init_level (loc, 1, braced_init_obstack), |
a1e3b3d9 | 7361 | true, braced_init_obstack); |
472d98b4 JM |
7362 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE |
7363 | && constructor_max_index | |
7364 | && tree_int_cst_lt (constructor_max_index, | |
7365 | constructor_index)) | |
34cf811f | 7366 | process_init_element (input_location, |
ea58ef42 | 7367 | pop_init_level (loc, 1, braced_init_obstack), |
a1e3b3d9 | 7368 | true, braced_init_obstack); |
472d98b4 JM |
7369 | else |
7370 | break; | |
7371 | } | |
3e4093b6 | 7372 | } |
400fbf9f | 7373 | |
3e4093b6 RS |
7374 | /* Unless this is an explicit brace, we need to preserve previous |
7375 | content if any. */ | |
7376 | if (implicit) | |
7377 | { | |
7378 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7379 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7380 | && constructor_fields) | |
a1e3b3d9 | 7381 | value = find_init_member (constructor_fields, braced_init_obstack); |
3e4093b6 | 7382 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
a1e3b3d9 | 7383 | value = find_init_member (constructor_index, braced_init_obstack); |
400fbf9f JW |
7384 | } |
7385 | ||
5d038c4c | 7386 | p = XNEW (struct constructor_stack); |
3e4093b6 RS |
7387 | p->type = constructor_type; |
7388 | p->fields = constructor_fields; | |
7389 | p->index = constructor_index; | |
7390 | p->max_index = constructor_max_index; | |
7391 | p->unfilled_index = constructor_unfilled_index; | |
7392 | p->unfilled_fields = constructor_unfilled_fields; | |
7393 | p->bit_index = constructor_bit_index; | |
7394 | p->elements = constructor_elements; | |
7395 | p->constant = constructor_constant; | |
7396 | p->simple = constructor_simple; | |
928c19bb | 7397 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
7398 | p->erroneous = constructor_erroneous; |
7399 | p->pending_elts = constructor_pending_elts; | |
7400 | p->depth = constructor_depth; | |
916c5919 JM |
7401 | p->replacement_value.value = 0; |
7402 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 7403 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
7404 | p->implicit = implicit; |
7405 | p->outer = 0; | |
7406 | p->incremental = constructor_incremental; | |
7407 | p->designated = constructor_designated; | |
976d5a22 | 7408 | p->designator_depth = designator_depth; |
3e4093b6 RS |
7409 | p->next = constructor_stack; |
7410 | p->range_stack = 0; | |
7411 | constructor_stack = p; | |
400fbf9f | 7412 | |
3e4093b6 RS |
7413 | constructor_constant = 1; |
7414 | constructor_simple = 1; | |
928c19bb | 7415 | constructor_nonconst = 0; |
3e4093b6 | 7416 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 7417 | constructor_elements = NULL; |
3e4093b6 RS |
7418 | constructor_incremental = 1; |
7419 | constructor_designated = 0; | |
7420 | constructor_pending_elts = 0; | |
7421 | if (!implicit) | |
400fbf9f | 7422 | { |
3e4093b6 RS |
7423 | p->range_stack = constructor_range_stack; |
7424 | constructor_range_stack = 0; | |
7425 | designator_depth = 0; | |
b06df647 | 7426 | designator_erroneous = 0; |
3e4093b6 | 7427 | } |
400fbf9f | 7428 | |
3e4093b6 RS |
7429 | /* Don't die if an entire brace-pair level is superfluous |
7430 | in the containing level. */ | |
7431 | if (constructor_type == 0) | |
7432 | ; | |
7433 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
7434 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7435 | { | |
7436 | /* Don't die if there are extra init elts at the end. */ | |
7437 | if (constructor_fields == 0) | |
7438 | constructor_type = 0; | |
7439 | else | |
400fbf9f | 7440 | { |
3e4093b6 RS |
7441 | constructor_type = TREE_TYPE (constructor_fields); |
7442 | push_member_name (constructor_fields); | |
7443 | constructor_depth++; | |
400fbf9f | 7444 | } |
6a358dcb MP |
7445 | /* If upper initializer is designated, then mark this as |
7446 | designated too to prevent bogus warnings. */ | |
7447 | constructor_designated = p->designated; | |
3e4093b6 RS |
7448 | } |
7449 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7450 | { | |
7451 | constructor_type = TREE_TYPE (constructor_type); | |
ae7e9ddd | 7452 | push_array_bounds (tree_to_uhwi (constructor_index)); |
3e4093b6 | 7453 | constructor_depth++; |
400fbf9f JW |
7454 | } |
7455 | ||
3e4093b6 | 7456 | if (constructor_type == 0) |
400fbf9f | 7457 | { |
ea58ef42 | 7458 | error_init (loc, "extra brace group at end of initializer"); |
3e4093b6 RS |
7459 | constructor_fields = 0; |
7460 | constructor_unfilled_fields = 0; | |
7461 | return; | |
400fbf9f JW |
7462 | } |
7463 | ||
3e4093b6 RS |
7464 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
7465 | { | |
7466 | constructor_constant = TREE_CONSTANT (value); | |
7467 | constructor_simple = TREE_STATIC (value); | |
928c19bb | 7468 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); |
3e4093b6 | 7469 | constructor_elements = CONSTRUCTOR_ELTS (value); |
9771b263 | 7470 | if (!vec_safe_is_empty (constructor_elements) |
3e4093b6 RS |
7471 | && (TREE_CODE (constructor_type) == RECORD_TYPE |
7472 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
a1e3b3d9 | 7473 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 7474 | } |
400fbf9f | 7475 | |
9bac5cbb G |
7476 | if (implicit == 1) |
7477 | found_missing_braces = 1; | |
400fbf9f | 7478 | |
3e4093b6 RS |
7479 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
7480 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7481 | { | |
7482 | constructor_fields = TYPE_FIELDS (constructor_type); | |
7483 | /* Skip any nameless bit fields at the beginning. */ | |
7484 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
7485 | && DECL_NAME (constructor_fields) == 0) | |
f7587ed0 | 7486 | constructor_fields = DECL_CHAIN (constructor_fields); |
103b7b17 | 7487 | |
3e4093b6 RS |
7488 | constructor_unfilled_fields = constructor_fields; |
7489 | constructor_bit_index = bitsize_zero_node; | |
7490 | } | |
31521951 | 7491 | else if (VECTOR_TYPE_P (constructor_type)) |
3e4093b6 RS |
7492 | { |
7493 | /* Vectors are like simple fixed-size arrays. */ | |
7494 | constructor_max_index = | |
c62c040f RG |
7495 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
7496 | constructor_index = bitsize_int (0); | |
3e4093b6 RS |
7497 | constructor_unfilled_index = constructor_index; |
7498 | } | |
7499 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7500 | { | |
7501 | if (TYPE_DOMAIN (constructor_type)) | |
7502 | { | |
7503 | constructor_max_index | |
7504 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 7505 | |
3e4093b6 RS |
7506 | /* Detect non-empty initializations of zero-length arrays. */ |
7507 | if (constructor_max_index == NULL_TREE | |
7508 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 7509 | constructor_max_index = integer_minus_one_node; |
de520661 | 7510 | |
3e4093b6 RS |
7511 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
7512 | to initialize VLAs will cause a proper error; avoid tree | |
7513 | checking errors as well by setting a safe value. */ | |
7514 | if (constructor_max_index | |
7515 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 7516 | constructor_max_index = integer_minus_one_node; |
b62acd60 | 7517 | |
3e4093b6 RS |
7518 | constructor_index |
7519 | = convert (bitsizetype, | |
7520 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7521 | } | |
7522 | else | |
7523 | constructor_index = bitsize_zero_node; | |
de520661 | 7524 | |
3e4093b6 RS |
7525 | constructor_unfilled_index = constructor_index; |
7526 | if (value && TREE_CODE (value) == STRING_CST) | |
7527 | { | |
7528 | /* We need to split the char/wchar array into individual | |
7529 | characters, so that we don't have to special case it | |
7530 | everywhere. */ | |
a1e3b3d9 | 7531 | set_nonincremental_init_from_string (value, braced_init_obstack); |
3e4093b6 RS |
7532 | } |
7533 | } | |
7534 | else | |
7535 | { | |
b4519d39 | 7536 | if (constructor_type != error_mark_node) |
96b40f8d | 7537 | warning_init (input_location, 0, "braces around scalar initializer"); |
3e4093b6 RS |
7538 | constructor_fields = constructor_type; |
7539 | constructor_unfilled_fields = constructor_type; | |
7540 | } | |
7541 | } | |
8b6a5902 | 7542 | |
3e4093b6 | 7543 | /* At the end of an implicit or explicit brace level, |
916c5919 JM |
7544 | finish up that level of constructor. If a single expression |
7545 | with redundant braces initialized that level, return the | |
7546 | c_expr structure for that expression. Otherwise, the original_code | |
7547 | element is set to ERROR_MARK. | |
7548 | If we were outputting the elements as they are read, return 0 as the value | |
3e4093b6 | 7549 | from inner levels (process_init_element ignores that), |
916c5919 | 7550 | but return error_mark_node as the value from the outermost level |
3e4093b6 | 7551 | (that's what we want to put in DECL_INITIAL). |
916c5919 | 7552 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
de520661 | 7553 | |
916c5919 | 7554 | struct c_expr |
ea58ef42 MP |
7555 | pop_init_level (location_t loc, int implicit, |
7556 | struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7557 | { |
7558 | struct constructor_stack *p; | |
916c5919 JM |
7559 | struct c_expr ret; |
7560 | ret.value = 0; | |
7561 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 7562 | ret.original_type = NULL; |
de520661 | 7563 | |
3e4093b6 RS |
7564 | if (implicit == 0) |
7565 | { | |
7566 | /* When we come to an explicit close brace, | |
7567 | pop any inner levels that didn't have explicit braces. */ | |
7568 | while (constructor_stack->implicit) | |
34cf811f | 7569 | process_init_element (input_location, |
ea58ef42 | 7570 | pop_init_level (loc, 1, braced_init_obstack), |
34cf811f | 7571 | true, braced_init_obstack); |
366de0ce | 7572 | gcc_assert (!constructor_range_stack); |
3e4093b6 | 7573 | } |
e5e809f4 | 7574 | |
0066ef9c RH |
7575 | /* Now output all pending elements. */ |
7576 | constructor_incremental = 1; | |
a1e3b3d9 | 7577 | output_pending_init_elements (1, braced_init_obstack); |
0066ef9c | 7578 | |
3e4093b6 | 7579 | p = constructor_stack; |
e5e809f4 | 7580 | |
3e4093b6 RS |
7581 | /* Error for initializing a flexible array member, or a zero-length |
7582 | array member in an inappropriate context. */ | |
7583 | if (constructor_type && constructor_fields | |
7584 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
7585 | && TYPE_DOMAIN (constructor_type) | |
3f75a254 | 7586 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
3e4093b6 RS |
7587 | { |
7588 | /* Silently discard empty initializations. The parser will | |
7589 | already have pedwarned for empty brackets. */ | |
7590 | if (integer_zerop (constructor_unfilled_index)) | |
7591 | constructor_type = NULL_TREE; | |
366de0ce | 7592 | else |
3e4093b6 | 7593 | { |
366de0ce | 7594 | gcc_assert (!TYPE_SIZE (constructor_type)); |
c22cacf3 | 7595 | |
3e4093b6 | 7596 | if (constructor_depth > 2) |
ea58ef42 | 7597 | error_init (loc, "initialization of flexible array member in a nested context"); |
fcf73884 | 7598 | else |
d033409e | 7599 | pedwarn_init (loc, OPT_Wpedantic, |
509c9d60 | 7600 | "initialization of a flexible array member"); |
de520661 | 7601 | |
3e4093b6 RS |
7602 | /* We have already issued an error message for the existence |
7603 | of a flexible array member not at the end of the structure. | |
535a42b1 | 7604 | Discard the initializer so that we do not die later. */ |
910ad8de | 7605 | if (DECL_CHAIN (constructor_fields) != NULL_TREE) |
3e4093b6 RS |
7606 | constructor_type = NULL_TREE; |
7607 | } | |
3e4093b6 | 7608 | } |
de520661 | 7609 | |
7b33f0c8 MP |
7610 | switch (vec_safe_length (constructor_elements)) |
7611 | { | |
7612 | case 0: | |
7613 | /* Initialization with { } counts as zeroinit. */ | |
7614 | constructor_zeroinit = 1; | |
7615 | break; | |
7616 | case 1: | |
7617 | /* This might be zeroinit as well. */ | |
7618 | if (integer_zerop ((*constructor_elements)[0].value)) | |
7619 | constructor_zeroinit = 1; | |
7620 | break; | |
7621 | default: | |
7622 | /* If the constructor has more than one element, it can't be { 0 }. */ | |
7623 | constructor_zeroinit = 0; | |
7624 | break; | |
7625 | } | |
9bac5cbb G |
7626 | |
7627 | /* Warn when some structs are initialized with direct aggregation. */ | |
7628 | if (!implicit && found_missing_braces && warn_missing_braces | |
7629 | && !constructor_zeroinit) | |
7b33f0c8 MP |
7630 | warning_init (loc, OPT_Wmissing_braces, |
7631 | "missing braces around initializer"); | |
9bac5cbb | 7632 | |
3e4093b6 | 7633 | /* Warn when some struct elements are implicitly initialized to zero. */ |
eaac4679 | 7634 | if (warn_missing_field_initializers |
3e4093b6 RS |
7635 | && constructor_type |
7636 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
7637 | && constructor_unfilled_fields) | |
7638 | { | |
7639 | /* Do not warn for flexible array members or zero-length arrays. */ | |
7640 | while (constructor_unfilled_fields | |
3f75a254 | 7641 | && (!DECL_SIZE (constructor_unfilled_fields) |
3e4093b6 | 7642 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) |
910ad8de | 7643 | constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); |
cc77d4d5 | 7644 | |
49819fef AM |
7645 | if (constructor_unfilled_fields |
7646 | /* Do not warn if this level of the initializer uses member | |
7647 | designators; it is likely to be deliberate. */ | |
7648 | && !constructor_designated | |
84937de2 | 7649 | /* Do not warn about initializing with { 0 } or with { }. */ |
49819fef | 7650 | && !constructor_zeroinit) |
3e4093b6 | 7651 | { |
32397f22 MLI |
7652 | if (warning_at (input_location, OPT_Wmissing_field_initializers, |
7653 | "missing initializer for field %qD of %qT", | |
7654 | constructor_unfilled_fields, | |
7655 | constructor_type)) | |
7656 | inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), | |
ae933128 | 7657 | "%qD declared here", constructor_unfilled_fields); |
3e4093b6 RS |
7658 | } |
7659 | } | |
de520661 | 7660 | |
3e4093b6 | 7661 | /* Pad out the end of the structure. */ |
916c5919 | 7662 | if (p->replacement_value.value) |
3e4093b6 RS |
7663 | /* If this closes a superfluous brace pair, |
7664 | just pass out the element between them. */ | |
916c5919 | 7665 | ret = p->replacement_value; |
3e4093b6 RS |
7666 | else if (constructor_type == 0) |
7667 | ; | |
7668 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7669 | && TREE_CODE (constructor_type) != UNION_TYPE | |
7670 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
31521951 | 7671 | && !VECTOR_TYPE_P (constructor_type)) |
3e4093b6 RS |
7672 | { |
7673 | /* A nonincremental scalar initializer--just return | |
7674 | the element, after verifying there is just one. */ | |
9771b263 | 7675 | if (vec_safe_is_empty (constructor_elements)) |
3e4093b6 RS |
7676 | { |
7677 | if (!constructor_erroneous) | |
ea58ef42 | 7678 | error_init (loc, "empty scalar initializer"); |
916c5919 | 7679 | ret.value = error_mark_node; |
3e4093b6 | 7680 | } |
9771b263 | 7681 | else if (vec_safe_length (constructor_elements) != 1) |
3e4093b6 | 7682 | { |
ea58ef42 | 7683 | error_init (loc, "extra elements in scalar initializer"); |
9771b263 | 7684 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7685 | } |
7686 | else | |
9771b263 | 7687 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7688 | } |
7689 | else | |
7690 | { | |
7691 | if (constructor_erroneous) | |
916c5919 | 7692 | ret.value = error_mark_node; |
3e4093b6 RS |
7693 | else |
7694 | { | |
916c5919 | 7695 | ret.value = build_constructor (constructor_type, |
4038c495 | 7696 | constructor_elements); |
3e4093b6 | 7697 | if (constructor_constant) |
51eed280 | 7698 | TREE_CONSTANT (ret.value) = 1; |
3e4093b6 | 7699 | if (constructor_constant && constructor_simple) |
916c5919 | 7700 | TREE_STATIC (ret.value) = 1; |
928c19bb JM |
7701 | if (constructor_nonconst) |
7702 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
3e4093b6 RS |
7703 | } |
7704 | } | |
de520661 | 7705 | |
928c19bb JM |
7706 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) |
7707 | { | |
7708 | if (constructor_nonconst) | |
7709 | ret.original_code = C_MAYBE_CONST_EXPR; | |
7710 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
7711 | ret.original_code = ERROR_MARK; | |
7712 | } | |
7713 | ||
3e4093b6 RS |
7714 | constructor_type = p->type; |
7715 | constructor_fields = p->fields; | |
7716 | constructor_index = p->index; | |
7717 | constructor_max_index = p->max_index; | |
7718 | constructor_unfilled_index = p->unfilled_index; | |
7719 | constructor_unfilled_fields = p->unfilled_fields; | |
7720 | constructor_bit_index = p->bit_index; | |
7721 | constructor_elements = p->elements; | |
7722 | constructor_constant = p->constant; | |
7723 | constructor_simple = p->simple; | |
928c19bb | 7724 | constructor_nonconst = p->nonconst; |
3e4093b6 RS |
7725 | constructor_erroneous = p->erroneous; |
7726 | constructor_incremental = p->incremental; | |
7727 | constructor_designated = p->designated; | |
976d5a22 | 7728 | designator_depth = p->designator_depth; |
3e4093b6 RS |
7729 | constructor_pending_elts = p->pending_elts; |
7730 | constructor_depth = p->depth; | |
7731 | if (!p->implicit) | |
7732 | constructor_range_stack = p->range_stack; | |
7733 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
de520661 | 7734 | |
3e4093b6 RS |
7735 | constructor_stack = p->next; |
7736 | free (p); | |
b621a4dd | 7737 | |
5d5e98dc VR |
7738 | if (ret.value == 0 && constructor_stack == 0) |
7739 | ret.value = error_mark_node; | |
916c5919 | 7740 | return ret; |
3e4093b6 | 7741 | } |
8b6a5902 | 7742 | |
3e4093b6 RS |
7743 | /* Common handling for both array range and field name designators. |
7744 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
400fbf9f | 7745 | |
3e4093b6 | 7746 | static int |
ea58ef42 MP |
7747 | set_designator (location_t loc, int array, |
7748 | struct obstack *braced_init_obstack) | |
de520661 | 7749 | { |
3e4093b6 RS |
7750 | tree subtype; |
7751 | enum tree_code subcode; | |
de520661 | 7752 | |
3e4093b6 RS |
7753 | /* Don't die if an entire brace-pair level is superfluous |
7754 | in the containing level. */ | |
7755 | if (constructor_type == 0) | |
7756 | return 1; | |
de520661 | 7757 | |
366de0ce NS |
7758 | /* If there were errors in this designator list already, bail out |
7759 | silently. */ | |
b06df647 | 7760 | if (designator_erroneous) |
3e4093b6 | 7761 | return 1; |
e28cae4f | 7762 | |
3e4093b6 RS |
7763 | if (!designator_depth) |
7764 | { | |
366de0ce | 7765 | gcc_assert (!constructor_range_stack); |
de520661 | 7766 | |
3e4093b6 RS |
7767 | /* Designator list starts at the level of closest explicit |
7768 | braces. */ | |
7769 | while (constructor_stack->implicit) | |
34cf811f | 7770 | process_init_element (input_location, |
ea58ef42 | 7771 | pop_init_level (loc, 1, braced_init_obstack), |
34cf811f | 7772 | true, braced_init_obstack); |
3e4093b6 RS |
7773 | constructor_designated = 1; |
7774 | return 0; | |
7775 | } | |
de520661 | 7776 | |
366de0ce | 7777 | switch (TREE_CODE (constructor_type)) |
3c3fa147 | 7778 | { |
366de0ce NS |
7779 | case RECORD_TYPE: |
7780 | case UNION_TYPE: | |
3e4093b6 RS |
7781 | subtype = TREE_TYPE (constructor_fields); |
7782 | if (subtype != error_mark_node) | |
7783 | subtype = TYPE_MAIN_VARIANT (subtype); | |
366de0ce NS |
7784 | break; |
7785 | case ARRAY_TYPE: | |
3e4093b6 | 7786 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
366de0ce NS |
7787 | break; |
7788 | default: | |
7789 | gcc_unreachable (); | |
de520661 | 7790 | } |
400fbf9f | 7791 | |
3e4093b6 RS |
7792 | subcode = TREE_CODE (subtype); |
7793 | if (array && subcode != ARRAY_TYPE) | |
7794 | { | |
ea58ef42 | 7795 | error_init (loc, "array index in non-array initializer"); |
3e4093b6 RS |
7796 | return 1; |
7797 | } | |
7798 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
7799 | { | |
ea58ef42 | 7800 | error_init (loc, "field name not in record or union initializer"); |
3e4093b6 RS |
7801 | return 1; |
7802 | } | |
d45cf215 | 7803 | |
3e4093b6 | 7804 | constructor_designated = 1; |
ea58ef42 | 7805 | push_init_level (loc, 2, braced_init_obstack); |
3e4093b6 | 7806 | return 0; |
de520661 | 7807 | } |
400fbf9f | 7808 | |
3e4093b6 RS |
7809 | /* If there are range designators in designator list, push a new designator |
7810 | to constructor_range_stack. RANGE_END is end of such stack range or | |
7811 | NULL_TREE if there is no range designator at this level. */ | |
400fbf9f | 7812 | |
3e4093b6 | 7813 | static void |
a1e3b3d9 | 7814 | push_range_stack (tree range_end, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7815 | { |
7816 | struct constructor_range_stack *p; | |
400fbf9f | 7817 | |
a1e3b3d9 LB |
7818 | p = (struct constructor_range_stack *) |
7819 | obstack_alloc (braced_init_obstack, | |
7820 | sizeof (struct constructor_range_stack)); | |
3e4093b6 RS |
7821 | p->prev = constructor_range_stack; |
7822 | p->next = 0; | |
7823 | p->fields = constructor_fields; | |
7824 | p->range_start = constructor_index; | |
7825 | p->index = constructor_index; | |
7826 | p->stack = constructor_stack; | |
7827 | p->range_end = range_end; | |
8b6a5902 | 7828 | if (constructor_range_stack) |
3e4093b6 RS |
7829 | constructor_range_stack->next = p; |
7830 | constructor_range_stack = p; | |
de520661 | 7831 | } |
400fbf9f | 7832 | |
3e4093b6 RS |
7833 | /* Within an array initializer, specify the next index to be initialized. |
7834 | FIRST is that index. If LAST is nonzero, then initialize a range | |
7835 | of indices, running from FIRST through LAST. */ | |
5a7ec9d9 | 7836 | |
de520661 | 7837 | void |
ea58ef42 | 7838 | set_init_index (location_t loc, tree first, tree last, |
d033409e | 7839 | struct obstack *braced_init_obstack) |
de520661 | 7840 | { |
ea58ef42 | 7841 | if (set_designator (loc, 1, braced_init_obstack)) |
3e4093b6 | 7842 | return; |
de520661 | 7843 | |
b06df647 | 7844 | designator_erroneous = 1; |
de520661 | 7845 | |
3ea8cd06 JM |
7846 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
7847 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
7848 | { | |
ea58ef42 | 7849 | error_init (loc, "array index in initializer not of integer type"); |
3ea8cd06 JM |
7850 | return; |
7851 | } | |
7852 | ||
2b6da65c JM |
7853 | if (TREE_CODE (first) != INTEGER_CST) |
7854 | { | |
7855 | first = c_fully_fold (first, false, NULL); | |
7856 | if (TREE_CODE (first) == INTEGER_CST) | |
d033409e | 7857 | pedwarn_init (loc, OPT_Wpedantic, |
2b6da65c JM |
7858 | "array index in initializer is not " |
7859 | "an integer constant expression"); | |
7860 | } | |
7861 | ||
7862 | if (last && TREE_CODE (last) != INTEGER_CST) | |
7863 | { | |
7864 | last = c_fully_fold (last, false, NULL); | |
7865 | if (TREE_CODE (last) == INTEGER_CST) | |
d033409e | 7866 | pedwarn_init (loc, OPT_Wpedantic, |
2b6da65c JM |
7867 | "array index in initializer is not " |
7868 | "an integer constant expression"); | |
7869 | } | |
7870 | ||
3e4093b6 | 7871 | if (TREE_CODE (first) != INTEGER_CST) |
ea58ef42 | 7872 | error_init (loc, "nonconstant array index in initializer"); |
3e4093b6 | 7873 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) |
ea58ef42 | 7874 | error_init (loc, "nonconstant array index in initializer"); |
3e4093b6 | 7875 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) |
ea58ef42 | 7876 | error_init (loc, "array index in non-array initializer"); |
622adc7e | 7877 | else if (tree_int_cst_sgn (first) == -1) |
ea58ef42 | 7878 | error_init (loc, "array index in initializer exceeds array bounds"); |
3e4093b6 RS |
7879 | else if (constructor_max_index |
7880 | && tree_int_cst_lt (constructor_max_index, first)) | |
ea58ef42 | 7881 | error_init (loc, "array index in initializer exceeds array bounds"); |
3e4093b6 | 7882 | else |
de520661 | 7883 | { |
928c19bb JM |
7884 | constant_expression_warning (first); |
7885 | if (last) | |
7886 | constant_expression_warning (last); | |
3e4093b6 | 7887 | constructor_index = convert (bitsizetype, first); |
40d3d530 JR |
7888 | if (tree_int_cst_lt (constructor_index, first)) |
7889 | { | |
7890 | constructor_index = copy_node (constructor_index); | |
7891 | TREE_OVERFLOW (constructor_index) = 1; | |
7892 | } | |
665f2503 | 7893 | |
3e4093b6 | 7894 | if (last) |
2bede729 | 7895 | { |
3e4093b6 RS |
7896 | if (tree_int_cst_equal (first, last)) |
7897 | last = 0; | |
7898 | else if (tree_int_cst_lt (last, first)) | |
7899 | { | |
ea58ef42 | 7900 | error_init (loc, "empty index range in initializer"); |
3e4093b6 RS |
7901 | last = 0; |
7902 | } | |
7903 | else | |
7904 | { | |
7905 | last = convert (bitsizetype, last); | |
7906 | if (constructor_max_index != 0 | |
7907 | && tree_int_cst_lt (constructor_max_index, last)) | |
7908 | { | |
ea58ef42 MP |
7909 | error_init (loc, "array index range in initializer exceeds " |
7910 | "array bounds"); | |
3e4093b6 RS |
7911 | last = 0; |
7912 | } | |
7913 | } | |
2bede729 | 7914 | } |
fed3cef0 | 7915 | |
3e4093b6 | 7916 | designator_depth++; |
b06df647 | 7917 | designator_erroneous = 0; |
3e4093b6 | 7918 | if (constructor_range_stack || last) |
a1e3b3d9 | 7919 | push_range_stack (last, braced_init_obstack); |
de520661 | 7920 | } |
de520661 | 7921 | } |
3e4093b6 RS |
7922 | |
7923 | /* Within a struct initializer, specify the next field to be initialized. */ | |
400fbf9f | 7924 | |
de520661 | 7925 | void |
ea58ef42 MP |
7926 | set_init_label (location_t loc, tree fieldname, |
7927 | struct obstack *braced_init_obstack) | |
de520661 | 7928 | { |
0fb96aa4 | 7929 | tree field; |
94ba5069 | 7930 | |
ea58ef42 | 7931 | if (set_designator (loc, 0, braced_init_obstack)) |
3e4093b6 RS |
7932 | return; |
7933 | ||
b06df647 | 7934 | designator_erroneous = 1; |
3e4093b6 RS |
7935 | |
7936 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7937 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
94ba5069 | 7938 | { |
ea58ef42 | 7939 | error_init (loc, "field name not in record or union initializer"); |
3e4093b6 | 7940 | return; |
94ba5069 RS |
7941 | } |
7942 | ||
0fb96aa4 | 7943 | field = lookup_field (constructor_type, fieldname); |
8b6a5902 | 7944 | |
0fb96aa4 | 7945 | if (field == 0) |
9babc352 | 7946 | error_at (loc, "unknown field %qE specified in initializer", fieldname); |
3e4093b6 | 7947 | else |
0fb96aa4 JM |
7948 | do |
7949 | { | |
7950 | constructor_fields = TREE_VALUE (field); | |
7951 | designator_depth++; | |
7952 | designator_erroneous = 0; | |
7953 | if (constructor_range_stack) | |
7954 | push_range_stack (NULL_TREE, braced_init_obstack); | |
7955 | field = TREE_CHAIN (field); | |
7956 | if (field) | |
7957 | { | |
ea58ef42 | 7958 | if (set_designator (loc, 0, braced_init_obstack)) |
0fb96aa4 JM |
7959 | return; |
7960 | } | |
7961 | } | |
7962 | while (field != NULL_TREE); | |
3e4093b6 RS |
7963 | } |
7964 | \f | |
7965 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
7966 | identifies the initializer, either array index or field in a structure. | |
bbbbb16a ILT |
7967 | VALUE is the value of that index or field. If ORIGTYPE is not |
7968 | NULL_TREE, it is the original type of VALUE. | |
b295aee2 JJ |
7969 | |
7970 | IMPLICIT is true if value comes from pop_init_level (1), | |
7971 | the new initializer has been merged with the existing one | |
7972 | and thus no warnings should be emitted about overriding an | |
7973 | existing initializer. */ | |
de520661 | 7974 | |
3e4093b6 | 7975 | static void |
96b40f8d MP |
7976 | add_pending_init (location_t loc, tree purpose, tree value, tree origtype, |
7977 | bool implicit, struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7978 | { |
7979 | struct init_node *p, **q, *r; | |
7980 | ||
7981 | q = &constructor_pending_elts; | |
7982 | p = 0; | |
7983 | ||
7984 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 7985 | { |
3e4093b6 | 7986 | while (*q != 0) |
91fa3c30 | 7987 | { |
3e4093b6 RS |
7988 | p = *q; |
7989 | if (tree_int_cst_lt (purpose, p->purpose)) | |
7990 | q = &p->left; | |
7991 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
7992 | q = &p->right; | |
7993 | else | |
7994 | { | |
b295aee2 JJ |
7995 | if (!implicit) |
7996 | { | |
7997 | if (TREE_SIDE_EFFECTS (p->value)) | |
755e528f | 7998 | warning_init (loc, OPT_Woverride_init_side_effects, |
96b40f8d MP |
7999 | "initialized field with side-effects " |
8000 | "overwritten"); | |
b295aee2 | 8001 | else if (warn_override_init) |
96b40f8d MP |
8002 | warning_init (loc, OPT_Woverride_init, |
8003 | "initialized field overwritten"); | |
b295aee2 | 8004 | } |
3e4093b6 | 8005 | p->value = value; |
bbbbb16a | 8006 | p->origtype = origtype; |
3e4093b6 RS |
8007 | return; |
8008 | } | |
91fa3c30 | 8009 | } |
de520661 | 8010 | } |
3e4093b6 | 8011 | else |
de520661 | 8012 | { |
3e4093b6 | 8013 | tree bitpos; |
400fbf9f | 8014 | |
3e4093b6 RS |
8015 | bitpos = bit_position (purpose); |
8016 | while (*q != NULL) | |
8017 | { | |
8018 | p = *q; | |
8019 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
8020 | q = &p->left; | |
8021 | else if (p->purpose != purpose) | |
8022 | q = &p->right; | |
8023 | else | |
8024 | { | |
b295aee2 JJ |
8025 | if (!implicit) |
8026 | { | |
8027 | if (TREE_SIDE_EFFECTS (p->value)) | |
755e528f | 8028 | warning_init (loc, OPT_Woverride_init_side_effects, |
96b40f8d MP |
8029 | "initialized field with side-effects " |
8030 | "overwritten"); | |
b295aee2 | 8031 | else if (warn_override_init) |
96b40f8d MP |
8032 | warning_init (loc, OPT_Woverride_init, |
8033 | "initialized field overwritten"); | |
b295aee2 | 8034 | } |
3e4093b6 | 8035 | p->value = value; |
bbbbb16a | 8036 | p->origtype = origtype; |
3e4093b6 RS |
8037 | return; |
8038 | } | |
8039 | } | |
91fa3c30 | 8040 | } |
b71c7f8a | 8041 | |
a1e3b3d9 LB |
8042 | r = (struct init_node *) obstack_alloc (braced_init_obstack, |
8043 | sizeof (struct init_node)); | |
3e4093b6 RS |
8044 | r->purpose = purpose; |
8045 | r->value = value; | |
bbbbb16a | 8046 | r->origtype = origtype; |
8b6a5902 | 8047 | |
3e4093b6 RS |
8048 | *q = r; |
8049 | r->parent = p; | |
8050 | r->left = 0; | |
8051 | r->right = 0; | |
8052 | r->balance = 0; | |
b71c7f8a | 8053 | |
3e4093b6 | 8054 | while (p) |
de520661 | 8055 | { |
3e4093b6 | 8056 | struct init_node *s; |
665f2503 | 8057 | |
3e4093b6 | 8058 | if (r == p->left) |
2bede729 | 8059 | { |
3e4093b6 RS |
8060 | if (p->balance == 0) |
8061 | p->balance = -1; | |
8062 | else if (p->balance < 0) | |
8063 | { | |
8064 | if (r->balance < 0) | |
8065 | { | |
8066 | /* L rotation. */ | |
8067 | p->left = r->right; | |
8068 | if (p->left) | |
8069 | p->left->parent = p; | |
8070 | r->right = p; | |
e7b6a0ee | 8071 | |
3e4093b6 RS |
8072 | p->balance = 0; |
8073 | r->balance = 0; | |
39bc99c2 | 8074 | |
3e4093b6 RS |
8075 | s = p->parent; |
8076 | p->parent = r; | |
8077 | r->parent = s; | |
8078 | if (s) | |
8079 | { | |
8080 | if (s->left == p) | |
8081 | s->left = r; | |
8082 | else | |
8083 | s->right = r; | |
8084 | } | |
8085 | else | |
8086 | constructor_pending_elts = r; | |
8087 | } | |
8088 | else | |
8089 | { | |
8090 | /* LR rotation. */ | |
8091 | struct init_node *t = r->right; | |
e7b6a0ee | 8092 | |
3e4093b6 RS |
8093 | r->right = t->left; |
8094 | if (r->right) | |
8095 | r->right->parent = r; | |
8096 | t->left = r; | |
8097 | ||
8098 | p->left = t->right; | |
8099 | if (p->left) | |
8100 | p->left->parent = p; | |
8101 | t->right = p; | |
8102 | ||
8103 | p->balance = t->balance < 0; | |
8104 | r->balance = -(t->balance > 0); | |
8105 | t->balance = 0; | |
8106 | ||
8107 | s = p->parent; | |
8108 | p->parent = t; | |
8109 | r->parent = t; | |
8110 | t->parent = s; | |
8111 | if (s) | |
8112 | { | |
8113 | if (s->left == p) | |
8114 | s->left = t; | |
8115 | else | |
8116 | s->right = t; | |
8117 | } | |
8118 | else | |
8119 | constructor_pending_elts = t; | |
8120 | } | |
8121 | break; | |
8122 | } | |
8123 | else | |
8124 | { | |
8125 | /* p->balance == +1; growth of left side balances the node. */ | |
8126 | p->balance = 0; | |
8127 | break; | |
8128 | } | |
2bede729 | 8129 | } |
3e4093b6 RS |
8130 | else /* r == p->right */ |
8131 | { | |
8132 | if (p->balance == 0) | |
8133 | /* Growth propagation from right side. */ | |
8134 | p->balance++; | |
8135 | else if (p->balance > 0) | |
8136 | { | |
8137 | if (r->balance > 0) | |
8138 | { | |
8139 | /* R rotation. */ | |
8140 | p->right = r->left; | |
8141 | if (p->right) | |
8142 | p->right->parent = p; | |
8143 | r->left = p; | |
8144 | ||
8145 | p->balance = 0; | |
8146 | r->balance = 0; | |
8147 | ||
8148 | s = p->parent; | |
8149 | p->parent = r; | |
8150 | r->parent = s; | |
8151 | if (s) | |
8152 | { | |
8153 | if (s->left == p) | |
8154 | s->left = r; | |
8155 | else | |
8156 | s->right = r; | |
8157 | } | |
8158 | else | |
8159 | constructor_pending_elts = r; | |
8160 | } | |
8161 | else /* r->balance == -1 */ | |
8162 | { | |
8163 | /* RL rotation */ | |
8164 | struct init_node *t = r->left; | |
8165 | ||
8166 | r->left = t->right; | |
8167 | if (r->left) | |
8168 | r->left->parent = r; | |
8169 | t->right = r; | |
8170 | ||
8171 | p->right = t->left; | |
8172 | if (p->right) | |
8173 | p->right->parent = p; | |
8174 | t->left = p; | |
8175 | ||
8176 | r->balance = (t->balance < 0); | |
8177 | p->balance = -(t->balance > 0); | |
8178 | t->balance = 0; | |
8179 | ||
8180 | s = p->parent; | |
8181 | p->parent = t; | |
8182 | r->parent = t; | |
8183 | t->parent = s; | |
8184 | if (s) | |
8185 | { | |
8186 | if (s->left == p) | |
8187 | s->left = t; | |
8188 | else | |
8189 | s->right = t; | |
8190 | } | |
8191 | else | |
8192 | constructor_pending_elts = t; | |
8193 | } | |
8194 | break; | |
8195 | } | |
8196 | else | |
8197 | { | |
8198 | /* p->balance == -1; growth of right side balances the node. */ | |
8199 | p->balance = 0; | |
8200 | break; | |
8201 | } | |
8202 | } | |
8203 | ||
8204 | r = p; | |
8205 | p = p->parent; | |
8206 | } | |
8207 | } | |
8208 | ||
8209 | /* Build AVL tree from a sorted chain. */ | |
8210 | ||
8211 | static void | |
a1e3b3d9 | 8212 | set_nonincremental_init (struct obstack * braced_init_obstack) |
3e4093b6 | 8213 | { |
4038c495 GB |
8214 | unsigned HOST_WIDE_INT ix; |
8215 | tree index, value; | |
3e4093b6 RS |
8216 | |
8217 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
8218 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
8219 | return; | |
8220 | ||
4038c495 | 8221 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) |
96b40f8d MP |
8222 | add_pending_init (input_location, index, value, NULL_TREE, true, |
8223 | braced_init_obstack); | |
9771b263 | 8224 | constructor_elements = NULL; |
3e4093b6 RS |
8225 | if (TREE_CODE (constructor_type) == RECORD_TYPE) |
8226 | { | |
8227 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
8228 | /* Skip any nameless bit fields at the beginning. */ | |
8229 | while (constructor_unfilled_fields != 0 | |
8230 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8231 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8232 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
fed3cef0 | 8233 | |
de520661 | 8234 | } |
3e4093b6 | 8235 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 8236 | { |
3e4093b6 RS |
8237 | if (TYPE_DOMAIN (constructor_type)) |
8238 | constructor_unfilled_index | |
8239 | = convert (bitsizetype, | |
8240 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
8241 | else | |
8242 | constructor_unfilled_index = bitsize_zero_node; | |
de520661 | 8243 | } |
3e4093b6 | 8244 | constructor_incremental = 0; |
de520661 | 8245 | } |
400fbf9f | 8246 | |
3e4093b6 | 8247 | /* Build AVL tree from a string constant. */ |
de520661 | 8248 | |
3e4093b6 | 8249 | static void |
a1e3b3d9 LB |
8250 | set_nonincremental_init_from_string (tree str, |
8251 | struct obstack * braced_init_obstack) | |
de520661 | 8252 | { |
3e4093b6 RS |
8253 | tree value, purpose, type; |
8254 | HOST_WIDE_INT val[2]; | |
8255 | const char *p, *end; | |
8256 | int byte, wchar_bytes, charwidth, bitpos; | |
de520661 | 8257 | |
366de0ce | 8258 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); |
940ff66d | 8259 | |
c466b2cd | 8260 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; |
3e4093b6 RS |
8261 | charwidth = TYPE_PRECISION (char_type_node); |
8262 | type = TREE_TYPE (constructor_type); | |
8263 | p = TREE_STRING_POINTER (str); | |
8264 | end = p + TREE_STRING_LENGTH (str); | |
91fa3c30 | 8265 | |
3e4093b6 | 8266 | for (purpose = bitsize_zero_node; |
8824edff JJ |
8267 | p < end |
8268 | && !(constructor_max_index | |
8269 | && tree_int_cst_lt (constructor_max_index, purpose)); | |
3e4093b6 | 8270 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) |
584ef5fe | 8271 | { |
3e4093b6 | 8272 | if (wchar_bytes == 1) |
ffc5c6a9 | 8273 | { |
807e902e KZ |
8274 | val[0] = (unsigned char) *p++; |
8275 | val[1] = 0; | |
ffc5c6a9 RH |
8276 | } |
8277 | else | |
3e4093b6 | 8278 | { |
3e4093b6 | 8279 | val[1] = 0; |
807e902e | 8280 | val[0] = 0; |
3e4093b6 RS |
8281 | for (byte = 0; byte < wchar_bytes; byte++) |
8282 | { | |
8283 | if (BYTES_BIG_ENDIAN) | |
8284 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
8285 | else | |
8286 | bitpos = byte * charwidth; | |
807e902e | 8287 | val[bitpos % HOST_BITS_PER_WIDE_INT] |
3e4093b6 RS |
8288 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) |
8289 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
8290 | } | |
8291 | } | |
584ef5fe | 8292 | |
8df83eae | 8293 | if (!TYPE_UNSIGNED (type)) |
3e4093b6 RS |
8294 | { |
8295 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
8296 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
8297 | { | |
807e902e | 8298 | if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) |
3e4093b6 | 8299 | { |
aa256c4a | 8300 | val[0] |= HOST_WIDE_INT_M1U << bitpos; |
807e902e | 8301 | val[1] = -1; |
3e4093b6 RS |
8302 | } |
8303 | } | |
8304 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
8305 | { | |
807e902e KZ |
8306 | if (val[0] < 0) |
8307 | val[1] = -1; | |
3e4093b6 | 8308 | } |
807e902e | 8309 | else if (val[1] & (((HOST_WIDE_INT) 1) |
3e4093b6 | 8310 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) |
aa256c4a | 8311 | val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT); |
3e4093b6 | 8312 | } |
ffc5c6a9 | 8313 | |
807e902e KZ |
8314 | value = wide_int_to_tree (type, |
8315 | wide_int::from_array (val, 2, | |
8316 | HOST_BITS_PER_WIDE_INT * 2)); | |
96b40f8d | 8317 | add_pending_init (input_location, purpose, value, NULL_TREE, true, |
a1e3b3d9 | 8318 | braced_init_obstack); |
9dfcc8db BH |
8319 | } |
8320 | ||
3e4093b6 RS |
8321 | constructor_incremental = 0; |
8322 | } | |
de520661 | 8323 | |
3e4093b6 RS |
8324 | /* Return value of FIELD in pending initializer or zero if the field was |
8325 | not initialized yet. */ | |
8326 | ||
8327 | static tree | |
a1e3b3d9 | 8328 | find_init_member (tree field, struct obstack * braced_init_obstack) |
3e4093b6 RS |
8329 | { |
8330 | struct init_node *p; | |
8331 | ||
8332 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
19d76e60 | 8333 | { |
3e4093b6 RS |
8334 | if (constructor_incremental |
8335 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 8336 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8337 | |
8338 | p = constructor_pending_elts; | |
8339 | while (p) | |
19d76e60 | 8340 | { |
3e4093b6 RS |
8341 | if (tree_int_cst_lt (field, p->purpose)) |
8342 | p = p->left; | |
8343 | else if (tree_int_cst_lt (p->purpose, field)) | |
8344 | p = p->right; | |
8345 | else | |
8346 | return p->value; | |
19d76e60 | 8347 | } |
19d76e60 | 8348 | } |
3e4093b6 | 8349 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
de520661 | 8350 | { |
3e4093b6 | 8351 | tree bitpos = bit_position (field); |
de520661 | 8352 | |
3e4093b6 RS |
8353 | if (constructor_incremental |
8354 | && (!constructor_unfilled_fields | |
8355 | || tree_int_cst_lt (bitpos, | |
8356 | bit_position (constructor_unfilled_fields)))) | |
a1e3b3d9 | 8357 | set_nonincremental_init (braced_init_obstack); |
de520661 | 8358 | |
3e4093b6 RS |
8359 | p = constructor_pending_elts; |
8360 | while (p) | |
8361 | { | |
8362 | if (field == p->purpose) | |
8363 | return p->value; | |
8364 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
8365 | p = p->left; | |
8366 | else | |
8367 | p = p->right; | |
8368 | } | |
8369 | } | |
8370 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 8371 | { |
9771b263 DN |
8372 | if (!vec_safe_is_empty (constructor_elements) |
8373 | && (constructor_elements->last ().index == field)) | |
8374 | return constructor_elements->last ().value; | |
de520661 | 8375 | } |
3e4093b6 | 8376 | return 0; |
de520661 RS |
8377 | } |
8378 | ||
3e4093b6 RS |
8379 | /* "Output" the next constructor element. |
8380 | At top level, really output it to assembler code now. | |
8381 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
bbbbb16a | 8382 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. |
3e4093b6 RS |
8383 | TYPE is the data type that the containing data type wants here. |
8384 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
916c5919 JM |
8385 | If VALUE is a string constant, STRICT_STRING is true if it is |
8386 | unparenthesized or we should not warn here for it being parenthesized. | |
8387 | For other types of VALUE, STRICT_STRING is not used. | |
8b6a5902 | 8388 | |
3e4093b6 RS |
8389 | PENDING if non-nil means output pending elements that belong |
8390 | right after this element. (PENDING is normally 1; | |
b295aee2 JJ |
8391 | it is 0 while outputting pending elements, to avoid recursion.) |
8392 | ||
8393 | IMPLICIT is true if value comes from pop_init_level (1), | |
8394 | the new initializer has been merged with the existing one | |
8395 | and thus no warnings should be emitted about overriding an | |
8396 | existing initializer. */ | |
8b6a5902 | 8397 | |
3e4093b6 | 8398 | static void |
34cf811f MP |
8399 | output_init_element (location_t loc, tree value, tree origtype, |
8400 | bool strict_string, tree type, tree field, int pending, | |
8401 | bool implicit, struct obstack * braced_init_obstack) | |
3e4093b6 | 8402 | { |
8ce94e44 | 8403 | tree semantic_type = NULL_TREE; |
928c19bb JM |
8404 | bool maybe_const = true; |
8405 | bool npc; | |
4038c495 | 8406 | |
0a880880 | 8407 | if (type == error_mark_node || value == error_mark_node) |
8b6a5902 | 8408 | { |
3e4093b6 RS |
8409 | constructor_erroneous = 1; |
8410 | return; | |
8b6a5902 | 8411 | } |
46bdb9cf JM |
8412 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
8413 | && (TREE_CODE (value) == STRING_CST | |
8414 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
8415 | && !(TREE_CODE (value) == STRING_CST | |
8416 | && TREE_CODE (type) == ARRAY_TYPE | |
8417 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
8418 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
8419 | TYPE_MAIN_VARIANT (type))) | |
c2255bc4 | 8420 | value = array_to_pointer_conversion (input_location, value); |
8b6a5902 | 8421 | |
3e4093b6 | 8422 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
4435bb92 | 8423 | && require_constant_value && pending) |
8b6a5902 | 8424 | { |
3e4093b6 RS |
8425 | /* As an extension, allow initializing objects with static storage |
8426 | duration with compound literals (which are then treated just as | |
8427 | the brace enclosed list they contain). */ | |
4435bb92 MP |
8428 | if (flag_isoc99) |
8429 | pedwarn_init (loc, OPT_Wpedantic, "initializer element is not " | |
8430 | "constant"); | |
3e4093b6 RS |
8431 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); |
8432 | value = DECL_INITIAL (decl); | |
8b6a5902 JJ |
8433 | } |
8434 | ||
928c19bb | 8435 | npc = null_pointer_constant_p (value); |
8ce94e44 JM |
8436 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) |
8437 | { | |
8438 | semantic_type = TREE_TYPE (value); | |
8439 | value = TREE_OPERAND (value, 0); | |
8440 | } | |
928c19bb JM |
8441 | value = c_fully_fold (value, require_constant_value, &maybe_const); |
8442 | ||
3e4093b6 RS |
8443 | if (value == error_mark_node) |
8444 | constructor_erroneous = 1; | |
8445 | else if (!TREE_CONSTANT (value)) | |
8446 | constructor_constant = 0; | |
116df786 | 8447 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) |
3e4093b6 RS |
8448 | || ((TREE_CODE (constructor_type) == RECORD_TYPE |
8449 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8450 | && DECL_C_BIT_FIELD (field) | |
8451 | && TREE_CODE (value) != INTEGER_CST)) | |
8452 | constructor_simple = 0; | |
928c19bb JM |
8453 | if (!maybe_const) |
8454 | constructor_nonconst = 1; | |
3e4093b6 | 8455 | |
116df786 | 8456 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) |
8b6a5902 | 8457 | { |
116df786 RH |
8458 | if (require_constant_value) |
8459 | { | |
ea58ef42 | 8460 | error_init (loc, "initializer element is not constant"); |
116df786 RH |
8461 | value = error_mark_node; |
8462 | } | |
8463 | else if (require_constant_elements) | |
8337d1db | 8464 | pedwarn (loc, OPT_Wpedantic, |
509c9d60 | 8465 | "initializer element is not computable at load time"); |
8b6a5902 | 8466 | } |
928c19bb JM |
8467 | else if (!maybe_const |
8468 | && (require_constant_value || require_constant_elements)) | |
8337d1db | 8469 | pedwarn_init (loc, OPT_Wpedantic, |
928c19bb | 8470 | "initializer element is not a constant expression"); |
3e4093b6 | 8471 | |
81f40b79 ILT |
8472 | /* Issue -Wc++-compat warnings about initializing a bitfield with |
8473 | enum type. */ | |
8474 | if (warn_cxx_compat | |
8475 | && field != NULL_TREE | |
8476 | && TREE_CODE (field) == FIELD_DECL | |
8477 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
8478 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
8479 | != TYPE_MAIN_VARIANT (type)) | |
8480 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
8481 | { | |
8482 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
8483 | if (checktype != error_mark_node | |
8484 | && (TYPE_MAIN_VARIANT (checktype) | |
8485 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
96b40f8d | 8486 | warning_init (loc, OPT_Wc___compat, |
81f40b79 ILT |
8487 | "enum conversion in initialization is invalid in C++"); |
8488 | } | |
8489 | ||
3e4093b6 RS |
8490 | /* If this field is empty (and not at the end of structure), |
8491 | don't do anything other than checking the initializer. */ | |
8492 | if (field | |
8493 | && (TREE_TYPE (field) == error_mark_node | |
8494 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
8495 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
8496 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
910ad8de | 8497 | || DECL_CHAIN (field))))) |
3e4093b6 RS |
8498 | return; |
8499 | ||
8ce94e44 JM |
8500 | if (semantic_type) |
8501 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
34cf811f MP |
8502 | value = digest_init (loc, type, value, origtype, npc, strict_string, |
8503 | require_constant_value); | |
3e4093b6 | 8504 | if (value == error_mark_node) |
8b6a5902 | 8505 | { |
3e4093b6 RS |
8506 | constructor_erroneous = 1; |
8507 | return; | |
8b6a5902 | 8508 | } |
928c19bb JM |
8509 | if (require_constant_value || require_constant_elements) |
8510 | constant_expression_warning (value); | |
8b6a5902 | 8511 | |
3e4093b6 RS |
8512 | /* If this element doesn't come next in sequence, |
8513 | put it on constructor_pending_elts. */ | |
8514 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
8515 | && (!constructor_incremental | |
8516 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
8b6a5902 | 8517 | { |
3e4093b6 RS |
8518 | if (constructor_incremental |
8519 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 8520 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 8521 | |
96b40f8d | 8522 | add_pending_init (loc, field, value, origtype, implicit, |
a1e3b3d9 | 8523 | braced_init_obstack); |
3e4093b6 | 8524 | return; |
8b6a5902 | 8525 | } |
3e4093b6 RS |
8526 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
8527 | && (!constructor_incremental | |
8528 | || field != constructor_unfilled_fields)) | |
8b6a5902 | 8529 | { |
3e4093b6 RS |
8530 | /* We do this for records but not for unions. In a union, |
8531 | no matter which field is specified, it can be initialized | |
8532 | right away since it starts at the beginning of the union. */ | |
8533 | if (constructor_incremental) | |
8534 | { | |
8535 | if (!constructor_unfilled_fields) | |
a1e3b3d9 | 8536 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8537 | else |
8538 | { | |
8539 | tree bitpos, unfillpos; | |
8540 | ||
8541 | bitpos = bit_position (field); | |
8542 | unfillpos = bit_position (constructor_unfilled_fields); | |
8543 | ||
8544 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
a1e3b3d9 | 8545 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8546 | } |
8547 | } | |
8548 | ||
96b40f8d | 8549 | add_pending_init (loc, field, value, origtype, implicit, |
a1e3b3d9 | 8550 | braced_init_obstack); |
3e4093b6 | 8551 | return; |
8b6a5902 | 8552 | } |
3e4093b6 | 8553 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
9771b263 | 8554 | && !vec_safe_is_empty (constructor_elements)) |
3e4093b6 | 8555 | { |
b295aee2 JJ |
8556 | if (!implicit) |
8557 | { | |
9771b263 | 8558 | if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) |
755e528f | 8559 | warning_init (loc, OPT_Woverride_init_side_effects, |
b295aee2 JJ |
8560 | "initialized field with side-effects overwritten"); |
8561 | else if (warn_override_init) | |
96b40f8d MP |
8562 | warning_init (loc, OPT_Woverride_init, |
8563 | "initialized field overwritten"); | |
b295aee2 | 8564 | } |
8b6a5902 | 8565 | |
3e4093b6 | 8566 | /* We can have just one union field set. */ |
9771b263 | 8567 | constructor_elements = NULL; |
3e4093b6 | 8568 | } |
8b6a5902 | 8569 | |
3e4093b6 RS |
8570 | /* Otherwise, output this element either to |
8571 | constructor_elements or to the assembler file. */ | |
8b6a5902 | 8572 | |
f32682ca | 8573 | constructor_elt celt = {field, value}; |
9771b263 | 8574 | vec_safe_push (constructor_elements, celt); |
8b6a5902 | 8575 | |
3e4093b6 RS |
8576 | /* Advance the variable that indicates sequential elements output. */ |
8577 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8578 | constructor_unfilled_index | |
db3927fb AH |
8579 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, |
8580 | bitsize_one_node); | |
3e4093b6 RS |
8581 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
8582 | { | |
8583 | constructor_unfilled_fields | |
910ad8de | 8584 | = DECL_CHAIN (constructor_unfilled_fields); |
8b6a5902 | 8585 | |
3e4093b6 RS |
8586 | /* Skip any nameless bit fields. */ |
8587 | while (constructor_unfilled_fields != 0 | |
8588 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8589 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8590 | constructor_unfilled_fields = | |
910ad8de | 8591 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 RS |
8592 | } |
8593 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
8594 | constructor_unfilled_fields = 0; | |
de520661 | 8595 | |
3e4093b6 RS |
8596 | /* Now output any pending elements which have become next. */ |
8597 | if (pending) | |
a1e3b3d9 | 8598 | output_pending_init_elements (0, braced_init_obstack); |
3e4093b6 | 8599 | } |
8b6a5902 | 8600 | |
3e4093b6 RS |
8601 | /* Output any pending elements which have become next. |
8602 | As we output elements, constructor_unfilled_{fields,index} | |
8603 | advances, which may cause other elements to become next; | |
8604 | if so, they too are output. | |
8b6a5902 | 8605 | |
3e4093b6 RS |
8606 | If ALL is 0, we return when there are |
8607 | no more pending elements to output now. | |
665f2503 | 8608 | |
3e4093b6 RS |
8609 | If ALL is 1, we output space as necessary so that |
8610 | we can output all the pending elements. */ | |
3e4093b6 | 8611 | static void |
a1e3b3d9 | 8612 | output_pending_init_elements (int all, struct obstack * braced_init_obstack) |
3e4093b6 RS |
8613 | { |
8614 | struct init_node *elt = constructor_pending_elts; | |
8615 | tree next; | |
de520661 | 8616 | |
3e4093b6 RS |
8617 | retry: |
8618 | ||
ba228239 | 8619 | /* Look through the whole pending tree. |
3e4093b6 RS |
8620 | If we find an element that should be output now, |
8621 | output it. Otherwise, set NEXT to the element | |
8622 | that comes first among those still pending. */ | |
8623 | ||
8624 | next = 0; | |
8625 | while (elt) | |
8626 | { | |
8627 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8b6a5902 | 8628 | { |
3e4093b6 RS |
8629 | if (tree_int_cst_equal (elt->purpose, |
8630 | constructor_unfilled_index)) | |
34cf811f MP |
8631 | output_init_element (input_location, elt->value, elt->origtype, |
8632 | true, TREE_TYPE (constructor_type), | |
a1e3b3d9 LB |
8633 | constructor_unfilled_index, 0, false, |
8634 | braced_init_obstack); | |
3e4093b6 RS |
8635 | else if (tree_int_cst_lt (constructor_unfilled_index, |
8636 | elt->purpose)) | |
8b6a5902 | 8637 | { |
3e4093b6 RS |
8638 | /* Advance to the next smaller node. */ |
8639 | if (elt->left) | |
8640 | elt = elt->left; | |
8641 | else | |
8642 | { | |
8643 | /* We have reached the smallest node bigger than the | |
8644 | current unfilled index. Fill the space first. */ | |
8645 | next = elt->purpose; | |
8646 | break; | |
8647 | } | |
8b6a5902 | 8648 | } |
ce662d4c JJ |
8649 | else |
8650 | { | |
3e4093b6 RS |
8651 | /* Advance to the next bigger node. */ |
8652 | if (elt->right) | |
8653 | elt = elt->right; | |
8654 | else | |
ce662d4c | 8655 | { |
3e4093b6 RS |
8656 | /* We have reached the biggest node in a subtree. Find |
8657 | the parent of it, which is the next bigger node. */ | |
8658 | while (elt->parent && elt->parent->right == elt) | |
8659 | elt = elt->parent; | |
8660 | elt = elt->parent; | |
8661 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
8662 | elt->purpose)) | |
8663 | { | |
8664 | next = elt->purpose; | |
8665 | break; | |
8666 | } | |
ce662d4c JJ |
8667 | } |
8668 | } | |
8b6a5902 | 8669 | } |
3e4093b6 RS |
8670 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
8671 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8672 | { | |
8673 | tree ctor_unfilled_bitpos, elt_bitpos; | |
ce662d4c | 8674 | |
3e4093b6 RS |
8675 | /* If the current record is complete we are done. */ |
8676 | if (constructor_unfilled_fields == 0) | |
8677 | break; | |
de520661 | 8678 | |
3e4093b6 RS |
8679 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
8680 | elt_bitpos = bit_position (elt->purpose); | |
8681 | /* We can't compare fields here because there might be empty | |
8682 | fields in between. */ | |
8683 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
8684 | { | |
8685 | constructor_unfilled_fields = elt->purpose; | |
34cf811f MP |
8686 | output_init_element (input_location, elt->value, elt->origtype, |
8687 | true, TREE_TYPE (elt->purpose), | |
a1e3b3d9 LB |
8688 | elt->purpose, 0, false, |
8689 | braced_init_obstack); | |
3e4093b6 RS |
8690 | } |
8691 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
8692 | { | |
8693 | /* Advance to the next smaller node. */ | |
8694 | if (elt->left) | |
8695 | elt = elt->left; | |
8696 | else | |
8697 | { | |
8698 | /* We have reached the smallest node bigger than the | |
8699 | current unfilled field. Fill the space first. */ | |
8700 | next = elt->purpose; | |
8701 | break; | |
8702 | } | |
8703 | } | |
8704 | else | |
8705 | { | |
8706 | /* Advance to the next bigger node. */ | |
8707 | if (elt->right) | |
8708 | elt = elt->right; | |
8709 | else | |
8710 | { | |
8711 | /* We have reached the biggest node in a subtree. Find | |
8712 | the parent of it, which is the next bigger node. */ | |
8713 | while (elt->parent && elt->parent->right == elt) | |
8714 | elt = elt->parent; | |
8715 | elt = elt->parent; | |
8716 | if (elt | |
8717 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
8718 | bit_position (elt->purpose)))) | |
8719 | { | |
8720 | next = elt->purpose; | |
8721 | break; | |
8722 | } | |
8723 | } | |
8724 | } | |
8725 | } | |
8726 | } | |
de520661 | 8727 | |
3e4093b6 RS |
8728 | /* Ordinarily return, but not if we want to output all |
8729 | and there are elements left. */ | |
3f75a254 | 8730 | if (!(all && next != 0)) |
e5cfb88f RK |
8731 | return; |
8732 | ||
3e4093b6 RS |
8733 | /* If it's not incremental, just skip over the gap, so that after |
8734 | jumping to retry we will output the next successive element. */ | |
8735 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
8736 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8737 | constructor_unfilled_fields = next; | |
8738 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8739 | constructor_unfilled_index = next; | |
de520661 | 8740 | |
3e4093b6 RS |
8741 | /* ELT now points to the node in the pending tree with the next |
8742 | initializer to output. */ | |
8743 | goto retry; | |
de520661 RS |
8744 | } |
8745 | \f | |
3e4093b6 RS |
8746 | /* Add one non-braced element to the current constructor level. |
8747 | This adjusts the current position within the constructor's type. | |
8748 | This may also start or terminate implicit levels | |
8749 | to handle a partly-braced initializer. | |
e5e809f4 | 8750 | |
3e4093b6 | 8751 | Once this has found the correct level for the new element, |
b295aee2 JJ |
8752 | it calls output_init_element. |
8753 | ||
8754 | IMPLICIT is true if value comes from pop_init_level (1), | |
8755 | the new initializer has been merged with the existing one | |
8756 | and thus no warnings should be emitted about overriding an | |
8757 | existing initializer. */ | |
3e4093b6 RS |
8758 | |
8759 | void | |
34cf811f | 8760 | process_init_element (location_t loc, struct c_expr value, bool implicit, |
a1e3b3d9 | 8761 | struct obstack * braced_init_obstack) |
e5e809f4 | 8762 | { |
916c5919 JM |
8763 | tree orig_value = value.value; |
8764 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
8765 | bool strict_string = value.original_code == STRING_CST; | |
340baef7 | 8766 | bool was_designated = designator_depth != 0; |
e5e809f4 | 8767 | |
3e4093b6 | 8768 | designator_depth = 0; |
b06df647 | 8769 | designator_erroneous = 0; |
e5e809f4 | 8770 | |
9bac5cbb G |
8771 | if (!implicit && value.value && !integer_zerop (value.value)) |
8772 | constructor_zeroinit = 0; | |
8773 | ||
3e4093b6 RS |
8774 | /* Handle superfluous braces around string cst as in |
8775 | char x[] = {"foo"}; */ | |
8776 | if (string_flag | |
8777 | && constructor_type | |
340baef7 | 8778 | && !was_designated |
3e4093b6 | 8779 | && TREE_CODE (constructor_type) == ARRAY_TYPE |
197463ae | 8780 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
3e4093b6 | 8781 | && integer_zerop (constructor_unfilled_index)) |
e5e809f4 | 8782 | { |
916c5919 | 8783 | if (constructor_stack->replacement_value.value) |
ea58ef42 | 8784 | error_init (loc, "excess elements in char array initializer"); |
3e4093b6 RS |
8785 | constructor_stack->replacement_value = value; |
8786 | return; | |
e5e809f4 | 8787 | } |
8b6a5902 | 8788 | |
916c5919 | 8789 | if (constructor_stack->replacement_value.value != 0) |
3e4093b6 | 8790 | { |
ea58ef42 | 8791 | error_init (loc, "excess elements in struct initializer"); |
3e4093b6 | 8792 | return; |
e5e809f4 JL |
8793 | } |
8794 | ||
3e4093b6 RS |
8795 | /* Ignore elements of a brace group if it is entirely superfluous |
8796 | and has already been diagnosed. */ | |
8797 | if (constructor_type == 0) | |
8798 | return; | |
e5e809f4 | 8799 | |
976d5a22 TT |
8800 | if (!implicit && warn_designated_init && !was_designated |
8801 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
8802 | && lookup_attribute ("designated_init", | |
8803 | TYPE_ATTRIBUTES (constructor_type))) | |
8804 | warning_init (loc, | |
8805 | OPT_Wdesignated_init, | |
8806 | "positional initialization of field " | |
8807 | "in %<struct%> declared with %<designated_init%> attribute"); | |
8808 | ||
3e4093b6 RS |
8809 | /* If we've exhausted any levels that didn't have braces, |
8810 | pop them now. */ | |
8811 | while (constructor_stack->implicit) | |
8812 | { | |
8813 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
8814 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8815 | && constructor_fields == 0) | |
ea58ef42 MP |
8816 | process_init_element (loc, |
8817 | pop_init_level (loc, 1, braced_init_obstack), | |
a1e3b3d9 | 8818 | true, braced_init_obstack); |
53650abe | 8819 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE |
31521951 | 8820 | || VECTOR_TYPE_P (constructor_type)) |
8824edff JJ |
8821 | && constructor_max_index |
8822 | && tree_int_cst_lt (constructor_max_index, | |
8823 | constructor_index)) | |
ea58ef42 MP |
8824 | process_init_element (loc, |
8825 | pop_init_level (loc, 1, braced_init_obstack), | |
a1e3b3d9 | 8826 | true, braced_init_obstack); |
3e4093b6 RS |
8827 | else |
8828 | break; | |
8829 | } | |
e5e809f4 | 8830 | |
3e4093b6 RS |
8831 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
8832 | if (constructor_range_stack) | |
e5e809f4 | 8833 | { |
3e4093b6 RS |
8834 | /* If value is a compound literal and we'll be just using its |
8835 | content, don't put it into a SAVE_EXPR. */ | |
916c5919 | 8836 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
c3e38a03 | 8837 | || !require_constant_value) |
8ce94e44 JM |
8838 | { |
8839 | tree semantic_type = NULL_TREE; | |
8840 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
8841 | { | |
8842 | semantic_type = TREE_TYPE (value.value); | |
8843 | value.value = TREE_OPERAND (value.value, 0); | |
8844 | } | |
8845 | value.value = c_save_expr (value.value); | |
8846 | if (semantic_type) | |
8847 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
8848 | value.value); | |
8849 | } | |
3e4093b6 | 8850 | } |
e5e809f4 | 8851 | |
3e4093b6 RS |
8852 | while (1) |
8853 | { | |
8854 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
e5e809f4 | 8855 | { |
3e4093b6 RS |
8856 | tree fieldtype; |
8857 | enum tree_code fieldcode; | |
e5e809f4 | 8858 | |
3e4093b6 RS |
8859 | if (constructor_fields == 0) |
8860 | { | |
ea58ef42 | 8861 | pedwarn_init (loc, 0, "excess elements in struct initializer"); |
3e4093b6 RS |
8862 | break; |
8863 | } | |
e5e809f4 | 8864 | |
3e4093b6 RS |
8865 | fieldtype = TREE_TYPE (constructor_fields); |
8866 | if (fieldtype != error_mark_node) | |
8867 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8868 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8869 | |
3e4093b6 RS |
8870 | /* Error for non-static initialization of a flexible array member. */ |
8871 | if (fieldcode == ARRAY_TYPE | |
8872 | && !require_constant_value | |
8873 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
f7587ed0 | 8874 | && DECL_CHAIN (constructor_fields) == NULL_TREE) |
3e4093b6 | 8875 | { |
ea58ef42 MP |
8876 | error_init (loc, "non-static initialization of a flexible " |
8877 | "array member"); | |
3e4093b6 RS |
8878 | break; |
8879 | } | |
e5e809f4 | 8880 | |
2cc901dc MP |
8881 | /* Error for initialization of a flexible array member with |
8882 | a string constant if the structure is in an array. E.g.: | |
8883 | struct S { int x; char y[]; }; | |
8884 | struct S s[] = { { 1, "foo" } }; | |
8885 | is invalid. */ | |
8886 | if (string_flag | |
8887 | && fieldcode == ARRAY_TYPE | |
8888 | && constructor_depth > 1 | |
8889 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
8890 | && DECL_CHAIN (constructor_fields) == NULL_TREE) | |
8891 | { | |
8892 | bool in_array_p = false; | |
8893 | for (struct constructor_stack *p = constructor_stack; | |
8894 | p && p->type; p = p->next) | |
8895 | if (TREE_CODE (p->type) == ARRAY_TYPE) | |
8896 | { | |
8897 | in_array_p = true; | |
8898 | break; | |
8899 | } | |
8900 | if (in_array_p) | |
8901 | { | |
8902 | error_init (loc, "initialization of flexible array " | |
8903 | "member in a nested context"); | |
8904 | break; | |
8905 | } | |
8906 | } | |
8907 | ||
3e4093b6 | 8908 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8909 | if (value.value != 0 |
3e4093b6 | 8910 | && fieldcode == ARRAY_TYPE |
197463ae | 8911 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8912 | && string_flag) |
916c5919 | 8913 | value.value = orig_value; |
3e4093b6 RS |
8914 | /* Otherwise, if we have come to a subaggregate, |
8915 | and we don't have an element of its type, push into it. */ | |
0953878d | 8916 | else if (value.value != 0 |
916c5919 JM |
8917 | && value.value != error_mark_node |
8918 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 8919 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 8920 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 8921 | { |
ea58ef42 | 8922 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
8923 | continue; |
8924 | } | |
e5e809f4 | 8925 | |
916c5919 | 8926 | if (value.value) |
3e4093b6 RS |
8927 | { |
8928 | push_member_name (constructor_fields); | |
34cf811f | 8929 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 8930 | strict_string, fieldtype, |
a1e3b3d9 LB |
8931 | constructor_fields, 1, implicit, |
8932 | braced_init_obstack); | |
3e4093b6 | 8933 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
8934 | } |
8935 | else | |
3e4093b6 RS |
8936 | /* Do the bookkeeping for an element that was |
8937 | directly output as a constructor. */ | |
e5e809f4 | 8938 | { |
3e4093b6 RS |
8939 | /* For a record, keep track of end position of last field. */ |
8940 | if (DECL_SIZE (constructor_fields)) | |
c22cacf3 | 8941 | constructor_bit_index |
db3927fb AH |
8942 | = size_binop_loc (input_location, PLUS_EXPR, |
8943 | bit_position (constructor_fields), | |
8944 | DECL_SIZE (constructor_fields)); | |
3e4093b6 RS |
8945 | |
8946 | /* If the current field was the first one not yet written out, | |
8947 | it isn't now, so update. */ | |
8948 | if (constructor_unfilled_fields == constructor_fields) | |
8949 | { | |
910ad8de | 8950 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8951 | /* Skip any nameless bit fields. */ |
8952 | while (constructor_unfilled_fields != 0 | |
8953 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8954 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8955 | constructor_unfilled_fields = | |
910ad8de | 8956 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 | 8957 | } |
e5e809f4 | 8958 | } |
3e4093b6 | 8959 | |
910ad8de | 8960 | constructor_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8961 | /* Skip any nameless bit fields at the beginning. */ |
8962 | while (constructor_fields != 0 | |
8963 | && DECL_C_BIT_FIELD (constructor_fields) | |
8964 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 8965 | constructor_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 8966 | } |
3e4093b6 | 8967 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
e5e809f4 | 8968 | { |
3e4093b6 RS |
8969 | tree fieldtype; |
8970 | enum tree_code fieldcode; | |
e5e809f4 | 8971 | |
3e4093b6 RS |
8972 | if (constructor_fields == 0) |
8973 | { | |
d033409e | 8974 | pedwarn_init (loc, 0, |
509c9d60 | 8975 | "excess elements in union initializer"); |
3e4093b6 RS |
8976 | break; |
8977 | } | |
e5e809f4 | 8978 | |
3e4093b6 RS |
8979 | fieldtype = TREE_TYPE (constructor_fields); |
8980 | if (fieldtype != error_mark_node) | |
8981 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8982 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8983 | |
3e4093b6 RS |
8984 | /* Warn that traditional C rejects initialization of unions. |
8985 | We skip the warning if the value is zero. This is done | |
8986 | under the assumption that the zero initializer in user | |
8987 | code appears conditioned on e.g. __STDC__ to avoid | |
8988 | "missing initializer" warnings and relies on default | |
8989 | initialization to zero in the traditional C case. | |
8990 | We also skip the warning if the initializer is designated, | |
8991 | again on the assumption that this must be conditional on | |
8992 | __STDC__ anyway (and we've already complained about the | |
8993 | member-designator already). */ | |
8400e75e | 8994 | if (!in_system_header_at (input_location) && !constructor_designated |
916c5919 JM |
8995 | && !(value.value && (integer_zerop (value.value) |
8996 | || real_zerop (value.value)))) | |
3176a0c2 DD |
8997 | warning (OPT_Wtraditional, "traditional C rejects initialization " |
8998 | "of unions"); | |
e5e809f4 | 8999 | |
3e4093b6 | 9000 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 9001 | if (value.value != 0 |
3e4093b6 | 9002 | && fieldcode == ARRAY_TYPE |
197463ae | 9003 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 9004 | && string_flag) |
916c5919 | 9005 | value.value = orig_value; |
3e4093b6 RS |
9006 | /* Otherwise, if we have come to a subaggregate, |
9007 | and we don't have an element of its type, push into it. */ | |
0953878d | 9008 | else if (value.value != 0 |
916c5919 JM |
9009 | && value.value != error_mark_node |
9010 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 9011 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 9012 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 9013 | { |
ea58ef42 | 9014 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
9015 | continue; |
9016 | } | |
e5e809f4 | 9017 | |
916c5919 | 9018 | if (value.value) |
3e4093b6 RS |
9019 | { |
9020 | push_member_name (constructor_fields); | |
34cf811f | 9021 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 9022 | strict_string, fieldtype, |
a1e3b3d9 LB |
9023 | constructor_fields, 1, implicit, |
9024 | braced_init_obstack); | |
3e4093b6 | 9025 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
9026 | } |
9027 | else | |
3e4093b6 RS |
9028 | /* Do the bookkeeping for an element that was |
9029 | directly output as a constructor. */ | |
e5e809f4 | 9030 | { |
3e4093b6 | 9031 | constructor_bit_index = DECL_SIZE (constructor_fields); |
f7587ed0 | 9032 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 9033 | } |
e5e809f4 | 9034 | |
3e4093b6 RS |
9035 | constructor_fields = 0; |
9036 | } | |
9037 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
9038 | { | |
9039 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
9040 | enum tree_code eltcode = TREE_CODE (elttype); | |
e5e809f4 | 9041 | |
3e4093b6 | 9042 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 9043 | if (value.value != 0 |
3e4093b6 | 9044 | && eltcode == ARRAY_TYPE |
197463ae | 9045 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
3e4093b6 | 9046 | && string_flag) |
916c5919 | 9047 | value.value = orig_value; |
3e4093b6 RS |
9048 | /* Otherwise, if we have come to a subaggregate, |
9049 | and we don't have an element of its type, push into it. */ | |
0953878d | 9050 | else if (value.value != 0 |
916c5919 JM |
9051 | && value.value != error_mark_node |
9052 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
3e4093b6 | 9053 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
53650abe | 9054 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) |
3e4093b6 | 9055 | { |
ea58ef42 | 9056 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
9057 | continue; |
9058 | } | |
8b6a5902 | 9059 | |
3e4093b6 RS |
9060 | if (constructor_max_index != 0 |
9061 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
9062 | || integer_all_onesp (constructor_max_index))) | |
9063 | { | |
d033409e | 9064 | pedwarn_init (loc, 0, |
509c9d60 | 9065 | "excess elements in array initializer"); |
3e4093b6 RS |
9066 | break; |
9067 | } | |
8b6a5902 | 9068 | |
3e4093b6 | 9069 | /* Now output the actual element. */ |
916c5919 | 9070 | if (value.value) |
3e4093b6 | 9071 | { |
ae7e9ddd | 9072 | push_array_bounds (tree_to_uhwi (constructor_index)); |
34cf811f | 9073 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 9074 | strict_string, elttype, |
a1e3b3d9 LB |
9075 | constructor_index, 1, implicit, |
9076 | braced_init_obstack); | |
3e4093b6 RS |
9077 | RESTORE_SPELLING_DEPTH (constructor_depth); |
9078 | } | |
2f6e4e97 | 9079 | |
3e4093b6 | 9080 | constructor_index |
db3927fb AH |
9081 | = size_binop_loc (input_location, PLUS_EXPR, |
9082 | constructor_index, bitsize_one_node); | |
8b6a5902 | 9083 | |
916c5919 | 9084 | if (!value.value) |
3e4093b6 RS |
9085 | /* If we are doing the bookkeeping for an element that was |
9086 | directly output as a constructor, we must update | |
9087 | constructor_unfilled_index. */ | |
9088 | constructor_unfilled_index = constructor_index; | |
9089 | } | |
31521951 | 9090 | else if (VECTOR_TYPE_P (constructor_type)) |
3e4093b6 RS |
9091 | { |
9092 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8b6a5902 | 9093 | |
c22cacf3 MS |
9094 | /* Do a basic check of initializer size. Note that vectors |
9095 | always have a fixed size derived from their type. */ | |
3e4093b6 RS |
9096 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) |
9097 | { | |
d033409e | 9098 | pedwarn_init (loc, 0, |
509c9d60 | 9099 | "excess elements in vector initializer"); |
3e4093b6 RS |
9100 | break; |
9101 | } | |
8b6a5902 | 9102 | |
3e4093b6 | 9103 | /* Now output the actual element. */ |
916c5919 | 9104 | if (value.value) |
53650abe AP |
9105 | { |
9106 | if (TREE_CODE (value.value) == VECTOR_CST) | |
9107 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
34cf811f | 9108 | output_init_element (loc, value.value, value.original_type, |
53650abe | 9109 | strict_string, elttype, |
a1e3b3d9 LB |
9110 | constructor_index, 1, implicit, |
9111 | braced_init_obstack); | |
53650abe | 9112 | } |
8b6a5902 | 9113 | |
3e4093b6 | 9114 | constructor_index |
db3927fb AH |
9115 | = size_binop_loc (input_location, |
9116 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 9117 | |
916c5919 | 9118 | if (!value.value) |
3e4093b6 RS |
9119 | /* If we are doing the bookkeeping for an element that was |
9120 | directly output as a constructor, we must update | |
9121 | constructor_unfilled_index. */ | |
9122 | constructor_unfilled_index = constructor_index; | |
9123 | } | |
8b6a5902 | 9124 | |
3e4093b6 RS |
9125 | /* Handle the sole element allowed in a braced initializer |
9126 | for a scalar variable. */ | |
b4519d39 SB |
9127 | else if (constructor_type != error_mark_node |
9128 | && constructor_fields == 0) | |
8b6a5902 | 9129 | { |
d033409e | 9130 | pedwarn_init (loc, 0, |
509c9d60 | 9131 | "excess elements in scalar initializer"); |
3e4093b6 | 9132 | break; |
8b6a5902 JJ |
9133 | } |
9134 | else | |
9135 | { | |
916c5919 | 9136 | if (value.value) |
34cf811f | 9137 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 9138 | strict_string, constructor_type, |
a1e3b3d9 LB |
9139 | NULL_TREE, 1, implicit, |
9140 | braced_init_obstack); | |
3e4093b6 | 9141 | constructor_fields = 0; |
8b6a5902 JJ |
9142 | } |
9143 | ||
3e4093b6 RS |
9144 | /* Handle range initializers either at this level or anywhere higher |
9145 | in the designator stack. */ | |
9146 | if (constructor_range_stack) | |
8b6a5902 | 9147 | { |
3e4093b6 RS |
9148 | struct constructor_range_stack *p, *range_stack; |
9149 | int finish = 0; | |
9150 | ||
9151 | range_stack = constructor_range_stack; | |
9152 | constructor_range_stack = 0; | |
9153 | while (constructor_stack != range_stack->stack) | |
8b6a5902 | 9154 | { |
366de0ce | 9155 | gcc_assert (constructor_stack->implicit); |
34cf811f | 9156 | process_init_element (loc, |
ea58ef42 MP |
9157 | pop_init_level (loc, 1, |
9158 | braced_init_obstack), | |
a1e3b3d9 | 9159 | true, braced_init_obstack); |
8b6a5902 | 9160 | } |
3e4093b6 RS |
9161 | for (p = range_stack; |
9162 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
9163 | p = p->prev) | |
8b6a5902 | 9164 | { |
366de0ce | 9165 | gcc_assert (constructor_stack->implicit); |
34cf811f | 9166 | process_init_element (loc, |
ea58ef42 MP |
9167 | pop_init_level (loc, 1, |
9168 | braced_init_obstack), | |
a1e3b3d9 | 9169 | true, braced_init_obstack); |
8b6a5902 | 9170 | } |
3e4093b6 | 9171 | |
db3927fb AH |
9172 | p->index = size_binop_loc (input_location, |
9173 | PLUS_EXPR, p->index, bitsize_one_node); | |
3e4093b6 RS |
9174 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) |
9175 | finish = 1; | |
9176 | ||
9177 | while (1) | |
9178 | { | |
9179 | constructor_index = p->index; | |
9180 | constructor_fields = p->fields; | |
9181 | if (finish && p->range_end && p->index == p->range_start) | |
9182 | { | |
9183 | finish = 0; | |
9184 | p->prev = 0; | |
9185 | } | |
9186 | p = p->next; | |
9187 | if (!p) | |
9188 | break; | |
ea58ef42 | 9189 | push_init_level (loc, 2, braced_init_obstack); |
3e4093b6 RS |
9190 | p->stack = constructor_stack; |
9191 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
9192 | p->index = p->range_start; | |
9193 | } | |
9194 | ||
9195 | if (!finish) | |
9196 | constructor_range_stack = range_stack; | |
9197 | continue; | |
8b6a5902 JJ |
9198 | } |
9199 | ||
3e4093b6 | 9200 | break; |
8b6a5902 JJ |
9201 | } |
9202 | ||
3e4093b6 RS |
9203 | constructor_range_stack = 0; |
9204 | } | |
9205 | \f | |
9f0e2d86 ZW |
9206 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
9207 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
e130a54b | 9208 | an ASM_EXPR node). */ |
3e4093b6 | 9209 | tree |
9f0e2d86 | 9210 | build_asm_stmt (tree cv_qualifier, tree args) |
3e4093b6 | 9211 | { |
6de9cd9a DN |
9212 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
9213 | ASM_VOLATILE_P (args) = 1; | |
9f0e2d86 | 9214 | return add_stmt (args); |
8b6a5902 JJ |
9215 | } |
9216 | ||
9f0e2d86 ZW |
9217 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
9218 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
9219 | SIMPLE indicates whether there was anything at all after the | |
9220 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
e130a54b | 9221 | are subtly different. We use a ASM_EXPR node to represent this. */ |
3e4093b6 | 9222 | tree |
c2255bc4 | 9223 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, |
1c384bf1 | 9224 | tree clobbers, tree labels, bool simple) |
e5e809f4 | 9225 | { |
3e4093b6 | 9226 | tree tail; |
9f0e2d86 | 9227 | tree args; |
6de9cd9a DN |
9228 | int i; |
9229 | const char *constraint; | |
74f0c611 | 9230 | const char **oconstraints; |
6de9cd9a | 9231 | bool allows_mem, allows_reg, is_inout; |
74f0c611 | 9232 | int ninputs, noutputs; |
6de9cd9a DN |
9233 | |
9234 | ninputs = list_length (inputs); | |
9235 | noutputs = list_length (outputs); | |
74f0c611 RH |
9236 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); |
9237 | ||
1c384bf1 | 9238 | string = resolve_asm_operand_names (string, outputs, inputs, labels); |
3e4093b6 | 9239 | |
6de9cd9a DN |
9240 | /* Remove output conversions that change the type but not the mode. */ |
9241 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
e5e809f4 | 9242 | { |
3e4093b6 | 9243 | tree output = TREE_VALUE (tail); |
74f0c611 | 9244 | |
eadd3d0d JJ |
9245 | output = c_fully_fold (output, false, NULL); |
9246 | ||
74f0c611 RH |
9247 | /* ??? Really, this should not be here. Users should be using a |
9248 | proper lvalue, dammit. But there's a long history of using casts | |
9249 | in the output operands. In cases like longlong.h, this becomes a | |
9250 | primitive form of typechecking -- if the cast can be removed, then | |
9251 | the output operand had a type of the proper width; otherwise we'll | |
9252 | get an error. Gross, but ... */ | |
3e4093b6 | 9253 | STRIP_NOPS (output); |
74f0c611 | 9254 | |
7bd11157 | 9255 | if (!lvalue_or_else (loc, output, lv_asm)) |
74f0c611 | 9256 | output = error_mark_node; |
8b6a5902 | 9257 | |
5544530a PB |
9258 | if (output != error_mark_node |
9259 | && (TREE_READONLY (output) | |
9260 | || TYPE_READONLY (TREE_TYPE (output)) | |
9261 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
9262 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
9263 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
c02065fc | 9264 | readonly_error (loc, output, lv_asm); |
5544530a | 9265 | |
6de9cd9a | 9266 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
74f0c611 RH |
9267 | oconstraints[i] = constraint; |
9268 | ||
9269 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
9270 | &allows_mem, &allows_reg, &is_inout)) | |
9271 | { | |
9272 | /* If the operand is going to end up in memory, | |
9273 | mark it addressable. */ | |
9274 | if (!allows_reg && !c_mark_addressable (output)) | |
9275 | output = error_mark_node; | |
bae5cddf JJ |
9276 | if (!(!allows_reg && allows_mem) |
9277 | && output != error_mark_node | |
9278 | && VOID_TYPE_P (TREE_TYPE (output))) | |
9279 | { | |
9280 | error_at (loc, "invalid use of void expression"); | |
9281 | output = error_mark_node; | |
9282 | } | |
74f0c611 RH |
9283 | } |
9284 | else | |
c22cacf3 | 9285 | output = error_mark_node; |
3e4093b6 | 9286 | |
74f0c611 | 9287 | TREE_VALUE (tail) = output; |
8b6a5902 | 9288 | } |
3e4093b6 | 9289 | |
74f0c611 RH |
9290 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) |
9291 | { | |
9292 | tree input; | |
9293 | ||
9294 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
9295 | input = TREE_VALUE (tail); | |
9296 | ||
74f0c611 RH |
9297 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
9298 | oconstraints, &allows_mem, &allows_reg)) | |
9299 | { | |
9300 | /* If the operand is going to end up in memory, | |
9301 | mark it addressable. */ | |
b4c33883 AP |
9302 | if (!allows_reg && allows_mem) |
9303 | { | |
eadd3d0d JJ |
9304 | input = c_fully_fold (input, false, NULL); |
9305 | ||
b4c33883 AP |
9306 | /* Strip the nops as we allow this case. FIXME, this really |
9307 | should be rejected or made deprecated. */ | |
9308 | STRIP_NOPS (input); | |
9309 | if (!c_mark_addressable (input)) | |
9310 | input = error_mark_node; | |
bae5cddf | 9311 | } |
eadd3d0d | 9312 | else |
bae5cddf | 9313 | { |
eadd3d0d JJ |
9314 | struct c_expr expr; |
9315 | memset (&expr, 0, sizeof (expr)); | |
9316 | expr.value = input; | |
267bac10 | 9317 | expr = convert_lvalue_to_rvalue (loc, expr, true, false); |
eadd3d0d JJ |
9318 | input = c_fully_fold (expr.value, false, NULL); |
9319 | ||
9320 | if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) | |
9321 | { | |
9322 | error_at (loc, "invalid use of void expression"); | |
9323 | input = error_mark_node; | |
9324 | } | |
bae5cddf | 9325 | } |
74f0c611 RH |
9326 | } |
9327 | else | |
9328 | input = error_mark_node; | |
9329 | ||
9330 | TREE_VALUE (tail) = input; | |
9331 | } | |
3e4093b6 | 9332 | |
1c384bf1 RH |
9333 | /* ASMs with labels cannot have outputs. This should have been |
9334 | enforced by the parser. */ | |
9335 | gcc_assert (outputs == NULL || labels == NULL); | |
9336 | ||
9337 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
9f0e2d86 | 9338 | |
5544530a PB |
9339 | /* asm statements without outputs, including simple ones, are treated |
9340 | as volatile. */ | |
9341 | ASM_INPUT_P (args) = simple; | |
9342 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
74f0c611 | 9343 | |
9f0e2d86 | 9344 | return args; |
e5e809f4 | 9345 | } |
3e4093b6 | 9346 | \f |
c2255bc4 AH |
9347 | /* Generate a goto statement to LABEL. LOC is the location of the |
9348 | GOTO. */ | |
506e2710 RH |
9349 | |
9350 | tree | |
c2255bc4 | 9351 | c_finish_goto_label (location_t loc, tree label) |
506e2710 | 9352 | { |
e1b7793c | 9353 | tree decl = lookup_label_for_goto (loc, label); |
506e2710 RH |
9354 | if (!decl) |
9355 | return NULL_TREE; | |
506e2710 | 9356 | TREE_USED (decl) = 1; |
c2255bc4 AH |
9357 | { |
9358 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
9359 | SET_EXPR_LOCATION (t, loc); | |
9360 | return add_stmt (t); | |
9361 | } | |
506e2710 RH |
9362 | } |
9363 | ||
c2255bc4 AH |
9364 | /* Generate a computed goto statement to EXPR. LOC is the location of |
9365 | the GOTO. */ | |
506e2710 RH |
9366 | |
9367 | tree | |
c2255bc4 | 9368 | c_finish_goto_ptr (location_t loc, tree expr) |
506e2710 | 9369 | { |
c2255bc4 | 9370 | tree t; |
c1771a20 | 9371 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); |
928c19bb | 9372 | expr = c_fully_fold (expr, false, NULL); |
506e2710 | 9373 | expr = convert (ptr_type_node, expr); |
c2255bc4 AH |
9374 | t = build1 (GOTO_EXPR, void_type_node, expr); |
9375 | SET_EXPR_LOCATION (t, loc); | |
9376 | return add_stmt (t); | |
506e2710 RH |
9377 | } |
9378 | ||
5088b058 | 9379 | /* Generate a C `return' statement. RETVAL is the expression for what |
c2255bc4 | 9380 | to return, or a null pointer for `return;' with no value. LOC is |
6e07c515 MP |
9381 | the location of the return statement, or the location of the expression, |
9382 | if the statement has any. If ORIGTYPE is not NULL_TREE, it | |
c2255bc4 | 9383 | is the original type of RETVAL. */ |
de520661 | 9384 | |
506e2710 | 9385 | tree |
c2255bc4 | 9386 | c_finish_return (location_t loc, tree retval, tree origtype) |
3e4093b6 | 9387 | { |
0c9b182b JJ |
9388 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; |
9389 | bool no_warning = false; | |
928c19bb | 9390 | bool npc = false; |
36536d79 | 9391 | size_t rank = 0; |
3e4093b6 | 9392 | |
de8ddd5f MP |
9393 | /* Use the expansion point to handle cases such as returning NULL |
9394 | in a function returning void. */ | |
9395 | source_location xloc = expansion_point_location_if_in_system_header (loc); | |
9396 | ||
3e4093b6 | 9397 | if (TREE_THIS_VOLATILE (current_function_decl)) |
de8ddd5f | 9398 | warning_at (xloc, 0, |
c2255bc4 | 9399 | "function declared %<noreturn%> has a %<return%> statement"); |
3e4093b6 | 9400 | |
b72271b9 | 9401 | if (flag_cilkplus && contains_array_notation_expr (retval)) |
36536d79 BI |
9402 | { |
9403 | /* Array notations are allowed in a return statement if it is inside a | |
9404 | built-in array notation reduction function. */ | |
9405 | if (!find_rank (loc, retval, retval, false, &rank)) | |
9406 | return error_mark_node; | |
9407 | if (rank >= 1) | |
9408 | { | |
9409 | error_at (loc, "array notation expression cannot be used as a " | |
9410 | "return value"); | |
9411 | return error_mark_node; | |
9412 | } | |
9413 | } | |
3af9c5e9 | 9414 | if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval)) |
939b37da BI |
9415 | { |
9416 | error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not " | |
9417 | "allowed"); | |
9418 | return error_mark_node; | |
9419 | } | |
928c19bb JM |
9420 | if (retval) |
9421 | { | |
8ce94e44 | 9422 | tree semantic_type = NULL_TREE; |
928c19bb | 9423 | npc = null_pointer_constant_p (retval); |
8ce94e44 JM |
9424 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) |
9425 | { | |
9426 | semantic_type = TREE_TYPE (retval); | |
9427 | retval = TREE_OPERAND (retval, 0); | |
9428 | } | |
928c19bb | 9429 | retval = c_fully_fold (retval, false, NULL); |
8ce94e44 JM |
9430 | if (semantic_type) |
9431 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
928c19bb JM |
9432 | } |
9433 | ||
3e4093b6 | 9434 | if (!retval) |
de520661 | 9435 | { |
3e4093b6 RS |
9436 | current_function_returns_null = 1; |
9437 | if ((warn_return_type || flag_isoc99) | |
9438 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
0c9b182b | 9439 | { |
35aff4fb MP |
9440 | if (flag_isoc99) |
9441 | pedwarn (loc, 0, "%<return%> with no value, in " | |
9442 | "function returning non-void"); | |
9443 | else | |
9444 | warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, " | |
9445 | "in function returning non-void"); | |
0c9b182b JJ |
9446 | no_warning = true; |
9447 | } | |
400fbf9f | 9448 | } |
3e4093b6 | 9449 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
de520661 | 9450 | { |
3e4093b6 | 9451 | current_function_returns_null = 1; |
2397c575 | 9452 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) |
de8ddd5f | 9453 | pedwarn (xloc, 0, |
509c9d60 | 9454 | "%<return%> with a value, in function returning void"); |
b8698a0f | 9455 | else |
de8ddd5f | 9456 | pedwarn (xloc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 9457 | "%<return%> with expression, in function returning void"); |
de520661 | 9458 | } |
3e4093b6 | 9459 | else |
de520661 | 9460 | { |
68fca595 MP |
9461 | tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype, |
9462 | retval, origtype, ic_return, | |
c2255bc4 | 9463 | npc, NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
9464 | tree res = DECL_RESULT (current_function_decl); |
9465 | tree inner; | |
9feb29df | 9466 | bool save; |
3e4093b6 RS |
9467 | |
9468 | current_function_returns_value = 1; | |
9469 | if (t == error_mark_node) | |
506e2710 | 9470 | return NULL_TREE; |
3e4093b6 | 9471 | |
9feb29df JJ |
9472 | save = in_late_binary_op; |
9473 | if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE | |
e5341100 JJ |
9474 | || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE |
9475 | || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE | |
9476 | && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE | |
9477 | || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE) | |
9478 | && (flag_sanitize & SANITIZE_FLOAT_CAST))) | |
9feb29df | 9479 | in_late_binary_op = true; |
3e4093b6 | 9480 | inner = t = convert (TREE_TYPE (res), t); |
9feb29df | 9481 | in_late_binary_op = save; |
3e4093b6 RS |
9482 | |
9483 | /* Strip any conversions, additions, and subtractions, and see if | |
9484 | we are returning the address of a local variable. Warn if so. */ | |
9485 | while (1) | |
8b6a5902 | 9486 | { |
3e4093b6 | 9487 | switch (TREE_CODE (inner)) |
8b6a5902 | 9488 | { |
849421a3 JJ |
9489 | CASE_CONVERT: |
9490 | case NON_LVALUE_EXPR: | |
3e4093b6 | 9491 | case PLUS_EXPR: |
849421a3 | 9492 | case POINTER_PLUS_EXPR: |
3e4093b6 RS |
9493 | inner = TREE_OPERAND (inner, 0); |
9494 | continue; | |
9495 | ||
9496 | case MINUS_EXPR: | |
9497 | /* If the second operand of the MINUS_EXPR has a pointer | |
9498 | type (or is converted from it), this may be valid, so | |
9499 | don't give a warning. */ | |
9500 | { | |
9501 | tree op1 = TREE_OPERAND (inner, 1); | |
8b6a5902 | 9502 | |
3f75a254 | 9503 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) |
1043771b TB |
9504 | && (CONVERT_EXPR_P (op1) |
9505 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
3e4093b6 | 9506 | op1 = TREE_OPERAND (op1, 0); |
8b6a5902 | 9507 | |
3e4093b6 RS |
9508 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
9509 | break; | |
8b6a5902 | 9510 | |
3e4093b6 RS |
9511 | inner = TREE_OPERAND (inner, 0); |
9512 | continue; | |
9513 | } | |
400fbf9f | 9514 | |
3e4093b6 RS |
9515 | case ADDR_EXPR: |
9516 | inner = TREE_OPERAND (inner, 0); | |
c2f4acb7 | 9517 | |
6615c446 | 9518 | while (REFERENCE_CLASS_P (inner) |
22d03525 | 9519 | && !INDIRECT_REF_P (inner)) |
3e4093b6 | 9520 | inner = TREE_OPERAND (inner, 0); |
8b6a5902 | 9521 | |
a2f1f4c3 | 9522 | if (DECL_P (inner) |
3f75a254 JM |
9523 | && !DECL_EXTERNAL (inner) |
9524 | && !TREE_STATIC (inner) | |
3e4093b6 | 9525 | && DECL_CONTEXT (inner) == current_function_decl) |
19fc9faa MP |
9526 | { |
9527 | if (TREE_CODE (inner) == LABEL_DECL) | |
9528 | warning_at (loc, OPT_Wreturn_local_addr, | |
9529 | "function returns address of label"); | |
9530 | else | |
b4dfdc11 MG |
9531 | { |
9532 | warning_at (loc, OPT_Wreturn_local_addr, | |
9533 | "function returns address of local variable"); | |
9534 | tree zero = build_zero_cst (TREE_TYPE (res)); | |
9535 | t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero); | |
9536 | } | |
19fc9faa | 9537 | } |
3e4093b6 | 9538 | break; |
8b6a5902 | 9539 | |
3e4093b6 RS |
9540 | default: |
9541 | break; | |
9542 | } | |
de520661 | 9543 | |
3e4093b6 RS |
9544 | break; |
9545 | } | |
9546 | ||
53fb4de3 | 9547 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
c2255bc4 | 9548 | SET_EXPR_LOCATION (retval, loc); |
ca085fd7 MLI |
9549 | |
9550 | if (warn_sequence_point) | |
9551 | verify_sequence_points (retval); | |
de520661 | 9552 | } |
8b6a5902 | 9553 | |
c2255bc4 | 9554 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); |
0c9b182b JJ |
9555 | TREE_NO_WARNING (ret_stmt) |= no_warning; |
9556 | return add_stmt (ret_stmt); | |
de520661 | 9557 | } |
3e4093b6 RS |
9558 | \f |
9559 | struct c_switch { | |
604f5adf ILT |
9560 | /* The SWITCH_EXPR being built. */ |
9561 | tree switch_expr; | |
a6c0a76c | 9562 | |
89dbed81 | 9563 | /* The original type of the testing expression, i.e. before the |
a6c0a76c SB |
9564 | default conversion is applied. */ |
9565 | tree orig_type; | |
9566 | ||
3e4093b6 RS |
9567 | /* A splay-tree mapping the low element of a case range to the high |
9568 | element, or NULL_TREE if there is no high element. Used to | |
9569 | determine whether or not a new case label duplicates an old case | |
9570 | label. We need a tree, rather than simply a hash table, because | |
9571 | of the GNU case range extension. */ | |
9572 | splay_tree cases; | |
a6c0a76c | 9573 | |
e1b7793c ILT |
9574 | /* The bindings at the point of the switch. This is used for |
9575 | warnings crossing decls when branching to a case label. */ | |
9576 | struct c_spot_bindings *bindings; | |
187230a7 | 9577 | |
3e4093b6 RS |
9578 | /* The next node on the stack. */ |
9579 | struct c_switch *next; | |
b155cfd9 MP |
9580 | |
9581 | /* Remember whether the controlling expression had boolean type | |
9582 | before integer promotions for the sake of -Wswitch-bool. */ | |
9583 | bool bool_cond_p; | |
9584 | ||
9585 | /* Remember whether there was a case value that is outside the | |
9586 | range of the ORIG_TYPE. */ | |
9587 | bool outside_range_p; | |
3e4093b6 | 9588 | }; |
400fbf9f | 9589 | |
3e4093b6 RS |
9590 | /* A stack of the currently active switch statements. The innermost |
9591 | switch statement is on the top of the stack. There is no need to | |
9592 | mark the stack for garbage collection because it is only active | |
9593 | during the processing of the body of a function, and we never | |
9594 | collect at that point. */ | |
de520661 | 9595 | |
506e2710 | 9596 | struct c_switch *c_switch_stack; |
de520661 | 9597 | |
3e4093b6 | 9598 | /* Start a C switch statement, testing expression EXP. Return the new |
c2255bc4 | 9599 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. |
fedfecef | 9600 | SWITCH_COND_LOC is the location of the switch's condition. |
b155cfd9 | 9601 | EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */ |
de520661 | 9602 | |
3e4093b6 | 9603 | tree |
c2255bc4 AH |
9604 | c_start_case (location_t switch_loc, |
9605 | location_t switch_cond_loc, | |
fedfecef | 9606 | tree exp, bool explicit_cast_p) |
de520661 | 9607 | { |
c58e8676 | 9608 | tree orig_type = error_mark_node; |
b155cfd9 | 9609 | bool bool_cond_p = false; |
3e4093b6 | 9610 | struct c_switch *cs; |
2f6e4e97 | 9611 | |
3e4093b6 | 9612 | if (exp != error_mark_node) |
de520661 | 9613 | { |
3e4093b6 RS |
9614 | orig_type = TREE_TYPE (exp); |
9615 | ||
c58e8676 | 9616 | if (!INTEGRAL_TYPE_P (orig_type)) |
de520661 | 9617 | { |
c58e8676 VR |
9618 | if (orig_type != error_mark_node) |
9619 | { | |
c2255bc4 | 9620 | error_at (switch_cond_loc, "switch quantity not an integer"); |
c58e8676 VR |
9621 | orig_type = error_mark_node; |
9622 | } | |
3e4093b6 | 9623 | exp = integer_zero_node; |
de520661 | 9624 | } |
3e4093b6 | 9625 | else |
de520661 | 9626 | { |
c58e8676 | 9627 | tree type = TYPE_MAIN_VARIANT (orig_type); |
fedfecef MP |
9628 | tree e = exp; |
9629 | ||
9630 | /* Warn if the condition has boolean value. */ | |
9631 | while (TREE_CODE (e) == COMPOUND_EXPR) | |
9632 | e = TREE_OPERAND (e, 1); | |
9633 | ||
9634 | if ((TREE_CODE (type) == BOOLEAN_TYPE | |
9635 | || truth_value_p (TREE_CODE (e))) | |
9636 | /* Explicit cast to int suppresses this warning. */ | |
9637 | && !(TREE_CODE (type) == INTEGER_TYPE | |
9638 | && explicit_cast_p)) | |
b155cfd9 | 9639 | bool_cond_p = true; |
8b6a5902 | 9640 | |
8400e75e | 9641 | if (!in_system_header_at (input_location) |
3e4093b6 RS |
9642 | && (type == long_integer_type_node |
9643 | || type == long_unsigned_type_node)) | |
c2255bc4 AH |
9644 | warning_at (switch_cond_loc, |
9645 | OPT_Wtraditional, "%<long%> switch expression not " | |
9646 | "converted to %<int%> in ISO C"); | |
8b6a5902 | 9647 | |
928c19bb | 9648 | exp = c_fully_fold (exp, false, NULL); |
3e4093b6 | 9649 | exp = default_conversion (exp); |
ca085fd7 MLI |
9650 | |
9651 | if (warn_sequence_point) | |
9652 | verify_sequence_points (exp); | |
3e4093b6 RS |
9653 | } |
9654 | } | |
9655 | ||
604f5adf | 9656 | /* Add this new SWITCH_EXPR to the stack. */ |
5d038c4c | 9657 | cs = XNEW (struct c_switch); |
604f5adf | 9658 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); |
c2255bc4 | 9659 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); |
a6c0a76c | 9660 | cs->orig_type = orig_type; |
3e4093b6 | 9661 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
e1b7793c | 9662 | cs->bindings = c_get_switch_bindings (); |
b155cfd9 MP |
9663 | cs->bool_cond_p = bool_cond_p; |
9664 | cs->outside_range_p = false; | |
506e2710 RH |
9665 | cs->next = c_switch_stack; |
9666 | c_switch_stack = cs; | |
3e4093b6 | 9667 | |
604f5adf | 9668 | return add_stmt (cs->switch_expr); |
3e4093b6 RS |
9669 | } |
9670 | ||
c2255bc4 | 9671 | /* Process a case label at location LOC. */ |
3e4093b6 RS |
9672 | |
9673 | tree | |
c2255bc4 | 9674 | do_case (location_t loc, tree low_value, tree high_value) |
3e4093b6 RS |
9675 | { |
9676 | tree label = NULL_TREE; | |
9677 | ||
17cede2e JM |
9678 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) |
9679 | { | |
9680 | low_value = c_fully_fold (low_value, false, NULL); | |
9681 | if (TREE_CODE (low_value) == INTEGER_CST) | |
d033409e | 9682 | pedwarn (loc, OPT_Wpedantic, |
17cede2e JM |
9683 | "case label is not an integer constant expression"); |
9684 | } | |
9685 | ||
9686 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
9687 | { | |
9688 | high_value = c_fully_fold (high_value, false, NULL); | |
9689 | if (TREE_CODE (high_value) == INTEGER_CST) | |
c1771a20 | 9690 | pedwarn (input_location, OPT_Wpedantic, |
17cede2e JM |
9691 | "case label is not an integer constant expression"); |
9692 | } | |
9693 | ||
e1b7793c | 9694 | if (c_switch_stack == NULL) |
187230a7 JM |
9695 | { |
9696 | if (low_value) | |
e1b7793c | 9697 | error_at (loc, "case label not within a switch statement"); |
187230a7 | 9698 | else |
e1b7793c ILT |
9699 | error_at (loc, "%<default%> label not within a switch statement"); |
9700 | return NULL_TREE; | |
187230a7 | 9701 | } |
de520661 | 9702 | |
e1b7793c ILT |
9703 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, |
9704 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
9705 | loc)) | |
9706 | return NULL_TREE; | |
9707 | ||
9708 | label = c_add_case_label (loc, c_switch_stack->cases, | |
9709 | SWITCH_COND (c_switch_stack->switch_expr), | |
9710 | c_switch_stack->orig_type, | |
b155cfd9 MP |
9711 | low_value, high_value, |
9712 | &c_switch_stack->outside_range_p); | |
e1b7793c ILT |
9713 | if (label == error_mark_node) |
9714 | label = NULL_TREE; | |
3e4093b6 RS |
9715 | return label; |
9716 | } | |
de520661 | 9717 | |
083e891e MP |
9718 | /* Finish the switch statement. TYPE is the original type of the |
9719 | controlling expression of the switch, or NULL_TREE. */ | |
de520661 | 9720 | |
3e4093b6 | 9721 | void |
083e891e | 9722 | c_finish_case (tree body, tree type) |
3e4093b6 | 9723 | { |
506e2710 | 9724 | struct c_switch *cs = c_switch_stack; |
fbc315db | 9725 | location_t switch_location; |
3e4093b6 | 9726 | |
604f5adf | 9727 | SWITCH_BODY (cs->switch_expr) = body; |
325c3691 | 9728 | |
6de9cd9a | 9729 | /* Emit warnings as needed. */ |
c2255bc4 | 9730 | switch_location = EXPR_LOCATION (cs->switch_expr); |
fbc315db | 9731 | c_do_switch_warnings (cs->cases, switch_location, |
083e891e | 9732 | type ? type : TREE_TYPE (cs->switch_expr), |
b155cfd9 MP |
9733 | SWITCH_COND (cs->switch_expr), |
9734 | cs->bool_cond_p, cs->outside_range_p); | |
6de9cd9a | 9735 | |
3e4093b6 | 9736 | /* Pop the stack. */ |
506e2710 | 9737 | c_switch_stack = cs->next; |
3e4093b6 | 9738 | splay_tree_delete (cs->cases); |
e1b7793c | 9739 | c_release_switch_bindings (cs->bindings); |
5d038c4c | 9740 | XDELETE (cs); |
de520661 | 9741 | } |
325c3691 | 9742 | \f |
506e2710 RH |
9743 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
9744 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
9745 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
9746 | statement, and was not surrounded with parenthesis. */ | |
325c3691 | 9747 | |
9e51cf9d | 9748 | void |
506e2710 RH |
9749 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
9750 | tree else_block, bool nested_if) | |
325c3691 | 9751 | { |
506e2710 | 9752 | tree stmt; |
325c3691 | 9753 | |
25c22937 BI |
9754 | /* If the condition has array notations, then the rank of the then_block and |
9755 | else_block must be either 0 or be equal to the rank of the condition. If | |
9756 | the condition does not have array notations then break them up as it is | |
9757 | broken up in a normal expression. */ | |
b72271b9 | 9758 | if (flag_cilkplus && contains_array_notation_expr (cond)) |
25c22937 BI |
9759 | { |
9760 | size_t then_rank = 0, cond_rank = 0, else_rank = 0; | |
9761 | if (!find_rank (if_locus, cond, cond, true, &cond_rank)) | |
9762 | return; | |
9763 | if (then_block | |
9764 | && !find_rank (if_locus, then_block, then_block, true, &then_rank)) | |
9765 | return; | |
9766 | if (else_block | |
9767 | && !find_rank (if_locus, else_block, else_block, true, &else_rank)) | |
9768 | return; | |
9769 | if (cond_rank != then_rank && then_rank != 0) | |
9770 | { | |
9771 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9772 | " and the then-block"); | |
9773 | return; | |
9774 | } | |
9775 | else if (cond_rank != else_rank && else_rank != 0) | |
9776 | { | |
9777 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9778 | " and the else-block"); | |
9779 | return; | |
9780 | } | |
9781 | } | |
506e2710 RH |
9782 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
9783 | if (warn_parentheses && nested_if && else_block == NULL) | |
325c3691 | 9784 | { |
506e2710 | 9785 | tree inner_if = then_block; |
16865eaa | 9786 | |
61ada8ae | 9787 | /* We know from the grammar productions that there is an IF nested |
506e2710 RH |
9788 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
9789 | it might not be exactly THEN_BLOCK, but should be the last | |
9790 | non-container statement within. */ | |
9791 | while (1) | |
9792 | switch (TREE_CODE (inner_if)) | |
9793 | { | |
9794 | case COND_EXPR: | |
9795 | goto found; | |
9796 | case BIND_EXPR: | |
9797 | inner_if = BIND_EXPR_BODY (inner_if); | |
9798 | break; | |
9799 | case STATEMENT_LIST: | |
9800 | inner_if = expr_last (then_block); | |
9801 | break; | |
9802 | case TRY_FINALLY_EXPR: | |
9803 | case TRY_CATCH_EXPR: | |
9804 | inner_if = TREE_OPERAND (inner_if, 0); | |
9805 | break; | |
9806 | default: | |
366de0ce | 9807 | gcc_unreachable (); |
506e2710 RH |
9808 | } |
9809 | found: | |
16865eaa | 9810 | |
506e2710 | 9811 | if (COND_EXPR_ELSE (inner_if)) |
fab922b1 MLI |
9812 | warning_at (if_locus, OPT_Wparentheses, |
9813 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
506e2710 | 9814 | } |
16865eaa | 9815 | |
2214de30 | 9816 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); |
a281759f | 9817 | SET_EXPR_LOCATION (stmt, if_locus); |
506e2710 | 9818 | add_stmt (stmt); |
325c3691 RH |
9819 | } |
9820 | ||
506e2710 RH |
9821 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
9822 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
9823 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
61ada8ae | 9824 | the statement controlled by the loop. BLAB is the break label. CLAB is |
506e2710 | 9825 | the continue label. Everything is allowed to be NULL. */ |
325c3691 RH |
9826 | |
9827 | void | |
506e2710 RH |
9828 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
9829 | tree blab, tree clab, bool cond_is_first) | |
325c3691 | 9830 | { |
506e2710 RH |
9831 | tree entry = NULL, exit = NULL, t; |
9832 | ||
e5e44252 AK |
9833 | /* In theory could forbid cilk spawn for loop increment expression, |
9834 | but it should work just fine. */ | |
36536d79 | 9835 | |
28af952a RS |
9836 | /* If the condition is zero don't generate a loop construct. */ |
9837 | if (cond && integer_zerop (cond)) | |
9838 | { | |
9839 | if (cond_is_first) | |
9840 | { | |
9841 | t = build_and_jump (&blab); | |
9842 | SET_EXPR_LOCATION (t, start_locus); | |
9843 | add_stmt (t); | |
9844 | } | |
9845 | } | |
9846 | else | |
506e2710 RH |
9847 | { |
9848 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
c22cacf3 | 9849 | |
506e2710 | 9850 | /* If we have an exit condition, then we build an IF with gotos either |
c22cacf3 MS |
9851 | out of the loop, or to the top of it. If there's no exit condition, |
9852 | then we just build a jump back to the top. */ | |
506e2710 | 9853 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); |
c22cacf3 | 9854 | |
28af952a | 9855 | if (cond && !integer_nonzerop (cond)) |
c22cacf3 MS |
9856 | { |
9857 | /* Canonicalize the loop condition to the end. This means | |
9858 | generating a branch to the loop condition. Reuse the | |
9859 | continue label, if possible. */ | |
9860 | if (cond_is_first) | |
9861 | { | |
9862 | if (incr || !clab) | |
9863 | { | |
9864 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
9865 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
9866 | } | |
9867 | else | |
9868 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
a281759f | 9869 | SET_EXPR_LOCATION (t, start_locus); |
c22cacf3 MS |
9870 | add_stmt (t); |
9871 | } | |
9872 | ||
506e2710 | 9873 | t = build_and_jump (&blab); |
506e2710 | 9874 | if (cond_is_first) |
db3927fb AH |
9875 | exit = fold_build3_loc (start_locus, |
9876 | COND_EXPR, void_type_node, cond, exit, t); | |
506e2710 | 9877 | else |
db3927fb AH |
9878 | exit = fold_build3_loc (input_location, |
9879 | COND_EXPR, void_type_node, cond, exit, t); | |
c22cacf3 MS |
9880 | } |
9881 | ||
506e2710 RH |
9882 | add_stmt (top); |
9883 | } | |
c22cacf3 | 9884 | |
506e2710 RH |
9885 | if (body) |
9886 | add_stmt (body); | |
9887 | if (clab) | |
9888 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
9889 | if (incr) | |
9890 | add_stmt (incr); | |
9891 | if (entry) | |
9892 | add_stmt (entry); | |
9893 | if (exit) | |
9894 | add_stmt (exit); | |
9895 | if (blab) | |
9896 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
325c3691 | 9897 | } |
325c3691 RH |
9898 | |
9899 | tree | |
c2255bc4 | 9900 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) |
325c3691 | 9901 | { |
089efaa4 | 9902 | bool skip; |
506e2710 | 9903 | tree label = *label_p; |
325c3691 | 9904 | |
089efaa4 ILT |
9905 | /* In switch statements break is sometimes stylistically used after |
9906 | a return statement. This can lead to spurious warnings about | |
9907 | control reaching the end of a non-void function when it is | |
9908 | inlined. Note that we are calling block_may_fallthru with | |
9909 | language specific tree nodes; this works because | |
9910 | block_may_fallthru returns true when given something it does not | |
9911 | understand. */ | |
9912 | skip = !block_may_fallthru (cur_stmt_list); | |
9913 | ||
506e2710 | 9914 | if (!label) |
089efaa4 ILT |
9915 | { |
9916 | if (!skip) | |
c2255bc4 | 9917 | *label_p = label = create_artificial_label (loc); |
089efaa4 | 9918 | } |
953ff289 DN |
9919 | else if (TREE_CODE (label) == LABEL_DECL) |
9920 | ; | |
9921 | else switch (TREE_INT_CST_LOW (label)) | |
506e2710 | 9922 | { |
953ff289 | 9923 | case 0: |
506e2710 | 9924 | if (is_break) |
c2255bc4 | 9925 | error_at (loc, "break statement not within loop or switch"); |
506e2710 | 9926 | else |
c2255bc4 | 9927 | error_at (loc, "continue statement not within a loop"); |
506e2710 | 9928 | return NULL_TREE; |
953ff289 DN |
9929 | |
9930 | case 1: | |
9931 | gcc_assert (is_break); | |
c2255bc4 | 9932 | error_at (loc, "break statement used with OpenMP for loop"); |
953ff289 DN |
9933 | return NULL_TREE; |
9934 | ||
c02065fc AH |
9935 | case 2: |
9936 | if (is_break) | |
9937 | error ("break statement within %<#pragma simd%> loop body"); | |
9938 | else | |
9939 | error ("continue statement within %<#pragma simd%> loop body"); | |
9940 | return NULL_TREE; | |
9941 | ||
953ff289 DN |
9942 | default: |
9943 | gcc_unreachable (); | |
506e2710 | 9944 | } |
325c3691 | 9945 | |
089efaa4 ILT |
9946 | if (skip) |
9947 | return NULL_TREE; | |
9948 | ||
2e28e797 JH |
9949 | if (!is_break) |
9950 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
9951 | ||
53fb4de3 | 9952 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
325c3691 RH |
9953 | } |
9954 | ||
506e2710 | 9955 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
3a5b9284 RH |
9956 | |
9957 | static void | |
c2255bc4 | 9958 | emit_side_effect_warnings (location_t loc, tree expr) |
3a5b9284 | 9959 | { |
e6b5a630 RH |
9960 | if (expr == error_mark_node) |
9961 | ; | |
9962 | else if (!TREE_SIDE_EFFECTS (expr)) | |
3a5b9284 RH |
9963 | { |
9964 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
c2255bc4 | 9965 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); |
3a5b9284 | 9966 | } |
789eadcd MP |
9967 | else if (TREE_CODE (expr) == COMPOUND_EXPR) |
9968 | { | |
9969 | tree r = expr; | |
9970 | location_t cloc = loc; | |
9971 | while (TREE_CODE (r) == COMPOUND_EXPR) | |
9972 | { | |
9973 | if (EXPR_HAS_LOCATION (r)) | |
9974 | cloc = EXPR_LOCATION (r); | |
9975 | r = TREE_OPERAND (r, 1); | |
9976 | } | |
9977 | if (!TREE_SIDE_EFFECTS (r) | |
9978 | && !VOID_TYPE_P (TREE_TYPE (r)) | |
9979 | && !CONVERT_EXPR_P (r) | |
cc28fc7f | 9980 | && !TREE_NO_WARNING (r) |
789eadcd MP |
9981 | && !TREE_NO_WARNING (expr)) |
9982 | warning_at (cloc, OPT_Wunused_value, | |
9983 | "right-hand operand of comma expression has no effect"); | |
9984 | } | |
27f33b15 | 9985 | else |
c2255bc4 | 9986 | warn_if_unused_value (expr, loc); |
3a5b9284 RH |
9987 | } |
9988 | ||
506e2710 | 9989 | /* Process an expression as if it were a complete statement. Emit |
c2255bc4 AH |
9990 | diagnostics, but do not call ADD_STMT. LOC is the location of the |
9991 | statement. */ | |
3a5b9284 | 9992 | |
506e2710 | 9993 | tree |
c2255bc4 | 9994 | c_process_expr_stmt (location_t loc, tree expr) |
3a5b9284 | 9995 | { |
056928b2 JJ |
9996 | tree exprv; |
9997 | ||
3a5b9284 | 9998 | if (!expr) |
506e2710 | 9999 | return NULL_TREE; |
3a5b9284 | 10000 | |
928c19bb JM |
10001 | expr = c_fully_fold (expr, false, NULL); |
10002 | ||
3a5b9284 RH |
10003 | if (warn_sequence_point) |
10004 | verify_sequence_points (expr); | |
10005 | ||
10006 | if (TREE_TYPE (expr) != error_mark_node | |
10007 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
10008 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
c2255bc4 | 10009 | error_at (loc, "expression statement has incomplete type"); |
3a5b9284 RH |
10010 | |
10011 | /* If we're not processing a statement expression, warn about unused values. | |
10012 | Warnings for statement expressions will be emitted later, once we figure | |
10013 | out which is the result. */ | |
10014 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
27f33b15 | 10015 | && warn_unused_value) |
c2255bc4 | 10016 | emit_side_effect_warnings (loc, expr); |
3a5b9284 | 10017 | |
056928b2 JJ |
10018 | exprv = expr; |
10019 | while (TREE_CODE (exprv) == COMPOUND_EXPR) | |
10020 | exprv = TREE_OPERAND (exprv, 1); | |
f1a89dd0 JJ |
10021 | while (CONVERT_EXPR_P (exprv)) |
10022 | exprv = TREE_OPERAND (exprv, 0); | |
10023 | if (DECL_P (exprv) | |
10024 | || handled_component_p (exprv) | |
10025 | || TREE_CODE (exprv) == ADDR_EXPR) | |
056928b2 | 10026 | mark_exp_read (exprv); |
fa8351f8 | 10027 | |
3a5b9284 RH |
10028 | /* If the expression is not of a type to which we cannot assign a line |
10029 | number, wrap the thing in a no-op NOP_EXPR. */ | |
6615c446 | 10030 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) |
c2255bc4 AH |
10031 | { |
10032 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
10033 | SET_EXPR_LOCATION (expr, loc); | |
10034 | } | |
506e2710 RH |
10035 | |
10036 | return expr; | |
10037 | } | |
10038 | ||
c2255bc4 AH |
10039 | /* Emit an expression as a statement. LOC is the location of the |
10040 | expression. */ | |
506e2710 RH |
10041 | |
10042 | tree | |
c2255bc4 | 10043 | c_finish_expr_stmt (location_t loc, tree expr) |
506e2710 RH |
10044 | { |
10045 | if (expr) | |
c2255bc4 | 10046 | return add_stmt (c_process_expr_stmt (loc, expr)); |
506e2710 RH |
10047 | else |
10048 | return NULL; | |
3a5b9284 RH |
10049 | } |
10050 | ||
10051 | /* Do the opposite and emit a statement as an expression. To begin, | |
10052 | create a new binding level and return it. */ | |
325c3691 RH |
10053 | |
10054 | tree | |
10055 | c_begin_stmt_expr (void) | |
10056 | { | |
10057 | tree ret; | |
10058 | ||
10059 | /* We must force a BLOCK for this level so that, if it is not expanded | |
10060 | later, there is a way to turn off the entire subtree of blocks that | |
10061 | are contained in it. */ | |
10062 | keep_next_level (); | |
10063 | ret = c_begin_compound_stmt (true); | |
e1b7793c ILT |
10064 | |
10065 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
10066 | ? NULL | |
10067 | : c_switch_stack->bindings); | |
325c3691 RH |
10068 | |
10069 | /* Mark the current statement list as belonging to a statement list. */ | |
10070 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
10071 | ||
10072 | return ret; | |
10073 | } | |
10074 | ||
c2255bc4 AH |
10075 | /* LOC is the location of the compound statement to which this body |
10076 | belongs. */ | |
10077 | ||
325c3691 | 10078 | tree |
c2255bc4 | 10079 | c_finish_stmt_expr (location_t loc, tree body) |
325c3691 | 10080 | { |
3a5b9284 | 10081 | tree last, type, tmp, val; |
325c3691 RH |
10082 | tree *last_p; |
10083 | ||
c2255bc4 | 10084 | body = c_end_compound_stmt (loc, body, true); |
e1b7793c ILT |
10085 | |
10086 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
10087 | ? NULL | |
10088 | : c_switch_stack->bindings); | |
325c3691 | 10089 | |
3a5b9284 RH |
10090 | /* Locate the last statement in BODY. See c_end_compound_stmt |
10091 | about always returning a BIND_EXPR. */ | |
10092 | last_p = &BIND_EXPR_BODY (body); | |
10093 | last = BIND_EXPR_BODY (body); | |
10094 | ||
10095 | continue_searching: | |
325c3691 RH |
10096 | if (TREE_CODE (last) == STATEMENT_LIST) |
10097 | { | |
3a5b9284 RH |
10098 | tree_stmt_iterator i; |
10099 | ||
10100 | /* This can happen with degenerate cases like ({ }). No value. */ | |
10101 | if (!TREE_SIDE_EFFECTS (last)) | |
10102 | return body; | |
10103 | ||
10104 | /* If we're supposed to generate side effects warnings, process | |
10105 | all of the statements except the last. */ | |
27f33b15 | 10106 | if (warn_unused_value) |
325c3691 | 10107 | { |
3a5b9284 | 10108 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
c2255bc4 AH |
10109 | { |
10110 | location_t tloc; | |
10111 | tree t = tsi_stmt (i); | |
10112 | ||
10113 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
10114 | emit_side_effect_warnings (tloc, t); | |
10115 | } | |
325c3691 RH |
10116 | } |
10117 | else | |
3a5b9284 RH |
10118 | i = tsi_last (last); |
10119 | last_p = tsi_stmt_ptr (i); | |
10120 | last = *last_p; | |
325c3691 RH |
10121 | } |
10122 | ||
3a5b9284 RH |
10123 | /* If the end of the list is exception related, then the list was split |
10124 | by a call to push_cleanup. Continue searching. */ | |
10125 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
10126 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
10127 | { | |
10128 | last_p = &TREE_OPERAND (last, 0); | |
10129 | last = *last_p; | |
10130 | goto continue_searching; | |
10131 | } | |
10132 | ||
26d8af35 JM |
10133 | if (last == error_mark_node) |
10134 | return last; | |
10135 | ||
3a5b9284 RH |
10136 | /* In the case that the BIND_EXPR is not necessary, return the |
10137 | expression out from inside it. */ | |
26d8af35 JM |
10138 | if (last == BIND_EXPR_BODY (body) |
10139 | && BIND_EXPR_VARS (body) == NULL) | |
591baeb0 | 10140 | { |
928c19bb JM |
10141 | /* Even if this looks constant, do not allow it in a constant |
10142 | expression. */ | |
e5a94231 | 10143 | last = c_wrap_maybe_const (last, true); |
591baeb0 JM |
10144 | /* Do not warn if the return value of a statement expression is |
10145 | unused. */ | |
928c19bb | 10146 | TREE_NO_WARNING (last) = 1; |
591baeb0 JM |
10147 | return last; |
10148 | } | |
325c3691 RH |
10149 | |
10150 | /* Extract the type of said expression. */ | |
10151 | type = TREE_TYPE (last); | |
325c3691 | 10152 | |
3a5b9284 RH |
10153 | /* If we're not returning a value at all, then the BIND_EXPR that |
10154 | we already have is a fine expression to return. */ | |
10155 | if (!type || VOID_TYPE_P (type)) | |
10156 | return body; | |
10157 | ||
10158 | /* Now that we've located the expression containing the value, it seems | |
10159 | silly to make voidify_wrapper_expr repeat the process. Create a | |
10160 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
b731b390 | 10161 | tmp = create_tmp_var_raw (type); |
3a5b9284 RH |
10162 | |
10163 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
10164 | tree_expr_nonnegative_p giving up immediately. */ | |
10165 | val = last; | |
10166 | if (TREE_CODE (val) == NOP_EXPR | |
10167 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
10168 | val = TREE_OPERAND (val, 0); | |
10169 | ||
53fb4de3 | 10170 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
5e278028 | 10171 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); |
3a5b9284 | 10172 | |
c2255bc4 AH |
10173 | { |
10174 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
10175 | SET_EXPR_LOCATION (t, loc); | |
10176 | return t; | |
10177 | } | |
325c3691 RH |
10178 | } |
10179 | \f | |
10180 | /* Begin and end compound statements. This is as simple as pushing | |
10181 | and popping new statement lists from the tree. */ | |
10182 | ||
10183 | tree | |
10184 | c_begin_compound_stmt (bool do_scope) | |
10185 | { | |
10186 | tree stmt = push_stmt_list (); | |
10187 | if (do_scope) | |
4dfa0342 | 10188 | push_scope (); |
325c3691 RH |
10189 | return stmt; |
10190 | } | |
10191 | ||
c2255bc4 AH |
10192 | /* End a compound statement. STMT is the statement. LOC is the |
10193 | location of the compound statement-- this is usually the location | |
10194 | of the opening brace. */ | |
10195 | ||
325c3691 | 10196 | tree |
c2255bc4 | 10197 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) |
325c3691 RH |
10198 | { |
10199 | tree block = NULL; | |
10200 | ||
10201 | if (do_scope) | |
10202 | { | |
10203 | if (c_dialect_objc ()) | |
10204 | objc_clear_super_receiver (); | |
10205 | block = pop_scope (); | |
10206 | } | |
10207 | ||
10208 | stmt = pop_stmt_list (stmt); | |
c2255bc4 | 10209 | stmt = c_build_bind_expr (loc, block, stmt); |
325c3691 RH |
10210 | |
10211 | /* If this compound statement is nested immediately inside a statement | |
10212 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
10213 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
10214 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
10215 | was really last. */ | |
38e01f9e | 10216 | if (building_stmt_list_p () |
325c3691 RH |
10217 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) |
10218 | && TREE_CODE (stmt) != BIND_EXPR) | |
10219 | { | |
53fb4de3 | 10220 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
325c3691 | 10221 | TREE_SIDE_EFFECTS (stmt) = 1; |
c2255bc4 | 10222 | SET_EXPR_LOCATION (stmt, loc); |
325c3691 RH |
10223 | } |
10224 | ||
10225 | return stmt; | |
10226 | } | |
5a508662 RH |
10227 | |
10228 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
10229 | when the current scope is exited. EH_ONLY is true when this is not | |
10230 | meant to apply to normal control flow transfer. */ | |
10231 | ||
10232 | void | |
c2255bc4 | 10233 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
5a508662 | 10234 | { |
3a5b9284 RH |
10235 | enum tree_code code; |
10236 | tree stmt, list; | |
10237 | bool stmt_expr; | |
10238 | ||
10239 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
c2255bc4 | 10240 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); |
5a508662 | 10241 | add_stmt (stmt); |
3a5b9284 RH |
10242 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
10243 | list = push_stmt_list (); | |
10244 | TREE_OPERAND (stmt, 0) = list; | |
10245 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
5a508662 | 10246 | } |
325c3691 | 10247 | \f |
9f47c7e5 IE |
10248 | /* Build a vector comparison of ARG0 and ARG1 using CODE opcode |
10249 | into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */ | |
10250 | ||
10251 | static tree | |
10252 | build_vec_cmp (tree_code code, tree type, | |
10253 | tree arg0, tree arg1) | |
10254 | { | |
10255 | tree zero_vec = build_zero_cst (type); | |
10256 | tree minus_one_vec = build_minus_one_cst (type); | |
10257 | tree cmp_type = build_same_sized_truth_vector_type (type); | |
10258 | tree cmp = build2 (code, cmp_type, arg0, arg1); | |
10259 | return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec); | |
10260 | } | |
10261 | ||
3e4093b6 RS |
10262 | /* Build a binary-operation expression without default conversions. |
10263 | CODE is the kind of expression to build. | |
ba47d38d | 10264 | LOCATION is the operator's location. |
3e4093b6 RS |
10265 | This function differs from `build' in several ways: |
10266 | the data type of the result is computed and recorded in it, | |
10267 | warnings are generated if arg data types are invalid, | |
10268 | special handling for addition and subtraction of pointers is known, | |
10269 | and some optimization is done (operations on narrow ints | |
10270 | are done in the narrower type when that gives the same result). | |
10271 | Constant folding is also done before the result is returned. | |
de520661 | 10272 | |
3e4093b6 RS |
10273 | Note that the operands will never have enumeral types, or function |
10274 | or array types, because either they will have the default conversions | |
10275 | performed or they have both just been converted to some other type in which | |
10276 | the arithmetic is to be done. */ | |
10277 | ||
10278 | tree | |
ba47d38d AH |
10279 | build_binary_op (location_t location, enum tree_code code, |
10280 | tree orig_op0, tree orig_op1, int convert_p) | |
de520661 | 10281 | { |
8ce94e44 JM |
10282 | tree type0, type1, orig_type0, orig_type1; |
10283 | tree eptype; | |
3e4093b6 RS |
10284 | enum tree_code code0, code1; |
10285 | tree op0, op1; | |
c9f9eb5d | 10286 | tree ret = error_mark_node; |
4de67c26 | 10287 | const char *invalid_op_diag; |
4d84fe7c | 10288 | bool op0_int_operands, op1_int_operands; |
928c19bb | 10289 | bool int_const, int_const_or_overflow, int_operands; |
b62acd60 | 10290 | |
3e4093b6 RS |
10291 | /* Expression code to give to the expression when it is built. |
10292 | Normally this is CODE, which is what the caller asked for, | |
10293 | but in some special cases we change it. */ | |
10294 | enum tree_code resultcode = code; | |
8b6a5902 | 10295 | |
3e4093b6 RS |
10296 | /* Data type in which the computation is to be performed. |
10297 | In the simplest cases this is the common type of the arguments. */ | |
10298 | tree result_type = NULL; | |
10299 | ||
8ce94e44 JM |
10300 | /* When the computation is in excess precision, the type of the |
10301 | final EXCESS_PRECISION_EXPR. */ | |
2d2e923f | 10302 | tree semantic_result_type = NULL; |
8ce94e44 | 10303 | |
3e4093b6 RS |
10304 | /* Nonzero means operands have already been type-converted |
10305 | in whatever way is necessary. | |
10306 | Zero means they need to be converted to RESULT_TYPE. */ | |
10307 | int converted = 0; | |
10308 | ||
10309 | /* Nonzero means create the expression with this type, rather than | |
10310 | RESULT_TYPE. */ | |
10311 | tree build_type = 0; | |
10312 | ||
10313 | /* Nonzero means after finally constructing the expression | |
10314 | convert it to this type. */ | |
10315 | tree final_type = 0; | |
10316 | ||
10317 | /* Nonzero if this is an operation like MIN or MAX which can | |
10318 | safely be computed in short if both args are promoted shorts. | |
10319 | Also implies COMMON. | |
10320 | -1 indicates a bitwise operation; this makes a difference | |
10321 | in the exact conditions for when it is safe to do the operation | |
10322 | in a narrower mode. */ | |
10323 | int shorten = 0; | |
10324 | ||
10325 | /* Nonzero if this is a comparison operation; | |
10326 | if both args are promoted shorts, compare the original shorts. | |
10327 | Also implies COMMON. */ | |
10328 | int short_compare = 0; | |
10329 | ||
10330 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
10331 | original short and then promoted if the operand is a promoted short. */ | |
10332 | int short_shift = 0; | |
10333 | ||
10334 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
10335 | int common = 0; | |
10336 | ||
58393038 ZL |
10337 | /* True means types are compatible as far as ObjC is concerned. */ |
10338 | bool objc_ok; | |
10339 | ||
8ce94e44 JM |
10340 | /* True means this is an arithmetic operation that may need excess |
10341 | precision. */ | |
10342 | bool may_need_excess_precision; | |
10343 | ||
180f8dbb JM |
10344 | /* True means this is a boolean operation that converts both its |
10345 | operands to truth-values. */ | |
10346 | bool boolean_op = false; | |
10347 | ||
de5a5fa1 MP |
10348 | /* Remember whether we're doing / or %. */ |
10349 | bool doing_div_or_mod = false; | |
10350 | ||
10351 | /* Remember whether we're doing << or >>. */ | |
10352 | bool doing_shift = false; | |
10353 | ||
10354 | /* Tree holding instrumentation expression. */ | |
10355 | tree instrument_expr = NULL; | |
10356 | ||
ba47d38d AH |
10357 | if (location == UNKNOWN_LOCATION) |
10358 | location = input_location; | |
10359 | ||
4d84fe7c JM |
10360 | op0 = orig_op0; |
10361 | op1 = orig_op1; | |
10362 | ||
10363 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
10364 | if (op0_int_operands) | |
10365 | op0 = remove_c_maybe_const_expr (op0); | |
10366 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
10367 | if (op1_int_operands) | |
10368 | op1 = remove_c_maybe_const_expr (op1); | |
10369 | int_operands = (op0_int_operands && op1_int_operands); | |
928c19bb JM |
10370 | if (int_operands) |
10371 | { | |
10372 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
10373 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
10374 | int_const = (int_const_or_overflow | |
10375 | && !TREE_OVERFLOW (orig_op0) | |
10376 | && !TREE_OVERFLOW (orig_op1)); | |
10377 | } | |
10378 | else | |
10379 | int_const = int_const_or_overflow = false; | |
10380 | ||
0e3a99ae | 10381 | /* Do not apply default conversion in mixed vector/scalar expression. */ |
f90e8e2e | 10382 | if (convert_p |
31521951 | 10383 | && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1))) |
790e9490 | 10384 | { |
4d84fe7c JM |
10385 | op0 = default_conversion (op0); |
10386 | op1 = default_conversion (op1); | |
790e9490 RS |
10387 | } |
10388 | ||
36536d79 BI |
10389 | /* When Cilk Plus is enabled and there are array notations inside op0, then |
10390 | we check to see if there are builtin array notation functions. If | |
10391 | so, then we take on the type of the array notation inside it. */ | |
b72271b9 | 10392 | if (flag_cilkplus && contains_array_notation_expr (op0)) |
36536d79 BI |
10393 | orig_type0 = type0 = find_correct_array_notation_type (op0); |
10394 | else | |
10395 | orig_type0 = type0 = TREE_TYPE (op0); | |
10396 | ||
b72271b9 | 10397 | if (flag_cilkplus && contains_array_notation_expr (op1)) |
36536d79 BI |
10398 | orig_type1 = type1 = find_correct_array_notation_type (op1); |
10399 | else | |
10400 | orig_type1 = type1 = TREE_TYPE (op1); | |
91fa3c30 | 10401 | |
3e4093b6 RS |
10402 | /* The expression codes of the data types of the arguments tell us |
10403 | whether the arguments are integers, floating, pointers, etc. */ | |
10404 | code0 = TREE_CODE (type0); | |
10405 | code1 = TREE_CODE (type1); | |
10406 | ||
10407 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
10408 | STRIP_TYPE_NOPS (op0); | |
10409 | STRIP_TYPE_NOPS (op1); | |
10410 | ||
10411 | /* If an error was already reported for one of the arguments, | |
10412 | avoid reporting another error. */ | |
10413 | ||
10414 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
10415 | return error_mark_node; | |
10416 | ||
1807ffc1 MS |
10417 | if (code0 == POINTER_TYPE |
10418 | && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0))) | |
10419 | return error_mark_node; | |
10420 | ||
10421 | if (code1 == POINTER_TYPE | |
10422 | && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1))) | |
10423 | return error_mark_node; | |
10424 | ||
4de67c26 JM |
10425 | if ((invalid_op_diag |
10426 | = targetm.invalid_binary_op (code, type0, type1))) | |
10427 | { | |
ba47d38d | 10428 | error_at (location, invalid_op_diag); |
4de67c26 JM |
10429 | return error_mark_node; |
10430 | } | |
10431 | ||
8ce94e44 JM |
10432 | switch (code) |
10433 | { | |
10434 | case PLUS_EXPR: | |
10435 | case MINUS_EXPR: | |
10436 | case MULT_EXPR: | |
10437 | case TRUNC_DIV_EXPR: | |
10438 | case CEIL_DIV_EXPR: | |
10439 | case FLOOR_DIV_EXPR: | |
10440 | case ROUND_DIV_EXPR: | |
10441 | case EXACT_DIV_EXPR: | |
10442 | may_need_excess_precision = true; | |
10443 | break; | |
10444 | default: | |
10445 | may_need_excess_precision = false; | |
10446 | break; | |
10447 | } | |
10448 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
10449 | { | |
10450 | op0 = TREE_OPERAND (op0, 0); | |
10451 | type0 = TREE_TYPE (op0); | |
10452 | } | |
10453 | else if (may_need_excess_precision | |
10454 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
10455 | { | |
10456 | type0 = eptype; | |
10457 | op0 = convert (eptype, op0); | |
10458 | } | |
10459 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
10460 | { | |
10461 | op1 = TREE_OPERAND (op1, 0); | |
10462 | type1 = TREE_TYPE (op1); | |
10463 | } | |
10464 | else if (may_need_excess_precision | |
10465 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
10466 | { | |
10467 | type1 = eptype; | |
10468 | op1 = convert (eptype, op1); | |
10469 | } | |
10470 | ||
58393038 ZL |
10471 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); |
10472 | ||
0e3a99ae AS |
10473 | /* In case when one of the operands of the binary operation is |
10474 | a vector and another is a scalar -- convert scalar to vector. */ | |
10475 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
10476 | { | |
a212e43f MG |
10477 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, |
10478 | true); | |
f90e8e2e | 10479 | |
0e3a99ae AS |
10480 | switch (convert_flag) |
10481 | { | |
10482 | case stv_error: | |
10483 | return error_mark_node; | |
10484 | case stv_firstarg: | |
10485 | { | |
10486 | bool maybe_const = true; | |
10487 | tree sc; | |
10488 | sc = c_fully_fold (op0, false, &maybe_const); | |
10489 | sc = save_expr (sc); | |
10490 | sc = convert (TREE_TYPE (type1), sc); | |
10491 | op0 = build_vector_from_val (type1, sc); | |
10492 | if (!maybe_const) | |
10493 | op0 = c_wrap_maybe_const (op0, true); | |
10494 | orig_type0 = type0 = TREE_TYPE (op0); | |
10495 | code0 = TREE_CODE (type0); | |
10496 | converted = 1; | |
10497 | break; | |
10498 | } | |
10499 | case stv_secondarg: | |
10500 | { | |
10501 | bool maybe_const = true; | |
10502 | tree sc; | |
10503 | sc = c_fully_fold (op1, false, &maybe_const); | |
10504 | sc = save_expr (sc); | |
10505 | sc = convert (TREE_TYPE (type0), sc); | |
10506 | op1 = build_vector_from_val (type0, sc); | |
10507 | if (!maybe_const) | |
54b9f838 | 10508 | op1 = c_wrap_maybe_const (op1, true); |
0e3a99ae AS |
10509 | orig_type1 = type1 = TREE_TYPE (op1); |
10510 | code1 = TREE_CODE (type1); | |
10511 | converted = 1; | |
10512 | break; | |
10513 | } | |
10514 | default: | |
10515 | break; | |
10516 | } | |
10517 | } | |
10518 | ||
3e4093b6 | 10519 | switch (code) |
de520661 | 10520 | { |
3e4093b6 RS |
10521 | case PLUS_EXPR: |
10522 | /* Handle the pointer + int case. */ | |
10523 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 10524 | { |
db3927fb | 10525 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); |
c9f9eb5d AH |
10526 | goto return_build_binary_op; |
10527 | } | |
3e4093b6 | 10528 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) |
c9f9eb5d | 10529 | { |
db3927fb | 10530 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); |
c9f9eb5d AH |
10531 | goto return_build_binary_op; |
10532 | } | |
fe67cf58 | 10533 | else |
3e4093b6 RS |
10534 | common = 1; |
10535 | break; | |
400fbf9f | 10536 | |
3e4093b6 RS |
10537 | case MINUS_EXPR: |
10538 | /* Subtraction of two similar pointers. | |
10539 | We must subtract them as integers, then divide by object size. */ | |
10540 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
744aa42f | 10541 | && comp_target_types (location, type0, type1)) |
c9f9eb5d | 10542 | { |
db3927fb | 10543 | ret = pointer_diff (location, op0, op1); |
c9f9eb5d AH |
10544 | goto return_build_binary_op; |
10545 | } | |
3e4093b6 RS |
10546 | /* Handle pointer minus int. Just like pointer plus int. */ |
10547 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 10548 | { |
db3927fb | 10549 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); |
c9f9eb5d AH |
10550 | goto return_build_binary_op; |
10551 | } | |
3e4093b6 RS |
10552 | else |
10553 | common = 1; | |
10554 | break; | |
8b6a5902 | 10555 | |
3e4093b6 RS |
10556 | case MULT_EXPR: |
10557 | common = 1; | |
10558 | break; | |
10559 | ||
10560 | case TRUNC_DIV_EXPR: | |
10561 | case CEIL_DIV_EXPR: | |
10562 | case FLOOR_DIV_EXPR: | |
10563 | case ROUND_DIV_EXPR: | |
10564 | case EXACT_DIV_EXPR: | |
de5a5fa1 | 10565 | doing_div_or_mod = true; |
c9f9eb5d | 10566 | warn_for_div_by_zero (location, op1); |
3e4093b6 RS |
10567 | |
10568 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 10569 | || code0 == FIXED_POINT_TYPE |
3e4093b6 RS |
10570 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
10571 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
ab22c1fa | 10572 | || code1 == FIXED_POINT_TYPE |
3e4093b6 | 10573 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 10574 | { |
5bed876a AH |
10575 | enum tree_code tcode0 = code0, tcode1 = code1; |
10576 | ||
3a021db2 | 10577 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
5bed876a | 10578 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
3a021db2 | 10579 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) |
5bed876a | 10580 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
3a021db2 | 10581 | |
ab22c1fa CF |
10582 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) |
10583 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
3e4093b6 RS |
10584 | resultcode = RDIV_EXPR; |
10585 | else | |
10586 | /* Although it would be tempting to shorten always here, that | |
10587 | loses on some targets, since the modulo instruction is | |
10588 | undefined if the quotient can't be represented in the | |
10589 | computation mode. We shorten only if unsigned or if | |
10590 | dividing by something we know != -1. */ | |
8df83eae | 10591 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 10592 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 10593 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
10594 | common = 1; |
10595 | } | |
10596 | break; | |
de520661 | 10597 | |
3e4093b6 | 10598 | case BIT_AND_EXPR: |
3e4093b6 RS |
10599 | case BIT_IOR_EXPR: |
10600 | case BIT_XOR_EXPR: | |
10601 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
10602 | shorten = -1; | |
9ef0c8d9 AP |
10603 | /* Allow vector types which are not floating point types. */ |
10604 | else if (code0 == VECTOR_TYPE | |
10605 | && code1 == VECTOR_TYPE | |
10606 | && !VECTOR_FLOAT_TYPE_P (type0) | |
10607 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
3e4093b6 RS |
10608 | common = 1; |
10609 | break; | |
10610 | ||
10611 | case TRUNC_MOD_EXPR: | |
10612 | case FLOOR_MOD_EXPR: | |
de5a5fa1 | 10613 | doing_div_or_mod = true; |
c9f9eb5d | 10614 | warn_for_div_by_zero (location, op1); |
de520661 | 10615 | |
5cfd5d9b AP |
10616 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
10617 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
10618 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
10619 | common = 1; | |
10620 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3e4093b6 RS |
10621 | { |
10622 | /* Although it would be tempting to shorten always here, that loses | |
10623 | on some targets, since the modulo instruction is undefined if the | |
10624 | quotient can't be represented in the computation mode. We shorten | |
10625 | only if unsigned or if dividing by something we know != -1. */ | |
8df83eae | 10626 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 10627 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 10628 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
10629 | common = 1; |
10630 | } | |
10631 | break; | |
de520661 | 10632 | |
3e4093b6 RS |
10633 | case TRUTH_ANDIF_EXPR: |
10634 | case TRUTH_ORIF_EXPR: | |
10635 | case TRUTH_AND_EXPR: | |
10636 | case TRUTH_OR_EXPR: | |
10637 | case TRUTH_XOR_EXPR: | |
10638 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
ab22c1fa CF |
10639 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
10640 | || code0 == FIXED_POINT_TYPE) | |
3e4093b6 | 10641 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE |
ab22c1fa CF |
10642 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE |
10643 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10644 | { |
10645 | /* Result of these operations is always an int, | |
10646 | but that does not mean the operands should be | |
10647 | converted to ints! */ | |
10648 | result_type = integer_type_node; | |
a27d595d JM |
10649 | if (op0_int_operands) |
10650 | { | |
10651 | op0 = c_objc_common_truthvalue_conversion (location, orig_op0); | |
10652 | op0 = remove_c_maybe_const_expr (op0); | |
10653 | } | |
10654 | else | |
10655 | op0 = c_objc_common_truthvalue_conversion (location, op0); | |
10656 | if (op1_int_operands) | |
10657 | { | |
10658 | op1 = c_objc_common_truthvalue_conversion (location, orig_op1); | |
10659 | op1 = remove_c_maybe_const_expr (op1); | |
10660 | } | |
10661 | else | |
10662 | op1 = c_objc_common_truthvalue_conversion (location, op1); | |
3e4093b6 | 10663 | converted = 1; |
180f8dbb | 10664 | boolean_op = true; |
3e4093b6 | 10665 | } |
928c19bb JM |
10666 | if (code == TRUTH_ANDIF_EXPR) |
10667 | { | |
10668 | int_const_or_overflow = (int_operands | |
10669 | && TREE_CODE (orig_op0) == INTEGER_CST | |
10670 | && (op0 == truthvalue_false_node | |
10671 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
10672 | int_const = (int_const_or_overflow | |
10673 | && !TREE_OVERFLOW (orig_op0) | |
10674 | && (op0 == truthvalue_false_node | |
10675 | || !TREE_OVERFLOW (orig_op1))); | |
10676 | } | |
10677 | else if (code == TRUTH_ORIF_EXPR) | |
10678 | { | |
10679 | int_const_or_overflow = (int_operands | |
10680 | && TREE_CODE (orig_op0) == INTEGER_CST | |
10681 | && (op0 == truthvalue_true_node | |
10682 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
10683 | int_const = (int_const_or_overflow | |
10684 | && !TREE_OVERFLOW (orig_op0) | |
10685 | && (op0 == truthvalue_true_node | |
10686 | || !TREE_OVERFLOW (orig_op1))); | |
10687 | } | |
3e4093b6 | 10688 | break; |
eba80994 | 10689 | |
3e4093b6 RS |
10690 | /* Shift operations: result has same type as first operand; |
10691 | always convert second operand to int. | |
10692 | Also set SHORT_SHIFT if shifting rightward. */ | |
de520661 | 10693 | |
3e4093b6 | 10694 | case RSHIFT_EXPR: |
f87bd04b AS |
10695 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
10696 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
10697 | { | |
10698 | result_type = type0; | |
10699 | converted = 1; | |
10700 | } | |
10701 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
451b5e48 MP |
10702 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
10703 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
10704 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
f87bd04b AS |
10705 | { |
10706 | result_type = type0; | |
10707 | converted = 1; | |
10708 | } | |
10709 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
451b5e48 | 10710 | && code1 == INTEGER_TYPE) |
3e4093b6 | 10711 | { |
de5a5fa1 | 10712 | doing_shift = true; |
928c19bb | 10713 | if (TREE_CODE (op1) == INTEGER_CST) |
b62acd60 | 10714 | { |
3e4093b6 | 10715 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
10716 | { |
10717 | int_const = false; | |
7d882b83 | 10718 | if (c_inhibit_evaluation_warnings == 0) |
13c21655 PC |
10719 | warning_at (location, OPT_Wshift_count_negative, |
10720 | "right shift count is negative"); | |
928c19bb | 10721 | } |
3e4093b6 | 10722 | else |
bbb818c6 | 10723 | { |
3f75a254 | 10724 | if (!integer_zerop (op1)) |
3e4093b6 RS |
10725 | short_shift = 1; |
10726 | ||
10727 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
928c19bb JM |
10728 | { |
10729 | int_const = false; | |
7d882b83 | 10730 | if (c_inhibit_evaluation_warnings == 0) |
13c21655 PC |
10731 | warning_at (location, OPT_Wshift_count_overflow, |
10732 | "right shift count >= width of type"); | |
928c19bb | 10733 | } |
bbb818c6 | 10734 | } |
b62acd60 | 10735 | } |
de520661 | 10736 | |
3e4093b6 RS |
10737 | /* Use the type of the value to be shifted. */ |
10738 | result_type = type0; | |
3e4093b6 RS |
10739 | /* Avoid converting op1 to result_type later. */ |
10740 | converted = 1; | |
400fbf9f | 10741 | } |
3e4093b6 | 10742 | break; |
253b6b82 | 10743 | |
3e4093b6 | 10744 | case LSHIFT_EXPR: |
f87bd04b AS |
10745 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
10746 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
10747 | { | |
10748 | result_type = type0; | |
10749 | converted = 1; | |
10750 | } | |
10751 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
451b5e48 MP |
10752 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
10753 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
10754 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
f87bd04b AS |
10755 | { |
10756 | result_type = type0; | |
10757 | converted = 1; | |
10758 | } | |
10759 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
451b5e48 | 10760 | && code1 == INTEGER_TYPE) |
3e4093b6 | 10761 | { |
de5a5fa1 | 10762 | doing_shift = true; |
0173bd2a MP |
10763 | if (TREE_CODE (op0) == INTEGER_CST |
10764 | && tree_int_cst_sgn (op0) < 0) | |
10765 | { | |
10766 | /* Don't reject a left shift of a negative value in a context | |
10767 | where a constant expression is needed in C90. */ | |
10768 | if (flag_isoc99) | |
10769 | int_const = false; | |
10770 | if (c_inhibit_evaluation_warnings == 0) | |
10771 | warning_at (location, OPT_Wshift_negative_value, | |
10772 | "left shift of negative value"); | |
10773 | } | |
928c19bb | 10774 | if (TREE_CODE (op1) == INTEGER_CST) |
de520661 | 10775 | { |
3e4093b6 | 10776 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
10777 | { |
10778 | int_const = false; | |
7d882b83 | 10779 | if (c_inhibit_evaluation_warnings == 0) |
13c21655 PC |
10780 | warning_at (location, OPT_Wshift_count_negative, |
10781 | "left shift count is negative"); | |
928c19bb | 10782 | } |
3e4093b6 | 10783 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
928c19bb JM |
10784 | { |
10785 | int_const = false; | |
7d882b83 | 10786 | if (c_inhibit_evaluation_warnings == 0) |
13c21655 PC |
10787 | warning_at (location, OPT_Wshift_count_overflow, |
10788 | "left shift count >= width of type"); | |
928c19bb | 10789 | } |
451b5e48 MP |
10790 | else if (TREE_CODE (op0) == INTEGER_CST |
10791 | && maybe_warn_shift_overflow (location, op0, op1) | |
10792 | && flag_isoc99) | |
10793 | int_const = false; | |
94ba5069 | 10794 | } |
de520661 | 10795 | |
3e4093b6 RS |
10796 | /* Use the type of the value to be shifted. */ |
10797 | result_type = type0; | |
3e4093b6 RS |
10798 | /* Avoid converting op1 to result_type later. */ |
10799 | converted = 1; | |
400fbf9f | 10800 | } |
3e4093b6 | 10801 | break; |
de520661 | 10802 | |
3e4093b6 RS |
10803 | case EQ_EXPR: |
10804 | case NE_EXPR: | |
d246ab4f AS |
10805 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10806 | { | |
10807 | tree intt; | |
0af94e6f | 10808 | if (!vector_types_compatible_elements_p (type0, type1)) |
d246ab4f AS |
10809 | { |
10810 | error_at (location, "comparing vectors with different " | |
10811 | "element types"); | |
10812 | return error_mark_node; | |
10813 | } | |
10814 | ||
10815 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10816 | { | |
10817 | error_at (location, "comparing vectors with different " | |
10818 | "number of elements"); | |
10819 | return error_mark_node; | |
10820 | } | |
10821 | ||
10822 | /* Always construct signed integer vector type. */ | |
10823 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10824 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10825 | result_type = build_opaque_vector_type (intt, | |
10826 | TYPE_VECTOR_SUBPARTS (type0)); | |
10827 | converted = 1; | |
9f47c7e5 IE |
10828 | ret = build_vec_cmp (resultcode, result_type, op0, op1); |
10829 | goto return_build_binary_op; | |
d246ab4f | 10830 | } |
ae311566 | 10831 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) |
ba47d38d AH |
10832 | warning_at (location, |
10833 | OPT_Wfloat_equal, | |
10834 | "comparing floating point with == or != is unsafe"); | |
3e4093b6 RS |
10835 | /* Result of comparison is always int, |
10836 | but don't convert the args to int! */ | |
10837 | build_type = integer_type_node; | |
10838 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 10839 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) |
3e4093b6 | 10840 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
ab22c1fa | 10841 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) |
3e4093b6 | 10842 | short_compare = 1; |
637f1455 SZ |
10843 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
10844 | { | |
dbb68221 MW |
10845 | if (warn_nonnull |
10846 | && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0)) | |
10847 | warning_at (location, OPT_Wnonnull, | |
10848 | "nonnull argument %qD compared to NULL", op0); | |
10849 | ||
637f1455 SZ |
10850 | if (TREE_CODE (op0) == ADDR_EXPR |
10851 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) | |
10852 | { | |
10853 | if (code == EQ_EXPR) | |
10854 | warning_at (location, | |
10855 | OPT_Waddress, | |
10856 | "the comparison will always evaluate as %<false%> " | |
10857 | "for the address of %qD will never be NULL", | |
10858 | TREE_OPERAND (op0, 0)); | |
10859 | else | |
10860 | warning_at (location, | |
10861 | OPT_Waddress, | |
10862 | "the comparison will always evaluate as %<true%> " | |
10863 | "for the address of %qD will never be NULL", | |
10864 | TREE_OPERAND (op0, 0)); | |
10865 | } | |
10866 | result_type = type0; | |
10867 | } | |
10868 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) | |
10869 | { | |
dbb68221 MW |
10870 | if (warn_nonnull |
10871 | && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1)) | |
10872 | warning_at (location, OPT_Wnonnull, | |
10873 | "nonnull argument %qD compared to NULL", op1); | |
10874 | ||
637f1455 SZ |
10875 | if (TREE_CODE (op1) == ADDR_EXPR |
10876 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) | |
10877 | { | |
10878 | if (code == EQ_EXPR) | |
10879 | warning_at (location, | |
f90e8e2e | 10880 | OPT_Waddress, |
637f1455 SZ |
10881 | "the comparison will always evaluate as %<false%> " |
10882 | "for the address of %qD will never be NULL", | |
10883 | TREE_OPERAND (op1, 0)); | |
10884 | else | |
10885 | warning_at (location, | |
10886 | OPT_Waddress, | |
10887 | "the comparison will always evaluate as %<true%> " | |
10888 | "for the address of %qD will never be NULL", | |
10889 | TREE_OPERAND (op1, 0)); | |
10890 | } | |
10891 | result_type = type1; | |
10892 | } | |
3e4093b6 RS |
10893 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
10894 | { | |
10895 | tree tt0 = TREE_TYPE (type0); | |
10896 | tree tt1 = TREE_TYPE (type1); | |
36c5e70a BE |
10897 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); |
10898 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
10899 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
10900 | ||
3e4093b6 RS |
10901 | /* Anything compares with void *. void * compares with anything. |
10902 | Otherwise, the targets must be compatible | |
10903 | and both must be object or both incomplete. */ | |
744aa42f | 10904 | if (comp_target_types (location, type0, type1)) |
10bc1b1b | 10905 | result_type = common_pointer_type (type0, type1); |
36c5e70a BE |
10906 | else if (!addr_space_superset (as0, as1, &as_common)) |
10907 | { | |
10908 | error_at (location, "comparison of pointers to " | |
10909 | "disjoint address spaces"); | |
10910 | return error_mark_node; | |
10911 | } | |
267bac10 | 10912 | else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0)) |
ee2990e7 | 10913 | { |
36c5e70a | 10914 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) |
c1771a20 | 10915 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10916 | "comparison of %<void *%> with function pointer"); |
ee2990e7 | 10917 | } |
267bac10 | 10918 | else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1)) |
e6834654 | 10919 | { |
36c5e70a | 10920 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) |
c1771a20 | 10921 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10922 | "comparison of %<void *%> with function pointer"); |
e6834654 | 10923 | } |
3e4093b6 | 10924 | else |
58393038 ZL |
10925 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
10926 | if (!objc_ok) | |
ba47d38d | 10927 | pedwarn (location, 0, |
509c9d60 | 10928 | "comparison of distinct pointer types lacks a cast"); |
e6834654 | 10929 | |
3e4093b6 | 10930 | if (result_type == NULL_TREE) |
36c5e70a BE |
10931 | { |
10932 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
10933 | result_type = build_pointer_type | |
10934 | (build_qualified_type (void_type_node, qual)); | |
10935 | } | |
e6834654 | 10936 | } |
3e4093b6 | 10937 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
de520661 | 10938 | { |
3e4093b6 | 10939 | result_type = type0; |
ba47d38d | 10940 | pedwarn (location, 0, "comparison between pointer and integer"); |
de520661 | 10941 | } |
3e4093b6 | 10942 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
8b6a5902 | 10943 | { |
3e4093b6 | 10944 | result_type = type1; |
ba47d38d | 10945 | pedwarn (location, 0, "comparison between pointer and integer"); |
8b6a5902 | 10946 | } |
04159acf MP |
10947 | if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE |
10948 | || truth_value_p (TREE_CODE (orig_op0))) | |
10949 | ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE | |
10950 | || truth_value_p (TREE_CODE (orig_op1)))) | |
10951 | maybe_warn_bool_compare (location, code, orig_op0, orig_op1); | |
3e4093b6 | 10952 | break; |
8b6a5902 | 10953 | |
3e4093b6 RS |
10954 | case LE_EXPR: |
10955 | case GE_EXPR: | |
10956 | case LT_EXPR: | |
10957 | case GT_EXPR: | |
d246ab4f AS |
10958 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10959 | { | |
10960 | tree intt; | |
0af94e6f | 10961 | if (!vector_types_compatible_elements_p (type0, type1)) |
d246ab4f AS |
10962 | { |
10963 | error_at (location, "comparing vectors with different " | |
10964 | "element types"); | |
10965 | return error_mark_node; | |
10966 | } | |
10967 | ||
10968 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10969 | { | |
10970 | error_at (location, "comparing vectors with different " | |
10971 | "number of elements"); | |
10972 | return error_mark_node; | |
10973 | } | |
10974 | ||
10975 | /* Always construct signed integer vector type. */ | |
10976 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10977 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10978 | result_type = build_opaque_vector_type (intt, | |
10979 | TYPE_VECTOR_SUBPARTS (type0)); | |
10980 | converted = 1; | |
9f47c7e5 IE |
10981 | ret = build_vec_cmp (resultcode, result_type, op0, op1); |
10982 | goto return_build_binary_op; | |
d246ab4f | 10983 | } |
3e4093b6 | 10984 | build_type = integer_type_node; |
ab22c1fa CF |
10985 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
10986 | || code0 == FIXED_POINT_TYPE) | |
10987 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
10988 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10989 | short_compare = 1; |
10990 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
10991 | { | |
36c5e70a BE |
10992 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); |
10993 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
10994 | addr_space_t as_common; | |
10995 | ||
744aa42f | 10996 | if (comp_target_types (location, type0, type1)) |
3e4093b6 | 10997 | { |
10bc1b1b | 10998 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
10999 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
11000 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
ba47d38d | 11001 | pedwarn (location, 0, |
509c9d60 | 11002 | "comparison of complete and incomplete pointers"); |
fcf73884 | 11003 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) |
c1771a20 | 11004 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 11005 | "ordered comparisons of pointers to functions"); |
d42ba3b8 SZ |
11006 | else if (null_pointer_constant_p (orig_op0) |
11007 | || null_pointer_constant_p (orig_op1)) | |
11008 | warning_at (location, OPT_Wextra, | |
11009 | "ordered comparison of pointer with null pointer"); | |
11010 | ||
3e4093b6 | 11011 | } |
36c5e70a BE |
11012 | else if (!addr_space_superset (as0, as1, &as_common)) |
11013 | { | |
11014 | error_at (location, "comparison of pointers to " | |
11015 | "disjoint address spaces"); | |
11016 | return error_mark_node; | |
11017 | } | |
3e4093b6 RS |
11018 | else |
11019 | { | |
36c5e70a BE |
11020 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
11021 | result_type = build_pointer_type | |
11022 | (build_qualified_type (void_type_node, qual)); | |
ba47d38d | 11023 | pedwarn (location, 0, |
509c9d60 | 11024 | "comparison of distinct pointer types lacks a cast"); |
3e4093b6 RS |
11025 | } |
11026 | } | |
6aa3c60d | 11027 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
3e4093b6 RS |
11028 | { |
11029 | result_type = type0; | |
fcf73884 | 11030 | if (pedantic) |
c1771a20 | 11031 | pedwarn (location, OPT_Wpedantic, |
fcf73884 MLI |
11032 | "ordered comparison of pointer with integer zero"); |
11033 | else if (extra_warnings) | |
ba47d38d | 11034 | warning_at (location, OPT_Wextra, |
d42ba3b8 | 11035 | "ordered comparison of pointer with integer zero"); |
3e4093b6 | 11036 | } |
6aa3c60d | 11037 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
3e4093b6 RS |
11038 | { |
11039 | result_type = type1; | |
d42ba3b8 | 11040 | if (pedantic) |
c1771a20 | 11041 | pedwarn (location, OPT_Wpedantic, |
d42ba3b8 SZ |
11042 | "ordered comparison of pointer with integer zero"); |
11043 | else if (extra_warnings) | |
11044 | warning_at (location, OPT_Wextra, | |
11045 | "ordered comparison of pointer with integer zero"); | |
3e4093b6 RS |
11046 | } |
11047 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
11048 | { | |
11049 | result_type = type0; | |
ba47d38d | 11050 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
11051 | } |
11052 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
11053 | { | |
11054 | result_type = type1; | |
ba47d38d | 11055 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 | 11056 | } |
04159acf MP |
11057 | if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE |
11058 | || truth_value_p (TREE_CODE (orig_op0))) | |
11059 | ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE | |
11060 | || truth_value_p (TREE_CODE (orig_op1)))) | |
11061 | maybe_warn_bool_compare (location, code, orig_op0, orig_op1); | |
3e4093b6 | 11062 | break; |
64094f6a | 11063 | |
3e4093b6 | 11064 | default: |
37b2f290 | 11065 | gcc_unreachable (); |
c9fe6f9f | 11066 | } |
8f17b5c5 | 11067 | |
e57e265b PB |
11068 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
11069 | return error_mark_node; | |
11070 | ||
5bed876a AH |
11071 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
11072 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
0af94e6f | 11073 | || !vector_types_compatible_elements_p (type0, type1))) |
5bed876a | 11074 | { |
ba47d38d | 11075 | binary_op_error (location, code, type0, type1); |
5bed876a AH |
11076 | return error_mark_node; |
11077 | } | |
11078 | ||
3e4093b6 | 11079 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
ab22c1fa | 11080 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) |
3e4093b6 RS |
11081 | && |
11082 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
ab22c1fa | 11083 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 11084 | { |
2ca862e9 JM |
11085 | bool first_complex = (code0 == COMPLEX_TYPE); |
11086 | bool second_complex = (code1 == COMPLEX_TYPE); | |
11087 | int none_complex = (!first_complex && !second_complex); | |
39b726dd | 11088 | |
3e4093b6 | 11089 | if (shorten || common || short_compare) |
3bf6bfcc JJ |
11090 | { |
11091 | result_type = c_common_type (type0, type1); | |
0a0b3574 MM |
11092 | do_warn_double_promotion (result_type, type0, type1, |
11093 | "implicit conversion from %qT to %qT " | |
11094 | "to match other operand of binary " | |
11095 | "expression", | |
11096 | location); | |
3bf6bfcc JJ |
11097 | if (result_type == error_mark_node) |
11098 | return error_mark_node; | |
11099 | } | |
400fbf9f | 11100 | |
2ca862e9 JM |
11101 | if (first_complex != second_complex |
11102 | && (code == PLUS_EXPR | |
11103 | || code == MINUS_EXPR | |
11104 | || code == MULT_EXPR | |
11105 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
11106 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
11107 | && flag_signed_zeros) | |
11108 | { | |
11109 | /* An operation on mixed real/complex operands must be | |
11110 | handled specially, but the language-independent code can | |
11111 | more easily optimize the plain complex arithmetic if | |
11112 | -fno-signed-zeros. */ | |
11113 | tree real_type = TREE_TYPE (result_type); | |
11114 | tree real, imag; | |
11115 | if (type0 != orig_type0 || type1 != orig_type1) | |
11116 | { | |
11117 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 11118 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
2ca862e9 JM |
11119 | } |
11120 | if (first_complex) | |
11121 | { | |
11122 | if (TREE_TYPE (op0) != result_type) | |
68fca595 | 11123 | op0 = convert_and_check (location, result_type, op0); |
2ca862e9 | 11124 | if (TREE_TYPE (op1) != real_type) |
68fca595 | 11125 | op1 = convert_and_check (location, real_type, op1); |
2ca862e9 JM |
11126 | } |
11127 | else | |
11128 | { | |
11129 | if (TREE_TYPE (op0) != real_type) | |
68fca595 | 11130 | op0 = convert_and_check (location, real_type, op0); |
2ca862e9 | 11131 | if (TREE_TYPE (op1) != result_type) |
68fca595 | 11132 | op1 = convert_and_check (location, result_type, op1); |
2ca862e9 JM |
11133 | } |
11134 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
11135 | return error_mark_node; | |
11136 | if (first_complex) | |
11137 | { | |
11138 | op0 = c_save_expr (op0); | |
11139 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
11140 | op0, 1); | |
11141 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
11142 | op0, 1); | |
11143 | switch (code) | |
11144 | { | |
11145 | case MULT_EXPR: | |
11146 | case TRUNC_DIV_EXPR: | |
c4603e7c | 11147 | op1 = c_save_expr (op1); |
2ca862e9 JM |
11148 | imag = build2 (resultcode, real_type, imag, op1); |
11149 | /* Fall through. */ | |
11150 | case PLUS_EXPR: | |
11151 | case MINUS_EXPR: | |
11152 | real = build2 (resultcode, real_type, real, op1); | |
11153 | break; | |
11154 | default: | |
11155 | gcc_unreachable(); | |
11156 | } | |
11157 | } | |
11158 | else | |
11159 | { | |
11160 | op1 = c_save_expr (op1); | |
11161 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
11162 | op1, 1); | |
11163 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
11164 | op1, 1); | |
11165 | switch (code) | |
11166 | { | |
11167 | case MULT_EXPR: | |
c4603e7c | 11168 | op0 = c_save_expr (op0); |
2ca862e9 JM |
11169 | imag = build2 (resultcode, real_type, op0, imag); |
11170 | /* Fall through. */ | |
11171 | case PLUS_EXPR: | |
11172 | real = build2 (resultcode, real_type, op0, real); | |
11173 | break; | |
11174 | case MINUS_EXPR: | |
11175 | real = build2 (resultcode, real_type, op0, real); | |
11176 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
11177 | break; | |
11178 | default: | |
11179 | gcc_unreachable(); | |
11180 | } | |
11181 | } | |
11182 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
11183 | goto return_build_binary_op; | |
11184 | } | |
11185 | ||
3e4093b6 RS |
11186 | /* For certain operations (which identify themselves by shorten != 0) |
11187 | if both args were extended from the same smaller type, | |
11188 | do the arithmetic in that type and then extend. | |
400fbf9f | 11189 | |
3e4093b6 RS |
11190 | shorten !=0 and !=1 indicates a bitwise operation. |
11191 | For them, this optimization is safe only if | |
11192 | both args are zero-extended or both are sign-extended. | |
11193 | Otherwise, we might change the result. | |
11194 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
11195 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
400fbf9f | 11196 | |
3e4093b6 RS |
11197 | if (shorten && none_complex) |
11198 | { | |
3e4093b6 | 11199 | final_type = result_type; |
b8698a0f | 11200 | result_type = shorten_binary_op (result_type, op0, op1, |
6715192c | 11201 | shorten == -1); |
3e4093b6 | 11202 | } |
88a3dbc1 | 11203 | |
3e4093b6 | 11204 | /* Shifts can be shortened if shifting right. */ |
2f6e4e97 | 11205 | |
3e4093b6 RS |
11206 | if (short_shift) |
11207 | { | |
11208 | int unsigned_arg; | |
11209 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
88a3dbc1 | 11210 | |
3e4093b6 | 11211 | final_type = result_type; |
abe80e6d | 11212 | |
3e4093b6 | 11213 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
8df83eae | 11214 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
e9a25f70 | 11215 | |
3e4093b6 | 11216 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
be123439 | 11217 | && tree_int_cst_sgn (op1) > 0 |
3e4093b6 RS |
11218 | /* We can shorten only if the shift count is less than the |
11219 | number of bits in the smaller type size. */ | |
11220 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
11221 | /* We cannot drop an unsigned shift after sign-extension. */ | |
8df83eae | 11222 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
3e4093b6 RS |
11223 | { |
11224 | /* Do an unsigned shift if the operand was zero-extended. */ | |
11225 | result_type | |
11226 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
11227 | TREE_TYPE (arg0)); | |
11228 | /* Convert value-to-be-shifted to that type. */ | |
11229 | if (TREE_TYPE (op0) != result_type) | |
11230 | op0 = convert (result_type, op0); | |
11231 | converted = 1; | |
abe80e6d | 11232 | } |
88a3dbc1 RK |
11233 | } |
11234 | ||
3e4093b6 RS |
11235 | /* Comparison operations are shortened too but differently. |
11236 | They identify themselves by setting short_compare = 1. */ | |
56cb9733 | 11237 | |
3e4093b6 RS |
11238 | if (short_compare) |
11239 | { | |
11240 | /* Don't write &op0, etc., because that would prevent op0 | |
11241 | from being kept in a register. | |
11242 | Instead, make copies of the our local variables and | |
11243 | pass the copies by reference, then copy them back afterward. */ | |
11244 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
11245 | enum tree_code xresultcode = resultcode; | |
11246 | tree val | |
393e8e8b MP |
11247 | = shorten_compare (location, &xop0, &xop1, &xresult_type, |
11248 | &xresultcode); | |
8f17b5c5 | 11249 | |
3e4093b6 | 11250 | if (val != 0) |
c9f9eb5d AH |
11251 | { |
11252 | ret = val; | |
11253 | goto return_build_binary_op; | |
11254 | } | |
8f17b5c5 | 11255 | |
3e4093b6 RS |
11256 | op0 = xop0, op1 = xop1; |
11257 | converted = 1; | |
11258 | resultcode = xresultcode; | |
8f17b5c5 | 11259 | |
7d882b83 | 11260 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
11261 | { |
11262 | bool op0_maybe_const = true; | |
11263 | bool op1_maybe_const = true; | |
11264 | tree orig_op0_folded, orig_op1_folded; | |
11265 | ||
11266 | if (in_late_binary_op) | |
11267 | { | |
11268 | orig_op0_folded = orig_op0; | |
11269 | orig_op1_folded = orig_op1; | |
11270 | } | |
11271 | else | |
11272 | { | |
11273 | /* Fold for the sake of possible warnings, as in | |
11274 | build_conditional_expr. This requires the | |
11275 | "original" values to be folded, not just op0 and | |
11276 | op1. */ | |
f5178456 | 11277 | c_inhibit_evaluation_warnings++; |
928c19bb JM |
11278 | op0 = c_fully_fold (op0, require_constant_value, |
11279 | &op0_maybe_const); | |
11280 | op1 = c_fully_fold (op1, require_constant_value, | |
11281 | &op1_maybe_const); | |
f5178456 | 11282 | c_inhibit_evaluation_warnings--; |
928c19bb JM |
11283 | orig_op0_folded = c_fully_fold (orig_op0, |
11284 | require_constant_value, | |
11285 | NULL); | |
11286 | orig_op1_folded = c_fully_fold (orig_op1, | |
11287 | require_constant_value, | |
11288 | NULL); | |
11289 | } | |
11290 | ||
11291 | if (warn_sign_compare) | |
11292 | warn_for_sign_compare (location, orig_op0_folded, | |
11293 | orig_op1_folded, op0, op1, | |
11294 | result_type, resultcode); | |
5c2f94b4 | 11295 | if (!in_late_binary_op && !int_operands) |
928c19bb JM |
11296 | { |
11297 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
e5a94231 | 11298 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); |
928c19bb | 11299 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) |
e5a94231 | 11300 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 11301 | } |
3e4093b6 | 11302 | } |
2ad1815d | 11303 | } |
64094f6a | 11304 | } |
64094f6a | 11305 | |
3e4093b6 RS |
11306 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
11307 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
11308 | Then the expression will be built. | |
11309 | It will be given type FINAL_TYPE if that is nonzero; | |
11310 | otherwise, it will be given type RESULT_TYPE. */ | |
400fbf9f | 11311 | |
3e4093b6 RS |
11312 | if (!result_type) |
11313 | { | |
ba47d38d | 11314 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); |
3e4093b6 RS |
11315 | return error_mark_node; |
11316 | } | |
400fbf9f | 11317 | |
3e4093b6 | 11318 | if (build_type == NULL_TREE) |
8ce94e44 JM |
11319 | { |
11320 | build_type = result_type; | |
180f8dbb JM |
11321 | if ((type0 != orig_type0 || type1 != orig_type1) |
11322 | && !boolean_op) | |
8ce94e44 JM |
11323 | { |
11324 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 11325 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
8ce94e44 JM |
11326 | } |
11327 | } | |
400fbf9f | 11328 | |
2d2e923f MLI |
11329 | if (!converted) |
11330 | { | |
68fca595 MP |
11331 | op0 = ep_convert_and_check (location, result_type, op0, |
11332 | semantic_result_type); | |
11333 | op1 = ep_convert_and_check (location, result_type, op1, | |
11334 | semantic_result_type); | |
2d2e923f MLI |
11335 | |
11336 | /* This can happen if one operand has a vector type, and the other | |
11337 | has a different type. */ | |
11338 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
11339 | return error_mark_node; | |
11340 | } | |
11341 | ||
f8ed5150 MP |
11342 | if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE |
11343 | | SANITIZE_FLOAT_DIVIDE)) | |
802ac282 | 11344 | && do_ubsan_in_current_function () |
b2aaf235 MP |
11345 | && (doing_div_or_mod || doing_shift) |
11346 | && !require_constant_value) | |
de5a5fa1 MP |
11347 | { |
11348 | /* OP0 and/or OP1 might have side-effects. */ | |
11349 | op0 = c_save_expr (op0); | |
11350 | op1 = c_save_expr (op1); | |
11351 | op0 = c_fully_fold (op0, false, NULL); | |
11352 | op1 = c_fully_fold (op1, false, NULL); | |
f8ed5150 MP |
11353 | if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE |
11354 | | SANITIZE_FLOAT_DIVIDE))) | |
de5a5fa1 | 11355 | instrument_expr = ubsan_instrument_division (location, op0, op1); |
a24d975c | 11356 | else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT)) |
de5a5fa1 MP |
11357 | instrument_expr = ubsan_instrument_shift (location, code, op0, op1); |
11358 | } | |
11359 | ||
c9f9eb5d | 11360 | /* Treat expressions in initializers specially as they can't trap. */ |
928c19bb JM |
11361 | if (int_const_or_overflow) |
11362 | ret = (require_constant_value | |
db3927fb AH |
11363 | ? fold_build2_initializer_loc (location, resultcode, build_type, |
11364 | op0, op1) | |
11365 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
928c19bb JM |
11366 | else |
11367 | ret = build2 (resultcode, build_type, op0, op1); | |
c9f9eb5d AH |
11368 | if (final_type != 0) |
11369 | ret = convert (final_type, ret); | |
11370 | ||
11371 | return_build_binary_op: | |
11372 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
11373 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) |
11374 | ret = (int_operands | |
11375 | ? note_integer_operands (ret) | |
11376 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
11377 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
11378 | && !in_late_binary_op) | |
11379 | ret = note_integer_operands (ret); | |
2d2e923f MLI |
11380 | if (semantic_result_type) |
11381 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
c9f9eb5d | 11382 | protected_set_expr_location (ret, location); |
de5a5fa1 | 11383 | |
a24d975c | 11384 | if (instrument_expr != NULL) |
de5a5fa1 MP |
11385 | ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret), |
11386 | instrument_expr, ret); | |
11387 | ||
c9f9eb5d | 11388 | return ret; |
400fbf9f | 11389 | } |
85498824 JM |
11390 | |
11391 | ||
11392 | /* Convert EXPR to be a truth-value, validating its type for this | |
ba47d38d | 11393 | purpose. LOCATION is the source location for the expression. */ |
85498824 JM |
11394 | |
11395 | tree | |
ba47d38d | 11396 | c_objc_common_truthvalue_conversion (location_t location, tree expr) |
85498824 | 11397 | { |
928c19bb JM |
11398 | bool int_const, int_operands; |
11399 | ||
85498824 JM |
11400 | switch (TREE_CODE (TREE_TYPE (expr))) |
11401 | { | |
11402 | case ARRAY_TYPE: | |
ba47d38d | 11403 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); |
85498824 JM |
11404 | return error_mark_node; |
11405 | ||
11406 | case RECORD_TYPE: | |
ba47d38d | 11407 | error_at (location, "used struct type value where scalar is required"); |
85498824 JM |
11408 | return error_mark_node; |
11409 | ||
11410 | case UNION_TYPE: | |
ba47d38d | 11411 | error_at (location, "used union type value where scalar is required"); |
85498824 JM |
11412 | return error_mark_node; |
11413 | ||
04af8788 NP |
11414 | case VOID_TYPE: |
11415 | error_at (location, "void value not ignored as it ought to be"); | |
11416 | return error_mark_node; | |
11417 | ||
1807ffc1 MS |
11418 | case POINTER_TYPE: |
11419 | if (reject_gcc_builtin (expr)) | |
11420 | return error_mark_node; | |
11421 | break; | |
11422 | ||
46bdb9cf JM |
11423 | case FUNCTION_TYPE: |
11424 | gcc_unreachable (); | |
11425 | ||
d246ab4f AS |
11426 | case VECTOR_TYPE: |
11427 | error_at (location, "used vector type where scalar is required"); | |
11428 | return error_mark_node; | |
11429 | ||
85498824 JM |
11430 | default: |
11431 | break; | |
11432 | } | |
11433 | ||
928c19bb JM |
11434 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); |
11435 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
a27d595d JM |
11436 | if (int_operands && TREE_CODE (expr) != INTEGER_CST) |
11437 | { | |
11438 | expr = remove_c_maybe_const_expr (expr); | |
11439 | expr = build2 (NE_EXPR, integer_type_node, expr, | |
11440 | convert (TREE_TYPE (expr), integer_zero_node)); | |
11441 | expr = note_integer_operands (expr); | |
11442 | } | |
11443 | else | |
11444 | /* ??? Should we also give an error for vectors rather than leaving | |
11445 | those to give errors later? */ | |
11446 | expr = c_common_truthvalue_conversion (location, expr); | |
928c19bb JM |
11447 | |
11448 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
11449 | { | |
11450 | if (TREE_OVERFLOW (expr)) | |
11451 | return expr; | |
11452 | else | |
11453 | return note_integer_operands (expr); | |
11454 | } | |
11455 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
11456 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
11457 | return expr; | |
85498824 | 11458 | } |
73f397d4 JM |
11459 | \f |
11460 | ||
11461 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
11462 | required. */ | |
11463 | ||
11464 | tree | |
51eed280 | 11465 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) |
73f397d4 JM |
11466 | { |
11467 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
11468 | { | |
11469 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
11470 | /* Executing a compound literal inside a function reinitializes | |
11471 | it. */ | |
11472 | if (!TREE_STATIC (decl)) | |
11473 | *se = true; | |
11474 | return decl; | |
11475 | } | |
11476 | else | |
11477 | return expr; | |
11478 | } | |
953ff289 | 11479 | \f |
88bae6f4 TS |
11480 | /* Generate OMP construct CODE, with BODY and CLAUSES as its compound |
11481 | statement. LOC is the location of the construct. */ | |
41dbbb37 TS |
11482 | |
11483 | tree | |
88bae6f4 TS |
11484 | c_finish_omp_construct (location_t loc, enum tree_code code, tree body, |
11485 | tree clauses) | |
41dbbb37 | 11486 | { |
88bae6f4 | 11487 | body = c_end_compound_stmt (loc, body, true); |
41dbbb37 | 11488 | |
88bae6f4 | 11489 | tree stmt = make_node (code); |
41dbbb37 | 11490 | TREE_TYPE (stmt) = void_type_node; |
88bae6f4 TS |
11491 | OMP_BODY (stmt) = body; |
11492 | OMP_CLAUSES (stmt) = clauses; | |
41dbbb37 TS |
11493 | SET_EXPR_LOCATION (stmt, loc); |
11494 | ||
11495 | return add_stmt (stmt); | |
11496 | } | |
11497 | ||
11498 | /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound | |
11499 | statement. LOC is the location of the OACC_DATA. */ | |
11500 | ||
11501 | tree | |
11502 | c_finish_oacc_data (location_t loc, tree clauses, tree block) | |
11503 | { | |
11504 | tree stmt; | |
11505 | ||
11506 | block = c_end_compound_stmt (loc, block, true); | |
11507 | ||
11508 | stmt = make_node (OACC_DATA); | |
11509 | TREE_TYPE (stmt) = void_type_node; | |
11510 | OACC_DATA_CLAUSES (stmt) = clauses; | |
11511 | OACC_DATA_BODY (stmt) = block; | |
11512 | SET_EXPR_LOCATION (stmt, loc); | |
11513 | ||
11514 | return add_stmt (stmt); | |
11515 | } | |
11516 | ||
c0220ea4 | 11517 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
953ff289 DN |
11518 | |
11519 | tree | |
11520 | c_begin_omp_parallel (void) | |
11521 | { | |
11522 | tree block; | |
11523 | ||
11524 | keep_next_level (); | |
11525 | block = c_begin_compound_stmt (true); | |
11526 | ||
11527 | return block; | |
11528 | } | |
11529 | ||
c2255bc4 AH |
11530 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound |
11531 | statement. LOC is the location of the OMP_PARALLEL. */ | |
a68ab351 | 11532 | |
953ff289 | 11533 | tree |
c2255bc4 | 11534 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) |
953ff289 DN |
11535 | { |
11536 | tree stmt; | |
11537 | ||
c2255bc4 | 11538 | block = c_end_compound_stmt (loc, block, true); |
953ff289 DN |
11539 | |
11540 | stmt = make_node (OMP_PARALLEL); | |
11541 | TREE_TYPE (stmt) = void_type_node; | |
11542 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
11543 | OMP_PARALLEL_BODY (stmt) = block; | |
c2255bc4 | 11544 | SET_EXPR_LOCATION (stmt, loc); |
953ff289 DN |
11545 | |
11546 | return add_stmt (stmt); | |
11547 | } | |
11548 | ||
a68ab351 JJ |
11549 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
11550 | ||
11551 | tree | |
11552 | c_begin_omp_task (void) | |
11553 | { | |
11554 | tree block; | |
11555 | ||
11556 | keep_next_level (); | |
11557 | block = c_begin_compound_stmt (true); | |
11558 | ||
11559 | return block; | |
11560 | } | |
11561 | ||
c2255bc4 AH |
11562 | /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound |
11563 | statement. LOC is the location of the #pragma. */ | |
a68ab351 JJ |
11564 | |
11565 | tree | |
c2255bc4 | 11566 | c_finish_omp_task (location_t loc, tree clauses, tree block) |
a68ab351 JJ |
11567 | { |
11568 | tree stmt; | |
11569 | ||
c2255bc4 | 11570 | block = c_end_compound_stmt (loc, block, true); |
a68ab351 JJ |
11571 | |
11572 | stmt = make_node (OMP_TASK); | |
11573 | TREE_TYPE (stmt) = void_type_node; | |
11574 | OMP_TASK_CLAUSES (stmt) = clauses; | |
11575 | OMP_TASK_BODY (stmt) = block; | |
c2255bc4 | 11576 | SET_EXPR_LOCATION (stmt, loc); |
a68ab351 JJ |
11577 | |
11578 | return add_stmt (stmt); | |
11579 | } | |
11580 | ||
acf0174b JJ |
11581 | /* Generate GOMP_cancel call for #pragma omp cancel. */ |
11582 | ||
11583 | void | |
11584 | c_finish_omp_cancel (location_t loc, tree clauses) | |
11585 | { | |
11586 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL); | |
11587 | int mask = 0; | |
11588 | if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) | |
11589 | mask = 1; | |
11590 | else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) | |
11591 | mask = 2; | |
11592 | else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) | |
11593 | mask = 4; | |
11594 | else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) | |
11595 | mask = 8; | |
11596 | else | |
11597 | { | |
11598 | error_at (loc, "%<#pragma omp cancel must specify one of " | |
11599 | "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " | |
11600 | "clauses"); | |
11601 | return; | |
11602 | } | |
11603 | tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF); | |
11604 | if (ifc != NULL_TREE) | |
11605 | { | |
11606 | tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc)); | |
11607 | ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR, | |
11608 | boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc), | |
11609 | build_zero_cst (type)); | |
11610 | } | |
11611 | else | |
11612 | ifc = boolean_true_node; | |
11613 | tree stmt = build_call_expr_loc (loc, fn, 2, | |
11614 | build_int_cst (integer_type_node, mask), | |
11615 | ifc); | |
11616 | add_stmt (stmt); | |
11617 | } | |
11618 | ||
11619 | /* Generate GOMP_cancellation_point call for | |
11620 | #pragma omp cancellation point. */ | |
11621 | ||
11622 | void | |
11623 | c_finish_omp_cancellation_point (location_t loc, tree clauses) | |
11624 | { | |
11625 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT); | |
11626 | int mask = 0; | |
11627 | if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) | |
11628 | mask = 1; | |
11629 | else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) | |
11630 | mask = 2; | |
11631 | else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) | |
11632 | mask = 4; | |
11633 | else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) | |
11634 | mask = 8; | |
11635 | else | |
11636 | { | |
11637 | error_at (loc, "%<#pragma omp cancellation point must specify one of " | |
11638 | "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " | |
11639 | "clauses"); | |
11640 | return; | |
11641 | } | |
11642 | tree stmt = build_call_expr_loc (loc, fn, 1, | |
11643 | build_int_cst (integer_type_node, mask)); | |
11644 | add_stmt (stmt); | |
11645 | } | |
11646 | ||
11647 | /* Helper function for handle_omp_array_sections. Called recursively | |
11648 | to handle multiple array-section-subscripts. C is the clause, | |
11649 | T current expression (initially OMP_CLAUSE_DECL), which is either | |
11650 | a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound | |
11651 | expression if specified, TREE_VALUE length expression if specified, | |
11652 | TREE_CHAIN is what it has been specified after, or some decl. | |
11653 | TYPES vector is populated with array section types, MAYBE_ZERO_LEN | |
11654 | set to true if any of the array-section-subscript could have length | |
11655 | of zero (explicit or implicit), FIRST_NON_ONE is the index of the | |
11656 | first array-section-subscript which is known not to have length | |
11657 | of one. Given say: | |
11658 | map(a[:b][2:1][:c][:2][:d][e:f][2:5]) | |
11659 | FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c] | |
11660 | all are or may have length of 1, array-section-subscript [:2] is the | |
d9a6bd32 | 11661 | first one known not to have length 1. For array-section-subscript |
acf0174b JJ |
11662 | <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't |
11663 | 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we | |
11664 | can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above | |
11665 | case though, as some lengths could be zero. */ | |
11666 | ||
11667 | static tree | |
11668 | handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types, | |
d9a6bd32 JJ |
11669 | bool &maybe_zero_len, unsigned int &first_non_one, |
11670 | bool is_omp) | |
acf0174b JJ |
11671 | { |
11672 | tree ret, low_bound, length, type; | |
11673 | if (TREE_CODE (t) != TREE_LIST) | |
11674 | { | |
7a0ca710 | 11675 | if (error_operand_p (t)) |
acf0174b | 11676 | return error_mark_node; |
d9a6bd32 JJ |
11677 | ret = t; |
11678 | if (TREE_CODE (t) == COMPONENT_REF | |
11679 | && is_omp | |
11680 | && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP | |
11681 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO | |
11682 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)) | |
11683 | { | |
11684 | if (DECL_BIT_FIELD (TREE_OPERAND (t, 1))) | |
11685 | { | |
11686 | error_at (OMP_CLAUSE_LOCATION (c), | |
11687 | "bit-field %qE in %qs clause", | |
11688 | t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11689 | return error_mark_node; | |
11690 | } | |
11691 | while (TREE_CODE (t) == COMPONENT_REF) | |
11692 | { | |
11693 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE) | |
11694 | { | |
11695 | error_at (OMP_CLAUSE_LOCATION (c), | |
11696 | "%qE is a member of a union", t); | |
11697 | return error_mark_node; | |
11698 | } | |
11699 | t = TREE_OPERAND (t, 0); | |
11700 | } | |
11701 | } | |
0ae9bd27 | 11702 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
acf0174b JJ |
11703 | { |
11704 | if (DECL_P (t)) | |
11705 | error_at (OMP_CLAUSE_LOCATION (c), | |
11706 | "%qD is not a variable in %qs clause", t, | |
11707 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11708 | else | |
11709 | error_at (OMP_CLAUSE_LOCATION (c), | |
11710 | "%qE is not a variable in %qs clause", t, | |
11711 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11712 | return error_mark_node; | |
11713 | } | |
11714 | else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND | |
0ae9bd27 | 11715 | && VAR_P (t) && DECL_THREAD_LOCAL_P (t)) |
acf0174b JJ |
11716 | { |
11717 | error_at (OMP_CLAUSE_LOCATION (c), | |
11718 | "%qD is threadprivate variable in %qs clause", t, | |
11719 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11720 | return error_mark_node; | |
11721 | } | |
d9a6bd32 | 11722 | return ret; |
acf0174b JJ |
11723 | } |
11724 | ||
11725 | ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types, | |
d9a6bd32 | 11726 | maybe_zero_len, first_non_one, is_omp); |
acf0174b JJ |
11727 | if (ret == error_mark_node || ret == NULL_TREE) |
11728 | return ret; | |
11729 | ||
11730 | type = TREE_TYPE (ret); | |
11731 | low_bound = TREE_PURPOSE (t); | |
11732 | length = TREE_VALUE (t); | |
11733 | ||
11734 | if (low_bound == error_mark_node || length == error_mark_node) | |
11735 | return error_mark_node; | |
11736 | ||
11737 | if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound))) | |
11738 | { | |
11739 | error_at (OMP_CLAUSE_LOCATION (c), | |
11740 | "low bound %qE of array section does not have integral type", | |
11741 | low_bound); | |
11742 | return error_mark_node; | |
11743 | } | |
11744 | if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length))) | |
11745 | { | |
11746 | error_at (OMP_CLAUSE_LOCATION (c), | |
11747 | "length %qE of array section does not have integral type", | |
11748 | length); | |
11749 | return error_mark_node; | |
11750 | } | |
11751 | if (low_bound | |
11752 | && TREE_CODE (low_bound) == INTEGER_CST | |
11753 | && TYPE_PRECISION (TREE_TYPE (low_bound)) | |
11754 | > TYPE_PRECISION (sizetype)) | |
11755 | low_bound = fold_convert (sizetype, low_bound); | |
11756 | if (length | |
11757 | && TREE_CODE (length) == INTEGER_CST | |
11758 | && TYPE_PRECISION (TREE_TYPE (length)) | |
11759 | > TYPE_PRECISION (sizetype)) | |
11760 | length = fold_convert (sizetype, length); | |
11761 | if (low_bound == NULL_TREE) | |
11762 | low_bound = integer_zero_node; | |
11763 | ||
11764 | if (length != NULL_TREE) | |
11765 | { | |
11766 | if (!integer_nonzerop (length)) | |
d9a6bd32 JJ |
11767 | { |
11768 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND | |
11769 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION) | |
11770 | { | |
11771 | if (integer_zerop (length)) | |
11772 | { | |
11773 | error_at (OMP_CLAUSE_LOCATION (c), | |
11774 | "zero length array section in %qs clause", | |
11775 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11776 | return error_mark_node; | |
11777 | } | |
11778 | } | |
11779 | else | |
11780 | maybe_zero_len = true; | |
11781 | } | |
acf0174b JJ |
11782 | if (first_non_one == types.length () |
11783 | && (TREE_CODE (length) != INTEGER_CST || integer_onep (length))) | |
11784 | first_non_one++; | |
11785 | } | |
d9a6bd32 JJ |
11786 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION |
11787 | && !integer_zerop (low_bound)) | |
11788 | { | |
11789 | error_at (OMP_CLAUSE_LOCATION (c), | |
11790 | "%<reduction%> array section has to be zero-based"); | |
11791 | return error_mark_node; | |
11792 | } | |
acf0174b JJ |
11793 | if (TREE_CODE (type) == ARRAY_TYPE) |
11794 | { | |
11795 | if (length == NULL_TREE | |
11796 | && (TYPE_DOMAIN (type) == NULL_TREE | |
11797 | || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)) | |
11798 | { | |
11799 | error_at (OMP_CLAUSE_LOCATION (c), | |
11800 | "for unknown bound array type length expression must " | |
11801 | "be specified"); | |
11802 | return error_mark_node; | |
11803 | } | |
11804 | if (TREE_CODE (low_bound) == INTEGER_CST | |
11805 | && tree_int_cst_sgn (low_bound) == -1) | |
11806 | { | |
11807 | error_at (OMP_CLAUSE_LOCATION (c), | |
11808 | "negative low bound in array section in %qs clause", | |
11809 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11810 | return error_mark_node; | |
11811 | } | |
11812 | if (length != NULL_TREE | |
11813 | && TREE_CODE (length) == INTEGER_CST | |
11814 | && tree_int_cst_sgn (length) == -1) | |
11815 | { | |
11816 | error_at (OMP_CLAUSE_LOCATION (c), | |
11817 | "negative length in array section in %qs clause", | |
11818 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11819 | return error_mark_node; | |
11820 | } | |
11821 | if (TYPE_DOMAIN (type) | |
11822 | && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) | |
11823 | && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) | |
11824 | == INTEGER_CST) | |
11825 | { | |
11826 | tree size = size_binop (PLUS_EXPR, | |
11827 | TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
11828 | size_one_node); | |
11829 | if (TREE_CODE (low_bound) == INTEGER_CST) | |
11830 | { | |
11831 | if (tree_int_cst_lt (size, low_bound)) | |
11832 | { | |
11833 | error_at (OMP_CLAUSE_LOCATION (c), | |
11834 | "low bound %qE above array section size " | |
11835 | "in %qs clause", low_bound, | |
11836 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11837 | return error_mark_node; | |
11838 | } | |
11839 | if (tree_int_cst_equal (size, low_bound)) | |
d9a6bd32 JJ |
11840 | { |
11841 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND | |
11842 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION) | |
11843 | { | |
11844 | error_at (OMP_CLAUSE_LOCATION (c), | |
11845 | "zero length array section in %qs clause", | |
11846 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11847 | return error_mark_node; | |
11848 | } | |
11849 | maybe_zero_len = true; | |
11850 | } | |
acf0174b JJ |
11851 | else if (length == NULL_TREE |
11852 | && first_non_one == types.length () | |
11853 | && tree_int_cst_equal | |
11854 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
11855 | low_bound)) | |
11856 | first_non_one++; | |
11857 | } | |
11858 | else if (length == NULL_TREE) | |
11859 | { | |
d9a6bd32 JJ |
11860 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND |
11861 | && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION) | |
11862 | maybe_zero_len = true; | |
acf0174b JJ |
11863 | if (first_non_one == types.length ()) |
11864 | first_non_one++; | |
11865 | } | |
11866 | if (length && TREE_CODE (length) == INTEGER_CST) | |
11867 | { | |
11868 | if (tree_int_cst_lt (size, length)) | |
11869 | { | |
11870 | error_at (OMP_CLAUSE_LOCATION (c), | |
11871 | "length %qE above array section size " | |
11872 | "in %qs clause", length, | |
11873 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11874 | return error_mark_node; | |
11875 | } | |
11876 | if (TREE_CODE (low_bound) == INTEGER_CST) | |
11877 | { | |
11878 | tree lbpluslen | |
11879 | = size_binop (PLUS_EXPR, | |
11880 | fold_convert (sizetype, low_bound), | |
11881 | fold_convert (sizetype, length)); | |
11882 | if (TREE_CODE (lbpluslen) == INTEGER_CST | |
11883 | && tree_int_cst_lt (size, lbpluslen)) | |
11884 | { | |
11885 | error_at (OMP_CLAUSE_LOCATION (c), | |
11886 | "high bound %qE above array section size " | |
11887 | "in %qs clause", lbpluslen, | |
11888 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11889 | return error_mark_node; | |
11890 | } | |
11891 | } | |
11892 | } | |
11893 | } | |
11894 | else if (length == NULL_TREE) | |
11895 | { | |
d9a6bd32 JJ |
11896 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND |
11897 | && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION) | |
11898 | maybe_zero_len = true; | |
acf0174b JJ |
11899 | if (first_non_one == types.length ()) |
11900 | first_non_one++; | |
11901 | } | |
11902 | ||
11903 | /* For [lb:] we will need to evaluate lb more than once. */ | |
11904 | if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) | |
11905 | { | |
11906 | tree lb = c_save_expr (low_bound); | |
11907 | if (lb != low_bound) | |
11908 | { | |
11909 | TREE_PURPOSE (t) = lb; | |
11910 | low_bound = lb; | |
11911 | } | |
11912 | } | |
11913 | } | |
11914 | else if (TREE_CODE (type) == POINTER_TYPE) | |
11915 | { | |
11916 | if (length == NULL_TREE) | |
11917 | { | |
11918 | error_at (OMP_CLAUSE_LOCATION (c), | |
11919 | "for pointer type length expression must be specified"); | |
11920 | return error_mark_node; | |
11921 | } | |
d9a6bd32 JJ |
11922 | if (length != NULL_TREE |
11923 | && TREE_CODE (length) == INTEGER_CST | |
11924 | && tree_int_cst_sgn (length) == -1) | |
11925 | { | |
11926 | error_at (OMP_CLAUSE_LOCATION (c), | |
11927 | "negative length in array section in %qs clause", | |
11928 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11929 | return error_mark_node; | |
11930 | } | |
acf0174b JJ |
11931 | /* If there is a pointer type anywhere but in the very first |
11932 | array-section-subscript, the array section can't be contiguous. */ | |
11933 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND | |
11934 | && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST) | |
11935 | { | |
11936 | error_at (OMP_CLAUSE_LOCATION (c), | |
11937 | "array section is not contiguous in %qs clause", | |
11938 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11939 | return error_mark_node; | |
11940 | } | |
11941 | } | |
11942 | else | |
11943 | { | |
11944 | error_at (OMP_CLAUSE_LOCATION (c), | |
11945 | "%qE does not have pointer or array type", ret); | |
11946 | return error_mark_node; | |
11947 | } | |
11948 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) | |
11949 | types.safe_push (TREE_TYPE (ret)); | |
11950 | /* We will need to evaluate lb more than once. */ | |
11951 | tree lb = c_save_expr (low_bound); | |
11952 | if (lb != low_bound) | |
11953 | { | |
11954 | TREE_PURPOSE (t) = lb; | |
11955 | low_bound = lb; | |
11956 | } | |
11957 | ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound); | |
11958 | return ret; | |
11959 | } | |
11960 | ||
11961 | /* Handle array sections for clause C. */ | |
11962 | ||
11963 | static bool | |
d9a6bd32 | 11964 | handle_omp_array_sections (tree c, bool is_omp) |
acf0174b JJ |
11965 | { |
11966 | bool maybe_zero_len = false; | |
11967 | unsigned int first_non_one = 0; | |
d9a6bd32 | 11968 | auto_vec<tree, 10> types; |
acf0174b | 11969 | tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types, |
d9a6bd32 JJ |
11970 | maybe_zero_len, first_non_one, |
11971 | is_omp); | |
acf0174b | 11972 | if (first == error_mark_node) |
d9a6bd32 | 11973 | return true; |
acf0174b | 11974 | if (first == NULL_TREE) |
d9a6bd32 | 11975 | return false; |
acf0174b JJ |
11976 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND) |
11977 | { | |
11978 | tree t = OMP_CLAUSE_DECL (c); | |
11979 | tree tem = NULL_TREE; | |
acf0174b JJ |
11980 | /* Need to evaluate side effects in the length expressions |
11981 | if any. */ | |
11982 | while (TREE_CODE (t) == TREE_LIST) | |
11983 | { | |
11984 | if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t))) | |
11985 | { | |
11986 | if (tem == NULL_TREE) | |
11987 | tem = TREE_VALUE (t); | |
11988 | else | |
11989 | tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), | |
11990 | TREE_VALUE (t), tem); | |
11991 | } | |
11992 | t = TREE_CHAIN (t); | |
11993 | } | |
11994 | if (tem) | |
11995 | first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first); | |
11996 | first = c_fully_fold (first, false, NULL); | |
11997 | OMP_CLAUSE_DECL (c) = first; | |
11998 | } | |
11999 | else | |
12000 | { | |
12001 | unsigned int num = types.length (), i; | |
12002 | tree t, side_effects = NULL_TREE, size = NULL_TREE; | |
12003 | tree condition = NULL_TREE; | |
12004 | ||
12005 | if (int_size_in_bytes (TREE_TYPE (first)) <= 0) | |
12006 | maybe_zero_len = true; | |
12007 | ||
12008 | for (i = num, t = OMP_CLAUSE_DECL (c); i > 0; | |
12009 | t = TREE_CHAIN (t)) | |
12010 | { | |
12011 | tree low_bound = TREE_PURPOSE (t); | |
12012 | tree length = TREE_VALUE (t); | |
12013 | ||
12014 | i--; | |
12015 | if (low_bound | |
12016 | && TREE_CODE (low_bound) == INTEGER_CST | |
12017 | && TYPE_PRECISION (TREE_TYPE (low_bound)) | |
12018 | > TYPE_PRECISION (sizetype)) | |
12019 | low_bound = fold_convert (sizetype, low_bound); | |
12020 | if (length | |
12021 | && TREE_CODE (length) == INTEGER_CST | |
12022 | && TYPE_PRECISION (TREE_TYPE (length)) | |
12023 | > TYPE_PRECISION (sizetype)) | |
12024 | length = fold_convert (sizetype, length); | |
12025 | if (low_bound == NULL_TREE) | |
12026 | low_bound = integer_zero_node; | |
12027 | if (!maybe_zero_len && i > first_non_one) | |
12028 | { | |
12029 | if (integer_nonzerop (low_bound)) | |
12030 | goto do_warn_noncontiguous; | |
12031 | if (length != NULL_TREE | |
12032 | && TREE_CODE (length) == INTEGER_CST | |
12033 | && TYPE_DOMAIN (types[i]) | |
12034 | && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])) | |
12035 | && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))) | |
12036 | == INTEGER_CST) | |
12037 | { | |
12038 | tree size; | |
12039 | size = size_binop (PLUS_EXPR, | |
12040 | TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), | |
12041 | size_one_node); | |
12042 | if (!tree_int_cst_equal (length, size)) | |
12043 | { | |
12044 | do_warn_noncontiguous: | |
12045 | error_at (OMP_CLAUSE_LOCATION (c), | |
12046 | "array section is not contiguous in %qs " | |
12047 | "clause", | |
12048 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
acf0174b JJ |
12049 | return true; |
12050 | } | |
12051 | } | |
12052 | if (length != NULL_TREE | |
12053 | && TREE_SIDE_EFFECTS (length)) | |
12054 | { | |
12055 | if (side_effects == NULL_TREE) | |
12056 | side_effects = length; | |
12057 | else | |
12058 | side_effects = build2 (COMPOUND_EXPR, | |
12059 | TREE_TYPE (side_effects), | |
12060 | length, side_effects); | |
12061 | } | |
12062 | } | |
12063 | else | |
12064 | { | |
12065 | tree l; | |
12066 | ||
d9a6bd32 JJ |
12067 | if (i > first_non_one |
12068 | && ((length && integer_nonzerop (length)) | |
12069 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)) | |
acf0174b JJ |
12070 | continue; |
12071 | if (length) | |
12072 | l = fold_convert (sizetype, length); | |
12073 | else | |
12074 | { | |
12075 | l = size_binop (PLUS_EXPR, | |
12076 | TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), | |
12077 | size_one_node); | |
12078 | l = size_binop (MINUS_EXPR, l, | |
12079 | fold_convert (sizetype, low_bound)); | |
12080 | } | |
12081 | if (i > first_non_one) | |
12082 | { | |
12083 | l = fold_build2 (NE_EXPR, boolean_type_node, l, | |
12084 | size_zero_node); | |
12085 | if (condition == NULL_TREE) | |
12086 | condition = l; | |
12087 | else | |
12088 | condition = fold_build2 (BIT_AND_EXPR, boolean_type_node, | |
12089 | l, condition); | |
12090 | } | |
12091 | else if (size == NULL_TREE) | |
12092 | { | |
12093 | size = size_in_bytes (TREE_TYPE (types[i])); | |
d9a6bd32 JJ |
12094 | tree eltype = TREE_TYPE (types[num - 1]); |
12095 | while (TREE_CODE (eltype) == ARRAY_TYPE) | |
12096 | eltype = TREE_TYPE (eltype); | |
12097 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION) | |
12098 | { | |
12099 | if (integer_zerop (size) | |
12100 | || integer_zerop (size_in_bytes (eltype))) | |
12101 | { | |
12102 | error_at (OMP_CLAUSE_LOCATION (c), | |
12103 | "zero length array section in %qs clause", | |
12104 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12105 | return error_mark_node; | |
12106 | } | |
12107 | size = size_binop (EXACT_DIV_EXPR, size, | |
12108 | size_in_bytes (eltype)); | |
12109 | } | |
acf0174b JJ |
12110 | size = size_binop (MULT_EXPR, size, l); |
12111 | if (condition) | |
12112 | size = fold_build3 (COND_EXPR, sizetype, condition, | |
12113 | size, size_zero_node); | |
12114 | } | |
12115 | else | |
12116 | size = size_binop (MULT_EXPR, size, l); | |
12117 | } | |
12118 | } | |
acf0174b JJ |
12119 | if (side_effects) |
12120 | size = build2 (COMPOUND_EXPR, sizetype, side_effects, size); | |
d9a6bd32 JJ |
12121 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION) |
12122 | { | |
12123 | size = size_binop (MINUS_EXPR, size, size_one_node); | |
12124 | size = c_fully_fold (size, false, NULL); | |
12125 | tree index_type = build_index_type (size); | |
12126 | tree eltype = TREE_TYPE (first); | |
12127 | while (TREE_CODE (eltype) == ARRAY_TYPE) | |
12128 | eltype = TREE_TYPE (eltype); | |
12129 | tree type = build_array_type (eltype, index_type); | |
12130 | tree ptype = build_pointer_type (eltype); | |
12131 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
12132 | t = build_fold_addr_expr (t); | |
12133 | t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0)); | |
12134 | OMP_CLAUSE_DECL (c) = t; | |
12135 | return false; | |
12136 | } | |
acf0174b JJ |
12137 | first = c_fully_fold (first, false, NULL); |
12138 | OMP_CLAUSE_DECL (c) = first; | |
12139 | if (size) | |
12140 | size = c_fully_fold (size, false, NULL); | |
12141 | OMP_CLAUSE_SIZE (c) = size; | |
d9a6bd32 JJ |
12142 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP |
12143 | || (TREE_CODE (t) == COMPONENT_REF | |
12144 | && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)) | |
acf0174b | 12145 | return false; |
41dbbb37 | 12146 | gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR); |
d9a6bd32 JJ |
12147 | if (is_omp) |
12148 | switch (OMP_CLAUSE_MAP_KIND (c)) | |
12149 | { | |
12150 | case GOMP_MAP_ALLOC: | |
12151 | case GOMP_MAP_TO: | |
12152 | case GOMP_MAP_FROM: | |
12153 | case GOMP_MAP_TOFROM: | |
12154 | case GOMP_MAP_ALWAYS_TO: | |
12155 | case GOMP_MAP_ALWAYS_FROM: | |
12156 | case GOMP_MAP_ALWAYS_TOFROM: | |
12157 | case GOMP_MAP_RELEASE: | |
12158 | case GOMP_MAP_DELETE: | |
12159 | OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1; | |
12160 | break; | |
12161 | default: | |
12162 | break; | |
12163 | } | |
acf0174b | 12164 | tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP); |
d9a6bd32 JJ |
12165 | OMP_CLAUSE_SET_MAP_KIND (c2, is_omp |
12166 | ? GOMP_MAP_FIRSTPRIVATE_POINTER | |
12167 | : GOMP_MAP_POINTER); | |
12168 | if (!is_omp && !c_mark_addressable (t)) | |
acf0174b JJ |
12169 | return false; |
12170 | OMP_CLAUSE_DECL (c2) = t; | |
12171 | t = build_fold_addr_expr (first); | |
12172 | t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t); | |
12173 | tree ptr = OMP_CLAUSE_DECL (c2); | |
12174 | if (!POINTER_TYPE_P (TREE_TYPE (ptr))) | |
12175 | ptr = build_fold_addr_expr (ptr); | |
12176 | t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
12177 | ptrdiff_type_node, t, | |
12178 | fold_convert_loc (OMP_CLAUSE_LOCATION (c), | |
12179 | ptrdiff_type_node, ptr)); | |
12180 | t = c_fully_fold (t, false, NULL); | |
12181 | OMP_CLAUSE_SIZE (c2) = t; | |
12182 | OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c); | |
12183 | OMP_CLAUSE_CHAIN (c) = c2; | |
12184 | } | |
12185 | return false; | |
12186 | } | |
12187 | ||
12188 | /* Helper function of finish_omp_clauses. Clone STMT as if we were making | |
12189 | an inline call. But, remap | |
12190 | the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER | |
12191 | and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */ | |
12192 | ||
12193 | static tree | |
12194 | c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2, | |
12195 | tree decl, tree placeholder) | |
12196 | { | |
12197 | copy_body_data id; | |
b787e7a2 | 12198 | hash_map<tree, tree> decl_map; |
acf0174b | 12199 | |
b787e7a2 TS |
12200 | decl_map.put (omp_decl1, placeholder); |
12201 | decl_map.put (omp_decl2, decl); | |
acf0174b JJ |
12202 | memset (&id, 0, sizeof (id)); |
12203 | id.src_fn = DECL_CONTEXT (omp_decl1); | |
12204 | id.dst_fn = current_function_decl; | |
12205 | id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn); | |
b787e7a2 | 12206 | id.decl_map = &decl_map; |
acf0174b JJ |
12207 | |
12208 | id.copy_decl = copy_decl_no_change; | |
12209 | id.transform_call_graph_edges = CB_CGE_DUPLICATE; | |
12210 | id.transform_new_cfg = true; | |
12211 | id.transform_return_to_modify = false; | |
12212 | id.transform_lang_insert_block = NULL; | |
12213 | id.eh_lp_nr = 0; | |
12214 | walk_tree (&stmt, copy_tree_body_r, &id, NULL); | |
acf0174b JJ |
12215 | return stmt; |
12216 | } | |
12217 | ||
12218 | /* Helper function of c_finish_omp_clauses, called via walk_tree. | |
12219 | Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */ | |
12220 | ||
12221 | static tree | |
12222 | c_find_omp_placeholder_r (tree *tp, int *, void *data) | |
12223 | { | |
12224 | if (*tp == (tree) data) | |
12225 | return *tp; | |
12226 | return NULL_TREE; | |
12227 | } | |
12228 | ||
41dbbb37 | 12229 | /* For all elements of CLAUSES, validate them against their constraints. |
953ff289 DN |
12230 | Remove any elements from the list that are invalid. */ |
12231 | ||
12232 | tree | |
d9a6bd32 | 12233 | c_finish_omp_clauses (tree clauses, bool is_omp, bool declare_simd) |
953ff289 DN |
12234 | { |
12235 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
d9a6bd32 JJ |
12236 | bitmap_head aligned_head, map_head, map_field_head, generic_field_head; |
12237 | tree c, t, type, *pc; | |
12238 | tree simdlen = NULL_TREE, safelen = NULL_TREE; | |
acf0174b JJ |
12239 | bool branch_seen = false; |
12240 | bool copyprivate_seen = false; | |
12241 | tree *nowait_clause = NULL; | |
953ff289 DN |
12242 | |
12243 | bitmap_obstack_initialize (NULL); | |
12244 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
12245 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
12246 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
acf0174b | 12247 | bitmap_initialize (&aligned_head, &bitmap_default_obstack); |
d9a6bd32 JJ |
12248 | bitmap_initialize (&map_head, &bitmap_default_obstack); |
12249 | bitmap_initialize (&map_field_head, &bitmap_default_obstack); | |
12250 | bitmap_initialize (&generic_field_head, &bitmap_default_obstack); | |
953ff289 DN |
12251 | |
12252 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
12253 | { | |
12254 | bool remove = false; | |
12255 | bool need_complete = false; | |
12256 | bool need_implicitly_determined = false; | |
12257 | ||
aaf46ef9 | 12258 | switch (OMP_CLAUSE_CODE (c)) |
953ff289 DN |
12259 | { |
12260 | case OMP_CLAUSE_SHARED: | |
953ff289 DN |
12261 | need_implicitly_determined = true; |
12262 | goto check_dup_generic; | |
12263 | ||
12264 | case OMP_CLAUSE_PRIVATE: | |
953ff289 DN |
12265 | need_complete = true; |
12266 | need_implicitly_determined = true; | |
12267 | goto check_dup_generic; | |
12268 | ||
12269 | case OMP_CLAUSE_REDUCTION: | |
953ff289 DN |
12270 | need_implicitly_determined = true; |
12271 | t = OMP_CLAUSE_DECL (c); | |
d9a6bd32 JJ |
12272 | if (TREE_CODE (t) == TREE_LIST) |
12273 | { | |
12274 | if (handle_omp_array_sections (c, is_omp)) | |
12275 | { | |
12276 | remove = true; | |
12277 | break; | |
12278 | } | |
12279 | ||
12280 | t = OMP_CLAUSE_DECL (c); | |
12281 | } | |
12282 | t = require_complete_type (t); | |
12283 | if (t == error_mark_node) | |
12284 | { | |
12285 | remove = true; | |
12286 | break; | |
12287 | } | |
12288 | type = TREE_TYPE (t); | |
12289 | if (TREE_CODE (t) == MEM_REF) | |
12290 | type = TREE_TYPE (type); | |
12291 | if (TREE_CODE (type) == ARRAY_TYPE) | |
12292 | { | |
12293 | tree oatype = type; | |
12294 | gcc_assert (TREE_CODE (t) != MEM_REF); | |
12295 | while (TREE_CODE (type) == ARRAY_TYPE) | |
12296 | type = TREE_TYPE (type); | |
12297 | if (integer_zerop (TYPE_SIZE_UNIT (type))) | |
12298 | { | |
12299 | error_at (OMP_CLAUSE_LOCATION (c), | |
12300 | "%qD in %<reduction%> clause is a zero size array", | |
12301 | t); | |
12302 | remove = true; | |
12303 | break; | |
12304 | } | |
12305 | tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype), | |
12306 | TYPE_SIZE_UNIT (type)); | |
12307 | if (integer_zerop (size)) | |
12308 | { | |
12309 | error_at (OMP_CLAUSE_LOCATION (c), | |
12310 | "%qD in %<reduction%> clause is a zero size array", | |
12311 | t); | |
12312 | remove = true; | |
12313 | break; | |
12314 | } | |
12315 | size = size_binop (MINUS_EXPR, size, size_one_node); | |
12316 | tree index_type = build_index_type (size); | |
12317 | tree atype = build_array_type (type, index_type); | |
12318 | tree ptype = build_pointer_type (type); | |
12319 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
12320 | t = build_fold_addr_expr (t); | |
12321 | t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0)); | |
12322 | OMP_CLAUSE_DECL (c) = t; | |
12323 | } | |
acf0174b | 12324 | if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE |
d9a6bd32 JJ |
12325 | && (FLOAT_TYPE_P (type) |
12326 | || TREE_CODE (type) == COMPLEX_TYPE)) | |
953ff289 DN |
12327 | { |
12328 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
12329 | const char *r_name = NULL; | |
12330 | ||
12331 | switch (r_code) | |
12332 | { | |
12333 | case PLUS_EXPR: | |
12334 | case MULT_EXPR: | |
12335 | case MINUS_EXPR: | |
652fea39 | 12336 | break; |
20906c66 | 12337 | case MIN_EXPR: |
d9a6bd32 | 12338 | if (TREE_CODE (type) == COMPLEX_TYPE) |
652fea39 JJ |
12339 | r_name = "min"; |
12340 | break; | |
20906c66 | 12341 | case MAX_EXPR: |
d9a6bd32 | 12342 | if (TREE_CODE (type) == COMPLEX_TYPE) |
652fea39 | 12343 | r_name = "max"; |
953ff289 DN |
12344 | break; |
12345 | case BIT_AND_EXPR: | |
12346 | r_name = "&"; | |
12347 | break; | |
12348 | case BIT_XOR_EXPR: | |
12349 | r_name = "^"; | |
12350 | break; | |
12351 | case BIT_IOR_EXPR: | |
12352 | r_name = "|"; | |
12353 | break; | |
12354 | case TRUTH_ANDIF_EXPR: | |
d9a6bd32 | 12355 | if (FLOAT_TYPE_P (type)) |
652fea39 | 12356 | r_name = "&&"; |
953ff289 DN |
12357 | break; |
12358 | case TRUTH_ORIF_EXPR: | |
d9a6bd32 | 12359 | if (FLOAT_TYPE_P (type)) |
652fea39 | 12360 | r_name = "||"; |
953ff289 DN |
12361 | break; |
12362 | default: | |
12363 | gcc_unreachable (); | |
12364 | } | |
12365 | if (r_name) | |
12366 | { | |
c2255bc4 AH |
12367 | error_at (OMP_CLAUSE_LOCATION (c), |
12368 | "%qE has invalid type for %<reduction(%s)%>", | |
12369 | t, r_name); | |
953ff289 | 12370 | remove = true; |
ee1d5a02 | 12371 | break; |
953ff289 DN |
12372 | } |
12373 | } | |
acf0174b JJ |
12374 | else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node) |
12375 | { | |
12376 | error_at (OMP_CLAUSE_LOCATION (c), | |
d9a6bd32 | 12377 | "user defined reduction not found for %qE", t); |
acf0174b | 12378 | remove = true; |
ee1d5a02 | 12379 | break; |
acf0174b JJ |
12380 | } |
12381 | else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)) | |
12382 | { | |
12383 | tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c); | |
d9a6bd32 | 12384 | type = TYPE_MAIN_VARIANT (type); |
acf0174b JJ |
12385 | tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c), |
12386 | VAR_DECL, NULL_TREE, type); | |
d9a6bd32 | 12387 | tree decl_placeholder = NULL_TREE; |
acf0174b JJ |
12388 | OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder; |
12389 | DECL_ARTIFICIAL (placeholder) = 1; | |
12390 | DECL_IGNORED_P (placeholder) = 1; | |
d9a6bd32 JJ |
12391 | if (TREE_CODE (t) == MEM_REF) |
12392 | { | |
12393 | decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c), | |
12394 | VAR_DECL, NULL_TREE, type); | |
12395 | OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder; | |
12396 | DECL_ARTIFICIAL (decl_placeholder) = 1; | |
12397 | DECL_IGNORED_P (decl_placeholder) = 1; | |
12398 | } | |
acf0174b JJ |
12399 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0))) |
12400 | c_mark_addressable (placeholder); | |
12401 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1))) | |
d9a6bd32 JJ |
12402 | c_mark_addressable (decl_placeholder ? decl_placeholder |
12403 | : OMP_CLAUSE_DECL (c)); | |
acf0174b JJ |
12404 | OMP_CLAUSE_REDUCTION_MERGE (c) |
12405 | = c_clone_omp_udr (TREE_VEC_ELT (list, 2), | |
12406 | TREE_VEC_ELT (list, 0), | |
12407 | TREE_VEC_ELT (list, 1), | |
d9a6bd32 JJ |
12408 | decl_placeholder ? decl_placeholder |
12409 | : OMP_CLAUSE_DECL (c), placeholder); | |
acf0174b JJ |
12410 | OMP_CLAUSE_REDUCTION_MERGE (c) |
12411 | = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, | |
12412 | void_type_node, NULL_TREE, | |
d9a6bd32 | 12413 | OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE); |
acf0174b JJ |
12414 | TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1; |
12415 | if (TREE_VEC_LENGTH (list) == 6) | |
12416 | { | |
12417 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3))) | |
d9a6bd32 JJ |
12418 | c_mark_addressable (decl_placeholder ? decl_placeholder |
12419 | : OMP_CLAUSE_DECL (c)); | |
acf0174b JJ |
12420 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4))) |
12421 | c_mark_addressable (placeholder); | |
12422 | tree init = TREE_VEC_ELT (list, 5); | |
12423 | if (init == error_mark_node) | |
12424 | init = DECL_INITIAL (TREE_VEC_ELT (list, 3)); | |
12425 | OMP_CLAUSE_REDUCTION_INIT (c) | |
12426 | = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4), | |
12427 | TREE_VEC_ELT (list, 3), | |
d9a6bd32 JJ |
12428 | decl_placeholder ? decl_placeholder |
12429 | : OMP_CLAUSE_DECL (c), placeholder); | |
acf0174b | 12430 | if (TREE_VEC_ELT (list, 5) == error_mark_node) |
d9a6bd32 JJ |
12431 | { |
12432 | tree v = decl_placeholder ? decl_placeholder : t; | |
12433 | OMP_CLAUSE_REDUCTION_INIT (c) | |
12434 | = build2 (INIT_EXPR, TREE_TYPE (v), v, | |
12435 | OMP_CLAUSE_REDUCTION_INIT (c)); | |
12436 | } | |
acf0174b JJ |
12437 | if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c), |
12438 | c_find_omp_placeholder_r, | |
12439 | placeholder, NULL)) | |
12440 | OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1; | |
12441 | } | |
12442 | else | |
12443 | { | |
12444 | tree init; | |
d9a6bd32 JJ |
12445 | tree v = decl_placeholder ? decl_placeholder : t; |
12446 | if (AGGREGATE_TYPE_P (TREE_TYPE (v))) | |
12447 | init = build_constructor (TREE_TYPE (v), NULL); | |
acf0174b | 12448 | else |
d9a6bd32 | 12449 | init = fold_convert (TREE_TYPE (v), integer_zero_node); |
acf0174b | 12450 | OMP_CLAUSE_REDUCTION_INIT (c) |
d9a6bd32 | 12451 | = build2 (INIT_EXPR, TREE_TYPE (v), v, init); |
acf0174b JJ |
12452 | } |
12453 | OMP_CLAUSE_REDUCTION_INIT (c) | |
12454 | = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, | |
12455 | void_type_node, NULL_TREE, | |
12456 | OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE); | |
12457 | TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1; | |
12458 | } | |
d9a6bd32 JJ |
12459 | if (TREE_CODE (t) == MEM_REF) |
12460 | { | |
12461 | if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE | |
12462 | || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))) | |
12463 | != INTEGER_CST) | |
12464 | { | |
12465 | sorry ("variable length element type in array " | |
12466 | "%<reduction%> clause"); | |
12467 | remove = true; | |
12468 | break; | |
12469 | } | |
12470 | t = TREE_OPERAND (t, 0); | |
12471 | if (TREE_CODE (t) == ADDR_EXPR) | |
12472 | t = TREE_OPERAND (t, 0); | |
12473 | } | |
12474 | goto check_dup_generic_t; | |
953ff289 DN |
12475 | |
12476 | case OMP_CLAUSE_COPYPRIVATE: | |
acf0174b JJ |
12477 | copyprivate_seen = true; |
12478 | if (nowait_clause) | |
12479 | { | |
12480 | error_at (OMP_CLAUSE_LOCATION (*nowait_clause), | |
12481 | "%<nowait%> clause must not be used together " | |
12482 | "with %<copyprivate%>"); | |
12483 | *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause); | |
12484 | nowait_clause = NULL; | |
12485 | } | |
953ff289 DN |
12486 | goto check_dup_generic; |
12487 | ||
12488 | case OMP_CLAUSE_COPYIN: | |
953ff289 | 12489 | t = OMP_CLAUSE_DECL (c); |
0ae9bd27 | 12490 | if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t)) |
953ff289 | 12491 | { |
c2255bc4 AH |
12492 | error_at (OMP_CLAUSE_LOCATION (c), |
12493 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
953ff289 | 12494 | remove = true; |
ee1d5a02 | 12495 | break; |
953ff289 DN |
12496 | } |
12497 | goto check_dup_generic; | |
12498 | ||
acf0174b | 12499 | case OMP_CLAUSE_LINEAR: |
d9a6bd32 JJ |
12500 | if (!declare_simd) |
12501 | need_implicitly_determined = true; | |
acf0174b | 12502 | t = OMP_CLAUSE_DECL (c); |
d9a6bd32 JJ |
12503 | if (!declare_simd |
12504 | && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT) | |
12505 | { | |
12506 | error_at (OMP_CLAUSE_LOCATION (c), | |
12507 | "modifier should not be specified in %<linear%> " | |
12508 | "clause on %<simd%> or %<for%> constructs"); | |
12509 | OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT; | |
12510 | } | |
acf0174b JJ |
12511 | if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) |
12512 | && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE) | |
12513 | { | |
12514 | error_at (OMP_CLAUSE_LOCATION (c), | |
12515 | "linear clause applied to non-integral non-pointer " | |
12516 | "variable with type %qT", TREE_TYPE (t)); | |
12517 | remove = true; | |
12518 | break; | |
12519 | } | |
12520 | if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE) | |
12521 | { | |
12522 | tree s = OMP_CLAUSE_LINEAR_STEP (c); | |
12523 | s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR, | |
12524 | OMP_CLAUSE_DECL (c), s); | |
12525 | s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
a8fc2579 RB |
12526 | sizetype, fold_convert (sizetype, s), |
12527 | fold_convert | |
12528 | (sizetype, OMP_CLAUSE_DECL (c))); | |
acf0174b JJ |
12529 | if (s == error_mark_node) |
12530 | s = size_one_node; | |
12531 | OMP_CLAUSE_LINEAR_STEP (c) = s; | |
12532 | } | |
da6f124d JJ |
12533 | else |
12534 | OMP_CLAUSE_LINEAR_STEP (c) | |
12535 | = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c)); | |
acf0174b JJ |
12536 | goto check_dup_generic; |
12537 | ||
953ff289 DN |
12538 | check_dup_generic: |
12539 | t = OMP_CLAUSE_DECL (c); | |
d9a6bd32 | 12540 | check_dup_generic_t: |
0ae9bd27 | 12541 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
953ff289 | 12542 | { |
c2255bc4 | 12543 | error_at (OMP_CLAUSE_LOCATION (c), |
acf0174b JJ |
12544 | "%qE is not a variable in clause %qs", t, |
12545 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
953ff289 DN |
12546 | remove = true; |
12547 | } | |
12548 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12549 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
12550 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
12551 | { | |
c2255bc4 AH |
12552 | error_at (OMP_CLAUSE_LOCATION (c), |
12553 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12554 | remove = true; |
12555 | } | |
12556 | else | |
12557 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
12558 | break; | |
12559 | ||
12560 | case OMP_CLAUSE_FIRSTPRIVATE: | |
953ff289 DN |
12561 | t = OMP_CLAUSE_DECL (c); |
12562 | need_complete = true; | |
12563 | need_implicitly_determined = true; | |
0ae9bd27 | 12564 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
953ff289 | 12565 | { |
c2255bc4 AH |
12566 | error_at (OMP_CLAUSE_LOCATION (c), |
12567 | "%qE is not a variable in clause %<firstprivate%>", t); | |
953ff289 DN |
12568 | remove = true; |
12569 | } | |
12570 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12571 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
12572 | { | |
c2255bc4 AH |
12573 | error_at (OMP_CLAUSE_LOCATION (c), |
12574 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12575 | remove = true; |
12576 | } | |
12577 | else | |
12578 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
12579 | break; | |
12580 | ||
12581 | case OMP_CLAUSE_LASTPRIVATE: | |
953ff289 DN |
12582 | t = OMP_CLAUSE_DECL (c); |
12583 | need_complete = true; | |
12584 | need_implicitly_determined = true; | |
0ae9bd27 | 12585 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
953ff289 | 12586 | { |
c2255bc4 AH |
12587 | error_at (OMP_CLAUSE_LOCATION (c), |
12588 | "%qE is not a variable in clause %<lastprivate%>", t); | |
953ff289 DN |
12589 | remove = true; |
12590 | } | |
12591 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12592 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
12593 | { | |
c2255bc4 AH |
12594 | error_at (OMP_CLAUSE_LOCATION (c), |
12595 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12596 | remove = true; |
12597 | } | |
12598 | else | |
12599 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
12600 | break; | |
12601 | ||
acf0174b JJ |
12602 | case OMP_CLAUSE_ALIGNED: |
12603 | t = OMP_CLAUSE_DECL (c); | |
0ae9bd27 | 12604 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
acf0174b JJ |
12605 | { |
12606 | error_at (OMP_CLAUSE_LOCATION (c), | |
12607 | "%qE is not a variable in %<aligned%> clause", t); | |
12608 | remove = true; | |
12609 | } | |
5a9785fb JJ |
12610 | else if (!POINTER_TYPE_P (TREE_TYPE (t)) |
12611 | && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE) | |
12612 | { | |
12613 | error_at (OMP_CLAUSE_LOCATION (c), | |
12614 | "%qE in %<aligned%> clause is neither a pointer nor " | |
12615 | "an array", t); | |
12616 | remove = true; | |
12617 | } | |
acf0174b JJ |
12618 | else if (bitmap_bit_p (&aligned_head, DECL_UID (t))) |
12619 | { | |
12620 | error_at (OMP_CLAUSE_LOCATION (c), | |
12621 | "%qE appears more than once in %<aligned%> clauses", | |
12622 | t); | |
12623 | remove = true; | |
12624 | } | |
12625 | else | |
12626 | bitmap_set_bit (&aligned_head, DECL_UID (t)); | |
12627 | break; | |
12628 | ||
12629 | case OMP_CLAUSE_DEPEND: | |
12630 | t = OMP_CLAUSE_DECL (c); | |
d9a6bd32 JJ |
12631 | if (t == NULL_TREE) |
12632 | { | |
12633 | gcc_assert (OMP_CLAUSE_DEPEND_KIND (c) | |
12634 | == OMP_CLAUSE_DEPEND_SOURCE); | |
12635 | break; | |
12636 | } | |
12637 | if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK) | |
12638 | { | |
12639 | gcc_assert (TREE_CODE (t) == TREE_LIST); | |
12640 | for (; t; t = TREE_CHAIN (t)) | |
12641 | { | |
12642 | tree decl = TREE_VALUE (t); | |
12643 | if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE) | |
12644 | { | |
12645 | tree offset = TREE_PURPOSE (t); | |
12646 | bool neg = wi::neg_p ((wide_int) offset); | |
12647 | offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset); | |
12648 | tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c), | |
12649 | neg ? MINUS_EXPR : PLUS_EXPR, | |
12650 | decl, offset); | |
12651 | t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
a8fc2579 RB |
12652 | sizetype, |
12653 | fold_convert (sizetype, t2), | |
12654 | fold_convert (sizetype, decl)); | |
d9a6bd32 JJ |
12655 | if (t2 == error_mark_node) |
12656 | { | |
12657 | remove = true; | |
12658 | break; | |
12659 | } | |
12660 | TREE_PURPOSE (t) = t2; | |
12661 | } | |
12662 | } | |
12663 | break; | |
12664 | } | |
acf0174b JJ |
12665 | if (TREE_CODE (t) == TREE_LIST) |
12666 | { | |
d9a6bd32 | 12667 | if (handle_omp_array_sections (c, is_omp)) |
acf0174b JJ |
12668 | remove = true; |
12669 | break; | |
12670 | } | |
12671 | if (t == error_mark_node) | |
12672 | remove = true; | |
0ae9bd27 | 12673 | else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
acf0174b JJ |
12674 | { |
12675 | error_at (OMP_CLAUSE_LOCATION (c), | |
12676 | "%qE is not a variable in %<depend%> clause", t); | |
12677 | remove = true; | |
12678 | } | |
12679 | else if (!c_mark_addressable (t)) | |
12680 | remove = true; | |
12681 | break; | |
12682 | ||
12683 | case OMP_CLAUSE_MAP: | |
12684 | case OMP_CLAUSE_TO: | |
12685 | case OMP_CLAUSE_FROM: | |
41dbbb37 | 12686 | case OMP_CLAUSE__CACHE_: |
acf0174b JJ |
12687 | t = OMP_CLAUSE_DECL (c); |
12688 | if (TREE_CODE (t) == TREE_LIST) | |
12689 | { | |
d9a6bd32 | 12690 | if (handle_omp_array_sections (c, is_omp)) |
acf0174b JJ |
12691 | remove = true; |
12692 | else | |
12693 | { | |
12694 | t = OMP_CLAUSE_DECL (c); | |
b17a8b07 | 12695 | if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) |
acf0174b JJ |
12696 | { |
12697 | error_at (OMP_CLAUSE_LOCATION (c), | |
12698 | "array section does not have mappable type " | |
12699 | "in %qs clause", | |
12700 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12701 | remove = true; | |
12702 | } | |
d9a6bd32 JJ |
12703 | while (TREE_CODE (t) == ARRAY_REF) |
12704 | t = TREE_OPERAND (t, 0); | |
12705 | if (TREE_CODE (t) == COMPONENT_REF | |
12706 | && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
12707 | { | |
12708 | while (TREE_CODE (t) == COMPONENT_REF) | |
12709 | t = TREE_OPERAND (t, 0); | |
12710 | if (bitmap_bit_p (&map_field_head, DECL_UID (t))) | |
12711 | break; | |
12712 | if (bitmap_bit_p (&map_head, DECL_UID (t))) | |
12713 | { | |
12714 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) | |
12715 | error ("%qD appears more than once in motion" | |
12716 | " clauses", t); | |
12717 | else | |
12718 | error ("%qD appears more than once in map" | |
12719 | " clauses", t); | |
12720 | remove = true; | |
12721 | } | |
12722 | else | |
12723 | { | |
12724 | bitmap_set_bit (&map_head, DECL_UID (t)); | |
12725 | bitmap_set_bit (&map_field_head, DECL_UID (t)); | |
12726 | } | |
12727 | } | |
acf0174b JJ |
12728 | } |
12729 | break; | |
12730 | } | |
12731 | if (t == error_mark_node) | |
d9a6bd32 JJ |
12732 | { |
12733 | remove = true; | |
12734 | break; | |
12735 | } | |
12736 | if (TREE_CODE (t) == COMPONENT_REF | |
12737 | && is_omp | |
12738 | && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_) | |
12739 | { | |
12740 | if (DECL_BIT_FIELD (TREE_OPERAND (t, 1))) | |
12741 | { | |
12742 | error_at (OMP_CLAUSE_LOCATION (c), | |
12743 | "bit-field %qE in %qs clause", | |
12744 | t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12745 | remove = true; | |
12746 | } | |
12747 | else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) | |
12748 | { | |
12749 | error_at (OMP_CLAUSE_LOCATION (c), | |
12750 | "%qE does not have a mappable type in %qs clause", | |
12751 | t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12752 | remove = true; | |
12753 | } | |
12754 | while (TREE_CODE (t) == COMPONENT_REF) | |
12755 | { | |
12756 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) | |
12757 | == UNION_TYPE) | |
12758 | { | |
12759 | error_at (OMP_CLAUSE_LOCATION (c), | |
12760 | "%qE is a member of a union", t); | |
12761 | remove = true; | |
12762 | break; | |
12763 | } | |
12764 | t = TREE_OPERAND (t, 0); | |
12765 | } | |
12766 | if (remove) | |
12767 | break; | |
12768 | if (VAR_P (t) || TREE_CODE (t) == PARM_DECL) | |
12769 | { | |
12770 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP | |
12771 | && (OMP_CLAUSE_MAP_KIND (c) | |
12772 | == GOMP_MAP_FIRSTPRIVATE_POINTER)) | |
12773 | { | |
12774 | if (bitmap_bit_p (&generic_field_head, DECL_UID (t))) | |
12775 | break; | |
12776 | } | |
12777 | else if (bitmap_bit_p (&map_field_head, DECL_UID (t))) | |
12778 | break; | |
12779 | } | |
12780 | } | |
12781 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) | |
acf0174b JJ |
12782 | { |
12783 | error_at (OMP_CLAUSE_LOCATION (c), | |
12784 | "%qE is not a variable in %qs clause", t, | |
12785 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12786 | remove = true; | |
12787 | } | |
0ae9bd27 | 12788 | else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t)) |
acf0174b JJ |
12789 | { |
12790 | error_at (OMP_CLAUSE_LOCATION (c), | |
12791 | "%qD is threadprivate variable in %qs clause", t, | |
12792 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12793 | remove = true; | |
12794 | } | |
12795 | else if (!c_mark_addressable (t)) | |
12796 | remove = true; | |
b17a8b07 | 12797 | else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP |
41dbbb37 | 12798 | && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER |
d9a6bd32 JJ |
12799 | || (OMP_CLAUSE_MAP_KIND (c) |
12800 | == GOMP_MAP_FIRSTPRIVATE_POINTER) | |
41dbbb37 TS |
12801 | || (OMP_CLAUSE_MAP_KIND (c) |
12802 | == GOMP_MAP_FORCE_DEVICEPTR))) | |
d9a6bd32 | 12803 | && t == OMP_CLAUSE_DECL (c) |
b17a8b07 | 12804 | && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) |
acf0174b JJ |
12805 | { |
12806 | error_at (OMP_CLAUSE_LOCATION (c), | |
12807 | "%qD does not have a mappable type in %qs clause", t, | |
12808 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12809 | remove = true; | |
12810 | } | |
d9a6bd32 JJ |
12811 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP |
12812 | && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER) | |
12813 | { | |
12814 | if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12815 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
12816 | { | |
12817 | error ("%qD appears more than once in data clauses", t); | |
12818 | remove = true; | |
12819 | } | |
12820 | else | |
12821 | { | |
12822 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
12823 | if (t != OMP_CLAUSE_DECL (c) | |
12824 | && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF) | |
12825 | bitmap_set_bit (&generic_field_head, DECL_UID (t)); | |
12826 | } | |
12827 | } | |
12828 | else if (bitmap_bit_p (&map_head, DECL_UID (t))) | |
acf0174b JJ |
12829 | { |
12830 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) | |
12831 | error ("%qD appears more than once in motion clauses", t); | |
12832 | else | |
12833 | error ("%qD appears more than once in map clauses", t); | |
12834 | remove = true; | |
12835 | } | |
12836 | else | |
d9a6bd32 JJ |
12837 | { |
12838 | bitmap_set_bit (&map_head, DECL_UID (t)); | |
12839 | if (t != OMP_CLAUSE_DECL (c) | |
12840 | && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF) | |
12841 | bitmap_set_bit (&map_field_head, DECL_UID (t)); | |
12842 | } | |
12843 | break; | |
12844 | ||
12845 | case OMP_CLAUSE_TO_DECLARE: | |
12846 | t = OMP_CLAUSE_DECL (c); | |
12847 | if (TREE_CODE (t) == FUNCTION_DECL) | |
12848 | break; | |
12849 | /* FALLTHRU */ | |
12850 | case OMP_CLAUSE_LINK: | |
12851 | t = OMP_CLAUSE_DECL (c); | |
12852 | if (!VAR_P (t)) | |
12853 | { | |
12854 | error_at (OMP_CLAUSE_LOCATION (c), | |
12855 | "%qE is not a variable in clause %qs", t, | |
12856 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12857 | remove = true; | |
12858 | } | |
12859 | else if (DECL_THREAD_LOCAL_P (t)) | |
12860 | { | |
12861 | error_at (OMP_CLAUSE_LOCATION (c), | |
12862 | "%qD is threadprivate variable in %qs clause", t, | |
12863 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12864 | remove = true; | |
12865 | } | |
12866 | else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) | |
12867 | { | |
12868 | error_at (OMP_CLAUSE_LOCATION (c), | |
12869 | "%qD does not have a mappable type in %qs clause", t, | |
12870 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12871 | remove = true; | |
12872 | } | |
acf0174b JJ |
12873 | break; |
12874 | ||
12875 | case OMP_CLAUSE_UNIFORM: | |
12876 | t = OMP_CLAUSE_DECL (c); | |
12877 | if (TREE_CODE (t) != PARM_DECL) | |
12878 | { | |
12879 | if (DECL_P (t)) | |
12880 | error_at (OMP_CLAUSE_LOCATION (c), | |
12881 | "%qD is not an argument in %<uniform%> clause", t); | |
12882 | else | |
12883 | error_at (OMP_CLAUSE_LOCATION (c), | |
12884 | "%qE is not an argument in %<uniform%> clause", t); | |
12885 | remove = true; | |
ee1d5a02 | 12886 | break; |
acf0174b | 12887 | } |
ee1d5a02 | 12888 | goto check_dup_generic; |
acf0174b | 12889 | |
d9a6bd32 JJ |
12890 | case OMP_CLAUSE_IS_DEVICE_PTR: |
12891 | case OMP_CLAUSE_USE_DEVICE_PTR: | |
12892 | t = OMP_CLAUSE_DECL (c); | |
12893 | if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE | |
12894 | && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE) | |
12895 | { | |
12896 | error_at (OMP_CLAUSE_LOCATION (c), | |
12897 | "%qs variable is neither a pointer nor an array", | |
12898 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12899 | remove = true; | |
12900 | } | |
12901 | goto check_dup_generic; | |
12902 | ||
acf0174b JJ |
12903 | case OMP_CLAUSE_NOWAIT: |
12904 | if (copyprivate_seen) | |
12905 | { | |
12906 | error_at (OMP_CLAUSE_LOCATION (c), | |
12907 | "%<nowait%> clause must not be used together " | |
12908 | "with %<copyprivate%>"); | |
12909 | remove = true; | |
12910 | break; | |
12911 | } | |
12912 | nowait_clause = pc; | |
12913 | pc = &OMP_CLAUSE_CHAIN (c); | |
12914 | continue; | |
12915 | ||
953ff289 DN |
12916 | case OMP_CLAUSE_IF: |
12917 | case OMP_CLAUSE_NUM_THREADS: | |
acf0174b JJ |
12918 | case OMP_CLAUSE_NUM_TEAMS: |
12919 | case OMP_CLAUSE_THREAD_LIMIT: | |
953ff289 | 12920 | case OMP_CLAUSE_SCHEDULE: |
953ff289 DN |
12921 | case OMP_CLAUSE_ORDERED: |
12922 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 JJ |
12923 | case OMP_CLAUSE_UNTIED: |
12924 | case OMP_CLAUSE_COLLAPSE: | |
20906c66 JJ |
12925 | case OMP_CLAUSE_FINAL: |
12926 | case OMP_CLAUSE_MERGEABLE: | |
acf0174b JJ |
12927 | case OMP_CLAUSE_DEVICE: |
12928 | case OMP_CLAUSE_DIST_SCHEDULE: | |
12929 | case OMP_CLAUSE_PARALLEL: | |
12930 | case OMP_CLAUSE_FOR: | |
12931 | case OMP_CLAUSE_SECTIONS: | |
12932 | case OMP_CLAUSE_TASKGROUP: | |
12933 | case OMP_CLAUSE_PROC_BIND: | |
d9a6bd32 JJ |
12934 | case OMP_CLAUSE_PRIORITY: |
12935 | case OMP_CLAUSE_GRAINSIZE: | |
12936 | case OMP_CLAUSE_NUM_TASKS: | |
12937 | case OMP_CLAUSE_NOGROUP: | |
12938 | case OMP_CLAUSE_THREADS: | |
12939 | case OMP_CLAUSE_SIMD: | |
12940 | case OMP_CLAUSE_HINT: | |
12941 | case OMP_CLAUSE_DEFAULTMAP: | |
9a771876 | 12942 | case OMP_CLAUSE__CILK_FOR_COUNT_: |
41dbbb37 TS |
12943 | case OMP_CLAUSE_NUM_GANGS: |
12944 | case OMP_CLAUSE_NUM_WORKERS: | |
12945 | case OMP_CLAUSE_VECTOR_LENGTH: | |
12946 | case OMP_CLAUSE_ASYNC: | |
12947 | case OMP_CLAUSE_WAIT: | |
12948 | case OMP_CLAUSE_AUTO: | |
12949 | case OMP_CLAUSE_SEQ: | |
12950 | case OMP_CLAUSE_GANG: | |
12951 | case OMP_CLAUSE_WORKER: | |
12952 | case OMP_CLAUSE_VECTOR: | |
acf0174b JJ |
12953 | pc = &OMP_CLAUSE_CHAIN (c); |
12954 | continue; | |
12955 | ||
d9a6bd32 JJ |
12956 | case OMP_CLAUSE_SAFELEN: |
12957 | safelen = c; | |
12958 | pc = &OMP_CLAUSE_CHAIN (c); | |
12959 | continue; | |
12960 | case OMP_CLAUSE_SIMDLEN: | |
12961 | simdlen = c; | |
12962 | pc = &OMP_CLAUSE_CHAIN (c); | |
12963 | continue; | |
12964 | ||
acf0174b JJ |
12965 | case OMP_CLAUSE_INBRANCH: |
12966 | case OMP_CLAUSE_NOTINBRANCH: | |
12967 | if (branch_seen) | |
12968 | { | |
12969 | error_at (OMP_CLAUSE_LOCATION (c), | |
12970 | "%<inbranch%> clause is incompatible with " | |
12971 | "%<notinbranch%>"); | |
12972 | remove = true; | |
12973 | break; | |
12974 | } | |
12975 | branch_seen = true; | |
953ff289 DN |
12976 | pc = &OMP_CLAUSE_CHAIN (c); |
12977 | continue; | |
12978 | ||
12979 | default: | |
12980 | gcc_unreachable (); | |
12981 | } | |
12982 | ||
12983 | if (!remove) | |
12984 | { | |
12985 | t = OMP_CLAUSE_DECL (c); | |
12986 | ||
12987 | if (need_complete) | |
12988 | { | |
12989 | t = require_complete_type (t); | |
12990 | if (t == error_mark_node) | |
12991 | remove = true; | |
12992 | } | |
12993 | ||
12994 | if (need_implicitly_determined) | |
12995 | { | |
12996 | const char *share_name = NULL; | |
12997 | ||
0ae9bd27 | 12998 | if (VAR_P (t) && DECL_THREAD_LOCAL_P (t)) |
953ff289 DN |
12999 | share_name = "threadprivate"; |
13000 | else switch (c_omp_predetermined_sharing (t)) | |
13001 | { | |
13002 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
13003 | break; | |
13004 | case OMP_CLAUSE_DEFAULT_SHARED: | |
20906c66 JJ |
13005 | /* const vars may be specified in firstprivate clause. */ |
13006 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
13007 | && TREE_READONLY (t)) | |
13008 | break; | |
953ff289 DN |
13009 | share_name = "shared"; |
13010 | break; | |
13011 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
13012 | share_name = "private"; | |
13013 | break; | |
13014 | default: | |
13015 | gcc_unreachable (); | |
13016 | } | |
13017 | if (share_name) | |
13018 | { | |
c2255bc4 AH |
13019 | error_at (OMP_CLAUSE_LOCATION (c), |
13020 | "%qE is predetermined %qs for %qs", | |
acf0174b JJ |
13021 | t, share_name, |
13022 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
953ff289 DN |
13023 | remove = true; |
13024 | } | |
13025 | } | |
13026 | } | |
13027 | ||
13028 | if (remove) | |
13029 | *pc = OMP_CLAUSE_CHAIN (c); | |
13030 | else | |
13031 | pc = &OMP_CLAUSE_CHAIN (c); | |
13032 | } | |
13033 | ||
d9a6bd32 JJ |
13034 | if (simdlen |
13035 | && safelen | |
13036 | && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen), | |
13037 | OMP_CLAUSE_SIMDLEN_EXPR (simdlen))) | |
13038 | { | |
13039 | error_at (OMP_CLAUSE_LOCATION (simdlen), | |
13040 | "%<simdlen%> clause value is bigger than " | |
13041 | "%<safelen%> clause value"); | |
13042 | OMP_CLAUSE_SIMDLEN_EXPR (simdlen) | |
13043 | = OMP_CLAUSE_SAFELEN_EXPR (safelen); | |
13044 | } | |
13045 | ||
953ff289 DN |
13046 | bitmap_obstack_release (NULL); |
13047 | return clauses; | |
13048 | } | |
9ae165a0 | 13049 | |
0a35513e AH |
13050 | /* Create a transaction node. */ |
13051 | ||
13052 | tree | |
13053 | c_finish_transaction (location_t loc, tree block, int flags) | |
13054 | { | |
13055 | tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); | |
13056 | if (flags & TM_STMT_ATTR_OUTER) | |
13057 | TRANSACTION_EXPR_OUTER (stmt) = 1; | |
13058 | if (flags & TM_STMT_ATTR_RELAXED) | |
13059 | TRANSACTION_EXPR_RELAXED (stmt) = 1; | |
13060 | return add_stmt (stmt); | |
13061 | } | |
13062 | ||
9ae165a0 DG |
13063 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
13064 | down to the element type of an array. */ | |
13065 | ||
13066 | tree | |
13067 | c_build_qualified_type (tree type, int type_quals) | |
13068 | { | |
13069 | if (type == error_mark_node) | |
13070 | return type; | |
13071 | ||
13072 | if (TREE_CODE (type) == ARRAY_TYPE) | |
13073 | { | |
13074 | tree t; | |
13075 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
13076 | type_quals); | |
13077 | ||
13078 | /* See if we already have an identically qualified type. */ | |
13079 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
13080 | { | |
13081 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
13082 | && TYPE_NAME (t) == TYPE_NAME (type) | |
13083 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
13084 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
13085 | TYPE_ATTRIBUTES (type))) | |
13086 | break; | |
13087 | } | |
13088 | if (!t) | |
13089 | { | |
13090 | tree domain = TYPE_DOMAIN (type); | |
13091 | ||
13092 | t = build_variant_type_copy (type); | |
13093 | TREE_TYPE (t) = element_type; | |
13094 | ||
13095 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
13096 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
13097 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
13098 | else if (TYPE_CANONICAL (element_type) != element_type | |
13099 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
13100 | { | |
b8698a0f | 13101 | tree unqualified_canon |
9ae165a0 | 13102 | = build_array_type (TYPE_CANONICAL (element_type), |
b8698a0f | 13103 | domain? TYPE_CANONICAL (domain) |
9ae165a0 | 13104 | : NULL_TREE); |
b8698a0f | 13105 | TYPE_CANONICAL (t) |
9ae165a0 DG |
13106 | = c_build_qualified_type (unqualified_canon, type_quals); |
13107 | } | |
13108 | else | |
13109 | TYPE_CANONICAL (t) = t; | |
13110 | } | |
13111 | return t; | |
13112 | } | |
13113 | ||
13114 | /* A restrict-qualified pointer type must be a pointer to object or | |
13115 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
13116 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
13117 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
13118 | && (!POINTER_TYPE_P (type) | |
13119 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
13120 | { | |
13121 | error ("invalid use of %<restrict%>"); | |
13122 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
13123 | } | |
13124 | ||
e922069e JW |
13125 | tree var_type = build_qualified_type (type, type_quals); |
13126 | /* A variant type does not inherit the list of incomplete vars from the | |
13127 | type main variant. */ | |
13128 | if (TREE_CODE (var_type) == RECORD_TYPE | |
13129 | || TREE_CODE (var_type) == UNION_TYPE) | |
13130 | C_TYPE_INCOMPLETE_VARS (var_type) = 0; | |
13131 | return var_type; | |
9ae165a0 | 13132 | } |
72b5577d ILT |
13133 | |
13134 | /* Build a VA_ARG_EXPR for the C parser. */ | |
13135 | ||
13136 | tree | |
c2255bc4 | 13137 | c_build_va_arg (location_t loc, tree expr, tree type) |
72b5577d | 13138 | { |
ec3fba51 MP |
13139 | if (error_operand_p (type)) |
13140 | return error_mark_node; | |
13141 | else if (!COMPLETE_TYPE_P (type)) | |
4e81b788 | 13142 | { |
ec3fba51 MP |
13143 | error_at (loc, "second argument to %<va_arg%> is of incomplete " |
13144 | "type %qT", type); | |
4e81b788 MP |
13145 | return error_mark_node; |
13146 | } | |
ec3fba51 MP |
13147 | else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) |
13148 | warning_at (loc, OPT_Wc___compat, | |
13149 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
c2255bc4 | 13150 | return build_va_arg (loc, expr, type); |
72b5577d | 13151 | } |
acf0174b JJ |
13152 | |
13153 | /* Return truthvalue of whether T1 is the same tree structure as T2. | |
13154 | Return 1 if they are the same. Return 0 if they are different. */ | |
13155 | ||
13156 | bool | |
13157 | c_tree_equal (tree t1, tree t2) | |
13158 | { | |
13159 | enum tree_code code1, code2; | |
13160 | ||
13161 | if (t1 == t2) | |
13162 | return true; | |
13163 | if (!t1 || !t2) | |
13164 | return false; | |
13165 | ||
13166 | for (code1 = TREE_CODE (t1); | |
13167 | CONVERT_EXPR_CODE_P (code1) | |
13168 | || code1 == NON_LVALUE_EXPR; | |
13169 | code1 = TREE_CODE (t1)) | |
13170 | t1 = TREE_OPERAND (t1, 0); | |
13171 | for (code2 = TREE_CODE (t2); | |
13172 | CONVERT_EXPR_CODE_P (code2) | |
13173 | || code2 == NON_LVALUE_EXPR; | |
13174 | code2 = TREE_CODE (t2)) | |
13175 | t2 = TREE_OPERAND (t2, 0); | |
13176 | ||
13177 | /* They might have become equal now. */ | |
13178 | if (t1 == t2) | |
13179 | return true; | |
13180 | ||
13181 | if (code1 != code2) | |
13182 | return false; | |
13183 | ||
13184 | switch (code1) | |
13185 | { | |
13186 | case INTEGER_CST: | |
807e902e | 13187 | return wi::eq_p (t1, t2); |
acf0174b JJ |
13188 | |
13189 | case REAL_CST: | |
624d31fe | 13190 | return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2)); |
acf0174b JJ |
13191 | |
13192 | case STRING_CST: | |
13193 | return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2) | |
13194 | && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), | |
13195 | TREE_STRING_LENGTH (t1)); | |
13196 | ||
13197 | case FIXED_CST: | |
13198 | return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), | |
13199 | TREE_FIXED_CST (t2)); | |
13200 | ||
13201 | case COMPLEX_CST: | |
13202 | return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2)) | |
13203 | && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2)); | |
13204 | ||
13205 | case VECTOR_CST: | |
13206 | return operand_equal_p (t1, t2, OEP_ONLY_CONST); | |
13207 | ||
13208 | case CONSTRUCTOR: | |
13209 | /* We need to do this when determining whether or not two | |
13210 | non-type pointer to member function template arguments | |
13211 | are the same. */ | |
13212 | if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)) | |
13213 | || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2)) | |
13214 | return false; | |
13215 | { | |
13216 | tree field, value; | |
13217 | unsigned int i; | |
13218 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value) | |
13219 | { | |
13220 | constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i); | |
13221 | if (!c_tree_equal (field, elt2->index) | |
13222 | || !c_tree_equal (value, elt2->value)) | |
13223 | return false; | |
13224 | } | |
13225 | } | |
13226 | return true; | |
13227 | ||
13228 | case TREE_LIST: | |
13229 | if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))) | |
13230 | return false; | |
13231 | if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2))) | |
13232 | return false; | |
13233 | return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
13234 | ||
13235 | case SAVE_EXPR: | |
13236 | return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
13237 | ||
13238 | case CALL_EXPR: | |
13239 | { | |
13240 | tree arg1, arg2; | |
13241 | call_expr_arg_iterator iter1, iter2; | |
13242 | if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) | |
13243 | return false; | |
13244 | for (arg1 = first_call_expr_arg (t1, &iter1), | |
13245 | arg2 = first_call_expr_arg (t2, &iter2); | |
13246 | arg1 && arg2; | |
13247 | arg1 = next_call_expr_arg (&iter1), | |
13248 | arg2 = next_call_expr_arg (&iter2)) | |
13249 | if (!c_tree_equal (arg1, arg2)) | |
13250 | return false; | |
13251 | if (arg1 || arg2) | |
13252 | return false; | |
13253 | return true; | |
13254 | } | |
13255 | ||
13256 | case TARGET_EXPR: | |
13257 | { | |
13258 | tree o1 = TREE_OPERAND (t1, 0); | |
13259 | tree o2 = TREE_OPERAND (t2, 0); | |
13260 | ||
13261 | /* Special case: if either target is an unallocated VAR_DECL, | |
13262 | it means that it's going to be unified with whatever the | |
13263 | TARGET_EXPR is really supposed to initialize, so treat it | |
13264 | as being equivalent to anything. */ | |
0ae9bd27 | 13265 | if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE |
acf0174b JJ |
13266 | && !DECL_RTL_SET_P (o1)) |
13267 | /*Nop*/; | |
0ae9bd27 | 13268 | else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE |
acf0174b JJ |
13269 | && !DECL_RTL_SET_P (o2)) |
13270 | /*Nop*/; | |
13271 | else if (!c_tree_equal (o1, o2)) | |
13272 | return false; | |
13273 | ||
13274 | return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); | |
13275 | } | |
13276 | ||
13277 | case COMPONENT_REF: | |
13278 | if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)) | |
13279 | return false; | |
13280 | return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
13281 | ||
13282 | case PARM_DECL: | |
13283 | case VAR_DECL: | |
13284 | case CONST_DECL: | |
13285 | case FIELD_DECL: | |
13286 | case FUNCTION_DECL: | |
13287 | case IDENTIFIER_NODE: | |
13288 | case SSA_NAME: | |
13289 | return false; | |
13290 | ||
13291 | case TREE_VEC: | |
13292 | { | |
13293 | unsigned ix; | |
13294 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
13295 | return false; | |
13296 | for (ix = TREE_VEC_LENGTH (t1); ix--;) | |
13297 | if (!c_tree_equal (TREE_VEC_ELT (t1, ix), | |
13298 | TREE_VEC_ELT (t2, ix))) | |
13299 | return false; | |
13300 | return true; | |
13301 | } | |
13302 | ||
13303 | default: | |
13304 | break; | |
13305 | } | |
13306 | ||
13307 | switch (TREE_CODE_CLASS (code1)) | |
13308 | { | |
13309 | case tcc_unary: | |
13310 | case tcc_binary: | |
13311 | case tcc_comparison: | |
13312 | case tcc_expression: | |
13313 | case tcc_vl_exp: | |
13314 | case tcc_reference: | |
13315 | case tcc_statement: | |
13316 | { | |
13317 | int i, n = TREE_OPERAND_LENGTH (t1); | |
13318 | ||
13319 | switch (code1) | |
13320 | { | |
13321 | case PREINCREMENT_EXPR: | |
13322 | case PREDECREMENT_EXPR: | |
13323 | case POSTINCREMENT_EXPR: | |
13324 | case POSTDECREMENT_EXPR: | |
13325 | n = 1; | |
13326 | break; | |
13327 | case ARRAY_REF: | |
13328 | n = 2; | |
13329 | break; | |
13330 | default: | |
13331 | break; | |
13332 | } | |
13333 | ||
13334 | if (TREE_CODE_CLASS (code1) == tcc_vl_exp | |
13335 | && n != TREE_OPERAND_LENGTH (t2)) | |
13336 | return false; | |
13337 | ||
13338 | for (i = 0; i < n; ++i) | |
13339 | if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i))) | |
13340 | return false; | |
13341 | ||
13342 | return true; | |
13343 | } | |
13344 | ||
13345 | case tcc_type: | |
13346 | return comptypes (t1, t2); | |
13347 | default: | |
13348 | gcc_unreachable (); | |
13349 | } | |
13350 | /* We can get here with --disable-checking. */ | |
13351 | return false; | |
13352 | } | |
12893402 BI |
13353 | |
13354 | /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a | |
13355 | spawn-helper and BODY is the newly created body for FNDECL. */ | |
13356 | ||
13357 | void | |
13358 | cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w) | |
13359 | { | |
13360 | tree list = alloc_stmt_list (); | |
13361 | tree frame = make_cilk_frame (fndecl); | |
13362 | tree dtor = create_cilk_function_exit (frame, false, true); | |
13363 | add_local_decl (cfun, frame); | |
13364 | ||
13365 | DECL_SAVED_TREE (fndecl) = list; | |
13366 | tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)), | |
13367 | frame); | |
13368 | tree body_list = cilk_install_body_pedigree_operations (frame_ptr); | |
13369 | gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST); | |
13370 | ||
13371 | tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr); | |
13372 | append_to_statement_list (detach_expr, &body_list); | |
13373 | ||
13374 | cilk_outline (fndecl, &body, (struct wrapper_data *) w); | |
13375 | body = fold_build_cleanup_point_expr (void_type_node, body); | |
13376 | ||
13377 | append_to_statement_list (body, &body_list); | |
13378 | append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR, | |
13379 | body_list, dtor), &list); | |
13380 | } | |
1807ffc1 MS |
13381 | |
13382 | /* Returns true when the function declaration FNDECL is implicit, | |
13383 | introduced as a result of a call to an otherwise undeclared | |
13384 | function, and false otherwise. */ | |
13385 | ||
13386 | bool | |
13387 | c_decl_implicit (const_tree fndecl) | |
13388 | { | |
13389 | return C_DECL_IMPLICIT (fndecl); | |
13390 | } |