]>
Commit | Line | Data |
---|---|---|
400fbf9f | 1 | /* Build expressions with type checking for C compiler. |
d1e082c2 | 2 | Copyright (C) 1987-2013 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 "pointer-set.h" |
44 | #include "basic-block.h" | |
45 | #include "gimple-expr.h" | |
45b0be94 | 46 | #include "gimplify.h" |
acf0174b | 47 | #include "tree-inline.h" |
0645c1a2 | 48 | #include "omp-low.h" |
61d3ce20 | 49 | #include "c-family/c-objc.h" |
a212e43f | 50 | #include "c-family/c-common.h" |
de5a5fa1 | 51 | #include "c-family/c-ubsan.h" |
325c3691 | 52 | |
2ac2f164 JM |
53 | /* Possible cases of implicit bad conversions. Used to select |
54 | diagnostic messages in convert_for_assignment. */ | |
55 | enum impl_conv { | |
56 | ic_argpass, | |
57 | ic_assign, | |
58 | ic_init, | |
59 | ic_return | |
60 | }; | |
61 | ||
bc4b653b JM |
62 | /* The level of nesting inside "__alignof__". */ |
63 | int in_alignof; | |
64 | ||
65 | /* The level of nesting inside "sizeof". */ | |
66 | int in_sizeof; | |
67 | ||
68 | /* The level of nesting inside "typeof". */ | |
69 | int in_typeof; | |
400fbf9f | 70 | |
1a4049e7 JJ |
71 | /* The argument of last parsed sizeof expression, only to be tested |
72 | if expr.original_code == SIZEOF_EXPR. */ | |
73 | tree c_last_sizeof_arg; | |
74 | ||
b71c7f8a | 75 | /* Nonzero if we've already printed a "missing braces around initializer" |
103b7b17 | 76 | message within this initializer. */ |
b71c7f8a | 77 | static int missing_braces_mentioned; |
103b7b17 | 78 | |
bf730f15 RS |
79 | static int require_constant_value; |
80 | static int require_constant_elements; | |
81 | ||
58f9752a | 82 | static bool null_pointer_constant_p (const_tree); |
f55ade6e | 83 | static tree qualify_type (tree, tree); |
dc5027f4 JM |
84 | static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *, |
85 | bool *); | |
744aa42f | 86 | static int comp_target_types (location_t, tree, tree); |
dc5027f4 JM |
87 | static int function_types_compatible_p (const_tree, const_tree, bool *, |
88 | bool *); | |
89 | static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *); | |
f55ade6e | 90 | static tree lookup_field (tree, tree); |
9771b263 DN |
91 | static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *, |
92 | tree, tree); | |
db3927fb | 93 | static tree pointer_diff (location_t, tree, tree); |
744aa42f ILT |
94 | static tree convert_for_assignment (location_t, tree, tree, tree, |
95 | enum impl_conv, bool, tree, tree, int); | |
f55ade6e AJ |
96 | static tree valid_compound_expr_initializer (tree, tree); |
97 | static void push_string (const char *); | |
98 | static void push_member_name (tree); | |
f55ade6e AJ |
99 | static int spelling_length (void); |
100 | static char *print_spelling (char *); | |
683d6ff9 | 101 | static void warning_init (int, const char *); |
c2255bc4 | 102 | static tree digest_init (location_t, tree, tree, tree, bool, bool, int); |
a1e3b3d9 LB |
103 | static void output_init_element (tree, tree, bool, tree, tree, int, bool, |
104 | struct obstack *); | |
105 | static void output_pending_init_elements (int, struct obstack *); | |
106 | static int set_designator (int, struct obstack *); | |
107 | static void push_range_stack (tree, struct obstack *); | |
108 | static void add_pending_init (tree, tree, tree, bool, struct obstack *); | |
109 | static void set_nonincremental_init (struct obstack *); | |
110 | static void set_nonincremental_init_from_string (tree, struct obstack *); | |
111 | static tree find_init_member (tree, struct obstack *); | |
f37acdf9 | 112 | static void readonly_warning (tree, enum lvalue_use); |
7bd11157 | 113 | static int lvalue_or_else (location_t, const_tree, enum lvalue_use); |
4e2fb7de | 114 | static void record_maybe_used_decl (tree); |
dc5027f4 | 115 | static int comptypes_internal (const_tree, const_tree, bool *, bool *); |
6aa3c60d JM |
116 | \f |
117 | /* Return true if EXP is a null pointer constant, false otherwise. */ | |
118 | ||
119 | static bool | |
58f9752a | 120 | null_pointer_constant_p (const_tree expr) |
6aa3c60d JM |
121 | { |
122 | /* This should really operate on c_expr structures, but they aren't | |
123 | yet available everywhere required. */ | |
124 | tree type = TREE_TYPE (expr); | |
125 | return (TREE_CODE (expr) == INTEGER_CST | |
8bcd6380 | 126 | && !TREE_OVERFLOW (expr) |
6aa3c60d JM |
127 | && integer_zerop (expr) |
128 | && (INTEGRAL_TYPE_P (type) | |
129 | || (TREE_CODE (type) == POINTER_TYPE | |
130 | && VOID_TYPE_P (TREE_TYPE (type)) | |
131 | && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); | |
132 | } | |
928c19bb JM |
133 | |
134 | /* EXPR may appear in an unevaluated part of an integer constant | |
135 | expression, but not in an evaluated part. Wrap it in a | |
136 | C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an | |
137 | INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ | |
138 | ||
139 | static tree | |
140 | note_integer_operands (tree expr) | |
141 | { | |
142 | tree ret; | |
143 | if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) | |
144 | { | |
145 | ret = copy_node (expr); | |
146 | TREE_OVERFLOW (ret) = 1; | |
147 | } | |
148 | else | |
149 | { | |
150 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); | |
151 | C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; | |
152 | } | |
153 | return ret; | |
154 | } | |
155 | ||
4d84fe7c JM |
156 | /* Having checked whether EXPR may appear in an unevaluated part of an |
157 | integer constant expression and found that it may, remove any | |
158 | C_MAYBE_CONST_EXPR noting this fact and return the resulting | |
159 | expression. */ | |
160 | ||
161 | static inline tree | |
162 | remove_c_maybe_const_expr (tree expr) | |
163 | { | |
164 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
165 | return C_MAYBE_CONST_EXPR_EXPR (expr); | |
166 | else | |
167 | return expr; | |
168 | } | |
169 | ||
f13c9b2c AP |
170 | \f/* This is a cache to hold if two types are compatible or not. */ |
171 | ||
172 | struct tagged_tu_seen_cache { | |
173 | const struct tagged_tu_seen_cache * next; | |
58f9752a KG |
174 | const_tree t1; |
175 | const_tree t2; | |
f13c9b2c AP |
176 | /* The return value of tagged_types_tu_compatible_p if we had seen |
177 | these two types already. */ | |
178 | int val; | |
179 | }; | |
180 | ||
181 | static const struct tagged_tu_seen_cache * tagged_tu_seen_base; | |
182 | static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); | |
183 | ||
400fbf9f JW |
184 | /* Do `exp = require_complete_type (exp);' to make sure exp |
185 | does not have an incomplete type. (That includes void types.) */ | |
186 | ||
187 | tree | |
2f6e4e97 | 188 | require_complete_type (tree value) |
400fbf9f JW |
189 | { |
190 | tree type = TREE_TYPE (value); | |
191 | ||
c3d5c3fa | 192 | if (value == error_mark_node || type == error_mark_node) |
ea0f786b CB |
193 | return error_mark_node; |
194 | ||
400fbf9f | 195 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 196 | if (COMPLETE_TYPE_P (type)) |
400fbf9f JW |
197 | return value; |
198 | ||
7a228918 | 199 | c_incomplete_type_error (value, type); |
400fbf9f JW |
200 | return error_mark_node; |
201 | } | |
202 | ||
203 | /* Print an error message for invalid use of an incomplete type. | |
204 | VALUE is the expression that was used (or 0 if that isn't known) | |
205 | and TYPE is the type that was invalid. */ | |
206 | ||
207 | void | |
ac7d7749 | 208 | c_incomplete_type_error (const_tree value, const_tree type) |
400fbf9f | 209 | { |
5d5993dd | 210 | const char *type_code_string; |
400fbf9f JW |
211 | |
212 | /* Avoid duplicate error message. */ | |
213 | if (TREE_CODE (type) == ERROR_MARK) | |
214 | return; | |
215 | ||
216 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
217 | || TREE_CODE (value) == PARM_DECL)) | |
c51a1ba9 | 218 | error ("%qD has an incomplete type", value); |
400fbf9f JW |
219 | else |
220 | { | |
221 | retry: | |
222 | /* We must print an error message. Be clever about what it says. */ | |
223 | ||
224 | switch (TREE_CODE (type)) | |
225 | { | |
226 | case RECORD_TYPE: | |
ab87f8c8 | 227 | type_code_string = "struct"; |
400fbf9f JW |
228 | break; |
229 | ||
230 | case UNION_TYPE: | |
ab87f8c8 | 231 | type_code_string = "union"; |
400fbf9f JW |
232 | break; |
233 | ||
234 | case ENUMERAL_TYPE: | |
ab87f8c8 | 235 | type_code_string = "enum"; |
400fbf9f JW |
236 | break; |
237 | ||
238 | case VOID_TYPE: | |
239 | error ("invalid use of void expression"); | |
240 | return; | |
241 | ||
242 | case ARRAY_TYPE: | |
243 | if (TYPE_DOMAIN (type)) | |
244 | { | |
fba78abb RH |
245 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) |
246 | { | |
247 | error ("invalid use of flexible array member"); | |
248 | return; | |
249 | } | |
400fbf9f JW |
250 | type = TREE_TYPE (type); |
251 | goto retry; | |
252 | } | |
253 | error ("invalid use of array with unspecified bounds"); | |
254 | return; | |
255 | ||
256 | default: | |
366de0ce | 257 | gcc_unreachable (); |
400fbf9f JW |
258 | } |
259 | ||
260 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
c51a1ba9 JM |
261 | error ("invalid use of undefined type %<%s %E%>", |
262 | type_code_string, TYPE_NAME (type)); | |
400fbf9f JW |
263 | else |
264 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
c51a1ba9 | 265 | error ("invalid use of incomplete typedef %qD", TYPE_NAME (type)); |
400fbf9f JW |
266 | } |
267 | } | |
268 | ||
ab393bf1 NB |
269 | /* Given a type, apply default promotions wrt unnamed function |
270 | arguments and return the new type. */ | |
271 | ||
272 | tree | |
2f6e4e97 | 273 | c_type_promotes_to (tree type) |
ab393bf1 | 274 | { |
267bac10 | 275 | tree ret = NULL_TREE; |
ab393bf1 | 276 | |
267bac10 JM |
277 | if (TYPE_MAIN_VARIANT (type) == float_type_node) |
278 | ret = double_type_node; | |
279 | else if (c_promoting_integer_type_p (type)) | |
ab393bf1 NB |
280 | { |
281 | /* Preserve unsignedness if not really getting any wider. */ | |
8df83eae | 282 | if (TYPE_UNSIGNED (type) |
c22cacf3 | 283 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) |
267bac10 JM |
284 | ret = unsigned_type_node; |
285 | else | |
286 | ret = integer_type_node; | |
ab393bf1 NB |
287 | } |
288 | ||
267bac10 JM |
289 | if (ret != NULL_TREE) |
290 | return (TYPE_ATOMIC (type) | |
291 | ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC) | |
292 | : ret); | |
293 | ||
ab393bf1 NB |
294 | return type; |
295 | } | |
296 | ||
36c5e70a BE |
297 | /* Return true if between two named address spaces, whether there is a superset |
298 | named address space that encompasses both address spaces. If there is a | |
299 | superset, return which address space is the superset. */ | |
300 | ||
301 | static bool | |
302 | addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) | |
303 | { | |
304 | if (as1 == as2) | |
305 | { | |
306 | *common = as1; | |
307 | return true; | |
308 | } | |
309 | else if (targetm.addr_space.subset_p (as1, as2)) | |
310 | { | |
311 | *common = as2; | |
312 | return true; | |
313 | } | |
314 | else if (targetm.addr_space.subset_p (as2, as1)) | |
315 | { | |
316 | *common = as1; | |
317 | return true; | |
318 | } | |
319 | else | |
320 | return false; | |
321 | } | |
322 | ||
400fbf9f JW |
323 | /* Return a variant of TYPE which has all the type qualifiers of LIKE |
324 | as well as those of TYPE. */ | |
325 | ||
326 | static tree | |
2f6e4e97 | 327 | qualify_type (tree type, tree like) |
400fbf9f | 328 | { |
36c5e70a BE |
329 | addr_space_t as_type = TYPE_ADDR_SPACE (type); |
330 | addr_space_t as_like = TYPE_ADDR_SPACE (like); | |
331 | addr_space_t as_common; | |
332 | ||
333 | /* If the two named address spaces are different, determine the common | |
334 | superset address space. If there isn't one, raise an error. */ | |
335 | if (!addr_space_superset (as_type, as_like, &as_common)) | |
336 | { | |
337 | as_common = as_type; | |
338 | error ("%qT and %qT are in disjoint named address spaces", | |
339 | type, like); | |
340 | } | |
341 | ||
2f6e4e97 | 342 | return c_build_qualified_type (type, |
36c5e70a | 343 | TYPE_QUALS_NO_ADDR_SPACE (type) |
267bac10 | 344 | | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like) |
36c5e70a | 345 | | ENCODE_QUAL_ADDR_SPACE (as_common)); |
400fbf9f | 346 | } |
52ffd86e MS |
347 | |
348 | /* Return true iff the given tree T is a variable length array. */ | |
349 | ||
350 | bool | |
ac7d7749 | 351 | c_vla_type_p (const_tree t) |
52ffd86e MS |
352 | { |
353 | if (TREE_CODE (t) == ARRAY_TYPE | |
354 | && C_TYPE_VARIABLE_SIZE (t)) | |
355 | return true; | |
356 | return false; | |
357 | } | |
400fbf9f | 358 | \f |
10bc1b1b | 359 | /* Return the composite type of two compatible types. |
5305f6d7 | 360 | |
10bc1b1b JM |
361 | We assume that comptypes has already been done and returned |
362 | nonzero; if that isn't so, this may crash. In particular, we | |
363 | assume that qualifiers match. */ | |
400fbf9f JW |
364 | |
365 | tree | |
10bc1b1b | 366 | composite_type (tree t1, tree t2) |
400fbf9f | 367 | { |
b3694847 SS |
368 | enum tree_code code1; |
369 | enum tree_code code2; | |
4b027d16 | 370 | tree attributes; |
400fbf9f JW |
371 | |
372 | /* Save time if the two types are the same. */ | |
373 | ||
374 | if (t1 == t2) return t1; | |
375 | ||
376 | /* If one type is nonsense, use the other. */ | |
377 | if (t1 == error_mark_node) | |
378 | return t2; | |
379 | if (t2 == error_mark_node) | |
380 | return t1; | |
381 | ||
10bc1b1b JM |
382 | code1 = TREE_CODE (t1); |
383 | code2 = TREE_CODE (t2); | |
384 | ||
d9525bec | 385 | /* Merge the attributes. */ |
5fd9b178 | 386 | attributes = targetm.merge_type_attributes (t1, t2); |
4b027d16 | 387 | |
10bc1b1b JM |
388 | /* If one is an enumerated type and the other is the compatible |
389 | integer type, the composite type might be either of the two | |
390 | (DR#013 question 3). For consistency, use the enumerated type as | |
391 | the composite type. */ | |
400fbf9f | 392 | |
10bc1b1b JM |
393 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) |
394 | return t1; | |
395 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
396 | return t2; | |
75326e8c | 397 | |
366de0ce | 398 | gcc_assert (code1 == code2); |
b6a10c9f | 399 | |
400fbf9f JW |
400 | switch (code1) |
401 | { | |
400fbf9f | 402 | case POINTER_TYPE: |
10bc1b1b | 403 | /* For two pointers, do this recursively on the target type. */ |
400fbf9f | 404 | { |
3932261a MM |
405 | tree pointed_to_1 = TREE_TYPE (t1); |
406 | tree pointed_to_2 = TREE_TYPE (t2); | |
10bc1b1b | 407 | tree target = composite_type (pointed_to_1, pointed_to_2); |
3db684fb | 408 | t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false); |
fe7080d2 AP |
409 | t1 = build_type_attribute_variant (t1, attributes); |
410 | return qualify_type (t1, t2); | |
400fbf9f | 411 | } |
400fbf9f JW |
412 | |
413 | case ARRAY_TYPE: | |
414 | { | |
10bc1b1b | 415 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
46df2823 JM |
416 | int quals; |
417 | tree unqual_elt; | |
ca8bdb78 JM |
418 | tree d1 = TYPE_DOMAIN (t1); |
419 | tree d2 = TYPE_DOMAIN (t2); | |
420 | bool d1_variable, d2_variable; | |
421 | bool d1_zero, d2_zero; | |
f6294de7 | 422 | bool t1_complete, t2_complete; |
46df2823 | 423 | |
de46b2fe | 424 | /* We should not have any type quals on arrays at all. */ |
36c5e70a BE |
425 | gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) |
426 | && !TYPE_QUALS_NO_ADDR_SPACE (t2)); | |
c22cacf3 | 427 | |
f6294de7 JM |
428 | t1_complete = COMPLETE_TYPE_P (t1); |
429 | t2_complete = COMPLETE_TYPE_P (t2); | |
430 | ||
ca8bdb78 JM |
431 | d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); |
432 | d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); | |
433 | ||
434 | d1_variable = (!d1_zero | |
435 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
436 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
437 | d2_variable = (!d2_zero | |
438 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
439 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
440 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
441 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
ca8bdb78 | 442 | |
400fbf9f | 443 | /* Save space: see if the result is identical to one of the args. */ |
ca8bdb78 JM |
444 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) |
445 | && (d2_variable || d2_zero || !d1_variable)) | |
4b027d16 | 446 | return build_type_attribute_variant (t1, attributes); |
ca8bdb78 JM |
447 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) |
448 | && (d1_variable || d1_zero || !d2_variable)) | |
4b027d16 | 449 | return build_type_attribute_variant (t2, attributes); |
c22cacf3 | 450 | |
de46b2fe AP |
451 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) |
452 | return build_type_attribute_variant (t1, attributes); | |
453 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
454 | return build_type_attribute_variant (t2, attributes); | |
c22cacf3 | 455 | |
46df2823 JM |
456 | /* Merge the element types, and have a size if either arg has |
457 | one. We may have qualifiers on the element types. To set | |
458 | up TYPE_MAIN_VARIANT correctly, we need to form the | |
459 | composite of the unqualified types and add the qualifiers | |
460 | back at the end. */ | |
461 | quals = TYPE_QUALS (strip_array_types (elt)); | |
462 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
463 | t1 = build_array_type (unqual_elt, | |
ca8bdb78 JM |
464 | TYPE_DOMAIN ((TYPE_DOMAIN (t1) |
465 | && (d2_variable | |
466 | || d2_zero | |
467 | || !d1_variable)) | |
468 | ? t1 | |
469 | : t2)); | |
f6294de7 JM |
470 | /* Ensure a composite type involving a zero-length array type |
471 | is a zero-length type not an incomplete type. */ | |
472 | if (d1_zero && d2_zero | |
473 | && (t1_complete || t2_complete) | |
474 | && !COMPLETE_TYPE_P (t1)) | |
475 | { | |
476 | TYPE_SIZE (t1) = bitsize_zero_node; | |
477 | TYPE_SIZE_UNIT (t1) = size_zero_node; | |
478 | } | |
46df2823 | 479 | t1 = c_build_qualified_type (t1, quals); |
de46b2fe | 480 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
481 | } |
482 | ||
fcb99e7b JJ |
483 | case ENUMERAL_TYPE: |
484 | case RECORD_TYPE: | |
485 | case UNION_TYPE: | |
486 | if (attributes != NULL) | |
487 | { | |
488 | /* Try harder not to create a new aggregate type. */ | |
489 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
490 | return t1; | |
491 | if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
492 | return t2; | |
493 | } | |
494 | return build_type_attribute_variant (t1, attributes); | |
495 | ||
400fbf9f JW |
496 | case FUNCTION_TYPE: |
497 | /* Function types: prefer the one that specified arg types. | |
498 | If both do, merge the arg types. Also merge the return types. */ | |
499 | { | |
10bc1b1b | 500 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
400fbf9f JW |
501 | tree p1 = TYPE_ARG_TYPES (t1); |
502 | tree p2 = TYPE_ARG_TYPES (t2); | |
503 | int len; | |
504 | tree newargs, n; | |
505 | int i; | |
506 | ||
507 | /* Save space: see if the result is identical to one of the args. */ | |
3f75a254 | 508 | if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) |
4b027d16 | 509 | return build_type_attribute_variant (t1, attributes); |
3f75a254 | 510 | if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) |
4b027d16 | 511 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
512 | |
513 | /* Simple way if one arg fails to specify argument types. */ | |
514 | if (TYPE_ARG_TYPES (t1) == 0) | |
4b027d16 | 515 | { |
fe7080d2 AP |
516 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); |
517 | t1 = build_type_attribute_variant (t1, attributes); | |
518 | return qualify_type (t1, t2); | |
4b027d16 | 519 | } |
400fbf9f | 520 | if (TYPE_ARG_TYPES (t2) == 0) |
4b027d16 RK |
521 | { |
522 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
fe7080d2 AP |
523 | t1 = build_type_attribute_variant (t1, attributes); |
524 | return qualify_type (t1, t2); | |
4b027d16 | 525 | } |
400fbf9f JW |
526 | |
527 | /* If both args specify argument types, we must merge the two | |
528 | lists, argument by argument. */ | |
2f4e8f2b | 529 | |
400fbf9f JW |
530 | len = list_length (p1); |
531 | newargs = 0; | |
532 | ||
533 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 534 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
535 | |
536 | n = newargs; | |
537 | ||
538 | for (; p1; | |
539 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
540 | { | |
541 | /* A null type means arg type is not specified. | |
542 | Take whatever the other function type has. */ | |
543 | if (TREE_VALUE (p1) == 0) | |
544 | { | |
545 | TREE_VALUE (n) = TREE_VALUE (p2); | |
546 | goto parm_done; | |
547 | } | |
548 | if (TREE_VALUE (p2) == 0) | |
549 | { | |
550 | TREE_VALUE (n) = TREE_VALUE (p1); | |
551 | goto parm_done; | |
552 | } | |
2f6e4e97 | 553 | |
400fbf9f JW |
554 | /* Given wait (union {union wait *u; int *i} *) |
555 | and wait (union wait *), | |
556 | prefer union wait * as type of parm. */ | |
557 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
558 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
559 | { | |
560 | tree memb; | |
58cb41e6 JJ |
561 | tree mv2 = TREE_VALUE (p2); |
562 | if (mv2 && mv2 != error_mark_node | |
563 | && TREE_CODE (mv2) != ARRAY_TYPE) | |
564 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
400fbf9f | 565 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); |
910ad8de | 566 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
567 | { |
568 | tree mv3 = TREE_TYPE (memb); | |
569 | if (mv3 && mv3 != error_mark_node | |
570 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
571 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
572 | if (comptypes (mv3, mv2)) | |
573 | { | |
574 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
575 | TREE_VALUE (p2)); | |
c1771a20 | 576 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 577 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
578 | goto parm_done; |
579 | } | |
580 | } | |
400fbf9f JW |
581 | } |
582 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
583 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
584 | { | |
585 | tree memb; | |
58cb41e6 JJ |
586 | tree mv1 = TREE_VALUE (p1); |
587 | if (mv1 && mv1 != error_mark_node | |
588 | && TREE_CODE (mv1) != ARRAY_TYPE) | |
589 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
400fbf9f | 590 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); |
910ad8de | 591 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
592 | { |
593 | tree mv3 = TREE_TYPE (memb); | |
594 | if (mv3 && mv3 != error_mark_node | |
595 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
596 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
597 | if (comptypes (mv3, mv1)) | |
598 | { | |
599 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
600 | TREE_VALUE (p1)); | |
c1771a20 | 601 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 602 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
603 | goto parm_done; |
604 | } | |
605 | } | |
400fbf9f | 606 | } |
10bc1b1b | 607 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); |
400fbf9f JW |
608 | parm_done: ; |
609 | } | |
610 | ||
4b027d16 | 611 | t1 = build_function_type (valtype, newargs); |
fe7080d2 | 612 | t1 = qualify_type (t1, t2); |
0f41302f | 613 | /* ... falls through ... */ |
400fbf9f JW |
614 | } |
615 | ||
616 | default: | |
4b027d16 | 617 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
618 | } |
619 | ||
620 | } | |
10bc1b1b JM |
621 | |
622 | /* Return the type of a conditional expression between pointers to | |
623 | possibly differently qualified versions of compatible types. | |
624 | ||
625 | We assume that comp_target_types has already been done and returned | |
626 | nonzero; if that isn't so, this may crash. */ | |
627 | ||
628 | static tree | |
629 | common_pointer_type (tree t1, tree t2) | |
630 | { | |
631 | tree attributes; | |
46df2823 JM |
632 | tree pointed_to_1, mv1; |
633 | tree pointed_to_2, mv2; | |
10bc1b1b | 634 | tree target; |
eb1387a0 | 635 | unsigned target_quals; |
36c5e70a BE |
636 | addr_space_t as1, as2, as_common; |
637 | int quals1, quals2; | |
10bc1b1b JM |
638 | |
639 | /* Save time if the two types are the same. */ | |
640 | ||
641 | if (t1 == t2) return t1; | |
642 | ||
643 | /* If one type is nonsense, use the other. */ | |
644 | if (t1 == error_mark_node) | |
645 | return t2; | |
646 | if (t2 == error_mark_node) | |
647 | return t1; | |
648 | ||
366de0ce | 649 | gcc_assert (TREE_CODE (t1) == POINTER_TYPE |
c22cacf3 | 650 | && TREE_CODE (t2) == POINTER_TYPE); |
10bc1b1b JM |
651 | |
652 | /* Merge the attributes. */ | |
653 | attributes = targetm.merge_type_attributes (t1, t2); | |
654 | ||
655 | /* Find the composite type of the target types, and combine the | |
46df2823 JM |
656 | qualifiers of the two types' targets. Do not lose qualifiers on |
657 | array element types by taking the TYPE_MAIN_VARIANT. */ | |
658 | mv1 = pointed_to_1 = TREE_TYPE (t1); | |
659 | mv2 = pointed_to_2 = TREE_TYPE (t2); | |
660 | if (TREE_CODE (mv1) != ARRAY_TYPE) | |
661 | mv1 = TYPE_MAIN_VARIANT (pointed_to_1); | |
662 | if (TREE_CODE (mv2) != ARRAY_TYPE) | |
663 | mv2 = TYPE_MAIN_VARIANT (pointed_to_2); | |
664 | target = composite_type (mv1, mv2); | |
eb1387a0 RG |
665 | |
666 | /* For function types do not merge const qualifiers, but drop them | |
667 | if used inconsistently. The middle-end uses these to mark const | |
668 | and noreturn functions. */ | |
36c5e70a BE |
669 | quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1); |
670 | quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2); | |
671 | ||
eb1387a0 | 672 | if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) |
36c5e70a | 673 | target_quals = (quals1 & quals2); |
eb1387a0 | 674 | else |
36c5e70a BE |
675 | target_quals = (quals1 | quals2); |
676 | ||
677 | /* If the two named address spaces are different, determine the common | |
678 | superset address space. This is guaranteed to exist due to the | |
679 | assumption that comp_target_type returned non-zero. */ | |
680 | as1 = TYPE_ADDR_SPACE (pointed_to_1); | |
681 | as2 = TYPE_ADDR_SPACE (pointed_to_2); | |
682 | if (!addr_space_superset (as1, as2, &as_common)) | |
683 | gcc_unreachable (); | |
684 | ||
685 | target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); | |
686 | ||
eb1387a0 | 687 | t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); |
10bc1b1b JM |
688 | return build_type_attribute_variant (t1, attributes); |
689 | } | |
690 | ||
691 | /* Return the common type for two arithmetic types under the usual | |
692 | arithmetic conversions. The default conversions have already been | |
693 | applied, and enumerated types converted to their compatible integer | |
694 | types. The resulting type is unqualified and has no attributes. | |
695 | ||
696 | This is the type for the result of most arithmetic operations | |
697 | if the operands have the given two types. */ | |
698 | ||
ccf7f880 JJ |
699 | static tree |
700 | c_common_type (tree t1, tree t2) | |
10bc1b1b JM |
701 | { |
702 | enum tree_code code1; | |
703 | enum tree_code code2; | |
704 | ||
705 | /* If one type is nonsense, use the other. */ | |
706 | if (t1 == error_mark_node) | |
707 | return t2; | |
708 | if (t2 == error_mark_node) | |
709 | return t1; | |
710 | ||
711 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
712 | t1 = TYPE_MAIN_VARIANT (t1); | |
713 | ||
714 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
715 | t2 = TYPE_MAIN_VARIANT (t2); | |
716 | ||
717 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
718 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
719 | ||
720 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
721 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
722 | ||
723 | /* Save time if the two types are the same. */ | |
724 | ||
725 | if (t1 == t2) return t1; | |
726 | ||
727 | code1 = TREE_CODE (t1); | |
728 | code2 = TREE_CODE (t2); | |
729 | ||
366de0ce | 730 | gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE |
ab22c1fa CF |
731 | || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE |
732 | || code1 == INTEGER_TYPE); | |
366de0ce | 733 | gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE |
ab22c1fa CF |
734 | || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE |
735 | || code2 == INTEGER_TYPE); | |
10bc1b1b | 736 | |
5fc89bfd JJ |
737 | /* When one operand is a decimal float type, the other operand cannot be |
738 | a generic float type or a complex type. We also disallow vector types | |
739 | here. */ | |
740 | if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) | |
741 | && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) | |
742 | { | |
743 | if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) | |
744 | { | |
745 | error ("can%'t mix operands of decimal float and vector types"); | |
746 | return error_mark_node; | |
747 | } | |
748 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
749 | { | |
750 | error ("can%'t mix operands of decimal float and complex types"); | |
751 | return error_mark_node; | |
752 | } | |
753 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
754 | { | |
755 | error ("can%'t mix operands of decimal float and other float types"); | |
756 | return error_mark_node; | |
757 | } | |
758 | } | |
759 | ||
10bc1b1b JM |
760 | /* If one type is a vector type, return that type. (How the usual |
761 | arithmetic conversions apply to the vector types extension is not | |
762 | precisely specified.) */ | |
763 | if (code1 == VECTOR_TYPE) | |
764 | return t1; | |
765 | ||
766 | if (code2 == VECTOR_TYPE) | |
767 | return t2; | |
768 | ||
769 | /* If one type is complex, form the common type of the non-complex | |
770 | components, then make that complex. Use T1 or T2 if it is the | |
771 | required type. */ | |
772 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
773 | { | |
774 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
775 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
ccf7f880 | 776 | tree subtype = c_common_type (subtype1, subtype2); |
10bc1b1b JM |
777 | |
778 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
779 | return t1; | |
780 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
781 | return t2; | |
782 | else | |
783 | return build_complex_type (subtype); | |
784 | } | |
785 | ||
786 | /* If only one is real, use it as the result. */ | |
787 | ||
788 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
789 | return t1; | |
790 | ||
791 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
792 | return t2; | |
793 | ||
9a8ce21f JG |
794 | /* If both are real and either are decimal floating point types, use |
795 | the decimal floating point type with the greater precision. */ | |
796 | ||
797 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
798 | { | |
799 | if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node | |
800 | || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) | |
801 | return dfloat128_type_node; | |
802 | else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node | |
803 | || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) | |
804 | return dfloat64_type_node; | |
805 | else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node | |
806 | || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) | |
807 | return dfloat32_type_node; | |
808 | } | |
809 | ||
ab22c1fa CF |
810 | /* Deal with fixed-point types. */ |
811 | if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) | |
812 | { | |
813 | unsigned int unsignedp = 0, satp = 0; | |
814 | enum machine_mode m1, m2; | |
815 | unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; | |
816 | ||
817 | m1 = TYPE_MODE (t1); | |
818 | m2 = TYPE_MODE (t2); | |
819 | ||
820 | /* If one input type is saturating, the result type is saturating. */ | |
821 | if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) | |
822 | satp = 1; | |
823 | ||
824 | /* If both fixed-point types are unsigned, the result type is unsigned. | |
825 | When mixing fixed-point and integer types, follow the sign of the | |
826 | fixed-point type. | |
827 | Otherwise, the result type is signed. */ | |
828 | if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) | |
829 | && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) | |
830 | || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE | |
831 | && TYPE_UNSIGNED (t1)) | |
832 | || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE | |
833 | && TYPE_UNSIGNED (t2))) | |
834 | unsignedp = 1; | |
835 | ||
836 | /* The result type is signed. */ | |
837 | if (unsignedp == 0) | |
838 | { | |
839 | /* If the input type is unsigned, we need to convert to the | |
840 | signed type. */ | |
841 | if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) | |
842 | { | |
d75d71e0 | 843 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
844 | if (GET_MODE_CLASS (m1) == MODE_UFRACT) |
845 | mclass = MODE_FRACT; | |
846 | else if (GET_MODE_CLASS (m1) == MODE_UACCUM) | |
847 | mclass = MODE_ACCUM; | |
848 | else | |
849 | gcc_unreachable (); | |
850 | m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); | |
851 | } | |
852 | if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) | |
853 | { | |
d75d71e0 | 854 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
855 | if (GET_MODE_CLASS (m2) == MODE_UFRACT) |
856 | mclass = MODE_FRACT; | |
857 | else if (GET_MODE_CLASS (m2) == MODE_UACCUM) | |
858 | mclass = MODE_ACCUM; | |
859 | else | |
860 | gcc_unreachable (); | |
861 | m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); | |
862 | } | |
863 | } | |
864 | ||
865 | if (code1 == FIXED_POINT_TYPE) | |
866 | { | |
867 | fbit1 = GET_MODE_FBIT (m1); | |
868 | ibit1 = GET_MODE_IBIT (m1); | |
869 | } | |
870 | else | |
871 | { | |
872 | fbit1 = 0; | |
873 | /* Signed integers need to subtract one sign bit. */ | |
874 | ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); | |
875 | } | |
876 | ||
877 | if (code2 == FIXED_POINT_TYPE) | |
878 | { | |
879 | fbit2 = GET_MODE_FBIT (m2); | |
880 | ibit2 = GET_MODE_IBIT (m2); | |
881 | } | |
882 | else | |
883 | { | |
884 | fbit2 = 0; | |
885 | /* Signed integers need to subtract one sign bit. */ | |
886 | ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); | |
887 | } | |
888 | ||
889 | max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; | |
890 | max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; | |
891 | return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, | |
892 | satp); | |
893 | } | |
894 | ||
10bc1b1b JM |
895 | /* Both real or both integers; use the one with greater precision. */ |
896 | ||
897 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
898 | return t1; | |
899 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
900 | return t2; | |
901 | ||
902 | /* Same precision. Prefer long longs to longs to ints when the | |
903 | same precision, following the C99 rules on integer type rank | |
904 | (which are equivalent to the C90 rules for C90 types). */ | |
905 | ||
906 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
907 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
908 | return long_long_unsigned_type_node; | |
909 | ||
910 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
911 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
912 | { | |
913 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
914 | return long_long_unsigned_type_node; | |
915 | else | |
c22cacf3 | 916 | return long_long_integer_type_node; |
10bc1b1b JM |
917 | } |
918 | ||
919 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
920 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
921 | return long_unsigned_type_node; | |
922 | ||
923 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
924 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
925 | { | |
926 | /* But preserve unsignedness from the other type, | |
927 | since long cannot hold all the values of an unsigned int. */ | |
928 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
929 | return long_unsigned_type_node; | |
930 | else | |
931 | return long_integer_type_node; | |
932 | } | |
933 | ||
934 | /* Likewise, prefer long double to double even if same size. */ | |
935 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
936 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
937 | return long_double_type_node; | |
938 | ||
2531a1d9 JR |
939 | /* Likewise, prefer double to float even if same size. |
940 | We got a couple of embedded targets with 32 bit doubles, and the | |
941 | pdp11 might have 64 bit floats. */ | |
942 | if (TYPE_MAIN_VARIANT (t1) == double_type_node | |
943 | || TYPE_MAIN_VARIANT (t2) == double_type_node) | |
944 | return double_type_node; | |
945 | ||
10bc1b1b JM |
946 | /* Otherwise prefer the unsigned one. */ |
947 | ||
948 | if (TYPE_UNSIGNED (t1)) | |
949 | return t1; | |
950 | else | |
951 | return t2; | |
952 | } | |
400fbf9f | 953 | \f |
5922c215 JM |
954 | /* Wrapper around c_common_type that is used by c-common.c and other |
955 | front end optimizations that remove promotions. ENUMERAL_TYPEs | |
b2232745 RS |
956 | are allowed here and are converted to their compatible integer types. |
957 | BOOLEAN_TYPEs are allowed here and return either boolean_type_node or | |
958 | preferably a non-Boolean type as the common type. */ | |
ccf7f880 JJ |
959 | tree |
960 | common_type (tree t1, tree t2) | |
961 | { | |
962 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
963 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); | |
964 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
965 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); | |
b2232745 RS |
966 | |
967 | /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ | |
968 | if (TREE_CODE (t1) == BOOLEAN_TYPE | |
969 | && TREE_CODE (t2) == BOOLEAN_TYPE) | |
970 | return boolean_type_node; | |
971 | ||
972 | /* If either type is BOOLEAN_TYPE, then return the other. */ | |
973 | if (TREE_CODE (t1) == BOOLEAN_TYPE) | |
974 | return t2; | |
975 | if (TREE_CODE (t2) == BOOLEAN_TYPE) | |
976 | return t1; | |
977 | ||
ccf7f880 JJ |
978 | return c_common_type (t1, t2); |
979 | } | |
f13c9b2c | 980 | |
400fbf9f JW |
981 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
982 | or various other operations. Return 2 if they are compatible | |
983 | but a warning may be needed if you use them together. */ | |
984 | ||
985 | int | |
132da1a5 | 986 | comptypes (tree type1, tree type2) |
f13c9b2c AP |
987 | { |
988 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
989 | int val; | |
990 | ||
dc5027f4 | 991 | val = comptypes_internal (type1, type2, NULL, NULL); |
744aa42f ILT |
992 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
993 | ||
994 | return val; | |
995 | } | |
996 | ||
997 | /* Like comptypes, but if it returns non-zero because enum and int are | |
998 | compatible, it sets *ENUM_AND_INT_P to true. */ | |
999 | ||
1000 | static int | |
1001 | comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) | |
1002 | { | |
1003 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1004 | int val; | |
1005 | ||
dc5027f4 JM |
1006 | val = comptypes_internal (type1, type2, enum_and_int_p, NULL); |
1007 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
1008 | ||
1009 | return val; | |
1010 | } | |
1011 | ||
1012 | /* Like comptypes, but if it returns nonzero for different types, it | |
1013 | sets *DIFFERENT_TYPES_P to true. */ | |
1014 | ||
1015 | int | |
1016 | comptypes_check_different_types (tree type1, tree type2, | |
1017 | bool *different_types_p) | |
1018 | { | |
1019 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1020 | int val; | |
1021 | ||
1022 | val = comptypes_internal (type1, type2, NULL, different_types_p); | |
f13c9b2c | 1023 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
c22cacf3 | 1024 | |
f13c9b2c | 1025 | return val; |
c22cacf3 MS |
1026 | } |
1027 | \f | |
f13c9b2c AP |
1028 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
1029 | or various other operations. Return 2 if they are compatible | |
744aa42f ILT |
1030 | but a warning may be needed if you use them together. If |
1031 | ENUM_AND_INT_P is not NULL, and one type is an enum and the other a | |
1032 | compatible integer type, then this sets *ENUM_AND_INT_P to true; | |
dc5027f4 JM |
1033 | *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not |
1034 | NULL, and the types are compatible but different enough not to be | |
48b0b196 | 1035 | permitted in C11 typedef redeclarations, then this sets |
dc5027f4 JM |
1036 | *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to |
1037 | false, but may or may not be set if the types are incompatible. | |
1038 | This differs from comptypes, in that we don't free the seen | |
1039 | types. */ | |
f13c9b2c AP |
1040 | |
1041 | static int | |
dc5027f4 JM |
1042 | comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, |
1043 | bool *different_types_p) | |
400fbf9f | 1044 | { |
58f9752a KG |
1045 | const_tree t1 = type1; |
1046 | const_tree t2 = type2; | |
4b027d16 | 1047 | int attrval, val; |
400fbf9f JW |
1048 | |
1049 | /* Suppress errors caused by previously reported errors. */ | |
1050 | ||
8d47dfc5 RH |
1051 | if (t1 == t2 || !t1 || !t2 |
1052 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
400fbf9f JW |
1053 | return 1; |
1054 | ||
bca63328 JM |
1055 | /* Enumerated types are compatible with integer types, but this is |
1056 | not transitive: two enumerated types in the same translation unit | |
1057 | are compatible with each other only if they are the same type. */ | |
400fbf9f | 1058 | |
bca63328 | 1059 | if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) |
744aa42f ILT |
1060 | { |
1061 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); | |
dc5027f4 JM |
1062 | if (TREE_CODE (t2) != VOID_TYPE) |
1063 | { | |
1064 | if (enum_and_int_p != NULL) | |
1065 | *enum_and_int_p = true; | |
1066 | if (different_types_p != NULL) | |
1067 | *different_types_p = true; | |
1068 | } | |
744aa42f | 1069 | } |
bca63328 | 1070 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) |
744aa42f ILT |
1071 | { |
1072 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); | |
dc5027f4 JM |
1073 | if (TREE_CODE (t1) != VOID_TYPE) |
1074 | { | |
1075 | if (enum_and_int_p != NULL) | |
1076 | *enum_and_int_p = true; | |
1077 | if (different_types_p != NULL) | |
1078 | *different_types_p = true; | |
1079 | } | |
744aa42f | 1080 | } |
400fbf9f JW |
1081 | |
1082 | if (t1 == t2) | |
1083 | return 1; | |
1084 | ||
1085 | /* Different classes of types can't be compatible. */ | |
1086 | ||
3aeb3655 EC |
1087 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
1088 | return 0; | |
400fbf9f | 1089 | |
118a3a8b | 1090 | /* Qualifiers must match. C99 6.7.3p9 */ |
400fbf9f | 1091 | |
3932261a | 1092 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
400fbf9f JW |
1093 | return 0; |
1094 | ||
08632da2 RS |
1095 | /* Allow for two different type nodes which have essentially the same |
1096 | definition. Note that we already checked for equality of the type | |
38e01259 | 1097 | qualifiers (just above). */ |
400fbf9f | 1098 | |
46df2823 JM |
1099 | if (TREE_CODE (t1) != ARRAY_TYPE |
1100 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
400fbf9f JW |
1101 | return 1; |
1102 | ||
4b027d16 | 1103 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ |
ac9a30ae | 1104 | if (!(attrval = comp_type_attributes (t1, t2))) |
4b027d16 RK |
1105 | return 0; |
1106 | ||
1107 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1108 | val = 0; | |
1109 | ||
400fbf9f JW |
1110 | switch (TREE_CODE (t1)) |
1111 | { | |
1112 | case POINTER_TYPE: | |
106f5de5 UW |
1113 | /* Do not remove mode or aliasing information. */ |
1114 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1115 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) | |
1116 | break; | |
4b027d16 | 1117 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
744aa42f | 1118 | ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 | 1119 | enum_and_int_p, different_types_p)); |
4b027d16 | 1120 | break; |
400fbf9f JW |
1121 | |
1122 | case FUNCTION_TYPE: | |
dc5027f4 JM |
1123 | val = function_types_compatible_p (t1, t2, enum_and_int_p, |
1124 | different_types_p); | |
4b027d16 | 1125 | break; |
400fbf9f JW |
1126 | |
1127 | case ARRAY_TYPE: | |
1128 | { | |
400fbf9f JW |
1129 | tree d1 = TYPE_DOMAIN (t1); |
1130 | tree d2 = TYPE_DOMAIN (t2); | |
3f85558f RH |
1131 | bool d1_variable, d2_variable; |
1132 | bool d1_zero, d2_zero; | |
4b027d16 | 1133 | val = 1; |
400fbf9f JW |
1134 | |
1135 | /* Target types must match incl. qualifiers. */ | |
1136 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
744aa42f | 1137 | && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 JM |
1138 | enum_and_int_p, |
1139 | different_types_p))) | |
400fbf9f JW |
1140 | return 0; |
1141 | ||
dc5027f4 JM |
1142 | if (different_types_p != NULL |
1143 | && (d1 == 0) != (d2 == 0)) | |
1144 | *different_types_p = true; | |
400fbf9f | 1145 | /* Sizes must match unless one is missing or variable. */ |
3f85558f | 1146 | if (d1 == 0 || d2 == 0 || d1 == d2) |
4b027d16 | 1147 | break; |
400fbf9f | 1148 | |
3f75a254 JM |
1149 | d1_zero = !TYPE_MAX_VALUE (d1); |
1150 | d2_zero = !TYPE_MAX_VALUE (d2); | |
3f85558f | 1151 | |
3f75a254 | 1152 | d1_variable = (!d1_zero |
3f85558f RH |
1153 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST |
1154 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
3f75a254 | 1155 | d2_variable = (!d2_zero |
3f85558f RH |
1156 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST |
1157 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
1158 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
1159 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
3f85558f | 1160 | |
dc5027f4 JM |
1161 | if (different_types_p != NULL |
1162 | && d1_variable != d2_variable) | |
1163 | *different_types_p = true; | |
3f85558f RH |
1164 | if (d1_variable || d2_variable) |
1165 | break; | |
1166 | if (d1_zero && d2_zero) | |
1167 | break; | |
1168 | if (d1_zero || d2_zero | |
3f75a254 JM |
1169 | || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) |
1170 | || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) | |
05bccae2 RK |
1171 | val = 0; |
1172 | ||
c22cacf3 | 1173 | break; |
400fbf9f JW |
1174 | } |
1175 | ||
d1bd0ded | 1176 | case ENUMERAL_TYPE: |
58393038 | 1177 | case RECORD_TYPE: |
d1bd0ded | 1178 | case UNION_TYPE: |
766beae1 | 1179 | if (val != 1 && !same_translation_unit_p (t1, t2)) |
c22cacf3 | 1180 | { |
fcb99e7b JJ |
1181 | tree a1 = TYPE_ATTRIBUTES (t1); |
1182 | tree a2 = TYPE_ATTRIBUTES (t2); | |
1183 | ||
1184 | if (! attribute_list_contained (a1, a2) | |
1185 | && ! attribute_list_contained (a2, a1)) | |
1186 | break; | |
1187 | ||
f13c9b2c | 1188 | if (attrval != 2) |
dc5027f4 JM |
1189 | return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, |
1190 | different_types_p); | |
1191 | val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, | |
1192 | different_types_p); | |
f13c9b2c | 1193 | } |
4b027d16 | 1194 | break; |
e9a25f70 | 1195 | |
62e1dfcf | 1196 | case VECTOR_TYPE: |
744aa42f ILT |
1197 | val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) |
1198 | && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
dc5027f4 | 1199 | enum_and_int_p, different_types_p)); |
62e1dfcf NC |
1200 | break; |
1201 | ||
e9a25f70 JL |
1202 | default: |
1203 | break; | |
400fbf9f | 1204 | } |
4b027d16 | 1205 | return attrval == 2 && val == 1 ? 2 : val; |
400fbf9f JW |
1206 | } |
1207 | ||
36c5e70a BE |
1208 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring |
1209 | their qualifiers, except for named address spaces. If the pointers point to | |
1210 | different named addresses, then we must determine if one address space is a | |
1211 | subset of the other. */ | |
400fbf9f JW |
1212 | |
1213 | static int | |
744aa42f | 1214 | comp_target_types (location_t location, tree ttl, tree ttr) |
400fbf9f | 1215 | { |
392202b0 | 1216 | int val; |
36c5e70a BE |
1217 | tree mvl = TREE_TYPE (ttl); |
1218 | tree mvr = TREE_TYPE (ttr); | |
1219 | addr_space_t asl = TYPE_ADDR_SPACE (mvl); | |
1220 | addr_space_t asr = TYPE_ADDR_SPACE (mvr); | |
1221 | addr_space_t as_common; | |
744aa42f | 1222 | bool enum_and_int_p; |
8b40563c | 1223 | |
36c5e70a BE |
1224 | /* Fail if pointers point to incompatible address spaces. */ |
1225 | if (!addr_space_superset (asl, asr, &as_common)) | |
1226 | return 0; | |
1227 | ||
46df2823 JM |
1228 | /* Do not lose qualifiers on element types of array types that are |
1229 | pointer targets by taking their TYPE_MAIN_VARIANT. */ | |
46df2823 | 1230 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
267bac10 JM |
1231 | mvl = (TYPE_ATOMIC (mvl) |
1232 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC) | |
1233 | : TYPE_MAIN_VARIANT (mvl)); | |
46df2823 | 1234 | if (TREE_CODE (mvr) != ARRAY_TYPE) |
267bac10 JM |
1235 | mvr = (TYPE_ATOMIC (mvr) |
1236 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC) | |
1237 | : TYPE_MAIN_VARIANT (mvr)); | |
744aa42f ILT |
1238 | enum_and_int_p = false; |
1239 | val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); | |
8b40563c | 1240 | |
fcf73884 | 1241 | if (val == 2) |
c1771a20 | 1242 | pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); |
744aa42f ILT |
1243 | |
1244 | if (val == 1 && enum_and_int_p && warn_cxx_compat) | |
1245 | warning_at (location, OPT_Wc___compat, | |
1246 | "pointer target types incompatible in C++"); | |
1247 | ||
400fbf9f JW |
1248 | return val; |
1249 | } | |
1250 | \f | |
1251 | /* Subroutines of `comptypes'. */ | |
1252 | ||
f75fbaf7 ZW |
1253 | /* Determine whether two trees derive from the same translation unit. |
1254 | If the CONTEXT chain ends in a null, that tree's context is still | |
1255 | being parsed, so if two trees have context chains ending in null, | |
766beae1 | 1256 | they're in the same translation unit. */ |
f75fbaf7 | 1257 | int |
58f9752a | 1258 | same_translation_unit_p (const_tree t1, const_tree t2) |
766beae1 ZW |
1259 | { |
1260 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
1261 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
1262 | { | |
6615c446 JO |
1263 | case tcc_declaration: |
1264 | t1 = DECL_CONTEXT (t1); break; | |
1265 | case tcc_type: | |
1266 | t1 = TYPE_CONTEXT (t1); break; | |
1267 | case tcc_exceptional: | |
1268 | t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ | |
366de0ce | 1269 | default: gcc_unreachable (); |
766beae1 ZW |
1270 | } |
1271 | ||
1272 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
1273 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
1274 | { | |
6615c446 JO |
1275 | case tcc_declaration: |
1276 | t2 = DECL_CONTEXT (t2); break; | |
1277 | case tcc_type: | |
1278 | t2 = TYPE_CONTEXT (t2); break; | |
1279 | case tcc_exceptional: | |
1280 | t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ | |
366de0ce | 1281 | default: gcc_unreachable (); |
766beae1 ZW |
1282 | } |
1283 | ||
1284 | return t1 == t2; | |
1285 | } | |
1286 | ||
f13c9b2c | 1287 | /* Allocate the seen two types, assuming that they are compatible. */ |
d1bd0ded | 1288 | |
f13c9b2c | 1289 | static struct tagged_tu_seen_cache * |
58f9752a | 1290 | alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) |
f13c9b2c | 1291 | { |
cceb1885 | 1292 | struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); |
f13c9b2c AP |
1293 | tu->next = tagged_tu_seen_base; |
1294 | tu->t1 = t1; | |
1295 | tu->t2 = t2; | |
c22cacf3 | 1296 | |
f13c9b2c | 1297 | tagged_tu_seen_base = tu; |
c22cacf3 | 1298 | |
f13c9b2c AP |
1299 | /* The C standard says that two structures in different translation |
1300 | units are compatible with each other only if the types of their | |
1301 | fields are compatible (among other things). We assume that they | |
1302 | are compatible until proven otherwise when building the cache. | |
1303 | An example where this can occur is: | |
1304 | struct a | |
1305 | { | |
1306 | struct a *next; | |
1307 | }; | |
1308 | If we are comparing this against a similar struct in another TU, | |
c83eecad | 1309 | and did not assume they were compatible, we end up with an infinite |
f13c9b2c AP |
1310 | loop. */ |
1311 | tu->val = 1; | |
1312 | return tu; | |
1313 | } | |
d1bd0ded | 1314 | |
f13c9b2c | 1315 | /* Free the seen types until we get to TU_TIL. */ |
d1bd0ded | 1316 | |
f13c9b2c AP |
1317 | static void |
1318 | free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) | |
1319 | { | |
1320 | const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; | |
1321 | while (tu != tu_til) | |
1322 | { | |
741ac903 KG |
1323 | const struct tagged_tu_seen_cache *const tu1 |
1324 | = (const struct tagged_tu_seen_cache *) tu; | |
f13c9b2c | 1325 | tu = tu1->next; |
b1d5455a | 1326 | free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); |
f13c9b2c AP |
1327 | } |
1328 | tagged_tu_seen_base = tu_til; | |
1329 | } | |
d1bd0ded GK |
1330 | |
1331 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
1332 | compatible. If the two types are not the same (which has been | |
1333 | checked earlier), this can only happen when multiple translation | |
1334 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
dc5027f4 JM |
1335 | rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1336 | comptypes_internal. */ | |
d1bd0ded GK |
1337 | |
1338 | static int | |
744aa42f | 1339 | tagged_types_tu_compatible_p (const_tree t1, const_tree t2, |
dc5027f4 | 1340 | bool *enum_and_int_p, bool *different_types_p) |
d1bd0ded GK |
1341 | { |
1342 | tree s1, s2; | |
1343 | bool needs_warning = false; | |
3aeb3655 | 1344 | |
d1bd0ded GK |
1345 | /* We have to verify that the tags of the types are the same. This |
1346 | is harder than it looks because this may be a typedef, so we have | |
1347 | to go look at the original type. It may even be a typedef of a | |
6de9cd9a DN |
1348 | typedef... |
1349 | In the case of compiler-created builtin structs the TYPE_DECL | |
1350 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
dea984dc ILT |
1351 | while (TYPE_NAME (t1) |
1352 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
1353 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
d1bd0ded GK |
1354 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); |
1355 | ||
dea984dc ILT |
1356 | while (TYPE_NAME (t2) |
1357 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
1358 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
d1bd0ded GK |
1359 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); |
1360 | ||
1361 | /* C90 didn't have the requirement that the two tags be the same. */ | |
1362 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
1363 | return 0; | |
3aeb3655 | 1364 | |
d1bd0ded GK |
1365 | /* C90 didn't say what happened if one or both of the types were |
1366 | incomplete; we choose to follow C99 rules here, which is that they | |
1367 | are compatible. */ | |
1368 | if (TYPE_SIZE (t1) == NULL | |
1369 | || TYPE_SIZE (t2) == NULL) | |
1370 | return 1; | |
3aeb3655 | 1371 | |
d1bd0ded | 1372 | { |
f13c9b2c | 1373 | const struct tagged_tu_seen_cache * tts_i; |
d1bd0ded GK |
1374 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) |
1375 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
f13c9b2c | 1376 | return tts_i->val; |
d1bd0ded | 1377 | } |
3aeb3655 | 1378 | |
d1bd0ded GK |
1379 | switch (TREE_CODE (t1)) |
1380 | { | |
1381 | case ENUMERAL_TYPE: | |
1382 | { | |
f13c9b2c | 1383 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
c22cacf3 MS |
1384 | /* Speed up the case where the type values are in the same order. */ |
1385 | tree tv1 = TYPE_VALUES (t1); | |
1386 | tree tv2 = TYPE_VALUES (t2); | |
3aeb3655 | 1387 | |
c22cacf3 | 1388 | if (tv1 == tv2) |
f13c9b2c AP |
1389 | { |
1390 | return 1; | |
1391 | } | |
3aeb3655 | 1392 | |
c22cacf3 MS |
1393 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) |
1394 | { | |
1395 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) | |
1396 | break; | |
1397 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
f13c9b2c | 1398 | { |
c22cacf3 | 1399 | tu->val = 0; |
f13c9b2c AP |
1400 | return 0; |
1401 | } | |
c22cacf3 | 1402 | } |
3aeb3655 | 1403 | |
c22cacf3 | 1404 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) |
f13c9b2c AP |
1405 | { |
1406 | return 1; | |
1407 | } | |
c22cacf3 | 1408 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) |
f13c9b2c AP |
1409 | { |
1410 | tu->val = 0; | |
1411 | return 0; | |
1412 | } | |
3aeb3655 | 1413 | |
d1bd0ded | 1414 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) |
f13c9b2c AP |
1415 | { |
1416 | tu->val = 0; | |
1417 | return 0; | |
1418 | } | |
3aeb3655 | 1419 | |
d1bd0ded GK |
1420 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) |
1421 | { | |
1422 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
1423 | if (s2 == NULL | |
1424 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
f13c9b2c AP |
1425 | { |
1426 | tu->val = 0; | |
1427 | return 0; | |
1428 | } | |
d1bd0ded GK |
1429 | } |
1430 | return 1; | |
1431 | } | |
1432 | ||
1433 | case UNION_TYPE: | |
1434 | { | |
f13c9b2c | 1435 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
d1bd0ded | 1436 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) |
f13c9b2c AP |
1437 | { |
1438 | tu->val = 0; | |
1439 | return 0; | |
1440 | } | |
c22cacf3 | 1441 | |
f13c9b2c AP |
1442 | /* Speed up the common case where the fields are in the same order. */ |
1443 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; | |
910ad8de | 1444 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
f13c9b2c AP |
1445 | { |
1446 | int result; | |
c22cacf3 | 1447 | |
3ae4d3cc | 1448 | if (DECL_NAME (s1) != DECL_NAME (s2)) |
f13c9b2c | 1449 | break; |
744aa42f | 1450 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1451 | enum_and_int_p, different_types_p); |
3ae4d3cc AO |
1452 | |
1453 | if (result != 1 && !DECL_NAME (s1)) | |
1454 | break; | |
f13c9b2c AP |
1455 | if (result == 0) |
1456 | { | |
1457 | tu->val = 0; | |
1458 | return 0; | |
1459 | } | |
1460 | if (result == 2) | |
1461 | needs_warning = true; | |
1462 | ||
1463 | if (TREE_CODE (s1) == FIELD_DECL | |
1464 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1465 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1466 | { | |
1467 | tu->val = 0; | |
1468 | return 0; | |
1469 | } | |
1470 | } | |
1471 | if (!s1 && !s2) | |
1472 | { | |
1473 | tu->val = needs_warning ? 2 : 1; | |
1474 | return tu->val; | |
1475 | } | |
d1bd0ded | 1476 | |
910ad8de | 1477 | for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1)) |
d1bd0ded GK |
1478 | { |
1479 | bool ok = false; | |
3aeb3655 | 1480 | |
910ad8de | 1481 | for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2)) |
3ae4d3cc AO |
1482 | if (DECL_NAME (s1) == DECL_NAME (s2)) |
1483 | { | |
1484 | int result; | |
1485 | ||
744aa42f | 1486 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 JM |
1487 | enum_and_int_p, |
1488 | different_types_p); | |
3ae4d3cc AO |
1489 | |
1490 | if (result != 1 && !DECL_NAME (s1)) | |
1491 | continue; | |
1492 | if (result == 0) | |
1493 | { | |
1494 | tu->val = 0; | |
1495 | return 0; | |
1496 | } | |
1497 | if (result == 2) | |
1498 | needs_warning = true; | |
1499 | ||
1500 | if (TREE_CODE (s1) == FIELD_DECL | |
1501 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1502 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
d1bd0ded | 1503 | break; |
3ae4d3cc AO |
1504 | |
1505 | ok = true; | |
1506 | break; | |
1507 | } | |
3f75a254 | 1508 | if (!ok) |
f13c9b2c AP |
1509 | { |
1510 | tu->val = 0; | |
1511 | return 0; | |
1512 | } | |
d1bd0ded | 1513 | } |
f13c9b2c AP |
1514 | tu->val = needs_warning ? 2 : 10; |
1515 | return tu->val; | |
d1bd0ded GK |
1516 | } |
1517 | ||
1518 | case RECORD_TYPE: | |
1519 | { | |
c22cacf3 | 1520 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
3aeb3655 EC |
1521 | |
1522 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
d1bd0ded | 1523 | s1 && s2; |
910ad8de | 1524 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
d1bd0ded GK |
1525 | { |
1526 | int result; | |
1527 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
1528 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
1529 | break; | |
744aa42f | 1530 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1531 | enum_and_int_p, different_types_p); |
d1bd0ded GK |
1532 | if (result == 0) |
1533 | break; | |
1534 | if (result == 2) | |
1535 | needs_warning = true; | |
3aeb3655 | 1536 | |
d1bd0ded GK |
1537 | if (TREE_CODE (s1) == FIELD_DECL |
1538 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1539 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1540 | break; | |
1541 | } | |
d1bd0ded | 1542 | if (s1 && s2) |
f13c9b2c AP |
1543 | tu->val = 0; |
1544 | else | |
1545 | tu->val = needs_warning ? 2 : 1; | |
1546 | return tu->val; | |
d1bd0ded GK |
1547 | } |
1548 | ||
1549 | default: | |
366de0ce | 1550 | gcc_unreachable (); |
d1bd0ded GK |
1551 | } |
1552 | } | |
1553 | ||
400fbf9f JW |
1554 | /* Return 1 if two function types F1 and F2 are compatible. |
1555 | If either type specifies no argument types, | |
1556 | the other must specify a fixed number of self-promoting arg types. | |
2f6e4e97 | 1557 | Otherwise, if one type specifies only the number of arguments, |
400fbf9f | 1558 | the other must specify that number of self-promoting arg types. |
744aa42f | 1559 | Otherwise, the argument types must match. |
dc5027f4 | 1560 | ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */ |
400fbf9f JW |
1561 | |
1562 | static int | |
744aa42f | 1563 | function_types_compatible_p (const_tree f1, const_tree f2, |
dc5027f4 | 1564 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1565 | { |
1566 | tree args1, args2; | |
1567 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1568 | int val = 1; | |
1569 | int val1; | |
a6fdc086 GK |
1570 | tree ret1, ret2; |
1571 | ||
1572 | ret1 = TREE_TYPE (f1); | |
1573 | ret2 = TREE_TYPE (f2); | |
1574 | ||
e508a019 JM |
1575 | /* 'volatile' qualifiers on a function's return type used to mean |
1576 | the function is noreturn. */ | |
1577 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
509c9d60 | 1578 | pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); |
a6fdc086 GK |
1579 | if (TYPE_VOLATILE (ret1)) |
1580 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
1581 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
1582 | if (TYPE_VOLATILE (ret2)) | |
1583 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
1584 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
dc5027f4 | 1585 | val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p); |
a6fdc086 | 1586 | if (val == 0) |
400fbf9f JW |
1587 | return 0; |
1588 | ||
1589 | args1 = TYPE_ARG_TYPES (f1); | |
1590 | args2 = TYPE_ARG_TYPES (f2); | |
1591 | ||
dc5027f4 JM |
1592 | if (different_types_p != NULL |
1593 | && (args1 == 0) != (args2 == 0)) | |
1594 | *different_types_p = true; | |
1595 | ||
400fbf9f JW |
1596 | /* An unspecified parmlist matches any specified parmlist |
1597 | whose argument types don't need default promotions. */ | |
1598 | ||
1599 | if (args1 == 0) | |
1600 | { | |
1601 | if (!self_promoting_args_p (args2)) | |
1602 | return 0; | |
1603 | /* If one of these types comes from a non-prototype fn definition, | |
1604 | compare that with the other type's arglist. | |
3176a0c2 | 1605 | If they don't match, ask for a warning (but no error). */ |
400fbf9f | 1606 | if (TYPE_ACTUAL_ARG_TYPES (f1) |
744aa42f | 1607 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), |
dc5027f4 | 1608 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1609 | val = 2; |
1610 | return val; | |
1611 | } | |
1612 | if (args2 == 0) | |
1613 | { | |
1614 | if (!self_promoting_args_p (args1)) | |
1615 | return 0; | |
1616 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
744aa42f | 1617 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), |
dc5027f4 | 1618 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1619 | val = 2; |
1620 | return val; | |
1621 | } | |
1622 | ||
1623 | /* Both types have argument lists: compare them and propagate results. */ | |
dc5027f4 JM |
1624 | val1 = type_lists_compatible_p (args1, args2, enum_and_int_p, |
1625 | different_types_p); | |
400fbf9f JW |
1626 | return val1 != 1 ? val1 : val; |
1627 | } | |
1628 | ||
744aa42f ILT |
1629 | /* Check two lists of types for compatibility, returning 0 for |
1630 | incompatible, 1 for compatible, or 2 for compatible with | |
dc5027f4 JM |
1631 | warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1632 | comptypes_internal. */ | |
400fbf9f JW |
1633 | |
1634 | static int | |
744aa42f | 1635 | type_lists_compatible_p (const_tree args1, const_tree args2, |
dc5027f4 | 1636 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1637 | { |
1638 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1639 | int val = 1; | |
9d5f3e49 | 1640 | int newval = 0; |
400fbf9f JW |
1641 | |
1642 | while (1) | |
1643 | { | |
46df2823 | 1644 | tree a1, mv1, a2, mv2; |
400fbf9f JW |
1645 | if (args1 == 0 && args2 == 0) |
1646 | return val; | |
1647 | /* If one list is shorter than the other, | |
1648 | they fail to match. */ | |
1649 | if (args1 == 0 || args2 == 0) | |
1650 | return 0; | |
46df2823 JM |
1651 | mv1 = a1 = TREE_VALUE (args1); |
1652 | mv2 = a2 = TREE_VALUE (args2); | |
1653 | if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) | |
267bac10 JM |
1654 | mv1 = (TYPE_ATOMIC (mv1) |
1655 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1), | |
1656 | TYPE_QUAL_ATOMIC) | |
1657 | : TYPE_MAIN_VARIANT (mv1)); | |
46df2823 | 1658 | if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) |
267bac10 JM |
1659 | mv2 = (TYPE_ATOMIC (mv2) |
1660 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2), | |
1661 | TYPE_QUAL_ATOMIC) | |
1662 | : TYPE_MAIN_VARIANT (mv2)); | |
400fbf9f JW |
1663 | /* A null pointer instead of a type |
1664 | means there is supposed to be an argument | |
1665 | but nothing is specified about what type it has. | |
1666 | So match anything that self-promotes. */ | |
dc5027f4 JM |
1667 | if (different_types_p != NULL |
1668 | && (a1 == 0) != (a2 == 0)) | |
1669 | *different_types_p = true; | |
46df2823 | 1670 | if (a1 == 0) |
400fbf9f | 1671 | { |
46df2823 | 1672 | if (c_type_promotes_to (a2) != a2) |
400fbf9f JW |
1673 | return 0; |
1674 | } | |
46df2823 | 1675 | else if (a2 == 0) |
400fbf9f | 1676 | { |
46df2823 | 1677 | if (c_type_promotes_to (a1) != a1) |
400fbf9f JW |
1678 | return 0; |
1679 | } | |
8f5b6d29 | 1680 | /* If one of the lists has an error marker, ignore this arg. */ |
46df2823 JM |
1681 | else if (TREE_CODE (a1) == ERROR_MARK |
1682 | || TREE_CODE (a2) == ERROR_MARK) | |
8f5b6d29 | 1683 | ; |
dc5027f4 JM |
1684 | else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p, |
1685 | different_types_p))) | |
400fbf9f | 1686 | { |
dc5027f4 JM |
1687 | if (different_types_p != NULL) |
1688 | *different_types_p = true; | |
400fbf9f JW |
1689 | /* Allow wait (union {union wait *u; int *i} *) |
1690 | and wait (union wait *) to be compatible. */ | |
46df2823 JM |
1691 | if (TREE_CODE (a1) == UNION_TYPE |
1692 | && (TYPE_NAME (a1) == 0 | |
ebf0bf7f | 1693 | || TYPE_TRANSPARENT_AGGR (a1)) |
46df2823 JM |
1694 | && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST |
1695 | && tree_int_cst_equal (TYPE_SIZE (a1), | |
1696 | TYPE_SIZE (a2))) | |
400fbf9f JW |
1697 | { |
1698 | tree memb; | |
46df2823 | 1699 | for (memb = TYPE_FIELDS (a1); |
910ad8de | 1700 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1701 | { |
1702 | tree mv3 = TREE_TYPE (memb); | |
1703 | if (mv3 && mv3 != error_mark_node | |
1704 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
267bac10 JM |
1705 | mv3 = (TYPE_ATOMIC (mv3) |
1706 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), | |
1707 | TYPE_QUAL_ATOMIC) | |
1708 | : TYPE_MAIN_VARIANT (mv3)); | |
dc5027f4 JM |
1709 | if (comptypes_internal (mv3, mv2, enum_and_int_p, |
1710 | different_types_p)) | |
58cb41e6 JJ |
1711 | break; |
1712 | } | |
400fbf9f JW |
1713 | if (memb == 0) |
1714 | return 0; | |
1715 | } | |
46df2823 JM |
1716 | else if (TREE_CODE (a2) == UNION_TYPE |
1717 | && (TYPE_NAME (a2) == 0 | |
ebf0bf7f | 1718 | || TYPE_TRANSPARENT_AGGR (a2)) |
46df2823 JM |
1719 | && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST |
1720 | && tree_int_cst_equal (TYPE_SIZE (a2), | |
1721 | TYPE_SIZE (a1))) | |
400fbf9f JW |
1722 | { |
1723 | tree memb; | |
46df2823 | 1724 | for (memb = TYPE_FIELDS (a2); |
910ad8de | 1725 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1726 | { |
1727 | tree mv3 = TREE_TYPE (memb); | |
1728 | if (mv3 && mv3 != error_mark_node | |
1729 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
267bac10 JM |
1730 | mv3 = (TYPE_ATOMIC (mv3) |
1731 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3), | |
1732 | TYPE_QUAL_ATOMIC) | |
1733 | : TYPE_MAIN_VARIANT (mv3)); | |
dc5027f4 JM |
1734 | if (comptypes_internal (mv3, mv1, enum_and_int_p, |
1735 | different_types_p)) | |
58cb41e6 JJ |
1736 | break; |
1737 | } | |
400fbf9f JW |
1738 | if (memb == 0) |
1739 | return 0; | |
1740 | } | |
1741 | else | |
1742 | return 0; | |
1743 | } | |
1744 | ||
1745 | /* comptypes said ok, but record if it said to warn. */ | |
1746 | if (newval > val) | |
1747 | val = newval; | |
1748 | ||
1749 | args1 = TREE_CHAIN (args1); | |
1750 | args2 = TREE_CHAIN (args2); | |
1751 | } | |
1752 | } | |
400fbf9f | 1753 | \f |
400fbf9f JW |
1754 | /* Compute the size to increment a pointer by. */ |
1755 | ||
4e2fb7de | 1756 | static tree |
58f9752a | 1757 | c_size_in_bytes (const_tree type) |
400fbf9f JW |
1758 | { |
1759 | enum tree_code code = TREE_CODE (type); | |
1760 | ||
fed3cef0 RK |
1761 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) |
1762 | return size_one_node; | |
1763 | ||
d0f062fb | 1764 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
400fbf9f JW |
1765 | { |
1766 | error ("arithmetic on pointer to an incomplete type"); | |
fed3cef0 | 1767 | return size_one_node; |
400fbf9f JW |
1768 | } |
1769 | ||
1770 | /* Convert in case a char is more than one unit. */ | |
db3927fb AH |
1771 | return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
1772 | size_int (TYPE_PRECISION (char_type_node) | |
1773 | / BITS_PER_UNIT)); | |
400fbf9f | 1774 | } |
400fbf9f | 1775 | \f |
400fbf9f JW |
1776 | /* Return either DECL or its known constant value (if it has one). */ |
1777 | ||
56cb9733 | 1778 | tree |
2f6e4e97 | 1779 | decl_constant_value (tree decl) |
400fbf9f | 1780 | { |
a7c1916a | 1781 | if (/* Don't change a variable array bound or initial value to a constant |
4f976745 RK |
1782 | in a place where a variable is invalid. Note that DECL_INITIAL |
1783 | isn't valid for a PARM_DECL. */ | |
a7c1916a | 1784 | current_function_decl != 0 |
4f976745 | 1785 | && TREE_CODE (decl) != PARM_DECL |
3f75a254 | 1786 | && !TREE_THIS_VOLATILE (decl) |
83bab8db | 1787 | && TREE_READONLY (decl) |
400fbf9f JW |
1788 | && DECL_INITIAL (decl) != 0 |
1789 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1790 | /* This is invalid if initial value is not constant. | |
1791 | If it has either a function call, a memory reference, | |
1792 | or a variable, then re-evaluating it could give different results. */ | |
1793 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1794 | /* Check for cases where this is sub-optimal, even though valid. */ | |
2f74f7e9 | 1795 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
400fbf9f JW |
1796 | return DECL_INITIAL (decl); |
1797 | return decl; | |
1798 | } | |
1799 | ||
f2a71bbc JM |
1800 | /* Convert the array expression EXP to a pointer. */ |
1801 | static tree | |
c2255bc4 | 1802 | array_to_pointer_conversion (location_t loc, tree exp) |
207bf485 | 1803 | { |
f2a71bbc | 1804 | tree orig_exp = exp; |
207bf485 | 1805 | tree type = TREE_TYPE (exp); |
f2a71bbc JM |
1806 | tree adr; |
1807 | tree restype = TREE_TYPE (type); | |
1808 | tree ptrtype; | |
207bf485 | 1809 | |
f2a71bbc | 1810 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
207bf485 | 1811 | |
f2a71bbc | 1812 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1813 | |
487a92fe JM |
1814 | if (TREE_NO_WARNING (orig_exp)) |
1815 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1816 | |
f2a71bbc JM |
1817 | ptrtype = build_pointer_type (restype); |
1818 | ||
1819 | if (TREE_CODE (exp) == INDIRECT_REF) | |
1820 | return convert (ptrtype, TREE_OPERAND (exp, 0)); | |
1821 | ||
1f37c583 JM |
1822 | /* In C++ array compound literals are temporary objects unless they are |
1823 | const or appear in namespace scope, so they are destroyed too soon | |
1824 | to use them for much of anything (c++/53220). */ | |
1825 | if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) | |
1826 | { | |
1827 | tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
1828 | if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) | |
1829 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, | |
1830 | "converting an array compound literal to a pointer " | |
1831 | "is ill-formed in C++"); | |
1832 | } | |
1833 | ||
c2255bc4 | 1834 | adr = build_unary_op (loc, ADDR_EXPR, exp, 1); |
f2a71bbc JM |
1835 | return convert (ptrtype, adr); |
1836 | } | |
207bf485 | 1837 | |
f2a71bbc JM |
1838 | /* Convert the function expression EXP to a pointer. */ |
1839 | static tree | |
c2255bc4 | 1840 | function_to_pointer_conversion (location_t loc, tree exp) |
f2a71bbc JM |
1841 | { |
1842 | tree orig_exp = exp; | |
207bf485 | 1843 | |
f2a71bbc | 1844 | gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); |
207bf485 | 1845 | |
f2a71bbc | 1846 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1847 | |
f2a71bbc JM |
1848 | if (TREE_NO_WARNING (orig_exp)) |
1849 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1850 | |
c2255bc4 | 1851 | return build_unary_op (loc, ADDR_EXPR, exp, 0); |
f2a71bbc | 1852 | } |
207bf485 | 1853 | |
ebfbbdc5 JJ |
1854 | /* Mark EXP as read, not just set, for set but not used -Wunused |
1855 | warning purposes. */ | |
1856 | ||
1857 | void | |
1858 | mark_exp_read (tree exp) | |
1859 | { | |
1860 | switch (TREE_CODE (exp)) | |
1861 | { | |
1862 | case VAR_DECL: | |
1863 | case PARM_DECL: | |
1864 | DECL_READ_P (exp) = 1; | |
1865 | break; | |
1866 | case ARRAY_REF: | |
1867 | case COMPONENT_REF: | |
1868 | case MODIFY_EXPR: | |
1869 | case REALPART_EXPR: | |
1870 | case IMAGPART_EXPR: | |
1871 | CASE_CONVERT: | |
1872 | case ADDR_EXPR: | |
1873 | mark_exp_read (TREE_OPERAND (exp, 0)); | |
1874 | break; | |
1875 | case COMPOUND_EXPR: | |
82c3c067 | 1876 | case C_MAYBE_CONST_EXPR: |
ebfbbdc5 JJ |
1877 | mark_exp_read (TREE_OPERAND (exp, 1)); |
1878 | break; | |
1879 | default: | |
1880 | break; | |
1881 | } | |
1882 | } | |
1883 | ||
f2a71bbc JM |
1884 | /* Perform the default conversion of arrays and functions to pointers. |
1885 | Return the result of converting EXP. For any other expression, just | |
c2255bc4 AH |
1886 | return EXP. |
1887 | ||
1888 | LOC is the location of the expression. */ | |
f2a71bbc JM |
1889 | |
1890 | struct c_expr | |
c2255bc4 | 1891 | default_function_array_conversion (location_t loc, struct c_expr exp) |
f2a71bbc JM |
1892 | { |
1893 | tree orig_exp = exp.value; | |
1894 | tree type = TREE_TYPE (exp.value); | |
1895 | enum tree_code code = TREE_CODE (type); | |
1896 | ||
1897 | switch (code) | |
1898 | { | |
1899 | case ARRAY_TYPE: | |
1900 | { | |
1901 | bool not_lvalue = false; | |
1902 | bool lvalue_array_p; | |
1903 | ||
1904 | while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR | |
1043771b | 1905 | || CONVERT_EXPR_P (exp.value)) |
f2a71bbc JM |
1906 | && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) |
1907 | { | |
1908 | if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) | |
1909 | not_lvalue = true; | |
1910 | exp.value = TREE_OPERAND (exp.value, 0); | |
1911 | } | |
1912 | ||
1913 | if (TREE_NO_WARNING (orig_exp)) | |
1914 | TREE_NO_WARNING (exp.value) = 1; | |
1915 | ||
1916 | lvalue_array_p = !not_lvalue && lvalue_p (exp.value); | |
1917 | if (!flag_isoc99 && !lvalue_array_p) | |
1918 | { | |
1919 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1920 | Normally, using such an array would be invalid; but it can | |
1921 | be used correctly inside sizeof or as a statement expression. | |
1922 | Thus, do not give an error here; an error will result later. */ | |
1923 | return exp; | |
1924 | } | |
1925 | ||
c2255bc4 | 1926 | exp.value = array_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1927 | } |
1928 | break; | |
1929 | case FUNCTION_TYPE: | |
c2255bc4 | 1930 | exp.value = function_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1931 | break; |
1932 | default: | |
f2a71bbc | 1933 | break; |
207bf485 | 1934 | } |
f2a71bbc | 1935 | |
207bf485 JM |
1936 | return exp; |
1937 | } | |
1938 | ||
ebfbbdc5 JJ |
1939 | struct c_expr |
1940 | default_function_array_read_conversion (location_t loc, struct c_expr exp) | |
1941 | { | |
1942 | mark_exp_read (exp.value); | |
1943 | return default_function_array_conversion (loc, exp); | |
1944 | } | |
522ddfa2 | 1945 | |
267bac10 JM |
1946 | /* Return whether EXPR should be treated as an atomic lvalue for the |
1947 | purposes of load and store handling. */ | |
1948 | ||
1949 | static bool | |
1950 | really_atomic_lvalue (tree expr) | |
1951 | { | |
1952 | if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node) | |
1953 | return false; | |
1954 | if (!TYPE_ATOMIC (TREE_TYPE (expr))) | |
1955 | return false; | |
1956 | if (!lvalue_p (expr)) | |
1957 | return false; | |
1958 | ||
1959 | /* Ignore _Atomic on register variables, since their addresses can't | |
1960 | be taken so (a) atomicity is irrelevant and (b) the normal atomic | |
1961 | sequences wouldn't work. Ignore _Atomic on structures containing | |
1962 | bit-fields, since accessing elements of atomic structures or | |
1963 | unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if | |
1964 | it's undefined at translation time or execution time, and the | |
1965 | normal atomic sequences again wouldn't work. */ | |
1966 | while (handled_component_p (expr)) | |
1967 | { | |
1968 | if (TREE_CODE (expr) == COMPONENT_REF | |
1969 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
1970 | return false; | |
1971 | expr = TREE_OPERAND (expr, 0); | |
1972 | } | |
1973 | if (DECL_P (expr) && C_DECL_REGISTER (expr)) | |
1974 | return false; | |
1975 | return true; | |
1976 | } | |
1977 | ||
1978 | /* Convert expression EXP (location LOC) from lvalue to rvalue, | |
1979 | including converting functions and arrays to pointers if CONVERT_P. | |
1980 | If READ_P, also mark the expression as having been read. */ | |
1981 | ||
1982 | struct c_expr | |
1983 | convert_lvalue_to_rvalue (location_t loc, struct c_expr exp, | |
1984 | bool convert_p, bool read_p) | |
1985 | { | |
1986 | if (read_p) | |
1987 | mark_exp_read (exp.value); | |
1988 | if (convert_p) | |
1989 | exp = default_function_array_conversion (loc, exp); | |
1990 | if (really_atomic_lvalue (exp.value)) | |
1991 | { | |
1992 | vec<tree, va_gc> *params; | |
1993 | tree nonatomic_type, tmp, tmp_addr, fndecl, func_call; | |
1994 | tree expr_type = TREE_TYPE (exp.value); | |
1995 | tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0); | |
1996 | tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); | |
1997 | ||
1998 | gcc_assert (TYPE_ATOMIC (expr_type)); | |
1999 | ||
2000 | /* Expansion of a generic atomic load may require an addition | |
2001 | element, so allocate enough to prevent a resize. */ | |
2002 | vec_alloc (params, 4); | |
2003 | ||
2004 | /* Remove the qualifiers for the rest of the expressions and | |
2005 | create the VAL temp variable to hold the RHS. */ | |
2006 | nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED); | |
2007 | tmp = create_tmp_var (nonatomic_type, NULL); | |
2008 | tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0); | |
2009 | TREE_ADDRESSABLE (tmp) = 1; | |
2010 | ||
2011 | /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */ | |
2012 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); | |
2013 | params->quick_push (expr_addr); | |
2014 | params->quick_push (tmp_addr); | |
2015 | params->quick_push (seq_cst); | |
2016 | func_call = build_function_call_vec (loc, fndecl, params, NULL); | |
2017 | ||
2018 | /* Return tmp which contains the value loaded. */ | |
2019 | exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp); | |
2020 | } | |
2021 | return exp; | |
2022 | } | |
2023 | ||
522ddfa2 JM |
2024 | /* EXP is an expression of integer type. Apply the integer promotions |
2025 | to it and return the promoted value. */ | |
400fbf9f JW |
2026 | |
2027 | tree | |
522ddfa2 | 2028 | perform_integral_promotions (tree exp) |
400fbf9f | 2029 | { |
b3694847 SS |
2030 | tree type = TREE_TYPE (exp); |
2031 | enum tree_code code = TREE_CODE (type); | |
400fbf9f | 2032 | |
522ddfa2 | 2033 | gcc_assert (INTEGRAL_TYPE_P (type)); |
157689c6 | 2034 | |
400fbf9f JW |
2035 | /* Normally convert enums to int, |
2036 | but convert wide enums to something wider. */ | |
2037 | if (code == ENUMERAL_TYPE) | |
2038 | { | |
b0c48229 NB |
2039 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
2040 | TYPE_PRECISION (integer_type_node)), | |
2041 | ((TYPE_PRECISION (type) | |
2042 | >= TYPE_PRECISION (integer_type_node)) | |
8df83eae | 2043 | && TYPE_UNSIGNED (type))); |
05bccae2 | 2044 | |
400fbf9f JW |
2045 | return convert (type, exp); |
2046 | } | |
2047 | ||
522ddfa2 JM |
2048 | /* ??? This should no longer be needed now bit-fields have their |
2049 | proper types. */ | |
9753f113 | 2050 | if (TREE_CODE (exp) == COMPONENT_REF |
05bccae2 | 2051 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) |
cff9c407 | 2052 | /* If it's thinner than an int, promote it like a |
d72040f5 | 2053 | c_promoting_integer_type_p, otherwise leave it alone. */ |
05bccae2 RK |
2054 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), |
2055 | TYPE_PRECISION (integer_type_node))) | |
f458d1d5 | 2056 | return convert (integer_type_node, exp); |
9753f113 | 2057 | |
d72040f5 | 2058 | if (c_promoting_integer_type_p (type)) |
400fbf9f | 2059 | { |
f458d1d5 | 2060 | /* Preserve unsignedness if not really getting any wider. */ |
8df83eae | 2061 | if (TYPE_UNSIGNED (type) |
f458d1d5 | 2062 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
400fbf9f | 2063 | return convert (unsigned_type_node, exp); |
05bccae2 | 2064 | |
400fbf9f JW |
2065 | return convert (integer_type_node, exp); |
2066 | } | |
05bccae2 | 2067 | |
522ddfa2 JM |
2068 | return exp; |
2069 | } | |
2070 | ||
2071 | ||
2072 | /* Perform default promotions for C data used in expressions. | |
46bdb9cf | 2073 | Enumeral types or short or char are converted to int. |
522ddfa2 JM |
2074 | In addition, manifest constants symbols are replaced by their values. */ |
2075 | ||
2076 | tree | |
2077 | default_conversion (tree exp) | |
2078 | { | |
2079 | tree orig_exp; | |
2080 | tree type = TREE_TYPE (exp); | |
2081 | enum tree_code code = TREE_CODE (type); | |
40449a90 | 2082 | tree promoted_type; |
522ddfa2 | 2083 | |
ebfbbdc5 JJ |
2084 | mark_exp_read (exp); |
2085 | ||
46bdb9cf JM |
2086 | /* Functions and arrays have been converted during parsing. */ |
2087 | gcc_assert (code != FUNCTION_TYPE); | |
2088 | if (code == ARRAY_TYPE) | |
2089 | return exp; | |
522ddfa2 JM |
2090 | |
2091 | /* Constants can be used directly unless they're not loadable. */ | |
2092 | if (TREE_CODE (exp) == CONST_DECL) | |
2093 | exp = DECL_INITIAL (exp); | |
2094 | ||
522ddfa2 JM |
2095 | /* Strip no-op conversions. */ |
2096 | orig_exp = exp; | |
2097 | STRIP_TYPE_NOPS (exp); | |
2098 | ||
2099 | if (TREE_NO_WARNING (orig_exp)) | |
2100 | TREE_NO_WARNING (exp) = 1; | |
2101 | ||
400fbf9f JW |
2102 | if (code == VOID_TYPE) |
2103 | { | |
2104 | error ("void value not ignored as it ought to be"); | |
2105 | return error_mark_node; | |
2106 | } | |
808d6eaa JM |
2107 | |
2108 | exp = require_complete_type (exp); | |
2109 | if (exp == error_mark_node) | |
2110 | return error_mark_node; | |
2111 | ||
40449a90 SL |
2112 | promoted_type = targetm.promoted_type (type); |
2113 | if (promoted_type) | |
2114 | return convert (promoted_type, exp); | |
2115 | ||
808d6eaa JM |
2116 | if (INTEGRAL_TYPE_P (type)) |
2117 | return perform_integral_promotions (exp); | |
2118 | ||
400fbf9f JW |
2119 | return exp; |
2120 | } | |
2121 | \f | |
0fb96aa4 | 2122 | /* Look up COMPONENT in a structure or union TYPE. |
e9b2c823 NB |
2123 | |
2124 | If the component name is not found, returns NULL_TREE. Otherwise, | |
2125 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
2126 | stepping down the chain to the component, which is in the last | |
2127 | TREE_VALUE of the list. Normally the list is of length one, but if | |
2128 | the component is embedded within (nested) anonymous structures or | |
2129 | unions, the list steps down the chain to the component. */ | |
2f6e4e97 | 2130 | |
2f2d13da | 2131 | static tree |
0fb96aa4 | 2132 | lookup_field (tree type, tree component) |
2f2d13da DE |
2133 | { |
2134 | tree field; | |
2135 | ||
2136 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
2137 | to the field elements. Use a binary search on this array to quickly | |
2138 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
2139 | will always be set for structures which have many elements. */ | |
2140 | ||
22a0b85f | 2141 | if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) |
2f2d13da DE |
2142 | { |
2143 | int bot, top, half; | |
d07605f5 | 2144 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; |
2f2d13da DE |
2145 | |
2146 | field = TYPE_FIELDS (type); | |
2147 | bot = 0; | |
d07605f5 | 2148 | top = TYPE_LANG_SPECIFIC (type)->s->len; |
2f2d13da DE |
2149 | while (top - bot > 1) |
2150 | { | |
2f2d13da DE |
2151 | half = (top - bot + 1) >> 1; |
2152 | field = field_array[bot+half]; | |
2153 | ||
2154 | if (DECL_NAME (field) == NULL_TREE) | |
2155 | { | |
2156 | /* Step through all anon unions in linear fashion. */ | |
2157 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
2158 | { | |
2f2d13da | 2159 | field = field_array[bot++]; |
a68b98cf RK |
2160 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
2161 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
19d76e60 | 2162 | { |
0fb96aa4 | 2163 | tree anon = lookup_field (TREE_TYPE (field), component); |
e9b2c823 NB |
2164 | |
2165 | if (anon) | |
2166 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2167 | |
2168 | /* The Plan 9 compiler permits referring | |
2169 | directly to an anonymous struct/union field | |
2170 | using a typedef name. */ | |
2171 | if (flag_plan9_extensions | |
2172 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2173 | && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) | |
2174 | == TYPE_DECL) | |
2175 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2176 | == component)) | |
2177 | break; | |
2f6e4e97 | 2178 | } |
2f2d13da DE |
2179 | } |
2180 | ||
2181 | /* Entire record is only anon unions. */ | |
2182 | if (bot > top) | |
2183 | return NULL_TREE; | |
2184 | ||
2185 | /* Restart the binary search, with new lower bound. */ | |
2186 | continue; | |
2187 | } | |
2188 | ||
e8b87aac | 2189 | if (DECL_NAME (field) == component) |
2f2d13da | 2190 | break; |
e8b87aac | 2191 | if (DECL_NAME (field) < component) |
2f2d13da DE |
2192 | bot += half; |
2193 | else | |
2194 | top = bot + half; | |
2195 | } | |
2196 | ||
2197 | if (DECL_NAME (field_array[bot]) == component) | |
2198 | field = field_array[bot]; | |
2199 | else if (DECL_NAME (field) != component) | |
e9b2c823 | 2200 | return NULL_TREE; |
2f2d13da DE |
2201 | } |
2202 | else | |
2203 | { | |
910ad8de | 2204 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
2f2d13da | 2205 | { |
e9b2c823 NB |
2206 | if (DECL_NAME (field) == NULL_TREE |
2207 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
2208 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
2f2d13da | 2209 | { |
0fb96aa4 | 2210 | tree anon = lookup_field (TREE_TYPE (field), component); |
a68b98cf | 2211 | |
e9b2c823 NB |
2212 | if (anon) |
2213 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2214 | |
2215 | /* The Plan 9 compiler permits referring directly to an | |
2216 | anonymous struct/union field using a typedef | |
2217 | name. */ | |
2218 | if (flag_plan9_extensions | |
2219 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2220 | && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL | |
2221 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2222 | == component)) | |
2223 | break; | |
2f2d13da DE |
2224 | } |
2225 | ||
2226 | if (DECL_NAME (field) == component) | |
2227 | break; | |
2228 | } | |
e9b2c823 NB |
2229 | |
2230 | if (field == NULL_TREE) | |
2231 | return NULL_TREE; | |
2f2d13da DE |
2232 | } |
2233 | ||
e9b2c823 | 2234 | return tree_cons (NULL_TREE, field, NULL_TREE); |
2f2d13da DE |
2235 | } |
2236 | ||
c2255bc4 AH |
2237 | /* Make an expression to refer to the COMPONENT field of structure or |
2238 | union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the | |
2239 | location of the COMPONENT_REF. */ | |
400fbf9f JW |
2240 | |
2241 | tree | |
c2255bc4 | 2242 | build_component_ref (location_t loc, tree datum, tree component) |
400fbf9f | 2243 | { |
b3694847 SS |
2244 | tree type = TREE_TYPE (datum); |
2245 | enum tree_code code = TREE_CODE (type); | |
2246 | tree field = NULL; | |
2247 | tree ref; | |
1e57bf47 | 2248 | bool datum_lvalue = lvalue_p (datum); |
400fbf9f | 2249 | |
7a3ea201 RH |
2250 | if (!objc_is_public (datum, component)) |
2251 | return error_mark_node; | |
2252 | ||
46a88c12 | 2253 | /* Detect Objective-C property syntax object.property. */ |
668ea4b1 | 2254 | if (c_dialect_objc () |
46a88c12 | 2255 | && (ref = objc_maybe_build_component_ref (datum, component))) |
668ea4b1 IS |
2256 | return ref; |
2257 | ||
400fbf9f JW |
2258 | /* See if there is a field or component with name COMPONENT. */ |
2259 | ||
2260 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
2261 | { | |
d0f062fb | 2262 | if (!COMPLETE_TYPE_P (type)) |
400fbf9f | 2263 | { |
7a228918 | 2264 | c_incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
2265 | return error_mark_node; |
2266 | } | |
2267 | ||
0fb96aa4 | 2268 | field = lookup_field (type, component); |
400fbf9f JW |
2269 | |
2270 | if (!field) | |
2271 | { | |
c2255bc4 | 2272 | error_at (loc, "%qT has no member named %qE", type, component); |
400fbf9f JW |
2273 | return error_mark_node; |
2274 | } | |
400fbf9f | 2275 | |
e9b2c823 NB |
2276 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. |
2277 | This might be better solved in future the way the C++ front | |
2278 | end does it - by giving the anonymous entities each a | |
2279 | separate name and type, and then have build_component_ref | |
2280 | recursively call itself. We can't do that here. */ | |
46ea50cb | 2281 | do |
19d76e60 | 2282 | { |
e9b2c823 | 2283 | tree subdatum = TREE_VALUE (field); |
efed193e JM |
2284 | int quals; |
2285 | tree subtype; | |
1e57bf47 | 2286 | bool use_datum_quals; |
e9b2c823 NB |
2287 | |
2288 | if (TREE_TYPE (subdatum) == error_mark_node) | |
2289 | return error_mark_node; | |
2290 | ||
1e57bf47 JM |
2291 | /* If this is an rvalue, it does not have qualifiers in C |
2292 | standard terms and we must avoid propagating such | |
2293 | qualifiers down to a non-lvalue array that is then | |
2294 | converted to a pointer. */ | |
2295 | use_datum_quals = (datum_lvalue | |
2296 | || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); | |
2297 | ||
efed193e | 2298 | quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); |
1e57bf47 JM |
2299 | if (use_datum_quals) |
2300 | quals |= TYPE_QUALS (TREE_TYPE (datum)); | |
efed193e JM |
2301 | subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); |
2302 | ||
2303 | ref = build3 (COMPONENT_REF, subtype, datum, subdatum, | |
53fb4de3 | 2304 | NULL_TREE); |
c2255bc4 | 2305 | SET_EXPR_LOCATION (ref, loc); |
1e57bf47 JM |
2306 | if (TREE_READONLY (subdatum) |
2307 | || (use_datum_quals && TREE_READONLY (datum))) | |
19d76e60 | 2308 | TREE_READONLY (ref) = 1; |
1e57bf47 JM |
2309 | if (TREE_THIS_VOLATILE (subdatum) |
2310 | || (use_datum_quals && TREE_THIS_VOLATILE (datum))) | |
19d76e60 | 2311 | TREE_THIS_VOLATILE (ref) = 1; |
e23bd218 IR |
2312 | |
2313 | if (TREE_DEPRECATED (subdatum)) | |
9b86d6bb | 2314 | warn_deprecated_use (subdatum, NULL_TREE); |
e23bd218 | 2315 | |
19d76e60 | 2316 | datum = ref; |
46ea50cb RS |
2317 | |
2318 | field = TREE_CHAIN (field); | |
19d76e60 | 2319 | } |
46ea50cb | 2320 | while (field); |
19d76e60 | 2321 | |
400fbf9f JW |
2322 | return ref; |
2323 | } | |
2324 | else if (code != ERROR_MARK) | |
c2255bc4 AH |
2325 | error_at (loc, |
2326 | "request for member %qE in something not a structure or union", | |
2327 | component); | |
400fbf9f JW |
2328 | |
2329 | return error_mark_node; | |
2330 | } | |
2331 | \f | |
2332 | /* Given an expression PTR for a pointer, return an expression | |
2333 | for the value pointed to. | |
6a3799eb AH |
2334 | ERRORSTRING is the name of the operator to appear in error messages. |
2335 | ||
2336 | LOC is the location to use for the generated tree. */ | |
400fbf9f JW |
2337 | |
2338 | tree | |
dd865ef6 | 2339 | build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) |
400fbf9f | 2340 | { |
b3694847 SS |
2341 | tree pointer = default_conversion (ptr); |
2342 | tree type = TREE_TYPE (pointer); | |
6a3799eb | 2343 | tree ref; |
400fbf9f JW |
2344 | |
2345 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b | 2346 | { |
1043771b | 2347 | if (CONVERT_EXPR_P (pointer) |
79bedddc SR |
2348 | || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) |
2349 | { | |
2350 | /* If a warning is issued, mark it to avoid duplicates from | |
2351 | the backend. This only needs to be done at | |
2352 | warn_strict_aliasing > 2. */ | |
2353 | if (warn_strict_aliasing > 2) | |
2354 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), | |
2355 | type, TREE_OPERAND (pointer, 0))) | |
2356 | TREE_NO_WARNING (pointer) = 1; | |
2357 | } | |
2358 | ||
870cc33b | 2359 | if (TREE_CODE (pointer) == ADDR_EXPR |
870cc33b RS |
2360 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) |
2361 | == TREE_TYPE (type))) | |
6a3799eb AH |
2362 | { |
2363 | ref = TREE_OPERAND (pointer, 0); | |
2364 | protected_set_expr_location (ref, loc); | |
2365 | return ref; | |
2366 | } | |
870cc33b RS |
2367 | else |
2368 | { | |
2369 | tree t = TREE_TYPE (type); | |
46df2823 | 2370 | |
984dfd8c | 2371 | ref = build1 (INDIRECT_REF, t, pointer); |
400fbf9f | 2372 | |
baae9b65 | 2373 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) |
870cc33b | 2374 | { |
c9f9eb5d | 2375 | error_at (loc, "dereferencing pointer to incomplete type"); |
870cc33b RS |
2376 | return error_mark_node; |
2377 | } | |
7d882b83 | 2378 | if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) |
c9f9eb5d | 2379 | warning_at (loc, 0, "dereferencing %<void *%> pointer"); |
870cc33b RS |
2380 | |
2381 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
2382 | so that we get the proper error message if the result is used | |
2383 | to assign to. Also, &* is supposed to be a no-op. | |
2384 | And ANSI C seems to specify that the type of the result | |
2385 | should be the const type. */ | |
2386 | /* A de-reference of a pointer to const is not a const. It is valid | |
2387 | to change it via some other pointer. */ | |
2388 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
2389 | TREE_SIDE_EFFECTS (ref) | |
271bd540 | 2390 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); |
493692cd | 2391 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
6a3799eb | 2392 | protected_set_expr_location (ref, loc); |
870cc33b RS |
2393 | return ref; |
2394 | } | |
2395 | } | |
400fbf9f | 2396 | else if (TREE_CODE (pointer) != ERROR_MARK) |
7a6daeb0 NF |
2397 | invalid_indirection_error (loc, type, errstring); |
2398 | ||
400fbf9f JW |
2399 | return error_mark_node; |
2400 | } | |
2401 | ||
2402 | /* This handles expressions of the form "a[i]", which denotes | |
2403 | an array reference. | |
2404 | ||
2405 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2406 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2407 | This avoids forcing the array out of registers, and can work on | |
2408 | arrays that are not lvalues (for example, members of structures returned | |
6a3799eb AH |
2409 | by functions). |
2410 | ||
30cd1c5d AS |
2411 | For vector types, allow vector[i] but not i[vector], and create |
2412 | *(((type*)&vectortype) + i) for the expression. | |
2413 | ||
6a3799eb | 2414 | LOC is the location to use for the returned expression. */ |
400fbf9f JW |
2415 | |
2416 | tree | |
c2255bc4 | 2417 | build_array_ref (location_t loc, tree array, tree index) |
400fbf9f | 2418 | { |
6a3799eb | 2419 | tree ret; |
a4ab7973 | 2420 | bool swapped = false; |
400fbf9f JW |
2421 | if (TREE_TYPE (array) == error_mark_node |
2422 | || TREE_TYPE (index) == error_mark_node) | |
2423 | return error_mark_node; | |
2424 | ||
36536d79 BI |
2425 | if (flag_enable_cilkplus && contains_array_notation_expr (index)) |
2426 | { | |
2427 | size_t rank = 0; | |
2428 | if (!find_rank (loc, index, index, true, &rank)) | |
2429 | return error_mark_node; | |
2430 | if (rank > 1) | |
2431 | { | |
2432 | error_at (loc, "rank of the array's index is greater than 1"); | |
2433 | return error_mark_node; | |
2434 | } | |
2435 | } | |
a4ab7973 | 2436 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE |
30cd1c5d AS |
2437 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE |
2438 | /* Allow vector[index] but not index[vector]. */ | |
2439 | && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE) | |
400fbf9f | 2440 | { |
a4ab7973 JM |
2441 | tree temp; |
2442 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE | |
2443 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
fdeefd49 | 2444 | { |
f90e8e2e | 2445 | error_at (loc, |
30cd1c5d AS |
2446 | "subscripted value is neither array nor pointer nor vector"); |
2447 | ||
fdeefd49 RS |
2448 | return error_mark_node; |
2449 | } | |
a4ab7973 JM |
2450 | temp = array; |
2451 | array = index; | |
2452 | index = temp; | |
2453 | swapped = true; | |
2454 | } | |
2455 | ||
2456 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2457 | { | |
a63068b6 | 2458 | error_at (loc, "array subscript is not an integer"); |
a4ab7973 JM |
2459 | return error_mark_node; |
2460 | } | |
2461 | ||
2462 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2463 | { | |
a63068b6 | 2464 | error_at (loc, "subscripted value is pointer to function"); |
a4ab7973 JM |
2465 | return error_mark_node; |
2466 | } | |
2467 | ||
ff6b6641 GDR |
2468 | /* ??? Existing practice has been to warn only when the char |
2469 | index is syntactically the index, not for char[array]. */ | |
2470 | if (!swapped) | |
2471 | warn_array_subscript_with_type_char (index); | |
a4ab7973 JM |
2472 | |
2473 | /* Apply default promotions *after* noticing character types. */ | |
2474 | index = default_conversion (index); | |
2475 | ||
2476 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
f90e8e2e | 2477 | |
7edaa4d2 | 2478 | convert_vector_to_pointer_for_subscript (loc, &array, index); |
a4ab7973 JM |
2479 | |
2480 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2481 | { | |
e4d35515 | 2482 | tree rval, type; |
fdeefd49 | 2483 | |
400fbf9f JW |
2484 | /* An array that is indexed by a non-constant |
2485 | cannot be stored in a register; we must be able to do | |
2486 | address arithmetic on its address. | |
2487 | Likewise an array of elements of variable size. */ | |
2488 | if (TREE_CODE (index) != INTEGER_CST | |
d0f062fb | 2489 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
400fbf9f JW |
2490 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
2491 | { | |
dffd7eb6 | 2492 | if (!c_mark_addressable (array)) |
400fbf9f JW |
2493 | return error_mark_node; |
2494 | } | |
e6d52559 JW |
2495 | /* An array that is indexed by a constant value which is not within |
2496 | the array bounds cannot be stored in a register either; because we | |
2497 | would get a crash in store_bit_field/extract_bit_field when trying | |
2498 | to access a non-existent part of the register. */ | |
2499 | if (TREE_CODE (index) == INTEGER_CST | |
eb34af89 | 2500 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3f75a254 | 2501 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) |
e6d52559 | 2502 | { |
dffd7eb6 | 2503 | if (!c_mark_addressable (array)) |
e6d52559 JW |
2504 | return error_mark_node; |
2505 | } | |
400fbf9f | 2506 | |
400fbf9f JW |
2507 | if (pedantic) |
2508 | { | |
2509 | tree foo = array; | |
2510 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2511 | foo = TREE_OPERAND (foo, 0); | |
5baeaac0 | 2512 | if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) |
c1771a20 | 2513 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 2514 | "ISO C forbids subscripting %<register%> array"); |
3f75a254 | 2515 | else if (!flag_isoc99 && !lvalue_p (foo)) |
c1771a20 | 2516 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 2517 | "ISO C90 forbids subscripting non-lvalue array"); |
400fbf9f JW |
2518 | } |
2519 | ||
46df2823 | 2520 | type = TREE_TYPE (TREE_TYPE (array)); |
53fb4de3 | 2521 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
400fbf9f | 2522 | /* Array ref is const/volatile if the array elements are |
c22cacf3 | 2523 | or if the array is. */ |
400fbf9f JW |
2524 | TREE_READONLY (rval) |
2525 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2526 | | TREE_READONLY (array)); | |
2527 | TREE_SIDE_EFFECTS (rval) | |
2528 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2529 | | TREE_SIDE_EFFECTS (array)); | |
2530 | TREE_THIS_VOLATILE (rval) | |
2531 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2532 | /* This was added by rms on 16 Nov 91. | |
2f6e4e97 | 2533 | It fixes vol struct foo *a; a->elts[1] |
400fbf9f JW |
2534 | in an inline function. |
2535 | Hope it doesn't break something else. */ | |
2536 | | TREE_THIS_VOLATILE (array)); | |
928c19bb | 2537 | ret = require_complete_type (rval); |
6a3799eb AH |
2538 | protected_set_expr_location (ret, loc); |
2539 | return ret; | |
400fbf9f | 2540 | } |
a4ab7973 JM |
2541 | else |
2542 | { | |
2543 | tree ar = default_conversion (array); | |
400fbf9f | 2544 | |
a4ab7973 JM |
2545 | if (ar == error_mark_node) |
2546 | return ar; | |
400fbf9f | 2547 | |
a4ab7973 JM |
2548 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); |
2549 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
400fbf9f | 2550 | |
ba47d38d | 2551 | return build_indirect_ref |
c9f9eb5d | 2552 | (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0), |
dd865ef6 | 2553 | RO_ARRAY_INDEXING); |
a4ab7973 | 2554 | } |
400fbf9f JW |
2555 | } |
2556 | \f | |
7e585d16 | 2557 | /* Build an external reference to identifier ID. FUN indicates |
766beb40 | 2558 | whether this will be used for a function call. LOC is the source |
6866c6e8 ILT |
2559 | location of the identifier. This sets *TYPE to the type of the |
2560 | identifier, which is not the same as the type of the returned value | |
2561 | for CONST_DECLs defined as enum constants. If the type of the | |
2562 | identifier is not available, *TYPE is set to NULL. */ | |
7e585d16 | 2563 | tree |
c2255bc4 | 2564 | build_external_ref (location_t loc, tree id, int fun, tree *type) |
7e585d16 ZW |
2565 | { |
2566 | tree ref; | |
2567 | tree decl = lookup_name (id); | |
16b34ad6 ZL |
2568 | |
2569 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2570 | whatever lookup_name() found. */ | |
2571 | decl = objc_lookup_ivar (decl, id); | |
7e585d16 | 2572 | |
6866c6e8 | 2573 | *type = NULL; |
339a28b9 | 2574 | if (decl && decl != error_mark_node) |
6866c6e8 ILT |
2575 | { |
2576 | ref = decl; | |
2577 | *type = TREE_TYPE (ref); | |
2578 | } | |
339a28b9 ZW |
2579 | else if (fun) |
2580 | /* Implicit function declaration. */ | |
c2255bc4 | 2581 | ref = implicitly_declare (loc, id); |
339a28b9 ZW |
2582 | else if (decl == error_mark_node) |
2583 | /* Don't complain about something that's already been | |
2584 | complained about. */ | |
2585 | return error_mark_node; | |
2586 | else | |
2587 | { | |
c2255bc4 | 2588 | undeclared_variable (loc, id); |
339a28b9 ZW |
2589 | return error_mark_node; |
2590 | } | |
7e585d16 ZW |
2591 | |
2592 | if (TREE_TYPE (ref) == error_mark_node) | |
2593 | return error_mark_node; | |
2594 | ||
339a28b9 | 2595 | if (TREE_DEPRECATED (ref)) |
9b86d6bb | 2596 | warn_deprecated_use (ref, NULL_TREE); |
339a28b9 | 2597 | |
ad960f56 | 2598 | /* Recursive call does not count as usage. */ |
b8698a0f | 2599 | if (ref != current_function_decl) |
ad960f56 | 2600 | { |
ad960f56 MLI |
2601 | TREE_USED (ref) = 1; |
2602 | } | |
7e585d16 | 2603 | |
bc4b653b JM |
2604 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) |
2605 | { | |
2606 | if (!in_sizeof && !in_typeof) | |
2607 | C_DECL_USED (ref) = 1; | |
2608 | else if (DECL_INITIAL (ref) == 0 | |
2609 | && DECL_EXTERNAL (ref) | |
2610 | && !TREE_PUBLIC (ref)) | |
2611 | record_maybe_used_decl (ref); | |
2612 | } | |
2613 | ||
7e585d16 ZW |
2614 | if (TREE_CODE (ref) == CONST_DECL) |
2615 | { | |
6193b8b7 | 2616 | used_types_insert (TREE_TYPE (ref)); |
24b97832 ILT |
2617 | |
2618 | if (warn_cxx_compat | |
2619 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2620 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2621 | { | |
2622 | warning_at (loc, OPT_Wc___compat, | |
2623 | ("enum constant defined in struct or union " | |
2624 | "is not visible in C++")); | |
2625 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2626 | } | |
2627 | ||
7e585d16 ZW |
2628 | ref = DECL_INITIAL (ref); |
2629 | TREE_CONSTANT (ref) = 1; | |
2630 | } | |
6a29edea | 2631 | else if (current_function_decl != 0 |
4b1e44be | 2632 | && !DECL_FILE_SCOPE_P (current_function_decl) |
6a29edea EB |
2633 | && (TREE_CODE (ref) == VAR_DECL |
2634 | || TREE_CODE (ref) == PARM_DECL | |
2635 | || TREE_CODE (ref) == FUNCTION_DECL)) | |
2636 | { | |
2637 | tree context = decl_function_context (ref); | |
2f6e4e97 | 2638 | |
6a29edea EB |
2639 | if (context != 0 && context != current_function_decl) |
2640 | DECL_NONLOCAL (ref) = 1; | |
2641 | } | |
71113fcd GK |
2642 | /* C99 6.7.4p3: An inline definition of a function with external |
2643 | linkage ... shall not contain a reference to an identifier with | |
2644 | internal linkage. */ | |
2645 | else if (current_function_decl != 0 | |
2646 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2647 | && DECL_EXTERNAL (current_function_decl) | |
2648 | && VAR_OR_FUNCTION_DECL_P (ref) | |
2649 | && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref)) | |
1033ffa8 JJ |
2650 | && ! TREE_PUBLIC (ref) |
2651 | && DECL_CONTEXT (ref) != current_function_decl) | |
991d6621 JM |
2652 | record_inline_static (loc, current_function_decl, ref, |
2653 | csi_internal); | |
7e585d16 ZW |
2654 | |
2655 | return ref; | |
2656 | } | |
2657 | ||
bc4b653b JM |
2658 | /* Record details of decls possibly used inside sizeof or typeof. */ |
2659 | struct maybe_used_decl | |
2660 | { | |
2661 | /* The decl. */ | |
2662 | tree decl; | |
2663 | /* The level seen at (in_sizeof + in_typeof). */ | |
2664 | int level; | |
2665 | /* The next one at this level or above, or NULL. */ | |
2666 | struct maybe_used_decl *next; | |
2667 | }; | |
2668 | ||
2669 | static struct maybe_used_decl *maybe_used_decls; | |
2670 | ||
2671 | /* Record that DECL, an undefined static function reference seen | |
2672 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2673 | a VLA type or the operand of typeof is a variably modified | |
2674 | type. */ | |
2675 | ||
4e2fb7de | 2676 | static void |
bc4b653b JM |
2677 | record_maybe_used_decl (tree decl) |
2678 | { | |
2679 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2680 | t->decl = decl; | |
2681 | t->level = in_sizeof + in_typeof; | |
2682 | t->next = maybe_used_decls; | |
2683 | maybe_used_decls = t; | |
2684 | } | |
2685 | ||
2686 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2687 | USED is false, just discard them. If it is true, mark them used | |
2688 | (if no longer inside sizeof or typeof) or move them to the next | |
2689 | level up (if still inside sizeof or typeof). */ | |
2690 | ||
2691 | void | |
2692 | pop_maybe_used (bool used) | |
2693 | { | |
2694 | struct maybe_used_decl *p = maybe_used_decls; | |
2695 | int cur_level = in_sizeof + in_typeof; | |
2696 | while (p && p->level > cur_level) | |
2697 | { | |
2698 | if (used) | |
2699 | { | |
2700 | if (cur_level == 0) | |
2701 | C_DECL_USED (p->decl) = 1; | |
2702 | else | |
2703 | p->level = cur_level; | |
2704 | } | |
2705 | p = p->next; | |
2706 | } | |
2707 | if (!used || cur_level == 0) | |
2708 | maybe_used_decls = p; | |
2709 | } | |
2710 | ||
2711 | /* Return the result of sizeof applied to EXPR. */ | |
2712 | ||
2713 | struct c_expr | |
c2255bc4 | 2714 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) |
bc4b653b JM |
2715 | { |
2716 | struct c_expr ret; | |
ad97f4be JM |
2717 | if (expr.value == error_mark_node) |
2718 | { | |
2719 | ret.value = error_mark_node; | |
2720 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 2721 | ret.original_type = NULL; |
ad97f4be JM |
2722 | pop_maybe_used (false); |
2723 | } | |
2724 | else | |
2725 | { | |
928c19bb JM |
2726 | bool expr_const_operands = true; |
2727 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, | |
2728 | &expr_const_operands); | |
c2255bc4 | 2729 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); |
1a4049e7 JJ |
2730 | c_last_sizeof_arg = expr.value; |
2731 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2732 | ret.original_type = NULL; |
928c19bb | 2733 | if (c_vla_type_p (TREE_TYPE (folded_expr))) |
52ffd86e MS |
2734 | { |
2735 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
928c19bb JM |
2736 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2737 | folded_expr, ret.value); | |
2738 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
c2255bc4 | 2739 | SET_EXPR_LOCATION (ret.value, loc); |
52ffd86e | 2740 | } |
928c19bb | 2741 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); |
ad97f4be | 2742 | } |
bc4b653b JM |
2743 | return ret; |
2744 | } | |
2745 | ||
2746 | /* Return the result of sizeof applied to T, a structure for the type | |
c2255bc4 AH |
2747 | name passed to sizeof (rather than the type itself). LOC is the |
2748 | location of the original expression. */ | |
bc4b653b JM |
2749 | |
2750 | struct c_expr | |
c2255bc4 | 2751 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) |
bc4b653b JM |
2752 | { |
2753 | tree type; | |
2754 | struct c_expr ret; | |
928c19bb JM |
2755 | tree type_expr = NULL_TREE; |
2756 | bool type_expr_const = true; | |
2757 | type = groktypename (t, &type_expr, &type_expr_const); | |
c2255bc4 | 2758 | ret.value = c_sizeof (loc, type); |
1a4049e7 JJ |
2759 | c_last_sizeof_arg = type; |
2760 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2761 | ret.original_type = NULL; |
24070fcb JM |
2762 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) |
2763 | && c_vla_type_p (type)) | |
928c19bb | 2764 | { |
24070fcb JM |
2765 | /* If the type is a [*] array, it is a VLA but is represented as |
2766 | having a size of zero. In such a case we must ensure that | |
2767 | the result of sizeof does not get folded to a constant by | |
2768 | c_fully_fold, because if the size is evaluated the result is | |
2769 | not constant and so constraints on zero or negative size | |
2770 | arrays must not be applied when this sizeof call is inside | |
2771 | another array declarator. */ | |
2772 | if (!type_expr) | |
2773 | type_expr = integer_zero_node; | |
928c19bb JM |
2774 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2775 | type_expr, ret.value); | |
2776 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2777 | } | |
16464cc1 VR |
2778 | pop_maybe_used (type != error_mark_node |
2779 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
bc4b653b JM |
2780 | return ret; |
2781 | } | |
2782 | ||
400fbf9f | 2783 | /* Build a function call to function FUNCTION with parameters PARAMS. |
c2255bc4 | 2784 | The function call is at LOC. |
400fbf9f JW |
2785 | PARAMS is a list--a chain of TREE_LIST nodes--in which the |
2786 | TREE_VALUE of each node is a parameter-expression. | |
2787 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2788 | ||
2789 | tree | |
c2255bc4 | 2790 | build_function_call (location_t loc, tree function, tree params) |
bbbbb16a | 2791 | { |
9771b263 | 2792 | vec<tree, va_gc> *v; |
bbbbb16a ILT |
2793 | tree ret; |
2794 | ||
9771b263 | 2795 | vec_alloc (v, list_length (params)); |
bbbbb16a | 2796 | for (; params; params = TREE_CHAIN (params)) |
9771b263 DN |
2797 | v->quick_push (TREE_VALUE (params)); |
2798 | ret = build_function_call_vec (loc, function, v, NULL); | |
2799 | vec_free (v); | |
bbbbb16a ILT |
2800 | return ret; |
2801 | } | |
2802 | ||
ae52741c MLI |
2803 | /* Give a note about the location of the declaration of DECL. */ |
2804 | ||
2805 | static void inform_declaration (tree decl) | |
2806 | { | |
2807 | if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl))) | |
2808 | inform (DECL_SOURCE_LOCATION (decl), "declared here"); | |
2809 | } | |
2810 | ||
bbbbb16a ILT |
2811 | /* Build a function call to function FUNCTION with parameters PARAMS. |
2812 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2813 | either NULL or the original type of the corresponding element in | |
2814 | PARAMS. The original type may differ from TREE_TYPE of the | |
2815 | parameter for enums. FUNCTION's data type may be a function type | |
2816 | or pointer-to-function. This function changes the elements of | |
2817 | PARAMS. */ | |
2818 | ||
2819 | tree | |
9771b263 DN |
2820 | build_function_call_vec (location_t loc, tree function, |
2821 | vec<tree, va_gc> *params, | |
2822 | vec<tree, va_gc> *origtypes) | |
400fbf9f | 2823 | { |
b3694847 | 2824 | tree fntype, fundecl = 0; |
4977bab6 | 2825 | tree name = NULL_TREE, result; |
c96f4f73 | 2826 | tree tem; |
94a0dd7b SL |
2827 | int nargs; |
2828 | tree *argarray; | |
b8698a0f | 2829 | |
400fbf9f | 2830 | |
fc76e425 | 2831 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 2832 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
2833 | |
2834 | /* Convert anything with function type to a pointer-to-function. */ | |
2835 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2836 | { | |
58646b77 PB |
2837 | /* Implement type-directed function overloading for builtins. |
2838 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
2839 | handle all the type checking. The result is a complete expression | |
2840 | that implements this function call. */ | |
c2255bc4 | 2841 | tem = resolve_overloaded_builtin (loc, function, params); |
58646b77 PB |
2842 | if (tem) |
2843 | return tem; | |
48ae6c13 | 2844 | |
400fbf9f | 2845 | name = DECL_NAME (function); |
0a35513e AH |
2846 | |
2847 | if (flag_tm) | |
2848 | tm_malloc_replacement (function); | |
a5eadacc | 2849 | fundecl = function; |
86951993 AM |
2850 | /* Atomic functions have type checking/casting already done. They are |
2851 | often rewritten and don't match the original parameter list. */ | |
2852 | if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) | |
2853 | origtypes = NULL; | |
36536d79 BI |
2854 | |
2855 | if (flag_enable_cilkplus | |
2856 | && is_cilkplus_reduce_builtin (function)) | |
2857 | origtypes = NULL; | |
400fbf9f | 2858 | } |
f2a71bbc | 2859 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) |
c2255bc4 | 2860 | function = function_to_pointer_conversion (loc, function); |
400fbf9f | 2861 | |
6e955430 ZL |
2862 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2863 | expressions, like those used for ObjC messenger dispatches. */ | |
9771b263 DN |
2864 | if (params && !params->is_empty ()) |
2865 | function = objc_rewrite_function_call (function, (*params)[0]); | |
6e955430 | 2866 | |
928c19bb JM |
2867 | function = c_fully_fold (function, false, NULL); |
2868 | ||
400fbf9f JW |
2869 | fntype = TREE_TYPE (function); |
2870 | ||
2871 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2872 | return error_mark_node; | |
2873 | ||
2874 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2875 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2876 | { | |
ae52741c MLI |
2877 | if (!flag_diagnostics_show_caret) |
2878 | error_at (loc, | |
2879 | "called object %qE is not a function or function pointer", | |
2880 | function); | |
2881 | else if (DECL_P (function)) | |
2882 | { | |
2883 | error_at (loc, | |
2884 | "called object %qD is not a function or function pointer", | |
2885 | function); | |
2886 | inform_declaration (function); | |
2887 | } | |
2888 | else | |
2889 | error_at (loc, | |
2890 | "called object is not a function or function pointer"); | |
400fbf9f JW |
2891 | return error_mark_node; |
2892 | } | |
2893 | ||
5ce89b2e JM |
2894 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
2895 | current_function_returns_abnormally = 1; | |
2896 | ||
400fbf9f JW |
2897 | /* fntype now gets the type of function pointed to. */ |
2898 | fntype = TREE_TYPE (fntype); | |
2899 | ||
ab4194da JM |
2900 | /* Convert the parameters to the types declared in the |
2901 | function prototype, or apply default promotions. */ | |
2902 | ||
bbbbb16a ILT |
2903 | nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes, |
2904 | function, fundecl); | |
ab4194da JM |
2905 | if (nargs < 0) |
2906 | return error_mark_node; | |
2907 | ||
c96f4f73 EB |
2908 | /* Check that the function is called through a compatible prototype. |
2909 | If it is not, replace the call by a trap, wrapped up in a compound | |
2910 | expression if necessary. This has the nice side-effect to prevent | |
2911 | the tree-inliner from generating invalid assignment trees which may | |
58393038 | 2912 | blow up in the RTL expander later. */ |
1043771b | 2913 | if (CONVERT_EXPR_P (function) |
c96f4f73 EB |
2914 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR |
2915 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
3f75a254 | 2916 | && !comptypes (fntype, TREE_TYPE (tem))) |
c96f4f73 EB |
2917 | { |
2918 | tree return_type = TREE_TYPE (fntype); | |
e79983f4 MM |
2919 | tree trap = build_function_call (loc, |
2920 | builtin_decl_explicit (BUILT_IN_TRAP), | |
c96f4f73 | 2921 | NULL_TREE); |
ab4194da | 2922 | int i; |
c96f4f73 EB |
2923 | |
2924 | /* This situation leads to run-time undefined behavior. We can't, | |
2925 | therefore, simply error unless we can prove that all possible | |
2926 | executions of the program must execute the code. */ | |
c2255bc4 | 2927 | if (warning_at (loc, 0, "function called through a non-compatible type")) |
71205d17 MLI |
2928 | /* We can, however, treat "undefined" any way we please. |
2929 | Call abort to encourage the user to fix the program. */ | |
c2255bc4 | 2930 | inform (loc, "if this code is reached, the program will abort"); |
ab4194da JM |
2931 | /* Before the abort, allow the function arguments to exit or |
2932 | call longjmp. */ | |
2933 | for (i = 0; i < nargs; i++) | |
9771b263 | 2934 | trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap); |
bba745c1 | 2935 | |
c96f4f73 | 2936 | if (VOID_TYPE_P (return_type)) |
3ce62965 JM |
2937 | { |
2938 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
db3927fb | 2939 | pedwarn (loc, 0, |
3ce62965 JM |
2940 | "function with qualified void return type called"); |
2941 | return trap; | |
2942 | } | |
c96f4f73 EB |
2943 | else |
2944 | { | |
2945 | tree rhs; | |
2946 | ||
2947 | if (AGGREGATE_TYPE_P (return_type)) | |
c2255bc4 | 2948 | rhs = build_compound_literal (loc, return_type, |
9771b263 DN |
2949 | build_constructor (return_type, |
2950 | NULL), | |
928c19bb | 2951 | false); |
c96f4f73 | 2952 | else |
e8160c9a | 2953 | rhs = build_zero_cst (return_type); |
c96f4f73 | 2954 | |
3ce62965 JM |
2955 | return require_complete_type (build2 (COMPOUND_EXPR, return_type, |
2956 | trap, rhs)); | |
c96f4f73 EB |
2957 | } |
2958 | } | |
2959 | ||
9771b263 | 2960 | argarray = vec_safe_address (params); |
bbbbb16a | 2961 | |
83322951 RG |
2962 | /* Check that arguments to builtin functions match the expectations. */ |
2963 | if (fundecl | |
2964 | && DECL_BUILT_IN (fundecl) | |
2965 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2966 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2967 | return error_mark_node; | |
400fbf9f | 2968 | |
83322951 | 2969 | /* Check that the arguments to the function are valid. */ |
dde05067 | 2970 | check_function_arguments (fntype, nargs, argarray); |
400fbf9f | 2971 | |
928c19bb JM |
2972 | if (name != NULL_TREE |
2973 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
bf730f15 | 2974 | { |
928c19bb | 2975 | if (require_constant_value) |
b8698a0f | 2976 | result = |
db3927fb AH |
2977 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), |
2978 | function, nargs, argarray); | |
928c19bb | 2979 | else |
db3927fb AH |
2980 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), |
2981 | function, nargs, argarray); | |
928c19bb JM |
2982 | if (TREE_CODE (result) == NOP_EXPR |
2983 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2984 | STRIP_TYPE_NOPS (result); | |
bf730f15 RS |
2985 | } |
2986 | else | |
db3927fb AH |
2987 | result = build_call_array_loc (loc, TREE_TYPE (fntype), |
2988 | function, nargs, argarray); | |
b0b3afb2 | 2989 | |
71653180 | 2990 | if (VOID_TYPE_P (TREE_TYPE (result))) |
3ce62965 JM |
2991 | { |
2992 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
db3927fb | 2993 | pedwarn (loc, 0, |
3ce62965 JM |
2994 | "function with qualified void return type called"); |
2995 | return result; | |
2996 | } | |
1eb8759b | 2997 | return require_complete_type (result); |
400fbf9f JW |
2998 | } |
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 | ||
94a0dd7b | 3015 | Returns the actual number of arguments processed (which may be less |
bbbbb16a ILT |
3016 | than the length of VALUES in some error situations), or -1 on |
3017 | failure. */ | |
94a0dd7b SL |
3018 | |
3019 | static int | |
9771b263 DN |
3020 | convert_arguments (tree typelist, vec<tree, va_gc> *values, |
3021 | vec<tree, va_gc> *origtypes, tree function, tree fundecl) | |
400fbf9f | 3022 | { |
bbbbb16a ILT |
3023 | tree typetail, val; |
3024 | unsigned int parmnum; | |
06302a02 | 3025 | bool error_args = false; |
b5d32c25 KG |
3026 | const bool type_generic = fundecl |
3027 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl))); | |
8ce94e44 | 3028 | bool type_generic_remove_excess_precision = false; |
03dafa61 | 3029 | tree selector; |
03dafa61 | 3030 | |
2ac2f164 JM |
3031 | /* Change pointer to function to the function itself for |
3032 | diagnostics. */ | |
03dafa61 JM |
3033 | if (TREE_CODE (function) == ADDR_EXPR |
3034 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
2ac2f164 | 3035 | function = TREE_OPERAND (function, 0); |
03dafa61 JM |
3036 | |
3037 | /* Handle an ObjC selector specially for diagnostics. */ | |
3038 | selector = objc_message_selector (); | |
400fbf9f | 3039 | |
8ce94e44 JM |
3040 | /* For type-generic built-in functions, determine whether excess |
3041 | precision should be removed (classification) or not | |
3042 | (comparison). */ | |
3043 | if (type_generic | |
3044 | && DECL_BUILT_IN (fundecl) | |
3045 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
3046 | { | |
3047 | switch (DECL_FUNCTION_CODE (fundecl)) | |
3048 | { | |
3049 | case BUILT_IN_ISFINITE: | |
3050 | case BUILT_IN_ISINF: | |
3051 | case BUILT_IN_ISINF_SIGN: | |
3052 | case BUILT_IN_ISNAN: | |
3053 | case BUILT_IN_ISNORMAL: | |
3054 | case BUILT_IN_FPCLASSIFY: | |
3055 | type_generic_remove_excess_precision = true; | |
3056 | break; | |
3057 | ||
3058 | default: | |
3059 | type_generic_remove_excess_precision = false; | |
3060 | break; | |
3061 | } | |
3062 | } | |
6d6efbb3 BI |
3063 | if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl)) |
3064 | return vec_safe_length (values); | |
8ce94e44 | 3065 | |
400fbf9f | 3066 | /* Scan the given expressions and types, producing individual |
bbbbb16a | 3067 | converted arguments. */ |
400fbf9f | 3068 | |
bbbbb16a | 3069 | for (typetail = typelist, parmnum = 0; |
9771b263 | 3070 | values && values->iterate (parmnum, &val); |
bbbbb16a | 3071 | ++parmnum) |
400fbf9f | 3072 | { |
b3694847 | 3073 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
8ce94e44 | 3074 | tree valtype = TREE_TYPE (val); |
03dafa61 JM |
3075 | tree rname = function; |
3076 | int argnum = parmnum + 1; | |
4d3e6fae | 3077 | const char *invalid_func_diag; |
8ce94e44 | 3078 | bool excess_precision = false; |
928c19bb | 3079 | bool npc; |
bbbbb16a | 3080 | tree parmval; |
400fbf9f JW |
3081 | |
3082 | if (type == void_type_node) | |
3083 | { | |
19dc6d01 NP |
3084 | if (selector) |
3085 | error_at (input_location, | |
3086 | "too many arguments to method %qE", selector); | |
3087 | else | |
3088 | error_at (input_location, | |
3089 | "too many arguments to function %qE", function); | |
ae52741c | 3090 | inform_declaration (fundecl); |
94a0dd7b | 3091 | return parmnum; |
400fbf9f JW |
3092 | } |
3093 | ||
03dafa61 JM |
3094 | if (selector && argnum > 2) |
3095 | { | |
3096 | rname = selector; | |
3097 | argnum -= 2; | |
3098 | } | |
3099 | ||
928c19bb | 3100 | npc = null_pointer_constant_p (val); |
8ce94e44 JM |
3101 | |
3102 | /* If there is excess precision and a prototype, convert once to | |
3103 | the required type rather than converting via the semantic | |
3104 | type. Likewise without a prototype a float value represented | |
3105 | as long double should be converted once to double. But for | |
3106 | type-generic classification functions excess precision must | |
3107 | be removed here. */ | |
3108 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
3109 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
3110 | { | |
3111 | val = TREE_OPERAND (val, 0); | |
3112 | excess_precision = true; | |
3113 | } | |
928c19bb | 3114 | val = c_fully_fold (val, false, NULL); |
ed248cf7 | 3115 | STRIP_TYPE_NOPS (val); |
400fbf9f | 3116 | |
400fbf9f JW |
3117 | val = require_complete_type (val); |
3118 | ||
3119 | if (type != 0) | |
3120 | { | |
3121 | /* Formal parm type is specified by a function prototype. */ | |
400fbf9f | 3122 | |
20913689 | 3123 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) |
400fbf9f JW |
3124 | { |
3125 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
3126 | parmval = val; | |
3127 | } | |
3128 | else | |
3129 | { | |
bbbbb16a ILT |
3130 | tree origtype; |
3131 | ||
d45cf215 RS |
3132 | /* Optionally warn about conversions that |
3133 | differ from the default conversions. */ | |
05170031 | 3134 | if (warn_traditional_conversion || warn_traditional) |
400fbf9f | 3135 | { |
e3a64162 | 3136 | unsigned int formal_prec = TYPE_PRECISION (type); |
400fbf9f | 3137 | |
aae43c5f | 3138 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3139 | && TREE_CODE (valtype) == REAL_TYPE) |
d4ee4d25 | 3140 | warning (0, "passing argument %d of %qE as integer " |
03dafa61 JM |
3141 | "rather than floating due to prototype", |
3142 | argnum, rname); | |
03829ad2 | 3143 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3144 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
d4ee4d25 | 3145 | warning (0, "passing argument %d of %qE as integer " |
03dafa61 JM |
3146 | "rather than complex due to prototype", |
3147 | argnum, rname); | |
aae43c5f | 3148 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3149 | && TREE_CODE (valtype) == REAL_TYPE) |
d4ee4d25 | 3150 | warning (0, "passing argument %d of %qE as complex " |
03dafa61 JM |
3151 | "rather than floating due to prototype", |
3152 | argnum, rname); | |
400fbf9f | 3153 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3154 | && INTEGRAL_TYPE_P (valtype)) |
d4ee4d25 | 3155 | warning (0, "passing argument %d of %qE as floating " |
03dafa61 JM |
3156 | "rather than integer due to prototype", |
3157 | argnum, rname); | |
03829ad2 | 3158 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3159 | && INTEGRAL_TYPE_P (valtype)) |
d4ee4d25 | 3160 | warning (0, "passing argument %d of %qE as complex " |
03dafa61 JM |
3161 | "rather than integer due to prototype", |
3162 | argnum, rname); | |
aae43c5f | 3163 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3164 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
d4ee4d25 | 3165 | warning (0, "passing argument %d of %qE as floating " |
03dafa61 JM |
3166 | "rather than complex due to prototype", |
3167 | argnum, rname); | |
aae43c5f RK |
3168 | /* ??? At some point, messages should be written about |
3169 | conversions between complex types, but that's too messy | |
3170 | to do now. */ | |
d45cf215 | 3171 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3172 | && TREE_CODE (valtype) == REAL_TYPE) |
d45cf215 RS |
3173 | { |
3174 | /* Warn if any argument is passed as `float', | |
047de90b | 3175 | since without a prototype it would be `double'. */ |
9a8ce21f JG |
3176 | if (formal_prec == TYPE_PRECISION (float_type_node) |
3177 | && type != dfloat32_type_node) | |
d4ee4d25 | 3178 | warning (0, "passing argument %d of %qE as %<float%> " |
03dafa61 JM |
3179 | "rather than %<double%> due to prototype", |
3180 | argnum, rname); | |
9a8ce21f JG |
3181 | |
3182 | /* Warn if mismatch between argument and prototype | |
3183 | for decimal float types. Warn of conversions with | |
3184 | binary float types and of precision narrowing due to | |
3185 | prototype. */ | |
8ce94e44 | 3186 | else if (type != valtype |
9a8ce21f JG |
3187 | && (type == dfloat32_type_node |
3188 | || type == dfloat64_type_node | |
c22cacf3 | 3189 | || type == dfloat128_type_node |
8ce94e44 JM |
3190 | || valtype == dfloat32_type_node |
3191 | || valtype == dfloat64_type_node | |
3192 | || valtype == dfloat128_type_node) | |
c22cacf3 | 3193 | && (formal_prec |
8ce94e44 | 3194 | <= TYPE_PRECISION (valtype) |
9a8ce21f | 3195 | || (type == dfloat128_type_node |
8ce94e44 | 3196 | && (valtype |
c22cacf3 | 3197 | != dfloat64_type_node |
8ce94e44 | 3198 | && (valtype |
9a8ce21f JG |
3199 | != dfloat32_type_node))) |
3200 | || (type == dfloat64_type_node | |
8ce94e44 | 3201 | && (valtype |
9a8ce21f JG |
3202 | != dfloat32_type_node)))) |
3203 | warning (0, "passing argument %d of %qE as %qT " | |
3204 | "rather than %qT due to prototype", | |
8ce94e44 | 3205 | argnum, rname, type, valtype); |
9a8ce21f | 3206 | |
d45cf215 | 3207 | } |
3ed56f8a KG |
3208 | /* Detect integer changing in width or signedness. |
3209 | These warnings are only activated with | |
05170031 MLI |
3210 | -Wtraditional-conversion, not with -Wtraditional. */ |
3211 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
8ce94e44 | 3212 | && INTEGRAL_TYPE_P (valtype)) |
400fbf9f | 3213 | { |
d45cf215 RS |
3214 | tree would_have_been = default_conversion (val); |
3215 | tree type1 = TREE_TYPE (would_have_been); | |
3216 | ||
754a4d82 | 3217 | if (TREE_CODE (type) == ENUMERAL_TYPE |
a38b987a | 3218 | && (TYPE_MAIN_VARIANT (type) |
8ce94e44 | 3219 | == TYPE_MAIN_VARIANT (valtype))) |
754a4d82 RS |
3220 | /* No warning if function asks for enum |
3221 | and the actual arg is that enum type. */ | |
3222 | ; | |
3223 | else if (formal_prec != TYPE_PRECISION (type1)) | |
c2255bc4 AH |
3224 | warning (OPT_Wtraditional_conversion, |
3225 | "passing argument %d of %qE " | |
3176a0c2 DD |
3226 | "with different width due to prototype", |
3227 | argnum, rname); | |
8df83eae | 3228 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
d45cf215 | 3229 | ; |
800cd3b9 RS |
3230 | /* Don't complain if the formal parameter type |
3231 | is an enum, because we can't tell now whether | |
3232 | the value was an enum--even the same enum. */ | |
3233 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
3234 | ; | |
400fbf9f JW |
3235 | else if (TREE_CODE (val) == INTEGER_CST |
3236 | && int_fits_type_p (val, type)) | |
3237 | /* Change in signedness doesn't matter | |
3238 | if a constant value is unaffected. */ | |
3239 | ; | |
ce9895ae RS |
3240 | /* If the value is extended from a narrower |
3241 | unsigned type, it doesn't matter whether we | |
3242 | pass it as signed or unsigned; the value | |
3243 | certainly is the same either way. */ | |
8ce94e44 JM |
3244 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) |
3245 | && TYPE_UNSIGNED (valtype)) | |
ce9895ae | 3246 | ; |
8df83eae | 3247 | else if (TYPE_UNSIGNED (type)) |
c2255bc4 AH |
3248 | warning (OPT_Wtraditional_conversion, |
3249 | "passing argument %d of %qE " | |
3176a0c2 DD |
3250 | "as unsigned due to prototype", |
3251 | argnum, rname); | |
3ed56f8a | 3252 | else |
c2255bc4 AH |
3253 | warning (OPT_Wtraditional_conversion, |
3254 | "passing argument %d of %qE " | |
3176a0c2 | 3255 | "as signed due to prototype", argnum, rname); |
400fbf9f JW |
3256 | } |
3257 | } | |
3258 | ||
8ce94e44 JM |
3259 | /* Possibly restore an EXCESS_PRECISION_EXPR for the |
3260 | sake of better warnings from convert_and_check. */ | |
3261 | if (excess_precision) | |
3262 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
9771b263 | 3263 | origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; |
744aa42f ILT |
3264 | parmval = convert_for_assignment (input_location, type, val, |
3265 | origtype, ic_argpass, npc, | |
2ac2f164 JM |
3266 | fundecl, function, |
3267 | parmnum + 1); | |
2f6e4e97 | 3268 | |
61f71b34 | 3269 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
b6d6aa84 | 3270 | && INTEGRAL_TYPE_P (type) |
400fbf9f JW |
3271 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
3272 | parmval = default_conversion (parmval); | |
400fbf9f | 3273 | } |
400fbf9f | 3274 | } |
8ce94e44 JM |
3275 | else if (TREE_CODE (valtype) == REAL_TYPE |
3276 | && (TYPE_PRECISION (valtype) | |
2531a1d9 | 3277 | <= TYPE_PRECISION (double_type_node)) |
0fdd4508 JR |
3278 | && TYPE_MAIN_VARIANT (valtype) != double_type_node |
3279 | && TYPE_MAIN_VARIANT (valtype) != long_double_type_node | |
8ce94e44 | 3280 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) |
b5d32c25 KG |
3281 | { |
3282 | if (type_generic) | |
bbbbb16a | 3283 | parmval = val; |
b5d32c25 | 3284 | else |
0a0b3574 MM |
3285 | { |
3286 | /* Convert `float' to `double'. */ | |
3287 | if (warn_double_promotion && !c_inhibit_evaluation_warnings) | |
3288 | warning (OPT_Wdouble_promotion, | |
3289 | "implicit conversion from %qT to %qT when passing " | |
3290 | "argument to function", | |
3291 | valtype, double_type_node); | |
3292 | parmval = convert (double_type_node, val); | |
3293 | } | |
b5d32c25 | 3294 | } |
8ce94e44 JM |
3295 | else if (excess_precision && !type_generic) |
3296 | /* A "double" argument with excess precision being passed | |
3297 | without a prototype or in variable arguments. */ | |
bbbbb16a | 3298 | parmval = convert (valtype, val); |
c22cacf3 MS |
3299 | else if ((invalid_func_diag = |
3300 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
4d3e6fae FJ |
3301 | { |
3302 | error (invalid_func_diag); | |
94a0dd7b | 3303 | return -1; |
4d3e6fae | 3304 | } |
400fbf9f JW |
3305 | else |
3306 | /* Convert `short' and `char' to full-size `int'. */ | |
bbbbb16a ILT |
3307 | parmval = default_conversion (val); |
3308 | ||
9771b263 | 3309 | (*values)[parmnum] = parmval; |
06302a02 JJ |
3310 | if (parmval == error_mark_node) |
3311 | error_args = true; | |
400fbf9f JW |
3312 | |
3313 | if (typetail) | |
3314 | typetail = TREE_CHAIN (typetail); | |
3315 | } | |
3316 | ||
9771b263 | 3317 | gcc_assert (parmnum == vec_safe_length (values)); |
94a0dd7b | 3318 | |
400fbf9f | 3319 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) |
3789b316 | 3320 | { |
6d6efbb3 BI |
3321 | error_at (input_location, |
3322 | "too few arguments to function %qE", function); | |
3323 | inform_declaration (fundecl); | |
3324 | return -1; | |
3789b316 | 3325 | } |
400fbf9f | 3326 | |
06302a02 | 3327 | return error_args ? -1 : (int) parmnum; |
400fbf9f JW |
3328 | } |
3329 | \f | |
43f6dfd3 RS |
3330 | /* This is the entry point used by the parser to build unary operators |
3331 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3332 | ARG is the operand. For unary plus, the C parser currently uses | |
6a3799eb AH |
3333 | CONVERT_EXPR for code. |
3334 | ||
3335 | LOC is the location to use for the tree generated. | |
3336 | */ | |
43f6dfd3 RS |
3337 | |
3338 | struct c_expr | |
c2255bc4 | 3339 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) |
43f6dfd3 RS |
3340 | { |
3341 | struct c_expr result; | |
3342 | ||
c9f9eb5d | 3343 | result.value = build_unary_op (loc, code, arg.value, 0); |
100d537d | 3344 | result.original_code = code; |
6866c6e8 ILT |
3345 | result.original_type = NULL; |
3346 | ||
59c0753d | 3347 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) |
c2255bc4 | 3348 | overflow_warning (loc, result.value); |
59c0753d | 3349 | |
43f6dfd3 RS |
3350 | return result; |
3351 | } | |
3352 | ||
3353 | /* This is the entry point used by the parser to build binary operators | |
3354 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3355 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3356 | expression, we check for operands that were written with other binary | |
ba47d38d AH |
3357 | operators in a way that is likely to confuse the user. |
3358 | ||
3359 | LOCATION is the location of the binary operator. */ | |
edc7c4ec | 3360 | |
487a92fe | 3361 | struct c_expr |
ba47d38d AH |
3362 | parser_build_binary_op (location_t location, enum tree_code code, |
3363 | struct c_expr arg1, struct c_expr arg2) | |
400fbf9f | 3364 | { |
487a92fe | 3365 | struct c_expr result; |
400fbf9f | 3366 | |
487a92fe JM |
3367 | enum tree_code code1 = arg1.original_code; |
3368 | enum tree_code code2 = arg2.original_code; | |
6866c6e8 ILT |
3369 | tree type1 = (arg1.original_type |
3370 | ? arg1.original_type | |
3371 | : TREE_TYPE (arg1.value)); | |
3372 | tree type2 = (arg2.original_type | |
3373 | ? arg2.original_type | |
3374 | : TREE_TYPE (arg2.value)); | |
400fbf9f | 3375 | |
ba47d38d AH |
3376 | result.value = build_binary_op (location, code, |
3377 | arg1.value, arg2.value, 1); | |
487a92fe | 3378 | result.original_code = code; |
6866c6e8 | 3379 | result.original_type = NULL; |
58bf601b | 3380 | |
487a92fe JM |
3381 | if (TREE_CODE (result.value) == ERROR_MARK) |
3382 | return result; | |
400fbf9f | 3383 | |
ba47d38d AH |
3384 | if (location != UNKNOWN_LOCATION) |
3385 | protected_set_expr_location (result.value, location); | |
3386 | ||
400fbf9f | 3387 | /* Check for cases such as x+y<<z which users are likely |
487a92fe | 3388 | to misinterpret. */ |
400fbf9f | 3389 | if (warn_parentheses) |
5d9de0d0 PC |
3390 | warn_about_parentheses (input_location, code, |
3391 | code1, arg1.value, code2, arg2.value); | |
001af587 | 3392 | |
ca409efd | 3393 | if (warn_logical_op) |
a243fb4a | 3394 | warn_logical_operator (input_location, code, TREE_TYPE (result.value), |
ca409efd | 3395 | code1, arg1.value, code2, arg2.value); |
63a08740 | 3396 | |
e994a705 RS |
3397 | /* Warn about comparisons against string literals, with the exception |
3398 | of testing for equality or inequality of a string literal with NULL. */ | |
3399 | if (code == EQ_EXPR || code == NE_EXPR) | |
3400 | { | |
3401 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3402 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
c2255bc4 AH |
3403 | warning_at (location, OPT_Waddress, |
3404 | "comparison with string literal results in unspecified behavior"); | |
e994a705 RS |
3405 | } |
3406 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3407 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
c2255bc4 AH |
3408 | warning_at (location, OPT_Waddress, |
3409 | "comparison with string literal results in unspecified behavior"); | |
e994a705 | 3410 | |
b8698a0f L |
3411 | if (TREE_OVERFLOW_P (result.value) |
3412 | && !TREE_OVERFLOW_P (arg1.value) | |
59c0753d | 3413 | && !TREE_OVERFLOW_P (arg2.value)) |
c2255bc4 | 3414 | overflow_warning (location, result.value); |
400fbf9f | 3415 | |
6866c6e8 ILT |
3416 | /* Warn about comparisons of different enum types. */ |
3417 | if (warn_enum_compare | |
3418 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3419 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3420 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3421 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3422 | warning_at (location, OPT_Wenum_compare, | |
3423 | "comparison between %qT and %qT", | |
3424 | type1, type2); | |
3425 | ||
400fbf9f JW |
3426 | return result; |
3427 | } | |
3e4093b6 | 3428 | \f |
3e4093b6 RS |
3429 | /* Return a tree for the difference of pointers OP0 and OP1. |
3430 | The resulting tree has type int. */ | |
293c9fdd | 3431 | |
3e4093b6 | 3432 | static tree |
db3927fb | 3433 | pointer_diff (location_t loc, tree op0, tree op1) |
3e4093b6 | 3434 | { |
3e4093b6 | 3435 | tree restype = ptrdiff_type_node; |
36c5e70a | 3436 | tree result, inttype; |
400fbf9f | 3437 | |
36c5e70a BE |
3438 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); |
3439 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
3e4093b6 RS |
3440 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
3441 | tree con0, con1, lit0, lit1; | |
3442 | tree orig_op1 = op1; | |
400fbf9f | 3443 | |
36c5e70a BE |
3444 | /* If the operands point into different address spaces, we need to |
3445 | explicitly convert them to pointers into the common address space | |
3446 | before we can subtract the numerical address values. */ | |
3447 | if (as0 != as1) | |
3448 | { | |
3449 | addr_space_t as_common; | |
3450 | tree common_type; | |
3451 | ||
3452 | /* Determine the common superset address space. This is guaranteed | |
3453 | to exist because the caller verified that comp_target_types | |
3454 | returned non-zero. */ | |
3455 | if (!addr_space_superset (as0, as1, &as_common)) | |
3456 | gcc_unreachable (); | |
3457 | ||
3458 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3459 | op0 = convert (common_type, op0); | |
3460 | op1 = convert (common_type, op1); | |
3461 | } | |
3462 | ||
3463 | /* Determine integer type to perform computations in. This will usually | |
3464 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3465 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3466 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
647d4b75 | 3467 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); |
36c5e70a BE |
3468 | else |
3469 | inttype = restype; | |
3470 | ||
3471 | ||
fcf73884 | 3472 | if (TREE_CODE (target_type) == VOID_TYPE) |
44d90fe1 | 3473 | pedwarn (loc, OPT_Wpointer_arith, |
fcf73884 MLI |
3474 | "pointer of type %<void *%> used in subtraction"); |
3475 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
44d90fe1 | 3476 | pedwarn (loc, OPT_Wpointer_arith, |
fcf73884 | 3477 | "pointer to a function used in subtraction"); |
400fbf9f | 3478 | |
3e4093b6 RS |
3479 | /* If the conversion to ptrdiff_type does anything like widening or |
3480 | converting a partial to an integral mode, we get a convert_expression | |
3481 | that is in the way to do any simplifications. | |
3482 | (fold-const.c doesn't know that the extra bits won't be needed. | |
3483 | split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a | |
3484 | different mode in place.) | |
3485 | So first try to find a common term here 'by hand'; we want to cover | |
3486 | at least the cases that occur in legal static initializers. */ | |
1043771b | 3487 | if (CONVERT_EXPR_P (op0) |
1344f9a3 JM |
3488 | && (TYPE_PRECISION (TREE_TYPE (op0)) |
3489 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))) | |
3490 | con0 = TREE_OPERAND (op0, 0); | |
3491 | else | |
3492 | con0 = op0; | |
1043771b | 3493 | if (CONVERT_EXPR_P (op1) |
1344f9a3 JM |
3494 | && (TYPE_PRECISION (TREE_TYPE (op1)) |
3495 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))) | |
3496 | con1 = TREE_OPERAND (op1, 0); | |
3497 | else | |
3498 | con1 = op1; | |
400fbf9f | 3499 | |
5de72424 | 3500 | if (TREE_CODE (con0) == POINTER_PLUS_EXPR) |
3e4093b6 RS |
3501 | { |
3502 | lit0 = TREE_OPERAND (con0, 1); | |
3503 | con0 = TREE_OPERAND (con0, 0); | |
3504 | } | |
3505 | else | |
3506 | lit0 = integer_zero_node; | |
400fbf9f | 3507 | |
5de72424 | 3508 | if (TREE_CODE (con1) == POINTER_PLUS_EXPR) |
400fbf9f | 3509 | { |
3e4093b6 RS |
3510 | lit1 = TREE_OPERAND (con1, 1); |
3511 | con1 = TREE_OPERAND (con1, 0); | |
400fbf9f JW |
3512 | } |
3513 | else | |
3e4093b6 RS |
3514 | lit1 = integer_zero_node; |
3515 | ||
3516 | if (operand_equal_p (con0, con1, 0)) | |
400fbf9f | 3517 | { |
3e4093b6 RS |
3518 | op0 = lit0; |
3519 | op1 = lit1; | |
400fbf9f JW |
3520 | } |
3521 | ||
400fbf9f | 3522 | |
3e4093b6 RS |
3523 | /* First do the subtraction as integers; |
3524 | then drop through to build the divide operator. | |
3525 | Do not do default conversions on the minus operator | |
3526 | in case restype is a short type. */ | |
400fbf9f | 3527 | |
db3927fb | 3528 | op0 = build_binary_op (loc, |
36c5e70a BE |
3529 | MINUS_EXPR, convert (inttype, op0), |
3530 | convert (inttype, op1), 0); | |
3e4093b6 RS |
3531 | /* This generates an error if op1 is pointer to incomplete type. */ |
3532 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
db3927fb | 3533 | error_at (loc, "arithmetic on pointer to an incomplete type"); |
400fbf9f | 3534 | |
3e4093b6 RS |
3535 | /* This generates an error if op0 is pointer to incomplete type. */ |
3536 | op1 = c_size_in_bytes (target_type); | |
400fbf9f | 3537 | |
3e4093b6 | 3538 | /* Divide by the size, in easiest possible way. */ |
36c5e70a BE |
3539 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, |
3540 | op0, convert (inttype, op1)); | |
3541 | ||
3542 | /* Convert to final result type if necessary. */ | |
3543 | return convert (restype, result); | |
3e4093b6 RS |
3544 | } |
3545 | \f | |
267bac10 JM |
3546 | /* Expand atomic compound assignments into an approriate sequence as |
3547 | specified by the C11 standard section 6.5.16.2. | |
3548 | given | |
3549 | _Atomic T1 E1 | |
3550 | T2 E2 | |
3551 | E1 op= E2 | |
3552 | ||
3553 | This sequence is used for all types for which these operations are | |
3554 | supported. | |
3555 | ||
3556 | In addition, built-in versions of the 'fe' prefixed routines may | |
3557 | need to be invoked for floating point (real, complex or vector) when | |
3558 | floating-point exceptions are supported. See 6.5.16.2 footnote 113. | |
3559 | ||
3560 | T1 newval; | |
3561 | T1 old; | |
3562 | T1 *addr | |
3563 | T2 val | |
3564 | fenv_t fenv | |
3565 | ||
3566 | addr = &E1; | |
3567 | val = (E2); | |
3568 | __atomic_load (addr, &old, SEQ_CST); | |
3569 | feholdexcept (&fenv); | |
3570 | loop: | |
3571 | newval = old op val; | |
3572 | if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST, | |
3573 | SEQ_CST)) | |
3574 | goto done; | |
3575 | feclearexcept (FE_ALL_EXCEPT); | |
3576 | goto loop: | |
3577 | done: | |
3578 | feupdateenv (&fenv); | |
3579 | ||
3580 | Also note that the compiler is simply issuing the generic form of | |
3581 | the atomic operations. This requires temp(s) and has their address | |
3582 | taken. The atomic processing is smart enough to figure out when the | |
3583 | size of an object can utilize a lock-free version, and convert the | |
3584 | built-in call to the appropriate lock-free routine. The optimizers | |
3585 | will then dispose of any temps that are no longer required, and | |
3586 | lock-free implementations are utilized as long as there is target | |
3587 | support for the required size. | |
3588 | ||
3589 | If the operator is NOP_EXPR, then this is a simple assignment, and | |
3590 | an __atomic_store is issued to perform the assignment rather than | |
3591 | the above loop. | |
3592 | ||
3593 | */ | |
3594 | ||
3595 | /* Build an atomic assignment at LOC, expanding into the proper | |
3596 | sequence to store LHS MODIFYCODE= RHS. Return a value representing | |
3597 | the result of the operation, unless RETURN_OLD_P in which case | |
3598 | return the old value of LHS (this is only for postincrement and | |
3599 | postdecrement). */ | |
3600 | static tree | |
3601 | build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode, | |
3602 | tree rhs, bool return_old_p) | |
3603 | { | |
3604 | tree fndecl, func_call; | |
3605 | vec<tree, va_gc> *params; | |
3606 | tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr; | |
3607 | tree old, old_addr; | |
3608 | tree compound_stmt; | |
3609 | tree stmt, goto_stmt; | |
3610 | tree loop_label, loop_decl, done_label, done_decl; | |
3611 | ||
3612 | tree lhs_type = TREE_TYPE (lhs); | |
3613 | tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0); | |
3614 | tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST); | |
3615 | tree rhs_type = TREE_TYPE (rhs); | |
3616 | ||
3617 | gcc_assert (TYPE_ATOMIC (lhs_type)); | |
3618 | ||
3619 | if (return_old_p) | |
3620 | gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR); | |
3621 | ||
3622 | /* Allocate enough vector items for a compare_exchange. */ | |
3623 | vec_alloc (params, 6); | |
3624 | ||
3625 | /* Create a compound statement to hold the sequence of statements | |
3626 | with a loop. */ | |
3627 | compound_stmt = c_begin_compound_stmt (false); | |
3628 | ||
3629 | /* Fold the RHS if it hasn't already been folded. */ | |
3630 | if (modifycode != NOP_EXPR) | |
3631 | rhs = c_fully_fold (rhs, false, NULL); | |
3632 | ||
3633 | /* Remove the qualifiers for the rest of the expressions and create | |
3634 | the VAL temp variable to hold the RHS. */ | |
3635 | nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED); | |
3636 | nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED); | |
3637 | val = create_tmp_var (nonatomic_rhs_type, NULL); | |
3638 | TREE_ADDRESSABLE (val) = 1; | |
3639 | rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs); | |
3640 | SET_EXPR_LOCATION (rhs, loc); | |
3641 | add_stmt (rhs); | |
3642 | ||
3643 | /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue | |
3644 | an atomic_store. */ | |
3645 | if (modifycode == NOP_EXPR) | |
3646 | { | |
3647 | /* Build __atomic_store (&lhs, &val, SEQ_CST) */ | |
3648 | rhs = build_unary_op (loc, ADDR_EXPR, val, 0); | |
3649 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE); | |
3650 | params->quick_push (lhs_addr); | |
3651 | params->quick_push (rhs); | |
3652 | params->quick_push (seq_cst); | |
3653 | func_call = build_function_call_vec (loc, fndecl, params, NULL); | |
3654 | add_stmt (func_call); | |
3655 | ||
3656 | /* Finish the compound statement. */ | |
3657 | compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); | |
3658 | ||
3659 | /* VAL is the value which was stored, return a COMPOUND_STMT of | |
3660 | the statement and that value. */ | |
3661 | return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val); | |
3662 | } | |
3663 | ||
3664 | /* Create the variables and labels required for the op= form. */ | |
3665 | old = create_tmp_var (nonatomic_lhs_type, NULL); | |
3666 | old_addr = build_unary_op (loc, ADDR_EXPR, old, 0); | |
3667 | TREE_ADDRESSABLE (val) = 1; | |
3668 | ||
3669 | newval = create_tmp_var (nonatomic_lhs_type, NULL); | |
3670 | newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0); | |
3671 | TREE_ADDRESSABLE (newval) = 1; | |
3672 | ||
3673 | loop_decl = create_artificial_label (loc); | |
3674 | loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl); | |
3675 | ||
3676 | done_decl = create_artificial_label (loc); | |
3677 | done_label = build1 (LABEL_EXPR, void_type_node, done_decl); | |
3678 | ||
3679 | /* __atomic_load (addr, &old, SEQ_CST). */ | |
3680 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD); | |
3681 | params->quick_push (lhs_addr); | |
3682 | params->quick_push (old_addr); | |
3683 | params->quick_push (seq_cst); | |
3684 | func_call = build_function_call_vec (loc, fndecl, params, NULL); | |
3685 | add_stmt (func_call); | |
3686 | params->truncate (0); | |
3687 | ||
3688 | /* Create the expressions for floating-point environment | |
3689 | manipulation, if required. */ | |
3690 | bool need_fenv = (flag_trapping_math | |
3691 | && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type))); | |
3692 | tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE; | |
3693 | if (need_fenv) | |
3694 | targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call); | |
3695 | ||
3696 | if (hold_call) | |
3697 | add_stmt (hold_call); | |
3698 | ||
3699 | /* loop: */ | |
3700 | add_stmt (loop_label); | |
3701 | ||
3702 | /* newval = old + val; */ | |
3703 | rhs = build_binary_op (loc, modifycode, old, val, 1); | |
3704 | rhs = convert_for_assignment (loc, nonatomic_lhs_type, rhs, NULL_TREE, | |
3705 | ic_assign, false, NULL_TREE, | |
3706 | NULL_TREE, 0); | |
3707 | if (rhs != error_mark_node) | |
3708 | { | |
3709 | rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs); | |
3710 | SET_EXPR_LOCATION (rhs, loc); | |
3711 | add_stmt (rhs); | |
3712 | } | |
3713 | ||
3714 | /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST)) | |
3715 | goto done; */ | |
3716 | fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE); | |
3717 | params->quick_push (lhs_addr); | |
3718 | params->quick_push (old_addr); | |
3719 | params->quick_push (newval_addr); | |
3720 | params->quick_push (integer_zero_node); | |
3721 | params->quick_push (seq_cst); | |
3722 | params->quick_push (seq_cst); | |
3723 | func_call = build_function_call_vec (loc, fndecl, params, NULL); | |
3724 | ||
3725 | goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl); | |
3726 | SET_EXPR_LOCATION (goto_stmt, loc); | |
3727 | ||
3728 | stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE); | |
3729 | SET_EXPR_LOCATION (stmt, loc); | |
3730 | add_stmt (stmt); | |
3731 | ||
3732 | if (clear_call) | |
3733 | add_stmt (clear_call); | |
3734 | ||
3735 | /* goto loop; */ | |
3736 | goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl); | |
3737 | SET_EXPR_LOCATION (goto_stmt, loc); | |
3738 | add_stmt (goto_stmt); | |
3739 | ||
3740 | /* done: */ | |
3741 | add_stmt (done_label); | |
3742 | ||
3743 | if (update_call) | |
3744 | add_stmt (update_call); | |
3745 | ||
3746 | /* Finish the compound statement. */ | |
3747 | compound_stmt = c_end_compound_stmt (loc, compound_stmt, false); | |
3748 | ||
3749 | /* NEWVAL is the value that was successfully stored, return a | |
3750 | COMPOUND_EXPR of the statement and the appropriate value. */ | |
3751 | return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, | |
3752 | return_old_p ? old : newval); | |
3753 | } | |
3754 | ||
3e4093b6 RS |
3755 | /* Construct and perhaps optimize a tree representation |
3756 | for a unary operation. CODE, a tree_code, specifies the operation | |
3757 | and XARG is the operand. | |
3758 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3759 | the default promotions (such as from short to int). | |
3760 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3761 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
c9f9eb5d AH |
3762 | arrays to pointers in C99. |
3763 | ||
3764 | LOCATION is the location of the operator. */ | |
400fbf9f | 3765 | |
3e4093b6 | 3766 | tree |
c9f9eb5d AH |
3767 | build_unary_op (location_t location, |
3768 | enum tree_code code, tree xarg, int flag) | |
3e4093b6 RS |
3769 | { |
3770 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3771 | tree arg = xarg; | |
3772 | tree argtype = 0; | |
808d6eaa | 3773 | enum tree_code typecode; |
3e4093b6 | 3774 | tree val; |
c9f9eb5d | 3775 | tree ret = error_mark_node; |
8ce94e44 | 3776 | tree eptype = NULL_TREE; |
3e4093b6 | 3777 | int noconvert = flag; |
4de67c26 | 3778 | const char *invalid_op_diag; |
928c19bb JM |
3779 | bool int_operands; |
3780 | ||
3781 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
4d84fe7c JM |
3782 | if (int_operands) |
3783 | arg = remove_c_maybe_const_expr (arg); | |
400fbf9f | 3784 | |
808d6eaa JM |
3785 | if (code != ADDR_EXPR) |
3786 | arg = require_complete_type (arg); | |
3787 | ||
3788 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
3e4093b6 RS |
3789 | if (typecode == ERROR_MARK) |
3790 | return error_mark_node; | |
3791 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3792 | typecode = INTEGER_TYPE; | |
6c36d76b | 3793 | |
4de67c26 JM |
3794 | if ((invalid_op_diag |
3795 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3796 | { | |
c9f9eb5d | 3797 | error_at (location, invalid_op_diag); |
4de67c26 JM |
3798 | return error_mark_node; |
3799 | } | |
3800 | ||
8ce94e44 JM |
3801 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
3802 | { | |
3803 | eptype = TREE_TYPE (arg); | |
3804 | arg = TREE_OPERAND (arg, 0); | |
3805 | } | |
3806 | ||
3e4093b6 RS |
3807 | switch (code) |
3808 | { | |
3809 | case CONVERT_EXPR: | |
3810 | /* This is used for unary plus, because a CONVERT_EXPR | |
3811 | is enough to prevent anybody from looking inside for | |
3812 | associativity, but won't generate any code. */ | |
3813 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3814 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
8a2cee38 | 3815 | || typecode == VECTOR_TYPE)) |
400fbf9f | 3816 | { |
c9f9eb5d | 3817 | error_at (location, "wrong type argument to unary plus"); |
3e4093b6 | 3818 | return error_mark_node; |
400fbf9f | 3819 | } |
3e4093b6 RS |
3820 | else if (!noconvert) |
3821 | arg = default_conversion (arg); | |
db3927fb | 3822 | arg = non_lvalue_loc (location, arg); |
400fbf9f JW |
3823 | break; |
3824 | ||
3e4093b6 RS |
3825 | case NEGATE_EXPR: |
3826 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3827 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
3e4093b6 RS |
3828 | || typecode == VECTOR_TYPE)) |
3829 | { | |
c9f9eb5d | 3830 | error_at (location, "wrong type argument to unary minus"); |
3e4093b6 RS |
3831 | return error_mark_node; |
3832 | } | |
3833 | else if (!noconvert) | |
3834 | arg = default_conversion (arg); | |
400fbf9f JW |
3835 | break; |
3836 | ||
3e4093b6 | 3837 | case BIT_NOT_EXPR: |
462643f0 AP |
3838 | /* ~ works on integer types and non float vectors. */ |
3839 | if (typecode == INTEGER_TYPE | |
3840 | || (typecode == VECTOR_TYPE | |
3841 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
03d5b1f5 | 3842 | { |
3e4093b6 RS |
3843 | if (!noconvert) |
3844 | arg = default_conversion (arg); | |
03d5b1f5 | 3845 | } |
3e4093b6 | 3846 | else if (typecode == COMPLEX_TYPE) |
400fbf9f | 3847 | { |
3e4093b6 | 3848 | code = CONJ_EXPR; |
c1771a20 | 3849 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 3850 | "ISO C does not support %<~%> for complex conjugation"); |
3e4093b6 RS |
3851 | if (!noconvert) |
3852 | arg = default_conversion (arg); | |
3853 | } | |
3854 | else | |
3855 | { | |
c9f9eb5d | 3856 | error_at (location, "wrong type argument to bit-complement"); |
3e4093b6 | 3857 | return error_mark_node; |
400fbf9f JW |
3858 | } |
3859 | break; | |
3860 | ||
3e4093b6 | 3861 | case ABS_EXPR: |
11017cc7 | 3862 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
400fbf9f | 3863 | { |
c9f9eb5d | 3864 | error_at (location, "wrong type argument to abs"); |
3e4093b6 | 3865 | return error_mark_node; |
400fbf9f | 3866 | } |
3e4093b6 RS |
3867 | else if (!noconvert) |
3868 | arg = default_conversion (arg); | |
400fbf9f JW |
3869 | break; |
3870 | ||
3e4093b6 RS |
3871 | case CONJ_EXPR: |
3872 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3873 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3874 | || typecode == COMPLEX_TYPE)) | |
400fbf9f | 3875 | { |
c9f9eb5d | 3876 | error_at (location, "wrong type argument to conjugation"); |
3e4093b6 | 3877 | return error_mark_node; |
400fbf9f | 3878 | } |
3e4093b6 RS |
3879 | else if (!noconvert) |
3880 | arg = default_conversion (arg); | |
400fbf9f JW |
3881 | break; |
3882 | ||
3e4093b6 | 3883 | case TRUTH_NOT_EXPR: |
ab22c1fa | 3884 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3885 | && typecode != REAL_TYPE && typecode != POINTER_TYPE |
46bdb9cf | 3886 | && typecode != COMPLEX_TYPE) |
400fbf9f | 3887 | { |
c9f9eb5d AH |
3888 | error_at (location, |
3889 | "wrong type argument to unary exclamation mark"); | |
3e4093b6 | 3890 | return error_mark_node; |
400fbf9f | 3891 | } |
a27d595d JM |
3892 | if (int_operands) |
3893 | { | |
3894 | arg = c_objc_common_truthvalue_conversion (location, xarg); | |
3895 | arg = remove_c_maybe_const_expr (arg); | |
3896 | } | |
3897 | else | |
3898 | arg = c_objc_common_truthvalue_conversion (location, arg); | |
db3927fb | 3899 | ret = invert_truthvalue_loc (location, arg); |
ca80e52b EB |
3900 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ |
3901 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3902 | location = EXPR_LOCATION (ret); | |
c9f9eb5d | 3903 | goto return_build_unary_op; |
3e4093b6 | 3904 | |
3e4093b6 | 3905 | case REALPART_EXPR: |
3e4093b6 | 3906 | case IMAGPART_EXPR: |
fb52b50a NF |
3907 | ret = build_real_imag_expr (location, code, arg); |
3908 | if (ret == error_mark_node) | |
3909 | return error_mark_node; | |
8ce94e44 JM |
3910 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3911 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3912 | goto return_build_unary_op; |
3e4093b6 RS |
3913 | |
3914 | case PREINCREMENT_EXPR: | |
3915 | case POSTINCREMENT_EXPR: | |
3916 | case PREDECREMENT_EXPR: | |
3917 | case POSTDECREMENT_EXPR: | |
3e4093b6 | 3918 | |
928c19bb JM |
3919 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) |
3920 | { | |
3921 | tree inner = build_unary_op (location, code, | |
3922 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3923 | if (inner == error_mark_node) | |
3924 | return error_mark_node; | |
3925 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3926 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3927 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3928 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3929 | goto return_build_unary_op; | |
3930 | } | |
3931 | ||
925e8657 NP |
3932 | /* Complain about anything that is not a true lvalue. In |
3933 | Objective-C, skip this check for property_refs. */ | |
f90e8e2e | 3934 | if (!objc_is_property_ref (arg) |
7bd11157 TT |
3935 | && !lvalue_or_else (location, |
3936 | arg, ((code == PREINCREMENT_EXPR | |
925e8657 NP |
3937 | || code == POSTINCREMENT_EXPR) |
3938 | ? lv_increment | |
3939 | : lv_decrement))) | |
928c19bb JM |
3940 | return error_mark_node; |
3941 | ||
09639a83 ILT |
3942 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) |
3943 | { | |
3944 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3945 | warning_at (location, OPT_Wc___compat, | |
3946 | "increment of enumeration value is invalid in C++"); | |
3947 | else | |
3948 | warning_at (location, OPT_Wc___compat, | |
3949 | "decrement of enumeration value is invalid in C++"); | |
3950 | } | |
3951 | ||
928c19bb JM |
3952 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ |
3953 | arg = c_fully_fold (arg, false, NULL); | |
3954 | ||
267bac10 JM |
3955 | bool atomic_op; |
3956 | atomic_op = really_atomic_lvalue (arg); | |
3957 | ||
3e4093b6 RS |
3958 | /* Increment or decrement the real part of the value, |
3959 | and don't change the imaginary part. */ | |
3960 | if (typecode == COMPLEX_TYPE) | |
400fbf9f | 3961 | { |
3e4093b6 RS |
3962 | tree real, imag; |
3963 | ||
c1771a20 | 3964 | pedwarn (location, OPT_Wpedantic, |
509c9d60 | 3965 | "ISO C does not support %<++%> and %<--%> on complex types"); |
3e4093b6 | 3966 | |
267bac10 JM |
3967 | if (!atomic_op) |
3968 | { | |
3969 | arg = stabilize_reference (arg); | |
3970 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); | |
3971 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
3972 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
3973 | if (real == error_mark_node || imag == error_mark_node) | |
3974 | return error_mark_node; | |
3975 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), | |
3976 | real, imag); | |
3977 | goto return_build_unary_op; | |
3978 | } | |
400fbf9f | 3979 | } |
3e4093b6 RS |
3980 | |
3981 | /* Report invalid types. */ | |
3982 | ||
ab22c1fa | 3983 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE |
267bac10 | 3984 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE |
241b71bb | 3985 | && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE) |
400fbf9f | 3986 | { |
3e4093b6 | 3987 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c9f9eb5d | 3988 | error_at (location, "wrong type argument to increment"); |
c22cacf3 | 3989 | else |
c9f9eb5d | 3990 | error_at (location, "wrong type argument to decrement"); |
3e4093b6 RS |
3991 | |
3992 | return error_mark_node; | |
400fbf9f | 3993 | } |
400fbf9f | 3994 | |
3e4093b6 RS |
3995 | { |
3996 | tree inc; | |
400fbf9f | 3997 | |
3e4093b6 RS |
3998 | argtype = TREE_TYPE (arg); |
3999 | ||
4000 | /* Compute the increment. */ | |
4001 | ||
4002 | if (typecode == POINTER_TYPE) | |
4003 | { | |
4004 | /* If pointer target is an undefined struct, | |
4005 | we just cannot know how to do the arithmetic. */ | |
b70cef5d | 4006 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) |
3e4093b6 RS |
4007 | { |
4008 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
c9f9eb5d AH |
4009 | error_at (location, |
4010 | "increment of pointer to unknown structure"); | |
3e4093b6 | 4011 | else |
c9f9eb5d AH |
4012 | error_at (location, |
4013 | "decrement of pointer to unknown structure"); | |
3e4093b6 | 4014 | } |
b70cef5d JJ |
4015 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE |
4016 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
c22cacf3 | 4017 | { |
3e4093b6 | 4018 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
44d90fe1 | 4019 | pedwarn (location, OPT_Wpointer_arith, |
fcf73884 | 4020 | "wrong type argument to increment"); |
3e4093b6 | 4021 | else |
44d90fe1 | 4022 | pedwarn (location, OPT_Wpointer_arith, |
fcf73884 | 4023 | "wrong type argument to decrement"); |
3e4093b6 RS |
4024 | } |
4025 | ||
b70cef5d | 4026 | inc = c_size_in_bytes (TREE_TYPE (argtype)); |
0d82a1c8 | 4027 | inc = convert_to_ptrofftype_loc (location, inc); |
3e4093b6 | 4028 | } |
b70cef5d | 4029 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) |
ab22c1fa CF |
4030 | { |
4031 | /* For signed fract types, we invert ++ to -- or | |
4032 | -- to ++, and change inc from 1 to -1, because | |
4033 | it is not possible to represent 1 in signed fract constants. | |
4034 | For unsigned fract types, the result always overflows and | |
4035 | we get an undefined (original) or the maximum value. */ | |
4036 | if (code == PREINCREMENT_EXPR) | |
4037 | code = PREDECREMENT_EXPR; | |
4038 | else if (code == PREDECREMENT_EXPR) | |
4039 | code = PREINCREMENT_EXPR; | |
4040 | else if (code == POSTINCREMENT_EXPR) | |
4041 | code = POSTDECREMENT_EXPR; | |
4042 | else /* code == POSTDECREMENT_EXPR */ | |
4043 | code = POSTINCREMENT_EXPR; | |
4044 | ||
4045 | inc = integer_minus_one_node; | |
4046 | inc = convert (argtype, inc); | |
4047 | } | |
3e4093b6 | 4048 | else |
5be014d5 | 4049 | { |
241b71bb TV |
4050 | inc = VECTOR_TYPE_P (argtype) |
4051 | ? build_one_cst (argtype) | |
4052 | : integer_one_node; | |
5be014d5 AP |
4053 | inc = convert (argtype, inc); |
4054 | } | |
3e4093b6 | 4055 | |
925e8657 NP |
4056 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
4057 | need to ask Objective-C to build the increment or decrement | |
4058 | expression for it. */ | |
4059 | if (objc_is_property_ref (arg)) | |
f90e8e2e | 4060 | return objc_build_incr_expr_for_property_ref (location, code, |
925e8657 NP |
4061 | arg, inc); |
4062 | ||
3e4093b6 | 4063 | /* Report a read-only lvalue. */ |
f37acdf9 | 4064 | if (TYPE_READONLY (argtype)) |
953ff289 | 4065 | { |
c02065fc | 4066 | readonly_error (location, arg, |
953ff289 DN |
4067 | ((code == PREINCREMENT_EXPR |
4068 | || code == POSTINCREMENT_EXPR) | |
4069 | ? lv_increment : lv_decrement)); | |
4070 | return error_mark_node; | |
4071 | } | |
f37acdf9 JM |
4072 | else if (TREE_READONLY (arg)) |
4073 | readonly_warning (arg, | |
4074 | ((code == PREINCREMENT_EXPR | |
4075 | || code == POSTINCREMENT_EXPR) | |
4076 | ? lv_increment : lv_decrement)); | |
3e4093b6 | 4077 | |
267bac10 JM |
4078 | /* If the argument is atomic, use the special code sequences for |
4079 | atomic compound assignment. */ | |
4080 | if (atomic_op) | |
4081 | { | |
4082 | arg = stabilize_reference (arg); | |
4083 | ret = build_atomic_assign (location, arg, | |
4084 | ((code == PREINCREMENT_EXPR | |
4085 | || code == POSTINCREMENT_EXPR) | |
4086 | ? PLUS_EXPR | |
4087 | : MINUS_EXPR), | |
4088 | (FRACT_MODE_P (TYPE_MODE (argtype)) | |
4089 | ? inc | |
4090 | : integer_one_node), | |
4091 | (code == POSTINCREMENT_EXPR | |
4092 | || code == POSTDECREMENT_EXPR)); | |
4093 | goto return_build_unary_op; | |
4094 | } | |
4095 | ||
3e4093b6 RS |
4096 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) |
4097 | val = boolean_increment (code, arg); | |
4098 | else | |
53fb4de3 | 4099 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
3e4093b6 | 4100 | TREE_SIDE_EFFECTS (val) = 1; |
3e4093b6 | 4101 | if (TREE_CODE (val) != code) |
6de9cd9a | 4102 | TREE_NO_WARNING (val) = 1; |
c9f9eb5d AH |
4103 | ret = val; |
4104 | goto return_build_unary_op; | |
3e4093b6 RS |
4105 | } |
4106 | ||
4107 | case ADDR_EXPR: | |
4108 | /* Note that this operation never does default_conversion. */ | |
4109 | ||
2b4b7036 JM |
4110 | /* The operand of unary '&' must be an lvalue (which excludes |
4111 | expressions of type void), or, in C99, the result of a [] or | |
4112 | unary '*' operator. */ | |
4113 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
4114 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
4115 | && (TREE_CODE (arg) != INDIRECT_REF | |
4116 | || !flag_isoc99)) | |
4117 | pedwarn (location, 0, "taking address of expression of type %<void%>"); | |
4118 | ||
3e4093b6 RS |
4119 | /* Let &* cancel out to simplify resulting code. */ |
4120 | if (TREE_CODE (arg) == INDIRECT_REF) | |
400fbf9f | 4121 | { |
3e4093b6 RS |
4122 | /* Don't let this be an lvalue. */ |
4123 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
db3927fb | 4124 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); |
c9f9eb5d AH |
4125 | ret = TREE_OPERAND (arg, 0); |
4126 | goto return_build_unary_op; | |
400fbf9f | 4127 | } |
1eb8759b | 4128 | |
7c672dfc | 4129 | /* For &x[y], return x+y */ |
3e4093b6 | 4130 | if (TREE_CODE (arg) == ARRAY_REF) |
1eb8759b | 4131 | { |
f2a71bbc JM |
4132 | tree op0 = TREE_OPERAND (arg, 0); |
4133 | if (!c_mark_addressable (op0)) | |
3e4093b6 | 4134 | return error_mark_node; |
1eb8759b | 4135 | } |
1eb8759b | 4136 | |
3e4093b6 RS |
4137 | /* Anything not already handled and not a true memory reference |
4138 | or a non-lvalue array is an error. */ | |
4139 | else if (typecode != FUNCTION_TYPE && !flag | |
7bd11157 | 4140 | && !lvalue_or_else (location, arg, lv_addressof)) |
3e4093b6 | 4141 | return error_mark_node; |
b6a10c9f | 4142 | |
928c19bb JM |
4143 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify |
4144 | folding later. */ | |
4145 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
4146 | { | |
4147 | tree inner = build_unary_op (location, code, | |
4148 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
4149 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4150 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
4151 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
4152 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
4153 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
4154 | goto return_build_unary_op; | |
4155 | } | |
4156 | ||
3e4093b6 RS |
4157 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
4158 | argtype = TREE_TYPE (arg); | |
400fbf9f | 4159 | |
3e4093b6 | 4160 | /* If the lvalue is const or volatile, merge that into the type |
e73a83fc | 4161 | to which the address will point. This is only needed |
f2c1da78 | 4162 | for function types. */ |
6615c446 | 4163 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) |
e73a83fc RB |
4164 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) |
4165 | && TREE_CODE (argtype) == FUNCTION_TYPE) | |
f2c1da78 JM |
4166 | { |
4167 | int orig_quals = TYPE_QUALS (strip_array_types (argtype)); | |
4168 | int quals = orig_quals; | |
4169 | ||
4170 | if (TREE_READONLY (arg)) | |
4171 | quals |= TYPE_QUAL_CONST; | |
4172 | if (TREE_THIS_VOLATILE (arg)) | |
4173 | quals |= TYPE_QUAL_VOLATILE; | |
4174 | ||
f2c1da78 JM |
4175 | argtype = c_build_qualified_type (argtype, quals); |
4176 | } | |
400fbf9f | 4177 | |
3e4093b6 RS |
4178 | if (!c_mark_addressable (arg)) |
4179 | return error_mark_node; | |
400fbf9f | 4180 | |
abb54d14 JM |
4181 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF |
4182 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
400fbf9f | 4183 | |
5cc200fc | 4184 | argtype = build_pointer_type (argtype); |
5e55f99d RH |
4185 | |
4186 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
4187 | when we have proper support for integer constant expressions. */ | |
4188 | val = get_base_address (arg); | |
4189 | if (val && TREE_CODE (val) == INDIRECT_REF | |
3aa2ddb8 JJ |
4190 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
4191 | { | |
cf9e9959 | 4192 | ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg)); |
c9f9eb5d | 4193 | goto return_build_unary_op; |
3aa2ddb8 | 4194 | } |
5e55f99d | 4195 | |
5cc200fc | 4196 | val = build1 (ADDR_EXPR, argtype, arg); |
400fbf9f | 4197 | |
c9f9eb5d AH |
4198 | ret = val; |
4199 | goto return_build_unary_op; | |
400fbf9f | 4200 | |
3e4093b6 | 4201 | default: |
1344f9a3 | 4202 | gcc_unreachable (); |
3e4093b6 | 4203 | } |
400fbf9f | 4204 | |
3e4093b6 RS |
4205 | if (argtype == 0) |
4206 | argtype = TREE_TYPE (arg); | |
928c19bb JM |
4207 | if (TREE_CODE (arg) == INTEGER_CST) |
4208 | ret = (require_constant_value | |
db3927fb AH |
4209 | ? fold_build1_initializer_loc (location, code, argtype, arg) |
4210 | : fold_build1_loc (location, code, argtype, arg)); | |
928c19bb JM |
4211 | else |
4212 | ret = build1 (code, argtype, arg); | |
c9f9eb5d AH |
4213 | return_build_unary_op: |
4214 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
4215 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) |
4216 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
4217 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
4218 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
4219 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
4220 | if (eptype) |
4221 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
c9f9eb5d AH |
4222 | protected_set_expr_location (ret, location); |
4223 | return ret; | |
3e4093b6 | 4224 | } |
400fbf9f | 4225 | |
3e4093b6 RS |
4226 | /* Return nonzero if REF is an lvalue valid for this language. |
4227 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
5baeaac0 | 4228 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
400fbf9f | 4229 | |
1e4ae551 | 4230 | bool |
58f9752a | 4231 | lvalue_p (const_tree ref) |
3e4093b6 | 4232 | { |
58f9752a | 4233 | const enum tree_code code = TREE_CODE (ref); |
400fbf9f | 4234 | |
3e4093b6 RS |
4235 | switch (code) |
4236 | { | |
4237 | case REALPART_EXPR: | |
4238 | case IMAGPART_EXPR: | |
4239 | case COMPONENT_REF: | |
4240 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
400fbf9f | 4241 | |
928c19bb JM |
4242 | case C_MAYBE_CONST_EXPR: |
4243 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
4244 | ||
3e4093b6 RS |
4245 | case COMPOUND_LITERAL_EXPR: |
4246 | case STRING_CST: | |
4247 | return 1; | |
400fbf9f | 4248 | |
3e4093b6 RS |
4249 | case INDIRECT_REF: |
4250 | case ARRAY_REF: | |
36536d79 | 4251 | case ARRAY_NOTATION_REF: |
3e4093b6 RS |
4252 | case VAR_DECL: |
4253 | case PARM_DECL: | |
4254 | case RESULT_DECL: | |
4255 | case ERROR_MARK: | |
4256 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
4257 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
665f2503 | 4258 | |
3e4093b6 | 4259 | case BIND_EXPR: |
3e4093b6 | 4260 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
665f2503 | 4261 | |
3e4093b6 RS |
4262 | default: |
4263 | return 0; | |
4264 | } | |
4265 | } | |
400fbf9f | 4266 | \f |
f37acdf9 JM |
4267 | /* Give a warning for storing in something that is read-only in GCC |
4268 | terms but not const in ISO C terms. */ | |
4269 | ||
4270 | static void | |
4271 | readonly_warning (tree arg, enum lvalue_use use) | |
4272 | { | |
4273 | switch (use) | |
4274 | { | |
4275 | case lv_assign: | |
4276 | warning (0, "assignment of read-only location %qE", arg); | |
4277 | break; | |
4278 | case lv_increment: | |
4279 | warning (0, "increment of read-only location %qE", arg); | |
4280 | break; | |
4281 | case lv_decrement: | |
4282 | warning (0, "decrement of read-only location %qE", arg); | |
4283 | break; | |
4284 | default: | |
4285 | gcc_unreachable (); | |
4286 | } | |
4287 | return; | |
4288 | } | |
4289 | ||
37dc0d8d JM |
4290 | |
4291 | /* Return nonzero if REF is an lvalue valid for this language; | |
4292 | otherwise, print an error message and return zero. USE says | |
7bd11157 TT |
4293 | how the lvalue is being used and so selects the error message. |
4294 | LOCATION is the location at which any error should be reported. */ | |
37dc0d8d JM |
4295 | |
4296 | static int | |
7bd11157 | 4297 | lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) |
37dc0d8d JM |
4298 | { |
4299 | int win = lvalue_p (ref); | |
4300 | ||
4301 | if (!win) | |
7bd11157 | 4302 | lvalue_error (loc, use); |
37dc0d8d JM |
4303 | |
4304 | return win; | |
4305 | } | |
3e4093b6 RS |
4306 | \f |
4307 | /* Mark EXP saying that we need to be able to take the | |
4308 | address of it; it should not be allocated in a register. | |
4309 | Returns true if successful. */ | |
54c93c30 | 4310 | |
3e4093b6 RS |
4311 | bool |
4312 | c_mark_addressable (tree exp) | |
400fbf9f | 4313 | { |
3e4093b6 | 4314 | tree x = exp; |
95602da1 | 4315 | |
3e4093b6 RS |
4316 | while (1) |
4317 | switch (TREE_CODE (x)) | |
4318 | { | |
4319 | case COMPONENT_REF: | |
4320 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
4321 | { | |
0039fa55 AN |
4322 | error |
4323 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
3e4093b6 RS |
4324 | return false; |
4325 | } | |
95602da1 | 4326 | |
3e4093b6 | 4327 | /* ... fall through ... */ |
95602da1 | 4328 | |
3e4093b6 RS |
4329 | case ADDR_EXPR: |
4330 | case ARRAY_REF: | |
4331 | case REALPART_EXPR: | |
4332 | case IMAGPART_EXPR: | |
4333 | x = TREE_OPERAND (x, 0); | |
4334 | break; | |
95602da1 | 4335 | |
3e4093b6 RS |
4336 | case COMPOUND_LITERAL_EXPR: |
4337 | case CONSTRUCTOR: | |
4338 | TREE_ADDRESSABLE (x) = 1; | |
4339 | return true; | |
95602da1 | 4340 | |
3e4093b6 RS |
4341 | case VAR_DECL: |
4342 | case CONST_DECL: | |
4343 | case PARM_DECL: | |
4344 | case RESULT_DECL: | |
5baeaac0 | 4345 | if (C_DECL_REGISTER (x) |
3e4093b6 RS |
4346 | && DECL_NONLOCAL (x)) |
4347 | { | |
e697b20f | 4348 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
3e4093b6 | 4349 | { |
0039fa55 AN |
4350 | error |
4351 | ("global register variable %qD used in nested function", x); | |
3e4093b6 RS |
4352 | return false; |
4353 | } | |
509c9d60 | 4354 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); |
3e4093b6 | 4355 | } |
5baeaac0 | 4356 | else if (C_DECL_REGISTER (x)) |
3e4093b6 | 4357 | { |
e697b20f | 4358 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
0039fa55 AN |
4359 | error ("address of global register variable %qD requested", x); |
4360 | else | |
4361 | error ("address of register variable %qD requested", x); | |
4362 | return false; | |
3e4093b6 | 4363 | } |
400fbf9f | 4364 | |
3e4093b6 RS |
4365 | /* drops in */ |
4366 | case FUNCTION_DECL: | |
4367 | TREE_ADDRESSABLE (x) = 1; | |
4368 | /* drops out */ | |
4369 | default: | |
4370 | return true; | |
4371 | } | |
4372 | } | |
4373 | \f | |
2d2e923f MLI |
4374 | /* Convert EXPR to TYPE, warning about conversion problems with |
4375 | constants. SEMANTIC_TYPE is the type this conversion would use | |
4376 | without excess precision. If SEMANTIC_TYPE is NULL, this function | |
4377 | is equivalent to convert_and_check. This function is a wrapper that | |
4378 | handles conversions that may be different than | |
4379 | the usual ones because of excess precision. */ | |
4380 | ||
4381 | static tree | |
4382 | ep_convert_and_check (tree type, tree expr, tree semantic_type) | |
4383 | { | |
4384 | if (TREE_TYPE (expr) == type) | |
4385 | return expr; | |
4386 | ||
4387 | if (!semantic_type) | |
4388 | return convert_and_check (type, expr); | |
4389 | ||
4390 | if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
4391 | && TREE_TYPE (expr) != semantic_type) | |
4392 | { | |
4393 | /* For integers, we need to check the real conversion, not | |
4394 | the conversion to the excess precision type. */ | |
4395 | expr = convert_and_check (semantic_type, expr); | |
4396 | } | |
4397 | /* Result type is the excess precision type, which should be | |
4398 | large enough, so do not check. */ | |
4399 | return convert (type, expr); | |
4400 | } | |
4401 | ||
928c19bb JM |
4402 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If |
4403 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
4404 | if folded to an integer constant then the unselected half may | |
4405 | contain arbitrary operations not normally permitted in constant | |
c2255bc4 | 4406 | expressions. Set the location of the expression to LOC. */ |
400fbf9f JW |
4407 | |
4408 | tree | |
744aa42f | 4409 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, |
d130ae11 ILT |
4410 | tree op1, tree op1_original_type, tree op2, |
4411 | tree op2_original_type) | |
400fbf9f | 4412 | { |
3e4093b6 RS |
4413 | tree type1; |
4414 | tree type2; | |
4415 | enum tree_code code1; | |
4416 | enum tree_code code2; | |
4417 | tree result_type = NULL; | |
2d2e923f | 4418 | tree semantic_result_type = NULL; |
3e4093b6 | 4419 | tree orig_op1 = op1, orig_op2 = op2; |
928c19bb | 4420 | bool int_const, op1_int_operands, op2_int_operands, int_operands; |
4d84fe7c | 4421 | bool ifexp_int_operands; |
928c19bb | 4422 | tree ret; |
400fbf9f | 4423 | |
4d84fe7c JM |
4424 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); |
4425 | if (op1_int_operands) | |
4426 | op1 = remove_c_maybe_const_expr (op1); | |
4427 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
4428 | if (op2_int_operands) | |
4429 | op2 = remove_c_maybe_const_expr (op2); | |
4430 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
4431 | if (ifexp_int_operands) | |
4432 | ifexp = remove_c_maybe_const_expr (ifexp); | |
4433 | ||
3e4093b6 RS |
4434 | /* Promote both alternatives. */ |
4435 | ||
4436 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
4437 | op1 = default_conversion (op1); | |
4438 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
4439 | op2 = default_conversion (op2); | |
4440 | ||
4441 | if (TREE_CODE (ifexp) == ERROR_MARK | |
4442 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
4443 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
400fbf9f | 4444 | return error_mark_node; |
400fbf9f | 4445 | |
3e4093b6 RS |
4446 | type1 = TREE_TYPE (op1); |
4447 | code1 = TREE_CODE (type1); | |
4448 | type2 = TREE_TYPE (op2); | |
4449 | code2 = TREE_CODE (type2); | |
4450 | ||
b1adf557 JM |
4451 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
4452 | In C99 they will be pointers by now. */ | |
4453 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
4454 | { | |
744aa42f | 4455 | error_at (colon_loc, "non-lvalue array in conditional expression"); |
b1adf557 JM |
4456 | return error_mark_node; |
4457 | } | |
4458 | ||
8ce94e44 JM |
4459 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR |
4460 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4461 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4462 | || code1 == COMPLEX_TYPE) | |
4463 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4464 | || code2 == COMPLEX_TYPE)) | |
4465 | { | |
2d2e923f | 4466 | semantic_result_type = c_common_type (type1, type2); |
8ce94e44 JM |
4467 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) |
4468 | { | |
4469 | op1 = TREE_OPERAND (op1, 0); | |
4470 | type1 = TREE_TYPE (op1); | |
4471 | gcc_assert (TREE_CODE (type1) == code1); | |
4472 | } | |
4473 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4474 | { | |
4475 | op2 = TREE_OPERAND (op2, 0); | |
4476 | type2 = TREE_TYPE (op2); | |
4477 | gcc_assert (TREE_CODE (type2) == code2); | |
4478 | } | |
4479 | } | |
4480 | ||
d130ae11 ILT |
4481 | if (warn_cxx_compat) |
4482 | { | |
4483 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
4484 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
4485 | ||
4486 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
4487 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
4488 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
4489 | warning_at (colon_loc, OPT_Wc___compat, | |
4490 | ("different enum types in conditional is " | |
4491 | "invalid in C++: %qT vs %qT"), | |
4492 | t1, t2); | |
4493 | } | |
4494 | ||
3e4093b6 RS |
4495 | /* Quickly detect the usual case where op1 and op2 have the same type |
4496 | after promotion. */ | |
4497 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
400fbf9f | 4498 | { |
3e4093b6 RS |
4499 | if (type1 == type2) |
4500 | result_type = type1; | |
4501 | else | |
4502 | result_type = TYPE_MAIN_VARIANT (type1); | |
4503 | } | |
4504 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
c22cacf3 MS |
4505 | || code1 == COMPLEX_TYPE) |
4506 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4507 | || code2 == COMPLEX_TYPE)) | |
3e4093b6 | 4508 | { |
ccf7f880 | 4509 | result_type = c_common_type (type1, type2); |
0a0b3574 MM |
4510 | do_warn_double_promotion (result_type, type1, type2, |
4511 | "implicit conversion from %qT to %qT to " | |
4512 | "match other result of conditional", | |
4513 | colon_loc); | |
400fbf9f | 4514 | |
3e4093b6 RS |
4515 | /* If -Wsign-compare, warn here if type1 and type2 have |
4516 | different signedness. We'll promote the signed to unsigned | |
4517 | and later code won't know it used to be different. | |
4518 | Do this check on the original types, so that explicit casts | |
4519 | will be considered, but default promotions won't. */ | |
7d882b83 | 4520 | if (c_inhibit_evaluation_warnings == 0) |
ab87f8c8 | 4521 | { |
8df83eae RK |
4522 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
4523 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
400fbf9f | 4524 | |
3e4093b6 RS |
4525 | if (unsigned_op1 ^ unsigned_op2) |
4526 | { | |
6ac01510 ILT |
4527 | bool ovf; |
4528 | ||
3e4093b6 RS |
4529 | /* Do not warn if the result type is signed, since the |
4530 | signed type will only be chosen if it can represent | |
4531 | all the values of the unsigned type. */ | |
3f75a254 | 4532 | if (!TYPE_UNSIGNED (result_type)) |
3e4093b6 | 4533 | /* OK */; |
3e4093b6 | 4534 | else |
928c19bb JM |
4535 | { |
4536 | bool op1_maybe_const = true; | |
4537 | bool op2_maybe_const = true; | |
4538 | ||
4539 | /* Do not warn if the signed quantity is an | |
4540 | unsuffixed integer literal (or some static | |
4541 | constant expression involving such literals) and | |
4542 | it is non-negative. This warning requires the | |
4543 | operands to be folded for best results, so do | |
4544 | that folding in this case even without | |
4545 | warn_sign_compare to avoid warning options | |
4546 | possibly affecting code generation. */ | |
f5178456 RS |
4547 | c_inhibit_evaluation_warnings |
4548 | += (ifexp == truthvalue_false_node); | |
928c19bb JM |
4549 | op1 = c_fully_fold (op1, require_constant_value, |
4550 | &op1_maybe_const); | |
f5178456 RS |
4551 | c_inhibit_evaluation_warnings |
4552 | -= (ifexp == truthvalue_false_node); | |
4553 | ||
4554 | c_inhibit_evaluation_warnings | |
4555 | += (ifexp == truthvalue_true_node); | |
928c19bb JM |
4556 | op2 = c_fully_fold (op2, require_constant_value, |
4557 | &op2_maybe_const); | |
f5178456 RS |
4558 | c_inhibit_evaluation_warnings |
4559 | -= (ifexp == truthvalue_true_node); | |
928c19bb JM |
4560 | |
4561 | if (warn_sign_compare) | |
4562 | { | |
4563 | if ((unsigned_op2 | |
4564 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4565 | || (unsigned_op1 | |
4566 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4567 | /* OK */; | |
4568 | else | |
744aa42f ILT |
4569 | warning_at (colon_loc, OPT_Wsign_compare, |
4570 | ("signed and unsigned type in " | |
4571 | "conditional expression")); | |
928c19bb JM |
4572 | } |
4573 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
e5a94231 | 4574 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 4575 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) |
e5a94231 | 4576 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); |
928c19bb | 4577 | } |
3e4093b6 RS |
4578 | } |
4579 | } | |
4580 | } | |
4581 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4582 | { | |
fcf73884 | 4583 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) |
c1771a20 | 4584 | pedwarn (colon_loc, OPT_Wpedantic, |
fcf73884 | 4585 | "ISO C forbids conditional expr with only one void side"); |
3e4093b6 RS |
4586 | result_type = void_type_node; |
4587 | } | |
4588 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4589 | { | |
36c5e70a BE |
4590 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); |
4591 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4592 | addr_space_t as_common; | |
4593 | ||
744aa42f | 4594 | if (comp_target_types (colon_loc, type1, type2)) |
10bc1b1b | 4595 | result_type = common_pointer_type (type1, type2); |
6aa3c60d | 4596 | else if (null_pointer_constant_p (orig_op1)) |
36c5e70a | 4597 | result_type = type2; |
6aa3c60d | 4598 | else if (null_pointer_constant_p (orig_op2)) |
36c5e70a BE |
4599 | result_type = type1; |
4600 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4601 | { | |
4602 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4603 | "used in conditional expression"); | |
4604 | return error_mark_node; | |
4605 | } | |
267bac10 JM |
4606 | else if (VOID_TYPE_P (TREE_TYPE (type1)) |
4607 | && !TYPE_ATOMIC (TREE_TYPE (type1))) | |
34a80643 | 4608 | { |
fcf73884 | 4609 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
c1771a20 | 4610 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4611 | "ISO C forbids conditional expr between " |
bda67431 | 4612 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4613 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), |
4614 | TREE_TYPE (type2))); | |
34a80643 | 4615 | } |
267bac10 JM |
4616 | else if (VOID_TYPE_P (TREE_TYPE (type2)) |
4617 | && !TYPE_ATOMIC (TREE_TYPE (type2))) | |
1c2a9b35 | 4618 | { |
fcf73884 | 4619 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
c1771a20 | 4620 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4621 | "ISO C forbids conditional expr between " |
bda67431 | 4622 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4623 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), |
4624 | TREE_TYPE (type1))); | |
1c2a9b35 | 4625 | } |
b581b85b NP |
4626 | /* Objective-C pointer comparisons are a bit more lenient. */ |
4627 | else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) | |
4628 | result_type = objc_common_type (type1, type2); | |
34a80643 | 4629 | else |
ab87f8c8 | 4630 | { |
36c5e70a BE |
4631 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
4632 | ||
b581b85b NP |
4633 | pedwarn (colon_loc, 0, |
4634 | "pointer type mismatch in conditional expression"); | |
36c5e70a BE |
4635 | result_type = build_pointer_type |
4636 | (build_qualified_type (void_type_node, qual)); | |
ab87f8c8 | 4637 | } |
3e4093b6 RS |
4638 | } |
4639 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4640 | { | |
6aa3c60d | 4641 | if (!null_pointer_constant_p (orig_op2)) |
744aa42f | 4642 | pedwarn (colon_loc, 0, |
509c9d60 | 4643 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4644 | else |
ab87f8c8 | 4645 | { |
3e4093b6 | 4646 | op2 = null_pointer_node; |
ab87f8c8 | 4647 | } |
3e4093b6 RS |
4648 | result_type = type1; |
4649 | } | |
4650 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4651 | { | |
6aa3c60d | 4652 | if (!null_pointer_constant_p (orig_op1)) |
744aa42f | 4653 | pedwarn (colon_loc, 0, |
509c9d60 | 4654 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4655 | else |
ab87f8c8 | 4656 | { |
3e4093b6 | 4657 | op1 = null_pointer_node; |
ab87f8c8 | 4658 | } |
3e4093b6 RS |
4659 | result_type = type2; |
4660 | } | |
1c2a9b35 | 4661 | |
3e4093b6 RS |
4662 | if (!result_type) |
4663 | { | |
4664 | if (flag_cond_mismatch) | |
4665 | result_type = void_type_node; | |
4666 | else | |
400fbf9f | 4667 | { |
c2255bc4 | 4668 | error_at (colon_loc, "type mismatch in conditional expression"); |
ab87f8c8 | 4669 | return error_mark_node; |
400fbf9f | 4670 | } |
3e4093b6 | 4671 | } |
400fbf9f | 4672 | |
3e4093b6 RS |
4673 | /* Merge const and volatile flags of the incoming types. */ |
4674 | result_type | |
4675 | = build_type_variant (result_type, | |
afbd0665 AS |
4676 | TYPE_READONLY (type1) || TYPE_READONLY (type2), |
4677 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
b6a10c9f | 4678 | |
2d2e923f MLI |
4679 | op1 = ep_convert_and_check (result_type, op1, semantic_result_type); |
4680 | op2 = ep_convert_and_check (result_type, op2, semantic_result_type); | |
b6a10c9f | 4681 | |
928c19bb JM |
4682 | if (ifexp_bcp && ifexp == truthvalue_true_node) |
4683 | { | |
4684 | op2_int_operands = true; | |
4685 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4686 | } | |
4687 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4688 | { | |
4689 | op1_int_operands = true; | |
4690 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4691 | } | |
4d84fe7c | 4692 | int_const = int_operands = (ifexp_int_operands |
928c19bb JM |
4693 | && op1_int_operands |
4694 | && op2_int_operands); | |
4695 | if (int_operands) | |
4696 | { | |
4697 | int_const = ((ifexp == truthvalue_true_node | |
4698 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4699 | && !TREE_OVERFLOW (orig_op1)) | |
4700 | || (ifexp == truthvalue_false_node | |
4701 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4702 | && !TREE_OVERFLOW (orig_op2))); | |
4703 | } | |
4704 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) | |
db3927fb | 4705 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); |
928c19bb JM |
4706 | else |
4707 | { | |
01c7ccbb JM |
4708 | if (int_operands) |
4709 | { | |
4710 | op1 = remove_c_maybe_const_expr (op1); | |
4711 | op2 = remove_c_maybe_const_expr (op2); | |
4712 | } | |
928c19bb JM |
4713 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); |
4714 | if (int_operands) | |
4715 | ret = note_integer_operands (ret); | |
4716 | } | |
2d2e923f MLI |
4717 | if (semantic_result_type) |
4718 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
928c19bb | 4719 | |
c2255bc4 | 4720 | protected_set_expr_location (ret, colon_loc); |
928c19bb | 4721 | return ret; |
3e4093b6 RS |
4722 | } |
4723 | \f | |
487a92fe | 4724 | /* Return a compound expression that performs two expressions and |
c2255bc4 AH |
4725 | returns the value of the second of them. |
4726 | ||
4727 | LOC is the location of the COMPOUND_EXPR. */ | |
400fbf9f | 4728 | |
3e4093b6 | 4729 | tree |
c2255bc4 | 4730 | build_compound_expr (location_t loc, tree expr1, tree expr2) |
3e4093b6 | 4731 | { |
4d84fe7c | 4732 | bool expr1_int_operands, expr2_int_operands; |
8ce94e44 | 4733 | tree eptype = NULL_TREE; |
928c19bb JM |
4734 | tree ret; |
4735 | ||
939b37da BI |
4736 | if (flag_enable_cilkplus |
4737 | && (TREE_CODE (expr1) == CILK_SPAWN_STMT | |
4738 | || TREE_CODE (expr2) == CILK_SPAWN_STMT)) | |
4739 | { | |
4740 | error_at (loc, | |
4741 | "spawned function call cannot be part of a comma expression"); | |
4742 | return error_mark_node; | |
4743 | } | |
4d84fe7c JM |
4744 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); |
4745 | if (expr1_int_operands) | |
4746 | expr1 = remove_c_maybe_const_expr (expr1); | |
4747 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4748 | if (expr2_int_operands) | |
4749 | expr2 = remove_c_maybe_const_expr (expr2); | |
4750 | ||
8ce94e44 JM |
4751 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) |
4752 | expr1 = TREE_OPERAND (expr1, 0); | |
4753 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4754 | { | |
4755 | eptype = TREE_TYPE (expr2); | |
4756 | expr2 = TREE_OPERAND (expr2, 0); | |
4757 | } | |
4758 | ||
3f75a254 | 4759 | if (!TREE_SIDE_EFFECTS (expr1)) |
3e4093b6 RS |
4760 | { |
4761 | /* The left-hand operand of a comma expression is like an expression | |
c5409249 | 4762 | statement: with -Wunused, we should warn if it doesn't have |
3e4093b6 | 4763 | any side-effects, unless it was explicitly cast to (void). */ |
e14a6540 | 4764 | if (warn_unused_value) |
47aecf47 | 4765 | { |
e14a6540 | 4766 | if (VOID_TYPE_P (TREE_TYPE (expr1)) |
1043771b | 4767 | && CONVERT_EXPR_P (expr1)) |
47aecf47 | 4768 | ; /* (void) a, b */ |
e14a6540 JM |
4769 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) |
4770 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
1043771b | 4771 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) |
47aecf47 JM |
4772 | ; /* (void) a, (void) b, c */ |
4773 | else | |
b8698a0f | 4774 | warning_at (loc, OPT_Wunused_value, |
c2255bc4 | 4775 | "left-hand operand of comma expression has no effect"); |
47aecf47 | 4776 | } |
3e4093b6 | 4777 | } |
400fbf9f | 4778 | |
3e4093b6 RS |
4779 | /* With -Wunused, we should also warn if the left-hand operand does have |
4780 | side-effects, but computes a value which is not used. For example, in | |
4781 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4782 | so we should issue a warning. */ | |
4783 | else if (warn_unused_value) | |
c2255bc4 | 4784 | warn_if_unused_value (expr1, loc); |
400fbf9f | 4785 | |
e63d6886 AP |
4786 | if (expr2 == error_mark_node) |
4787 | return error_mark_node; | |
4788 | ||
928c19bb JM |
4789 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
4790 | ||
4791 | if (flag_isoc99 | |
4d84fe7c JM |
4792 | && expr1_int_operands |
4793 | && expr2_int_operands) | |
928c19bb JM |
4794 | ret = note_integer_operands (ret); |
4795 | ||
8ce94e44 JM |
4796 | if (eptype) |
4797 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4798 | ||
c2255bc4 | 4799 | protected_set_expr_location (ret, loc); |
928c19bb | 4800 | return ret; |
3e4093b6 | 4801 | } |
400fbf9f | 4802 | |
67165eb3 ILT |
4803 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to |
4804 | which we are casting. OTYPE is the type of the expression being | |
2ee3cb35 MLI |
4805 | cast. Both TYPE and OTYPE are pointer types. LOC is the location |
4806 | of the cast. -Wcast-qual appeared on the command line. Named | |
4807 | address space qualifiers are not handled here, because they result | |
4808 | in different warnings. */ | |
67165eb3 ILT |
4809 | |
4810 | static void | |
2ee3cb35 | 4811 | handle_warn_cast_qual (location_t loc, tree type, tree otype) |
67165eb3 ILT |
4812 | { |
4813 | tree in_type = type; | |
4814 | tree in_otype = otype; | |
4815 | int added = 0; | |
4816 | int discarded = 0; | |
4817 | bool is_const; | |
4818 | ||
4819 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4820 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4821 | nodes is uninteresting and we stop as soon as we hit a | |
4822 | non-POINTER_TYPE node on either type. */ | |
4823 | do | |
4824 | { | |
4825 | in_otype = TREE_TYPE (in_otype); | |
4826 | in_type = TREE_TYPE (in_type); | |
4827 | ||
4828 | /* GNU C allows cv-qualified function types. 'const' means the | |
4829 | function is very pure, 'volatile' means it can't return. We | |
4830 | need to warn when such qualifiers are added, not when they're | |
4831 | taken away. */ | |
4832 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4833 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
36c5e70a BE |
4834 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) |
4835 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
67165eb3 | 4836 | else |
36c5e70a BE |
4837 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) |
4838 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
67165eb3 ILT |
4839 | } |
4840 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4841 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4842 | ||
4843 | if (added) | |
2ee3cb35 MLI |
4844 | warning_at (loc, OPT_Wcast_qual, |
4845 | "cast adds %q#v qualifier to function type", added); | |
67165eb3 ILT |
4846 | |
4847 | if (discarded) | |
4848 | /* There are qualifiers present in IN_OTYPE that are not present | |
4849 | in IN_TYPE. */ | |
2ee3cb35 MLI |
4850 | warning_at (loc, OPT_Wcast_qual, |
4851 | "cast discards %q#v qualifier from pointer target type", | |
4852 | discarded); | |
67165eb3 ILT |
4853 | |
4854 | if (added || discarded) | |
4855 | return; | |
4856 | ||
4857 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4858 | const value to be changed with no additional warning. We only | |
4859 | issue this warning if T is the same on both sides, and we only | |
4860 | issue the warning if there are the same number of pointers on | |
4861 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4862 | cast is unsafe when a qualifier is added at one level and const | |
4863 | is not present at all outer levels. | |
4864 | ||
4865 | To issue this warning, we check at each level whether the cast | |
4866 | adds new qualifiers not already seen. We don't need to special | |
4867 | case function types, as they won't have the same | |
4868 | TYPE_MAIN_VARIANT. */ | |
4869 | ||
4870 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4871 | return; | |
4872 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4873 | return; | |
4874 | ||
4875 | in_type = type; | |
4876 | in_otype = otype; | |
4877 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4878 | do | |
4879 | { | |
4880 | in_type = TREE_TYPE (in_type); | |
4881 | in_otype = TREE_TYPE (in_otype); | |
4882 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4883 | && !is_const) | |
4884 | { | |
2ee3cb35 MLI |
4885 | warning_at (loc, OPT_Wcast_qual, |
4886 | "to be safe all intermediate pointers in cast from " | |
4887 | "%qT to %qT must be %<const%> qualified", | |
4888 | otype, type); | |
67165eb3 ILT |
4889 | break; |
4890 | } | |
4891 | if (is_const) | |
4892 | is_const = TYPE_READONLY (in_type); | |
4893 | } | |
4894 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4895 | } | |
4896 | ||
b8698a0f | 4897 | /* Build an expression representing a cast to type TYPE of expression EXPR. |
c2255bc4 | 4898 | LOC is the location of the cast-- typically the open paren of the cast. */ |
400fbf9f | 4899 | |
3e4093b6 | 4900 | tree |
c2255bc4 | 4901 | build_c_cast (location_t loc, tree type, tree expr) |
3e4093b6 | 4902 | { |
8ce94e44 JM |
4903 | tree value; |
4904 | ||
4905 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
4906 | expr = TREE_OPERAND (expr, 0); | |
4907 | ||
4908 | value = expr; | |
400fbf9f | 4909 | |
3e4093b6 RS |
4910 | if (type == error_mark_node || expr == error_mark_node) |
4911 | return error_mark_node; | |
400fbf9f | 4912 | |
3e4093b6 RS |
4913 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
4914 | only in <protocol> qualifications. But when constructing cast expressions, | |
4915 | the protocols do matter and must be kept around. */ | |
700686fa ZL |
4916 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) |
4917 | return build1 (NOP_EXPR, type, expr); | |
4918 | ||
4919 | type = TYPE_MAIN_VARIANT (type); | |
400fbf9f | 4920 | |
3e4093b6 RS |
4921 | if (TREE_CODE (type) == ARRAY_TYPE) |
4922 | { | |
c2255bc4 | 4923 | error_at (loc, "cast specifies array type"); |
3e4093b6 RS |
4924 | return error_mark_node; |
4925 | } | |
400fbf9f | 4926 | |
3e4093b6 RS |
4927 | if (TREE_CODE (type) == FUNCTION_TYPE) |
4928 | { | |
c2255bc4 | 4929 | error_at (loc, "cast specifies function type"); |
3e4093b6 RS |
4930 | return error_mark_node; |
4931 | } | |
400fbf9f | 4932 | |
808d6eaa JM |
4933 | if (!VOID_TYPE_P (type)) |
4934 | { | |
4935 | value = require_complete_type (value); | |
4936 | if (value == error_mark_node) | |
4937 | return error_mark_node; | |
4938 | } | |
4939 | ||
3e4093b6 RS |
4940 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
4941 | { | |
fcf73884 MLI |
4942 | if (TREE_CODE (type) == RECORD_TYPE |
4943 | || TREE_CODE (type) == UNION_TYPE) | |
c1771a20 | 4944 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 4945 | "ISO C forbids casting nonscalar to the same type"); |
3e4093b6 RS |
4946 | } |
4947 | else if (TREE_CODE (type) == UNION_TYPE) | |
4948 | { | |
4949 | tree field; | |
400fbf9f | 4950 | |
910ad8de | 4951 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
39ffbac9 VR |
4952 | if (TREE_TYPE (field) != error_mark_node |
4953 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
4954 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3e4093b6 RS |
4955 | break; |
4956 | ||
4957 | if (field) | |
400fbf9f | 4958 | { |
3e4093b6 | 4959 | tree t; |
e616f54d | 4960 | bool maybe_const = true; |
3e4093b6 | 4961 | |
c1771a20 | 4962 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); |
e616f54d JM |
4963 | t = c_fully_fold (value, false, &maybe_const); |
4964 | t = build_constructor_single (type, field, t); | |
4965 | if (!maybe_const) | |
4966 | t = c_wrap_maybe_const (t, true); | |
4967 | t = digest_init (loc, type, t, | |
bbbbb16a | 4968 | NULL_TREE, false, true, 0); |
3e4093b6 RS |
4969 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
4970 | return t; | |
400fbf9f | 4971 | } |
c2255bc4 | 4972 | error_at (loc, "cast to union type from type not present in union"); |
3e4093b6 RS |
4973 | return error_mark_node; |
4974 | } | |
4975 | else | |
4976 | { | |
4977 | tree otype, ovalue; | |
400fbf9f | 4978 | |
3e4093b6 | 4979 | if (type == void_type_node) |
c2255bc4 AH |
4980 | { |
4981 | tree t = build1 (CONVERT_EXPR, type, value); | |
4982 | SET_EXPR_LOCATION (t, loc); | |
4983 | return t; | |
4984 | } | |
400fbf9f | 4985 | |
3e4093b6 | 4986 | otype = TREE_TYPE (value); |
400fbf9f | 4987 | |
3e4093b6 | 4988 | /* Optionally warn about potentially worrisome casts. */ |
3e4093b6 RS |
4989 | if (warn_cast_qual |
4990 | && TREE_CODE (type) == POINTER_TYPE | |
4991 | && TREE_CODE (otype) == POINTER_TYPE) | |
2ee3cb35 | 4992 | handle_warn_cast_qual (loc, type, otype); |
400fbf9f | 4993 | |
36c5e70a BE |
4994 | /* Warn about conversions between pointers to disjoint |
4995 | address spaces. */ | |
4996 | if (TREE_CODE (type) == POINTER_TYPE | |
4997 | && TREE_CODE (otype) == POINTER_TYPE | |
4998 | && !null_pointer_constant_p (value)) | |
4999 | { | |
5000 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
5001 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
5002 | addr_space_t as_common; | |
5003 | ||
5004 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
5005 | { | |
5006 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
5007 | warning_at (loc, 0, "cast to %s address space pointer " | |
5008 | "from disjoint generic address space pointer", | |
5009 | c_addr_space_name (as_to)); | |
5010 | ||
5011 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
5012 | warning_at (loc, 0, "cast to generic address space pointer " | |
5013 | "from disjoint %s address space pointer", | |
5014 | c_addr_space_name (as_from)); | |
5015 | ||
5016 | else | |
5017 | warning_at (loc, 0, "cast to %s address space pointer " | |
5018 | "from disjoint %s address space pointer", | |
5019 | c_addr_space_name (as_to), | |
5020 | c_addr_space_name (as_from)); | |
5021 | } | |
5022 | } | |
5023 | ||
3e4093b6 | 5024 | /* Warn about possible alignment problems. */ |
3176a0c2 | 5025 | if (STRICT_ALIGNMENT |
3e4093b6 RS |
5026 | && TREE_CODE (type) == POINTER_TYPE |
5027 | && TREE_CODE (otype) == POINTER_TYPE | |
5028 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
5029 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
5030 | /* Don't warn about opaque types, where the actual alignment | |
5031 | restriction is unknown. */ | |
5032 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
5033 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
5034 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
5035 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
c2255bc4 AH |
5036 | warning_at (loc, OPT_Wcast_align, |
5037 | "cast increases required alignment of target type"); | |
e9a25f70 | 5038 | |
3176a0c2 | 5039 | if (TREE_CODE (type) == INTEGER_TYPE |
3e4093b6 | 5040 | && TREE_CODE (otype) == POINTER_TYPE |
8d1c7aef | 5041 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) |
c22cacf3 MS |
5042 | /* Unlike conversion of integers to pointers, where the |
5043 | warning is disabled for converting constants because | |
5044 | of cases such as SIG_*, warn about converting constant | |
5045 | pointers to integers. In some cases it may cause unwanted | |
8d1c7aef | 5046 | sign extension, and a warning is appropriate. */ |
c2255bc4 AH |
5047 | warning_at (loc, OPT_Wpointer_to_int_cast, |
5048 | "cast from pointer to integer of different size"); | |
400fbf9f | 5049 | |
3176a0c2 | 5050 | if (TREE_CODE (value) == CALL_EXPR |
3e4093b6 | 5051 | && TREE_CODE (type) != TREE_CODE (otype)) |
c2255bc4 AH |
5052 | warning_at (loc, OPT_Wbad_function_cast, |
5053 | "cast from function call of type %qT " | |
5054 | "to non-matching type %qT", otype, type); | |
400fbf9f | 5055 | |
3176a0c2 | 5056 | if (TREE_CODE (type) == POINTER_TYPE |
3e4093b6 RS |
5057 | && TREE_CODE (otype) == INTEGER_TYPE |
5058 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
5059 | /* Don't warn about converting any constant. */ | |
5060 | && !TREE_CONSTANT (value)) | |
c2255bc4 AH |
5061 | warning_at (loc, |
5062 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
5063 | "of different size"); | |
400fbf9f | 5064 | |
79bedddc SR |
5065 | if (warn_strict_aliasing <= 2) |
5066 | strict_aliasing_warning (otype, type, expr); | |
400fbf9f | 5067 | |
3897f229 JM |
5068 | /* If pedantic, warn for conversions between function and object |
5069 | pointer types, except for converting a null pointer constant | |
5070 | to function pointer type. */ | |
5071 | if (pedantic | |
5072 | && TREE_CODE (type) == POINTER_TYPE | |
5073 | && TREE_CODE (otype) == POINTER_TYPE | |
5074 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
5075 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
c1771a20 | 5076 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 5077 | "conversion of function pointer to object pointer type"); |
3897f229 JM |
5078 | |
5079 | if (pedantic | |
5080 | && TREE_CODE (type) == POINTER_TYPE | |
5081 | && TREE_CODE (otype) == POINTER_TYPE | |
5082 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
5083 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
6aa3c60d | 5084 | && !null_pointer_constant_p (value)) |
c1771a20 | 5085 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 5086 | "conversion of object pointer to function pointer type"); |
3897f229 | 5087 | |
3e4093b6 | 5088 | ovalue = value; |
3e4093b6 | 5089 | value = convert (type, value); |
400fbf9f | 5090 | |
3e4093b6 | 5091 | /* Ignore any integer overflow caused by the cast. */ |
928c19bb | 5092 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) |
3e4093b6 | 5093 | { |
8bcd6380 | 5094 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) |
6414bad6 | 5095 | { |
8bcd6380 RS |
5096 | if (!TREE_OVERFLOW (value)) |
5097 | { | |
5098 | /* Avoid clobbering a shared constant. */ | |
5099 | value = copy_node (value); | |
5100 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
5101 | } | |
6414bad6 | 5102 | } |
8bcd6380 | 5103 | else if (TREE_OVERFLOW (value)) |
d8e1f97b PB |
5104 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ |
5105 | value = build_int_cst_wide (TREE_TYPE (value), | |
5106 | TREE_INT_CST_LOW (value), | |
5107 | TREE_INT_CST_HIGH (value)); | |
3e4093b6 RS |
5108 | } |
5109 | } | |
400fbf9f | 5110 | |
53cd18ec JM |
5111 | /* Don't let a cast be an lvalue. */ |
5112 | if (value == expr) | |
db3927fb | 5113 | value = non_lvalue_loc (loc, value); |
e9a25f70 | 5114 | |
928c19bb JM |
5115 | /* Don't allow the results of casting to floating-point or complex |
5116 | types be confused with actual constants, or casts involving | |
5117 | integer and pointer types other than direct integer-to-integer | |
5118 | and integer-to-pointer be confused with integer constant | |
5119 | expressions and null pointer constants. */ | |
5120 | if (TREE_CODE (value) == REAL_CST | |
5121 | || TREE_CODE (value) == COMPLEX_CST | |
5122 | || (TREE_CODE (value) == INTEGER_CST | |
5123 | && !((TREE_CODE (expr) == INTEGER_CST | |
5124 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
5125 | || TREE_CODE (expr) == REAL_CST | |
5126 | || TREE_CODE (expr) == COMPLEX_CST))) | |
5127 | value = build1 (NOP_EXPR, type, value); | |
5128 | ||
c2255bc4 AH |
5129 | if (CAN_HAVE_LOCATION_P (value)) |
5130 | SET_EXPR_LOCATION (value, loc); | |
3e4093b6 | 5131 | return value; |
400fbf9f JW |
5132 | } |
5133 | ||
c2255bc4 AH |
5134 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the |
5135 | location of the open paren of the cast, or the position of the cast | |
5136 | expr. */ | |
3e4093b6 | 5137 | tree |
c2255bc4 | 5138 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) |
400fbf9f | 5139 | { |
f8893e47 | 5140 | tree type; |
928c19bb JM |
5141 | tree type_expr = NULL_TREE; |
5142 | bool type_expr_const = true; | |
5143 | tree ret; | |
3e4093b6 | 5144 | int saved_wsp = warn_strict_prototypes; |
c5c76735 | 5145 | |
3e4093b6 RS |
5146 | /* This avoids warnings about unprototyped casts on |
5147 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
5148 | if (TREE_CODE (expr) == INTEGER_CST) | |
5149 | warn_strict_prototypes = 0; | |
928c19bb | 5150 | type = groktypename (type_name, &type_expr, &type_expr_const); |
3e4093b6 | 5151 | warn_strict_prototypes = saved_wsp; |
c5c76735 | 5152 | |
c2255bc4 | 5153 | ret = build_c_cast (loc, type, expr); |
928c19bb JM |
5154 | if (type_expr) |
5155 | { | |
9f33203d JM |
5156 | bool inner_expr_const = true; |
5157 | ret = c_fully_fold (ret, require_constant_value, &inner_expr_const); | |
928c19bb | 5158 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); |
9f33203d JM |
5159 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const |
5160 | && inner_expr_const); | |
c2255bc4 | 5161 | SET_EXPR_LOCATION (ret, loc); |
928c19bb | 5162 | } |
24b97832 ILT |
5163 | |
5164 | if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) | |
5165 | SET_EXPR_LOCATION (ret, loc); | |
5166 | ||
9e5b2115 PB |
5167 | /* C++ does not permits types to be defined in a cast, but it |
5168 | allows references to incomplete types. */ | |
5169 | if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) | |
24b97832 ILT |
5170 | warning_at (loc, OPT_Wc___compat, |
5171 | "defining a type in a cast is invalid in C++"); | |
5172 | ||
928c19bb | 5173 | return ret; |
400fbf9f | 5174 | } |
3e4093b6 RS |
5175 | \f |
5176 | /* Build an assignment expression of lvalue LHS from value RHS. | |
32e8bb8e ILT |
5177 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which |
5178 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
3e4093b6 RS |
5179 | MODIFYCODE is the code for a binary operator that we use |
5180 | to combine the old value of LHS with RHS to get the new value. | |
c9f9eb5d | 5181 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
bbbbb16a ILT |
5182 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, |
5183 | which may differ from TREE_TYPE (RHS) for an enum value. | |
c9f9eb5d | 5184 | |
c2255bc4 AH |
5185 | LOCATION is the location of the MODIFYCODE operator. |
5186 | RHS_LOC is the location of the RHS. */ | |
2f6e4e97 | 5187 | |
3e4093b6 | 5188 | tree |
32e8bb8e | 5189 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, |
b8698a0f | 5190 | enum tree_code modifycode, |
c2255bc4 | 5191 | location_t rhs_loc, tree rhs, tree rhs_origtype) |
400fbf9f | 5192 | { |
3e4093b6 RS |
5193 | tree result; |
5194 | tree newrhs; | |
8ce94e44 | 5195 | tree rhs_semantic_type = NULL_TREE; |
3e4093b6 RS |
5196 | tree lhstype = TREE_TYPE (lhs); |
5197 | tree olhstype = lhstype; | |
928c19bb | 5198 | bool npc; |
267bac10 | 5199 | bool is_atomic_op; |
e9a25f70 | 5200 | |
3e4093b6 RS |
5201 | /* Types that aren't fully specified cannot be used in assignments. */ |
5202 | lhs = require_complete_type (lhs); | |
e9a25f70 | 5203 | |
3e4093b6 RS |
5204 | /* Avoid duplicate error messages from operands that had errors. */ |
5205 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
5206 | return error_mark_node; | |
400fbf9f | 5207 | |
46a88c12 | 5208 | /* For ObjC properties, defer this check. */ |
7bd11157 | 5209 | if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) |
c0bcacec VR |
5210 | return error_mark_node; |
5211 | ||
267bac10 JM |
5212 | is_atomic_op = really_atomic_lvalue (lhs); |
5213 | ||
8ce94e44 JM |
5214 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5215 | { | |
5216 | rhs_semantic_type = TREE_TYPE (rhs); | |
5217 | rhs = TREE_OPERAND (rhs, 0); | |
5218 | } | |
5219 | ||
3e4093b6 | 5220 | newrhs = rhs; |
400fbf9f | 5221 | |
928c19bb JM |
5222 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) |
5223 | { | |
5224 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
c2255bc4 | 5225 | lhs_origtype, modifycode, rhs_loc, rhs, |
32e8bb8e | 5226 | rhs_origtype); |
928c19bb JM |
5227 | if (inner == error_mark_node) |
5228 | return error_mark_node; | |
5229 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
5230 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
5231 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
5232 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
5233 | protected_set_expr_location (result, location); | |
5234 | return result; | |
5235 | } | |
5236 | ||
3e4093b6 RS |
5237 | /* If a binary op has been requested, combine the old LHS value with the RHS |
5238 | producing the value we should actually store into the LHS. */ | |
5239 | ||
5240 | if (modifycode != NOP_EXPR) | |
400fbf9f | 5241 | { |
928c19bb | 5242 | lhs = c_fully_fold (lhs, false, NULL); |
3e4093b6 | 5243 | lhs = stabilize_reference (lhs); |
bbbbb16a | 5244 | |
267bac10 JM |
5245 | /* Construct the RHS for any non-atomic compound assignemnt. */ |
5246 | if (!is_atomic_op) | |
5247 | { | |
5248 | newrhs = build_binary_op (location, | |
5249 | modifycode, lhs, rhs, 1); | |
5250 | ||
5251 | /* The original type of the right hand side is no longer | |
5252 | meaningful. */ | |
5253 | rhs_origtype = NULL_TREE; | |
5254 | } | |
400fbf9f | 5255 | } |
400fbf9f | 5256 | |
668ea4b1 IS |
5257 | if (c_dialect_objc ()) |
5258 | { | |
46a88c12 NP |
5259 | /* Check if we are modifying an Objective-C property reference; |
5260 | if so, we need to generate setter calls. */ | |
5261 | result = objc_maybe_build_modify_expr (lhs, newrhs); | |
668ea4b1 IS |
5262 | if (result) |
5263 | return result; | |
46a88c12 NP |
5264 | |
5265 | /* Else, do the check that we postponed for Objective-C. */ | |
7bd11157 | 5266 | if (!lvalue_or_else (location, lhs, lv_assign)) |
668ea4b1 IS |
5267 | return error_mark_node; |
5268 | } | |
5269 | ||
9bf24266 | 5270 | /* Give an error for storing in something that is 'const'. */ |
bbbd6700 | 5271 | |
f37acdf9 | 5272 | if (TYPE_READONLY (lhstype) |
3e4093b6 RS |
5273 | || ((TREE_CODE (lhstype) == RECORD_TYPE |
5274 | || TREE_CODE (lhstype) == UNION_TYPE) | |
5275 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
953ff289 | 5276 | { |
c02065fc | 5277 | readonly_error (location, lhs, lv_assign); |
953ff289 DN |
5278 | return error_mark_node; |
5279 | } | |
f37acdf9 JM |
5280 | else if (TREE_READONLY (lhs)) |
5281 | readonly_warning (lhs, lv_assign); | |
bbbd6700 | 5282 | |
3e4093b6 RS |
5283 | /* If storing into a structure or union member, |
5284 | it has probably been given type `int'. | |
5285 | Compute the type that would go with | |
5286 | the actual amount of storage the member occupies. */ | |
bbbd6700 | 5287 | |
3e4093b6 RS |
5288 | if (TREE_CODE (lhs) == COMPONENT_REF |
5289 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
5290 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
5291 | || TREE_CODE (lhstype) == REAL_TYPE | |
5292 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
5293 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
400fbf9f | 5294 | |
3e4093b6 RS |
5295 | /* If storing in a field that is in actuality a short or narrower than one, |
5296 | we must store in the field in its actual type. */ | |
5297 | ||
5298 | if (lhstype != TREE_TYPE (lhs)) | |
5299 | { | |
5300 | lhs = copy_node (lhs); | |
5301 | TREE_TYPE (lhs) = lhstype; | |
400fbf9f | 5302 | } |
400fbf9f | 5303 | |
32e8bb8e ILT |
5304 | /* Issue -Wc++-compat warnings about an assignment to an enum type |
5305 | when LHS does not have its original type. This happens for, | |
5306 | e.g., an enum bitfield in a struct. */ | |
5307 | if (warn_cxx_compat | |
5308 | && lhs_origtype != NULL_TREE | |
5309 | && lhs_origtype != lhstype | |
5310 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
5311 | { | |
5312 | tree checktype = (rhs_origtype != NULL_TREE | |
5313 | ? rhs_origtype | |
5314 | : TREE_TYPE (rhs)); | |
5315 | if (checktype != error_mark_node | |
267bac10 JM |
5316 | && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype) |
5317 | || (is_atomic_op && modifycode != NOP_EXPR))) | |
32e8bb8e ILT |
5318 | warning_at (location, OPT_Wc___compat, |
5319 | "enum conversion in assignment is invalid in C++"); | |
5320 | } | |
5321 | ||
267bac10 JM |
5322 | /* If the lhs is atomic, remove that qualifier. */ |
5323 | if (is_atomic_op) | |
5324 | { | |
5325 | lhstype = build_qualified_type (lhstype, | |
5326 | (TYPE_QUALS (lhstype) | |
5327 | & ~TYPE_QUAL_ATOMIC)); | |
5328 | olhstype = build_qualified_type (olhstype, | |
5329 | (TYPE_QUALS (lhstype) | |
5330 | & ~TYPE_QUAL_ATOMIC)); | |
5331 | } | |
5332 | ||
8ce94e44 JM |
5333 | /* Convert new value to destination type. Fold it first, then |
5334 | restore any excess precision information, for the sake of | |
5335 | conversion warnings. */ | |
400fbf9f | 5336 | |
267bac10 JM |
5337 | if (!(is_atomic_op && modifycode != NOP_EXPR)) |
5338 | { | |
5339 | npc = null_pointer_constant_p (newrhs); | |
5340 | newrhs = c_fully_fold (newrhs, false, NULL); | |
5341 | if (rhs_semantic_type) | |
5342 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
5343 | newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype, | |
5344 | ic_assign, npc, NULL_TREE, | |
5345 | NULL_TREE, 0); | |
5346 | if (TREE_CODE (newrhs) == ERROR_MARK) | |
5347 | return error_mark_node; | |
5348 | } | |
400fbf9f | 5349 | |
6e955430 ZL |
5350 | /* Emit ObjC write barrier, if necessary. */ |
5351 | if (c_dialect_objc () && flag_objc_gc) | |
5352 | { | |
5353 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
5354 | if (result) | |
c9f9eb5d AH |
5355 | { |
5356 | protected_set_expr_location (result, location); | |
5357 | return result; | |
5358 | } | |
6e955430 ZL |
5359 | } |
5360 | ||
ea4b7848 | 5361 | /* Scan operands. */ |
400fbf9f | 5362 | |
267bac10 JM |
5363 | if (is_atomic_op) |
5364 | result = build_atomic_assign (location, lhs, modifycode, newrhs, false); | |
5365 | else | |
5366 | { | |
5367 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); | |
5368 | TREE_SIDE_EFFECTS (result) = 1; | |
5369 | protected_set_expr_location (result, location); | |
5370 | } | |
400fbf9f | 5371 | |
3e4093b6 RS |
5372 | /* If we got the LHS in a different type for storing in, |
5373 | convert the result back to the nominal type of LHS | |
5374 | so that the value we return always has the same type | |
5375 | as the LHS argument. */ | |
e855c5ce | 5376 | |
3e4093b6 RS |
5377 | if (olhstype == TREE_TYPE (result)) |
5378 | return result; | |
c9f9eb5d | 5379 | |
744aa42f ILT |
5380 | result = convert_for_assignment (location, olhstype, result, rhs_origtype, |
5381 | ic_assign, false, NULL_TREE, NULL_TREE, 0); | |
c9f9eb5d AH |
5382 | protected_set_expr_location (result, location); |
5383 | return result; | |
3e4093b6 RS |
5384 | } |
5385 | \f | |
478a1c5b ILT |
5386 | /* Return whether STRUCT_TYPE has an anonymous field with type TYPE. |
5387 | This is used to implement -fplan9-extensions. */ | |
5388 | ||
5389 | static bool | |
5390 | find_anonymous_field_with_type (tree struct_type, tree type) | |
5391 | { | |
5392 | tree field; | |
5393 | bool found; | |
5394 | ||
5395 | gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE | |
5396 | || TREE_CODE (struct_type) == UNION_TYPE); | |
5397 | found = false; | |
5398 | for (field = TYPE_FIELDS (struct_type); | |
5399 | field != NULL_TREE; | |
5400 | field = TREE_CHAIN (field)) | |
5401 | { | |
267bac10 JM |
5402 | tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) |
5403 | ? c_build_qualified_type (TREE_TYPE (field), | |
5404 | TYPE_QUAL_ATOMIC) | |
5405 | : TYPE_MAIN_VARIANT (TREE_TYPE (field))); | |
478a1c5b | 5406 | if (DECL_NAME (field) == NULL |
267bac10 | 5407 | && comptypes (type, fieldtype)) |
478a1c5b ILT |
5408 | { |
5409 | if (found) | |
5410 | return false; | |
5411 | found = true; | |
5412 | } | |
5413 | else if (DECL_NAME (field) == NULL | |
5414 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
5415 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
5416 | && find_anonymous_field_with_type (TREE_TYPE (field), type)) | |
5417 | { | |
5418 | if (found) | |
5419 | return false; | |
5420 | found = true; | |
5421 | } | |
5422 | } | |
5423 | return found; | |
5424 | } | |
5425 | ||
5426 | /* RHS is an expression whose type is pointer to struct. If there is | |
5427 | an anonymous field in RHS with type TYPE, then return a pointer to | |
5428 | that field in RHS. This is used with -fplan9-extensions. This | |
5429 | returns NULL if no conversion could be found. */ | |
5430 | ||
5431 | static tree | |
5432 | convert_to_anonymous_field (location_t location, tree type, tree rhs) | |
5433 | { | |
5434 | tree rhs_struct_type, lhs_main_type; | |
5435 | tree field, found_field; | |
5436 | bool found_sub_field; | |
5437 | tree ret; | |
5438 | ||
5439 | gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); | |
5440 | rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); | |
5441 | gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE | |
5442 | || TREE_CODE (rhs_struct_type) == UNION_TYPE); | |
5443 | ||
5444 | gcc_assert (POINTER_TYPE_P (type)); | |
267bac10 JM |
5445 | lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type)) |
5446 | ? c_build_qualified_type (TREE_TYPE (type), | |
5447 | TYPE_QUAL_ATOMIC) | |
5448 | : TYPE_MAIN_VARIANT (TREE_TYPE (type))); | |
478a1c5b ILT |
5449 | |
5450 | found_field = NULL_TREE; | |
5451 | found_sub_field = false; | |
5452 | for (field = TYPE_FIELDS (rhs_struct_type); | |
5453 | field != NULL_TREE; | |
5454 | field = TREE_CHAIN (field)) | |
5455 | { | |
5456 | if (DECL_NAME (field) != NULL_TREE | |
5457 | || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE | |
5458 | && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)) | |
5459 | continue; | |
267bac10 JM |
5460 | tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field)) |
5461 | ? c_build_qualified_type (TREE_TYPE (field), | |
5462 | TYPE_QUAL_ATOMIC) | |
5463 | : TYPE_MAIN_VARIANT (TREE_TYPE (field))); | |
5464 | if (comptypes (lhs_main_type, fieldtype)) | |
478a1c5b ILT |
5465 | { |
5466 | if (found_field != NULL_TREE) | |
5467 | return NULL_TREE; | |
5468 | found_field = field; | |
5469 | } | |
5470 | else if (find_anonymous_field_with_type (TREE_TYPE (field), | |
5471 | lhs_main_type)) | |
5472 | { | |
5473 | if (found_field != NULL_TREE) | |
5474 | return NULL_TREE; | |
5475 | found_field = field; | |
5476 | found_sub_field = true; | |
5477 | } | |
5478 | } | |
5479 | ||
5480 | if (found_field == NULL_TREE) | |
5481 | return NULL_TREE; | |
5482 | ||
5483 | ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), | |
5484 | build_fold_indirect_ref (rhs), found_field, | |
5485 | NULL_TREE); | |
5486 | ret = build_fold_addr_expr_loc (location, ret); | |
5487 | ||
5488 | if (found_sub_field) | |
5489 | { | |
5490 | ret = convert_to_anonymous_field (location, type, ret); | |
5491 | gcc_assert (ret != NULL_TREE); | |
5492 | } | |
5493 | ||
5494 | return ret; | |
5495 | } | |
5496 | ||
bbbbb16a ILT |
5497 | /* Convert value RHS to type TYPE as preparation for an assignment to |
5498 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
5499 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
5500 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
5501 | constant before any folding. | |
3e4093b6 RS |
5502 | The real work of conversion is done by `convert'. |
5503 | The purpose of this function is to generate error messages | |
5504 | for assignments that are not allowed in C. | |
2ac2f164 JM |
5505 | ERRTYPE says whether it is argument passing, assignment, |
5506 | initialization or return. | |
2f6e4e97 | 5507 | |
c2255bc4 | 5508 | LOCATION is the location of the RHS. |
2ac2f164 | 5509 | FUNCTION is a tree for the function being called. |
3e4093b6 | 5510 | PARMNUM is the number of the argument, for printing in error messages. */ |
cb3ca04e | 5511 | |
3e4093b6 | 5512 | static tree |
744aa42f ILT |
5513 | convert_for_assignment (location_t location, tree type, tree rhs, |
5514 | tree origtype, enum impl_conv errtype, | |
5515 | bool null_pointer_constant, tree fundecl, | |
5516 | tree function, int parmnum) | |
3e4093b6 RS |
5517 | { |
5518 | enum tree_code codel = TREE_CODE (type); | |
8ce94e44 | 5519 | tree orig_rhs = rhs; |
3e4093b6 RS |
5520 | tree rhstype; |
5521 | enum tree_code coder; | |
2ac2f164 | 5522 | tree rname = NULL_TREE; |
58393038 | 5523 | bool objc_ok = false; |
2ac2f164 | 5524 | |
6b4ef5c1 | 5525 | if (errtype == ic_argpass) |
2ac2f164 JM |
5526 | { |
5527 | tree selector; | |
5528 | /* Change pointer to function to the function itself for | |
5529 | diagnostics. */ | |
5530 | if (TREE_CODE (function) == ADDR_EXPR | |
5531 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
5532 | function = TREE_OPERAND (function, 0); | |
5533 | ||
5534 | /* Handle an ObjC selector specially for diagnostics. */ | |
5535 | selector = objc_message_selector (); | |
5536 | rname = function; | |
5537 | if (selector && parmnum > 2) | |
5538 | { | |
5539 | rname = selector; | |
5540 | parmnum -= 2; | |
5541 | } | |
5542 | } | |
5543 | ||
5544 | /* This macro is used to emit diagnostics to ensure that all format | |
5545 | strings are complete sentences, visible to gettext and checked at | |
5546 | compile time. */ | |
c2255bc4 | 5547 | #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \ |
1e053dfe MLI |
5548 | do { \ |
5549 | switch (errtype) \ | |
5550 | { \ | |
5551 | case ic_argpass: \ | |
5552 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \ | |
c2255bc4 AH |
5553 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5554 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
1e053dfe MLI |
5555 | "expected %qT but argument is of type %qT", \ |
5556 | type, rhstype); \ | |
5557 | break; \ | |
1e053dfe MLI |
5558 | case ic_assign: \ |
5559 | pedwarn (LOCATION, OPT, AS); \ | |
5560 | break; \ | |
5561 | case ic_init: \ | |
6a8f4e12 | 5562 | pedwarn_init (LOCATION, OPT, IN); \ |
1e053dfe MLI |
5563 | break; \ |
5564 | case ic_return: \ | |
c2255bc4 | 5565 | pedwarn (LOCATION, OPT, RE); \ |
1e053dfe MLI |
5566 | break; \ |
5567 | default: \ | |
5568 | gcc_unreachable (); \ | |
5569 | } \ | |
2ac2f164 | 5570 | } while (0) |
cb3ca04e | 5571 | |
49706e39 MLI |
5572 | /* This macro is used to emit diagnostics to ensure that all format |
5573 | strings are complete sentences, visible to gettext and checked at | |
5574 | compile time. It is the same as WARN_FOR_ASSIGNMENT but with an | |
5575 | extra parameter to enumerate qualifiers. */ | |
5576 | ||
5577 | #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \ | |
5578 | do { \ | |
5579 | switch (errtype) \ | |
5580 | { \ | |
5581 | case ic_argpass: \ | |
5582 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \ | |
5583 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ | |
5584 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
5585 | "expected %qT but argument is of type %qT", \ | |
5586 | type, rhstype); \ | |
5587 | break; \ | |
5588 | case ic_assign: \ | |
5589 | pedwarn (LOCATION, OPT, AS, QUALS); \ | |
5590 | break; \ | |
5591 | case ic_init: \ | |
5592 | pedwarn (LOCATION, OPT, IN, QUALS); \ | |
5593 | break; \ | |
5594 | case ic_return: \ | |
5595 | pedwarn (LOCATION, OPT, RE, QUALS); \ | |
5596 | break; \ | |
5597 | default: \ | |
5598 | gcc_unreachable (); \ | |
5599 | } \ | |
5600 | } while (0) | |
5601 | ||
8ce94e44 JM |
5602 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5603 | rhs = TREE_OPERAND (rhs, 0); | |
5604 | ||
3e4093b6 RS |
5605 | rhstype = TREE_TYPE (rhs); |
5606 | coder = TREE_CODE (rhstype); | |
5607 | ||
5608 | if (coder == ERROR_MARK) | |
5609 | return error_mark_node; | |
5610 | ||
58393038 ZL |
5611 | if (c_dialect_objc ()) |
5612 | { | |
5613 | int parmno; | |
5614 | ||
5615 | switch (errtype) | |
5616 | { | |
5617 | case ic_return: | |
5618 | parmno = 0; | |
5619 | break; | |
5620 | ||
5621 | case ic_assign: | |
5622 | parmno = -1; | |
5623 | break; | |
5624 | ||
5625 | case ic_init: | |
5626 | parmno = -2; | |
5627 | break; | |
5628 | ||
5629 | default: | |
5630 | parmno = parmnum; | |
5631 | break; | |
5632 | } | |
5633 | ||
5634 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
5635 | } | |
5636 | ||
bbbbb16a ILT |
5637 | if (warn_cxx_compat) |
5638 | { | |
5639 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
5640 | if (checktype != error_mark_node | |
5641 | && TREE_CODE (type) == ENUMERAL_TYPE | |
5642 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
5643 | { | |
81f40b79 ILT |
5644 | WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat, |
5645 | G_("enum conversion when passing argument " | |
5646 | "%d of %qE is invalid in C++"), | |
5647 | G_("enum conversion in assignment is " | |
5648 | "invalid in C++"), | |
5649 | G_("enum conversion in initialization is " | |
5650 | "invalid in C++"), | |
5651 | G_("enum conversion in return is " | |
5652 | "invalid in C++")); | |
bbbbb16a ILT |
5653 | } |
5654 | } | |
5655 | ||
3e4093b6 | 5656 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) |
59c0753d | 5657 | return rhs; |
3e4093b6 RS |
5658 | |
5659 | if (coder == VOID_TYPE) | |
400fbf9f | 5660 | { |
6dcc04b0 JM |
5661 | /* Except for passing an argument to an unprototyped function, |
5662 | this is a constraint violation. When passing an argument to | |
5663 | an unprototyped function, it is compile-time undefined; | |
5664 | making it a constraint in that case was rejected in | |
5665 | DR#252. */ | |
c2255bc4 | 5666 | error_at (location, "void value not ignored as it ought to be"); |
3e4093b6 | 5667 | return error_mark_node; |
400fbf9f | 5668 | } |
808d6eaa JM |
5669 | rhs = require_complete_type (rhs); |
5670 | if (rhs == error_mark_node) | |
5671 | return error_mark_node; | |
cd192ccc MS |
5672 | /* A non-reference type can convert to a reference. This handles |
5673 | va_start, va_copy and possibly port built-ins. */ | |
5674 | if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) | |
400fbf9f | 5675 | { |
3e4093b6 | 5676 | if (!lvalue_p (rhs)) |
400fbf9f | 5677 | { |
c2255bc4 | 5678 | error_at (location, "cannot pass rvalue to reference parameter"); |
3e4093b6 | 5679 | return error_mark_node; |
400fbf9f | 5680 | } |
3e4093b6 RS |
5681 | if (!c_mark_addressable (rhs)) |
5682 | return error_mark_node; | |
5683 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
c2255bc4 | 5684 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5685 | |
cd192ccc MS |
5686 | rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)), |
5687 | rhs, origtype, errtype, null_pointer_constant, | |
5688 | fundecl, function, parmnum); | |
5689 | if (rhs == error_mark_node) | |
5690 | return error_mark_node; | |
3e4093b6 RS |
5691 | |
5692 | rhs = build1 (NOP_EXPR, type, rhs); | |
c2255bc4 | 5693 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5694 | return rhs; |
400fbf9f | 5695 | } |
3e4093b6 | 5696 | /* Some types can interconvert without explicit casts. */ |
3274deff | 5697 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE |
00c8e9f6 | 5698 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) |
3e4093b6 RS |
5699 | return convert (type, rhs); |
5700 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
5701 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
ab22c1fa | 5702 | || codel == FIXED_POINT_TYPE |
3e4093b6 RS |
5703 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE |
5704 | || codel == BOOLEAN_TYPE) | |
5705 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
ab22c1fa | 5706 | || coder == FIXED_POINT_TYPE |
3e4093b6 RS |
5707 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE |
5708 | || coder == BOOLEAN_TYPE)) | |
928c19bb JM |
5709 | { |
5710 | tree ret; | |
5711 | bool save = in_late_binary_op; | |
dfe776dd | 5712 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) |
928c19bb | 5713 | in_late_binary_op = true; |
8ce94e44 | 5714 | ret = convert_and_check (type, orig_rhs); |
dfe776dd | 5715 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) |
928c19bb JM |
5716 | in_late_binary_op = save; |
5717 | return ret; | |
5718 | } | |
400fbf9f | 5719 | |
79077aea JJ |
5720 | /* Aggregates in different TUs might need conversion. */ |
5721 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
5722 | && codel == coder | |
5723 | && comptypes (type, rhstype)) | |
5724 | return convert_and_check (type, rhs); | |
5725 | ||
ebf0bf7f | 5726 | /* Conversion to a transparent union or record from its member types. |
3e4093b6 | 5727 | This applies only to function arguments. */ |
ebf0bf7f JJ |
5728 | if (((codel == UNION_TYPE || codel == RECORD_TYPE) |
5729 | && TYPE_TRANSPARENT_AGGR (type)) | |
6b4ef5c1 | 5730 | && errtype == ic_argpass) |
400fbf9f | 5731 | { |
0257e383 | 5732 | tree memb, marginal_memb = NULL_TREE; |
3e4093b6 | 5733 | |
910ad8de | 5734 | for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) |
400fbf9f | 5735 | { |
0257e383 | 5736 | tree memb_type = TREE_TYPE (memb); |
400fbf9f | 5737 | |
3e4093b6 | 5738 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
132da1a5 | 5739 | TYPE_MAIN_VARIANT (rhstype))) |
3e4093b6 | 5740 | break; |
e58cd767 | 5741 | |
3e4093b6 RS |
5742 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
5743 | continue; | |
2f6e4e97 | 5744 | |
3e4093b6 RS |
5745 | if (coder == POINTER_TYPE) |
5746 | { | |
5747 | tree ttl = TREE_TYPE (memb_type); | |
5748 | tree ttr = TREE_TYPE (rhstype); | |
400fbf9f | 5749 | |
3e4093b6 RS |
5750 | /* Any non-function converts to a [const][volatile] void * |
5751 | and vice versa; otherwise, targets must be the same. | |
5752 | Meanwhile, the lhs target must have all the qualifiers of | |
5753 | the rhs. */ | |
267bac10 JM |
5754 | if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) |
5755 | || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) | |
744aa42f | 5756 | || comp_target_types (location, memb_type, rhstype)) |
3e4093b6 | 5757 | { |
267bac10 JM |
5758 | int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC; |
5759 | int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC; | |
3e4093b6 | 5760 | /* If this type won't generate any warnings, use it. */ |
267bac10 | 5761 | if (lquals == rquals |
3e4093b6 RS |
5762 | || ((TREE_CODE (ttr) == FUNCTION_TYPE |
5763 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
267bac10 JM |
5764 | ? ((lquals | rquals) == rquals) |
5765 | : ((lquals | rquals) == lquals))) | |
3e4093b6 | 5766 | break; |
400fbf9f | 5767 | |
3e4093b6 | 5768 | /* Keep looking for a better type, but remember this one. */ |
0257e383 RH |
5769 | if (!marginal_memb) |
5770 | marginal_memb = memb; | |
3e4093b6 RS |
5771 | } |
5772 | } | |
82bde854 | 5773 | |
3e4093b6 | 5774 | /* Can convert integer zero to any pointer type. */ |
928c19bb | 5775 | if (null_pointer_constant) |
3e4093b6 RS |
5776 | { |
5777 | rhs = null_pointer_node; | |
5778 | break; | |
5779 | } | |
5780 | } | |
400fbf9f | 5781 | |
0257e383 | 5782 | if (memb || marginal_memb) |
3e4093b6 | 5783 | { |
0257e383 | 5784 | if (!memb) |
3e4093b6 RS |
5785 | { |
5786 | /* We have only a marginally acceptable member type; | |
5787 | it needs a warning. */ | |
0257e383 | 5788 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); |
3e4093b6 | 5789 | tree ttr = TREE_TYPE (rhstype); |
714a0864 | 5790 | |
3e4093b6 RS |
5791 | /* Const and volatile mean something different for function |
5792 | types, so the usual warnings are not appropriate. */ | |
5793 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
5794 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5795 | { | |
5796 | /* Because const and volatile on functions are | |
5797 | restrictions that say the function will not do | |
5798 | certain things, it is okay to use a const or volatile | |
5799 | function where an ordinary one is wanted, but not | |
5800 | vice-versa. */ | |
36c5e70a BE |
5801 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5802 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
49706e39 | 5803 | WARN_FOR_QUALIFIERS (location, 0, |
509c9d60 | 5804 | G_("passing argument %d of %qE " |
49706e39 | 5805 | "makes %q#v qualified function " |
2ac2f164 | 5806 | "pointer from unqualified"), |
49706e39 | 5807 | G_("assignment makes %q#v qualified " |
2ac2f164 JM |
5808 | "function pointer from " |
5809 | "unqualified"), | |
49706e39 | 5810 | G_("initialization makes %q#v qualified " |
2ac2f164 JM |
5811 | "function pointer from " |
5812 | "unqualified"), | |
49706e39 MLI |
5813 | G_("return makes %q#v qualified function " |
5814 | "pointer from unqualified"), | |
5815 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 | 5816 | } |
36c5e70a BE |
5817 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5818 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
49706e39 | 5819 | WARN_FOR_QUALIFIERS (location, 0, |
509c9d60 | 5820 | G_("passing argument %d of %qE discards " |
49706e39 MLI |
5821 | "%qv qualifier from pointer target type"), |
5822 | G_("assignment discards %qv qualifier " | |
2ac2f164 | 5823 | "from pointer target type"), |
49706e39 | 5824 | G_("initialization discards %qv qualifier " |
2ac2f164 | 5825 | "from pointer target type"), |
49706e39 MLI |
5826 | G_("return discards %qv qualifier from " |
5827 | "pointer target type"), | |
5828 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
0257e383 RH |
5829 | |
5830 | memb = marginal_memb; | |
3e4093b6 | 5831 | } |
400fbf9f | 5832 | |
fcf73884 | 5833 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) |
c1771a20 | 5834 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 5835 | "ISO C prohibits argument conversion to union type"); |
0e7c47fa | 5836 | |
db3927fb | 5837 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); |
0257e383 | 5838 | return build_constructor_single (type, memb, rhs); |
3e4093b6 | 5839 | } |
0e7c47fa RK |
5840 | } |
5841 | ||
3e4093b6 RS |
5842 | /* Conversions among pointers */ |
5843 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
5844 | && (coder == codel)) | |
400fbf9f | 5845 | { |
3e4093b6 RS |
5846 | tree ttl = TREE_TYPE (type); |
5847 | tree ttr = TREE_TYPE (rhstype); | |
46df2823 JM |
5848 | tree mvl = ttl; |
5849 | tree mvr = ttr; | |
3e4093b6 | 5850 | bool is_opaque_pointer; |
264fa2db | 5851 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
36c5e70a BE |
5852 | addr_space_t asl; |
5853 | addr_space_t asr; | |
400fbf9f | 5854 | |
46df2823 | 5855 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
267bac10 JM |
5856 | mvl = (TYPE_ATOMIC (mvl) |
5857 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), | |
5858 | TYPE_QUAL_ATOMIC) | |
5859 | : TYPE_MAIN_VARIANT (mvl)); | |
46df2823 | 5860 | if (TREE_CODE (mvr) != ARRAY_TYPE) |
267bac10 JM |
5861 | mvr = (TYPE_ATOMIC (mvr) |
5862 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), | |
5863 | TYPE_QUAL_ATOMIC) | |
5864 | : TYPE_MAIN_VARIANT (mvr)); | |
3e4093b6 | 5865 | /* Opaque pointers are treated like void pointers. */ |
f83c7f63 | 5866 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); |
c22cacf3 | 5867 | |
478a1c5b ILT |
5868 | /* The Plan 9 compiler permits a pointer to a struct to be |
5869 | automatically converted into a pointer to an anonymous field | |
5870 | within the struct. */ | |
5871 | if (flag_plan9_extensions | |
5872 | && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE) | |
5873 | && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE) | |
5874 | && mvl != mvr) | |
5875 | { | |
5876 | tree new_rhs = convert_to_anonymous_field (location, type, rhs); | |
5877 | if (new_rhs != NULL_TREE) | |
5878 | { | |
5879 | rhs = new_rhs; | |
5880 | rhstype = TREE_TYPE (rhs); | |
5881 | coder = TREE_CODE (rhstype); | |
5882 | ttr = TREE_TYPE (rhstype); | |
5883 | mvr = TYPE_MAIN_VARIANT (ttr); | |
5884 | } | |
5885 | } | |
5886 | ||
b7e20b53 | 5887 | /* C++ does not allow the implicit conversion void* -> T*. However, |
c22cacf3 MS |
5888 | for the purpose of reducing the number of false positives, we |
5889 | tolerate the special case of | |
b7e20b53 | 5890 | |
c22cacf3 | 5891 | int *p = NULL; |
b7e20b53 | 5892 | |
c22cacf3 | 5893 | where NULL is typically defined in C to be '(void *) 0'. */ |
c6bdf92e | 5894 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) |
c2255bc4 AH |
5895 | warning_at (location, OPT_Wc___compat, |
5896 | "request for implicit conversion " | |
5897 | "from %qT to %qT not permitted in C++", rhstype, type); | |
400fbf9f | 5898 | |
36c5e70a BE |
5899 | /* See if the pointers point to incompatible address spaces. */ |
5900 | asl = TYPE_ADDR_SPACE (ttl); | |
5901 | asr = TYPE_ADDR_SPACE (ttr); | |
5902 | if (!null_pointer_constant_p (rhs) | |
5903 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
5904 | { | |
5905 | switch (errtype) | |
5906 | { | |
5907 | case ic_argpass: | |
5908 | error_at (location, "passing argument %d of %qE from pointer to " | |
5909 | "non-enclosed address space", parmnum, rname); | |
5910 | break; | |
5911 | case ic_assign: | |
5912 | error_at (location, "assignment from pointer to " | |
5913 | "non-enclosed address space"); | |
5914 | break; | |
5915 | case ic_init: | |
5916 | error_at (location, "initialization from pointer to " | |
5917 | "non-enclosed address space"); | |
5918 | break; | |
5919 | case ic_return: | |
5920 | error_at (location, "return from pointer to " | |
5921 | "non-enclosed address space"); | |
5922 | break; | |
5923 | default: | |
5924 | gcc_unreachable (); | |
5925 | } | |
5926 | return error_mark_node; | |
5927 | } | |
5928 | ||
7876a414 KG |
5929 | /* Check if the right-hand side has a format attribute but the |
5930 | left-hand side doesn't. */ | |
90137d8f | 5931 | if (warn_suggest_attribute_format |
104f8784 | 5932 | && check_missing_format_attribute (type, rhstype)) |
c22cacf3 | 5933 | { |
104f8784 KG |
5934 | switch (errtype) |
5935 | { | |
5936 | case ic_argpass: | |
90137d8f | 5937 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
5938 | "argument %d of %qE might be " |
5939 | "a candidate for a format attribute", | |
5940 | parmnum, rname); | |
104f8784 KG |
5941 | break; |
5942 | case ic_assign: | |
90137d8f | 5943 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
5944 | "assignment left-hand side might be " |
5945 | "a candidate for a format attribute"); | |
104f8784 KG |
5946 | break; |
5947 | case ic_init: | |
90137d8f | 5948 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
5949 | "initialization left-hand side might be " |
5950 | "a candidate for a format attribute"); | |
104f8784 KG |
5951 | break; |
5952 | case ic_return: | |
90137d8f | 5953 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
5954 | "return type might be " |
5955 | "a candidate for a format attribute"); | |
104f8784 KG |
5956 | break; |
5957 | default: | |
5958 | gcc_unreachable (); | |
5959 | } | |
7876a414 | 5960 | } |
c22cacf3 | 5961 | |
3e4093b6 RS |
5962 | /* Any non-function converts to a [const][volatile] void * |
5963 | and vice versa; otherwise, targets must be the same. | |
5964 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
267bac10 JM |
5965 | if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl)) |
5966 | || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr)) | |
744aa42f | 5967 | || (target_cmp = comp_target_types (location, type, rhstype)) |
3e4093b6 | 5968 | || is_opaque_pointer |
f8a93a2e JJ |
5969 | || ((c_common_unsigned_type (mvl) |
5970 | == c_common_unsigned_type (mvr)) | |
267bac10 JM |
5971 | && (c_common_signed_type (mvl) |
5972 | == c_common_signed_type (mvr)) | |
5973 | && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr))) | |
3e4093b6 RS |
5974 | { |
5975 | if (pedantic | |
5976 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5977 | || | |
5978 | (VOID_TYPE_P (ttr) | |
928c19bb | 5979 | && !null_pointer_constant |
3e4093b6 | 5980 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
c1771a20 | 5981 | WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic, |
509c9d60 | 5982 | G_("ISO C forbids passing argument %d of " |
2ac2f164 JM |
5983 | "%qE between function pointer " |
5984 | "and %<void *%>"), | |
4b794eaf | 5985 | G_("ISO C forbids assignment between " |
2ac2f164 | 5986 | "function pointer and %<void *%>"), |
4b794eaf | 5987 | G_("ISO C forbids initialization between " |
2ac2f164 | 5988 | "function pointer and %<void *%>"), |
4b794eaf | 5989 | G_("ISO C forbids return between function " |
2ac2f164 | 5990 | "pointer and %<void *%>")); |
3e4093b6 RS |
5991 | /* Const and volatile mean something different for function types, |
5992 | so the usual warnings are not appropriate. */ | |
5993 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
5994 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
5995 | { | |
267bac10 JM |
5996 | /* Assignments between atomic and non-atomic objects are OK. */ |
5997 | if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr) | |
5998 | & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl)) | |
58393038 | 5999 | { |
6c39e757 NP |
6000 | WARN_FOR_QUALIFIERS (location, 0, |
6001 | G_("passing argument %d of %qE discards " | |
6002 | "%qv qualifier from pointer target type"), | |
6003 | G_("assignment discards %qv qualifier " | |
6004 | "from pointer target type"), | |
6005 | G_("initialization discards %qv qualifier " | |
6006 | "from pointer target type"), | |
6007 | G_("return discards %qv qualifier from " | |
6008 | "pointer target type"), | |
6009 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
58393038 | 6010 | } |
3e4093b6 RS |
6011 | /* If this is not a case of ignoring a mismatch in signedness, |
6012 | no warning. */ | |
6013 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 6014 | || target_cmp) |
3e4093b6 RS |
6015 | ; |
6016 | /* If there is a mismatch, do warn. */ | |
f2fd3821 | 6017 | else if (warn_pointer_sign) |
c2255bc4 | 6018 | WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign, |
509c9d60 | 6019 | G_("pointer targets in passing argument " |
2ac2f164 | 6020 | "%d of %qE differ in signedness"), |
4b794eaf | 6021 | G_("pointer targets in assignment " |
2ac2f164 | 6022 | "differ in signedness"), |
4b794eaf | 6023 | G_("pointer targets in initialization " |
2ac2f164 | 6024 | "differ in signedness"), |
4b794eaf | 6025 | G_("pointer targets in return differ " |
2ac2f164 | 6026 | "in signedness")); |
3e4093b6 RS |
6027 | } |
6028 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
6029 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
6030 | { | |
6031 | /* Because const and volatile on functions are restrictions | |
6032 | that say the function will not do certain things, | |
6033 | it is okay to use a const or volatile function | |
6034 | where an ordinary one is wanted, but not vice-versa. */ | |
36c5e70a BE |
6035 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
6036 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
49706e39 | 6037 | WARN_FOR_QUALIFIERS (location, 0, |
509c9d60 | 6038 | G_("passing argument %d of %qE makes " |
49706e39 | 6039 | "%q#v qualified function pointer " |
2ac2f164 | 6040 | "from unqualified"), |
49706e39 | 6041 | G_("assignment makes %q#v qualified function " |
2ac2f164 | 6042 | "pointer from unqualified"), |
49706e39 | 6043 | G_("initialization makes %q#v qualified " |
2ac2f164 | 6044 | "function pointer from unqualified"), |
49706e39 MLI |
6045 | G_("return makes %q#v qualified function " |
6046 | "pointer from unqualified"), | |
6047 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 RS |
6048 | } |
6049 | } | |
6050 | else | |
58393038 ZL |
6051 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
6052 | if (!objc_ok) | |
c2255bc4 | 6053 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 6054 | G_("passing argument %d of %qE from " |
58393038 | 6055 | "incompatible pointer type"), |
4b794eaf JJ |
6056 | G_("assignment from incompatible pointer type"), |
6057 | G_("initialization from incompatible " | |
58393038 | 6058 | "pointer type"), |
4b794eaf | 6059 | G_("return from incompatible pointer type")); |
58393038 | 6060 | |
3e4093b6 RS |
6061 | return convert (type, rhs); |
6062 | } | |
b494fd98 EB |
6063 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
6064 | { | |
6dcc04b0 JM |
6065 | /* ??? This should not be an error when inlining calls to |
6066 | unprototyped functions. */ | |
c2255bc4 | 6067 | error_at (location, "invalid use of non-lvalue array"); |
b494fd98 EB |
6068 | return error_mark_node; |
6069 | } | |
3e4093b6 | 6070 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
400fbf9f | 6071 | { |
3e4093b6 RS |
6072 | /* An explicit constant 0 can convert to a pointer, |
6073 | or one that results from arithmetic, even including | |
6074 | a cast to integer type. */ | |
928c19bb | 6075 | if (!null_pointer_constant) |
c2255bc4 | 6076 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 6077 | G_("passing argument %d of %qE makes " |
2ac2f164 | 6078 | "pointer from integer without a cast"), |
4b794eaf | 6079 | G_("assignment makes pointer from integer " |
2ac2f164 | 6080 | "without a cast"), |
4b794eaf | 6081 | G_("initialization makes pointer from " |
2ac2f164 | 6082 | "integer without a cast"), |
4b794eaf | 6083 | G_("return makes pointer from integer " |
2ac2f164 | 6084 | "without a cast")); |
b3006337 EB |
6085 | |
6086 | return convert (type, rhs); | |
400fbf9f | 6087 | } |
3e4093b6 | 6088 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
400fbf9f | 6089 | { |
c2255bc4 | 6090 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 6091 | G_("passing argument %d of %qE makes integer " |
2ac2f164 | 6092 | "from pointer without a cast"), |
4b794eaf | 6093 | G_("assignment makes integer from pointer " |
2ac2f164 | 6094 | "without a cast"), |
4b794eaf | 6095 | G_("initialization makes integer from pointer " |
2ac2f164 | 6096 | "without a cast"), |
4b794eaf | 6097 | G_("return makes integer from pointer " |
2ac2f164 | 6098 | "without a cast")); |
3e4093b6 RS |
6099 | return convert (type, rhs); |
6100 | } | |
6101 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
928c19bb JM |
6102 | { |
6103 | tree ret; | |
6104 | bool save = in_late_binary_op; | |
6105 | in_late_binary_op = true; | |
6106 | ret = convert (type, rhs); | |
6107 | in_late_binary_op = save; | |
6108 | return ret; | |
6109 | } | |
400fbf9f | 6110 | |
2ac2f164 | 6111 | switch (errtype) |
3e4093b6 | 6112 | { |
2ac2f164 | 6113 | case ic_argpass: |
c2255bc4 | 6114 | error_at (location, "incompatible type for argument %d of %qE", parmnum, rname); |
a7da8b42 MLI |
6115 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) |
6116 | ? DECL_SOURCE_LOCATION (fundecl) : input_location, | |
6117 | "expected %qT but argument is of type %qT", type, rhstype); | |
2ac2f164 JM |
6118 | break; |
6119 | case ic_assign: | |
c2255bc4 AH |
6120 | error_at (location, "incompatible types when assigning to type %qT from " |
6121 | "type %qT", type, rhstype); | |
2ac2f164 JM |
6122 | break; |
6123 | case ic_init: | |
c2255bc4 AH |
6124 | error_at (location, |
6125 | "incompatible types when initializing type %qT using type %qT", | |
6126 | type, rhstype); | |
2ac2f164 JM |
6127 | break; |
6128 | case ic_return: | |
c2255bc4 AH |
6129 | error_at (location, |
6130 | "incompatible types when returning type %qT but %qT was " | |
6131 | "expected", rhstype, type); | |
2ac2f164 JM |
6132 | break; |
6133 | default: | |
6134 | gcc_unreachable (); | |
400fbf9f | 6135 | } |
53b01f59 | 6136 | |
3e4093b6 RS |
6137 | return error_mark_node; |
6138 | } | |
3e4093b6 RS |
6139 | \f |
6140 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
6141 | return its value. Otherwise, return error_mark_node. | |
15b732b2 | 6142 | |
3e4093b6 RS |
6143 | This is for handling COMPOUND_EXPRs as initializer elements |
6144 | which is allowed with a warning when -pedantic is specified. */ | |
15b732b2 | 6145 | |
3e4093b6 RS |
6146 | static tree |
6147 | valid_compound_expr_initializer (tree value, tree endtype) | |
6148 | { | |
6149 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
6150 | { | |
6151 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
6152 | == error_mark_node) | |
6153 | return error_mark_node; | |
6154 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
6155 | endtype); | |
6156 | } | |
116df786 | 6157 | else if (!initializer_constant_valid_p (value, endtype)) |
3e4093b6 RS |
6158 | return error_mark_node; |
6159 | else | |
6160 | return value; | |
15b732b2 | 6161 | } |
400fbf9f | 6162 | \f |
3e4093b6 RS |
6163 | /* Perform appropriate conversions on the initial value of a variable, |
6164 | store it in the declaration DECL, | |
6165 | and print any error messages that are appropriate. | |
bbbbb16a | 6166 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
c2255bc4 AH |
6167 | If the init is invalid, store an ERROR_MARK. |
6168 | ||
6169 | INIT_LOC is the location of the initial value. */ | |
400fbf9f | 6170 | |
3e4093b6 | 6171 | void |
c2255bc4 | 6172 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) |
400fbf9f | 6173 | { |
3e4093b6 | 6174 | tree value, type; |
928c19bb | 6175 | bool npc = false; |
400fbf9f | 6176 | |
3e4093b6 | 6177 | /* If variable's type was invalidly declared, just ignore it. */ |
400fbf9f | 6178 | |
3e4093b6 RS |
6179 | type = TREE_TYPE (decl); |
6180 | if (TREE_CODE (type) == ERROR_MARK) | |
6181 | return; | |
400fbf9f | 6182 | |
3e4093b6 | 6183 | /* Digest the specified initializer into an expression. */ |
400fbf9f | 6184 | |
928c19bb JM |
6185 | if (init) |
6186 | npc = null_pointer_constant_p (init); | |
c2255bc4 AH |
6187 | value = digest_init (init_loc, type, init, origtype, npc, |
6188 | true, TREE_STATIC (decl)); | |
400fbf9f | 6189 | |
3e4093b6 | 6190 | /* Store the expression if valid; else report error. */ |
400fbf9f | 6191 | |
8400e75e | 6192 | if (!in_system_header_at (input_location) |
3f75a254 | 6193 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) |
3176a0c2 DD |
6194 | warning (OPT_Wtraditional, "traditional C rejects automatic " |
6195 | "aggregate initialization"); | |
2f6e4e97 | 6196 | |
3e4093b6 | 6197 | DECL_INITIAL (decl) = value; |
400fbf9f | 6198 | |
3e4093b6 RS |
6199 | /* ANSI wants warnings about out-of-range constant initializers. */ |
6200 | STRIP_TYPE_NOPS (value); | |
b8698a0f | 6201 | if (TREE_STATIC (decl)) |
c2658540 | 6202 | constant_expression_warning (value); |
400fbf9f | 6203 | |
3e4093b6 RS |
6204 | /* Check if we need to set array size from compound literal size. */ |
6205 | if (TREE_CODE (type) == ARRAY_TYPE | |
6206 | && TYPE_DOMAIN (type) == 0 | |
6207 | && value != error_mark_node) | |
400fbf9f | 6208 | { |
3e4093b6 RS |
6209 | tree inside_init = init; |
6210 | ||
ed248cf7 | 6211 | STRIP_TYPE_NOPS (inside_init); |
3e4093b6 RS |
6212 | inside_init = fold (inside_init); |
6213 | ||
6214 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
6215 | { | |
8d9f82d5 | 6216 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
3e4093b6 | 6217 | |
8d9f82d5 | 6218 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) |
3e4093b6 RS |
6219 | { |
6220 | /* For int foo[] = (int [3]){1}; we need to set array size | |
6221 | now since later on array initializer will be just the | |
6222 | brace enclosed list of the compound literal. */ | |
e30ecc5d | 6223 | tree etype = strip_array_types (TREE_TYPE (decl)); |
8d9f82d5 | 6224 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); |
8d9f82d5 | 6225 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); |
3e4093b6 | 6226 | layout_type (type); |
8d9f82d5 | 6227 | layout_decl (cldecl, 0); |
e30ecc5d JJ |
6228 | TREE_TYPE (decl) |
6229 | = c_build_qualified_type (type, TYPE_QUALS (etype)); | |
3e4093b6 RS |
6230 | } |
6231 | } | |
400fbf9f | 6232 | } |
3e4093b6 RS |
6233 | } |
6234 | \f | |
6235 | /* Methods for storing and printing names for error messages. */ | |
400fbf9f | 6236 | |
3e4093b6 RS |
6237 | /* Implement a spelling stack that allows components of a name to be pushed |
6238 | and popped. Each element on the stack is this structure. */ | |
400fbf9f | 6239 | |
3e4093b6 RS |
6240 | struct spelling |
6241 | { | |
6242 | int kind; | |
6243 | union | |
400fbf9f | 6244 | { |
a0f0ab9f | 6245 | unsigned HOST_WIDE_INT i; |
3e4093b6 RS |
6246 | const char *s; |
6247 | } u; | |
6248 | }; | |
2f6e4e97 | 6249 | |
3e4093b6 RS |
6250 | #define SPELLING_STRING 1 |
6251 | #define SPELLING_MEMBER 2 | |
6252 | #define SPELLING_BOUNDS 3 | |
400fbf9f | 6253 | |
3e4093b6 RS |
6254 | static struct spelling *spelling; /* Next stack element (unused). */ |
6255 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
6256 | static int spelling_size; /* Size of the spelling stack. */ | |
400fbf9f | 6257 | |
3e4093b6 RS |
6258 | /* Macros to save and restore the spelling stack around push_... functions. |
6259 | Alternative to SAVE_SPELLING_STACK. */ | |
400fbf9f | 6260 | |
3e4093b6 RS |
6261 | #define SPELLING_DEPTH() (spelling - spelling_base) |
6262 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
400fbf9f | 6263 | |
3e4093b6 RS |
6264 | /* Push an element on the spelling stack with type KIND and assign VALUE |
6265 | to MEMBER. */ | |
400fbf9f | 6266 | |
3e4093b6 RS |
6267 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
6268 | { \ | |
6269 | int depth = SPELLING_DEPTH (); \ | |
6270 | \ | |
6271 | if (depth >= spelling_size) \ | |
6272 | { \ | |
6273 | spelling_size += 10; \ | |
cca8ead2 BI |
6274 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
6275 | spelling_size); \ | |
3e4093b6 RS |
6276 | RESTORE_SPELLING_DEPTH (depth); \ |
6277 | } \ | |
6278 | \ | |
6279 | spelling->kind = (KIND); \ | |
6280 | spelling->MEMBER = (VALUE); \ | |
6281 | spelling++; \ | |
6282 | } | |
400fbf9f | 6283 | |
3e4093b6 | 6284 | /* Push STRING on the stack. Printed literally. */ |
400fbf9f | 6285 | |
3e4093b6 RS |
6286 | static void |
6287 | push_string (const char *string) | |
6288 | { | |
6289 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
6290 | } | |
400fbf9f | 6291 | |
3e4093b6 | 6292 | /* Push a member name on the stack. Printed as '.' STRING. */ |
400fbf9f | 6293 | |
3e4093b6 RS |
6294 | static void |
6295 | push_member_name (tree decl) | |
6296 | { | |
6297 | const char *const string | |
88388a52 JM |
6298 | = (DECL_NAME (decl) |
6299 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
6300 | : _("<anonymous>")); | |
3e4093b6 RS |
6301 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
6302 | } | |
400fbf9f | 6303 | |
3e4093b6 | 6304 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
400fbf9f | 6305 | |
3e4093b6 | 6306 | static void |
a0f0ab9f | 6307 | push_array_bounds (unsigned HOST_WIDE_INT bounds) |
3e4093b6 RS |
6308 | { |
6309 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
6310 | } | |
bb58bec5 | 6311 | |
3e4093b6 | 6312 | /* Compute the maximum size in bytes of the printed spelling. */ |
400fbf9f | 6313 | |
3e4093b6 RS |
6314 | static int |
6315 | spelling_length (void) | |
6316 | { | |
6317 | int size = 0; | |
6318 | struct spelling *p; | |
400fbf9f | 6319 | |
3e4093b6 RS |
6320 | for (p = spelling_base; p < spelling; p++) |
6321 | { | |
6322 | if (p->kind == SPELLING_BOUNDS) | |
6323 | size += 25; | |
6324 | else | |
6325 | size += strlen (p->u.s) + 1; | |
6326 | } | |
6327 | ||
6328 | return size; | |
400fbf9f | 6329 | } |
400fbf9f | 6330 | |
3e4093b6 | 6331 | /* Print the spelling to BUFFER and return it. */ |
400fbf9f | 6332 | |
3e4093b6 RS |
6333 | static char * |
6334 | print_spelling (char *buffer) | |
400fbf9f | 6335 | { |
3e4093b6 RS |
6336 | char *d = buffer; |
6337 | struct spelling *p; | |
400fbf9f | 6338 | |
3e4093b6 RS |
6339 | for (p = spelling_base; p < spelling; p++) |
6340 | if (p->kind == SPELLING_BOUNDS) | |
6341 | { | |
a0f0ab9f | 6342 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); |
3e4093b6 RS |
6343 | d += strlen (d); |
6344 | } | |
6345 | else | |
6346 | { | |
6347 | const char *s; | |
6348 | if (p->kind == SPELLING_MEMBER) | |
6349 | *d++ = '.'; | |
6350 | for (s = p->u.s; (*d = *s++); d++) | |
6351 | ; | |
6352 | } | |
6353 | *d++ = '\0'; | |
6354 | return buffer; | |
6355 | } | |
400fbf9f | 6356 | |
3e4093b6 | 6357 | /* Issue an error message for a bad initializer component. |
6a8f4e12 | 6358 | GMSGID identifies the message. |
3e4093b6 | 6359 | The component name is taken from the spelling stack. */ |
400fbf9f | 6360 | |
3e4093b6 | 6361 | void |
6a8f4e12 | 6362 | error_init (const char *gmsgid) |
3e4093b6 RS |
6363 | { |
6364 | char *ofwhat; | |
400fbf9f | 6365 | |
6a8f4e12 AP |
6366 | /* The gmsgid may be a format string with %< and %>. */ |
6367 | error (gmsgid); | |
28dab132 | 6368 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 6369 | if (*ofwhat) |
bda67431 | 6370 | error ("(near initialization for %qs)", ofwhat); |
3e4093b6 | 6371 | } |
400fbf9f | 6372 | |
fcf73884 MLI |
6373 | /* Issue a pedantic warning for a bad initializer component. OPT is |
6374 | the option OPT_* (from options.h) controlling this warning or 0 if | |
6a8f4e12 | 6375 | it is unconditionally given. GMSGID identifies the message. The |
fcf73884 | 6376 | component name is taken from the spelling stack. */ |
400fbf9f | 6377 | |
3e4093b6 | 6378 | void |
6a8f4e12 | 6379 | pedwarn_init (location_t location, int opt, const char *gmsgid) |
3e4093b6 RS |
6380 | { |
6381 | char *ofwhat; | |
f90e8e2e | 6382 | |
6a8f4e12 AP |
6383 | /* The gmsgid may be a format string with %< and %>. */ |
6384 | pedwarn (location, opt, gmsgid); | |
28dab132 | 6385 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 6386 | if (*ofwhat) |
509c9d60 | 6387 | pedwarn (location, opt, "(near initialization for %qs)", ofwhat); |
3e4093b6 | 6388 | } |
9f720c3e | 6389 | |
b8698a0f | 6390 | /* Issue a warning for a bad initializer component. |
683d6ff9 MLI |
6391 | |
6392 | OPT is the OPT_W* value corresponding to the warning option that | |
6a8f4e12 | 6393 | controls this warning. GMSGID identifies the message. The |
683d6ff9 | 6394 | component name is taken from the spelling stack. */ |
61179109 | 6395 | |
3e4093b6 | 6396 | static void |
6a8f4e12 | 6397 | warning_init (int opt, const char *gmsgid) |
3e4093b6 RS |
6398 | { |
6399 | char *ofwhat; | |
7e842ef8 | 6400 | |
6a8f4e12 AP |
6401 | /* The gmsgid may be a format string with %< and %>. */ |
6402 | warning (opt, gmsgid); | |
28dab132 | 6403 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 6404 | if (*ofwhat) |
683d6ff9 | 6405 | warning (opt, "(near initialization for %qs)", ofwhat); |
3e4093b6 RS |
6406 | } |
6407 | \f | |
916c5919 JM |
6408 | /* If TYPE is an array type and EXPR is a parenthesized string |
6409 | constant, warn if pedantic that EXPR is being used to initialize an | |
6410 | object of type TYPE. */ | |
6411 | ||
6412 | void | |
6413 | maybe_warn_string_init (tree type, struct c_expr expr) | |
6414 | { | |
6415 | if (pedantic | |
6416 | && TREE_CODE (type) == ARRAY_TYPE | |
6417 | && TREE_CODE (expr.value) == STRING_CST | |
6418 | && expr.original_code != STRING_CST) | |
c1771a20 | 6419 | pedwarn_init (input_location, OPT_Wpedantic, |
fcf73884 | 6420 | "array initialized from parenthesized string constant"); |
916c5919 JM |
6421 | } |
6422 | ||
3e4093b6 RS |
6423 | /* Digest the parser output INIT as an initializer for type TYPE. |
6424 | Return a C expression of type TYPE to represent the initial value. | |
7e842ef8 | 6425 | |
bbbbb16a ILT |
6426 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
6427 | ||
928c19bb JM |
6428 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. |
6429 | ||
916c5919 JM |
6430 | If INIT is a string constant, STRICT_STRING is true if it is |
6431 | unparenthesized or we should not warn here for it being parenthesized. | |
6432 | For other types of INIT, STRICT_STRING is not used. | |
6433 | ||
c2255bc4 AH |
6434 | INIT_LOC is the location of the INIT. |
6435 | ||
3e4093b6 RS |
6436 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
6437 | elements are seen. */ | |
7e842ef8 | 6438 | |
3e4093b6 | 6439 | static tree |
c2255bc4 AH |
6440 | digest_init (location_t init_loc, tree type, tree init, tree origtype, |
6441 | bool null_pointer_constant, bool strict_string, | |
6442 | int require_constant) | |
3e4093b6 RS |
6443 | { |
6444 | enum tree_code code = TREE_CODE (type); | |
6445 | tree inside_init = init; | |
8ce94e44 | 6446 | tree semantic_type = NULL_TREE; |
928c19bb | 6447 | bool maybe_const = true; |
7e842ef8 | 6448 | |
3e4093b6 | 6449 | if (type == error_mark_node |
f01da1a5 | 6450 | || !init |
3e4093b6 RS |
6451 | || init == error_mark_node |
6452 | || TREE_TYPE (init) == error_mark_node) | |
6453 | return error_mark_node; | |
7e842ef8 | 6454 | |
ed248cf7 | 6455 | STRIP_TYPE_NOPS (inside_init); |
7e842ef8 | 6456 | |
8ce94e44 JM |
6457 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) |
6458 | { | |
6459 | semantic_type = TREE_TYPE (inside_init); | |
6460 | inside_init = TREE_OPERAND (inside_init, 0); | |
6461 | } | |
928c19bb JM |
6462 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); |
6463 | inside_init = decl_constant_value_for_optimization (inside_init); | |
7e842ef8 | 6464 | |
3e4093b6 RS |
6465 | /* Initialization of an array of chars from a string constant |
6466 | optionally enclosed in braces. */ | |
7e842ef8 | 6467 | |
197463ae JM |
6468 | if (code == ARRAY_TYPE && inside_init |
6469 | && TREE_CODE (inside_init) == STRING_CST) | |
3e4093b6 | 6470 | { |
267bac10 JM |
6471 | tree typ1 |
6472 | = (TYPE_ATOMIC (TREE_TYPE (type)) | |
6473 | ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)), | |
6474 | TYPE_QUAL_ATOMIC) | |
6475 | : TYPE_MAIN_VARIANT (TREE_TYPE (type))); | |
197463ae JM |
6476 | /* Note that an array could be both an array of character type |
6477 | and an array of wchar_t if wchar_t is signed char or unsigned | |
6478 | char. */ | |
6479 | bool char_array = (typ1 == char_type_node | |
6480 | || typ1 == signed_char_type_node | |
6481 | || typ1 == unsigned_char_type_node); | |
6482 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
c466b2cd KVH |
6483 | bool char16_array = !!comptypes (typ1, char16_type_node); |
6484 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
6485 | ||
6486 | if (char_array || wchar_array || char16_array || char32_array) | |
7e842ef8 | 6487 | { |
916c5919 | 6488 | struct c_expr expr; |
c466b2cd | 6489 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); |
916c5919 JM |
6490 | expr.value = inside_init; |
6491 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
6866c6e8 | 6492 | expr.original_type = NULL; |
916c5919 JM |
6493 | maybe_warn_string_init (type, expr); |
6494 | ||
a45e580b | 6495 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
c1771a20 | 6496 | pedwarn_init (init_loc, OPT_Wpedantic, |
a45e580b JM |
6497 | "initialization of a flexible array member"); |
6498 | ||
3e4093b6 | 6499 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 6500 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 | 6501 | return inside_init; |
7e842ef8 | 6502 | |
c466b2cd | 6503 | if (char_array) |
3e4093b6 | 6504 | { |
c466b2cd KVH |
6505 | if (typ2 != char_type_node) |
6506 | { | |
6507 | error_init ("char-array initialized from wide string"); | |
6508 | return error_mark_node; | |
6509 | } | |
3e4093b6 | 6510 | } |
c466b2cd | 6511 | else |
3e4093b6 | 6512 | { |
c466b2cd KVH |
6513 | if (typ2 == char_type_node) |
6514 | { | |
6515 | error_init ("wide character array initialized from non-wide " | |
6516 | "string"); | |
6517 | return error_mark_node; | |
6518 | } | |
6519 | else if (!comptypes(typ1, typ2)) | |
6520 | { | |
6521 | error_init ("wide character array initialized from " | |
6522 | "incompatible wide string"); | |
6523 | return error_mark_node; | |
6524 | } | |
7e842ef8 | 6525 | } |
2f6e4e97 | 6526 | |
3e4093b6 RS |
6527 | TREE_TYPE (inside_init) = type; |
6528 | if (TYPE_DOMAIN (type) != 0 | |
6529 | && TYPE_SIZE (type) != 0 | |
5eb4df45 ILT |
6530 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
6531 | { | |
6532 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
6533 | ||
c466b2cd | 6534 | /* Subtract the size of a single (possibly wide) character |
3e4093b6 RS |
6535 | because it's ok to ignore the terminating null char |
6536 | that is counted in the length of the constant. */ | |
5eb4df45 ILT |
6537 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), |
6538 | (len | |
6539 | - (TYPE_PRECISION (typ1) | |
6540 | / BITS_PER_UNIT)))) | |
6541 | pedwarn_init (init_loc, 0, | |
6542 | ("initializer-string for array of chars " | |
6543 | "is too long")); | |
6544 | else if (warn_cxx_compat | |
6545 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
6546 | warning_at (init_loc, OPT_Wc___compat, | |
6547 | ("initializer-string for array chars " | |
6548 | "is too long for C++")); | |
6549 | } | |
7e842ef8 | 6550 | |
3e4093b6 | 6551 | return inside_init; |
7e842ef8 | 6552 | } |
197463ae JM |
6553 | else if (INTEGRAL_TYPE_P (typ1)) |
6554 | { | |
6555 | error_init ("array of inappropriate type initialized " | |
6556 | "from string constant"); | |
6557 | return error_mark_node; | |
6558 | } | |
7e842ef8 PE |
6559 | } |
6560 | ||
3e4093b6 RS |
6561 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
6562 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
6563 | below and handle as a constructor. */ | |
e89be13b JJ |
6564 | if (code == VECTOR_TYPE |
6565 | && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE | |
00c8e9f6 | 6566 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) |
e89be13b JJ |
6567 | && TREE_CONSTANT (inside_init)) |
6568 | { | |
6569 | if (TREE_CODE (inside_init) == VECTOR_CST | |
6570 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6571 | TYPE_MAIN_VARIANT (type))) | |
6572 | return inside_init; | |
6573 | ||
6574 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
6575 | { | |
4038c495 GB |
6576 | unsigned HOST_WIDE_INT ix; |
6577 | tree value; | |
6578 | bool constant_p = true; | |
e89be13b JJ |
6579 | |
6580 | /* Iterate through elements and check if all constructor | |
6581 | elements are *_CSTs. */ | |
4038c495 GB |
6582 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) |
6583 | if (!CONSTANT_CLASS_P (value)) | |
6584 | { | |
6585 | constant_p = false; | |
6586 | break; | |
6587 | } | |
e89be13b | 6588 | |
4038c495 GB |
6589 | if (constant_p) |
6590 | return build_vector_from_ctor (type, | |
6591 | CONSTRUCTOR_ELTS (inside_init)); | |
e89be13b JJ |
6592 | } |
6593 | } | |
6035d635 | 6594 | |
ca085fd7 MLI |
6595 | if (warn_sequence_point) |
6596 | verify_sequence_points (inside_init); | |
6597 | ||
3e4093b6 RS |
6598 | /* Any type can be initialized |
6599 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 6600 | |
3e4093b6 RS |
6601 | if (inside_init && TREE_TYPE (inside_init) != 0 |
6602 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
132da1a5 | 6603 | TYPE_MAIN_VARIANT (type)) |
3e4093b6 | 6604 | || (code == ARRAY_TYPE |
132da1a5 | 6605 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6606 | || (code == VECTOR_TYPE |
132da1a5 | 6607 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6608 | || (code == POINTER_TYPE |
3897f229 | 6609 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
3e4093b6 | 6610 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
132da1a5 | 6611 | TREE_TYPE (type))))) |
3e4093b6 RS |
6612 | { |
6613 | if (code == POINTER_TYPE) | |
b494fd98 | 6614 | { |
b494fd98 EB |
6615 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) |
6616 | { | |
f2a71bbc JM |
6617 | if (TREE_CODE (inside_init) == STRING_CST |
6618 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
c2255bc4 AH |
6619 | inside_init = array_to_pointer_conversion |
6620 | (init_loc, inside_init); | |
f2a71bbc JM |
6621 | else |
6622 | { | |
6623 | error_init ("invalid use of non-lvalue array"); | |
6624 | return error_mark_node; | |
6625 | } | |
b494fd98 | 6626 | } |
f2a71bbc | 6627 | } |
b494fd98 | 6628 | |
bae39a73 NS |
6629 | if (code == VECTOR_TYPE) |
6630 | /* Although the types are compatible, we may require a | |
6631 | conversion. */ | |
6632 | inside_init = convert (type, inside_init); | |
3e4093b6 | 6633 | |
ca58211b PB |
6634 | if (require_constant |
6635 | && (code == VECTOR_TYPE || !flag_isoc99) | |
3e4093b6 | 6636 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) |
400fbf9f | 6637 | { |
3e4093b6 RS |
6638 | /* As an extension, allow initializing objects with static storage |
6639 | duration with compound literals (which are then treated just as | |
ca58211b PB |
6640 | the brace enclosed list they contain). Also allow this for |
6641 | vectors, as we can only assign them with compound literals. */ | |
3e4093b6 RS |
6642 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
6643 | inside_init = DECL_INITIAL (decl); | |
400fbf9f | 6644 | } |
3e4093b6 RS |
6645 | |
6646 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
6647 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 6648 | { |
3e4093b6 RS |
6649 | error_init ("array initialized from non-constant array expression"); |
6650 | return error_mark_node; | |
400fbf9f | 6651 | } |
400fbf9f | 6652 | |
c1771a20 | 6653 | /* Compound expressions can only occur here if -Wpedantic or |
3e4093b6 RS |
6654 | -pedantic-errors is specified. In the later case, we always want |
6655 | an error. In the former case, we simply want a warning. */ | |
6656 | if (require_constant && pedantic | |
6657 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
6658 | { | |
6659 | inside_init | |
6660 | = valid_compound_expr_initializer (inside_init, | |
6661 | TREE_TYPE (inside_init)); | |
6662 | if (inside_init == error_mark_node) | |
6663 | error_init ("initializer element is not constant"); | |
2f6e4e97 | 6664 | else |
c1771a20 | 6665 | pedwarn_init (init_loc, OPT_Wpedantic, |
509c9d60 | 6666 | "initializer element is not constant"); |
3e4093b6 RS |
6667 | if (flag_pedantic_errors) |
6668 | inside_init = error_mark_node; | |
6669 | } | |
6670 | else if (require_constant | |
116df786 RH |
6671 | && !initializer_constant_valid_p (inside_init, |
6672 | TREE_TYPE (inside_init))) | |
3e4093b6 RS |
6673 | { |
6674 | error_init ("initializer element is not constant"); | |
6675 | inside_init = error_mark_node; | |
8b40563c | 6676 | } |
928c19bb | 6677 | else if (require_constant && !maybe_const) |
c2255bc4 | 6678 | pedwarn_init (init_loc, 0, |
928c19bb | 6679 | "initializer element is not a constant expression"); |
f735a153 | 6680 | |
90137d8f | 6681 | /* Added to enable additional -Wsuggest-attribute=format warnings. */ |
8fcef540 | 6682 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) |
c2255bc4 AH |
6683 | inside_init = convert_for_assignment (init_loc, type, inside_init, |
6684 | origtype, | |
bbbbb16a | 6685 | ic_init, null_pointer_constant, |
928c19bb | 6686 | NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
6687 | return inside_init; |
6688 | } | |
f735a153 | 6689 | |
3e4093b6 | 6690 | /* Handle scalar types, including conversions. */ |
400fbf9f | 6691 | |
ab22c1fa CF |
6692 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE |
6693 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
6694 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
400fbf9f | 6695 | { |
f2a71bbc JM |
6696 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE |
6697 | && (TREE_CODE (init) == STRING_CST | |
6698 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
c2255bc4 | 6699 | inside_init = init = array_to_pointer_conversion (init_loc, init); |
8ce94e44 JM |
6700 | if (semantic_type) |
6701 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
6702 | inside_init); | |
3e4093b6 | 6703 | inside_init |
c2255bc4 AH |
6704 | = convert_for_assignment (init_loc, type, inside_init, origtype, |
6705 | ic_init, null_pointer_constant, | |
3e4093b6 | 6706 | NULL_TREE, NULL_TREE, 0); |
2f6e4e97 | 6707 | |
3274deff JW |
6708 | /* Check to see if we have already given an error message. */ |
6709 | if (inside_init == error_mark_node) | |
6710 | ; | |
3f75a254 | 6711 | else if (require_constant && !TREE_CONSTANT (inside_init)) |
400fbf9f | 6712 | { |
3e4093b6 RS |
6713 | error_init ("initializer element is not constant"); |
6714 | inside_init = error_mark_node; | |
400fbf9f | 6715 | } |
3e4093b6 | 6716 | else if (require_constant |
116df786 RH |
6717 | && !initializer_constant_valid_p (inside_init, |
6718 | TREE_TYPE (inside_init))) | |
400fbf9f | 6719 | { |
3e4093b6 RS |
6720 | error_init ("initializer element is not computable at load time"); |
6721 | inside_init = error_mark_node; | |
400fbf9f | 6722 | } |
928c19bb | 6723 | else if (require_constant && !maybe_const) |
c2255bc4 | 6724 | pedwarn_init (init_loc, 0, |
928c19bb | 6725 | "initializer element is not a constant expression"); |
3e4093b6 RS |
6726 | |
6727 | return inside_init; | |
400fbf9f | 6728 | } |
d9fc6069 | 6729 | |
3e4093b6 | 6730 | /* Come here only for records and arrays. */ |
d9fc6069 | 6731 | |
3e4093b6 | 6732 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
d9fc6069 | 6733 | { |
3e4093b6 RS |
6734 | error_init ("variable-sized object may not be initialized"); |
6735 | return error_mark_node; | |
d9fc6069 | 6736 | } |
3e4093b6 RS |
6737 | |
6738 | error_init ("invalid initializer"); | |
6739 | return error_mark_node; | |
d9fc6069 | 6740 | } |
400fbf9f | 6741 | \f |
3e4093b6 | 6742 | /* Handle initializers that use braces. */ |
400fbf9f | 6743 | |
3e4093b6 RS |
6744 | /* Type of object we are accumulating a constructor for. |
6745 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
6746 | static tree constructor_type; | |
400fbf9f | 6747 | |
3e4093b6 RS |
6748 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
6749 | left to fill. */ | |
6750 | static tree constructor_fields; | |
400fbf9f | 6751 | |
3e4093b6 RS |
6752 | /* For an ARRAY_TYPE, this is the specified index |
6753 | at which to store the next element we get. */ | |
6754 | static tree constructor_index; | |
400fbf9f | 6755 | |
3e4093b6 RS |
6756 | /* For an ARRAY_TYPE, this is the maximum index. */ |
6757 | static tree constructor_max_index; | |
400fbf9f | 6758 | |
3e4093b6 RS |
6759 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
6760 | static tree constructor_unfilled_fields; | |
400fbf9f | 6761 | |
3e4093b6 RS |
6762 | /* For an ARRAY_TYPE, this is the index of the first element |
6763 | not yet written out. */ | |
6764 | static tree constructor_unfilled_index; | |
895ea614 | 6765 | |
3e4093b6 RS |
6766 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
6767 | This is so we can generate gaps between fields, when appropriate. */ | |
6768 | static tree constructor_bit_index; | |
10d5caec | 6769 | |
3e4093b6 RS |
6770 | /* If we are saving up the elements rather than allocating them, |
6771 | this is the list of elements so far (in reverse order, | |
6772 | most recent first). */ | |
9771b263 | 6773 | static vec<constructor_elt, va_gc> *constructor_elements; |
ad47f1e5 | 6774 | |
3e4093b6 RS |
6775 | /* 1 if constructor should be incrementally stored into a constructor chain, |
6776 | 0 if all the elements should be kept in AVL tree. */ | |
6777 | static int constructor_incremental; | |
ad47f1e5 | 6778 | |
3e4093b6 RS |
6779 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
6780 | static int constructor_constant; | |
ad47f1e5 | 6781 | |
3e4093b6 RS |
6782 | /* 1 if so far this constructor's elements are all valid address constants. */ |
6783 | static int constructor_simple; | |
ad47f1e5 | 6784 | |
928c19bb JM |
6785 | /* 1 if this constructor has an element that cannot be part of a |
6786 | constant expression. */ | |
6787 | static int constructor_nonconst; | |
6788 | ||
3e4093b6 RS |
6789 | /* 1 if this constructor is erroneous so far. */ |
6790 | static int constructor_erroneous; | |
d45cf215 | 6791 | |
3e4093b6 RS |
6792 | /* Structure for managing pending initializer elements, organized as an |
6793 | AVL tree. */ | |
d45cf215 | 6794 | |
3e4093b6 | 6795 | struct init_node |
d45cf215 | 6796 | { |
3e4093b6 RS |
6797 | struct init_node *left, *right; |
6798 | struct init_node *parent; | |
6799 | int balance; | |
6800 | tree purpose; | |
6801 | tree value; | |
bbbbb16a | 6802 | tree origtype; |
d45cf215 RS |
6803 | }; |
6804 | ||
3e4093b6 RS |
6805 | /* Tree of pending elements at this constructor level. |
6806 | These are elements encountered out of order | |
6807 | which belong at places we haven't reached yet in actually | |
6808 | writing the output. | |
6809 | Will never hold tree nodes across GC runs. */ | |
6810 | static struct init_node *constructor_pending_elts; | |
d45cf215 | 6811 | |
3e4093b6 RS |
6812 | /* The SPELLING_DEPTH of this constructor. */ |
6813 | static int constructor_depth; | |
d45cf215 | 6814 | |
3e4093b6 RS |
6815 | /* DECL node for which an initializer is being read. |
6816 | 0 means we are reading a constructor expression | |
6817 | such as (struct foo) {...}. */ | |
6818 | static tree constructor_decl; | |
d45cf215 | 6819 | |
3e4093b6 RS |
6820 | /* Nonzero if this is an initializer for a top-level decl. */ |
6821 | static int constructor_top_level; | |
d45cf215 | 6822 | |
3e4093b6 RS |
6823 | /* Nonzero if there were any member designators in this initializer. */ |
6824 | static int constructor_designated; | |
d45cf215 | 6825 | |
3e4093b6 RS |
6826 | /* Nesting depth of designator list. */ |
6827 | static int designator_depth; | |
d45cf215 | 6828 | |
3e4093b6 | 6829 | /* Nonzero if there were diagnosed errors in this designator list. */ |
b06df647 | 6830 | static int designator_erroneous; |
d45cf215 | 6831 | |
3e4093b6 RS |
6832 | \f |
6833 | /* This stack has a level for each implicit or explicit level of | |
6834 | structuring in the initializer, including the outermost one. It | |
6835 | saves the values of most of the variables above. */ | |
d45cf215 | 6836 | |
3e4093b6 RS |
6837 | struct constructor_range_stack; |
6838 | ||
6839 | struct constructor_stack | |
d45cf215 | 6840 | { |
3e4093b6 RS |
6841 | struct constructor_stack *next; |
6842 | tree type; | |
6843 | tree fields; | |
6844 | tree index; | |
6845 | tree max_index; | |
6846 | tree unfilled_index; | |
6847 | tree unfilled_fields; | |
6848 | tree bit_index; | |
9771b263 | 6849 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
6850 | struct init_node *pending_elts; |
6851 | int offset; | |
6852 | int depth; | |
916c5919 | 6853 | /* If value nonzero, this value should replace the entire |
3e4093b6 | 6854 | constructor at this level. */ |
916c5919 | 6855 | struct c_expr replacement_value; |
3e4093b6 RS |
6856 | struct constructor_range_stack *range_stack; |
6857 | char constant; | |
6858 | char simple; | |
928c19bb | 6859 | char nonconst; |
3e4093b6 RS |
6860 | char implicit; |
6861 | char erroneous; | |
6862 | char outer; | |
6863 | char incremental; | |
6864 | char designated; | |
6865 | }; | |
d45cf215 | 6866 | |
802415d1 | 6867 | static struct constructor_stack *constructor_stack; |
d45cf215 | 6868 | |
3e4093b6 RS |
6869 | /* This stack represents designators from some range designator up to |
6870 | the last designator in the list. */ | |
d45cf215 | 6871 | |
3e4093b6 RS |
6872 | struct constructor_range_stack |
6873 | { | |
6874 | struct constructor_range_stack *next, *prev; | |
6875 | struct constructor_stack *stack; | |
6876 | tree range_start; | |
6877 | tree index; | |
6878 | tree range_end; | |
6879 | tree fields; | |
6880 | }; | |
d45cf215 | 6881 | |
802415d1 | 6882 | static struct constructor_range_stack *constructor_range_stack; |
d45cf215 | 6883 | |
3e4093b6 RS |
6884 | /* This stack records separate initializers that are nested. |
6885 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
6886 | in cases like { ... (struct foo) { ... } ... }. */ | |
d45cf215 | 6887 | |
3e4093b6 | 6888 | struct initializer_stack |
d45cf215 | 6889 | { |
3e4093b6 RS |
6890 | struct initializer_stack *next; |
6891 | tree decl; | |
3e4093b6 RS |
6892 | struct constructor_stack *constructor_stack; |
6893 | struct constructor_range_stack *constructor_range_stack; | |
9771b263 | 6894 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
6895 | struct spelling *spelling; |
6896 | struct spelling *spelling_base; | |
6897 | int spelling_size; | |
6898 | char top_level; | |
6899 | char require_constant_value; | |
6900 | char require_constant_elements; | |
6901 | }; | |
d45cf215 | 6902 | |
802415d1 | 6903 | static struct initializer_stack *initializer_stack; |
3e4093b6 RS |
6904 | \f |
6905 | /* Prepare to parse and output the initializer for variable DECL. */ | |
400fbf9f JW |
6906 | |
6907 | void | |
a396f8ae | 6908 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) |
400fbf9f | 6909 | { |
3e4093b6 | 6910 | const char *locus; |
cceb1885 | 6911 | struct initializer_stack *p = XNEW (struct initializer_stack); |
400fbf9f | 6912 | |
3e4093b6 | 6913 | p->decl = constructor_decl; |
3e4093b6 RS |
6914 | p->require_constant_value = require_constant_value; |
6915 | p->require_constant_elements = require_constant_elements; | |
6916 | p->constructor_stack = constructor_stack; | |
6917 | p->constructor_range_stack = constructor_range_stack; | |
6918 | p->elements = constructor_elements; | |
6919 | p->spelling = spelling; | |
6920 | p->spelling_base = spelling_base; | |
6921 | p->spelling_size = spelling_size; | |
6922 | p->top_level = constructor_top_level; | |
6923 | p->next = initializer_stack; | |
6924 | initializer_stack = p; | |
400fbf9f | 6925 | |
3e4093b6 | 6926 | constructor_decl = decl; |
3e4093b6 RS |
6927 | constructor_designated = 0; |
6928 | constructor_top_level = top_level; | |
400fbf9f | 6929 | |
6f17bbcf | 6930 | if (decl != 0 && decl != error_mark_node) |
3e4093b6 RS |
6931 | { |
6932 | require_constant_value = TREE_STATIC (decl); | |
6933 | require_constant_elements | |
6934 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
6935 | /* For a scalar, you can always use any value to initialize, | |
6936 | even within braces. */ | |
6937 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
6938 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
6939 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
6940 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
88388a52 | 6941 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); |
3e4093b6 RS |
6942 | } |
6943 | else | |
6944 | { | |
6945 | require_constant_value = 0; | |
6946 | require_constant_elements = 0; | |
88388a52 | 6947 | locus = _("(anonymous)"); |
3e4093b6 | 6948 | } |
b71c7f8a | 6949 | |
3e4093b6 RS |
6950 | constructor_stack = 0; |
6951 | constructor_range_stack = 0; | |
b71c7f8a | 6952 | |
3e4093b6 RS |
6953 | missing_braces_mentioned = 0; |
6954 | ||
6955 | spelling_base = 0; | |
6956 | spelling_size = 0; | |
6957 | RESTORE_SPELLING_DEPTH (0); | |
6958 | ||
6959 | if (locus) | |
6960 | push_string (locus); | |
6961 | } | |
6962 | ||
6963 | void | |
6964 | finish_init (void) | |
b71c7f8a | 6965 | { |
3e4093b6 | 6966 | struct initializer_stack *p = initializer_stack; |
b71c7f8a | 6967 | |
3e4093b6 RS |
6968 | /* Free the whole constructor stack of this initializer. */ |
6969 | while (constructor_stack) | |
6970 | { | |
6971 | struct constructor_stack *q = constructor_stack; | |
6972 | constructor_stack = q->next; | |
6973 | free (q); | |
6974 | } | |
6975 | ||
366de0ce | 6976 | gcc_assert (!constructor_range_stack); |
3e4093b6 RS |
6977 | |
6978 | /* Pop back to the data of the outer initializer (if any). */ | |
36579663 | 6979 | free (spelling_base); |
3aeb3655 | 6980 | |
3e4093b6 | 6981 | constructor_decl = p->decl; |
3e4093b6 RS |
6982 | require_constant_value = p->require_constant_value; |
6983 | require_constant_elements = p->require_constant_elements; | |
6984 | constructor_stack = p->constructor_stack; | |
6985 | constructor_range_stack = p->constructor_range_stack; | |
6986 | constructor_elements = p->elements; | |
6987 | spelling = p->spelling; | |
6988 | spelling_base = p->spelling_base; | |
6989 | spelling_size = p->spelling_size; | |
6990 | constructor_top_level = p->top_level; | |
6991 | initializer_stack = p->next; | |
6992 | free (p); | |
b71c7f8a | 6993 | } |
400fbf9f | 6994 | \f |
3e4093b6 RS |
6995 | /* Call here when we see the initializer is surrounded by braces. |
6996 | This is instead of a call to push_init_level; | |
6997 | it is matched by a call to pop_init_level. | |
400fbf9f | 6998 | |
3e4093b6 RS |
6999 | TYPE is the type to initialize, for a constructor expression. |
7000 | For an initializer for a decl, TYPE is zero. */ | |
400fbf9f | 7001 | |
3e4093b6 RS |
7002 | void |
7003 | really_start_incremental_init (tree type) | |
400fbf9f | 7004 | { |
5d038c4c | 7005 | struct constructor_stack *p = XNEW (struct constructor_stack); |
400fbf9f | 7006 | |
3e4093b6 RS |
7007 | if (type == 0) |
7008 | type = TREE_TYPE (constructor_decl); | |
400fbf9f | 7009 | |
b6fc2cdb PB |
7010 | if (TREE_CODE (type) == VECTOR_TYPE |
7011 | && TYPE_VECTOR_OPAQUE (type)) | |
3e4093b6 | 7012 | error ("opaque vector types cannot be initialized"); |
400fbf9f | 7013 | |
3e4093b6 RS |
7014 | p->type = constructor_type; |
7015 | p->fields = constructor_fields; | |
7016 | p->index = constructor_index; | |
7017 | p->max_index = constructor_max_index; | |
7018 | p->unfilled_index = constructor_unfilled_index; | |
7019 | p->unfilled_fields = constructor_unfilled_fields; | |
7020 | p->bit_index = constructor_bit_index; | |
7021 | p->elements = constructor_elements; | |
7022 | p->constant = constructor_constant; | |
7023 | p->simple = constructor_simple; | |
928c19bb | 7024 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
7025 | p->erroneous = constructor_erroneous; |
7026 | p->pending_elts = constructor_pending_elts; | |
7027 | p->depth = constructor_depth; | |
916c5919 JM |
7028 | p->replacement_value.value = 0; |
7029 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 7030 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
7031 | p->implicit = 0; |
7032 | p->range_stack = 0; | |
7033 | p->outer = 0; | |
7034 | p->incremental = constructor_incremental; | |
7035 | p->designated = constructor_designated; | |
7036 | p->next = 0; | |
7037 | constructor_stack = p; | |
b13aca19 | 7038 | |
3e4093b6 RS |
7039 | constructor_constant = 1; |
7040 | constructor_simple = 1; | |
928c19bb | 7041 | constructor_nonconst = 0; |
3e4093b6 | 7042 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 7043 | constructor_elements = NULL; |
3e4093b6 RS |
7044 | constructor_pending_elts = 0; |
7045 | constructor_type = type; | |
7046 | constructor_incremental = 1; | |
7047 | constructor_designated = 0; | |
7048 | designator_depth = 0; | |
b06df647 | 7049 | designator_erroneous = 0; |
400fbf9f | 7050 | |
3e4093b6 RS |
7051 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
7052 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 7053 | { |
3e4093b6 RS |
7054 | constructor_fields = TYPE_FIELDS (constructor_type); |
7055 | /* Skip any nameless bit fields at the beginning. */ | |
7056 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
7057 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 7058 | constructor_fields = DECL_CHAIN (constructor_fields); |
05bccae2 | 7059 | |
3e4093b6 RS |
7060 | constructor_unfilled_fields = constructor_fields; |
7061 | constructor_bit_index = bitsize_zero_node; | |
400fbf9f | 7062 | } |
3e4093b6 RS |
7063 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
7064 | { | |
7065 | if (TYPE_DOMAIN (constructor_type)) | |
7066 | { | |
7067 | constructor_max_index | |
7068 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 7069 | |
3e4093b6 RS |
7070 | /* Detect non-empty initializations of zero-length arrays. */ |
7071 | if (constructor_max_index == NULL_TREE | |
7072 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 7073 | constructor_max_index = integer_minus_one_node; |
400fbf9f | 7074 | |
3e4093b6 RS |
7075 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
7076 | to initialize VLAs will cause a proper error; avoid tree | |
7077 | checking errors as well by setting a safe value. */ | |
7078 | if (constructor_max_index | |
7079 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 7080 | constructor_max_index = integer_minus_one_node; |
59c83dbf | 7081 | |
3e4093b6 RS |
7082 | constructor_index |
7083 | = convert (bitsizetype, | |
7084 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
59c83dbf | 7085 | } |
3e4093b6 | 7086 | else |
493179da JM |
7087 | { |
7088 | constructor_index = bitsize_zero_node; | |
7089 | constructor_max_index = NULL_TREE; | |
7090 | } | |
59c83dbf | 7091 | |
3e4093b6 RS |
7092 | constructor_unfilled_index = constructor_index; |
7093 | } | |
7094 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
7095 | { | |
7096 | /* Vectors are like simple fixed-size arrays. */ | |
7097 | constructor_max_index = | |
c62c040f | 7098 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
a0f0ab9f | 7099 | constructor_index = bitsize_zero_node; |
3e4093b6 RS |
7100 | constructor_unfilled_index = constructor_index; |
7101 | } | |
7102 | else | |
7103 | { | |
7104 | /* Handle the case of int x = {5}; */ | |
7105 | constructor_fields = constructor_type; | |
7106 | constructor_unfilled_fields = constructor_type; | |
7107 | } | |
7108 | } | |
7109 | \f | |
7110 | /* Push down into a subobject, for initialization. | |
7111 | If this is for an explicit set of braces, IMPLICIT is 0. | |
7112 | If it is because the next element belongs at a lower level, | |
7113 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
400fbf9f | 7114 | |
3e4093b6 | 7115 | void |
a1e3b3d9 | 7116 | push_init_level (int implicit, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7117 | { |
7118 | struct constructor_stack *p; | |
7119 | tree value = NULL_TREE; | |
400fbf9f | 7120 | |
3e4093b6 | 7121 | /* If we've exhausted any levels that didn't have braces, |
472d98b4 JM |
7122 | pop them now. If implicit == 1, this will have been done in |
7123 | process_init_element; do not repeat it here because in the case | |
7124 | of excess initializers for an empty aggregate this leads to an | |
7125 | infinite cycle of popping a level and immediately recreating | |
7126 | it. */ | |
7127 | if (implicit != 1) | |
3e4093b6 | 7128 | { |
472d98b4 JM |
7129 | while (constructor_stack->implicit) |
7130 | { | |
7131 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7132 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7133 | && constructor_fields == 0) | |
a1e3b3d9 LB |
7134 | process_init_element (pop_init_level (1, braced_init_obstack), |
7135 | true, braced_init_obstack); | |
472d98b4 JM |
7136 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE |
7137 | && constructor_max_index | |
7138 | && tree_int_cst_lt (constructor_max_index, | |
7139 | constructor_index)) | |
a1e3b3d9 LB |
7140 | process_init_element (pop_init_level (1, braced_init_obstack), |
7141 | true, braced_init_obstack); | |
472d98b4 JM |
7142 | else |
7143 | break; | |
7144 | } | |
3e4093b6 | 7145 | } |
400fbf9f | 7146 | |
3e4093b6 RS |
7147 | /* Unless this is an explicit brace, we need to preserve previous |
7148 | content if any. */ | |
7149 | if (implicit) | |
7150 | { | |
7151 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7152 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7153 | && constructor_fields) | |
a1e3b3d9 | 7154 | value = find_init_member (constructor_fields, braced_init_obstack); |
3e4093b6 | 7155 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
a1e3b3d9 | 7156 | value = find_init_member (constructor_index, braced_init_obstack); |
400fbf9f JW |
7157 | } |
7158 | ||
5d038c4c | 7159 | p = XNEW (struct constructor_stack); |
3e4093b6 RS |
7160 | p->type = constructor_type; |
7161 | p->fields = constructor_fields; | |
7162 | p->index = constructor_index; | |
7163 | p->max_index = constructor_max_index; | |
7164 | p->unfilled_index = constructor_unfilled_index; | |
7165 | p->unfilled_fields = constructor_unfilled_fields; | |
7166 | p->bit_index = constructor_bit_index; | |
7167 | p->elements = constructor_elements; | |
7168 | p->constant = constructor_constant; | |
7169 | p->simple = constructor_simple; | |
928c19bb | 7170 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
7171 | p->erroneous = constructor_erroneous; |
7172 | p->pending_elts = constructor_pending_elts; | |
7173 | p->depth = constructor_depth; | |
916c5919 JM |
7174 | p->replacement_value.value = 0; |
7175 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 7176 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
7177 | p->implicit = implicit; |
7178 | p->outer = 0; | |
7179 | p->incremental = constructor_incremental; | |
7180 | p->designated = constructor_designated; | |
7181 | p->next = constructor_stack; | |
7182 | p->range_stack = 0; | |
7183 | constructor_stack = p; | |
400fbf9f | 7184 | |
3e4093b6 RS |
7185 | constructor_constant = 1; |
7186 | constructor_simple = 1; | |
928c19bb | 7187 | constructor_nonconst = 0; |
3e4093b6 | 7188 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 7189 | constructor_elements = NULL; |
3e4093b6 RS |
7190 | constructor_incremental = 1; |
7191 | constructor_designated = 0; | |
7192 | constructor_pending_elts = 0; | |
7193 | if (!implicit) | |
400fbf9f | 7194 | { |
3e4093b6 RS |
7195 | p->range_stack = constructor_range_stack; |
7196 | constructor_range_stack = 0; | |
7197 | designator_depth = 0; | |
b06df647 | 7198 | designator_erroneous = 0; |
3e4093b6 | 7199 | } |
400fbf9f | 7200 | |
3e4093b6 RS |
7201 | /* Don't die if an entire brace-pair level is superfluous |
7202 | in the containing level. */ | |
7203 | if (constructor_type == 0) | |
7204 | ; | |
7205 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
7206 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7207 | { | |
7208 | /* Don't die if there are extra init elts at the end. */ | |
7209 | if (constructor_fields == 0) | |
7210 | constructor_type = 0; | |
7211 | else | |
400fbf9f | 7212 | { |
3e4093b6 RS |
7213 | constructor_type = TREE_TYPE (constructor_fields); |
7214 | push_member_name (constructor_fields); | |
7215 | constructor_depth++; | |
400fbf9f | 7216 | } |
3e4093b6 RS |
7217 | } |
7218 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7219 | { | |
7220 | constructor_type = TREE_TYPE (constructor_type); | |
ae7e9ddd | 7221 | push_array_bounds (tree_to_uhwi (constructor_index)); |
3e4093b6 | 7222 | constructor_depth++; |
400fbf9f JW |
7223 | } |
7224 | ||
3e4093b6 | 7225 | if (constructor_type == 0) |
400fbf9f | 7226 | { |
3e4093b6 RS |
7227 | error_init ("extra brace group at end of initializer"); |
7228 | constructor_fields = 0; | |
7229 | constructor_unfilled_fields = 0; | |
7230 | return; | |
400fbf9f JW |
7231 | } |
7232 | ||
3e4093b6 RS |
7233 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
7234 | { | |
7235 | constructor_constant = TREE_CONSTANT (value); | |
7236 | constructor_simple = TREE_STATIC (value); | |
928c19bb | 7237 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); |
3e4093b6 | 7238 | constructor_elements = CONSTRUCTOR_ELTS (value); |
9771b263 | 7239 | if (!vec_safe_is_empty (constructor_elements) |
3e4093b6 RS |
7240 | && (TREE_CODE (constructor_type) == RECORD_TYPE |
7241 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
a1e3b3d9 | 7242 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 7243 | } |
400fbf9f | 7244 | |
3e4093b6 RS |
7245 | if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) |
7246 | { | |
7247 | missing_braces_mentioned = 1; | |
683d6ff9 | 7248 | warning_init (OPT_Wmissing_braces, "missing braces around initializer"); |
3e4093b6 | 7249 | } |
400fbf9f | 7250 | |
3e4093b6 RS |
7251 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
7252 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7253 | { | |
7254 | constructor_fields = TYPE_FIELDS (constructor_type); | |
7255 | /* Skip any nameless bit fields at the beginning. */ | |
7256 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
7257 | && DECL_NAME (constructor_fields) == 0) | |
f7587ed0 | 7258 | constructor_fields = DECL_CHAIN (constructor_fields); |
103b7b17 | 7259 | |
3e4093b6 RS |
7260 | constructor_unfilled_fields = constructor_fields; |
7261 | constructor_bit_index = bitsize_zero_node; | |
7262 | } | |
7263 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
7264 | { | |
7265 | /* Vectors are like simple fixed-size arrays. */ | |
7266 | constructor_max_index = | |
c62c040f RG |
7267 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
7268 | constructor_index = bitsize_int (0); | |
3e4093b6 RS |
7269 | constructor_unfilled_index = constructor_index; |
7270 | } | |
7271 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7272 | { | |
7273 | if (TYPE_DOMAIN (constructor_type)) | |
7274 | { | |
7275 | constructor_max_index | |
7276 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 7277 | |
3e4093b6 RS |
7278 | /* Detect non-empty initializations of zero-length arrays. */ |
7279 | if (constructor_max_index == NULL_TREE | |
7280 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 7281 | constructor_max_index = integer_minus_one_node; |
de520661 | 7282 | |
3e4093b6 RS |
7283 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
7284 | to initialize VLAs will cause a proper error; avoid tree | |
7285 | checking errors as well by setting a safe value. */ | |
7286 | if (constructor_max_index | |
7287 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 7288 | constructor_max_index = integer_minus_one_node; |
b62acd60 | 7289 | |
3e4093b6 RS |
7290 | constructor_index |
7291 | = convert (bitsizetype, | |
7292 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7293 | } | |
7294 | else | |
7295 | constructor_index = bitsize_zero_node; | |
de520661 | 7296 | |
3e4093b6 RS |
7297 | constructor_unfilled_index = constructor_index; |
7298 | if (value && TREE_CODE (value) == STRING_CST) | |
7299 | { | |
7300 | /* We need to split the char/wchar array into individual | |
7301 | characters, so that we don't have to special case it | |
7302 | everywhere. */ | |
a1e3b3d9 | 7303 | set_nonincremental_init_from_string (value, braced_init_obstack); |
3e4093b6 RS |
7304 | } |
7305 | } | |
7306 | else | |
7307 | { | |
b4519d39 | 7308 | if (constructor_type != error_mark_node) |
683d6ff9 | 7309 | warning_init (0, "braces around scalar initializer"); |
3e4093b6 RS |
7310 | constructor_fields = constructor_type; |
7311 | constructor_unfilled_fields = constructor_type; | |
7312 | } | |
7313 | } | |
8b6a5902 | 7314 | |
3e4093b6 | 7315 | /* At the end of an implicit or explicit brace level, |
916c5919 JM |
7316 | finish up that level of constructor. If a single expression |
7317 | with redundant braces initialized that level, return the | |
7318 | c_expr structure for that expression. Otherwise, the original_code | |
7319 | element is set to ERROR_MARK. | |
7320 | If we were outputting the elements as they are read, return 0 as the value | |
3e4093b6 | 7321 | from inner levels (process_init_element ignores that), |
916c5919 | 7322 | but return error_mark_node as the value from the outermost level |
3e4093b6 | 7323 | (that's what we want to put in DECL_INITIAL). |
916c5919 | 7324 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
de520661 | 7325 | |
916c5919 | 7326 | struct c_expr |
a1e3b3d9 | 7327 | pop_init_level (int implicit, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7328 | { |
7329 | struct constructor_stack *p; | |
916c5919 JM |
7330 | struct c_expr ret; |
7331 | ret.value = 0; | |
7332 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 7333 | ret.original_type = NULL; |
de520661 | 7334 | |
3e4093b6 RS |
7335 | if (implicit == 0) |
7336 | { | |
7337 | /* When we come to an explicit close brace, | |
7338 | pop any inner levels that didn't have explicit braces. */ | |
7339 | while (constructor_stack->implicit) | |
a1e3b3d9 LB |
7340 | { |
7341 | process_init_element (pop_init_level (1, braced_init_obstack), | |
7342 | true, braced_init_obstack); | |
7343 | } | |
366de0ce | 7344 | gcc_assert (!constructor_range_stack); |
3e4093b6 | 7345 | } |
e5e809f4 | 7346 | |
0066ef9c RH |
7347 | /* Now output all pending elements. */ |
7348 | constructor_incremental = 1; | |
a1e3b3d9 | 7349 | output_pending_init_elements (1, braced_init_obstack); |
0066ef9c | 7350 | |
3e4093b6 | 7351 | p = constructor_stack; |
e5e809f4 | 7352 | |
3e4093b6 RS |
7353 | /* Error for initializing a flexible array member, or a zero-length |
7354 | array member in an inappropriate context. */ | |
7355 | if (constructor_type && constructor_fields | |
7356 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
7357 | && TYPE_DOMAIN (constructor_type) | |
3f75a254 | 7358 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
3e4093b6 RS |
7359 | { |
7360 | /* Silently discard empty initializations. The parser will | |
7361 | already have pedwarned for empty brackets. */ | |
7362 | if (integer_zerop (constructor_unfilled_index)) | |
7363 | constructor_type = NULL_TREE; | |
366de0ce | 7364 | else |
3e4093b6 | 7365 | { |
366de0ce | 7366 | gcc_assert (!TYPE_SIZE (constructor_type)); |
c22cacf3 | 7367 | |
3e4093b6 RS |
7368 | if (constructor_depth > 2) |
7369 | error_init ("initialization of flexible array member in a nested context"); | |
fcf73884 | 7370 | else |
c1771a20 | 7371 | pedwarn_init (input_location, OPT_Wpedantic, |
509c9d60 | 7372 | "initialization of a flexible array member"); |
de520661 | 7373 | |
3e4093b6 RS |
7374 | /* We have already issued an error message for the existence |
7375 | of a flexible array member not at the end of the structure. | |
535a42b1 | 7376 | Discard the initializer so that we do not die later. */ |
910ad8de | 7377 | if (DECL_CHAIN (constructor_fields) != NULL_TREE) |
3e4093b6 RS |
7378 | constructor_type = NULL_TREE; |
7379 | } | |
3e4093b6 | 7380 | } |
de520661 | 7381 | |
3e4093b6 | 7382 | /* Warn when some struct elements are implicitly initialized to zero. */ |
eaac4679 | 7383 | if (warn_missing_field_initializers |
3e4093b6 RS |
7384 | && constructor_type |
7385 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
7386 | && constructor_unfilled_fields) | |
7387 | { | |
49819fef | 7388 | bool constructor_zeroinit = |
9771b263 DN |
7389 | (vec_safe_length (constructor_elements) == 1 |
7390 | && integer_zerop ((*constructor_elements)[0].value)); | |
49819fef | 7391 | |
3e4093b6 RS |
7392 | /* Do not warn for flexible array members or zero-length arrays. */ |
7393 | while (constructor_unfilled_fields | |
3f75a254 | 7394 | && (!DECL_SIZE (constructor_unfilled_fields) |
3e4093b6 | 7395 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) |
910ad8de | 7396 | constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); |
cc77d4d5 | 7397 | |
49819fef AM |
7398 | if (constructor_unfilled_fields |
7399 | /* Do not warn if this level of the initializer uses member | |
7400 | designators; it is likely to be deliberate. */ | |
7401 | && !constructor_designated | |
7402 | /* Do not warn about initializing with ` = {0}'. */ | |
7403 | && !constructor_zeroinit) | |
3e4093b6 | 7404 | { |
32397f22 MLI |
7405 | if (warning_at (input_location, OPT_Wmissing_field_initializers, |
7406 | "missing initializer for field %qD of %qT", | |
7407 | constructor_unfilled_fields, | |
7408 | constructor_type)) | |
7409 | inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), | |
ae933128 | 7410 | "%qD declared here", constructor_unfilled_fields); |
3e4093b6 RS |
7411 | } |
7412 | } | |
de520661 | 7413 | |
3e4093b6 | 7414 | /* Pad out the end of the structure. */ |
916c5919 | 7415 | if (p->replacement_value.value) |
3e4093b6 RS |
7416 | /* If this closes a superfluous brace pair, |
7417 | just pass out the element between them. */ | |
916c5919 | 7418 | ret = p->replacement_value; |
3e4093b6 RS |
7419 | else if (constructor_type == 0) |
7420 | ; | |
7421 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7422 | && TREE_CODE (constructor_type) != UNION_TYPE | |
7423 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
7424 | && TREE_CODE (constructor_type) != VECTOR_TYPE) | |
7425 | { | |
7426 | /* A nonincremental scalar initializer--just return | |
7427 | the element, after verifying there is just one. */ | |
9771b263 | 7428 | if (vec_safe_is_empty (constructor_elements)) |
3e4093b6 RS |
7429 | { |
7430 | if (!constructor_erroneous) | |
7431 | error_init ("empty scalar initializer"); | |
916c5919 | 7432 | ret.value = error_mark_node; |
3e4093b6 | 7433 | } |
9771b263 | 7434 | else if (vec_safe_length (constructor_elements) != 1) |
3e4093b6 RS |
7435 | { |
7436 | error_init ("extra elements in scalar initializer"); | |
9771b263 | 7437 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7438 | } |
7439 | else | |
9771b263 | 7440 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7441 | } |
7442 | else | |
7443 | { | |
7444 | if (constructor_erroneous) | |
916c5919 | 7445 | ret.value = error_mark_node; |
3e4093b6 RS |
7446 | else |
7447 | { | |
916c5919 | 7448 | ret.value = build_constructor (constructor_type, |
4038c495 | 7449 | constructor_elements); |
3e4093b6 | 7450 | if (constructor_constant) |
51eed280 | 7451 | TREE_CONSTANT (ret.value) = 1; |
3e4093b6 | 7452 | if (constructor_constant && constructor_simple) |
916c5919 | 7453 | TREE_STATIC (ret.value) = 1; |
928c19bb JM |
7454 | if (constructor_nonconst) |
7455 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
3e4093b6 RS |
7456 | } |
7457 | } | |
de520661 | 7458 | |
928c19bb JM |
7459 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) |
7460 | { | |
7461 | if (constructor_nonconst) | |
7462 | ret.original_code = C_MAYBE_CONST_EXPR; | |
7463 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
7464 | ret.original_code = ERROR_MARK; | |
7465 | } | |
7466 | ||
3e4093b6 RS |
7467 | constructor_type = p->type; |
7468 | constructor_fields = p->fields; | |
7469 | constructor_index = p->index; | |
7470 | constructor_max_index = p->max_index; | |
7471 | constructor_unfilled_index = p->unfilled_index; | |
7472 | constructor_unfilled_fields = p->unfilled_fields; | |
7473 | constructor_bit_index = p->bit_index; | |
7474 | constructor_elements = p->elements; | |
7475 | constructor_constant = p->constant; | |
7476 | constructor_simple = p->simple; | |
928c19bb | 7477 | constructor_nonconst = p->nonconst; |
3e4093b6 RS |
7478 | constructor_erroneous = p->erroneous; |
7479 | constructor_incremental = p->incremental; | |
7480 | constructor_designated = p->designated; | |
7481 | constructor_pending_elts = p->pending_elts; | |
7482 | constructor_depth = p->depth; | |
7483 | if (!p->implicit) | |
7484 | constructor_range_stack = p->range_stack; | |
7485 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
de520661 | 7486 | |
3e4093b6 RS |
7487 | constructor_stack = p->next; |
7488 | free (p); | |
b621a4dd | 7489 | |
5d5e98dc VR |
7490 | if (ret.value == 0 && constructor_stack == 0) |
7491 | ret.value = error_mark_node; | |
916c5919 | 7492 | return ret; |
3e4093b6 | 7493 | } |
8b6a5902 | 7494 | |
3e4093b6 RS |
7495 | /* Common handling for both array range and field name designators. |
7496 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
400fbf9f | 7497 | |
3e4093b6 | 7498 | static int |
a1e3b3d9 | 7499 | set_designator (int array, struct obstack * braced_init_obstack) |
de520661 | 7500 | { |
3e4093b6 RS |
7501 | tree subtype; |
7502 | enum tree_code subcode; | |
de520661 | 7503 | |
3e4093b6 RS |
7504 | /* Don't die if an entire brace-pair level is superfluous |
7505 | in the containing level. */ | |
7506 | if (constructor_type == 0) | |
7507 | return 1; | |
de520661 | 7508 | |
366de0ce NS |
7509 | /* If there were errors in this designator list already, bail out |
7510 | silently. */ | |
b06df647 | 7511 | if (designator_erroneous) |
3e4093b6 | 7512 | return 1; |
e28cae4f | 7513 | |
3e4093b6 RS |
7514 | if (!designator_depth) |
7515 | { | |
366de0ce | 7516 | gcc_assert (!constructor_range_stack); |
de520661 | 7517 | |
3e4093b6 RS |
7518 | /* Designator list starts at the level of closest explicit |
7519 | braces. */ | |
7520 | while (constructor_stack->implicit) | |
a1e3b3d9 LB |
7521 | { |
7522 | process_init_element (pop_init_level (1, braced_init_obstack), | |
7523 | true, braced_init_obstack); | |
7524 | } | |
3e4093b6 RS |
7525 | constructor_designated = 1; |
7526 | return 0; | |
7527 | } | |
de520661 | 7528 | |
366de0ce | 7529 | switch (TREE_CODE (constructor_type)) |
3c3fa147 | 7530 | { |
366de0ce NS |
7531 | case RECORD_TYPE: |
7532 | case UNION_TYPE: | |
3e4093b6 RS |
7533 | subtype = TREE_TYPE (constructor_fields); |
7534 | if (subtype != error_mark_node) | |
7535 | subtype = TYPE_MAIN_VARIANT (subtype); | |
366de0ce NS |
7536 | break; |
7537 | case ARRAY_TYPE: | |
3e4093b6 | 7538 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
366de0ce NS |
7539 | break; |
7540 | default: | |
7541 | gcc_unreachable (); | |
de520661 | 7542 | } |
400fbf9f | 7543 | |
3e4093b6 RS |
7544 | subcode = TREE_CODE (subtype); |
7545 | if (array && subcode != ARRAY_TYPE) | |
7546 | { | |
7547 | error_init ("array index in non-array initializer"); | |
7548 | return 1; | |
7549 | } | |
7550 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
7551 | { | |
7552 | error_init ("field name not in record or union initializer"); | |
7553 | return 1; | |
7554 | } | |
d45cf215 | 7555 | |
3e4093b6 | 7556 | constructor_designated = 1; |
a1e3b3d9 | 7557 | push_init_level (2, braced_init_obstack); |
3e4093b6 | 7558 | return 0; |
de520661 | 7559 | } |
400fbf9f | 7560 | |
3e4093b6 RS |
7561 | /* If there are range designators in designator list, push a new designator |
7562 | to constructor_range_stack. RANGE_END is end of such stack range or | |
7563 | NULL_TREE if there is no range designator at this level. */ | |
400fbf9f | 7564 | |
3e4093b6 | 7565 | static void |
a1e3b3d9 | 7566 | push_range_stack (tree range_end, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7567 | { |
7568 | struct constructor_range_stack *p; | |
400fbf9f | 7569 | |
a1e3b3d9 LB |
7570 | p = (struct constructor_range_stack *) |
7571 | obstack_alloc (braced_init_obstack, | |
7572 | sizeof (struct constructor_range_stack)); | |
3e4093b6 RS |
7573 | p->prev = constructor_range_stack; |
7574 | p->next = 0; | |
7575 | p->fields = constructor_fields; | |
7576 | p->range_start = constructor_index; | |
7577 | p->index = constructor_index; | |
7578 | p->stack = constructor_stack; | |
7579 | p->range_end = range_end; | |
8b6a5902 | 7580 | if (constructor_range_stack) |
3e4093b6 RS |
7581 | constructor_range_stack->next = p; |
7582 | constructor_range_stack = p; | |
de520661 | 7583 | } |
400fbf9f | 7584 | |
3e4093b6 RS |
7585 | /* Within an array initializer, specify the next index to be initialized. |
7586 | FIRST is that index. If LAST is nonzero, then initialize a range | |
7587 | of indices, running from FIRST through LAST. */ | |
5a7ec9d9 | 7588 | |
de520661 | 7589 | void |
a1e3b3d9 LB |
7590 | set_init_index (tree first, tree last, |
7591 | struct obstack * braced_init_obstack) | |
de520661 | 7592 | { |
a1e3b3d9 | 7593 | if (set_designator (1, braced_init_obstack)) |
3e4093b6 | 7594 | return; |
de520661 | 7595 | |
b06df647 | 7596 | designator_erroneous = 1; |
de520661 | 7597 | |
3ea8cd06 JM |
7598 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
7599 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
7600 | { | |
7601 | error_init ("array index in initializer not of integer type"); | |
7602 | return; | |
7603 | } | |
7604 | ||
2b6da65c JM |
7605 | if (TREE_CODE (first) != INTEGER_CST) |
7606 | { | |
7607 | first = c_fully_fold (first, false, NULL); | |
7608 | if (TREE_CODE (first) == INTEGER_CST) | |
c1771a20 | 7609 | pedwarn_init (input_location, OPT_Wpedantic, |
2b6da65c JM |
7610 | "array index in initializer is not " |
7611 | "an integer constant expression"); | |
7612 | } | |
7613 | ||
7614 | if (last && TREE_CODE (last) != INTEGER_CST) | |
7615 | { | |
7616 | last = c_fully_fold (last, false, NULL); | |
7617 | if (TREE_CODE (last) == INTEGER_CST) | |
c1771a20 | 7618 | pedwarn_init (input_location, OPT_Wpedantic, |
2b6da65c JM |
7619 | "array index in initializer is not " |
7620 | "an integer constant expression"); | |
7621 | } | |
7622 | ||
3e4093b6 RS |
7623 | if (TREE_CODE (first) != INTEGER_CST) |
7624 | error_init ("nonconstant array index in initializer"); | |
7625 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) | |
7626 | error_init ("nonconstant array index in initializer"); | |
7627 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7628 | error_init ("array index in non-array initializer"); | |
622adc7e MK |
7629 | else if (tree_int_cst_sgn (first) == -1) |
7630 | error_init ("array index in initializer exceeds array bounds"); | |
3e4093b6 RS |
7631 | else if (constructor_max_index |
7632 | && tree_int_cst_lt (constructor_max_index, first)) | |
7633 | error_init ("array index in initializer exceeds array bounds"); | |
7634 | else | |
de520661 | 7635 | { |
928c19bb JM |
7636 | constant_expression_warning (first); |
7637 | if (last) | |
7638 | constant_expression_warning (last); | |
3e4093b6 | 7639 | constructor_index = convert (bitsizetype, first); |
40d3d530 JR |
7640 | if (tree_int_cst_lt (constructor_index, first)) |
7641 | { | |
7642 | constructor_index = copy_node (constructor_index); | |
7643 | TREE_OVERFLOW (constructor_index) = 1; | |
7644 | } | |
665f2503 | 7645 | |
3e4093b6 | 7646 | if (last) |
2bede729 | 7647 | { |
3e4093b6 RS |
7648 | if (tree_int_cst_equal (first, last)) |
7649 | last = 0; | |
7650 | else if (tree_int_cst_lt (last, first)) | |
7651 | { | |
7652 | error_init ("empty index range in initializer"); | |
7653 | last = 0; | |
7654 | } | |
7655 | else | |
7656 | { | |
7657 | last = convert (bitsizetype, last); | |
7658 | if (constructor_max_index != 0 | |
7659 | && tree_int_cst_lt (constructor_max_index, last)) | |
7660 | { | |
7661 | error_init ("array index range in initializer exceeds array bounds"); | |
7662 | last = 0; | |
7663 | } | |
7664 | } | |
2bede729 | 7665 | } |
fed3cef0 | 7666 | |
3e4093b6 | 7667 | designator_depth++; |
b06df647 | 7668 | designator_erroneous = 0; |
3e4093b6 | 7669 | if (constructor_range_stack || last) |
a1e3b3d9 | 7670 | push_range_stack (last, braced_init_obstack); |
de520661 | 7671 | } |
de520661 | 7672 | } |
3e4093b6 RS |
7673 | |
7674 | /* Within a struct initializer, specify the next field to be initialized. */ | |
400fbf9f | 7675 | |
de520661 | 7676 | void |
a1e3b3d9 | 7677 | set_init_label (tree fieldname, struct obstack * braced_init_obstack) |
de520661 | 7678 | { |
0fb96aa4 | 7679 | tree field; |
94ba5069 | 7680 | |
a1e3b3d9 | 7681 | if (set_designator (0, braced_init_obstack)) |
3e4093b6 RS |
7682 | return; |
7683 | ||
b06df647 | 7684 | designator_erroneous = 1; |
3e4093b6 RS |
7685 | |
7686 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7687 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
94ba5069 | 7688 | { |
3e4093b6 RS |
7689 | error_init ("field name not in record or union initializer"); |
7690 | return; | |
94ba5069 RS |
7691 | } |
7692 | ||
0fb96aa4 | 7693 | field = lookup_field (constructor_type, fieldname); |
8b6a5902 | 7694 | |
0fb96aa4 | 7695 | if (field == 0) |
c51a1ba9 | 7696 | error ("unknown field %qE specified in initializer", fieldname); |
3e4093b6 | 7697 | else |
0fb96aa4 JM |
7698 | do |
7699 | { | |
7700 | constructor_fields = TREE_VALUE (field); | |
7701 | designator_depth++; | |
7702 | designator_erroneous = 0; | |
7703 | if (constructor_range_stack) | |
7704 | push_range_stack (NULL_TREE, braced_init_obstack); | |
7705 | field = TREE_CHAIN (field); | |
7706 | if (field) | |
7707 | { | |
7708 | if (set_designator (0, braced_init_obstack)) | |
7709 | return; | |
7710 | } | |
7711 | } | |
7712 | while (field != NULL_TREE); | |
3e4093b6 RS |
7713 | } |
7714 | \f | |
7715 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
7716 | identifies the initializer, either array index or field in a structure. | |
bbbbb16a ILT |
7717 | VALUE is the value of that index or field. If ORIGTYPE is not |
7718 | NULL_TREE, it is the original type of VALUE. | |
b295aee2 JJ |
7719 | |
7720 | IMPLICIT is true if value comes from pop_init_level (1), | |
7721 | the new initializer has been merged with the existing one | |
7722 | and thus no warnings should be emitted about overriding an | |
7723 | existing initializer. */ | |
de520661 | 7724 | |
3e4093b6 | 7725 | static void |
a1e3b3d9 LB |
7726 | add_pending_init (tree purpose, tree value, tree origtype, bool implicit, |
7727 | struct obstack * braced_init_obstack) | |
3e4093b6 RS |
7728 | { |
7729 | struct init_node *p, **q, *r; | |
7730 | ||
7731 | q = &constructor_pending_elts; | |
7732 | p = 0; | |
7733 | ||
7734 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 7735 | { |
3e4093b6 | 7736 | while (*q != 0) |
91fa3c30 | 7737 | { |
3e4093b6 RS |
7738 | p = *q; |
7739 | if (tree_int_cst_lt (purpose, p->purpose)) | |
7740 | q = &p->left; | |
7741 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
7742 | q = &p->right; | |
7743 | else | |
7744 | { | |
b295aee2 JJ |
7745 | if (!implicit) |
7746 | { | |
7747 | if (TREE_SIDE_EFFECTS (p->value)) | |
7748 | warning_init (0, "initialized field with side-effects overwritten"); | |
7749 | else if (warn_override_init) | |
7750 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7751 | } | |
3e4093b6 | 7752 | p->value = value; |
bbbbb16a | 7753 | p->origtype = origtype; |
3e4093b6 RS |
7754 | return; |
7755 | } | |
91fa3c30 | 7756 | } |
de520661 | 7757 | } |
3e4093b6 | 7758 | else |
de520661 | 7759 | { |
3e4093b6 | 7760 | tree bitpos; |
400fbf9f | 7761 | |
3e4093b6 RS |
7762 | bitpos = bit_position (purpose); |
7763 | while (*q != NULL) | |
7764 | { | |
7765 | p = *q; | |
7766 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7767 | q = &p->left; | |
7768 | else if (p->purpose != purpose) | |
7769 | q = &p->right; | |
7770 | else | |
7771 | { | |
b295aee2 JJ |
7772 | if (!implicit) |
7773 | { | |
7774 | if (TREE_SIDE_EFFECTS (p->value)) | |
7775 | warning_init (0, "initialized field with side-effects overwritten"); | |
7776 | else if (warn_override_init) | |
7777 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7778 | } | |
3e4093b6 | 7779 | p->value = value; |
bbbbb16a | 7780 | p->origtype = origtype; |
3e4093b6 RS |
7781 | return; |
7782 | } | |
7783 | } | |
91fa3c30 | 7784 | } |
b71c7f8a | 7785 | |
a1e3b3d9 LB |
7786 | r = (struct init_node *) obstack_alloc (braced_init_obstack, |
7787 | sizeof (struct init_node)); | |
3e4093b6 RS |
7788 | r->purpose = purpose; |
7789 | r->value = value; | |
bbbbb16a | 7790 | r->origtype = origtype; |
8b6a5902 | 7791 | |
3e4093b6 RS |
7792 | *q = r; |
7793 | r->parent = p; | |
7794 | r->left = 0; | |
7795 | r->right = 0; | |
7796 | r->balance = 0; | |
b71c7f8a | 7797 | |
3e4093b6 | 7798 | while (p) |
de520661 | 7799 | { |
3e4093b6 | 7800 | struct init_node *s; |
665f2503 | 7801 | |
3e4093b6 | 7802 | if (r == p->left) |
2bede729 | 7803 | { |
3e4093b6 RS |
7804 | if (p->balance == 0) |
7805 | p->balance = -1; | |
7806 | else if (p->balance < 0) | |
7807 | { | |
7808 | if (r->balance < 0) | |
7809 | { | |
7810 | /* L rotation. */ | |
7811 | p->left = r->right; | |
7812 | if (p->left) | |
7813 | p->left->parent = p; | |
7814 | r->right = p; | |
e7b6a0ee | 7815 | |
3e4093b6 RS |
7816 | p->balance = 0; |
7817 | r->balance = 0; | |
39bc99c2 | 7818 | |
3e4093b6 RS |
7819 | s = p->parent; |
7820 | p->parent = r; | |
7821 | r->parent = s; | |
7822 | if (s) | |
7823 | { | |
7824 | if (s->left == p) | |
7825 | s->left = r; | |
7826 | else | |
7827 | s->right = r; | |
7828 | } | |
7829 | else | |
7830 | constructor_pending_elts = r; | |
7831 | } | |
7832 | else | |
7833 | { | |
7834 | /* LR rotation. */ | |
7835 | struct init_node *t = r->right; | |
e7b6a0ee | 7836 | |
3e4093b6 RS |
7837 | r->right = t->left; |
7838 | if (r->right) | |
7839 | r->right->parent = r; | |
7840 | t->left = r; | |
7841 | ||
7842 | p->left = t->right; | |
7843 | if (p->left) | |
7844 | p->left->parent = p; | |
7845 | t->right = p; | |
7846 | ||
7847 | p->balance = t->balance < 0; | |
7848 | r->balance = -(t->balance > 0); | |
7849 | t->balance = 0; | |
7850 | ||
7851 | s = p->parent; | |
7852 | p->parent = t; | |
7853 | r->parent = t; | |
7854 | t->parent = s; | |
7855 | if (s) | |
7856 | { | |
7857 | if (s->left == p) | |
7858 | s->left = t; | |
7859 | else | |
7860 | s->right = t; | |
7861 | } | |
7862 | else | |
7863 | constructor_pending_elts = t; | |
7864 | } | |
7865 | break; | |
7866 | } | |
7867 | else | |
7868 | { | |
7869 | /* p->balance == +1; growth of left side balances the node. */ | |
7870 | p->balance = 0; | |
7871 | break; | |
7872 | } | |
2bede729 | 7873 | } |
3e4093b6 RS |
7874 | else /* r == p->right */ |
7875 | { | |
7876 | if (p->balance == 0) | |
7877 | /* Growth propagation from right side. */ | |
7878 | p->balance++; | |
7879 | else if (p->balance > 0) | |
7880 | { | |
7881 | if (r->balance > 0) | |
7882 | { | |
7883 | /* R rotation. */ | |
7884 | p->right = r->left; | |
7885 | if (p->right) | |
7886 | p->right->parent = p; | |
7887 | r->left = p; | |
7888 | ||
7889 | p->balance = 0; | |
7890 | r->balance = 0; | |
7891 | ||
7892 | s = p->parent; | |
7893 | p->parent = r; | |
7894 | r->parent = s; | |
7895 | if (s) | |
7896 | { | |
7897 | if (s->left == p) | |
7898 | s->left = r; | |
7899 | else | |
7900 | s->right = r; | |
7901 | } | |
7902 | else | |
7903 | constructor_pending_elts = r; | |
7904 | } | |
7905 | else /* r->balance == -1 */ | |
7906 | { | |
7907 | /* RL rotation */ | |
7908 | struct init_node *t = r->left; | |
7909 | ||
7910 | r->left = t->right; | |
7911 | if (r->left) | |
7912 | r->left->parent = r; | |
7913 | t->right = r; | |
7914 | ||
7915 | p->right = t->left; | |
7916 | if (p->right) | |
7917 | p->right->parent = p; | |
7918 | t->left = p; | |
7919 | ||
7920 | r->balance = (t->balance < 0); | |
7921 | p->balance = -(t->balance > 0); | |
7922 | t->balance = 0; | |
7923 | ||
7924 | s = p->parent; | |
7925 | p->parent = t; | |
7926 | r->parent = t; | |
7927 | t->parent = s; | |
7928 | if (s) | |
7929 | { | |
7930 | if (s->left == p) | |
7931 | s->left = t; | |
7932 | else | |
7933 | s->right = t; | |
7934 | } | |
7935 | else | |
7936 | constructor_pending_elts = t; | |
7937 | } | |
7938 | break; | |
7939 | } | |
7940 | else | |
7941 | { | |
7942 | /* p->balance == -1; growth of right side balances the node. */ | |
7943 | p->balance = 0; | |
7944 | break; | |
7945 | } | |
7946 | } | |
7947 | ||
7948 | r = p; | |
7949 | p = p->parent; | |
7950 | } | |
7951 | } | |
7952 | ||
7953 | /* Build AVL tree from a sorted chain. */ | |
7954 | ||
7955 | static void | |
a1e3b3d9 | 7956 | set_nonincremental_init (struct obstack * braced_init_obstack) |
3e4093b6 | 7957 | { |
4038c495 GB |
7958 | unsigned HOST_WIDE_INT ix; |
7959 | tree index, value; | |
3e4093b6 RS |
7960 | |
7961 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7962 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7963 | return; | |
7964 | ||
4038c495 | 7965 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) |
a1e3b3d9 | 7966 | { |
fdce1719 | 7967 | add_pending_init (index, value, NULL_TREE, true, |
a1e3b3d9 LB |
7968 | braced_init_obstack); |
7969 | } | |
9771b263 | 7970 | constructor_elements = NULL; |
3e4093b6 RS |
7971 | if (TREE_CODE (constructor_type) == RECORD_TYPE) |
7972 | { | |
7973 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
7974 | /* Skip any nameless bit fields at the beginning. */ | |
7975 | while (constructor_unfilled_fields != 0 | |
7976 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7977 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7978 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
fed3cef0 | 7979 | |
de520661 | 7980 | } |
3e4093b6 | 7981 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 7982 | { |
3e4093b6 RS |
7983 | if (TYPE_DOMAIN (constructor_type)) |
7984 | constructor_unfilled_index | |
7985 | = convert (bitsizetype, | |
7986 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7987 | else | |
7988 | constructor_unfilled_index = bitsize_zero_node; | |
de520661 | 7989 | } |
3e4093b6 | 7990 | constructor_incremental = 0; |
de520661 | 7991 | } |
400fbf9f | 7992 | |
3e4093b6 | 7993 | /* Build AVL tree from a string constant. */ |
de520661 | 7994 | |
3e4093b6 | 7995 | static void |
a1e3b3d9 LB |
7996 | set_nonincremental_init_from_string (tree str, |
7997 | struct obstack * braced_init_obstack) | |
de520661 | 7998 | { |
3e4093b6 RS |
7999 | tree value, purpose, type; |
8000 | HOST_WIDE_INT val[2]; | |
8001 | const char *p, *end; | |
8002 | int byte, wchar_bytes, charwidth, bitpos; | |
de520661 | 8003 | |
366de0ce | 8004 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); |
940ff66d | 8005 | |
c466b2cd | 8006 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; |
3e4093b6 RS |
8007 | charwidth = TYPE_PRECISION (char_type_node); |
8008 | type = TREE_TYPE (constructor_type); | |
8009 | p = TREE_STRING_POINTER (str); | |
8010 | end = p + TREE_STRING_LENGTH (str); | |
91fa3c30 | 8011 | |
3e4093b6 | 8012 | for (purpose = bitsize_zero_node; |
8824edff JJ |
8013 | p < end |
8014 | && !(constructor_max_index | |
8015 | && tree_int_cst_lt (constructor_max_index, purpose)); | |
3e4093b6 | 8016 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) |
584ef5fe | 8017 | { |
3e4093b6 | 8018 | if (wchar_bytes == 1) |
ffc5c6a9 | 8019 | { |
3e4093b6 RS |
8020 | val[1] = (unsigned char) *p++; |
8021 | val[0] = 0; | |
ffc5c6a9 RH |
8022 | } |
8023 | else | |
3e4093b6 RS |
8024 | { |
8025 | val[0] = 0; | |
8026 | val[1] = 0; | |
8027 | for (byte = 0; byte < wchar_bytes; byte++) | |
8028 | { | |
8029 | if (BYTES_BIG_ENDIAN) | |
8030 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
8031 | else | |
8032 | bitpos = byte * charwidth; | |
8033 | val[bitpos < HOST_BITS_PER_WIDE_INT] | |
8034 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) | |
8035 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
8036 | } | |
8037 | } | |
584ef5fe | 8038 | |
8df83eae | 8039 | if (!TYPE_UNSIGNED (type)) |
3e4093b6 RS |
8040 | { |
8041 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
8042 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
8043 | { | |
8044 | if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) | |
8045 | { | |
8046 | val[1] |= ((HOST_WIDE_INT) -1) << bitpos; | |
8047 | val[0] = -1; | |
8048 | } | |
8049 | } | |
8050 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
8051 | { | |
8052 | if (val[1] < 0) | |
c22cacf3 | 8053 | val[0] = -1; |
3e4093b6 RS |
8054 | } |
8055 | else if (val[0] & (((HOST_WIDE_INT) 1) | |
8056 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) | |
8057 | val[0] |= ((HOST_WIDE_INT) -1) | |
8058 | << (bitpos - HOST_BITS_PER_WIDE_INT); | |
8059 | } | |
ffc5c6a9 | 8060 | |
7d60be94 | 8061 | value = build_int_cst_wide (type, val[1], val[0]); |
fdce1719 | 8062 | add_pending_init (purpose, value, NULL_TREE, true, |
a1e3b3d9 | 8063 | braced_init_obstack); |
9dfcc8db BH |
8064 | } |
8065 | ||
3e4093b6 RS |
8066 | constructor_incremental = 0; |
8067 | } | |
de520661 | 8068 | |
3e4093b6 RS |
8069 | /* Return value of FIELD in pending initializer or zero if the field was |
8070 | not initialized yet. */ | |
8071 | ||
8072 | static tree | |
a1e3b3d9 | 8073 | find_init_member (tree field, struct obstack * braced_init_obstack) |
3e4093b6 RS |
8074 | { |
8075 | struct init_node *p; | |
8076 | ||
8077 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
19d76e60 | 8078 | { |
3e4093b6 RS |
8079 | if (constructor_incremental |
8080 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 8081 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8082 | |
8083 | p = constructor_pending_elts; | |
8084 | while (p) | |
19d76e60 | 8085 | { |
3e4093b6 RS |
8086 | if (tree_int_cst_lt (field, p->purpose)) |
8087 | p = p->left; | |
8088 | else if (tree_int_cst_lt (p->purpose, field)) | |
8089 | p = p->right; | |
8090 | else | |
8091 | return p->value; | |
19d76e60 | 8092 | } |
19d76e60 | 8093 | } |
3e4093b6 | 8094 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
de520661 | 8095 | { |
3e4093b6 | 8096 | tree bitpos = bit_position (field); |
de520661 | 8097 | |
3e4093b6 RS |
8098 | if (constructor_incremental |
8099 | && (!constructor_unfilled_fields | |
8100 | || tree_int_cst_lt (bitpos, | |
8101 | bit_position (constructor_unfilled_fields)))) | |
a1e3b3d9 | 8102 | set_nonincremental_init (braced_init_obstack); |
de520661 | 8103 | |
3e4093b6 RS |
8104 | p = constructor_pending_elts; |
8105 | while (p) | |
8106 | { | |
8107 | if (field == p->purpose) | |
8108 | return p->value; | |
8109 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
8110 | p = p->left; | |
8111 | else | |
8112 | p = p->right; | |
8113 | } | |
8114 | } | |
8115 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 8116 | { |
9771b263 DN |
8117 | if (!vec_safe_is_empty (constructor_elements) |
8118 | && (constructor_elements->last ().index == field)) | |
8119 | return constructor_elements->last ().value; | |
de520661 | 8120 | } |
3e4093b6 | 8121 | return 0; |
de520661 RS |
8122 | } |
8123 | ||
3e4093b6 RS |
8124 | /* "Output" the next constructor element. |
8125 | At top level, really output it to assembler code now. | |
8126 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
bbbbb16a | 8127 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. |
3e4093b6 RS |
8128 | TYPE is the data type that the containing data type wants here. |
8129 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
916c5919 JM |
8130 | If VALUE is a string constant, STRICT_STRING is true if it is |
8131 | unparenthesized or we should not warn here for it being parenthesized. | |
8132 | For other types of VALUE, STRICT_STRING is not used. | |
8b6a5902 | 8133 | |
3e4093b6 RS |
8134 | PENDING if non-nil means output pending elements that belong |
8135 | right after this element. (PENDING is normally 1; | |
b295aee2 JJ |
8136 | it is 0 while outputting pending elements, to avoid recursion.) |
8137 | ||
8138 | IMPLICIT is true if value comes from pop_init_level (1), | |
8139 | the new initializer has been merged with the existing one | |
8140 | and thus no warnings should be emitted about overriding an | |
8141 | existing initializer. */ | |
8b6a5902 | 8142 | |
3e4093b6 | 8143 | static void |
bbbbb16a | 8144 | output_init_element (tree value, tree origtype, bool strict_string, tree type, |
a1e3b3d9 LB |
8145 | tree field, int pending, bool implicit, |
8146 | struct obstack * braced_init_obstack) | |
3e4093b6 | 8147 | { |
8ce94e44 | 8148 | tree semantic_type = NULL_TREE; |
928c19bb JM |
8149 | bool maybe_const = true; |
8150 | bool npc; | |
4038c495 | 8151 | |
0a880880 | 8152 | if (type == error_mark_node || value == error_mark_node) |
8b6a5902 | 8153 | { |
3e4093b6 RS |
8154 | constructor_erroneous = 1; |
8155 | return; | |
8b6a5902 | 8156 | } |
46bdb9cf JM |
8157 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
8158 | && (TREE_CODE (value) == STRING_CST | |
8159 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
8160 | && !(TREE_CODE (value) == STRING_CST | |
8161 | && TREE_CODE (type) == ARRAY_TYPE | |
8162 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
8163 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
8164 | TYPE_MAIN_VARIANT (type))) | |
c2255bc4 | 8165 | value = array_to_pointer_conversion (input_location, value); |
8b6a5902 | 8166 | |
3e4093b6 RS |
8167 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
8168 | && require_constant_value && !flag_isoc99 && pending) | |
8b6a5902 | 8169 | { |
3e4093b6 RS |
8170 | /* As an extension, allow initializing objects with static storage |
8171 | duration with compound literals (which are then treated just as | |
8172 | the brace enclosed list they contain). */ | |
8173 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); | |
8174 | value = DECL_INITIAL (decl); | |
8b6a5902 JJ |
8175 | } |
8176 | ||
928c19bb | 8177 | npc = null_pointer_constant_p (value); |
8ce94e44 JM |
8178 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) |
8179 | { | |
8180 | semantic_type = TREE_TYPE (value); | |
8181 | value = TREE_OPERAND (value, 0); | |
8182 | } | |
928c19bb JM |
8183 | value = c_fully_fold (value, require_constant_value, &maybe_const); |
8184 | ||
3e4093b6 RS |
8185 | if (value == error_mark_node) |
8186 | constructor_erroneous = 1; | |
8187 | else if (!TREE_CONSTANT (value)) | |
8188 | constructor_constant = 0; | |
116df786 | 8189 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) |
3e4093b6 RS |
8190 | || ((TREE_CODE (constructor_type) == RECORD_TYPE |
8191 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8192 | && DECL_C_BIT_FIELD (field) | |
8193 | && TREE_CODE (value) != INTEGER_CST)) | |
8194 | constructor_simple = 0; | |
928c19bb JM |
8195 | if (!maybe_const) |
8196 | constructor_nonconst = 1; | |
3e4093b6 | 8197 | |
116df786 | 8198 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) |
8b6a5902 | 8199 | { |
116df786 RH |
8200 | if (require_constant_value) |
8201 | { | |
8202 | error_init ("initializer element is not constant"); | |
8203 | value = error_mark_node; | |
8204 | } | |
8205 | else if (require_constant_elements) | |
509c9d60 MLI |
8206 | pedwarn (input_location, 0, |
8207 | "initializer element is not computable at load time"); | |
8b6a5902 | 8208 | } |
928c19bb JM |
8209 | else if (!maybe_const |
8210 | && (require_constant_value || require_constant_elements)) | |
8211 | pedwarn_init (input_location, 0, | |
8212 | "initializer element is not a constant expression"); | |
3e4093b6 | 8213 | |
81f40b79 ILT |
8214 | /* Issue -Wc++-compat warnings about initializing a bitfield with |
8215 | enum type. */ | |
8216 | if (warn_cxx_compat | |
8217 | && field != NULL_TREE | |
8218 | && TREE_CODE (field) == FIELD_DECL | |
8219 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
8220 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
8221 | != TYPE_MAIN_VARIANT (type)) | |
8222 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
8223 | { | |
8224 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
8225 | if (checktype != error_mark_node | |
8226 | && (TYPE_MAIN_VARIANT (checktype) | |
8227 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
8228 | warning_init (OPT_Wc___compat, | |
8229 | "enum conversion in initialization is invalid in C++"); | |
8230 | } | |
8231 | ||
3e4093b6 RS |
8232 | /* If this field is empty (and not at the end of structure), |
8233 | don't do anything other than checking the initializer. */ | |
8234 | if (field | |
8235 | && (TREE_TYPE (field) == error_mark_node | |
8236 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
8237 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
8238 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
910ad8de | 8239 | || DECL_CHAIN (field))))) |
3e4093b6 RS |
8240 | return; |
8241 | ||
8ce94e44 JM |
8242 | if (semantic_type) |
8243 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
c2255bc4 AH |
8244 | value = digest_init (input_location, type, value, origtype, npc, |
8245 | strict_string, require_constant_value); | |
3e4093b6 | 8246 | if (value == error_mark_node) |
8b6a5902 | 8247 | { |
3e4093b6 RS |
8248 | constructor_erroneous = 1; |
8249 | return; | |
8b6a5902 | 8250 | } |
928c19bb JM |
8251 | if (require_constant_value || require_constant_elements) |
8252 | constant_expression_warning (value); | |
8b6a5902 | 8253 | |
3e4093b6 RS |
8254 | /* If this element doesn't come next in sequence, |
8255 | put it on constructor_pending_elts. */ | |
8256 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
8257 | && (!constructor_incremental | |
8258 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
8b6a5902 | 8259 | { |
3e4093b6 RS |
8260 | if (constructor_incremental |
8261 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 8262 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 8263 | |
a1e3b3d9 LB |
8264 | add_pending_init (field, value, origtype, implicit, |
8265 | braced_init_obstack); | |
3e4093b6 | 8266 | return; |
8b6a5902 | 8267 | } |
3e4093b6 RS |
8268 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
8269 | && (!constructor_incremental | |
8270 | || field != constructor_unfilled_fields)) | |
8b6a5902 | 8271 | { |
3e4093b6 RS |
8272 | /* We do this for records but not for unions. In a union, |
8273 | no matter which field is specified, it can be initialized | |
8274 | right away since it starts at the beginning of the union. */ | |
8275 | if (constructor_incremental) | |
8276 | { | |
8277 | if (!constructor_unfilled_fields) | |
a1e3b3d9 | 8278 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8279 | else |
8280 | { | |
8281 | tree bitpos, unfillpos; | |
8282 | ||
8283 | bitpos = bit_position (field); | |
8284 | unfillpos = bit_position (constructor_unfilled_fields); | |
8285 | ||
8286 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
a1e3b3d9 | 8287 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
8288 | } |
8289 | } | |
8290 | ||
a1e3b3d9 LB |
8291 | add_pending_init (field, value, origtype, implicit, |
8292 | braced_init_obstack); | |
3e4093b6 | 8293 | return; |
8b6a5902 | 8294 | } |
3e4093b6 | 8295 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
9771b263 | 8296 | && !vec_safe_is_empty (constructor_elements)) |
3e4093b6 | 8297 | { |
b295aee2 JJ |
8298 | if (!implicit) |
8299 | { | |
9771b263 | 8300 | if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) |
b295aee2 JJ |
8301 | warning_init (0, |
8302 | "initialized field with side-effects overwritten"); | |
8303 | else if (warn_override_init) | |
8304 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
8305 | } | |
8b6a5902 | 8306 | |
3e4093b6 | 8307 | /* We can have just one union field set. */ |
9771b263 | 8308 | constructor_elements = NULL; |
3e4093b6 | 8309 | } |
8b6a5902 | 8310 | |
3e4093b6 RS |
8311 | /* Otherwise, output this element either to |
8312 | constructor_elements or to the assembler file. */ | |
8b6a5902 | 8313 | |
f32682ca | 8314 | constructor_elt celt = {field, value}; |
9771b263 | 8315 | vec_safe_push (constructor_elements, celt); |
8b6a5902 | 8316 | |
3e4093b6 RS |
8317 | /* Advance the variable that indicates sequential elements output. */ |
8318 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8319 | constructor_unfilled_index | |
db3927fb AH |
8320 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, |
8321 | bitsize_one_node); | |
3e4093b6 RS |
8322 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
8323 | { | |
8324 | constructor_unfilled_fields | |
910ad8de | 8325 | = DECL_CHAIN (constructor_unfilled_fields); |
8b6a5902 | 8326 | |
3e4093b6 RS |
8327 | /* Skip any nameless bit fields. */ |
8328 | while (constructor_unfilled_fields != 0 | |
8329 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8330 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8331 | constructor_unfilled_fields = | |
910ad8de | 8332 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 RS |
8333 | } |
8334 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
8335 | constructor_unfilled_fields = 0; | |
de520661 | 8336 | |
3e4093b6 RS |
8337 | /* Now output any pending elements which have become next. */ |
8338 | if (pending) | |
a1e3b3d9 | 8339 | output_pending_init_elements (0, braced_init_obstack); |
3e4093b6 | 8340 | } |
8b6a5902 | 8341 | |
3e4093b6 RS |
8342 | /* Output any pending elements which have become next. |
8343 | As we output elements, constructor_unfilled_{fields,index} | |
8344 | advances, which may cause other elements to become next; | |
8345 | if so, they too are output. | |
8b6a5902 | 8346 | |
3e4093b6 RS |
8347 | If ALL is 0, we return when there are |
8348 | no more pending elements to output now. | |
665f2503 | 8349 | |
3e4093b6 RS |
8350 | If ALL is 1, we output space as necessary so that |
8351 | we can output all the pending elements. */ | |
3e4093b6 | 8352 | static void |
a1e3b3d9 | 8353 | output_pending_init_elements (int all, struct obstack * braced_init_obstack) |
3e4093b6 RS |
8354 | { |
8355 | struct init_node *elt = constructor_pending_elts; | |
8356 | tree next; | |
de520661 | 8357 | |
3e4093b6 RS |
8358 | retry: |
8359 | ||
ba228239 | 8360 | /* Look through the whole pending tree. |
3e4093b6 RS |
8361 | If we find an element that should be output now, |
8362 | output it. Otherwise, set NEXT to the element | |
8363 | that comes first among those still pending. */ | |
8364 | ||
8365 | next = 0; | |
8366 | while (elt) | |
8367 | { | |
8368 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8b6a5902 | 8369 | { |
3e4093b6 RS |
8370 | if (tree_int_cst_equal (elt->purpose, |
8371 | constructor_unfilled_index)) | |
bbbbb16a | 8372 | output_init_element (elt->value, elt->origtype, true, |
3e4093b6 | 8373 | TREE_TYPE (constructor_type), |
a1e3b3d9 LB |
8374 | constructor_unfilled_index, 0, false, |
8375 | braced_init_obstack); | |
3e4093b6 RS |
8376 | else if (tree_int_cst_lt (constructor_unfilled_index, |
8377 | elt->purpose)) | |
8b6a5902 | 8378 | { |
3e4093b6 RS |
8379 | /* Advance to the next smaller node. */ |
8380 | if (elt->left) | |
8381 | elt = elt->left; | |
8382 | else | |
8383 | { | |
8384 | /* We have reached the smallest node bigger than the | |
8385 | current unfilled index. Fill the space first. */ | |
8386 | next = elt->purpose; | |
8387 | break; | |
8388 | } | |
8b6a5902 | 8389 | } |
ce662d4c JJ |
8390 | else |
8391 | { | |
3e4093b6 RS |
8392 | /* Advance to the next bigger node. */ |
8393 | if (elt->right) | |
8394 | elt = elt->right; | |
8395 | else | |
ce662d4c | 8396 | { |
3e4093b6 RS |
8397 | /* We have reached the biggest node in a subtree. Find |
8398 | the parent of it, which is the next bigger node. */ | |
8399 | while (elt->parent && elt->parent->right == elt) | |
8400 | elt = elt->parent; | |
8401 | elt = elt->parent; | |
8402 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
8403 | elt->purpose)) | |
8404 | { | |
8405 | next = elt->purpose; | |
8406 | break; | |
8407 | } | |
ce662d4c JJ |
8408 | } |
8409 | } | |
8b6a5902 | 8410 | } |
3e4093b6 RS |
8411 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
8412 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8413 | { | |
8414 | tree ctor_unfilled_bitpos, elt_bitpos; | |
ce662d4c | 8415 | |
3e4093b6 RS |
8416 | /* If the current record is complete we are done. */ |
8417 | if (constructor_unfilled_fields == 0) | |
8418 | break; | |
de520661 | 8419 | |
3e4093b6 RS |
8420 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
8421 | elt_bitpos = bit_position (elt->purpose); | |
8422 | /* We can't compare fields here because there might be empty | |
8423 | fields in between. */ | |
8424 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
8425 | { | |
8426 | constructor_unfilled_fields = elt->purpose; | |
bbbbb16a ILT |
8427 | output_init_element (elt->value, elt->origtype, true, |
8428 | TREE_TYPE (elt->purpose), | |
a1e3b3d9 LB |
8429 | elt->purpose, 0, false, |
8430 | braced_init_obstack); | |
3e4093b6 RS |
8431 | } |
8432 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
8433 | { | |
8434 | /* Advance to the next smaller node. */ | |
8435 | if (elt->left) | |
8436 | elt = elt->left; | |
8437 | else | |
8438 | { | |
8439 | /* We have reached the smallest node bigger than the | |
8440 | current unfilled field. Fill the space first. */ | |
8441 | next = elt->purpose; | |
8442 | break; | |
8443 | } | |
8444 | } | |
8445 | else | |
8446 | { | |
8447 | /* Advance to the next bigger node. */ | |
8448 | if (elt->right) | |
8449 | elt = elt->right; | |
8450 | else | |
8451 | { | |
8452 | /* We have reached the biggest node in a subtree. Find | |
8453 | the parent of it, which is the next bigger node. */ | |
8454 | while (elt->parent && elt->parent->right == elt) | |
8455 | elt = elt->parent; | |
8456 | elt = elt->parent; | |
8457 | if (elt | |
8458 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
8459 | bit_position (elt->purpose)))) | |
8460 | { | |
8461 | next = elt->purpose; | |
8462 | break; | |
8463 | } | |
8464 | } | |
8465 | } | |
8466 | } | |
8467 | } | |
de520661 | 8468 | |
3e4093b6 RS |
8469 | /* Ordinarily return, but not if we want to output all |
8470 | and there are elements left. */ | |
3f75a254 | 8471 | if (!(all && next != 0)) |
e5cfb88f RK |
8472 | return; |
8473 | ||
3e4093b6 RS |
8474 | /* If it's not incremental, just skip over the gap, so that after |
8475 | jumping to retry we will output the next successive element. */ | |
8476 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
8477 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8478 | constructor_unfilled_fields = next; | |
8479 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8480 | constructor_unfilled_index = next; | |
de520661 | 8481 | |
3e4093b6 RS |
8482 | /* ELT now points to the node in the pending tree with the next |
8483 | initializer to output. */ | |
8484 | goto retry; | |
de520661 RS |
8485 | } |
8486 | \f | |
3e4093b6 RS |
8487 | /* Add one non-braced element to the current constructor level. |
8488 | This adjusts the current position within the constructor's type. | |
8489 | This may also start or terminate implicit levels | |
8490 | to handle a partly-braced initializer. | |
e5e809f4 | 8491 | |
3e4093b6 | 8492 | Once this has found the correct level for the new element, |
b295aee2 JJ |
8493 | it calls output_init_element. |
8494 | ||
8495 | IMPLICIT is true if value comes from pop_init_level (1), | |
8496 | the new initializer has been merged with the existing one | |
8497 | and thus no warnings should be emitted about overriding an | |
8498 | existing initializer. */ | |
3e4093b6 RS |
8499 | |
8500 | void | |
a1e3b3d9 LB |
8501 | process_init_element (struct c_expr value, bool implicit, |
8502 | struct obstack * braced_init_obstack) | |
e5e809f4 | 8503 | { |
916c5919 JM |
8504 | tree orig_value = value.value; |
8505 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
8506 | bool strict_string = value.original_code == STRING_CST; | |
340baef7 | 8507 | bool was_designated = designator_depth != 0; |
e5e809f4 | 8508 | |
3e4093b6 | 8509 | designator_depth = 0; |
b06df647 | 8510 | designator_erroneous = 0; |
e5e809f4 | 8511 | |
3e4093b6 RS |
8512 | /* Handle superfluous braces around string cst as in |
8513 | char x[] = {"foo"}; */ | |
8514 | if (string_flag | |
8515 | && constructor_type | |
340baef7 | 8516 | && !was_designated |
3e4093b6 | 8517 | && TREE_CODE (constructor_type) == ARRAY_TYPE |
197463ae | 8518 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
3e4093b6 | 8519 | && integer_zerop (constructor_unfilled_index)) |
e5e809f4 | 8520 | { |
916c5919 | 8521 | if (constructor_stack->replacement_value.value) |
c22cacf3 | 8522 | error_init ("excess elements in char array initializer"); |
3e4093b6 RS |
8523 | constructor_stack->replacement_value = value; |
8524 | return; | |
e5e809f4 | 8525 | } |
8b6a5902 | 8526 | |
916c5919 | 8527 | if (constructor_stack->replacement_value.value != 0) |
3e4093b6 RS |
8528 | { |
8529 | error_init ("excess elements in struct initializer"); | |
8530 | return; | |
e5e809f4 JL |
8531 | } |
8532 | ||
3e4093b6 RS |
8533 | /* Ignore elements of a brace group if it is entirely superfluous |
8534 | and has already been diagnosed. */ | |
8535 | if (constructor_type == 0) | |
8536 | return; | |
e5e809f4 | 8537 | |
3e4093b6 RS |
8538 | /* If we've exhausted any levels that didn't have braces, |
8539 | pop them now. */ | |
8540 | while (constructor_stack->implicit) | |
8541 | { | |
8542 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
8543 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8544 | && constructor_fields == 0) | |
a1e3b3d9 LB |
8545 | process_init_element (pop_init_level (1, braced_init_obstack), |
8546 | true, braced_init_obstack); | |
53650abe AP |
8547 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE |
8548 | || TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8824edff JJ |
8549 | && constructor_max_index |
8550 | && tree_int_cst_lt (constructor_max_index, | |
8551 | constructor_index)) | |
a1e3b3d9 LB |
8552 | process_init_element (pop_init_level (1, braced_init_obstack), |
8553 | true, braced_init_obstack); | |
3e4093b6 RS |
8554 | else |
8555 | break; | |
8556 | } | |
e5e809f4 | 8557 | |
3e4093b6 RS |
8558 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
8559 | if (constructor_range_stack) | |
e5e809f4 | 8560 | { |
3e4093b6 RS |
8561 | /* If value is a compound literal and we'll be just using its |
8562 | content, don't put it into a SAVE_EXPR. */ | |
916c5919 | 8563 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
3e4093b6 RS |
8564 | || !require_constant_value |
8565 | || flag_isoc99) | |
8ce94e44 JM |
8566 | { |
8567 | tree semantic_type = NULL_TREE; | |
8568 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
8569 | { | |
8570 | semantic_type = TREE_TYPE (value.value); | |
8571 | value.value = TREE_OPERAND (value.value, 0); | |
8572 | } | |
8573 | value.value = c_save_expr (value.value); | |
8574 | if (semantic_type) | |
8575 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
8576 | value.value); | |
8577 | } | |
3e4093b6 | 8578 | } |
e5e809f4 | 8579 | |
3e4093b6 RS |
8580 | while (1) |
8581 | { | |
8582 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
e5e809f4 | 8583 | { |
3e4093b6 RS |
8584 | tree fieldtype; |
8585 | enum tree_code fieldcode; | |
e5e809f4 | 8586 | |
3e4093b6 RS |
8587 | if (constructor_fields == 0) |
8588 | { | |
509c9d60 MLI |
8589 | pedwarn_init (input_location, 0, |
8590 | "excess elements in struct initializer"); | |
3e4093b6 RS |
8591 | break; |
8592 | } | |
e5e809f4 | 8593 | |
3e4093b6 RS |
8594 | fieldtype = TREE_TYPE (constructor_fields); |
8595 | if (fieldtype != error_mark_node) | |
8596 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8597 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8598 | |
3e4093b6 RS |
8599 | /* Error for non-static initialization of a flexible array member. */ |
8600 | if (fieldcode == ARRAY_TYPE | |
8601 | && !require_constant_value | |
8602 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
f7587ed0 | 8603 | && DECL_CHAIN (constructor_fields) == NULL_TREE) |
3e4093b6 RS |
8604 | { |
8605 | error_init ("non-static initialization of a flexible array member"); | |
8606 | break; | |
8607 | } | |
e5e809f4 | 8608 | |
3e4093b6 | 8609 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8610 | if (value.value != 0 |
3e4093b6 | 8611 | && fieldcode == ARRAY_TYPE |
197463ae | 8612 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8613 | && string_flag) |
916c5919 | 8614 | value.value = orig_value; |
3e4093b6 RS |
8615 | /* Otherwise, if we have come to a subaggregate, |
8616 | and we don't have an element of its type, push into it. */ | |
0953878d | 8617 | else if (value.value != 0 |
916c5919 JM |
8618 | && value.value != error_mark_node |
8619 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 8620 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 8621 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 8622 | { |
a1e3b3d9 | 8623 | push_init_level (1, braced_init_obstack); |
3e4093b6 RS |
8624 | continue; |
8625 | } | |
e5e809f4 | 8626 | |
916c5919 | 8627 | if (value.value) |
3e4093b6 RS |
8628 | { |
8629 | push_member_name (constructor_fields); | |
bbbbb16a ILT |
8630 | output_init_element (value.value, value.original_type, |
8631 | strict_string, fieldtype, | |
a1e3b3d9 LB |
8632 | constructor_fields, 1, implicit, |
8633 | braced_init_obstack); | |
3e4093b6 | 8634 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
8635 | } |
8636 | else | |
3e4093b6 RS |
8637 | /* Do the bookkeeping for an element that was |
8638 | directly output as a constructor. */ | |
e5e809f4 | 8639 | { |
3e4093b6 RS |
8640 | /* For a record, keep track of end position of last field. */ |
8641 | if (DECL_SIZE (constructor_fields)) | |
c22cacf3 | 8642 | constructor_bit_index |
db3927fb AH |
8643 | = size_binop_loc (input_location, PLUS_EXPR, |
8644 | bit_position (constructor_fields), | |
8645 | DECL_SIZE (constructor_fields)); | |
3e4093b6 RS |
8646 | |
8647 | /* If the current field was the first one not yet written out, | |
8648 | it isn't now, so update. */ | |
8649 | if (constructor_unfilled_fields == constructor_fields) | |
8650 | { | |
910ad8de | 8651 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8652 | /* Skip any nameless bit fields. */ |
8653 | while (constructor_unfilled_fields != 0 | |
8654 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8655 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8656 | constructor_unfilled_fields = | |
910ad8de | 8657 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 | 8658 | } |
e5e809f4 | 8659 | } |
3e4093b6 | 8660 | |
910ad8de | 8661 | constructor_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8662 | /* Skip any nameless bit fields at the beginning. */ |
8663 | while (constructor_fields != 0 | |
8664 | && DECL_C_BIT_FIELD (constructor_fields) | |
8665 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 8666 | constructor_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 8667 | } |
3e4093b6 | 8668 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
e5e809f4 | 8669 | { |
3e4093b6 RS |
8670 | tree fieldtype; |
8671 | enum tree_code fieldcode; | |
e5e809f4 | 8672 | |
3e4093b6 RS |
8673 | if (constructor_fields == 0) |
8674 | { | |
509c9d60 MLI |
8675 | pedwarn_init (input_location, 0, |
8676 | "excess elements in union initializer"); | |
3e4093b6 RS |
8677 | break; |
8678 | } | |
e5e809f4 | 8679 | |
3e4093b6 RS |
8680 | fieldtype = TREE_TYPE (constructor_fields); |
8681 | if (fieldtype != error_mark_node) | |
8682 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8683 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8684 | |
3e4093b6 RS |
8685 | /* Warn that traditional C rejects initialization of unions. |
8686 | We skip the warning if the value is zero. This is done | |
8687 | under the assumption that the zero initializer in user | |
8688 | code appears conditioned on e.g. __STDC__ to avoid | |
8689 | "missing initializer" warnings and relies on default | |
8690 | initialization to zero in the traditional C case. | |
8691 | We also skip the warning if the initializer is designated, | |
8692 | again on the assumption that this must be conditional on | |
8693 | __STDC__ anyway (and we've already complained about the | |
8694 | member-designator already). */ | |
8400e75e | 8695 | if (!in_system_header_at (input_location) && !constructor_designated |
916c5919 JM |
8696 | && !(value.value && (integer_zerop (value.value) |
8697 | || real_zerop (value.value)))) | |
3176a0c2 DD |
8698 | warning (OPT_Wtraditional, "traditional C rejects initialization " |
8699 | "of unions"); | |
e5e809f4 | 8700 | |
3e4093b6 | 8701 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8702 | if (value.value != 0 |
3e4093b6 | 8703 | && fieldcode == ARRAY_TYPE |
197463ae | 8704 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8705 | && string_flag) |
916c5919 | 8706 | value.value = orig_value; |
3e4093b6 RS |
8707 | /* Otherwise, if we have come to a subaggregate, |
8708 | and we don't have an element of its type, push into it. */ | |
0953878d | 8709 | else if (value.value != 0 |
916c5919 JM |
8710 | && value.value != error_mark_node |
8711 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 8712 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 8713 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 8714 | { |
a1e3b3d9 | 8715 | push_init_level (1, braced_init_obstack); |
3e4093b6 RS |
8716 | continue; |
8717 | } | |
e5e809f4 | 8718 | |
916c5919 | 8719 | if (value.value) |
3e4093b6 RS |
8720 | { |
8721 | push_member_name (constructor_fields); | |
bbbbb16a ILT |
8722 | output_init_element (value.value, value.original_type, |
8723 | strict_string, fieldtype, | |
a1e3b3d9 LB |
8724 | constructor_fields, 1, implicit, |
8725 | braced_init_obstack); | |
3e4093b6 | 8726 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
8727 | } |
8728 | else | |
3e4093b6 RS |
8729 | /* Do the bookkeeping for an element that was |
8730 | directly output as a constructor. */ | |
e5e809f4 | 8731 | { |
3e4093b6 | 8732 | constructor_bit_index = DECL_SIZE (constructor_fields); |
f7587ed0 | 8733 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 8734 | } |
e5e809f4 | 8735 | |
3e4093b6 RS |
8736 | constructor_fields = 0; |
8737 | } | |
8738 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8739 | { | |
8740 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8741 | enum tree_code eltcode = TREE_CODE (elttype); | |
e5e809f4 | 8742 | |
3e4093b6 | 8743 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8744 | if (value.value != 0 |
3e4093b6 | 8745 | && eltcode == ARRAY_TYPE |
197463ae | 8746 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
3e4093b6 | 8747 | && string_flag) |
916c5919 | 8748 | value.value = orig_value; |
3e4093b6 RS |
8749 | /* Otherwise, if we have come to a subaggregate, |
8750 | and we don't have an element of its type, push into it. */ | |
0953878d | 8751 | else if (value.value != 0 |
916c5919 JM |
8752 | && value.value != error_mark_node |
8753 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
3e4093b6 | 8754 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
53650abe | 8755 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) |
3e4093b6 | 8756 | { |
a1e3b3d9 | 8757 | push_init_level (1, braced_init_obstack); |
3e4093b6 RS |
8758 | continue; |
8759 | } | |
8b6a5902 | 8760 | |
3e4093b6 RS |
8761 | if (constructor_max_index != 0 |
8762 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
8763 | || integer_all_onesp (constructor_max_index))) | |
8764 | { | |
509c9d60 MLI |
8765 | pedwarn_init (input_location, 0, |
8766 | "excess elements in array initializer"); | |
3e4093b6 RS |
8767 | break; |
8768 | } | |
8b6a5902 | 8769 | |
3e4093b6 | 8770 | /* Now output the actual element. */ |
916c5919 | 8771 | if (value.value) |
3e4093b6 | 8772 | { |
ae7e9ddd | 8773 | push_array_bounds (tree_to_uhwi (constructor_index)); |
bbbbb16a ILT |
8774 | output_init_element (value.value, value.original_type, |
8775 | strict_string, elttype, | |
a1e3b3d9 LB |
8776 | constructor_index, 1, implicit, |
8777 | braced_init_obstack); | |
3e4093b6 RS |
8778 | RESTORE_SPELLING_DEPTH (constructor_depth); |
8779 | } | |
2f6e4e97 | 8780 | |
3e4093b6 | 8781 | constructor_index |
db3927fb AH |
8782 | = size_binop_loc (input_location, PLUS_EXPR, |
8783 | constructor_index, bitsize_one_node); | |
8b6a5902 | 8784 | |
916c5919 | 8785 | if (!value.value) |
3e4093b6 RS |
8786 | /* If we are doing the bookkeeping for an element that was |
8787 | directly output as a constructor, we must update | |
8788 | constructor_unfilled_index. */ | |
8789 | constructor_unfilled_index = constructor_index; | |
8790 | } | |
8791 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8792 | { | |
8793 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8b6a5902 | 8794 | |
c22cacf3 MS |
8795 | /* Do a basic check of initializer size. Note that vectors |
8796 | always have a fixed size derived from their type. */ | |
3e4093b6 RS |
8797 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) |
8798 | { | |
509c9d60 MLI |
8799 | pedwarn_init (input_location, 0, |
8800 | "excess elements in vector initializer"); | |
3e4093b6 RS |
8801 | break; |
8802 | } | |
8b6a5902 | 8803 | |
3e4093b6 | 8804 | /* Now output the actual element. */ |
916c5919 | 8805 | if (value.value) |
53650abe AP |
8806 | { |
8807 | if (TREE_CODE (value.value) == VECTOR_CST) | |
8808 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
8809 | output_init_element (value.value, value.original_type, | |
8810 | strict_string, elttype, | |
a1e3b3d9 LB |
8811 | constructor_index, 1, implicit, |
8812 | braced_init_obstack); | |
53650abe | 8813 | } |
8b6a5902 | 8814 | |
3e4093b6 | 8815 | constructor_index |
db3927fb AH |
8816 | = size_binop_loc (input_location, |
8817 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 8818 | |
916c5919 | 8819 | if (!value.value) |
3e4093b6 RS |
8820 | /* If we are doing the bookkeeping for an element that was |
8821 | directly output as a constructor, we must update | |
8822 | constructor_unfilled_index. */ | |
8823 | constructor_unfilled_index = constructor_index; | |
8824 | } | |
8b6a5902 | 8825 | |
3e4093b6 RS |
8826 | /* Handle the sole element allowed in a braced initializer |
8827 | for a scalar variable. */ | |
b4519d39 SB |
8828 | else if (constructor_type != error_mark_node |
8829 | && constructor_fields == 0) | |
8b6a5902 | 8830 | { |
509c9d60 MLI |
8831 | pedwarn_init (input_location, 0, |
8832 | "excess elements in scalar initializer"); | |
3e4093b6 | 8833 | break; |
8b6a5902 JJ |
8834 | } |
8835 | else | |
8836 | { | |
916c5919 | 8837 | if (value.value) |
bbbbb16a ILT |
8838 | output_init_element (value.value, value.original_type, |
8839 | strict_string, constructor_type, | |
a1e3b3d9 LB |
8840 | NULL_TREE, 1, implicit, |
8841 | braced_init_obstack); | |
3e4093b6 | 8842 | constructor_fields = 0; |
8b6a5902 JJ |
8843 | } |
8844 | ||
3e4093b6 RS |
8845 | /* Handle range initializers either at this level or anywhere higher |
8846 | in the designator stack. */ | |
8847 | if (constructor_range_stack) | |
8b6a5902 | 8848 | { |
3e4093b6 RS |
8849 | struct constructor_range_stack *p, *range_stack; |
8850 | int finish = 0; | |
8851 | ||
8852 | range_stack = constructor_range_stack; | |
8853 | constructor_range_stack = 0; | |
8854 | while (constructor_stack != range_stack->stack) | |
8b6a5902 | 8855 | { |
366de0ce | 8856 | gcc_assert (constructor_stack->implicit); |
a1e3b3d9 LB |
8857 | process_init_element (pop_init_level (1, |
8858 | braced_init_obstack), | |
8859 | true, braced_init_obstack); | |
8b6a5902 | 8860 | } |
3e4093b6 RS |
8861 | for (p = range_stack; |
8862 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
8863 | p = p->prev) | |
8b6a5902 | 8864 | { |
366de0ce | 8865 | gcc_assert (constructor_stack->implicit); |
a1e3b3d9 LB |
8866 | process_init_element (pop_init_level (1, braced_init_obstack), |
8867 | true, braced_init_obstack); | |
8b6a5902 | 8868 | } |
3e4093b6 | 8869 | |
db3927fb AH |
8870 | p->index = size_binop_loc (input_location, |
8871 | PLUS_EXPR, p->index, bitsize_one_node); | |
3e4093b6 RS |
8872 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) |
8873 | finish = 1; | |
8874 | ||
8875 | while (1) | |
8876 | { | |
8877 | constructor_index = p->index; | |
8878 | constructor_fields = p->fields; | |
8879 | if (finish && p->range_end && p->index == p->range_start) | |
8880 | { | |
8881 | finish = 0; | |
8882 | p->prev = 0; | |
8883 | } | |
8884 | p = p->next; | |
8885 | if (!p) | |
8886 | break; | |
a1e3b3d9 | 8887 | push_init_level (2, braced_init_obstack); |
3e4093b6 RS |
8888 | p->stack = constructor_stack; |
8889 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
8890 | p->index = p->range_start; | |
8891 | } | |
8892 | ||
8893 | if (!finish) | |
8894 | constructor_range_stack = range_stack; | |
8895 | continue; | |
8b6a5902 JJ |
8896 | } |
8897 | ||
3e4093b6 | 8898 | break; |
8b6a5902 JJ |
8899 | } |
8900 | ||
3e4093b6 RS |
8901 | constructor_range_stack = 0; |
8902 | } | |
8903 | \f | |
9f0e2d86 ZW |
8904 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
8905 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
e130a54b | 8906 | an ASM_EXPR node). */ |
3e4093b6 | 8907 | tree |
9f0e2d86 | 8908 | build_asm_stmt (tree cv_qualifier, tree args) |
3e4093b6 | 8909 | { |
6de9cd9a DN |
8910 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
8911 | ASM_VOLATILE_P (args) = 1; | |
9f0e2d86 | 8912 | return add_stmt (args); |
8b6a5902 JJ |
8913 | } |
8914 | ||
9f0e2d86 ZW |
8915 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
8916 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
8917 | SIMPLE indicates whether there was anything at all after the | |
8918 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
e130a54b | 8919 | are subtly different. We use a ASM_EXPR node to represent this. */ |
3e4093b6 | 8920 | tree |
c2255bc4 | 8921 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, |
1c384bf1 | 8922 | tree clobbers, tree labels, bool simple) |
e5e809f4 | 8923 | { |
3e4093b6 | 8924 | tree tail; |
9f0e2d86 | 8925 | tree args; |
6de9cd9a DN |
8926 | int i; |
8927 | const char *constraint; | |
74f0c611 | 8928 | const char **oconstraints; |
6de9cd9a | 8929 | bool allows_mem, allows_reg, is_inout; |
74f0c611 | 8930 | int ninputs, noutputs; |
6de9cd9a DN |
8931 | |
8932 | ninputs = list_length (inputs); | |
8933 | noutputs = list_length (outputs); | |
74f0c611 RH |
8934 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); |
8935 | ||
1c384bf1 | 8936 | string = resolve_asm_operand_names (string, outputs, inputs, labels); |
3e4093b6 | 8937 | |
6de9cd9a DN |
8938 | /* Remove output conversions that change the type but not the mode. */ |
8939 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
e5e809f4 | 8940 | { |
3e4093b6 | 8941 | tree output = TREE_VALUE (tail); |
74f0c611 | 8942 | |
eadd3d0d JJ |
8943 | output = c_fully_fold (output, false, NULL); |
8944 | ||
74f0c611 RH |
8945 | /* ??? Really, this should not be here. Users should be using a |
8946 | proper lvalue, dammit. But there's a long history of using casts | |
8947 | in the output operands. In cases like longlong.h, this becomes a | |
8948 | primitive form of typechecking -- if the cast can be removed, then | |
8949 | the output operand had a type of the proper width; otherwise we'll | |
8950 | get an error. Gross, but ... */ | |
3e4093b6 | 8951 | STRIP_NOPS (output); |
74f0c611 | 8952 | |
7bd11157 | 8953 | if (!lvalue_or_else (loc, output, lv_asm)) |
74f0c611 | 8954 | output = error_mark_node; |
8b6a5902 | 8955 | |
5544530a PB |
8956 | if (output != error_mark_node |
8957 | && (TREE_READONLY (output) | |
8958 | || TYPE_READONLY (TREE_TYPE (output)) | |
8959 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
8960 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
8961 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
c02065fc | 8962 | readonly_error (loc, output, lv_asm); |
5544530a | 8963 | |
6de9cd9a | 8964 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
74f0c611 RH |
8965 | oconstraints[i] = constraint; |
8966 | ||
8967 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
8968 | &allows_mem, &allows_reg, &is_inout)) | |
8969 | { | |
8970 | /* If the operand is going to end up in memory, | |
8971 | mark it addressable. */ | |
8972 | if (!allows_reg && !c_mark_addressable (output)) | |
8973 | output = error_mark_node; | |
bae5cddf JJ |
8974 | if (!(!allows_reg && allows_mem) |
8975 | && output != error_mark_node | |
8976 | && VOID_TYPE_P (TREE_TYPE (output))) | |
8977 | { | |
8978 | error_at (loc, "invalid use of void expression"); | |
8979 | output = error_mark_node; | |
8980 | } | |
74f0c611 RH |
8981 | } |
8982 | else | |
c22cacf3 | 8983 | output = error_mark_node; |
3e4093b6 | 8984 | |
74f0c611 | 8985 | TREE_VALUE (tail) = output; |
8b6a5902 | 8986 | } |
3e4093b6 | 8987 | |
74f0c611 RH |
8988 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) |
8989 | { | |
8990 | tree input; | |
8991 | ||
8992 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8993 | input = TREE_VALUE (tail); | |
8994 | ||
74f0c611 RH |
8995 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
8996 | oconstraints, &allows_mem, &allows_reg)) | |
8997 | { | |
8998 | /* If the operand is going to end up in memory, | |
8999 | mark it addressable. */ | |
b4c33883 AP |
9000 | if (!allows_reg && allows_mem) |
9001 | { | |
eadd3d0d JJ |
9002 | input = c_fully_fold (input, false, NULL); |
9003 | ||
b4c33883 AP |
9004 | /* Strip the nops as we allow this case. FIXME, this really |
9005 | should be rejected or made deprecated. */ | |
9006 | STRIP_NOPS (input); | |
9007 | if (!c_mark_addressable (input)) | |
9008 | input = error_mark_node; | |
bae5cddf | 9009 | } |
eadd3d0d | 9010 | else |
bae5cddf | 9011 | { |
eadd3d0d JJ |
9012 | struct c_expr expr; |
9013 | memset (&expr, 0, sizeof (expr)); | |
9014 | expr.value = input; | |
267bac10 | 9015 | expr = convert_lvalue_to_rvalue (loc, expr, true, false); |
eadd3d0d JJ |
9016 | input = c_fully_fold (expr.value, false, NULL); |
9017 | ||
9018 | if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) | |
9019 | { | |
9020 | error_at (loc, "invalid use of void expression"); | |
9021 | input = error_mark_node; | |
9022 | } | |
bae5cddf | 9023 | } |
74f0c611 RH |
9024 | } |
9025 | else | |
9026 | input = error_mark_node; | |
9027 | ||
9028 | TREE_VALUE (tail) = input; | |
9029 | } | |
3e4093b6 | 9030 | |
1c384bf1 RH |
9031 | /* ASMs with labels cannot have outputs. This should have been |
9032 | enforced by the parser. */ | |
9033 | gcc_assert (outputs == NULL || labels == NULL); | |
9034 | ||
9035 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
9f0e2d86 | 9036 | |
5544530a PB |
9037 | /* asm statements without outputs, including simple ones, are treated |
9038 | as volatile. */ | |
9039 | ASM_INPUT_P (args) = simple; | |
9040 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
74f0c611 | 9041 | |
9f0e2d86 | 9042 | return args; |
e5e809f4 | 9043 | } |
3e4093b6 | 9044 | \f |
c2255bc4 AH |
9045 | /* Generate a goto statement to LABEL. LOC is the location of the |
9046 | GOTO. */ | |
506e2710 RH |
9047 | |
9048 | tree | |
c2255bc4 | 9049 | c_finish_goto_label (location_t loc, tree label) |
506e2710 | 9050 | { |
e1b7793c | 9051 | tree decl = lookup_label_for_goto (loc, label); |
506e2710 RH |
9052 | if (!decl) |
9053 | return NULL_TREE; | |
506e2710 | 9054 | TREE_USED (decl) = 1; |
c2255bc4 AH |
9055 | { |
9056 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
9057 | SET_EXPR_LOCATION (t, loc); | |
9058 | return add_stmt (t); | |
9059 | } | |
506e2710 RH |
9060 | } |
9061 | ||
c2255bc4 AH |
9062 | /* Generate a computed goto statement to EXPR. LOC is the location of |
9063 | the GOTO. */ | |
506e2710 RH |
9064 | |
9065 | tree | |
c2255bc4 | 9066 | c_finish_goto_ptr (location_t loc, tree expr) |
506e2710 | 9067 | { |
c2255bc4 | 9068 | tree t; |
c1771a20 | 9069 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); |
928c19bb | 9070 | expr = c_fully_fold (expr, false, NULL); |
506e2710 | 9071 | expr = convert (ptr_type_node, expr); |
c2255bc4 AH |
9072 | t = build1 (GOTO_EXPR, void_type_node, expr); |
9073 | SET_EXPR_LOCATION (t, loc); | |
9074 | return add_stmt (t); | |
506e2710 RH |
9075 | } |
9076 | ||
5088b058 | 9077 | /* Generate a C `return' statement. RETVAL is the expression for what |
c2255bc4 AH |
9078 | to return, or a null pointer for `return;' with no value. LOC is |
9079 | the location of the return statement. If ORIGTYPE is not NULL_TREE, it | |
9080 | is the original type of RETVAL. */ | |
de520661 | 9081 | |
506e2710 | 9082 | tree |
c2255bc4 | 9083 | c_finish_return (location_t loc, tree retval, tree origtype) |
3e4093b6 | 9084 | { |
0c9b182b JJ |
9085 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; |
9086 | bool no_warning = false; | |
928c19bb | 9087 | bool npc = false; |
36536d79 | 9088 | size_t rank = 0; |
3e4093b6 RS |
9089 | |
9090 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
c2255bc4 AH |
9091 | warning_at (loc, 0, |
9092 | "function declared %<noreturn%> has a %<return%> statement"); | |
3e4093b6 | 9093 | |
36536d79 BI |
9094 | if (flag_enable_cilkplus && contains_array_notation_expr (retval)) |
9095 | { | |
9096 | /* Array notations are allowed in a return statement if it is inside a | |
9097 | built-in array notation reduction function. */ | |
9098 | if (!find_rank (loc, retval, retval, false, &rank)) | |
9099 | return error_mark_node; | |
9100 | if (rank >= 1) | |
9101 | { | |
9102 | error_at (loc, "array notation expression cannot be used as a " | |
9103 | "return value"); | |
9104 | return error_mark_node; | |
9105 | } | |
9106 | } | |
939b37da BI |
9107 | if (flag_enable_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT) |
9108 | { | |
9109 | error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not " | |
9110 | "allowed"); | |
9111 | return error_mark_node; | |
9112 | } | |
928c19bb JM |
9113 | if (retval) |
9114 | { | |
8ce94e44 | 9115 | tree semantic_type = NULL_TREE; |
928c19bb | 9116 | npc = null_pointer_constant_p (retval); |
8ce94e44 JM |
9117 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) |
9118 | { | |
9119 | semantic_type = TREE_TYPE (retval); | |
9120 | retval = TREE_OPERAND (retval, 0); | |
9121 | } | |
928c19bb | 9122 | retval = c_fully_fold (retval, false, NULL); |
8ce94e44 JM |
9123 | if (semantic_type) |
9124 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
928c19bb JM |
9125 | } |
9126 | ||
3e4093b6 | 9127 | if (!retval) |
de520661 | 9128 | { |
3e4093b6 RS |
9129 | current_function_returns_null = 1; |
9130 | if ((warn_return_type || flag_isoc99) | |
9131 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
0c9b182b | 9132 | { |
b8698a0f | 9133 | pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type, |
fcf73884 | 9134 | "%<return%> with no value, in " |
0c9b182b JJ |
9135 | "function returning non-void"); |
9136 | no_warning = true; | |
9137 | } | |
400fbf9f | 9138 | } |
3e4093b6 | 9139 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
de520661 | 9140 | { |
3e4093b6 | 9141 | current_function_returns_null = 1; |
2397c575 | 9142 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) |
b8698a0f | 9143 | pedwarn (loc, 0, |
509c9d60 | 9144 | "%<return%> with a value, in function returning void"); |
b8698a0f | 9145 | else |
c1771a20 | 9146 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 9147 | "%<return%> with expression, in function returning void"); |
de520661 | 9148 | } |
3e4093b6 | 9149 | else |
de520661 | 9150 | { |
c2255bc4 AH |
9151 | tree t = convert_for_assignment (loc, valtype, retval, origtype, |
9152 | ic_return, | |
9153 | npc, NULL_TREE, NULL_TREE, 0); | |
3e4093b6 RS |
9154 | tree res = DECL_RESULT (current_function_decl); |
9155 | tree inner; | |
9feb29df | 9156 | bool save; |
3e4093b6 RS |
9157 | |
9158 | current_function_returns_value = 1; | |
9159 | if (t == error_mark_node) | |
506e2710 | 9160 | return NULL_TREE; |
3e4093b6 | 9161 | |
9feb29df JJ |
9162 | save = in_late_binary_op; |
9163 | if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE | |
9164 | || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE) | |
9165 | in_late_binary_op = true; | |
3e4093b6 | 9166 | inner = t = convert (TREE_TYPE (res), t); |
9feb29df | 9167 | in_late_binary_op = save; |
3e4093b6 RS |
9168 | |
9169 | /* Strip any conversions, additions, and subtractions, and see if | |
9170 | we are returning the address of a local variable. Warn if so. */ | |
9171 | while (1) | |
8b6a5902 | 9172 | { |
3e4093b6 | 9173 | switch (TREE_CODE (inner)) |
8b6a5902 | 9174 | { |
849421a3 JJ |
9175 | CASE_CONVERT: |
9176 | case NON_LVALUE_EXPR: | |
3e4093b6 | 9177 | case PLUS_EXPR: |
849421a3 | 9178 | case POINTER_PLUS_EXPR: |
3e4093b6 RS |
9179 | inner = TREE_OPERAND (inner, 0); |
9180 | continue; | |
9181 | ||
9182 | case MINUS_EXPR: | |
9183 | /* If the second operand of the MINUS_EXPR has a pointer | |
9184 | type (or is converted from it), this may be valid, so | |
9185 | don't give a warning. */ | |
9186 | { | |
9187 | tree op1 = TREE_OPERAND (inner, 1); | |
8b6a5902 | 9188 | |
3f75a254 | 9189 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) |
1043771b TB |
9190 | && (CONVERT_EXPR_P (op1) |
9191 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
3e4093b6 | 9192 | op1 = TREE_OPERAND (op1, 0); |
8b6a5902 | 9193 | |
3e4093b6 RS |
9194 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
9195 | break; | |
8b6a5902 | 9196 | |
3e4093b6 RS |
9197 | inner = TREE_OPERAND (inner, 0); |
9198 | continue; | |
9199 | } | |
400fbf9f | 9200 | |
3e4093b6 RS |
9201 | case ADDR_EXPR: |
9202 | inner = TREE_OPERAND (inner, 0); | |
c2f4acb7 | 9203 | |
6615c446 | 9204 | while (REFERENCE_CLASS_P (inner) |
c22cacf3 | 9205 | && TREE_CODE (inner) != INDIRECT_REF) |
3e4093b6 | 9206 | inner = TREE_OPERAND (inner, 0); |
8b6a5902 | 9207 | |
a2f1f4c3 | 9208 | if (DECL_P (inner) |
3f75a254 JM |
9209 | && !DECL_EXTERNAL (inner) |
9210 | && !TREE_STATIC (inner) | |
3e4093b6 | 9211 | && DECL_CONTEXT (inner) == current_function_decl) |
c2255bc4 | 9212 | warning_at (loc, |
880661a4 JW |
9213 | OPT_Wreturn_local_addr, "function returns address " |
9214 | "of local variable"); | |
3e4093b6 | 9215 | break; |
8b6a5902 | 9216 | |
3e4093b6 RS |
9217 | default: |
9218 | break; | |
9219 | } | |
de520661 | 9220 | |
3e4093b6 RS |
9221 | break; |
9222 | } | |
9223 | ||
53fb4de3 | 9224 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
c2255bc4 | 9225 | SET_EXPR_LOCATION (retval, loc); |
ca085fd7 MLI |
9226 | |
9227 | if (warn_sequence_point) | |
9228 | verify_sequence_points (retval); | |
de520661 | 9229 | } |
8b6a5902 | 9230 | |
c2255bc4 | 9231 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); |
0c9b182b JJ |
9232 | TREE_NO_WARNING (ret_stmt) |= no_warning; |
9233 | return add_stmt (ret_stmt); | |
de520661 | 9234 | } |
3e4093b6 RS |
9235 | \f |
9236 | struct c_switch { | |
604f5adf ILT |
9237 | /* The SWITCH_EXPR being built. */ |
9238 | tree switch_expr; | |
a6c0a76c | 9239 | |
89dbed81 | 9240 | /* The original type of the testing expression, i.e. before the |
a6c0a76c SB |
9241 | default conversion is applied. */ |
9242 | tree orig_type; | |
9243 | ||
3e4093b6 RS |
9244 | /* A splay-tree mapping the low element of a case range to the high |
9245 | element, or NULL_TREE if there is no high element. Used to | |
9246 | determine whether or not a new case label duplicates an old case | |
9247 | label. We need a tree, rather than simply a hash table, because | |
9248 | of the GNU case range extension. */ | |
9249 | splay_tree cases; | |
a6c0a76c | 9250 | |
e1b7793c ILT |
9251 | /* The bindings at the point of the switch. This is used for |
9252 | warnings crossing decls when branching to a case label. */ | |
9253 | struct c_spot_bindings *bindings; | |
187230a7 | 9254 | |
3e4093b6 RS |
9255 | /* The next node on the stack. */ |
9256 | struct c_switch *next; | |
9257 | }; | |
400fbf9f | 9258 | |
3e4093b6 RS |
9259 | /* A stack of the currently active switch statements. The innermost |
9260 | switch statement is on the top of the stack. There is no need to | |
9261 | mark the stack for garbage collection because it is only active | |
9262 | during the processing of the body of a function, and we never | |
9263 | collect at that point. */ | |
de520661 | 9264 | |
506e2710 | 9265 | struct c_switch *c_switch_stack; |
de520661 | 9266 | |
3e4093b6 | 9267 | /* Start a C switch statement, testing expression EXP. Return the new |
c2255bc4 AH |
9268 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. |
9269 | SWITCH_COND_LOC is the location of the switch's condition. */ | |
de520661 | 9270 | |
3e4093b6 | 9271 | tree |
c2255bc4 AH |
9272 | c_start_case (location_t switch_loc, |
9273 | location_t switch_cond_loc, | |
9274 | tree exp) | |
de520661 | 9275 | { |
c58e8676 | 9276 | tree orig_type = error_mark_node; |
3e4093b6 | 9277 | struct c_switch *cs; |
2f6e4e97 | 9278 | |
3e4093b6 | 9279 | if (exp != error_mark_node) |
de520661 | 9280 | { |
3e4093b6 RS |
9281 | orig_type = TREE_TYPE (exp); |
9282 | ||
c58e8676 | 9283 | if (!INTEGRAL_TYPE_P (orig_type)) |
de520661 | 9284 | { |
c58e8676 VR |
9285 | if (orig_type != error_mark_node) |
9286 | { | |
c2255bc4 | 9287 | error_at (switch_cond_loc, "switch quantity not an integer"); |
c58e8676 VR |
9288 | orig_type = error_mark_node; |
9289 | } | |
3e4093b6 | 9290 | exp = integer_zero_node; |
de520661 | 9291 | } |
3e4093b6 | 9292 | else |
de520661 | 9293 | { |
c58e8676 | 9294 | tree type = TYPE_MAIN_VARIANT (orig_type); |
8b6a5902 | 9295 | |
8400e75e | 9296 | if (!in_system_header_at (input_location) |
3e4093b6 RS |
9297 | && (type == long_integer_type_node |
9298 | || type == long_unsigned_type_node)) | |
c2255bc4 AH |
9299 | warning_at (switch_cond_loc, |
9300 | OPT_Wtraditional, "%<long%> switch expression not " | |
9301 | "converted to %<int%> in ISO C"); | |
8b6a5902 | 9302 | |
928c19bb | 9303 | exp = c_fully_fold (exp, false, NULL); |
3e4093b6 | 9304 | exp = default_conversion (exp); |
ca085fd7 MLI |
9305 | |
9306 | if (warn_sequence_point) | |
9307 | verify_sequence_points (exp); | |
3e4093b6 RS |
9308 | } |
9309 | } | |
9310 | ||
604f5adf | 9311 | /* Add this new SWITCH_EXPR to the stack. */ |
5d038c4c | 9312 | cs = XNEW (struct c_switch); |
604f5adf | 9313 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); |
c2255bc4 | 9314 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); |
a6c0a76c | 9315 | cs->orig_type = orig_type; |
3e4093b6 | 9316 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
e1b7793c | 9317 | cs->bindings = c_get_switch_bindings (); |
506e2710 RH |
9318 | cs->next = c_switch_stack; |
9319 | c_switch_stack = cs; | |
3e4093b6 | 9320 | |
604f5adf | 9321 | return add_stmt (cs->switch_expr); |
3e4093b6 RS |
9322 | } |
9323 | ||
c2255bc4 | 9324 | /* Process a case label at location LOC. */ |
3e4093b6 RS |
9325 | |
9326 | tree | |
c2255bc4 | 9327 | do_case (location_t loc, tree low_value, tree high_value) |
3e4093b6 RS |
9328 | { |
9329 | tree label = NULL_TREE; | |
9330 | ||
17cede2e JM |
9331 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) |
9332 | { | |
9333 | low_value = c_fully_fold (low_value, false, NULL); | |
9334 | if (TREE_CODE (low_value) == INTEGER_CST) | |
c1771a20 | 9335 | pedwarn (input_location, OPT_Wpedantic, |
17cede2e JM |
9336 | "case label is not an integer constant expression"); |
9337 | } | |
9338 | ||
9339 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
9340 | { | |
9341 | high_value = c_fully_fold (high_value, false, NULL); | |
9342 | if (TREE_CODE (high_value) == INTEGER_CST) | |
c1771a20 | 9343 | pedwarn (input_location, OPT_Wpedantic, |
17cede2e JM |
9344 | "case label is not an integer constant expression"); |
9345 | } | |
9346 | ||
e1b7793c | 9347 | if (c_switch_stack == NULL) |
187230a7 JM |
9348 | { |
9349 | if (low_value) | |
e1b7793c | 9350 | error_at (loc, "case label not within a switch statement"); |
187230a7 | 9351 | else |
e1b7793c ILT |
9352 | error_at (loc, "%<default%> label not within a switch statement"); |
9353 | return NULL_TREE; | |
187230a7 | 9354 | } |
de520661 | 9355 | |
e1b7793c ILT |
9356 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, |
9357 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
9358 | loc)) | |
9359 | return NULL_TREE; | |
9360 | ||
9361 | label = c_add_case_label (loc, c_switch_stack->cases, | |
9362 | SWITCH_COND (c_switch_stack->switch_expr), | |
9363 | c_switch_stack->orig_type, | |
9364 | low_value, high_value); | |
9365 | if (label == error_mark_node) | |
9366 | label = NULL_TREE; | |
3e4093b6 RS |
9367 | return label; |
9368 | } | |
de520661 | 9369 | |
3e4093b6 | 9370 | /* Finish the switch statement. */ |
de520661 | 9371 | |
3e4093b6 | 9372 | void |
325c3691 | 9373 | c_finish_case (tree body) |
3e4093b6 | 9374 | { |
506e2710 | 9375 | struct c_switch *cs = c_switch_stack; |
fbc315db | 9376 | location_t switch_location; |
3e4093b6 | 9377 | |
604f5adf | 9378 | SWITCH_BODY (cs->switch_expr) = body; |
325c3691 | 9379 | |
6de9cd9a | 9380 | /* Emit warnings as needed. */ |
c2255bc4 | 9381 | switch_location = EXPR_LOCATION (cs->switch_expr); |
fbc315db ILT |
9382 | c_do_switch_warnings (cs->cases, switch_location, |
9383 | TREE_TYPE (cs->switch_expr), | |
9384 | SWITCH_COND (cs->switch_expr)); | |
6de9cd9a | 9385 | |
3e4093b6 | 9386 | /* Pop the stack. */ |
506e2710 | 9387 | c_switch_stack = cs->next; |
3e4093b6 | 9388 | splay_tree_delete (cs->cases); |
e1b7793c | 9389 | c_release_switch_bindings (cs->bindings); |
5d038c4c | 9390 | XDELETE (cs); |
de520661 | 9391 | } |
325c3691 | 9392 | \f |
506e2710 RH |
9393 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
9394 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
9395 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
9396 | statement, and was not surrounded with parenthesis. */ | |
325c3691 | 9397 | |
9e51cf9d | 9398 | void |
506e2710 RH |
9399 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
9400 | tree else_block, bool nested_if) | |
325c3691 | 9401 | { |
506e2710 | 9402 | tree stmt; |
325c3691 | 9403 | |
25c22937 BI |
9404 | /* If the condition has array notations, then the rank of the then_block and |
9405 | else_block must be either 0 or be equal to the rank of the condition. If | |
9406 | the condition does not have array notations then break them up as it is | |
9407 | broken up in a normal expression. */ | |
9408 | if (flag_enable_cilkplus && contains_array_notation_expr (cond)) | |
9409 | { | |
9410 | size_t then_rank = 0, cond_rank = 0, else_rank = 0; | |
9411 | if (!find_rank (if_locus, cond, cond, true, &cond_rank)) | |
9412 | return; | |
9413 | if (then_block | |
9414 | && !find_rank (if_locus, then_block, then_block, true, &then_rank)) | |
9415 | return; | |
9416 | if (else_block | |
9417 | && !find_rank (if_locus, else_block, else_block, true, &else_rank)) | |
9418 | return; | |
9419 | if (cond_rank != then_rank && then_rank != 0) | |
9420 | { | |
9421 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9422 | " and the then-block"); | |
9423 | return; | |
9424 | } | |
9425 | else if (cond_rank != else_rank && else_rank != 0) | |
9426 | { | |
9427 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9428 | " and the else-block"); | |
9429 | return; | |
9430 | } | |
9431 | } | |
506e2710 RH |
9432 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
9433 | if (warn_parentheses && nested_if && else_block == NULL) | |
325c3691 | 9434 | { |
506e2710 | 9435 | tree inner_if = then_block; |
16865eaa | 9436 | |
61ada8ae | 9437 | /* We know from the grammar productions that there is an IF nested |
506e2710 RH |
9438 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
9439 | it might not be exactly THEN_BLOCK, but should be the last | |
9440 | non-container statement within. */ | |
9441 | while (1) | |
9442 | switch (TREE_CODE (inner_if)) | |
9443 | { | |
9444 | case COND_EXPR: | |
9445 | goto found; | |
9446 | case BIND_EXPR: | |
9447 | inner_if = BIND_EXPR_BODY (inner_if); | |
9448 | break; | |
9449 | case STATEMENT_LIST: | |
9450 | inner_if = expr_last (then_block); | |
9451 | break; | |
9452 | case TRY_FINALLY_EXPR: | |
9453 | case TRY_CATCH_EXPR: | |
9454 | inner_if = TREE_OPERAND (inner_if, 0); | |
9455 | break; | |
9456 | default: | |
366de0ce | 9457 | gcc_unreachable (); |
506e2710 RH |
9458 | } |
9459 | found: | |
16865eaa | 9460 | |
506e2710 | 9461 | if (COND_EXPR_ELSE (inner_if)) |
fab922b1 MLI |
9462 | warning_at (if_locus, OPT_Wparentheses, |
9463 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
506e2710 | 9464 | } |
16865eaa | 9465 | |
2214de30 | 9466 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); |
a281759f | 9467 | SET_EXPR_LOCATION (stmt, if_locus); |
506e2710 | 9468 | add_stmt (stmt); |
325c3691 RH |
9469 | } |
9470 | ||
506e2710 RH |
9471 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
9472 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
9473 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
61ada8ae | 9474 | the statement controlled by the loop. BLAB is the break label. CLAB is |
506e2710 | 9475 | the continue label. Everything is allowed to be NULL. */ |
325c3691 RH |
9476 | |
9477 | void | |
506e2710 RH |
9478 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
9479 | tree blab, tree clab, bool cond_is_first) | |
325c3691 | 9480 | { |
506e2710 RH |
9481 | tree entry = NULL, exit = NULL, t; |
9482 | ||
36536d79 BI |
9483 | if (flag_enable_cilkplus && contains_array_notation_expr (cond)) |
9484 | { | |
9485 | error_at (start_locus, "array notation expression cannot be used in a " | |
9486 | "loop%'s condition"); | |
9487 | return; | |
9488 | } | |
9489 | ||
28af952a RS |
9490 | /* If the condition is zero don't generate a loop construct. */ |
9491 | if (cond && integer_zerop (cond)) | |
9492 | { | |
9493 | if (cond_is_first) | |
9494 | { | |
9495 | t = build_and_jump (&blab); | |
9496 | SET_EXPR_LOCATION (t, start_locus); | |
9497 | add_stmt (t); | |
9498 | } | |
9499 | } | |
9500 | else | |
506e2710 RH |
9501 | { |
9502 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
c22cacf3 | 9503 | |
506e2710 | 9504 | /* If we have an exit condition, then we build an IF with gotos either |
c22cacf3 MS |
9505 | out of the loop, or to the top of it. If there's no exit condition, |
9506 | then we just build a jump back to the top. */ | |
506e2710 | 9507 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); |
c22cacf3 | 9508 | |
28af952a | 9509 | if (cond && !integer_nonzerop (cond)) |
c22cacf3 MS |
9510 | { |
9511 | /* Canonicalize the loop condition to the end. This means | |
9512 | generating a branch to the loop condition. Reuse the | |
9513 | continue label, if possible. */ | |
9514 | if (cond_is_first) | |
9515 | { | |
9516 | if (incr || !clab) | |
9517 | { | |
9518 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
9519 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
9520 | } | |
9521 | else | |
9522 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
a281759f | 9523 | SET_EXPR_LOCATION (t, start_locus); |
c22cacf3 MS |
9524 | add_stmt (t); |
9525 | } | |
9526 | ||
506e2710 | 9527 | t = build_and_jump (&blab); |
506e2710 | 9528 | if (cond_is_first) |
db3927fb AH |
9529 | exit = fold_build3_loc (start_locus, |
9530 | COND_EXPR, void_type_node, cond, exit, t); | |
506e2710 | 9531 | else |
db3927fb AH |
9532 | exit = fold_build3_loc (input_location, |
9533 | COND_EXPR, void_type_node, cond, exit, t); | |
c22cacf3 MS |
9534 | } |
9535 | ||
506e2710 RH |
9536 | add_stmt (top); |
9537 | } | |
c22cacf3 | 9538 | |
506e2710 RH |
9539 | if (body) |
9540 | add_stmt (body); | |
9541 | if (clab) | |
9542 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
9543 | if (incr) | |
9544 | add_stmt (incr); | |
9545 | if (entry) | |
9546 | add_stmt (entry); | |
9547 | if (exit) | |
9548 | add_stmt (exit); | |
9549 | if (blab) | |
9550 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
325c3691 | 9551 | } |
325c3691 RH |
9552 | |
9553 | tree | |
c2255bc4 | 9554 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) |
325c3691 | 9555 | { |
089efaa4 | 9556 | bool skip; |
506e2710 | 9557 | tree label = *label_p; |
325c3691 | 9558 | |
089efaa4 ILT |
9559 | /* In switch statements break is sometimes stylistically used after |
9560 | a return statement. This can lead to spurious warnings about | |
9561 | control reaching the end of a non-void function when it is | |
9562 | inlined. Note that we are calling block_may_fallthru with | |
9563 | language specific tree nodes; this works because | |
9564 | block_may_fallthru returns true when given something it does not | |
9565 | understand. */ | |
9566 | skip = !block_may_fallthru (cur_stmt_list); | |
9567 | ||
506e2710 | 9568 | if (!label) |
089efaa4 ILT |
9569 | { |
9570 | if (!skip) | |
c2255bc4 | 9571 | *label_p = label = create_artificial_label (loc); |
089efaa4 | 9572 | } |
953ff289 DN |
9573 | else if (TREE_CODE (label) == LABEL_DECL) |
9574 | ; | |
9575 | else switch (TREE_INT_CST_LOW (label)) | |
506e2710 | 9576 | { |
953ff289 | 9577 | case 0: |
506e2710 | 9578 | if (is_break) |
c2255bc4 | 9579 | error_at (loc, "break statement not within loop or switch"); |
506e2710 | 9580 | else |
c2255bc4 | 9581 | error_at (loc, "continue statement not within a loop"); |
506e2710 | 9582 | return NULL_TREE; |
953ff289 DN |
9583 | |
9584 | case 1: | |
9585 | gcc_assert (is_break); | |
c2255bc4 | 9586 | error_at (loc, "break statement used with OpenMP for loop"); |
953ff289 DN |
9587 | return NULL_TREE; |
9588 | ||
c02065fc AH |
9589 | case 2: |
9590 | if (is_break) | |
9591 | error ("break statement within %<#pragma simd%> loop body"); | |
9592 | else | |
9593 | error ("continue statement within %<#pragma simd%> loop body"); | |
9594 | return NULL_TREE; | |
9595 | ||
953ff289 DN |
9596 | default: |
9597 | gcc_unreachable (); | |
506e2710 | 9598 | } |
325c3691 | 9599 | |
089efaa4 ILT |
9600 | if (skip) |
9601 | return NULL_TREE; | |
9602 | ||
2e28e797 JH |
9603 | if (!is_break) |
9604 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
9605 | ||
53fb4de3 | 9606 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
325c3691 RH |
9607 | } |
9608 | ||
506e2710 | 9609 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
3a5b9284 RH |
9610 | |
9611 | static void | |
c2255bc4 | 9612 | emit_side_effect_warnings (location_t loc, tree expr) |
3a5b9284 | 9613 | { |
e6b5a630 RH |
9614 | if (expr == error_mark_node) |
9615 | ; | |
9616 | else if (!TREE_SIDE_EFFECTS (expr)) | |
3a5b9284 RH |
9617 | { |
9618 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
c2255bc4 | 9619 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); |
3a5b9284 | 9620 | } |
27f33b15 | 9621 | else |
c2255bc4 | 9622 | warn_if_unused_value (expr, loc); |
3a5b9284 RH |
9623 | } |
9624 | ||
506e2710 | 9625 | /* Process an expression as if it were a complete statement. Emit |
c2255bc4 AH |
9626 | diagnostics, but do not call ADD_STMT. LOC is the location of the |
9627 | statement. */ | |
3a5b9284 | 9628 | |
506e2710 | 9629 | tree |
c2255bc4 | 9630 | c_process_expr_stmt (location_t loc, tree expr) |
3a5b9284 | 9631 | { |
056928b2 JJ |
9632 | tree exprv; |
9633 | ||
3a5b9284 | 9634 | if (!expr) |
506e2710 | 9635 | return NULL_TREE; |
3a5b9284 | 9636 | |
928c19bb JM |
9637 | expr = c_fully_fold (expr, false, NULL); |
9638 | ||
3a5b9284 RH |
9639 | if (warn_sequence_point) |
9640 | verify_sequence_points (expr); | |
9641 | ||
9642 | if (TREE_TYPE (expr) != error_mark_node | |
9643 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
9644 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
c2255bc4 | 9645 | error_at (loc, "expression statement has incomplete type"); |
3a5b9284 RH |
9646 | |
9647 | /* If we're not processing a statement expression, warn about unused values. | |
9648 | Warnings for statement expressions will be emitted later, once we figure | |
9649 | out which is the result. */ | |
9650 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
27f33b15 | 9651 | && warn_unused_value) |
c2255bc4 | 9652 | emit_side_effect_warnings (loc, expr); |
3a5b9284 | 9653 | |
056928b2 JJ |
9654 | exprv = expr; |
9655 | while (TREE_CODE (exprv) == COMPOUND_EXPR) | |
9656 | exprv = TREE_OPERAND (exprv, 1); | |
f1a89dd0 JJ |
9657 | while (CONVERT_EXPR_P (exprv)) |
9658 | exprv = TREE_OPERAND (exprv, 0); | |
9659 | if (DECL_P (exprv) | |
9660 | || handled_component_p (exprv) | |
9661 | || TREE_CODE (exprv) == ADDR_EXPR) | |
056928b2 | 9662 | mark_exp_read (exprv); |
fa8351f8 | 9663 | |
3a5b9284 RH |
9664 | /* If the expression is not of a type to which we cannot assign a line |
9665 | number, wrap the thing in a no-op NOP_EXPR. */ | |
6615c446 | 9666 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) |
c2255bc4 AH |
9667 | { |
9668 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
9669 | SET_EXPR_LOCATION (expr, loc); | |
9670 | } | |
506e2710 RH |
9671 | |
9672 | return expr; | |
9673 | } | |
9674 | ||
c2255bc4 AH |
9675 | /* Emit an expression as a statement. LOC is the location of the |
9676 | expression. */ | |
506e2710 RH |
9677 | |
9678 | tree | |
c2255bc4 | 9679 | c_finish_expr_stmt (location_t loc, tree expr) |
506e2710 RH |
9680 | { |
9681 | if (expr) | |
c2255bc4 | 9682 | return add_stmt (c_process_expr_stmt (loc, expr)); |
506e2710 RH |
9683 | else |
9684 | return NULL; | |
3a5b9284 RH |
9685 | } |
9686 | ||
9687 | /* Do the opposite and emit a statement as an expression. To begin, | |
9688 | create a new binding level and return it. */ | |
325c3691 RH |
9689 | |
9690 | tree | |
9691 | c_begin_stmt_expr (void) | |
9692 | { | |
9693 | tree ret; | |
9694 | ||
9695 | /* We must force a BLOCK for this level so that, if it is not expanded | |
9696 | later, there is a way to turn off the entire subtree of blocks that | |
9697 | are contained in it. */ | |
9698 | keep_next_level (); | |
9699 | ret = c_begin_compound_stmt (true); | |
e1b7793c ILT |
9700 | |
9701 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
9702 | ? NULL | |
9703 | : c_switch_stack->bindings); | |
325c3691 RH |
9704 | |
9705 | /* Mark the current statement list as belonging to a statement list. */ | |
9706 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
9707 | ||
9708 | return ret; | |
9709 | } | |
9710 | ||
c2255bc4 AH |
9711 | /* LOC is the location of the compound statement to which this body |
9712 | belongs. */ | |
9713 | ||
325c3691 | 9714 | tree |
c2255bc4 | 9715 | c_finish_stmt_expr (location_t loc, tree body) |
325c3691 | 9716 | { |
3a5b9284 | 9717 | tree last, type, tmp, val; |
325c3691 RH |
9718 | tree *last_p; |
9719 | ||
c2255bc4 | 9720 | body = c_end_compound_stmt (loc, body, true); |
e1b7793c ILT |
9721 | |
9722 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
9723 | ? NULL | |
9724 | : c_switch_stack->bindings); | |
325c3691 | 9725 | |
3a5b9284 RH |
9726 | /* Locate the last statement in BODY. See c_end_compound_stmt |
9727 | about always returning a BIND_EXPR. */ | |
9728 | last_p = &BIND_EXPR_BODY (body); | |
9729 | last = BIND_EXPR_BODY (body); | |
9730 | ||
9731 | continue_searching: | |
325c3691 RH |
9732 | if (TREE_CODE (last) == STATEMENT_LIST) |
9733 | { | |
3a5b9284 RH |
9734 | tree_stmt_iterator i; |
9735 | ||
9736 | /* This can happen with degenerate cases like ({ }). No value. */ | |
9737 | if (!TREE_SIDE_EFFECTS (last)) | |
9738 | return body; | |
9739 | ||
9740 | /* If we're supposed to generate side effects warnings, process | |
9741 | all of the statements except the last. */ | |
27f33b15 | 9742 | if (warn_unused_value) |
325c3691 | 9743 | { |
3a5b9284 | 9744 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
c2255bc4 AH |
9745 | { |
9746 | location_t tloc; | |
9747 | tree t = tsi_stmt (i); | |
9748 | ||
9749 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
9750 | emit_side_effect_warnings (tloc, t); | |
9751 | } | |
325c3691 RH |
9752 | } |
9753 | else | |
3a5b9284 RH |
9754 | i = tsi_last (last); |
9755 | last_p = tsi_stmt_ptr (i); | |
9756 | last = *last_p; | |
325c3691 RH |
9757 | } |
9758 | ||
3a5b9284 RH |
9759 | /* If the end of the list is exception related, then the list was split |
9760 | by a call to push_cleanup. Continue searching. */ | |
9761 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
9762 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
9763 | { | |
9764 | last_p = &TREE_OPERAND (last, 0); | |
9765 | last = *last_p; | |
9766 | goto continue_searching; | |
9767 | } | |
9768 | ||
26d8af35 JM |
9769 | if (last == error_mark_node) |
9770 | return last; | |
9771 | ||
3a5b9284 RH |
9772 | /* In the case that the BIND_EXPR is not necessary, return the |
9773 | expression out from inside it. */ | |
26d8af35 JM |
9774 | if (last == BIND_EXPR_BODY (body) |
9775 | && BIND_EXPR_VARS (body) == NULL) | |
591baeb0 | 9776 | { |
928c19bb JM |
9777 | /* Even if this looks constant, do not allow it in a constant |
9778 | expression. */ | |
e5a94231 | 9779 | last = c_wrap_maybe_const (last, true); |
591baeb0 JM |
9780 | /* Do not warn if the return value of a statement expression is |
9781 | unused. */ | |
928c19bb | 9782 | TREE_NO_WARNING (last) = 1; |
591baeb0 JM |
9783 | return last; |
9784 | } | |
325c3691 RH |
9785 | |
9786 | /* Extract the type of said expression. */ | |
9787 | type = TREE_TYPE (last); | |
325c3691 | 9788 | |
3a5b9284 RH |
9789 | /* If we're not returning a value at all, then the BIND_EXPR that |
9790 | we already have is a fine expression to return. */ | |
9791 | if (!type || VOID_TYPE_P (type)) | |
9792 | return body; | |
9793 | ||
9794 | /* Now that we've located the expression containing the value, it seems | |
9795 | silly to make voidify_wrapper_expr repeat the process. Create a | |
9796 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
9797 | tmp = create_tmp_var_raw (type, NULL); | |
9798 | ||
9799 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
9800 | tree_expr_nonnegative_p giving up immediately. */ | |
9801 | val = last; | |
9802 | if (TREE_CODE (val) == NOP_EXPR | |
9803 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
9804 | val = TREE_OPERAND (val, 0); | |
9805 | ||
53fb4de3 | 9806 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
5e278028 | 9807 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); |
3a5b9284 | 9808 | |
c2255bc4 AH |
9809 | { |
9810 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
9811 | SET_EXPR_LOCATION (t, loc); | |
9812 | return t; | |
9813 | } | |
325c3691 RH |
9814 | } |
9815 | \f | |
9816 | /* Begin and end compound statements. This is as simple as pushing | |
9817 | and popping new statement lists from the tree. */ | |
9818 | ||
9819 | tree | |
9820 | c_begin_compound_stmt (bool do_scope) | |
9821 | { | |
9822 | tree stmt = push_stmt_list (); | |
9823 | if (do_scope) | |
4dfa0342 | 9824 | push_scope (); |
325c3691 RH |
9825 | return stmt; |
9826 | } | |
9827 | ||
c2255bc4 AH |
9828 | /* End a compound statement. STMT is the statement. LOC is the |
9829 | location of the compound statement-- this is usually the location | |
9830 | of the opening brace. */ | |
9831 | ||
325c3691 | 9832 | tree |
c2255bc4 | 9833 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) |
325c3691 RH |
9834 | { |
9835 | tree block = NULL; | |
9836 | ||
9837 | if (do_scope) | |
9838 | { | |
9839 | if (c_dialect_objc ()) | |
9840 | objc_clear_super_receiver (); | |
9841 | block = pop_scope (); | |
9842 | } | |
9843 | ||
9844 | stmt = pop_stmt_list (stmt); | |
c2255bc4 | 9845 | stmt = c_build_bind_expr (loc, block, stmt); |
325c3691 RH |
9846 | |
9847 | /* If this compound statement is nested immediately inside a statement | |
9848 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
9849 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
9850 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
9851 | was really last. */ | |
38e01f9e | 9852 | if (building_stmt_list_p () |
325c3691 RH |
9853 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) |
9854 | && TREE_CODE (stmt) != BIND_EXPR) | |
9855 | { | |
53fb4de3 | 9856 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
325c3691 | 9857 | TREE_SIDE_EFFECTS (stmt) = 1; |
c2255bc4 | 9858 | SET_EXPR_LOCATION (stmt, loc); |
325c3691 RH |
9859 | } |
9860 | ||
9861 | return stmt; | |
9862 | } | |
5a508662 RH |
9863 | |
9864 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
9865 | when the current scope is exited. EH_ONLY is true when this is not | |
9866 | meant to apply to normal control flow transfer. */ | |
9867 | ||
9868 | void | |
c2255bc4 | 9869 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
5a508662 | 9870 | { |
3a5b9284 RH |
9871 | enum tree_code code; |
9872 | tree stmt, list; | |
9873 | bool stmt_expr; | |
9874 | ||
9875 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
c2255bc4 | 9876 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); |
5a508662 | 9877 | add_stmt (stmt); |
3a5b9284 RH |
9878 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
9879 | list = push_stmt_list (); | |
9880 | TREE_OPERAND (stmt, 0) = list; | |
9881 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
5a508662 | 9882 | } |
325c3691 | 9883 | \f |
3e4093b6 RS |
9884 | /* Build a binary-operation expression without default conversions. |
9885 | CODE is the kind of expression to build. | |
ba47d38d | 9886 | LOCATION is the operator's location. |
3e4093b6 RS |
9887 | This function differs from `build' in several ways: |
9888 | the data type of the result is computed and recorded in it, | |
9889 | warnings are generated if arg data types are invalid, | |
9890 | special handling for addition and subtraction of pointers is known, | |
9891 | and some optimization is done (operations on narrow ints | |
9892 | are done in the narrower type when that gives the same result). | |
9893 | Constant folding is also done before the result is returned. | |
de520661 | 9894 | |
3e4093b6 RS |
9895 | Note that the operands will never have enumeral types, or function |
9896 | or array types, because either they will have the default conversions | |
9897 | performed or they have both just been converted to some other type in which | |
9898 | the arithmetic is to be done. */ | |
9899 | ||
9900 | tree | |
ba47d38d AH |
9901 | build_binary_op (location_t location, enum tree_code code, |
9902 | tree orig_op0, tree orig_op1, int convert_p) | |
de520661 | 9903 | { |
8ce94e44 JM |
9904 | tree type0, type1, orig_type0, orig_type1; |
9905 | tree eptype; | |
3e4093b6 RS |
9906 | enum tree_code code0, code1; |
9907 | tree op0, op1; | |
c9f9eb5d | 9908 | tree ret = error_mark_node; |
4de67c26 | 9909 | const char *invalid_op_diag; |
4d84fe7c | 9910 | bool op0_int_operands, op1_int_operands; |
928c19bb | 9911 | bool int_const, int_const_or_overflow, int_operands; |
b62acd60 | 9912 | |
3e4093b6 RS |
9913 | /* Expression code to give to the expression when it is built. |
9914 | Normally this is CODE, which is what the caller asked for, | |
9915 | but in some special cases we change it. */ | |
9916 | enum tree_code resultcode = code; | |
8b6a5902 | 9917 | |
3e4093b6 RS |
9918 | /* Data type in which the computation is to be performed. |
9919 | In the simplest cases this is the common type of the arguments. */ | |
9920 | tree result_type = NULL; | |
9921 | ||
8ce94e44 JM |
9922 | /* When the computation is in excess precision, the type of the |
9923 | final EXCESS_PRECISION_EXPR. */ | |
2d2e923f | 9924 | tree semantic_result_type = NULL; |
8ce94e44 | 9925 | |
3e4093b6 RS |
9926 | /* Nonzero means operands have already been type-converted |
9927 | in whatever way is necessary. | |
9928 | Zero means they need to be converted to RESULT_TYPE. */ | |
9929 | int converted = 0; | |
9930 | ||
9931 | /* Nonzero means create the expression with this type, rather than | |
9932 | RESULT_TYPE. */ | |
9933 | tree build_type = 0; | |
9934 | ||
9935 | /* Nonzero means after finally constructing the expression | |
9936 | convert it to this type. */ | |
9937 | tree final_type = 0; | |
9938 | ||
9939 | /* Nonzero if this is an operation like MIN or MAX which can | |
9940 | safely be computed in short if both args are promoted shorts. | |
9941 | Also implies COMMON. | |
9942 | -1 indicates a bitwise operation; this makes a difference | |
9943 | in the exact conditions for when it is safe to do the operation | |
9944 | in a narrower mode. */ | |
9945 | int shorten = 0; | |
9946 | ||
9947 | /* Nonzero if this is a comparison operation; | |
9948 | if both args are promoted shorts, compare the original shorts. | |
9949 | Also implies COMMON. */ | |
9950 | int short_compare = 0; | |
9951 | ||
9952 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
9953 | original short and then promoted if the operand is a promoted short. */ | |
9954 | int short_shift = 0; | |
9955 | ||
9956 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
9957 | int common = 0; | |
9958 | ||
58393038 ZL |
9959 | /* True means types are compatible as far as ObjC is concerned. */ |
9960 | bool objc_ok; | |
9961 | ||
8ce94e44 JM |
9962 | /* True means this is an arithmetic operation that may need excess |
9963 | precision. */ | |
9964 | bool may_need_excess_precision; | |
9965 | ||
180f8dbb JM |
9966 | /* True means this is a boolean operation that converts both its |
9967 | operands to truth-values. */ | |
9968 | bool boolean_op = false; | |
9969 | ||
de5a5fa1 MP |
9970 | /* Remember whether we're doing / or %. */ |
9971 | bool doing_div_or_mod = false; | |
9972 | ||
9973 | /* Remember whether we're doing << or >>. */ | |
9974 | bool doing_shift = false; | |
9975 | ||
9976 | /* Tree holding instrumentation expression. */ | |
9977 | tree instrument_expr = NULL; | |
9978 | ||
ba47d38d AH |
9979 | if (location == UNKNOWN_LOCATION) |
9980 | location = input_location; | |
9981 | ||
4d84fe7c JM |
9982 | op0 = orig_op0; |
9983 | op1 = orig_op1; | |
9984 | ||
9985 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
9986 | if (op0_int_operands) | |
9987 | op0 = remove_c_maybe_const_expr (op0); | |
9988 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
9989 | if (op1_int_operands) | |
9990 | op1 = remove_c_maybe_const_expr (op1); | |
9991 | int_operands = (op0_int_operands && op1_int_operands); | |
928c19bb JM |
9992 | if (int_operands) |
9993 | { | |
9994 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
9995 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
9996 | int_const = (int_const_or_overflow | |
9997 | && !TREE_OVERFLOW (orig_op0) | |
9998 | && !TREE_OVERFLOW (orig_op1)); | |
9999 | } | |
10000 | else | |
10001 | int_const = int_const_or_overflow = false; | |
10002 | ||
0e3a99ae | 10003 | /* Do not apply default conversion in mixed vector/scalar expression. */ |
f90e8e2e AS |
10004 | if (convert_p |
10005 | && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE) | |
0e3a99ae | 10006 | != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE))) |
790e9490 | 10007 | { |
4d84fe7c JM |
10008 | op0 = default_conversion (op0); |
10009 | op1 = default_conversion (op1); | |
790e9490 RS |
10010 | } |
10011 | ||
36536d79 BI |
10012 | /* When Cilk Plus is enabled and there are array notations inside op0, then |
10013 | we check to see if there are builtin array notation functions. If | |
10014 | so, then we take on the type of the array notation inside it. */ | |
10015 | if (flag_enable_cilkplus && contains_array_notation_expr (op0)) | |
10016 | orig_type0 = type0 = find_correct_array_notation_type (op0); | |
10017 | else | |
10018 | orig_type0 = type0 = TREE_TYPE (op0); | |
10019 | ||
10020 | if (flag_enable_cilkplus && contains_array_notation_expr (op1)) | |
10021 | orig_type1 = type1 = find_correct_array_notation_type (op1); | |
10022 | else | |
10023 | orig_type1 = type1 = TREE_TYPE (op1); | |
91fa3c30 | 10024 | |
3e4093b6 RS |
10025 | /* The expression codes of the data types of the arguments tell us |
10026 | whether the arguments are integers, floating, pointers, etc. */ | |
10027 | code0 = TREE_CODE (type0); | |
10028 | code1 = TREE_CODE (type1); | |
10029 | ||
10030 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
10031 | STRIP_TYPE_NOPS (op0); | |
10032 | STRIP_TYPE_NOPS (op1); | |
10033 | ||
10034 | /* If an error was already reported for one of the arguments, | |
10035 | avoid reporting another error. */ | |
10036 | ||
10037 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
10038 | return error_mark_node; | |
10039 | ||
4de67c26 JM |
10040 | if ((invalid_op_diag |
10041 | = targetm.invalid_binary_op (code, type0, type1))) | |
10042 | { | |
ba47d38d | 10043 | error_at (location, invalid_op_diag); |
4de67c26 JM |
10044 | return error_mark_node; |
10045 | } | |
10046 | ||
8ce94e44 JM |
10047 | switch (code) |
10048 | { | |
10049 | case PLUS_EXPR: | |
10050 | case MINUS_EXPR: | |
10051 | case MULT_EXPR: | |
10052 | case TRUNC_DIV_EXPR: | |
10053 | case CEIL_DIV_EXPR: | |
10054 | case FLOOR_DIV_EXPR: | |
10055 | case ROUND_DIV_EXPR: | |
10056 | case EXACT_DIV_EXPR: | |
10057 | may_need_excess_precision = true; | |
10058 | break; | |
10059 | default: | |
10060 | may_need_excess_precision = false; | |
10061 | break; | |
10062 | } | |
10063 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
10064 | { | |
10065 | op0 = TREE_OPERAND (op0, 0); | |
10066 | type0 = TREE_TYPE (op0); | |
10067 | } | |
10068 | else if (may_need_excess_precision | |
10069 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
10070 | { | |
10071 | type0 = eptype; | |
10072 | op0 = convert (eptype, op0); | |
10073 | } | |
10074 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
10075 | { | |
10076 | op1 = TREE_OPERAND (op1, 0); | |
10077 | type1 = TREE_TYPE (op1); | |
10078 | } | |
10079 | else if (may_need_excess_precision | |
10080 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
10081 | { | |
10082 | type1 = eptype; | |
10083 | op1 = convert (eptype, op1); | |
10084 | } | |
10085 | ||
58393038 ZL |
10086 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); |
10087 | ||
0e3a99ae AS |
10088 | /* In case when one of the operands of the binary operation is |
10089 | a vector and another is a scalar -- convert scalar to vector. */ | |
10090 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
10091 | { | |
a212e43f MG |
10092 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, |
10093 | true); | |
f90e8e2e | 10094 | |
0e3a99ae AS |
10095 | switch (convert_flag) |
10096 | { | |
10097 | case stv_error: | |
10098 | return error_mark_node; | |
10099 | case stv_firstarg: | |
10100 | { | |
10101 | bool maybe_const = true; | |
10102 | tree sc; | |
10103 | sc = c_fully_fold (op0, false, &maybe_const); | |
10104 | sc = save_expr (sc); | |
10105 | sc = convert (TREE_TYPE (type1), sc); | |
10106 | op0 = build_vector_from_val (type1, sc); | |
10107 | if (!maybe_const) | |
10108 | op0 = c_wrap_maybe_const (op0, true); | |
10109 | orig_type0 = type0 = TREE_TYPE (op0); | |
10110 | code0 = TREE_CODE (type0); | |
10111 | converted = 1; | |
10112 | break; | |
10113 | } | |
10114 | case stv_secondarg: | |
10115 | { | |
10116 | bool maybe_const = true; | |
10117 | tree sc; | |
10118 | sc = c_fully_fold (op1, false, &maybe_const); | |
10119 | sc = save_expr (sc); | |
10120 | sc = convert (TREE_TYPE (type0), sc); | |
10121 | op1 = build_vector_from_val (type0, sc); | |
10122 | if (!maybe_const) | |
54b9f838 | 10123 | op1 = c_wrap_maybe_const (op1, true); |
0e3a99ae AS |
10124 | orig_type1 = type1 = TREE_TYPE (op1); |
10125 | code1 = TREE_CODE (type1); | |
10126 | converted = 1; | |
10127 | break; | |
10128 | } | |
10129 | default: | |
10130 | break; | |
10131 | } | |
10132 | } | |
10133 | ||
3e4093b6 | 10134 | switch (code) |
de520661 | 10135 | { |
3e4093b6 RS |
10136 | case PLUS_EXPR: |
10137 | /* Handle the pointer + int case. */ | |
10138 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 10139 | { |
db3927fb | 10140 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); |
c9f9eb5d AH |
10141 | goto return_build_binary_op; |
10142 | } | |
3e4093b6 | 10143 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) |
c9f9eb5d | 10144 | { |
db3927fb | 10145 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); |
c9f9eb5d AH |
10146 | goto return_build_binary_op; |
10147 | } | |
fe67cf58 | 10148 | else |
3e4093b6 RS |
10149 | common = 1; |
10150 | break; | |
400fbf9f | 10151 | |
3e4093b6 RS |
10152 | case MINUS_EXPR: |
10153 | /* Subtraction of two similar pointers. | |
10154 | We must subtract them as integers, then divide by object size. */ | |
10155 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
744aa42f | 10156 | && comp_target_types (location, type0, type1)) |
c9f9eb5d | 10157 | { |
db3927fb | 10158 | ret = pointer_diff (location, op0, op1); |
c9f9eb5d AH |
10159 | goto return_build_binary_op; |
10160 | } | |
3e4093b6 RS |
10161 | /* Handle pointer minus int. Just like pointer plus int. */ |
10162 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 10163 | { |
db3927fb | 10164 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); |
c9f9eb5d AH |
10165 | goto return_build_binary_op; |
10166 | } | |
3e4093b6 RS |
10167 | else |
10168 | common = 1; | |
10169 | break; | |
8b6a5902 | 10170 | |
3e4093b6 RS |
10171 | case MULT_EXPR: |
10172 | common = 1; | |
10173 | break; | |
10174 | ||
10175 | case TRUNC_DIV_EXPR: | |
10176 | case CEIL_DIV_EXPR: | |
10177 | case FLOOR_DIV_EXPR: | |
10178 | case ROUND_DIV_EXPR: | |
10179 | case EXACT_DIV_EXPR: | |
de5a5fa1 | 10180 | doing_div_or_mod = true; |
c9f9eb5d | 10181 | warn_for_div_by_zero (location, op1); |
3e4093b6 RS |
10182 | |
10183 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 10184 | || code0 == FIXED_POINT_TYPE |
3e4093b6 RS |
10185 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
10186 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
ab22c1fa | 10187 | || code1 == FIXED_POINT_TYPE |
3e4093b6 | 10188 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 10189 | { |
5bed876a AH |
10190 | enum tree_code tcode0 = code0, tcode1 = code1; |
10191 | ||
3a021db2 | 10192 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
5bed876a | 10193 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
3a021db2 | 10194 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) |
5bed876a | 10195 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
3a021db2 | 10196 | |
ab22c1fa CF |
10197 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) |
10198 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
3e4093b6 RS |
10199 | resultcode = RDIV_EXPR; |
10200 | else | |
10201 | /* Although it would be tempting to shorten always here, that | |
10202 | loses on some targets, since the modulo instruction is | |
10203 | undefined if the quotient can't be represented in the | |
10204 | computation mode. We shorten only if unsigned or if | |
10205 | dividing by something we know != -1. */ | |
8df83eae | 10206 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 10207 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 10208 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
10209 | common = 1; |
10210 | } | |
10211 | break; | |
de520661 | 10212 | |
3e4093b6 | 10213 | case BIT_AND_EXPR: |
3e4093b6 RS |
10214 | case BIT_IOR_EXPR: |
10215 | case BIT_XOR_EXPR: | |
10216 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
10217 | shorten = -1; | |
9ef0c8d9 AP |
10218 | /* Allow vector types which are not floating point types. */ |
10219 | else if (code0 == VECTOR_TYPE | |
10220 | && code1 == VECTOR_TYPE | |
10221 | && !VECTOR_FLOAT_TYPE_P (type0) | |
10222 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
3e4093b6 RS |
10223 | common = 1; |
10224 | break; | |
10225 | ||
10226 | case TRUNC_MOD_EXPR: | |
10227 | case FLOOR_MOD_EXPR: | |
de5a5fa1 | 10228 | doing_div_or_mod = true; |
c9f9eb5d | 10229 | warn_for_div_by_zero (location, op1); |
de520661 | 10230 | |
5cfd5d9b AP |
10231 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
10232 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
10233 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
10234 | common = 1; | |
10235 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3e4093b6 RS |
10236 | { |
10237 | /* Although it would be tempting to shorten always here, that loses | |
10238 | on some targets, since the modulo instruction is undefined if the | |
10239 | quotient can't be represented in the computation mode. We shorten | |
10240 | only if unsigned or if dividing by something we know != -1. */ | |
8df83eae | 10241 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 10242 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 10243 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
10244 | common = 1; |
10245 | } | |
10246 | break; | |
de520661 | 10247 | |
3e4093b6 RS |
10248 | case TRUTH_ANDIF_EXPR: |
10249 | case TRUTH_ORIF_EXPR: | |
10250 | case TRUTH_AND_EXPR: | |
10251 | case TRUTH_OR_EXPR: | |
10252 | case TRUTH_XOR_EXPR: | |
10253 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
ab22c1fa CF |
10254 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
10255 | || code0 == FIXED_POINT_TYPE) | |
3e4093b6 | 10256 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE |
ab22c1fa CF |
10257 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE |
10258 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10259 | { |
10260 | /* Result of these operations is always an int, | |
10261 | but that does not mean the operands should be | |
10262 | converted to ints! */ | |
10263 | result_type = integer_type_node; | |
a27d595d JM |
10264 | if (op0_int_operands) |
10265 | { | |
10266 | op0 = c_objc_common_truthvalue_conversion (location, orig_op0); | |
10267 | op0 = remove_c_maybe_const_expr (op0); | |
10268 | } | |
10269 | else | |
10270 | op0 = c_objc_common_truthvalue_conversion (location, op0); | |
10271 | if (op1_int_operands) | |
10272 | { | |
10273 | op1 = c_objc_common_truthvalue_conversion (location, orig_op1); | |
10274 | op1 = remove_c_maybe_const_expr (op1); | |
10275 | } | |
10276 | else | |
10277 | op1 = c_objc_common_truthvalue_conversion (location, op1); | |
3e4093b6 | 10278 | converted = 1; |
180f8dbb | 10279 | boolean_op = true; |
3e4093b6 | 10280 | } |
928c19bb JM |
10281 | if (code == TRUTH_ANDIF_EXPR) |
10282 | { | |
10283 | int_const_or_overflow = (int_operands | |
10284 | && TREE_CODE (orig_op0) == INTEGER_CST | |
10285 | && (op0 == truthvalue_false_node | |
10286 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
10287 | int_const = (int_const_or_overflow | |
10288 | && !TREE_OVERFLOW (orig_op0) | |
10289 | && (op0 == truthvalue_false_node | |
10290 | || !TREE_OVERFLOW (orig_op1))); | |
10291 | } | |
10292 | else if (code == TRUTH_ORIF_EXPR) | |
10293 | { | |
10294 | int_const_or_overflow = (int_operands | |
10295 | && TREE_CODE (orig_op0) == INTEGER_CST | |
10296 | && (op0 == truthvalue_true_node | |
10297 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
10298 | int_const = (int_const_or_overflow | |
10299 | && !TREE_OVERFLOW (orig_op0) | |
10300 | && (op0 == truthvalue_true_node | |
10301 | || !TREE_OVERFLOW (orig_op1))); | |
10302 | } | |
3e4093b6 | 10303 | break; |
eba80994 | 10304 | |
3e4093b6 RS |
10305 | /* Shift operations: result has same type as first operand; |
10306 | always convert second operand to int. | |
10307 | Also set SHORT_SHIFT if shifting rightward. */ | |
de520661 | 10308 | |
3e4093b6 | 10309 | case RSHIFT_EXPR: |
f87bd04b AS |
10310 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
10311 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
10312 | { | |
10313 | result_type = type0; | |
10314 | converted = 1; | |
10315 | } | |
10316 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
10317 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
10318 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
10319 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
10320 | { | |
10321 | result_type = type0; | |
10322 | converted = 1; | |
10323 | } | |
10324 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
ab22c1fa | 10325 | && code1 == INTEGER_TYPE) |
3e4093b6 | 10326 | { |
de5a5fa1 | 10327 | doing_shift = true; |
928c19bb | 10328 | if (TREE_CODE (op1) == INTEGER_CST) |
b62acd60 | 10329 | { |
3e4093b6 | 10330 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
10331 | { |
10332 | int_const = false; | |
7d882b83 | 10333 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
10334 | warning (0, "right shift count is negative"); |
10335 | } | |
3e4093b6 | 10336 | else |
bbb818c6 | 10337 | { |
3f75a254 | 10338 | if (!integer_zerop (op1)) |
3e4093b6 RS |
10339 | short_shift = 1; |
10340 | ||
10341 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
928c19bb JM |
10342 | { |
10343 | int_const = false; | |
7d882b83 | 10344 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
10345 | warning (0, "right shift count >= width of type"); |
10346 | } | |
bbb818c6 | 10347 | } |
b62acd60 | 10348 | } |
de520661 | 10349 | |
3e4093b6 RS |
10350 | /* Use the type of the value to be shifted. */ |
10351 | result_type = type0; | |
f87bd04b AS |
10352 | /* Convert the non vector shift-count to an integer, regardless |
10353 | of size of value being shifted. */ | |
10354 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
10355 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
3e4093b6 RS |
10356 | op1 = convert (integer_type_node, op1); |
10357 | /* Avoid converting op1 to result_type later. */ | |
10358 | converted = 1; | |
400fbf9f | 10359 | } |
3e4093b6 | 10360 | break; |
253b6b82 | 10361 | |
3e4093b6 | 10362 | case LSHIFT_EXPR: |
f87bd04b AS |
10363 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
10364 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
10365 | { | |
10366 | result_type = type0; | |
10367 | converted = 1; | |
10368 | } | |
10369 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
10370 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
10371 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
10372 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
10373 | { | |
10374 | result_type = type0; | |
10375 | converted = 1; | |
10376 | } | |
10377 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
ab22c1fa | 10378 | && code1 == INTEGER_TYPE) |
3e4093b6 | 10379 | { |
de5a5fa1 | 10380 | doing_shift = true; |
928c19bb | 10381 | if (TREE_CODE (op1) == INTEGER_CST) |
de520661 | 10382 | { |
3e4093b6 | 10383 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
10384 | { |
10385 | int_const = false; | |
7d882b83 | 10386 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
10387 | warning (0, "left shift count is negative"); |
10388 | } | |
de520661 | 10389 | |
3e4093b6 | 10390 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
928c19bb JM |
10391 | { |
10392 | int_const = false; | |
7d882b83 | 10393 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
10394 | warning (0, "left shift count >= width of type"); |
10395 | } | |
94ba5069 | 10396 | } |
de520661 | 10397 | |
3e4093b6 RS |
10398 | /* Use the type of the value to be shifted. */ |
10399 | result_type = type0; | |
f87bd04b AS |
10400 | /* Convert the non vector shift-count to an integer, regardless |
10401 | of size of value being shifted. */ | |
10402 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
10403 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
3e4093b6 RS |
10404 | op1 = convert (integer_type_node, op1); |
10405 | /* Avoid converting op1 to result_type later. */ | |
10406 | converted = 1; | |
400fbf9f | 10407 | } |
3e4093b6 | 10408 | break; |
de520661 | 10409 | |
3e4093b6 RS |
10410 | case EQ_EXPR: |
10411 | case NE_EXPR: | |
d246ab4f AS |
10412 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10413 | { | |
10414 | tree intt; | |
0af94e6f | 10415 | if (!vector_types_compatible_elements_p (type0, type1)) |
d246ab4f AS |
10416 | { |
10417 | error_at (location, "comparing vectors with different " | |
10418 | "element types"); | |
10419 | return error_mark_node; | |
10420 | } | |
10421 | ||
10422 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10423 | { | |
10424 | error_at (location, "comparing vectors with different " | |
10425 | "number of elements"); | |
10426 | return error_mark_node; | |
10427 | } | |
10428 | ||
10429 | /* Always construct signed integer vector type. */ | |
10430 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10431 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10432 | result_type = build_opaque_vector_type (intt, | |
10433 | TYPE_VECTOR_SUBPARTS (type0)); | |
10434 | converted = 1; | |
10435 | break; | |
10436 | } | |
ae311566 | 10437 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) |
ba47d38d AH |
10438 | warning_at (location, |
10439 | OPT_Wfloat_equal, | |
10440 | "comparing floating point with == or != is unsafe"); | |
3e4093b6 RS |
10441 | /* Result of comparison is always int, |
10442 | but don't convert the args to int! */ | |
10443 | build_type = integer_type_node; | |
10444 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 10445 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) |
3e4093b6 | 10446 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
ab22c1fa | 10447 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) |
3e4093b6 | 10448 | short_compare = 1; |
637f1455 SZ |
10449 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
10450 | { | |
10451 | if (TREE_CODE (op0) == ADDR_EXPR | |
10452 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) | |
10453 | { | |
10454 | if (code == EQ_EXPR) | |
10455 | warning_at (location, | |
10456 | OPT_Waddress, | |
10457 | "the comparison will always evaluate as %<false%> " | |
10458 | "for the address of %qD will never be NULL", | |
10459 | TREE_OPERAND (op0, 0)); | |
10460 | else | |
10461 | warning_at (location, | |
10462 | OPT_Waddress, | |
10463 | "the comparison will always evaluate as %<true%> " | |
10464 | "for the address of %qD will never be NULL", | |
10465 | TREE_OPERAND (op0, 0)); | |
10466 | } | |
10467 | result_type = type0; | |
10468 | } | |
10469 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) | |
10470 | { | |
10471 | if (TREE_CODE (op1) == ADDR_EXPR | |
10472 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) | |
10473 | { | |
10474 | if (code == EQ_EXPR) | |
10475 | warning_at (location, | |
f90e8e2e | 10476 | OPT_Waddress, |
637f1455 SZ |
10477 | "the comparison will always evaluate as %<false%> " |
10478 | "for the address of %qD will never be NULL", | |
10479 | TREE_OPERAND (op1, 0)); | |
10480 | else | |
10481 | warning_at (location, | |
10482 | OPT_Waddress, | |
10483 | "the comparison will always evaluate as %<true%> " | |
10484 | "for the address of %qD will never be NULL", | |
10485 | TREE_OPERAND (op1, 0)); | |
10486 | } | |
10487 | result_type = type1; | |
10488 | } | |
3e4093b6 RS |
10489 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
10490 | { | |
10491 | tree tt0 = TREE_TYPE (type0); | |
10492 | tree tt1 = TREE_TYPE (type1); | |
36c5e70a BE |
10493 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); |
10494 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
10495 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
10496 | ||
3e4093b6 RS |
10497 | /* Anything compares with void *. void * compares with anything. |
10498 | Otherwise, the targets must be compatible | |
10499 | and both must be object or both incomplete. */ | |
744aa42f | 10500 | if (comp_target_types (location, type0, type1)) |
10bc1b1b | 10501 | result_type = common_pointer_type (type0, type1); |
36c5e70a BE |
10502 | else if (!addr_space_superset (as0, as1, &as_common)) |
10503 | { | |
10504 | error_at (location, "comparison of pointers to " | |
10505 | "disjoint address spaces"); | |
10506 | return error_mark_node; | |
10507 | } | |
267bac10 | 10508 | else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0)) |
ee2990e7 | 10509 | { |
36c5e70a | 10510 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) |
c1771a20 | 10511 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10512 | "comparison of %<void *%> with function pointer"); |
ee2990e7 | 10513 | } |
267bac10 | 10514 | else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1)) |
e6834654 | 10515 | { |
36c5e70a | 10516 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) |
c1771a20 | 10517 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10518 | "comparison of %<void *%> with function pointer"); |
e6834654 | 10519 | } |
3e4093b6 | 10520 | else |
58393038 ZL |
10521 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
10522 | if (!objc_ok) | |
ba47d38d | 10523 | pedwarn (location, 0, |
509c9d60 | 10524 | "comparison of distinct pointer types lacks a cast"); |
e6834654 | 10525 | |
3e4093b6 | 10526 | if (result_type == NULL_TREE) |
36c5e70a BE |
10527 | { |
10528 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
10529 | result_type = build_pointer_type | |
10530 | (build_qualified_type (void_type_node, qual)); | |
10531 | } | |
e6834654 | 10532 | } |
3e4093b6 | 10533 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
de520661 | 10534 | { |
3e4093b6 | 10535 | result_type = type0; |
ba47d38d | 10536 | pedwarn (location, 0, "comparison between pointer and integer"); |
de520661 | 10537 | } |
3e4093b6 | 10538 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
8b6a5902 | 10539 | { |
3e4093b6 | 10540 | result_type = type1; |
ba47d38d | 10541 | pedwarn (location, 0, "comparison between pointer and integer"); |
8b6a5902 | 10542 | } |
3e4093b6 | 10543 | break; |
8b6a5902 | 10544 | |
3e4093b6 RS |
10545 | case LE_EXPR: |
10546 | case GE_EXPR: | |
10547 | case LT_EXPR: | |
10548 | case GT_EXPR: | |
d246ab4f AS |
10549 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10550 | { | |
10551 | tree intt; | |
0af94e6f | 10552 | if (!vector_types_compatible_elements_p (type0, type1)) |
d246ab4f AS |
10553 | { |
10554 | error_at (location, "comparing vectors with different " | |
10555 | "element types"); | |
10556 | return error_mark_node; | |
10557 | } | |
10558 | ||
10559 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10560 | { | |
10561 | error_at (location, "comparing vectors with different " | |
10562 | "number of elements"); | |
10563 | return error_mark_node; | |
10564 | } | |
10565 | ||
10566 | /* Always construct signed integer vector type. */ | |
10567 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10568 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10569 | result_type = build_opaque_vector_type (intt, | |
10570 | TYPE_VECTOR_SUBPARTS (type0)); | |
10571 | converted = 1; | |
10572 | break; | |
10573 | } | |
3e4093b6 | 10574 | build_type = integer_type_node; |
ab22c1fa CF |
10575 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
10576 | || code0 == FIXED_POINT_TYPE) | |
10577 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
10578 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10579 | short_compare = 1; |
10580 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
10581 | { | |
36c5e70a BE |
10582 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); |
10583 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
10584 | addr_space_t as_common; | |
10585 | ||
744aa42f | 10586 | if (comp_target_types (location, type0, type1)) |
3e4093b6 | 10587 | { |
10bc1b1b | 10588 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
10589 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
10590 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
ba47d38d | 10591 | pedwarn (location, 0, |
509c9d60 | 10592 | "comparison of complete and incomplete pointers"); |
fcf73884 | 10593 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) |
c1771a20 | 10594 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10595 | "ordered comparisons of pointers to functions"); |
d42ba3b8 SZ |
10596 | else if (null_pointer_constant_p (orig_op0) |
10597 | || null_pointer_constant_p (orig_op1)) | |
10598 | warning_at (location, OPT_Wextra, | |
10599 | "ordered comparison of pointer with null pointer"); | |
10600 | ||
3e4093b6 | 10601 | } |
36c5e70a BE |
10602 | else if (!addr_space_superset (as0, as1, &as_common)) |
10603 | { | |
10604 | error_at (location, "comparison of pointers to " | |
10605 | "disjoint address spaces"); | |
10606 | return error_mark_node; | |
10607 | } | |
3e4093b6 RS |
10608 | else |
10609 | { | |
36c5e70a BE |
10610 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
10611 | result_type = build_pointer_type | |
10612 | (build_qualified_type (void_type_node, qual)); | |
ba47d38d | 10613 | pedwarn (location, 0, |
509c9d60 | 10614 | "comparison of distinct pointer types lacks a cast"); |
3e4093b6 RS |
10615 | } |
10616 | } | |
6aa3c60d | 10617 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
3e4093b6 RS |
10618 | { |
10619 | result_type = type0; | |
fcf73884 | 10620 | if (pedantic) |
c1771a20 | 10621 | pedwarn (location, OPT_Wpedantic, |
fcf73884 MLI |
10622 | "ordered comparison of pointer with integer zero"); |
10623 | else if (extra_warnings) | |
ba47d38d | 10624 | warning_at (location, OPT_Wextra, |
d42ba3b8 | 10625 | "ordered comparison of pointer with integer zero"); |
3e4093b6 | 10626 | } |
6aa3c60d | 10627 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
3e4093b6 RS |
10628 | { |
10629 | result_type = type1; | |
d42ba3b8 | 10630 | if (pedantic) |
c1771a20 | 10631 | pedwarn (location, OPT_Wpedantic, |
d42ba3b8 SZ |
10632 | "ordered comparison of pointer with integer zero"); |
10633 | else if (extra_warnings) | |
10634 | warning_at (location, OPT_Wextra, | |
10635 | "ordered comparison of pointer with integer zero"); | |
3e4093b6 RS |
10636 | } |
10637 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
10638 | { | |
10639 | result_type = type0; | |
ba47d38d | 10640 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
10641 | } |
10642 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
10643 | { | |
10644 | result_type = type1; | |
ba47d38d | 10645 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
10646 | } |
10647 | break; | |
64094f6a | 10648 | |
3e4093b6 | 10649 | default: |
37b2f290 | 10650 | gcc_unreachable (); |
c9fe6f9f | 10651 | } |
8f17b5c5 | 10652 | |
e57e265b PB |
10653 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
10654 | return error_mark_node; | |
10655 | ||
5bed876a AH |
10656 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
10657 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
0af94e6f | 10658 | || !vector_types_compatible_elements_p (type0, type1))) |
5bed876a | 10659 | { |
ba47d38d | 10660 | binary_op_error (location, code, type0, type1); |
5bed876a AH |
10661 | return error_mark_node; |
10662 | } | |
10663 | ||
3e4093b6 | 10664 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
ab22c1fa | 10665 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) |
3e4093b6 RS |
10666 | && |
10667 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
ab22c1fa | 10668 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 10669 | { |
2ca862e9 JM |
10670 | bool first_complex = (code0 == COMPLEX_TYPE); |
10671 | bool second_complex = (code1 == COMPLEX_TYPE); | |
10672 | int none_complex = (!first_complex && !second_complex); | |
39b726dd | 10673 | |
3e4093b6 | 10674 | if (shorten || common || short_compare) |
3bf6bfcc JJ |
10675 | { |
10676 | result_type = c_common_type (type0, type1); | |
0a0b3574 MM |
10677 | do_warn_double_promotion (result_type, type0, type1, |
10678 | "implicit conversion from %qT to %qT " | |
10679 | "to match other operand of binary " | |
10680 | "expression", | |
10681 | location); | |
3bf6bfcc JJ |
10682 | if (result_type == error_mark_node) |
10683 | return error_mark_node; | |
10684 | } | |
400fbf9f | 10685 | |
2ca862e9 JM |
10686 | if (first_complex != second_complex |
10687 | && (code == PLUS_EXPR | |
10688 | || code == MINUS_EXPR | |
10689 | || code == MULT_EXPR | |
10690 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
10691 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
10692 | && flag_signed_zeros) | |
10693 | { | |
10694 | /* An operation on mixed real/complex operands must be | |
10695 | handled specially, but the language-independent code can | |
10696 | more easily optimize the plain complex arithmetic if | |
10697 | -fno-signed-zeros. */ | |
10698 | tree real_type = TREE_TYPE (result_type); | |
10699 | tree real, imag; | |
10700 | if (type0 != orig_type0 || type1 != orig_type1) | |
10701 | { | |
10702 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 10703 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
2ca862e9 JM |
10704 | } |
10705 | if (first_complex) | |
10706 | { | |
10707 | if (TREE_TYPE (op0) != result_type) | |
10708 | op0 = convert_and_check (result_type, op0); | |
10709 | if (TREE_TYPE (op1) != real_type) | |
10710 | op1 = convert_and_check (real_type, op1); | |
10711 | } | |
10712 | else | |
10713 | { | |
10714 | if (TREE_TYPE (op0) != real_type) | |
10715 | op0 = convert_and_check (real_type, op0); | |
10716 | if (TREE_TYPE (op1) != result_type) | |
10717 | op1 = convert_and_check (result_type, op1); | |
10718 | } | |
10719 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10720 | return error_mark_node; | |
10721 | if (first_complex) | |
10722 | { | |
10723 | op0 = c_save_expr (op0); | |
10724 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
10725 | op0, 1); | |
10726 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
10727 | op0, 1); | |
10728 | switch (code) | |
10729 | { | |
10730 | case MULT_EXPR: | |
10731 | case TRUNC_DIV_EXPR: | |
c4603e7c | 10732 | op1 = c_save_expr (op1); |
2ca862e9 JM |
10733 | imag = build2 (resultcode, real_type, imag, op1); |
10734 | /* Fall through. */ | |
10735 | case PLUS_EXPR: | |
10736 | case MINUS_EXPR: | |
10737 | real = build2 (resultcode, real_type, real, op1); | |
10738 | break; | |
10739 | default: | |
10740 | gcc_unreachable(); | |
10741 | } | |
10742 | } | |
10743 | else | |
10744 | { | |
10745 | op1 = c_save_expr (op1); | |
10746 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
10747 | op1, 1); | |
10748 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
10749 | op1, 1); | |
10750 | switch (code) | |
10751 | { | |
10752 | case MULT_EXPR: | |
c4603e7c | 10753 | op0 = c_save_expr (op0); |
2ca862e9 JM |
10754 | imag = build2 (resultcode, real_type, op0, imag); |
10755 | /* Fall through. */ | |
10756 | case PLUS_EXPR: | |
10757 | real = build2 (resultcode, real_type, op0, real); | |
10758 | break; | |
10759 | case MINUS_EXPR: | |
10760 | real = build2 (resultcode, real_type, op0, real); | |
10761 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
10762 | break; | |
10763 | default: | |
10764 | gcc_unreachable(); | |
10765 | } | |
10766 | } | |
10767 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
10768 | goto return_build_binary_op; | |
10769 | } | |
10770 | ||
3e4093b6 RS |
10771 | /* For certain operations (which identify themselves by shorten != 0) |
10772 | if both args were extended from the same smaller type, | |
10773 | do the arithmetic in that type and then extend. | |
400fbf9f | 10774 | |
3e4093b6 RS |
10775 | shorten !=0 and !=1 indicates a bitwise operation. |
10776 | For them, this optimization is safe only if | |
10777 | both args are zero-extended or both are sign-extended. | |
10778 | Otherwise, we might change the result. | |
10779 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
10780 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
400fbf9f | 10781 | |
3e4093b6 RS |
10782 | if (shorten && none_complex) |
10783 | { | |
3e4093b6 | 10784 | final_type = result_type; |
b8698a0f | 10785 | result_type = shorten_binary_op (result_type, op0, op1, |
6715192c | 10786 | shorten == -1); |
3e4093b6 | 10787 | } |
88a3dbc1 | 10788 | |
3e4093b6 | 10789 | /* Shifts can be shortened if shifting right. */ |
2f6e4e97 | 10790 | |
3e4093b6 RS |
10791 | if (short_shift) |
10792 | { | |
10793 | int unsigned_arg; | |
10794 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
88a3dbc1 | 10795 | |
3e4093b6 | 10796 | final_type = result_type; |
abe80e6d | 10797 | |
3e4093b6 | 10798 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
8df83eae | 10799 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
e9a25f70 | 10800 | |
3e4093b6 | 10801 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
be123439 | 10802 | && tree_int_cst_sgn (op1) > 0 |
3e4093b6 RS |
10803 | /* We can shorten only if the shift count is less than the |
10804 | number of bits in the smaller type size. */ | |
10805 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
10806 | /* We cannot drop an unsigned shift after sign-extension. */ | |
8df83eae | 10807 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
3e4093b6 RS |
10808 | { |
10809 | /* Do an unsigned shift if the operand was zero-extended. */ | |
10810 | result_type | |
10811 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
10812 | TREE_TYPE (arg0)); | |
10813 | /* Convert value-to-be-shifted to that type. */ | |
10814 | if (TREE_TYPE (op0) != result_type) | |
10815 | op0 = convert (result_type, op0); | |
10816 | converted = 1; | |
abe80e6d | 10817 | } |
88a3dbc1 RK |
10818 | } |
10819 | ||
3e4093b6 RS |
10820 | /* Comparison operations are shortened too but differently. |
10821 | They identify themselves by setting short_compare = 1. */ | |
56cb9733 | 10822 | |
3e4093b6 RS |
10823 | if (short_compare) |
10824 | { | |
10825 | /* Don't write &op0, etc., because that would prevent op0 | |
10826 | from being kept in a register. | |
10827 | Instead, make copies of the our local variables and | |
10828 | pass the copies by reference, then copy them back afterward. */ | |
10829 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
10830 | enum tree_code xresultcode = resultcode; | |
10831 | tree val | |
10832 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
8f17b5c5 | 10833 | |
3e4093b6 | 10834 | if (val != 0) |
c9f9eb5d AH |
10835 | { |
10836 | ret = val; | |
10837 | goto return_build_binary_op; | |
10838 | } | |
8f17b5c5 | 10839 | |
3e4093b6 RS |
10840 | op0 = xop0, op1 = xop1; |
10841 | converted = 1; | |
10842 | resultcode = xresultcode; | |
8f17b5c5 | 10843 | |
7d882b83 | 10844 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
10845 | { |
10846 | bool op0_maybe_const = true; | |
10847 | bool op1_maybe_const = true; | |
10848 | tree orig_op0_folded, orig_op1_folded; | |
10849 | ||
10850 | if (in_late_binary_op) | |
10851 | { | |
10852 | orig_op0_folded = orig_op0; | |
10853 | orig_op1_folded = orig_op1; | |
10854 | } | |
10855 | else | |
10856 | { | |
10857 | /* Fold for the sake of possible warnings, as in | |
10858 | build_conditional_expr. This requires the | |
10859 | "original" values to be folded, not just op0 and | |
10860 | op1. */ | |
f5178456 | 10861 | c_inhibit_evaluation_warnings++; |
928c19bb JM |
10862 | op0 = c_fully_fold (op0, require_constant_value, |
10863 | &op0_maybe_const); | |
10864 | op1 = c_fully_fold (op1, require_constant_value, | |
10865 | &op1_maybe_const); | |
f5178456 | 10866 | c_inhibit_evaluation_warnings--; |
928c19bb JM |
10867 | orig_op0_folded = c_fully_fold (orig_op0, |
10868 | require_constant_value, | |
10869 | NULL); | |
10870 | orig_op1_folded = c_fully_fold (orig_op1, | |
10871 | require_constant_value, | |
10872 | NULL); | |
10873 | } | |
10874 | ||
10875 | if (warn_sign_compare) | |
10876 | warn_for_sign_compare (location, orig_op0_folded, | |
10877 | orig_op1_folded, op0, op1, | |
10878 | result_type, resultcode); | |
5c2f94b4 | 10879 | if (!in_late_binary_op && !int_operands) |
928c19bb JM |
10880 | { |
10881 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
e5a94231 | 10882 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); |
928c19bb | 10883 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) |
e5a94231 | 10884 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 10885 | } |
3e4093b6 | 10886 | } |
2ad1815d | 10887 | } |
64094f6a | 10888 | } |
64094f6a | 10889 | |
3e4093b6 RS |
10890 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
10891 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
10892 | Then the expression will be built. | |
10893 | It will be given type FINAL_TYPE if that is nonzero; | |
10894 | otherwise, it will be given type RESULT_TYPE. */ | |
400fbf9f | 10895 | |
3e4093b6 RS |
10896 | if (!result_type) |
10897 | { | |
ba47d38d | 10898 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); |
3e4093b6 RS |
10899 | return error_mark_node; |
10900 | } | |
400fbf9f | 10901 | |
3e4093b6 | 10902 | if (build_type == NULL_TREE) |
8ce94e44 JM |
10903 | { |
10904 | build_type = result_type; | |
180f8dbb JM |
10905 | if ((type0 != orig_type0 || type1 != orig_type1) |
10906 | && !boolean_op) | |
8ce94e44 JM |
10907 | { |
10908 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 10909 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
8ce94e44 JM |
10910 | } |
10911 | } | |
400fbf9f | 10912 | |
2d2e923f MLI |
10913 | if (!converted) |
10914 | { | |
10915 | op0 = ep_convert_and_check (result_type, op0, semantic_result_type); | |
10916 | op1 = ep_convert_and_check (result_type, op1, semantic_result_type); | |
10917 | ||
10918 | /* This can happen if one operand has a vector type, and the other | |
10919 | has a different type. */ | |
10920 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10921 | return error_mark_node; | |
10922 | } | |
10923 | ||
a24d975c | 10924 | if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE)) |
de5a5fa1 | 10925 | && current_function_decl != 0 |
ce6923c5 MP |
10926 | && !lookup_attribute ("no_sanitize_undefined", |
10927 | DECL_ATTRIBUTES (current_function_decl)) | |
de5a5fa1 MP |
10928 | && (doing_div_or_mod || doing_shift)) |
10929 | { | |
10930 | /* OP0 and/or OP1 might have side-effects. */ | |
10931 | op0 = c_save_expr (op0); | |
10932 | op1 = c_save_expr (op1); | |
10933 | op0 = c_fully_fold (op0, false, NULL); | |
10934 | op1 = c_fully_fold (op1, false, NULL); | |
a24d975c | 10935 | if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE)) |
de5a5fa1 | 10936 | instrument_expr = ubsan_instrument_division (location, op0, op1); |
a24d975c | 10937 | else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT)) |
de5a5fa1 MP |
10938 | instrument_expr = ubsan_instrument_shift (location, code, op0, op1); |
10939 | } | |
10940 | ||
c9f9eb5d | 10941 | /* Treat expressions in initializers specially as they can't trap. */ |
928c19bb JM |
10942 | if (int_const_or_overflow) |
10943 | ret = (require_constant_value | |
db3927fb AH |
10944 | ? fold_build2_initializer_loc (location, resultcode, build_type, |
10945 | op0, op1) | |
10946 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
928c19bb JM |
10947 | else |
10948 | ret = build2 (resultcode, build_type, op0, op1); | |
c9f9eb5d AH |
10949 | if (final_type != 0) |
10950 | ret = convert (final_type, ret); | |
10951 | ||
10952 | return_build_binary_op: | |
10953 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
10954 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) |
10955 | ret = (int_operands | |
10956 | ? note_integer_operands (ret) | |
10957 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
10958 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
10959 | && !in_late_binary_op) | |
10960 | ret = note_integer_operands (ret); | |
2d2e923f MLI |
10961 | if (semantic_result_type) |
10962 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
c9f9eb5d | 10963 | protected_set_expr_location (ret, location); |
de5a5fa1 | 10964 | |
a24d975c | 10965 | if (instrument_expr != NULL) |
de5a5fa1 MP |
10966 | ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret), |
10967 | instrument_expr, ret); | |
10968 | ||
c9f9eb5d | 10969 | return ret; |
400fbf9f | 10970 | } |
85498824 JM |
10971 | |
10972 | ||
10973 | /* Convert EXPR to be a truth-value, validating its type for this | |
ba47d38d | 10974 | purpose. LOCATION is the source location for the expression. */ |
85498824 JM |
10975 | |
10976 | tree | |
ba47d38d | 10977 | c_objc_common_truthvalue_conversion (location_t location, tree expr) |
85498824 | 10978 | { |
928c19bb JM |
10979 | bool int_const, int_operands; |
10980 | ||
85498824 JM |
10981 | switch (TREE_CODE (TREE_TYPE (expr))) |
10982 | { | |
10983 | case ARRAY_TYPE: | |
ba47d38d | 10984 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); |
85498824 JM |
10985 | return error_mark_node; |
10986 | ||
10987 | case RECORD_TYPE: | |
ba47d38d | 10988 | error_at (location, "used struct type value where scalar is required"); |
85498824 JM |
10989 | return error_mark_node; |
10990 | ||
10991 | case UNION_TYPE: | |
ba47d38d | 10992 | error_at (location, "used union type value where scalar is required"); |
85498824 JM |
10993 | return error_mark_node; |
10994 | ||
04af8788 NP |
10995 | case VOID_TYPE: |
10996 | error_at (location, "void value not ignored as it ought to be"); | |
10997 | return error_mark_node; | |
10998 | ||
46bdb9cf JM |
10999 | case FUNCTION_TYPE: |
11000 | gcc_unreachable (); | |
11001 | ||
d246ab4f AS |
11002 | case VECTOR_TYPE: |
11003 | error_at (location, "used vector type where scalar is required"); | |
11004 | return error_mark_node; | |
11005 | ||
85498824 JM |
11006 | default: |
11007 | break; | |
11008 | } | |
11009 | ||
928c19bb JM |
11010 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); |
11011 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
a27d595d JM |
11012 | if (int_operands && TREE_CODE (expr) != INTEGER_CST) |
11013 | { | |
11014 | expr = remove_c_maybe_const_expr (expr); | |
11015 | expr = build2 (NE_EXPR, integer_type_node, expr, | |
11016 | convert (TREE_TYPE (expr), integer_zero_node)); | |
11017 | expr = note_integer_operands (expr); | |
11018 | } | |
11019 | else | |
11020 | /* ??? Should we also give an error for vectors rather than leaving | |
11021 | those to give errors later? */ | |
11022 | expr = c_common_truthvalue_conversion (location, expr); | |
928c19bb JM |
11023 | |
11024 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
11025 | { | |
11026 | if (TREE_OVERFLOW (expr)) | |
11027 | return expr; | |
11028 | else | |
11029 | return note_integer_operands (expr); | |
11030 | } | |
11031 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
11032 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
11033 | return expr; | |
85498824 | 11034 | } |
73f397d4 JM |
11035 | \f |
11036 | ||
11037 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
11038 | required. */ | |
11039 | ||
11040 | tree | |
51eed280 | 11041 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) |
73f397d4 JM |
11042 | { |
11043 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
11044 | { | |
11045 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
11046 | /* Executing a compound literal inside a function reinitializes | |
11047 | it. */ | |
11048 | if (!TREE_STATIC (decl)) | |
11049 | *se = true; | |
11050 | return decl; | |
11051 | } | |
11052 | else | |
11053 | return expr; | |
11054 | } | |
953ff289 | 11055 | \f |
c0220ea4 | 11056 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
953ff289 DN |
11057 | |
11058 | tree | |
11059 | c_begin_omp_parallel (void) | |
11060 | { | |
11061 | tree block; | |
11062 | ||
11063 | keep_next_level (); | |
11064 | block = c_begin_compound_stmt (true); | |
11065 | ||
11066 | return block; | |
11067 | } | |
11068 | ||
c2255bc4 AH |
11069 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound |
11070 | statement. LOC is the location of the OMP_PARALLEL. */ | |
a68ab351 | 11071 | |
953ff289 | 11072 | tree |
c2255bc4 | 11073 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) |
953ff289 DN |
11074 | { |
11075 | tree stmt; | |
11076 | ||
c2255bc4 | 11077 | block = c_end_compound_stmt (loc, block, true); |
953ff289 DN |
11078 | |
11079 | stmt = make_node (OMP_PARALLEL); | |
11080 | TREE_TYPE (stmt) = void_type_node; | |
11081 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
11082 | OMP_PARALLEL_BODY (stmt) = block; | |
c2255bc4 | 11083 | SET_EXPR_LOCATION (stmt, loc); |
953ff289 DN |
11084 | |
11085 | return add_stmt (stmt); | |
11086 | } | |
11087 | ||
a68ab351 JJ |
11088 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
11089 | ||
11090 | tree | |
11091 | c_begin_omp_task (void) | |
11092 | { | |
11093 | tree block; | |
11094 | ||
11095 | keep_next_level (); | |
11096 | block = c_begin_compound_stmt (true); | |
11097 | ||
11098 | return block; | |
11099 | } | |
11100 | ||
c2255bc4 AH |
11101 | /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound |
11102 | statement. LOC is the location of the #pragma. */ | |
a68ab351 JJ |
11103 | |
11104 | tree | |
c2255bc4 | 11105 | c_finish_omp_task (location_t loc, tree clauses, tree block) |
a68ab351 JJ |
11106 | { |
11107 | tree stmt; | |
11108 | ||
c2255bc4 | 11109 | block = c_end_compound_stmt (loc, block, true); |
a68ab351 JJ |
11110 | |
11111 | stmt = make_node (OMP_TASK); | |
11112 | TREE_TYPE (stmt) = void_type_node; | |
11113 | OMP_TASK_CLAUSES (stmt) = clauses; | |
11114 | OMP_TASK_BODY (stmt) = block; | |
c2255bc4 | 11115 | SET_EXPR_LOCATION (stmt, loc); |
a68ab351 JJ |
11116 | |
11117 | return add_stmt (stmt); | |
11118 | } | |
11119 | ||
acf0174b JJ |
11120 | /* Generate GOMP_cancel call for #pragma omp cancel. */ |
11121 | ||
11122 | void | |
11123 | c_finish_omp_cancel (location_t loc, tree clauses) | |
11124 | { | |
11125 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL); | |
11126 | int mask = 0; | |
11127 | if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) | |
11128 | mask = 1; | |
11129 | else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) | |
11130 | mask = 2; | |
11131 | else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) | |
11132 | mask = 4; | |
11133 | else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) | |
11134 | mask = 8; | |
11135 | else | |
11136 | { | |
11137 | error_at (loc, "%<#pragma omp cancel must specify one of " | |
11138 | "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " | |
11139 | "clauses"); | |
11140 | return; | |
11141 | } | |
11142 | tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF); | |
11143 | if (ifc != NULL_TREE) | |
11144 | { | |
11145 | tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc)); | |
11146 | ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR, | |
11147 | boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc), | |
11148 | build_zero_cst (type)); | |
11149 | } | |
11150 | else | |
11151 | ifc = boolean_true_node; | |
11152 | tree stmt = build_call_expr_loc (loc, fn, 2, | |
11153 | build_int_cst (integer_type_node, mask), | |
11154 | ifc); | |
11155 | add_stmt (stmt); | |
11156 | } | |
11157 | ||
11158 | /* Generate GOMP_cancellation_point call for | |
11159 | #pragma omp cancellation point. */ | |
11160 | ||
11161 | void | |
11162 | c_finish_omp_cancellation_point (location_t loc, tree clauses) | |
11163 | { | |
11164 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT); | |
11165 | int mask = 0; | |
11166 | if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL)) | |
11167 | mask = 1; | |
11168 | else if (find_omp_clause (clauses, OMP_CLAUSE_FOR)) | |
11169 | mask = 2; | |
11170 | else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS)) | |
11171 | mask = 4; | |
11172 | else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP)) | |
11173 | mask = 8; | |
11174 | else | |
11175 | { | |
11176 | error_at (loc, "%<#pragma omp cancellation point must specify one of " | |
11177 | "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> " | |
11178 | "clauses"); | |
11179 | return; | |
11180 | } | |
11181 | tree stmt = build_call_expr_loc (loc, fn, 1, | |
11182 | build_int_cst (integer_type_node, mask)); | |
11183 | add_stmt (stmt); | |
11184 | } | |
11185 | ||
11186 | /* Helper function for handle_omp_array_sections. Called recursively | |
11187 | to handle multiple array-section-subscripts. C is the clause, | |
11188 | T current expression (initially OMP_CLAUSE_DECL), which is either | |
11189 | a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound | |
11190 | expression if specified, TREE_VALUE length expression if specified, | |
11191 | TREE_CHAIN is what it has been specified after, or some decl. | |
11192 | TYPES vector is populated with array section types, MAYBE_ZERO_LEN | |
11193 | set to true if any of the array-section-subscript could have length | |
11194 | of zero (explicit or implicit), FIRST_NON_ONE is the index of the | |
11195 | first array-section-subscript which is known not to have length | |
11196 | of one. Given say: | |
11197 | map(a[:b][2:1][:c][:2][:d][e:f][2:5]) | |
11198 | FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c] | |
11199 | all are or may have length of 1, array-section-subscript [:2] is the | |
11200 | first one knonwn not to have length 1. For array-section-subscript | |
11201 | <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't | |
11202 | 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we | |
11203 | can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above | |
11204 | case though, as some lengths could be zero. */ | |
11205 | ||
11206 | static tree | |
11207 | handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types, | |
11208 | bool &maybe_zero_len, unsigned int &first_non_one) | |
11209 | { | |
11210 | tree ret, low_bound, length, type; | |
11211 | if (TREE_CODE (t) != TREE_LIST) | |
11212 | { | |
11213 | if (t == error_mark_node || TREE_TYPE (t) == error_mark_node) | |
11214 | return error_mark_node; | |
11215 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11216 | { | |
11217 | if (DECL_P (t)) | |
11218 | error_at (OMP_CLAUSE_LOCATION (c), | |
11219 | "%qD is not a variable in %qs clause", t, | |
11220 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11221 | else | |
11222 | error_at (OMP_CLAUSE_LOCATION (c), | |
11223 | "%qE is not a variable in %qs clause", t, | |
11224 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11225 | return error_mark_node; | |
11226 | } | |
11227 | else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND | |
11228 | && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
11229 | { | |
11230 | error_at (OMP_CLAUSE_LOCATION (c), | |
11231 | "%qD is threadprivate variable in %qs clause", t, | |
11232 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11233 | return error_mark_node; | |
11234 | } | |
11235 | return t; | |
11236 | } | |
11237 | ||
11238 | ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types, | |
11239 | maybe_zero_len, first_non_one); | |
11240 | if (ret == error_mark_node || ret == NULL_TREE) | |
11241 | return ret; | |
11242 | ||
11243 | type = TREE_TYPE (ret); | |
11244 | low_bound = TREE_PURPOSE (t); | |
11245 | length = TREE_VALUE (t); | |
11246 | ||
11247 | if (low_bound == error_mark_node || length == error_mark_node) | |
11248 | return error_mark_node; | |
11249 | ||
11250 | if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound))) | |
11251 | { | |
11252 | error_at (OMP_CLAUSE_LOCATION (c), | |
11253 | "low bound %qE of array section does not have integral type", | |
11254 | low_bound); | |
11255 | return error_mark_node; | |
11256 | } | |
11257 | if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length))) | |
11258 | { | |
11259 | error_at (OMP_CLAUSE_LOCATION (c), | |
11260 | "length %qE of array section does not have integral type", | |
11261 | length); | |
11262 | return error_mark_node; | |
11263 | } | |
11264 | if (low_bound | |
11265 | && TREE_CODE (low_bound) == INTEGER_CST | |
11266 | && TYPE_PRECISION (TREE_TYPE (low_bound)) | |
11267 | > TYPE_PRECISION (sizetype)) | |
11268 | low_bound = fold_convert (sizetype, low_bound); | |
11269 | if (length | |
11270 | && TREE_CODE (length) == INTEGER_CST | |
11271 | && TYPE_PRECISION (TREE_TYPE (length)) | |
11272 | > TYPE_PRECISION (sizetype)) | |
11273 | length = fold_convert (sizetype, length); | |
11274 | if (low_bound == NULL_TREE) | |
11275 | low_bound = integer_zero_node; | |
11276 | ||
11277 | if (length != NULL_TREE) | |
11278 | { | |
11279 | if (!integer_nonzerop (length)) | |
11280 | maybe_zero_len = true; | |
11281 | if (first_non_one == types.length () | |
11282 | && (TREE_CODE (length) != INTEGER_CST || integer_onep (length))) | |
11283 | first_non_one++; | |
11284 | } | |
11285 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11286 | { | |
11287 | if (length == NULL_TREE | |
11288 | && (TYPE_DOMAIN (type) == NULL_TREE | |
11289 | || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)) | |
11290 | { | |
11291 | error_at (OMP_CLAUSE_LOCATION (c), | |
11292 | "for unknown bound array type length expression must " | |
11293 | "be specified"); | |
11294 | return error_mark_node; | |
11295 | } | |
11296 | if (TREE_CODE (low_bound) == INTEGER_CST | |
11297 | && tree_int_cst_sgn (low_bound) == -1) | |
11298 | { | |
11299 | error_at (OMP_CLAUSE_LOCATION (c), | |
11300 | "negative low bound in array section in %qs clause", | |
11301 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11302 | return error_mark_node; | |
11303 | } | |
11304 | if (length != NULL_TREE | |
11305 | && TREE_CODE (length) == INTEGER_CST | |
11306 | && tree_int_cst_sgn (length) == -1) | |
11307 | { | |
11308 | error_at (OMP_CLAUSE_LOCATION (c), | |
11309 | "negative length in array section in %qs clause", | |
11310 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11311 | return error_mark_node; | |
11312 | } | |
11313 | if (TYPE_DOMAIN (type) | |
11314 | && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) | |
11315 | && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) | |
11316 | == INTEGER_CST) | |
11317 | { | |
11318 | tree size = size_binop (PLUS_EXPR, | |
11319 | TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
11320 | size_one_node); | |
11321 | if (TREE_CODE (low_bound) == INTEGER_CST) | |
11322 | { | |
11323 | if (tree_int_cst_lt (size, low_bound)) | |
11324 | { | |
11325 | error_at (OMP_CLAUSE_LOCATION (c), | |
11326 | "low bound %qE above array section size " | |
11327 | "in %qs clause", low_bound, | |
11328 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11329 | return error_mark_node; | |
11330 | } | |
11331 | if (tree_int_cst_equal (size, low_bound)) | |
11332 | maybe_zero_len = true; | |
11333 | else if (length == NULL_TREE | |
11334 | && first_non_one == types.length () | |
11335 | && tree_int_cst_equal | |
11336 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
11337 | low_bound)) | |
11338 | first_non_one++; | |
11339 | } | |
11340 | else if (length == NULL_TREE) | |
11341 | { | |
11342 | maybe_zero_len = true; | |
11343 | if (first_non_one == types.length ()) | |
11344 | first_non_one++; | |
11345 | } | |
11346 | if (length && TREE_CODE (length) == INTEGER_CST) | |
11347 | { | |
11348 | if (tree_int_cst_lt (size, length)) | |
11349 | { | |
11350 | error_at (OMP_CLAUSE_LOCATION (c), | |
11351 | "length %qE above array section size " | |
11352 | "in %qs clause", length, | |
11353 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11354 | return error_mark_node; | |
11355 | } | |
11356 | if (TREE_CODE (low_bound) == INTEGER_CST) | |
11357 | { | |
11358 | tree lbpluslen | |
11359 | = size_binop (PLUS_EXPR, | |
11360 | fold_convert (sizetype, low_bound), | |
11361 | fold_convert (sizetype, length)); | |
11362 | if (TREE_CODE (lbpluslen) == INTEGER_CST | |
11363 | && tree_int_cst_lt (size, lbpluslen)) | |
11364 | { | |
11365 | error_at (OMP_CLAUSE_LOCATION (c), | |
11366 | "high bound %qE above array section size " | |
11367 | "in %qs clause", lbpluslen, | |
11368 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11369 | return error_mark_node; | |
11370 | } | |
11371 | } | |
11372 | } | |
11373 | } | |
11374 | else if (length == NULL_TREE) | |
11375 | { | |
11376 | maybe_zero_len = true; | |
11377 | if (first_non_one == types.length ()) | |
11378 | first_non_one++; | |
11379 | } | |
11380 | ||
11381 | /* For [lb:] we will need to evaluate lb more than once. */ | |
11382 | if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) | |
11383 | { | |
11384 | tree lb = c_save_expr (low_bound); | |
11385 | if (lb != low_bound) | |
11386 | { | |
11387 | TREE_PURPOSE (t) = lb; | |
11388 | low_bound = lb; | |
11389 | } | |
11390 | } | |
11391 | } | |
11392 | else if (TREE_CODE (type) == POINTER_TYPE) | |
11393 | { | |
11394 | if (length == NULL_TREE) | |
11395 | { | |
11396 | error_at (OMP_CLAUSE_LOCATION (c), | |
11397 | "for pointer type length expression must be specified"); | |
11398 | return error_mark_node; | |
11399 | } | |
11400 | /* If there is a pointer type anywhere but in the very first | |
11401 | array-section-subscript, the array section can't be contiguous. */ | |
11402 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND | |
11403 | && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST) | |
11404 | { | |
11405 | error_at (OMP_CLAUSE_LOCATION (c), | |
11406 | "array section is not contiguous in %qs clause", | |
11407 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11408 | return error_mark_node; | |
11409 | } | |
11410 | } | |
11411 | else | |
11412 | { | |
11413 | error_at (OMP_CLAUSE_LOCATION (c), | |
11414 | "%qE does not have pointer or array type", ret); | |
11415 | return error_mark_node; | |
11416 | } | |
11417 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND) | |
11418 | types.safe_push (TREE_TYPE (ret)); | |
11419 | /* We will need to evaluate lb more than once. */ | |
11420 | tree lb = c_save_expr (low_bound); | |
11421 | if (lb != low_bound) | |
11422 | { | |
11423 | TREE_PURPOSE (t) = lb; | |
11424 | low_bound = lb; | |
11425 | } | |
11426 | ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound); | |
11427 | return ret; | |
11428 | } | |
11429 | ||
11430 | /* Handle array sections for clause C. */ | |
11431 | ||
11432 | static bool | |
11433 | handle_omp_array_sections (tree c) | |
11434 | { | |
11435 | bool maybe_zero_len = false; | |
11436 | unsigned int first_non_one = 0; | |
11437 | vec<tree> types = vNULL; | |
11438 | tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types, | |
11439 | maybe_zero_len, first_non_one); | |
11440 | if (first == error_mark_node) | |
11441 | { | |
11442 | types.release (); | |
11443 | return true; | |
11444 | } | |
11445 | if (first == NULL_TREE) | |
11446 | { | |
11447 | types.release (); | |
11448 | return false; | |
11449 | } | |
11450 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND) | |
11451 | { | |
11452 | tree t = OMP_CLAUSE_DECL (c); | |
11453 | tree tem = NULL_TREE; | |
11454 | types.release (); | |
11455 | /* Need to evaluate side effects in the length expressions | |
11456 | if any. */ | |
11457 | while (TREE_CODE (t) == TREE_LIST) | |
11458 | { | |
11459 | if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t))) | |
11460 | { | |
11461 | if (tem == NULL_TREE) | |
11462 | tem = TREE_VALUE (t); | |
11463 | else | |
11464 | tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), | |
11465 | TREE_VALUE (t), tem); | |
11466 | } | |
11467 | t = TREE_CHAIN (t); | |
11468 | } | |
11469 | if (tem) | |
11470 | first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first); | |
11471 | first = c_fully_fold (first, false, NULL); | |
11472 | OMP_CLAUSE_DECL (c) = first; | |
11473 | } | |
11474 | else | |
11475 | { | |
11476 | unsigned int num = types.length (), i; | |
11477 | tree t, side_effects = NULL_TREE, size = NULL_TREE; | |
11478 | tree condition = NULL_TREE; | |
11479 | ||
11480 | if (int_size_in_bytes (TREE_TYPE (first)) <= 0) | |
11481 | maybe_zero_len = true; | |
11482 | ||
11483 | for (i = num, t = OMP_CLAUSE_DECL (c); i > 0; | |
11484 | t = TREE_CHAIN (t)) | |
11485 | { | |
11486 | tree low_bound = TREE_PURPOSE (t); | |
11487 | tree length = TREE_VALUE (t); | |
11488 | ||
11489 | i--; | |
11490 | if (low_bound | |
11491 | && TREE_CODE (low_bound) == INTEGER_CST | |
11492 | && TYPE_PRECISION (TREE_TYPE (low_bound)) | |
11493 | > TYPE_PRECISION (sizetype)) | |
11494 | low_bound = fold_convert (sizetype, low_bound); | |
11495 | if (length | |
11496 | && TREE_CODE (length) == INTEGER_CST | |
11497 | && TYPE_PRECISION (TREE_TYPE (length)) | |
11498 | > TYPE_PRECISION (sizetype)) | |
11499 | length = fold_convert (sizetype, length); | |
11500 | if (low_bound == NULL_TREE) | |
11501 | low_bound = integer_zero_node; | |
11502 | if (!maybe_zero_len && i > first_non_one) | |
11503 | { | |
11504 | if (integer_nonzerop (low_bound)) | |
11505 | goto do_warn_noncontiguous; | |
11506 | if (length != NULL_TREE | |
11507 | && TREE_CODE (length) == INTEGER_CST | |
11508 | && TYPE_DOMAIN (types[i]) | |
11509 | && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])) | |
11510 | && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))) | |
11511 | == INTEGER_CST) | |
11512 | { | |
11513 | tree size; | |
11514 | size = size_binop (PLUS_EXPR, | |
11515 | TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), | |
11516 | size_one_node); | |
11517 | if (!tree_int_cst_equal (length, size)) | |
11518 | { | |
11519 | do_warn_noncontiguous: | |
11520 | error_at (OMP_CLAUSE_LOCATION (c), | |
11521 | "array section is not contiguous in %qs " | |
11522 | "clause", | |
11523 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11524 | types.release (); | |
11525 | return true; | |
11526 | } | |
11527 | } | |
11528 | if (length != NULL_TREE | |
11529 | && TREE_SIDE_EFFECTS (length)) | |
11530 | { | |
11531 | if (side_effects == NULL_TREE) | |
11532 | side_effects = length; | |
11533 | else | |
11534 | side_effects = build2 (COMPOUND_EXPR, | |
11535 | TREE_TYPE (side_effects), | |
11536 | length, side_effects); | |
11537 | } | |
11538 | } | |
11539 | else | |
11540 | { | |
11541 | tree l; | |
11542 | ||
11543 | if (i > first_non_one && length && integer_nonzerop (length)) | |
11544 | continue; | |
11545 | if (length) | |
11546 | l = fold_convert (sizetype, length); | |
11547 | else | |
11548 | { | |
11549 | l = size_binop (PLUS_EXPR, | |
11550 | TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])), | |
11551 | size_one_node); | |
11552 | l = size_binop (MINUS_EXPR, l, | |
11553 | fold_convert (sizetype, low_bound)); | |
11554 | } | |
11555 | if (i > first_non_one) | |
11556 | { | |
11557 | l = fold_build2 (NE_EXPR, boolean_type_node, l, | |
11558 | size_zero_node); | |
11559 | if (condition == NULL_TREE) | |
11560 | condition = l; | |
11561 | else | |
11562 | condition = fold_build2 (BIT_AND_EXPR, boolean_type_node, | |
11563 | l, condition); | |
11564 | } | |
11565 | else if (size == NULL_TREE) | |
11566 | { | |
11567 | size = size_in_bytes (TREE_TYPE (types[i])); | |
11568 | size = size_binop (MULT_EXPR, size, l); | |
11569 | if (condition) | |
11570 | size = fold_build3 (COND_EXPR, sizetype, condition, | |
11571 | size, size_zero_node); | |
11572 | } | |
11573 | else | |
11574 | size = size_binop (MULT_EXPR, size, l); | |
11575 | } | |
11576 | } | |
11577 | types.release (); | |
11578 | if (side_effects) | |
11579 | size = build2 (COMPOUND_EXPR, sizetype, side_effects, size); | |
11580 | first = c_fully_fold (first, false, NULL); | |
11581 | OMP_CLAUSE_DECL (c) = first; | |
11582 | if (size) | |
11583 | size = c_fully_fold (size, false, NULL); | |
11584 | OMP_CLAUSE_SIZE (c) = size; | |
11585 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) | |
11586 | return false; | |
11587 | tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP); | |
11588 | OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER; | |
11589 | if (!c_mark_addressable (t)) | |
11590 | return false; | |
11591 | OMP_CLAUSE_DECL (c2) = t; | |
11592 | t = build_fold_addr_expr (first); | |
11593 | t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t); | |
11594 | tree ptr = OMP_CLAUSE_DECL (c2); | |
11595 | if (!POINTER_TYPE_P (TREE_TYPE (ptr))) | |
11596 | ptr = build_fold_addr_expr (ptr); | |
11597 | t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
11598 | ptrdiff_type_node, t, | |
11599 | fold_convert_loc (OMP_CLAUSE_LOCATION (c), | |
11600 | ptrdiff_type_node, ptr)); | |
11601 | t = c_fully_fold (t, false, NULL); | |
11602 | OMP_CLAUSE_SIZE (c2) = t; | |
11603 | OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c); | |
11604 | OMP_CLAUSE_CHAIN (c) = c2; | |
11605 | } | |
11606 | return false; | |
11607 | } | |
11608 | ||
11609 | /* Helper function of finish_omp_clauses. Clone STMT as if we were making | |
11610 | an inline call. But, remap | |
11611 | the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER | |
11612 | and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */ | |
11613 | ||
11614 | static tree | |
11615 | c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2, | |
11616 | tree decl, tree placeholder) | |
11617 | { | |
11618 | copy_body_data id; | |
11619 | struct pointer_map_t *decl_map = pointer_map_create (); | |
11620 | ||
11621 | *pointer_map_insert (decl_map, omp_decl1) = placeholder; | |
11622 | *pointer_map_insert (decl_map, omp_decl2) = decl; | |
11623 | memset (&id, 0, sizeof (id)); | |
11624 | id.src_fn = DECL_CONTEXT (omp_decl1); | |
11625 | id.dst_fn = current_function_decl; | |
11626 | id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn); | |
11627 | id.decl_map = decl_map; | |
11628 | ||
11629 | id.copy_decl = copy_decl_no_change; | |
11630 | id.transform_call_graph_edges = CB_CGE_DUPLICATE; | |
11631 | id.transform_new_cfg = true; | |
11632 | id.transform_return_to_modify = false; | |
11633 | id.transform_lang_insert_block = NULL; | |
11634 | id.eh_lp_nr = 0; | |
11635 | walk_tree (&stmt, copy_tree_body_r, &id, NULL); | |
11636 | pointer_map_destroy (decl_map); | |
11637 | return stmt; | |
11638 | } | |
11639 | ||
11640 | /* Helper function of c_finish_omp_clauses, called via walk_tree. | |
11641 | Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */ | |
11642 | ||
11643 | static tree | |
11644 | c_find_omp_placeholder_r (tree *tp, int *, void *data) | |
11645 | { | |
11646 | if (*tp == (tree) data) | |
11647 | return *tp; | |
11648 | return NULL_TREE; | |
11649 | } | |
11650 | ||
953ff289 DN |
11651 | /* For all elements of CLAUSES, validate them vs OpenMP constraints. |
11652 | Remove any elements from the list that are invalid. */ | |
11653 | ||
11654 | tree | |
11655 | c_finish_omp_clauses (tree clauses) | |
11656 | { | |
11657 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
acf0174b | 11658 | bitmap_head aligned_head; |
953ff289 | 11659 | tree c, t, *pc = &clauses; |
acf0174b JJ |
11660 | bool branch_seen = false; |
11661 | bool copyprivate_seen = false; | |
11662 | tree *nowait_clause = NULL; | |
953ff289 DN |
11663 | |
11664 | bitmap_obstack_initialize (NULL); | |
11665 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
11666 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
11667 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
acf0174b | 11668 | bitmap_initialize (&aligned_head, &bitmap_default_obstack); |
953ff289 DN |
11669 | |
11670 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
11671 | { | |
11672 | bool remove = false; | |
11673 | bool need_complete = false; | |
11674 | bool need_implicitly_determined = false; | |
11675 | ||
aaf46ef9 | 11676 | switch (OMP_CLAUSE_CODE (c)) |
953ff289 DN |
11677 | { |
11678 | case OMP_CLAUSE_SHARED: | |
953ff289 DN |
11679 | need_implicitly_determined = true; |
11680 | goto check_dup_generic; | |
11681 | ||
11682 | case OMP_CLAUSE_PRIVATE: | |
953ff289 DN |
11683 | need_complete = true; |
11684 | need_implicitly_determined = true; | |
11685 | goto check_dup_generic; | |
11686 | ||
11687 | case OMP_CLAUSE_REDUCTION: | |
953ff289 DN |
11688 | need_implicitly_determined = true; |
11689 | t = OMP_CLAUSE_DECL (c); | |
acf0174b JJ |
11690 | if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE |
11691 | && FLOAT_TYPE_P (TREE_TYPE (t))) | |
953ff289 DN |
11692 | { |
11693 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
11694 | const char *r_name = NULL; | |
11695 | ||
11696 | switch (r_code) | |
11697 | { | |
11698 | case PLUS_EXPR: | |
11699 | case MULT_EXPR: | |
11700 | case MINUS_EXPR: | |
20906c66 JJ |
11701 | case MIN_EXPR: |
11702 | case MAX_EXPR: | |
953ff289 DN |
11703 | break; |
11704 | case BIT_AND_EXPR: | |
11705 | r_name = "&"; | |
11706 | break; | |
11707 | case BIT_XOR_EXPR: | |
11708 | r_name = "^"; | |
11709 | break; | |
11710 | case BIT_IOR_EXPR: | |
11711 | r_name = "|"; | |
11712 | break; | |
11713 | case TRUTH_ANDIF_EXPR: | |
11714 | r_name = "&&"; | |
11715 | break; | |
11716 | case TRUTH_ORIF_EXPR: | |
11717 | r_name = "||"; | |
11718 | break; | |
11719 | default: | |
11720 | gcc_unreachable (); | |
11721 | } | |
11722 | if (r_name) | |
11723 | { | |
c2255bc4 AH |
11724 | error_at (OMP_CLAUSE_LOCATION (c), |
11725 | "%qE has invalid type for %<reduction(%s)%>", | |
11726 | t, r_name); | |
953ff289 | 11727 | remove = true; |
ee1d5a02 | 11728 | break; |
953ff289 DN |
11729 | } |
11730 | } | |
acf0174b JJ |
11731 | else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node) |
11732 | { | |
11733 | error_at (OMP_CLAUSE_LOCATION (c), | |
11734 | "user defined reduction not found for %qD", t); | |
11735 | remove = true; | |
ee1d5a02 | 11736 | break; |
acf0174b JJ |
11737 | } |
11738 | else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)) | |
11739 | { | |
11740 | tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c); | |
11741 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t)); | |
11742 | tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c), | |
11743 | VAR_DECL, NULL_TREE, type); | |
11744 | OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder; | |
11745 | DECL_ARTIFICIAL (placeholder) = 1; | |
11746 | DECL_IGNORED_P (placeholder) = 1; | |
11747 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0))) | |
11748 | c_mark_addressable (placeholder); | |
11749 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1))) | |
11750 | c_mark_addressable (OMP_CLAUSE_DECL (c)); | |
11751 | OMP_CLAUSE_REDUCTION_MERGE (c) | |
11752 | = c_clone_omp_udr (TREE_VEC_ELT (list, 2), | |
11753 | TREE_VEC_ELT (list, 0), | |
11754 | TREE_VEC_ELT (list, 1), | |
11755 | OMP_CLAUSE_DECL (c), placeholder); | |
11756 | OMP_CLAUSE_REDUCTION_MERGE (c) | |
11757 | = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, | |
11758 | void_type_node, NULL_TREE, | |
11759 | OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE); | |
11760 | TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1; | |
11761 | if (TREE_VEC_LENGTH (list) == 6) | |
11762 | { | |
11763 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3))) | |
11764 | c_mark_addressable (OMP_CLAUSE_DECL (c)); | |
11765 | if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4))) | |
11766 | c_mark_addressable (placeholder); | |
11767 | tree init = TREE_VEC_ELT (list, 5); | |
11768 | if (init == error_mark_node) | |
11769 | init = DECL_INITIAL (TREE_VEC_ELT (list, 3)); | |
11770 | OMP_CLAUSE_REDUCTION_INIT (c) | |
11771 | = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4), | |
11772 | TREE_VEC_ELT (list, 3), | |
11773 | OMP_CLAUSE_DECL (c), placeholder); | |
11774 | if (TREE_VEC_ELT (list, 5) == error_mark_node) | |
11775 | OMP_CLAUSE_REDUCTION_INIT (c) | |
11776 | = build2 (INIT_EXPR, TREE_TYPE (t), t, | |
11777 | OMP_CLAUSE_REDUCTION_INIT (c)); | |
11778 | if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c), | |
11779 | c_find_omp_placeholder_r, | |
11780 | placeholder, NULL)) | |
11781 | OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1; | |
11782 | } | |
11783 | else | |
11784 | { | |
11785 | tree init; | |
11786 | if (AGGREGATE_TYPE_P (TREE_TYPE (t))) | |
11787 | init = build_constructor (TREE_TYPE (t), NULL); | |
11788 | else | |
11789 | init = fold_convert (TREE_TYPE (t), integer_zero_node); | |
11790 | OMP_CLAUSE_REDUCTION_INIT (c) | |
11791 | = build2 (INIT_EXPR, TREE_TYPE (t), t, init); | |
11792 | } | |
11793 | OMP_CLAUSE_REDUCTION_INIT (c) | |
11794 | = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR, | |
11795 | void_type_node, NULL_TREE, | |
11796 | OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE); | |
11797 | TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1; | |
11798 | } | |
953ff289 DN |
11799 | goto check_dup_generic; |
11800 | ||
11801 | case OMP_CLAUSE_COPYPRIVATE: | |
acf0174b JJ |
11802 | copyprivate_seen = true; |
11803 | if (nowait_clause) | |
11804 | { | |
11805 | error_at (OMP_CLAUSE_LOCATION (*nowait_clause), | |
11806 | "%<nowait%> clause must not be used together " | |
11807 | "with %<copyprivate%>"); | |
11808 | *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause); | |
11809 | nowait_clause = NULL; | |
11810 | } | |
953ff289 DN |
11811 | goto check_dup_generic; |
11812 | ||
11813 | case OMP_CLAUSE_COPYIN: | |
953ff289 DN |
11814 | t = OMP_CLAUSE_DECL (c); |
11815 | if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) | |
11816 | { | |
c2255bc4 AH |
11817 | error_at (OMP_CLAUSE_LOCATION (c), |
11818 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
953ff289 | 11819 | remove = true; |
ee1d5a02 | 11820 | break; |
953ff289 DN |
11821 | } |
11822 | goto check_dup_generic; | |
11823 | ||
acf0174b JJ |
11824 | case OMP_CLAUSE_LINEAR: |
11825 | t = OMP_CLAUSE_DECL (c); | |
11826 | if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) | |
11827 | && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE) | |
11828 | { | |
11829 | error_at (OMP_CLAUSE_LOCATION (c), | |
11830 | "linear clause applied to non-integral non-pointer " | |
11831 | "variable with type %qT", TREE_TYPE (t)); | |
11832 | remove = true; | |
11833 | break; | |
11834 | } | |
11835 | if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE) | |
11836 | { | |
11837 | tree s = OMP_CLAUSE_LINEAR_STEP (c); | |
11838 | s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR, | |
11839 | OMP_CLAUSE_DECL (c), s); | |
11840 | s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR, | |
11841 | sizetype, s, OMP_CLAUSE_DECL (c)); | |
11842 | if (s == error_mark_node) | |
11843 | s = size_one_node; | |
11844 | OMP_CLAUSE_LINEAR_STEP (c) = s; | |
11845 | } | |
11846 | goto check_dup_generic; | |
11847 | ||
953ff289 DN |
11848 | check_dup_generic: |
11849 | t = OMP_CLAUSE_DECL (c); | |
11850 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11851 | { | |
c2255bc4 | 11852 | error_at (OMP_CLAUSE_LOCATION (c), |
acf0174b JJ |
11853 | "%qE is not a variable in clause %qs", t, |
11854 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
953ff289 DN |
11855 | remove = true; |
11856 | } | |
11857 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
11858 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
11859 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
11860 | { | |
c2255bc4 AH |
11861 | error_at (OMP_CLAUSE_LOCATION (c), |
11862 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
11863 | remove = true; |
11864 | } | |
11865 | else | |
11866 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
11867 | break; | |
11868 | ||
11869 | case OMP_CLAUSE_FIRSTPRIVATE: | |
953ff289 DN |
11870 | t = OMP_CLAUSE_DECL (c); |
11871 | need_complete = true; | |
11872 | need_implicitly_determined = true; | |
11873 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11874 | { | |
c2255bc4 AH |
11875 | error_at (OMP_CLAUSE_LOCATION (c), |
11876 | "%qE is not a variable in clause %<firstprivate%>", t); | |
953ff289 DN |
11877 | remove = true; |
11878 | } | |
11879 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
11880 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
11881 | { | |
c2255bc4 AH |
11882 | error_at (OMP_CLAUSE_LOCATION (c), |
11883 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
11884 | remove = true; |
11885 | } | |
11886 | else | |
11887 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
11888 | break; | |
11889 | ||
11890 | case OMP_CLAUSE_LASTPRIVATE: | |
953ff289 DN |
11891 | t = OMP_CLAUSE_DECL (c); |
11892 | need_complete = true; | |
11893 | need_implicitly_determined = true; | |
11894 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11895 | { | |
c2255bc4 AH |
11896 | error_at (OMP_CLAUSE_LOCATION (c), |
11897 | "%qE is not a variable in clause %<lastprivate%>", t); | |
953ff289 DN |
11898 | remove = true; |
11899 | } | |
11900 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
11901 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
11902 | { | |
c2255bc4 AH |
11903 | error_at (OMP_CLAUSE_LOCATION (c), |
11904 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
11905 | remove = true; |
11906 | } | |
11907 | else | |
11908 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
11909 | break; | |
11910 | ||
acf0174b JJ |
11911 | case OMP_CLAUSE_ALIGNED: |
11912 | t = OMP_CLAUSE_DECL (c); | |
11913 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11914 | { | |
11915 | error_at (OMP_CLAUSE_LOCATION (c), | |
11916 | "%qE is not a variable in %<aligned%> clause", t); | |
11917 | remove = true; | |
11918 | } | |
5a9785fb JJ |
11919 | else if (!POINTER_TYPE_P (TREE_TYPE (t)) |
11920 | && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE) | |
11921 | { | |
11922 | error_at (OMP_CLAUSE_LOCATION (c), | |
11923 | "%qE in %<aligned%> clause is neither a pointer nor " | |
11924 | "an array", t); | |
11925 | remove = true; | |
11926 | } | |
acf0174b JJ |
11927 | else if (bitmap_bit_p (&aligned_head, DECL_UID (t))) |
11928 | { | |
11929 | error_at (OMP_CLAUSE_LOCATION (c), | |
11930 | "%qE appears more than once in %<aligned%> clauses", | |
11931 | t); | |
11932 | remove = true; | |
11933 | } | |
11934 | else | |
11935 | bitmap_set_bit (&aligned_head, DECL_UID (t)); | |
11936 | break; | |
11937 | ||
11938 | case OMP_CLAUSE_DEPEND: | |
11939 | t = OMP_CLAUSE_DECL (c); | |
11940 | if (TREE_CODE (t) == TREE_LIST) | |
11941 | { | |
11942 | if (handle_omp_array_sections (c)) | |
11943 | remove = true; | |
11944 | break; | |
11945 | } | |
11946 | if (t == error_mark_node) | |
11947 | remove = true; | |
11948 | else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11949 | { | |
11950 | error_at (OMP_CLAUSE_LOCATION (c), | |
11951 | "%qE is not a variable in %<depend%> clause", t); | |
11952 | remove = true; | |
11953 | } | |
11954 | else if (!c_mark_addressable (t)) | |
11955 | remove = true; | |
11956 | break; | |
11957 | ||
11958 | case OMP_CLAUSE_MAP: | |
11959 | case OMP_CLAUSE_TO: | |
11960 | case OMP_CLAUSE_FROM: | |
11961 | t = OMP_CLAUSE_DECL (c); | |
11962 | if (TREE_CODE (t) == TREE_LIST) | |
11963 | { | |
11964 | if (handle_omp_array_sections (c)) | |
11965 | remove = true; | |
11966 | else | |
11967 | { | |
11968 | t = OMP_CLAUSE_DECL (c); | |
11969 | if (!COMPLETE_TYPE_P (TREE_TYPE (t))) | |
11970 | { | |
11971 | error_at (OMP_CLAUSE_LOCATION (c), | |
11972 | "array section does not have mappable type " | |
11973 | "in %qs clause", | |
11974 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11975 | remove = true; | |
11976 | } | |
11977 | } | |
11978 | break; | |
11979 | } | |
11980 | if (t == error_mark_node) | |
11981 | remove = true; | |
11982 | else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
11983 | { | |
11984 | error_at (OMP_CLAUSE_LOCATION (c), | |
11985 | "%qE is not a variable in %qs clause", t, | |
11986 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11987 | remove = true; | |
11988 | } | |
11989 | else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
11990 | { | |
11991 | error_at (OMP_CLAUSE_LOCATION (c), | |
11992 | "%qD is threadprivate variable in %qs clause", t, | |
11993 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
11994 | remove = true; | |
11995 | } | |
11996 | else if (!c_mark_addressable (t)) | |
11997 | remove = true; | |
11998 | else if (!COMPLETE_TYPE_P (TREE_TYPE (t)) | |
11999 | && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP | |
12000 | && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)) | |
12001 | { | |
12002 | error_at (OMP_CLAUSE_LOCATION (c), | |
12003 | "%qD does not have a mappable type in %qs clause", t, | |
12004 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
12005 | remove = true; | |
12006 | } | |
12007 | else if (bitmap_bit_p (&generic_head, DECL_UID (t))) | |
12008 | { | |
12009 | if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP) | |
12010 | error ("%qD appears more than once in motion clauses", t); | |
12011 | else | |
12012 | error ("%qD appears more than once in map clauses", t); | |
12013 | remove = true; | |
12014 | } | |
12015 | else | |
12016 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
12017 | break; | |
12018 | ||
12019 | case OMP_CLAUSE_UNIFORM: | |
12020 | t = OMP_CLAUSE_DECL (c); | |
12021 | if (TREE_CODE (t) != PARM_DECL) | |
12022 | { | |
12023 | if (DECL_P (t)) | |
12024 | error_at (OMP_CLAUSE_LOCATION (c), | |
12025 | "%qD is not an argument in %<uniform%> clause", t); | |
12026 | else | |
12027 | error_at (OMP_CLAUSE_LOCATION (c), | |
12028 | "%qE is not an argument in %<uniform%> clause", t); | |
12029 | remove = true; | |
ee1d5a02 | 12030 | break; |
acf0174b | 12031 | } |
ee1d5a02 | 12032 | goto check_dup_generic; |
acf0174b JJ |
12033 | |
12034 | case OMP_CLAUSE_NOWAIT: | |
12035 | if (copyprivate_seen) | |
12036 | { | |
12037 | error_at (OMP_CLAUSE_LOCATION (c), | |
12038 | "%<nowait%> clause must not be used together " | |
12039 | "with %<copyprivate%>"); | |
12040 | remove = true; | |
12041 | break; | |
12042 | } | |
12043 | nowait_clause = pc; | |
12044 | pc = &OMP_CLAUSE_CHAIN (c); | |
12045 | continue; | |
12046 | ||
953ff289 DN |
12047 | case OMP_CLAUSE_IF: |
12048 | case OMP_CLAUSE_NUM_THREADS: | |
acf0174b JJ |
12049 | case OMP_CLAUSE_NUM_TEAMS: |
12050 | case OMP_CLAUSE_THREAD_LIMIT: | |
953ff289 | 12051 | case OMP_CLAUSE_SCHEDULE: |
953ff289 DN |
12052 | case OMP_CLAUSE_ORDERED: |
12053 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 JJ |
12054 | case OMP_CLAUSE_UNTIED: |
12055 | case OMP_CLAUSE_COLLAPSE: | |
20906c66 JJ |
12056 | case OMP_CLAUSE_FINAL: |
12057 | case OMP_CLAUSE_MERGEABLE: | |
acf0174b JJ |
12058 | case OMP_CLAUSE_SAFELEN: |
12059 | case OMP_CLAUSE_SIMDLEN: | |
12060 | case OMP_CLAUSE_DEVICE: | |
12061 | case OMP_CLAUSE_DIST_SCHEDULE: | |
12062 | case OMP_CLAUSE_PARALLEL: | |
12063 | case OMP_CLAUSE_FOR: | |
12064 | case OMP_CLAUSE_SECTIONS: | |
12065 | case OMP_CLAUSE_TASKGROUP: | |
12066 | case OMP_CLAUSE_PROC_BIND: | |
12067 | pc = &OMP_CLAUSE_CHAIN (c); | |
12068 | continue; | |
12069 | ||
12070 | case OMP_CLAUSE_INBRANCH: | |
12071 | case OMP_CLAUSE_NOTINBRANCH: | |
12072 | if (branch_seen) | |
12073 | { | |
12074 | error_at (OMP_CLAUSE_LOCATION (c), | |
12075 | "%<inbranch%> clause is incompatible with " | |
12076 | "%<notinbranch%>"); | |
12077 | remove = true; | |
12078 | break; | |
12079 | } | |
12080 | branch_seen = true; | |
953ff289 DN |
12081 | pc = &OMP_CLAUSE_CHAIN (c); |
12082 | continue; | |
12083 | ||
12084 | default: | |
12085 | gcc_unreachable (); | |
12086 | } | |
12087 | ||
12088 | if (!remove) | |
12089 | { | |
12090 | t = OMP_CLAUSE_DECL (c); | |
12091 | ||
12092 | if (need_complete) | |
12093 | { | |
12094 | t = require_complete_type (t); | |
12095 | if (t == error_mark_node) | |
12096 | remove = true; | |
12097 | } | |
12098 | ||
12099 | if (need_implicitly_determined) | |
12100 | { | |
12101 | const char *share_name = NULL; | |
12102 | ||
12103 | if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
12104 | share_name = "threadprivate"; | |
12105 | else switch (c_omp_predetermined_sharing (t)) | |
12106 | { | |
12107 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
12108 | break; | |
12109 | case OMP_CLAUSE_DEFAULT_SHARED: | |
20906c66 JJ |
12110 | /* const vars may be specified in firstprivate clause. */ |
12111 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
12112 | && TREE_READONLY (t)) | |
12113 | break; | |
953ff289 DN |
12114 | share_name = "shared"; |
12115 | break; | |
12116 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
12117 | share_name = "private"; | |
12118 | break; | |
12119 | default: | |
12120 | gcc_unreachable (); | |
12121 | } | |
12122 | if (share_name) | |
12123 | { | |
c2255bc4 AH |
12124 | error_at (OMP_CLAUSE_LOCATION (c), |
12125 | "%qE is predetermined %qs for %qs", | |
acf0174b JJ |
12126 | t, share_name, |
12127 | omp_clause_code_name[OMP_CLAUSE_CODE (c)]); | |
953ff289 DN |
12128 | remove = true; |
12129 | } | |
12130 | } | |
12131 | } | |
12132 | ||
12133 | if (remove) | |
12134 | *pc = OMP_CLAUSE_CHAIN (c); | |
12135 | else | |
12136 | pc = &OMP_CLAUSE_CHAIN (c); | |
12137 | } | |
12138 | ||
12139 | bitmap_obstack_release (NULL); | |
12140 | return clauses; | |
12141 | } | |
9ae165a0 | 12142 | |
0a35513e AH |
12143 | /* Create a transaction node. */ |
12144 | ||
12145 | tree | |
12146 | c_finish_transaction (location_t loc, tree block, int flags) | |
12147 | { | |
12148 | tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); | |
12149 | if (flags & TM_STMT_ATTR_OUTER) | |
12150 | TRANSACTION_EXPR_OUTER (stmt) = 1; | |
12151 | if (flags & TM_STMT_ATTR_RELAXED) | |
12152 | TRANSACTION_EXPR_RELAXED (stmt) = 1; | |
12153 | return add_stmt (stmt); | |
12154 | } | |
12155 | ||
9ae165a0 DG |
12156 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
12157 | down to the element type of an array. */ | |
12158 | ||
12159 | tree | |
12160 | c_build_qualified_type (tree type, int type_quals) | |
12161 | { | |
12162 | if (type == error_mark_node) | |
12163 | return type; | |
12164 | ||
12165 | if (TREE_CODE (type) == ARRAY_TYPE) | |
12166 | { | |
12167 | tree t; | |
12168 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
12169 | type_quals); | |
12170 | ||
12171 | /* See if we already have an identically qualified type. */ | |
12172 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
12173 | { | |
12174 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
12175 | && TYPE_NAME (t) == TYPE_NAME (type) | |
12176 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
12177 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
12178 | TYPE_ATTRIBUTES (type))) | |
12179 | break; | |
12180 | } | |
12181 | if (!t) | |
12182 | { | |
12183 | tree domain = TYPE_DOMAIN (type); | |
12184 | ||
12185 | t = build_variant_type_copy (type); | |
12186 | TREE_TYPE (t) = element_type; | |
12187 | ||
12188 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
12189 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
12190 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
12191 | else if (TYPE_CANONICAL (element_type) != element_type | |
12192 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
12193 | { | |
b8698a0f | 12194 | tree unqualified_canon |
9ae165a0 | 12195 | = build_array_type (TYPE_CANONICAL (element_type), |
b8698a0f | 12196 | domain? TYPE_CANONICAL (domain) |
9ae165a0 | 12197 | : NULL_TREE); |
b8698a0f | 12198 | TYPE_CANONICAL (t) |
9ae165a0 DG |
12199 | = c_build_qualified_type (unqualified_canon, type_quals); |
12200 | } | |
12201 | else | |
12202 | TYPE_CANONICAL (t) = t; | |
12203 | } | |
12204 | return t; | |
12205 | } | |
12206 | ||
12207 | /* A restrict-qualified pointer type must be a pointer to object or | |
12208 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
12209 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
12210 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
12211 | && (!POINTER_TYPE_P (type) | |
12212 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
12213 | { | |
12214 | error ("invalid use of %<restrict%>"); | |
12215 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
12216 | } | |
12217 | ||
12218 | return build_qualified_type (type, type_quals); | |
12219 | } | |
72b5577d ILT |
12220 | |
12221 | /* Build a VA_ARG_EXPR for the C parser. */ | |
12222 | ||
12223 | tree | |
c2255bc4 | 12224 | c_build_va_arg (location_t loc, tree expr, tree type) |
72b5577d ILT |
12225 | { |
12226 | if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) | |
12227 | warning_at (loc, OPT_Wc___compat, | |
12228 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
c2255bc4 | 12229 | return build_va_arg (loc, expr, type); |
72b5577d | 12230 | } |
acf0174b JJ |
12231 | |
12232 | /* Return truthvalue of whether T1 is the same tree structure as T2. | |
12233 | Return 1 if they are the same. Return 0 if they are different. */ | |
12234 | ||
12235 | bool | |
12236 | c_tree_equal (tree t1, tree t2) | |
12237 | { | |
12238 | enum tree_code code1, code2; | |
12239 | ||
12240 | if (t1 == t2) | |
12241 | return true; | |
12242 | if (!t1 || !t2) | |
12243 | return false; | |
12244 | ||
12245 | for (code1 = TREE_CODE (t1); | |
12246 | CONVERT_EXPR_CODE_P (code1) | |
12247 | || code1 == NON_LVALUE_EXPR; | |
12248 | code1 = TREE_CODE (t1)) | |
12249 | t1 = TREE_OPERAND (t1, 0); | |
12250 | for (code2 = TREE_CODE (t2); | |
12251 | CONVERT_EXPR_CODE_P (code2) | |
12252 | || code2 == NON_LVALUE_EXPR; | |
12253 | code2 = TREE_CODE (t2)) | |
12254 | t2 = TREE_OPERAND (t2, 0); | |
12255 | ||
12256 | /* They might have become equal now. */ | |
12257 | if (t1 == t2) | |
12258 | return true; | |
12259 | ||
12260 | if (code1 != code2) | |
12261 | return false; | |
12262 | ||
12263 | switch (code1) | |
12264 | { | |
12265 | case INTEGER_CST: | |
12266 | return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2) | |
12267 | && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2); | |
12268 | ||
12269 | case REAL_CST: | |
12270 | return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2)); | |
12271 | ||
12272 | case STRING_CST: | |
12273 | return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2) | |
12274 | && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), | |
12275 | TREE_STRING_LENGTH (t1)); | |
12276 | ||
12277 | case FIXED_CST: | |
12278 | return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), | |
12279 | TREE_FIXED_CST (t2)); | |
12280 | ||
12281 | case COMPLEX_CST: | |
12282 | return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2)) | |
12283 | && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2)); | |
12284 | ||
12285 | case VECTOR_CST: | |
12286 | return operand_equal_p (t1, t2, OEP_ONLY_CONST); | |
12287 | ||
12288 | case CONSTRUCTOR: | |
12289 | /* We need to do this when determining whether or not two | |
12290 | non-type pointer to member function template arguments | |
12291 | are the same. */ | |
12292 | if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)) | |
12293 | || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2)) | |
12294 | return false; | |
12295 | { | |
12296 | tree field, value; | |
12297 | unsigned int i; | |
12298 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value) | |
12299 | { | |
12300 | constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i); | |
12301 | if (!c_tree_equal (field, elt2->index) | |
12302 | || !c_tree_equal (value, elt2->value)) | |
12303 | return false; | |
12304 | } | |
12305 | } | |
12306 | return true; | |
12307 | ||
12308 | case TREE_LIST: | |
12309 | if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))) | |
12310 | return false; | |
12311 | if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2))) | |
12312 | return false; | |
12313 | return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
12314 | ||
12315 | case SAVE_EXPR: | |
12316 | return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
12317 | ||
12318 | case CALL_EXPR: | |
12319 | { | |
12320 | tree arg1, arg2; | |
12321 | call_expr_arg_iterator iter1, iter2; | |
12322 | if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) | |
12323 | return false; | |
12324 | for (arg1 = first_call_expr_arg (t1, &iter1), | |
12325 | arg2 = first_call_expr_arg (t2, &iter2); | |
12326 | arg1 && arg2; | |
12327 | arg1 = next_call_expr_arg (&iter1), | |
12328 | arg2 = next_call_expr_arg (&iter2)) | |
12329 | if (!c_tree_equal (arg1, arg2)) | |
12330 | return false; | |
12331 | if (arg1 || arg2) | |
12332 | return false; | |
12333 | return true; | |
12334 | } | |
12335 | ||
12336 | case TARGET_EXPR: | |
12337 | { | |
12338 | tree o1 = TREE_OPERAND (t1, 0); | |
12339 | tree o2 = TREE_OPERAND (t2, 0); | |
12340 | ||
12341 | /* Special case: if either target is an unallocated VAR_DECL, | |
12342 | it means that it's going to be unified with whatever the | |
12343 | TARGET_EXPR is really supposed to initialize, so treat it | |
12344 | as being equivalent to anything. */ | |
12345 | if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE | |
12346 | && !DECL_RTL_SET_P (o1)) | |
12347 | /*Nop*/; | |
12348 | else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE | |
12349 | && !DECL_RTL_SET_P (o2)) | |
12350 | /*Nop*/; | |
12351 | else if (!c_tree_equal (o1, o2)) | |
12352 | return false; | |
12353 | ||
12354 | return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); | |
12355 | } | |
12356 | ||
12357 | case COMPONENT_REF: | |
12358 | if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)) | |
12359 | return false; | |
12360 | return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
12361 | ||
12362 | case PARM_DECL: | |
12363 | case VAR_DECL: | |
12364 | case CONST_DECL: | |
12365 | case FIELD_DECL: | |
12366 | case FUNCTION_DECL: | |
12367 | case IDENTIFIER_NODE: | |
12368 | case SSA_NAME: | |
12369 | return false; | |
12370 | ||
12371 | case TREE_VEC: | |
12372 | { | |
12373 | unsigned ix; | |
12374 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
12375 | return false; | |
12376 | for (ix = TREE_VEC_LENGTH (t1); ix--;) | |
12377 | if (!c_tree_equal (TREE_VEC_ELT (t1, ix), | |
12378 | TREE_VEC_ELT (t2, ix))) | |
12379 | return false; | |
12380 | return true; | |
12381 | } | |
12382 | ||
12383 | default: | |
12384 | break; | |
12385 | } | |
12386 | ||
12387 | switch (TREE_CODE_CLASS (code1)) | |
12388 | { | |
12389 | case tcc_unary: | |
12390 | case tcc_binary: | |
12391 | case tcc_comparison: | |
12392 | case tcc_expression: | |
12393 | case tcc_vl_exp: | |
12394 | case tcc_reference: | |
12395 | case tcc_statement: | |
12396 | { | |
12397 | int i, n = TREE_OPERAND_LENGTH (t1); | |
12398 | ||
12399 | switch (code1) | |
12400 | { | |
12401 | case PREINCREMENT_EXPR: | |
12402 | case PREDECREMENT_EXPR: | |
12403 | case POSTINCREMENT_EXPR: | |
12404 | case POSTDECREMENT_EXPR: | |
12405 | n = 1; | |
12406 | break; | |
12407 | case ARRAY_REF: | |
12408 | n = 2; | |
12409 | break; | |
12410 | default: | |
12411 | break; | |
12412 | } | |
12413 | ||
12414 | if (TREE_CODE_CLASS (code1) == tcc_vl_exp | |
12415 | && n != TREE_OPERAND_LENGTH (t2)) | |
12416 | return false; | |
12417 | ||
12418 | for (i = 0; i < n; ++i) | |
12419 | if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i))) | |
12420 | return false; | |
12421 | ||
12422 | return true; | |
12423 | } | |
12424 | ||
12425 | case tcc_type: | |
12426 | return comptypes (t1, t2); | |
12427 | default: | |
12428 | gcc_unreachable (); | |
12429 | } | |
12430 | /* We can get here with --disable-checking. */ | |
12431 | return false; | |
12432 | } |