]>
Commit | Line | Data |
---|---|---|
400fbf9f | 1 | /* Build expressions with type checking for C compiler. |
23a5b65a | 2 | Copyright (C) 1987-2014 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" |
d8a2d370 DN |
31 | #include "stor-layout.h" |
32 | #include "trans-mem.h" | |
33 | #include "varasm.h" | |
34 | #include "stmt.h" | |
e57e265b | 35 | #include "langhooks.h" |
400fbf9f | 36 | #include "c-tree.h" |
29cc57cf | 37 | #include "c-lang.h" |
400fbf9f | 38 | #include "flags.h" |
ab87f8c8 | 39 | #include "intl.h" |
672a6f42 | 40 | #include "target.h" |
325c3691 | 41 | #include "tree-iterator.h" |
6662d794 | 42 | #include "bitmap.h" |
2fb9a547 AM |
43 | #include "basic-block.h" |
44 | #include "gimple-expr.h" | |
45b0be94 | 45 | #include "gimplify.h" |
acf0174b | 46 | #include "tree-inline.h" |
0645c1a2 | 47 | #include "omp-low.h" |
61d3ce20 | 48 | #include "c-family/c-objc.h" |
a212e43f | 49 | #include "c-family/c-common.h" |
de5a5fa1 | 50 | #include "c-family/c-ubsan.h" |
12893402 | 51 | #include "cilk.h" |
807e902e | 52 | #include "wide-int.h" |
325c3691 | 53 | |
2ac2f164 JM |
54 | /* Possible cases of implicit bad conversions. Used to select |
55 | diagnostic messages in convert_for_assignment. */ | |
56 | enum impl_conv { | |
57 | ic_argpass, | |
58 | ic_assign, | |
59 | ic_init, | |
60 | ic_return | |
61 | }; | |
62 | ||
bc4b653b JM |
63 | /* The level of nesting inside "__alignof__". */ |
64 | int in_alignof; | |
65 | ||
66 | /* The level of nesting inside "sizeof". */ | |
67 | int in_sizeof; | |
68 | ||
69 | /* The level of nesting inside "typeof". */ | |
70 | int in_typeof; | |
400fbf9f | 71 | |
1a4049e7 JJ |
72 | /* The argument of last parsed sizeof expression, only to be tested |
73 | if expr.original_code == SIZEOF_EXPR. */ | |
74 | tree c_last_sizeof_arg; | |
75 | ||
9bac5cbb G |
76 | /* Nonzero if we might need to print a "missing braces around |
77 | initializer" message within this initializer. */ | |
78 | static int found_missing_braces; | |
103b7b17 | 79 | |
bf730f15 RS |
80 | static int require_constant_value; |
81 | static int require_constant_elements; | |
82 | ||
58f9752a | 83 | static bool null_pointer_constant_p (const_tree); |
f55ade6e | 84 | static tree qualify_type (tree, tree); |
dc5027f4 JM |
85 | static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *, |
86 | bool *); | |
744aa42f | 87 | static int comp_target_types (location_t, tree, tree); |
dc5027f4 JM |
88 | static int function_types_compatible_p (const_tree, const_tree, bool *, |
89 | bool *); | |
90 | static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *); | |
f55ade6e | 91 | static tree lookup_field (tree, tree); |
81e5eca8 MP |
92 | static int convert_arguments (location_t, vec<location_t>, tree, |
93 | vec<tree, va_gc> *, vec<tree, va_gc> *, tree, | |
94 | tree); | |
db3927fb | 95 | static tree pointer_diff (location_t, tree, tree); |
68fca595 | 96 | static tree convert_for_assignment (location_t, location_t, tree, tree, tree, |
744aa42f | 97 | enum impl_conv, bool, tree, tree, int); |
f55ade6e AJ |
98 | static tree valid_compound_expr_initializer (tree, tree); |
99 | static void push_string (const char *); | |
100 | static void push_member_name (tree); | |
f55ade6e AJ |
101 | static int spelling_length (void); |
102 | static char *print_spelling (char *); | |
96b40f8d | 103 | static void warning_init (location_t, int, const char *); |
c2255bc4 | 104 | static tree digest_init (location_t, tree, tree, tree, bool, bool, int); |
34cf811f MP |
105 | static void output_init_element (location_t, tree, tree, bool, tree, tree, int, |
106 | bool, struct obstack *); | |
a1e3b3d9 | 107 | static void output_pending_init_elements (int, struct obstack *); |
ea58ef42 | 108 | static int set_designator (location_t, int, struct obstack *); |
a1e3b3d9 | 109 | static void push_range_stack (tree, struct obstack *); |
96b40f8d MP |
110 | static void add_pending_init (location_t, tree, tree, tree, bool, |
111 | struct obstack *); | |
a1e3b3d9 LB |
112 | static void set_nonincremental_init (struct obstack *); |
113 | static void set_nonincremental_init_from_string (tree, struct obstack *); | |
114 | static tree find_init_member (tree, struct obstack *); | |
f37acdf9 | 115 | static void readonly_warning (tree, enum lvalue_use); |
7bd11157 | 116 | static int lvalue_or_else (location_t, const_tree, enum lvalue_use); |
4e2fb7de | 117 | static void record_maybe_used_decl (tree); |
dc5027f4 | 118 | static int comptypes_internal (const_tree, const_tree, bool *, bool *); |
6aa3c60d JM |
119 | \f |
120 | /* Return true if EXP is a null pointer constant, false otherwise. */ | |
121 | ||
122 | static bool | |
58f9752a | 123 | null_pointer_constant_p (const_tree expr) |
6aa3c60d JM |
124 | { |
125 | /* This should really operate on c_expr structures, but they aren't | |
126 | yet available everywhere required. */ | |
127 | tree type = TREE_TYPE (expr); | |
128 | return (TREE_CODE (expr) == INTEGER_CST | |
8bcd6380 | 129 | && !TREE_OVERFLOW (expr) |
6aa3c60d JM |
130 | && integer_zerop (expr) |
131 | && (INTEGRAL_TYPE_P (type) | |
132 | || (TREE_CODE (type) == POINTER_TYPE | |
133 | && VOID_TYPE_P (TREE_TYPE (type)) | |
134 | && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); | |
135 | } | |
928c19bb JM |
136 | |
137 | /* EXPR may appear in an unevaluated part of an integer constant | |
138 | expression, but not in an evaluated part. Wrap it in a | |
139 | C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an | |
140 | INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ | |
141 | ||
142 | static tree | |
143 | note_integer_operands (tree expr) | |
144 | { | |
145 | tree ret; | |
146 | if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) | |
147 | { | |
148 | ret = copy_node (expr); | |
149 | TREE_OVERFLOW (ret) = 1; | |
150 | } | |
151 | else | |
152 | { | |
153 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); | |
154 | C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; | |
155 | } | |
156 | return ret; | |
157 | } | |
158 | ||
4d84fe7c JM |
159 | /* Having checked whether EXPR may appear in an unevaluated part of an |
160 | integer constant expression and found that it may, remove any | |
161 | C_MAYBE_CONST_EXPR noting this fact and return the resulting | |
162 | expression. */ | |
163 | ||
164 | static inline tree | |
165 | remove_c_maybe_const_expr (tree expr) | |
166 | { | |
167 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
168 | return C_MAYBE_CONST_EXPR_EXPR (expr); | |
169 | else | |
170 | return expr; | |
171 | } | |
172 | ||
f13c9b2c AP |
173 | \f/* This is a cache to hold if two types are compatible or not. */ |
174 | ||
175 | struct tagged_tu_seen_cache { | |
176 | const struct tagged_tu_seen_cache * next; | |
58f9752a KG |
177 | const_tree t1; |
178 | const_tree t2; | |
f13c9b2c AP |
179 | /* The return value of tagged_types_tu_compatible_p if we had seen |
180 | these two types already. */ | |
181 | int val; | |
182 | }; | |
183 | ||
184 | static const struct tagged_tu_seen_cache * tagged_tu_seen_base; | |
185 | static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); | |
186 | ||
400fbf9f JW |
187 | /* Do `exp = require_complete_type (exp);' to make sure exp |
188 | does not have an incomplete type. (That includes void types.) */ | |
189 | ||
190 | tree | |
2f6e4e97 | 191 | require_complete_type (tree value) |
400fbf9f JW |
192 | { |
193 | tree type = TREE_TYPE (value); | |
194 | ||
c3d5c3fa | 195 | if (value == error_mark_node || type == error_mark_node) |
ea0f786b CB |
196 | return error_mark_node; |
197 | ||
400fbf9f | 198 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 199 | if (COMPLETE_TYPE_P (type)) |
400fbf9f JW |
200 | return value; |
201 | ||
7a228918 | 202 | c_incomplete_type_error (value, type); |
400fbf9f JW |
203 | return error_mark_node; |
204 | } | |
205 | ||
206 | /* Print an error message for invalid use of an incomplete type. | |
207 | VALUE is the expression that was used (or 0 if that isn't known) | |
208 | and TYPE is the type that was invalid. */ | |
209 | ||
210 | void | |
ac7d7749 | 211 | c_incomplete_type_error (const_tree value, const_tree type) |
400fbf9f | 212 | { |
5d5993dd | 213 | const char *type_code_string; |
400fbf9f JW |
214 | |
215 | /* Avoid duplicate error message. */ | |
216 | if (TREE_CODE (type) == ERROR_MARK) | |
217 | return; | |
218 | ||
219 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
220 | || TREE_CODE (value) == PARM_DECL)) | |
c51a1ba9 | 221 | error ("%qD has an incomplete type", value); |
400fbf9f JW |
222 | else |
223 | { | |
224 | retry: | |
225 | /* We must print an error message. Be clever about what it says. */ | |
226 | ||
227 | switch (TREE_CODE (type)) | |
228 | { | |
229 | case RECORD_TYPE: | |
ab87f8c8 | 230 | type_code_string = "struct"; |
400fbf9f JW |
231 | break; |
232 | ||
233 | case UNION_TYPE: | |
ab87f8c8 | 234 | type_code_string = "union"; |
400fbf9f JW |
235 | break; |
236 | ||
237 | case ENUMERAL_TYPE: | |
ab87f8c8 | 238 | type_code_string = "enum"; |
400fbf9f JW |
239 | break; |
240 | ||
241 | case VOID_TYPE: | |
242 | error ("invalid use of void expression"); | |
243 | return; | |
244 | ||
245 | case ARRAY_TYPE: | |
246 | if (TYPE_DOMAIN (type)) | |
247 | { | |
fba78abb RH |
248 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) |
249 | { | |
250 | error ("invalid use of flexible array member"); | |
251 | return; | |
252 | } | |
400fbf9f JW |
253 | type = TREE_TYPE (type); |
254 | goto retry; | |
255 | } | |
256 | error ("invalid use of array with unspecified bounds"); | |
257 | return; | |
258 | ||
259 | default: | |
366de0ce | 260 | gcc_unreachable (); |
400fbf9f JW |
261 | } |
262 | ||
263 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
c51a1ba9 JM |
264 | error ("invalid use of undefined type %<%s %E%>", |
265 | type_code_string, TYPE_NAME (type)); | |
400fbf9f JW |
266 | else |
267 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
c51a1ba9 | 268 | error ("invalid use of incomplete typedef %qD", TYPE_NAME (type)); |
400fbf9f JW |
269 | } |
270 | } | |
271 | ||
ab393bf1 NB |
272 | /* Given a type, apply default promotions wrt unnamed function |
273 | arguments and return the new type. */ | |
274 | ||
275 | tree | |
2f6e4e97 | 276 | c_type_promotes_to (tree type) |
ab393bf1 | 277 | { |
267bac10 | 278 | tree ret = NULL_TREE; |
ab393bf1 | 279 | |
267bac10 JM |
280 | if (TYPE_MAIN_VARIANT (type) == float_type_node) |
281 | ret = double_type_node; | |
282 | else if (c_promoting_integer_type_p (type)) | |
ab393bf1 NB |
283 | { |
284 | /* Preserve unsignedness if not really getting any wider. */ | |
8df83eae | 285 | if (TYPE_UNSIGNED (type) |
c22cacf3 | 286 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) |
267bac10 JM |
287 | ret = unsigned_type_node; |
288 | else | |
289 | ret = integer_type_node; | |
ab393bf1 NB |
290 | } |
291 | ||
267bac10 JM |
292 | if (ret != NULL_TREE) |
293 | return (TYPE_ATOMIC (type) | |
294 | ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC) | |
295 | : ret); | |
296 | ||
ab393bf1 NB |
297 | return type; |
298 | } | |
299 | ||
36c5e70a BE |
300 | /* Return true if between two named address spaces, whether there is a superset |
301 | named address space that encompasses both address spaces. If there is a | |
302 | superset, return which address space is the superset. */ | |
303 | ||
304 | static bool | |
305 | addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) | |
306 | { | |
307 | if (as1 == as2) | |
308 | { | |
309 | *common = as1; | |
310 | return true; | |
311 | } | |
312 | else if (targetm.addr_space.subset_p (as1, as2)) | |
313 | { | |
314 | *common = as2; | |
315 | return true; | |
316 | } | |
317 | else if (targetm.addr_space.subset_p (as2, as1)) | |
318 | { | |
319 | *common = as1; | |
320 | return true; | |
321 | } | |
322 | else | |
323 | return false; | |
324 | } | |
325 | ||
400fbf9f JW |
326 | /* Return a variant of TYPE which has all the type qualifiers of LIKE |
327 | as well as those of TYPE. */ | |
328 | ||
329 | static tree | |
2f6e4e97 | 330 | qualify_type (tree type, tree like) |
400fbf9f | 331 | { |
36c5e70a BE |
332 | addr_space_t as_type = TYPE_ADDR_SPACE (type); |
333 | addr_space_t as_like = TYPE_ADDR_SPACE (like); | |
334 | addr_space_t as_common; | |
335 | ||
336 | /* If the two named address spaces are different, determine the common | |
337 | superset address space. If there isn't one, raise an error. */ | |
338 | if (!addr_space_superset (as_type, as_like, &as_common)) | |
339 | { | |
340 | as_common = as_type; | |
341 | error ("%qT and %qT are in disjoint named address spaces", | |
342 | type, like); | |
343 | } | |
344 | ||
2f6e4e97 | 345 | return c_build_qualified_type (type, |
36c5e70a | 346 | TYPE_QUALS_NO_ADDR_SPACE (type) |
267bac10 | 347 | | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like) |
36c5e70a | 348 | | ENCODE_QUAL_ADDR_SPACE (as_common)); |
400fbf9f | 349 | } |
52ffd86e MS |
350 | |
351 | /* Return true iff the given tree T is a variable length array. */ | |
352 | ||
353 | bool | |
ac7d7749 | 354 | c_vla_type_p (const_tree t) |
52ffd86e MS |
355 | { |
356 | if (TREE_CODE (t) == ARRAY_TYPE | |
357 | && C_TYPE_VARIABLE_SIZE (t)) | |
358 | return true; | |
359 | return false; | |
360 | } | |
400fbf9f | 361 | \f |
10bc1b1b | 362 | /* Return the composite type of two compatible types. |
5305f6d7 | 363 | |
10bc1b1b JM |
364 | We assume that comptypes has already been done and returned |
365 | nonzero; if that isn't so, this may crash. In particular, we | |
366 | assume that qualifiers match. */ | |
400fbf9f JW |
367 | |
368 | tree | |
10bc1b1b | 369 | composite_type (tree t1, tree t2) |
400fbf9f | 370 | { |
b3694847 SS |
371 | enum tree_code code1; |
372 | enum tree_code code2; | |
4b027d16 | 373 | tree attributes; |
400fbf9f JW |
374 | |
375 | /* Save time if the two types are the same. */ | |
376 | ||
377 | if (t1 == t2) return t1; | |
378 | ||
379 | /* If one type is nonsense, use the other. */ | |
380 | if (t1 == error_mark_node) | |
381 | return t2; | |
382 | if (t2 == error_mark_node) | |
383 | return t1; | |
384 | ||
10bc1b1b JM |
385 | code1 = TREE_CODE (t1); |
386 | code2 = TREE_CODE (t2); | |
387 | ||
d9525bec | 388 | /* Merge the attributes. */ |
5fd9b178 | 389 | attributes = targetm.merge_type_attributes (t1, t2); |
4b027d16 | 390 | |
10bc1b1b JM |
391 | /* If one is an enumerated type and the other is the compatible |
392 | integer type, the composite type might be either of the two | |
393 | (DR#013 question 3). For consistency, use the enumerated type as | |
394 | the composite type. */ | |
400fbf9f | 395 | |
10bc1b1b JM |
396 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) |
397 | return t1; | |
398 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
399 | return t2; | |
75326e8c | 400 | |
366de0ce | 401 | gcc_assert (code1 == code2); |
b6a10c9f | 402 | |
400fbf9f JW |
403 | switch (code1) |
404 | { | |
400fbf9f | 405 | case POINTER_TYPE: |
10bc1b1b | 406 | /* For two pointers, do this recursively on the target type. */ |
400fbf9f | 407 | { |
3932261a MM |
408 | tree pointed_to_1 = TREE_TYPE (t1); |
409 | tree pointed_to_2 = TREE_TYPE (t2); | |
10bc1b1b | 410 | tree target = composite_type (pointed_to_1, pointed_to_2); |
3db684fb | 411 | t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false); |
fe7080d2 AP |
412 | t1 = build_type_attribute_variant (t1, attributes); |
413 | return qualify_type (t1, t2); | |
400fbf9f | 414 | } |
400fbf9f JW |
415 | |
416 | case ARRAY_TYPE: | |
417 | { | |
10bc1b1b | 418 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
46df2823 JM |
419 | int quals; |
420 | tree unqual_elt; | |
ca8bdb78 JM |
421 | tree d1 = TYPE_DOMAIN (t1); |
422 | tree d2 = TYPE_DOMAIN (t2); | |
423 | bool d1_variable, d2_variable; | |
424 | bool d1_zero, d2_zero; | |
f6294de7 | 425 | bool t1_complete, t2_complete; |
46df2823 | 426 | |
de46b2fe | 427 | /* We should not have any type quals on arrays at all. */ |
36c5e70a BE |
428 | gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) |
429 | && !TYPE_QUALS_NO_ADDR_SPACE (t2)); | |
c22cacf3 | 430 | |
f6294de7 JM |
431 | t1_complete = COMPLETE_TYPE_P (t1); |
432 | t2_complete = COMPLETE_TYPE_P (t2); | |
433 | ||
ca8bdb78 JM |
434 | d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); |
435 | d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); | |
436 | ||
437 | d1_variable = (!d1_zero | |
438 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
439 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
440 | d2_variable = (!d2_zero | |
441 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
442 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
443 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
444 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
ca8bdb78 | 445 | |
400fbf9f | 446 | /* Save space: see if the result is identical to one of the args. */ |
ca8bdb78 JM |
447 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) |
448 | && (d2_variable || d2_zero || !d1_variable)) | |
4b027d16 | 449 | return build_type_attribute_variant (t1, attributes); |
ca8bdb78 JM |
450 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) |
451 | && (d1_variable || d1_zero || !d2_variable)) | |
4b027d16 | 452 | return build_type_attribute_variant (t2, attributes); |
c22cacf3 | 453 | |
de46b2fe AP |
454 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) |
455 | return build_type_attribute_variant (t1, attributes); | |
456 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
457 | return build_type_attribute_variant (t2, attributes); | |
c22cacf3 | 458 | |
46df2823 JM |
459 | /* Merge the element types, and have a size if either arg has |
460 | one. We may have qualifiers on the element types. To set | |
461 | up TYPE_MAIN_VARIANT correctly, we need to form the | |
462 | composite of the unqualified types and add the qualifiers | |
463 | back at the end. */ | |
464 | quals = TYPE_QUALS (strip_array_types (elt)); | |
465 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
466 | t1 = build_array_type (unqual_elt, | |
ca8bdb78 JM |
467 | TYPE_DOMAIN ((TYPE_DOMAIN (t1) |
468 | && (d2_variable | |
469 | || d2_zero | |
470 | || !d1_variable)) | |
471 | ? t1 | |
472 | : t2)); | |
f6294de7 JM |
473 | /* Ensure a composite type involving a zero-length array type |
474 | is a zero-length type not an incomplete type. */ | |
475 | if (d1_zero && d2_zero | |
476 | && (t1_complete || t2_complete) | |
477 | && !COMPLETE_TYPE_P (t1)) | |
478 | { | |
479 | TYPE_SIZE (t1) = bitsize_zero_node; | |
480 | TYPE_SIZE_UNIT (t1) = size_zero_node; | |
481 | } | |
46df2823 | 482 | t1 = c_build_qualified_type (t1, quals); |
de46b2fe | 483 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
484 | } |
485 | ||
fcb99e7b JJ |
486 | case ENUMERAL_TYPE: |
487 | case RECORD_TYPE: | |
488 | case UNION_TYPE: | |
489 | if (attributes != NULL) | |
490 | { | |
491 | /* Try harder not to create a new aggregate type. */ | |
492 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
493 | return t1; | |
494 | if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
495 | return t2; | |
496 | } | |
497 | return build_type_attribute_variant (t1, attributes); | |
498 | ||
400fbf9f JW |
499 | case FUNCTION_TYPE: |
500 | /* Function types: prefer the one that specified arg types. | |
501 | If both do, merge the arg types. Also merge the return types. */ | |
502 | { | |
10bc1b1b | 503 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
400fbf9f JW |
504 | tree p1 = TYPE_ARG_TYPES (t1); |
505 | tree p2 = TYPE_ARG_TYPES (t2); | |
506 | int len; | |
507 | tree newargs, n; | |
508 | int i; | |
509 | ||
510 | /* Save space: see if the result is identical to one of the args. */ | |
3f75a254 | 511 | if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) |
4b027d16 | 512 | return build_type_attribute_variant (t1, attributes); |
3f75a254 | 513 | if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) |
4b027d16 | 514 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
515 | |
516 | /* Simple way if one arg fails to specify argument types. */ | |
517 | if (TYPE_ARG_TYPES (t1) == 0) | |
4b027d16 | 518 | { |
fe7080d2 AP |
519 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); |
520 | t1 = build_type_attribute_variant (t1, attributes); | |
521 | return qualify_type (t1, t2); | |
4b027d16 | 522 | } |
400fbf9f | 523 | if (TYPE_ARG_TYPES (t2) == 0) |
4b027d16 RK |
524 | { |
525 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
fe7080d2 AP |
526 | t1 = build_type_attribute_variant (t1, attributes); |
527 | return qualify_type (t1, t2); | |
4b027d16 | 528 | } |
400fbf9f JW |
529 | |
530 | /* If both args specify argument types, we must merge the two | |
531 | lists, argument by argument. */ | |
2f4e8f2b | 532 | |
400fbf9f JW |
533 | len = list_length (p1); |
534 | newargs = 0; | |
535 | ||
536 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 537 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
538 | |
539 | n = newargs; | |
540 | ||
541 | for (; p1; | |
542 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
543 | { | |
544 | /* A null type means arg type is not specified. | |
545 | Take whatever the other function type has. */ | |
546 | if (TREE_VALUE (p1) == 0) | |
547 | { | |
548 | TREE_VALUE (n) = TREE_VALUE (p2); | |
549 | goto parm_done; | |
550 | } | |
551 | if (TREE_VALUE (p2) == 0) | |
552 | { | |
553 | TREE_VALUE (n) = TREE_VALUE (p1); | |
554 | goto parm_done; | |
555 | } | |
2f6e4e97 | 556 | |
400fbf9f JW |
557 | /* Given wait (union {union wait *u; int *i} *) |
558 | and wait (union wait *), | |
559 | prefer union wait * as type of parm. */ | |
560 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
561 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
562 | { | |
563 | tree memb; | |
58cb41e6 JJ |
564 | tree mv2 = TREE_VALUE (p2); |
565 | if (mv2 && mv2 != error_mark_node | |
566 | && TREE_CODE (mv2) != ARRAY_TYPE) | |
567 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
400fbf9f | 568 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); |
910ad8de | 569 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
570 | { |
571 | tree mv3 = TREE_TYPE (memb); | |
572 | if (mv3 && mv3 != error_mark_node | |
573 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
574 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
575 | if (comptypes (mv3, mv2)) | |
576 | { | |
577 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
578 | TREE_VALUE (p2)); | |
c1771a20 | 579 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 580 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
581 | goto parm_done; |
582 | } | |
583 | } | |
400fbf9f JW |
584 | } |
585 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
586 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
587 | { | |
588 | tree memb; | |
58cb41e6 JJ |
589 | tree mv1 = TREE_VALUE (p1); |
590 | if (mv1 && mv1 != error_mark_node | |
591 | && TREE_CODE (mv1) != ARRAY_TYPE) | |
592 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
400fbf9f | 593 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); |
910ad8de | 594 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
595 | { |
596 | tree mv3 = TREE_TYPE (memb); | |
597 | if (mv3 && mv3 != error_mark_node | |
598 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
599 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
600 | if (comptypes (mv3, mv1)) | |
601 | { | |
602 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
603 | TREE_VALUE (p1)); | |
c1771a20 | 604 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 605 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
606 | goto parm_done; |
607 | } | |
608 | } | |
400fbf9f | 609 | } |
10bc1b1b | 610 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); |
400fbf9f JW |
611 | parm_done: ; |
612 | } | |
613 | ||
4b027d16 | 614 | t1 = build_function_type (valtype, newargs); |
fe7080d2 | 615 | t1 = qualify_type (t1, t2); |
0f41302f | 616 | /* ... falls through ... */ |
400fbf9f JW |
617 | } |
618 | ||
619 | default: | |
4b027d16 | 620 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
621 | } |
622 | ||
623 | } | |
10bc1b1b JM |
624 | |
625 | /* Return the type of a conditional expression between pointers to | |
626 | possibly differently qualified versions of compatible types. | |
627 | ||
628 | We assume that comp_target_types has already been done and returned | |
629 | nonzero; if that isn't so, this may crash. */ | |
630 | ||
631 | static tree | |
632 | common_pointer_type (tree t1, tree t2) | |
633 | { | |
634 | tree attributes; | |
46df2823 JM |
635 | tree pointed_to_1, mv1; |
636 | tree pointed_to_2, mv2; | |
10bc1b1b | 637 | tree target; |
eb1387a0 | 638 | unsigned target_quals; |
36c5e70a BE |
639 | addr_space_t as1, as2, as_common; |
640 | int quals1, quals2; | |
10bc1b1b JM |
641 | |
642 | /* Save time if the two types are the same. */ | |
643 | ||
644 | if (t1 == t2) return t1; | |
645 | ||
646 | /* If one type is nonsense, use the other. */ | |
647 | if (t1 == error_mark_node) | |
648 | return t2; | |
649 | if (t2 == error_mark_node) | |
650 | return t1; | |
651 | ||
366de0ce | 652 | gcc_assert (TREE_CODE (t1) == POINTER_TYPE |
c22cacf3 | 653 | && TREE_CODE (t2) == POINTER_TYPE); |
10bc1b1b JM |
654 | |
655 | /* Merge the attributes. */ | |
656 | attributes = targetm.merge_type_attributes (t1, t2); | |
657 | ||
658 | /* Find the composite type of the target types, and combine the | |
46df2823 JM |
659 | qualifiers of the two types' targets. Do not lose qualifiers on |
660 | array element types by taking the TYPE_MAIN_VARIANT. */ | |
661 | mv1 = pointed_to_1 = TREE_TYPE (t1); | |
662 | mv2 = pointed_to_2 = TREE_TYPE (t2); | |
663 | if (TREE_CODE (mv1) != ARRAY_TYPE) | |
664 | mv1 = TYPE_MAIN_VARIANT (pointed_to_1); | |
665 | if (TREE_CODE (mv2) != ARRAY_TYPE) | |
666 | mv2 = TYPE_MAIN_VARIANT (pointed_to_2); | |
667 | target = composite_type (mv1, mv2); | |
eb1387a0 RG |
668 | |
669 | /* For function types do not merge const qualifiers, but drop them | |
670 | if used inconsistently. The middle-end uses these to mark const | |
671 | and noreturn functions. */ | |
36c5e70a BE |
672 | quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1); |
673 | quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2); | |
674 | ||
eb1387a0 | 675 | if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) |
36c5e70a | 676 | target_quals = (quals1 & quals2); |
eb1387a0 | 677 | else |
36c5e70a BE |
678 | target_quals = (quals1 | quals2); |
679 | ||
680 | /* If the two named address spaces are different, determine the common | |
681 | superset address space. This is guaranteed to exist due to the | |
682 | assumption that comp_target_type returned non-zero. */ | |
683 | as1 = TYPE_ADDR_SPACE (pointed_to_1); | |
684 | as2 = TYPE_ADDR_SPACE (pointed_to_2); | |
685 | if (!addr_space_superset (as1, as2, &as_common)) | |
686 | gcc_unreachable (); | |
687 | ||
688 | target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); | |
689 | ||
eb1387a0 | 690 | t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); |
10bc1b1b JM |
691 | return build_type_attribute_variant (t1, attributes); |
692 | } | |
693 | ||
694 | /* Return the common type for two arithmetic types under the usual | |
695 | arithmetic conversions. The default conversions have already been | |
696 | applied, and enumerated types converted to their compatible integer | |
697 | types. The resulting type is unqualified and has no attributes. | |
698 | ||
699 | This is the type for the result of most arithmetic operations | |
700 | if the operands have the given two types. */ | |
701 | ||
ccf7f880 JJ |
702 | static tree |
703 | c_common_type (tree t1, tree t2) | |
10bc1b1b JM |
704 | { |
705 | enum tree_code code1; | |
706 | enum tree_code code2; | |
707 | ||
708 | /* If one type is nonsense, use the other. */ | |
709 | if (t1 == error_mark_node) | |
710 | return t2; | |
711 | if (t2 == error_mark_node) | |
712 | return t1; | |
713 | ||
714 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
715 | t1 = TYPE_MAIN_VARIANT (t1); | |
716 | ||
717 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
718 | t2 = TYPE_MAIN_VARIANT (t2); | |
719 | ||
720 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
721 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
722 | ||
723 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
724 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
725 | ||
726 | /* Save time if the two types are the same. */ | |
727 | ||
728 | if (t1 == t2) return t1; | |
729 | ||
730 | code1 = TREE_CODE (t1); | |
731 | code2 = TREE_CODE (t2); | |
732 | ||
366de0ce | 733 | gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE |
ab22c1fa CF |
734 | || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE |
735 | || code1 == INTEGER_TYPE); | |
366de0ce | 736 | gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE |
ab22c1fa CF |
737 | || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE |
738 | || code2 == INTEGER_TYPE); | |
10bc1b1b | 739 | |
5fc89bfd JJ |
740 | /* When one operand is a decimal float type, the other operand cannot be |
741 | a generic float type or a complex type. We also disallow vector types | |
742 | here. */ | |
743 | if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) | |
744 | && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) | |
745 | { | |
746 | if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) | |
747 | { | |
748 | error ("can%'t mix operands of decimal float and vector types"); | |
749 | return error_mark_node; | |
750 | } | |
751 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
752 | { | |
753 | error ("can%'t mix operands of decimal float and complex types"); | |
754 | return error_mark_node; | |
755 | } | |
756 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
757 | { | |
758 | error ("can%'t mix operands of decimal float and other float types"); | |
759 | return error_mark_node; | |
760 | } | |
761 | } | |
762 | ||
10bc1b1b JM |
763 | /* If one type is a vector type, return that type. (How the usual |
764 | arithmetic conversions apply to the vector types extension is not | |
765 | precisely specified.) */ | |
766 | if (code1 == VECTOR_TYPE) | |
767 | return t1; | |
768 | ||
769 | if (code2 == VECTOR_TYPE) | |
770 | return t2; | |
771 | ||
772 | /* If one type is complex, form the common type of the non-complex | |
773 | components, then make that complex. Use T1 or T2 if it is the | |
774 | required type. */ | |
775 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
776 | { | |
777 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
778 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
ccf7f880 | 779 | tree subtype = c_common_type (subtype1, subtype2); |
10bc1b1b JM |
780 | |
781 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
782 | return t1; | |
783 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
784 | return t2; | |
785 | else | |
786 | return build_complex_type (subtype); | |
787 | } | |
788 | ||
789 | /* If only one is real, use it as the result. */ | |
790 | ||
791 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
792 | return t1; | |
793 | ||
794 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
795 | return t2; | |
796 | ||
9a8ce21f JG |
797 | /* If both are real and either are decimal floating point types, use |
798 | the decimal floating point type with the greater precision. */ | |
799 | ||
800 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
801 | { | |
802 | if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node | |
803 | || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) | |
804 | return dfloat128_type_node; | |
805 | else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node | |
806 | || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) | |
807 | return dfloat64_type_node; | |
808 | else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node | |
809 | || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) | |
810 | return dfloat32_type_node; | |
811 | } | |
812 | ||
ab22c1fa CF |
813 | /* Deal with fixed-point types. */ |
814 | if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) | |
815 | { | |
816 | unsigned int unsignedp = 0, satp = 0; | |
817 | enum machine_mode m1, m2; | |
818 | unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; | |
819 | ||
820 | m1 = TYPE_MODE (t1); | |
821 | m2 = TYPE_MODE (t2); | |
822 | ||
823 | /* If one input type is saturating, the result type is saturating. */ | |
824 | if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) | |
825 | satp = 1; | |
826 | ||
827 | /* If both fixed-point types are unsigned, the result type is unsigned. | |
828 | When mixing fixed-point and integer types, follow the sign of the | |
829 | fixed-point type. | |
830 | Otherwise, the result type is signed. */ | |
831 | if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) | |
832 | && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) | |
833 | || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE | |
834 | && TYPE_UNSIGNED (t1)) | |
835 | || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE | |
836 | && TYPE_UNSIGNED (t2))) | |
837 | unsignedp = 1; | |
838 | ||
839 | /* The result type is signed. */ | |
840 | if (unsignedp == 0) | |
841 | { | |
842 | /* If the input type is unsigned, we need to convert to the | |
843 | signed type. */ | |
844 | if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) | |
845 | { | |
d75d71e0 | 846 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
847 | if (GET_MODE_CLASS (m1) == MODE_UFRACT) |
848 | mclass = MODE_FRACT; | |
849 | else if (GET_MODE_CLASS (m1) == MODE_UACCUM) | |
850 | mclass = MODE_ACCUM; | |
851 | else | |
852 | gcc_unreachable (); | |
853 | m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); | |
854 | } | |
855 | if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) | |
856 | { | |
d75d71e0 | 857 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
858 | if (GET_MODE_CLASS (m2) == MODE_UFRACT) |
859 | mclass = MODE_FRACT; | |
860 | else if (GET_MODE_CLASS (m2) == MODE_UACCUM) | |
861 | mclass = MODE_ACCUM; | |
862 | else | |
863 | gcc_unreachable (); | |
864 | m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); | |
865 | } | |
866 | } | |
867 | ||
868 | if (code1 == FIXED_POINT_TYPE) | |
869 | { | |
870 | fbit1 = GET_MODE_FBIT (m1); | |
871 | ibit1 = GET_MODE_IBIT (m1); | |
872 | } | |
873 | else | |
874 | { | |
875 | fbit1 = 0; | |
876 | /* Signed integers need to subtract one sign bit. */ | |
877 | ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); | |
878 | } | |
879 | ||
880 | if (code2 == FIXED_POINT_TYPE) | |
881 | { | |
882 | fbit2 = GET_MODE_FBIT (m2); | |
883 | ibit2 = GET_MODE_IBIT (m2); | |
884 | } | |
885 | else | |
886 | { | |
887 | fbit2 = 0; | |
888 | /* Signed integers need to subtract one sign bit. */ | |
889 | ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); | |
890 | } | |
891 | ||
892 | max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; | |
893 | max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; | |
894 | return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, | |
895 | satp); | |
896 | } | |
897 | ||
10bc1b1b JM |
898 | /* Both real or both integers; use the one with greater precision. */ |
899 | ||
900 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
901 | return t1; | |
902 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
903 | return t2; | |
904 | ||
905 | /* Same precision. Prefer long longs to longs to ints when the | |
906 | same precision, following the C99 rules on integer type rank | |
907 | (which are equivalent to the C90 rules for C90 types). */ | |
908 | ||
909 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
910 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
911 | return long_long_unsigned_type_node; | |
912 | ||
913 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
914 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
915 | { | |
916 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
917 | return long_long_unsigned_type_node; | |
918 | else | |
c22cacf3 | 919 | return long_long_integer_type_node; |
10bc1b1b JM |
920 | } |
921 | ||
922 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
923 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
924 | return long_unsigned_type_node; | |
925 | ||
926 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
927 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
928 | { | |
929 | /* But preserve unsignedness from the other type, | |
930 | since long cannot hold all the values of an unsigned int. */ | |
931 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
932 | return long_unsigned_type_node; | |
933 | else | |
934 | return long_integer_type_node; | |
935 | } | |
936 | ||
937 | /* Likewise, prefer long double to double even if same size. */ | |
938 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
939 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
940 | return long_double_type_node; | |
941 | ||
2531a1d9 JR |
942 | /* Likewise, prefer double to float even if same size. |
943 | We got a couple of embedded targets with 32 bit doubles, and the | |
944 | pdp11 might have 64 bit floats. */ | |
945 | if (TYPE_MAIN_VARIANT (t1) == double_type_node | |
946 | || TYPE_MAIN_VARIANT (t2) == double_type_node) | |
947 | return double_type_node; | |
948 | ||
10bc1b1b JM |
949 | /* Otherwise prefer the unsigned one. */ |
950 | ||
951 | if (TYPE_UNSIGNED (t1)) | |
952 | return t1; | |
953 | else | |
954 | return t2; | |
955 | } | |
400fbf9f | 956 | \f |
5922c215 JM |
957 | /* Wrapper around c_common_type that is used by c-common.c and other |
958 | front end optimizations that remove promotions. ENUMERAL_TYPEs | |
b2232745 RS |
959 | are allowed here and are converted to their compatible integer types. |
960 | BOOLEAN_TYPEs are allowed here and return either boolean_type_node or | |
961 | preferably a non-Boolean type as the common type. */ | |
ccf7f880 JJ |
962 | tree |
963 | common_type (tree t1, tree t2) | |
964 | { | |
965 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
966 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); | |
967 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
968 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); | |
b2232745 RS |
969 | |
970 | /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ | |
971 | if (TREE_CODE (t1) == BOOLEAN_TYPE | |
972 | && TREE_CODE (t2) == BOOLEAN_TYPE) | |
973 | return boolean_type_node; | |
974 | ||
975 | /* If either type is BOOLEAN_TYPE, then return the other. */ | |
976 | if (TREE_CODE (t1) == BOOLEAN_TYPE) | |
977 | return t2; | |
978 | if (TREE_CODE (t2) == BOOLEAN_TYPE) | |
979 | return t1; | |
980 | ||
ccf7f880 JJ |
981 | return c_common_type (t1, t2); |
982 | } | |
f13c9b2c | 983 | |
400fbf9f JW |
984 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
985 | or various other operations. Return 2 if they are compatible | |
986 | but a warning may be needed if you use them together. */ | |
987 | ||
988 | int | |
132da1a5 | 989 | comptypes (tree type1, tree type2) |
f13c9b2c AP |
990 | { |
991 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
992 | int val; | |
993 | ||
dc5027f4 | 994 | val = comptypes_internal (type1, type2, NULL, NULL); |
744aa42f ILT |
995 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
996 | ||
997 | return val; | |
998 | } | |
999 | ||
1000 | /* Like comptypes, but if it returns non-zero because enum and int are | |
1001 | compatible, it sets *ENUM_AND_INT_P to true. */ | |
1002 | ||
1003 | static int | |
1004 | comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) | |
1005 | { | |
1006 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1007 | int val; | |
1008 | ||
dc5027f4 JM |
1009 | val = comptypes_internal (type1, type2, enum_and_int_p, NULL); |
1010 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
1011 | ||
1012 | return val; | |
1013 | } | |
1014 | ||
1015 | /* Like comptypes, but if it returns nonzero for different types, it | |
1016 | sets *DIFFERENT_TYPES_P to true. */ | |
1017 | ||
1018 | int | |
1019 | comptypes_check_different_types (tree type1, tree type2, | |
1020 | bool *different_types_p) | |
1021 | { | |
1022 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1023 | int val; | |
1024 | ||
1025 | val = comptypes_internal (type1, type2, NULL, different_types_p); | |
f13c9b2c | 1026 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
c22cacf3 | 1027 | |
f13c9b2c | 1028 | return val; |
c22cacf3 MS |
1029 | } |
1030 | \f | |
f13c9b2c AP |
1031 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
1032 | or various other operations. Return 2 if they are compatible | |
744aa42f ILT |
1033 | but a warning may be needed if you use them together. If |
1034 | ENUM_AND_INT_P is not NULL, and one type is an enum and the other a | |
1035 | compatible integer type, then this sets *ENUM_AND_INT_P to true; | |
dc5027f4 JM |
1036 | *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not |
1037 | NULL, and the types are compatible but different enough not to be | |
48b0b196 | 1038 | permitted in C11 typedef redeclarations, then this sets |
dc5027f4 JM |
1039 | *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to |
1040 | false, but may or may not be set if the types are incompatible. | |
1041 | This differs from comptypes, in that we don't free the seen | |
1042 | types. */ | |
f13c9b2c AP |
1043 | |
1044 | static int | |
dc5027f4 JM |
1045 | comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, |
1046 | bool *different_types_p) | |
400fbf9f | 1047 | { |
58f9752a KG |
1048 | const_tree t1 = type1; |
1049 | const_tree t2 = type2; | |
4b027d16 | 1050 | int attrval, val; |
400fbf9f JW |
1051 | |
1052 | /* Suppress errors caused by previously reported errors. */ | |
1053 | ||
8d47dfc5 RH |
1054 | if (t1 == t2 || !t1 || !t2 |
1055 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
400fbf9f JW |
1056 | return 1; |
1057 | ||
bca63328 JM |
1058 | /* Enumerated types are compatible with integer types, but this is |
1059 | not transitive: two enumerated types in the same translation unit | |
1060 | are compatible with each other only if they are the same type. */ | |
400fbf9f | 1061 | |
bca63328 | 1062 | if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) |
744aa42f ILT |
1063 | { |
1064 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); | |
dc5027f4 JM |
1065 | if (TREE_CODE (t2) != VOID_TYPE) |
1066 | { | |
1067 | if (enum_and_int_p != NULL) | |
1068 | *enum_and_int_p = true; | |
1069 | if (different_types_p != NULL) | |
1070 | *different_types_p = true; | |
1071 | } | |
744aa42f | 1072 | } |
bca63328 | 1073 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) |
744aa42f ILT |
1074 | { |
1075 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); | |
dc5027f4 JM |
1076 | if (TREE_CODE (t1) != VOID_TYPE) |
1077 | { | |
1078 | if (enum_and_int_p != NULL) | |
1079 | *enum_and_int_p = true; | |
1080 | if (different_types_p != NULL) | |
1081 | *different_types_p = true; | |
1082 | } | |
744aa42f | 1083 | } |
400fbf9f JW |
1084 | |
1085 | if (t1 == t2) | |
1086 | return 1; | |
1087 | ||
1088 | /* Different classes of types can't be compatible. */ | |
1089 | ||
3aeb3655 EC |
1090 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
1091 | return 0; | |
400fbf9f | 1092 | |
118a3a8b | 1093 | /* Qualifiers must match. C99 6.7.3p9 */ |
400fbf9f | 1094 | |
3932261a | 1095 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
400fbf9f JW |
1096 | return 0; |
1097 | ||
08632da2 RS |
1098 | /* Allow for two different type nodes which have essentially the same |
1099 | definition. Note that we already checked for equality of the type | |
38e01259 | 1100 | qualifiers (just above). */ |
400fbf9f | 1101 | |
46df2823 JM |
1102 | if (TREE_CODE (t1) != ARRAY_TYPE |
1103 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
400fbf9f JW |
1104 | return 1; |
1105 | ||
4b027d16 | 1106 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ |
ac9a30ae | 1107 | if (!(attrval = comp_type_attributes (t1, t2))) |
4b027d16 RK |
1108 | return 0; |
1109 | ||
1110 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1111 | val = 0; | |
1112 | ||
400fbf9f JW |
1113 | switch (TREE_CODE (t1)) |
1114 | { | |
1115 | case POINTER_TYPE: | |
106f5de5 UW |
1116 | /* Do not remove mode or aliasing information. */ |
1117 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1118 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) | |
1119 | break; | |
4b027d16 | 1120 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
744aa42f | 1121 | ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 | 1122 | enum_and_int_p, different_types_p)); |
4b027d16 | 1123 | break; |
400fbf9f JW |
1124 | |
1125 | case FUNCTION_TYPE: | |
dc5027f4 JM |
1126 | val = function_types_compatible_p (t1, t2, enum_and_int_p, |
1127 | different_types_p); | |
4b027d16 | 1128 | break; |
400fbf9f JW |
1129 | |
1130 | case ARRAY_TYPE: | |
1131 | { | |
400fbf9f JW |
1132 | tree d1 = TYPE_DOMAIN (t1); |
1133 | tree d2 = TYPE_DOMAIN (t2); | |
3f85558f RH |
1134 | bool d1_variable, d2_variable; |
1135 | bool d1_zero, d2_zero; | |
4b027d16 | 1136 | val = 1; |
400fbf9f JW |
1137 | |
1138 | /* Target types must match incl. qualifiers. */ | |
1139 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
744aa42f | 1140 | && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 JM |
1141 | enum_and_int_p, |
1142 | different_types_p))) | |
400fbf9f JW |
1143 | return 0; |
1144 | ||
dc5027f4 JM |
1145 | if (different_types_p != NULL |
1146 | && (d1 == 0) != (d2 == 0)) | |
1147 | *different_types_p = true; | |
400fbf9f | 1148 | /* Sizes must match unless one is missing or variable. */ |
3f85558f | 1149 | if (d1 == 0 || d2 == 0 || d1 == d2) |
4b027d16 | 1150 | break; |
400fbf9f | 1151 | |
3f75a254 JM |
1152 | d1_zero = !TYPE_MAX_VALUE (d1); |
1153 | d2_zero = !TYPE_MAX_VALUE (d2); | |
3f85558f | 1154 | |
3f75a254 | 1155 | d1_variable = (!d1_zero |
3f85558f RH |
1156 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST |
1157 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
3f75a254 | 1158 | d2_variable = (!d2_zero |
3f85558f RH |
1159 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST |
1160 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
1161 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
1162 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
3f85558f | 1163 | |
dc5027f4 JM |
1164 | if (different_types_p != NULL |
1165 | && d1_variable != d2_variable) | |
1166 | *different_types_p = true; | |
3f85558f RH |
1167 | if (d1_variable || d2_variable) |
1168 | break; | |
1169 | if (d1_zero && d2_zero) | |
1170 | break; | |
1171 | if (d1_zero || d2_zero | |
3f75a254 JM |
1172 | || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) |
1173 | || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) | |
05bccae2 RK |
1174 | val = 0; |
1175 | ||
c22cacf3 | 1176 | break; |
400fbf9f JW |
1177 | } |
1178 | ||
d1bd0ded | 1179 | case ENUMERAL_TYPE: |
58393038 | 1180 | case RECORD_TYPE: |
d1bd0ded | 1181 | case UNION_TYPE: |
766beae1 | 1182 | if (val != 1 && !same_translation_unit_p (t1, t2)) |
c22cacf3 | 1183 | { |
fcb99e7b JJ |
1184 | tree a1 = TYPE_ATTRIBUTES (t1); |
1185 | tree a2 = TYPE_ATTRIBUTES (t2); | |
1186 | ||
1187 | if (! attribute_list_contained (a1, a2) | |
1188 | && ! attribute_list_contained (a2, a1)) | |
1189 | break; | |
1190 | ||
f13c9b2c | 1191 | if (attrval != 2) |
dc5027f4 JM |
1192 | return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, |
1193 | different_types_p); | |
1194 | val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, | |
1195 | different_types_p); | |
f13c9b2c | 1196 | } |
4b027d16 | 1197 | break; |
e9a25f70 | 1198 | |
62e1dfcf | 1199 | case VECTOR_TYPE: |
744aa42f ILT |
1200 | val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) |
1201 | && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
dc5027f4 | 1202 | enum_and_int_p, different_types_p)); |
62e1dfcf NC |
1203 | break; |
1204 | ||
e9a25f70 JL |
1205 | default: |
1206 | break; | |
400fbf9f | 1207 | } |
4b027d16 | 1208 | return attrval == 2 && val == 1 ? 2 : val; |
400fbf9f JW |
1209 | } |
1210 | ||
36c5e70a BE |
1211 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring |
1212 | their qualifiers, except for named address spaces. If the pointers point to | |
1213 | different named addresses, then we must determine if one address space is a | |
1214 | subset of the other. */ | |
400fbf9f JW |
1215 | |
1216 | static int | |
744aa42f | 1217 | comp_target_types (location_t location, tree ttl, tree ttr) |
400fbf9f | 1218 | { |
392202b0 | 1219 | int val; |
36c5e70a BE |
1220 | tree mvl = TREE_TYPE (ttl); |
1221 | tree mvr = TREE_TYPE (ttr); | |
1222 | addr_space_t asl = TYPE_ADDR_SPACE (mvl); | |
1223 | addr_space_t asr = TYPE_ADDR_SPACE (mvr); | |
1224 | addr_space_t as_common; | |
744aa42f | 1225 | bool enum_and_int_p; |
8b40563c | 1226 | |
36c5e70a BE |
1227 | /* Fail if pointers point to incompatible address spaces. */ |
1228 | if (!addr_space_superset (asl, asr, &as_common)) | |
1229 | return 0; | |
1230 | ||
46df2823 JM |
1231 | /* Do not lose qualifiers on element types of array types that are |
1232 | pointer targets by taking their TYPE_MAIN_VARIANT. */ | |
46df2823 | 1233 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
267bac10 JM |
1234 | mvl = (TYPE_ATOMIC (mvl) |
1235 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC) | |
1236 | : TYPE_MAIN_VARIANT (mvl)); | |
46df2823 | 1237 | if (TREE_CODE (mvr) != ARRAY_TYPE) |
267bac10 JM |
1238 | mvr = (TYPE_ATOMIC (mvr) |
1239 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC) | |
1240 | : TYPE_MAIN_VARIANT (mvr)); | |
744aa42f ILT |
1241 | enum_and_int_p = false; |
1242 | val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); | |
8b40563c | 1243 | |
fcf73884 | 1244 | if (val == 2) |
c1771a20 | 1245 | pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); |
744aa42f ILT |
1246 | |
1247 | if (val == 1 && enum_and_int_p && warn_cxx_compat) | |
1248 | warning_at (location, OPT_Wc___compat, | |
1249 | "pointer target types incompatible in C++"); | |
1250 | ||
400fbf9f JW |
1251 | return val; |
1252 | } | |
1253 | \f | |
1254 | /* Subroutines of `comptypes'. */ | |
1255 | ||
f75fbaf7 ZW |
1256 | /* Determine whether two trees derive from the same translation unit. |
1257 | If the CONTEXT chain ends in a null, that tree's context is still | |
1258 | being parsed, so if two trees have context chains ending in null, | |
766beae1 | 1259 | they're in the same translation unit. */ |
f75fbaf7 | 1260 | int |
58f9752a | 1261 | same_translation_unit_p (const_tree t1, const_tree t2) |
766beae1 ZW |
1262 | { |
1263 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
1264 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
1265 | { | |
6615c446 JO |
1266 | case tcc_declaration: |
1267 | t1 = DECL_CONTEXT (t1); break; | |
1268 | case tcc_type: | |
1269 | t1 = TYPE_CONTEXT (t1); break; | |
1270 | case tcc_exceptional: | |
1271 | t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ | |
366de0ce | 1272 | default: gcc_unreachable (); |
766beae1 ZW |
1273 | } |
1274 | ||
1275 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
1276 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
1277 | { | |
6615c446 JO |
1278 | case tcc_declaration: |
1279 | t2 = DECL_CONTEXT (t2); break; | |
1280 | case tcc_type: | |
1281 | t2 = TYPE_CONTEXT (t2); break; | |
1282 | case tcc_exceptional: | |
1283 | t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ | |
366de0ce | 1284 | default: gcc_unreachable (); |
766beae1 ZW |
1285 | } |
1286 | ||
1287 | return t1 == t2; | |
1288 | } | |
1289 | ||
f13c9b2c | 1290 | /* Allocate the seen two types, assuming that they are compatible. */ |
d1bd0ded | 1291 | |
f13c9b2c | 1292 | static struct tagged_tu_seen_cache * |
58f9752a | 1293 | alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) |
f13c9b2c | 1294 | { |
cceb1885 | 1295 | struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); |
f13c9b2c AP |
1296 | tu->next = tagged_tu_seen_base; |
1297 | tu->t1 = t1; | |
1298 | tu->t2 = t2; | |
c22cacf3 | 1299 | |
f13c9b2c | 1300 | tagged_tu_seen_base = tu; |
c22cacf3 | 1301 | |
f13c9b2c AP |
1302 | /* The C standard says that two structures in different translation |
1303 | units are compatible with each other only if the types of their | |
1304 | fields are compatible (among other things). We assume that they | |
1305 | are compatible until proven otherwise when building the cache. | |
1306 | An example where this can occur is: | |
1307 | struct a | |
1308 | { | |
1309 | struct a *next; | |
1310 | }; | |
1311 | If we are comparing this against a similar struct in another TU, | |
c83eecad | 1312 | and did not assume they were compatible, we end up with an infinite |
f13c9b2c AP |
1313 | loop. */ |
1314 | tu->val = 1; | |
1315 | return tu; | |
1316 | } | |
d1bd0ded | 1317 | |
f13c9b2c | 1318 | /* Free the seen types until we get to TU_TIL. */ |
d1bd0ded | 1319 | |
f13c9b2c AP |
1320 | static void |
1321 | free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) | |
1322 | { | |
1323 | const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; | |
1324 | while (tu != tu_til) | |
1325 | { | |
741ac903 KG |
1326 | const struct tagged_tu_seen_cache *const tu1 |
1327 | = (const struct tagged_tu_seen_cache *) tu; | |
f13c9b2c | 1328 | tu = tu1->next; |
b1d5455a | 1329 | free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); |
f13c9b2c AP |
1330 | } |
1331 | tagged_tu_seen_base = tu_til; | |
1332 | } | |
d1bd0ded GK |
1333 | |
1334 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
1335 | compatible. If the two types are not the same (which has been | |
1336 | checked earlier), this can only happen when multiple translation | |
1337 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
dc5027f4 JM |
1338 | rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1339 | comptypes_internal. */ | |
d1bd0ded GK |
1340 | |
1341 | static int | |
744aa42f | 1342 | tagged_types_tu_compatible_p (const_tree t1, const_tree t2, |
dc5027f4 | 1343 | bool *enum_and_int_p, bool *different_types_p) |
d1bd0ded GK |
1344 | { |
1345 | tree s1, s2; | |
1346 | bool needs_warning = false; | |
3aeb3655 | 1347 | |
d1bd0ded GK |
1348 | /* We have to verify that the tags of the types are the same. This |
1349 | is harder than it looks because this may be a typedef, so we have | |
1350 | to go look at the original type. It may even be a typedef of a | |
6de9cd9a DN |
1351 | typedef... |
1352 | In the case of compiler-created builtin structs the TYPE_DECL | |
1353 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
dea984dc ILT |
1354 | while (TYPE_NAME (t1) |
1355 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
1356 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
d1bd0ded GK |
1357 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); |
1358 | ||
dea984dc ILT |
1359 | while (TYPE_NAME (t2) |
1360 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
1361 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
d1bd0ded GK |
1362 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); |
1363 | ||
1364 | /* C90 didn't have the requirement that the two tags be the same. */ | |
1365 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
1366 | return 0; | |
3aeb3655 | 1367 | |
d1bd0ded GK |
1368 | /* C90 didn't say what happened if one or both of the types were |
1369 | incomplete; we choose to follow C99 rules here, which is that they | |
1370 | are compatible. */ | |
1371 | if (TYPE_SIZE (t1) == NULL | |
1372 | || TYPE_SIZE (t2) == NULL) | |
1373 | return 1; | |
3aeb3655 | 1374 | |
d1bd0ded | 1375 | { |
f13c9b2c | 1376 | const struct tagged_tu_seen_cache * tts_i; |
d1bd0ded GK |
1377 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) |
1378 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
f13c9b2c | 1379 | return tts_i->val; |
d1bd0ded | 1380 | } |
3aeb3655 | 1381 | |
d1bd0ded GK |
1382 | switch (TREE_CODE (t1)) |
1383 | { | |
1384 | case ENUMERAL_TYPE: | |
1385 | { | |
f13c9b2c | 1386 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
c22cacf3 MS |
1387 | /* Speed up the case where the type values are in the same order. */ |
1388 | tree tv1 = TYPE_VALUES (t1); | |
1389 | tree tv2 = TYPE_VALUES (t2); | |
3aeb3655 | 1390 | |
c22cacf3 | 1391 | if (tv1 == tv2) |
f13c9b2c AP |
1392 | { |
1393 | return 1; | |
1394 | } | |
3aeb3655 | 1395 | |
c22cacf3 MS |
1396 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) |
1397 | { | |
1398 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) | |
1399 | break; | |
1400 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
f13c9b2c | 1401 | { |
c22cacf3 | 1402 | tu->val = 0; |
f13c9b2c AP |
1403 | return 0; |
1404 | } | |
c22cacf3 | 1405 | } |
3aeb3655 | 1406 | |
c22cacf3 | 1407 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) |
f13c9b2c AP |
1408 | { |
1409 | return 1; | |
1410 | } | |
c22cacf3 | 1411 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) |
f13c9b2c AP |
1412 | { |
1413 | tu->val = 0; | |
1414 | return 0; | |
1415 | } | |
3aeb3655 | 1416 | |
d1bd0ded | 1417 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) |
f13c9b2c AP |
1418 | { |
1419 | tu->val = 0; | |
1420 | return 0; | |
1421 | } | |
3aeb3655 | 1422 | |
d1bd0ded GK |
1423 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) |
1424 | { | |
1425 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
1426 | if (s2 == NULL | |
1427 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
f13c9b2c AP |
1428 | { |
1429 | tu->val = 0; | |
1430 | return 0; | |
1431 | } | |
d1bd0ded GK |
1432 | } |
1433 | return 1; | |
1434 | } | |
1435 | ||
1436 | case UNION_TYPE: | |
1437 | { | |
f13c9b2c | 1438 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
d1bd0ded | 1439 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) |
f13c9b2c AP |
1440 | { |
1441 | tu->val = 0; | |
1442 | return 0; | |
1443 | } | |
c22cacf3 | 1444 | |
f13c9b2c AP |
1445 | /* Speed up the common case where the fields are in the same order. */ |
1446 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; | |
910ad8de | 1447 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
f13c9b2c AP |
1448 | { |
1449 | int result; | |
c22cacf3 | 1450 | |
3ae4d3cc | 1451 | if (DECL_NAME (s1) != DECL_NAME (s2)) |
f13c9b2c | 1452 | break; |
744aa42f | 1453 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1454 | enum_and_int_p, different_types_p); |
3ae4d3cc AO |
1455 | |
1456 | if (result != 1 && !DECL_NAME (s1)) | |
1457 | break; | |
f13c9b2c AP |
1458 | if (result == 0) |
1459 | { | |
1460 | tu->val = 0; | |
1461 | return 0; | |
1462 | } | |
1463 | if (result == 2) | |
1464 | needs_warning = true; | |
1465 | ||
1466 | if (TREE_CODE (s1) == FIELD_DECL | |
1467 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1468 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1469 | { | |
1470 | tu->val = 0; | |
1471 | return 0; | |
1472 | } | |
1473 | } | |
1474 | if (!s1 && !s2) | |
1475 | { | |
1476 | tu->val = needs_warning ? 2 : 1; | |
1477 | return tu->val; | |
1478 | } | |
d1bd0ded | 1479 | |
910ad8de | 1480 | for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1)) |
d1bd0ded GK |
1481 | { |
1482 | bool ok = false; | |
3aeb3655 | 1483 | |
910ad8de | 1484 | for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2)) |
3ae4d3cc AO |
1485 | if (DECL_NAME (s1) == DECL_NAME (s2)) |
1486 | { | |
1487 | int result; | |
1488 | ||
744aa42f | 1489 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 JM |
1490 | enum_and_int_p, |
1491 | different_types_p); | |
3ae4d3cc AO |
1492 | |
1493 | if (result != 1 && !DECL_NAME (s1)) | |
1494 | continue; | |
1495 | if (result == 0) | |
1496 | { | |
1497 | tu->val = 0; | |
1498 | return 0; | |
1499 | } | |
1500 | if (result == 2) | |
1501 | needs_warning = true; | |
1502 | ||
1503 | if (TREE_CODE (s1) == FIELD_DECL | |
1504 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1505 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
d1bd0ded | 1506 | break; |
3ae4d3cc AO |
1507 | |
1508 | ok = true; | |
1509 | break; | |
1510 | } | |
3f75a254 | 1511 | if (!ok) |
f13c9b2c AP |
1512 | { |
1513 | tu->val = 0; | |
1514 | return 0; | |
1515 | } | |
d1bd0ded | 1516 | } |
f13c9b2c AP |
1517 | tu->val = needs_warning ? 2 : 10; |
1518 | return tu->val; | |
d1bd0ded GK |
1519 | } |
1520 | ||
1521 | case RECORD_TYPE: | |
1522 | { | |
c22cacf3 | 1523 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
3aeb3655 EC |
1524 | |
1525 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
d1bd0ded | 1526 | s1 && s2; |
910ad8de | 1527 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
d1bd0ded GK |
1528 | { |
1529 | int result; | |
1530 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
1531 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
1532 | break; | |
744aa42f | 1533 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1534 | enum_and_int_p, different_types_p); |
d1bd0ded GK |
1535 | if (result == 0) |
1536 | break; | |
1537 | if (result == 2) | |
1538 | needs_warning = true; | |
3aeb3655 | 1539 | |
d1bd0ded GK |
1540 | if (TREE_CODE (s1) == FIELD_DECL |
1541 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1542 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1543 | break; | |
1544 | } | |
d1bd0ded | 1545 | if (s1 && s2) |
f13c9b2c AP |
1546 | tu->val = 0; |
1547 | else | |
1548 | tu->val = needs_warning ? 2 : 1; | |
1549 | return tu->val; | |
d1bd0ded GK |
1550 | } |
1551 | ||
1552 | default: | |
366de0ce | 1553 | gcc_unreachable (); |
d1bd0ded GK |
1554 | } |
1555 | } | |
1556 | ||
400fbf9f JW |
1557 | /* Return 1 if two function types F1 and F2 are compatible. |
1558 | If either type specifies no argument types, | |
1559 | the other must specify a fixed number of self-promoting arg types. | |
2f6e4e97 | 1560 | Otherwise, if one type specifies only the number of arguments, |
400fbf9f | 1561 | the other must specify that number of self-promoting arg types. |
744aa42f | 1562 | Otherwise, the argument types must match. |
dc5027f4 | 1563 | ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */ |
400fbf9f JW |
1564 | |
1565 | static int | |
744aa42f | 1566 | function_types_compatible_p (const_tree f1, const_tree f2, |
dc5027f4 | 1567 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1568 | { |
1569 | tree args1, args2; | |
1570 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1571 | int val = 1; | |
1572 | int val1; | |
a6fdc086 GK |
1573 | tree ret1, ret2; |
1574 | ||
1575 | ret1 = TREE_TYPE (f1); | |
1576 | ret2 = TREE_TYPE (f2); | |
1577 | ||
e508a019 JM |
1578 | /* 'volatile' qualifiers on a function's return type used to mean |
1579 | the function is noreturn. */ | |
1580 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
509c9d60 | 1581 | pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); |
a6fdc086 GK |
1582 | if (TYPE_VOLATILE (ret1)) |
1583 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
1584 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
1585 | if (TYPE_VOLATILE (ret2)) | |
1586 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
1587 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
dc5027f4 | 1588 | val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p); |
a6fdc086 | 1589 | if (val == 0) |
400fbf9f JW |
1590 | return 0; |
1591 | ||
1592 | args1 = TYPE_ARG_TYPES (f1); | |
1593 | args2 = TYPE_ARG_TYPES (f2); | |
1594 | ||
dc5027f4 JM |
1595 | if (different_types_p != NULL |
1596 | && (args1 == 0) != (args2 == 0)) | |
1597 | *different_types_p = true; | |
1598 | ||
400fbf9f JW |
1599 | /* An unspecified parmlist matches any specified parmlist |
1600 | whose argument types don't need default promotions. */ | |
1601 | ||
1602 | if (args1 == 0) | |
1603 | { | |
1604 | if (!self_promoting_args_p (args2)) | |
1605 | return 0; | |
1606 | /* If one of these types comes from a non-prototype fn definition, | |
1607 | compare that with the other type's arglist. | |
3176a0c2 | 1608 | If they don't match, ask for a warning (but no error). */ |
400fbf9f | 1609 | if (TYPE_ACTUAL_ARG_TYPES (f1) |
744aa42f | 1610 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), |
dc5027f4 | 1611 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1612 | val = 2; |
1613 | return val; | |
1614 | } | |
1615 | if (args2 == 0) | |
1616 | { | |
1617 | if (!self_promoting_args_p (args1)) | |
1618 | return 0; | |
1619 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
744aa42f | 1620 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), |
dc5027f4 | 1621 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1622 | val = 2; |
1623 | return val; | |
1624 | } | |
1625 | ||
1626 | /* Both types have argument lists: compare them and propagate results. */ | |
dc5027f4 JM |
1627 | val1 = type_lists_compatible_p (args1, args2, enum_and_int_p, |
1628 | different_types_p); | |
400fbf9f JW |
1629 | return val1 != 1 ? val1 : val; |
1630 | } | |
1631 | ||
744aa42f ILT |
1632 | /* Check two lists of types for compatibility, returning 0 for |
1633 | incompatible, 1 for compatible, or 2 for compatible with | |
dc5027f4 JM |
1634 | warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1635 | comptypes_internal. */ | |
400fbf9f JW |
1636 | |
1637 | static int | |
744aa42f | 1638 | type_lists_compatible_p (const_tree args1, const_tree args2, |
dc5027f4 | 1639 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1640 | { |
1641 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1642 | int val = 1; | |
9d5f3e49 | 1643 | int newval = 0; |
400fbf9f JW |
1644 | |
1645 | while (1) | |
1646 | { | |
46df2823 | 1647 | tree a1, mv1, a2, mv2; |
400fbf9f JW |
1648 | if (args1 == 0 && args2 == 0) |
1649 | return val; | |
1650 | /* If one list is shorter than the other, | |
1651 | they fail to match. */ | |
1652 | if (args1 == 0 || args2 == 0) | |
1653 | return 0; | |
46df2823 JM |
1654 | mv1 = a1 = TREE_VALUE (args1); |
1655 | mv2 = a2 = TREE_VALUE (args2); | |
1656 | if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) | |
267bac10 JM |
1657 | mv1 = (TYPE_ATOMIC (mv1) |
1658 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1), | |
1659 | TYPE_QUAL_ATOMIC) | |
1660 | : TYPE_MAIN_VARIANT (mv1)); | |
46df2823 | 1661 | if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) |
267bac10 JM |
1662 | mv2 = (TYPE_ATOMIC (mv2) |
1663 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2), | |
1664 | TYPE_QUAL_ATOMIC) | |
1665 | : TYPE_MAIN_VARIANT (mv2)); | |
400fbf9f JW |
1666 | /* A null pointer instead of a type |
1667 | means there is supposed to be an argument | |
1668 | but nothing is specified about what type it has. | |
1669 | So match anything that self-promotes. */ | |
dc5027f4 JM |
1670 | if (different_types_p != NULL |
1671 | && (a1 == 0) != (a2 == 0)) | |
1672 | *different_types_p = true; | |
46df2823 | 1673 | if (a1 == 0) |
400fbf9f | 1674 | { |
46df2823 | 1675 | if (c_type_promotes_to (a2) != a2) |
400fbf9f JW |
1676 | return 0; |
1677 | } | |
46df2823 | 1678 | else if (a2 == 0) |
400fbf9f | 1679 | { |
46df2823 | 1680 | if (c_type_promotes_to (a1) != a1) |
400fbf9f JW |
1681 | return 0; |
1682 | } | |
8f5b6d29 | 1683 | /* If one of the lists has an error marker, ignore this arg. */ |
46df2823 JM |
1684 | else if (TREE_CODE (a1) == ERROR_MARK |
1685 | || TREE_CODE (a2) == ERROR_MARK) | |
8f5b6d29 | 1686 | ; |
dc5027f4 JM |
1687 | else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p, |
1688 | different_types_p))) | |
400fbf9f | 1689 | { |
dc5027f4 JM |
1690 | if (different_types_p != NULL) |
1691 | *different_types_p = true; | |
400fbf9f JW |
1692 | /* Allow wait (union {union wait *u; int *i} *) |
1693 | and wait (union wait *) to be compatible. */ | |
46df2823 JM |
1694 | if (TREE_CODE (a1) == UNION_TYPE |
1695 | && (TYPE_NAME (a1) == 0 | |
ebf0bf7f | 1696 | || TYPE_TRANSPARENT_AGGR (a1)) |
46df2823 JM |
1697 | && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST |
1698 | && tree_int_cst_equal (TYPE_SIZE (a1), | |
1699 | TYPE_SIZE (a2))) | |
400fbf9f JW |
1700 | { |
1701 | tree memb; | |
46df2823 | 1702 | for (memb = TYPE_FIELDS (a1); |
910ad8de | 1703 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1704 | { |
1705 | tree mv3 = TREE_TYPE (memb); | |
1706 | if (mv3 && mv3 != error_mark_node | |
1707 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
267bac10 JM |
1708 | mv3 = (TYPE_ATOMIC (mv3) |
1709 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), | |
1710 | TYPE_QUAL_ATOMIC) | |
1711 | : TYPE_MAIN_VARIANT (mv3)); | |
dc5027f4 JM |
1712 | if (comptypes_internal (mv3, mv2, enum_and_int_p, |
1713 | different_types_p)) | |
58cb41e6 JJ |
1714 | break; |
1715 | } | |
400fbf9f JW |
1716 | if (memb == 0) |
1717 | return 0; | |
1718 | } | |
46df2823 JM |
1719 | else if (TREE_CODE (a2) == UNION_TYPE |
1720 | && (TYPE_NAME (a2) == 0 | |
ebf0bf7f | 1721 | || TYPE_TRANSPARENT_AGGR (a2)) |
46df2823 JM |
1722 | && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST |
1723 | && tree_int_cst_equal (TYPE_SIZE (a2), | |
1724 | TYPE_SIZE (a1))) | |
400fbf9f JW |
1725 | { |
1726 | tree memb; | |
46df2823 | 1727 | for (memb = TYPE_FIELDS (a2); |
910ad8de | 1728 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1729 | { |
1730 | tree mv3 = TREE_TYPE (memb); | |
1731 | if (mv3 && mv3 != error_mark_node | |
1732 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
267bac10 JM |
1733 | mv3 = (TYPE_ATOMIC (mv3) |
1734 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), | |
1735 | TYPE_QUAL_ATOMIC) | |
1736 | : TYPE_MAIN_VARIANT (mv3)); | |
dc5027f4 JM |
1737 | if (comptypes_internal (mv3, mv1, enum_and_int_p, |
1738 | different_types_p)) | |
58cb41e6 JJ |
1739 | break; |
1740 | } | |
400fbf9f JW |
1741 | if (memb == 0) |
1742 | return 0; | |
1743 | } | |
1744 | else | |
1745 | return 0; | |
1746 | } | |
1747 | ||
1748 | /* comptypes said ok, but record if it said to warn. */ | |
1749 | if (newval > val) | |
1750 | val = newval; | |
1751 | ||
1752 | args1 = TREE_CHAIN (args1); | |
1753 | args2 = TREE_CHAIN (args2); | |
1754 | } | |
1755 | } | |
400fbf9f | 1756 | \f |
a0e24419 MP |
1757 | /* Compute the size to increment a pointer by. When a function type or void |
1758 | type or incomplete type is passed, size_one_node is returned. | |
1759 | This function does not emit any diagnostics; the caller is responsible | |
1760 | for that. */ | |
400fbf9f | 1761 | |
4e2fb7de | 1762 | static tree |
58f9752a | 1763 | c_size_in_bytes (const_tree type) |
400fbf9f JW |
1764 | { |
1765 | enum tree_code code = TREE_CODE (type); | |
1766 | ||
a0e24419 MP |
1767 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK |
1768 | || !COMPLETE_TYPE_P (type)) | |
fed3cef0 RK |
1769 | return size_one_node; |
1770 | ||
400fbf9f | 1771 | /* Convert in case a char is more than one unit. */ |
db3927fb AH |
1772 | return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
1773 | size_int (TYPE_PRECISION (char_type_node) | |
1774 | / BITS_PER_UNIT)); | |
400fbf9f | 1775 | } |
400fbf9f | 1776 | \f |
400fbf9f JW |
1777 | /* Return either DECL or its known constant value (if it has one). */ |
1778 | ||
56cb9733 | 1779 | tree |
2f6e4e97 | 1780 | decl_constant_value (tree decl) |
400fbf9f | 1781 | { |
a7c1916a | 1782 | if (/* Don't change a variable array bound or initial value to a constant |
4f976745 RK |
1783 | in a place where a variable is invalid. Note that DECL_INITIAL |
1784 | isn't valid for a PARM_DECL. */ | |
a7c1916a | 1785 | current_function_decl != 0 |
4f976745 | 1786 | && TREE_CODE (decl) != PARM_DECL |
3f75a254 | 1787 | && !TREE_THIS_VOLATILE (decl) |
83bab8db | 1788 | && TREE_READONLY (decl) |
400fbf9f JW |
1789 | && DECL_INITIAL (decl) != 0 |
1790 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1791 | /* This is invalid if initial value is not constant. | |
1792 | If it has either a function call, a memory reference, | |
1793 | or a variable, then re-evaluating it could give different results. */ | |
1794 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1795 | /* Check for cases where this is sub-optimal, even though valid. */ | |
2f74f7e9 | 1796 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
400fbf9f JW |
1797 | return DECL_INITIAL (decl); |
1798 | return decl; | |
1799 | } | |
1800 | ||
f2a71bbc JM |
1801 | /* Convert the array expression EXP to a pointer. */ |
1802 | static tree | |
c2255bc4 | 1803 | array_to_pointer_conversion (location_t loc, tree exp) |
207bf485 | 1804 | { |
f2a71bbc | 1805 | tree orig_exp = exp; |
207bf485 | 1806 | tree type = TREE_TYPE (exp); |
f2a71bbc JM |
1807 | tree adr; |
1808 | tree restype = TREE_TYPE (type); | |
1809 | tree ptrtype; | |
207bf485 | 1810 | |
f2a71bbc | 1811 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
207bf485 | 1812 | |
f2a71bbc | 1813 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1814 | |
487a92fe JM |
1815 | if (TREE_NO_WARNING (orig_exp)) |
1816 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1817 | |
f2a71bbc JM |
1818 | ptrtype = build_pointer_type (restype); |
1819 | ||
1820 | if (TREE_CODE (exp) == INDIRECT_REF) | |
1821 | return convert (ptrtype, TREE_OPERAND (exp, 0)); | |
1822 | ||
1f37c583 JM |
1823 | /* In C++ array compound literals are temporary objects unless they are |
1824 | const or appear in namespace scope, so they are destroyed too soon | |
1825 | to use them for much of anything (c++/53220). */ | |
1826 | if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) | |
1827 | { | |
1828 | tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
1829 | if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) | |
1830 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, | |
1831 | "converting an array compound literal to a pointer " | |
1832 | "is ill-formed in C++"); | |
1833 | } | |
1834 | ||
c2255bc4 | 1835 | adr = build_unary_op (loc, ADDR_EXPR, exp, 1); |
f2a71bbc JM |
1836 | return convert (ptrtype, adr); |
1837 | } | |
207bf485 | 1838 | |
f2a71bbc JM |
1839 | /* Convert the function expression EXP to a pointer. */ |
1840 | static tree | |
c2255bc4 | 1841 | function_to_pointer_conversion (location_t loc, tree exp) |
f2a71bbc JM |
1842 | { |
1843 | tree orig_exp = exp; | |
207bf485 | 1844 | |
f2a71bbc | 1845 | gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); |
207bf485 | 1846 | |
f2a71bbc | 1847 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1848 | |
f2a71bbc JM |
1849 | if (TREE_NO_WARNING (orig_exp)) |
1850 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1851 | |
c2255bc4 | 1852 | return build_unary_op (loc, ADDR_EXPR, exp, 0); |
f2a71bbc | 1853 | } |
207bf485 | 1854 | |
ebfbbdc5 JJ |
1855 | /* Mark EXP as read, not just set, for set but not used -Wunused |
1856 | warning purposes. */ | |
1857 | ||
1858 | void | |
1859 | mark_exp_read (tree exp) | |
1860 | { | |
1861 | switch (TREE_CODE (exp)) | |
1862 | { | |
1863 | case VAR_DECL: | |
1864 | case PARM_DECL: | |
1865 | DECL_READ_P (exp) = 1; | |
1866 | break; | |
1867 | case ARRAY_REF: | |
1868 | case COMPONENT_REF: | |
1869 | case MODIFY_EXPR: | |
1870 | case REALPART_EXPR: | |
1871 | case IMAGPART_EXPR: | |
1872 | CASE_CONVERT: | |
1873 | case ADDR_EXPR: | |
1874 | mark_exp_read (TREE_OPERAND (exp, 0)); | |
1875 | break; | |
1876 | case COMPOUND_EXPR: | |
82c3c067 | 1877 | case C_MAYBE_CONST_EXPR: |
ebfbbdc5 JJ |
1878 | mark_exp_read (TREE_OPERAND (exp, 1)); |
1879 | break; | |
1880 | default: | |
1881 | break; | |
1882 | } | |
1883 | } | |
1884 | ||
f2a71bbc JM |
1885 | /* Perform the default conversion of arrays and functions to pointers. |
1886 | Return the result of converting EXP. For any other expression, just | |
c2255bc4 AH |
1887 | return EXP. |
1888 | ||
1889 | LOC is the location of the expression. */ | |
f2a71bbc JM |
1890 | |
1891 | struct c_expr | |
c2255bc4 | 1892 | default_function_array_conversion (location_t loc, struct c_expr exp) |
f2a71bbc JM |
1893 | { |
1894 | tree orig_exp = exp.value; | |
1895 | tree type = TREE_TYPE (exp.value); | |
1896 | enum tree_code code = TREE_CODE (type); | |
1897 | ||
1898 | switch (code) | |
1899 | { | |
1900 | case ARRAY_TYPE: | |
1901 | { | |
1902 | bool not_lvalue = false; | |
1903 | bool lvalue_array_p; | |
1904 | ||
1905 | while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR | |
1043771b | 1906 | || CONVERT_EXPR_P (exp.value)) |
f2a71bbc JM |
1907 | && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) |
1908 | { | |
1909 | if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) | |
1910 | not_lvalue = true; | |
1911 | exp.value = TREE_OPERAND (exp.value, 0); | |
1912 | } | |
1913 | ||
1914 | if (TREE_NO_WARNING (orig_exp)) | |
1915 | TREE_NO_WARNING (exp.value) = 1; | |
1916 | ||
1917 | lvalue_array_p = !not_lvalue && lvalue_p (exp.value); | |
1918 | if (!flag_isoc99 && !lvalue_array_p) | |
1919 | { | |
1920 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1921 | Normally, using such an array would be invalid; but it can | |
1922 | be used correctly inside sizeof or as a statement expression. | |
1923 | Thus, do not give an error here; an error will result later. */ | |
1924 | return exp; | |
1925 | } | |
1926 | ||
c2255bc4 | 1927 | exp.value = array_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1928 | } |
1929 | break; | |
1930 | case FUNCTION_TYPE: | |
c2255bc4 | 1931 | exp.value = function_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1932 | break; |
1933 | default: | |
f2a71bbc | 1934 | break; |
207bf485 | 1935 | } |
f2a71bbc | 1936 | |
207bf485 JM |
1937 | return exp; |
1938 | } | |
1939 | ||
ebfbbdc5 JJ |
1940 | struct c_expr |
1941 | default_function_array_read_conversion (location_t loc, struct c_expr exp) | |
1942 | { | |
1943 | mark_exp_read (exp.value); | |
1944 | return default_function_array_conversion (loc, exp); | |
1945 | } | |
522ddfa2 | 1946 | |
267bac10 JM |
1947 | /* Return whether EXPR should be treated as an atomic lvalue for the |
1948 | purposes of load and store handling. */ | |
1949 | ||
1950 | static bool | |
1951 | really_atomic_lvalue (tree expr) | |
1952 | { | |
1953 | if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node) | |
1954 | return false; | |
1955 | if (!TYPE_ATOMIC (TREE_TYPE (expr))) | |
1956 | return false; | |
1957 | if (!lvalue_p (expr)) | |
1958 | return false; | |
1959 | ||
1960 | /* Ignore _Atomic on register variables, since their addresses can't | |
1961 | be taken so (a) atomicity is irrelevant and (b) the normal atomic | |
1962 | sequences wouldn't work. Ignore _Atomic on structures containing | |
1963 | bit-fields, since accessing elements of atomic structures or | |
1964 | unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if | |
1965 | it's undefined at translation time or execution time, and the | |
1966 | normal atomic sequences again wouldn't work. */ | |
1967 | while (handled_component_p (expr)) | |
1968 | { | |
1969 | if (TREE_CODE (expr) == COMPONENT_REF | |
1970 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
1971 | return false; | |
1972 | expr = TREE_OPERAND (expr, 0); | |
1973 | } | |
1974 | if (DECL_P (expr) && C_DECL_REGISTER (expr)) | |
1975 | return false; | |
1976 | return true; | |
1977 | } | |
1978 | ||
1979 | /* Convert expression EXP (location LOC) from lvalue to rvalue, | |
1980 | including converting functions and arrays to pointers if CONVERT_P. | |
1981 | If READ_P, also mark the expression as having been read. */ | |
1982 | ||
1983 | struct c_expr | |
1984 | convert_lvalue_to_rvalue (location_t loc, struct c_expr exp, | |
1985 | bool convert_p, bool read_p) | |
1986 | { | |
1987 | if (read_p) | |
1988 | mark_exp_read (exp.value); | |
1989 | if (convert_p) | |
1990 | exp = default_function_array_conversion (loc, exp); | |
1991 | if (really_atomic_lvalue (exp.value)) | |
1992 | { | |
1993 | vec<tree, va_gc> *params; | |
1994 | tree nonatomic_type, tmp, tmp_addr, fndecl, func_call; | |
1995 | tree expr_type = TREE_TYPE (exp.value); | |
1996 | tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0); | |
1997 | tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); | |
1998 | ||
1999 | gcc_assert (TYPE_ATOMIC (expr_type)); | |
2000 | ||
2001 | /* Expansion of a generic atomic load may require an addition | |
2002 | element, so allocate enough to prevent a resize. */ | |
2003 | vec_alloc (params, 4); | |
2004 | ||
2005 | /* Remove the qualifiers for the rest of the expressions and | |
2006 | create the VAL temp variable to hold the RHS. */ | |
2007 | nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED); | |
2008 | tmp = create_tmp_var (nonatomic_type, NULL); | |
2009 | tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0); | |
2010 | TREE_ADDRESSABLE (tmp) = 1; | |
cc28fc7f | 2011 | TREE_NO_WARNING (tmp) = 1; |
267bac10 JM |
2012 | |
2013 | /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */ | |
2014 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); | |
2015 | params->quick_push (expr_addr); | |
2016 | params->quick_push (tmp_addr); | |
2017 | params->quick_push (seq_cst); | |
8edbfaa6 | 2018 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 | 2019 | |
cc28fc7f MP |
2020 | /* EXPR is always read. */ |
2021 | mark_exp_read (exp.value); | |
2022 | ||
267bac10 JM |
2023 | /* Return tmp which contains the value loaded. */ |
2024 | exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp); | |
2025 | } | |
2026 | return exp; | |
2027 | } | |
2028 | ||
522ddfa2 JM |
2029 | /* EXP is an expression of integer type. Apply the integer promotions |
2030 | to it and return the promoted value. */ | |
400fbf9f JW |
2031 | |
2032 | tree | |
522ddfa2 | 2033 | perform_integral_promotions (tree exp) |
400fbf9f | 2034 | { |
b3694847 SS |
2035 | tree type = TREE_TYPE (exp); |
2036 | enum tree_code code = TREE_CODE (type); | |
400fbf9f | 2037 | |
522ddfa2 | 2038 | gcc_assert (INTEGRAL_TYPE_P (type)); |
157689c6 | 2039 | |
400fbf9f JW |
2040 | /* Normally convert enums to int, |
2041 | but convert wide enums to something wider. */ | |
2042 | if (code == ENUMERAL_TYPE) | |
2043 | { | |
b0c48229 NB |
2044 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
2045 | TYPE_PRECISION (integer_type_node)), | |
2046 | ((TYPE_PRECISION (type) | |
2047 | >= TYPE_PRECISION (integer_type_node)) | |
8df83eae | 2048 | && TYPE_UNSIGNED (type))); |
05bccae2 | 2049 | |
400fbf9f JW |
2050 | return convert (type, exp); |
2051 | } | |
2052 | ||
522ddfa2 JM |
2053 | /* ??? This should no longer be needed now bit-fields have their |
2054 | proper types. */ | |
9753f113 | 2055 | if (TREE_CODE (exp) == COMPONENT_REF |
05bccae2 | 2056 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) |
cff9c407 | 2057 | /* If it's thinner than an int, promote it like a |
d72040f5 | 2058 | c_promoting_integer_type_p, otherwise leave it alone. */ |
05bccae2 RK |
2059 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), |
2060 | TYPE_PRECISION (integer_type_node))) | |
f458d1d5 | 2061 | return convert (integer_type_node, exp); |
9753f113 | 2062 | |
d72040f5 | 2063 | if (c_promoting_integer_type_p (type)) |
400fbf9f | 2064 | { |
f458d1d5 | 2065 | /* Preserve unsignedness if not really getting any wider. */ |
8df83eae | 2066 | if (TYPE_UNSIGNED (type) |
f458d1d5 | 2067 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
400fbf9f | 2068 | return convert (unsigned_type_node, exp); |
05bccae2 | 2069 | |
400fbf9f JW |
2070 | return convert (integer_type_node, exp); |
2071 | } | |
05bccae2 | 2072 | |
522ddfa2 JM |
2073 | return exp; |
2074 | } | |
2075 | ||
2076 | ||
2077 | /* Perform default promotions for C data used in expressions. | |
46bdb9cf | 2078 | Enumeral types or short or char are converted to int. |
522ddfa2 JM |
2079 | In addition, manifest constants symbols are replaced by their values. */ |
2080 | ||
2081 | tree | |
2082 | default_conversion (tree exp) | |
2083 | { | |
2084 | tree orig_exp; | |
2085 | tree type = TREE_TYPE (exp); | |
2086 | enum tree_code code = TREE_CODE (type); | |
40449a90 | 2087 | tree promoted_type; |
522ddfa2 | 2088 | |
ebfbbdc5 JJ |
2089 | mark_exp_read (exp); |
2090 | ||
46bdb9cf JM |
2091 | /* Functions and arrays have been converted during parsing. */ |
2092 | gcc_assert (code != FUNCTION_TYPE); | |
2093 | if (code == ARRAY_TYPE) | |
2094 | return exp; | |
522ddfa2 JM |
2095 | |
2096 | /* Constants can be used directly unless they're not loadable. */ | |
2097 | if (TREE_CODE (exp) == CONST_DECL) | |
2098 | exp = DECL_INITIAL (exp); | |
2099 | ||
522ddfa2 JM |
2100 | /* Strip no-op conversions. */ |
2101 | orig_exp = exp; | |
2102 | STRIP_TYPE_NOPS (exp); | |
2103 | ||
2104 | if (TREE_NO_WARNING (orig_exp)) | |
2105 | TREE_NO_WARNING (exp) = 1; | |
2106 | ||
400fbf9f JW |
2107 | if (code == VOID_TYPE) |
2108 | { | |
5436fa2e MP |
2109 | error_at (EXPR_LOC_OR_LOC (exp, input_location), |
2110 | "void value not ignored as it ought to be"); | |
400fbf9f JW |
2111 | return error_mark_node; |
2112 | } | |
808d6eaa JM |
2113 | |
2114 | exp = require_complete_type (exp); | |
2115 | if (exp == error_mark_node) | |
2116 | return error_mark_node; | |
2117 | ||
40449a90 SL |
2118 | promoted_type = targetm.promoted_type (type); |
2119 | if (promoted_type) | |
2120 | return convert (promoted_type, exp); | |
2121 | ||
808d6eaa JM |
2122 | if (INTEGRAL_TYPE_P (type)) |
2123 | return perform_integral_promotions (exp); | |
2124 | ||
400fbf9f JW |
2125 | return exp; |
2126 | } | |
2127 | \f | |
0fb96aa4 | 2128 | /* Look up COMPONENT in a structure or union TYPE. |
e9b2c823 NB |
2129 | |
2130 | If the component name is not found, returns NULL_TREE. Otherwise, | |
2131 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
2132 | stepping down the chain to the component, which is in the last | |
2133 | TREE_VALUE of the list. Normally the list is of length one, but if | |
2134 | the component is embedded within (nested) anonymous structures or | |
2135 | unions, the list steps down the chain to the component. */ | |
2f6e4e97 | 2136 | |
2f2d13da | 2137 | static tree |
0fb96aa4 | 2138 | lookup_field (tree type, tree component) |
2f2d13da DE |
2139 | { |
2140 | tree field; | |
2141 | ||
2142 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
2143 | to the field elements. Use a binary search on this array to quickly | |
2144 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
2145 | will always be set for structures which have many elements. */ | |
2146 | ||
22a0b85f | 2147 | if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) |
2f2d13da DE |
2148 | { |
2149 | int bot, top, half; | |
d07605f5 | 2150 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; |
2f2d13da DE |
2151 | |
2152 | field = TYPE_FIELDS (type); | |
2153 | bot = 0; | |
d07605f5 | 2154 | top = TYPE_LANG_SPECIFIC (type)->s->len; |
2f2d13da DE |
2155 | while (top - bot > 1) |
2156 | { | |
2f2d13da DE |
2157 | half = (top - bot + 1) >> 1; |
2158 | field = field_array[bot+half]; | |
2159 | ||
2160 | if (DECL_NAME (field) == NULL_TREE) | |
2161 | { | |
2162 | /* Step through all anon unions in linear fashion. */ | |
2163 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
2164 | { | |
2f2d13da | 2165 | field = field_array[bot++]; |
a68b98cf RK |
2166 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
2167 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
19d76e60 | 2168 | { |
0fb96aa4 | 2169 | tree anon = lookup_field (TREE_TYPE (field), component); |
e9b2c823 NB |
2170 | |
2171 | if (anon) | |
2172 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2173 | |
2174 | /* The Plan 9 compiler permits referring | |
2175 | directly to an anonymous struct/union field | |
2176 | using a typedef name. */ | |
2177 | if (flag_plan9_extensions | |
2178 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2179 | && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) | |
2180 | == TYPE_DECL) | |
2181 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2182 | == component)) | |
2183 | break; | |
2f6e4e97 | 2184 | } |
2f2d13da DE |
2185 | } |
2186 | ||
2187 | /* Entire record is only anon unions. */ | |
2188 | if (bot > top) | |
2189 | return NULL_TREE; | |
2190 | ||
2191 | /* Restart the binary search, with new lower bound. */ | |
2192 | continue; | |
2193 | } | |
2194 | ||
e8b87aac | 2195 | if (DECL_NAME (field) == component) |
2f2d13da | 2196 | break; |
e8b87aac | 2197 | if (DECL_NAME (field) < component) |
2f2d13da DE |
2198 | bot += half; |
2199 | else | |
2200 | top = bot + half; | |
2201 | } | |
2202 | ||
2203 | if (DECL_NAME (field_array[bot]) == component) | |
2204 | field = field_array[bot]; | |
2205 | else if (DECL_NAME (field) != component) | |
e9b2c823 | 2206 | return NULL_TREE; |
2f2d13da DE |
2207 | } |
2208 | else | |
2209 | { | |
910ad8de | 2210 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
2f2d13da | 2211 | { |
e9b2c823 NB |
2212 | if (DECL_NAME (field) == NULL_TREE |
2213 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
2214 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
2f2d13da | 2215 | { |
0fb96aa4 | 2216 | tree anon = lookup_field (TREE_TYPE (field), component); |
a68b98cf | 2217 | |
e9b2c823 NB |
2218 | if (anon) |
2219 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2220 | |
2221 | /* The Plan 9 compiler permits referring directly to an | |
2222 | anonymous struct/union field using a typedef | |
2223 | name. */ | |
2224 | if (flag_plan9_extensions | |
2225 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2226 | && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL | |
2227 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2228 | == component)) | |
2229 | break; | |
2f2d13da DE |
2230 | } |
2231 | ||
2232 | if (DECL_NAME (field) == component) | |
2233 | break; | |
2234 | } | |
e9b2c823 NB |
2235 | |
2236 | if (field == NULL_TREE) | |
2237 | return NULL_TREE; | |
2f2d13da DE |
2238 | } |
2239 | ||
e9b2c823 | 2240 | return tree_cons (NULL_TREE, field, NULL_TREE); |
2f2d13da DE |
2241 | } |
2242 | ||
c2255bc4 AH |
2243 | /* Make an expression to refer to the COMPONENT field of structure or |
2244 | union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the | |
2245 | location of the COMPONENT_REF. */ | |
400fbf9f JW |
2246 | |
2247 | tree | |
c2255bc4 | 2248 | build_component_ref (location_t loc, tree datum, tree component) |
400fbf9f | 2249 | { |
b3694847 SS |
2250 | tree type = TREE_TYPE (datum); |
2251 | enum tree_code code = TREE_CODE (type); | |
2252 | tree field = NULL; | |
2253 | tree ref; | |
1e57bf47 | 2254 | bool datum_lvalue = lvalue_p (datum); |
400fbf9f | 2255 | |
7a3ea201 RH |
2256 | if (!objc_is_public (datum, component)) |
2257 | return error_mark_node; | |
2258 | ||
46a88c12 | 2259 | /* Detect Objective-C property syntax object.property. */ |
668ea4b1 | 2260 | if (c_dialect_objc () |
46a88c12 | 2261 | && (ref = objc_maybe_build_component_ref (datum, component))) |
668ea4b1 IS |
2262 | return ref; |
2263 | ||
400fbf9f JW |
2264 | /* See if there is a field or component with name COMPONENT. */ |
2265 | ||
2266 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
2267 | { | |
d0f062fb | 2268 | if (!COMPLETE_TYPE_P (type)) |
400fbf9f | 2269 | { |
7a228918 | 2270 | c_incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
2271 | return error_mark_node; |
2272 | } | |
2273 | ||
0fb96aa4 | 2274 | field = lookup_field (type, component); |
400fbf9f JW |
2275 | |
2276 | if (!field) | |
2277 | { | |
c2255bc4 | 2278 | error_at (loc, "%qT has no member named %qE", type, component); |
400fbf9f JW |
2279 | return error_mark_node; |
2280 | } | |
400fbf9f | 2281 | |
e9b2c823 NB |
2282 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. |
2283 | This might be better solved in future the way the C++ front | |
2284 | end does it - by giving the anonymous entities each a | |
2285 | separate name and type, and then have build_component_ref | |
2286 | recursively call itself. We can't do that here. */ | |
46ea50cb | 2287 | do |
19d76e60 | 2288 | { |
e9b2c823 | 2289 | tree subdatum = TREE_VALUE (field); |
efed193e JM |
2290 | int quals; |
2291 | tree subtype; | |
1e57bf47 | 2292 | bool use_datum_quals; |
e9b2c823 NB |
2293 | |
2294 | if (TREE_TYPE (subdatum) == error_mark_node) | |
2295 | return error_mark_node; | |
2296 | ||
1e57bf47 JM |
2297 | /* If this is an rvalue, it does not have qualifiers in C |
2298 | standard terms and we must avoid propagating such | |
2299 | qualifiers down to a non-lvalue array that is then | |
2300 | converted to a pointer. */ | |
2301 | use_datum_quals = (datum_lvalue | |
2302 | || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); | |
2303 | ||
efed193e | 2304 | quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); |
1e57bf47 JM |
2305 | if (use_datum_quals) |
2306 | quals |= TYPE_QUALS (TREE_TYPE (datum)); | |
efed193e JM |
2307 | subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); |
2308 | ||
2309 | ref = build3 (COMPONENT_REF, subtype, datum, subdatum, | |
53fb4de3 | 2310 | NULL_TREE); |
c2255bc4 | 2311 | SET_EXPR_LOCATION (ref, loc); |
1e57bf47 JM |
2312 | if (TREE_READONLY (subdatum) |
2313 | || (use_datum_quals && TREE_READONLY (datum))) | |
19d76e60 | 2314 | TREE_READONLY (ref) = 1; |
1e57bf47 JM |
2315 | if (TREE_THIS_VOLATILE (subdatum) |
2316 | || (use_datum_quals && TREE_THIS_VOLATILE (datum))) | |
19d76e60 | 2317 | TREE_THIS_VOLATILE (ref) = 1; |
e23bd218 IR |
2318 | |
2319 | if (TREE_DEPRECATED (subdatum)) | |
9b86d6bb | 2320 | warn_deprecated_use (subdatum, NULL_TREE); |
e23bd218 | 2321 | |
19d76e60 | 2322 | datum = ref; |
46ea50cb RS |
2323 | |
2324 | field = TREE_CHAIN (field); | |
19d76e60 | 2325 | } |
46ea50cb | 2326 | while (field); |
19d76e60 | 2327 | |
400fbf9f JW |
2328 | return ref; |
2329 | } | |
2330 | else if (code != ERROR_MARK) | |
c2255bc4 AH |
2331 | error_at (loc, |
2332 | "request for member %qE in something not a structure or union", | |
2333 | component); | |
400fbf9f JW |
2334 | |
2335 | return error_mark_node; | |
2336 | } | |
2337 | \f | |
2338 | /* Given an expression PTR for a pointer, return an expression | |
2339 | for the value pointed to. | |
6a3799eb AH |
2340 | ERRORSTRING is the name of the operator to appear in error messages. |
2341 | ||
2342 | LOC is the location to use for the generated tree. */ | |
400fbf9f JW |
2343 | |
2344 | tree | |
dd865ef6 | 2345 | build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) |
400fbf9f | 2346 | { |
b3694847 SS |
2347 | tree pointer = default_conversion (ptr); |
2348 | tree type = TREE_TYPE (pointer); | |
6a3799eb | 2349 | tree ref; |
400fbf9f JW |
2350 | |
2351 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b | 2352 | { |
1043771b | 2353 | if (CONVERT_EXPR_P (pointer) |
79bedddc SR |
2354 | || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) |
2355 | { | |
2356 | /* If a warning is issued, mark it to avoid duplicates from | |
2357 | the backend. This only needs to be done at | |
2358 | warn_strict_aliasing > 2. */ | |
2359 | if (warn_strict_aliasing > 2) | |
2360 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), | |
2361 | type, TREE_OPERAND (pointer, 0))) | |
2362 | TREE_NO_WARNING (pointer) = 1; | |
2363 | } | |
2364 | ||
870cc33b | 2365 | if (TREE_CODE (pointer) == ADDR_EXPR |
870cc33b RS |
2366 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) |
2367 | == TREE_TYPE (type))) | |
6a3799eb AH |
2368 | { |
2369 | ref = TREE_OPERAND (pointer, 0); | |
2370 | protected_set_expr_location (ref, loc); | |
2371 | return ref; | |
2372 | } | |
870cc33b RS |
2373 | else |
2374 | { | |
2375 | tree t = TREE_TYPE (type); | |
46df2823 | 2376 | |
984dfd8c | 2377 | ref = build1 (INDIRECT_REF, t, pointer); |
400fbf9f | 2378 | |
baae9b65 | 2379 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) |
870cc33b | 2380 | { |
c9f9eb5d | 2381 | error_at (loc, "dereferencing pointer to incomplete type"); |
870cc33b RS |
2382 | return error_mark_node; |
2383 | } | |
7d882b83 | 2384 | if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) |
c9f9eb5d | 2385 | warning_at (loc, 0, "dereferencing %<void *%> pointer"); |
870cc33b RS |
2386 | |
2387 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
2388 | so that we get the proper error message if the result is used | |
2389 | to assign to. Also, &* is supposed to be a no-op. | |
2390 | And ANSI C seems to specify that the type of the result | |
2391 | should be the const type. */ | |
2392 | /* A de-reference of a pointer to const is not a const. It is valid | |
2393 | to change it via some other pointer. */ | |
2394 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
2395 | TREE_SIDE_EFFECTS (ref) | |
271bd540 | 2396 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); |
493692cd | 2397 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
6a3799eb | 2398 | protected_set_expr_location (ref, loc); |
870cc33b RS |
2399 | return ref; |
2400 | } | |
2401 | } | |
400fbf9f | 2402 | else if (TREE_CODE (pointer) != ERROR_MARK) |
7a6daeb0 NF |
2403 | invalid_indirection_error (loc, type, errstring); |
2404 | ||
400fbf9f JW |
2405 | return error_mark_node; |
2406 | } | |
2407 | ||
2408 | /* This handles expressions of the form "a[i]", which denotes | |
2409 | an array reference. | |
2410 | ||
2411 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2412 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2413 | This avoids forcing the array out of registers, and can work on | |
2414 | arrays that are not lvalues (for example, members of structures returned | |
6a3799eb AH |
2415 | by functions). |
2416 | ||
30cd1c5d AS |
2417 | For vector types, allow vector[i] but not i[vector], and create |
2418 | *(((type*)&vectortype) + i) for the expression. | |
2419 | ||
6a3799eb | 2420 | LOC is the location to use for the returned expression. */ |
400fbf9f JW |
2421 | |
2422 | tree | |
c2255bc4 | 2423 | build_array_ref (location_t loc, tree array, tree index) |
400fbf9f | 2424 | { |
6a3799eb | 2425 | tree ret; |
a4ab7973 | 2426 | bool swapped = false; |
400fbf9f JW |
2427 | if (TREE_TYPE (array) == error_mark_node |
2428 | || TREE_TYPE (index) == error_mark_node) | |
2429 | return error_mark_node; | |
2430 | ||
b72271b9 | 2431 | if (flag_cilkplus && contains_array_notation_expr (index)) |
36536d79 BI |
2432 | { |
2433 | size_t rank = 0; | |
2434 | if (!find_rank (loc, index, index, true, &rank)) | |
2435 | return error_mark_node; | |
2436 | if (rank > 1) | |
2437 | { | |
2438 | error_at (loc, "rank of the array's index is greater than 1"); | |
2439 | return error_mark_node; | |
2440 | } | |
2441 | } | |
a4ab7973 | 2442 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE |
30cd1c5d AS |
2443 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE |
2444 | /* Allow vector[index] but not index[vector]. */ | |
2445 | && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE) | |
400fbf9f | 2446 | { |
a4ab7973 JM |
2447 | tree temp; |
2448 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE | |
2449 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
fdeefd49 | 2450 | { |
f90e8e2e | 2451 | error_at (loc, |
30cd1c5d AS |
2452 | "subscripted value is neither array nor pointer nor vector"); |
2453 | ||
fdeefd49 RS |
2454 | return error_mark_node; |
2455 | } | |
a4ab7973 JM |
2456 | temp = array; |
2457 | array = index; | |
2458 | index = temp; | |
2459 | swapped = true; | |
2460 | } | |
2461 | ||
2462 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2463 | { | |
a63068b6 | 2464 | error_at (loc, "array subscript is not an integer"); |
a4ab7973 JM |
2465 | return error_mark_node; |
2466 | } | |
2467 | ||
2468 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2469 | { | |
a63068b6 | 2470 | error_at (loc, "subscripted value is pointer to function"); |
a4ab7973 JM |
2471 | return error_mark_node; |
2472 | } | |
2473 | ||
ff6b6641 GDR |
2474 | /* ??? Existing practice has been to warn only when the char |
2475 | index is syntactically the index, not for char[array]. */ | |
2476 | if (!swapped) | |
2477 | warn_array_subscript_with_type_char (index); | |
a4ab7973 JM |
2478 | |
2479 | /* Apply default promotions *after* noticing character types. */ | |
2480 | index = default_conversion (index); | |
2481 | ||
2482 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
f90e8e2e | 2483 | |
7edaa4d2 | 2484 | convert_vector_to_pointer_for_subscript (loc, &array, index); |
a4ab7973 JM |
2485 | |
2486 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2487 | { | |
e4d35515 | 2488 | tree rval, type; |
fdeefd49 | 2489 | |
400fbf9f JW |
2490 | /* An array that is indexed by a non-constant |
2491 | cannot be stored in a register; we must be able to do | |
2492 | address arithmetic on its address. | |
2493 | Likewise an array of elements of variable size. */ | |
2494 | if (TREE_CODE (index) != INTEGER_CST | |
d0f062fb | 2495 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
400fbf9f JW |
2496 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
2497 | { | |
dffd7eb6 | 2498 | if (!c_mark_addressable (array)) |
400fbf9f JW |
2499 | return error_mark_node; |
2500 | } | |
e6d52559 JW |
2501 | /* An array that is indexed by a constant value which is not within |
2502 | the array bounds cannot be stored in a register either; because we | |
2503 | would get a crash in store_bit_field/extract_bit_field when trying | |
2504 | to access a non-existent part of the register. */ | |
2505 | if (TREE_CODE (index) == INTEGER_CST | |
eb34af89 | 2506 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3f75a254 | 2507 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) |
e6d52559 | 2508 | { |
dffd7eb6 | 2509 | if (!c_mark_addressable (array)) |
e6d52559 JW |
2510 | return error_mark_node; |
2511 | } | |
400fbf9f | 2512 | |
400fbf9f JW |
2513 | if (pedantic) |
2514 | { | |
2515 | tree foo = array; | |
2516 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2517 | foo = TREE_OPERAND (foo, 0); | |
5baeaac0 | 2518 | if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) |
c1771a20 | 2519 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 2520 | "ISO C forbids subscripting %<register%> array"); |
3f75a254 | 2521 | else if (!flag_isoc99 && !lvalue_p (foo)) |
c1771a20 | 2522 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 2523 | "ISO C90 forbids subscripting non-lvalue array"); |
400fbf9f JW |
2524 | } |
2525 | ||
46df2823 | 2526 | type = TREE_TYPE (TREE_TYPE (array)); |
53fb4de3 | 2527 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
400fbf9f | 2528 | /* Array ref is const/volatile if the array elements are |
c22cacf3 | 2529 | or if the array is. */ |
400fbf9f JW |
2530 | TREE_READONLY (rval) |
2531 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2532 | | TREE_READONLY (array)); | |
2533 | TREE_SIDE_EFFECTS (rval) | |
2534 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2535 | | TREE_SIDE_EFFECTS (array)); | |
2536 | TREE_THIS_VOLATILE (rval) | |
2537 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2538 | /* This was added by rms on 16 Nov 91. | |
2f6e4e97 | 2539 | It fixes vol struct foo *a; a->elts[1] |
400fbf9f JW |
2540 | in an inline function. |
2541 | Hope it doesn't break something else. */ | |
2542 | | TREE_THIS_VOLATILE (array)); | |
928c19bb | 2543 | ret = require_complete_type (rval); |
6a3799eb AH |
2544 | protected_set_expr_location (ret, loc); |
2545 | return ret; | |
400fbf9f | 2546 | } |
a4ab7973 JM |
2547 | else |
2548 | { | |
2549 | tree ar = default_conversion (array); | |
400fbf9f | 2550 | |
a4ab7973 JM |
2551 | if (ar == error_mark_node) |
2552 | return ar; | |
400fbf9f | 2553 | |
a4ab7973 JM |
2554 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); |
2555 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
400fbf9f | 2556 | |
ba47d38d | 2557 | return build_indirect_ref |
c9f9eb5d | 2558 | (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0), |
dd865ef6 | 2559 | RO_ARRAY_INDEXING); |
a4ab7973 | 2560 | } |
400fbf9f JW |
2561 | } |
2562 | \f | |
7e585d16 | 2563 | /* Build an external reference to identifier ID. FUN indicates |
766beb40 | 2564 | whether this will be used for a function call. LOC is the source |
6866c6e8 ILT |
2565 | location of the identifier. This sets *TYPE to the type of the |
2566 | identifier, which is not the same as the type of the returned value | |
2567 | for CONST_DECLs defined as enum constants. If the type of the | |
2568 | identifier is not available, *TYPE is set to NULL. */ | |
7e585d16 | 2569 | tree |
c2255bc4 | 2570 | build_external_ref (location_t loc, tree id, int fun, tree *type) |
7e585d16 ZW |
2571 | { |
2572 | tree ref; | |
2573 | tree decl = lookup_name (id); | |
16b34ad6 ZL |
2574 | |
2575 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2576 | whatever lookup_name() found. */ | |
2577 | decl = objc_lookup_ivar (decl, id); | |
7e585d16 | 2578 | |
6866c6e8 | 2579 | *type = NULL; |
339a28b9 | 2580 | if (decl && decl != error_mark_node) |
6866c6e8 ILT |
2581 | { |
2582 | ref = decl; | |
2583 | *type = TREE_TYPE (ref); | |
2584 | } | |
339a28b9 ZW |
2585 | else if (fun) |
2586 | /* Implicit function declaration. */ | |
c2255bc4 | 2587 | ref = implicitly_declare (loc, id); |
339a28b9 ZW |
2588 | else if (decl == error_mark_node) |
2589 | /* Don't complain about something that's already been | |
2590 | complained about. */ | |
2591 | return error_mark_node; | |
2592 | else | |
2593 | { | |
c2255bc4 | 2594 | undeclared_variable (loc, id); |
339a28b9 ZW |
2595 | return error_mark_node; |
2596 | } | |
7e585d16 ZW |
2597 | |
2598 | if (TREE_TYPE (ref) == error_mark_node) | |
2599 | return error_mark_node; | |
2600 | ||
339a28b9 | 2601 | if (TREE_DEPRECATED (ref)) |
9b86d6bb | 2602 | warn_deprecated_use (ref, NULL_TREE); |
339a28b9 | 2603 | |
ad960f56 | 2604 | /* Recursive call does not count as usage. */ |
b8698a0f | 2605 | if (ref != current_function_decl) |
ad960f56 | 2606 | { |
ad960f56 MLI |
2607 | TREE_USED (ref) = 1; |
2608 | } | |
7e585d16 | 2609 | |
bc4b653b JM |
2610 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) |
2611 | { | |
2612 | if (!in_sizeof && !in_typeof) | |
2613 | C_DECL_USED (ref) = 1; | |
2614 | else if (DECL_INITIAL (ref) == 0 | |
2615 | && DECL_EXTERNAL (ref) | |
2616 | && !TREE_PUBLIC (ref)) | |
2617 | record_maybe_used_decl (ref); | |
2618 | } | |
2619 | ||
7e585d16 ZW |
2620 | if (TREE_CODE (ref) == CONST_DECL) |
2621 | { | |
6193b8b7 | 2622 | used_types_insert (TREE_TYPE (ref)); |
24b97832 ILT |
2623 | |
2624 | if (warn_cxx_compat | |
2625 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2626 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2627 | { | |
2628 | warning_at (loc, OPT_Wc___compat, | |
2629 | ("enum constant defined in struct or union " | |
2630 | "is not visible in C++")); | |
2631 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2632 | } | |
2633 | ||
7e585d16 ZW |
2634 | ref = DECL_INITIAL (ref); |
2635 | TREE_CONSTANT (ref) = 1; | |
2636 | } | |
6a29edea | 2637 | else if (current_function_decl != 0 |
4b1e44be | 2638 | && !DECL_FILE_SCOPE_P (current_function_decl) |
6a29edea EB |
2639 | && (TREE_CODE (ref) == VAR_DECL |
2640 | || TREE_CODE (ref) == PARM_DECL | |
2641 | || TREE_CODE (ref) == FUNCTION_DECL)) | |
2642 | { | |
2643 | tree context = decl_function_context (ref); | |
2f6e4e97 | 2644 | |
6a29edea EB |
2645 | if (context != 0 && context != current_function_decl) |
2646 | DECL_NONLOCAL (ref) = 1; | |
2647 | } | |
71113fcd GK |
2648 | /* C99 6.7.4p3: An inline definition of a function with external |
2649 | linkage ... shall not contain a reference to an identifier with | |
2650 | internal linkage. */ | |
2651 | else if (current_function_decl != 0 | |
2652 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2653 | && DECL_EXTERNAL (current_function_decl) | |
2654 | && VAR_OR_FUNCTION_DECL_P (ref) | |
2655 | && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref)) | |
1033ffa8 JJ |
2656 | && ! TREE_PUBLIC (ref) |
2657 | && DECL_CONTEXT (ref) != current_function_decl) | |
991d6621 JM |
2658 | record_inline_static (loc, current_function_decl, ref, |
2659 | csi_internal); | |
7e585d16 ZW |
2660 | |
2661 | return ref; | |
2662 | } | |
2663 | ||
bc4b653b JM |
2664 | /* Record details of decls possibly used inside sizeof or typeof. */ |
2665 | struct maybe_used_decl | |
2666 | { | |
2667 | /* The decl. */ | |
2668 | tree decl; | |
2669 | /* The level seen at (in_sizeof + in_typeof). */ | |
2670 | int level; | |
2671 | /* The next one at this level or above, or NULL. */ | |
2672 | struct maybe_used_decl *next; | |
2673 | }; | |
2674 | ||
2675 | static struct maybe_used_decl *maybe_used_decls; | |
2676 | ||
2677 | /* Record that DECL, an undefined static function reference seen | |
2678 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2679 | a VLA type or the operand of typeof is a variably modified | |
2680 | type. */ | |
2681 | ||
4e2fb7de | 2682 | static void |
bc4b653b JM |
2683 | record_maybe_used_decl (tree decl) |
2684 | { | |
2685 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2686 | t->decl = decl; | |
2687 | t->level = in_sizeof + in_typeof; | |
2688 | t->next = maybe_used_decls; | |
2689 | maybe_used_decls = t; | |
2690 | } | |
2691 | ||
2692 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2693 | USED is false, just discard them. If it is true, mark them used | |
2694 | (if no longer inside sizeof or typeof) or move them to the next | |
2695 | level up (if still inside sizeof or typeof). */ | |
2696 | ||
2697 | void | |
2698 | pop_maybe_used (bool used) | |
2699 | { | |
2700 | struct maybe_used_decl *p = maybe_used_decls; | |
2701 | int cur_level = in_sizeof + in_typeof; | |
2702 | while (p && p->level > cur_level) | |
2703 | { | |
2704 | if (used) | |
2705 | { | |
2706 | if (cur_level == 0) | |
2707 | C_DECL_USED (p->decl) = 1; | |
2708 | else | |
2709 | p->level = cur_level; | |
2710 | } | |
2711 | p = p->next; | |
2712 | } | |
2713 | if (!used || cur_level == 0) | |
2714 | maybe_used_decls = p; | |
2715 | } | |
2716 | ||
2717 | /* Return the result of sizeof applied to EXPR. */ | |
2718 | ||
2719 | struct c_expr | |
c2255bc4 | 2720 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) |
bc4b653b JM |
2721 | { |
2722 | struct c_expr ret; | |
ad97f4be JM |
2723 | if (expr.value == error_mark_node) |
2724 | { | |
2725 | ret.value = error_mark_node; | |
2726 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 2727 | ret.original_type = NULL; |
ad97f4be JM |
2728 | pop_maybe_used (false); |
2729 | } | |
2730 | else | |
2731 | { | |
928c19bb | 2732 | bool expr_const_operands = true; |
773ec47f MP |
2733 | |
2734 | if (TREE_CODE (expr.value) == PARM_DECL | |
2735 | && C_ARRAY_PARAMETER (expr.value)) | |
2736 | { | |
2737 | if (warning_at (loc, OPT_Wsizeof_array_argument, | |
2738 | "%<sizeof%> on array function parameter %qE will " | |
2739 | "return size of %qT", expr.value, | |
2740 | expr.original_type)) | |
2741 | inform (DECL_SOURCE_LOCATION (expr.value), "declared here"); | |
2742 | } | |
928c19bb JM |
2743 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, |
2744 | &expr_const_operands); | |
c2255bc4 | 2745 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); |
1a4049e7 JJ |
2746 | c_last_sizeof_arg = expr.value; |
2747 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2748 | ret.original_type = NULL; |
928c19bb | 2749 | if (c_vla_type_p (TREE_TYPE (folded_expr))) |
52ffd86e MS |
2750 | { |
2751 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
928c19bb JM |
2752 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2753 | folded_expr, ret.value); | |
2754 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
c2255bc4 | 2755 | SET_EXPR_LOCATION (ret.value, loc); |
52ffd86e | 2756 | } |
928c19bb | 2757 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); |
ad97f4be | 2758 | } |
bc4b653b JM |
2759 | return ret; |
2760 | } | |
2761 | ||
2762 | /* Return the result of sizeof applied to T, a structure for the type | |
c2255bc4 AH |
2763 | name passed to sizeof (rather than the type itself). LOC is the |
2764 | location of the original expression. */ | |
bc4b653b JM |
2765 | |
2766 | struct c_expr | |
c2255bc4 | 2767 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) |
bc4b653b JM |
2768 | { |
2769 | tree type; | |
2770 | struct c_expr ret; | |
928c19bb JM |
2771 | tree type_expr = NULL_TREE; |
2772 | bool type_expr_const = true; | |
2773 | type = groktypename (t, &type_expr, &type_expr_const); | |
c2255bc4 | 2774 | ret.value = c_sizeof (loc, type); |
1a4049e7 JJ |
2775 | c_last_sizeof_arg = type; |
2776 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2777 | ret.original_type = NULL; |
24070fcb JM |
2778 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) |
2779 | && c_vla_type_p (type)) | |
928c19bb | 2780 | { |
24070fcb JM |
2781 | /* If the type is a [*] array, it is a VLA but is represented as |
2782 | having a size of zero. In such a case we must ensure that | |
2783 | the result of sizeof does not get folded to a constant by | |
2784 | c_fully_fold, because if the size is evaluated the result is | |
2785 | not constant and so constraints on zero or negative size | |
2786 | arrays must not be applied when this sizeof call is inside | |
2787 | another array declarator. */ | |
2788 | if (!type_expr) | |
2789 | type_expr = integer_zero_node; | |
928c19bb JM |
2790 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2791 | type_expr, ret.value); | |
2792 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2793 | } | |
16464cc1 VR |
2794 | pop_maybe_used (type != error_mark_node |
2795 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
bc4b653b JM |
2796 | return ret; |
2797 | } | |
2798 | ||
400fbf9f | 2799 | /* Build a function call to function FUNCTION with parameters PARAMS. |
c2255bc4 | 2800 | The function call is at LOC. |
400fbf9f JW |
2801 | PARAMS is a list--a chain of TREE_LIST nodes--in which the |
2802 | TREE_VALUE of each node is a parameter-expression. | |
2803 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2804 | ||
2805 | tree | |
c2255bc4 | 2806 | build_function_call (location_t loc, tree function, tree params) |
bbbbb16a | 2807 | { |
9771b263 | 2808 | vec<tree, va_gc> *v; |
bbbbb16a ILT |
2809 | tree ret; |
2810 | ||
9771b263 | 2811 | vec_alloc (v, list_length (params)); |
bbbbb16a | 2812 | for (; params; params = TREE_CHAIN (params)) |
9771b263 | 2813 | v->quick_push (TREE_VALUE (params)); |
8edbfaa6 | 2814 | ret = c_build_function_call_vec (loc, vNULL, function, v, NULL); |
9771b263 | 2815 | vec_free (v); |
bbbbb16a ILT |
2816 | return ret; |
2817 | } | |
2818 | ||
ae52741c MLI |
2819 | /* Give a note about the location of the declaration of DECL. */ |
2820 | ||
2821 | static void inform_declaration (tree decl) | |
2822 | { | |
2823 | if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl))) | |
2824 | inform (DECL_SOURCE_LOCATION (decl), "declared here"); | |
2825 | } | |
2826 | ||
bbbbb16a ILT |
2827 | /* Build a function call to function FUNCTION with parameters PARAMS. |
2828 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2829 | either NULL or the original type of the corresponding element in | |
2830 | PARAMS. The original type may differ from TREE_TYPE of the | |
2831 | parameter for enums. FUNCTION's data type may be a function type | |
2832 | or pointer-to-function. This function changes the elements of | |
2833 | PARAMS. */ | |
2834 | ||
2835 | tree | |
81e5eca8 MP |
2836 | build_function_call_vec (location_t loc, vec<location_t> arg_loc, |
2837 | tree function, vec<tree, va_gc> *params, | |
9771b263 | 2838 | vec<tree, va_gc> *origtypes) |
400fbf9f | 2839 | { |
b3694847 | 2840 | tree fntype, fundecl = 0; |
4977bab6 | 2841 | tree name = NULL_TREE, result; |
c96f4f73 | 2842 | tree tem; |
94a0dd7b SL |
2843 | int nargs; |
2844 | tree *argarray; | |
b8698a0f | 2845 | |
400fbf9f | 2846 | |
fc76e425 | 2847 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 2848 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
2849 | |
2850 | /* Convert anything with function type to a pointer-to-function. */ | |
2851 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2852 | { | |
2853 | name = DECL_NAME (function); | |
0a35513e AH |
2854 | |
2855 | if (flag_tm) | |
2856 | tm_malloc_replacement (function); | |
a5eadacc | 2857 | fundecl = function; |
86951993 AM |
2858 | /* Atomic functions have type checking/casting already done. They are |
2859 | often rewritten and don't match the original parameter list. */ | |
2860 | if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) | |
2861 | origtypes = NULL; | |
36536d79 | 2862 | |
b72271b9 | 2863 | if (flag_cilkplus |
36536d79 BI |
2864 | && is_cilkplus_reduce_builtin (function)) |
2865 | origtypes = NULL; | |
400fbf9f | 2866 | } |
f2a71bbc | 2867 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) |
c2255bc4 | 2868 | function = function_to_pointer_conversion (loc, function); |
400fbf9f | 2869 | |
6e955430 ZL |
2870 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2871 | expressions, like those used for ObjC messenger dispatches. */ | |
9771b263 DN |
2872 | if (params && !params->is_empty ()) |
2873 | function = objc_rewrite_function_call (function, (*params)[0]); | |
6e955430 | 2874 | |
928c19bb JM |
2875 | function = c_fully_fold (function, false, NULL); |
2876 | ||
400fbf9f JW |
2877 | fntype = TREE_TYPE (function); |
2878 | ||
2879 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2880 | return error_mark_node; | |
2881 | ||
2882 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2883 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2884 | { | |
ae52741c MLI |
2885 | if (!flag_diagnostics_show_caret) |
2886 | error_at (loc, | |
2887 | "called object %qE is not a function or function pointer", | |
2888 | function); | |
2889 | else if (DECL_P (function)) | |
2890 | { | |
2891 | error_at (loc, | |
2892 | "called object %qD is not a function or function pointer", | |
2893 | function); | |
2894 | inform_declaration (function); | |
2895 | } | |
2896 | else | |
2897 | error_at (loc, | |
2898 | "called object is not a function or function pointer"); | |
400fbf9f JW |
2899 | return error_mark_node; |
2900 | } | |
2901 | ||
5ce89b2e JM |
2902 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
2903 | current_function_returns_abnormally = 1; | |
2904 | ||
400fbf9f JW |
2905 | /* fntype now gets the type of function pointed to. */ |
2906 | fntype = TREE_TYPE (fntype); | |
2907 | ||
ab4194da JM |
2908 | /* Convert the parameters to the types declared in the |
2909 | function prototype, or apply default promotions. */ | |
2910 | ||
81e5eca8 MP |
2911 | nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params, |
2912 | origtypes, function, fundecl); | |
ab4194da JM |
2913 | if (nargs < 0) |
2914 | return error_mark_node; | |
2915 | ||
c96f4f73 | 2916 | /* Check that the function is called through a compatible prototype. |
fa337f3a | 2917 | If it is not, warn. */ |
1043771b | 2918 | if (CONVERT_EXPR_P (function) |
c96f4f73 EB |
2919 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR |
2920 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
3f75a254 | 2921 | && !comptypes (fntype, TREE_TYPE (tem))) |
c96f4f73 EB |
2922 | { |
2923 | tree return_type = TREE_TYPE (fntype); | |
c96f4f73 EB |
2924 | |
2925 | /* This situation leads to run-time undefined behavior. We can't, | |
2926 | therefore, simply error unless we can prove that all possible | |
2927 | executions of the program must execute the code. */ | |
fa337f3a | 2928 | warning_at (loc, 0, "function called through a non-compatible type"); |
c96f4f73 | 2929 | |
fa337f3a RB |
2930 | if (VOID_TYPE_P (return_type) |
2931 | && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
2932 | pedwarn (loc, 0, | |
2933 | "function with qualified void return type called"); | |
2934 | } | |
c96f4f73 | 2935 | |
9771b263 | 2936 | argarray = vec_safe_address (params); |
bbbbb16a | 2937 | |
83322951 RG |
2938 | /* Check that arguments to builtin functions match the expectations. */ |
2939 | if (fundecl | |
2940 | && DECL_BUILT_IN (fundecl) | |
2941 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2942 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2943 | return error_mark_node; | |
400fbf9f | 2944 | |
83322951 | 2945 | /* Check that the arguments to the function are valid. */ |
dde05067 | 2946 | check_function_arguments (fntype, nargs, argarray); |
400fbf9f | 2947 | |
928c19bb JM |
2948 | if (name != NULL_TREE |
2949 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
bf730f15 | 2950 | { |
928c19bb | 2951 | if (require_constant_value) |
b8698a0f | 2952 | result = |
db3927fb AH |
2953 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), |
2954 | function, nargs, argarray); | |
928c19bb | 2955 | else |
db3927fb AH |
2956 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), |
2957 | function, nargs, argarray); | |
928c19bb JM |
2958 | if (TREE_CODE (result) == NOP_EXPR |
2959 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2960 | STRIP_TYPE_NOPS (result); | |
bf730f15 RS |
2961 | } |
2962 | else | |
db3927fb AH |
2963 | result = build_call_array_loc (loc, TREE_TYPE (fntype), |
2964 | function, nargs, argarray); | |
b0b3afb2 | 2965 | |
71653180 | 2966 | if (VOID_TYPE_P (TREE_TYPE (result))) |
3ce62965 JM |
2967 | { |
2968 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
db3927fb | 2969 | pedwarn (loc, 0, |
3ce62965 JM |
2970 | "function with qualified void return type called"); |
2971 | return result; | |
2972 | } | |
1eb8759b | 2973 | return require_complete_type (result); |
400fbf9f | 2974 | } |
8edbfaa6 JJ |
2975 | |
2976 | /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */ | |
2977 | ||
2978 | tree | |
2979 | c_build_function_call_vec (location_t loc, vec<location_t> arg_loc, | |
2980 | tree function, vec<tree, va_gc> *params, | |
2981 | vec<tree, va_gc> *origtypes) | |
2982 | { | |
2983 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
2984 | STRIP_TYPE_NOPS (function); | |
2985 | ||
2986 | /* Convert anything with function type to a pointer-to-function. */ | |
2987 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2988 | { | |
2989 | /* Implement type-directed function overloading for builtins. | |
2990 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
2991 | handle all the type checking. The result is a complete expression | |
2992 | that implements this function call. */ | |
2993 | tree tem = resolve_overloaded_builtin (loc, function, params); | |
2994 | if (tem) | |
2995 | return tem; | |
2996 | } | |
2997 | return build_function_call_vec (loc, arg_loc, function, params, origtypes); | |
2998 | } | |
400fbf9f | 2999 | \f |
bbbbb16a ILT |
3000 | /* Convert the argument expressions in the vector VALUES |
3001 | to the types in the list TYPELIST. | |
400fbf9f JW |
3002 | |
3003 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
3004 | perform the default conversions. | |
3005 | ||
bbbbb16a ILT |
3006 | ORIGTYPES is the original types of the expressions in VALUES. This |
3007 | holds the type of enum values which have been converted to integral | |
3008 | types. It may be NULL. | |
400fbf9f | 3009 | |
03dafa61 JM |
3010 | FUNCTION is a tree for the called function. It is used only for |
3011 | error messages, where it is formatted with %qE. | |
400fbf9f JW |
3012 | |
3013 | This is also where warnings about wrong number of args are generated. | |
3014 | ||
81e5eca8 MP |
3015 | ARG_LOC are locations of function arguments (if any). |
3016 | ||
94a0dd7b | 3017 | Returns the actual number of arguments processed (which may be less |
bbbbb16a ILT |
3018 | than the length of VALUES in some error situations), or -1 on |
3019 | failure. */ | |
94a0dd7b SL |
3020 | |
3021 | static int | |
81e5eca8 MP |
3022 | convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist, |
3023 | vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes, | |
3024 | tree function, tree fundecl) | |
400fbf9f | 3025 | { |
bbbbb16a ILT |
3026 | tree typetail, val; |
3027 | unsigned int parmnum; | |
06302a02 | 3028 | bool error_args = false; |
b5d32c25 | 3029 | const bool type_generic = fundecl |
81e5eca8 | 3030 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl))); |
8ce94e44 | 3031 | bool type_generic_remove_excess_precision = false; |
03dafa61 | 3032 | tree selector; |
03dafa61 | 3033 | |
2ac2f164 JM |
3034 | /* Change pointer to function to the function itself for |
3035 | diagnostics. */ | |
03dafa61 JM |
3036 | if (TREE_CODE (function) == ADDR_EXPR |
3037 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
2ac2f164 | 3038 | function = TREE_OPERAND (function, 0); |
03dafa61 JM |
3039 | |
3040 | /* Handle an ObjC selector specially for diagnostics. */ | |
3041 | selector = objc_message_selector (); | |
400fbf9f | 3042 | |
8ce94e44 JM |
3043 | /* For type-generic built-in functions, determine whether excess |
3044 | precision should be removed (classification) or not | |
3045 | (comparison). */ | |
3046 | if (type_generic | |
3047 | && DECL_BUILT_IN (fundecl) | |
3048 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
3049 | { | |
3050 | switch (DECL_FUNCTION_CODE (fundecl)) | |
3051 | { | |
3052 | case BUILT_IN_ISFINITE: | |
3053 | case BUILT_IN_ISINF: | |
3054 | case BUILT_IN_ISINF_SIGN: | |
3055 | case BUILT_IN_ISNAN: | |
3056 | case BUILT_IN_ISNORMAL: | |
3057 | case BUILT_IN_FPCLASSIFY: | |
3058 | type_generic_remove_excess_precision = true; | |
3059 | break; | |
3060 | ||
3061 | default: | |
3062 | type_generic_remove_excess_precision = false; | |
3063 | break; | |
3064 | } | |
3065 | } | |
b72271b9 | 3066 | if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl)) |
6d6efbb3 | 3067 | return vec_safe_length (values); |
8ce94e44 | 3068 | |
400fbf9f | 3069 | /* Scan the given expressions and types, producing individual |
bbbbb16a | 3070 | converted arguments. */ |
400fbf9f | 3071 | |
bbbbb16a | 3072 | for (typetail = typelist, parmnum = 0; |
9771b263 | 3073 | values && values->iterate (parmnum, &val); |
bbbbb16a | 3074 | ++parmnum) |
400fbf9f | 3075 | { |
b3694847 | 3076 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
8ce94e44 | 3077 | tree valtype = TREE_TYPE (val); |
03dafa61 JM |
3078 | tree rname = function; |
3079 | int argnum = parmnum + 1; | |
4d3e6fae | 3080 | const char *invalid_func_diag; |
8ce94e44 | 3081 | bool excess_precision = false; |
928c19bb | 3082 | bool npc; |
bbbbb16a | 3083 | tree parmval; |
5c1bc275 MP |
3084 | /* Some __atomic_* builtins have additional hidden argument at |
3085 | position 0. */ | |
3086 | location_t ploc | |
3087 | = !arg_loc.is_empty () && values->length () == arg_loc.length () | |
3088 | ? expansion_point_location_if_in_system_header (arg_loc[parmnum]) | |
3089 | : input_location; | |
400fbf9f JW |
3090 | |
3091 | if (type == void_type_node) | |
3092 | { | |
19dc6d01 | 3093 | if (selector) |
68fca595 | 3094 | error_at (loc, "too many arguments to method %qE", selector); |
19dc6d01 | 3095 | else |
68fca595 | 3096 | error_at (loc, "too many arguments to function %qE", function); |
ae52741c | 3097 | inform_declaration (fundecl); |
94a0dd7b | 3098 | return parmnum; |
400fbf9f JW |
3099 | } |
3100 | ||
03dafa61 JM |
3101 | if (selector && argnum > 2) |
3102 | { | |
3103 | rname = selector; | |
3104 | argnum -= 2; | |
3105 | } | |
3106 | ||
928c19bb | 3107 | npc = null_pointer_constant_p (val); |
8ce94e44 JM |
3108 | |
3109 | /* If there is excess precision and a prototype, convert once to | |
3110 | the required type rather than converting via the semantic | |
3111 | type. Likewise without a prototype a float value represented | |
3112 | as long double should be converted once to double. But for | |
3113 | type-generic classification functions excess precision must | |
3114 | be removed here. */ | |
3115 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
3116 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
3117 | { | |
3118 | val = TREE_OPERAND (val, 0); | |
3119 | excess_precision = true; | |
3120 | } | |
928c19bb | 3121 | val = c_fully_fold (val, false, NULL); |
ed248cf7 | 3122 | STRIP_TYPE_NOPS (val); |
400fbf9f | 3123 | |
400fbf9f JW |
3124 | val = require_complete_type (val); |
3125 | ||
3126 | if (type != 0) | |
3127 | { | |
3128 | /* Formal parm type is specified by a function prototype. */ | |
400fbf9f | 3129 | |
20913689 | 3130 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) |
400fbf9f | 3131 | { |
5c1bc275 MP |
3132 | error_at (ploc, "type of formal parameter %d is incomplete", |
3133 | parmnum + 1); | |
400fbf9f JW |
3134 | parmval = val; |
3135 | } | |
3136 | else | |
3137 | { | |
bbbbb16a ILT |
3138 | tree origtype; |
3139 | ||
d45cf215 RS |
3140 | /* Optionally warn about conversions that |
3141 | differ from the default conversions. */ | |
05170031 | 3142 | if (warn_traditional_conversion || warn_traditional) |
400fbf9f | 3143 | { |
e3a64162 | 3144 | unsigned int formal_prec = TYPE_PRECISION (type); |
400fbf9f | 3145 | |
aae43c5f | 3146 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3147 | && TREE_CODE (valtype) == REAL_TYPE) |
5c1bc275 MP |
3148 | warning_at (ploc, OPT_Wtraditional_conversion, |
3149 | "passing argument %d of %qE as integer rather " | |
3150 | "than floating due to prototype", | |
3151 | argnum, rname); | |
03829ad2 | 3152 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3153 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
5c1bc275 MP |
3154 | warning_at (ploc, OPT_Wtraditional_conversion, |
3155 | "passing argument %d of %qE as integer rather " | |
3156 | "than complex due to prototype", | |
3157 | argnum, rname); | |
aae43c5f | 3158 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3159 | && TREE_CODE (valtype) == REAL_TYPE) |
5c1bc275 MP |
3160 | warning_at (ploc, OPT_Wtraditional_conversion, |
3161 | "passing argument %d of %qE as complex rather " | |
3162 | "than floating due to prototype", | |
3163 | argnum, rname); | |
400fbf9f | 3164 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3165 | && INTEGRAL_TYPE_P (valtype)) |
5c1bc275 MP |
3166 | warning_at (ploc, OPT_Wtraditional_conversion, |
3167 | "passing argument %d of %qE as floating rather " | |
3168 | "than integer due to prototype", | |
3169 | argnum, rname); | |
03829ad2 | 3170 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3171 | && INTEGRAL_TYPE_P (valtype)) |
5c1bc275 MP |
3172 | warning_at (ploc, OPT_Wtraditional_conversion, |
3173 | "passing argument %d of %qE as complex rather " | |
3174 | "than integer due to prototype", | |
3175 | argnum, rname); | |
aae43c5f | 3176 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3177 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
5c1bc275 MP |
3178 | warning_at (ploc, OPT_Wtraditional_conversion, |
3179 | "passing argument %d of %qE as floating rather " | |
3180 | "than complex due to prototype", | |
3181 | argnum, rname); | |
aae43c5f RK |
3182 | /* ??? At some point, messages should be written about |
3183 | conversions between complex types, but that's too messy | |
3184 | to do now. */ | |
d45cf215 | 3185 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3186 | && TREE_CODE (valtype) == REAL_TYPE) |
d45cf215 RS |
3187 | { |
3188 | /* Warn if any argument is passed as `float', | |
047de90b | 3189 | since without a prototype it would be `double'. */ |
9a8ce21f JG |
3190 | if (formal_prec == TYPE_PRECISION (float_type_node) |
3191 | && type != dfloat32_type_node) | |
5c1bc275 MP |
3192 | warning_at (ploc, 0, |
3193 | "passing argument %d of %qE as %<float%> " | |
3194 | "rather than %<double%> due to prototype", | |
3195 | argnum, rname); | |
9a8ce21f JG |
3196 | |
3197 | /* Warn if mismatch between argument and prototype | |
3198 | for decimal float types. Warn of conversions with | |
3199 | binary float types and of precision narrowing due to | |
3200 | prototype. */ | |
8ce94e44 | 3201 | else if (type != valtype |
9a8ce21f JG |
3202 | && (type == dfloat32_type_node |
3203 | || type == dfloat64_type_node | |
c22cacf3 | 3204 | || type == dfloat128_type_node |
8ce94e44 JM |
3205 | || valtype == dfloat32_type_node |
3206 | || valtype == dfloat64_type_node | |
3207 | || valtype == dfloat128_type_node) | |
c22cacf3 | 3208 | && (formal_prec |
8ce94e44 | 3209 | <= TYPE_PRECISION (valtype) |
9a8ce21f | 3210 | || (type == dfloat128_type_node |
8ce94e44 | 3211 | && (valtype |
c22cacf3 | 3212 | != dfloat64_type_node |
8ce94e44 | 3213 | && (valtype |
9a8ce21f JG |
3214 | != dfloat32_type_node))) |
3215 | || (type == dfloat64_type_node | |
8ce94e44 | 3216 | && (valtype |
9a8ce21f | 3217 | != dfloat32_type_node)))) |
5c1bc275 MP |
3218 | warning_at (ploc, 0, |
3219 | "passing argument %d of %qE as %qT " | |
3220 | "rather than %qT due to prototype", | |
3221 | argnum, rname, type, valtype); | |
9a8ce21f | 3222 | |
d45cf215 | 3223 | } |
3ed56f8a KG |
3224 | /* Detect integer changing in width or signedness. |
3225 | These warnings are only activated with | |
05170031 MLI |
3226 | -Wtraditional-conversion, not with -Wtraditional. */ |
3227 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
8ce94e44 | 3228 | && INTEGRAL_TYPE_P (valtype)) |
400fbf9f | 3229 | { |
d45cf215 RS |
3230 | tree would_have_been = default_conversion (val); |
3231 | tree type1 = TREE_TYPE (would_have_been); | |
3232 | ||
754a4d82 | 3233 | if (TREE_CODE (type) == ENUMERAL_TYPE |
a38b987a | 3234 | && (TYPE_MAIN_VARIANT (type) |
8ce94e44 | 3235 | == TYPE_MAIN_VARIANT (valtype))) |
754a4d82 RS |
3236 | /* No warning if function asks for enum |
3237 | and the actual arg is that enum type. */ | |
3238 | ; | |
3239 | else if (formal_prec != TYPE_PRECISION (type1)) | |
5c1bc275 MP |
3240 | warning_at (ploc, OPT_Wtraditional_conversion, |
3241 | "passing argument %d of %qE " | |
3242 | "with different width due to prototype", | |
3243 | argnum, rname); | |
8df83eae | 3244 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
d45cf215 | 3245 | ; |
800cd3b9 RS |
3246 | /* Don't complain if the formal parameter type |
3247 | is an enum, because we can't tell now whether | |
3248 | the value was an enum--even the same enum. */ | |
3249 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
3250 | ; | |
400fbf9f JW |
3251 | else if (TREE_CODE (val) == INTEGER_CST |
3252 | && int_fits_type_p (val, type)) | |
3253 | /* Change in signedness doesn't matter | |
3254 | if a constant value is unaffected. */ | |
3255 | ; | |
ce9895ae RS |
3256 | /* If the value is extended from a narrower |
3257 | unsigned type, it doesn't matter whether we | |
3258 | pass it as signed or unsigned; the value | |
3259 | certainly is the same either way. */ | |
8ce94e44 JM |
3260 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) |
3261 | && TYPE_UNSIGNED (valtype)) | |
ce9895ae | 3262 | ; |
8df83eae | 3263 | else if (TYPE_UNSIGNED (type)) |
5c1bc275 MP |
3264 | warning_at (ploc, OPT_Wtraditional_conversion, |
3265 | "passing argument %d of %qE " | |
3266 | "as unsigned due to prototype", | |
3267 | argnum, rname); | |
3ed56f8a | 3268 | else |
5c1bc275 MP |
3269 | warning_at (ploc, OPT_Wtraditional_conversion, |
3270 | "passing argument %d of %qE " | |
3271 | "as signed due to prototype", | |
3272 | argnum, rname); | |
400fbf9f JW |
3273 | } |
3274 | } | |
3275 | ||
8ce94e44 JM |
3276 | /* Possibly restore an EXCESS_PRECISION_EXPR for the |
3277 | sake of better warnings from convert_and_check. */ | |
3278 | if (excess_precision) | |
3279 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
9771b263 | 3280 | origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; |
5c1bc275 | 3281 | parmval = convert_for_assignment (loc, ploc, type, |
68fca595 MP |
3282 | val, origtype, ic_argpass, |
3283 | npc, fundecl, function, | |
2ac2f164 | 3284 | parmnum + 1); |
2f6e4e97 | 3285 | |
61f71b34 | 3286 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
b6d6aa84 | 3287 | && INTEGRAL_TYPE_P (type) |
400fbf9f JW |
3288 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
3289 | parmval = default_conversion (parmval); | |
400fbf9f | 3290 | } |
400fbf9f | 3291 | } |
8ce94e44 JM |
3292 | else if (TREE_CODE (valtype) == REAL_TYPE |
3293 | && (TYPE_PRECISION (valtype) | |
2531a1d9 | 3294 | <= TYPE_PRECISION (double_type_node)) |
0fdd4508 JR |
3295 | && TYPE_MAIN_VARIANT (valtype) != double_type_node |
3296 | && TYPE_MAIN_VARIANT (valtype) != long_double_type_node | |
8ce94e44 | 3297 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) |
b5d32c25 KG |
3298 | { |
3299 | if (type_generic) | |
bbbbb16a | 3300 | parmval = val; |
b5d32c25 | 3301 | else |
0a0b3574 MM |
3302 | { |
3303 | /* Convert `float' to `double'. */ | |
3304 | if (warn_double_promotion && !c_inhibit_evaluation_warnings) | |
5c1bc275 MP |
3305 | warning_at (ploc, OPT_Wdouble_promotion, |
3306 | "implicit conversion from %qT to %qT when passing " | |
3307 | "argument to function", | |
3308 | valtype, double_type_node); | |
0a0b3574 MM |
3309 | parmval = convert (double_type_node, val); |
3310 | } | |
b5d32c25 | 3311 | } |
8ce94e44 JM |
3312 | else if (excess_precision && !type_generic) |
3313 | /* A "double" argument with excess precision being passed | |
3314 | without a prototype or in variable arguments. */ | |
bbbbb16a | 3315 | parmval = convert (valtype, val); |
c22cacf3 MS |
3316 | else if ((invalid_func_diag = |
3317 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
4d3e6fae FJ |
3318 | { |
3319 | error (invalid_func_diag); | |
94a0dd7b | 3320 | return -1; |
4d3e6fae | 3321 | } |
400fbf9f JW |
3322 | else |
3323 | /* Convert `short' and `char' to full-size `int'. */ | |
bbbbb16a ILT |
3324 | parmval = default_conversion (val); |
3325 | ||
9771b263 | 3326 | (*values)[parmnum] = parmval; |
06302a02 JJ |
3327 | if (parmval == error_mark_node) |
3328 | error_args = true; | |
400fbf9f JW |
3329 | |
3330 | if (typetail) | |
3331 | typetail = TREE_CHAIN (typetail); | |
3332 | } | |
3333 | ||
9771b263 | 3334 | gcc_assert (parmnum == vec_safe_length (values)); |
94a0dd7b | 3335 | |
400fbf9f | 3336 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) |
3789b316 | 3337 | { |
68fca595 | 3338 | error_at (loc, "too few arguments to function %qE", function); |
6d6efbb3 BI |
3339 | inform_declaration (fundecl); |
3340 | return -1; | |
3789b316 | 3341 | } |
400fbf9f | 3342 | |
06302a02 | 3343 | return error_args ? -1 : (int) parmnum; |
400fbf9f JW |
3344 | } |
3345 | \f | |
43f6dfd3 RS |
3346 | /* This is the entry point used by the parser to build unary operators |
3347 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3348 | ARG is the operand. For unary plus, the C parser currently uses | |
6a3799eb AH |
3349 | CONVERT_EXPR for code. |
3350 | ||
3351 | LOC is the location to use for the tree generated. | |
3352 | */ | |
43f6dfd3 RS |
3353 | |
3354 | struct c_expr | |
c2255bc4 | 3355 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) |
43f6dfd3 RS |
3356 | { |
3357 | struct c_expr result; | |
3358 | ||
c9f9eb5d | 3359 | result.value = build_unary_op (loc, code, arg.value, 0); |
100d537d | 3360 | result.original_code = code; |
6866c6e8 ILT |
3361 | result.original_type = NULL; |
3362 | ||
59c0753d | 3363 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) |
c2255bc4 | 3364 | overflow_warning (loc, result.value); |
59c0753d | 3365 | |
43f6dfd3 RS |
3366 | return result; |
3367 | } | |
3368 | ||
3369 | /* This is the entry point used by the parser to build binary operators | |
3370 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3371 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3372 | expression, we check for operands that were written with other binary | |
ba47d38d AH |
3373 | operators in a way that is likely to confuse the user. |
3374 | ||
3375 | LOCATION is the location of the binary operator. */ | |
edc7c4ec | 3376 | |
487a92fe | 3377 | struct c_expr |
ba47d38d AH |
3378 | parser_build_binary_op (location_t location, enum tree_code code, |
3379 | struct c_expr arg1, struct c_expr arg2) | |
400fbf9f | 3380 | { |
487a92fe | 3381 | struct c_expr result; |
400fbf9f | 3382 | |
487a92fe JM |
3383 | enum tree_code code1 = arg1.original_code; |
3384 | enum tree_code code2 = arg2.original_code; | |
6866c6e8 ILT |
3385 | tree type1 = (arg1.original_type |
3386 | ? arg1.original_type | |
3387 | : TREE_TYPE (arg1.value)); | |
3388 | tree type2 = (arg2.original_type | |
3389 | ? arg2.original_type | |
3390 | : TREE_TYPE (arg2.value)); | |
400fbf9f | 3391 | |
ba47d38d AH |
3392 | result.value = build_binary_op (location, code, |
3393 | arg1.value, arg2.value, 1); | |
487a92fe | 3394 | result.original_code = code; |
6866c6e8 | 3395 | result.original_type = NULL; |
58bf601b | 3396 | |
487a92fe JM |
3397 | if (TREE_CODE (result.value) == ERROR_MARK) |
3398 | return result; | |
400fbf9f | 3399 | |
ba47d38d AH |
3400 | if (location != UNKNOWN_LOCATION) |
3401 | protected_set_expr_location (result.value, location); | |
3402 | ||
400fbf9f | 3403 | /* Check for cases such as x+y<<z which users are likely |
487a92fe | 3404 | to misinterpret. */ |
400fbf9f | 3405 | if (warn_parentheses) |
393e8e8b MP |
3406 | warn_about_parentheses (location, code, code1, arg1.value, code2, |
3407 | arg2.value); | |
001af587 | 3408 | |
ca409efd | 3409 | if (warn_logical_op) |
393e8e8b | 3410 | warn_logical_operator (location, code, TREE_TYPE (result.value), |
ca409efd | 3411 | code1, arg1.value, code2, arg2.value); |
63a08740 | 3412 | |
742938c9 | 3413 | if (warn_logical_not_paren |
47c2554f MP |
3414 | && code1 == TRUTH_NOT_EXPR |
3415 | && code2 != TRUTH_NOT_EXPR) | |
742938c9 MP |
3416 | warn_logical_not_parentheses (location, code, arg1.value, arg2.value); |
3417 | ||
e994a705 RS |
3418 | /* Warn about comparisons against string literals, with the exception |
3419 | of testing for equality or inequality of a string literal with NULL. */ | |
3420 | if (code == EQ_EXPR || code == NE_EXPR) | |
3421 | { | |
3422 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3423 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
c2255bc4 AH |
3424 | warning_at (location, OPT_Waddress, |
3425 | "comparison with string literal results in unspecified behavior"); | |
e994a705 RS |
3426 | } |
3427 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3428 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
c2255bc4 AH |
3429 | warning_at (location, OPT_Waddress, |
3430 | "comparison with string literal results in unspecified behavior"); | |
e994a705 | 3431 | |
b8698a0f L |
3432 | if (TREE_OVERFLOW_P (result.value) |
3433 | && !TREE_OVERFLOW_P (arg1.value) | |
59c0753d | 3434 | && !TREE_OVERFLOW_P (arg2.value)) |
c2255bc4 | 3435 | overflow_warning (location, result.value); |
400fbf9f | 3436 | |
6866c6e8 ILT |
3437 | /* Warn about comparisons of different enum types. */ |
3438 | if (warn_enum_compare | |
3439 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3440 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3441 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3442 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3443 | warning_at (location, OPT_Wenum_compare, | |
3444 | "comparison between %qT and %qT", | |
3445 | type1, type2); | |
3446 | ||
400fbf9f JW |
3447 | return result; |
3448 | } | |
3e4093b6 | 3449 | \f |
3e4093b6 RS |
3450 | /* Return a tree for the difference of pointers OP0 and OP1. |
3451 | The resulting tree has type int. */ | |
293c9fdd | 3452 | |
3e4093b6 | 3453 | static tree |
db3927fb | 3454 | pointer_diff (location_t loc, tree op0, tree op1) |
3e4093b6 | 3455 | { |
3e4093b6 | 3456 | tree restype = ptrdiff_type_node; |
36c5e70a | 3457 | tree result, inttype; |
400fbf9f | 3458 | |
36c5e70a BE |
3459 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); |
3460 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
3e4093b6 | 3461 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
3e4093b6 | 3462 | tree orig_op1 = op1; |
400fbf9f | 3463 | |
36c5e70a BE |
3464 | /* If the operands point into different address spaces, we need to |
3465 | explicitly convert them to pointers into the common address space | |
3466 | before we can subtract the numerical address values. */ | |
3467 | if (as0 != as1) | |
3468 | { | |
3469 | addr_space_t as_common; | |
3470 | tree common_type; | |
3471 | ||
3472 | /* Determine the common superset address space. This is guaranteed | |
3473 | to exist because the caller verified that comp_target_types | |
3474 | returned non-zero. */ | |
3475 | if (!addr_space_superset (as0, as1, &as_common)) | |
3476 | gcc_unreachable (); | |
3477 | ||
3478 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3479 | op0 = convert (common_type, op0); | |
3480 | op1 = convert (common_type, op1); | |
3481 | } | |
3482 | ||
3483 | /* Determine integer type to perform computations in. This will usually | |
3484 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3485 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3486 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
647d4b75 | 3487 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); |
36c5e70a BE |
3488 | else |
3489 | inttype = restype; | |
3490 | ||
fcf73884 | 3491 | if (TREE_CODE (target_type) == VOID_TYPE) |
44d90fe1 | 3492 | pedwarn (loc, OPT_Wpointer_arith, |
fcf73884 MLI |
3493 | "pointer of type %<void *%> used in subtraction"); |
3494 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
44d90fe1 | 3495 | pedwarn (loc, OPT_Wpointer_arith, |
fcf73884 | 3496 | "pointer to a function used in subtraction"); |
400fbf9f | 3497 | |
3e4093b6 RS |
3498 | /* First do the subtraction as integers; |
3499 | then drop through to build the divide operator. | |
3500 | Do not do default conversions on the minus operator | |
3501 | in case restype is a short type. */ | |
400fbf9f | 3502 | |
db3927fb | 3503 | op0 = build_binary_op (loc, |
36c5e70a BE |
3504 | MINUS_EXPR, convert (inttype, op0), |
3505 | convert (inttype, op1), 0); | |
3e4093b6 RS |
3506 | /* This generates an error if op1 is pointer to incomplete type. */ |
3507 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
db3927fb | 3508 | error_at (loc, "arithmetic on pointer to an incomplete type"); |
400fbf9f | 3509 | |
3e4093b6 | 3510 | op1 = c_size_in_bytes (target_type); |
400fbf9f | 3511 | |
f04dda30 MP |
3512 | if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1))) |
3513 | error_at (loc, "arithmetic on pointer to an empty aggregate"); | |
3514 | ||
3e4093b6 | 3515 | /* Divide by the size, in easiest possible way. */ |
36c5e70a BE |
3516 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, |
3517 | op0, convert (inttype, op1)); | |
3518 | ||
3519 | /* Convert to final result type if necessary. */ | |
3520 | return convert (restype, result); | |
3e4093b6 RS |
3521 | } |
3522 | \f | |
267bac10 JM |
3523 | /* Expand atomic compound assignments into an approriate sequence as |
3524 | specified by the C11 standard section 6.5.16.2. | |
3525 | given | |
3526 | _Atomic T1 E1 | |
3527 | T2 E2 | |
3528 | E1 op= E2 | |
3529 | ||
3530 | This sequence is used for all types for which these operations are | |
3531 | supported. | |
3532 | ||
3533 | In addition, built-in versions of the 'fe' prefixed routines may | |
3534 | need to be invoked for floating point (real, complex or vector) when | |
3535 | floating-point exceptions are supported. See 6.5.16.2 footnote 113. | |
3536 | ||
3537 | T1 newval; | |
3538 | T1 old; | |
3539 | T1 *addr | |
3540 | T2 val | |
3541 | fenv_t fenv | |
3542 | ||
3543 | addr = &E1; | |
3544 | val = (E2); | |
3545 | __atomic_load (addr, &old, SEQ_CST); | |
3546 | feholdexcept (&fenv); | |
3547 | loop: | |
3548 | newval = old op val; | |
3549 | if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST, | |
3550 | SEQ_CST)) | |
3551 | goto done; | |
3552 | feclearexcept (FE_ALL_EXCEPT); | |
3553 | goto loop: | |
3554 | done: | |
3555 | feupdateenv (&fenv); | |
3556 | ||
3557 | Also note that the compiler is simply issuing the generic form of | |
3558 | the atomic operations. This requires temp(s) and has their address | |
3559 | taken. The atomic processing is smart enough to figure out when the | |
3560 | size of an object can utilize a lock-free version, and convert the | |
3561 | built-in call to the appropriate lock-free routine. The optimizers | |
3562 | will then dispose of any temps that are no longer required, and | |
3563 | lock-free implementations are utilized as long as there is target | |
3564 | support for the required size. | |
3565 | ||
3566 | If the operator is NOP_EXPR, then this is a simple assignment, and | |
3567 | an __atomic_store is issued to perform the assignment rather than | |
3568 | the above loop. | |
3569 | ||
3570 | */ | |
3571 | ||
3572 | /* Build an atomic assignment at LOC, expanding into the proper | |
3573 | sequence to store LHS MODIFYCODE= RHS. Return a value representing | |
3574 | the result of the operation, unless RETURN_OLD_P in which case | |
3575 | return the old value of LHS (this is only for postincrement and | |
3576 | postdecrement). */ | |
3577 | static tree | |
3578 | build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode, | |
3579 | tree rhs, bool return_old_p) | |
3580 | { | |
3581 | tree fndecl, func_call; | |
3582 | vec<tree, va_gc> *params; | |
3583 | tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr; | |
3584 | tree old, old_addr; | |
3585 | tree compound_stmt; | |
3586 | tree stmt, goto_stmt; | |
3587 | tree loop_label, loop_decl, done_label, done_decl; | |
3588 | ||
3589 | tree lhs_type = TREE_TYPE (lhs); | |
3590 | tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0); | |
3591 | tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); | |
3592 | tree rhs_type = TREE_TYPE (rhs); | |
3593 | ||
3594 | gcc_assert (TYPE_ATOMIC (lhs_type)); | |
3595 | ||
3596 | if (return_old_p) | |
3597 | gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR); | |
3598 | ||
3599 | /* Allocate enough vector items for a compare_exchange. */ | |
3600 | vec_alloc (params, 6); | |
3601 | ||
3602 | /* Create a compound statement to hold the sequence of statements | |
3603 | with a loop. */ | |
3604 | compound_stmt = c_begin_compound_stmt (false); | |
3605 | ||
3606 | /* Fold the RHS if it hasn't already been folded. */ | |
3607 | if (modifycode != NOP_EXPR) | |
3608 | rhs = c_fully_fold (rhs, false, NULL); | |
3609 | ||
3610 | /* Remove the qualifiers for the rest of the expressions and create | |
3611 | the VAL temp variable to hold the RHS. */ | |
3612 | nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED); | |
3613 | nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED); | |
3614 | val = create_tmp_var (nonatomic_rhs_type, NULL); | |
3615 | TREE_ADDRESSABLE (val) = 1; | |
cc28fc7f | 3616 | TREE_NO_WARNING (val) = 1; |
267bac10 JM |
3617 | rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs); |
3618 | SET_EXPR_LOCATION (rhs, loc); | |
3619 | add_stmt (rhs); | |
3620 | ||
3621 | /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue | |
3622 | an atomic_store. */ | |
3623 | if (modifycode == NOP_EXPR) | |
3624 | { | |
3625 | /* Build __atomic_store (&lhs, &val, SEQ_CST) */ | |
3626 | rhs = build_unary_op (loc, ADDR_EXPR, val, 0); | |
3627 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE); | |
3628 | params->quick_push (lhs_addr); | |
3629 | params->quick_push (rhs); | |
3630 | params->quick_push (seq_cst); | |
8edbfaa6 | 3631 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 JM |
3632 | add_stmt (func_call); |
3633 | ||
3634 | /* Finish the compound statement. */ | |
3635 | compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); | |
3636 | ||
3637 | /* VAL is the value which was stored, return a COMPOUND_STMT of | |
3638 | the statement and that value. */ | |
3639 | return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val); | |
3640 | } | |
3641 | ||
3642 | /* Create the variables and labels required for the op= form. */ | |
3643 | old = create_tmp_var (nonatomic_lhs_type, NULL); | |
3644 | old_addr = build_unary_op (loc, ADDR_EXPR, old, 0); | |
cc28fc7f MP |
3645 | TREE_ADDRESSABLE (old) = 1; |
3646 | TREE_NO_WARNING (old) = 1; | |
267bac10 JM |
3647 | |
3648 | newval = create_tmp_var (nonatomic_lhs_type, NULL); | |
3649 | newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0); | |
3650 | TREE_ADDRESSABLE (newval) = 1; | |
3651 | ||
3652 | loop_decl = create_artificial_label (loc); | |
3653 | loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl); | |
3654 | ||
3655 | done_decl = create_artificial_label (loc); | |
3656 | done_label = build1 (LABEL_EXPR, void_type_node, done_decl); | |
3657 | ||
3658 | /* __atomic_load (addr, &old, SEQ_CST). */ | |
3659 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); | |
3660 | params->quick_push (lhs_addr); | |
3661 | params->quick_push (old_addr); | |
3662 | params->quick_push (seq_cst); | |
8edbfaa6 | 3663 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 JM |
3664 | add_stmt (func_call); |
3665 | params->truncate (0); | |
3666 | ||
3667 | /* Create the expressions for floating-point environment | |
3668 | manipulation, if required. */ | |
3669 | bool need_fenv = (flag_trapping_math | |
3670 | && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type))); | |
3671 | tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE; | |
3672 | if (need_fenv) | |
3673 | targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call); | |
3674 | ||
3675 | if (hold_call) | |
3676 | add_stmt (hold_call); | |
3677 | ||
3678 | /* loop: */ | |
3679 | add_stmt (loop_label); | |
3680 | ||
3681 | /* newval = old + val; */ | |
3682 | rhs = build_binary_op (loc, modifycode, old, val, 1); | |
68fca595 MP |
3683 | rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type, |
3684 | rhs, NULL_TREE, ic_assign, false, NULL_TREE, | |
267bac10 JM |
3685 | NULL_TREE, 0); |
3686 | if (rhs != error_mark_node) | |
3687 | { | |
3688 | rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs); | |
3689 | SET_EXPR_LOCATION (rhs, loc); | |
3690 | add_stmt (rhs); | |
3691 | } | |
3692 | ||
3693 | /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST)) | |
3694 | goto done; */ | |
3695 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE); | |
3696 | params->quick_push (lhs_addr); | |
3697 | params->quick_push (old_addr); | |
3698 | params->quick_push (newval_addr); | |
3699 | params->quick_push (integer_zero_node); | |
3700 | params->quick_push (seq_cst); | |
3701 | params->quick_push (seq_cst); | |
8edbfaa6 | 3702 | func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL); |
267bac10 JM |
3703 | |
3704 | goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl); | |
3705 | SET_EXPR_LOCATION (goto_stmt, loc); | |
3706 | ||
3707 | stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE); | |
3708 | SET_EXPR_LOCATION (stmt, loc); | |
3709 | add_stmt (stmt); | |
3710 | ||
3711 | if (clear_call) | |
3712 | add_stmt (clear_call); | |
3713 | ||
3714 | /* goto loop; */ | |
3715 | goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl); | |
3716 | SET_EXPR_LOCATION (goto_stmt, loc); | |
3717 | add_stmt (goto_stmt); | |
3718 | ||
3719 | /* done: */ | |
3720 | add_stmt (done_label); | |
3721 | ||
3722 | if (update_call) | |
3723 | add_stmt (update_call); | |
3724 | ||
3725 | /* Finish the compound statement. */ | |
3726 | compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); | |
3727 | ||
3728 | /* NEWVAL is the value that was successfully stored, return a | |
3729 | COMPOUND_EXPR of the statement and the appropriate value. */ | |
3730 | return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, | |
3731 | return_old_p ? old : newval); | |
3732 | } | |
3733 | ||
3e4093b6 RS |
3734 | /* Construct and perhaps optimize a tree representation |
3735 | for a unary operation. CODE, a tree_code, specifies the operation | |
3736 | and XARG is the operand. | |
3737 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3738 | the default promotions (such as from short to int). | |
3739 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3740 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
c9f9eb5d AH |
3741 | arrays to pointers in C99. |
3742 | ||
3743 | LOCATION is the location of the operator. */ | |
400fbf9f | 3744 | |
3e4093b6 | 3745 | tree |
c9f9eb5d AH |
3746 | build_unary_op (location_t location, |
3747 | enum tree_code code, tree xarg, int flag) | |
3e4093b6 RS |
3748 | { |
3749 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3750 | tree arg = xarg; | |
3751 | tree argtype = 0; | |
808d6eaa | 3752 | enum tree_code typecode; |
3e4093b6 | 3753 | tree val; |
c9f9eb5d | 3754 | tree ret = error_mark_node; |
8ce94e44 | 3755 | tree eptype = NULL_TREE; |
3e4093b6 | 3756 | int noconvert = flag; |
4de67c26 | 3757 | const char *invalid_op_diag; |
928c19bb JM |
3758 | bool int_operands; |
3759 | ||
3760 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
4d84fe7c JM |
3761 | if (int_operands) |
3762 | arg = remove_c_maybe_const_expr (arg); | |
400fbf9f | 3763 | |
808d6eaa JM |
3764 | if (code != ADDR_EXPR) |
3765 | arg = require_complete_type (arg); | |
3766 | ||
3767 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
3e4093b6 RS |
3768 | if (typecode == ERROR_MARK) |
3769 | return error_mark_node; | |
3770 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3771 | typecode = INTEGER_TYPE; | |
6c36d76b | 3772 | |
4de67c26 JM |
3773 | if ((invalid_op_diag |
3774 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3775 | { | |
c9f9eb5d | 3776 | error_at (location, invalid_op_diag); |
4de67c26 JM |
3777 | return error_mark_node; |
3778 | } | |
3779 | ||
8ce94e44 JM |
3780 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
3781 | { | |
3782 | eptype = TREE_TYPE (arg); | |
3783 | arg = TREE_OPERAND (arg, 0); | |
3784 | } | |
3785 | ||
3e4093b6 RS |
3786 | switch (code) |
3787 | { | |
3788 | case CONVERT_EXPR: | |
3789 | /* This is used for unary plus, because a CONVERT_EXPR | |
3790 | is enough to prevent anybody from looking inside for | |
3791 | associativity, but won't generate any code. */ | |
3792 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3793 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
8a2cee38 | 3794 | || typecode == VECTOR_TYPE)) |
400fbf9f | 3795 | { |
c9f9eb5d | 3796 | error_at (location, "wrong type argument to unary plus"); |
3e4093b6 | 3797 | return error_mark_node; |
400fbf9f | 3798 | } |
3e4093b6 RS |
3799 | else if (!noconvert) |
3800 | arg = default_conversion (arg); | |
db3927fb | 3801 | arg = non_lvalue_loc (location, arg); |
400fbf9f JW |
3802 | break; |
3803 | ||
3e4093b6 RS |
3804 | case NEGATE_EXPR: |
3805 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3806 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
3e4093b6 RS |
3807 | || typecode == VECTOR_TYPE)) |
3808 | { | |
c9f9eb5d | 3809 | error_at (location, "wrong type argument to unary minus"); |
3e4093b6 RS |
3810 | return error_mark_node; |
3811 | } | |
3812 | else if (!noconvert) | |
3813 | arg = default_conversion (arg); | |
400fbf9f JW |
3814 | break; |
3815 | ||
3e4093b6 | 3816 | case BIT_NOT_EXPR: |
462643f0 AP |
3817 | /* ~ works on integer types and non float vectors. */ |
3818 | if (typecode == INTEGER_TYPE | |
3819 | || (typecode == VECTOR_TYPE | |
3820 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
03d5b1f5 | 3821 | { |
3e4093b6 RS |
3822 | if (!noconvert) |
3823 | arg = default_conversion (arg); | |
03d5b1f5 | 3824 | } |
3e4093b6 | 3825 | else if (typecode == COMPLEX_TYPE) |
400fbf9f | 3826 | { |
3e4093b6 | 3827 | code = CONJ_EXPR; |
c1771a20 | 3828 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 3829 | "ISO C does not support %<~%> for complex conjugation"); |
3e4093b6 RS |
3830 | if (!noconvert) |
3831 | arg = default_conversion (arg); | |
3832 | } | |
3833 | else | |
3834 | { | |
c9f9eb5d | 3835 | error_at (location, "wrong type argument to bit-complement"); |
3e4093b6 | 3836 | return error_mark_node; |
400fbf9f JW |
3837 | } |
3838 | break; | |
3839 | ||
3e4093b6 | 3840 | case ABS_EXPR: |
11017cc7 | 3841 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
400fbf9f | 3842 | { |
c9f9eb5d | 3843 | error_at (location, "wrong type argument to abs"); |
3e4093b6 | 3844 | return error_mark_node; |
400fbf9f | 3845 | } |
3e4093b6 RS |
3846 | else if (!noconvert) |
3847 | arg = default_conversion (arg); | |
400fbf9f JW |
3848 | break; |
3849 | ||
3e4093b6 RS |
3850 | case CONJ_EXPR: |
3851 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3852 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3853 | || typecode == COMPLEX_TYPE)) | |
400fbf9f | 3854 | { |
c9f9eb5d | 3855 | error_at (location, "wrong type argument to conjugation"); |
3e4093b6 | 3856 | return error_mark_node; |
400fbf9f | 3857 | } |
3e4093b6 RS |
3858 | else if (!noconvert) |
3859 | arg = default_conversion (arg); | |
400fbf9f JW |
3860 | break; |
3861 | ||
3e4093b6 | 3862 | case TRUTH_NOT_EXPR: |
ab22c1fa | 3863 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3864 | && typecode != REAL_TYPE && typecode != POINTER_TYPE |
46bdb9cf | 3865 | && typecode != COMPLEX_TYPE) |
400fbf9f | 3866 | { |
c9f9eb5d AH |
3867 | error_at (location, |
3868 | "wrong type argument to unary exclamation mark"); | |
3e4093b6 | 3869 | return error_mark_node; |
400fbf9f | 3870 | } |
a27d595d JM |
3871 | if (int_operands) |
3872 | { | |
3873 | arg = c_objc_common_truthvalue_conversion (location, xarg); | |
3874 | arg = remove_c_maybe_const_expr (arg); | |
3875 | } | |
3876 | else | |
3877 | arg = c_objc_common_truthvalue_conversion (location, arg); | |
db3927fb | 3878 | ret = invert_truthvalue_loc (location, arg); |
ca80e52b EB |
3879 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ |
3880 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3881 | location = EXPR_LOCATION (ret); | |
c9f9eb5d | 3882 | goto return_build_unary_op; |
3e4093b6 | 3883 | |
3e4093b6 | 3884 | case REALPART_EXPR: |
3e4093b6 | 3885 | case IMAGPART_EXPR: |
fb52b50a NF |
3886 | ret = build_real_imag_expr (location, code, arg); |
3887 | if (ret == error_mark_node) | |
3888 | return error_mark_node; | |
8ce94e44 JM |
3889 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3890 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3891 | goto return_build_unary_op; |
3e4093b6 RS |
3892 | |
3893 | case PREINCREMENT_EXPR: | |
3894 | case POSTINCREMENT_EXPR: | |
3895 | case PREDECREMENT_EXPR: | |
3896 | case POSTDECREMENT_EXPR: | |
3e4093b6 | 3897 | |
928c19bb JM |
3898 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) |
3899 | { | |
3900 | tree inner = build_unary_op (location, code, | |
3901 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3902 | if (inner == error_mark_node) | |
3903 | return error_mark_node; | |
3904 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3905 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3906 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3907 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3908 | goto return_build_unary_op; | |
3909 | } | |
3910 | ||
925e8657 NP |
3911 | /* Complain about anything that is not a true lvalue. In |
3912 | Objective-C, skip this check for property_refs. */ | |
f90e8e2e | 3913 | if (!objc_is_property_ref (arg) |
7bd11157 TT |
3914 | && !lvalue_or_else (location, |
3915 | arg, ((code == PREINCREMENT_EXPR | |
925e8657 NP |
3916 | || code == POSTINCREMENT_EXPR) |
3917 | ? lv_increment | |
3918 | : lv_decrement))) | |
928c19bb JM |
3919 | return error_mark_node; |
3920 | ||
09639a83 ILT |
3921 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) |
3922 | { | |
3923 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3924 | warning_at (location, OPT_Wc___compat, | |
3925 | "increment of enumeration value is invalid in C++"); | |
3926 | else | |
3927 | warning_at (location, OPT_Wc___compat, | |
3928 | "decrement of enumeration value is invalid in C++"); | |
3929 | } | |
3930 | ||
928c19bb JM |
3931 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ |
3932 | arg = c_fully_fold (arg, false, NULL); | |
3933 | ||
267bac10 JM |
3934 | bool atomic_op; |
3935 | atomic_op = really_atomic_lvalue (arg); | |
3936 | ||
3e4093b6 RS |
3937 | /* Increment or decrement the real part of the value, |
3938 | and don't change the imaginary part. */ | |
3939 | if (typecode == COMPLEX_TYPE) | |
400fbf9f | 3940 | { |
3e4093b6 RS |
3941 | tree real, imag; |
3942 | ||
c1771a20 | 3943 | pedwarn (location, OPT_Wpedantic, |
509c9d60 | 3944 | "ISO C does not support %<++%> and %<--%> on complex types"); |
3e4093b6 | 3945 | |
267bac10 JM |
3946 | if (!atomic_op) |
3947 | { | |
3948 | arg = stabilize_reference (arg); | |
3949 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); | |
3950 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
3951 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
3952 | if (real == error_mark_node || imag == error_mark_node) | |
3953 | return error_mark_node; | |
3954 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), | |
3955 | real, imag); | |
3956 | goto return_build_unary_op; | |
3957 | } | |
400fbf9f | 3958 | } |
3e4093b6 RS |
3959 | |
3960 | /* Report invalid types. */ | |
3961 | ||
ab22c1fa | 3962 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE |
267bac10 | 3963 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE |
241b71bb | 3964 | && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE) |
400fbf9f | 3965 | { |
3e4093b6 | 3966 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c9f9eb5d | 3967 | error_at (location, "wrong type argument to increment"); |
c22cacf3 | 3968 | else |
c9f9eb5d | 3969 | error_at (location, "wrong type argument to decrement"); |
3e4093b6 RS |
3970 | |
3971 | return error_mark_node; | |
400fbf9f | 3972 | } |
400fbf9f | 3973 | |
3e4093b6 RS |
3974 | { |
3975 | tree inc; | |
400fbf9f | 3976 | |
3e4093b6 RS |
3977 | argtype = TREE_TYPE (arg); |
3978 | ||
3979 | /* Compute the increment. */ | |
3980 | ||
3981 | if (typecode == POINTER_TYPE) | |
3982 | { | |
a0e24419 | 3983 | /* If pointer target is an incomplete type, |
3e4093b6 | 3984 | we just cannot know how to do the arithmetic. */ |
b70cef5d | 3985 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) |
3e4093b6 RS |
3986 | { |
3987 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
c9f9eb5d | 3988 | error_at (location, |
a0e24419 MP |
3989 | "increment of pointer to an incomplete type %qT", |
3990 | TREE_TYPE (argtype)); | |
3e4093b6 | 3991 | else |
c9f9eb5d | 3992 | error_at (location, |
a0e24419 MP |
3993 | "decrement of pointer to an incomplete type %qT", |
3994 | TREE_TYPE (argtype)); | |
3e4093b6 | 3995 | } |
b70cef5d JJ |
3996 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE |
3997 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
c22cacf3 | 3998 | { |
3e4093b6 | 3999 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
44d90fe1 | 4000 | pedwarn (location, OPT_Wpointer_arith, |
fcf73884 | 4001 | "wrong type argument to increment"); |
3e4093b6 | 4002 | else |
44d90fe1 | 4003 | pedwarn (location, OPT_Wpointer_arith, |
fcf73884 | 4004 | "wrong type argument to decrement"); |
3e4093b6 RS |
4005 | } |
4006 | ||
b70cef5d | 4007 | inc = c_size_in_bytes (TREE_TYPE (argtype)); |
0d82a1c8 | 4008 | inc = convert_to_ptrofftype_loc (location, inc); |
3e4093b6 | 4009 | } |
b70cef5d | 4010 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) |
ab22c1fa CF |
4011 | { |
4012 | /* For signed fract types, we invert ++ to -- or | |
4013 | -- to ++, and change inc from 1 to -1, because | |
4014 | it is not possible to represent 1 in signed fract constants. | |
4015 | For unsigned fract types, the result always overflows and | |
4016 | we get an undefined (original) or the maximum value. */ | |
4017 | if (code == PREINCREMENT_EXPR) | |
4018 | code = PREDECREMENT_EXPR; | |
4019 | else if (code == PREDECREMENT_EXPR) | |
4020 | code = PREINCREMENT_EXPR; | |
4021 | else if (code == POSTINCREMENT_EXPR) | |
4022 | code = POSTDECREMENT_EXPR; | |
4023 | else /* code == POSTDECREMENT_EXPR */ | |
4024 | code = POSTINCREMENT_EXPR; | |
4025 | ||
4026 | inc = integer_minus_one_node; | |
4027 | inc = convert (argtype, inc); | |
4028 | } | |
3e4093b6 | 4029 | else |
5be014d5 | 4030 | { |
241b71bb TV |
4031 | inc = VECTOR_TYPE_P (argtype) |
4032 | ? build_one_cst (argtype) | |
4033 | : integer_one_node; | |
5be014d5 AP |
4034 | inc = convert (argtype, inc); |
4035 | } | |
3e4093b6 | 4036 | |
925e8657 NP |
4037 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
4038 | need to ask Objective-C to build the increment or decrement | |
4039 | expression for it. */ | |
4040 | if (objc_is_property_ref (arg)) | |
f90e8e2e | 4041 | return objc_build_incr_expr_for_property_ref (location, code, |
925e8657 NP |
4042 | arg, inc); |
4043 | ||
3e4093b6 | 4044 | /* Report a read-only lvalue. */ |
f37acdf9 | 4045 | if (TYPE_READONLY (argtype)) |
953ff289 | 4046 | { |
c02065fc | 4047 | readonly_error (location, arg, |
953ff289 DN |
4048 | ((code == PREINCREMENT_EXPR |
4049 | || code == POSTINCREMENT_EXPR) | |
4050 | ? lv_increment : lv_decrement)); | |
4051 | return error_mark_node; | |
4052 | } | |
f37acdf9 JM |
4053 | else if (TREE_READONLY (arg)) |
4054 | readonly_warning (arg, | |
4055 | ((code == PREINCREMENT_EXPR | |
4056 | || code == POSTINCREMENT_EXPR) | |
4057 | ? lv_increment : lv_decrement)); | |
3e4093b6 | 4058 | |
267bac10 JM |
4059 | /* If the argument is atomic, use the special code sequences for |
4060 | atomic compound assignment. */ | |
4061 | if (atomic_op) | |
4062 | { | |
4063 | arg = stabilize_reference (arg); | |
4064 | ret = build_atomic_assign (location, arg, | |
4065 | ((code == PREINCREMENT_EXPR | |
4066 | || code == POSTINCREMENT_EXPR) | |
4067 | ? PLUS_EXPR | |
4068 | : MINUS_EXPR), | |
4069 | (FRACT_MODE_P (TYPE_MODE (argtype)) | |
4070 | ? inc | |
4071 | : integer_one_node), | |
4072 | (code == POSTINCREMENT_EXPR | |
4073 | || code == POSTDECREMENT_EXPR)); | |
4074 | goto return_build_unary_op; | |
4075 | } | |
4076 | ||
3e4093b6 RS |
4077 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) |
4078 | val = boolean_increment (code, arg); | |
4079 | else | |
53fb4de3 | 4080 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
3e4093b6 | 4081 | TREE_SIDE_EFFECTS (val) = 1; |
3e4093b6 | 4082 | if (TREE_CODE (val) != code) |
6de9cd9a | 4083 | TREE_NO_WARNING (val) = 1; |
c9f9eb5d AH |
4084 | ret = val; |
4085 | goto return_build_unary_op; | |
3e4093b6 RS |
4086 | } |
4087 | ||
4088 | case ADDR_EXPR: | |
4089 | /* Note that this operation never does default_conversion. */ | |
4090 | ||
2b4b7036 JM |
4091 | /* The operand of unary '&' must be an lvalue (which excludes |
4092 | expressions of type void), or, in C99, the result of a [] or | |
4093 | unary '*' operator. */ | |
4094 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
4095 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
4096 | && (TREE_CODE (arg) != INDIRECT_REF | |
4097 | || !flag_isoc99)) | |
4098 | pedwarn (location, 0, "taking address of expression of type %<void%>"); | |
4099 | ||
3e4093b6 RS |
4100 | /* Let &* cancel out to simplify resulting code. */ |
4101 | if (TREE_CODE (arg) == INDIRECT_REF) | |
400fbf9f | 4102 | { |
3e4093b6 RS |
4103 | /* Don't let this be an lvalue. */ |
4104 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
db3927fb | 4105 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); |
c9f9eb5d AH |
4106 | ret = TREE_OPERAND (arg, 0); |
4107 | goto return_build_unary_op; | |
400fbf9f | 4108 | } |
1eb8759b | 4109 | |
7c672dfc | 4110 | /* For &x[y], return x+y */ |
3e4093b6 | 4111 | if (TREE_CODE (arg) == ARRAY_REF) |
1eb8759b | 4112 | { |
f2a71bbc JM |
4113 | tree op0 = TREE_OPERAND (arg, 0); |
4114 | if (!c_mark_addressable (op0)) | |
3e4093b6 | 4115 | return error_mark_node; |
1eb8759b | 4116 | } |
1eb8759b | 4117 | |
3e4093b6 RS |
4118 | /* Anything not already handled and not a true memory reference |
4119 | or a non-lvalue array is an error. */ | |
4120 | else if (typecode != FUNCTION_TYPE && !flag | |
7bd11157 | 4121 | && !lvalue_or_else (location, arg, lv_addressof)) |
3e4093b6 | 4122 | return error_mark_node; |
b6a10c9f | 4123 | |
928c19bb JM |
4124 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify |
4125 | folding later. */ | |
4126 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
4127 | { | |
4128 | tree inner = build_unary_op (location, code, | |
4129 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
4130 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4131 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
4132 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
4133 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
4134 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
4135 | goto return_build_unary_op; | |
4136 | } | |
4137 | ||
3e4093b6 RS |
4138 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
4139 | argtype = TREE_TYPE (arg); | |
400fbf9f | 4140 | |
3e4093b6 | 4141 | /* If the lvalue is const or volatile, merge that into the type |
e73a83fc | 4142 | to which the address will point. This is only needed |
f2c1da78 | 4143 | for function types. */ |
6615c446 | 4144 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) |
e73a83fc RB |
4145 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) |
4146 | && TREE_CODE (argtype) == FUNCTION_TYPE) | |
f2c1da78 JM |
4147 | { |
4148 | int orig_quals = TYPE_QUALS (strip_array_types (argtype)); | |
4149 | int quals = orig_quals; | |
4150 | ||
4151 | if (TREE_READONLY (arg)) | |
4152 | quals |= TYPE_QUAL_CONST; | |
4153 | if (TREE_THIS_VOLATILE (arg)) | |
4154 | quals |= TYPE_QUAL_VOLATILE; | |
4155 | ||
f2c1da78 JM |
4156 | argtype = c_build_qualified_type (argtype, quals); |
4157 | } | |
400fbf9f | 4158 | |
3e4093b6 RS |
4159 | if (!c_mark_addressable (arg)) |
4160 | return error_mark_node; | |
400fbf9f | 4161 | |
abb54d14 JM |
4162 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF |
4163 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
400fbf9f | 4164 | |
5cc200fc | 4165 | argtype = build_pointer_type (argtype); |
5e55f99d RH |
4166 | |
4167 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
4168 | when we have proper support for integer constant expressions. */ | |
4169 | val = get_base_address (arg); | |
4170 | if (val && TREE_CODE (val) == INDIRECT_REF | |
3aa2ddb8 JJ |
4171 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
4172 | { | |
cf9e9959 | 4173 | ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg)); |
c9f9eb5d | 4174 | goto return_build_unary_op; |
3aa2ddb8 | 4175 | } |
5e55f99d | 4176 | |
5cc200fc | 4177 | val = build1 (ADDR_EXPR, argtype, arg); |
400fbf9f | 4178 | |
c9f9eb5d AH |
4179 | ret = val; |
4180 | goto return_build_unary_op; | |
400fbf9f | 4181 | |
3e4093b6 | 4182 | default: |
1344f9a3 | 4183 | gcc_unreachable (); |
3e4093b6 | 4184 | } |
400fbf9f | 4185 | |
3e4093b6 RS |
4186 | if (argtype == 0) |
4187 | argtype = TREE_TYPE (arg); | |
928c19bb JM |
4188 | if (TREE_CODE (arg) == INTEGER_CST) |
4189 | ret = (require_constant_value | |
db3927fb AH |
4190 | ? fold_build1_initializer_loc (location, code, argtype, arg) |
4191 | : fold_build1_loc (location, code, argtype, arg)); | |
928c19bb JM |
4192 | else |
4193 | ret = build1 (code, argtype, arg); | |
c9f9eb5d AH |
4194 | return_build_unary_op: |
4195 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
4196 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) |
4197 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
4198 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
4199 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
4200 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
4201 | if (eptype) |
4202 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
c9f9eb5d AH |
4203 | protected_set_expr_location (ret, location); |
4204 | return ret; | |
3e4093b6 | 4205 | } |
400fbf9f | 4206 | |
3e4093b6 RS |
4207 | /* Return nonzero if REF is an lvalue valid for this language. |
4208 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
5baeaac0 | 4209 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
400fbf9f | 4210 | |
1e4ae551 | 4211 | bool |
58f9752a | 4212 | lvalue_p (const_tree ref) |
3e4093b6 | 4213 | { |
58f9752a | 4214 | const enum tree_code code = TREE_CODE (ref); |
400fbf9f | 4215 | |
3e4093b6 RS |
4216 | switch (code) |
4217 | { | |
4218 | case REALPART_EXPR: | |
4219 | case IMAGPART_EXPR: | |
4220 | case COMPONENT_REF: | |
4221 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
400fbf9f | 4222 | |
928c19bb JM |
4223 | case C_MAYBE_CONST_EXPR: |
4224 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
4225 | ||
3e4093b6 RS |
4226 | case COMPOUND_LITERAL_EXPR: |
4227 | case STRING_CST: | |
4228 | return 1; | |
400fbf9f | 4229 | |
3e4093b6 RS |
4230 | case INDIRECT_REF: |
4231 | case ARRAY_REF: | |
36536d79 | 4232 | case ARRAY_NOTATION_REF: |
3e4093b6 RS |
4233 | case VAR_DECL: |
4234 | case PARM_DECL: | |
4235 | case RESULT_DECL: | |
4236 | case ERROR_MARK: | |
4237 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
4238 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
665f2503 | 4239 | |
3e4093b6 | 4240 | case BIND_EXPR: |
3e4093b6 | 4241 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
665f2503 | 4242 | |
3e4093b6 RS |
4243 | default: |
4244 | return 0; | |
4245 | } | |
4246 | } | |
400fbf9f | 4247 | \f |
f37acdf9 JM |
4248 | /* Give a warning for storing in something that is read-only in GCC |
4249 | terms but not const in ISO C terms. */ | |
4250 | ||
4251 | static void | |
4252 | readonly_warning (tree arg, enum lvalue_use use) | |
4253 | { | |
4254 | switch (use) | |
4255 | { | |
4256 | case lv_assign: | |
4257 | warning (0, "assignment of read-only location %qE", arg); | |
4258 | break; | |
4259 | case lv_increment: | |
4260 | warning (0, "increment of read-only location %qE", arg); | |
4261 | break; | |
4262 | case lv_decrement: | |
4263 | warning (0, "decrement of read-only location %qE", arg); | |
4264 | break; | |
4265 | default: | |
4266 | gcc_unreachable (); | |
4267 | } | |
4268 | return; | |
4269 | } | |
4270 | ||
37dc0d8d JM |
4271 | |
4272 | /* Return nonzero if REF is an lvalue valid for this language; | |
4273 | otherwise, print an error message and return zero. USE says | |
7bd11157 TT |
4274 | how the lvalue is being used and so selects the error message. |
4275 | LOCATION is the location at which any error should be reported. */ | |
37dc0d8d JM |
4276 | |
4277 | static int | |
7bd11157 | 4278 | lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) |
37dc0d8d JM |
4279 | { |
4280 | int win = lvalue_p (ref); | |
4281 | ||
4282 | if (!win) | |
7bd11157 | 4283 | lvalue_error (loc, use); |
37dc0d8d JM |
4284 | |
4285 | return win; | |
4286 | } | |
3e4093b6 RS |
4287 | \f |
4288 | /* Mark EXP saying that we need to be able to take the | |
4289 | address of it; it should not be allocated in a register. | |
4290 | Returns true if successful. */ | |
54c93c30 | 4291 | |
3e4093b6 RS |
4292 | bool |
4293 | c_mark_addressable (tree exp) | |
400fbf9f | 4294 | { |
3e4093b6 | 4295 | tree x = exp; |
95602da1 | 4296 | |
3e4093b6 RS |
4297 | while (1) |
4298 | switch (TREE_CODE (x)) | |
4299 | { | |
4300 | case COMPONENT_REF: | |
4301 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
4302 | { | |
0039fa55 AN |
4303 | error |
4304 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
3e4093b6 RS |
4305 | return false; |
4306 | } | |
95602da1 | 4307 | |
3e4093b6 | 4308 | /* ... fall through ... */ |
95602da1 | 4309 | |
3e4093b6 RS |
4310 | case ADDR_EXPR: |
4311 | case ARRAY_REF: | |
4312 | case REALPART_EXPR: | |
4313 | case IMAGPART_EXPR: | |
4314 | x = TREE_OPERAND (x, 0); | |
4315 | break; | |
95602da1 | 4316 | |
3e4093b6 RS |
4317 | case COMPOUND_LITERAL_EXPR: |
4318 | case CONSTRUCTOR: | |
4319 | TREE_ADDRESSABLE (x) = 1; | |
4320 | return true; | |
95602da1 | 4321 | |
3e4093b6 RS |
4322 | case VAR_DECL: |
4323 | case CONST_DECL: | |
4324 | case PARM_DECL: | |
4325 | case RESULT_DECL: | |
5baeaac0 | 4326 | if (C_DECL_REGISTER (x) |
3e4093b6 RS |
4327 | && DECL_NONLOCAL (x)) |
4328 | { | |
e697b20f | 4329 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
3e4093b6 | 4330 | { |
0039fa55 AN |
4331 | error |
4332 | ("global register variable %qD used in nested function", x); | |
3e4093b6 RS |
4333 | return false; |
4334 | } | |
509c9d60 | 4335 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); |
3e4093b6 | 4336 | } |
5baeaac0 | 4337 | else if (C_DECL_REGISTER (x)) |
3e4093b6 | 4338 | { |
e697b20f | 4339 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
0039fa55 AN |
4340 | error ("address of global register variable %qD requested", x); |
4341 | else | |
4342 | error ("address of register variable %qD requested", x); | |
4343 | return false; | |
3e4093b6 | 4344 | } |
400fbf9f | 4345 | |
3e4093b6 RS |
4346 | /* drops in */ |
4347 | case FUNCTION_DECL: | |
4348 | TREE_ADDRESSABLE (x) = 1; | |
4349 | /* drops out */ | |
4350 | default: | |
4351 | return true; | |
4352 | } | |
4353 | } | |
4354 | \f | |
2d2e923f MLI |
4355 | /* Convert EXPR to TYPE, warning about conversion problems with |
4356 | constants. SEMANTIC_TYPE is the type this conversion would use | |
4357 | without excess precision. If SEMANTIC_TYPE is NULL, this function | |
4358 | is equivalent to convert_and_check. This function is a wrapper that | |
4359 | handles conversions that may be different than | |
4360 | the usual ones because of excess precision. */ | |
4361 | ||
4362 | static tree | |
68fca595 MP |
4363 | ep_convert_and_check (location_t loc, tree type, tree expr, |
4364 | tree semantic_type) | |
2d2e923f MLI |
4365 | { |
4366 | if (TREE_TYPE (expr) == type) | |
4367 | return expr; | |
4368 | ||
4369 | if (!semantic_type) | |
68fca595 | 4370 | return convert_and_check (loc, type, expr); |
2d2e923f MLI |
4371 | |
4372 | if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
4373 | && TREE_TYPE (expr) != semantic_type) | |
4374 | { | |
4375 | /* For integers, we need to check the real conversion, not | |
4376 | the conversion to the excess precision type. */ | |
68fca595 | 4377 | expr = convert_and_check (loc, semantic_type, expr); |
2d2e923f MLI |
4378 | } |
4379 | /* Result type is the excess precision type, which should be | |
4380 | large enough, so do not check. */ | |
4381 | return convert (type, expr); | |
4382 | } | |
4383 | ||
928c19bb JM |
4384 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If |
4385 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
4386 | if folded to an integer constant then the unselected half may | |
4387 | contain arbitrary operations not normally permitted in constant | |
c2255bc4 | 4388 | expressions. Set the location of the expression to LOC. */ |
400fbf9f JW |
4389 | |
4390 | tree | |
744aa42f | 4391 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, |
d130ae11 ILT |
4392 | tree op1, tree op1_original_type, tree op2, |
4393 | tree op2_original_type) | |
400fbf9f | 4394 | { |
3e4093b6 RS |
4395 | tree type1; |
4396 | tree type2; | |
4397 | enum tree_code code1; | |
4398 | enum tree_code code2; | |
4399 | tree result_type = NULL; | |
2d2e923f | 4400 | tree semantic_result_type = NULL; |
3e4093b6 | 4401 | tree orig_op1 = op1, orig_op2 = op2; |
928c19bb | 4402 | bool int_const, op1_int_operands, op2_int_operands, int_operands; |
4d84fe7c | 4403 | bool ifexp_int_operands; |
928c19bb | 4404 | tree ret; |
400fbf9f | 4405 | |
4d84fe7c JM |
4406 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); |
4407 | if (op1_int_operands) | |
4408 | op1 = remove_c_maybe_const_expr (op1); | |
4409 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
4410 | if (op2_int_operands) | |
4411 | op2 = remove_c_maybe_const_expr (op2); | |
4412 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
4413 | if (ifexp_int_operands) | |
4414 | ifexp = remove_c_maybe_const_expr (ifexp); | |
4415 | ||
3e4093b6 RS |
4416 | /* Promote both alternatives. */ |
4417 | ||
4418 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
4419 | op1 = default_conversion (op1); | |
4420 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
4421 | op2 = default_conversion (op2); | |
4422 | ||
4423 | if (TREE_CODE (ifexp) == ERROR_MARK | |
4424 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
4425 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
400fbf9f | 4426 | return error_mark_node; |
400fbf9f | 4427 | |
3e4093b6 RS |
4428 | type1 = TREE_TYPE (op1); |
4429 | code1 = TREE_CODE (type1); | |
4430 | type2 = TREE_TYPE (op2); | |
4431 | code2 = TREE_CODE (type2); | |
4432 | ||
b1adf557 JM |
4433 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
4434 | In C99 they will be pointers by now. */ | |
4435 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
4436 | { | |
744aa42f | 4437 | error_at (colon_loc, "non-lvalue array in conditional expression"); |
b1adf557 JM |
4438 | return error_mark_node; |
4439 | } | |
4440 | ||
8ce94e44 JM |
4441 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR |
4442 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4443 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4444 | || code1 == COMPLEX_TYPE) | |
4445 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4446 | || code2 == COMPLEX_TYPE)) | |
4447 | { | |
2d2e923f | 4448 | semantic_result_type = c_common_type (type1, type2); |
8ce94e44 JM |
4449 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) |
4450 | { | |
4451 | op1 = TREE_OPERAND (op1, 0); | |
4452 | type1 = TREE_TYPE (op1); | |
4453 | gcc_assert (TREE_CODE (type1) == code1); | |
4454 | } | |
4455 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4456 | { | |
4457 | op2 = TREE_OPERAND (op2, 0); | |
4458 | type2 = TREE_TYPE (op2); | |
4459 | gcc_assert (TREE_CODE (type2) == code2); | |
4460 | } | |
4461 | } | |
4462 | ||
d130ae11 ILT |
4463 | if (warn_cxx_compat) |
4464 | { | |
4465 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
4466 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
4467 | ||
4468 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
4469 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
4470 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
4471 | warning_at (colon_loc, OPT_Wc___compat, | |
4472 | ("different enum types in conditional is " | |
4473 | "invalid in C++: %qT vs %qT"), | |
4474 | t1, t2); | |
4475 | } | |
4476 | ||
3e4093b6 RS |
4477 | /* Quickly detect the usual case where op1 and op2 have the same type |
4478 | after promotion. */ | |
4479 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
400fbf9f | 4480 | { |
3e4093b6 RS |
4481 | if (type1 == type2) |
4482 | result_type = type1; | |
4483 | else | |
4484 | result_type = TYPE_MAIN_VARIANT (type1); | |
4485 | } | |
4486 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
c22cacf3 MS |
4487 | || code1 == COMPLEX_TYPE) |
4488 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4489 | || code2 == COMPLEX_TYPE)) | |
3e4093b6 | 4490 | { |
ccf7f880 | 4491 | result_type = c_common_type (type1, type2); |
0a0b3574 MM |
4492 | do_warn_double_promotion (result_type, type1, type2, |
4493 | "implicit conversion from %qT to %qT to " | |
4494 | "match other result of conditional", | |
4495 | colon_loc); | |
400fbf9f | 4496 | |
3e4093b6 RS |
4497 | /* If -Wsign-compare, warn here if type1 and type2 have |
4498 | different signedness. We'll promote the signed to unsigned | |
4499 | and later code won't know it used to be different. | |
4500 | Do this check on the original types, so that explicit casts | |
4501 | will be considered, but default promotions won't. */ | |
7d882b83 | 4502 | if (c_inhibit_evaluation_warnings == 0) |
ab87f8c8 | 4503 | { |
8df83eae RK |
4504 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
4505 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
400fbf9f | 4506 | |
3e4093b6 RS |
4507 | if (unsigned_op1 ^ unsigned_op2) |
4508 | { | |
6ac01510 ILT |
4509 | bool ovf; |
4510 | ||
3e4093b6 RS |
4511 | /* Do not warn if the result type is signed, since the |
4512 | signed type will only be chosen if it can represent | |
4513 | all the values of the unsigned type. */ | |
3f75a254 | 4514 | if (!TYPE_UNSIGNED (result_type)) |
3e4093b6 | 4515 | /* OK */; |
3e4093b6 | 4516 | else |
928c19bb JM |
4517 | { |
4518 | bool op1_maybe_const = true; | |
4519 | bool op2_maybe_const = true; | |
4520 | ||
4521 | /* Do not warn if the signed quantity is an | |
4522 | unsuffixed integer literal (or some static | |
4523 | constant expression involving such literals) and | |
4524 | it is non-negative. This warning requires the | |
4525 | operands to be folded for best results, so do | |
4526 | that folding in this case even without | |
4527 | warn_sign_compare to avoid warning options | |
4528 | possibly affecting code generation. */ | |
f5178456 RS |
4529 | c_inhibit_evaluation_warnings |
4530 | += (ifexp == truthvalue_false_node); | |
928c19bb JM |
4531 | op1 = c_fully_fold (op1, require_constant_value, |
4532 | &op1_maybe_const); | |
f5178456 RS |
4533 | c_inhibit_evaluation_warnings |
4534 | -= (ifexp == truthvalue_false_node); | |
4535 | ||
4536 | c_inhibit_evaluation_warnings | |
4537 | += (ifexp == truthvalue_true_node); | |
928c19bb JM |
4538 | op2 = c_fully_fold (op2, require_constant_value, |
4539 | &op2_maybe_const); | |
f5178456 RS |
4540 | c_inhibit_evaluation_warnings |
4541 | -= (ifexp == truthvalue_true_node); | |
928c19bb JM |
4542 | |
4543 | if (warn_sign_compare) | |
4544 | { | |
4545 | if ((unsigned_op2 | |
4546 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4547 | || (unsigned_op1 | |
4548 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4549 | /* OK */; | |
4550 | else | |
744aa42f ILT |
4551 | warning_at (colon_loc, OPT_Wsign_compare, |
4552 | ("signed and unsigned type in " | |
4553 | "conditional expression")); | |
928c19bb JM |
4554 | } |
4555 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
e5a94231 | 4556 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 4557 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) |
e5a94231 | 4558 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); |
928c19bb | 4559 | } |
3e4093b6 RS |
4560 | } |
4561 | } | |
4562 | } | |
4563 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4564 | { | |
fcf73884 | 4565 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) |
c1771a20 | 4566 | pedwarn (colon_loc, OPT_Wpedantic, |
fcf73884 | 4567 | "ISO C forbids conditional expr with only one void side"); |
3e4093b6 RS |
4568 | result_type = void_type_node; |
4569 | } | |
4570 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4571 | { | |
36c5e70a BE |
4572 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); |
4573 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4574 | addr_space_t as_common; | |
4575 | ||
744aa42f | 4576 | if (comp_target_types (colon_loc, type1, type2)) |
10bc1b1b | 4577 | result_type = common_pointer_type (type1, type2); |
6aa3c60d | 4578 | else if (null_pointer_constant_p (orig_op1)) |
36c5e70a | 4579 | result_type = type2; |
6aa3c60d | 4580 | else if (null_pointer_constant_p (orig_op2)) |
36c5e70a BE |
4581 | result_type = type1; |
4582 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4583 | { | |
4584 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4585 | "used in conditional expression"); | |
4586 | return error_mark_node; | |
4587 | } | |
267bac10 JM |
4588 | else if (VOID_TYPE_P (TREE_TYPE (type1)) |
4589 | && !TYPE_ATOMIC (TREE_TYPE (type1))) | |
34a80643 | 4590 | { |
fcf73884 | 4591 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
c1771a20 | 4592 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4593 | "ISO C forbids conditional expr between " |
bda67431 | 4594 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4595 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), |
4596 | TREE_TYPE (type2))); | |
34a80643 | 4597 | } |
267bac10 JM |
4598 | else if (VOID_TYPE_P (TREE_TYPE (type2)) |
4599 | && !TYPE_ATOMIC (TREE_TYPE (type2))) | |
1c2a9b35 | 4600 | { |
fcf73884 | 4601 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
c1771a20 | 4602 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4603 | "ISO C forbids conditional expr between " |
bda67431 | 4604 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4605 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), |
4606 | TREE_TYPE (type1))); | |
1c2a9b35 | 4607 | } |
b581b85b NP |
4608 | /* Objective-C pointer comparisons are a bit more lenient. */ |
4609 | else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) | |
4610 | result_type = objc_common_type (type1, type2); | |
34a80643 | 4611 | else |
ab87f8c8 | 4612 | { |
36c5e70a BE |
4613 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
4614 | ||
b581b85b NP |
4615 | pedwarn (colon_loc, 0, |
4616 | "pointer type mismatch in conditional expression"); | |
36c5e70a BE |
4617 | result_type = build_pointer_type |
4618 | (build_qualified_type (void_type_node, qual)); | |
ab87f8c8 | 4619 | } |
3e4093b6 RS |
4620 | } |
4621 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4622 | { | |
6aa3c60d | 4623 | if (!null_pointer_constant_p (orig_op2)) |
744aa42f | 4624 | pedwarn (colon_loc, 0, |
509c9d60 | 4625 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4626 | else |
ab87f8c8 | 4627 | { |
3e4093b6 | 4628 | op2 = null_pointer_node; |
ab87f8c8 | 4629 | } |
3e4093b6 RS |
4630 | result_type = type1; |
4631 | } | |
4632 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4633 | { | |
6aa3c60d | 4634 | if (!null_pointer_constant_p (orig_op1)) |
744aa42f | 4635 | pedwarn (colon_loc, 0, |
509c9d60 | 4636 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4637 | else |
ab87f8c8 | 4638 | { |
3e4093b6 | 4639 | op1 = null_pointer_node; |
ab87f8c8 | 4640 | } |
3e4093b6 RS |
4641 | result_type = type2; |
4642 | } | |
1c2a9b35 | 4643 | |
3e4093b6 RS |
4644 | if (!result_type) |
4645 | { | |
4646 | if (flag_cond_mismatch) | |
4647 | result_type = void_type_node; | |
4648 | else | |
400fbf9f | 4649 | { |
c2255bc4 | 4650 | error_at (colon_loc, "type mismatch in conditional expression"); |
ab87f8c8 | 4651 | return error_mark_node; |
400fbf9f | 4652 | } |
3e4093b6 | 4653 | } |
400fbf9f | 4654 | |
3e4093b6 RS |
4655 | /* Merge const and volatile flags of the incoming types. */ |
4656 | result_type | |
4657 | = build_type_variant (result_type, | |
afbd0665 AS |
4658 | TYPE_READONLY (type1) || TYPE_READONLY (type2), |
4659 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
b6a10c9f | 4660 | |
68fca595 MP |
4661 | op1 = ep_convert_and_check (colon_loc, result_type, op1, |
4662 | semantic_result_type); | |
4663 | op2 = ep_convert_and_check (colon_loc, result_type, op2, | |
4664 | semantic_result_type); | |
b6a10c9f | 4665 | |
928c19bb JM |
4666 | if (ifexp_bcp && ifexp == truthvalue_true_node) |
4667 | { | |
4668 | op2_int_operands = true; | |
4669 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4670 | } | |
4671 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4672 | { | |
4673 | op1_int_operands = true; | |
4674 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4675 | } | |
4d84fe7c | 4676 | int_const = int_operands = (ifexp_int_operands |
928c19bb JM |
4677 | && op1_int_operands |
4678 | && op2_int_operands); | |
4679 | if (int_operands) | |
4680 | { | |
4681 | int_const = ((ifexp == truthvalue_true_node | |
4682 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4683 | && !TREE_OVERFLOW (orig_op1)) | |
4684 | || (ifexp == truthvalue_false_node | |
4685 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4686 | && !TREE_OVERFLOW (orig_op2))); | |
4687 | } | |
4688 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) | |
db3927fb | 4689 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); |
928c19bb JM |
4690 | else |
4691 | { | |
01c7ccbb JM |
4692 | if (int_operands) |
4693 | { | |
f34f1c87 MP |
4694 | /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be |
4695 | nested inside of the expression. */ | |
4696 | op1 = c_fully_fold (op1, false, NULL); | |
4697 | op2 = c_fully_fold (op2, false, NULL); | |
01c7ccbb | 4698 | } |
928c19bb JM |
4699 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); |
4700 | if (int_operands) | |
4701 | ret = note_integer_operands (ret); | |
4702 | } | |
2d2e923f MLI |
4703 | if (semantic_result_type) |
4704 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
928c19bb | 4705 | |
c2255bc4 | 4706 | protected_set_expr_location (ret, colon_loc); |
928c19bb | 4707 | return ret; |
3e4093b6 RS |
4708 | } |
4709 | \f | |
487a92fe | 4710 | /* Return a compound expression that performs two expressions and |
c2255bc4 AH |
4711 | returns the value of the second of them. |
4712 | ||
4713 | LOC is the location of the COMPOUND_EXPR. */ | |
400fbf9f | 4714 | |
3e4093b6 | 4715 | tree |
c2255bc4 | 4716 | build_compound_expr (location_t loc, tree expr1, tree expr2) |
3e4093b6 | 4717 | { |
4d84fe7c | 4718 | bool expr1_int_operands, expr2_int_operands; |
8ce94e44 | 4719 | tree eptype = NULL_TREE; |
928c19bb JM |
4720 | tree ret; |
4721 | ||
b72271b9 | 4722 | if (flag_cilkplus |
939b37da BI |
4723 | && (TREE_CODE (expr1) == CILK_SPAWN_STMT |
4724 | || TREE_CODE (expr2) == CILK_SPAWN_STMT)) | |
4725 | { | |
4726 | error_at (loc, | |
4727 | "spawned function call cannot be part of a comma expression"); | |
4728 | return error_mark_node; | |
4729 | } | |
4d84fe7c JM |
4730 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); |
4731 | if (expr1_int_operands) | |
4732 | expr1 = remove_c_maybe_const_expr (expr1); | |
4733 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4734 | if (expr2_int_operands) | |
4735 | expr2 = remove_c_maybe_const_expr (expr2); | |
4736 | ||
8ce94e44 JM |
4737 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) |
4738 | expr1 = TREE_OPERAND (expr1, 0); | |
4739 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4740 | { | |
4741 | eptype = TREE_TYPE (expr2); | |
4742 | expr2 = TREE_OPERAND (expr2, 0); | |
4743 | } | |
4744 | ||
3f75a254 | 4745 | if (!TREE_SIDE_EFFECTS (expr1)) |
3e4093b6 RS |
4746 | { |
4747 | /* The left-hand operand of a comma expression is like an expression | |
c5409249 | 4748 | statement: with -Wunused, we should warn if it doesn't have |
3e4093b6 | 4749 | any side-effects, unless it was explicitly cast to (void). */ |
e14a6540 | 4750 | if (warn_unused_value) |
47aecf47 | 4751 | { |
e14a6540 | 4752 | if (VOID_TYPE_P (TREE_TYPE (expr1)) |
1043771b | 4753 | && CONVERT_EXPR_P (expr1)) |
47aecf47 | 4754 | ; /* (void) a, b */ |
e14a6540 JM |
4755 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) |
4756 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
1043771b | 4757 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) |
47aecf47 JM |
4758 | ; /* (void) a, (void) b, c */ |
4759 | else | |
b8698a0f | 4760 | warning_at (loc, OPT_Wunused_value, |
c2255bc4 | 4761 | "left-hand operand of comma expression has no effect"); |
47aecf47 | 4762 | } |
3e4093b6 | 4763 | } |
789eadcd MP |
4764 | else if (TREE_CODE (expr1) == COMPOUND_EXPR |
4765 | && warn_unused_value) | |
4766 | { | |
4767 | tree r = expr1; | |
4768 | location_t cloc = loc; | |
4769 | while (TREE_CODE (r) == COMPOUND_EXPR) | |
4770 | { | |
4771 | if (EXPR_HAS_LOCATION (r)) | |
4772 | cloc = EXPR_LOCATION (r); | |
4773 | r = TREE_OPERAND (r, 1); | |
4774 | } | |
4775 | if (!TREE_SIDE_EFFECTS (r) | |
4776 | && !VOID_TYPE_P (TREE_TYPE (r)) | |
4777 | && !CONVERT_EXPR_P (r)) | |
4778 | warning_at (cloc, OPT_Wunused_value, | |
4779 | "right-hand operand of comma expression has no effect"); | |
4780 | } | |
400fbf9f | 4781 | |
3e4093b6 RS |
4782 | /* With -Wunused, we should also warn if the left-hand operand does have |
4783 | side-effects, but computes a value which is not used. For example, in | |
4784 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4785 | so we should issue a warning. */ | |
4786 | else if (warn_unused_value) | |
c2255bc4 | 4787 | warn_if_unused_value (expr1, loc); |
400fbf9f | 4788 | |
e63d6886 AP |
4789 | if (expr2 == error_mark_node) |
4790 | return error_mark_node; | |
4791 | ||
928c19bb JM |
4792 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
4793 | ||
4794 | if (flag_isoc99 | |
4d84fe7c JM |
4795 | && expr1_int_operands |
4796 | && expr2_int_operands) | |
928c19bb JM |
4797 | ret = note_integer_operands (ret); |
4798 | ||
8ce94e44 JM |
4799 | if (eptype) |
4800 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4801 | ||
c2255bc4 | 4802 | protected_set_expr_location (ret, loc); |
928c19bb | 4803 | return ret; |
3e4093b6 | 4804 | } |
400fbf9f | 4805 | |
67165eb3 ILT |
4806 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to |
4807 | which we are casting. OTYPE is the type of the expression being | |
2ee3cb35 MLI |
4808 | cast. Both TYPE and OTYPE are pointer types. LOC is the location |
4809 | of the cast. -Wcast-qual appeared on the command line. Named | |
4810 | address space qualifiers are not handled here, because they result | |
4811 | in different warnings. */ | |
67165eb3 ILT |
4812 | |
4813 | static void | |
2ee3cb35 | 4814 | handle_warn_cast_qual (location_t loc, tree type, tree otype) |
67165eb3 ILT |
4815 | { |
4816 | tree in_type = type; | |
4817 | tree in_otype = otype; | |
4818 | int added = 0; | |
4819 | int discarded = 0; | |
4820 | bool is_const; | |
4821 | ||
4822 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4823 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4824 | nodes is uninteresting and we stop as soon as we hit a | |
4825 | non-POINTER_TYPE node on either type. */ | |
4826 | do | |
4827 | { | |
4828 | in_otype = TREE_TYPE (in_otype); | |
4829 | in_type = TREE_TYPE (in_type); | |
4830 | ||
4831 | /* GNU C allows cv-qualified function types. 'const' means the | |
4832 | function is very pure, 'volatile' means it can't return. We | |
4833 | need to warn when such qualifiers are added, not when they're | |
4834 | taken away. */ | |
4835 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4836 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
36c5e70a BE |
4837 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) |
4838 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
67165eb3 | 4839 | else |
36c5e70a BE |
4840 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) |
4841 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
67165eb3 ILT |
4842 | } |
4843 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4844 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4845 | ||
4846 | if (added) | |
2ee3cb35 MLI |
4847 | warning_at (loc, OPT_Wcast_qual, |
4848 | "cast adds %q#v qualifier to function type", added); | |
67165eb3 ILT |
4849 | |
4850 | if (discarded) | |
4851 | /* There are qualifiers present in IN_OTYPE that are not present | |
4852 | in IN_TYPE. */ | |
2ee3cb35 | 4853 | warning_at (loc, OPT_Wcast_qual, |
7485aeea | 4854 | "cast discards %qv qualifier from pointer target type", |
2ee3cb35 | 4855 | discarded); |
67165eb3 ILT |
4856 | |
4857 | if (added || discarded) | |
4858 | return; | |
4859 | ||
4860 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4861 | const value to be changed with no additional warning. We only | |
4862 | issue this warning if T is the same on both sides, and we only | |
4863 | issue the warning if there are the same number of pointers on | |
4864 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4865 | cast is unsafe when a qualifier is added at one level and const | |
4866 | is not present at all outer levels. | |
4867 | ||
4868 | To issue this warning, we check at each level whether the cast | |
4869 | adds new qualifiers not already seen. We don't need to special | |
4870 | case function types, as they won't have the same | |
4871 | TYPE_MAIN_VARIANT. */ | |
4872 | ||
4873 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4874 | return; | |
4875 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4876 | return; | |
4877 | ||
4878 | in_type = type; | |
4879 | in_otype = otype; | |
4880 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4881 | do | |
4882 | { | |
4883 | in_type = TREE_TYPE (in_type); | |
4884 | in_otype = TREE_TYPE (in_otype); | |
4885 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4886 | && !is_const) | |
4887 | { | |
2ee3cb35 MLI |
4888 | warning_at (loc, OPT_Wcast_qual, |
4889 | "to be safe all intermediate pointers in cast from " | |
4890 | "%qT to %qT must be %<const%> qualified", | |
4891 | otype, type); | |
67165eb3 ILT |
4892 | break; |
4893 | } | |
4894 | if (is_const) | |
4895 | is_const = TYPE_READONLY (in_type); | |
4896 | } | |
4897 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4898 | } | |
4899 | ||
b8698a0f | 4900 | /* Build an expression representing a cast to type TYPE of expression EXPR. |
c2255bc4 | 4901 | LOC is the location of the cast-- typically the open paren of the cast. */ |
400fbf9f | 4902 | |
3e4093b6 | 4903 | tree |
c2255bc4 | 4904 | build_c_cast (location_t loc, tree type, tree expr) |
3e4093b6 | 4905 | { |
8ce94e44 JM |
4906 | tree value; |
4907 | ||
4908 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
4909 | expr = TREE_OPERAND (expr, 0); | |
4910 | ||
4911 | value = expr; | |
400fbf9f | 4912 | |
3e4093b6 RS |
4913 | if (type == error_mark_node || expr == error_mark_node) |
4914 | return error_mark_node; | |
400fbf9f | 4915 | |
3e4093b6 RS |
4916 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
4917 | only in <protocol> qualifications. But when constructing cast expressions, | |
4918 | the protocols do matter and must be kept around. */ | |
700686fa ZL |
4919 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) |
4920 | return build1 (NOP_EXPR, type, expr); | |
4921 | ||
4922 | type = TYPE_MAIN_VARIANT (type); | |
400fbf9f | 4923 | |
3e4093b6 RS |
4924 | if (TREE_CODE (type) == ARRAY_TYPE) |
4925 | { | |
c2255bc4 | 4926 | error_at (loc, "cast specifies array type"); |
3e4093b6 RS |
4927 | return error_mark_node; |
4928 | } | |
400fbf9f | 4929 | |
3e4093b6 RS |
4930 | if (TREE_CODE (type) == FUNCTION_TYPE) |
4931 | { | |
c2255bc4 | 4932 | error_at (loc, "cast specifies function type"); |
3e4093b6 RS |
4933 | return error_mark_node; |
4934 | } | |
400fbf9f | 4935 | |
808d6eaa JM |
4936 | if (!VOID_TYPE_P (type)) |
4937 | { | |
4938 | value = require_complete_type (value); | |
4939 | if (value == error_mark_node) | |
4940 | return error_mark_node; | |
4941 | } | |
4942 | ||
3e4093b6 RS |
4943 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
4944 | { | |
fcf73884 MLI |
4945 | if (TREE_CODE (type) == RECORD_TYPE |
4946 | || TREE_CODE (type) == UNION_TYPE) | |
c1771a20 | 4947 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 4948 | "ISO C forbids casting nonscalar to the same type"); |
3e4093b6 RS |
4949 | } |
4950 | else if (TREE_CODE (type) == UNION_TYPE) | |
4951 | { | |
4952 | tree field; | |
400fbf9f | 4953 | |
910ad8de | 4954 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
39ffbac9 VR |
4955 | if (TREE_TYPE (field) != error_mark_node |
4956 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
4957 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3e4093b6 RS |
4958 | break; |
4959 | ||
4960 | if (field) | |
400fbf9f | 4961 | { |
3e4093b6 | 4962 | tree t; |
e616f54d | 4963 | bool maybe_const = true; |
3e4093b6 | 4964 | |
c1771a20 | 4965 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); |
e616f54d JM |
4966 | t = c_fully_fold (value, false, &maybe_const); |
4967 | t = build_constructor_single (type, field, t); | |
4968 | if (!maybe_const) | |
4969 | t = c_wrap_maybe_const (t, true); | |
4970 | t = digest_init (loc, type, t, | |
bbbbb16a | 4971 | NULL_TREE, false, true, 0); |
3e4093b6 RS |
4972 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
4973 | return t; | |
400fbf9f | 4974 | } |
c2255bc4 | 4975 | error_at (loc, "cast to union type from type not present in union"); |
3e4093b6 RS |
4976 | return error_mark_node; |
4977 | } | |
4978 | else | |
4979 | { | |
4980 | tree otype, ovalue; | |
400fbf9f | 4981 | |
3e4093b6 | 4982 | if (type == void_type_node) |
c2255bc4 AH |
4983 | { |
4984 | tree t = build1 (CONVERT_EXPR, type, value); | |
4985 | SET_EXPR_LOCATION (t, loc); | |
4986 | return t; | |
4987 | } | |
400fbf9f | 4988 | |
3e4093b6 | 4989 | otype = TREE_TYPE (value); |
400fbf9f | 4990 | |
3e4093b6 | 4991 | /* Optionally warn about potentially worrisome casts. */ |
3e4093b6 RS |
4992 | if (warn_cast_qual |
4993 | && TREE_CODE (type) == POINTER_TYPE | |
4994 | && TREE_CODE (otype) == POINTER_TYPE) | |
2ee3cb35 | 4995 | handle_warn_cast_qual (loc, type, otype); |
400fbf9f | 4996 | |
36c5e70a BE |
4997 | /* Warn about conversions between pointers to disjoint |
4998 | address spaces. */ | |
4999 | if (TREE_CODE (type) == POINTER_TYPE | |
5000 | && TREE_CODE (otype) == POINTER_TYPE | |
5001 | && !null_pointer_constant_p (value)) | |
5002 | { | |
5003 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
5004 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
5005 | addr_space_t as_common; | |
5006 | ||
5007 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
5008 | { | |
5009 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
5010 | warning_at (loc, 0, "cast to %s address space pointer " | |
5011 | "from disjoint generic address space pointer", | |
5012 | c_addr_space_name (as_to)); | |
5013 | ||
5014 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
5015 | warning_at (loc, 0, "cast to generic address space pointer " | |
5016 | "from disjoint %s address space pointer", | |
5017 | c_addr_space_name (as_from)); | |
5018 | ||
5019 | else | |
5020 | warning_at (loc, 0, "cast to %s address space pointer " | |
5021 | "from disjoint %s address space pointer", | |
5022 | c_addr_space_name (as_to), | |
5023 | c_addr_space_name (as_from)); | |
5024 | } | |
5025 | } | |
5026 | ||
3e4093b6 | 5027 | /* Warn about possible alignment problems. */ |
3176a0c2 | 5028 | if (STRICT_ALIGNMENT |
3e4093b6 RS |
5029 | && TREE_CODE (type) == POINTER_TYPE |
5030 | && TREE_CODE (otype) == POINTER_TYPE | |
5031 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
5032 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
5033 | /* Don't warn about opaque types, where the actual alignment | |
5034 | restriction is unknown. */ | |
5035 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
5036 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
5037 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
5038 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
c2255bc4 AH |
5039 | warning_at (loc, OPT_Wcast_align, |
5040 | "cast increases required alignment of target type"); | |
e9a25f70 | 5041 | |
3176a0c2 | 5042 | if (TREE_CODE (type) == INTEGER_TYPE |
3e4093b6 | 5043 | && TREE_CODE (otype) == POINTER_TYPE |
8d1c7aef | 5044 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) |
c22cacf3 MS |
5045 | /* Unlike conversion of integers to pointers, where the |
5046 | warning is disabled for converting constants because | |
5047 | of cases such as SIG_*, warn about converting constant | |
5048 | pointers to integers. In some cases it may cause unwanted | |
8d1c7aef | 5049 | sign extension, and a warning is appropriate. */ |
c2255bc4 AH |
5050 | warning_at (loc, OPT_Wpointer_to_int_cast, |
5051 | "cast from pointer to integer of different size"); | |
400fbf9f | 5052 | |
3176a0c2 | 5053 | if (TREE_CODE (value) == CALL_EXPR |
3e4093b6 | 5054 | && TREE_CODE (type) != TREE_CODE (otype)) |
c2255bc4 AH |
5055 | warning_at (loc, OPT_Wbad_function_cast, |
5056 | "cast from function call of type %qT " | |
5057 | "to non-matching type %qT", otype, type); | |
400fbf9f | 5058 | |
3176a0c2 | 5059 | if (TREE_CODE (type) == POINTER_TYPE |
3e4093b6 RS |
5060 | && TREE_CODE (otype) == INTEGER_TYPE |
5061 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
5062 | /* Don't warn about converting any constant. */ | |
5063 | && !TREE_CONSTANT (value)) | |
c2255bc4 AH |
5064 | warning_at (loc, |
5065 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
5066 | "of different size"); | |
400fbf9f | 5067 | |
79bedddc SR |
5068 | if (warn_strict_aliasing <= 2) |
5069 | strict_aliasing_warning (otype, type, expr); | |
400fbf9f | 5070 | |
3897f229 JM |
5071 | /* If pedantic, warn for conversions between function and object |
5072 | pointer types, except for converting a null pointer constant | |
5073 | to function pointer type. */ | |
5074 | if (pedantic | |
5075 | && TREE_CODE (type) == POINTER_TYPE | |
5076 | && TREE_CODE (otype) == POINTER_TYPE | |
5077 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
5078 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
c1771a20 | 5079 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 5080 | "conversion of function pointer to object pointer type"); |
3897f229 JM |
5081 | |
5082 | if (pedantic | |
5083 | && TREE_CODE (type) == POINTER_TYPE | |
5084 | && TREE_CODE (otype) == POINTER_TYPE | |
5085 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
5086 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
6aa3c60d | 5087 | && !null_pointer_constant_p (value)) |
c1771a20 | 5088 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 5089 | "conversion of object pointer to function pointer type"); |
3897f229 | 5090 | |
3e4093b6 | 5091 | ovalue = value; |
3e4093b6 | 5092 | value = convert (type, value); |
400fbf9f | 5093 | |
3e4093b6 | 5094 | /* Ignore any integer overflow caused by the cast. */ |
928c19bb | 5095 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) |
3e4093b6 | 5096 | { |
8bcd6380 | 5097 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) |
6414bad6 | 5098 | { |
8bcd6380 RS |
5099 | if (!TREE_OVERFLOW (value)) |
5100 | { | |
5101 | /* Avoid clobbering a shared constant. */ | |
5102 | value = copy_node (value); | |
5103 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
5104 | } | |
6414bad6 | 5105 | } |
8bcd6380 | 5106 | else if (TREE_OVERFLOW (value)) |
d8e1f97b | 5107 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ |
807e902e | 5108 | value = wide_int_to_tree (TREE_TYPE (value), value); |
3e4093b6 RS |
5109 | } |
5110 | } | |
400fbf9f | 5111 | |
53cd18ec JM |
5112 | /* Don't let a cast be an lvalue. */ |
5113 | if (value == expr) | |
db3927fb | 5114 | value = non_lvalue_loc (loc, value); |
e9a25f70 | 5115 | |
928c19bb JM |
5116 | /* Don't allow the results of casting to floating-point or complex |
5117 | types be confused with actual constants, or casts involving | |
5118 | integer and pointer types other than direct integer-to-integer | |
5119 | and integer-to-pointer be confused with integer constant | |
5120 | expressions and null pointer constants. */ | |
5121 | if (TREE_CODE (value) == REAL_CST | |
5122 | || TREE_CODE (value) == COMPLEX_CST | |
5123 | || (TREE_CODE (value) == INTEGER_CST | |
5124 | && !((TREE_CODE (expr) == INTEGER_CST | |
5125 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
5126 | || TREE_CODE (expr) == REAL_CST | |
5127 | || TREE_CODE (expr) == COMPLEX_CST))) | |
5128 | value = build1 (NOP_EXPR, type, value); | |
5129 | ||
c2255bc4 AH |
5130 | if (CAN_HAVE_LOCATION_P (value)) |
5131 | SET_EXPR_LOCATION (value, loc); | |
3e4093b6 | 5132 | return value; |
400fbf9f JW |
5133 | } |
5134 | ||
c2255bc4 AH |
5135 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the |
5136 | location of the open paren of the cast, or the position of the cast | |
5137 | expr. */ | |
3e4093b6 | 5138 | tree |
c2255bc4 | 5139 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) |
400fbf9f | 5140 | { |
f8893e47 | 5141 | tree type; |
928c19bb JM |
5142 | tree type_expr = NULL_TREE; |
5143 | bool type_expr_const = true; | |
5144 | tree ret; | |
3e4093b6 | 5145 | int saved_wsp = warn_strict_prototypes; |
c5c76735 | 5146 | |
3e4093b6 RS |
5147 | /* This avoids warnings about unprototyped casts on |
5148 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
5149 | if (TREE_CODE (expr) == INTEGER_CST) | |
5150 | warn_strict_prototypes = 0; | |
928c19bb | 5151 | type = groktypename (type_name, &type_expr, &type_expr_const); |
3e4093b6 | 5152 | warn_strict_prototypes = saved_wsp; |
c5c76735 | 5153 | |
c2255bc4 | 5154 | ret = build_c_cast (loc, type, expr); |
928c19bb JM |
5155 | if (type_expr) |
5156 | { | |
9f33203d JM |
5157 | bool inner_expr_const = true; |
5158 | ret = c_fully_fold (ret, require_constant_value, &inner_expr_const); | |
928c19bb | 5159 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); |
9f33203d JM |
5160 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const |
5161 | && inner_expr_const); | |
c2255bc4 | 5162 | SET_EXPR_LOCATION (ret, loc); |
928c19bb | 5163 | } |
24b97832 ILT |
5164 | |
5165 | if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) | |
5166 | SET_EXPR_LOCATION (ret, loc); | |
5167 | ||
9e5b2115 PB |
5168 | /* C++ does not permits types to be defined in a cast, but it |
5169 | allows references to incomplete types. */ | |
5170 | if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) | |
24b97832 ILT |
5171 | warning_at (loc, OPT_Wc___compat, |
5172 | "defining a type in a cast is invalid in C++"); | |
5173 | ||
928c19bb | 5174 | return ret; |
400fbf9f | 5175 | } |
3e4093b6 RS |
5176 | \f |
5177 | /* Build an assignment expression of lvalue LHS from value RHS. | |
32e8bb8e ILT |
5178 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which |
5179 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
3e4093b6 RS |
5180 | MODIFYCODE is the code for a binary operator that we use |
5181 | to combine the old value of LHS with RHS to get the new value. | |
c9f9eb5d | 5182 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
bbbbb16a ILT |
5183 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, |
5184 | which may differ from TREE_TYPE (RHS) for an enum value. | |
c9f9eb5d | 5185 | |
c2255bc4 AH |
5186 | LOCATION is the location of the MODIFYCODE operator. |
5187 | RHS_LOC is the location of the RHS. */ | |
2f6e4e97 | 5188 | |
3e4093b6 | 5189 | tree |
32e8bb8e | 5190 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, |
b8698a0f | 5191 | enum tree_code modifycode, |
c2255bc4 | 5192 | location_t rhs_loc, tree rhs, tree rhs_origtype) |
400fbf9f | 5193 | { |
3e4093b6 RS |
5194 | tree result; |
5195 | tree newrhs; | |
241f845a | 5196 | tree rhseval = NULL_TREE; |
8ce94e44 | 5197 | tree rhs_semantic_type = NULL_TREE; |
3e4093b6 RS |
5198 | tree lhstype = TREE_TYPE (lhs); |
5199 | tree olhstype = lhstype; | |
928c19bb | 5200 | bool npc; |
267bac10 | 5201 | bool is_atomic_op; |
e9a25f70 | 5202 | |
3e4093b6 RS |
5203 | /* Types that aren't fully specified cannot be used in assignments. */ |
5204 | lhs = require_complete_type (lhs); | |
e9a25f70 | 5205 | |
3e4093b6 RS |
5206 | /* Avoid duplicate error messages from operands that had errors. */ |
5207 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
5208 | return error_mark_node; | |
400fbf9f | 5209 | |
4c2ecab0 JM |
5210 | /* Ensure an error for assigning a non-lvalue array to an array in |
5211 | C90. */ | |
5212 | if (TREE_CODE (lhstype) == ARRAY_TYPE) | |
5213 | { | |
5214 | error_at (location, "assignment to expression with array type"); | |
5215 | return error_mark_node; | |
5216 | } | |
5217 | ||
46a88c12 | 5218 | /* For ObjC properties, defer this check. */ |
7bd11157 | 5219 | if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) |
c0bcacec VR |
5220 | return error_mark_node; |
5221 | ||
267bac10 JM |
5222 | is_atomic_op = really_atomic_lvalue (lhs); |
5223 | ||
8ce94e44 JM |
5224 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5225 | { | |
5226 | rhs_semantic_type = TREE_TYPE (rhs); | |
5227 | rhs = TREE_OPERAND (rhs, 0); | |
5228 | } | |
5229 | ||
3e4093b6 | 5230 | newrhs = rhs; |
400fbf9f | 5231 | |
928c19bb JM |
5232 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) |
5233 | { | |
5234 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
c2255bc4 | 5235 | lhs_origtype, modifycode, rhs_loc, rhs, |
32e8bb8e | 5236 | rhs_origtype); |
928c19bb JM |
5237 | if (inner == error_mark_node) |
5238 | return error_mark_node; | |
5239 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
5240 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
5241 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
5242 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
5243 | protected_set_expr_location (result, location); | |
5244 | return result; | |
5245 | } | |
5246 | ||
3e4093b6 RS |
5247 | /* If a binary op has been requested, combine the old LHS value with the RHS |
5248 | producing the value we should actually store into the LHS. */ | |
5249 | ||
5250 | if (modifycode != NOP_EXPR) | |
400fbf9f | 5251 | { |
928c19bb | 5252 | lhs = c_fully_fold (lhs, false, NULL); |
3e4093b6 | 5253 | lhs = stabilize_reference (lhs); |
bbbbb16a | 5254 | |
267bac10 JM |
5255 | /* Construct the RHS for any non-atomic compound assignemnt. */ |
5256 | if (!is_atomic_op) | |
5257 | { | |
241f845a JJ |
5258 | /* If in LHS op= RHS the RHS has side-effects, ensure they |
5259 | are preevaluated before the rest of the assignment expression's | |
5260 | side-effects, because RHS could contain e.g. function calls | |
5261 | that modify LHS. */ | |
5262 | if (TREE_SIDE_EFFECTS (rhs)) | |
5263 | { | |
5264 | newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs); | |
5265 | rhseval = newrhs; | |
5266 | } | |
267bac10 | 5267 | newrhs = build_binary_op (location, |
241f845a | 5268 | modifycode, lhs, newrhs, 1); |
267bac10 JM |
5269 | |
5270 | /* The original type of the right hand side is no longer | |
5271 | meaningful. */ | |
5272 | rhs_origtype = NULL_TREE; | |
5273 | } | |
400fbf9f | 5274 | } |
400fbf9f | 5275 | |
668ea4b1 IS |
5276 | if (c_dialect_objc ()) |
5277 | { | |
46a88c12 NP |
5278 | /* Check if we are modifying an Objective-C property reference; |
5279 | if so, we need to generate setter calls. */ | |
5280 | result = objc_maybe_build_modify_expr (lhs, newrhs); | |
668ea4b1 | 5281 | if (result) |
241f845a | 5282 | goto return_result; |
46a88c12 NP |
5283 | |
5284 | /* Else, do the check that we postponed for Objective-C. */ | |
7bd11157 | 5285 | if (!lvalue_or_else (location, lhs, lv_assign)) |
668ea4b1 IS |
5286 | return error_mark_node; |
5287 | } | |
5288 | ||
9bf24266 | 5289 | /* Give an error for storing in something that is 'const'. */ |
bbbd6700 | 5290 | |
f37acdf9 | 5291 | if (TYPE_READONLY (lhstype) |
3e4093b6 RS |
5292 | || ((TREE_CODE (lhstype) == RECORD_TYPE |
5293 | || TREE_CODE (lhstype) == UNION_TYPE) | |
5294 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
953ff289 | 5295 | { |
c02065fc | 5296 | readonly_error (location, lhs, lv_assign); |
953ff289 DN |
5297 | return error_mark_node; |
5298 | } | |
f37acdf9 JM |
5299 | else if (TREE_READONLY (lhs)) |
5300 | readonly_warning (lhs, lv_assign); | |
bbbd6700 | 5301 | |
3e4093b6 RS |
5302 | /* If storing into a structure or union member, |
5303 | it has probably been given type `int'. | |
5304 | Compute the type that would go with | |
5305 | the actual amount of storage the member occupies. */ | |
bbbd6700 | 5306 | |
3e4093b6 RS |
5307 | if (TREE_CODE (lhs) == COMPONENT_REF |
5308 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
5309 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
5310 | || TREE_CODE (lhstype) == REAL_TYPE | |
5311 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
5312 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
400fbf9f | 5313 | |
3e4093b6 RS |
5314 | /* If storing in a field that is in actuality a short or narrower than one, |
5315 | we must store in the field in its actual type. */ | |
5316 | ||
5317 | if (lhstype != TREE_TYPE (lhs)) | |
5318 | { | |
5319 | lhs = copy_node (lhs); | |
5320 | TREE_TYPE (lhs) = lhstype; | |
400fbf9f | 5321 | } |
400fbf9f | 5322 | |
32e8bb8e ILT |
5323 | /* Issue -Wc++-compat warnings about an assignment to an enum type |
5324 | when LHS does not have its original type. This happens for, | |
5325 | e.g., an enum bitfield in a struct. */ | |
5326 | if (warn_cxx_compat | |
5327 | && lhs_origtype != NULL_TREE | |
5328 | && lhs_origtype != lhstype | |
5329 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
5330 | { | |
5331 | tree checktype = (rhs_origtype != NULL_TREE | |
5332 | ? rhs_origtype | |
5333 | : TREE_TYPE (rhs)); | |
5334 | if (checktype != error_mark_node | |
267bac10 JM |
5335 | && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype) |
5336 | || (is_atomic_op && modifycode != NOP_EXPR))) | |
32e8bb8e ILT |
5337 | warning_at (location, OPT_Wc___compat, |
5338 | "enum conversion in assignment is invalid in C++"); | |
5339 | } | |
5340 | ||
267bac10 JM |
5341 | /* If the lhs is atomic, remove that qualifier. */ |
5342 | if (is_atomic_op) | |
5343 | { | |
5344 | lhstype = build_qualified_type (lhstype, | |
5345 | (TYPE_QUALS (lhstype) | |
5346 | & ~TYPE_QUAL_ATOMIC)); | |
5347 | olhstype = build_qualified_type (olhstype, | |
5348 | (TYPE_QUALS (lhstype) | |
5349 | & ~TYPE_QUAL_ATOMIC)); | |
5350 | } | |
5351 | ||
8ce94e44 JM |
5352 | /* Convert new value to destination type. Fold it first, then |
5353 | restore any excess precision information, for the sake of | |
5354 | conversion warnings. */ | |
400fbf9f | 5355 | |
267bac10 JM |
5356 | if (!(is_atomic_op && modifycode != NOP_EXPR)) |
5357 | { | |
5358 | npc = null_pointer_constant_p (newrhs); | |
5359 | newrhs = c_fully_fold (newrhs, false, NULL); | |
5360 | if (rhs_semantic_type) | |
5361 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
68fca595 MP |
5362 | newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs, |
5363 | rhs_origtype, ic_assign, npc, | |
5364 | NULL_TREE, NULL_TREE, 0); | |
267bac10 JM |
5365 | if (TREE_CODE (newrhs) == ERROR_MARK) |
5366 | return error_mark_node; | |
5367 | } | |
400fbf9f | 5368 | |
6e955430 ZL |
5369 | /* Emit ObjC write barrier, if necessary. */ |
5370 | if (c_dialect_objc () && flag_objc_gc) | |
5371 | { | |
5372 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
5373 | if (result) | |
c9f9eb5d AH |
5374 | { |
5375 | protected_set_expr_location (result, location); | |
241f845a | 5376 | goto return_result; |
c9f9eb5d | 5377 | } |
6e955430 ZL |
5378 | } |
5379 | ||
ea4b7848 | 5380 | /* Scan operands. */ |
400fbf9f | 5381 | |
267bac10 JM |
5382 | if (is_atomic_op) |
5383 | result = build_atomic_assign (location, lhs, modifycode, newrhs, false); | |
5384 | else | |
5385 | { | |
5386 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); | |
5387 | TREE_SIDE_EFFECTS (result) = 1; | |
5388 | protected_set_expr_location (result, location); | |
5389 | } | |
400fbf9f | 5390 | |
3e4093b6 RS |
5391 | /* If we got the LHS in a different type for storing in, |
5392 | convert the result back to the nominal type of LHS | |
5393 | so that the value we return always has the same type | |
5394 | as the LHS argument. */ | |
e855c5ce | 5395 | |
3e4093b6 | 5396 | if (olhstype == TREE_TYPE (result)) |
241f845a | 5397 | goto return_result; |
c9f9eb5d | 5398 | |
68fca595 MP |
5399 | result = convert_for_assignment (location, rhs_loc, olhstype, result, |
5400 | rhs_origtype, ic_assign, false, NULL_TREE, | |
5401 | NULL_TREE, 0); | |
c9f9eb5d | 5402 | protected_set_expr_location (result, location); |
241f845a JJ |
5403 | |
5404 | return_result: | |
5405 | if (rhseval) | |
5406 | result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result); | |
c9f9eb5d | 5407 | return result; |
3e4093b6 RS |
5408 | } |
5409 | \f | |
478a1c5b ILT |
5410 | /* Return whether STRUCT_TYPE has an anonymous field with type TYPE. |
5411 | This is used to implement -fplan9-extensions. */ | |
5412 | ||
5413 | static bool | |
5414 | find_anonymous_field_with_type (tree struct_type, tree type) | |
5415 | { | |
5416 | tree field; | |
5417 | bool found; | |
5418 | ||
5419 | gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE | |
5420 | || TREE_CODE (struct_type) == UNION_TYPE); | |
5421 | found = false; | |
5422 | for (field = TYPE_FIELDS (struct_type); | |
5423 | field != NULL_TREE; | |
5424 | field = TREE_CHAIN (field)) | |
5425 | { | |
267bac10 JM |
5426 | tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) |
5427 | ? c_build_qualified_type (TREE_TYPE (field), | |
5428 | TYPE_QUAL_ATOMIC) | |
5429 | : TYPE_MAIN_VARIANT (TREE_TYPE (field))); | |
478a1c5b | 5430 | if (DECL_NAME (field) == NULL |
267bac10 | 5431 | && comptypes (type, fieldtype)) |
478a1c5b ILT |
5432 | { |
5433 | if (found) | |
5434 | return false; | |
5435 | found = true; | |
5436 | } | |
5437 | else if (DECL_NAME (field) == NULL | |
5438 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
5439 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
5440 | && find_anonymous_field_with_type (TREE_TYPE (field), type)) | |
5441 | { | |
5442 | if (found) | |
5443 | return false; | |
5444 | found = true; | |
5445 | } | |
5446 | } | |
5447 | return found; | |
5448 | } | |
5449 | ||
5450 | /* RHS is an expression whose type is pointer to struct. If there is | |
5451 | an anonymous field in RHS with type TYPE, then return a pointer to | |
5452 | that field in RHS. This is used with -fplan9-extensions. This | |
5453 | returns NULL if no conversion could be found. */ | |
5454 | ||
5455 | static tree | |
5456 | convert_to_anonymous_field (location_t location, tree type, tree rhs) | |
5457 | { | |
5458 | tree rhs_struct_type, lhs_main_type; | |
5459 | tree field, found_field; | |
5460 | bool found_sub_field; | |
5461 | tree ret; | |
5462 | ||
5463 | gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); | |
5464 | rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); | |
5465 | gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE | |
5466 | || TREE_CODE (rhs_struct_type) == UNION_TYPE); | |
5467 | ||
5468 | gcc_assert (POINTER_TYPE_P (type)); | |
267bac10 JM |
5469 | lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type)) |
5470 | ? c_build_qualified_type (TREE_TYPE (type), | |
5471 | TYPE_QUAL_ATOMIC) | |
5472 | : TYPE_MAIN_VARIANT (TREE_TYPE (type))); | |
478a1c5b ILT |
5473 | |
5474 | found_field = NULL_TREE; | |
5475 | found_sub_field = false; | |
5476 | for (field = TYPE_FIELDS (rhs_struct_type); | |
5477 | field != NULL_TREE; | |
5478 | field = TREE_CHAIN (field)) | |
5479 | { | |
5480 | if (DECL_NAME (field) != NULL_TREE | |
5481 | || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE | |
5482 | && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)) | |
5483 | continue; | |
267bac10 JM |
5484 | tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) |
5485 | ? c_build_qualified_type (TREE_TYPE (field), | |
5486 | TYPE_QUAL_ATOMIC) | |
5487 | : TYPE_MAIN_VARIANT (TREE_TYPE (field))); | |
5488 | if (comptypes (lhs_main_type, fieldtype)) | |
478a1c5b ILT |
5489 | { |
5490 | if (found_field != NULL_TREE) | |
5491 | return NULL_TREE; | |
5492 | found_field = field; | |
5493 | } | |
5494 | else if (find_anonymous_field_with_type (TREE_TYPE (field), | |
5495 | lhs_main_type)) | |
5496 | { | |
5497 | if (found_field != NULL_TREE) | |
5498 | return NULL_TREE; | |
5499 | found_field = field; | |
5500 | found_sub_field = true; | |
5501 | } | |
5502 | } | |
5503 | ||
5504 | if (found_field == NULL_TREE) | |
5505 | return NULL_TREE; | |
5506 | ||
5507 | ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), | |
5508 | build_fold_indirect_ref (rhs), found_field, | |
5509 | NULL_TREE); | |
5510 | ret = build_fold_addr_expr_loc (location, ret); | |
5511 | ||
5512 | if (found_sub_field) | |
5513 | { | |
5514 | ret = convert_to_anonymous_field (location, type, ret); | |
5515 | gcc_assert (ret != NULL_TREE); | |
5516 | } | |
5517 | ||
5518 | return ret; | |
5519 | } | |
5520 | ||
63bc4e87 MP |
5521 | /* Issue an error message for a bad initializer component. |
5522 | GMSGID identifies the message. | |
5523 | The component name is taken from the spelling stack. */ | |
5524 | ||
5525 | static void | |
ea58ef42 | 5526 | error_init (location_t loc, const char *gmsgid) |
63bc4e87 MP |
5527 | { |
5528 | char *ofwhat; | |
5529 | ||
5530 | /* The gmsgid may be a format string with %< and %>. */ | |
ea58ef42 | 5531 | error_at (loc, gmsgid); |
63bc4e87 MP |
5532 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
5533 | if (*ofwhat) | |
d7ff7ae5 | 5534 | inform (loc, "(near initialization for %qs)", ofwhat); |
63bc4e87 MP |
5535 | } |
5536 | ||
5537 | /* Issue a pedantic warning for a bad initializer component. OPT is | |
5538 | the option OPT_* (from options.h) controlling this warning or 0 if | |
5539 | it is unconditionally given. GMSGID identifies the message. The | |
5540 | component name is taken from the spelling stack. */ | |
5541 | ||
5542 | static void | |
5543 | pedwarn_init (location_t location, int opt, const char *gmsgid) | |
5544 | { | |
5545 | char *ofwhat; | |
d7ff7ae5 | 5546 | bool warned; |
63bc4e87 MP |
5547 | |
5548 | /* The gmsgid may be a format string with %< and %>. */ | |
d7ff7ae5 | 5549 | warned = pedwarn (location, opt, gmsgid); |
63bc4e87 | 5550 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
d7ff7ae5 MP |
5551 | if (*ofwhat && warned) |
5552 | inform (location, "(near initialization for %qs)", ofwhat); | |
63bc4e87 MP |
5553 | } |
5554 | ||
5555 | /* Issue a warning for a bad initializer component. | |
5556 | ||
5557 | OPT is the OPT_W* value corresponding to the warning option that | |
5558 | controls this warning. GMSGID identifies the message. The | |
5559 | component name is taken from the spelling stack. */ | |
5560 | ||
5561 | static void | |
5562 | warning_init (location_t loc, int opt, const char *gmsgid) | |
5563 | { | |
5564 | char *ofwhat; | |
d7ff7ae5 | 5565 | bool warned; |
63bc4e87 MP |
5566 | |
5567 | /* The gmsgid may be a format string with %< and %>. */ | |
d7ff7ae5 | 5568 | warned = warning_at (loc, opt, gmsgid); |
63bc4e87 | 5569 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
d7ff7ae5 MP |
5570 | if (*ofwhat && warned) |
5571 | inform (loc, "(near initialization for %qs)", ofwhat); | |
63bc4e87 MP |
5572 | } |
5573 | \f | |
5574 | /* If TYPE is an array type and EXPR is a parenthesized string | |
5575 | constant, warn if pedantic that EXPR is being used to initialize an | |
5576 | object of type TYPE. */ | |
5577 | ||
5578 | void | |
d033409e | 5579 | maybe_warn_string_init (location_t loc, tree type, struct c_expr expr) |
63bc4e87 MP |
5580 | { |
5581 | if (pedantic | |
5582 | && TREE_CODE (type) == ARRAY_TYPE | |
5583 | && TREE_CODE (expr.value) == STRING_CST | |
5584 | && expr.original_code != STRING_CST) | |
d033409e | 5585 | pedwarn_init (loc, OPT_Wpedantic, |
63bc4e87 MP |
5586 | "array initialized from parenthesized string constant"); |
5587 | } | |
5588 | ||
bbbbb16a ILT |
5589 | /* Convert value RHS to type TYPE as preparation for an assignment to |
5590 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
5591 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
5592 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
5593 | constant before any folding. | |
3e4093b6 RS |
5594 | The real work of conversion is done by `convert'. |
5595 | The purpose of this function is to generate error messages | |
5596 | for assignments that are not allowed in C. | |
2ac2f164 JM |
5597 | ERRTYPE says whether it is argument passing, assignment, |
5598 | initialization or return. | |
2f6e4e97 | 5599 | |
81e5eca8 MP |
5600 | LOCATION is the location of the assignment, EXPR_LOC is the location of |
5601 | the RHS or, for a function, location of an argument. | |
2ac2f164 | 5602 | FUNCTION is a tree for the function being called. |
3e4093b6 | 5603 | PARMNUM is the number of the argument, for printing in error messages. */ |
cb3ca04e | 5604 | |
3e4093b6 | 5605 | static tree |
81e5eca8 | 5606 | convert_for_assignment (location_t location, location_t expr_loc, tree type, |
68fca595 | 5607 | tree rhs, tree origtype, enum impl_conv errtype, |
744aa42f ILT |
5608 | bool null_pointer_constant, tree fundecl, |
5609 | tree function, int parmnum) | |
3e4093b6 RS |
5610 | { |
5611 | enum tree_code codel = TREE_CODE (type); | |
8ce94e44 | 5612 | tree orig_rhs = rhs; |
3e4093b6 RS |
5613 | tree rhstype; |
5614 | enum tree_code coder; | |
2ac2f164 | 5615 | tree rname = NULL_TREE; |
58393038 | 5616 | bool objc_ok = false; |
2ac2f164 | 5617 | |
6b4ef5c1 | 5618 | if (errtype == ic_argpass) |
2ac2f164 JM |
5619 | { |
5620 | tree selector; | |
5621 | /* Change pointer to function to the function itself for | |
5622 | diagnostics. */ | |
5623 | if (TREE_CODE (function) == ADDR_EXPR | |
5624 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
5625 | function = TREE_OPERAND (function, 0); | |
5626 | ||
5627 | /* Handle an ObjC selector specially for diagnostics. */ | |
5628 | selector = objc_message_selector (); | |
5629 | rname = function; | |
5630 | if (selector && parmnum > 2) | |
5631 | { | |
5632 | rname = selector; | |
5633 | parmnum -= 2; | |
5634 | } | |
5635 | } | |
5636 | ||
5637 | /* This macro is used to emit diagnostics to ensure that all format | |
5638 | strings are complete sentences, visible to gettext and checked at | |
5639 | compile time. */ | |
5c1bc275 | 5640 | #define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \ |
1e053dfe MLI |
5641 | do { \ |
5642 | switch (errtype) \ | |
5643 | { \ | |
5644 | case ic_argpass: \ | |
5c1bc275 | 5645 | if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \ |
c2255bc4 | 5646 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5c1bc275 | 5647 | ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ |
1e053dfe MLI |
5648 | "expected %qT but argument is of type %qT", \ |
5649 | type, rhstype); \ | |
5650 | break; \ | |
1e053dfe MLI |
5651 | case ic_assign: \ |
5652 | pedwarn (LOCATION, OPT, AS); \ | |
5653 | break; \ | |
5654 | case ic_init: \ | |
6a8f4e12 | 5655 | pedwarn_init (LOCATION, OPT, IN); \ |
1e053dfe MLI |
5656 | break; \ |
5657 | case ic_return: \ | |
d033409e | 5658 | pedwarn (LOCATION, OPT, RE); \ |
1e053dfe MLI |
5659 | break; \ |
5660 | default: \ | |
5661 | gcc_unreachable (); \ | |
5662 | } \ | |
2ac2f164 | 5663 | } while (0) |
cb3ca04e | 5664 | |
49706e39 MLI |
5665 | /* This macro is used to emit diagnostics to ensure that all format |
5666 | strings are complete sentences, visible to gettext and checked at | |
5667 | compile time. It is the same as WARN_FOR_ASSIGNMENT but with an | |
5668 | extra parameter to enumerate qualifiers. */ | |
5669 | ||
5c1bc275 | 5670 | #define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \ |
49706e39 MLI |
5671 | do { \ |
5672 | switch (errtype) \ | |
5673 | { \ | |
5674 | case ic_argpass: \ | |
5c1bc275 | 5675 | if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \ |
49706e39 | 5676 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5c1bc275 | 5677 | ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \ |
49706e39 MLI |
5678 | "expected %qT but argument is of type %qT", \ |
5679 | type, rhstype); \ | |
5680 | break; \ | |
5681 | case ic_assign: \ | |
5c1bc275 | 5682 | pedwarn (LOCATION, OPT, AS, QUALS); \ |
49706e39 MLI |
5683 | break; \ |
5684 | case ic_init: \ | |
5c1bc275 | 5685 | pedwarn (LOCATION, OPT, IN, QUALS); \ |
49706e39 MLI |
5686 | break; \ |
5687 | case ic_return: \ | |
5c1bc275 | 5688 | pedwarn (LOCATION, OPT, RE, QUALS); \ |
49706e39 MLI |
5689 | break; \ |
5690 | default: \ | |
5691 | gcc_unreachable (); \ | |
5692 | } \ | |
5693 | } while (0) | |
5694 | ||
8ce94e44 JM |
5695 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5696 | rhs = TREE_OPERAND (rhs, 0); | |
5697 | ||
3e4093b6 RS |
5698 | rhstype = TREE_TYPE (rhs); |
5699 | coder = TREE_CODE (rhstype); | |
5700 | ||
5701 | if (coder == ERROR_MARK) | |
5702 | return error_mark_node; | |
5703 | ||
58393038 ZL |
5704 | if (c_dialect_objc ()) |
5705 | { | |
5706 | int parmno; | |
5707 | ||
5708 | switch (errtype) | |
5709 | { | |
5710 | case ic_return: | |
5711 | parmno = 0; | |
5712 | break; | |
5713 | ||
5714 | case ic_assign: | |
5715 | parmno = -1; | |
5716 | break; | |
5717 | ||
5718 | case ic_init: | |
5719 | parmno = -2; | |
5720 | break; | |
5721 | ||
5722 | default: | |
5723 | parmno = parmnum; | |
5724 | break; | |
5725 | } | |
5726 | ||
5727 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
5728 | } | |
5729 | ||
bbbbb16a ILT |
5730 | if (warn_cxx_compat) |
5731 | { | |
5732 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
5733 | if (checktype != error_mark_node | |
5734 | && TREE_CODE (type) == ENUMERAL_TYPE | |
5735 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
5736 | { | |
5c1bc275 | 5737 | WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat, |
81f40b79 ILT |
5738 | G_("enum conversion when passing argument " |
5739 | "%d of %qE is invalid in C++"), | |
5740 | G_("enum conversion in assignment is " | |
5741 | "invalid in C++"), | |
5742 | G_("enum conversion in initialization is " | |
5743 | "invalid in C++"), | |
5744 | G_("enum conversion in return is " | |
5745 | "invalid in C++")); | |
bbbbb16a ILT |
5746 | } |
5747 | } | |
5748 | ||
3e4093b6 | 5749 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) |
59c0753d | 5750 | return rhs; |
3e4093b6 RS |
5751 | |
5752 | if (coder == VOID_TYPE) | |
400fbf9f | 5753 | { |
6dcc04b0 JM |
5754 | /* Except for passing an argument to an unprototyped function, |
5755 | this is a constraint violation. When passing an argument to | |
5756 | an unprototyped function, it is compile-time undefined; | |
5757 | making it a constraint in that case was rejected in | |
5758 | DR#252. */ | |
c2255bc4 | 5759 | error_at (location, "void value not ignored as it ought to be"); |
3e4093b6 | 5760 | return error_mark_node; |
400fbf9f | 5761 | } |
808d6eaa JM |
5762 | rhs = require_complete_type (rhs); |
5763 | if (rhs == error_mark_node) | |
5764 | return error_mark_node; | |
cd192ccc MS |
5765 | /* A non-reference type can convert to a reference. This handles |
5766 | va_start, va_copy and possibly port built-ins. */ | |
5767 | if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) | |
400fbf9f | 5768 | { |
3e4093b6 | 5769 | if (!lvalue_p (rhs)) |
400fbf9f | 5770 | { |
c2255bc4 | 5771 | error_at (location, "cannot pass rvalue to reference parameter"); |
3e4093b6 | 5772 | return error_mark_node; |
400fbf9f | 5773 | } |
3e4093b6 RS |
5774 | if (!c_mark_addressable (rhs)) |
5775 | return error_mark_node; | |
5776 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
c2255bc4 | 5777 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5778 | |
81e5eca8 | 5779 | rhs = convert_for_assignment (location, expr_loc, |
68fca595 MP |
5780 | build_pointer_type (TREE_TYPE (type)), |
5781 | rhs, origtype, errtype, | |
5782 | null_pointer_constant, fundecl, function, | |
5783 | parmnum); | |
cd192ccc MS |
5784 | if (rhs == error_mark_node) |
5785 | return error_mark_node; | |
3e4093b6 RS |
5786 | |
5787 | rhs = build1 (NOP_EXPR, type, rhs); | |
c2255bc4 | 5788 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5789 | return rhs; |
400fbf9f | 5790 | } |
3e4093b6 | 5791 | /* Some types can interconvert without explicit casts. */ |
3274deff | 5792 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE |
00c8e9f6 | 5793 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) |
3e4093b6 RS |
5794 | return convert (type, rhs); |
5795 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
5796 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
ab22c1fa | 5797 | || codel == FIXED_POINT_TYPE |
3e4093b6 RS |
5798 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE |
5799 | || codel == BOOLEAN_TYPE) | |
5800 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
ab22c1fa | 5801 | || coder == FIXED_POINT_TYPE |
3e4093b6 RS |
5802 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE |
5803 | || coder == BOOLEAN_TYPE)) | |
928c19bb JM |
5804 | { |
5805 | tree ret; | |
5806 | bool save = in_late_binary_op; | |
dfe776dd | 5807 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) |
928c19bb | 5808 | in_late_binary_op = true; |
81e5eca8 MP |
5809 | ret = convert_and_check (expr_loc != UNKNOWN_LOCATION |
5810 | ? expr_loc : location, type, orig_rhs); | |
dfe776dd | 5811 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) |
928c19bb JM |
5812 | in_late_binary_op = save; |
5813 | return ret; | |
5814 | } | |
400fbf9f | 5815 | |
79077aea JJ |
5816 | /* Aggregates in different TUs might need conversion. */ |
5817 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
5818 | && codel == coder | |
5819 | && comptypes (type, rhstype)) | |
81e5eca8 MP |
5820 | return convert_and_check (expr_loc != UNKNOWN_LOCATION |
5821 | ? expr_loc : location, type, rhs); | |
79077aea | 5822 | |
ebf0bf7f | 5823 | /* Conversion to a transparent union or record from its member types. |
3e4093b6 | 5824 | This applies only to function arguments. */ |
ebf0bf7f JJ |
5825 | if (((codel == UNION_TYPE || codel == RECORD_TYPE) |
5826 | && TYPE_TRANSPARENT_AGGR (type)) | |
6b4ef5c1 | 5827 | && errtype == ic_argpass) |
400fbf9f | 5828 | { |
0257e383 | 5829 | tree memb, marginal_memb = NULL_TREE; |
3e4093b6 | 5830 | |
910ad8de | 5831 | for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) |
400fbf9f | 5832 | { |
0257e383 | 5833 | tree memb_type = TREE_TYPE (memb); |
400fbf9f | 5834 | |
3e4093b6 | 5835 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
132da1a5 | 5836 | TYPE_MAIN_VARIANT (rhstype))) |
3e4093b6 | 5837 | break; |
e58cd767 | 5838 | |
3e4093b6 RS |
5839 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
5840 | continue; | |
2f6e4e97 | 5841 | |
3e4093b6 RS |
5842 | if (coder == POINTER_TYPE) |
5843 | { | |
5844 | tree ttl = TREE_TYPE (memb_type); | |
5845 | tree ttr = TREE_TYPE (rhstype); | |
400fbf9f | 5846 | |
3e4093b6 RS |
5847 | /* Any non-function converts to a [const][volatile] void * |
5848 | and vice versa; otherwise, targets must be the same. | |
5849 | Meanwhile, the lhs target must have all the qualifiers of | |
5850 | the rhs. */ | |
267bac10 JM |
5851 | if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) |
5852 | || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) | |
744aa42f | 5853 | || comp_target_types (location, memb_type, rhstype)) |
3e4093b6 | 5854 | { |
267bac10 JM |
5855 | int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC; |
5856 | int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC; | |
3e4093b6 | 5857 | /* If this type won't generate any warnings, use it. */ |
267bac10 | 5858 | if (lquals == rquals |
3e4093b6 RS |
5859 | || ((TREE_CODE (ttr) == FUNCTION_TYPE |
5860 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
267bac10 JM |
5861 | ? ((lquals | rquals) == rquals) |
5862 | : ((lquals | rquals) == lquals))) | |
3e4093b6 | 5863 | break; |
400fbf9f | 5864 | |
3e4093b6 | 5865 | /* Keep looking for a better type, but remember this one. */ |
0257e383 RH |
5866 | if (!marginal_memb) |
5867 | marginal_memb = memb; | |
3e4093b6 RS |
5868 | } |
5869 | } | |
82bde854 | 5870 | |
3e4093b6 | 5871 | /* Can convert integer zero to any pointer type. */ |
928c19bb | 5872 | if (null_pointer_constant) |
3e4093b6 RS |
5873 | { |
5874 | rhs = null_pointer_node; | |
5875 | break; | |
5876 | } | |
5877 | } | |
400fbf9f | 5878 | |
0257e383 | 5879 | if (memb || marginal_memb) |
3e4093b6 | 5880 | { |
0257e383 | 5881 | if (!memb) |
3e4093b6 RS |
5882 | { |
5883 | /* We have only a marginally acceptable member type; | |
5884 | it needs a warning. */ | |
0257e383 | 5885 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); |
3e4093b6 | 5886 | tree ttr = TREE_TYPE (rhstype); |
714a0864 | 5887 | |
3e4093b6 RS |
5888 | /* Const and volatile mean something different for function |
5889 | types, so the usual warnings are not appropriate. */ | |
5890 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
5891 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5892 | { | |
5893 | /* Because const and volatile on functions are | |
5894 | restrictions that say the function will not do | |
5895 | certain things, it is okay to use a const or volatile | |
5896 | function where an ordinary one is wanted, but not | |
5897 | vice-versa. */ | |
36c5e70a BE |
5898 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5899 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
5c1bc275 MP |
5900 | WARN_FOR_QUALIFIERS (location, expr_loc, |
5901 | OPT_Wdiscarded_qualifiers, | |
509c9d60 | 5902 | G_("passing argument %d of %qE " |
49706e39 | 5903 | "makes %q#v qualified function " |
2ac2f164 | 5904 | "pointer from unqualified"), |
49706e39 | 5905 | G_("assignment makes %q#v qualified " |
2ac2f164 JM |
5906 | "function pointer from " |
5907 | "unqualified"), | |
49706e39 | 5908 | G_("initialization makes %q#v qualified " |
2ac2f164 JM |
5909 | "function pointer from " |
5910 | "unqualified"), | |
49706e39 MLI |
5911 | G_("return makes %q#v qualified function " |
5912 | "pointer from unqualified"), | |
5913 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 | 5914 | } |
36c5e70a BE |
5915 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5916 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
5c1bc275 MP |
5917 | WARN_FOR_QUALIFIERS (location, expr_loc, |
5918 | OPT_Wdiscarded_qualifiers, | |
509c9d60 | 5919 | G_("passing argument %d of %qE discards " |
49706e39 MLI |
5920 | "%qv qualifier from pointer target type"), |
5921 | G_("assignment discards %qv qualifier " | |
2ac2f164 | 5922 | "from pointer target type"), |
49706e39 | 5923 | G_("initialization discards %qv qualifier " |
2ac2f164 | 5924 | "from pointer target type"), |
49706e39 MLI |
5925 | G_("return discards %qv qualifier from " |
5926 | "pointer target type"), | |
5927 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
0257e383 RH |
5928 | |
5929 | memb = marginal_memb; | |
3e4093b6 | 5930 | } |
400fbf9f | 5931 | |
fcf73884 | 5932 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) |
c1771a20 | 5933 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 5934 | "ISO C prohibits argument conversion to union type"); |
0e7c47fa | 5935 | |
db3927fb | 5936 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); |
0257e383 | 5937 | return build_constructor_single (type, memb, rhs); |
3e4093b6 | 5938 | } |
0e7c47fa RK |
5939 | } |
5940 | ||
3e4093b6 RS |
5941 | /* Conversions among pointers */ |
5942 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
5943 | && (coder == codel)) | |
400fbf9f | 5944 | { |
3e4093b6 RS |
5945 | tree ttl = TREE_TYPE (type); |
5946 | tree ttr = TREE_TYPE (rhstype); | |
46df2823 JM |
5947 | tree mvl = ttl; |
5948 | tree mvr = ttr; | |
3e4093b6 | 5949 | bool is_opaque_pointer; |
264fa2db | 5950 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
36c5e70a BE |
5951 | addr_space_t asl; |
5952 | addr_space_t asr; | |
400fbf9f | 5953 | |
46df2823 | 5954 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
267bac10 JM |
5955 | mvl = (TYPE_ATOMIC (mvl) |
5956 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), | |
5957 | TYPE_QUAL_ATOMIC) | |
5958 | : TYPE_MAIN_VARIANT (mvl)); | |
46df2823 | 5959 | if (TREE_CODE (mvr) != ARRAY_TYPE) |
267bac10 JM |
5960 | mvr = (TYPE_ATOMIC (mvr) |
5961 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), | |
5962 | TYPE_QUAL_ATOMIC) | |
5963 | : TYPE_MAIN_VARIANT (mvr)); | |
3e4093b6 | 5964 | /* Opaque pointers are treated like void pointers. */ |
f83c7f63 | 5965 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); |
c22cacf3 | 5966 | |
478a1c5b ILT |
5967 | /* The Plan 9 compiler permits a pointer to a struct to be |
5968 | automatically converted into a pointer to an anonymous field | |
5969 | within the struct. */ | |
5970 | if (flag_plan9_extensions | |
5971 | && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE) | |
5972 | && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE) | |
5973 | && mvl != mvr) | |
5974 | { | |
5975 | tree new_rhs = convert_to_anonymous_field (location, type, rhs); | |
5976 | if (new_rhs != NULL_TREE) | |
5977 | { | |
5978 | rhs = new_rhs; | |
5979 | rhstype = TREE_TYPE (rhs); | |
5980 | coder = TREE_CODE (rhstype); | |
5981 | ttr = TREE_TYPE (rhstype); | |
5982 | mvr = TYPE_MAIN_VARIANT (ttr); | |
5983 | } | |
5984 | } | |
5985 | ||
b7e20b53 | 5986 | /* C++ does not allow the implicit conversion void* -> T*. However, |
c22cacf3 MS |
5987 | for the purpose of reducing the number of false positives, we |
5988 | tolerate the special case of | |
b7e20b53 | 5989 | |
c22cacf3 | 5990 | int *p = NULL; |
b7e20b53 | 5991 | |
c22cacf3 | 5992 | where NULL is typically defined in C to be '(void *) 0'. */ |
c6bdf92e | 5993 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) |
8ffcdea8 MP |
5994 | warning_at (errtype == ic_argpass ? expr_loc : location, |
5995 | OPT_Wc___compat, | |
5996 | "request for implicit conversion " | |
c2255bc4 | 5997 | "from %qT to %qT not permitted in C++", rhstype, type); |
400fbf9f | 5998 | |
36c5e70a BE |
5999 | /* See if the pointers point to incompatible address spaces. */ |
6000 | asl = TYPE_ADDR_SPACE (ttl); | |
6001 | asr = TYPE_ADDR_SPACE (ttr); | |
6002 | if (!null_pointer_constant_p (rhs) | |
6003 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
6004 | { | |
6005 | switch (errtype) | |
6006 | { | |
6007 | case ic_argpass: | |
8ffcdea8 | 6008 | error_at (expr_loc, "passing argument %d of %qE from pointer to " |
36c5e70a BE |
6009 | "non-enclosed address space", parmnum, rname); |
6010 | break; | |
6011 | case ic_assign: | |
6012 | error_at (location, "assignment from pointer to " | |
6013 | "non-enclosed address space"); | |
6014 | break; | |
6015 | case ic_init: | |
6016 | error_at (location, "initialization from pointer to " | |
6017 | "non-enclosed address space"); | |
6018 | break; | |
6019 | case ic_return: | |
6020 | error_at (location, "return from pointer to " | |
6021 | "non-enclosed address space"); | |
6022 | break; | |
6023 | default: | |
6024 | gcc_unreachable (); | |
6025 | } | |
6026 | return error_mark_node; | |
6027 | } | |
6028 | ||
7876a414 KG |
6029 | /* Check if the right-hand side has a format attribute but the |
6030 | left-hand side doesn't. */ | |
90137d8f | 6031 | if (warn_suggest_attribute_format |
104f8784 | 6032 | && check_missing_format_attribute (type, rhstype)) |
c22cacf3 | 6033 | { |
104f8784 KG |
6034 | switch (errtype) |
6035 | { | |
6036 | case ic_argpass: | |
8ffcdea8 | 6037 | warning_at (expr_loc, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6038 | "argument %d of %qE might be " |
6039 | "a candidate for a format attribute", | |
6040 | parmnum, rname); | |
104f8784 KG |
6041 | break; |
6042 | case ic_assign: | |
90137d8f | 6043 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6044 | "assignment left-hand side might be " |
6045 | "a candidate for a format attribute"); | |
104f8784 KG |
6046 | break; |
6047 | case ic_init: | |
90137d8f | 6048 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6049 | "initialization left-hand side might be " |
6050 | "a candidate for a format attribute"); | |
104f8784 KG |
6051 | break; |
6052 | case ic_return: | |
90137d8f | 6053 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
6054 | "return type might be " |
6055 | "a candidate for a format attribute"); | |
104f8784 KG |
6056 | break; |
6057 | default: | |
6058 | gcc_unreachable (); | |
6059 | } | |
7876a414 | 6060 | } |
c22cacf3 | 6061 | |
3e4093b6 RS |
6062 | /* Any non-function converts to a [const][volatile] void * |
6063 | and vice versa; otherwise, targets must be the same. | |
6064 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
267bac10 JM |
6065 | if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) |
6066 | || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) | |
744aa42f | 6067 | || (target_cmp = comp_target_types (location, type, rhstype)) |
3e4093b6 | 6068 | || is_opaque_pointer |
f8a93a2e JJ |
6069 | || ((c_common_unsigned_type (mvl) |
6070 | == c_common_unsigned_type (mvr)) | |
267bac10 JM |
6071 | && (c_common_signed_type (mvl) |
6072 | == c_common_signed_type (mvr)) | |
6073 | && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr))) | |
3e4093b6 RS |
6074 | { |
6075 | if (pedantic | |
6076 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) | |
6077 | || | |
6078 | (VOID_TYPE_P (ttr) | |
928c19bb | 6079 | && !null_pointer_constant |
3e4093b6 | 6080 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
5c1bc275 | 6081 | WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic, |
509c9d60 | 6082 | G_("ISO C forbids passing argument %d of " |
2ac2f164 JM |
6083 | "%qE between function pointer " |
6084 | "and %<void *%>"), | |
4b794eaf | 6085 | G_("ISO C forbids assignment between " |
2ac2f164 | 6086 | "function pointer and %<void *%>"), |
4b794eaf | 6087 | G_("ISO C forbids initialization between " |
2ac2f164 | 6088 | "function pointer and %<void *%>"), |
4b794eaf | 6089 | G_("ISO C forbids return between function " |
2ac2f164 | 6090 | "pointer and %<void *%>")); |
3e4093b6 RS |
6091 | /* Const and volatile mean something different for function types, |
6092 | so the usual warnings are not appropriate. */ | |
6093 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
6094 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
6095 | { | |
267bac10 JM |
6096 | /* Assignments between atomic and non-atomic objects are OK. */ |
6097 | if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) | |
6098 | & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) | |
58393038 | 6099 | { |
5c1bc275 MP |
6100 | WARN_FOR_QUALIFIERS (location, expr_loc, |
6101 | OPT_Wdiscarded_qualifiers, | |
6c39e757 NP |
6102 | G_("passing argument %d of %qE discards " |
6103 | "%qv qualifier from pointer target type"), | |
6104 | G_("assignment discards %qv qualifier " | |
6105 | "from pointer target type"), | |
6106 | G_("initialization discards %qv qualifier " | |
6107 | "from pointer target type"), | |
6108 | G_("return discards %qv qualifier from " | |
6109 | "pointer target type"), | |
6110 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
58393038 | 6111 | } |
3e4093b6 RS |
6112 | /* If this is not a case of ignoring a mismatch in signedness, |
6113 | no warning. */ | |
6114 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 6115 | || target_cmp) |
3e4093b6 RS |
6116 | ; |
6117 | /* If there is a mismatch, do warn. */ | |
f2fd3821 | 6118 | else if (warn_pointer_sign) |
5c1bc275 | 6119 | WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign, |
509c9d60 | 6120 | G_("pointer targets in passing argument " |
2ac2f164 | 6121 | "%d of %qE differ in signedness"), |
4b794eaf | 6122 | G_("pointer targets in assignment " |
2ac2f164 | 6123 | "differ in signedness"), |
4b794eaf | 6124 | G_("pointer targets in initialization " |
2ac2f164 | 6125 | "differ in signedness"), |
4b794eaf | 6126 | G_("pointer targets in return differ " |
2ac2f164 | 6127 | "in signedness")); |
3e4093b6 RS |
6128 | } |
6129 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
6130 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
6131 | { | |
6132 | /* Because const and volatile on functions are restrictions | |
6133 | that say the function will not do certain things, | |
6134 | it is okay to use a const or volatile function | |
6135 | where an ordinary one is wanted, but not vice-versa. */ | |
36c5e70a BE |
6136 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
6137 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
5c1bc275 MP |
6138 | WARN_FOR_QUALIFIERS (location, expr_loc, |
6139 | OPT_Wdiscarded_qualifiers, | |
509c9d60 | 6140 | G_("passing argument %d of %qE makes " |
49706e39 | 6141 | "%q#v qualified function pointer " |
2ac2f164 | 6142 | "from unqualified"), |
49706e39 | 6143 | G_("assignment makes %q#v qualified function " |
2ac2f164 | 6144 | "pointer from unqualified"), |
49706e39 | 6145 | G_("initialization makes %q#v qualified " |
2ac2f164 | 6146 | "function pointer from unqualified"), |
49706e39 MLI |
6147 | G_("return makes %q#v qualified function " |
6148 | "pointer from unqualified"), | |
6149 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 RS |
6150 | } |
6151 | } | |
6152 | else | |
58393038 ZL |
6153 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
6154 | if (!objc_ok) | |
d5c3d343 MP |
6155 | WARN_FOR_ASSIGNMENT (location, expr_loc, |
6156 | OPT_Wincompatible_pointer_types, | |
509c9d60 | 6157 | G_("passing argument %d of %qE from " |
58393038 | 6158 | "incompatible pointer type"), |
4b794eaf JJ |
6159 | G_("assignment from incompatible pointer type"), |
6160 | G_("initialization from incompatible " | |
58393038 | 6161 | "pointer type"), |
4b794eaf | 6162 | G_("return from incompatible pointer type")); |
58393038 | 6163 | |
3e4093b6 RS |
6164 | return convert (type, rhs); |
6165 | } | |
b494fd98 EB |
6166 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
6167 | { | |
6dcc04b0 JM |
6168 | /* ??? This should not be an error when inlining calls to |
6169 | unprototyped functions. */ | |
c2255bc4 | 6170 | error_at (location, "invalid use of non-lvalue array"); |
b494fd98 EB |
6171 | return error_mark_node; |
6172 | } | |
3e4093b6 | 6173 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
400fbf9f | 6174 | { |
3e4093b6 RS |
6175 | /* An explicit constant 0 can convert to a pointer, |
6176 | or one that results from arithmetic, even including | |
6177 | a cast to integer type. */ | |
928c19bb | 6178 | if (!null_pointer_constant) |
52ec0ea3 MP |
6179 | WARN_FOR_ASSIGNMENT (location, expr_loc, |
6180 | OPT_Wint_conversion, | |
509c9d60 | 6181 | G_("passing argument %d of %qE makes " |
2ac2f164 | 6182 | "pointer from integer without a cast"), |
4b794eaf | 6183 | G_("assignment makes pointer from integer " |
2ac2f164 | 6184 | "without a cast"), |
4b794eaf | 6185 | G_("initialization makes pointer from " |
2ac2f164 | 6186 | "integer without a cast"), |
4b794eaf | 6187 | G_("return makes pointer from integer " |
2ac2f164 | 6188 | "without a cast")); |
b3006337 EB |
6189 | |
6190 | return convert (type, rhs); | |
400fbf9f | 6191 | } |
3e4093b6 | 6192 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
400fbf9f | 6193 | { |
52ec0ea3 MP |
6194 | WARN_FOR_ASSIGNMENT (location, expr_loc, |
6195 | OPT_Wint_conversion, | |
509c9d60 | 6196 | G_("passing argument %d of %qE makes integer " |
2ac2f164 | 6197 | "from pointer without a cast"), |
4b794eaf | 6198 | G_("assignment makes integer from pointer " |
2ac2f164 | 6199 | "without a cast"), |
4b794eaf | 6200 | G_("initialization makes integer from pointer " |
2ac2f164 | 6201 | "without a cast"), |
4b794eaf | 6202 | G_("return makes integer from pointer " |
2ac2f164 | 6203 | "without a cast")); |
3e4093b6 RS |
6204 | return convert (type, rhs); |
6205 | } | |
6206 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
928c19bb JM |
6207 | { |
6208 | tree ret; | |
6209 | bool save = in_late_binary_op; | |
6210 | in_late_binary_op = true; | |
6211 | ret = convert (type, rhs); | |
6212 | in_late_binary_op = save; | |
6213 | return ret; | |
6214 | } | |
400fbf9f | 6215 | |
2ac2f164 | 6216 | switch (errtype) |
3e4093b6 | 6217 | { |
2ac2f164 | 6218 | case ic_argpass: |
8ffcdea8 MP |
6219 | error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum, |
6220 | rname); | |
a7da8b42 | 6221 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) |
8ffcdea8 | 6222 | ? DECL_SOURCE_LOCATION (fundecl) : expr_loc, |
a7da8b42 | 6223 | "expected %qT but argument is of type %qT", type, rhstype); |
2ac2f164 JM |
6224 | break; |
6225 | case ic_assign: | |
c2255bc4 AH |
6226 | error_at (location, "incompatible types when assigning to type %qT from " |
6227 | "type %qT", type, rhstype); | |
2ac2f164 JM |
6228 | break; |
6229 | case ic_init: | |
c2255bc4 | 6230 | error_at (location, |
8ffcdea8 | 6231 | "incompatible types when initializing type %qT using type %qT", |
c2255bc4 | 6232 | type, rhstype); |
2ac2f164 JM |
6233 | break; |
6234 | case ic_return: | |
c2255bc4 | 6235 | error_at (location, |
8ffcdea8 | 6236 | "incompatible types when returning type %qT but %qT was " |
c2255bc4 | 6237 | "expected", rhstype, type); |
2ac2f164 JM |
6238 | break; |
6239 | default: | |
6240 | gcc_unreachable (); | |
400fbf9f | 6241 | } |
53b01f59 | 6242 | |
3e4093b6 RS |
6243 | return error_mark_node; |
6244 | } | |
3e4093b6 RS |
6245 | \f |
6246 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
6247 | return its value. Otherwise, return error_mark_node. | |
15b732b2 | 6248 | |
3e4093b6 RS |
6249 | This is for handling COMPOUND_EXPRs as initializer elements |
6250 | which is allowed with a warning when -pedantic is specified. */ | |
15b732b2 | 6251 | |
3e4093b6 RS |
6252 | static tree |
6253 | valid_compound_expr_initializer (tree value, tree endtype) | |
6254 | { | |
6255 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
6256 | { | |
6257 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
6258 | == error_mark_node) | |
6259 | return error_mark_node; | |
6260 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
6261 | endtype); | |
6262 | } | |
116df786 | 6263 | else if (!initializer_constant_valid_p (value, endtype)) |
3e4093b6 RS |
6264 | return error_mark_node; |
6265 | else | |
6266 | return value; | |
15b732b2 | 6267 | } |
400fbf9f | 6268 | \f |
3e4093b6 RS |
6269 | /* Perform appropriate conversions on the initial value of a variable, |
6270 | store it in the declaration DECL, | |
6271 | and print any error messages that are appropriate. | |
bbbbb16a | 6272 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
c2255bc4 AH |
6273 | If the init is invalid, store an ERROR_MARK. |
6274 | ||
6275 | INIT_LOC is the location of the initial value. */ | |
400fbf9f | 6276 | |
3e4093b6 | 6277 | void |
c2255bc4 | 6278 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) |
400fbf9f | 6279 | { |
3e4093b6 | 6280 | tree value, type; |
928c19bb | 6281 | bool npc = false; |
400fbf9f | 6282 | |
3e4093b6 | 6283 | /* If variable's type was invalidly declared, just ignore it. */ |
400fbf9f | 6284 | |
3e4093b6 RS |
6285 | type = TREE_TYPE (decl); |
6286 | if (TREE_CODE (type) == ERROR_MARK) | |
6287 | return; | |
400fbf9f | 6288 | |
3e4093b6 | 6289 | /* Digest the specified initializer into an expression. */ |
400fbf9f | 6290 | |
928c19bb JM |
6291 | if (init) |
6292 | npc = null_pointer_constant_p (init); | |
c2255bc4 AH |
6293 | value = digest_init (init_loc, type, init, origtype, npc, |
6294 | true, TREE_STATIC (decl)); | |
400fbf9f | 6295 | |
3e4093b6 | 6296 | /* Store the expression if valid; else report error. */ |
400fbf9f | 6297 | |
8400e75e | 6298 | if (!in_system_header_at (input_location) |
3f75a254 | 6299 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) |
3176a0c2 DD |
6300 | warning (OPT_Wtraditional, "traditional C rejects automatic " |
6301 | "aggregate initialization"); | |
2f6e4e97 | 6302 | |
3e4093b6 | 6303 | DECL_INITIAL (decl) = value; |
400fbf9f | 6304 | |
3e4093b6 RS |
6305 | /* ANSI wants warnings about out-of-range constant initializers. */ |
6306 | STRIP_TYPE_NOPS (value); | |
b8698a0f | 6307 | if (TREE_STATIC (decl)) |
c2658540 | 6308 | constant_expression_warning (value); |
400fbf9f | 6309 | |
3e4093b6 RS |
6310 | /* Check if we need to set array size from compound literal size. */ |
6311 | if (TREE_CODE (type) == ARRAY_TYPE | |
6312 | && TYPE_DOMAIN (type) == 0 | |
6313 | && value != error_mark_node) | |
400fbf9f | 6314 | { |
3e4093b6 RS |
6315 | tree inside_init = init; |
6316 | ||
ed248cf7 | 6317 | STRIP_TYPE_NOPS (inside_init); |
3e4093b6 RS |
6318 | inside_init = fold (inside_init); |
6319 | ||
6320 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
6321 | { | |
8d9f82d5 | 6322 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
3e4093b6 | 6323 | |
8d9f82d5 | 6324 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) |
3e4093b6 RS |
6325 | { |
6326 | /* For int foo[] = (int [3]){1}; we need to set array size | |
6327 | now since later on array initializer will be just the | |
6328 | brace enclosed list of the compound literal. */ | |
e30ecc5d | 6329 | tree etype = strip_array_types (TREE_TYPE (decl)); |
8d9f82d5 | 6330 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); |
8d9f82d5 | 6331 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); |
3e4093b6 | 6332 | layout_type (type); |
8d9f82d5 | 6333 | layout_decl (cldecl, 0); |
e30ecc5d JJ |
6334 | TREE_TYPE (decl) |
6335 | = c_build_qualified_type (type, TYPE_QUALS (etype)); | |
3e4093b6 RS |
6336 | } |
6337 | } | |
400fbf9f | 6338 | } |
3e4093b6 RS |
6339 | } |
6340 | \f | |
6341 | /* Methods for storing and printing names for error messages. */ | |
400fbf9f | 6342 | |
3e4093b6 RS |
6343 | /* Implement a spelling stack that allows components of a name to be pushed |
6344 | and popped. Each element on the stack is this structure. */ | |
400fbf9f | 6345 | |
3e4093b6 RS |
6346 | struct spelling |
6347 | { | |
6348 | int kind; | |
6349 | union | |
400fbf9f | 6350 | { |
a0f0ab9f | 6351 | unsigned HOST_WIDE_INT i; |
3e4093b6 RS |
6352 | const char *s; |
6353 | } u; | |
6354 | }; | |
2f6e4e97 | 6355 | |
3e4093b6 RS |
6356 | #define SPELLING_STRING 1 |
6357 | #define SPELLING_MEMBER 2 | |
6358 | #define SPELLING_BOUNDS 3 | |
400fbf9f | 6359 | |
3e4093b6 RS |
6360 | static struct spelling *spelling; /* Next stack element (unused). */ |
6361 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
6362 | static int spelling_size; /* Size of the spelling stack. */ | |
400fbf9f | 6363 | |
3e4093b6 RS |
6364 | /* Macros to save and restore the spelling stack around push_... functions. |
6365 | Alternative to SAVE_SPELLING_STACK. */ | |
400fbf9f | 6366 | |
3e4093b6 RS |
6367 | #define SPELLING_DEPTH() (spelling - spelling_base) |
6368 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
400fbf9f | 6369 | |
3e4093b6 RS |
6370 | /* Push an element on the spelling stack with type KIND and assign VALUE |
6371 | to MEMBER. */ | |
400fbf9f | 6372 | |
3e4093b6 RS |
6373 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
6374 | { \ | |
6375 | int depth = SPELLING_DEPTH (); \ | |
6376 | \ | |
6377 | if (depth >= spelling_size) \ | |
6378 | { \ | |
6379 | spelling_size += 10; \ | |
cca8ead2 BI |
6380 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
6381 | spelling_size); \ | |
3e4093b6 RS |
6382 | RESTORE_SPELLING_DEPTH (depth); \ |
6383 | } \ | |
6384 | \ | |
6385 | spelling->kind = (KIND); \ | |
6386 | spelling->MEMBER = (VALUE); \ | |
6387 | spelling++; \ | |
6388 | } | |
400fbf9f | 6389 | |
3e4093b6 | 6390 | /* Push STRING on the stack. Printed literally. */ |
400fbf9f | 6391 | |
3e4093b6 RS |
6392 | static void |
6393 | push_string (const char *string) | |
6394 | { | |
6395 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
6396 | } | |
400fbf9f | 6397 | |
3e4093b6 | 6398 | /* Push a member name on the stack. Printed as '.' STRING. */ |
400fbf9f | 6399 | |
3e4093b6 RS |
6400 | static void |
6401 | push_member_name (tree decl) | |
6402 | { | |
6403 | const char *const string | |
88388a52 JM |
6404 | = (DECL_NAME (decl) |
6405 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
6406 | : _("<anonymous>")); | |
3e4093b6 RS |
6407 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
6408 | } | |
400fbf9f | 6409 | |
3e4093b6 | 6410 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
400fbf9f | 6411 | |
3e4093b6 | 6412 | static void |
a0f0ab9f | 6413 | push_array_bounds (unsigned HOST_WIDE_INT bounds) |
3e4093b6 RS |
6414 | { |
6415 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
6416 | } | |
bb58bec5 | 6417 | |
3e4093b6 | 6418 | /* Compute the maximum size in bytes of the printed spelling. */ |
400fbf9f | 6419 | |
3e4093b6 RS |
6420 | static int |
6421 | spelling_length (void) | |
6422 | { | |
6423 | int size = 0; | |
6424 | struct spelling *p; | |
400fbf9f | 6425 | |
3e4093b6 RS |
6426 | for (p = spelling_base; p < spelling; p++) |
6427 | { | |
6428 | if (p->kind == SPELLING_BOUNDS) | |
6429 | size += 25; | |
6430 | else | |
6431 | size += strlen (p->u.s) + 1; | |
6432 | } | |
6433 | ||
6434 | return size; | |
400fbf9f | 6435 | } |
400fbf9f | 6436 | |
3e4093b6 | 6437 | /* Print the spelling to BUFFER and return it. */ |
400fbf9f | 6438 | |
3e4093b6 RS |
6439 | static char * |
6440 | print_spelling (char *buffer) | |
400fbf9f | 6441 | { |
3e4093b6 RS |
6442 | char *d = buffer; |
6443 | struct spelling *p; | |
400fbf9f | 6444 | |
3e4093b6 RS |
6445 | for (p = spelling_base; p < spelling; p++) |
6446 | if (p->kind == SPELLING_BOUNDS) | |
6447 | { | |
a0f0ab9f | 6448 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); |
3e4093b6 RS |
6449 | d += strlen (d); |
6450 | } | |
6451 | else | |
6452 | { | |
6453 | const char *s; | |
6454 | if (p->kind == SPELLING_MEMBER) | |
6455 | *d++ = '.'; | |
6456 | for (s = p->u.s; (*d = *s++); d++) | |
6457 | ; | |
6458 | } | |
6459 | *d++ = '\0'; | |
6460 | return buffer; | |
6461 | } | |
400fbf9f | 6462 | |
3e4093b6 RS |
6463 | /* Digest the parser output INIT as an initializer for type TYPE. |
6464 | Return a C expression of type TYPE to represent the initial value. | |
7e842ef8 | 6465 | |
bbbbb16a ILT |
6466 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
6467 | ||
928c19bb JM |
6468 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. |
6469 | ||
916c5919 JM |
6470 | If INIT is a string constant, STRICT_STRING is true if it is |
6471 | unparenthesized or we should not warn here for it being parenthesized. | |
6472 | For other types of INIT, STRICT_STRING is not used. | |
6473 | ||
c2255bc4 AH |
6474 | INIT_LOC is the location of the INIT. |
6475 | ||
3e4093b6 RS |
6476 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
6477 | elements are seen. */ | |
7e842ef8 | 6478 | |
3e4093b6 | 6479 | static tree |
c2255bc4 AH |
6480 | digest_init (location_t init_loc, tree type, tree init, tree origtype, |
6481 | bool null_pointer_constant, bool strict_string, | |
6482 | int require_constant) | |
3e4093b6 RS |
6483 | { |
6484 | enum tree_code code = TREE_CODE (type); | |
6485 | tree inside_init = init; | |
8ce94e44 | 6486 | tree semantic_type = NULL_TREE; |
928c19bb | 6487 | bool maybe_const = true; |
7e842ef8 | 6488 | |
3e4093b6 | 6489 | if (type == error_mark_node |
f01da1a5 | 6490 | || !init |
3e4093b6 RS |
6491 | || init == error_mark_node |
6492 | || TREE_TYPE (init) == error_mark_node) | |
6493 | return error_mark_node; | |
7e842ef8 | 6494 | |
ed248cf7 | 6495 | STRIP_TYPE_NOPS (inside_init); |
7e842ef8 | 6496 | |
8ce94e44 JM |
6497 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) |
6498 | { | |
6499 | semantic_type = TREE_TYPE (inside_init); | |
6500 | inside_init = TREE_OPERAND (inside_init, 0); | |
6501 | } | |
928c19bb JM |
6502 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); |
6503 | inside_init = decl_constant_value_for_optimization (inside_init); | |
7e842ef8 | 6504 | |
3e4093b6 RS |
6505 | /* Initialization of an array of chars from a string constant |
6506 | optionally enclosed in braces. */ | |
7e842ef8 | 6507 | |
197463ae JM |
6508 | if (code == ARRAY_TYPE && inside_init |
6509 | && TREE_CODE (inside_init) == STRING_CST) | |
3e4093b6 | 6510 | { |
267bac10 JM |
6511 | tree typ1 |
6512 | = (TYPE_ATOMIC (TREE_TYPE (type)) | |
6513 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)), | |
6514 | TYPE_QUAL_ATOMIC) | |
6515 | : TYPE_MAIN_VARIANT (TREE_TYPE (type))); | |
197463ae JM |
6516 | /* Note that an array could be both an array of character type |
6517 | and an array of wchar_t if wchar_t is signed char or unsigned | |
6518 | char. */ | |
6519 | bool char_array = (typ1 == char_type_node | |
6520 | || typ1 == signed_char_type_node | |
6521 | || typ1 == unsigned_char_type_node); | |
6522 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
c466b2cd KVH |
6523 | bool char16_array = !!comptypes (typ1, char16_type_node); |
6524 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
6525 | ||
6526 | if (char_array || wchar_array || char16_array || char32_array) | |
7e842ef8 | 6527 | { |
916c5919 | 6528 | struct c_expr expr; |
c466b2cd | 6529 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); |
916c5919 JM |
6530 | expr.value = inside_init; |
6531 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
6866c6e8 | 6532 | expr.original_type = NULL; |
d033409e | 6533 | maybe_warn_string_init (init_loc, type, expr); |
916c5919 | 6534 | |
a45e580b | 6535 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
c1771a20 | 6536 | pedwarn_init (init_loc, OPT_Wpedantic, |
a45e580b JM |
6537 | "initialization of a flexible array member"); |
6538 | ||
3e4093b6 | 6539 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 6540 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 | 6541 | return inside_init; |
7e842ef8 | 6542 | |
c466b2cd | 6543 | if (char_array) |
3e4093b6 | 6544 | { |
c466b2cd KVH |
6545 | if (typ2 != char_type_node) |
6546 | { | |
ea58ef42 MP |
6547 | error_init (init_loc, "char-array initialized from wide " |
6548 | "string"); | |
c466b2cd KVH |
6549 | return error_mark_node; |
6550 | } | |
3e4093b6 | 6551 | } |
c466b2cd | 6552 | else |
3e4093b6 | 6553 | { |
c466b2cd KVH |
6554 | if (typ2 == char_type_node) |
6555 | { | |
ea58ef42 MP |
6556 | error_init (init_loc, "wide character array initialized " |
6557 | "from non-wide string"); | |
c466b2cd KVH |
6558 | return error_mark_node; |
6559 | } | |
6560 | else if (!comptypes(typ1, typ2)) | |
6561 | { | |
ea58ef42 MP |
6562 | error_init (init_loc, "wide character array initialized " |
6563 | "from incompatible wide string"); | |
c466b2cd KVH |
6564 | return error_mark_node; |
6565 | } | |
7e842ef8 | 6566 | } |
2f6e4e97 | 6567 | |
3e4093b6 RS |
6568 | TREE_TYPE (inside_init) = type; |
6569 | if (TYPE_DOMAIN (type) != 0 | |
6570 | && TYPE_SIZE (type) != 0 | |
5eb4df45 ILT |
6571 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
6572 | { | |
6573 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
6574 | ||
c466b2cd | 6575 | /* Subtract the size of a single (possibly wide) character |
3e4093b6 RS |
6576 | because it's ok to ignore the terminating null char |
6577 | that is counted in the length of the constant. */ | |
5eb4df45 ILT |
6578 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), |
6579 | (len | |
6580 | - (TYPE_PRECISION (typ1) | |
6581 | / BITS_PER_UNIT)))) | |
6582 | pedwarn_init (init_loc, 0, | |
6583 | ("initializer-string for array of chars " | |
6584 | "is too long")); | |
6585 | else if (warn_cxx_compat | |
6586 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
6587 | warning_at (init_loc, OPT_Wc___compat, | |
6588 | ("initializer-string for array chars " | |
6589 | "is too long for C++")); | |
6590 | } | |
7e842ef8 | 6591 | |
3e4093b6 | 6592 | return inside_init; |
7e842ef8 | 6593 | } |
197463ae JM |
6594 | else if (INTEGRAL_TYPE_P (typ1)) |
6595 | { | |
ea58ef42 | 6596 | error_init (init_loc, "array of inappropriate type initialized " |
197463ae JM |
6597 | "from string constant"); |
6598 | return error_mark_node; | |
6599 | } | |
7e842ef8 PE |
6600 | } |
6601 | ||
3e4093b6 RS |
6602 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
6603 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
6604 | below and handle as a constructor. */ | |
e89be13b JJ |
6605 | if (code == VECTOR_TYPE |
6606 | && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE | |
00c8e9f6 | 6607 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) |
e89be13b JJ |
6608 | && TREE_CONSTANT (inside_init)) |
6609 | { | |
6610 | if (TREE_CODE (inside_init) == VECTOR_CST | |
6611 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6612 | TYPE_MAIN_VARIANT (type))) | |
6613 | return inside_init; | |
6614 | ||
6615 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
6616 | { | |
4038c495 GB |
6617 | unsigned HOST_WIDE_INT ix; |
6618 | tree value; | |
6619 | bool constant_p = true; | |
e89be13b JJ |
6620 | |
6621 | /* Iterate through elements and check if all constructor | |
6622 | elements are *_CSTs. */ | |
4038c495 GB |
6623 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) |
6624 | if (!CONSTANT_CLASS_P (value)) | |
6625 | { | |
6626 | constant_p = false; | |
6627 | break; | |
6628 | } | |
e89be13b | 6629 | |
4038c495 GB |
6630 | if (constant_p) |
6631 | return build_vector_from_ctor (type, | |
6632 | CONSTRUCTOR_ELTS (inside_init)); | |
e89be13b JJ |
6633 | } |
6634 | } | |
6035d635 | 6635 | |
ca085fd7 MLI |
6636 | if (warn_sequence_point) |
6637 | verify_sequence_points (inside_init); | |
6638 | ||
3e4093b6 RS |
6639 | /* Any type can be initialized |
6640 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 6641 | |
3e4093b6 RS |
6642 | if (inside_init && TREE_TYPE (inside_init) != 0 |
6643 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
132da1a5 | 6644 | TYPE_MAIN_VARIANT (type)) |
3e4093b6 | 6645 | || (code == ARRAY_TYPE |
132da1a5 | 6646 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6647 | || (code == VECTOR_TYPE |
132da1a5 | 6648 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6649 | || (code == POINTER_TYPE |
3897f229 | 6650 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
3e4093b6 | 6651 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
132da1a5 | 6652 | TREE_TYPE (type))))) |
3e4093b6 RS |
6653 | { |
6654 | if (code == POINTER_TYPE) | |
b494fd98 | 6655 | { |
b494fd98 EB |
6656 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) |
6657 | { | |
f2a71bbc JM |
6658 | if (TREE_CODE (inside_init) == STRING_CST |
6659 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
c2255bc4 AH |
6660 | inside_init = array_to_pointer_conversion |
6661 | (init_loc, inside_init); | |
f2a71bbc JM |
6662 | else |
6663 | { | |
ea58ef42 | 6664 | error_init (init_loc, "invalid use of non-lvalue array"); |
f2a71bbc JM |
6665 | return error_mark_node; |
6666 | } | |
b494fd98 | 6667 | } |
f2a71bbc | 6668 | } |
b494fd98 | 6669 | |
bae39a73 NS |
6670 | if (code == VECTOR_TYPE) |
6671 | /* Although the types are compatible, we may require a | |
6672 | conversion. */ | |
6673 | inside_init = convert (type, inside_init); | |
3e4093b6 | 6674 | |
ca58211b PB |
6675 | if (require_constant |
6676 | && (code == VECTOR_TYPE || !flag_isoc99) | |
3e4093b6 | 6677 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) |
400fbf9f | 6678 | { |
3e4093b6 RS |
6679 | /* As an extension, allow initializing objects with static storage |
6680 | duration with compound literals (which are then treated just as | |
ca58211b PB |
6681 | the brace enclosed list they contain). Also allow this for |
6682 | vectors, as we can only assign them with compound literals. */ | |
3e4093b6 RS |
6683 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
6684 | inside_init = DECL_INITIAL (decl); | |
400fbf9f | 6685 | } |
3e4093b6 RS |
6686 | |
6687 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
6688 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 6689 | { |
ea58ef42 MP |
6690 | error_init (init_loc, "array initialized from non-constant array " |
6691 | "expression"); | |
3e4093b6 | 6692 | return error_mark_node; |
400fbf9f | 6693 | } |
400fbf9f | 6694 | |
c1771a20 | 6695 | /* Compound expressions can only occur here if -Wpedantic or |
3e4093b6 RS |
6696 | -pedantic-errors is specified. In the later case, we always want |
6697 | an error. In the former case, we simply want a warning. */ | |
6698 | if (require_constant && pedantic | |
6699 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
6700 | { | |
6701 | inside_init | |
6702 | = valid_compound_expr_initializer (inside_init, | |
6703 | TREE_TYPE (inside_init)); | |
6704 | if (inside_init == error_mark_node) | |
ea58ef42 | 6705 | error_init (init_loc, "initializer element is not constant"); |
2f6e4e97 | 6706 | else |
c1771a20 | 6707 | pedwarn_init (init_loc, OPT_Wpedantic, |
509c9d60 | 6708 | "initializer element is not constant"); |
3e4093b6 RS |
6709 | if (flag_pedantic_errors) |
6710 | inside_init = error_mark_node; | |
6711 | } | |
6712 | else if (require_constant | |
116df786 RH |
6713 | && !initializer_constant_valid_p (inside_init, |
6714 | TREE_TYPE (inside_init))) | |
3e4093b6 | 6715 | { |
ea58ef42 | 6716 | error_init (init_loc, "initializer element is not constant"); |
3e4093b6 | 6717 | inside_init = error_mark_node; |
8b40563c | 6718 | } |
928c19bb | 6719 | else if (require_constant && !maybe_const) |
c2255bc4 | 6720 | pedwarn_init (init_loc, 0, |
928c19bb | 6721 | "initializer element is not a constant expression"); |
f735a153 | 6722 | |
90137d8f | 6723 | /* Added to enable additional -Wsuggest-attribute=format warnings. */ |
8fcef540 | 6724 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) |
68fca595 MP |
6725 | inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION, |
6726 | type, inside_init, origtype, | |
bbbbb16a | 6727 | ic_init, null_pointer_constant, |
928c19bb | 6728 | NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
6729 | return inside_init; |
6730 | } | |
f735a153 | 6731 | |
3e4093b6 | 6732 | /* Handle scalar types, including conversions. */ |
400fbf9f | 6733 | |
ab22c1fa CF |
6734 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE |
6735 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
6736 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
400fbf9f | 6737 | { |
f2a71bbc JM |
6738 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE |
6739 | && (TREE_CODE (init) == STRING_CST | |
6740 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
c2255bc4 | 6741 | inside_init = init = array_to_pointer_conversion (init_loc, init); |
8ce94e44 JM |
6742 | if (semantic_type) |
6743 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
6744 | inside_init); | |
3e4093b6 | 6745 | inside_init |
68fca595 MP |
6746 | = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type, |
6747 | inside_init, origtype, ic_init, | |
6748 | null_pointer_constant, NULL_TREE, NULL_TREE, | |
6749 | 0); | |
2f6e4e97 | 6750 | |
3274deff JW |
6751 | /* Check to see if we have already given an error message. */ |
6752 | if (inside_init == error_mark_node) | |
6753 | ; | |
3f75a254 | 6754 | else if (require_constant && !TREE_CONSTANT (inside_init)) |
400fbf9f | 6755 | { |
ea58ef42 | 6756 | error_init (init_loc, "initializer element is not constant"); |
3e4093b6 | 6757 | inside_init = error_mark_node; |
400fbf9f | 6758 | } |
3e4093b6 | 6759 | else if (require_constant |
116df786 RH |
6760 | && !initializer_constant_valid_p (inside_init, |
6761 | TREE_TYPE (inside_init))) | |
400fbf9f | 6762 | { |
ea58ef42 MP |
6763 | error_init (init_loc, "initializer element is not computable at " |
6764 | "load time"); | |
3e4093b6 | 6765 | inside_init = error_mark_node; |
400fbf9f | 6766 | } |
928c19bb | 6767 | else if (require_constant && !maybe_const) |
c2255bc4 | 6768 | pedwarn_init (init_loc, 0, |
928c19bb | 6769 | "initializer element is not a constant expression"); |
3e4093b6 RS |
6770 | |
6771 | return inside_init; | |
400fbf9f | 6772 | } |
d9fc6069 | 6773 | |
3e4093b6 | 6774 | /* Come here only for records and arrays. */ |
d9fc6069 | 6775 | |
3e4093b6 | 6776 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
d9fc6069 | 6777 | { |
ea58ef42 | 6778 | error_init (init_loc, "variable-sized object may not be initialized"); |
3e4093b6 | 6779 | return error_mark_node; |
d9fc6069 | 6780 | } |
3e4093b6 | 6781 | |
ea58ef42 | 6782 | error_init (init_loc, "invalid initializer"); |
3e4093b6 | 6783 | return error_mark_node; |
d9fc6069 | 6784 | } |
400fbf9f | 6785 | \f |
3e4093b6 | 6786 | /* Handle initializers that use braces. */ |
400fbf9f | 6787 | |
3e4093b6 RS |
6788 | /* Type of object we are accumulating a constructor for. |
6789 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
6790 | static tree constructor_type; | |
400fbf9f | 6791 | |
3e4093b6 RS |
6792 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
6793 | left to fill. */ | |
6794 | static tree constructor_fields; | |
400fbf9f | 6795 | |
3e4093b6 RS |
6796 | /* For an ARRAY_TYPE, this is the specified index |
6797 | at which to store the next element we get. */ | |
6798 | static tree constructor_index; | |
400fbf9f | 6799 | |
3e4093b6 RS |
6800 | /* For an ARRAY_TYPE, this is the maximum index. */ |
6801 | static tree constructor_max_index; | |
400fbf9f | 6802 | |
3e4093b6 RS |
6803 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
6804 | static tree constructor_unfilled_fields; | |
400fbf9f | 6805 | |
3e4093b6 RS |
6806 | /* For an ARRAY_TYPE, this is the index of the first element |
6807 | not yet written out. */ | |
6808 | static tree constructor_unfilled_index; | |
895ea614 | 6809 | |
3e4093b6 RS |
6810 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
6811 | This is so we can generate gaps between fields, when appropriate. */ | |
6812 | static tree constructor_bit_index; | |
10d5caec | 6813 | |
3e4093b6 RS |
6814 | /* If we are saving up the elements rather than allocating them, |
6815 | this is the list of elements so far (in reverse order, | |
6816 | most recent first). */ | |
9771b263 | 6817 | static vec<constructor_elt, va_gc> *constructor_elements; |
ad47f1e5 | 6818 | |
3e4093b6 RS |
6819 | /* 1 if constructor should be incrementally stored into a constructor chain, |
6820 | 0 if all the elements should be kept in AVL tree. */ | |
6821 | static int constructor_incremental; | |
ad47f1e5 | 6822 | |
3e4093b6 RS |
6823 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
6824 | static int constructor_constant; | |
ad47f1e5 | 6825 | |
3e4093b6 RS |
6826 | /* 1 if so far this constructor's elements are all valid address constants. */ |
6827 | static int constructor_simple; | |
ad47f1e5 | 6828 | |
928c19bb JM |
6829 | /* 1 if this constructor has an element that cannot be part of a |
6830 | constant expression. */ | |
6831 | static int constructor_nonconst; | |
6832 | ||
3e4093b6 RS |
6833 | /* 1 if this constructor is erroneous so far. */ |
6834 | static int constructor_erroneous; | |
d45cf215 | 6835 | |
9bac5cbb G |
6836 | /* 1 if this constructor is the universal zero initializer { 0 }. */ |
6837 | static int constructor_zeroinit; | |
6838 | ||
3e4093b6 RS |
6839 | /* Structure for managing pending initializer elements, organized as an |
6840 | AVL tree. */ | |
d45cf215 | 6841 | |
3e4093b6 | 6842 | struct init_node |
d45cf215 | 6843 | { |
3e4093b6 RS |
6844 | struct init_node *left, *right; |
6845 | struct init_node *parent; | |
6846 | int balance; | |
6847 | tree purpose; | |
6848 | tree value; | |
bbbbb16a | 6849 | tree origtype; |
d45cf215 RS |
6850 | }; |
6851 | ||
3e4093b6 RS |
6852 | /* Tree of pending elements at this constructor level. |
6853 | These are elements encountered out of order | |
6854 | which belong at places we haven't reached yet in actually | |
6855 | writing the output. | |
6856 | Will never hold tree nodes across GC runs. */ | |
6857 | static struct init_node *constructor_pending_elts; | |
d45cf215 | 6858 | |
3e4093b6 RS |
6859 | /* The SPELLING_DEPTH of this constructor. */ |
6860 | static int constructor_depth; | |
d45cf215 | 6861 | |
3e4093b6 RS |
6862 | /* DECL node for which an initializer is being read. |
6863 | 0 means we are reading a constructor expression | |
6864 | such as (struct foo) {...}. */ | |
6865 | static tree constructor_decl; | |
d45cf215 | 6866 | |
3e4093b6 RS |
6867 | /* Nonzero if this is an initializer for a top-level decl. */ |
6868 | static int constructor_top_level; | |
d45cf215 | 6869 | |
3e4093b6 RS |
6870 | /* Nonzero if there were any member designators in this initializer. */ |
6871 | static int constructor_designated; | |
d45cf215 | 6872 | |
3e4093b6 RS |
6873 | /* Nesting depth of designator list. */ |
6874 | static int designator_depth; | |
d45cf215 | 6875 | |
3e4093b6 | 6876 | /* Nonzero if there were diagnosed errors in this designator list. */ |
b06df647 | 6877 | static int designator_erroneous; |
d45cf215 | 6878 | |
3e4093b6 RS |
6879 | \f |
6880 | /* This stack has a level for each implicit or explicit level of | |
6881 | structuring in the initializer, including the outermost one. It | |
6882 | saves the values of most of the variables above. */ | |
d45cf215 | 6883 | |
3e4093b6 RS |
6884 | struct constructor_range_stack; |
6885 | ||
6886 | struct constructor_stack | |
d45cf215 | 6887 | { |
3e4093b6 RS |
6888 | struct constructor_stack *next; |
6889 | tree type; | |
6890 | tree fields; | |
6891 | tree index; | |
6892 | tree max_index; | |
6893 | tree unfilled_index; | |
6894 | tree unfilled_fields; | |
6895 | tree bit_index; | |
9771b263 | 6896 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
6897 | struct init_node *pending_elts; |
6898 | int offset; | |
6899 | int depth; | |
916c5919 | 6900 | /* If value nonzero, this value should replace the entire |
3e4093b6 | 6901 | constructor at this level. */ |
916c5919 | 6902 | struct c_expr replacement_value; |
3e4093b6 RS |
6903 | struct constructor_range_stack *range_stack; |
6904 | char constant; | |
6905 | char simple; | |
928c19bb | 6906 | char nonconst; |
3e4093b6 RS |
6907 | char implicit; |
6908 | char erroneous; | |
6909 | char outer; | |
6910 | char incremental; | |
6911 | char designated; | |
976d5a22 | 6912 | int designator_depth; |
3e4093b6 | 6913 | }; |
d45cf215 | 6914 | |
802415d1 | 6915 | static struct constructor_stack *constructor_stack; |
d45cf215 | 6916 | |
3e4093b6 RS |
6917 | /* This stack represents designators from some range designator up to |
6918 | the last designator in the list. */ | |
d45cf215 | 6919 | |
3e4093b6 RS |
6920 | struct constructor_range_stack |
6921 | { | |
6922 | struct constructor_range_stack *next, *prev; | |
6923 | struct constructor_stack *stack; | |
6924 | tree range_start; | |
6925 | tree index; | |
6926 | tree range_end; | |
6927 | tree fields; | |
6928 | }; | |
d45cf215 | 6929 | |
802415d1 | 6930 | static struct constructor_range_stack *constructor_range_stack; |
d45cf215 | 6931 | |
3e4093b6 RS |
6932 | /* This stack records separate initializers that are nested. |
6933 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
6934 | in cases like { ... (struct foo) { ... } ... }. */ | |
d45cf215 | 6935 | |
3e4093b6 | 6936 | struct initializer_stack |
d45cf215 | 6937 | { |
3e4093b6 RS |
6938 | struct initializer_stack *next; |
6939 | tree decl; | |
3e4093b6 RS |
6940 | struct constructor_stack *constructor_stack; |
6941 | struct constructor_range_stack *constructor_range_stack; | |
9771b263 | 6942 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
6943 | struct spelling *spelling; |
6944 | struct spelling *spelling_base; | |
6945 | int spelling_size; | |
6946 | char top_level; | |
6947 | char require_constant_value; | |
6948 | char require_constant_elements; | |
6949 | }; | |
d45cf215 | 6950 | |
802415d1 | 6951 | static struct initializer_stack *initializer_stack; |
3e4093b6 RS |
6952 | \f |
6953 | /* Prepare to parse and output the initializer for variable DECL. */ | |
400fbf9f JW |
6954 | |
6955 | void | |
a396f8ae | 6956 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) |
400fbf9f | 6957 | { |
3e4093b6 | 6958 | const char *locus; |
cceb1885 | 6959 | struct initializer_stack *p = XNEW (struct initializer_stack); |
400fbf9f | 6960 | |
3e4093b6 | 6961 | p->decl = constructor_decl; |
3e4093b6 RS |
6962 | p->require_constant_value = require_constant_value; |
6963 | p->require_constant_elements = require_constant_elements; | |
6964 | p->constructor_stack = constructor_stack; | |
6965 | p->constructor_range_stack = constructor_range_stack; | |
6966 | p->elements = constructor_elements; | |
6967 | p->spelling = spelling; | |
6968 | p->spelling_base = spelling_base; | |
6969 | p->spelling_size = spelling_size; | |
6970 | p->top_level = constructor_top_level; | |
6971 | p->next = initializer_stack; | |
6972 | initializer_stack = p; | |
400fbf9f | 6973 | |
3e4093b6 | 6974 | constructor_decl = decl; |
3e4093b6 RS |
6975 | constructor_designated = 0; |
6976 | constructor_top_level = top_level; | |
400fbf9f | 6977 | |
6f17bbcf | 6978 | if (decl != 0 && decl != error_mark_node) |
3e4093b6 RS |
6979 | { |
6980 | require_constant_value = TREE_STATIC (decl); | |
6981 | require_constant_elements | |
6982 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
6983 | /* For a scalar, you can always use any value to initialize, | |
6984 | even within braces. */ | |
6985 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
6986 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
6987 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
6988 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
88388a52 | 6989 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); |
3e4093b6 RS |
6990 | } |
6991 | else | |
6992 | { | |
6993 | require_constant_value = 0; | |
6994 | require_constant_elements = 0; | |
88388a52 | 6995 | locus = _("(anonymous)"); |
3e4093b6 | 6996 | } |
b71c7f8a | 6997 | |
3e4093b6 RS |
6998 | constructor_stack = 0; |
6999 | constructor_range_stack = 0; | |
b71c7f8a | 7000 | |
9bac5cbb | 7001 | found_missing_braces = 0; |
3e4093b6 RS |
7002 | |
7003 | spelling_base = 0; | |
7004 | spelling_size = 0; | |
7005 | RESTORE_SPELLING_DEPTH (0); | |
7006 | ||
7007 | if (locus) | |
7008 | push_string (locus); | |
7009 | } | |
7010 | ||
7011 | void | |
7012 | finish_init (void) | |
b71c7f8a | 7013 | { |
3e4093b6 | 7014 | struct initializer_stack *p = initializer_stack; |
b71c7f8a | 7015 | |
3e4093b6 RS |
7016 | /* Free the whole constructor stack of this initializer. */ |
7017 | while (constructor_stack) | |
7018 | { | |
7019 | struct constructor_stack *q = constructor_stack; | |
7020 | constructor_stack = q->next; | |
7021 | free (q); | |
7022 | } | |
7023 | ||
366de0ce | 7024 | gcc_assert (!constructor_range_stack); |
3e4093b6 RS |
7025 | |
7026 | /* Pop back to the data of the outer initializer (if any). */ | |
36579663 | 7027 | free (spelling_base); |
3aeb3655 | 7028 | |
3e4093b6 | 7029 | constructor_decl = p->decl; |
3e4093b6 RS |
7030 | require_constant_value = p->require_constant_value; |
7031 | require_constant_elements = p->require_constant_elements; | |
7032 | constructor_stack = p->constructor_stack; | |
7033 | constructor_range_stack = p->constructor_range_stack; | |
7034 | constructor_elements = p->elements; | |
7035 | spelling = p->spelling; | |
7036 | spelling_base = p->spelling_base; | |
7037 | spelling_size = p->spelling_size; | |
7038 | constructor_top_level = p->top_level; | |
7039 | initializer_stack = p->next; | |
7040 | free (p); | |
b71c7f8a | 7041 | } |
400fbf9f | 7042 | \f |
3e4093b6 RS |
7043 | /* Call here when we see the initializer is surrounded by braces. |
7044 | This is instead of a call to push_init_level; | |
7045 | it is matched by a call to pop_init_level. | |
400fbf9f | 7046 | |
3e4093b6 RS |
7047 | TYPE is the type to initialize, for a constructor expression. |
7048 | For an initializer for a decl, TYPE is zero. */ | |
400fbf9f | 7049 | |
3e4093b6 RS |
7050 | void |
7051 | really_start_incremental_init (tree type) | |
400fbf9f | 7052 | { |
5d038c4c | 7053 | struct constructor_stack *p = XNEW (struct constructor_stack); |
400fbf9f | 7054 | |
3e4093b6 RS |
7055 | if (type == 0) |
7056 | type = TREE_TYPE (constructor_decl); | |
400fbf9f | 7057 | |
b6fc2cdb PB |
7058 | if (TREE_CODE (type) == VECTOR_TYPE |
7059 | && TYPE_VECTOR_OPAQUE (type)) | |
3e4093b6 | 7060 | error ("opaque vector types cannot be initialized"); |
400fbf9f | 7061 | |
3e4093b6 RS |
7062 | p->type = constructor_type; |
7063 | p->fields = constructor_fields; | |
7064 | p->index = constructor_index; | |
7065 | p->max_index = constructor_max_index; | |
7066 | p->unfilled_index = constructor_unfilled_index; | |
7067 | p->unfilled_fields = constructor_unfilled_fields; | |
7068 | p->bit_index = constructor_bit_index; | |
7069 | p->elements = constructor_elements; | |
7070 | p->constant = constructor_constant; | |
7071 | p->simple = constructor_simple; | |
928c19bb | 7072 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
7073 | p->erroneous = constructor_erroneous; |
7074 | p->pending_elts = constructor_pending_elts; | |
7075 | p->depth = constructor_depth; | |
916c5919 JM |
7076 | p->replacement_value.value = 0; |
7077 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 7078 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
7079 | p->implicit = 0; |
7080 | p->range_stack = 0; | |
7081 | p->outer = 0; | |
7082 | p->incremental = constructor_incremental; | |
7083 | p->designated = constructor_designated; | |
976d5a22 | 7084 | p->designator_depth = designator_depth; |
3e4093b6 RS |
7085 | p->next = 0; |
7086 | constructor_stack = p; | |
b13aca19 | 7087 | |
3e4093b6 RS |
7088 | constructor_constant = 1; |
7089 | constructor_simple = 1; | |
928c19bb | 7090 | constructor_nonconst = 0; |
3e4093b6 | 7091 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 7092 | constructor_elements = NULL; |
3e4093b6 RS |
7093 | constructor_pending_elts = 0; |
7094 | constructor_type = type; | |
7095 | constructor_incremental = 1; | |
7096 | constructor_designated = 0; | |
9bac5cbb | 7097 | constructor_zeroinit = 1; |
3e4093b6 | 7098 | designator_depth = 0; |
b06df647 | 7099 | designator_erroneous = 0; |
400fbf9f | 7100 | |
3e4093b6 RS |
7101 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
7102 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 7103 | { |
3e4093b6 RS |
7104 | constructor_fields = TYPE_FIELDS (constructor_type); |
7105 | /* Skip any nameless bit fields at the beginning. */ | |
7106 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
7107 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 7108 | constructor_fields = DECL_CHAIN (constructor_fields); |
05bccae2 | 7109 | |
3e4093b6 RS |
7110 | constructor_unfilled_fields = constructor_fields; |
7111 | constructor_bit_index = bitsize_zero_node; | |
400fbf9f | 7112 | } |
3e4093b6 RS |
7113 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
7114 | { | |
7115 | if (TYPE_DOMAIN (constructor_type)) | |
7116 | { | |
7117 | constructor_max_index | |
7118 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 7119 | |
3e4093b6 RS |
7120 | /* Detect non-empty initializations of zero-length arrays. */ |
7121 | if (constructor_max_index == NULL_TREE | |
7122 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 7123 | constructor_max_index = integer_minus_one_node; |
400fbf9f | 7124 | |
3e4093b6 RS |
7125 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
7126 | to initialize VLAs will cause a proper error; avoid tree | |
7127 | checking errors as well by setting a safe value. */ | |
7128 | if (constructor_max_index | |
7129 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 7130 | constructor_max_index = integer_minus_one_node; |
59c83dbf | 7131 | |
3e4093b6 RS |
7132 | constructor_index |
7133 | = convert (bitsizetype, | |
7134 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
59c83dbf | 7135 | } |
3e4093b6 | 7136 | else |
493179da JM |
7137 | { |
7138 | constructor_index = bitsize_zero_node; | |
7139 | constructor_max_index = NULL_TREE; | |
7140 | } | |
59c83dbf | 7141 | |
3e4093b6 RS |
7142 | constructor_unfilled_index = constructor_index; |
7143 | } | |
7144 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
7145 | { | |
7146 | /* Vectors are like simple fixed-size arrays. */ | |
7147 | constructor_max_index = | |
c62c040f | 7148 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
a0f0ab9f | 7149 | constructor_index = bitsize_zero_node; |
3e4093b6 RS |
7150 | constructor_unfilled_index = constructor_index; |
7151 | } | |
7152 | else | |
7153 | { | |
7154 | /* Handle the case of int x = {5}; */ | |
7155 | constructor_fields = constructor_type; | |
7156 | constructor_unfilled_fields = constructor_type; | |
7157 | } | |
7158 | } | |
7159 | \f | |
7160 | /* Push down into a subobject, for initialization. | |
7161 | If this is for an explicit set of braces, IMPLICIT is 0. | |
7162 | If it is because the next element belongs at a lower level, | |
7163 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
400fbf9f | 7164 | |
3e4093b6 | 7165 | void |
ea58ef42 MP |
7166 | push_init_level (location_t loc, int implicit, |
7167 | struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7168 | { |
7169 | struct constructor_stack *p; | |
7170 | tree value = NULL_TREE; | |
400fbf9f | 7171 | |
3e4093b6 | 7172 | /* If we've exhausted any levels that didn't have braces, |
472d98b4 JM |
7173 | pop them now. If implicit == 1, this will have been done in |
7174 | process_init_element; do not repeat it here because in the case | |
7175 | of excess initializers for an empty aggregate this leads to an | |
7176 | infinite cycle of popping a level and immediately recreating | |
7177 | it. */ | |
7178 | if (implicit != 1) | |
3e4093b6 | 7179 | { |
472d98b4 JM |
7180 | while (constructor_stack->implicit) |
7181 | { | |
7182 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7183 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7184 | && constructor_fields == 0) | |
34cf811f | 7185 | process_init_element (input_location, |
ea58ef42 | 7186 | pop_init_level (loc, 1, braced_init_obstack), |
a1e3b3d9 | 7187 | true, braced_init_obstack); |
472d98b4 JM |
7188 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE |
7189 | && constructor_max_index | |
7190 | && tree_int_cst_lt (constructor_max_index, | |
7191 | constructor_index)) | |
34cf811f | 7192 | process_init_element (input_location, |
ea58ef42 | 7193 | pop_init_level (loc, 1, braced_init_obstack), |
a1e3b3d9 | 7194 | true, braced_init_obstack); |
472d98b4 JM |
7195 | else |
7196 | break; | |
7197 | } | |
3e4093b6 | 7198 | } |
400fbf9f | 7199 | |
3e4093b6 RS |
7200 | /* Unless this is an explicit brace, we need to preserve previous |
7201 | content if any. */ | |
7202 | if (implicit) | |
7203 | { | |
7204 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7205 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7206 | && constructor_fields) | |
a1e3b3d9 | 7207 | value = find_init_member (constructor_fields, braced_init_obstack); |
3e4093b6 | 7208 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
a1e3b3d9 | 7209 | value = find_init_member (constructor_index, braced_init_obstack); |
400fbf9f JW |
7210 | } |
7211 | ||
5d038c4c | 7212 | p = XNEW (struct constructor_stack); |
3e4093b6 RS |
7213 | p->type = constructor_type; |
7214 | p->fields = constructor_fields; | |
7215 | p->index = constructor_index; | |
7216 | p->max_index = constructor_max_index; | |
7217 | p->unfilled_index = constructor_unfilled_index; | |
7218 | p->unfilled_fields = constructor_unfilled_fields; | |
7219 | p->bit_index = constructor_bit_index; | |
7220 | p->elements = constructor_elements; | |
7221 | p->constant = constructor_constant; | |
7222 | p->simple = constructor_simple; | |
928c19bb | 7223 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
7224 | p->erroneous = constructor_erroneous; |
7225 | p->pending_elts = constructor_pending_elts; | |
7226 | p->depth = constructor_depth; | |
916c5919 JM |
7227 | p->replacement_value.value = 0; |
7228 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 7229 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
7230 | p->implicit = implicit; |
7231 | p->outer = 0; | |
7232 | p->incremental = constructor_incremental; | |
7233 | p->designated = constructor_designated; | |
976d5a22 | 7234 | p->designator_depth = designator_depth; |
3e4093b6 RS |
7235 | p->next = constructor_stack; |
7236 | p->range_stack = 0; | |
7237 | constructor_stack = p; | |
400fbf9f | 7238 | |
3e4093b6 RS |
7239 | constructor_constant = 1; |
7240 | constructor_simple = 1; | |
928c19bb | 7241 | constructor_nonconst = 0; |
3e4093b6 | 7242 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 7243 | constructor_elements = NULL; |
3e4093b6 RS |
7244 | constructor_incremental = 1; |
7245 | constructor_designated = 0; | |
7246 | constructor_pending_elts = 0; | |
7247 | if (!implicit) | |
400fbf9f | 7248 | { |
3e4093b6 RS |
7249 | p->range_stack = constructor_range_stack; |
7250 | constructor_range_stack = 0; | |
7251 | designator_depth = 0; | |
b06df647 | 7252 | designator_erroneous = 0; |
3e4093b6 | 7253 | } |
400fbf9f | 7254 | |
3e4093b6 RS |
7255 | /* Don't die if an entire brace-pair level is superfluous |
7256 | in the containing level. */ | |
7257 | if (constructor_type == 0) | |
7258 | ; | |
7259 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
7260 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7261 | { | |
7262 | /* Don't die if there are extra init elts at the end. */ | |
7263 | if (constructor_fields == 0) | |
7264 | constructor_type = 0; | |
7265 | else | |
400fbf9f | 7266 | { |
3e4093b6 RS |
7267 | constructor_type = TREE_TYPE (constructor_fields); |
7268 | push_member_name (constructor_fields); | |
7269 | constructor_depth++; | |
400fbf9f | 7270 | } |
6a358dcb MP |
7271 | /* If upper initializer is designated, then mark this as |
7272 | designated too to prevent bogus warnings. */ | |
7273 | constructor_designated = p->designated; | |
3e4093b6 RS |
7274 | } |
7275 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7276 | { | |
7277 | constructor_type = TREE_TYPE (constructor_type); | |
ae7e9ddd | 7278 | push_array_bounds (tree_to_uhwi (constructor_index)); |
3e4093b6 | 7279 | constructor_depth++; |
400fbf9f JW |
7280 | } |
7281 | ||
3e4093b6 | 7282 | if (constructor_type == 0) |
400fbf9f | 7283 | { |
ea58ef42 | 7284 | error_init (loc, "extra brace group at end of initializer"); |
3e4093b6 RS |
7285 | constructor_fields = 0; |
7286 | constructor_unfilled_fields = 0; | |
7287 | return; | |
400fbf9f JW |
7288 | } |
7289 | ||
3e4093b6 RS |
7290 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
7291 | { | |
7292 | constructor_constant = TREE_CONSTANT (value); | |
7293 | constructor_simple = TREE_STATIC (value); | |
928c19bb | 7294 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); |
3e4093b6 | 7295 | constructor_elements = CONSTRUCTOR_ELTS (value); |
9771b263 | 7296 | if (!vec_safe_is_empty (constructor_elements) |
3e4093b6 RS |
7297 | && (TREE_CODE (constructor_type) == RECORD_TYPE |
7298 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
a1e3b3d9 | 7299 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 7300 | } |
400fbf9f | 7301 | |
9bac5cbb G |
7302 | if (implicit == 1) |
7303 | found_missing_braces = 1; | |
400fbf9f | 7304 | |
3e4093b6 RS |
7305 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
7306 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7307 | { | |
7308 | constructor_fields = TYPE_FIELDS (constructor_type); | |
7309 | /* Skip any nameless bit fields at the beginning. */ | |
7310 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
7311 | && DECL_NAME (constructor_fields) == 0) | |
f7587ed0 | 7312 | constructor_fields = DECL_CHAIN (constructor_fields); |
103b7b17 | 7313 | |
3e4093b6 RS |
7314 | constructor_unfilled_fields = constructor_fields; |
7315 | constructor_bit_index = bitsize_zero_node; | |
7316 | } | |
7317 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
7318 | { | |
7319 | /* Vectors are like simple fixed-size arrays. */ | |
7320 | constructor_max_index = | |
c62c040f RG |
7321 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
7322 | constructor_index = bitsize_int (0); | |
3e4093b6 RS |
7323 | constructor_unfilled_index = constructor_index; |
7324 | } | |
7325 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7326 | { | |
7327 | if (TYPE_DOMAIN (constructor_type)) | |
7328 | { | |
7329 | constructor_max_index | |
7330 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 7331 | |
3e4093b6 RS |
7332 | /* Detect non-empty initializations of zero-length arrays. */ |
7333 | if (constructor_max_index == NULL_TREE | |
7334 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 7335 | constructor_max_index = integer_minus_one_node; |
de520661 | 7336 | |
3e4093b6 RS |
7337 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
7338 | to initialize VLAs will cause a proper error; avoid tree | |
7339 | checking errors as well by setting a safe value. */ | |
7340 | if (constructor_max_index | |
7341 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 7342 | constructor_max_index = integer_minus_one_node; |
b62acd60 | 7343 | |
3e4093b6 RS |
7344 | constructor_index |
7345 | = convert (bitsizetype, | |
7346 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7347 | } | |
7348 | else | |
7349 | constructor_index = bitsize_zero_node; | |
de520661 | 7350 | |
3e4093b6 RS |
7351 | constructor_unfilled_index = constructor_index; |
7352 | if (value && TREE_CODE (value) == STRING_CST) | |
7353 | { | |
7354 | /* We need to split the char/wchar array into individual | |
7355 | characters, so that we don't have to special case it | |
7356 | everywhere. */ | |
a1e3b3d9 | 7357 | set_nonincremental_init_from_string (value, braced_init_obstack); |
3e4093b6 RS |
7358 | } |
7359 | } | |
7360 | else | |
7361 | { | |
b4519d39 | 7362 | if (constructor_type != error_mark_node) |
96b40f8d | 7363 | warning_init (input_location, 0, "braces around scalar initializer"); |
3e4093b6 RS |
7364 | constructor_fields = constructor_type; |
7365 | constructor_unfilled_fields = constructor_type; | |
7366 | } | |
7367 | } | |
8b6a5902 | 7368 | |
3e4093b6 | 7369 | /* At the end of an implicit or explicit brace level, |
916c5919 JM |
7370 | finish up that level of constructor. If a single expression |
7371 | with redundant braces initialized that level, return the | |
7372 | c_expr structure for that expression. Otherwise, the original_code | |
7373 | element is set to ERROR_MARK. | |
7374 | If we were outputting the elements as they are read, return 0 as the value | |
3e4093b6 | 7375 | from inner levels (process_init_element ignores that), |
916c5919 | 7376 | but return error_mark_node as the value from the outermost level |
3e4093b6 | 7377 | (that's what we want to put in DECL_INITIAL). |
916c5919 | 7378 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
de520661 | 7379 | |
916c5919 | 7380 | struct c_expr |
ea58ef42 MP |
7381 | pop_init_level (location_t loc, int implicit, |
7382 | struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7383 | { |
7384 | struct constructor_stack *p; | |
916c5919 JM |
7385 | struct c_expr ret; |
7386 | ret.value = 0; | |
7387 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 7388 | ret.original_type = NULL; |
de520661 | 7389 | |
3e4093b6 RS |
7390 | if (implicit == 0) |
7391 | { | |
7392 | /* When we come to an explicit close brace, | |
7393 | pop any inner levels that didn't have explicit braces. */ | |
7394 | while (constructor_stack->implicit) | |
34cf811f | 7395 | process_init_element (input_location, |
ea58ef42 | 7396 | pop_init_level (loc, 1, braced_init_obstack), |
34cf811f | 7397 | true, braced_init_obstack); |
366de0ce | 7398 | gcc_assert (!constructor_range_stack); |
3e4093b6 | 7399 | } |
e5e809f4 | 7400 | |
0066ef9c RH |
7401 | /* Now output all pending elements. */ |
7402 | constructor_incremental = 1; | |
a1e3b3d9 | 7403 | output_pending_init_elements (1, braced_init_obstack); |
0066ef9c | 7404 | |
3e4093b6 | 7405 | p = constructor_stack; |
e5e809f4 | 7406 | |
3e4093b6 RS |
7407 | /* Error for initializing a flexible array member, or a zero-length |
7408 | array member in an inappropriate context. */ | |
7409 | if (constructor_type && constructor_fields | |
7410 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
7411 | && TYPE_DOMAIN (constructor_type) | |
3f75a254 | 7412 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
3e4093b6 RS |
7413 | { |
7414 | /* Silently discard empty initializations. The parser will | |
7415 | already have pedwarned for empty brackets. */ | |
7416 | if (integer_zerop (constructor_unfilled_index)) | |
7417 | constructor_type = NULL_TREE; | |
366de0ce | 7418 | else |
3e4093b6 | 7419 | { |
366de0ce | 7420 | gcc_assert (!TYPE_SIZE (constructor_type)); |
c22cacf3 | 7421 | |
3e4093b6 | 7422 | if (constructor_depth > 2) |
ea58ef42 | 7423 | error_init (loc, "initialization of flexible array member in a nested context"); |
fcf73884 | 7424 | else |
d033409e | 7425 | pedwarn_init (loc, OPT_Wpedantic, |
509c9d60 | 7426 | "initialization of a flexible array member"); |
de520661 | 7427 | |
3e4093b6 RS |
7428 | /* We have already issued an error message for the existence |
7429 | of a flexible array member not at the end of the structure. | |
535a42b1 | 7430 | Discard the initializer so that we do not die later. */ |
910ad8de | 7431 | if (DECL_CHAIN (constructor_fields) != NULL_TREE) |
3e4093b6 RS |
7432 | constructor_type = NULL_TREE; |
7433 | } | |
3e4093b6 | 7434 | } |
de520661 | 7435 | |
9bac5cbb G |
7436 | if (vec_safe_length (constructor_elements) != 1) |
7437 | constructor_zeroinit = 0; | |
7438 | ||
7439 | /* Warn when some structs are initialized with direct aggregation. */ | |
7440 | if (!implicit && found_missing_braces && warn_missing_braces | |
7441 | && !constructor_zeroinit) | |
7442 | { | |
7443 | warning_init (loc, OPT_Wmissing_braces, | |
7444 | "missing braces around initializer"); | |
7445 | } | |
7446 | ||
3e4093b6 | 7447 | /* Warn when some struct elements are implicitly initialized to zero. */ |
eaac4679 | 7448 | if (warn_missing_field_initializers |
3e4093b6 RS |
7449 | && constructor_type |
7450 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
7451 | && constructor_unfilled_fields) | |
7452 | { | |
7453 | /* Do not warn for flexible array members or zero-length arrays. */ | |
7454 | while (constructor_unfilled_fields | |
3f75a254 | 7455 | && (!DECL_SIZE (constructor_unfilled_fields) |
3e4093b6 | 7456 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) |
910ad8de | 7457 | constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); |
cc77d4d5 | 7458 | |
49819fef AM |
7459 | if (constructor_unfilled_fields |
7460 | /* Do not warn if this level of the initializer uses member | |
7461 | designators; it is likely to be deliberate. */ | |
7462 | && !constructor_designated | |
7463 | /* Do not warn about initializing with ` = {0}'. */ | |
7464 | && !constructor_zeroinit) | |
3e4093b6 | 7465 | { |
32397f22 MLI |
7466 | if (warning_at (input_location, OPT_Wmissing_field_initializers, |
7467 | "missing initializer for field %qD of %qT", | |
7468 | constructor_unfilled_fields, | |
7469 | constructor_type)) | |
7470 | inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), | |
ae933128 | 7471 | "%qD declared here", constructor_unfilled_fields); |
3e4093b6 RS |
7472 | } |
7473 | } | |
de520661 | 7474 | |
3e4093b6 | 7475 | /* Pad out the end of the structure. */ |
916c5919 | 7476 | if (p->replacement_value.value) |
3e4093b6 RS |
7477 | /* If this closes a superfluous brace pair, |
7478 | just pass out the element between them. */ | |
916c5919 | 7479 | ret = p->replacement_value; |
3e4093b6 RS |
7480 | else if (constructor_type == 0) |
7481 | ; | |
7482 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7483 | && TREE_CODE (constructor_type) != UNION_TYPE | |
7484 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
7485 | && TREE_CODE (constructor_type) != VECTOR_TYPE) | |
7486 | { | |
7487 | /* A nonincremental scalar initializer--just return | |
7488 | the element, after verifying there is just one. */ | |
9771b263 | 7489 | if (vec_safe_is_empty (constructor_elements)) |
3e4093b6 RS |
7490 | { |
7491 | if (!constructor_erroneous) | |
ea58ef42 | 7492 | error_init (loc, "empty scalar initializer"); |
916c5919 | 7493 | ret.value = error_mark_node; |
3e4093b6 | 7494 | } |
9771b263 | 7495 | else if (vec_safe_length (constructor_elements) != 1) |
3e4093b6 | 7496 | { |
ea58ef42 | 7497 | error_init (loc, "extra elements in scalar initializer"); |
9771b263 | 7498 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7499 | } |
7500 | else | |
9771b263 | 7501 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7502 | } |
7503 | else | |
7504 | { | |
7505 | if (constructor_erroneous) | |
916c5919 | 7506 | ret.value = error_mark_node; |
3e4093b6 RS |
7507 | else |
7508 | { | |
916c5919 | 7509 | ret.value = build_constructor (constructor_type, |
4038c495 | 7510 | constructor_elements); |
3e4093b6 | 7511 | if (constructor_constant) |
51eed280 | 7512 | TREE_CONSTANT (ret.value) = 1; |
3e4093b6 | 7513 | if (constructor_constant && constructor_simple) |
916c5919 | 7514 | TREE_STATIC (ret.value) = 1; |
928c19bb JM |
7515 | if (constructor_nonconst) |
7516 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
3e4093b6 RS |
7517 | } |
7518 | } | |
de520661 | 7519 | |
928c19bb JM |
7520 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) |
7521 | { | |
7522 | if (constructor_nonconst) | |
7523 | ret.original_code = C_MAYBE_CONST_EXPR; | |
7524 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
7525 | ret.original_code = ERROR_MARK; | |
7526 | } | |
7527 | ||
3e4093b6 RS |
7528 | constructor_type = p->type; |
7529 | constructor_fields = p->fields; | |
7530 | constructor_index = p->index; | |
7531 | constructor_max_index = p->max_index; | |
7532 | constructor_unfilled_index = p->unfilled_index; | |
7533 | constructor_unfilled_fields = p->unfilled_fields; | |
7534 | constructor_bit_index = p->bit_index; | |
7535 | constructor_elements = p->elements; | |
7536 | constructor_constant = p->constant; | |
7537 | constructor_simple = p->simple; | |
928c19bb | 7538 | constructor_nonconst = p->nonconst; |
3e4093b6 RS |
7539 | constructor_erroneous = p->erroneous; |
7540 | constructor_incremental = p->incremental; | |
7541 | constructor_designated = p->designated; | |
976d5a22 | 7542 | designator_depth = p->designator_depth; |
3e4093b6 RS |
7543 | constructor_pending_elts = p->pending_elts; |
7544 | constructor_depth = p->depth; | |
7545 | if (!p->implicit) | |
7546 | constructor_range_stack = p->range_stack; | |
7547 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
de520661 | 7548 | |
3e4093b6 RS |
7549 | constructor_stack = p->next; |
7550 | free (p); | |
b621a4dd | 7551 | |
5d5e98dc VR |
7552 | if (ret.value == 0 && constructor_stack == 0) |
7553 | ret.value = error_mark_node; | |
916c5919 | 7554 | return ret; |
3e4093b6 | 7555 | } |
8b6a5902 | 7556 | |
3e4093b6 RS |
7557 | /* Common handling for both array range and field name designators. |
7558 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
400fbf9f | 7559 | |
3e4093b6 | 7560 | static int |
ea58ef42 MP |
7561 | set_designator (location_t loc, int array, |
7562 | struct obstack *braced_init_obstack) | |
de520661 | 7563 | { |
3e4093b6 RS |
7564 | tree subtype; |
7565 | enum tree_code subcode; | |
de520661 | 7566 | |
3e4093b6 RS |
7567 | /* Don't die if an entire brace-pair level is superfluous |
7568 | in the containing level. */ | |
7569 | if (constructor_type == 0) | |
7570 | return 1; | |
de520661 | 7571 | |
366de0ce NS |
7572 | /* If there were errors in this designator list already, bail out |
7573 | silently. */ | |
b06df647 | 7574 | if (designator_erroneous) |
3e4093b6 | 7575 | return 1; |
e28cae4f | 7576 | |
3e4093b6 RS |
7577 | if (!designator_depth) |
7578 | { | |
366de0ce | 7579 | gcc_assert (!constructor_range_stack); |
de520661 | 7580 | |
3e4093b6 RS |
7581 | /* Designator list starts at the level of closest explicit |
7582 | braces. */ | |
7583 | while (constructor_stack->implicit) | |
34cf811f | 7584 | process_init_element (input_location, |
ea58ef42 | 7585 | pop_init_level (loc, 1, braced_init_obstack), |
34cf811f | 7586 | true, braced_init_obstack); |
3e4093b6 RS |
7587 | constructor_designated = 1; |
7588 | return 0; | |
7589 | } | |
de520661 | 7590 | |
366de0ce | 7591 | switch (TREE_CODE (constructor_type)) |
3c3fa147 | 7592 | { |
366de0ce NS |
7593 | case RECORD_TYPE: |
7594 | case UNION_TYPE: | |
3e4093b6 RS |
7595 | subtype = TREE_TYPE (constructor_fields); |
7596 | if (subtype != error_mark_node) | |
7597 | subtype = TYPE_MAIN_VARIANT (subtype); | |
366de0ce NS |
7598 | break; |
7599 | case ARRAY_TYPE: | |
3e4093b6 | 7600 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
366de0ce NS |
7601 | break; |
7602 | default: | |
7603 | gcc_unreachable (); | |
de520661 | 7604 | } |
400fbf9f | 7605 | |
3e4093b6 RS |
7606 | subcode = TREE_CODE (subtype); |
7607 | if (array && subcode != ARRAY_TYPE) | |
7608 | { | |
ea58ef42 | 7609 | error_init (loc, "array index in non-array initializer"); |
3e4093b6 RS |
7610 | return 1; |
7611 | } | |
7612 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
7613 | { | |
ea58ef42 | 7614 | error_init (loc, "field name not in record or union initializer"); |
3e4093b6 RS |
7615 | return 1; |
7616 | } | |
d45cf215 | 7617 | |
3e4093b6 | 7618 | constructor_designated = 1; |
ea58ef42 | 7619 | push_init_level (loc, 2, braced_init_obstack); |
3e4093b6 | 7620 | return 0; |
de520661 | 7621 | } |
400fbf9f | 7622 | |
3e4093b6 RS |
7623 | /* If there are range designators in designator list, push a new designator |
7624 | to constructor_range_stack. RANGE_END is end of such stack range or | |
7625 | NULL_TREE if there is no range designator at this level. */ | |
400fbf9f | 7626 | |
3e4093b6 | 7627 | static void |
a1e3b3d9 | 7628 | push_range_stack (tree range_end, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7629 | { |
7630 | struct constructor_range_stack *p; | |
400fbf9f | 7631 | |
a1e3b3d9 LB |
7632 | p = (struct constructor_range_stack *) |
7633 | obstack_alloc (braced_init_obstack, | |
7634 | sizeof (struct constructor_range_stack)); | |
3e4093b6 RS |
7635 | p->prev = constructor_range_stack; |
7636 | p->next = 0; | |
7637 | p->fields = constructor_fields; | |
7638 | p->range_start = constructor_index; | |
7639 | p->index = constructor_index; | |
7640 | p->stack = constructor_stack; | |
7641 | p->range_end = range_end; | |
8b6a5902 | 7642 | if (constructor_range_stack) |
3e4093b6 RS |
7643 | constructor_range_stack->next = p; |
7644 | constructor_range_stack = p; | |
de520661 | 7645 | } |
400fbf9f | 7646 | |
3e4093b6 RS |
7647 | /* Within an array initializer, specify the next index to be initialized. |
7648 | FIRST is that index. If LAST is nonzero, then initialize a range | |
7649 | of indices, running from FIRST through LAST. */ | |
5a7ec9d9 | 7650 | |
de520661 | 7651 | void |
ea58ef42 | 7652 | set_init_index (location_t loc, tree first, tree last, |
d033409e | 7653 | struct obstack *braced_init_obstack) |
de520661 | 7654 | { |
ea58ef42 | 7655 | if (set_designator (loc, 1, braced_init_obstack)) |
3e4093b6 | 7656 | return; |
de520661 | 7657 | |
b06df647 | 7658 | designator_erroneous = 1; |
de520661 | 7659 | |
3ea8cd06 JM |
7660 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
7661 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
7662 | { | |
ea58ef42 | 7663 | error_init (loc, "array index in initializer not of integer type"); |
3ea8cd06 JM |
7664 | return; |
7665 | } | |
7666 | ||
2b6da65c JM |
7667 | if (TREE_CODE (first) != INTEGER_CST) |
7668 | { | |
7669 | first = c_fully_fold (first, false, NULL); | |
7670 | if (TREE_CODE (first) == INTEGER_CST) | |
d033409e | 7671 | pedwarn_init (loc, OPT_Wpedantic, |
2b6da65c JM |
7672 | "array index in initializer is not " |
7673 | "an integer constant expression"); | |
7674 | } | |
7675 | ||
7676 | if (last && TREE_CODE (last) != INTEGER_CST) | |
7677 | { | |
7678 | last = c_fully_fold (last, false, NULL); | |
7679 | if (TREE_CODE (last) == INTEGER_CST) | |
d033409e | 7680 | pedwarn_init (loc, OPT_Wpedantic, |
2b6da65c JM |
7681 | "array index in initializer is not " |
7682 | "an integer constant expression"); | |
7683 | } | |
7684 | ||
3e4093b6 | 7685 | if (TREE_CODE (first) != INTEGER_CST) |
ea58ef42 | 7686 | error_init (loc, "nonconstant array index in initializer"); |
3e4093b6 | 7687 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) |
ea58ef42 | 7688 | error_init (loc, "nonconstant array index in initializer"); |
3e4093b6 | 7689 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) |
ea58ef42 | 7690 | error_init (loc, "array index in non-array initializer"); |
622adc7e | 7691 | else if (tree_int_cst_sgn (first) == -1) |
ea58ef42 | 7692 | error_init (loc, "array index in initializer exceeds array bounds"); |
3e4093b6 RS |
7693 | else if (constructor_max_index |
7694 | && tree_int_cst_lt (constructor_max_index, first)) | |
ea58ef42 | 7695 | error_init (loc, "array index in initializer exceeds array bounds"); |
3e4093b6 | 7696 | else |
de520661 | 7697 | { |
928c19bb JM |
7698 | constant_expression_warning (first); |
7699 | if (last) | |
7700 | constant_expression_warning (last); | |
3e4093b6 | 7701 | constructor_index = convert (bitsizetype, first); |
40d3d530 JR |
7702 | if (tree_int_cst_lt (constructor_index, first)) |
7703 | { | |
7704 | constructor_index = copy_node (constructor_index); | |
7705 | TREE_OVERFLOW (constructor_index) = 1; | |
7706 | } | |
665f2503 | 7707 | |
3e4093b6 | 7708 | if (last) |
2bede729 | 7709 | { |
3e4093b6 RS |
7710 | if (tree_int_cst_equal (first, last)) |
7711 | last = 0; | |
7712 | else if (tree_int_cst_lt (last, first)) | |
7713 | { | |
ea58ef42 | 7714 | error_init (loc, "empty index range in initializer"); |
3e4093b6 RS |
7715 | last = 0; |
7716 | } | |
7717 | else | |
7718 | { | |
7719 | last = convert (bitsizetype, last); | |
7720 | if (constructor_max_index != 0 | |
7721 | && tree_int_cst_lt (constructor_max_index, last)) | |
7722 | { | |
ea58ef42 MP |
7723 | error_init (loc, "array index range in initializer exceeds " |
7724 | "array bounds"); | |
3e4093b6 RS |
7725 | last = 0; |
7726 | } | |
7727 | } | |
2bede729 | 7728 | } |
fed3cef0 | 7729 | |
3e4093b6 | 7730 | designator_depth++; |
b06df647 | 7731 | designator_erroneous = 0; |
3e4093b6 | 7732 | if (constructor_range_stack || last) |
a1e3b3d9 | 7733 | push_range_stack (last, braced_init_obstack); |
de520661 | 7734 | } |
de520661 | 7735 | } |
3e4093b6 RS |
7736 | |
7737 | /* Within a struct initializer, specify the next field to be initialized. */ | |
400fbf9f | 7738 | |
de520661 | 7739 | void |
ea58ef42 MP |
7740 | set_init_label (location_t loc, tree fieldname, |
7741 | struct obstack *braced_init_obstack) | |
de520661 | 7742 | { |
0fb96aa4 | 7743 | tree field; |
94ba5069 | 7744 | |
ea58ef42 | 7745 | if (set_designator (loc, 0, braced_init_obstack)) |
3e4093b6 RS |
7746 | return; |
7747 | ||
b06df647 | 7748 | designator_erroneous = 1; |
3e4093b6 RS |
7749 | |
7750 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7751 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
94ba5069 | 7752 | { |
ea58ef42 | 7753 | error_init (loc, "field name not in record or union initializer"); |
3e4093b6 | 7754 | return; |
94ba5069 RS |
7755 | } |
7756 | ||
0fb96aa4 | 7757 | field = lookup_field (constructor_type, fieldname); |
8b6a5902 | 7758 | |
0fb96aa4 | 7759 | if (field == 0) |
c51a1ba9 | 7760 | error ("unknown field %qE specified in initializer", fieldname); |
3e4093b6 | 7761 | else |
0fb96aa4 JM |
7762 | do |
7763 | { | |
7764 | constructor_fields = TREE_VALUE (field); | |
7765 | designator_depth++; | |
7766 | designator_erroneous = 0; | |
7767 | if (constructor_range_stack) | |
7768 | push_range_stack (NULL_TREE, braced_init_obstack); | |
7769 | field = TREE_CHAIN (field); | |
7770 | if (field) | |
7771 | { | |
ea58ef42 | 7772 | if (set_designator (loc, 0, braced_init_obstack)) |
0fb96aa4 JM |
7773 | return; |
7774 | } | |
7775 | } | |
7776 | while (field != NULL_TREE); | |
3e4093b6 RS |
7777 | } |
7778 | \f | |
7779 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
7780 | identifies the initializer, either array index or field in a structure. | |
bbbbb16a ILT |
7781 | VALUE is the value of that index or field. If ORIGTYPE is not |
7782 | NULL_TREE, it is the original type of VALUE. | |
b295aee2 JJ |
7783 | |
7784 | IMPLICIT is true if value comes from pop_init_level (1), | |
7785 | the new initializer has been merged with the existing one | |
7786 | and thus no warnings should be emitted about overriding an | |
7787 | existing initializer. */ | |
de520661 | 7788 | |
3e4093b6 | 7789 | static void |
96b40f8d MP |
7790 | add_pending_init (location_t loc, tree purpose, tree value, tree origtype, |
7791 | bool implicit, struct obstack *braced_init_obstack) | |
3e4093b6 RS |
7792 | { |
7793 | struct init_node *p, **q, *r; | |
7794 | ||
7795 | q = &constructor_pending_elts; | |
7796 | p = 0; | |
7797 | ||
7798 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 7799 | { |
3e4093b6 | 7800 | while (*q != 0) |
91fa3c30 | 7801 | { |
3e4093b6 RS |
7802 | p = *q; |
7803 | if (tree_int_cst_lt (purpose, p->purpose)) | |
7804 | q = &p->left; | |
7805 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
7806 | q = &p->right; | |
7807 | else | |
7808 | { | |
b295aee2 JJ |
7809 | if (!implicit) |
7810 | { | |
7811 | if (TREE_SIDE_EFFECTS (p->value)) | |
96b40f8d MP |
7812 | warning_init (loc, 0, |
7813 | "initialized field with side-effects " | |
7814 | "overwritten"); | |
b295aee2 | 7815 | else if (warn_override_init) |
96b40f8d MP |
7816 | warning_init (loc, OPT_Woverride_init, |
7817 | "initialized field overwritten"); | |
b295aee2 | 7818 | } |
3e4093b6 | 7819 | p->value = value; |
bbbbb16a | 7820 | p->origtype = origtype; |
3e4093b6 RS |
7821 | return; |
7822 | } | |
91fa3c30 | 7823 | } |
de520661 | 7824 | } |
3e4093b6 | 7825 | else |
de520661 | 7826 | { |
3e4093b6 | 7827 | tree bitpos; |
400fbf9f | 7828 | |
3e4093b6 RS |
7829 | bitpos = bit_position (purpose); |
7830 | while (*q != NULL) | |
7831 | { | |
7832 | p = *q; | |
7833 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7834 | q = &p->left; | |
7835 | else if (p->purpose != purpose) | |
7836 | q = &p->right; | |
7837 | else | |
7838 | { | |
b295aee2 JJ |
7839 | if (!implicit) |
7840 | { | |
7841 | if (TREE_SIDE_EFFECTS (p->value)) | |
96b40f8d MP |
7842 | warning_init (loc, 0, |
7843 | "initialized field with side-effects " | |
7844 | "overwritten"); | |
b295aee2 | 7845 | else if (warn_override_init) |
96b40f8d MP |
7846 | warning_init (loc, OPT_Woverride_init, |
7847 | "initialized field overwritten"); | |
b295aee2 | 7848 | } |
3e4093b6 | 7849 | p->value = value; |
bbbbb16a | 7850 | p->origtype = origtype; |
3e4093b6 RS |
7851 | return; |
7852 | } | |
7853 | } | |
91fa3c30 | 7854 | } |
b71c7f8a | 7855 | |
a1e3b3d9 LB |
7856 | r = (struct init_node *) obstack_alloc (braced_init_obstack, |
7857 | sizeof (struct init_node)); | |
3e4093b6 RS |
7858 | r->purpose = purpose; |
7859 | r->value = value; | |
bbbbb16a | 7860 | r->origtype = origtype; |
8b6a5902 | 7861 | |
3e4093b6 RS |
7862 | *q = r; |
7863 | r->parent = p; | |
7864 | r->left = 0; | |
7865 | r->right = 0; | |
7866 | r->balance = 0; | |
b71c7f8a | 7867 | |
3e4093b6 | 7868 | while (p) |
de520661 | 7869 | { |
3e4093b6 | 7870 | struct init_node *s; |
665f2503 | 7871 | |
3e4093b6 | 7872 | if (r == p->left) |
2bede729 | 7873 | { |
3e4093b6 RS |
7874 | if (p->balance == 0) |
7875 | p->balance = -1; | |
7876 | else if (p->balance < 0) | |
7877 | { | |
7878 | if (r->balance < 0) | |
7879 | { | |
7880 | /* L rotation. */ | |
7881 | p->left = r->right; | |
7882 | if (p->left) | |
7883 | p->left->parent = p; | |
7884 | r->right = p; | |
e7b6a0ee | 7885 | |
3e4093b6 RS |
7886 | p->balance = 0; |
7887 | r->balance = 0; | |
39bc99c2 | 7888 | |
3e4093b6 RS |
7889 | s = p->parent; |
7890 | p->parent = r; | |
7891 | r->parent = s; | |
7892 | if (s) | |
7893 | { | |
7894 | if (s->left == p) | |
7895 | s->left = r; | |
7896 | else | |
7897 | s->right = r; | |
7898 | } | |
7899 | else | |
7900 | constructor_pending_elts = r; | |
7901 | } | |
7902 | else | |
7903 | { | |
7904 | /* LR rotation. */ | |
7905 | struct init_node *t = r->right; | |
e7b6a0ee | 7906 | |
3e4093b6 RS |
7907 | r->right = t->left; |
7908 | if (r->right) | |
7909 | r->right->parent = r; | |
7910 | t->left = r; | |
7911 | ||
7912 | p->left = t->right; | |
7913 | if (p->left) | |
7914 | p->left->parent = p; | |
7915 | t->right = p; | |
7916 | ||
7917 | p->balance = t->balance < 0; | |
7918 | r->balance = -(t->balance > 0); | |
7919 | t->balance = 0; | |
7920 | ||
7921 | s = p->parent; | |
7922 | p->parent = t; | |
7923 | r->parent = t; | |
7924 | t->parent = s; | |
7925 | if (s) | |
7926 | { | |
7927 | if (s->left == p) | |
7928 | s->left = t; | |
7929 | else | |
7930 | s->right = t; | |
7931 | } | |
7932 | else | |
7933 | constructor_pending_elts = t; | |
7934 | } | |
7935 | break; | |
7936 | } | |
7937 | else | |
7938 | { | |
7939 | /* p->balance == +1; growth of left side balances the node. */ | |
7940 | p->balance = 0; | |
7941 | break; | |
7942 | } | |
2bede729 | 7943 | } |
3e4093b6 RS |
7944 | else /* r == p->right */ |
7945 | { | |
7946 | if (p->balance == 0) | |
7947 | /* Growth propagation from right side. */ | |
7948 | p->balance++; | |
7949 | else if (p->balance > 0) | |
7950 | { | |
7951 | if (r->balance > 0) | |
7952 | { | |
7953 | /* R rotation. */ | |
7954 | p->right = r->left; | |
7955 | if (p->right) | |
7956 | p->right->parent = p; | |
7957 | r->left = p; | |
7958 | ||
7959 | p->balance = 0; | |
7960 | r->balance = 0; | |
7961 | ||
7962 | s = p->parent; | |
7963 | p->parent = r; | |
7964 | r->parent = s; | |
7965 | if (s) | |
7966 | { | |
7967 | if (s->left == p) | |
7968 | s->left = r; | |
7969 | else | |
7970 | s->right = r; | |
7971 | } | |
7972 | else | |
7973 | constructor_pending_elts = r; | |
7974 | } | |
7975 | else /* r->balance == -1 */ | |
7976 | { | |
7977 | /* RL rotation */ | |
7978 | struct init_node *t = r->left; | |
7979 | ||
7980 | r->left = t->right; | |
7981 | if (r->left) | |
7982 | r->left->parent = r; | |
7983 | t->right = r; | |
7984 | ||
7985 | p->right = t->left; | |
7986 | if (p->right) | |
7987 | p->right->parent = p; | |
7988 | t->left = p; | |
7989 | ||
7990 | r->balance = (t->balance < 0); | |
7991 | p->balance = -(t->balance > 0); | |
7992 | t->balance = 0; | |
7993 | ||
7994 | s = p->parent; | |
7995 | p->parent = t; | |
7996 | r->parent = t; | |
7997 | t->parent = s; | |
7998 | if (s) | |
7999 | { | |
8000 | if (s->left == p) | |
8001 | s->left = t; | |
8002 | else | |
8003 | s->right = t; | |
8004 | } | |
8005 | else | |
8006 | constructor_pending_elts = t; | |
8007 | } | |
8008 | break; | |
8009 | } | |
8010 | else | |
8011 | { | |
8012 | /* p->balance == -1; growth of right side balances the node. */ | |
8013 | p->balance = 0; | |
8014 | break; | |
8015 | } | |
8016 | } | |
8017 | ||
8018 | r = p; | |
8019 | p = p->parent; | |
8020 | } | |
8021 | } | |
8022 | ||
8023 | /* Build AVL tree from a sorted chain. */ | |
8024 | ||
8025 | static void | |
a1e3b3d9 | 8026 | set_nonincremental_init (struct obstack * braced_init_obstack) |
3e4093b6 | 8027 | { |
4038c495 GB |
8028 | unsigned HOST_WIDE_INT ix; |
8029 | tree index, value; | |
3e4093b6 RS |
8030 | |
8031 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
8032 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
8033 | return; | |
8034 | ||
4038c495 | 8035 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) |
96b40f8d MP |
8036 | add_pending_init (input_location, index, value, NULL_TREE, true, |
8037 | braced_init_obstack); | |
9771b263 | 8038 | constructor_elements = NULL; |
3e4093b6 RS |
8039 | if (TREE_CODE (constructor_type) == RECORD_TYPE) |
8040 | { | |
8041 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
8042 | /* Skip any nameless bit fields at the beginning. */ | |
8043 | while (constructor_unfilled_fields != 0 | |
8044 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8045 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8046 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
fed3cef0 | 8047 | |
de520661 | 8048 | } |
3e4093b6 | 8049 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 8050 | { |
3e4093b6 RS |
8051 | if (TYPE_DOMAIN (constructor_type)) |
8052 | constructor_unfilled_index | |
8053 | = convert (bitsizetype, | |
8054 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
8055 | else | |
8056 | constructor_unfilled_index = bitsize_zero_node; | |
de520661 | 8057 | } |
3e4093b6 | 8058 | constructor_incremental = 0; |
de520661 | 8059 | } |
400fbf9f | 8060 | |
3e4093b6 | 8061 | /* Build AVL tree from a string constant. */ |
de520661 | 8062 | |
3e4093b6 | 8063 | static void |
a1e3b3d9 LB |
8064 | set_nonincremental_init_from_string (tree str, |
8065 | struct obstack * braced_init_obstack) | |
de520661 | 8066 | { |
3e4093b6 RS |
8067 | tree value, purpose, type; |
8068 | HOST_WIDE_INT val[2]; | |
8069 | const char *p, *end; | |
8070 | int byte, wchar_bytes, charwidth, bitpos; | |
de520661 | 8071 | |
366de0ce | 8072 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); |
940ff66d | 8073 | |
c466b2cd | 8074 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; |
3e4093b6 RS |
8075 | charwidth = TYPE_PRECISION (char_type_node); |
8076 | type = TREE_TYPE (constructor_type); | |
8077 | p = TREE_STRING_POINTER (str); | |
8078 | end = p + TREE_STRING_LENGTH (str); | |
91fa3c30 | 8079 | |
3e4093b6 | 8080 | for (purpose = bitsize_zero_node; |
8824edff JJ |
8081 | p < end |
8082 | && !(constructor_max_index | |
8083 | && tree_int_cst_lt (constructor_max_index, purpose)); | |
3e4093b6 | 8084 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) |
584ef5fe | 8085 | { |
3e4093b6 | 8086 | if (wchar_bytes == 1) |
ffc5c6a9 | 8087 | { |
807e902e KZ |
8088 | val[0] = (unsigned char) *p++; |
8089 | val[1] = 0; | |
ffc5c6a9 RH |
8090 | } |
8091 | else | |
3e4093b6 | 8092 | { |
3e4093b6 | 8093 | val[1] = 0; |
807e902e | 8094 | val[0] = 0; |
3e4093b6 RS |
8095 | for (byte = 0; byte < wchar_bytes; byte++) |
8096 | { | |
8097 | if (BYTES_BIG_ENDIAN) | |
8098 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
8099 | else | |
8100 | bitpos = byte * charwidth; | |
807e902e | 8101 | val[bitpos % HOST_BITS_PER_WIDE_INT] |
3e4093b6 RS |
8102 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) |
8103 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
8104 | } | |
8105 | } | |
584ef5fe | 8106 | |
8df83eae | 8107 | if (!TYPE_UNSIGNED (type)) |
3e4093b6 RS |
8108 | { |
8109 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
8110 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
8111 | { | |
807e902e | 8112 | if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) |
3e4093b6 | 8113 | { |
807e902e KZ |
8114 | val[0] |= ((HOST_WIDE_INT) -1) << bitpos; |
8115 | val[1] = -1; | |
3e4093b6 RS |
8116 | } |
8117 | } | |
8118 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
8119 | { | |
807e902e KZ |
8120 | if (val[0] < 0) |
8121 | val[1] = -1; | |
3e4093b6 | 8122 | } |
807e902e | 8123 | else if (val[1] & (((HOST_WIDE_INT) 1) |
3e4093b6 | 8124 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) |
807e902e | 8125 | val[1] |= ((HOST_WIDE_INT) -1) |
3e4093b6 RS |
8126 | << (bitpos - HOST_BITS_PER_WIDE_INT); |
8127 | } | |
ffc5c6a9 | 8128 | |
807e902e KZ |
8129 | value = wide_int_to_tree (type, |
8130 | wide_int::from_array (val, 2, | |
8131 | HOST_BITS_PER_WIDE_INT * 2)); | |
96b40f8d | 8132 | add_pending_init (input_location, purpose, value, NULL_TREE, true, |
a1e3b3d9 | 8133 | braced_init_obstack); |
9dfcc8db BH |
8134 | } |
8135 | ||
3e4093b6 RS |
8136 | constructor_incremental = 0; |
8137 | } | |
de520661 | 8138 | |
3e4093b6 RS |
8139 | /* Return value of FIELD in pending initializer or zero if the field was |
8140 | not initialized yet. */ | |
8141 | ||
8142 | static tree | |
a1e3b3d9 | 8143 | find_init_member (tree field, struct obstack * braced_init_obstack) |
3e4093b6 RS |
8144 | { |
8145 | struct init_node *p; | |
8146 | ||
8147 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
19d76e60 | 8148 | { |
3e4093b6 RS |
8149 | if (constructor_incremental |
8150 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 8151 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8152 | |
8153 | p = constructor_pending_elts; | |
8154 | while (p) | |
19d76e60 | 8155 | { |
3e4093b6 RS |
8156 | if (tree_int_cst_lt (field, p->purpose)) |
8157 | p = p->left; | |
8158 | else if (tree_int_cst_lt (p->purpose, field)) | |
8159 | p = p->right; | |
8160 | else | |
8161 | return p->value; | |
19d76e60 | 8162 | } |
19d76e60 | 8163 | } |
3e4093b6 | 8164 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
de520661 | 8165 | { |
3e4093b6 | 8166 | tree bitpos = bit_position (field); |
de520661 | 8167 | |
3e4093b6 RS |
8168 | if (constructor_incremental |
8169 | && (!constructor_unfilled_fields | |
8170 | || tree_int_cst_lt (bitpos, | |
8171 | bit_position (constructor_unfilled_fields)))) | |
a1e3b3d9 | 8172 | set_nonincremental_init (braced_init_obstack); |
de520661 | 8173 | |
3e4093b6 RS |
8174 | p = constructor_pending_elts; |
8175 | while (p) | |
8176 | { | |
8177 | if (field == p->purpose) | |
8178 | return p->value; | |
8179 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
8180 | p = p->left; | |
8181 | else | |
8182 | p = p->right; | |
8183 | } | |
8184 | } | |
8185 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 8186 | { |
9771b263 DN |
8187 | if (!vec_safe_is_empty (constructor_elements) |
8188 | && (constructor_elements->last ().index == field)) | |
8189 | return constructor_elements->last ().value; | |
de520661 | 8190 | } |
3e4093b6 | 8191 | return 0; |
de520661 RS |
8192 | } |
8193 | ||
3e4093b6 RS |
8194 | /* "Output" the next constructor element. |
8195 | At top level, really output it to assembler code now. | |
8196 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
bbbbb16a | 8197 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. |
3e4093b6 RS |
8198 | TYPE is the data type that the containing data type wants here. |
8199 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
916c5919 JM |
8200 | If VALUE is a string constant, STRICT_STRING is true if it is |
8201 | unparenthesized or we should not warn here for it being parenthesized. | |
8202 | For other types of VALUE, STRICT_STRING is not used. | |
8b6a5902 | 8203 | |
3e4093b6 RS |
8204 | PENDING if non-nil means output pending elements that belong |
8205 | right after this element. (PENDING is normally 1; | |
b295aee2 JJ |
8206 | it is 0 while outputting pending elements, to avoid recursion.) |
8207 | ||
8208 | IMPLICIT is true if value comes from pop_init_level (1), | |
8209 | the new initializer has been merged with the existing one | |
8210 | and thus no warnings should be emitted about overriding an | |
8211 | existing initializer. */ | |
8b6a5902 | 8212 | |
3e4093b6 | 8213 | static void |
34cf811f MP |
8214 | output_init_element (location_t loc, tree value, tree origtype, |
8215 | bool strict_string, tree type, tree field, int pending, | |
8216 | bool implicit, struct obstack * braced_init_obstack) | |
3e4093b6 | 8217 | { |
8ce94e44 | 8218 | tree semantic_type = NULL_TREE; |
928c19bb JM |
8219 | bool maybe_const = true; |
8220 | bool npc; | |
4038c495 | 8221 | |
0a880880 | 8222 | if (type == error_mark_node || value == error_mark_node) |
8b6a5902 | 8223 | { |
3e4093b6 RS |
8224 | constructor_erroneous = 1; |
8225 | return; | |
8b6a5902 | 8226 | } |
46bdb9cf JM |
8227 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
8228 | && (TREE_CODE (value) == STRING_CST | |
8229 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
8230 | && !(TREE_CODE (value) == STRING_CST | |
8231 | && TREE_CODE (type) == ARRAY_TYPE | |
8232 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
8233 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
8234 | TYPE_MAIN_VARIANT (type))) | |
c2255bc4 | 8235 | value = array_to_pointer_conversion (input_location, value); |
8b6a5902 | 8236 | |
3e4093b6 RS |
8237 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
8238 | && require_constant_value && !flag_isoc99 && pending) | |
8b6a5902 | 8239 | { |
3e4093b6 RS |
8240 | /* As an extension, allow initializing objects with static storage |
8241 | duration with compound literals (which are then treated just as | |
8242 | the brace enclosed list they contain). */ | |
8243 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); | |
8244 | value = DECL_INITIAL (decl); | |
8b6a5902 JJ |
8245 | } |
8246 | ||
928c19bb | 8247 | npc = null_pointer_constant_p (value); |
8ce94e44 JM |
8248 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) |
8249 | { | |
8250 | semantic_type = TREE_TYPE (value); | |
8251 | value = TREE_OPERAND (value, 0); | |
8252 | } | |
928c19bb JM |
8253 | value = c_fully_fold (value, require_constant_value, &maybe_const); |
8254 | ||
3e4093b6 RS |
8255 | if (value == error_mark_node) |
8256 | constructor_erroneous = 1; | |
8257 | else if (!TREE_CONSTANT (value)) | |
8258 | constructor_constant = 0; | |
116df786 | 8259 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) |
3e4093b6 RS |
8260 | || ((TREE_CODE (constructor_type) == RECORD_TYPE |
8261 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8262 | && DECL_C_BIT_FIELD (field) | |
8263 | && TREE_CODE (value) != INTEGER_CST)) | |
8264 | constructor_simple = 0; | |
928c19bb JM |
8265 | if (!maybe_const) |
8266 | constructor_nonconst = 1; | |
3e4093b6 | 8267 | |
116df786 | 8268 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) |
8b6a5902 | 8269 | { |
116df786 RH |
8270 | if (require_constant_value) |
8271 | { | |
ea58ef42 | 8272 | error_init (loc, "initializer element is not constant"); |
116df786 RH |
8273 | value = error_mark_node; |
8274 | } | |
8275 | else if (require_constant_elements) | |
8337d1db | 8276 | pedwarn (loc, OPT_Wpedantic, |
509c9d60 | 8277 | "initializer element is not computable at load time"); |
8b6a5902 | 8278 | } |
928c19bb JM |
8279 | else if (!maybe_const |
8280 | && (require_constant_value || require_constant_elements)) | |
8337d1db | 8281 | pedwarn_init (loc, OPT_Wpedantic, |
928c19bb | 8282 | "initializer element is not a constant expression"); |
3e4093b6 | 8283 | |
81f40b79 ILT |
8284 | /* Issue -Wc++-compat warnings about initializing a bitfield with |
8285 | enum type. */ | |
8286 | if (warn_cxx_compat | |
8287 | && field != NULL_TREE | |
8288 | && TREE_CODE (field) == FIELD_DECL | |
8289 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
8290 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
8291 | != TYPE_MAIN_VARIANT (type)) | |
8292 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
8293 | { | |
8294 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
8295 | if (checktype != error_mark_node | |
8296 | && (TYPE_MAIN_VARIANT (checktype) | |
8297 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
96b40f8d | 8298 | warning_init (loc, OPT_Wc___compat, |
81f40b79 ILT |
8299 | "enum conversion in initialization is invalid in C++"); |
8300 | } | |
8301 | ||
3e4093b6 RS |
8302 | /* If this field is empty (and not at the end of structure), |
8303 | don't do anything other than checking the initializer. */ | |
8304 | if (field | |
8305 | && (TREE_TYPE (field) == error_mark_node | |
8306 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
8307 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
8308 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
910ad8de | 8309 | || DECL_CHAIN (field))))) |
3e4093b6 RS |
8310 | return; |
8311 | ||
8ce94e44 JM |
8312 | if (semantic_type) |
8313 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
34cf811f MP |
8314 | value = digest_init (loc, type, value, origtype, npc, strict_string, |
8315 | require_constant_value); | |
3e4093b6 | 8316 | if (value == error_mark_node) |
8b6a5902 | 8317 | { |
3e4093b6 RS |
8318 | constructor_erroneous = 1; |
8319 | return; | |
8b6a5902 | 8320 | } |
928c19bb JM |
8321 | if (require_constant_value || require_constant_elements) |
8322 | constant_expression_warning (value); | |
8b6a5902 | 8323 | |
3e4093b6 RS |
8324 | /* If this element doesn't come next in sequence, |
8325 | put it on constructor_pending_elts. */ | |
8326 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
8327 | && (!constructor_incremental | |
8328 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
8b6a5902 | 8329 | { |
3e4093b6 RS |
8330 | if (constructor_incremental |
8331 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 8332 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 8333 | |
96b40f8d | 8334 | add_pending_init (loc, field, value, origtype, implicit, |
a1e3b3d9 | 8335 | braced_init_obstack); |
3e4093b6 | 8336 | return; |
8b6a5902 | 8337 | } |
3e4093b6 RS |
8338 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
8339 | && (!constructor_incremental | |
8340 | || field != constructor_unfilled_fields)) | |
8b6a5902 | 8341 | { |
3e4093b6 RS |
8342 | /* We do this for records but not for unions. In a union, |
8343 | no matter which field is specified, it can be initialized | |
8344 | right away since it starts at the beginning of the union. */ | |
8345 | if (constructor_incremental) | |
8346 | { | |
8347 | if (!constructor_unfilled_fields) | |
a1e3b3d9 | 8348 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8349 | else |
8350 | { | |
8351 | tree bitpos, unfillpos; | |
8352 | ||
8353 | bitpos = bit_position (field); | |
8354 | unfillpos = bit_position (constructor_unfilled_fields); | |
8355 | ||
8356 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
a1e3b3d9 | 8357 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8358 | } |
8359 | } | |
8360 | ||
96b40f8d | 8361 | add_pending_init (loc, field, value, origtype, implicit, |
a1e3b3d9 | 8362 | braced_init_obstack); |
3e4093b6 | 8363 | return; |
8b6a5902 | 8364 | } |
3e4093b6 | 8365 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
9771b263 | 8366 | && !vec_safe_is_empty (constructor_elements)) |
3e4093b6 | 8367 | { |
b295aee2 JJ |
8368 | if (!implicit) |
8369 | { | |
9771b263 | 8370 | if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) |
96b40f8d | 8371 | warning_init (loc, 0, |
b295aee2 JJ |
8372 | "initialized field with side-effects overwritten"); |
8373 | else if (warn_override_init) | |
96b40f8d MP |
8374 | warning_init (loc, OPT_Woverride_init, |
8375 | "initialized field overwritten"); | |
b295aee2 | 8376 | } |
8b6a5902 | 8377 | |
3e4093b6 | 8378 | /* We can have just one union field set. */ |
9771b263 | 8379 | constructor_elements = NULL; |
3e4093b6 | 8380 | } |
8b6a5902 | 8381 | |
3e4093b6 RS |
8382 | /* Otherwise, output this element either to |
8383 | constructor_elements or to the assembler file. */ | |
8b6a5902 | 8384 | |
f32682ca | 8385 | constructor_elt celt = {field, value}; |
9771b263 | 8386 | vec_safe_push (constructor_elements, celt); |
8b6a5902 | 8387 | |
3e4093b6 RS |
8388 | /* Advance the variable that indicates sequential elements output. */ |
8389 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8390 | constructor_unfilled_index | |
db3927fb AH |
8391 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, |
8392 | bitsize_one_node); | |
3e4093b6 RS |
8393 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
8394 | { | |
8395 | constructor_unfilled_fields | |
910ad8de | 8396 | = DECL_CHAIN (constructor_unfilled_fields); |
8b6a5902 | 8397 | |
3e4093b6 RS |
8398 | /* Skip any nameless bit fields. */ |
8399 | while (constructor_unfilled_fields != 0 | |
8400 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8401 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8402 | constructor_unfilled_fields = | |
910ad8de | 8403 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 RS |
8404 | } |
8405 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
8406 | constructor_unfilled_fields = 0; | |
de520661 | 8407 | |
3e4093b6 RS |
8408 | /* Now output any pending elements which have become next. */ |
8409 | if (pending) | |
a1e3b3d9 | 8410 | output_pending_init_elements (0, braced_init_obstack); |
3e4093b6 | 8411 | } |
8b6a5902 | 8412 | |
3e4093b6 RS |
8413 | /* Output any pending elements which have become next. |
8414 | As we output elements, constructor_unfilled_{fields,index} | |
8415 | advances, which may cause other elements to become next; | |
8416 | if so, they too are output. | |
8b6a5902 | 8417 | |
3e4093b6 RS |
8418 | If ALL is 0, we return when there are |
8419 | no more pending elements to output now. | |
665f2503 | 8420 | |
3e4093b6 RS |
8421 | If ALL is 1, we output space as necessary so that |
8422 | we can output all the pending elements. */ | |
3e4093b6 | 8423 | static void |
a1e3b3d9 | 8424 | output_pending_init_elements (int all, struct obstack * braced_init_obstack) |
3e4093b6 RS |
8425 | { |
8426 | struct init_node *elt = constructor_pending_elts; | |
8427 | tree next; | |
de520661 | 8428 | |
3e4093b6 RS |
8429 | retry: |
8430 | ||
ba228239 | 8431 | /* Look through the whole pending tree. |
3e4093b6 RS |
8432 | If we find an element that should be output now, |
8433 | output it. Otherwise, set NEXT to the element | |
8434 | that comes first among those still pending. */ | |
8435 | ||
8436 | next = 0; | |
8437 | while (elt) | |
8438 | { | |
8439 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8b6a5902 | 8440 | { |
3e4093b6 RS |
8441 | if (tree_int_cst_equal (elt->purpose, |
8442 | constructor_unfilled_index)) | |
34cf811f MP |
8443 | output_init_element (input_location, elt->value, elt->origtype, |
8444 | true, TREE_TYPE (constructor_type), | |
a1e3b3d9 LB |
8445 | constructor_unfilled_index, 0, false, |
8446 | braced_init_obstack); | |
3e4093b6 RS |
8447 | else if (tree_int_cst_lt (constructor_unfilled_index, |
8448 | elt->purpose)) | |
8b6a5902 | 8449 | { |
3e4093b6 RS |
8450 | /* Advance to the next smaller node. */ |
8451 | if (elt->left) | |
8452 | elt = elt->left; | |
8453 | else | |
8454 | { | |
8455 | /* We have reached the smallest node bigger than the | |
8456 | current unfilled index. Fill the space first. */ | |
8457 | next = elt->purpose; | |
8458 | break; | |
8459 | } | |
8b6a5902 | 8460 | } |
ce662d4c JJ |
8461 | else |
8462 | { | |
3e4093b6 RS |
8463 | /* Advance to the next bigger node. */ |
8464 | if (elt->right) | |
8465 | elt = elt->right; | |
8466 | else | |
ce662d4c | 8467 | { |
3e4093b6 RS |
8468 | /* We have reached the biggest node in a subtree. Find |
8469 | the parent of it, which is the next bigger node. */ | |
8470 | while (elt->parent && elt->parent->right == elt) | |
8471 | elt = elt->parent; | |
8472 | elt = elt->parent; | |
8473 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
8474 | elt->purpose)) | |
8475 | { | |
8476 | next = elt->purpose; | |
8477 | break; | |
8478 | } | |
ce662d4c JJ |
8479 | } |
8480 | } | |
8b6a5902 | 8481 | } |
3e4093b6 RS |
8482 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
8483 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8484 | { | |
8485 | tree ctor_unfilled_bitpos, elt_bitpos; | |
ce662d4c | 8486 | |
3e4093b6 RS |
8487 | /* If the current record is complete we are done. */ |
8488 | if (constructor_unfilled_fields == 0) | |
8489 | break; | |
de520661 | 8490 | |
3e4093b6 RS |
8491 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
8492 | elt_bitpos = bit_position (elt->purpose); | |
8493 | /* We can't compare fields here because there might be empty | |
8494 | fields in between. */ | |
8495 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
8496 | { | |
8497 | constructor_unfilled_fields = elt->purpose; | |
34cf811f MP |
8498 | output_init_element (input_location, elt->value, elt->origtype, |
8499 | true, TREE_TYPE (elt->purpose), | |
a1e3b3d9 LB |
8500 | elt->purpose, 0, false, |
8501 | braced_init_obstack); | |
3e4093b6 RS |
8502 | } |
8503 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
8504 | { | |
8505 | /* Advance to the next smaller node. */ | |
8506 | if (elt->left) | |
8507 | elt = elt->left; | |
8508 | else | |
8509 | { | |
8510 | /* We have reached the smallest node bigger than the | |
8511 | current unfilled field. Fill the space first. */ | |
8512 | next = elt->purpose; | |
8513 | break; | |
8514 | } | |
8515 | } | |
8516 | else | |
8517 | { | |
8518 | /* Advance to the next bigger node. */ | |
8519 | if (elt->right) | |
8520 | elt = elt->right; | |
8521 | else | |
8522 | { | |
8523 | /* We have reached the biggest node in a subtree. Find | |
8524 | the parent of it, which is the next bigger node. */ | |
8525 | while (elt->parent && elt->parent->right == elt) | |
8526 | elt = elt->parent; | |
8527 | elt = elt->parent; | |
8528 | if (elt | |
8529 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
8530 | bit_position (elt->purpose)))) | |
8531 | { | |
8532 | next = elt->purpose; | |
8533 | break; | |
8534 | } | |
8535 | } | |
8536 | } | |
8537 | } | |
8538 | } | |
de520661 | 8539 | |
3e4093b6 RS |
8540 | /* Ordinarily return, but not if we want to output all |
8541 | and there are elements left. */ | |
3f75a254 | 8542 | if (!(all && next != 0)) |
e5cfb88f RK |
8543 | return; |
8544 | ||
3e4093b6 RS |
8545 | /* If it's not incremental, just skip over the gap, so that after |
8546 | jumping to retry we will output the next successive element. */ | |
8547 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
8548 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8549 | constructor_unfilled_fields = next; | |
8550 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8551 | constructor_unfilled_index = next; | |
de520661 | 8552 | |
3e4093b6 RS |
8553 | /* ELT now points to the node in the pending tree with the next |
8554 | initializer to output. */ | |
8555 | goto retry; | |
de520661 RS |
8556 | } |
8557 | \f | |
3e4093b6 RS |
8558 | /* Add one non-braced element to the current constructor level. |
8559 | This adjusts the current position within the constructor's type. | |
8560 | This may also start or terminate implicit levels | |
8561 | to handle a partly-braced initializer. | |
e5e809f4 | 8562 | |
3e4093b6 | 8563 | Once this has found the correct level for the new element, |
b295aee2 JJ |
8564 | it calls output_init_element. |
8565 | ||
8566 | IMPLICIT is true if value comes from pop_init_level (1), | |
8567 | the new initializer has been merged with the existing one | |
8568 | and thus no warnings should be emitted about overriding an | |
8569 | existing initializer. */ | |
3e4093b6 RS |
8570 | |
8571 | void | |
34cf811f | 8572 | process_init_element (location_t loc, struct c_expr value, bool implicit, |
a1e3b3d9 | 8573 | struct obstack * braced_init_obstack) |
e5e809f4 | 8574 | { |
916c5919 JM |
8575 | tree orig_value = value.value; |
8576 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
8577 | bool strict_string = value.original_code == STRING_CST; | |
340baef7 | 8578 | bool was_designated = designator_depth != 0; |
e5e809f4 | 8579 | |
3e4093b6 | 8580 | designator_depth = 0; |
b06df647 | 8581 | designator_erroneous = 0; |
e5e809f4 | 8582 | |
9bac5cbb G |
8583 | if (!implicit && value.value && !integer_zerop (value.value)) |
8584 | constructor_zeroinit = 0; | |
8585 | ||
3e4093b6 RS |
8586 | /* Handle superfluous braces around string cst as in |
8587 | char x[] = {"foo"}; */ | |
8588 | if (string_flag | |
8589 | && constructor_type | |
340baef7 | 8590 | && !was_designated |
3e4093b6 | 8591 | && TREE_CODE (constructor_type) == ARRAY_TYPE |
197463ae | 8592 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
3e4093b6 | 8593 | && integer_zerop (constructor_unfilled_index)) |
e5e809f4 | 8594 | { |
916c5919 | 8595 | if (constructor_stack->replacement_value.value) |
ea58ef42 | 8596 | error_init (loc, "excess elements in char array initializer"); |
3e4093b6 RS |
8597 | constructor_stack->replacement_value = value; |
8598 | return; | |
e5e809f4 | 8599 | } |
8b6a5902 | 8600 | |
916c5919 | 8601 | if (constructor_stack->replacement_value.value != 0) |
3e4093b6 | 8602 | { |
ea58ef42 | 8603 | error_init (loc, "excess elements in struct initializer"); |
3e4093b6 | 8604 | return; |
e5e809f4 JL |
8605 | } |
8606 | ||
3e4093b6 RS |
8607 | /* Ignore elements of a brace group if it is entirely superfluous |
8608 | and has already been diagnosed. */ | |
8609 | if (constructor_type == 0) | |
8610 | return; | |
e5e809f4 | 8611 | |
976d5a22 TT |
8612 | if (!implicit && warn_designated_init && !was_designated |
8613 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
8614 | && lookup_attribute ("designated_init", | |
8615 | TYPE_ATTRIBUTES (constructor_type))) | |
8616 | warning_init (loc, | |
8617 | OPT_Wdesignated_init, | |
8618 | "positional initialization of field " | |
8619 | "in %<struct%> declared with %<designated_init%> attribute"); | |
8620 | ||
3e4093b6 RS |
8621 | /* If we've exhausted any levels that didn't have braces, |
8622 | pop them now. */ | |
8623 | while (constructor_stack->implicit) | |
8624 | { | |
8625 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
8626 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8627 | && constructor_fields == 0) | |
ea58ef42 MP |
8628 | process_init_element (loc, |
8629 | pop_init_level (loc, 1, braced_init_obstack), | |
a1e3b3d9 | 8630 | true, braced_init_obstack); |
53650abe AP |
8631 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE |
8632 | || TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8824edff JJ |
8633 | && constructor_max_index |
8634 | && tree_int_cst_lt (constructor_max_index, | |
8635 | constructor_index)) | |
ea58ef42 MP |
8636 | process_init_element (loc, |
8637 | pop_init_level (loc, 1, braced_init_obstack), | |
a1e3b3d9 | 8638 | true, braced_init_obstack); |
3e4093b6 RS |
8639 | else |
8640 | break; | |
8641 | } | |
e5e809f4 | 8642 | |
3e4093b6 RS |
8643 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
8644 | if (constructor_range_stack) | |
e5e809f4 | 8645 | { |
3e4093b6 RS |
8646 | /* If value is a compound literal and we'll be just using its |
8647 | content, don't put it into a SAVE_EXPR. */ | |
916c5919 | 8648 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
3e4093b6 RS |
8649 | || !require_constant_value |
8650 | || flag_isoc99) | |
8ce94e44 JM |
8651 | { |
8652 | tree semantic_type = NULL_TREE; | |
8653 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
8654 | { | |
8655 | semantic_type = TREE_TYPE (value.value); | |
8656 | value.value = TREE_OPERAND (value.value, 0); | |
8657 | } | |
8658 | value.value = c_save_expr (value.value); | |
8659 | if (semantic_type) | |
8660 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
8661 | value.value); | |
8662 | } | |
3e4093b6 | 8663 | } |
e5e809f4 | 8664 | |
3e4093b6 RS |
8665 | while (1) |
8666 | { | |
8667 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
e5e809f4 | 8668 | { |
3e4093b6 RS |
8669 | tree fieldtype; |
8670 | enum tree_code fieldcode; | |
e5e809f4 | 8671 | |
3e4093b6 RS |
8672 | if (constructor_fields == 0) |
8673 | { | |
ea58ef42 | 8674 | pedwarn_init (loc, 0, "excess elements in struct initializer"); |
3e4093b6 RS |
8675 | break; |
8676 | } | |
e5e809f4 | 8677 | |
3e4093b6 RS |
8678 | fieldtype = TREE_TYPE (constructor_fields); |
8679 | if (fieldtype != error_mark_node) | |
8680 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8681 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8682 | |
3e4093b6 RS |
8683 | /* Error for non-static initialization of a flexible array member. */ |
8684 | if (fieldcode == ARRAY_TYPE | |
8685 | && !require_constant_value | |
8686 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
f7587ed0 | 8687 | && DECL_CHAIN (constructor_fields) == NULL_TREE) |
3e4093b6 | 8688 | { |
ea58ef42 MP |
8689 | error_init (loc, "non-static initialization of a flexible " |
8690 | "array member"); | |
3e4093b6 RS |
8691 | break; |
8692 | } | |
e5e809f4 | 8693 | |
3e4093b6 | 8694 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8695 | if (value.value != 0 |
3e4093b6 | 8696 | && fieldcode == ARRAY_TYPE |
197463ae | 8697 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8698 | && string_flag) |
916c5919 | 8699 | value.value = orig_value; |
3e4093b6 RS |
8700 | /* Otherwise, if we have come to a subaggregate, |
8701 | and we don't have an element of its type, push into it. */ | |
0953878d | 8702 | else if (value.value != 0 |
916c5919 JM |
8703 | && value.value != error_mark_node |
8704 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 8705 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 8706 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 8707 | { |
ea58ef42 | 8708 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
8709 | continue; |
8710 | } | |
e5e809f4 | 8711 | |
916c5919 | 8712 | if (value.value) |
3e4093b6 RS |
8713 | { |
8714 | push_member_name (constructor_fields); | |
34cf811f | 8715 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 8716 | strict_string, fieldtype, |
a1e3b3d9 LB |
8717 | constructor_fields, 1, implicit, |
8718 | braced_init_obstack); | |
3e4093b6 | 8719 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
8720 | } |
8721 | else | |
3e4093b6 RS |
8722 | /* Do the bookkeeping for an element that was |
8723 | directly output as a constructor. */ | |
e5e809f4 | 8724 | { |
3e4093b6 RS |
8725 | /* For a record, keep track of end position of last field. */ |
8726 | if (DECL_SIZE (constructor_fields)) | |
c22cacf3 | 8727 | constructor_bit_index |
db3927fb AH |
8728 | = size_binop_loc (input_location, PLUS_EXPR, |
8729 | bit_position (constructor_fields), | |
8730 | DECL_SIZE (constructor_fields)); | |
3e4093b6 RS |
8731 | |
8732 | /* If the current field was the first one not yet written out, | |
8733 | it isn't now, so update. */ | |
8734 | if (constructor_unfilled_fields == constructor_fields) | |
8735 | { | |
910ad8de | 8736 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8737 | /* Skip any nameless bit fields. */ |
8738 | while (constructor_unfilled_fields != 0 | |
8739 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8740 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8741 | constructor_unfilled_fields = | |
910ad8de | 8742 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 | 8743 | } |
e5e809f4 | 8744 | } |
3e4093b6 | 8745 | |
910ad8de | 8746 | constructor_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8747 | /* Skip any nameless bit fields at the beginning. */ |
8748 | while (constructor_fields != 0 | |
8749 | && DECL_C_BIT_FIELD (constructor_fields) | |
8750 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 8751 | constructor_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 8752 | } |
3e4093b6 | 8753 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
e5e809f4 | 8754 | { |
3e4093b6 RS |
8755 | tree fieldtype; |
8756 | enum tree_code fieldcode; | |
e5e809f4 | 8757 | |
3e4093b6 RS |
8758 | if (constructor_fields == 0) |
8759 | { | |
d033409e | 8760 | pedwarn_init (loc, 0, |
509c9d60 | 8761 | "excess elements in union initializer"); |
3e4093b6 RS |
8762 | break; |
8763 | } | |
e5e809f4 | 8764 | |
3e4093b6 RS |
8765 | fieldtype = TREE_TYPE (constructor_fields); |
8766 | if (fieldtype != error_mark_node) | |
8767 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8768 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8769 | |
3e4093b6 RS |
8770 | /* Warn that traditional C rejects initialization of unions. |
8771 | We skip the warning if the value is zero. This is done | |
8772 | under the assumption that the zero initializer in user | |
8773 | code appears conditioned on e.g. __STDC__ to avoid | |
8774 | "missing initializer" warnings and relies on default | |
8775 | initialization to zero in the traditional C case. | |
8776 | We also skip the warning if the initializer is designated, | |
8777 | again on the assumption that this must be conditional on | |
8778 | __STDC__ anyway (and we've already complained about the | |
8779 | member-designator already). */ | |
8400e75e | 8780 | if (!in_system_header_at (input_location) && !constructor_designated |
916c5919 JM |
8781 | && !(value.value && (integer_zerop (value.value) |
8782 | || real_zerop (value.value)))) | |
3176a0c2 DD |
8783 | warning (OPT_Wtraditional, "traditional C rejects initialization " |
8784 | "of unions"); | |
e5e809f4 | 8785 | |
3e4093b6 | 8786 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8787 | if (value.value != 0 |
3e4093b6 | 8788 | && fieldcode == ARRAY_TYPE |
197463ae | 8789 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8790 | && string_flag) |
916c5919 | 8791 | value.value = orig_value; |
3e4093b6 RS |
8792 | /* Otherwise, if we have come to a subaggregate, |
8793 | and we don't have an element of its type, push into it. */ | |
0953878d | 8794 | else if (value.value != 0 |
916c5919 JM |
8795 | && value.value != error_mark_node |
8796 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 8797 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 8798 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 8799 | { |
ea58ef42 | 8800 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
8801 | continue; |
8802 | } | |
e5e809f4 | 8803 | |
916c5919 | 8804 | if (value.value) |
3e4093b6 RS |
8805 | { |
8806 | push_member_name (constructor_fields); | |
34cf811f | 8807 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 8808 | strict_string, fieldtype, |
a1e3b3d9 LB |
8809 | constructor_fields, 1, implicit, |
8810 | braced_init_obstack); | |
3e4093b6 | 8811 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
8812 | } |
8813 | else | |
3e4093b6 RS |
8814 | /* Do the bookkeeping for an element that was |
8815 | directly output as a constructor. */ | |
e5e809f4 | 8816 | { |
3e4093b6 | 8817 | constructor_bit_index = DECL_SIZE (constructor_fields); |
f7587ed0 | 8818 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 8819 | } |
e5e809f4 | 8820 | |
3e4093b6 RS |
8821 | constructor_fields = 0; |
8822 | } | |
8823 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8824 | { | |
8825 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8826 | enum tree_code eltcode = TREE_CODE (elttype); | |
e5e809f4 | 8827 | |
3e4093b6 | 8828 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8829 | if (value.value != 0 |
3e4093b6 | 8830 | && eltcode == ARRAY_TYPE |
197463ae | 8831 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
3e4093b6 | 8832 | && string_flag) |
916c5919 | 8833 | value.value = orig_value; |
3e4093b6 RS |
8834 | /* Otherwise, if we have come to a subaggregate, |
8835 | and we don't have an element of its type, push into it. */ | |
0953878d | 8836 | else if (value.value != 0 |
916c5919 JM |
8837 | && value.value != error_mark_node |
8838 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
3e4093b6 | 8839 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
53650abe | 8840 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) |
3e4093b6 | 8841 | { |
ea58ef42 | 8842 | push_init_level (loc, 1, braced_init_obstack); |
3e4093b6 RS |
8843 | continue; |
8844 | } | |
8b6a5902 | 8845 | |
3e4093b6 RS |
8846 | if (constructor_max_index != 0 |
8847 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
8848 | || integer_all_onesp (constructor_max_index))) | |
8849 | { | |
d033409e | 8850 | pedwarn_init (loc, 0, |
509c9d60 | 8851 | "excess elements in array initializer"); |
3e4093b6 RS |
8852 | break; |
8853 | } | |
8b6a5902 | 8854 | |
3e4093b6 | 8855 | /* Now output the actual element. */ |
916c5919 | 8856 | if (value.value) |
3e4093b6 | 8857 | { |
ae7e9ddd | 8858 | push_array_bounds (tree_to_uhwi (constructor_index)); |
34cf811f | 8859 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 8860 | strict_string, elttype, |
a1e3b3d9 LB |
8861 | constructor_index, 1, implicit, |
8862 | braced_init_obstack); | |
3e4093b6 RS |
8863 | RESTORE_SPELLING_DEPTH (constructor_depth); |
8864 | } | |
2f6e4e97 | 8865 | |
3e4093b6 | 8866 | constructor_index |
db3927fb AH |
8867 | = size_binop_loc (input_location, PLUS_EXPR, |
8868 | constructor_index, bitsize_one_node); | |
8b6a5902 | 8869 | |
916c5919 | 8870 | if (!value.value) |
3e4093b6 RS |
8871 | /* If we are doing the bookkeeping for an element that was |
8872 | directly output as a constructor, we must update | |
8873 | constructor_unfilled_index. */ | |
8874 | constructor_unfilled_index = constructor_index; | |
8875 | } | |
8876 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8877 | { | |
8878 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8b6a5902 | 8879 | |
c22cacf3 MS |
8880 | /* Do a basic check of initializer size. Note that vectors |
8881 | always have a fixed size derived from their type. */ | |
3e4093b6 RS |
8882 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) |
8883 | { | |
d033409e | 8884 | pedwarn_init (loc, 0, |
509c9d60 | 8885 | "excess elements in vector initializer"); |
3e4093b6 RS |
8886 | break; |
8887 | } | |
8b6a5902 | 8888 | |
3e4093b6 | 8889 | /* Now output the actual element. */ |
916c5919 | 8890 | if (value.value) |
53650abe AP |
8891 | { |
8892 | if (TREE_CODE (value.value) == VECTOR_CST) | |
8893 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
34cf811f | 8894 | output_init_element (loc, value.value, value.original_type, |
53650abe | 8895 | strict_string, elttype, |
a1e3b3d9 LB |
8896 | constructor_index, 1, implicit, |
8897 | braced_init_obstack); | |
53650abe | 8898 | } |
8b6a5902 | 8899 | |
3e4093b6 | 8900 | constructor_index |
db3927fb AH |
8901 | = size_binop_loc (input_location, |
8902 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 8903 | |
916c5919 | 8904 | if (!value.value) |
3e4093b6 RS |
8905 | /* If we are doing the bookkeeping for an element that was |
8906 | directly output as a constructor, we must update | |
8907 | constructor_unfilled_index. */ | |
8908 | constructor_unfilled_index = constructor_index; | |
8909 | } | |
8b6a5902 | 8910 | |
3e4093b6 RS |
8911 | /* Handle the sole element allowed in a braced initializer |
8912 | for a scalar variable. */ | |
b4519d39 SB |
8913 | else if (constructor_type != error_mark_node |
8914 | && constructor_fields == 0) | |
8b6a5902 | 8915 | { |
d033409e | 8916 | pedwarn_init (loc, 0, |
509c9d60 | 8917 | "excess elements in scalar initializer"); |
3e4093b6 | 8918 | break; |
8b6a5902 JJ |
8919 | } |
8920 | else | |
8921 | { | |
916c5919 | 8922 | if (value.value) |
34cf811f | 8923 | output_init_element (loc, value.value, value.original_type, |
bbbbb16a | 8924 | strict_string, constructor_type, |
a1e3b3d9 LB |
8925 | NULL_TREE, 1, implicit, |
8926 | braced_init_obstack); | |
3e4093b6 | 8927 | constructor_fields = 0; |
8b6a5902 JJ |
8928 | } |
8929 | ||
3e4093b6 RS |
8930 | /* Handle range initializers either at this level or anywhere higher |
8931 | in the designator stack. */ | |
8932 | if (constructor_range_stack) | |
8b6a5902 | 8933 | { |
3e4093b6 RS |
8934 | struct constructor_range_stack *p, *range_stack; |
8935 | int finish = 0; | |
8936 | ||
8937 | range_stack = constructor_range_stack; | |
8938 | constructor_range_stack = 0; | |
8939 | while (constructor_stack != range_stack->stack) | |
8b6a5902 | 8940 | { |
366de0ce | 8941 | gcc_assert (constructor_stack->implicit); |
34cf811f | 8942 | process_init_element (loc, |
ea58ef42 MP |
8943 | pop_init_level (loc, 1, |
8944 | braced_init_obstack), | |
a1e3b3d9 | 8945 | true, braced_init_obstack); |
8b6a5902 | 8946 | } |
3e4093b6 RS |
8947 | for (p = range_stack; |
8948 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
8949 | p = p->prev) | |
8b6a5902 | 8950 | { |
366de0ce | 8951 | gcc_assert (constructor_stack->implicit); |
34cf811f | 8952 | process_init_element (loc, |
ea58ef42 MP |
8953 | pop_init_level (loc, 1, |
8954 | braced_init_obstack), | |
a1e3b3d9 | 8955 | true, braced_init_obstack); |
8b6a5902 | 8956 | } |
3e4093b6 | 8957 | |
db3927fb AH |
8958 | p->index = size_binop_loc (input_location, |
8959 | PLUS_EXPR, p->index, bitsize_one_node); | |
3e4093b6 RS |
8960 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) |
8961 | finish = 1; | |
8962 | ||
8963 | while (1) | |
8964 | { | |
8965 | constructor_index = p->index; | |
8966 | constructor_fields = p->fields; | |
8967 | if (finish && p->range_end && p->index == p->range_start) | |
8968 | { | |
8969 | finish = 0; | |
8970 | p->prev = 0; | |
8971 | } | |
8972 | p = p->next; | |
8973 | if (!p) | |
8974 | break; | |
ea58ef42 | 8975 | push_init_level (loc, 2, braced_init_obstack); |
3e4093b6 RS |
8976 | p->stack = constructor_stack; |
8977 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
8978 | p->index = p->range_start; | |
8979 | } | |
8980 | ||
8981 | if (!finish) | |
8982 | constructor_range_stack = range_stack; | |
8983 | continue; | |
8b6a5902 JJ |
8984 | } |
8985 | ||
3e4093b6 | 8986 | break; |
8b6a5902 JJ |
8987 | } |
8988 | ||
3e4093b6 RS |
8989 | constructor_range_stack = 0; |
8990 | } | |
8991 | \f | |
9f0e2d86 ZW |
8992 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
8993 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
e130a54b | 8994 | an ASM_EXPR node). */ |
3e4093b6 | 8995 | tree |
9f0e2d86 | 8996 | build_asm_stmt (tree cv_qualifier, tree args) |
3e4093b6 | 8997 | { |
6de9cd9a DN |
8998 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
8999 | ASM_VOLATILE_P (args) = 1; | |
9f0e2d86 | 9000 | return add_stmt (args); |
8b6a5902 JJ |
9001 | } |
9002 | ||
9f0e2d86 ZW |
9003 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
9004 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
9005 | SIMPLE indicates whether there was anything at all after the | |
9006 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
e130a54b | 9007 | are subtly different. We use a ASM_EXPR node to represent this. */ |
3e4093b6 | 9008 | tree |
c2255bc4 | 9009 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, |
1c384bf1 | 9010 | tree clobbers, tree labels, bool simple) |
e5e809f4 | 9011 | { |
3e4093b6 | 9012 | tree tail; |
9f0e2d86 | 9013 | tree args; |
6de9cd9a DN |
9014 | int i; |
9015 | const char *constraint; | |
74f0c611 | 9016 | const char **oconstraints; |
6de9cd9a | 9017 | bool allows_mem, allows_reg, is_inout; |
74f0c611 | 9018 | int ninputs, noutputs; |
6de9cd9a DN |
9019 | |
9020 | ninputs = list_length (inputs); | |
9021 | noutputs = list_length (outputs); | |
74f0c611 RH |
9022 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); |
9023 | ||
1c384bf1 | 9024 | string = resolve_asm_operand_names (string, outputs, inputs, labels); |
3e4093b6 | 9025 | |
6de9cd9a DN |
9026 | /* Remove output conversions that change the type but not the mode. */ |
9027 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
e5e809f4 | 9028 | { |
3e4093b6 | 9029 | tree output = TREE_VALUE (tail); |
74f0c611 | 9030 | |
eadd3d0d JJ |
9031 | output = c_fully_fold (output, false, NULL); |
9032 | ||
74f0c611 RH |
9033 | /* ??? Really, this should not be here. Users should be using a |
9034 | proper lvalue, dammit. But there's a long history of using casts | |
9035 | in the output operands. In cases like longlong.h, this becomes a | |
9036 | primitive form of typechecking -- if the cast can be removed, then | |
9037 | the output operand had a type of the proper width; otherwise we'll | |
9038 | get an error. Gross, but ... */ | |
3e4093b6 | 9039 | STRIP_NOPS (output); |
74f0c611 | 9040 | |
7bd11157 | 9041 | if (!lvalue_or_else (loc, output, lv_asm)) |
74f0c611 | 9042 | output = error_mark_node; |
8b6a5902 | 9043 | |
5544530a PB |
9044 | if (output != error_mark_node |
9045 | && (TREE_READONLY (output) | |
9046 | || TYPE_READONLY (TREE_TYPE (output)) | |
9047 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
9048 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
9049 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
c02065fc | 9050 | readonly_error (loc, output, lv_asm); |
5544530a | 9051 | |
6de9cd9a | 9052 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
74f0c611 RH |
9053 | oconstraints[i] = constraint; |
9054 | ||
9055 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
9056 | &allows_mem, &allows_reg, &is_inout)) | |
9057 | { | |
9058 | /* If the operand is going to end up in memory, | |
9059 | mark it addressable. */ | |
9060 | if (!allows_reg && !c_mark_addressable (output)) | |
9061 | output = error_mark_node; | |
bae5cddf JJ |
9062 | if (!(!allows_reg && allows_mem) |
9063 | && output != error_mark_node | |
9064 | && VOID_TYPE_P (TREE_TYPE (output))) | |
9065 | { | |
9066 | error_at (loc, "invalid use of void expression"); | |
9067 | output = error_mark_node; | |
9068 | } | |
74f0c611 RH |
9069 | } |
9070 | else | |
c22cacf3 | 9071 | output = error_mark_node; |
3e4093b6 | 9072 | |
74f0c611 | 9073 | TREE_VALUE (tail) = output; |
8b6a5902 | 9074 | } |
3e4093b6 | 9075 | |
74f0c611 RH |
9076 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) |
9077 | { | |
9078 | tree input; | |
9079 | ||
9080 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
9081 | input = TREE_VALUE (tail); | |
9082 | ||
74f0c611 RH |
9083 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
9084 | oconstraints, &allows_mem, &allows_reg)) | |
9085 | { | |
9086 | /* If the operand is going to end up in memory, | |
9087 | mark it addressable. */ | |
b4c33883 AP |
9088 | if (!allows_reg && allows_mem) |
9089 | { | |
eadd3d0d JJ |
9090 | input = c_fully_fold (input, false, NULL); |
9091 | ||
b4c33883 AP |
9092 | /* Strip the nops as we allow this case. FIXME, this really |
9093 | should be rejected or made deprecated. */ | |
9094 | STRIP_NOPS (input); | |
9095 | if (!c_mark_addressable (input)) | |
9096 | input = error_mark_node; | |
bae5cddf | 9097 | } |
eadd3d0d | 9098 | else |
bae5cddf | 9099 | { |
eadd3d0d JJ |
9100 | struct c_expr expr; |
9101 | memset (&expr, 0, sizeof (expr)); | |
9102 | expr.value = input; | |
267bac10 | 9103 | expr = convert_lvalue_to_rvalue (loc, expr, true, false); |
eadd3d0d JJ |
9104 | input = c_fully_fold (expr.value, false, NULL); |
9105 | ||
9106 | if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) | |
9107 | { | |
9108 | error_at (loc, "invalid use of void expression"); | |
9109 | input = error_mark_node; | |
9110 | } | |
bae5cddf | 9111 | } |
74f0c611 RH |
9112 | } |
9113 | else | |
9114 | input = error_mark_node; | |
9115 | ||
9116 | TREE_VALUE (tail) = input; | |
9117 | } | |
3e4093b6 | 9118 | |
1c384bf1 RH |
9119 | /* ASMs with labels cannot have outputs. This should have been |
9120 | enforced by the parser. */ | |
9121 | gcc_assert (outputs == NULL || labels == NULL); | |
9122 | ||
9123 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
9f0e2d86 | 9124 | |
5544530a PB |
9125 | /* asm statements without outputs, including simple ones, are treated |
9126 | as volatile. */ | |
9127 | ASM_INPUT_P (args) = simple; | |
9128 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
74f0c611 | 9129 | |
9f0e2d86 | 9130 | return args; |
e5e809f4 | 9131 | } |
3e4093b6 | 9132 | \f |
c2255bc4 AH |
9133 | /* Generate a goto statement to LABEL. LOC is the location of the |
9134 | GOTO. */ | |
506e2710 RH |
9135 | |
9136 | tree | |
c2255bc4 | 9137 | c_finish_goto_label (location_t loc, tree label) |
506e2710 | 9138 | { |
e1b7793c | 9139 | tree decl = lookup_label_for_goto (loc, label); |
506e2710 RH |
9140 | if (!decl) |
9141 | return NULL_TREE; | |
506e2710 | 9142 | TREE_USED (decl) = 1; |
c2255bc4 AH |
9143 | { |
9144 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
9145 | SET_EXPR_LOCATION (t, loc); | |
9146 | return add_stmt (t); | |
9147 | } | |
506e2710 RH |
9148 | } |
9149 | ||
c2255bc4 AH |
9150 | /* Generate a computed goto statement to EXPR. LOC is the location of |
9151 | the GOTO. */ | |
506e2710 RH |
9152 | |
9153 | tree | |
c2255bc4 | 9154 | c_finish_goto_ptr (location_t loc, tree expr) |
506e2710 | 9155 | { |
c2255bc4 | 9156 | tree t; |
c1771a20 | 9157 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); |
928c19bb | 9158 | expr = c_fully_fold (expr, false, NULL); |
506e2710 | 9159 | expr = convert (ptr_type_node, expr); |
c2255bc4 AH |
9160 | t = build1 (GOTO_EXPR, void_type_node, expr); |
9161 | SET_EXPR_LOCATION (t, loc); | |
9162 | return add_stmt (t); | |
506e2710 RH |
9163 | } |
9164 | ||
5088b058 | 9165 | /* Generate a C `return' statement. RETVAL is the expression for what |
c2255bc4 | 9166 | to return, or a null pointer for `return;' with no value. LOC is |
6e07c515 MP |
9167 | the location of the return statement, or the location of the expression, |
9168 | if the statement has any. If ORIGTYPE is not NULL_TREE, it | |
c2255bc4 | 9169 | is the original type of RETVAL. */ |
de520661 | 9170 | |
506e2710 | 9171 | tree |
c2255bc4 | 9172 | c_finish_return (location_t loc, tree retval, tree origtype) |
3e4093b6 | 9173 | { |
0c9b182b JJ |
9174 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; |
9175 | bool no_warning = false; | |
928c19bb | 9176 | bool npc = false; |
36536d79 | 9177 | size_t rank = 0; |
3e4093b6 RS |
9178 | |
9179 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
c2255bc4 AH |
9180 | warning_at (loc, 0, |
9181 | "function declared %<noreturn%> has a %<return%> statement"); | |
3e4093b6 | 9182 | |
b72271b9 | 9183 | if (flag_cilkplus && contains_array_notation_expr (retval)) |
36536d79 BI |
9184 | { |
9185 | /* Array notations are allowed in a return statement if it is inside a | |
9186 | built-in array notation reduction function. */ | |
9187 | if (!find_rank (loc, retval, retval, false, &rank)) | |
9188 | return error_mark_node; | |
9189 | if (rank >= 1) | |
9190 | { | |
9191 | error_at (loc, "array notation expression cannot be used as a " | |
9192 | "return value"); | |
9193 | return error_mark_node; | |
9194 | } | |
9195 | } | |
3af9c5e9 | 9196 | if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval)) |
939b37da BI |
9197 | { |
9198 | error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not " | |
9199 | "allowed"); | |
9200 | return error_mark_node; | |
9201 | } | |
928c19bb JM |
9202 | if (retval) |
9203 | { | |
8ce94e44 | 9204 | tree semantic_type = NULL_TREE; |
928c19bb | 9205 | npc = null_pointer_constant_p (retval); |
8ce94e44 JM |
9206 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) |
9207 | { | |
9208 | semantic_type = TREE_TYPE (retval); | |
9209 | retval = TREE_OPERAND (retval, 0); | |
9210 | } | |
928c19bb | 9211 | retval = c_fully_fold (retval, false, NULL); |
8ce94e44 JM |
9212 | if (semantic_type) |
9213 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
928c19bb JM |
9214 | } |
9215 | ||
3e4093b6 | 9216 | if (!retval) |
de520661 | 9217 | { |
3e4093b6 RS |
9218 | current_function_returns_null = 1; |
9219 | if ((warn_return_type || flag_isoc99) | |
9220 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
0c9b182b | 9221 | { |
b8698a0f | 9222 | pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type, |
fcf73884 | 9223 | "%<return%> with no value, in " |
0c9b182b JJ |
9224 | "function returning non-void"); |
9225 | no_warning = true; | |
9226 | } | |
400fbf9f | 9227 | } |
3e4093b6 | 9228 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
de520661 | 9229 | { |
3e4093b6 | 9230 | current_function_returns_null = 1; |
2397c575 | 9231 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) |
b8698a0f | 9232 | pedwarn (loc, 0, |
509c9d60 | 9233 | "%<return%> with a value, in function returning void"); |
b8698a0f | 9234 | else |
c1771a20 | 9235 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 9236 | "%<return%> with expression, in function returning void"); |
de520661 | 9237 | } |
3e4093b6 | 9238 | else |
de520661 | 9239 | { |
68fca595 MP |
9240 | tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype, |
9241 | retval, origtype, ic_return, | |
c2255bc4 | 9242 | npc, NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
9243 | tree res = DECL_RESULT (current_function_decl); |
9244 | tree inner; | |
9feb29df | 9245 | bool save; |
3e4093b6 RS |
9246 | |
9247 | current_function_returns_value = 1; | |
9248 | if (t == error_mark_node) | |
506e2710 | 9249 | return NULL_TREE; |
3e4093b6 | 9250 | |
9feb29df JJ |
9251 | save = in_late_binary_op; |
9252 | if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE | |
9253 | || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE) | |
9254 | in_late_binary_op = true; | |
3e4093b6 | 9255 | inner = t = convert (TREE_TYPE (res), t); |
9feb29df | 9256 | in_late_binary_op = save; |
3e4093b6 RS |
9257 | |
9258 | /* Strip any conversions, additions, and subtractions, and see if | |
9259 | we are returning the address of a local variable. Warn if so. */ | |
9260 | while (1) | |
8b6a5902 | 9261 | { |
3e4093b6 | 9262 | switch (TREE_CODE (inner)) |
8b6a5902 | 9263 | { |
849421a3 JJ |
9264 | CASE_CONVERT: |
9265 | case NON_LVALUE_EXPR: | |
3e4093b6 | 9266 | case PLUS_EXPR: |
849421a3 | 9267 | case POINTER_PLUS_EXPR: |
3e4093b6 RS |
9268 | inner = TREE_OPERAND (inner, 0); |
9269 | continue; | |
9270 | ||
9271 | case MINUS_EXPR: | |
9272 | /* If the second operand of the MINUS_EXPR has a pointer | |
9273 | type (or is converted from it), this may be valid, so | |
9274 | don't give a warning. */ | |
9275 | { | |
9276 | tree op1 = TREE_OPERAND (inner, 1); | |
8b6a5902 | 9277 | |
3f75a254 | 9278 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) |
1043771b TB |
9279 | && (CONVERT_EXPR_P (op1) |
9280 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
3e4093b6 | 9281 | op1 = TREE_OPERAND (op1, 0); |
8b6a5902 | 9282 | |
3e4093b6 RS |
9283 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
9284 | break; | |
8b6a5902 | 9285 | |
3e4093b6 RS |
9286 | inner = TREE_OPERAND (inner, 0); |
9287 | continue; | |
9288 | } | |
400fbf9f | 9289 | |
3e4093b6 RS |
9290 | case ADDR_EXPR: |
9291 | inner = TREE_OPERAND (inner, 0); | |
c2f4acb7 | 9292 | |
6615c446 | 9293 | while (REFERENCE_CLASS_P (inner) |
c22cacf3 | 9294 | && TREE_CODE (inner) != INDIRECT_REF) |
3e4093b6 | 9295 | inner = TREE_OPERAND (inner, 0); |
8b6a5902 | 9296 | |
a2f1f4c3 | 9297 | if (DECL_P (inner) |
3f75a254 JM |
9298 | && !DECL_EXTERNAL (inner) |
9299 | && !TREE_STATIC (inner) | |
3e4093b6 | 9300 | && DECL_CONTEXT (inner) == current_function_decl) |
19fc9faa MP |
9301 | { |
9302 | if (TREE_CODE (inner) == LABEL_DECL) | |
9303 | warning_at (loc, OPT_Wreturn_local_addr, | |
9304 | "function returns address of label"); | |
9305 | else | |
b4dfdc11 MG |
9306 | { |
9307 | warning_at (loc, OPT_Wreturn_local_addr, | |
9308 | "function returns address of local variable"); | |
9309 | tree zero = build_zero_cst (TREE_TYPE (res)); | |
9310 | t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero); | |
9311 | } | |
19fc9faa | 9312 | } |
3e4093b6 | 9313 | break; |
8b6a5902 | 9314 | |
3e4093b6 RS |
9315 | default: |
9316 | break; | |
9317 | } | |
de520661 | 9318 | |
3e4093b6 RS |
9319 | break; |
9320 | } | |
9321 | ||
53fb4de3 | 9322 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
c2255bc4 | 9323 | SET_EXPR_LOCATION (retval, loc); |
ca085fd7 MLI |
9324 | |
9325 | if (warn_sequence_point) | |
9326 | verify_sequence_points (retval); | |
de520661 | 9327 | } |
8b6a5902 | 9328 | |
c2255bc4 | 9329 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); |
0c9b182b JJ |
9330 | TREE_NO_WARNING (ret_stmt) |= no_warning; |
9331 | return add_stmt (ret_stmt); | |
de520661 | 9332 | } |
3e4093b6 RS |
9333 | \f |
9334 | struct c_switch { | |
604f5adf ILT |
9335 | /* The SWITCH_EXPR being built. */ |
9336 | tree switch_expr; | |
a6c0a76c | 9337 | |
89dbed81 | 9338 | /* The original type of the testing expression, i.e. before the |
a6c0a76c SB |
9339 | default conversion is applied. */ |
9340 | tree orig_type; | |
9341 | ||
3e4093b6 RS |
9342 | /* A splay-tree mapping the low element of a case range to the high |
9343 | element, or NULL_TREE if there is no high element. Used to | |
9344 | determine whether or not a new case label duplicates an old case | |
9345 | label. We need a tree, rather than simply a hash table, because | |
9346 | of the GNU case range extension. */ | |
9347 | splay_tree cases; | |
a6c0a76c | 9348 | |
e1b7793c ILT |
9349 | /* The bindings at the point of the switch. This is used for |
9350 | warnings crossing decls when branching to a case label. */ | |
9351 | struct c_spot_bindings *bindings; | |
187230a7 | 9352 | |
3e4093b6 RS |
9353 | /* The next node on the stack. */ |
9354 | struct c_switch *next; | |
9355 | }; | |
400fbf9f | 9356 | |
3e4093b6 RS |
9357 | /* A stack of the currently active switch statements. The innermost |
9358 | switch statement is on the top of the stack. There is no need to | |
9359 | mark the stack for garbage collection because it is only active | |
9360 | during the processing of the body of a function, and we never | |
9361 | collect at that point. */ | |
de520661 | 9362 | |
506e2710 | 9363 | struct c_switch *c_switch_stack; |
de520661 | 9364 | |
3e4093b6 | 9365 | /* Start a C switch statement, testing expression EXP. Return the new |
c2255bc4 | 9366 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. |
fedfecef MP |
9367 | SWITCH_COND_LOC is the location of the switch's condition. |
9368 | EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */ | |
de520661 | 9369 | |
3e4093b6 | 9370 | tree |
c2255bc4 AH |
9371 | c_start_case (location_t switch_loc, |
9372 | location_t switch_cond_loc, | |
fedfecef | 9373 | tree exp, bool explicit_cast_p) |
de520661 | 9374 | { |
c58e8676 | 9375 | tree orig_type = error_mark_node; |
3e4093b6 | 9376 | struct c_switch *cs; |
2f6e4e97 | 9377 | |
3e4093b6 | 9378 | if (exp != error_mark_node) |
de520661 | 9379 | { |
3e4093b6 RS |
9380 | orig_type = TREE_TYPE (exp); |
9381 | ||
c58e8676 | 9382 | if (!INTEGRAL_TYPE_P (orig_type)) |
de520661 | 9383 | { |
c58e8676 VR |
9384 | if (orig_type != error_mark_node) |
9385 | { | |
c2255bc4 | 9386 | error_at (switch_cond_loc, "switch quantity not an integer"); |
c58e8676 VR |
9387 | orig_type = error_mark_node; |
9388 | } | |
3e4093b6 | 9389 | exp = integer_zero_node; |
de520661 | 9390 | } |
3e4093b6 | 9391 | else |
de520661 | 9392 | { |
c58e8676 | 9393 | tree type = TYPE_MAIN_VARIANT (orig_type); |
fedfecef MP |
9394 | tree e = exp; |
9395 | ||
9396 | /* Warn if the condition has boolean value. */ | |
9397 | while (TREE_CODE (e) == COMPOUND_EXPR) | |
9398 | e = TREE_OPERAND (e, 1); | |
9399 | ||
9400 | if ((TREE_CODE (type) == BOOLEAN_TYPE | |
9401 | || truth_value_p (TREE_CODE (e))) | |
9402 | /* Explicit cast to int suppresses this warning. */ | |
9403 | && !(TREE_CODE (type) == INTEGER_TYPE | |
9404 | && explicit_cast_p)) | |
9405 | warning_at (switch_cond_loc, OPT_Wswitch_bool, | |
9406 | "switch condition has boolean value"); | |
8b6a5902 | 9407 | |
8400e75e | 9408 | if (!in_system_header_at (input_location) |
3e4093b6 RS |
9409 | && (type == long_integer_type_node |
9410 | || type == long_unsigned_type_node)) | |
c2255bc4 AH |
9411 | warning_at (switch_cond_loc, |
9412 | OPT_Wtraditional, "%<long%> switch expression not " | |
9413 | "converted to %<int%> in ISO C"); | |
8b6a5902 | 9414 | |
928c19bb | 9415 | exp = c_fully_fold (exp, false, NULL); |
3e4093b6 | 9416 | exp = default_conversion (exp); |
ca085fd7 MLI |
9417 | |
9418 | if (warn_sequence_point) | |
9419 | verify_sequence_points (exp); | |
3e4093b6 RS |
9420 | } |
9421 | } | |
9422 | ||
604f5adf | 9423 | /* Add this new SWITCH_EXPR to the stack. */ |
5d038c4c | 9424 | cs = XNEW (struct c_switch); |
604f5adf | 9425 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); |
c2255bc4 | 9426 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); |
a6c0a76c | 9427 | cs->orig_type = orig_type; |
3e4093b6 | 9428 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
e1b7793c | 9429 | cs->bindings = c_get_switch_bindings (); |
506e2710 RH |
9430 | cs->next = c_switch_stack; |
9431 | c_switch_stack = cs; | |
3e4093b6 | 9432 | |
604f5adf | 9433 | return add_stmt (cs->switch_expr); |
3e4093b6 RS |
9434 | } |
9435 | ||
c2255bc4 | 9436 | /* Process a case label at location LOC. */ |
3e4093b6 RS |
9437 | |
9438 | tree | |
c2255bc4 | 9439 | do_case (location_t loc, tree low_value, tree high_value) |
3e4093b6 RS |
9440 | { |
9441 | tree label = NULL_TREE; | |
9442 | ||
17cede2e JM |
9443 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) |
9444 | { | |
9445 | low_value = c_fully_fold (low_value, false, NULL); | |
9446 | if (TREE_CODE (low_value) == INTEGER_CST) | |
d033409e | 9447 | pedwarn (loc, OPT_Wpedantic, |
17cede2e JM |
9448 | "case label is not an integer constant expression"); |
9449 | } | |
9450 | ||
9451 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
9452 | { | |
9453 | high_value = c_fully_fold (high_value, false, NULL); | |
9454 | if (TREE_CODE (high_value) == INTEGER_CST) | |
c1771a20 | 9455 | pedwarn (input_location, OPT_Wpedantic, |
17cede2e JM |
9456 | "case label is not an integer constant expression"); |
9457 | } | |
9458 | ||
e1b7793c | 9459 | if (c_switch_stack == NULL) |
187230a7 JM |
9460 | { |
9461 | if (low_value) | |
e1b7793c | 9462 | error_at (loc, "case label not within a switch statement"); |
187230a7 | 9463 | else |
e1b7793c ILT |
9464 | error_at (loc, "%<default%> label not within a switch statement"); |
9465 | return NULL_TREE; | |
187230a7 | 9466 | } |
de520661 | 9467 | |
e1b7793c ILT |
9468 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, |
9469 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
9470 | loc)) | |
9471 | return NULL_TREE; | |
9472 | ||
9473 | label = c_add_case_label (loc, c_switch_stack->cases, | |
9474 | SWITCH_COND (c_switch_stack->switch_expr), | |
9475 | c_switch_stack->orig_type, | |
9476 | low_value, high_value); | |
9477 | if (label == error_mark_node) | |
9478 | label = NULL_TREE; | |
3e4093b6 RS |
9479 | return label; |
9480 | } | |
de520661 | 9481 | |
3e4093b6 | 9482 | /* Finish the switch statement. */ |
de520661 | 9483 | |
3e4093b6 | 9484 | void |
325c3691 | 9485 | c_finish_case (tree body) |
3e4093b6 | 9486 | { |
506e2710 | 9487 | struct c_switch *cs = c_switch_stack; |
fbc315db | 9488 | location_t switch_location; |
3e4093b6 | 9489 | |
604f5adf | 9490 | SWITCH_BODY (cs->switch_expr) = body; |
325c3691 | 9491 | |
6de9cd9a | 9492 | /* Emit warnings as needed. */ |
c2255bc4 | 9493 | switch_location = EXPR_LOCATION (cs->switch_expr); |
fbc315db ILT |
9494 | c_do_switch_warnings (cs->cases, switch_location, |
9495 | TREE_TYPE (cs->switch_expr), | |
9496 | SWITCH_COND (cs->switch_expr)); | |
6de9cd9a | 9497 | |
3e4093b6 | 9498 | /* Pop the stack. */ |
506e2710 | 9499 | c_switch_stack = cs->next; |
3e4093b6 | 9500 | splay_tree_delete (cs->cases); |
e1b7793c | 9501 | c_release_switch_bindings (cs->bindings); |
5d038c4c | 9502 | XDELETE (cs); |
de520661 | 9503 | } |
325c3691 | 9504 | \f |
506e2710 RH |
9505 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
9506 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
9507 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
9508 | statement, and was not surrounded with parenthesis. */ | |
325c3691 | 9509 | |
9e51cf9d | 9510 | void |
506e2710 RH |
9511 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
9512 | tree else_block, bool nested_if) | |
325c3691 | 9513 | { |
506e2710 | 9514 | tree stmt; |
325c3691 | 9515 | |
25c22937 BI |
9516 | /* If the condition has array notations, then the rank of the then_block and |
9517 | else_block must be either 0 or be equal to the rank of the condition. If | |
9518 | the condition does not have array notations then break them up as it is | |
9519 | broken up in a normal expression. */ | |
b72271b9 | 9520 | if (flag_cilkplus && contains_array_notation_expr (cond)) |
25c22937 BI |
9521 | { |
9522 | size_t then_rank = 0, cond_rank = 0, else_rank = 0; | |
9523 | if (!find_rank (if_locus, cond, cond, true, &cond_rank)) | |
9524 | return; | |
9525 | if (then_block | |
9526 | && !find_rank (if_locus, then_block, then_block, true, &then_rank)) | |
9527 | return; | |
9528 | if (else_block | |
9529 | && !find_rank (if_locus, else_block, else_block, true, &else_rank)) | |
9530 | return; | |
9531 | if (cond_rank != then_rank && then_rank != 0) | |
9532 | { | |
9533 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9534 | " and the then-block"); | |
9535 | return; | |
9536 | } | |
9537 | else if (cond_rank != else_rank && else_rank != 0) | |
9538 | { | |
9539 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9540 | " and the else-block"); | |
9541 | return; | |
9542 | } | |
9543 | } | |
506e2710 RH |
9544 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
9545 | if (warn_parentheses && nested_if && else_block == NULL) | |
325c3691 | 9546 | { |
506e2710 | 9547 | tree inner_if = then_block; |
16865eaa | 9548 | |
61ada8ae | 9549 | /* We know from the grammar productions that there is an IF nested |
506e2710 RH |
9550 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
9551 | it might not be exactly THEN_BLOCK, but should be the last | |
9552 | non-container statement within. */ | |
9553 | while (1) | |
9554 | switch (TREE_CODE (inner_if)) | |
9555 | { | |
9556 | case COND_EXPR: | |
9557 | goto found; | |
9558 | case BIND_EXPR: | |
9559 | inner_if = BIND_EXPR_BODY (inner_if); | |
9560 | break; | |
9561 | case STATEMENT_LIST: | |
9562 | inner_if = expr_last (then_block); | |
9563 | break; | |
9564 | case TRY_FINALLY_EXPR: | |
9565 | case TRY_CATCH_EXPR: | |
9566 | inner_if = TREE_OPERAND (inner_if, 0); | |
9567 | break; | |
9568 | default: | |
366de0ce | 9569 | gcc_unreachable (); |
506e2710 RH |
9570 | } |
9571 | found: | |
16865eaa | 9572 | |
506e2710 | 9573 | if (COND_EXPR_ELSE (inner_if)) |
fab922b1 MLI |
9574 | warning_at (if_locus, OPT_Wparentheses, |
9575 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
506e2710 | 9576 | } |
16865eaa | 9577 | |
2214de30 | 9578 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); |
a281759f | 9579 | SET_EXPR_LOCATION (stmt, if_locus); |
506e2710 | 9580 | add_stmt (stmt); |
325c3691 RH |
9581 | } |
9582 | ||
506e2710 RH |
9583 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
9584 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
9585 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
61ada8ae | 9586 | the statement controlled by the loop. BLAB is the break label. CLAB is |
506e2710 | 9587 | the continue label. Everything is allowed to be NULL. */ |
325c3691 RH |
9588 | |
9589 | void | |
506e2710 RH |
9590 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
9591 | tree blab, tree clab, bool cond_is_first) | |
325c3691 | 9592 | { |
506e2710 RH |
9593 | tree entry = NULL, exit = NULL, t; |
9594 | ||
b72271b9 | 9595 | if (flag_cilkplus && contains_array_notation_expr (cond)) |
36536d79 BI |
9596 | { |
9597 | error_at (start_locus, "array notation expression cannot be used in a " | |
9598 | "loop%'s condition"); | |
9599 | return; | |
9600 | } | |
9601 | ||
28af952a RS |
9602 | /* If the condition is zero don't generate a loop construct. */ |
9603 | if (cond && integer_zerop (cond)) | |
9604 | { | |
9605 | if (cond_is_first) | |
9606 | { | |
9607 | t = build_and_jump (&blab); | |
9608 | SET_EXPR_LOCATION (t, start_locus); | |
9609 | add_stmt (t); | |
9610 | } | |
9611 | } | |
9612 | else | |
506e2710 RH |
9613 | { |
9614 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
c22cacf3 | 9615 | |
506e2710 | 9616 | /* If we have an exit condition, then we build an IF with gotos either |
c22cacf3 MS |
9617 | out of the loop, or to the top of it. If there's no exit condition, |
9618 | then we just build a jump back to the top. */ | |
506e2710 | 9619 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); |
c22cacf3 | 9620 | |
28af952a | 9621 | if (cond && !integer_nonzerop (cond)) |
c22cacf3 MS |
9622 | { |
9623 | /* Canonicalize the loop condition to the end. This means | |
9624 | generating a branch to the loop condition. Reuse the | |
9625 | continue label, if possible. */ | |
9626 | if (cond_is_first) | |
9627 | { | |
9628 | if (incr || !clab) | |
9629 | { | |
9630 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
9631 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
9632 | } | |
9633 | else | |
9634 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
a281759f | 9635 | SET_EXPR_LOCATION (t, start_locus); |
c22cacf3 MS |
9636 | add_stmt (t); |
9637 | } | |
9638 | ||
506e2710 | 9639 | t = build_and_jump (&blab); |
506e2710 | 9640 | if (cond_is_first) |
db3927fb AH |
9641 | exit = fold_build3_loc (start_locus, |
9642 | COND_EXPR, void_type_node, cond, exit, t); | |
506e2710 | 9643 | else |
db3927fb AH |
9644 | exit = fold_build3_loc (input_location, |
9645 | COND_EXPR, void_type_node, cond, exit, t); | |
c22cacf3 MS |
9646 | } |
9647 | ||
506e2710 RH |
9648 | add_stmt (top); |
9649 | } | |
c22cacf3 | 9650 | |
506e2710 RH |
9651 | if (body) |
9652 | add_stmt (body); | |
9653 | if (clab) | |
9654 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
9655 | if (incr) | |
9656 | add_stmt (incr); | |
9657 | if (entry) | |
9658 | add_stmt (entry); | |
9659 | if (exit) | |
9660 | add_stmt (exit); | |
9661 | if (blab) | |
9662 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
325c3691 | 9663 | } |
325c3691 RH |
9664 | |
9665 | tree | |
c2255bc4 | 9666 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) |
325c3691 | 9667 | { |
089efaa4 | 9668 | bool skip; |
506e2710 | 9669 | tree label = *label_p; |
325c3691 | 9670 | |
089efaa4 ILT |
9671 | /* In switch statements break is sometimes stylistically used after |
9672 | a return statement. This can lead to spurious warnings about | |
9673 | control reaching the end of a non-void function when it is | |
9674 | inlined. Note that we are calling block_may_fallthru with | |
9675 | language specific tree nodes; this works because | |
9676 | block_may_fallthru returns true when given something it does not | |
9677 | understand. */ | |
9678 | skip = !block_may_fallthru (cur_stmt_list); | |
9679 | ||
506e2710 | 9680 | if (!label) |
089efaa4 ILT |
9681 | { |
9682 | if (!skip) | |
c2255bc4 | 9683 | *label_p = label = create_artificial_label (loc); |
089efaa4 | 9684 | } |
953ff289 DN |
9685 | else if (TREE_CODE (label) == LABEL_DECL) |
9686 | ; | |
9687 | else switch (TREE_INT_CST_LOW (label)) | |
506e2710 | 9688 | { |
953ff289 | 9689 | case 0: |
506e2710 | 9690 | if (is_break) |
c2255bc4 | 9691 | error_at (loc, "break statement not within loop or switch"); |
506e2710 | 9692 | else |
c2255bc4 | 9693 | error_at (loc, "continue statement not within a loop"); |
506e2710 | 9694 | return NULL_TREE; |
953ff289 DN |
9695 | |
9696 | case 1: | |
9697 | gcc_assert (is_break); | |
c2255bc4 | 9698 | error_at (loc, "break statement used with OpenMP for loop"); |
953ff289 DN |
9699 | return NULL_TREE; |
9700 | ||
c02065fc AH |
9701 | case 2: |
9702 | if (is_break) | |
9703 | error ("break statement within %<#pragma simd%> loop body"); | |
9704 | else | |
9705 | error ("continue statement within %<#pragma simd%> loop body"); | |
9706 | return NULL_TREE; | |
9707 | ||
953ff289 DN |
9708 | default: |
9709 | gcc_unreachable (); | |
506e2710 | 9710 | } |
325c3691 | 9711 | |
089efaa4 ILT |
9712 | if (skip) |
9713 | return NULL_TREE; | |
9714 | ||
2e28e797 JH |
9715 | if (!is_break) |
9716 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
9717 | ||
53fb4de3 | 9718 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
325c3691 RH |
9719 | } |
9720 | ||
506e2710 | 9721 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
3a5b9284 RH |
9722 | |
9723 | static void | |
c2255bc4 | 9724 | emit_side_effect_warnings (location_t loc, tree expr) |
3a5b9284 | 9725 | { |
e6b5a630 RH |
9726 | if (expr == error_mark_node) |
9727 | ; | |
9728 | else if (!TREE_SIDE_EFFECTS (expr)) | |
3a5b9284 RH |
9729 | { |
9730 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
c2255bc4 | 9731 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); |
3a5b9284 | 9732 | } |
789eadcd MP |
9733 | else if (TREE_CODE (expr) == COMPOUND_EXPR) |
9734 | { | |
9735 | tree r = expr; | |
9736 | location_t cloc = loc; | |
9737 | while (TREE_CODE (r) == COMPOUND_EXPR) | |
9738 | { | |
9739 | if (EXPR_HAS_LOCATION (r)) | |
9740 | cloc = EXPR_LOCATION (r); | |
9741 | r = TREE_OPERAND (r, 1); | |
9742 | } | |
9743 | if (!TREE_SIDE_EFFECTS (r) | |
9744 | && !VOID_TYPE_P (TREE_TYPE (r)) | |
9745 | && !CONVERT_EXPR_P (r) | |
cc28fc7f | 9746 | && !TREE_NO_WARNING (r) |
789eadcd MP |
9747 | && !TREE_NO_WARNING (expr)) |
9748 | warning_at (cloc, OPT_Wunused_value, | |
9749 | "right-hand operand of comma expression has no effect"); | |
9750 | } | |
27f33b15 | 9751 | else |
c2255bc4 | 9752 | warn_if_unused_value (expr, loc); |
3a5b9284 RH |
9753 | } |
9754 | ||
506e2710 | 9755 | /* Process an expression as if it were a complete statement. Emit |
c2255bc4 AH |
9756 | diagnostics, but do not call ADD_STMT. LOC is the location of the |
9757 | statement. */ | |
3a5b9284 | 9758 | |
506e2710 | 9759 | tree |
c2255bc4 | 9760 | c_process_expr_stmt (location_t loc, tree expr) |
3a5b9284 | 9761 | { |
056928b2 JJ |
9762 | tree exprv; |
9763 | ||
3a5b9284 | 9764 | if (!expr) |
506e2710 | 9765 | return NULL_TREE; |
3a5b9284 | 9766 | |
928c19bb JM |
9767 | expr = c_fully_fold (expr, false, NULL); |
9768 | ||
3a5b9284 RH |
9769 | if (warn_sequence_point) |
9770 | verify_sequence_points (expr); | |
9771 | ||
9772 | if (TREE_TYPE (expr) != error_mark_node | |
9773 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
9774 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
c2255bc4 | 9775 | error_at (loc, "expression statement has incomplete type"); |
3a5b9284 RH |
9776 | |
9777 | /* If we're not processing a statement expression, warn about unused values. | |
9778 | Warnings for statement expressions will be emitted later, once we figure | |
9779 | out which is the result. */ | |
9780 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
27f33b15 | 9781 | && warn_unused_value) |
c2255bc4 | 9782 | emit_side_effect_warnings (loc, expr); |
3a5b9284 | 9783 | |
056928b2 JJ |
9784 | exprv = expr; |
9785 | while (TREE_CODE (exprv) == COMPOUND_EXPR) | |
9786 | exprv = TREE_OPERAND (exprv, 1); | |
f1a89dd0 JJ |
9787 | while (CONVERT_EXPR_P (exprv)) |
9788 | exprv = TREE_OPERAND (exprv, 0); | |
9789 | if (DECL_P (exprv) | |
9790 | || handled_component_p (exprv) | |
9791 | || TREE_CODE (exprv) == ADDR_EXPR) | |
056928b2 | 9792 | mark_exp_read (exprv); |
fa8351f8 | 9793 | |
3a5b9284 RH |
9794 | /* If the expression is not of a type to which we cannot assign a line |
9795 | number, wrap the thing in a no-op NOP_EXPR. */ | |
6615c446 | 9796 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) |
c2255bc4 AH |
9797 | { |
9798 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
9799 | SET_EXPR_LOCATION (expr, loc); | |
9800 | } | |
506e2710 RH |
9801 | |
9802 | return expr; | |
9803 | } | |
9804 | ||
c2255bc4 AH |
9805 | /* Emit an expression as a statement. LOC is the location of the |
9806 | expression. */ | |
506e2710 RH |
9807 | |
9808 | tree | |
c2255bc4 | 9809 | c_finish_expr_stmt (location_t loc, tree expr) |
506e2710 RH |
9810 | { |
9811 | if (expr) | |
c2255bc4 | 9812 | return add_stmt (c_process_expr_stmt (loc, expr)); |
506e2710 RH |
9813 | else |
9814 | return NULL; | |
3a5b9284 RH |
9815 | } |
9816 | ||
9817 | /* Do the opposite and emit a statement as an expression. To begin, | |
9818 | create a new binding level and return it. */ | |
325c3691 RH |
9819 | |
9820 | tree | |
9821 | c_begin_stmt_expr (void) | |
9822 | { | |
9823 | tree ret; | |
9824 | ||
9825 | /* We must force a BLOCK for this level so that, if it is not expanded | |
9826 | later, there is a way to turn off the entire subtree of blocks that | |
9827 | are contained in it. */ | |
9828 | keep_next_level (); | |
9829 | ret = c_begin_compound_stmt (true); | |
e1b7793c ILT |
9830 | |
9831 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
9832 | ? NULL | |
9833 | : c_switch_stack->bindings); | |
325c3691 RH |
9834 | |
9835 | /* Mark the current statement list as belonging to a statement list. */ | |
9836 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
9837 | ||
9838 | return ret; | |
9839 | } | |
9840 | ||
c2255bc4 AH |
9841 | /* LOC is the location of the compound statement to which this body |
9842 | belongs. */ | |
9843 | ||
325c3691 | 9844 | tree |
c2255bc4 | 9845 | c_finish_stmt_expr (location_t loc, tree body) |
325c3691 | 9846 | { |
3a5b9284 | 9847 | tree last, type, tmp, val; |
325c3691 RH |
9848 | tree *last_p; |
9849 | ||
c2255bc4 | 9850 | body = c_end_compound_stmt (loc, body, true); |
e1b7793c ILT |
9851 | |
9852 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
9853 | ? NULL | |
9854 | : c_switch_stack->bindings); | |
325c3691 | 9855 | |
3a5b9284 RH |
9856 | /* Locate the last statement in BODY. See c_end_compound_stmt |
9857 | about always returning a BIND_EXPR. */ | |
9858 | last_p = &BIND_EXPR_BODY (body); | |
9859 | last = BIND_EXPR_BODY (body); | |
9860 | ||
9861 | continue_searching: | |
325c3691 RH |
9862 | if (TREE_CODE (last) == STATEMENT_LIST) |
9863 | { | |
3a5b9284 RH |
9864 | tree_stmt_iterator i; |
9865 | ||
9866 | /* This can happen with degenerate cases like ({ }). No value. */ | |
9867 | if (!TREE_SIDE_EFFECTS (last)) | |
9868 | return body; | |
9869 | ||
9870 | /* If we're supposed to generate side effects warnings, process | |
9871 | all of the statements except the last. */ | |
27f33b15 | 9872 | if (warn_unused_value) |
325c3691 | 9873 | { |
3a5b9284 | 9874 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
c2255bc4 AH |
9875 | { |
9876 | location_t tloc; | |
9877 | tree t = tsi_stmt (i); | |
9878 | ||
9879 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
9880 | emit_side_effect_warnings (tloc, t); | |
9881 | } | |
325c3691 RH |
9882 | } |
9883 | else | |
3a5b9284 RH |
9884 | i = tsi_last (last); |
9885 | last_p = tsi_stmt_ptr (i); | |
9886 | last = *last_p; | |
325c3691 RH |
9887 | } |
9888 | ||
3a5b9284 RH |
9889 | /* If the end of the list is exception related, then the list was split |
9890 | by a call to push_cleanup. Continue searching. */ | |
9891 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
9892 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
9893 | { | |
9894 | last_p = &TREE_OPERAND (last, 0); | |
9895 | last = *last_p; | |
9896 | goto continue_searching; | |
9897 | } | |
9898 | ||
26d8af35 JM |
9899 | if (last == error_mark_node) |
9900 | return last; | |
9901 | ||
3a5b9284 RH |
9902 | /* In the case that the BIND_EXPR is not necessary, return the |
9903 | expression out from inside it. */ | |
26d8af35 JM |
9904 | if (last == BIND_EXPR_BODY (body) |
9905 | && BIND_EXPR_VARS (body) == NULL) | |
591baeb0 | 9906 | { |
928c19bb JM |
9907 | /* Even if this looks constant, do not allow it in a constant |
9908 | expression. */ | |
e5a94231 | 9909 | last = c_wrap_maybe_const (last, true); |
591baeb0 JM |
9910 | /* Do not warn if the return value of a statement expression is |
9911 | unused. */ | |
928c19bb | 9912 | TREE_NO_WARNING (last) = 1; |
591baeb0 JM |
9913 | return last; |
9914 | } | |
325c3691 RH |
9915 | |
9916 | /* Extract the type of said expression. */ | |
9917 | type = TREE_TYPE (last); | |
325c3691 | 9918 | |
3a5b9284 RH |
9919 | /* If we're not returning a value at all, then the BIND_EXPR that |
9920 | we already have is a fine expression to return. */ | |
9921 | if (!type || VOID_TYPE_P (type)) | |
9922 | return body; | |
9923 | ||
9924 | /* Now that we've located the expression containing the value, it seems | |
9925 | silly to make voidify_wrapper_expr repeat the process. Create a | |
9926 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
9927 | tmp = create_tmp_var_raw (type, NULL); | |
9928 | ||
9929 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
9930 | tree_expr_nonnegative_p giving up immediately. */ | |
9931 | val = last; | |
9932 | if (TREE_CODE (val) == NOP_EXPR | |
9933 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
9934 | val = TREE_OPERAND (val, 0); | |
9935 | ||
53fb4de3 | 9936 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
5e278028 | 9937 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); |
3a5b9284 | 9938 | |
c2255bc4 AH |
9939 | { |
9940 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
9941 | SET_EXPR_LOCATION (t, loc); | |
9942 | return t; | |
9943 | } | |
325c3691 RH |
9944 | } |
9945 | \f | |
9946 | /* Begin and end compound statements. This is as simple as pushing | |
9947 | and popping new statement lists from the tree. */ | |
9948 | ||
9949 | tree | |
9950 | c_begin_compound_stmt (bool do_scope) | |
9951 | { | |
9952 | tree stmt = push_stmt_list (); | |
9953 | if (do_scope) | |
4dfa0342 | 9954 | push_scope (); |
325c3691 RH |
9955 | return stmt; |
9956 | } | |
9957 | ||
c2255bc4 AH |
9958 | /* End a compound statement. STMT is the statement. LOC is the |
9959 | location of the compound statement-- this is usually the location | |
9960 | of the opening brace. */ | |
9961 | ||
325c3691 | 9962 | tree |
c2255bc4 | 9963 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) |
325c3691 RH |
9964 | { |
9965 | tree block = NULL; | |
9966 | ||
9967 | if (do_scope) | |
9968 | { | |
9969 | if (c_dialect_objc ()) | |
9970 | objc_clear_super_receiver (); | |
9971 | block = pop_scope (); | |
9972 | } | |
9973 | ||
9974 | stmt = pop_stmt_list (stmt); | |
c2255bc4 | 9975 | stmt = c_build_bind_expr (loc, block, stmt); |
325c3691 RH |
9976 | |
9977 | /* If this compound statement is nested immediately inside a statement | |
9978 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
9979 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
9980 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
9981 | was really last. */ | |
38e01f9e | 9982 | if (building_stmt_list_p () |
325c3691 RH |
9983 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) |
9984 | && TREE_CODE (stmt) != BIND_EXPR) | |
9985 | { | |
53fb4de3 | 9986 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
325c3691 | 9987 | TREE_SIDE_EFFECTS (stmt) = 1; |
c2255bc4 | 9988 | SET_EXPR_LOCATION (stmt, loc); |
325c3691 RH |
9989 | } |
9990 | ||
9991 | return stmt; | |
9992 | } | |
5a508662 RH |
9993 | |
9994 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
9995 | when the current scope is exited. EH_ONLY is true when this is not | |
9996 | meant to apply to normal control flow transfer. */ | |
9997 | ||
9998 | void | |
c2255bc4 | 9999 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
5a508662 | 10000 | { |
3a5b9284 RH |
10001 | enum tree_code code; |
10002 | tree stmt, list; | |
10003 | bool stmt_expr; | |
10004 | ||
10005 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
c2255bc4 | 10006 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); |
5a508662 | 10007 | add_stmt (stmt); |
3a5b9284 RH |
10008 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
10009 | list = push_stmt_list (); | |
10010 | TREE_OPERAND (stmt, 0) = list; | |
10011 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
5a508662 | 10012 | } |
325c3691 | 10013 | \f |
3e4093b6 RS |
10014 | /* Build a binary-operation expression without default conversions. |
10015 | CODE is the kind of expression to build. | |
ba47d38d | 10016 | LOCATION is the operator's location. |
3e4093b6 RS |
10017 | This function differs from `build' in several ways: |
10018 | the data type of the result is computed and recorded in it, | |
10019 | warnings are generated if arg data types are invalid, | |
10020 | special handling for addition and subtraction of pointers is known, | |
10021 | and some optimization is done (operations on narrow ints | |
10022 | are done in the narrower type when that gives the same result). | |
10023 | Constant folding is also done before the result is returned. | |
de520661 | 10024 | |
3e4093b6 RS |
10025 | Note that the operands will never have enumeral types, or function |
10026 | or array types, because either they will have the default conversions | |
10027 | performed or they have both just been converted to some other type in which | |
10028 | the arithmetic is to be done. */ | |
10029 | ||
10030 | tree | |
ba47d38d AH |
10031 | build_binary_op (location_t location, enum tree_code code, |
10032 | tree orig_op0, tree orig_op1, int convert_p) | |
de520661 | 10033 | { |
8ce94e44 JM |
10034 | tree type0, type1, orig_type0, orig_type1; |
10035 | tree eptype; | |
3e4093b6 RS |
10036 | enum tree_code code0, code1; |
10037 | tree op0, op1; | |
c9f9eb5d | 10038 | tree ret = error_mark_node; |
4de67c26 | 10039 | const char *invalid_op_diag; |
4d84fe7c | 10040 | bool op0_int_operands, op1_int_operands; |
928c19bb | 10041 | bool int_const, int_const_or_overflow, int_operands; |
b62acd60 | 10042 | |
3e4093b6 RS |
10043 | /* Expression code to give to the expression when it is built. |
10044 | Normally this is CODE, which is what the caller asked for, | |
10045 | but in some special cases we change it. */ | |
10046 | enum tree_code resultcode = code; | |
8b6a5902 | 10047 | |
3e4093b6 RS |
10048 | /* Data type in which the computation is to be performed. |
10049 | In the simplest cases this is the common type of the arguments. */ | |
10050 | tree result_type = NULL; | |
10051 | ||
8ce94e44 JM |
10052 | /* When the computation is in excess precision, the type of the |
10053 | final EXCESS_PRECISION_EXPR. */ | |
2d2e923f | 10054 | tree semantic_result_type = NULL; |
8ce94e44 | 10055 | |
3e4093b6 RS |
10056 | /* Nonzero means operands have already been type-converted |
10057 | in whatever way is necessary. | |
10058 | Zero means they need to be converted to RESULT_TYPE. */ | |
10059 | int converted = 0; | |
10060 | ||
10061 | /* Nonzero means create the expression with this type, rather than | |
10062 | RESULT_TYPE. */ | |
10063 | tree build_type = 0; | |
10064 | ||
10065 | /* Nonzero means after finally constructing the expression | |
10066 | convert it to this type. */ | |
10067 | tree final_type = 0; | |
10068 | ||
10069 | /* Nonzero if this is an operation like MIN or MAX which can | |
10070 | safely be computed in short if both args are promoted shorts. | |
10071 | Also implies COMMON. | |
10072 | -1 indicates a bitwise operation; this makes a difference | |
10073 | in the exact conditions for when it is safe to do the operation | |
10074 | in a narrower mode. */ | |
10075 | int shorten = 0; | |
10076 | ||
10077 | /* Nonzero if this is a comparison operation; | |
10078 | if both args are promoted shorts, compare the original shorts. | |
10079 | Also implies COMMON. */ | |
10080 | int short_compare = 0; | |
10081 | ||
10082 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
10083 | original short and then promoted if the operand is a promoted short. */ | |
10084 | int short_shift = 0; | |
10085 | ||
10086 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
10087 | int common = 0; | |
10088 | ||
58393038 ZL |
10089 | /* True means types are compatible as far as ObjC is concerned. */ |
10090 | bool objc_ok; | |
10091 | ||
8ce94e44 JM |
10092 | /* True means this is an arithmetic operation that may need excess |
10093 | precision. */ | |
10094 | bool may_need_excess_precision; | |
10095 | ||
180f8dbb JM |
10096 | /* True means this is a boolean operation that converts both its |
10097 | operands to truth-values. */ | |
10098 | bool boolean_op = false; | |
10099 | ||
de5a5fa1 MP |
10100 | /* Remember whether we're doing / or %. */ |
10101 | bool doing_div_or_mod = false; | |
10102 | ||
10103 | /* Remember whether we're doing << or >>. */ | |
10104 | bool doing_shift = false; | |
10105 | ||
10106 | /* Tree holding instrumentation expression. */ | |
10107 | tree instrument_expr = NULL; | |
10108 | ||
ba47d38d AH |
10109 | if (location == UNKNOWN_LOCATION) |
10110 | location = input_location; | |
10111 | ||
4d84fe7c JM |
10112 | op0 = orig_op0; |
10113 | op1 = orig_op1; | |
10114 | ||
10115 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
10116 | if (op0_int_operands) | |
10117 | op0 = remove_c_maybe_const_expr (op0); | |
10118 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
10119 | if (op1_int_operands) | |
10120 | op1 = remove_c_maybe_const_expr (op1); | |
10121 | int_operands = (op0_int_operands && op1_int_operands); | |
928c19bb JM |
10122 | if (int_operands) |
10123 | { | |
10124 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
10125 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
10126 | int_const = (int_const_or_overflow | |
10127 | && !TREE_OVERFLOW (orig_op0) | |
10128 | && !TREE_OVERFLOW (orig_op1)); | |
10129 | } | |
10130 | else | |
10131 | int_const = int_const_or_overflow = false; | |
10132 | ||
0e3a99ae | 10133 | /* Do not apply default conversion in mixed vector/scalar expression. */ |
f90e8e2e AS |
10134 | if (convert_p |
10135 | && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE) | |
0e3a99ae | 10136 | != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE))) |
790e9490 | 10137 | { |
4d84fe7c JM |
10138 | op0 = default_conversion (op0); |
10139 | op1 = default_conversion (op1); | |
790e9490 RS |
10140 | } |
10141 | ||
36536d79 BI |
10142 | /* When Cilk Plus is enabled and there are array notations inside op0, then |
10143 | we check to see if there are builtin array notation functions. If | |
10144 | so, then we take on the type of the array notation inside it. */ | |
b72271b9 | 10145 | if (flag_cilkplus && contains_array_notation_expr (op0)) |
36536d79 BI |
10146 | orig_type0 = type0 = find_correct_array_notation_type (op0); |
10147 | else | |
10148 | orig_type0 = type0 = TREE_TYPE (op0); | |
10149 | ||
b72271b9 | 10150 | if (flag_cilkplus && contains_array_notation_expr (op1)) |
36536d79 BI |
10151 | orig_type1 = type1 = find_correct_array_notation_type (op1); |
10152 | else | |
10153 | orig_type1 = type1 = TREE_TYPE (op1); | |
91fa3c30 | 10154 | |
3e4093b6 RS |
10155 | /* The expression codes of the data types of the arguments tell us |
10156 | whether the arguments are integers, floating, pointers, etc. */ | |
10157 | code0 = TREE_CODE (type0); | |
10158 | code1 = TREE_CODE (type1); | |
10159 | ||
10160 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
10161 | STRIP_TYPE_NOPS (op0); | |
10162 | STRIP_TYPE_NOPS (op1); | |
10163 | ||
10164 | /* If an error was already reported for one of the arguments, | |
10165 | avoid reporting another error. */ | |
10166 | ||
10167 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
10168 | return error_mark_node; | |
10169 | ||
4de67c26 JM |
10170 | if ((invalid_op_diag |
10171 | = targetm.invalid_binary_op (code, type0, type1))) | |
10172 | { | |
ba47d38d | 10173 | error_at (location, invalid_op_diag); |
4de67c26 JM |
10174 | return error_mark_node; |
10175 | } | |
10176 | ||
8ce94e44 JM |
10177 | switch (code) |
10178 | { | |
10179 | case PLUS_EXPR: | |
10180 | case MINUS_EXPR: | |
10181 | case MULT_EXPR: | |
10182 | case TRUNC_DIV_EXPR: | |
10183 | case CEIL_DIV_EXPR: | |
10184 | case FLOOR_DIV_EXPR: | |
10185 | case ROUND_DIV_EXPR: | |
10186 | case EXACT_DIV_EXPR: | |
10187 | may_need_excess_precision = true; | |
10188 | break; | |
10189 | default: | |
10190 | may_need_excess_precision = false; | |
10191 | break; | |
10192 | } | |
10193 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
10194 | { | |
10195 | op0 = TREE_OPERAND (op0, 0); | |
10196 | type0 = TREE_TYPE (op0); | |
10197 | } | |
10198 | else if (may_need_excess_precision | |
10199 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
10200 | { | |
10201 | type0 = eptype; | |
10202 | op0 = convert (eptype, op0); | |
10203 | } | |
10204 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
10205 | { | |
10206 | op1 = TREE_OPERAND (op1, 0); | |
10207 | type1 = TREE_TYPE (op1); | |
10208 | } | |
10209 | else if (may_need_excess_precision | |
10210 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
10211 | { | |
10212 | type1 = eptype; | |
10213 | op1 = convert (eptype, op1); | |
10214 | } | |
10215 | ||
58393038 ZL |
10216 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); |
10217 | ||
0e3a99ae AS |
10218 | /* In case when one of the operands of the binary operation is |
10219 | a vector and another is a scalar -- convert scalar to vector. */ | |
10220 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
10221 | { | |
a212e43f MG |
10222 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, |
10223 | true); | |
f90e8e2e | 10224 | |
0e3a99ae AS |
10225 | switch (convert_flag) |
10226 | { | |
10227 | case stv_error: | |
10228 | return error_mark_node; | |
10229 | case stv_firstarg: | |
10230 | { | |
10231 | bool maybe_const = true; | |
10232 | tree sc; | |
10233 | sc = c_fully_fold (op0, false, &maybe_const); | |
10234 | sc = save_expr (sc); | |
10235 | sc = convert (TREE_TYPE (type1), sc); | |
10236 | op0 = build_vector_from_val (type1, sc); | |
10237 | if (!maybe_const) | |
10238 | op0 = c_wrap_maybe_const (op0, true); | |
10239 | orig_type0 = type0 = TREE_TYPE (op0); | |
10240 | code0 = TREE_CODE (type0); | |
10241 | converted = 1; | |
10242 | break; | |
10243 | } | |
10244 | case stv_secondarg: | |
10245 | { | |
10246 | bool maybe_const = true; | |
10247 | tree sc; | |
10248 | sc = c_fully_fold (op1, false, &maybe_const); | |
10249 | sc = save_expr (sc); | |
10250 | sc = convert (TREE_TYPE (type0), sc); | |
10251 | op1 = build_vector_from_val (type0, sc); | |
10252 | if (!maybe_const) | |
54b9f838 | 10253 | op1 = c_wrap_maybe_const (op1, true); |
0e3a99ae AS |
10254 | orig_type1 = type1 = TREE_TYPE (op1); |
10255 | code1 = TREE_CODE (type1); | |
10256 | converted = 1; | |
10257 | break; | |
10258 | } | |
10259 | default: | |
10260 | break; | |
10261 | } | |
10262 | } | |
10263 | ||
3e4093b6 | 10264 | switch (code) |
de520661 | 10265 | { |
3e4093b6 RS |
10266 | case PLUS_EXPR: |
10267 | /* Handle the pointer + int case. */ | |
10268 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 10269 | { |
db3927fb | 10270 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); |
c9f9eb5d AH |
10271 | goto return_build_binary_op; |
10272 | } | |
3e4093b6 | 10273 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) |
c9f9eb5d | 10274 | { |
db3927fb | 10275 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); |
c9f9eb5d AH |
10276 | goto return_build_binary_op; |
10277 | } | |
fe67cf58 | 10278 | else |
3e4093b6 RS |
10279 | common = 1; |
10280 | break; | |
400fbf9f | 10281 | |
3e4093b6 RS |
10282 | case MINUS_EXPR: |
10283 | /* Subtraction of two similar pointers. | |
10284 | We must subtract them as integers, then divide by object size. */ | |
10285 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
744aa42f | 10286 | && comp_target_types (location, type0, type1)) |
c9f9eb5d | 10287 | { |
db3927fb | 10288 | ret = pointer_diff (location, op0, op1); |
c9f9eb5d AH |
10289 | goto return_build_binary_op; |
10290 | } | |
3e4093b6 RS |
10291 | /* Handle pointer minus int. Just like pointer plus int. */ |
10292 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 10293 | { |
db3927fb | 10294 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); |
c9f9eb5d AH |
10295 | goto return_build_binary_op; |
10296 | } | |
3e4093b6 RS |
10297 | else |
10298 | common = 1; | |
10299 | break; | |
8b6a5902 | 10300 | |
3e4093b6 RS |
10301 | case MULT_EXPR: |
10302 | common = 1; | |
10303 | break; | |
10304 | ||
10305 | case TRUNC_DIV_EXPR: | |
10306 | case CEIL_DIV_EXPR: | |
10307 | case FLOOR_DIV_EXPR: | |
10308 | case ROUND_DIV_EXPR: | |
10309 | case EXACT_DIV_EXPR: | |
de5a5fa1 | 10310 | doing_div_or_mod = true; |
c9f9eb5d | 10311 | warn_for_div_by_zero (location, op1); |
3e4093b6 RS |
10312 | |
10313 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 10314 | || code0 == FIXED_POINT_TYPE |
3e4093b6 RS |
10315 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
10316 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
ab22c1fa | 10317 | || code1 == FIXED_POINT_TYPE |
3e4093b6 | 10318 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 10319 | { |
5bed876a AH |
10320 | enum tree_code tcode0 = code0, tcode1 = code1; |
10321 | ||
3a021db2 | 10322 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
5bed876a | 10323 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
3a021db2 | 10324 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) |
5bed876a | 10325 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
3a021db2 | 10326 | |
ab22c1fa CF |
10327 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) |
10328 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
3e4093b6 RS |
10329 | resultcode = RDIV_EXPR; |
10330 | else | |
10331 | /* Although it would be tempting to shorten always here, that | |
10332 | loses on some targets, since the modulo instruction is | |
10333 | undefined if the quotient can't be represented in the | |
10334 | computation mode. We shorten only if unsigned or if | |
10335 | dividing by something we know != -1. */ | |
8df83eae | 10336 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 10337 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 10338 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
10339 | common = 1; |
10340 | } | |
10341 | break; | |
de520661 | 10342 | |
3e4093b6 | 10343 | case BIT_AND_EXPR: |
3e4093b6 RS |
10344 | case BIT_IOR_EXPR: |
10345 | case BIT_XOR_EXPR: | |
10346 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
10347 | shorten = -1; | |
9ef0c8d9 AP |
10348 | /* Allow vector types which are not floating point types. */ |
10349 | else if (code0 == VECTOR_TYPE | |
10350 | && code1 == VECTOR_TYPE | |
10351 | && !VECTOR_FLOAT_TYPE_P (type0) | |
10352 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
3e4093b6 RS |
10353 | common = 1; |
10354 | break; | |
10355 | ||
10356 | case TRUNC_MOD_EXPR: | |
10357 | case FLOOR_MOD_EXPR: | |
de5a5fa1 | 10358 | doing_div_or_mod = true; |
c9f9eb5d | 10359 | warn_for_div_by_zero (location, op1); |
de520661 | 10360 | |
5cfd5d9b AP |
10361 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
10362 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
10363 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
10364 | common = 1; | |
10365 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3e4093b6 RS |
10366 | { |
10367 | /* Although it would be tempting to shorten always here, that loses | |
10368 | on some targets, since the modulo instruction is undefined if the | |
10369 | quotient can't be represented in the computation mode. We shorten | |
10370 | only if unsigned or if dividing by something we know != -1. */ | |
8df83eae | 10371 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 10372 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 10373 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
10374 | common = 1; |
10375 | } | |
10376 | break; | |
de520661 | 10377 | |
3e4093b6 RS |
10378 | case TRUTH_ANDIF_EXPR: |
10379 | case TRUTH_ORIF_EXPR: | |
10380 | case TRUTH_AND_EXPR: | |
10381 | case TRUTH_OR_EXPR: | |
10382 | case TRUTH_XOR_EXPR: | |
10383 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
ab22c1fa CF |
10384 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
10385 | || code0 == FIXED_POINT_TYPE) | |
3e4093b6 | 10386 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE |
ab22c1fa CF |
10387 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE |
10388 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10389 | { |
10390 | /* Result of these operations is always an int, | |
10391 | but that does not mean the operands should be | |
10392 | converted to ints! */ | |
10393 | result_type = integer_type_node; | |
a27d595d JM |
10394 | if (op0_int_operands) |
10395 | { | |
10396 | op0 = c_objc_common_truthvalue_conversion (location, orig_op0); | |
10397 | op0 = remove_c_maybe_const_expr (op0); | |
10398 | } | |
10399 | else | |
10400 | op0 = c_objc_common_truthvalue_conversion (location, op0); | |
10401 | if (op1_int_operands) | |
10402 | { | |
10403 | op1 = c_objc_common_truthvalue_conversion (location, orig_op1); | |
10404 | op1 = remove_c_maybe_const_expr (op1); | |
10405 | } | |
10406 | else | |
10407 | op1 = c_objc_common_truthvalue_conversion (location, op1); | |
3e4093b6 | 10408 | converted = 1; |
180f8dbb | 10409 | boolean_op = true; |
3e4093b6 | 10410 | } |
928c19bb JM |
10411 | if (code == TRUTH_ANDIF_EXPR) |
10412 | { | |
10413 | int_const_or_overflow = (int_operands | |
10414 | && TREE_CODE (orig_op0) == INTEGER_CST | |
10415 | && (op0 == truthvalue_false_node | |
10416 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
10417 | int_const = (int_const_or_overflow | |
10418 | && !TREE_OVERFLOW (orig_op0) | |
10419 | && (op0 == truthvalue_false_node | |
10420 | || !TREE_OVERFLOW (orig_op1))); | |
10421 | } | |
10422 | else if (code == TRUTH_ORIF_EXPR) | |
10423 | { | |
10424 | int_const_or_overflow = (int_operands | |
10425 | && TREE_CODE (orig_op0) == INTEGER_CST | |
10426 | && (op0 == truthvalue_true_node | |
10427 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
10428 | int_const = (int_const_or_overflow | |
10429 | && !TREE_OVERFLOW (orig_op0) | |
10430 | && (op0 == truthvalue_true_node | |
10431 | || !TREE_OVERFLOW (orig_op1))); | |
10432 | } | |
3e4093b6 | 10433 | break; |
eba80994 | 10434 | |
3e4093b6 RS |
10435 | /* Shift operations: result has same type as first operand; |
10436 | always convert second operand to int. | |
10437 | Also set SHORT_SHIFT if shifting rightward. */ | |
de520661 | 10438 | |
3e4093b6 | 10439 | case RSHIFT_EXPR: |
f87bd04b AS |
10440 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
10441 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
10442 | { | |
10443 | result_type = type0; | |
10444 | converted = 1; | |
10445 | } | |
10446 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
10447 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
10448 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
10449 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
10450 | { | |
10451 | result_type = type0; | |
10452 | converted = 1; | |
10453 | } | |
10454 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
ab22c1fa | 10455 | && code1 == INTEGER_TYPE) |
3e4093b6 | 10456 | { |
de5a5fa1 | 10457 | doing_shift = true; |
928c19bb | 10458 | if (TREE_CODE (op1) == INTEGER_CST) |
b62acd60 | 10459 | { |
3e4093b6 | 10460 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
10461 | { |
10462 | int_const = false; | |
7d882b83 | 10463 | if (c_inhibit_evaluation_warnings == 0) |
c4bdc42f | 10464 | warning_at (location, 0, "right shift count is negative"); |
928c19bb | 10465 | } |
3e4093b6 | 10466 | else |
bbb818c6 | 10467 | { |
3f75a254 | 10468 | if (!integer_zerop (op1)) |
3e4093b6 RS |
10469 | short_shift = 1; |
10470 | ||
10471 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
928c19bb JM |
10472 | { |
10473 | int_const = false; | |
7d882b83 | 10474 | if (c_inhibit_evaluation_warnings == 0) |
c4bdc42f MP |
10475 | warning_at (location, 0, "right shift count >= width " |
10476 | "of type"); | |
928c19bb | 10477 | } |
bbb818c6 | 10478 | } |
b62acd60 | 10479 | } |
de520661 | 10480 | |
3e4093b6 RS |
10481 | /* Use the type of the value to be shifted. */ |
10482 | result_type = type0; | |
f87bd04b AS |
10483 | /* Convert the non vector shift-count to an integer, regardless |
10484 | of size of value being shifted. */ | |
10485 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
10486 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
3e4093b6 RS |
10487 | op1 = convert (integer_type_node, op1); |
10488 | /* Avoid converting op1 to result_type later. */ | |
10489 | converted = 1; | |
400fbf9f | 10490 | } |
3e4093b6 | 10491 | break; |
253b6b82 | 10492 | |
3e4093b6 | 10493 | case LSHIFT_EXPR: |
f87bd04b AS |
10494 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
10495 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
10496 | { | |
10497 | result_type = type0; | |
10498 | converted = 1; | |
10499 | } | |
10500 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
10501 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
10502 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
10503 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
10504 | { | |
10505 | result_type = type0; | |
10506 | converted = 1; | |
10507 | } | |
10508 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
ab22c1fa | 10509 | && code1 == INTEGER_TYPE) |
3e4093b6 | 10510 | { |
de5a5fa1 | 10511 | doing_shift = true; |
928c19bb | 10512 | if (TREE_CODE (op1) == INTEGER_CST) |
de520661 | 10513 | { |
3e4093b6 | 10514 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
10515 | { |
10516 | int_const = false; | |
7d882b83 | 10517 | if (c_inhibit_evaluation_warnings == 0) |
c4bdc42f | 10518 | warning_at (location, 0, "left shift count is negative"); |
928c19bb | 10519 | } |
de520661 | 10520 | |
3e4093b6 | 10521 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
928c19bb JM |
10522 | { |
10523 | int_const = false; | |
7d882b83 | 10524 | if (c_inhibit_evaluation_warnings == 0) |
c4bdc42f MP |
10525 | warning_at (location, 0, "left shift count >= width of " |
10526 | "type"); | |
928c19bb | 10527 | } |
94ba5069 | 10528 | } |
de520661 | 10529 | |
3e4093b6 RS |
10530 | /* Use the type of the value to be shifted. */ |
10531 | result_type = type0; | |
f87bd04b AS |
10532 | /* Convert the non vector shift-count to an integer, regardless |
10533 | of size of value being shifted. */ | |
10534 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
10535 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
3e4093b6 RS |
10536 | op1 = convert (integer_type_node, op1); |
10537 | /* Avoid converting op1 to result_type later. */ | |
10538 | converted = 1; | |
400fbf9f | 10539 | } |
3e4093b6 | 10540 | break; |
de520661 | 10541 | |
3e4093b6 RS |
10542 | case EQ_EXPR: |
10543 | case NE_EXPR: | |
d246ab4f AS |
10544 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10545 | { | |
10546 | tree intt; | |
0af94e6f | 10547 | if (!vector_types_compatible_elements_p (type0, type1)) |
d246ab4f AS |
10548 | { |
10549 | error_at (location, "comparing vectors with different " | |
10550 | "element types"); | |
10551 | return error_mark_node; | |
10552 | } | |
10553 | ||
10554 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10555 | { | |
10556 | error_at (location, "comparing vectors with different " | |
10557 | "number of elements"); | |
10558 | return error_mark_node; | |
10559 | } | |
10560 | ||
10561 | /* Always construct signed integer vector type. */ | |
10562 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10563 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10564 | result_type = build_opaque_vector_type (intt, | |
10565 | TYPE_VECTOR_SUBPARTS (type0)); | |
10566 | converted = 1; | |
10567 | break; | |
10568 | } | |
ae311566 | 10569 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) |
ba47d38d AH |
10570 | warning_at (location, |
10571 | OPT_Wfloat_equal, | |
10572 | "comparing floating point with == or != is unsafe"); | |
3e4093b6 RS |
10573 | /* Result of comparison is always int, |
10574 | but don't convert the args to int! */ | |
10575 | build_type = integer_type_node; | |
10576 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 10577 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) |
3e4093b6 | 10578 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
ab22c1fa | 10579 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) |
3e4093b6 | 10580 | short_compare = 1; |
637f1455 SZ |
10581 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
10582 | { | |
10583 | if (TREE_CODE (op0) == ADDR_EXPR | |
10584 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) | |
10585 | { | |
10586 | if (code == EQ_EXPR) | |
10587 | warning_at (location, | |
10588 | OPT_Waddress, | |
10589 | "the comparison will always evaluate as %<false%> " | |
10590 | "for the address of %qD will never be NULL", | |
10591 | TREE_OPERAND (op0, 0)); | |
10592 | else | |
10593 | warning_at (location, | |
10594 | OPT_Waddress, | |
10595 | "the comparison will always evaluate as %<true%> " | |
10596 | "for the address of %qD will never be NULL", | |
10597 | TREE_OPERAND (op0, 0)); | |
10598 | } | |
10599 | result_type = type0; | |
10600 | } | |
10601 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) | |
10602 | { | |
10603 | if (TREE_CODE (op1) == ADDR_EXPR | |
10604 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) | |
10605 | { | |
10606 | if (code == EQ_EXPR) | |
10607 | warning_at (location, | |
f90e8e2e | 10608 | OPT_Waddress, |
637f1455 SZ |
10609 | "the comparison will always evaluate as %<false%> " |
10610 | "for the address of %qD will never be NULL", | |
10611 | TREE_OPERAND (op1, 0)); | |
10612 | else | |
10613 | warning_at (location, | |
10614 | OPT_Waddress, | |
10615 | "the comparison will always evaluate as %<true%> " | |
10616 | "for the address of %qD will never be NULL", | |
10617 | TREE_OPERAND (op1, 0)); | |
10618 | } | |
10619 | result_type = type1; | |
10620 | } | |
3e4093b6 RS |
10621 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
10622 | { | |
10623 | tree tt0 = TREE_TYPE (type0); | |
10624 | tree tt1 = TREE_TYPE (type1); | |
36c5e70a BE |
10625 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); |
10626 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
10627 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
10628 | ||
3e4093b6 RS |
10629 | /* Anything compares with void *. void * compares with anything. |
10630 | Otherwise, the targets must be compatible | |
10631 | and both must be object or both incomplete. */ | |
744aa42f | 10632 | if (comp_target_types (location, type0, type1)) |
10bc1b1b | 10633 | result_type = common_pointer_type (type0, type1); |
36c5e70a BE |
10634 | else if (!addr_space_superset (as0, as1, &as_common)) |
10635 | { | |
10636 | error_at (location, "comparison of pointers to " | |
10637 | "disjoint address spaces"); | |
10638 | return error_mark_node; | |
10639 | } | |
267bac10 | 10640 | else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0)) |
ee2990e7 | 10641 | { |
36c5e70a | 10642 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) |
c1771a20 | 10643 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10644 | "comparison of %<void *%> with function pointer"); |
ee2990e7 | 10645 | } |
267bac10 | 10646 | else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1)) |
e6834654 | 10647 | { |
36c5e70a | 10648 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) |
c1771a20 | 10649 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10650 | "comparison of %<void *%> with function pointer"); |
e6834654 | 10651 | } |
3e4093b6 | 10652 | else |
58393038 ZL |
10653 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
10654 | if (!objc_ok) | |
ba47d38d | 10655 | pedwarn (location, 0, |
509c9d60 | 10656 | "comparison of distinct pointer types lacks a cast"); |
e6834654 | 10657 | |
3e4093b6 | 10658 | if (result_type == NULL_TREE) |
36c5e70a BE |
10659 | { |
10660 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
10661 | result_type = build_pointer_type | |
10662 | (build_qualified_type (void_type_node, qual)); | |
10663 | } | |
e6834654 | 10664 | } |
3e4093b6 | 10665 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
de520661 | 10666 | { |
3e4093b6 | 10667 | result_type = type0; |
ba47d38d | 10668 | pedwarn (location, 0, "comparison between pointer and integer"); |
de520661 | 10669 | } |
3e4093b6 | 10670 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
8b6a5902 | 10671 | { |
3e4093b6 | 10672 | result_type = type1; |
ba47d38d | 10673 | pedwarn (location, 0, "comparison between pointer and integer"); |
8b6a5902 | 10674 | } |
3e4093b6 | 10675 | break; |
8b6a5902 | 10676 | |
3e4093b6 RS |
10677 | case LE_EXPR: |
10678 | case GE_EXPR: | |
10679 | case LT_EXPR: | |
10680 | case GT_EXPR: | |
d246ab4f AS |
10681 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10682 | { | |
10683 | tree intt; | |
0af94e6f | 10684 | if (!vector_types_compatible_elements_p (type0, type1)) |
d246ab4f AS |
10685 | { |
10686 | error_at (location, "comparing vectors with different " | |
10687 | "element types"); | |
10688 | return error_mark_node; | |
10689 | } | |
10690 | ||
10691 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10692 | { | |
10693 | error_at (location, "comparing vectors with different " | |
10694 | "number of elements"); | |
10695 | return error_mark_node; | |
10696 | } | |
10697 | ||
10698 | /* Always construct signed integer vector type. */ | |
10699 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10700 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10701 | result_type = build_opaque_vector_type (intt, | |
10702 | TYPE_VECTOR_SUBPARTS (type0)); | |
10703 | converted = 1; | |
10704 | break; | |
10705 | } | |
3e4093b6 | 10706 | build_type = integer_type_node; |
ab22c1fa CF |
10707 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
10708 | || code0 == FIXED_POINT_TYPE) | |
10709 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
10710 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10711 | short_compare = 1; |
10712 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
10713 | { | |
36c5e70a BE |
10714 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); |
10715 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
10716 | addr_space_t as_common; | |
10717 | ||
744aa42f | 10718 | if (comp_target_types (location, type0, type1)) |
3e4093b6 | 10719 | { |
10bc1b1b | 10720 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
10721 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
10722 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
ba47d38d | 10723 | pedwarn (location, 0, |
509c9d60 | 10724 | "comparison of complete and incomplete pointers"); |
fcf73884 | 10725 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) |
c1771a20 | 10726 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10727 | "ordered comparisons of pointers to functions"); |
d42ba3b8 SZ |
10728 | else if (null_pointer_constant_p (orig_op0) |
10729 | || null_pointer_constant_p (orig_op1)) | |
10730 | warning_at (location, OPT_Wextra, | |
10731 | "ordered comparison of pointer with null pointer"); | |
10732 | ||
3e4093b6 | 10733 | } |
36c5e70a BE |
10734 | else if (!addr_space_superset (as0, as1, &as_common)) |
10735 | { | |
10736 | error_at (location, "comparison of pointers to " | |
10737 | "disjoint address spaces"); | |
10738 | return error_mark_node; | |
10739 | } | |
3e4093b6 RS |
10740 | else |
10741 | { | |
36c5e70a BE |
10742 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
10743 | result_type = build_pointer_type | |
10744 | (build_qualified_type (void_type_node, qual)); | |
ba47d38d | 10745 | pedwarn (location, 0, |
509c9d60 | 10746 | "comparison of distinct pointer types lacks a cast"); |
3e4093b6 RS |
10747 | } |
10748 | } | |
6aa3c60d | 10749 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
3e4093b6 RS |
10750 | { |
10751 | result_type = type0; | |
fcf73884 | 10752 | if (pedantic) |
c1771a20 | 10753 | pedwarn (location, OPT_Wpedantic, |
fcf73884 MLI |
10754 | "ordered comparison of pointer with integer zero"); |
10755 | else if (extra_warnings) | |
ba47d38d | 10756 | warning_at (location, OPT_Wextra, |
d42ba3b8 | 10757 | "ordered comparison of pointer with integer zero"); |
3e4093b6 | 10758 | } |
6aa3c60d | 10759 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
3e4093b6 RS |
10760 | { |
10761 | result_type = type1; | |
d42ba3b8 | 10762 | if (pedantic) |
c1771a20 | 10763 | pedwarn (location, OPT_Wpedantic, |
d42ba3b8 SZ |
10764 | "ordered comparison of pointer with integer zero"); |
10765 | else if (extra_warnings) | |
10766 | warning_at (location, OPT_Wextra, | |
10767 | "ordered comparison of pointer with integer zero"); | |
3e4093b6 RS |
10768 | } |
10769 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
10770 | { | |
10771 | result_type = type0; | |
ba47d38d | 10772 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
10773 | } |
10774 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
10775 | { | |
10776 | result_type = type1; | |
ba47d38d | 10777 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
10778 | } |
10779 | break; | |
64094f6a | 10780 | |
3e4093b6 | 10781 | default: |
37b2f290 | 10782 | gcc_unreachable (); |
c9fe6f9f | 10783 | } |
8f17b5c5 | 10784 | |
e57e265b PB |
10785 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
10786 | return error_mark_node; | |
10787 | ||
5bed876a AH |
10788 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
10789 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
0af94e6f | 10790 | || !vector_types_compatible_elements_p (type0, type1))) |
5bed876a | 10791 | { |
ba47d38d | 10792 | binary_op_error (location, code, type0, type1); |
5bed876a AH |
10793 | return error_mark_node; |
10794 | } | |
10795 | ||
3e4093b6 | 10796 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
ab22c1fa | 10797 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) |
3e4093b6 RS |
10798 | && |
10799 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
ab22c1fa | 10800 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 10801 | { |
2ca862e9 JM |
10802 | bool first_complex = (code0 == COMPLEX_TYPE); |
10803 | bool second_complex = (code1 == COMPLEX_TYPE); | |
10804 | int none_complex = (!first_complex && !second_complex); | |
39b726dd | 10805 | |
3e4093b6 | 10806 | if (shorten || common || short_compare) |
3bf6bfcc JJ |
10807 | { |
10808 | result_type = c_common_type (type0, type1); | |
0a0b3574 MM |
10809 | do_warn_double_promotion (result_type, type0, type1, |
10810 | "implicit conversion from %qT to %qT " | |
10811 | "to match other operand of binary " | |
10812 | "expression", | |
10813 | location); | |
3bf6bfcc JJ |
10814 | if (result_type == error_mark_node) |
10815 | return error_mark_node; | |
10816 | } | |
400fbf9f | 10817 | |
2ca862e9 JM |
10818 | if (first_complex != second_complex |
10819 | && (code == PLUS_EXPR | |
10820 | || code == MINUS_EXPR | |
10821 | || code == MULT_EXPR | |
10822 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
10823 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
10824 | && flag_signed_zeros) | |
10825 | { | |
10826 | /* An operation on mixed real/complex operands must be | |
10827 | handled specially, but the language-independent code can | |
10828 | more easily optimize the plain complex arithmetic if | |
10829 | -fno-signed-zeros. */ | |
10830 | tree real_type = TREE_TYPE (result_type); | |
10831 | tree real, imag; | |
10832 | if (type0 != orig_type0 || type1 != orig_type1) | |
10833 | { | |
10834 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 10835 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
2ca862e9 JM |
10836 | } |
10837 | if (first_complex) | |
10838 | { | |
10839 | if (TREE_TYPE (op0) != result_type) | |
68fca595 | 10840 | op0 = convert_and_check (location, result_type, op0); |
2ca862e9 | 10841 | if (TREE_TYPE (op1) != real_type) |
68fca595 | 10842 | op1 = convert_and_check (location, real_type, op1); |
2ca862e9 JM |
10843 | } |
10844 | else | |
10845 | { | |
10846 | if (TREE_TYPE (op0) != real_type) | |
68fca595 | 10847 | op0 = convert_and_check (location, real_type, op0); |
2ca862e9 | 10848 | if (TREE_TYPE (op1) != result_type) |
68fca595 | 10849 | op1 = convert_and_check (location, result_type, op1); |
2ca862e9 JM |
10850 | } |
10851 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10852 | return error_mark_node; | |
10853 | if (first_complex) | |
10854 | { | |
10855 | op0 = c_save_expr (op0); | |
10856 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
10857 | op0, 1); | |
10858 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
10859 | op0, 1); | |
10860 | switch (code) | |
10861 | { | |
10862 | case MULT_EXPR: | |
10863 | case TRUNC_DIV_EXPR: | |
c4603e7c | 10864 | op1 = c_save_expr (op1); |
2ca862e9 JM |
10865 | imag = build2 (resultcode, real_type, imag, op1); |
10866 | /* Fall through. */ | |
10867 | case PLUS_EXPR: | |
10868 | case MINUS_EXPR: | |
10869 | real = build2 (resultcode, real_type, real, op1); | |
10870 | break; | |
10871 | default: | |
10872 | gcc_unreachable(); | |
10873 | } | |
10874 | } | |
10875 | else | |
10876 | { | |
10877 | op1 = c_save_expr (op1); | |
10878 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
10879 | op1, 1); | |
10880 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
10881 | op1, 1); | |
10882 | switch (code) | |
10883 | { | |
10884 | case MULT_EXPR: | |
c4603e7c | 10885 | op0 = c_save_expr (op0); |
2ca862e9 JM |
10886 | imag = build2 (resultcode, real_type, op0, imag); |
10887 | /* Fall through. */ | |
10888 | case PLUS_EXPR: | |
10889 | real = build2 (resultcode, real_type, op0, real); | |
10890 | break; | |
10891 | case MINUS_EXPR: | |
10892 | real = build2 (resultcode, real_type, op0, real); | |
10893 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
10894 | break; | |
10895 | default: | |
10896 | gcc_unreachable(); | |
10897 | } | |
10898 | } | |
10899 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
10900 | goto return_build_binary_op; | |
10901 | } | |
10902 | ||
3e4093b6 RS |
10903 | /* For certain operations (which identify themselves by shorten != 0) |
10904 | if both args were extended from the same smaller type, | |
10905 | do the arithmetic in that type and then extend. | |
400fbf9f | 10906 | |
3e4093b6 RS |
10907 | shorten !=0 and !=1 indicates a bitwise operation. |
10908 | For them, this optimization is safe only if | |
10909 | both args are zero-extended or both are sign-extended. | |
10910 | Otherwise, we might change the result. | |
10911 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
10912 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
400fbf9f | 10913 | |
3e4093b6 RS |
10914 | if (shorten && none_complex) |
10915 | { | |
3e4093b6 | 10916 | final_type = result_type; |
b8698a0f | 10917 | result_type = shorten_binary_op (result_type, op0, op1, |
6715192c | 10918 | shorten == -1); |
3e4093b6 | 10919 | } |
88a3dbc1 | 10920 | |
3e4093b6 | 10921 | /* Shifts can be shortened if shifting right. */ |
2f6e4e97 | 10922 | |
3e4093b6 RS |
10923 | if (short_shift) |
10924 | { | |
10925 | int unsigned_arg; | |
10926 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
88a3dbc1 | 10927 | |
3e4093b6 | 10928 | final_type = result_type; |
abe80e6d | 10929 | |
3e4093b6 | 10930 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
8df83eae | 10931 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
e9a25f70 | 10932 | |
3e4093b6 | 10933 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
be123439 | 10934 | && tree_int_cst_sgn (op1) > 0 |
3e4093b6 RS |
10935 | /* We can shorten only if the shift count is less than the |
10936 | number of bits in the smaller type size. */ | |
10937 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
10938 | /* We cannot drop an unsigned shift after sign-extension. */ | |
8df83eae | 10939 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
3e4093b6 RS |
10940 | { |
10941 | /* Do an unsigned shift if the operand was zero-extended. */ | |
10942 | result_type | |
10943 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
10944 | TREE_TYPE (arg0)); | |
10945 | /* Convert value-to-be-shifted to that type. */ | |
10946 | if (TREE_TYPE (op0) != result_type) | |
10947 | op0 = convert (result_type, op0); | |
10948 | converted = 1; | |
abe80e6d | 10949 | } |
88a3dbc1 RK |
10950 | } |
10951 | ||
3e4093b6 RS |
10952 | /* Comparison operations are shortened too but differently. |
10953 | They identify themselves by setting short_compare = 1. */ | |
56cb9733 | 10954 | |
3e4093b6 RS |
10955 | if (short_compare) |
10956 | { | |
10957 | /* Don't write &op0, etc., because that would prevent op0 | |
10958 | from being kept in a register. | |
10959 | Instead, make copies of the our local variables and | |
10960 | pass the copies by reference, then copy them back afterward. */ | |
10961 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
10962 | enum tree_code xresultcode = resultcode; | |
10963 | tree val | |
393e8e8b MP |
10964 | = shorten_compare (location, &xop0, &xop1, &xresult_type, |
10965 | &xresultcode); | |
8f17b5c5 | 10966 | |
3e4093b6 | 10967 | if (val != 0) |
c9f9eb5d AH |
10968 | { |
10969 | ret = val; | |
10970 | goto return_build_binary_op; | |
10971 | } | |
8f17b5c5 | 10972 | |
3e4093b6 RS |
10973 | op0 = xop0, op1 = xop1; |
10974 | converted = 1; | |
10975 | resultcode = xresultcode; | |
8f17b5c5 | 10976 | |
7d882b83 | 10977 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
10978 | { |
10979 | bool op0_maybe_const = true; | |
10980 | bool op1_maybe_const = true; | |
10981 | tree orig_op0_folded, orig_op1_folded; | |
10982 | ||
10983 | if (in_late_binary_op) | |
10984 | { | |
10985 | orig_op0_folded = orig_op0; | |
10986 | orig_op1_folded = orig_op1; | |
10987 | } | |
10988 | else | |
10989 | { | |
10990 | /* Fold for the sake of possible warnings, as in | |
10991 | build_conditional_expr. This requires the | |
10992 | "original" values to be folded, not just op0 and | |
10993 | op1. */ | |
f5178456 | 10994 | c_inhibit_evaluation_warnings++; |
928c19bb JM |
10995 | op0 = c_fully_fold (op0, require_constant_value, |
10996 | &op0_maybe_const); | |
10997 | op1 = c_fully_fold (op1, require_constant_value, | |
10998 | &op1_maybe_const); | |
f5178456 | 10999 | c_inhibit_evaluation_warnings--; |
928c19bb JM |
11000 | orig_op0_folded = c_fully_fold (orig_op0, |
11001 | require_constant_value, | |
11002 | NULL); | |
11003 | orig_op1_folded = c_fully_fold (orig_op1, | |
11004 | require_constant_value, | |
11005 | NULL); | |
11006 | } | |
11007 | ||
11008 | if (warn_sign_compare) | |
11009 | warn_for_sign_compare (location, orig_op0_folded, | |
11010 | orig_op1_folded, op0, op1, | |
11011 | result_type, resultcode); | |
5c2f94b4 | 11012 | if (!in_late_binary_op && !int_operands) |
928c19bb JM |
11013 | { |
11014 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
e5a94231 | 11015 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); |
928c19bb | 11016 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) |
e5a94231 | 11017 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 11018 | } |
3e4093b6 | 11019 | } |
2ad1815d | 11020 | } |
64094f6a | 11021 | } |
64094f6a | 11022 | |
3e4093b6 RS |
11023 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
11024 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
11025 | Then the expression will be built. | |
11026 | It will be given type FINAL_TYPE if that is nonzero; | |
11027 | otherwise, it will be given type RESULT_TYPE. */ | |
400fbf9f | 11028 | |
3e4093b6 RS |
11029 | if (!result_type) |
11030 | { | |
ba47d38d | 11031 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); |
3e4093b6 RS |
11032 | return error_mark_node; |
11033 | } | |
400fbf9f | 11034 | |
3e4093b6 | 11035 | if (build_type == NULL_TREE) |
8ce94e44 JM |
11036 | { |
11037 | build_type = result_type; | |
180f8dbb JM |
11038 | if ((type0 != orig_type0 || type1 != orig_type1) |
11039 | && !boolean_op) | |
8ce94e44 JM |
11040 | { |
11041 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 11042 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
8ce94e44 JM |
11043 | } |
11044 | } | |
400fbf9f | 11045 | |
2d2e923f MLI |
11046 | if (!converted) |
11047 | { | |
68fca595 MP |
11048 | op0 = ep_convert_and_check (location, result_type, op0, |
11049 | semantic_result_type); | |
11050 | op1 = ep_convert_and_check (location, result_type, op1, | |
11051 | semantic_result_type); | |
2d2e923f MLI |
11052 | |
11053 | /* This can happen if one operand has a vector type, and the other | |
11054 | has a different type. */ | |
11055 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
11056 | return error_mark_node; | |
11057 | } | |
11058 | ||
f8ed5150 MP |
11059 | if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE |
11060 | | SANITIZE_FLOAT_DIVIDE)) | |
de5a5fa1 | 11061 | && current_function_decl != 0 |
ce6923c5 MP |
11062 | && !lookup_attribute ("no_sanitize_undefined", |
11063 | DECL_ATTRIBUTES (current_function_decl)) | |
de5a5fa1 MP |
11064 | && (doing_div_or_mod || doing_shift)) |
11065 | { | |
11066 | /* OP0 and/or OP1 might have side-effects. */ | |
11067 | op0 = c_save_expr (op0); | |
11068 | op1 = c_save_expr (op1); | |
11069 | op0 = c_fully_fold (op0, false, NULL); | |
11070 | op1 = c_fully_fold (op1, false, NULL); | |
f8ed5150 MP |
11071 | if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE |
11072 | | SANITIZE_FLOAT_DIVIDE))) | |
de5a5fa1 | 11073 | instrument_expr = ubsan_instrument_division (location, op0, op1); |
a24d975c | 11074 | else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT)) |
de5a5fa1 MP |
11075 | instrument_expr = ubsan_instrument_shift (location, code, op0, op1); |
11076 | } | |
11077 | ||
c9f9eb5d | 11078 | /* Treat expressions in initializers specially as they can't trap. */ |
928c19bb JM |
11079 | if (int_const_or_overflow) |
11080 | ret = (require_constant_value | |
db3927fb AH |
11081 | ? fold_build2_initializer_loc (location, resultcode, build_type, |
11082 | op0, op1) | |
11083 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
928c19bb JM |
11084 | else |
11085 | ret = build2 (resultcode, build_type, op0, op1); | |
c9f9eb5d AH |
11086 | if (final_type != 0) |
11087 | ret = convert (final_type, ret); | |
11088 | ||
11089 | return_build_binary_op: | |
11090 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
11091 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) |
11092 | ret = (int_operands | |
11093 | ? note_integer_operands (ret) | |
11094 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
11095 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
11096 | && !in_late_binary_op) | |
11097 | ret = note_integer_operands (ret); | |
2d2e923f MLI |
11098 | if (semantic_result_type) |
11099 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
c9f9eb5d | 11100 | protected_set_expr_location (ret, location); |
de5a5fa1 | 11101 | |
a24d975c | 11102 | if (instrument_expr != NULL) |
de5a5fa1 MP |
11103 | ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret), |
11104 | instrument_expr, ret); | |
11105 | ||
c9f9eb5d | 11106 | return ret; |
400fbf9f | 11107 | } |
85498824 JM |
11108 | |
11109 | ||
11110 | /* Convert EXPR to be a truth-value, validating its type for this | |
ba47d38d | 11111 | purpose. LOCATION is the source location for the expression. */ |
85498824 JM |
11112 | |
11113 | tree | |
ba47d38d | 11114 | c_objc_common_truthvalue_conversion (location_t location, tree expr) |
85498824 | 11115 | { |
928c19bb JM |
11116 | bool int_const, int_operands; |
11117 | ||
85498824 JM |
11118 | switch (TREE_CODE (TREE_TYPE (expr))) |
11119 | { | |
11120 | case ARRAY_TYPE: | |
ba47d38d | 11121 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); |
85498824 JM |
11122 | return error_mark_node; |
11123 | ||
11124 | case RECORD_TYPE: | |
ba47d38d | 11125 | error_at (location, "used struct type value where scalar is required"); |
85498824 JM |
11126 | return error_mark_node; |
11127 | ||
11128 | case UNION_TYPE: | |
ba47d38d | 11129 | error_at (location, "used union type value where scalar is required"); |
85498824 JM |
11130 | return error_mark_node; |
11131 | ||
04af8788 NP |
11132 | case VOID_TYPE: |
11133 | error_at (location, "void value not ignored as it ought to be"); | |
11134 | return error_mark_node; | |
11135 | ||
46bdb9cf JM |
11136 | case FUNCTION_TYPE: |
11137 | gcc_unreachable (); | |
11138 | ||
d246ab4f AS |
11139 | case VECTOR_TYPE: |
11140 | error_at (location, "used vector type where scalar is required"); | |
11141 | return error_mark_node; | |
11142 | ||
85498824 JM |
11143 | default: |
11144 | break; | |
11145 | } | |
11146 | ||
928c19bb JM |
11147 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); |
11148 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
a27d595d JM |
11149 | if (int_operands && TREE_CODE (expr) != INTEGER_CST) |
11150 | { | |
11151 | expr = remove_c_maybe_const_expr (expr); | |
11152 | expr = build2 (NE_EXPR, integer_type_node, expr, | |
11153 | convert (TREE_TYPE (expr), integer_zero_node)); | |
11154 | expr = note_integer_operands (expr); | |
11155 | } | |
11156 | else | |
11157 | /* ??? Should we also give an error for vectors rather than leaving | |
11158 | those to give errors later? */ | |
11159 | expr = c_common_truthvalue_conversion (location, expr); | |
928c19bb JM |
11160 | |
11161 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
11162 | { | |
11163 | if (TREE_OVERFLOW (expr)) | |
11164 | return expr; | |
11165 | else | |
11166 | return note_integer_operands (expr); | |
11167 | } | |
11168 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
11169 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
11170 | return expr; | |
85498824 | 11171 | } |
73f397d4 JM |
11172 | \f |
11173 | ||
11174 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
11175 | required. */ | |
11176 | ||
11177 | tree | |
51eed280 | 11178 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) |
73f397d4 JM |
11179 | { |
11180 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
11181 | { | |
11182 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
11183 | /* Executing a compound literal inside a function reinitializes | |
11184 | it. */ | |
11185 | if (!TREE_STATIC (decl)) | |
11186 | *se = true; | |
11187 | return decl; | |
11188 | } | |
11189 | else | |
11190 | return expr; | |
11191 | } | |
953ff289 | 11192 | \f |
c0220ea4 | 11193 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
953ff289 DN |
11194 | |
11195 | tree | |
11196 | c_begin_omp_parallel (void) | |
11197 | { | |
11198 | tree block; | |
11199 | ||
11200 | keep_next_level (); | |
11201 | block = c_begin_compound_stmt (true); | |
11202 | ||
11203 | return block; | |
11204 | } | |
11205 | ||
c2255bc4 AH |
11206 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound |
11207 | statement. LOC is the location of the OMP_PARALLEL. */ | |
a68ab351 | 11208 | |
953ff289 | 11209 | tree |
c2255bc4 | 11210 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) |
953ff289 DN |
11211 | { |
11212 | tree stmt; | |
11213 | ||
c2255bc4 | 11214 | block = c_end_compound_stmt (loc, block, true); |
953ff289 DN |
11215 | |
11216 | stmt = make_node (OMP_PARALLEL); | |
11217 | TREE_TYPE (stmt) = void_type_node; | |
11218 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
11219 | OMP_PARALLEL_BODY (stmt) = block; | |
c2255bc4 | 11220 | SET_EXPR_LOCATION (stmt, loc); |
953ff289 DN |
11221 | |
11222 | return add_stmt (stmt); | |
11223 | } | |
11224 | ||
a68ab351 JJ |
11225 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
11226 | ||
11227 | tree | |
11228 | c_begin_omp_task (void) | |
11229 | { | |
11230 | tree block; | |
11231 | ||
11232 | keep_next_level (); | |
11233 | block = c_begin_compound_stmt (true); | |
11234 | ||
11235 | return block; | |
11236 | } | |
11237 | ||
c2255bc4 AH |
11238 | /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound |
11239 | statement. LOC is the location of the #pragma. */ | |
a68ab351 JJ |
11240 | |
11241 | tree | |
c2255bc4 | 11242 | c_finish_omp_task (location_t loc, tree clauses, tree block) |
a68ab351 JJ |
11243 | { |
11244 | tree stmt; | |
11245 | ||
c2255bc4 | 11246 | block = c_end_compound_stmt (loc, block, true); |
a68ab351 JJ |
11247 | |
11248 | stmt = make_node (OMP_TASK); | |
11249 | TREE_TYPE (stmt) = void_type_node; | |
11250 | OMP_TASK_CLAUSES (stmt) = clauses; | |
11251 | OMP_TASK_BODY (stmt) = block; | |
c2255bc4 | 11252 | SET_EXPR_LOCATION (stmt, loc); |
a68ab351 JJ |
11253 | |
11254 | return add_stmt (stmt); | |
11255 | } | |
11256 | ||
acf0174b JJ |
11257 | /* Generate GOMP_cancel call for #pragma omp cancel. */ |
11258 | ||
11259 | void | |
11260 | c_finish_omp_cancel (location_t loc, tree clauses) | |
11261 | { | |
11262 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL); | |
11263 | int mask = 0; | |
11264 | if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) | |
11265 | mask = 1; | |
11266 | else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) | |
11267 | mask = 2; | |
11268 | else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) | |
11269 | mask = 4; | |
11270 | else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) | |
11271 | mask = 8; | |
11272 | else | |
11273 | { | |
11274 | error_at (loc, "%<#pragma omp cancel must specify one of " | |
11275 | "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " | |
11276 | "clauses"); | |
11277 | return; | |
11278 | } | |
11279 | tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF); | |
11280 | if (ifc != NULL_TREE) | |
11281 | { | |
11282 | tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc)); | |
11283 | ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR, | |
11284 | boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc), | |
11285 | build_zero_cst (type)); | |
11286 | } | |
11287 | else | |
11288 | ifc = boolean_true_node; | |
11289 | tree stmt = build_call_expr_loc (loc, fn, 2, | |
11290 | build_int_cst (integer_type_node, mask), | |
11291 | ifc); | |
11292 | add_stmt (stmt); | |
11293 | } | |
11294 | ||
11295 | /* Generate GOMP_cancellation_point call for | |
11296 | #pragma omp cancellation point. */ | |
11297 | ||
11298 | void | |
11299 | c_finish_omp_cancellation_point (location_t loc, tree clauses) | |
11300 | { | |
11301 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT); | |
11302 | int mask = 0; | |
11303 | if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) | |
11304 | mask = 1; | |
11305 | else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) | |
11306 | mask = 2; | |
11307 | else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) | |
11308 | mask = 4; | |
11309 | else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) | |
11310 | mask = 8; | |
11311 | else | |
11312 | { | |
11313 | error_at (loc, "%<#pragma omp cancellation point must specify one of " | |
11314 | "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " | |
11315 | "clauses"); | |
11316 | return; | |
11317 | } | |
11318 | tree stmt = build_call_expr_loc (loc, fn, 1, | |
11319 | build_int_cst (integer_type_node, mask)); | |
11320 | add_stmt (stmt); | |
11321 | } | |
11322 | ||
11323 | /* Helper function for handle_omp_array_sections. Called recursively | |
11324 | to handle multiple array-section-subscripts. C is the clause, | |
11325 | T current expression (initially OMP_CLAUSE_DECL), which is either | |
11326 | a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound | |
11327 | expression if specified, TREE_VALUE length expression if specified, | |
11328 | TREE_CHAIN is what it has been specified after, or some decl. | |
11329 | TYPES vector is populated with array section types, MAYBE_ZERO_LEN | |
11330 | set to true if any of the array-section-subscript could have length | |
11331 | of zero (explicit or implicit), FIRST_NON_ONE is the index of the | |
11332 | first array-section-subscript which is known not to have length | |
11333 | of one. Given say: | |
11334 | map(a[:b][2:1][:c][:2][:d][e:f][2:5]) | |
11335 | FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c] | |
11336 | all are or may have length of 1, array-section-subscript [:2] is the | |
11337 | first one knonwn not to have length 1. For array-section-subscript | |
11338 | <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't | |
11339 | 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we | |
11340 | can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above | |
11341 | case though, as some lengths could be zero. */ | |
11342 | ||
11343 | static tree | |
11344 | handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types, | |
11345 | bool &maybe_zero_len, unsigned int &first_non_one) | |
11346 | { | |
11347 | tree ret, low_bound, length, type; | |
11348 | if (TREE_CODE (t) != TREE_LIST) | |
11349 | { | |
11350 | if (t == error_mark_node || TREE_TYPE (t) == error_mark_node) | |
11351 | return error_mark_node; | |
11352 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11353 | { | |
11354 | if (DECL_P (t)) | |
11355 | error_at (OMP_CLAUSE_LOCATION (c), | |
11356 | "%qD is not a variable in %qs clause", t, | |
11357 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11358 | else | |
11359 | error_at (OMP_CLAUSE_LOCATION (c), | |
11360 | "%qE is not a variable in %qs clause", t, | |
11361 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11362 | return error_mark_node; | |
11363 | } | |
11364 | else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND | |
11365 | && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
11366 | { | |
11367 | error_at (OMP_CLAUSE_LOCATION (c), | |
11368 | "%qD is threadprivate variable in %qs clause", t, | |
11369 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11370 | return error_mark_node; | |
11371 | } | |
11372 | return t; | |
11373 | } | |
11374 | ||
11375 | ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types, | |
11376 | maybe_zero_len, first_non_one); | |
11377 | if (ret == error_mark_node || ret == NULL_TREE) | |
11378 | return ret; | |
11379 | ||
11380 | type = TREE_TYPE (ret); | |
11381 | low_bound = TREE_PURPOSE (t); | |
11382 | length = TREE_VALUE (t); | |
11383 | ||
11384 | if (low_bound == error_mark_node || length == error_mark_node) | |
11385 | return error_mark_node; | |
11386 | ||
11387 | if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound))) | |
11388 | { | |
11389 | error_at (OMP_CLAUSE_LOCATION (c), | |
11390 | "low bound %qE of array section does not have integral type", | |
11391 | low_bound); | |
11392 | return error_mark_node; | |
11393 | } | |
11394 | if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length))) | |
11395 | { | |
11396 | error_at (OMP_CLAUSE_LOCATION (c), | |
11397 | "length %qE of array section does not have integral type", | |
11398 | length); | |
11399 | return error_mark_node; | |
11400 | } | |
11401 | if (low_bound | |
11402 | && TREE_CODE (low_bound) == INTEGER_CST | |
11403 | && TYPE_PRECISION (TREE_TYPE (low_bound)) | |
11404 | > TYPE_PRECISION (sizetype)) | |
11405 | low_bound = fold_convert (sizetype, low_bound); | |
11406 | if (length | |
11407 | && TREE_CODE (length) == INTEGER_CST | |
11408 | && TYPE_PRECISION (TREE_TYPE (length)) | |
11409 | > TYPE_PRECISION (sizetype)) | |
11410 | length = fold_convert (sizetype, length); | |
11411 | if (low_bound == NULL_TREE) | |
11412 | low_bound = integer_zero_node; | |
11413 | ||
11414 | if (length != NULL_TREE) | |
11415 | { | |
11416 | if (!integer_nonzerop (length)) | |
11417 | maybe_zero_len = true; | |
11418 | if (first_non_one == types.length () | |
11419 | && (TREE_CODE (length) != INTEGER_CST || integer_onep (length))) | |
11420 | first_non_one++; | |
11421 | } | |
11422 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11423 | { | |
11424 | if (length == NULL_TREE | |
11425 | && (TYPE_DOMAIN (type) == NULL_TREE | |
11426 | || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)) | |
11427 | { | |
11428 | error_at (OMP_CLAUSE_LOCATION (c), | |
11429 | "for unknown bound array type length expression must " | |
11430 | "be specified"); | |
11431 | return error_mark_node; | |
11432 | } | |
11433 | if (TREE_CODE (low_bound) == INTEGER_CST | |
11434 | && tree_int_cst_sgn (low_bound) == -1) | |
11435 | { | |
11436 | error_at (OMP_CLAUSE_LOCATION (c), | |
11437 | "negative low bound in array section in %qs clause", | |
11438 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11439 | return error_mark_node; | |
11440 | } | |
11441 | if (length != NULL_TREE | |
11442 | && TREE_CODE (length) == INTEGER_CST | |
11443 | && tree_int_cst_sgn (length) == -1) | |
11444 | { | |
11445 | error_at (OMP_CLAUSE_LOCATION (c), | |
11446 | "negative length in array section in %qs clause", | |
11447 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11448 | return error_mark_node; | |
11449 | } | |
11450 | if (TYPE_DOMAIN (type) | |
11451 | && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) | |
11452 | && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) | |
11453 | == INTEGER_CST) | |
11454 | { | |
11455 | tree size = size_binop (PLUS_EXPR, | |
11456 | TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
11457 | size_one_node); | |
11458 | if (TREE_CODE (low_bound) == INTEGER_CST) | |
11459 | { | |
11460 | if (tree_int_cst_lt (size, low_bound)) | |
11461 | { | |
11462 | error_at (OMP_CLAUSE_LOCATION (c), | |
11463 | "low bound %qE above array section size " | |
11464 | "in %qs clause", low_bound, | |
11465 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11466 | return error_mark_node; | |
11467 | } | |
11468 | if (tree_int_cst_equal (size, low_bound)) | |
11469 | maybe_zero_len = true; | |
11470 | else if (length == NULL_TREE | |
11471 | && first_non_one == types.length () | |
11472 | && tree_int_cst_equal | |
11473 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
11474 | low_bound)) | |
11475 | first_non_one++; | |
11476 | } | |
11477 | else if (length == NULL_TREE) | |
11478 | { | |
11479 | maybe_zero_len = true; | |
11480 | if (first_non_one == types.length ()) | |
11481 | first_non_one++; | |
11482 | } | |
11483 | if (length && TREE_CODE (length) == INTEGER_CST) | |
11484 | { | |
11485 | if (tree_int_cst_lt (size, length)) | |
11486 | { | |
11487 | error_at (OMP_CLAUSE_LOCATION (c), | |
11488 | "length %qE above array section size " | |
11489 | "in %qs clause", length, | |
11490 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11491 | return error_mark_node; | |
11492 | } | |
11493 | if (TREE_CODE (low_bound) == INTEGER_CST) | |
11494 | { | |
11495 | tree lbpluslen | |
11496 | = size_binop (PLUS_EXPR, | |
11497 | fold_convert (sizetype, low_bound), | |
11498 | fold_convert (sizetype, length)); | |
11499 | if (TREE_CODE (lbpluslen) == INTEGER_CST | |
11500 | && tree_int_cst_lt (size, lbpluslen)) | |
11501 | { | |
11502 | error_at (OMP_CLAUSE_LOCATION (c), | |
11503 | "high bound %qE above array section size " | |
11504 | "in %qs clause", lbpluslen, | |
11505 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11506 | return error_mark_node; | |
11507 | } | |
11508 | } | |
11509 | } | |
11510 | } | |
11511 | else if (length == NULL_TREE) | |
11512 | { | |
11513 | maybe_zero_len = true; | |
11514 | if (first_non_one == types.length ()) | |
11515 | first_non_one++; | |
11516 | } | |
11517 | ||
11518 | /* For [lb:] we will need to evaluate lb more than once. */ | |
11519 | if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) | |
11520 | { | |
11521 | tree lb = c_save_expr (low_bound); | |
11522 | if (lb != low_bound) | |
11523 | { | |
11524 | TREE_PURPOSE (t) = lb; | |
11525 | low_bound = lb; | |
11526 | } | |
11527 | } | |
11528 | } | |
11529 | else if (TREE_CODE (type) == POINTER_TYPE) | |
11530 | { | |
11531 | if (length == NULL_TREE) | |
11532 | { | |
11533 | error_at (OMP_CLAUSE_LOCATION (c), | |
11534 | "for pointer type length expression must be specified"); | |
11535 | return error_mark_node; | |
11536 | } | |
11537 | /* If there is a pointer type anywhere but in the very first | |
11538 | array-section-subscript, the array section can't be contiguous. */ | |
11539 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND | |
11540 | && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST) | |
11541 | { | |
11542 | error_at (OMP_CLAUSE_LOCATION (c), | |
11543 | "array section is not contiguous in %qs clause", | |
11544 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11545 | return error_mark_node; | |
11546 | } | |
11547 | } | |
11548 | else | |
11549 | { | |
11550 | error_at (OMP_CLAUSE_LOCATION (c), | |
11551 | "%qE does not have pointer or array type", ret); | |
11552 | return error_mark_node; | |
11553 | } | |
11554 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) | |
11555 | types.safe_push (TREE_TYPE (ret)); | |
11556 | /* We will need to evaluate lb more than once. */ | |
11557 | tree lb = c_save_expr (low_bound); | |
11558 | if (lb != low_bound) | |
11559 | { | |
11560 | TREE_PURPOSE (t) = lb; | |
11561 | low_bound = lb; | |
11562 | } | |
11563 | ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound); | |
11564 | return ret; | |
11565 | } | |
11566 | ||
11567 | /* Handle array sections for clause C. */ | |
11568 | ||
11569 | static bool | |
11570 | handle_omp_array_sections (tree c) | |
11571 | { | |
11572 | bool maybe_zero_len = false; | |
11573 | unsigned int first_non_one = 0; | |
11574 | vec<tree> types = vNULL; | |
11575 | tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types, | |
11576 | maybe_zero_len, first_non_one); | |
11577 | if (first == error_mark_node) | |
11578 | { | |
11579 | types.release (); | |
11580 | return true; | |
11581 | } | |
11582 | if (first == NULL_TREE) | |
11583 | { | |
11584 | types.release (); | |
11585 | return false; | |
11586 | } | |
11587 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND) | |
11588 | { | |
11589 | tree t = OMP_CLAUSE_DECL (c); | |
11590 | tree tem = NULL_TREE; | |
11591 | types.release (); | |
11592 | /* Need to evaluate side effects in the length expressions | |
11593 | if any. */ | |
11594 | while (TREE_CODE (t) == TREE_LIST) | |
11595 | { | |
11596 | if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t))) | |
11597 | { | |
11598 | if (tem == NULL_TREE) | |
11599 | tem = TREE_VALUE (t); | |
11600 | else | |
11601 | tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), | |
11602 | TREE_VALUE (t), tem); | |
11603 | } | |
11604 | t = TREE_CHAIN (t); | |
11605 | } | |
11606 | if (tem) | |
11607 | first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first); | |
11608 | first = c_fully_fold (first, false, NULL); | |
11609 | OMP_CLAUSE_DECL (c) = first; | |
11610 | } | |
11611 | else | |
11612 | { | |
11613 | unsigned int num = types.length (), i; | |
11614 | tree t, side_effects = NULL_TREE, size = NULL_TREE; | |
11615 | tree condition = NULL_TREE; | |
11616 | ||
11617 | if (int_size_in_bytes (TREE_TYPE (first)) <= 0) | |
11618 | maybe_zero_len = true; | |
11619 | ||
11620 | for (i = num, t = OMP_CLAUSE_DECL (c); i > 0; | |
11621 | t = TREE_CHAIN (t)) | |
11622 | { | |
11623 | tree low_bound = TREE_PURPOSE (t); | |
11624 | tree length = TREE_VALUE (t); | |
11625 | ||
11626 | i--; | |
11627 | if (low_bound | |
11628 | && TREE_CODE (low_bound) == INTEGER_CST | |
11629 | && TYPE_PRECISION (TREE_TYPE (low_bound)) | |
11630 | > TYPE_PRECISION (sizetype)) | |
11631 | low_bound = fold_convert (sizetype, low_bound); | |
11632 | if (length | |
11633 | && TREE_CODE (length) == INTEGER_CST | |
11634 | && TYPE_PRECISION (TREE_TYPE (length)) | |
11635 | > TYPE_PRECISION (sizetype)) | |
11636 | length = fold_convert (sizetype, length); | |
11637 | if (low_bound == NULL_TREE) | |
11638 | low_bound = integer_zero_node; | |
11639 | if (!maybe_zero_len && i > first_non_one) | |
11640 | { | |
11641 | if (integer_nonzerop (low_bound)) | |
11642 | goto do_warn_noncontiguous; | |
11643 | if (length != NULL_TREE | |
11644 | && TREE_CODE (length) == INTEGER_CST | |
11645 | && TYPE_DOMAIN (types[i]) | |
11646 | && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])) | |
11647 | && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))) | |
11648 | == INTEGER_CST) | |
11649 | { | |
11650 | tree size; | |
11651 | size = size_binop (PLUS_EXPR, | |
11652 | TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), | |
11653 | size_one_node); | |
11654 | if (!tree_int_cst_equal (length, size)) | |
11655 | { | |
11656 | do_warn_noncontiguous: | |
11657 | error_at (OMP_CLAUSE_LOCATION (c), | |
11658 | "array section is not contiguous in %qs " | |
11659 | "clause", | |
11660 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11661 | types.release (); | |
11662 | return true; | |
11663 | } | |
11664 | } | |
11665 | if (length != NULL_TREE | |
11666 | && TREE_SIDE_EFFECTS (length)) | |
11667 | { | |
11668 | if (side_effects == NULL_TREE) | |
11669 | side_effects = length; | |
11670 | else | |
11671 | side_effects = build2 (COMPOUND_EXPR, | |
11672 | TREE_TYPE (side_effects), | |
11673 | length, side_effects); | |
11674 | } | |
11675 | } | |
11676 | else | |
11677 | { | |
11678 | tree l; | |
11679 | ||
11680 | if (i > first_non_one && length && integer_nonzerop (length)) | |
11681 | continue; | |
11682 | if (length) | |
11683 | l = fold_convert (sizetype, length); | |
11684 | else | |
11685 | { | |
11686 | l = size_binop (PLUS_EXPR, | |
11687 | TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), | |
11688 | size_one_node); | |
11689 | l = size_binop (MINUS_EXPR, l, | |
11690 | fold_convert (sizetype, low_bound)); | |
11691 | } | |
11692 | if (i > first_non_one) | |
11693 | { | |
11694 | l = fold_build2 (NE_EXPR, boolean_type_node, l, | |
11695 | size_zero_node); | |
11696 | if (condition == NULL_TREE) | |
11697 | condition = l; | |
11698 | else | |
11699 | condition = fold_build2 (BIT_AND_EXPR, boolean_type_node, | |
11700 | l, condition); | |
11701 | } | |
11702 | else if (size == NULL_TREE) | |
11703 | { | |
11704 | size = size_in_bytes (TREE_TYPE (types[i])); | |
11705 | size = size_binop (MULT_EXPR, size, l); | |
11706 | if (condition) | |
11707 | size = fold_build3 (COND_EXPR, sizetype, condition, | |
11708 | size, size_zero_node); | |
11709 | } | |
11710 | else | |
11711 | size = size_binop (MULT_EXPR, size, l); | |
11712 | } | |
11713 | } | |
11714 | types.release (); | |
11715 | if (side_effects) | |
11716 | size = build2 (COMPOUND_EXPR, sizetype, side_effects, size); | |
11717 | first = c_fully_fold (first, false, NULL); | |
11718 | OMP_CLAUSE_DECL (c) = first; | |
11719 | if (size) | |
11720 | size = c_fully_fold (size, false, NULL); | |
11721 | OMP_CLAUSE_SIZE (c) = size; | |
11722 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) | |
11723 | return false; | |
11724 | tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP); | |
11725 | OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER; | |
11726 | if (!c_mark_addressable (t)) | |
11727 | return false; | |
11728 | OMP_CLAUSE_DECL (c2) = t; | |
11729 | t = build_fold_addr_expr (first); | |
11730 | t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t); | |
11731 | tree ptr = OMP_CLAUSE_DECL (c2); | |
11732 | if (!POINTER_TYPE_P (TREE_TYPE (ptr))) | |
11733 | ptr = build_fold_addr_expr (ptr); | |
11734 | t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
11735 | ptrdiff_type_node, t, | |
11736 | fold_convert_loc (OMP_CLAUSE_LOCATION (c), | |
11737 | ptrdiff_type_node, ptr)); | |
11738 | t = c_fully_fold (t, false, NULL); | |
11739 | OMP_CLAUSE_SIZE (c2) = t; | |
11740 | OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c); | |
11741 | OMP_CLAUSE_CHAIN (c) = c2; | |
11742 | } | |
11743 | return false; | |
11744 | } | |
11745 | ||
11746 | /* Helper function of finish_omp_clauses. Clone STMT as if we were making | |
11747 | an inline call. But, remap | |
11748 | the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER | |
11749 | and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */ | |
11750 | ||
11751 | static tree | |
11752 | c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2, | |
11753 | tree decl, tree placeholder) | |
11754 | { | |
11755 | copy_body_data id; | |
b787e7a2 | 11756 | hash_map<tree, tree> decl_map; |
acf0174b | 11757 | |
b787e7a2 TS |
11758 | decl_map.put (omp_decl1, placeholder); |
11759 | decl_map.put (omp_decl2, decl); | |
acf0174b JJ |
11760 | memset (&id, 0, sizeof (id)); |
11761 | id.src_fn = DECL_CONTEXT (omp_decl1); | |
11762 | id.dst_fn = current_function_decl; | |
11763 | id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn); | |
b787e7a2 | 11764 | id.decl_map = &decl_map; |
acf0174b JJ |
11765 | |
11766 | id.copy_decl = copy_decl_no_change; | |
11767 | id.transform_call_graph_edges = CB_CGE_DUPLICATE; | |
11768 | id.transform_new_cfg = true; | |
11769 | id.transform_return_to_modify = false; | |
11770 | id.transform_lang_insert_block = NULL; | |
11771 | id.eh_lp_nr = 0; | |
11772 | walk_tree (&stmt, copy_tree_body_r, &id, NULL); | |
acf0174b JJ |
11773 | return stmt; |
11774 | } | |
11775 | ||
11776 | /* Helper function of c_finish_omp_clauses, called via walk_tree. | |
11777 | Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */ | |
11778 | ||
11779 | static tree | |
11780 | c_find_omp_placeholder_r (tree *tp, int *, void *data) | |
11781 | { | |
11782 | if (*tp == (tree) data) | |
11783 | return *tp; | |
11784 | return NULL_TREE; | |
11785 | } | |
11786 | ||
953ff289 DN |
11787 | /* For all elements of CLAUSES, validate them vs OpenMP constraints. |
11788 | Remove any elements from the list that are invalid. */ | |
11789 | ||
11790 | tree | |
11791 | c_finish_omp_clauses (tree clauses) | |
11792 | { | |
11793 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
acf0174b | 11794 | bitmap_head aligned_head; |
f3316c6d | 11795 | tree c, t, *pc; |
acf0174b JJ |
11796 | bool branch_seen = false; |
11797 | bool copyprivate_seen = false; | |
11798 | tree *nowait_clause = NULL; | |
953ff289 DN |
11799 | |
11800 | bitmap_obstack_initialize (NULL); | |
11801 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
11802 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
11803 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
acf0174b | 11804 | bitmap_initialize (&aligned_head, &bitmap_default_obstack); |
953ff289 DN |
11805 | |
11806 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
11807 | { | |
11808 | bool remove = false; | |
11809 | bool need_complete = false; | |
11810 | bool need_implicitly_determined = false; | |
11811 | ||
aaf46ef9 | 11812 | switch (OMP_CLAUSE_CODE (c)) |
953ff289 DN |
11813 | { |
11814 | case OMP_CLAUSE_SHARED: | |
953ff289 DN |
11815 | need_implicitly_determined = true; |
11816 | goto check_dup_generic; | |
11817 | ||
11818 | case OMP_CLAUSE_PRIVATE: | |
953ff289 DN |
11819 | need_complete = true; |
11820 | need_implicitly_determined = true; | |
11821 | goto check_dup_generic; | |
11822 | ||
11823 | case OMP_CLAUSE_REDUCTION: | |
953ff289 DN |
11824 | need_implicitly_determined = true; |
11825 | t = OMP_CLAUSE_DECL (c); | |
acf0174b | 11826 | if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE |
652fea39 JJ |
11827 | && (FLOAT_TYPE_P (TREE_TYPE (t)) |
11828 | || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)) | |
953ff289 DN |
11829 | { |
11830 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
11831 | const char *r_name = NULL; | |
11832 | ||
11833 | switch (r_code) | |
11834 | { | |
11835 | case PLUS_EXPR: | |
11836 | case MULT_EXPR: | |
11837 | case MINUS_EXPR: | |
652fea39 | 11838 | break; |
20906c66 | 11839 | case MIN_EXPR: |
652fea39 JJ |
11840 | if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE) |
11841 | r_name = "min"; | |
11842 | break; | |
20906c66 | 11843 | case MAX_EXPR: |
652fea39 JJ |
11844 | if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE) |
11845 | r_name = "max"; | |
953ff289 DN |
11846 | break; |
11847 | case BIT_AND_EXPR: | |
11848 | r_name = "&"; | |
11849 | break; | |
11850 | case BIT_XOR_EXPR: | |
11851 | r_name = "^"; | |
11852 | break; | |
11853 | case BIT_IOR_EXPR: | |
11854 | r_name = "|"; | |
11855 | break; | |
11856 | case TRUTH_ANDIF_EXPR: | |
652fea39 JJ |
11857 | if (FLOAT_TYPE_P (TREE_TYPE (t))) |
11858 | r_name = "&&"; | |
953ff289 DN |
11859 | break; |
11860 | case TRUTH_ORIF_EXPR: | |
652fea39 JJ |
11861 | if (FLOAT_TYPE_P (TREE_TYPE (t))) |
11862 | r_name = "||"; | |
953ff289 DN |
11863 | break; |
11864 | default: | |
11865 | gcc_unreachable (); | |
11866 | } | |
11867 | if (r_name) | |
11868 | { | |
c2255bc4 AH |
11869 | error_at (OMP_CLAUSE_LOCATION (c), |
11870 | "%qE has invalid type for %<reduction(%s)%>", | |
11871 | t, r_name); | |
953ff289 | 11872 | remove = true; |
ee1d5a02 | 11873 | break; |
953ff289 DN |
11874 | } |
11875 | } | |
acf0174b JJ |
11876 | else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node) |
11877 | { | |
11878 | error_at (OMP_CLAUSE_LOCATION (c), | |
11879 | "user defined reduction not found for %qD", t); | |
11880 | remove = true; | |
ee1d5a02 | 11881 | break; |
acf0174b JJ |
11882 | } |
11883 | else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)) | |
11884 | { | |
11885 | tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c); | |
11886 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t)); | |
11887 | tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c), | |
11888 | VAR_DECL, NULL_TREE, type); | |
11889 | OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder; | |
11890 | DECL_ARTIFICIAL (placeholder) = 1; | |
11891 | DECL_IGNORED_P (placeholder) = 1; | |
11892 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0))) | |
11893 | c_mark_addressable (placeholder); | |
11894 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1))) | |
11895 | c_mark_addressable (OMP_CLAUSE_DECL (c)); | |
11896 | OMP_CLAUSE_REDUCTION_MERGE (c) | |
11897 | = c_clone_omp_udr (TREE_VEC_ELT (list, 2), | |
11898 | TREE_VEC_ELT (list, 0), | |
11899 | TREE_VEC_ELT (list, 1), | |
11900 | OMP_CLAUSE_DECL (c), placeholder); | |
11901 | OMP_CLAUSE_REDUCTION_MERGE (c) | |
11902 | = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, | |
11903 | void_type_node, NULL_TREE, | |
11904 | OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE); | |
11905 | TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1; | |
11906 | if (TREE_VEC_LENGTH (list) == 6) | |
11907 | { | |
11908 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3))) | |
11909 | c_mark_addressable (OMP_CLAUSE_DECL (c)); | |
11910 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4))) | |
11911 | c_mark_addressable (placeholder); | |
11912 | tree init = TREE_VEC_ELT (list, 5); | |
11913 | if (init == error_mark_node) | |
11914 | init = DECL_INITIAL (TREE_VEC_ELT (list, 3)); | |
11915 | OMP_CLAUSE_REDUCTION_INIT (c) | |
11916 | = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4), | |
11917 | TREE_VEC_ELT (list, 3), | |
11918 | OMP_CLAUSE_DECL (c), placeholder); | |
11919 | if (TREE_VEC_ELT (list, 5) == error_mark_node) | |
11920 | OMP_CLAUSE_REDUCTION_INIT (c) | |
11921 | = build2 (INIT_EXPR, TREE_TYPE (t), t, | |
11922 | OMP_CLAUSE_REDUCTION_INIT (c)); | |
11923 | if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c), | |
11924 | c_find_omp_placeholder_r, | |
11925 | placeholder, NULL)) | |
11926 | OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1; | |
11927 | } | |
11928 | else | |
11929 | { | |
11930 | tree init; | |
11931 | if (AGGREGATE_TYPE_P (TREE_TYPE (t))) | |
11932 | init = build_constructor (TREE_TYPE (t), NULL); | |
11933 | else | |
11934 | init = fold_convert (TREE_TYPE (t), integer_zero_node); | |
11935 | OMP_CLAUSE_REDUCTION_INIT (c) | |
11936 | = build2 (INIT_EXPR, TREE_TYPE (t), t, init); | |
11937 | } | |
11938 | OMP_CLAUSE_REDUCTION_INIT (c) | |
11939 | = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, | |
11940 | void_type_node, NULL_TREE, | |
11941 | OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE); | |
11942 | TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1; | |
11943 | } | |
953ff289 DN |
11944 | goto check_dup_generic; |
11945 | ||
11946 | case OMP_CLAUSE_COPYPRIVATE: | |
acf0174b JJ |
11947 | copyprivate_seen = true; |
11948 | if (nowait_clause) | |
11949 | { | |
11950 | error_at (OMP_CLAUSE_LOCATION (*nowait_clause), | |
11951 | "%<nowait%> clause must not be used together " | |
11952 | "with %<copyprivate%>"); | |
11953 | *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause); | |
11954 | nowait_clause = NULL; | |
11955 | } | |
953ff289 DN |
11956 | goto check_dup_generic; |
11957 | ||
11958 | case OMP_CLAUSE_COPYIN: | |
953ff289 DN |
11959 | t = OMP_CLAUSE_DECL (c); |
11960 | if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) | |
11961 | { | |
c2255bc4 AH |
11962 | error_at (OMP_CLAUSE_LOCATION (c), |
11963 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
953ff289 | 11964 | remove = true; |
ee1d5a02 | 11965 | break; |
953ff289 DN |
11966 | } |
11967 | goto check_dup_generic; | |
11968 | ||
acf0174b JJ |
11969 | case OMP_CLAUSE_LINEAR: |
11970 | t = OMP_CLAUSE_DECL (c); | |
11971 | if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) | |
11972 | && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE) | |
11973 | { | |
11974 | error_at (OMP_CLAUSE_LOCATION (c), | |
11975 | "linear clause applied to non-integral non-pointer " | |
11976 | "variable with type %qT", TREE_TYPE (t)); | |
11977 | remove = true; | |
11978 | break; | |
11979 | } | |
11980 | if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE) | |
11981 | { | |
11982 | tree s = OMP_CLAUSE_LINEAR_STEP (c); | |
11983 | s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR, | |
11984 | OMP_CLAUSE_DECL (c), s); | |
11985 | s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
11986 | sizetype, s, OMP_CLAUSE_DECL (c)); | |
11987 | if (s == error_mark_node) | |
11988 | s = size_one_node; | |
11989 | OMP_CLAUSE_LINEAR_STEP (c) = s; | |
11990 | } | |
da6f124d JJ |
11991 | else |
11992 | OMP_CLAUSE_LINEAR_STEP (c) | |
11993 | = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c)); | |
acf0174b JJ |
11994 | goto check_dup_generic; |
11995 | ||
953ff289 DN |
11996 | check_dup_generic: |
11997 | t = OMP_CLAUSE_DECL (c); | |
11998 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11999 | { | |
c2255bc4 | 12000 | error_at (OMP_CLAUSE_LOCATION (c), |
acf0174b JJ |
12001 | "%qE is not a variable in clause %qs", t, |
12002 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
953ff289 DN |
12003 | remove = true; |
12004 | } | |
12005 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12006 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
12007 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
12008 | { | |
c2255bc4 AH |
12009 | error_at (OMP_CLAUSE_LOCATION (c), |
12010 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12011 | remove = true; |
12012 | } | |
12013 | else | |
12014 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
12015 | break; | |
12016 | ||
12017 | case OMP_CLAUSE_FIRSTPRIVATE: | |
953ff289 DN |
12018 | t = OMP_CLAUSE_DECL (c); |
12019 | need_complete = true; | |
12020 | need_implicitly_determined = true; | |
12021 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
12022 | { | |
c2255bc4 AH |
12023 | error_at (OMP_CLAUSE_LOCATION (c), |
12024 | "%qE is not a variable in clause %<firstprivate%>", t); | |
953ff289 DN |
12025 | remove = true; |
12026 | } | |
12027 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12028 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
12029 | { | |
c2255bc4 AH |
12030 | error_at (OMP_CLAUSE_LOCATION (c), |
12031 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12032 | remove = true; |
12033 | } | |
12034 | else | |
12035 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
12036 | break; | |
12037 | ||
12038 | case OMP_CLAUSE_LASTPRIVATE: | |
953ff289 DN |
12039 | t = OMP_CLAUSE_DECL (c); |
12040 | need_complete = true; | |
12041 | need_implicitly_determined = true; | |
12042 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
12043 | { | |
c2255bc4 AH |
12044 | error_at (OMP_CLAUSE_LOCATION (c), |
12045 | "%qE is not a variable in clause %<lastprivate%>", t); | |
953ff289 DN |
12046 | remove = true; |
12047 | } | |
12048 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
12049 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
12050 | { | |
c2255bc4 AH |
12051 | error_at (OMP_CLAUSE_LOCATION (c), |
12052 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
12053 | remove = true; |
12054 | } | |
12055 | else | |
12056 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
12057 | break; | |
12058 | ||
acf0174b JJ |
12059 | case OMP_CLAUSE_ALIGNED: |
12060 | t = OMP_CLAUSE_DECL (c); | |
12061 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
12062 | { | |
12063 | error_at (OMP_CLAUSE_LOCATION (c), | |
12064 | "%qE is not a variable in %<aligned%> clause", t); | |
12065 | remove = true; | |
12066 | } | |
5a9785fb JJ |
12067 | else if (!POINTER_TYPE_P (TREE_TYPE (t)) |
12068 | && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE) | |
12069 | { | |
12070 | error_at (OMP_CLAUSE_LOCATION (c), | |
12071 | "%qE in %<aligned%> clause is neither a pointer nor " | |
12072 | "an array", t); | |
12073 | remove = true; | |
12074 | } | |
acf0174b JJ |
12075 | else if (bitmap_bit_p (&aligned_head, DECL_UID (t))) |
12076 | { | |
12077 | error_at (OMP_CLAUSE_LOCATION (c), | |
12078 | "%qE appears more than once in %<aligned%> clauses", | |
12079 | t); | |
12080 | remove = true; | |
12081 | } | |
12082 | else | |
12083 | bitmap_set_bit (&aligned_head, DECL_UID (t)); | |
12084 | break; | |
12085 | ||
12086 | case OMP_CLAUSE_DEPEND: | |
12087 | t = OMP_CLAUSE_DECL (c); | |
12088 | if (TREE_CODE (t) == TREE_LIST) | |
12089 | { | |
12090 | if (handle_omp_array_sections (c)) | |
12091 | remove = true; | |
12092 | break; | |
12093 | } | |
12094 | if (t == error_mark_node) | |
12095 | remove = true; | |
12096 | else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
12097 | { | |
12098 | error_at (OMP_CLAUSE_LOCATION (c), | |
12099 | "%qE is not a variable in %<depend%> clause", t); | |
12100 | remove = true; | |
12101 | } | |
12102 | else if (!c_mark_addressable (t)) | |
12103 | remove = true; | |
12104 | break; | |
12105 | ||
12106 | case OMP_CLAUSE_MAP: | |
12107 | case OMP_CLAUSE_TO: | |
12108 | case OMP_CLAUSE_FROM: | |
12109 | t = OMP_CLAUSE_DECL (c); | |
12110 | if (TREE_CODE (t) == TREE_LIST) | |
12111 | { | |
12112 | if (handle_omp_array_sections (c)) | |
12113 | remove = true; | |
12114 | else | |
12115 | { | |
12116 | t = OMP_CLAUSE_DECL (c); | |
b17a8b07 | 12117 | if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) |
acf0174b JJ |
12118 | { |
12119 | error_at (OMP_CLAUSE_LOCATION (c), | |
12120 | "array section does not have mappable type " | |
12121 | "in %qs clause", | |
12122 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12123 | remove = true; | |
12124 | } | |
12125 | } | |
12126 | break; | |
12127 | } | |
12128 | if (t == error_mark_node) | |
12129 | remove = true; | |
12130 | else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
12131 | { | |
12132 | error_at (OMP_CLAUSE_LOCATION (c), | |
12133 | "%qE is not a variable in %qs clause", t, | |
12134 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12135 | remove = true; | |
12136 | } | |
12137 | else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
12138 | { | |
12139 | error_at (OMP_CLAUSE_LOCATION (c), | |
12140 | "%qD is threadprivate variable in %qs clause", t, | |
12141 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12142 | remove = true; | |
12143 | } | |
12144 | else if (!c_mark_addressable (t)) | |
12145 | remove = true; | |
b17a8b07 TS |
12146 | else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP |
12147 | && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER) | |
12148 | && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t))) | |
acf0174b JJ |
12149 | { |
12150 | error_at (OMP_CLAUSE_LOCATION (c), | |
12151 | "%qD does not have a mappable type in %qs clause", t, | |
12152 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12153 | remove = true; | |
12154 | } | |
12155 | else if (bitmap_bit_p (&generic_head, DECL_UID (t))) | |
12156 | { | |
12157 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) | |
12158 | error ("%qD appears more than once in motion clauses", t); | |
12159 | else | |
12160 | error ("%qD appears more than once in map clauses", t); | |
12161 | remove = true; | |
12162 | } | |
12163 | else | |
12164 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
12165 | break; | |
12166 | ||
12167 | case OMP_CLAUSE_UNIFORM: | |
12168 | t = OMP_CLAUSE_DECL (c); | |
12169 | if (TREE_CODE (t) != PARM_DECL) | |
12170 | { | |
12171 | if (DECL_P (t)) | |
12172 | error_at (OMP_CLAUSE_LOCATION (c), | |
12173 | "%qD is not an argument in %<uniform%> clause", t); | |
12174 | else | |
12175 | error_at (OMP_CLAUSE_LOCATION (c), | |
12176 | "%qE is not an argument in %<uniform%> clause", t); | |
12177 | remove = true; | |
ee1d5a02 | 12178 | break; |
acf0174b | 12179 | } |
ee1d5a02 | 12180 | goto check_dup_generic; |
acf0174b JJ |
12181 | |
12182 | case OMP_CLAUSE_NOWAIT: | |
12183 | if (copyprivate_seen) | |
12184 | { | |
12185 | error_at (OMP_CLAUSE_LOCATION (c), | |
12186 | "%<nowait%> clause must not be used together " | |
12187 | "with %<copyprivate%>"); | |
12188 | remove = true; | |
12189 | break; | |
12190 | } | |
12191 | nowait_clause = pc; | |
12192 | pc = &OMP_CLAUSE_CHAIN (c); | |
12193 | continue; | |
12194 | ||
953ff289 DN |
12195 | case OMP_CLAUSE_IF: |
12196 | case OMP_CLAUSE_NUM_THREADS: | |
acf0174b JJ |
12197 | case OMP_CLAUSE_NUM_TEAMS: |
12198 | case OMP_CLAUSE_THREAD_LIMIT: | |
953ff289 | 12199 | case OMP_CLAUSE_SCHEDULE: |
953ff289 DN |
12200 | case OMP_CLAUSE_ORDERED: |
12201 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 JJ |
12202 | case OMP_CLAUSE_UNTIED: |
12203 | case OMP_CLAUSE_COLLAPSE: | |
20906c66 JJ |
12204 | case OMP_CLAUSE_FINAL: |
12205 | case OMP_CLAUSE_MERGEABLE: | |
acf0174b JJ |
12206 | case OMP_CLAUSE_SAFELEN: |
12207 | case OMP_CLAUSE_SIMDLEN: | |
12208 | case OMP_CLAUSE_DEVICE: | |
12209 | case OMP_CLAUSE_DIST_SCHEDULE: | |
12210 | case OMP_CLAUSE_PARALLEL: | |
12211 | case OMP_CLAUSE_FOR: | |
12212 | case OMP_CLAUSE_SECTIONS: | |
12213 | case OMP_CLAUSE_TASKGROUP: | |
12214 | case OMP_CLAUSE_PROC_BIND: | |
12215 | pc = &OMP_CLAUSE_CHAIN (c); | |
12216 | continue; | |
12217 | ||
12218 | case OMP_CLAUSE_INBRANCH: | |
12219 | case OMP_CLAUSE_NOTINBRANCH: | |
12220 | if (branch_seen) | |
12221 | { | |
12222 | error_at (OMP_CLAUSE_LOCATION (c), | |
12223 | "%<inbranch%> clause is incompatible with " | |
12224 | "%<notinbranch%>"); | |
12225 | remove = true; | |
12226 | break; | |
12227 | } | |
12228 | branch_seen = true; | |
953ff289 DN |
12229 | pc = &OMP_CLAUSE_CHAIN (c); |
12230 | continue; | |
12231 | ||
12232 | default: | |
12233 | gcc_unreachable (); | |
12234 | } | |
12235 | ||
12236 | if (!remove) | |
12237 | { | |
12238 | t = OMP_CLAUSE_DECL (c); | |
12239 | ||
12240 | if (need_complete) | |
12241 | { | |
12242 | t = require_complete_type (t); | |
12243 | if (t == error_mark_node) | |
12244 | remove = true; | |
12245 | } | |
12246 | ||
12247 | if (need_implicitly_determined) | |
12248 | { | |
12249 | const char *share_name = NULL; | |
12250 | ||
12251 | if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
12252 | share_name = "threadprivate"; | |
12253 | else switch (c_omp_predetermined_sharing (t)) | |
12254 | { | |
12255 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
12256 | break; | |
12257 | case OMP_CLAUSE_DEFAULT_SHARED: | |
20906c66 JJ |
12258 | /* const vars may be specified in firstprivate clause. */ |
12259 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
12260 | && TREE_READONLY (t)) | |
12261 | break; | |
953ff289 DN |
12262 | share_name = "shared"; |
12263 | break; | |
12264 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
12265 | share_name = "private"; | |
12266 | break; | |
12267 | default: | |
12268 | gcc_unreachable (); | |
12269 | } | |
12270 | if (share_name) | |
12271 | { | |
c2255bc4 AH |
12272 | error_at (OMP_CLAUSE_LOCATION (c), |
12273 | "%qE is predetermined %qs for %qs", | |
acf0174b JJ |
12274 | t, share_name, |
12275 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
953ff289 DN |
12276 | remove = true; |
12277 | } | |
12278 | } | |
12279 | } | |
12280 | ||
12281 | if (remove) | |
12282 | *pc = OMP_CLAUSE_CHAIN (c); | |
12283 | else | |
12284 | pc = &OMP_CLAUSE_CHAIN (c); | |
12285 | } | |
12286 | ||
12287 | bitmap_obstack_release (NULL); | |
12288 | return clauses; | |
12289 | } | |
9ae165a0 | 12290 | |
0a35513e AH |
12291 | /* Create a transaction node. */ |
12292 | ||
12293 | tree | |
12294 | c_finish_transaction (location_t loc, tree block, int flags) | |
12295 | { | |
12296 | tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); | |
12297 | if (flags & TM_STMT_ATTR_OUTER) | |
12298 | TRANSACTION_EXPR_OUTER (stmt) = 1; | |
12299 | if (flags & TM_STMT_ATTR_RELAXED) | |
12300 | TRANSACTION_EXPR_RELAXED (stmt) = 1; | |
12301 | return add_stmt (stmt); | |
12302 | } | |
12303 | ||
9ae165a0 DG |
12304 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
12305 | down to the element type of an array. */ | |
12306 | ||
12307 | tree | |
12308 | c_build_qualified_type (tree type, int type_quals) | |
12309 | { | |
12310 | if (type == error_mark_node) | |
12311 | return type; | |
12312 | ||
12313 | if (TREE_CODE (type) == ARRAY_TYPE) | |
12314 | { | |
12315 | tree t; | |
12316 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
12317 | type_quals); | |
12318 | ||
12319 | /* See if we already have an identically qualified type. */ | |
12320 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
12321 | { | |
12322 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
12323 | && TYPE_NAME (t) == TYPE_NAME (type) | |
12324 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
12325 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
12326 | TYPE_ATTRIBUTES (type))) | |
12327 | break; | |
12328 | } | |
12329 | if (!t) | |
12330 | { | |
12331 | tree domain = TYPE_DOMAIN (type); | |
12332 | ||
12333 | t = build_variant_type_copy (type); | |
12334 | TREE_TYPE (t) = element_type; | |
12335 | ||
12336 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
12337 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
12338 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
12339 | else if (TYPE_CANONICAL (element_type) != element_type | |
12340 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
12341 | { | |
b8698a0f | 12342 | tree unqualified_canon |
9ae165a0 | 12343 | = build_array_type (TYPE_CANONICAL (element_type), |
b8698a0f | 12344 | domain? TYPE_CANONICAL (domain) |
9ae165a0 | 12345 | : NULL_TREE); |
b8698a0f | 12346 | TYPE_CANONICAL (t) |
9ae165a0 DG |
12347 | = c_build_qualified_type (unqualified_canon, type_quals); |
12348 | } | |
12349 | else | |
12350 | TYPE_CANONICAL (t) = t; | |
12351 | } | |
12352 | return t; | |
12353 | } | |
12354 | ||
12355 | /* A restrict-qualified pointer type must be a pointer to object or | |
12356 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
12357 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
12358 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
12359 | && (!POINTER_TYPE_P (type) | |
12360 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
12361 | { | |
12362 | error ("invalid use of %<restrict%>"); | |
12363 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
12364 | } | |
12365 | ||
12366 | return build_qualified_type (type, type_quals); | |
12367 | } | |
72b5577d ILT |
12368 | |
12369 | /* Build a VA_ARG_EXPR for the C parser. */ | |
12370 | ||
12371 | tree | |
c2255bc4 | 12372 | c_build_va_arg (location_t loc, tree expr, tree type) |
72b5577d ILT |
12373 | { |
12374 | if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) | |
12375 | warning_at (loc, OPT_Wc___compat, | |
12376 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
c2255bc4 | 12377 | return build_va_arg (loc, expr, type); |
72b5577d | 12378 | } |
acf0174b JJ |
12379 | |
12380 | /* Return truthvalue of whether T1 is the same tree structure as T2. | |
12381 | Return 1 if they are the same. Return 0 if they are different. */ | |
12382 | ||
12383 | bool | |
12384 | c_tree_equal (tree t1, tree t2) | |
12385 | { | |
12386 | enum tree_code code1, code2; | |
12387 | ||
12388 | if (t1 == t2) | |
12389 | return true; | |
12390 | if (!t1 || !t2) | |
12391 | return false; | |
12392 | ||
12393 | for (code1 = TREE_CODE (t1); | |
12394 | CONVERT_EXPR_CODE_P (code1) | |
12395 | || code1 == NON_LVALUE_EXPR; | |
12396 | code1 = TREE_CODE (t1)) | |
12397 | t1 = TREE_OPERAND (t1, 0); | |
12398 | for (code2 = TREE_CODE (t2); | |
12399 | CONVERT_EXPR_CODE_P (code2) | |
12400 | || code2 == NON_LVALUE_EXPR; | |
12401 | code2 = TREE_CODE (t2)) | |
12402 | t2 = TREE_OPERAND (t2, 0); | |
12403 | ||
12404 | /* They might have become equal now. */ | |
12405 | if (t1 == t2) | |
12406 | return true; | |
12407 | ||
12408 | if (code1 != code2) | |
12409 | return false; | |
12410 | ||
12411 | switch (code1) | |
12412 | { | |
12413 | case INTEGER_CST: | |
807e902e | 12414 | return wi::eq_p (t1, t2); |
acf0174b JJ |
12415 | |
12416 | case REAL_CST: | |
12417 | return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2)); | |
12418 | ||
12419 | case STRING_CST: | |
12420 | return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2) | |
12421 | && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), | |
12422 | TREE_STRING_LENGTH (t1)); | |
12423 | ||
12424 | case FIXED_CST: | |
12425 | return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), | |
12426 | TREE_FIXED_CST (t2)); | |
12427 | ||
12428 | case COMPLEX_CST: | |
12429 | return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2)) | |
12430 | && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2)); | |
12431 | ||
12432 | case VECTOR_CST: | |
12433 | return operand_equal_p (t1, t2, OEP_ONLY_CONST); | |
12434 | ||
12435 | case CONSTRUCTOR: | |
12436 | /* We need to do this when determining whether or not two | |
12437 | non-type pointer to member function template arguments | |
12438 | are the same. */ | |
12439 | if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)) | |
12440 | || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2)) | |
12441 | return false; | |
12442 | { | |
12443 | tree field, value; | |
12444 | unsigned int i; | |
12445 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value) | |
12446 | { | |
12447 | constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i); | |
12448 | if (!c_tree_equal (field, elt2->index) | |
12449 | || !c_tree_equal (value, elt2->value)) | |
12450 | return false; | |
12451 | } | |
12452 | } | |
12453 | return true; | |
12454 | ||
12455 | case TREE_LIST: | |
12456 | if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))) | |
12457 | return false; | |
12458 | if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2))) | |
12459 | return false; | |
12460 | return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
12461 | ||
12462 | case SAVE_EXPR: | |
12463 | return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
12464 | ||
12465 | case CALL_EXPR: | |
12466 | { | |
12467 | tree arg1, arg2; | |
12468 | call_expr_arg_iterator iter1, iter2; | |
12469 | if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) | |
12470 | return false; | |
12471 | for (arg1 = first_call_expr_arg (t1, &iter1), | |
12472 | arg2 = first_call_expr_arg (t2, &iter2); | |
12473 | arg1 && arg2; | |
12474 | arg1 = next_call_expr_arg (&iter1), | |
12475 | arg2 = next_call_expr_arg (&iter2)) | |
12476 | if (!c_tree_equal (arg1, arg2)) | |
12477 | return false; | |
12478 | if (arg1 || arg2) | |
12479 | return false; | |
12480 | return true; | |
12481 | } | |
12482 | ||
12483 | case TARGET_EXPR: | |
12484 | { | |
12485 | tree o1 = TREE_OPERAND (t1, 0); | |
12486 | tree o2 = TREE_OPERAND (t2, 0); | |
12487 | ||
12488 | /* Special case: if either target is an unallocated VAR_DECL, | |
12489 | it means that it's going to be unified with whatever the | |
12490 | TARGET_EXPR is really supposed to initialize, so treat it | |
12491 | as being equivalent to anything. */ | |
12492 | if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE | |
12493 | && !DECL_RTL_SET_P (o1)) | |
12494 | /*Nop*/; | |
12495 | else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE | |
12496 | && !DECL_RTL_SET_P (o2)) | |
12497 | /*Nop*/; | |
12498 | else if (!c_tree_equal (o1, o2)) | |
12499 | return false; | |
12500 | ||
12501 | return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); | |
12502 | } | |
12503 | ||
12504 | case COMPONENT_REF: | |
12505 | if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)) | |
12506 | return false; | |
12507 | return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
12508 | ||
12509 | case PARM_DECL: | |
12510 | case VAR_DECL: | |
12511 | case CONST_DECL: | |
12512 | case FIELD_DECL: | |
12513 | case FUNCTION_DECL: | |
12514 | case IDENTIFIER_NODE: | |
12515 | case SSA_NAME: | |
12516 | return false; | |
12517 | ||
12518 | case TREE_VEC: | |
12519 | { | |
12520 | unsigned ix; | |
12521 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
12522 | return false; | |
12523 | for (ix = TREE_VEC_LENGTH (t1); ix--;) | |
12524 | if (!c_tree_equal (TREE_VEC_ELT (t1, ix), | |
12525 | TREE_VEC_ELT (t2, ix))) | |
12526 | return false; | |
12527 | return true; | |
12528 | } | |
12529 | ||
12530 | default: | |
12531 | break; | |
12532 | } | |
12533 | ||
12534 | switch (TREE_CODE_CLASS (code1)) | |
12535 | { | |
12536 | case tcc_unary: | |
12537 | case tcc_binary: | |
12538 | case tcc_comparison: | |
12539 | case tcc_expression: | |
12540 | case tcc_vl_exp: | |
12541 | case tcc_reference: | |
12542 | case tcc_statement: | |
12543 | { | |
12544 | int i, n = TREE_OPERAND_LENGTH (t1); | |
12545 | ||
12546 | switch (code1) | |
12547 | { | |
12548 | case PREINCREMENT_EXPR: | |
12549 | case PREDECREMENT_EXPR: | |
12550 | case POSTINCREMENT_EXPR: | |
12551 | case POSTDECREMENT_EXPR: | |
12552 | n = 1; | |
12553 | break; | |
12554 | case ARRAY_REF: | |
12555 | n = 2; | |
12556 | break; | |
12557 | default: | |
12558 | break; | |
12559 | } | |
12560 | ||
12561 | if (TREE_CODE_CLASS (code1) == tcc_vl_exp | |
12562 | && n != TREE_OPERAND_LENGTH (t2)) | |
12563 | return false; | |
12564 | ||
12565 | for (i = 0; i < n; ++i) | |
12566 | if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i))) | |
12567 | return false; | |
12568 | ||
12569 | return true; | |
12570 | } | |
12571 | ||
12572 | case tcc_type: | |
12573 | return comptypes (t1, t2); | |
12574 | default: | |
12575 | gcc_unreachable (); | |
12576 | } | |
12577 | /* We can get here with --disable-checking. */ | |
12578 | return false; | |
12579 | } | |
12893402 BI |
12580 | |
12581 | /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a | |
12582 | spawn-helper and BODY is the newly created body for FNDECL. */ | |
12583 | ||
12584 | void | |
12585 | cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w) | |
12586 | { | |
12587 | tree list = alloc_stmt_list (); | |
12588 | tree frame = make_cilk_frame (fndecl); | |
12589 | tree dtor = create_cilk_function_exit (frame, false, true); | |
12590 | add_local_decl (cfun, frame); | |
12591 | ||
12592 | DECL_SAVED_TREE (fndecl) = list; | |
12593 | tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)), | |
12594 | frame); | |
12595 | tree body_list = cilk_install_body_pedigree_operations (frame_ptr); | |
12596 | gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST); | |
12597 | ||
12598 | tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr); | |
12599 | append_to_statement_list (detach_expr, &body_list); | |
12600 | ||
12601 | cilk_outline (fndecl, &body, (struct wrapper_data *) w); | |
12602 | body = fold_build_cleanup_point_expr (void_type_node, body); | |
12603 | ||
12604 | append_to_statement_list (body, &body_list); | |
12605 | append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR, | |
12606 | body_list, dtor), &list); | |
12607 | } |