]>
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 ZW |
28 | #include "coretypes.h" |
29 | #include "tm.h" | |
400fbf9f | 30 | #include "tree.h" |
c7131fb2 | 31 | #include "alias.h" |
40e23961 | 32 | #include "fold-const.h" |
d8a2d370 DN |
33 | #include "stor-layout.h" |
34 | #include "trans-mem.h" | |
35 | #include "varasm.h" | |
36 | #include "stmt.h" | |
e57e265b | 37 | #include "langhooks.h" |
400fbf9f | 38 | #include "c-tree.h" |
29cc57cf | 39 | #include "c-lang.h" |
400fbf9f | 40 | #include "flags.h" |
ab87f8c8 | 41 | #include "intl.h" |
672a6f42 | 42 | #include "target.h" |
325c3691 | 43 | #include "tree-iterator.h" |
6662d794 | 44 | #include "bitmap.h" |
60393bbc | 45 | #include "predict.h" |
60393bbc | 46 | #include "hard-reg-set.h" |
60393bbc | 47 | #include "function.h" |
2fb9a547 | 48 | #include "gimple-expr.h" |
45b0be94 | 49 | #include "gimplify.h" |
acf0174b | 50 | #include "tree-inline.h" |
0645c1a2 | 51 | #include "omp-low.h" |
61d3ce20 | 52 | #include "c-family/c-objc.h" |
a212e43f | 53 | #include "c-family/c-common.h" |
de5a5fa1 | 54 | #include "c-family/c-ubsan.h" |
12893402 | 55 | #include "cilk.h" |
41dbbb37 | 56 | #include "gomp-constants.h" |
325c3691 | 57 | |
2ac2f164 JM |
58 | /* Possible cases of implicit bad conversions. Used to select |
59 | diagnostic messages in convert_for_assignment. */ | |
60 | enum impl_conv { | |
61 | ic_argpass, | |
62 | ic_assign, | |
63 | ic_init, | |
64 | ic_return | |
65 | }; | |
66 | ||
bc4b653b JM |
67 | /* The level of nesting inside "__alignof__". */ |
68 | int in_alignof; | |
69 | ||
70 | /* The level of nesting inside "sizeof". */ | |
71 | int in_sizeof; | |
72 | ||
73 | /* The level of nesting inside "typeof". */ | |
74 | int in_typeof; | |
400fbf9f | 75 | |
1a4049e7 JJ |
76 | /* The argument of last parsed sizeof expression, only to be tested |
77 | if expr.original_code == SIZEOF_EXPR. */ | |
78 | tree c_last_sizeof_arg; | |
79 | ||
9bac5cbb G |
80 | /* Nonzero if we might need to print a "missing braces around |
81 | initializer" message within this initializer. */ | |
82 | static int found_missing_braces; | |
103b7b17 | 83 | |
bf730f15 RS |
84 | static int require_constant_value; |
85 | static int require_constant_elements; | |
86 | ||
58f9752a | 87 | static bool null_pointer_constant_p (const_tree); |
f55ade6e | 88 | static tree qualify_type (tree, tree); |
dc5027f4 JM |
89 | static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *, |
90 | bool *); | |
744aa42f | 91 | static int comp_target_types (location_t, tree, tree); |
dc5027f4 JM |
92 | static int function_types_compatible_p (const_tree, const_tree, bool *, |
93 | bool *); | |
94 | static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *); | |
f55ade6e | 95 | static tree lookup_field (tree, tree); |
81e5eca8 MP |
96 | static int convert_arguments (location_t, vec<location_t>, tree, |
97 | vec<tree, va_gc> *, vec<tree, va_gc> *, tree, | |
98 | tree); | |
db3927fb | 99 | static tree pointer_diff (location_t, tree, tree); |
68fca595 | 100 | static tree convert_for_assignment (location_t, location_t, tree, tree, tree, |
744aa42f | 101 | enum impl_conv, bool, tree, tree, int); |
f55ade6e AJ |
102 | static tree valid_compound_expr_initializer (tree, tree); |
103 | static void push_string (const char *); | |
104 | static void push_member_name (tree); | |
f55ade6e AJ |
105 | static int spelling_length (void); |
106 | static char *print_spelling (char *); | |
96b40f8d | 107 | static void warning_init (location_t, int, const char *); |
c2255bc4 | 108 | static tree digest_init (location_t, tree, tree, tree, bool, bool, int); |
34cf811f MP |
109 | static void output_init_element (location_t, tree, tree, bool, tree, tree, int, |
110 | bool, struct obstack *); | |
a1e3b3d9 | 111 | static void output_pending_init_elements (int, struct obstack *); |
ea58ef42 | 112 | static int set_designator (location_t, int, struct obstack *); |
a1e3b3d9 | 113 | static void push_range_stack (tree, struct obstack *); |
96b40f8d MP |
114 | static void add_pending_init (location_t, tree, tree, tree, bool, |
115 | struct obstack *); | |
a1e3b3d9 LB |
116 | static void set_nonincremental_init (struct obstack *); |
117 | static void set_nonincremental_init_from_string (tree, struct obstack *); | |
118 | static tree find_init_member (tree, struct obstack *); | |
f37acdf9 | 119 | static void readonly_warning (tree, enum lvalue_use); |
7bd11157 | 120 | static int lvalue_or_else (location_t, const_tree, enum lvalue_use); |
4e2fb7de | 121 | static void record_maybe_used_decl (tree); |
dc5027f4 | 122 | static int comptypes_internal (const_tree, const_tree, bool *, bool *); |
6aa3c60d JM |
123 | \f |
124 | /* Return true if EXP is a null pointer constant, false otherwise. */ | |
125 | ||
126 | static bool | |
58f9752a | 127 | null_pointer_constant_p (const_tree expr) |
6aa3c60d JM |
128 | { |
129 | /* This should really operate on c_expr structures, but they aren't | |
130 | yet available everywhere required. */ | |
131 | tree type = TREE_TYPE (expr); | |
132 | return (TREE_CODE (expr) == INTEGER_CST | |
8bcd6380 | 133 | && !TREE_OVERFLOW (expr) |
6aa3c60d JM |
134 | && integer_zerop (expr) |
135 | && (INTEGRAL_TYPE_P (type) | |
136 | || (TREE_CODE (type) == POINTER_TYPE | |
137 | && VOID_TYPE_P (TREE_TYPE (type)) | |
138 | && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); | |
139 | } | |
928c19bb JM |
140 | |
141 | /* EXPR may appear in an unevaluated part of an integer constant | |
142 | expression, but not in an evaluated part. Wrap it in a | |
143 | C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an | |
144 | INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ | |
145 | ||
146 | static tree | |
147 | note_integer_operands (tree expr) | |
148 | { | |
149 | tree ret; | |
150 | if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) | |
151 | { | |
152 | ret = copy_node (expr); | |
153 | TREE_OVERFLOW (ret) = 1; | |
154 | } | |
155 | else | |
156 | { | |
157 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); | |
158 | C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; | |
159 | } | |
160 | return ret; | |
161 | } | |
162 | ||
4d84fe7c JM |
163 | /* Having checked whether EXPR may appear in an unevaluated part of an |
164 | integer constant expression and found that it may, remove any | |
165 | C_MAYBE_CONST_EXPR noting this fact and return the resulting | |
166 | expression. */ | |
167 | ||
168 | static inline tree | |
169 | remove_c_maybe_const_expr (tree expr) | |
170 | { | |
171 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
172 | return C_MAYBE_CONST_EXPR_EXPR (expr); | |
173 | else | |
174 | return expr; | |
175 | } | |
176 | ||
f13c9b2c AP |
177 | \f/* This is a cache to hold if two types are compatible or not. */ |
178 | ||
179 | struct tagged_tu_seen_cache { | |
180 | const struct tagged_tu_seen_cache * next; | |
58f9752a KG |
181 | const_tree t1; |
182 | const_tree t2; | |
f13c9b2c AP |
183 | /* The return value of tagged_types_tu_compatible_p if we had seen |
184 | these two types already. */ | |
185 | int val; | |
186 | }; | |
187 | ||
188 | static const struct tagged_tu_seen_cache * tagged_tu_seen_base; | |
189 | static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); | |
190 | ||
400fbf9f JW |
191 | /* Do `exp = require_complete_type (exp);' to make sure exp |
192 | does not have an incomplete type. (That includes void types.) */ | |
193 | ||
194 | tree | |
2f6e4e97 | 195 | require_complete_type (tree value) |
400fbf9f JW |
196 | { |
197 | tree type = TREE_TYPE (value); | |
198 | ||
7a0ca710 | 199 | if (error_operand_p (value)) |
ea0f786b CB |
200 | return error_mark_node; |
201 | ||
400fbf9f | 202 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 203 | if (COMPLETE_TYPE_P (type)) |
400fbf9f JW |
204 | return value; |
205 | ||
7a228918 | 206 | c_incomplete_type_error (value, type); |
400fbf9f JW |
207 | return error_mark_node; |
208 | } | |
209 | ||
210 | /* Print an error message for invalid use of an incomplete type. | |
211 | VALUE is the expression that was used (or 0 if that isn't known) | |
212 | and TYPE is the type that was invalid. */ | |
213 | ||
214 | void | |
ac7d7749 | 215 | c_incomplete_type_error (const_tree value, const_tree type) |
400fbf9f | 216 | { |
400fbf9f JW |
217 | /* Avoid duplicate error message. */ |
218 | if (TREE_CODE (type) == ERROR_MARK) | |
219 | return; | |
220 | ||
0ae9bd27 | 221 | if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL)) |
ac9f18db | 222 | error ("%qD has an incomplete type %qT", value, type); |
400fbf9f JW |
223 | else |
224 | { | |
225 | retry: | |
226 | /* We must print an error message. Be clever about what it says. */ | |
227 | ||
228 | switch (TREE_CODE (type)) | |
229 | { | |
230 | case RECORD_TYPE: | |
400fbf9f | 231 | case UNION_TYPE: |
400fbf9f | 232 | case ENUMERAL_TYPE: |
400fbf9f JW |
233 | break; |
234 | ||
235 | case VOID_TYPE: | |
236 | error ("invalid use of void expression"); | |
237 | return; | |
238 | ||
239 | case ARRAY_TYPE: | |
240 | if (TYPE_DOMAIN (type)) | |
241 | { | |
fba78abb RH |
242 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) |
243 | { | |
244 | error ("invalid use of flexible array member"); | |
245 | return; | |
246 | } | |
400fbf9f JW |
247 | type = TREE_TYPE (type); |
248 | goto retry; | |
249 | } | |
250 | error ("invalid use of array with unspecified bounds"); | |
251 | return; | |
252 | ||
253 | default: | |
366de0ce | 254 | gcc_unreachable (); |
400fbf9f JW |
255 | } |
256 | ||
257 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
ac9f18db | 258 | error ("invalid use of undefined type %qT", type); |
400fbf9f JW |
259 | else |
260 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
ac9f18db | 261 | error ("invalid use of incomplete typedef %qT", type); |
400fbf9f JW |
262 | } |
263 | } | |
264 | ||
ab393bf1 NB |
265 | /* Given a type, apply default promotions wrt unnamed function |
266 | arguments and return the new type. */ | |
267 | ||
268 | tree | |
2f6e4e97 | 269 | c_type_promotes_to (tree type) |
ab393bf1 | 270 | { |
267bac10 | 271 | tree ret = NULL_TREE; |
ab393bf1 | 272 | |
267bac10 JM |
273 | if (TYPE_MAIN_VARIANT (type) == float_type_node) |
274 | ret = double_type_node; | |
275 | else if (c_promoting_integer_type_p (type)) | |
ab393bf1 NB |
276 | { |
277 | /* Preserve unsignedness if not really getting any wider. */ | |
8df83eae | 278 | if (TYPE_UNSIGNED (type) |
c22cacf3 | 279 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) |
267bac10 JM |
280 | ret = unsigned_type_node; |
281 | else | |
282 | ret = integer_type_node; | |
ab393bf1 NB |
283 | } |
284 | ||
267bac10 JM |
285 | if (ret != NULL_TREE) |
286 | return (TYPE_ATOMIC (type) | |
287 | ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC) | |
288 | : ret); | |
289 | ||
ab393bf1 NB |
290 | return type; |
291 | } | |
292 | ||
36c5e70a BE |
293 | /* Return true if between two named address spaces, whether there is a superset |
294 | named address space that encompasses both address spaces. If there is a | |
295 | superset, return which address space is the superset. */ | |
296 | ||
297 | static bool | |
298 | addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) | |
299 | { | |
300 | if (as1 == as2) | |
301 | { | |
302 | *common = as1; | |
303 | return true; | |
304 | } | |
305 | else if (targetm.addr_space.subset_p (as1, as2)) | |
306 | { | |
307 | *common = as2; | |
308 | return true; | |
309 | } | |
310 | else if (targetm.addr_space.subset_p (as2, as1)) | |
311 | { | |
312 | *common = as1; | |
313 | return true; | |
314 | } | |
315 | else | |
316 | return false; | |
317 | } | |
318 | ||
400fbf9f JW |
319 | /* Return a variant of TYPE which has all the type qualifiers of LIKE |
320 | as well as those of TYPE. */ | |
321 | ||
322 | static tree | |
2f6e4e97 | 323 | qualify_type (tree type, tree like) |
400fbf9f | 324 | { |
36c5e70a BE |
325 | addr_space_t as_type = TYPE_ADDR_SPACE (type); |
326 | addr_space_t as_like = TYPE_ADDR_SPACE (like); | |
327 | addr_space_t as_common; | |
328 | ||
329 | /* If the two named address spaces are different, determine the common | |
330 | superset address space. If there isn't one, raise an error. */ | |
331 | if (!addr_space_superset (as_type, as_like, &as_common)) | |
332 | { | |
333 | as_common = as_type; | |
334 | error ("%qT and %qT are in disjoint named address spaces", | |
335 | type, like); | |
336 | } | |
337 | ||
2f6e4e97 | 338 | return c_build_qualified_type (type, |
36c5e70a | 339 | TYPE_QUALS_NO_ADDR_SPACE (type) |
267bac10 | 340 | | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like) |
36c5e70a | 341 | | ENCODE_QUAL_ADDR_SPACE (as_common)); |
400fbf9f | 342 | } |
52ffd86e MS |
343 | |
344 | /* Return true iff the given tree T is a variable length array. */ | |
345 | ||
346 | bool | |
ac7d7749 | 347 | c_vla_type_p (const_tree t) |
52ffd86e MS |
348 | { |
349 | if (TREE_CODE (t) == ARRAY_TYPE | |
350 | && C_TYPE_VARIABLE_SIZE (t)) | |
351 | return true; | |
352 | return false; | |
353 | } | |
400fbf9f | 354 | \f |
10bc1b1b | 355 | /* Return the composite type of two compatible types. |
5305f6d7 | 356 | |
10bc1b1b JM |
357 | We assume that comptypes has already been done and returned |
358 | nonzero; if that isn't so, this may crash. In particular, we | |
359 | assume that qualifiers match. */ | |
400fbf9f JW |
360 | |
361 | tree | |
10bc1b1b | 362 | composite_type (tree t1, tree t2) |
400fbf9f | 363 | { |
b3694847 SS |
364 | enum tree_code code1; |
365 | enum tree_code code2; | |
4b027d16 | 366 | tree attributes; |
400fbf9f JW |
367 | |
368 | /* Save time if the two types are the same. */ | |
369 | ||
370 | if (t1 == t2) return t1; | |
371 | ||
372 | /* If one type is nonsense, use the other. */ | |
373 | if (t1 == error_mark_node) | |
374 | return t2; | |
375 | if (t2 == error_mark_node) | |
376 | return t1; | |
377 | ||
10bc1b1b JM |
378 | code1 = TREE_CODE (t1); |
379 | code2 = TREE_CODE (t2); | |
380 | ||
d9525bec | 381 | /* Merge the attributes. */ |
5fd9b178 | 382 | attributes = targetm.merge_type_attributes (t1, t2); |
4b027d16 | 383 | |
10bc1b1b JM |
384 | /* If one is an enumerated type and the other is the compatible |
385 | integer type, the composite type might be either of the two | |
386 | (DR#013 question 3). For consistency, use the enumerated type as | |
387 | the composite type. */ | |
400fbf9f | 388 | |
10bc1b1b JM |
389 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) |
390 | return t1; | |
391 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
392 | return t2; | |
75326e8c | 393 | |
366de0ce | 394 | gcc_assert (code1 == code2); |
b6a10c9f | 395 | |
400fbf9f JW |
396 | switch (code1) |
397 | { | |
400fbf9f | 398 | case POINTER_TYPE: |
10bc1b1b | 399 | /* For two pointers, do this recursively on the target type. */ |
400fbf9f | 400 | { |
3932261a MM |
401 | tree pointed_to_1 = TREE_TYPE (t1); |
402 | tree pointed_to_2 = TREE_TYPE (t2); | |
10bc1b1b | 403 | tree target = composite_type (pointed_to_1, pointed_to_2); |
3db684fb | 404 | t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false); |
fe7080d2 AP |
405 | t1 = build_type_attribute_variant (t1, attributes); |
406 | return qualify_type (t1, t2); | |
400fbf9f | 407 | } |
400fbf9f JW |
408 | |
409 | case ARRAY_TYPE: | |
410 | { | |
10bc1b1b | 411 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
46df2823 JM |
412 | int quals; |
413 | tree unqual_elt; | |
ca8bdb78 JM |
414 | tree d1 = TYPE_DOMAIN (t1); |
415 | tree d2 = TYPE_DOMAIN (t2); | |
416 | bool d1_variable, d2_variable; | |
417 | bool d1_zero, d2_zero; | |
f6294de7 | 418 | bool t1_complete, t2_complete; |
46df2823 | 419 | |
de46b2fe | 420 | /* We should not have any type quals on arrays at all. */ |
36c5e70a BE |
421 | gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) |
422 | && !TYPE_QUALS_NO_ADDR_SPACE (t2)); | |
c22cacf3 | 423 | |
f6294de7 JM |
424 | t1_complete = COMPLETE_TYPE_P (t1); |
425 | t2_complete = COMPLETE_TYPE_P (t2); | |
426 | ||
ca8bdb78 JM |
427 | d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); |
428 | d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); | |
429 | ||
430 | d1_variable = (!d1_zero | |
431 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
432 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
433 | d2_variable = (!d2_zero | |
434 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
435 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
436 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
437 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
ca8bdb78 | 438 | |
400fbf9f | 439 | /* Save space: see if the result is identical to one of the args. */ |
ca8bdb78 JM |
440 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) |
441 | && (d2_variable || d2_zero || !d1_variable)) | |
4b027d16 | 442 | return build_type_attribute_variant (t1, attributes); |
ca8bdb78 JM |
443 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) |
444 | && (d1_variable || d1_zero || !d2_variable)) | |
4b027d16 | 445 | return build_type_attribute_variant (t2, attributes); |
c22cacf3 | 446 | |
de46b2fe AP |
447 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) |
448 | return build_type_attribute_variant (t1, attributes); | |
449 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
450 | return build_type_attribute_variant (t2, attributes); | |
c22cacf3 | 451 | |
46df2823 JM |
452 | /* Merge the element types, and have a size if either arg has |
453 | one. We may have qualifiers on the element types. To set | |
454 | up TYPE_MAIN_VARIANT correctly, we need to form the | |
455 | composite of the unqualified types and add the qualifiers | |
456 | back at the end. */ | |
457 | quals = TYPE_QUALS (strip_array_types (elt)); | |
458 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
459 | t1 = build_array_type (unqual_elt, | |
ca8bdb78 JM |
460 | TYPE_DOMAIN ((TYPE_DOMAIN (t1) |
461 | && (d2_variable | |
462 | || d2_zero | |
463 | || !d1_variable)) | |
464 | ? t1 | |
465 | : t2)); | |
f6294de7 JM |
466 | /* Ensure a composite type involving a zero-length array type |
467 | is a zero-length type not an incomplete type. */ | |
468 | if (d1_zero && d2_zero | |
469 | && (t1_complete || t2_complete) | |
470 | && !COMPLETE_TYPE_P (t1)) | |
471 | { | |
472 | TYPE_SIZE (t1) = bitsize_zero_node; | |
473 | TYPE_SIZE_UNIT (t1) = size_zero_node; | |
474 | } | |
46df2823 | 475 | t1 = c_build_qualified_type (t1, quals); |
de46b2fe | 476 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
477 | } |
478 | ||
fcb99e7b JJ |
479 | case ENUMERAL_TYPE: |
480 | case RECORD_TYPE: | |
481 | case UNION_TYPE: | |
482 | if (attributes != NULL) | |
483 | { | |
484 | /* Try harder not to create a new aggregate type. */ | |
485 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
486 | return t1; | |
487 | if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
488 | return t2; | |
489 | } | |
490 | return build_type_attribute_variant (t1, attributes); | |
491 | ||
400fbf9f JW |
492 | case FUNCTION_TYPE: |
493 | /* Function types: prefer the one that specified arg types. | |
494 | If both do, merge the arg types. Also merge the return types. */ | |
495 | { | |
10bc1b1b | 496 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
400fbf9f JW |
497 | tree p1 = TYPE_ARG_TYPES (t1); |
498 | tree p2 = TYPE_ARG_TYPES (t2); | |
499 | int len; | |
500 | tree newargs, n; | |
501 | int i; | |
502 | ||
503 | /* Save space: see if the result is identical to one of the args. */ | |
3f75a254 | 504 | if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) |
4b027d16 | 505 | return build_type_attribute_variant (t1, attributes); |
3f75a254 | 506 | if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) |
4b027d16 | 507 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
508 | |
509 | /* Simple way if one arg fails to specify argument types. */ | |
510 | if (TYPE_ARG_TYPES (t1) == 0) | |
4b027d16 | 511 | { |
fe7080d2 AP |
512 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); |
513 | t1 = build_type_attribute_variant (t1, attributes); | |
514 | return qualify_type (t1, t2); | |
4b027d16 | 515 | } |
400fbf9f | 516 | if (TYPE_ARG_TYPES (t2) == 0) |
4b027d16 RK |
517 | { |
518 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
fe7080d2 AP |
519 | t1 = build_type_attribute_variant (t1, attributes); |
520 | return qualify_type (t1, t2); | |
4b027d16 | 521 | } |
400fbf9f JW |
522 | |
523 | /* If both args specify argument types, we must merge the two | |
524 | lists, argument by argument. */ | |
2f4e8f2b | 525 | |
400fbf9f JW |
526 | len = list_length (p1); |
527 | newargs = 0; | |
528 | ||
529 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 530 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
531 | |
532 | n = newargs; | |
533 | ||
534 | for (; p1; | |
535 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
536 | { | |
537 | /* A null type means arg type is not specified. | |
538 | Take whatever the other function type has. */ | |
539 | if (TREE_VALUE (p1) == 0) | |
540 | { | |
541 | TREE_VALUE (n) = TREE_VALUE (p2); | |
542 | goto parm_done; | |
543 | } | |
544 | if (TREE_VALUE (p2) == 0) | |
545 | { | |
546 | TREE_VALUE (n) = TREE_VALUE (p1); | |
547 | goto parm_done; | |
548 | } | |
2f6e4e97 | 549 | |
400fbf9f JW |
550 | /* Given wait (union {union wait *u; int *i} *) |
551 | and wait (union wait *), | |
552 | prefer union wait * as type of parm. */ | |
553 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
554 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
555 | { | |
556 | tree memb; | |
58cb41e6 JJ |
557 | tree mv2 = TREE_VALUE (p2); |
558 | if (mv2 && mv2 != error_mark_node | |
559 | && TREE_CODE (mv2) != ARRAY_TYPE) | |
560 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
400fbf9f | 561 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); |
910ad8de | 562 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
563 | { |
564 | tree mv3 = TREE_TYPE (memb); | |
565 | if (mv3 && mv3 != error_mark_node | |
566 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
567 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
568 | if (comptypes (mv3, mv2)) | |
569 | { | |
570 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
571 | TREE_VALUE (p2)); | |
c1771a20 | 572 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 573 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
574 | goto parm_done; |
575 | } | |
576 | } | |
400fbf9f JW |
577 | } |
578 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
579 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
580 | { | |
581 | tree memb; | |
58cb41e6 JJ |
582 | tree mv1 = TREE_VALUE (p1); |
583 | if (mv1 && mv1 != error_mark_node | |
584 | && TREE_CODE (mv1) != ARRAY_TYPE) | |
585 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
400fbf9f | 586 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); |
910ad8de | 587 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
588 | { |
589 | tree mv3 = TREE_TYPE (memb); | |
590 | if (mv3 && mv3 != error_mark_node | |
591 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
592 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
593 | if (comptypes (mv3, mv1)) | |
594 | { | |
595 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
596 | TREE_VALUE (p1)); | |
c1771a20 | 597 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 598 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
599 | goto parm_done; |
600 | } | |
601 | } | |
400fbf9f | 602 | } |
10bc1b1b | 603 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); |
400fbf9f JW |
604 | parm_done: ; |
605 | } | |
606 | ||
4b027d16 | 607 | t1 = build_function_type (valtype, newargs); |
fe7080d2 | 608 | t1 = qualify_type (t1, t2); |
0f41302f | 609 | /* ... falls through ... */ |
400fbf9f JW |
610 | } |
611 | ||
612 | default: | |
4b027d16 | 613 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
614 | } |
615 | ||
616 | } | |
10bc1b1b JM |
617 | |
618 | /* Return the type of a conditional expression between pointers to | |
619 | possibly differently qualified versions of compatible types. | |
620 | ||
621 | We assume that comp_target_types has already been done and returned | |
622 | nonzero; if that isn't so, this may crash. */ | |
623 | ||
624 | static tree | |
625 | common_pointer_type (tree t1, tree t2) | |
626 | { | |
627 | tree attributes; | |
46df2823 JM |
628 | tree pointed_to_1, mv1; |
629 | tree pointed_to_2, mv2; | |
10bc1b1b | 630 | tree target; |
eb1387a0 | 631 | unsigned target_quals; |
36c5e70a BE |
632 | addr_space_t as1, as2, as_common; |
633 | int quals1, quals2; | |
10bc1b1b JM |
634 | |
635 | /* Save time if the two types are the same. */ | |
636 | ||
637 | if (t1 == t2) return t1; | |
638 | ||
639 | /* If one type is nonsense, use the other. */ | |
640 | if (t1 == error_mark_node) | |
641 | return t2; | |
642 | if (t2 == error_mark_node) | |
643 | return t1; | |
644 | ||
366de0ce | 645 | gcc_assert (TREE_CODE (t1) == POINTER_TYPE |
c22cacf3 | 646 | && TREE_CODE (t2) == POINTER_TYPE); |
10bc1b1b JM |
647 | |
648 | /* Merge the attributes. */ | |
649 | attributes = targetm.merge_type_attributes (t1, t2); | |
650 | ||
651 | /* Find the composite type of the target types, and combine the | |
46df2823 JM |
652 | qualifiers of the two types' targets. Do not lose qualifiers on |
653 | array element types by taking the TYPE_MAIN_VARIANT. */ | |
654 | mv1 = pointed_to_1 = TREE_TYPE (t1); | |
655 | mv2 = pointed_to_2 = TREE_TYPE (t2); | |
656 | if (TREE_CODE (mv1) != ARRAY_TYPE) | |
657 | mv1 = TYPE_MAIN_VARIANT (pointed_to_1); | |
658 | if (TREE_CODE (mv2) != ARRAY_TYPE) | |
659 | mv2 = TYPE_MAIN_VARIANT (pointed_to_2); | |
660 | target = composite_type (mv1, mv2); | |
eb1387a0 | 661 | |
768952be MU |
662 | /* Strip array types to get correct qualifier for pointers to arrays */ |
663 | quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1)); | |
664 | quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2)); | |
665 | ||
eb1387a0 RG |
666 | /* For function types do not merge const qualifiers, but drop them |
667 | if used inconsistently. The middle-end uses these to mark const | |
668 | and noreturn functions. */ | |
669 | if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) | |
36c5e70a | 670 | target_quals = (quals1 & quals2); |
eb1387a0 | 671 | else |
36c5e70a BE |
672 | target_quals = (quals1 | quals2); |
673 | ||
674 | /* If the two named address spaces are different, determine the common | |
675 | superset address space. This is guaranteed to exist due to the | |
676 | assumption that comp_target_type returned non-zero. */ | |
677 | as1 = TYPE_ADDR_SPACE (pointed_to_1); | |
678 | as2 = TYPE_ADDR_SPACE (pointed_to_2); | |
679 | if (!addr_space_superset (as1, as2, &as_common)) | |
680 | gcc_unreachable (); | |
681 | ||
682 | target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); | |
683 | ||
eb1387a0 | 684 | t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); |
10bc1b1b JM |
685 | return build_type_attribute_variant (t1, attributes); |
686 | } | |
687 | ||
688 | /* Return the common type for two arithmetic types under the usual | |
689 | arithmetic conversions. The default conversions have already been | |
690 | applied, and enumerated types converted to their compatible integer | |
691 | types. The resulting type is unqualified and has no attributes. | |
692 | ||
693 | This is the type for the result of most arithmetic operations | |
694 | if the operands have the given two types. */ | |
695 | ||
ccf7f880 JJ |
696 | static tree |
697 | c_common_type (tree t1, tree t2) | |
10bc1b1b JM |
698 | { |
699 | enum tree_code code1; | |
700 | enum tree_code code2; | |
701 | ||
702 | /* If one type is nonsense, use the other. */ | |
703 | if (t1 == error_mark_node) | |
704 | return t2; | |
705 | if (t2 == error_mark_node) | |
706 | return t1; | |
707 | ||
708 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
709 | t1 = TYPE_MAIN_VARIANT (t1); | |
710 | ||
711 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
712 | t2 = TYPE_MAIN_VARIANT (t2); | |
713 | ||
714 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
715 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
716 | ||
717 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
718 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
719 | ||
720 | /* Save time if the two types are the same. */ | |
721 | ||
722 | if (t1 == t2) return t1; | |
723 | ||
724 | code1 = TREE_CODE (t1); | |
725 | code2 = TREE_CODE (t2); | |
726 | ||
366de0ce | 727 | gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE |
ab22c1fa CF |
728 | || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE |
729 | || code1 == INTEGER_TYPE); | |
366de0ce | 730 | gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE |
ab22c1fa CF |
731 | || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE |
732 | || code2 == INTEGER_TYPE); | |
10bc1b1b | 733 | |
5fc89bfd JJ |
734 | /* When one operand is a decimal float type, the other operand cannot be |
735 | a generic float type or a complex type. We also disallow vector types | |
736 | here. */ | |
737 | if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) | |
738 | && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) | |
739 | { | |
740 | if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) | |
741 | { | |
742 | error ("can%'t mix operands of decimal float and vector types"); | |
743 | return error_mark_node; | |
744 | } | |
745 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
746 | { | |
747 | error ("can%'t mix operands of decimal float and complex types"); | |
748 | return error_mark_node; | |
749 | } | |
750 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
751 | { | |
752 | error ("can%'t mix operands of decimal float and other float types"); | |
753 | return error_mark_node; | |
754 | } | |
755 | } | |
756 | ||
10bc1b1b JM |
757 | /* If one type is a vector type, return that type. (How the usual |
758 | arithmetic conversions apply to the vector types extension is not | |
759 | precisely specified.) */ | |
760 | if (code1 == VECTOR_TYPE) | |
761 | return t1; | |
762 | ||
763 | if (code2 == VECTOR_TYPE) | |
764 | return t2; | |
765 | ||
766 | /* If one type is complex, form the common type of the non-complex | |
767 | components, then make that complex. Use T1 or T2 if it is the | |
768 | required type. */ | |
769 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
770 | { | |
771 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
772 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
ccf7f880 | 773 | tree subtype = c_common_type (subtype1, subtype2); |
10bc1b1b JM |
774 | |
775 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
776 | return t1; | |
777 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
778 | return t2; | |
779 | else | |
780 | return build_complex_type (subtype); | |
781 | } | |
782 | ||
783 | /* If only one is real, use it as the result. */ | |
784 | ||
785 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
786 | return t1; | |
787 | ||
788 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
789 | return t2; | |
790 | ||
9a8ce21f JG |
791 | /* If both are real and either are decimal floating point types, use |
792 | the decimal floating point type with the greater precision. */ | |
793 | ||
794 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
795 | { | |
796 | if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node | |
797 | || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) | |
798 | return dfloat128_type_node; | |
799 | else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node | |
800 | || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) | |
801 | return dfloat64_type_node; | |
802 | else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node | |
803 | || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) | |
804 | return dfloat32_type_node; | |
805 | } | |
806 | ||
ab22c1fa CF |
807 | /* Deal with fixed-point types. */ |
808 | if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) | |
809 | { | |
810 | unsigned int unsignedp = 0, satp = 0; | |
ef4bddc2 | 811 | machine_mode m1, m2; |
ab22c1fa CF |
812 | unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; |
813 | ||
814 | m1 = TYPE_MODE (t1); | |
815 | m2 = TYPE_MODE (t2); | |
816 | ||
817 | /* If one input type is saturating, the result type is saturating. */ | |
818 | if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) | |
819 | satp = 1; | |
820 | ||
821 | /* If both fixed-point types are unsigned, the result type is unsigned. | |
822 | When mixing fixed-point and integer types, follow the sign of the | |
823 | fixed-point type. | |
824 | Otherwise, the result type is signed. */ | |
825 | if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) | |
826 | && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) | |
827 | || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE | |
828 | && TYPE_UNSIGNED (t1)) | |
829 | || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE | |
830 | && TYPE_UNSIGNED (t2))) | |
831 | unsignedp = 1; | |
832 | ||
833 | /* The result type is signed. */ | |
834 | if (unsignedp == 0) | |
835 | { | |
836 | /* If the input type is unsigned, we need to convert to the | |
837 | signed type. */ | |
838 | if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) | |
839 | { | |
d75d71e0 | 840 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
841 | if (GET_MODE_CLASS (m1) == MODE_UFRACT) |
842 | mclass = MODE_FRACT; | |
843 | else if (GET_MODE_CLASS (m1) == MODE_UACCUM) | |
844 | mclass = MODE_ACCUM; | |
845 | else | |
846 | gcc_unreachable (); | |
847 | m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); | |
848 | } | |
849 | if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) | |
850 | { | |
d75d71e0 | 851 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
852 | if (GET_MODE_CLASS (m2) == MODE_UFRACT) |
853 | mclass = MODE_FRACT; | |
854 | else if (GET_MODE_CLASS (m2) == MODE_UACCUM) | |
855 | mclass = MODE_ACCUM; | |
856 | else | |
857 | gcc_unreachable (); | |
858 | m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); | |
859 | } | |
860 | } | |
861 | ||
862 | if (code1 == FIXED_POINT_TYPE) | |
863 | { | |
864 | fbit1 = GET_MODE_FBIT (m1); | |
865 | ibit1 = GET_MODE_IBIT (m1); | |
866 | } | |
867 | else | |
868 | { | |
869 | fbit1 = 0; | |
870 | /* Signed integers need to subtract one sign bit. */ | |
871 | ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); | |
872 | } | |
873 | ||
874 | if (code2 == FIXED_POINT_TYPE) | |
875 | { | |
876 | fbit2 = GET_MODE_FBIT (m2); | |
877 | ibit2 = GET_MODE_IBIT (m2); | |
878 | } | |
879 | else | |
880 | { | |
881 | fbit2 = 0; | |
882 | /* Signed integers need to subtract one sign bit. */ | |
883 | ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); | |
884 | } | |
885 | ||
886 | max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; | |
887 | max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; | |
888 | return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, | |
889 | satp); | |
890 | } | |
891 | ||
10bc1b1b JM |
892 | /* Both real or both integers; use the one with greater precision. */ |
893 | ||
894 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
895 | return t1; | |
896 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
897 | return t2; | |
898 | ||
899 | /* Same precision. Prefer long longs to longs to ints when the | |
900 | same precision, following the C99 rules on integer type rank | |
901 | (which are equivalent to the C90 rules for C90 types). */ | |
902 | ||
903 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
904 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
905 | return long_long_unsigned_type_node; | |
906 | ||
907 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
908 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
909 | { | |
910 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
911 | return long_long_unsigned_type_node; | |
912 | else | |
c22cacf3 | 913 | return long_long_integer_type_node; |
10bc1b1b JM |
914 | } |
915 | ||
916 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
917 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
918 | return long_unsigned_type_node; | |
919 | ||
920 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
921 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
922 | { | |
923 | /* But preserve unsignedness from the other type, | |
924 | since long cannot hold all the values of an unsigned int. */ | |
925 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
926 | return long_unsigned_type_node; | |
927 | else | |
928 | return long_integer_type_node; | |
929 | } | |
930 | ||
931 | /* Likewise, prefer long double to double even if same size. */ | |
932 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
933 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
934 | return long_double_type_node; | |
935 | ||
2531a1d9 JR |
936 | /* Likewise, prefer double to float even if same size. |
937 | We got a couple of embedded targets with 32 bit doubles, and the | |
938 | pdp11 might have 64 bit floats. */ | |
939 | if (TYPE_MAIN_VARIANT (t1) == double_type_node | |
940 | || TYPE_MAIN_VARIANT (t2) == double_type_node) | |
941 | return double_type_node; | |
942 | ||
10bc1b1b JM |
943 | /* Otherwise prefer the unsigned one. */ |
944 | ||
945 | if (TYPE_UNSIGNED (t1)) | |
946 | return t1; | |
947 | else | |
948 | return t2; | |
949 | } | |
400fbf9f | 950 | \f |
5922c215 JM |
951 | /* Wrapper around c_common_type that is used by c-common.c and other |
952 | front end optimizations that remove promotions. ENUMERAL_TYPEs | |
b2232745 RS |
953 | are allowed here and are converted to their compatible integer types. |
954 | BOOLEAN_TYPEs are allowed here and return either boolean_type_node or | |
955 | preferably a non-Boolean type as the common type. */ | |
ccf7f880 JJ |
956 | tree |
957 | common_type (tree t1, tree t2) | |
958 | { | |
959 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
960 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); | |
961 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
962 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); | |
b2232745 RS |
963 | |
964 | /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ | |
965 | if (TREE_CODE (t1) == BOOLEAN_TYPE | |
966 | && TREE_CODE (t2) == BOOLEAN_TYPE) | |
967 | return boolean_type_node; | |
968 | ||
969 | /* If either type is BOOLEAN_TYPE, then return the other. */ | |
970 | if (TREE_CODE (t1) == BOOLEAN_TYPE) | |
971 | return t2; | |
972 | if (TREE_CODE (t2) == BOOLEAN_TYPE) | |
973 | return t1; | |
974 | ||
ccf7f880 JJ |
975 | return c_common_type (t1, t2); |
976 | } | |
f13c9b2c | 977 | |
400fbf9f JW |
978 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
979 | or various other operations. Return 2 if they are compatible | |
980 | but a warning may be needed if you use them together. */ | |
981 | ||
982 | int | |
132da1a5 | 983 | comptypes (tree type1, tree type2) |
f13c9b2c AP |
984 | { |
985 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
986 | int val; | |
987 | ||
dc5027f4 | 988 | val = comptypes_internal (type1, type2, NULL, NULL); |
744aa42f ILT |
989 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
990 | ||
991 | return val; | |
992 | } | |
993 | ||
994 | /* Like comptypes, but if it returns non-zero because enum and int are | |
995 | compatible, it sets *ENUM_AND_INT_P to true. */ | |
996 | ||
997 | static int | |
998 | comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) | |
999 | { | |
1000 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1001 | int val; | |
1002 | ||
dc5027f4 JM |
1003 | val = comptypes_internal (type1, type2, enum_and_int_p, NULL); |
1004 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
1005 | ||
1006 | return val; | |
1007 | } | |
1008 | ||
1009 | /* Like comptypes, but if it returns nonzero for different types, it | |
1010 | sets *DIFFERENT_TYPES_P to true. */ | |
1011 | ||
1012 | int | |
1013 | comptypes_check_different_types (tree type1, tree type2, | |
1014 | bool *different_types_p) | |
1015 | { | |
1016 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1017 | int val; | |
1018 | ||
1019 | val = comptypes_internal (type1, type2, NULL, different_types_p); | |
f13c9b2c | 1020 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
c22cacf3 | 1021 | |
f13c9b2c | 1022 | return val; |
c22cacf3 MS |
1023 | } |
1024 | \f | |
f13c9b2c AP |
1025 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
1026 | or various other operations. Return 2 if they are compatible | |
744aa42f ILT |
1027 | but a warning may be needed if you use them together. If |
1028 | ENUM_AND_INT_P is not NULL, and one type is an enum and the other a | |
1029 | compatible integer type, then this sets *ENUM_AND_INT_P to true; | |
dc5027f4 JM |
1030 | *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not |
1031 | NULL, and the types are compatible but different enough not to be | |
48b0b196 | 1032 | permitted in C11 typedef redeclarations, then this sets |
dc5027f4 JM |
1033 | *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to |
1034 | false, but may or may not be set if the types are incompatible. | |
1035 | This differs from comptypes, in that we don't free the seen | |
1036 | types. */ | |
f13c9b2c AP |
1037 | |
1038 | static int | |
dc5027f4 JM |
1039 | comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, |
1040 | bool *different_types_p) | |
400fbf9f | 1041 | { |
58f9752a KG |
1042 | const_tree t1 = type1; |
1043 | const_tree t2 = type2; | |
4b027d16 | 1044 | int attrval, val; |
400fbf9f JW |
1045 | |
1046 | /* Suppress errors caused by previously reported errors. */ | |
1047 | ||
8d47dfc5 RH |
1048 | if (t1 == t2 || !t1 || !t2 |
1049 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
400fbf9f JW |
1050 | return 1; |
1051 | ||
bca63328 JM |
1052 | /* Enumerated types are compatible with integer types, but this is |
1053 | not transitive: two enumerated types in the same translation unit | |
1054 | are compatible with each other only if they are the same type. */ | |
400fbf9f | 1055 | |
bca63328 | 1056 | if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) |
744aa42f ILT |
1057 | { |
1058 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); | |
dc5027f4 JM |
1059 | if (TREE_CODE (t2) != VOID_TYPE) |
1060 | { | |
1061 | if (enum_and_int_p != NULL) | |
1062 | *enum_and_int_p = true; | |
1063 | if (different_types_p != NULL) | |
1064 | *different_types_p = true; | |
1065 | } | |
744aa42f | 1066 | } |
bca63328 | 1067 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) |
744aa42f ILT |
1068 | { |
1069 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); | |
dc5027f4 JM |
1070 | if (TREE_CODE (t1) != VOID_TYPE) |
1071 | { | |
1072 | if (enum_and_int_p != NULL) | |
1073 | *enum_and_int_p = true; | |
1074 | if (different_types_p != NULL) | |
1075 | *different_types_p = true; | |
1076 | } | |
744aa42f | 1077 | } |
400fbf9f JW |
1078 | |
1079 | if (t1 == t2) | |
1080 | return 1; | |
1081 | ||
1082 | /* Different classes of types can't be compatible. */ | |
1083 | ||
3aeb3655 EC |
1084 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
1085 | return 0; | |
400fbf9f | 1086 | |
118a3a8b | 1087 | /* Qualifiers must match. C99 6.7.3p9 */ |
400fbf9f | 1088 | |
3932261a | 1089 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
400fbf9f JW |
1090 | return 0; |
1091 | ||
08632da2 RS |
1092 | /* Allow for two different type nodes which have essentially the same |
1093 | definition. Note that we already checked for equality of the type | |
38e01259 | 1094 | qualifiers (just above). */ |
400fbf9f | 1095 | |
46df2823 JM |
1096 | if (TREE_CODE (t1) != ARRAY_TYPE |
1097 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
400fbf9f JW |
1098 | return 1; |
1099 | ||
4b027d16 | 1100 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ |
ac9a30ae | 1101 | if (!(attrval = comp_type_attributes (t1, t2))) |
4b027d16 RK |
1102 | return 0; |
1103 | ||
1104 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1105 | val = 0; | |
1106 | ||
400fbf9f JW |
1107 | switch (TREE_CODE (t1)) |
1108 | { | |
1109 | case POINTER_TYPE: | |
106f5de5 UW |
1110 | /* Do not remove mode or aliasing information. */ |
1111 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1112 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) | |
1113 | break; | |
4b027d16 | 1114 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
744aa42f | 1115 | ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 | 1116 | enum_and_int_p, different_types_p)); |
4b027d16 | 1117 | break; |
400fbf9f JW |
1118 | |
1119 | case FUNCTION_TYPE: | |
dc5027f4 JM |
1120 | val = function_types_compatible_p (t1, t2, enum_and_int_p, |
1121 | different_types_p); | |
4b027d16 | 1122 | break; |
400fbf9f JW |
1123 | |
1124 | case ARRAY_TYPE: | |
1125 | { | |
400fbf9f JW |
1126 | tree d1 = TYPE_DOMAIN (t1); |
1127 | tree d2 = TYPE_DOMAIN (t2); | |
3f85558f RH |
1128 | bool d1_variable, d2_variable; |
1129 | bool d1_zero, d2_zero; | |
4b027d16 | 1130 | val = 1; |
400fbf9f JW |
1131 | |
1132 | /* Target types must match incl. qualifiers. */ | |
1133 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
744aa42f | 1134 | && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 JM |
1135 | enum_and_int_p, |
1136 | different_types_p))) | |
400fbf9f JW |
1137 | return 0; |
1138 | ||
dc5027f4 JM |
1139 | if (different_types_p != NULL |
1140 | && (d1 == 0) != (d2 == 0)) | |
1141 | *different_types_p = true; | |
400fbf9f | 1142 | /* Sizes must match unless one is missing or variable. */ |
3f85558f | 1143 | if (d1 == 0 || d2 == 0 || d1 == d2) |
4b027d16 | 1144 | break; |
400fbf9f | 1145 | |
3f75a254 JM |
1146 | d1_zero = !TYPE_MAX_VALUE (d1); |
1147 | d2_zero = !TYPE_MAX_VALUE (d2); | |
3f85558f | 1148 | |
3f75a254 | 1149 | d1_variable = (!d1_zero |
3f85558f RH |
1150 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST |
1151 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
3f75a254 | 1152 | d2_variable = (!d2_zero |
3f85558f RH |
1153 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST |
1154 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
1155 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
1156 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
3f85558f | 1157 | |
dc5027f4 JM |
1158 | if (different_types_p != NULL |
1159 | && d1_variable != d2_variable) | |
1160 | *different_types_p = true; | |
3f85558f RH |
1161 | if (d1_variable || d2_variable) |
1162 | break; | |
1163 | if (d1_zero && d2_zero) | |
1164 | break; | |
1165 | if (d1_zero || d2_zero | |
3f75a254 JM |
1166 | || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) |
1167 | || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) | |
05bccae2 RK |
1168 | val = 0; |
1169 | ||
c22cacf3 | 1170 | break; |
400fbf9f JW |
1171 | } |
1172 | ||
d1bd0ded | 1173 | case ENUMERAL_TYPE: |
58393038 | 1174 | case RECORD_TYPE: |
d1bd0ded | 1175 | case UNION_TYPE: |
766beae1 | 1176 | if (val != 1 && !same_translation_unit_p (t1, t2)) |
c22cacf3 | 1177 | { |
fcb99e7b JJ |
1178 | tree a1 = TYPE_ATTRIBUTES (t1); |
1179 | tree a2 = TYPE_ATTRIBUTES (t2); | |
1180 | ||
1181 | if (! attribute_list_contained (a1, a2) | |
1182 | && ! attribute_list_contained (a2, a1)) | |
1183 | break; | |
1184 | ||
f13c9b2c | 1185 | if (attrval != 2) |
dc5027f4 JM |
1186 | return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, |
1187 | different_types_p); | |
1188 | val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, | |
1189 | different_types_p); | |
f13c9b2c | 1190 | } |
4b027d16 | 1191 | break; |
e9a25f70 | 1192 | |
62e1dfcf | 1193 | case VECTOR_TYPE: |
744aa42f ILT |
1194 | val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) |
1195 | && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
dc5027f4 | 1196 | enum_and_int_p, different_types_p)); |
62e1dfcf NC |
1197 | break; |
1198 | ||
e9a25f70 JL |
1199 | default: |
1200 | break; | |
400fbf9f | 1201 | } |
4b027d16 | 1202 | return attrval == 2 && val == 1 ? 2 : val; |
400fbf9f JW |
1203 | } |
1204 | ||
36c5e70a BE |
1205 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring |
1206 | their qualifiers, except for named address spaces. If the pointers point to | |
1207 | different named addresses, then we must determine if one address space is a | |
1208 | subset of the other. */ | |
400fbf9f JW |
1209 | |
1210 | static int | |
744aa42f | 1211 | comp_target_types (location_t location, tree ttl, tree ttr) |
400fbf9f | 1212 | { |
392202b0 | 1213 | int val; |
768952be | 1214 | int val_ped; |
36c5e70a BE |
1215 | tree mvl = TREE_TYPE (ttl); |
1216 | tree mvr = TREE_TYPE (ttr); | |
1217 | addr_space_t asl = TYPE_ADDR_SPACE (mvl); | |
1218 | addr_space_t asr = TYPE_ADDR_SPACE (mvr); | |
1219 | addr_space_t as_common; | |
744aa42f | 1220 | bool enum_and_int_p; |
8b40563c | 1221 | |
36c5e70a BE |
1222 | /* Fail if pointers point to incompatible address spaces. */ |
1223 | if (!addr_space_superset (asl, asr, &as_common)) | |
1224 | return 0; | |
1225 | ||
768952be MU |
1226 | /* For pedantic record result of comptypes on arrays before losing |
1227 | qualifiers on the element type below. */ | |
1228 | val_ped = 1; | |
1229 | ||
1230 | if (TREE_CODE (mvl) == ARRAY_TYPE | |
1231 | && TREE_CODE (mvr) == ARRAY_TYPE) | |
1232 | val_ped = comptypes (mvl, mvr); | |
1233 | ||
1234 | /* Qualifiers on element types of array types that are | |
1235 | pointer targets are lost by taking their TYPE_MAIN_VARIANT. */ | |
1236 | ||
1237 | mvl = (TYPE_ATOMIC (strip_array_types (mvl)) | |
1238 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC) | |
1239 | : TYPE_MAIN_VARIANT (mvl)); | |
1240 | ||
1241 | mvr = (TYPE_ATOMIC (strip_array_types (mvr)) | |
1242 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC) | |
1243 | : TYPE_MAIN_VARIANT (mvr)); | |
1244 | ||
744aa42f ILT |
1245 | enum_and_int_p = false; |
1246 | val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); | |
8b40563c | 1247 | |
768952be MU |
1248 | if (val == 1 && val_ped != 1) |
1249 | pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers " | |
1250 | "are incompatible in ISO C"); | |
1251 | ||
fcf73884 | 1252 | if (val == 2) |
c1771a20 | 1253 | pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); |
744aa42f ILT |
1254 | |
1255 | if (val == 1 && enum_and_int_p && warn_cxx_compat) | |
1256 | warning_at (location, OPT_Wc___compat, | |
1257 | "pointer target types incompatible in C++"); | |
1258 | ||
400fbf9f JW |
1259 | return val; |
1260 | } | |
1261 | \f | |
1262 | /* Subroutines of `comptypes'. */ | |
1263 | ||
f75fbaf7 ZW |
1264 | /* Determine whether two trees derive from the same translation unit. |
1265 | If the CONTEXT chain ends in a null, that tree's context is still | |
1266 | being parsed, so if two trees have context chains ending in null, | |
766beae1 | 1267 | they're in the same translation unit. */ |
f75fbaf7 | 1268 | int |
58f9752a | 1269 | same_translation_unit_p (const_tree t1, const_tree t2) |
766beae1 ZW |
1270 | { |
1271 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
1272 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
1273 | { | |
6615c446 JO |
1274 | case tcc_declaration: |
1275 | t1 = DECL_CONTEXT (t1); break; | |
1276 | case tcc_type: | |
1277 | t1 = TYPE_CONTEXT (t1); break; | |
1278 | case tcc_exceptional: | |
1279 | t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ | |
366de0ce | 1280 | default: gcc_unreachable (); |
766beae1 ZW |
1281 | } |
1282 | ||
1283 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
1284 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
1285 | { | |
6615c446 JO |
1286 | case tcc_declaration: |
1287 | t2 = DECL_CONTEXT (t2); break; | |
1288 | case tcc_type: | |
1289 | t2 = TYPE_CONTEXT (t2); break; | |
1290 | case tcc_exceptional: | |
1291 | t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ | |
366de0ce | 1292 | default: gcc_unreachable (); |
766beae1 ZW |
1293 | } |
1294 | ||
1295 | return t1 == t2; | |
1296 | } | |
1297 | ||
f13c9b2c | 1298 | /* Allocate the seen two types, assuming that they are compatible. */ |
d1bd0ded | 1299 | |
f13c9b2c | 1300 | static struct tagged_tu_seen_cache * |
58f9752a | 1301 | alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) |
f13c9b2c | 1302 | { |
cceb1885 | 1303 | struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); |
f13c9b2c AP |
1304 | tu->next = tagged_tu_seen_base; |
1305 | tu->t1 = t1; | |
1306 | tu->t2 = t2; | |
c22cacf3 | 1307 | |
f13c9b2c | 1308 | tagged_tu_seen_base = tu; |
c22cacf3 | 1309 | |
f13c9b2c AP |
1310 | /* The C standard says that two structures in different translation |
1311 | units are compatible with each other only if the types of their | |
1312 | fields are compatible (among other things). We assume that they | |
1313 | are compatible until proven otherwise when building the cache. | |
1314 | An example where this can occur is: | |
1315 | struct a | |
1316 | { | |
1317 | struct a *next; | |
1318 | }; | |
1319 | If we are comparing this against a similar struct in another TU, | |
c83eecad | 1320 | and did not assume they were compatible, we end up with an infinite |
f13c9b2c AP |
1321 | loop. */ |
1322 | tu->val = 1; | |
1323 | return tu; | |
1324 | } | |
d1bd0ded | 1325 | |
f13c9b2c | 1326 | /* Free the seen types until we get to TU_TIL. */ |
d1bd0ded | 1327 | |
f13c9b2c AP |
1328 | static void |
1329 | free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) | |
1330 | { | |
1331 | const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; | |
1332 | while (tu != tu_til) | |
1333 | { | |
741ac903 KG |
1334 | const struct tagged_tu_seen_cache *const tu1 |
1335 | = (const struct tagged_tu_seen_cache *) tu; | |
f13c9b2c | 1336 | tu = tu1->next; |
b1d5455a | 1337 | free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); |
f13c9b2c AP |
1338 | } |
1339 | tagged_tu_seen_base = tu_til; | |
1340 | } | |
d1bd0ded GK |
1341 | |
1342 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
1343 | compatible. If the two types are not the same (which has been | |
1344 | checked earlier), this can only happen when multiple translation | |
1345 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
dc5027f4 JM |
1346 | rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1347 | comptypes_internal. */ | |
d1bd0ded GK |
1348 | |
1349 | static int | |
744aa42f | 1350 | tagged_types_tu_compatible_p (const_tree t1, const_tree t2, |
dc5027f4 | 1351 | bool *enum_and_int_p, bool *different_types_p) |
d1bd0ded GK |
1352 | { |
1353 | tree s1, s2; | |
1354 | bool needs_warning = false; | |
3aeb3655 | 1355 | |
d1bd0ded GK |
1356 | /* We have to verify that the tags of the types are the same. This |
1357 | is harder than it looks because this may be a typedef, so we have | |
1358 | to go look at the original type. It may even be a typedef of a | |
6de9cd9a DN |
1359 | typedef... |
1360 | In the case of compiler-created builtin structs the TYPE_DECL | |
1361 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
dea984dc ILT |
1362 | while (TYPE_NAME (t1) |
1363 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
1364 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
d1bd0ded GK |
1365 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); |
1366 | ||
dea984dc ILT |
1367 | while (TYPE_NAME (t2) |
1368 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
1369 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
d1bd0ded GK |
1370 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); |
1371 | ||
1372 | /* C90 didn't have the requirement that the two tags be the same. */ | |
1373 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
1374 | return 0; | |
3aeb3655 | 1375 | |
d1bd0ded GK |
1376 | /* C90 didn't say what happened if one or both of the types were |
1377 | incomplete; we choose to follow C99 rules here, which is that they | |
1378 | are compatible. */ | |
1379 | if (TYPE_SIZE (t1) == NULL | |
1380 | || TYPE_SIZE (t2) == NULL) | |
1381 | return 1; | |
3aeb3655 | 1382 | |
d1bd0ded | 1383 | { |
f13c9b2c | 1384 | const struct tagged_tu_seen_cache * tts_i; |
d1bd0ded GK |
1385 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) |
1386 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
f13c9b2c | 1387 | return tts_i->val; |
d1bd0ded | 1388 | } |
3aeb3655 | 1389 | |
d1bd0ded GK |
1390 | switch (TREE_CODE (t1)) |
1391 | { | |
1392 | case ENUMERAL_TYPE: | |
1393 | { | |
f13c9b2c | 1394 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
c22cacf3 MS |
1395 | /* Speed up the case where the type values are in the same order. */ |
1396 | tree tv1 = TYPE_VALUES (t1); | |
1397 | tree tv2 = TYPE_VALUES (t2); | |
3aeb3655 | 1398 | |
c22cacf3 | 1399 | if (tv1 == tv2) |
f13c9b2c AP |
1400 | { |
1401 | return 1; | |
1402 | } | |
3aeb3655 | 1403 | |
c22cacf3 MS |
1404 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) |
1405 | { | |
1406 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) | |
1407 | break; | |
1408 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
f13c9b2c | 1409 | { |
c22cacf3 | 1410 | tu->val = 0; |
f13c9b2c AP |
1411 | return 0; |
1412 | } | |
c22cacf3 | 1413 | } |
3aeb3655 | 1414 | |
c22cacf3 | 1415 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) |
f13c9b2c AP |
1416 | { |
1417 | return 1; | |
1418 | } | |
c22cacf3 | 1419 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) |
f13c9b2c AP |
1420 | { |
1421 | tu->val = 0; | |
1422 | return 0; | |
1423 | } | |
3aeb3655 | 1424 | |
d1bd0ded | 1425 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) |
f13c9b2c AP |
1426 | { |
1427 | tu->val = 0; | |
1428 | return 0; | |
1429 | } | |
3aeb3655 | 1430 | |
d1bd0ded GK |
1431 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) |
1432 | { | |
1433 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
1434 | if (s2 == NULL | |
1435 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
f13c9b2c AP |
1436 | { |
1437 | tu->val = 0; | |
1438 | return 0; | |
1439 | } | |
d1bd0ded GK |
1440 | } |
1441 | return 1; | |
1442 | } | |
1443 | ||
1444 | case UNION_TYPE: | |
1445 | { | |
f13c9b2c | 1446 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
d1bd0ded | 1447 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) |
f13c9b2c AP |
1448 | { |
1449 | tu->val = 0; | |
1450 | return 0; | |
1451 | } | |
c22cacf3 | 1452 | |
f13c9b2c AP |
1453 | /* Speed up the common case where the fields are in the same order. */ |
1454 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; | |
910ad8de | 1455 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
f13c9b2c AP |
1456 | { |
1457 | int result; | |
c22cacf3 | 1458 | |
3ae4d3cc | 1459 | if (DECL_NAME (s1) != DECL_NAME (s2)) |
f13c9b2c | 1460 | break; |
744aa42f | 1461 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1462 | enum_and_int_p, different_types_p); |
3ae4d3cc AO |
1463 | |
1464 | if (result != 1 && !DECL_NAME (s1)) | |
1465 | break; | |
f13c9b2c AP |
1466 | if (result == 0) |
1467 | { | |
1468 | tu->val = 0; | |
1469 | return 0; | |
1470 | } | |
1471 | if (result == 2) | |
1472 | needs_warning = true; | |
1473 | ||
1474 | if (TREE_CODE (s1) == FIELD_DECL | |
1475 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1476 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1477 | { | |
1478 | tu->val = 0; | |
1479 | return 0; | |
1480 | } | |
1481 | } | |
1482 | if (!s1 && !s2) | |
1483 | { | |
1484 | tu->val = needs_warning ? 2 : 1; | |
1485 | return tu->val; | |
1486 | } | |
d1bd0ded | 1487 | |
910ad8de | 1488 | for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1)) |
d1bd0ded GK |
1489 | { |
1490 | bool ok = false; | |
3aeb3655 | 1491 | |
910ad8de | 1492 | for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2)) |
3ae4d3cc AO |
1493 | if (DECL_NAME (s1) == DECL_NAME (s2)) |
1494 | { | |
1495 | int result; | |
1496 | ||
744aa42f | 1497 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 JM |
1498 | enum_and_int_p, |
1499 | different_types_p); | |
3ae4d3cc AO |
1500 | |
1501 | if (result != 1 && !DECL_NAME (s1)) | |
1502 | continue; | |
1503 | if (result == 0) | |
1504 | { | |
1505 | tu->val = 0; | |
1506 | return 0; | |
1507 | } | |
1508 | if (result == 2) | |
1509 | needs_warning = true; | |
1510 | ||
1511 | if (TREE_CODE (s1) == FIELD_DECL | |
1512 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1513 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
d1bd0ded | 1514 | break; |
3ae4d3cc AO |
1515 | |
1516 | ok = true; | |
1517 | break; | |
1518 | } | |
3f75a254 | 1519 | if (!ok) |
f13c9b2c AP |
1520 | { |
1521 | tu->val = 0; | |
1522 | return 0; | |
1523 | } | |
d1bd0ded | 1524 | } |
f13c9b2c AP |
1525 | tu->val = needs_warning ? 2 : 10; |
1526 | return tu->val; | |
d1bd0ded GK |
1527 | } |
1528 | ||
1529 | case RECORD_TYPE: | |
1530 | { | |
c22cacf3 | 1531 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
3aeb3655 EC |
1532 | |
1533 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
d1bd0ded | 1534 | s1 && s2; |
910ad8de | 1535 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
d1bd0ded GK |
1536 | { |
1537 | int result; | |
1538 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
1539 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
1540 | break; | |
744aa42f | 1541 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1542 | enum_and_int_p, different_types_p); |
d1bd0ded GK |
1543 | if (result == 0) |
1544 | break; | |
1545 | if (result == 2) | |
1546 | needs_warning = true; | |
3aeb3655 | 1547 | |
d1bd0ded GK |
1548 | if (TREE_CODE (s1) == FIELD_DECL |
1549 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1550 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1551 | break; | |
1552 | } | |
d1bd0ded | 1553 | if (s1 && s2) |
f13c9b2c AP |
1554 | tu->val = 0; |
1555 | else | |
1556 | tu->val = needs_warning ? 2 : 1; | |
1557 | return tu->val; | |
d1bd0ded GK |
1558 | } |
1559 | ||
1560 | default: | |
366de0ce | 1561 | gcc_unreachable (); |
d1bd0ded GK |
1562 | } |
1563 | } | |
1564 | ||
400fbf9f JW |
1565 | /* Return 1 if two function types F1 and F2 are compatible. |
1566 | If either type specifies no argument types, | |
1567 | the other must specify a fixed number of self-promoting arg types. | |
2f6e4e97 | 1568 | Otherwise, if one type specifies only the number of arguments, |
400fbf9f | 1569 | the other must specify that number of self-promoting arg types. |
744aa42f | 1570 | Otherwise, the argument types must match. |
dc5027f4 | 1571 | ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */ |
400fbf9f JW |
1572 | |
1573 | static int | |
744aa42f | 1574 | function_types_compatible_p (const_tree f1, const_tree f2, |
dc5027f4 | 1575 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1576 | { |
1577 | tree args1, args2; | |
1578 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1579 | int val = 1; | |
1580 | int val1; | |
a6fdc086 GK |
1581 | tree ret1, ret2; |
1582 | ||
1583 | ret1 = TREE_TYPE (f1); | |
1584 | ret2 = TREE_TYPE (f2); | |
1585 | ||
e508a019 JM |
1586 | /* 'volatile' qualifiers on a function's return type used to mean |
1587 | the function is noreturn. */ | |
1588 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
509c9d60 | 1589 | pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); |
a6fdc086 GK |
1590 | if (TYPE_VOLATILE (ret1)) |
1591 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
1592 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
1593 | if (TYPE_VOLATILE (ret2)) | |
1594 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
1595 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
dc5027f4 | 1596 | val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p); |
a6fdc086 | 1597 | if (val == 0) |
400fbf9f JW |
1598 | return 0; |
1599 | ||
1600 | args1 = TYPE_ARG_TYPES (f1); | |
1601 | args2 = TYPE_ARG_TYPES (f2); | |
1602 | ||
dc5027f4 JM |
1603 | if (different_types_p != NULL |
1604 | && (args1 == 0) != (args2 == 0)) | |
1605 | *different_types_p = true; | |
1606 | ||
400fbf9f JW |
1607 | /* An unspecified parmlist matches any specified parmlist |
1608 | whose argument types don't need default promotions. */ | |
1609 | ||
1610 | if (args1 == 0) | |
1611 | { | |
1612 | if (!self_promoting_args_p (args2)) | |
1613 | return 0; | |
1614 | /* If one of these types comes from a non-prototype fn definition, | |
1615 | compare that with the other type's arglist. | |
3176a0c2 | 1616 | If they don't match, ask for a warning (but no error). */ |
400fbf9f | 1617 | if (TYPE_ACTUAL_ARG_TYPES (f1) |
744aa42f | 1618 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), |
dc5027f4 | 1619 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1620 | val = 2; |
1621 | return val; | |
1622 | } | |
1623 | if (args2 == 0) | |
1624 | { | |
1625 | if (!self_promoting_args_p (args1)) | |
1626 | return 0; | |
1627 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
744aa42f | 1628 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), |
dc5027f4 | 1629 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1630 | val = 2; |
1631 | return val; | |
1632 | } | |
1633 | ||
1634 | /* Both types have argument lists: compare them and propagate results. */ | |
dc5027f4 JM |
1635 | val1 = type_lists_compatible_p (args1, args2, enum_and_int_p, |
1636 | different_types_p); | |
400fbf9f JW |
1637 | return val1 != 1 ? val1 : val; |
1638 | } | |
1639 | ||
744aa42f ILT |
1640 | /* Check two lists of types for compatibility, returning 0 for |
1641 | incompatible, 1 for compatible, or 2 for compatible with | |
dc5027f4 JM |
1642 | warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1643 | comptypes_internal. */ | |
400fbf9f JW |
1644 | |
1645 | static int | |
744aa42f | 1646 | type_lists_compatible_p (const_tree args1, const_tree args2, |
dc5027f4 | 1647 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1648 | { |
1649 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1650 | int val = 1; | |
9d5f3e49 | 1651 | int newval = 0; |
400fbf9f JW |
1652 | |
1653 | while (1) | |
1654 | { | |
46df2823 | 1655 | tree a1, mv1, a2, mv2; |
400fbf9f JW |
1656 | if (args1 == 0 && args2 == 0) |
1657 | return val; | |
1658 | /* If one list is shorter than the other, | |
1659 | they fail to match. */ | |
1660 | if (args1 == 0 || args2 == 0) | |
1661 | return 0; | |
46df2823 JM |
1662 | mv1 = a1 = TREE_VALUE (args1); |
1663 | mv2 = a2 = TREE_VALUE (args2); | |
1664 | if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) | |
267bac10 JM |
1665 | mv1 = (TYPE_ATOMIC (mv1) |
1666 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1), | |
1667 | TYPE_QUAL_ATOMIC) | |
1668 | : TYPE_MAIN_VARIANT (mv1)); | |
46df2823 | 1669 | if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) |
267bac10 JM |
1670 | mv2 = (TYPE_ATOMIC (mv2) |
1671 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2), | |
1672 | TYPE_QUAL_ATOMIC) | |
1673 | : TYPE_MAIN_VARIANT (mv2)); | |
400fbf9f JW |
1674 | /* A null pointer instead of a type |
1675 | means there is supposed to be an argument | |
1676 | but nothing is specified about what type it has. | |
1677 | So match anything that self-promotes. */ | |
dc5027f4 JM |
1678 | if (different_types_p != NULL |
1679 | && (a1 == 0) != (a2 == 0)) | |
1680 | *different_types_p = true; | |
46df2823 | 1681 | if (a1 == 0) |
400fbf9f | 1682 | { |
46df2823 | 1683 | if (c_type_promotes_to (a2) != a2) |
400fbf9f JW |
1684 | return 0; |
1685 | } | |
46df2823 | 1686 | else if (a2 == 0) |
400fbf9f | 1687 | { |
46df2823 | 1688 | if (c_type_promotes_to (a1) != a1) |
400fbf9f JW |
1689 | return 0; |
1690 | } | |
8f5b6d29 | 1691 | /* If one of the lists has an error marker, ignore this arg. */ |
46df2823 JM |
1692 | else if (TREE_CODE (a1) == ERROR_MARK |
1693 | || TREE_CODE (a2) == ERROR_MARK) | |
8f5b6d29 | 1694 | ; |
dc5027f4 JM |
1695 | else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p, |
1696 | different_types_p))) | |
400fbf9f | 1697 | { |
dc5027f4 JM |
1698 | if (different_types_p != NULL) |
1699 | *different_types_p = true; | |
400fbf9f JW |
1700 | /* Allow wait (union {union wait *u; int *i} *) |
1701 | and wait (union wait *) to be compatible. */ | |
46df2823 JM |
1702 | if (TREE_CODE (a1) == UNION_TYPE |
1703 | && (TYPE_NAME (a1) == 0 | |
ebf0bf7f | 1704 | || TYPE_TRANSPARENT_AGGR (a1)) |
46df2823 JM |
1705 | && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST |
1706 | && tree_int_cst_equal (TYPE_SIZE (a1), | |
1707 | TYPE_SIZE (a2))) | |
400fbf9f JW |
1708 | { |
1709 | tree memb; | |
46df2823 | 1710 | for (memb = TYPE_FIELDS (a1); |
910ad8de | 1711 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1712 | { |
1713 | tree mv3 = TREE_TYPE (memb); | |
1714 | if (mv3 && mv3 != error_mark_node | |
1715 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
267bac10 JM |
1716 | mv3 = (TYPE_ATOMIC (mv3) |
1717 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), | |
1718 | TYPE_QUAL_ATOMIC) | |
1719 | : TYPE_MAIN_VARIANT (mv3)); | |
dc5027f4 JM |
1720 | if (comptypes_internal (mv3, mv2, enum_and_int_p, |
1721 | different_types_p)) | |
58cb41e6 JJ |
1722 | break; |
1723 | } | |
400fbf9f JW |
1724 | if (memb == 0) |
1725 | return 0; | |
1726 | } | |
46df2823 JM |
1727 | else if (TREE_CODE (a2) == UNION_TYPE |
1728 | && (TYPE_NAME (a2) == 0 | |
ebf0bf7f | 1729 | || TYPE_TRANSPARENT_AGGR (a2)) |
46df2823 JM |
1730 | && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST |
1731 | && tree_int_cst_equal (TYPE_SIZE (a2), | |
1732 | TYPE_SIZE (a1))) | |
400fbf9f JW |
1733 | { |
1734 | tree memb; | |
46df2823 | 1735 | for (memb = TYPE_FIELDS (a2); |
910ad8de | 1736 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1737 | { |
1738 | tree mv3 = TREE_TYPE (memb); | |
1739 | if (mv3 && mv3 != error_mark_node | |
1740 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
267bac10 JM |
1741 | mv3 = (TYPE_ATOMIC (mv3) |
1742 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), | |
1743 | TYPE_QUAL_ATOMIC) | |
1744 | : TYPE_MAIN_VARIANT (mv3)); | |
dc5027f4 JM |
1745 | if (comptypes_internal (mv3, mv1, enum_and_int_p, |
1746 | different_types_p)) | |
58cb41e6 JJ |
1747 | break; |
1748 | } | |
400fbf9f JW |
1749 | if (memb == 0) |
1750 | return 0; | |
1751 | } | |
1752 | else | |
1753 | return 0; | |
1754 | } | |
1755 | ||
1756 | /* comptypes said ok, but record if it said to warn. */ | |
1757 | if (newval > val) | |
1758 | val = newval; | |
1759 | ||
1760 | args1 = TREE_CHAIN (args1); | |
1761 | args2 = TREE_CHAIN (args2); | |
1762 | } | |
1763 | } | |
400fbf9f | 1764 | \f |
a0e24419 MP |
1765 | /* Compute the size to increment a pointer by. When a function type or void |
1766 | type or incomplete type is passed, size_one_node is returned. | |
1767 | This function does not emit any diagnostics; the caller is responsible | |
1768 | for that. */ | |
400fbf9f | 1769 | |
4e2fb7de | 1770 | static tree |
58f9752a | 1771 | c_size_in_bytes (const_tree type) |
400fbf9f JW |
1772 | { |
1773 | enum tree_code code = TREE_CODE (type); | |
1774 | ||
a0e24419 MP |
1775 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK |
1776 | || !COMPLETE_TYPE_P (type)) | |
fed3cef0 RK |
1777 | return size_one_node; |
1778 | ||
400fbf9f | 1779 | /* Convert in case a char is more than one unit. */ |
db3927fb AH |
1780 | return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
1781 | size_int (TYPE_PRECISION (char_type_node) | |
1782 | / BITS_PER_UNIT)); | |
400fbf9f | 1783 | } |
400fbf9f | 1784 | \f |
400fbf9f JW |
1785 | /* Return either DECL or its known constant value (if it has one). */ |
1786 | ||
56cb9733 | 1787 | tree |
2f6e4e97 | 1788 | decl_constant_value (tree decl) |
400fbf9f | 1789 | { |
a7c1916a | 1790 | if (/* Don't change a variable array bound or initial value to a constant |
4f976745 RK |
1791 | in a place where a variable is invalid. Note that DECL_INITIAL |
1792 | isn't valid for a PARM_DECL. */ | |
a7c1916a | 1793 | current_function_decl != 0 |
4f976745 | 1794 | && TREE_CODE (decl) != PARM_DECL |
3f75a254 | 1795 | && !TREE_THIS_VOLATILE (decl) |
83bab8db | 1796 | && TREE_READONLY (decl) |
400fbf9f JW |
1797 | && DECL_INITIAL (decl) != 0 |
1798 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1799 | /* This is invalid if initial value is not constant. | |
1800 | If it has either a function call, a memory reference, | |
1801 | or a variable, then re-evaluating it could give different results. */ | |
1802 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1803 | /* Check for cases where this is sub-optimal, even though valid. */ | |
2f74f7e9 | 1804 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
400fbf9f JW |
1805 | return DECL_INITIAL (decl); |
1806 | return decl; | |
1807 | } | |
1808 | ||
f2a71bbc JM |
1809 | /* Convert the array expression EXP to a pointer. */ |
1810 | static tree | |
c2255bc4 | 1811 | array_to_pointer_conversion (location_t loc, tree exp) |
207bf485 | 1812 | { |
f2a71bbc | 1813 | tree orig_exp = exp; |
207bf485 | 1814 | tree type = TREE_TYPE (exp); |
f2a71bbc JM |
1815 | tree adr; |
1816 | tree restype = TREE_TYPE (type); | |
1817 | tree ptrtype; | |
207bf485 | 1818 | |
f2a71bbc | 1819 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
207bf485 | 1820 | |
f2a71bbc | 1821 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1822 | |
487a92fe JM |
1823 | if (TREE_NO_WARNING (orig_exp)) |
1824 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1825 | |
f2a71bbc JM |
1826 | ptrtype = build_pointer_type (restype); |
1827 | ||
22d03525 | 1828 | if (INDIRECT_REF_P (exp)) |
f2a71bbc JM |
1829 | return convert (ptrtype, TREE_OPERAND (exp, 0)); |
1830 | ||
1f37c583 JM |
1831 | /* In C++ array compound literals are temporary objects unless they are |
1832 | const or appear in namespace scope, so they are destroyed too soon | |
1833 | to use them for much of anything (c++/53220). */ | |
1834 | if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) | |
1835 | { | |
1836 | tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
1837 | if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) | |
1838 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, | |
1839 | "converting an array compound literal to a pointer " | |
1840 | "is ill-formed in C++"); | |
1841 | } | |
1842 | ||
c2255bc4 | 1843 | adr = build_unary_op (loc, ADDR_EXPR, exp, 1); |
f2a71bbc JM |
1844 | return convert (ptrtype, adr); |
1845 | } | |
207bf485 | 1846 | |
f2a71bbc JM |
1847 | /* Convert the function expression EXP to a pointer. */ |
1848 | static tree | |
c2255bc4 | 1849 | function_to_pointer_conversion (location_t loc, tree exp) |
f2a71bbc JM |
1850 | { |
1851 | tree orig_exp = exp; | |
207bf485 | 1852 | |
f2a71bbc | 1853 | gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); |
207bf485 | 1854 | |
f2a71bbc | 1855 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1856 | |
f2a71bbc JM |
1857 | if (TREE_NO_WARNING (orig_exp)) |
1858 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1859 | |
c2255bc4 | 1860 | return build_unary_op (loc, ADDR_EXPR, exp, 0); |
f2a71bbc | 1861 | } |
207bf485 | 1862 | |
ebfbbdc5 JJ |
1863 | /* Mark EXP as read, not just set, for set but not used -Wunused |
1864 | warning purposes. */ | |
1865 | ||
1866 | void | |
1867 | mark_exp_read (tree exp) | |
1868 | { | |
1869 | switch (TREE_CODE (exp)) | |
1870 | { | |
1871 | case VAR_DECL: | |
1872 | case PARM_DECL: | |
1873 | DECL_READ_P (exp) = 1; | |
1874 | break; | |
1875 | case ARRAY_REF: | |
1876 | case COMPONENT_REF: | |
1877 | case MODIFY_EXPR: | |
1878 | case REALPART_EXPR: | |
1879 | case IMAGPART_EXPR: | |
1880 | CASE_CONVERT: | |
1881 | case ADDR_EXPR: | |
1882 | mark_exp_read (TREE_OPERAND (exp, 0)); | |
1883 | break; | |
1884 | case COMPOUND_EXPR: | |
82c3c067 | 1885 | case C_MAYBE_CONST_EXPR: |
ebfbbdc5 JJ |
1886 | mark_exp_read (TREE_OPERAND (exp, 1)); |
1887 | break; | |
1888 | default: | |
1889 | break; | |
1890 | } | |
1891 | } | |
1892 | ||
f2a71bbc JM |
1893 | /* Perform the default conversion of arrays and functions to pointers. |
1894 | Return the result of converting EXP. For any other expression, just | |
c2255bc4 AH |
1895 | return EXP. |
1896 | ||
1897 | LOC is the location of the expression. */ | |
f2a71bbc JM |
1898 | |
1899 | struct c_expr | |
c2255bc4 | 1900 | default_function_array_conversion (location_t loc, struct c_expr exp) |
f2a71bbc JM |
1901 | { |
1902 | tree orig_exp = exp.value; | |
1903 | tree type = TREE_TYPE (exp.value); | |
1904 | enum tree_code code = TREE_CODE (type); | |
1905 | ||
1906 | switch (code) | |
1907 | { | |
1908 | case ARRAY_TYPE: | |
1909 | { | |
1910 | bool not_lvalue = false; | |
1911 | bool lvalue_array_p; | |
1912 | ||
1913 | while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR | |
1043771b | 1914 | || CONVERT_EXPR_P (exp.value)) |
f2a71bbc JM |
1915 | && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) |
1916 | { | |
1917 | if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) | |
1918 | not_lvalue = true; | |
1919 | exp.value = TREE_OPERAND (exp.value, 0); | |
1920 | } | |
1921 | ||
1922 | if (TREE_NO_WARNING (orig_exp)) | |
1923 | TREE_NO_WARNING (exp.value) = 1; | |
1924 | ||
1925 | lvalue_array_p = !not_lvalue && lvalue_p (exp.value); | |
1926 | if (!flag_isoc99 && !lvalue_array_p) | |
1927 | { | |
1928 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1929 | Normally, using such an array would be invalid; but it can | |
1930 | be used correctly inside sizeof or as a statement expression. | |
1931 | Thus, do not give an error here; an error will result later. */ | |
1932 | return exp; | |
1933 | } | |
1934 | ||
c2255bc4 | 1935 | exp.value = array_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1936 | } |
1937 | break; | |
1938 | case FUNCTION_TYPE: | |
c2255bc4 | 1939 | exp.value = function_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1940 | break; |
1941 | default: | |
f2a71bbc | 1942 | break; |
207bf485 | 1943 | } |
f2a71bbc | 1944 | |
207bf485 JM |
1945 | return exp; |
1946 | } | |
1947 | ||
ebfbbdc5 JJ |
1948 | struct c_expr |
1949 | default_function_array_read_conversion (location_t loc, struct c_expr exp) | |
1950 | { | |
1951 | mark_exp_read (exp.value); | |
1952 | return default_function_array_conversion (loc, exp); | |
1953 | } | |
522ddfa2 | 1954 | |
267bac10 JM |
1955 | /* Return whether EXPR should be treated as an atomic lvalue for the |
1956 | purposes of load and store handling. */ | |
1957 | ||
1958 | static bool | |
1959 | really_atomic_lvalue (tree expr) | |
1960 | { | |
7a0ca710 | 1961 | if (error_operand_p (expr)) |
267bac10 JM |
1962 | return false; |
1963 | if (!TYPE_ATOMIC (TREE_TYPE (expr))) | |
1964 | return false; | |
1965 | if (!lvalue_p (expr)) | |
1966 | return false; | |
1967 | ||
1968 | /* Ignore _Atomic on register variables, since their addresses can't | |
1969 | be taken so (a) atomicity is irrelevant and (b) the normal atomic | |
1970 | sequences wouldn't work. Ignore _Atomic on structures containing | |
1971 | bit-fields, since accessing elements of atomic structures or | |
1972 | unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if | |
1973 | it's undefined at translation time or execution time, and the | |
1974 | normal atomic sequences again wouldn't work. */ | |
1975 | while (handled_component_p (expr)) | |
1976 | { | |
1977 | if (TREE_CODE (expr) == COMPONENT_REF | |
1978 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
1979 | return false; | |
1980 | expr = TREE_OPERAND (expr, 0); | |
1981 | } | |
1982 | if (DECL_P (expr) && C_DECL_REGISTER (expr)) | |
1983 | return false; | |
1984 | return true; | |
1985 | } | |
1986 | ||
1987 | /* Convert expression EXP (location LOC) from lvalue to rvalue, | |
1988 | including converting functions and arrays to pointers if CONVERT_P. | |
1989 | If READ_P, also mark the expression as having been read. */ | |
1990 | ||
1991 | struct c_expr | |
1992 | convert_lvalue_to_rvalue (location_t loc, struct c_expr exp, | |
1993 | bool convert_p, bool read_p) | |
1994 | { | |
1995 | if (read_p) | |
1996 | mark_exp_read (exp.value); | |
1997 | if (convert_p) | |
1998 | exp = default_function_array_conversion (loc, exp); | |
1999 | if (really_atomic_lvalue (exp.value)) | |
2000 | { | |
2001 | vec<tree, va_gc> *params; | |
2002 | tree nonatomic_type, tmp, tmp_addr, fndecl, func_call; | |
2003 | tree expr_type = TREE_TYPE (exp.value); | |
2004 | tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0); | |
2005 | tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); | |
2006 | ||
2007 | gcc_assert (TYPE_ATOMIC (expr_type)); | |
2008 | ||
2009 | /* Expansion of a generic atomic load may require an addition | |
2010 | element, so allocate enough to prevent a resize. */ | |
2011 | vec_alloc (params, 4); | |
2012 | ||
2013 | /* Remove the qualifiers for the rest of the expressions and | |
2014 | create the VAL temp variable to hold the RHS. */ | |
2015 | nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED); | |
5c4abbb8 | 2016 | tmp = create_tmp_var_raw (nonatomic_type); |
267bac10 JM |
2017 | tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0); |
2018 | TREE_ADDRESSABLE (tmp) = 1; | |
cc28fc7f | 2019 | TREE_NO_WARNING (tmp) = 1; |
267bac10 JM |
2020 | |
2021 | /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */ | |
2022 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); | |
2023 | params->quick_push (expr_addr); | |
2024 | params->quick_push (tmp_addr); | |
2025 | params->quick_push (seq_cst); | |
8edbfaa6 | 2026 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 | 2027 | |
cc28fc7f MP |
2028 | /* EXPR is always read. */ |
2029 | mark_exp_read (exp.value); | |
2030 | ||
267bac10 | 2031 | /* Return tmp which contains the value loaded. */ |
5c4abbb8 MP |
2032 | exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call, |
2033 | NULL_TREE, NULL_TREE); | |
267bac10 JM |
2034 | } |
2035 | return exp; | |
2036 | } | |
2037 | ||
522ddfa2 JM |
2038 | /* EXP is an expression of integer type. Apply the integer promotions |
2039 | to it and return the promoted value. */ | |
400fbf9f JW |
2040 | |
2041 | tree | |
522ddfa2 | 2042 | perform_integral_promotions (tree exp) |
400fbf9f | 2043 | { |
b3694847 SS |
2044 | tree type = TREE_TYPE (exp); |
2045 | enum tree_code code = TREE_CODE (type); | |
400fbf9f | 2046 | |
522ddfa2 | 2047 | gcc_assert (INTEGRAL_TYPE_P (type)); |
157689c6 | 2048 | |
400fbf9f JW |
2049 | /* Normally convert enums to int, |
2050 | but convert wide enums to something wider. */ | |
2051 | if (code == ENUMERAL_TYPE) | |
2052 | { | |
b0c48229 NB |
2053 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
2054 | TYPE_PRECISION (integer_type_node)), | |
2055 | ((TYPE_PRECISION (type) | |
2056 | >= TYPE_PRECISION (integer_type_node)) | |
8df83eae | 2057 | && TYPE_UNSIGNED (type))); |
05bccae2 | 2058 | |
400fbf9f JW |
2059 | return convert (type, exp); |
2060 | } | |
2061 | ||
522ddfa2 JM |
2062 | /* ??? This should no longer be needed now bit-fields have their |
2063 | proper types. */ | |
9753f113 | 2064 | if (TREE_CODE (exp) == COMPONENT_REF |
05bccae2 | 2065 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) |
cff9c407 | 2066 | /* If it's thinner than an int, promote it like a |
d72040f5 | 2067 | c_promoting_integer_type_p, otherwise leave it alone. */ |
05bccae2 RK |
2068 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), |
2069 | TYPE_PRECISION (integer_type_node))) | |
f458d1d5 | 2070 | return convert (integer_type_node, exp); |
9753f113 | 2071 | |
d72040f5 | 2072 | if (c_promoting_integer_type_p (type)) |
400fbf9f | 2073 | { |
f458d1d5 | 2074 | /* Preserve unsignedness if not really getting any wider. */ |
8df83eae | 2075 | if (TYPE_UNSIGNED (type) |
f458d1d5 | 2076 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
400fbf9f | 2077 | return convert (unsigned_type_node, exp); |
05bccae2 | 2078 | |
400fbf9f JW |
2079 | return convert (integer_type_node, exp); |
2080 | } | |
05bccae2 | 2081 | |
522ddfa2 JM |
2082 | return exp; |
2083 | } | |
2084 | ||
2085 | ||
2086 | /* Perform default promotions for C data used in expressions. | |
46bdb9cf | 2087 | Enumeral types or short or char are converted to int. |
522ddfa2 JM |
2088 | In addition, manifest constants symbols are replaced by their values. */ |
2089 | ||
2090 | tree | |
2091 | default_conversion (tree exp) | |
2092 | { | |
2093 | tree orig_exp; | |
2094 | tree type = TREE_TYPE (exp); | |
2095 | enum tree_code code = TREE_CODE (type); | |
40449a90 | 2096 | tree promoted_type; |
522ddfa2 | 2097 | |
ebfbbdc5 JJ |
2098 | mark_exp_read (exp); |
2099 | ||
46bdb9cf JM |
2100 | /* Functions and arrays have been converted during parsing. */ |
2101 | gcc_assert (code != FUNCTION_TYPE); | |
2102 | if (code == ARRAY_TYPE) | |
2103 | return exp; | |
522ddfa2 JM |
2104 | |
2105 | /* Constants can be used directly unless they're not loadable. */ | |
2106 | if (TREE_CODE (exp) == CONST_DECL) | |
2107 | exp = DECL_INITIAL (exp); | |
2108 | ||
522ddfa2 JM |
2109 | /* Strip no-op conversions. */ |
2110 | orig_exp = exp; | |
2111 | STRIP_TYPE_NOPS (exp); | |
2112 | ||
2113 | if (TREE_NO_WARNING (orig_exp)) | |
2114 | TREE_NO_WARNING (exp) = 1; | |
2115 | ||
400fbf9f JW |
2116 | if (code == VOID_TYPE) |
2117 | { | |
5436fa2e MP |
2118 | error_at (EXPR_LOC_OR_LOC (exp, input_location), |
2119 | "void value not ignored as it ought to be"); | |
400fbf9f JW |
2120 | return error_mark_node; |
2121 | } | |
808d6eaa JM |
2122 | |
2123 | exp = require_complete_type (exp); | |
2124 | if (exp == error_mark_node) | |
2125 | return error_mark_node; | |
2126 | ||
40449a90 SL |
2127 | promoted_type = targetm.promoted_type (type); |
2128 | if (promoted_type) | |
2129 | return convert (promoted_type, exp); | |
2130 | ||
808d6eaa JM |
2131 | if (INTEGRAL_TYPE_P (type)) |
2132 | return perform_integral_promotions (exp); | |
2133 | ||
400fbf9f JW |
2134 | return exp; |
2135 | } | |
2136 | \f | |
0fb96aa4 | 2137 | /* Look up COMPONENT in a structure or union TYPE. |
e9b2c823 NB |
2138 | |
2139 | If the component name is not found, returns NULL_TREE. Otherwise, | |
2140 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
2141 | stepping down the chain to the component, which is in the last | |
2142 | TREE_VALUE of the list. Normally the list is of length one, but if | |
2143 | the component is embedded within (nested) anonymous structures or | |
2144 | unions, the list steps down the chain to the component. */ | |
2f6e4e97 | 2145 | |
2f2d13da | 2146 | static tree |
0fb96aa4 | 2147 | lookup_field (tree type, tree component) |
2f2d13da DE |
2148 | { |
2149 | tree field; | |
2150 | ||
2151 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
2152 | to the field elements. Use a binary search on this array to quickly | |
2153 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
2154 | will always be set for structures which have many elements. */ | |
2155 | ||
22a0b85f | 2156 | if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) |
2f2d13da DE |
2157 | { |
2158 | int bot, top, half; | |
d07605f5 | 2159 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; |
2f2d13da DE |
2160 | |
2161 | field = TYPE_FIELDS (type); | |
2162 | bot = 0; | |
d07605f5 | 2163 | top = TYPE_LANG_SPECIFIC (type)->s->len; |
2f2d13da DE |
2164 | while (top - bot > 1) |
2165 | { | |
2f2d13da DE |
2166 | half = (top - bot + 1) >> 1; |
2167 | field = field_array[bot+half]; | |
2168 | ||
2169 | if (DECL_NAME (field) == NULL_TREE) | |
2170 | { | |
2171 | /* Step through all anon unions in linear fashion. */ | |
2172 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
2173 | { | |
2f2d13da | 2174 | field = field_array[bot++]; |
a68b98cf RK |
2175 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
2176 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
19d76e60 | 2177 | { |
0fb96aa4 | 2178 | tree anon = lookup_field (TREE_TYPE (field), component); |
e9b2c823 NB |
2179 | |
2180 | if (anon) | |
2181 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2182 | |
2183 | /* The Plan 9 compiler permits referring | |
2184 | directly to an anonymous struct/union field | |
2185 | using a typedef name. */ | |
2186 | if (flag_plan9_extensions | |
2187 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2188 | && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) | |
2189 | == TYPE_DECL) | |
2190 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2191 | == component)) | |
2192 | break; | |
2f6e4e97 | 2193 | } |
2f2d13da DE |
2194 | } |
2195 | ||
2196 | /* Entire record is only anon unions. */ | |
2197 | if (bot > top) | |
2198 | return NULL_TREE; | |
2199 | ||
2200 | /* Restart the binary search, with new lower bound. */ | |
2201 | continue; | |
2202 | } | |
2203 | ||
e8b87aac | 2204 | if (DECL_NAME (field) == component) |
2f2d13da | 2205 | break; |
e8b87aac | 2206 | if (DECL_NAME (field) < component) |
2f2d13da DE |
2207 | bot += half; |
2208 | else | |
2209 | top = bot + half; | |
2210 | } | |
2211 | ||
2212 | if (DECL_NAME (field_array[bot]) == component) | |
2213 | field = field_array[bot]; | |
2214 | else if (DECL_NAME (field) != component) | |
e9b2c823 | 2215 | return NULL_TREE; |
2f2d13da DE |
2216 | } |
2217 | else | |
2218 | { | |
910ad8de | 2219 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
2f2d13da | 2220 | { |
e9b2c823 NB |
2221 | if (DECL_NAME (field) == NULL_TREE |
2222 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
2223 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
2f2d13da | 2224 | { |
0fb96aa4 | 2225 | tree anon = lookup_field (TREE_TYPE (field), component); |
a68b98cf | 2226 | |
e9b2c823 NB |
2227 | if (anon) |
2228 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2229 | |
2230 | /* The Plan 9 compiler permits referring directly to an | |
2231 | anonymous struct/union field using a typedef | |
2232 | name. */ | |
2233 | if (flag_plan9_extensions | |
2234 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2235 | && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL | |
2236 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2237 | == component)) | |
2238 | break; | |
2f2d13da DE |
2239 | } |
2240 | ||
2241 | if (DECL_NAME (field) == component) | |
2242 | break; | |
2243 | } | |
e9b2c823 NB |
2244 | |
2245 | if (field == NULL_TREE) | |
2246 | return NULL_TREE; | |
2f2d13da DE |
2247 | } |
2248 | ||
e9b2c823 | 2249 | return tree_cons (NULL_TREE, field, NULL_TREE); |
2f2d13da DE |
2250 | } |
2251 | ||
c2255bc4 AH |
2252 | /* Make an expression to refer to the COMPONENT field of structure or |
2253 | union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the | |
2254 | location of the COMPONENT_REF. */ | |
400fbf9f JW |
2255 | |
2256 | tree | |
c2255bc4 | 2257 | build_component_ref (location_t loc, tree datum, tree component) |
400fbf9f | 2258 | { |
b3694847 SS |
2259 | tree type = TREE_TYPE (datum); |
2260 | enum tree_code code = TREE_CODE (type); | |
2261 | tree field = NULL; | |
2262 | tree ref; | |
1e57bf47 | 2263 | bool datum_lvalue = lvalue_p (datum); |
400fbf9f | 2264 | |
7a3ea201 RH |
2265 | if (!objc_is_public (datum, component)) |
2266 | return error_mark_node; | |
2267 | ||
46a88c12 | 2268 | /* Detect Objective-C property syntax object.property. */ |
668ea4b1 | 2269 | if (c_dialect_objc () |
46a88c12 | 2270 | && (ref = objc_maybe_build_component_ref (datum, component))) |
668ea4b1 IS |
2271 | return ref; |
2272 | ||
400fbf9f JW |
2273 | /* See if there is a field or component with name COMPONENT. */ |
2274 | ||
2275 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
2276 | { | |
d0f062fb | 2277 | if (!COMPLETE_TYPE_P (type)) |
400fbf9f | 2278 | { |
7a228918 | 2279 | c_incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
2280 | return error_mark_node; |
2281 | } | |
2282 | ||
0fb96aa4 | 2283 | field = lookup_field (type, component); |
400fbf9f JW |
2284 | |
2285 | if (!field) | |
2286 | { | |
c2255bc4 | 2287 | error_at (loc, "%qT has no member named %qE", type, component); |
400fbf9f JW |
2288 | return error_mark_node; |
2289 | } | |
400fbf9f | 2290 | |
e9b2c823 NB |
2291 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. |
2292 | This might be better solved in future the way the C++ front | |
2293 | end does it - by giving the anonymous entities each a | |
2294 | separate name and type, and then have build_component_ref | |
2295 | recursively call itself. We can't do that here. */ | |
46ea50cb | 2296 | do |
19d76e60 | 2297 | { |
e9b2c823 | 2298 | tree subdatum = TREE_VALUE (field); |
efed193e JM |
2299 | int quals; |
2300 | tree subtype; | |
1e57bf47 | 2301 | bool use_datum_quals; |
e9b2c823 NB |
2302 | |
2303 | if (TREE_TYPE (subdatum) == error_mark_node) | |
2304 | return error_mark_node; | |
2305 | ||
1e57bf47 JM |
2306 | /* If this is an rvalue, it does not have qualifiers in C |
2307 | standard terms and we must avoid propagating such | |
2308 | qualifiers down to a non-lvalue array that is then | |
2309 | converted to a pointer. */ | |
2310 | use_datum_quals = (datum_lvalue | |
2311 | || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); | |
2312 | ||
efed193e | 2313 | quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); |
1e57bf47 JM |
2314 | if (use_datum_quals) |
2315 | quals |= TYPE_QUALS (TREE_TYPE (datum)); | |
efed193e JM |
2316 | subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); |
2317 | ||
2318 | ref = build3 (COMPONENT_REF, subtype, datum, subdatum, | |
53fb4de3 | 2319 | NULL_TREE); |
c2255bc4 | 2320 | SET_EXPR_LOCATION (ref, loc); |
1e57bf47 JM |
2321 | if (TREE_READONLY (subdatum) |
2322 | || (use_datum_quals && TREE_READONLY (datum))) | |
19d76e60 | 2323 | TREE_READONLY (ref) = 1; |
1e57bf47 JM |
2324 | if (TREE_THIS_VOLATILE (subdatum) |
2325 | || (use_datum_quals && TREE_THIS_VOLATILE (datum))) | |
19d76e60 | 2326 | TREE_THIS_VOLATILE (ref) = 1; |
e23bd218 IR |
2327 | |
2328 | if (TREE_DEPRECATED (subdatum)) | |
9b86d6bb | 2329 | warn_deprecated_use (subdatum, NULL_TREE); |
e23bd218 | 2330 | |
19d76e60 | 2331 | datum = ref; |
46ea50cb RS |
2332 | |
2333 | field = TREE_CHAIN (field); | |
19d76e60 | 2334 | } |
46ea50cb | 2335 | while (field); |
19d76e60 | 2336 | |
400fbf9f JW |
2337 | return ref; |
2338 | } | |
2339 | else if (code != ERROR_MARK) | |
c2255bc4 AH |
2340 | error_at (loc, |
2341 | "request for member %qE in something not a structure or union", | |
2342 | component); | |
400fbf9f JW |
2343 | |
2344 | return error_mark_node; | |
2345 | } | |
2346 | \f | |
2347 | /* Given an expression PTR for a pointer, return an expression | |
2348 | for the value pointed to. | |
6a3799eb AH |
2349 | ERRORSTRING is the name of the operator to appear in error messages. |
2350 | ||
2351 | LOC is the location to use for the generated tree. */ | |
400fbf9f JW |
2352 | |
2353 | tree | |
dd865ef6 | 2354 | build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) |
400fbf9f | 2355 | { |
b3694847 SS |
2356 | tree pointer = default_conversion (ptr); |
2357 | tree type = TREE_TYPE (pointer); | |
6a3799eb | 2358 | tree ref; |
400fbf9f JW |
2359 | |
2360 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b | 2361 | { |
1043771b | 2362 | if (CONVERT_EXPR_P (pointer) |
79bedddc SR |
2363 | || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) |
2364 | { | |
2365 | /* If a warning is issued, mark it to avoid duplicates from | |
2366 | the backend. This only needs to be done at | |
2367 | warn_strict_aliasing > 2. */ | |
2368 | if (warn_strict_aliasing > 2) | |
2369 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), | |
2370 | type, TREE_OPERAND (pointer, 0))) | |
2371 | TREE_NO_WARNING (pointer) = 1; | |
2372 | } | |
2373 | ||
870cc33b | 2374 | if (TREE_CODE (pointer) == ADDR_EXPR |
870cc33b RS |
2375 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) |
2376 | == TREE_TYPE (type))) | |
6a3799eb AH |
2377 | { |
2378 | ref = TREE_OPERAND (pointer, 0); | |
2379 | protected_set_expr_location (ref, loc); | |
2380 | return ref; | |
2381 | } | |
870cc33b RS |
2382 | else |
2383 | { | |
2384 | tree t = TREE_TYPE (type); | |
46df2823 | 2385 | |
984dfd8c | 2386 | ref = build1 (INDIRECT_REF, t, pointer); |
400fbf9f | 2387 | |
baae9b65 | 2388 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) |
870cc33b | 2389 | { |
d9b7be2e MP |
2390 | if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr))) |
2391 | { | |
2392 | error_at (loc, "dereferencing pointer to incomplete type " | |
2393 | "%qT", t); | |
2394 | C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1; | |
2395 | } | |
870cc33b RS |
2396 | return error_mark_node; |
2397 | } | |
7d882b83 | 2398 | if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) |
c9f9eb5d | 2399 | warning_at (loc, 0, "dereferencing %<void *%> pointer"); |
870cc33b RS |
2400 | |
2401 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
2402 | so that we get the proper error message if the result is used | |
2403 | to assign to. Also, &* is supposed to be a no-op. | |
2404 | And ANSI C seems to specify that the type of the result | |
2405 | should be the const type. */ | |
2406 | /* A de-reference of a pointer to const is not a const. It is valid | |
2407 | to change it via some other pointer. */ | |
2408 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
2409 | TREE_SIDE_EFFECTS (ref) | |
271bd540 | 2410 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); |
493692cd | 2411 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
6a3799eb | 2412 | protected_set_expr_location (ref, loc); |
870cc33b RS |
2413 | return ref; |
2414 | } | |
2415 | } | |
400fbf9f | 2416 | else if (TREE_CODE (pointer) != ERROR_MARK) |
7a6daeb0 NF |
2417 | invalid_indirection_error (loc, type, errstring); |
2418 | ||
400fbf9f JW |
2419 | return error_mark_node; |
2420 | } | |
2421 | ||
2422 | /* This handles expressions of the form "a[i]", which denotes | |
2423 | an array reference. | |
2424 | ||
2425 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2426 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2427 | This avoids forcing the array out of registers, and can work on | |
2428 | arrays that are not lvalues (for example, members of structures returned | |
6a3799eb AH |
2429 | by functions). |
2430 | ||
30cd1c5d AS |
2431 | For vector types, allow vector[i] but not i[vector], and create |
2432 | *(((type*)&vectortype) + i) for the expression. | |
2433 | ||
6a3799eb | 2434 | LOC is the location to use for the returned expression. */ |
400fbf9f JW |
2435 | |
2436 | tree | |
c2255bc4 | 2437 | build_array_ref (location_t loc, tree array, tree index) |
400fbf9f | 2438 | { |
6a3799eb | 2439 | tree ret; |
a4ab7973 | 2440 | bool swapped = false; |
400fbf9f JW |
2441 | if (TREE_TYPE (array) == error_mark_node |
2442 | || TREE_TYPE (index) == error_mark_node) | |
2443 | return error_mark_node; | |
2444 | ||
b72271b9 | 2445 | if (flag_cilkplus && contains_array_notation_expr (index)) |
36536d79 BI |
2446 | { |
2447 | size_t rank = 0; | |
2448 | if (!find_rank (loc, index, index, true, &rank)) | |
2449 | return error_mark_node; | |
2450 | if (rank > 1) | |
2451 | { | |
2452 | error_at (loc, "rank of the array's index is greater than 1"); | |
2453 | return error_mark_node; | |
2454 | } | |
2455 | } | |
a4ab7973 | 2456 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE |
30cd1c5d AS |
2457 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE |
2458 | /* Allow vector[index] but not index[vector]. */ | |
31521951 | 2459 | && !VECTOR_TYPE_P (TREE_TYPE (array))) |
400fbf9f | 2460 | { |
a4ab7973 JM |
2461 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE |
2462 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
fdeefd49 | 2463 | { |
f90e8e2e | 2464 | error_at (loc, |
30cd1c5d AS |
2465 | "subscripted value is neither array nor pointer nor vector"); |
2466 | ||
fdeefd49 RS |
2467 | return error_mark_node; |
2468 | } | |
fab27f52 | 2469 | std::swap (array, index); |
a4ab7973 JM |
2470 | swapped = true; |
2471 | } | |
2472 | ||
2473 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2474 | { | |
a63068b6 | 2475 | error_at (loc, "array subscript is not an integer"); |
a4ab7973 JM |
2476 | return error_mark_node; |
2477 | } | |
2478 | ||
2479 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2480 | { | |
a63068b6 | 2481 | error_at (loc, "subscripted value is pointer to function"); |
a4ab7973 JM |
2482 | return error_mark_node; |
2483 | } | |
2484 | ||
ff6b6641 GDR |
2485 | /* ??? Existing practice has been to warn only when the char |
2486 | index is syntactically the index, not for char[array]. */ | |
2487 | if (!swapped) | |
5bd012f8 | 2488 | warn_array_subscript_with_type_char (loc, index); |
a4ab7973 JM |
2489 | |
2490 | /* Apply default promotions *after* noticing character types. */ | |
2491 | index = default_conversion (index); | |
f406ae1f MP |
2492 | if (index == error_mark_node) |
2493 | return error_mark_node; | |
a4ab7973 JM |
2494 | |
2495 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
f90e8e2e | 2496 | |
aa7da51a JJ |
2497 | bool non_lvalue |
2498 | = convert_vector_to_pointer_for_subscript (loc, &array, index); | |
a4ab7973 JM |
2499 | |
2500 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2501 | { | |
e4d35515 | 2502 | tree rval, type; |
fdeefd49 | 2503 | |
400fbf9f JW |
2504 | /* An array that is indexed by a non-constant |
2505 | cannot be stored in a register; we must be able to do | |
2506 | address arithmetic on its address. | |
2507 | Likewise an array of elements of variable size. */ | |
2508 | if (TREE_CODE (index) != INTEGER_CST | |
d0f062fb | 2509 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
400fbf9f JW |
2510 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
2511 | { | |
dffd7eb6 | 2512 | if (!c_mark_addressable (array)) |
400fbf9f JW |
2513 | return error_mark_node; |
2514 | } | |
e6d52559 JW |
2515 | /* An array that is indexed by a constant value which is not within |
2516 | the array bounds cannot be stored in a register either; because we | |
2517 | would get a crash in store_bit_field/extract_bit_field when trying | |
2518 | to access a non-existent part of the register. */ | |
2519 | if (TREE_CODE (index) == INTEGER_CST | |
eb34af89 | 2520 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3f75a254 | 2521 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) |
e6d52559 | 2522 | { |
dffd7eb6 | 2523 | if (!c_mark_addressable (array)) |
e6d52559 JW |
2524 | return error_mark_node; |
2525 | } | |
400fbf9f | 2526 | |
f3bede71 | 2527 | if (pedantic || warn_c90_c99_compat) |
400fbf9f JW |
2528 | { |
2529 | tree foo = array; | |
2530 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2531 | foo = TREE_OPERAND (foo, 0); | |
0ae9bd27 | 2532 | if (VAR_P (foo) && C_DECL_REGISTER (foo)) |
c1771a20 | 2533 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 2534 | "ISO C forbids subscripting %<register%> array"); |
f3bede71 MP |
2535 | else if (!lvalue_p (foo)) |
2536 | pedwarn_c90 (loc, OPT_Wpedantic, | |
2537 | "ISO C90 forbids subscripting non-lvalue " | |
2538 | "array"); | |
400fbf9f JW |
2539 | } |
2540 | ||
46df2823 | 2541 | type = TREE_TYPE (TREE_TYPE (array)); |
53fb4de3 | 2542 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
400fbf9f | 2543 | /* Array ref is const/volatile if the array elements are |
c22cacf3 | 2544 | or if the array is. */ |
400fbf9f JW |
2545 | TREE_READONLY (rval) |
2546 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2547 | | TREE_READONLY (array)); | |
2548 | TREE_SIDE_EFFECTS (rval) | |
2549 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2550 | | TREE_SIDE_EFFECTS (array)); | |
2551 | TREE_THIS_VOLATILE (rval) | |
2552 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2553 | /* This was added by rms on 16 Nov 91. | |
2f6e4e97 | 2554 | It fixes vol struct foo *a; a->elts[1] |
400fbf9f JW |
2555 | in an inline function. |
2556 | Hope it doesn't break something else. */ | |
2557 | | TREE_THIS_VOLATILE (array)); | |
928c19bb | 2558 | ret = require_complete_type (rval); |
6a3799eb | 2559 | protected_set_expr_location (ret, loc); |
aa7da51a JJ |
2560 | if (non_lvalue) |
2561 | ret = non_lvalue_loc (loc, ret); | |
6a3799eb | 2562 | return ret; |
400fbf9f | 2563 | } |
a4ab7973 JM |
2564 | else |
2565 | { | |
2566 | tree ar = default_conversion (array); | |
400fbf9f | 2567 | |
a4ab7973 JM |
2568 | if (ar == error_mark_node) |
2569 | return ar; | |
400fbf9f | 2570 | |
a4ab7973 JM |
2571 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); |
2572 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
400fbf9f | 2573 | |
aa7da51a JJ |
2574 | ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar, |
2575 | index, 0), | |
2576 | RO_ARRAY_INDEXING); | |
2577 | if (non_lvalue) | |
2578 | ret = non_lvalue_loc (loc, ret); | |
2579 | return ret; | |
a4ab7973 | 2580 | } |
400fbf9f JW |
2581 | } |
2582 | \f | |
7e585d16 | 2583 | /* Build an external reference to identifier ID. FUN indicates |
766beb40 | 2584 | whether this will be used for a function call. LOC is the source |
6866c6e8 ILT |
2585 | location of the identifier. This sets *TYPE to the type of the |
2586 | identifier, which is not the same as the type of the returned value | |
2587 | for CONST_DECLs defined as enum constants. If the type of the | |
2588 | identifier is not available, *TYPE is set to NULL. */ | |
7e585d16 | 2589 | tree |
c2255bc4 | 2590 | build_external_ref (location_t loc, tree id, int fun, tree *type) |
7e585d16 ZW |
2591 | { |
2592 | tree ref; | |
2593 | tree decl = lookup_name (id); | |
16b34ad6 ZL |
2594 | |
2595 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2596 | whatever lookup_name() found. */ | |
2597 | decl = objc_lookup_ivar (decl, id); | |
7e585d16 | 2598 | |
6866c6e8 | 2599 | *type = NULL; |
339a28b9 | 2600 | if (decl && decl != error_mark_node) |
6866c6e8 ILT |
2601 | { |
2602 | ref = decl; | |
2603 | *type = TREE_TYPE (ref); | |
2604 | } | |
339a28b9 ZW |
2605 | else if (fun) |
2606 | /* Implicit function declaration. */ | |
c2255bc4 | 2607 | ref = implicitly_declare (loc, id); |
339a28b9 ZW |
2608 | else if (decl == error_mark_node) |
2609 | /* Don't complain about something that's already been | |
2610 | complained about. */ | |
2611 | return error_mark_node; | |
2612 | else | |
2613 | { | |
c2255bc4 | 2614 | undeclared_variable (loc, id); |
339a28b9 ZW |
2615 | return error_mark_node; |
2616 | } | |
7e585d16 ZW |
2617 | |
2618 | if (TREE_TYPE (ref) == error_mark_node) | |
2619 | return error_mark_node; | |
2620 | ||
339a28b9 | 2621 | if (TREE_DEPRECATED (ref)) |
9b86d6bb | 2622 | warn_deprecated_use (ref, NULL_TREE); |
339a28b9 | 2623 | |
ad960f56 | 2624 | /* Recursive call does not count as usage. */ |
b8698a0f | 2625 | if (ref != current_function_decl) |
ad960f56 | 2626 | { |
ad960f56 MLI |
2627 | TREE_USED (ref) = 1; |
2628 | } | |
7e585d16 | 2629 | |
bc4b653b JM |
2630 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) |
2631 | { | |
2632 | if (!in_sizeof && !in_typeof) | |
2633 | C_DECL_USED (ref) = 1; | |
2634 | else if (DECL_INITIAL (ref) == 0 | |
2635 | && DECL_EXTERNAL (ref) | |
2636 | && !TREE_PUBLIC (ref)) | |
2637 | record_maybe_used_decl (ref); | |
2638 | } | |
2639 | ||
7e585d16 ZW |
2640 | if (TREE_CODE (ref) == CONST_DECL) |
2641 | { | |
6193b8b7 | 2642 | used_types_insert (TREE_TYPE (ref)); |
24b97832 ILT |
2643 | |
2644 | if (warn_cxx_compat | |
2645 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2646 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2647 | { | |
2648 | warning_at (loc, OPT_Wc___compat, | |
2649 | ("enum constant defined in struct or union " | |
2650 | "is not visible in C++")); | |
2651 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2652 | } | |
2653 | ||
7e585d16 ZW |
2654 | ref = DECL_INITIAL (ref); |
2655 | TREE_CONSTANT (ref) = 1; | |
2656 | } | |
6a29edea | 2657 | else if (current_function_decl != 0 |
4b1e44be | 2658 | && !DECL_FILE_SCOPE_P (current_function_decl) |
21b634ae MP |
2659 | && (VAR_OR_FUNCTION_DECL_P (ref) |
2660 | || TREE_CODE (ref) == PARM_DECL)) | |
6a29edea EB |
2661 | { |
2662 | tree context = decl_function_context (ref); | |
2f6e4e97 | 2663 | |
6a29edea EB |
2664 | if (context != 0 && context != current_function_decl) |
2665 | DECL_NONLOCAL (ref) = 1; | |
2666 | } | |
71113fcd GK |
2667 | /* C99 6.7.4p3: An inline definition of a function with external |
2668 | linkage ... shall not contain a reference to an identifier with | |
2669 | internal linkage. */ | |
2670 | else if (current_function_decl != 0 | |
2671 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2672 | && DECL_EXTERNAL (current_function_decl) | |
2673 | && VAR_OR_FUNCTION_DECL_P (ref) | |
0ae9bd27 | 2674 | && (!VAR_P (ref) || TREE_STATIC (ref)) |
1033ffa8 JJ |
2675 | && ! TREE_PUBLIC (ref) |
2676 | && DECL_CONTEXT (ref) != current_function_decl) | |
991d6621 JM |
2677 | record_inline_static (loc, current_function_decl, ref, |
2678 | csi_internal); | |
7e585d16 ZW |
2679 | |
2680 | return ref; | |
2681 | } | |
2682 | ||
bc4b653b JM |
2683 | /* Record details of decls possibly used inside sizeof or typeof. */ |
2684 | struct maybe_used_decl | |
2685 | { | |
2686 | /* The decl. */ | |
2687 | tree decl; | |
2688 | /* The level seen at (in_sizeof + in_typeof). */ | |
2689 | int level; | |
2690 | /* The next one at this level or above, or NULL. */ | |
2691 | struct maybe_used_decl *next; | |
2692 | }; | |
2693 | ||
2694 | static struct maybe_used_decl *maybe_used_decls; | |
2695 | ||
2696 | /* Record that DECL, an undefined static function reference seen | |
2697 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2698 | a VLA type or the operand of typeof is a variably modified | |
2699 | type. */ | |
2700 | ||
4e2fb7de | 2701 | static void |
bc4b653b JM |
2702 | record_maybe_used_decl (tree decl) |
2703 | { | |
2704 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2705 | t->decl = decl; | |
2706 | t->level = in_sizeof + in_typeof; | |
2707 | t->next = maybe_used_decls; | |
2708 | maybe_used_decls = t; | |
2709 | } | |
2710 | ||
2711 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2712 | USED is false, just discard them. If it is true, mark them used | |
2713 | (if no longer inside sizeof or typeof) or move them to the next | |
2714 | level up (if still inside sizeof or typeof). */ | |
2715 | ||
2716 | void | |
2717 | pop_maybe_used (bool used) | |
2718 | { | |
2719 | struct maybe_used_decl *p = maybe_used_decls; | |
2720 | int cur_level = in_sizeof + in_typeof; | |
2721 | while (p && p->level > cur_level) | |
2722 | { | |
2723 | if (used) | |
2724 | { | |
2725 | if (cur_level == 0) | |
2726 | C_DECL_USED (p->decl) = 1; | |
2727 | else | |
2728 | p->level = cur_level; | |
2729 | } | |
2730 | p = p->next; | |
2731 | } | |
2732 | if (!used || cur_level == 0) | |
2733 | maybe_used_decls = p; | |
2734 | } | |
2735 | ||
2736 | /* Return the result of sizeof applied to EXPR. */ | |
2737 | ||
2738 | struct c_expr | |
c2255bc4 | 2739 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) |
bc4b653b JM |
2740 | { |
2741 | struct c_expr ret; | |
ad97f4be JM |
2742 | if (expr.value == error_mark_node) |
2743 | { | |
2744 | ret.value = error_mark_node; | |
2745 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 2746 | ret.original_type = NULL; |
ad97f4be JM |
2747 | pop_maybe_used (false); |
2748 | } | |
2749 | else | |
2750 | { | |
928c19bb | 2751 | bool expr_const_operands = true; |
773ec47f MP |
2752 | |
2753 | if (TREE_CODE (expr.value) == PARM_DECL | |
2754 | && C_ARRAY_PARAMETER (expr.value)) | |
2755 | { | |
2756 | if (warning_at (loc, OPT_Wsizeof_array_argument, | |
2757 | "%<sizeof%> on array function parameter %qE will " | |
2758 | "return size of %qT", expr.value, | |
2759 | expr.original_type)) | |
2760 | inform (DECL_SOURCE_LOCATION (expr.value), "declared here"); | |
2761 | } | |
928c19bb JM |
2762 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, |
2763 | &expr_const_operands); | |
c2255bc4 | 2764 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); |
1a4049e7 JJ |
2765 | c_last_sizeof_arg = expr.value; |
2766 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2767 | ret.original_type = NULL; |
928c19bb | 2768 | if (c_vla_type_p (TREE_TYPE (folded_expr))) |
52ffd86e MS |
2769 | { |
2770 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
928c19bb JM |
2771 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2772 | folded_expr, ret.value); | |
2773 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
c2255bc4 | 2774 | SET_EXPR_LOCATION (ret.value, loc); |
52ffd86e | 2775 | } |
928c19bb | 2776 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); |
ad97f4be | 2777 | } |
bc4b653b JM |
2778 | return ret; |
2779 | } | |
2780 | ||
2781 | /* Return the result of sizeof applied to T, a structure for the type | |
c2255bc4 AH |
2782 | name passed to sizeof (rather than the type itself). LOC is the |
2783 | location of the original expression. */ | |
bc4b653b JM |
2784 | |
2785 | struct c_expr | |
c2255bc4 | 2786 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) |
bc4b653b JM |
2787 | { |
2788 | tree type; | |
2789 | struct c_expr ret; | |
928c19bb JM |
2790 | tree type_expr = NULL_TREE; |
2791 | bool type_expr_const = true; | |
2792 | type = groktypename (t, &type_expr, &type_expr_const); | |
c2255bc4 | 2793 | ret.value = c_sizeof (loc, type); |
1a4049e7 JJ |
2794 | c_last_sizeof_arg = type; |
2795 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2796 | ret.original_type = NULL; |
24070fcb JM |
2797 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) |
2798 | && c_vla_type_p (type)) | |
928c19bb | 2799 | { |
24070fcb JM |
2800 | /* If the type is a [*] array, it is a VLA but is represented as |
2801 | having a size of zero. In such a case we must ensure that | |
2802 | the result of sizeof does not get folded to a constant by | |
2803 | c_fully_fold, because if the size is evaluated the result is | |
2804 | not constant and so constraints on zero or negative size | |
2805 | arrays must not be applied when this sizeof call is inside | |
2806 | another array declarator. */ | |
2807 | if (!type_expr) | |
2808 | type_expr = integer_zero_node; | |
928c19bb JM |
2809 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2810 | type_expr, ret.value); | |
2811 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2812 | } | |
16464cc1 VR |
2813 | pop_maybe_used (type != error_mark_node |
2814 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
bc4b653b JM |
2815 | return ret; |
2816 | } | |
2817 | ||
400fbf9f | 2818 | /* Build a function call to function FUNCTION with parameters PARAMS. |
c2255bc4 | 2819 | The function call is at LOC. |
400fbf9f JW |
2820 | PARAMS is a list--a chain of TREE_LIST nodes--in which the |
2821 | TREE_VALUE of each node is a parameter-expression. | |
2822 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2823 | ||
2824 | tree | |
c2255bc4 | 2825 | build_function_call (location_t loc, tree function, tree params) |
bbbbb16a | 2826 | { |
9771b263 | 2827 | vec<tree, va_gc> *v; |
bbbbb16a ILT |
2828 | tree ret; |
2829 | ||
9771b263 | 2830 | vec_alloc (v, list_length (params)); |
bbbbb16a | 2831 | for (; params; params = TREE_CHAIN (params)) |
9771b263 | 2832 | v->quick_push (TREE_VALUE (params)); |
8edbfaa6 | 2833 | ret = c_build_function_call_vec (loc, vNULL, function, v, NULL); |
9771b263 | 2834 | vec_free (v); |
bbbbb16a ILT |
2835 | return ret; |
2836 | } | |
2837 | ||
ae52741c MLI |
2838 | /* Give a note about the location of the declaration of DECL. */ |
2839 | ||
c7b70a3c MP |
2840 | static void |
2841 | inform_declaration (tree decl) | |
ae52741c | 2842 | { |
c7b70a3c | 2843 | if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl))) |
ae52741c MLI |
2844 | inform (DECL_SOURCE_LOCATION (decl), "declared here"); |
2845 | } | |
2846 | ||
bbbbb16a ILT |
2847 | /* Build a function call to function FUNCTION with parameters PARAMS. |
2848 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2849 | either NULL or the original type of the corresponding element in | |
2850 | PARAMS. The original type may differ from TREE_TYPE of the | |
2851 | parameter for enums. FUNCTION's data type may be a function type | |
2852 | or pointer-to-function. This function changes the elements of | |
2853 | PARAMS. */ | |
2854 | ||
2855 | tree | |
81e5eca8 MP |
2856 | build_function_call_vec (location_t loc, vec<location_t> arg_loc, |
2857 | tree function, vec<tree, va_gc> *params, | |
9771b263 | 2858 | vec<tree, va_gc> *origtypes) |
400fbf9f | 2859 | { |
b3694847 | 2860 | tree fntype, fundecl = 0; |
4977bab6 | 2861 | tree name = NULL_TREE, result; |
c96f4f73 | 2862 | tree tem; |
94a0dd7b SL |
2863 | int nargs; |
2864 | tree *argarray; | |
b8698a0f | 2865 | |
400fbf9f | 2866 | |
fc76e425 | 2867 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 2868 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
2869 | |
2870 | /* Convert anything with function type to a pointer-to-function. */ | |
2871 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2872 | { | |
2873 | name = DECL_NAME (function); | |
0a35513e AH |
2874 | |
2875 | if (flag_tm) | |
2876 | tm_malloc_replacement (function); | |
a5eadacc | 2877 | fundecl = function; |
86951993 AM |
2878 | /* Atomic functions have type checking/casting already done. They are |
2879 | often rewritten and don't match the original parameter list. */ | |
2880 | if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) | |
2881 | origtypes = NULL; | |
36536d79 | 2882 | |
b72271b9 | 2883 | if (flag_cilkplus |
36536d79 BI |
2884 | && is_cilkplus_reduce_builtin (function)) |
2885 | origtypes = NULL; | |
400fbf9f | 2886 | } |
f2a71bbc | 2887 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) |
c2255bc4 | 2888 | function = function_to_pointer_conversion (loc, function); |
400fbf9f | 2889 | |
6e955430 ZL |
2890 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2891 | expressions, like those used for ObjC messenger dispatches. */ | |
9771b263 DN |
2892 | if (params && !params->is_empty ()) |
2893 | function = objc_rewrite_function_call (function, (*params)[0]); | |
6e955430 | 2894 | |
928c19bb JM |
2895 | function = c_fully_fold (function, false, NULL); |
2896 | ||
400fbf9f JW |
2897 | fntype = TREE_TYPE (function); |
2898 | ||
2899 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2900 | return error_mark_node; | |
2901 | ||
2902 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2903 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2904 | { | |
ae52741c MLI |
2905 | if (!flag_diagnostics_show_caret) |
2906 | error_at (loc, | |
2907 | "called object %qE is not a function or function pointer", | |
2908 | function); | |
2909 | else if (DECL_P (function)) | |
2910 | { | |
2911 | error_at (loc, | |
2912 | "called object %qD is not a function or function pointer", | |
2913 | function); | |
2914 | inform_declaration (function); | |
2915 | } | |
2916 | else | |
2917 | error_at (loc, | |
2918 | "called object is not a function or function pointer"); | |
400fbf9f JW |
2919 | return error_mark_node; |
2920 | } | |
2921 | ||
5ce89b2e JM |
2922 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
2923 | current_function_returns_abnormally = 1; | |
2924 | ||
400fbf9f JW |
2925 | /* fntype now gets the type of function pointed to. */ |
2926 | fntype = TREE_TYPE (fntype); | |
2927 | ||
ab4194da JM |
2928 | /* Convert the parameters to the types declared in the |
2929 | function prototype, or apply default promotions. */ | |
2930 | ||
81e5eca8 MP |
2931 | nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params, |
2932 | origtypes, function, fundecl); | |
ab4194da JM |
2933 | if (nargs < 0) |
2934 | return error_mark_node; | |
2935 | ||
c96f4f73 | 2936 | /* Check that the function is called through a compatible prototype. |
fa337f3a | 2937 | If it is not, warn. */ |
1043771b | 2938 | if (CONVERT_EXPR_P (function) |
c96f4f73 EB |
2939 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR |
2940 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
3f75a254 | 2941 | && !comptypes (fntype, TREE_TYPE (tem))) |
c96f4f73 EB |
2942 | { |
2943 | tree return_type = TREE_TYPE (fntype); | |
c96f4f73 EB |
2944 | |
2945 | /* This situation leads to run-time undefined behavior. We can't, | |
2946 | therefore, simply error unless we can prove that all possible | |
2947 | executions of the program must execute the code. */ | |
fa337f3a | 2948 | warning_at (loc, 0, "function called through a non-compatible type"); |
c96f4f73 | 2949 | |
fa337f3a RB |
2950 | if (VOID_TYPE_P (return_type) |
2951 | && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
2952 | pedwarn (loc, 0, | |
2953 | "function with qualified void return type called"); | |
2954 | } | |
c96f4f73 | 2955 | |
9771b263 | 2956 | argarray = vec_safe_address (params); |
bbbbb16a | 2957 | |
83322951 RG |
2958 | /* Check that arguments to builtin functions match the expectations. */ |
2959 | if (fundecl | |
2960 | && DECL_BUILT_IN (fundecl) | |
2961 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2962 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2963 | return error_mark_node; | |
400fbf9f | 2964 | |
83322951 | 2965 | /* Check that the arguments to the function are valid. */ |
dde05067 | 2966 | check_function_arguments (fntype, nargs, argarray); |
400fbf9f | 2967 | |
928c19bb JM |
2968 | if (name != NULL_TREE |
2969 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
bf730f15 | 2970 | { |
928c19bb | 2971 | if (require_constant_value) |
b8698a0f | 2972 | result = |
db3927fb AH |
2973 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), |
2974 | function, nargs, argarray); | |
928c19bb | 2975 | else |
db3927fb AH |
2976 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), |
2977 | function, nargs, argarray); | |
928c19bb JM |
2978 | if (TREE_CODE (result) == NOP_EXPR |
2979 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2980 | STRIP_TYPE_NOPS (result); | |
bf730f15 RS |
2981 | } |
2982 | else | |
db3927fb AH |
2983 | result = build_call_array_loc (loc, TREE_TYPE (fntype), |
2984 | function, nargs, argarray); | |
b0b3afb2 | 2985 | |
71653180 | 2986 | if (VOID_TYPE_P (TREE_TYPE (result))) |
3ce62965 JM |
2987 | { |
2988 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
db3927fb | 2989 | pedwarn (loc, 0, |
3ce62965 JM |
2990 | "function with qualified void return type called"); |
2991 | return result; | |
2992 | } | |
1eb8759b | 2993 | return require_complete_type (result); |
400fbf9f | 2994 | } |
8edbfaa6 JJ |
2995 | |
2996 | /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */ | |
2997 | ||
2998 | tree | |
2999 | c_build_function_call_vec (location_t loc, vec<location_t> arg_loc, | |
3000 | tree function, vec<tree, va_gc> *params, | |
3001 | vec<tree, va_gc> *origtypes) | |
3002 | { | |
3003 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
3004 | STRIP_TYPE_NOPS (function); | |
3005 | ||
3006 | /* Convert anything with function type to a pointer-to-function. */ | |
3007 | if (TREE_CODE (function) == FUNCTION_DECL) | |
3008 | { | |
3009 | /* Implement type-directed function overloading for builtins. | |
3010 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
3011 | handle all the type checking. The result is a complete expression | |
3012 | that implements this function call. */ | |
3013 | tree tem = resolve_overloaded_builtin (loc, function, params); | |
3014 | if (tem) | |
3015 | return tem; | |
3016 | } | |
3017 | return build_function_call_vec (loc, arg_loc, function, params, origtypes); | |
3018 | } | |
400fbf9f | 3019 | \f |
bbbbb16a ILT |
3020 | /* Convert the argument expressions in the vector VALUES |
3021 | to the types in the list TYPELIST. | |
400fbf9f JW |
3022 | |
3023 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
3024 | perform the default conversions. | |
3025 | ||
bbbbb16a ILT |
3026 | ORIGTYPES is the original types of the expressions in VALUES. This |
3027 | holds the type of enum values which have been converted to integral | |
3028 | types. It may be NULL. | |
400fbf9f | 3029 | |
03dafa61 JM |
3030 | FUNCTION is a tree for the called function. It is used only for |
3031 | error messages, where it is formatted with %qE. | |
400fbf9f JW |
3032 | |
3033 | This is also where warnings about wrong number of args are generated. | |
3034 | ||
81e5eca8 MP |
3035 | ARG_LOC are locations of function arguments (if any). |
3036 | ||
94a0dd7b | 3037 | Returns the actual number of arguments processed (which may be less |
bbbbb16a ILT |
3038 | than the length of VALUES in some error situations), or -1 on |
3039 | failure. */ | |
94a0dd7b SL |
3040 | |
3041 | static int | |
81e5eca8 MP |
3042 | convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist, |
3043 | vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes, | |
3044 | tree function, tree fundecl) | |
400fbf9f | 3045 | { |
bbbbb16a ILT |
3046 | tree typetail, val; |
3047 | unsigned int parmnum; | |
06302a02 | 3048 | bool error_args = false; |
b5d32c25 | 3049 | const bool type_generic = fundecl |
81e5eca8 | 3050 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl))); |
8ce94e44 | 3051 | bool type_generic_remove_excess_precision = false; |
03dafa61 | 3052 | tree selector; |
03dafa61 | 3053 | |
2ac2f164 JM |
3054 | /* Change pointer to function to the function itself for |
3055 | diagnostics. */ | |
03dafa61 JM |
3056 | if (TREE_CODE (function) == ADDR_EXPR |
3057 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
2ac2f164 | 3058 | function = TREE_OPERAND (function, 0); |
03dafa61 JM |
3059 | |
3060 | /* Handle an ObjC selector specially for diagnostics. */ | |
3061 | selector = objc_message_selector (); | |
400fbf9f | 3062 | |
8ce94e44 JM |
3063 | /* For type-generic built-in functions, determine whether excess |
3064 | precision should be removed (classification) or not | |
3065 | (comparison). */ | |
3066 | if (type_generic | |
3067 | && DECL_BUILT_IN (fundecl) | |
3068 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
3069 | { | |
3070 | switch (DECL_FUNCTION_CODE (fundecl)) | |
3071 | { | |
3072 | case BUILT_IN_ISFINITE: | |
3073 | case BUILT_IN_ISINF: | |
3074 | case BUILT_IN_ISINF_SIGN: | |
3075 | case BUILT_IN_ISNAN: | |
3076 | case BUILT_IN_ISNORMAL: | |
3077 | case BUILT_IN_FPCLASSIFY: | |
3078 | type_generic_remove_excess_precision = true; | |
3079 | break; | |
3080 | ||
3081 | default: | |
3082 | type_generic_remove_excess_precision = false; | |
3083 | break; | |
3084 | } | |
3085 | } | |
b72271b9 | 3086 | if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl)) |
6d6efbb3 | 3087 | return vec_safe_length (values); |
8ce94e44 | 3088 | |
400fbf9f | 3089 | /* Scan the given expressions and types, producing individual |
bbbbb16a | 3090 | converted arguments. */ |
400fbf9f | 3091 | |
bbbbb16a | 3092 | for (typetail = typelist, parmnum = 0; |
9771b263 | 3093 | values && values->iterate (parmnum, &val); |
bbbbb16a | 3094 | ++parmnum) |
400fbf9f | 3095 | { |
b3694847 | 3096 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
8ce94e44 | 3097 | tree valtype = TREE_TYPE (val); |
03dafa61 JM |
3098 | tree rname = function; |
3099 | int argnum = parmnum + 1; | |
4d3e6fae | 3100 | const char *invalid_func_diag; |
8ce94e44 | 3101 | bool excess_precision = false; |
928c19bb | 3102 | bool npc; |
bbbbb16a | 3103 | tree parmval; |
5c1bc275 MP |
3104 | /* Some __atomic_* builtins have additional hidden argument at |
3105 | position 0. */ | |
3106 | location_t ploc | |
3107 | = !arg_loc.is_empty () && values->length () == arg_loc.length () | |
3108 | ? expansion_point_location_if_in_system_header (arg_loc[parmnum]) | |
3109 | : input_location; | |
400fbf9f JW |
3110 | |
3111 | if (type == void_type_node) | |
3112 | { | |
19dc6d01 | 3113 | if (selector) |
68fca595 | 3114 | error_at (loc, "too many arguments to method %qE", selector); |
19dc6d01 | 3115 | else |
68fca595 | 3116 | error_at (loc, "too many arguments to function %qE", function); |
ae52741c | 3117 | inform_declaration (fundecl); |
d38f7dce | 3118 | return error_args ? -1 : (int) parmnum; |
400fbf9f JW |
3119 | } |
3120 | ||
03dafa61 JM |
3121 | if (selector && argnum > 2) |
3122 | { | |
3123 | rname = selector; | |
3124 | argnum -= 2; | |
3125 | } | |
3126 | ||
928c19bb | 3127 | npc = null_pointer_constant_p (val); |
8ce94e44 JM |
3128 | |
3129 | /* If there is excess precision and a prototype, convert once to | |
3130 | the required type rather than converting via the semantic | |
3131 | type. Likewise without a prototype a float value represented | |
3132 | as long double should be converted once to double. But for | |
3133 | type-generic classification functions excess precision must | |
3134 | be removed here. */ | |
3135 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
3136 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
3137 | { | |
3138 | val = TREE_OPERAND (val, 0); | |
3139 | excess_precision = true; | |
3140 | } | |
928c19bb | 3141 | val = c_fully_fold (val, false, NULL); |
ed248cf7 | 3142 | STRIP_TYPE_NOPS (val); |
400fbf9f | 3143 | |
400fbf9f JW |
3144 | val = require_complete_type (val); |
3145 | ||
3146 | if (type != 0) | |
3147 | { | |
3148 | /* Formal parm type is specified by a function prototype. */ | |
400fbf9f | 3149 | |
20913689 | 3150 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) |
400fbf9f | 3151 | { |
5c1bc275 MP |
3152 | error_at (ploc, "type of formal parameter %d is incomplete", |
3153 | parmnum + 1); | |
400fbf9f JW |
3154 | parmval = val; |
3155 | } | |
3156 | else | |
3157 | { | |
bbbbb16a ILT |
3158 | tree origtype; |
3159 | ||
d45cf215 RS |
3160 | /* Optionally warn about conversions that |
3161 | differ from the default conversions. */ | |
05170031 | 3162 | if (warn_traditional_conversion || warn_traditional) |
400fbf9f | 3163 | { |
e3a64162 | 3164 | unsigned int formal_prec = TYPE_PRECISION (type); |
400fbf9f | 3165 | |
aae43c5f | 3166 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3167 | && TREE_CODE (valtype) == REAL_TYPE) |
5c1bc275 MP |
3168 | warning_at (ploc, OPT_Wtraditional_conversion, |
3169 | "passing argument %d of %qE as integer rather " | |
3170 | "than floating due to prototype", | |
3171 | argnum, rname); | |
03829ad2 | 3172 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3173 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
5c1bc275 MP |
3174 | warning_at (ploc, OPT_Wtraditional_conversion, |
3175 | "passing argument %d of %qE as integer rather " | |
3176 | "than complex due to prototype", | |
3177 | argnum, rname); | |
aae43c5f | 3178 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3179 | && TREE_CODE (valtype) == REAL_TYPE) |
5c1bc275 MP |
3180 | warning_at (ploc, OPT_Wtraditional_conversion, |
3181 | "passing argument %d of %qE as complex rather " | |
3182 | "than floating due to prototype", | |
3183 | argnum, rname); | |
400fbf9f | 3184 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3185 | && INTEGRAL_TYPE_P (valtype)) |
5c1bc275 MP |
3186 | warning_at (ploc, OPT_Wtraditional_conversion, |
3187 | "passing argument %d of %qE as floating rather " | |
3188 | "than integer due to prototype", | |
3189 | argnum, rname); | |
03829ad2 | 3190 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3191 | && INTEGRAL_TYPE_P (valtype)) |
5c1bc275 MP |
3192 | warning_at (ploc, OPT_Wtraditional_conversion, |
3193 | "passing argument %d of %qE as complex rather " | |
3194 | "than integer due to prototype", | |
3195 | argnum, rname); | |
aae43c5f | 3196 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3197 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
5c1bc275 MP |
3198 | warning_at (ploc, OPT_Wtraditional_conversion, |
3199 | "passing argument %d of %qE as floating rather " | |
3200 | "than complex due to prototype", | |
3201 | argnum, rname); | |
aae43c5f RK |
3202 | /* ??? At some point, messages should be written about |
3203 | conversions between complex types, but that's too messy | |
3204 | to do now. */ | |
d45cf215 | 3205 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3206 | && TREE_CODE (valtype) == REAL_TYPE) |
d45cf215 RS |
3207 | { |
3208 | /* Warn if any argument is passed as `float', | |
047de90b | 3209 | since without a prototype it would be `double'. */ |
9a8ce21f JG |
3210 | if (formal_prec == TYPE_PRECISION (float_type_node) |
3211 | && type != dfloat32_type_node) | |
5c1bc275 MP |
3212 | warning_at (ploc, 0, |
3213 | "passing argument %d of %qE as %<float%> " | |
3214 | "rather than %<double%> due to prototype", | |
3215 | argnum, rname); | |
9a8ce21f JG |
3216 | |
3217 | /* Warn if mismatch between argument and prototype | |
3218 | for decimal float types. Warn of conversions with | |
3219 | binary float types and of precision narrowing due to | |
3220 | prototype. */ | |
8ce94e44 | 3221 | else if (type != valtype |
9a8ce21f JG |
3222 | && (type == dfloat32_type_node |
3223 | || type == dfloat64_type_node | |
c22cacf3 | 3224 | || type == dfloat128_type_node |
8ce94e44 JM |
3225 | || valtype == dfloat32_type_node |
3226 | || valtype == dfloat64_type_node | |
3227 | || valtype == dfloat128_type_node) | |
c22cacf3 | 3228 | && (formal_prec |
8ce94e44 | 3229 | <= TYPE_PRECISION (valtype) |
9a8ce21f | 3230 | || (type == dfloat128_type_node |
8ce94e44 | 3231 | && (valtype |
c22cacf3 | 3232 | != dfloat64_type_node |
8ce94e44 | 3233 | && (valtype |
9a8ce21f JG |
3234 | != dfloat32_type_node))) |
3235 | || (type == dfloat64_type_node | |
8ce94e44 | 3236 | && (valtype |
9a8ce21f | 3237 | != dfloat32_type_node)))) |
5c1bc275 MP |
3238 | warning_at (ploc, 0, |
3239 | "passing argument %d of %qE as %qT " | |
3240 | "rather than %qT due to prototype", | |
3241 | argnum, rname, type, valtype); | |
9a8ce21f | 3242 | |
d45cf215 | 3243 | } |
3ed56f8a KG |
3244 | /* Detect integer changing in width or signedness. |
3245 | These warnings are only activated with | |
05170031 MLI |
3246 | -Wtraditional-conversion, not with -Wtraditional. */ |
3247 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
8ce94e44 | 3248 | && INTEGRAL_TYPE_P (valtype)) |
400fbf9f | 3249 | { |
d45cf215 RS |
3250 | tree would_have_been = default_conversion (val); |
3251 | tree type1 = TREE_TYPE (would_have_been); | |
3252 | ||
754a4d82 | 3253 | if (TREE_CODE (type) == ENUMERAL_TYPE |
a38b987a | 3254 | && (TYPE_MAIN_VARIANT (type) |
8ce94e44 | 3255 | == TYPE_MAIN_VARIANT (valtype))) |
754a4d82 RS |
3256 | /* No warning if function asks for enum |
3257 | and the actual arg is that enum type. */ | |
3258 | ; | |
3259 | else if (formal_prec != TYPE_PRECISION (type1)) | |
5c1bc275 MP |
3260 | warning_at (ploc, OPT_Wtraditional_conversion, |
3261 | "passing argument %d of %qE " | |
3262 | "with different width due to prototype", | |
3263 | argnum, rname); | |
8df83eae | 3264 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
d45cf215 | 3265 | ; |
800cd3b9 RS |
3266 | /* Don't complain if the formal parameter type |
3267 | is an enum, because we can't tell now whether | |
3268 | the value was an enum--even the same enum. */ | |
3269 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
3270 | ; | |
400fbf9f JW |
3271 | else if (TREE_CODE (val) == INTEGER_CST |
3272 | && int_fits_type_p (val, type)) | |
3273 | /* Change in signedness doesn't matter | |
3274 | if a constant value is unaffected. */ | |
3275 | ; | |
ce9895ae RS |
3276 | /* If the value is extended from a narrower |
3277 | unsigned type, it doesn't matter whether we | |
3278 | pass it as signed or unsigned; the value | |
3279 | certainly is the same either way. */ | |
8ce94e44 JM |
3280 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) |
3281 | && TYPE_UNSIGNED (valtype)) | |
ce9895ae | 3282 | ; |
8df83eae | 3283 | else if (TYPE_UNSIGNED (type)) |
5c1bc275 MP |
3284 | warning_at (ploc, OPT_Wtraditional_conversion, |
3285 | "passing argument %d of %qE " | |
3286 | "as unsigned due to prototype", | |
3287 | argnum, rname); | |
3ed56f8a | 3288 | else |
5c1bc275 MP |
3289 | warning_at (ploc, OPT_Wtraditional_conversion, |
3290 | "passing argument %d of %qE " | |
3291 | "as signed due to prototype", | |
3292 | argnum, rname); | |
400fbf9f JW |
3293 | } |
3294 | } | |
3295 | ||
8ce94e44 JM |
3296 | /* Possibly restore an EXCESS_PRECISION_EXPR for the |
3297 | sake of better warnings from convert_and_check. */ | |
3298 | if (excess_precision) | |
3299 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
9771b263 | 3300 | origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; |
5c1bc275 | 3301 | parmval = convert_for_assignment (loc, ploc, type, |
68fca595 MP |
3302 | val, origtype, ic_argpass, |
3303 | npc, fundecl, function, | |
2ac2f164 | 3304 | parmnum + 1); |
2f6e4e97 | 3305 | |
61f71b34 | 3306 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
b6d6aa84 | 3307 | && INTEGRAL_TYPE_P (type) |
400fbf9f JW |
3308 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
3309 | parmval = default_conversion (parmval); | |
400fbf9f | 3310 | } |
400fbf9f | 3311 | } |
8ce94e44 JM |
3312 | else if (TREE_CODE (valtype) == REAL_TYPE |
3313 | && (TYPE_PRECISION (valtype) | |
2531a1d9 | 3314 | <= TYPE_PRECISION (double_type_node)) |
0fdd4508 JR |
3315 | && TYPE_MAIN_VARIANT (valtype) != double_type_node |
3316 | && TYPE_MAIN_VARIANT (valtype) != long_double_type_node | |
8ce94e44 | 3317 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) |
b5d32c25 KG |
3318 | { |
3319 | if (type_generic) | |
bbbbb16a | 3320 | parmval = val; |
b5d32c25 | 3321 | else |
0a0b3574 MM |
3322 | { |
3323 | /* Convert `float' to `double'. */ | |
3324 | if (warn_double_promotion && !c_inhibit_evaluation_warnings) | |
5c1bc275 MP |
3325 | warning_at (ploc, OPT_Wdouble_promotion, |
3326 | "implicit conversion from %qT to %qT when passing " | |
3327 | "argument to function", | |
3328 | valtype, double_type_node); | |
0a0b3574 MM |
3329 | parmval = convert (double_type_node, val); |
3330 | } | |
b5d32c25 | 3331 | } |
8ce94e44 JM |
3332 | else if (excess_precision && !type_generic) |
3333 | /* A "double" argument with excess precision being passed | |
3334 | without a prototype or in variable arguments. */ | |
bbbbb16a | 3335 | parmval = convert (valtype, val); |
c22cacf3 MS |
3336 | else if ((invalid_func_diag = |
3337 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
4d3e6fae FJ |
3338 | { |
3339 | error (invalid_func_diag); | |
94a0dd7b | 3340 | return -1; |
4d3e6fae | 3341 | } |
1807ffc1 MS |
3342 | else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val)) |
3343 | { | |
3344 | return -1; | |
3345 | } | |
400fbf9f JW |
3346 | else |
3347 | /* Convert `short' and `char' to full-size `int'. */ | |
bbbbb16a ILT |
3348 | parmval = default_conversion (val); |
3349 | ||
9771b263 | 3350 | (*values)[parmnum] = parmval; |
06302a02 JJ |
3351 | if (parmval == error_mark_node) |
3352 | error_args = true; | |
400fbf9f JW |
3353 | |
3354 | if (typetail) | |
3355 | typetail = TREE_CHAIN (typetail); | |
3356 | } | |
3357 | ||
9771b263 | 3358 | gcc_assert (parmnum == vec_safe_length (values)); |
94a0dd7b | 3359 | |
400fbf9f | 3360 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) |
3789b316 | 3361 | { |
68fca595 | 3362 | error_at (loc, "too few arguments to function %qE", function); |
6d6efbb3 BI |
3363 | inform_declaration (fundecl); |
3364 | return -1; | |
3789b316 | 3365 | } |
400fbf9f | 3366 | |
06302a02 | 3367 | return error_args ? -1 : (int) parmnum; |
400fbf9f JW |
3368 | } |
3369 | \f | |
43f6dfd3 RS |
3370 | /* This is the entry point used by the parser to build unary operators |
3371 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3372 | ARG is the operand. For unary plus, the C parser currently uses | |
6a3799eb AH |
3373 | CONVERT_EXPR for code. |
3374 | ||
3375 | LOC is the location to use for the tree generated. | |
3376 | */ | |
43f6dfd3 RS |
3377 | |
3378 | struct c_expr | |
c2255bc4 | 3379 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) |
43f6dfd3 RS |
3380 | { |
3381 | struct c_expr result; | |
3382 | ||
100d537d | 3383 | result.original_code = code; |
6866c6e8 ILT |
3384 | result.original_type = NULL; |
3385 | ||
1807ffc1 MS |
3386 | if (reject_gcc_builtin (arg.value)) |
3387 | { | |
3388 | result.value = error_mark_node; | |
3389 | } | |
3390 | else | |
3391 | { | |
3392 | result.value = build_unary_op (loc, code, arg.value, 0); | |
3393 | ||
59c0753d | 3394 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) |
c2255bc4 | 3395 | overflow_warning (loc, result.value); |
1807ffc1 | 3396 | } |
59c0753d | 3397 | |
43f6dfd3 RS |
3398 | return result; |
3399 | } | |
3400 | ||
3401 | /* This is the entry point used by the parser to build binary operators | |
3402 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3403 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3404 | expression, we check for operands that were written with other binary | |
ba47d38d AH |
3405 | operators in a way that is likely to confuse the user. |
3406 | ||
3407 | LOCATION is the location of the binary operator. */ | |
edc7c4ec | 3408 | |
487a92fe | 3409 | struct c_expr |
ba47d38d AH |
3410 | parser_build_binary_op (location_t location, enum tree_code code, |
3411 | struct c_expr arg1, struct c_expr arg2) | |
400fbf9f | 3412 | { |
487a92fe | 3413 | struct c_expr result; |
400fbf9f | 3414 | |
487a92fe JM |
3415 | enum tree_code code1 = arg1.original_code; |
3416 | enum tree_code code2 = arg2.original_code; | |
6866c6e8 ILT |
3417 | tree type1 = (arg1.original_type |
3418 | ? arg1.original_type | |
3419 | : TREE_TYPE (arg1.value)); | |
3420 | tree type2 = (arg2.original_type | |
3421 | ? arg2.original_type | |
3422 | : TREE_TYPE (arg2.value)); | |
400fbf9f | 3423 | |
ba47d38d AH |
3424 | result.value = build_binary_op (location, code, |
3425 | arg1.value, arg2.value, 1); | |
487a92fe | 3426 | result.original_code = code; |
6866c6e8 | 3427 | result.original_type = NULL; |
58bf601b | 3428 | |
487a92fe JM |
3429 | if (TREE_CODE (result.value) == ERROR_MARK) |
3430 | return result; | |
400fbf9f | 3431 | |
ba47d38d AH |
3432 | if (location != UNKNOWN_LOCATION) |
3433 | protected_set_expr_location (result.value, location); | |
3434 | ||
400fbf9f | 3435 | /* Check for cases such as x+y<<z which users are likely |
487a92fe | 3436 | to misinterpret. */ |
400fbf9f | 3437 | if (warn_parentheses) |
393e8e8b MP |
3438 | warn_about_parentheses (location, code, code1, arg1.value, code2, |
3439 | arg2.value); | |
001af587 | 3440 | |
ca409efd | 3441 | if (warn_logical_op) |
393e8e8b | 3442 | warn_logical_operator (location, code, TREE_TYPE (result.value), |
ca409efd | 3443 | code1, arg1.value, code2, arg2.value); |
63a08740 | 3444 | |
05b28fd6 MP |
3445 | if (warn_tautological_compare) |
3446 | warn_tautological_cmp (location, code, arg1.value, arg2.value); | |
3447 | ||
742938c9 | 3448 | if (warn_logical_not_paren |
7ccb1a11 | 3449 | && TREE_CODE_CLASS (code) == tcc_comparison |
47c2554f | 3450 | && code1 == TRUTH_NOT_EXPR |
01177669 JJ |
3451 | && code2 != TRUTH_NOT_EXPR |
3452 | /* Avoid warning for !!x == y. */ | |
3453 | && (TREE_CODE (arg1.value) != NE_EXPR | |
3454 | || !integer_zerop (TREE_OPERAND (arg1.value, 1)))) | |
3455 | { | |
3456 | /* Avoid warning for !b == y where b has _Bool type. */ | |
3457 | tree t = integer_zero_node; | |
3458 | if (TREE_CODE (arg1.value) == EQ_EXPR | |
3459 | && integer_zerop (TREE_OPERAND (arg1.value, 1)) | |
3460 | && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node) | |
3461 | { | |
3462 | t = TREE_OPERAND (arg1.value, 0); | |
3463 | do | |
3464 | { | |
3465 | if (TREE_TYPE (t) != integer_type_node) | |
3466 | break; | |
3467 | if (TREE_CODE (t) == C_MAYBE_CONST_EXPR) | |
3468 | t = C_MAYBE_CONST_EXPR_EXPR (t); | |
3469 | else if (CONVERT_EXPR_P (t)) | |
3470 | t = TREE_OPERAND (t, 0); | |
3471 | else | |
3472 | break; | |
3473 | } | |
3474 | while (1); | |
3475 | } | |
3476 | if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE) | |
3477 | warn_logical_not_parentheses (location, code, arg2.value); | |
3478 | } | |
742938c9 | 3479 | |
e994a705 RS |
3480 | /* Warn about comparisons against string literals, with the exception |
3481 | of testing for equality or inequality of a string literal with NULL. */ | |
3482 | if (code == EQ_EXPR || code == NE_EXPR) | |
3483 | { | |
3484 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3485 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
c2255bc4 AH |
3486 | warning_at (location, OPT_Waddress, |
3487 | "comparison with string literal results in unspecified behavior"); | |
e994a705 RS |
3488 | } |
3489 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3490 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
c2255bc4 AH |
3491 | warning_at (location, OPT_Waddress, |
3492 | "comparison with string literal results in unspecified behavior"); | |
e994a705 | 3493 | |
b8698a0f L |
3494 | if (TREE_OVERFLOW_P (result.value) |
3495 | && !TREE_OVERFLOW_P (arg1.value) | |
59c0753d | 3496 | && !TREE_OVERFLOW_P (arg2.value)) |
c2255bc4 | 3497 | overflow_warning (location, result.value); |
400fbf9f | 3498 | |
6866c6e8 ILT |
3499 | /* Warn about comparisons of different enum types. */ |
3500 | if (warn_enum_compare | |
3501 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3502 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3503 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3504 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3505 | warning_at (location, OPT_Wenum_compare, | |
3506 | "comparison between %qT and %qT", | |
3507 | type1, type2); | |
3508 | ||
400fbf9f JW |
3509 | return result; |
3510 | } | |
3e4093b6 | 3511 | \f |
3e4093b6 RS |
3512 | /* Return a tree for the difference of pointers OP0 and OP1. |
3513 | The resulting tree has type int. */ | |
293c9fdd | 3514 | |
3e4093b6 | 3515 | static tree |
db3927fb | 3516 | pointer_diff (location_t loc, tree op0, tree op1) |
3e4093b6 | 3517 | { |
3e4093b6 | 3518 | tree restype = ptrdiff_type_node; |
36c5e70a | 3519 | tree result, inttype; |
400fbf9f | 3520 | |
36c5e70a BE |
3521 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); |
3522 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
3e4093b6 | 3523 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
3e4093b6 | 3524 | tree orig_op1 = op1; |
400fbf9f | 3525 | |
36c5e70a BE |
3526 | /* If the operands point into different address spaces, we need to |
3527 | explicitly convert them to pointers into the common address space | |
3528 | before we can subtract the numerical address values. */ | |
3529 | if (as0 != as1) | |
3530 | { | |
3531 | addr_space_t as_common; | |
3532 | tree common_type; | |
3533 | ||
3534 | /* Determine the common superset address space. This is guaranteed | |
3535 | to exist because the caller verified that comp_target_types | |
3536 | returned non-zero. */ | |
3537 | if (!addr_space_superset (as0, as1, &as_common)) | |
3538 | gcc_unreachable (); | |
3539 | ||
3540 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3541 | op0 = convert (common_type, op0); | |
3542 | op1 = convert (common_type, op1); | |
3543 | } | |
3544 | ||
3545 | /* Determine integer type to perform computations in. This will usually | |
3546 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3547 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3548 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
647d4b75 | 3549 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); |
36c5e70a BE |
3550 | else |
3551 | inttype = restype; | |
3552 | ||
fcf73884 | 3553 | if (TREE_CODE (target_type) == VOID_TYPE) |
44d90fe1 | 3554 | pedwarn (loc, OPT_Wpointer_arith, |
fcf73884 MLI |
3555 | "pointer of type %<void *%> used in subtraction"); |
3556 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
44d90fe1 | 3557 | pedwarn (loc, OPT_Wpointer_arith, |
fcf73884 | 3558 | "pointer to a function used in subtraction"); |
400fbf9f | 3559 | |
3e4093b6 RS |
3560 | /* First do the subtraction as integers; |
3561 | then drop through to build the divide operator. | |
3562 | Do not do default conversions on the minus operator | |
3563 | in case restype is a short type. */ | |
400fbf9f | 3564 | |
db3927fb | 3565 | op0 = build_binary_op (loc, |
36c5e70a BE |
3566 | MINUS_EXPR, convert (inttype, op0), |
3567 | convert (inttype, op1), 0); | |
3e4093b6 RS |
3568 | /* This generates an error if op1 is pointer to incomplete type. */ |
3569 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
db3927fb | 3570 | error_at (loc, "arithmetic on pointer to an incomplete type"); |
400fbf9f | 3571 | |
3e4093b6 | 3572 | op1 = c_size_in_bytes (target_type); |
400fbf9f | 3573 | |
f04dda30 MP |
3574 | if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1))) |
3575 | error_at (loc, "arithmetic on pointer to an empty aggregate"); | |
3576 | ||
3e4093b6 | 3577 | /* Divide by the size, in easiest possible way. */ |
36c5e70a BE |
3578 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, |
3579 | op0, convert (inttype, op1)); | |
3580 | ||
3581 | /* Convert to final result type if necessary. */ | |
3582 | return convert (restype, result); | |
3e4093b6 RS |
3583 | } |
3584 | \f | |
267bac10 JM |
3585 | /* Expand atomic compound assignments into an approriate sequence as |
3586 | specified by the C11 standard section 6.5.16.2. | |
3587 | given | |
3588 | _Atomic T1 E1 | |
3589 | T2 E2 | |
3590 | E1 op= E2 | |
3591 | ||
3592 | This sequence is used for all types for which these operations are | |
3593 | supported. | |
3594 | ||
3595 | In addition, built-in versions of the 'fe' prefixed routines may | |
3596 | need to be invoked for floating point (real, complex or vector) when | |
3597 | floating-point exceptions are supported. See 6.5.16.2 footnote 113. | |
3598 | ||
3599 | T1 newval; | |
3600 | T1 old; | |
3601 | T1 *addr | |
3602 | T2 val | |
3603 | fenv_t fenv | |
3604 | ||
3605 | addr = &E1; | |
3606 | val = (E2); | |
3607 | __atomic_load (addr, &old, SEQ_CST); | |
3608 | feholdexcept (&fenv); | |
3609 | loop: | |
3610 | newval = old op val; | |
3611 | if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST, | |
3612 | SEQ_CST)) | |
3613 | goto done; | |
3614 | feclearexcept (FE_ALL_EXCEPT); | |
3615 | goto loop: | |
3616 | done: | |
3617 | feupdateenv (&fenv); | |
3618 | ||
3619 | Also note that the compiler is simply issuing the generic form of | |
3620 | the atomic operations. This requires temp(s) and has their address | |
3621 | taken. The atomic processing is smart enough to figure out when the | |
3622 | size of an object can utilize a lock-free version, and convert the | |
3623 | built-in call to the appropriate lock-free routine. The optimizers | |
3624 | will then dispose of any temps that are no longer required, and | |
3625 | lock-free implementations are utilized as long as there is target | |
3626 | support for the required size. | |
3627 | ||
3628 | If the operator is NOP_EXPR, then this is a simple assignment, and | |
3629 | an __atomic_store is issued to perform the assignment rather than | |
3630 | the above loop. | |
3631 | ||
3632 | */ | |
3633 | ||
3634 | /* Build an atomic assignment at LOC, expanding into the proper | |
3635 | sequence to store LHS MODIFYCODE= RHS. Return a value representing | |
3636 | the result of the operation, unless RETURN_OLD_P in which case | |
3637 | return the old value of LHS (this is only for postincrement and | |
3638 | postdecrement). */ | |
3639 | static tree | |
3640 | build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode, | |
3641 | tree rhs, bool return_old_p) | |
3642 | { | |
3643 | tree fndecl, func_call; | |
3644 | vec<tree, va_gc> *params; | |
3645 | tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr; | |
3646 | tree old, old_addr; | |
3647 | tree compound_stmt; | |
3648 | tree stmt, goto_stmt; | |
3649 | tree loop_label, loop_decl, done_label, done_decl; | |
3650 | ||
3651 | tree lhs_type = TREE_TYPE (lhs); | |
3652 | tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0); | |
3653 | tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); | |
3654 | tree rhs_type = TREE_TYPE (rhs); | |
3655 | ||
3656 | gcc_assert (TYPE_ATOMIC (lhs_type)); | |
3657 | ||
3658 | if (return_old_p) | |
3659 | gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR); | |
3660 | ||
3661 | /* Allocate enough vector items for a compare_exchange. */ | |
3662 | vec_alloc (params, 6); | |
3663 | ||
3664 | /* Create a compound statement to hold the sequence of statements | |
3665 | with a loop. */ | |
3666 | compound_stmt = c_begin_compound_stmt (false); | |
3667 | ||
3668 | /* Fold the RHS if it hasn't already been folded. */ | |
3669 | if (modifycode != NOP_EXPR) | |
3670 | rhs = c_fully_fold (rhs, false, NULL); | |
3671 | ||
3672 | /* Remove the qualifiers for the rest of the expressions and create | |
3673 | the VAL temp variable to hold the RHS. */ | |
3674 | nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED); | |
3675 | nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED); | |
5c4abbb8 | 3676 | val = create_tmp_var_raw (nonatomic_rhs_type); |
267bac10 | 3677 | TREE_ADDRESSABLE (val) = 1; |
cc28fc7f | 3678 | TREE_NO_WARNING (val) = 1; |
5c4abbb8 MP |
3679 | rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE, |
3680 | NULL_TREE); | |
267bac10 JM |
3681 | SET_EXPR_LOCATION (rhs, loc); |
3682 | add_stmt (rhs); | |
3683 | ||
3684 | /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue | |
3685 | an atomic_store. */ | |
3686 | if (modifycode == NOP_EXPR) | |
3687 | { | |
3688 | /* Build __atomic_store (&lhs, &val, SEQ_CST) */ | |
3689 | rhs = build_unary_op (loc, ADDR_EXPR, val, 0); | |
3690 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE); | |
3691 | params->quick_push (lhs_addr); | |
3692 | params->quick_push (rhs); | |
3693 | params->quick_push (seq_cst); | |
8edbfaa6 | 3694 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 JM |
3695 | add_stmt (func_call); |
3696 | ||
3697 | /* Finish the compound statement. */ | |
3698 | compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); | |
3699 | ||
3700 | /* VAL is the value which was stored, return a COMPOUND_STMT of | |
3701 | the statement and that value. */ | |
3702 | return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val); | |
3703 | } | |
3704 | ||
3705 | /* Create the variables and labels required for the op= form. */ | |
5c4abbb8 | 3706 | old = create_tmp_var_raw (nonatomic_lhs_type); |
267bac10 | 3707 | old_addr = build_unary_op (loc, ADDR_EXPR, old, 0); |
cc28fc7f MP |
3708 | TREE_ADDRESSABLE (old) = 1; |
3709 | TREE_NO_WARNING (old) = 1; | |
267bac10 | 3710 | |
5c4abbb8 | 3711 | newval = create_tmp_var_raw (nonatomic_lhs_type); |
267bac10 JM |
3712 | newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0); |
3713 | TREE_ADDRESSABLE (newval) = 1; | |
3714 | ||
3715 | loop_decl = create_artificial_label (loc); | |
3716 | loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl); | |
3717 | ||
3718 | done_decl = create_artificial_label (loc); | |
3719 | done_label = build1 (LABEL_EXPR, void_type_node, done_decl); | |
3720 | ||
3721 | /* __atomic_load (addr, &old, SEQ_CST). */ | |
3722 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); | |
3723 | params->quick_push (lhs_addr); | |
3724 | params->quick_push (old_addr); | |
3725 | params->quick_push (seq_cst); | |
8edbfaa6 | 3726 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
5c4abbb8 MP |
3727 | old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE, |
3728 | NULL_TREE); | |
3729 | add_stmt (old); | |
267bac10 JM |
3730 | params->truncate (0); |
3731 | ||
3732 | /* Create the expressions for floating-point environment | |
3733 | manipulation, if required. */ | |
3734 | bool need_fenv = (flag_trapping_math | |
3735 | && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type))); | |
3736 | tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE; | |
3737 | if (need_fenv) | |
3738 | targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call); | |
3739 | ||
3740 | if (hold_call) | |
3741 | add_stmt (hold_call); | |
3742 | ||
3743 | /* loop: */ | |
3744 | add_stmt (loop_label); | |
3745 | ||
3746 | /* newval = old + val; */ | |
3747 | rhs = build_binary_op (loc, modifycode, old, val, 1); | |
5c4abbb8 | 3748 | rhs = c_fully_fold (rhs, false, NULL); |
68fca595 MP |
3749 | rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type, |
3750 | rhs, NULL_TREE, ic_assign, false, NULL_TREE, | |
267bac10 JM |
3751 | NULL_TREE, 0); |
3752 | if (rhs != error_mark_node) | |
3753 | { | |
5c4abbb8 MP |
3754 | rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE, |
3755 | NULL_TREE); | |
267bac10 JM |
3756 | SET_EXPR_LOCATION (rhs, loc); |
3757 | add_stmt (rhs); | |
3758 | } | |
3759 | ||
3760 | /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST)) | |
3761 | goto done; */ | |
3762 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE); | |
3763 | params->quick_push (lhs_addr); | |
3764 | params->quick_push (old_addr); | |
3765 | params->quick_push (newval_addr); | |
3766 | params->quick_push (integer_zero_node); | |
3767 | params->quick_push (seq_cst); | |
3768 | params->quick_push (seq_cst); | |
8edbfaa6 | 3769 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 JM |
3770 | |
3771 | goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl); | |
3772 | SET_EXPR_LOCATION (goto_stmt, loc); | |
3773 | ||
3774 | stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE); | |
3775 | SET_EXPR_LOCATION (stmt, loc); | |
3776 | add_stmt (stmt); | |
5c4abbb8 | 3777 | |
267bac10 JM |
3778 | if (clear_call) |
3779 | add_stmt (clear_call); | |
3780 | ||
3781 | /* goto loop; */ | |
3782 | goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl); | |
3783 | SET_EXPR_LOCATION (goto_stmt, loc); | |
3784 | add_stmt (goto_stmt); | |
5c4abbb8 | 3785 | |
267bac10 JM |
3786 | /* done: */ |
3787 | add_stmt (done_label); | |
3788 | ||
3789 | if (update_call) | |
3790 | add_stmt (update_call); | |
3791 | ||
3792 | /* Finish the compound statement. */ | |
3793 | compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); | |
3794 | ||
3795 | /* NEWVAL is the value that was successfully stored, return a | |
3796 | COMPOUND_EXPR of the statement and the appropriate value. */ | |
3797 | return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, | |
3798 | return_old_p ? old : newval); | |
3799 | } | |
3800 | ||
3e4093b6 RS |
3801 | /* Construct and perhaps optimize a tree representation |
3802 | for a unary operation. CODE, a tree_code, specifies the operation | |
3803 | and XARG is the operand. | |
3804 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3805 | the default promotions (such as from short to int). | |
3806 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3807 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
c9f9eb5d AH |
3808 | arrays to pointers in C99. |
3809 | ||
3810 | LOCATION is the location of the operator. */ | |
400fbf9f | 3811 | |
3e4093b6 | 3812 | tree |
c9f9eb5d AH |
3813 | build_unary_op (location_t location, |
3814 | enum tree_code code, tree xarg, int flag) | |
3e4093b6 RS |
3815 | { |
3816 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3817 | tree arg = xarg; | |
3818 | tree argtype = 0; | |
808d6eaa | 3819 | enum tree_code typecode; |
3e4093b6 | 3820 | tree val; |
c9f9eb5d | 3821 | tree ret = error_mark_node; |
8ce94e44 | 3822 | tree eptype = NULL_TREE; |
3e4093b6 | 3823 | int noconvert = flag; |
4de67c26 | 3824 | const char *invalid_op_diag; |
928c19bb JM |
3825 | bool int_operands; |
3826 | ||
3827 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
4d84fe7c JM |
3828 | if (int_operands) |
3829 | arg = remove_c_maybe_const_expr (arg); | |
400fbf9f | 3830 | |
808d6eaa JM |
3831 | if (code != ADDR_EXPR) |
3832 | arg = require_complete_type (arg); | |
3833 | ||
3834 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
3e4093b6 RS |
3835 | if (typecode == ERROR_MARK) |
3836 | return error_mark_node; | |
3837 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3838 | typecode = INTEGER_TYPE; | |
6c36d76b | 3839 | |
4de67c26 JM |
3840 | if ((invalid_op_diag |
3841 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3842 | { | |
c9f9eb5d | 3843 | error_at (location, invalid_op_diag); |
4de67c26 JM |
3844 | return error_mark_node; |
3845 | } | |
3846 | ||
8ce94e44 JM |
3847 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
3848 | { | |
3849 | eptype = TREE_TYPE (arg); | |
3850 | arg = TREE_OPERAND (arg, 0); | |
3851 | } | |
3852 | ||
3e4093b6 RS |
3853 | switch (code) |
3854 | { | |
3855 | case CONVERT_EXPR: | |
3856 | /* This is used for unary plus, because a CONVERT_EXPR | |
3857 | is enough to prevent anybody from looking inside for | |
3858 | associativity, but won't generate any code. */ | |
3859 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3860 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
8a2cee38 | 3861 | || typecode == VECTOR_TYPE)) |
400fbf9f | 3862 | { |
c9f9eb5d | 3863 | error_at (location, "wrong type argument to unary plus"); |
3e4093b6 | 3864 | return error_mark_node; |
400fbf9f | 3865 | } |
3e4093b6 RS |
3866 | else if (!noconvert) |
3867 | arg = default_conversion (arg); | |
db3927fb | 3868 | arg = non_lvalue_loc (location, arg); |
400fbf9f JW |
3869 | break; |
3870 | ||
3e4093b6 RS |
3871 | case NEGATE_EXPR: |
3872 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3873 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
3e4093b6 RS |
3874 | || typecode == VECTOR_TYPE)) |
3875 | { | |
c9f9eb5d | 3876 | error_at (location, "wrong type argument to unary minus"); |
3e4093b6 RS |
3877 | return error_mark_node; |
3878 | } | |
3879 | else if (!noconvert) | |
3880 | arg = default_conversion (arg); | |
400fbf9f JW |
3881 | break; |
3882 | ||
3e4093b6 | 3883 | case BIT_NOT_EXPR: |
462643f0 AP |
3884 | /* ~ works on integer types and non float vectors. */ |
3885 | if (typecode == INTEGER_TYPE | |
3886 | || (typecode == VECTOR_TYPE | |
3887 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
03d5b1f5 | 3888 | { |
3e4093b6 RS |
3889 | if (!noconvert) |
3890 | arg = default_conversion (arg); | |
03d5b1f5 | 3891 | } |
3e4093b6 | 3892 | else if (typecode == COMPLEX_TYPE) |
400fbf9f | 3893 | { |
3e4093b6 | 3894 | code = CONJ_EXPR; |
c1771a20 | 3895 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 3896 | "ISO C does not support %<~%> for complex conjugation"); |
3e4093b6 RS |
3897 | if (!noconvert) |
3898 | arg = default_conversion (arg); | |
3899 | } | |
3900 | else | |
3901 | { | |
c9f9eb5d | 3902 | error_at (location, "wrong type argument to bit-complement"); |
3e4093b6 | 3903 | return error_mark_node; |
400fbf9f JW |
3904 | } |
3905 | break; | |
3906 | ||
3e4093b6 | 3907 | case ABS_EXPR: |
11017cc7 | 3908 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
400fbf9f | 3909 | { |
c9f9eb5d | 3910 | error_at (location, "wrong type argument to abs"); |
3e4093b6 | 3911 | return error_mark_node; |
400fbf9f | 3912 | } |
3e4093b6 RS |
3913 | else if (!noconvert) |
3914 | arg = default_conversion (arg); | |
400fbf9f JW |
3915 | break; |
3916 | ||
3e4093b6 RS |
3917 | case CONJ_EXPR: |
3918 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3919 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3920 | || typecode == COMPLEX_TYPE)) | |
400fbf9f | 3921 | { |
c9f9eb5d | 3922 | error_at (location, "wrong type argument to conjugation"); |
3e4093b6 | 3923 | return error_mark_node; |
400fbf9f | 3924 | } |
3e4093b6 RS |
3925 | else if (!noconvert) |
3926 | arg = default_conversion (arg); | |
400fbf9f JW |
3927 | break; |
3928 | ||
3e4093b6 | 3929 | case TRUTH_NOT_EXPR: |
ab22c1fa | 3930 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3931 | && typecode != REAL_TYPE && typecode != POINTER_TYPE |
46bdb9cf | 3932 | && typecode != COMPLEX_TYPE) |
400fbf9f | 3933 | { |
c9f9eb5d AH |
3934 | error_at (location, |
3935 | "wrong type argument to unary exclamation mark"); | |
3e4093b6 | 3936 | return error_mark_node; |
400fbf9f | 3937 | } |
a27d595d JM |
3938 | if (int_operands) |
3939 | { | |
3940 | arg = c_objc_common_truthvalue_conversion (location, xarg); | |
3941 | arg = remove_c_maybe_const_expr (arg); | |
3942 | } | |
3943 | else | |
3944 | arg = c_objc_common_truthvalue_conversion (location, arg); | |
db3927fb | 3945 | ret = invert_truthvalue_loc (location, arg); |
ca80e52b EB |
3946 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ |
3947 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3948 | location = EXPR_LOCATION (ret); | |
c9f9eb5d | 3949 | goto return_build_unary_op; |
3e4093b6 | 3950 | |
3e4093b6 | 3951 | case REALPART_EXPR: |
3e4093b6 | 3952 | case IMAGPART_EXPR: |
fb52b50a NF |
3953 | ret = build_real_imag_expr (location, code, arg); |
3954 | if (ret == error_mark_node) | |
3955 | return error_mark_node; | |
8ce94e44 JM |
3956 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3957 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3958 | goto return_build_unary_op; |
3e4093b6 RS |
3959 | |
3960 | case PREINCREMENT_EXPR: | |
3961 | case POSTINCREMENT_EXPR: | |
3962 | case PREDECREMENT_EXPR: | |
3963 | case POSTDECREMENT_EXPR: | |
3e4093b6 | 3964 | |
928c19bb JM |
3965 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) |
3966 | { | |
3967 | tree inner = build_unary_op (location, code, | |
3968 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3969 | if (inner == error_mark_node) | |
3970 | return error_mark_node; | |
3971 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3972 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3973 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3974 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3975 | goto return_build_unary_op; | |
3976 | } | |
3977 | ||
925e8657 NP |
3978 | /* Complain about anything that is not a true lvalue. In |
3979 | Objective-C, skip this check for property_refs. */ | |
f90e8e2e | 3980 | if (!objc_is_property_ref (arg) |
7bd11157 TT |
3981 | && !lvalue_or_else (location, |
3982 | arg, ((code == PREINCREMENT_EXPR | |
925e8657 NP |
3983 | || code == POSTINCREMENT_EXPR) |
3984 | ? lv_increment | |
3985 | : lv_decrement))) | |
928c19bb JM |
3986 | return error_mark_node; |
3987 | ||
09639a83 ILT |
3988 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) |
3989 | { | |
3990 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3991 | warning_at (location, OPT_Wc___compat, | |
3992 | "increment of enumeration value is invalid in C++"); | |
3993 | else | |
3994 | warning_at (location, OPT_Wc___compat, | |
3995 | "decrement of enumeration value is invalid in C++"); | |
3996 | } | |
3997 | ||
928c19bb JM |
3998 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ |
3999 | arg = c_fully_fold (arg, false, NULL); | |
4000 | ||
267bac10 JM |
4001 | bool atomic_op; |
4002 | atomic_op = really_atomic_lvalue (arg); | |
4003 | ||
3e4093b6 RS |
4004 | /* Increment or decrement the real part of the value, |
4005 | and don't change the imaginary part. */ | |
4006 | if (typecode == COMPLEX_TYPE) | |
400fbf9f | 4007 | { |
3e4093b6 RS |
4008 | tree real, imag; |
4009 | ||
c1771a20 | 4010 | pedwarn (location, OPT_Wpedantic, |
509c9d60 | 4011 | "ISO C does not support %<++%> and %<--%> on complex types"); |
3e4093b6 | 4012 | |
267bac10 JM |
4013 | if (!atomic_op) |
4014 | { | |
4015 | arg = stabilize_reference (arg); | |
4016 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); | |
4017 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
4018 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
4019 | if (real == error_mark_node || imag == error_mark_node) | |
4020 | return error_mark_node; | |
4021 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), | |
4022 | real, imag); | |
4023 | goto return_build_unary_op; | |
4024 | } | |
400fbf9f | 4025 | } |
3e4093b6 RS |
4026 | |
4027 | /* Report invalid types. */ | |
4028 | ||
ab22c1fa | 4029 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE |
267bac10 | 4030 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE |
241b71bb | 4031 | && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE) |
400fbf9f | 4032 | { |
3e4093b6 | 4033 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c9f9eb5d | 4034 | error_at (location, "wrong type argument to increment"); |
c22cacf3 | 4035 | else |
c9f9eb5d | 4036 | error_at (location, "wrong type argument to decrement"); |
3e4093b6 RS |
4037 | |
4038 | return error_mark_node; | |
400fbf9f | 4039 | } |
400fbf9f | 4040 | |
3e4093b6 RS |
4041 | { |
4042 | tree inc; | |
400fbf9f | 4043 | |
3e4093b6 RS |
4044 | argtype = TREE_TYPE (arg); |
4045 | ||
4046 | /* Compute the increment. */ | |
4047 | ||
4048 | if (typecode == POINTER_TYPE) | |
4049 | { | |
a0e24419 | 4050 | /* If pointer target is an incomplete type, |
3e4093b6 | 4051 | we just cannot know how to do the arithmetic. */ |
b70cef5d | 4052 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) |
3e4093b6 RS |
4053 | { |
4054 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
c9f9eb5d | 4055 | error_at (location, |
a0e24419 MP |
4056 | "increment of pointer to an incomplete type %qT", |
4057 | TREE_TYPE (argtype)); | |
3e4093b6 | 4058 | else |
c9f9eb5d | 4059 | error_at (location, |
a0e24419 MP |
4060 | "decrement of pointer to an incomplete type %qT", |
4061 | TREE_TYPE (argtype)); | |
3e4093b6 | 4062 | } |
b70cef5d JJ |
4063 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE |
4064 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
c22cacf3 | 4065 | { |
3e4093b6 | 4066 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
44d90fe1 | 4067 | pedwarn (location, OPT_Wpointer_arith, |
fcf73884 | 4068 | "wrong type argument to increment"); |
3e4093b6 | 4069 | else |
44d90fe1 | 4070 | pedwarn (location, OPT_Wpointer_arith, |
fcf73884 | 4071 | "wrong type argument to decrement"); |
3e4093b6 RS |
4072 | } |
4073 | ||
b70cef5d | 4074 | inc = c_size_in_bytes (TREE_TYPE (argtype)); |
0d82a1c8 | 4075 | inc = convert_to_ptrofftype_loc (location, inc); |
3e4093b6 | 4076 | } |
b70cef5d | 4077 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) |
ab22c1fa CF |
4078 | { |
4079 | /* For signed fract types, we invert ++ to -- or | |
4080 | -- to ++, and change inc from 1 to -1, because | |
4081 | it is not possible to represent 1 in signed fract constants. | |
4082 | For unsigned fract types, the result always overflows and | |
4083 | we get an undefined (original) or the maximum value. */ | |
4084 | if (code == PREINCREMENT_EXPR) | |
4085 | code = PREDECREMENT_EXPR; | |
4086 | else if (code == PREDECREMENT_EXPR) | |
4087 | code = PREINCREMENT_EXPR; | |
4088 | else if (code == POSTINCREMENT_EXPR) | |
4089 | code = POSTDECREMENT_EXPR; | |
4090 | else /* code == POSTDECREMENT_EXPR */ | |
4091 | code = POSTINCREMENT_EXPR; | |
4092 | ||
4093 | inc = integer_minus_one_node; | |
4094 | inc = convert (argtype, inc); | |
4095 | } | |
3e4093b6 | 4096 | else |
5be014d5 | 4097 | { |
241b71bb TV |
4098 | inc = VECTOR_TYPE_P (argtype) |
4099 | ? build_one_cst (argtype) | |
4100 | : integer_one_node; | |
5be014d5 AP |
4101 | inc = convert (argtype, inc); |
4102 | } | |
3e4093b6 | 4103 | |
925e8657 NP |
4104 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
4105 | need to ask Objective-C to build the increment or decrement | |
4106 | expression for it. */ | |
4107 | if (objc_is_property_ref (arg)) | |
f90e8e2e | 4108 | return objc_build_incr_expr_for_property_ref (location, code, |
925e8657 NP |
4109 | arg, inc); |
4110 | ||
3e4093b6 | 4111 | /* Report a read-only lvalue. */ |
f37acdf9 | 4112 | if (TYPE_READONLY (argtype)) |
953ff289 | 4113 | { |
c02065fc | 4114 | readonly_error (location, arg, |
953ff289 DN |
4115 | ((code == PREINCREMENT_EXPR |
4116 | || code == POSTINCREMENT_EXPR) | |
4117 | ? lv_increment : lv_decrement)); | |
4118 | return error_mark_node; | |
4119 | } | |
f37acdf9 JM |
4120 | else if (TREE_READONLY (arg)) |
4121 | readonly_warning (arg, | |
4122 | ((code == PREINCREMENT_EXPR | |
4123 | || code == POSTINCREMENT_EXPR) | |
4124 | ? lv_increment : lv_decrement)); | |
3e4093b6 | 4125 | |
267bac10 JM |
4126 | /* If the argument is atomic, use the special code sequences for |
4127 | atomic compound assignment. */ | |
4128 | if (atomic_op) | |
4129 | { | |
4130 | arg = stabilize_reference (arg); | |
4131 | ret = build_atomic_assign (location, arg, | |
4132 | ((code == PREINCREMENT_EXPR | |
4133 | || code == POSTINCREMENT_EXPR) | |
4134 | ? PLUS_EXPR | |
4135 | : MINUS_EXPR), | |
4136 | (FRACT_MODE_P (TYPE_MODE (argtype)) | |
4137 | ? inc | |
4138 | : integer_one_node), | |
4139 | (code == POSTINCREMENT_EXPR | |
4140 | || code == POSTDECREMENT_EXPR)); | |
4141 | goto return_build_unary_op; | |
4142 | } | |
4143 | ||
3e4093b6 RS |
4144 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) |
4145 | val = boolean_increment (code, arg); | |
4146 | else | |
53fb4de3 | 4147 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
3e4093b6 | 4148 | TREE_SIDE_EFFECTS (val) = 1; |
3e4093b6 | 4149 | if (TREE_CODE (val) != code) |
6de9cd9a | 4150 | TREE_NO_WARNING (val) = 1; |
c9f9eb5d AH |
4151 | ret = val; |
4152 | goto return_build_unary_op; | |
3e4093b6 RS |
4153 | } |
4154 | ||
4155 | case ADDR_EXPR: | |
4156 | /* Note that this operation never does default_conversion. */ | |
4157 | ||
2b4b7036 JM |
4158 | /* The operand of unary '&' must be an lvalue (which excludes |
4159 | expressions of type void), or, in C99, the result of a [] or | |
4160 | unary '*' operator. */ | |
4161 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
4162 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
22d03525 | 4163 | && (!INDIRECT_REF_P (arg) || !flag_isoc99)) |
2b4b7036 JM |
4164 | pedwarn (location, 0, "taking address of expression of type %<void%>"); |
4165 | ||
3e4093b6 | 4166 | /* Let &* cancel out to simplify resulting code. */ |
22d03525 | 4167 | if (INDIRECT_REF_P (arg)) |
400fbf9f | 4168 | { |
3e4093b6 RS |
4169 | /* Don't let this be an lvalue. */ |
4170 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
db3927fb | 4171 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); |
c9f9eb5d AH |
4172 | ret = TREE_OPERAND (arg, 0); |
4173 | goto return_build_unary_op; | |
400fbf9f | 4174 | } |
1eb8759b | 4175 | |
7c672dfc | 4176 | /* For &x[y], return x+y */ |
3e4093b6 | 4177 | if (TREE_CODE (arg) == ARRAY_REF) |
1eb8759b | 4178 | { |
f2a71bbc JM |
4179 | tree op0 = TREE_OPERAND (arg, 0); |
4180 | if (!c_mark_addressable (op0)) | |
3e4093b6 | 4181 | return error_mark_node; |
1eb8759b | 4182 | } |
1eb8759b | 4183 | |
3e4093b6 RS |
4184 | /* Anything not already handled and not a true memory reference |
4185 | or a non-lvalue array is an error. */ | |
4186 | else if (typecode != FUNCTION_TYPE && !flag | |
7bd11157 | 4187 | && !lvalue_or_else (location, arg, lv_addressof)) |
3e4093b6 | 4188 | return error_mark_node; |
b6a10c9f | 4189 | |
928c19bb JM |
4190 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify |
4191 | folding later. */ | |
4192 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
4193 | { | |
4194 | tree inner = build_unary_op (location, code, | |
4195 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
4196 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4197 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
4198 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
4199 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
4200 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
4201 | goto return_build_unary_op; | |
4202 | } | |
4203 | ||
3e4093b6 RS |
4204 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
4205 | argtype = TREE_TYPE (arg); | |
400fbf9f | 4206 | |
3e4093b6 | 4207 | /* If the lvalue is const or volatile, merge that into the type |
e73a83fc | 4208 | to which the address will point. This is only needed |
f2c1da78 | 4209 | for function types. */ |
6615c446 | 4210 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) |
e73a83fc RB |
4211 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) |
4212 | && TREE_CODE (argtype) == FUNCTION_TYPE) | |
f2c1da78 JM |
4213 | { |
4214 | int orig_quals = TYPE_QUALS (strip_array_types (argtype)); | |
4215 | int quals = orig_quals; | |
4216 | ||
4217 | if (TREE_READONLY (arg)) | |
4218 | quals |= TYPE_QUAL_CONST; | |
4219 | if (TREE_THIS_VOLATILE (arg)) | |
4220 | quals |= TYPE_QUAL_VOLATILE; | |
4221 | ||
f2c1da78 JM |
4222 | argtype = c_build_qualified_type (argtype, quals); |
4223 | } | |
400fbf9f | 4224 | |
3e4093b6 RS |
4225 | if (!c_mark_addressable (arg)) |
4226 | return error_mark_node; | |
400fbf9f | 4227 | |
abb54d14 JM |
4228 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF |
4229 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
400fbf9f | 4230 | |
5cc200fc | 4231 | argtype = build_pointer_type (argtype); |
5e55f99d RH |
4232 | |
4233 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
4234 | when we have proper support for integer constant expressions. */ | |
4235 | val = get_base_address (arg); | |
22d03525 | 4236 | if (val && INDIRECT_REF_P (val) |
3aa2ddb8 JJ |
4237 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
4238 | { | |
cf9e9959 | 4239 | ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg)); |
c9f9eb5d | 4240 | goto return_build_unary_op; |
3aa2ddb8 | 4241 | } |
5e55f99d | 4242 | |
5cc200fc | 4243 | val = build1 (ADDR_EXPR, argtype, arg); |
400fbf9f | 4244 | |
c9f9eb5d AH |
4245 | ret = val; |
4246 | goto return_build_unary_op; | |
400fbf9f | 4247 | |
3e4093b6 | 4248 | default: |
1344f9a3 | 4249 | gcc_unreachable (); |
3e4093b6 | 4250 | } |
400fbf9f | 4251 | |
3e4093b6 RS |
4252 | if (argtype == 0) |
4253 | argtype = TREE_TYPE (arg); | |
928c19bb JM |
4254 | if (TREE_CODE (arg) == INTEGER_CST) |
4255 | ret = (require_constant_value | |
db3927fb AH |
4256 | ? fold_build1_initializer_loc (location, code, argtype, arg) |
4257 | : fold_build1_loc (location, code, argtype, arg)); | |
928c19bb JM |
4258 | else |
4259 | ret = build1 (code, argtype, arg); | |
c9f9eb5d AH |
4260 | return_build_unary_op: |
4261 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
4262 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) |
4263 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
4264 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
4265 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
4266 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
4267 | if (eptype) |
4268 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
c9f9eb5d AH |
4269 | protected_set_expr_location (ret, location); |
4270 | return ret; | |
3e4093b6 | 4271 | } |
400fbf9f | 4272 | |
3e4093b6 RS |
4273 | /* Return nonzero if REF is an lvalue valid for this language. |
4274 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
5baeaac0 | 4275 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
400fbf9f | 4276 | |
1e4ae551 | 4277 | bool |
58f9752a | 4278 | lvalue_p (const_tree ref) |
3e4093b6 | 4279 | { |
58f9752a | 4280 | const enum tree_code code = TREE_CODE (ref); |
400fbf9f | 4281 | |
3e4093b6 RS |
4282 | switch (code) |
4283 | { | |
4284 | case REALPART_EXPR: | |
4285 | case IMAGPART_EXPR: | |
4286 | case COMPONENT_REF: | |
4287 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
400fbf9f | 4288 | |
928c19bb JM |
4289 | case C_MAYBE_CONST_EXPR: |
4290 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
4291 | ||
3e4093b6 RS |
4292 | case COMPOUND_LITERAL_EXPR: |
4293 | case STRING_CST: | |
4294 | return 1; | |
400fbf9f | 4295 | |
3e4093b6 RS |
4296 | case INDIRECT_REF: |
4297 | case ARRAY_REF: | |
36536d79 | 4298 | case ARRAY_NOTATION_REF: |
3e4093b6 RS |
4299 | case VAR_DECL: |
4300 | case PARM_DECL: | |
4301 | case RESULT_DECL: | |
4302 | case ERROR_MARK: | |
4303 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
4304 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
665f2503 | 4305 | |
3e4093b6 | 4306 | case BIND_EXPR: |
3e4093b6 | 4307 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
665f2503 | 4308 | |
3e4093b6 RS |
4309 | default: |
4310 | return 0; | |
4311 | } | |
4312 | } | |
400fbf9f | 4313 | \f |
f37acdf9 JM |
4314 | /* Give a warning for storing in something that is read-only in GCC |
4315 | terms but not const in ISO C terms. */ | |
4316 | ||
4317 | static void | |
4318 | readonly_warning (tree arg, enum lvalue_use use) | |
4319 | { | |
4320 | switch (use) | |
4321 | { | |
4322 | case lv_assign: | |
4323 | warning (0, "assignment of read-only location %qE", arg); | |
4324 | break; | |
4325 | case lv_increment: | |
4326 | warning (0, "increment of read-only location %qE", arg); | |
4327 | break; | |
4328 | case lv_decrement: | |
4329 | warning (0, "decrement of read-only location %qE", arg); | |
4330 | break; | |
4331 | default: | |
4332 | gcc_unreachable (); | |
4333 | } | |
4334 | return; | |
4335 | } | |
4336 | ||
37dc0d8d JM |
4337 | |
4338 | /* Return nonzero if REF is an lvalue valid for this language; | |
4339 | otherwise, print an error message and return zero. USE says | |
7bd11157 TT |
4340 | how the lvalue is being used and so selects the error message. |
4341 | LOCATION is the location at which any error should be reported. */ | |
37dc0d8d JM |
4342 | |
4343 | static int | |
7bd11157 | 4344 | lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) |
37dc0d8d JM |
4345 | { |
4346 | int win = lvalue_p (ref); | |
4347 | ||
4348 | if (!win) | |
7bd11157 | 4349 | lvalue_error (loc, use); |
37dc0d8d JM |
4350 | |
4351 | return win; | |
4352 | } | |
3e4093b6 RS |
4353 | \f |
4354 | /* Mark EXP saying that we need to be able to take the | |
4355 | address of it; it should not be allocated in a register. | |
4356 | Returns true if successful. */ | |
54c93c30 | 4357 | |
3e4093b6 RS |
4358 | bool |
4359 | c_mark_addressable (tree exp) | |
400fbf9f | 4360 | { |
3e4093b6 | 4361 | tree x = exp; |
95602da1 | 4362 | |
3e4093b6 RS |
4363 | while (1) |
4364 | switch (TREE_CODE (x)) | |
4365 | { | |
4366 | case COMPONENT_REF: | |
4367 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
4368 | { | |
0039fa55 AN |
4369 | error |
4370 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
3e4093b6 RS |
4371 | return false; |
4372 | } | |
95602da1 | 4373 | |
3e4093b6 | 4374 | /* ... fall through ... */ |
95602da1 | 4375 | |
3e4093b6 RS |
4376 | case ADDR_EXPR: |
4377 | case ARRAY_REF: | |
4378 | case REALPART_EXPR: | |
4379 | case IMAGPART_EXPR: | |
4380 | x = TREE_OPERAND (x, 0); | |
4381 | break; | |
95602da1 | 4382 | |
3e4093b6 RS |
4383 | case COMPOUND_LITERAL_EXPR: |
4384 | case CONSTRUCTOR: | |
4385 | TREE_ADDRESSABLE (x) = 1; | |
4386 | return true; | |
95602da1 | 4387 | |
3e4093b6 RS |
4388 | case VAR_DECL: |
4389 | case CONST_DECL: | |
4390 | case PARM_DECL: | |
4391 | case RESULT_DECL: | |
5baeaac0 | 4392 | if (C_DECL_REGISTER (x) |
3e4093b6 RS |
4393 | && DECL_NONLOCAL (x)) |
4394 | { | |
62f9079a | 4395 | if (TREE_PUBLIC (x) || is_global_var (x)) |
3e4093b6 | 4396 | { |
0039fa55 AN |
4397 | error |
4398 | ("global register variable %qD used in nested function", x); | |
3e4093b6 RS |
4399 | return false; |
4400 | } | |
509c9d60 | 4401 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); |
3e4093b6 | 4402 | } |
5baeaac0 | 4403 | else if (C_DECL_REGISTER (x)) |
3e4093b6 | 4404 | { |
62f9079a | 4405 | if (TREE_PUBLIC (x) || is_global_var (x)) |
0039fa55 AN |
4406 | error ("address of global register variable %qD requested", x); |
4407 | else | |
4408 | error ("address of register variable %qD requested", x); | |
4409 | return false; | |
3e4093b6 | 4410 | } |
400fbf9f | 4411 | |
3e4093b6 RS |
4412 | /* drops in */ |
4413 | case FUNCTION_DECL: | |
4414 | TREE_ADDRESSABLE (x) = 1; | |
4415 | /* drops out */ | |
4416 | default: | |
4417 | return true; | |
4418 | } | |
4419 | } | |
4420 | \f | |
2d2e923f MLI |
4421 | /* Convert EXPR to TYPE, warning about conversion problems with |
4422 | constants. SEMANTIC_TYPE is the type this conversion would use | |
4423 | without excess precision. If SEMANTIC_TYPE is NULL, this function | |
4424 | is equivalent to convert_and_check. This function is a wrapper that | |
4425 | handles conversions that may be different than | |
4426 | the usual ones because of excess precision. */ | |
4427 | ||
4428 | static tree | |
68fca595 MP |
4429 | ep_convert_and_check (location_t loc, tree type, tree expr, |
4430 | tree semantic_type) | |
2d2e923f MLI |
4431 | { |
4432 | if (TREE_TYPE (expr) == type) | |
4433 | return expr; | |
4434 | ||
4435 | if (!semantic_type) | |
68fca595 | 4436 | return convert_and_check (loc, type, expr); |
2d2e923f MLI |
4437 | |
4438 | if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
4439 | && TREE_TYPE (expr) != semantic_type) | |
4440 | { | |
4441 | /* For integers, we need to check the real conversion, not | |
4442 | the conversion to the excess precision type. */ | |
68fca595 | 4443 | expr = convert_and_check (loc, semantic_type, expr); |
2d2e923f MLI |
4444 | } |
4445 | /* Result type is the excess precision type, which should be | |
4446 | large enough, so do not check. */ | |
4447 | return convert (type, expr); | |
4448 | } | |
4449 | ||
928c19bb JM |
4450 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If |
4451 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
4452 | if folded to an integer constant then the unselected half may | |
4453 | contain arbitrary operations not normally permitted in constant | |
c2255bc4 | 4454 | expressions. Set the location of the expression to LOC. */ |
400fbf9f JW |
4455 | |
4456 | tree | |
744aa42f | 4457 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, |
d130ae11 ILT |
4458 | tree op1, tree op1_original_type, tree op2, |
4459 | tree op2_original_type) | |
400fbf9f | 4460 | { |
3e4093b6 RS |
4461 | tree type1; |
4462 | tree type2; | |
4463 | enum tree_code code1; | |
4464 | enum tree_code code2; | |
4465 | tree result_type = NULL; | |
2d2e923f | 4466 | tree semantic_result_type = NULL; |
3e4093b6 | 4467 | tree orig_op1 = op1, orig_op2 = op2; |
928c19bb | 4468 | bool int_const, op1_int_operands, op2_int_operands, int_operands; |
4d84fe7c | 4469 | bool ifexp_int_operands; |
928c19bb | 4470 | tree ret; |
400fbf9f | 4471 | |
4d84fe7c JM |
4472 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); |
4473 | if (op1_int_operands) | |
4474 | op1 = remove_c_maybe_const_expr (op1); | |
4475 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
4476 | if (op2_int_operands) | |
4477 | op2 = remove_c_maybe_const_expr (op2); | |
4478 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
4479 | if (ifexp_int_operands) | |
4480 | ifexp = remove_c_maybe_const_expr (ifexp); | |
4481 | ||
3e4093b6 RS |
4482 | /* Promote both alternatives. */ |
4483 | ||
4484 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
4485 | op1 = default_conversion (op1); | |
4486 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
4487 | op2 = default_conversion (op2); | |
4488 | ||
4489 | if (TREE_CODE (ifexp) == ERROR_MARK | |
4490 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
4491 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
400fbf9f | 4492 | return error_mark_node; |
400fbf9f | 4493 | |
3e4093b6 RS |
4494 | type1 = TREE_TYPE (op1); |
4495 | code1 = TREE_CODE (type1); | |
4496 | type2 = TREE_TYPE (op2); | |
4497 | code2 = TREE_CODE (type2); | |
4498 | ||
1807ffc1 MS |
4499 | if (code1 == POINTER_TYPE && reject_gcc_builtin (op1)) |
4500 | return error_mark_node; | |
4501 | ||
4502 | if (code2 == POINTER_TYPE && reject_gcc_builtin (op2)) | |
4503 | return error_mark_node; | |
4504 | ||
b1adf557 JM |
4505 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
4506 | In C99 they will be pointers by now. */ | |
4507 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
4508 | { | |
744aa42f | 4509 | error_at (colon_loc, "non-lvalue array in conditional expression"); |
b1adf557 JM |
4510 | return error_mark_node; |
4511 | } | |
4512 | ||
8ce94e44 JM |
4513 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR |
4514 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4515 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4516 | || code1 == COMPLEX_TYPE) | |
4517 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4518 | || code2 == COMPLEX_TYPE)) | |
4519 | { | |
2d2e923f | 4520 | semantic_result_type = c_common_type (type1, type2); |
8ce94e44 JM |
4521 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) |
4522 | { | |
4523 | op1 = TREE_OPERAND (op1, 0); | |
4524 | type1 = TREE_TYPE (op1); | |
4525 | gcc_assert (TREE_CODE (type1) == code1); | |
4526 | } | |
4527 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4528 | { | |
4529 | op2 = TREE_OPERAND (op2, 0); | |
4530 | type2 = TREE_TYPE (op2); | |
4531 | gcc_assert (TREE_CODE (type2) == code2); | |
4532 | } | |
4533 | } | |
4534 | ||
d130ae11 ILT |
4535 | if (warn_cxx_compat) |
4536 | { | |
4537 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
4538 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
4539 | ||
4540 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
4541 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
4542 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
4543 | warning_at (colon_loc, OPT_Wc___compat, | |
4544 | ("different enum types in conditional is " | |
4545 | "invalid in C++: %qT vs %qT"), | |
4546 | t1, t2); | |
4547 | } | |
4548 | ||
3e4093b6 RS |
4549 | /* Quickly detect the usual case where op1 and op2 have the same type |
4550 | after promotion. */ | |
4551 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
400fbf9f | 4552 | { |
3e4093b6 RS |
4553 | if (type1 == type2) |
4554 | result_type = type1; | |
4555 | else | |
4556 | result_type = TYPE_MAIN_VARIANT (type1); | |
4557 | } | |
4558 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
c22cacf3 MS |
4559 | || code1 == COMPLEX_TYPE) |
4560 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4561 | || code2 == COMPLEX_TYPE)) | |
3e4093b6 | 4562 | { |
ccf7f880 | 4563 | result_type = c_common_type (type1, type2); |
0a0b3574 MM |
4564 | do_warn_double_promotion (result_type, type1, type2, |
4565 | "implicit conversion from %qT to %qT to " | |
4566 | "match other result of conditional", | |
4567 | colon_loc); | |
400fbf9f | 4568 | |
3e4093b6 RS |
4569 | /* If -Wsign-compare, warn here if type1 and type2 have |
4570 | different signedness. We'll promote the signed to unsigned | |
4571 | and later code won't know it used to be different. | |
4572 | Do this check on the original types, so that explicit casts | |
4573 | will be considered, but default promotions won't. */ | |
7d882b83 | 4574 | if (c_inhibit_evaluation_warnings == 0) |
ab87f8c8 | 4575 | { |
8df83eae RK |
4576 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
4577 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
400fbf9f | 4578 | |
3e4093b6 RS |
4579 | if (unsigned_op1 ^ unsigned_op2) |
4580 | { | |
6ac01510 ILT |
4581 | bool ovf; |
4582 | ||
3e4093b6 RS |
4583 | /* Do not warn if the result type is signed, since the |
4584 | signed type will only be chosen if it can represent | |
4585 | all the values of the unsigned type. */ | |
3f75a254 | 4586 | if (!TYPE_UNSIGNED (result_type)) |
3e4093b6 | 4587 | /* OK */; |
3e4093b6 | 4588 | else |
928c19bb JM |
4589 | { |
4590 | bool op1_maybe_const = true; | |
4591 | bool op2_maybe_const = true; | |
4592 | ||
4593 | /* Do not warn if the signed quantity is an | |
4594 | unsuffixed integer literal (or some static | |
4595 | constant expression involving such literals) and | |
4596 | it is non-negative. This warning requires the | |
4597 | operands to be folded for best results, so do | |
4598 | that folding in this case even without | |
4599 | warn_sign_compare to avoid warning options | |
4600 | possibly affecting code generation. */ | |
f5178456 RS |
4601 | c_inhibit_evaluation_warnings |
4602 | += (ifexp == truthvalue_false_node); | |
928c19bb JM |
4603 | op1 = c_fully_fold (op1, require_constant_value, |
4604 | &op1_maybe_const); | |
f5178456 RS |
4605 | c_inhibit_evaluation_warnings |
4606 | -= (ifexp == truthvalue_false_node); | |
4607 | ||
4608 | c_inhibit_evaluation_warnings | |
4609 | += (ifexp == truthvalue_true_node); | |
928c19bb JM |
4610 | op2 = c_fully_fold (op2, require_constant_value, |
4611 | &op2_maybe_const); | |
f5178456 RS |
4612 | c_inhibit_evaluation_warnings |
4613 | -= (ifexp == truthvalue_true_node); | |
928c19bb JM |
4614 | |
4615 | if (warn_sign_compare) | |
4616 | { | |
4617 | if ((unsigned_op2 | |
4618 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4619 | || (unsigned_op1 | |
4620 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4621 | /* OK */; | |
4622 | else | |
744aa42f ILT |
4623 | warning_at (colon_loc, OPT_Wsign_compare, |
4624 | ("signed and unsigned type in " | |
4625 | "conditional expression")); | |
928c19bb JM |
4626 | } |
4627 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
e5a94231 | 4628 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 4629 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) |
e5a94231 | 4630 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); |
928c19bb | 4631 | } |
3e4093b6 RS |
4632 | } |
4633 | } | |
4634 | } | |
4635 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4636 | { | |
fcf73884 | 4637 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) |
c1771a20 | 4638 | pedwarn (colon_loc, OPT_Wpedantic, |
fcf73884 | 4639 | "ISO C forbids conditional expr with only one void side"); |
3e4093b6 RS |
4640 | result_type = void_type_node; |
4641 | } | |
4642 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4643 | { | |
36c5e70a BE |
4644 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); |
4645 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4646 | addr_space_t as_common; | |
4647 | ||
744aa42f | 4648 | if (comp_target_types (colon_loc, type1, type2)) |
10bc1b1b | 4649 | result_type = common_pointer_type (type1, type2); |
6aa3c60d | 4650 | else if (null_pointer_constant_p (orig_op1)) |
36c5e70a | 4651 | result_type = type2; |
6aa3c60d | 4652 | else if (null_pointer_constant_p (orig_op2)) |
36c5e70a BE |
4653 | result_type = type1; |
4654 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4655 | { | |
4656 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4657 | "used in conditional expression"); | |
4658 | return error_mark_node; | |
4659 | } | |
267bac10 JM |
4660 | else if (VOID_TYPE_P (TREE_TYPE (type1)) |
4661 | && !TYPE_ATOMIC (TREE_TYPE (type1))) | |
34a80643 | 4662 | { |
768952be MU |
4663 | if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE) |
4664 | && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2))) | |
4665 | & ~TYPE_QUALS (TREE_TYPE (type1)))) | |
4666 | warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers, | |
4667 | "pointer to array loses qualifier " | |
4668 | "in conditional expression"); | |
4669 | ||
fcf73884 | 4670 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
c1771a20 | 4671 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4672 | "ISO C forbids conditional expr between " |
bda67431 | 4673 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4674 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), |
4675 | TREE_TYPE (type2))); | |
34a80643 | 4676 | } |
267bac10 JM |
4677 | else if (VOID_TYPE_P (TREE_TYPE (type2)) |
4678 | && !TYPE_ATOMIC (TREE_TYPE (type2))) | |
1c2a9b35 | 4679 | { |
768952be MU |
4680 | if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE) |
4681 | && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1))) | |
4682 | & ~TYPE_QUALS (TREE_TYPE (type2)))) | |
4683 | warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers, | |
4684 | "pointer to array loses qualifier " | |
4685 | "in conditional expression"); | |
4686 | ||
fcf73884 | 4687 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
c1771a20 | 4688 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4689 | "ISO C forbids conditional expr between " |
bda67431 | 4690 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4691 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), |
4692 | TREE_TYPE (type1))); | |
1c2a9b35 | 4693 | } |
b581b85b NP |
4694 | /* Objective-C pointer comparisons are a bit more lenient. */ |
4695 | else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) | |
4696 | result_type = objc_common_type (type1, type2); | |
34a80643 | 4697 | else |
ab87f8c8 | 4698 | { |
36c5e70a BE |
4699 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
4700 | ||
b581b85b NP |
4701 | pedwarn (colon_loc, 0, |
4702 | "pointer type mismatch in conditional expression"); | |
36c5e70a BE |
4703 | result_type = build_pointer_type |
4704 | (build_qualified_type (void_type_node, qual)); | |
ab87f8c8 | 4705 | } |
3e4093b6 RS |
4706 | } |
4707 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4708 | { | |
6aa3c60d | 4709 | if (!null_pointer_constant_p (orig_op2)) |
744aa42f | 4710 | pedwarn (colon_loc, 0, |
509c9d60 | 4711 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4712 | else |
ab87f8c8 | 4713 | { |
3e4093b6 | 4714 | op2 = null_pointer_node; |
ab87f8c8 | 4715 | } |
3e4093b6 RS |
4716 | result_type = type1; |
4717 | } | |
4718 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4719 | { | |
6aa3c60d | 4720 | if (!null_pointer_constant_p (orig_op1)) |
744aa42f | 4721 | pedwarn (colon_loc, 0, |
509c9d60 | 4722 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4723 | else |
ab87f8c8 | 4724 | { |
3e4093b6 | 4725 | op1 = null_pointer_node; |
ab87f8c8 | 4726 | } |
3e4093b6 RS |
4727 | result_type = type2; |
4728 | } | |
1c2a9b35 | 4729 | |
3e4093b6 RS |
4730 | if (!result_type) |
4731 | { | |
4732 | if (flag_cond_mismatch) | |
4733 | result_type = void_type_node; | |
4734 | else | |
400fbf9f | 4735 | { |
c2255bc4 | 4736 | error_at (colon_loc, "type mismatch in conditional expression"); |
ab87f8c8 | 4737 | return error_mark_node; |
400fbf9f | 4738 | } |
3e4093b6 | 4739 | } |
400fbf9f | 4740 | |
3e4093b6 RS |
4741 | /* Merge const and volatile flags of the incoming types. */ |
4742 | result_type | |
4743 | = build_type_variant (result_type, | |
afbd0665 AS |
4744 | TYPE_READONLY (type1) || TYPE_READONLY (type2), |
4745 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
b6a10c9f | 4746 | |
68fca595 MP |
4747 | op1 = ep_convert_and_check (colon_loc, result_type, op1, |
4748 | semantic_result_type); | |
4749 | op2 = ep_convert_and_check (colon_loc, result_type, op2, | |
4750 | semantic_result_type); | |
b6a10c9f | 4751 | |
928c19bb JM |
4752 | if (ifexp_bcp && ifexp == truthvalue_true_node) |
4753 | { | |
4754 | op2_int_operands = true; | |
4755 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4756 | } | |
4757 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4758 | { | |
4759 | op1_int_operands = true; | |
4760 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4761 | } | |
4d84fe7c | 4762 | int_const = int_operands = (ifexp_int_operands |
928c19bb JM |
4763 | && op1_int_operands |
4764 | && op2_int_operands); | |
4765 | if (int_operands) | |
4766 | { | |
4767 | int_const = ((ifexp == truthvalue_true_node | |
4768 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4769 | && !TREE_OVERFLOW (orig_op1)) | |
4770 | || (ifexp == truthvalue_false_node | |
4771 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4772 | && !TREE_OVERFLOW (orig_op2))); | |
4773 | } | |
4774 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) | |
db3927fb | 4775 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); |
928c19bb JM |
4776 | else |
4777 | { | |
01c7ccbb JM |
4778 | if (int_operands) |
4779 | { | |
f34f1c87 MP |
4780 | /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be |
4781 | nested inside of the expression. */ | |
4782 | op1 = c_fully_fold (op1, false, NULL); | |
4783 | op2 = c_fully_fold (op2, false, NULL); | |
01c7ccbb | 4784 | } |
928c19bb JM |
4785 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); |
4786 | if (int_operands) | |
4787 | ret = note_integer_operands (ret); | |
4788 | } | |
2d2e923f MLI |
4789 | if (semantic_result_type) |
4790 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
928c19bb | 4791 | |
c2255bc4 | 4792 | protected_set_expr_location (ret, colon_loc); |
928c19bb | 4793 | return ret; |
3e4093b6 RS |
4794 | } |
4795 | \f | |
487a92fe | 4796 | /* Return a compound expression that performs two expressions and |
c2255bc4 AH |
4797 | returns the value of the second of them. |
4798 | ||
4799 | LOC is the location of the COMPOUND_EXPR. */ | |
400fbf9f | 4800 | |
3e4093b6 | 4801 | tree |
c2255bc4 | 4802 | build_compound_expr (location_t loc, tree expr1, tree expr2) |
3e4093b6 | 4803 | { |
4d84fe7c | 4804 | bool expr1_int_operands, expr2_int_operands; |
8ce94e44 | 4805 | tree eptype = NULL_TREE; |
928c19bb JM |
4806 | tree ret; |
4807 | ||
b72271b9 | 4808 | if (flag_cilkplus |
939b37da BI |
4809 | && (TREE_CODE (expr1) == CILK_SPAWN_STMT |
4810 | || TREE_CODE (expr2) == CILK_SPAWN_STMT)) | |
4811 | { | |
4812 | error_at (loc, | |
4813 | "spawned function call cannot be part of a comma expression"); | |
4814 | return error_mark_node; | |
4815 | } | |
4d84fe7c JM |
4816 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); |
4817 | if (expr1_int_operands) | |
4818 | expr1 = remove_c_maybe_const_expr (expr1); | |
4819 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4820 | if (expr2_int_operands) | |
4821 | expr2 = remove_c_maybe_const_expr (expr2); | |
4822 | ||
8ce94e44 JM |
4823 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) |
4824 | expr1 = TREE_OPERAND (expr1, 0); | |
4825 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4826 | { | |
4827 | eptype = TREE_TYPE (expr2); | |
4828 | expr2 = TREE_OPERAND (expr2, 0); | |
4829 | } | |
4830 | ||
3f75a254 | 4831 | if (!TREE_SIDE_EFFECTS (expr1)) |
3e4093b6 RS |
4832 | { |
4833 | /* The left-hand operand of a comma expression is like an expression | |
c5409249 | 4834 | statement: with -Wunused, we should warn if it doesn't have |
3e4093b6 | 4835 | any side-effects, unless it was explicitly cast to (void). */ |
e14a6540 | 4836 | if (warn_unused_value) |
47aecf47 | 4837 | { |
e14a6540 | 4838 | if (VOID_TYPE_P (TREE_TYPE (expr1)) |
1043771b | 4839 | && CONVERT_EXPR_P (expr1)) |
47aecf47 | 4840 | ; /* (void) a, b */ |
e14a6540 JM |
4841 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) |
4842 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
1043771b | 4843 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) |
47aecf47 JM |
4844 | ; /* (void) a, (void) b, c */ |
4845 | else | |
b8698a0f | 4846 | warning_at (loc, OPT_Wunused_value, |
c2255bc4 | 4847 | "left-hand operand of comma expression has no effect"); |
47aecf47 | 4848 | } |
3e4093b6 | 4849 | } |
789eadcd MP |
4850 | else if (TREE_CODE (expr1) == COMPOUND_EXPR |
4851 | && warn_unused_value) | |
4852 | { | |
4853 | tree r = expr1; | |
4854 | location_t cloc = loc; | |
4855 | while (TREE_CODE (r) == COMPOUND_EXPR) | |
4856 | { | |
4857 | if (EXPR_HAS_LOCATION (r)) | |
4858 | cloc = EXPR_LOCATION (r); | |
4859 | r = TREE_OPERAND (r, 1); | |
4860 | } | |
4861 | if (!TREE_SIDE_EFFECTS (r) | |
4862 | && !VOID_TYPE_P (TREE_TYPE (r)) | |
4863 | && !CONVERT_EXPR_P (r)) | |
4864 | warning_at (cloc, OPT_Wunused_value, | |
4865 | "right-hand operand of comma expression has no effect"); | |
4866 | } | |
400fbf9f | 4867 | |
3e4093b6 RS |
4868 | /* With -Wunused, we should also warn if the left-hand operand does have |
4869 | side-effects, but computes a value which is not used. For example, in | |
4870 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4871 | so we should issue a warning. */ | |
4872 | else if (warn_unused_value) | |
c2255bc4 | 4873 | warn_if_unused_value (expr1, loc); |
400fbf9f | 4874 | |
e63d6886 AP |
4875 | if (expr2 == error_mark_node) |
4876 | return error_mark_node; | |
4877 | ||
928c19bb JM |
4878 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
4879 | ||
4880 | if (flag_isoc99 | |
4d84fe7c JM |
4881 | && expr1_int_operands |
4882 | && expr2_int_operands) | |
928c19bb JM |
4883 | ret = note_integer_operands (ret); |
4884 | ||
8ce94e44 JM |
4885 | if (eptype) |
4886 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4887 | ||
c2255bc4 | 4888 | protected_set_expr_location (ret, loc); |
928c19bb | 4889 | return ret; |
3e4093b6 | 4890 | } |
400fbf9f | 4891 | |
67165eb3 ILT |
4892 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to |
4893 | which we are casting. OTYPE is the type of the expression being | |
2ee3cb35 MLI |
4894 | cast. Both TYPE and OTYPE are pointer types. LOC is the location |
4895 | of the cast. -Wcast-qual appeared on the command line. Named | |
4896 | address space qualifiers are not handled here, because they result | |
4897 | in different warnings. */ | |
67165eb3 ILT |
4898 | |
4899 | static void | |
2ee3cb35 | 4900 | handle_warn_cast_qual (location_t loc, tree type, tree otype) |
67165eb3 ILT |
4901 | { |
4902 | tree in_type = type; | |
4903 | tree in_otype = otype; | |
4904 | int added = 0; | |
4905 | int discarded = 0; | |
4906 | bool is_const; | |
4907 | ||
4908 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4909 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4910 | nodes is uninteresting and we stop as soon as we hit a | |
4911 | non-POINTER_TYPE node on either type. */ | |
4912 | do | |
4913 | { | |
4914 | in_otype = TREE_TYPE (in_otype); | |
4915 | in_type = TREE_TYPE (in_type); | |
4916 | ||
4917 | /* GNU C allows cv-qualified function types. 'const' means the | |
4918 | function is very pure, 'volatile' means it can't return. We | |
4919 | need to warn when such qualifiers are added, not when they're | |
4920 | taken away. */ | |
4921 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4922 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
36c5e70a BE |
4923 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) |
4924 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
67165eb3 | 4925 | else |
36c5e70a BE |
4926 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) |
4927 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
67165eb3 ILT |
4928 | } |
4929 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4930 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4931 | ||
4932 | if (added) | |
2ee3cb35 MLI |
4933 | warning_at (loc, OPT_Wcast_qual, |
4934 | "cast adds %q#v qualifier to function type", added); | |
67165eb3 ILT |
4935 | |
4936 | if (discarded) | |
4937 | /* There are qualifiers present in IN_OTYPE that are not present | |
4938 | in IN_TYPE. */ | |
2ee3cb35 | 4939 | warning_at (loc, OPT_Wcast_qual, |
7485aeea | 4940 | "cast discards %qv qualifier from pointer target type", |
2ee3cb35 | 4941 | discarded); |
67165eb3 ILT |
4942 | |
4943 | if (added || discarded) | |
4944 | return; | |
4945 | ||
4946 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4947 | const value to be changed with no additional warning. We only | |
4948 | issue this warning if T is the same on both sides, and we only | |
4949 | issue the warning if there are the same number of pointers on | |
4950 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4951 | cast is unsafe when a qualifier is added at one level and const | |
4952 | is not present at all outer levels. | |
4953 | ||
4954 | To issue this warning, we check at each level whether the cast | |
4955 | adds new qualifiers not already seen. We don't need to special | |
4956 | case function types, as they won't have the same | |
4957 | TYPE_MAIN_VARIANT. */ | |
4958 | ||
4959 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4960 | return; | |
4961 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4962 | return; | |
4963 | ||
4964 | in_type = type; | |
4965 | in_otype = otype; | |
4966 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4967 | do | |
4968 | { | |
4969 | in_type = TREE_TYPE (in_type); | |
4970 | in_otype = TREE_TYPE (in_otype); | |
4971 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4972 | && !is_const) | |
4973 | { | |
2ee3cb35 MLI |
4974 | warning_at (loc, OPT_Wcast_qual, |
4975 | "to be safe all intermediate pointers in cast from " | |
4976 | "%qT to %qT must be %<const%> qualified", | |
4977 | otype, type); | |
67165eb3 ILT |
4978 | break; |
4979 | } | |
4980 | if (is_const) | |
4981 | is_const = TYPE_READONLY (in_type); | |
4982 | } | |
4983 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4984 | } | |
4985 | ||
b8698a0f | 4986 | /* Build an expression representing a cast to type TYPE of expression EXPR. |
c2255bc4 | 4987 | LOC is the location of the cast-- typically the open paren of the cast. */ |
400fbf9f | 4988 | |
3e4093b6 | 4989 | tree |
c2255bc4 | 4990 | build_c_cast (location_t loc, tree type, tree expr) |
3e4093b6 | 4991 | { |
8ce94e44 JM |
4992 | tree value; |
4993 | ||
4994 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
4995 | expr = TREE_OPERAND (expr, 0); | |
4996 | ||
4997 | value = expr; | |
400fbf9f | 4998 | |
3e4093b6 RS |
4999 | if (type == error_mark_node || expr == error_mark_node) |
5000 | return error_mark_node; | |
400fbf9f | 5001 | |
3e4093b6 RS |
5002 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
5003 | only in <protocol> qualifications. But when constructing cast expressions, | |
5004 | the protocols do matter and must be kept around. */ | |
700686fa ZL |
5005 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) |
5006 | return build1 (NOP_EXPR, type, expr); | |
5007 | ||
5008 | type = TYPE_MAIN_VARIANT (type); | |
400fbf9f | 5009 | |
3e4093b6 RS |
5010 | if (TREE_CODE (type) == ARRAY_TYPE) |
5011 | { | |
c2255bc4 | 5012 | error_at (loc, "cast specifies array type"); |
3e4093b6 RS |
5013 | return error_mark_node; |
5014 | } | |
400fbf9f | 5015 | |
3e4093b6 RS |
5016 | if (TREE_CODE (type) == FUNCTION_TYPE) |
5017 | { | |
c2255bc4 | 5018 | error_at (loc, "cast specifies function type"); |
3e4093b6 RS |
5019 | return error_mark_node; |
5020 | } | |
400fbf9f | 5021 | |
808d6eaa JM |
5022 | if (!VOID_TYPE_P (type)) |
5023 | { | |
5024 | value = require_complete_type (value); | |
5025 | if (value == error_mark_node) | |
5026 | return error_mark_node; | |
5027 | } | |
5028 | ||
3e4093b6 RS |
5029 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
5030 | { | |
fcf73884 MLI |
5031 | if (TREE_CODE (type) == RECORD_TYPE |
5032 | || TREE_CODE (type) == UNION_TYPE) | |
c1771a20 | 5033 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 5034 | "ISO C forbids casting nonscalar to the same type"); |
c77935ee PP |
5035 | |
5036 | /* Convert to remove any qualifiers from VALUE's type. */ | |
5037 | value = convert (type, value); | |
3e4093b6 RS |
5038 | } |
5039 | else if (TREE_CODE (type) == UNION_TYPE) | |
5040 | { | |
5041 | tree field; | |
400fbf9f | 5042 | |
910ad8de | 5043 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
39ffbac9 VR |
5044 | if (TREE_TYPE (field) != error_mark_node |
5045 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
5046 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3e4093b6 RS |
5047 | break; |
5048 | ||
5049 | if (field) | |
400fbf9f | 5050 | { |
3e4093b6 | 5051 | tree t; |
e616f54d | 5052 | bool maybe_const = true; |
3e4093b6 | 5053 | |
c1771a20 | 5054 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); |
e616f54d JM |
5055 | t = c_fully_fold (value, false, &maybe_const); |
5056 | t = build_constructor_single (type, field, t); | |
5057 | if (!maybe_const) | |
5058 | t = c_wrap_maybe_const (t, true); | |
5059 | t = digest_init (loc, type, t, | |
bbbbb16a | 5060 | NULL_TREE, false, true, 0); |
3e4093b6 RS |
5061 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
5062 | return t; | |
400fbf9f | 5063 | } |
c2255bc4 | 5064 | error_at (loc, "cast to union type from type not present in union"); |
3e4093b6 RS |
5065 | return error_mark_node; |
5066 | } | |
5067 | else | |
5068 | { | |
5069 | tree otype, ovalue; | |
400fbf9f | 5070 | |
3e4093b6 | 5071 | if (type == void_type_node) |
c2255bc4 AH |
5072 | { |
5073 | tree t = build1 (CONVERT_EXPR, type, value); | |
5074 | SET_EXPR_LOCATION (t, loc); | |
5075 | return t; | |
5076 | } | |
400fbf9f | 5077 | |
3e4093b6 | 5078 | otype = TREE_TYPE (value); |
400fbf9f | 5079 | |
3e4093b6 | 5080 | /* Optionally warn about potentially worrisome casts. */ |
3e4093b6 RS |
5081 | if (warn_cast_qual |
5082 | && TREE_CODE (type) == POINTER_TYPE | |
5083 | && TREE_CODE (otype) == POINTER_TYPE) | |
2ee3cb35 | 5084 | handle_warn_cast_qual (loc, type, otype); |
400fbf9f | 5085 | |
36c5e70a BE |
5086 | /* Warn about conversions between pointers to disjoint |
5087 | address spaces. */ | |
5088 | if (TREE_CODE (type) == POINTER_TYPE | |
5089 | && TREE_CODE (otype) == POINTER_TYPE | |
5090 | && !null_pointer_constant_p (value)) | |
5091 | { | |
5092 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
5093 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
5094 | addr_space_t as_common; | |
5095 | ||
5096 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
5097 | { | |
5098 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
5099 | warning_at (loc, 0, "cast to %s address space pointer " | |
5100 | "from disjoint generic address space pointer", | |
5101 | c_addr_space_name (as_to)); | |
5102 | ||
5103 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
5104 | warning_at (loc, 0, "cast to generic address space pointer " | |
5105 | "from disjoint %s address space pointer", | |
5106 | c_addr_space_name (as_from)); | |
5107 | ||
5108 | else | |
5109 | warning_at (loc, 0, "cast to %s address space pointer " | |
5110 | "from disjoint %s address space pointer", | |
5111 | c_addr_space_name (as_to), | |
5112 | c_addr_space_name (as_from)); | |
5113 | } | |
5114 | } | |
5115 | ||
3e4093b6 | 5116 | /* Warn about possible alignment problems. */ |
3176a0c2 | 5117 | if (STRICT_ALIGNMENT |
3e4093b6 RS |
5118 | && TREE_CODE (type) == POINTER_TYPE |
5119 | && TREE_CODE (otype) == POINTER_TYPE | |
5120 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
5121 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
5122 | /* Don't warn about opaque types, where the actual alignment | |
5123 | restriction is unknown. */ | |
5124 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
5125 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
5126 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
5127 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
c2255bc4 AH |
5128 | warning_at (loc, OPT_Wcast_align, |
5129 | "cast increases required alignment of target type"); | |
e9a25f70 | 5130 | |
3176a0c2 | 5131 | if (TREE_CODE (type) == INTEGER_TYPE |
3e4093b6 | 5132 | && TREE_CODE (otype) == POINTER_TYPE |
8d1c7aef | 5133 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) |
c22cacf3 MS |
5134 | /* Unlike conversion of integers to pointers, where the |
5135 | warning is disabled for converting constants because | |
5136 | of cases such as SIG_*, warn about converting constant | |
5137 | pointers to integers. In some cases it may cause unwanted | |
8d1c7aef | 5138 | sign extension, and a warning is appropriate. */ |
c2255bc4 AH |
5139 | warning_at (loc, OPT_Wpointer_to_int_cast, |
5140 | "cast from pointer to integer of different size"); | |
400fbf9f | 5141 | |
3176a0c2 | 5142 | if (TREE_CODE (value) == CALL_EXPR |
3e4093b6 | 5143 | && TREE_CODE (type) != TREE_CODE (otype)) |
c2255bc4 AH |
5144 | warning_at (loc, OPT_Wbad_function_cast, |
5145 | "cast from function call of type %qT " | |
5146 | "to non-matching type %qT", otype, type); | |
400fbf9f | 5147 | |
3176a0c2 | 5148 | if (TREE_CODE (type) == POINTER_TYPE |
3e4093b6 RS |
5149 | && TREE_CODE (otype) == INTEGER_TYPE |
5150 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
5151 | /* Don't warn about converting any constant. */ | |
5152 | && !TREE_CONSTANT (value)) | |
c2255bc4 AH |
5153 | warning_at (loc, |
5154 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
5155 | "of different size"); | |
400fbf9f | 5156 | |
79bedddc SR |
5157 | if (warn_strict_aliasing <= 2) |
5158 | strict_aliasing_warning (otype, type, expr); | |
400fbf9f | 5159 | |
3897f229 JM |
5160 | /* If pedantic, warn for conversions between function and object |
5161 | pointer types, except for converting a null pointer constant | |
5162 | to function pointer type. */ | |
5163 | if (pedantic | |
5164 | && TREE_CODE (type) == POINTER_TYPE | |
5165 | && TREE_CODE (otype) == POINTER_TYPE | |
5166 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
5167 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
c1771a20 | 5168 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 5169 | "conversion of function pointer to object pointer type"); |
3897f229 JM |
5170 | |
5171 | if (pedantic | |
5172 | && TREE_CODE (type) == POINTER_TYPE | |
5173 | && TREE_CODE (otype) == POINTER_TYPE | |
5174 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
5175 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
6aa3c60d | 5176 | && !null_pointer_constant_p (value)) |
c1771a20 | 5177 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 5178 | "conversion of object pointer to function pointer type"); |
3897f229 | 5179 | |
3e4093b6 | 5180 | ovalue = value; |
3e4093b6 | 5181 | value = convert (type, value); |
400fbf9f | 5182 | |
3e4093b6 | 5183 | /* Ignore any integer overflow caused by the cast. */ |
928c19bb | 5184 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) |
3e4093b6 | 5185 | { |
8bcd6380 | 5186 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) |
6414bad6 | 5187 | { |
8bcd6380 RS |
5188 | if (!TREE_OVERFLOW (value)) |
5189 | { | |
5190 | /* Avoid clobbering a shared constant. */ | |
5191 | value = copy_node (value); | |
5192 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
5193 | } | |
6414bad6 | 5194 | } |
8bcd6380 | 5195 | else if (TREE_OVERFLOW (value)) |
d8e1f97b | 5196 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ |
807e902e | 5197 | value = wide_int_to_tree (TREE_TYPE (value), value); |
3e4093b6 RS |
5198 | } |
5199 | } | |
400fbf9f | 5200 | |
53cd18ec | 5201 | /* Don't let a cast be an lvalue. */ |
9482b620 | 5202 | if (lvalue_p (value)) |
db3927fb | 5203 | value = non_lvalue_loc (loc, value); |
e9a25f70 | 5204 | |
928c19bb JM |
5205 | /* Don't allow the results of casting to floating-point or complex |
5206 | types be confused with actual constants, or casts involving | |
5207 | integer and pointer types other than direct integer-to-integer | |
5208 | and integer-to-pointer be confused with integer constant | |
5209 | expressions and null pointer constants. */ | |
5210 | if (TREE_CODE (value) == REAL_CST | |
5211 | || TREE_CODE (value) == COMPLEX_CST | |
5212 | || (TREE_CODE (value) == INTEGER_CST | |
5213 | && !((TREE_CODE (expr) == INTEGER_CST | |
5214 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
5215 | || TREE_CODE (expr) == REAL_CST | |
5216 | || TREE_CODE (expr) == COMPLEX_CST))) | |
5217 | value = build1 (NOP_EXPR, type, value); | |
5218 | ||
c2255bc4 AH |
5219 | if (CAN_HAVE_LOCATION_P (value)) |
5220 | SET_EXPR_LOCATION (value, loc); | |
3e4093b6 | 5221 | return value; |
400fbf9f JW |
5222 | } |
5223 | ||
c2255bc4 AH |
5224 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the |
5225 | location of the open paren of the cast, or the position of the cast | |
5226 | expr. */ | |
3e4093b6 | 5227 | tree |
c2255bc4 | 5228 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) |
400fbf9f | 5229 | { |
f8893e47 | 5230 | tree type; |
928c19bb JM |
5231 | tree type_expr = NULL_TREE; |
5232 | bool type_expr_const = true; | |
5233 | tree ret; | |
3e4093b6 | 5234 | int saved_wsp = warn_strict_prototypes; |
c5c76735 | 5235 | |
3e4093b6 RS |
5236 | /* This avoids warnings about unprototyped casts on |
5237 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
5238 | if (TREE_CODE (expr) == INTEGER_CST) | |
5239 | warn_strict_prototypes = 0; | |
928c19bb | 5240 | type = groktypename (type_name, &type_expr, &type_expr_const); |
3e4093b6 | 5241 | warn_strict_prototypes = saved_wsp; |
c5c76735 | 5242 | |
1807ffc1 MS |
5243 | if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type) |
5244 | && reject_gcc_builtin (expr)) | |
5245 | return error_mark_node; | |
5246 | ||
c2255bc4 | 5247 | ret = build_c_cast (loc, type, expr); |
928c19bb JM |
5248 | if (type_expr) |
5249 | { | |
9f33203d JM |
5250 | bool inner_expr_const = true; |
5251 | ret = c_fully_fold (ret, require_constant_value, &inner_expr_const); | |
928c19bb | 5252 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); |
9f33203d JM |
5253 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const |
5254 | && inner_expr_const); | |
c2255bc4 | 5255 | SET_EXPR_LOCATION (ret, loc); |
928c19bb | 5256 | } |
24b97832 ILT |
5257 | |
5258 | if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) | |
5259 | SET_EXPR_LOCATION (ret, loc); | |
5260 | ||
9e5b2115 PB |
5261 | /* C++ does not permits types to be defined in a cast, but it |
5262 | allows references to incomplete types. */ | |
5263 | if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) | |
24b97832 ILT |
5264 | warning_at (loc, OPT_Wc___compat, |
5265 | "defining a type in a cast is invalid in C++"); | |
5266 | ||
928c19bb | 5267 | return ret; |
400fbf9f | 5268 | } |
3e4093b6 RS |
5269 | \f |
5270 | /* Build an assignment expression of lvalue LHS from value RHS. | |
32e8bb8e ILT |
5271 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which |
5272 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
3e4093b6 RS |
5273 | MODIFYCODE is the code for a binary operator that we use |
5274 | to combine the old value of LHS with RHS to get the new value. | |
c9f9eb5d | 5275 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
bbbbb16a ILT |
5276 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, |
5277 | which may differ from TREE_TYPE (RHS) for an enum value. | |
c9f9eb5d | 5278 | |
c2255bc4 AH |
5279 | LOCATION is the location of the MODIFYCODE operator. |
5280 | RHS_LOC is the location of the RHS. */ | |
2f6e4e97 | 5281 | |
3e4093b6 | 5282 | tree |
32e8bb8e | 5283 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, |
b8698a0f | 5284 | enum tree_code modifycode, |
c2255bc4 | 5285 | location_t rhs_loc, tree rhs, tree rhs_origtype) |
400fbf9f | 5286 | { |
3e4093b6 RS |
5287 | tree result; |
5288 | tree newrhs; | |
241f845a | 5289 | tree rhseval = NULL_TREE; |
8ce94e44 | 5290 | tree rhs_semantic_type = NULL_TREE; |
3e4093b6 RS |
5291 | tree lhstype = TREE_TYPE (lhs); |
5292 | tree olhstype = lhstype; | |
928c19bb | 5293 | bool npc; |
267bac10 | 5294 | bool is_atomic_op; |
e9a25f70 | 5295 | |
3e4093b6 RS |
5296 | /* Types that aren't fully specified cannot be used in assignments. */ |
5297 | lhs = require_complete_type (lhs); | |
e9a25f70 | 5298 | |
3e4093b6 RS |
5299 | /* Avoid duplicate error messages from operands that had errors. */ |
5300 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
5301 | return error_mark_node; | |
400fbf9f | 5302 | |
4c2ecab0 JM |
5303 | /* Ensure an error for assigning a non-lvalue array to an array in |
5304 | C90. */ | |
5305 | if (TREE_CODE (lhstype) == ARRAY_TYPE) | |
5306 | { | |
5307 | error_at (location, "assignment to expression with array type"); | |
5308 | return error_mark_node; | |
5309 | } | |
5310 | ||
46a88c12 | 5311 | /* For ObjC properties, defer this check. */ |
7bd11157 | 5312 | if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) |
c0bcacec VR |
5313 | return error_mark_node; |
5314 | ||
267bac10 JM |
5315 | is_atomic_op = really_atomic_lvalue (lhs); |
5316 | ||
8ce94e44 JM |
5317 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5318 | { | |
5319 | rhs_semantic_type = TREE_TYPE (rhs); | |
5320 | rhs = TREE_OPERAND (rhs, 0); | |
5321 | } | |
5322 | ||
3e4093b6 | 5323 | newrhs = rhs; |
400fbf9f | 5324 | |
928c19bb JM |
5325 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) |
5326 | { | |
5327 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
c2255bc4 | 5328 | lhs_origtype, modifycode, rhs_loc, rhs, |
32e8bb8e | 5329 | rhs_origtype); |
928c19bb JM |
5330 | if (inner == error_mark_node) |
5331 | return error_mark_node; | |
5332 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
5333 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
5334 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
5335 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
5336 | protected_set_expr_location (result, location); | |
5337 | return result; | |
5338 | } | |
5339 | ||
3e4093b6 RS |
5340 | /* If a binary op has been requested, combine the old LHS value with the RHS |
5341 | producing the value we should actually store into the LHS. */ | |
5342 | ||
5343 | if (modifycode != NOP_EXPR) | |
400fbf9f | 5344 | { |
928c19bb | 5345 | lhs = c_fully_fold (lhs, false, NULL); |
3e4093b6 | 5346 | lhs = stabilize_reference (lhs); |
bbbbb16a | 5347 | |
267bac10 JM |
5348 | /* Construct the RHS for any non-atomic compound assignemnt. */ |
5349 | if (!is_atomic_op) | |
5350 | { | |
241f845a JJ |
5351 | /* If in LHS op= RHS the RHS has side-effects, ensure they |
5352 | are preevaluated before the rest of the assignment expression's | |
5353 | side-effects, because RHS could contain e.g. function calls | |
5354 | that modify LHS. */ | |
5355 | if (TREE_SIDE_EFFECTS (rhs)) | |
5356 | { | |
5357 | newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs); | |
5358 | rhseval = newrhs; | |
5359 | } | |
267bac10 | 5360 | newrhs = build_binary_op (location, |
241f845a | 5361 | modifycode, lhs, newrhs, 1); |
267bac10 JM |
5362 | |
5363 | /* The original type of the right hand side is no longer | |
5364 | meaningful. */ | |
5365 | rhs_origtype = NULL_TREE; | |
5366 | } | |
400fbf9f | 5367 | } |
400fbf9f | 5368 | |
668ea4b1 IS |
5369 | if (c_dialect_objc ()) |
5370 | { | |
46a88c12 NP |
5371 | /* Check if we are modifying an Objective-C property reference; |
5372 | if so, we need to generate setter calls. */ | |
5373 | result = objc_maybe_build_modify_expr (lhs, newrhs); | |
668ea4b1 | 5374 | if (result) |
241f845a | 5375 | goto return_result; |
46a88c12 NP |
5376 | |
5377 | /* Else, do the check that we postponed for Objective-C. */ | |
7bd11157 | 5378 | if (!lvalue_or_else (location, lhs, lv_assign)) |
668ea4b1 IS |
5379 | return error_mark_node; |
5380 | } | |
5381 | ||
9bf24266 | 5382 | /* Give an error for storing in something that is 'const'. */ |
bbbd6700 | 5383 | |
f37acdf9 | 5384 | if (TYPE_READONLY (lhstype) |
3e4093b6 RS |
5385 | || ((TREE_CODE (lhstype) == RECORD_TYPE |
5386 | || TREE_CODE (lhstype) == UNION_TYPE) | |
5387 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
953ff289 | 5388 | { |
c02065fc | 5389 | readonly_error (location, lhs, lv_assign); |
953ff289 DN |
5390 | return error_mark_node; |
5391 | } | |
f37acdf9 JM |
5392 | else if (TREE_READONLY (lhs)) |
5393 | readonly_warning (lhs, lv_assign); | |
bbbd6700 | 5394 | |
3e4093b6 RS |
5395 | /* If storing into a structure or union member, |
5396 | it has probably been given type `int'. | |
5397 | Compute the type that would go with | |
5398 | the actual amount of storage the member occupies. */ | |
bbbd6700 | 5399 | |
3e4093b6 RS |
5400 | if (TREE_CODE (lhs) == COMPONENT_REF |
5401 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
5402 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
5403 | || TREE_CODE (lhstype) == REAL_TYPE | |
5404 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
5405 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
400fbf9f | 5406 | |
3e4093b6 RS |
5407 | /* If storing in a field that is in actuality a short or narrower than one, |
5408 | we must store in the field in its actual type. */ | |
5409 | ||
5410 | if (lhstype != TREE_TYPE (lhs)) | |
5411 | { | |
5412 | lhs = copy_node (lhs); | |
5413 | TREE_TYPE (lhs) = lhstype; | |
400fbf9f | 5414 | } |
400fbf9f | 5415 | |
32e8bb8e ILT |
5416 | /* Issue -Wc++-compat warnings about an assignment to an enum type |
5417 | when LHS does not have its original type. This happens for, | |
5418 | e.g., an enum bitfield in a struct. */ | |
5419 | if (warn_cxx_compat | |
5420 | && lhs_origtype != NULL_TREE | |
5421 | && lhs_origtype != lhstype | |
5422 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
5423 | { | |
5424 | tree checktype = (rhs_origtype != NULL_TREE | |
5425 | ? rhs_origtype | |
5426 | : TREE_TYPE (rhs)); | |
5427 | if (checktype != error_mark_node | |
267bac10 JM |
5428 | && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype) |
5429 | || (is_atomic_op && modifycode != NOP_EXPR))) | |
32e8bb8e ILT |
5430 | warning_at (location, OPT_Wc___compat, |
5431 | "enum conversion in assignment is invalid in C++"); | |
5432 | } | |
5433 | ||
267bac10 JM |
5434 | /* If the lhs is atomic, remove that qualifier. */ |
5435 | if (is_atomic_op) | |
5436 | { | |
5437 | lhstype = build_qualified_type (lhstype, | |
5438 | (TYPE_QUALS (lhstype) | |
5439 | & ~TYPE_QUAL_ATOMIC)); | |
5440 | olhstype = build_qualified_type (olhstype, | |
5441 | (TYPE_QUALS (lhstype) | |
5442 | & ~TYPE_QUAL_ATOMIC)); | |
5443 | } | |
5444 | ||
8ce94e44 JM |
5445 | /* Convert new value to destination type. Fold it first, then |
5446 | restore any excess precision information, for the sake of | |
5447 | conversion warnings. */ | |
400fbf9f | 5448 | |
267bac10 JM |
5449 | if (!(is_atomic_op && modifycode != NOP_EXPR)) |
5450 | { | |
5451 | npc = null_pointer_constant_p (newrhs); | |
5452 | newrhs = c_fully_fold (newrhs, false, NULL); | |
5453 | if (rhs_semantic_type) | |
5454 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
68fca595 MP |
5455 | newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs, |
5456 | rhs_origtype, ic_assign, npc, | |
5457 | NULL_TREE, NULL_TREE, 0); | |
267bac10 JM |
5458 | if (TREE_CODE (newrhs) == ERROR_MARK) |
5459 | return error_mark_node; | |
5460 | } | |
400fbf9f | 5461 | |
6e955430 ZL |
5462 | /* Emit ObjC write barrier, if necessary. */ |
5463 | if (c_dialect_objc () && flag_objc_gc) | |
5464 | { | |
5465 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
5466 | if (result) | |
c9f9eb5d AH |
5467 | { |
5468 | protected_set_expr_location (result, location); | |
241f845a | 5469 | goto return_result; |
c9f9eb5d | 5470 | } |
6e955430 ZL |
5471 | } |
5472 | ||
ea4b7848 | 5473 | /* Scan operands. */ |
400fbf9f | 5474 | |
267bac10 JM |
5475 | if (is_atomic_op) |
5476 | result = build_atomic_assign (location, lhs, modifycode, newrhs, false); | |
5477 | else | |
5478 | { | |
5479 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); | |
5480 | TREE_SIDE_EFFECTS (result) = 1; | |
5481 | protected_set_expr_location (result, location); | |
5482 | } | |
400fbf9f | 5483 | |
3e4093b6 RS |
5484 | /* If we got the LHS in a different type for storing in, |
5485 | convert the result back to the nominal type of LHS | |
5486 | so that the value we return always has the same type | |
5487 | as the LHS argument. */ | |
e855c5ce | 5488 | |
3e4093b6 | 5489 | if (olhstype == TREE_TYPE (result)) |
241f845a | 5490 | goto return_result; |
c9f9eb5d | 5491 | |
68fca595 MP |
5492 | result = convert_for_assignment (location, rhs_loc, olhstype, result, |
5493 | rhs_origtype, ic_assign, false, NULL_TREE, | |
5494 | NULL_TREE, 0); | |
c9f9eb5d | 5495 | protected_set_expr_location (result, location); |
241f845a JJ |
5496 | |
5497 | return_result: | |
5498 | if (rhseval) | |
5499 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result); | |
c9f9eb5d | 5500 | return result; |
3e4093b6 RS |
5501 | } |
5502 | \f | |
478a1c5b ILT |
5503 | /* Return whether STRUCT_TYPE has an anonymous field with type TYPE. |
5504 | This is used to implement -fplan9-extensions. */ | |
5505 | ||
5506 | static bool | |
5507 | find_anonymous_field_with_type (tree struct_type, tree type) | |
5508 | { | |
5509 | tree field; | |
5510 | bool found; | |
5511 | ||
5512 | gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE | |
5513 | || TREE_CODE (struct_type) == UNION_TYPE); | |
5514 | found = false; | |
5515 | for (field = TYPE_FIELDS (struct_type); | |
5516 | field != NULL_TREE; | |
5517 | field = TREE_CHAIN (field)) | |
5518 | { | |
267bac10 JM |
5519 | tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) |
5520 | ? c_build_qualified_type (TREE_TYPE (field), | |
5521 | TYPE_QUAL_ATOMIC) | |
5522 | : TYPE_MAIN_VARIANT (TREE_TYPE (field))); | |
478a1c5b | 5523 | if (DECL_NAME (field) == NULL |
267bac10 | 5524 | && comptypes (type, fieldtype)) |
478a1c5b ILT |
5525 | { |
5526 | if (found) | |
5527 | return false; | |
5528 | found = true; | |
5529 | } | |
5530 | else if (DECL_NAME (field) == NULL | |
5531 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
5532 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
5533 | && find_anonymous_field_with_type (TREE_TYPE (field), type)) | |
5534 | { | |
5535 | if (found) | |
5536 | return false; | |
5537 | found = true; | |
5538 | } | |
5539 | } | |
5540 | return found; | |
5541 | } | |
5542 | ||
5543 | /* RHS is an expression whose type is pointer to struct. If there is | |
5544 | an anonymous field in RHS with type TYPE, then return a pointer to | |
5545 | that field in RHS. This is used with -fplan9-extensions. This | |
5546 | returns NULL if no conversion could be found. */ | |
5547 | ||
5548 | static tree | |
5549 | convert_to_anonymous_field (location_t location, tree type, tree rhs) | |
5550 | { | |
5551 | tree rhs_struct_type, lhs_main_type; | |
5552 | tree field, found_field; | |
5553 | bool found_sub_field; | |
5554 | tree ret; | |
5555 | ||
5556 | gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); | |
5557 | rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); | |
5558 | gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE | |
5559 | || TREE_CODE (rhs_struct_type) == UNION_TYPE); | |
5560 | ||
5561 | gcc_assert (POINTER_TYPE_P (type)); | |
267bac10 JM |
5562 | lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type)) |
5563 | ? c_build_qualified_type (TREE_TYPE (type), | |
5564 | TYPE_QUAL_ATOMIC) | |
5565 | : TYPE_MAIN_VARIANT (TREE_TYPE (type))); | |
478a1c5b ILT |
5566 | |
5567 | found_field = NULL_TREE; | |
5568 | found_sub_field = false; | |
5569 | for (field = TYPE_FIELDS (rhs_struct_type); | |
5570 | field != NULL_TREE; | |
5571 | field = TREE_CHAIN (field)) | |
5572 | { | |
5573 | if (DECL_NAME (field) != NULL_TREE | |
5574 | || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE | |
5575 | && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)) | |
5576 | continue; | |
267bac10 JM |
5577 | tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) |
5578 | ? c_build_qualified_type (TREE_TYPE (field), | |
5579 | TYPE_QUAL_ATOMIC) | |
5580 | : TYPE_MAIN_VARIANT (TREE_TYPE (field))); | |
5581 | if (comptypes (lhs_main_type, fieldtype)) | |
478a1c5b ILT |
5582 | { |
5583 | if (found_field != NULL_TREE) | |
5584 | return NULL_TREE; | |
5585 | found_field = field; | |
5586 | } | |
5587 | else if (find_anonymous_field_with_type (TREE_TYPE (field), | |
5588 | lhs_main_type)) | |
5589 | { | |
5590 | if (found_field != NULL_TREE) | |
5591 | return NULL_TREE; | |
5592 | found_field = field; | |
5593 | found_sub_field = true; | |
5594 | } | |
5595 | } | |
5596 | ||
5597 | if (found_field == NULL_TREE) | |
5598 | return NULL_TREE; | |
5599 | ||
5600 | ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), | |
5601 | build_fold_indirect_ref (rhs), found_field, | |
5602 | NULL_TREE); | |
5603 | ret = build_fold_addr_expr_loc (location, ret); | |
5604 | ||
5605 | if (found_sub_field) | |
5606 | { | |
5607 | ret = convert_to_anonymous_field (location, type, ret); | |
5608 | gcc_assert (ret != NULL_TREE); | |
5609 | } | |
5610 | ||
5611 | return ret; | |
5612 | } | |
5613 | ||
63bc4e87 MP |
5614 | /* Issue an error message for a bad initializer component. |
5615 | GMSGID identifies the message. | |
5616 | The component name is taken from the spelling stack. */ | |
5617 | ||
5618 | static void | |
ea58ef42 | 5619 | error_init (location_t loc, const char *gmsgid) |
63bc4e87 MP |
5620 | { |
5621 | char *ofwhat; | |
5622 | ||
5623 | /* The gmsgid may be a format string with %< and %>. */ | |
ea58ef42 | 5624 | error_at (loc, gmsgid); |
63bc4e87 MP |
5625 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
5626 | if (*ofwhat) | |
d7ff7ae5 | 5627 | inform (loc, "(near initialization for %qs)", ofwhat); |
63bc4e87 MP |
5628 | } |
5629 | ||
5630 | /* Issue a pedantic warning for a bad initializer component. OPT is | |
5631 | the option OPT_* (from options.h) controlling this warning or 0 if | |
5632 | it is unconditionally given. GMSGID identifies the message. The | |
5633 | component name is taken from the spelling stack. */ | |
5634 | ||
5635 | static void | |
5636 | pedwarn_init (location_t location, int opt, const char *gmsgid) | |
5637 | { | |
5638 | char *ofwhat; | |
d7ff7ae5 | 5639 | bool warned; |
63bc4e87 MP |
5640 | |
5641 | /* The gmsgid may be a format string with %< and %>. */ | |
d7ff7ae5 | 5642 | warned = pedwarn (location, opt, gmsgid); |
63bc4e87 | 5643 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
d7ff7ae5 MP |
5644 | if (*ofwhat && warned) |
5645 | inform (location, "(near initialization for %qs)", ofwhat); | |
63bc4e87 MP |
5646 | } |
5647 | ||
5648 | /* Issue a warning for a bad initializer component. | |
5649 | ||
5650 | OPT is the OPT_W* value corresponding to the warning option that | |
5651 | controls this warning. GMSGID identifies the message. The | |
5652 | component name is taken from the spelling stack. */ | |
5653 | ||
5654 | static void | |
5655 | warning_init (location_t loc, int opt, const char *gmsgid) | |
5656 | { | |
5657 | char *ofwhat; | |
d7ff7ae5 | 5658 | bool warned; |
63bc4e87 MP |
5659 | |
5660 | /* The gmsgid may be a format string with %< and %>. */ | |
d7ff7ae5 | 5661 | warned = warning_at (loc, opt, gmsgid); |
63bc4e87 | 5662 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
d7ff7ae5 MP |
5663 | if (*ofwhat && warned) |
5664 | inform (loc, "(near initialization for %qs)", ofwhat); | |
63bc4e87 MP |
5665 | } |
5666 | \f | |
5667 | /* If TYPE is an array type and EXPR is a parenthesized string | |
5668 | constant, warn if pedantic that EXPR is being used to initialize an | |
5669 | object of type TYPE. */ | |
5670 | ||
5671 | void | |
d033409e | 5672 | maybe_warn_string_init (location_t loc, tree type, struct c_expr expr) |
63bc4e87 MP |
5673 | { |
5674 | if (pedantic | |
5675 | && TREE_CODE (type) == ARRAY_TYPE | |
5676 | && TREE_CODE (expr.value) == STRING_CST | |
5677 | && expr.original_code != STRING_CST) | |
d033409e | 5678 | pedwarn_init (loc, OPT_Wpedantic, |
63bc4e87 MP |
5679 | "array initialized from parenthesized string constant"); |
5680 | } | |
5681 | ||
bbbbb16a ILT |
5682 | /* Convert value RHS to type TYPE as preparation for an assignment to |
5683 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
5684 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
5685 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
5686 | constant before any folding. | |
3e4093b6 RS |
5687 | The real work of conversion is done by `convert'. |
5688 | The purpose of this function is to generate error messages | |
5689 | for assignments that are not allowed in C. | |
2ac2f164 JM |
5690 | ERRTYPE says whether it is argument passing, assignment, |
5691 | initialization or return. | |
2f6e4e97 | 5692 | |
aabef2de MP |
5693 | In the following example, '~' denotes where EXPR_LOC and '^' where |
5694 | LOCATION point to: | |
5695 | ||
5696 | f (var); [ic_argpass] | |
5697 | ^ ~~~ | |
5698 | x = var; [ic_assign] | |
5699 | ^ ~~~; | |
5700 | int x = var; [ic_init] | |
5701 | ^^^ | |
5702 | return x; [ic_return] | |
5703 | ^ | |
5704 | ||
2ac2f164 | 5705 | FUNCTION is a tree for the function being called. |
3e4093b6 | 5706 | PARMNUM is the number of the argument, for printing in error messages. */ |
cb3ca04e | 5707 | |
3e4093b6 | 5708 | static tree |
81e5eca8 | 5709 | convert_for_assignment (location_t location, location_t expr_loc, tree type, |
68fca595 | 5710 | tree rhs, tree origtype, enum impl_conv errtype, |
744aa42f ILT |
5711 | bool null_pointer_constant, tree fundecl, |
5712 | tree function, int parmnum) | |
3e4093b6 RS |
5713 | { |
5714 | enum tree_code codel = TREE_CODE (type); | |
8ce94e44 | 5715 | tree orig_rhs = rhs; |
3e4093b6 RS |
5716 | tree rhstype; |
5717 | enum tree_code coder; | |
2ac2f164 | 5718 | tree rname = NULL_TREE; |
58393038 | 5719 | bool objc_ok = false; |
2ac2f164 | 5720 | |
1c7485af MP |
5721 | /* Use the expansion point location to handle cases such as user's |
5722 | function returning a wrong-type macro defined in a system header. */ | |
5723 | location = expansion_point_location_if_in_system_header (location); | |
5724 | ||
6b4ef5c1 | 5725 | if (errtype == ic_argpass) |
2ac2f164 JM |
5726 | { |
5727 | tree selector; | |
5728 | /* Change pointer to function to the function itself for | |
5729 | diagnostics. */ | |
5730 | if (TREE_CODE (function) == ADDR_EXPR | |
5731 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
5732 | function = TREE_OPERAND (function, 0); | |
5733 | ||
5734 | /* Handle an ObjC selector specially for diagnostics. */ | |
5735 | selector = objc_message_selector (); | |
5736 | rname = function; | |
5737 | if (selector && parmnum > 2) | |
5738 | { | |
5739 | rname = selector; | |
5740 | parmnum -= 2; | |
5741 | } | |
5742 | } | |
5743 | ||
5744 | /* This macro is used to emit diagnostics to ensure that all format | |
5745 | strings are complete sentences, visible to gettext and checked at | |
5746 | compile time. */ | |
768952be | 5747 | #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \ |
1e053dfe MLI |
5748 | do { \ |
5749 | switch (errtype) \ | |
5750 | { \ | |
5751 | case ic_argpass: \ | |
5c1bc275 | 5752 | if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \ |
c2255bc4 | 5753 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5c1bc275 | 5754 | ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ |
1e053dfe MLI |
5755 | "expected %qT but argument is of type %qT", \ |
5756 | type, rhstype); \ | |
5757 | break; \ | |
1e053dfe MLI |
5758 | case ic_assign: \ |
5759 | pedwarn (LOCATION, OPT, AS); \ | |
5760 | break; \ | |
5761 | case ic_init: \ | |
6a8f4e12 | 5762 | pedwarn_init (LOCATION, OPT, IN); \ |
1e053dfe MLI |
5763 | break; \ |
5764 | case ic_return: \ | |
d033409e | 5765 | pedwarn (LOCATION, OPT, RE); \ |
1e053dfe MLI |
5766 | break; \ |
5767 | default: \ | |
5768 | gcc_unreachable (); \ | |
5769 | } \ | |
2ac2f164 | 5770 | } while (0) |
cb3ca04e | 5771 | |
49706e39 MLI |
5772 | /* This macro is used to emit diagnostics to ensure that all format |
5773 | strings are complete sentences, visible to gettext and checked at | |
768952be | 5774 | compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an |
49706e39 | 5775 | extra parameter to enumerate qualifiers. */ |
768952be | 5776 | #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ |
49706e39 MLI |
5777 | do { \ |
5778 | switch (errtype) \ | |
5779 | { \ | |
5780 | case ic_argpass: \ | |
5c1bc275 | 5781 | if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \ |
49706e39 | 5782 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5c1bc275 | 5783 | ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ |
49706e39 MLI |
5784 | "expected %qT but argument is of type %qT", \ |
5785 | type, rhstype); \ | |
5786 | break; \ | |
5787 | case ic_assign: \ | |
5c1bc275 | 5788 | pedwarn (LOCATION, OPT, AS, QUALS); \ |
49706e39 MLI |
5789 | break; \ |
5790 | case ic_init: \ | |
5c1bc275 | 5791 | pedwarn (LOCATION, OPT, IN, QUALS); \ |
49706e39 MLI |
5792 | break; \ |
5793 | case ic_return: \ | |
5c1bc275 | 5794 | pedwarn (LOCATION, OPT, RE, QUALS); \ |
49706e39 MLI |
5795 | break; \ |
5796 | default: \ | |
5797 | gcc_unreachable (); \ | |
5798 | } \ | |
5799 | } while (0) | |
5800 | ||
768952be MU |
5801 | /* This macro is used to emit diagnostics to ensure that all format |
5802 | strings are complete sentences, visible to gettext and checked at | |
5803 | compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses | |
5804 | warning_at instead of pedwarn. */ | |
5805 | #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ | |
5806 | do { \ | |
5807 | switch (errtype) \ | |
5808 | { \ | |
5809 | case ic_argpass: \ | |
5810 | if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \ | |
5811 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ | |
5812 | ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ | |
5813 | "expected %qT but argument is of type %qT", \ | |
5814 | type, rhstype); \ | |
5815 | break; \ | |
5816 | case ic_assign: \ | |
5817 | warning_at (LOCATION, OPT, AS, QUALS); \ | |
5818 | break; \ | |
5819 | case ic_init: \ | |
5820 | warning_at (LOCATION, OPT, IN, QUALS); \ | |
5821 | break; \ | |
5822 | case ic_return: \ | |
5823 | warning_at (LOCATION, OPT, RE, QUALS); \ | |
5824 | break; \ | |
5825 | default: \ | |
5826 | gcc_unreachable (); \ | |
5827 | } \ | |
5828 | } while (0) | |
5829 | ||
8ce94e44 JM |
5830 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5831 | rhs = TREE_OPERAND (rhs, 0); | |
5832 | ||
3e4093b6 RS |
5833 | rhstype = TREE_TYPE (rhs); |
5834 | coder = TREE_CODE (rhstype); | |
5835 | ||
5836 | if (coder == ERROR_MARK) | |
5837 | return error_mark_node; | |
5838 | ||
58393038 ZL |
5839 | if (c_dialect_objc ()) |
5840 | { | |
5841 | int parmno; | |
5842 | ||
5843 | switch (errtype) | |
5844 | { | |
5845 | case ic_return: | |
5846 | parmno = 0; | |
5847 | break; | |
5848 | ||
5849 | case ic_assign: | |
5850 | parmno = -1; | |
5851 | break; | |
5852 | ||
5853 | case ic_init: | |
5854 | parmno = -2; | |
5855 | break; | |
5856 | ||
5857 | default: | |
5858 | parmno = parmnum; | |
5859 | break; | |
5860 | } | |
5861 | ||
5862 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
5863 | } | |
5864 | ||
bbbbb16a ILT |
5865 | if (warn_cxx_compat) |
5866 | { | |
5867 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
5868 | if (checktype != error_mark_node | |
5869 | && TREE_CODE (type) == ENUMERAL_TYPE | |
5870 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
5871 | { | |
768952be MU |
5872 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat, |
5873 | G_("enum conversion when passing argument " | |
5874 | "%d of %qE is invalid in C++"), | |
5875 | G_("enum conversion in assignment is " | |
5876 | "invalid in C++"), | |
5877 | G_("enum conversion in initialization is " | |
5878 | "invalid in C++"), | |
5879 | G_("enum conversion in return is " | |
5880 | "invalid in C++")); | |
bbbbb16a ILT |
5881 | } |
5882 | } | |
5883 | ||
3e4093b6 | 5884 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) |
59c0753d | 5885 | return rhs; |
3e4093b6 RS |
5886 | |
5887 | if (coder == VOID_TYPE) | |
400fbf9f | 5888 | { |
6dcc04b0 JM |
5889 | /* Except for passing an argument to an unprototyped function, |
5890 | this is a constraint violation. When passing an argument to | |
5891 | an unprototyped function, it is compile-time undefined; | |
5892 | making it a constraint in that case was rejected in | |
5893 | DR#252. */ | |
c2255bc4 | 5894 | error_at (location, "void value not ignored as it ought to be"); |
3e4093b6 | 5895 | return error_mark_node; |
400fbf9f | 5896 | } |
808d6eaa JM |
5897 | rhs = require_complete_type (rhs); |
5898 | if (rhs == error_mark_node) | |
5899 | return error_mark_node; | |
1807ffc1 MS |
5900 | |
5901 | if (coder == POINTER_TYPE && reject_gcc_builtin (rhs)) | |
5902 | return error_mark_node; | |
5903 | ||
cd192ccc MS |
5904 | /* A non-reference type can convert to a reference. This handles |
5905 | va_start, va_copy and possibly port built-ins. */ | |
5906 | if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) | |
400fbf9f | 5907 | { |
3e4093b6 | 5908 | if (!lvalue_p (rhs)) |
400fbf9f | 5909 | { |
c2255bc4 | 5910 | error_at (location, "cannot pass rvalue to reference parameter"); |
3e4093b6 | 5911 | return error_mark_node; |
400fbf9f | 5912 | } |
3e4093b6 RS |
5913 | if (!c_mark_addressable (rhs)) |
5914 | return error_mark_node; | |
5915 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
c2255bc4 | 5916 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5917 | |
81e5eca8 | 5918 | rhs = convert_for_assignment (location, expr_loc, |
68fca595 MP |
5919 | build_pointer_type (TREE_TYPE (type)), |
5920 | rhs, origtype, errtype, | |
5921 | null_pointer_constant, fundecl, function, | |
5922 | parmnum); | |
cd192ccc MS |
5923 | if (rhs == error_mark_node) |
5924 | return error_mark_node; | |
3e4093b6 RS |
5925 | |
5926 | rhs = build1 (NOP_EXPR, type, rhs); | |
c2255bc4 | 5927 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5928 | return rhs; |
400fbf9f | 5929 | } |
3e4093b6 | 5930 | /* Some types can interconvert without explicit casts. */ |
3274deff | 5931 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE |
00c8e9f6 | 5932 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) |
3e4093b6 RS |
5933 | return convert (type, rhs); |
5934 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
5935 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
ab22c1fa | 5936 | || codel == FIXED_POINT_TYPE |
3e4093b6 RS |
5937 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE |
5938 | || codel == BOOLEAN_TYPE) | |
5939 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
ab22c1fa | 5940 | || coder == FIXED_POINT_TYPE |
3e4093b6 RS |
5941 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE |
5942 | || coder == BOOLEAN_TYPE)) | |
928c19bb JM |
5943 | { |
5944 | tree ret; | |
5945 | bool save = in_late_binary_op; | |
e5341100 JJ |
5946 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE |
5947 | || (coder == REAL_TYPE | |
5948 | && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE) | |
5949 | && (flag_sanitize & SANITIZE_FLOAT_CAST))) | |
928c19bb | 5950 | in_late_binary_op = true; |
81e5eca8 MP |
5951 | ret = convert_and_check (expr_loc != UNKNOWN_LOCATION |
5952 | ? expr_loc : location, type, orig_rhs); | |
e5341100 | 5953 | in_late_binary_op = save; |
928c19bb JM |
5954 | return ret; |
5955 | } | |
400fbf9f | 5956 | |
79077aea JJ |
5957 | /* Aggregates in different TUs might need conversion. */ |
5958 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
5959 | && codel == coder | |
5960 | && comptypes (type, rhstype)) | |
81e5eca8 MP |
5961 | return convert_and_check (expr_loc != UNKNOWN_LOCATION |
5962 | ? expr_loc : location, type, rhs); | |
79077aea | 5963 | |
ebf0bf7f | 5964 | /* Conversion to a transparent union or record from its member types. |
3e4093b6 | 5965 | This applies only to function arguments. */ |
ebf0bf7f JJ |
5966 | if (((codel == UNION_TYPE || codel == RECORD_TYPE) |
5967 | && TYPE_TRANSPARENT_AGGR (type)) | |
6b4ef5c1 | 5968 | && errtype == ic_argpass) |
400fbf9f | 5969 | { |
0257e383 | 5970 | tree memb, marginal_memb = NULL_TREE; |
3e4093b6 | 5971 | |
910ad8de | 5972 | for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) |
400fbf9f | 5973 | { |
0257e383 | 5974 | tree memb_type = TREE_TYPE (memb); |
400fbf9f | 5975 | |
3e4093b6 | 5976 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
132da1a5 | 5977 | TYPE_MAIN_VARIANT (rhstype))) |
3e4093b6 | 5978 | break; |
e58cd767 | 5979 | |
3e4093b6 RS |
5980 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
5981 | continue; | |
2f6e4e97 | 5982 | |
3e4093b6 RS |
5983 | if (coder == POINTER_TYPE) |
5984 | { | |
5985 | tree ttl = TREE_TYPE (memb_type); | |
5986 | tree ttr = TREE_TYPE (rhstype); | |
400fbf9f | 5987 | |
3e4093b6 RS |
5988 | /* Any non-function converts to a [const][volatile] void * |
5989 | and vice versa; otherwise, targets must be the same. | |
5990 | Meanwhile, the lhs target must have all the qualifiers of | |
5991 | the rhs. */ | |
267bac10 JM |
5992 | if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) |
5993 | || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) | |
744aa42f | 5994 | || comp_target_types (location, memb_type, rhstype)) |
3e4093b6 | 5995 | { |
267bac10 JM |
5996 | int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC; |
5997 | int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC; | |
3e4093b6 | 5998 | /* If this type won't generate any warnings, use it. */ |
267bac10 | 5999 | if (lquals == rquals |
3e4093b6 RS |
6000 | || ((TREE_CODE (ttr) == FUNCTION_TYPE |
6001 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
267bac10 JM |
6002 | ? ((lquals | rquals) == rquals) |
6003 | : ((lquals | rquals) == lquals))) | |
3e4093b6 | 6004 | break; |
400fbf9f | 6005 | |
3e4093b6 | 6006 | /* Keep looking for a better type, but remember this one. */ |
0257e383 RH |
6007 | if (!marginal_memb) |
6008 | marginal_memb = memb; | |
3e4093b6 RS |
6009 | } |
6010 | } | |
82bde854 | 6011 | |
3e4093b6 | 6012 | /* Can convert integer zero to any pointer type. */ |
928c19bb | 6013 | if (null_pointer_constant) |
3e4093b6 RS |
6014 | { |
6015 | rhs = null_pointer_node; | |
6016 | break; | |
6017 | } | |
6018 | } | |
400fbf9f | 6019 | |
0257e383 | 6020 | if (memb || marginal_memb) |
3e4093b6 | 6021 | { |
0257e383 | 6022 | if (!memb) |
3e4093b6 RS |
6023 | { |
6024 | /* We have only a marginally acceptable member type; | |
6025 | it needs a warning. */ | |
0257e383 | 6026 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); |
3e4093b6 | 6027 | tree ttr = TREE_TYPE (rhstype); |
714a0864 | 6028 | |
3e4093b6 RS |
6029 | /* Const and volatile mean something different for function |
6030 | types, so the usual warnings are not appropriate. */ | |
6031 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
6032 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
6033 | { | |
6034 | /* Because const and volatile on functions are | |
6035 | restrictions that say the function will not do | |
6036 | certain things, it is okay to use a const or volatile | |
6037 | function where an ordinary one is wanted, but not | |
6038 | vice-versa. */ | |
36c5e70a BE |
6039 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
6040 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
768952be MU |
6041 | PEDWARN_FOR_QUALIFIERS (location, expr_loc, |
6042 | OPT_Wdiscarded_qualifiers, | |
6043 | G_("passing argument %d of %qE " | |
6044 | "makes %q#v qualified function " | |
6045 | "pointer from unqualified"), | |
6046 | G_("assignment makes %q#v qualified " | |
6047 | "function pointer from " | |
6048 | "unqualified"), | |
6049 | G_("initialization makes %q#v qualified " | |
6050 | "function pointer from " | |
6051 | "unqualified"), | |
6052 | G_("return makes %q#v qualified function " | |
6053 | "pointer from unqualified"), | |
6054 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 | 6055 | } |
36c5e70a BE |
6056 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
6057 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
768952be MU |
6058 | PEDWARN_FOR_QUALIFIERS (location, expr_loc, |
6059 | OPT_Wdiscarded_qualifiers, | |
6060 | G_("passing argument %d of %qE discards " | |
6061 | "%qv qualifier from pointer target type"), | |
6062 | G_("assignment discards %qv qualifier " | |
6063 | "from pointer target type"), | |
6064 | G_("initialization discards %qv qualifier " | |
6065 | "from pointer target type"), | |
6066 | G_("return discards %qv qualifier from " | |
6067 | "pointer target type"), | |
6068 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
0257e383 RH |
6069 | |
6070 | memb = marginal_memb; | |
3e4093b6 | 6071 | } |
400fbf9f | 6072 | |
fcf73884 | 6073 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) |
c1771a20 | 6074 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 6075 | "ISO C prohibits argument conversion to union type"); |
0e7c47fa | 6076 | |
db3927fb | 6077 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); |
0257e383 | 6078 | return build_constructor_single (type, memb, rhs); |
3e4093b6 | 6079 | } |
0e7c47fa RK |
6080 | } |
6081 | ||
3e4093b6 RS |
6082 | /* Conversions among pointers */ |
6083 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
6084 | && (coder == codel)) | |
400fbf9f | 6085 | { |
3e4093b6 RS |
6086 | tree ttl = TREE_TYPE (type); |
6087 | tree ttr = TREE_TYPE (rhstype); | |
46df2823 JM |
6088 | tree mvl = ttl; |
6089 | tree mvr = ttr; | |
3e4093b6 | 6090 | bool is_opaque_pointer; |
264fa2db | 6091 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
36c5e70a BE |
6092 | addr_space_t asl; |
6093 | addr_space_t asr; | |
400fbf9f | 6094 | |
46df2823 | 6095 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
267bac10 JM |
6096 | mvl = (TYPE_ATOMIC (mvl) |
6097 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), | |
6098 | TYPE_QUAL_ATOMIC) | |
6099 | : TYPE_MAIN_VARIANT (mvl)); | |
46df2823 | 6100 | if (TREE_CODE (mvr) != ARRAY_TYPE) |
267bac10 JM |
6101 | mvr = (TYPE_ATOMIC (mvr) |
6102 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), | |
6103 | TYPE_QUAL_ATOMIC) | |
6104 | : TYPE_MAIN_VARIANT (mvr)); | |
3e4093b6 | 6105 | /* Opaque pointers are treated like void pointers. */ |
f83c7f63 | 6106 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); |
c22cacf3 | 6107 | |
478a1c5b ILT |
6108 | /* The Plan 9 compiler permits a pointer to a struct to be |
6109 | automatically converted into a pointer to an anonymous field | |
6110 | within the struct. */ | |
6111 | if (flag_plan9_extensions | |
6112 | && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE) | |
6113 | && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE) | |
6114 | && mvl != mvr) | |
6115 | { | |
6116 | tree new_rhs = convert_to_anonymous_field (location, type, rhs); | |
6117 | if (new_rhs != NULL_TREE) | |
6118 | { | |
6119 | rhs = new_rhs; | |
6120 | rhstype = TREE_TYPE (rhs); | |
6121 | coder = TREE_CODE (rhstype); | |
6122 | ttr = TREE_TYPE (rhstype); | |
6123 | mvr = TYPE_MAIN_VARIANT (ttr); | |
6124 | } | |
6125 | } | |
6126 | ||
b7e20b53 | 6127 | /* C++ does not allow the implicit conversion void* -> T*. However, |
c22cacf3 MS |
6128 | for the purpose of reducing the number of false positives, we |
6129 | tolerate the special case of | |
b7e20b53 | 6130 | |
c22cacf3 | 6131 | int *p = NULL; |
b7e20b53 | 6132 | |
c22cacf3 | 6133 | where NULL is typically defined in C to be '(void *) 0'. */ |
c6bdf92e | 6134 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) |
8ffcdea8 MP |
6135 | warning_at (errtype == ic_argpass ? expr_loc : location, |
6136 | OPT_Wc___compat, | |
6137 | "request for implicit conversion " | |
c2255bc4 | 6138 | "from %qT to %qT not permitted in C++", rhstype, type); |
400fbf9f | 6139 | |
36c5e70a BE |
6140 | /* See if the pointers point to incompatible address spaces. */ |
6141 | asl = TYPE_ADDR_SPACE (ttl); | |
6142 | asr = TYPE_ADDR_SPACE (ttr); | |
6143 | if (!null_pointer_constant_p (rhs) | |
6144 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
6145 | { | |
6146 | switch (errtype) | |
6147 | { | |
6148 | case ic_argpass: | |
8ffcdea8 | 6149 | error_at (expr_loc, "passing argument %d of %qE from pointer to " |
36c5e70a BE |
6150 | "non-enclosed address space", parmnum, rname); |
6151 | break; | |
6152 | case ic_assign: | |
6153 | error_at (location, "assignment from pointer to " | |
6154 | "non-enclosed address space"); | |
6155 | break; | |
6156 | case ic_init: | |
6157 | error_at (location, "initialization from pointer to " | |
6158 | "non-enclosed address space"); | |
6159 | break; | |
6160 | case ic_return: | |
6161 | error_at (location, "return from pointer to " | |
6162 | "non-enclosed address space"); | |
6163 | break; | |
6164 | default: | |
6165 | gcc_unreachable (); | |
6166 | } | |
6167 | return error_mark_node; | |
6168 | } | |
6169 | ||
7876a414 KG |
6170 | /* Check if the right-hand side has a format attribute but the |
6171 | left-hand side doesn't. */ | |
90137d8f | 6172 | if (warn_suggest_attribute_format |
104f8784 | 6173 | && check_missing_format_attribute (type, rhstype)) |
c22cacf3 | 6174 | { |
104f8784 KG |
6175 | switch (errtype) |
6176 | { | |
6177 | case ic_argpass: | |
8ffcdea8 | 6178 | warning_at (expr_loc, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6179 | "argument %d of %qE might be " |
6180 | "a candidate for a format attribute", | |
6181 | parmnum, rname); | |
104f8784 KG |
6182 | break; |
6183 | case ic_assign: | |
90137d8f | 6184 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6185 | "assignment left-hand side might be " |
6186 | "a candidate for a format attribute"); | |
104f8784 KG |
6187 | break; |
6188 | case ic_init: | |
90137d8f | 6189 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6190 | "initialization left-hand side might be " |
6191 | "a candidate for a format attribute"); | |
104f8784 KG |
6192 | break; |
6193 | case ic_return: | |
90137d8f | 6194 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6195 | "return type might be " |
6196 | "a candidate for a format attribute"); | |
104f8784 KG |
6197 | break; |
6198 | default: | |
6199 | gcc_unreachable (); | |
6200 | } | |
7876a414 | 6201 | } |
c22cacf3 | 6202 | |
3e4093b6 RS |
6203 | /* Any non-function converts to a [const][volatile] void * |
6204 | and vice versa; otherwise, targets must be the same. | |
6205 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
267bac10 JM |
6206 | if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) |
6207 | || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) | |
744aa42f | 6208 | || (target_cmp = comp_target_types (location, type, rhstype)) |
3e4093b6 | 6209 | || is_opaque_pointer |
f8a93a2e JJ |
6210 | || ((c_common_unsigned_type (mvl) |
6211 | == c_common_unsigned_type (mvr)) | |
267bac10 JM |
6212 | && (c_common_signed_type (mvl) |
6213 | == c_common_signed_type (mvr)) | |
6214 | && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr))) | |
3e4093b6 | 6215 | { |
768952be MU |
6216 | /* Warn about loss of qualifers from pointers to arrays with |
6217 | qualifiers on the element type. */ | |
6218 | if (TREE_CODE (ttr) == ARRAY_TYPE) | |
6219 | { | |
6220 | ttr = strip_array_types (ttr); | |
6221 | ttl = strip_array_types (ttl); | |
6222 | ||
6223 | if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) | |
6224 | & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) | |
6225 | WARNING_FOR_QUALIFIERS (location, expr_loc, | |
6226 | OPT_Wdiscarded_array_qualifiers, | |
6227 | G_("passing argument %d of %qE discards " | |
6228 | "%qv qualifier from pointer target type"), | |
6229 | G_("assignment discards %qv qualifier " | |
6230 | "from pointer target type"), | |
6231 | G_("initialization discards %qv qualifier " | |
6232 | "from pointer target type"), | |
6233 | G_("return discards %qv qualifier from " | |
6234 | "pointer target type"), | |
6235 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
6236 | } | |
6237 | else if (pedantic | |
3e4093b6 RS |
6238 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) |
6239 | || | |
6240 | (VOID_TYPE_P (ttr) | |
928c19bb | 6241 | && !null_pointer_constant |
3e4093b6 | 6242 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
768952be MU |
6243 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic, |
6244 | G_("ISO C forbids passing argument %d of " | |
6245 | "%qE between function pointer " | |
6246 | "and %<void *%>"), | |
6247 | G_("ISO C forbids assignment between " | |
6248 | "function pointer and %<void *%>"), | |
6249 | G_("ISO C forbids initialization between " | |
6250 | "function pointer and %<void *%>"), | |
6251 | G_("ISO C forbids return between function " | |
6252 | "pointer and %<void *%>")); | |
3e4093b6 RS |
6253 | /* Const and volatile mean something different for function types, |
6254 | so the usual warnings are not appropriate. */ | |
6255 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
6256 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
6257 | { | |
768952be MU |
6258 | /* Don't warn about loss of qualifier for conversions from |
6259 | qualified void* to pointers to arrays with corresponding | |
6260 | qualifier on the element type. */ | |
6261 | if (!pedantic) | |
6262 | ttl = strip_array_types (ttl); | |
6263 | ||
267bac10 JM |
6264 | /* Assignments between atomic and non-atomic objects are OK. */ |
6265 | if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) | |
6266 | & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) | |
58393038 | 6267 | { |
768952be MU |
6268 | PEDWARN_FOR_QUALIFIERS (location, expr_loc, |
6269 | OPT_Wdiscarded_qualifiers, | |
6270 | G_("passing argument %d of %qE discards " | |
6271 | "%qv qualifier from pointer target type"), | |
6272 | G_("assignment discards %qv qualifier " | |
6273 | "from pointer target type"), | |
6274 | G_("initialization discards %qv qualifier " | |
6275 | "from pointer target type"), | |
6276 | G_("return discards %qv qualifier from " | |
6277 | "pointer target type"), | |
6278 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
58393038 | 6279 | } |
3e4093b6 RS |
6280 | /* If this is not a case of ignoring a mismatch in signedness, |
6281 | no warning. */ | |
6282 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 6283 | || target_cmp) |
3e4093b6 RS |
6284 | ; |
6285 | /* If there is a mismatch, do warn. */ | |
f2fd3821 | 6286 | else if (warn_pointer_sign) |
768952be MU |
6287 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign, |
6288 | G_("pointer targets in passing argument " | |
6289 | "%d of %qE differ in signedness"), | |
6290 | G_("pointer targets in assignment " | |
6291 | "differ in signedness"), | |
6292 | G_("pointer targets in initialization " | |
6293 | "differ in signedness"), | |
6294 | G_("pointer targets in return differ " | |
6295 | "in signedness")); | |
3e4093b6 RS |
6296 | } |
6297 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
6298 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
6299 | { | |
6300 | /* Because const and volatile on functions are restrictions | |
6301 | that say the function will not do certain things, | |
6302 | it is okay to use a const or volatile function | |
6303 | where an ordinary one is wanted, but not vice-versa. */ | |
36c5e70a BE |
6304 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
6305 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
768952be MU |
6306 | PEDWARN_FOR_QUALIFIERS (location, expr_loc, |
6307 | OPT_Wdiscarded_qualifiers, | |
6308 | G_("passing argument %d of %qE makes " | |
6309 | "%q#v qualified function pointer " | |
6310 | "from unqualified"), | |
6311 | G_("assignment makes %q#v qualified function " | |
6312 | "pointer from unqualified"), | |
6313 | G_("initialization makes %q#v qualified " | |
6314 | "function pointer from unqualified"), | |
6315 | G_("return makes %q#v qualified function " | |
6316 | "pointer from unqualified"), | |
6317 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 RS |
6318 | } |
6319 | } | |
6320 | else | |
58393038 ZL |
6321 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
6322 | if (!objc_ok) | |
768952be MU |
6323 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, |
6324 | OPT_Wincompatible_pointer_types, | |
6325 | G_("passing argument %d of %qE from " | |
6326 | "incompatible pointer type"), | |
6327 | G_("assignment from incompatible pointer type"), | |
6328 | G_("initialization from incompatible " | |
6329 | "pointer type"), | |
6330 | G_("return from incompatible pointer type")); | |
58393038 | 6331 | |
3e4093b6 RS |
6332 | return convert (type, rhs); |
6333 | } | |
b494fd98 EB |
6334 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
6335 | { | |
6dcc04b0 JM |
6336 | /* ??? This should not be an error when inlining calls to |
6337 | unprototyped functions. */ | |
c2255bc4 | 6338 | error_at (location, "invalid use of non-lvalue array"); |
b494fd98 EB |
6339 | return error_mark_node; |
6340 | } | |
3e4093b6 | 6341 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
400fbf9f | 6342 | { |
3e4093b6 RS |
6343 | /* An explicit constant 0 can convert to a pointer, |
6344 | or one that results from arithmetic, even including | |
6345 | a cast to integer type. */ | |
928c19bb | 6346 | if (!null_pointer_constant) |
768952be MU |
6347 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, |
6348 | OPT_Wint_conversion, | |
6349 | G_("passing argument %d of %qE makes " | |
6350 | "pointer from integer without a cast"), | |
6351 | G_("assignment makes pointer from integer " | |
6352 | "without a cast"), | |
6353 | G_("initialization makes pointer from " | |
6354 | "integer without a cast"), | |
6355 | G_("return makes pointer from integer " | |
6356 | "without a cast")); | |
b3006337 EB |
6357 | |
6358 | return convert (type, rhs); | |
400fbf9f | 6359 | } |
3e4093b6 | 6360 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
400fbf9f | 6361 | { |
768952be MU |
6362 | PEDWARN_FOR_ASSIGNMENT (location, expr_loc, |
6363 | OPT_Wint_conversion, | |
6364 | G_("passing argument %d of %qE makes integer " | |
6365 | "from pointer without a cast"), | |
6366 | G_("assignment makes integer from pointer " | |
6367 | "without a cast"), | |
6368 | G_("initialization makes integer from pointer " | |
6369 | "without a cast"), | |
6370 | G_("return makes integer from pointer " | |
6371 | "without a cast")); | |
3e4093b6 RS |
6372 | return convert (type, rhs); |
6373 | } | |
6374 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
928c19bb JM |
6375 | { |
6376 | tree ret; | |
6377 | bool save = in_late_binary_op; | |
6378 | in_late_binary_op = true; | |
6379 | ret = convert (type, rhs); | |
6380 | in_late_binary_op = save; | |
6381 | return ret; | |
6382 | } | |
400fbf9f | 6383 | |
2ac2f164 | 6384 | switch (errtype) |
3e4093b6 | 6385 | { |
2ac2f164 | 6386 | case ic_argpass: |
8ffcdea8 MP |
6387 | error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum, |
6388 | rname); | |
a7da8b42 | 6389 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) |
8ffcdea8 | 6390 | ? DECL_SOURCE_LOCATION (fundecl) : expr_loc, |
a7da8b42 | 6391 | "expected %qT but argument is of type %qT", type, rhstype); |
2ac2f164 JM |
6392 | break; |
6393 | case ic_assign: | |
c2255bc4 AH |
6394 | error_at (location, "incompatible types when assigning to type %qT from " |
6395 | "type %qT", type, rhstype); | |
2ac2f164 JM |
6396 | break; |
6397 | case ic_init: | |
c2255bc4 | 6398 | error_at (location, |
8ffcdea8 | 6399 | "incompatible types when initializing type %qT using type %qT", |
c2255bc4 | 6400 | type, rhstype); |
2ac2f164 JM |
6401 | break; |
6402 | case ic_return: | |
c2255bc4 | 6403 | error_at (location, |
8ffcdea8 | 6404 | "incompatible types when returning type %qT but %qT was " |
c2255bc4 | 6405 | "expected", rhstype, type); |
2ac2f164 JM |
6406 | break; |
6407 | default: | |
6408 | gcc_unreachable (); | |
400fbf9f | 6409 | } |
53b01f59 | 6410 | |
3e4093b6 RS |
6411 | return error_mark_node; |
6412 | } | |
3e4093b6 RS |
6413 | \f |
6414 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
6415 | return its value. Otherwise, return error_mark_node. | |
15b732b2 | 6416 | |
3e4093b6 RS |
6417 | This is for handling COMPOUND_EXPRs as initializer elements |
6418 | which is allowed with a warning when -pedantic is specified. */ | |
15b732b2 | 6419 | |
3e4093b6 RS |
6420 | static tree |
6421 | valid_compound_expr_initializer (tree value, tree endtype) | |
6422 | { | |
6423 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
6424 | { | |
6425 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
6426 | == error_mark_node) | |
6427 | return error_mark_node; | |
6428 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
6429 | endtype); | |
6430 | } | |
116df786 | 6431 | else if (!initializer_constant_valid_p (value, endtype)) |
3e4093b6 RS |
6432 | return error_mark_node; |
6433 | else | |
6434 | return value; | |
15b732b2 | 6435 | } |
400fbf9f | 6436 | \f |
3e4093b6 RS |
6437 | /* Perform appropriate conversions on the initial value of a variable, |
6438 | store it in the declaration DECL, | |
6439 | and print any error messages that are appropriate. | |
bbbbb16a | 6440 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
c2255bc4 AH |
6441 | If the init is invalid, store an ERROR_MARK. |
6442 | ||
6443 | INIT_LOC is the location of the initial value. */ | |
400fbf9f | 6444 | |
3e4093b6 | 6445 | void |
c2255bc4 | 6446 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) |
400fbf9f | 6447 | { |
3e4093b6 | 6448 | tree value, type; |
928c19bb | 6449 | bool npc = false; |
400fbf9f | 6450 | |
3e4093b6 | 6451 | /* If variable's type was invalidly declared, just ignore it. */ |
400fbf9f | 6452 | |
3e4093b6 RS |
6453 | type = TREE_TYPE (decl); |
6454 | if (TREE_CODE (type) == ERROR_MARK) | |
6455 | return; | |
400fbf9f | 6456 | |
3e4093b6 | 6457 | /* Digest the specified initializer into an expression. */ |
400fbf9f | 6458 | |
928c19bb JM |
6459 | if (init) |
6460 | npc = null_pointer_constant_p (init); | |
c2255bc4 AH |
6461 | value = digest_init (init_loc, type, init, origtype, npc, |
6462 | true, TREE_STATIC (decl)); | |
400fbf9f | 6463 | |
3e4093b6 | 6464 | /* Store the expression if valid; else report error. */ |
400fbf9f | 6465 | |
8400e75e | 6466 | if (!in_system_header_at (input_location) |
3f75a254 | 6467 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) |
3176a0c2 DD |
6468 | warning (OPT_Wtraditional, "traditional C rejects automatic " |
6469 | "aggregate initialization"); | |
2f6e4e97 | 6470 | |
dea63e49 JJ |
6471 | if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL) |
6472 | DECL_INITIAL (decl) = value; | |
400fbf9f | 6473 | |
3e4093b6 RS |
6474 | /* ANSI wants warnings about out-of-range constant initializers. */ |
6475 | STRIP_TYPE_NOPS (value); | |
b8698a0f | 6476 | if (TREE_STATIC (decl)) |
c2658540 | 6477 | constant_expression_warning (value); |
400fbf9f | 6478 | |
3e4093b6 RS |
6479 | /* Check if we need to set array size from compound literal size. */ |
6480 | if (TREE_CODE (type) == ARRAY_TYPE | |
6481 | && TYPE_DOMAIN (type) == 0 | |
6482 | && value != error_mark_node) | |
400fbf9f | 6483 | { |
3e4093b6 RS |
6484 | tree inside_init = init; |
6485 | ||
ed248cf7 | 6486 | STRIP_TYPE_NOPS (inside_init); |
3e4093b6 RS |
6487 | inside_init = fold (inside_init); |
6488 | ||
6489 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
6490 | { | |
8d9f82d5 | 6491 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
3e4093b6 | 6492 | |
8d9f82d5 | 6493 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) |
3e4093b6 RS |
6494 | { |
6495 | /* For int foo[] = (int [3]){1}; we need to set array size | |
6496 | now since later on array initializer will be just the | |
6497 | brace enclosed list of the compound literal. */ | |
e30ecc5d | 6498 | tree etype = strip_array_types (TREE_TYPE (decl)); |
8d9f82d5 | 6499 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); |
8d9f82d5 | 6500 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); |
3e4093b6 | 6501 | layout_type (type); |
8d9f82d5 | 6502 | layout_decl (cldecl, 0); |
e30ecc5d JJ |
6503 | TREE_TYPE (decl) |
6504 | = c_build_qualified_type (type, TYPE_QUALS (etype)); | |
3e4093b6 RS |
6505 | } |
6506 | } | |
400fbf9f | 6507 | } |
3e4093b6 RS |
6508 | } |
6509 | \f | |
6510 | /* Methods for storing and printing names for error messages. */ | |
400fbf9f | 6511 | |
3e4093b6 RS |
6512 | /* Implement a spelling stack that allows components of a name to be pushed |
6513 | and popped. Each element on the stack is this structure. */ | |
400fbf9f | 6514 | |
3e4093b6 RS |
6515 | struct spelling |
6516 | { | |
6517 | int kind; | |
6518 | union | |
400fbf9f | 6519 | { |
a0f0ab9f | 6520 | unsigned HOST_WIDE_INT i; |
3e4093b6 RS |
6521 | const char *s; |
6522 | } u; | |
6523 | }; | |
2f6e4e97 | 6524 | |
3e4093b6 RS |
6525 | #define SPELLING_STRING 1 |
6526 | #define SPELLING_MEMBER 2 | |
6527 | #define SPELLING_BOUNDS 3 | |
400fbf9f | 6528 | |
3e4093b6 RS |
6529 | static struct spelling *spelling; /* Next stack element (unused). */ |
6530 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
6531 | static int spelling_size; /* Size of the spelling stack. */ | |
400fbf9f | 6532 | |
3e4093b6 RS |
6533 | /* Macros to save and restore the spelling stack around push_... functions. |
6534 | Alternative to SAVE_SPELLING_STACK. */ | |
400fbf9f | 6535 | |
3e4093b6 RS |
6536 | #define SPELLING_DEPTH() (spelling - spelling_base) |
6537 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
400fbf9f | 6538 | |
3e4093b6 RS |
6539 | /* Push an element on the spelling stack with type KIND and assign VALUE |
6540 | to MEMBER. */ | |
400fbf9f | 6541 | |
3e4093b6 RS |
6542 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
6543 | { \ | |
6544 | int depth = SPELLING_DEPTH (); \ | |
6545 | \ | |
6546 | if (depth >= spelling_size) \ | |
6547 | { \ | |
6548 | spelling_size += 10; \ | |
cca8ead2 BI |
6549 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
6550 | spelling_size); \ | |
3e4093b6 RS |
6551 | RESTORE_SPELLING_DEPTH (depth); \ |
6552 | } \ | |
6553 | \ | |
6554 | spelling->kind = (KIND); \ | |
6555 | spelling->MEMBER = (VALUE); \ | |
6556 | spelling++; \ | |
6557 | } | |
400fbf9f | 6558 | |
3e4093b6 | 6559 | /* Push STRING on the stack. Printed literally. */ |
400fbf9f | 6560 | |
3e4093b6 RS |
6561 | static void |
6562 | push_string (const char *string) | |
6563 | { | |
6564 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
6565 | } | |
400fbf9f | 6566 | |
3e4093b6 | 6567 | /* Push a member name on the stack. Printed as '.' STRING. */ |
400fbf9f | 6568 | |
3e4093b6 RS |
6569 | static void |
6570 | push_member_name (tree decl) | |
6571 | { | |
6572 | const char *const string | |
88388a52 JM |
6573 | = (DECL_NAME (decl) |
6574 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
6575 | : _("<anonymous>")); | |
3e4093b6 RS |
6576 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
6577 | } | |
400fbf9f | 6578 | |
3e4093b6 | 6579 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
400fbf9f | 6580 | |
3e4093b6 | 6581 | static void |
a0f0ab9f | 6582 | push_array_bounds (unsigned HOST_WIDE_INT bounds) |
3e4093b6 RS |
6583 | { |
6584 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
6585 | } | |
bb58bec5 | 6586 | |
3e4093b6 | 6587 | /* Compute the maximum size in bytes of the printed spelling. */ |
400fbf9f | 6588 | |
3e4093b6 RS |
6589 | static int |
6590 | spelling_length (void) | |
6591 | { | |
6592 | int size = 0; | |
6593 | struct spelling *p; | |
400fbf9f | 6594 | |
3e4093b6 RS |
6595 | for (p = spelling_base; p < spelling; p++) |
6596 | { | |
6597 | if (p->kind == SPELLING_BOUNDS) | |
6598 | size += 25; | |
6599 | else | |
6600 | size += strlen (p->u.s) + 1; | |
6601 | } | |
6602 | ||
6603 | return size; | |
400fbf9f | 6604 | } |
400fbf9f | 6605 | |
3e4093b6 | 6606 | /* Print the spelling to BUFFER and return it. */ |
400fbf9f | 6607 | |
3e4093b6 RS |
6608 | static char * |
6609 | print_spelling (char *buffer) | |
400fbf9f | 6610 | { |
3e4093b6 RS |
6611 | char *d = buffer; |
6612 | struct spelling *p; | |
400fbf9f | 6613 | |
3e4093b6 RS |
6614 | for (p = spelling_base; p < spelling; p++) |
6615 | if (p->kind == SPELLING_BOUNDS) | |
6616 | { | |
a0f0ab9f | 6617 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); |
3e4093b6 RS |
6618 | d += strlen (d); |
6619 | } | |
6620 | else | |
6621 | { | |
6622 | const char *s; | |
6623 | if (p->kind == SPELLING_MEMBER) | |
6624 | *d++ = '.'; | |
6625 | for (s = p->u.s; (*d = *s++); d++) | |
6626 | ; | |
6627 | } | |
6628 | *d++ = '\0'; | |
6629 | return buffer; | |
6630 | } | |
400fbf9f | 6631 | |
3e4093b6 RS |
6632 | /* Digest the parser output INIT as an initializer for type TYPE. |
6633 | Return a C expression of type TYPE to represent the initial value. | |
7e842ef8 | 6634 | |
bbbbb16a ILT |
6635 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
6636 | ||
928c19bb JM |
6637 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. |
6638 | ||
916c5919 JM |
6639 | If INIT is a string constant, STRICT_STRING is true if it is |
6640 | unparenthesized or we should not warn here for it being parenthesized. | |
6641 | For other types of INIT, STRICT_STRING is not used. | |
6642 | ||
c2255bc4 AH |
6643 | INIT_LOC is the location of the INIT. |
6644 | ||
3e4093b6 RS |
6645 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
6646 | elements are seen. */ | |
7e842ef8 | 6647 | |
3e4093b6 | 6648 | static tree |
c2255bc4 AH |
6649 | digest_init (location_t init_loc, tree type, tree init, tree origtype, |
6650 | bool null_pointer_constant, bool strict_string, | |
6651 | int require_constant) | |
3e4093b6 RS |
6652 | { |
6653 | enum tree_code code = TREE_CODE (type); | |
6654 | tree inside_init = init; | |
8ce94e44 | 6655 | tree semantic_type = NULL_TREE; |
928c19bb | 6656 | bool maybe_const = true; |
7e842ef8 | 6657 | |
3e4093b6 | 6658 | if (type == error_mark_node |
f01da1a5 | 6659 | || !init |
7a0ca710 | 6660 | || error_operand_p (init)) |
3e4093b6 | 6661 | return error_mark_node; |
7e842ef8 | 6662 | |
ed248cf7 | 6663 | STRIP_TYPE_NOPS (inside_init); |
7e842ef8 | 6664 | |
8ce94e44 JM |
6665 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) |
6666 | { | |
6667 | semantic_type = TREE_TYPE (inside_init); | |
6668 | inside_init = TREE_OPERAND (inside_init, 0); | |
6669 | } | |
928c19bb JM |
6670 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); |
6671 | inside_init = decl_constant_value_for_optimization (inside_init); | |
7e842ef8 | 6672 | |
3e4093b6 RS |
6673 | /* Initialization of an array of chars from a string constant |
6674 | optionally enclosed in braces. */ | |
7e842ef8 | 6675 | |
197463ae JM |
6676 | if (code == ARRAY_TYPE && inside_init |
6677 | && TREE_CODE (inside_init) == STRING_CST) | |
3e4093b6 | 6678 | { |
267bac10 JM |
6679 | tree typ1 |
6680 | = (TYPE_ATOMIC (TREE_TYPE (type)) | |
6681 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)), | |
6682 | TYPE_QUAL_ATOMIC) | |
6683 | : TYPE_MAIN_VARIANT (TREE_TYPE (type))); | |
197463ae JM |
6684 | /* Note that an array could be both an array of character type |
6685 | and an array of wchar_t if wchar_t is signed char or unsigned | |
6686 | char. */ | |
6687 | bool char_array = (typ1 == char_type_node | |
6688 | || typ1 == signed_char_type_node | |
6689 | || typ1 == unsigned_char_type_node); | |
6690 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
c466b2cd KVH |
6691 | bool char16_array = !!comptypes (typ1, char16_type_node); |
6692 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
6693 | ||
6694 | if (char_array || wchar_array || char16_array || char32_array) | |
7e842ef8 | 6695 | { |
916c5919 | 6696 | struct c_expr expr; |
c466b2cd | 6697 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); |
916c5919 JM |
6698 | expr.value = inside_init; |
6699 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
6866c6e8 | 6700 | expr.original_type = NULL; |
d033409e | 6701 | maybe_warn_string_init (init_loc, type, expr); |
916c5919 | 6702 | |
a45e580b | 6703 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
c1771a20 | 6704 | pedwarn_init (init_loc, OPT_Wpedantic, |
a45e580b JM |
6705 | "initialization of a flexible array member"); |
6706 | ||
3e4093b6 | 6707 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 6708 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 | 6709 | return inside_init; |
7e842ef8 | 6710 | |
c466b2cd | 6711 | if (char_array) |
3e4093b6 | 6712 | { |
c466b2cd KVH |
6713 | if (typ2 != char_type_node) |
6714 | { | |
ea58ef42 MP |
6715 | error_init (init_loc, "char-array initialized from wide " |
6716 | "string"); | |
c466b2cd KVH |
6717 | return error_mark_node; |
6718 | } | |
3e4093b6 | 6719 | } |
c466b2cd | 6720 | else |
3e4093b6 | 6721 | { |
c466b2cd KVH |
6722 | if (typ2 == char_type_node) |
6723 | { | |
ea58ef42 MP |
6724 | error_init (init_loc, "wide character array initialized " |
6725 | "from non-wide string"); | |
c466b2cd KVH |
6726 | return error_mark_node; |
6727 | } | |
6728 | else if (!comptypes(typ1, typ2)) | |
6729 | { | |
ea58ef42 MP |
6730 | error_init (init_loc, "wide character array initialized " |
6731 | "from incompatible wide string"); | |
c466b2cd KVH |
6732 | return error_mark_node; |
6733 | } | |
7e842ef8 | 6734 | } |
2f6e4e97 | 6735 | |
3e4093b6 RS |
6736 | TREE_TYPE (inside_init) = type; |
6737 | if (TYPE_DOMAIN (type) != 0 | |
6738 | && TYPE_SIZE (type) != 0 | |
5eb4df45 ILT |
6739 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
6740 | { | |
6741 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
6742 | ||
c466b2cd | 6743 | /* Subtract the size of a single (possibly wide) character |
3e4093b6 RS |
6744 | because it's ok to ignore the terminating null char |
6745 | that is counted in the length of the constant. */ | |
5eb4df45 ILT |
6746 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), |
6747 | (len | |
6748 | - (TYPE_PRECISION (typ1) | |
6749 | / BITS_PER_UNIT)))) | |
6750 | pedwarn_init (init_loc, 0, | |
6751 | ("initializer-string for array of chars " | |
6752 | "is too long")); | |
6753 | else if (warn_cxx_compat | |
6754 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
6755 | warning_at (init_loc, OPT_Wc___compat, | |
6756 | ("initializer-string for array chars " | |
6757 | "is too long for C++")); | |
6758 | } | |
7e842ef8 | 6759 | |
3e4093b6 | 6760 | return inside_init; |
7e842ef8 | 6761 | } |
197463ae JM |
6762 | else if (INTEGRAL_TYPE_P (typ1)) |
6763 | { | |
ea58ef42 | 6764 | error_init (init_loc, "array of inappropriate type initialized " |
197463ae JM |
6765 | "from string constant"); |
6766 | return error_mark_node; | |
6767 | } | |
7e842ef8 PE |
6768 | } |
6769 | ||
3e4093b6 RS |
6770 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
6771 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
6772 | below and handle as a constructor. */ | |
e89be13b | 6773 | if (code == VECTOR_TYPE |
31521951 | 6774 | && VECTOR_TYPE_P (TREE_TYPE (inside_init)) |
00c8e9f6 | 6775 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) |
e89be13b JJ |
6776 | && TREE_CONSTANT (inside_init)) |
6777 | { | |
6778 | if (TREE_CODE (inside_init) == VECTOR_CST | |
6779 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6780 | TYPE_MAIN_VARIANT (type))) | |
6781 | return inside_init; | |
6782 | ||
6783 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
6784 | { | |
4038c495 GB |
6785 | unsigned HOST_WIDE_INT ix; |
6786 | tree value; | |
6787 | bool constant_p = true; | |
e89be13b JJ |
6788 | |
6789 | /* Iterate through elements and check if all constructor | |
6790 | elements are *_CSTs. */ | |
4038c495 GB |
6791 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) |
6792 | if (!CONSTANT_CLASS_P (value)) | |
6793 | { | |
6794 | constant_p = false; | |
6795 | break; | |
6796 | } | |
e89be13b | 6797 | |
4038c495 GB |
6798 | if (constant_p) |
6799 | return build_vector_from_ctor (type, | |
6800 | CONSTRUCTOR_ELTS (inside_init)); | |
e89be13b JJ |
6801 | } |
6802 | } | |
6035d635 | 6803 | |
ca085fd7 MLI |
6804 | if (warn_sequence_point) |
6805 | verify_sequence_points (inside_init); | |
6806 | ||
3e4093b6 RS |
6807 | /* Any type can be initialized |
6808 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 6809 | |
3e4093b6 RS |
6810 | if (inside_init && TREE_TYPE (inside_init) != 0 |
6811 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
132da1a5 | 6812 | TYPE_MAIN_VARIANT (type)) |
3e4093b6 | 6813 | || (code == ARRAY_TYPE |
132da1a5 | 6814 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6815 | || (code == VECTOR_TYPE |
132da1a5 | 6816 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6817 | || (code == POINTER_TYPE |
3897f229 | 6818 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
3e4093b6 | 6819 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
132da1a5 | 6820 | TREE_TYPE (type))))) |
3e4093b6 RS |
6821 | { |
6822 | if (code == POINTER_TYPE) | |
b494fd98 | 6823 | { |
b494fd98 EB |
6824 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) |
6825 | { | |
f2a71bbc JM |
6826 | if (TREE_CODE (inside_init) == STRING_CST |
6827 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
c2255bc4 AH |
6828 | inside_init = array_to_pointer_conversion |
6829 | (init_loc, inside_init); | |
f2a71bbc JM |
6830 | else |
6831 | { | |
ea58ef42 | 6832 | error_init (init_loc, "invalid use of non-lvalue array"); |
f2a71bbc JM |
6833 | return error_mark_node; |
6834 | } | |
b494fd98 | 6835 | } |
f2a71bbc | 6836 | } |
b494fd98 | 6837 | |
bae39a73 NS |
6838 | if (code == VECTOR_TYPE) |
6839 | /* Although the types are compatible, we may require a | |
6840 | conversion. */ | |
6841 | inside_init = convert (type, inside_init); | |
3e4093b6 | 6842 | |
ca58211b | 6843 | if (require_constant |
3e4093b6 | 6844 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) |
400fbf9f | 6845 | { |
3e4093b6 RS |
6846 | /* As an extension, allow initializing objects with static storage |
6847 | duration with compound literals (which are then treated just as | |
ca58211b PB |
6848 | the brace enclosed list they contain). Also allow this for |
6849 | vectors, as we can only assign them with compound literals. */ | |
7278465e MP |
6850 | if (flag_isoc99 && code != VECTOR_TYPE) |
6851 | pedwarn_init (init_loc, OPT_Wpedantic, "initializer element " | |
6852 | "is not constant"); | |
3e4093b6 RS |
6853 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
6854 | inside_init = DECL_INITIAL (decl); | |
400fbf9f | 6855 | } |
3e4093b6 RS |
6856 | |
6857 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
6858 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 6859 | { |
ea58ef42 MP |
6860 | error_init (init_loc, "array initialized from non-constant array " |
6861 | "expression"); | |
3e4093b6 | 6862 | return error_mark_node; |
400fbf9f | 6863 | } |
400fbf9f | 6864 | |
c1771a20 | 6865 | /* Compound expressions can only occur here if -Wpedantic or |
3e4093b6 RS |
6866 | -pedantic-errors is specified. In the later case, we always want |
6867 | an error. In the former case, we simply want a warning. */ | |
6868 | if (require_constant && pedantic | |
6869 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
6870 | { | |
6871 | inside_init | |
6872 | = valid_compound_expr_initializer (inside_init, | |
6873 | TREE_TYPE (inside_init)); | |
6874 | if (inside_init == error_mark_node) | |
ea58ef42 | 6875 | error_init (init_loc, "initializer element is not constant"); |
2f6e4e97 | 6876 | else |
c1771a20 | 6877 | pedwarn_init (init_loc, OPT_Wpedantic, |
509c9d60 | 6878 | "initializer element is not constant"); |
3e4093b6 RS |
6879 | if (flag_pedantic_errors) |
6880 | inside_init = error_mark_node; | |
6881 | } | |
6882 | else if (require_constant | |
116df786 RH |
6883 | && !initializer_constant_valid_p (inside_init, |
6884 | TREE_TYPE (inside_init))) | |
3e4093b6 | 6885 | { |
ea58ef42 | 6886 | error_init (init_loc, "initializer element is not constant"); |
3e4093b6 | 6887 | inside_init = error_mark_node; |
8b40563c | 6888 | } |
928c19bb | 6889 | else if (require_constant && !maybe_const) |
3aa3c9fc | 6890 | pedwarn_init (init_loc, OPT_Wpedantic, |
928c19bb | 6891 | "initializer element is not a constant expression"); |
f735a153 | 6892 | |
90137d8f | 6893 | /* Added to enable additional -Wsuggest-attribute=format warnings. */ |
8fcef540 | 6894 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) |
68fca595 MP |
6895 | inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION, |
6896 | type, inside_init, origtype, | |
bbbbb16a | 6897 | ic_init, null_pointer_constant, |
928c19bb | 6898 | NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
6899 | return inside_init; |
6900 | } | |
f735a153 | 6901 | |
3e4093b6 | 6902 | /* Handle scalar types, including conversions. */ |
400fbf9f | 6903 | |
ab22c1fa CF |
6904 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE |
6905 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
6906 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
400fbf9f | 6907 | { |
f2a71bbc JM |
6908 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE |
6909 | && (TREE_CODE (init) == STRING_CST | |
6910 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
c2255bc4 | 6911 | inside_init = init = array_to_pointer_conversion (init_loc, init); |
8ce94e44 JM |
6912 | if (semantic_type) |
6913 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
6914 | inside_init); | |
3e4093b6 | 6915 | inside_init |
68fca595 MP |
6916 | = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type, |
6917 | inside_init, origtype, ic_init, | |
6918 | null_pointer_constant, NULL_TREE, NULL_TREE, | |
6919 | 0); | |
2f6e4e97 | 6920 | |
3274deff JW |
6921 | /* Check to see if we have already given an error message. */ |
6922 | if (inside_init == error_mark_node) | |
6923 | ; | |
3f75a254 | 6924 | else if (require_constant && !TREE_CONSTANT (inside_init)) |
400fbf9f | 6925 | { |
ea58ef42 | 6926 | error_init (init_loc, "initializer element is not constant"); |
3e4093b6 | 6927 | inside_init = error_mark_node; |
400fbf9f | 6928 | } |
3e4093b6 | 6929 | else if (require_constant |
116df786 RH |
6930 | && !initializer_constant_valid_p (inside_init, |
6931 | TREE_TYPE (inside_init))) | |
400fbf9f | 6932 | { |
ea58ef42 MP |
6933 | error_init (init_loc, "initializer element is not computable at " |
6934 | "load time"); | |
3e4093b6 | 6935 | inside_init = error_mark_node; |
400fbf9f | 6936 | } |
928c19bb | 6937 | else if (require_constant && !maybe_const) |
451b5e48 | 6938 | pedwarn_init (init_loc, OPT_Wpedantic, |
928c19bb | 6939 | "initializer element is not a constant expression"); |
3e4093b6 RS |
6940 | |
6941 | return inside_init; | |
400fbf9f | 6942 | } |
d9fc6069 | 6943 | |
3e4093b6 | 6944 | /* Come here only for records and arrays. */ |
d9fc6069 | 6945 | |
3e4093b6 | 6946 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
d9fc6069 | 6947 | { |
ea58ef42 | 6948 | error_init (init_loc, "variable-sized object may not be initialized"); |
3e4093b6 | 6949 | return error_mark_node; |
d9fc6069 | 6950 | } |
3e4093b6 | 6951 | |
ea58ef42 | 6952 | error_init (init_loc, "invalid initializer"); |
3e4093b6 | 6953 | return error_mark_node; |
d9fc6069 | 6954 | } |
400fbf9f | 6955 | \f |
3e4093b6 | 6956 | /* Handle initializers that use braces. */ |
400fbf9f | 6957 | |
3e4093b6 RS |
6958 | /* Type of object we are accumulating a constructor for. |
6959 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
6960 | static tree constructor_type; | |
400fbf9f | 6961 | |
3e4093b6 RS |
6962 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
6963 | left to fill. */ | |
6964 | static tree constructor_fields; | |
400fbf9f | 6965 | |
3e4093b6 RS |
6966 | /* For an ARRAY_TYPE, this is the specified index |
6967 | at which to store the next element we get. */ | |
6968 | static tree constructor_index; | |
400fbf9f | 6969 | |
3e4093b6 RS |
6970 | /* For an ARRAY_TYPE, this is the maximum index. */ |
6971 | static tree constructor_max_index; | |
400fbf9f | 6972 | |
3e4093b6 RS |
6973 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
6974 | static tree constructor_unfilled_fields; | |
400fbf9f | 6975 | |
3e4093b6 RS |
6976 | /* For an ARRAY_TYPE, this is the index of the first element |
6977 | not yet written out. */ | |
6978 | static tree constructor_unfilled_index; | |
895ea614 | 6979 | |
3e4093b6 RS |
6980 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
6981 | This is so we can generate gaps between fields, when appropriate. */ | |
6982 | static tree constructor_bit_index; | |
10d5caec | 6983 | |
3e4093b6 RS |
6984 | /* If we are saving up the elements rather than allocating them, |
6985 | this is the list of elements so far (in reverse order, | |
6986 | most recent first). */ | |
9771b263 | 6987 | static vec<constructor_elt, va_gc> *constructor_elements; |
ad47f1e5 | 6988 | |
3e4093b6 RS |
6989 | /* 1 if constructor should be incrementally stored into a constructor chain, |
6990 | 0 if all the elements should be kept in AVL tree. */ | |
6991 | static int constructor_incremental; | |
ad47f1e5 | 6992 | |
3e4093b6 RS |
6993 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
6994 | static int constructor_constant; | |
ad47f1e5 | 6995 | |
3e4093b6 RS |
6996 | /* 1 if so far this constructor's elements are all valid address constants. */ |
6997 | static int constructor_simple; | |
ad47f1e5 | 6998 | |
928c19bb JM |
6999 | /* 1 if this constructor has an element that cannot be part of a |
7000 | constant expression. */ | |
7001 | static int constructor_nonconst; | |
7002 | ||
3e4093b6 RS |
7003 | /* 1 if this constructor is erroneous so far. */ |
7004 | static int constructor_erroneous; | |
d45cf215 | 7005 | |
9bac5cbb G |
7006 | /* 1 if this constructor is the universal zero initializer { 0 }. */ |
7007 | static int constructor_zeroinit; | |
7008 | ||
3e4093b6 RS |
7009 | /* Structure for managing pending initializer elements, organized as an |
7010 | AVL tree. */ | |
d45cf215 | 7011 | |
3e4093b6 | 7012 | struct init_node |
d45cf215 | 7013 | { |
3e4093b6 RS |
7014 | struct init_node *left, *right; |
7015 | struct init_node *parent; | |
7016 | int balance; | |
7017 | tree purpose; | |
7018 | tree value; | |
bbbbb16a | 7019 | tree origtype; |
d45cf215 RS |
7020 | }; |
7021 | ||
3e4093b6 RS |
7022 | /* Tree of pending elements at this constructor level. |
7023 | These are elements encountered out of order | |
7024 | which belong at places we haven't reached yet in actually | |
7025 | writing the output. | |
7026 | Will never hold tree nodes across GC runs. */ | |
7027 | static struct init_node *constructor_pending_elts; | |
d45cf215 | 7028 | |
3e4093b6 RS |
7029 | /* The SPELLING_DEPTH of this constructor. */ |
7030 | static int constructor_depth; | |
d45cf215 | 7031 | |
3e4093b6 RS |
7032 | /* DECL node for which an initializer is being read. |
7033 | 0 means we are reading a constructor expression | |
7034 | such as (struct foo) {...}. */ | |
7035 | static tree constructor_decl; | |
d45cf215 | 7036 | |
3e4093b6 RS |
7037 | /* Nonzero if this is an initializer for a top-level decl. */ |
7038 | static int constructor_top_level; | |
d45cf215 | 7039 | |
3e4093b6 RS |
7040 | /* Nonzero if there were any member designators in this initializer. */ |
7041 | static int constructor_designated; | |
d45cf215 | 7042 | |
3e4093b6 RS |
7043 | /* Nesting depth of designator list. */ |
7044 | static int designator_depth; | |
d45cf215 | 7045 | |
3e4093b6 | 7046 | /* Nonzero if there were diagnosed errors in this designator list. */ |
b06df647 | 7047 | static int designator_erroneous; |
d45cf215 | 7048 | |
3e4093b6 RS |
7049 | \f |
7050 | /* This stack has a level for each implicit or explicit level of | |
7051 | structuring in the initializer, including the outermost one. It | |
7052 | saves the values of most of the variables above. */ | |
d45cf215 | 7053 | |
3e4093b6 RS |
7054 | struct constructor_range_stack; |
7055 | ||
7056 | struct constructor_stack | |
d45cf215 | 7057 | { |
3e4093b6 RS |
7058 | struct constructor_stack *next; |
7059 | tree type; | |
7060 | tree fields; | |
7061 | tree index; | |
7062 | tree max_index; | |
7063 | tree unfilled_index; | |
7064 | tree unfilled_fields; | |
7065 | tree bit_index; | |
9771b263 | 7066 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
7067 | struct init_node *pending_elts; |
7068 | int offset; | |
7069 | int depth; | |
916c5919 | 7070 | /* If value nonzero, this value should replace the entire |
3e4093b6 | 7071 | constructor at this level. */ |
916c5919 | 7072 | struct c_expr replacement_value; |
3e4093b6 RS |
7073 | struct constructor_range_stack *range_stack; |
7074 | char constant; | |
7075 | char simple; | |
928c19bb | 7076 | char nonconst; |
3e4093b6 RS |
7077 | char implicit; |
7078 | char erroneous; | |
7079 | char outer; | |
7080 | char incremental; | |
7081 | char designated; | |
976d5a22 | 7082 | int designator_depth; |
3e4093b6 | 7083 | }; |
d45cf215 | 7084 | |
802415d1 | 7085 | static struct constructor_stack *constructor_stack; |
d45cf215 | 7086 | |
3e4093b6 RS |
7087 | /* This stack represents designators from some range designator up to |
7088 | the last designator in the list. */ | |
d45cf215 | 7089 | |
3e4093b6 RS |
7090 | struct constructor_range_stack |
7091 | { | |
7092 | struct constructor_range_stack *next, *prev; | |
7093 | struct constructor_stack *stack; | |
7094 | tree range_start; | |
7095 | tree index; | |
7096 | tree range_end; | |
7097 | tree fields; | |
7098 | }; | |
d45cf215 | 7099 | |
802415d1 | 7100 | static struct constructor_range_stack *constructor_range_stack; |
d45cf215 | 7101 | |
3e4093b6 RS |
7102 | /* This stack records separate initializers that are nested. |
7103 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
7104 | in cases like { ... (struct foo) { ... } ... }. */ | |
d45cf215 | 7105 | |
3e4093b6 | 7106 | struct initializer_stack |
d45cf215 | 7107 | { |
3e4093b6 RS |
7108 | struct initializer_stack *next; |
7109 | tree decl; | |
3e4093b6 RS |
7110 | struct constructor_stack *constructor_stack; |
7111 | struct constructor_range_stack *constructor_range_stack; | |
9771b263 | 7112 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
7113 | struct spelling *spelling; |
7114 | struct spelling *spelling_base; | |
7115 | int spelling_size; | |
7116 | char top_level; | |
7117 | char require_constant_value; | |
7118 | char require_constant_elements; | |
7119 | }; | |
d45cf215 | 7120 | |
802415d1 | 7121 | static struct initializer_stack *initializer_stack; |
3e4093b6 RS |
7122 | \f |
7123 | /* Prepare to parse and output the initializer for variable DECL. */ | |
400fbf9f JW |
7124 | |
7125 | void | |
a396f8ae | 7126 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) |
400fbf9f | 7127 | { |
3e4093b6 | 7128 | const char *locus; |
cceb1885 | 7129 | struct initializer_stack *p = XNEW (struct initializer_stack); |
400fbf9f | 7130 | |
3e4093b6 | 7131 | p->decl = constructor_decl; |
3e4093b6 RS |
7132 | p->require_constant_value = require_constant_value; |
7133 | p->require_constant_elements = require_constant_elements; | |
7134 | p->constructor_stack = constructor_stack; | |
7135 | p->constructor_range_stack = constructor_range_stack; | |
7136 | p->elements = constructor_elements; | |
7137 | p->spelling = spelling; | |
7138 | p->spelling_base = spelling_base; | |
7139 | p->spelling_size = spelling_size; | |
7140 | p->top_level = constructor_top_level; | |
7141 | p->next = initializer_stack; | |
7142 | initializer_stack = p; | |
400fbf9f | 7143 | |
3e4093b6 | 7144 | constructor_decl = decl; |
3e4093b6 RS |
7145 | constructor_designated = 0; |
7146 | constructor_top_level = top_level; | |
400fbf9f | 7147 | |
6f17bbcf | 7148 | if (decl != 0 && decl != error_mark_node) |
3e4093b6 RS |
7149 | { |
7150 | require_constant_value = TREE_STATIC (decl); | |
7151 | require_constant_elements | |
7152 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
7153 | /* For a scalar, you can always use any value to initialize, | |
7154 | even within braces. */ | |
296a8c2f | 7155 | && AGGREGATE_TYPE_P (TREE_TYPE (decl))); |
88388a52 | 7156 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); |
3e4093b6 RS |
7157 | } |
7158 | else | |
7159 | { | |
7160 | require_constant_value = 0; | |
7161 | require_constant_elements = 0; | |
88388a52 | 7162 | locus = _("(anonymous)"); |
3e4093b6 | 7163 | } |
b71c7f8a | 7164 | |
3e4093b6 RS |
7165 | constructor_stack = 0; |
7166 | constructor_range_stack = 0; | |
b71c7f8a | 7167 | |
9bac5cbb | 7168 | found_missing_braces = 0; |
3e4093b6 RS |
7169 | |
7170 | spelling_base = 0; | |
7171 | spelling_size = 0; | |
7172 | RESTORE_SPELLING_DEPTH (0); | |
7173 | ||
7174 | if (locus) | |
7175 | push_string (locus); | |
7176 | } | |
7177 | ||
7178 | void | |
7179 | finish_init (void) | |
b71c7f8a | 7180 | { |
3e4093b6 | 7181 | struct initializer_stack *p = initializer_stack; |
b71c7f8a | 7182 | |
3e4093b6 RS |
7183 | /* Free the whole constructor stack of this initializer. */ |
7184 | while (constructor_stack) | |
7185 | { | |
7186 | struct constructor_stack *q = constructor_stack; | |
7187 | constructor_stack = q->next; | |
7188 | free (q); | |
7189 | } | |
7190 | ||
366de0ce | 7191 | gcc_assert (!constructor_range_stack); |
3e4093b6 RS |
7192 | |
7193 | /* Pop back to the data of the outer initializer (if any). */ | |
36579663 | 7194 | free (spelling_base); |
3aeb3655 | 7195 | |
3e4093b6 | 7196 | constructor_decl = p->decl; |
3e4093b6 RS |
7197 | require_constant_value = p->require_constant_value; |
7198 | require_constant_elements = p->require_constant_elements; | |
7199 | constructor_stack = p->constructor_stack; | |
7200 | constructor_range_stack = p->constructor_range_stack; | |
7201 | constructor_elements = p->elements; | |
7202 | spelling = p->spelling; | |
7203 | spelling_base = p->spelling_base; | |
7204 | spelling_size = p->spelling_size; | |
7205 | constructor_top_level = p->top_level; | |
7206 | initializer_stack = p->next; | |
7207 | free (p); | |
b71c7f8a | 7208 | } |
400fbf9f | 7209 | \f |
3e4093b6 RS |
7210 | /* Call here when we see the initializer is surrounded by braces. |
7211 | This is instead of a call to push_init_level; | |
7212 | it is matched by a call to pop_init_level. | |
400fbf9f | 7213 | |
3e4093b6 RS |
7214 | TYPE is the type to initialize, for a constructor expression. |
7215 | For an initializer for a decl, TYPE is zero. */ | |
400fbf9f | 7216 | |
3e4093b6 RS |
7217 | void |
7218 | really_start_incremental_init (tree type) | |
400fbf9f | 7219 | { |
5d038c4c | 7220 | struct constructor_stack *p = XNEW (struct constructor_stack); |
400fbf9f | 7221 | |
3e4093b6 RS |
7222 | if (type == 0) |
7223 | type = TREE_TYPE (constructor_decl); | |
400fbf9f | 7224 | |
31521951 | 7225 | if (VECTOR_TYPE_P (type) |
b6fc2cdb | 7226 | && TYPE_VECTOR_OPAQUE (type)) |
3e4093b6 | 7227 | error ("opaque vector types cannot be initialized"); |
400fbf9f | 7228 | |
3e4093b6 RS |
7229 | p->type = constructor_type; |
7230 | p->fields = constructor_fields; | |
7231 | p->index = constructor_index; | |
7232 | p->max_index = constructor_max_index; | |
7233 | p->unfilled_index = constructor_unfilled_index; | |
7234 | p->unfilled_fields = constructor_unfilled_fields; | |
7235 | p->bit_index = constructor_bit_index; | |
7236 | p->elements = constructor_elements; | |
7237 | p->constant = constructor_constant; | |
7238 | p->simple = constructor_simple; | |
928c19bb | 7239 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
7240 | p->erroneous = constructor_erroneous; |
7241 | p->pending_elts = constructor_pending_elts; | |
7242 | p->depth = constructor_depth; | |
916c5919 JM |
7243 | p->replacement_value.value = 0; |
7244 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 7245 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
7246 | p->implicit = 0; |
7247 | p->range_stack = 0; | |
7248 | p->outer = 0; | |
7249 | p->incremental = constructor_incremental; | |
7250 | p->designated = constructor_designated; | |
976d5a22 | 7251 | p->designator_depth = designator_depth; |
3e4093b6 RS |
7252 | p->next = 0; |
7253 | constructor_stack = p; | |
b13aca19 | 7254 | |
3e4093b6 RS |
7255 | constructor_constant = 1; |
7256 | constructor_simple = 1; | |
928c19bb | 7257 | constructor_nonconst = 0; |
3e4093b6 | 7258 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 7259 | constructor_elements = NULL; |
3e4093b6 RS |
7260 | constructor_pending_elts = 0; |
7261 | constructor_type = type; | |
7262 | constructor_incremental = 1; | |
7263 | constructor_designated = 0; | |
9bac5cbb | 7264 | constructor_zeroinit = 1; |
3e4093b6 | 7265 | designator_depth = 0; |
b06df647 | 7266 | designator_erroneous = 0; |
400fbf9f | 7267 | |
3e4093b6 RS |
7268 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
7269 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 7270 | { |
3e4093b6 RS |
7271 | constructor_fields = TYPE_FIELDS (constructor_type); |
7272 | /* Skip any nameless bit fields at the beginning. */ | |
7273 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
7274 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 7275 | constructor_fields = DECL_CHAIN (constructor_fields); |
05bccae2 | 7276 | |
3e4093b6 RS |
7277 | constructor_unfilled_fields = constructor_fields; |
7278 | constructor_bit_index = bitsize_zero_node; | |
400fbf9f | 7279 | } |
3e4093b6 RS |
7280 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
7281 | { | |
7282 | if (TYPE_DOMAIN (constructor_type)) | |
7283 | { | |
7284 | constructor_max_index | |
7285 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 7286 | |
3e4093b6 RS |
7287 | /* Detect non-empty initializations of zero-length arrays. */ |
7288 | if (constructor_max_index == NULL_TREE | |
7289 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 7290 | constructor_max_index = integer_minus_one_node; |
400fbf9f | 7291 | |
3e4093b6 RS |
7292 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
7293 | to initialize VLAs will cause a proper error; avoid tree | |
7294 | checking errors as well by setting a safe value. */ | |
7295 | if (constructor_max_index | |
7296 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 7297 | constructor_max_index = integer_minus_one_node; |
59c83dbf | 7298 | |
3e4093b6 RS |
7299 | constructor_index |
7300 | = convert (bitsizetype, | |
7301 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
59c83dbf | 7302 | } |
3e4093b6 | 7303 | else |
493179da JM |
7304 | { |
7305 | constructor_index = bitsize_zero_node; | |
7306 | constructor_max_index = NULL_TREE; | |
7307 | } | |
59c83dbf | 7308 | |
3e4093b6 RS |
7309 | constructor_unfilled_index = constructor_index; |
7310 | } | |
31521951 | 7311 | else if (VECTOR_TYPE_P (constructor_type)) |
3e4093b6 RS |
7312 | { |
7313 | /* Vectors are like simple fixed-size arrays. */ | |
7314 | constructor_max_index = | |
c62c040f | 7315 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
a0f0ab9f | 7316 | constructor_index = bitsize_zero_node; |
3e4093b6 RS |
7317 | constructor_unfilled_index = constructor_index; |
7318 | } | |
7319 | else | |
7320 | { | |
7321 | /* Handle the case of int x = {5}; */ | |
7322 | constructor_fields = constructor_type; | |
7323 | constructor_unfilled_fields = constructor_type; | |
7324 | } | |
7325 | } | |
7326 | \f | |
7327 | /* Push down into a subobject, for initialization. | |
7328 | If this is for an explicit set of braces, IMPLICIT is 0. | |
7329 | If it is because the next element belongs at a lower level, | |
7330 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
400fbf9f | 7331 | |
3e4093b6 | 7332 | void |
ea58ef42 MP |
7333 | push_init_level (location_t loc, int implicit, |
7334 | struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7335 | { |
7336 | struct constructor_stack *p; | |
7337 | tree value = NULL_TREE; | |
400fbf9f | 7338 | |
3e4093b6 | 7339 | /* If we've exhausted any levels that didn't have braces, |
472d98b4 JM |
7340 | pop them now. If implicit == 1, this will have been done in |
7341 | process_init_element; do not repeat it here because in the case | |
7342 | of excess initializers for an empty aggregate this leads to an | |
7343 | infinite cycle of popping a level and immediately recreating | |
7344 | it. */ | |
7345 | if (implicit != 1) | |
3e4093b6 | 7346 | { |
472d98b4 JM |
7347 | while (constructor_stack->implicit) |
7348 | { | |
7349 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7350 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7351 | && constructor_fields == 0) | |
34cf811f | 7352 | process_init_element (input_location, |
ea58ef42 | 7353 | pop_init_level (loc, 1, braced_init_obstack), |
a1e3b3d9 | 7354 | true, braced_init_obstack); |
472d98b4 JM |
7355 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE |
7356 | && constructor_max_index | |
7357 | && tree_int_cst_lt (constructor_max_index, | |
7358 | constructor_index)) | |
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 |
7363 | break; | |
7364 | } | |
3e4093b6 | 7365 | } |
400fbf9f | 7366 | |
3e4093b6 RS |
7367 | /* Unless this is an explicit brace, we need to preserve previous |
7368 | content if any. */ | |
7369 | if (implicit) | |
7370 | { | |
7371 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7372 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7373 | && constructor_fields) | |
a1e3b3d9 | 7374 | value = find_init_member (constructor_fields, braced_init_obstack); |
3e4093b6 | 7375 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
a1e3b3d9 | 7376 | value = find_init_member (constructor_index, braced_init_obstack); |
400fbf9f JW |
7377 | } |
7378 | ||
5d038c4c | 7379 | p = XNEW (struct constructor_stack); |
3e4093b6 RS |
7380 | p->type = constructor_type; |
7381 | p->fields = constructor_fields; | |
7382 | p->index = constructor_index; | |
7383 | p->max_index = constructor_max_index; | |
7384 | p->unfilled_index = constructor_unfilled_index; | |
7385 | p->unfilled_fields = constructor_unfilled_fields; | |
7386 | p->bit_index = constructor_bit_index; | |
7387 | p->elements = constructor_elements; | |
7388 | p->constant = constructor_constant; | |
7389 | p->simple = constructor_simple; | |
928c19bb | 7390 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
7391 | p->erroneous = constructor_erroneous; |
7392 | p->pending_elts = constructor_pending_elts; | |
7393 | p->depth = constructor_depth; | |
916c5919 JM |
7394 | p->replacement_value.value = 0; |
7395 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 7396 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
7397 | p->implicit = implicit; |
7398 | p->outer = 0; | |
7399 | p->incremental = constructor_incremental; | |
7400 | p->designated = constructor_designated; | |
976d5a22 | 7401 | p->designator_depth = designator_depth; |
3e4093b6 RS |
7402 | p->next = constructor_stack; |
7403 | p->range_stack = 0; | |
7404 | constructor_stack = p; | |
400fbf9f | 7405 | |
3e4093b6 RS |
7406 | constructor_constant = 1; |
7407 | constructor_simple = 1; | |
928c19bb | 7408 | constructor_nonconst = 0; |
3e4093b6 | 7409 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 7410 | constructor_elements = NULL; |
3e4093b6 RS |
7411 | constructor_incremental = 1; |
7412 | constructor_designated = 0; | |
7413 | constructor_pending_elts = 0; | |
7414 | if (!implicit) | |
400fbf9f | 7415 | { |
3e4093b6 RS |
7416 | p->range_stack = constructor_range_stack; |
7417 | constructor_range_stack = 0; | |
7418 | designator_depth = 0; | |
b06df647 | 7419 | designator_erroneous = 0; |
3e4093b6 | 7420 | } |
400fbf9f | 7421 | |
3e4093b6 RS |
7422 | /* Don't die if an entire brace-pair level is superfluous |
7423 | in the containing level. */ | |
7424 | if (constructor_type == 0) | |
7425 | ; | |
7426 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
7427 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7428 | { | |
7429 | /* Don't die if there are extra init elts at the end. */ | |
7430 | if (constructor_fields == 0) | |
7431 | constructor_type = 0; | |
7432 | else | |
400fbf9f | 7433 | { |
3e4093b6 RS |
7434 | constructor_type = TREE_TYPE (constructor_fields); |
7435 | push_member_name (constructor_fields); | |
7436 | constructor_depth++; | |
400fbf9f | 7437 | } |
6a358dcb MP |
7438 | /* If upper initializer is designated, then mark this as |
7439 | designated too to prevent bogus warnings. */ | |
7440 | constructor_designated = p->designated; | |
3e4093b6 RS |
7441 | } |
7442 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7443 | { | |
7444 | constructor_type = TREE_TYPE (constructor_type); | |
ae7e9ddd | 7445 | push_array_bounds (tree_to_uhwi (constructor_index)); |
3e4093b6 | 7446 | constructor_depth++; |
400fbf9f JW |
7447 | } |
7448 | ||
3e4093b6 | 7449 | if (constructor_type == 0) |
400fbf9f | 7450 | { |
ea58ef42 | 7451 | error_init (loc, "extra brace group at end of initializer"); |
3e4093b6 RS |
7452 | constructor_fields = 0; |
7453 | constructor_unfilled_fields = 0; | |
7454 | return; | |
400fbf9f JW |
7455 | } |
7456 | ||
3e4093b6 RS |
7457 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
7458 | { | |
7459 | constructor_constant = TREE_CONSTANT (value); | |
7460 | constructor_simple = TREE_STATIC (value); | |
928c19bb | 7461 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); |
3e4093b6 | 7462 | constructor_elements = CONSTRUCTOR_ELTS (value); |
9771b263 | 7463 | if (!vec_safe_is_empty (constructor_elements) |
3e4093b6 RS |
7464 | && (TREE_CODE (constructor_type) == RECORD_TYPE |
7465 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
a1e3b3d9 | 7466 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 7467 | } |
400fbf9f | 7468 | |
9bac5cbb G |
7469 | if (implicit == 1) |
7470 | found_missing_braces = 1; | |
400fbf9f | 7471 | |
3e4093b6 RS |
7472 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
7473 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7474 | { | |
7475 | constructor_fields = TYPE_FIELDS (constructor_type); | |
7476 | /* Skip any nameless bit fields at the beginning. */ | |
7477 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
7478 | && DECL_NAME (constructor_fields) == 0) | |
f7587ed0 | 7479 | constructor_fields = DECL_CHAIN (constructor_fields); |
103b7b17 | 7480 | |
3e4093b6 RS |
7481 | constructor_unfilled_fields = constructor_fields; |
7482 | constructor_bit_index = bitsize_zero_node; | |
7483 | } | |
31521951 | 7484 | else if (VECTOR_TYPE_P (constructor_type)) |
3e4093b6 RS |
7485 | { |
7486 | /* Vectors are like simple fixed-size arrays. */ | |
7487 | constructor_max_index = | |
c62c040f RG |
7488 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
7489 | constructor_index = bitsize_int (0); | |
3e4093b6 RS |
7490 | constructor_unfilled_index = constructor_index; |
7491 | } | |
7492 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7493 | { | |
7494 | if (TYPE_DOMAIN (constructor_type)) | |
7495 | { | |
7496 | constructor_max_index | |
7497 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 7498 | |
3e4093b6 RS |
7499 | /* Detect non-empty initializations of zero-length arrays. */ |
7500 | if (constructor_max_index == NULL_TREE | |
7501 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 7502 | constructor_max_index = integer_minus_one_node; |
de520661 | 7503 | |
3e4093b6 RS |
7504 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
7505 | to initialize VLAs will cause a proper error; avoid tree | |
7506 | checking errors as well by setting a safe value. */ | |
7507 | if (constructor_max_index | |
7508 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 7509 | constructor_max_index = integer_minus_one_node; |
b62acd60 | 7510 | |
3e4093b6 RS |
7511 | constructor_index |
7512 | = convert (bitsizetype, | |
7513 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7514 | } | |
7515 | else | |
7516 | constructor_index = bitsize_zero_node; | |
de520661 | 7517 | |
3e4093b6 RS |
7518 | constructor_unfilled_index = constructor_index; |
7519 | if (value && TREE_CODE (value) == STRING_CST) | |
7520 | { | |
7521 | /* We need to split the char/wchar array into individual | |
7522 | characters, so that we don't have to special case it | |
7523 | everywhere. */ | |
a1e3b3d9 | 7524 | set_nonincremental_init_from_string (value, braced_init_obstack); |
3e4093b6 RS |
7525 | } |
7526 | } | |
7527 | else | |
7528 | { | |
b4519d39 | 7529 | if (constructor_type != error_mark_node) |
96b40f8d | 7530 | warning_init (input_location, 0, "braces around scalar initializer"); |
3e4093b6 RS |
7531 | constructor_fields = constructor_type; |
7532 | constructor_unfilled_fields = constructor_type; | |
7533 | } | |
7534 | } | |
8b6a5902 | 7535 | |
3e4093b6 | 7536 | /* At the end of an implicit or explicit brace level, |
916c5919 JM |
7537 | finish up that level of constructor. If a single expression |
7538 | with redundant braces initialized that level, return the | |
7539 | c_expr structure for that expression. Otherwise, the original_code | |
7540 | element is set to ERROR_MARK. | |
7541 | If we were outputting the elements as they are read, return 0 as the value | |
3e4093b6 | 7542 | from inner levels (process_init_element ignores that), |
916c5919 | 7543 | but return error_mark_node as the value from the outermost level |
3e4093b6 | 7544 | (that's what we want to put in DECL_INITIAL). |
916c5919 | 7545 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
de520661 | 7546 | |
916c5919 | 7547 | struct c_expr |
ea58ef42 MP |
7548 | pop_init_level (location_t loc, int implicit, |
7549 | struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7550 | { |
7551 | struct constructor_stack *p; | |
916c5919 JM |
7552 | struct c_expr ret; |
7553 | ret.value = 0; | |
7554 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 7555 | ret.original_type = NULL; |
de520661 | 7556 | |
3e4093b6 RS |
7557 | if (implicit == 0) |
7558 | { | |
7559 | /* When we come to an explicit close brace, | |
7560 | pop any inner levels that didn't have explicit braces. */ | |
7561 | while (constructor_stack->implicit) | |
34cf811f | 7562 | process_init_element (input_location, |
ea58ef42 | 7563 | pop_init_level (loc, 1, braced_init_obstack), |
34cf811f | 7564 | true, braced_init_obstack); |
366de0ce | 7565 | gcc_assert (!constructor_range_stack); |
3e4093b6 | 7566 | } |
e5e809f4 | 7567 | |
0066ef9c RH |
7568 | /* Now output all pending elements. */ |
7569 | constructor_incremental = 1; | |
a1e3b3d9 | 7570 | output_pending_init_elements (1, braced_init_obstack); |
0066ef9c | 7571 | |
3e4093b6 | 7572 | p = constructor_stack; |
e5e809f4 | 7573 | |
3e4093b6 RS |
7574 | /* Error for initializing a flexible array member, or a zero-length |
7575 | array member in an inappropriate context. */ | |
7576 | if (constructor_type && constructor_fields | |
7577 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
7578 | && TYPE_DOMAIN (constructor_type) | |
3f75a254 | 7579 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
3e4093b6 RS |
7580 | { |
7581 | /* Silently discard empty initializations. The parser will | |
7582 | already have pedwarned for empty brackets. */ | |
7583 | if (integer_zerop (constructor_unfilled_index)) | |
7584 | constructor_type = NULL_TREE; | |
366de0ce | 7585 | else |
3e4093b6 | 7586 | { |
366de0ce | 7587 | gcc_assert (!TYPE_SIZE (constructor_type)); |
c22cacf3 | 7588 | |
3e4093b6 | 7589 | if (constructor_depth > 2) |
ea58ef42 | 7590 | error_init (loc, "initialization of flexible array member in a nested context"); |
fcf73884 | 7591 | else |
d033409e | 7592 | pedwarn_init (loc, OPT_Wpedantic, |
509c9d60 | 7593 | "initialization of a flexible array member"); |
de520661 | 7594 | |
3e4093b6 RS |
7595 | /* We have already issued an error message for the existence |
7596 | of a flexible array member not at the end of the structure. | |
535a42b1 | 7597 | Discard the initializer so that we do not die later. */ |
910ad8de | 7598 | if (DECL_CHAIN (constructor_fields) != NULL_TREE) |
3e4093b6 RS |
7599 | constructor_type = NULL_TREE; |
7600 | } | |
3e4093b6 | 7601 | } |
de520661 | 7602 | |
7b33f0c8 MP |
7603 | switch (vec_safe_length (constructor_elements)) |
7604 | { | |
7605 | case 0: | |
7606 | /* Initialization with { } counts as zeroinit. */ | |
7607 | constructor_zeroinit = 1; | |
7608 | break; | |
7609 | case 1: | |
7610 | /* This might be zeroinit as well. */ | |
7611 | if (integer_zerop ((*constructor_elements)[0].value)) | |
7612 | constructor_zeroinit = 1; | |
7613 | break; | |
7614 | default: | |
7615 | /* If the constructor has more than one element, it can't be { 0 }. */ | |
7616 | constructor_zeroinit = 0; | |
7617 | break; | |
7618 | } | |
9bac5cbb G |
7619 | |
7620 | /* Warn when some structs are initialized with direct aggregation. */ | |
7621 | if (!implicit && found_missing_braces && warn_missing_braces | |
7622 | && !constructor_zeroinit) | |
7b33f0c8 MP |
7623 | warning_init (loc, OPT_Wmissing_braces, |
7624 | "missing braces around initializer"); | |
9bac5cbb | 7625 | |
3e4093b6 | 7626 | /* Warn when some struct elements are implicitly initialized to zero. */ |
eaac4679 | 7627 | if (warn_missing_field_initializers |
3e4093b6 RS |
7628 | && constructor_type |
7629 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
7630 | && constructor_unfilled_fields) | |
7631 | { | |
7632 | /* Do not warn for flexible array members or zero-length arrays. */ | |
7633 | while (constructor_unfilled_fields | |
3f75a254 | 7634 | && (!DECL_SIZE (constructor_unfilled_fields) |
3e4093b6 | 7635 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) |
910ad8de | 7636 | constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); |
cc77d4d5 | 7637 | |
49819fef AM |
7638 | if (constructor_unfilled_fields |
7639 | /* Do not warn if this level of the initializer uses member | |
7640 | designators; it is likely to be deliberate. */ | |
7641 | && !constructor_designated | |
84937de2 | 7642 | /* Do not warn about initializing with { 0 } or with { }. */ |
49819fef | 7643 | && !constructor_zeroinit) |
3e4093b6 | 7644 | { |
32397f22 MLI |
7645 | if (warning_at (input_location, OPT_Wmissing_field_initializers, |
7646 | "missing initializer for field %qD of %qT", | |
7647 | constructor_unfilled_fields, | |
7648 | constructor_type)) | |
7649 | inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), | |
ae933128 | 7650 | "%qD declared here", constructor_unfilled_fields); |
3e4093b6 RS |
7651 | } |
7652 | } | |
de520661 | 7653 | |
3e4093b6 | 7654 | /* Pad out the end of the structure. */ |
916c5919 | 7655 | if (p->replacement_value.value) |
3e4093b6 RS |
7656 | /* If this closes a superfluous brace pair, |
7657 | just pass out the element between them. */ | |
916c5919 | 7658 | ret = p->replacement_value; |
3e4093b6 RS |
7659 | else if (constructor_type == 0) |
7660 | ; | |
7661 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7662 | && TREE_CODE (constructor_type) != UNION_TYPE | |
7663 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
31521951 | 7664 | && !VECTOR_TYPE_P (constructor_type)) |
3e4093b6 RS |
7665 | { |
7666 | /* A nonincremental scalar initializer--just return | |
7667 | the element, after verifying there is just one. */ | |
9771b263 | 7668 | if (vec_safe_is_empty (constructor_elements)) |
3e4093b6 RS |
7669 | { |
7670 | if (!constructor_erroneous) | |
ea58ef42 | 7671 | error_init (loc, "empty scalar initializer"); |
916c5919 | 7672 | ret.value = error_mark_node; |
3e4093b6 | 7673 | } |
9771b263 | 7674 | else if (vec_safe_length (constructor_elements) != 1) |
3e4093b6 | 7675 | { |
ea58ef42 | 7676 | error_init (loc, "extra elements in scalar initializer"); |
9771b263 | 7677 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7678 | } |
7679 | else | |
9771b263 | 7680 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7681 | } |
7682 | else | |
7683 | { | |
7684 | if (constructor_erroneous) | |
916c5919 | 7685 | ret.value = error_mark_node; |
3e4093b6 RS |
7686 | else |
7687 | { | |
916c5919 | 7688 | ret.value = build_constructor (constructor_type, |
4038c495 | 7689 | constructor_elements); |
3e4093b6 | 7690 | if (constructor_constant) |
51eed280 | 7691 | TREE_CONSTANT (ret.value) = 1; |
3e4093b6 | 7692 | if (constructor_constant && constructor_simple) |
916c5919 | 7693 | TREE_STATIC (ret.value) = 1; |
928c19bb JM |
7694 | if (constructor_nonconst) |
7695 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
3e4093b6 RS |
7696 | } |
7697 | } | |
de520661 | 7698 | |
928c19bb JM |
7699 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) |
7700 | { | |
7701 | if (constructor_nonconst) | |
7702 | ret.original_code = C_MAYBE_CONST_EXPR; | |
7703 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
7704 | ret.original_code = ERROR_MARK; | |
7705 | } | |
7706 | ||
3e4093b6 RS |
7707 | constructor_type = p->type; |
7708 | constructor_fields = p->fields; | |
7709 | constructor_index = p->index; | |
7710 | constructor_max_index = p->max_index; | |
7711 | constructor_unfilled_index = p->unfilled_index; | |
7712 | constructor_unfilled_fields = p->unfilled_fields; | |
7713 | constructor_bit_index = p->bit_index; | |
7714 | constructor_elements = p->elements; | |
7715 | constructor_constant = p->constant; | |
7716 | constructor_simple = p->simple; | |
928c19bb | 7717 | constructor_nonconst = p->nonconst; |
3e4093b6 RS |
7718 | constructor_erroneous = p->erroneous; |
7719 | constructor_incremental = p->incremental; | |
7720 | constructor_designated = p->designated; | |
976d5a22 | 7721 | designator_depth = p->designator_depth; |
3e4093b6 RS |
7722 | constructor_pending_elts = p->pending_elts; |
7723 | constructor_depth = p->depth; | |
7724 | if (!p->implicit) | |
7725 | constructor_range_stack = p->range_stack; | |
7726 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
de520661 | 7727 | |
3e4093b6 RS |
7728 | constructor_stack = p->next; |
7729 | free (p); | |
b621a4dd | 7730 | |
5d5e98dc VR |
7731 | if (ret.value == 0 && constructor_stack == 0) |
7732 | ret.value = error_mark_node; | |
916c5919 | 7733 | return ret; |
3e4093b6 | 7734 | } |
8b6a5902 | 7735 | |
3e4093b6 RS |
7736 | /* Common handling for both array range and field name designators. |
7737 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
400fbf9f | 7738 | |
3e4093b6 | 7739 | static int |
ea58ef42 MP |
7740 | set_designator (location_t loc, int array, |
7741 | struct obstack *braced_init_obstack) | |
de520661 | 7742 | { |
3e4093b6 RS |
7743 | tree subtype; |
7744 | enum tree_code subcode; | |
de520661 | 7745 | |
3e4093b6 RS |
7746 | /* Don't die if an entire brace-pair level is superfluous |
7747 | in the containing level. */ | |
7748 | if (constructor_type == 0) | |
7749 | return 1; | |
de520661 | 7750 | |
366de0ce NS |
7751 | /* If there were errors in this designator list already, bail out |
7752 | silently. */ | |
b06df647 | 7753 | if (designator_erroneous) |
3e4093b6 | 7754 | return 1; |
e28cae4f | 7755 | |
3e4093b6 RS |
7756 | if (!designator_depth) |
7757 | { | |
366de0ce | 7758 | gcc_assert (!constructor_range_stack); |
de520661 | 7759 | |
3e4093b6 RS |
7760 | /* Designator list starts at the level of closest explicit |
7761 | braces. */ | |
7762 | while (constructor_stack->implicit) | |
34cf811f | 7763 | process_init_element (input_location, |
ea58ef42 | 7764 | pop_init_level (loc, 1, braced_init_obstack), |
34cf811f | 7765 | true, braced_init_obstack); |
3e4093b6 RS |
7766 | constructor_designated = 1; |
7767 | return 0; | |
7768 | } | |
de520661 | 7769 | |
366de0ce | 7770 | switch (TREE_CODE (constructor_type)) |
3c3fa147 | 7771 | { |
366de0ce NS |
7772 | case RECORD_TYPE: |
7773 | case UNION_TYPE: | |
3e4093b6 RS |
7774 | subtype = TREE_TYPE (constructor_fields); |
7775 | if (subtype != error_mark_node) | |
7776 | subtype = TYPE_MAIN_VARIANT (subtype); | |
366de0ce NS |
7777 | break; |
7778 | case ARRAY_TYPE: | |
3e4093b6 | 7779 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
366de0ce NS |
7780 | break; |
7781 | default: | |
7782 | gcc_unreachable (); | |
de520661 | 7783 | } |
400fbf9f | 7784 | |
3e4093b6 RS |
7785 | subcode = TREE_CODE (subtype); |
7786 | if (array && subcode != ARRAY_TYPE) | |
7787 | { | |
ea58ef42 | 7788 | error_init (loc, "array index in non-array initializer"); |
3e4093b6 RS |
7789 | return 1; |
7790 | } | |
7791 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
7792 | { | |
ea58ef42 | 7793 | error_init (loc, "field name not in record or union initializer"); |
3e4093b6 RS |
7794 | return 1; |
7795 | } | |
d45cf215 | 7796 | |
3e4093b6 | 7797 | constructor_designated = 1; |
ea58ef42 | 7798 | push_init_level (loc, 2, braced_init_obstack); |
3e4093b6 | 7799 | return 0; |
de520661 | 7800 | } |
400fbf9f | 7801 | |
3e4093b6 RS |
7802 | /* If there are range designators in designator list, push a new designator |
7803 | to constructor_range_stack. RANGE_END is end of such stack range or | |
7804 | NULL_TREE if there is no range designator at this level. */ | |
400fbf9f | 7805 | |
3e4093b6 | 7806 | static void |
a1e3b3d9 | 7807 | push_range_stack (tree range_end, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7808 | { |
7809 | struct constructor_range_stack *p; | |
400fbf9f | 7810 | |
a1e3b3d9 LB |
7811 | p = (struct constructor_range_stack *) |
7812 | obstack_alloc (braced_init_obstack, | |
7813 | sizeof (struct constructor_range_stack)); | |
3e4093b6 RS |
7814 | p->prev = constructor_range_stack; |
7815 | p->next = 0; | |
7816 | p->fields = constructor_fields; | |
7817 | p->range_start = constructor_index; | |
7818 | p->index = constructor_index; | |
7819 | p->stack = constructor_stack; | |
7820 | p->range_end = range_end; | |
8b6a5902 | 7821 | if (constructor_range_stack) |
3e4093b6 RS |
7822 | constructor_range_stack->next = p; |
7823 | constructor_range_stack = p; | |
de520661 | 7824 | } |
400fbf9f | 7825 | |
3e4093b6 RS |
7826 | /* Within an array initializer, specify the next index to be initialized. |
7827 | FIRST is that index. If LAST is nonzero, then initialize a range | |
7828 | of indices, running from FIRST through LAST. */ | |
5a7ec9d9 | 7829 | |
de520661 | 7830 | void |
ea58ef42 | 7831 | set_init_index (location_t loc, tree first, tree last, |
d033409e | 7832 | struct obstack *braced_init_obstack) |
de520661 | 7833 | { |
ea58ef42 | 7834 | if (set_designator (loc, 1, braced_init_obstack)) |
3e4093b6 | 7835 | return; |
de520661 | 7836 | |
b06df647 | 7837 | designator_erroneous = 1; |
de520661 | 7838 | |
3ea8cd06 JM |
7839 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
7840 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
7841 | { | |
ea58ef42 | 7842 | error_init (loc, "array index in initializer not of integer type"); |
3ea8cd06 JM |
7843 | return; |
7844 | } | |
7845 | ||
2b6da65c JM |
7846 | if (TREE_CODE (first) != INTEGER_CST) |
7847 | { | |
7848 | first = c_fully_fold (first, false, NULL); | |
7849 | if (TREE_CODE (first) == INTEGER_CST) | |
d033409e | 7850 | pedwarn_init (loc, OPT_Wpedantic, |
2b6da65c JM |
7851 | "array index in initializer is not " |
7852 | "an integer constant expression"); | |
7853 | } | |
7854 | ||
7855 | if (last && TREE_CODE (last) != INTEGER_CST) | |
7856 | { | |
7857 | last = c_fully_fold (last, false, NULL); | |
7858 | if (TREE_CODE (last) == INTEGER_CST) | |
d033409e | 7859 | pedwarn_init (loc, OPT_Wpedantic, |
2b6da65c JM |
7860 | "array index in initializer is not " |
7861 | "an integer constant expression"); | |
7862 | } | |
7863 | ||
3e4093b6 | 7864 | if (TREE_CODE (first) != INTEGER_CST) |
ea58ef42 | 7865 | error_init (loc, "nonconstant array index in initializer"); |
3e4093b6 | 7866 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) |
ea58ef42 | 7867 | error_init (loc, "nonconstant array index in initializer"); |
3e4093b6 | 7868 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) |
ea58ef42 | 7869 | error_init (loc, "array index in non-array initializer"); |
622adc7e | 7870 | else if (tree_int_cst_sgn (first) == -1) |
ea58ef42 | 7871 | error_init (loc, "array index in initializer exceeds array bounds"); |
3e4093b6 RS |
7872 | else if (constructor_max_index |
7873 | && tree_int_cst_lt (constructor_max_index, first)) | |
ea58ef42 | 7874 | error_init (loc, "array index in initializer exceeds array bounds"); |
3e4093b6 | 7875 | else |
de520661 | 7876 | { |
928c19bb JM |
7877 | constant_expression_warning (first); |
7878 | if (last) | |
7879 | constant_expression_warning (last); | |
3e4093b6 | 7880 | constructor_index = convert (bitsizetype, first); |
40d3d530 JR |
7881 | if (tree_int_cst_lt (constructor_index, first)) |
7882 | { | |
7883 | constructor_index = copy_node (constructor_index); | |
7884 | TREE_OVERFLOW (constructor_index) = 1; | |
7885 | } | |
665f2503 | 7886 | |
3e4093b6 | 7887 | if (last) |
2bede729 | 7888 | { |
3e4093b6 RS |
7889 | if (tree_int_cst_equal (first, last)) |
7890 | last = 0; | |
7891 | else if (tree_int_cst_lt (last, first)) | |
7892 | { | |
ea58ef42 | 7893 | error_init (loc, "empty index range in initializer"); |
3e4093b6 RS |
7894 | last = 0; |
7895 | } | |
7896 | else | |
7897 | { | |
7898 | last = convert (bitsizetype, last); | |
7899 | if (constructor_max_index != 0 | |
7900 | && tree_int_cst_lt (constructor_max_index, last)) | |
7901 | { | |
ea58ef42 MP |
7902 | error_init (loc, "array index range in initializer exceeds " |
7903 | "array bounds"); | |
3e4093b6 RS |
7904 | last = 0; |
7905 | } | |
7906 | } | |
2bede729 | 7907 | } |
fed3cef0 | 7908 | |
3e4093b6 | 7909 | designator_depth++; |
b06df647 | 7910 | designator_erroneous = 0; |
3e4093b6 | 7911 | if (constructor_range_stack || last) |
a1e3b3d9 | 7912 | push_range_stack (last, braced_init_obstack); |
de520661 | 7913 | } |
de520661 | 7914 | } |
3e4093b6 RS |
7915 | |
7916 | /* Within a struct initializer, specify the next field to be initialized. */ | |
400fbf9f | 7917 | |
de520661 | 7918 | void |
ea58ef42 MP |
7919 | set_init_label (location_t loc, tree fieldname, |
7920 | struct obstack *braced_init_obstack) | |
de520661 | 7921 | { |
0fb96aa4 | 7922 | tree field; |
94ba5069 | 7923 | |
ea58ef42 | 7924 | if (set_designator (loc, 0, braced_init_obstack)) |
3e4093b6 RS |
7925 | return; |
7926 | ||
b06df647 | 7927 | designator_erroneous = 1; |
3e4093b6 RS |
7928 | |
7929 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7930 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
94ba5069 | 7931 | { |
ea58ef42 | 7932 | error_init (loc, "field name not in record or union initializer"); |
3e4093b6 | 7933 | return; |
94ba5069 RS |
7934 | } |
7935 | ||
0fb96aa4 | 7936 | field = lookup_field (constructor_type, fieldname); |
8b6a5902 | 7937 | |
0fb96aa4 | 7938 | if (field == 0) |
9babc352 | 7939 | error_at (loc, "unknown field %qE specified in initializer", fieldname); |
3e4093b6 | 7940 | else |
0fb96aa4 JM |
7941 | do |
7942 | { | |
7943 | constructor_fields = TREE_VALUE (field); | |
7944 | designator_depth++; | |
7945 | designator_erroneous = 0; | |
7946 | if (constructor_range_stack) | |
7947 | push_range_stack (NULL_TREE, braced_init_obstack); | |
7948 | field = TREE_CHAIN (field); | |
7949 | if (field) | |
7950 | { | |
ea58ef42 | 7951 | if (set_designator (loc, 0, braced_init_obstack)) |
0fb96aa4 JM |
7952 | return; |
7953 | } | |
7954 | } | |
7955 | while (field != NULL_TREE); | |
3e4093b6 RS |
7956 | } |
7957 | \f | |
7958 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
7959 | identifies the initializer, either array index or field in a structure. | |
bbbbb16a ILT |
7960 | VALUE is the value of that index or field. If ORIGTYPE is not |
7961 | NULL_TREE, it is the original type of VALUE. | |
b295aee2 JJ |
7962 | |
7963 | IMPLICIT is true if value comes from pop_init_level (1), | |
7964 | the new initializer has been merged with the existing one | |
7965 | and thus no warnings should be emitted about overriding an | |
7966 | existing initializer. */ | |
de520661 | 7967 | |
3e4093b6 | 7968 | static void |
96b40f8d MP |
7969 | add_pending_init (location_t loc, tree purpose, tree value, tree origtype, |
7970 | bool implicit, struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7971 | { |
7972 | struct init_node *p, **q, *r; | |
7973 | ||
7974 | q = &constructor_pending_elts; | |
7975 | p = 0; | |
7976 | ||
7977 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 7978 | { |
3e4093b6 | 7979 | while (*q != 0) |
91fa3c30 | 7980 | { |
3e4093b6 RS |
7981 | p = *q; |
7982 | if (tree_int_cst_lt (purpose, p->purpose)) | |
7983 | q = &p->left; | |
7984 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
7985 | q = &p->right; | |
7986 | else | |
7987 | { | |
b295aee2 JJ |
7988 | if (!implicit) |
7989 | { | |
7990 | if (TREE_SIDE_EFFECTS (p->value)) | |
755e528f | 7991 | warning_init (loc, OPT_Woverride_init_side_effects, |
96b40f8d MP |
7992 | "initialized field with side-effects " |
7993 | "overwritten"); | |
b295aee2 | 7994 | else if (warn_override_init) |
96b40f8d MP |
7995 | warning_init (loc, OPT_Woverride_init, |
7996 | "initialized field overwritten"); | |
b295aee2 | 7997 | } |
3e4093b6 | 7998 | p->value = value; |
bbbbb16a | 7999 | p->origtype = origtype; |
3e4093b6 RS |
8000 | return; |
8001 | } | |
91fa3c30 | 8002 | } |
de520661 | 8003 | } |
3e4093b6 | 8004 | else |
de520661 | 8005 | { |
3e4093b6 | 8006 | tree bitpos; |
400fbf9f | 8007 | |
3e4093b6 RS |
8008 | bitpos = bit_position (purpose); |
8009 | while (*q != NULL) | |
8010 | { | |
8011 | p = *q; | |
8012 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
8013 | q = &p->left; | |
8014 | else if (p->purpose != purpose) | |
8015 | q = &p->right; | |
8016 | else | |
8017 | { | |
b295aee2 JJ |
8018 | if (!implicit) |
8019 | { | |
8020 | if (TREE_SIDE_EFFECTS (p->value)) | |
755e528f | 8021 | warning_init (loc, OPT_Woverride_init_side_effects, |
96b40f8d MP |
8022 | "initialized field with side-effects " |
8023 | "overwritten"); | |
b295aee2 | 8024 | else if (warn_override_init) |
96b40f8d MP |
8025 | warning_init (loc, OPT_Woverride_init, |
8026 | "initialized field overwritten"); | |
b295aee2 | 8027 | } |
3e4093b6 | 8028 | p->value = value; |
bbbbb16a | 8029 | p->origtype = origtype; |
3e4093b6 RS |
8030 | return; |
8031 | } | |
8032 | } | |
91fa3c30 | 8033 | } |
b71c7f8a | 8034 | |
a1e3b3d9 LB |
8035 | r = (struct init_node *) obstack_alloc (braced_init_obstack, |
8036 | sizeof (struct init_node)); | |
3e4093b6 RS |
8037 | r->purpose = purpose; |
8038 | r->value = value; | |
bbbbb16a | 8039 | r->origtype = origtype; |
8b6a5902 | 8040 | |
3e4093b6 RS |
8041 | *q = r; |
8042 | r->parent = p; | |
8043 | r->left = 0; | |
8044 | r->right = 0; | |
8045 | r->balance = 0; | |
b71c7f8a | 8046 | |
3e4093b6 | 8047 | while (p) |
de520661 | 8048 | { |
3e4093b6 | 8049 | struct init_node *s; |
665f2503 | 8050 | |
3e4093b6 | 8051 | if (r == p->left) |
2bede729 | 8052 | { |
3e4093b6 RS |
8053 | if (p->balance == 0) |
8054 | p->balance = -1; | |
8055 | else if (p->balance < 0) | |
8056 | { | |
8057 | if (r->balance < 0) | |
8058 | { | |
8059 | /* L rotation. */ | |
8060 | p->left = r->right; | |
8061 | if (p->left) | |
8062 | p->left->parent = p; | |
8063 | r->right = p; | |
e7b6a0ee | 8064 | |
3e4093b6 RS |
8065 | p->balance = 0; |
8066 | r->balance = 0; | |
39bc99c2 | 8067 | |
3e4093b6 RS |
8068 | s = p->parent; |
8069 | p->parent = r; | |
8070 | r->parent = s; | |
8071 | if (s) | |
8072 | { | |
8073 | if (s->left == p) | |
8074 | s->left = r; | |
8075 | else | |
8076 | s->right = r; | |
8077 | } | |
8078 | else | |
8079 | constructor_pending_elts = r; | |
8080 | } | |
8081 | else | |
8082 | { | |
8083 | /* LR rotation. */ | |
8084 | struct init_node *t = r->right; | |
e7b6a0ee | 8085 | |
3e4093b6 RS |
8086 | r->right = t->left; |
8087 | if (r->right) | |
8088 | r->right->parent = r; | |
8089 | t->left = r; | |
8090 | ||
8091 | p->left = t->right; | |
8092 | if (p->left) | |
8093 | p->left->parent = p; | |
8094 | t->right = p; | |
8095 | ||
8096 | p->balance = t->balance < 0; | |
8097 | r->balance = -(t->balance > 0); | |
8098 | t->balance = 0; | |
8099 | ||
8100 | s = p->parent; | |
8101 | p->parent = t; | |
8102 | r->parent = t; | |
8103 | t->parent = s; | |
8104 | if (s) | |
8105 | { | |
8106 | if (s->left == p) | |
8107 | s->left = t; | |
8108 | else | |
8109 | s->right = t; | |
8110 | } | |
8111 | else | |
8112 | constructor_pending_elts = t; | |
8113 | } | |
8114 | break; | |
8115 | } | |
8116 | else | |
8117 | { | |
8118 | /* p->balance == +1; growth of left side balances the node. */ | |
8119 | p->balance = 0; | |
8120 | break; | |
8121 | } | |
2bede729 | 8122 | } |
3e4093b6 RS |
8123 | else /* r == p->right */ |
8124 | { | |
8125 | if (p->balance == 0) | |
8126 | /* Growth propagation from right side. */ | |
8127 | p->balance++; | |
8128 | else if (p->balance > 0) | |
8129 | { | |
8130 | if (r->balance > 0) | |
8131 | { | |
8132 | /* R rotation. */ | |
8133 | p->right = r->left; | |
8134 | if (p->right) | |
8135 | p->right->parent = p; | |
8136 | r->left = p; | |
8137 | ||
8138 | p->balance = 0; | |
8139 | r->balance = 0; | |
8140 | ||
8141 | s = p->parent; | |
8142 | p->parent = r; | |
8143 | r->parent = s; | |
8144 | if (s) | |
8145 | { | |
8146 | if (s->left == p) | |
8147 | s->left = r; | |
8148 | else | |
8149 | s->right = r; | |
8150 | } | |
8151 | else | |
8152 | constructor_pending_elts = r; | |
8153 | } | |
8154 | else /* r->balance == -1 */ | |
8155 | { | |
8156 | /* RL rotation */ | |
8157 | struct init_node *t = r->left; | |
8158 | ||
8159 | r->left = t->right; | |
8160 | if (r->left) | |
8161 | r->left->parent = r; | |
8162 | t->right = r; | |
8163 | ||
8164 | p->right = t->left; | |
8165 | if (p->right) | |
8166 | p->right->parent = p; | |
8167 | t->left = p; | |
8168 | ||
8169 | r->balance = (t->balance < 0); | |
8170 | p->balance = -(t->balance > 0); | |
8171 | t->balance = 0; | |
8172 | ||
8173 | s = p->parent; | |
8174 | p->parent = t; | |
8175 | r->parent = t; | |
8176 | t->parent = s; | |
8177 | if (s) | |
8178 | { | |
8179 | if (s->left == p) | |
8180 | s->left = t; | |
8181 | else | |
8182 | s->right = t; | |
8183 | } | |
8184 | else | |
8185 | constructor_pending_elts = t; | |
8186 | } | |
8187 | break; | |
8188 | } | |
8189 | else | |
8190 | { | |
8191 | /* p->balance == -1; growth of right side balances the node. */ | |
8192 | p->balance = 0; | |
8193 | break; | |
8194 | } | |
8195 | } | |
8196 | ||
8197 | r = p; | |
8198 | p = p->parent; | |
8199 | } | |
8200 | } | |
8201 | ||
8202 | /* Build AVL tree from a sorted chain. */ | |
8203 | ||
8204 | static void | |
a1e3b3d9 | 8205 | set_nonincremental_init (struct obstack * braced_init_obstack) |
3e4093b6 | 8206 | { |
4038c495 GB |
8207 | unsigned HOST_WIDE_INT ix; |
8208 | tree index, value; | |
3e4093b6 RS |
8209 | |
8210 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
8211 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
8212 | return; | |
8213 | ||
4038c495 | 8214 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) |
96b40f8d MP |
8215 | add_pending_init (input_location, index, value, NULL_TREE, true, |
8216 | braced_init_obstack); | |
9771b263 | 8217 | constructor_elements = NULL; |
3e4093b6 RS |
8218 | if (TREE_CODE (constructor_type) == RECORD_TYPE) |
8219 | { | |
8220 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
8221 | /* Skip any nameless bit fields at the beginning. */ | |
8222 | while (constructor_unfilled_fields != 0 | |
8223 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8224 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8225 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
fed3cef0 | 8226 | |
de520661 | 8227 | } |
3e4093b6 | 8228 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 8229 | { |
3e4093b6 RS |
8230 | if (TYPE_DOMAIN (constructor_type)) |
8231 | constructor_unfilled_index | |
8232 | = convert (bitsizetype, | |
8233 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
8234 | else | |
8235 | constructor_unfilled_index = bitsize_zero_node; | |
de520661 | 8236 | } |
3e4093b6 | 8237 | constructor_incremental = 0; |
de520661 | 8238 | } |
400fbf9f | 8239 | |
3e4093b6 | 8240 | /* Build AVL tree from a string constant. */ |
de520661 | 8241 | |
3e4093b6 | 8242 | static void |
a1e3b3d9 LB |
8243 | set_nonincremental_init_from_string (tree str, |
8244 | struct obstack * braced_init_obstack) | |
de520661 | 8245 | { |
3e4093b6 RS |
8246 | tree value, purpose, type; |
8247 | HOST_WIDE_INT val[2]; | |
8248 | const char *p, *end; | |
8249 | int byte, wchar_bytes, charwidth, bitpos; | |
de520661 | 8250 | |
366de0ce | 8251 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); |
940ff66d | 8252 | |
c466b2cd | 8253 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; |
3e4093b6 RS |
8254 | charwidth = TYPE_PRECISION (char_type_node); |
8255 | type = TREE_TYPE (constructor_type); | |
8256 | p = TREE_STRING_POINTER (str); | |
8257 | end = p + TREE_STRING_LENGTH (str); | |
91fa3c30 | 8258 | |
3e4093b6 | 8259 | for (purpose = bitsize_zero_node; |
8824edff JJ |
8260 | p < end |
8261 | && !(constructor_max_index | |
8262 | && tree_int_cst_lt (constructor_max_index, purpose)); | |
3e4093b6 | 8263 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) |
584ef5fe | 8264 | { |
3e4093b6 | 8265 | if (wchar_bytes == 1) |
ffc5c6a9 | 8266 | { |
807e902e KZ |
8267 | val[0] = (unsigned char) *p++; |
8268 | val[1] = 0; | |
ffc5c6a9 RH |
8269 | } |
8270 | else | |
3e4093b6 | 8271 | { |
3e4093b6 | 8272 | val[1] = 0; |
807e902e | 8273 | val[0] = 0; |
3e4093b6 RS |
8274 | for (byte = 0; byte < wchar_bytes; byte++) |
8275 | { | |
8276 | if (BYTES_BIG_ENDIAN) | |
8277 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
8278 | else | |
8279 | bitpos = byte * charwidth; | |
807e902e | 8280 | val[bitpos % HOST_BITS_PER_WIDE_INT] |
3e4093b6 RS |
8281 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) |
8282 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
8283 | } | |
8284 | } | |
584ef5fe | 8285 | |
8df83eae | 8286 | if (!TYPE_UNSIGNED (type)) |
3e4093b6 RS |
8287 | { |
8288 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
8289 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
8290 | { | |
807e902e | 8291 | if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) |
3e4093b6 | 8292 | { |
aa256c4a | 8293 | val[0] |= HOST_WIDE_INT_M1U << bitpos; |
807e902e | 8294 | val[1] = -1; |
3e4093b6 RS |
8295 | } |
8296 | } | |
8297 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
8298 | { | |
807e902e KZ |
8299 | if (val[0] < 0) |
8300 | val[1] = -1; | |
3e4093b6 | 8301 | } |
807e902e | 8302 | else if (val[1] & (((HOST_WIDE_INT) 1) |
3e4093b6 | 8303 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) |
aa256c4a | 8304 | val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT); |
3e4093b6 | 8305 | } |
ffc5c6a9 | 8306 | |
807e902e KZ |
8307 | value = wide_int_to_tree (type, |
8308 | wide_int::from_array (val, 2, | |
8309 | HOST_BITS_PER_WIDE_INT * 2)); | |
96b40f8d | 8310 | add_pending_init (input_location, purpose, value, NULL_TREE, true, |
a1e3b3d9 | 8311 | braced_init_obstack); |
9dfcc8db BH |
8312 | } |
8313 | ||
3e4093b6 RS |
8314 | constructor_incremental = 0; |
8315 | } | |
de520661 | 8316 | |
3e4093b6 RS |
8317 | /* Return value of FIELD in pending initializer or zero if the field was |
8318 | not initialized yet. */ | |
8319 | ||
8320 | static tree | |
a1e3b3d9 | 8321 | find_init_member (tree field, struct obstack * braced_init_obstack) |
3e4093b6 RS |
8322 | { |
8323 | struct init_node *p; | |
8324 | ||
8325 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
19d76e60 | 8326 | { |
3e4093b6 RS |
8327 | if (constructor_incremental |
8328 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 8329 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8330 | |
8331 | p = constructor_pending_elts; | |
8332 | while (p) | |
19d76e60 | 8333 | { |
3e4093b6 RS |
8334 | if (tree_int_cst_lt (field, p->purpose)) |
8335 | p = p->left; | |
8336 | else if (tree_int_cst_lt (p->purpose, field)) | |
8337 | p = p->right; | |
8338 | else | |
8339 | return p->value; | |
19d76e60 | 8340 | } |
19d76e60 | 8341 | } |
3e4093b6 | 8342 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
de520661 | 8343 | { |
3e4093b6 | 8344 | tree bitpos = bit_position (field); |
de520661 | 8345 | |
3e4093b6 RS |
8346 | if (constructor_incremental |
8347 | && (!constructor_unfilled_fields | |
8348 | || tree_int_cst_lt (bitpos, | |
8349 | bit_position (constructor_unfilled_fields)))) | |
a1e3b3d9 | 8350 | set_nonincremental_init (braced_init_obstack); |
de520661 | 8351 | |
3e4093b6 RS |
8352 | p = constructor_pending_elts; |
8353 | while (p) | |
8354 | { | |
8355 | if (field == p->purpose) | |
8356 | return p->value; | |
8357 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
8358 | p = p->left; | |
8359 | else | |
8360 | p = p->right; | |
8361 | } | |
8362 | } | |
8363 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 8364 | { |
9771b263 DN |
8365 | if (!vec_safe_is_empty (constructor_elements) |
8366 | && (constructor_elements->last ().index == field)) | |
8367 | return constructor_elements->last ().value; | |
de520661 | 8368 | } |
3e4093b6 | 8369 | return 0; |
de520661 RS |
8370 | } |
8371 | ||
3e4093b6 RS |
8372 | /* "Output" the next constructor element. |
8373 | At top level, really output it to assembler code now. | |
8374 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
bbbbb16a | 8375 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. |
3e4093b6 RS |
8376 | TYPE is the data type that the containing data type wants here. |
8377 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
916c5919 JM |
8378 | If VALUE is a string constant, STRICT_STRING is true if it is |
8379 | unparenthesized or we should not warn here for it being parenthesized. | |
8380 | For other types of VALUE, STRICT_STRING is not used. | |
8b6a5902 | 8381 | |
3e4093b6 RS |
8382 | PENDING if non-nil means output pending elements that belong |
8383 | right after this element. (PENDING is normally 1; | |
b295aee2 JJ |
8384 | it is 0 while outputting pending elements, to avoid recursion.) |
8385 | ||
8386 | IMPLICIT is true if value comes from pop_init_level (1), | |
8387 | the new initializer has been merged with the existing one | |
8388 | and thus no warnings should be emitted about overriding an | |
8389 | existing initializer. */ | |
8b6a5902 | 8390 | |
3e4093b6 | 8391 | static void |
34cf811f MP |
8392 | output_init_element (location_t loc, tree value, tree origtype, |
8393 | bool strict_string, tree type, tree field, int pending, | |
8394 | bool implicit, struct obstack * braced_init_obstack) | |
3e4093b6 | 8395 | { |
8ce94e44 | 8396 | tree semantic_type = NULL_TREE; |
928c19bb JM |
8397 | bool maybe_const = true; |
8398 | bool npc; | |
4038c495 | 8399 | |
0a880880 | 8400 | if (type == error_mark_node || value == error_mark_node) |
8b6a5902 | 8401 | { |
3e4093b6 RS |
8402 | constructor_erroneous = 1; |
8403 | return; | |
8b6a5902 | 8404 | } |
46bdb9cf JM |
8405 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
8406 | && (TREE_CODE (value) == STRING_CST | |
8407 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
8408 | && !(TREE_CODE (value) == STRING_CST | |
8409 | && TREE_CODE (type) == ARRAY_TYPE | |
8410 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
8411 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
8412 | TYPE_MAIN_VARIANT (type))) | |
c2255bc4 | 8413 | value = array_to_pointer_conversion (input_location, value); |
8b6a5902 | 8414 | |
3e4093b6 | 8415 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
4435bb92 | 8416 | && require_constant_value && pending) |
8b6a5902 | 8417 | { |
3e4093b6 RS |
8418 | /* As an extension, allow initializing objects with static storage |
8419 | duration with compound literals (which are then treated just as | |
8420 | the brace enclosed list they contain). */ | |
4435bb92 MP |
8421 | if (flag_isoc99) |
8422 | pedwarn_init (loc, OPT_Wpedantic, "initializer element is not " | |
8423 | "constant"); | |
3e4093b6 RS |
8424 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); |
8425 | value = DECL_INITIAL (decl); | |
8b6a5902 JJ |
8426 | } |
8427 | ||
928c19bb | 8428 | npc = null_pointer_constant_p (value); |
8ce94e44 JM |
8429 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) |
8430 | { | |
8431 | semantic_type = TREE_TYPE (value); | |
8432 | value = TREE_OPERAND (value, 0); | |
8433 | } | |
928c19bb JM |
8434 | value = c_fully_fold (value, require_constant_value, &maybe_const); |
8435 | ||
3e4093b6 RS |
8436 | if (value == error_mark_node) |
8437 | constructor_erroneous = 1; | |
8438 | else if (!TREE_CONSTANT (value)) | |
8439 | constructor_constant = 0; | |
116df786 | 8440 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) |
3e4093b6 RS |
8441 | || ((TREE_CODE (constructor_type) == RECORD_TYPE |
8442 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8443 | && DECL_C_BIT_FIELD (field) | |
8444 | && TREE_CODE (value) != INTEGER_CST)) | |
8445 | constructor_simple = 0; | |
928c19bb JM |
8446 | if (!maybe_const) |
8447 | constructor_nonconst = 1; | |
3e4093b6 | 8448 | |
116df786 | 8449 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) |
8b6a5902 | 8450 | { |
116df786 RH |
8451 | if (require_constant_value) |
8452 | { | |
ea58ef42 | 8453 | error_init (loc, "initializer element is not constant"); |
116df786 RH |
8454 | value = error_mark_node; |
8455 | } | |
8456 | else if (require_constant_elements) | |
8337d1db | 8457 | pedwarn (loc, OPT_Wpedantic, |
509c9d60 | 8458 | "initializer element is not computable at load time"); |
8b6a5902 | 8459 | } |
928c19bb JM |
8460 | else if (!maybe_const |
8461 | && (require_constant_value || require_constant_elements)) | |
8337d1db | 8462 | pedwarn_init (loc, OPT_Wpedantic, |
928c19bb | 8463 | "initializer element is not a constant expression"); |
3e4093b6 | 8464 | |
81f40b79 ILT |
8465 | /* Issue -Wc++-compat warnings about initializing a bitfield with |
8466 | enum type. */ | |
8467 | if (warn_cxx_compat | |
8468 | && field != NULL_TREE | |
8469 | && TREE_CODE (field) == FIELD_DECL | |
8470 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
8471 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
8472 | != TYPE_MAIN_VARIANT (type)) | |
8473 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
8474 | { | |
8475 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
8476 | if (checktype != error_mark_node | |
8477 | && (TYPE_MAIN_VARIANT (checktype) | |
8478 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
96b40f8d | 8479 | warning_init (loc, OPT_Wc___compat, |
81f40b79 ILT |
8480 | "enum conversion in initialization is invalid in C++"); |
8481 | } | |
8482 | ||
3e4093b6 RS |
8483 | /* If this field is empty (and not at the end of structure), |
8484 | don't do anything other than checking the initializer. */ | |
8485 | if (field | |
8486 | && (TREE_TYPE (field) == error_mark_node | |
8487 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
8488 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
8489 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
910ad8de | 8490 | || DECL_CHAIN (field))))) |
3e4093b6 RS |
8491 | return; |
8492 | ||
8ce94e44 JM |
8493 | if (semantic_type) |
8494 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
34cf811f MP |
8495 | value = digest_init (loc, type, value, origtype, npc, strict_string, |
8496 | require_constant_value); | |
3e4093b6 | 8497 | if (value == error_mark_node) |
8b6a5902 | 8498 | { |
3e4093b6 RS |
8499 | constructor_erroneous = 1; |
8500 | return; | |
8b6a5902 | 8501 | } |
928c19bb JM |
8502 | if (require_constant_value || require_constant_elements) |
8503 | constant_expression_warning (value); | |
8b6a5902 | 8504 | |
3e4093b6 RS |
8505 | /* If this element doesn't come next in sequence, |
8506 | put it on constructor_pending_elts. */ | |
8507 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
8508 | && (!constructor_incremental | |
8509 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
8b6a5902 | 8510 | { |
3e4093b6 RS |
8511 | if (constructor_incremental |
8512 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 8513 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 8514 | |
96b40f8d | 8515 | add_pending_init (loc, field, value, origtype, implicit, |
a1e3b3d9 | 8516 | braced_init_obstack); |
3e4093b6 | 8517 | return; |
8b6a5902 | 8518 | } |
3e4093b6 RS |
8519 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
8520 | && (!constructor_incremental | |
8521 | || field != constructor_unfilled_fields)) | |
8b6a5902 | 8522 | { |
3e4093b6 RS |
8523 | /* We do this for records but not for unions. In a union, |
8524 | no matter which field is specified, it can be initialized | |
8525 | right away since it starts at the beginning of the union. */ | |
8526 | if (constructor_incremental) | |
8527 | { | |
8528 | if (!constructor_unfilled_fields) | |
a1e3b3d9 | 8529 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8530 | else |
8531 | { | |
8532 | tree bitpos, unfillpos; | |
8533 | ||
8534 | bitpos = bit_position (field); | |
8535 | unfillpos = bit_position (constructor_unfilled_fields); | |
8536 | ||
8537 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
a1e3b3d9 | 8538 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8539 | } |
8540 | } | |
8541 | ||
96b40f8d | 8542 | add_pending_init (loc, field, value, origtype, implicit, |
a1e3b3d9 | 8543 | braced_init_obstack); |
3e4093b6 | 8544 | return; |
8b6a5902 | 8545 | } |
3e4093b6 | 8546 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
9771b263 | 8547 | && !vec_safe_is_empty (constructor_elements)) |
3e4093b6 | 8548 | { |
b295aee2 JJ |
8549 | if (!implicit) |
8550 | { | |
9771b263 | 8551 | if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) |
755e528f | 8552 | warning_init (loc, OPT_Woverride_init_side_effects, |
b295aee2 JJ |
8553 | "initialized field with side-effects overwritten"); |
8554 | else if (warn_override_init) | |
96b40f8d MP |
8555 | warning_init (loc, OPT_Woverride_init, |
8556 | "initialized field overwritten"); | |
b295aee2 | 8557 | } |
8b6a5902 | 8558 | |
3e4093b6 | 8559 | /* We can have just one union field set. */ |
9771b263 | 8560 | constructor_elements = NULL; |
3e4093b6 | 8561 | } |
8b6a5902 | 8562 | |
3e4093b6 RS |
8563 | /* Otherwise, output this element either to |
8564 | constructor_elements or to the assembler file. */ | |
8b6a5902 | 8565 | |
f32682ca | 8566 | constructor_elt celt = {field, value}; |
9771b263 | 8567 | vec_safe_push (constructor_elements, celt); |
8b6a5902 | 8568 | |
3e4093b6 RS |
8569 | /* Advance the variable that indicates sequential elements output. */ |
8570 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8571 | constructor_unfilled_index | |
db3927fb AH |
8572 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, |
8573 | bitsize_one_node); | |
3e4093b6 RS |
8574 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
8575 | { | |
8576 | constructor_unfilled_fields | |
910ad8de | 8577 | = DECL_CHAIN (constructor_unfilled_fields); |
8b6a5902 | 8578 | |
3e4093b6 RS |
8579 | /* Skip any nameless bit fields. */ |
8580 | while (constructor_unfilled_fields != 0 | |
8581 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8582 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8583 | constructor_unfilled_fields = | |
910ad8de | 8584 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 RS |
8585 | } |
8586 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
8587 | constructor_unfilled_fields = 0; | |
de520661 | 8588 | |
3e4093b6 RS |
8589 | /* Now output any pending elements which have become next. */ |
8590 | if (pending) | |
a1e3b3d9 | 8591 | output_pending_init_elements (0, braced_init_obstack); |
3e4093b6 | 8592 | } |
8b6a5902 | 8593 | |
3e4093b6 RS |
8594 | /* Output any pending elements which have become next. |
8595 | As we output elements, constructor_unfilled_{fields,index} | |
8596 | advances, which may cause other elements to become next; | |
8597 | if so, they too are output. | |
8b6a5902 | 8598 | |
3e4093b6 RS |
8599 | If ALL is 0, we return when there are |
8600 | no more pending elements to output now. | |
665f2503 | 8601 | |
3e4093b6 RS |
8602 | If ALL is 1, we output space as necessary so that |
8603 | we can output all the pending elements. */ | |
3e4093b6 | 8604 | static void |
a1e3b3d9 | 8605 | output_pending_init_elements (int all, struct obstack * braced_init_obstack) |
3e4093b6 RS |
8606 | { |
8607 | struct init_node *elt = constructor_pending_elts; | |
8608 | tree next; | |
de520661 | 8609 | |
3e4093b6 RS |
8610 | retry: |
8611 | ||
ba228239 | 8612 | /* Look through the whole pending tree. |
3e4093b6 RS |
8613 | If we find an element that should be output now, |
8614 | output it. Otherwise, set NEXT to the element | |
8615 | that comes first among those still pending. */ | |
8616 | ||
8617 | next = 0; | |
8618 | while (elt) | |
8619 | { | |
8620 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8b6a5902 | 8621 | { |
3e4093b6 RS |
8622 | if (tree_int_cst_equal (elt->purpose, |
8623 | constructor_unfilled_index)) | |
34cf811f MP |
8624 | output_init_element (input_location, elt->value, elt->origtype, |
8625 | true, TREE_TYPE (constructor_type), | |
a1e3b3d9 LB |
8626 | constructor_unfilled_index, 0, false, |
8627 | braced_init_obstack); | |
3e4093b6 RS |
8628 | else if (tree_int_cst_lt (constructor_unfilled_index, |
8629 | elt->purpose)) | |
8b6a5902 | 8630 | { |
3e4093b6 RS |
8631 | /* Advance to the next smaller node. */ |
8632 | if (elt->left) | |
8633 | elt = elt->left; | |
8634 | else | |
8635 | { | |
8636 | /* We have reached the smallest node bigger than the | |
8637 | current unfilled index. Fill the space first. */ | |
8638 | next = elt->purpose; | |
8639 | break; | |
8640 | } | |
8b6a5902 | 8641 | } |
ce662d4c JJ |
8642 | else |
8643 | { | |
3e4093b6 RS |
8644 | /* Advance to the next bigger node. */ |
8645 | if (elt->right) | |
8646 | elt = elt->right; | |
8647 | else | |
ce662d4c | 8648 | { |
3e4093b6 RS |
8649 | /* We have reached the biggest node in a subtree. Find |
8650 | the parent of it, which is the next bigger node. */ | |
8651 | while (elt->parent && elt->parent->right == elt) | |
8652 | elt = elt->parent; | |
8653 | elt = elt->parent; | |
8654 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
8655 | elt->purpose)) | |
8656 | { | |
8657 | next = elt->purpose; | |
8658 | break; | |
8659 | } | |
ce662d4c JJ |
8660 | } |
8661 | } | |
8b6a5902 | 8662 | } |
3e4093b6 RS |
8663 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
8664 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8665 | { | |
8666 | tree ctor_unfilled_bitpos, elt_bitpos; | |
ce662d4c | 8667 | |
3e4093b6 RS |
8668 | /* If the current record is complete we are done. */ |
8669 | if (constructor_unfilled_fields == 0) | |
8670 | break; | |
de520661 | 8671 | |
3e4093b6 RS |
8672 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
8673 | elt_bitpos = bit_position (elt->purpose); | |
8674 | /* We can't compare fields here because there might be empty | |
8675 | fields in between. */ | |
8676 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
8677 | { | |
8678 | constructor_unfilled_fields = elt->purpose; | |
34cf811f MP |
8679 | output_init_element (input_location, elt->value, elt->origtype, |
8680 | true, TREE_TYPE (elt->purpose), | |
a1e3b3d9 LB |
8681 | elt->purpose, 0, false, |
8682 | braced_init_obstack); | |
3e4093b6 RS |
8683 | } |
8684 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
8685 | { | |
8686 | /* Advance to the next smaller node. */ | |
8687 | if (elt->left) | |
8688 | elt = elt->left; | |
8689 | else | |
8690 | { | |
8691 | /* We have reached the smallest node bigger than the | |
8692 | current unfilled field. Fill the space first. */ | |
8693 | next = elt->purpose; | |
8694 | break; | |
8695 | } | |
8696 | } | |
8697 | else | |
8698 | { | |
8699 | /* Advance to the next bigger node. */ | |
8700 | if (elt->right) | |
8701 | elt = elt->right; | |
8702 | else | |
8703 | { | |
8704 | /* We have reached the biggest node in a subtree. Find | |
8705 | the parent of it, which is the next bigger node. */ | |
8706 | while (elt->parent && elt->parent->right == elt) | |
8707 | elt = elt->parent; | |
8708 | elt = elt->parent; | |
8709 | if (elt | |
8710 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
8711 | bit_position (elt->purpose)))) | |
8712 | { | |
8713 | next = elt->purpose; | |
8714 | break; | |
8715 | } | |
8716 | } | |
8717 | } | |
8718 | } | |
8719 | } | |
de520661 | 8720 | |
3e4093b6 RS |
8721 | /* Ordinarily return, but not if we want to output all |
8722 | and there are elements left. */ | |
3f75a254 | 8723 | if (!(all && next != 0)) |
e5cfb88f RK |
8724 | return; |
8725 | ||
3e4093b6 RS |
8726 | /* If it's not incremental, just skip over the gap, so that after |
8727 | jumping to retry we will output the next successive element. */ | |
8728 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
8729 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8730 | constructor_unfilled_fields = next; | |
8731 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8732 | constructor_unfilled_index = next; | |
de520661 | 8733 | |
3e4093b6 RS |
8734 | /* ELT now points to the node in the pending tree with the next |
8735 | initializer to output. */ | |
8736 | goto retry; | |
de520661 RS |
8737 | } |
8738 | \f | |
3e4093b6 RS |
8739 | /* Add one non-braced element to the current constructor level. |
8740 | This adjusts the current position within the constructor's type. | |
8741 | This may also start or terminate implicit levels | |
8742 | to handle a partly-braced initializer. | |
e5e809f4 | 8743 | |
3e4093b6 | 8744 | Once this has found the correct level for the new element, |
b295aee2 JJ |
8745 | it calls output_init_element. |
8746 | ||
8747 | IMPLICIT is true if value comes from pop_init_level (1), | |
8748 | the new initializer has been merged with the existing one | |
8749 | and thus no warnings should be emitted about overriding an | |
8750 | existing initializer. */ | |
3e4093b6 RS |
8751 | |
8752 | void | |
34cf811f | 8753 | process_init_element (location_t loc, struct c_expr value, bool implicit, |
a1e3b3d9 | 8754 | struct obstack * braced_init_obstack) |
e5e809f4 | 8755 | { |
916c5919 JM |
8756 | tree orig_value = value.value; |
8757 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
8758 | bool strict_string = value.original_code == STRING_CST; | |
340baef7 | 8759 | bool was_designated = designator_depth != 0; |
e5e809f4 | 8760 | |
3e4093b6 | 8761 | designator_depth = 0; |
b06df647 | 8762 | designator_erroneous = 0; |
e5e809f4 | 8763 | |
9bac5cbb G |
8764 | if (!implicit && value.value && !integer_zerop (value.value)) |
8765 | constructor_zeroinit = 0; | |
8766 | ||
3e4093b6 RS |
8767 | /* Handle superfluous braces around string cst as in |
8768 | char x[] = {"foo"}; */ | |
8769 | if (string_flag | |
8770 | && constructor_type | |
340baef7 | 8771 | && !was_designated |
3e4093b6 | 8772 | && TREE_CODE (constructor_type) == ARRAY_TYPE |
197463ae | 8773 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
3e4093b6 | 8774 | && integer_zerop (constructor_unfilled_index)) |
e5e809f4 | 8775 | { |
916c5919 | 8776 | if (constructor_stack->replacement_value.value) |
ea58ef42 | 8777 | error_init (loc, "excess elements in char array initializer"); |
3e4093b6 RS |
8778 | constructor_stack->replacement_value = value; |
8779 | return; | |
e5e809f4 | 8780 | } |
8b6a5902 | 8781 | |
916c5919 | 8782 | if (constructor_stack->replacement_value.value != 0) |
3e4093b6 | 8783 | { |
ea58ef42 | 8784 | error_init (loc, "excess elements in struct initializer"); |
3e4093b6 | 8785 | return; |
e5e809f4 JL |
8786 | } |
8787 | ||
3e4093b6 RS |
8788 | /* Ignore elements of a brace group if it is entirely superfluous |
8789 | and has already been diagnosed. */ | |
8790 | if (constructor_type == 0) | |
8791 | return; | |
e5e809f4 | 8792 | |
976d5a22 TT |
8793 | if (!implicit && warn_designated_init && !was_designated |
8794 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
8795 | && lookup_attribute ("designated_init", | |
8796 | TYPE_ATTRIBUTES (constructor_type))) | |
8797 | warning_init (loc, | |
8798 | OPT_Wdesignated_init, | |
8799 | "positional initialization of field " | |
8800 | "in %<struct%> declared with %<designated_init%> attribute"); | |
8801 | ||
3e4093b6 RS |
8802 | /* If we've exhausted any levels that didn't have braces, |
8803 | pop them now. */ | |
8804 | while (constructor_stack->implicit) | |
8805 | { | |
8806 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
8807 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8808 | && constructor_fields == 0) | |
ea58ef42 MP |
8809 | process_init_element (loc, |
8810 | pop_init_level (loc, 1, braced_init_obstack), | |
a1e3b3d9 | 8811 | true, braced_init_obstack); |
53650abe | 8812 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE |
31521951 | 8813 | || VECTOR_TYPE_P (constructor_type)) |
8824edff JJ |
8814 | && constructor_max_index |
8815 | && tree_int_cst_lt (constructor_max_index, | |
8816 | constructor_index)) | |
ea58ef42 MP |
8817 | process_init_element (loc, |
8818 | pop_init_level (loc, 1, braced_init_obstack), | |
a1e3b3d9 | 8819 | true, braced_init_obstack); |
3e4093b6 RS |
8820 | else |
8821 | break; | |
8822 | } | |
e5e809f4 | 8823 | |
3e4093b6 RS |
8824 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
8825 | if (constructor_range_stack) | |
e5e809f4 | 8826 | { |
3e4093b6 RS |
8827 | /* If value is a compound literal and we'll be just using its |
8828 | content, don't put it into a SAVE_EXPR. */ | |
916c5919 | 8829 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
c3e38a03 | 8830 | || !require_constant_value) |
8ce94e44 JM |
8831 | { |
8832 | tree semantic_type = NULL_TREE; | |
8833 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
8834 | { | |
8835 | semantic_type = TREE_TYPE (value.value); | |
8836 | value.value = TREE_OPERAND (value.value, 0); | |
8837 | } | |
8838 | value.value = c_save_expr (value.value); | |
8839 | if (semantic_type) | |
8840 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
8841 | value.value); | |
8842 | } | |
3e4093b6 | 8843 | } |
e5e809f4 | 8844 | |
3e4093b6 RS |
8845 | while (1) |
8846 | { | |
8847 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
e5e809f4 | 8848 | { |
3e4093b6 RS |
8849 | tree fieldtype; |
8850 | enum tree_code fieldcode; | |
e5e809f4 | 8851 | |
3e4093b6 RS |
8852 | if (constructor_fields == 0) |
8853 | { | |
ea58ef42 | 8854 | pedwarn_init (loc, 0, "excess elements in struct initializer"); |
3e4093b6 RS |
8855 | break; |
8856 | } | |
e5e809f4 | 8857 | |
3e4093b6 RS |
8858 | fieldtype = TREE_TYPE (constructor_fields); |
8859 | if (fieldtype != error_mark_node) | |
8860 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8861 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8862 | |
3e4093b6 RS |
8863 | /* Error for non-static initialization of a flexible array member. */ |
8864 | if (fieldcode == ARRAY_TYPE | |
8865 | && !require_constant_value | |
8866 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
f7587ed0 | 8867 | && DECL_CHAIN (constructor_fields) == NULL_TREE) |
3e4093b6 | 8868 | { |
ea58ef42 MP |
8869 | error_init (loc, "non-static initialization of a flexible " |
8870 | "array member"); | |
3e4093b6 RS |
8871 | break; |
8872 | } | |
e5e809f4 | 8873 | |
2cc901dc MP |
8874 | /* Error for initialization of a flexible array member with |
8875 | a string constant if the structure is in an array. E.g.: | |
8876 | struct S { int x; char y[]; }; | |
8877 | struct S s[] = { { 1, "foo" } }; | |
8878 | is invalid. */ | |
8879 | if (string_flag | |
8880 | && fieldcode == ARRAY_TYPE | |
8881 | && constructor_depth > 1 | |
8882 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
8883 | && DECL_CHAIN (constructor_fields) == NULL_TREE) | |
8884 | { | |
8885 | bool in_array_p = false; | |
8886 | for (struct constructor_stack *p = constructor_stack; | |
8887 | p && p->type; p = p->next) | |
8888 | if (TREE_CODE (p->type) == ARRAY_TYPE) | |
8889 | { | |
8890 | in_array_p = true; | |
8891 | break; | |
8892 | } | |
8893 | if (in_array_p) | |
8894 | { | |
8895 | error_init (loc, "initialization of flexible array " | |
8896 | "member in a nested context"); | |
8897 | break; | |
8898 | } | |
8899 | } | |
8900 | ||
3e4093b6 | 8901 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8902 | if (value.value != 0 |
3e4093b6 | 8903 | && fieldcode == ARRAY_TYPE |
197463ae | 8904 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8905 | && string_flag) |
916c5919 | 8906 | value.value = orig_value; |
3e4093b6 RS |
8907 | /* Otherwise, if we have come to a subaggregate, |
8908 | and we don't have an element of its type, push into it. */ | |
0953878d | 8909 | else if (value.value != 0 |
916c5919 JM |
8910 | && value.value != error_mark_node |
8911 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 8912 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 8913 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 8914 | { |
ea58ef42 | 8915 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
8916 | continue; |
8917 | } | |
e5e809f4 | 8918 | |
916c5919 | 8919 | if (value.value) |
3e4093b6 RS |
8920 | { |
8921 | push_member_name (constructor_fields); | |
34cf811f | 8922 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 8923 | strict_string, fieldtype, |
a1e3b3d9 LB |
8924 | constructor_fields, 1, implicit, |
8925 | braced_init_obstack); | |
3e4093b6 | 8926 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
8927 | } |
8928 | else | |
3e4093b6 RS |
8929 | /* Do the bookkeeping for an element that was |
8930 | directly output as a constructor. */ | |
e5e809f4 | 8931 | { |
3e4093b6 RS |
8932 | /* For a record, keep track of end position of last field. */ |
8933 | if (DECL_SIZE (constructor_fields)) | |
c22cacf3 | 8934 | constructor_bit_index |
db3927fb AH |
8935 | = size_binop_loc (input_location, PLUS_EXPR, |
8936 | bit_position (constructor_fields), | |
8937 | DECL_SIZE (constructor_fields)); | |
3e4093b6 RS |
8938 | |
8939 | /* If the current field was the first one not yet written out, | |
8940 | it isn't now, so update. */ | |
8941 | if (constructor_unfilled_fields == constructor_fields) | |
8942 | { | |
910ad8de | 8943 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8944 | /* Skip any nameless bit fields. */ |
8945 | while (constructor_unfilled_fields != 0 | |
8946 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8947 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8948 | constructor_unfilled_fields = | |
910ad8de | 8949 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 | 8950 | } |
e5e809f4 | 8951 | } |
3e4093b6 | 8952 | |
910ad8de | 8953 | constructor_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8954 | /* Skip any nameless bit fields at the beginning. */ |
8955 | while (constructor_fields != 0 | |
8956 | && DECL_C_BIT_FIELD (constructor_fields) | |
8957 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 8958 | constructor_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 8959 | } |
3e4093b6 | 8960 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
e5e809f4 | 8961 | { |
3e4093b6 RS |
8962 | tree fieldtype; |
8963 | enum tree_code fieldcode; | |
e5e809f4 | 8964 | |
3e4093b6 RS |
8965 | if (constructor_fields == 0) |
8966 | { | |
d033409e | 8967 | pedwarn_init (loc, 0, |
509c9d60 | 8968 | "excess elements in union initializer"); |
3e4093b6 RS |
8969 | break; |
8970 | } | |
e5e809f4 | 8971 | |
3e4093b6 RS |
8972 | fieldtype = TREE_TYPE (constructor_fields); |
8973 | if (fieldtype != error_mark_node) | |
8974 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8975 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8976 | |
3e4093b6 RS |
8977 | /* Warn that traditional C rejects initialization of unions. |
8978 | We skip the warning if the value is zero. This is done | |
8979 | under the assumption that the zero initializer in user | |
8980 | code appears conditioned on e.g. __STDC__ to avoid | |
8981 | "missing initializer" warnings and relies on default | |
8982 | initialization to zero in the traditional C case. | |
8983 | We also skip the warning if the initializer is designated, | |
8984 | again on the assumption that this must be conditional on | |
8985 | __STDC__ anyway (and we've already complained about the | |
8986 | member-designator already). */ | |
8400e75e | 8987 | if (!in_system_header_at (input_location) && !constructor_designated |
916c5919 JM |
8988 | && !(value.value && (integer_zerop (value.value) |
8989 | || real_zerop (value.value)))) | |
3176a0c2 DD |
8990 | warning (OPT_Wtraditional, "traditional C rejects initialization " |
8991 | "of unions"); | |
e5e809f4 | 8992 | |
3e4093b6 | 8993 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8994 | if (value.value != 0 |
3e4093b6 | 8995 | && fieldcode == ARRAY_TYPE |
197463ae | 8996 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8997 | && string_flag) |
916c5919 | 8998 | value.value = orig_value; |
3e4093b6 RS |
8999 | /* Otherwise, if we have come to a subaggregate, |
9000 | and we don't have an element of its type, push into it. */ | |
0953878d | 9001 | else if (value.value != 0 |
916c5919 JM |
9002 | && value.value != error_mark_node |
9003 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 9004 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 9005 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 9006 | { |
ea58ef42 | 9007 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
9008 | continue; |
9009 | } | |
e5e809f4 | 9010 | |
916c5919 | 9011 | if (value.value) |
3e4093b6 RS |
9012 | { |
9013 | push_member_name (constructor_fields); | |
34cf811f | 9014 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 9015 | strict_string, fieldtype, |
a1e3b3d9 LB |
9016 | constructor_fields, 1, implicit, |
9017 | braced_init_obstack); | |
3e4093b6 | 9018 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
9019 | } |
9020 | else | |
3e4093b6 RS |
9021 | /* Do the bookkeeping for an element that was |
9022 | directly output as a constructor. */ | |
e5e809f4 | 9023 | { |
3e4093b6 | 9024 | constructor_bit_index = DECL_SIZE (constructor_fields); |
f7587ed0 | 9025 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 9026 | } |
e5e809f4 | 9027 | |
3e4093b6 RS |
9028 | constructor_fields = 0; |
9029 | } | |
9030 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
9031 | { | |
9032 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
9033 | enum tree_code eltcode = TREE_CODE (elttype); | |
e5e809f4 | 9034 | |
3e4093b6 | 9035 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 9036 | if (value.value != 0 |
3e4093b6 | 9037 | && eltcode == ARRAY_TYPE |
197463ae | 9038 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
3e4093b6 | 9039 | && string_flag) |
916c5919 | 9040 | value.value = orig_value; |
3e4093b6 RS |
9041 | /* Otherwise, if we have come to a subaggregate, |
9042 | and we don't have an element of its type, push into it. */ | |
0953878d | 9043 | else if (value.value != 0 |
916c5919 JM |
9044 | && value.value != error_mark_node |
9045 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
3e4093b6 | 9046 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
53650abe | 9047 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) |
3e4093b6 | 9048 | { |
ea58ef42 | 9049 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
9050 | continue; |
9051 | } | |
8b6a5902 | 9052 | |
3e4093b6 RS |
9053 | if (constructor_max_index != 0 |
9054 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
9055 | || integer_all_onesp (constructor_max_index))) | |
9056 | { | |
d033409e | 9057 | pedwarn_init (loc, 0, |
509c9d60 | 9058 | "excess elements in array initializer"); |
3e4093b6 RS |
9059 | break; |
9060 | } | |
8b6a5902 | 9061 | |
3e4093b6 | 9062 | /* Now output the actual element. */ |
916c5919 | 9063 | if (value.value) |
3e4093b6 | 9064 | { |
ae7e9ddd | 9065 | push_array_bounds (tree_to_uhwi (constructor_index)); |
34cf811f | 9066 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 9067 | strict_string, elttype, |
a1e3b3d9 LB |
9068 | constructor_index, 1, implicit, |
9069 | braced_init_obstack); | |
3e4093b6 RS |
9070 | RESTORE_SPELLING_DEPTH (constructor_depth); |
9071 | } | |
2f6e4e97 | 9072 | |
3e4093b6 | 9073 | constructor_index |
db3927fb AH |
9074 | = size_binop_loc (input_location, PLUS_EXPR, |
9075 | constructor_index, bitsize_one_node); | |
8b6a5902 | 9076 | |
916c5919 | 9077 | if (!value.value) |
3e4093b6 RS |
9078 | /* If we are doing the bookkeeping for an element that was |
9079 | directly output as a constructor, we must update | |
9080 | constructor_unfilled_index. */ | |
9081 | constructor_unfilled_index = constructor_index; | |
9082 | } | |
31521951 | 9083 | else if (VECTOR_TYPE_P (constructor_type)) |
3e4093b6 RS |
9084 | { |
9085 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8b6a5902 | 9086 | |
c22cacf3 MS |
9087 | /* Do a basic check of initializer size. Note that vectors |
9088 | always have a fixed size derived from their type. */ | |
3e4093b6 RS |
9089 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) |
9090 | { | |
d033409e | 9091 | pedwarn_init (loc, 0, |
509c9d60 | 9092 | "excess elements in vector initializer"); |
3e4093b6 RS |
9093 | break; |
9094 | } | |
8b6a5902 | 9095 | |
3e4093b6 | 9096 | /* Now output the actual element. */ |
916c5919 | 9097 | if (value.value) |
53650abe AP |
9098 | { |
9099 | if (TREE_CODE (value.value) == VECTOR_CST) | |
9100 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
34cf811f | 9101 | output_init_element (loc, value.value, value.original_type, |
53650abe | 9102 | strict_string, elttype, |
a1e3b3d9 LB |
9103 | constructor_index, 1, implicit, |
9104 | braced_init_obstack); | |
53650abe | 9105 | } |
8b6a5902 | 9106 | |
3e4093b6 | 9107 | constructor_index |
db3927fb AH |
9108 | = size_binop_loc (input_location, |
9109 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 9110 | |
916c5919 | 9111 | if (!value.value) |
3e4093b6 RS |
9112 | /* If we are doing the bookkeeping for an element that was |
9113 | directly output as a constructor, we must update | |
9114 | constructor_unfilled_index. */ | |
9115 | constructor_unfilled_index = constructor_index; | |
9116 | } | |
8b6a5902 | 9117 | |
3e4093b6 RS |
9118 | /* Handle the sole element allowed in a braced initializer |
9119 | for a scalar variable. */ | |
b4519d39 SB |
9120 | else if (constructor_type != error_mark_node |
9121 | && constructor_fields == 0) | |
8b6a5902 | 9122 | { |
d033409e | 9123 | pedwarn_init (loc, 0, |
509c9d60 | 9124 | "excess elements in scalar initializer"); |
3e4093b6 | 9125 | break; |
8b6a5902 JJ |
9126 | } |
9127 | else | |
9128 | { | |
916c5919 | 9129 | if (value.value) |
34cf811f | 9130 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 9131 | strict_string, constructor_type, |
a1e3b3d9 LB |
9132 | NULL_TREE, 1, implicit, |
9133 | braced_init_obstack); | |
3e4093b6 | 9134 | constructor_fields = 0; |
8b6a5902 JJ |
9135 | } |
9136 | ||
3e4093b6 RS |
9137 | /* Handle range initializers either at this level or anywhere higher |
9138 | in the designator stack. */ | |
9139 | if (constructor_range_stack) | |
8b6a5902 | 9140 | { |
3e4093b6 RS |
9141 | struct constructor_range_stack *p, *range_stack; |
9142 | int finish = 0; | |
9143 | ||
9144 | range_stack = constructor_range_stack; | |
9145 | constructor_range_stack = 0; | |
9146 | while (constructor_stack != range_stack->stack) | |
8b6a5902 | 9147 | { |
366de0ce | 9148 | gcc_assert (constructor_stack->implicit); |
34cf811f | 9149 | process_init_element (loc, |
ea58ef42 MP |
9150 | pop_init_level (loc, 1, |
9151 | braced_init_obstack), | |
a1e3b3d9 | 9152 | true, braced_init_obstack); |
8b6a5902 | 9153 | } |
3e4093b6 RS |
9154 | for (p = range_stack; |
9155 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
9156 | p = p->prev) | |
8b6a5902 | 9157 | { |
366de0ce | 9158 | gcc_assert (constructor_stack->implicit); |
34cf811f | 9159 | process_init_element (loc, |
ea58ef42 MP |
9160 | pop_init_level (loc, 1, |
9161 | braced_init_obstack), | |
a1e3b3d9 | 9162 | true, braced_init_obstack); |
8b6a5902 | 9163 | } |
3e4093b6 | 9164 | |
db3927fb AH |
9165 | p->index = size_binop_loc (input_location, |
9166 | PLUS_EXPR, p->index, bitsize_one_node); | |
3e4093b6 RS |
9167 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) |
9168 | finish = 1; | |
9169 | ||
9170 | while (1) | |
9171 | { | |
9172 | constructor_index = p->index; | |
9173 | constructor_fields = p->fields; | |
9174 | if (finish && p->range_end && p->index == p->range_start) | |
9175 | { | |
9176 | finish = 0; | |
9177 | p->prev = 0; | |
9178 | } | |
9179 | p = p->next; | |
9180 | if (!p) | |
9181 | break; | |
ea58ef42 | 9182 | push_init_level (loc, 2, braced_init_obstack); |
3e4093b6 RS |
9183 | p->stack = constructor_stack; |
9184 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
9185 | p->index = p->range_start; | |
9186 | } | |
9187 | ||
9188 | if (!finish) | |
9189 | constructor_range_stack = range_stack; | |
9190 | continue; | |
8b6a5902 JJ |
9191 | } |
9192 | ||
3e4093b6 | 9193 | break; |
8b6a5902 JJ |
9194 | } |
9195 | ||
3e4093b6 RS |
9196 | constructor_range_stack = 0; |
9197 | } | |
9198 | \f | |
9f0e2d86 ZW |
9199 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
9200 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
e130a54b | 9201 | an ASM_EXPR node). */ |
3e4093b6 | 9202 | tree |
9f0e2d86 | 9203 | build_asm_stmt (tree cv_qualifier, tree args) |
3e4093b6 | 9204 | { |
6de9cd9a DN |
9205 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
9206 | ASM_VOLATILE_P (args) = 1; | |
9f0e2d86 | 9207 | return add_stmt (args); |
8b6a5902 JJ |
9208 | } |
9209 | ||
9f0e2d86 ZW |
9210 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
9211 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
9212 | SIMPLE indicates whether there was anything at all after the | |
9213 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
e130a54b | 9214 | are subtly different. We use a ASM_EXPR node to represent this. */ |
3e4093b6 | 9215 | tree |
c2255bc4 | 9216 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, |
1c384bf1 | 9217 | tree clobbers, tree labels, bool simple) |
e5e809f4 | 9218 | { |
3e4093b6 | 9219 | tree tail; |
9f0e2d86 | 9220 | tree args; |
6de9cd9a DN |
9221 | int i; |
9222 | const char *constraint; | |
74f0c611 | 9223 | const char **oconstraints; |
6de9cd9a | 9224 | bool allows_mem, allows_reg, is_inout; |
74f0c611 | 9225 | int ninputs, noutputs; |
6de9cd9a DN |
9226 | |
9227 | ninputs = list_length (inputs); | |
9228 | noutputs = list_length (outputs); | |
74f0c611 RH |
9229 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); |
9230 | ||
1c384bf1 | 9231 | string = resolve_asm_operand_names (string, outputs, inputs, labels); |
3e4093b6 | 9232 | |
6de9cd9a DN |
9233 | /* Remove output conversions that change the type but not the mode. */ |
9234 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
e5e809f4 | 9235 | { |
3e4093b6 | 9236 | tree output = TREE_VALUE (tail); |
74f0c611 | 9237 | |
eadd3d0d JJ |
9238 | output = c_fully_fold (output, false, NULL); |
9239 | ||
74f0c611 RH |
9240 | /* ??? Really, this should not be here. Users should be using a |
9241 | proper lvalue, dammit. But there's a long history of using casts | |
9242 | in the output operands. In cases like longlong.h, this becomes a | |
9243 | primitive form of typechecking -- if the cast can be removed, then | |
9244 | the output operand had a type of the proper width; otherwise we'll | |
9245 | get an error. Gross, but ... */ | |
3e4093b6 | 9246 | STRIP_NOPS (output); |
74f0c611 | 9247 | |
7bd11157 | 9248 | if (!lvalue_or_else (loc, output, lv_asm)) |
74f0c611 | 9249 | output = error_mark_node; |
8b6a5902 | 9250 | |
5544530a PB |
9251 | if (output != error_mark_node |
9252 | && (TREE_READONLY (output) | |
9253 | || TYPE_READONLY (TREE_TYPE (output)) | |
9254 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
9255 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
9256 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
c02065fc | 9257 | readonly_error (loc, output, lv_asm); |
5544530a | 9258 | |
6de9cd9a | 9259 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
74f0c611 RH |
9260 | oconstraints[i] = constraint; |
9261 | ||
9262 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
9263 | &allows_mem, &allows_reg, &is_inout)) | |
9264 | { | |
9265 | /* If the operand is going to end up in memory, | |
9266 | mark it addressable. */ | |
9267 | if (!allows_reg && !c_mark_addressable (output)) | |
9268 | output = error_mark_node; | |
bae5cddf JJ |
9269 | if (!(!allows_reg && allows_mem) |
9270 | && output != error_mark_node | |
9271 | && VOID_TYPE_P (TREE_TYPE (output))) | |
9272 | { | |
9273 | error_at (loc, "invalid use of void expression"); | |
9274 | output = error_mark_node; | |
9275 | } | |
74f0c611 RH |
9276 | } |
9277 | else | |
c22cacf3 | 9278 | output = error_mark_node; |
3e4093b6 | 9279 | |
74f0c611 | 9280 | TREE_VALUE (tail) = output; |
8b6a5902 | 9281 | } |
3e4093b6 | 9282 | |
74f0c611 RH |
9283 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) |
9284 | { | |
9285 | tree input; | |
9286 | ||
9287 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
9288 | input = TREE_VALUE (tail); | |
9289 | ||
74f0c611 RH |
9290 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
9291 | oconstraints, &allows_mem, &allows_reg)) | |
9292 | { | |
9293 | /* If the operand is going to end up in memory, | |
9294 | mark it addressable. */ | |
b4c33883 AP |
9295 | if (!allows_reg && allows_mem) |
9296 | { | |
eadd3d0d JJ |
9297 | input = c_fully_fold (input, false, NULL); |
9298 | ||
b4c33883 AP |
9299 | /* Strip the nops as we allow this case. FIXME, this really |
9300 | should be rejected or made deprecated. */ | |
9301 | STRIP_NOPS (input); | |
9302 | if (!c_mark_addressable (input)) | |
9303 | input = error_mark_node; | |
bae5cddf | 9304 | } |
eadd3d0d | 9305 | else |
bae5cddf | 9306 | { |
eadd3d0d JJ |
9307 | struct c_expr expr; |
9308 | memset (&expr, 0, sizeof (expr)); | |
9309 | expr.value = input; | |
267bac10 | 9310 | expr = convert_lvalue_to_rvalue (loc, expr, true, false); |
eadd3d0d JJ |
9311 | input = c_fully_fold (expr.value, false, NULL); |
9312 | ||
9313 | if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) | |
9314 | { | |
9315 | error_at (loc, "invalid use of void expression"); | |
9316 | input = error_mark_node; | |
9317 | } | |
bae5cddf | 9318 | } |
74f0c611 RH |
9319 | } |
9320 | else | |
9321 | input = error_mark_node; | |
9322 | ||
9323 | TREE_VALUE (tail) = input; | |
9324 | } | |
3e4093b6 | 9325 | |
1c384bf1 RH |
9326 | /* ASMs with labels cannot have outputs. This should have been |
9327 | enforced by the parser. */ | |
9328 | gcc_assert (outputs == NULL || labels == NULL); | |
9329 | ||
9330 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
9f0e2d86 | 9331 | |
5544530a PB |
9332 | /* asm statements without outputs, including simple ones, are treated |
9333 | as volatile. */ | |
9334 | ASM_INPUT_P (args) = simple; | |
9335 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
74f0c611 | 9336 | |
9f0e2d86 | 9337 | return args; |
e5e809f4 | 9338 | } |
3e4093b6 | 9339 | \f |
c2255bc4 AH |
9340 | /* Generate a goto statement to LABEL. LOC is the location of the |
9341 | GOTO. */ | |
506e2710 RH |
9342 | |
9343 | tree | |
c2255bc4 | 9344 | c_finish_goto_label (location_t loc, tree label) |
506e2710 | 9345 | { |
e1b7793c | 9346 | tree decl = lookup_label_for_goto (loc, label); |
506e2710 RH |
9347 | if (!decl) |
9348 | return NULL_TREE; | |
506e2710 | 9349 | TREE_USED (decl) = 1; |
c2255bc4 AH |
9350 | { |
9351 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
9352 | SET_EXPR_LOCATION (t, loc); | |
9353 | return add_stmt (t); | |
9354 | } | |
506e2710 RH |
9355 | } |
9356 | ||
c2255bc4 AH |
9357 | /* Generate a computed goto statement to EXPR. LOC is the location of |
9358 | the GOTO. */ | |
506e2710 RH |
9359 | |
9360 | tree | |
c2255bc4 | 9361 | c_finish_goto_ptr (location_t loc, tree expr) |
506e2710 | 9362 | { |
c2255bc4 | 9363 | tree t; |
c1771a20 | 9364 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); |
928c19bb | 9365 | expr = c_fully_fold (expr, false, NULL); |
506e2710 | 9366 | expr = convert (ptr_type_node, expr); |
c2255bc4 AH |
9367 | t = build1 (GOTO_EXPR, void_type_node, expr); |
9368 | SET_EXPR_LOCATION (t, loc); | |
9369 | return add_stmt (t); | |
506e2710 RH |
9370 | } |
9371 | ||
5088b058 | 9372 | /* Generate a C `return' statement. RETVAL is the expression for what |
c2255bc4 | 9373 | to return, or a null pointer for `return;' with no value. LOC is |
6e07c515 MP |
9374 | the location of the return statement, or the location of the expression, |
9375 | if the statement has any. If ORIGTYPE is not NULL_TREE, it | |
c2255bc4 | 9376 | is the original type of RETVAL. */ |
de520661 | 9377 | |
506e2710 | 9378 | tree |
c2255bc4 | 9379 | c_finish_return (location_t loc, tree retval, tree origtype) |
3e4093b6 | 9380 | { |
0c9b182b JJ |
9381 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; |
9382 | bool no_warning = false; | |
928c19bb | 9383 | bool npc = false; |
36536d79 | 9384 | size_t rank = 0; |
3e4093b6 | 9385 | |
de8ddd5f MP |
9386 | /* Use the expansion point to handle cases such as returning NULL |
9387 | in a function returning void. */ | |
9388 | source_location xloc = expansion_point_location_if_in_system_header (loc); | |
9389 | ||
3e4093b6 | 9390 | if (TREE_THIS_VOLATILE (current_function_decl)) |
de8ddd5f | 9391 | warning_at (xloc, 0, |
c2255bc4 | 9392 | "function declared %<noreturn%> has a %<return%> statement"); |
3e4093b6 | 9393 | |
b72271b9 | 9394 | if (flag_cilkplus && contains_array_notation_expr (retval)) |
36536d79 BI |
9395 | { |
9396 | /* Array notations are allowed in a return statement if it is inside a | |
9397 | built-in array notation reduction function. */ | |
9398 | if (!find_rank (loc, retval, retval, false, &rank)) | |
9399 | return error_mark_node; | |
9400 | if (rank >= 1) | |
9401 | { | |
9402 | error_at (loc, "array notation expression cannot be used as a " | |
9403 | "return value"); | |
9404 | return error_mark_node; | |
9405 | } | |
9406 | } | |
3af9c5e9 | 9407 | if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval)) |
939b37da BI |
9408 | { |
9409 | error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not " | |
9410 | "allowed"); | |
9411 | return error_mark_node; | |
9412 | } | |
928c19bb JM |
9413 | if (retval) |
9414 | { | |
8ce94e44 | 9415 | tree semantic_type = NULL_TREE; |
928c19bb | 9416 | npc = null_pointer_constant_p (retval); |
8ce94e44 JM |
9417 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) |
9418 | { | |
9419 | semantic_type = TREE_TYPE (retval); | |
9420 | retval = TREE_OPERAND (retval, 0); | |
9421 | } | |
928c19bb | 9422 | retval = c_fully_fold (retval, false, NULL); |
8ce94e44 JM |
9423 | if (semantic_type) |
9424 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
928c19bb JM |
9425 | } |
9426 | ||
3e4093b6 | 9427 | if (!retval) |
de520661 | 9428 | { |
3e4093b6 RS |
9429 | current_function_returns_null = 1; |
9430 | if ((warn_return_type || flag_isoc99) | |
9431 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
0c9b182b | 9432 | { |
35aff4fb MP |
9433 | if (flag_isoc99) |
9434 | pedwarn (loc, 0, "%<return%> with no value, in " | |
9435 | "function returning non-void"); | |
9436 | else | |
9437 | warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, " | |
9438 | "in function returning non-void"); | |
0c9b182b JJ |
9439 | no_warning = true; |
9440 | } | |
400fbf9f | 9441 | } |
3e4093b6 | 9442 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
de520661 | 9443 | { |
3e4093b6 | 9444 | current_function_returns_null = 1; |
2397c575 | 9445 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) |
de8ddd5f | 9446 | pedwarn (xloc, 0, |
509c9d60 | 9447 | "%<return%> with a value, in function returning void"); |
b8698a0f | 9448 | else |
de8ddd5f | 9449 | pedwarn (xloc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 9450 | "%<return%> with expression, in function returning void"); |
de520661 | 9451 | } |
3e4093b6 | 9452 | else |
de520661 | 9453 | { |
68fca595 MP |
9454 | tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype, |
9455 | retval, origtype, ic_return, | |
c2255bc4 | 9456 | npc, NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
9457 | tree res = DECL_RESULT (current_function_decl); |
9458 | tree inner; | |
9feb29df | 9459 | bool save; |
3e4093b6 RS |
9460 | |
9461 | current_function_returns_value = 1; | |
9462 | if (t == error_mark_node) | |
506e2710 | 9463 | return NULL_TREE; |
3e4093b6 | 9464 | |
9feb29df JJ |
9465 | save = in_late_binary_op; |
9466 | if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE | |
e5341100 JJ |
9467 | || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE |
9468 | || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE | |
9469 | && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE | |
9470 | || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE) | |
9471 | && (flag_sanitize & SANITIZE_FLOAT_CAST))) | |
9feb29df | 9472 | in_late_binary_op = true; |
3e4093b6 | 9473 | inner = t = convert (TREE_TYPE (res), t); |
9feb29df | 9474 | in_late_binary_op = save; |
3e4093b6 RS |
9475 | |
9476 | /* Strip any conversions, additions, and subtractions, and see if | |
9477 | we are returning the address of a local variable. Warn if so. */ | |
9478 | while (1) | |
8b6a5902 | 9479 | { |
3e4093b6 | 9480 | switch (TREE_CODE (inner)) |
8b6a5902 | 9481 | { |
849421a3 JJ |
9482 | CASE_CONVERT: |
9483 | case NON_LVALUE_EXPR: | |
3e4093b6 | 9484 | case PLUS_EXPR: |
849421a3 | 9485 | case POINTER_PLUS_EXPR: |
3e4093b6 RS |
9486 | inner = TREE_OPERAND (inner, 0); |
9487 | continue; | |
9488 | ||
9489 | case MINUS_EXPR: | |
9490 | /* If the second operand of the MINUS_EXPR has a pointer | |
9491 | type (or is converted from it), this may be valid, so | |
9492 | don't give a warning. */ | |
9493 | { | |
9494 | tree op1 = TREE_OPERAND (inner, 1); | |
8b6a5902 | 9495 | |
3f75a254 | 9496 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) |
1043771b TB |
9497 | && (CONVERT_EXPR_P (op1) |
9498 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
3e4093b6 | 9499 | op1 = TREE_OPERAND (op1, 0); |
8b6a5902 | 9500 | |
3e4093b6 RS |
9501 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
9502 | break; | |
8b6a5902 | 9503 | |
3e4093b6 RS |
9504 | inner = TREE_OPERAND (inner, 0); |
9505 | continue; | |
9506 | } | |
400fbf9f | 9507 | |
3e4093b6 RS |
9508 | case ADDR_EXPR: |
9509 | inner = TREE_OPERAND (inner, 0); | |
c2f4acb7 | 9510 | |
6615c446 | 9511 | while (REFERENCE_CLASS_P (inner) |
22d03525 | 9512 | && !INDIRECT_REF_P (inner)) |
3e4093b6 | 9513 | inner = TREE_OPERAND (inner, 0); |
8b6a5902 | 9514 | |
a2f1f4c3 | 9515 | if (DECL_P (inner) |
3f75a254 JM |
9516 | && !DECL_EXTERNAL (inner) |
9517 | && !TREE_STATIC (inner) | |
3e4093b6 | 9518 | && DECL_CONTEXT (inner) == current_function_decl) |
19fc9faa MP |
9519 | { |
9520 | if (TREE_CODE (inner) == LABEL_DECL) | |
9521 | warning_at (loc, OPT_Wreturn_local_addr, | |
9522 | "function returns address of label"); | |
9523 | else | |
b4dfdc11 MG |
9524 | { |
9525 | warning_at (loc, OPT_Wreturn_local_addr, | |
9526 | "function returns address of local variable"); | |
9527 | tree zero = build_zero_cst (TREE_TYPE (res)); | |
9528 | t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero); | |
9529 | } | |
19fc9faa | 9530 | } |
3e4093b6 | 9531 | break; |
8b6a5902 | 9532 | |
3e4093b6 RS |
9533 | default: |
9534 | break; | |
9535 | } | |
de520661 | 9536 | |
3e4093b6 RS |
9537 | break; |
9538 | } | |
9539 | ||
53fb4de3 | 9540 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
c2255bc4 | 9541 | SET_EXPR_LOCATION (retval, loc); |
ca085fd7 MLI |
9542 | |
9543 | if (warn_sequence_point) | |
9544 | verify_sequence_points (retval); | |
de520661 | 9545 | } |
8b6a5902 | 9546 | |
c2255bc4 | 9547 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); |
0c9b182b JJ |
9548 | TREE_NO_WARNING (ret_stmt) |= no_warning; |
9549 | return add_stmt (ret_stmt); | |
de520661 | 9550 | } |
3e4093b6 RS |
9551 | \f |
9552 | struct c_switch { | |
604f5adf ILT |
9553 | /* The SWITCH_EXPR being built. */ |
9554 | tree switch_expr; | |
a6c0a76c | 9555 | |
89dbed81 | 9556 | /* The original type of the testing expression, i.e. before the |
a6c0a76c SB |
9557 | default conversion is applied. */ |
9558 | tree orig_type; | |
9559 | ||
3e4093b6 RS |
9560 | /* A splay-tree mapping the low element of a case range to the high |
9561 | element, or NULL_TREE if there is no high element. Used to | |
9562 | determine whether or not a new case label duplicates an old case | |
9563 | label. We need a tree, rather than simply a hash table, because | |
9564 | of the GNU case range extension. */ | |
9565 | splay_tree cases; | |
a6c0a76c | 9566 | |
e1b7793c ILT |
9567 | /* The bindings at the point of the switch. This is used for |
9568 | warnings crossing decls when branching to a case label. */ | |
9569 | struct c_spot_bindings *bindings; | |
187230a7 | 9570 | |
3e4093b6 RS |
9571 | /* The next node on the stack. */ |
9572 | struct c_switch *next; | |
b155cfd9 MP |
9573 | |
9574 | /* Remember whether the controlling expression had boolean type | |
9575 | before integer promotions for the sake of -Wswitch-bool. */ | |
9576 | bool bool_cond_p; | |
9577 | ||
9578 | /* Remember whether there was a case value that is outside the | |
9579 | range of the ORIG_TYPE. */ | |
9580 | bool outside_range_p; | |
3e4093b6 | 9581 | }; |
400fbf9f | 9582 | |
3e4093b6 RS |
9583 | /* A stack of the currently active switch statements. The innermost |
9584 | switch statement is on the top of the stack. There is no need to | |
9585 | mark the stack for garbage collection because it is only active | |
9586 | during the processing of the body of a function, and we never | |
9587 | collect at that point. */ | |
de520661 | 9588 | |
506e2710 | 9589 | struct c_switch *c_switch_stack; |
de520661 | 9590 | |
3e4093b6 | 9591 | /* Start a C switch statement, testing expression EXP. Return the new |
c2255bc4 | 9592 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. |
fedfecef | 9593 | SWITCH_COND_LOC is the location of the switch's condition. |
b155cfd9 | 9594 | EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */ |
de520661 | 9595 | |
3e4093b6 | 9596 | tree |
c2255bc4 AH |
9597 | c_start_case (location_t switch_loc, |
9598 | location_t switch_cond_loc, | |
fedfecef | 9599 | tree exp, bool explicit_cast_p) |
de520661 | 9600 | { |
c58e8676 | 9601 | tree orig_type = error_mark_node; |
b155cfd9 | 9602 | bool bool_cond_p = false; |
3e4093b6 | 9603 | struct c_switch *cs; |
2f6e4e97 | 9604 | |
3e4093b6 | 9605 | if (exp != error_mark_node) |
de520661 | 9606 | { |
3e4093b6 RS |
9607 | orig_type = TREE_TYPE (exp); |
9608 | ||
c58e8676 | 9609 | if (!INTEGRAL_TYPE_P (orig_type)) |
de520661 | 9610 | { |
c58e8676 VR |
9611 | if (orig_type != error_mark_node) |
9612 | { | |
c2255bc4 | 9613 | error_at (switch_cond_loc, "switch quantity not an integer"); |
c58e8676 VR |
9614 | orig_type = error_mark_node; |
9615 | } | |
3e4093b6 | 9616 | exp = integer_zero_node; |
de520661 | 9617 | } |
3e4093b6 | 9618 | else |
de520661 | 9619 | { |
c58e8676 | 9620 | tree type = TYPE_MAIN_VARIANT (orig_type); |
fedfecef MP |
9621 | tree e = exp; |
9622 | ||
9623 | /* Warn if the condition has boolean value. */ | |
9624 | while (TREE_CODE (e) == COMPOUND_EXPR) | |
9625 | e = TREE_OPERAND (e, 1); | |
9626 | ||
9627 | if ((TREE_CODE (type) == BOOLEAN_TYPE | |
9628 | || truth_value_p (TREE_CODE (e))) | |
9629 | /* Explicit cast to int suppresses this warning. */ | |
9630 | && !(TREE_CODE (type) == INTEGER_TYPE | |
9631 | && explicit_cast_p)) | |
b155cfd9 | 9632 | bool_cond_p = true; |
8b6a5902 | 9633 | |
8400e75e | 9634 | if (!in_system_header_at (input_location) |
3e4093b6 RS |
9635 | && (type == long_integer_type_node |
9636 | || type == long_unsigned_type_node)) | |
c2255bc4 AH |
9637 | warning_at (switch_cond_loc, |
9638 | OPT_Wtraditional, "%<long%> switch expression not " | |
9639 | "converted to %<int%> in ISO C"); | |
8b6a5902 | 9640 | |
928c19bb | 9641 | exp = c_fully_fold (exp, false, NULL); |
3e4093b6 | 9642 | exp = default_conversion (exp); |
ca085fd7 MLI |
9643 | |
9644 | if (warn_sequence_point) | |
9645 | verify_sequence_points (exp); | |
3e4093b6 RS |
9646 | } |
9647 | } | |
9648 | ||
604f5adf | 9649 | /* Add this new SWITCH_EXPR to the stack. */ |
5d038c4c | 9650 | cs = XNEW (struct c_switch); |
604f5adf | 9651 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); |
c2255bc4 | 9652 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); |
a6c0a76c | 9653 | cs->orig_type = orig_type; |
3e4093b6 | 9654 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
e1b7793c | 9655 | cs->bindings = c_get_switch_bindings (); |
b155cfd9 MP |
9656 | cs->bool_cond_p = bool_cond_p; |
9657 | cs->outside_range_p = false; | |
506e2710 RH |
9658 | cs->next = c_switch_stack; |
9659 | c_switch_stack = cs; | |
3e4093b6 | 9660 | |
604f5adf | 9661 | return add_stmt (cs->switch_expr); |
3e4093b6 RS |
9662 | } |
9663 | ||
c2255bc4 | 9664 | /* Process a case label at location LOC. */ |
3e4093b6 RS |
9665 | |
9666 | tree | |
c2255bc4 | 9667 | do_case (location_t loc, tree low_value, tree high_value) |
3e4093b6 RS |
9668 | { |
9669 | tree label = NULL_TREE; | |
9670 | ||
17cede2e JM |
9671 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) |
9672 | { | |
9673 | low_value = c_fully_fold (low_value, false, NULL); | |
9674 | if (TREE_CODE (low_value) == INTEGER_CST) | |
d033409e | 9675 | pedwarn (loc, OPT_Wpedantic, |
17cede2e JM |
9676 | "case label is not an integer constant expression"); |
9677 | } | |
9678 | ||
9679 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
9680 | { | |
9681 | high_value = c_fully_fold (high_value, false, NULL); | |
9682 | if (TREE_CODE (high_value) == INTEGER_CST) | |
c1771a20 | 9683 | pedwarn (input_location, OPT_Wpedantic, |
17cede2e JM |
9684 | "case label is not an integer constant expression"); |
9685 | } | |
9686 | ||
e1b7793c | 9687 | if (c_switch_stack == NULL) |
187230a7 JM |
9688 | { |
9689 | if (low_value) | |
e1b7793c | 9690 | error_at (loc, "case label not within a switch statement"); |
187230a7 | 9691 | else |
e1b7793c ILT |
9692 | error_at (loc, "%<default%> label not within a switch statement"); |
9693 | return NULL_TREE; | |
187230a7 | 9694 | } |
de520661 | 9695 | |
e1b7793c ILT |
9696 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, |
9697 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
9698 | loc)) | |
9699 | return NULL_TREE; | |
9700 | ||
9701 | label = c_add_case_label (loc, c_switch_stack->cases, | |
9702 | SWITCH_COND (c_switch_stack->switch_expr), | |
9703 | c_switch_stack->orig_type, | |
b155cfd9 MP |
9704 | low_value, high_value, |
9705 | &c_switch_stack->outside_range_p); | |
e1b7793c ILT |
9706 | if (label == error_mark_node) |
9707 | label = NULL_TREE; | |
3e4093b6 RS |
9708 | return label; |
9709 | } | |
de520661 | 9710 | |
083e891e MP |
9711 | /* Finish the switch statement. TYPE is the original type of the |
9712 | controlling expression of the switch, or NULL_TREE. */ | |
de520661 | 9713 | |
3e4093b6 | 9714 | void |
083e891e | 9715 | c_finish_case (tree body, tree type) |
3e4093b6 | 9716 | { |
506e2710 | 9717 | struct c_switch *cs = c_switch_stack; |
fbc315db | 9718 | location_t switch_location; |
3e4093b6 | 9719 | |
604f5adf | 9720 | SWITCH_BODY (cs->switch_expr) = body; |
325c3691 | 9721 | |
6de9cd9a | 9722 | /* Emit warnings as needed. */ |
c2255bc4 | 9723 | switch_location = EXPR_LOCATION (cs->switch_expr); |
fbc315db | 9724 | c_do_switch_warnings (cs->cases, switch_location, |
083e891e | 9725 | type ? type : TREE_TYPE (cs->switch_expr), |
b155cfd9 MP |
9726 | SWITCH_COND (cs->switch_expr), |
9727 | cs->bool_cond_p, cs->outside_range_p); | |
6de9cd9a | 9728 | |
3e4093b6 | 9729 | /* Pop the stack. */ |
506e2710 | 9730 | c_switch_stack = cs->next; |
3e4093b6 | 9731 | splay_tree_delete (cs->cases); |
e1b7793c | 9732 | c_release_switch_bindings (cs->bindings); |
5d038c4c | 9733 | XDELETE (cs); |
de520661 | 9734 | } |
325c3691 | 9735 | \f |
506e2710 RH |
9736 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
9737 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
9738 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
9739 | statement, and was not surrounded with parenthesis. */ | |
325c3691 | 9740 | |
9e51cf9d | 9741 | void |
506e2710 RH |
9742 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
9743 | tree else_block, bool nested_if) | |
325c3691 | 9744 | { |
506e2710 | 9745 | tree stmt; |
325c3691 | 9746 | |
25c22937 BI |
9747 | /* If the condition has array notations, then the rank of the then_block and |
9748 | else_block must be either 0 or be equal to the rank of the condition. If | |
9749 | the condition does not have array notations then break them up as it is | |
9750 | broken up in a normal expression. */ | |
b72271b9 | 9751 | if (flag_cilkplus && contains_array_notation_expr (cond)) |
25c22937 BI |
9752 | { |
9753 | size_t then_rank = 0, cond_rank = 0, else_rank = 0; | |
9754 | if (!find_rank (if_locus, cond, cond, true, &cond_rank)) | |
9755 | return; | |
9756 | if (then_block | |
9757 | && !find_rank (if_locus, then_block, then_block, true, &then_rank)) | |
9758 | return; | |
9759 | if (else_block | |
9760 | && !find_rank (if_locus, else_block, else_block, true, &else_rank)) | |
9761 | return; | |
9762 | if (cond_rank != then_rank && then_rank != 0) | |
9763 | { | |
9764 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9765 | " and the then-block"); | |
9766 | return; | |
9767 | } | |
9768 | else if (cond_rank != else_rank && else_rank != 0) | |
9769 | { | |
9770 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9771 | " and the else-block"); | |
9772 | return; | |
9773 | } | |
9774 | } | |
506e2710 RH |
9775 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
9776 | if (warn_parentheses && nested_if && else_block == NULL) | |
325c3691 | 9777 | { |
506e2710 | 9778 | tree inner_if = then_block; |
16865eaa | 9779 | |
61ada8ae | 9780 | /* We know from the grammar productions that there is an IF nested |
506e2710 RH |
9781 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
9782 | it might not be exactly THEN_BLOCK, but should be the last | |
9783 | non-container statement within. */ | |
9784 | while (1) | |
9785 | switch (TREE_CODE (inner_if)) | |
9786 | { | |
9787 | case COND_EXPR: | |
9788 | goto found; | |
9789 | case BIND_EXPR: | |
9790 | inner_if = BIND_EXPR_BODY (inner_if); | |
9791 | break; | |
9792 | case STATEMENT_LIST: | |
9793 | inner_if = expr_last (then_block); | |
9794 | break; | |
9795 | case TRY_FINALLY_EXPR: | |
9796 | case TRY_CATCH_EXPR: | |
9797 | inner_if = TREE_OPERAND (inner_if, 0); | |
9798 | break; | |
9799 | default: | |
366de0ce | 9800 | gcc_unreachable (); |
506e2710 RH |
9801 | } |
9802 | found: | |
16865eaa | 9803 | |
506e2710 | 9804 | if (COND_EXPR_ELSE (inner_if)) |
fab922b1 MLI |
9805 | warning_at (if_locus, OPT_Wparentheses, |
9806 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
506e2710 | 9807 | } |
16865eaa | 9808 | |
2214de30 | 9809 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); |
a281759f | 9810 | SET_EXPR_LOCATION (stmt, if_locus); |
506e2710 | 9811 | add_stmt (stmt); |
325c3691 RH |
9812 | } |
9813 | ||
506e2710 RH |
9814 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
9815 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
9816 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
61ada8ae | 9817 | the statement controlled by the loop. BLAB is the break label. CLAB is |
506e2710 | 9818 | the continue label. Everything is allowed to be NULL. */ |
325c3691 RH |
9819 | |
9820 | void | |
506e2710 RH |
9821 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
9822 | tree blab, tree clab, bool cond_is_first) | |
325c3691 | 9823 | { |
506e2710 RH |
9824 | tree entry = NULL, exit = NULL, t; |
9825 | ||
e5e44252 AK |
9826 | /* In theory could forbid cilk spawn for loop increment expression, |
9827 | but it should work just fine. */ | |
36536d79 | 9828 | |
28af952a RS |
9829 | /* If the condition is zero don't generate a loop construct. */ |
9830 | if (cond && integer_zerop (cond)) | |
9831 | { | |
9832 | if (cond_is_first) | |
9833 | { | |
9834 | t = build_and_jump (&blab); | |
9835 | SET_EXPR_LOCATION (t, start_locus); | |
9836 | add_stmt (t); | |
9837 | } | |
9838 | } | |
9839 | else | |
506e2710 RH |
9840 | { |
9841 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
c22cacf3 | 9842 | |
506e2710 | 9843 | /* If we have an exit condition, then we build an IF with gotos either |
c22cacf3 MS |
9844 | out of the loop, or to the top of it. If there's no exit condition, |
9845 | then we just build a jump back to the top. */ | |
506e2710 | 9846 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); |
c22cacf3 | 9847 | |
28af952a | 9848 | if (cond && !integer_nonzerop (cond)) |
c22cacf3 MS |
9849 | { |
9850 | /* Canonicalize the loop condition to the end. This means | |
9851 | generating a branch to the loop condition. Reuse the | |
9852 | continue label, if possible. */ | |
9853 | if (cond_is_first) | |
9854 | { | |
9855 | if (incr || !clab) | |
9856 | { | |
9857 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
9858 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
9859 | } | |
9860 | else | |
9861 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
a281759f | 9862 | SET_EXPR_LOCATION (t, start_locus); |
c22cacf3 MS |
9863 | add_stmt (t); |
9864 | } | |
9865 | ||
506e2710 | 9866 | t = build_and_jump (&blab); |
506e2710 | 9867 | if (cond_is_first) |
db3927fb AH |
9868 | exit = fold_build3_loc (start_locus, |
9869 | COND_EXPR, void_type_node, cond, exit, t); | |
506e2710 | 9870 | else |
db3927fb AH |
9871 | exit = fold_build3_loc (input_location, |
9872 | COND_EXPR, void_type_node, cond, exit, t); | |
c22cacf3 MS |
9873 | } |
9874 | ||
506e2710 RH |
9875 | add_stmt (top); |
9876 | } | |
c22cacf3 | 9877 | |
506e2710 RH |
9878 | if (body) |
9879 | add_stmt (body); | |
9880 | if (clab) | |
9881 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
9882 | if (incr) | |
9883 | add_stmt (incr); | |
9884 | if (entry) | |
9885 | add_stmt (entry); | |
9886 | if (exit) | |
9887 | add_stmt (exit); | |
9888 | if (blab) | |
9889 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
325c3691 | 9890 | } |
325c3691 RH |
9891 | |
9892 | tree | |
c2255bc4 | 9893 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) |
325c3691 | 9894 | { |
089efaa4 | 9895 | bool skip; |
506e2710 | 9896 | tree label = *label_p; |
325c3691 | 9897 | |
089efaa4 ILT |
9898 | /* In switch statements break is sometimes stylistically used after |
9899 | a return statement. This can lead to spurious warnings about | |
9900 | control reaching the end of a non-void function when it is | |
9901 | inlined. Note that we are calling block_may_fallthru with | |
9902 | language specific tree nodes; this works because | |
9903 | block_may_fallthru returns true when given something it does not | |
9904 | understand. */ | |
9905 | skip = !block_may_fallthru (cur_stmt_list); | |
9906 | ||
506e2710 | 9907 | if (!label) |
089efaa4 ILT |
9908 | { |
9909 | if (!skip) | |
c2255bc4 | 9910 | *label_p = label = create_artificial_label (loc); |
089efaa4 | 9911 | } |
953ff289 DN |
9912 | else if (TREE_CODE (label) == LABEL_DECL) |
9913 | ; | |
9914 | else switch (TREE_INT_CST_LOW (label)) | |
506e2710 | 9915 | { |
953ff289 | 9916 | case 0: |
506e2710 | 9917 | if (is_break) |
c2255bc4 | 9918 | error_at (loc, "break statement not within loop or switch"); |
506e2710 | 9919 | else |
c2255bc4 | 9920 | error_at (loc, "continue statement not within a loop"); |
506e2710 | 9921 | return NULL_TREE; |
953ff289 DN |
9922 | |
9923 | case 1: | |
9924 | gcc_assert (is_break); | |
c2255bc4 | 9925 | error_at (loc, "break statement used with OpenMP for loop"); |
953ff289 DN |
9926 | return NULL_TREE; |
9927 | ||
c02065fc AH |
9928 | case 2: |
9929 | if (is_break) | |
9930 | error ("break statement within %<#pragma simd%> loop body"); | |
9931 | else | |
9932 | error ("continue statement within %<#pragma simd%> loop body"); | |
9933 | return NULL_TREE; | |
9934 | ||
953ff289 DN |
9935 | default: |
9936 | gcc_unreachable (); | |
506e2710 | 9937 | } |
325c3691 | 9938 | |
089efaa4 ILT |
9939 | if (skip) |
9940 | return NULL_TREE; | |
9941 | ||
2e28e797 JH |
9942 | if (!is_break) |
9943 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
9944 | ||
53fb4de3 | 9945 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
325c3691 RH |
9946 | } |
9947 | ||
506e2710 | 9948 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
3a5b9284 RH |
9949 | |
9950 | static void | |
c2255bc4 | 9951 | emit_side_effect_warnings (location_t loc, tree expr) |
3a5b9284 | 9952 | { |
e6b5a630 RH |
9953 | if (expr == error_mark_node) |
9954 | ; | |
9955 | else if (!TREE_SIDE_EFFECTS (expr)) | |
3a5b9284 RH |
9956 | { |
9957 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
c2255bc4 | 9958 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); |
3a5b9284 | 9959 | } |
789eadcd MP |
9960 | else if (TREE_CODE (expr) == COMPOUND_EXPR) |
9961 | { | |
9962 | tree r = expr; | |
9963 | location_t cloc = loc; | |
9964 | while (TREE_CODE (r) == COMPOUND_EXPR) | |
9965 | { | |
9966 | if (EXPR_HAS_LOCATION (r)) | |
9967 | cloc = EXPR_LOCATION (r); | |
9968 | r = TREE_OPERAND (r, 1); | |
9969 | } | |
9970 | if (!TREE_SIDE_EFFECTS (r) | |
9971 | && !VOID_TYPE_P (TREE_TYPE (r)) | |
9972 | && !CONVERT_EXPR_P (r) | |
cc28fc7f | 9973 | && !TREE_NO_WARNING (r) |
789eadcd MP |
9974 | && !TREE_NO_WARNING (expr)) |
9975 | warning_at (cloc, OPT_Wunused_value, | |
9976 | "right-hand operand of comma expression has no effect"); | |
9977 | } | |
27f33b15 | 9978 | else |
c2255bc4 | 9979 | warn_if_unused_value (expr, loc); |
3a5b9284 RH |
9980 | } |
9981 | ||
506e2710 | 9982 | /* Process an expression as if it were a complete statement. Emit |
c2255bc4 AH |
9983 | diagnostics, but do not call ADD_STMT. LOC is the location of the |
9984 | statement. */ | |
3a5b9284 | 9985 | |
506e2710 | 9986 | tree |
c2255bc4 | 9987 | c_process_expr_stmt (location_t loc, tree expr) |
3a5b9284 | 9988 | { |
056928b2 JJ |
9989 | tree exprv; |
9990 | ||
3a5b9284 | 9991 | if (!expr) |
506e2710 | 9992 | return NULL_TREE; |
3a5b9284 | 9993 | |
928c19bb JM |
9994 | expr = c_fully_fold (expr, false, NULL); |
9995 | ||
3a5b9284 RH |
9996 | if (warn_sequence_point) |
9997 | verify_sequence_points (expr); | |
9998 | ||
9999 | if (TREE_TYPE (expr) != error_mark_node | |
10000 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
10001 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
c2255bc4 | 10002 | error_at (loc, "expression statement has incomplete type"); |
3a5b9284 RH |
10003 | |
10004 | /* If we're not processing a statement expression, warn about unused values. | |
10005 | Warnings for statement expressions will be emitted later, once we figure | |
10006 | out which is the result. */ | |
10007 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
27f33b15 | 10008 | && warn_unused_value) |
c2255bc4 | 10009 | emit_side_effect_warnings (loc, expr); |
3a5b9284 | 10010 | |
056928b2 JJ |
10011 | exprv = expr; |
10012 | while (TREE_CODE (exprv) == COMPOUND_EXPR) | |
10013 | exprv = TREE_OPERAND (exprv, 1); | |
f1a89dd0 JJ |
10014 | while (CONVERT_EXPR_P (exprv)) |
10015 | exprv = TREE_OPERAND (exprv, 0); | |
10016 | if (DECL_P (exprv) | |
10017 | || handled_component_p (exprv) | |
10018 | || TREE_CODE (exprv) == ADDR_EXPR) | |
056928b2 | 10019 | mark_exp_read (exprv); |
fa8351f8 | 10020 | |
3a5b9284 RH |
10021 | /* If the expression is not of a type to which we cannot assign a line |
10022 | number, wrap the thing in a no-op NOP_EXPR. */ | |
6615c446 | 10023 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) |
c2255bc4 AH |
10024 | { |
10025 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
10026 | SET_EXPR_LOCATION (expr, loc); | |
10027 | } | |
506e2710 RH |
10028 | |
10029 | return expr; | |
10030 | } | |
10031 | ||
c2255bc4 AH |
10032 | /* Emit an expression as a statement. LOC is the location of the |
10033 | expression. */ | |
506e2710 RH |
10034 | |
10035 | tree | |
c2255bc4 | 10036 | c_finish_expr_stmt (location_t loc, tree expr) |
506e2710 RH |
10037 | { |
10038 | if (expr) | |
c2255bc4 | 10039 | return add_stmt (c_process_expr_stmt (loc, expr)); |
506e2710 RH |
10040 | else |
10041 | return NULL; | |
3a5b9284 RH |
10042 | } |
10043 | ||
10044 | /* Do the opposite and emit a statement as an expression. To begin, | |
10045 | create a new binding level and return it. */ | |
325c3691 RH |
10046 | |
10047 | tree | |
10048 | c_begin_stmt_expr (void) | |
10049 | { | |
10050 | tree ret; | |
10051 | ||
10052 | /* We must force a BLOCK for this level so that, if it is not expanded | |
10053 | later, there is a way to turn off the entire subtree of blocks that | |
10054 | are contained in it. */ | |
10055 | keep_next_level (); | |
10056 | ret = c_begin_compound_stmt (true); | |
e1b7793c ILT |
10057 | |
10058 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
10059 | ? NULL | |
10060 | : c_switch_stack->bindings); | |
325c3691 RH |
10061 | |
10062 | /* Mark the current statement list as belonging to a statement list. */ | |
10063 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
10064 | ||
10065 | return ret; | |
10066 | } | |
10067 | ||
c2255bc4 AH |
10068 | /* LOC is the location of the compound statement to which this body |
10069 | belongs. */ | |
10070 | ||
325c3691 | 10071 | tree |
c2255bc4 | 10072 | c_finish_stmt_expr (location_t loc, tree body) |
325c3691 | 10073 | { |
3a5b9284 | 10074 | tree last, type, tmp, val; |
325c3691 RH |
10075 | tree *last_p; |
10076 | ||
c2255bc4 | 10077 | body = c_end_compound_stmt (loc, body, true); |
e1b7793c ILT |
10078 | |
10079 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
10080 | ? NULL | |
10081 | : c_switch_stack->bindings); | |
325c3691 | 10082 | |
3a5b9284 RH |
10083 | /* Locate the last statement in BODY. See c_end_compound_stmt |
10084 | about always returning a BIND_EXPR. */ | |
10085 | last_p = &BIND_EXPR_BODY (body); | |
10086 | last = BIND_EXPR_BODY (body); | |
10087 | ||
10088 | continue_searching: | |
325c3691 RH |
10089 | if (TREE_CODE (last) == STATEMENT_LIST) |
10090 | { | |
3a5b9284 RH |
10091 | tree_stmt_iterator i; |
10092 | ||
10093 | /* This can happen with degenerate cases like ({ }). No value. */ | |
10094 | if (!TREE_SIDE_EFFECTS (last)) | |
10095 | return body; | |
10096 | ||
10097 | /* If we're supposed to generate side effects warnings, process | |
10098 | all of the statements except the last. */ | |
27f33b15 | 10099 | if (warn_unused_value) |
325c3691 | 10100 | { |
3a5b9284 | 10101 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
c2255bc4 AH |
10102 | { |
10103 | location_t tloc; | |
10104 | tree t = tsi_stmt (i); | |
10105 | ||
10106 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
10107 | emit_side_effect_warnings (tloc, t); | |
10108 | } | |
325c3691 RH |
10109 | } |
10110 | else | |
3a5b9284 RH |
10111 | i = tsi_last (last); |
10112 | last_p = tsi_stmt_ptr (i); | |
10113 | last = *last_p; | |
325c3691 RH |
10114 | } |
10115 | ||
3a5b9284 RH |
10116 | /* If the end of the list is exception related, then the list was split |
10117 | by a call to push_cleanup. Continue searching. */ | |
10118 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
10119 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
10120 | { | |
10121 | last_p = &TREE_OPERAND (last, 0); | |
10122 | last = *last_p; | |
10123 | goto continue_searching; | |
10124 | } | |
10125 | ||
26d8af35 JM |
10126 | if (last == error_mark_node) |
10127 | return last; | |
10128 | ||
3a5b9284 RH |
10129 | /* In the case that the BIND_EXPR is not necessary, return the |
10130 | expression out from inside it. */ | |
26d8af35 JM |
10131 | if (last == BIND_EXPR_BODY (body) |
10132 | && BIND_EXPR_VARS (body) == NULL) | |
591baeb0 | 10133 | { |
928c19bb JM |
10134 | /* Even if this looks constant, do not allow it in a constant |
10135 | expression. */ | |
e5a94231 | 10136 | last = c_wrap_maybe_const (last, true); |
591baeb0 JM |
10137 | /* Do not warn if the return value of a statement expression is |
10138 | unused. */ | |
928c19bb | 10139 | TREE_NO_WARNING (last) = 1; |
591baeb0 JM |
10140 | return last; |
10141 | } | |
325c3691 RH |
10142 | |
10143 | /* Extract the type of said expression. */ | |
10144 | type = TREE_TYPE (last); | |
325c3691 | 10145 | |
3a5b9284 RH |
10146 | /* If we're not returning a value at all, then the BIND_EXPR that |
10147 | we already have is a fine expression to return. */ | |
10148 | if (!type || VOID_TYPE_P (type)) | |
10149 | return body; | |
10150 | ||
10151 | /* Now that we've located the expression containing the value, it seems | |
10152 | silly to make voidify_wrapper_expr repeat the process. Create a | |
10153 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
b731b390 | 10154 | tmp = create_tmp_var_raw (type); |
3a5b9284 RH |
10155 | |
10156 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
10157 | tree_expr_nonnegative_p giving up immediately. */ | |
10158 | val = last; | |
10159 | if (TREE_CODE (val) == NOP_EXPR | |
10160 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
10161 | val = TREE_OPERAND (val, 0); | |
10162 | ||
53fb4de3 | 10163 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
5e278028 | 10164 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); |
3a5b9284 | 10165 | |
c2255bc4 AH |
10166 | { |
10167 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
10168 | SET_EXPR_LOCATION (t, loc); | |
10169 | return t; | |
10170 | } | |
325c3691 RH |
10171 | } |
10172 | \f | |
10173 | /* Begin and end compound statements. This is as simple as pushing | |
10174 | and popping new statement lists from the tree. */ | |
10175 | ||
10176 | tree | |
10177 | c_begin_compound_stmt (bool do_scope) | |
10178 | { | |
10179 | tree stmt = push_stmt_list (); | |
10180 | if (do_scope) | |
4dfa0342 | 10181 | push_scope (); |
325c3691 RH |
10182 | return stmt; |
10183 | } | |
10184 | ||
c2255bc4 AH |
10185 | /* End a compound statement. STMT is the statement. LOC is the |
10186 | location of the compound statement-- this is usually the location | |
10187 | of the opening brace. */ | |
10188 | ||
325c3691 | 10189 | tree |
c2255bc4 | 10190 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) |
325c3691 RH |
10191 | { |
10192 | tree block = NULL; | |
10193 | ||
10194 | if (do_scope) | |
10195 | { | |
10196 | if (c_dialect_objc ()) | |
10197 | objc_clear_super_receiver (); | |
10198 | block = pop_scope (); | |
10199 | } | |
10200 | ||
10201 | stmt = pop_stmt_list (stmt); | |
c2255bc4 | 10202 | stmt = c_build_bind_expr (loc, block, stmt); |
325c3691 RH |
10203 | |
10204 | /* If this compound statement is nested immediately inside a statement | |
10205 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
10206 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
10207 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
10208 | was really last. */ | |
38e01f9e | 10209 | if (building_stmt_list_p () |
325c3691 RH |
10210 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) |
10211 | && TREE_CODE (stmt) != BIND_EXPR) | |
10212 | { | |
53fb4de3 | 10213 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
325c3691 | 10214 | TREE_SIDE_EFFECTS (stmt) = 1; |
c2255bc4 | 10215 | SET_EXPR_LOCATION (stmt, loc); |
325c3691 RH |
10216 | } |
10217 | ||
10218 | return stmt; | |
10219 | } | |
5a508662 RH |
10220 | |
10221 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
10222 | when the current scope is exited. EH_ONLY is true when this is not | |
10223 | meant to apply to normal control flow transfer. */ | |
10224 | ||
10225 | void | |
c2255bc4 | 10226 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
5a508662 | 10227 | { |
3a5b9284 RH |
10228 | enum tree_code code; |
10229 | tree stmt, list; | |
10230 | bool stmt_expr; | |
10231 | ||
10232 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
c2255bc4 | 10233 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); |
5a508662 | 10234 | add_stmt (stmt); |
3a5b9284 RH |
10235 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
10236 | list = push_stmt_list (); | |
10237 | TREE_OPERAND (stmt, 0) = list; | |
10238 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
5a508662 | 10239 | } |
325c3691 | 10240 | \f |
3e4093b6 RS |
10241 | /* Build a binary-operation expression without default conversions. |
10242 | CODE is the kind of expression to build. | |
ba47d38d | 10243 | LOCATION is the operator's location. |
3e4093b6 RS |
10244 | This function differs from `build' in several ways: |
10245 | the data type of the result is computed and recorded in it, | |
10246 | warnings are generated if arg data types are invalid, | |
10247 | special handling for addition and subtraction of pointers is known, | |
10248 | and some optimization is done (operations on narrow ints | |
10249 | are done in the narrower type when that gives the same result). | |
10250 | Constant folding is also done before the result is returned. | |
de520661 | 10251 | |
3e4093b6 RS |
10252 | Note that the operands will never have enumeral types, or function |
10253 | or array types, because either they will have the default conversions | |
10254 | performed or they have both just been converted to some other type in which | |
10255 | the arithmetic is to be done. */ | |
10256 | ||
10257 | tree | |
ba47d38d AH |
10258 | build_binary_op (location_t location, enum tree_code code, |
10259 | tree orig_op0, tree orig_op1, int convert_p) | |
de520661 | 10260 | { |
8ce94e44 JM |
10261 | tree type0, type1, orig_type0, orig_type1; |
10262 | tree eptype; | |
3e4093b6 RS |
10263 | enum tree_code code0, code1; |
10264 | tree op0, op1; | |
c9f9eb5d | 10265 | tree ret = error_mark_node; |
4de67c26 | 10266 | const char *invalid_op_diag; |
4d84fe7c | 10267 | bool op0_int_operands, op1_int_operands; |
928c19bb | 10268 | bool int_const, int_const_or_overflow, int_operands; |
b62acd60 | 10269 | |
3e4093b6 RS |
10270 | /* Expression code to give to the expression when it is built. |
10271 | Normally this is CODE, which is what the caller asked for, | |
10272 | but in some special cases we change it. */ | |
10273 | enum tree_code resultcode = code; | |
8b6a5902 | 10274 | |
3e4093b6 RS |
10275 | /* Data type in which the computation is to be performed. |
10276 | In the simplest cases this is the common type of the arguments. */ | |
10277 | tree result_type = NULL; | |
10278 | ||
8ce94e44 JM |
10279 | /* When the computation is in excess precision, the type of the |
10280 | final EXCESS_PRECISION_EXPR. */ | |
2d2e923f | 10281 | tree semantic_result_type = NULL; |
8ce94e44 | 10282 | |
3e4093b6 RS |
10283 | /* Nonzero means operands have already been type-converted |
10284 | in whatever way is necessary. | |
10285 | Zero means they need to be converted to RESULT_TYPE. */ | |
10286 | int converted = 0; | |
10287 | ||
10288 | /* Nonzero means create the expression with this type, rather than | |
10289 | RESULT_TYPE. */ | |
10290 | tree build_type = 0; | |
10291 | ||
10292 | /* Nonzero means after finally constructing the expression | |
10293 | convert it to this type. */ | |
10294 | tree final_type = 0; | |
10295 | ||
10296 | /* Nonzero if this is an operation like MIN or MAX which can | |
10297 | safely be computed in short if both args are promoted shorts. | |
10298 | Also implies COMMON. | |
10299 | -1 indicates a bitwise operation; this makes a difference | |
10300 | in the exact conditions for when it is safe to do the operation | |
10301 | in a narrower mode. */ | |
10302 | int shorten = 0; | |
10303 | ||
10304 | /* Nonzero if this is a comparison operation; | |
10305 | if both args are promoted shorts, compare the original shorts. | |
10306 | Also implies COMMON. */ | |
10307 | int short_compare = 0; | |
10308 | ||
10309 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
10310 | original short and then promoted if the operand is a promoted short. */ | |
10311 | int short_shift = 0; | |
10312 | ||
10313 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
10314 | int common = 0; | |
10315 | ||
58393038 ZL |
10316 | /* True means types are compatible as far as ObjC is concerned. */ |
10317 | bool objc_ok; | |
10318 | ||
8ce94e44 JM |
10319 | /* True means this is an arithmetic operation that may need excess |
10320 | precision. */ | |
10321 | bool may_need_excess_precision; | |
10322 | ||
180f8dbb JM |
10323 | /* True means this is a boolean operation that converts both its |
10324 | operands to truth-values. */ | |
10325 | bool boolean_op = false; | |
10326 | ||
de5a5fa1 MP |
10327 | /* Remember whether we're doing / or %. */ |
10328 | bool doing_div_or_mod = false; | |
10329 | ||
10330 | /* Remember whether we're doing << or >>. */ | |
10331 | bool doing_shift = false; | |
10332 | ||
10333 | /* Tree holding instrumentation expression. */ | |
10334 | tree instrument_expr = NULL; | |
10335 | ||
ba47d38d AH |
10336 | if (location == UNKNOWN_LOCATION) |
10337 | location = input_location; | |
10338 | ||
4d84fe7c JM |
10339 | op0 = orig_op0; |
10340 | op1 = orig_op1; | |
10341 | ||
10342 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
10343 | if (op0_int_operands) | |
10344 | op0 = remove_c_maybe_const_expr (op0); | |
10345 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
10346 | if (op1_int_operands) | |
10347 | op1 = remove_c_maybe_const_expr (op1); | |
10348 | int_operands = (op0_int_operands && op1_int_operands); | |
928c19bb JM |
10349 | if (int_operands) |
10350 | { | |
10351 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
10352 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
10353 | int_const = (int_const_or_overflow | |
10354 | && !TREE_OVERFLOW (orig_op0) | |
10355 | && !TREE_OVERFLOW (orig_op1)); | |
10356 | } | |
10357 | else | |
10358 | int_const = int_const_or_overflow = false; | |
10359 | ||
0e3a99ae | 10360 | /* Do not apply default conversion in mixed vector/scalar expression. */ |
f90e8e2e | 10361 | if (convert_p |
31521951 | 10362 | && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1))) |
790e9490 | 10363 | { |
4d84fe7c JM |
10364 | op0 = default_conversion (op0); |
10365 | op1 = default_conversion (op1); | |
790e9490 RS |
10366 | } |
10367 | ||
36536d79 BI |
10368 | /* When Cilk Plus is enabled and there are array notations inside op0, then |
10369 | we check to see if there are builtin array notation functions. If | |
10370 | so, then we take on the type of the array notation inside it. */ | |
b72271b9 | 10371 | if (flag_cilkplus && contains_array_notation_expr (op0)) |
36536d79 BI |
10372 | orig_type0 = type0 = find_correct_array_notation_type (op0); |
10373 | else | |
10374 | orig_type0 = type0 = TREE_TYPE (op0); | |
10375 | ||
b72271b9 | 10376 | if (flag_cilkplus && contains_array_notation_expr (op1)) |
36536d79 BI |
10377 | orig_type1 = type1 = find_correct_array_notation_type (op1); |
10378 | else | |
10379 | orig_type1 = type1 = TREE_TYPE (op1); | |
91fa3c30 | 10380 | |
3e4093b6 RS |
10381 | /* The expression codes of the data types of the arguments tell us |
10382 | whether the arguments are integers, floating, pointers, etc. */ | |
10383 | code0 = TREE_CODE (type0); | |
10384 | code1 = TREE_CODE (type1); | |
10385 | ||
10386 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
10387 | STRIP_TYPE_NOPS (op0); | |
10388 | STRIP_TYPE_NOPS (op1); | |
10389 | ||
10390 | /* If an error was already reported for one of the arguments, | |
10391 | avoid reporting another error. */ | |
10392 | ||
10393 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
10394 | return error_mark_node; | |
10395 | ||
1807ffc1 MS |
10396 | if (code0 == POINTER_TYPE |
10397 | && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0))) | |
10398 | return error_mark_node; | |
10399 | ||
10400 | if (code1 == POINTER_TYPE | |
10401 | && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1))) | |
10402 | return error_mark_node; | |
10403 | ||
4de67c26 JM |
10404 | if ((invalid_op_diag |
10405 | = targetm.invalid_binary_op (code, type0, type1))) | |
10406 | { | |
ba47d38d | 10407 | error_at (location, invalid_op_diag); |
4de67c26 JM |
10408 | return error_mark_node; |
10409 | } | |
10410 | ||
8ce94e44 JM |
10411 | switch (code) |
10412 | { | |
10413 | case PLUS_EXPR: | |
10414 | case MINUS_EXPR: | |
10415 | case MULT_EXPR: | |
10416 | case TRUNC_DIV_EXPR: | |
10417 | case CEIL_DIV_EXPR: | |
10418 | case FLOOR_DIV_EXPR: | |
10419 | case ROUND_DIV_EXPR: | |
10420 | case EXACT_DIV_EXPR: | |
10421 | may_need_excess_precision = true; | |
10422 | break; | |
10423 | default: | |
10424 | may_need_excess_precision = false; | |
10425 | break; | |
10426 | } | |
10427 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
10428 | { | |
10429 | op0 = TREE_OPERAND (op0, 0); | |
10430 | type0 = TREE_TYPE (op0); | |
10431 | } | |
10432 | else if (may_need_excess_precision | |
10433 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
10434 | { | |
10435 | type0 = eptype; | |
10436 | op0 = convert (eptype, op0); | |
10437 | } | |
10438 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
10439 | { | |
10440 | op1 = TREE_OPERAND (op1, 0); | |
10441 | type1 = TREE_TYPE (op1); | |
10442 | } | |
10443 | else if (may_need_excess_precision | |
10444 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
10445 | { | |
10446 | type1 = eptype; | |
10447 | op1 = convert (eptype, op1); | |
10448 | } | |
10449 | ||
58393038 ZL |
10450 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); |
10451 | ||
0e3a99ae AS |
10452 | /* In case when one of the operands of the binary operation is |
10453 | a vector and another is a scalar -- convert scalar to vector. */ | |
10454 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
10455 | { | |
a212e43f MG |
10456 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, |
10457 | true); | |
f90e8e2e | 10458 | |
0e3a99ae AS |
10459 | switch (convert_flag) |
10460 | { | |
10461 | case stv_error: | |
10462 | return error_mark_node; | |
10463 | case stv_firstarg: | |
10464 | { | |
10465 | bool maybe_const = true; | |
10466 | tree sc; | |
10467 | sc = c_fully_fold (op0, false, &maybe_const); | |
10468 | sc = save_expr (sc); | |
10469 | sc = convert (TREE_TYPE (type1), sc); | |
10470 | op0 = build_vector_from_val (type1, sc); | |
10471 | if (!maybe_const) | |
10472 | op0 = c_wrap_maybe_const (op0, true); | |
10473 | orig_type0 = type0 = TREE_TYPE (op0); | |
10474 | code0 = TREE_CODE (type0); | |
10475 | converted = 1; | |
10476 | break; | |
10477 | } | |
10478 | case stv_secondarg: | |
10479 | { | |
10480 | bool maybe_const = true; | |
10481 | tree sc; | |
10482 | sc = c_fully_fold (op1, false, &maybe_const); | |
10483 | sc = save_expr (sc); | |
10484 | sc = convert (TREE_TYPE (type0), sc); | |
10485 | op1 = build_vector_from_val (type0, sc); | |
10486 | if (!maybe_const) | |
54b9f838 | 10487 | op1 = c_wrap_maybe_const (op1, true); |
0e3a99ae AS |
10488 | orig_type1 = type1 = TREE_TYPE (op1); |
10489 | code1 = TREE_CODE (type1); | |
10490 | converted = 1; | |
10491 | break; | |
10492 | } | |
10493 | default: | |
10494 | break; | |
10495 | } | |
10496 | } | |
10497 | ||
3e4093b6 | 10498 | switch (code) |
de520661 | 10499 | { |
3e4093b6 RS |
10500 | case PLUS_EXPR: |
10501 | /* Handle the pointer + int case. */ | |
10502 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 10503 | { |
db3927fb | 10504 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); |
c9f9eb5d AH |
10505 | goto return_build_binary_op; |
10506 | } | |
3e4093b6 | 10507 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) |
c9f9eb5d | 10508 | { |
db3927fb | 10509 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); |
c9f9eb5d AH |
10510 | goto return_build_binary_op; |
10511 | } | |
fe67cf58 | 10512 | else |
3e4093b6 RS |
10513 | common = 1; |
10514 | break; | |
400fbf9f | 10515 | |
3e4093b6 RS |
10516 | case MINUS_EXPR: |
10517 | /* Subtraction of two similar pointers. | |
10518 | We must subtract them as integers, then divide by object size. */ | |
10519 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
744aa42f | 10520 | && comp_target_types (location, type0, type1)) |
c9f9eb5d | 10521 | { |
db3927fb | 10522 | ret = pointer_diff (location, op0, op1); |
c9f9eb5d AH |
10523 | goto return_build_binary_op; |
10524 | } | |
3e4093b6 RS |
10525 | /* Handle pointer minus int. Just like pointer plus int. */ |
10526 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 10527 | { |
db3927fb | 10528 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); |
c9f9eb5d AH |
10529 | goto return_build_binary_op; |
10530 | } | |
3e4093b6 RS |
10531 | else |
10532 | common = 1; | |
10533 | break; | |
8b6a5902 | 10534 | |
3e4093b6 RS |
10535 | case MULT_EXPR: |
10536 | common = 1; | |
10537 | break; | |
10538 | ||
10539 | case TRUNC_DIV_EXPR: | |
10540 | case CEIL_DIV_EXPR: | |
10541 | case FLOOR_DIV_EXPR: | |
10542 | case ROUND_DIV_EXPR: | |
10543 | case EXACT_DIV_EXPR: | |
de5a5fa1 | 10544 | doing_div_or_mod = true; |
c9f9eb5d | 10545 | warn_for_div_by_zero (location, op1); |
3e4093b6 RS |
10546 | |
10547 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 10548 | || code0 == FIXED_POINT_TYPE |
3e4093b6 RS |
10549 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
10550 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
ab22c1fa | 10551 | || code1 == FIXED_POINT_TYPE |
3e4093b6 | 10552 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 10553 | { |
5bed876a AH |
10554 | enum tree_code tcode0 = code0, tcode1 = code1; |
10555 | ||
3a021db2 | 10556 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
5bed876a | 10557 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
3a021db2 | 10558 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) |
5bed876a | 10559 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
3a021db2 | 10560 | |
ab22c1fa CF |
10561 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) |
10562 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
3e4093b6 RS |
10563 | resultcode = RDIV_EXPR; |
10564 | else | |
10565 | /* Although it would be tempting to shorten always here, that | |
10566 | loses on some targets, since the modulo instruction is | |
10567 | undefined if the quotient can't be represented in the | |
10568 | computation mode. We shorten only if unsigned or if | |
10569 | dividing by something we know != -1. */ | |
8df83eae | 10570 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 10571 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 10572 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
10573 | common = 1; |
10574 | } | |
10575 | break; | |
de520661 | 10576 | |
3e4093b6 | 10577 | case BIT_AND_EXPR: |
3e4093b6 RS |
10578 | case BIT_IOR_EXPR: |
10579 | case BIT_XOR_EXPR: | |
10580 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
10581 | shorten = -1; | |
9ef0c8d9 AP |
10582 | /* Allow vector types which are not floating point types. */ |
10583 | else if (code0 == VECTOR_TYPE | |
10584 | && code1 == VECTOR_TYPE | |
10585 | && !VECTOR_FLOAT_TYPE_P (type0) | |
10586 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
3e4093b6 RS |
10587 | common = 1; |
10588 | break; | |
10589 | ||
10590 | case TRUNC_MOD_EXPR: | |
10591 | case FLOOR_MOD_EXPR: | |
de5a5fa1 | 10592 | doing_div_or_mod = true; |
c9f9eb5d | 10593 | warn_for_div_by_zero (location, op1); |
de520661 | 10594 | |
5cfd5d9b AP |
10595 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
10596 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
10597 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
10598 | common = 1; | |
10599 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3e4093b6 RS |
10600 | { |
10601 | /* Although it would be tempting to shorten always here, that loses | |
10602 | on some targets, since the modulo instruction is undefined if the | |
10603 | quotient can't be represented in the computation mode. We shorten | |
10604 | only if unsigned or if dividing by something we know != -1. */ | |
8df83eae | 10605 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 10606 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 10607 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
10608 | common = 1; |
10609 | } | |
10610 | break; | |
de520661 | 10611 | |
3e4093b6 RS |
10612 | case TRUTH_ANDIF_EXPR: |
10613 | case TRUTH_ORIF_EXPR: | |
10614 | case TRUTH_AND_EXPR: | |
10615 | case TRUTH_OR_EXPR: | |
10616 | case TRUTH_XOR_EXPR: | |
10617 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
ab22c1fa CF |
10618 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
10619 | || code0 == FIXED_POINT_TYPE) | |
3e4093b6 | 10620 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE |
ab22c1fa CF |
10621 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE |
10622 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10623 | { |
10624 | /* Result of these operations is always an int, | |
10625 | but that does not mean the operands should be | |
10626 | converted to ints! */ | |
10627 | result_type = integer_type_node; | |
a27d595d JM |
10628 | if (op0_int_operands) |
10629 | { | |
10630 | op0 = c_objc_common_truthvalue_conversion (location, orig_op0); | |
10631 | op0 = remove_c_maybe_const_expr (op0); | |
10632 | } | |
10633 | else | |
10634 | op0 = c_objc_common_truthvalue_conversion (location, op0); | |
10635 | if (op1_int_operands) | |
10636 | { | |
10637 | op1 = c_objc_common_truthvalue_conversion (location, orig_op1); | |
10638 | op1 = remove_c_maybe_const_expr (op1); | |
10639 | } | |
10640 | else | |
10641 | op1 = c_objc_common_truthvalue_conversion (location, op1); | |
3e4093b6 | 10642 | converted = 1; |
180f8dbb | 10643 | boolean_op = true; |
3e4093b6 | 10644 | } |
928c19bb JM |
10645 | if (code == TRUTH_ANDIF_EXPR) |
10646 | { | |
10647 | int_const_or_overflow = (int_operands | |
10648 | && TREE_CODE (orig_op0) == INTEGER_CST | |
10649 | && (op0 == truthvalue_false_node | |
10650 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
10651 | int_const = (int_const_or_overflow | |
10652 | && !TREE_OVERFLOW (orig_op0) | |
10653 | && (op0 == truthvalue_false_node | |
10654 | || !TREE_OVERFLOW (orig_op1))); | |
10655 | } | |
10656 | else if (code == TRUTH_ORIF_EXPR) | |
10657 | { | |
10658 | int_const_or_overflow = (int_operands | |
10659 | && TREE_CODE (orig_op0) == INTEGER_CST | |
10660 | && (op0 == truthvalue_true_node | |
10661 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
10662 | int_const = (int_const_or_overflow | |
10663 | && !TREE_OVERFLOW (orig_op0) | |
10664 | && (op0 == truthvalue_true_node | |
10665 | || !TREE_OVERFLOW (orig_op1))); | |
10666 | } | |
3e4093b6 | 10667 | break; |
eba80994 | 10668 | |
3e4093b6 RS |
10669 | /* Shift operations: result has same type as first operand; |
10670 | always convert second operand to int. | |
10671 | Also set SHORT_SHIFT if shifting rightward. */ | |
de520661 | 10672 | |
3e4093b6 | 10673 | case RSHIFT_EXPR: |
f87bd04b AS |
10674 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
10675 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
10676 | { | |
10677 | result_type = type0; | |
10678 | converted = 1; | |
10679 | } | |
10680 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
451b5e48 MP |
10681 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
10682 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
10683 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
f87bd04b AS |
10684 | { |
10685 | result_type = type0; | |
10686 | converted = 1; | |
10687 | } | |
10688 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
451b5e48 | 10689 | && code1 == INTEGER_TYPE) |
3e4093b6 | 10690 | { |
de5a5fa1 | 10691 | doing_shift = true; |
928c19bb | 10692 | if (TREE_CODE (op1) == INTEGER_CST) |
b62acd60 | 10693 | { |
3e4093b6 | 10694 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
10695 | { |
10696 | int_const = false; | |
7d882b83 | 10697 | if (c_inhibit_evaluation_warnings == 0) |
13c21655 PC |
10698 | warning_at (location, OPT_Wshift_count_negative, |
10699 | "right shift count is negative"); | |
928c19bb | 10700 | } |
3e4093b6 | 10701 | else |
bbb818c6 | 10702 | { |
3f75a254 | 10703 | if (!integer_zerop (op1)) |
3e4093b6 RS |
10704 | short_shift = 1; |
10705 | ||
10706 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
928c19bb JM |
10707 | { |
10708 | int_const = false; | |
7d882b83 | 10709 | if (c_inhibit_evaluation_warnings == 0) |
13c21655 PC |
10710 | warning_at (location, OPT_Wshift_count_overflow, |
10711 | "right shift count >= width of type"); | |
928c19bb | 10712 | } |
bbb818c6 | 10713 | } |
b62acd60 | 10714 | } |
de520661 | 10715 | |
3e4093b6 RS |
10716 | /* Use the type of the value to be shifted. */ |
10717 | result_type = type0; | |
3e4093b6 RS |
10718 | /* Avoid converting op1 to result_type later. */ |
10719 | converted = 1; | |
400fbf9f | 10720 | } |
3e4093b6 | 10721 | break; |
253b6b82 | 10722 | |
3e4093b6 | 10723 | case LSHIFT_EXPR: |
f87bd04b AS |
10724 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
10725 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
10726 | { | |
10727 | result_type = type0; | |
10728 | converted = 1; | |
10729 | } | |
10730 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
451b5e48 MP |
10731 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE |
10732 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
10733 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
f87bd04b AS |
10734 | { |
10735 | result_type = type0; | |
10736 | converted = 1; | |
10737 | } | |
10738 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
451b5e48 | 10739 | && code1 == INTEGER_TYPE) |
3e4093b6 | 10740 | { |
de5a5fa1 | 10741 | doing_shift = true; |
0173bd2a MP |
10742 | if (TREE_CODE (op0) == INTEGER_CST |
10743 | && tree_int_cst_sgn (op0) < 0) | |
10744 | { | |
10745 | /* Don't reject a left shift of a negative value in a context | |
10746 | where a constant expression is needed in C90. */ | |
10747 | if (flag_isoc99) | |
10748 | int_const = false; | |
10749 | if (c_inhibit_evaluation_warnings == 0) | |
10750 | warning_at (location, OPT_Wshift_negative_value, | |
10751 | "left shift of negative value"); | |
10752 | } | |
928c19bb | 10753 | if (TREE_CODE (op1) == INTEGER_CST) |
de520661 | 10754 | { |
3e4093b6 | 10755 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
10756 | { |
10757 | int_const = false; | |
7d882b83 | 10758 | if (c_inhibit_evaluation_warnings == 0) |
13c21655 PC |
10759 | warning_at (location, OPT_Wshift_count_negative, |
10760 | "left shift count is negative"); | |
928c19bb | 10761 | } |
3e4093b6 | 10762 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
928c19bb JM |
10763 | { |
10764 | int_const = false; | |
7d882b83 | 10765 | if (c_inhibit_evaluation_warnings == 0) |
13c21655 PC |
10766 | warning_at (location, OPT_Wshift_count_overflow, |
10767 | "left shift count >= width of type"); | |
928c19bb | 10768 | } |
451b5e48 MP |
10769 | else if (TREE_CODE (op0) == INTEGER_CST |
10770 | && maybe_warn_shift_overflow (location, op0, op1) | |
10771 | && flag_isoc99) | |
10772 | int_const = false; | |
94ba5069 | 10773 | } |
de520661 | 10774 | |
3e4093b6 RS |
10775 | /* Use the type of the value to be shifted. */ |
10776 | result_type = type0; | |
3e4093b6 RS |
10777 | /* Avoid converting op1 to result_type later. */ |
10778 | converted = 1; | |
400fbf9f | 10779 | } |
3e4093b6 | 10780 | break; |
de520661 | 10781 | |
3e4093b6 RS |
10782 | case EQ_EXPR: |
10783 | case NE_EXPR: | |
d246ab4f AS |
10784 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10785 | { | |
10786 | tree intt; | |
0af94e6f | 10787 | if (!vector_types_compatible_elements_p (type0, type1)) |
d246ab4f AS |
10788 | { |
10789 | error_at (location, "comparing vectors with different " | |
10790 | "element types"); | |
10791 | return error_mark_node; | |
10792 | } | |
10793 | ||
10794 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10795 | { | |
10796 | error_at (location, "comparing vectors with different " | |
10797 | "number of elements"); | |
10798 | return error_mark_node; | |
10799 | } | |
10800 | ||
10801 | /* Always construct signed integer vector type. */ | |
10802 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10803 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10804 | result_type = build_opaque_vector_type (intt, | |
10805 | TYPE_VECTOR_SUBPARTS (type0)); | |
10806 | converted = 1; | |
10807 | break; | |
10808 | } | |
ae311566 | 10809 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) |
ba47d38d AH |
10810 | warning_at (location, |
10811 | OPT_Wfloat_equal, | |
10812 | "comparing floating point with == or != is unsafe"); | |
3e4093b6 RS |
10813 | /* Result of comparison is always int, |
10814 | but don't convert the args to int! */ | |
10815 | build_type = integer_type_node; | |
10816 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 10817 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) |
3e4093b6 | 10818 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
ab22c1fa | 10819 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) |
3e4093b6 | 10820 | short_compare = 1; |
637f1455 SZ |
10821 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
10822 | { | |
dbb68221 MW |
10823 | if (warn_nonnull |
10824 | && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0)) | |
10825 | warning_at (location, OPT_Wnonnull, | |
10826 | "nonnull argument %qD compared to NULL", op0); | |
10827 | ||
637f1455 SZ |
10828 | if (TREE_CODE (op0) == ADDR_EXPR |
10829 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) | |
10830 | { | |
10831 | if (code == EQ_EXPR) | |
10832 | warning_at (location, | |
10833 | OPT_Waddress, | |
10834 | "the comparison will always evaluate as %<false%> " | |
10835 | "for the address of %qD will never be NULL", | |
10836 | TREE_OPERAND (op0, 0)); | |
10837 | else | |
10838 | warning_at (location, | |
10839 | OPT_Waddress, | |
10840 | "the comparison will always evaluate as %<true%> " | |
10841 | "for the address of %qD will never be NULL", | |
10842 | TREE_OPERAND (op0, 0)); | |
10843 | } | |
10844 | result_type = type0; | |
10845 | } | |
10846 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) | |
10847 | { | |
dbb68221 MW |
10848 | if (warn_nonnull |
10849 | && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1)) | |
10850 | warning_at (location, OPT_Wnonnull, | |
10851 | "nonnull argument %qD compared to NULL", op1); | |
10852 | ||
637f1455 SZ |
10853 | if (TREE_CODE (op1) == ADDR_EXPR |
10854 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) | |
10855 | { | |
10856 | if (code == EQ_EXPR) | |
10857 | warning_at (location, | |
f90e8e2e | 10858 | OPT_Waddress, |
637f1455 SZ |
10859 | "the comparison will always evaluate as %<false%> " |
10860 | "for the address of %qD will never be NULL", | |
10861 | TREE_OPERAND (op1, 0)); | |
10862 | else | |
10863 | warning_at (location, | |
10864 | OPT_Waddress, | |
10865 | "the comparison will always evaluate as %<true%> " | |
10866 | "for the address of %qD will never be NULL", | |
10867 | TREE_OPERAND (op1, 0)); | |
10868 | } | |
10869 | result_type = type1; | |
10870 | } | |
3e4093b6 RS |
10871 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
10872 | { | |
10873 | tree tt0 = TREE_TYPE (type0); | |
10874 | tree tt1 = TREE_TYPE (type1); | |
36c5e70a BE |
10875 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); |
10876 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
10877 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
10878 | ||
3e4093b6 RS |
10879 | /* Anything compares with void *. void * compares with anything. |
10880 | Otherwise, the targets must be compatible | |
10881 | and both must be object or both incomplete. */ | |
744aa42f | 10882 | if (comp_target_types (location, type0, type1)) |
10bc1b1b | 10883 | result_type = common_pointer_type (type0, type1); |
36c5e70a BE |
10884 | else if (!addr_space_superset (as0, as1, &as_common)) |
10885 | { | |
10886 | error_at (location, "comparison of pointers to " | |
10887 | "disjoint address spaces"); | |
10888 | return error_mark_node; | |
10889 | } | |
267bac10 | 10890 | else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0)) |
ee2990e7 | 10891 | { |
36c5e70a | 10892 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) |
c1771a20 | 10893 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10894 | "comparison of %<void *%> with function pointer"); |
ee2990e7 | 10895 | } |
267bac10 | 10896 | else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1)) |
e6834654 | 10897 | { |
36c5e70a | 10898 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) |
c1771a20 | 10899 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10900 | "comparison of %<void *%> with function pointer"); |
e6834654 | 10901 | } |
3e4093b6 | 10902 | else |
58393038 ZL |
10903 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
10904 | if (!objc_ok) | |
ba47d38d | 10905 | pedwarn (location, 0, |
509c9d60 | 10906 | "comparison of distinct pointer types lacks a cast"); |
e6834654 | 10907 | |
3e4093b6 | 10908 | if (result_type == NULL_TREE) |
36c5e70a BE |
10909 | { |
10910 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
10911 | result_type = build_pointer_type | |
10912 | (build_qualified_type (void_type_node, qual)); | |
10913 | } | |
e6834654 | 10914 | } |
3e4093b6 | 10915 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
de520661 | 10916 | { |
3e4093b6 | 10917 | result_type = type0; |
ba47d38d | 10918 | pedwarn (location, 0, "comparison between pointer and integer"); |
de520661 | 10919 | } |
3e4093b6 | 10920 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
8b6a5902 | 10921 | { |
3e4093b6 | 10922 | result_type = type1; |
ba47d38d | 10923 | pedwarn (location, 0, "comparison between pointer and integer"); |
8b6a5902 | 10924 | } |
04159acf MP |
10925 | if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE |
10926 | || truth_value_p (TREE_CODE (orig_op0))) | |
10927 | ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE | |
10928 | || truth_value_p (TREE_CODE (orig_op1)))) | |
10929 | maybe_warn_bool_compare (location, code, orig_op0, orig_op1); | |
3e4093b6 | 10930 | break; |
8b6a5902 | 10931 | |
3e4093b6 RS |
10932 | case LE_EXPR: |
10933 | case GE_EXPR: | |
10934 | case LT_EXPR: | |
10935 | case GT_EXPR: | |
d246ab4f AS |
10936 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10937 | { | |
10938 | tree intt; | |
0af94e6f | 10939 | if (!vector_types_compatible_elements_p (type0, type1)) |
d246ab4f AS |
10940 | { |
10941 | error_at (location, "comparing vectors with different " | |
10942 | "element types"); | |
10943 | return error_mark_node; | |
10944 | } | |
10945 | ||
10946 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10947 | { | |
10948 | error_at (location, "comparing vectors with different " | |
10949 | "number of elements"); | |
10950 | return error_mark_node; | |
10951 | } | |
10952 | ||
10953 | /* Always construct signed integer vector type. */ | |
10954 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10955 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10956 | result_type = build_opaque_vector_type (intt, | |
10957 | TYPE_VECTOR_SUBPARTS (type0)); | |
10958 | converted = 1; | |
10959 | break; | |
10960 | } | |
3e4093b6 | 10961 | build_type = integer_type_node; |
ab22c1fa CF |
10962 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
10963 | || code0 == FIXED_POINT_TYPE) | |
10964 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
10965 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10966 | short_compare = 1; |
10967 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
10968 | { | |
36c5e70a BE |
10969 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); |
10970 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
10971 | addr_space_t as_common; | |
10972 | ||
744aa42f | 10973 | if (comp_target_types (location, type0, type1)) |
3e4093b6 | 10974 | { |
10bc1b1b | 10975 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
10976 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
10977 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
ba47d38d | 10978 | pedwarn (location, 0, |
509c9d60 | 10979 | "comparison of complete and incomplete pointers"); |
fcf73884 | 10980 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) |
c1771a20 | 10981 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10982 | "ordered comparisons of pointers to functions"); |
d42ba3b8 SZ |
10983 | else if (null_pointer_constant_p (orig_op0) |
10984 | || null_pointer_constant_p (orig_op1)) | |
10985 | warning_at (location, OPT_Wextra, | |
10986 | "ordered comparison of pointer with null pointer"); | |
10987 | ||
3e4093b6 | 10988 | } |
36c5e70a BE |
10989 | else if (!addr_space_superset (as0, as1, &as_common)) |
10990 | { | |
10991 | error_at (location, "comparison of pointers to " | |
10992 | "disjoint address spaces"); | |
10993 | return error_mark_node; | |
10994 | } | |
3e4093b6 RS |
10995 | else |
10996 | { | |
36c5e70a BE |
10997 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
10998 | result_type = build_pointer_type | |
10999 | (build_qualified_type (void_type_node, qual)); | |
ba47d38d | 11000 | pedwarn (location, 0, |
509c9d60 | 11001 | "comparison of distinct pointer types lacks a cast"); |
3e4093b6 RS |
11002 | } |
11003 | } | |
6aa3c60d | 11004 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
3e4093b6 RS |
11005 | { |
11006 | result_type = type0; | |
fcf73884 | 11007 | if (pedantic) |
c1771a20 | 11008 | pedwarn (location, OPT_Wpedantic, |
fcf73884 MLI |
11009 | "ordered comparison of pointer with integer zero"); |
11010 | else if (extra_warnings) | |
ba47d38d | 11011 | warning_at (location, OPT_Wextra, |
d42ba3b8 | 11012 | "ordered comparison of pointer with integer zero"); |
3e4093b6 | 11013 | } |
6aa3c60d | 11014 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
3e4093b6 RS |
11015 | { |
11016 | result_type = type1; | |
d42ba3b8 | 11017 | if (pedantic) |
c1771a20 | 11018 | pedwarn (location, OPT_Wpedantic, |
d42ba3b8 SZ |
11019 | "ordered comparison of pointer with integer zero"); |
11020 | else if (extra_warnings) | |
11021 | warning_at (location, OPT_Wextra, | |
11022 | "ordered comparison of pointer with integer zero"); | |
3e4093b6 RS |
11023 | } |
11024 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
11025 | { | |
11026 | result_type = type0; | |
ba47d38d | 11027 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
11028 | } |
11029 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
11030 | { | |
11031 | result_type = type1; | |
ba47d38d | 11032 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 | 11033 | } |
04159acf MP |
11034 | if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE |
11035 | || truth_value_p (TREE_CODE (orig_op0))) | |
11036 | ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE | |
11037 | || truth_value_p (TREE_CODE (orig_op1)))) | |
11038 | maybe_warn_bool_compare (location, code, orig_op0, orig_op1); | |
3e4093b6 | 11039 | break; |
64094f6a | 11040 | |
3e4093b6 | 11041 | default: |
37b2f290 | 11042 | gcc_unreachable (); |
c9fe6f9f | 11043 | } |
8f17b5c5 | 11044 | |
e57e265b PB |
11045 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
11046 | return error_mark_node; | |
11047 | ||
5bed876a AH |
11048 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
11049 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
0af94e6f | 11050 | || !vector_types_compatible_elements_p (type0, type1))) |
5bed876a | 11051 | { |
ba47d38d | 11052 | binary_op_error (location, code, type0, type1); |
5bed876a AH |
11053 | return error_mark_node; |
11054 | } | |
11055 | ||
3e4093b6 | 11056 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
ab22c1fa | 11057 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) |
3e4093b6 RS |
11058 | && |
11059 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
ab22c1fa | 11060 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 11061 | { |
2ca862e9 JM |
11062 | bool first_complex = (code0 == COMPLEX_TYPE); |
11063 | bool second_complex = (code1 == COMPLEX_TYPE); | |
11064 | int none_complex = (!first_complex && !second_complex); | |
39b726dd | 11065 | |
3e4093b6 | 11066 | if (shorten || common || short_compare) |
3bf6bfcc JJ |
11067 | { |
11068 | result_type = c_common_type (type0, type1); | |
0a0b3574 MM |
11069 | do_warn_double_promotion (result_type, type0, type1, |
11070 | "implicit conversion from %qT to %qT " | |
11071 | "to match other operand of binary " | |
11072 | "expression", | |
11073 | location); | |
3bf6bfcc JJ |
11074 | if (result_type == error_mark_node) |
11075 | return error_mark_node; | |
11076 | } | |
400fbf9f | 11077 | |
2ca862e9 JM |
11078 | if (first_complex != second_complex |
11079 | && (code == PLUS_EXPR | |
11080 | || code == MINUS_EXPR | |
11081 | || code == MULT_EXPR | |
11082 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
11083 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
11084 | && flag_signed_zeros) | |
11085 | { | |
11086 | /* An operation on mixed real/complex operands must be | |
11087 | handled specially, but the language-independent code can | |
11088 | more easily optimize the plain complex arithmetic if | |
11089 | -fno-signed-zeros. */ | |
11090 | tree real_type = TREE_TYPE (result_type); | |
11091 | tree real, imag; | |
11092 | if (type0 != orig_type0 || type1 != orig_type1) | |
11093 | { | |
11094 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 11095 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
2ca862e9 JM |
11096 | } |
11097 | if (first_complex) | |
11098 | { | |
11099 | if (TREE_TYPE (op0) != result_type) | |
68fca595 | 11100 | op0 = convert_and_check (location, result_type, op0); |
2ca862e9 | 11101 | if (TREE_TYPE (op1) != real_type) |
68fca595 | 11102 | op1 = convert_and_check (location, real_type, op1); |
2ca862e9 JM |
11103 | } |
11104 | else | |
11105 | { | |
11106 | if (TREE_TYPE (op0) != real_type) | |
68fca595 | 11107 | op0 = convert_and_check (location, real_type, op0); |
2ca862e9 | 11108 | if (TREE_TYPE (op1) != result_type) |
68fca595 | 11109 | op1 = convert_and_check (location, result_type, op1); |
2ca862e9 JM |
11110 | } |
11111 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
11112 | return error_mark_node; | |
11113 | if (first_complex) | |
11114 | { | |
11115 | op0 = c_save_expr (op0); | |
11116 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
11117 | op0, 1); | |
11118 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
11119 | op0, 1); | |
11120 | switch (code) | |
11121 | { | |
11122 | case MULT_EXPR: | |
11123 | case TRUNC_DIV_EXPR: | |
c4603e7c | 11124 | op1 = c_save_expr (op1); |
2ca862e9 JM |
11125 | imag = build2 (resultcode, real_type, imag, op1); |
11126 | /* Fall through. */ | |
11127 | case PLUS_EXPR: | |
11128 | case MINUS_EXPR: | |
11129 | real = build2 (resultcode, real_type, real, op1); | |
11130 | break; | |
11131 | default: | |
11132 | gcc_unreachable(); | |
11133 | } | |
11134 | } | |
11135 | else | |
11136 | { | |
11137 | op1 = c_save_expr (op1); | |
11138 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
11139 | op1, 1); | |
11140 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
11141 | op1, 1); | |
11142 | switch (code) | |
11143 | { | |
11144 | case MULT_EXPR: | |
c4603e7c | 11145 | op0 = c_save_expr (op0); |
2ca862e9 JM |
11146 | imag = build2 (resultcode, real_type, op0, imag); |
11147 | /* Fall through. */ | |
11148 | case PLUS_EXPR: | |
11149 | real = build2 (resultcode, real_type, op0, real); | |
11150 | break; | |
11151 | case MINUS_EXPR: | |
11152 | real = build2 (resultcode, real_type, op0, real); | |
11153 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
11154 | break; | |
11155 | default: | |
11156 | gcc_unreachable(); | |
11157 | } | |
11158 | } | |
11159 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
11160 | goto return_build_binary_op; | |
11161 | } | |
11162 | ||
3e4093b6 RS |
11163 | /* For certain operations (which identify themselves by shorten != 0) |
11164 | if both args were extended from the same smaller type, | |
11165 | do the arithmetic in that type and then extend. | |
400fbf9f | 11166 | |
3e4093b6 RS |
11167 | shorten !=0 and !=1 indicates a bitwise operation. |
11168 | For them, this optimization is safe only if | |
11169 | both args are zero-extended or both are sign-extended. | |
11170 | Otherwise, we might change the result. | |
11171 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
11172 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
400fbf9f | 11173 | |
3e4093b6 RS |
11174 | if (shorten && none_complex) |
11175 | { | |
3e4093b6 | 11176 | final_type = result_type; |
b8698a0f | 11177 | result_type = shorten_binary_op (result_type, op0, op1, |
6715192c | 11178 | shorten == -1); |
3e4093b6 | 11179 | } |
88a3dbc1 | 11180 | |
3e4093b6 | 11181 | /* Shifts can be shortened if shifting right. */ |
2f6e4e97 | 11182 | |
3e4093b6 RS |
11183 | if (short_shift) |
11184 | { | |
11185 | int unsigned_arg; | |
11186 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
88a3dbc1 | 11187 | |
3e4093b6 | 11188 | final_type = result_type; |
abe80e6d | 11189 | |
3e4093b6 | 11190 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
8df83eae | 11191 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
e9a25f70 | 11192 | |
3e4093b6 | 11193 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
be123439 | 11194 | && tree_int_cst_sgn (op1) > 0 |
3e4093b6 RS |
11195 | /* We can shorten only if the shift count is less than the |
11196 | number of bits in the smaller type size. */ | |
11197 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
11198 | /* We cannot drop an unsigned shift after sign-extension. */ | |
8df83eae | 11199 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
3e4093b6 RS |
11200 | { |
11201 | /* Do an unsigned shift if the operand was zero-extended. */ | |
11202 | result_type | |
11203 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
11204 | TREE_TYPE (arg0)); | |
11205 | /* Convert value-to-be-shifted to that type. */ | |
11206 | if (TREE_TYPE (op0) != result_type) | |
11207 | op0 = convert (result_type, op0); | |
11208 | converted = 1; | |
abe80e6d | 11209 | } |
88a3dbc1 RK |
11210 | } |
11211 | ||
3e4093b6 RS |
11212 | /* Comparison operations are shortened too but differently. |
11213 | They identify themselves by setting short_compare = 1. */ | |
56cb9733 | 11214 | |
3e4093b6 RS |
11215 | if (short_compare) |
11216 | { | |
11217 | /* Don't write &op0, etc., because that would prevent op0 | |
11218 | from being kept in a register. | |
11219 | Instead, make copies of the our local variables and | |
11220 | pass the copies by reference, then copy them back afterward. */ | |
11221 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
11222 | enum tree_code xresultcode = resultcode; | |
11223 | tree val | |
393e8e8b MP |
11224 | = shorten_compare (location, &xop0, &xop1, &xresult_type, |
11225 | &xresultcode); | |
8f17b5c5 | 11226 | |
3e4093b6 | 11227 | if (val != 0) |
c9f9eb5d AH |
11228 | { |
11229 | ret = val; | |
11230 | goto return_build_binary_op; | |
11231 | } | |
8f17b5c5 | 11232 | |
3e4093b6 RS |
11233 | op0 = xop0, op1 = xop1; |
11234 | converted = 1; | |
11235 | resultcode = xresultcode; | |
8f17b5c5 | 11236 | |
7d882b83 | 11237 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
11238 | { |
11239 | bool op0_maybe_const = true; | |
11240 | bool op1_maybe_const = true; | |
11241 | tree orig_op0_folded, orig_op1_folded; | |
11242 | ||
11243 | if (in_late_binary_op) | |
11244 | { | |
11245 | orig_op0_folded = orig_op0; | |
11246 | orig_op1_folded = orig_op1; | |
11247 | } | |
11248 | else | |
11249 | { | |
11250 | /* Fold for the sake of possible warnings, as in | |
11251 | build_conditional_expr. This requires the | |
11252 | "original" values to be folded, not just op0 and | |
11253 | op1. */ | |
f5178456 | 11254 | c_inhibit_evaluation_warnings++; |
928c19bb JM |
11255 | op0 = c_fully_fold (op0, require_constant_value, |
11256 | &op0_maybe_const); | |
11257 | op1 = c_fully_fold (op1, require_constant_value, | |
11258 | &op1_maybe_const); | |
f5178456 | 11259 | c_inhibit_evaluation_warnings--; |
928c19bb JM |
11260 | orig_op0_folded = c_fully_fold (orig_op0, |
11261 | require_constant_value, | |
11262 | NULL); | |
11263 | orig_op1_folded = c_fully_fold (orig_op1, | |
11264 | require_constant_value, | |
11265 | NULL); | |
11266 | } | |
11267 | ||
11268 | if (warn_sign_compare) | |
11269 | warn_for_sign_compare (location, orig_op0_folded, | |
11270 | orig_op1_folded, op0, op1, | |
11271 | result_type, resultcode); | |
5c2f94b4 | 11272 | if (!in_late_binary_op && !int_operands) |
928c19bb JM |
11273 | { |
11274 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
e5a94231 | 11275 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); |
928c19bb | 11276 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) |
e5a94231 | 11277 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 11278 | } |
3e4093b6 | 11279 | } |
2ad1815d | 11280 | } |
64094f6a | 11281 | } |
64094f6a | 11282 | |
3e4093b6 RS |
11283 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
11284 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
11285 | Then the expression will be built. | |
11286 | It will be given type FINAL_TYPE if that is nonzero; | |
11287 | otherwise, it will be given type RESULT_TYPE. */ | |
400fbf9f | 11288 | |
3e4093b6 RS |
11289 | if (!result_type) |
11290 | { | |
ba47d38d | 11291 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); |
3e4093b6 RS |
11292 | return error_mark_node; |
11293 | } | |
400fbf9f | 11294 | |
3e4093b6 | 11295 | if (build_type == NULL_TREE) |
8ce94e44 JM |
11296 | { |
11297 | build_type = result_type; | |
180f8dbb JM |
11298 | if ((type0 != orig_type0 || type1 != orig_type1) |
11299 | && !boolean_op) | |
8ce94e44 JM |
11300 | { |
11301 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 11302 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
8ce94e44 JM |
11303 | } |
11304 | } | |
400fbf9f | 11305 | |
2d2e923f MLI |
11306 | if (!converted) |
11307 | { | |
68fca595 MP |
11308 | op0 = ep_convert_and_check (location, result_type, op0, |
11309 | semantic_result_type); | |
11310 | op1 = ep_convert_and_check (location, result_type, op1, | |
11311 | semantic_result_type); | |
2d2e923f MLI |
11312 | |
11313 | /* This can happen if one operand has a vector type, and the other | |
11314 | has a different type. */ | |
11315 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
11316 | return error_mark_node; | |
11317 | } | |
11318 | ||
f8ed5150 MP |
11319 | if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE |
11320 | | SANITIZE_FLOAT_DIVIDE)) | |
802ac282 | 11321 | && do_ubsan_in_current_function () |
b2aaf235 MP |
11322 | && (doing_div_or_mod || doing_shift) |
11323 | && !require_constant_value) | |
de5a5fa1 MP |
11324 | { |
11325 | /* OP0 and/or OP1 might have side-effects. */ | |
11326 | op0 = c_save_expr (op0); | |
11327 | op1 = c_save_expr (op1); | |
11328 | op0 = c_fully_fold (op0, false, NULL); | |
11329 | op1 = c_fully_fold (op1, false, NULL); | |
f8ed5150 MP |
11330 | if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE |
11331 | | SANITIZE_FLOAT_DIVIDE))) | |
de5a5fa1 | 11332 | instrument_expr = ubsan_instrument_division (location, op0, op1); |
a24d975c | 11333 | else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT)) |
de5a5fa1 MP |
11334 | instrument_expr = ubsan_instrument_shift (location, code, op0, op1); |
11335 | } | |
11336 | ||
c9f9eb5d | 11337 | /* Treat expressions in initializers specially as they can't trap. */ |
928c19bb JM |
11338 | if (int_const_or_overflow) |
11339 | ret = (require_constant_value | |
db3927fb AH |
11340 | ? fold_build2_initializer_loc (location, resultcode, build_type, |
11341 | op0, op1) | |
11342 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
928c19bb JM |
11343 | else |
11344 | ret = build2 (resultcode, build_type, op0, op1); | |
c9f9eb5d AH |
11345 | if (final_type != 0) |
11346 | ret = convert (final_type, ret); | |
11347 | ||
11348 | return_build_binary_op: | |
11349 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
11350 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) |
11351 | ret = (int_operands | |
11352 | ? note_integer_operands (ret) | |
11353 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
11354 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
11355 | && !in_late_binary_op) | |
11356 | ret = note_integer_operands (ret); | |
2d2e923f MLI |
11357 | if (semantic_result_type) |
11358 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
c9f9eb5d | 11359 | protected_set_expr_location (ret, location); |
de5a5fa1 | 11360 | |
a24d975c | 11361 | if (instrument_expr != NULL) |
de5a5fa1 MP |
11362 | ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret), |
11363 | instrument_expr, ret); | |
11364 | ||
c9f9eb5d | 11365 | return ret; |
400fbf9f | 11366 | } |
85498824 JM |
11367 | |
11368 | ||
11369 | /* Convert EXPR to be a truth-value, validating its type for this | |
ba47d38d | 11370 | purpose. LOCATION is the source location for the expression. */ |
85498824 JM |
11371 | |
11372 | tree | |
ba47d38d | 11373 | c_objc_common_truthvalue_conversion (location_t location, tree expr) |
85498824 | 11374 | { |
928c19bb JM |
11375 | bool int_const, int_operands; |
11376 | ||
85498824 JM |
11377 | switch (TREE_CODE (TREE_TYPE (expr))) |
11378 | { | |
11379 | case ARRAY_TYPE: | |
ba47d38d | 11380 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); |
85498824 JM |
11381 | return error_mark_node; |
11382 | ||
11383 | case RECORD_TYPE: | |
ba47d38d | 11384 | error_at (location, "used struct type value where scalar is required"); |
85498824 JM |
11385 | return error_mark_node; |
11386 | ||
11387 | case UNION_TYPE: | |
ba47d38d | 11388 | error_at (location, "used union type value where scalar is required"); |
85498824 JM |
11389 | return error_mark_node; |
11390 | ||
04af8788 NP |
11391 | case VOID_TYPE: |
11392 | error_at (location, "void value not ignored as it ought to be"); | |
11393 | return error_mark_node; | |
11394 | ||
1807ffc1 MS |
11395 | case POINTER_TYPE: |
11396 | if (reject_gcc_builtin (expr)) | |
11397 | return error_mark_node; | |
11398 | break; | |
11399 | ||
46bdb9cf JM |
11400 | case FUNCTION_TYPE: |
11401 | gcc_unreachable (); | |
11402 | ||
d246ab4f AS |
11403 | case VECTOR_TYPE: |
11404 | error_at (location, "used vector type where scalar is required"); | |
11405 | return error_mark_node; | |
11406 | ||
85498824 JM |
11407 | default: |
11408 | break; | |
11409 | } | |
11410 | ||
928c19bb JM |
11411 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); |
11412 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
a27d595d JM |
11413 | if (int_operands && TREE_CODE (expr) != INTEGER_CST) |
11414 | { | |
11415 | expr = remove_c_maybe_const_expr (expr); | |
11416 | expr = build2 (NE_EXPR, integer_type_node, expr, | |
11417 | convert (TREE_TYPE (expr), integer_zero_node)); | |
11418 | expr = note_integer_operands (expr); | |
11419 | } | |
11420 | else | |
11421 | /* ??? Should we also give an error for vectors rather than leaving | |
11422 | those to give errors later? */ | |
11423 | expr = c_common_truthvalue_conversion (location, expr); | |
928c19bb JM |
11424 | |
11425 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
11426 | { | |
11427 | if (TREE_OVERFLOW (expr)) | |
11428 | return expr; | |
11429 | else | |
11430 | return note_integer_operands (expr); | |
11431 | } | |
11432 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
11433 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
11434 | return expr; | |
85498824 | 11435 | } |
73f397d4 JM |
11436 | \f |
11437 | ||
11438 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
11439 | required. */ | |
11440 | ||
11441 | tree | |
51eed280 | 11442 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) |
73f397d4 JM |
11443 | { |
11444 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
11445 | { | |
11446 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
11447 | /* Executing a compound literal inside a function reinitializes | |
11448 | it. */ | |
11449 | if (!TREE_STATIC (decl)) | |
11450 | *se = true; | |
11451 | return decl; | |
11452 | } | |
11453 | else | |
11454 | return expr; | |
11455 | } | |
953ff289 | 11456 | \f |
41dbbb37 TS |
11457 | /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound |
11458 | statement. LOC is the location of the OACC_PARALLEL. */ | |
11459 | ||
11460 | tree | |
11461 | c_finish_oacc_parallel (location_t loc, tree clauses, tree block) | |
11462 | { | |
11463 | tree stmt; | |
11464 | ||
11465 | block = c_end_compound_stmt (loc, block, true); | |
11466 | ||
11467 | stmt = make_node (OACC_PARALLEL); | |
11468 | TREE_TYPE (stmt) = void_type_node; | |
11469 | OACC_PARALLEL_CLAUSES (stmt) = clauses; | |
11470 | OACC_PARALLEL_BODY (stmt) = block; | |
11471 | SET_EXPR_LOCATION (stmt, loc); | |
11472 | ||
11473 | return add_stmt (stmt); | |
11474 | } | |
11475 | ||
11476 | /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound | |
11477 | statement. LOC is the location of the OACC_KERNELS. */ | |
11478 | ||
11479 | tree | |
11480 | c_finish_oacc_kernels (location_t loc, tree clauses, tree block) | |
11481 | { | |
11482 | tree stmt; | |
11483 | ||
11484 | block = c_end_compound_stmt (loc, block, true); | |
11485 | ||
11486 | stmt = make_node (OACC_KERNELS); | |
11487 | TREE_TYPE (stmt) = void_type_node; | |
11488 | OACC_KERNELS_CLAUSES (stmt) = clauses; | |
11489 | OACC_KERNELS_BODY (stmt) = block; | |
11490 | SET_EXPR_LOCATION (stmt, loc); | |
11491 | ||
11492 | return add_stmt (stmt); | |
11493 | } | |
11494 | ||
11495 | /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound | |
11496 | statement. LOC is the location of the OACC_DATA. */ | |
11497 | ||
11498 | tree | |
11499 | c_finish_oacc_data (location_t loc, tree clauses, tree block) | |
11500 | { | |
11501 | tree stmt; | |
11502 | ||
11503 | block = c_end_compound_stmt (loc, block, true); | |
11504 | ||
11505 | stmt = make_node (OACC_DATA); | |
11506 | TREE_TYPE (stmt) = void_type_node; | |
11507 | OACC_DATA_CLAUSES (stmt) = clauses; | |
11508 | OACC_DATA_BODY (stmt) = block; | |
11509 | SET_EXPR_LOCATION (stmt, loc); | |
11510 | ||
11511 | return add_stmt (stmt); | |
11512 | } | |
11513 | ||
c0220ea4 | 11514 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
953ff289 DN |
11515 | |
11516 | tree | |
11517 | c_begin_omp_parallel (void) | |
11518 | { | |
11519 | tree block; | |
11520 | ||
11521 | keep_next_level (); | |
11522 | block = c_begin_compound_stmt (true); | |
11523 | ||
11524 | return block; | |
11525 | } | |
11526 | ||
c2255bc4 AH |
11527 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound |
11528 | statement. LOC is the location of the OMP_PARALLEL. */ | |
a68ab351 | 11529 | |
953ff289 | 11530 | tree |
c2255bc4 | 11531 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) |
953ff289 DN |
11532 | { |
11533 | tree stmt; | |
11534 | ||
c2255bc4 | 11535 | block = c_end_compound_stmt (loc, block, true); |
953ff289 DN |
11536 | |
11537 | stmt = make_node (OMP_PARALLEL); | |
11538 | TREE_TYPE (stmt) = void_type_node; | |
11539 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
11540 | OMP_PARALLEL_BODY (stmt) = block; | |
c2255bc4 | 11541 | SET_EXPR_LOCATION (stmt, loc); |
953ff289 DN |
11542 | |
11543 | return add_stmt (stmt); | |
11544 | } | |
11545 | ||
a68ab351 JJ |
11546 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
11547 | ||
11548 | tree | |
11549 | c_begin_omp_task (void) | |
11550 | { | |
11551 | tree block; | |
11552 | ||
11553 | keep_next_level (); | |
11554 | block = c_begin_compound_stmt (true); | |
11555 | ||
11556 | return block; | |
11557 | } | |
11558 | ||
c2255bc4 AH |
11559 | /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound |
11560 | statement. LOC is the location of the #pragma. */ | |
a68ab351 JJ |
11561 | |
11562 | tree | |
c2255bc4 | 11563 | c_finish_omp_task (location_t loc, tree clauses, tree block) |
a68ab351 JJ |
11564 | { |
11565 | tree stmt; | |
11566 | ||
c2255bc4 | 11567 | block = c_end_compound_stmt (loc, block, true); |
a68ab351 JJ |
11568 | |
11569 | stmt = make_node (OMP_TASK); | |
11570 | TREE_TYPE (stmt) = void_type_node; | |
11571 | OMP_TASK_CLAUSES (stmt) = clauses; | |
11572 | OMP_TASK_BODY (stmt) = block; | |
c2255bc4 | 11573 | SET_EXPR_LOCATION (stmt, loc); |
a68ab351 JJ |
11574 | |
11575 | return add_stmt (stmt); | |
11576 | } | |
11577 | ||
acf0174b JJ |
11578 | /* Generate GOMP_cancel call for #pragma omp cancel. */ |
11579 | ||
11580 | void | |
11581 | c_finish_omp_cancel (location_t loc, tree clauses) | |
11582 | { | |
11583 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL); | |
11584 | int mask = 0; | |
11585 | if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) | |
11586 | mask = 1; | |
11587 | else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) | |
11588 | mask = 2; | |
11589 | else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) | |
11590 | mask = 4; | |
11591 | else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) | |
11592 | mask = 8; | |
11593 | else | |
11594 | { | |
11595 | error_at (loc, "%<#pragma omp cancel must specify one of " | |
11596 | "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " | |
11597 | "clauses"); | |
11598 | return; | |
11599 | } | |
11600 | tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF); | |
11601 | if (ifc != NULL_TREE) | |
11602 | { | |
11603 | tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc)); | |
11604 | ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR, | |
11605 | boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc), | |
11606 | build_zero_cst (type)); | |
11607 | } | |
11608 | else | |
11609 | ifc = boolean_true_node; | |
11610 | tree stmt = build_call_expr_loc (loc, fn, 2, | |
11611 | build_int_cst (integer_type_node, mask), | |
11612 | ifc); | |
11613 | add_stmt (stmt); | |
11614 | } | |
11615 | ||
11616 | /* Generate GOMP_cancellation_point call for | |
11617 | #pragma omp cancellation point. */ | |
11618 | ||
11619 | void | |
11620 | c_finish_omp_cancellation_point (location_t loc, tree clauses) | |
11621 | { | |
11622 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT); | |
11623 | int mask = 0; | |
11624 | if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) | |
11625 | mask = 1; | |
11626 | else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) | |
11627 | mask = 2; | |
11628 | else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) | |
11629 | mask = 4; | |
11630 | else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) | |
11631 | mask = 8; | |
11632 | else | |
11633 | { | |
11634 | error_at (loc, "%<#pragma omp cancellation point must specify one of " | |
11635 | "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " | |
11636 | "clauses"); | |
11637 | return; | |
11638 | } | |
11639 | tree stmt = build_call_expr_loc (loc, fn, 1, | |
11640 | build_int_cst (integer_type_node, mask)); | |
11641 | add_stmt (stmt); | |
11642 | } | |
11643 | ||
11644 | /* Helper function for handle_omp_array_sections. Called recursively | |
11645 | to handle multiple array-section-subscripts. C is the clause, | |
11646 | T current expression (initially OMP_CLAUSE_DECL), which is either | |
11647 | a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound | |
11648 | expression if specified, TREE_VALUE length expression if specified, | |
11649 | TREE_CHAIN is what it has been specified after, or some decl. | |
11650 | TYPES vector is populated with array section types, MAYBE_ZERO_LEN | |
11651 | set to true if any of the array-section-subscript could have length | |
11652 | of zero (explicit or implicit), FIRST_NON_ONE is the index of the | |
11653 | first array-section-subscript which is known not to have length | |
11654 | of one. Given say: | |
11655 | map(a[:b][2:1][:c][:2][:d][e:f][2:5]) | |
11656 | FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c] | |
11657 | all are or may have length of 1, array-section-subscript [:2] is the | |
11658 | first one knonwn not to have length 1. For array-section-subscript | |
11659 | <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't | |
11660 | 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we | |
11661 | can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above | |
11662 | case though, as some lengths could be zero. */ | |
11663 | ||
11664 | static tree | |
11665 | handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types, | |
11666 | bool &maybe_zero_len, unsigned int &first_non_one) | |
11667 | { | |
11668 | tree ret, low_bound, length, type; | |
11669 | if (TREE_CODE (t) != TREE_LIST) | |
11670 | { | |
7a0ca710 | 11671 | if (error_operand_p (t)) |
acf0174b | 11672 | return error_mark_node; |
0ae9bd27 | 11673 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
acf0174b JJ |
11674 | { |
11675 | if (DECL_P (t)) | |
11676 | error_at (OMP_CLAUSE_LOCATION (c), | |
11677 | "%qD is not a variable in %qs clause", t, | |
11678 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11679 | else | |
11680 | error_at (OMP_CLAUSE_LOCATION (c), | |
11681 | "%qE is not a variable in %qs clause", t, | |
11682 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11683 | return error_mark_node; | |
11684 | } | |
11685 | else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND | |
0ae9bd27 | 11686 | && VAR_P (t) && DECL_THREAD_LOCAL_P (t)) |
acf0174b JJ |
11687 | { |
11688 | error_at (OMP_CLAUSE_LOCATION (c), | |
11689 | "%qD is threadprivate variable in %qs clause", t, | |
11690 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11691 | return error_mark_node; | |
11692 | } | |
11693 | return t; | |
11694 | } | |
11695 | ||
11696 | ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types, | |
11697 | maybe_zero_len, first_non_one); | |
11698 | if (ret == error_mark_node || ret == NULL_TREE) | |
11699 | return ret; | |
11700 | ||
11701 | type = TREE_TYPE (ret); | |
11702 | low_bound = TREE_PURPOSE (t); | |
11703 | length = TREE_VALUE (t); | |
11704 | ||
11705 | if (low_bound == error_mark_node || length == error_mark_node) | |
11706 | return error_mark_node; | |
11707 | ||
11708 | if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound))) | |
11709 | { | |
11710 | error_at (OMP_CLAUSE_LOCATION (c), | |
11711 | "low bound %qE of array section does not have integral type", | |
11712 | low_bound); | |
11713 | return error_mark_node; | |
11714 | } | |
11715 | if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length))) | |
11716 | { | |
11717 | error_at (OMP_CLAUSE_LOCATION (c), | |
11718 | "length %qE of array section does not have integral type", | |
11719 | length); | |
11720 | return error_mark_node; | |
11721 | } | |
11722 | if (low_bound | |
11723 | && TREE_CODE (low_bound) == INTEGER_CST | |
11724 | && TYPE_PRECISION (TREE_TYPE (low_bound)) | |
11725 | > TYPE_PRECISION (sizetype)) | |
11726 | low_bound = fold_convert (sizetype, low_bound); | |
11727 | if (length | |
11728 | && TREE_CODE (length) == INTEGER_CST | |
11729 | && TYPE_PRECISION (TREE_TYPE (length)) | |
11730 | > TYPE_PRECISION (sizetype)) | |
11731 | length = fold_convert (sizetype, length); | |
11732 | if (low_bound == NULL_TREE) | |
11733 | low_bound = integer_zero_node; | |
11734 | ||
11735 | if (length != NULL_TREE) | |
11736 | { | |
11737 | if (!integer_nonzerop (length)) | |
11738 | maybe_zero_len = true; | |
11739 | if (first_non_one == types.length () | |
11740 | && (TREE_CODE (length) != INTEGER_CST || integer_onep (length))) | |
11741 | first_non_one++; | |
11742 | } | |
11743 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11744 | { | |
11745 | if (length == NULL_TREE | |
11746 | && (TYPE_DOMAIN (type) == NULL_TREE | |
11747 | || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)) | |
11748 | { | |
11749 | error_at (OMP_CLAUSE_LOCATION (c), | |
11750 | "for unknown bound array type length expression must " | |
11751 | "be specified"); | |
11752 | return error_mark_node; | |
11753 | } | |
11754 | if (TREE_CODE (low_bound) == INTEGER_CST | |
11755 | && tree_int_cst_sgn (low_bound) == -1) | |
11756 | { | |
11757 | error_at (OMP_CLAUSE_LOCATION (c), | |
11758 | "negative low bound in array section in %qs clause", | |
11759 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11760 | return error_mark_node; | |
11761 | } | |
11762 | if (length != NULL_TREE | |
11763 | && TREE_CODE (length) == INTEGER_CST | |
11764 | && tree_int_cst_sgn (length) == -1) | |
11765 | { | |
11766 | error_at (OMP_CLAUSE_LOCATION (c), | |
11767 | "negative length in array section in %qs clause", | |
11768 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11769 | return error_mark_node; | |
11770 | } | |
11771 | if (TYPE_DOMAIN (type) | |
11772 | && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) | |
11773 | && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) | |
11774 | == INTEGER_CST) | |
11775 | { | |
11776 | tree size = size_binop (PLUS_EXPR, | |
11777 | TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
11778 | size_one_node); | |
11779 | if (TREE_CODE (low_bound) == INTEGER_CST) | |
11780 | { | |
11781 | if (tree_int_cst_lt (size, low_bound)) | |
11782 | { | |
11783 | error_at (OMP_CLAUSE_LOCATION (c), | |
11784 | "low bound %qE above array section size " | |
11785 | "in %qs clause", low_bound, | |
11786 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11787 | return error_mark_node; | |
11788 | } | |
11789 | if (tree_int_cst_equal (size, low_bound)) | |
11790 | maybe_zero_len = true; | |
11791 | else if (length == NULL_TREE | |
11792 | && first_non_one == types.length () | |
11793 | && tree_int_cst_equal | |
11794 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
11795 | low_bound)) | |
11796 | first_non_one++; | |
11797 | } | |
11798 | else if (length == NULL_TREE) | |
11799 | { | |
11800 | maybe_zero_len = true; | |
11801 | if (first_non_one == types.length ()) | |
11802 | first_non_one++; | |
11803 | } | |
11804 | if (length && TREE_CODE (length) == INTEGER_CST) | |
11805 | { | |
11806 | if (tree_int_cst_lt (size, length)) | |
11807 | { | |
11808 | error_at (OMP_CLAUSE_LOCATION (c), | |
11809 | "length %qE above array section size " | |
11810 | "in %qs clause", length, | |
11811 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11812 | return error_mark_node; | |
11813 | } | |
11814 | if (TREE_CODE (low_bound) == INTEGER_CST) | |
11815 | { | |
11816 | tree lbpluslen | |
11817 | = size_binop (PLUS_EXPR, | |
11818 | fold_convert (sizetype, low_bound), | |
11819 | fold_convert (sizetype, length)); | |
11820 | if (TREE_CODE (lbpluslen) == INTEGER_CST | |
11821 | && tree_int_cst_lt (size, lbpluslen)) | |
11822 | { | |
11823 | error_at (OMP_CLAUSE_LOCATION (c), | |
11824 | "high bound %qE above array section size " | |
11825 | "in %qs clause", lbpluslen, | |
11826 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11827 | return error_mark_node; | |
11828 | } | |
11829 | } | |
11830 | } | |
11831 | } | |
11832 | else if (length == NULL_TREE) | |
11833 | { | |
11834 | maybe_zero_len = true; | |
11835 | if (first_non_one == types.length ()) | |
11836 | first_non_one++; | |
11837 | } | |
11838 | ||
11839 | /* For [lb:] we will need to evaluate lb more than once. */ | |
11840 | if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) | |
11841 | { | |
11842 | tree lb = c_save_expr (low_bound); | |
11843 | if (lb != low_bound) | |
11844 | { | |
11845 | TREE_PURPOSE (t) = lb; | |
11846 | low_bound = lb; | |
11847 | } | |
11848 | } | |
11849 | } | |
11850 | else if (TREE_CODE (type) == POINTER_TYPE) | |
11851 | { | |
11852 | if (length == NULL_TREE) | |
11853 | { | |
11854 | error_at (OMP_CLAUSE_LOCATION (c), | |
11855 | "for pointer type length expression must be specified"); | |
11856 | return error_mark_node; | |
11857 | } | |
11858 | /* If there is a pointer type anywhere but in the very first | |
11859 | array-section-subscript, the array section can't be contiguous. */ | |
11860 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND | |
11861 | && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST) | |
11862 | { | |
11863 | error_at (OMP_CLAUSE_LOCATION (c), | |
11864 | "array section is not contiguous in %qs clause", | |
11865 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11866 | return error_mark_node; | |
11867 | } | |
11868 | } | |
11869 | else | |
11870 | { | |
11871 | error_at (OMP_CLAUSE_LOCATION (c), | |
11872 | "%qE does not have pointer or array type", ret); | |
11873 | return error_mark_node; | |
11874 | } | |
11875 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) | |
11876 | types.safe_push (TREE_TYPE (ret)); | |
11877 | /* We will need to evaluate lb more than once. */ | |
11878 | tree lb = c_save_expr (low_bound); | |
11879 | if (lb != low_bound) | |
11880 | { | |
11881 | TREE_PURPOSE (t) = lb; | |
11882 | low_bound = lb; | |
11883 | } | |
11884 | ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound); | |
11885 | return ret; | |
11886 | } | |
11887 | ||
11888 | /* Handle array sections for clause C. */ | |
11889 | ||
11890 | static bool | |
11891 | handle_omp_array_sections (tree c) | |
11892 | { | |
11893 | bool maybe_zero_len = false; | |
11894 | unsigned int first_non_one = 0; | |
11895 | vec<tree> types = vNULL; | |
11896 | tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types, | |
11897 | maybe_zero_len, first_non_one); | |
11898 | if (first == error_mark_node) | |
11899 | { | |
11900 | types.release (); | |
11901 | return true; | |
11902 | } | |
11903 | if (first == NULL_TREE) | |
11904 | { | |
11905 | types.release (); | |
11906 | return false; | |
11907 | } | |
11908 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND) | |
11909 | { | |
11910 | tree t = OMP_CLAUSE_DECL (c); | |
11911 | tree tem = NULL_TREE; | |
11912 | types.release (); | |
11913 | /* Need to evaluate side effects in the length expressions | |
11914 | if any. */ | |
11915 | while (TREE_CODE (t) == TREE_LIST) | |
11916 | { | |
11917 | if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t))) | |
11918 | { | |
11919 | if (tem == NULL_TREE) | |
11920 | tem = TREE_VALUE (t); | |
11921 | else | |
11922 | tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), | |
11923 | TREE_VALUE (t), tem); | |
11924 | } | |
11925 | t = TREE_CHAIN (t); | |
11926 | } | |
11927 | if (tem) | |
11928 | first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first); | |
11929 | first = c_fully_fold (first, false, NULL); | |
11930 | OMP_CLAUSE_DECL (c) = first; | |
11931 | } | |
11932 | else | |
11933 | { | |
11934 | unsigned int num = types.length (), i; | |
11935 | tree t, side_effects = NULL_TREE, size = NULL_TREE; | |
11936 | tree condition = NULL_TREE; | |
11937 | ||
11938 | if (int_size_in_bytes (TREE_TYPE (first)) <= 0) | |
11939 | maybe_zero_len = true; | |
11940 | ||
11941 | for (i = num, t = OMP_CLAUSE_DECL (c); i > 0; | |
11942 | t = TREE_CHAIN (t)) | |
11943 | { | |
11944 | tree low_bound = TREE_PURPOSE (t); | |
11945 | tree length = TREE_VALUE (t); | |
11946 | ||
11947 | i--; | |
11948 | if (low_bound | |
11949 | && TREE_CODE (low_bound) == INTEGER_CST | |
11950 | && TYPE_PRECISION (TREE_TYPE (low_bound)) | |
11951 | > TYPE_PRECISION (sizetype)) | |
11952 | low_bound = fold_convert (sizetype, low_bound); | |
11953 | if (length | |
11954 | && TREE_CODE (length) == INTEGER_CST | |
11955 | && TYPE_PRECISION (TREE_TYPE (length)) | |
11956 | > TYPE_PRECISION (sizetype)) | |
11957 | length = fold_convert (sizetype, length); | |
11958 | if (low_bound == NULL_TREE) | |
11959 | low_bound = integer_zero_node; | |
11960 | if (!maybe_zero_len && i > first_non_one) | |
11961 | { | |
11962 | if (integer_nonzerop (low_bound)) | |
11963 | goto do_warn_noncontiguous; | |
11964 | if (length != NULL_TREE | |
11965 | && TREE_CODE (length) == INTEGER_CST | |
11966 | && TYPE_DOMAIN (types[i]) | |
11967 | && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])) | |
11968 | && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))) | |
11969 | == INTEGER_CST) | |
11970 | { | |
11971 | tree size; | |
11972 | size = size_binop (PLUS_EXPR, | |
11973 | TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), | |
11974 | size_one_node); | |
11975 | if (!tree_int_cst_equal (length, size)) | |
11976 | { | |
11977 | do_warn_noncontiguous: | |
11978 | error_at (OMP_CLAUSE_LOCATION (c), | |
11979 | "array section is not contiguous in %qs " | |
11980 | "clause", | |
11981 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11982 | types.release (); | |
11983 | return true; | |
11984 | } | |
11985 | } | |
11986 | if (length != NULL_TREE | |
11987 | && TREE_SIDE_EFFECTS (length)) | |
11988 | { | |
11989 | if (side_effects == NULL_TREE) | |
11990 | side_effects = length; | |
11991 | else | |
11992 | side_effects = build2 (COMPOUND_EXPR, | |
11993 | TREE_TYPE (side_effects), | |
11994 | length, side_effects); | |
11995 | } | |
11996 | } | |
11997 | else | |
11998 | { | |
11999 | tree l; | |
12000 | ||
12001 | if (i > first_non_one && length && integer_nonzerop (length)) | |
12002 | continue; | |
12003 | if (length) | |
12004 | l = fold_convert (sizetype, length); | |
12005 | else | |
12006 | { | |
12007 | l = size_binop (PLUS_EXPR, | |
12008 | TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), | |
12009 | size_one_node); | |
12010 | l = size_binop (MINUS_EXPR, l, | |
12011 | fold_convert (sizetype, low_bound)); | |
12012 | } | |
12013 | if (i > first_non_one) | |
12014 | { | |
12015 | l = fold_build2 (NE_EXPR, boolean_type_node, l, | |
12016 | size_zero_node); | |
12017 | if (condition == NULL_TREE) | |
12018 | condition = l; | |
12019 | else | |
12020 | condition = fold_build2 (BIT_AND_EXPR, boolean_type_node, | |
12021 | l, condition); | |
12022 | } | |
12023 | else if (size == NULL_TREE) | |
12024 | { | |
12025 | size = size_in_bytes (TREE_TYPE (types[i])); | |
12026 | size = size_binop (MULT_EXPR, size, l); | |
12027 | if (condition) | |
12028 | size = fold_build3 (COND_EXPR, sizetype, condition, | |
12029 | size, size_zero_node); | |
12030 | } | |
12031 | else | |
12032 | size = size_binop (MULT_EXPR, size, l); | |
12033 | } | |
12034 | } | |
12035 | types.release (); | |
12036 | if (side_effects) | |
12037 | size = build2 (COMPOUND_EXPR, sizetype, side_effects, size); | |
12038 | first = c_fully_fold (first, false, NULL); | |
12039 | OMP_CLAUSE_DECL (c) = first; | |
12040 | if (size) | |
12041 | size = c_fully_fold (size, false, NULL); | |
12042 | OMP_CLAUSE_SIZE (c) = size; | |
12043 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) | |
12044 | return false; | |
41dbbb37 | 12045 | gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR); |
acf0174b | 12046 | tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP); |
41dbbb37 | 12047 | OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER); |
acf0174b JJ |
12048 | if (!c_mark_addressable (t)) |
12049 | return false; | |
12050 | OMP_CLAUSE_DECL (c2) = t; | |
12051 | t = build_fold_addr_expr (first); | |
12052 | t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t); | |
12053 | tree ptr = OMP_CLAUSE_DECL (c2); | |
12054 | if (!POINTER_TYPE_P (TREE_TYPE (ptr))) | |
12055 | ptr = build_fold_addr_expr (ptr); | |
12056 | t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
12057 | ptrdiff_type_node, t, | |
12058 | fold_convert_loc (OMP_CLAUSE_LOCATION (c), | |
12059 | ptrdiff_type_node, ptr)); | |
12060 | t = c_fully_fold (t, false, NULL); | |
12061 | OMP_CLAUSE_SIZE (c2) = t; | |
12062 | OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c); | |
12063 | OMP_CLAUSE_CHAIN (c) = c2; | |
12064 | } | |
12065 | return false; | |
12066 | } | |
12067 | ||
12068 | /* Helper function of finish_omp_clauses. Clone STMT as if we were making | |
12069 | an inline call. But, remap | |
12070 | the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER | |
12071 | and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */ | |
12072 | ||
12073 | static tree | |
12074 | c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2, | |
12075 | tree decl, tree placeholder) | |
12076 | { | |
12077 | copy_body_data id; | |
b787e7a2 | 12078 | hash_map<tree, tree> decl_map; |
acf0174b | 12079 | |
b787e7a2 TS |
12080 | decl_map.put (omp_decl1, placeholder); |
12081 | decl_map.put (omp_decl2, decl); | |
acf0174b JJ |
12082 | memset (&id, 0, sizeof (id)); |
12083 | id.src_fn = DECL_CONTEXT (omp_decl1); | |
12084 | id.dst_fn = current_function_decl; | |
12085 | id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn); | |
b787e7a2 | 12086 | id.decl_map = &decl_map; |
acf0174b JJ |
12087 | |
12088 | id.copy_decl = copy_decl_no_change; | |
12089 | id.transform_call_graph_edges = CB_CGE_DUPLICATE; | |
12090 | id.transform_new_cfg = true; | |
12091 | id.transform_return_to_modify = false; | |
12092 | id.transform_lang_insert_block = NULL; | |
12093 | id.eh_lp_nr = 0; | |
12094 | walk_tree (&stmt, copy_tree_body_r, &id, NULL); | |
acf0174b JJ |
12095 | return stmt; |
12096 | } | |
12097 | ||
12098 | /* Helper function of c_finish_omp_clauses, called via walk_tree. | |
12099 | Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */ | |
12100 | ||
12101 | static tree | |
12102 | c_find_omp_placeholder_r (tree *tp, int *, void *data) | |
12103 | { | |
12104 | if (*tp == (tree) data) | |
12105 | return *tp; | |
12106 | return NULL_TREE; | |
12107 | } | |
12108 | ||
41dbbb37 | 12109 | /* For all elements of CLAUSES, validate them against their constraints. |
953ff289 DN |
12110 | Remove any elements from the list that are invalid. */ |
12111 | ||
12112 | tree | |
12113 | c_finish_omp_clauses (tree clauses) | |
12114 | { | |
12115 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
acf0174b | 12116 | bitmap_head aligned_head; |
f3316c6d | 12117 | tree c, t, *pc; |
acf0174b JJ |
12118 | bool branch_seen = false; |
12119 | bool copyprivate_seen = false; | |
12120 | tree *nowait_clause = NULL; | |
953ff289 DN |
12121 | |
12122 | bitmap_obstack_initialize (NULL); | |
12123 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
12124 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
12125 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
acf0174b | 12126 | bitmap_initialize (&aligned_head, &bitmap_default_obstack); |
953ff289 DN |
12127 | |
12128 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
12129 | { | |
12130 | bool remove = false; | |
12131 | bool need_complete = false; | |
12132 | bool need_implicitly_determined = false; | |
12133 | ||
aaf46ef9 | 12134 | switch (OMP_CLAUSE_CODE (c)) |
953ff289 DN |
12135 | { |
12136 | case OMP_CLAUSE_SHARED: | |
953ff289 DN |
12137 | need_implicitly_determined = true; |
12138 | goto check_dup_generic; | |
12139 | ||
12140 | case OMP_CLAUSE_PRIVATE: | |
953ff289 DN |
12141 | need_complete = true; |
12142 | need_implicitly_determined = true; | |
12143 | goto check_dup_generic; | |
12144 | ||
12145 | case OMP_CLAUSE_REDUCTION: | |
953ff289 DN |
12146 | need_implicitly_determined = true; |
12147 | t = OMP_CLAUSE_DECL (c); | |
acf0174b | 12148 | if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE |
652fea39 JJ |
12149 | && (FLOAT_TYPE_P (TREE_TYPE (t)) |
12150 | || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)) | |
953ff289 DN |
12151 | { |
12152 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
12153 | const char *r_name = NULL; | |
12154 | ||
12155 | switch (r_code) | |
12156 | { | |
12157 | case PLUS_EXPR: | |
12158 | case MULT_EXPR: | |
12159 | case MINUS_EXPR: | |
652fea39 | 12160 | break; |
20906c66 | 12161 | case MIN_EXPR: |
652fea39 JJ |
12162 | if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE) |
12163 | r_name = "min"; | |
12164 | break; | |
20906c66 | 12165 | case MAX_EXPR: |
652fea39 JJ |
12166 | if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE) |
12167 | r_name = "max"; | |
953ff289 DN |
12168 | break; |
12169 | case BIT_AND_EXPR: | |
12170 | r_name = "&"; | |
12171 | break; | |
12172 | case BIT_XOR_EXPR: | |
12173 | r_name = "^"; | |
12174 | break; | |
12175 | case BIT_IOR_EXPR: | |
12176 | r_name = "|"; | |
12177 | break; | |
12178 | case TRUTH_ANDIF_EXPR: | |
652fea39 JJ |
12179 | if (FLOAT_TYPE_P (TREE_TYPE (t))) |
12180 | r_name = "&&"; | |
953ff289 DN |
12181 | break; |
12182 | case TRUTH_ORIF_EXPR: | |
652fea39 JJ |
12183 | if (FLOAT_TYPE_P (TREE_TYPE (t))) |
12184 | r_name = "||"; | |
953ff289 DN |
12185 | break; |
12186 | default: | |
12187 | gcc_unreachable (); | |
12188 | } | |
12189 | if (r_name) | |
12190 | { | |
c2255bc4 AH |
12191 | error_at (OMP_CLAUSE_LOCATION (c), |
12192 | "%qE has invalid type for %<reduction(%s)%>", | |
12193 | t, r_name); | |
953ff289 | 12194 | remove = true; |
ee1d5a02 | 12195 | break; |
953ff289 DN |
12196 | } |
12197 | } | |
acf0174b JJ |
12198 | else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node) |
12199 | { | |
12200 | error_at (OMP_CLAUSE_LOCATION (c), | |
12201 | "user defined reduction not found for %qD", t); | |
12202 | remove = true; | |
ee1d5a02 | 12203 | break; |
acf0174b JJ |
12204 | } |
12205 | else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)) | |
12206 | { | |
12207 | tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c); | |
12208 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t)); | |
12209 | tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c), | |
12210 | VAR_DECL, NULL_TREE, type); | |
12211 | OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder; | |
12212 | DECL_ARTIFICIAL (placeholder) = 1; | |
12213 | DECL_IGNORED_P (placeholder) = 1; | |
12214 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0))) | |
12215 | c_mark_addressable (placeholder); | |
12216 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1))) | |
12217 | c_mark_addressable (OMP_CLAUSE_DECL (c)); | |
12218 | OMP_CLAUSE_REDUCTION_MERGE (c) | |
12219 | = c_clone_omp_udr (TREE_VEC_ELT (list, 2), | |
12220 | TREE_VEC_ELT (list, 0), | |
12221 | TREE_VEC_ELT (list, 1), | |
12222 | OMP_CLAUSE_DECL (c), placeholder); | |
12223 | OMP_CLAUSE_REDUCTION_MERGE (c) | |
12224 | = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, | |
12225 | void_type_node, NULL_TREE, | |
12226 | OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE); | |
12227 | TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1; | |
12228 | if (TREE_VEC_LENGTH (list) == 6) | |
12229 | { | |
12230 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3))) | |
12231 | c_mark_addressable (OMP_CLAUSE_DECL (c)); | |
12232 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4))) | |
12233 | c_mark_addressable (placeholder); | |
12234 | tree init = TREE_VEC_ELT (list, 5); | |
12235 | if (init == error_mark_node) | |
12236 | init = DECL_INITIAL (TREE_VEC_ELT (list, 3)); | |
12237 | OMP_CLAUSE_REDUCTION_INIT (c) | |
12238 | = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4), | |
12239 | TREE_VEC_ELT (list, 3), | |
12240 | OMP_CLAUSE_DECL (c), placeholder); | |
12241 | if (TREE_VEC_ELT (list, 5) == error_mark_node) | |
12242 | OMP_CLAUSE_REDUCTION_INIT (c) | |
12243 | = build2 (INIT_EXPR, TREE_TYPE (t), t, | |
12244 | OMP_CLAUSE_REDUCTION_INIT (c)); | |
12245 | if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c), | |
12246 | c_find_omp_placeholder_r, | |
12247 | placeholder, NULL)) | |
12248 | OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1; | |
12249 | } | |
12250 | else | |
12251 | { | |
12252 | tree init; | |
12253 | if (AGGREGATE_TYPE_P (TREE_TYPE (t))) | |
12254 | init = build_constructor (TREE_TYPE (t), NULL); | |
12255 | else | |
12256 | init = fold_convert (TREE_TYPE (t), integer_zero_node); | |
12257 | OMP_CLAUSE_REDUCTION_INIT (c) | |
12258 | = build2 (INIT_EXPR, TREE_TYPE (t), t, init); | |
12259 | } | |
12260 | OMP_CLAUSE_REDUCTION_INIT (c) | |
12261 | = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, | |
12262 | void_type_node, NULL_TREE, | |
12263 | OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE); | |
12264 | TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1; | |
12265 | } | |
953ff289 DN |
12266 | goto check_dup_generic; |
12267 | ||
12268 | case OMP_CLAUSE_COPYPRIVATE: | |
acf0174b JJ |
12269 | copyprivate_seen = true; |
12270 | if (nowait_clause) | |
12271 | { | |
12272 | error_at (OMP_CLAUSE_LOCATION (*nowait_clause), | |
12273 | "%<nowait%> clause must not be used together " | |
12274 | "with %<copyprivate%>"); | |
12275 | *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause); | |
12276 | nowait_clause = NULL; | |
12277 | } | |
953ff289 DN |
12278 | goto check_dup_generic; |
12279 | ||
12280 | case OMP_CLAUSE_COPYIN: | |
953ff289 | 12281 | t = OMP_CLAUSE_DECL (c); |
0ae9bd27 | 12282 | if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t)) |
953ff289 | 12283 | { |
c2255bc4 AH |
12284 | error_at (OMP_CLAUSE_LOCATION (c), |
12285 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
953ff289 | 12286 | remove = true; |
ee1d5a02 | 12287 | break; |
953ff289 DN |
12288 | } |
12289 | goto check_dup_generic; | |
12290 | ||
acf0174b JJ |
12291 | case OMP_CLAUSE_LINEAR: |
12292 | t = OMP_CLAUSE_DECL (c); | |
12293 | if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) | |
12294 | && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE) | |
12295 | { | |
12296 | error_at (OMP_CLAUSE_LOCATION (c), | |
12297 | "linear clause applied to non-integral non-pointer " | |
12298 | "variable with type %qT", TREE_TYPE (t)); | |
12299 | remove = true; | |
12300 | break; | |
12301 | } | |
12302 | if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE) | |
12303 | { | |
12304 | tree s = OMP_CLAUSE_LINEAR_STEP (c); | |
12305 | s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR, | |
12306 | OMP_CLAUSE_DECL (c), s); | |
12307 | s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
12308 | sizetype, s, OMP_CLAUSE_DECL (c)); | |
12309 | if (s == error_mark_node) | |
12310 | s = size_one_node; | |
12311 | OMP_CLAUSE_LINEAR_STEP (c) = s; | |
12312 | } | |
da6f124d JJ |
12313 | else |
12314 | OMP_CLAUSE_LINEAR_STEP (c) | |
12315 | = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c)); | |
acf0174b JJ |
12316 | goto check_dup_generic; |
12317 | ||
953ff289 DN |
12318 | check_dup_generic: |
12319 | t = OMP_CLAUSE_DECL (c); | |
0ae9bd27 | 12320 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
953ff289 | 12321 | { |
c2255bc4 | 12322 | error_at (OMP_CLAUSE_LOCATION (c), |
acf0174b JJ |
12323 | "%qE is not a variable in clause %qs", t, |
12324 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
953ff289 DN |
12325 | remove = true; |
12326 | } | |
12327 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12328 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
12329 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
12330 | { | |
c2255bc4 AH |
12331 | error_at (OMP_CLAUSE_LOCATION (c), |
12332 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12333 | remove = true; |
12334 | } | |
12335 | else | |
12336 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
12337 | break; | |
12338 | ||
12339 | case OMP_CLAUSE_FIRSTPRIVATE: | |
953ff289 DN |
12340 | t = OMP_CLAUSE_DECL (c); |
12341 | need_complete = true; | |
12342 | need_implicitly_determined = true; | |
0ae9bd27 | 12343 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
953ff289 | 12344 | { |
c2255bc4 AH |
12345 | error_at (OMP_CLAUSE_LOCATION (c), |
12346 | "%qE is not a variable in clause %<firstprivate%>", t); | |
953ff289 DN |
12347 | remove = true; |
12348 | } | |
12349 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12350 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
12351 | { | |
c2255bc4 AH |
12352 | error_at (OMP_CLAUSE_LOCATION (c), |
12353 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12354 | remove = true; |
12355 | } | |
12356 | else | |
12357 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
12358 | break; | |
12359 | ||
12360 | case OMP_CLAUSE_LASTPRIVATE: | |
953ff289 DN |
12361 | t = OMP_CLAUSE_DECL (c); |
12362 | need_complete = true; | |
12363 | need_implicitly_determined = true; | |
0ae9bd27 | 12364 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
953ff289 | 12365 | { |
c2255bc4 AH |
12366 | error_at (OMP_CLAUSE_LOCATION (c), |
12367 | "%qE is not a variable in clause %<lastprivate%>", t); | |
953ff289 DN |
12368 | remove = true; |
12369 | } | |
12370 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12371 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
12372 | { | |
c2255bc4 AH |
12373 | error_at (OMP_CLAUSE_LOCATION (c), |
12374 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12375 | remove = true; |
12376 | } | |
12377 | else | |
12378 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
12379 | break; | |
12380 | ||
acf0174b JJ |
12381 | case OMP_CLAUSE_ALIGNED: |
12382 | t = OMP_CLAUSE_DECL (c); | |
0ae9bd27 | 12383 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
acf0174b JJ |
12384 | { |
12385 | error_at (OMP_CLAUSE_LOCATION (c), | |
12386 | "%qE is not a variable in %<aligned%> clause", t); | |
12387 | remove = true; | |
12388 | } | |
5a9785fb JJ |
12389 | else if (!POINTER_TYPE_P (TREE_TYPE (t)) |
12390 | && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE) | |
12391 | { | |
12392 | error_at (OMP_CLAUSE_LOCATION (c), | |
12393 | "%qE in %<aligned%> clause is neither a pointer nor " | |
12394 | "an array", t); | |
12395 | remove = true; | |
12396 | } | |
acf0174b JJ |
12397 | else if (bitmap_bit_p (&aligned_head, DECL_UID (t))) |
12398 | { | |
12399 | error_at (OMP_CLAUSE_LOCATION (c), | |
12400 | "%qE appears more than once in %<aligned%> clauses", | |
12401 | t); | |
12402 | remove = true; | |
12403 | } | |
12404 | else | |
12405 | bitmap_set_bit (&aligned_head, DECL_UID (t)); | |
12406 | break; | |
12407 | ||
12408 | case OMP_CLAUSE_DEPEND: | |
12409 | t = OMP_CLAUSE_DECL (c); | |
12410 | if (TREE_CODE (t) == TREE_LIST) | |
12411 | { | |
12412 | if (handle_omp_array_sections (c)) | |
12413 | remove = true; | |
12414 | break; | |
12415 | } | |
12416 | if (t == error_mark_node) | |
12417 | remove = true; | |
0ae9bd27 | 12418 | else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
acf0174b JJ |
12419 | { |
12420 | error_at (OMP_CLAUSE_LOCATION (c), | |
12421 | "%qE is not a variable in %<depend%> clause", t); | |
12422 | remove = true; | |
12423 | } | |
12424 | else if (!c_mark_addressable (t)) | |
12425 | remove = true; | |
12426 | break; | |
12427 | ||
12428 | case OMP_CLAUSE_MAP: | |
12429 | case OMP_CLAUSE_TO: | |
12430 | case OMP_CLAUSE_FROM: | |
41dbbb37 | 12431 | case OMP_CLAUSE__CACHE_: |
acf0174b JJ |
12432 | t = OMP_CLAUSE_DECL (c); |
12433 | if (TREE_CODE (t) == TREE_LIST) | |
12434 | { | |
12435 | if (handle_omp_array_sections (c)) | |
12436 | remove = true; | |
12437 | else | |
12438 | { | |
12439 | t = OMP_CLAUSE_DECL (c); | |
b17a8b07 | 12440 | if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) |
acf0174b JJ |
12441 | { |
12442 | error_at (OMP_CLAUSE_LOCATION (c), | |
12443 | "array section does not have mappable type " | |
12444 | "in %qs clause", | |
12445 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12446 | remove = true; | |
12447 | } | |
12448 | } | |
12449 | break; | |
12450 | } | |
12451 | if (t == error_mark_node) | |
12452 | remove = true; | |
0ae9bd27 | 12453 | else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
acf0174b JJ |
12454 | { |
12455 | error_at (OMP_CLAUSE_LOCATION (c), | |
12456 | "%qE is not a variable in %qs clause", t, | |
12457 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12458 | remove = true; | |
12459 | } | |
0ae9bd27 | 12460 | else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t)) |
acf0174b JJ |
12461 | { |
12462 | error_at (OMP_CLAUSE_LOCATION (c), | |
12463 | "%qD is threadprivate variable in %qs clause", t, | |
12464 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12465 | remove = true; | |
12466 | } | |
12467 | else if (!c_mark_addressable (t)) | |
12468 | remove = true; | |
b17a8b07 | 12469 | else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP |
41dbbb37 TS |
12470 | && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER |
12471 | || (OMP_CLAUSE_MAP_KIND (c) | |
12472 | == GOMP_MAP_FORCE_DEVICEPTR))) | |
b17a8b07 | 12473 | && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) |
acf0174b JJ |
12474 | { |
12475 | error_at (OMP_CLAUSE_LOCATION (c), | |
12476 | "%qD does not have a mappable type in %qs clause", t, | |
12477 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12478 | remove = true; | |
12479 | } | |
12480 | else if (bitmap_bit_p (&generic_head, DECL_UID (t))) | |
12481 | { | |
12482 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) | |
12483 | error ("%qD appears more than once in motion clauses", t); | |
12484 | else | |
12485 | error ("%qD appears more than once in map clauses", t); | |
12486 | remove = true; | |
12487 | } | |
12488 | else | |
12489 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
12490 | break; | |
12491 | ||
12492 | case OMP_CLAUSE_UNIFORM: | |
12493 | t = OMP_CLAUSE_DECL (c); | |
12494 | if (TREE_CODE (t) != PARM_DECL) | |
12495 | { | |
12496 | if (DECL_P (t)) | |
12497 | error_at (OMP_CLAUSE_LOCATION (c), | |
12498 | "%qD is not an argument in %<uniform%> clause", t); | |
12499 | else | |
12500 | error_at (OMP_CLAUSE_LOCATION (c), | |
12501 | "%qE is not an argument in %<uniform%> clause", t); | |
12502 | remove = true; | |
ee1d5a02 | 12503 | break; |
acf0174b | 12504 | } |
ee1d5a02 | 12505 | goto check_dup_generic; |
acf0174b JJ |
12506 | |
12507 | case OMP_CLAUSE_NOWAIT: | |
12508 | if (copyprivate_seen) | |
12509 | { | |
12510 | error_at (OMP_CLAUSE_LOCATION (c), | |
12511 | "%<nowait%> clause must not be used together " | |
12512 | "with %<copyprivate%>"); | |
12513 | remove = true; | |
12514 | break; | |
12515 | } | |
12516 | nowait_clause = pc; | |
12517 | pc = &OMP_CLAUSE_CHAIN (c); | |
12518 | continue; | |
12519 | ||
953ff289 DN |
12520 | case OMP_CLAUSE_IF: |
12521 | case OMP_CLAUSE_NUM_THREADS: | |
acf0174b JJ |
12522 | case OMP_CLAUSE_NUM_TEAMS: |
12523 | case OMP_CLAUSE_THREAD_LIMIT: | |
953ff289 | 12524 | case OMP_CLAUSE_SCHEDULE: |
953ff289 DN |
12525 | case OMP_CLAUSE_ORDERED: |
12526 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 JJ |
12527 | case OMP_CLAUSE_UNTIED: |
12528 | case OMP_CLAUSE_COLLAPSE: | |
20906c66 JJ |
12529 | case OMP_CLAUSE_FINAL: |
12530 | case OMP_CLAUSE_MERGEABLE: | |
acf0174b JJ |
12531 | case OMP_CLAUSE_SAFELEN: |
12532 | case OMP_CLAUSE_SIMDLEN: | |
12533 | case OMP_CLAUSE_DEVICE: | |
12534 | case OMP_CLAUSE_DIST_SCHEDULE: | |
12535 | case OMP_CLAUSE_PARALLEL: | |
12536 | case OMP_CLAUSE_FOR: | |
12537 | case OMP_CLAUSE_SECTIONS: | |
12538 | case OMP_CLAUSE_TASKGROUP: | |
12539 | case OMP_CLAUSE_PROC_BIND: | |
9a771876 | 12540 | case OMP_CLAUSE__CILK_FOR_COUNT_: |
41dbbb37 TS |
12541 | case OMP_CLAUSE_NUM_GANGS: |
12542 | case OMP_CLAUSE_NUM_WORKERS: | |
12543 | case OMP_CLAUSE_VECTOR_LENGTH: | |
12544 | case OMP_CLAUSE_ASYNC: | |
12545 | case OMP_CLAUSE_WAIT: | |
12546 | case OMP_CLAUSE_AUTO: | |
12547 | case OMP_CLAUSE_SEQ: | |
12548 | case OMP_CLAUSE_GANG: | |
12549 | case OMP_CLAUSE_WORKER: | |
12550 | case OMP_CLAUSE_VECTOR: | |
acf0174b JJ |
12551 | pc = &OMP_CLAUSE_CHAIN (c); |
12552 | continue; | |
12553 | ||
12554 | case OMP_CLAUSE_INBRANCH: | |
12555 | case OMP_CLAUSE_NOTINBRANCH: | |
12556 | if (branch_seen) | |
12557 | { | |
12558 | error_at (OMP_CLAUSE_LOCATION (c), | |
12559 | "%<inbranch%> clause is incompatible with " | |
12560 | "%<notinbranch%>"); | |
12561 | remove = true; | |
12562 | break; | |
12563 | } | |
12564 | branch_seen = true; | |
953ff289 DN |
12565 | pc = &OMP_CLAUSE_CHAIN (c); |
12566 | continue; | |
12567 | ||
12568 | default: | |
12569 | gcc_unreachable (); | |
12570 | } | |
12571 | ||
12572 | if (!remove) | |
12573 | { | |
12574 | t = OMP_CLAUSE_DECL (c); | |
12575 | ||
12576 | if (need_complete) | |
12577 | { | |
12578 | t = require_complete_type (t); | |
12579 | if (t == error_mark_node) | |
12580 | remove = true; | |
12581 | } | |
12582 | ||
12583 | if (need_implicitly_determined) | |
12584 | { | |
12585 | const char *share_name = NULL; | |
12586 | ||
0ae9bd27 | 12587 | if (VAR_P (t) && DECL_THREAD_LOCAL_P (t)) |
953ff289 DN |
12588 | share_name = "threadprivate"; |
12589 | else switch (c_omp_predetermined_sharing (t)) | |
12590 | { | |
12591 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
12592 | break; | |
12593 | case OMP_CLAUSE_DEFAULT_SHARED: | |
20906c66 JJ |
12594 | /* const vars may be specified in firstprivate clause. */ |
12595 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
12596 | && TREE_READONLY (t)) | |
12597 | break; | |
953ff289 DN |
12598 | share_name = "shared"; |
12599 | break; | |
12600 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
12601 | share_name = "private"; | |
12602 | break; | |
12603 | default: | |
12604 | gcc_unreachable (); | |
12605 | } | |
12606 | if (share_name) | |
12607 | { | |
c2255bc4 AH |
12608 | error_at (OMP_CLAUSE_LOCATION (c), |
12609 | "%qE is predetermined %qs for %qs", | |
acf0174b JJ |
12610 | t, share_name, |
12611 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
953ff289 DN |
12612 | remove = true; |
12613 | } | |
12614 | } | |
12615 | } | |
12616 | ||
12617 | if (remove) | |
12618 | *pc = OMP_CLAUSE_CHAIN (c); | |
12619 | else | |
12620 | pc = &OMP_CLAUSE_CHAIN (c); | |
12621 | } | |
12622 | ||
12623 | bitmap_obstack_release (NULL); | |
12624 | return clauses; | |
12625 | } | |
9ae165a0 | 12626 | |
0a35513e AH |
12627 | /* Create a transaction node. */ |
12628 | ||
12629 | tree | |
12630 | c_finish_transaction (location_t loc, tree block, int flags) | |
12631 | { | |
12632 | tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); | |
12633 | if (flags & TM_STMT_ATTR_OUTER) | |
12634 | TRANSACTION_EXPR_OUTER (stmt) = 1; | |
12635 | if (flags & TM_STMT_ATTR_RELAXED) | |
12636 | TRANSACTION_EXPR_RELAXED (stmt) = 1; | |
12637 | return add_stmt (stmt); | |
12638 | } | |
12639 | ||
9ae165a0 DG |
12640 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
12641 | down to the element type of an array. */ | |
12642 | ||
12643 | tree | |
12644 | c_build_qualified_type (tree type, int type_quals) | |
12645 | { | |
12646 | if (type == error_mark_node) | |
12647 | return type; | |
12648 | ||
12649 | if (TREE_CODE (type) == ARRAY_TYPE) | |
12650 | { | |
12651 | tree t; | |
12652 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
12653 | type_quals); | |
12654 | ||
12655 | /* See if we already have an identically qualified type. */ | |
12656 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
12657 | { | |
12658 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
12659 | && TYPE_NAME (t) == TYPE_NAME (type) | |
12660 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
12661 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
12662 | TYPE_ATTRIBUTES (type))) | |
12663 | break; | |
12664 | } | |
12665 | if (!t) | |
12666 | { | |
12667 | tree domain = TYPE_DOMAIN (type); | |
12668 | ||
12669 | t = build_variant_type_copy (type); | |
12670 | TREE_TYPE (t) = element_type; | |
12671 | ||
12672 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
12673 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
12674 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
12675 | else if (TYPE_CANONICAL (element_type) != element_type | |
12676 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
12677 | { | |
b8698a0f | 12678 | tree unqualified_canon |
9ae165a0 | 12679 | = build_array_type (TYPE_CANONICAL (element_type), |
b8698a0f | 12680 | domain? TYPE_CANONICAL (domain) |
9ae165a0 | 12681 | : NULL_TREE); |
b8698a0f | 12682 | TYPE_CANONICAL (t) |
9ae165a0 DG |
12683 | = c_build_qualified_type (unqualified_canon, type_quals); |
12684 | } | |
12685 | else | |
12686 | TYPE_CANONICAL (t) = t; | |
12687 | } | |
12688 | return t; | |
12689 | } | |
12690 | ||
12691 | /* A restrict-qualified pointer type must be a pointer to object or | |
12692 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
12693 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
12694 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
12695 | && (!POINTER_TYPE_P (type) | |
12696 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
12697 | { | |
12698 | error ("invalid use of %<restrict%>"); | |
12699 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
12700 | } | |
12701 | ||
12702 | return build_qualified_type (type, type_quals); | |
12703 | } | |
72b5577d ILT |
12704 | |
12705 | /* Build a VA_ARG_EXPR for the C parser. */ | |
12706 | ||
12707 | tree | |
c2255bc4 | 12708 | c_build_va_arg (location_t loc, tree expr, tree type) |
72b5577d | 12709 | { |
ec3fba51 MP |
12710 | if (error_operand_p (type)) |
12711 | return error_mark_node; | |
12712 | else if (!COMPLETE_TYPE_P (type)) | |
4e81b788 | 12713 | { |
ec3fba51 MP |
12714 | error_at (loc, "second argument to %<va_arg%> is of incomplete " |
12715 | "type %qT", type); | |
4e81b788 MP |
12716 | return error_mark_node; |
12717 | } | |
ec3fba51 MP |
12718 | else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) |
12719 | warning_at (loc, OPT_Wc___compat, | |
12720 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
c2255bc4 | 12721 | return build_va_arg (loc, expr, type); |
72b5577d | 12722 | } |
acf0174b JJ |
12723 | |
12724 | /* Return truthvalue of whether T1 is the same tree structure as T2. | |
12725 | Return 1 if they are the same. Return 0 if they are different. */ | |
12726 | ||
12727 | bool | |
12728 | c_tree_equal (tree t1, tree t2) | |
12729 | { | |
12730 | enum tree_code code1, code2; | |
12731 | ||
12732 | if (t1 == t2) | |
12733 | return true; | |
12734 | if (!t1 || !t2) | |
12735 | return false; | |
12736 | ||
12737 | for (code1 = TREE_CODE (t1); | |
12738 | CONVERT_EXPR_CODE_P (code1) | |
12739 | || code1 == NON_LVALUE_EXPR; | |
12740 | code1 = TREE_CODE (t1)) | |
12741 | t1 = TREE_OPERAND (t1, 0); | |
12742 | for (code2 = TREE_CODE (t2); | |
12743 | CONVERT_EXPR_CODE_P (code2) | |
12744 | || code2 == NON_LVALUE_EXPR; | |
12745 | code2 = TREE_CODE (t2)) | |
12746 | t2 = TREE_OPERAND (t2, 0); | |
12747 | ||
12748 | /* They might have become equal now. */ | |
12749 | if (t1 == t2) | |
12750 | return true; | |
12751 | ||
12752 | if (code1 != code2) | |
12753 | return false; | |
12754 | ||
12755 | switch (code1) | |
12756 | { | |
12757 | case INTEGER_CST: | |
807e902e | 12758 | return wi::eq_p (t1, t2); |
acf0174b JJ |
12759 | |
12760 | case REAL_CST: | |
12761 | return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2)); | |
12762 | ||
12763 | case STRING_CST: | |
12764 | return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2) | |
12765 | && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), | |
12766 | TREE_STRING_LENGTH (t1)); | |
12767 | ||
12768 | case FIXED_CST: | |
12769 | return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), | |
12770 | TREE_FIXED_CST (t2)); | |
12771 | ||
12772 | case COMPLEX_CST: | |
12773 | return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2)) | |
12774 | && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2)); | |
12775 | ||
12776 | case VECTOR_CST: | |
12777 | return operand_equal_p (t1, t2, OEP_ONLY_CONST); | |
12778 | ||
12779 | case CONSTRUCTOR: | |
12780 | /* We need to do this when determining whether or not two | |
12781 | non-type pointer to member function template arguments | |
12782 | are the same. */ | |
12783 | if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)) | |
12784 | || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2)) | |
12785 | return false; | |
12786 | { | |
12787 | tree field, value; | |
12788 | unsigned int i; | |
12789 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value) | |
12790 | { | |
12791 | constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i); | |
12792 | if (!c_tree_equal (field, elt2->index) | |
12793 | || !c_tree_equal (value, elt2->value)) | |
12794 | return false; | |
12795 | } | |
12796 | } | |
12797 | return true; | |
12798 | ||
12799 | case TREE_LIST: | |
12800 | if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))) | |
12801 | return false; | |
12802 | if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2))) | |
12803 | return false; | |
12804 | return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
12805 | ||
12806 | case SAVE_EXPR: | |
12807 | return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
12808 | ||
12809 | case CALL_EXPR: | |
12810 | { | |
12811 | tree arg1, arg2; | |
12812 | call_expr_arg_iterator iter1, iter2; | |
12813 | if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) | |
12814 | return false; | |
12815 | for (arg1 = first_call_expr_arg (t1, &iter1), | |
12816 | arg2 = first_call_expr_arg (t2, &iter2); | |
12817 | arg1 && arg2; | |
12818 | arg1 = next_call_expr_arg (&iter1), | |
12819 | arg2 = next_call_expr_arg (&iter2)) | |
12820 | if (!c_tree_equal (arg1, arg2)) | |
12821 | return false; | |
12822 | if (arg1 || arg2) | |
12823 | return false; | |
12824 | return true; | |
12825 | } | |
12826 | ||
12827 | case TARGET_EXPR: | |
12828 | { | |
12829 | tree o1 = TREE_OPERAND (t1, 0); | |
12830 | tree o2 = TREE_OPERAND (t2, 0); | |
12831 | ||
12832 | /* Special case: if either target is an unallocated VAR_DECL, | |
12833 | it means that it's going to be unified with whatever the | |
12834 | TARGET_EXPR is really supposed to initialize, so treat it | |
12835 | as being equivalent to anything. */ | |
0ae9bd27 | 12836 | if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE |
acf0174b JJ |
12837 | && !DECL_RTL_SET_P (o1)) |
12838 | /*Nop*/; | |
0ae9bd27 | 12839 | else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE |
acf0174b JJ |
12840 | && !DECL_RTL_SET_P (o2)) |
12841 | /*Nop*/; | |
12842 | else if (!c_tree_equal (o1, o2)) | |
12843 | return false; | |
12844 | ||
12845 | return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); | |
12846 | } | |
12847 | ||
12848 | case COMPONENT_REF: | |
12849 | if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)) | |
12850 | return false; | |
12851 | return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
12852 | ||
12853 | case PARM_DECL: | |
12854 | case VAR_DECL: | |
12855 | case CONST_DECL: | |
12856 | case FIELD_DECL: | |
12857 | case FUNCTION_DECL: | |
12858 | case IDENTIFIER_NODE: | |
12859 | case SSA_NAME: | |
12860 | return false; | |
12861 | ||
12862 | case TREE_VEC: | |
12863 | { | |
12864 | unsigned ix; | |
12865 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
12866 | return false; | |
12867 | for (ix = TREE_VEC_LENGTH (t1); ix--;) | |
12868 | if (!c_tree_equal (TREE_VEC_ELT (t1, ix), | |
12869 | TREE_VEC_ELT (t2, ix))) | |
12870 | return false; | |
12871 | return true; | |
12872 | } | |
12873 | ||
12874 | default: | |
12875 | break; | |
12876 | } | |
12877 | ||
12878 | switch (TREE_CODE_CLASS (code1)) | |
12879 | { | |
12880 | case tcc_unary: | |
12881 | case tcc_binary: | |
12882 | case tcc_comparison: | |
12883 | case tcc_expression: | |
12884 | case tcc_vl_exp: | |
12885 | case tcc_reference: | |
12886 | case tcc_statement: | |
12887 | { | |
12888 | int i, n = TREE_OPERAND_LENGTH (t1); | |
12889 | ||
12890 | switch (code1) | |
12891 | { | |
12892 | case PREINCREMENT_EXPR: | |
12893 | case PREDECREMENT_EXPR: | |
12894 | case POSTINCREMENT_EXPR: | |
12895 | case POSTDECREMENT_EXPR: | |
12896 | n = 1; | |
12897 | break; | |
12898 | case ARRAY_REF: | |
12899 | n = 2; | |
12900 | break; | |
12901 | default: | |
12902 | break; | |
12903 | } | |
12904 | ||
12905 | if (TREE_CODE_CLASS (code1) == tcc_vl_exp | |
12906 | && n != TREE_OPERAND_LENGTH (t2)) | |
12907 | return false; | |
12908 | ||
12909 | for (i = 0; i < n; ++i) | |
12910 | if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i))) | |
12911 | return false; | |
12912 | ||
12913 | return true; | |
12914 | } | |
12915 | ||
12916 | case tcc_type: | |
12917 | return comptypes (t1, t2); | |
12918 | default: | |
12919 | gcc_unreachable (); | |
12920 | } | |
12921 | /* We can get here with --disable-checking. */ | |
12922 | return false; | |
12923 | } | |
12893402 BI |
12924 | |
12925 | /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a | |
12926 | spawn-helper and BODY is the newly created body for FNDECL. */ | |
12927 | ||
12928 | void | |
12929 | cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w) | |
12930 | { | |
12931 | tree list = alloc_stmt_list (); | |
12932 | tree frame = make_cilk_frame (fndecl); | |
12933 | tree dtor = create_cilk_function_exit (frame, false, true); | |
12934 | add_local_decl (cfun, frame); | |
12935 | ||
12936 | DECL_SAVED_TREE (fndecl) = list; | |
12937 | tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)), | |
12938 | frame); | |
12939 | tree body_list = cilk_install_body_pedigree_operations (frame_ptr); | |
12940 | gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST); | |
12941 | ||
12942 | tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr); | |
12943 | append_to_statement_list (detach_expr, &body_list); | |
12944 | ||
12945 | cilk_outline (fndecl, &body, (struct wrapper_data *) w); | |
12946 | body = fold_build_cleanup_point_expr (void_type_node, body); | |
12947 | ||
12948 | append_to_statement_list (body, &body_list); | |
12949 | append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR, | |
12950 | body_list, dtor), &list); | |
12951 | } | |
1807ffc1 MS |
12952 | |
12953 | /* Returns true when the function declaration FNDECL is implicit, | |
12954 | introduced as a result of a call to an otherwise undeclared | |
12955 | function, and false otherwise. */ | |
12956 | ||
12957 | bool | |
12958 | c_decl_implicit (const_tree fndecl) | |
12959 | { | |
12960 | return C_DECL_IMPLICIT (fndecl); | |
12961 | } |