]>
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" |
e57e265b | 31 | #include "langhooks.h" |
400fbf9f | 32 | #include "c-tree.h" |
29cc57cf | 33 | #include "c-lang.h" |
400fbf9f | 34 | #include "flags.h" |
ab87f8c8 | 35 | #include "intl.h" |
672a6f42 | 36 | #include "target.h" |
325c3691 | 37 | #include "tree-iterator.h" |
6662d794 MLI |
38 | #include "bitmap.h" |
39 | #include "gimple.h" | |
61d3ce20 | 40 | #include "c-family/c-objc.h" |
a212e43f | 41 | #include "c-family/c-common.h" |
325c3691 | 42 | |
2ac2f164 JM |
43 | /* Possible cases of implicit bad conversions. Used to select |
44 | diagnostic messages in convert_for_assignment. */ | |
45 | enum impl_conv { | |
46 | ic_argpass, | |
47 | ic_assign, | |
48 | ic_init, | |
49 | ic_return | |
50 | }; | |
51 | ||
bc4b653b JM |
52 | /* The level of nesting inside "__alignof__". */ |
53 | int in_alignof; | |
54 | ||
55 | /* The level of nesting inside "sizeof". */ | |
56 | int in_sizeof; | |
57 | ||
58 | /* The level of nesting inside "typeof". */ | |
59 | int in_typeof; | |
400fbf9f | 60 | |
1a4049e7 JJ |
61 | /* The argument of last parsed sizeof expression, only to be tested |
62 | if expr.original_code == SIZEOF_EXPR. */ | |
63 | tree c_last_sizeof_arg; | |
64 | ||
b71c7f8a | 65 | /* Nonzero if we've already printed a "missing braces around initializer" |
103b7b17 | 66 | message within this initializer. */ |
b71c7f8a | 67 | static int missing_braces_mentioned; |
103b7b17 | 68 | |
bf730f15 RS |
69 | static int require_constant_value; |
70 | static int require_constant_elements; | |
71 | ||
58f9752a | 72 | static bool null_pointer_constant_p (const_tree); |
f55ade6e | 73 | static tree qualify_type (tree, tree); |
dc5027f4 JM |
74 | static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *, |
75 | bool *); | |
744aa42f | 76 | static int comp_target_types (location_t, tree, tree); |
dc5027f4 JM |
77 | static int function_types_compatible_p (const_tree, const_tree, bool *, |
78 | bool *); | |
79 | static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *); | |
f55ade6e | 80 | static tree lookup_field (tree, tree); |
9771b263 DN |
81 | static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *, |
82 | tree, tree); | |
db3927fb | 83 | static tree pointer_diff (location_t, tree, tree); |
744aa42f ILT |
84 | static tree convert_for_assignment (location_t, tree, tree, tree, |
85 | enum impl_conv, bool, tree, tree, int); | |
f55ade6e AJ |
86 | static tree valid_compound_expr_initializer (tree, tree); |
87 | static void push_string (const char *); | |
88 | static void push_member_name (tree); | |
f55ade6e AJ |
89 | static int spelling_length (void); |
90 | static char *print_spelling (char *); | |
683d6ff9 | 91 | static void warning_init (int, const char *); |
c2255bc4 | 92 | static tree digest_init (location_t, tree, tree, tree, bool, bool, int); |
a1e3b3d9 LB |
93 | static void output_init_element (tree, tree, bool, tree, tree, int, bool, |
94 | struct obstack *); | |
95 | static void output_pending_init_elements (int, struct obstack *); | |
96 | static int set_designator (int, struct obstack *); | |
97 | static void push_range_stack (tree, struct obstack *); | |
98 | static void add_pending_init (tree, tree, tree, bool, struct obstack *); | |
99 | static void set_nonincremental_init (struct obstack *); | |
100 | static void set_nonincremental_init_from_string (tree, struct obstack *); | |
101 | static tree find_init_member (tree, struct obstack *); | |
f37acdf9 | 102 | static void readonly_warning (tree, enum lvalue_use); |
7bd11157 | 103 | static int lvalue_or_else (location_t, const_tree, enum lvalue_use); |
4e2fb7de | 104 | static void record_maybe_used_decl (tree); |
dc5027f4 | 105 | static int comptypes_internal (const_tree, const_tree, bool *, bool *); |
6aa3c60d JM |
106 | \f |
107 | /* Return true if EXP is a null pointer constant, false otherwise. */ | |
108 | ||
109 | static bool | |
58f9752a | 110 | null_pointer_constant_p (const_tree expr) |
6aa3c60d JM |
111 | { |
112 | /* This should really operate on c_expr structures, but they aren't | |
113 | yet available everywhere required. */ | |
114 | tree type = TREE_TYPE (expr); | |
115 | return (TREE_CODE (expr) == INTEGER_CST | |
8bcd6380 | 116 | && !TREE_OVERFLOW (expr) |
6aa3c60d JM |
117 | && integer_zerop (expr) |
118 | && (INTEGRAL_TYPE_P (type) | |
119 | || (TREE_CODE (type) == POINTER_TYPE | |
120 | && VOID_TYPE_P (TREE_TYPE (type)) | |
121 | && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); | |
122 | } | |
928c19bb JM |
123 | |
124 | /* EXPR may appear in an unevaluated part of an integer constant | |
125 | expression, but not in an evaluated part. Wrap it in a | |
126 | C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an | |
127 | INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ | |
128 | ||
129 | static tree | |
130 | note_integer_operands (tree expr) | |
131 | { | |
132 | tree ret; | |
133 | if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) | |
134 | { | |
135 | ret = copy_node (expr); | |
136 | TREE_OVERFLOW (ret) = 1; | |
137 | } | |
138 | else | |
139 | { | |
140 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); | |
141 | C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; | |
142 | } | |
143 | return ret; | |
144 | } | |
145 | ||
4d84fe7c JM |
146 | /* Having checked whether EXPR may appear in an unevaluated part of an |
147 | integer constant expression and found that it may, remove any | |
148 | C_MAYBE_CONST_EXPR noting this fact and return the resulting | |
149 | expression. */ | |
150 | ||
151 | static inline tree | |
152 | remove_c_maybe_const_expr (tree expr) | |
153 | { | |
154 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
155 | return C_MAYBE_CONST_EXPR_EXPR (expr); | |
156 | else | |
157 | return expr; | |
158 | } | |
159 | ||
f13c9b2c AP |
160 | \f/* This is a cache to hold if two types are compatible or not. */ |
161 | ||
162 | struct tagged_tu_seen_cache { | |
163 | const struct tagged_tu_seen_cache * next; | |
58f9752a KG |
164 | const_tree t1; |
165 | const_tree t2; | |
f13c9b2c AP |
166 | /* The return value of tagged_types_tu_compatible_p if we had seen |
167 | these two types already. */ | |
168 | int val; | |
169 | }; | |
170 | ||
171 | static const struct tagged_tu_seen_cache * tagged_tu_seen_base; | |
172 | static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); | |
173 | ||
400fbf9f JW |
174 | /* Do `exp = require_complete_type (exp);' to make sure exp |
175 | does not have an incomplete type. (That includes void types.) */ | |
176 | ||
177 | tree | |
2f6e4e97 | 178 | require_complete_type (tree value) |
400fbf9f JW |
179 | { |
180 | tree type = TREE_TYPE (value); | |
181 | ||
c3d5c3fa | 182 | if (value == error_mark_node || type == error_mark_node) |
ea0f786b CB |
183 | return error_mark_node; |
184 | ||
400fbf9f | 185 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 186 | if (COMPLETE_TYPE_P (type)) |
400fbf9f JW |
187 | return value; |
188 | ||
7a228918 | 189 | c_incomplete_type_error (value, type); |
400fbf9f JW |
190 | return error_mark_node; |
191 | } | |
192 | ||
193 | /* Print an error message for invalid use of an incomplete type. | |
194 | VALUE is the expression that was used (or 0 if that isn't known) | |
195 | and TYPE is the type that was invalid. */ | |
196 | ||
197 | void | |
ac7d7749 | 198 | c_incomplete_type_error (const_tree value, const_tree type) |
400fbf9f | 199 | { |
5d5993dd | 200 | const char *type_code_string; |
400fbf9f JW |
201 | |
202 | /* Avoid duplicate error message. */ | |
203 | if (TREE_CODE (type) == ERROR_MARK) | |
204 | return; | |
205 | ||
206 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
207 | || TREE_CODE (value) == PARM_DECL)) | |
c51a1ba9 | 208 | error ("%qD has an incomplete type", value); |
400fbf9f JW |
209 | else |
210 | { | |
211 | retry: | |
212 | /* We must print an error message. Be clever about what it says. */ | |
213 | ||
214 | switch (TREE_CODE (type)) | |
215 | { | |
216 | case RECORD_TYPE: | |
ab87f8c8 | 217 | type_code_string = "struct"; |
400fbf9f JW |
218 | break; |
219 | ||
220 | case UNION_TYPE: | |
ab87f8c8 | 221 | type_code_string = "union"; |
400fbf9f JW |
222 | break; |
223 | ||
224 | case ENUMERAL_TYPE: | |
ab87f8c8 | 225 | type_code_string = "enum"; |
400fbf9f JW |
226 | break; |
227 | ||
228 | case VOID_TYPE: | |
229 | error ("invalid use of void expression"); | |
230 | return; | |
231 | ||
232 | case ARRAY_TYPE: | |
233 | if (TYPE_DOMAIN (type)) | |
234 | { | |
fba78abb RH |
235 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) |
236 | { | |
237 | error ("invalid use of flexible array member"); | |
238 | return; | |
239 | } | |
400fbf9f JW |
240 | type = TREE_TYPE (type); |
241 | goto retry; | |
242 | } | |
243 | error ("invalid use of array with unspecified bounds"); | |
244 | return; | |
245 | ||
246 | default: | |
366de0ce | 247 | gcc_unreachable (); |
400fbf9f JW |
248 | } |
249 | ||
250 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
c51a1ba9 JM |
251 | error ("invalid use of undefined type %<%s %E%>", |
252 | type_code_string, TYPE_NAME (type)); | |
400fbf9f JW |
253 | else |
254 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
c51a1ba9 | 255 | error ("invalid use of incomplete typedef %qD", TYPE_NAME (type)); |
400fbf9f JW |
256 | } |
257 | } | |
258 | ||
ab393bf1 NB |
259 | /* Given a type, apply default promotions wrt unnamed function |
260 | arguments and return the new type. */ | |
261 | ||
262 | tree | |
2f6e4e97 | 263 | c_type_promotes_to (tree type) |
ab393bf1 NB |
264 | { |
265 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
266 | return double_type_node; | |
267 | ||
268 | if (c_promoting_integer_type_p (type)) | |
269 | { | |
270 | /* Preserve unsignedness if not really getting any wider. */ | |
8df83eae | 271 | if (TYPE_UNSIGNED (type) |
c22cacf3 MS |
272 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) |
273 | return unsigned_type_node; | |
ab393bf1 NB |
274 | return integer_type_node; |
275 | } | |
276 | ||
277 | return type; | |
278 | } | |
279 | ||
36c5e70a BE |
280 | /* Return true if between two named address spaces, whether there is a superset |
281 | named address space that encompasses both address spaces. If there is a | |
282 | superset, return which address space is the superset. */ | |
283 | ||
284 | static bool | |
285 | addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) | |
286 | { | |
287 | if (as1 == as2) | |
288 | { | |
289 | *common = as1; | |
290 | return true; | |
291 | } | |
292 | else if (targetm.addr_space.subset_p (as1, as2)) | |
293 | { | |
294 | *common = as2; | |
295 | return true; | |
296 | } | |
297 | else if (targetm.addr_space.subset_p (as2, as1)) | |
298 | { | |
299 | *common = as1; | |
300 | return true; | |
301 | } | |
302 | else | |
303 | return false; | |
304 | } | |
305 | ||
400fbf9f JW |
306 | /* Return a variant of TYPE which has all the type qualifiers of LIKE |
307 | as well as those of TYPE. */ | |
308 | ||
309 | static tree | |
2f6e4e97 | 310 | qualify_type (tree type, tree like) |
400fbf9f | 311 | { |
36c5e70a BE |
312 | addr_space_t as_type = TYPE_ADDR_SPACE (type); |
313 | addr_space_t as_like = TYPE_ADDR_SPACE (like); | |
314 | addr_space_t as_common; | |
315 | ||
316 | /* If the two named address spaces are different, determine the common | |
317 | superset address space. If there isn't one, raise an error. */ | |
318 | if (!addr_space_superset (as_type, as_like, &as_common)) | |
319 | { | |
320 | as_common = as_type; | |
321 | error ("%qT and %qT are in disjoint named address spaces", | |
322 | type, like); | |
323 | } | |
324 | ||
2f6e4e97 | 325 | return c_build_qualified_type (type, |
36c5e70a BE |
326 | TYPE_QUALS_NO_ADDR_SPACE (type) |
327 | | TYPE_QUALS_NO_ADDR_SPACE (like) | |
328 | | ENCODE_QUAL_ADDR_SPACE (as_common)); | |
400fbf9f | 329 | } |
52ffd86e MS |
330 | |
331 | /* Return true iff the given tree T is a variable length array. */ | |
332 | ||
333 | bool | |
ac7d7749 | 334 | c_vla_type_p (const_tree t) |
52ffd86e MS |
335 | { |
336 | if (TREE_CODE (t) == ARRAY_TYPE | |
337 | && C_TYPE_VARIABLE_SIZE (t)) | |
338 | return true; | |
339 | return false; | |
340 | } | |
400fbf9f | 341 | \f |
10bc1b1b | 342 | /* Return the composite type of two compatible types. |
5305f6d7 | 343 | |
10bc1b1b JM |
344 | We assume that comptypes has already been done and returned |
345 | nonzero; if that isn't so, this may crash. In particular, we | |
346 | assume that qualifiers match. */ | |
400fbf9f JW |
347 | |
348 | tree | |
10bc1b1b | 349 | composite_type (tree t1, tree t2) |
400fbf9f | 350 | { |
b3694847 SS |
351 | enum tree_code code1; |
352 | enum tree_code code2; | |
4b027d16 | 353 | tree attributes; |
400fbf9f JW |
354 | |
355 | /* Save time if the two types are the same. */ | |
356 | ||
357 | if (t1 == t2) return t1; | |
358 | ||
359 | /* If one type is nonsense, use the other. */ | |
360 | if (t1 == error_mark_node) | |
361 | return t2; | |
362 | if (t2 == error_mark_node) | |
363 | return t1; | |
364 | ||
10bc1b1b JM |
365 | code1 = TREE_CODE (t1); |
366 | code2 = TREE_CODE (t2); | |
367 | ||
d9525bec | 368 | /* Merge the attributes. */ |
5fd9b178 | 369 | attributes = targetm.merge_type_attributes (t1, t2); |
4b027d16 | 370 | |
10bc1b1b JM |
371 | /* If one is an enumerated type and the other is the compatible |
372 | integer type, the composite type might be either of the two | |
373 | (DR#013 question 3). For consistency, use the enumerated type as | |
374 | the composite type. */ | |
400fbf9f | 375 | |
10bc1b1b JM |
376 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) |
377 | return t1; | |
378 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
379 | return t2; | |
75326e8c | 380 | |
366de0ce | 381 | gcc_assert (code1 == code2); |
b6a10c9f | 382 | |
400fbf9f JW |
383 | switch (code1) |
384 | { | |
400fbf9f | 385 | case POINTER_TYPE: |
10bc1b1b | 386 | /* For two pointers, do this recursively on the target type. */ |
400fbf9f | 387 | { |
3932261a MM |
388 | tree pointed_to_1 = TREE_TYPE (t1); |
389 | tree pointed_to_2 = TREE_TYPE (t2); | |
10bc1b1b | 390 | tree target = composite_type (pointed_to_1, pointed_to_2); |
3db684fb | 391 | t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false); |
fe7080d2 AP |
392 | t1 = build_type_attribute_variant (t1, attributes); |
393 | return qualify_type (t1, t2); | |
400fbf9f | 394 | } |
400fbf9f JW |
395 | |
396 | case ARRAY_TYPE: | |
397 | { | |
10bc1b1b | 398 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
46df2823 JM |
399 | int quals; |
400 | tree unqual_elt; | |
ca8bdb78 JM |
401 | tree d1 = TYPE_DOMAIN (t1); |
402 | tree d2 = TYPE_DOMAIN (t2); | |
403 | bool d1_variable, d2_variable; | |
404 | bool d1_zero, d2_zero; | |
f6294de7 | 405 | bool t1_complete, t2_complete; |
46df2823 | 406 | |
de46b2fe | 407 | /* We should not have any type quals on arrays at all. */ |
36c5e70a BE |
408 | gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) |
409 | && !TYPE_QUALS_NO_ADDR_SPACE (t2)); | |
c22cacf3 | 410 | |
f6294de7 JM |
411 | t1_complete = COMPLETE_TYPE_P (t1); |
412 | t2_complete = COMPLETE_TYPE_P (t2); | |
413 | ||
ca8bdb78 JM |
414 | d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); |
415 | d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); | |
416 | ||
417 | d1_variable = (!d1_zero | |
418 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
419 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
420 | d2_variable = (!d2_zero | |
421 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
422 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
423 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
424 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
ca8bdb78 | 425 | |
400fbf9f | 426 | /* Save space: see if the result is identical to one of the args. */ |
ca8bdb78 JM |
427 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) |
428 | && (d2_variable || d2_zero || !d1_variable)) | |
4b027d16 | 429 | return build_type_attribute_variant (t1, attributes); |
ca8bdb78 JM |
430 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) |
431 | && (d1_variable || d1_zero || !d2_variable)) | |
4b027d16 | 432 | return build_type_attribute_variant (t2, attributes); |
c22cacf3 | 433 | |
de46b2fe AP |
434 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) |
435 | return build_type_attribute_variant (t1, attributes); | |
436 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
437 | return build_type_attribute_variant (t2, attributes); | |
c22cacf3 | 438 | |
46df2823 JM |
439 | /* Merge the element types, and have a size if either arg has |
440 | one. We may have qualifiers on the element types. To set | |
441 | up TYPE_MAIN_VARIANT correctly, we need to form the | |
442 | composite of the unqualified types and add the qualifiers | |
443 | back at the end. */ | |
444 | quals = TYPE_QUALS (strip_array_types (elt)); | |
445 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
446 | t1 = build_array_type (unqual_elt, | |
ca8bdb78 JM |
447 | TYPE_DOMAIN ((TYPE_DOMAIN (t1) |
448 | && (d2_variable | |
449 | || d2_zero | |
450 | || !d1_variable)) | |
451 | ? t1 | |
452 | : t2)); | |
f6294de7 JM |
453 | /* Ensure a composite type involving a zero-length array type |
454 | is a zero-length type not an incomplete type. */ | |
455 | if (d1_zero && d2_zero | |
456 | && (t1_complete || t2_complete) | |
457 | && !COMPLETE_TYPE_P (t1)) | |
458 | { | |
459 | TYPE_SIZE (t1) = bitsize_zero_node; | |
460 | TYPE_SIZE_UNIT (t1) = size_zero_node; | |
461 | } | |
46df2823 | 462 | t1 = c_build_qualified_type (t1, quals); |
de46b2fe | 463 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
464 | } |
465 | ||
fcb99e7b JJ |
466 | case ENUMERAL_TYPE: |
467 | case RECORD_TYPE: | |
468 | case UNION_TYPE: | |
469 | if (attributes != NULL) | |
470 | { | |
471 | /* Try harder not to create a new aggregate type. */ | |
472 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
473 | return t1; | |
474 | if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
475 | return t2; | |
476 | } | |
477 | return build_type_attribute_variant (t1, attributes); | |
478 | ||
400fbf9f JW |
479 | case FUNCTION_TYPE: |
480 | /* Function types: prefer the one that specified arg types. | |
481 | If both do, merge the arg types. Also merge the return types. */ | |
482 | { | |
10bc1b1b | 483 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
400fbf9f JW |
484 | tree p1 = TYPE_ARG_TYPES (t1); |
485 | tree p2 = TYPE_ARG_TYPES (t2); | |
486 | int len; | |
487 | tree newargs, n; | |
488 | int i; | |
489 | ||
490 | /* Save space: see if the result is identical to one of the args. */ | |
3f75a254 | 491 | if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) |
4b027d16 | 492 | return build_type_attribute_variant (t1, attributes); |
3f75a254 | 493 | if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) |
4b027d16 | 494 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
495 | |
496 | /* Simple way if one arg fails to specify argument types. */ | |
497 | if (TYPE_ARG_TYPES (t1) == 0) | |
4b027d16 | 498 | { |
fe7080d2 AP |
499 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); |
500 | t1 = build_type_attribute_variant (t1, attributes); | |
501 | return qualify_type (t1, t2); | |
4b027d16 | 502 | } |
400fbf9f | 503 | if (TYPE_ARG_TYPES (t2) == 0) |
4b027d16 RK |
504 | { |
505 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
fe7080d2 AP |
506 | t1 = build_type_attribute_variant (t1, attributes); |
507 | return qualify_type (t1, t2); | |
4b027d16 | 508 | } |
400fbf9f JW |
509 | |
510 | /* If both args specify argument types, we must merge the two | |
511 | lists, argument by argument. */ | |
2f4e8f2b | 512 | |
400fbf9f JW |
513 | len = list_length (p1); |
514 | newargs = 0; | |
515 | ||
516 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 517 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
518 | |
519 | n = newargs; | |
520 | ||
521 | for (; p1; | |
522 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
523 | { | |
524 | /* A null type means arg type is not specified. | |
525 | Take whatever the other function type has. */ | |
526 | if (TREE_VALUE (p1) == 0) | |
527 | { | |
528 | TREE_VALUE (n) = TREE_VALUE (p2); | |
529 | goto parm_done; | |
530 | } | |
531 | if (TREE_VALUE (p2) == 0) | |
532 | { | |
533 | TREE_VALUE (n) = TREE_VALUE (p1); | |
534 | goto parm_done; | |
535 | } | |
2f6e4e97 | 536 | |
400fbf9f JW |
537 | /* Given wait (union {union wait *u; int *i} *) |
538 | and wait (union wait *), | |
539 | prefer union wait * as type of parm. */ | |
540 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
541 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
542 | { | |
543 | tree memb; | |
58cb41e6 JJ |
544 | tree mv2 = TREE_VALUE (p2); |
545 | if (mv2 && mv2 != error_mark_node | |
546 | && TREE_CODE (mv2) != ARRAY_TYPE) | |
547 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
400fbf9f | 548 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); |
910ad8de | 549 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
550 | { |
551 | tree mv3 = TREE_TYPE (memb); | |
552 | if (mv3 && mv3 != error_mark_node | |
553 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
554 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
555 | if (comptypes (mv3, mv2)) | |
556 | { | |
557 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
558 | TREE_VALUE (p2)); | |
c1771a20 | 559 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 560 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
561 | goto parm_done; |
562 | } | |
563 | } | |
400fbf9f JW |
564 | } |
565 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
566 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
567 | { | |
568 | tree memb; | |
58cb41e6 JJ |
569 | tree mv1 = TREE_VALUE (p1); |
570 | if (mv1 && mv1 != error_mark_node | |
571 | && TREE_CODE (mv1) != ARRAY_TYPE) | |
572 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
400fbf9f | 573 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); |
910ad8de | 574 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
575 | { |
576 | tree mv3 = TREE_TYPE (memb); | |
577 | if (mv3 && mv3 != error_mark_node | |
578 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
579 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
580 | if (comptypes (mv3, mv1)) | |
581 | { | |
582 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
583 | TREE_VALUE (p1)); | |
c1771a20 | 584 | pedwarn (input_location, OPT_Wpedantic, |
fcf73884 | 585 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
586 | goto parm_done; |
587 | } | |
588 | } | |
400fbf9f | 589 | } |
10bc1b1b | 590 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); |
400fbf9f JW |
591 | parm_done: ; |
592 | } | |
593 | ||
4b027d16 | 594 | t1 = build_function_type (valtype, newargs); |
fe7080d2 | 595 | t1 = qualify_type (t1, t2); |
0f41302f | 596 | /* ... falls through ... */ |
400fbf9f JW |
597 | } |
598 | ||
599 | default: | |
4b027d16 | 600 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
601 | } |
602 | ||
603 | } | |
10bc1b1b JM |
604 | |
605 | /* Return the type of a conditional expression between pointers to | |
606 | possibly differently qualified versions of compatible types. | |
607 | ||
608 | We assume that comp_target_types has already been done and returned | |
609 | nonzero; if that isn't so, this may crash. */ | |
610 | ||
611 | static tree | |
612 | common_pointer_type (tree t1, tree t2) | |
613 | { | |
614 | tree attributes; | |
46df2823 JM |
615 | tree pointed_to_1, mv1; |
616 | tree pointed_to_2, mv2; | |
10bc1b1b | 617 | tree target; |
eb1387a0 | 618 | unsigned target_quals; |
36c5e70a BE |
619 | addr_space_t as1, as2, as_common; |
620 | int quals1, quals2; | |
10bc1b1b JM |
621 | |
622 | /* Save time if the two types are the same. */ | |
623 | ||
624 | if (t1 == t2) return t1; | |
625 | ||
626 | /* If one type is nonsense, use the other. */ | |
627 | if (t1 == error_mark_node) | |
628 | return t2; | |
629 | if (t2 == error_mark_node) | |
630 | return t1; | |
631 | ||
366de0ce | 632 | gcc_assert (TREE_CODE (t1) == POINTER_TYPE |
c22cacf3 | 633 | && TREE_CODE (t2) == POINTER_TYPE); |
10bc1b1b JM |
634 | |
635 | /* Merge the attributes. */ | |
636 | attributes = targetm.merge_type_attributes (t1, t2); | |
637 | ||
638 | /* Find the composite type of the target types, and combine the | |
46df2823 JM |
639 | qualifiers of the two types' targets. Do not lose qualifiers on |
640 | array element types by taking the TYPE_MAIN_VARIANT. */ | |
641 | mv1 = pointed_to_1 = TREE_TYPE (t1); | |
642 | mv2 = pointed_to_2 = TREE_TYPE (t2); | |
643 | if (TREE_CODE (mv1) != ARRAY_TYPE) | |
644 | mv1 = TYPE_MAIN_VARIANT (pointed_to_1); | |
645 | if (TREE_CODE (mv2) != ARRAY_TYPE) | |
646 | mv2 = TYPE_MAIN_VARIANT (pointed_to_2); | |
647 | target = composite_type (mv1, mv2); | |
eb1387a0 RG |
648 | |
649 | /* For function types do not merge const qualifiers, but drop them | |
650 | if used inconsistently. The middle-end uses these to mark const | |
651 | and noreturn functions. */ | |
36c5e70a BE |
652 | quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1); |
653 | quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2); | |
654 | ||
eb1387a0 | 655 | if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) |
36c5e70a | 656 | target_quals = (quals1 & quals2); |
eb1387a0 | 657 | else |
36c5e70a BE |
658 | target_quals = (quals1 | quals2); |
659 | ||
660 | /* If the two named address spaces are different, determine the common | |
661 | superset address space. This is guaranteed to exist due to the | |
662 | assumption that comp_target_type returned non-zero. */ | |
663 | as1 = TYPE_ADDR_SPACE (pointed_to_1); | |
664 | as2 = TYPE_ADDR_SPACE (pointed_to_2); | |
665 | if (!addr_space_superset (as1, as2, &as_common)) | |
666 | gcc_unreachable (); | |
667 | ||
668 | target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); | |
669 | ||
eb1387a0 | 670 | t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); |
10bc1b1b JM |
671 | return build_type_attribute_variant (t1, attributes); |
672 | } | |
673 | ||
674 | /* Return the common type for two arithmetic types under the usual | |
675 | arithmetic conversions. The default conversions have already been | |
676 | applied, and enumerated types converted to their compatible integer | |
677 | types. The resulting type is unqualified and has no attributes. | |
678 | ||
679 | This is the type for the result of most arithmetic operations | |
680 | if the operands have the given two types. */ | |
681 | ||
ccf7f880 JJ |
682 | static tree |
683 | c_common_type (tree t1, tree t2) | |
10bc1b1b JM |
684 | { |
685 | enum tree_code code1; | |
686 | enum tree_code code2; | |
687 | ||
688 | /* If one type is nonsense, use the other. */ | |
689 | if (t1 == error_mark_node) | |
690 | return t2; | |
691 | if (t2 == error_mark_node) | |
692 | return t1; | |
693 | ||
694 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
695 | t1 = TYPE_MAIN_VARIANT (t1); | |
696 | ||
697 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
698 | t2 = TYPE_MAIN_VARIANT (t2); | |
699 | ||
700 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
701 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
702 | ||
703 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
704 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
705 | ||
706 | /* Save time if the two types are the same. */ | |
707 | ||
708 | if (t1 == t2) return t1; | |
709 | ||
710 | code1 = TREE_CODE (t1); | |
711 | code2 = TREE_CODE (t2); | |
712 | ||
366de0ce | 713 | gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE |
ab22c1fa CF |
714 | || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE |
715 | || code1 == INTEGER_TYPE); | |
366de0ce | 716 | gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE |
ab22c1fa CF |
717 | || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE |
718 | || code2 == INTEGER_TYPE); | |
10bc1b1b | 719 | |
5fc89bfd JJ |
720 | /* When one operand is a decimal float type, the other operand cannot be |
721 | a generic float type or a complex type. We also disallow vector types | |
722 | here. */ | |
723 | if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) | |
724 | && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) | |
725 | { | |
726 | if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) | |
727 | { | |
728 | error ("can%'t mix operands of decimal float and vector types"); | |
729 | return error_mark_node; | |
730 | } | |
731 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
732 | { | |
733 | error ("can%'t mix operands of decimal float and complex types"); | |
734 | return error_mark_node; | |
735 | } | |
736 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
737 | { | |
738 | error ("can%'t mix operands of decimal float and other float types"); | |
739 | return error_mark_node; | |
740 | } | |
741 | } | |
742 | ||
10bc1b1b JM |
743 | /* If one type is a vector type, return that type. (How the usual |
744 | arithmetic conversions apply to the vector types extension is not | |
745 | precisely specified.) */ | |
746 | if (code1 == VECTOR_TYPE) | |
747 | return t1; | |
748 | ||
749 | if (code2 == VECTOR_TYPE) | |
750 | return t2; | |
751 | ||
752 | /* If one type is complex, form the common type of the non-complex | |
753 | components, then make that complex. Use T1 or T2 if it is the | |
754 | required type. */ | |
755 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
756 | { | |
757 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
758 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
ccf7f880 | 759 | tree subtype = c_common_type (subtype1, subtype2); |
10bc1b1b JM |
760 | |
761 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
762 | return t1; | |
763 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
764 | return t2; | |
765 | else | |
766 | return build_complex_type (subtype); | |
767 | } | |
768 | ||
769 | /* If only one is real, use it as the result. */ | |
770 | ||
771 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
772 | return t1; | |
773 | ||
774 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
775 | return t2; | |
776 | ||
9a8ce21f JG |
777 | /* If both are real and either are decimal floating point types, use |
778 | the decimal floating point type with the greater precision. */ | |
779 | ||
780 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
781 | { | |
782 | if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node | |
783 | || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) | |
784 | return dfloat128_type_node; | |
785 | else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node | |
786 | || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) | |
787 | return dfloat64_type_node; | |
788 | else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node | |
789 | || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) | |
790 | return dfloat32_type_node; | |
791 | } | |
792 | ||
ab22c1fa CF |
793 | /* Deal with fixed-point types. */ |
794 | if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) | |
795 | { | |
796 | unsigned int unsignedp = 0, satp = 0; | |
797 | enum machine_mode m1, m2; | |
798 | unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; | |
799 | ||
800 | m1 = TYPE_MODE (t1); | |
801 | m2 = TYPE_MODE (t2); | |
802 | ||
803 | /* If one input type is saturating, the result type is saturating. */ | |
804 | if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) | |
805 | satp = 1; | |
806 | ||
807 | /* If both fixed-point types are unsigned, the result type is unsigned. | |
808 | When mixing fixed-point and integer types, follow the sign of the | |
809 | fixed-point type. | |
810 | Otherwise, the result type is signed. */ | |
811 | if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) | |
812 | && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) | |
813 | || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE | |
814 | && TYPE_UNSIGNED (t1)) | |
815 | || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE | |
816 | && TYPE_UNSIGNED (t2))) | |
817 | unsignedp = 1; | |
818 | ||
819 | /* The result type is signed. */ | |
820 | if (unsignedp == 0) | |
821 | { | |
822 | /* If the input type is unsigned, we need to convert to the | |
823 | signed type. */ | |
824 | if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) | |
825 | { | |
d75d71e0 | 826 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
827 | if (GET_MODE_CLASS (m1) == MODE_UFRACT) |
828 | mclass = MODE_FRACT; | |
829 | else if (GET_MODE_CLASS (m1) == MODE_UACCUM) | |
830 | mclass = MODE_ACCUM; | |
831 | else | |
832 | gcc_unreachable (); | |
833 | m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); | |
834 | } | |
835 | if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) | |
836 | { | |
d75d71e0 | 837 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
838 | if (GET_MODE_CLASS (m2) == MODE_UFRACT) |
839 | mclass = MODE_FRACT; | |
840 | else if (GET_MODE_CLASS (m2) == MODE_UACCUM) | |
841 | mclass = MODE_ACCUM; | |
842 | else | |
843 | gcc_unreachable (); | |
844 | m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); | |
845 | } | |
846 | } | |
847 | ||
848 | if (code1 == FIXED_POINT_TYPE) | |
849 | { | |
850 | fbit1 = GET_MODE_FBIT (m1); | |
851 | ibit1 = GET_MODE_IBIT (m1); | |
852 | } | |
853 | else | |
854 | { | |
855 | fbit1 = 0; | |
856 | /* Signed integers need to subtract one sign bit. */ | |
857 | ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); | |
858 | } | |
859 | ||
860 | if (code2 == FIXED_POINT_TYPE) | |
861 | { | |
862 | fbit2 = GET_MODE_FBIT (m2); | |
863 | ibit2 = GET_MODE_IBIT (m2); | |
864 | } | |
865 | else | |
866 | { | |
867 | fbit2 = 0; | |
868 | /* Signed integers need to subtract one sign bit. */ | |
869 | ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); | |
870 | } | |
871 | ||
872 | max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; | |
873 | max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; | |
874 | return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, | |
875 | satp); | |
876 | } | |
877 | ||
10bc1b1b JM |
878 | /* Both real or both integers; use the one with greater precision. */ |
879 | ||
880 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
881 | return t1; | |
882 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
883 | return t2; | |
884 | ||
885 | /* Same precision. Prefer long longs to longs to ints when the | |
886 | same precision, following the C99 rules on integer type rank | |
887 | (which are equivalent to the C90 rules for C90 types). */ | |
888 | ||
889 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
890 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
891 | return long_long_unsigned_type_node; | |
892 | ||
893 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
894 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
895 | { | |
896 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
897 | return long_long_unsigned_type_node; | |
898 | else | |
c22cacf3 | 899 | return long_long_integer_type_node; |
10bc1b1b JM |
900 | } |
901 | ||
902 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
903 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
904 | return long_unsigned_type_node; | |
905 | ||
906 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
907 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
908 | { | |
909 | /* But preserve unsignedness from the other type, | |
910 | since long cannot hold all the values of an unsigned int. */ | |
911 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
912 | return long_unsigned_type_node; | |
913 | else | |
914 | return long_integer_type_node; | |
915 | } | |
916 | ||
917 | /* Likewise, prefer long double to double even if same size. */ | |
918 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
919 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
920 | return long_double_type_node; | |
921 | ||
922 | /* Otherwise prefer the unsigned one. */ | |
923 | ||
924 | if (TYPE_UNSIGNED (t1)) | |
925 | return t1; | |
926 | else | |
927 | return t2; | |
928 | } | |
400fbf9f | 929 | \f |
5922c215 JM |
930 | /* Wrapper around c_common_type that is used by c-common.c and other |
931 | front end optimizations that remove promotions. ENUMERAL_TYPEs | |
b2232745 RS |
932 | are allowed here and are converted to their compatible integer types. |
933 | BOOLEAN_TYPEs are allowed here and return either boolean_type_node or | |
934 | preferably a non-Boolean type as the common type. */ | |
ccf7f880 JJ |
935 | tree |
936 | common_type (tree t1, tree t2) | |
937 | { | |
938 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
939 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); | |
940 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
941 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); | |
b2232745 RS |
942 | |
943 | /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ | |
944 | if (TREE_CODE (t1) == BOOLEAN_TYPE | |
945 | && TREE_CODE (t2) == BOOLEAN_TYPE) | |
946 | return boolean_type_node; | |
947 | ||
948 | /* If either type is BOOLEAN_TYPE, then return the other. */ | |
949 | if (TREE_CODE (t1) == BOOLEAN_TYPE) | |
950 | return t2; | |
951 | if (TREE_CODE (t2) == BOOLEAN_TYPE) | |
952 | return t1; | |
953 | ||
ccf7f880 JJ |
954 | return c_common_type (t1, t2); |
955 | } | |
f13c9b2c | 956 | |
400fbf9f JW |
957 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
958 | or various other operations. Return 2 if they are compatible | |
959 | but a warning may be needed if you use them together. */ | |
960 | ||
961 | int | |
132da1a5 | 962 | comptypes (tree type1, tree type2) |
f13c9b2c AP |
963 | { |
964 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
965 | int val; | |
966 | ||
dc5027f4 | 967 | val = comptypes_internal (type1, type2, NULL, NULL); |
744aa42f ILT |
968 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
969 | ||
970 | return val; | |
971 | } | |
972 | ||
973 | /* Like comptypes, but if it returns non-zero because enum and int are | |
974 | compatible, it sets *ENUM_AND_INT_P to true. */ | |
975 | ||
976 | static int | |
977 | comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) | |
978 | { | |
979 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
980 | int val; | |
981 | ||
dc5027f4 JM |
982 | val = comptypes_internal (type1, type2, enum_and_int_p, NULL); |
983 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
984 | ||
985 | return val; | |
986 | } | |
987 | ||
988 | /* Like comptypes, but if it returns nonzero for different types, it | |
989 | sets *DIFFERENT_TYPES_P to true. */ | |
990 | ||
991 | int | |
992 | comptypes_check_different_types (tree type1, tree type2, | |
993 | bool *different_types_p) | |
994 | { | |
995 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
996 | int val; | |
997 | ||
998 | val = comptypes_internal (type1, type2, NULL, different_types_p); | |
f13c9b2c | 999 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
c22cacf3 | 1000 | |
f13c9b2c | 1001 | return val; |
c22cacf3 MS |
1002 | } |
1003 | \f | |
f13c9b2c AP |
1004 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
1005 | or various other operations. Return 2 if they are compatible | |
744aa42f ILT |
1006 | but a warning may be needed if you use them together. If |
1007 | ENUM_AND_INT_P is not NULL, and one type is an enum and the other a | |
1008 | compatible integer type, then this sets *ENUM_AND_INT_P to true; | |
dc5027f4 JM |
1009 | *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not |
1010 | NULL, and the types are compatible but different enough not to be | |
48b0b196 | 1011 | permitted in C11 typedef redeclarations, then this sets |
dc5027f4 JM |
1012 | *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to |
1013 | false, but may or may not be set if the types are incompatible. | |
1014 | This differs from comptypes, in that we don't free the seen | |
1015 | types. */ | |
f13c9b2c AP |
1016 | |
1017 | static int | |
dc5027f4 JM |
1018 | comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, |
1019 | bool *different_types_p) | |
400fbf9f | 1020 | { |
58f9752a KG |
1021 | const_tree t1 = type1; |
1022 | const_tree t2 = type2; | |
4b027d16 | 1023 | int attrval, val; |
400fbf9f JW |
1024 | |
1025 | /* Suppress errors caused by previously reported errors. */ | |
1026 | ||
8d47dfc5 RH |
1027 | if (t1 == t2 || !t1 || !t2 |
1028 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
400fbf9f JW |
1029 | return 1; |
1030 | ||
bca63328 JM |
1031 | /* Enumerated types are compatible with integer types, but this is |
1032 | not transitive: two enumerated types in the same translation unit | |
1033 | are compatible with each other only if they are the same type. */ | |
400fbf9f | 1034 | |
bca63328 | 1035 | if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) |
744aa42f ILT |
1036 | { |
1037 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); | |
dc5027f4 JM |
1038 | if (TREE_CODE (t2) != VOID_TYPE) |
1039 | { | |
1040 | if (enum_and_int_p != NULL) | |
1041 | *enum_and_int_p = true; | |
1042 | if (different_types_p != NULL) | |
1043 | *different_types_p = true; | |
1044 | } | |
744aa42f | 1045 | } |
bca63328 | 1046 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) |
744aa42f ILT |
1047 | { |
1048 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); | |
dc5027f4 JM |
1049 | if (TREE_CODE (t1) != VOID_TYPE) |
1050 | { | |
1051 | if (enum_and_int_p != NULL) | |
1052 | *enum_and_int_p = true; | |
1053 | if (different_types_p != NULL) | |
1054 | *different_types_p = true; | |
1055 | } | |
744aa42f | 1056 | } |
400fbf9f JW |
1057 | |
1058 | if (t1 == t2) | |
1059 | return 1; | |
1060 | ||
1061 | /* Different classes of types can't be compatible. */ | |
1062 | ||
3aeb3655 EC |
1063 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
1064 | return 0; | |
400fbf9f | 1065 | |
118a3a8b | 1066 | /* Qualifiers must match. C99 6.7.3p9 */ |
400fbf9f | 1067 | |
3932261a | 1068 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
400fbf9f JW |
1069 | return 0; |
1070 | ||
08632da2 RS |
1071 | /* Allow for two different type nodes which have essentially the same |
1072 | definition. Note that we already checked for equality of the type | |
38e01259 | 1073 | qualifiers (just above). */ |
400fbf9f | 1074 | |
46df2823 JM |
1075 | if (TREE_CODE (t1) != ARRAY_TYPE |
1076 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
400fbf9f JW |
1077 | return 1; |
1078 | ||
4b027d16 | 1079 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ |
ac9a30ae | 1080 | if (!(attrval = comp_type_attributes (t1, t2))) |
4b027d16 RK |
1081 | return 0; |
1082 | ||
1083 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1084 | val = 0; | |
1085 | ||
400fbf9f JW |
1086 | switch (TREE_CODE (t1)) |
1087 | { | |
1088 | case POINTER_TYPE: | |
106f5de5 UW |
1089 | /* Do not remove mode or aliasing information. */ |
1090 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1091 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) | |
1092 | break; | |
4b027d16 | 1093 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
744aa42f | 1094 | ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 | 1095 | enum_and_int_p, different_types_p)); |
4b027d16 | 1096 | break; |
400fbf9f JW |
1097 | |
1098 | case FUNCTION_TYPE: | |
dc5027f4 JM |
1099 | val = function_types_compatible_p (t1, t2, enum_and_int_p, |
1100 | different_types_p); | |
4b027d16 | 1101 | break; |
400fbf9f JW |
1102 | |
1103 | case ARRAY_TYPE: | |
1104 | { | |
400fbf9f JW |
1105 | tree d1 = TYPE_DOMAIN (t1); |
1106 | tree d2 = TYPE_DOMAIN (t2); | |
3f85558f RH |
1107 | bool d1_variable, d2_variable; |
1108 | bool d1_zero, d2_zero; | |
4b027d16 | 1109 | val = 1; |
400fbf9f JW |
1110 | |
1111 | /* Target types must match incl. qualifiers. */ | |
1112 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
744aa42f | 1113 | && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
dc5027f4 JM |
1114 | enum_and_int_p, |
1115 | different_types_p))) | |
400fbf9f JW |
1116 | return 0; |
1117 | ||
dc5027f4 JM |
1118 | if (different_types_p != NULL |
1119 | && (d1 == 0) != (d2 == 0)) | |
1120 | *different_types_p = true; | |
400fbf9f | 1121 | /* Sizes must match unless one is missing or variable. */ |
3f85558f | 1122 | if (d1 == 0 || d2 == 0 || d1 == d2) |
4b027d16 | 1123 | break; |
400fbf9f | 1124 | |
3f75a254 JM |
1125 | d1_zero = !TYPE_MAX_VALUE (d1); |
1126 | d2_zero = !TYPE_MAX_VALUE (d2); | |
3f85558f | 1127 | |
3f75a254 | 1128 | d1_variable = (!d1_zero |
3f85558f RH |
1129 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST |
1130 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
3f75a254 | 1131 | d2_variable = (!d2_zero |
3f85558f RH |
1132 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST |
1133 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
1134 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
1135 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
3f85558f | 1136 | |
dc5027f4 JM |
1137 | if (different_types_p != NULL |
1138 | && d1_variable != d2_variable) | |
1139 | *different_types_p = true; | |
3f85558f RH |
1140 | if (d1_variable || d2_variable) |
1141 | break; | |
1142 | if (d1_zero && d2_zero) | |
1143 | break; | |
1144 | if (d1_zero || d2_zero | |
3f75a254 JM |
1145 | || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) |
1146 | || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) | |
05bccae2 RK |
1147 | val = 0; |
1148 | ||
c22cacf3 | 1149 | break; |
400fbf9f JW |
1150 | } |
1151 | ||
d1bd0ded | 1152 | case ENUMERAL_TYPE: |
58393038 | 1153 | case RECORD_TYPE: |
d1bd0ded | 1154 | case UNION_TYPE: |
766beae1 | 1155 | if (val != 1 && !same_translation_unit_p (t1, t2)) |
c22cacf3 | 1156 | { |
fcb99e7b JJ |
1157 | tree a1 = TYPE_ATTRIBUTES (t1); |
1158 | tree a2 = TYPE_ATTRIBUTES (t2); | |
1159 | ||
1160 | if (! attribute_list_contained (a1, a2) | |
1161 | && ! attribute_list_contained (a2, a1)) | |
1162 | break; | |
1163 | ||
f13c9b2c | 1164 | if (attrval != 2) |
dc5027f4 JM |
1165 | return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, |
1166 | different_types_p); | |
1167 | val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, | |
1168 | different_types_p); | |
f13c9b2c | 1169 | } |
4b027d16 | 1170 | break; |
e9a25f70 | 1171 | |
62e1dfcf | 1172 | case VECTOR_TYPE: |
744aa42f ILT |
1173 | val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) |
1174 | && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
dc5027f4 | 1175 | enum_and_int_p, different_types_p)); |
62e1dfcf NC |
1176 | break; |
1177 | ||
e9a25f70 JL |
1178 | default: |
1179 | break; | |
400fbf9f | 1180 | } |
4b027d16 | 1181 | return attrval == 2 && val == 1 ? 2 : val; |
400fbf9f JW |
1182 | } |
1183 | ||
36c5e70a BE |
1184 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring |
1185 | their qualifiers, except for named address spaces. If the pointers point to | |
1186 | different named addresses, then we must determine if one address space is a | |
1187 | subset of the other. */ | |
400fbf9f JW |
1188 | |
1189 | static int | |
744aa42f | 1190 | comp_target_types (location_t location, tree ttl, tree ttr) |
400fbf9f | 1191 | { |
392202b0 | 1192 | int val; |
36c5e70a BE |
1193 | tree mvl = TREE_TYPE (ttl); |
1194 | tree mvr = TREE_TYPE (ttr); | |
1195 | addr_space_t asl = TYPE_ADDR_SPACE (mvl); | |
1196 | addr_space_t asr = TYPE_ADDR_SPACE (mvr); | |
1197 | addr_space_t as_common; | |
744aa42f | 1198 | bool enum_and_int_p; |
8b40563c | 1199 | |
36c5e70a BE |
1200 | /* Fail if pointers point to incompatible address spaces. */ |
1201 | if (!addr_space_superset (asl, asr, &as_common)) | |
1202 | return 0; | |
1203 | ||
46df2823 JM |
1204 | /* Do not lose qualifiers on element types of array types that are |
1205 | pointer targets by taking their TYPE_MAIN_VARIANT. */ | |
46df2823 JM |
1206 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
1207 | mvl = TYPE_MAIN_VARIANT (mvl); | |
1208 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
1209 | mvr = TYPE_MAIN_VARIANT (mvr); | |
744aa42f ILT |
1210 | enum_and_int_p = false; |
1211 | val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); | |
8b40563c | 1212 | |
fcf73884 | 1213 | if (val == 2) |
c1771a20 | 1214 | pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); |
744aa42f ILT |
1215 | |
1216 | if (val == 1 && enum_and_int_p && warn_cxx_compat) | |
1217 | warning_at (location, OPT_Wc___compat, | |
1218 | "pointer target types incompatible in C++"); | |
1219 | ||
400fbf9f JW |
1220 | return val; |
1221 | } | |
1222 | \f | |
1223 | /* Subroutines of `comptypes'. */ | |
1224 | ||
f75fbaf7 ZW |
1225 | /* Determine whether two trees derive from the same translation unit. |
1226 | If the CONTEXT chain ends in a null, that tree's context is still | |
1227 | being parsed, so if two trees have context chains ending in null, | |
766beae1 | 1228 | they're in the same translation unit. */ |
f75fbaf7 | 1229 | int |
58f9752a | 1230 | same_translation_unit_p (const_tree t1, const_tree t2) |
766beae1 ZW |
1231 | { |
1232 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
1233 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
1234 | { | |
6615c446 JO |
1235 | case tcc_declaration: |
1236 | t1 = DECL_CONTEXT (t1); break; | |
1237 | case tcc_type: | |
1238 | t1 = TYPE_CONTEXT (t1); break; | |
1239 | case tcc_exceptional: | |
1240 | t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ | |
366de0ce | 1241 | default: gcc_unreachable (); |
766beae1 ZW |
1242 | } |
1243 | ||
1244 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
1245 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
1246 | { | |
6615c446 JO |
1247 | case tcc_declaration: |
1248 | t2 = DECL_CONTEXT (t2); break; | |
1249 | case tcc_type: | |
1250 | t2 = TYPE_CONTEXT (t2); break; | |
1251 | case tcc_exceptional: | |
1252 | t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ | |
366de0ce | 1253 | default: gcc_unreachable (); |
766beae1 ZW |
1254 | } |
1255 | ||
1256 | return t1 == t2; | |
1257 | } | |
1258 | ||
f13c9b2c | 1259 | /* Allocate the seen two types, assuming that they are compatible. */ |
d1bd0ded | 1260 | |
f13c9b2c | 1261 | static struct tagged_tu_seen_cache * |
58f9752a | 1262 | alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) |
f13c9b2c | 1263 | { |
cceb1885 | 1264 | struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); |
f13c9b2c AP |
1265 | tu->next = tagged_tu_seen_base; |
1266 | tu->t1 = t1; | |
1267 | tu->t2 = t2; | |
c22cacf3 | 1268 | |
f13c9b2c | 1269 | tagged_tu_seen_base = tu; |
c22cacf3 | 1270 | |
f13c9b2c AP |
1271 | /* The C standard says that two structures in different translation |
1272 | units are compatible with each other only if the types of their | |
1273 | fields are compatible (among other things). We assume that they | |
1274 | are compatible until proven otherwise when building the cache. | |
1275 | An example where this can occur is: | |
1276 | struct a | |
1277 | { | |
1278 | struct a *next; | |
1279 | }; | |
1280 | If we are comparing this against a similar struct in another TU, | |
c83eecad | 1281 | and did not assume they were compatible, we end up with an infinite |
f13c9b2c AP |
1282 | loop. */ |
1283 | tu->val = 1; | |
1284 | return tu; | |
1285 | } | |
d1bd0ded | 1286 | |
f13c9b2c | 1287 | /* Free the seen types until we get to TU_TIL. */ |
d1bd0ded | 1288 | |
f13c9b2c AP |
1289 | static void |
1290 | free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) | |
1291 | { | |
1292 | const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; | |
1293 | while (tu != tu_til) | |
1294 | { | |
741ac903 KG |
1295 | const struct tagged_tu_seen_cache *const tu1 |
1296 | = (const struct tagged_tu_seen_cache *) tu; | |
f13c9b2c | 1297 | tu = tu1->next; |
b1d5455a | 1298 | free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); |
f13c9b2c AP |
1299 | } |
1300 | tagged_tu_seen_base = tu_til; | |
1301 | } | |
d1bd0ded GK |
1302 | |
1303 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
1304 | compatible. If the two types are not the same (which has been | |
1305 | checked earlier), this can only happen when multiple translation | |
1306 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
dc5027f4 JM |
1307 | rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1308 | comptypes_internal. */ | |
d1bd0ded GK |
1309 | |
1310 | static int | |
744aa42f | 1311 | tagged_types_tu_compatible_p (const_tree t1, const_tree t2, |
dc5027f4 | 1312 | bool *enum_and_int_p, bool *different_types_p) |
d1bd0ded GK |
1313 | { |
1314 | tree s1, s2; | |
1315 | bool needs_warning = false; | |
3aeb3655 | 1316 | |
d1bd0ded GK |
1317 | /* We have to verify that the tags of the types are the same. This |
1318 | is harder than it looks because this may be a typedef, so we have | |
1319 | to go look at the original type. It may even be a typedef of a | |
6de9cd9a DN |
1320 | typedef... |
1321 | In the case of compiler-created builtin structs the TYPE_DECL | |
1322 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
dea984dc ILT |
1323 | while (TYPE_NAME (t1) |
1324 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
1325 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
d1bd0ded GK |
1326 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); |
1327 | ||
dea984dc ILT |
1328 | while (TYPE_NAME (t2) |
1329 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
1330 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
d1bd0ded GK |
1331 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); |
1332 | ||
1333 | /* C90 didn't have the requirement that the two tags be the same. */ | |
1334 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
1335 | return 0; | |
3aeb3655 | 1336 | |
d1bd0ded GK |
1337 | /* C90 didn't say what happened if one or both of the types were |
1338 | incomplete; we choose to follow C99 rules here, which is that they | |
1339 | are compatible. */ | |
1340 | if (TYPE_SIZE (t1) == NULL | |
1341 | || TYPE_SIZE (t2) == NULL) | |
1342 | return 1; | |
3aeb3655 | 1343 | |
d1bd0ded | 1344 | { |
f13c9b2c | 1345 | const struct tagged_tu_seen_cache * tts_i; |
d1bd0ded GK |
1346 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) |
1347 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
f13c9b2c | 1348 | return tts_i->val; |
d1bd0ded | 1349 | } |
3aeb3655 | 1350 | |
d1bd0ded GK |
1351 | switch (TREE_CODE (t1)) |
1352 | { | |
1353 | case ENUMERAL_TYPE: | |
1354 | { | |
f13c9b2c | 1355 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
c22cacf3 MS |
1356 | /* Speed up the case where the type values are in the same order. */ |
1357 | tree tv1 = TYPE_VALUES (t1); | |
1358 | tree tv2 = TYPE_VALUES (t2); | |
3aeb3655 | 1359 | |
c22cacf3 | 1360 | if (tv1 == tv2) |
f13c9b2c AP |
1361 | { |
1362 | return 1; | |
1363 | } | |
3aeb3655 | 1364 | |
c22cacf3 MS |
1365 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) |
1366 | { | |
1367 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) | |
1368 | break; | |
1369 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
f13c9b2c | 1370 | { |
c22cacf3 | 1371 | tu->val = 0; |
f13c9b2c AP |
1372 | return 0; |
1373 | } | |
c22cacf3 | 1374 | } |
3aeb3655 | 1375 | |
c22cacf3 | 1376 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) |
f13c9b2c AP |
1377 | { |
1378 | return 1; | |
1379 | } | |
c22cacf3 | 1380 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) |
f13c9b2c AP |
1381 | { |
1382 | tu->val = 0; | |
1383 | return 0; | |
1384 | } | |
3aeb3655 | 1385 | |
d1bd0ded | 1386 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) |
f13c9b2c AP |
1387 | { |
1388 | tu->val = 0; | |
1389 | return 0; | |
1390 | } | |
3aeb3655 | 1391 | |
d1bd0ded GK |
1392 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) |
1393 | { | |
1394 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
1395 | if (s2 == NULL | |
1396 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
f13c9b2c AP |
1397 | { |
1398 | tu->val = 0; | |
1399 | return 0; | |
1400 | } | |
d1bd0ded GK |
1401 | } |
1402 | return 1; | |
1403 | } | |
1404 | ||
1405 | case UNION_TYPE: | |
1406 | { | |
f13c9b2c | 1407 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
d1bd0ded | 1408 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) |
f13c9b2c AP |
1409 | { |
1410 | tu->val = 0; | |
1411 | return 0; | |
1412 | } | |
c22cacf3 | 1413 | |
f13c9b2c AP |
1414 | /* Speed up the common case where the fields are in the same order. */ |
1415 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; | |
910ad8de | 1416 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
f13c9b2c AP |
1417 | { |
1418 | int result; | |
c22cacf3 | 1419 | |
3ae4d3cc | 1420 | if (DECL_NAME (s1) != DECL_NAME (s2)) |
f13c9b2c | 1421 | break; |
744aa42f | 1422 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1423 | enum_and_int_p, different_types_p); |
3ae4d3cc AO |
1424 | |
1425 | if (result != 1 && !DECL_NAME (s1)) | |
1426 | break; | |
f13c9b2c AP |
1427 | if (result == 0) |
1428 | { | |
1429 | tu->val = 0; | |
1430 | return 0; | |
1431 | } | |
1432 | if (result == 2) | |
1433 | needs_warning = true; | |
1434 | ||
1435 | if (TREE_CODE (s1) == FIELD_DECL | |
1436 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1437 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1438 | { | |
1439 | tu->val = 0; | |
1440 | return 0; | |
1441 | } | |
1442 | } | |
1443 | if (!s1 && !s2) | |
1444 | { | |
1445 | tu->val = needs_warning ? 2 : 1; | |
1446 | return tu->val; | |
1447 | } | |
d1bd0ded | 1448 | |
910ad8de | 1449 | for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1)) |
d1bd0ded GK |
1450 | { |
1451 | bool ok = false; | |
3aeb3655 | 1452 | |
910ad8de | 1453 | for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2)) |
3ae4d3cc AO |
1454 | if (DECL_NAME (s1) == DECL_NAME (s2)) |
1455 | { | |
1456 | int result; | |
1457 | ||
744aa42f | 1458 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 JM |
1459 | enum_and_int_p, |
1460 | different_types_p); | |
3ae4d3cc AO |
1461 | |
1462 | if (result != 1 && !DECL_NAME (s1)) | |
1463 | continue; | |
1464 | if (result == 0) | |
1465 | { | |
1466 | tu->val = 0; | |
1467 | return 0; | |
1468 | } | |
1469 | if (result == 2) | |
1470 | needs_warning = true; | |
1471 | ||
1472 | if (TREE_CODE (s1) == FIELD_DECL | |
1473 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1474 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
d1bd0ded | 1475 | break; |
3ae4d3cc AO |
1476 | |
1477 | ok = true; | |
1478 | break; | |
1479 | } | |
3f75a254 | 1480 | if (!ok) |
f13c9b2c AP |
1481 | { |
1482 | tu->val = 0; | |
1483 | return 0; | |
1484 | } | |
d1bd0ded | 1485 | } |
f13c9b2c AP |
1486 | tu->val = needs_warning ? 2 : 10; |
1487 | return tu->val; | |
d1bd0ded GK |
1488 | } |
1489 | ||
1490 | case RECORD_TYPE: | |
1491 | { | |
c22cacf3 | 1492 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
3aeb3655 EC |
1493 | |
1494 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
d1bd0ded | 1495 | s1 && s2; |
910ad8de | 1496 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) |
d1bd0ded GK |
1497 | { |
1498 | int result; | |
1499 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
1500 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
1501 | break; | |
744aa42f | 1502 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
dc5027f4 | 1503 | enum_and_int_p, different_types_p); |
d1bd0ded GK |
1504 | if (result == 0) |
1505 | break; | |
1506 | if (result == 2) | |
1507 | needs_warning = true; | |
3aeb3655 | 1508 | |
d1bd0ded GK |
1509 | if (TREE_CODE (s1) == FIELD_DECL |
1510 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1511 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1512 | break; | |
1513 | } | |
d1bd0ded | 1514 | if (s1 && s2) |
f13c9b2c AP |
1515 | tu->val = 0; |
1516 | else | |
1517 | tu->val = needs_warning ? 2 : 1; | |
1518 | return tu->val; | |
d1bd0ded GK |
1519 | } |
1520 | ||
1521 | default: | |
366de0ce | 1522 | gcc_unreachable (); |
d1bd0ded GK |
1523 | } |
1524 | } | |
1525 | ||
400fbf9f JW |
1526 | /* Return 1 if two function types F1 and F2 are compatible. |
1527 | If either type specifies no argument types, | |
1528 | the other must specify a fixed number of self-promoting arg types. | |
2f6e4e97 | 1529 | Otherwise, if one type specifies only the number of arguments, |
400fbf9f | 1530 | the other must specify that number of self-promoting arg types. |
744aa42f | 1531 | Otherwise, the argument types must match. |
dc5027f4 | 1532 | ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */ |
400fbf9f JW |
1533 | |
1534 | static int | |
744aa42f | 1535 | function_types_compatible_p (const_tree f1, const_tree f2, |
dc5027f4 | 1536 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1537 | { |
1538 | tree args1, args2; | |
1539 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1540 | int val = 1; | |
1541 | int val1; | |
a6fdc086 GK |
1542 | tree ret1, ret2; |
1543 | ||
1544 | ret1 = TREE_TYPE (f1); | |
1545 | ret2 = TREE_TYPE (f2); | |
1546 | ||
e508a019 JM |
1547 | /* 'volatile' qualifiers on a function's return type used to mean |
1548 | the function is noreturn. */ | |
1549 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
509c9d60 | 1550 | pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); |
a6fdc086 GK |
1551 | if (TYPE_VOLATILE (ret1)) |
1552 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
1553 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
1554 | if (TYPE_VOLATILE (ret2)) | |
1555 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
1556 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
dc5027f4 | 1557 | val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p); |
a6fdc086 | 1558 | if (val == 0) |
400fbf9f JW |
1559 | return 0; |
1560 | ||
1561 | args1 = TYPE_ARG_TYPES (f1); | |
1562 | args2 = TYPE_ARG_TYPES (f2); | |
1563 | ||
dc5027f4 JM |
1564 | if (different_types_p != NULL |
1565 | && (args1 == 0) != (args2 == 0)) | |
1566 | *different_types_p = true; | |
1567 | ||
400fbf9f JW |
1568 | /* An unspecified parmlist matches any specified parmlist |
1569 | whose argument types don't need default promotions. */ | |
1570 | ||
1571 | if (args1 == 0) | |
1572 | { | |
1573 | if (!self_promoting_args_p (args2)) | |
1574 | return 0; | |
1575 | /* If one of these types comes from a non-prototype fn definition, | |
1576 | compare that with the other type's arglist. | |
3176a0c2 | 1577 | If they don't match, ask for a warning (but no error). */ |
400fbf9f | 1578 | if (TYPE_ACTUAL_ARG_TYPES (f1) |
744aa42f | 1579 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), |
dc5027f4 | 1580 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1581 | val = 2; |
1582 | return val; | |
1583 | } | |
1584 | if (args2 == 0) | |
1585 | { | |
1586 | if (!self_promoting_args_p (args1)) | |
1587 | return 0; | |
1588 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
744aa42f | 1589 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), |
dc5027f4 | 1590 | enum_and_int_p, different_types_p)) |
400fbf9f JW |
1591 | val = 2; |
1592 | return val; | |
1593 | } | |
1594 | ||
1595 | /* Both types have argument lists: compare them and propagate results. */ | |
dc5027f4 JM |
1596 | val1 = type_lists_compatible_p (args1, args2, enum_and_int_p, |
1597 | different_types_p); | |
400fbf9f JW |
1598 | return val1 != 1 ? val1 : val; |
1599 | } | |
1600 | ||
744aa42f ILT |
1601 | /* Check two lists of types for compatibility, returning 0 for |
1602 | incompatible, 1 for compatible, or 2 for compatible with | |
dc5027f4 JM |
1603 | warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in |
1604 | comptypes_internal. */ | |
400fbf9f JW |
1605 | |
1606 | static int | |
744aa42f | 1607 | type_lists_compatible_p (const_tree args1, const_tree args2, |
dc5027f4 | 1608 | bool *enum_and_int_p, bool *different_types_p) |
400fbf9f JW |
1609 | { |
1610 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1611 | int val = 1; | |
9d5f3e49 | 1612 | int newval = 0; |
400fbf9f JW |
1613 | |
1614 | while (1) | |
1615 | { | |
46df2823 | 1616 | tree a1, mv1, a2, mv2; |
400fbf9f JW |
1617 | if (args1 == 0 && args2 == 0) |
1618 | return val; | |
1619 | /* If one list is shorter than the other, | |
1620 | they fail to match. */ | |
1621 | if (args1 == 0 || args2 == 0) | |
1622 | return 0; | |
46df2823 JM |
1623 | mv1 = a1 = TREE_VALUE (args1); |
1624 | mv2 = a2 = TREE_VALUE (args2); | |
1625 | if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) | |
1626 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
1627 | if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) | |
1628 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
400fbf9f JW |
1629 | /* A null pointer instead of a type |
1630 | means there is supposed to be an argument | |
1631 | but nothing is specified about what type it has. | |
1632 | So match anything that self-promotes. */ | |
dc5027f4 JM |
1633 | if (different_types_p != NULL |
1634 | && (a1 == 0) != (a2 == 0)) | |
1635 | *different_types_p = true; | |
46df2823 | 1636 | if (a1 == 0) |
400fbf9f | 1637 | { |
46df2823 | 1638 | if (c_type_promotes_to (a2) != a2) |
400fbf9f JW |
1639 | return 0; |
1640 | } | |
46df2823 | 1641 | else if (a2 == 0) |
400fbf9f | 1642 | { |
46df2823 | 1643 | if (c_type_promotes_to (a1) != a1) |
400fbf9f JW |
1644 | return 0; |
1645 | } | |
8f5b6d29 | 1646 | /* If one of the lists has an error marker, ignore this arg. */ |
46df2823 JM |
1647 | else if (TREE_CODE (a1) == ERROR_MARK |
1648 | || TREE_CODE (a2) == ERROR_MARK) | |
8f5b6d29 | 1649 | ; |
dc5027f4 JM |
1650 | else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p, |
1651 | different_types_p))) | |
400fbf9f | 1652 | { |
dc5027f4 JM |
1653 | if (different_types_p != NULL) |
1654 | *different_types_p = true; | |
400fbf9f JW |
1655 | /* Allow wait (union {union wait *u; int *i} *) |
1656 | and wait (union wait *) to be compatible. */ | |
46df2823 JM |
1657 | if (TREE_CODE (a1) == UNION_TYPE |
1658 | && (TYPE_NAME (a1) == 0 | |
ebf0bf7f | 1659 | || TYPE_TRANSPARENT_AGGR (a1)) |
46df2823 JM |
1660 | && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST |
1661 | && tree_int_cst_equal (TYPE_SIZE (a1), | |
1662 | TYPE_SIZE (a2))) | |
400fbf9f JW |
1663 | { |
1664 | tree memb; | |
46df2823 | 1665 | for (memb = TYPE_FIELDS (a1); |
910ad8de | 1666 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1667 | { |
1668 | tree mv3 = TREE_TYPE (memb); | |
1669 | if (mv3 && mv3 != error_mark_node | |
1670 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
1671 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
dc5027f4 JM |
1672 | if (comptypes_internal (mv3, mv2, enum_and_int_p, |
1673 | different_types_p)) | |
58cb41e6 JJ |
1674 | break; |
1675 | } | |
400fbf9f JW |
1676 | if (memb == 0) |
1677 | return 0; | |
1678 | } | |
46df2823 JM |
1679 | else if (TREE_CODE (a2) == UNION_TYPE |
1680 | && (TYPE_NAME (a2) == 0 | |
ebf0bf7f | 1681 | || TYPE_TRANSPARENT_AGGR (a2)) |
46df2823 JM |
1682 | && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST |
1683 | && tree_int_cst_equal (TYPE_SIZE (a2), | |
1684 | TYPE_SIZE (a1))) | |
400fbf9f JW |
1685 | { |
1686 | tree memb; | |
46df2823 | 1687 | for (memb = TYPE_FIELDS (a2); |
910ad8de | 1688 | memb; memb = DECL_CHAIN (memb)) |
58cb41e6 JJ |
1689 | { |
1690 | tree mv3 = TREE_TYPE (memb); | |
1691 | if (mv3 && mv3 != error_mark_node | |
1692 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
1693 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
dc5027f4 JM |
1694 | if (comptypes_internal (mv3, mv1, enum_and_int_p, |
1695 | different_types_p)) | |
58cb41e6 JJ |
1696 | break; |
1697 | } | |
400fbf9f JW |
1698 | if (memb == 0) |
1699 | return 0; | |
1700 | } | |
1701 | else | |
1702 | return 0; | |
1703 | } | |
1704 | ||
1705 | /* comptypes said ok, but record if it said to warn. */ | |
1706 | if (newval > val) | |
1707 | val = newval; | |
1708 | ||
1709 | args1 = TREE_CHAIN (args1); | |
1710 | args2 = TREE_CHAIN (args2); | |
1711 | } | |
1712 | } | |
400fbf9f | 1713 | \f |
400fbf9f JW |
1714 | /* Compute the size to increment a pointer by. */ |
1715 | ||
4e2fb7de | 1716 | static tree |
58f9752a | 1717 | c_size_in_bytes (const_tree type) |
400fbf9f JW |
1718 | { |
1719 | enum tree_code code = TREE_CODE (type); | |
1720 | ||
fed3cef0 RK |
1721 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) |
1722 | return size_one_node; | |
1723 | ||
d0f062fb | 1724 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
400fbf9f JW |
1725 | { |
1726 | error ("arithmetic on pointer to an incomplete type"); | |
fed3cef0 | 1727 | return size_one_node; |
400fbf9f JW |
1728 | } |
1729 | ||
1730 | /* Convert in case a char is more than one unit. */ | |
db3927fb AH |
1731 | return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
1732 | size_int (TYPE_PRECISION (char_type_node) | |
1733 | / BITS_PER_UNIT)); | |
400fbf9f | 1734 | } |
400fbf9f | 1735 | \f |
400fbf9f JW |
1736 | /* Return either DECL or its known constant value (if it has one). */ |
1737 | ||
56cb9733 | 1738 | tree |
2f6e4e97 | 1739 | decl_constant_value (tree decl) |
400fbf9f | 1740 | { |
a7c1916a | 1741 | if (/* Don't change a variable array bound or initial value to a constant |
4f976745 RK |
1742 | in a place where a variable is invalid. Note that DECL_INITIAL |
1743 | isn't valid for a PARM_DECL. */ | |
a7c1916a | 1744 | current_function_decl != 0 |
4f976745 | 1745 | && TREE_CODE (decl) != PARM_DECL |
3f75a254 | 1746 | && !TREE_THIS_VOLATILE (decl) |
83bab8db | 1747 | && TREE_READONLY (decl) |
400fbf9f JW |
1748 | && DECL_INITIAL (decl) != 0 |
1749 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1750 | /* This is invalid if initial value is not constant. | |
1751 | If it has either a function call, a memory reference, | |
1752 | or a variable, then re-evaluating it could give different results. */ | |
1753 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1754 | /* Check for cases where this is sub-optimal, even though valid. */ | |
2f74f7e9 | 1755 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
400fbf9f JW |
1756 | return DECL_INITIAL (decl); |
1757 | return decl; | |
1758 | } | |
1759 | ||
f2a71bbc JM |
1760 | /* Convert the array expression EXP to a pointer. */ |
1761 | static tree | |
c2255bc4 | 1762 | array_to_pointer_conversion (location_t loc, tree exp) |
207bf485 | 1763 | { |
f2a71bbc | 1764 | tree orig_exp = exp; |
207bf485 | 1765 | tree type = TREE_TYPE (exp); |
f2a71bbc JM |
1766 | tree adr; |
1767 | tree restype = TREE_TYPE (type); | |
1768 | tree ptrtype; | |
207bf485 | 1769 | |
f2a71bbc | 1770 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
207bf485 | 1771 | |
f2a71bbc | 1772 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1773 | |
487a92fe JM |
1774 | if (TREE_NO_WARNING (orig_exp)) |
1775 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1776 | |
f2a71bbc JM |
1777 | ptrtype = build_pointer_type (restype); |
1778 | ||
1779 | if (TREE_CODE (exp) == INDIRECT_REF) | |
1780 | return convert (ptrtype, TREE_OPERAND (exp, 0)); | |
1781 | ||
1f37c583 JM |
1782 | /* In C++ array compound literals are temporary objects unless they are |
1783 | const or appear in namespace scope, so they are destroyed too soon | |
1784 | to use them for much of anything (c++/53220). */ | |
1785 | if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) | |
1786 | { | |
1787 | tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
1788 | if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) | |
1789 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, | |
1790 | "converting an array compound literal to a pointer " | |
1791 | "is ill-formed in C++"); | |
1792 | } | |
1793 | ||
c2255bc4 | 1794 | adr = build_unary_op (loc, ADDR_EXPR, exp, 1); |
f2a71bbc JM |
1795 | return convert (ptrtype, adr); |
1796 | } | |
207bf485 | 1797 | |
f2a71bbc JM |
1798 | /* Convert the function expression EXP to a pointer. */ |
1799 | static tree | |
c2255bc4 | 1800 | function_to_pointer_conversion (location_t loc, tree exp) |
f2a71bbc JM |
1801 | { |
1802 | tree orig_exp = exp; | |
207bf485 | 1803 | |
f2a71bbc | 1804 | gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); |
207bf485 | 1805 | |
f2a71bbc | 1806 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1807 | |
f2a71bbc JM |
1808 | if (TREE_NO_WARNING (orig_exp)) |
1809 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1810 | |
c2255bc4 | 1811 | return build_unary_op (loc, ADDR_EXPR, exp, 0); |
f2a71bbc | 1812 | } |
207bf485 | 1813 | |
ebfbbdc5 JJ |
1814 | /* Mark EXP as read, not just set, for set but not used -Wunused |
1815 | warning purposes. */ | |
1816 | ||
1817 | void | |
1818 | mark_exp_read (tree exp) | |
1819 | { | |
1820 | switch (TREE_CODE (exp)) | |
1821 | { | |
1822 | case VAR_DECL: | |
1823 | case PARM_DECL: | |
1824 | DECL_READ_P (exp) = 1; | |
1825 | break; | |
1826 | case ARRAY_REF: | |
1827 | case COMPONENT_REF: | |
1828 | case MODIFY_EXPR: | |
1829 | case REALPART_EXPR: | |
1830 | case IMAGPART_EXPR: | |
1831 | CASE_CONVERT: | |
1832 | case ADDR_EXPR: | |
1833 | mark_exp_read (TREE_OPERAND (exp, 0)); | |
1834 | break; | |
1835 | case COMPOUND_EXPR: | |
82c3c067 | 1836 | case C_MAYBE_CONST_EXPR: |
ebfbbdc5 JJ |
1837 | mark_exp_read (TREE_OPERAND (exp, 1)); |
1838 | break; | |
1839 | default: | |
1840 | break; | |
1841 | } | |
1842 | } | |
1843 | ||
f2a71bbc JM |
1844 | /* Perform the default conversion of arrays and functions to pointers. |
1845 | Return the result of converting EXP. For any other expression, just | |
c2255bc4 AH |
1846 | return EXP. |
1847 | ||
1848 | LOC is the location of the expression. */ | |
f2a71bbc JM |
1849 | |
1850 | struct c_expr | |
c2255bc4 | 1851 | default_function_array_conversion (location_t loc, struct c_expr exp) |
f2a71bbc JM |
1852 | { |
1853 | tree orig_exp = exp.value; | |
1854 | tree type = TREE_TYPE (exp.value); | |
1855 | enum tree_code code = TREE_CODE (type); | |
1856 | ||
1857 | switch (code) | |
1858 | { | |
1859 | case ARRAY_TYPE: | |
1860 | { | |
1861 | bool not_lvalue = false; | |
1862 | bool lvalue_array_p; | |
1863 | ||
1864 | while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR | |
1043771b | 1865 | || CONVERT_EXPR_P (exp.value)) |
f2a71bbc JM |
1866 | && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) |
1867 | { | |
1868 | if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) | |
1869 | not_lvalue = true; | |
1870 | exp.value = TREE_OPERAND (exp.value, 0); | |
1871 | } | |
1872 | ||
1873 | if (TREE_NO_WARNING (orig_exp)) | |
1874 | TREE_NO_WARNING (exp.value) = 1; | |
1875 | ||
1876 | lvalue_array_p = !not_lvalue && lvalue_p (exp.value); | |
1877 | if (!flag_isoc99 && !lvalue_array_p) | |
1878 | { | |
1879 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1880 | Normally, using such an array would be invalid; but it can | |
1881 | be used correctly inside sizeof or as a statement expression. | |
1882 | Thus, do not give an error here; an error will result later. */ | |
1883 | return exp; | |
1884 | } | |
1885 | ||
c2255bc4 | 1886 | exp.value = array_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1887 | } |
1888 | break; | |
1889 | case FUNCTION_TYPE: | |
c2255bc4 | 1890 | exp.value = function_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1891 | break; |
1892 | default: | |
f2a71bbc | 1893 | break; |
207bf485 | 1894 | } |
f2a71bbc | 1895 | |
207bf485 JM |
1896 | return exp; |
1897 | } | |
1898 | ||
ebfbbdc5 JJ |
1899 | struct c_expr |
1900 | default_function_array_read_conversion (location_t loc, struct c_expr exp) | |
1901 | { | |
1902 | mark_exp_read (exp.value); | |
1903 | return default_function_array_conversion (loc, exp); | |
1904 | } | |
522ddfa2 JM |
1905 | |
1906 | /* EXP is an expression of integer type. Apply the integer promotions | |
1907 | to it and return the promoted value. */ | |
400fbf9f JW |
1908 | |
1909 | tree | |
522ddfa2 | 1910 | perform_integral_promotions (tree exp) |
400fbf9f | 1911 | { |
b3694847 SS |
1912 | tree type = TREE_TYPE (exp); |
1913 | enum tree_code code = TREE_CODE (type); | |
400fbf9f | 1914 | |
522ddfa2 | 1915 | gcc_assert (INTEGRAL_TYPE_P (type)); |
157689c6 | 1916 | |
400fbf9f JW |
1917 | /* Normally convert enums to int, |
1918 | but convert wide enums to something wider. */ | |
1919 | if (code == ENUMERAL_TYPE) | |
1920 | { | |
b0c48229 NB |
1921 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
1922 | TYPE_PRECISION (integer_type_node)), | |
1923 | ((TYPE_PRECISION (type) | |
1924 | >= TYPE_PRECISION (integer_type_node)) | |
8df83eae | 1925 | && TYPE_UNSIGNED (type))); |
05bccae2 | 1926 | |
400fbf9f JW |
1927 | return convert (type, exp); |
1928 | } | |
1929 | ||
522ddfa2 JM |
1930 | /* ??? This should no longer be needed now bit-fields have their |
1931 | proper types. */ | |
9753f113 | 1932 | if (TREE_CODE (exp) == COMPONENT_REF |
05bccae2 | 1933 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) |
cff9c407 | 1934 | /* If it's thinner than an int, promote it like a |
d72040f5 | 1935 | c_promoting_integer_type_p, otherwise leave it alone. */ |
05bccae2 RK |
1936 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), |
1937 | TYPE_PRECISION (integer_type_node))) | |
f458d1d5 | 1938 | return convert (integer_type_node, exp); |
9753f113 | 1939 | |
d72040f5 | 1940 | if (c_promoting_integer_type_p (type)) |
400fbf9f | 1941 | { |
f458d1d5 | 1942 | /* Preserve unsignedness if not really getting any wider. */ |
8df83eae | 1943 | if (TYPE_UNSIGNED (type) |
f458d1d5 | 1944 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
400fbf9f | 1945 | return convert (unsigned_type_node, exp); |
05bccae2 | 1946 | |
400fbf9f JW |
1947 | return convert (integer_type_node, exp); |
1948 | } | |
05bccae2 | 1949 | |
522ddfa2 JM |
1950 | return exp; |
1951 | } | |
1952 | ||
1953 | ||
1954 | /* Perform default promotions for C data used in expressions. | |
46bdb9cf | 1955 | Enumeral types or short or char are converted to int. |
522ddfa2 JM |
1956 | In addition, manifest constants symbols are replaced by their values. */ |
1957 | ||
1958 | tree | |
1959 | default_conversion (tree exp) | |
1960 | { | |
1961 | tree orig_exp; | |
1962 | tree type = TREE_TYPE (exp); | |
1963 | enum tree_code code = TREE_CODE (type); | |
40449a90 | 1964 | tree promoted_type; |
522ddfa2 | 1965 | |
ebfbbdc5 JJ |
1966 | mark_exp_read (exp); |
1967 | ||
46bdb9cf JM |
1968 | /* Functions and arrays have been converted during parsing. */ |
1969 | gcc_assert (code != FUNCTION_TYPE); | |
1970 | if (code == ARRAY_TYPE) | |
1971 | return exp; | |
522ddfa2 JM |
1972 | |
1973 | /* Constants can be used directly unless they're not loadable. */ | |
1974 | if (TREE_CODE (exp) == CONST_DECL) | |
1975 | exp = DECL_INITIAL (exp); | |
1976 | ||
522ddfa2 JM |
1977 | /* Strip no-op conversions. */ |
1978 | orig_exp = exp; | |
1979 | STRIP_TYPE_NOPS (exp); | |
1980 | ||
1981 | if (TREE_NO_WARNING (orig_exp)) | |
1982 | TREE_NO_WARNING (exp) = 1; | |
1983 | ||
400fbf9f JW |
1984 | if (code == VOID_TYPE) |
1985 | { | |
1986 | error ("void value not ignored as it ought to be"); | |
1987 | return error_mark_node; | |
1988 | } | |
808d6eaa JM |
1989 | |
1990 | exp = require_complete_type (exp); | |
1991 | if (exp == error_mark_node) | |
1992 | return error_mark_node; | |
1993 | ||
40449a90 SL |
1994 | promoted_type = targetm.promoted_type (type); |
1995 | if (promoted_type) | |
1996 | return convert (promoted_type, exp); | |
1997 | ||
808d6eaa JM |
1998 | if (INTEGRAL_TYPE_P (type)) |
1999 | return perform_integral_promotions (exp); | |
2000 | ||
400fbf9f JW |
2001 | return exp; |
2002 | } | |
2003 | \f | |
0fb96aa4 | 2004 | /* Look up COMPONENT in a structure or union TYPE. |
e9b2c823 NB |
2005 | |
2006 | If the component name is not found, returns NULL_TREE. Otherwise, | |
2007 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
2008 | stepping down the chain to the component, which is in the last | |
2009 | TREE_VALUE of the list. Normally the list is of length one, but if | |
2010 | the component is embedded within (nested) anonymous structures or | |
2011 | unions, the list steps down the chain to the component. */ | |
2f6e4e97 | 2012 | |
2f2d13da | 2013 | static tree |
0fb96aa4 | 2014 | lookup_field (tree type, tree component) |
2f2d13da DE |
2015 | { |
2016 | tree field; | |
2017 | ||
2018 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
2019 | to the field elements. Use a binary search on this array to quickly | |
2020 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
2021 | will always be set for structures which have many elements. */ | |
2022 | ||
22a0b85f | 2023 | if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) |
2f2d13da DE |
2024 | { |
2025 | int bot, top, half; | |
d07605f5 | 2026 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; |
2f2d13da DE |
2027 | |
2028 | field = TYPE_FIELDS (type); | |
2029 | bot = 0; | |
d07605f5 | 2030 | top = TYPE_LANG_SPECIFIC (type)->s->len; |
2f2d13da DE |
2031 | while (top - bot > 1) |
2032 | { | |
2f2d13da DE |
2033 | half = (top - bot + 1) >> 1; |
2034 | field = field_array[bot+half]; | |
2035 | ||
2036 | if (DECL_NAME (field) == NULL_TREE) | |
2037 | { | |
2038 | /* Step through all anon unions in linear fashion. */ | |
2039 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
2040 | { | |
2f2d13da | 2041 | field = field_array[bot++]; |
a68b98cf RK |
2042 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
2043 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
19d76e60 | 2044 | { |
0fb96aa4 | 2045 | tree anon = lookup_field (TREE_TYPE (field), component); |
e9b2c823 NB |
2046 | |
2047 | if (anon) | |
2048 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2049 | |
2050 | /* The Plan 9 compiler permits referring | |
2051 | directly to an anonymous struct/union field | |
2052 | using a typedef name. */ | |
2053 | if (flag_plan9_extensions | |
2054 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2055 | && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) | |
2056 | == TYPE_DECL) | |
2057 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2058 | == component)) | |
2059 | break; | |
2f6e4e97 | 2060 | } |
2f2d13da DE |
2061 | } |
2062 | ||
2063 | /* Entire record is only anon unions. */ | |
2064 | if (bot > top) | |
2065 | return NULL_TREE; | |
2066 | ||
2067 | /* Restart the binary search, with new lower bound. */ | |
2068 | continue; | |
2069 | } | |
2070 | ||
e8b87aac | 2071 | if (DECL_NAME (field) == component) |
2f2d13da | 2072 | break; |
e8b87aac | 2073 | if (DECL_NAME (field) < component) |
2f2d13da DE |
2074 | bot += half; |
2075 | else | |
2076 | top = bot + half; | |
2077 | } | |
2078 | ||
2079 | if (DECL_NAME (field_array[bot]) == component) | |
2080 | field = field_array[bot]; | |
2081 | else if (DECL_NAME (field) != component) | |
e9b2c823 | 2082 | return NULL_TREE; |
2f2d13da DE |
2083 | } |
2084 | else | |
2085 | { | |
910ad8de | 2086 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
2f2d13da | 2087 | { |
e9b2c823 NB |
2088 | if (DECL_NAME (field) == NULL_TREE |
2089 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
2090 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
2f2d13da | 2091 | { |
0fb96aa4 | 2092 | tree anon = lookup_field (TREE_TYPE (field), component); |
a68b98cf | 2093 | |
e9b2c823 NB |
2094 | if (anon) |
2095 | return tree_cons (NULL_TREE, field, anon); | |
478a1c5b ILT |
2096 | |
2097 | /* The Plan 9 compiler permits referring directly to an | |
2098 | anonymous struct/union field using a typedef | |
2099 | name. */ | |
2100 | if (flag_plan9_extensions | |
2101 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2102 | && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL | |
2103 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2104 | == component)) | |
2105 | break; | |
2f2d13da DE |
2106 | } |
2107 | ||
2108 | if (DECL_NAME (field) == component) | |
2109 | break; | |
2110 | } | |
e9b2c823 NB |
2111 | |
2112 | if (field == NULL_TREE) | |
2113 | return NULL_TREE; | |
2f2d13da DE |
2114 | } |
2115 | ||
e9b2c823 | 2116 | return tree_cons (NULL_TREE, field, NULL_TREE); |
2f2d13da DE |
2117 | } |
2118 | ||
c2255bc4 AH |
2119 | /* Make an expression to refer to the COMPONENT field of structure or |
2120 | union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the | |
2121 | location of the COMPONENT_REF. */ | |
400fbf9f JW |
2122 | |
2123 | tree | |
c2255bc4 | 2124 | build_component_ref (location_t loc, tree datum, tree component) |
400fbf9f | 2125 | { |
b3694847 SS |
2126 | tree type = TREE_TYPE (datum); |
2127 | enum tree_code code = TREE_CODE (type); | |
2128 | tree field = NULL; | |
2129 | tree ref; | |
1e57bf47 | 2130 | bool datum_lvalue = lvalue_p (datum); |
400fbf9f | 2131 | |
7a3ea201 RH |
2132 | if (!objc_is_public (datum, component)) |
2133 | return error_mark_node; | |
2134 | ||
46a88c12 | 2135 | /* Detect Objective-C property syntax object.property. */ |
668ea4b1 | 2136 | if (c_dialect_objc () |
46a88c12 | 2137 | && (ref = objc_maybe_build_component_ref (datum, component))) |
668ea4b1 IS |
2138 | return ref; |
2139 | ||
400fbf9f JW |
2140 | /* See if there is a field or component with name COMPONENT. */ |
2141 | ||
2142 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
2143 | { | |
d0f062fb | 2144 | if (!COMPLETE_TYPE_P (type)) |
400fbf9f | 2145 | { |
7a228918 | 2146 | c_incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
2147 | return error_mark_node; |
2148 | } | |
2149 | ||
0fb96aa4 | 2150 | field = lookup_field (type, component); |
400fbf9f JW |
2151 | |
2152 | if (!field) | |
2153 | { | |
c2255bc4 | 2154 | error_at (loc, "%qT has no member named %qE", type, component); |
400fbf9f JW |
2155 | return error_mark_node; |
2156 | } | |
400fbf9f | 2157 | |
e9b2c823 NB |
2158 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. |
2159 | This might be better solved in future the way the C++ front | |
2160 | end does it - by giving the anonymous entities each a | |
2161 | separate name and type, and then have build_component_ref | |
2162 | recursively call itself. We can't do that here. */ | |
46ea50cb | 2163 | do |
19d76e60 | 2164 | { |
e9b2c823 | 2165 | tree subdatum = TREE_VALUE (field); |
efed193e JM |
2166 | int quals; |
2167 | tree subtype; | |
1e57bf47 | 2168 | bool use_datum_quals; |
e9b2c823 NB |
2169 | |
2170 | if (TREE_TYPE (subdatum) == error_mark_node) | |
2171 | return error_mark_node; | |
2172 | ||
1e57bf47 JM |
2173 | /* If this is an rvalue, it does not have qualifiers in C |
2174 | standard terms and we must avoid propagating such | |
2175 | qualifiers down to a non-lvalue array that is then | |
2176 | converted to a pointer. */ | |
2177 | use_datum_quals = (datum_lvalue | |
2178 | || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); | |
2179 | ||
efed193e | 2180 | quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); |
1e57bf47 JM |
2181 | if (use_datum_quals) |
2182 | quals |= TYPE_QUALS (TREE_TYPE (datum)); | |
efed193e JM |
2183 | subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); |
2184 | ||
2185 | ref = build3 (COMPONENT_REF, subtype, datum, subdatum, | |
53fb4de3 | 2186 | NULL_TREE); |
c2255bc4 | 2187 | SET_EXPR_LOCATION (ref, loc); |
1e57bf47 JM |
2188 | if (TREE_READONLY (subdatum) |
2189 | || (use_datum_quals && TREE_READONLY (datum))) | |
19d76e60 | 2190 | TREE_READONLY (ref) = 1; |
1e57bf47 JM |
2191 | if (TREE_THIS_VOLATILE (subdatum) |
2192 | || (use_datum_quals && TREE_THIS_VOLATILE (datum))) | |
19d76e60 | 2193 | TREE_THIS_VOLATILE (ref) = 1; |
e23bd218 IR |
2194 | |
2195 | if (TREE_DEPRECATED (subdatum)) | |
9b86d6bb | 2196 | warn_deprecated_use (subdatum, NULL_TREE); |
e23bd218 | 2197 | |
19d76e60 | 2198 | datum = ref; |
46ea50cb RS |
2199 | |
2200 | field = TREE_CHAIN (field); | |
19d76e60 | 2201 | } |
46ea50cb | 2202 | while (field); |
19d76e60 | 2203 | |
400fbf9f JW |
2204 | return ref; |
2205 | } | |
2206 | else if (code != ERROR_MARK) | |
c2255bc4 AH |
2207 | error_at (loc, |
2208 | "request for member %qE in something not a structure or union", | |
2209 | component); | |
400fbf9f JW |
2210 | |
2211 | return error_mark_node; | |
2212 | } | |
2213 | \f | |
2214 | /* Given an expression PTR for a pointer, return an expression | |
2215 | for the value pointed to. | |
6a3799eb AH |
2216 | ERRORSTRING is the name of the operator to appear in error messages. |
2217 | ||
2218 | LOC is the location to use for the generated tree. */ | |
400fbf9f JW |
2219 | |
2220 | tree | |
dd865ef6 | 2221 | build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) |
400fbf9f | 2222 | { |
b3694847 SS |
2223 | tree pointer = default_conversion (ptr); |
2224 | tree type = TREE_TYPE (pointer); | |
6a3799eb | 2225 | tree ref; |
400fbf9f JW |
2226 | |
2227 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b | 2228 | { |
1043771b | 2229 | if (CONVERT_EXPR_P (pointer) |
79bedddc SR |
2230 | || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) |
2231 | { | |
2232 | /* If a warning is issued, mark it to avoid duplicates from | |
2233 | the backend. This only needs to be done at | |
2234 | warn_strict_aliasing > 2. */ | |
2235 | if (warn_strict_aliasing > 2) | |
2236 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), | |
2237 | type, TREE_OPERAND (pointer, 0))) | |
2238 | TREE_NO_WARNING (pointer) = 1; | |
2239 | } | |
2240 | ||
870cc33b | 2241 | if (TREE_CODE (pointer) == ADDR_EXPR |
870cc33b RS |
2242 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) |
2243 | == TREE_TYPE (type))) | |
6a3799eb AH |
2244 | { |
2245 | ref = TREE_OPERAND (pointer, 0); | |
2246 | protected_set_expr_location (ref, loc); | |
2247 | return ref; | |
2248 | } | |
870cc33b RS |
2249 | else |
2250 | { | |
2251 | tree t = TREE_TYPE (type); | |
46df2823 | 2252 | |
984dfd8c | 2253 | ref = build1 (INDIRECT_REF, t, pointer); |
400fbf9f | 2254 | |
baae9b65 | 2255 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) |
870cc33b | 2256 | { |
c9f9eb5d | 2257 | error_at (loc, "dereferencing pointer to incomplete type"); |
870cc33b RS |
2258 | return error_mark_node; |
2259 | } | |
7d882b83 | 2260 | if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) |
c9f9eb5d | 2261 | warning_at (loc, 0, "dereferencing %<void *%> pointer"); |
870cc33b RS |
2262 | |
2263 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
2264 | so that we get the proper error message if the result is used | |
2265 | to assign to. Also, &* is supposed to be a no-op. | |
2266 | And ANSI C seems to specify that the type of the result | |
2267 | should be the const type. */ | |
2268 | /* A de-reference of a pointer to const is not a const. It is valid | |
2269 | to change it via some other pointer. */ | |
2270 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
2271 | TREE_SIDE_EFFECTS (ref) | |
271bd540 | 2272 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); |
493692cd | 2273 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
6a3799eb | 2274 | protected_set_expr_location (ref, loc); |
870cc33b RS |
2275 | return ref; |
2276 | } | |
2277 | } | |
400fbf9f | 2278 | else if (TREE_CODE (pointer) != ERROR_MARK) |
7a6daeb0 NF |
2279 | invalid_indirection_error (loc, type, errstring); |
2280 | ||
400fbf9f JW |
2281 | return error_mark_node; |
2282 | } | |
2283 | ||
2284 | /* This handles expressions of the form "a[i]", which denotes | |
2285 | an array reference. | |
2286 | ||
2287 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2288 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2289 | This avoids forcing the array out of registers, and can work on | |
2290 | arrays that are not lvalues (for example, members of structures returned | |
6a3799eb AH |
2291 | by functions). |
2292 | ||
30cd1c5d AS |
2293 | For vector types, allow vector[i] but not i[vector], and create |
2294 | *(((type*)&vectortype) + i) for the expression. | |
2295 | ||
6a3799eb | 2296 | LOC is the location to use for the returned expression. */ |
400fbf9f JW |
2297 | |
2298 | tree | |
c2255bc4 | 2299 | build_array_ref (location_t loc, tree array, tree index) |
400fbf9f | 2300 | { |
6a3799eb | 2301 | tree ret; |
a4ab7973 | 2302 | bool swapped = false; |
400fbf9f JW |
2303 | if (TREE_TYPE (array) == error_mark_node |
2304 | || TREE_TYPE (index) == error_mark_node) | |
2305 | return error_mark_node; | |
2306 | ||
a4ab7973 | 2307 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE |
30cd1c5d AS |
2308 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE |
2309 | /* Allow vector[index] but not index[vector]. */ | |
2310 | && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE) | |
400fbf9f | 2311 | { |
a4ab7973 JM |
2312 | tree temp; |
2313 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE | |
2314 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
fdeefd49 | 2315 | { |
f90e8e2e | 2316 | error_at (loc, |
30cd1c5d AS |
2317 | "subscripted value is neither array nor pointer nor vector"); |
2318 | ||
fdeefd49 RS |
2319 | return error_mark_node; |
2320 | } | |
a4ab7973 JM |
2321 | temp = array; |
2322 | array = index; | |
2323 | index = temp; | |
2324 | swapped = true; | |
2325 | } | |
2326 | ||
2327 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2328 | { | |
a63068b6 | 2329 | error_at (loc, "array subscript is not an integer"); |
a4ab7973 JM |
2330 | return error_mark_node; |
2331 | } | |
2332 | ||
2333 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2334 | { | |
a63068b6 | 2335 | error_at (loc, "subscripted value is pointer to function"); |
a4ab7973 JM |
2336 | return error_mark_node; |
2337 | } | |
2338 | ||
ff6b6641 GDR |
2339 | /* ??? Existing practice has been to warn only when the char |
2340 | index is syntactically the index, not for char[array]. */ | |
2341 | if (!swapped) | |
2342 | warn_array_subscript_with_type_char (index); | |
a4ab7973 JM |
2343 | |
2344 | /* Apply default promotions *after* noticing character types. */ | |
2345 | index = default_conversion (index); | |
2346 | ||
2347 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
f90e8e2e | 2348 | |
7edaa4d2 | 2349 | convert_vector_to_pointer_for_subscript (loc, &array, index); |
a4ab7973 JM |
2350 | |
2351 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2352 | { | |
e4d35515 | 2353 | tree rval, type; |
fdeefd49 | 2354 | |
400fbf9f JW |
2355 | /* An array that is indexed by a non-constant |
2356 | cannot be stored in a register; we must be able to do | |
2357 | address arithmetic on its address. | |
2358 | Likewise an array of elements of variable size. */ | |
2359 | if (TREE_CODE (index) != INTEGER_CST | |
d0f062fb | 2360 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
400fbf9f JW |
2361 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
2362 | { | |
dffd7eb6 | 2363 | if (!c_mark_addressable (array)) |
400fbf9f JW |
2364 | return error_mark_node; |
2365 | } | |
e6d52559 JW |
2366 | /* An array that is indexed by a constant value which is not within |
2367 | the array bounds cannot be stored in a register either; because we | |
2368 | would get a crash in store_bit_field/extract_bit_field when trying | |
2369 | to access a non-existent part of the register. */ | |
2370 | if (TREE_CODE (index) == INTEGER_CST | |
eb34af89 | 2371 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3f75a254 | 2372 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) |
e6d52559 | 2373 | { |
dffd7eb6 | 2374 | if (!c_mark_addressable (array)) |
e6d52559 JW |
2375 | return error_mark_node; |
2376 | } | |
400fbf9f | 2377 | |
400fbf9f JW |
2378 | if (pedantic) |
2379 | { | |
2380 | tree foo = array; | |
2381 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2382 | foo = TREE_OPERAND (foo, 0); | |
5baeaac0 | 2383 | if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) |
c1771a20 | 2384 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 2385 | "ISO C forbids subscripting %<register%> array"); |
3f75a254 | 2386 | else if (!flag_isoc99 && !lvalue_p (foo)) |
c1771a20 | 2387 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 2388 | "ISO C90 forbids subscripting non-lvalue array"); |
400fbf9f JW |
2389 | } |
2390 | ||
46df2823 | 2391 | type = TREE_TYPE (TREE_TYPE (array)); |
53fb4de3 | 2392 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
400fbf9f | 2393 | /* Array ref is const/volatile if the array elements are |
c22cacf3 | 2394 | or if the array is. */ |
400fbf9f JW |
2395 | TREE_READONLY (rval) |
2396 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2397 | | TREE_READONLY (array)); | |
2398 | TREE_SIDE_EFFECTS (rval) | |
2399 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2400 | | TREE_SIDE_EFFECTS (array)); | |
2401 | TREE_THIS_VOLATILE (rval) | |
2402 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2403 | /* This was added by rms on 16 Nov 91. | |
2f6e4e97 | 2404 | It fixes vol struct foo *a; a->elts[1] |
400fbf9f JW |
2405 | in an inline function. |
2406 | Hope it doesn't break something else. */ | |
2407 | | TREE_THIS_VOLATILE (array)); | |
928c19bb | 2408 | ret = require_complete_type (rval); |
6a3799eb AH |
2409 | protected_set_expr_location (ret, loc); |
2410 | return ret; | |
400fbf9f | 2411 | } |
a4ab7973 JM |
2412 | else |
2413 | { | |
2414 | tree ar = default_conversion (array); | |
400fbf9f | 2415 | |
a4ab7973 JM |
2416 | if (ar == error_mark_node) |
2417 | return ar; | |
400fbf9f | 2418 | |
a4ab7973 JM |
2419 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); |
2420 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
400fbf9f | 2421 | |
ba47d38d | 2422 | return build_indirect_ref |
c9f9eb5d | 2423 | (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0), |
dd865ef6 | 2424 | RO_ARRAY_INDEXING); |
a4ab7973 | 2425 | } |
400fbf9f JW |
2426 | } |
2427 | \f | |
7e585d16 | 2428 | /* Build an external reference to identifier ID. FUN indicates |
766beb40 | 2429 | whether this will be used for a function call. LOC is the source |
6866c6e8 ILT |
2430 | location of the identifier. This sets *TYPE to the type of the |
2431 | identifier, which is not the same as the type of the returned value | |
2432 | for CONST_DECLs defined as enum constants. If the type of the | |
2433 | identifier is not available, *TYPE is set to NULL. */ | |
7e585d16 | 2434 | tree |
c2255bc4 | 2435 | build_external_ref (location_t loc, tree id, int fun, tree *type) |
7e585d16 ZW |
2436 | { |
2437 | tree ref; | |
2438 | tree decl = lookup_name (id); | |
16b34ad6 ZL |
2439 | |
2440 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2441 | whatever lookup_name() found. */ | |
2442 | decl = objc_lookup_ivar (decl, id); | |
7e585d16 | 2443 | |
6866c6e8 | 2444 | *type = NULL; |
339a28b9 | 2445 | if (decl && decl != error_mark_node) |
6866c6e8 ILT |
2446 | { |
2447 | ref = decl; | |
2448 | *type = TREE_TYPE (ref); | |
2449 | } | |
339a28b9 ZW |
2450 | else if (fun) |
2451 | /* Implicit function declaration. */ | |
c2255bc4 | 2452 | ref = implicitly_declare (loc, id); |
339a28b9 ZW |
2453 | else if (decl == error_mark_node) |
2454 | /* Don't complain about something that's already been | |
2455 | complained about. */ | |
2456 | return error_mark_node; | |
2457 | else | |
2458 | { | |
c2255bc4 | 2459 | undeclared_variable (loc, id); |
339a28b9 ZW |
2460 | return error_mark_node; |
2461 | } | |
7e585d16 ZW |
2462 | |
2463 | if (TREE_TYPE (ref) == error_mark_node) | |
2464 | return error_mark_node; | |
2465 | ||
339a28b9 | 2466 | if (TREE_DEPRECATED (ref)) |
9b86d6bb | 2467 | warn_deprecated_use (ref, NULL_TREE); |
339a28b9 | 2468 | |
ad960f56 | 2469 | /* Recursive call does not count as usage. */ |
b8698a0f | 2470 | if (ref != current_function_decl) |
ad960f56 | 2471 | { |
ad960f56 MLI |
2472 | TREE_USED (ref) = 1; |
2473 | } | |
7e585d16 | 2474 | |
bc4b653b JM |
2475 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) |
2476 | { | |
2477 | if (!in_sizeof && !in_typeof) | |
2478 | C_DECL_USED (ref) = 1; | |
2479 | else if (DECL_INITIAL (ref) == 0 | |
2480 | && DECL_EXTERNAL (ref) | |
2481 | && !TREE_PUBLIC (ref)) | |
2482 | record_maybe_used_decl (ref); | |
2483 | } | |
2484 | ||
7e585d16 ZW |
2485 | if (TREE_CODE (ref) == CONST_DECL) |
2486 | { | |
6193b8b7 | 2487 | used_types_insert (TREE_TYPE (ref)); |
24b97832 ILT |
2488 | |
2489 | if (warn_cxx_compat | |
2490 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2491 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2492 | { | |
2493 | warning_at (loc, OPT_Wc___compat, | |
2494 | ("enum constant defined in struct or union " | |
2495 | "is not visible in C++")); | |
2496 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2497 | } | |
2498 | ||
7e585d16 ZW |
2499 | ref = DECL_INITIAL (ref); |
2500 | TREE_CONSTANT (ref) = 1; | |
2501 | } | |
6a29edea | 2502 | else if (current_function_decl != 0 |
4b1e44be | 2503 | && !DECL_FILE_SCOPE_P (current_function_decl) |
6a29edea EB |
2504 | && (TREE_CODE (ref) == VAR_DECL |
2505 | || TREE_CODE (ref) == PARM_DECL | |
2506 | || TREE_CODE (ref) == FUNCTION_DECL)) | |
2507 | { | |
2508 | tree context = decl_function_context (ref); | |
2f6e4e97 | 2509 | |
6a29edea EB |
2510 | if (context != 0 && context != current_function_decl) |
2511 | DECL_NONLOCAL (ref) = 1; | |
2512 | } | |
71113fcd GK |
2513 | /* C99 6.7.4p3: An inline definition of a function with external |
2514 | linkage ... shall not contain a reference to an identifier with | |
2515 | internal linkage. */ | |
2516 | else if (current_function_decl != 0 | |
2517 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2518 | && DECL_EXTERNAL (current_function_decl) | |
2519 | && VAR_OR_FUNCTION_DECL_P (ref) | |
2520 | && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref)) | |
1033ffa8 JJ |
2521 | && ! TREE_PUBLIC (ref) |
2522 | && DECL_CONTEXT (ref) != current_function_decl) | |
991d6621 JM |
2523 | record_inline_static (loc, current_function_decl, ref, |
2524 | csi_internal); | |
7e585d16 ZW |
2525 | |
2526 | return ref; | |
2527 | } | |
2528 | ||
bc4b653b JM |
2529 | /* Record details of decls possibly used inside sizeof or typeof. */ |
2530 | struct maybe_used_decl | |
2531 | { | |
2532 | /* The decl. */ | |
2533 | tree decl; | |
2534 | /* The level seen at (in_sizeof + in_typeof). */ | |
2535 | int level; | |
2536 | /* The next one at this level or above, or NULL. */ | |
2537 | struct maybe_used_decl *next; | |
2538 | }; | |
2539 | ||
2540 | static struct maybe_used_decl *maybe_used_decls; | |
2541 | ||
2542 | /* Record that DECL, an undefined static function reference seen | |
2543 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2544 | a VLA type or the operand of typeof is a variably modified | |
2545 | type. */ | |
2546 | ||
4e2fb7de | 2547 | static void |
bc4b653b JM |
2548 | record_maybe_used_decl (tree decl) |
2549 | { | |
2550 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2551 | t->decl = decl; | |
2552 | t->level = in_sizeof + in_typeof; | |
2553 | t->next = maybe_used_decls; | |
2554 | maybe_used_decls = t; | |
2555 | } | |
2556 | ||
2557 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2558 | USED is false, just discard them. If it is true, mark them used | |
2559 | (if no longer inside sizeof or typeof) or move them to the next | |
2560 | level up (if still inside sizeof or typeof). */ | |
2561 | ||
2562 | void | |
2563 | pop_maybe_used (bool used) | |
2564 | { | |
2565 | struct maybe_used_decl *p = maybe_used_decls; | |
2566 | int cur_level = in_sizeof + in_typeof; | |
2567 | while (p && p->level > cur_level) | |
2568 | { | |
2569 | if (used) | |
2570 | { | |
2571 | if (cur_level == 0) | |
2572 | C_DECL_USED (p->decl) = 1; | |
2573 | else | |
2574 | p->level = cur_level; | |
2575 | } | |
2576 | p = p->next; | |
2577 | } | |
2578 | if (!used || cur_level == 0) | |
2579 | maybe_used_decls = p; | |
2580 | } | |
2581 | ||
2582 | /* Return the result of sizeof applied to EXPR. */ | |
2583 | ||
2584 | struct c_expr | |
c2255bc4 | 2585 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) |
bc4b653b JM |
2586 | { |
2587 | struct c_expr ret; | |
ad97f4be JM |
2588 | if (expr.value == error_mark_node) |
2589 | { | |
2590 | ret.value = error_mark_node; | |
2591 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 2592 | ret.original_type = NULL; |
ad97f4be JM |
2593 | pop_maybe_used (false); |
2594 | } | |
2595 | else | |
2596 | { | |
928c19bb JM |
2597 | bool expr_const_operands = true; |
2598 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, | |
2599 | &expr_const_operands); | |
c2255bc4 | 2600 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); |
1a4049e7 JJ |
2601 | c_last_sizeof_arg = expr.value; |
2602 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2603 | ret.original_type = NULL; |
928c19bb | 2604 | if (c_vla_type_p (TREE_TYPE (folded_expr))) |
52ffd86e MS |
2605 | { |
2606 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
928c19bb JM |
2607 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2608 | folded_expr, ret.value); | |
2609 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
c2255bc4 | 2610 | SET_EXPR_LOCATION (ret.value, loc); |
52ffd86e | 2611 | } |
928c19bb | 2612 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); |
ad97f4be | 2613 | } |
bc4b653b JM |
2614 | return ret; |
2615 | } | |
2616 | ||
2617 | /* Return the result of sizeof applied to T, a structure for the type | |
c2255bc4 AH |
2618 | name passed to sizeof (rather than the type itself). LOC is the |
2619 | location of the original expression. */ | |
bc4b653b JM |
2620 | |
2621 | struct c_expr | |
c2255bc4 | 2622 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) |
bc4b653b JM |
2623 | { |
2624 | tree type; | |
2625 | struct c_expr ret; | |
928c19bb JM |
2626 | tree type_expr = NULL_TREE; |
2627 | bool type_expr_const = true; | |
2628 | type = groktypename (t, &type_expr, &type_expr_const); | |
c2255bc4 | 2629 | ret.value = c_sizeof (loc, type); |
1a4049e7 JJ |
2630 | c_last_sizeof_arg = type; |
2631 | ret.original_code = SIZEOF_EXPR; | |
6866c6e8 | 2632 | ret.original_type = NULL; |
24070fcb JM |
2633 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) |
2634 | && c_vla_type_p (type)) | |
928c19bb | 2635 | { |
24070fcb JM |
2636 | /* If the type is a [*] array, it is a VLA but is represented as |
2637 | having a size of zero. In such a case we must ensure that | |
2638 | the result of sizeof does not get folded to a constant by | |
2639 | c_fully_fold, because if the size is evaluated the result is | |
2640 | not constant and so constraints on zero or negative size | |
2641 | arrays must not be applied when this sizeof call is inside | |
2642 | another array declarator. */ | |
2643 | if (!type_expr) | |
2644 | type_expr = integer_zero_node; | |
928c19bb JM |
2645 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2646 | type_expr, ret.value); | |
2647 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2648 | } | |
16464cc1 VR |
2649 | pop_maybe_used (type != error_mark_node |
2650 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
bc4b653b JM |
2651 | return ret; |
2652 | } | |
2653 | ||
400fbf9f | 2654 | /* Build a function call to function FUNCTION with parameters PARAMS. |
c2255bc4 | 2655 | The function call is at LOC. |
400fbf9f JW |
2656 | PARAMS is a list--a chain of TREE_LIST nodes--in which the |
2657 | TREE_VALUE of each node is a parameter-expression. | |
2658 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2659 | ||
2660 | tree | |
c2255bc4 | 2661 | build_function_call (location_t loc, tree function, tree params) |
bbbbb16a | 2662 | { |
9771b263 | 2663 | vec<tree, va_gc> *v; |
bbbbb16a ILT |
2664 | tree ret; |
2665 | ||
9771b263 | 2666 | vec_alloc (v, list_length (params)); |
bbbbb16a | 2667 | for (; params; params = TREE_CHAIN (params)) |
9771b263 DN |
2668 | v->quick_push (TREE_VALUE (params)); |
2669 | ret = build_function_call_vec (loc, function, v, NULL); | |
2670 | vec_free (v); | |
bbbbb16a ILT |
2671 | return ret; |
2672 | } | |
2673 | ||
ae52741c MLI |
2674 | /* Give a note about the location of the declaration of DECL. */ |
2675 | ||
2676 | static void inform_declaration (tree decl) | |
2677 | { | |
2678 | if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl))) | |
2679 | inform (DECL_SOURCE_LOCATION (decl), "declared here"); | |
2680 | } | |
2681 | ||
bbbbb16a ILT |
2682 | /* Build a function call to function FUNCTION with parameters PARAMS. |
2683 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2684 | either NULL or the original type of the corresponding element in | |
2685 | PARAMS. The original type may differ from TREE_TYPE of the | |
2686 | parameter for enums. FUNCTION's data type may be a function type | |
2687 | or pointer-to-function. This function changes the elements of | |
2688 | PARAMS. */ | |
2689 | ||
2690 | tree | |
9771b263 DN |
2691 | build_function_call_vec (location_t loc, tree function, |
2692 | vec<tree, va_gc> *params, | |
2693 | vec<tree, va_gc> *origtypes) | |
400fbf9f | 2694 | { |
b3694847 | 2695 | tree fntype, fundecl = 0; |
4977bab6 | 2696 | tree name = NULL_TREE, result; |
c96f4f73 | 2697 | tree tem; |
94a0dd7b SL |
2698 | int nargs; |
2699 | tree *argarray; | |
b8698a0f | 2700 | |
400fbf9f | 2701 | |
fc76e425 | 2702 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 2703 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
2704 | |
2705 | /* Convert anything with function type to a pointer-to-function. */ | |
2706 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2707 | { | |
58646b77 PB |
2708 | /* Implement type-directed function overloading for builtins. |
2709 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
2710 | handle all the type checking. The result is a complete expression | |
2711 | that implements this function call. */ | |
c2255bc4 | 2712 | tem = resolve_overloaded_builtin (loc, function, params); |
58646b77 PB |
2713 | if (tem) |
2714 | return tem; | |
48ae6c13 | 2715 | |
400fbf9f | 2716 | name = DECL_NAME (function); |
0a35513e AH |
2717 | |
2718 | if (flag_tm) | |
2719 | tm_malloc_replacement (function); | |
a5eadacc | 2720 | fundecl = function; |
86951993 AM |
2721 | /* Atomic functions have type checking/casting already done. They are |
2722 | often rewritten and don't match the original parameter list. */ | |
2723 | if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) | |
2724 | origtypes = NULL; | |
400fbf9f | 2725 | } |
f2a71bbc | 2726 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) |
c2255bc4 | 2727 | function = function_to_pointer_conversion (loc, function); |
400fbf9f | 2728 | |
6e955430 ZL |
2729 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2730 | expressions, like those used for ObjC messenger dispatches. */ | |
9771b263 DN |
2731 | if (params && !params->is_empty ()) |
2732 | function = objc_rewrite_function_call (function, (*params)[0]); | |
6e955430 | 2733 | |
928c19bb JM |
2734 | function = c_fully_fold (function, false, NULL); |
2735 | ||
400fbf9f JW |
2736 | fntype = TREE_TYPE (function); |
2737 | ||
2738 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2739 | return error_mark_node; | |
2740 | ||
2741 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2742 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2743 | { | |
ae52741c MLI |
2744 | if (!flag_diagnostics_show_caret) |
2745 | error_at (loc, | |
2746 | "called object %qE is not a function or function pointer", | |
2747 | function); | |
2748 | else if (DECL_P (function)) | |
2749 | { | |
2750 | error_at (loc, | |
2751 | "called object %qD is not a function or function pointer", | |
2752 | function); | |
2753 | inform_declaration (function); | |
2754 | } | |
2755 | else | |
2756 | error_at (loc, | |
2757 | "called object is not a function or function pointer"); | |
400fbf9f JW |
2758 | return error_mark_node; |
2759 | } | |
2760 | ||
5ce89b2e JM |
2761 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
2762 | current_function_returns_abnormally = 1; | |
2763 | ||
400fbf9f JW |
2764 | /* fntype now gets the type of function pointed to. */ |
2765 | fntype = TREE_TYPE (fntype); | |
2766 | ||
ab4194da JM |
2767 | /* Convert the parameters to the types declared in the |
2768 | function prototype, or apply default promotions. */ | |
2769 | ||
bbbbb16a ILT |
2770 | nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes, |
2771 | function, fundecl); | |
ab4194da JM |
2772 | if (nargs < 0) |
2773 | return error_mark_node; | |
2774 | ||
c96f4f73 EB |
2775 | /* Check that the function is called through a compatible prototype. |
2776 | If it is not, replace the call by a trap, wrapped up in a compound | |
2777 | expression if necessary. This has the nice side-effect to prevent | |
2778 | the tree-inliner from generating invalid assignment trees which may | |
58393038 | 2779 | blow up in the RTL expander later. */ |
1043771b | 2780 | if (CONVERT_EXPR_P (function) |
c96f4f73 EB |
2781 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR |
2782 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
3f75a254 | 2783 | && !comptypes (fntype, TREE_TYPE (tem))) |
c96f4f73 EB |
2784 | { |
2785 | tree return_type = TREE_TYPE (fntype); | |
e79983f4 MM |
2786 | tree trap = build_function_call (loc, |
2787 | builtin_decl_explicit (BUILT_IN_TRAP), | |
c96f4f73 | 2788 | NULL_TREE); |
ab4194da | 2789 | int i; |
c96f4f73 EB |
2790 | |
2791 | /* This situation leads to run-time undefined behavior. We can't, | |
2792 | therefore, simply error unless we can prove that all possible | |
2793 | executions of the program must execute the code. */ | |
c2255bc4 | 2794 | if (warning_at (loc, 0, "function called through a non-compatible type")) |
71205d17 MLI |
2795 | /* We can, however, treat "undefined" any way we please. |
2796 | Call abort to encourage the user to fix the program. */ | |
c2255bc4 | 2797 | inform (loc, "if this code is reached, the program will abort"); |
ab4194da JM |
2798 | /* Before the abort, allow the function arguments to exit or |
2799 | call longjmp. */ | |
2800 | for (i = 0; i < nargs; i++) | |
9771b263 | 2801 | trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap); |
bba745c1 | 2802 | |
c96f4f73 | 2803 | if (VOID_TYPE_P (return_type)) |
3ce62965 JM |
2804 | { |
2805 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
db3927fb | 2806 | pedwarn (loc, 0, |
3ce62965 JM |
2807 | "function with qualified void return type called"); |
2808 | return trap; | |
2809 | } | |
c96f4f73 EB |
2810 | else |
2811 | { | |
2812 | tree rhs; | |
2813 | ||
2814 | if (AGGREGATE_TYPE_P (return_type)) | |
c2255bc4 | 2815 | rhs = build_compound_literal (loc, return_type, |
9771b263 DN |
2816 | build_constructor (return_type, |
2817 | NULL), | |
928c19bb | 2818 | false); |
c96f4f73 | 2819 | else |
e8160c9a | 2820 | rhs = build_zero_cst (return_type); |
c96f4f73 | 2821 | |
3ce62965 JM |
2822 | return require_complete_type (build2 (COMPOUND_EXPR, return_type, |
2823 | trap, rhs)); | |
c96f4f73 EB |
2824 | } |
2825 | } | |
2826 | ||
9771b263 | 2827 | argarray = vec_safe_address (params); |
bbbbb16a | 2828 | |
83322951 RG |
2829 | /* Check that arguments to builtin functions match the expectations. */ |
2830 | if (fundecl | |
2831 | && DECL_BUILT_IN (fundecl) | |
2832 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2833 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2834 | return error_mark_node; | |
400fbf9f | 2835 | |
83322951 | 2836 | /* Check that the arguments to the function are valid. */ |
dde05067 | 2837 | check_function_arguments (fntype, nargs, argarray); |
400fbf9f | 2838 | |
928c19bb JM |
2839 | if (name != NULL_TREE |
2840 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
bf730f15 | 2841 | { |
928c19bb | 2842 | if (require_constant_value) |
b8698a0f | 2843 | result = |
db3927fb AH |
2844 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), |
2845 | function, nargs, argarray); | |
928c19bb | 2846 | else |
db3927fb AH |
2847 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), |
2848 | function, nargs, argarray); | |
928c19bb JM |
2849 | if (TREE_CODE (result) == NOP_EXPR |
2850 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2851 | STRIP_TYPE_NOPS (result); | |
bf730f15 RS |
2852 | } |
2853 | else | |
db3927fb AH |
2854 | result = build_call_array_loc (loc, TREE_TYPE (fntype), |
2855 | function, nargs, argarray); | |
b0b3afb2 | 2856 | |
71653180 | 2857 | if (VOID_TYPE_P (TREE_TYPE (result))) |
3ce62965 JM |
2858 | { |
2859 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
db3927fb | 2860 | pedwarn (loc, 0, |
3ce62965 JM |
2861 | "function with qualified void return type called"); |
2862 | return result; | |
2863 | } | |
1eb8759b | 2864 | return require_complete_type (result); |
400fbf9f JW |
2865 | } |
2866 | \f | |
bbbbb16a ILT |
2867 | /* Convert the argument expressions in the vector VALUES |
2868 | to the types in the list TYPELIST. | |
400fbf9f JW |
2869 | |
2870 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
2871 | perform the default conversions. | |
2872 | ||
bbbbb16a ILT |
2873 | ORIGTYPES is the original types of the expressions in VALUES. This |
2874 | holds the type of enum values which have been converted to integral | |
2875 | types. It may be NULL. | |
400fbf9f | 2876 | |
03dafa61 JM |
2877 | FUNCTION is a tree for the called function. It is used only for |
2878 | error messages, where it is formatted with %qE. | |
400fbf9f JW |
2879 | |
2880 | This is also where warnings about wrong number of args are generated. | |
2881 | ||
94a0dd7b | 2882 | Returns the actual number of arguments processed (which may be less |
bbbbb16a ILT |
2883 | than the length of VALUES in some error situations), or -1 on |
2884 | failure. */ | |
94a0dd7b SL |
2885 | |
2886 | static int | |
9771b263 DN |
2887 | convert_arguments (tree typelist, vec<tree, va_gc> *values, |
2888 | vec<tree, va_gc> *origtypes, tree function, tree fundecl) | |
400fbf9f | 2889 | { |
bbbbb16a ILT |
2890 | tree typetail, val; |
2891 | unsigned int parmnum; | |
06302a02 | 2892 | bool error_args = false; |
b5d32c25 KG |
2893 | const bool type_generic = fundecl |
2894 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl))); | |
8ce94e44 | 2895 | bool type_generic_remove_excess_precision = false; |
03dafa61 | 2896 | tree selector; |
03dafa61 | 2897 | |
2ac2f164 JM |
2898 | /* Change pointer to function to the function itself for |
2899 | diagnostics. */ | |
03dafa61 JM |
2900 | if (TREE_CODE (function) == ADDR_EXPR |
2901 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
2ac2f164 | 2902 | function = TREE_OPERAND (function, 0); |
03dafa61 JM |
2903 | |
2904 | /* Handle an ObjC selector specially for diagnostics. */ | |
2905 | selector = objc_message_selector (); | |
400fbf9f | 2906 | |
8ce94e44 JM |
2907 | /* For type-generic built-in functions, determine whether excess |
2908 | precision should be removed (classification) or not | |
2909 | (comparison). */ | |
2910 | if (type_generic | |
2911 | && DECL_BUILT_IN (fundecl) | |
2912 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
2913 | { | |
2914 | switch (DECL_FUNCTION_CODE (fundecl)) | |
2915 | { | |
2916 | case BUILT_IN_ISFINITE: | |
2917 | case BUILT_IN_ISINF: | |
2918 | case BUILT_IN_ISINF_SIGN: | |
2919 | case BUILT_IN_ISNAN: | |
2920 | case BUILT_IN_ISNORMAL: | |
2921 | case BUILT_IN_FPCLASSIFY: | |
2922 | type_generic_remove_excess_precision = true; | |
2923 | break; | |
2924 | ||
2925 | default: | |
2926 | type_generic_remove_excess_precision = false; | |
2927 | break; | |
2928 | } | |
2929 | } | |
2930 | ||
400fbf9f | 2931 | /* Scan the given expressions and types, producing individual |
bbbbb16a | 2932 | converted arguments. */ |
400fbf9f | 2933 | |
bbbbb16a | 2934 | for (typetail = typelist, parmnum = 0; |
9771b263 | 2935 | values && values->iterate (parmnum, &val); |
bbbbb16a | 2936 | ++parmnum) |
400fbf9f | 2937 | { |
b3694847 | 2938 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
8ce94e44 | 2939 | tree valtype = TREE_TYPE (val); |
03dafa61 JM |
2940 | tree rname = function; |
2941 | int argnum = parmnum + 1; | |
4d3e6fae | 2942 | const char *invalid_func_diag; |
8ce94e44 | 2943 | bool excess_precision = false; |
928c19bb | 2944 | bool npc; |
bbbbb16a | 2945 | tree parmval; |
400fbf9f JW |
2946 | |
2947 | if (type == void_type_node) | |
2948 | { | |
19dc6d01 NP |
2949 | if (selector) |
2950 | error_at (input_location, | |
2951 | "too many arguments to method %qE", selector); | |
2952 | else | |
2953 | error_at (input_location, | |
2954 | "too many arguments to function %qE", function); | |
ae52741c | 2955 | inform_declaration (fundecl); |
94a0dd7b | 2956 | return parmnum; |
400fbf9f JW |
2957 | } |
2958 | ||
03dafa61 JM |
2959 | if (selector && argnum > 2) |
2960 | { | |
2961 | rname = selector; | |
2962 | argnum -= 2; | |
2963 | } | |
2964 | ||
928c19bb | 2965 | npc = null_pointer_constant_p (val); |
8ce94e44 JM |
2966 | |
2967 | /* If there is excess precision and a prototype, convert once to | |
2968 | the required type rather than converting via the semantic | |
2969 | type. Likewise without a prototype a float value represented | |
2970 | as long double should be converted once to double. But for | |
2971 | type-generic classification functions excess precision must | |
2972 | be removed here. */ | |
2973 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
2974 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
2975 | { | |
2976 | val = TREE_OPERAND (val, 0); | |
2977 | excess_precision = true; | |
2978 | } | |
928c19bb | 2979 | val = c_fully_fold (val, false, NULL); |
ed248cf7 | 2980 | STRIP_TYPE_NOPS (val); |
400fbf9f | 2981 | |
400fbf9f JW |
2982 | val = require_complete_type (val); |
2983 | ||
2984 | if (type != 0) | |
2985 | { | |
2986 | /* Formal parm type is specified by a function prototype. */ | |
400fbf9f | 2987 | |
20913689 | 2988 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) |
400fbf9f JW |
2989 | { |
2990 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
2991 | parmval = val; | |
2992 | } | |
2993 | else | |
2994 | { | |
bbbbb16a ILT |
2995 | tree origtype; |
2996 | ||
d45cf215 RS |
2997 | /* Optionally warn about conversions that |
2998 | differ from the default conversions. */ | |
05170031 | 2999 | if (warn_traditional_conversion || warn_traditional) |
400fbf9f | 3000 | { |
e3a64162 | 3001 | unsigned int formal_prec = TYPE_PRECISION (type); |
400fbf9f | 3002 | |
aae43c5f | 3003 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3004 | && TREE_CODE (valtype) == REAL_TYPE) |
d4ee4d25 | 3005 | warning (0, "passing argument %d of %qE as integer " |
03dafa61 JM |
3006 | "rather than floating due to prototype", |
3007 | argnum, rname); | |
03829ad2 | 3008 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 3009 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
d4ee4d25 | 3010 | warning (0, "passing argument %d of %qE as integer " |
03dafa61 JM |
3011 | "rather than complex due to prototype", |
3012 | argnum, rname); | |
aae43c5f | 3013 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3014 | && TREE_CODE (valtype) == REAL_TYPE) |
d4ee4d25 | 3015 | warning (0, "passing argument %d of %qE as complex " |
03dafa61 JM |
3016 | "rather than floating due to prototype", |
3017 | argnum, rname); | |
400fbf9f | 3018 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3019 | && INTEGRAL_TYPE_P (valtype)) |
d4ee4d25 | 3020 | warning (0, "passing argument %d of %qE as floating " |
03dafa61 JM |
3021 | "rather than integer due to prototype", |
3022 | argnum, rname); | |
03829ad2 | 3023 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 3024 | && INTEGRAL_TYPE_P (valtype)) |
d4ee4d25 | 3025 | warning (0, "passing argument %d of %qE as complex " |
03dafa61 JM |
3026 | "rather than integer due to prototype", |
3027 | argnum, rname); | |
aae43c5f | 3028 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3029 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
d4ee4d25 | 3030 | warning (0, "passing argument %d of %qE as floating " |
03dafa61 JM |
3031 | "rather than complex due to prototype", |
3032 | argnum, rname); | |
aae43c5f RK |
3033 | /* ??? At some point, messages should be written about |
3034 | conversions between complex types, but that's too messy | |
3035 | to do now. */ | |
d45cf215 | 3036 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 3037 | && TREE_CODE (valtype) == REAL_TYPE) |
d45cf215 RS |
3038 | { |
3039 | /* Warn if any argument is passed as `float', | |
047de90b | 3040 | since without a prototype it would be `double'. */ |
9a8ce21f JG |
3041 | if (formal_prec == TYPE_PRECISION (float_type_node) |
3042 | && type != dfloat32_type_node) | |
d4ee4d25 | 3043 | warning (0, "passing argument %d of %qE as %<float%> " |
03dafa61 JM |
3044 | "rather than %<double%> due to prototype", |
3045 | argnum, rname); | |
9a8ce21f JG |
3046 | |
3047 | /* Warn if mismatch between argument and prototype | |
3048 | for decimal float types. Warn of conversions with | |
3049 | binary float types and of precision narrowing due to | |
3050 | prototype. */ | |
8ce94e44 | 3051 | else if (type != valtype |
9a8ce21f JG |
3052 | && (type == dfloat32_type_node |
3053 | || type == dfloat64_type_node | |
c22cacf3 | 3054 | || type == dfloat128_type_node |
8ce94e44 JM |
3055 | || valtype == dfloat32_type_node |
3056 | || valtype == dfloat64_type_node | |
3057 | || valtype == dfloat128_type_node) | |
c22cacf3 | 3058 | && (formal_prec |
8ce94e44 | 3059 | <= TYPE_PRECISION (valtype) |
9a8ce21f | 3060 | || (type == dfloat128_type_node |
8ce94e44 | 3061 | && (valtype |
c22cacf3 | 3062 | != dfloat64_type_node |
8ce94e44 | 3063 | && (valtype |
9a8ce21f JG |
3064 | != dfloat32_type_node))) |
3065 | || (type == dfloat64_type_node | |
8ce94e44 | 3066 | && (valtype |
9a8ce21f JG |
3067 | != dfloat32_type_node)))) |
3068 | warning (0, "passing argument %d of %qE as %qT " | |
3069 | "rather than %qT due to prototype", | |
8ce94e44 | 3070 | argnum, rname, type, valtype); |
9a8ce21f | 3071 | |
d45cf215 | 3072 | } |
3ed56f8a KG |
3073 | /* Detect integer changing in width or signedness. |
3074 | These warnings are only activated with | |
05170031 MLI |
3075 | -Wtraditional-conversion, not with -Wtraditional. */ |
3076 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
8ce94e44 | 3077 | && INTEGRAL_TYPE_P (valtype)) |
400fbf9f | 3078 | { |
d45cf215 RS |
3079 | tree would_have_been = default_conversion (val); |
3080 | tree type1 = TREE_TYPE (would_have_been); | |
3081 | ||
754a4d82 | 3082 | if (TREE_CODE (type) == ENUMERAL_TYPE |
a38b987a | 3083 | && (TYPE_MAIN_VARIANT (type) |
8ce94e44 | 3084 | == TYPE_MAIN_VARIANT (valtype))) |
754a4d82 RS |
3085 | /* No warning if function asks for enum |
3086 | and the actual arg is that enum type. */ | |
3087 | ; | |
3088 | else if (formal_prec != TYPE_PRECISION (type1)) | |
c2255bc4 AH |
3089 | warning (OPT_Wtraditional_conversion, |
3090 | "passing argument %d of %qE " | |
3176a0c2 DD |
3091 | "with different width due to prototype", |
3092 | argnum, rname); | |
8df83eae | 3093 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
d45cf215 | 3094 | ; |
800cd3b9 RS |
3095 | /* Don't complain if the formal parameter type |
3096 | is an enum, because we can't tell now whether | |
3097 | the value was an enum--even the same enum. */ | |
3098 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
3099 | ; | |
400fbf9f JW |
3100 | else if (TREE_CODE (val) == INTEGER_CST |
3101 | && int_fits_type_p (val, type)) | |
3102 | /* Change in signedness doesn't matter | |
3103 | if a constant value is unaffected. */ | |
3104 | ; | |
ce9895ae RS |
3105 | /* If the value is extended from a narrower |
3106 | unsigned type, it doesn't matter whether we | |
3107 | pass it as signed or unsigned; the value | |
3108 | certainly is the same either way. */ | |
8ce94e44 JM |
3109 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) |
3110 | && TYPE_UNSIGNED (valtype)) | |
ce9895ae | 3111 | ; |
8df83eae | 3112 | else if (TYPE_UNSIGNED (type)) |
c2255bc4 AH |
3113 | warning (OPT_Wtraditional_conversion, |
3114 | "passing argument %d of %qE " | |
3176a0c2 DD |
3115 | "as unsigned due to prototype", |
3116 | argnum, rname); | |
3ed56f8a | 3117 | else |
c2255bc4 AH |
3118 | warning (OPT_Wtraditional_conversion, |
3119 | "passing argument %d of %qE " | |
3176a0c2 | 3120 | "as signed due to prototype", argnum, rname); |
400fbf9f JW |
3121 | } |
3122 | } | |
3123 | ||
8ce94e44 JM |
3124 | /* Possibly restore an EXCESS_PRECISION_EXPR for the |
3125 | sake of better warnings from convert_and_check. */ | |
3126 | if (excess_precision) | |
3127 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
9771b263 | 3128 | origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; |
744aa42f ILT |
3129 | parmval = convert_for_assignment (input_location, type, val, |
3130 | origtype, ic_argpass, npc, | |
2ac2f164 JM |
3131 | fundecl, function, |
3132 | parmnum + 1); | |
2f6e4e97 | 3133 | |
61f71b34 | 3134 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
b6d6aa84 | 3135 | && INTEGRAL_TYPE_P (type) |
400fbf9f JW |
3136 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
3137 | parmval = default_conversion (parmval); | |
400fbf9f | 3138 | } |
400fbf9f | 3139 | } |
8ce94e44 JM |
3140 | else if (TREE_CODE (valtype) == REAL_TYPE |
3141 | && (TYPE_PRECISION (valtype) | |
c22cacf3 | 3142 | < TYPE_PRECISION (double_type_node)) |
8ce94e44 | 3143 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) |
b5d32c25 KG |
3144 | { |
3145 | if (type_generic) | |
bbbbb16a | 3146 | parmval = val; |
b5d32c25 | 3147 | else |
0a0b3574 MM |
3148 | { |
3149 | /* Convert `float' to `double'. */ | |
3150 | if (warn_double_promotion && !c_inhibit_evaluation_warnings) | |
3151 | warning (OPT_Wdouble_promotion, | |
3152 | "implicit conversion from %qT to %qT when passing " | |
3153 | "argument to function", | |
3154 | valtype, double_type_node); | |
3155 | parmval = convert (double_type_node, val); | |
3156 | } | |
b5d32c25 | 3157 | } |
8ce94e44 JM |
3158 | else if (excess_precision && !type_generic) |
3159 | /* A "double" argument with excess precision being passed | |
3160 | without a prototype or in variable arguments. */ | |
bbbbb16a | 3161 | parmval = convert (valtype, val); |
c22cacf3 MS |
3162 | else if ((invalid_func_diag = |
3163 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
4d3e6fae FJ |
3164 | { |
3165 | error (invalid_func_diag); | |
94a0dd7b | 3166 | return -1; |
4d3e6fae | 3167 | } |
400fbf9f JW |
3168 | else |
3169 | /* Convert `short' and `char' to full-size `int'. */ | |
bbbbb16a ILT |
3170 | parmval = default_conversion (val); |
3171 | ||
9771b263 | 3172 | (*values)[parmnum] = parmval; |
06302a02 JJ |
3173 | if (parmval == error_mark_node) |
3174 | error_args = true; | |
400fbf9f JW |
3175 | |
3176 | if (typetail) | |
3177 | typetail = TREE_CHAIN (typetail); | |
3178 | } | |
3179 | ||
9771b263 | 3180 | gcc_assert (parmnum == vec_safe_length (values)); |
94a0dd7b | 3181 | |
400fbf9f | 3182 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) |
3789b316 | 3183 | { |
f90e8e2e | 3184 | error_at (input_location, |
a98c2819 | 3185 | "too few arguments to function %qE", function); |
ae52741c | 3186 | inform_declaration (fundecl); |
94a0dd7b | 3187 | return -1; |
3789b316 | 3188 | } |
400fbf9f | 3189 | |
06302a02 | 3190 | return error_args ? -1 : (int) parmnum; |
400fbf9f JW |
3191 | } |
3192 | \f | |
43f6dfd3 RS |
3193 | /* This is the entry point used by the parser to build unary operators |
3194 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3195 | ARG is the operand. For unary plus, the C parser currently uses | |
6a3799eb AH |
3196 | CONVERT_EXPR for code. |
3197 | ||
3198 | LOC is the location to use for the tree generated. | |
3199 | */ | |
43f6dfd3 RS |
3200 | |
3201 | struct c_expr | |
c2255bc4 | 3202 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) |
43f6dfd3 RS |
3203 | { |
3204 | struct c_expr result; | |
3205 | ||
c9f9eb5d | 3206 | result.value = build_unary_op (loc, code, arg.value, 0); |
100d537d | 3207 | result.original_code = code; |
6866c6e8 ILT |
3208 | result.original_type = NULL; |
3209 | ||
59c0753d | 3210 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) |
c2255bc4 | 3211 | overflow_warning (loc, result.value); |
59c0753d | 3212 | |
43f6dfd3 RS |
3213 | return result; |
3214 | } | |
3215 | ||
3216 | /* This is the entry point used by the parser to build binary operators | |
3217 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3218 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3219 | expression, we check for operands that were written with other binary | |
ba47d38d AH |
3220 | operators in a way that is likely to confuse the user. |
3221 | ||
3222 | LOCATION is the location of the binary operator. */ | |
edc7c4ec | 3223 | |
487a92fe | 3224 | struct c_expr |
ba47d38d AH |
3225 | parser_build_binary_op (location_t location, enum tree_code code, |
3226 | struct c_expr arg1, struct c_expr arg2) | |
400fbf9f | 3227 | { |
487a92fe | 3228 | struct c_expr result; |
400fbf9f | 3229 | |
487a92fe JM |
3230 | enum tree_code code1 = arg1.original_code; |
3231 | enum tree_code code2 = arg2.original_code; | |
6866c6e8 ILT |
3232 | tree type1 = (arg1.original_type |
3233 | ? arg1.original_type | |
3234 | : TREE_TYPE (arg1.value)); | |
3235 | tree type2 = (arg2.original_type | |
3236 | ? arg2.original_type | |
3237 | : TREE_TYPE (arg2.value)); | |
400fbf9f | 3238 | |
ba47d38d AH |
3239 | result.value = build_binary_op (location, code, |
3240 | arg1.value, arg2.value, 1); | |
487a92fe | 3241 | result.original_code = code; |
6866c6e8 | 3242 | result.original_type = NULL; |
58bf601b | 3243 | |
487a92fe JM |
3244 | if (TREE_CODE (result.value) == ERROR_MARK) |
3245 | return result; | |
400fbf9f | 3246 | |
ba47d38d AH |
3247 | if (location != UNKNOWN_LOCATION) |
3248 | protected_set_expr_location (result.value, location); | |
3249 | ||
400fbf9f | 3250 | /* Check for cases such as x+y<<z which users are likely |
487a92fe | 3251 | to misinterpret. */ |
400fbf9f | 3252 | if (warn_parentheses) |
5d9de0d0 PC |
3253 | warn_about_parentheses (input_location, code, |
3254 | code1, arg1.value, code2, arg2.value); | |
001af587 | 3255 | |
ca409efd | 3256 | if (warn_logical_op) |
a243fb4a | 3257 | warn_logical_operator (input_location, code, TREE_TYPE (result.value), |
ca409efd | 3258 | code1, arg1.value, code2, arg2.value); |
63a08740 | 3259 | |
e994a705 RS |
3260 | /* Warn about comparisons against string literals, with the exception |
3261 | of testing for equality or inequality of a string literal with NULL. */ | |
3262 | if (code == EQ_EXPR || code == NE_EXPR) | |
3263 | { | |
3264 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3265 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
c2255bc4 AH |
3266 | warning_at (location, OPT_Waddress, |
3267 | "comparison with string literal results in unspecified behavior"); | |
e994a705 RS |
3268 | } |
3269 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3270 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
c2255bc4 AH |
3271 | warning_at (location, OPT_Waddress, |
3272 | "comparison with string literal results in unspecified behavior"); | |
e994a705 | 3273 | |
b8698a0f L |
3274 | if (TREE_OVERFLOW_P (result.value) |
3275 | && !TREE_OVERFLOW_P (arg1.value) | |
59c0753d | 3276 | && !TREE_OVERFLOW_P (arg2.value)) |
c2255bc4 | 3277 | overflow_warning (location, result.value); |
400fbf9f | 3278 | |
6866c6e8 ILT |
3279 | /* Warn about comparisons of different enum types. */ |
3280 | if (warn_enum_compare | |
3281 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3282 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3283 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3284 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3285 | warning_at (location, OPT_Wenum_compare, | |
3286 | "comparison between %qT and %qT", | |
3287 | type1, type2); | |
3288 | ||
400fbf9f JW |
3289 | return result; |
3290 | } | |
3e4093b6 | 3291 | \f |
3e4093b6 RS |
3292 | /* Return a tree for the difference of pointers OP0 and OP1. |
3293 | The resulting tree has type int. */ | |
293c9fdd | 3294 | |
3e4093b6 | 3295 | static tree |
db3927fb | 3296 | pointer_diff (location_t loc, tree op0, tree op1) |
3e4093b6 | 3297 | { |
3e4093b6 | 3298 | tree restype = ptrdiff_type_node; |
36c5e70a | 3299 | tree result, inttype; |
400fbf9f | 3300 | |
36c5e70a BE |
3301 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); |
3302 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
3e4093b6 RS |
3303 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
3304 | tree con0, con1, lit0, lit1; | |
3305 | tree orig_op1 = op1; | |
400fbf9f | 3306 | |
36c5e70a BE |
3307 | /* If the operands point into different address spaces, we need to |
3308 | explicitly convert them to pointers into the common address space | |
3309 | before we can subtract the numerical address values. */ | |
3310 | if (as0 != as1) | |
3311 | { | |
3312 | addr_space_t as_common; | |
3313 | tree common_type; | |
3314 | ||
3315 | /* Determine the common superset address space. This is guaranteed | |
3316 | to exist because the caller verified that comp_target_types | |
3317 | returned non-zero. */ | |
3318 | if (!addr_space_superset (as0, as1, &as_common)) | |
3319 | gcc_unreachable (); | |
3320 | ||
3321 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3322 | op0 = convert (common_type, op0); | |
3323 | op1 = convert (common_type, op1); | |
3324 | } | |
3325 | ||
3326 | /* Determine integer type to perform computations in. This will usually | |
3327 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3328 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3329 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
647d4b75 | 3330 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); |
36c5e70a BE |
3331 | else |
3332 | inttype = restype; | |
3333 | ||
3334 | ||
fcf73884 | 3335 | if (TREE_CODE (target_type) == VOID_TYPE) |
c1771a20 | 3336 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
fcf73884 MLI |
3337 | "pointer of type %<void *%> used in subtraction"); |
3338 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
c1771a20 | 3339 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
fcf73884 | 3340 | "pointer to a function used in subtraction"); |
400fbf9f | 3341 | |
3e4093b6 RS |
3342 | /* If the conversion to ptrdiff_type does anything like widening or |
3343 | converting a partial to an integral mode, we get a convert_expression | |
3344 | that is in the way to do any simplifications. | |
3345 | (fold-const.c doesn't know that the extra bits won't be needed. | |
3346 | split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a | |
3347 | different mode in place.) | |
3348 | So first try to find a common term here 'by hand'; we want to cover | |
3349 | at least the cases that occur in legal static initializers. */ | |
1043771b | 3350 | if (CONVERT_EXPR_P (op0) |
1344f9a3 JM |
3351 | && (TYPE_PRECISION (TREE_TYPE (op0)) |
3352 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))) | |
3353 | con0 = TREE_OPERAND (op0, 0); | |
3354 | else | |
3355 | con0 = op0; | |
1043771b | 3356 | if (CONVERT_EXPR_P (op1) |
1344f9a3 JM |
3357 | && (TYPE_PRECISION (TREE_TYPE (op1)) |
3358 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))) | |
3359 | con1 = TREE_OPERAND (op1, 0); | |
3360 | else | |
3361 | con1 = op1; | |
400fbf9f | 3362 | |
5de72424 | 3363 | if (TREE_CODE (con0) == POINTER_PLUS_EXPR) |
3e4093b6 RS |
3364 | { |
3365 | lit0 = TREE_OPERAND (con0, 1); | |
3366 | con0 = TREE_OPERAND (con0, 0); | |
3367 | } | |
3368 | else | |
3369 | lit0 = integer_zero_node; | |
400fbf9f | 3370 | |
5de72424 | 3371 | if (TREE_CODE (con1) == POINTER_PLUS_EXPR) |
400fbf9f | 3372 | { |
3e4093b6 RS |
3373 | lit1 = TREE_OPERAND (con1, 1); |
3374 | con1 = TREE_OPERAND (con1, 0); | |
400fbf9f JW |
3375 | } |
3376 | else | |
3e4093b6 RS |
3377 | lit1 = integer_zero_node; |
3378 | ||
3379 | if (operand_equal_p (con0, con1, 0)) | |
400fbf9f | 3380 | { |
3e4093b6 RS |
3381 | op0 = lit0; |
3382 | op1 = lit1; | |
400fbf9f JW |
3383 | } |
3384 | ||
400fbf9f | 3385 | |
3e4093b6 RS |
3386 | /* First do the subtraction as integers; |
3387 | then drop through to build the divide operator. | |
3388 | Do not do default conversions on the minus operator | |
3389 | in case restype is a short type. */ | |
400fbf9f | 3390 | |
db3927fb | 3391 | op0 = build_binary_op (loc, |
36c5e70a BE |
3392 | MINUS_EXPR, convert (inttype, op0), |
3393 | convert (inttype, op1), 0); | |
3e4093b6 RS |
3394 | /* This generates an error if op1 is pointer to incomplete type. */ |
3395 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
db3927fb | 3396 | error_at (loc, "arithmetic on pointer to an incomplete type"); |
400fbf9f | 3397 | |
3e4093b6 RS |
3398 | /* This generates an error if op0 is pointer to incomplete type. */ |
3399 | op1 = c_size_in_bytes (target_type); | |
400fbf9f | 3400 | |
3e4093b6 | 3401 | /* Divide by the size, in easiest possible way. */ |
36c5e70a BE |
3402 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, |
3403 | op0, convert (inttype, op1)); | |
3404 | ||
3405 | /* Convert to final result type if necessary. */ | |
3406 | return convert (restype, result); | |
3e4093b6 RS |
3407 | } |
3408 | \f | |
3409 | /* Construct and perhaps optimize a tree representation | |
3410 | for a unary operation. CODE, a tree_code, specifies the operation | |
3411 | and XARG is the operand. | |
3412 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3413 | the default promotions (such as from short to int). | |
3414 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3415 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
c9f9eb5d AH |
3416 | arrays to pointers in C99. |
3417 | ||
3418 | LOCATION is the location of the operator. */ | |
400fbf9f | 3419 | |
3e4093b6 | 3420 | tree |
c9f9eb5d AH |
3421 | build_unary_op (location_t location, |
3422 | enum tree_code code, tree xarg, int flag) | |
3e4093b6 RS |
3423 | { |
3424 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3425 | tree arg = xarg; | |
3426 | tree argtype = 0; | |
808d6eaa | 3427 | enum tree_code typecode; |
3e4093b6 | 3428 | tree val; |
c9f9eb5d | 3429 | tree ret = error_mark_node; |
8ce94e44 | 3430 | tree eptype = NULL_TREE; |
3e4093b6 | 3431 | int noconvert = flag; |
4de67c26 | 3432 | const char *invalid_op_diag; |
928c19bb JM |
3433 | bool int_operands; |
3434 | ||
3435 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
4d84fe7c JM |
3436 | if (int_operands) |
3437 | arg = remove_c_maybe_const_expr (arg); | |
400fbf9f | 3438 | |
808d6eaa JM |
3439 | if (code != ADDR_EXPR) |
3440 | arg = require_complete_type (arg); | |
3441 | ||
3442 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
3e4093b6 RS |
3443 | if (typecode == ERROR_MARK) |
3444 | return error_mark_node; | |
3445 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3446 | typecode = INTEGER_TYPE; | |
6c36d76b | 3447 | |
4de67c26 JM |
3448 | if ((invalid_op_diag |
3449 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3450 | { | |
c9f9eb5d | 3451 | error_at (location, invalid_op_diag); |
4de67c26 JM |
3452 | return error_mark_node; |
3453 | } | |
3454 | ||
8ce94e44 JM |
3455 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
3456 | { | |
3457 | eptype = TREE_TYPE (arg); | |
3458 | arg = TREE_OPERAND (arg, 0); | |
3459 | } | |
3460 | ||
3e4093b6 RS |
3461 | switch (code) |
3462 | { | |
3463 | case CONVERT_EXPR: | |
3464 | /* This is used for unary plus, because a CONVERT_EXPR | |
3465 | is enough to prevent anybody from looking inside for | |
3466 | associativity, but won't generate any code. */ | |
3467 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3468 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
8a2cee38 | 3469 | || typecode == VECTOR_TYPE)) |
400fbf9f | 3470 | { |
c9f9eb5d | 3471 | error_at (location, "wrong type argument to unary plus"); |
3e4093b6 | 3472 | return error_mark_node; |
400fbf9f | 3473 | } |
3e4093b6 RS |
3474 | else if (!noconvert) |
3475 | arg = default_conversion (arg); | |
db3927fb | 3476 | arg = non_lvalue_loc (location, arg); |
400fbf9f JW |
3477 | break; |
3478 | ||
3e4093b6 RS |
3479 | case NEGATE_EXPR: |
3480 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3481 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
3e4093b6 RS |
3482 | || typecode == VECTOR_TYPE)) |
3483 | { | |
c9f9eb5d | 3484 | error_at (location, "wrong type argument to unary minus"); |
3e4093b6 RS |
3485 | return error_mark_node; |
3486 | } | |
3487 | else if (!noconvert) | |
3488 | arg = default_conversion (arg); | |
400fbf9f JW |
3489 | break; |
3490 | ||
3e4093b6 | 3491 | case BIT_NOT_EXPR: |
462643f0 AP |
3492 | /* ~ works on integer types and non float vectors. */ |
3493 | if (typecode == INTEGER_TYPE | |
3494 | || (typecode == VECTOR_TYPE | |
3495 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
03d5b1f5 | 3496 | { |
3e4093b6 RS |
3497 | if (!noconvert) |
3498 | arg = default_conversion (arg); | |
03d5b1f5 | 3499 | } |
3e4093b6 | 3500 | else if (typecode == COMPLEX_TYPE) |
400fbf9f | 3501 | { |
3e4093b6 | 3502 | code = CONJ_EXPR; |
c1771a20 | 3503 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 3504 | "ISO C does not support %<~%> for complex conjugation"); |
3e4093b6 RS |
3505 | if (!noconvert) |
3506 | arg = default_conversion (arg); | |
3507 | } | |
3508 | else | |
3509 | { | |
c9f9eb5d | 3510 | error_at (location, "wrong type argument to bit-complement"); |
3e4093b6 | 3511 | return error_mark_node; |
400fbf9f JW |
3512 | } |
3513 | break; | |
3514 | ||
3e4093b6 | 3515 | case ABS_EXPR: |
11017cc7 | 3516 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
400fbf9f | 3517 | { |
c9f9eb5d | 3518 | error_at (location, "wrong type argument to abs"); |
3e4093b6 | 3519 | return error_mark_node; |
400fbf9f | 3520 | } |
3e4093b6 RS |
3521 | else if (!noconvert) |
3522 | arg = default_conversion (arg); | |
400fbf9f JW |
3523 | break; |
3524 | ||
3e4093b6 RS |
3525 | case CONJ_EXPR: |
3526 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3527 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3528 | || typecode == COMPLEX_TYPE)) | |
400fbf9f | 3529 | { |
c9f9eb5d | 3530 | error_at (location, "wrong type argument to conjugation"); |
3e4093b6 | 3531 | return error_mark_node; |
400fbf9f | 3532 | } |
3e4093b6 RS |
3533 | else if (!noconvert) |
3534 | arg = default_conversion (arg); | |
400fbf9f JW |
3535 | break; |
3536 | ||
3e4093b6 | 3537 | case TRUTH_NOT_EXPR: |
ab22c1fa | 3538 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3539 | && typecode != REAL_TYPE && typecode != POINTER_TYPE |
46bdb9cf | 3540 | && typecode != COMPLEX_TYPE) |
400fbf9f | 3541 | { |
c9f9eb5d AH |
3542 | error_at (location, |
3543 | "wrong type argument to unary exclamation mark"); | |
3e4093b6 | 3544 | return error_mark_node; |
400fbf9f | 3545 | } |
a27d595d JM |
3546 | if (int_operands) |
3547 | { | |
3548 | arg = c_objc_common_truthvalue_conversion (location, xarg); | |
3549 | arg = remove_c_maybe_const_expr (arg); | |
3550 | } | |
3551 | else | |
3552 | arg = c_objc_common_truthvalue_conversion (location, arg); | |
db3927fb | 3553 | ret = invert_truthvalue_loc (location, arg); |
ca80e52b EB |
3554 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ |
3555 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3556 | location = EXPR_LOCATION (ret); | |
c9f9eb5d | 3557 | goto return_build_unary_op; |
3e4093b6 | 3558 | |
3e4093b6 | 3559 | case REALPART_EXPR: |
3e4093b6 | 3560 | case IMAGPART_EXPR: |
fb52b50a NF |
3561 | ret = build_real_imag_expr (location, code, arg); |
3562 | if (ret == error_mark_node) | |
3563 | return error_mark_node; | |
8ce94e44 JM |
3564 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3565 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3566 | goto return_build_unary_op; |
3e4093b6 RS |
3567 | |
3568 | case PREINCREMENT_EXPR: | |
3569 | case POSTINCREMENT_EXPR: | |
3570 | case PREDECREMENT_EXPR: | |
3571 | case POSTDECREMENT_EXPR: | |
3e4093b6 | 3572 | |
928c19bb JM |
3573 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) |
3574 | { | |
3575 | tree inner = build_unary_op (location, code, | |
3576 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3577 | if (inner == error_mark_node) | |
3578 | return error_mark_node; | |
3579 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3580 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3581 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3582 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3583 | goto return_build_unary_op; | |
3584 | } | |
3585 | ||
925e8657 NP |
3586 | /* Complain about anything that is not a true lvalue. In |
3587 | Objective-C, skip this check for property_refs. */ | |
f90e8e2e | 3588 | if (!objc_is_property_ref (arg) |
7bd11157 TT |
3589 | && !lvalue_or_else (location, |
3590 | arg, ((code == PREINCREMENT_EXPR | |
925e8657 NP |
3591 | || code == POSTINCREMENT_EXPR) |
3592 | ? lv_increment | |
3593 | : lv_decrement))) | |
928c19bb JM |
3594 | return error_mark_node; |
3595 | ||
09639a83 ILT |
3596 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) |
3597 | { | |
3598 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3599 | warning_at (location, OPT_Wc___compat, | |
3600 | "increment of enumeration value is invalid in C++"); | |
3601 | else | |
3602 | warning_at (location, OPT_Wc___compat, | |
3603 | "decrement of enumeration value is invalid in C++"); | |
3604 | } | |
3605 | ||
928c19bb JM |
3606 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ |
3607 | arg = c_fully_fold (arg, false, NULL); | |
3608 | ||
3e4093b6 RS |
3609 | /* Increment or decrement the real part of the value, |
3610 | and don't change the imaginary part. */ | |
3611 | if (typecode == COMPLEX_TYPE) | |
400fbf9f | 3612 | { |
3e4093b6 RS |
3613 | tree real, imag; |
3614 | ||
c1771a20 | 3615 | pedwarn (location, OPT_Wpedantic, |
509c9d60 | 3616 | "ISO C does not support %<++%> and %<--%> on complex types"); |
3e4093b6 RS |
3617 | |
3618 | arg = stabilize_reference (arg); | |
c9f9eb5d AH |
3619 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); |
3620 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
3621 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
a87db577 AP |
3622 | if (real == error_mark_node || imag == error_mark_node) |
3623 | return error_mark_node; | |
c9f9eb5d AH |
3624 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
3625 | real, imag); | |
3626 | goto return_build_unary_op; | |
400fbf9f | 3627 | } |
3e4093b6 RS |
3628 | |
3629 | /* Report invalid types. */ | |
3630 | ||
ab22c1fa | 3631 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3632 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE) |
400fbf9f | 3633 | { |
3e4093b6 | 3634 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c9f9eb5d | 3635 | error_at (location, "wrong type argument to increment"); |
c22cacf3 | 3636 | else |
c9f9eb5d | 3637 | error_at (location, "wrong type argument to decrement"); |
3e4093b6 RS |
3638 | |
3639 | return error_mark_node; | |
400fbf9f | 3640 | } |
400fbf9f | 3641 | |
3e4093b6 RS |
3642 | { |
3643 | tree inc; | |
400fbf9f | 3644 | |
3e4093b6 RS |
3645 | argtype = TREE_TYPE (arg); |
3646 | ||
3647 | /* Compute the increment. */ | |
3648 | ||
3649 | if (typecode == POINTER_TYPE) | |
3650 | { | |
3651 | /* If pointer target is an undefined struct, | |
3652 | we just cannot know how to do the arithmetic. */ | |
b70cef5d | 3653 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) |
3e4093b6 RS |
3654 | { |
3655 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
c9f9eb5d AH |
3656 | error_at (location, |
3657 | "increment of pointer to unknown structure"); | |
3e4093b6 | 3658 | else |
c9f9eb5d AH |
3659 | error_at (location, |
3660 | "decrement of pointer to unknown structure"); | |
3e4093b6 | 3661 | } |
b70cef5d JJ |
3662 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE |
3663 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
c22cacf3 | 3664 | { |
3e4093b6 | 3665 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c1771a20 | 3666 | pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
fcf73884 | 3667 | "wrong type argument to increment"); |
3e4093b6 | 3668 | else |
c1771a20 | 3669 | pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
fcf73884 | 3670 | "wrong type argument to decrement"); |
3e4093b6 RS |
3671 | } |
3672 | ||
b70cef5d | 3673 | inc = c_size_in_bytes (TREE_TYPE (argtype)); |
0d82a1c8 | 3674 | inc = convert_to_ptrofftype_loc (location, inc); |
3e4093b6 | 3675 | } |
b70cef5d | 3676 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) |
ab22c1fa CF |
3677 | { |
3678 | /* For signed fract types, we invert ++ to -- or | |
3679 | -- to ++, and change inc from 1 to -1, because | |
3680 | it is not possible to represent 1 in signed fract constants. | |
3681 | For unsigned fract types, the result always overflows and | |
3682 | we get an undefined (original) or the maximum value. */ | |
3683 | if (code == PREINCREMENT_EXPR) | |
3684 | code = PREDECREMENT_EXPR; | |
3685 | else if (code == PREDECREMENT_EXPR) | |
3686 | code = PREINCREMENT_EXPR; | |
3687 | else if (code == POSTINCREMENT_EXPR) | |
3688 | code = POSTDECREMENT_EXPR; | |
3689 | else /* code == POSTDECREMENT_EXPR */ | |
3690 | code = POSTINCREMENT_EXPR; | |
3691 | ||
3692 | inc = integer_minus_one_node; | |
3693 | inc = convert (argtype, inc); | |
3694 | } | |
3e4093b6 | 3695 | else |
5be014d5 AP |
3696 | { |
3697 | inc = integer_one_node; | |
3698 | inc = convert (argtype, inc); | |
3699 | } | |
3e4093b6 | 3700 | |
925e8657 NP |
3701 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we |
3702 | need to ask Objective-C to build the increment or decrement | |
3703 | expression for it. */ | |
3704 | if (objc_is_property_ref (arg)) | |
f90e8e2e | 3705 | return objc_build_incr_expr_for_property_ref (location, code, |
925e8657 NP |
3706 | arg, inc); |
3707 | ||
3e4093b6 | 3708 | /* Report a read-only lvalue. */ |
f37acdf9 | 3709 | if (TYPE_READONLY (argtype)) |
953ff289 DN |
3710 | { |
3711 | readonly_error (arg, | |
3712 | ((code == PREINCREMENT_EXPR | |
3713 | || code == POSTINCREMENT_EXPR) | |
3714 | ? lv_increment : lv_decrement)); | |
3715 | return error_mark_node; | |
3716 | } | |
f37acdf9 JM |
3717 | else if (TREE_READONLY (arg)) |
3718 | readonly_warning (arg, | |
3719 | ((code == PREINCREMENT_EXPR | |
3720 | || code == POSTINCREMENT_EXPR) | |
3721 | ? lv_increment : lv_decrement)); | |
3e4093b6 RS |
3722 | |
3723 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) | |
3724 | val = boolean_increment (code, arg); | |
3725 | else | |
53fb4de3 | 3726 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
3e4093b6 | 3727 | TREE_SIDE_EFFECTS (val) = 1; |
3e4093b6 | 3728 | if (TREE_CODE (val) != code) |
6de9cd9a | 3729 | TREE_NO_WARNING (val) = 1; |
c9f9eb5d AH |
3730 | ret = val; |
3731 | goto return_build_unary_op; | |
3e4093b6 RS |
3732 | } |
3733 | ||
3734 | case ADDR_EXPR: | |
3735 | /* Note that this operation never does default_conversion. */ | |
3736 | ||
2b4b7036 JM |
3737 | /* The operand of unary '&' must be an lvalue (which excludes |
3738 | expressions of type void), or, in C99, the result of a [] or | |
3739 | unary '*' operator. */ | |
3740 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
3741 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
3742 | && (TREE_CODE (arg) != INDIRECT_REF | |
3743 | || !flag_isoc99)) | |
3744 | pedwarn (location, 0, "taking address of expression of type %<void%>"); | |
3745 | ||
3e4093b6 RS |
3746 | /* Let &* cancel out to simplify resulting code. */ |
3747 | if (TREE_CODE (arg) == INDIRECT_REF) | |
400fbf9f | 3748 | { |
3e4093b6 RS |
3749 | /* Don't let this be an lvalue. */ |
3750 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
db3927fb | 3751 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); |
c9f9eb5d AH |
3752 | ret = TREE_OPERAND (arg, 0); |
3753 | goto return_build_unary_op; | |
400fbf9f | 3754 | } |
1eb8759b | 3755 | |
7c672dfc | 3756 | /* For &x[y], return x+y */ |
3e4093b6 | 3757 | if (TREE_CODE (arg) == ARRAY_REF) |
1eb8759b | 3758 | { |
f2a71bbc JM |
3759 | tree op0 = TREE_OPERAND (arg, 0); |
3760 | if (!c_mark_addressable (op0)) | |
3e4093b6 | 3761 | return error_mark_node; |
1eb8759b | 3762 | } |
1eb8759b | 3763 | |
3e4093b6 RS |
3764 | /* Anything not already handled and not a true memory reference |
3765 | or a non-lvalue array is an error. */ | |
3766 | else if (typecode != FUNCTION_TYPE && !flag | |
7bd11157 | 3767 | && !lvalue_or_else (location, arg, lv_addressof)) |
3e4093b6 | 3768 | return error_mark_node; |
b6a10c9f | 3769 | |
928c19bb JM |
3770 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify |
3771 | folding later. */ | |
3772 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
3773 | { | |
3774 | tree inner = build_unary_op (location, code, | |
3775 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3776 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3777 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3778 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3779 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
3780 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
3781 | goto return_build_unary_op; | |
3782 | } | |
3783 | ||
3e4093b6 RS |
3784 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
3785 | argtype = TREE_TYPE (arg); | |
400fbf9f | 3786 | |
3e4093b6 | 3787 | /* If the lvalue is const or volatile, merge that into the type |
e73a83fc | 3788 | to which the address will point. This is only needed |
f2c1da78 | 3789 | for function types. */ |
6615c446 | 3790 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) |
e73a83fc RB |
3791 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) |
3792 | && TREE_CODE (argtype) == FUNCTION_TYPE) | |
f2c1da78 JM |
3793 | { |
3794 | int orig_quals = TYPE_QUALS (strip_array_types (argtype)); | |
3795 | int quals = orig_quals; | |
3796 | ||
3797 | if (TREE_READONLY (arg)) | |
3798 | quals |= TYPE_QUAL_CONST; | |
3799 | if (TREE_THIS_VOLATILE (arg)) | |
3800 | quals |= TYPE_QUAL_VOLATILE; | |
3801 | ||
f2c1da78 JM |
3802 | argtype = c_build_qualified_type (argtype, quals); |
3803 | } | |
400fbf9f | 3804 | |
3e4093b6 RS |
3805 | if (!c_mark_addressable (arg)) |
3806 | return error_mark_node; | |
400fbf9f | 3807 | |
abb54d14 JM |
3808 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF |
3809 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
400fbf9f | 3810 | |
5cc200fc | 3811 | argtype = build_pointer_type (argtype); |
5e55f99d RH |
3812 | |
3813 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
3814 | when we have proper support for integer constant expressions. */ | |
3815 | val = get_base_address (arg); | |
3816 | if (val && TREE_CODE (val) == INDIRECT_REF | |
3aa2ddb8 JJ |
3817 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
3818 | { | |
cf9e9959 | 3819 | ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg)); |
c9f9eb5d | 3820 | goto return_build_unary_op; |
3aa2ddb8 | 3821 | } |
5e55f99d | 3822 | |
5cc200fc | 3823 | val = build1 (ADDR_EXPR, argtype, arg); |
400fbf9f | 3824 | |
c9f9eb5d AH |
3825 | ret = val; |
3826 | goto return_build_unary_op; | |
400fbf9f | 3827 | |
3e4093b6 | 3828 | default: |
1344f9a3 | 3829 | gcc_unreachable (); |
3e4093b6 | 3830 | } |
400fbf9f | 3831 | |
3e4093b6 RS |
3832 | if (argtype == 0) |
3833 | argtype = TREE_TYPE (arg); | |
928c19bb JM |
3834 | if (TREE_CODE (arg) == INTEGER_CST) |
3835 | ret = (require_constant_value | |
db3927fb AH |
3836 | ? fold_build1_initializer_loc (location, code, argtype, arg) |
3837 | : fold_build1_loc (location, code, argtype, arg)); | |
928c19bb JM |
3838 | else |
3839 | ret = build1 (code, argtype, arg); | |
c9f9eb5d AH |
3840 | return_build_unary_op: |
3841 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
3842 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) |
3843 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
3844 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
3845 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
3846 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
3847 | if (eptype) |
3848 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
c9f9eb5d AH |
3849 | protected_set_expr_location (ret, location); |
3850 | return ret; | |
3e4093b6 | 3851 | } |
400fbf9f | 3852 | |
3e4093b6 RS |
3853 | /* Return nonzero if REF is an lvalue valid for this language. |
3854 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
5baeaac0 | 3855 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
400fbf9f | 3856 | |
1e4ae551 | 3857 | bool |
58f9752a | 3858 | lvalue_p (const_tree ref) |
3e4093b6 | 3859 | { |
58f9752a | 3860 | const enum tree_code code = TREE_CODE (ref); |
400fbf9f | 3861 | |
3e4093b6 RS |
3862 | switch (code) |
3863 | { | |
3864 | case REALPART_EXPR: | |
3865 | case IMAGPART_EXPR: | |
3866 | case COMPONENT_REF: | |
3867 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
400fbf9f | 3868 | |
928c19bb JM |
3869 | case C_MAYBE_CONST_EXPR: |
3870 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
3871 | ||
3e4093b6 RS |
3872 | case COMPOUND_LITERAL_EXPR: |
3873 | case STRING_CST: | |
3874 | return 1; | |
400fbf9f | 3875 | |
3e4093b6 RS |
3876 | case INDIRECT_REF: |
3877 | case ARRAY_REF: | |
3878 | case VAR_DECL: | |
3879 | case PARM_DECL: | |
3880 | case RESULT_DECL: | |
3881 | case ERROR_MARK: | |
3882 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
3883 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
665f2503 | 3884 | |
3e4093b6 | 3885 | case BIND_EXPR: |
3e4093b6 | 3886 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
665f2503 | 3887 | |
3e4093b6 RS |
3888 | default: |
3889 | return 0; | |
3890 | } | |
3891 | } | |
400fbf9f | 3892 | \f |
f37acdf9 JM |
3893 | /* Give a warning for storing in something that is read-only in GCC |
3894 | terms but not const in ISO C terms. */ | |
3895 | ||
3896 | static void | |
3897 | readonly_warning (tree arg, enum lvalue_use use) | |
3898 | { | |
3899 | switch (use) | |
3900 | { | |
3901 | case lv_assign: | |
3902 | warning (0, "assignment of read-only location %qE", arg); | |
3903 | break; | |
3904 | case lv_increment: | |
3905 | warning (0, "increment of read-only location %qE", arg); | |
3906 | break; | |
3907 | case lv_decrement: | |
3908 | warning (0, "decrement of read-only location %qE", arg); | |
3909 | break; | |
3910 | default: | |
3911 | gcc_unreachable (); | |
3912 | } | |
3913 | return; | |
3914 | } | |
3915 | ||
37dc0d8d JM |
3916 | |
3917 | /* Return nonzero if REF is an lvalue valid for this language; | |
3918 | otherwise, print an error message and return zero. USE says | |
7bd11157 TT |
3919 | how the lvalue is being used and so selects the error message. |
3920 | LOCATION is the location at which any error should be reported. */ | |
37dc0d8d JM |
3921 | |
3922 | static int | |
7bd11157 | 3923 | lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) |
37dc0d8d JM |
3924 | { |
3925 | int win = lvalue_p (ref); | |
3926 | ||
3927 | if (!win) | |
7bd11157 | 3928 | lvalue_error (loc, use); |
37dc0d8d JM |
3929 | |
3930 | return win; | |
3931 | } | |
3e4093b6 RS |
3932 | \f |
3933 | /* Mark EXP saying that we need to be able to take the | |
3934 | address of it; it should not be allocated in a register. | |
3935 | Returns true if successful. */ | |
54c93c30 | 3936 | |
3e4093b6 RS |
3937 | bool |
3938 | c_mark_addressable (tree exp) | |
400fbf9f | 3939 | { |
3e4093b6 | 3940 | tree x = exp; |
95602da1 | 3941 | |
3e4093b6 RS |
3942 | while (1) |
3943 | switch (TREE_CODE (x)) | |
3944 | { | |
3945 | case COMPONENT_REF: | |
3946 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
3947 | { | |
0039fa55 AN |
3948 | error |
3949 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
3e4093b6 RS |
3950 | return false; |
3951 | } | |
95602da1 | 3952 | |
3e4093b6 | 3953 | /* ... fall through ... */ |
95602da1 | 3954 | |
3e4093b6 RS |
3955 | case ADDR_EXPR: |
3956 | case ARRAY_REF: | |
3957 | case REALPART_EXPR: | |
3958 | case IMAGPART_EXPR: | |
3959 | x = TREE_OPERAND (x, 0); | |
3960 | break; | |
95602da1 | 3961 | |
3e4093b6 RS |
3962 | case COMPOUND_LITERAL_EXPR: |
3963 | case CONSTRUCTOR: | |
3964 | TREE_ADDRESSABLE (x) = 1; | |
3965 | return true; | |
95602da1 | 3966 | |
3e4093b6 RS |
3967 | case VAR_DECL: |
3968 | case CONST_DECL: | |
3969 | case PARM_DECL: | |
3970 | case RESULT_DECL: | |
5baeaac0 | 3971 | if (C_DECL_REGISTER (x) |
3e4093b6 RS |
3972 | && DECL_NONLOCAL (x)) |
3973 | { | |
e697b20f | 3974 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
3e4093b6 | 3975 | { |
0039fa55 AN |
3976 | error |
3977 | ("global register variable %qD used in nested function", x); | |
3e4093b6 RS |
3978 | return false; |
3979 | } | |
509c9d60 | 3980 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); |
3e4093b6 | 3981 | } |
5baeaac0 | 3982 | else if (C_DECL_REGISTER (x)) |
3e4093b6 | 3983 | { |
e697b20f | 3984 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
0039fa55 AN |
3985 | error ("address of global register variable %qD requested", x); |
3986 | else | |
3987 | error ("address of register variable %qD requested", x); | |
3988 | return false; | |
3e4093b6 | 3989 | } |
400fbf9f | 3990 | |
3e4093b6 RS |
3991 | /* drops in */ |
3992 | case FUNCTION_DECL: | |
3993 | TREE_ADDRESSABLE (x) = 1; | |
3994 | /* drops out */ | |
3995 | default: | |
3996 | return true; | |
3997 | } | |
3998 | } | |
3999 | \f | |
2d2e923f MLI |
4000 | /* Convert EXPR to TYPE, warning about conversion problems with |
4001 | constants. SEMANTIC_TYPE is the type this conversion would use | |
4002 | without excess precision. If SEMANTIC_TYPE is NULL, this function | |
4003 | is equivalent to convert_and_check. This function is a wrapper that | |
4004 | handles conversions that may be different than | |
4005 | the usual ones because of excess precision. */ | |
4006 | ||
4007 | static tree | |
4008 | ep_convert_and_check (tree type, tree expr, tree semantic_type) | |
4009 | { | |
4010 | if (TREE_TYPE (expr) == type) | |
4011 | return expr; | |
4012 | ||
4013 | if (!semantic_type) | |
4014 | return convert_and_check (type, expr); | |
4015 | ||
4016 | if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
4017 | && TREE_TYPE (expr) != semantic_type) | |
4018 | { | |
4019 | /* For integers, we need to check the real conversion, not | |
4020 | the conversion to the excess precision type. */ | |
4021 | expr = convert_and_check (semantic_type, expr); | |
4022 | } | |
4023 | /* Result type is the excess precision type, which should be | |
4024 | large enough, so do not check. */ | |
4025 | return convert (type, expr); | |
4026 | } | |
4027 | ||
928c19bb JM |
4028 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If |
4029 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
4030 | if folded to an integer constant then the unselected half may | |
4031 | contain arbitrary operations not normally permitted in constant | |
c2255bc4 | 4032 | expressions. Set the location of the expression to LOC. */ |
400fbf9f JW |
4033 | |
4034 | tree | |
744aa42f | 4035 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, |
d130ae11 ILT |
4036 | tree op1, tree op1_original_type, tree op2, |
4037 | tree op2_original_type) | |
400fbf9f | 4038 | { |
3e4093b6 RS |
4039 | tree type1; |
4040 | tree type2; | |
4041 | enum tree_code code1; | |
4042 | enum tree_code code2; | |
4043 | tree result_type = NULL; | |
2d2e923f | 4044 | tree semantic_result_type = NULL; |
3e4093b6 | 4045 | tree orig_op1 = op1, orig_op2 = op2; |
928c19bb | 4046 | bool int_const, op1_int_operands, op2_int_operands, int_operands; |
4d84fe7c | 4047 | bool ifexp_int_operands; |
928c19bb | 4048 | tree ret; |
400fbf9f | 4049 | |
4d84fe7c JM |
4050 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); |
4051 | if (op1_int_operands) | |
4052 | op1 = remove_c_maybe_const_expr (op1); | |
4053 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
4054 | if (op2_int_operands) | |
4055 | op2 = remove_c_maybe_const_expr (op2); | |
4056 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
4057 | if (ifexp_int_operands) | |
4058 | ifexp = remove_c_maybe_const_expr (ifexp); | |
4059 | ||
3e4093b6 RS |
4060 | /* Promote both alternatives. */ |
4061 | ||
4062 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
4063 | op1 = default_conversion (op1); | |
4064 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
4065 | op2 = default_conversion (op2); | |
4066 | ||
4067 | if (TREE_CODE (ifexp) == ERROR_MARK | |
4068 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
4069 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
400fbf9f | 4070 | return error_mark_node; |
400fbf9f | 4071 | |
3e4093b6 RS |
4072 | type1 = TREE_TYPE (op1); |
4073 | code1 = TREE_CODE (type1); | |
4074 | type2 = TREE_TYPE (op2); | |
4075 | code2 = TREE_CODE (type2); | |
4076 | ||
b1adf557 JM |
4077 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
4078 | In C99 they will be pointers by now. */ | |
4079 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
4080 | { | |
744aa42f | 4081 | error_at (colon_loc, "non-lvalue array in conditional expression"); |
b1adf557 JM |
4082 | return error_mark_node; |
4083 | } | |
4084 | ||
8ce94e44 JM |
4085 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR |
4086 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4087 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4088 | || code1 == COMPLEX_TYPE) | |
4089 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4090 | || code2 == COMPLEX_TYPE)) | |
4091 | { | |
2d2e923f | 4092 | semantic_result_type = c_common_type (type1, type2); |
8ce94e44 JM |
4093 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) |
4094 | { | |
4095 | op1 = TREE_OPERAND (op1, 0); | |
4096 | type1 = TREE_TYPE (op1); | |
4097 | gcc_assert (TREE_CODE (type1) == code1); | |
4098 | } | |
4099 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4100 | { | |
4101 | op2 = TREE_OPERAND (op2, 0); | |
4102 | type2 = TREE_TYPE (op2); | |
4103 | gcc_assert (TREE_CODE (type2) == code2); | |
4104 | } | |
4105 | } | |
4106 | ||
d130ae11 ILT |
4107 | if (warn_cxx_compat) |
4108 | { | |
4109 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
4110 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
4111 | ||
4112 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
4113 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
4114 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
4115 | warning_at (colon_loc, OPT_Wc___compat, | |
4116 | ("different enum types in conditional is " | |
4117 | "invalid in C++: %qT vs %qT"), | |
4118 | t1, t2); | |
4119 | } | |
4120 | ||
3e4093b6 RS |
4121 | /* Quickly detect the usual case where op1 and op2 have the same type |
4122 | after promotion. */ | |
4123 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
400fbf9f | 4124 | { |
3e4093b6 RS |
4125 | if (type1 == type2) |
4126 | result_type = type1; | |
4127 | else | |
4128 | result_type = TYPE_MAIN_VARIANT (type1); | |
4129 | } | |
4130 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
c22cacf3 MS |
4131 | || code1 == COMPLEX_TYPE) |
4132 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4133 | || code2 == COMPLEX_TYPE)) | |
3e4093b6 | 4134 | { |
ccf7f880 | 4135 | result_type = c_common_type (type1, type2); |
0a0b3574 MM |
4136 | do_warn_double_promotion (result_type, type1, type2, |
4137 | "implicit conversion from %qT to %qT to " | |
4138 | "match other result of conditional", | |
4139 | colon_loc); | |
400fbf9f | 4140 | |
3e4093b6 RS |
4141 | /* If -Wsign-compare, warn here if type1 and type2 have |
4142 | different signedness. We'll promote the signed to unsigned | |
4143 | and later code won't know it used to be different. | |
4144 | Do this check on the original types, so that explicit casts | |
4145 | will be considered, but default promotions won't. */ | |
7d882b83 | 4146 | if (c_inhibit_evaluation_warnings == 0) |
ab87f8c8 | 4147 | { |
8df83eae RK |
4148 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
4149 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
400fbf9f | 4150 | |
3e4093b6 RS |
4151 | if (unsigned_op1 ^ unsigned_op2) |
4152 | { | |
6ac01510 ILT |
4153 | bool ovf; |
4154 | ||
3e4093b6 RS |
4155 | /* Do not warn if the result type is signed, since the |
4156 | signed type will only be chosen if it can represent | |
4157 | all the values of the unsigned type. */ | |
3f75a254 | 4158 | if (!TYPE_UNSIGNED (result_type)) |
3e4093b6 | 4159 | /* OK */; |
3e4093b6 | 4160 | else |
928c19bb JM |
4161 | { |
4162 | bool op1_maybe_const = true; | |
4163 | bool op2_maybe_const = true; | |
4164 | ||
4165 | /* Do not warn if the signed quantity is an | |
4166 | unsuffixed integer literal (or some static | |
4167 | constant expression involving such literals) and | |
4168 | it is non-negative. This warning requires the | |
4169 | operands to be folded for best results, so do | |
4170 | that folding in this case even without | |
4171 | warn_sign_compare to avoid warning options | |
4172 | possibly affecting code generation. */ | |
f5178456 RS |
4173 | c_inhibit_evaluation_warnings |
4174 | += (ifexp == truthvalue_false_node); | |
928c19bb JM |
4175 | op1 = c_fully_fold (op1, require_constant_value, |
4176 | &op1_maybe_const); | |
f5178456 RS |
4177 | c_inhibit_evaluation_warnings |
4178 | -= (ifexp == truthvalue_false_node); | |
4179 | ||
4180 | c_inhibit_evaluation_warnings | |
4181 | += (ifexp == truthvalue_true_node); | |
928c19bb JM |
4182 | op2 = c_fully_fold (op2, require_constant_value, |
4183 | &op2_maybe_const); | |
f5178456 RS |
4184 | c_inhibit_evaluation_warnings |
4185 | -= (ifexp == truthvalue_true_node); | |
928c19bb JM |
4186 | |
4187 | if (warn_sign_compare) | |
4188 | { | |
4189 | if ((unsigned_op2 | |
4190 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4191 | || (unsigned_op1 | |
4192 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4193 | /* OK */; | |
4194 | else | |
744aa42f ILT |
4195 | warning_at (colon_loc, OPT_Wsign_compare, |
4196 | ("signed and unsigned type in " | |
4197 | "conditional expression")); | |
928c19bb JM |
4198 | } |
4199 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
e5a94231 | 4200 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 4201 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) |
e5a94231 | 4202 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); |
928c19bb | 4203 | } |
3e4093b6 RS |
4204 | } |
4205 | } | |
4206 | } | |
4207 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4208 | { | |
fcf73884 | 4209 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) |
c1771a20 | 4210 | pedwarn (colon_loc, OPT_Wpedantic, |
fcf73884 | 4211 | "ISO C forbids conditional expr with only one void side"); |
3e4093b6 RS |
4212 | result_type = void_type_node; |
4213 | } | |
4214 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4215 | { | |
36c5e70a BE |
4216 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); |
4217 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4218 | addr_space_t as_common; | |
4219 | ||
744aa42f | 4220 | if (comp_target_types (colon_loc, type1, type2)) |
10bc1b1b | 4221 | result_type = common_pointer_type (type1, type2); |
6aa3c60d | 4222 | else if (null_pointer_constant_p (orig_op1)) |
36c5e70a | 4223 | result_type = type2; |
6aa3c60d | 4224 | else if (null_pointer_constant_p (orig_op2)) |
36c5e70a BE |
4225 | result_type = type1; |
4226 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4227 | { | |
4228 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4229 | "used in conditional expression"); | |
4230 | return error_mark_node; | |
4231 | } | |
3e4093b6 | 4232 | else if (VOID_TYPE_P (TREE_TYPE (type1))) |
34a80643 | 4233 | { |
fcf73884 | 4234 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
c1771a20 | 4235 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4236 | "ISO C forbids conditional expr between " |
bda67431 | 4237 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4238 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), |
4239 | TREE_TYPE (type2))); | |
34a80643 | 4240 | } |
3e4093b6 | 4241 | else if (VOID_TYPE_P (TREE_TYPE (type2))) |
1c2a9b35 | 4242 | { |
fcf73884 | 4243 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
c1771a20 | 4244 | pedwarn (colon_loc, OPT_Wpedantic, |
509c9d60 | 4245 | "ISO C forbids conditional expr between " |
bda67431 | 4246 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4247 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), |
4248 | TREE_TYPE (type1))); | |
1c2a9b35 | 4249 | } |
b581b85b NP |
4250 | /* Objective-C pointer comparisons are a bit more lenient. */ |
4251 | else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) | |
4252 | result_type = objc_common_type (type1, type2); | |
34a80643 | 4253 | else |
ab87f8c8 | 4254 | { |
36c5e70a BE |
4255 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
4256 | ||
b581b85b NP |
4257 | pedwarn (colon_loc, 0, |
4258 | "pointer type mismatch in conditional expression"); | |
36c5e70a BE |
4259 | result_type = build_pointer_type |
4260 | (build_qualified_type (void_type_node, qual)); | |
ab87f8c8 | 4261 | } |
3e4093b6 RS |
4262 | } |
4263 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4264 | { | |
6aa3c60d | 4265 | if (!null_pointer_constant_p (orig_op2)) |
744aa42f | 4266 | pedwarn (colon_loc, 0, |
509c9d60 | 4267 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4268 | else |
ab87f8c8 | 4269 | { |
3e4093b6 | 4270 | op2 = null_pointer_node; |
ab87f8c8 | 4271 | } |
3e4093b6 RS |
4272 | result_type = type1; |
4273 | } | |
4274 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4275 | { | |
6aa3c60d | 4276 | if (!null_pointer_constant_p (orig_op1)) |
744aa42f | 4277 | pedwarn (colon_loc, 0, |
509c9d60 | 4278 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4279 | else |
ab87f8c8 | 4280 | { |
3e4093b6 | 4281 | op1 = null_pointer_node; |
ab87f8c8 | 4282 | } |
3e4093b6 RS |
4283 | result_type = type2; |
4284 | } | |
1c2a9b35 | 4285 | |
3e4093b6 RS |
4286 | if (!result_type) |
4287 | { | |
4288 | if (flag_cond_mismatch) | |
4289 | result_type = void_type_node; | |
4290 | else | |
400fbf9f | 4291 | { |
c2255bc4 | 4292 | error_at (colon_loc, "type mismatch in conditional expression"); |
ab87f8c8 | 4293 | return error_mark_node; |
400fbf9f | 4294 | } |
3e4093b6 | 4295 | } |
400fbf9f | 4296 | |
3e4093b6 RS |
4297 | /* Merge const and volatile flags of the incoming types. */ |
4298 | result_type | |
4299 | = build_type_variant (result_type, | |
afbd0665 AS |
4300 | TYPE_READONLY (type1) || TYPE_READONLY (type2), |
4301 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
b6a10c9f | 4302 | |
2d2e923f MLI |
4303 | op1 = ep_convert_and_check (result_type, op1, semantic_result_type); |
4304 | op2 = ep_convert_and_check (result_type, op2, semantic_result_type); | |
b6a10c9f | 4305 | |
928c19bb JM |
4306 | if (ifexp_bcp && ifexp == truthvalue_true_node) |
4307 | { | |
4308 | op2_int_operands = true; | |
4309 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4310 | } | |
4311 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4312 | { | |
4313 | op1_int_operands = true; | |
4314 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4315 | } | |
4d84fe7c | 4316 | int_const = int_operands = (ifexp_int_operands |
928c19bb JM |
4317 | && op1_int_operands |
4318 | && op2_int_operands); | |
4319 | if (int_operands) | |
4320 | { | |
4321 | int_const = ((ifexp == truthvalue_true_node | |
4322 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4323 | && !TREE_OVERFLOW (orig_op1)) | |
4324 | || (ifexp == truthvalue_false_node | |
4325 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4326 | && !TREE_OVERFLOW (orig_op2))); | |
4327 | } | |
4328 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) | |
db3927fb | 4329 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); |
928c19bb JM |
4330 | else |
4331 | { | |
01c7ccbb JM |
4332 | if (int_operands) |
4333 | { | |
4334 | op1 = remove_c_maybe_const_expr (op1); | |
4335 | op2 = remove_c_maybe_const_expr (op2); | |
4336 | } | |
928c19bb JM |
4337 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); |
4338 | if (int_operands) | |
4339 | ret = note_integer_operands (ret); | |
4340 | } | |
2d2e923f MLI |
4341 | if (semantic_result_type) |
4342 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
928c19bb | 4343 | |
c2255bc4 | 4344 | protected_set_expr_location (ret, colon_loc); |
928c19bb | 4345 | return ret; |
3e4093b6 RS |
4346 | } |
4347 | \f | |
487a92fe | 4348 | /* Return a compound expression that performs two expressions and |
c2255bc4 AH |
4349 | returns the value of the second of them. |
4350 | ||
4351 | LOC is the location of the COMPOUND_EXPR. */ | |
400fbf9f | 4352 | |
3e4093b6 | 4353 | tree |
c2255bc4 | 4354 | build_compound_expr (location_t loc, tree expr1, tree expr2) |
3e4093b6 | 4355 | { |
4d84fe7c | 4356 | bool expr1_int_operands, expr2_int_operands; |
8ce94e44 | 4357 | tree eptype = NULL_TREE; |
928c19bb JM |
4358 | tree ret; |
4359 | ||
4d84fe7c JM |
4360 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); |
4361 | if (expr1_int_operands) | |
4362 | expr1 = remove_c_maybe_const_expr (expr1); | |
4363 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4364 | if (expr2_int_operands) | |
4365 | expr2 = remove_c_maybe_const_expr (expr2); | |
4366 | ||
8ce94e44 JM |
4367 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) |
4368 | expr1 = TREE_OPERAND (expr1, 0); | |
4369 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4370 | { | |
4371 | eptype = TREE_TYPE (expr2); | |
4372 | expr2 = TREE_OPERAND (expr2, 0); | |
4373 | } | |
4374 | ||
3f75a254 | 4375 | if (!TREE_SIDE_EFFECTS (expr1)) |
3e4093b6 RS |
4376 | { |
4377 | /* The left-hand operand of a comma expression is like an expression | |
c5409249 | 4378 | statement: with -Wunused, we should warn if it doesn't have |
3e4093b6 | 4379 | any side-effects, unless it was explicitly cast to (void). */ |
e14a6540 | 4380 | if (warn_unused_value) |
47aecf47 | 4381 | { |
e14a6540 | 4382 | if (VOID_TYPE_P (TREE_TYPE (expr1)) |
1043771b | 4383 | && CONVERT_EXPR_P (expr1)) |
47aecf47 | 4384 | ; /* (void) a, b */ |
e14a6540 JM |
4385 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) |
4386 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
1043771b | 4387 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) |
47aecf47 JM |
4388 | ; /* (void) a, (void) b, c */ |
4389 | else | |
b8698a0f | 4390 | warning_at (loc, OPT_Wunused_value, |
c2255bc4 | 4391 | "left-hand operand of comma expression has no effect"); |
47aecf47 | 4392 | } |
3e4093b6 | 4393 | } |
400fbf9f | 4394 | |
3e4093b6 RS |
4395 | /* With -Wunused, we should also warn if the left-hand operand does have |
4396 | side-effects, but computes a value which is not used. For example, in | |
4397 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4398 | so we should issue a warning. */ | |
4399 | else if (warn_unused_value) | |
c2255bc4 | 4400 | warn_if_unused_value (expr1, loc); |
400fbf9f | 4401 | |
e63d6886 AP |
4402 | if (expr2 == error_mark_node) |
4403 | return error_mark_node; | |
4404 | ||
928c19bb JM |
4405 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
4406 | ||
4407 | if (flag_isoc99 | |
4d84fe7c JM |
4408 | && expr1_int_operands |
4409 | && expr2_int_operands) | |
928c19bb JM |
4410 | ret = note_integer_operands (ret); |
4411 | ||
8ce94e44 JM |
4412 | if (eptype) |
4413 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4414 | ||
c2255bc4 | 4415 | protected_set_expr_location (ret, loc); |
928c19bb | 4416 | return ret; |
3e4093b6 | 4417 | } |
400fbf9f | 4418 | |
67165eb3 ILT |
4419 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to |
4420 | which we are casting. OTYPE is the type of the expression being | |
2ee3cb35 MLI |
4421 | cast. Both TYPE and OTYPE are pointer types. LOC is the location |
4422 | of the cast. -Wcast-qual appeared on the command line. Named | |
4423 | address space qualifiers are not handled here, because they result | |
4424 | in different warnings. */ | |
67165eb3 ILT |
4425 | |
4426 | static void | |
2ee3cb35 | 4427 | handle_warn_cast_qual (location_t loc, tree type, tree otype) |
67165eb3 ILT |
4428 | { |
4429 | tree in_type = type; | |
4430 | tree in_otype = otype; | |
4431 | int added = 0; | |
4432 | int discarded = 0; | |
4433 | bool is_const; | |
4434 | ||
4435 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4436 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4437 | nodes is uninteresting and we stop as soon as we hit a | |
4438 | non-POINTER_TYPE node on either type. */ | |
4439 | do | |
4440 | { | |
4441 | in_otype = TREE_TYPE (in_otype); | |
4442 | in_type = TREE_TYPE (in_type); | |
4443 | ||
4444 | /* GNU C allows cv-qualified function types. 'const' means the | |
4445 | function is very pure, 'volatile' means it can't return. We | |
4446 | need to warn when such qualifiers are added, not when they're | |
4447 | taken away. */ | |
4448 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4449 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
36c5e70a BE |
4450 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) |
4451 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
67165eb3 | 4452 | else |
36c5e70a BE |
4453 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) |
4454 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
67165eb3 ILT |
4455 | } |
4456 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4457 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4458 | ||
4459 | if (added) | |
2ee3cb35 MLI |
4460 | warning_at (loc, OPT_Wcast_qual, |
4461 | "cast adds %q#v qualifier to function type", added); | |
67165eb3 ILT |
4462 | |
4463 | if (discarded) | |
4464 | /* There are qualifiers present in IN_OTYPE that are not present | |
4465 | in IN_TYPE. */ | |
2ee3cb35 MLI |
4466 | warning_at (loc, OPT_Wcast_qual, |
4467 | "cast discards %q#v qualifier from pointer target type", | |
4468 | discarded); | |
67165eb3 ILT |
4469 | |
4470 | if (added || discarded) | |
4471 | return; | |
4472 | ||
4473 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4474 | const value to be changed with no additional warning. We only | |
4475 | issue this warning if T is the same on both sides, and we only | |
4476 | issue the warning if there are the same number of pointers on | |
4477 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4478 | cast is unsafe when a qualifier is added at one level and const | |
4479 | is not present at all outer levels. | |
4480 | ||
4481 | To issue this warning, we check at each level whether the cast | |
4482 | adds new qualifiers not already seen. We don't need to special | |
4483 | case function types, as they won't have the same | |
4484 | TYPE_MAIN_VARIANT. */ | |
4485 | ||
4486 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4487 | return; | |
4488 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4489 | return; | |
4490 | ||
4491 | in_type = type; | |
4492 | in_otype = otype; | |
4493 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4494 | do | |
4495 | { | |
4496 | in_type = TREE_TYPE (in_type); | |
4497 | in_otype = TREE_TYPE (in_otype); | |
4498 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4499 | && !is_const) | |
4500 | { | |
2ee3cb35 MLI |
4501 | warning_at (loc, OPT_Wcast_qual, |
4502 | "to be safe all intermediate pointers in cast from " | |
4503 | "%qT to %qT must be %<const%> qualified", | |
4504 | otype, type); | |
67165eb3 ILT |
4505 | break; |
4506 | } | |
4507 | if (is_const) | |
4508 | is_const = TYPE_READONLY (in_type); | |
4509 | } | |
4510 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4511 | } | |
4512 | ||
b8698a0f | 4513 | /* Build an expression representing a cast to type TYPE of expression EXPR. |
c2255bc4 | 4514 | LOC is the location of the cast-- typically the open paren of the cast. */ |
400fbf9f | 4515 | |
3e4093b6 | 4516 | tree |
c2255bc4 | 4517 | build_c_cast (location_t loc, tree type, tree expr) |
3e4093b6 | 4518 | { |
8ce94e44 JM |
4519 | tree value; |
4520 | ||
4521 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
4522 | expr = TREE_OPERAND (expr, 0); | |
4523 | ||
4524 | value = expr; | |
400fbf9f | 4525 | |
3e4093b6 RS |
4526 | if (type == error_mark_node || expr == error_mark_node) |
4527 | return error_mark_node; | |
400fbf9f | 4528 | |
3e4093b6 RS |
4529 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
4530 | only in <protocol> qualifications. But when constructing cast expressions, | |
4531 | the protocols do matter and must be kept around. */ | |
700686fa ZL |
4532 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) |
4533 | return build1 (NOP_EXPR, type, expr); | |
4534 | ||
4535 | type = TYPE_MAIN_VARIANT (type); | |
400fbf9f | 4536 | |
3e4093b6 RS |
4537 | if (TREE_CODE (type) == ARRAY_TYPE) |
4538 | { | |
c2255bc4 | 4539 | error_at (loc, "cast specifies array type"); |
3e4093b6 RS |
4540 | return error_mark_node; |
4541 | } | |
400fbf9f | 4542 | |
3e4093b6 RS |
4543 | if (TREE_CODE (type) == FUNCTION_TYPE) |
4544 | { | |
c2255bc4 | 4545 | error_at (loc, "cast specifies function type"); |
3e4093b6 RS |
4546 | return error_mark_node; |
4547 | } | |
400fbf9f | 4548 | |
808d6eaa JM |
4549 | if (!VOID_TYPE_P (type)) |
4550 | { | |
4551 | value = require_complete_type (value); | |
4552 | if (value == error_mark_node) | |
4553 | return error_mark_node; | |
4554 | } | |
4555 | ||
3e4093b6 RS |
4556 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
4557 | { | |
fcf73884 MLI |
4558 | if (TREE_CODE (type) == RECORD_TYPE |
4559 | || TREE_CODE (type) == UNION_TYPE) | |
c1771a20 | 4560 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 4561 | "ISO C forbids casting nonscalar to the same type"); |
3e4093b6 RS |
4562 | } |
4563 | else if (TREE_CODE (type) == UNION_TYPE) | |
4564 | { | |
4565 | tree field; | |
400fbf9f | 4566 | |
910ad8de | 4567 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
39ffbac9 VR |
4568 | if (TREE_TYPE (field) != error_mark_node |
4569 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
4570 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3e4093b6 RS |
4571 | break; |
4572 | ||
4573 | if (field) | |
400fbf9f | 4574 | { |
3e4093b6 | 4575 | tree t; |
e616f54d | 4576 | bool maybe_const = true; |
3e4093b6 | 4577 | |
c1771a20 | 4578 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); |
e616f54d JM |
4579 | t = c_fully_fold (value, false, &maybe_const); |
4580 | t = build_constructor_single (type, field, t); | |
4581 | if (!maybe_const) | |
4582 | t = c_wrap_maybe_const (t, true); | |
4583 | t = digest_init (loc, type, t, | |
bbbbb16a | 4584 | NULL_TREE, false, true, 0); |
3e4093b6 RS |
4585 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
4586 | return t; | |
400fbf9f | 4587 | } |
c2255bc4 | 4588 | error_at (loc, "cast to union type from type not present in union"); |
3e4093b6 RS |
4589 | return error_mark_node; |
4590 | } | |
4591 | else | |
4592 | { | |
4593 | tree otype, ovalue; | |
400fbf9f | 4594 | |
3e4093b6 | 4595 | if (type == void_type_node) |
c2255bc4 AH |
4596 | { |
4597 | tree t = build1 (CONVERT_EXPR, type, value); | |
4598 | SET_EXPR_LOCATION (t, loc); | |
4599 | return t; | |
4600 | } | |
400fbf9f | 4601 | |
3e4093b6 | 4602 | otype = TREE_TYPE (value); |
400fbf9f | 4603 | |
3e4093b6 | 4604 | /* Optionally warn about potentially worrisome casts. */ |
3e4093b6 RS |
4605 | if (warn_cast_qual |
4606 | && TREE_CODE (type) == POINTER_TYPE | |
4607 | && TREE_CODE (otype) == POINTER_TYPE) | |
2ee3cb35 | 4608 | handle_warn_cast_qual (loc, type, otype); |
400fbf9f | 4609 | |
36c5e70a BE |
4610 | /* Warn about conversions between pointers to disjoint |
4611 | address spaces. */ | |
4612 | if (TREE_CODE (type) == POINTER_TYPE | |
4613 | && TREE_CODE (otype) == POINTER_TYPE | |
4614 | && !null_pointer_constant_p (value)) | |
4615 | { | |
4616 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
4617 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
4618 | addr_space_t as_common; | |
4619 | ||
4620 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
4621 | { | |
4622 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
4623 | warning_at (loc, 0, "cast to %s address space pointer " | |
4624 | "from disjoint generic address space pointer", | |
4625 | c_addr_space_name (as_to)); | |
4626 | ||
4627 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
4628 | warning_at (loc, 0, "cast to generic address space pointer " | |
4629 | "from disjoint %s address space pointer", | |
4630 | c_addr_space_name (as_from)); | |
4631 | ||
4632 | else | |
4633 | warning_at (loc, 0, "cast to %s address space pointer " | |
4634 | "from disjoint %s address space pointer", | |
4635 | c_addr_space_name (as_to), | |
4636 | c_addr_space_name (as_from)); | |
4637 | } | |
4638 | } | |
4639 | ||
3e4093b6 | 4640 | /* Warn about possible alignment problems. */ |
3176a0c2 | 4641 | if (STRICT_ALIGNMENT |
3e4093b6 RS |
4642 | && TREE_CODE (type) == POINTER_TYPE |
4643 | && TREE_CODE (otype) == POINTER_TYPE | |
4644 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
4645 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
4646 | /* Don't warn about opaque types, where the actual alignment | |
4647 | restriction is unknown. */ | |
4648 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
4649 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
4650 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
4651 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
c2255bc4 AH |
4652 | warning_at (loc, OPT_Wcast_align, |
4653 | "cast increases required alignment of target type"); | |
e9a25f70 | 4654 | |
3176a0c2 | 4655 | if (TREE_CODE (type) == INTEGER_TYPE |
3e4093b6 | 4656 | && TREE_CODE (otype) == POINTER_TYPE |
8d1c7aef | 4657 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) |
c22cacf3 MS |
4658 | /* Unlike conversion of integers to pointers, where the |
4659 | warning is disabled for converting constants because | |
4660 | of cases such as SIG_*, warn about converting constant | |
4661 | pointers to integers. In some cases it may cause unwanted | |
8d1c7aef | 4662 | sign extension, and a warning is appropriate. */ |
c2255bc4 AH |
4663 | warning_at (loc, OPT_Wpointer_to_int_cast, |
4664 | "cast from pointer to integer of different size"); | |
400fbf9f | 4665 | |
3176a0c2 | 4666 | if (TREE_CODE (value) == CALL_EXPR |
3e4093b6 | 4667 | && TREE_CODE (type) != TREE_CODE (otype)) |
c2255bc4 AH |
4668 | warning_at (loc, OPT_Wbad_function_cast, |
4669 | "cast from function call of type %qT " | |
4670 | "to non-matching type %qT", otype, type); | |
400fbf9f | 4671 | |
3176a0c2 | 4672 | if (TREE_CODE (type) == POINTER_TYPE |
3e4093b6 RS |
4673 | && TREE_CODE (otype) == INTEGER_TYPE |
4674 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
4675 | /* Don't warn about converting any constant. */ | |
4676 | && !TREE_CONSTANT (value)) | |
c2255bc4 AH |
4677 | warning_at (loc, |
4678 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
4679 | "of different size"); | |
400fbf9f | 4680 | |
79bedddc SR |
4681 | if (warn_strict_aliasing <= 2) |
4682 | strict_aliasing_warning (otype, type, expr); | |
400fbf9f | 4683 | |
3897f229 JM |
4684 | /* If pedantic, warn for conversions between function and object |
4685 | pointer types, except for converting a null pointer constant | |
4686 | to function pointer type. */ | |
4687 | if (pedantic | |
4688 | && TREE_CODE (type) == POINTER_TYPE | |
4689 | && TREE_CODE (otype) == POINTER_TYPE | |
4690 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
4691 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
c1771a20 | 4692 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 4693 | "conversion of function pointer to object pointer type"); |
3897f229 JM |
4694 | |
4695 | if (pedantic | |
4696 | && TREE_CODE (type) == POINTER_TYPE | |
4697 | && TREE_CODE (otype) == POINTER_TYPE | |
4698 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
4699 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
6aa3c60d | 4700 | && !null_pointer_constant_p (value)) |
c1771a20 | 4701 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 4702 | "conversion of object pointer to function pointer type"); |
3897f229 | 4703 | |
3e4093b6 | 4704 | ovalue = value; |
3e4093b6 | 4705 | value = convert (type, value); |
400fbf9f | 4706 | |
3e4093b6 | 4707 | /* Ignore any integer overflow caused by the cast. */ |
928c19bb | 4708 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) |
3e4093b6 | 4709 | { |
8bcd6380 | 4710 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) |
6414bad6 | 4711 | { |
8bcd6380 RS |
4712 | if (!TREE_OVERFLOW (value)) |
4713 | { | |
4714 | /* Avoid clobbering a shared constant. */ | |
4715 | value = copy_node (value); | |
4716 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
4717 | } | |
6414bad6 | 4718 | } |
8bcd6380 | 4719 | else if (TREE_OVERFLOW (value)) |
d8e1f97b PB |
4720 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ |
4721 | value = build_int_cst_wide (TREE_TYPE (value), | |
4722 | TREE_INT_CST_LOW (value), | |
4723 | TREE_INT_CST_HIGH (value)); | |
3e4093b6 RS |
4724 | } |
4725 | } | |
400fbf9f | 4726 | |
53cd18ec JM |
4727 | /* Don't let a cast be an lvalue. */ |
4728 | if (value == expr) | |
db3927fb | 4729 | value = non_lvalue_loc (loc, value); |
e9a25f70 | 4730 | |
928c19bb JM |
4731 | /* Don't allow the results of casting to floating-point or complex |
4732 | types be confused with actual constants, or casts involving | |
4733 | integer and pointer types other than direct integer-to-integer | |
4734 | and integer-to-pointer be confused with integer constant | |
4735 | expressions and null pointer constants. */ | |
4736 | if (TREE_CODE (value) == REAL_CST | |
4737 | || TREE_CODE (value) == COMPLEX_CST | |
4738 | || (TREE_CODE (value) == INTEGER_CST | |
4739 | && !((TREE_CODE (expr) == INTEGER_CST | |
4740 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
4741 | || TREE_CODE (expr) == REAL_CST | |
4742 | || TREE_CODE (expr) == COMPLEX_CST))) | |
4743 | value = build1 (NOP_EXPR, type, value); | |
4744 | ||
c2255bc4 AH |
4745 | if (CAN_HAVE_LOCATION_P (value)) |
4746 | SET_EXPR_LOCATION (value, loc); | |
3e4093b6 | 4747 | return value; |
400fbf9f JW |
4748 | } |
4749 | ||
c2255bc4 AH |
4750 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the |
4751 | location of the open paren of the cast, or the position of the cast | |
4752 | expr. */ | |
3e4093b6 | 4753 | tree |
c2255bc4 | 4754 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) |
400fbf9f | 4755 | { |
f8893e47 | 4756 | tree type; |
928c19bb JM |
4757 | tree type_expr = NULL_TREE; |
4758 | bool type_expr_const = true; | |
4759 | tree ret; | |
3e4093b6 | 4760 | int saved_wsp = warn_strict_prototypes; |
c5c76735 | 4761 | |
3e4093b6 RS |
4762 | /* This avoids warnings about unprototyped casts on |
4763 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
4764 | if (TREE_CODE (expr) == INTEGER_CST) | |
4765 | warn_strict_prototypes = 0; | |
928c19bb | 4766 | type = groktypename (type_name, &type_expr, &type_expr_const); |
3e4093b6 | 4767 | warn_strict_prototypes = saved_wsp; |
c5c76735 | 4768 | |
c2255bc4 | 4769 | ret = build_c_cast (loc, type, expr); |
928c19bb JM |
4770 | if (type_expr) |
4771 | { | |
9f33203d JM |
4772 | bool inner_expr_const = true; |
4773 | ret = c_fully_fold (ret, require_constant_value, &inner_expr_const); | |
928c19bb | 4774 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); |
9f33203d JM |
4775 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const |
4776 | && inner_expr_const); | |
c2255bc4 | 4777 | SET_EXPR_LOCATION (ret, loc); |
928c19bb | 4778 | } |
24b97832 ILT |
4779 | |
4780 | if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) | |
4781 | SET_EXPR_LOCATION (ret, loc); | |
4782 | ||
9e5b2115 PB |
4783 | /* C++ does not permits types to be defined in a cast, but it |
4784 | allows references to incomplete types. */ | |
4785 | if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) | |
24b97832 ILT |
4786 | warning_at (loc, OPT_Wc___compat, |
4787 | "defining a type in a cast is invalid in C++"); | |
4788 | ||
928c19bb | 4789 | return ret; |
400fbf9f | 4790 | } |
3e4093b6 RS |
4791 | \f |
4792 | /* Build an assignment expression of lvalue LHS from value RHS. | |
32e8bb8e ILT |
4793 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which |
4794 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
3e4093b6 RS |
4795 | MODIFYCODE is the code for a binary operator that we use |
4796 | to combine the old value of LHS with RHS to get the new value. | |
c9f9eb5d | 4797 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
bbbbb16a ILT |
4798 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, |
4799 | which may differ from TREE_TYPE (RHS) for an enum value. | |
c9f9eb5d | 4800 | |
c2255bc4 AH |
4801 | LOCATION is the location of the MODIFYCODE operator. |
4802 | RHS_LOC is the location of the RHS. */ | |
2f6e4e97 | 4803 | |
3e4093b6 | 4804 | tree |
32e8bb8e | 4805 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, |
b8698a0f | 4806 | enum tree_code modifycode, |
c2255bc4 | 4807 | location_t rhs_loc, tree rhs, tree rhs_origtype) |
400fbf9f | 4808 | { |
3e4093b6 RS |
4809 | tree result; |
4810 | tree newrhs; | |
8ce94e44 | 4811 | tree rhs_semantic_type = NULL_TREE; |
3e4093b6 RS |
4812 | tree lhstype = TREE_TYPE (lhs); |
4813 | tree olhstype = lhstype; | |
928c19bb | 4814 | bool npc; |
e9a25f70 | 4815 | |
3e4093b6 RS |
4816 | /* Types that aren't fully specified cannot be used in assignments. */ |
4817 | lhs = require_complete_type (lhs); | |
e9a25f70 | 4818 | |
3e4093b6 RS |
4819 | /* Avoid duplicate error messages from operands that had errors. */ |
4820 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
4821 | return error_mark_node; | |
400fbf9f | 4822 | |
46a88c12 | 4823 | /* For ObjC properties, defer this check. */ |
7bd11157 | 4824 | if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) |
c0bcacec VR |
4825 | return error_mark_node; |
4826 | ||
8ce94e44 JM |
4827 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
4828 | { | |
4829 | rhs_semantic_type = TREE_TYPE (rhs); | |
4830 | rhs = TREE_OPERAND (rhs, 0); | |
4831 | } | |
4832 | ||
3e4093b6 | 4833 | newrhs = rhs; |
400fbf9f | 4834 | |
928c19bb JM |
4835 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) |
4836 | { | |
4837 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
c2255bc4 | 4838 | lhs_origtype, modifycode, rhs_loc, rhs, |
32e8bb8e | 4839 | rhs_origtype); |
928c19bb JM |
4840 | if (inner == error_mark_node) |
4841 | return error_mark_node; | |
4842 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4843 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
4844 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
4845 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
4846 | protected_set_expr_location (result, location); | |
4847 | return result; | |
4848 | } | |
4849 | ||
3e4093b6 RS |
4850 | /* If a binary op has been requested, combine the old LHS value with the RHS |
4851 | producing the value we should actually store into the LHS. */ | |
4852 | ||
4853 | if (modifycode != NOP_EXPR) | |
400fbf9f | 4854 | { |
928c19bb | 4855 | lhs = c_fully_fold (lhs, false, NULL); |
3e4093b6 | 4856 | lhs = stabilize_reference (lhs); |
c9f9eb5d | 4857 | newrhs = build_binary_op (location, |
ba47d38d | 4858 | modifycode, lhs, rhs, 1); |
bbbbb16a ILT |
4859 | |
4860 | /* The original type of the right hand side is no longer | |
4861 | meaningful. */ | |
4862 | rhs_origtype = NULL_TREE; | |
400fbf9f | 4863 | } |
400fbf9f | 4864 | |
668ea4b1 IS |
4865 | if (c_dialect_objc ()) |
4866 | { | |
46a88c12 NP |
4867 | /* Check if we are modifying an Objective-C property reference; |
4868 | if so, we need to generate setter calls. */ | |
4869 | result = objc_maybe_build_modify_expr (lhs, newrhs); | |
668ea4b1 IS |
4870 | if (result) |
4871 | return result; | |
46a88c12 NP |
4872 | |
4873 | /* Else, do the check that we postponed for Objective-C. */ | |
7bd11157 | 4874 | if (!lvalue_or_else (location, lhs, lv_assign)) |
668ea4b1 IS |
4875 | return error_mark_node; |
4876 | } | |
4877 | ||
9bf24266 | 4878 | /* Give an error for storing in something that is 'const'. */ |
bbbd6700 | 4879 | |
f37acdf9 | 4880 | if (TYPE_READONLY (lhstype) |
3e4093b6 RS |
4881 | || ((TREE_CODE (lhstype) == RECORD_TYPE |
4882 | || TREE_CODE (lhstype) == UNION_TYPE) | |
4883 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
953ff289 DN |
4884 | { |
4885 | readonly_error (lhs, lv_assign); | |
4886 | return error_mark_node; | |
4887 | } | |
f37acdf9 JM |
4888 | else if (TREE_READONLY (lhs)) |
4889 | readonly_warning (lhs, lv_assign); | |
bbbd6700 | 4890 | |
3e4093b6 RS |
4891 | /* If storing into a structure or union member, |
4892 | it has probably been given type `int'. | |
4893 | Compute the type that would go with | |
4894 | the actual amount of storage the member occupies. */ | |
bbbd6700 | 4895 | |
3e4093b6 RS |
4896 | if (TREE_CODE (lhs) == COMPONENT_REF |
4897 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
4898 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
4899 | || TREE_CODE (lhstype) == REAL_TYPE | |
4900 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
4901 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
400fbf9f | 4902 | |
3e4093b6 RS |
4903 | /* If storing in a field that is in actuality a short or narrower than one, |
4904 | we must store in the field in its actual type. */ | |
4905 | ||
4906 | if (lhstype != TREE_TYPE (lhs)) | |
4907 | { | |
4908 | lhs = copy_node (lhs); | |
4909 | TREE_TYPE (lhs) = lhstype; | |
400fbf9f | 4910 | } |
400fbf9f | 4911 | |
32e8bb8e ILT |
4912 | /* Issue -Wc++-compat warnings about an assignment to an enum type |
4913 | when LHS does not have its original type. This happens for, | |
4914 | e.g., an enum bitfield in a struct. */ | |
4915 | if (warn_cxx_compat | |
4916 | && lhs_origtype != NULL_TREE | |
4917 | && lhs_origtype != lhstype | |
4918 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
4919 | { | |
4920 | tree checktype = (rhs_origtype != NULL_TREE | |
4921 | ? rhs_origtype | |
4922 | : TREE_TYPE (rhs)); | |
4923 | if (checktype != error_mark_node | |
4924 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)) | |
4925 | warning_at (location, OPT_Wc___compat, | |
4926 | "enum conversion in assignment is invalid in C++"); | |
4927 | } | |
4928 | ||
8ce94e44 JM |
4929 | /* Convert new value to destination type. Fold it first, then |
4930 | restore any excess precision information, for the sake of | |
4931 | conversion warnings. */ | |
400fbf9f | 4932 | |
928c19bb JM |
4933 | npc = null_pointer_constant_p (newrhs); |
4934 | newrhs = c_fully_fold (newrhs, false, NULL); | |
8ce94e44 JM |
4935 | if (rhs_semantic_type) |
4936 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
744aa42f ILT |
4937 | newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype, |
4938 | ic_assign, npc, NULL_TREE, NULL_TREE, 0); | |
3e4093b6 RS |
4939 | if (TREE_CODE (newrhs) == ERROR_MARK) |
4940 | return error_mark_node; | |
400fbf9f | 4941 | |
6e955430 ZL |
4942 | /* Emit ObjC write barrier, if necessary. */ |
4943 | if (c_dialect_objc () && flag_objc_gc) | |
4944 | { | |
4945 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
4946 | if (result) | |
c9f9eb5d AH |
4947 | { |
4948 | protected_set_expr_location (result, location); | |
4949 | return result; | |
4950 | } | |
6e955430 ZL |
4951 | } |
4952 | ||
ea4b7848 | 4953 | /* Scan operands. */ |
400fbf9f | 4954 | |
53fb4de3 | 4955 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); |
3e4093b6 | 4956 | TREE_SIDE_EFFECTS (result) = 1; |
c9f9eb5d | 4957 | protected_set_expr_location (result, location); |
400fbf9f | 4958 | |
3e4093b6 RS |
4959 | /* If we got the LHS in a different type for storing in, |
4960 | convert the result back to the nominal type of LHS | |
4961 | so that the value we return always has the same type | |
4962 | as the LHS argument. */ | |
e855c5ce | 4963 | |
3e4093b6 RS |
4964 | if (olhstype == TREE_TYPE (result)) |
4965 | return result; | |
c9f9eb5d | 4966 | |
744aa42f ILT |
4967 | result = convert_for_assignment (location, olhstype, result, rhs_origtype, |
4968 | ic_assign, false, NULL_TREE, NULL_TREE, 0); | |
c9f9eb5d AH |
4969 | protected_set_expr_location (result, location); |
4970 | return result; | |
3e4093b6 RS |
4971 | } |
4972 | \f | |
478a1c5b ILT |
4973 | /* Return whether STRUCT_TYPE has an anonymous field with type TYPE. |
4974 | This is used to implement -fplan9-extensions. */ | |
4975 | ||
4976 | static bool | |
4977 | find_anonymous_field_with_type (tree struct_type, tree type) | |
4978 | { | |
4979 | tree field; | |
4980 | bool found; | |
4981 | ||
4982 | gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE | |
4983 | || TREE_CODE (struct_type) == UNION_TYPE); | |
4984 | found = false; | |
4985 | for (field = TYPE_FIELDS (struct_type); | |
4986 | field != NULL_TREE; | |
4987 | field = TREE_CHAIN (field)) | |
4988 | { | |
4989 | if (DECL_NAME (field) == NULL | |
4990 | && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field)))) | |
4991 | { | |
4992 | if (found) | |
4993 | return false; | |
4994 | found = true; | |
4995 | } | |
4996 | else if (DECL_NAME (field) == NULL | |
4997 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
4998 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
4999 | && find_anonymous_field_with_type (TREE_TYPE (field), type)) | |
5000 | { | |
5001 | if (found) | |
5002 | return false; | |
5003 | found = true; | |
5004 | } | |
5005 | } | |
5006 | return found; | |
5007 | } | |
5008 | ||
5009 | /* RHS is an expression whose type is pointer to struct. If there is | |
5010 | an anonymous field in RHS with type TYPE, then return a pointer to | |
5011 | that field in RHS. This is used with -fplan9-extensions. This | |
5012 | returns NULL if no conversion could be found. */ | |
5013 | ||
5014 | static tree | |
5015 | convert_to_anonymous_field (location_t location, tree type, tree rhs) | |
5016 | { | |
5017 | tree rhs_struct_type, lhs_main_type; | |
5018 | tree field, found_field; | |
5019 | bool found_sub_field; | |
5020 | tree ret; | |
5021 | ||
5022 | gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); | |
5023 | rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); | |
5024 | gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE | |
5025 | || TREE_CODE (rhs_struct_type) == UNION_TYPE); | |
5026 | ||
5027 | gcc_assert (POINTER_TYPE_P (type)); | |
5028 | lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
5029 | ||
5030 | found_field = NULL_TREE; | |
5031 | found_sub_field = false; | |
5032 | for (field = TYPE_FIELDS (rhs_struct_type); | |
5033 | field != NULL_TREE; | |
5034 | field = TREE_CHAIN (field)) | |
5035 | { | |
5036 | if (DECL_NAME (field) != NULL_TREE | |
5037 | || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE | |
5038 | && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)) | |
5039 | continue; | |
5040 | if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field)))) | |
5041 | { | |
5042 | if (found_field != NULL_TREE) | |
5043 | return NULL_TREE; | |
5044 | found_field = field; | |
5045 | } | |
5046 | else if (find_anonymous_field_with_type (TREE_TYPE (field), | |
5047 | lhs_main_type)) | |
5048 | { | |
5049 | if (found_field != NULL_TREE) | |
5050 | return NULL_TREE; | |
5051 | found_field = field; | |
5052 | found_sub_field = true; | |
5053 | } | |
5054 | } | |
5055 | ||
5056 | if (found_field == NULL_TREE) | |
5057 | return NULL_TREE; | |
5058 | ||
5059 | ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), | |
5060 | build_fold_indirect_ref (rhs), found_field, | |
5061 | NULL_TREE); | |
5062 | ret = build_fold_addr_expr_loc (location, ret); | |
5063 | ||
5064 | if (found_sub_field) | |
5065 | { | |
5066 | ret = convert_to_anonymous_field (location, type, ret); | |
5067 | gcc_assert (ret != NULL_TREE); | |
5068 | } | |
5069 | ||
5070 | return ret; | |
5071 | } | |
5072 | ||
bbbbb16a ILT |
5073 | /* Convert value RHS to type TYPE as preparation for an assignment to |
5074 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
5075 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
5076 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
5077 | constant before any folding. | |
3e4093b6 RS |
5078 | The real work of conversion is done by `convert'. |
5079 | The purpose of this function is to generate error messages | |
5080 | for assignments that are not allowed in C. | |
2ac2f164 JM |
5081 | ERRTYPE says whether it is argument passing, assignment, |
5082 | initialization or return. | |
2f6e4e97 | 5083 | |
c2255bc4 | 5084 | LOCATION is the location of the RHS. |
2ac2f164 | 5085 | FUNCTION is a tree for the function being called. |
3e4093b6 | 5086 | PARMNUM is the number of the argument, for printing in error messages. */ |
cb3ca04e | 5087 | |
3e4093b6 | 5088 | static tree |
744aa42f ILT |
5089 | convert_for_assignment (location_t location, tree type, tree rhs, |
5090 | tree origtype, enum impl_conv errtype, | |
5091 | bool null_pointer_constant, tree fundecl, | |
5092 | tree function, int parmnum) | |
3e4093b6 RS |
5093 | { |
5094 | enum tree_code codel = TREE_CODE (type); | |
8ce94e44 | 5095 | tree orig_rhs = rhs; |
3e4093b6 RS |
5096 | tree rhstype; |
5097 | enum tree_code coder; | |
2ac2f164 | 5098 | tree rname = NULL_TREE; |
58393038 | 5099 | bool objc_ok = false; |
2ac2f164 | 5100 | |
6b4ef5c1 | 5101 | if (errtype == ic_argpass) |
2ac2f164 JM |
5102 | { |
5103 | tree selector; | |
5104 | /* Change pointer to function to the function itself for | |
5105 | diagnostics. */ | |
5106 | if (TREE_CODE (function) == ADDR_EXPR | |
5107 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
5108 | function = TREE_OPERAND (function, 0); | |
5109 | ||
5110 | /* Handle an ObjC selector specially for diagnostics. */ | |
5111 | selector = objc_message_selector (); | |
5112 | rname = function; | |
5113 | if (selector && parmnum > 2) | |
5114 | { | |
5115 | rname = selector; | |
5116 | parmnum -= 2; | |
5117 | } | |
5118 | } | |
5119 | ||
5120 | /* This macro is used to emit diagnostics to ensure that all format | |
5121 | strings are complete sentences, visible to gettext and checked at | |
5122 | compile time. */ | |
c2255bc4 | 5123 | #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \ |
1e053dfe MLI |
5124 | do { \ |
5125 | switch (errtype) \ | |
5126 | { \ | |
5127 | case ic_argpass: \ | |
5128 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \ | |
c2255bc4 AH |
5129 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
5130 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
1e053dfe MLI |
5131 | "expected %qT but argument is of type %qT", \ |
5132 | type, rhstype); \ | |
5133 | break; \ | |
1e053dfe MLI |
5134 | case ic_assign: \ |
5135 | pedwarn (LOCATION, OPT, AS); \ | |
5136 | break; \ | |
5137 | case ic_init: \ | |
6a8f4e12 | 5138 | pedwarn_init (LOCATION, OPT, IN); \ |
1e053dfe MLI |
5139 | break; \ |
5140 | case ic_return: \ | |
c2255bc4 | 5141 | pedwarn (LOCATION, OPT, RE); \ |
1e053dfe MLI |
5142 | break; \ |
5143 | default: \ | |
5144 | gcc_unreachable (); \ | |
5145 | } \ | |
2ac2f164 | 5146 | } while (0) |
cb3ca04e | 5147 | |
49706e39 MLI |
5148 | /* This macro is used to emit diagnostics to ensure that all format |
5149 | strings are complete sentences, visible to gettext and checked at | |
5150 | compile time. It is the same as WARN_FOR_ASSIGNMENT but with an | |
5151 | extra parameter to enumerate qualifiers. */ | |
5152 | ||
5153 | #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \ | |
5154 | do { \ | |
5155 | switch (errtype) \ | |
5156 | { \ | |
5157 | case ic_argpass: \ | |
5158 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \ | |
5159 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ | |
5160 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
5161 | "expected %qT but argument is of type %qT", \ | |
5162 | type, rhstype); \ | |
5163 | break; \ | |
5164 | case ic_assign: \ | |
5165 | pedwarn (LOCATION, OPT, AS, QUALS); \ | |
5166 | break; \ | |
5167 | case ic_init: \ | |
5168 | pedwarn (LOCATION, OPT, IN, QUALS); \ | |
5169 | break; \ | |
5170 | case ic_return: \ | |
5171 | pedwarn (LOCATION, OPT, RE, QUALS); \ | |
5172 | break; \ | |
5173 | default: \ | |
5174 | gcc_unreachable (); \ | |
5175 | } \ | |
5176 | } while (0) | |
5177 | ||
8ce94e44 JM |
5178 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
5179 | rhs = TREE_OPERAND (rhs, 0); | |
5180 | ||
3e4093b6 RS |
5181 | rhstype = TREE_TYPE (rhs); |
5182 | coder = TREE_CODE (rhstype); | |
5183 | ||
5184 | if (coder == ERROR_MARK) | |
5185 | return error_mark_node; | |
5186 | ||
58393038 ZL |
5187 | if (c_dialect_objc ()) |
5188 | { | |
5189 | int parmno; | |
5190 | ||
5191 | switch (errtype) | |
5192 | { | |
5193 | case ic_return: | |
5194 | parmno = 0; | |
5195 | break; | |
5196 | ||
5197 | case ic_assign: | |
5198 | parmno = -1; | |
5199 | break; | |
5200 | ||
5201 | case ic_init: | |
5202 | parmno = -2; | |
5203 | break; | |
5204 | ||
5205 | default: | |
5206 | parmno = parmnum; | |
5207 | break; | |
5208 | } | |
5209 | ||
5210 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
5211 | } | |
5212 | ||
bbbbb16a ILT |
5213 | if (warn_cxx_compat) |
5214 | { | |
5215 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
5216 | if (checktype != error_mark_node | |
5217 | && TREE_CODE (type) == ENUMERAL_TYPE | |
5218 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
5219 | { | |
81f40b79 ILT |
5220 | WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat, |
5221 | G_("enum conversion when passing argument " | |
5222 | "%d of %qE is invalid in C++"), | |
5223 | G_("enum conversion in assignment is " | |
5224 | "invalid in C++"), | |
5225 | G_("enum conversion in initialization is " | |
5226 | "invalid in C++"), | |
5227 | G_("enum conversion in return is " | |
5228 | "invalid in C++")); | |
bbbbb16a ILT |
5229 | } |
5230 | } | |
5231 | ||
3e4093b6 | 5232 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) |
59c0753d | 5233 | return rhs; |
3e4093b6 RS |
5234 | |
5235 | if (coder == VOID_TYPE) | |
400fbf9f | 5236 | { |
6dcc04b0 JM |
5237 | /* Except for passing an argument to an unprototyped function, |
5238 | this is a constraint violation. When passing an argument to | |
5239 | an unprototyped function, it is compile-time undefined; | |
5240 | making it a constraint in that case was rejected in | |
5241 | DR#252. */ | |
c2255bc4 | 5242 | error_at (location, "void value not ignored as it ought to be"); |
3e4093b6 | 5243 | return error_mark_node; |
400fbf9f | 5244 | } |
808d6eaa JM |
5245 | rhs = require_complete_type (rhs); |
5246 | if (rhs == error_mark_node) | |
5247 | return error_mark_node; | |
3e4093b6 RS |
5248 | /* A type converts to a reference to it. |
5249 | This code doesn't fully support references, it's just for the | |
5250 | special case of va_start and va_copy. */ | |
5251 | if (codel == REFERENCE_TYPE | |
132da1a5 | 5252 | && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1) |
400fbf9f | 5253 | { |
3e4093b6 | 5254 | if (!lvalue_p (rhs)) |
400fbf9f | 5255 | { |
c2255bc4 | 5256 | error_at (location, "cannot pass rvalue to reference parameter"); |
3e4093b6 | 5257 | return error_mark_node; |
400fbf9f | 5258 | } |
3e4093b6 RS |
5259 | if (!c_mark_addressable (rhs)) |
5260 | return error_mark_node; | |
5261 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
c2255bc4 | 5262 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 RS |
5263 | |
5264 | /* We already know that these two types are compatible, but they | |
5265 | may not be exactly identical. In fact, `TREE_TYPE (type)' is | |
5266 | likely to be __builtin_va_list and `TREE_TYPE (rhs)' is | |
5267 | likely to be va_list, a typedef to __builtin_va_list, which | |
5268 | is different enough that it will cause problems later. */ | |
5269 | if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type)) | |
c2255bc4 AH |
5270 | { |
5271 | rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs); | |
5272 | SET_EXPR_LOCATION (rhs, location); | |
5273 | } | |
3e4093b6 RS |
5274 | |
5275 | rhs = build1 (NOP_EXPR, type, rhs); | |
c2255bc4 | 5276 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 5277 | return rhs; |
400fbf9f | 5278 | } |
3e4093b6 | 5279 | /* Some types can interconvert without explicit casts. */ |
3274deff | 5280 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE |
00c8e9f6 | 5281 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) |
3e4093b6 RS |
5282 | return convert (type, rhs); |
5283 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
5284 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
ab22c1fa | 5285 | || codel == FIXED_POINT_TYPE |
3e4093b6 RS |
5286 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE |
5287 | || codel == BOOLEAN_TYPE) | |
5288 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
ab22c1fa | 5289 | || coder == FIXED_POINT_TYPE |
3e4093b6 RS |
5290 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE |
5291 | || coder == BOOLEAN_TYPE)) | |
928c19bb JM |
5292 | { |
5293 | tree ret; | |
5294 | bool save = in_late_binary_op; | |
dfe776dd | 5295 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) |
928c19bb | 5296 | in_late_binary_op = true; |
8ce94e44 | 5297 | ret = convert_and_check (type, orig_rhs); |
dfe776dd | 5298 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) |
928c19bb JM |
5299 | in_late_binary_op = save; |
5300 | return ret; | |
5301 | } | |
400fbf9f | 5302 | |
79077aea JJ |
5303 | /* Aggregates in different TUs might need conversion. */ |
5304 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
5305 | && codel == coder | |
5306 | && comptypes (type, rhstype)) | |
5307 | return convert_and_check (type, rhs); | |
5308 | ||
ebf0bf7f | 5309 | /* Conversion to a transparent union or record from its member types. |
3e4093b6 | 5310 | This applies only to function arguments. */ |
ebf0bf7f JJ |
5311 | if (((codel == UNION_TYPE || codel == RECORD_TYPE) |
5312 | && TYPE_TRANSPARENT_AGGR (type)) | |
6b4ef5c1 | 5313 | && errtype == ic_argpass) |
400fbf9f | 5314 | { |
0257e383 | 5315 | tree memb, marginal_memb = NULL_TREE; |
3e4093b6 | 5316 | |
910ad8de | 5317 | for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) |
400fbf9f | 5318 | { |
0257e383 | 5319 | tree memb_type = TREE_TYPE (memb); |
400fbf9f | 5320 | |
3e4093b6 | 5321 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
132da1a5 | 5322 | TYPE_MAIN_VARIANT (rhstype))) |
3e4093b6 | 5323 | break; |
e58cd767 | 5324 | |
3e4093b6 RS |
5325 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
5326 | continue; | |
2f6e4e97 | 5327 | |
3e4093b6 RS |
5328 | if (coder == POINTER_TYPE) |
5329 | { | |
5330 | tree ttl = TREE_TYPE (memb_type); | |
5331 | tree ttr = TREE_TYPE (rhstype); | |
400fbf9f | 5332 | |
3e4093b6 RS |
5333 | /* Any non-function converts to a [const][volatile] void * |
5334 | and vice versa; otherwise, targets must be the same. | |
5335 | Meanwhile, the lhs target must have all the qualifiers of | |
5336 | the rhs. */ | |
5337 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
744aa42f | 5338 | || comp_target_types (location, memb_type, rhstype)) |
3e4093b6 RS |
5339 | { |
5340 | /* If this type won't generate any warnings, use it. */ | |
5341 | if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr) | |
5342 | || ((TREE_CODE (ttr) == FUNCTION_TYPE | |
5343 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5344 | ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5345 | == TYPE_QUALS (ttr)) | |
5346 | : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5347 | == TYPE_QUALS (ttl)))) | |
5348 | break; | |
400fbf9f | 5349 | |
3e4093b6 | 5350 | /* Keep looking for a better type, but remember this one. */ |
0257e383 RH |
5351 | if (!marginal_memb) |
5352 | marginal_memb = memb; | |
3e4093b6 RS |
5353 | } |
5354 | } | |
82bde854 | 5355 | |
3e4093b6 | 5356 | /* Can convert integer zero to any pointer type. */ |
928c19bb | 5357 | if (null_pointer_constant) |
3e4093b6 RS |
5358 | { |
5359 | rhs = null_pointer_node; | |
5360 | break; | |
5361 | } | |
5362 | } | |
400fbf9f | 5363 | |
0257e383 | 5364 | if (memb || marginal_memb) |
3e4093b6 | 5365 | { |
0257e383 | 5366 | if (!memb) |
3e4093b6 RS |
5367 | { |
5368 | /* We have only a marginally acceptable member type; | |
5369 | it needs a warning. */ | |
0257e383 | 5370 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); |
3e4093b6 | 5371 | tree ttr = TREE_TYPE (rhstype); |
714a0864 | 5372 | |
3e4093b6 RS |
5373 | /* Const and volatile mean something different for function |
5374 | types, so the usual warnings are not appropriate. */ | |
5375 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
5376 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5377 | { | |
5378 | /* Because const and volatile on functions are | |
5379 | restrictions that say the function will not do | |
5380 | certain things, it is okay to use a const or volatile | |
5381 | function where an ordinary one is wanted, but not | |
5382 | vice-versa. */ | |
36c5e70a BE |
5383 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5384 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
49706e39 | 5385 | WARN_FOR_QUALIFIERS (location, 0, |
509c9d60 | 5386 | G_("passing argument %d of %qE " |
49706e39 | 5387 | "makes %q#v qualified function " |
2ac2f164 | 5388 | "pointer from unqualified"), |
49706e39 | 5389 | G_("assignment makes %q#v qualified " |
2ac2f164 JM |
5390 | "function pointer from " |
5391 | "unqualified"), | |
49706e39 | 5392 | G_("initialization makes %q#v qualified " |
2ac2f164 JM |
5393 | "function pointer from " |
5394 | "unqualified"), | |
49706e39 MLI |
5395 | G_("return makes %q#v qualified function " |
5396 | "pointer from unqualified"), | |
5397 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 | 5398 | } |
36c5e70a BE |
5399 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5400 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
49706e39 | 5401 | WARN_FOR_QUALIFIERS (location, 0, |
509c9d60 | 5402 | G_("passing argument %d of %qE discards " |
49706e39 MLI |
5403 | "%qv qualifier from pointer target type"), |
5404 | G_("assignment discards %qv qualifier " | |
2ac2f164 | 5405 | "from pointer target type"), |
49706e39 | 5406 | G_("initialization discards %qv qualifier " |
2ac2f164 | 5407 | "from pointer target type"), |
49706e39 MLI |
5408 | G_("return discards %qv qualifier from " |
5409 | "pointer target type"), | |
5410 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
0257e383 RH |
5411 | |
5412 | memb = marginal_memb; | |
3e4093b6 | 5413 | } |
400fbf9f | 5414 | |
fcf73884 | 5415 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) |
c1771a20 | 5416 | pedwarn (location, OPT_Wpedantic, |
fcf73884 | 5417 | "ISO C prohibits argument conversion to union type"); |
0e7c47fa | 5418 | |
db3927fb | 5419 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); |
0257e383 | 5420 | return build_constructor_single (type, memb, rhs); |
3e4093b6 | 5421 | } |
0e7c47fa RK |
5422 | } |
5423 | ||
3e4093b6 RS |
5424 | /* Conversions among pointers */ |
5425 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
5426 | && (coder == codel)) | |
400fbf9f | 5427 | { |
3e4093b6 RS |
5428 | tree ttl = TREE_TYPE (type); |
5429 | tree ttr = TREE_TYPE (rhstype); | |
46df2823 JM |
5430 | tree mvl = ttl; |
5431 | tree mvr = ttr; | |
3e4093b6 | 5432 | bool is_opaque_pointer; |
264fa2db | 5433 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
36c5e70a BE |
5434 | addr_space_t asl; |
5435 | addr_space_t asr; | |
400fbf9f | 5436 | |
46df2823 JM |
5437 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
5438 | mvl = TYPE_MAIN_VARIANT (mvl); | |
5439 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
5440 | mvr = TYPE_MAIN_VARIANT (mvr); | |
3e4093b6 | 5441 | /* Opaque pointers are treated like void pointers. */ |
f83c7f63 | 5442 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); |
c22cacf3 | 5443 | |
478a1c5b ILT |
5444 | /* The Plan 9 compiler permits a pointer to a struct to be |
5445 | automatically converted into a pointer to an anonymous field | |
5446 | within the struct. */ | |
5447 | if (flag_plan9_extensions | |
5448 | && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE) | |
5449 | && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE) | |
5450 | && mvl != mvr) | |
5451 | { | |
5452 | tree new_rhs = convert_to_anonymous_field (location, type, rhs); | |
5453 | if (new_rhs != NULL_TREE) | |
5454 | { | |
5455 | rhs = new_rhs; | |
5456 | rhstype = TREE_TYPE (rhs); | |
5457 | coder = TREE_CODE (rhstype); | |
5458 | ttr = TREE_TYPE (rhstype); | |
5459 | mvr = TYPE_MAIN_VARIANT (ttr); | |
5460 | } | |
5461 | } | |
5462 | ||
b7e20b53 | 5463 | /* C++ does not allow the implicit conversion void* -> T*. However, |
c22cacf3 MS |
5464 | for the purpose of reducing the number of false positives, we |
5465 | tolerate the special case of | |
b7e20b53 | 5466 | |
c22cacf3 | 5467 | int *p = NULL; |
b7e20b53 | 5468 | |
c22cacf3 | 5469 | where NULL is typically defined in C to be '(void *) 0'. */ |
c6bdf92e | 5470 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) |
c2255bc4 AH |
5471 | warning_at (location, OPT_Wc___compat, |
5472 | "request for implicit conversion " | |
5473 | "from %qT to %qT not permitted in C++", rhstype, type); | |
400fbf9f | 5474 | |
36c5e70a BE |
5475 | /* See if the pointers point to incompatible address spaces. */ |
5476 | asl = TYPE_ADDR_SPACE (ttl); | |
5477 | asr = TYPE_ADDR_SPACE (ttr); | |
5478 | if (!null_pointer_constant_p (rhs) | |
5479 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
5480 | { | |
5481 | switch (errtype) | |
5482 | { | |
5483 | case ic_argpass: | |
5484 | error_at (location, "passing argument %d of %qE from pointer to " | |
5485 | "non-enclosed address space", parmnum, rname); | |
5486 | break; | |
5487 | case ic_assign: | |
5488 | error_at (location, "assignment from pointer to " | |
5489 | "non-enclosed address space"); | |
5490 | break; | |
5491 | case ic_init: | |
5492 | error_at (location, "initialization from pointer to " | |
5493 | "non-enclosed address space"); | |
5494 | break; | |
5495 | case ic_return: | |
5496 | error_at (location, "return from pointer to " | |
5497 | "non-enclosed address space"); | |
5498 | break; | |
5499 | default: | |
5500 | gcc_unreachable (); | |
5501 | } | |
5502 | return error_mark_node; | |
5503 | } | |
5504 | ||
7876a414 KG |
5505 | /* Check if the right-hand side has a format attribute but the |
5506 | left-hand side doesn't. */ | |
90137d8f | 5507 | if (warn_suggest_attribute_format |
104f8784 | 5508 | && check_missing_format_attribute (type, rhstype)) |
c22cacf3 | 5509 | { |
104f8784 KG |
5510 | switch (errtype) |
5511 | { | |
5512 | case ic_argpass: | |
90137d8f | 5513 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
5514 | "argument %d of %qE might be " |
5515 | "a candidate for a format attribute", | |
5516 | parmnum, rname); | |
104f8784 KG |
5517 | break; |
5518 | case ic_assign: | |
90137d8f | 5519 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
5520 | "assignment left-hand side might be " |
5521 | "a candidate for a format attribute"); | |
104f8784 KG |
5522 | break; |
5523 | case ic_init: | |
90137d8f | 5524 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
5525 | "initialization left-hand side might be " |
5526 | "a candidate for a format attribute"); | |
104f8784 KG |
5527 | break; |
5528 | case ic_return: | |
90137d8f | 5529 | warning_at (location, OPT_Wsuggest_attribute_format, |
c2255bc4 AH |
5530 | "return type might be " |
5531 | "a candidate for a format attribute"); | |
104f8784 KG |
5532 | break; |
5533 | default: | |
5534 | gcc_unreachable (); | |
5535 | } | |
7876a414 | 5536 | } |
c22cacf3 | 5537 | |
3e4093b6 RS |
5538 | /* Any non-function converts to a [const][volatile] void * |
5539 | and vice versa; otherwise, targets must be the same. | |
5540 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
5541 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
744aa42f | 5542 | || (target_cmp = comp_target_types (location, type, rhstype)) |
3e4093b6 | 5543 | || is_opaque_pointer |
f8a93a2e JJ |
5544 | || ((c_common_unsigned_type (mvl) |
5545 | == c_common_unsigned_type (mvr)) | |
5546 | && c_common_signed_type (mvl) | |
5547 | == c_common_signed_type (mvr))) | |
3e4093b6 RS |
5548 | { |
5549 | if (pedantic | |
5550 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5551 | || | |
5552 | (VOID_TYPE_P (ttr) | |
928c19bb | 5553 | && !null_pointer_constant |
3e4093b6 | 5554 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
c1771a20 | 5555 | WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic, |
509c9d60 | 5556 | G_("ISO C forbids passing argument %d of " |
2ac2f164 JM |
5557 | "%qE between function pointer " |
5558 | "and %<void *%>"), | |
4b794eaf | 5559 | G_("ISO C forbids assignment between " |
2ac2f164 | 5560 | "function pointer and %<void *%>"), |
4b794eaf | 5561 | G_("ISO C forbids initialization between " |
2ac2f164 | 5562 | "function pointer and %<void *%>"), |
4b794eaf | 5563 | G_("ISO C forbids return between function " |
2ac2f164 | 5564 | "pointer and %<void *%>")); |
3e4093b6 RS |
5565 | /* Const and volatile mean something different for function types, |
5566 | so the usual warnings are not appropriate. */ | |
5567 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
5568 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
5569 | { | |
36c5e70a BE |
5570 | if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5571 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
58393038 | 5572 | { |
6c39e757 NP |
5573 | WARN_FOR_QUALIFIERS (location, 0, |
5574 | G_("passing argument %d of %qE discards " | |
5575 | "%qv qualifier from pointer target type"), | |
5576 | G_("assignment discards %qv qualifier " | |
5577 | "from pointer target type"), | |
5578 | G_("initialization discards %qv qualifier " | |
5579 | "from pointer target type"), | |
5580 | G_("return discards %qv qualifier from " | |
5581 | "pointer target type"), | |
5582 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
58393038 | 5583 | } |
3e4093b6 RS |
5584 | /* If this is not a case of ignoring a mismatch in signedness, |
5585 | no warning. */ | |
5586 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 5587 | || target_cmp) |
3e4093b6 RS |
5588 | ; |
5589 | /* If there is a mismatch, do warn. */ | |
f2fd3821 | 5590 | else if (warn_pointer_sign) |
c2255bc4 | 5591 | WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign, |
509c9d60 | 5592 | G_("pointer targets in passing argument " |
2ac2f164 | 5593 | "%d of %qE differ in signedness"), |
4b794eaf | 5594 | G_("pointer targets in assignment " |
2ac2f164 | 5595 | "differ in signedness"), |
4b794eaf | 5596 | G_("pointer targets in initialization " |
2ac2f164 | 5597 | "differ in signedness"), |
4b794eaf | 5598 | G_("pointer targets in return differ " |
2ac2f164 | 5599 | "in signedness")); |
3e4093b6 RS |
5600 | } |
5601 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
5602 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5603 | { | |
5604 | /* Because const and volatile on functions are restrictions | |
5605 | that say the function will not do certain things, | |
5606 | it is okay to use a const or volatile function | |
5607 | where an ordinary one is wanted, but not vice-versa. */ | |
36c5e70a BE |
5608 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5609 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
49706e39 | 5610 | WARN_FOR_QUALIFIERS (location, 0, |
509c9d60 | 5611 | G_("passing argument %d of %qE makes " |
49706e39 | 5612 | "%q#v qualified function pointer " |
2ac2f164 | 5613 | "from unqualified"), |
49706e39 | 5614 | G_("assignment makes %q#v qualified function " |
2ac2f164 | 5615 | "pointer from unqualified"), |
49706e39 | 5616 | G_("initialization makes %q#v qualified " |
2ac2f164 | 5617 | "function pointer from unqualified"), |
49706e39 MLI |
5618 | G_("return makes %q#v qualified function " |
5619 | "pointer from unqualified"), | |
5620 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
3e4093b6 RS |
5621 | } |
5622 | } | |
5623 | else | |
58393038 ZL |
5624 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
5625 | if (!objc_ok) | |
c2255bc4 | 5626 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5627 | G_("passing argument %d of %qE from " |
58393038 | 5628 | "incompatible pointer type"), |
4b794eaf JJ |
5629 | G_("assignment from incompatible pointer type"), |
5630 | G_("initialization from incompatible " | |
58393038 | 5631 | "pointer type"), |
4b794eaf | 5632 | G_("return from incompatible pointer type")); |
58393038 | 5633 | |
3e4093b6 RS |
5634 | return convert (type, rhs); |
5635 | } | |
b494fd98 EB |
5636 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
5637 | { | |
6dcc04b0 JM |
5638 | /* ??? This should not be an error when inlining calls to |
5639 | unprototyped functions. */ | |
c2255bc4 | 5640 | error_at (location, "invalid use of non-lvalue array"); |
b494fd98 EB |
5641 | return error_mark_node; |
5642 | } | |
3e4093b6 | 5643 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
400fbf9f | 5644 | { |
3e4093b6 RS |
5645 | /* An explicit constant 0 can convert to a pointer, |
5646 | or one that results from arithmetic, even including | |
5647 | a cast to integer type. */ | |
928c19bb | 5648 | if (!null_pointer_constant) |
c2255bc4 | 5649 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5650 | G_("passing argument %d of %qE makes " |
2ac2f164 | 5651 | "pointer from integer without a cast"), |
4b794eaf | 5652 | G_("assignment makes pointer from integer " |
2ac2f164 | 5653 | "without a cast"), |
4b794eaf | 5654 | G_("initialization makes pointer from " |
2ac2f164 | 5655 | "integer without a cast"), |
4b794eaf | 5656 | G_("return makes pointer from integer " |
2ac2f164 | 5657 | "without a cast")); |
b3006337 EB |
5658 | |
5659 | return convert (type, rhs); | |
400fbf9f | 5660 | } |
3e4093b6 | 5661 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
400fbf9f | 5662 | { |
c2255bc4 | 5663 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5664 | G_("passing argument %d of %qE makes integer " |
2ac2f164 | 5665 | "from pointer without a cast"), |
4b794eaf | 5666 | G_("assignment makes integer from pointer " |
2ac2f164 | 5667 | "without a cast"), |
4b794eaf | 5668 | G_("initialization makes integer from pointer " |
2ac2f164 | 5669 | "without a cast"), |
4b794eaf | 5670 | G_("return makes integer from pointer " |
2ac2f164 | 5671 | "without a cast")); |
3e4093b6 RS |
5672 | return convert (type, rhs); |
5673 | } | |
5674 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
928c19bb JM |
5675 | { |
5676 | tree ret; | |
5677 | bool save = in_late_binary_op; | |
5678 | in_late_binary_op = true; | |
5679 | ret = convert (type, rhs); | |
5680 | in_late_binary_op = save; | |
5681 | return ret; | |
5682 | } | |
400fbf9f | 5683 | |
2ac2f164 | 5684 | switch (errtype) |
3e4093b6 | 5685 | { |
2ac2f164 | 5686 | case ic_argpass: |
c2255bc4 | 5687 | error_at (location, "incompatible type for argument %d of %qE", parmnum, rname); |
a7da8b42 MLI |
5688 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) |
5689 | ? DECL_SOURCE_LOCATION (fundecl) : input_location, | |
5690 | "expected %qT but argument is of type %qT", type, rhstype); | |
2ac2f164 JM |
5691 | break; |
5692 | case ic_assign: | |
c2255bc4 AH |
5693 | error_at (location, "incompatible types when assigning to type %qT from " |
5694 | "type %qT", type, rhstype); | |
2ac2f164 JM |
5695 | break; |
5696 | case ic_init: | |
c2255bc4 AH |
5697 | error_at (location, |
5698 | "incompatible types when initializing type %qT using type %qT", | |
5699 | type, rhstype); | |
2ac2f164 JM |
5700 | break; |
5701 | case ic_return: | |
c2255bc4 AH |
5702 | error_at (location, |
5703 | "incompatible types when returning type %qT but %qT was " | |
5704 | "expected", rhstype, type); | |
2ac2f164 JM |
5705 | break; |
5706 | default: | |
5707 | gcc_unreachable (); | |
400fbf9f | 5708 | } |
53b01f59 | 5709 | |
3e4093b6 RS |
5710 | return error_mark_node; |
5711 | } | |
3e4093b6 RS |
5712 | \f |
5713 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
5714 | return its value. Otherwise, return error_mark_node. | |
15b732b2 | 5715 | |
3e4093b6 RS |
5716 | This is for handling COMPOUND_EXPRs as initializer elements |
5717 | which is allowed with a warning when -pedantic is specified. */ | |
15b732b2 | 5718 | |
3e4093b6 RS |
5719 | static tree |
5720 | valid_compound_expr_initializer (tree value, tree endtype) | |
5721 | { | |
5722 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
5723 | { | |
5724 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
5725 | == error_mark_node) | |
5726 | return error_mark_node; | |
5727 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
5728 | endtype); | |
5729 | } | |
116df786 | 5730 | else if (!initializer_constant_valid_p (value, endtype)) |
3e4093b6 RS |
5731 | return error_mark_node; |
5732 | else | |
5733 | return value; | |
15b732b2 | 5734 | } |
400fbf9f | 5735 | \f |
3e4093b6 RS |
5736 | /* Perform appropriate conversions on the initial value of a variable, |
5737 | store it in the declaration DECL, | |
5738 | and print any error messages that are appropriate. | |
bbbbb16a | 5739 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
c2255bc4 AH |
5740 | If the init is invalid, store an ERROR_MARK. |
5741 | ||
5742 | INIT_LOC is the location of the initial value. */ | |
400fbf9f | 5743 | |
3e4093b6 | 5744 | void |
c2255bc4 | 5745 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) |
400fbf9f | 5746 | { |
3e4093b6 | 5747 | tree value, type; |
928c19bb | 5748 | bool npc = false; |
400fbf9f | 5749 | |
3e4093b6 | 5750 | /* If variable's type was invalidly declared, just ignore it. */ |
400fbf9f | 5751 | |
3e4093b6 RS |
5752 | type = TREE_TYPE (decl); |
5753 | if (TREE_CODE (type) == ERROR_MARK) | |
5754 | return; | |
400fbf9f | 5755 | |
3e4093b6 | 5756 | /* Digest the specified initializer into an expression. */ |
400fbf9f | 5757 | |
928c19bb JM |
5758 | if (init) |
5759 | npc = null_pointer_constant_p (init); | |
c2255bc4 AH |
5760 | value = digest_init (init_loc, type, init, origtype, npc, |
5761 | true, TREE_STATIC (decl)); | |
400fbf9f | 5762 | |
3e4093b6 | 5763 | /* Store the expression if valid; else report error. */ |
400fbf9f | 5764 | |
3176a0c2 | 5765 | if (!in_system_header |
3f75a254 | 5766 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) |
3176a0c2 DD |
5767 | warning (OPT_Wtraditional, "traditional C rejects automatic " |
5768 | "aggregate initialization"); | |
2f6e4e97 | 5769 | |
3e4093b6 | 5770 | DECL_INITIAL (decl) = value; |
400fbf9f | 5771 | |
3e4093b6 RS |
5772 | /* ANSI wants warnings about out-of-range constant initializers. */ |
5773 | STRIP_TYPE_NOPS (value); | |
b8698a0f | 5774 | if (TREE_STATIC (decl)) |
c2658540 | 5775 | constant_expression_warning (value); |
400fbf9f | 5776 | |
3e4093b6 RS |
5777 | /* Check if we need to set array size from compound literal size. */ |
5778 | if (TREE_CODE (type) == ARRAY_TYPE | |
5779 | && TYPE_DOMAIN (type) == 0 | |
5780 | && value != error_mark_node) | |
400fbf9f | 5781 | { |
3e4093b6 RS |
5782 | tree inside_init = init; |
5783 | ||
ed248cf7 | 5784 | STRIP_TYPE_NOPS (inside_init); |
3e4093b6 RS |
5785 | inside_init = fold (inside_init); |
5786 | ||
5787 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
5788 | { | |
8d9f82d5 | 5789 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
3e4093b6 | 5790 | |
8d9f82d5 | 5791 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) |
3e4093b6 RS |
5792 | { |
5793 | /* For int foo[] = (int [3]){1}; we need to set array size | |
5794 | now since later on array initializer will be just the | |
5795 | brace enclosed list of the compound literal. */ | |
e30ecc5d | 5796 | tree etype = strip_array_types (TREE_TYPE (decl)); |
8d9f82d5 | 5797 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); |
8d9f82d5 | 5798 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); |
3e4093b6 | 5799 | layout_type (type); |
8d9f82d5 | 5800 | layout_decl (cldecl, 0); |
e30ecc5d JJ |
5801 | TREE_TYPE (decl) |
5802 | = c_build_qualified_type (type, TYPE_QUALS (etype)); | |
3e4093b6 RS |
5803 | } |
5804 | } | |
400fbf9f | 5805 | } |
3e4093b6 RS |
5806 | } |
5807 | \f | |
5808 | /* Methods for storing and printing names for error messages. */ | |
400fbf9f | 5809 | |
3e4093b6 RS |
5810 | /* Implement a spelling stack that allows components of a name to be pushed |
5811 | and popped. Each element on the stack is this structure. */ | |
400fbf9f | 5812 | |
3e4093b6 RS |
5813 | struct spelling |
5814 | { | |
5815 | int kind; | |
5816 | union | |
400fbf9f | 5817 | { |
a0f0ab9f | 5818 | unsigned HOST_WIDE_INT i; |
3e4093b6 RS |
5819 | const char *s; |
5820 | } u; | |
5821 | }; | |
2f6e4e97 | 5822 | |
3e4093b6 RS |
5823 | #define SPELLING_STRING 1 |
5824 | #define SPELLING_MEMBER 2 | |
5825 | #define SPELLING_BOUNDS 3 | |
400fbf9f | 5826 | |
3e4093b6 RS |
5827 | static struct spelling *spelling; /* Next stack element (unused). */ |
5828 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
5829 | static int spelling_size; /* Size of the spelling stack. */ | |
400fbf9f | 5830 | |
3e4093b6 RS |
5831 | /* Macros to save and restore the spelling stack around push_... functions. |
5832 | Alternative to SAVE_SPELLING_STACK. */ | |
400fbf9f | 5833 | |
3e4093b6 RS |
5834 | #define SPELLING_DEPTH() (spelling - spelling_base) |
5835 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
400fbf9f | 5836 | |
3e4093b6 RS |
5837 | /* Push an element on the spelling stack with type KIND and assign VALUE |
5838 | to MEMBER. */ | |
400fbf9f | 5839 | |
3e4093b6 RS |
5840 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
5841 | { \ | |
5842 | int depth = SPELLING_DEPTH (); \ | |
5843 | \ | |
5844 | if (depth >= spelling_size) \ | |
5845 | { \ | |
5846 | spelling_size += 10; \ | |
cca8ead2 BI |
5847 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
5848 | spelling_size); \ | |
3e4093b6 RS |
5849 | RESTORE_SPELLING_DEPTH (depth); \ |
5850 | } \ | |
5851 | \ | |
5852 | spelling->kind = (KIND); \ | |
5853 | spelling->MEMBER = (VALUE); \ | |
5854 | spelling++; \ | |
5855 | } | |
400fbf9f | 5856 | |
3e4093b6 | 5857 | /* Push STRING on the stack. Printed literally. */ |
400fbf9f | 5858 | |
3e4093b6 RS |
5859 | static void |
5860 | push_string (const char *string) | |
5861 | { | |
5862 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
5863 | } | |
400fbf9f | 5864 | |
3e4093b6 | 5865 | /* Push a member name on the stack. Printed as '.' STRING. */ |
400fbf9f | 5866 | |
3e4093b6 RS |
5867 | static void |
5868 | push_member_name (tree decl) | |
5869 | { | |
5870 | const char *const string | |
88388a52 JM |
5871 | = (DECL_NAME (decl) |
5872 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
5873 | : _("<anonymous>")); | |
3e4093b6 RS |
5874 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
5875 | } | |
400fbf9f | 5876 | |
3e4093b6 | 5877 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
400fbf9f | 5878 | |
3e4093b6 | 5879 | static void |
a0f0ab9f | 5880 | push_array_bounds (unsigned HOST_WIDE_INT bounds) |
3e4093b6 RS |
5881 | { |
5882 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
5883 | } | |
bb58bec5 | 5884 | |
3e4093b6 | 5885 | /* Compute the maximum size in bytes of the printed spelling. */ |
400fbf9f | 5886 | |
3e4093b6 RS |
5887 | static int |
5888 | spelling_length (void) | |
5889 | { | |
5890 | int size = 0; | |
5891 | struct spelling *p; | |
400fbf9f | 5892 | |
3e4093b6 RS |
5893 | for (p = spelling_base; p < spelling; p++) |
5894 | { | |
5895 | if (p->kind == SPELLING_BOUNDS) | |
5896 | size += 25; | |
5897 | else | |
5898 | size += strlen (p->u.s) + 1; | |
5899 | } | |
5900 | ||
5901 | return size; | |
400fbf9f | 5902 | } |
400fbf9f | 5903 | |
3e4093b6 | 5904 | /* Print the spelling to BUFFER and return it. */ |
400fbf9f | 5905 | |
3e4093b6 RS |
5906 | static char * |
5907 | print_spelling (char *buffer) | |
400fbf9f | 5908 | { |
3e4093b6 RS |
5909 | char *d = buffer; |
5910 | struct spelling *p; | |
400fbf9f | 5911 | |
3e4093b6 RS |
5912 | for (p = spelling_base; p < spelling; p++) |
5913 | if (p->kind == SPELLING_BOUNDS) | |
5914 | { | |
a0f0ab9f | 5915 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); |
3e4093b6 RS |
5916 | d += strlen (d); |
5917 | } | |
5918 | else | |
5919 | { | |
5920 | const char *s; | |
5921 | if (p->kind == SPELLING_MEMBER) | |
5922 | *d++ = '.'; | |
5923 | for (s = p->u.s; (*d = *s++); d++) | |
5924 | ; | |
5925 | } | |
5926 | *d++ = '\0'; | |
5927 | return buffer; | |
5928 | } | |
400fbf9f | 5929 | |
3e4093b6 | 5930 | /* Issue an error message for a bad initializer component. |
6a8f4e12 | 5931 | GMSGID identifies the message. |
3e4093b6 | 5932 | The component name is taken from the spelling stack. */ |
400fbf9f | 5933 | |
3e4093b6 | 5934 | void |
6a8f4e12 | 5935 | error_init (const char *gmsgid) |
3e4093b6 RS |
5936 | { |
5937 | char *ofwhat; | |
400fbf9f | 5938 | |
6a8f4e12 AP |
5939 | /* The gmsgid may be a format string with %< and %>. */ |
5940 | error (gmsgid); | |
28dab132 | 5941 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5942 | if (*ofwhat) |
bda67431 | 5943 | error ("(near initialization for %qs)", ofwhat); |
3e4093b6 | 5944 | } |
400fbf9f | 5945 | |
fcf73884 MLI |
5946 | /* Issue a pedantic warning for a bad initializer component. OPT is |
5947 | the option OPT_* (from options.h) controlling this warning or 0 if | |
6a8f4e12 | 5948 | it is unconditionally given. GMSGID identifies the message. The |
fcf73884 | 5949 | component name is taken from the spelling stack. */ |
400fbf9f | 5950 | |
3e4093b6 | 5951 | void |
6a8f4e12 | 5952 | pedwarn_init (location_t location, int opt, const char *gmsgid) |
3e4093b6 RS |
5953 | { |
5954 | char *ofwhat; | |
f90e8e2e | 5955 | |
6a8f4e12 AP |
5956 | /* The gmsgid may be a format string with %< and %>. */ |
5957 | pedwarn (location, opt, gmsgid); | |
28dab132 | 5958 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5959 | if (*ofwhat) |
509c9d60 | 5960 | pedwarn (location, opt, "(near initialization for %qs)", ofwhat); |
3e4093b6 | 5961 | } |
9f720c3e | 5962 | |
b8698a0f | 5963 | /* Issue a warning for a bad initializer component. |
683d6ff9 MLI |
5964 | |
5965 | OPT is the OPT_W* value corresponding to the warning option that | |
6a8f4e12 | 5966 | controls this warning. GMSGID identifies the message. The |
683d6ff9 | 5967 | component name is taken from the spelling stack. */ |
61179109 | 5968 | |
3e4093b6 | 5969 | static void |
6a8f4e12 | 5970 | warning_init (int opt, const char *gmsgid) |
3e4093b6 RS |
5971 | { |
5972 | char *ofwhat; | |
7e842ef8 | 5973 | |
6a8f4e12 AP |
5974 | /* The gmsgid may be a format string with %< and %>. */ |
5975 | warning (opt, gmsgid); | |
28dab132 | 5976 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5977 | if (*ofwhat) |
683d6ff9 | 5978 | warning (opt, "(near initialization for %qs)", ofwhat); |
3e4093b6 RS |
5979 | } |
5980 | \f | |
916c5919 JM |
5981 | /* If TYPE is an array type and EXPR is a parenthesized string |
5982 | constant, warn if pedantic that EXPR is being used to initialize an | |
5983 | object of type TYPE. */ | |
5984 | ||
5985 | void | |
5986 | maybe_warn_string_init (tree type, struct c_expr expr) | |
5987 | { | |
5988 | if (pedantic | |
5989 | && TREE_CODE (type) == ARRAY_TYPE | |
5990 | && TREE_CODE (expr.value) == STRING_CST | |
5991 | && expr.original_code != STRING_CST) | |
c1771a20 | 5992 | pedwarn_init (input_location, OPT_Wpedantic, |
fcf73884 | 5993 | "array initialized from parenthesized string constant"); |
916c5919 JM |
5994 | } |
5995 | ||
3e4093b6 RS |
5996 | /* Digest the parser output INIT as an initializer for type TYPE. |
5997 | Return a C expression of type TYPE to represent the initial value. | |
7e842ef8 | 5998 | |
bbbbb16a ILT |
5999 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
6000 | ||
928c19bb JM |
6001 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. |
6002 | ||
916c5919 JM |
6003 | If INIT is a string constant, STRICT_STRING is true if it is |
6004 | unparenthesized or we should not warn here for it being parenthesized. | |
6005 | For other types of INIT, STRICT_STRING is not used. | |
6006 | ||
c2255bc4 AH |
6007 | INIT_LOC is the location of the INIT. |
6008 | ||
3e4093b6 RS |
6009 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
6010 | elements are seen. */ | |
7e842ef8 | 6011 | |
3e4093b6 | 6012 | static tree |
c2255bc4 AH |
6013 | digest_init (location_t init_loc, tree type, tree init, tree origtype, |
6014 | bool null_pointer_constant, bool strict_string, | |
6015 | int require_constant) | |
3e4093b6 RS |
6016 | { |
6017 | enum tree_code code = TREE_CODE (type); | |
6018 | tree inside_init = init; | |
8ce94e44 | 6019 | tree semantic_type = NULL_TREE; |
928c19bb | 6020 | bool maybe_const = true; |
7e842ef8 | 6021 | |
3e4093b6 | 6022 | if (type == error_mark_node |
f01da1a5 | 6023 | || !init |
3e4093b6 RS |
6024 | || init == error_mark_node |
6025 | || TREE_TYPE (init) == error_mark_node) | |
6026 | return error_mark_node; | |
7e842ef8 | 6027 | |
ed248cf7 | 6028 | STRIP_TYPE_NOPS (inside_init); |
7e842ef8 | 6029 | |
8ce94e44 JM |
6030 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) |
6031 | { | |
6032 | semantic_type = TREE_TYPE (inside_init); | |
6033 | inside_init = TREE_OPERAND (inside_init, 0); | |
6034 | } | |
928c19bb JM |
6035 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); |
6036 | inside_init = decl_constant_value_for_optimization (inside_init); | |
7e842ef8 | 6037 | |
3e4093b6 RS |
6038 | /* Initialization of an array of chars from a string constant |
6039 | optionally enclosed in braces. */ | |
7e842ef8 | 6040 | |
197463ae JM |
6041 | if (code == ARRAY_TYPE && inside_init |
6042 | && TREE_CODE (inside_init) == STRING_CST) | |
3e4093b6 RS |
6043 | { |
6044 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
197463ae JM |
6045 | /* Note that an array could be both an array of character type |
6046 | and an array of wchar_t if wchar_t is signed char or unsigned | |
6047 | char. */ | |
6048 | bool char_array = (typ1 == char_type_node | |
6049 | || typ1 == signed_char_type_node | |
6050 | || typ1 == unsigned_char_type_node); | |
6051 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
c466b2cd KVH |
6052 | bool char16_array = !!comptypes (typ1, char16_type_node); |
6053 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
6054 | ||
6055 | if (char_array || wchar_array || char16_array || char32_array) | |
7e842ef8 | 6056 | { |
916c5919 | 6057 | struct c_expr expr; |
c466b2cd | 6058 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); |
916c5919 JM |
6059 | expr.value = inside_init; |
6060 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
6866c6e8 | 6061 | expr.original_type = NULL; |
916c5919 JM |
6062 | maybe_warn_string_init (type, expr); |
6063 | ||
a45e580b | 6064 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
c1771a20 | 6065 | pedwarn_init (init_loc, OPT_Wpedantic, |
a45e580b JM |
6066 | "initialization of a flexible array member"); |
6067 | ||
3e4093b6 | 6068 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 6069 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 | 6070 | return inside_init; |
7e842ef8 | 6071 | |
c466b2cd | 6072 | if (char_array) |
3e4093b6 | 6073 | { |
c466b2cd KVH |
6074 | if (typ2 != char_type_node) |
6075 | { | |
6076 | error_init ("char-array initialized from wide string"); | |
6077 | return error_mark_node; | |
6078 | } | |
3e4093b6 | 6079 | } |
c466b2cd | 6080 | else |
3e4093b6 | 6081 | { |
c466b2cd KVH |
6082 | if (typ2 == char_type_node) |
6083 | { | |
6084 | error_init ("wide character array initialized from non-wide " | |
6085 | "string"); | |
6086 | return error_mark_node; | |
6087 | } | |
6088 | else if (!comptypes(typ1, typ2)) | |
6089 | { | |
6090 | error_init ("wide character array initialized from " | |
6091 | "incompatible wide string"); | |
6092 | return error_mark_node; | |
6093 | } | |
7e842ef8 | 6094 | } |
2f6e4e97 | 6095 | |
3e4093b6 RS |
6096 | TREE_TYPE (inside_init) = type; |
6097 | if (TYPE_DOMAIN (type) != 0 | |
6098 | && TYPE_SIZE (type) != 0 | |
5eb4df45 ILT |
6099 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
6100 | { | |
6101 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
6102 | ||
c466b2cd | 6103 | /* Subtract the size of a single (possibly wide) character |
3e4093b6 RS |
6104 | because it's ok to ignore the terminating null char |
6105 | that is counted in the length of the constant. */ | |
5eb4df45 ILT |
6106 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), |
6107 | (len | |
6108 | - (TYPE_PRECISION (typ1) | |
6109 | / BITS_PER_UNIT)))) | |
6110 | pedwarn_init (init_loc, 0, | |
6111 | ("initializer-string for array of chars " | |
6112 | "is too long")); | |
6113 | else if (warn_cxx_compat | |
6114 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
6115 | warning_at (init_loc, OPT_Wc___compat, | |
6116 | ("initializer-string for array chars " | |
6117 | "is too long for C++")); | |
6118 | } | |
7e842ef8 | 6119 | |
3e4093b6 | 6120 | return inside_init; |
7e842ef8 | 6121 | } |
197463ae JM |
6122 | else if (INTEGRAL_TYPE_P (typ1)) |
6123 | { | |
6124 | error_init ("array of inappropriate type initialized " | |
6125 | "from string constant"); | |
6126 | return error_mark_node; | |
6127 | } | |
7e842ef8 PE |
6128 | } |
6129 | ||
3e4093b6 RS |
6130 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
6131 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
6132 | below and handle as a constructor. */ | |
e89be13b JJ |
6133 | if (code == VECTOR_TYPE |
6134 | && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE | |
00c8e9f6 | 6135 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) |
e89be13b JJ |
6136 | && TREE_CONSTANT (inside_init)) |
6137 | { | |
6138 | if (TREE_CODE (inside_init) == VECTOR_CST | |
6139 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6140 | TYPE_MAIN_VARIANT (type))) | |
6141 | return inside_init; | |
6142 | ||
6143 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
6144 | { | |
4038c495 GB |
6145 | unsigned HOST_WIDE_INT ix; |
6146 | tree value; | |
6147 | bool constant_p = true; | |
e89be13b JJ |
6148 | |
6149 | /* Iterate through elements and check if all constructor | |
6150 | elements are *_CSTs. */ | |
4038c495 GB |
6151 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) |
6152 | if (!CONSTANT_CLASS_P (value)) | |
6153 | { | |
6154 | constant_p = false; | |
6155 | break; | |
6156 | } | |
e89be13b | 6157 | |
4038c495 GB |
6158 | if (constant_p) |
6159 | return build_vector_from_ctor (type, | |
6160 | CONSTRUCTOR_ELTS (inside_init)); | |
e89be13b JJ |
6161 | } |
6162 | } | |
6035d635 | 6163 | |
ca085fd7 MLI |
6164 | if (warn_sequence_point) |
6165 | verify_sequence_points (inside_init); | |
6166 | ||
3e4093b6 RS |
6167 | /* Any type can be initialized |
6168 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 6169 | |
3e4093b6 RS |
6170 | if (inside_init && TREE_TYPE (inside_init) != 0 |
6171 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
132da1a5 | 6172 | TYPE_MAIN_VARIANT (type)) |
3e4093b6 | 6173 | || (code == ARRAY_TYPE |
132da1a5 | 6174 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6175 | || (code == VECTOR_TYPE |
132da1a5 | 6176 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 6177 | || (code == POINTER_TYPE |
3897f229 | 6178 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
3e4093b6 | 6179 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
132da1a5 | 6180 | TREE_TYPE (type))))) |
3e4093b6 RS |
6181 | { |
6182 | if (code == POINTER_TYPE) | |
b494fd98 | 6183 | { |
b494fd98 EB |
6184 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) |
6185 | { | |
f2a71bbc JM |
6186 | if (TREE_CODE (inside_init) == STRING_CST |
6187 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
c2255bc4 AH |
6188 | inside_init = array_to_pointer_conversion |
6189 | (init_loc, inside_init); | |
f2a71bbc JM |
6190 | else |
6191 | { | |
6192 | error_init ("invalid use of non-lvalue array"); | |
6193 | return error_mark_node; | |
6194 | } | |
b494fd98 | 6195 | } |
f2a71bbc | 6196 | } |
b494fd98 | 6197 | |
bae39a73 NS |
6198 | if (code == VECTOR_TYPE) |
6199 | /* Although the types are compatible, we may require a | |
6200 | conversion. */ | |
6201 | inside_init = convert (type, inside_init); | |
3e4093b6 | 6202 | |
ca58211b PB |
6203 | if (require_constant |
6204 | && (code == VECTOR_TYPE || !flag_isoc99) | |
3e4093b6 | 6205 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) |
400fbf9f | 6206 | { |
3e4093b6 RS |
6207 | /* As an extension, allow initializing objects with static storage |
6208 | duration with compound literals (which are then treated just as | |
ca58211b PB |
6209 | the brace enclosed list they contain). Also allow this for |
6210 | vectors, as we can only assign them with compound literals. */ | |
3e4093b6 RS |
6211 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
6212 | inside_init = DECL_INITIAL (decl); | |
400fbf9f | 6213 | } |
3e4093b6 RS |
6214 | |
6215 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
6216 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 6217 | { |
3e4093b6 RS |
6218 | error_init ("array initialized from non-constant array expression"); |
6219 | return error_mark_node; | |
400fbf9f | 6220 | } |
400fbf9f | 6221 | |
c1771a20 | 6222 | /* Compound expressions can only occur here if -Wpedantic or |
3e4093b6 RS |
6223 | -pedantic-errors is specified. In the later case, we always want |
6224 | an error. In the former case, we simply want a warning. */ | |
6225 | if (require_constant && pedantic | |
6226 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
6227 | { | |
6228 | inside_init | |
6229 | = valid_compound_expr_initializer (inside_init, | |
6230 | TREE_TYPE (inside_init)); | |
6231 | if (inside_init == error_mark_node) | |
6232 | error_init ("initializer element is not constant"); | |
2f6e4e97 | 6233 | else |
c1771a20 | 6234 | pedwarn_init (init_loc, OPT_Wpedantic, |
509c9d60 | 6235 | "initializer element is not constant"); |
3e4093b6 RS |
6236 | if (flag_pedantic_errors) |
6237 | inside_init = error_mark_node; | |
6238 | } | |
6239 | else if (require_constant | |
116df786 RH |
6240 | && !initializer_constant_valid_p (inside_init, |
6241 | TREE_TYPE (inside_init))) | |
3e4093b6 RS |
6242 | { |
6243 | error_init ("initializer element is not constant"); | |
6244 | inside_init = error_mark_node; | |
8b40563c | 6245 | } |
928c19bb | 6246 | else if (require_constant && !maybe_const) |
c2255bc4 | 6247 | pedwarn_init (init_loc, 0, |
928c19bb | 6248 | "initializer element is not a constant expression"); |
f735a153 | 6249 | |
90137d8f | 6250 | /* Added to enable additional -Wsuggest-attribute=format warnings. */ |
8fcef540 | 6251 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) |
c2255bc4 AH |
6252 | inside_init = convert_for_assignment (init_loc, type, inside_init, |
6253 | origtype, | |
bbbbb16a | 6254 | ic_init, null_pointer_constant, |
928c19bb | 6255 | NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
6256 | return inside_init; |
6257 | } | |
f735a153 | 6258 | |
3e4093b6 | 6259 | /* Handle scalar types, including conversions. */ |
400fbf9f | 6260 | |
ab22c1fa CF |
6261 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE |
6262 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
6263 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
400fbf9f | 6264 | { |
f2a71bbc JM |
6265 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE |
6266 | && (TREE_CODE (init) == STRING_CST | |
6267 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
c2255bc4 | 6268 | inside_init = init = array_to_pointer_conversion (init_loc, init); |
8ce94e44 JM |
6269 | if (semantic_type) |
6270 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
6271 | inside_init); | |
3e4093b6 | 6272 | inside_init |
c2255bc4 AH |
6273 | = convert_for_assignment (init_loc, type, inside_init, origtype, |
6274 | ic_init, null_pointer_constant, | |
3e4093b6 | 6275 | NULL_TREE, NULL_TREE, 0); |
2f6e4e97 | 6276 | |
3274deff JW |
6277 | /* Check to see if we have already given an error message. */ |
6278 | if (inside_init == error_mark_node) | |
6279 | ; | |
3f75a254 | 6280 | else if (require_constant && !TREE_CONSTANT (inside_init)) |
400fbf9f | 6281 | { |
3e4093b6 RS |
6282 | error_init ("initializer element is not constant"); |
6283 | inside_init = error_mark_node; | |
400fbf9f | 6284 | } |
3e4093b6 | 6285 | else if (require_constant |
116df786 RH |
6286 | && !initializer_constant_valid_p (inside_init, |
6287 | TREE_TYPE (inside_init))) | |
400fbf9f | 6288 | { |
3e4093b6 RS |
6289 | error_init ("initializer element is not computable at load time"); |
6290 | inside_init = error_mark_node; | |
400fbf9f | 6291 | } |
928c19bb | 6292 | else if (require_constant && !maybe_const) |
c2255bc4 | 6293 | pedwarn_init (init_loc, 0, |
928c19bb | 6294 | "initializer element is not a constant expression"); |
3e4093b6 RS |
6295 | |
6296 | return inside_init; | |
400fbf9f | 6297 | } |
d9fc6069 | 6298 | |
3e4093b6 | 6299 | /* Come here only for records and arrays. */ |
d9fc6069 | 6300 | |
3e4093b6 | 6301 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
d9fc6069 | 6302 | { |
3e4093b6 RS |
6303 | error_init ("variable-sized object may not be initialized"); |
6304 | return error_mark_node; | |
d9fc6069 | 6305 | } |
3e4093b6 RS |
6306 | |
6307 | error_init ("invalid initializer"); | |
6308 | return error_mark_node; | |
d9fc6069 | 6309 | } |
400fbf9f | 6310 | \f |
3e4093b6 | 6311 | /* Handle initializers that use braces. */ |
400fbf9f | 6312 | |
3e4093b6 RS |
6313 | /* Type of object we are accumulating a constructor for. |
6314 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
6315 | static tree constructor_type; | |
400fbf9f | 6316 | |
3e4093b6 RS |
6317 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
6318 | left to fill. */ | |
6319 | static tree constructor_fields; | |
400fbf9f | 6320 | |
3e4093b6 RS |
6321 | /* For an ARRAY_TYPE, this is the specified index |
6322 | at which to store the next element we get. */ | |
6323 | static tree constructor_index; | |
400fbf9f | 6324 | |
3e4093b6 RS |
6325 | /* For an ARRAY_TYPE, this is the maximum index. */ |
6326 | static tree constructor_max_index; | |
400fbf9f | 6327 | |
3e4093b6 RS |
6328 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
6329 | static tree constructor_unfilled_fields; | |
400fbf9f | 6330 | |
3e4093b6 RS |
6331 | /* For an ARRAY_TYPE, this is the index of the first element |
6332 | not yet written out. */ | |
6333 | static tree constructor_unfilled_index; | |
895ea614 | 6334 | |
3e4093b6 RS |
6335 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
6336 | This is so we can generate gaps between fields, when appropriate. */ | |
6337 | static tree constructor_bit_index; | |
10d5caec | 6338 | |
3e4093b6 RS |
6339 | /* If we are saving up the elements rather than allocating them, |
6340 | this is the list of elements so far (in reverse order, | |
6341 | most recent first). */ | |
9771b263 | 6342 | static vec<constructor_elt, va_gc> *constructor_elements; |
ad47f1e5 | 6343 | |
3e4093b6 RS |
6344 | /* 1 if constructor should be incrementally stored into a constructor chain, |
6345 | 0 if all the elements should be kept in AVL tree. */ | |
6346 | static int constructor_incremental; | |
ad47f1e5 | 6347 | |
3e4093b6 RS |
6348 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
6349 | static int constructor_constant; | |
ad47f1e5 | 6350 | |
3e4093b6 RS |
6351 | /* 1 if so far this constructor's elements are all valid address constants. */ |
6352 | static int constructor_simple; | |
ad47f1e5 | 6353 | |
928c19bb JM |
6354 | /* 1 if this constructor has an element that cannot be part of a |
6355 | constant expression. */ | |
6356 | static int constructor_nonconst; | |
6357 | ||
3e4093b6 RS |
6358 | /* 1 if this constructor is erroneous so far. */ |
6359 | static int constructor_erroneous; | |
d45cf215 | 6360 | |
3e4093b6 RS |
6361 | /* Structure for managing pending initializer elements, organized as an |
6362 | AVL tree. */ | |
d45cf215 | 6363 | |
3e4093b6 | 6364 | struct init_node |
d45cf215 | 6365 | { |
3e4093b6 RS |
6366 | struct init_node *left, *right; |
6367 | struct init_node *parent; | |
6368 | int balance; | |
6369 | tree purpose; | |
6370 | tree value; | |
bbbbb16a | 6371 | tree origtype; |
d45cf215 RS |
6372 | }; |
6373 | ||
3e4093b6 RS |
6374 | /* Tree of pending elements at this constructor level. |
6375 | These are elements encountered out of order | |
6376 | which belong at places we haven't reached yet in actually | |
6377 | writing the output. | |
6378 | Will never hold tree nodes across GC runs. */ | |
6379 | static struct init_node *constructor_pending_elts; | |
d45cf215 | 6380 | |
3e4093b6 RS |
6381 | /* The SPELLING_DEPTH of this constructor. */ |
6382 | static int constructor_depth; | |
d45cf215 | 6383 | |
3e4093b6 RS |
6384 | /* DECL node for which an initializer is being read. |
6385 | 0 means we are reading a constructor expression | |
6386 | such as (struct foo) {...}. */ | |
6387 | static tree constructor_decl; | |
d45cf215 | 6388 | |
3e4093b6 RS |
6389 | /* Nonzero if this is an initializer for a top-level decl. */ |
6390 | static int constructor_top_level; | |
d45cf215 | 6391 | |
3e4093b6 RS |
6392 | /* Nonzero if there were any member designators in this initializer. */ |
6393 | static int constructor_designated; | |
d45cf215 | 6394 | |
3e4093b6 RS |
6395 | /* Nesting depth of designator list. */ |
6396 | static int designator_depth; | |
d45cf215 | 6397 | |
3e4093b6 | 6398 | /* Nonzero if there were diagnosed errors in this designator list. */ |
b06df647 | 6399 | static int designator_erroneous; |
d45cf215 | 6400 | |
3e4093b6 RS |
6401 | \f |
6402 | /* This stack has a level for each implicit or explicit level of | |
6403 | structuring in the initializer, including the outermost one. It | |
6404 | saves the values of most of the variables above. */ | |
d45cf215 | 6405 | |
3e4093b6 RS |
6406 | struct constructor_range_stack; |
6407 | ||
6408 | struct constructor_stack | |
d45cf215 | 6409 | { |
3e4093b6 RS |
6410 | struct constructor_stack *next; |
6411 | tree type; | |
6412 | tree fields; | |
6413 | tree index; | |
6414 | tree max_index; | |
6415 | tree unfilled_index; | |
6416 | tree unfilled_fields; | |
6417 | tree bit_index; | |
9771b263 | 6418 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
6419 | struct init_node *pending_elts; |
6420 | int offset; | |
6421 | int depth; | |
916c5919 | 6422 | /* If value nonzero, this value should replace the entire |
3e4093b6 | 6423 | constructor at this level. */ |
916c5919 | 6424 | struct c_expr replacement_value; |
3e4093b6 RS |
6425 | struct constructor_range_stack *range_stack; |
6426 | char constant; | |
6427 | char simple; | |
928c19bb | 6428 | char nonconst; |
3e4093b6 RS |
6429 | char implicit; |
6430 | char erroneous; | |
6431 | char outer; | |
6432 | char incremental; | |
6433 | char designated; | |
6434 | }; | |
d45cf215 | 6435 | |
802415d1 | 6436 | static struct constructor_stack *constructor_stack; |
d45cf215 | 6437 | |
3e4093b6 RS |
6438 | /* This stack represents designators from some range designator up to |
6439 | the last designator in the list. */ | |
d45cf215 | 6440 | |
3e4093b6 RS |
6441 | struct constructor_range_stack |
6442 | { | |
6443 | struct constructor_range_stack *next, *prev; | |
6444 | struct constructor_stack *stack; | |
6445 | tree range_start; | |
6446 | tree index; | |
6447 | tree range_end; | |
6448 | tree fields; | |
6449 | }; | |
d45cf215 | 6450 | |
802415d1 | 6451 | static struct constructor_range_stack *constructor_range_stack; |
d45cf215 | 6452 | |
3e4093b6 RS |
6453 | /* This stack records separate initializers that are nested. |
6454 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
6455 | in cases like { ... (struct foo) { ... } ... }. */ | |
d45cf215 | 6456 | |
3e4093b6 | 6457 | struct initializer_stack |
d45cf215 | 6458 | { |
3e4093b6 RS |
6459 | struct initializer_stack *next; |
6460 | tree decl; | |
3e4093b6 RS |
6461 | struct constructor_stack *constructor_stack; |
6462 | struct constructor_range_stack *constructor_range_stack; | |
9771b263 | 6463 | vec<constructor_elt, va_gc> *elements; |
3e4093b6 RS |
6464 | struct spelling *spelling; |
6465 | struct spelling *spelling_base; | |
6466 | int spelling_size; | |
6467 | char top_level; | |
6468 | char require_constant_value; | |
6469 | char require_constant_elements; | |
6470 | }; | |
d45cf215 | 6471 | |
802415d1 | 6472 | static struct initializer_stack *initializer_stack; |
3e4093b6 RS |
6473 | \f |
6474 | /* Prepare to parse and output the initializer for variable DECL. */ | |
400fbf9f JW |
6475 | |
6476 | void | |
a396f8ae | 6477 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) |
400fbf9f | 6478 | { |
3e4093b6 | 6479 | const char *locus; |
cceb1885 | 6480 | struct initializer_stack *p = XNEW (struct initializer_stack); |
400fbf9f | 6481 | |
3e4093b6 | 6482 | p->decl = constructor_decl; |
3e4093b6 RS |
6483 | p->require_constant_value = require_constant_value; |
6484 | p->require_constant_elements = require_constant_elements; | |
6485 | p->constructor_stack = constructor_stack; | |
6486 | p->constructor_range_stack = constructor_range_stack; | |
6487 | p->elements = constructor_elements; | |
6488 | p->spelling = spelling; | |
6489 | p->spelling_base = spelling_base; | |
6490 | p->spelling_size = spelling_size; | |
6491 | p->top_level = constructor_top_level; | |
6492 | p->next = initializer_stack; | |
6493 | initializer_stack = p; | |
400fbf9f | 6494 | |
3e4093b6 | 6495 | constructor_decl = decl; |
3e4093b6 RS |
6496 | constructor_designated = 0; |
6497 | constructor_top_level = top_level; | |
400fbf9f | 6498 | |
6f17bbcf | 6499 | if (decl != 0 && decl != error_mark_node) |
3e4093b6 RS |
6500 | { |
6501 | require_constant_value = TREE_STATIC (decl); | |
6502 | require_constant_elements | |
6503 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
6504 | /* For a scalar, you can always use any value to initialize, | |
6505 | even within braces. */ | |
6506 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
6507 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
6508 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
6509 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
88388a52 | 6510 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); |
3e4093b6 RS |
6511 | } |
6512 | else | |
6513 | { | |
6514 | require_constant_value = 0; | |
6515 | require_constant_elements = 0; | |
88388a52 | 6516 | locus = _("(anonymous)"); |
3e4093b6 | 6517 | } |
b71c7f8a | 6518 | |
3e4093b6 RS |
6519 | constructor_stack = 0; |
6520 | constructor_range_stack = 0; | |
b71c7f8a | 6521 | |
3e4093b6 RS |
6522 | missing_braces_mentioned = 0; |
6523 | ||
6524 | spelling_base = 0; | |
6525 | spelling_size = 0; | |
6526 | RESTORE_SPELLING_DEPTH (0); | |
6527 | ||
6528 | if (locus) | |
6529 | push_string (locus); | |
6530 | } | |
6531 | ||
6532 | void | |
6533 | finish_init (void) | |
b71c7f8a | 6534 | { |
3e4093b6 | 6535 | struct initializer_stack *p = initializer_stack; |
b71c7f8a | 6536 | |
3e4093b6 RS |
6537 | /* Free the whole constructor stack of this initializer. */ |
6538 | while (constructor_stack) | |
6539 | { | |
6540 | struct constructor_stack *q = constructor_stack; | |
6541 | constructor_stack = q->next; | |
6542 | free (q); | |
6543 | } | |
6544 | ||
366de0ce | 6545 | gcc_assert (!constructor_range_stack); |
3e4093b6 RS |
6546 | |
6547 | /* Pop back to the data of the outer initializer (if any). */ | |
36579663 | 6548 | free (spelling_base); |
3aeb3655 | 6549 | |
3e4093b6 | 6550 | constructor_decl = p->decl; |
3e4093b6 RS |
6551 | require_constant_value = p->require_constant_value; |
6552 | require_constant_elements = p->require_constant_elements; | |
6553 | constructor_stack = p->constructor_stack; | |
6554 | constructor_range_stack = p->constructor_range_stack; | |
6555 | constructor_elements = p->elements; | |
6556 | spelling = p->spelling; | |
6557 | spelling_base = p->spelling_base; | |
6558 | spelling_size = p->spelling_size; | |
6559 | constructor_top_level = p->top_level; | |
6560 | initializer_stack = p->next; | |
6561 | free (p); | |
b71c7f8a | 6562 | } |
400fbf9f | 6563 | \f |
3e4093b6 RS |
6564 | /* Call here when we see the initializer is surrounded by braces. |
6565 | This is instead of a call to push_init_level; | |
6566 | it is matched by a call to pop_init_level. | |
400fbf9f | 6567 | |
3e4093b6 RS |
6568 | TYPE is the type to initialize, for a constructor expression. |
6569 | For an initializer for a decl, TYPE is zero. */ | |
400fbf9f | 6570 | |
3e4093b6 RS |
6571 | void |
6572 | really_start_incremental_init (tree type) | |
400fbf9f | 6573 | { |
5d038c4c | 6574 | struct constructor_stack *p = XNEW (struct constructor_stack); |
400fbf9f | 6575 | |
3e4093b6 RS |
6576 | if (type == 0) |
6577 | type = TREE_TYPE (constructor_decl); | |
400fbf9f | 6578 | |
b6fc2cdb PB |
6579 | if (TREE_CODE (type) == VECTOR_TYPE |
6580 | && TYPE_VECTOR_OPAQUE (type)) | |
3e4093b6 | 6581 | error ("opaque vector types cannot be initialized"); |
400fbf9f | 6582 | |
3e4093b6 RS |
6583 | p->type = constructor_type; |
6584 | p->fields = constructor_fields; | |
6585 | p->index = constructor_index; | |
6586 | p->max_index = constructor_max_index; | |
6587 | p->unfilled_index = constructor_unfilled_index; | |
6588 | p->unfilled_fields = constructor_unfilled_fields; | |
6589 | p->bit_index = constructor_bit_index; | |
6590 | p->elements = constructor_elements; | |
6591 | p->constant = constructor_constant; | |
6592 | p->simple = constructor_simple; | |
928c19bb | 6593 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
6594 | p->erroneous = constructor_erroneous; |
6595 | p->pending_elts = constructor_pending_elts; | |
6596 | p->depth = constructor_depth; | |
916c5919 JM |
6597 | p->replacement_value.value = 0; |
6598 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 6599 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
6600 | p->implicit = 0; |
6601 | p->range_stack = 0; | |
6602 | p->outer = 0; | |
6603 | p->incremental = constructor_incremental; | |
6604 | p->designated = constructor_designated; | |
6605 | p->next = 0; | |
6606 | constructor_stack = p; | |
b13aca19 | 6607 | |
3e4093b6 RS |
6608 | constructor_constant = 1; |
6609 | constructor_simple = 1; | |
928c19bb | 6610 | constructor_nonconst = 0; |
3e4093b6 | 6611 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 6612 | constructor_elements = NULL; |
3e4093b6 RS |
6613 | constructor_pending_elts = 0; |
6614 | constructor_type = type; | |
6615 | constructor_incremental = 1; | |
6616 | constructor_designated = 0; | |
6617 | designator_depth = 0; | |
b06df647 | 6618 | designator_erroneous = 0; |
400fbf9f | 6619 | |
3e4093b6 RS |
6620 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6621 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 6622 | { |
3e4093b6 RS |
6623 | constructor_fields = TYPE_FIELDS (constructor_type); |
6624 | /* Skip any nameless bit fields at the beginning. */ | |
6625 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6626 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 6627 | constructor_fields = DECL_CHAIN (constructor_fields); |
05bccae2 | 6628 | |
3e4093b6 RS |
6629 | constructor_unfilled_fields = constructor_fields; |
6630 | constructor_bit_index = bitsize_zero_node; | |
400fbf9f | 6631 | } |
3e4093b6 RS |
6632 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
6633 | { | |
6634 | if (TYPE_DOMAIN (constructor_type)) | |
6635 | { | |
6636 | constructor_max_index | |
6637 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 6638 | |
3e4093b6 RS |
6639 | /* Detect non-empty initializations of zero-length arrays. */ |
6640 | if (constructor_max_index == NULL_TREE | |
6641 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 6642 | constructor_max_index = integer_minus_one_node; |
400fbf9f | 6643 | |
3e4093b6 RS |
6644 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
6645 | to initialize VLAs will cause a proper error; avoid tree | |
6646 | checking errors as well by setting a safe value. */ | |
6647 | if (constructor_max_index | |
6648 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 6649 | constructor_max_index = integer_minus_one_node; |
59c83dbf | 6650 | |
3e4093b6 RS |
6651 | constructor_index |
6652 | = convert (bitsizetype, | |
6653 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
59c83dbf | 6654 | } |
3e4093b6 | 6655 | else |
493179da JM |
6656 | { |
6657 | constructor_index = bitsize_zero_node; | |
6658 | constructor_max_index = NULL_TREE; | |
6659 | } | |
59c83dbf | 6660 | |
3e4093b6 RS |
6661 | constructor_unfilled_index = constructor_index; |
6662 | } | |
6663 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6664 | { | |
6665 | /* Vectors are like simple fixed-size arrays. */ | |
6666 | constructor_max_index = | |
c62c040f | 6667 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
a0f0ab9f | 6668 | constructor_index = bitsize_zero_node; |
3e4093b6 RS |
6669 | constructor_unfilled_index = constructor_index; |
6670 | } | |
6671 | else | |
6672 | { | |
6673 | /* Handle the case of int x = {5}; */ | |
6674 | constructor_fields = constructor_type; | |
6675 | constructor_unfilled_fields = constructor_type; | |
6676 | } | |
6677 | } | |
6678 | \f | |
6679 | /* Push down into a subobject, for initialization. | |
6680 | If this is for an explicit set of braces, IMPLICIT is 0. | |
6681 | If it is because the next element belongs at a lower level, | |
6682 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
400fbf9f | 6683 | |
3e4093b6 | 6684 | void |
a1e3b3d9 | 6685 | push_init_level (int implicit, struct obstack * braced_init_obstack) |
3e4093b6 RS |
6686 | { |
6687 | struct constructor_stack *p; | |
6688 | tree value = NULL_TREE; | |
400fbf9f | 6689 | |
3e4093b6 | 6690 | /* If we've exhausted any levels that didn't have braces, |
472d98b4 JM |
6691 | pop them now. If implicit == 1, this will have been done in |
6692 | process_init_element; do not repeat it here because in the case | |
6693 | of excess initializers for an empty aggregate this leads to an | |
6694 | infinite cycle of popping a level and immediately recreating | |
6695 | it. */ | |
6696 | if (implicit != 1) | |
3e4093b6 | 6697 | { |
472d98b4 JM |
6698 | while (constructor_stack->implicit) |
6699 | { | |
6700 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6701 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6702 | && constructor_fields == 0) | |
a1e3b3d9 LB |
6703 | process_init_element (pop_init_level (1, braced_init_obstack), |
6704 | true, braced_init_obstack); | |
472d98b4 JM |
6705 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE |
6706 | && constructor_max_index | |
6707 | && tree_int_cst_lt (constructor_max_index, | |
6708 | constructor_index)) | |
a1e3b3d9 LB |
6709 | process_init_element (pop_init_level (1, braced_init_obstack), |
6710 | true, braced_init_obstack); | |
472d98b4 JM |
6711 | else |
6712 | break; | |
6713 | } | |
3e4093b6 | 6714 | } |
400fbf9f | 6715 | |
3e4093b6 RS |
6716 | /* Unless this is an explicit brace, we need to preserve previous |
6717 | content if any. */ | |
6718 | if (implicit) | |
6719 | { | |
6720 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6721 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6722 | && constructor_fields) | |
a1e3b3d9 | 6723 | value = find_init_member (constructor_fields, braced_init_obstack); |
3e4093b6 | 6724 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
a1e3b3d9 | 6725 | value = find_init_member (constructor_index, braced_init_obstack); |
400fbf9f JW |
6726 | } |
6727 | ||
5d038c4c | 6728 | p = XNEW (struct constructor_stack); |
3e4093b6 RS |
6729 | p->type = constructor_type; |
6730 | p->fields = constructor_fields; | |
6731 | p->index = constructor_index; | |
6732 | p->max_index = constructor_max_index; | |
6733 | p->unfilled_index = constructor_unfilled_index; | |
6734 | p->unfilled_fields = constructor_unfilled_fields; | |
6735 | p->bit_index = constructor_bit_index; | |
6736 | p->elements = constructor_elements; | |
6737 | p->constant = constructor_constant; | |
6738 | p->simple = constructor_simple; | |
928c19bb | 6739 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
6740 | p->erroneous = constructor_erroneous; |
6741 | p->pending_elts = constructor_pending_elts; | |
6742 | p->depth = constructor_depth; | |
916c5919 JM |
6743 | p->replacement_value.value = 0; |
6744 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 6745 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
6746 | p->implicit = implicit; |
6747 | p->outer = 0; | |
6748 | p->incremental = constructor_incremental; | |
6749 | p->designated = constructor_designated; | |
6750 | p->next = constructor_stack; | |
6751 | p->range_stack = 0; | |
6752 | constructor_stack = p; | |
400fbf9f | 6753 | |
3e4093b6 RS |
6754 | constructor_constant = 1; |
6755 | constructor_simple = 1; | |
928c19bb | 6756 | constructor_nonconst = 0; |
3e4093b6 | 6757 | constructor_depth = SPELLING_DEPTH (); |
9771b263 | 6758 | constructor_elements = NULL; |
3e4093b6 RS |
6759 | constructor_incremental = 1; |
6760 | constructor_designated = 0; | |
6761 | constructor_pending_elts = 0; | |
6762 | if (!implicit) | |
400fbf9f | 6763 | { |
3e4093b6 RS |
6764 | p->range_stack = constructor_range_stack; |
6765 | constructor_range_stack = 0; | |
6766 | designator_depth = 0; | |
b06df647 | 6767 | designator_erroneous = 0; |
3e4093b6 | 6768 | } |
400fbf9f | 6769 | |
3e4093b6 RS |
6770 | /* Don't die if an entire brace-pair level is superfluous |
6771 | in the containing level. */ | |
6772 | if (constructor_type == 0) | |
6773 | ; | |
6774 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6775 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6776 | { | |
6777 | /* Don't die if there are extra init elts at the end. */ | |
6778 | if (constructor_fields == 0) | |
6779 | constructor_type = 0; | |
6780 | else | |
400fbf9f | 6781 | { |
3e4093b6 RS |
6782 | constructor_type = TREE_TYPE (constructor_fields); |
6783 | push_member_name (constructor_fields); | |
6784 | constructor_depth++; | |
400fbf9f | 6785 | } |
3e4093b6 RS |
6786 | } |
6787 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6788 | { | |
6789 | constructor_type = TREE_TYPE (constructor_type); | |
a0f0ab9f | 6790 | push_array_bounds (tree_low_cst (constructor_index, 1)); |
3e4093b6 | 6791 | constructor_depth++; |
400fbf9f JW |
6792 | } |
6793 | ||
3e4093b6 | 6794 | if (constructor_type == 0) |
400fbf9f | 6795 | { |
3e4093b6 RS |
6796 | error_init ("extra brace group at end of initializer"); |
6797 | constructor_fields = 0; | |
6798 | constructor_unfilled_fields = 0; | |
6799 | return; | |
400fbf9f JW |
6800 | } |
6801 | ||
3e4093b6 RS |
6802 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
6803 | { | |
6804 | constructor_constant = TREE_CONSTANT (value); | |
6805 | constructor_simple = TREE_STATIC (value); | |
928c19bb | 6806 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); |
3e4093b6 | 6807 | constructor_elements = CONSTRUCTOR_ELTS (value); |
9771b263 | 6808 | if (!vec_safe_is_empty (constructor_elements) |
3e4093b6 RS |
6809 | && (TREE_CODE (constructor_type) == RECORD_TYPE |
6810 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
a1e3b3d9 | 6811 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 6812 | } |
400fbf9f | 6813 | |
3e4093b6 RS |
6814 | if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) |
6815 | { | |
6816 | missing_braces_mentioned = 1; | |
683d6ff9 | 6817 | warning_init (OPT_Wmissing_braces, "missing braces around initializer"); |
3e4093b6 | 6818 | } |
400fbf9f | 6819 | |
3e4093b6 RS |
6820 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6821 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6822 | { | |
6823 | constructor_fields = TYPE_FIELDS (constructor_type); | |
6824 | /* Skip any nameless bit fields at the beginning. */ | |
6825 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6826 | && DECL_NAME (constructor_fields) == 0) | |
f7587ed0 | 6827 | constructor_fields = DECL_CHAIN (constructor_fields); |
103b7b17 | 6828 | |
3e4093b6 RS |
6829 | constructor_unfilled_fields = constructor_fields; |
6830 | constructor_bit_index = bitsize_zero_node; | |
6831 | } | |
6832 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6833 | { | |
6834 | /* Vectors are like simple fixed-size arrays. */ | |
6835 | constructor_max_index = | |
c62c040f RG |
6836 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
6837 | constructor_index = bitsize_int (0); | |
3e4093b6 RS |
6838 | constructor_unfilled_index = constructor_index; |
6839 | } | |
6840 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6841 | { | |
6842 | if (TYPE_DOMAIN (constructor_type)) | |
6843 | { | |
6844 | constructor_max_index | |
6845 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 6846 | |
3e4093b6 RS |
6847 | /* Detect non-empty initializations of zero-length arrays. */ |
6848 | if (constructor_max_index == NULL_TREE | |
6849 | && TYPE_SIZE (constructor_type)) | |
9a9d280e | 6850 | constructor_max_index = integer_minus_one_node; |
de520661 | 6851 | |
3e4093b6 RS |
6852 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
6853 | to initialize VLAs will cause a proper error; avoid tree | |
6854 | checking errors as well by setting a safe value. */ | |
6855 | if (constructor_max_index | |
6856 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
9a9d280e | 6857 | constructor_max_index = integer_minus_one_node; |
b62acd60 | 6858 | |
3e4093b6 RS |
6859 | constructor_index |
6860 | = convert (bitsizetype, | |
6861 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
6862 | } | |
6863 | else | |
6864 | constructor_index = bitsize_zero_node; | |
de520661 | 6865 | |
3e4093b6 RS |
6866 | constructor_unfilled_index = constructor_index; |
6867 | if (value && TREE_CODE (value) == STRING_CST) | |
6868 | { | |
6869 | /* We need to split the char/wchar array into individual | |
6870 | characters, so that we don't have to special case it | |
6871 | everywhere. */ | |
a1e3b3d9 | 6872 | set_nonincremental_init_from_string (value, braced_init_obstack); |
3e4093b6 RS |
6873 | } |
6874 | } | |
6875 | else | |
6876 | { | |
b4519d39 | 6877 | if (constructor_type != error_mark_node) |
683d6ff9 | 6878 | warning_init (0, "braces around scalar initializer"); |
3e4093b6 RS |
6879 | constructor_fields = constructor_type; |
6880 | constructor_unfilled_fields = constructor_type; | |
6881 | } | |
6882 | } | |
8b6a5902 | 6883 | |
3e4093b6 | 6884 | /* At the end of an implicit or explicit brace level, |
916c5919 JM |
6885 | finish up that level of constructor. If a single expression |
6886 | with redundant braces initialized that level, return the | |
6887 | c_expr structure for that expression. Otherwise, the original_code | |
6888 | element is set to ERROR_MARK. | |
6889 | If we were outputting the elements as they are read, return 0 as the value | |
3e4093b6 | 6890 | from inner levels (process_init_element ignores that), |
916c5919 | 6891 | but return error_mark_node as the value from the outermost level |
3e4093b6 | 6892 | (that's what we want to put in DECL_INITIAL). |
916c5919 | 6893 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
de520661 | 6894 | |
916c5919 | 6895 | struct c_expr |
a1e3b3d9 | 6896 | pop_init_level (int implicit, struct obstack * braced_init_obstack) |
3e4093b6 RS |
6897 | { |
6898 | struct constructor_stack *p; | |
916c5919 JM |
6899 | struct c_expr ret; |
6900 | ret.value = 0; | |
6901 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 6902 | ret.original_type = NULL; |
de520661 | 6903 | |
3e4093b6 RS |
6904 | if (implicit == 0) |
6905 | { | |
6906 | /* When we come to an explicit close brace, | |
6907 | pop any inner levels that didn't have explicit braces. */ | |
6908 | while (constructor_stack->implicit) | |
a1e3b3d9 LB |
6909 | { |
6910 | process_init_element (pop_init_level (1, braced_init_obstack), | |
6911 | true, braced_init_obstack); | |
6912 | } | |
366de0ce | 6913 | gcc_assert (!constructor_range_stack); |
3e4093b6 | 6914 | } |
e5e809f4 | 6915 | |
0066ef9c RH |
6916 | /* Now output all pending elements. */ |
6917 | constructor_incremental = 1; | |
a1e3b3d9 | 6918 | output_pending_init_elements (1, braced_init_obstack); |
0066ef9c | 6919 | |
3e4093b6 | 6920 | p = constructor_stack; |
e5e809f4 | 6921 | |
3e4093b6 RS |
6922 | /* Error for initializing a flexible array member, or a zero-length |
6923 | array member in an inappropriate context. */ | |
6924 | if (constructor_type && constructor_fields | |
6925 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
6926 | && TYPE_DOMAIN (constructor_type) | |
3f75a254 | 6927 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
3e4093b6 RS |
6928 | { |
6929 | /* Silently discard empty initializations. The parser will | |
6930 | already have pedwarned for empty brackets. */ | |
6931 | if (integer_zerop (constructor_unfilled_index)) | |
6932 | constructor_type = NULL_TREE; | |
366de0ce | 6933 | else |
3e4093b6 | 6934 | { |
366de0ce | 6935 | gcc_assert (!TYPE_SIZE (constructor_type)); |
c22cacf3 | 6936 | |
3e4093b6 RS |
6937 | if (constructor_depth > 2) |
6938 | error_init ("initialization of flexible array member in a nested context"); | |
fcf73884 | 6939 | else |
c1771a20 | 6940 | pedwarn_init (input_location, OPT_Wpedantic, |
509c9d60 | 6941 | "initialization of a flexible array member"); |
de520661 | 6942 | |
3e4093b6 RS |
6943 | /* We have already issued an error message for the existence |
6944 | of a flexible array member not at the end of the structure. | |
535a42b1 | 6945 | Discard the initializer so that we do not die later. */ |
910ad8de | 6946 | if (DECL_CHAIN (constructor_fields) != NULL_TREE) |
3e4093b6 RS |
6947 | constructor_type = NULL_TREE; |
6948 | } | |
3e4093b6 | 6949 | } |
de520661 | 6950 | |
3e4093b6 | 6951 | /* Warn when some struct elements are implicitly initialized to zero. */ |
eaac4679 | 6952 | if (warn_missing_field_initializers |
3e4093b6 RS |
6953 | && constructor_type |
6954 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
6955 | && constructor_unfilled_fields) | |
6956 | { | |
49819fef | 6957 | bool constructor_zeroinit = |
9771b263 DN |
6958 | (vec_safe_length (constructor_elements) == 1 |
6959 | && integer_zerop ((*constructor_elements)[0].value)); | |
49819fef | 6960 | |
3e4093b6 RS |
6961 | /* Do not warn for flexible array members or zero-length arrays. */ |
6962 | while (constructor_unfilled_fields | |
3f75a254 | 6963 | && (!DECL_SIZE (constructor_unfilled_fields) |
3e4093b6 | 6964 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) |
910ad8de | 6965 | constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); |
cc77d4d5 | 6966 | |
49819fef AM |
6967 | if (constructor_unfilled_fields |
6968 | /* Do not warn if this level of the initializer uses member | |
6969 | designators; it is likely to be deliberate. */ | |
6970 | && !constructor_designated | |
6971 | /* Do not warn about initializing with ` = {0}'. */ | |
6972 | && !constructor_zeroinit) | |
3e4093b6 | 6973 | { |
32397f22 MLI |
6974 | if (warning_at (input_location, OPT_Wmissing_field_initializers, |
6975 | "missing initializer for field %qD of %qT", | |
6976 | constructor_unfilled_fields, | |
6977 | constructor_type)) | |
6978 | inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), | |
ae933128 | 6979 | "%qD declared here", constructor_unfilled_fields); |
3e4093b6 RS |
6980 | } |
6981 | } | |
de520661 | 6982 | |
3e4093b6 | 6983 | /* Pad out the end of the structure. */ |
916c5919 | 6984 | if (p->replacement_value.value) |
3e4093b6 RS |
6985 | /* If this closes a superfluous brace pair, |
6986 | just pass out the element between them. */ | |
916c5919 | 6987 | ret = p->replacement_value; |
3e4093b6 RS |
6988 | else if (constructor_type == 0) |
6989 | ; | |
6990 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
6991 | && TREE_CODE (constructor_type) != UNION_TYPE | |
6992 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
6993 | && TREE_CODE (constructor_type) != VECTOR_TYPE) | |
6994 | { | |
6995 | /* A nonincremental scalar initializer--just return | |
6996 | the element, after verifying there is just one. */ | |
9771b263 | 6997 | if (vec_safe_is_empty (constructor_elements)) |
3e4093b6 RS |
6998 | { |
6999 | if (!constructor_erroneous) | |
7000 | error_init ("empty scalar initializer"); | |
916c5919 | 7001 | ret.value = error_mark_node; |
3e4093b6 | 7002 | } |
9771b263 | 7003 | else if (vec_safe_length (constructor_elements) != 1) |
3e4093b6 RS |
7004 | { |
7005 | error_init ("extra elements in scalar initializer"); | |
9771b263 | 7006 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7007 | } |
7008 | else | |
9771b263 | 7009 | ret.value = (*constructor_elements)[0].value; |
3e4093b6 RS |
7010 | } |
7011 | else | |
7012 | { | |
7013 | if (constructor_erroneous) | |
916c5919 | 7014 | ret.value = error_mark_node; |
3e4093b6 RS |
7015 | else |
7016 | { | |
916c5919 | 7017 | ret.value = build_constructor (constructor_type, |
4038c495 | 7018 | constructor_elements); |
3e4093b6 | 7019 | if (constructor_constant) |
51eed280 | 7020 | TREE_CONSTANT (ret.value) = 1; |
3e4093b6 | 7021 | if (constructor_constant && constructor_simple) |
916c5919 | 7022 | TREE_STATIC (ret.value) = 1; |
928c19bb JM |
7023 | if (constructor_nonconst) |
7024 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
3e4093b6 RS |
7025 | } |
7026 | } | |
de520661 | 7027 | |
928c19bb JM |
7028 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) |
7029 | { | |
7030 | if (constructor_nonconst) | |
7031 | ret.original_code = C_MAYBE_CONST_EXPR; | |
7032 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
7033 | ret.original_code = ERROR_MARK; | |
7034 | } | |
7035 | ||
3e4093b6 RS |
7036 | constructor_type = p->type; |
7037 | constructor_fields = p->fields; | |
7038 | constructor_index = p->index; | |
7039 | constructor_max_index = p->max_index; | |
7040 | constructor_unfilled_index = p->unfilled_index; | |
7041 | constructor_unfilled_fields = p->unfilled_fields; | |
7042 | constructor_bit_index = p->bit_index; | |
7043 | constructor_elements = p->elements; | |
7044 | constructor_constant = p->constant; | |
7045 | constructor_simple = p->simple; | |
928c19bb | 7046 | constructor_nonconst = p->nonconst; |
3e4093b6 RS |
7047 | constructor_erroneous = p->erroneous; |
7048 | constructor_incremental = p->incremental; | |
7049 | constructor_designated = p->designated; | |
7050 | constructor_pending_elts = p->pending_elts; | |
7051 | constructor_depth = p->depth; | |
7052 | if (!p->implicit) | |
7053 | constructor_range_stack = p->range_stack; | |
7054 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
de520661 | 7055 | |
3e4093b6 RS |
7056 | constructor_stack = p->next; |
7057 | free (p); | |
b621a4dd | 7058 | |
5d5e98dc VR |
7059 | if (ret.value == 0 && constructor_stack == 0) |
7060 | ret.value = error_mark_node; | |
916c5919 | 7061 | return ret; |
3e4093b6 | 7062 | } |
8b6a5902 | 7063 | |
3e4093b6 RS |
7064 | /* Common handling for both array range and field name designators. |
7065 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
400fbf9f | 7066 | |
3e4093b6 | 7067 | static int |
a1e3b3d9 | 7068 | set_designator (int array, struct obstack * braced_init_obstack) |
de520661 | 7069 | { |
3e4093b6 RS |
7070 | tree subtype; |
7071 | enum tree_code subcode; | |
de520661 | 7072 | |
3e4093b6 RS |
7073 | /* Don't die if an entire brace-pair level is superfluous |
7074 | in the containing level. */ | |
7075 | if (constructor_type == 0) | |
7076 | return 1; | |
de520661 | 7077 | |
366de0ce NS |
7078 | /* If there were errors in this designator list already, bail out |
7079 | silently. */ | |
b06df647 | 7080 | if (designator_erroneous) |
3e4093b6 | 7081 | return 1; |
e28cae4f | 7082 | |
3e4093b6 RS |
7083 | if (!designator_depth) |
7084 | { | |
366de0ce | 7085 | gcc_assert (!constructor_range_stack); |
de520661 | 7086 | |
3e4093b6 RS |
7087 | /* Designator list starts at the level of closest explicit |
7088 | braces. */ | |
7089 | while (constructor_stack->implicit) | |
a1e3b3d9 LB |
7090 | { |
7091 | process_init_element (pop_init_level (1, braced_init_obstack), | |
7092 | true, braced_init_obstack); | |
7093 | } | |
3e4093b6 RS |
7094 | constructor_designated = 1; |
7095 | return 0; | |
7096 | } | |
de520661 | 7097 | |
366de0ce | 7098 | switch (TREE_CODE (constructor_type)) |
3c3fa147 | 7099 | { |
366de0ce NS |
7100 | case RECORD_TYPE: |
7101 | case UNION_TYPE: | |
3e4093b6 RS |
7102 | subtype = TREE_TYPE (constructor_fields); |
7103 | if (subtype != error_mark_node) | |
7104 | subtype = TYPE_MAIN_VARIANT (subtype); | |
366de0ce NS |
7105 | break; |
7106 | case ARRAY_TYPE: | |
3e4093b6 | 7107 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
366de0ce NS |
7108 | break; |
7109 | default: | |
7110 | gcc_unreachable (); | |
de520661 | 7111 | } |
400fbf9f | 7112 | |
3e4093b6 RS |
7113 | subcode = TREE_CODE (subtype); |
7114 | if (array && subcode != ARRAY_TYPE) | |
7115 | { | |
7116 | error_init ("array index in non-array initializer"); | |
7117 | return 1; | |
7118 | } | |
7119 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
7120 | { | |
7121 | error_init ("field name not in record or union initializer"); | |
7122 | return 1; | |
7123 | } | |
d45cf215 | 7124 | |
3e4093b6 | 7125 | constructor_designated = 1; |
a1e3b3d9 | 7126 | push_init_level (2, braced_init_obstack); |
3e4093b6 | 7127 | return 0; |
de520661 | 7128 | } |
400fbf9f | 7129 | |
3e4093b6 RS |
7130 | /* If there are range designators in designator list, push a new designator |
7131 | to constructor_range_stack. RANGE_END is end of such stack range or | |
7132 | NULL_TREE if there is no range designator at this level. */ | |
400fbf9f | 7133 | |
3e4093b6 | 7134 | static void |
a1e3b3d9 | 7135 | push_range_stack (tree range_end, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7136 | { |
7137 | struct constructor_range_stack *p; | |
400fbf9f | 7138 | |
a1e3b3d9 LB |
7139 | p = (struct constructor_range_stack *) |
7140 | obstack_alloc (braced_init_obstack, | |
7141 | sizeof (struct constructor_range_stack)); | |
3e4093b6 RS |
7142 | p->prev = constructor_range_stack; |
7143 | p->next = 0; | |
7144 | p->fields = constructor_fields; | |
7145 | p->range_start = constructor_index; | |
7146 | p->index = constructor_index; | |
7147 | p->stack = constructor_stack; | |
7148 | p->range_end = range_end; | |
8b6a5902 | 7149 | if (constructor_range_stack) |
3e4093b6 RS |
7150 | constructor_range_stack->next = p; |
7151 | constructor_range_stack = p; | |
de520661 | 7152 | } |
400fbf9f | 7153 | |
3e4093b6 RS |
7154 | /* Within an array initializer, specify the next index to be initialized. |
7155 | FIRST is that index. If LAST is nonzero, then initialize a range | |
7156 | of indices, running from FIRST through LAST. */ | |
5a7ec9d9 | 7157 | |
de520661 | 7158 | void |
a1e3b3d9 LB |
7159 | set_init_index (tree first, tree last, |
7160 | struct obstack * braced_init_obstack) | |
de520661 | 7161 | { |
a1e3b3d9 | 7162 | if (set_designator (1, braced_init_obstack)) |
3e4093b6 | 7163 | return; |
de520661 | 7164 | |
b06df647 | 7165 | designator_erroneous = 1; |
de520661 | 7166 | |
3ea8cd06 JM |
7167 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
7168 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
7169 | { | |
7170 | error_init ("array index in initializer not of integer type"); | |
7171 | return; | |
7172 | } | |
7173 | ||
2b6da65c JM |
7174 | if (TREE_CODE (first) != INTEGER_CST) |
7175 | { | |
7176 | first = c_fully_fold (first, false, NULL); | |
7177 | if (TREE_CODE (first) == INTEGER_CST) | |
c1771a20 | 7178 | pedwarn_init (input_location, OPT_Wpedantic, |
2b6da65c JM |
7179 | "array index in initializer is not " |
7180 | "an integer constant expression"); | |
7181 | } | |
7182 | ||
7183 | if (last && TREE_CODE (last) != INTEGER_CST) | |
7184 | { | |
7185 | last = c_fully_fold (last, false, NULL); | |
7186 | if (TREE_CODE (last) == INTEGER_CST) | |
c1771a20 | 7187 | pedwarn_init (input_location, OPT_Wpedantic, |
2b6da65c JM |
7188 | "array index in initializer is not " |
7189 | "an integer constant expression"); | |
7190 | } | |
7191 | ||
3e4093b6 RS |
7192 | if (TREE_CODE (first) != INTEGER_CST) |
7193 | error_init ("nonconstant array index in initializer"); | |
7194 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) | |
7195 | error_init ("nonconstant array index in initializer"); | |
7196 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7197 | error_init ("array index in non-array initializer"); | |
622adc7e MK |
7198 | else if (tree_int_cst_sgn (first) == -1) |
7199 | error_init ("array index in initializer exceeds array bounds"); | |
3e4093b6 RS |
7200 | else if (constructor_max_index |
7201 | && tree_int_cst_lt (constructor_max_index, first)) | |
7202 | error_init ("array index in initializer exceeds array bounds"); | |
7203 | else | |
de520661 | 7204 | { |
928c19bb JM |
7205 | constant_expression_warning (first); |
7206 | if (last) | |
7207 | constant_expression_warning (last); | |
3e4093b6 | 7208 | constructor_index = convert (bitsizetype, first); |
665f2503 | 7209 | |
3e4093b6 | 7210 | if (last) |
2bede729 | 7211 | { |
3e4093b6 RS |
7212 | if (tree_int_cst_equal (first, last)) |
7213 | last = 0; | |
7214 | else if (tree_int_cst_lt (last, first)) | |
7215 | { | |
7216 | error_init ("empty index range in initializer"); | |
7217 | last = 0; | |
7218 | } | |
7219 | else | |
7220 | { | |
7221 | last = convert (bitsizetype, last); | |
7222 | if (constructor_max_index != 0 | |
7223 | && tree_int_cst_lt (constructor_max_index, last)) | |
7224 | { | |
7225 | error_init ("array index range in initializer exceeds array bounds"); | |
7226 | last = 0; | |
7227 | } | |
7228 | } | |
2bede729 | 7229 | } |
fed3cef0 | 7230 | |
3e4093b6 | 7231 | designator_depth++; |
b06df647 | 7232 | designator_erroneous = 0; |
3e4093b6 | 7233 | if (constructor_range_stack || last) |
a1e3b3d9 | 7234 | push_range_stack (last, braced_init_obstack); |
de520661 | 7235 | } |
de520661 | 7236 | } |
3e4093b6 RS |
7237 | |
7238 | /* Within a struct initializer, specify the next field to be initialized. */ | |
400fbf9f | 7239 | |
de520661 | 7240 | void |
a1e3b3d9 | 7241 | set_init_label (tree fieldname, struct obstack * braced_init_obstack) |
de520661 | 7242 | { |
0fb96aa4 | 7243 | tree field; |
94ba5069 | 7244 | |
a1e3b3d9 | 7245 | if (set_designator (0, braced_init_obstack)) |
3e4093b6 RS |
7246 | return; |
7247 | ||
b06df647 | 7248 | designator_erroneous = 1; |
3e4093b6 RS |
7249 | |
7250 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7251 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
94ba5069 | 7252 | { |
3e4093b6 RS |
7253 | error_init ("field name not in record or union initializer"); |
7254 | return; | |
94ba5069 RS |
7255 | } |
7256 | ||
0fb96aa4 | 7257 | field = lookup_field (constructor_type, fieldname); |
8b6a5902 | 7258 | |
0fb96aa4 | 7259 | if (field == 0) |
c51a1ba9 | 7260 | error ("unknown field %qE specified in initializer", fieldname); |
3e4093b6 | 7261 | else |
0fb96aa4 JM |
7262 | do |
7263 | { | |
7264 | constructor_fields = TREE_VALUE (field); | |
7265 | designator_depth++; | |
7266 | designator_erroneous = 0; | |
7267 | if (constructor_range_stack) | |
7268 | push_range_stack (NULL_TREE, braced_init_obstack); | |
7269 | field = TREE_CHAIN (field); | |
7270 | if (field) | |
7271 | { | |
7272 | if (set_designator (0, braced_init_obstack)) | |
7273 | return; | |
7274 | } | |
7275 | } | |
7276 | while (field != NULL_TREE); | |
3e4093b6 RS |
7277 | } |
7278 | \f | |
7279 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
7280 | identifies the initializer, either array index or field in a structure. | |
bbbbb16a ILT |
7281 | VALUE is the value of that index or field. If ORIGTYPE is not |
7282 | NULL_TREE, it is the original type of VALUE. | |
b295aee2 JJ |
7283 | |
7284 | IMPLICIT is true if value comes from pop_init_level (1), | |
7285 | the new initializer has been merged with the existing one | |
7286 | and thus no warnings should be emitted about overriding an | |
7287 | existing initializer. */ | |
de520661 | 7288 | |
3e4093b6 | 7289 | static void |
a1e3b3d9 LB |
7290 | add_pending_init (tree purpose, tree value, tree origtype, bool implicit, |
7291 | struct obstack * braced_init_obstack) | |
3e4093b6 RS |
7292 | { |
7293 | struct init_node *p, **q, *r; | |
7294 | ||
7295 | q = &constructor_pending_elts; | |
7296 | p = 0; | |
7297 | ||
7298 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 7299 | { |
3e4093b6 | 7300 | while (*q != 0) |
91fa3c30 | 7301 | { |
3e4093b6 RS |
7302 | p = *q; |
7303 | if (tree_int_cst_lt (purpose, p->purpose)) | |
7304 | q = &p->left; | |
7305 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
7306 | q = &p->right; | |
7307 | else | |
7308 | { | |
b295aee2 JJ |
7309 | if (!implicit) |
7310 | { | |
7311 | if (TREE_SIDE_EFFECTS (p->value)) | |
7312 | warning_init (0, "initialized field with side-effects overwritten"); | |
7313 | else if (warn_override_init) | |
7314 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7315 | } | |
3e4093b6 | 7316 | p->value = value; |
bbbbb16a | 7317 | p->origtype = origtype; |
3e4093b6 RS |
7318 | return; |
7319 | } | |
91fa3c30 | 7320 | } |
de520661 | 7321 | } |
3e4093b6 | 7322 | else |
de520661 | 7323 | { |
3e4093b6 | 7324 | tree bitpos; |
400fbf9f | 7325 | |
3e4093b6 RS |
7326 | bitpos = bit_position (purpose); |
7327 | while (*q != NULL) | |
7328 | { | |
7329 | p = *q; | |
7330 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7331 | q = &p->left; | |
7332 | else if (p->purpose != purpose) | |
7333 | q = &p->right; | |
7334 | else | |
7335 | { | |
b295aee2 JJ |
7336 | if (!implicit) |
7337 | { | |
7338 | if (TREE_SIDE_EFFECTS (p->value)) | |
7339 | warning_init (0, "initialized field with side-effects overwritten"); | |
7340 | else if (warn_override_init) | |
7341 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7342 | } | |
3e4093b6 | 7343 | p->value = value; |
bbbbb16a | 7344 | p->origtype = origtype; |
3e4093b6 RS |
7345 | return; |
7346 | } | |
7347 | } | |
91fa3c30 | 7348 | } |
b71c7f8a | 7349 | |
a1e3b3d9 LB |
7350 | r = (struct init_node *) obstack_alloc (braced_init_obstack, |
7351 | sizeof (struct init_node)); | |
3e4093b6 RS |
7352 | r->purpose = purpose; |
7353 | r->value = value; | |
bbbbb16a | 7354 | r->origtype = origtype; |
8b6a5902 | 7355 | |
3e4093b6 RS |
7356 | *q = r; |
7357 | r->parent = p; | |
7358 | r->left = 0; | |
7359 | r->right = 0; | |
7360 | r->balance = 0; | |
b71c7f8a | 7361 | |
3e4093b6 | 7362 | while (p) |
de520661 | 7363 | { |
3e4093b6 | 7364 | struct init_node *s; |
665f2503 | 7365 | |
3e4093b6 | 7366 | if (r == p->left) |
2bede729 | 7367 | { |
3e4093b6 RS |
7368 | if (p->balance == 0) |
7369 | p->balance = -1; | |
7370 | else if (p->balance < 0) | |
7371 | { | |
7372 | if (r->balance < 0) | |
7373 | { | |
7374 | /* L rotation. */ | |
7375 | p->left = r->right; | |
7376 | if (p->left) | |
7377 | p->left->parent = p; | |
7378 | r->right = p; | |
e7b6a0ee | 7379 | |
3e4093b6 RS |
7380 | p->balance = 0; |
7381 | r->balance = 0; | |
39bc99c2 | 7382 | |
3e4093b6 RS |
7383 | s = p->parent; |
7384 | p->parent = r; | |
7385 | r->parent = s; | |
7386 | if (s) | |
7387 | { | |
7388 | if (s->left == p) | |
7389 | s->left = r; | |
7390 | else | |
7391 | s->right = r; | |
7392 | } | |
7393 | else | |
7394 | constructor_pending_elts = r; | |
7395 | } | |
7396 | else | |
7397 | { | |
7398 | /* LR rotation. */ | |
7399 | struct init_node *t = r->right; | |
e7b6a0ee | 7400 | |
3e4093b6 RS |
7401 | r->right = t->left; |
7402 | if (r->right) | |
7403 | r->right->parent = r; | |
7404 | t->left = r; | |
7405 | ||
7406 | p->left = t->right; | |
7407 | if (p->left) | |
7408 | p->left->parent = p; | |
7409 | t->right = p; | |
7410 | ||
7411 | p->balance = t->balance < 0; | |
7412 | r->balance = -(t->balance > 0); | |
7413 | t->balance = 0; | |
7414 | ||
7415 | s = p->parent; | |
7416 | p->parent = t; | |
7417 | r->parent = t; | |
7418 | t->parent = s; | |
7419 | if (s) | |
7420 | { | |
7421 | if (s->left == p) | |
7422 | s->left = t; | |
7423 | else | |
7424 | s->right = t; | |
7425 | } | |
7426 | else | |
7427 | constructor_pending_elts = t; | |
7428 | } | |
7429 | break; | |
7430 | } | |
7431 | else | |
7432 | { | |
7433 | /* p->balance == +1; growth of left side balances the node. */ | |
7434 | p->balance = 0; | |
7435 | break; | |
7436 | } | |
2bede729 | 7437 | } |
3e4093b6 RS |
7438 | else /* r == p->right */ |
7439 | { | |
7440 | if (p->balance == 0) | |
7441 | /* Growth propagation from right side. */ | |
7442 | p->balance++; | |
7443 | else if (p->balance > 0) | |
7444 | { | |
7445 | if (r->balance > 0) | |
7446 | { | |
7447 | /* R rotation. */ | |
7448 | p->right = r->left; | |
7449 | if (p->right) | |
7450 | p->right->parent = p; | |
7451 | r->left = p; | |
7452 | ||
7453 | p->balance = 0; | |
7454 | r->balance = 0; | |
7455 | ||
7456 | s = p->parent; | |
7457 | p->parent = r; | |
7458 | r->parent = s; | |
7459 | if (s) | |
7460 | { | |
7461 | if (s->left == p) | |
7462 | s->left = r; | |
7463 | else | |
7464 | s->right = r; | |
7465 | } | |
7466 | else | |
7467 | constructor_pending_elts = r; | |
7468 | } | |
7469 | else /* r->balance == -1 */ | |
7470 | { | |
7471 | /* RL rotation */ | |
7472 | struct init_node *t = r->left; | |
7473 | ||
7474 | r->left = t->right; | |
7475 | if (r->left) | |
7476 | r->left->parent = r; | |
7477 | t->right = r; | |
7478 | ||
7479 | p->right = t->left; | |
7480 | if (p->right) | |
7481 | p->right->parent = p; | |
7482 | t->left = p; | |
7483 | ||
7484 | r->balance = (t->balance < 0); | |
7485 | p->balance = -(t->balance > 0); | |
7486 | t->balance = 0; | |
7487 | ||
7488 | s = p->parent; | |
7489 | p->parent = t; | |
7490 | r->parent = t; | |
7491 | t->parent = s; | |
7492 | if (s) | |
7493 | { | |
7494 | if (s->left == p) | |
7495 | s->left = t; | |
7496 | else | |
7497 | s->right = t; | |
7498 | } | |
7499 | else | |
7500 | constructor_pending_elts = t; | |
7501 | } | |
7502 | break; | |
7503 | } | |
7504 | else | |
7505 | { | |
7506 | /* p->balance == -1; growth of right side balances the node. */ | |
7507 | p->balance = 0; | |
7508 | break; | |
7509 | } | |
7510 | } | |
7511 | ||
7512 | r = p; | |
7513 | p = p->parent; | |
7514 | } | |
7515 | } | |
7516 | ||
7517 | /* Build AVL tree from a sorted chain. */ | |
7518 | ||
7519 | static void | |
a1e3b3d9 | 7520 | set_nonincremental_init (struct obstack * braced_init_obstack) |
3e4093b6 | 7521 | { |
4038c495 GB |
7522 | unsigned HOST_WIDE_INT ix; |
7523 | tree index, value; | |
3e4093b6 RS |
7524 | |
7525 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7526 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7527 | return; | |
7528 | ||
4038c495 | 7529 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) |
a1e3b3d9 | 7530 | { |
fdce1719 | 7531 | add_pending_init (index, value, NULL_TREE, true, |
a1e3b3d9 LB |
7532 | braced_init_obstack); |
7533 | } | |
9771b263 | 7534 | constructor_elements = NULL; |
3e4093b6 RS |
7535 | if (TREE_CODE (constructor_type) == RECORD_TYPE) |
7536 | { | |
7537 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
7538 | /* Skip any nameless bit fields at the beginning. */ | |
7539 | while (constructor_unfilled_fields != 0 | |
7540 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7541 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7542 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
fed3cef0 | 7543 | |
de520661 | 7544 | } |
3e4093b6 | 7545 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 7546 | { |
3e4093b6 RS |
7547 | if (TYPE_DOMAIN (constructor_type)) |
7548 | constructor_unfilled_index | |
7549 | = convert (bitsizetype, | |
7550 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7551 | else | |
7552 | constructor_unfilled_index = bitsize_zero_node; | |
de520661 | 7553 | } |
3e4093b6 | 7554 | constructor_incremental = 0; |
de520661 | 7555 | } |
400fbf9f | 7556 | |
3e4093b6 | 7557 | /* Build AVL tree from a string constant. */ |
de520661 | 7558 | |
3e4093b6 | 7559 | static void |
a1e3b3d9 LB |
7560 | set_nonincremental_init_from_string (tree str, |
7561 | struct obstack * braced_init_obstack) | |
de520661 | 7562 | { |
3e4093b6 RS |
7563 | tree value, purpose, type; |
7564 | HOST_WIDE_INT val[2]; | |
7565 | const char *p, *end; | |
7566 | int byte, wchar_bytes, charwidth, bitpos; | |
de520661 | 7567 | |
366de0ce | 7568 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); |
940ff66d | 7569 | |
c466b2cd | 7570 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; |
3e4093b6 RS |
7571 | charwidth = TYPE_PRECISION (char_type_node); |
7572 | type = TREE_TYPE (constructor_type); | |
7573 | p = TREE_STRING_POINTER (str); | |
7574 | end = p + TREE_STRING_LENGTH (str); | |
91fa3c30 | 7575 | |
3e4093b6 RS |
7576 | for (purpose = bitsize_zero_node; |
7577 | p < end && !tree_int_cst_lt (constructor_max_index, purpose); | |
7578 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) | |
584ef5fe | 7579 | { |
3e4093b6 | 7580 | if (wchar_bytes == 1) |
ffc5c6a9 | 7581 | { |
3e4093b6 RS |
7582 | val[1] = (unsigned char) *p++; |
7583 | val[0] = 0; | |
ffc5c6a9 RH |
7584 | } |
7585 | else | |
3e4093b6 RS |
7586 | { |
7587 | val[0] = 0; | |
7588 | val[1] = 0; | |
7589 | for (byte = 0; byte < wchar_bytes; byte++) | |
7590 | { | |
7591 | if (BYTES_BIG_ENDIAN) | |
7592 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
7593 | else | |
7594 | bitpos = byte * charwidth; | |
7595 | val[bitpos < HOST_BITS_PER_WIDE_INT] | |
7596 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) | |
7597 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
7598 | } | |
7599 | } | |
584ef5fe | 7600 | |
8df83eae | 7601 | if (!TYPE_UNSIGNED (type)) |
3e4093b6 RS |
7602 | { |
7603 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
7604 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
7605 | { | |
7606 | if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) | |
7607 | { | |
7608 | val[1] |= ((HOST_WIDE_INT) -1) << bitpos; | |
7609 | val[0] = -1; | |
7610 | } | |
7611 | } | |
7612 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
7613 | { | |
7614 | if (val[1] < 0) | |
c22cacf3 | 7615 | val[0] = -1; |
3e4093b6 RS |
7616 | } |
7617 | else if (val[0] & (((HOST_WIDE_INT) 1) | |
7618 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) | |
7619 | val[0] |= ((HOST_WIDE_INT) -1) | |
7620 | << (bitpos - HOST_BITS_PER_WIDE_INT); | |
7621 | } | |
ffc5c6a9 | 7622 | |
7d60be94 | 7623 | value = build_int_cst_wide (type, val[1], val[0]); |
fdce1719 | 7624 | add_pending_init (purpose, value, NULL_TREE, true, |
a1e3b3d9 | 7625 | braced_init_obstack); |
9dfcc8db BH |
7626 | } |
7627 | ||
3e4093b6 RS |
7628 | constructor_incremental = 0; |
7629 | } | |
de520661 | 7630 | |
3e4093b6 RS |
7631 | /* Return value of FIELD in pending initializer or zero if the field was |
7632 | not initialized yet. */ | |
7633 | ||
7634 | static tree | |
a1e3b3d9 | 7635 | find_init_member (tree field, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7636 | { |
7637 | struct init_node *p; | |
7638 | ||
7639 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
19d76e60 | 7640 | { |
3e4093b6 RS |
7641 | if (constructor_incremental |
7642 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 7643 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
7644 | |
7645 | p = constructor_pending_elts; | |
7646 | while (p) | |
19d76e60 | 7647 | { |
3e4093b6 RS |
7648 | if (tree_int_cst_lt (field, p->purpose)) |
7649 | p = p->left; | |
7650 | else if (tree_int_cst_lt (p->purpose, field)) | |
7651 | p = p->right; | |
7652 | else | |
7653 | return p->value; | |
19d76e60 | 7654 | } |
19d76e60 | 7655 | } |
3e4093b6 | 7656 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
de520661 | 7657 | { |
3e4093b6 | 7658 | tree bitpos = bit_position (field); |
de520661 | 7659 | |
3e4093b6 RS |
7660 | if (constructor_incremental |
7661 | && (!constructor_unfilled_fields | |
7662 | || tree_int_cst_lt (bitpos, | |
7663 | bit_position (constructor_unfilled_fields)))) | |
a1e3b3d9 | 7664 | set_nonincremental_init (braced_init_obstack); |
de520661 | 7665 | |
3e4093b6 RS |
7666 | p = constructor_pending_elts; |
7667 | while (p) | |
7668 | { | |
7669 | if (field == p->purpose) | |
7670 | return p->value; | |
7671 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7672 | p = p->left; | |
7673 | else | |
7674 | p = p->right; | |
7675 | } | |
7676 | } | |
7677 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 7678 | { |
9771b263 DN |
7679 | if (!vec_safe_is_empty (constructor_elements) |
7680 | && (constructor_elements->last ().index == field)) | |
7681 | return constructor_elements->last ().value; | |
de520661 | 7682 | } |
3e4093b6 | 7683 | return 0; |
de520661 RS |
7684 | } |
7685 | ||
3e4093b6 RS |
7686 | /* "Output" the next constructor element. |
7687 | At top level, really output it to assembler code now. | |
7688 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
bbbbb16a | 7689 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. |
3e4093b6 RS |
7690 | TYPE is the data type that the containing data type wants here. |
7691 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
916c5919 JM |
7692 | If VALUE is a string constant, STRICT_STRING is true if it is |
7693 | unparenthesized or we should not warn here for it being parenthesized. | |
7694 | For other types of VALUE, STRICT_STRING is not used. | |
8b6a5902 | 7695 | |
3e4093b6 RS |
7696 | PENDING if non-nil means output pending elements that belong |
7697 | right after this element. (PENDING is normally 1; | |
b295aee2 JJ |
7698 | it is 0 while outputting pending elements, to avoid recursion.) |
7699 | ||
7700 | IMPLICIT is true if value comes from pop_init_level (1), | |
7701 | the new initializer has been merged with the existing one | |
7702 | and thus no warnings should be emitted about overriding an | |
7703 | existing initializer. */ | |
8b6a5902 | 7704 | |
3e4093b6 | 7705 | static void |
bbbbb16a | 7706 | output_init_element (tree value, tree origtype, bool strict_string, tree type, |
a1e3b3d9 LB |
7707 | tree field, int pending, bool implicit, |
7708 | struct obstack * braced_init_obstack) | |
3e4093b6 | 7709 | { |
8ce94e44 | 7710 | tree semantic_type = NULL_TREE; |
928c19bb JM |
7711 | bool maybe_const = true; |
7712 | bool npc; | |
4038c495 | 7713 | |
0a880880 | 7714 | if (type == error_mark_node || value == error_mark_node) |
8b6a5902 | 7715 | { |
3e4093b6 RS |
7716 | constructor_erroneous = 1; |
7717 | return; | |
8b6a5902 | 7718 | } |
46bdb9cf JM |
7719 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
7720 | && (TREE_CODE (value) == STRING_CST | |
7721 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
7722 | && !(TREE_CODE (value) == STRING_CST | |
7723 | && TREE_CODE (type) == ARRAY_TYPE | |
7724 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
7725 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
7726 | TYPE_MAIN_VARIANT (type))) | |
c2255bc4 | 7727 | value = array_to_pointer_conversion (input_location, value); |
8b6a5902 | 7728 | |
3e4093b6 RS |
7729 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
7730 | && require_constant_value && !flag_isoc99 && pending) | |
8b6a5902 | 7731 | { |
3e4093b6 RS |
7732 | /* As an extension, allow initializing objects with static storage |
7733 | duration with compound literals (which are then treated just as | |
7734 | the brace enclosed list they contain). */ | |
7735 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); | |
7736 | value = DECL_INITIAL (decl); | |
8b6a5902 JJ |
7737 | } |
7738 | ||
928c19bb | 7739 | npc = null_pointer_constant_p (value); |
8ce94e44 JM |
7740 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) |
7741 | { | |
7742 | semantic_type = TREE_TYPE (value); | |
7743 | value = TREE_OPERAND (value, 0); | |
7744 | } | |
928c19bb JM |
7745 | value = c_fully_fold (value, require_constant_value, &maybe_const); |
7746 | ||
3e4093b6 RS |
7747 | if (value == error_mark_node) |
7748 | constructor_erroneous = 1; | |
7749 | else if (!TREE_CONSTANT (value)) | |
7750 | constructor_constant = 0; | |
116df786 | 7751 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) |
3e4093b6 RS |
7752 | || ((TREE_CODE (constructor_type) == RECORD_TYPE |
7753 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7754 | && DECL_C_BIT_FIELD (field) | |
7755 | && TREE_CODE (value) != INTEGER_CST)) | |
7756 | constructor_simple = 0; | |
928c19bb JM |
7757 | if (!maybe_const) |
7758 | constructor_nonconst = 1; | |
3e4093b6 | 7759 | |
116df786 | 7760 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) |
8b6a5902 | 7761 | { |
116df786 RH |
7762 | if (require_constant_value) |
7763 | { | |
7764 | error_init ("initializer element is not constant"); | |
7765 | value = error_mark_node; | |
7766 | } | |
7767 | else if (require_constant_elements) | |
509c9d60 MLI |
7768 | pedwarn (input_location, 0, |
7769 | "initializer element is not computable at load time"); | |
8b6a5902 | 7770 | } |
928c19bb JM |
7771 | else if (!maybe_const |
7772 | && (require_constant_value || require_constant_elements)) | |
7773 | pedwarn_init (input_location, 0, | |
7774 | "initializer element is not a constant expression"); | |
3e4093b6 | 7775 | |
81f40b79 ILT |
7776 | /* Issue -Wc++-compat warnings about initializing a bitfield with |
7777 | enum type. */ | |
7778 | if (warn_cxx_compat | |
7779 | && field != NULL_TREE | |
7780 | && TREE_CODE (field) == FIELD_DECL | |
7781 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
7782 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
7783 | != TYPE_MAIN_VARIANT (type)) | |
7784 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
7785 | { | |
7786 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
7787 | if (checktype != error_mark_node | |
7788 | && (TYPE_MAIN_VARIANT (checktype) | |
7789 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
7790 | warning_init (OPT_Wc___compat, | |
7791 | "enum conversion in initialization is invalid in C++"); | |
7792 | } | |
7793 | ||
3e4093b6 RS |
7794 | /* If this field is empty (and not at the end of structure), |
7795 | don't do anything other than checking the initializer. */ | |
7796 | if (field | |
7797 | && (TREE_TYPE (field) == error_mark_node | |
7798 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
7799 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
7800 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
910ad8de | 7801 | || DECL_CHAIN (field))))) |
3e4093b6 RS |
7802 | return; |
7803 | ||
8ce94e44 JM |
7804 | if (semantic_type) |
7805 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
c2255bc4 AH |
7806 | value = digest_init (input_location, type, value, origtype, npc, |
7807 | strict_string, require_constant_value); | |
3e4093b6 | 7808 | if (value == error_mark_node) |
8b6a5902 | 7809 | { |
3e4093b6 RS |
7810 | constructor_erroneous = 1; |
7811 | return; | |
8b6a5902 | 7812 | } |
928c19bb JM |
7813 | if (require_constant_value || require_constant_elements) |
7814 | constant_expression_warning (value); | |
8b6a5902 | 7815 | |
3e4093b6 RS |
7816 | /* If this element doesn't come next in sequence, |
7817 | put it on constructor_pending_elts. */ | |
7818 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
7819 | && (!constructor_incremental | |
7820 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
8b6a5902 | 7821 | { |
3e4093b6 RS |
7822 | if (constructor_incremental |
7823 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
a1e3b3d9 | 7824 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 | 7825 | |
a1e3b3d9 LB |
7826 | add_pending_init (field, value, origtype, implicit, |
7827 | braced_init_obstack); | |
3e4093b6 | 7828 | return; |
8b6a5902 | 7829 | } |
3e4093b6 RS |
7830 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
7831 | && (!constructor_incremental | |
7832 | || field != constructor_unfilled_fields)) | |
8b6a5902 | 7833 | { |
3e4093b6 RS |
7834 | /* We do this for records but not for unions. In a union, |
7835 | no matter which field is specified, it can be initialized | |
7836 | right away since it starts at the beginning of the union. */ | |
7837 | if (constructor_incremental) | |
7838 | { | |
7839 | if (!constructor_unfilled_fields) | |
a1e3b3d9 | 7840 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
7841 | else |
7842 | { | |
7843 | tree bitpos, unfillpos; | |
7844 | ||
7845 | bitpos = bit_position (field); | |
7846 | unfillpos = bit_position (constructor_unfilled_fields); | |
7847 | ||
7848 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
a1e3b3d9 | 7849 | set_nonincremental_init (braced_init_obstack); |
3e4093b6 RS |
7850 | } |
7851 | } | |
7852 | ||
a1e3b3d9 LB |
7853 | add_pending_init (field, value, origtype, implicit, |
7854 | braced_init_obstack); | |
3e4093b6 | 7855 | return; |
8b6a5902 | 7856 | } |
3e4093b6 | 7857 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
9771b263 | 7858 | && !vec_safe_is_empty (constructor_elements)) |
3e4093b6 | 7859 | { |
b295aee2 JJ |
7860 | if (!implicit) |
7861 | { | |
9771b263 | 7862 | if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) |
b295aee2 JJ |
7863 | warning_init (0, |
7864 | "initialized field with side-effects overwritten"); | |
7865 | else if (warn_override_init) | |
7866 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7867 | } | |
8b6a5902 | 7868 | |
3e4093b6 | 7869 | /* We can have just one union field set. */ |
9771b263 | 7870 | constructor_elements = NULL; |
3e4093b6 | 7871 | } |
8b6a5902 | 7872 | |
3e4093b6 RS |
7873 | /* Otherwise, output this element either to |
7874 | constructor_elements or to the assembler file. */ | |
8b6a5902 | 7875 | |
f32682ca | 7876 | constructor_elt celt = {field, value}; |
9771b263 | 7877 | vec_safe_push (constructor_elements, celt); |
8b6a5902 | 7878 | |
3e4093b6 RS |
7879 | /* Advance the variable that indicates sequential elements output. */ |
7880 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7881 | constructor_unfilled_index | |
db3927fb AH |
7882 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, |
7883 | bitsize_one_node); | |
3e4093b6 RS |
7884 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
7885 | { | |
7886 | constructor_unfilled_fields | |
910ad8de | 7887 | = DECL_CHAIN (constructor_unfilled_fields); |
8b6a5902 | 7888 | |
3e4093b6 RS |
7889 | /* Skip any nameless bit fields. */ |
7890 | while (constructor_unfilled_fields != 0 | |
7891 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7892 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7893 | constructor_unfilled_fields = | |
910ad8de | 7894 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 RS |
7895 | } |
7896 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
7897 | constructor_unfilled_fields = 0; | |
de520661 | 7898 | |
3e4093b6 RS |
7899 | /* Now output any pending elements which have become next. */ |
7900 | if (pending) | |
a1e3b3d9 | 7901 | output_pending_init_elements (0, braced_init_obstack); |
3e4093b6 | 7902 | } |
8b6a5902 | 7903 | |
3e4093b6 RS |
7904 | /* Output any pending elements which have become next. |
7905 | As we output elements, constructor_unfilled_{fields,index} | |
7906 | advances, which may cause other elements to become next; | |
7907 | if so, they too are output. | |
8b6a5902 | 7908 | |
3e4093b6 RS |
7909 | If ALL is 0, we return when there are |
7910 | no more pending elements to output now. | |
665f2503 | 7911 | |
3e4093b6 RS |
7912 | If ALL is 1, we output space as necessary so that |
7913 | we can output all the pending elements. */ | |
3e4093b6 | 7914 | static void |
a1e3b3d9 | 7915 | output_pending_init_elements (int all, struct obstack * braced_init_obstack) |
3e4093b6 RS |
7916 | { |
7917 | struct init_node *elt = constructor_pending_elts; | |
7918 | tree next; | |
de520661 | 7919 | |
3e4093b6 RS |
7920 | retry: |
7921 | ||
ba228239 | 7922 | /* Look through the whole pending tree. |
3e4093b6 RS |
7923 | If we find an element that should be output now, |
7924 | output it. Otherwise, set NEXT to the element | |
7925 | that comes first among those still pending. */ | |
7926 | ||
7927 | next = 0; | |
7928 | while (elt) | |
7929 | { | |
7930 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8b6a5902 | 7931 | { |
3e4093b6 RS |
7932 | if (tree_int_cst_equal (elt->purpose, |
7933 | constructor_unfilled_index)) | |
bbbbb16a | 7934 | output_init_element (elt->value, elt->origtype, true, |
3e4093b6 | 7935 | TREE_TYPE (constructor_type), |
a1e3b3d9 LB |
7936 | constructor_unfilled_index, 0, false, |
7937 | braced_init_obstack); | |
3e4093b6 RS |
7938 | else if (tree_int_cst_lt (constructor_unfilled_index, |
7939 | elt->purpose)) | |
8b6a5902 | 7940 | { |
3e4093b6 RS |
7941 | /* Advance to the next smaller node. */ |
7942 | if (elt->left) | |
7943 | elt = elt->left; | |
7944 | else | |
7945 | { | |
7946 | /* We have reached the smallest node bigger than the | |
7947 | current unfilled index. Fill the space first. */ | |
7948 | next = elt->purpose; | |
7949 | break; | |
7950 | } | |
8b6a5902 | 7951 | } |
ce662d4c JJ |
7952 | else |
7953 | { | |
3e4093b6 RS |
7954 | /* Advance to the next bigger node. */ |
7955 | if (elt->right) | |
7956 | elt = elt->right; | |
7957 | else | |
ce662d4c | 7958 | { |
3e4093b6 RS |
7959 | /* We have reached the biggest node in a subtree. Find |
7960 | the parent of it, which is the next bigger node. */ | |
7961 | while (elt->parent && elt->parent->right == elt) | |
7962 | elt = elt->parent; | |
7963 | elt = elt->parent; | |
7964 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
7965 | elt->purpose)) | |
7966 | { | |
7967 | next = elt->purpose; | |
7968 | break; | |
7969 | } | |
ce662d4c JJ |
7970 | } |
7971 | } | |
8b6a5902 | 7972 | } |
3e4093b6 RS |
7973 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
7974 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7975 | { | |
7976 | tree ctor_unfilled_bitpos, elt_bitpos; | |
ce662d4c | 7977 | |
3e4093b6 RS |
7978 | /* If the current record is complete we are done. */ |
7979 | if (constructor_unfilled_fields == 0) | |
7980 | break; | |
de520661 | 7981 | |
3e4093b6 RS |
7982 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
7983 | elt_bitpos = bit_position (elt->purpose); | |
7984 | /* We can't compare fields here because there might be empty | |
7985 | fields in between. */ | |
7986 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
7987 | { | |
7988 | constructor_unfilled_fields = elt->purpose; | |
bbbbb16a ILT |
7989 | output_init_element (elt->value, elt->origtype, true, |
7990 | TREE_TYPE (elt->purpose), | |
a1e3b3d9 LB |
7991 | elt->purpose, 0, false, |
7992 | braced_init_obstack); | |
3e4093b6 RS |
7993 | } |
7994 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
7995 | { | |
7996 | /* Advance to the next smaller node. */ | |
7997 | if (elt->left) | |
7998 | elt = elt->left; | |
7999 | else | |
8000 | { | |
8001 | /* We have reached the smallest node bigger than the | |
8002 | current unfilled field. Fill the space first. */ | |
8003 | next = elt->purpose; | |
8004 | break; | |
8005 | } | |
8006 | } | |
8007 | else | |
8008 | { | |
8009 | /* Advance to the next bigger node. */ | |
8010 | if (elt->right) | |
8011 | elt = elt->right; | |
8012 | else | |
8013 | { | |
8014 | /* We have reached the biggest node in a subtree. Find | |
8015 | the parent of it, which is the next bigger node. */ | |
8016 | while (elt->parent && elt->parent->right == elt) | |
8017 | elt = elt->parent; | |
8018 | elt = elt->parent; | |
8019 | if (elt | |
8020 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
8021 | bit_position (elt->purpose)))) | |
8022 | { | |
8023 | next = elt->purpose; | |
8024 | break; | |
8025 | } | |
8026 | } | |
8027 | } | |
8028 | } | |
8029 | } | |
de520661 | 8030 | |
3e4093b6 RS |
8031 | /* Ordinarily return, but not if we want to output all |
8032 | and there are elements left. */ | |
3f75a254 | 8033 | if (!(all && next != 0)) |
e5cfb88f RK |
8034 | return; |
8035 | ||
3e4093b6 RS |
8036 | /* If it's not incremental, just skip over the gap, so that after |
8037 | jumping to retry we will output the next successive element. */ | |
8038 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
8039 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8040 | constructor_unfilled_fields = next; | |
8041 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8042 | constructor_unfilled_index = next; | |
de520661 | 8043 | |
3e4093b6 RS |
8044 | /* ELT now points to the node in the pending tree with the next |
8045 | initializer to output. */ | |
8046 | goto retry; | |
de520661 RS |
8047 | } |
8048 | \f | |
3e4093b6 RS |
8049 | /* Add one non-braced element to the current constructor level. |
8050 | This adjusts the current position within the constructor's type. | |
8051 | This may also start or terminate implicit levels | |
8052 | to handle a partly-braced initializer. | |
e5e809f4 | 8053 | |
3e4093b6 | 8054 | Once this has found the correct level for the new element, |
b295aee2 JJ |
8055 | it calls output_init_element. |
8056 | ||
8057 | IMPLICIT is true if value comes from pop_init_level (1), | |
8058 | the new initializer has been merged with the existing one | |
8059 | and thus no warnings should be emitted about overriding an | |
8060 | existing initializer. */ | |
3e4093b6 RS |
8061 | |
8062 | void | |
a1e3b3d9 LB |
8063 | process_init_element (struct c_expr value, bool implicit, |
8064 | struct obstack * braced_init_obstack) | |
e5e809f4 | 8065 | { |
916c5919 JM |
8066 | tree orig_value = value.value; |
8067 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
8068 | bool strict_string = value.original_code == STRING_CST; | |
e5e809f4 | 8069 | |
3e4093b6 | 8070 | designator_depth = 0; |
b06df647 | 8071 | designator_erroneous = 0; |
e5e809f4 | 8072 | |
3e4093b6 RS |
8073 | /* Handle superfluous braces around string cst as in |
8074 | char x[] = {"foo"}; */ | |
8075 | if (string_flag | |
8076 | && constructor_type | |
8077 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
197463ae | 8078 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
3e4093b6 | 8079 | && integer_zerop (constructor_unfilled_index)) |
e5e809f4 | 8080 | { |
916c5919 | 8081 | if (constructor_stack->replacement_value.value) |
c22cacf3 | 8082 | error_init ("excess elements in char array initializer"); |
3e4093b6 RS |
8083 | constructor_stack->replacement_value = value; |
8084 | return; | |
e5e809f4 | 8085 | } |
8b6a5902 | 8086 | |
916c5919 | 8087 | if (constructor_stack->replacement_value.value != 0) |
3e4093b6 RS |
8088 | { |
8089 | error_init ("excess elements in struct initializer"); | |
8090 | return; | |
e5e809f4 JL |
8091 | } |
8092 | ||
3e4093b6 RS |
8093 | /* Ignore elements of a brace group if it is entirely superfluous |
8094 | and has already been diagnosed. */ | |
8095 | if (constructor_type == 0) | |
8096 | return; | |
e5e809f4 | 8097 | |
3e4093b6 RS |
8098 | /* If we've exhausted any levels that didn't have braces, |
8099 | pop them now. */ | |
8100 | while (constructor_stack->implicit) | |
8101 | { | |
8102 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
8103 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8104 | && constructor_fields == 0) | |
a1e3b3d9 LB |
8105 | process_init_element (pop_init_level (1, braced_init_obstack), |
8106 | true, braced_init_obstack); | |
53650abe AP |
8107 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE |
8108 | || TREE_CODE (constructor_type) == VECTOR_TYPE) | |
3e4093b6 RS |
8109 | && (constructor_max_index == 0 |
8110 | || tree_int_cst_lt (constructor_max_index, | |
8111 | constructor_index))) | |
a1e3b3d9 LB |
8112 | process_init_element (pop_init_level (1, braced_init_obstack), |
8113 | true, braced_init_obstack); | |
3e4093b6 RS |
8114 | else |
8115 | break; | |
8116 | } | |
e5e809f4 | 8117 | |
3e4093b6 RS |
8118 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
8119 | if (constructor_range_stack) | |
e5e809f4 | 8120 | { |
3e4093b6 RS |
8121 | /* If value is a compound literal and we'll be just using its |
8122 | content, don't put it into a SAVE_EXPR. */ | |
916c5919 | 8123 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
3e4093b6 RS |
8124 | || !require_constant_value |
8125 | || flag_isoc99) | |
8ce94e44 JM |
8126 | { |
8127 | tree semantic_type = NULL_TREE; | |
8128 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
8129 | { | |
8130 | semantic_type = TREE_TYPE (value.value); | |
8131 | value.value = TREE_OPERAND (value.value, 0); | |
8132 | } | |
8133 | value.value = c_save_expr (value.value); | |
8134 | if (semantic_type) | |
8135 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
8136 | value.value); | |
8137 | } | |
3e4093b6 | 8138 | } |
e5e809f4 | 8139 | |
3e4093b6 RS |
8140 | while (1) |
8141 | { | |
8142 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
e5e809f4 | 8143 | { |
3e4093b6 RS |
8144 | tree fieldtype; |
8145 | enum tree_code fieldcode; | |
e5e809f4 | 8146 | |
3e4093b6 RS |
8147 | if (constructor_fields == 0) |
8148 | { | |
509c9d60 MLI |
8149 | pedwarn_init (input_location, 0, |
8150 | "excess elements in struct initializer"); | |
3e4093b6 RS |
8151 | break; |
8152 | } | |
e5e809f4 | 8153 | |
3e4093b6 RS |
8154 | fieldtype = TREE_TYPE (constructor_fields); |
8155 | if (fieldtype != error_mark_node) | |
8156 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8157 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8158 | |
3e4093b6 RS |
8159 | /* Error for non-static initialization of a flexible array member. */ |
8160 | if (fieldcode == ARRAY_TYPE | |
8161 | && !require_constant_value | |
8162 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
f7587ed0 | 8163 | && DECL_CHAIN (constructor_fields) == NULL_TREE) |
3e4093b6 RS |
8164 | { |
8165 | error_init ("non-static initialization of a flexible array member"); | |
8166 | break; | |
8167 | } | |
e5e809f4 | 8168 | |
3e4093b6 | 8169 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8170 | if (value.value != 0 |
3e4093b6 | 8171 | && fieldcode == ARRAY_TYPE |
197463ae | 8172 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8173 | && string_flag) |
916c5919 | 8174 | value.value = orig_value; |
3e4093b6 RS |
8175 | /* Otherwise, if we have come to a subaggregate, |
8176 | and we don't have an element of its type, push into it. */ | |
0953878d | 8177 | else if (value.value != 0 |
916c5919 JM |
8178 | && value.value != error_mark_node |
8179 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 8180 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 8181 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 8182 | { |
a1e3b3d9 | 8183 | push_init_level (1, braced_init_obstack); |
3e4093b6 RS |
8184 | continue; |
8185 | } | |
e5e809f4 | 8186 | |
916c5919 | 8187 | if (value.value) |
3e4093b6 RS |
8188 | { |
8189 | push_member_name (constructor_fields); | |
bbbbb16a ILT |
8190 | output_init_element (value.value, value.original_type, |
8191 | strict_string, fieldtype, | |
a1e3b3d9 LB |
8192 | constructor_fields, 1, implicit, |
8193 | braced_init_obstack); | |
3e4093b6 | 8194 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
8195 | } |
8196 | else | |
3e4093b6 RS |
8197 | /* Do the bookkeeping for an element that was |
8198 | directly output as a constructor. */ | |
e5e809f4 | 8199 | { |
3e4093b6 RS |
8200 | /* For a record, keep track of end position of last field. */ |
8201 | if (DECL_SIZE (constructor_fields)) | |
c22cacf3 | 8202 | constructor_bit_index |
db3927fb AH |
8203 | = size_binop_loc (input_location, PLUS_EXPR, |
8204 | bit_position (constructor_fields), | |
8205 | DECL_SIZE (constructor_fields)); | |
3e4093b6 RS |
8206 | |
8207 | /* If the current field was the first one not yet written out, | |
8208 | it isn't now, so update. */ | |
8209 | if (constructor_unfilled_fields == constructor_fields) | |
8210 | { | |
910ad8de | 8211 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8212 | /* Skip any nameless bit fields. */ |
8213 | while (constructor_unfilled_fields != 0 | |
8214 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8215 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8216 | constructor_unfilled_fields = | |
910ad8de | 8217 | DECL_CHAIN (constructor_unfilled_fields); |
3e4093b6 | 8218 | } |
e5e809f4 | 8219 | } |
3e4093b6 | 8220 | |
910ad8de | 8221 | constructor_fields = DECL_CHAIN (constructor_fields); |
3e4093b6 RS |
8222 | /* Skip any nameless bit fields at the beginning. */ |
8223 | while (constructor_fields != 0 | |
8224 | && DECL_C_BIT_FIELD (constructor_fields) | |
8225 | && DECL_NAME (constructor_fields) == 0) | |
910ad8de | 8226 | constructor_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 8227 | } |
3e4093b6 | 8228 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
e5e809f4 | 8229 | { |
3e4093b6 RS |
8230 | tree fieldtype; |
8231 | enum tree_code fieldcode; | |
e5e809f4 | 8232 | |
3e4093b6 RS |
8233 | if (constructor_fields == 0) |
8234 | { | |
509c9d60 MLI |
8235 | pedwarn_init (input_location, 0, |
8236 | "excess elements in union initializer"); | |
3e4093b6 RS |
8237 | break; |
8238 | } | |
e5e809f4 | 8239 | |
3e4093b6 RS |
8240 | fieldtype = TREE_TYPE (constructor_fields); |
8241 | if (fieldtype != error_mark_node) | |
8242 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8243 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 8244 | |
3e4093b6 RS |
8245 | /* Warn that traditional C rejects initialization of unions. |
8246 | We skip the warning if the value is zero. This is done | |
8247 | under the assumption that the zero initializer in user | |
8248 | code appears conditioned on e.g. __STDC__ to avoid | |
8249 | "missing initializer" warnings and relies on default | |
8250 | initialization to zero in the traditional C case. | |
8251 | We also skip the warning if the initializer is designated, | |
8252 | again on the assumption that this must be conditional on | |
8253 | __STDC__ anyway (and we've already complained about the | |
8254 | member-designator already). */ | |
3176a0c2 | 8255 | if (!in_system_header && !constructor_designated |
916c5919 JM |
8256 | && !(value.value && (integer_zerop (value.value) |
8257 | || real_zerop (value.value)))) | |
3176a0c2 DD |
8258 | warning (OPT_Wtraditional, "traditional C rejects initialization " |
8259 | "of unions"); | |
e5e809f4 | 8260 | |
3e4093b6 | 8261 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8262 | if (value.value != 0 |
3e4093b6 | 8263 | && fieldcode == ARRAY_TYPE |
197463ae | 8264 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 8265 | && string_flag) |
916c5919 | 8266 | value.value = orig_value; |
3e4093b6 RS |
8267 | /* Otherwise, if we have come to a subaggregate, |
8268 | and we don't have an element of its type, push into it. */ | |
0953878d | 8269 | else if (value.value != 0 |
916c5919 JM |
8270 | && value.value != error_mark_node |
8271 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 8272 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 8273 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 | 8274 | { |
a1e3b3d9 | 8275 | push_init_level (1, braced_init_obstack); |
3e4093b6 RS |
8276 | continue; |
8277 | } | |
e5e809f4 | 8278 | |
916c5919 | 8279 | if (value.value) |
3e4093b6 RS |
8280 | { |
8281 | push_member_name (constructor_fields); | |
bbbbb16a ILT |
8282 | output_init_element (value.value, value.original_type, |
8283 | strict_string, fieldtype, | |
a1e3b3d9 LB |
8284 | constructor_fields, 1, implicit, |
8285 | braced_init_obstack); | |
3e4093b6 | 8286 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
8287 | } |
8288 | else | |
3e4093b6 RS |
8289 | /* Do the bookkeeping for an element that was |
8290 | directly output as a constructor. */ | |
e5e809f4 | 8291 | { |
3e4093b6 | 8292 | constructor_bit_index = DECL_SIZE (constructor_fields); |
f7587ed0 | 8293 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); |
e5e809f4 | 8294 | } |
e5e809f4 | 8295 | |
3e4093b6 RS |
8296 | constructor_fields = 0; |
8297 | } | |
8298 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8299 | { | |
8300 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8301 | enum tree_code eltcode = TREE_CODE (elttype); | |
e5e809f4 | 8302 | |
3e4093b6 | 8303 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 8304 | if (value.value != 0 |
3e4093b6 | 8305 | && eltcode == ARRAY_TYPE |
197463ae | 8306 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
3e4093b6 | 8307 | && string_flag) |
916c5919 | 8308 | value.value = orig_value; |
3e4093b6 RS |
8309 | /* Otherwise, if we have come to a subaggregate, |
8310 | and we don't have an element of its type, push into it. */ | |
0953878d | 8311 | else if (value.value != 0 |
916c5919 JM |
8312 | && value.value != error_mark_node |
8313 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
3e4093b6 | 8314 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
53650abe | 8315 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) |
3e4093b6 | 8316 | { |
a1e3b3d9 | 8317 | push_init_level (1, braced_init_obstack); |
3e4093b6 RS |
8318 | continue; |
8319 | } | |
8b6a5902 | 8320 | |
3e4093b6 RS |
8321 | if (constructor_max_index != 0 |
8322 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
8323 | || integer_all_onesp (constructor_max_index))) | |
8324 | { | |
509c9d60 MLI |
8325 | pedwarn_init (input_location, 0, |
8326 | "excess elements in array initializer"); | |
3e4093b6 RS |
8327 | break; |
8328 | } | |
8b6a5902 | 8329 | |
3e4093b6 | 8330 | /* Now output the actual element. */ |
916c5919 | 8331 | if (value.value) |
3e4093b6 | 8332 | { |
a0f0ab9f | 8333 | push_array_bounds (tree_low_cst (constructor_index, 1)); |
bbbbb16a ILT |
8334 | output_init_element (value.value, value.original_type, |
8335 | strict_string, elttype, | |
a1e3b3d9 LB |
8336 | constructor_index, 1, implicit, |
8337 | braced_init_obstack); | |
3e4093b6 RS |
8338 | RESTORE_SPELLING_DEPTH (constructor_depth); |
8339 | } | |
2f6e4e97 | 8340 | |
3e4093b6 | 8341 | constructor_index |
db3927fb AH |
8342 | = size_binop_loc (input_location, PLUS_EXPR, |
8343 | constructor_index, bitsize_one_node); | |
8b6a5902 | 8344 | |
916c5919 | 8345 | if (!value.value) |
3e4093b6 RS |
8346 | /* If we are doing the bookkeeping for an element that was |
8347 | directly output as a constructor, we must update | |
8348 | constructor_unfilled_index. */ | |
8349 | constructor_unfilled_index = constructor_index; | |
8350 | } | |
8351 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8352 | { | |
8353 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8b6a5902 | 8354 | |
c22cacf3 MS |
8355 | /* Do a basic check of initializer size. Note that vectors |
8356 | always have a fixed size derived from their type. */ | |
3e4093b6 RS |
8357 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) |
8358 | { | |
509c9d60 MLI |
8359 | pedwarn_init (input_location, 0, |
8360 | "excess elements in vector initializer"); | |
3e4093b6 RS |
8361 | break; |
8362 | } | |
8b6a5902 | 8363 | |
3e4093b6 | 8364 | /* Now output the actual element. */ |
916c5919 | 8365 | if (value.value) |
53650abe AP |
8366 | { |
8367 | if (TREE_CODE (value.value) == VECTOR_CST) | |
8368 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
8369 | output_init_element (value.value, value.original_type, | |
8370 | strict_string, elttype, | |
a1e3b3d9 LB |
8371 | constructor_index, 1, implicit, |
8372 | braced_init_obstack); | |
53650abe | 8373 | } |
8b6a5902 | 8374 | |
3e4093b6 | 8375 | constructor_index |
db3927fb AH |
8376 | = size_binop_loc (input_location, |
8377 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 8378 | |
916c5919 | 8379 | if (!value.value) |
3e4093b6 RS |
8380 | /* If we are doing the bookkeeping for an element that was |
8381 | directly output as a constructor, we must update | |
8382 | constructor_unfilled_index. */ | |
8383 | constructor_unfilled_index = constructor_index; | |
8384 | } | |
8b6a5902 | 8385 | |
3e4093b6 RS |
8386 | /* Handle the sole element allowed in a braced initializer |
8387 | for a scalar variable. */ | |
b4519d39 SB |
8388 | else if (constructor_type != error_mark_node |
8389 | && constructor_fields == 0) | |
8b6a5902 | 8390 | { |
509c9d60 MLI |
8391 | pedwarn_init (input_location, 0, |
8392 | "excess elements in scalar initializer"); | |
3e4093b6 | 8393 | break; |
8b6a5902 JJ |
8394 | } |
8395 | else | |
8396 | { | |
916c5919 | 8397 | if (value.value) |
bbbbb16a ILT |
8398 | output_init_element (value.value, value.original_type, |
8399 | strict_string, constructor_type, | |
a1e3b3d9 LB |
8400 | NULL_TREE, 1, implicit, |
8401 | braced_init_obstack); | |
3e4093b6 | 8402 | constructor_fields = 0; |
8b6a5902 JJ |
8403 | } |
8404 | ||
3e4093b6 RS |
8405 | /* Handle range initializers either at this level or anywhere higher |
8406 | in the designator stack. */ | |
8407 | if (constructor_range_stack) | |
8b6a5902 | 8408 | { |
3e4093b6 RS |
8409 | struct constructor_range_stack *p, *range_stack; |
8410 | int finish = 0; | |
8411 | ||
8412 | range_stack = constructor_range_stack; | |
8413 | constructor_range_stack = 0; | |
8414 | while (constructor_stack != range_stack->stack) | |
8b6a5902 | 8415 | { |
366de0ce | 8416 | gcc_assert (constructor_stack->implicit); |
a1e3b3d9 LB |
8417 | process_init_element (pop_init_level (1, |
8418 | braced_init_obstack), | |
8419 | true, braced_init_obstack); | |
8b6a5902 | 8420 | } |
3e4093b6 RS |
8421 | for (p = range_stack; |
8422 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
8423 | p = p->prev) | |
8b6a5902 | 8424 | { |
366de0ce | 8425 | gcc_assert (constructor_stack->implicit); |
a1e3b3d9 LB |
8426 | process_init_element (pop_init_level (1, braced_init_obstack), |
8427 | true, braced_init_obstack); | |
8b6a5902 | 8428 | } |
3e4093b6 | 8429 | |
db3927fb AH |
8430 | p->index = size_binop_loc (input_location, |
8431 | PLUS_EXPR, p->index, bitsize_one_node); | |
3e4093b6 RS |
8432 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) |
8433 | finish = 1; | |
8434 | ||
8435 | while (1) | |
8436 | { | |
8437 | constructor_index = p->index; | |
8438 | constructor_fields = p->fields; | |
8439 | if (finish && p->range_end && p->index == p->range_start) | |
8440 | { | |
8441 | finish = 0; | |
8442 | p->prev = 0; | |
8443 | } | |
8444 | p = p->next; | |
8445 | if (!p) | |
8446 | break; | |
a1e3b3d9 | 8447 | push_init_level (2, braced_init_obstack); |
3e4093b6 RS |
8448 | p->stack = constructor_stack; |
8449 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
8450 | p->index = p->range_start; | |
8451 | } | |
8452 | ||
8453 | if (!finish) | |
8454 | constructor_range_stack = range_stack; | |
8455 | continue; | |
8b6a5902 JJ |
8456 | } |
8457 | ||
3e4093b6 | 8458 | break; |
8b6a5902 JJ |
8459 | } |
8460 | ||
3e4093b6 RS |
8461 | constructor_range_stack = 0; |
8462 | } | |
8463 | \f | |
9f0e2d86 ZW |
8464 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
8465 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
e130a54b | 8466 | an ASM_EXPR node). */ |
3e4093b6 | 8467 | tree |
9f0e2d86 | 8468 | build_asm_stmt (tree cv_qualifier, tree args) |
3e4093b6 | 8469 | { |
6de9cd9a DN |
8470 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
8471 | ASM_VOLATILE_P (args) = 1; | |
9f0e2d86 | 8472 | return add_stmt (args); |
8b6a5902 JJ |
8473 | } |
8474 | ||
9f0e2d86 ZW |
8475 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
8476 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
8477 | SIMPLE indicates whether there was anything at all after the | |
8478 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
e130a54b | 8479 | are subtly different. We use a ASM_EXPR node to represent this. */ |
3e4093b6 | 8480 | tree |
c2255bc4 | 8481 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, |
1c384bf1 | 8482 | tree clobbers, tree labels, bool simple) |
e5e809f4 | 8483 | { |
3e4093b6 | 8484 | tree tail; |
9f0e2d86 | 8485 | tree args; |
6de9cd9a DN |
8486 | int i; |
8487 | const char *constraint; | |
74f0c611 | 8488 | const char **oconstraints; |
6de9cd9a | 8489 | bool allows_mem, allows_reg, is_inout; |
74f0c611 | 8490 | int ninputs, noutputs; |
6de9cd9a DN |
8491 | |
8492 | ninputs = list_length (inputs); | |
8493 | noutputs = list_length (outputs); | |
74f0c611 RH |
8494 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); |
8495 | ||
1c384bf1 | 8496 | string = resolve_asm_operand_names (string, outputs, inputs, labels); |
3e4093b6 | 8497 | |
6de9cd9a DN |
8498 | /* Remove output conversions that change the type but not the mode. */ |
8499 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
e5e809f4 | 8500 | { |
3e4093b6 | 8501 | tree output = TREE_VALUE (tail); |
74f0c611 | 8502 | |
eadd3d0d JJ |
8503 | output = c_fully_fold (output, false, NULL); |
8504 | ||
74f0c611 RH |
8505 | /* ??? Really, this should not be here. Users should be using a |
8506 | proper lvalue, dammit. But there's a long history of using casts | |
8507 | in the output operands. In cases like longlong.h, this becomes a | |
8508 | primitive form of typechecking -- if the cast can be removed, then | |
8509 | the output operand had a type of the proper width; otherwise we'll | |
8510 | get an error. Gross, but ... */ | |
3e4093b6 | 8511 | STRIP_NOPS (output); |
74f0c611 | 8512 | |
7bd11157 | 8513 | if (!lvalue_or_else (loc, output, lv_asm)) |
74f0c611 | 8514 | output = error_mark_node; |
8b6a5902 | 8515 | |
5544530a PB |
8516 | if (output != error_mark_node |
8517 | && (TREE_READONLY (output) | |
8518 | || TYPE_READONLY (TREE_TYPE (output)) | |
8519 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
8520 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
8521 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
8522 | readonly_error (output, lv_asm); | |
8523 | ||
6de9cd9a | 8524 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
74f0c611 RH |
8525 | oconstraints[i] = constraint; |
8526 | ||
8527 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
8528 | &allows_mem, &allows_reg, &is_inout)) | |
8529 | { | |
8530 | /* If the operand is going to end up in memory, | |
8531 | mark it addressable. */ | |
8532 | if (!allows_reg && !c_mark_addressable (output)) | |
8533 | output = error_mark_node; | |
bae5cddf JJ |
8534 | if (!(!allows_reg && allows_mem) |
8535 | && output != error_mark_node | |
8536 | && VOID_TYPE_P (TREE_TYPE (output))) | |
8537 | { | |
8538 | error_at (loc, "invalid use of void expression"); | |
8539 | output = error_mark_node; | |
8540 | } | |
74f0c611 RH |
8541 | } |
8542 | else | |
c22cacf3 | 8543 | output = error_mark_node; |
3e4093b6 | 8544 | |
74f0c611 | 8545 | TREE_VALUE (tail) = output; |
8b6a5902 | 8546 | } |
3e4093b6 | 8547 | |
74f0c611 RH |
8548 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) |
8549 | { | |
8550 | tree input; | |
8551 | ||
8552 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8553 | input = TREE_VALUE (tail); | |
8554 | ||
74f0c611 RH |
8555 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
8556 | oconstraints, &allows_mem, &allows_reg)) | |
8557 | { | |
8558 | /* If the operand is going to end up in memory, | |
8559 | mark it addressable. */ | |
b4c33883 AP |
8560 | if (!allows_reg && allows_mem) |
8561 | { | |
eadd3d0d JJ |
8562 | input = c_fully_fold (input, false, NULL); |
8563 | ||
b4c33883 AP |
8564 | /* Strip the nops as we allow this case. FIXME, this really |
8565 | should be rejected or made deprecated. */ | |
8566 | STRIP_NOPS (input); | |
8567 | if (!c_mark_addressable (input)) | |
8568 | input = error_mark_node; | |
bae5cddf | 8569 | } |
eadd3d0d | 8570 | else |
bae5cddf | 8571 | { |
eadd3d0d JJ |
8572 | struct c_expr expr; |
8573 | memset (&expr, 0, sizeof (expr)); | |
8574 | expr.value = input; | |
8575 | expr = default_function_array_conversion (loc, expr); | |
8576 | input = c_fully_fold (expr.value, false, NULL); | |
8577 | ||
8578 | if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) | |
8579 | { | |
8580 | error_at (loc, "invalid use of void expression"); | |
8581 | input = error_mark_node; | |
8582 | } | |
bae5cddf | 8583 | } |
74f0c611 RH |
8584 | } |
8585 | else | |
8586 | input = error_mark_node; | |
8587 | ||
8588 | TREE_VALUE (tail) = input; | |
8589 | } | |
3e4093b6 | 8590 | |
1c384bf1 RH |
8591 | /* ASMs with labels cannot have outputs. This should have been |
8592 | enforced by the parser. */ | |
8593 | gcc_assert (outputs == NULL || labels == NULL); | |
8594 | ||
8595 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
9f0e2d86 | 8596 | |
5544530a PB |
8597 | /* asm statements without outputs, including simple ones, are treated |
8598 | as volatile. */ | |
8599 | ASM_INPUT_P (args) = simple; | |
8600 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
74f0c611 | 8601 | |
9f0e2d86 | 8602 | return args; |
e5e809f4 | 8603 | } |
3e4093b6 | 8604 | \f |
c2255bc4 AH |
8605 | /* Generate a goto statement to LABEL. LOC is the location of the |
8606 | GOTO. */ | |
506e2710 RH |
8607 | |
8608 | tree | |
c2255bc4 | 8609 | c_finish_goto_label (location_t loc, tree label) |
506e2710 | 8610 | { |
e1b7793c | 8611 | tree decl = lookup_label_for_goto (loc, label); |
506e2710 RH |
8612 | if (!decl) |
8613 | return NULL_TREE; | |
506e2710 | 8614 | TREE_USED (decl) = 1; |
c2255bc4 AH |
8615 | { |
8616 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
8617 | SET_EXPR_LOCATION (t, loc); | |
8618 | return add_stmt (t); | |
8619 | } | |
506e2710 RH |
8620 | } |
8621 | ||
c2255bc4 AH |
8622 | /* Generate a computed goto statement to EXPR. LOC is the location of |
8623 | the GOTO. */ | |
506e2710 RH |
8624 | |
8625 | tree | |
c2255bc4 | 8626 | c_finish_goto_ptr (location_t loc, tree expr) |
506e2710 | 8627 | { |
c2255bc4 | 8628 | tree t; |
c1771a20 | 8629 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); |
928c19bb | 8630 | expr = c_fully_fold (expr, false, NULL); |
506e2710 | 8631 | expr = convert (ptr_type_node, expr); |
c2255bc4 AH |
8632 | t = build1 (GOTO_EXPR, void_type_node, expr); |
8633 | SET_EXPR_LOCATION (t, loc); | |
8634 | return add_stmt (t); | |
506e2710 RH |
8635 | } |
8636 | ||
5088b058 | 8637 | /* Generate a C `return' statement. RETVAL is the expression for what |
c2255bc4 AH |
8638 | to return, or a null pointer for `return;' with no value. LOC is |
8639 | the location of the return statement. If ORIGTYPE is not NULL_TREE, it | |
8640 | is the original type of RETVAL. */ | |
de520661 | 8641 | |
506e2710 | 8642 | tree |
c2255bc4 | 8643 | c_finish_return (location_t loc, tree retval, tree origtype) |
3e4093b6 | 8644 | { |
0c9b182b JJ |
8645 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; |
8646 | bool no_warning = false; | |
928c19bb | 8647 | bool npc = false; |
3e4093b6 RS |
8648 | |
8649 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
c2255bc4 AH |
8650 | warning_at (loc, 0, |
8651 | "function declared %<noreturn%> has a %<return%> statement"); | |
3e4093b6 | 8652 | |
928c19bb JM |
8653 | if (retval) |
8654 | { | |
8ce94e44 | 8655 | tree semantic_type = NULL_TREE; |
928c19bb | 8656 | npc = null_pointer_constant_p (retval); |
8ce94e44 JM |
8657 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) |
8658 | { | |
8659 | semantic_type = TREE_TYPE (retval); | |
8660 | retval = TREE_OPERAND (retval, 0); | |
8661 | } | |
928c19bb | 8662 | retval = c_fully_fold (retval, false, NULL); |
8ce94e44 JM |
8663 | if (semantic_type) |
8664 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
928c19bb JM |
8665 | } |
8666 | ||
3e4093b6 | 8667 | if (!retval) |
de520661 | 8668 | { |
3e4093b6 RS |
8669 | current_function_returns_null = 1; |
8670 | if ((warn_return_type || flag_isoc99) | |
8671 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
0c9b182b | 8672 | { |
b8698a0f | 8673 | pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type, |
fcf73884 | 8674 | "%<return%> with no value, in " |
0c9b182b JJ |
8675 | "function returning non-void"); |
8676 | no_warning = true; | |
8677 | } | |
400fbf9f | 8678 | } |
3e4093b6 | 8679 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
de520661 | 8680 | { |
3e4093b6 | 8681 | current_function_returns_null = 1; |
2397c575 | 8682 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) |
b8698a0f | 8683 | pedwarn (loc, 0, |
509c9d60 | 8684 | "%<return%> with a value, in function returning void"); |
b8698a0f | 8685 | else |
c1771a20 | 8686 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 8687 | "%<return%> with expression, in function returning void"); |
de520661 | 8688 | } |
3e4093b6 | 8689 | else |
de520661 | 8690 | { |
c2255bc4 AH |
8691 | tree t = convert_for_assignment (loc, valtype, retval, origtype, |
8692 | ic_return, | |
8693 | npc, NULL_TREE, NULL_TREE, 0); | |
3e4093b6 RS |
8694 | tree res = DECL_RESULT (current_function_decl); |
8695 | tree inner; | |
9feb29df | 8696 | bool save; |
3e4093b6 RS |
8697 | |
8698 | current_function_returns_value = 1; | |
8699 | if (t == error_mark_node) | |
506e2710 | 8700 | return NULL_TREE; |
3e4093b6 | 8701 | |
9feb29df JJ |
8702 | save = in_late_binary_op; |
8703 | if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE | |
8704 | || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE) | |
8705 | in_late_binary_op = true; | |
3e4093b6 | 8706 | inner = t = convert (TREE_TYPE (res), t); |
9feb29df | 8707 | in_late_binary_op = save; |
3e4093b6 RS |
8708 | |
8709 | /* Strip any conversions, additions, and subtractions, and see if | |
8710 | we are returning the address of a local variable. Warn if so. */ | |
8711 | while (1) | |
8b6a5902 | 8712 | { |
3e4093b6 | 8713 | switch (TREE_CODE (inner)) |
8b6a5902 | 8714 | { |
849421a3 JJ |
8715 | CASE_CONVERT: |
8716 | case NON_LVALUE_EXPR: | |
3e4093b6 | 8717 | case PLUS_EXPR: |
849421a3 | 8718 | case POINTER_PLUS_EXPR: |
3e4093b6 RS |
8719 | inner = TREE_OPERAND (inner, 0); |
8720 | continue; | |
8721 | ||
8722 | case MINUS_EXPR: | |
8723 | /* If the second operand of the MINUS_EXPR has a pointer | |
8724 | type (or is converted from it), this may be valid, so | |
8725 | don't give a warning. */ | |
8726 | { | |
8727 | tree op1 = TREE_OPERAND (inner, 1); | |
8b6a5902 | 8728 | |
3f75a254 | 8729 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) |
1043771b TB |
8730 | && (CONVERT_EXPR_P (op1) |
8731 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
3e4093b6 | 8732 | op1 = TREE_OPERAND (op1, 0); |
8b6a5902 | 8733 | |
3e4093b6 RS |
8734 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
8735 | break; | |
8b6a5902 | 8736 | |
3e4093b6 RS |
8737 | inner = TREE_OPERAND (inner, 0); |
8738 | continue; | |
8739 | } | |
400fbf9f | 8740 | |
3e4093b6 RS |
8741 | case ADDR_EXPR: |
8742 | inner = TREE_OPERAND (inner, 0); | |
c2f4acb7 | 8743 | |
6615c446 | 8744 | while (REFERENCE_CLASS_P (inner) |
c22cacf3 | 8745 | && TREE_CODE (inner) != INDIRECT_REF) |
3e4093b6 | 8746 | inner = TREE_OPERAND (inner, 0); |
8b6a5902 | 8747 | |
a2f1f4c3 | 8748 | if (DECL_P (inner) |
3f75a254 JM |
8749 | && !DECL_EXTERNAL (inner) |
8750 | && !TREE_STATIC (inner) | |
3e4093b6 | 8751 | && DECL_CONTEXT (inner) == current_function_decl) |
c2255bc4 | 8752 | warning_at (loc, |
880661a4 JW |
8753 | OPT_Wreturn_local_addr, "function returns address " |
8754 | "of local variable"); | |
3e4093b6 | 8755 | break; |
8b6a5902 | 8756 | |
3e4093b6 RS |
8757 | default: |
8758 | break; | |
8759 | } | |
de520661 | 8760 | |
3e4093b6 RS |
8761 | break; |
8762 | } | |
8763 | ||
53fb4de3 | 8764 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
c2255bc4 | 8765 | SET_EXPR_LOCATION (retval, loc); |
ca085fd7 MLI |
8766 | |
8767 | if (warn_sequence_point) | |
8768 | verify_sequence_points (retval); | |
de520661 | 8769 | } |
8b6a5902 | 8770 | |
c2255bc4 | 8771 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); |
0c9b182b JJ |
8772 | TREE_NO_WARNING (ret_stmt) |= no_warning; |
8773 | return add_stmt (ret_stmt); | |
de520661 | 8774 | } |
3e4093b6 RS |
8775 | \f |
8776 | struct c_switch { | |
604f5adf ILT |
8777 | /* The SWITCH_EXPR being built. */ |
8778 | tree switch_expr; | |
a6c0a76c | 8779 | |
89dbed81 | 8780 | /* The original type of the testing expression, i.e. before the |
a6c0a76c SB |
8781 | default conversion is applied. */ |
8782 | tree orig_type; | |
8783 | ||
3e4093b6 RS |
8784 | /* A splay-tree mapping the low element of a case range to the high |
8785 | element, or NULL_TREE if there is no high element. Used to | |
8786 | determine whether or not a new case label duplicates an old case | |
8787 | label. We need a tree, rather than simply a hash table, because | |
8788 | of the GNU case range extension. */ | |
8789 | splay_tree cases; | |
a6c0a76c | 8790 | |
e1b7793c ILT |
8791 | /* The bindings at the point of the switch. This is used for |
8792 | warnings crossing decls when branching to a case label. */ | |
8793 | struct c_spot_bindings *bindings; | |
187230a7 | 8794 | |
3e4093b6 RS |
8795 | /* The next node on the stack. */ |
8796 | struct c_switch *next; | |
8797 | }; | |
400fbf9f | 8798 | |
3e4093b6 RS |
8799 | /* A stack of the currently active switch statements. The innermost |
8800 | switch statement is on the top of the stack. There is no need to | |
8801 | mark the stack for garbage collection because it is only active | |
8802 | during the processing of the body of a function, and we never | |
8803 | collect at that point. */ | |
de520661 | 8804 | |
506e2710 | 8805 | struct c_switch *c_switch_stack; |
de520661 | 8806 | |
3e4093b6 | 8807 | /* Start a C switch statement, testing expression EXP. Return the new |
c2255bc4 AH |
8808 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. |
8809 | SWITCH_COND_LOC is the location of the switch's condition. */ | |
de520661 | 8810 | |
3e4093b6 | 8811 | tree |
c2255bc4 AH |
8812 | c_start_case (location_t switch_loc, |
8813 | location_t switch_cond_loc, | |
8814 | tree exp) | |
de520661 | 8815 | { |
c58e8676 | 8816 | tree orig_type = error_mark_node; |
3e4093b6 | 8817 | struct c_switch *cs; |
2f6e4e97 | 8818 | |
3e4093b6 | 8819 | if (exp != error_mark_node) |
de520661 | 8820 | { |
3e4093b6 RS |
8821 | orig_type = TREE_TYPE (exp); |
8822 | ||
c58e8676 | 8823 | if (!INTEGRAL_TYPE_P (orig_type)) |
de520661 | 8824 | { |
c58e8676 VR |
8825 | if (orig_type != error_mark_node) |
8826 | { | |
c2255bc4 | 8827 | error_at (switch_cond_loc, "switch quantity not an integer"); |
c58e8676 VR |
8828 | orig_type = error_mark_node; |
8829 | } | |
3e4093b6 | 8830 | exp = integer_zero_node; |
de520661 | 8831 | } |
3e4093b6 | 8832 | else |
de520661 | 8833 | { |
c58e8676 | 8834 | tree type = TYPE_MAIN_VARIANT (orig_type); |
8b6a5902 | 8835 | |
3176a0c2 | 8836 | if (!in_system_header |
3e4093b6 RS |
8837 | && (type == long_integer_type_node |
8838 | || type == long_unsigned_type_node)) | |
c2255bc4 AH |
8839 | warning_at (switch_cond_loc, |
8840 | OPT_Wtraditional, "%<long%> switch expression not " | |
8841 | "converted to %<int%> in ISO C"); | |
8b6a5902 | 8842 | |
928c19bb | 8843 | exp = c_fully_fold (exp, false, NULL); |
3e4093b6 | 8844 | exp = default_conversion (exp); |
ca085fd7 MLI |
8845 | |
8846 | if (warn_sequence_point) | |
8847 | verify_sequence_points (exp); | |
3e4093b6 RS |
8848 | } |
8849 | } | |
8850 | ||
604f5adf | 8851 | /* Add this new SWITCH_EXPR to the stack. */ |
5d038c4c | 8852 | cs = XNEW (struct c_switch); |
604f5adf | 8853 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); |
c2255bc4 | 8854 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); |
a6c0a76c | 8855 | cs->orig_type = orig_type; |
3e4093b6 | 8856 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
e1b7793c | 8857 | cs->bindings = c_get_switch_bindings (); |
506e2710 RH |
8858 | cs->next = c_switch_stack; |
8859 | c_switch_stack = cs; | |
3e4093b6 | 8860 | |
604f5adf | 8861 | return add_stmt (cs->switch_expr); |
3e4093b6 RS |
8862 | } |
8863 | ||
c2255bc4 | 8864 | /* Process a case label at location LOC. */ |
3e4093b6 RS |
8865 | |
8866 | tree | |
c2255bc4 | 8867 | do_case (location_t loc, tree low_value, tree high_value) |
3e4093b6 RS |
8868 | { |
8869 | tree label = NULL_TREE; | |
8870 | ||
17cede2e JM |
8871 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) |
8872 | { | |
8873 | low_value = c_fully_fold (low_value, false, NULL); | |
8874 | if (TREE_CODE (low_value) == INTEGER_CST) | |
c1771a20 | 8875 | pedwarn (input_location, OPT_Wpedantic, |
17cede2e JM |
8876 | "case label is not an integer constant expression"); |
8877 | } | |
8878 | ||
8879 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
8880 | { | |
8881 | high_value = c_fully_fold (high_value, false, NULL); | |
8882 | if (TREE_CODE (high_value) == INTEGER_CST) | |
c1771a20 | 8883 | pedwarn (input_location, OPT_Wpedantic, |
17cede2e JM |
8884 | "case label is not an integer constant expression"); |
8885 | } | |
8886 | ||
e1b7793c | 8887 | if (c_switch_stack == NULL) |
187230a7 JM |
8888 | { |
8889 | if (low_value) | |
e1b7793c | 8890 | error_at (loc, "case label not within a switch statement"); |
187230a7 | 8891 | else |
e1b7793c ILT |
8892 | error_at (loc, "%<default%> label not within a switch statement"); |
8893 | return NULL_TREE; | |
187230a7 | 8894 | } |
de520661 | 8895 | |
e1b7793c ILT |
8896 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, |
8897 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
8898 | loc)) | |
8899 | return NULL_TREE; | |
8900 | ||
8901 | label = c_add_case_label (loc, c_switch_stack->cases, | |
8902 | SWITCH_COND (c_switch_stack->switch_expr), | |
8903 | c_switch_stack->orig_type, | |
8904 | low_value, high_value); | |
8905 | if (label == error_mark_node) | |
8906 | label = NULL_TREE; | |
3e4093b6 RS |
8907 | return label; |
8908 | } | |
de520661 | 8909 | |
3e4093b6 | 8910 | /* Finish the switch statement. */ |
de520661 | 8911 | |
3e4093b6 | 8912 | void |
325c3691 | 8913 | c_finish_case (tree body) |
3e4093b6 | 8914 | { |
506e2710 | 8915 | struct c_switch *cs = c_switch_stack; |
fbc315db | 8916 | location_t switch_location; |
3e4093b6 | 8917 | |
604f5adf | 8918 | SWITCH_BODY (cs->switch_expr) = body; |
325c3691 | 8919 | |
6de9cd9a | 8920 | /* Emit warnings as needed. */ |
c2255bc4 | 8921 | switch_location = EXPR_LOCATION (cs->switch_expr); |
fbc315db ILT |
8922 | c_do_switch_warnings (cs->cases, switch_location, |
8923 | TREE_TYPE (cs->switch_expr), | |
8924 | SWITCH_COND (cs->switch_expr)); | |
6de9cd9a | 8925 | |
3e4093b6 | 8926 | /* Pop the stack. */ |
506e2710 | 8927 | c_switch_stack = cs->next; |
3e4093b6 | 8928 | splay_tree_delete (cs->cases); |
e1b7793c | 8929 | c_release_switch_bindings (cs->bindings); |
5d038c4c | 8930 | XDELETE (cs); |
de520661 | 8931 | } |
325c3691 | 8932 | \f |
506e2710 RH |
8933 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
8934 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
8935 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
8936 | statement, and was not surrounded with parenthesis. */ | |
325c3691 | 8937 | |
9e51cf9d | 8938 | void |
506e2710 RH |
8939 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
8940 | tree else_block, bool nested_if) | |
325c3691 | 8941 | { |
506e2710 | 8942 | tree stmt; |
325c3691 | 8943 | |
506e2710 RH |
8944 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
8945 | if (warn_parentheses && nested_if && else_block == NULL) | |
325c3691 | 8946 | { |
506e2710 | 8947 | tree inner_if = then_block; |
16865eaa | 8948 | |
61ada8ae | 8949 | /* We know from the grammar productions that there is an IF nested |
506e2710 RH |
8950 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
8951 | it might not be exactly THEN_BLOCK, but should be the last | |
8952 | non-container statement within. */ | |
8953 | while (1) | |
8954 | switch (TREE_CODE (inner_if)) | |
8955 | { | |
8956 | case COND_EXPR: | |
8957 | goto found; | |
8958 | case BIND_EXPR: | |
8959 | inner_if = BIND_EXPR_BODY (inner_if); | |
8960 | break; | |
8961 | case STATEMENT_LIST: | |
8962 | inner_if = expr_last (then_block); | |
8963 | break; | |
8964 | case TRY_FINALLY_EXPR: | |
8965 | case TRY_CATCH_EXPR: | |
8966 | inner_if = TREE_OPERAND (inner_if, 0); | |
8967 | break; | |
8968 | default: | |
366de0ce | 8969 | gcc_unreachable (); |
506e2710 RH |
8970 | } |
8971 | found: | |
16865eaa | 8972 | |
506e2710 | 8973 | if (COND_EXPR_ELSE (inner_if)) |
fab922b1 MLI |
8974 | warning_at (if_locus, OPT_Wparentheses, |
8975 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
506e2710 | 8976 | } |
16865eaa | 8977 | |
2214de30 | 8978 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); |
a281759f | 8979 | SET_EXPR_LOCATION (stmt, if_locus); |
506e2710 | 8980 | add_stmt (stmt); |
325c3691 RH |
8981 | } |
8982 | ||
506e2710 RH |
8983 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
8984 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
8985 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
61ada8ae | 8986 | the statement controlled by the loop. BLAB is the break label. CLAB is |
506e2710 | 8987 | the continue label. Everything is allowed to be NULL. */ |
325c3691 RH |
8988 | |
8989 | void | |
506e2710 RH |
8990 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
8991 | tree blab, tree clab, bool cond_is_first) | |
325c3691 | 8992 | { |
506e2710 RH |
8993 | tree entry = NULL, exit = NULL, t; |
8994 | ||
28af952a RS |
8995 | /* If the condition is zero don't generate a loop construct. */ |
8996 | if (cond && integer_zerop (cond)) | |
8997 | { | |
8998 | if (cond_is_first) | |
8999 | { | |
9000 | t = build_and_jump (&blab); | |
9001 | SET_EXPR_LOCATION (t, start_locus); | |
9002 | add_stmt (t); | |
9003 | } | |
9004 | } | |
9005 | else | |
506e2710 RH |
9006 | { |
9007 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
c22cacf3 | 9008 | |
506e2710 | 9009 | /* If we have an exit condition, then we build an IF with gotos either |
c22cacf3 MS |
9010 | out of the loop, or to the top of it. If there's no exit condition, |
9011 | then we just build a jump back to the top. */ | |
506e2710 | 9012 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); |
c22cacf3 | 9013 | |
28af952a | 9014 | if (cond && !integer_nonzerop (cond)) |
c22cacf3 MS |
9015 | { |
9016 | /* Canonicalize the loop condition to the end. This means | |
9017 | generating a branch to the loop condition. Reuse the | |
9018 | continue label, if possible. */ | |
9019 | if (cond_is_first) | |
9020 | { | |
9021 | if (incr || !clab) | |
9022 | { | |
9023 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
9024 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
9025 | } | |
9026 | else | |
9027 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
a281759f | 9028 | SET_EXPR_LOCATION (t, start_locus); |
c22cacf3 MS |
9029 | add_stmt (t); |
9030 | } | |
9031 | ||
506e2710 | 9032 | t = build_and_jump (&blab); |
506e2710 | 9033 | if (cond_is_first) |
db3927fb AH |
9034 | exit = fold_build3_loc (start_locus, |
9035 | COND_EXPR, void_type_node, cond, exit, t); | |
506e2710 | 9036 | else |
db3927fb AH |
9037 | exit = fold_build3_loc (input_location, |
9038 | COND_EXPR, void_type_node, cond, exit, t); | |
c22cacf3 MS |
9039 | } |
9040 | ||
506e2710 RH |
9041 | add_stmt (top); |
9042 | } | |
c22cacf3 | 9043 | |
506e2710 RH |
9044 | if (body) |
9045 | add_stmt (body); | |
9046 | if (clab) | |
9047 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
9048 | if (incr) | |
9049 | add_stmt (incr); | |
9050 | if (entry) | |
9051 | add_stmt (entry); | |
9052 | if (exit) | |
9053 | add_stmt (exit); | |
9054 | if (blab) | |
9055 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
325c3691 | 9056 | } |
325c3691 RH |
9057 | |
9058 | tree | |
c2255bc4 | 9059 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) |
325c3691 | 9060 | { |
089efaa4 | 9061 | bool skip; |
506e2710 | 9062 | tree label = *label_p; |
325c3691 | 9063 | |
089efaa4 ILT |
9064 | /* In switch statements break is sometimes stylistically used after |
9065 | a return statement. This can lead to spurious warnings about | |
9066 | control reaching the end of a non-void function when it is | |
9067 | inlined. Note that we are calling block_may_fallthru with | |
9068 | language specific tree nodes; this works because | |
9069 | block_may_fallthru returns true when given something it does not | |
9070 | understand. */ | |
9071 | skip = !block_may_fallthru (cur_stmt_list); | |
9072 | ||
506e2710 | 9073 | if (!label) |
089efaa4 ILT |
9074 | { |
9075 | if (!skip) | |
c2255bc4 | 9076 | *label_p = label = create_artificial_label (loc); |
089efaa4 | 9077 | } |
953ff289 DN |
9078 | else if (TREE_CODE (label) == LABEL_DECL) |
9079 | ; | |
9080 | else switch (TREE_INT_CST_LOW (label)) | |
506e2710 | 9081 | { |
953ff289 | 9082 | case 0: |
506e2710 | 9083 | if (is_break) |
c2255bc4 | 9084 | error_at (loc, "break statement not within loop or switch"); |
506e2710 | 9085 | else |
c2255bc4 | 9086 | error_at (loc, "continue statement not within a loop"); |
506e2710 | 9087 | return NULL_TREE; |
953ff289 DN |
9088 | |
9089 | case 1: | |
9090 | gcc_assert (is_break); | |
c2255bc4 | 9091 | error_at (loc, "break statement used with OpenMP for loop"); |
953ff289 DN |
9092 | return NULL_TREE; |
9093 | ||
9094 | default: | |
9095 | gcc_unreachable (); | |
506e2710 | 9096 | } |
325c3691 | 9097 | |
089efaa4 ILT |
9098 | if (skip) |
9099 | return NULL_TREE; | |
9100 | ||
2e28e797 JH |
9101 | if (!is_break) |
9102 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
9103 | ||
53fb4de3 | 9104 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
325c3691 RH |
9105 | } |
9106 | ||
506e2710 | 9107 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
3a5b9284 RH |
9108 | |
9109 | static void | |
c2255bc4 | 9110 | emit_side_effect_warnings (location_t loc, tree expr) |
3a5b9284 | 9111 | { |
e6b5a630 RH |
9112 | if (expr == error_mark_node) |
9113 | ; | |
9114 | else if (!TREE_SIDE_EFFECTS (expr)) | |
3a5b9284 RH |
9115 | { |
9116 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
c2255bc4 | 9117 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); |
3a5b9284 | 9118 | } |
27f33b15 | 9119 | else |
c2255bc4 | 9120 | warn_if_unused_value (expr, loc); |
3a5b9284 RH |
9121 | } |
9122 | ||
506e2710 | 9123 | /* Process an expression as if it were a complete statement. Emit |
c2255bc4 AH |
9124 | diagnostics, but do not call ADD_STMT. LOC is the location of the |
9125 | statement. */ | |
3a5b9284 | 9126 | |
506e2710 | 9127 | tree |
c2255bc4 | 9128 | c_process_expr_stmt (location_t loc, tree expr) |
3a5b9284 | 9129 | { |
056928b2 JJ |
9130 | tree exprv; |
9131 | ||
3a5b9284 | 9132 | if (!expr) |
506e2710 | 9133 | return NULL_TREE; |
3a5b9284 | 9134 | |
928c19bb JM |
9135 | expr = c_fully_fold (expr, false, NULL); |
9136 | ||
3a5b9284 RH |
9137 | if (warn_sequence_point) |
9138 | verify_sequence_points (expr); | |
9139 | ||
9140 | if (TREE_TYPE (expr) != error_mark_node | |
9141 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
9142 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
c2255bc4 | 9143 | error_at (loc, "expression statement has incomplete type"); |
3a5b9284 RH |
9144 | |
9145 | /* If we're not processing a statement expression, warn about unused values. | |
9146 | Warnings for statement expressions will be emitted later, once we figure | |
9147 | out which is the result. */ | |
9148 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
27f33b15 | 9149 | && warn_unused_value) |
c2255bc4 | 9150 | emit_side_effect_warnings (loc, expr); |
3a5b9284 | 9151 | |
056928b2 JJ |
9152 | exprv = expr; |
9153 | while (TREE_CODE (exprv) == COMPOUND_EXPR) | |
9154 | exprv = TREE_OPERAND (exprv, 1); | |
f1a89dd0 JJ |
9155 | while (CONVERT_EXPR_P (exprv)) |
9156 | exprv = TREE_OPERAND (exprv, 0); | |
9157 | if (DECL_P (exprv) | |
9158 | || handled_component_p (exprv) | |
9159 | || TREE_CODE (exprv) == ADDR_EXPR) | |
056928b2 | 9160 | mark_exp_read (exprv); |
fa8351f8 | 9161 | |
3a5b9284 RH |
9162 | /* If the expression is not of a type to which we cannot assign a line |
9163 | number, wrap the thing in a no-op NOP_EXPR. */ | |
6615c446 | 9164 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) |
c2255bc4 AH |
9165 | { |
9166 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
9167 | SET_EXPR_LOCATION (expr, loc); | |
9168 | } | |
506e2710 RH |
9169 | |
9170 | return expr; | |
9171 | } | |
9172 | ||
c2255bc4 AH |
9173 | /* Emit an expression as a statement. LOC is the location of the |
9174 | expression. */ | |
506e2710 RH |
9175 | |
9176 | tree | |
c2255bc4 | 9177 | c_finish_expr_stmt (location_t loc, tree expr) |
506e2710 RH |
9178 | { |
9179 | if (expr) | |
c2255bc4 | 9180 | return add_stmt (c_process_expr_stmt (loc, expr)); |
506e2710 RH |
9181 | else |
9182 | return NULL; | |
3a5b9284 RH |
9183 | } |
9184 | ||
9185 | /* Do the opposite and emit a statement as an expression. To begin, | |
9186 | create a new binding level and return it. */ | |
325c3691 RH |
9187 | |
9188 | tree | |
9189 | c_begin_stmt_expr (void) | |
9190 | { | |
9191 | tree ret; | |
9192 | ||
9193 | /* We must force a BLOCK for this level so that, if it is not expanded | |
9194 | later, there is a way to turn off the entire subtree of blocks that | |
9195 | are contained in it. */ | |
9196 | keep_next_level (); | |
9197 | ret = c_begin_compound_stmt (true); | |
e1b7793c ILT |
9198 | |
9199 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
9200 | ? NULL | |
9201 | : c_switch_stack->bindings); | |
325c3691 RH |
9202 | |
9203 | /* Mark the current statement list as belonging to a statement list. */ | |
9204 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
9205 | ||
9206 | return ret; | |
9207 | } | |
9208 | ||
c2255bc4 AH |
9209 | /* LOC is the location of the compound statement to which this body |
9210 | belongs. */ | |
9211 | ||
325c3691 | 9212 | tree |
c2255bc4 | 9213 | c_finish_stmt_expr (location_t loc, tree body) |
325c3691 | 9214 | { |
3a5b9284 | 9215 | tree last, type, tmp, val; |
325c3691 RH |
9216 | tree *last_p; |
9217 | ||
c2255bc4 | 9218 | body = c_end_compound_stmt (loc, body, true); |
e1b7793c ILT |
9219 | |
9220 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
9221 | ? NULL | |
9222 | : c_switch_stack->bindings); | |
325c3691 | 9223 | |
3a5b9284 RH |
9224 | /* Locate the last statement in BODY. See c_end_compound_stmt |
9225 | about always returning a BIND_EXPR. */ | |
9226 | last_p = &BIND_EXPR_BODY (body); | |
9227 | last = BIND_EXPR_BODY (body); | |
9228 | ||
9229 | continue_searching: | |
325c3691 RH |
9230 | if (TREE_CODE (last) == STATEMENT_LIST) |
9231 | { | |
3a5b9284 RH |
9232 | tree_stmt_iterator i; |
9233 | ||
9234 | /* This can happen with degenerate cases like ({ }). No value. */ | |
9235 | if (!TREE_SIDE_EFFECTS (last)) | |
9236 | return body; | |
9237 | ||
9238 | /* If we're supposed to generate side effects warnings, process | |
9239 | all of the statements except the last. */ | |
27f33b15 | 9240 | if (warn_unused_value) |
325c3691 | 9241 | { |
3a5b9284 | 9242 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
c2255bc4 AH |
9243 | { |
9244 | location_t tloc; | |
9245 | tree t = tsi_stmt (i); | |
9246 | ||
9247 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
9248 | emit_side_effect_warnings (tloc, t); | |
9249 | } | |
325c3691 RH |
9250 | } |
9251 | else | |
3a5b9284 RH |
9252 | i = tsi_last (last); |
9253 | last_p = tsi_stmt_ptr (i); | |
9254 | last = *last_p; | |
325c3691 RH |
9255 | } |
9256 | ||
3a5b9284 RH |
9257 | /* If the end of the list is exception related, then the list was split |
9258 | by a call to push_cleanup. Continue searching. */ | |
9259 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
9260 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
9261 | { | |
9262 | last_p = &TREE_OPERAND (last, 0); | |
9263 | last = *last_p; | |
9264 | goto continue_searching; | |
9265 | } | |
9266 | ||
26d8af35 JM |
9267 | if (last == error_mark_node) |
9268 | return last; | |
9269 | ||
3a5b9284 RH |
9270 | /* In the case that the BIND_EXPR is not necessary, return the |
9271 | expression out from inside it. */ | |
26d8af35 JM |
9272 | if (last == BIND_EXPR_BODY (body) |
9273 | && BIND_EXPR_VARS (body) == NULL) | |
591baeb0 | 9274 | { |
928c19bb JM |
9275 | /* Even if this looks constant, do not allow it in a constant |
9276 | expression. */ | |
e5a94231 | 9277 | last = c_wrap_maybe_const (last, true); |
591baeb0 JM |
9278 | /* Do not warn if the return value of a statement expression is |
9279 | unused. */ | |
928c19bb | 9280 | TREE_NO_WARNING (last) = 1; |
591baeb0 JM |
9281 | return last; |
9282 | } | |
325c3691 RH |
9283 | |
9284 | /* Extract the type of said expression. */ | |
9285 | type = TREE_TYPE (last); | |
325c3691 | 9286 | |
3a5b9284 RH |
9287 | /* If we're not returning a value at all, then the BIND_EXPR that |
9288 | we already have is a fine expression to return. */ | |
9289 | if (!type || VOID_TYPE_P (type)) | |
9290 | return body; | |
9291 | ||
9292 | /* Now that we've located the expression containing the value, it seems | |
9293 | silly to make voidify_wrapper_expr repeat the process. Create a | |
9294 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
9295 | tmp = create_tmp_var_raw (type, NULL); | |
9296 | ||
9297 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
9298 | tree_expr_nonnegative_p giving up immediately. */ | |
9299 | val = last; | |
9300 | if (TREE_CODE (val) == NOP_EXPR | |
9301 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
9302 | val = TREE_OPERAND (val, 0); | |
9303 | ||
53fb4de3 | 9304 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
5e278028 | 9305 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); |
3a5b9284 | 9306 | |
c2255bc4 AH |
9307 | { |
9308 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
9309 | SET_EXPR_LOCATION (t, loc); | |
9310 | return t; | |
9311 | } | |
325c3691 RH |
9312 | } |
9313 | \f | |
9314 | /* Begin and end compound statements. This is as simple as pushing | |
9315 | and popping new statement lists from the tree. */ | |
9316 | ||
9317 | tree | |
9318 | c_begin_compound_stmt (bool do_scope) | |
9319 | { | |
9320 | tree stmt = push_stmt_list (); | |
9321 | if (do_scope) | |
4dfa0342 | 9322 | push_scope (); |
325c3691 RH |
9323 | return stmt; |
9324 | } | |
9325 | ||
c2255bc4 AH |
9326 | /* End a compound statement. STMT is the statement. LOC is the |
9327 | location of the compound statement-- this is usually the location | |
9328 | of the opening brace. */ | |
9329 | ||
325c3691 | 9330 | tree |
c2255bc4 | 9331 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) |
325c3691 RH |
9332 | { |
9333 | tree block = NULL; | |
9334 | ||
9335 | if (do_scope) | |
9336 | { | |
9337 | if (c_dialect_objc ()) | |
9338 | objc_clear_super_receiver (); | |
9339 | block = pop_scope (); | |
9340 | } | |
9341 | ||
9342 | stmt = pop_stmt_list (stmt); | |
c2255bc4 | 9343 | stmt = c_build_bind_expr (loc, block, stmt); |
325c3691 RH |
9344 | |
9345 | /* If this compound statement is nested immediately inside a statement | |
9346 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
9347 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
9348 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
9349 | was really last. */ | |
38e01f9e | 9350 | if (building_stmt_list_p () |
325c3691 RH |
9351 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) |
9352 | && TREE_CODE (stmt) != BIND_EXPR) | |
9353 | { | |
53fb4de3 | 9354 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
325c3691 | 9355 | TREE_SIDE_EFFECTS (stmt) = 1; |
c2255bc4 | 9356 | SET_EXPR_LOCATION (stmt, loc); |
325c3691 RH |
9357 | } |
9358 | ||
9359 | return stmt; | |
9360 | } | |
5a508662 RH |
9361 | |
9362 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
9363 | when the current scope is exited. EH_ONLY is true when this is not | |
9364 | meant to apply to normal control flow transfer. */ | |
9365 | ||
9366 | void | |
c2255bc4 | 9367 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
5a508662 | 9368 | { |
3a5b9284 RH |
9369 | enum tree_code code; |
9370 | tree stmt, list; | |
9371 | bool stmt_expr; | |
9372 | ||
9373 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
c2255bc4 | 9374 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); |
5a508662 | 9375 | add_stmt (stmt); |
3a5b9284 RH |
9376 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
9377 | list = push_stmt_list (); | |
9378 | TREE_OPERAND (stmt, 0) = list; | |
9379 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
5a508662 | 9380 | } |
325c3691 | 9381 | \f |
3e4093b6 RS |
9382 | /* Build a binary-operation expression without default conversions. |
9383 | CODE is the kind of expression to build. | |
ba47d38d | 9384 | LOCATION is the operator's location. |
3e4093b6 RS |
9385 | This function differs from `build' in several ways: |
9386 | the data type of the result is computed and recorded in it, | |
9387 | warnings are generated if arg data types are invalid, | |
9388 | special handling for addition and subtraction of pointers is known, | |
9389 | and some optimization is done (operations on narrow ints | |
9390 | are done in the narrower type when that gives the same result). | |
9391 | Constant folding is also done before the result is returned. | |
de520661 | 9392 | |
3e4093b6 RS |
9393 | Note that the operands will never have enumeral types, or function |
9394 | or array types, because either they will have the default conversions | |
9395 | performed or they have both just been converted to some other type in which | |
9396 | the arithmetic is to be done. */ | |
9397 | ||
9398 | tree | |
ba47d38d AH |
9399 | build_binary_op (location_t location, enum tree_code code, |
9400 | tree orig_op0, tree orig_op1, int convert_p) | |
de520661 | 9401 | { |
8ce94e44 JM |
9402 | tree type0, type1, orig_type0, orig_type1; |
9403 | tree eptype; | |
3e4093b6 RS |
9404 | enum tree_code code0, code1; |
9405 | tree op0, op1; | |
c9f9eb5d | 9406 | tree ret = error_mark_node; |
4de67c26 | 9407 | const char *invalid_op_diag; |
4d84fe7c | 9408 | bool op0_int_operands, op1_int_operands; |
928c19bb | 9409 | bool int_const, int_const_or_overflow, int_operands; |
b62acd60 | 9410 | |
3e4093b6 RS |
9411 | /* Expression code to give to the expression when it is built. |
9412 | Normally this is CODE, which is what the caller asked for, | |
9413 | but in some special cases we change it. */ | |
9414 | enum tree_code resultcode = code; | |
8b6a5902 | 9415 | |
3e4093b6 RS |
9416 | /* Data type in which the computation is to be performed. |
9417 | In the simplest cases this is the common type of the arguments. */ | |
9418 | tree result_type = NULL; | |
9419 | ||
8ce94e44 JM |
9420 | /* When the computation is in excess precision, the type of the |
9421 | final EXCESS_PRECISION_EXPR. */ | |
2d2e923f | 9422 | tree semantic_result_type = NULL; |
8ce94e44 | 9423 | |
3e4093b6 RS |
9424 | /* Nonzero means operands have already been type-converted |
9425 | in whatever way is necessary. | |
9426 | Zero means they need to be converted to RESULT_TYPE. */ | |
9427 | int converted = 0; | |
9428 | ||
9429 | /* Nonzero means create the expression with this type, rather than | |
9430 | RESULT_TYPE. */ | |
9431 | tree build_type = 0; | |
9432 | ||
9433 | /* Nonzero means after finally constructing the expression | |
9434 | convert it to this type. */ | |
9435 | tree final_type = 0; | |
9436 | ||
9437 | /* Nonzero if this is an operation like MIN or MAX which can | |
9438 | safely be computed in short if both args are promoted shorts. | |
9439 | Also implies COMMON. | |
9440 | -1 indicates a bitwise operation; this makes a difference | |
9441 | in the exact conditions for when it is safe to do the operation | |
9442 | in a narrower mode. */ | |
9443 | int shorten = 0; | |
9444 | ||
9445 | /* Nonzero if this is a comparison operation; | |
9446 | if both args are promoted shorts, compare the original shorts. | |
9447 | Also implies COMMON. */ | |
9448 | int short_compare = 0; | |
9449 | ||
9450 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
9451 | original short and then promoted if the operand is a promoted short. */ | |
9452 | int short_shift = 0; | |
9453 | ||
9454 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
9455 | int common = 0; | |
9456 | ||
58393038 ZL |
9457 | /* True means types are compatible as far as ObjC is concerned. */ |
9458 | bool objc_ok; | |
9459 | ||
8ce94e44 JM |
9460 | /* True means this is an arithmetic operation that may need excess |
9461 | precision. */ | |
9462 | bool may_need_excess_precision; | |
9463 | ||
180f8dbb JM |
9464 | /* True means this is a boolean operation that converts both its |
9465 | operands to truth-values. */ | |
9466 | bool boolean_op = false; | |
9467 | ||
ba47d38d AH |
9468 | if (location == UNKNOWN_LOCATION) |
9469 | location = input_location; | |
9470 | ||
4d84fe7c JM |
9471 | op0 = orig_op0; |
9472 | op1 = orig_op1; | |
9473 | ||
9474 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
9475 | if (op0_int_operands) | |
9476 | op0 = remove_c_maybe_const_expr (op0); | |
9477 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
9478 | if (op1_int_operands) | |
9479 | op1 = remove_c_maybe_const_expr (op1); | |
9480 | int_operands = (op0_int_operands && op1_int_operands); | |
928c19bb JM |
9481 | if (int_operands) |
9482 | { | |
9483 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
9484 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
9485 | int_const = (int_const_or_overflow | |
9486 | && !TREE_OVERFLOW (orig_op0) | |
9487 | && !TREE_OVERFLOW (orig_op1)); | |
9488 | } | |
9489 | else | |
9490 | int_const = int_const_or_overflow = false; | |
9491 | ||
0e3a99ae | 9492 | /* Do not apply default conversion in mixed vector/scalar expression. */ |
f90e8e2e AS |
9493 | if (convert_p |
9494 | && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE) | |
0e3a99ae | 9495 | != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE))) |
790e9490 | 9496 | { |
4d84fe7c JM |
9497 | op0 = default_conversion (op0); |
9498 | op1 = default_conversion (op1); | |
790e9490 RS |
9499 | } |
9500 | ||
8ce94e44 JM |
9501 | orig_type0 = type0 = TREE_TYPE (op0); |
9502 | orig_type1 = type1 = TREE_TYPE (op1); | |
91fa3c30 | 9503 | |
3e4093b6 RS |
9504 | /* The expression codes of the data types of the arguments tell us |
9505 | whether the arguments are integers, floating, pointers, etc. */ | |
9506 | code0 = TREE_CODE (type0); | |
9507 | code1 = TREE_CODE (type1); | |
9508 | ||
9509 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
9510 | STRIP_TYPE_NOPS (op0); | |
9511 | STRIP_TYPE_NOPS (op1); | |
9512 | ||
9513 | /* If an error was already reported for one of the arguments, | |
9514 | avoid reporting another error. */ | |
9515 | ||
9516 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
9517 | return error_mark_node; | |
9518 | ||
4de67c26 JM |
9519 | if ((invalid_op_diag |
9520 | = targetm.invalid_binary_op (code, type0, type1))) | |
9521 | { | |
ba47d38d | 9522 | error_at (location, invalid_op_diag); |
4de67c26 JM |
9523 | return error_mark_node; |
9524 | } | |
9525 | ||
8ce94e44 JM |
9526 | switch (code) |
9527 | { | |
9528 | case PLUS_EXPR: | |
9529 | case MINUS_EXPR: | |
9530 | case MULT_EXPR: | |
9531 | case TRUNC_DIV_EXPR: | |
9532 | case CEIL_DIV_EXPR: | |
9533 | case FLOOR_DIV_EXPR: | |
9534 | case ROUND_DIV_EXPR: | |
9535 | case EXACT_DIV_EXPR: | |
9536 | may_need_excess_precision = true; | |
9537 | break; | |
9538 | default: | |
9539 | may_need_excess_precision = false; | |
9540 | break; | |
9541 | } | |
9542 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
9543 | { | |
9544 | op0 = TREE_OPERAND (op0, 0); | |
9545 | type0 = TREE_TYPE (op0); | |
9546 | } | |
9547 | else if (may_need_excess_precision | |
9548 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
9549 | { | |
9550 | type0 = eptype; | |
9551 | op0 = convert (eptype, op0); | |
9552 | } | |
9553 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
9554 | { | |
9555 | op1 = TREE_OPERAND (op1, 0); | |
9556 | type1 = TREE_TYPE (op1); | |
9557 | } | |
9558 | else if (may_need_excess_precision | |
9559 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
9560 | { | |
9561 | type1 = eptype; | |
9562 | op1 = convert (eptype, op1); | |
9563 | } | |
9564 | ||
58393038 ZL |
9565 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); |
9566 | ||
0e3a99ae AS |
9567 | /* In case when one of the operands of the binary operation is |
9568 | a vector and another is a scalar -- convert scalar to vector. */ | |
9569 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
9570 | { | |
a212e43f MG |
9571 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, |
9572 | true); | |
f90e8e2e | 9573 | |
0e3a99ae AS |
9574 | switch (convert_flag) |
9575 | { | |
9576 | case stv_error: | |
9577 | return error_mark_node; | |
9578 | case stv_firstarg: | |
9579 | { | |
9580 | bool maybe_const = true; | |
9581 | tree sc; | |
9582 | sc = c_fully_fold (op0, false, &maybe_const); | |
9583 | sc = save_expr (sc); | |
9584 | sc = convert (TREE_TYPE (type1), sc); | |
9585 | op0 = build_vector_from_val (type1, sc); | |
9586 | if (!maybe_const) | |
9587 | op0 = c_wrap_maybe_const (op0, true); | |
9588 | orig_type0 = type0 = TREE_TYPE (op0); | |
9589 | code0 = TREE_CODE (type0); | |
9590 | converted = 1; | |
9591 | break; | |
9592 | } | |
9593 | case stv_secondarg: | |
9594 | { | |
9595 | bool maybe_const = true; | |
9596 | tree sc; | |
9597 | sc = c_fully_fold (op1, false, &maybe_const); | |
9598 | sc = save_expr (sc); | |
9599 | sc = convert (TREE_TYPE (type0), sc); | |
9600 | op1 = build_vector_from_val (type0, sc); | |
9601 | if (!maybe_const) | |
54b9f838 | 9602 | op1 = c_wrap_maybe_const (op1, true); |
0e3a99ae AS |
9603 | orig_type1 = type1 = TREE_TYPE (op1); |
9604 | code1 = TREE_CODE (type1); | |
9605 | converted = 1; | |
9606 | break; | |
9607 | } | |
9608 | default: | |
9609 | break; | |
9610 | } | |
9611 | } | |
9612 | ||
3e4093b6 | 9613 | switch (code) |
de520661 | 9614 | { |
3e4093b6 RS |
9615 | case PLUS_EXPR: |
9616 | /* Handle the pointer + int case. */ | |
9617 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 9618 | { |
db3927fb | 9619 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); |
c9f9eb5d AH |
9620 | goto return_build_binary_op; |
9621 | } | |
3e4093b6 | 9622 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) |
c9f9eb5d | 9623 | { |
db3927fb | 9624 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); |
c9f9eb5d AH |
9625 | goto return_build_binary_op; |
9626 | } | |
fe67cf58 | 9627 | else |
3e4093b6 RS |
9628 | common = 1; |
9629 | break; | |
400fbf9f | 9630 | |
3e4093b6 RS |
9631 | case MINUS_EXPR: |
9632 | /* Subtraction of two similar pointers. | |
9633 | We must subtract them as integers, then divide by object size. */ | |
9634 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
744aa42f | 9635 | && comp_target_types (location, type0, type1)) |
c9f9eb5d | 9636 | { |
db3927fb | 9637 | ret = pointer_diff (location, op0, op1); |
c9f9eb5d AH |
9638 | goto return_build_binary_op; |
9639 | } | |
3e4093b6 RS |
9640 | /* Handle pointer minus int. Just like pointer plus int. */ |
9641 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 9642 | { |
db3927fb | 9643 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); |
c9f9eb5d AH |
9644 | goto return_build_binary_op; |
9645 | } | |
3e4093b6 RS |
9646 | else |
9647 | common = 1; | |
9648 | break; | |
8b6a5902 | 9649 | |
3e4093b6 RS |
9650 | case MULT_EXPR: |
9651 | common = 1; | |
9652 | break; | |
9653 | ||
9654 | case TRUNC_DIV_EXPR: | |
9655 | case CEIL_DIV_EXPR: | |
9656 | case FLOOR_DIV_EXPR: | |
9657 | case ROUND_DIV_EXPR: | |
9658 | case EXACT_DIV_EXPR: | |
c9f9eb5d | 9659 | warn_for_div_by_zero (location, op1); |
3e4093b6 RS |
9660 | |
9661 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 9662 | || code0 == FIXED_POINT_TYPE |
3e4093b6 RS |
9663 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
9664 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
ab22c1fa | 9665 | || code1 == FIXED_POINT_TYPE |
3e4093b6 | 9666 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 9667 | { |
5bed876a AH |
9668 | enum tree_code tcode0 = code0, tcode1 = code1; |
9669 | ||
3a021db2 | 9670 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
5bed876a | 9671 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
3a021db2 | 9672 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) |
5bed876a | 9673 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
3a021db2 | 9674 | |
ab22c1fa CF |
9675 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) |
9676 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
3e4093b6 RS |
9677 | resultcode = RDIV_EXPR; |
9678 | else | |
9679 | /* Although it would be tempting to shorten always here, that | |
9680 | loses on some targets, since the modulo instruction is | |
9681 | undefined if the quotient can't be represented in the | |
9682 | computation mode. We shorten only if unsigned or if | |
9683 | dividing by something we know != -1. */ | |
8df83eae | 9684 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 9685 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 9686 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
9687 | common = 1; |
9688 | } | |
9689 | break; | |
de520661 | 9690 | |
3e4093b6 | 9691 | case BIT_AND_EXPR: |
3e4093b6 RS |
9692 | case BIT_IOR_EXPR: |
9693 | case BIT_XOR_EXPR: | |
9694 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
9695 | shorten = -1; | |
9ef0c8d9 AP |
9696 | /* Allow vector types which are not floating point types. */ |
9697 | else if (code0 == VECTOR_TYPE | |
9698 | && code1 == VECTOR_TYPE | |
9699 | && !VECTOR_FLOAT_TYPE_P (type0) | |
9700 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
3e4093b6 RS |
9701 | common = 1; |
9702 | break; | |
9703 | ||
9704 | case TRUNC_MOD_EXPR: | |
9705 | case FLOOR_MOD_EXPR: | |
c9f9eb5d | 9706 | warn_for_div_by_zero (location, op1); |
de520661 | 9707 | |
5cfd5d9b AP |
9708 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
9709 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9710 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
9711 | common = 1; | |
9712 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3e4093b6 RS |
9713 | { |
9714 | /* Although it would be tempting to shorten always here, that loses | |
9715 | on some targets, since the modulo instruction is undefined if the | |
9716 | quotient can't be represented in the computation mode. We shorten | |
9717 | only if unsigned or if dividing by something we know != -1. */ | |
8df83eae | 9718 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 9719 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 9720 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
9721 | common = 1; |
9722 | } | |
9723 | break; | |
de520661 | 9724 | |
3e4093b6 RS |
9725 | case TRUTH_ANDIF_EXPR: |
9726 | case TRUTH_ORIF_EXPR: | |
9727 | case TRUTH_AND_EXPR: | |
9728 | case TRUTH_OR_EXPR: | |
9729 | case TRUTH_XOR_EXPR: | |
9730 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
ab22c1fa CF |
9731 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
9732 | || code0 == FIXED_POINT_TYPE) | |
3e4093b6 | 9733 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE |
ab22c1fa CF |
9734 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE |
9735 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
9736 | { |
9737 | /* Result of these operations is always an int, | |
9738 | but that does not mean the operands should be | |
9739 | converted to ints! */ | |
9740 | result_type = integer_type_node; | |
a27d595d JM |
9741 | if (op0_int_operands) |
9742 | { | |
9743 | op0 = c_objc_common_truthvalue_conversion (location, orig_op0); | |
9744 | op0 = remove_c_maybe_const_expr (op0); | |
9745 | } | |
9746 | else | |
9747 | op0 = c_objc_common_truthvalue_conversion (location, op0); | |
9748 | if (op1_int_operands) | |
9749 | { | |
9750 | op1 = c_objc_common_truthvalue_conversion (location, orig_op1); | |
9751 | op1 = remove_c_maybe_const_expr (op1); | |
9752 | } | |
9753 | else | |
9754 | op1 = c_objc_common_truthvalue_conversion (location, op1); | |
3e4093b6 | 9755 | converted = 1; |
180f8dbb | 9756 | boolean_op = true; |
3e4093b6 | 9757 | } |
928c19bb JM |
9758 | if (code == TRUTH_ANDIF_EXPR) |
9759 | { | |
9760 | int_const_or_overflow = (int_operands | |
9761 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9762 | && (op0 == truthvalue_false_node | |
9763 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9764 | int_const = (int_const_or_overflow | |
9765 | && !TREE_OVERFLOW (orig_op0) | |
9766 | && (op0 == truthvalue_false_node | |
9767 | || !TREE_OVERFLOW (orig_op1))); | |
9768 | } | |
9769 | else if (code == TRUTH_ORIF_EXPR) | |
9770 | { | |
9771 | int_const_or_overflow = (int_operands | |
9772 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9773 | && (op0 == truthvalue_true_node | |
9774 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9775 | int_const = (int_const_or_overflow | |
9776 | && !TREE_OVERFLOW (orig_op0) | |
9777 | && (op0 == truthvalue_true_node | |
9778 | || !TREE_OVERFLOW (orig_op1))); | |
9779 | } | |
3e4093b6 | 9780 | break; |
eba80994 | 9781 | |
3e4093b6 RS |
9782 | /* Shift operations: result has same type as first operand; |
9783 | always convert second operand to int. | |
9784 | Also set SHORT_SHIFT if shifting rightward. */ | |
de520661 | 9785 | |
3e4093b6 | 9786 | case RSHIFT_EXPR: |
f87bd04b AS |
9787 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
9788 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
9789 | { | |
9790 | result_type = type0; | |
9791 | converted = 1; | |
9792 | } | |
9793 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
9794 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9795 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
9796 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
9797 | { | |
9798 | result_type = type0; | |
9799 | converted = 1; | |
9800 | } | |
9801 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
ab22c1fa | 9802 | && code1 == INTEGER_TYPE) |
3e4093b6 | 9803 | { |
928c19bb | 9804 | if (TREE_CODE (op1) == INTEGER_CST) |
b62acd60 | 9805 | { |
3e4093b6 | 9806 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
9807 | { |
9808 | int_const = false; | |
7d882b83 | 9809 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9810 | warning (0, "right shift count is negative"); |
9811 | } | |
3e4093b6 | 9812 | else |
bbb818c6 | 9813 | { |
3f75a254 | 9814 | if (!integer_zerop (op1)) |
3e4093b6 RS |
9815 | short_shift = 1; |
9816 | ||
9817 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
928c19bb JM |
9818 | { |
9819 | int_const = false; | |
7d882b83 | 9820 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9821 | warning (0, "right shift count >= width of type"); |
9822 | } | |
bbb818c6 | 9823 | } |
b62acd60 | 9824 | } |
de520661 | 9825 | |
3e4093b6 RS |
9826 | /* Use the type of the value to be shifted. */ |
9827 | result_type = type0; | |
f87bd04b AS |
9828 | /* Convert the non vector shift-count to an integer, regardless |
9829 | of size of value being shifted. */ | |
9830 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
9831 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
3e4093b6 RS |
9832 | op1 = convert (integer_type_node, op1); |
9833 | /* Avoid converting op1 to result_type later. */ | |
9834 | converted = 1; | |
400fbf9f | 9835 | } |
3e4093b6 | 9836 | break; |
253b6b82 | 9837 | |
3e4093b6 | 9838 | case LSHIFT_EXPR: |
f87bd04b AS |
9839 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE |
9840 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
9841 | { | |
9842 | result_type = type0; | |
9843 | converted = 1; | |
9844 | } | |
9845 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
9846 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9847 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
9848 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
9849 | { | |
9850 | result_type = type0; | |
9851 | converted = 1; | |
9852 | } | |
9853 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
ab22c1fa | 9854 | && code1 == INTEGER_TYPE) |
3e4093b6 | 9855 | { |
928c19bb | 9856 | if (TREE_CODE (op1) == INTEGER_CST) |
de520661 | 9857 | { |
3e4093b6 | 9858 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
9859 | { |
9860 | int_const = false; | |
7d882b83 | 9861 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9862 | warning (0, "left shift count is negative"); |
9863 | } | |
de520661 | 9864 | |
3e4093b6 | 9865 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
928c19bb JM |
9866 | { |
9867 | int_const = false; | |
7d882b83 | 9868 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9869 | warning (0, "left shift count >= width of type"); |
9870 | } | |
94ba5069 | 9871 | } |
de520661 | 9872 | |
3e4093b6 RS |
9873 | /* Use the type of the value to be shifted. */ |
9874 | result_type = type0; | |
f87bd04b AS |
9875 | /* Convert the non vector shift-count to an integer, regardless |
9876 | of size of value being shifted. */ | |
9877 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
9878 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
3e4093b6 RS |
9879 | op1 = convert (integer_type_node, op1); |
9880 | /* Avoid converting op1 to result_type later. */ | |
9881 | converted = 1; | |
400fbf9f | 9882 | } |
3e4093b6 | 9883 | break; |
de520661 | 9884 | |
3e4093b6 RS |
9885 | case EQ_EXPR: |
9886 | case NE_EXPR: | |
d246ab4f AS |
9887 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
9888 | { | |
9889 | tree intt; | |
9890 | if (TREE_TYPE (type0) != TREE_TYPE (type1)) | |
9891 | { | |
9892 | error_at (location, "comparing vectors with different " | |
9893 | "element types"); | |
9894 | return error_mark_node; | |
9895 | } | |
9896 | ||
9897 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
9898 | { | |
9899 | error_at (location, "comparing vectors with different " | |
9900 | "number of elements"); | |
9901 | return error_mark_node; | |
9902 | } | |
9903 | ||
9904 | /* Always construct signed integer vector type. */ | |
9905 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
9906 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
9907 | result_type = build_opaque_vector_type (intt, | |
9908 | TYPE_VECTOR_SUBPARTS (type0)); | |
9909 | converted = 1; | |
9910 | break; | |
9911 | } | |
ae311566 | 9912 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) |
ba47d38d AH |
9913 | warning_at (location, |
9914 | OPT_Wfloat_equal, | |
9915 | "comparing floating point with == or != is unsafe"); | |
3e4093b6 RS |
9916 | /* Result of comparison is always int, |
9917 | but don't convert the args to int! */ | |
9918 | build_type = integer_type_node; | |
9919 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 9920 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) |
3e4093b6 | 9921 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
ab22c1fa | 9922 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) |
3e4093b6 | 9923 | short_compare = 1; |
637f1455 SZ |
9924 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
9925 | { | |
9926 | if (TREE_CODE (op0) == ADDR_EXPR | |
9927 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) | |
9928 | { | |
9929 | if (code == EQ_EXPR) | |
9930 | warning_at (location, | |
9931 | OPT_Waddress, | |
9932 | "the comparison will always evaluate as %<false%> " | |
9933 | "for the address of %qD will never be NULL", | |
9934 | TREE_OPERAND (op0, 0)); | |
9935 | else | |
9936 | warning_at (location, | |
9937 | OPT_Waddress, | |
9938 | "the comparison will always evaluate as %<true%> " | |
9939 | "for the address of %qD will never be NULL", | |
9940 | TREE_OPERAND (op0, 0)); | |
9941 | } | |
9942 | result_type = type0; | |
9943 | } | |
9944 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) | |
9945 | { | |
9946 | if (TREE_CODE (op1) == ADDR_EXPR | |
9947 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) | |
9948 | { | |
9949 | if (code == EQ_EXPR) | |
9950 | warning_at (location, | |
f90e8e2e | 9951 | OPT_Waddress, |
637f1455 SZ |
9952 | "the comparison will always evaluate as %<false%> " |
9953 | "for the address of %qD will never be NULL", | |
9954 | TREE_OPERAND (op1, 0)); | |
9955 | else | |
9956 | warning_at (location, | |
9957 | OPT_Waddress, | |
9958 | "the comparison will always evaluate as %<true%> " | |
9959 | "for the address of %qD will never be NULL", | |
9960 | TREE_OPERAND (op1, 0)); | |
9961 | } | |
9962 | result_type = type1; | |
9963 | } | |
3e4093b6 RS |
9964 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) |
9965 | { | |
9966 | tree tt0 = TREE_TYPE (type0); | |
9967 | tree tt1 = TREE_TYPE (type1); | |
36c5e70a BE |
9968 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); |
9969 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
9970 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
9971 | ||
3e4093b6 RS |
9972 | /* Anything compares with void *. void * compares with anything. |
9973 | Otherwise, the targets must be compatible | |
9974 | and both must be object or both incomplete. */ | |
744aa42f | 9975 | if (comp_target_types (location, type0, type1)) |
10bc1b1b | 9976 | result_type = common_pointer_type (type0, type1); |
36c5e70a BE |
9977 | else if (!addr_space_superset (as0, as1, &as_common)) |
9978 | { | |
9979 | error_at (location, "comparison of pointers to " | |
9980 | "disjoint address spaces"); | |
9981 | return error_mark_node; | |
9982 | } | |
3e4093b6 | 9983 | else if (VOID_TYPE_P (tt0)) |
ee2990e7 | 9984 | { |
36c5e70a | 9985 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) |
c1771a20 | 9986 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 9987 | "comparison of %<void *%> with function pointer"); |
ee2990e7 | 9988 | } |
3e4093b6 | 9989 | else if (VOID_TYPE_P (tt1)) |
e6834654 | 9990 | { |
36c5e70a | 9991 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) |
c1771a20 | 9992 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 9993 | "comparison of %<void *%> with function pointer"); |
e6834654 | 9994 | } |
3e4093b6 | 9995 | else |
58393038 ZL |
9996 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
9997 | if (!objc_ok) | |
ba47d38d | 9998 | pedwarn (location, 0, |
509c9d60 | 9999 | "comparison of distinct pointer types lacks a cast"); |
e6834654 | 10000 | |
3e4093b6 | 10001 | if (result_type == NULL_TREE) |
36c5e70a BE |
10002 | { |
10003 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
10004 | result_type = build_pointer_type | |
10005 | (build_qualified_type (void_type_node, qual)); | |
10006 | } | |
e6834654 | 10007 | } |
3e4093b6 | 10008 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
de520661 | 10009 | { |
3e4093b6 | 10010 | result_type = type0; |
ba47d38d | 10011 | pedwarn (location, 0, "comparison between pointer and integer"); |
de520661 | 10012 | } |
3e4093b6 | 10013 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
8b6a5902 | 10014 | { |
3e4093b6 | 10015 | result_type = type1; |
ba47d38d | 10016 | pedwarn (location, 0, "comparison between pointer and integer"); |
8b6a5902 | 10017 | } |
3e4093b6 | 10018 | break; |
8b6a5902 | 10019 | |
3e4093b6 RS |
10020 | case LE_EXPR: |
10021 | case GE_EXPR: | |
10022 | case LT_EXPR: | |
10023 | case GT_EXPR: | |
d246ab4f AS |
10024 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) |
10025 | { | |
10026 | tree intt; | |
10027 | if (TREE_TYPE (type0) != TREE_TYPE (type1)) | |
10028 | { | |
10029 | error_at (location, "comparing vectors with different " | |
10030 | "element types"); | |
10031 | return error_mark_node; | |
10032 | } | |
10033 | ||
10034 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10035 | { | |
10036 | error_at (location, "comparing vectors with different " | |
10037 | "number of elements"); | |
10038 | return error_mark_node; | |
10039 | } | |
10040 | ||
10041 | /* Always construct signed integer vector type. */ | |
10042 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10043 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10044 | result_type = build_opaque_vector_type (intt, | |
10045 | TYPE_VECTOR_SUBPARTS (type0)); | |
10046 | converted = 1; | |
10047 | break; | |
10048 | } | |
3e4093b6 | 10049 | build_type = integer_type_node; |
ab22c1fa CF |
10050 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
10051 | || code0 == FIXED_POINT_TYPE) | |
10052 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
10053 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
10054 | short_compare = 1; |
10055 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
10056 | { | |
36c5e70a BE |
10057 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); |
10058 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
10059 | addr_space_t as_common; | |
10060 | ||
744aa42f | 10061 | if (comp_target_types (location, type0, type1)) |
3e4093b6 | 10062 | { |
10bc1b1b | 10063 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
10064 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
10065 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
ba47d38d | 10066 | pedwarn (location, 0, |
509c9d60 | 10067 | "comparison of complete and incomplete pointers"); |
fcf73884 | 10068 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) |
c1771a20 | 10069 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " |
fcf73884 | 10070 | "ordered comparisons of pointers to functions"); |
d42ba3b8 SZ |
10071 | else if (null_pointer_constant_p (orig_op0) |
10072 | || null_pointer_constant_p (orig_op1)) | |
10073 | warning_at (location, OPT_Wextra, | |
10074 | "ordered comparison of pointer with null pointer"); | |
10075 | ||
3e4093b6 | 10076 | } |
36c5e70a BE |
10077 | else if (!addr_space_superset (as0, as1, &as_common)) |
10078 | { | |
10079 | error_at (location, "comparison of pointers to " | |
10080 | "disjoint address spaces"); | |
10081 | return error_mark_node; | |
10082 | } | |
3e4093b6 RS |
10083 | else |
10084 | { | |
36c5e70a BE |
10085 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
10086 | result_type = build_pointer_type | |
10087 | (build_qualified_type (void_type_node, qual)); | |
ba47d38d | 10088 | pedwarn (location, 0, |
509c9d60 | 10089 | "comparison of distinct pointer types lacks a cast"); |
3e4093b6 RS |
10090 | } |
10091 | } | |
6aa3c60d | 10092 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
3e4093b6 RS |
10093 | { |
10094 | result_type = type0; | |
fcf73884 | 10095 | if (pedantic) |
c1771a20 | 10096 | pedwarn (location, OPT_Wpedantic, |
fcf73884 MLI |
10097 | "ordered comparison of pointer with integer zero"); |
10098 | else if (extra_warnings) | |
ba47d38d | 10099 | warning_at (location, OPT_Wextra, |
d42ba3b8 | 10100 | "ordered comparison of pointer with integer zero"); |
3e4093b6 | 10101 | } |
6aa3c60d | 10102 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
3e4093b6 RS |
10103 | { |
10104 | result_type = type1; | |
d42ba3b8 | 10105 | if (pedantic) |
c1771a20 | 10106 | pedwarn (location, OPT_Wpedantic, |
d42ba3b8 SZ |
10107 | "ordered comparison of pointer with integer zero"); |
10108 | else if (extra_warnings) | |
10109 | warning_at (location, OPT_Wextra, | |
10110 | "ordered comparison of pointer with integer zero"); | |
3e4093b6 RS |
10111 | } |
10112 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
10113 | { | |
10114 | result_type = type0; | |
ba47d38d | 10115 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
10116 | } |
10117 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
10118 | { | |
10119 | result_type = type1; | |
ba47d38d | 10120 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
10121 | } |
10122 | break; | |
64094f6a | 10123 | |
3e4093b6 | 10124 | default: |
37b2f290 | 10125 | gcc_unreachable (); |
c9fe6f9f | 10126 | } |
8f17b5c5 | 10127 | |
e57e265b PB |
10128 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
10129 | return error_mark_node; | |
10130 | ||
5bed876a AH |
10131 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
10132 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
10133 | || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0), | |
10134 | TREE_TYPE (type1)))) | |
10135 | { | |
ba47d38d | 10136 | binary_op_error (location, code, type0, type1); |
5bed876a AH |
10137 | return error_mark_node; |
10138 | } | |
10139 | ||
3e4093b6 | 10140 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
ab22c1fa | 10141 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) |
3e4093b6 RS |
10142 | && |
10143 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
ab22c1fa | 10144 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 10145 | { |
2ca862e9 JM |
10146 | bool first_complex = (code0 == COMPLEX_TYPE); |
10147 | bool second_complex = (code1 == COMPLEX_TYPE); | |
10148 | int none_complex = (!first_complex && !second_complex); | |
39b726dd | 10149 | |
3e4093b6 | 10150 | if (shorten || common || short_compare) |
3bf6bfcc JJ |
10151 | { |
10152 | result_type = c_common_type (type0, type1); | |
0a0b3574 MM |
10153 | do_warn_double_promotion (result_type, type0, type1, |
10154 | "implicit conversion from %qT to %qT " | |
10155 | "to match other operand of binary " | |
10156 | "expression", | |
10157 | location); | |
3bf6bfcc JJ |
10158 | if (result_type == error_mark_node) |
10159 | return error_mark_node; | |
10160 | } | |
400fbf9f | 10161 | |
2ca862e9 JM |
10162 | if (first_complex != second_complex |
10163 | && (code == PLUS_EXPR | |
10164 | || code == MINUS_EXPR | |
10165 | || code == MULT_EXPR | |
10166 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
10167 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
10168 | && flag_signed_zeros) | |
10169 | { | |
10170 | /* An operation on mixed real/complex operands must be | |
10171 | handled specially, but the language-independent code can | |
10172 | more easily optimize the plain complex arithmetic if | |
10173 | -fno-signed-zeros. */ | |
10174 | tree real_type = TREE_TYPE (result_type); | |
10175 | tree real, imag; | |
10176 | if (type0 != orig_type0 || type1 != orig_type1) | |
10177 | { | |
10178 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 10179 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
2ca862e9 JM |
10180 | } |
10181 | if (first_complex) | |
10182 | { | |
10183 | if (TREE_TYPE (op0) != result_type) | |
10184 | op0 = convert_and_check (result_type, op0); | |
10185 | if (TREE_TYPE (op1) != real_type) | |
10186 | op1 = convert_and_check (real_type, op1); | |
10187 | } | |
10188 | else | |
10189 | { | |
10190 | if (TREE_TYPE (op0) != real_type) | |
10191 | op0 = convert_and_check (real_type, op0); | |
10192 | if (TREE_TYPE (op1) != result_type) | |
10193 | op1 = convert_and_check (result_type, op1); | |
10194 | } | |
10195 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10196 | return error_mark_node; | |
10197 | if (first_complex) | |
10198 | { | |
10199 | op0 = c_save_expr (op0); | |
10200 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
10201 | op0, 1); | |
10202 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
10203 | op0, 1); | |
10204 | switch (code) | |
10205 | { | |
10206 | case MULT_EXPR: | |
10207 | case TRUNC_DIV_EXPR: | |
c4603e7c | 10208 | op1 = c_save_expr (op1); |
2ca862e9 JM |
10209 | imag = build2 (resultcode, real_type, imag, op1); |
10210 | /* Fall through. */ | |
10211 | case PLUS_EXPR: | |
10212 | case MINUS_EXPR: | |
10213 | real = build2 (resultcode, real_type, real, op1); | |
10214 | break; | |
10215 | default: | |
10216 | gcc_unreachable(); | |
10217 | } | |
10218 | } | |
10219 | else | |
10220 | { | |
10221 | op1 = c_save_expr (op1); | |
10222 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
10223 | op1, 1); | |
10224 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
10225 | op1, 1); | |
10226 | switch (code) | |
10227 | { | |
10228 | case MULT_EXPR: | |
c4603e7c | 10229 | op0 = c_save_expr (op0); |
2ca862e9 JM |
10230 | imag = build2 (resultcode, real_type, op0, imag); |
10231 | /* Fall through. */ | |
10232 | case PLUS_EXPR: | |
10233 | real = build2 (resultcode, real_type, op0, real); | |
10234 | break; | |
10235 | case MINUS_EXPR: | |
10236 | real = build2 (resultcode, real_type, op0, real); | |
10237 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
10238 | break; | |
10239 | default: | |
10240 | gcc_unreachable(); | |
10241 | } | |
10242 | } | |
10243 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
10244 | goto return_build_binary_op; | |
10245 | } | |
10246 | ||
3e4093b6 RS |
10247 | /* For certain operations (which identify themselves by shorten != 0) |
10248 | if both args were extended from the same smaller type, | |
10249 | do the arithmetic in that type and then extend. | |
400fbf9f | 10250 | |
3e4093b6 RS |
10251 | shorten !=0 and !=1 indicates a bitwise operation. |
10252 | For them, this optimization is safe only if | |
10253 | both args are zero-extended or both are sign-extended. | |
10254 | Otherwise, we might change the result. | |
10255 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
10256 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
400fbf9f | 10257 | |
3e4093b6 RS |
10258 | if (shorten && none_complex) |
10259 | { | |
3e4093b6 | 10260 | final_type = result_type; |
b8698a0f | 10261 | result_type = shorten_binary_op (result_type, op0, op1, |
6715192c | 10262 | shorten == -1); |
3e4093b6 | 10263 | } |
88a3dbc1 | 10264 | |
3e4093b6 | 10265 | /* Shifts can be shortened if shifting right. */ |
2f6e4e97 | 10266 | |
3e4093b6 RS |
10267 | if (short_shift) |
10268 | { | |
10269 | int unsigned_arg; | |
10270 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
88a3dbc1 | 10271 | |
3e4093b6 | 10272 | final_type = result_type; |
abe80e6d | 10273 | |
3e4093b6 | 10274 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
8df83eae | 10275 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
e9a25f70 | 10276 | |
3e4093b6 | 10277 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
be123439 | 10278 | && tree_int_cst_sgn (op1) > 0 |
3e4093b6 RS |
10279 | /* We can shorten only if the shift count is less than the |
10280 | number of bits in the smaller type size. */ | |
10281 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
10282 | /* We cannot drop an unsigned shift after sign-extension. */ | |
8df83eae | 10283 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
3e4093b6 RS |
10284 | { |
10285 | /* Do an unsigned shift if the operand was zero-extended. */ | |
10286 | result_type | |
10287 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
10288 | TREE_TYPE (arg0)); | |
10289 | /* Convert value-to-be-shifted to that type. */ | |
10290 | if (TREE_TYPE (op0) != result_type) | |
10291 | op0 = convert (result_type, op0); | |
10292 | converted = 1; | |
abe80e6d | 10293 | } |
88a3dbc1 RK |
10294 | } |
10295 | ||
3e4093b6 RS |
10296 | /* Comparison operations are shortened too but differently. |
10297 | They identify themselves by setting short_compare = 1. */ | |
56cb9733 | 10298 | |
3e4093b6 RS |
10299 | if (short_compare) |
10300 | { | |
10301 | /* Don't write &op0, etc., because that would prevent op0 | |
10302 | from being kept in a register. | |
10303 | Instead, make copies of the our local variables and | |
10304 | pass the copies by reference, then copy them back afterward. */ | |
10305 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
10306 | enum tree_code xresultcode = resultcode; | |
10307 | tree val | |
10308 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
8f17b5c5 | 10309 | |
3e4093b6 | 10310 | if (val != 0) |
c9f9eb5d AH |
10311 | { |
10312 | ret = val; | |
10313 | goto return_build_binary_op; | |
10314 | } | |
8f17b5c5 | 10315 | |
3e4093b6 RS |
10316 | op0 = xop0, op1 = xop1; |
10317 | converted = 1; | |
10318 | resultcode = xresultcode; | |
8f17b5c5 | 10319 | |
7d882b83 | 10320 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
10321 | { |
10322 | bool op0_maybe_const = true; | |
10323 | bool op1_maybe_const = true; | |
10324 | tree orig_op0_folded, orig_op1_folded; | |
10325 | ||
10326 | if (in_late_binary_op) | |
10327 | { | |
10328 | orig_op0_folded = orig_op0; | |
10329 | orig_op1_folded = orig_op1; | |
10330 | } | |
10331 | else | |
10332 | { | |
10333 | /* Fold for the sake of possible warnings, as in | |
10334 | build_conditional_expr. This requires the | |
10335 | "original" values to be folded, not just op0 and | |
10336 | op1. */ | |
f5178456 | 10337 | c_inhibit_evaluation_warnings++; |
928c19bb JM |
10338 | op0 = c_fully_fold (op0, require_constant_value, |
10339 | &op0_maybe_const); | |
10340 | op1 = c_fully_fold (op1, require_constant_value, | |
10341 | &op1_maybe_const); | |
f5178456 | 10342 | c_inhibit_evaluation_warnings--; |
928c19bb JM |
10343 | orig_op0_folded = c_fully_fold (orig_op0, |
10344 | require_constant_value, | |
10345 | NULL); | |
10346 | orig_op1_folded = c_fully_fold (orig_op1, | |
10347 | require_constant_value, | |
10348 | NULL); | |
10349 | } | |
10350 | ||
10351 | if (warn_sign_compare) | |
10352 | warn_for_sign_compare (location, orig_op0_folded, | |
10353 | orig_op1_folded, op0, op1, | |
10354 | result_type, resultcode); | |
5c2f94b4 | 10355 | if (!in_late_binary_op && !int_operands) |
928c19bb JM |
10356 | { |
10357 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
e5a94231 | 10358 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); |
928c19bb | 10359 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) |
e5a94231 | 10360 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 10361 | } |
3e4093b6 | 10362 | } |
2ad1815d | 10363 | } |
64094f6a | 10364 | } |
64094f6a | 10365 | |
3e4093b6 RS |
10366 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
10367 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
10368 | Then the expression will be built. | |
10369 | It will be given type FINAL_TYPE if that is nonzero; | |
10370 | otherwise, it will be given type RESULT_TYPE. */ | |
400fbf9f | 10371 | |
3e4093b6 RS |
10372 | if (!result_type) |
10373 | { | |
ba47d38d | 10374 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); |
3e4093b6 RS |
10375 | return error_mark_node; |
10376 | } | |
400fbf9f | 10377 | |
3e4093b6 | 10378 | if (build_type == NULL_TREE) |
8ce94e44 JM |
10379 | { |
10380 | build_type = result_type; | |
180f8dbb JM |
10381 | if ((type0 != orig_type0 || type1 != orig_type1) |
10382 | && !boolean_op) | |
8ce94e44 JM |
10383 | { |
10384 | gcc_assert (may_need_excess_precision && common); | |
2d2e923f | 10385 | semantic_result_type = c_common_type (orig_type0, orig_type1); |
8ce94e44 JM |
10386 | } |
10387 | } | |
400fbf9f | 10388 | |
2d2e923f MLI |
10389 | if (!converted) |
10390 | { | |
10391 | op0 = ep_convert_and_check (result_type, op0, semantic_result_type); | |
10392 | op1 = ep_convert_and_check (result_type, op1, semantic_result_type); | |
10393 | ||
10394 | /* This can happen if one operand has a vector type, and the other | |
10395 | has a different type. */ | |
10396 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10397 | return error_mark_node; | |
10398 | } | |
10399 | ||
c9f9eb5d | 10400 | /* Treat expressions in initializers specially as they can't trap. */ |
928c19bb JM |
10401 | if (int_const_or_overflow) |
10402 | ret = (require_constant_value | |
db3927fb AH |
10403 | ? fold_build2_initializer_loc (location, resultcode, build_type, |
10404 | op0, op1) | |
10405 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
928c19bb JM |
10406 | else |
10407 | ret = build2 (resultcode, build_type, op0, op1); | |
c9f9eb5d AH |
10408 | if (final_type != 0) |
10409 | ret = convert (final_type, ret); | |
10410 | ||
10411 | return_build_binary_op: | |
10412 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
10413 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) |
10414 | ret = (int_operands | |
10415 | ? note_integer_operands (ret) | |
10416 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
10417 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
10418 | && !in_late_binary_op) | |
10419 | ret = note_integer_operands (ret); | |
2d2e923f MLI |
10420 | if (semantic_result_type) |
10421 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
c9f9eb5d AH |
10422 | protected_set_expr_location (ret, location); |
10423 | return ret; | |
400fbf9f | 10424 | } |
85498824 JM |
10425 | |
10426 | ||
10427 | /* Convert EXPR to be a truth-value, validating its type for this | |
ba47d38d | 10428 | purpose. LOCATION is the source location for the expression. */ |
85498824 JM |
10429 | |
10430 | tree | |
ba47d38d | 10431 | c_objc_common_truthvalue_conversion (location_t location, tree expr) |
85498824 | 10432 | { |
928c19bb JM |
10433 | bool int_const, int_operands; |
10434 | ||
85498824 JM |
10435 | switch (TREE_CODE (TREE_TYPE (expr))) |
10436 | { | |
10437 | case ARRAY_TYPE: | |
ba47d38d | 10438 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); |
85498824 JM |
10439 | return error_mark_node; |
10440 | ||
10441 | case RECORD_TYPE: | |
ba47d38d | 10442 | error_at (location, "used struct type value where scalar is required"); |
85498824 JM |
10443 | return error_mark_node; |
10444 | ||
10445 | case UNION_TYPE: | |
ba47d38d | 10446 | error_at (location, "used union type value where scalar is required"); |
85498824 JM |
10447 | return error_mark_node; |
10448 | ||
04af8788 NP |
10449 | case VOID_TYPE: |
10450 | error_at (location, "void value not ignored as it ought to be"); | |
10451 | return error_mark_node; | |
10452 | ||
46bdb9cf JM |
10453 | case FUNCTION_TYPE: |
10454 | gcc_unreachable (); | |
10455 | ||
d246ab4f AS |
10456 | case VECTOR_TYPE: |
10457 | error_at (location, "used vector type where scalar is required"); | |
10458 | return error_mark_node; | |
10459 | ||
85498824 JM |
10460 | default: |
10461 | break; | |
10462 | } | |
10463 | ||
928c19bb JM |
10464 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); |
10465 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
a27d595d JM |
10466 | if (int_operands && TREE_CODE (expr) != INTEGER_CST) |
10467 | { | |
10468 | expr = remove_c_maybe_const_expr (expr); | |
10469 | expr = build2 (NE_EXPR, integer_type_node, expr, | |
10470 | convert (TREE_TYPE (expr), integer_zero_node)); | |
10471 | expr = note_integer_operands (expr); | |
10472 | } | |
10473 | else | |
10474 | /* ??? Should we also give an error for vectors rather than leaving | |
10475 | those to give errors later? */ | |
10476 | expr = c_common_truthvalue_conversion (location, expr); | |
928c19bb JM |
10477 | |
10478 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
10479 | { | |
10480 | if (TREE_OVERFLOW (expr)) | |
10481 | return expr; | |
10482 | else | |
10483 | return note_integer_operands (expr); | |
10484 | } | |
10485 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
10486 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
10487 | return expr; | |
85498824 | 10488 | } |
73f397d4 JM |
10489 | \f |
10490 | ||
10491 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
10492 | required. */ | |
10493 | ||
10494 | tree | |
51eed280 | 10495 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) |
73f397d4 JM |
10496 | { |
10497 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
10498 | { | |
10499 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
10500 | /* Executing a compound literal inside a function reinitializes | |
10501 | it. */ | |
10502 | if (!TREE_STATIC (decl)) | |
10503 | *se = true; | |
10504 | return decl; | |
10505 | } | |
10506 | else | |
10507 | return expr; | |
10508 | } | |
953ff289 | 10509 | \f |
c0220ea4 | 10510 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
953ff289 DN |
10511 | |
10512 | tree | |
10513 | c_begin_omp_parallel (void) | |
10514 | { | |
10515 | tree block; | |
10516 | ||
10517 | keep_next_level (); | |
10518 | block = c_begin_compound_stmt (true); | |
10519 | ||
10520 | return block; | |
10521 | } | |
10522 | ||
c2255bc4 AH |
10523 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound |
10524 | statement. LOC is the location of the OMP_PARALLEL. */ | |
a68ab351 | 10525 | |
953ff289 | 10526 | tree |
c2255bc4 | 10527 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) |
953ff289 DN |
10528 | { |
10529 | tree stmt; | |
10530 | ||
c2255bc4 | 10531 | block = c_end_compound_stmt (loc, block, true); |
953ff289 DN |
10532 | |
10533 | stmt = make_node (OMP_PARALLEL); | |
10534 | TREE_TYPE (stmt) = void_type_node; | |
10535 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
10536 | OMP_PARALLEL_BODY (stmt) = block; | |
c2255bc4 | 10537 | SET_EXPR_LOCATION (stmt, loc); |
953ff289 DN |
10538 | |
10539 | return add_stmt (stmt); | |
10540 | } | |
10541 | ||
a68ab351 JJ |
10542 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
10543 | ||
10544 | tree | |
10545 | c_begin_omp_task (void) | |
10546 | { | |
10547 | tree block; | |
10548 | ||
10549 | keep_next_level (); | |
10550 | block = c_begin_compound_stmt (true); | |
10551 | ||
10552 | return block; | |
10553 | } | |
10554 | ||
c2255bc4 AH |
10555 | /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound |
10556 | statement. LOC is the location of the #pragma. */ | |
a68ab351 JJ |
10557 | |
10558 | tree | |
c2255bc4 | 10559 | c_finish_omp_task (location_t loc, tree clauses, tree block) |
a68ab351 JJ |
10560 | { |
10561 | tree stmt; | |
10562 | ||
c2255bc4 | 10563 | block = c_end_compound_stmt (loc, block, true); |
a68ab351 JJ |
10564 | |
10565 | stmt = make_node (OMP_TASK); | |
10566 | TREE_TYPE (stmt) = void_type_node; | |
10567 | OMP_TASK_CLAUSES (stmt) = clauses; | |
10568 | OMP_TASK_BODY (stmt) = block; | |
c2255bc4 | 10569 | SET_EXPR_LOCATION (stmt, loc); |
a68ab351 JJ |
10570 | |
10571 | return add_stmt (stmt); | |
10572 | } | |
10573 | ||
953ff289 DN |
10574 | /* For all elements of CLAUSES, validate them vs OpenMP constraints. |
10575 | Remove any elements from the list that are invalid. */ | |
10576 | ||
10577 | tree | |
10578 | c_finish_omp_clauses (tree clauses) | |
10579 | { | |
10580 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
10581 | tree c, t, *pc = &clauses; | |
10582 | const char *name; | |
10583 | ||
10584 | bitmap_obstack_initialize (NULL); | |
10585 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
10586 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
10587 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
10588 | ||
10589 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
10590 | { | |
10591 | bool remove = false; | |
10592 | bool need_complete = false; | |
10593 | bool need_implicitly_determined = false; | |
10594 | ||
aaf46ef9 | 10595 | switch (OMP_CLAUSE_CODE (c)) |
953ff289 DN |
10596 | { |
10597 | case OMP_CLAUSE_SHARED: | |
10598 | name = "shared"; | |
10599 | need_implicitly_determined = true; | |
10600 | goto check_dup_generic; | |
10601 | ||
10602 | case OMP_CLAUSE_PRIVATE: | |
10603 | name = "private"; | |
10604 | need_complete = true; | |
10605 | need_implicitly_determined = true; | |
10606 | goto check_dup_generic; | |
10607 | ||
10608 | case OMP_CLAUSE_REDUCTION: | |
10609 | name = "reduction"; | |
10610 | need_implicitly_determined = true; | |
10611 | t = OMP_CLAUSE_DECL (c); | |
10612 | if (AGGREGATE_TYPE_P (TREE_TYPE (t)) | |
10613 | || POINTER_TYPE_P (TREE_TYPE (t))) | |
10614 | { | |
c2255bc4 AH |
10615 | error_at (OMP_CLAUSE_LOCATION (c), |
10616 | "%qE has invalid type for %<reduction%>", t); | |
953ff289 DN |
10617 | remove = true; |
10618 | } | |
10619 | else if (FLOAT_TYPE_P (TREE_TYPE (t))) | |
10620 | { | |
10621 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
10622 | const char *r_name = NULL; | |
10623 | ||
10624 | switch (r_code) | |
10625 | { | |
10626 | case PLUS_EXPR: | |
10627 | case MULT_EXPR: | |
10628 | case MINUS_EXPR: | |
20906c66 JJ |
10629 | case MIN_EXPR: |
10630 | case MAX_EXPR: | |
953ff289 DN |
10631 | break; |
10632 | case BIT_AND_EXPR: | |
10633 | r_name = "&"; | |
10634 | break; | |
10635 | case BIT_XOR_EXPR: | |
10636 | r_name = "^"; | |
10637 | break; | |
10638 | case BIT_IOR_EXPR: | |
10639 | r_name = "|"; | |
10640 | break; | |
10641 | case TRUTH_ANDIF_EXPR: | |
10642 | r_name = "&&"; | |
10643 | break; | |
10644 | case TRUTH_ORIF_EXPR: | |
10645 | r_name = "||"; | |
10646 | break; | |
10647 | default: | |
10648 | gcc_unreachable (); | |
10649 | } | |
10650 | if (r_name) | |
10651 | { | |
c2255bc4 AH |
10652 | error_at (OMP_CLAUSE_LOCATION (c), |
10653 | "%qE has invalid type for %<reduction(%s)%>", | |
10654 | t, r_name); | |
953ff289 DN |
10655 | remove = true; |
10656 | } | |
10657 | } | |
10658 | goto check_dup_generic; | |
10659 | ||
10660 | case OMP_CLAUSE_COPYPRIVATE: | |
10661 | name = "copyprivate"; | |
10662 | goto check_dup_generic; | |
10663 | ||
10664 | case OMP_CLAUSE_COPYIN: | |
10665 | name = "copyin"; | |
10666 | t = OMP_CLAUSE_DECL (c); | |
10667 | if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) | |
10668 | { | |
c2255bc4 AH |
10669 | error_at (OMP_CLAUSE_LOCATION (c), |
10670 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
953ff289 DN |
10671 | remove = true; |
10672 | } | |
10673 | goto check_dup_generic; | |
10674 | ||
10675 | check_dup_generic: | |
10676 | t = OMP_CLAUSE_DECL (c); | |
10677 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10678 | { | |
c2255bc4 AH |
10679 | error_at (OMP_CLAUSE_LOCATION (c), |
10680 | "%qE is not a variable in clause %qs", t, name); | |
953ff289 DN |
10681 | remove = true; |
10682 | } | |
10683 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10684 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
10685 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10686 | { | |
c2255bc4 AH |
10687 | error_at (OMP_CLAUSE_LOCATION (c), |
10688 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10689 | remove = true; |
10690 | } | |
10691 | else | |
10692 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
10693 | break; | |
10694 | ||
10695 | case OMP_CLAUSE_FIRSTPRIVATE: | |
10696 | name = "firstprivate"; | |
10697 | t = OMP_CLAUSE_DECL (c); | |
10698 | need_complete = true; | |
10699 | need_implicitly_determined = true; | |
10700 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10701 | { | |
c2255bc4 AH |
10702 | error_at (OMP_CLAUSE_LOCATION (c), |
10703 | "%qE is not a variable in clause %<firstprivate%>", t); | |
953ff289 DN |
10704 | remove = true; |
10705 | } | |
10706 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10707 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
10708 | { | |
c2255bc4 AH |
10709 | error_at (OMP_CLAUSE_LOCATION (c), |
10710 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10711 | remove = true; |
10712 | } | |
10713 | else | |
10714 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
10715 | break; | |
10716 | ||
10717 | case OMP_CLAUSE_LASTPRIVATE: | |
10718 | name = "lastprivate"; | |
10719 | t = OMP_CLAUSE_DECL (c); | |
10720 | need_complete = true; | |
10721 | need_implicitly_determined = true; | |
10722 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10723 | { | |
c2255bc4 AH |
10724 | error_at (OMP_CLAUSE_LOCATION (c), |
10725 | "%qE is not a variable in clause %<lastprivate%>", t); | |
953ff289 DN |
10726 | remove = true; |
10727 | } | |
10728 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10729 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10730 | { | |
c2255bc4 AH |
10731 | error_at (OMP_CLAUSE_LOCATION (c), |
10732 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10733 | remove = true; |
10734 | } | |
10735 | else | |
10736 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
10737 | break; | |
10738 | ||
10739 | case OMP_CLAUSE_IF: | |
10740 | case OMP_CLAUSE_NUM_THREADS: | |
10741 | case OMP_CLAUSE_SCHEDULE: | |
10742 | case OMP_CLAUSE_NOWAIT: | |
10743 | case OMP_CLAUSE_ORDERED: | |
10744 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 JJ |
10745 | case OMP_CLAUSE_UNTIED: |
10746 | case OMP_CLAUSE_COLLAPSE: | |
20906c66 JJ |
10747 | case OMP_CLAUSE_FINAL: |
10748 | case OMP_CLAUSE_MERGEABLE: | |
953ff289 DN |
10749 | pc = &OMP_CLAUSE_CHAIN (c); |
10750 | continue; | |
10751 | ||
10752 | default: | |
10753 | gcc_unreachable (); | |
10754 | } | |
10755 | ||
10756 | if (!remove) | |
10757 | { | |
10758 | t = OMP_CLAUSE_DECL (c); | |
10759 | ||
10760 | if (need_complete) | |
10761 | { | |
10762 | t = require_complete_type (t); | |
10763 | if (t == error_mark_node) | |
10764 | remove = true; | |
10765 | } | |
10766 | ||
10767 | if (need_implicitly_determined) | |
10768 | { | |
10769 | const char *share_name = NULL; | |
10770 | ||
10771 | if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
10772 | share_name = "threadprivate"; | |
10773 | else switch (c_omp_predetermined_sharing (t)) | |
10774 | { | |
10775 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
10776 | break; | |
10777 | case OMP_CLAUSE_DEFAULT_SHARED: | |
20906c66 JJ |
10778 | /* const vars may be specified in firstprivate clause. */ |
10779 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
10780 | && TREE_READONLY (t)) | |
10781 | break; | |
953ff289 DN |
10782 | share_name = "shared"; |
10783 | break; | |
10784 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
10785 | share_name = "private"; | |
10786 | break; | |
10787 | default: | |
10788 | gcc_unreachable (); | |
10789 | } | |
10790 | if (share_name) | |
10791 | { | |
c2255bc4 AH |
10792 | error_at (OMP_CLAUSE_LOCATION (c), |
10793 | "%qE is predetermined %qs for %qs", | |
10794 | t, share_name, name); | |
953ff289 DN |
10795 | remove = true; |
10796 | } | |
10797 | } | |
10798 | } | |
10799 | ||
10800 | if (remove) | |
10801 | *pc = OMP_CLAUSE_CHAIN (c); | |
10802 | else | |
10803 | pc = &OMP_CLAUSE_CHAIN (c); | |
10804 | } | |
10805 | ||
10806 | bitmap_obstack_release (NULL); | |
10807 | return clauses; | |
10808 | } | |
9ae165a0 | 10809 | |
0a35513e AH |
10810 | /* Create a transaction node. */ |
10811 | ||
10812 | tree | |
10813 | c_finish_transaction (location_t loc, tree block, int flags) | |
10814 | { | |
10815 | tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); | |
10816 | if (flags & TM_STMT_ATTR_OUTER) | |
10817 | TRANSACTION_EXPR_OUTER (stmt) = 1; | |
10818 | if (flags & TM_STMT_ATTR_RELAXED) | |
10819 | TRANSACTION_EXPR_RELAXED (stmt) = 1; | |
10820 | return add_stmt (stmt); | |
10821 | } | |
10822 | ||
9ae165a0 DG |
10823 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
10824 | down to the element type of an array. */ | |
10825 | ||
10826 | tree | |
10827 | c_build_qualified_type (tree type, int type_quals) | |
10828 | { | |
10829 | if (type == error_mark_node) | |
10830 | return type; | |
10831 | ||
10832 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10833 | { | |
10834 | tree t; | |
10835 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
10836 | type_quals); | |
10837 | ||
10838 | /* See if we already have an identically qualified type. */ | |
10839 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
10840 | { | |
10841 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
10842 | && TYPE_NAME (t) == TYPE_NAME (type) | |
10843 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
10844 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
10845 | TYPE_ATTRIBUTES (type))) | |
10846 | break; | |
10847 | } | |
10848 | if (!t) | |
10849 | { | |
10850 | tree domain = TYPE_DOMAIN (type); | |
10851 | ||
10852 | t = build_variant_type_copy (type); | |
10853 | TREE_TYPE (t) = element_type; | |
10854 | ||
10855 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
10856 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
10857 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
10858 | else if (TYPE_CANONICAL (element_type) != element_type | |
10859 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
10860 | { | |
b8698a0f | 10861 | tree unqualified_canon |
9ae165a0 | 10862 | = build_array_type (TYPE_CANONICAL (element_type), |
b8698a0f | 10863 | domain? TYPE_CANONICAL (domain) |
9ae165a0 | 10864 | : NULL_TREE); |
b8698a0f | 10865 | TYPE_CANONICAL (t) |
9ae165a0 DG |
10866 | = c_build_qualified_type (unqualified_canon, type_quals); |
10867 | } | |
10868 | else | |
10869 | TYPE_CANONICAL (t) = t; | |
10870 | } | |
10871 | return t; | |
10872 | } | |
10873 | ||
10874 | /* A restrict-qualified pointer type must be a pointer to object or | |
10875 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
10876 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
10877 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
10878 | && (!POINTER_TYPE_P (type) | |
10879 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
10880 | { | |
10881 | error ("invalid use of %<restrict%>"); | |
10882 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
10883 | } | |
10884 | ||
10885 | return build_qualified_type (type, type_quals); | |
10886 | } | |
72b5577d ILT |
10887 | |
10888 | /* Build a VA_ARG_EXPR for the C parser. */ | |
10889 | ||
10890 | tree | |
c2255bc4 | 10891 | c_build_va_arg (location_t loc, tree expr, tree type) |
72b5577d ILT |
10892 | { |
10893 | if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) | |
10894 | warning_at (loc, OPT_Wc___compat, | |
10895 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
c2255bc4 | 10896 | return build_va_arg (loc, expr, type); |
72b5577d | 10897 | } |