]>
Commit | Line | Data |
---|---|---|
400fbf9f | 1 | /* Build expressions with type checking for C compiler. |
0953878d | 2 | Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
b295aee2 | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
068e6bb3 | 4 | Free Software Foundation, Inc. |
400fbf9f | 5 | |
1322177d | 6 | This file is part of GCC. |
400fbf9f | 7 | |
1322177d LB |
8 | GCC is free software; you can redistribute it and/or modify it under |
9 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 10 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 11 | version. |
400fbf9f | 12 | |
1322177d LB |
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
400fbf9f JW |
17 | |
18 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
400fbf9f JW |
21 | |
22 | ||
23 | /* This file is part of the C front end. | |
24 | It contains routines to build C expressions given their operands, | |
25 | including computing the types of the result, C-specific error checks, | |
5088b058 | 26 | and some optimization. */ |
400fbf9f JW |
27 | |
28 | #include "config.h" | |
670ee920 | 29 | #include "system.h" |
4977bab6 ZW |
30 | #include "coretypes.h" |
31 | #include "tm.h" | |
742b62e7 | 32 | #include "rtl.h" |
400fbf9f | 33 | #include "tree.h" |
e57e265b | 34 | #include "langhooks.h" |
400fbf9f | 35 | #include "c-tree.h" |
29cc57cf | 36 | #include "c-lang.h" |
6baf1cc8 | 37 | #include "tm_p.h" |
400fbf9f | 38 | #include "flags.h" |
e14417fa | 39 | #include "output.h" |
234042f4 | 40 | #include "expr.h" |
5f6da302 | 41 | #include "toplev.h" |
ab87f8c8 | 42 | #include "intl.h" |
4dd7201e | 43 | #include "ggc.h" |
672a6f42 | 44 | #include "target.h" |
325c3691 | 45 | #include "tree-iterator.h" |
726a989a | 46 | #include "gimple.h" |
089efaa4 | 47 | #include "tree-flow.h" |
325c3691 | 48 | |
2ac2f164 JM |
49 | /* Possible cases of implicit bad conversions. Used to select |
50 | diagnostic messages in convert_for_assignment. */ | |
51 | enum impl_conv { | |
52 | ic_argpass, | |
53 | ic_assign, | |
54 | ic_init, | |
55 | ic_return | |
56 | }; | |
57 | ||
928c19bb JM |
58 | /* Whether we are building a boolean conversion inside |
59 | convert_for_assignment, or some other late binary operation. If | |
60 | build_binary_op is called (from code shared with C++) in this case, | |
61 | then the operands have already been folded and the result will not | |
62 | be folded again, so C_MAYBE_CONST_EXPR should not be generated. */ | |
63 | bool in_late_binary_op; | |
64 | ||
bc4b653b JM |
65 | /* The level of nesting inside "__alignof__". */ |
66 | int in_alignof; | |
67 | ||
68 | /* The level of nesting inside "sizeof". */ | |
69 | int in_sizeof; | |
70 | ||
71 | /* The level of nesting inside "typeof". */ | |
72 | int in_typeof; | |
400fbf9f | 73 | |
b71c7f8a | 74 | /* Nonzero if we've already printed a "missing braces around initializer" |
103b7b17 | 75 | message within this initializer. */ |
b71c7f8a | 76 | static int missing_braces_mentioned; |
103b7b17 | 77 | |
bf730f15 RS |
78 | static int require_constant_value; |
79 | static int require_constant_elements; | |
80 | ||
58f9752a | 81 | static bool null_pointer_constant_p (const_tree); |
f55ade6e | 82 | static tree qualify_type (tree, tree); |
744aa42f ILT |
83 | static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *); |
84 | static int comp_target_types (location_t, tree, tree); | |
85 | static int function_types_compatible_p (const_tree, const_tree, bool *); | |
86 | static int type_lists_compatible_p (const_tree, const_tree, bool *); | |
f55ade6e | 87 | static tree lookup_field (tree, tree); |
bbbbb16a ILT |
88 | static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree, |
89 | tree); | |
db3927fb | 90 | static tree pointer_diff (location_t, tree, tree); |
744aa42f ILT |
91 | static tree convert_for_assignment (location_t, tree, tree, tree, |
92 | enum impl_conv, bool, tree, tree, int); | |
f55ade6e AJ |
93 | static tree valid_compound_expr_initializer (tree, tree); |
94 | static void push_string (const char *); | |
95 | static void push_member_name (tree); | |
f55ade6e AJ |
96 | static int spelling_length (void); |
97 | static char *print_spelling (char *); | |
683d6ff9 | 98 | static void warning_init (int, const char *); |
c2255bc4 | 99 | static tree digest_init (location_t, tree, tree, tree, bool, bool, int); |
bbbbb16a | 100 | static void output_init_element (tree, tree, bool, tree, tree, int, bool); |
f55ade6e AJ |
101 | static void output_pending_init_elements (int); |
102 | static int set_designator (int); | |
103 | static void push_range_stack (tree); | |
bbbbb16a | 104 | static void add_pending_init (tree, tree, tree, bool); |
f55ade6e AJ |
105 | static void set_nonincremental_init (void); |
106 | static void set_nonincremental_init_from_string (tree); | |
107 | static tree find_init_member (tree); | |
9bf24266 | 108 | static void readonly_error (tree, enum lvalue_use); |
f37acdf9 | 109 | static void readonly_warning (tree, enum lvalue_use); |
58f9752a | 110 | static int lvalue_or_else (const_tree, enum lvalue_use); |
4e2fb7de | 111 | static void record_maybe_used_decl (tree); |
744aa42f | 112 | static int comptypes_internal (const_tree, const_tree, bool *); |
6aa3c60d JM |
113 | \f |
114 | /* Return true if EXP is a null pointer constant, false otherwise. */ | |
115 | ||
116 | static bool | |
58f9752a | 117 | null_pointer_constant_p (const_tree expr) |
6aa3c60d JM |
118 | { |
119 | /* This should really operate on c_expr structures, but they aren't | |
120 | yet available everywhere required. */ | |
121 | tree type = TREE_TYPE (expr); | |
122 | return (TREE_CODE (expr) == INTEGER_CST | |
8bcd6380 | 123 | && !TREE_OVERFLOW (expr) |
6aa3c60d JM |
124 | && integer_zerop (expr) |
125 | && (INTEGRAL_TYPE_P (type) | |
126 | || (TREE_CODE (type) == POINTER_TYPE | |
127 | && VOID_TYPE_P (TREE_TYPE (type)) | |
128 | && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); | |
129 | } | |
928c19bb JM |
130 | |
131 | /* EXPR may appear in an unevaluated part of an integer constant | |
132 | expression, but not in an evaluated part. Wrap it in a | |
133 | C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an | |
134 | INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ | |
135 | ||
136 | static tree | |
137 | note_integer_operands (tree expr) | |
138 | { | |
139 | tree ret; | |
140 | if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) | |
141 | { | |
142 | ret = copy_node (expr); | |
143 | TREE_OVERFLOW (ret) = 1; | |
144 | } | |
145 | else | |
146 | { | |
147 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); | |
148 | C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; | |
149 | } | |
150 | return ret; | |
151 | } | |
152 | ||
4d84fe7c JM |
153 | /* Having checked whether EXPR may appear in an unevaluated part of an |
154 | integer constant expression and found that it may, remove any | |
155 | C_MAYBE_CONST_EXPR noting this fact and return the resulting | |
156 | expression. */ | |
157 | ||
158 | static inline tree | |
159 | remove_c_maybe_const_expr (tree expr) | |
160 | { | |
161 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
162 | return C_MAYBE_CONST_EXPR_EXPR (expr); | |
163 | else | |
164 | return expr; | |
165 | } | |
166 | ||
f13c9b2c AP |
167 | \f/* This is a cache to hold if two types are compatible or not. */ |
168 | ||
169 | struct tagged_tu_seen_cache { | |
170 | const struct tagged_tu_seen_cache * next; | |
58f9752a KG |
171 | const_tree t1; |
172 | const_tree t2; | |
f13c9b2c AP |
173 | /* The return value of tagged_types_tu_compatible_p if we had seen |
174 | these two types already. */ | |
175 | int val; | |
176 | }; | |
177 | ||
178 | static const struct tagged_tu_seen_cache * tagged_tu_seen_base; | |
179 | static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); | |
180 | ||
400fbf9f JW |
181 | /* Do `exp = require_complete_type (exp);' to make sure exp |
182 | does not have an incomplete type. (That includes void types.) */ | |
183 | ||
184 | tree | |
2f6e4e97 | 185 | require_complete_type (tree value) |
400fbf9f JW |
186 | { |
187 | tree type = TREE_TYPE (value); | |
188 | ||
c3d5c3fa | 189 | if (value == error_mark_node || type == error_mark_node) |
ea0f786b CB |
190 | return error_mark_node; |
191 | ||
400fbf9f | 192 | /* First, detect a valid value with a complete type. */ |
d0f062fb | 193 | if (COMPLETE_TYPE_P (type)) |
400fbf9f JW |
194 | return value; |
195 | ||
7a228918 | 196 | c_incomplete_type_error (value, type); |
400fbf9f JW |
197 | return error_mark_node; |
198 | } | |
199 | ||
200 | /* Print an error message for invalid use of an incomplete type. | |
201 | VALUE is the expression that was used (or 0 if that isn't known) | |
202 | and TYPE is the type that was invalid. */ | |
203 | ||
204 | void | |
ac7d7749 | 205 | c_incomplete_type_error (const_tree value, const_tree type) |
400fbf9f | 206 | { |
5d5993dd | 207 | const char *type_code_string; |
400fbf9f JW |
208 | |
209 | /* Avoid duplicate error message. */ | |
210 | if (TREE_CODE (type) == ERROR_MARK) | |
211 | return; | |
212 | ||
213 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
214 | || TREE_CODE (value) == PARM_DECL)) | |
c51a1ba9 | 215 | error ("%qD has an incomplete type", value); |
400fbf9f JW |
216 | else |
217 | { | |
218 | retry: | |
219 | /* We must print an error message. Be clever about what it says. */ | |
220 | ||
221 | switch (TREE_CODE (type)) | |
222 | { | |
223 | case RECORD_TYPE: | |
ab87f8c8 | 224 | type_code_string = "struct"; |
400fbf9f JW |
225 | break; |
226 | ||
227 | case UNION_TYPE: | |
ab87f8c8 | 228 | type_code_string = "union"; |
400fbf9f JW |
229 | break; |
230 | ||
231 | case ENUMERAL_TYPE: | |
ab87f8c8 | 232 | type_code_string = "enum"; |
400fbf9f JW |
233 | break; |
234 | ||
235 | case VOID_TYPE: | |
236 | error ("invalid use of void expression"); | |
237 | return; | |
238 | ||
239 | case ARRAY_TYPE: | |
240 | if (TYPE_DOMAIN (type)) | |
241 | { | |
fba78abb RH |
242 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) |
243 | { | |
244 | error ("invalid use of flexible array member"); | |
245 | return; | |
246 | } | |
400fbf9f JW |
247 | type = TREE_TYPE (type); |
248 | goto retry; | |
249 | } | |
250 | error ("invalid use of array with unspecified bounds"); | |
251 | return; | |
252 | ||
253 | default: | |
366de0ce | 254 | gcc_unreachable (); |
400fbf9f JW |
255 | } |
256 | ||
257 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
c51a1ba9 JM |
258 | error ("invalid use of undefined type %<%s %E%>", |
259 | type_code_string, TYPE_NAME (type)); | |
400fbf9f JW |
260 | else |
261 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
c51a1ba9 | 262 | error ("invalid use of incomplete typedef %qD", TYPE_NAME (type)); |
400fbf9f JW |
263 | } |
264 | } | |
265 | ||
ab393bf1 NB |
266 | /* Given a type, apply default promotions wrt unnamed function |
267 | arguments and return the new type. */ | |
268 | ||
269 | tree | |
2f6e4e97 | 270 | c_type_promotes_to (tree type) |
ab393bf1 NB |
271 | { |
272 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
273 | return double_type_node; | |
274 | ||
275 | if (c_promoting_integer_type_p (type)) | |
276 | { | |
277 | /* Preserve unsignedness if not really getting any wider. */ | |
8df83eae | 278 | if (TYPE_UNSIGNED (type) |
c22cacf3 MS |
279 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) |
280 | return unsigned_type_node; | |
ab393bf1 NB |
281 | return integer_type_node; |
282 | } | |
283 | ||
284 | return type; | |
285 | } | |
286 | ||
36c5e70a BE |
287 | /* Return true if between two named address spaces, whether there is a superset |
288 | named address space that encompasses both address spaces. If there is a | |
289 | superset, return which address space is the superset. */ | |
290 | ||
291 | static bool | |
292 | addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) | |
293 | { | |
294 | if (as1 == as2) | |
295 | { | |
296 | *common = as1; | |
297 | return true; | |
298 | } | |
299 | else if (targetm.addr_space.subset_p (as1, as2)) | |
300 | { | |
301 | *common = as2; | |
302 | return true; | |
303 | } | |
304 | else if (targetm.addr_space.subset_p (as2, as1)) | |
305 | { | |
306 | *common = as1; | |
307 | return true; | |
308 | } | |
309 | else | |
310 | return false; | |
311 | } | |
312 | ||
400fbf9f JW |
313 | /* Return a variant of TYPE which has all the type qualifiers of LIKE |
314 | as well as those of TYPE. */ | |
315 | ||
316 | static tree | |
2f6e4e97 | 317 | qualify_type (tree type, tree like) |
400fbf9f | 318 | { |
36c5e70a BE |
319 | addr_space_t as_type = TYPE_ADDR_SPACE (type); |
320 | addr_space_t as_like = TYPE_ADDR_SPACE (like); | |
321 | addr_space_t as_common; | |
322 | ||
323 | /* If the two named address spaces are different, determine the common | |
324 | superset address space. If there isn't one, raise an error. */ | |
325 | if (!addr_space_superset (as_type, as_like, &as_common)) | |
326 | { | |
327 | as_common = as_type; | |
328 | error ("%qT and %qT are in disjoint named address spaces", | |
329 | type, like); | |
330 | } | |
331 | ||
2f6e4e97 | 332 | return c_build_qualified_type (type, |
36c5e70a BE |
333 | TYPE_QUALS_NO_ADDR_SPACE (type) |
334 | | TYPE_QUALS_NO_ADDR_SPACE (like) | |
335 | | ENCODE_QUAL_ADDR_SPACE (as_common)); | |
400fbf9f | 336 | } |
52ffd86e MS |
337 | |
338 | /* Return true iff the given tree T is a variable length array. */ | |
339 | ||
340 | bool | |
ac7d7749 | 341 | c_vla_type_p (const_tree t) |
52ffd86e MS |
342 | { |
343 | if (TREE_CODE (t) == ARRAY_TYPE | |
344 | && C_TYPE_VARIABLE_SIZE (t)) | |
345 | return true; | |
346 | return false; | |
347 | } | |
400fbf9f | 348 | \f |
10bc1b1b | 349 | /* Return the composite type of two compatible types. |
5305f6d7 | 350 | |
10bc1b1b JM |
351 | We assume that comptypes has already been done and returned |
352 | nonzero; if that isn't so, this may crash. In particular, we | |
353 | assume that qualifiers match. */ | |
400fbf9f JW |
354 | |
355 | tree | |
10bc1b1b | 356 | composite_type (tree t1, tree t2) |
400fbf9f | 357 | { |
b3694847 SS |
358 | enum tree_code code1; |
359 | enum tree_code code2; | |
4b027d16 | 360 | tree attributes; |
400fbf9f JW |
361 | |
362 | /* Save time if the two types are the same. */ | |
363 | ||
364 | if (t1 == t2) return t1; | |
365 | ||
366 | /* If one type is nonsense, use the other. */ | |
367 | if (t1 == error_mark_node) | |
368 | return t2; | |
369 | if (t2 == error_mark_node) | |
370 | return t1; | |
371 | ||
10bc1b1b JM |
372 | code1 = TREE_CODE (t1); |
373 | code2 = TREE_CODE (t2); | |
374 | ||
d9525bec | 375 | /* Merge the attributes. */ |
5fd9b178 | 376 | attributes = targetm.merge_type_attributes (t1, t2); |
4b027d16 | 377 | |
10bc1b1b JM |
378 | /* If one is an enumerated type and the other is the compatible |
379 | integer type, the composite type might be either of the two | |
380 | (DR#013 question 3). For consistency, use the enumerated type as | |
381 | the composite type. */ | |
400fbf9f | 382 | |
10bc1b1b JM |
383 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) |
384 | return t1; | |
385 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
386 | return t2; | |
75326e8c | 387 | |
366de0ce | 388 | gcc_assert (code1 == code2); |
b6a10c9f | 389 | |
400fbf9f JW |
390 | switch (code1) |
391 | { | |
400fbf9f | 392 | case POINTER_TYPE: |
10bc1b1b | 393 | /* For two pointers, do this recursively on the target type. */ |
400fbf9f | 394 | { |
3932261a MM |
395 | tree pointed_to_1 = TREE_TYPE (t1); |
396 | tree pointed_to_2 = TREE_TYPE (t2); | |
10bc1b1b JM |
397 | tree target = composite_type (pointed_to_1, pointed_to_2); |
398 | t1 = build_pointer_type (target); | |
fe7080d2 AP |
399 | t1 = build_type_attribute_variant (t1, attributes); |
400 | return qualify_type (t1, t2); | |
400fbf9f | 401 | } |
400fbf9f JW |
402 | |
403 | case ARRAY_TYPE: | |
404 | { | |
10bc1b1b | 405 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
46df2823 JM |
406 | int quals; |
407 | tree unqual_elt; | |
ca8bdb78 JM |
408 | tree d1 = TYPE_DOMAIN (t1); |
409 | tree d2 = TYPE_DOMAIN (t2); | |
410 | bool d1_variable, d2_variable; | |
411 | bool d1_zero, d2_zero; | |
f6294de7 | 412 | bool t1_complete, t2_complete; |
46df2823 | 413 | |
de46b2fe | 414 | /* We should not have any type quals on arrays at all. */ |
36c5e70a BE |
415 | gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) |
416 | && !TYPE_QUALS_NO_ADDR_SPACE (t2)); | |
c22cacf3 | 417 | |
f6294de7 JM |
418 | t1_complete = COMPLETE_TYPE_P (t1); |
419 | t2_complete = COMPLETE_TYPE_P (t2); | |
420 | ||
ca8bdb78 JM |
421 | d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); |
422 | d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); | |
423 | ||
424 | d1_variable = (!d1_zero | |
425 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
426 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
427 | d2_variable = (!d2_zero | |
428 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
429 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
430 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
431 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
ca8bdb78 | 432 | |
400fbf9f | 433 | /* Save space: see if the result is identical to one of the args. */ |
ca8bdb78 JM |
434 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) |
435 | && (d2_variable || d2_zero || !d1_variable)) | |
4b027d16 | 436 | return build_type_attribute_variant (t1, attributes); |
ca8bdb78 JM |
437 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) |
438 | && (d1_variable || d1_zero || !d2_variable)) | |
4b027d16 | 439 | return build_type_attribute_variant (t2, attributes); |
c22cacf3 | 440 | |
de46b2fe AP |
441 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) |
442 | return build_type_attribute_variant (t1, attributes); | |
443 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
444 | return build_type_attribute_variant (t2, attributes); | |
c22cacf3 | 445 | |
46df2823 JM |
446 | /* Merge the element types, and have a size if either arg has |
447 | one. We may have qualifiers on the element types. To set | |
448 | up TYPE_MAIN_VARIANT correctly, we need to form the | |
449 | composite of the unqualified types and add the qualifiers | |
450 | back at the end. */ | |
451 | quals = TYPE_QUALS (strip_array_types (elt)); | |
452 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
453 | t1 = build_array_type (unqual_elt, | |
ca8bdb78 JM |
454 | TYPE_DOMAIN ((TYPE_DOMAIN (t1) |
455 | && (d2_variable | |
456 | || d2_zero | |
457 | || !d1_variable)) | |
458 | ? t1 | |
459 | : t2)); | |
f6294de7 JM |
460 | /* Ensure a composite type involving a zero-length array type |
461 | is a zero-length type not an incomplete type. */ | |
462 | if (d1_zero && d2_zero | |
463 | && (t1_complete || t2_complete) | |
464 | && !COMPLETE_TYPE_P (t1)) | |
465 | { | |
466 | TYPE_SIZE (t1) = bitsize_zero_node; | |
467 | TYPE_SIZE_UNIT (t1) = size_zero_node; | |
468 | } | |
46df2823 | 469 | t1 = c_build_qualified_type (t1, quals); |
de46b2fe | 470 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
471 | } |
472 | ||
fcb99e7b JJ |
473 | case ENUMERAL_TYPE: |
474 | case RECORD_TYPE: | |
475 | case UNION_TYPE: | |
476 | if (attributes != NULL) | |
477 | { | |
478 | /* Try harder not to create a new aggregate type. */ | |
479 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
480 | return t1; | |
481 | if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
482 | return t2; | |
483 | } | |
484 | return build_type_attribute_variant (t1, attributes); | |
485 | ||
400fbf9f JW |
486 | case FUNCTION_TYPE: |
487 | /* Function types: prefer the one that specified arg types. | |
488 | If both do, merge the arg types. Also merge the return types. */ | |
489 | { | |
10bc1b1b | 490 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); |
400fbf9f JW |
491 | tree p1 = TYPE_ARG_TYPES (t1); |
492 | tree p2 = TYPE_ARG_TYPES (t2); | |
493 | int len; | |
494 | tree newargs, n; | |
495 | int i; | |
496 | ||
497 | /* Save space: see if the result is identical to one of the args. */ | |
3f75a254 | 498 | if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) |
4b027d16 | 499 | return build_type_attribute_variant (t1, attributes); |
3f75a254 | 500 | if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) |
4b027d16 | 501 | return build_type_attribute_variant (t2, attributes); |
400fbf9f JW |
502 | |
503 | /* Simple way if one arg fails to specify argument types. */ | |
504 | if (TYPE_ARG_TYPES (t1) == 0) | |
4b027d16 | 505 | { |
fe7080d2 AP |
506 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); |
507 | t1 = build_type_attribute_variant (t1, attributes); | |
508 | return qualify_type (t1, t2); | |
4b027d16 | 509 | } |
400fbf9f | 510 | if (TYPE_ARG_TYPES (t2) == 0) |
4b027d16 RK |
511 | { |
512 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
fe7080d2 AP |
513 | t1 = build_type_attribute_variant (t1, attributes); |
514 | return qualify_type (t1, t2); | |
4b027d16 | 515 | } |
400fbf9f JW |
516 | |
517 | /* If both args specify argument types, we must merge the two | |
518 | lists, argument by argument. */ | |
f75fbaf7 | 519 | /* Tell global_bindings_p to return false so that variable_size |
535a42b1 | 520 | doesn't die on VLAs in parameter types. */ |
f75fbaf7 | 521 | c_override_global_bindings_to_false = true; |
2f4e8f2b | 522 | |
400fbf9f JW |
523 | len = list_length (p1); |
524 | newargs = 0; | |
525 | ||
526 | for (i = 0; i < len; i++) | |
8d9bfdc5 | 527 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); |
400fbf9f JW |
528 | |
529 | n = newargs; | |
530 | ||
531 | for (; p1; | |
532 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
533 | { | |
534 | /* A null type means arg type is not specified. | |
535 | Take whatever the other function type has. */ | |
536 | if (TREE_VALUE (p1) == 0) | |
537 | { | |
538 | TREE_VALUE (n) = TREE_VALUE (p2); | |
539 | goto parm_done; | |
540 | } | |
541 | if (TREE_VALUE (p2) == 0) | |
542 | { | |
543 | TREE_VALUE (n) = TREE_VALUE (p1); | |
544 | goto parm_done; | |
545 | } | |
2f6e4e97 | 546 | |
400fbf9f JW |
547 | /* Given wait (union {union wait *u; int *i} *) |
548 | and wait (union wait *), | |
549 | prefer union wait * as type of parm. */ | |
550 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
551 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
552 | { | |
553 | tree memb; | |
58cb41e6 JJ |
554 | tree mv2 = TREE_VALUE (p2); |
555 | if (mv2 && mv2 != error_mark_node | |
556 | && TREE_CODE (mv2) != ARRAY_TYPE) | |
557 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
400fbf9f JW |
558 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); |
559 | memb; memb = TREE_CHAIN (memb)) | |
58cb41e6 JJ |
560 | { |
561 | tree mv3 = TREE_TYPE (memb); | |
562 | if (mv3 && mv3 != error_mark_node | |
563 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
564 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
565 | if (comptypes (mv3, mv2)) | |
566 | { | |
567 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
568 | TREE_VALUE (p2)); | |
b8698a0f | 569 | pedwarn (input_location, OPT_pedantic, |
fcf73884 | 570 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
571 | goto parm_done; |
572 | } | |
573 | } | |
400fbf9f JW |
574 | } |
575 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
576 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
577 | { | |
578 | tree memb; | |
58cb41e6 JJ |
579 | tree mv1 = TREE_VALUE (p1); |
580 | if (mv1 && mv1 != error_mark_node | |
581 | && TREE_CODE (mv1) != ARRAY_TYPE) | |
582 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
400fbf9f JW |
583 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); |
584 | memb; memb = TREE_CHAIN (memb)) | |
58cb41e6 JJ |
585 | { |
586 | tree mv3 = TREE_TYPE (memb); | |
587 | if (mv3 && mv3 != error_mark_node | |
588 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
589 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
590 | if (comptypes (mv3, mv1)) | |
591 | { | |
592 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
593 | TREE_VALUE (p1)); | |
b8698a0f | 594 | pedwarn (input_location, OPT_pedantic, |
fcf73884 | 595 | "function types not truly compatible in ISO C"); |
58cb41e6 JJ |
596 | goto parm_done; |
597 | } | |
598 | } | |
400fbf9f | 599 | } |
10bc1b1b | 600 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); |
400fbf9f JW |
601 | parm_done: ; |
602 | } | |
603 | ||
f75fbaf7 | 604 | c_override_global_bindings_to_false = false; |
4b027d16 | 605 | t1 = build_function_type (valtype, newargs); |
fe7080d2 | 606 | t1 = qualify_type (t1, t2); |
0f41302f | 607 | /* ... falls through ... */ |
400fbf9f JW |
608 | } |
609 | ||
610 | default: | |
4b027d16 | 611 | return build_type_attribute_variant (t1, attributes); |
400fbf9f JW |
612 | } |
613 | ||
614 | } | |
10bc1b1b JM |
615 | |
616 | /* Return the type of a conditional expression between pointers to | |
617 | possibly differently qualified versions of compatible types. | |
618 | ||
619 | We assume that comp_target_types has already been done and returned | |
620 | nonzero; if that isn't so, this may crash. */ | |
621 | ||
622 | static tree | |
623 | common_pointer_type (tree t1, tree t2) | |
624 | { | |
625 | tree attributes; | |
46df2823 JM |
626 | tree pointed_to_1, mv1; |
627 | tree pointed_to_2, mv2; | |
10bc1b1b | 628 | tree target; |
eb1387a0 | 629 | unsigned target_quals; |
36c5e70a BE |
630 | addr_space_t as1, as2, as_common; |
631 | int quals1, quals2; | |
10bc1b1b JM |
632 | |
633 | /* Save time if the two types are the same. */ | |
634 | ||
635 | if (t1 == t2) return t1; | |
636 | ||
637 | /* If one type is nonsense, use the other. */ | |
638 | if (t1 == error_mark_node) | |
639 | return t2; | |
640 | if (t2 == error_mark_node) | |
641 | return t1; | |
642 | ||
366de0ce | 643 | gcc_assert (TREE_CODE (t1) == POINTER_TYPE |
c22cacf3 | 644 | && TREE_CODE (t2) == POINTER_TYPE); |
10bc1b1b JM |
645 | |
646 | /* Merge the attributes. */ | |
647 | attributes = targetm.merge_type_attributes (t1, t2); | |
648 | ||
649 | /* Find the composite type of the target types, and combine the | |
46df2823 JM |
650 | qualifiers of the two types' targets. Do not lose qualifiers on |
651 | array element types by taking the TYPE_MAIN_VARIANT. */ | |
652 | mv1 = pointed_to_1 = TREE_TYPE (t1); | |
653 | mv2 = pointed_to_2 = TREE_TYPE (t2); | |
654 | if (TREE_CODE (mv1) != ARRAY_TYPE) | |
655 | mv1 = TYPE_MAIN_VARIANT (pointed_to_1); | |
656 | if (TREE_CODE (mv2) != ARRAY_TYPE) | |
657 | mv2 = TYPE_MAIN_VARIANT (pointed_to_2); | |
658 | target = composite_type (mv1, mv2); | |
eb1387a0 RG |
659 | |
660 | /* For function types do not merge const qualifiers, but drop them | |
661 | if used inconsistently. The middle-end uses these to mark const | |
662 | and noreturn functions. */ | |
36c5e70a BE |
663 | quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1); |
664 | quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2); | |
665 | ||
eb1387a0 | 666 | if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) |
36c5e70a | 667 | target_quals = (quals1 & quals2); |
eb1387a0 | 668 | else |
36c5e70a BE |
669 | target_quals = (quals1 | quals2); |
670 | ||
671 | /* If the two named address spaces are different, determine the common | |
672 | superset address space. This is guaranteed to exist due to the | |
673 | assumption that comp_target_type returned non-zero. */ | |
674 | as1 = TYPE_ADDR_SPACE (pointed_to_1); | |
675 | as2 = TYPE_ADDR_SPACE (pointed_to_2); | |
676 | if (!addr_space_superset (as1, as2, &as_common)) | |
677 | gcc_unreachable (); | |
678 | ||
679 | target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); | |
680 | ||
eb1387a0 | 681 | t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); |
10bc1b1b JM |
682 | return build_type_attribute_variant (t1, attributes); |
683 | } | |
684 | ||
685 | /* Return the common type for two arithmetic types under the usual | |
686 | arithmetic conversions. The default conversions have already been | |
687 | applied, and enumerated types converted to their compatible integer | |
688 | types. The resulting type is unqualified and has no attributes. | |
689 | ||
690 | This is the type for the result of most arithmetic operations | |
691 | if the operands have the given two types. */ | |
692 | ||
ccf7f880 JJ |
693 | static tree |
694 | c_common_type (tree t1, tree t2) | |
10bc1b1b JM |
695 | { |
696 | enum tree_code code1; | |
697 | enum tree_code code2; | |
698 | ||
699 | /* If one type is nonsense, use the other. */ | |
700 | if (t1 == error_mark_node) | |
701 | return t2; | |
702 | if (t2 == error_mark_node) | |
703 | return t1; | |
704 | ||
705 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
706 | t1 = TYPE_MAIN_VARIANT (t1); | |
707 | ||
708 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
709 | t2 = TYPE_MAIN_VARIANT (t2); | |
710 | ||
711 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
712 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
713 | ||
714 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
715 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
716 | ||
717 | /* Save time if the two types are the same. */ | |
718 | ||
719 | if (t1 == t2) return t1; | |
720 | ||
721 | code1 = TREE_CODE (t1); | |
722 | code2 = TREE_CODE (t2); | |
723 | ||
366de0ce | 724 | gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE |
ab22c1fa CF |
725 | || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE |
726 | || code1 == INTEGER_TYPE); | |
366de0ce | 727 | gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE |
ab22c1fa CF |
728 | || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE |
729 | || code2 == INTEGER_TYPE); | |
10bc1b1b | 730 | |
5fc89bfd JJ |
731 | /* When one operand is a decimal float type, the other operand cannot be |
732 | a generic float type or a complex type. We also disallow vector types | |
733 | here. */ | |
734 | if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) | |
735 | && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) | |
736 | { | |
737 | if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) | |
738 | { | |
739 | error ("can%'t mix operands of decimal float and vector types"); | |
740 | return error_mark_node; | |
741 | } | |
742 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
743 | { | |
744 | error ("can%'t mix operands of decimal float and complex types"); | |
745 | return error_mark_node; | |
746 | } | |
747 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
748 | { | |
749 | error ("can%'t mix operands of decimal float and other float types"); | |
750 | return error_mark_node; | |
751 | } | |
752 | } | |
753 | ||
10bc1b1b JM |
754 | /* If one type is a vector type, return that type. (How the usual |
755 | arithmetic conversions apply to the vector types extension is not | |
756 | precisely specified.) */ | |
757 | if (code1 == VECTOR_TYPE) | |
758 | return t1; | |
759 | ||
760 | if (code2 == VECTOR_TYPE) | |
761 | return t2; | |
762 | ||
763 | /* If one type is complex, form the common type of the non-complex | |
764 | components, then make that complex. Use T1 or T2 if it is the | |
765 | required type. */ | |
766 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
767 | { | |
768 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
769 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
ccf7f880 | 770 | tree subtype = c_common_type (subtype1, subtype2); |
10bc1b1b JM |
771 | |
772 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
773 | return t1; | |
774 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
775 | return t2; | |
776 | else | |
777 | return build_complex_type (subtype); | |
778 | } | |
779 | ||
780 | /* If only one is real, use it as the result. */ | |
781 | ||
782 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
783 | return t1; | |
784 | ||
785 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
786 | return t2; | |
787 | ||
9a8ce21f JG |
788 | /* If both are real and either are decimal floating point types, use |
789 | the decimal floating point type with the greater precision. */ | |
790 | ||
791 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
792 | { | |
793 | if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node | |
794 | || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) | |
795 | return dfloat128_type_node; | |
796 | else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node | |
797 | || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) | |
798 | return dfloat64_type_node; | |
799 | else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node | |
800 | || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) | |
801 | return dfloat32_type_node; | |
802 | } | |
803 | ||
ab22c1fa CF |
804 | /* Deal with fixed-point types. */ |
805 | if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) | |
806 | { | |
807 | unsigned int unsignedp = 0, satp = 0; | |
808 | enum machine_mode m1, m2; | |
809 | unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; | |
810 | ||
811 | m1 = TYPE_MODE (t1); | |
812 | m2 = TYPE_MODE (t2); | |
813 | ||
814 | /* If one input type is saturating, the result type is saturating. */ | |
815 | if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) | |
816 | satp = 1; | |
817 | ||
818 | /* If both fixed-point types are unsigned, the result type is unsigned. | |
819 | When mixing fixed-point and integer types, follow the sign of the | |
820 | fixed-point type. | |
821 | Otherwise, the result type is signed. */ | |
822 | if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) | |
823 | && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) | |
824 | || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE | |
825 | && TYPE_UNSIGNED (t1)) | |
826 | || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE | |
827 | && TYPE_UNSIGNED (t2))) | |
828 | unsignedp = 1; | |
829 | ||
830 | /* The result type is signed. */ | |
831 | if (unsignedp == 0) | |
832 | { | |
833 | /* If the input type is unsigned, we need to convert to the | |
834 | signed type. */ | |
835 | if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) | |
836 | { | |
d75d71e0 | 837 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
838 | if (GET_MODE_CLASS (m1) == MODE_UFRACT) |
839 | mclass = MODE_FRACT; | |
840 | else if (GET_MODE_CLASS (m1) == MODE_UACCUM) | |
841 | mclass = MODE_ACCUM; | |
842 | else | |
843 | gcc_unreachable (); | |
844 | m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); | |
845 | } | |
846 | if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) | |
847 | { | |
d75d71e0 | 848 | enum mode_class mclass = (enum mode_class) 0; |
ab22c1fa CF |
849 | if (GET_MODE_CLASS (m2) == MODE_UFRACT) |
850 | mclass = MODE_FRACT; | |
851 | else if (GET_MODE_CLASS (m2) == MODE_UACCUM) | |
852 | mclass = MODE_ACCUM; | |
853 | else | |
854 | gcc_unreachable (); | |
855 | m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); | |
856 | } | |
857 | } | |
858 | ||
859 | if (code1 == FIXED_POINT_TYPE) | |
860 | { | |
861 | fbit1 = GET_MODE_FBIT (m1); | |
862 | ibit1 = GET_MODE_IBIT (m1); | |
863 | } | |
864 | else | |
865 | { | |
866 | fbit1 = 0; | |
867 | /* Signed integers need to subtract one sign bit. */ | |
868 | ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); | |
869 | } | |
870 | ||
871 | if (code2 == FIXED_POINT_TYPE) | |
872 | { | |
873 | fbit2 = GET_MODE_FBIT (m2); | |
874 | ibit2 = GET_MODE_IBIT (m2); | |
875 | } | |
876 | else | |
877 | { | |
878 | fbit2 = 0; | |
879 | /* Signed integers need to subtract one sign bit. */ | |
880 | ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); | |
881 | } | |
882 | ||
883 | max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; | |
884 | max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; | |
885 | return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, | |
886 | satp); | |
887 | } | |
888 | ||
10bc1b1b JM |
889 | /* Both real or both integers; use the one with greater precision. */ |
890 | ||
891 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
892 | return t1; | |
893 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
894 | return t2; | |
895 | ||
896 | /* Same precision. Prefer long longs to longs to ints when the | |
897 | same precision, following the C99 rules on integer type rank | |
898 | (which are equivalent to the C90 rules for C90 types). */ | |
899 | ||
900 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
901 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
902 | return long_long_unsigned_type_node; | |
903 | ||
904 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
905 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
906 | { | |
907 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
908 | return long_long_unsigned_type_node; | |
909 | else | |
c22cacf3 | 910 | return long_long_integer_type_node; |
10bc1b1b JM |
911 | } |
912 | ||
913 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
914 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
915 | return long_unsigned_type_node; | |
916 | ||
917 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
918 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
919 | { | |
920 | /* But preserve unsignedness from the other type, | |
921 | since long cannot hold all the values of an unsigned int. */ | |
922 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
923 | return long_unsigned_type_node; | |
924 | else | |
925 | return long_integer_type_node; | |
926 | } | |
927 | ||
928 | /* Likewise, prefer long double to double even if same size. */ | |
929 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
930 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
931 | return long_double_type_node; | |
932 | ||
933 | /* Otherwise prefer the unsigned one. */ | |
934 | ||
935 | if (TYPE_UNSIGNED (t1)) | |
936 | return t1; | |
937 | else | |
938 | return t2; | |
939 | } | |
400fbf9f | 940 | \f |
5922c215 JM |
941 | /* Wrapper around c_common_type that is used by c-common.c and other |
942 | front end optimizations that remove promotions. ENUMERAL_TYPEs | |
b2232745 RS |
943 | are allowed here and are converted to their compatible integer types. |
944 | BOOLEAN_TYPEs are allowed here and return either boolean_type_node or | |
945 | preferably a non-Boolean type as the common type. */ | |
ccf7f880 JJ |
946 | tree |
947 | common_type (tree t1, tree t2) | |
948 | { | |
949 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
950 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); | |
951 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
952 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); | |
b2232745 RS |
953 | |
954 | /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ | |
955 | if (TREE_CODE (t1) == BOOLEAN_TYPE | |
956 | && TREE_CODE (t2) == BOOLEAN_TYPE) | |
957 | return boolean_type_node; | |
958 | ||
959 | /* If either type is BOOLEAN_TYPE, then return the other. */ | |
960 | if (TREE_CODE (t1) == BOOLEAN_TYPE) | |
961 | return t2; | |
962 | if (TREE_CODE (t2) == BOOLEAN_TYPE) | |
963 | return t1; | |
964 | ||
ccf7f880 JJ |
965 | return c_common_type (t1, t2); |
966 | } | |
f13c9b2c | 967 | |
400fbf9f JW |
968 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
969 | or various other operations. Return 2 if they are compatible | |
970 | but a warning may be needed if you use them together. */ | |
971 | ||
972 | int | |
132da1a5 | 973 | comptypes (tree type1, tree type2) |
f13c9b2c AP |
974 | { |
975 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
976 | int val; | |
977 | ||
744aa42f ILT |
978 | val = comptypes_internal (type1, type2, NULL); |
979 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
980 | ||
981 | return val; | |
982 | } | |
983 | ||
984 | /* Like comptypes, but if it returns non-zero because enum and int are | |
985 | compatible, it sets *ENUM_AND_INT_P to true. */ | |
986 | ||
987 | static int | |
988 | comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) | |
989 | { | |
990 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
991 | int val; | |
992 | ||
993 | val = comptypes_internal (type1, type2, enum_and_int_p); | |
f13c9b2c | 994 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); |
c22cacf3 | 995 | |
f13c9b2c | 996 | return val; |
c22cacf3 MS |
997 | } |
998 | \f | |
f13c9b2c AP |
999 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment |
1000 | or various other operations. Return 2 if they are compatible | |
744aa42f ILT |
1001 | but a warning may be needed if you use them together. If |
1002 | ENUM_AND_INT_P is not NULL, and one type is an enum and the other a | |
1003 | compatible integer type, then this sets *ENUM_AND_INT_P to true; | |
1004 | *ENUM_AND_INT_P is never set to false. This differs from | |
1005 | comptypes, in that we don't free the seen types. */ | |
f13c9b2c AP |
1006 | |
1007 | static int | |
744aa42f | 1008 | comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p) |
400fbf9f | 1009 | { |
58f9752a KG |
1010 | const_tree t1 = type1; |
1011 | const_tree t2 = type2; | |
4b027d16 | 1012 | int attrval, val; |
400fbf9f JW |
1013 | |
1014 | /* Suppress errors caused by previously reported errors. */ | |
1015 | ||
8d47dfc5 RH |
1016 | if (t1 == t2 || !t1 || !t2 |
1017 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
400fbf9f JW |
1018 | return 1; |
1019 | ||
21318741 RK |
1020 | /* If either type is the internal version of sizetype, return the |
1021 | language version. */ | |
1022 | if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1) | |
eb1a2c88 DN |
1023 | && TYPE_ORIG_SIZE_TYPE (t1)) |
1024 | t1 = TYPE_ORIG_SIZE_TYPE (t1); | |
21318741 RK |
1025 | |
1026 | if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2) | |
eb1a2c88 DN |
1027 | && TYPE_ORIG_SIZE_TYPE (t2)) |
1028 | t2 = TYPE_ORIG_SIZE_TYPE (t2); | |
1029 | ||
21318741 | 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)); | |
1038 | if (enum_and_int_p != NULL && TREE_CODE (t2) != VOID_TYPE) | |
1039 | *enum_and_int_p = true; | |
1040 | } | |
bca63328 | 1041 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) |
744aa42f ILT |
1042 | { |
1043 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); | |
1044 | if (enum_and_int_p != NULL && TREE_CODE (t1) != VOID_TYPE) | |
1045 | *enum_and_int_p = true; | |
1046 | } | |
400fbf9f JW |
1047 | |
1048 | if (t1 == t2) | |
1049 | return 1; | |
1050 | ||
1051 | /* Different classes of types can't be compatible. */ | |
1052 | ||
3aeb3655 EC |
1053 | if (TREE_CODE (t1) != TREE_CODE (t2)) |
1054 | return 0; | |
400fbf9f | 1055 | |
118a3a8b | 1056 | /* Qualifiers must match. C99 6.7.3p9 */ |
400fbf9f | 1057 | |
3932261a | 1058 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) |
400fbf9f JW |
1059 | return 0; |
1060 | ||
08632da2 RS |
1061 | /* Allow for two different type nodes which have essentially the same |
1062 | definition. Note that we already checked for equality of the type | |
38e01259 | 1063 | qualifiers (just above). */ |
400fbf9f | 1064 | |
46df2823 JM |
1065 | if (TREE_CODE (t1) != ARRAY_TYPE |
1066 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
400fbf9f JW |
1067 | return 1; |
1068 | ||
4b027d16 | 1069 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ |
3f75a254 | 1070 | if (!(attrval = targetm.comp_type_attributes (t1, t2))) |
4b027d16 RK |
1071 | return 0; |
1072 | ||
1073 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1074 | val = 0; | |
1075 | ||
400fbf9f JW |
1076 | switch (TREE_CODE (t1)) |
1077 | { | |
1078 | case POINTER_TYPE: | |
106f5de5 UW |
1079 | /* Do not remove mode or aliasing information. */ |
1080 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1081 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) | |
1082 | break; | |
4b027d16 | 1083 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) |
744aa42f ILT |
1084 | ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
1085 | enum_and_int_p)); | |
4b027d16 | 1086 | break; |
400fbf9f JW |
1087 | |
1088 | case FUNCTION_TYPE: | |
744aa42f | 1089 | val = function_types_compatible_p (t1, t2, enum_and_int_p); |
4b027d16 | 1090 | break; |
400fbf9f JW |
1091 | |
1092 | case ARRAY_TYPE: | |
1093 | { | |
400fbf9f JW |
1094 | tree d1 = TYPE_DOMAIN (t1); |
1095 | tree d2 = TYPE_DOMAIN (t2); | |
3f85558f RH |
1096 | bool d1_variable, d2_variable; |
1097 | bool d1_zero, d2_zero; | |
4b027d16 | 1098 | val = 1; |
400fbf9f JW |
1099 | |
1100 | /* Target types must match incl. qualifiers. */ | |
1101 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
744aa42f ILT |
1102 | && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), |
1103 | enum_and_int_p))) | |
400fbf9f JW |
1104 | return 0; |
1105 | ||
1106 | /* Sizes must match unless one is missing or variable. */ | |
3f85558f | 1107 | if (d1 == 0 || d2 == 0 || d1 == d2) |
4b027d16 | 1108 | break; |
400fbf9f | 1109 | |
3f75a254 JM |
1110 | d1_zero = !TYPE_MAX_VALUE (d1); |
1111 | d2_zero = !TYPE_MAX_VALUE (d2); | |
3f85558f | 1112 | |
3f75a254 | 1113 | d1_variable = (!d1_zero |
3f85558f RH |
1114 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST |
1115 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
3f75a254 | 1116 | d2_variable = (!d2_zero |
3f85558f RH |
1117 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST |
1118 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
52ffd86e MS |
1119 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); |
1120 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
3f85558f RH |
1121 | |
1122 | if (d1_variable || d2_variable) | |
1123 | break; | |
1124 | if (d1_zero && d2_zero) | |
1125 | break; | |
1126 | if (d1_zero || d2_zero | |
3f75a254 JM |
1127 | || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) |
1128 | || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) | |
05bccae2 RK |
1129 | val = 0; |
1130 | ||
c22cacf3 | 1131 | break; |
400fbf9f JW |
1132 | } |
1133 | ||
d1bd0ded | 1134 | case ENUMERAL_TYPE: |
58393038 | 1135 | case RECORD_TYPE: |
d1bd0ded | 1136 | case UNION_TYPE: |
766beae1 | 1137 | if (val != 1 && !same_translation_unit_p (t1, t2)) |
c22cacf3 | 1138 | { |
fcb99e7b JJ |
1139 | tree a1 = TYPE_ATTRIBUTES (t1); |
1140 | tree a2 = TYPE_ATTRIBUTES (t2); | |
1141 | ||
1142 | if (! attribute_list_contained (a1, a2) | |
1143 | && ! attribute_list_contained (a2, a1)) | |
1144 | break; | |
1145 | ||
f13c9b2c | 1146 | if (attrval != 2) |
744aa42f ILT |
1147 | return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p); |
1148 | val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p); | |
f13c9b2c | 1149 | } |
4b027d16 | 1150 | break; |
e9a25f70 | 1151 | |
62e1dfcf | 1152 | case VECTOR_TYPE: |
744aa42f ILT |
1153 | val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) |
1154 | && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
1155 | enum_and_int_p)); | |
62e1dfcf NC |
1156 | break; |
1157 | ||
e9a25f70 JL |
1158 | default: |
1159 | break; | |
400fbf9f | 1160 | } |
4b027d16 | 1161 | return attrval == 2 && val == 1 ? 2 : val; |
400fbf9f JW |
1162 | } |
1163 | ||
36c5e70a BE |
1164 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring |
1165 | their qualifiers, except for named address spaces. If the pointers point to | |
1166 | different named addresses, then we must determine if one address space is a | |
1167 | subset of the other. */ | |
400fbf9f JW |
1168 | |
1169 | static int | |
744aa42f | 1170 | comp_target_types (location_t location, tree ttl, tree ttr) |
400fbf9f | 1171 | { |
392202b0 | 1172 | int val; |
36c5e70a BE |
1173 | tree mvl = TREE_TYPE (ttl); |
1174 | tree mvr = TREE_TYPE (ttr); | |
1175 | addr_space_t asl = TYPE_ADDR_SPACE (mvl); | |
1176 | addr_space_t asr = TYPE_ADDR_SPACE (mvr); | |
1177 | addr_space_t as_common; | |
744aa42f | 1178 | bool enum_and_int_p; |
8b40563c | 1179 | |
36c5e70a BE |
1180 | /* Fail if pointers point to incompatible address spaces. */ |
1181 | if (!addr_space_superset (asl, asr, &as_common)) | |
1182 | return 0; | |
1183 | ||
46df2823 JM |
1184 | /* Do not lose qualifiers on element types of array types that are |
1185 | pointer targets by taking their TYPE_MAIN_VARIANT. */ | |
46df2823 JM |
1186 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
1187 | mvl = TYPE_MAIN_VARIANT (mvl); | |
1188 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
1189 | mvr = TYPE_MAIN_VARIANT (mvr); | |
744aa42f ILT |
1190 | enum_and_int_p = false; |
1191 | val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); | |
8b40563c | 1192 | |
fcf73884 | 1193 | if (val == 2) |
744aa42f ILT |
1194 | pedwarn (location, OPT_pedantic, "types are not quite compatible"); |
1195 | ||
1196 | if (val == 1 && enum_and_int_p && warn_cxx_compat) | |
1197 | warning_at (location, OPT_Wc___compat, | |
1198 | "pointer target types incompatible in C++"); | |
1199 | ||
400fbf9f JW |
1200 | return val; |
1201 | } | |
1202 | \f | |
1203 | /* Subroutines of `comptypes'. */ | |
1204 | ||
f75fbaf7 ZW |
1205 | /* Determine whether two trees derive from the same translation unit. |
1206 | If the CONTEXT chain ends in a null, that tree's context is still | |
1207 | being parsed, so if two trees have context chains ending in null, | |
766beae1 | 1208 | they're in the same translation unit. */ |
f75fbaf7 | 1209 | int |
58f9752a | 1210 | same_translation_unit_p (const_tree t1, const_tree t2) |
766beae1 ZW |
1211 | { |
1212 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
1213 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
1214 | { | |
6615c446 JO |
1215 | case tcc_declaration: |
1216 | t1 = DECL_CONTEXT (t1); break; | |
1217 | case tcc_type: | |
1218 | t1 = TYPE_CONTEXT (t1); break; | |
1219 | case tcc_exceptional: | |
1220 | t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ | |
366de0ce | 1221 | default: gcc_unreachable (); |
766beae1 ZW |
1222 | } |
1223 | ||
1224 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
1225 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
1226 | { | |
6615c446 JO |
1227 | case tcc_declaration: |
1228 | t2 = DECL_CONTEXT (t2); break; | |
1229 | case tcc_type: | |
1230 | t2 = TYPE_CONTEXT (t2); break; | |
1231 | case tcc_exceptional: | |
1232 | t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ | |
366de0ce | 1233 | default: gcc_unreachable (); |
766beae1 ZW |
1234 | } |
1235 | ||
1236 | return t1 == t2; | |
1237 | } | |
1238 | ||
f13c9b2c | 1239 | /* Allocate the seen two types, assuming that they are compatible. */ |
d1bd0ded | 1240 | |
f13c9b2c | 1241 | static struct tagged_tu_seen_cache * |
58f9752a | 1242 | alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) |
f13c9b2c | 1243 | { |
cceb1885 | 1244 | struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); |
f13c9b2c AP |
1245 | tu->next = tagged_tu_seen_base; |
1246 | tu->t1 = t1; | |
1247 | tu->t2 = t2; | |
c22cacf3 | 1248 | |
f13c9b2c | 1249 | tagged_tu_seen_base = tu; |
c22cacf3 | 1250 | |
f13c9b2c AP |
1251 | /* The C standard says that two structures in different translation |
1252 | units are compatible with each other only if the types of their | |
1253 | fields are compatible (among other things). We assume that they | |
1254 | are compatible until proven otherwise when building the cache. | |
1255 | An example where this can occur is: | |
1256 | struct a | |
1257 | { | |
1258 | struct a *next; | |
1259 | }; | |
1260 | If we are comparing this against a similar struct in another TU, | |
c83eecad | 1261 | and did not assume they were compatible, we end up with an infinite |
f13c9b2c AP |
1262 | loop. */ |
1263 | tu->val = 1; | |
1264 | return tu; | |
1265 | } | |
d1bd0ded | 1266 | |
f13c9b2c | 1267 | /* Free the seen types until we get to TU_TIL. */ |
d1bd0ded | 1268 | |
f13c9b2c AP |
1269 | static void |
1270 | free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) | |
1271 | { | |
1272 | const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; | |
1273 | while (tu != tu_til) | |
1274 | { | |
741ac903 KG |
1275 | const struct tagged_tu_seen_cache *const tu1 |
1276 | = (const struct tagged_tu_seen_cache *) tu; | |
f13c9b2c | 1277 | tu = tu1->next; |
b1d5455a | 1278 | free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); |
f13c9b2c AP |
1279 | } |
1280 | tagged_tu_seen_base = tu_til; | |
1281 | } | |
d1bd0ded GK |
1282 | |
1283 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
1284 | compatible. If the two types are not the same (which has been | |
1285 | checked earlier), this can only happen when multiple translation | |
1286 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
744aa42f | 1287 | rules. ENUM_AND_INT_P is as in comptypes_internal. */ |
d1bd0ded GK |
1288 | |
1289 | static int | |
744aa42f ILT |
1290 | tagged_types_tu_compatible_p (const_tree t1, const_tree t2, |
1291 | bool *enum_and_int_p) | |
d1bd0ded GK |
1292 | { |
1293 | tree s1, s2; | |
1294 | bool needs_warning = false; | |
3aeb3655 | 1295 | |
d1bd0ded GK |
1296 | /* We have to verify that the tags of the types are the same. This |
1297 | is harder than it looks because this may be a typedef, so we have | |
1298 | to go look at the original type. It may even be a typedef of a | |
6de9cd9a DN |
1299 | typedef... |
1300 | In the case of compiler-created builtin structs the TYPE_DECL | |
1301 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
dea984dc ILT |
1302 | while (TYPE_NAME (t1) |
1303 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
1304 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
d1bd0ded GK |
1305 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); |
1306 | ||
dea984dc ILT |
1307 | while (TYPE_NAME (t2) |
1308 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
1309 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
d1bd0ded GK |
1310 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); |
1311 | ||
1312 | /* C90 didn't have the requirement that the two tags be the same. */ | |
1313 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
1314 | return 0; | |
3aeb3655 | 1315 | |
d1bd0ded GK |
1316 | /* C90 didn't say what happened if one or both of the types were |
1317 | incomplete; we choose to follow C99 rules here, which is that they | |
1318 | are compatible. */ | |
1319 | if (TYPE_SIZE (t1) == NULL | |
1320 | || TYPE_SIZE (t2) == NULL) | |
1321 | return 1; | |
3aeb3655 | 1322 | |
d1bd0ded | 1323 | { |
f13c9b2c | 1324 | const struct tagged_tu_seen_cache * tts_i; |
d1bd0ded GK |
1325 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) |
1326 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
f13c9b2c | 1327 | return tts_i->val; |
d1bd0ded | 1328 | } |
3aeb3655 | 1329 | |
d1bd0ded GK |
1330 | switch (TREE_CODE (t1)) |
1331 | { | |
1332 | case ENUMERAL_TYPE: | |
1333 | { | |
f13c9b2c | 1334 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
c22cacf3 MS |
1335 | /* Speed up the case where the type values are in the same order. */ |
1336 | tree tv1 = TYPE_VALUES (t1); | |
1337 | tree tv2 = TYPE_VALUES (t2); | |
3aeb3655 | 1338 | |
c22cacf3 | 1339 | if (tv1 == tv2) |
f13c9b2c AP |
1340 | { |
1341 | return 1; | |
1342 | } | |
3aeb3655 | 1343 | |
c22cacf3 MS |
1344 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) |
1345 | { | |
1346 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) | |
1347 | break; | |
1348 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
f13c9b2c | 1349 | { |
c22cacf3 | 1350 | tu->val = 0; |
f13c9b2c AP |
1351 | return 0; |
1352 | } | |
c22cacf3 | 1353 | } |
3aeb3655 | 1354 | |
c22cacf3 | 1355 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) |
f13c9b2c AP |
1356 | { |
1357 | return 1; | |
1358 | } | |
c22cacf3 | 1359 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) |
f13c9b2c AP |
1360 | { |
1361 | tu->val = 0; | |
1362 | return 0; | |
1363 | } | |
3aeb3655 | 1364 | |
d1bd0ded | 1365 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) |
f13c9b2c AP |
1366 | { |
1367 | tu->val = 0; | |
1368 | return 0; | |
1369 | } | |
3aeb3655 | 1370 | |
d1bd0ded GK |
1371 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) |
1372 | { | |
1373 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
1374 | if (s2 == NULL | |
1375 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
f13c9b2c AP |
1376 | { |
1377 | tu->val = 0; | |
1378 | return 0; | |
1379 | } | |
d1bd0ded GK |
1380 | } |
1381 | return 1; | |
1382 | } | |
1383 | ||
1384 | case UNION_TYPE: | |
1385 | { | |
f13c9b2c | 1386 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
d1bd0ded | 1387 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) |
f13c9b2c AP |
1388 | { |
1389 | tu->val = 0; | |
1390 | return 0; | |
1391 | } | |
c22cacf3 | 1392 | |
f13c9b2c AP |
1393 | /* Speed up the common case where the fields are in the same order. */ |
1394 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; | |
1395 | s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2)) | |
1396 | { | |
1397 | int result; | |
c22cacf3 | 1398 | |
3ae4d3cc | 1399 | if (DECL_NAME (s1) != DECL_NAME (s2)) |
f13c9b2c | 1400 | break; |
744aa42f ILT |
1401 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
1402 | enum_and_int_p); | |
3ae4d3cc AO |
1403 | |
1404 | if (result != 1 && !DECL_NAME (s1)) | |
1405 | break; | |
f13c9b2c AP |
1406 | if (result == 0) |
1407 | { | |
1408 | tu->val = 0; | |
1409 | return 0; | |
1410 | } | |
1411 | if (result == 2) | |
1412 | needs_warning = true; | |
1413 | ||
1414 | if (TREE_CODE (s1) == FIELD_DECL | |
1415 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1416 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1417 | { | |
1418 | tu->val = 0; | |
1419 | return 0; | |
1420 | } | |
1421 | } | |
1422 | if (!s1 && !s2) | |
1423 | { | |
1424 | tu->val = needs_warning ? 2 : 1; | |
1425 | return tu->val; | |
1426 | } | |
d1bd0ded GK |
1427 | |
1428 | for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1)) | |
1429 | { | |
1430 | bool ok = false; | |
3aeb3655 | 1431 | |
3ae4d3cc AO |
1432 | for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2)) |
1433 | if (DECL_NAME (s1) == DECL_NAME (s2)) | |
1434 | { | |
1435 | int result; | |
1436 | ||
744aa42f ILT |
1437 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
1438 | enum_and_int_p); | |
3ae4d3cc AO |
1439 | |
1440 | if (result != 1 && !DECL_NAME (s1)) | |
1441 | continue; | |
1442 | if (result == 0) | |
1443 | { | |
1444 | tu->val = 0; | |
1445 | return 0; | |
1446 | } | |
1447 | if (result == 2) | |
1448 | needs_warning = true; | |
1449 | ||
1450 | if (TREE_CODE (s1) == FIELD_DECL | |
1451 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1452 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
d1bd0ded | 1453 | break; |
3ae4d3cc AO |
1454 | |
1455 | ok = true; | |
1456 | break; | |
1457 | } | |
3f75a254 | 1458 | if (!ok) |
f13c9b2c AP |
1459 | { |
1460 | tu->val = 0; | |
1461 | return 0; | |
1462 | } | |
d1bd0ded | 1463 | } |
f13c9b2c AP |
1464 | tu->val = needs_warning ? 2 : 10; |
1465 | return tu->val; | |
d1bd0ded GK |
1466 | } |
1467 | ||
1468 | case RECORD_TYPE: | |
1469 | { | |
c22cacf3 | 1470 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); |
3aeb3655 EC |
1471 | |
1472 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
d1bd0ded GK |
1473 | s1 && s2; |
1474 | s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2)) | |
1475 | { | |
1476 | int result; | |
1477 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
1478 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
1479 | break; | |
744aa42f ILT |
1480 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), |
1481 | enum_and_int_p); | |
d1bd0ded GK |
1482 | if (result == 0) |
1483 | break; | |
1484 | if (result == 2) | |
1485 | needs_warning = true; | |
3aeb3655 | 1486 | |
d1bd0ded GK |
1487 | if (TREE_CODE (s1) == FIELD_DECL |
1488 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1489 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1490 | break; | |
1491 | } | |
d1bd0ded | 1492 | if (s1 && s2) |
f13c9b2c AP |
1493 | tu->val = 0; |
1494 | else | |
1495 | tu->val = needs_warning ? 2 : 1; | |
1496 | return tu->val; | |
d1bd0ded GK |
1497 | } |
1498 | ||
1499 | default: | |
366de0ce | 1500 | gcc_unreachable (); |
d1bd0ded GK |
1501 | } |
1502 | } | |
1503 | ||
400fbf9f JW |
1504 | /* Return 1 if two function types F1 and F2 are compatible. |
1505 | If either type specifies no argument types, | |
1506 | the other must specify a fixed number of self-promoting arg types. | |
2f6e4e97 | 1507 | Otherwise, if one type specifies only the number of arguments, |
400fbf9f | 1508 | the other must specify that number of self-promoting arg types. |
744aa42f ILT |
1509 | Otherwise, the argument types must match. |
1510 | ENUM_AND_INT_P is as in comptypes_internal. */ | |
400fbf9f JW |
1511 | |
1512 | static int | |
744aa42f ILT |
1513 | function_types_compatible_p (const_tree f1, const_tree f2, |
1514 | bool *enum_and_int_p) | |
400fbf9f JW |
1515 | { |
1516 | tree args1, args2; | |
1517 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1518 | int val = 1; | |
1519 | int val1; | |
a6fdc086 GK |
1520 | tree ret1, ret2; |
1521 | ||
1522 | ret1 = TREE_TYPE (f1); | |
1523 | ret2 = TREE_TYPE (f2); | |
1524 | ||
e508a019 JM |
1525 | /* 'volatile' qualifiers on a function's return type used to mean |
1526 | the function is noreturn. */ | |
1527 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
509c9d60 | 1528 | pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); |
a6fdc086 GK |
1529 | if (TYPE_VOLATILE (ret1)) |
1530 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
1531 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
1532 | if (TYPE_VOLATILE (ret2)) | |
1533 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
1534 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
744aa42f | 1535 | val = comptypes_internal (ret1, ret2, enum_and_int_p); |
a6fdc086 | 1536 | if (val == 0) |
400fbf9f JW |
1537 | return 0; |
1538 | ||
1539 | args1 = TYPE_ARG_TYPES (f1); | |
1540 | args2 = TYPE_ARG_TYPES (f2); | |
1541 | ||
1542 | /* An unspecified parmlist matches any specified parmlist | |
1543 | whose argument types don't need default promotions. */ | |
1544 | ||
1545 | if (args1 == 0) | |
1546 | { | |
1547 | if (!self_promoting_args_p (args2)) | |
1548 | return 0; | |
1549 | /* If one of these types comes from a non-prototype fn definition, | |
1550 | compare that with the other type's arglist. | |
3176a0c2 | 1551 | If they don't match, ask for a warning (but no error). */ |
400fbf9f | 1552 | if (TYPE_ACTUAL_ARG_TYPES (f1) |
744aa42f ILT |
1553 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), |
1554 | enum_and_int_p)) | |
400fbf9f JW |
1555 | val = 2; |
1556 | return val; | |
1557 | } | |
1558 | if (args2 == 0) | |
1559 | { | |
1560 | if (!self_promoting_args_p (args1)) | |
1561 | return 0; | |
1562 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
744aa42f ILT |
1563 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), |
1564 | enum_and_int_p)) | |
400fbf9f JW |
1565 | val = 2; |
1566 | return val; | |
1567 | } | |
1568 | ||
1569 | /* Both types have argument lists: compare them and propagate results. */ | |
744aa42f | 1570 | val1 = type_lists_compatible_p (args1, args2, enum_and_int_p); |
400fbf9f JW |
1571 | return val1 != 1 ? val1 : val; |
1572 | } | |
1573 | ||
744aa42f ILT |
1574 | /* Check two lists of types for compatibility, returning 0 for |
1575 | incompatible, 1 for compatible, or 2 for compatible with | |
1576 | warning. ENUM_AND_INT_P is as in comptypes_internal. */ | |
400fbf9f JW |
1577 | |
1578 | static int | |
744aa42f ILT |
1579 | type_lists_compatible_p (const_tree args1, const_tree args2, |
1580 | bool *enum_and_int_p) | |
400fbf9f JW |
1581 | { |
1582 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1583 | int val = 1; | |
9d5f3e49 | 1584 | int newval = 0; |
400fbf9f JW |
1585 | |
1586 | while (1) | |
1587 | { | |
46df2823 | 1588 | tree a1, mv1, a2, mv2; |
400fbf9f JW |
1589 | if (args1 == 0 && args2 == 0) |
1590 | return val; | |
1591 | /* If one list is shorter than the other, | |
1592 | they fail to match. */ | |
1593 | if (args1 == 0 || args2 == 0) | |
1594 | return 0; | |
46df2823 JM |
1595 | mv1 = a1 = TREE_VALUE (args1); |
1596 | mv2 = a2 = TREE_VALUE (args2); | |
1597 | if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) | |
1598 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
1599 | if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) | |
1600 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
400fbf9f JW |
1601 | /* A null pointer instead of a type |
1602 | means there is supposed to be an argument | |
1603 | but nothing is specified about what type it has. | |
1604 | So match anything that self-promotes. */ | |
46df2823 | 1605 | if (a1 == 0) |
400fbf9f | 1606 | { |
46df2823 | 1607 | if (c_type_promotes_to (a2) != a2) |
400fbf9f JW |
1608 | return 0; |
1609 | } | |
46df2823 | 1610 | else if (a2 == 0) |
400fbf9f | 1611 | { |
46df2823 | 1612 | if (c_type_promotes_to (a1) != a1) |
400fbf9f JW |
1613 | return 0; |
1614 | } | |
8f5b6d29 | 1615 | /* If one of the lists has an error marker, ignore this arg. */ |
46df2823 JM |
1616 | else if (TREE_CODE (a1) == ERROR_MARK |
1617 | || TREE_CODE (a2) == ERROR_MARK) | |
8f5b6d29 | 1618 | ; |
744aa42f | 1619 | else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p))) |
400fbf9f JW |
1620 | { |
1621 | /* Allow wait (union {union wait *u; int *i} *) | |
1622 | and wait (union wait *) to be compatible. */ | |
46df2823 JM |
1623 | if (TREE_CODE (a1) == UNION_TYPE |
1624 | && (TYPE_NAME (a1) == 0 | |
ebf0bf7f | 1625 | || TYPE_TRANSPARENT_AGGR (a1)) |
46df2823 JM |
1626 | && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST |
1627 | && tree_int_cst_equal (TYPE_SIZE (a1), | |
1628 | TYPE_SIZE (a2))) | |
400fbf9f JW |
1629 | { |
1630 | tree memb; | |
46df2823 | 1631 | for (memb = TYPE_FIELDS (a1); |
400fbf9f | 1632 | memb; memb = TREE_CHAIN (memb)) |
58cb41e6 JJ |
1633 | { |
1634 | tree mv3 = TREE_TYPE (memb); | |
1635 | if (mv3 && mv3 != error_mark_node | |
1636 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
1637 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
744aa42f | 1638 | if (comptypes_internal (mv3, mv2, enum_and_int_p)) |
58cb41e6 JJ |
1639 | break; |
1640 | } | |
400fbf9f JW |
1641 | if (memb == 0) |
1642 | return 0; | |
1643 | } | |
46df2823 JM |
1644 | else if (TREE_CODE (a2) == UNION_TYPE |
1645 | && (TYPE_NAME (a2) == 0 | |
ebf0bf7f | 1646 | || TYPE_TRANSPARENT_AGGR (a2)) |
46df2823 JM |
1647 | && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST |
1648 | && tree_int_cst_equal (TYPE_SIZE (a2), | |
1649 | TYPE_SIZE (a1))) | |
400fbf9f JW |
1650 | { |
1651 | tree memb; | |
46df2823 | 1652 | for (memb = TYPE_FIELDS (a2); |
400fbf9f | 1653 | memb; memb = TREE_CHAIN (memb)) |
58cb41e6 JJ |
1654 | { |
1655 | tree mv3 = TREE_TYPE (memb); | |
1656 | if (mv3 && mv3 != error_mark_node | |
1657 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
1658 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
744aa42f | 1659 | if (comptypes_internal (mv3, mv1, enum_and_int_p)) |
58cb41e6 JJ |
1660 | break; |
1661 | } | |
400fbf9f JW |
1662 | if (memb == 0) |
1663 | return 0; | |
1664 | } | |
1665 | else | |
1666 | return 0; | |
1667 | } | |
1668 | ||
1669 | /* comptypes said ok, but record if it said to warn. */ | |
1670 | if (newval > val) | |
1671 | val = newval; | |
1672 | ||
1673 | args1 = TREE_CHAIN (args1); | |
1674 | args2 = TREE_CHAIN (args2); | |
1675 | } | |
1676 | } | |
400fbf9f | 1677 | \f |
400fbf9f JW |
1678 | /* Compute the size to increment a pointer by. */ |
1679 | ||
4e2fb7de | 1680 | static tree |
58f9752a | 1681 | c_size_in_bytes (const_tree type) |
400fbf9f JW |
1682 | { |
1683 | enum tree_code code = TREE_CODE (type); | |
1684 | ||
fed3cef0 RK |
1685 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) |
1686 | return size_one_node; | |
1687 | ||
d0f062fb | 1688 | if (!COMPLETE_OR_VOID_TYPE_P (type)) |
400fbf9f JW |
1689 | { |
1690 | error ("arithmetic on pointer to an incomplete type"); | |
fed3cef0 | 1691 | return size_one_node; |
400fbf9f JW |
1692 | } |
1693 | ||
1694 | /* Convert in case a char is more than one unit. */ | |
db3927fb AH |
1695 | return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
1696 | size_int (TYPE_PRECISION (char_type_node) | |
1697 | / BITS_PER_UNIT)); | |
400fbf9f | 1698 | } |
400fbf9f | 1699 | \f |
400fbf9f JW |
1700 | /* Return either DECL or its known constant value (if it has one). */ |
1701 | ||
56cb9733 | 1702 | tree |
2f6e4e97 | 1703 | decl_constant_value (tree decl) |
400fbf9f | 1704 | { |
a7c1916a | 1705 | if (/* Don't change a variable array bound or initial value to a constant |
4f976745 RK |
1706 | in a place where a variable is invalid. Note that DECL_INITIAL |
1707 | isn't valid for a PARM_DECL. */ | |
a7c1916a | 1708 | current_function_decl != 0 |
4f976745 | 1709 | && TREE_CODE (decl) != PARM_DECL |
3f75a254 | 1710 | && !TREE_THIS_VOLATILE (decl) |
83bab8db | 1711 | && TREE_READONLY (decl) |
400fbf9f JW |
1712 | && DECL_INITIAL (decl) != 0 |
1713 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1714 | /* This is invalid if initial value is not constant. | |
1715 | If it has either a function call, a memory reference, | |
1716 | or a variable, then re-evaluating it could give different results. */ | |
1717 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1718 | /* Check for cases where this is sub-optimal, even though valid. */ | |
2f74f7e9 | 1719 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
400fbf9f JW |
1720 | return DECL_INITIAL (decl); |
1721 | return decl; | |
1722 | } | |
1723 | ||
f2a71bbc JM |
1724 | /* Convert the array expression EXP to a pointer. */ |
1725 | static tree | |
c2255bc4 | 1726 | array_to_pointer_conversion (location_t loc, tree exp) |
207bf485 | 1727 | { |
f2a71bbc | 1728 | tree orig_exp = exp; |
207bf485 | 1729 | tree type = TREE_TYPE (exp); |
f2a71bbc JM |
1730 | tree adr; |
1731 | tree restype = TREE_TYPE (type); | |
1732 | tree ptrtype; | |
207bf485 | 1733 | |
f2a71bbc | 1734 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
207bf485 | 1735 | |
f2a71bbc | 1736 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1737 | |
487a92fe JM |
1738 | if (TREE_NO_WARNING (orig_exp)) |
1739 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1740 | |
f2a71bbc JM |
1741 | ptrtype = build_pointer_type (restype); |
1742 | ||
1743 | if (TREE_CODE (exp) == INDIRECT_REF) | |
1744 | return convert (ptrtype, TREE_OPERAND (exp, 0)); | |
1745 | ||
c2255bc4 | 1746 | adr = build_unary_op (loc, ADDR_EXPR, exp, 1); |
f2a71bbc JM |
1747 | return convert (ptrtype, adr); |
1748 | } | |
207bf485 | 1749 | |
f2a71bbc JM |
1750 | /* Convert the function expression EXP to a pointer. */ |
1751 | static tree | |
c2255bc4 | 1752 | function_to_pointer_conversion (location_t loc, tree exp) |
f2a71bbc JM |
1753 | { |
1754 | tree orig_exp = exp; | |
207bf485 | 1755 | |
f2a71bbc | 1756 | gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); |
207bf485 | 1757 | |
f2a71bbc | 1758 | STRIP_TYPE_NOPS (exp); |
207bf485 | 1759 | |
f2a71bbc JM |
1760 | if (TREE_NO_WARNING (orig_exp)) |
1761 | TREE_NO_WARNING (exp) = 1; | |
207bf485 | 1762 | |
c2255bc4 | 1763 | return build_unary_op (loc, ADDR_EXPR, exp, 0); |
f2a71bbc | 1764 | } |
207bf485 | 1765 | |
f2a71bbc JM |
1766 | /* Perform the default conversion of arrays and functions to pointers. |
1767 | Return the result of converting EXP. For any other expression, just | |
c2255bc4 AH |
1768 | return EXP. |
1769 | ||
1770 | LOC is the location of the expression. */ | |
f2a71bbc JM |
1771 | |
1772 | struct c_expr | |
c2255bc4 | 1773 | default_function_array_conversion (location_t loc, struct c_expr exp) |
f2a71bbc JM |
1774 | { |
1775 | tree orig_exp = exp.value; | |
1776 | tree type = TREE_TYPE (exp.value); | |
1777 | enum tree_code code = TREE_CODE (type); | |
1778 | ||
1779 | switch (code) | |
1780 | { | |
1781 | case ARRAY_TYPE: | |
1782 | { | |
1783 | bool not_lvalue = false; | |
1784 | bool lvalue_array_p; | |
1785 | ||
1786 | while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR | |
1043771b | 1787 | || CONVERT_EXPR_P (exp.value)) |
f2a71bbc JM |
1788 | && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) |
1789 | { | |
1790 | if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) | |
1791 | not_lvalue = true; | |
1792 | exp.value = TREE_OPERAND (exp.value, 0); | |
1793 | } | |
1794 | ||
1795 | if (TREE_NO_WARNING (orig_exp)) | |
1796 | TREE_NO_WARNING (exp.value) = 1; | |
1797 | ||
1798 | lvalue_array_p = !not_lvalue && lvalue_p (exp.value); | |
1799 | if (!flag_isoc99 && !lvalue_array_p) | |
1800 | { | |
1801 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1802 | Normally, using such an array would be invalid; but it can | |
1803 | be used correctly inside sizeof or as a statement expression. | |
1804 | Thus, do not give an error here; an error will result later. */ | |
1805 | return exp; | |
1806 | } | |
1807 | ||
c2255bc4 | 1808 | exp.value = array_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1809 | } |
1810 | break; | |
1811 | case FUNCTION_TYPE: | |
c2255bc4 | 1812 | exp.value = function_to_pointer_conversion (loc, exp.value); |
f2a71bbc JM |
1813 | break; |
1814 | default: | |
f2a71bbc | 1815 | break; |
207bf485 | 1816 | } |
f2a71bbc | 1817 | |
207bf485 JM |
1818 | return exp; |
1819 | } | |
1820 | ||
522ddfa2 JM |
1821 | |
1822 | /* EXP is an expression of integer type. Apply the integer promotions | |
1823 | to it and return the promoted value. */ | |
400fbf9f JW |
1824 | |
1825 | tree | |
522ddfa2 | 1826 | perform_integral_promotions (tree exp) |
400fbf9f | 1827 | { |
b3694847 SS |
1828 | tree type = TREE_TYPE (exp); |
1829 | enum tree_code code = TREE_CODE (type); | |
400fbf9f | 1830 | |
522ddfa2 | 1831 | gcc_assert (INTEGRAL_TYPE_P (type)); |
157689c6 | 1832 | |
400fbf9f JW |
1833 | /* Normally convert enums to int, |
1834 | but convert wide enums to something wider. */ | |
1835 | if (code == ENUMERAL_TYPE) | |
1836 | { | |
b0c48229 NB |
1837 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
1838 | TYPE_PRECISION (integer_type_node)), | |
1839 | ((TYPE_PRECISION (type) | |
1840 | >= TYPE_PRECISION (integer_type_node)) | |
8df83eae | 1841 | && TYPE_UNSIGNED (type))); |
05bccae2 | 1842 | |
400fbf9f JW |
1843 | return convert (type, exp); |
1844 | } | |
1845 | ||
522ddfa2 JM |
1846 | /* ??? This should no longer be needed now bit-fields have their |
1847 | proper types. */ | |
9753f113 | 1848 | if (TREE_CODE (exp) == COMPONENT_REF |
05bccae2 | 1849 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) |
cff9c407 | 1850 | /* If it's thinner than an int, promote it like a |
d72040f5 | 1851 | c_promoting_integer_type_p, otherwise leave it alone. */ |
05bccae2 RK |
1852 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), |
1853 | TYPE_PRECISION (integer_type_node))) | |
f458d1d5 | 1854 | return convert (integer_type_node, exp); |
9753f113 | 1855 | |
d72040f5 | 1856 | if (c_promoting_integer_type_p (type)) |
400fbf9f | 1857 | { |
f458d1d5 | 1858 | /* Preserve unsignedness if not really getting any wider. */ |
8df83eae | 1859 | if (TYPE_UNSIGNED (type) |
f458d1d5 | 1860 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
400fbf9f | 1861 | return convert (unsigned_type_node, exp); |
05bccae2 | 1862 | |
400fbf9f JW |
1863 | return convert (integer_type_node, exp); |
1864 | } | |
05bccae2 | 1865 | |
522ddfa2 JM |
1866 | return exp; |
1867 | } | |
1868 | ||
1869 | ||
1870 | /* Perform default promotions for C data used in expressions. | |
46bdb9cf | 1871 | Enumeral types or short or char are converted to int. |
522ddfa2 JM |
1872 | In addition, manifest constants symbols are replaced by their values. */ |
1873 | ||
1874 | tree | |
1875 | default_conversion (tree exp) | |
1876 | { | |
1877 | tree orig_exp; | |
1878 | tree type = TREE_TYPE (exp); | |
1879 | enum tree_code code = TREE_CODE (type); | |
40449a90 | 1880 | tree promoted_type; |
522ddfa2 | 1881 | |
46bdb9cf JM |
1882 | /* Functions and arrays have been converted during parsing. */ |
1883 | gcc_assert (code != FUNCTION_TYPE); | |
1884 | if (code == ARRAY_TYPE) | |
1885 | return exp; | |
522ddfa2 JM |
1886 | |
1887 | /* Constants can be used directly unless they're not loadable. */ | |
1888 | if (TREE_CODE (exp) == CONST_DECL) | |
1889 | exp = DECL_INITIAL (exp); | |
1890 | ||
522ddfa2 JM |
1891 | /* Strip no-op conversions. */ |
1892 | orig_exp = exp; | |
1893 | STRIP_TYPE_NOPS (exp); | |
1894 | ||
1895 | if (TREE_NO_WARNING (orig_exp)) | |
1896 | TREE_NO_WARNING (exp) = 1; | |
1897 | ||
400fbf9f JW |
1898 | if (code == VOID_TYPE) |
1899 | { | |
1900 | error ("void value not ignored as it ought to be"); | |
1901 | return error_mark_node; | |
1902 | } | |
808d6eaa JM |
1903 | |
1904 | exp = require_complete_type (exp); | |
1905 | if (exp == error_mark_node) | |
1906 | return error_mark_node; | |
1907 | ||
40449a90 SL |
1908 | promoted_type = targetm.promoted_type (type); |
1909 | if (promoted_type) | |
1910 | return convert (promoted_type, exp); | |
1911 | ||
808d6eaa JM |
1912 | if (INTEGRAL_TYPE_P (type)) |
1913 | return perform_integral_promotions (exp); | |
1914 | ||
400fbf9f JW |
1915 | return exp; |
1916 | } | |
1917 | \f | |
e9b2c823 NB |
1918 | /* Look up COMPONENT in a structure or union DECL. |
1919 | ||
1920 | If the component name is not found, returns NULL_TREE. Otherwise, | |
1921 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
1922 | stepping down the chain to the component, which is in the last | |
1923 | TREE_VALUE of the list. Normally the list is of length one, but if | |
1924 | the component is embedded within (nested) anonymous structures or | |
1925 | unions, the list steps down the chain to the component. */ | |
2f6e4e97 | 1926 | |
2f2d13da | 1927 | static tree |
2f6e4e97 | 1928 | lookup_field (tree decl, tree component) |
2f2d13da | 1929 | { |
e9b2c823 | 1930 | tree type = TREE_TYPE (decl); |
2f2d13da DE |
1931 | tree field; |
1932 | ||
1933 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
1934 | to the field elements. Use a binary search on this array to quickly | |
1935 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
1936 | will always be set for structures which have many elements. */ | |
1937 | ||
22a0b85f | 1938 | if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) |
2f2d13da DE |
1939 | { |
1940 | int bot, top, half; | |
d07605f5 | 1941 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; |
2f2d13da DE |
1942 | |
1943 | field = TYPE_FIELDS (type); | |
1944 | bot = 0; | |
d07605f5 | 1945 | top = TYPE_LANG_SPECIFIC (type)->s->len; |
2f2d13da DE |
1946 | while (top - bot > 1) |
1947 | { | |
2f2d13da DE |
1948 | half = (top - bot + 1) >> 1; |
1949 | field = field_array[bot+half]; | |
1950 | ||
1951 | if (DECL_NAME (field) == NULL_TREE) | |
1952 | { | |
1953 | /* Step through all anon unions in linear fashion. */ | |
1954 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
1955 | { | |
2f2d13da | 1956 | field = field_array[bot++]; |
a68b98cf RK |
1957 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE |
1958 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
19d76e60 | 1959 | { |
e9b2c823 NB |
1960 | tree anon = lookup_field (field, component); |
1961 | ||
1962 | if (anon) | |
1963 | return tree_cons (NULL_TREE, field, anon); | |
2f6e4e97 | 1964 | } |
2f2d13da DE |
1965 | } |
1966 | ||
1967 | /* Entire record is only anon unions. */ | |
1968 | if (bot > top) | |
1969 | return NULL_TREE; | |
1970 | ||
1971 | /* Restart the binary search, with new lower bound. */ | |
1972 | continue; | |
1973 | } | |
1974 | ||
e8b87aac | 1975 | if (DECL_NAME (field) == component) |
2f2d13da | 1976 | break; |
e8b87aac | 1977 | if (DECL_NAME (field) < component) |
2f2d13da DE |
1978 | bot += half; |
1979 | else | |
1980 | top = bot + half; | |
1981 | } | |
1982 | ||
1983 | if (DECL_NAME (field_array[bot]) == component) | |
1984 | field = field_array[bot]; | |
1985 | else if (DECL_NAME (field) != component) | |
e9b2c823 | 1986 | return NULL_TREE; |
2f2d13da DE |
1987 | } |
1988 | else | |
1989 | { | |
1990 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1991 | { | |
e9b2c823 NB |
1992 | if (DECL_NAME (field) == NULL_TREE |
1993 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
1994 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
2f2d13da | 1995 | { |
e9b2c823 | 1996 | tree anon = lookup_field (field, component); |
a68b98cf | 1997 | |
e9b2c823 NB |
1998 | if (anon) |
1999 | return tree_cons (NULL_TREE, field, anon); | |
2f2d13da DE |
2000 | } |
2001 | ||
2002 | if (DECL_NAME (field) == component) | |
2003 | break; | |
2004 | } | |
e9b2c823 NB |
2005 | |
2006 | if (field == NULL_TREE) | |
2007 | return NULL_TREE; | |
2f2d13da DE |
2008 | } |
2009 | ||
e9b2c823 | 2010 | return tree_cons (NULL_TREE, field, NULL_TREE); |
2f2d13da DE |
2011 | } |
2012 | ||
c2255bc4 AH |
2013 | /* Make an expression to refer to the COMPONENT field of structure or |
2014 | union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the | |
2015 | location of the COMPONENT_REF. */ | |
400fbf9f JW |
2016 | |
2017 | tree | |
c2255bc4 | 2018 | build_component_ref (location_t loc, tree datum, tree component) |
400fbf9f | 2019 | { |
b3694847 SS |
2020 | tree type = TREE_TYPE (datum); |
2021 | enum tree_code code = TREE_CODE (type); | |
2022 | tree field = NULL; | |
2023 | tree ref; | |
1e57bf47 | 2024 | bool datum_lvalue = lvalue_p (datum); |
400fbf9f | 2025 | |
7a3ea201 RH |
2026 | if (!objc_is_public (datum, component)) |
2027 | return error_mark_node; | |
2028 | ||
400fbf9f JW |
2029 | /* See if there is a field or component with name COMPONENT. */ |
2030 | ||
2031 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
2032 | { | |
d0f062fb | 2033 | if (!COMPLETE_TYPE_P (type)) |
400fbf9f | 2034 | { |
7a228918 | 2035 | c_incomplete_type_error (NULL_TREE, type); |
400fbf9f JW |
2036 | return error_mark_node; |
2037 | } | |
2038 | ||
e9b2c823 | 2039 | field = lookup_field (datum, component); |
400fbf9f JW |
2040 | |
2041 | if (!field) | |
2042 | { | |
c2255bc4 | 2043 | error_at (loc, "%qT has no member named %qE", type, component); |
400fbf9f JW |
2044 | return error_mark_node; |
2045 | } | |
400fbf9f | 2046 | |
e9b2c823 NB |
2047 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. |
2048 | This might be better solved in future the way the C++ front | |
2049 | end does it - by giving the anonymous entities each a | |
2050 | separate name and type, and then have build_component_ref | |
2051 | recursively call itself. We can't do that here. */ | |
46ea50cb | 2052 | do |
19d76e60 | 2053 | { |
e9b2c823 | 2054 | tree subdatum = TREE_VALUE (field); |
efed193e JM |
2055 | int quals; |
2056 | tree subtype; | |
1e57bf47 | 2057 | bool use_datum_quals; |
e9b2c823 NB |
2058 | |
2059 | if (TREE_TYPE (subdatum) == error_mark_node) | |
2060 | return error_mark_node; | |
2061 | ||
1e57bf47 JM |
2062 | /* If this is an rvalue, it does not have qualifiers in C |
2063 | standard terms and we must avoid propagating such | |
2064 | qualifiers down to a non-lvalue array that is then | |
2065 | converted to a pointer. */ | |
2066 | use_datum_quals = (datum_lvalue | |
2067 | || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); | |
2068 | ||
efed193e | 2069 | quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); |
1e57bf47 JM |
2070 | if (use_datum_quals) |
2071 | quals |= TYPE_QUALS (TREE_TYPE (datum)); | |
efed193e JM |
2072 | subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); |
2073 | ||
2074 | ref = build3 (COMPONENT_REF, subtype, datum, subdatum, | |
53fb4de3 | 2075 | NULL_TREE); |
c2255bc4 | 2076 | SET_EXPR_LOCATION (ref, loc); |
1e57bf47 JM |
2077 | if (TREE_READONLY (subdatum) |
2078 | || (use_datum_quals && TREE_READONLY (datum))) | |
19d76e60 | 2079 | TREE_READONLY (ref) = 1; |
1e57bf47 JM |
2080 | if (TREE_THIS_VOLATILE (subdatum) |
2081 | || (use_datum_quals && TREE_THIS_VOLATILE (datum))) | |
19d76e60 | 2082 | TREE_THIS_VOLATILE (ref) = 1; |
e23bd218 IR |
2083 | |
2084 | if (TREE_DEPRECATED (subdatum)) | |
9b86d6bb | 2085 | warn_deprecated_use (subdatum, NULL_TREE); |
e23bd218 | 2086 | |
19d76e60 | 2087 | datum = ref; |
46ea50cb RS |
2088 | |
2089 | field = TREE_CHAIN (field); | |
19d76e60 | 2090 | } |
46ea50cb | 2091 | while (field); |
19d76e60 | 2092 | |
400fbf9f JW |
2093 | return ref; |
2094 | } | |
2095 | else if (code != ERROR_MARK) | |
c2255bc4 AH |
2096 | error_at (loc, |
2097 | "request for member %qE in something not a structure or union", | |
2098 | component); | |
400fbf9f JW |
2099 | |
2100 | return error_mark_node; | |
2101 | } | |
2102 | \f | |
2103 | /* Given an expression PTR for a pointer, return an expression | |
2104 | for the value pointed to. | |
6a3799eb AH |
2105 | ERRORSTRING is the name of the operator to appear in error messages. |
2106 | ||
2107 | LOC is the location to use for the generated tree. */ | |
400fbf9f JW |
2108 | |
2109 | tree | |
dd865ef6 | 2110 | build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) |
400fbf9f | 2111 | { |
b3694847 SS |
2112 | tree pointer = default_conversion (ptr); |
2113 | tree type = TREE_TYPE (pointer); | |
6a3799eb | 2114 | tree ref; |
400fbf9f JW |
2115 | |
2116 | if (TREE_CODE (type) == POINTER_TYPE) | |
870cc33b | 2117 | { |
1043771b | 2118 | if (CONVERT_EXPR_P (pointer) |
79bedddc SR |
2119 | || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) |
2120 | { | |
2121 | /* If a warning is issued, mark it to avoid duplicates from | |
2122 | the backend. This only needs to be done at | |
2123 | warn_strict_aliasing > 2. */ | |
2124 | if (warn_strict_aliasing > 2) | |
2125 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), | |
2126 | type, TREE_OPERAND (pointer, 0))) | |
2127 | TREE_NO_WARNING (pointer) = 1; | |
2128 | } | |
2129 | ||
870cc33b | 2130 | if (TREE_CODE (pointer) == ADDR_EXPR |
870cc33b RS |
2131 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) |
2132 | == TREE_TYPE (type))) | |
6a3799eb AH |
2133 | { |
2134 | ref = TREE_OPERAND (pointer, 0); | |
2135 | protected_set_expr_location (ref, loc); | |
2136 | return ref; | |
2137 | } | |
870cc33b RS |
2138 | else |
2139 | { | |
2140 | tree t = TREE_TYPE (type); | |
46df2823 | 2141 | |
984dfd8c | 2142 | ref = build1 (INDIRECT_REF, t, pointer); |
400fbf9f | 2143 | |
baae9b65 | 2144 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) |
870cc33b | 2145 | { |
c9f9eb5d | 2146 | error_at (loc, "dereferencing pointer to incomplete type"); |
870cc33b RS |
2147 | return error_mark_node; |
2148 | } | |
7d882b83 | 2149 | if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) |
c9f9eb5d | 2150 | warning_at (loc, 0, "dereferencing %<void *%> pointer"); |
870cc33b RS |
2151 | |
2152 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
2153 | so that we get the proper error message if the result is used | |
2154 | to assign to. Also, &* is supposed to be a no-op. | |
2155 | And ANSI C seems to specify that the type of the result | |
2156 | should be the const type. */ | |
2157 | /* A de-reference of a pointer to const is not a const. It is valid | |
2158 | to change it via some other pointer. */ | |
2159 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
2160 | TREE_SIDE_EFFECTS (ref) | |
271bd540 | 2161 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); |
493692cd | 2162 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); |
6a3799eb | 2163 | protected_set_expr_location (ref, loc); |
870cc33b RS |
2164 | return ref; |
2165 | } | |
2166 | } | |
400fbf9f | 2167 | else if (TREE_CODE (pointer) != ERROR_MARK) |
dd865ef6 SZ |
2168 | switch (errstring) |
2169 | { | |
2170 | case RO_ARRAY_INDEXING: | |
2171 | error_at (loc, | |
2172 | "invalid type argument of array indexing (have %qT)", | |
2173 | type); | |
2174 | break; | |
2175 | case RO_UNARY_STAR: | |
2176 | error_at (loc, | |
2177 | "invalid type argument of unary %<*%> (have %qT)", | |
2178 | type); | |
2179 | break; | |
2180 | case RO_ARROW: | |
2181 | error_at (loc, | |
2182 | "invalid type argument of %<->%> (have %qT)", | |
2183 | type); | |
2184 | break; | |
2185 | default: | |
2186 | gcc_unreachable (); | |
2187 | } | |
400fbf9f JW |
2188 | return error_mark_node; |
2189 | } | |
2190 | ||
2191 | /* This handles expressions of the form "a[i]", which denotes | |
2192 | an array reference. | |
2193 | ||
2194 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2195 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2196 | This avoids forcing the array out of registers, and can work on | |
2197 | arrays that are not lvalues (for example, members of structures returned | |
6a3799eb AH |
2198 | by functions). |
2199 | ||
2200 | LOC is the location to use for the returned expression. */ | |
400fbf9f JW |
2201 | |
2202 | tree | |
c2255bc4 | 2203 | build_array_ref (location_t loc, tree array, tree index) |
400fbf9f | 2204 | { |
6a3799eb | 2205 | tree ret; |
a4ab7973 | 2206 | bool swapped = false; |
400fbf9f JW |
2207 | if (TREE_TYPE (array) == error_mark_node |
2208 | || TREE_TYPE (index) == error_mark_node) | |
2209 | return error_mark_node; | |
2210 | ||
a4ab7973 JM |
2211 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE |
2212 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE) | |
400fbf9f | 2213 | { |
a4ab7973 JM |
2214 | tree temp; |
2215 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE | |
2216 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
fdeefd49 | 2217 | { |
a63068b6 | 2218 | error_at (loc, "subscripted value is neither array nor pointer"); |
fdeefd49 RS |
2219 | return error_mark_node; |
2220 | } | |
a4ab7973 JM |
2221 | temp = array; |
2222 | array = index; | |
2223 | index = temp; | |
2224 | swapped = true; | |
2225 | } | |
2226 | ||
2227 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2228 | { | |
a63068b6 | 2229 | error_at (loc, "array subscript is not an integer"); |
a4ab7973 JM |
2230 | return error_mark_node; |
2231 | } | |
2232 | ||
2233 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2234 | { | |
a63068b6 | 2235 | error_at (loc, "subscripted value is pointer to function"); |
a4ab7973 JM |
2236 | return error_mark_node; |
2237 | } | |
2238 | ||
ff6b6641 GDR |
2239 | /* ??? Existing practice has been to warn only when the char |
2240 | index is syntactically the index, not for char[array]. */ | |
2241 | if (!swapped) | |
2242 | warn_array_subscript_with_type_char (index); | |
a4ab7973 JM |
2243 | |
2244 | /* Apply default promotions *after* noticing character types. */ | |
2245 | index = default_conversion (index); | |
2246 | ||
2247 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
2248 | ||
2249 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2250 | { | |
e4d35515 | 2251 | tree rval, type; |
fdeefd49 | 2252 | |
400fbf9f JW |
2253 | /* An array that is indexed by a non-constant |
2254 | cannot be stored in a register; we must be able to do | |
2255 | address arithmetic on its address. | |
2256 | Likewise an array of elements of variable size. */ | |
2257 | if (TREE_CODE (index) != INTEGER_CST | |
d0f062fb | 2258 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
400fbf9f JW |
2259 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
2260 | { | |
dffd7eb6 | 2261 | if (!c_mark_addressable (array)) |
400fbf9f JW |
2262 | return error_mark_node; |
2263 | } | |
e6d52559 JW |
2264 | /* An array that is indexed by a constant value which is not within |
2265 | the array bounds cannot be stored in a register either; because we | |
2266 | would get a crash in store_bit_field/extract_bit_field when trying | |
2267 | to access a non-existent part of the register. */ | |
2268 | if (TREE_CODE (index) == INTEGER_CST | |
eb34af89 | 2269 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3f75a254 | 2270 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) |
e6d52559 | 2271 | { |
dffd7eb6 | 2272 | if (!c_mark_addressable (array)) |
e6d52559 JW |
2273 | return error_mark_node; |
2274 | } | |
400fbf9f | 2275 | |
400fbf9f JW |
2276 | if (pedantic) |
2277 | { | |
2278 | tree foo = array; | |
2279 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2280 | foo = TREE_OPERAND (foo, 0); | |
5baeaac0 | 2281 | if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) |
b8698a0f | 2282 | pedwarn (loc, OPT_pedantic, |
fcf73884 | 2283 | "ISO C forbids subscripting %<register%> array"); |
3f75a254 | 2284 | else if (!flag_isoc99 && !lvalue_p (foo)) |
b8698a0f | 2285 | pedwarn (loc, OPT_pedantic, |
fcf73884 | 2286 | "ISO C90 forbids subscripting non-lvalue array"); |
400fbf9f JW |
2287 | } |
2288 | ||
46df2823 | 2289 | type = TREE_TYPE (TREE_TYPE (array)); |
53fb4de3 | 2290 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
400fbf9f | 2291 | /* Array ref is const/volatile if the array elements are |
c22cacf3 | 2292 | or if the array is. */ |
400fbf9f JW |
2293 | TREE_READONLY (rval) |
2294 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2295 | | TREE_READONLY (array)); | |
2296 | TREE_SIDE_EFFECTS (rval) | |
2297 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2298 | | TREE_SIDE_EFFECTS (array)); | |
2299 | TREE_THIS_VOLATILE (rval) | |
2300 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2301 | /* This was added by rms on 16 Nov 91. | |
2f6e4e97 | 2302 | It fixes vol struct foo *a; a->elts[1] |
400fbf9f JW |
2303 | in an inline function. |
2304 | Hope it doesn't break something else. */ | |
2305 | | TREE_THIS_VOLATILE (array)); | |
928c19bb | 2306 | ret = require_complete_type (rval); |
6a3799eb AH |
2307 | protected_set_expr_location (ret, loc); |
2308 | return ret; | |
400fbf9f | 2309 | } |
a4ab7973 JM |
2310 | else |
2311 | { | |
2312 | tree ar = default_conversion (array); | |
400fbf9f | 2313 | |
a4ab7973 JM |
2314 | if (ar == error_mark_node) |
2315 | return ar; | |
400fbf9f | 2316 | |
a4ab7973 JM |
2317 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); |
2318 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
400fbf9f | 2319 | |
ba47d38d | 2320 | return build_indirect_ref |
c9f9eb5d | 2321 | (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0), |
dd865ef6 | 2322 | RO_ARRAY_INDEXING); |
a4ab7973 | 2323 | } |
400fbf9f JW |
2324 | } |
2325 | \f | |
7e585d16 | 2326 | /* Build an external reference to identifier ID. FUN indicates |
766beb40 | 2327 | whether this will be used for a function call. LOC is the source |
6866c6e8 ILT |
2328 | location of the identifier. This sets *TYPE to the type of the |
2329 | identifier, which is not the same as the type of the returned value | |
2330 | for CONST_DECLs defined as enum constants. If the type of the | |
2331 | identifier is not available, *TYPE is set to NULL. */ | |
7e585d16 | 2332 | tree |
c2255bc4 | 2333 | build_external_ref (location_t loc, tree id, int fun, tree *type) |
7e585d16 ZW |
2334 | { |
2335 | tree ref; | |
2336 | tree decl = lookup_name (id); | |
16b34ad6 ZL |
2337 | |
2338 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2339 | whatever lookup_name() found. */ | |
2340 | decl = objc_lookup_ivar (decl, id); | |
7e585d16 | 2341 | |
6866c6e8 | 2342 | *type = NULL; |
339a28b9 | 2343 | if (decl && decl != error_mark_node) |
6866c6e8 ILT |
2344 | { |
2345 | ref = decl; | |
2346 | *type = TREE_TYPE (ref); | |
2347 | } | |
339a28b9 ZW |
2348 | else if (fun) |
2349 | /* Implicit function declaration. */ | |
c2255bc4 | 2350 | ref = implicitly_declare (loc, id); |
339a28b9 ZW |
2351 | else if (decl == error_mark_node) |
2352 | /* Don't complain about something that's already been | |
2353 | complained about. */ | |
2354 | return error_mark_node; | |
2355 | else | |
2356 | { | |
c2255bc4 | 2357 | undeclared_variable (loc, id); |
339a28b9 ZW |
2358 | return error_mark_node; |
2359 | } | |
7e585d16 ZW |
2360 | |
2361 | if (TREE_TYPE (ref) == error_mark_node) | |
2362 | return error_mark_node; | |
2363 | ||
339a28b9 | 2364 | if (TREE_DEPRECATED (ref)) |
9b86d6bb | 2365 | warn_deprecated_use (ref, NULL_TREE); |
339a28b9 | 2366 | |
ad960f56 | 2367 | /* Recursive call does not count as usage. */ |
b8698a0f | 2368 | if (ref != current_function_decl) |
ad960f56 | 2369 | { |
ad960f56 MLI |
2370 | TREE_USED (ref) = 1; |
2371 | } | |
7e585d16 | 2372 | |
bc4b653b JM |
2373 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) |
2374 | { | |
2375 | if (!in_sizeof && !in_typeof) | |
2376 | C_DECL_USED (ref) = 1; | |
2377 | else if (DECL_INITIAL (ref) == 0 | |
2378 | && DECL_EXTERNAL (ref) | |
2379 | && !TREE_PUBLIC (ref)) | |
2380 | record_maybe_used_decl (ref); | |
2381 | } | |
2382 | ||
7e585d16 ZW |
2383 | if (TREE_CODE (ref) == CONST_DECL) |
2384 | { | |
6193b8b7 | 2385 | used_types_insert (TREE_TYPE (ref)); |
24b97832 ILT |
2386 | |
2387 | if (warn_cxx_compat | |
2388 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2389 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2390 | { | |
2391 | warning_at (loc, OPT_Wc___compat, | |
2392 | ("enum constant defined in struct or union " | |
2393 | "is not visible in C++")); | |
2394 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2395 | } | |
2396 | ||
7e585d16 ZW |
2397 | ref = DECL_INITIAL (ref); |
2398 | TREE_CONSTANT (ref) = 1; | |
2399 | } | |
6a29edea | 2400 | else if (current_function_decl != 0 |
4b1e44be | 2401 | && !DECL_FILE_SCOPE_P (current_function_decl) |
6a29edea EB |
2402 | && (TREE_CODE (ref) == VAR_DECL |
2403 | || TREE_CODE (ref) == PARM_DECL | |
2404 | || TREE_CODE (ref) == FUNCTION_DECL)) | |
2405 | { | |
2406 | tree context = decl_function_context (ref); | |
2f6e4e97 | 2407 | |
6a29edea EB |
2408 | if (context != 0 && context != current_function_decl) |
2409 | DECL_NONLOCAL (ref) = 1; | |
2410 | } | |
71113fcd GK |
2411 | /* C99 6.7.4p3: An inline definition of a function with external |
2412 | linkage ... shall not contain a reference to an identifier with | |
2413 | internal linkage. */ | |
2414 | else if (current_function_decl != 0 | |
2415 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2416 | && DECL_EXTERNAL (current_function_decl) | |
2417 | && VAR_OR_FUNCTION_DECL_P (ref) | |
2418 | && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref)) | |
1033ffa8 JJ |
2419 | && ! TREE_PUBLIC (ref) |
2420 | && DECL_CONTEXT (ref) != current_function_decl) | |
991d6621 JM |
2421 | record_inline_static (loc, current_function_decl, ref, |
2422 | csi_internal); | |
7e585d16 ZW |
2423 | |
2424 | return ref; | |
2425 | } | |
2426 | ||
bc4b653b JM |
2427 | /* Record details of decls possibly used inside sizeof or typeof. */ |
2428 | struct maybe_used_decl | |
2429 | { | |
2430 | /* The decl. */ | |
2431 | tree decl; | |
2432 | /* The level seen at (in_sizeof + in_typeof). */ | |
2433 | int level; | |
2434 | /* The next one at this level or above, or NULL. */ | |
2435 | struct maybe_used_decl *next; | |
2436 | }; | |
2437 | ||
2438 | static struct maybe_used_decl *maybe_used_decls; | |
2439 | ||
2440 | /* Record that DECL, an undefined static function reference seen | |
2441 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2442 | a VLA type or the operand of typeof is a variably modified | |
2443 | type. */ | |
2444 | ||
4e2fb7de | 2445 | static void |
bc4b653b JM |
2446 | record_maybe_used_decl (tree decl) |
2447 | { | |
2448 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2449 | t->decl = decl; | |
2450 | t->level = in_sizeof + in_typeof; | |
2451 | t->next = maybe_used_decls; | |
2452 | maybe_used_decls = t; | |
2453 | } | |
2454 | ||
2455 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2456 | USED is false, just discard them. If it is true, mark them used | |
2457 | (if no longer inside sizeof or typeof) or move them to the next | |
2458 | level up (if still inside sizeof or typeof). */ | |
2459 | ||
2460 | void | |
2461 | pop_maybe_used (bool used) | |
2462 | { | |
2463 | struct maybe_used_decl *p = maybe_used_decls; | |
2464 | int cur_level = in_sizeof + in_typeof; | |
2465 | while (p && p->level > cur_level) | |
2466 | { | |
2467 | if (used) | |
2468 | { | |
2469 | if (cur_level == 0) | |
2470 | C_DECL_USED (p->decl) = 1; | |
2471 | else | |
2472 | p->level = cur_level; | |
2473 | } | |
2474 | p = p->next; | |
2475 | } | |
2476 | if (!used || cur_level == 0) | |
2477 | maybe_used_decls = p; | |
2478 | } | |
2479 | ||
2480 | /* Return the result of sizeof applied to EXPR. */ | |
2481 | ||
2482 | struct c_expr | |
c2255bc4 | 2483 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) |
bc4b653b JM |
2484 | { |
2485 | struct c_expr ret; | |
ad97f4be JM |
2486 | if (expr.value == error_mark_node) |
2487 | { | |
2488 | ret.value = error_mark_node; | |
2489 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 2490 | ret.original_type = NULL; |
ad97f4be JM |
2491 | pop_maybe_used (false); |
2492 | } | |
2493 | else | |
2494 | { | |
928c19bb JM |
2495 | bool expr_const_operands = true; |
2496 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, | |
2497 | &expr_const_operands); | |
c2255bc4 | 2498 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); |
ad97f4be | 2499 | ret.original_code = ERROR_MARK; |
6866c6e8 | 2500 | ret.original_type = NULL; |
928c19bb | 2501 | if (c_vla_type_p (TREE_TYPE (folded_expr))) |
52ffd86e MS |
2502 | { |
2503 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
928c19bb JM |
2504 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2505 | folded_expr, ret.value); | |
2506 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
c2255bc4 | 2507 | SET_EXPR_LOCATION (ret.value, loc); |
52ffd86e | 2508 | } |
928c19bb | 2509 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); |
ad97f4be | 2510 | } |
bc4b653b JM |
2511 | return ret; |
2512 | } | |
2513 | ||
2514 | /* Return the result of sizeof applied to T, a structure for the type | |
c2255bc4 AH |
2515 | name passed to sizeof (rather than the type itself). LOC is the |
2516 | location of the original expression. */ | |
bc4b653b JM |
2517 | |
2518 | struct c_expr | |
c2255bc4 | 2519 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) |
bc4b653b JM |
2520 | { |
2521 | tree type; | |
2522 | struct c_expr ret; | |
928c19bb JM |
2523 | tree type_expr = NULL_TREE; |
2524 | bool type_expr_const = true; | |
2525 | type = groktypename (t, &type_expr, &type_expr_const); | |
c2255bc4 | 2526 | ret.value = c_sizeof (loc, type); |
bc4b653b | 2527 | ret.original_code = ERROR_MARK; |
6866c6e8 | 2528 | ret.original_type = NULL; |
24070fcb JM |
2529 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) |
2530 | && c_vla_type_p (type)) | |
928c19bb | 2531 | { |
24070fcb JM |
2532 | /* If the type is a [*] array, it is a VLA but is represented as |
2533 | having a size of zero. In such a case we must ensure that | |
2534 | the result of sizeof does not get folded to a constant by | |
2535 | c_fully_fold, because if the size is evaluated the result is | |
2536 | not constant and so constraints on zero or negative size | |
2537 | arrays must not be applied when this sizeof call is inside | |
2538 | another array declarator. */ | |
2539 | if (!type_expr) | |
2540 | type_expr = integer_zero_node; | |
928c19bb JM |
2541 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2542 | type_expr, ret.value); | |
2543 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2544 | } | |
16464cc1 VR |
2545 | pop_maybe_used (type != error_mark_node |
2546 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
bc4b653b JM |
2547 | return ret; |
2548 | } | |
2549 | ||
400fbf9f | 2550 | /* Build a function call to function FUNCTION with parameters PARAMS. |
c2255bc4 | 2551 | The function call is at LOC. |
400fbf9f JW |
2552 | PARAMS is a list--a chain of TREE_LIST nodes--in which the |
2553 | TREE_VALUE of each node is a parameter-expression. | |
2554 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2555 | ||
2556 | tree | |
c2255bc4 | 2557 | build_function_call (location_t loc, tree function, tree params) |
bbbbb16a ILT |
2558 | { |
2559 | VEC(tree,gc) *vec; | |
2560 | tree ret; | |
2561 | ||
2562 | vec = VEC_alloc (tree, gc, list_length (params)); | |
2563 | for (; params; params = TREE_CHAIN (params)) | |
2564 | VEC_quick_push (tree, vec, TREE_VALUE (params)); | |
c2255bc4 | 2565 | ret = build_function_call_vec (loc, function, vec, NULL); |
bbbbb16a ILT |
2566 | VEC_free (tree, gc, vec); |
2567 | return ret; | |
2568 | } | |
2569 | ||
2570 | /* Build a function call to function FUNCTION with parameters PARAMS. | |
2571 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2572 | either NULL or the original type of the corresponding element in | |
2573 | PARAMS. The original type may differ from TREE_TYPE of the | |
2574 | parameter for enums. FUNCTION's data type may be a function type | |
2575 | or pointer-to-function. This function changes the elements of | |
2576 | PARAMS. */ | |
2577 | ||
2578 | tree | |
c2255bc4 | 2579 | build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params, |
bbbbb16a | 2580 | VEC(tree,gc) *origtypes) |
400fbf9f | 2581 | { |
b3694847 | 2582 | tree fntype, fundecl = 0; |
4977bab6 | 2583 | tree name = NULL_TREE, result; |
c96f4f73 | 2584 | tree tem; |
94a0dd7b SL |
2585 | int nargs; |
2586 | tree *argarray; | |
b8698a0f | 2587 | |
400fbf9f | 2588 | |
fc76e425 | 2589 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 2590 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
2591 | |
2592 | /* Convert anything with function type to a pointer-to-function. */ | |
2593 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2594 | { | |
58646b77 PB |
2595 | /* Implement type-directed function overloading for builtins. |
2596 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
2597 | handle all the type checking. The result is a complete expression | |
2598 | that implements this function call. */ | |
c2255bc4 | 2599 | tem = resolve_overloaded_builtin (loc, function, params); |
58646b77 PB |
2600 | if (tem) |
2601 | return tem; | |
48ae6c13 | 2602 | |
400fbf9f | 2603 | name = DECL_NAME (function); |
a5eadacc | 2604 | fundecl = function; |
400fbf9f | 2605 | } |
f2a71bbc | 2606 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) |
c2255bc4 | 2607 | function = function_to_pointer_conversion (loc, function); |
400fbf9f | 2608 | |
6e955430 ZL |
2609 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2610 | expressions, like those used for ObjC messenger dispatches. */ | |
bbbbb16a ILT |
2611 | if (!VEC_empty (tree, params)) |
2612 | function = objc_rewrite_function_call (function, | |
2613 | VEC_index (tree, params, 0)); | |
6e955430 | 2614 | |
928c19bb JM |
2615 | function = c_fully_fold (function, false, NULL); |
2616 | ||
400fbf9f JW |
2617 | fntype = TREE_TYPE (function); |
2618 | ||
2619 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2620 | return error_mark_node; | |
2621 | ||
2622 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2623 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2624 | { | |
c2255bc4 | 2625 | error_at (loc, "called object %qE is not a function", function); |
400fbf9f JW |
2626 | return error_mark_node; |
2627 | } | |
2628 | ||
5ce89b2e JM |
2629 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
2630 | current_function_returns_abnormally = 1; | |
2631 | ||
400fbf9f JW |
2632 | /* fntype now gets the type of function pointed to. */ |
2633 | fntype = TREE_TYPE (fntype); | |
2634 | ||
ab4194da JM |
2635 | /* Convert the parameters to the types declared in the |
2636 | function prototype, or apply default promotions. */ | |
2637 | ||
bbbbb16a ILT |
2638 | nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes, |
2639 | function, fundecl); | |
ab4194da JM |
2640 | if (nargs < 0) |
2641 | return error_mark_node; | |
2642 | ||
c96f4f73 EB |
2643 | /* Check that the function is called through a compatible prototype. |
2644 | If it is not, replace the call by a trap, wrapped up in a compound | |
2645 | expression if necessary. This has the nice side-effect to prevent | |
2646 | the tree-inliner from generating invalid assignment trees which may | |
58393038 | 2647 | blow up in the RTL expander later. */ |
1043771b | 2648 | if (CONVERT_EXPR_P (function) |
c96f4f73 EB |
2649 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR |
2650 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
3f75a254 | 2651 | && !comptypes (fntype, TREE_TYPE (tem))) |
c96f4f73 EB |
2652 | { |
2653 | tree return_type = TREE_TYPE (fntype); | |
c2255bc4 | 2654 | tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP], |
c96f4f73 | 2655 | NULL_TREE); |
ab4194da | 2656 | int i; |
c96f4f73 EB |
2657 | |
2658 | /* This situation leads to run-time undefined behavior. We can't, | |
2659 | therefore, simply error unless we can prove that all possible | |
2660 | executions of the program must execute the code. */ | |
c2255bc4 | 2661 | if (warning_at (loc, 0, "function called through a non-compatible type")) |
71205d17 MLI |
2662 | /* We can, however, treat "undefined" any way we please. |
2663 | Call abort to encourage the user to fix the program. */ | |
c2255bc4 | 2664 | inform (loc, "if this code is reached, the program will abort"); |
ab4194da JM |
2665 | /* Before the abort, allow the function arguments to exit or |
2666 | call longjmp. */ | |
2667 | for (i = 0; i < nargs; i++) | |
bbbbb16a ILT |
2668 | trap = build2 (COMPOUND_EXPR, void_type_node, |
2669 | VEC_index (tree, params, i), trap); | |
bba745c1 | 2670 | |
c96f4f73 | 2671 | if (VOID_TYPE_P (return_type)) |
3ce62965 JM |
2672 | { |
2673 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
db3927fb | 2674 | pedwarn (loc, 0, |
3ce62965 JM |
2675 | "function with qualified void return type called"); |
2676 | return trap; | |
2677 | } | |
c96f4f73 EB |
2678 | else |
2679 | { | |
2680 | tree rhs; | |
2681 | ||
2682 | if (AGGREGATE_TYPE_P (return_type)) | |
c2255bc4 | 2683 | rhs = build_compound_literal (loc, return_type, |
928c19bb JM |
2684 | build_constructor (return_type, 0), |
2685 | false); | |
c96f4f73 | 2686 | else |
db3927fb | 2687 | rhs = fold_convert_loc (loc, return_type, integer_zero_node); |
c96f4f73 | 2688 | |
3ce62965 JM |
2689 | return require_complete_type (build2 (COMPOUND_EXPR, return_type, |
2690 | trap, rhs)); | |
c96f4f73 EB |
2691 | } |
2692 | } | |
2693 | ||
bbbbb16a ILT |
2694 | argarray = VEC_address (tree, params); |
2695 | ||
83322951 RG |
2696 | /* Check that arguments to builtin functions match the expectations. */ |
2697 | if (fundecl | |
2698 | && DECL_BUILT_IN (fundecl) | |
2699 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2700 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2701 | return error_mark_node; | |
400fbf9f | 2702 | |
83322951 | 2703 | /* Check that the arguments to the function are valid. */ |
94a0dd7b | 2704 | check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray, |
10a22b11 | 2705 | TYPE_ARG_TYPES (fntype)); |
400fbf9f | 2706 | |
928c19bb JM |
2707 | if (name != NULL_TREE |
2708 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
bf730f15 | 2709 | { |
928c19bb | 2710 | if (require_constant_value) |
b8698a0f | 2711 | result = |
db3927fb AH |
2712 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), |
2713 | function, nargs, argarray); | |
928c19bb | 2714 | else |
db3927fb AH |
2715 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), |
2716 | function, nargs, argarray); | |
928c19bb JM |
2717 | if (TREE_CODE (result) == NOP_EXPR |
2718 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2719 | STRIP_TYPE_NOPS (result); | |
bf730f15 RS |
2720 | } |
2721 | else | |
db3927fb AH |
2722 | result = build_call_array_loc (loc, TREE_TYPE (fntype), |
2723 | function, nargs, argarray); | |
b0b3afb2 | 2724 | |
71653180 | 2725 | if (VOID_TYPE_P (TREE_TYPE (result))) |
3ce62965 JM |
2726 | { |
2727 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
db3927fb | 2728 | pedwarn (loc, 0, |
3ce62965 JM |
2729 | "function with qualified void return type called"); |
2730 | return result; | |
2731 | } | |
1eb8759b | 2732 | return require_complete_type (result); |
400fbf9f JW |
2733 | } |
2734 | \f | |
bbbbb16a ILT |
2735 | /* Convert the argument expressions in the vector VALUES |
2736 | to the types in the list TYPELIST. | |
400fbf9f JW |
2737 | |
2738 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
2739 | perform the default conversions. | |
2740 | ||
bbbbb16a ILT |
2741 | ORIGTYPES is the original types of the expressions in VALUES. This |
2742 | holds the type of enum values which have been converted to integral | |
2743 | types. It may be NULL. | |
400fbf9f | 2744 | |
03dafa61 JM |
2745 | FUNCTION is a tree for the called function. It is used only for |
2746 | error messages, where it is formatted with %qE. | |
400fbf9f JW |
2747 | |
2748 | This is also where warnings about wrong number of args are generated. | |
2749 | ||
94a0dd7b | 2750 | Returns the actual number of arguments processed (which may be less |
bbbbb16a ILT |
2751 | than the length of VALUES in some error situations), or -1 on |
2752 | failure. */ | |
94a0dd7b SL |
2753 | |
2754 | static int | |
bbbbb16a ILT |
2755 | convert_arguments (tree typelist, VEC(tree,gc) *values, |
2756 | VEC(tree,gc) *origtypes, tree function, tree fundecl) | |
400fbf9f | 2757 | { |
bbbbb16a ILT |
2758 | tree typetail, val; |
2759 | unsigned int parmnum; | |
06302a02 | 2760 | bool error_args = false; |
b5d32c25 KG |
2761 | const bool type_generic = fundecl |
2762 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl))); | |
8ce94e44 | 2763 | bool type_generic_remove_excess_precision = false; |
03dafa61 | 2764 | tree selector; |
03dafa61 | 2765 | |
2ac2f164 JM |
2766 | /* Change pointer to function to the function itself for |
2767 | diagnostics. */ | |
03dafa61 JM |
2768 | if (TREE_CODE (function) == ADDR_EXPR |
2769 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
2ac2f164 | 2770 | function = TREE_OPERAND (function, 0); |
03dafa61 JM |
2771 | |
2772 | /* Handle an ObjC selector specially for diagnostics. */ | |
2773 | selector = objc_message_selector (); | |
400fbf9f | 2774 | |
8ce94e44 JM |
2775 | /* For type-generic built-in functions, determine whether excess |
2776 | precision should be removed (classification) or not | |
2777 | (comparison). */ | |
2778 | if (type_generic | |
2779 | && DECL_BUILT_IN (fundecl) | |
2780 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
2781 | { | |
2782 | switch (DECL_FUNCTION_CODE (fundecl)) | |
2783 | { | |
2784 | case BUILT_IN_ISFINITE: | |
2785 | case BUILT_IN_ISINF: | |
2786 | case BUILT_IN_ISINF_SIGN: | |
2787 | case BUILT_IN_ISNAN: | |
2788 | case BUILT_IN_ISNORMAL: | |
2789 | case BUILT_IN_FPCLASSIFY: | |
2790 | type_generic_remove_excess_precision = true; | |
2791 | break; | |
2792 | ||
2793 | default: | |
2794 | type_generic_remove_excess_precision = false; | |
2795 | break; | |
2796 | } | |
2797 | } | |
2798 | ||
400fbf9f | 2799 | /* Scan the given expressions and types, producing individual |
bbbbb16a | 2800 | converted arguments. */ |
400fbf9f | 2801 | |
bbbbb16a ILT |
2802 | for (typetail = typelist, parmnum = 0; |
2803 | VEC_iterate (tree, values, parmnum, val); | |
2804 | ++parmnum) | |
400fbf9f | 2805 | { |
b3694847 | 2806 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
8ce94e44 | 2807 | tree valtype = TREE_TYPE (val); |
03dafa61 JM |
2808 | tree rname = function; |
2809 | int argnum = parmnum + 1; | |
4d3e6fae | 2810 | const char *invalid_func_diag; |
8ce94e44 | 2811 | bool excess_precision = false; |
928c19bb | 2812 | bool npc; |
bbbbb16a | 2813 | tree parmval; |
400fbf9f JW |
2814 | |
2815 | if (type == void_type_node) | |
2816 | { | |
a98c2819 MLI |
2817 | error_at (input_location, |
2818 | "too many arguments to function %qE", function); | |
2819 | if (fundecl && !DECL_BUILT_IN (fundecl)) | |
2820 | inform (DECL_SOURCE_LOCATION (fundecl), "declared here"); | |
94a0dd7b | 2821 | return parmnum; |
400fbf9f JW |
2822 | } |
2823 | ||
03dafa61 JM |
2824 | if (selector && argnum > 2) |
2825 | { | |
2826 | rname = selector; | |
2827 | argnum -= 2; | |
2828 | } | |
2829 | ||
928c19bb | 2830 | npc = null_pointer_constant_p (val); |
8ce94e44 JM |
2831 | |
2832 | /* If there is excess precision and a prototype, convert once to | |
2833 | the required type rather than converting via the semantic | |
2834 | type. Likewise without a prototype a float value represented | |
2835 | as long double should be converted once to double. But for | |
2836 | type-generic classification functions excess precision must | |
2837 | be removed here. */ | |
2838 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
2839 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
2840 | { | |
2841 | val = TREE_OPERAND (val, 0); | |
2842 | excess_precision = true; | |
2843 | } | |
928c19bb | 2844 | val = c_fully_fold (val, false, NULL); |
ed248cf7 | 2845 | STRIP_TYPE_NOPS (val); |
400fbf9f | 2846 | |
400fbf9f JW |
2847 | val = require_complete_type (val); |
2848 | ||
2849 | if (type != 0) | |
2850 | { | |
2851 | /* Formal parm type is specified by a function prototype. */ | |
400fbf9f | 2852 | |
20913689 | 2853 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) |
400fbf9f JW |
2854 | { |
2855 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
2856 | parmval = val; | |
2857 | } | |
2858 | else | |
2859 | { | |
bbbbb16a ILT |
2860 | tree origtype; |
2861 | ||
d45cf215 RS |
2862 | /* Optionally warn about conversions that |
2863 | differ from the default conversions. */ | |
05170031 | 2864 | if (warn_traditional_conversion || warn_traditional) |
400fbf9f | 2865 | { |
e3a64162 | 2866 | unsigned int formal_prec = TYPE_PRECISION (type); |
400fbf9f | 2867 | |
aae43c5f | 2868 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 2869 | && TREE_CODE (valtype) == REAL_TYPE) |
d4ee4d25 | 2870 | warning (0, "passing argument %d of %qE as integer " |
03dafa61 JM |
2871 | "rather than floating due to prototype", |
2872 | argnum, rname); | |
03829ad2 | 2873 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 2874 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
d4ee4d25 | 2875 | warning (0, "passing argument %d of %qE as integer " |
03dafa61 JM |
2876 | "rather than complex due to prototype", |
2877 | argnum, rname); | |
aae43c5f | 2878 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 2879 | && TREE_CODE (valtype) == REAL_TYPE) |
d4ee4d25 | 2880 | warning (0, "passing argument %d of %qE as complex " |
03dafa61 JM |
2881 | "rather than floating due to prototype", |
2882 | argnum, rname); | |
400fbf9f | 2883 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 2884 | && INTEGRAL_TYPE_P (valtype)) |
d4ee4d25 | 2885 | warning (0, "passing argument %d of %qE as floating " |
03dafa61 JM |
2886 | "rather than integer due to prototype", |
2887 | argnum, rname); | |
03829ad2 | 2888 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 2889 | && INTEGRAL_TYPE_P (valtype)) |
d4ee4d25 | 2890 | warning (0, "passing argument %d of %qE as complex " |
03dafa61 JM |
2891 | "rather than integer due to prototype", |
2892 | argnum, rname); | |
aae43c5f | 2893 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 2894 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
d4ee4d25 | 2895 | warning (0, "passing argument %d of %qE as floating " |
03dafa61 JM |
2896 | "rather than complex due to prototype", |
2897 | argnum, rname); | |
aae43c5f RK |
2898 | /* ??? At some point, messages should be written about |
2899 | conversions between complex types, but that's too messy | |
2900 | to do now. */ | |
d45cf215 | 2901 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 2902 | && TREE_CODE (valtype) == REAL_TYPE) |
d45cf215 RS |
2903 | { |
2904 | /* Warn if any argument is passed as `float', | |
047de90b | 2905 | since without a prototype it would be `double'. */ |
9a8ce21f JG |
2906 | if (formal_prec == TYPE_PRECISION (float_type_node) |
2907 | && type != dfloat32_type_node) | |
d4ee4d25 | 2908 | warning (0, "passing argument %d of %qE as %<float%> " |
03dafa61 JM |
2909 | "rather than %<double%> due to prototype", |
2910 | argnum, rname); | |
9a8ce21f JG |
2911 | |
2912 | /* Warn if mismatch between argument and prototype | |
2913 | for decimal float types. Warn of conversions with | |
2914 | binary float types and of precision narrowing due to | |
2915 | prototype. */ | |
8ce94e44 | 2916 | else if (type != valtype |
9a8ce21f JG |
2917 | && (type == dfloat32_type_node |
2918 | || type == dfloat64_type_node | |
c22cacf3 | 2919 | || type == dfloat128_type_node |
8ce94e44 JM |
2920 | || valtype == dfloat32_type_node |
2921 | || valtype == dfloat64_type_node | |
2922 | || valtype == dfloat128_type_node) | |
c22cacf3 | 2923 | && (formal_prec |
8ce94e44 | 2924 | <= TYPE_PRECISION (valtype) |
9a8ce21f | 2925 | || (type == dfloat128_type_node |
8ce94e44 | 2926 | && (valtype |
c22cacf3 | 2927 | != dfloat64_type_node |
8ce94e44 | 2928 | && (valtype |
9a8ce21f JG |
2929 | != dfloat32_type_node))) |
2930 | || (type == dfloat64_type_node | |
8ce94e44 | 2931 | && (valtype |
9a8ce21f JG |
2932 | != dfloat32_type_node)))) |
2933 | warning (0, "passing argument %d of %qE as %qT " | |
2934 | "rather than %qT due to prototype", | |
8ce94e44 | 2935 | argnum, rname, type, valtype); |
9a8ce21f | 2936 | |
d45cf215 | 2937 | } |
3ed56f8a KG |
2938 | /* Detect integer changing in width or signedness. |
2939 | These warnings are only activated with | |
05170031 MLI |
2940 | -Wtraditional-conversion, not with -Wtraditional. */ |
2941 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
8ce94e44 | 2942 | && INTEGRAL_TYPE_P (valtype)) |
400fbf9f | 2943 | { |
d45cf215 RS |
2944 | tree would_have_been = default_conversion (val); |
2945 | tree type1 = TREE_TYPE (would_have_been); | |
2946 | ||
754a4d82 | 2947 | if (TREE_CODE (type) == ENUMERAL_TYPE |
a38b987a | 2948 | && (TYPE_MAIN_VARIANT (type) |
8ce94e44 | 2949 | == TYPE_MAIN_VARIANT (valtype))) |
754a4d82 RS |
2950 | /* No warning if function asks for enum |
2951 | and the actual arg is that enum type. */ | |
2952 | ; | |
2953 | else if (formal_prec != TYPE_PRECISION (type1)) | |
c2255bc4 AH |
2954 | warning (OPT_Wtraditional_conversion, |
2955 | "passing argument %d of %qE " | |
3176a0c2 DD |
2956 | "with different width due to prototype", |
2957 | argnum, rname); | |
8df83eae | 2958 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
d45cf215 | 2959 | ; |
800cd3b9 RS |
2960 | /* Don't complain if the formal parameter type |
2961 | is an enum, because we can't tell now whether | |
2962 | the value was an enum--even the same enum. */ | |
2963 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
2964 | ; | |
400fbf9f JW |
2965 | else if (TREE_CODE (val) == INTEGER_CST |
2966 | && int_fits_type_p (val, type)) | |
2967 | /* Change in signedness doesn't matter | |
2968 | if a constant value is unaffected. */ | |
2969 | ; | |
ce9895ae RS |
2970 | /* If the value is extended from a narrower |
2971 | unsigned type, it doesn't matter whether we | |
2972 | pass it as signed or unsigned; the value | |
2973 | certainly is the same either way. */ | |
8ce94e44 JM |
2974 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) |
2975 | && TYPE_UNSIGNED (valtype)) | |
ce9895ae | 2976 | ; |
8df83eae | 2977 | else if (TYPE_UNSIGNED (type)) |
c2255bc4 AH |
2978 | warning (OPT_Wtraditional_conversion, |
2979 | "passing argument %d of %qE " | |
3176a0c2 DD |
2980 | "as unsigned due to prototype", |
2981 | argnum, rname); | |
3ed56f8a | 2982 | else |
c2255bc4 AH |
2983 | warning (OPT_Wtraditional_conversion, |
2984 | "passing argument %d of %qE " | |
3176a0c2 | 2985 | "as signed due to prototype", argnum, rname); |
400fbf9f JW |
2986 | } |
2987 | } | |
2988 | ||
8ce94e44 JM |
2989 | /* Possibly restore an EXCESS_PRECISION_EXPR for the |
2990 | sake of better warnings from convert_and_check. */ | |
2991 | if (excess_precision) | |
2992 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
bbbbb16a ILT |
2993 | origtype = (origtypes == NULL |
2994 | ? NULL_TREE | |
2995 | : VEC_index (tree, origtypes, parmnum)); | |
744aa42f ILT |
2996 | parmval = convert_for_assignment (input_location, type, val, |
2997 | origtype, ic_argpass, npc, | |
2ac2f164 JM |
2998 | fundecl, function, |
2999 | parmnum + 1); | |
2f6e4e97 | 3000 | |
61f71b34 | 3001 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
b6d6aa84 | 3002 | && INTEGRAL_TYPE_P (type) |
400fbf9f JW |
3003 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
3004 | parmval = default_conversion (parmval); | |
400fbf9f | 3005 | } |
400fbf9f | 3006 | } |
8ce94e44 JM |
3007 | else if (TREE_CODE (valtype) == REAL_TYPE |
3008 | && (TYPE_PRECISION (valtype) | |
c22cacf3 | 3009 | < TYPE_PRECISION (double_type_node)) |
8ce94e44 | 3010 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) |
b5d32c25 KG |
3011 | { |
3012 | if (type_generic) | |
bbbbb16a | 3013 | parmval = val; |
b5d32c25 KG |
3014 | else |
3015 | /* Convert `float' to `double'. */ | |
bbbbb16a | 3016 | parmval = convert (double_type_node, val); |
b5d32c25 | 3017 | } |
8ce94e44 JM |
3018 | else if (excess_precision && !type_generic) |
3019 | /* A "double" argument with excess precision being passed | |
3020 | without a prototype or in variable arguments. */ | |
bbbbb16a | 3021 | parmval = convert (valtype, val); |
c22cacf3 MS |
3022 | else if ((invalid_func_diag = |
3023 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
4d3e6fae FJ |
3024 | { |
3025 | error (invalid_func_diag); | |
94a0dd7b | 3026 | return -1; |
4d3e6fae | 3027 | } |
400fbf9f JW |
3028 | else |
3029 | /* Convert `short' and `char' to full-size `int'. */ | |
bbbbb16a ILT |
3030 | parmval = default_conversion (val); |
3031 | ||
3032 | VEC_replace (tree, values, parmnum, parmval); | |
06302a02 JJ |
3033 | if (parmval == error_mark_node) |
3034 | error_args = true; | |
400fbf9f JW |
3035 | |
3036 | if (typetail) | |
3037 | typetail = TREE_CHAIN (typetail); | |
3038 | } | |
3039 | ||
bbbbb16a | 3040 | gcc_assert (parmnum == VEC_length (tree, values)); |
94a0dd7b | 3041 | |
400fbf9f | 3042 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) |
3789b316 | 3043 | { |
a98c2819 MLI |
3044 | error_at (input_location, |
3045 | "too few arguments to function %qE", function); | |
3046 | if (fundecl && !DECL_BUILT_IN (fundecl)) | |
3047 | inform (DECL_SOURCE_LOCATION (fundecl), "declared here"); | |
94a0dd7b | 3048 | return -1; |
3789b316 | 3049 | } |
400fbf9f | 3050 | |
06302a02 | 3051 | return error_args ? -1 : (int) parmnum; |
400fbf9f JW |
3052 | } |
3053 | \f | |
43f6dfd3 RS |
3054 | /* This is the entry point used by the parser to build unary operators |
3055 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3056 | ARG is the operand. For unary plus, the C parser currently uses | |
6a3799eb AH |
3057 | CONVERT_EXPR for code. |
3058 | ||
3059 | LOC is the location to use for the tree generated. | |
3060 | */ | |
43f6dfd3 RS |
3061 | |
3062 | struct c_expr | |
c2255bc4 | 3063 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) |
43f6dfd3 RS |
3064 | { |
3065 | struct c_expr result; | |
3066 | ||
c9f9eb5d | 3067 | result.value = build_unary_op (loc, code, arg.value, 0); |
100d537d | 3068 | result.original_code = code; |
6866c6e8 ILT |
3069 | result.original_type = NULL; |
3070 | ||
59c0753d | 3071 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) |
c2255bc4 | 3072 | overflow_warning (loc, result.value); |
59c0753d | 3073 | |
43f6dfd3 RS |
3074 | return result; |
3075 | } | |
3076 | ||
3077 | /* This is the entry point used by the parser to build binary operators | |
3078 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3079 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3080 | expression, we check for operands that were written with other binary | |
ba47d38d AH |
3081 | operators in a way that is likely to confuse the user. |
3082 | ||
3083 | LOCATION is the location of the binary operator. */ | |
edc7c4ec | 3084 | |
487a92fe | 3085 | struct c_expr |
ba47d38d AH |
3086 | parser_build_binary_op (location_t location, enum tree_code code, |
3087 | struct c_expr arg1, struct c_expr arg2) | |
400fbf9f | 3088 | { |
487a92fe | 3089 | struct c_expr result; |
400fbf9f | 3090 | |
487a92fe JM |
3091 | enum tree_code code1 = arg1.original_code; |
3092 | enum tree_code code2 = arg2.original_code; | |
6866c6e8 ILT |
3093 | tree type1 = (arg1.original_type |
3094 | ? arg1.original_type | |
3095 | : TREE_TYPE (arg1.value)); | |
3096 | tree type2 = (arg2.original_type | |
3097 | ? arg2.original_type | |
3098 | : TREE_TYPE (arg2.value)); | |
400fbf9f | 3099 | |
ba47d38d AH |
3100 | result.value = build_binary_op (location, code, |
3101 | arg1.value, arg2.value, 1); | |
487a92fe | 3102 | result.original_code = code; |
6866c6e8 | 3103 | result.original_type = NULL; |
58bf601b | 3104 | |
487a92fe JM |
3105 | if (TREE_CODE (result.value) == ERROR_MARK) |
3106 | return result; | |
400fbf9f | 3107 | |
ba47d38d AH |
3108 | if (location != UNKNOWN_LOCATION) |
3109 | protected_set_expr_location (result.value, location); | |
3110 | ||
400fbf9f | 3111 | /* Check for cases such as x+y<<z which users are likely |
487a92fe | 3112 | to misinterpret. */ |
400fbf9f | 3113 | if (warn_parentheses) |
100d537d | 3114 | warn_about_parentheses (code, code1, arg1.value, code2, arg2.value); |
001af587 | 3115 | |
ca409efd | 3116 | if (warn_logical_op) |
a243fb4a | 3117 | warn_logical_operator (input_location, code, TREE_TYPE (result.value), |
ca409efd | 3118 | code1, arg1.value, code2, arg2.value); |
63a08740 | 3119 | |
e994a705 RS |
3120 | /* Warn about comparisons against string literals, with the exception |
3121 | of testing for equality or inequality of a string literal with NULL. */ | |
3122 | if (code == EQ_EXPR || code == NE_EXPR) | |
3123 | { | |
3124 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3125 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
c2255bc4 AH |
3126 | warning_at (location, OPT_Waddress, |
3127 | "comparison with string literal results in unspecified behavior"); | |
e994a705 RS |
3128 | } |
3129 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3130 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
c2255bc4 AH |
3131 | warning_at (location, OPT_Waddress, |
3132 | "comparison with string literal results in unspecified behavior"); | |
e994a705 | 3133 | |
b8698a0f L |
3134 | if (TREE_OVERFLOW_P (result.value) |
3135 | && !TREE_OVERFLOW_P (arg1.value) | |
59c0753d | 3136 | && !TREE_OVERFLOW_P (arg2.value)) |
c2255bc4 | 3137 | overflow_warning (location, result.value); |
400fbf9f | 3138 | |
6866c6e8 ILT |
3139 | /* Warn about comparisons of different enum types. */ |
3140 | if (warn_enum_compare | |
3141 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3142 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3143 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3144 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3145 | warning_at (location, OPT_Wenum_compare, | |
3146 | "comparison between %qT and %qT", | |
3147 | type1, type2); | |
3148 | ||
400fbf9f JW |
3149 | return result; |
3150 | } | |
3e4093b6 | 3151 | \f |
3e4093b6 RS |
3152 | /* Return a tree for the difference of pointers OP0 and OP1. |
3153 | The resulting tree has type int. */ | |
293c9fdd | 3154 | |
3e4093b6 | 3155 | static tree |
db3927fb | 3156 | pointer_diff (location_t loc, tree op0, tree op1) |
3e4093b6 | 3157 | { |
3e4093b6 | 3158 | tree restype = ptrdiff_type_node; |
36c5e70a | 3159 | tree result, inttype; |
400fbf9f | 3160 | |
36c5e70a BE |
3161 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); |
3162 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
3e4093b6 RS |
3163 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
3164 | tree con0, con1, lit0, lit1; | |
3165 | tree orig_op1 = op1; | |
400fbf9f | 3166 | |
36c5e70a BE |
3167 | /* If the operands point into different address spaces, we need to |
3168 | explicitly convert them to pointers into the common address space | |
3169 | before we can subtract the numerical address values. */ | |
3170 | if (as0 != as1) | |
3171 | { | |
3172 | addr_space_t as_common; | |
3173 | tree common_type; | |
3174 | ||
3175 | /* Determine the common superset address space. This is guaranteed | |
3176 | to exist because the caller verified that comp_target_types | |
3177 | returned non-zero. */ | |
3178 | if (!addr_space_superset (as0, as1, &as_common)) | |
3179 | gcc_unreachable (); | |
3180 | ||
3181 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3182 | op0 = convert (common_type, op0); | |
3183 | op1 = convert (common_type, op1); | |
3184 | } | |
3185 | ||
3186 | /* Determine integer type to perform computations in. This will usually | |
3187 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3188 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3189 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
3190 | inttype = lang_hooks.types.type_for_size | |
3191 | (TYPE_PRECISION (TREE_TYPE (op0)), 0); | |
3192 | else | |
3193 | inttype = restype; | |
3194 | ||
3195 | ||
fcf73884 | 3196 | if (TREE_CODE (target_type) == VOID_TYPE) |
b8698a0f | 3197 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 MLI |
3198 | "pointer of type %<void *%> used in subtraction"); |
3199 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
b8698a0f | 3200 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 | 3201 | "pointer to a function used in subtraction"); |
400fbf9f | 3202 | |
3e4093b6 RS |
3203 | /* If the conversion to ptrdiff_type does anything like widening or |
3204 | converting a partial to an integral mode, we get a convert_expression | |
3205 | that is in the way to do any simplifications. | |
3206 | (fold-const.c doesn't know that the extra bits won't be needed. | |
3207 | split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a | |
3208 | different mode in place.) | |
3209 | So first try to find a common term here 'by hand'; we want to cover | |
3210 | at least the cases that occur in legal static initializers. */ | |
1043771b | 3211 | if (CONVERT_EXPR_P (op0) |
1344f9a3 JM |
3212 | && (TYPE_PRECISION (TREE_TYPE (op0)) |
3213 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))) | |
3214 | con0 = TREE_OPERAND (op0, 0); | |
3215 | else | |
3216 | con0 = op0; | |
1043771b | 3217 | if (CONVERT_EXPR_P (op1) |
1344f9a3 JM |
3218 | && (TYPE_PRECISION (TREE_TYPE (op1)) |
3219 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))) | |
3220 | con1 = TREE_OPERAND (op1, 0); | |
3221 | else | |
3222 | con1 = op1; | |
400fbf9f | 3223 | |
3e4093b6 RS |
3224 | if (TREE_CODE (con0) == PLUS_EXPR) |
3225 | { | |
3226 | lit0 = TREE_OPERAND (con0, 1); | |
3227 | con0 = TREE_OPERAND (con0, 0); | |
3228 | } | |
3229 | else | |
3230 | lit0 = integer_zero_node; | |
400fbf9f | 3231 | |
3e4093b6 | 3232 | if (TREE_CODE (con1) == PLUS_EXPR) |
400fbf9f | 3233 | { |
3e4093b6 RS |
3234 | lit1 = TREE_OPERAND (con1, 1); |
3235 | con1 = TREE_OPERAND (con1, 0); | |
400fbf9f JW |
3236 | } |
3237 | else | |
3e4093b6 RS |
3238 | lit1 = integer_zero_node; |
3239 | ||
3240 | if (operand_equal_p (con0, con1, 0)) | |
400fbf9f | 3241 | { |
3e4093b6 RS |
3242 | op0 = lit0; |
3243 | op1 = lit1; | |
400fbf9f JW |
3244 | } |
3245 | ||
400fbf9f | 3246 | |
3e4093b6 RS |
3247 | /* First do the subtraction as integers; |
3248 | then drop through to build the divide operator. | |
3249 | Do not do default conversions on the minus operator | |
3250 | in case restype is a short type. */ | |
400fbf9f | 3251 | |
db3927fb | 3252 | op0 = build_binary_op (loc, |
36c5e70a BE |
3253 | MINUS_EXPR, convert (inttype, op0), |
3254 | convert (inttype, op1), 0); | |
3e4093b6 RS |
3255 | /* This generates an error if op1 is pointer to incomplete type. */ |
3256 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
db3927fb | 3257 | error_at (loc, "arithmetic on pointer to an incomplete type"); |
400fbf9f | 3258 | |
3e4093b6 RS |
3259 | /* This generates an error if op0 is pointer to incomplete type. */ |
3260 | op1 = c_size_in_bytes (target_type); | |
400fbf9f | 3261 | |
3e4093b6 | 3262 | /* Divide by the size, in easiest possible way. */ |
36c5e70a BE |
3263 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, |
3264 | op0, convert (inttype, op1)); | |
3265 | ||
3266 | /* Convert to final result type if necessary. */ | |
3267 | return convert (restype, result); | |
3e4093b6 RS |
3268 | } |
3269 | \f | |
3270 | /* Construct and perhaps optimize a tree representation | |
3271 | for a unary operation. CODE, a tree_code, specifies the operation | |
3272 | and XARG is the operand. | |
3273 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3274 | the default promotions (such as from short to int). | |
3275 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3276 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
c9f9eb5d AH |
3277 | arrays to pointers in C99. |
3278 | ||
3279 | LOCATION is the location of the operator. */ | |
400fbf9f | 3280 | |
3e4093b6 | 3281 | tree |
c9f9eb5d AH |
3282 | build_unary_op (location_t location, |
3283 | enum tree_code code, tree xarg, int flag) | |
3e4093b6 RS |
3284 | { |
3285 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3286 | tree arg = xarg; | |
3287 | tree argtype = 0; | |
808d6eaa | 3288 | enum tree_code typecode; |
3e4093b6 | 3289 | tree val; |
c9f9eb5d | 3290 | tree ret = error_mark_node; |
8ce94e44 | 3291 | tree eptype = NULL_TREE; |
3e4093b6 | 3292 | int noconvert = flag; |
4de67c26 | 3293 | const char *invalid_op_diag; |
928c19bb JM |
3294 | bool int_operands; |
3295 | ||
3296 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
4d84fe7c JM |
3297 | if (int_operands) |
3298 | arg = remove_c_maybe_const_expr (arg); | |
400fbf9f | 3299 | |
808d6eaa JM |
3300 | if (code != ADDR_EXPR) |
3301 | arg = require_complete_type (arg); | |
3302 | ||
3303 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
3e4093b6 RS |
3304 | if (typecode == ERROR_MARK) |
3305 | return error_mark_node; | |
3306 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3307 | typecode = INTEGER_TYPE; | |
6c36d76b | 3308 | |
4de67c26 JM |
3309 | if ((invalid_op_diag |
3310 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3311 | { | |
c9f9eb5d | 3312 | error_at (location, invalid_op_diag); |
4de67c26 JM |
3313 | return error_mark_node; |
3314 | } | |
3315 | ||
8ce94e44 JM |
3316 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
3317 | { | |
3318 | eptype = TREE_TYPE (arg); | |
3319 | arg = TREE_OPERAND (arg, 0); | |
3320 | } | |
3321 | ||
3e4093b6 RS |
3322 | switch (code) |
3323 | { | |
3324 | case CONVERT_EXPR: | |
3325 | /* This is used for unary plus, because a CONVERT_EXPR | |
3326 | is enough to prevent anybody from looking inside for | |
3327 | associativity, but won't generate any code. */ | |
3328 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3329 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
8a2cee38 | 3330 | || typecode == VECTOR_TYPE)) |
400fbf9f | 3331 | { |
c9f9eb5d | 3332 | error_at (location, "wrong type argument to unary plus"); |
3e4093b6 | 3333 | return error_mark_node; |
400fbf9f | 3334 | } |
3e4093b6 RS |
3335 | else if (!noconvert) |
3336 | arg = default_conversion (arg); | |
db3927fb | 3337 | arg = non_lvalue_loc (location, arg); |
400fbf9f JW |
3338 | break; |
3339 | ||
3e4093b6 RS |
3340 | case NEGATE_EXPR: |
3341 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3342 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
3e4093b6 RS |
3343 | || typecode == VECTOR_TYPE)) |
3344 | { | |
c9f9eb5d | 3345 | error_at (location, "wrong type argument to unary minus"); |
3e4093b6 RS |
3346 | return error_mark_node; |
3347 | } | |
3348 | else if (!noconvert) | |
3349 | arg = default_conversion (arg); | |
400fbf9f JW |
3350 | break; |
3351 | ||
3e4093b6 | 3352 | case BIT_NOT_EXPR: |
462643f0 AP |
3353 | /* ~ works on integer types and non float vectors. */ |
3354 | if (typecode == INTEGER_TYPE | |
3355 | || (typecode == VECTOR_TYPE | |
3356 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
03d5b1f5 | 3357 | { |
3e4093b6 RS |
3358 | if (!noconvert) |
3359 | arg = default_conversion (arg); | |
03d5b1f5 | 3360 | } |
3e4093b6 | 3361 | else if (typecode == COMPLEX_TYPE) |
400fbf9f | 3362 | { |
3e4093b6 | 3363 | code = CONJ_EXPR; |
b8698a0f | 3364 | pedwarn (location, OPT_pedantic, |
fcf73884 | 3365 | "ISO C does not support %<~%> for complex conjugation"); |
3e4093b6 RS |
3366 | if (!noconvert) |
3367 | arg = default_conversion (arg); | |
3368 | } | |
3369 | else | |
3370 | { | |
c9f9eb5d | 3371 | error_at (location, "wrong type argument to bit-complement"); |
3e4093b6 | 3372 | return error_mark_node; |
400fbf9f JW |
3373 | } |
3374 | break; | |
3375 | ||
3e4093b6 | 3376 | case ABS_EXPR: |
11017cc7 | 3377 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
400fbf9f | 3378 | { |
c9f9eb5d | 3379 | error_at (location, "wrong type argument to abs"); |
3e4093b6 | 3380 | return error_mark_node; |
400fbf9f | 3381 | } |
3e4093b6 RS |
3382 | else if (!noconvert) |
3383 | arg = default_conversion (arg); | |
400fbf9f JW |
3384 | break; |
3385 | ||
3e4093b6 RS |
3386 | case CONJ_EXPR: |
3387 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3388 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3389 | || typecode == COMPLEX_TYPE)) | |
400fbf9f | 3390 | { |
c9f9eb5d | 3391 | error_at (location, "wrong type argument to conjugation"); |
3e4093b6 | 3392 | return error_mark_node; |
400fbf9f | 3393 | } |
3e4093b6 RS |
3394 | else if (!noconvert) |
3395 | arg = default_conversion (arg); | |
400fbf9f JW |
3396 | break; |
3397 | ||
3e4093b6 | 3398 | case TRUTH_NOT_EXPR: |
ab22c1fa | 3399 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3400 | && typecode != REAL_TYPE && typecode != POINTER_TYPE |
46bdb9cf | 3401 | && typecode != COMPLEX_TYPE) |
400fbf9f | 3402 | { |
c9f9eb5d AH |
3403 | error_at (location, |
3404 | "wrong type argument to unary exclamation mark"); | |
3e4093b6 | 3405 | return error_mark_node; |
400fbf9f | 3406 | } |
c9f9eb5d | 3407 | arg = c_objc_common_truthvalue_conversion (location, arg); |
db3927fb | 3408 | ret = invert_truthvalue_loc (location, arg); |
ca80e52b EB |
3409 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ |
3410 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3411 | location = EXPR_LOCATION (ret); | |
c9f9eb5d | 3412 | goto return_build_unary_op; |
3e4093b6 | 3413 | |
3e4093b6 RS |
3414 | case REALPART_EXPR: |
3415 | if (TREE_CODE (arg) == COMPLEX_CST) | |
c9f9eb5d | 3416 | ret = TREE_REALPART (arg); |
3e4093b6 | 3417 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
db3927fb AH |
3418 | ret = fold_build1_loc (location, |
3419 | REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg); | |
3e4093b6 | 3420 | else |
c9f9eb5d | 3421 | ret = arg; |
8ce94e44 JM |
3422 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3423 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3424 | goto return_build_unary_op; |
605a99f6 | 3425 | |
3e4093b6 RS |
3426 | case IMAGPART_EXPR: |
3427 | if (TREE_CODE (arg) == COMPLEX_CST) | |
c9f9eb5d | 3428 | ret = TREE_IMAGPART (arg); |
3e4093b6 | 3429 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
db3927fb AH |
3430 | ret = fold_build1_loc (location, |
3431 | IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg); | |
3e4093b6 | 3432 | else |
db3927fb AH |
3433 | ret = omit_one_operand_loc (location, TREE_TYPE (arg), |
3434 | integer_zero_node, arg); | |
8ce94e44 JM |
3435 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3436 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3437 | goto return_build_unary_op; |
3e4093b6 RS |
3438 | |
3439 | case PREINCREMENT_EXPR: | |
3440 | case POSTINCREMENT_EXPR: | |
3441 | case PREDECREMENT_EXPR: | |
3442 | case POSTDECREMENT_EXPR: | |
3e4093b6 | 3443 | |
928c19bb JM |
3444 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) |
3445 | { | |
3446 | tree inner = build_unary_op (location, code, | |
3447 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3448 | if (inner == error_mark_node) | |
3449 | return error_mark_node; | |
3450 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3451 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3452 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3453 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3454 | goto return_build_unary_op; | |
3455 | } | |
3456 | ||
3457 | /* Complain about anything that is not a true lvalue. */ | |
3458 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
3459 | || code == POSTINCREMENT_EXPR) | |
3460 | ? lv_increment | |
3461 | : lv_decrement))) | |
3462 | return error_mark_node; | |
3463 | ||
09639a83 ILT |
3464 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) |
3465 | { | |
3466 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3467 | warning_at (location, OPT_Wc___compat, | |
3468 | "increment of enumeration value is invalid in C++"); | |
3469 | else | |
3470 | warning_at (location, OPT_Wc___compat, | |
3471 | "decrement of enumeration value is invalid in C++"); | |
3472 | } | |
3473 | ||
928c19bb JM |
3474 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ |
3475 | arg = c_fully_fold (arg, false, NULL); | |
3476 | ||
3e4093b6 RS |
3477 | /* Increment or decrement the real part of the value, |
3478 | and don't change the imaginary part. */ | |
3479 | if (typecode == COMPLEX_TYPE) | |
400fbf9f | 3480 | { |
3e4093b6 RS |
3481 | tree real, imag; |
3482 | ||
b8698a0f | 3483 | pedwarn (location, OPT_pedantic, |
509c9d60 | 3484 | "ISO C does not support %<++%> and %<--%> on complex types"); |
3e4093b6 RS |
3485 | |
3486 | arg = stabilize_reference (arg); | |
c9f9eb5d AH |
3487 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); |
3488 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
3489 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
a87db577 AP |
3490 | if (real == error_mark_node || imag == error_mark_node) |
3491 | return error_mark_node; | |
c9f9eb5d AH |
3492 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
3493 | real, imag); | |
3494 | goto return_build_unary_op; | |
400fbf9f | 3495 | } |
3e4093b6 RS |
3496 | |
3497 | /* Report invalid types. */ | |
3498 | ||
ab22c1fa | 3499 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3500 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE) |
400fbf9f | 3501 | { |
3e4093b6 | 3502 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c9f9eb5d | 3503 | error_at (location, "wrong type argument to increment"); |
c22cacf3 | 3504 | else |
c9f9eb5d | 3505 | error_at (location, "wrong type argument to decrement"); |
3e4093b6 RS |
3506 | |
3507 | return error_mark_node; | |
400fbf9f | 3508 | } |
400fbf9f | 3509 | |
3e4093b6 RS |
3510 | { |
3511 | tree inc; | |
400fbf9f | 3512 | |
3e4093b6 RS |
3513 | argtype = TREE_TYPE (arg); |
3514 | ||
3515 | /* Compute the increment. */ | |
3516 | ||
3517 | if (typecode == POINTER_TYPE) | |
3518 | { | |
3519 | /* If pointer target is an undefined struct, | |
3520 | we just cannot know how to do the arithmetic. */ | |
b70cef5d | 3521 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) |
3e4093b6 RS |
3522 | { |
3523 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
c9f9eb5d AH |
3524 | error_at (location, |
3525 | "increment of pointer to unknown structure"); | |
3e4093b6 | 3526 | else |
c9f9eb5d AH |
3527 | error_at (location, |
3528 | "decrement of pointer to unknown structure"); | |
3e4093b6 | 3529 | } |
b70cef5d JJ |
3530 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE |
3531 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
c22cacf3 | 3532 | { |
3e4093b6 | 3533 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
b8698a0f | 3534 | pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 | 3535 | "wrong type argument to increment"); |
3e4093b6 | 3536 | else |
b8698a0f | 3537 | pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 | 3538 | "wrong type argument to decrement"); |
3e4093b6 RS |
3539 | } |
3540 | ||
b70cef5d | 3541 | inc = c_size_in_bytes (TREE_TYPE (argtype)); |
db3927fb | 3542 | inc = fold_convert_loc (location, sizetype, inc); |
3e4093b6 | 3543 | } |
b70cef5d | 3544 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) |
ab22c1fa CF |
3545 | { |
3546 | /* For signed fract types, we invert ++ to -- or | |
3547 | -- to ++, and change inc from 1 to -1, because | |
3548 | it is not possible to represent 1 in signed fract constants. | |
3549 | For unsigned fract types, the result always overflows and | |
3550 | we get an undefined (original) or the maximum value. */ | |
3551 | if (code == PREINCREMENT_EXPR) | |
3552 | code = PREDECREMENT_EXPR; | |
3553 | else if (code == PREDECREMENT_EXPR) | |
3554 | code = PREINCREMENT_EXPR; | |
3555 | else if (code == POSTINCREMENT_EXPR) | |
3556 | code = POSTDECREMENT_EXPR; | |
3557 | else /* code == POSTDECREMENT_EXPR */ | |
3558 | code = POSTINCREMENT_EXPR; | |
3559 | ||
3560 | inc = integer_minus_one_node; | |
3561 | inc = convert (argtype, inc); | |
3562 | } | |
3e4093b6 | 3563 | else |
5be014d5 AP |
3564 | { |
3565 | inc = integer_one_node; | |
3566 | inc = convert (argtype, inc); | |
3567 | } | |
3e4093b6 | 3568 | |
3e4093b6 | 3569 | /* Report a read-only lvalue. */ |
f37acdf9 | 3570 | if (TYPE_READONLY (argtype)) |
953ff289 DN |
3571 | { |
3572 | readonly_error (arg, | |
3573 | ((code == PREINCREMENT_EXPR | |
3574 | || code == POSTINCREMENT_EXPR) | |
3575 | ? lv_increment : lv_decrement)); | |
3576 | return error_mark_node; | |
3577 | } | |
f37acdf9 JM |
3578 | else if (TREE_READONLY (arg)) |
3579 | readonly_warning (arg, | |
3580 | ((code == PREINCREMENT_EXPR | |
3581 | || code == POSTINCREMENT_EXPR) | |
3582 | ? lv_increment : lv_decrement)); | |
3e4093b6 RS |
3583 | |
3584 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) | |
3585 | val = boolean_increment (code, arg); | |
3586 | else | |
53fb4de3 | 3587 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
3e4093b6 | 3588 | TREE_SIDE_EFFECTS (val) = 1; |
3e4093b6 | 3589 | if (TREE_CODE (val) != code) |
6de9cd9a | 3590 | TREE_NO_WARNING (val) = 1; |
c9f9eb5d AH |
3591 | ret = val; |
3592 | goto return_build_unary_op; | |
3e4093b6 RS |
3593 | } |
3594 | ||
3595 | case ADDR_EXPR: | |
3596 | /* Note that this operation never does default_conversion. */ | |
3597 | ||
2b4b7036 JM |
3598 | /* The operand of unary '&' must be an lvalue (which excludes |
3599 | expressions of type void), or, in C99, the result of a [] or | |
3600 | unary '*' operator. */ | |
3601 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
3602 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
3603 | && (TREE_CODE (arg) != INDIRECT_REF | |
3604 | || !flag_isoc99)) | |
3605 | pedwarn (location, 0, "taking address of expression of type %<void%>"); | |
3606 | ||
3e4093b6 RS |
3607 | /* Let &* cancel out to simplify resulting code. */ |
3608 | if (TREE_CODE (arg) == INDIRECT_REF) | |
400fbf9f | 3609 | { |
3e4093b6 RS |
3610 | /* Don't let this be an lvalue. */ |
3611 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
db3927fb | 3612 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); |
c9f9eb5d AH |
3613 | ret = TREE_OPERAND (arg, 0); |
3614 | goto return_build_unary_op; | |
400fbf9f | 3615 | } |
1eb8759b | 3616 | |
7c672dfc | 3617 | /* For &x[y], return x+y */ |
3e4093b6 | 3618 | if (TREE_CODE (arg) == ARRAY_REF) |
1eb8759b | 3619 | { |
f2a71bbc JM |
3620 | tree op0 = TREE_OPERAND (arg, 0); |
3621 | if (!c_mark_addressable (op0)) | |
3e4093b6 | 3622 | return error_mark_node; |
c9f9eb5d | 3623 | return build_binary_op (location, PLUS_EXPR, |
f2a71bbc | 3624 | (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE |
c2255bc4 AH |
3625 | ? array_to_pointer_conversion (location, |
3626 | op0) | |
f2a71bbc | 3627 | : op0), |
7c672dfc | 3628 | TREE_OPERAND (arg, 1), 1); |
1eb8759b | 3629 | } |
1eb8759b | 3630 | |
3e4093b6 RS |
3631 | /* Anything not already handled and not a true memory reference |
3632 | or a non-lvalue array is an error. */ | |
3633 | else if (typecode != FUNCTION_TYPE && !flag | |
9bf24266 | 3634 | && !lvalue_or_else (arg, lv_addressof)) |
3e4093b6 | 3635 | return error_mark_node; |
b6a10c9f | 3636 | |
928c19bb JM |
3637 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify |
3638 | folding later. */ | |
3639 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
3640 | { | |
3641 | tree inner = build_unary_op (location, code, | |
3642 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3643 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3644 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3645 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3646 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
3647 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
3648 | goto return_build_unary_op; | |
3649 | } | |
3650 | ||
3e4093b6 RS |
3651 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
3652 | argtype = TREE_TYPE (arg); | |
400fbf9f | 3653 | |
3e4093b6 | 3654 | /* If the lvalue is const or volatile, merge that into the type |
c22cacf3 | 3655 | to which the address will point. Note that you can't get a |
3e4093b6 RS |
3656 | restricted pointer by taking the address of something, so we |
3657 | only have to deal with `const' and `volatile' here. */ | |
6615c446 | 3658 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) |
3e4093b6 RS |
3659 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))) |
3660 | argtype = c_build_type_variant (argtype, | |
3661 | TREE_READONLY (arg), | |
3662 | TREE_THIS_VOLATILE (arg)); | |
400fbf9f | 3663 | |
3e4093b6 RS |
3664 | if (!c_mark_addressable (arg)) |
3665 | return error_mark_node; | |
400fbf9f | 3666 | |
abb54d14 JM |
3667 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF |
3668 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
400fbf9f | 3669 | |
5cc200fc | 3670 | argtype = build_pointer_type (argtype); |
5e55f99d RH |
3671 | |
3672 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
3673 | when we have proper support for integer constant expressions. */ | |
3674 | val = get_base_address (arg); | |
3675 | if (val && TREE_CODE (val) == INDIRECT_REF | |
3aa2ddb8 JJ |
3676 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
3677 | { | |
db3927fb AH |
3678 | tree op0 = fold_convert_loc (location, sizetype, |
3679 | fold_offsetof (arg, val)), op1; | |
3aa2ddb8 | 3680 | |
db3927fb AH |
3681 | op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0)); |
3682 | ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0); | |
c9f9eb5d | 3683 | goto return_build_unary_op; |
3aa2ddb8 | 3684 | } |
5e55f99d | 3685 | |
5cc200fc | 3686 | val = build1 (ADDR_EXPR, argtype, arg); |
400fbf9f | 3687 | |
c9f9eb5d AH |
3688 | ret = val; |
3689 | goto return_build_unary_op; | |
400fbf9f | 3690 | |
3e4093b6 | 3691 | default: |
1344f9a3 | 3692 | gcc_unreachable (); |
3e4093b6 | 3693 | } |
400fbf9f | 3694 | |
3e4093b6 RS |
3695 | if (argtype == 0) |
3696 | argtype = TREE_TYPE (arg); | |
928c19bb JM |
3697 | if (TREE_CODE (arg) == INTEGER_CST) |
3698 | ret = (require_constant_value | |
db3927fb AH |
3699 | ? fold_build1_initializer_loc (location, code, argtype, arg) |
3700 | : fold_build1_loc (location, code, argtype, arg)); | |
928c19bb JM |
3701 | else |
3702 | ret = build1 (code, argtype, arg); | |
c9f9eb5d AH |
3703 | return_build_unary_op: |
3704 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
3705 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) |
3706 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
3707 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
3708 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
3709 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
3710 | if (eptype) |
3711 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
c9f9eb5d AH |
3712 | protected_set_expr_location (ret, location); |
3713 | return ret; | |
3e4093b6 | 3714 | } |
400fbf9f | 3715 | |
3e4093b6 RS |
3716 | /* Return nonzero if REF is an lvalue valid for this language. |
3717 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
5baeaac0 | 3718 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
400fbf9f | 3719 | |
1e4ae551 | 3720 | bool |
58f9752a | 3721 | lvalue_p (const_tree ref) |
3e4093b6 | 3722 | { |
58f9752a | 3723 | const enum tree_code code = TREE_CODE (ref); |
400fbf9f | 3724 | |
3e4093b6 RS |
3725 | switch (code) |
3726 | { | |
3727 | case REALPART_EXPR: | |
3728 | case IMAGPART_EXPR: | |
3729 | case COMPONENT_REF: | |
3730 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
400fbf9f | 3731 | |
928c19bb JM |
3732 | case C_MAYBE_CONST_EXPR: |
3733 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
3734 | ||
3e4093b6 RS |
3735 | case COMPOUND_LITERAL_EXPR: |
3736 | case STRING_CST: | |
3737 | return 1; | |
400fbf9f | 3738 | |
3e4093b6 RS |
3739 | case INDIRECT_REF: |
3740 | case ARRAY_REF: | |
3741 | case VAR_DECL: | |
3742 | case PARM_DECL: | |
3743 | case RESULT_DECL: | |
3744 | case ERROR_MARK: | |
3745 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
3746 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
665f2503 | 3747 | |
3e4093b6 | 3748 | case BIND_EXPR: |
3e4093b6 | 3749 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
665f2503 | 3750 | |
3e4093b6 RS |
3751 | default: |
3752 | return 0; | |
3753 | } | |
3754 | } | |
400fbf9f | 3755 | \f |
9bf24266 | 3756 | /* Give an error for storing in something that is 'const'. */ |
54c93c30 | 3757 | |
9bf24266 JM |
3758 | static void |
3759 | readonly_error (tree arg, enum lvalue_use use) | |
54c93c30 | 3760 | { |
5544530a PB |
3761 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement |
3762 | || use == lv_asm); | |
9bf24266 JM |
3763 | /* Using this macro rather than (for example) arrays of messages |
3764 | ensures that all the format strings are checked at compile | |
3765 | time. */ | |
5544530a | 3766 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ |
c22cacf3 | 3767 | : (use == lv_increment ? (I) \ |
5544530a | 3768 | : (use == lv_decrement ? (D) : (AS)))) |
3e4093b6 | 3769 | if (TREE_CODE (arg) == COMPONENT_REF) |
54c93c30 | 3770 | { |
3e4093b6 | 3771 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) |
9bf24266 | 3772 | readonly_error (TREE_OPERAND (arg, 0), use); |
3e4093b6 | 3773 | else |
4b794eaf JJ |
3774 | error (READONLY_MSG (G_("assignment of read-only member %qD"), |
3775 | G_("increment of read-only member %qD"), | |
5544530a PB |
3776 | G_("decrement of read-only member %qD"), |
3777 | G_("read-only member %qD used as %<asm%> output")), | |
c51a1ba9 | 3778 | TREE_OPERAND (arg, 1)); |
54c93c30 | 3779 | } |
3e4093b6 | 3780 | else if (TREE_CODE (arg) == VAR_DECL) |
4b794eaf JJ |
3781 | error (READONLY_MSG (G_("assignment of read-only variable %qD"), |
3782 | G_("increment of read-only variable %qD"), | |
5544530a PB |
3783 | G_("decrement of read-only variable %qD"), |
3784 | G_("read-only variable %qD used as %<asm%> output")), | |
c51a1ba9 | 3785 | arg); |
3e4093b6 | 3786 | else |
d7705934 DB |
3787 | error (READONLY_MSG (G_("assignment of read-only location %qE"), |
3788 | G_("increment of read-only location %qE"), | |
3789 | G_("decrement of read-only location %qE"), | |
3790 | G_("read-only location %qE used as %<asm%> output")), | |
3791 | arg); | |
54c93c30 | 3792 | } |
37dc0d8d | 3793 | |
f37acdf9 JM |
3794 | /* Give a warning for storing in something that is read-only in GCC |
3795 | terms but not const in ISO C terms. */ | |
3796 | ||
3797 | static void | |
3798 | readonly_warning (tree arg, enum lvalue_use use) | |
3799 | { | |
3800 | switch (use) | |
3801 | { | |
3802 | case lv_assign: | |
3803 | warning (0, "assignment of read-only location %qE", arg); | |
3804 | break; | |
3805 | case lv_increment: | |
3806 | warning (0, "increment of read-only location %qE", arg); | |
3807 | break; | |
3808 | case lv_decrement: | |
3809 | warning (0, "decrement of read-only location %qE", arg); | |
3810 | break; | |
3811 | default: | |
3812 | gcc_unreachable (); | |
3813 | } | |
3814 | return; | |
3815 | } | |
3816 | ||
37dc0d8d JM |
3817 | |
3818 | /* Return nonzero if REF is an lvalue valid for this language; | |
3819 | otherwise, print an error message and return zero. USE says | |
3820 | how the lvalue is being used and so selects the error message. */ | |
3821 | ||
3822 | static int | |
58f9752a | 3823 | lvalue_or_else (const_tree ref, enum lvalue_use use) |
37dc0d8d JM |
3824 | { |
3825 | int win = lvalue_p (ref); | |
3826 | ||
3827 | if (!win) | |
3828 | lvalue_error (use); | |
3829 | ||
3830 | return win; | |
3831 | } | |
3e4093b6 RS |
3832 | \f |
3833 | /* Mark EXP saying that we need to be able to take the | |
3834 | address of it; it should not be allocated in a register. | |
3835 | Returns true if successful. */ | |
54c93c30 | 3836 | |
3e4093b6 RS |
3837 | bool |
3838 | c_mark_addressable (tree exp) | |
400fbf9f | 3839 | { |
3e4093b6 | 3840 | tree x = exp; |
95602da1 | 3841 | |
3e4093b6 RS |
3842 | while (1) |
3843 | switch (TREE_CODE (x)) | |
3844 | { | |
3845 | case COMPONENT_REF: | |
3846 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
3847 | { | |
0039fa55 AN |
3848 | error |
3849 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
3e4093b6 RS |
3850 | return false; |
3851 | } | |
95602da1 | 3852 | |
3e4093b6 | 3853 | /* ... fall through ... */ |
95602da1 | 3854 | |
3e4093b6 RS |
3855 | case ADDR_EXPR: |
3856 | case ARRAY_REF: | |
3857 | case REALPART_EXPR: | |
3858 | case IMAGPART_EXPR: | |
3859 | x = TREE_OPERAND (x, 0); | |
3860 | break; | |
95602da1 | 3861 | |
3e4093b6 RS |
3862 | case COMPOUND_LITERAL_EXPR: |
3863 | case CONSTRUCTOR: | |
3864 | TREE_ADDRESSABLE (x) = 1; | |
3865 | return true; | |
95602da1 | 3866 | |
3e4093b6 RS |
3867 | case VAR_DECL: |
3868 | case CONST_DECL: | |
3869 | case PARM_DECL: | |
3870 | case RESULT_DECL: | |
5baeaac0 | 3871 | if (C_DECL_REGISTER (x) |
3e4093b6 RS |
3872 | && DECL_NONLOCAL (x)) |
3873 | { | |
e697b20f | 3874 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
3e4093b6 | 3875 | { |
0039fa55 AN |
3876 | error |
3877 | ("global register variable %qD used in nested function", x); | |
3e4093b6 RS |
3878 | return false; |
3879 | } | |
509c9d60 | 3880 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); |
3e4093b6 | 3881 | } |
5baeaac0 | 3882 | else if (C_DECL_REGISTER (x)) |
3e4093b6 | 3883 | { |
e697b20f | 3884 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
0039fa55 AN |
3885 | error ("address of global register variable %qD requested", x); |
3886 | else | |
3887 | error ("address of register variable %qD requested", x); | |
3888 | return false; | |
3e4093b6 | 3889 | } |
400fbf9f | 3890 | |
3e4093b6 RS |
3891 | /* drops in */ |
3892 | case FUNCTION_DECL: | |
3893 | TREE_ADDRESSABLE (x) = 1; | |
3894 | /* drops out */ | |
3895 | default: | |
3896 | return true; | |
3897 | } | |
3898 | } | |
3899 | \f | |
928c19bb JM |
3900 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If |
3901 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
3902 | if folded to an integer constant then the unselected half may | |
3903 | contain arbitrary operations not normally permitted in constant | |
c2255bc4 | 3904 | expressions. Set the location of the expression to LOC. */ |
400fbf9f JW |
3905 | |
3906 | tree | |
744aa42f | 3907 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, |
d130ae11 ILT |
3908 | tree op1, tree op1_original_type, tree op2, |
3909 | tree op2_original_type) | |
400fbf9f | 3910 | { |
3e4093b6 RS |
3911 | tree type1; |
3912 | tree type2; | |
3913 | enum tree_code code1; | |
3914 | enum tree_code code2; | |
3915 | tree result_type = NULL; | |
8ce94e44 | 3916 | tree ep_result_type = NULL; |
3e4093b6 | 3917 | tree orig_op1 = op1, orig_op2 = op2; |
928c19bb | 3918 | bool int_const, op1_int_operands, op2_int_operands, int_operands; |
4d84fe7c | 3919 | bool ifexp_int_operands; |
928c19bb | 3920 | tree ret; |
4ea80a41 | 3921 | bool objc_ok; |
400fbf9f | 3922 | |
4d84fe7c JM |
3923 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); |
3924 | if (op1_int_operands) | |
3925 | op1 = remove_c_maybe_const_expr (op1); | |
3926 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
3927 | if (op2_int_operands) | |
3928 | op2 = remove_c_maybe_const_expr (op2); | |
3929 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
3930 | if (ifexp_int_operands) | |
3931 | ifexp = remove_c_maybe_const_expr (ifexp); | |
3932 | ||
3e4093b6 RS |
3933 | /* Promote both alternatives. */ |
3934 | ||
3935 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
3936 | op1 = default_conversion (op1); | |
3937 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
3938 | op2 = default_conversion (op2); | |
3939 | ||
3940 | if (TREE_CODE (ifexp) == ERROR_MARK | |
3941 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
3942 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
400fbf9f | 3943 | return error_mark_node; |
400fbf9f | 3944 | |
3e4093b6 RS |
3945 | type1 = TREE_TYPE (op1); |
3946 | code1 = TREE_CODE (type1); | |
3947 | type2 = TREE_TYPE (op2); | |
3948 | code2 = TREE_CODE (type2); | |
3949 | ||
b1adf557 JM |
3950 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
3951 | In C99 they will be pointers by now. */ | |
3952 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
3953 | { | |
744aa42f | 3954 | error_at (colon_loc, "non-lvalue array in conditional expression"); |
b1adf557 JM |
3955 | return error_mark_node; |
3956 | } | |
3957 | ||
4ea80a41 DA |
3958 | objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE); |
3959 | ||
8ce94e44 JM |
3960 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR |
3961 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
3962 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
3963 | || code1 == COMPLEX_TYPE) | |
3964 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
3965 | || code2 == COMPLEX_TYPE)) | |
3966 | { | |
3967 | ep_result_type = c_common_type (type1, type2); | |
3968 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
3969 | { | |
3970 | op1 = TREE_OPERAND (op1, 0); | |
3971 | type1 = TREE_TYPE (op1); | |
3972 | gcc_assert (TREE_CODE (type1) == code1); | |
3973 | } | |
3974 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
3975 | { | |
3976 | op2 = TREE_OPERAND (op2, 0); | |
3977 | type2 = TREE_TYPE (op2); | |
3978 | gcc_assert (TREE_CODE (type2) == code2); | |
3979 | } | |
3980 | } | |
3981 | ||
d130ae11 ILT |
3982 | if (warn_cxx_compat) |
3983 | { | |
3984 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
3985 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
3986 | ||
3987 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
3988 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
3989 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
3990 | warning_at (colon_loc, OPT_Wc___compat, | |
3991 | ("different enum types in conditional is " | |
3992 | "invalid in C++: %qT vs %qT"), | |
3993 | t1, t2); | |
3994 | } | |
3995 | ||
3e4093b6 RS |
3996 | /* Quickly detect the usual case where op1 and op2 have the same type |
3997 | after promotion. */ | |
3998 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
400fbf9f | 3999 | { |
3e4093b6 RS |
4000 | if (type1 == type2) |
4001 | result_type = type1; | |
4002 | else | |
4003 | result_type = TYPE_MAIN_VARIANT (type1); | |
4004 | } | |
4005 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
c22cacf3 MS |
4006 | || code1 == COMPLEX_TYPE) |
4007 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4008 | || code2 == COMPLEX_TYPE)) | |
3e4093b6 | 4009 | { |
ccf7f880 | 4010 | result_type = c_common_type (type1, type2); |
400fbf9f | 4011 | |
3e4093b6 RS |
4012 | /* If -Wsign-compare, warn here if type1 and type2 have |
4013 | different signedness. We'll promote the signed to unsigned | |
4014 | and later code won't know it used to be different. | |
4015 | Do this check on the original types, so that explicit casts | |
4016 | will be considered, but default promotions won't. */ | |
7d882b83 | 4017 | if (c_inhibit_evaluation_warnings == 0) |
ab87f8c8 | 4018 | { |
8df83eae RK |
4019 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
4020 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
400fbf9f | 4021 | |
3e4093b6 RS |
4022 | if (unsigned_op1 ^ unsigned_op2) |
4023 | { | |
6ac01510 ILT |
4024 | bool ovf; |
4025 | ||
3e4093b6 RS |
4026 | /* Do not warn if the result type is signed, since the |
4027 | signed type will only be chosen if it can represent | |
4028 | all the values of the unsigned type. */ | |
3f75a254 | 4029 | if (!TYPE_UNSIGNED (result_type)) |
3e4093b6 | 4030 | /* OK */; |
3e4093b6 | 4031 | else |
928c19bb JM |
4032 | { |
4033 | bool op1_maybe_const = true; | |
4034 | bool op2_maybe_const = true; | |
4035 | ||
4036 | /* Do not warn if the signed quantity is an | |
4037 | unsuffixed integer literal (or some static | |
4038 | constant expression involving such literals) and | |
4039 | it is non-negative. This warning requires the | |
4040 | operands to be folded for best results, so do | |
4041 | that folding in this case even without | |
4042 | warn_sign_compare to avoid warning options | |
4043 | possibly affecting code generation. */ | |
f5178456 RS |
4044 | c_inhibit_evaluation_warnings |
4045 | += (ifexp == truthvalue_false_node); | |
928c19bb JM |
4046 | op1 = c_fully_fold (op1, require_constant_value, |
4047 | &op1_maybe_const); | |
f5178456 RS |
4048 | c_inhibit_evaluation_warnings |
4049 | -= (ifexp == truthvalue_false_node); | |
4050 | ||
4051 | c_inhibit_evaluation_warnings | |
4052 | += (ifexp == truthvalue_true_node); | |
928c19bb JM |
4053 | op2 = c_fully_fold (op2, require_constant_value, |
4054 | &op2_maybe_const); | |
f5178456 RS |
4055 | c_inhibit_evaluation_warnings |
4056 | -= (ifexp == truthvalue_true_node); | |
928c19bb JM |
4057 | |
4058 | if (warn_sign_compare) | |
4059 | { | |
4060 | if ((unsigned_op2 | |
4061 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4062 | || (unsigned_op1 | |
4063 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4064 | /* OK */; | |
4065 | else | |
744aa42f ILT |
4066 | warning_at (colon_loc, OPT_Wsign_compare, |
4067 | ("signed and unsigned type in " | |
4068 | "conditional expression")); | |
928c19bb JM |
4069 | } |
4070 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
e5a94231 | 4071 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 4072 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) |
e5a94231 | 4073 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); |
928c19bb | 4074 | } |
3e4093b6 RS |
4075 | } |
4076 | } | |
4077 | } | |
4078 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4079 | { | |
fcf73884 | 4080 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) |
744aa42f | 4081 | pedwarn (colon_loc, OPT_pedantic, |
fcf73884 | 4082 | "ISO C forbids conditional expr with only one void side"); |
3e4093b6 RS |
4083 | result_type = void_type_node; |
4084 | } | |
4085 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4086 | { | |
36c5e70a BE |
4087 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); |
4088 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4089 | addr_space_t as_common; | |
4090 | ||
744aa42f | 4091 | if (comp_target_types (colon_loc, type1, type2)) |
10bc1b1b | 4092 | result_type = common_pointer_type (type1, type2); |
6aa3c60d | 4093 | else if (null_pointer_constant_p (orig_op1)) |
36c5e70a | 4094 | result_type = type2; |
6aa3c60d | 4095 | else if (null_pointer_constant_p (orig_op2)) |
36c5e70a BE |
4096 | result_type = type1; |
4097 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4098 | { | |
4099 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4100 | "used in conditional expression"); | |
4101 | return error_mark_node; | |
4102 | } | |
3e4093b6 | 4103 | else if (VOID_TYPE_P (TREE_TYPE (type1))) |
34a80643 | 4104 | { |
fcf73884 | 4105 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
744aa42f | 4106 | pedwarn (colon_loc, OPT_pedantic, |
509c9d60 | 4107 | "ISO C forbids conditional expr between " |
bda67431 | 4108 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4109 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), |
4110 | TREE_TYPE (type2))); | |
34a80643 | 4111 | } |
3e4093b6 | 4112 | else if (VOID_TYPE_P (TREE_TYPE (type2))) |
1c2a9b35 | 4113 | { |
fcf73884 | 4114 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
744aa42f | 4115 | pedwarn (colon_loc, OPT_pedantic, |
509c9d60 | 4116 | "ISO C forbids conditional expr between " |
bda67431 | 4117 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4118 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), |
4119 | TREE_TYPE (type1))); | |
1c2a9b35 | 4120 | } |
34a80643 | 4121 | else |
ab87f8c8 | 4122 | { |
36c5e70a BE |
4123 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
4124 | ||
4ea80a41 | 4125 | if (!objc_ok) |
744aa42f | 4126 | pedwarn (colon_loc, 0, |
4ea80a41 | 4127 | "pointer type mismatch in conditional expression"); |
36c5e70a BE |
4128 | result_type = build_pointer_type |
4129 | (build_qualified_type (void_type_node, qual)); | |
ab87f8c8 | 4130 | } |
3e4093b6 RS |
4131 | } |
4132 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4133 | { | |
6aa3c60d | 4134 | if (!null_pointer_constant_p (orig_op2)) |
744aa42f | 4135 | pedwarn (colon_loc, 0, |
509c9d60 | 4136 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4137 | else |
ab87f8c8 | 4138 | { |
3e4093b6 | 4139 | op2 = null_pointer_node; |
ab87f8c8 | 4140 | } |
3e4093b6 RS |
4141 | result_type = type1; |
4142 | } | |
4143 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4144 | { | |
6aa3c60d | 4145 | if (!null_pointer_constant_p (orig_op1)) |
744aa42f | 4146 | pedwarn (colon_loc, 0, |
509c9d60 | 4147 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4148 | else |
ab87f8c8 | 4149 | { |
3e4093b6 | 4150 | op1 = null_pointer_node; |
ab87f8c8 | 4151 | } |
3e4093b6 RS |
4152 | result_type = type2; |
4153 | } | |
1c2a9b35 | 4154 | |
3e4093b6 RS |
4155 | if (!result_type) |
4156 | { | |
4157 | if (flag_cond_mismatch) | |
4158 | result_type = void_type_node; | |
4159 | else | |
400fbf9f | 4160 | { |
c2255bc4 | 4161 | error_at (colon_loc, "type mismatch in conditional expression"); |
ab87f8c8 | 4162 | return error_mark_node; |
400fbf9f | 4163 | } |
3e4093b6 | 4164 | } |
400fbf9f | 4165 | |
3e4093b6 RS |
4166 | /* Merge const and volatile flags of the incoming types. */ |
4167 | result_type | |
4168 | = build_type_variant (result_type, | |
afbd0665 AS |
4169 | TYPE_READONLY (type1) || TYPE_READONLY (type2), |
4170 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
b6a10c9f | 4171 | |
afbd0665 | 4172 | if (result_type != type1) |
3e4093b6 | 4173 | op1 = convert_and_check (result_type, op1); |
afbd0665 | 4174 | if (result_type != type2) |
3e4093b6 | 4175 | op2 = convert_and_check (result_type, op2); |
b6a10c9f | 4176 | |
928c19bb JM |
4177 | if (ifexp_bcp && ifexp == truthvalue_true_node) |
4178 | { | |
4179 | op2_int_operands = true; | |
4180 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4181 | } | |
4182 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4183 | { | |
4184 | op1_int_operands = true; | |
4185 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4186 | } | |
4d84fe7c | 4187 | int_const = int_operands = (ifexp_int_operands |
928c19bb JM |
4188 | && op1_int_operands |
4189 | && op2_int_operands); | |
4190 | if (int_operands) | |
4191 | { | |
4192 | int_const = ((ifexp == truthvalue_true_node | |
4193 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4194 | && !TREE_OVERFLOW (orig_op1)) | |
4195 | || (ifexp == truthvalue_false_node | |
4196 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4197 | && !TREE_OVERFLOW (orig_op2))); | |
4198 | } | |
4199 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) | |
db3927fb | 4200 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); |
928c19bb JM |
4201 | else |
4202 | { | |
4203 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); | |
4204 | if (int_operands) | |
4205 | ret = note_integer_operands (ret); | |
4206 | } | |
8ce94e44 JM |
4207 | if (ep_result_type) |
4208 | ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret); | |
928c19bb | 4209 | |
c2255bc4 | 4210 | protected_set_expr_location (ret, colon_loc); |
928c19bb | 4211 | return ret; |
3e4093b6 RS |
4212 | } |
4213 | \f | |
487a92fe | 4214 | /* Return a compound expression that performs two expressions and |
c2255bc4 AH |
4215 | returns the value of the second of them. |
4216 | ||
4217 | LOC is the location of the COMPOUND_EXPR. */ | |
400fbf9f | 4218 | |
3e4093b6 | 4219 | tree |
c2255bc4 | 4220 | build_compound_expr (location_t loc, tree expr1, tree expr2) |
3e4093b6 | 4221 | { |
4d84fe7c | 4222 | bool expr1_int_operands, expr2_int_operands; |
8ce94e44 | 4223 | tree eptype = NULL_TREE; |
928c19bb JM |
4224 | tree ret; |
4225 | ||
4d84fe7c JM |
4226 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); |
4227 | if (expr1_int_operands) | |
4228 | expr1 = remove_c_maybe_const_expr (expr1); | |
4229 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4230 | if (expr2_int_operands) | |
4231 | expr2 = remove_c_maybe_const_expr (expr2); | |
4232 | ||
8ce94e44 JM |
4233 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) |
4234 | expr1 = TREE_OPERAND (expr1, 0); | |
4235 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4236 | { | |
4237 | eptype = TREE_TYPE (expr2); | |
4238 | expr2 = TREE_OPERAND (expr2, 0); | |
4239 | } | |
4240 | ||
3f75a254 | 4241 | if (!TREE_SIDE_EFFECTS (expr1)) |
3e4093b6 RS |
4242 | { |
4243 | /* The left-hand operand of a comma expression is like an expression | |
c5409249 | 4244 | statement: with -Wunused, we should warn if it doesn't have |
3e4093b6 | 4245 | any side-effects, unless it was explicitly cast to (void). */ |
e14a6540 | 4246 | if (warn_unused_value) |
47aecf47 | 4247 | { |
e14a6540 | 4248 | if (VOID_TYPE_P (TREE_TYPE (expr1)) |
1043771b | 4249 | && CONVERT_EXPR_P (expr1)) |
47aecf47 | 4250 | ; /* (void) a, b */ |
e14a6540 JM |
4251 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) |
4252 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
1043771b | 4253 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) |
47aecf47 JM |
4254 | ; /* (void) a, (void) b, c */ |
4255 | else | |
b8698a0f | 4256 | warning_at (loc, OPT_Wunused_value, |
c2255bc4 | 4257 | "left-hand operand of comma expression has no effect"); |
47aecf47 | 4258 | } |
3e4093b6 | 4259 | } |
400fbf9f | 4260 | |
3e4093b6 RS |
4261 | /* With -Wunused, we should also warn if the left-hand operand does have |
4262 | side-effects, but computes a value which is not used. For example, in | |
4263 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4264 | so we should issue a warning. */ | |
4265 | else if (warn_unused_value) | |
c2255bc4 | 4266 | warn_if_unused_value (expr1, loc); |
400fbf9f | 4267 | |
e63d6886 AP |
4268 | if (expr2 == error_mark_node) |
4269 | return error_mark_node; | |
4270 | ||
928c19bb JM |
4271 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
4272 | ||
4273 | if (flag_isoc99 | |
4d84fe7c JM |
4274 | && expr1_int_operands |
4275 | && expr2_int_operands) | |
928c19bb JM |
4276 | ret = note_integer_operands (ret); |
4277 | ||
8ce94e44 JM |
4278 | if (eptype) |
4279 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4280 | ||
c2255bc4 | 4281 | protected_set_expr_location (ret, loc); |
928c19bb | 4282 | return ret; |
3e4093b6 | 4283 | } |
400fbf9f | 4284 | |
67165eb3 ILT |
4285 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to |
4286 | which we are casting. OTYPE is the type of the expression being | |
4287 | cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared | |
36c5e70a BE |
4288 | on the command line. Named address space qualifiers are not handled |
4289 | here, because they result in different warnings. */ | |
67165eb3 ILT |
4290 | |
4291 | static void | |
4292 | handle_warn_cast_qual (tree type, tree otype) | |
4293 | { | |
4294 | tree in_type = type; | |
4295 | tree in_otype = otype; | |
4296 | int added = 0; | |
4297 | int discarded = 0; | |
4298 | bool is_const; | |
4299 | ||
4300 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4301 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4302 | nodes is uninteresting and we stop as soon as we hit a | |
4303 | non-POINTER_TYPE node on either type. */ | |
4304 | do | |
4305 | { | |
4306 | in_otype = TREE_TYPE (in_otype); | |
4307 | in_type = TREE_TYPE (in_type); | |
4308 | ||
4309 | /* GNU C allows cv-qualified function types. 'const' means the | |
4310 | function is very pure, 'volatile' means it can't return. We | |
4311 | need to warn when such qualifiers are added, not when they're | |
4312 | taken away. */ | |
4313 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4314 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
36c5e70a BE |
4315 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) |
4316 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
67165eb3 | 4317 | else |
36c5e70a BE |
4318 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) |
4319 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
67165eb3 ILT |
4320 | } |
4321 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4322 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4323 | ||
4324 | if (added) | |
4325 | warning (OPT_Wcast_qual, "cast adds new qualifiers to function type"); | |
4326 | ||
4327 | if (discarded) | |
4328 | /* There are qualifiers present in IN_OTYPE that are not present | |
4329 | in IN_TYPE. */ | |
4330 | warning (OPT_Wcast_qual, | |
4331 | "cast discards qualifiers from pointer target type"); | |
4332 | ||
4333 | if (added || discarded) | |
4334 | return; | |
4335 | ||
4336 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4337 | const value to be changed with no additional warning. We only | |
4338 | issue this warning if T is the same on both sides, and we only | |
4339 | issue the warning if there are the same number of pointers on | |
4340 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4341 | cast is unsafe when a qualifier is added at one level and const | |
4342 | is not present at all outer levels. | |
4343 | ||
4344 | To issue this warning, we check at each level whether the cast | |
4345 | adds new qualifiers not already seen. We don't need to special | |
4346 | case function types, as they won't have the same | |
4347 | TYPE_MAIN_VARIANT. */ | |
4348 | ||
4349 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4350 | return; | |
4351 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4352 | return; | |
4353 | ||
4354 | in_type = type; | |
4355 | in_otype = otype; | |
4356 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4357 | do | |
4358 | { | |
4359 | in_type = TREE_TYPE (in_type); | |
4360 | in_otype = TREE_TYPE (in_otype); | |
4361 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4362 | && !is_const) | |
4363 | { | |
4364 | warning (OPT_Wcast_qual, | |
4365 | ("new qualifiers in middle of multi-level non-const cast " | |
4366 | "are unsafe")); | |
4367 | break; | |
4368 | } | |
4369 | if (is_const) | |
4370 | is_const = TYPE_READONLY (in_type); | |
4371 | } | |
4372 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4373 | } | |
4374 | ||
b8698a0f | 4375 | /* Build an expression representing a cast to type TYPE of expression EXPR. |
c2255bc4 | 4376 | LOC is the location of the cast-- typically the open paren of the cast. */ |
400fbf9f | 4377 | |
3e4093b6 | 4378 | tree |
c2255bc4 | 4379 | build_c_cast (location_t loc, tree type, tree expr) |
3e4093b6 | 4380 | { |
8ce94e44 JM |
4381 | tree value; |
4382 | ||
4383 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
4384 | expr = TREE_OPERAND (expr, 0); | |
4385 | ||
4386 | value = expr; | |
400fbf9f | 4387 | |
3e4093b6 RS |
4388 | if (type == error_mark_node || expr == error_mark_node) |
4389 | return error_mark_node; | |
400fbf9f | 4390 | |
3e4093b6 RS |
4391 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
4392 | only in <protocol> qualifications. But when constructing cast expressions, | |
4393 | the protocols do matter and must be kept around. */ | |
700686fa ZL |
4394 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) |
4395 | return build1 (NOP_EXPR, type, expr); | |
4396 | ||
4397 | type = TYPE_MAIN_VARIANT (type); | |
400fbf9f | 4398 | |
3e4093b6 RS |
4399 | if (TREE_CODE (type) == ARRAY_TYPE) |
4400 | { | |
c2255bc4 | 4401 | error_at (loc, "cast specifies array type"); |
3e4093b6 RS |
4402 | return error_mark_node; |
4403 | } | |
400fbf9f | 4404 | |
3e4093b6 RS |
4405 | if (TREE_CODE (type) == FUNCTION_TYPE) |
4406 | { | |
c2255bc4 | 4407 | error_at (loc, "cast specifies function type"); |
3e4093b6 RS |
4408 | return error_mark_node; |
4409 | } | |
400fbf9f | 4410 | |
808d6eaa JM |
4411 | if (!VOID_TYPE_P (type)) |
4412 | { | |
4413 | value = require_complete_type (value); | |
4414 | if (value == error_mark_node) | |
4415 | return error_mark_node; | |
4416 | } | |
4417 | ||
3e4093b6 RS |
4418 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
4419 | { | |
fcf73884 MLI |
4420 | if (TREE_CODE (type) == RECORD_TYPE |
4421 | || TREE_CODE (type) == UNION_TYPE) | |
b8698a0f | 4422 | pedwarn (loc, OPT_pedantic, |
fcf73884 | 4423 | "ISO C forbids casting nonscalar to the same type"); |
3e4093b6 RS |
4424 | } |
4425 | else if (TREE_CODE (type) == UNION_TYPE) | |
4426 | { | |
4427 | tree field; | |
400fbf9f | 4428 | |
3e4093b6 | 4429 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
39ffbac9 VR |
4430 | if (TREE_TYPE (field) != error_mark_node |
4431 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
4432 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3e4093b6 RS |
4433 | break; |
4434 | ||
4435 | if (field) | |
400fbf9f | 4436 | { |
3e4093b6 | 4437 | tree t; |
e616f54d | 4438 | bool maybe_const = true; |
3e4093b6 | 4439 | |
c2255bc4 | 4440 | pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type"); |
e616f54d JM |
4441 | t = c_fully_fold (value, false, &maybe_const); |
4442 | t = build_constructor_single (type, field, t); | |
4443 | if (!maybe_const) | |
4444 | t = c_wrap_maybe_const (t, true); | |
4445 | t = digest_init (loc, type, t, | |
bbbbb16a | 4446 | NULL_TREE, false, true, 0); |
3e4093b6 RS |
4447 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
4448 | return t; | |
400fbf9f | 4449 | } |
c2255bc4 | 4450 | error_at (loc, "cast to union type from type not present in union"); |
3e4093b6 RS |
4451 | return error_mark_node; |
4452 | } | |
4453 | else | |
4454 | { | |
4455 | tree otype, ovalue; | |
400fbf9f | 4456 | |
3e4093b6 | 4457 | if (type == void_type_node) |
c2255bc4 AH |
4458 | { |
4459 | tree t = build1 (CONVERT_EXPR, type, value); | |
4460 | SET_EXPR_LOCATION (t, loc); | |
4461 | return t; | |
4462 | } | |
400fbf9f | 4463 | |
3e4093b6 | 4464 | otype = TREE_TYPE (value); |
400fbf9f | 4465 | |
3e4093b6 | 4466 | /* Optionally warn about potentially worrisome casts. */ |
3e4093b6 RS |
4467 | if (warn_cast_qual |
4468 | && TREE_CODE (type) == POINTER_TYPE | |
4469 | && TREE_CODE (otype) == POINTER_TYPE) | |
67165eb3 | 4470 | handle_warn_cast_qual (type, otype); |
400fbf9f | 4471 | |
36c5e70a BE |
4472 | /* Warn about conversions between pointers to disjoint |
4473 | address spaces. */ | |
4474 | if (TREE_CODE (type) == POINTER_TYPE | |
4475 | && TREE_CODE (otype) == POINTER_TYPE | |
4476 | && !null_pointer_constant_p (value)) | |
4477 | { | |
4478 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
4479 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
4480 | addr_space_t as_common; | |
4481 | ||
4482 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
4483 | { | |
4484 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
4485 | warning_at (loc, 0, "cast to %s address space pointer " | |
4486 | "from disjoint generic address space pointer", | |
4487 | c_addr_space_name (as_to)); | |
4488 | ||
4489 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
4490 | warning_at (loc, 0, "cast to generic address space pointer " | |
4491 | "from disjoint %s address space pointer", | |
4492 | c_addr_space_name (as_from)); | |
4493 | ||
4494 | else | |
4495 | warning_at (loc, 0, "cast to %s address space pointer " | |
4496 | "from disjoint %s address space pointer", | |
4497 | c_addr_space_name (as_to), | |
4498 | c_addr_space_name (as_from)); | |
4499 | } | |
4500 | } | |
4501 | ||
3e4093b6 | 4502 | /* Warn about possible alignment problems. */ |
3176a0c2 | 4503 | if (STRICT_ALIGNMENT |
3e4093b6 RS |
4504 | && TREE_CODE (type) == POINTER_TYPE |
4505 | && TREE_CODE (otype) == POINTER_TYPE | |
4506 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
4507 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
4508 | /* Don't warn about opaque types, where the actual alignment | |
4509 | restriction is unknown. */ | |
4510 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
4511 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
4512 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
4513 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
c2255bc4 AH |
4514 | warning_at (loc, OPT_Wcast_align, |
4515 | "cast increases required alignment of target type"); | |
e9a25f70 | 4516 | |
3176a0c2 | 4517 | if (TREE_CODE (type) == INTEGER_TYPE |
3e4093b6 | 4518 | && TREE_CODE (otype) == POINTER_TYPE |
8d1c7aef | 4519 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) |
c22cacf3 MS |
4520 | /* Unlike conversion of integers to pointers, where the |
4521 | warning is disabled for converting constants because | |
4522 | of cases such as SIG_*, warn about converting constant | |
4523 | pointers to integers. In some cases it may cause unwanted | |
8d1c7aef | 4524 | sign extension, and a warning is appropriate. */ |
c2255bc4 AH |
4525 | warning_at (loc, OPT_Wpointer_to_int_cast, |
4526 | "cast from pointer to integer of different size"); | |
400fbf9f | 4527 | |
3176a0c2 | 4528 | if (TREE_CODE (value) == CALL_EXPR |
3e4093b6 | 4529 | && TREE_CODE (type) != TREE_CODE (otype)) |
c2255bc4 AH |
4530 | warning_at (loc, OPT_Wbad_function_cast, |
4531 | "cast from function call of type %qT " | |
4532 | "to non-matching type %qT", otype, type); | |
400fbf9f | 4533 | |
3176a0c2 | 4534 | if (TREE_CODE (type) == POINTER_TYPE |
3e4093b6 RS |
4535 | && TREE_CODE (otype) == INTEGER_TYPE |
4536 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
4537 | /* Don't warn about converting any constant. */ | |
4538 | && !TREE_CONSTANT (value)) | |
c2255bc4 AH |
4539 | warning_at (loc, |
4540 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
4541 | "of different size"); | |
400fbf9f | 4542 | |
79bedddc SR |
4543 | if (warn_strict_aliasing <= 2) |
4544 | strict_aliasing_warning (otype, type, expr); | |
400fbf9f | 4545 | |
3897f229 JM |
4546 | /* If pedantic, warn for conversions between function and object |
4547 | pointer types, except for converting a null pointer constant | |
4548 | to function pointer type. */ | |
4549 | if (pedantic | |
4550 | && TREE_CODE (type) == POINTER_TYPE | |
4551 | && TREE_CODE (otype) == POINTER_TYPE | |
4552 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
4553 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
c2255bc4 | 4554 | pedwarn (loc, OPT_pedantic, "ISO C forbids " |
fcf73884 | 4555 | "conversion of function pointer to object pointer type"); |
3897f229 JM |
4556 | |
4557 | if (pedantic | |
4558 | && TREE_CODE (type) == POINTER_TYPE | |
4559 | && TREE_CODE (otype) == POINTER_TYPE | |
4560 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
4561 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
6aa3c60d | 4562 | && !null_pointer_constant_p (value)) |
c2255bc4 | 4563 | pedwarn (loc, OPT_pedantic, "ISO C forbids " |
fcf73884 | 4564 | "conversion of object pointer to function pointer type"); |
3897f229 | 4565 | |
3e4093b6 | 4566 | ovalue = value; |
3e4093b6 | 4567 | value = convert (type, value); |
400fbf9f | 4568 | |
3e4093b6 | 4569 | /* Ignore any integer overflow caused by the cast. */ |
928c19bb | 4570 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) |
3e4093b6 | 4571 | { |
8bcd6380 | 4572 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) |
6414bad6 | 4573 | { |
8bcd6380 RS |
4574 | if (!TREE_OVERFLOW (value)) |
4575 | { | |
4576 | /* Avoid clobbering a shared constant. */ | |
4577 | value = copy_node (value); | |
4578 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
4579 | } | |
6414bad6 | 4580 | } |
8bcd6380 | 4581 | else if (TREE_OVERFLOW (value)) |
d8e1f97b PB |
4582 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ |
4583 | value = build_int_cst_wide (TREE_TYPE (value), | |
4584 | TREE_INT_CST_LOW (value), | |
4585 | TREE_INT_CST_HIGH (value)); | |
3e4093b6 RS |
4586 | } |
4587 | } | |
400fbf9f | 4588 | |
53cd18ec JM |
4589 | /* Don't let a cast be an lvalue. */ |
4590 | if (value == expr) | |
db3927fb | 4591 | value = non_lvalue_loc (loc, value); |
e9a25f70 | 4592 | |
928c19bb JM |
4593 | /* Don't allow the results of casting to floating-point or complex |
4594 | types be confused with actual constants, or casts involving | |
4595 | integer and pointer types other than direct integer-to-integer | |
4596 | and integer-to-pointer be confused with integer constant | |
4597 | expressions and null pointer constants. */ | |
4598 | if (TREE_CODE (value) == REAL_CST | |
4599 | || TREE_CODE (value) == COMPLEX_CST | |
4600 | || (TREE_CODE (value) == INTEGER_CST | |
4601 | && !((TREE_CODE (expr) == INTEGER_CST | |
4602 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
4603 | || TREE_CODE (expr) == REAL_CST | |
4604 | || TREE_CODE (expr) == COMPLEX_CST))) | |
4605 | value = build1 (NOP_EXPR, type, value); | |
4606 | ||
c2255bc4 AH |
4607 | if (CAN_HAVE_LOCATION_P (value)) |
4608 | SET_EXPR_LOCATION (value, loc); | |
3e4093b6 | 4609 | return value; |
400fbf9f JW |
4610 | } |
4611 | ||
c2255bc4 AH |
4612 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the |
4613 | location of the open paren of the cast, or the position of the cast | |
4614 | expr. */ | |
3e4093b6 | 4615 | tree |
c2255bc4 | 4616 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) |
400fbf9f | 4617 | { |
f8893e47 | 4618 | tree type; |
928c19bb JM |
4619 | tree type_expr = NULL_TREE; |
4620 | bool type_expr_const = true; | |
4621 | tree ret; | |
3e4093b6 | 4622 | int saved_wsp = warn_strict_prototypes; |
c5c76735 | 4623 | |
3e4093b6 RS |
4624 | /* This avoids warnings about unprototyped casts on |
4625 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
4626 | if (TREE_CODE (expr) == INTEGER_CST) | |
4627 | warn_strict_prototypes = 0; | |
928c19bb | 4628 | type = groktypename (type_name, &type_expr, &type_expr_const); |
3e4093b6 | 4629 | warn_strict_prototypes = saved_wsp; |
c5c76735 | 4630 | |
c2255bc4 | 4631 | ret = build_c_cast (loc, type, expr); |
928c19bb JM |
4632 | if (type_expr) |
4633 | { | |
4634 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); | |
4635 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const; | |
c2255bc4 | 4636 | SET_EXPR_LOCATION (ret, loc); |
928c19bb | 4637 | } |
24b97832 ILT |
4638 | |
4639 | if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) | |
4640 | SET_EXPR_LOCATION (ret, loc); | |
4641 | ||
4642 | /* C++ does not permits types to be defined in a cast. */ | |
4643 | if (warn_cxx_compat && type_name->specs->tag_defined_p) | |
4644 | warning_at (loc, OPT_Wc___compat, | |
4645 | "defining a type in a cast is invalid in C++"); | |
4646 | ||
928c19bb | 4647 | return ret; |
400fbf9f | 4648 | } |
3e4093b6 RS |
4649 | \f |
4650 | /* Build an assignment expression of lvalue LHS from value RHS. | |
32e8bb8e ILT |
4651 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which |
4652 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
3e4093b6 RS |
4653 | MODIFYCODE is the code for a binary operator that we use |
4654 | to combine the old value of LHS with RHS to get the new value. | |
c9f9eb5d | 4655 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
bbbbb16a ILT |
4656 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, |
4657 | which may differ from TREE_TYPE (RHS) for an enum value. | |
c9f9eb5d | 4658 | |
c2255bc4 AH |
4659 | LOCATION is the location of the MODIFYCODE operator. |
4660 | RHS_LOC is the location of the RHS. */ | |
2f6e4e97 | 4661 | |
3e4093b6 | 4662 | tree |
32e8bb8e | 4663 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, |
b8698a0f | 4664 | enum tree_code modifycode, |
c2255bc4 | 4665 | location_t rhs_loc, tree rhs, tree rhs_origtype) |
400fbf9f | 4666 | { |
3e4093b6 RS |
4667 | tree result; |
4668 | tree newrhs; | |
8ce94e44 | 4669 | tree rhs_semantic_type = NULL_TREE; |
3e4093b6 RS |
4670 | tree lhstype = TREE_TYPE (lhs); |
4671 | tree olhstype = lhstype; | |
928c19bb | 4672 | bool npc; |
e9a25f70 | 4673 | |
3e4093b6 RS |
4674 | /* Types that aren't fully specified cannot be used in assignments. */ |
4675 | lhs = require_complete_type (lhs); | |
e9a25f70 | 4676 | |
3e4093b6 RS |
4677 | /* Avoid duplicate error messages from operands that had errors. */ |
4678 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
4679 | return error_mark_node; | |
400fbf9f | 4680 | |
c0bcacec VR |
4681 | if (!lvalue_or_else (lhs, lv_assign)) |
4682 | return error_mark_node; | |
4683 | ||
8ce94e44 JM |
4684 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
4685 | { | |
4686 | rhs_semantic_type = TREE_TYPE (rhs); | |
4687 | rhs = TREE_OPERAND (rhs, 0); | |
4688 | } | |
4689 | ||
3e4093b6 | 4690 | newrhs = rhs; |
400fbf9f | 4691 | |
928c19bb JM |
4692 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) |
4693 | { | |
4694 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
c2255bc4 | 4695 | lhs_origtype, modifycode, rhs_loc, rhs, |
32e8bb8e | 4696 | rhs_origtype); |
928c19bb JM |
4697 | if (inner == error_mark_node) |
4698 | return error_mark_node; | |
4699 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4700 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
4701 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
4702 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
4703 | protected_set_expr_location (result, location); | |
4704 | return result; | |
4705 | } | |
4706 | ||
3e4093b6 RS |
4707 | /* If a binary op has been requested, combine the old LHS value with the RHS |
4708 | producing the value we should actually store into the LHS. */ | |
4709 | ||
4710 | if (modifycode != NOP_EXPR) | |
400fbf9f | 4711 | { |
928c19bb | 4712 | lhs = c_fully_fold (lhs, false, NULL); |
3e4093b6 | 4713 | lhs = stabilize_reference (lhs); |
c9f9eb5d | 4714 | newrhs = build_binary_op (location, |
ba47d38d | 4715 | modifycode, lhs, rhs, 1); |
bbbbb16a ILT |
4716 | |
4717 | /* The original type of the right hand side is no longer | |
4718 | meaningful. */ | |
4719 | rhs_origtype = NULL_TREE; | |
400fbf9f | 4720 | } |
400fbf9f | 4721 | |
9bf24266 | 4722 | /* Give an error for storing in something that is 'const'. */ |
bbbd6700 | 4723 | |
f37acdf9 | 4724 | if (TYPE_READONLY (lhstype) |
3e4093b6 RS |
4725 | || ((TREE_CODE (lhstype) == RECORD_TYPE |
4726 | || TREE_CODE (lhstype) == UNION_TYPE) | |
4727 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
953ff289 DN |
4728 | { |
4729 | readonly_error (lhs, lv_assign); | |
4730 | return error_mark_node; | |
4731 | } | |
f37acdf9 JM |
4732 | else if (TREE_READONLY (lhs)) |
4733 | readonly_warning (lhs, lv_assign); | |
bbbd6700 | 4734 | |
3e4093b6 RS |
4735 | /* If storing into a structure or union member, |
4736 | it has probably been given type `int'. | |
4737 | Compute the type that would go with | |
4738 | the actual amount of storage the member occupies. */ | |
bbbd6700 | 4739 | |
3e4093b6 RS |
4740 | if (TREE_CODE (lhs) == COMPONENT_REF |
4741 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
4742 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
4743 | || TREE_CODE (lhstype) == REAL_TYPE | |
4744 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
4745 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
400fbf9f | 4746 | |
3e4093b6 RS |
4747 | /* If storing in a field that is in actuality a short or narrower than one, |
4748 | we must store in the field in its actual type. */ | |
4749 | ||
4750 | if (lhstype != TREE_TYPE (lhs)) | |
4751 | { | |
4752 | lhs = copy_node (lhs); | |
4753 | TREE_TYPE (lhs) = lhstype; | |
400fbf9f | 4754 | } |
400fbf9f | 4755 | |
32e8bb8e ILT |
4756 | /* Issue -Wc++-compat warnings about an assignment to an enum type |
4757 | when LHS does not have its original type. This happens for, | |
4758 | e.g., an enum bitfield in a struct. */ | |
4759 | if (warn_cxx_compat | |
4760 | && lhs_origtype != NULL_TREE | |
4761 | && lhs_origtype != lhstype | |
4762 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
4763 | { | |
4764 | tree checktype = (rhs_origtype != NULL_TREE | |
4765 | ? rhs_origtype | |
4766 | : TREE_TYPE (rhs)); | |
4767 | if (checktype != error_mark_node | |
4768 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)) | |
4769 | warning_at (location, OPT_Wc___compat, | |
4770 | "enum conversion in assignment is invalid in C++"); | |
4771 | } | |
4772 | ||
8ce94e44 JM |
4773 | /* Convert new value to destination type. Fold it first, then |
4774 | restore any excess precision information, for the sake of | |
4775 | conversion warnings. */ | |
400fbf9f | 4776 | |
928c19bb JM |
4777 | npc = null_pointer_constant_p (newrhs); |
4778 | newrhs = c_fully_fold (newrhs, false, NULL); | |
8ce94e44 JM |
4779 | if (rhs_semantic_type) |
4780 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
744aa42f ILT |
4781 | newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype, |
4782 | ic_assign, npc, NULL_TREE, NULL_TREE, 0); | |
3e4093b6 RS |
4783 | if (TREE_CODE (newrhs) == ERROR_MARK) |
4784 | return error_mark_node; | |
400fbf9f | 4785 | |
6e955430 ZL |
4786 | /* Emit ObjC write barrier, if necessary. */ |
4787 | if (c_dialect_objc () && flag_objc_gc) | |
4788 | { | |
4789 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
4790 | if (result) | |
c9f9eb5d AH |
4791 | { |
4792 | protected_set_expr_location (result, location); | |
4793 | return result; | |
4794 | } | |
6e955430 ZL |
4795 | } |
4796 | ||
ea4b7848 | 4797 | /* Scan operands. */ |
400fbf9f | 4798 | |
53fb4de3 | 4799 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); |
3e4093b6 | 4800 | TREE_SIDE_EFFECTS (result) = 1; |
c9f9eb5d | 4801 | protected_set_expr_location (result, location); |
400fbf9f | 4802 | |
3e4093b6 RS |
4803 | /* If we got the LHS in a different type for storing in, |
4804 | convert the result back to the nominal type of LHS | |
4805 | so that the value we return always has the same type | |
4806 | as the LHS argument. */ | |
e855c5ce | 4807 | |
3e4093b6 RS |
4808 | if (olhstype == TREE_TYPE (result)) |
4809 | return result; | |
c9f9eb5d | 4810 | |
744aa42f ILT |
4811 | result = convert_for_assignment (location, olhstype, result, rhs_origtype, |
4812 | ic_assign, false, NULL_TREE, NULL_TREE, 0); | |
c9f9eb5d AH |
4813 | protected_set_expr_location (result, location); |
4814 | return result; | |
3e4093b6 RS |
4815 | } |
4816 | \f | |
bbbbb16a ILT |
4817 | /* Convert value RHS to type TYPE as preparation for an assignment to |
4818 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
4819 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
4820 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
4821 | constant before any folding. | |
3e4093b6 RS |
4822 | The real work of conversion is done by `convert'. |
4823 | The purpose of this function is to generate error messages | |
4824 | for assignments that are not allowed in C. | |
2ac2f164 JM |
4825 | ERRTYPE says whether it is argument passing, assignment, |
4826 | initialization or return. | |
2f6e4e97 | 4827 | |
c2255bc4 | 4828 | LOCATION is the location of the RHS. |
2ac2f164 | 4829 | FUNCTION is a tree for the function being called. |
3e4093b6 | 4830 | PARMNUM is the number of the argument, for printing in error messages. */ |
cb3ca04e | 4831 | |
3e4093b6 | 4832 | static tree |
744aa42f ILT |
4833 | convert_for_assignment (location_t location, tree type, tree rhs, |
4834 | tree origtype, enum impl_conv errtype, | |
4835 | bool null_pointer_constant, tree fundecl, | |
4836 | tree function, int parmnum) | |
3e4093b6 RS |
4837 | { |
4838 | enum tree_code codel = TREE_CODE (type); | |
8ce94e44 | 4839 | tree orig_rhs = rhs; |
3e4093b6 RS |
4840 | tree rhstype; |
4841 | enum tree_code coder; | |
2ac2f164 | 4842 | tree rname = NULL_TREE; |
58393038 | 4843 | bool objc_ok = false; |
2ac2f164 | 4844 | |
6b4ef5c1 | 4845 | if (errtype == ic_argpass) |
2ac2f164 JM |
4846 | { |
4847 | tree selector; | |
4848 | /* Change pointer to function to the function itself for | |
4849 | diagnostics. */ | |
4850 | if (TREE_CODE (function) == ADDR_EXPR | |
4851 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
4852 | function = TREE_OPERAND (function, 0); | |
4853 | ||
4854 | /* Handle an ObjC selector specially for diagnostics. */ | |
4855 | selector = objc_message_selector (); | |
4856 | rname = function; | |
4857 | if (selector && parmnum > 2) | |
4858 | { | |
4859 | rname = selector; | |
4860 | parmnum -= 2; | |
4861 | } | |
4862 | } | |
4863 | ||
4864 | /* This macro is used to emit diagnostics to ensure that all format | |
4865 | strings are complete sentences, visible to gettext and checked at | |
4866 | compile time. */ | |
c2255bc4 | 4867 | #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \ |
1e053dfe MLI |
4868 | do { \ |
4869 | switch (errtype) \ | |
4870 | { \ | |
4871 | case ic_argpass: \ | |
4872 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \ | |
c2255bc4 AH |
4873 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
4874 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
1e053dfe MLI |
4875 | "expected %qT but argument is of type %qT", \ |
4876 | type, rhstype); \ | |
4877 | break; \ | |
1e053dfe MLI |
4878 | case ic_assign: \ |
4879 | pedwarn (LOCATION, OPT, AS); \ | |
4880 | break; \ | |
4881 | case ic_init: \ | |
4882 | pedwarn (LOCATION, OPT, IN); \ | |
4883 | break; \ | |
4884 | case ic_return: \ | |
c2255bc4 | 4885 | pedwarn (LOCATION, OPT, RE); \ |
1e053dfe MLI |
4886 | break; \ |
4887 | default: \ | |
4888 | gcc_unreachable (); \ | |
4889 | } \ | |
2ac2f164 | 4890 | } while (0) |
cb3ca04e | 4891 | |
8ce94e44 JM |
4892 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
4893 | rhs = TREE_OPERAND (rhs, 0); | |
4894 | ||
3e4093b6 RS |
4895 | rhstype = TREE_TYPE (rhs); |
4896 | coder = TREE_CODE (rhstype); | |
4897 | ||
4898 | if (coder == ERROR_MARK) | |
4899 | return error_mark_node; | |
4900 | ||
58393038 ZL |
4901 | if (c_dialect_objc ()) |
4902 | { | |
4903 | int parmno; | |
4904 | ||
4905 | switch (errtype) | |
4906 | { | |
4907 | case ic_return: | |
4908 | parmno = 0; | |
4909 | break; | |
4910 | ||
4911 | case ic_assign: | |
4912 | parmno = -1; | |
4913 | break; | |
4914 | ||
4915 | case ic_init: | |
4916 | parmno = -2; | |
4917 | break; | |
4918 | ||
4919 | default: | |
4920 | parmno = parmnum; | |
4921 | break; | |
4922 | } | |
4923 | ||
4924 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
4925 | } | |
4926 | ||
bbbbb16a ILT |
4927 | if (warn_cxx_compat) |
4928 | { | |
4929 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
4930 | if (checktype != error_mark_node | |
4931 | && TREE_CODE (type) == ENUMERAL_TYPE | |
4932 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
4933 | { | |
81f40b79 ILT |
4934 | WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat, |
4935 | G_("enum conversion when passing argument " | |
4936 | "%d of %qE is invalid in C++"), | |
4937 | G_("enum conversion in assignment is " | |
4938 | "invalid in C++"), | |
4939 | G_("enum conversion in initialization is " | |
4940 | "invalid in C++"), | |
4941 | G_("enum conversion in return is " | |
4942 | "invalid in C++")); | |
bbbbb16a ILT |
4943 | } |
4944 | } | |
4945 | ||
3e4093b6 | 4946 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) |
59c0753d | 4947 | return rhs; |
3e4093b6 RS |
4948 | |
4949 | if (coder == VOID_TYPE) | |
400fbf9f | 4950 | { |
6dcc04b0 JM |
4951 | /* Except for passing an argument to an unprototyped function, |
4952 | this is a constraint violation. When passing an argument to | |
4953 | an unprototyped function, it is compile-time undefined; | |
4954 | making it a constraint in that case was rejected in | |
4955 | DR#252. */ | |
c2255bc4 | 4956 | error_at (location, "void value not ignored as it ought to be"); |
3e4093b6 | 4957 | return error_mark_node; |
400fbf9f | 4958 | } |
808d6eaa JM |
4959 | rhs = require_complete_type (rhs); |
4960 | if (rhs == error_mark_node) | |
4961 | return error_mark_node; | |
3e4093b6 RS |
4962 | /* A type converts to a reference to it. |
4963 | This code doesn't fully support references, it's just for the | |
4964 | special case of va_start and va_copy. */ | |
4965 | if (codel == REFERENCE_TYPE | |
132da1a5 | 4966 | && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1) |
400fbf9f | 4967 | { |
3e4093b6 | 4968 | if (!lvalue_p (rhs)) |
400fbf9f | 4969 | { |
c2255bc4 | 4970 | error_at (location, "cannot pass rvalue to reference parameter"); |
3e4093b6 | 4971 | return error_mark_node; |
400fbf9f | 4972 | } |
3e4093b6 RS |
4973 | if (!c_mark_addressable (rhs)) |
4974 | return error_mark_node; | |
4975 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
c2255bc4 | 4976 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 RS |
4977 | |
4978 | /* We already know that these two types are compatible, but they | |
4979 | may not be exactly identical. In fact, `TREE_TYPE (type)' is | |
4980 | likely to be __builtin_va_list and `TREE_TYPE (rhs)' is | |
4981 | likely to be va_list, a typedef to __builtin_va_list, which | |
4982 | is different enough that it will cause problems later. */ | |
4983 | if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type)) | |
c2255bc4 AH |
4984 | { |
4985 | rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs); | |
4986 | SET_EXPR_LOCATION (rhs, location); | |
4987 | } | |
3e4093b6 RS |
4988 | |
4989 | rhs = build1 (NOP_EXPR, type, rhs); | |
c2255bc4 | 4990 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 4991 | return rhs; |
400fbf9f | 4992 | } |
3e4093b6 | 4993 | /* Some types can interconvert without explicit casts. */ |
3274deff | 4994 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE |
00c8e9f6 | 4995 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) |
3e4093b6 RS |
4996 | return convert (type, rhs); |
4997 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
4998 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
ab22c1fa | 4999 | || codel == FIXED_POINT_TYPE |
3e4093b6 RS |
5000 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE |
5001 | || codel == BOOLEAN_TYPE) | |
5002 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
ab22c1fa | 5003 | || coder == FIXED_POINT_TYPE |
3e4093b6 RS |
5004 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE |
5005 | || coder == BOOLEAN_TYPE)) | |
928c19bb JM |
5006 | { |
5007 | tree ret; | |
5008 | bool save = in_late_binary_op; | |
5009 | if (codel == BOOLEAN_TYPE) | |
5010 | in_late_binary_op = true; | |
8ce94e44 | 5011 | ret = convert_and_check (type, orig_rhs); |
928c19bb JM |
5012 | if (codel == BOOLEAN_TYPE) |
5013 | in_late_binary_op = save; | |
5014 | return ret; | |
5015 | } | |
400fbf9f | 5016 | |
79077aea JJ |
5017 | /* Aggregates in different TUs might need conversion. */ |
5018 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
5019 | && codel == coder | |
5020 | && comptypes (type, rhstype)) | |
5021 | return convert_and_check (type, rhs); | |
5022 | ||
ebf0bf7f | 5023 | /* Conversion to a transparent union or record from its member types. |
3e4093b6 | 5024 | This applies only to function arguments. */ |
ebf0bf7f JJ |
5025 | if (((codel == UNION_TYPE || codel == RECORD_TYPE) |
5026 | && TYPE_TRANSPARENT_AGGR (type)) | |
6b4ef5c1 | 5027 | && errtype == ic_argpass) |
400fbf9f | 5028 | { |
0257e383 | 5029 | tree memb, marginal_memb = NULL_TREE; |
3e4093b6 | 5030 | |
0257e383 | 5031 | for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb)) |
400fbf9f | 5032 | { |
0257e383 | 5033 | tree memb_type = TREE_TYPE (memb); |
400fbf9f | 5034 | |
3e4093b6 | 5035 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
132da1a5 | 5036 | TYPE_MAIN_VARIANT (rhstype))) |
3e4093b6 | 5037 | break; |
e58cd767 | 5038 | |
3e4093b6 RS |
5039 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
5040 | continue; | |
2f6e4e97 | 5041 | |
3e4093b6 RS |
5042 | if (coder == POINTER_TYPE) |
5043 | { | |
5044 | tree ttl = TREE_TYPE (memb_type); | |
5045 | tree ttr = TREE_TYPE (rhstype); | |
400fbf9f | 5046 | |
3e4093b6 RS |
5047 | /* Any non-function converts to a [const][volatile] void * |
5048 | and vice versa; otherwise, targets must be the same. | |
5049 | Meanwhile, the lhs target must have all the qualifiers of | |
5050 | the rhs. */ | |
5051 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
744aa42f | 5052 | || comp_target_types (location, memb_type, rhstype)) |
3e4093b6 RS |
5053 | { |
5054 | /* If this type won't generate any warnings, use it. */ | |
5055 | if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr) | |
5056 | || ((TREE_CODE (ttr) == FUNCTION_TYPE | |
5057 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5058 | ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5059 | == TYPE_QUALS (ttr)) | |
5060 | : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5061 | == TYPE_QUALS (ttl)))) | |
5062 | break; | |
400fbf9f | 5063 | |
3e4093b6 | 5064 | /* Keep looking for a better type, but remember this one. */ |
0257e383 RH |
5065 | if (!marginal_memb) |
5066 | marginal_memb = memb; | |
3e4093b6 RS |
5067 | } |
5068 | } | |
82bde854 | 5069 | |
3e4093b6 | 5070 | /* Can convert integer zero to any pointer type. */ |
928c19bb | 5071 | if (null_pointer_constant) |
3e4093b6 RS |
5072 | { |
5073 | rhs = null_pointer_node; | |
5074 | break; | |
5075 | } | |
5076 | } | |
400fbf9f | 5077 | |
0257e383 | 5078 | if (memb || marginal_memb) |
3e4093b6 | 5079 | { |
0257e383 | 5080 | if (!memb) |
3e4093b6 RS |
5081 | { |
5082 | /* We have only a marginally acceptable member type; | |
5083 | it needs a warning. */ | |
0257e383 | 5084 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); |
3e4093b6 | 5085 | tree ttr = TREE_TYPE (rhstype); |
714a0864 | 5086 | |
3e4093b6 RS |
5087 | /* Const and volatile mean something different for function |
5088 | types, so the usual warnings are not appropriate. */ | |
5089 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
5090 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5091 | { | |
5092 | /* Because const and volatile on functions are | |
5093 | restrictions that say the function will not do | |
5094 | certain things, it is okay to use a const or volatile | |
5095 | function where an ordinary one is wanted, but not | |
5096 | vice-versa. */ | |
36c5e70a BE |
5097 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5098 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
c2255bc4 | 5099 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5100 | G_("passing argument %d of %qE " |
2ac2f164 JM |
5101 | "makes qualified function " |
5102 | "pointer from unqualified"), | |
4b794eaf | 5103 | G_("assignment makes qualified " |
2ac2f164 JM |
5104 | "function pointer from " |
5105 | "unqualified"), | |
4b794eaf | 5106 | G_("initialization makes qualified " |
2ac2f164 JM |
5107 | "function pointer from " |
5108 | "unqualified"), | |
4b794eaf | 5109 | G_("return makes qualified function " |
2ac2f164 | 5110 | "pointer from unqualified")); |
3e4093b6 | 5111 | } |
36c5e70a BE |
5112 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5113 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
c2255bc4 | 5114 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5115 | G_("passing argument %d of %qE discards " |
2ac2f164 | 5116 | "qualifiers from pointer target type"), |
4b794eaf | 5117 | G_("assignment discards qualifiers " |
2ac2f164 | 5118 | "from pointer target type"), |
4b794eaf | 5119 | G_("initialization discards qualifiers " |
2ac2f164 | 5120 | "from pointer target type"), |
4b794eaf | 5121 | G_("return discards qualifiers from " |
2ac2f164 | 5122 | "pointer target type")); |
0257e383 RH |
5123 | |
5124 | memb = marginal_memb; | |
3e4093b6 | 5125 | } |
400fbf9f | 5126 | |
fcf73884 | 5127 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) |
b8698a0f | 5128 | pedwarn (location, OPT_pedantic, |
fcf73884 | 5129 | "ISO C prohibits argument conversion to union type"); |
0e7c47fa | 5130 | |
db3927fb | 5131 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); |
0257e383 | 5132 | return build_constructor_single (type, memb, rhs); |
3e4093b6 | 5133 | } |
0e7c47fa RK |
5134 | } |
5135 | ||
3e4093b6 RS |
5136 | /* Conversions among pointers */ |
5137 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
5138 | && (coder == codel)) | |
400fbf9f | 5139 | { |
3e4093b6 RS |
5140 | tree ttl = TREE_TYPE (type); |
5141 | tree ttr = TREE_TYPE (rhstype); | |
46df2823 JM |
5142 | tree mvl = ttl; |
5143 | tree mvr = ttr; | |
3e4093b6 | 5144 | bool is_opaque_pointer; |
264fa2db | 5145 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
36c5e70a BE |
5146 | addr_space_t asl; |
5147 | addr_space_t asr; | |
400fbf9f | 5148 | |
46df2823 JM |
5149 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
5150 | mvl = TYPE_MAIN_VARIANT (mvl); | |
5151 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
5152 | mvr = TYPE_MAIN_VARIANT (mvr); | |
3e4093b6 | 5153 | /* Opaque pointers are treated like void pointers. */ |
f83c7f63 | 5154 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); |
c22cacf3 | 5155 | |
b7e20b53 | 5156 | /* C++ does not allow the implicit conversion void* -> T*. However, |
c22cacf3 MS |
5157 | for the purpose of reducing the number of false positives, we |
5158 | tolerate the special case of | |
b7e20b53 | 5159 | |
c22cacf3 | 5160 | int *p = NULL; |
b7e20b53 | 5161 | |
c22cacf3 | 5162 | where NULL is typically defined in C to be '(void *) 0'. */ |
c6bdf92e | 5163 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) |
c2255bc4 AH |
5164 | warning_at (location, OPT_Wc___compat, |
5165 | "request for implicit conversion " | |
5166 | "from %qT to %qT not permitted in C++", rhstype, type); | |
400fbf9f | 5167 | |
36c5e70a BE |
5168 | /* See if the pointers point to incompatible address spaces. */ |
5169 | asl = TYPE_ADDR_SPACE (ttl); | |
5170 | asr = TYPE_ADDR_SPACE (ttr); | |
5171 | if (!null_pointer_constant_p (rhs) | |
5172 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
5173 | { | |
5174 | switch (errtype) | |
5175 | { | |
5176 | case ic_argpass: | |
5177 | error_at (location, "passing argument %d of %qE from pointer to " | |
5178 | "non-enclosed address space", parmnum, rname); | |
5179 | break; | |
5180 | case ic_assign: | |
5181 | error_at (location, "assignment from pointer to " | |
5182 | "non-enclosed address space"); | |
5183 | break; | |
5184 | case ic_init: | |
5185 | error_at (location, "initialization from pointer to " | |
5186 | "non-enclosed address space"); | |
5187 | break; | |
5188 | case ic_return: | |
5189 | error_at (location, "return from pointer to " | |
5190 | "non-enclosed address space"); | |
5191 | break; | |
5192 | default: | |
5193 | gcc_unreachable (); | |
5194 | } | |
5195 | return error_mark_node; | |
5196 | } | |
5197 | ||
7876a414 KG |
5198 | /* Check if the right-hand side has a format attribute but the |
5199 | left-hand side doesn't. */ | |
104f8784 KG |
5200 | if (warn_missing_format_attribute |
5201 | && check_missing_format_attribute (type, rhstype)) | |
c22cacf3 | 5202 | { |
104f8784 KG |
5203 | switch (errtype) |
5204 | { | |
5205 | case ic_argpass: | |
c2255bc4 AH |
5206 | warning_at (location, OPT_Wmissing_format_attribute, |
5207 | "argument %d of %qE might be " | |
5208 | "a candidate for a format attribute", | |
5209 | parmnum, rname); | |
104f8784 KG |
5210 | break; |
5211 | case ic_assign: | |
c2255bc4 AH |
5212 | warning_at (location, OPT_Wmissing_format_attribute, |
5213 | "assignment left-hand side might be " | |
5214 | "a candidate for a format attribute"); | |
104f8784 KG |
5215 | break; |
5216 | case ic_init: | |
c2255bc4 AH |
5217 | warning_at (location, OPT_Wmissing_format_attribute, |
5218 | "initialization left-hand side might be " | |
5219 | "a candidate for a format attribute"); | |
104f8784 KG |
5220 | break; |
5221 | case ic_return: | |
c2255bc4 AH |
5222 | warning_at (location, OPT_Wmissing_format_attribute, |
5223 | "return type might be " | |
5224 | "a candidate for a format attribute"); | |
104f8784 KG |
5225 | break; |
5226 | default: | |
5227 | gcc_unreachable (); | |
5228 | } | |
7876a414 | 5229 | } |
c22cacf3 | 5230 | |
3e4093b6 RS |
5231 | /* Any non-function converts to a [const][volatile] void * |
5232 | and vice versa; otherwise, targets must be the same. | |
5233 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
5234 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
744aa42f | 5235 | || (target_cmp = comp_target_types (location, type, rhstype)) |
3e4093b6 | 5236 | || is_opaque_pointer |
12753674 RE |
5237 | || (c_common_unsigned_type (mvl) |
5238 | == c_common_unsigned_type (mvr))) | |
3e4093b6 RS |
5239 | { |
5240 | if (pedantic | |
5241 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5242 | || | |
5243 | (VOID_TYPE_P (ttr) | |
928c19bb | 5244 | && !null_pointer_constant |
3e4093b6 | 5245 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
c2255bc4 | 5246 | WARN_FOR_ASSIGNMENT (location, OPT_pedantic, |
509c9d60 | 5247 | G_("ISO C forbids passing argument %d of " |
2ac2f164 JM |
5248 | "%qE between function pointer " |
5249 | "and %<void *%>"), | |
4b794eaf | 5250 | G_("ISO C forbids assignment between " |
2ac2f164 | 5251 | "function pointer and %<void *%>"), |
4b794eaf | 5252 | G_("ISO C forbids initialization between " |
2ac2f164 | 5253 | "function pointer and %<void *%>"), |
4b794eaf | 5254 | G_("ISO C forbids return between function " |
2ac2f164 | 5255 | "pointer and %<void *%>")); |
3e4093b6 RS |
5256 | /* Const and volatile mean something different for function types, |
5257 | so the usual warnings are not appropriate. */ | |
5258 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
5259 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
5260 | { | |
36c5e70a BE |
5261 | if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5262 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
58393038 ZL |
5263 | { |
5264 | /* Types differing only by the presence of the 'volatile' | |
5265 | qualifier are acceptable if the 'volatile' has been added | |
5266 | in by the Objective-C EH machinery. */ | |
5267 | if (!objc_type_quals_match (ttl, ttr)) | |
c2255bc4 | 5268 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5269 | G_("passing argument %d of %qE discards " |
58393038 | 5270 | "qualifiers from pointer target type"), |
4b794eaf | 5271 | G_("assignment discards qualifiers " |
58393038 | 5272 | "from pointer target type"), |
4b794eaf | 5273 | G_("initialization discards qualifiers " |
58393038 | 5274 | "from pointer target type"), |
4b794eaf | 5275 | G_("return discards qualifiers from " |
58393038 ZL |
5276 | "pointer target type")); |
5277 | } | |
3e4093b6 RS |
5278 | /* If this is not a case of ignoring a mismatch in signedness, |
5279 | no warning. */ | |
5280 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 5281 | || target_cmp) |
3e4093b6 RS |
5282 | ; |
5283 | /* If there is a mismatch, do warn. */ | |
f2fd3821 | 5284 | else if (warn_pointer_sign) |
c2255bc4 | 5285 | WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign, |
509c9d60 | 5286 | G_("pointer targets in passing argument " |
2ac2f164 | 5287 | "%d of %qE differ in signedness"), |
4b794eaf | 5288 | G_("pointer targets in assignment " |
2ac2f164 | 5289 | "differ in signedness"), |
4b794eaf | 5290 | G_("pointer targets in initialization " |
2ac2f164 | 5291 | "differ in signedness"), |
4b794eaf | 5292 | G_("pointer targets in return differ " |
2ac2f164 | 5293 | "in signedness")); |
3e4093b6 RS |
5294 | } |
5295 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
5296 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5297 | { | |
5298 | /* Because const and volatile on functions are restrictions | |
5299 | that say the function will not do certain things, | |
5300 | it is okay to use a const or volatile function | |
5301 | where an ordinary one is wanted, but not vice-versa. */ | |
36c5e70a BE |
5302 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5303 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
c2255bc4 | 5304 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5305 | G_("passing argument %d of %qE makes " |
2ac2f164 JM |
5306 | "qualified function pointer " |
5307 | "from unqualified"), | |
4b794eaf | 5308 | G_("assignment makes qualified function " |
2ac2f164 | 5309 | "pointer from unqualified"), |
4b794eaf | 5310 | G_("initialization makes qualified " |
2ac2f164 | 5311 | "function pointer from unqualified"), |
4b794eaf | 5312 | G_("return makes qualified function " |
2ac2f164 | 5313 | "pointer from unqualified")); |
3e4093b6 RS |
5314 | } |
5315 | } | |
5316 | else | |
58393038 ZL |
5317 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
5318 | if (!objc_ok) | |
c2255bc4 | 5319 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5320 | G_("passing argument %d of %qE from " |
58393038 | 5321 | "incompatible pointer type"), |
4b794eaf JJ |
5322 | G_("assignment from incompatible pointer type"), |
5323 | G_("initialization from incompatible " | |
58393038 | 5324 | "pointer type"), |
4b794eaf | 5325 | G_("return from incompatible pointer type")); |
58393038 | 5326 | |
3e4093b6 RS |
5327 | return convert (type, rhs); |
5328 | } | |
b494fd98 EB |
5329 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
5330 | { | |
6dcc04b0 JM |
5331 | /* ??? This should not be an error when inlining calls to |
5332 | unprototyped functions. */ | |
c2255bc4 | 5333 | error_at (location, "invalid use of non-lvalue array"); |
b494fd98 EB |
5334 | return error_mark_node; |
5335 | } | |
3e4093b6 | 5336 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
400fbf9f | 5337 | { |
3e4093b6 RS |
5338 | /* An explicit constant 0 can convert to a pointer, |
5339 | or one that results from arithmetic, even including | |
5340 | a cast to integer type. */ | |
928c19bb | 5341 | if (!null_pointer_constant) |
c2255bc4 | 5342 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5343 | G_("passing argument %d of %qE makes " |
2ac2f164 | 5344 | "pointer from integer without a cast"), |
4b794eaf | 5345 | G_("assignment makes pointer from integer " |
2ac2f164 | 5346 | "without a cast"), |
4b794eaf | 5347 | G_("initialization makes pointer from " |
2ac2f164 | 5348 | "integer without a cast"), |
4b794eaf | 5349 | G_("return makes pointer from integer " |
2ac2f164 | 5350 | "without a cast")); |
b3006337 EB |
5351 | |
5352 | return convert (type, rhs); | |
400fbf9f | 5353 | } |
3e4093b6 | 5354 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
400fbf9f | 5355 | { |
c2255bc4 | 5356 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5357 | G_("passing argument %d of %qE makes integer " |
2ac2f164 | 5358 | "from pointer without a cast"), |
4b794eaf | 5359 | G_("assignment makes integer from pointer " |
2ac2f164 | 5360 | "without a cast"), |
4b794eaf | 5361 | G_("initialization makes integer from pointer " |
2ac2f164 | 5362 | "without a cast"), |
4b794eaf | 5363 | G_("return makes integer from pointer " |
2ac2f164 | 5364 | "without a cast")); |
3e4093b6 RS |
5365 | return convert (type, rhs); |
5366 | } | |
5367 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
928c19bb JM |
5368 | { |
5369 | tree ret; | |
5370 | bool save = in_late_binary_op; | |
5371 | in_late_binary_op = true; | |
5372 | ret = convert (type, rhs); | |
5373 | in_late_binary_op = save; | |
5374 | return ret; | |
5375 | } | |
400fbf9f | 5376 | |
2ac2f164 | 5377 | switch (errtype) |
3e4093b6 | 5378 | { |
2ac2f164 | 5379 | case ic_argpass: |
c2255bc4 | 5380 | error_at (location, "incompatible type for argument %d of %qE", parmnum, rname); |
a7da8b42 MLI |
5381 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) |
5382 | ? DECL_SOURCE_LOCATION (fundecl) : input_location, | |
5383 | "expected %qT but argument is of type %qT", type, rhstype); | |
2ac2f164 JM |
5384 | break; |
5385 | case ic_assign: | |
c2255bc4 AH |
5386 | error_at (location, "incompatible types when assigning to type %qT from " |
5387 | "type %qT", type, rhstype); | |
2ac2f164 JM |
5388 | break; |
5389 | case ic_init: | |
c2255bc4 AH |
5390 | error_at (location, |
5391 | "incompatible types when initializing type %qT using type %qT", | |
5392 | type, rhstype); | |
2ac2f164 JM |
5393 | break; |
5394 | case ic_return: | |
c2255bc4 AH |
5395 | error_at (location, |
5396 | "incompatible types when returning type %qT but %qT was " | |
5397 | "expected", rhstype, type); | |
2ac2f164 JM |
5398 | break; |
5399 | default: | |
5400 | gcc_unreachable (); | |
400fbf9f | 5401 | } |
53b01f59 | 5402 | |
3e4093b6 RS |
5403 | return error_mark_node; |
5404 | } | |
3e4093b6 RS |
5405 | \f |
5406 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
5407 | return its value. Otherwise, return error_mark_node. | |
15b732b2 | 5408 | |
3e4093b6 RS |
5409 | This is for handling COMPOUND_EXPRs as initializer elements |
5410 | which is allowed with a warning when -pedantic is specified. */ | |
15b732b2 | 5411 | |
3e4093b6 RS |
5412 | static tree |
5413 | valid_compound_expr_initializer (tree value, tree endtype) | |
5414 | { | |
5415 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
5416 | { | |
5417 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
5418 | == error_mark_node) | |
5419 | return error_mark_node; | |
5420 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
5421 | endtype); | |
5422 | } | |
116df786 | 5423 | else if (!initializer_constant_valid_p (value, endtype)) |
3e4093b6 RS |
5424 | return error_mark_node; |
5425 | else | |
5426 | return value; | |
15b732b2 | 5427 | } |
400fbf9f | 5428 | \f |
3e4093b6 RS |
5429 | /* Perform appropriate conversions on the initial value of a variable, |
5430 | store it in the declaration DECL, | |
5431 | and print any error messages that are appropriate. | |
bbbbb16a | 5432 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
c2255bc4 AH |
5433 | If the init is invalid, store an ERROR_MARK. |
5434 | ||
5435 | INIT_LOC is the location of the initial value. */ | |
400fbf9f | 5436 | |
3e4093b6 | 5437 | void |
c2255bc4 | 5438 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) |
400fbf9f | 5439 | { |
3e4093b6 | 5440 | tree value, type; |
928c19bb | 5441 | bool npc = false; |
400fbf9f | 5442 | |
3e4093b6 | 5443 | /* If variable's type was invalidly declared, just ignore it. */ |
400fbf9f | 5444 | |
3e4093b6 RS |
5445 | type = TREE_TYPE (decl); |
5446 | if (TREE_CODE (type) == ERROR_MARK) | |
5447 | return; | |
400fbf9f | 5448 | |
3e4093b6 | 5449 | /* Digest the specified initializer into an expression. */ |
400fbf9f | 5450 | |
928c19bb JM |
5451 | if (init) |
5452 | npc = null_pointer_constant_p (init); | |
c2255bc4 AH |
5453 | value = digest_init (init_loc, type, init, origtype, npc, |
5454 | true, TREE_STATIC (decl)); | |
400fbf9f | 5455 | |
3e4093b6 | 5456 | /* Store the expression if valid; else report error. */ |
400fbf9f | 5457 | |
3176a0c2 | 5458 | if (!in_system_header |
3f75a254 | 5459 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) |
3176a0c2 DD |
5460 | warning (OPT_Wtraditional, "traditional C rejects automatic " |
5461 | "aggregate initialization"); | |
2f6e4e97 | 5462 | |
3e4093b6 | 5463 | DECL_INITIAL (decl) = value; |
400fbf9f | 5464 | |
3e4093b6 RS |
5465 | /* ANSI wants warnings about out-of-range constant initializers. */ |
5466 | STRIP_TYPE_NOPS (value); | |
b8698a0f | 5467 | if (TREE_STATIC (decl)) |
c2658540 | 5468 | constant_expression_warning (value); |
400fbf9f | 5469 | |
3e4093b6 RS |
5470 | /* Check if we need to set array size from compound literal size. */ |
5471 | if (TREE_CODE (type) == ARRAY_TYPE | |
5472 | && TYPE_DOMAIN (type) == 0 | |
5473 | && value != error_mark_node) | |
400fbf9f | 5474 | { |
3e4093b6 RS |
5475 | tree inside_init = init; |
5476 | ||
ed248cf7 | 5477 | STRIP_TYPE_NOPS (inside_init); |
3e4093b6 RS |
5478 | inside_init = fold (inside_init); |
5479 | ||
5480 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
5481 | { | |
8d9f82d5 | 5482 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
3e4093b6 | 5483 | |
8d9f82d5 | 5484 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) |
3e4093b6 RS |
5485 | { |
5486 | /* For int foo[] = (int [3]){1}; we need to set array size | |
5487 | now since later on array initializer will be just the | |
5488 | brace enclosed list of the compound literal. */ | |
8d9f82d5 AO |
5489 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); |
5490 | TREE_TYPE (decl) = type; | |
5491 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); | |
3e4093b6 | 5492 | layout_type (type); |
8d9f82d5 | 5493 | layout_decl (cldecl, 0); |
3e4093b6 RS |
5494 | } |
5495 | } | |
400fbf9f | 5496 | } |
3e4093b6 RS |
5497 | } |
5498 | \f | |
5499 | /* Methods for storing and printing names for error messages. */ | |
400fbf9f | 5500 | |
3e4093b6 RS |
5501 | /* Implement a spelling stack that allows components of a name to be pushed |
5502 | and popped. Each element on the stack is this structure. */ | |
400fbf9f | 5503 | |
3e4093b6 RS |
5504 | struct spelling |
5505 | { | |
5506 | int kind; | |
5507 | union | |
400fbf9f | 5508 | { |
a0f0ab9f | 5509 | unsigned HOST_WIDE_INT i; |
3e4093b6 RS |
5510 | const char *s; |
5511 | } u; | |
5512 | }; | |
2f6e4e97 | 5513 | |
3e4093b6 RS |
5514 | #define SPELLING_STRING 1 |
5515 | #define SPELLING_MEMBER 2 | |
5516 | #define SPELLING_BOUNDS 3 | |
400fbf9f | 5517 | |
3e4093b6 RS |
5518 | static struct spelling *spelling; /* Next stack element (unused). */ |
5519 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
5520 | static int spelling_size; /* Size of the spelling stack. */ | |
400fbf9f | 5521 | |
3e4093b6 RS |
5522 | /* Macros to save and restore the spelling stack around push_... functions. |
5523 | Alternative to SAVE_SPELLING_STACK. */ | |
400fbf9f | 5524 | |
3e4093b6 RS |
5525 | #define SPELLING_DEPTH() (spelling - spelling_base) |
5526 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
400fbf9f | 5527 | |
3e4093b6 RS |
5528 | /* Push an element on the spelling stack with type KIND and assign VALUE |
5529 | to MEMBER. */ | |
400fbf9f | 5530 | |
3e4093b6 RS |
5531 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
5532 | { \ | |
5533 | int depth = SPELLING_DEPTH (); \ | |
5534 | \ | |
5535 | if (depth >= spelling_size) \ | |
5536 | { \ | |
5537 | spelling_size += 10; \ | |
cca8ead2 BI |
5538 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
5539 | spelling_size); \ | |
3e4093b6 RS |
5540 | RESTORE_SPELLING_DEPTH (depth); \ |
5541 | } \ | |
5542 | \ | |
5543 | spelling->kind = (KIND); \ | |
5544 | spelling->MEMBER = (VALUE); \ | |
5545 | spelling++; \ | |
5546 | } | |
400fbf9f | 5547 | |
3e4093b6 | 5548 | /* Push STRING on the stack. Printed literally. */ |
400fbf9f | 5549 | |
3e4093b6 RS |
5550 | static void |
5551 | push_string (const char *string) | |
5552 | { | |
5553 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
5554 | } | |
400fbf9f | 5555 | |
3e4093b6 | 5556 | /* Push a member name on the stack. Printed as '.' STRING. */ |
400fbf9f | 5557 | |
3e4093b6 RS |
5558 | static void |
5559 | push_member_name (tree decl) | |
5560 | { | |
5561 | const char *const string | |
88388a52 JM |
5562 | = (DECL_NAME (decl) |
5563 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
5564 | : _("<anonymous>")); | |
3e4093b6 RS |
5565 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
5566 | } | |
400fbf9f | 5567 | |
3e4093b6 | 5568 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
400fbf9f | 5569 | |
3e4093b6 | 5570 | static void |
a0f0ab9f | 5571 | push_array_bounds (unsigned HOST_WIDE_INT bounds) |
3e4093b6 RS |
5572 | { |
5573 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
5574 | } | |
bb58bec5 | 5575 | |
3e4093b6 | 5576 | /* Compute the maximum size in bytes of the printed spelling. */ |
400fbf9f | 5577 | |
3e4093b6 RS |
5578 | static int |
5579 | spelling_length (void) | |
5580 | { | |
5581 | int size = 0; | |
5582 | struct spelling *p; | |
400fbf9f | 5583 | |
3e4093b6 RS |
5584 | for (p = spelling_base; p < spelling; p++) |
5585 | { | |
5586 | if (p->kind == SPELLING_BOUNDS) | |
5587 | size += 25; | |
5588 | else | |
5589 | size += strlen (p->u.s) + 1; | |
5590 | } | |
5591 | ||
5592 | return size; | |
400fbf9f | 5593 | } |
400fbf9f | 5594 | |
3e4093b6 | 5595 | /* Print the spelling to BUFFER and return it. */ |
400fbf9f | 5596 | |
3e4093b6 RS |
5597 | static char * |
5598 | print_spelling (char *buffer) | |
400fbf9f | 5599 | { |
3e4093b6 RS |
5600 | char *d = buffer; |
5601 | struct spelling *p; | |
400fbf9f | 5602 | |
3e4093b6 RS |
5603 | for (p = spelling_base; p < spelling; p++) |
5604 | if (p->kind == SPELLING_BOUNDS) | |
5605 | { | |
a0f0ab9f | 5606 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); |
3e4093b6 RS |
5607 | d += strlen (d); |
5608 | } | |
5609 | else | |
5610 | { | |
5611 | const char *s; | |
5612 | if (p->kind == SPELLING_MEMBER) | |
5613 | *d++ = '.'; | |
5614 | for (s = p->u.s; (*d = *s++); d++) | |
5615 | ; | |
5616 | } | |
5617 | *d++ = '\0'; | |
5618 | return buffer; | |
5619 | } | |
400fbf9f | 5620 | |
3e4093b6 RS |
5621 | /* Issue an error message for a bad initializer component. |
5622 | MSGID identifies the message. | |
5623 | The component name is taken from the spelling stack. */ | |
400fbf9f | 5624 | |
3e4093b6 RS |
5625 | void |
5626 | error_init (const char *msgid) | |
5627 | { | |
5628 | char *ofwhat; | |
400fbf9f | 5629 | |
3e4093b6 | 5630 | error ("%s", _(msgid)); |
28dab132 | 5631 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5632 | if (*ofwhat) |
bda67431 | 5633 | error ("(near initialization for %qs)", ofwhat); |
3e4093b6 | 5634 | } |
400fbf9f | 5635 | |
fcf73884 MLI |
5636 | /* Issue a pedantic warning for a bad initializer component. OPT is |
5637 | the option OPT_* (from options.h) controlling this warning or 0 if | |
5638 | it is unconditionally given. MSGID identifies the message. The | |
5639 | component name is taken from the spelling stack. */ | |
400fbf9f | 5640 | |
3e4093b6 | 5641 | void |
509c9d60 | 5642 | pedwarn_init (location_t location, int opt, const char *msgid) |
3e4093b6 RS |
5643 | { |
5644 | char *ofwhat; | |
9f720c3e | 5645 | |
509c9d60 | 5646 | pedwarn (location, opt, "%s", _(msgid)); |
28dab132 | 5647 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5648 | if (*ofwhat) |
509c9d60 | 5649 | pedwarn (location, opt, "(near initialization for %qs)", ofwhat); |
3e4093b6 | 5650 | } |
9f720c3e | 5651 | |
b8698a0f | 5652 | /* Issue a warning for a bad initializer component. |
683d6ff9 MLI |
5653 | |
5654 | OPT is the OPT_W* value corresponding to the warning option that | |
5655 | controls this warning. MSGID identifies the message. The | |
5656 | component name is taken from the spelling stack. */ | |
61179109 | 5657 | |
3e4093b6 | 5658 | static void |
683d6ff9 | 5659 | warning_init (int opt, const char *msgid) |
3e4093b6 RS |
5660 | { |
5661 | char *ofwhat; | |
7e842ef8 | 5662 | |
683d6ff9 | 5663 | warning (opt, "%s", _(msgid)); |
28dab132 | 5664 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5665 | if (*ofwhat) |
683d6ff9 | 5666 | warning (opt, "(near initialization for %qs)", ofwhat); |
3e4093b6 RS |
5667 | } |
5668 | \f | |
916c5919 JM |
5669 | /* If TYPE is an array type and EXPR is a parenthesized string |
5670 | constant, warn if pedantic that EXPR is being used to initialize an | |
5671 | object of type TYPE. */ | |
5672 | ||
5673 | void | |
5674 | maybe_warn_string_init (tree type, struct c_expr expr) | |
5675 | { | |
5676 | if (pedantic | |
5677 | && TREE_CODE (type) == ARRAY_TYPE | |
5678 | && TREE_CODE (expr.value) == STRING_CST | |
5679 | && expr.original_code != STRING_CST) | |
b8698a0f | 5680 | pedwarn_init (input_location, OPT_pedantic, |
fcf73884 | 5681 | "array initialized from parenthesized string constant"); |
916c5919 JM |
5682 | } |
5683 | ||
3e4093b6 RS |
5684 | /* Digest the parser output INIT as an initializer for type TYPE. |
5685 | Return a C expression of type TYPE to represent the initial value. | |
7e842ef8 | 5686 | |
bbbbb16a ILT |
5687 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
5688 | ||
928c19bb JM |
5689 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. |
5690 | ||
916c5919 JM |
5691 | If INIT is a string constant, STRICT_STRING is true if it is |
5692 | unparenthesized or we should not warn here for it being parenthesized. | |
5693 | For other types of INIT, STRICT_STRING is not used. | |
5694 | ||
c2255bc4 AH |
5695 | INIT_LOC is the location of the INIT. |
5696 | ||
3e4093b6 RS |
5697 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
5698 | elements are seen. */ | |
7e842ef8 | 5699 | |
3e4093b6 | 5700 | static tree |
c2255bc4 AH |
5701 | digest_init (location_t init_loc, tree type, tree init, tree origtype, |
5702 | bool null_pointer_constant, bool strict_string, | |
5703 | int require_constant) | |
3e4093b6 RS |
5704 | { |
5705 | enum tree_code code = TREE_CODE (type); | |
5706 | tree inside_init = init; | |
8ce94e44 | 5707 | tree semantic_type = NULL_TREE; |
928c19bb | 5708 | bool maybe_const = true; |
7e842ef8 | 5709 | |
3e4093b6 | 5710 | if (type == error_mark_node |
f01da1a5 | 5711 | || !init |
3e4093b6 RS |
5712 | || init == error_mark_node |
5713 | || TREE_TYPE (init) == error_mark_node) | |
5714 | return error_mark_node; | |
7e842ef8 | 5715 | |
ed248cf7 | 5716 | STRIP_TYPE_NOPS (inside_init); |
7e842ef8 | 5717 | |
8ce94e44 JM |
5718 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) |
5719 | { | |
5720 | semantic_type = TREE_TYPE (inside_init); | |
5721 | inside_init = TREE_OPERAND (inside_init, 0); | |
5722 | } | |
928c19bb JM |
5723 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); |
5724 | inside_init = decl_constant_value_for_optimization (inside_init); | |
7e842ef8 | 5725 | |
3e4093b6 RS |
5726 | /* Initialization of an array of chars from a string constant |
5727 | optionally enclosed in braces. */ | |
7e842ef8 | 5728 | |
197463ae JM |
5729 | if (code == ARRAY_TYPE && inside_init |
5730 | && TREE_CODE (inside_init) == STRING_CST) | |
3e4093b6 RS |
5731 | { |
5732 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
197463ae JM |
5733 | /* Note that an array could be both an array of character type |
5734 | and an array of wchar_t if wchar_t is signed char or unsigned | |
5735 | char. */ | |
5736 | bool char_array = (typ1 == char_type_node | |
5737 | || typ1 == signed_char_type_node | |
5738 | || typ1 == unsigned_char_type_node); | |
5739 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
c466b2cd KVH |
5740 | bool char16_array = !!comptypes (typ1, char16_type_node); |
5741 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
5742 | ||
5743 | if (char_array || wchar_array || char16_array || char32_array) | |
7e842ef8 | 5744 | { |
916c5919 | 5745 | struct c_expr expr; |
c466b2cd | 5746 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); |
916c5919 JM |
5747 | expr.value = inside_init; |
5748 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
6866c6e8 | 5749 | expr.original_type = NULL; |
916c5919 JM |
5750 | maybe_warn_string_init (type, expr); |
5751 | ||
a45e580b | 5752 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
c2255bc4 | 5753 | pedwarn_init (init_loc, OPT_pedantic, |
a45e580b JM |
5754 | "initialization of a flexible array member"); |
5755 | ||
3e4093b6 | 5756 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 5757 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 | 5758 | return inside_init; |
7e842ef8 | 5759 | |
c466b2cd | 5760 | if (char_array) |
3e4093b6 | 5761 | { |
c466b2cd KVH |
5762 | if (typ2 != char_type_node) |
5763 | { | |
5764 | error_init ("char-array initialized from wide string"); | |
5765 | return error_mark_node; | |
5766 | } | |
3e4093b6 | 5767 | } |
c466b2cd | 5768 | else |
3e4093b6 | 5769 | { |
c466b2cd KVH |
5770 | if (typ2 == char_type_node) |
5771 | { | |
5772 | error_init ("wide character array initialized from non-wide " | |
5773 | "string"); | |
5774 | return error_mark_node; | |
5775 | } | |
5776 | else if (!comptypes(typ1, typ2)) | |
5777 | { | |
5778 | error_init ("wide character array initialized from " | |
5779 | "incompatible wide string"); | |
5780 | return error_mark_node; | |
5781 | } | |
7e842ef8 | 5782 | } |
2f6e4e97 | 5783 | |
3e4093b6 RS |
5784 | TREE_TYPE (inside_init) = type; |
5785 | if (TYPE_DOMAIN (type) != 0 | |
5786 | && TYPE_SIZE (type) != 0 | |
5eb4df45 ILT |
5787 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
5788 | { | |
5789 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
5790 | ||
c466b2cd | 5791 | /* Subtract the size of a single (possibly wide) character |
3e4093b6 RS |
5792 | because it's ok to ignore the terminating null char |
5793 | that is counted in the length of the constant. */ | |
5eb4df45 ILT |
5794 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), |
5795 | (len | |
5796 | - (TYPE_PRECISION (typ1) | |
5797 | / BITS_PER_UNIT)))) | |
5798 | pedwarn_init (init_loc, 0, | |
5799 | ("initializer-string for array of chars " | |
5800 | "is too long")); | |
5801 | else if (warn_cxx_compat | |
5802 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
5803 | warning_at (init_loc, OPT_Wc___compat, | |
5804 | ("initializer-string for array chars " | |
5805 | "is too long for C++")); | |
5806 | } | |
7e842ef8 | 5807 | |
3e4093b6 | 5808 | return inside_init; |
7e842ef8 | 5809 | } |
197463ae JM |
5810 | else if (INTEGRAL_TYPE_P (typ1)) |
5811 | { | |
5812 | error_init ("array of inappropriate type initialized " | |
5813 | "from string constant"); | |
5814 | return error_mark_node; | |
5815 | } | |
7e842ef8 PE |
5816 | } |
5817 | ||
3e4093b6 RS |
5818 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
5819 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
5820 | below and handle as a constructor. */ | |
e89be13b JJ |
5821 | if (code == VECTOR_TYPE |
5822 | && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE | |
00c8e9f6 | 5823 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) |
e89be13b JJ |
5824 | && TREE_CONSTANT (inside_init)) |
5825 | { | |
5826 | if (TREE_CODE (inside_init) == VECTOR_CST | |
5827 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
5828 | TYPE_MAIN_VARIANT (type))) | |
5829 | return inside_init; | |
5830 | ||
5831 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
5832 | { | |
4038c495 GB |
5833 | unsigned HOST_WIDE_INT ix; |
5834 | tree value; | |
5835 | bool constant_p = true; | |
e89be13b JJ |
5836 | |
5837 | /* Iterate through elements and check if all constructor | |
5838 | elements are *_CSTs. */ | |
4038c495 GB |
5839 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) |
5840 | if (!CONSTANT_CLASS_P (value)) | |
5841 | { | |
5842 | constant_p = false; | |
5843 | break; | |
5844 | } | |
e89be13b | 5845 | |
4038c495 GB |
5846 | if (constant_p) |
5847 | return build_vector_from_ctor (type, | |
5848 | CONSTRUCTOR_ELTS (inside_init)); | |
e89be13b JJ |
5849 | } |
5850 | } | |
6035d635 | 5851 | |
ca085fd7 MLI |
5852 | if (warn_sequence_point) |
5853 | verify_sequence_points (inside_init); | |
5854 | ||
3e4093b6 RS |
5855 | /* Any type can be initialized |
5856 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 5857 | |
3e4093b6 RS |
5858 | if (inside_init && TREE_TYPE (inside_init) != 0 |
5859 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
132da1a5 | 5860 | TYPE_MAIN_VARIANT (type)) |
3e4093b6 | 5861 | || (code == ARRAY_TYPE |
132da1a5 | 5862 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 5863 | || (code == VECTOR_TYPE |
132da1a5 | 5864 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 5865 | || (code == POINTER_TYPE |
3897f229 | 5866 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
3e4093b6 | 5867 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
132da1a5 | 5868 | TREE_TYPE (type))))) |
3e4093b6 RS |
5869 | { |
5870 | if (code == POINTER_TYPE) | |
b494fd98 | 5871 | { |
b494fd98 EB |
5872 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) |
5873 | { | |
f2a71bbc JM |
5874 | if (TREE_CODE (inside_init) == STRING_CST |
5875 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
c2255bc4 AH |
5876 | inside_init = array_to_pointer_conversion |
5877 | (init_loc, inside_init); | |
f2a71bbc JM |
5878 | else |
5879 | { | |
5880 | error_init ("invalid use of non-lvalue array"); | |
5881 | return error_mark_node; | |
5882 | } | |
b494fd98 | 5883 | } |
f2a71bbc | 5884 | } |
b494fd98 | 5885 | |
bae39a73 NS |
5886 | if (code == VECTOR_TYPE) |
5887 | /* Although the types are compatible, we may require a | |
5888 | conversion. */ | |
5889 | inside_init = convert (type, inside_init); | |
3e4093b6 | 5890 | |
ca58211b PB |
5891 | if (require_constant |
5892 | && (code == VECTOR_TYPE || !flag_isoc99) | |
3e4093b6 | 5893 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) |
400fbf9f | 5894 | { |
3e4093b6 RS |
5895 | /* As an extension, allow initializing objects with static storage |
5896 | duration with compound literals (which are then treated just as | |
ca58211b PB |
5897 | the brace enclosed list they contain). Also allow this for |
5898 | vectors, as we can only assign them with compound literals. */ | |
3e4093b6 RS |
5899 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
5900 | inside_init = DECL_INITIAL (decl); | |
400fbf9f | 5901 | } |
3e4093b6 RS |
5902 | |
5903 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
5904 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 5905 | { |
3e4093b6 RS |
5906 | error_init ("array initialized from non-constant array expression"); |
5907 | return error_mark_node; | |
400fbf9f | 5908 | } |
400fbf9f | 5909 | |
3e4093b6 RS |
5910 | /* Compound expressions can only occur here if -pedantic or |
5911 | -pedantic-errors is specified. In the later case, we always want | |
5912 | an error. In the former case, we simply want a warning. */ | |
5913 | if (require_constant && pedantic | |
5914 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
5915 | { | |
5916 | inside_init | |
5917 | = valid_compound_expr_initializer (inside_init, | |
5918 | TREE_TYPE (inside_init)); | |
5919 | if (inside_init == error_mark_node) | |
5920 | error_init ("initializer element is not constant"); | |
2f6e4e97 | 5921 | else |
c2255bc4 | 5922 | pedwarn_init (init_loc, OPT_pedantic, |
509c9d60 | 5923 | "initializer element is not constant"); |
3e4093b6 RS |
5924 | if (flag_pedantic_errors) |
5925 | inside_init = error_mark_node; | |
5926 | } | |
5927 | else if (require_constant | |
116df786 RH |
5928 | && !initializer_constant_valid_p (inside_init, |
5929 | TREE_TYPE (inside_init))) | |
3e4093b6 RS |
5930 | { |
5931 | error_init ("initializer element is not constant"); | |
5932 | inside_init = error_mark_node; | |
8b40563c | 5933 | } |
928c19bb | 5934 | else if (require_constant && !maybe_const) |
c2255bc4 | 5935 | pedwarn_init (init_loc, 0, |
928c19bb | 5936 | "initializer element is not a constant expression"); |
f735a153 | 5937 | |
8fcef540 KG |
5938 | /* Added to enable additional -Wmissing-format-attribute warnings. */ |
5939 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) | |
c2255bc4 AH |
5940 | inside_init = convert_for_assignment (init_loc, type, inside_init, |
5941 | origtype, | |
bbbbb16a | 5942 | ic_init, null_pointer_constant, |
928c19bb | 5943 | NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
5944 | return inside_init; |
5945 | } | |
f735a153 | 5946 | |
3e4093b6 | 5947 | /* Handle scalar types, including conversions. */ |
400fbf9f | 5948 | |
ab22c1fa CF |
5949 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE |
5950 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
5951 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
400fbf9f | 5952 | { |
f2a71bbc JM |
5953 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE |
5954 | && (TREE_CODE (init) == STRING_CST | |
5955 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
c2255bc4 | 5956 | inside_init = init = array_to_pointer_conversion (init_loc, init); |
8ce94e44 JM |
5957 | if (semantic_type) |
5958 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
5959 | inside_init); | |
3e4093b6 | 5960 | inside_init |
c2255bc4 AH |
5961 | = convert_for_assignment (init_loc, type, inside_init, origtype, |
5962 | ic_init, null_pointer_constant, | |
3e4093b6 | 5963 | NULL_TREE, NULL_TREE, 0); |
2f6e4e97 | 5964 | |
3274deff JW |
5965 | /* Check to see if we have already given an error message. */ |
5966 | if (inside_init == error_mark_node) | |
5967 | ; | |
3f75a254 | 5968 | else if (require_constant && !TREE_CONSTANT (inside_init)) |
400fbf9f | 5969 | { |
3e4093b6 RS |
5970 | error_init ("initializer element is not constant"); |
5971 | inside_init = error_mark_node; | |
400fbf9f | 5972 | } |
3e4093b6 | 5973 | else if (require_constant |
116df786 RH |
5974 | && !initializer_constant_valid_p (inside_init, |
5975 | TREE_TYPE (inside_init))) | |
400fbf9f | 5976 | { |
3e4093b6 RS |
5977 | error_init ("initializer element is not computable at load time"); |
5978 | inside_init = error_mark_node; | |
400fbf9f | 5979 | } |
928c19bb | 5980 | else if (require_constant && !maybe_const) |
c2255bc4 | 5981 | pedwarn_init (init_loc, 0, |
928c19bb | 5982 | "initializer element is not a constant expression"); |
3e4093b6 RS |
5983 | |
5984 | return inside_init; | |
400fbf9f | 5985 | } |
d9fc6069 | 5986 | |
3e4093b6 | 5987 | /* Come here only for records and arrays. */ |
d9fc6069 | 5988 | |
3e4093b6 | 5989 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
d9fc6069 | 5990 | { |
3e4093b6 RS |
5991 | error_init ("variable-sized object may not be initialized"); |
5992 | return error_mark_node; | |
d9fc6069 | 5993 | } |
3e4093b6 RS |
5994 | |
5995 | error_init ("invalid initializer"); | |
5996 | return error_mark_node; | |
d9fc6069 | 5997 | } |
400fbf9f | 5998 | \f |
3e4093b6 | 5999 | /* Handle initializers that use braces. */ |
400fbf9f | 6000 | |
3e4093b6 RS |
6001 | /* Type of object we are accumulating a constructor for. |
6002 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
6003 | static tree constructor_type; | |
400fbf9f | 6004 | |
3e4093b6 RS |
6005 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
6006 | left to fill. */ | |
6007 | static tree constructor_fields; | |
400fbf9f | 6008 | |
3e4093b6 RS |
6009 | /* For an ARRAY_TYPE, this is the specified index |
6010 | at which to store the next element we get. */ | |
6011 | static tree constructor_index; | |
400fbf9f | 6012 | |
3e4093b6 RS |
6013 | /* For an ARRAY_TYPE, this is the maximum index. */ |
6014 | static tree constructor_max_index; | |
400fbf9f | 6015 | |
3e4093b6 RS |
6016 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
6017 | static tree constructor_unfilled_fields; | |
400fbf9f | 6018 | |
3e4093b6 RS |
6019 | /* For an ARRAY_TYPE, this is the index of the first element |
6020 | not yet written out. */ | |
6021 | static tree constructor_unfilled_index; | |
895ea614 | 6022 | |
3e4093b6 RS |
6023 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
6024 | This is so we can generate gaps between fields, when appropriate. */ | |
6025 | static tree constructor_bit_index; | |
10d5caec | 6026 | |
3e4093b6 RS |
6027 | /* If we are saving up the elements rather than allocating them, |
6028 | this is the list of elements so far (in reverse order, | |
6029 | most recent first). */ | |
4038c495 | 6030 | static VEC(constructor_elt,gc) *constructor_elements; |
ad47f1e5 | 6031 | |
3e4093b6 RS |
6032 | /* 1 if constructor should be incrementally stored into a constructor chain, |
6033 | 0 if all the elements should be kept in AVL tree. */ | |
6034 | static int constructor_incremental; | |
ad47f1e5 | 6035 | |
3e4093b6 RS |
6036 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
6037 | static int constructor_constant; | |
ad47f1e5 | 6038 | |
3e4093b6 RS |
6039 | /* 1 if so far this constructor's elements are all valid address constants. */ |
6040 | static int constructor_simple; | |
ad47f1e5 | 6041 | |
928c19bb JM |
6042 | /* 1 if this constructor has an element that cannot be part of a |
6043 | constant expression. */ | |
6044 | static int constructor_nonconst; | |
6045 | ||
3e4093b6 RS |
6046 | /* 1 if this constructor is erroneous so far. */ |
6047 | static int constructor_erroneous; | |
d45cf215 | 6048 | |
3e4093b6 RS |
6049 | /* Structure for managing pending initializer elements, organized as an |
6050 | AVL tree. */ | |
d45cf215 | 6051 | |
3e4093b6 | 6052 | struct init_node |
d45cf215 | 6053 | { |
3e4093b6 RS |
6054 | struct init_node *left, *right; |
6055 | struct init_node *parent; | |
6056 | int balance; | |
6057 | tree purpose; | |
6058 | tree value; | |
bbbbb16a | 6059 | tree origtype; |
d45cf215 RS |
6060 | }; |
6061 | ||
3e4093b6 RS |
6062 | /* Tree of pending elements at this constructor level. |
6063 | These are elements encountered out of order | |
6064 | which belong at places we haven't reached yet in actually | |
6065 | writing the output. | |
6066 | Will never hold tree nodes across GC runs. */ | |
6067 | static struct init_node *constructor_pending_elts; | |
d45cf215 | 6068 | |
3e4093b6 RS |
6069 | /* The SPELLING_DEPTH of this constructor. */ |
6070 | static int constructor_depth; | |
d45cf215 | 6071 | |
3e4093b6 RS |
6072 | /* DECL node for which an initializer is being read. |
6073 | 0 means we are reading a constructor expression | |
6074 | such as (struct foo) {...}. */ | |
6075 | static tree constructor_decl; | |
d45cf215 | 6076 | |
3e4093b6 RS |
6077 | /* Nonzero if this is an initializer for a top-level decl. */ |
6078 | static int constructor_top_level; | |
d45cf215 | 6079 | |
3e4093b6 RS |
6080 | /* Nonzero if there were any member designators in this initializer. */ |
6081 | static int constructor_designated; | |
d45cf215 | 6082 | |
3e4093b6 RS |
6083 | /* Nesting depth of designator list. */ |
6084 | static int designator_depth; | |
d45cf215 | 6085 | |
3e4093b6 | 6086 | /* Nonzero if there were diagnosed errors in this designator list. */ |
b06df647 | 6087 | static int designator_erroneous; |
d45cf215 | 6088 | |
3e4093b6 RS |
6089 | \f |
6090 | /* This stack has a level for each implicit or explicit level of | |
6091 | structuring in the initializer, including the outermost one. It | |
6092 | saves the values of most of the variables above. */ | |
d45cf215 | 6093 | |
3e4093b6 RS |
6094 | struct constructor_range_stack; |
6095 | ||
6096 | struct constructor_stack | |
d45cf215 | 6097 | { |
3e4093b6 RS |
6098 | struct constructor_stack *next; |
6099 | tree type; | |
6100 | tree fields; | |
6101 | tree index; | |
6102 | tree max_index; | |
6103 | tree unfilled_index; | |
6104 | tree unfilled_fields; | |
6105 | tree bit_index; | |
4038c495 | 6106 | VEC(constructor_elt,gc) *elements; |
3e4093b6 RS |
6107 | struct init_node *pending_elts; |
6108 | int offset; | |
6109 | int depth; | |
916c5919 | 6110 | /* If value nonzero, this value should replace the entire |
3e4093b6 | 6111 | constructor at this level. */ |
916c5919 | 6112 | struct c_expr replacement_value; |
3e4093b6 RS |
6113 | struct constructor_range_stack *range_stack; |
6114 | char constant; | |
6115 | char simple; | |
928c19bb | 6116 | char nonconst; |
3e4093b6 RS |
6117 | char implicit; |
6118 | char erroneous; | |
6119 | char outer; | |
6120 | char incremental; | |
6121 | char designated; | |
6122 | }; | |
d45cf215 | 6123 | |
802415d1 | 6124 | static struct constructor_stack *constructor_stack; |
d45cf215 | 6125 | |
3e4093b6 RS |
6126 | /* This stack represents designators from some range designator up to |
6127 | the last designator in the list. */ | |
d45cf215 | 6128 | |
3e4093b6 RS |
6129 | struct constructor_range_stack |
6130 | { | |
6131 | struct constructor_range_stack *next, *prev; | |
6132 | struct constructor_stack *stack; | |
6133 | tree range_start; | |
6134 | tree index; | |
6135 | tree range_end; | |
6136 | tree fields; | |
6137 | }; | |
d45cf215 | 6138 | |
802415d1 | 6139 | static struct constructor_range_stack *constructor_range_stack; |
d45cf215 | 6140 | |
3e4093b6 RS |
6141 | /* This stack records separate initializers that are nested. |
6142 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
6143 | in cases like { ... (struct foo) { ... } ... }. */ | |
d45cf215 | 6144 | |
3e4093b6 | 6145 | struct initializer_stack |
d45cf215 | 6146 | { |
3e4093b6 RS |
6147 | struct initializer_stack *next; |
6148 | tree decl; | |
3e4093b6 RS |
6149 | struct constructor_stack *constructor_stack; |
6150 | struct constructor_range_stack *constructor_range_stack; | |
4038c495 | 6151 | VEC(constructor_elt,gc) *elements; |
3e4093b6 RS |
6152 | struct spelling *spelling; |
6153 | struct spelling *spelling_base; | |
6154 | int spelling_size; | |
6155 | char top_level; | |
6156 | char require_constant_value; | |
6157 | char require_constant_elements; | |
6158 | }; | |
d45cf215 | 6159 | |
802415d1 | 6160 | static struct initializer_stack *initializer_stack; |
3e4093b6 RS |
6161 | \f |
6162 | /* Prepare to parse and output the initializer for variable DECL. */ | |
400fbf9f JW |
6163 | |
6164 | void | |
a396f8ae | 6165 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) |
400fbf9f | 6166 | { |
3e4093b6 | 6167 | const char *locus; |
cceb1885 | 6168 | struct initializer_stack *p = XNEW (struct initializer_stack); |
400fbf9f | 6169 | |
3e4093b6 | 6170 | p->decl = constructor_decl; |
3e4093b6 RS |
6171 | p->require_constant_value = require_constant_value; |
6172 | p->require_constant_elements = require_constant_elements; | |
6173 | p->constructor_stack = constructor_stack; | |
6174 | p->constructor_range_stack = constructor_range_stack; | |
6175 | p->elements = constructor_elements; | |
6176 | p->spelling = spelling; | |
6177 | p->spelling_base = spelling_base; | |
6178 | p->spelling_size = spelling_size; | |
6179 | p->top_level = constructor_top_level; | |
6180 | p->next = initializer_stack; | |
6181 | initializer_stack = p; | |
400fbf9f | 6182 | |
3e4093b6 | 6183 | constructor_decl = decl; |
3e4093b6 RS |
6184 | constructor_designated = 0; |
6185 | constructor_top_level = top_level; | |
400fbf9f | 6186 | |
6f17bbcf | 6187 | if (decl != 0 && decl != error_mark_node) |
3e4093b6 RS |
6188 | { |
6189 | require_constant_value = TREE_STATIC (decl); | |
6190 | require_constant_elements | |
6191 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
6192 | /* For a scalar, you can always use any value to initialize, | |
6193 | even within braces. */ | |
6194 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
6195 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
6196 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
6197 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
88388a52 | 6198 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); |
3e4093b6 RS |
6199 | } |
6200 | else | |
6201 | { | |
6202 | require_constant_value = 0; | |
6203 | require_constant_elements = 0; | |
88388a52 | 6204 | locus = _("(anonymous)"); |
3e4093b6 | 6205 | } |
b71c7f8a | 6206 | |
3e4093b6 RS |
6207 | constructor_stack = 0; |
6208 | constructor_range_stack = 0; | |
b71c7f8a | 6209 | |
3e4093b6 RS |
6210 | missing_braces_mentioned = 0; |
6211 | ||
6212 | spelling_base = 0; | |
6213 | spelling_size = 0; | |
6214 | RESTORE_SPELLING_DEPTH (0); | |
6215 | ||
6216 | if (locus) | |
6217 | push_string (locus); | |
6218 | } | |
6219 | ||
6220 | void | |
6221 | finish_init (void) | |
b71c7f8a | 6222 | { |
3e4093b6 | 6223 | struct initializer_stack *p = initializer_stack; |
b71c7f8a | 6224 | |
3e4093b6 RS |
6225 | /* Free the whole constructor stack of this initializer. */ |
6226 | while (constructor_stack) | |
6227 | { | |
6228 | struct constructor_stack *q = constructor_stack; | |
6229 | constructor_stack = q->next; | |
6230 | free (q); | |
6231 | } | |
6232 | ||
366de0ce | 6233 | gcc_assert (!constructor_range_stack); |
3e4093b6 RS |
6234 | |
6235 | /* Pop back to the data of the outer initializer (if any). */ | |
36579663 | 6236 | free (spelling_base); |
3aeb3655 | 6237 | |
3e4093b6 | 6238 | constructor_decl = p->decl; |
3e4093b6 RS |
6239 | require_constant_value = p->require_constant_value; |
6240 | require_constant_elements = p->require_constant_elements; | |
6241 | constructor_stack = p->constructor_stack; | |
6242 | constructor_range_stack = p->constructor_range_stack; | |
6243 | constructor_elements = p->elements; | |
6244 | spelling = p->spelling; | |
6245 | spelling_base = p->spelling_base; | |
6246 | spelling_size = p->spelling_size; | |
6247 | constructor_top_level = p->top_level; | |
6248 | initializer_stack = p->next; | |
6249 | free (p); | |
b71c7f8a | 6250 | } |
400fbf9f | 6251 | \f |
3e4093b6 RS |
6252 | /* Call here when we see the initializer is surrounded by braces. |
6253 | This is instead of a call to push_init_level; | |
6254 | it is matched by a call to pop_init_level. | |
400fbf9f | 6255 | |
3e4093b6 RS |
6256 | TYPE is the type to initialize, for a constructor expression. |
6257 | For an initializer for a decl, TYPE is zero. */ | |
400fbf9f | 6258 | |
3e4093b6 RS |
6259 | void |
6260 | really_start_incremental_init (tree type) | |
400fbf9f | 6261 | { |
5d038c4c | 6262 | struct constructor_stack *p = XNEW (struct constructor_stack); |
400fbf9f | 6263 | |
3e4093b6 RS |
6264 | if (type == 0) |
6265 | type = TREE_TYPE (constructor_decl); | |
400fbf9f | 6266 | |
b6fc2cdb PB |
6267 | if (TREE_CODE (type) == VECTOR_TYPE |
6268 | && TYPE_VECTOR_OPAQUE (type)) | |
3e4093b6 | 6269 | error ("opaque vector types cannot be initialized"); |
400fbf9f | 6270 | |
3e4093b6 RS |
6271 | p->type = constructor_type; |
6272 | p->fields = constructor_fields; | |
6273 | p->index = constructor_index; | |
6274 | p->max_index = constructor_max_index; | |
6275 | p->unfilled_index = constructor_unfilled_index; | |
6276 | p->unfilled_fields = constructor_unfilled_fields; | |
6277 | p->bit_index = constructor_bit_index; | |
6278 | p->elements = constructor_elements; | |
6279 | p->constant = constructor_constant; | |
6280 | p->simple = constructor_simple; | |
928c19bb | 6281 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
6282 | p->erroneous = constructor_erroneous; |
6283 | p->pending_elts = constructor_pending_elts; | |
6284 | p->depth = constructor_depth; | |
916c5919 JM |
6285 | p->replacement_value.value = 0; |
6286 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 6287 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
6288 | p->implicit = 0; |
6289 | p->range_stack = 0; | |
6290 | p->outer = 0; | |
6291 | p->incremental = constructor_incremental; | |
6292 | p->designated = constructor_designated; | |
6293 | p->next = 0; | |
6294 | constructor_stack = p; | |
b13aca19 | 6295 | |
3e4093b6 RS |
6296 | constructor_constant = 1; |
6297 | constructor_simple = 1; | |
928c19bb | 6298 | constructor_nonconst = 0; |
3e4093b6 RS |
6299 | constructor_depth = SPELLING_DEPTH (); |
6300 | constructor_elements = 0; | |
6301 | constructor_pending_elts = 0; | |
6302 | constructor_type = type; | |
6303 | constructor_incremental = 1; | |
6304 | constructor_designated = 0; | |
6305 | designator_depth = 0; | |
b06df647 | 6306 | designator_erroneous = 0; |
400fbf9f | 6307 | |
3e4093b6 RS |
6308 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6309 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 6310 | { |
3e4093b6 RS |
6311 | constructor_fields = TYPE_FIELDS (constructor_type); |
6312 | /* Skip any nameless bit fields at the beginning. */ | |
6313 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6314 | && DECL_NAME (constructor_fields) == 0) | |
6315 | constructor_fields = TREE_CHAIN (constructor_fields); | |
05bccae2 | 6316 | |
3e4093b6 RS |
6317 | constructor_unfilled_fields = constructor_fields; |
6318 | constructor_bit_index = bitsize_zero_node; | |
400fbf9f | 6319 | } |
3e4093b6 RS |
6320 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
6321 | { | |
6322 | if (TYPE_DOMAIN (constructor_type)) | |
6323 | { | |
6324 | constructor_max_index | |
6325 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 6326 | |
3e4093b6 RS |
6327 | /* Detect non-empty initializations of zero-length arrays. */ |
6328 | if (constructor_max_index == NULL_TREE | |
6329 | && TYPE_SIZE (constructor_type)) | |
7d60be94 | 6330 | constructor_max_index = build_int_cst (NULL_TREE, -1); |
400fbf9f | 6331 | |
3e4093b6 RS |
6332 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
6333 | to initialize VLAs will cause a proper error; avoid tree | |
6334 | checking errors as well by setting a safe value. */ | |
6335 | if (constructor_max_index | |
6336 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
7d60be94 | 6337 | constructor_max_index = build_int_cst (NULL_TREE, -1); |
59c83dbf | 6338 | |
3e4093b6 RS |
6339 | constructor_index |
6340 | = convert (bitsizetype, | |
6341 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
59c83dbf | 6342 | } |
3e4093b6 | 6343 | else |
493179da JM |
6344 | { |
6345 | constructor_index = bitsize_zero_node; | |
6346 | constructor_max_index = NULL_TREE; | |
6347 | } | |
59c83dbf | 6348 | |
3e4093b6 RS |
6349 | constructor_unfilled_index = constructor_index; |
6350 | } | |
6351 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6352 | { | |
6353 | /* Vectors are like simple fixed-size arrays. */ | |
6354 | constructor_max_index = | |
7d60be94 | 6355 | build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
a0f0ab9f | 6356 | constructor_index = bitsize_zero_node; |
3e4093b6 RS |
6357 | constructor_unfilled_index = constructor_index; |
6358 | } | |
6359 | else | |
6360 | { | |
6361 | /* Handle the case of int x = {5}; */ | |
6362 | constructor_fields = constructor_type; | |
6363 | constructor_unfilled_fields = constructor_type; | |
6364 | } | |
6365 | } | |
6366 | \f | |
6367 | /* Push down into a subobject, for initialization. | |
6368 | If this is for an explicit set of braces, IMPLICIT is 0. | |
6369 | If it is because the next element belongs at a lower level, | |
6370 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
400fbf9f | 6371 | |
3e4093b6 RS |
6372 | void |
6373 | push_init_level (int implicit) | |
6374 | { | |
6375 | struct constructor_stack *p; | |
6376 | tree value = NULL_TREE; | |
400fbf9f | 6377 | |
3e4093b6 | 6378 | /* If we've exhausted any levels that didn't have braces, |
472d98b4 JM |
6379 | pop them now. If implicit == 1, this will have been done in |
6380 | process_init_element; do not repeat it here because in the case | |
6381 | of excess initializers for an empty aggregate this leads to an | |
6382 | infinite cycle of popping a level and immediately recreating | |
6383 | it. */ | |
6384 | if (implicit != 1) | |
3e4093b6 | 6385 | { |
472d98b4 JM |
6386 | while (constructor_stack->implicit) |
6387 | { | |
6388 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6389 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6390 | && constructor_fields == 0) | |
b295aee2 | 6391 | process_init_element (pop_init_level (1), true); |
472d98b4 JM |
6392 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE |
6393 | && constructor_max_index | |
6394 | && tree_int_cst_lt (constructor_max_index, | |
6395 | constructor_index)) | |
b295aee2 | 6396 | process_init_element (pop_init_level (1), true); |
472d98b4 JM |
6397 | else |
6398 | break; | |
6399 | } | |
3e4093b6 | 6400 | } |
400fbf9f | 6401 | |
3e4093b6 RS |
6402 | /* Unless this is an explicit brace, we need to preserve previous |
6403 | content if any. */ | |
6404 | if (implicit) | |
6405 | { | |
6406 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6407 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6408 | && constructor_fields) | |
6409 | value = find_init_member (constructor_fields); | |
6410 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6411 | value = find_init_member (constructor_index); | |
400fbf9f JW |
6412 | } |
6413 | ||
5d038c4c | 6414 | p = XNEW (struct constructor_stack); |
3e4093b6 RS |
6415 | p->type = constructor_type; |
6416 | p->fields = constructor_fields; | |
6417 | p->index = constructor_index; | |
6418 | p->max_index = constructor_max_index; | |
6419 | p->unfilled_index = constructor_unfilled_index; | |
6420 | p->unfilled_fields = constructor_unfilled_fields; | |
6421 | p->bit_index = constructor_bit_index; | |
6422 | p->elements = constructor_elements; | |
6423 | p->constant = constructor_constant; | |
6424 | p->simple = constructor_simple; | |
928c19bb | 6425 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
6426 | p->erroneous = constructor_erroneous; |
6427 | p->pending_elts = constructor_pending_elts; | |
6428 | p->depth = constructor_depth; | |
916c5919 JM |
6429 | p->replacement_value.value = 0; |
6430 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 6431 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
6432 | p->implicit = implicit; |
6433 | p->outer = 0; | |
6434 | p->incremental = constructor_incremental; | |
6435 | p->designated = constructor_designated; | |
6436 | p->next = constructor_stack; | |
6437 | p->range_stack = 0; | |
6438 | constructor_stack = p; | |
400fbf9f | 6439 | |
3e4093b6 RS |
6440 | constructor_constant = 1; |
6441 | constructor_simple = 1; | |
928c19bb | 6442 | constructor_nonconst = 0; |
3e4093b6 RS |
6443 | constructor_depth = SPELLING_DEPTH (); |
6444 | constructor_elements = 0; | |
6445 | constructor_incremental = 1; | |
6446 | constructor_designated = 0; | |
6447 | constructor_pending_elts = 0; | |
6448 | if (!implicit) | |
400fbf9f | 6449 | { |
3e4093b6 RS |
6450 | p->range_stack = constructor_range_stack; |
6451 | constructor_range_stack = 0; | |
6452 | designator_depth = 0; | |
b06df647 | 6453 | designator_erroneous = 0; |
3e4093b6 | 6454 | } |
400fbf9f | 6455 | |
3e4093b6 RS |
6456 | /* Don't die if an entire brace-pair level is superfluous |
6457 | in the containing level. */ | |
6458 | if (constructor_type == 0) | |
6459 | ; | |
6460 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6461 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6462 | { | |
6463 | /* Don't die if there are extra init elts at the end. */ | |
6464 | if (constructor_fields == 0) | |
6465 | constructor_type = 0; | |
6466 | else | |
400fbf9f | 6467 | { |
3e4093b6 RS |
6468 | constructor_type = TREE_TYPE (constructor_fields); |
6469 | push_member_name (constructor_fields); | |
6470 | constructor_depth++; | |
400fbf9f | 6471 | } |
3e4093b6 RS |
6472 | } |
6473 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6474 | { | |
6475 | constructor_type = TREE_TYPE (constructor_type); | |
a0f0ab9f | 6476 | push_array_bounds (tree_low_cst (constructor_index, 1)); |
3e4093b6 | 6477 | constructor_depth++; |
400fbf9f JW |
6478 | } |
6479 | ||
3e4093b6 | 6480 | if (constructor_type == 0) |
400fbf9f | 6481 | { |
3e4093b6 RS |
6482 | error_init ("extra brace group at end of initializer"); |
6483 | constructor_fields = 0; | |
6484 | constructor_unfilled_fields = 0; | |
6485 | return; | |
400fbf9f JW |
6486 | } |
6487 | ||
3e4093b6 RS |
6488 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
6489 | { | |
6490 | constructor_constant = TREE_CONSTANT (value); | |
6491 | constructor_simple = TREE_STATIC (value); | |
928c19bb | 6492 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); |
3e4093b6 | 6493 | constructor_elements = CONSTRUCTOR_ELTS (value); |
4038c495 | 6494 | if (!VEC_empty (constructor_elt, constructor_elements) |
3e4093b6 RS |
6495 | && (TREE_CODE (constructor_type) == RECORD_TYPE |
6496 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
6497 | set_nonincremental_init (); | |
6498 | } | |
400fbf9f | 6499 | |
3e4093b6 RS |
6500 | if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) |
6501 | { | |
6502 | missing_braces_mentioned = 1; | |
683d6ff9 | 6503 | warning_init (OPT_Wmissing_braces, "missing braces around initializer"); |
3e4093b6 | 6504 | } |
400fbf9f | 6505 | |
3e4093b6 RS |
6506 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6507 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6508 | { | |
6509 | constructor_fields = TYPE_FIELDS (constructor_type); | |
6510 | /* Skip any nameless bit fields at the beginning. */ | |
6511 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6512 | && DECL_NAME (constructor_fields) == 0) | |
6513 | constructor_fields = TREE_CHAIN (constructor_fields); | |
103b7b17 | 6514 | |
3e4093b6 RS |
6515 | constructor_unfilled_fields = constructor_fields; |
6516 | constructor_bit_index = bitsize_zero_node; | |
6517 | } | |
6518 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6519 | { | |
6520 | /* Vectors are like simple fixed-size arrays. */ | |
6521 | constructor_max_index = | |
7d60be94 | 6522 | build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
3e4093b6 RS |
6523 | constructor_index = convert (bitsizetype, integer_zero_node); |
6524 | constructor_unfilled_index = constructor_index; | |
6525 | } | |
6526 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6527 | { | |
6528 | if (TYPE_DOMAIN (constructor_type)) | |
6529 | { | |
6530 | constructor_max_index | |
6531 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 6532 | |
3e4093b6 RS |
6533 | /* Detect non-empty initializations of zero-length arrays. */ |
6534 | if (constructor_max_index == NULL_TREE | |
6535 | && TYPE_SIZE (constructor_type)) | |
7d60be94 | 6536 | constructor_max_index = build_int_cst (NULL_TREE, -1); |
de520661 | 6537 | |
3e4093b6 RS |
6538 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
6539 | to initialize VLAs will cause a proper error; avoid tree | |
6540 | checking errors as well by setting a safe value. */ | |
6541 | if (constructor_max_index | |
6542 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
7d60be94 | 6543 | constructor_max_index = build_int_cst (NULL_TREE, -1); |
b62acd60 | 6544 | |
3e4093b6 RS |
6545 | constructor_index |
6546 | = convert (bitsizetype, | |
6547 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
6548 | } | |
6549 | else | |
6550 | constructor_index = bitsize_zero_node; | |
de520661 | 6551 | |
3e4093b6 RS |
6552 | constructor_unfilled_index = constructor_index; |
6553 | if (value && TREE_CODE (value) == STRING_CST) | |
6554 | { | |
6555 | /* We need to split the char/wchar array into individual | |
6556 | characters, so that we don't have to special case it | |
6557 | everywhere. */ | |
6558 | set_nonincremental_init_from_string (value); | |
6559 | } | |
6560 | } | |
6561 | else | |
6562 | { | |
b4519d39 | 6563 | if (constructor_type != error_mark_node) |
683d6ff9 | 6564 | warning_init (0, "braces around scalar initializer"); |
3e4093b6 RS |
6565 | constructor_fields = constructor_type; |
6566 | constructor_unfilled_fields = constructor_type; | |
6567 | } | |
6568 | } | |
8b6a5902 | 6569 | |
3e4093b6 | 6570 | /* At the end of an implicit or explicit brace level, |
916c5919 JM |
6571 | finish up that level of constructor. If a single expression |
6572 | with redundant braces initialized that level, return the | |
6573 | c_expr structure for that expression. Otherwise, the original_code | |
6574 | element is set to ERROR_MARK. | |
6575 | If we were outputting the elements as they are read, return 0 as the value | |
3e4093b6 | 6576 | from inner levels (process_init_element ignores that), |
916c5919 | 6577 | but return error_mark_node as the value from the outermost level |
3e4093b6 | 6578 | (that's what we want to put in DECL_INITIAL). |
916c5919 | 6579 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
de520661 | 6580 | |
916c5919 | 6581 | struct c_expr |
3e4093b6 RS |
6582 | pop_init_level (int implicit) |
6583 | { | |
6584 | struct constructor_stack *p; | |
916c5919 JM |
6585 | struct c_expr ret; |
6586 | ret.value = 0; | |
6587 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 6588 | ret.original_type = NULL; |
de520661 | 6589 | |
3e4093b6 RS |
6590 | if (implicit == 0) |
6591 | { | |
6592 | /* When we come to an explicit close brace, | |
6593 | pop any inner levels that didn't have explicit braces. */ | |
6594 | while (constructor_stack->implicit) | |
b295aee2 | 6595 | process_init_element (pop_init_level (1), true); |
de520661 | 6596 | |
366de0ce | 6597 | gcc_assert (!constructor_range_stack); |
3e4093b6 | 6598 | } |
e5e809f4 | 6599 | |
0066ef9c RH |
6600 | /* Now output all pending elements. */ |
6601 | constructor_incremental = 1; | |
6602 | output_pending_init_elements (1); | |
6603 | ||
3e4093b6 | 6604 | p = constructor_stack; |
e5e809f4 | 6605 | |
3e4093b6 RS |
6606 | /* Error for initializing a flexible array member, or a zero-length |
6607 | array member in an inappropriate context. */ | |
6608 | if (constructor_type && constructor_fields | |
6609 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
6610 | && TYPE_DOMAIN (constructor_type) | |
3f75a254 | 6611 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
3e4093b6 RS |
6612 | { |
6613 | /* Silently discard empty initializations. The parser will | |
6614 | already have pedwarned for empty brackets. */ | |
6615 | if (integer_zerop (constructor_unfilled_index)) | |
6616 | constructor_type = NULL_TREE; | |
366de0ce | 6617 | else |
3e4093b6 | 6618 | { |
366de0ce | 6619 | gcc_assert (!TYPE_SIZE (constructor_type)); |
c22cacf3 | 6620 | |
3e4093b6 RS |
6621 | if (constructor_depth > 2) |
6622 | error_init ("initialization of flexible array member in a nested context"); | |
fcf73884 | 6623 | else |
509c9d60 MLI |
6624 | pedwarn_init (input_location, OPT_pedantic, |
6625 | "initialization of a flexible array member"); | |
de520661 | 6626 | |
3e4093b6 RS |
6627 | /* We have already issued an error message for the existence |
6628 | of a flexible array member not at the end of the structure. | |
535a42b1 | 6629 | Discard the initializer so that we do not die later. */ |
3e4093b6 RS |
6630 | if (TREE_CHAIN (constructor_fields) != NULL_TREE) |
6631 | constructor_type = NULL_TREE; | |
6632 | } | |
3e4093b6 | 6633 | } |
de520661 | 6634 | |
3e4093b6 | 6635 | /* Warn when some struct elements are implicitly initialized to zero. */ |
eaac4679 | 6636 | if (warn_missing_field_initializers |
3e4093b6 RS |
6637 | && constructor_type |
6638 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
6639 | && constructor_unfilled_fields) | |
6640 | { | |
6641 | /* Do not warn for flexible array members or zero-length arrays. */ | |
6642 | while (constructor_unfilled_fields | |
3f75a254 | 6643 | && (!DECL_SIZE (constructor_unfilled_fields) |
3e4093b6 RS |
6644 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) |
6645 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
cc77d4d5 | 6646 | |
3e4093b6 RS |
6647 | /* Do not warn if this level of the initializer uses member |
6648 | designators; it is likely to be deliberate. */ | |
6649 | if (constructor_unfilled_fields && !constructor_designated) | |
6650 | { | |
6651 | push_member_name (constructor_unfilled_fields); | |
683d6ff9 MLI |
6652 | warning_init (OPT_Wmissing_field_initializers, |
6653 | "missing initializer"); | |
3e4093b6 RS |
6654 | RESTORE_SPELLING_DEPTH (constructor_depth); |
6655 | } | |
6656 | } | |
de520661 | 6657 | |
3e4093b6 | 6658 | /* Pad out the end of the structure. */ |
916c5919 | 6659 | if (p->replacement_value.value) |
3e4093b6 RS |
6660 | /* If this closes a superfluous brace pair, |
6661 | just pass out the element between them. */ | |
916c5919 | 6662 | ret = p->replacement_value; |
3e4093b6 RS |
6663 | else if (constructor_type == 0) |
6664 | ; | |
6665 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
6666 | && TREE_CODE (constructor_type) != UNION_TYPE | |
6667 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
6668 | && TREE_CODE (constructor_type) != VECTOR_TYPE) | |
6669 | { | |
6670 | /* A nonincremental scalar initializer--just return | |
6671 | the element, after verifying there is just one. */ | |
4038c495 | 6672 | if (VEC_empty (constructor_elt,constructor_elements)) |
3e4093b6 RS |
6673 | { |
6674 | if (!constructor_erroneous) | |
6675 | error_init ("empty scalar initializer"); | |
916c5919 | 6676 | ret.value = error_mark_node; |
3e4093b6 | 6677 | } |
4038c495 | 6678 | else if (VEC_length (constructor_elt,constructor_elements) != 1) |
3e4093b6 RS |
6679 | { |
6680 | error_init ("extra elements in scalar initializer"); | |
4038c495 | 6681 | ret.value = VEC_index (constructor_elt,constructor_elements,0)->value; |
3e4093b6 RS |
6682 | } |
6683 | else | |
4038c495 | 6684 | ret.value = VEC_index (constructor_elt,constructor_elements,0)->value; |
3e4093b6 RS |
6685 | } |
6686 | else | |
6687 | { | |
6688 | if (constructor_erroneous) | |
916c5919 | 6689 | ret.value = error_mark_node; |
3e4093b6 RS |
6690 | else |
6691 | { | |
916c5919 | 6692 | ret.value = build_constructor (constructor_type, |
4038c495 | 6693 | constructor_elements); |
3e4093b6 | 6694 | if (constructor_constant) |
51eed280 | 6695 | TREE_CONSTANT (ret.value) = 1; |
3e4093b6 | 6696 | if (constructor_constant && constructor_simple) |
916c5919 | 6697 | TREE_STATIC (ret.value) = 1; |
928c19bb JM |
6698 | if (constructor_nonconst) |
6699 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
3e4093b6 RS |
6700 | } |
6701 | } | |
de520661 | 6702 | |
928c19bb JM |
6703 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) |
6704 | { | |
6705 | if (constructor_nonconst) | |
6706 | ret.original_code = C_MAYBE_CONST_EXPR; | |
6707 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
6708 | ret.original_code = ERROR_MARK; | |
6709 | } | |
6710 | ||
3e4093b6 RS |
6711 | constructor_type = p->type; |
6712 | constructor_fields = p->fields; | |
6713 | constructor_index = p->index; | |
6714 | constructor_max_index = p->max_index; | |
6715 | constructor_unfilled_index = p->unfilled_index; | |
6716 | constructor_unfilled_fields = p->unfilled_fields; | |
6717 | constructor_bit_index = p->bit_index; | |
6718 | constructor_elements = p->elements; | |
6719 | constructor_constant = p->constant; | |
6720 | constructor_simple = p->simple; | |
928c19bb | 6721 | constructor_nonconst = p->nonconst; |
3e4093b6 RS |
6722 | constructor_erroneous = p->erroneous; |
6723 | constructor_incremental = p->incremental; | |
6724 | constructor_designated = p->designated; | |
6725 | constructor_pending_elts = p->pending_elts; | |
6726 | constructor_depth = p->depth; | |
6727 | if (!p->implicit) | |
6728 | constructor_range_stack = p->range_stack; | |
6729 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
de520661 | 6730 | |
3e4093b6 RS |
6731 | constructor_stack = p->next; |
6732 | free (p); | |
b621a4dd | 6733 | |
5d5e98dc VR |
6734 | if (ret.value == 0 && constructor_stack == 0) |
6735 | ret.value = error_mark_node; | |
916c5919 | 6736 | return ret; |
3e4093b6 | 6737 | } |
8b6a5902 | 6738 | |
3e4093b6 RS |
6739 | /* Common handling for both array range and field name designators. |
6740 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
400fbf9f | 6741 | |
3e4093b6 RS |
6742 | static int |
6743 | set_designator (int array) | |
de520661 | 6744 | { |
3e4093b6 RS |
6745 | tree subtype; |
6746 | enum tree_code subcode; | |
de520661 | 6747 | |
3e4093b6 RS |
6748 | /* Don't die if an entire brace-pair level is superfluous |
6749 | in the containing level. */ | |
6750 | if (constructor_type == 0) | |
6751 | return 1; | |
de520661 | 6752 | |
366de0ce NS |
6753 | /* If there were errors in this designator list already, bail out |
6754 | silently. */ | |
b06df647 | 6755 | if (designator_erroneous) |
3e4093b6 | 6756 | return 1; |
e28cae4f | 6757 | |
3e4093b6 RS |
6758 | if (!designator_depth) |
6759 | { | |
366de0ce | 6760 | gcc_assert (!constructor_range_stack); |
de520661 | 6761 | |
3e4093b6 RS |
6762 | /* Designator list starts at the level of closest explicit |
6763 | braces. */ | |
6764 | while (constructor_stack->implicit) | |
b295aee2 | 6765 | process_init_element (pop_init_level (1), true); |
3e4093b6 RS |
6766 | constructor_designated = 1; |
6767 | return 0; | |
6768 | } | |
de520661 | 6769 | |
366de0ce | 6770 | switch (TREE_CODE (constructor_type)) |
3c3fa147 | 6771 | { |
366de0ce NS |
6772 | case RECORD_TYPE: |
6773 | case UNION_TYPE: | |
3e4093b6 RS |
6774 | subtype = TREE_TYPE (constructor_fields); |
6775 | if (subtype != error_mark_node) | |
6776 | subtype = TYPE_MAIN_VARIANT (subtype); | |
366de0ce NS |
6777 | break; |
6778 | case ARRAY_TYPE: | |
3e4093b6 | 6779 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
366de0ce NS |
6780 | break; |
6781 | default: | |
6782 | gcc_unreachable (); | |
de520661 | 6783 | } |
400fbf9f | 6784 | |
3e4093b6 RS |
6785 | subcode = TREE_CODE (subtype); |
6786 | if (array && subcode != ARRAY_TYPE) | |
6787 | { | |
6788 | error_init ("array index in non-array initializer"); | |
6789 | return 1; | |
6790 | } | |
6791 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
6792 | { | |
6793 | error_init ("field name not in record or union initializer"); | |
6794 | return 1; | |
6795 | } | |
d45cf215 | 6796 | |
3e4093b6 RS |
6797 | constructor_designated = 1; |
6798 | push_init_level (2); | |
6799 | return 0; | |
de520661 | 6800 | } |
400fbf9f | 6801 | |
3e4093b6 RS |
6802 | /* If there are range designators in designator list, push a new designator |
6803 | to constructor_range_stack. RANGE_END is end of such stack range or | |
6804 | NULL_TREE if there is no range designator at this level. */ | |
400fbf9f | 6805 | |
3e4093b6 RS |
6806 | static void |
6807 | push_range_stack (tree range_end) | |
6808 | { | |
6809 | struct constructor_range_stack *p; | |
400fbf9f | 6810 | |
5d038c4c | 6811 | p = GGC_NEW (struct constructor_range_stack); |
3e4093b6 RS |
6812 | p->prev = constructor_range_stack; |
6813 | p->next = 0; | |
6814 | p->fields = constructor_fields; | |
6815 | p->range_start = constructor_index; | |
6816 | p->index = constructor_index; | |
6817 | p->stack = constructor_stack; | |
6818 | p->range_end = range_end; | |
8b6a5902 | 6819 | if (constructor_range_stack) |
3e4093b6 RS |
6820 | constructor_range_stack->next = p; |
6821 | constructor_range_stack = p; | |
de520661 | 6822 | } |
400fbf9f | 6823 | |
3e4093b6 RS |
6824 | /* Within an array initializer, specify the next index to be initialized. |
6825 | FIRST is that index. If LAST is nonzero, then initialize a range | |
6826 | of indices, running from FIRST through LAST. */ | |
5a7ec9d9 | 6827 | |
de520661 | 6828 | void |
3e4093b6 | 6829 | set_init_index (tree first, tree last) |
de520661 | 6830 | { |
3e4093b6 RS |
6831 | if (set_designator (1)) |
6832 | return; | |
de520661 | 6833 | |
b06df647 | 6834 | designator_erroneous = 1; |
de520661 | 6835 | |
3ea8cd06 JM |
6836 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
6837 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
6838 | { | |
6839 | error_init ("array index in initializer not of integer type"); | |
6840 | return; | |
6841 | } | |
6842 | ||
2b6da65c JM |
6843 | if (TREE_CODE (first) != INTEGER_CST) |
6844 | { | |
6845 | first = c_fully_fold (first, false, NULL); | |
6846 | if (TREE_CODE (first) == INTEGER_CST) | |
6847 | pedwarn_init (input_location, OPT_pedantic, | |
6848 | "array index in initializer is not " | |
6849 | "an integer constant expression"); | |
6850 | } | |
6851 | ||
6852 | if (last && TREE_CODE (last) != INTEGER_CST) | |
6853 | { | |
6854 | last = c_fully_fold (last, false, NULL); | |
6855 | if (TREE_CODE (last) == INTEGER_CST) | |
6856 | pedwarn_init (input_location, OPT_pedantic, | |
6857 | "array index in initializer is not " | |
6858 | "an integer constant expression"); | |
6859 | } | |
6860 | ||
3e4093b6 RS |
6861 | if (TREE_CODE (first) != INTEGER_CST) |
6862 | error_init ("nonconstant array index in initializer"); | |
6863 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) | |
6864 | error_init ("nonconstant array index in initializer"); | |
6865 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) | |
6866 | error_init ("array index in non-array initializer"); | |
622adc7e MK |
6867 | else if (tree_int_cst_sgn (first) == -1) |
6868 | error_init ("array index in initializer exceeds array bounds"); | |
3e4093b6 RS |
6869 | else if (constructor_max_index |
6870 | && tree_int_cst_lt (constructor_max_index, first)) | |
6871 | error_init ("array index in initializer exceeds array bounds"); | |
6872 | else | |
de520661 | 6873 | { |
928c19bb JM |
6874 | constant_expression_warning (first); |
6875 | if (last) | |
6876 | constant_expression_warning (last); | |
3e4093b6 | 6877 | constructor_index = convert (bitsizetype, first); |
665f2503 | 6878 | |
3e4093b6 | 6879 | if (last) |
2bede729 | 6880 | { |
3e4093b6 RS |
6881 | if (tree_int_cst_equal (first, last)) |
6882 | last = 0; | |
6883 | else if (tree_int_cst_lt (last, first)) | |
6884 | { | |
6885 | error_init ("empty index range in initializer"); | |
6886 | last = 0; | |
6887 | } | |
6888 | else | |
6889 | { | |
6890 | last = convert (bitsizetype, last); | |
6891 | if (constructor_max_index != 0 | |
6892 | && tree_int_cst_lt (constructor_max_index, last)) | |
6893 | { | |
6894 | error_init ("array index range in initializer exceeds array bounds"); | |
6895 | last = 0; | |
6896 | } | |
6897 | } | |
2bede729 | 6898 | } |
fed3cef0 | 6899 | |
3e4093b6 | 6900 | designator_depth++; |
b06df647 | 6901 | designator_erroneous = 0; |
3e4093b6 RS |
6902 | if (constructor_range_stack || last) |
6903 | push_range_stack (last); | |
de520661 | 6904 | } |
de520661 | 6905 | } |
3e4093b6 RS |
6906 | |
6907 | /* Within a struct initializer, specify the next field to be initialized. */ | |
400fbf9f | 6908 | |
de520661 | 6909 | void |
3e4093b6 | 6910 | set_init_label (tree fieldname) |
de520661 | 6911 | { |
3e4093b6 | 6912 | tree tail; |
94ba5069 | 6913 | |
3e4093b6 RS |
6914 | if (set_designator (0)) |
6915 | return; | |
6916 | ||
b06df647 | 6917 | designator_erroneous = 1; |
3e4093b6 RS |
6918 | |
6919 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
6920 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
94ba5069 | 6921 | { |
3e4093b6 RS |
6922 | error_init ("field name not in record or union initializer"); |
6923 | return; | |
94ba5069 RS |
6924 | } |
6925 | ||
3e4093b6 RS |
6926 | for (tail = TYPE_FIELDS (constructor_type); tail; |
6927 | tail = TREE_CHAIN (tail)) | |
8b6a5902 | 6928 | { |
3e4093b6 RS |
6929 | if (DECL_NAME (tail) == fieldname) |
6930 | break; | |
8b6a5902 JJ |
6931 | } |
6932 | ||
3e4093b6 | 6933 | if (tail == 0) |
c51a1ba9 | 6934 | error ("unknown field %qE specified in initializer", fieldname); |
3e4093b6 | 6935 | else |
8b6a5902 | 6936 | { |
3e4093b6 RS |
6937 | constructor_fields = tail; |
6938 | designator_depth++; | |
b06df647 | 6939 | designator_erroneous = 0; |
3e4093b6 RS |
6940 | if (constructor_range_stack) |
6941 | push_range_stack (NULL_TREE); | |
8b6a5902 | 6942 | } |
3e4093b6 RS |
6943 | } |
6944 | \f | |
6945 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
6946 | identifies the initializer, either array index or field in a structure. | |
bbbbb16a ILT |
6947 | VALUE is the value of that index or field. If ORIGTYPE is not |
6948 | NULL_TREE, it is the original type of VALUE. | |
b295aee2 JJ |
6949 | |
6950 | IMPLICIT is true if value comes from pop_init_level (1), | |
6951 | the new initializer has been merged with the existing one | |
6952 | and thus no warnings should be emitted about overriding an | |
6953 | existing initializer. */ | |
de520661 | 6954 | |
3e4093b6 | 6955 | static void |
bbbbb16a | 6956 | add_pending_init (tree purpose, tree value, tree origtype, bool implicit) |
3e4093b6 RS |
6957 | { |
6958 | struct init_node *p, **q, *r; | |
6959 | ||
6960 | q = &constructor_pending_elts; | |
6961 | p = 0; | |
6962 | ||
6963 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 6964 | { |
3e4093b6 | 6965 | while (*q != 0) |
91fa3c30 | 6966 | { |
3e4093b6 RS |
6967 | p = *q; |
6968 | if (tree_int_cst_lt (purpose, p->purpose)) | |
6969 | q = &p->left; | |
6970 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
6971 | q = &p->right; | |
6972 | else | |
6973 | { | |
b295aee2 JJ |
6974 | if (!implicit) |
6975 | { | |
6976 | if (TREE_SIDE_EFFECTS (p->value)) | |
6977 | warning_init (0, "initialized field with side-effects overwritten"); | |
6978 | else if (warn_override_init) | |
6979 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
6980 | } | |
3e4093b6 | 6981 | p->value = value; |
bbbbb16a | 6982 | p->origtype = origtype; |
3e4093b6 RS |
6983 | return; |
6984 | } | |
91fa3c30 | 6985 | } |
de520661 | 6986 | } |
3e4093b6 | 6987 | else |
de520661 | 6988 | { |
3e4093b6 | 6989 | tree bitpos; |
400fbf9f | 6990 | |
3e4093b6 RS |
6991 | bitpos = bit_position (purpose); |
6992 | while (*q != NULL) | |
6993 | { | |
6994 | p = *q; | |
6995 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
6996 | q = &p->left; | |
6997 | else if (p->purpose != purpose) | |
6998 | q = &p->right; | |
6999 | else | |
7000 | { | |
b295aee2 JJ |
7001 | if (!implicit) |
7002 | { | |
7003 | if (TREE_SIDE_EFFECTS (p->value)) | |
7004 | warning_init (0, "initialized field with side-effects overwritten"); | |
7005 | else if (warn_override_init) | |
7006 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7007 | } | |
3e4093b6 | 7008 | p->value = value; |
bbbbb16a | 7009 | p->origtype = origtype; |
3e4093b6 RS |
7010 | return; |
7011 | } | |
7012 | } | |
91fa3c30 | 7013 | } |
b71c7f8a | 7014 | |
5d038c4c | 7015 | r = GGC_NEW (struct init_node); |
3e4093b6 RS |
7016 | r->purpose = purpose; |
7017 | r->value = value; | |
bbbbb16a | 7018 | r->origtype = origtype; |
8b6a5902 | 7019 | |
3e4093b6 RS |
7020 | *q = r; |
7021 | r->parent = p; | |
7022 | r->left = 0; | |
7023 | r->right = 0; | |
7024 | r->balance = 0; | |
b71c7f8a | 7025 | |
3e4093b6 | 7026 | while (p) |
de520661 | 7027 | { |
3e4093b6 | 7028 | struct init_node *s; |
665f2503 | 7029 | |
3e4093b6 | 7030 | if (r == p->left) |
2bede729 | 7031 | { |
3e4093b6 RS |
7032 | if (p->balance == 0) |
7033 | p->balance = -1; | |
7034 | else if (p->balance < 0) | |
7035 | { | |
7036 | if (r->balance < 0) | |
7037 | { | |
7038 | /* L rotation. */ | |
7039 | p->left = r->right; | |
7040 | if (p->left) | |
7041 | p->left->parent = p; | |
7042 | r->right = p; | |
e7b6a0ee | 7043 | |
3e4093b6 RS |
7044 | p->balance = 0; |
7045 | r->balance = 0; | |
39bc99c2 | 7046 | |
3e4093b6 RS |
7047 | s = p->parent; |
7048 | p->parent = r; | |
7049 | r->parent = s; | |
7050 | if (s) | |
7051 | { | |
7052 | if (s->left == p) | |
7053 | s->left = r; | |
7054 | else | |
7055 | s->right = r; | |
7056 | } | |
7057 | else | |
7058 | constructor_pending_elts = r; | |
7059 | } | |
7060 | else | |
7061 | { | |
7062 | /* LR rotation. */ | |
7063 | struct init_node *t = r->right; | |
e7b6a0ee | 7064 | |
3e4093b6 RS |
7065 | r->right = t->left; |
7066 | if (r->right) | |
7067 | r->right->parent = r; | |
7068 | t->left = r; | |
7069 | ||
7070 | p->left = t->right; | |
7071 | if (p->left) | |
7072 | p->left->parent = p; | |
7073 | t->right = p; | |
7074 | ||
7075 | p->balance = t->balance < 0; | |
7076 | r->balance = -(t->balance > 0); | |
7077 | t->balance = 0; | |
7078 | ||
7079 | s = p->parent; | |
7080 | p->parent = t; | |
7081 | r->parent = t; | |
7082 | t->parent = s; | |
7083 | if (s) | |
7084 | { | |
7085 | if (s->left == p) | |
7086 | s->left = t; | |
7087 | else | |
7088 | s->right = t; | |
7089 | } | |
7090 | else | |
7091 | constructor_pending_elts = t; | |
7092 | } | |
7093 | break; | |
7094 | } | |
7095 | else | |
7096 | { | |
7097 | /* p->balance == +1; growth of left side balances the node. */ | |
7098 | p->balance = 0; | |
7099 | break; | |
7100 | } | |
2bede729 | 7101 | } |
3e4093b6 RS |
7102 | else /* r == p->right */ |
7103 | { | |
7104 | if (p->balance == 0) | |
7105 | /* Growth propagation from right side. */ | |
7106 | p->balance++; | |
7107 | else if (p->balance > 0) | |
7108 | { | |
7109 | if (r->balance > 0) | |
7110 | { | |
7111 | /* R rotation. */ | |
7112 | p->right = r->left; | |
7113 | if (p->right) | |
7114 | p->right->parent = p; | |
7115 | r->left = p; | |
7116 | ||
7117 | p->balance = 0; | |
7118 | r->balance = 0; | |
7119 | ||
7120 | s = p->parent; | |
7121 | p->parent = r; | |
7122 | r->parent = s; | |
7123 | if (s) | |
7124 | { | |
7125 | if (s->left == p) | |
7126 | s->left = r; | |
7127 | else | |
7128 | s->right = r; | |
7129 | } | |
7130 | else | |
7131 | constructor_pending_elts = r; | |
7132 | } | |
7133 | else /* r->balance == -1 */ | |
7134 | { | |
7135 | /* RL rotation */ | |
7136 | struct init_node *t = r->left; | |
7137 | ||
7138 | r->left = t->right; | |
7139 | if (r->left) | |
7140 | r->left->parent = r; | |
7141 | t->right = r; | |
7142 | ||
7143 | p->right = t->left; | |
7144 | if (p->right) | |
7145 | p->right->parent = p; | |
7146 | t->left = p; | |
7147 | ||
7148 | r->balance = (t->balance < 0); | |
7149 | p->balance = -(t->balance > 0); | |
7150 | t->balance = 0; | |
7151 | ||
7152 | s = p->parent; | |
7153 | p->parent = t; | |
7154 | r->parent = t; | |
7155 | t->parent = s; | |
7156 | if (s) | |
7157 | { | |
7158 | if (s->left == p) | |
7159 | s->left = t; | |
7160 | else | |
7161 | s->right = t; | |
7162 | } | |
7163 | else | |
7164 | constructor_pending_elts = t; | |
7165 | } | |
7166 | break; | |
7167 | } | |
7168 | else | |
7169 | { | |
7170 | /* p->balance == -1; growth of right side balances the node. */ | |
7171 | p->balance = 0; | |
7172 | break; | |
7173 | } | |
7174 | } | |
7175 | ||
7176 | r = p; | |
7177 | p = p->parent; | |
7178 | } | |
7179 | } | |
7180 | ||
7181 | /* Build AVL tree from a sorted chain. */ | |
7182 | ||
7183 | static void | |
7184 | set_nonincremental_init (void) | |
7185 | { | |
4038c495 GB |
7186 | unsigned HOST_WIDE_INT ix; |
7187 | tree index, value; | |
3e4093b6 RS |
7188 | |
7189 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7190 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7191 | return; | |
7192 | ||
4038c495 | 7193 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) |
bbbbb16a | 7194 | add_pending_init (index, value, NULL_TREE, false); |
3e4093b6 RS |
7195 | constructor_elements = 0; |
7196 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
7197 | { | |
7198 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
7199 | /* Skip any nameless bit fields at the beginning. */ | |
7200 | while (constructor_unfilled_fields != 0 | |
7201 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7202 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7203 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
fed3cef0 | 7204 | |
de520661 | 7205 | } |
3e4093b6 | 7206 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 7207 | { |
3e4093b6 RS |
7208 | if (TYPE_DOMAIN (constructor_type)) |
7209 | constructor_unfilled_index | |
7210 | = convert (bitsizetype, | |
7211 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7212 | else | |
7213 | constructor_unfilled_index = bitsize_zero_node; | |
de520661 | 7214 | } |
3e4093b6 | 7215 | constructor_incremental = 0; |
de520661 | 7216 | } |
400fbf9f | 7217 | |
3e4093b6 | 7218 | /* Build AVL tree from a string constant. */ |
de520661 | 7219 | |
3e4093b6 RS |
7220 | static void |
7221 | set_nonincremental_init_from_string (tree str) | |
de520661 | 7222 | { |
3e4093b6 RS |
7223 | tree value, purpose, type; |
7224 | HOST_WIDE_INT val[2]; | |
7225 | const char *p, *end; | |
7226 | int byte, wchar_bytes, charwidth, bitpos; | |
de520661 | 7227 | |
366de0ce | 7228 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); |
940ff66d | 7229 | |
c466b2cd | 7230 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; |
3e4093b6 RS |
7231 | charwidth = TYPE_PRECISION (char_type_node); |
7232 | type = TREE_TYPE (constructor_type); | |
7233 | p = TREE_STRING_POINTER (str); | |
7234 | end = p + TREE_STRING_LENGTH (str); | |
91fa3c30 | 7235 | |
3e4093b6 RS |
7236 | for (purpose = bitsize_zero_node; |
7237 | p < end && !tree_int_cst_lt (constructor_max_index, purpose); | |
7238 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) | |
584ef5fe | 7239 | { |
3e4093b6 | 7240 | if (wchar_bytes == 1) |
ffc5c6a9 | 7241 | { |
3e4093b6 RS |
7242 | val[1] = (unsigned char) *p++; |
7243 | val[0] = 0; | |
ffc5c6a9 RH |
7244 | } |
7245 | else | |
3e4093b6 RS |
7246 | { |
7247 | val[0] = 0; | |
7248 | val[1] = 0; | |
7249 | for (byte = 0; byte < wchar_bytes; byte++) | |
7250 | { | |
7251 | if (BYTES_BIG_ENDIAN) | |
7252 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
7253 | else | |
7254 | bitpos = byte * charwidth; | |
7255 | val[bitpos < HOST_BITS_PER_WIDE_INT] | |
7256 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) | |
7257 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
7258 | } | |
7259 | } | |
584ef5fe | 7260 | |
8df83eae | 7261 | if (!TYPE_UNSIGNED (type)) |
3e4093b6 RS |
7262 | { |
7263 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
7264 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
7265 | { | |
7266 | if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) | |
7267 | { | |
7268 | val[1] |= ((HOST_WIDE_INT) -1) << bitpos; | |
7269 | val[0] = -1; | |
7270 | } | |
7271 | } | |
7272 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
7273 | { | |
7274 | if (val[1] < 0) | |
c22cacf3 | 7275 | val[0] = -1; |
3e4093b6 RS |
7276 | } |
7277 | else if (val[0] & (((HOST_WIDE_INT) 1) | |
7278 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) | |
7279 | val[0] |= ((HOST_WIDE_INT) -1) | |
7280 | << (bitpos - HOST_BITS_PER_WIDE_INT); | |
7281 | } | |
ffc5c6a9 | 7282 | |
7d60be94 | 7283 | value = build_int_cst_wide (type, val[1], val[0]); |
bbbbb16a | 7284 | add_pending_init (purpose, value, NULL_TREE, false); |
9dfcc8db BH |
7285 | } |
7286 | ||
3e4093b6 RS |
7287 | constructor_incremental = 0; |
7288 | } | |
de520661 | 7289 | |
3e4093b6 RS |
7290 | /* Return value of FIELD in pending initializer or zero if the field was |
7291 | not initialized yet. */ | |
7292 | ||
7293 | static tree | |
7294 | find_init_member (tree field) | |
7295 | { | |
7296 | struct init_node *p; | |
7297 | ||
7298 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
19d76e60 | 7299 | { |
3e4093b6 RS |
7300 | if (constructor_incremental |
7301 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
7302 | set_nonincremental_init (); | |
7303 | ||
7304 | p = constructor_pending_elts; | |
7305 | while (p) | |
19d76e60 | 7306 | { |
3e4093b6 RS |
7307 | if (tree_int_cst_lt (field, p->purpose)) |
7308 | p = p->left; | |
7309 | else if (tree_int_cst_lt (p->purpose, field)) | |
7310 | p = p->right; | |
7311 | else | |
7312 | return p->value; | |
19d76e60 | 7313 | } |
19d76e60 | 7314 | } |
3e4093b6 | 7315 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
de520661 | 7316 | { |
3e4093b6 | 7317 | tree bitpos = bit_position (field); |
de520661 | 7318 | |
3e4093b6 RS |
7319 | if (constructor_incremental |
7320 | && (!constructor_unfilled_fields | |
7321 | || tree_int_cst_lt (bitpos, | |
7322 | bit_position (constructor_unfilled_fields)))) | |
7323 | set_nonincremental_init (); | |
de520661 | 7324 | |
3e4093b6 RS |
7325 | p = constructor_pending_elts; |
7326 | while (p) | |
7327 | { | |
7328 | if (field == p->purpose) | |
7329 | return p->value; | |
7330 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7331 | p = p->left; | |
7332 | else | |
7333 | p = p->right; | |
7334 | } | |
7335 | } | |
7336 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 7337 | { |
4038c495 GB |
7338 | if (!VEC_empty (constructor_elt, constructor_elements) |
7339 | && (VEC_last (constructor_elt, constructor_elements)->index | |
7340 | == field)) | |
7341 | return VEC_last (constructor_elt, constructor_elements)->value; | |
de520661 | 7342 | } |
3e4093b6 | 7343 | return 0; |
de520661 RS |
7344 | } |
7345 | ||
3e4093b6 RS |
7346 | /* "Output" the next constructor element. |
7347 | At top level, really output it to assembler code now. | |
7348 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
bbbbb16a | 7349 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. |
3e4093b6 RS |
7350 | TYPE is the data type that the containing data type wants here. |
7351 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
916c5919 JM |
7352 | If VALUE is a string constant, STRICT_STRING is true if it is |
7353 | unparenthesized or we should not warn here for it being parenthesized. | |
7354 | For other types of VALUE, STRICT_STRING is not used. | |
8b6a5902 | 7355 | |
3e4093b6 RS |
7356 | PENDING if non-nil means output pending elements that belong |
7357 | right after this element. (PENDING is normally 1; | |
b295aee2 JJ |
7358 | it is 0 while outputting pending elements, to avoid recursion.) |
7359 | ||
7360 | IMPLICIT is true if value comes from pop_init_level (1), | |
7361 | the new initializer has been merged with the existing one | |
7362 | and thus no warnings should be emitted about overriding an | |
7363 | existing initializer. */ | |
8b6a5902 | 7364 | |
3e4093b6 | 7365 | static void |
bbbbb16a ILT |
7366 | output_init_element (tree value, tree origtype, bool strict_string, tree type, |
7367 | tree field, int pending, bool implicit) | |
3e4093b6 | 7368 | { |
8ce94e44 | 7369 | tree semantic_type = NULL_TREE; |
4038c495 | 7370 | constructor_elt *celt; |
928c19bb JM |
7371 | bool maybe_const = true; |
7372 | bool npc; | |
4038c495 | 7373 | |
0a880880 | 7374 | if (type == error_mark_node || value == error_mark_node) |
8b6a5902 | 7375 | { |
3e4093b6 RS |
7376 | constructor_erroneous = 1; |
7377 | return; | |
8b6a5902 | 7378 | } |
46bdb9cf JM |
7379 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
7380 | && (TREE_CODE (value) == STRING_CST | |
7381 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
7382 | && !(TREE_CODE (value) == STRING_CST | |
7383 | && TREE_CODE (type) == ARRAY_TYPE | |
7384 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
7385 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
7386 | TYPE_MAIN_VARIANT (type))) | |
c2255bc4 | 7387 | value = array_to_pointer_conversion (input_location, value); |
8b6a5902 | 7388 | |
3e4093b6 RS |
7389 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
7390 | && require_constant_value && !flag_isoc99 && pending) | |
8b6a5902 | 7391 | { |
3e4093b6 RS |
7392 | /* As an extension, allow initializing objects with static storage |
7393 | duration with compound literals (which are then treated just as | |
7394 | the brace enclosed list they contain). */ | |
7395 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); | |
7396 | value = DECL_INITIAL (decl); | |
8b6a5902 JJ |
7397 | } |
7398 | ||
928c19bb | 7399 | npc = null_pointer_constant_p (value); |
8ce94e44 JM |
7400 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) |
7401 | { | |
7402 | semantic_type = TREE_TYPE (value); | |
7403 | value = TREE_OPERAND (value, 0); | |
7404 | } | |
928c19bb JM |
7405 | value = c_fully_fold (value, require_constant_value, &maybe_const); |
7406 | ||
3e4093b6 RS |
7407 | if (value == error_mark_node) |
7408 | constructor_erroneous = 1; | |
7409 | else if (!TREE_CONSTANT (value)) | |
7410 | constructor_constant = 0; | |
116df786 | 7411 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) |
3e4093b6 RS |
7412 | || ((TREE_CODE (constructor_type) == RECORD_TYPE |
7413 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7414 | && DECL_C_BIT_FIELD (field) | |
7415 | && TREE_CODE (value) != INTEGER_CST)) | |
7416 | constructor_simple = 0; | |
928c19bb JM |
7417 | if (!maybe_const) |
7418 | constructor_nonconst = 1; | |
3e4093b6 | 7419 | |
116df786 | 7420 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) |
8b6a5902 | 7421 | { |
116df786 RH |
7422 | if (require_constant_value) |
7423 | { | |
7424 | error_init ("initializer element is not constant"); | |
7425 | value = error_mark_node; | |
7426 | } | |
7427 | else if (require_constant_elements) | |
509c9d60 MLI |
7428 | pedwarn (input_location, 0, |
7429 | "initializer element is not computable at load time"); | |
8b6a5902 | 7430 | } |
928c19bb JM |
7431 | else if (!maybe_const |
7432 | && (require_constant_value || require_constant_elements)) | |
7433 | pedwarn_init (input_location, 0, | |
7434 | "initializer element is not a constant expression"); | |
3e4093b6 | 7435 | |
81f40b79 ILT |
7436 | /* Issue -Wc++-compat warnings about initializing a bitfield with |
7437 | enum type. */ | |
7438 | if (warn_cxx_compat | |
7439 | && field != NULL_TREE | |
7440 | && TREE_CODE (field) == FIELD_DECL | |
7441 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
7442 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
7443 | != TYPE_MAIN_VARIANT (type)) | |
7444 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
7445 | { | |
7446 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
7447 | if (checktype != error_mark_node | |
7448 | && (TYPE_MAIN_VARIANT (checktype) | |
7449 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
7450 | warning_init (OPT_Wc___compat, | |
7451 | "enum conversion in initialization is invalid in C++"); | |
7452 | } | |
7453 | ||
3e4093b6 RS |
7454 | /* If this field is empty (and not at the end of structure), |
7455 | don't do anything other than checking the initializer. */ | |
7456 | if (field | |
7457 | && (TREE_TYPE (field) == error_mark_node | |
7458 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
7459 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
7460 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
7461 | || TREE_CHAIN (field))))) | |
7462 | return; | |
7463 | ||
8ce94e44 JM |
7464 | if (semantic_type) |
7465 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
c2255bc4 AH |
7466 | value = digest_init (input_location, type, value, origtype, npc, |
7467 | strict_string, require_constant_value); | |
3e4093b6 | 7468 | if (value == error_mark_node) |
8b6a5902 | 7469 | { |
3e4093b6 RS |
7470 | constructor_erroneous = 1; |
7471 | return; | |
8b6a5902 | 7472 | } |
928c19bb JM |
7473 | if (require_constant_value || require_constant_elements) |
7474 | constant_expression_warning (value); | |
8b6a5902 | 7475 | |
3e4093b6 RS |
7476 | /* If this element doesn't come next in sequence, |
7477 | put it on constructor_pending_elts. */ | |
7478 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
7479 | && (!constructor_incremental | |
7480 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
8b6a5902 | 7481 | { |
3e4093b6 RS |
7482 | if (constructor_incremental |
7483 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
7484 | set_nonincremental_init (); | |
7485 | ||
bbbbb16a | 7486 | add_pending_init (field, value, origtype, implicit); |
3e4093b6 | 7487 | return; |
8b6a5902 | 7488 | } |
3e4093b6 RS |
7489 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
7490 | && (!constructor_incremental | |
7491 | || field != constructor_unfilled_fields)) | |
8b6a5902 | 7492 | { |
3e4093b6 RS |
7493 | /* We do this for records but not for unions. In a union, |
7494 | no matter which field is specified, it can be initialized | |
7495 | right away since it starts at the beginning of the union. */ | |
7496 | if (constructor_incremental) | |
7497 | { | |
7498 | if (!constructor_unfilled_fields) | |
7499 | set_nonincremental_init (); | |
7500 | else | |
7501 | { | |
7502 | tree bitpos, unfillpos; | |
7503 | ||
7504 | bitpos = bit_position (field); | |
7505 | unfillpos = bit_position (constructor_unfilled_fields); | |
7506 | ||
7507 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
7508 | set_nonincremental_init (); | |
7509 | } | |
7510 | } | |
7511 | ||
bbbbb16a | 7512 | add_pending_init (field, value, origtype, implicit); |
3e4093b6 | 7513 | return; |
8b6a5902 | 7514 | } |
3e4093b6 | 7515 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
4038c495 | 7516 | && !VEC_empty (constructor_elt, constructor_elements)) |
3e4093b6 | 7517 | { |
b295aee2 JJ |
7518 | if (!implicit) |
7519 | { | |
7520 | if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt, | |
7521 | constructor_elements)->value)) | |
7522 | warning_init (0, | |
7523 | "initialized field with side-effects overwritten"); | |
7524 | else if (warn_override_init) | |
7525 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7526 | } | |
8b6a5902 | 7527 | |
3e4093b6 RS |
7528 | /* We can have just one union field set. */ |
7529 | constructor_elements = 0; | |
7530 | } | |
8b6a5902 | 7531 | |
3e4093b6 RS |
7532 | /* Otherwise, output this element either to |
7533 | constructor_elements or to the assembler file. */ | |
8b6a5902 | 7534 | |
4038c495 GB |
7535 | celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL); |
7536 | celt->index = field; | |
7537 | celt->value = value; | |
8b6a5902 | 7538 | |
3e4093b6 RS |
7539 | /* Advance the variable that indicates sequential elements output. */ |
7540 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7541 | constructor_unfilled_index | |
db3927fb AH |
7542 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, |
7543 | bitsize_one_node); | |
3e4093b6 RS |
7544 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
7545 | { | |
7546 | constructor_unfilled_fields | |
7547 | = TREE_CHAIN (constructor_unfilled_fields); | |
8b6a5902 | 7548 | |
3e4093b6 RS |
7549 | /* Skip any nameless bit fields. */ |
7550 | while (constructor_unfilled_fields != 0 | |
7551 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7552 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7553 | constructor_unfilled_fields = | |
7554 | TREE_CHAIN (constructor_unfilled_fields); | |
7555 | } | |
7556 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
7557 | constructor_unfilled_fields = 0; | |
de520661 | 7558 | |
3e4093b6 RS |
7559 | /* Now output any pending elements which have become next. */ |
7560 | if (pending) | |
7561 | output_pending_init_elements (0); | |
7562 | } | |
8b6a5902 | 7563 | |
3e4093b6 RS |
7564 | /* Output any pending elements which have become next. |
7565 | As we output elements, constructor_unfilled_{fields,index} | |
7566 | advances, which may cause other elements to become next; | |
7567 | if so, they too are output. | |
8b6a5902 | 7568 | |
3e4093b6 RS |
7569 | If ALL is 0, we return when there are |
7570 | no more pending elements to output now. | |
665f2503 | 7571 | |
3e4093b6 RS |
7572 | If ALL is 1, we output space as necessary so that |
7573 | we can output all the pending elements. */ | |
19d76e60 | 7574 | |
3e4093b6 RS |
7575 | static void |
7576 | output_pending_init_elements (int all) | |
7577 | { | |
7578 | struct init_node *elt = constructor_pending_elts; | |
7579 | tree next; | |
de520661 | 7580 | |
3e4093b6 RS |
7581 | retry: |
7582 | ||
ba228239 | 7583 | /* Look through the whole pending tree. |
3e4093b6 RS |
7584 | If we find an element that should be output now, |
7585 | output it. Otherwise, set NEXT to the element | |
7586 | that comes first among those still pending. */ | |
7587 | ||
7588 | next = 0; | |
7589 | while (elt) | |
7590 | { | |
7591 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8b6a5902 | 7592 | { |
3e4093b6 RS |
7593 | if (tree_int_cst_equal (elt->purpose, |
7594 | constructor_unfilled_index)) | |
bbbbb16a | 7595 | output_init_element (elt->value, elt->origtype, true, |
3e4093b6 | 7596 | TREE_TYPE (constructor_type), |
b295aee2 | 7597 | constructor_unfilled_index, 0, false); |
3e4093b6 RS |
7598 | else if (tree_int_cst_lt (constructor_unfilled_index, |
7599 | elt->purpose)) | |
8b6a5902 | 7600 | { |
3e4093b6 RS |
7601 | /* Advance to the next smaller node. */ |
7602 | if (elt->left) | |
7603 | elt = elt->left; | |
7604 | else | |
7605 | { | |
7606 | /* We have reached the smallest node bigger than the | |
7607 | current unfilled index. Fill the space first. */ | |
7608 | next = elt->purpose; | |
7609 | break; | |
7610 | } | |
8b6a5902 | 7611 | } |
ce662d4c JJ |
7612 | else |
7613 | { | |
3e4093b6 RS |
7614 | /* Advance to the next bigger node. */ |
7615 | if (elt->right) | |
7616 | elt = elt->right; | |
7617 | else | |
ce662d4c | 7618 | { |
3e4093b6 RS |
7619 | /* We have reached the biggest node in a subtree. Find |
7620 | the parent of it, which is the next bigger node. */ | |
7621 | while (elt->parent && elt->parent->right == elt) | |
7622 | elt = elt->parent; | |
7623 | elt = elt->parent; | |
7624 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
7625 | elt->purpose)) | |
7626 | { | |
7627 | next = elt->purpose; | |
7628 | break; | |
7629 | } | |
ce662d4c JJ |
7630 | } |
7631 | } | |
8b6a5902 | 7632 | } |
3e4093b6 RS |
7633 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
7634 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7635 | { | |
7636 | tree ctor_unfilled_bitpos, elt_bitpos; | |
ce662d4c | 7637 | |
3e4093b6 RS |
7638 | /* If the current record is complete we are done. */ |
7639 | if (constructor_unfilled_fields == 0) | |
7640 | break; | |
de520661 | 7641 | |
3e4093b6 RS |
7642 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
7643 | elt_bitpos = bit_position (elt->purpose); | |
7644 | /* We can't compare fields here because there might be empty | |
7645 | fields in between. */ | |
7646 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
7647 | { | |
7648 | constructor_unfilled_fields = elt->purpose; | |
bbbbb16a ILT |
7649 | output_init_element (elt->value, elt->origtype, true, |
7650 | TREE_TYPE (elt->purpose), | |
b295aee2 | 7651 | elt->purpose, 0, false); |
3e4093b6 RS |
7652 | } |
7653 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
7654 | { | |
7655 | /* Advance to the next smaller node. */ | |
7656 | if (elt->left) | |
7657 | elt = elt->left; | |
7658 | else | |
7659 | { | |
7660 | /* We have reached the smallest node bigger than the | |
7661 | current unfilled field. Fill the space first. */ | |
7662 | next = elt->purpose; | |
7663 | break; | |
7664 | } | |
7665 | } | |
7666 | else | |
7667 | { | |
7668 | /* Advance to the next bigger node. */ | |
7669 | if (elt->right) | |
7670 | elt = elt->right; | |
7671 | else | |
7672 | { | |
7673 | /* We have reached the biggest node in a subtree. Find | |
7674 | the parent of it, which is the next bigger node. */ | |
7675 | while (elt->parent && elt->parent->right == elt) | |
7676 | elt = elt->parent; | |
7677 | elt = elt->parent; | |
7678 | if (elt | |
7679 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
7680 | bit_position (elt->purpose)))) | |
7681 | { | |
7682 | next = elt->purpose; | |
7683 | break; | |
7684 | } | |
7685 | } | |
7686 | } | |
7687 | } | |
7688 | } | |
de520661 | 7689 | |
3e4093b6 RS |
7690 | /* Ordinarily return, but not if we want to output all |
7691 | and there are elements left. */ | |
3f75a254 | 7692 | if (!(all && next != 0)) |
e5cfb88f RK |
7693 | return; |
7694 | ||
3e4093b6 RS |
7695 | /* If it's not incremental, just skip over the gap, so that after |
7696 | jumping to retry we will output the next successive element. */ | |
7697 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
7698 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7699 | constructor_unfilled_fields = next; | |
7700 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7701 | constructor_unfilled_index = next; | |
de520661 | 7702 | |
3e4093b6 RS |
7703 | /* ELT now points to the node in the pending tree with the next |
7704 | initializer to output. */ | |
7705 | goto retry; | |
de520661 RS |
7706 | } |
7707 | \f | |
3e4093b6 RS |
7708 | /* Add one non-braced element to the current constructor level. |
7709 | This adjusts the current position within the constructor's type. | |
7710 | This may also start or terminate implicit levels | |
7711 | to handle a partly-braced initializer. | |
e5e809f4 | 7712 | |
3e4093b6 | 7713 | Once this has found the correct level for the new element, |
b295aee2 JJ |
7714 | it calls output_init_element. |
7715 | ||
7716 | IMPLICIT is true if value comes from pop_init_level (1), | |
7717 | the new initializer has been merged with the existing one | |
7718 | and thus no warnings should be emitted about overriding an | |
7719 | existing initializer. */ | |
3e4093b6 RS |
7720 | |
7721 | void | |
b295aee2 | 7722 | process_init_element (struct c_expr value, bool implicit) |
e5e809f4 | 7723 | { |
916c5919 JM |
7724 | tree orig_value = value.value; |
7725 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
7726 | bool strict_string = value.original_code == STRING_CST; | |
e5e809f4 | 7727 | |
3e4093b6 | 7728 | designator_depth = 0; |
b06df647 | 7729 | designator_erroneous = 0; |
e5e809f4 | 7730 | |
3e4093b6 RS |
7731 | /* Handle superfluous braces around string cst as in |
7732 | char x[] = {"foo"}; */ | |
7733 | if (string_flag | |
7734 | && constructor_type | |
7735 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
197463ae | 7736 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
3e4093b6 | 7737 | && integer_zerop (constructor_unfilled_index)) |
e5e809f4 | 7738 | { |
916c5919 | 7739 | if (constructor_stack->replacement_value.value) |
c22cacf3 | 7740 | error_init ("excess elements in char array initializer"); |
3e4093b6 RS |
7741 | constructor_stack->replacement_value = value; |
7742 | return; | |
e5e809f4 | 7743 | } |
8b6a5902 | 7744 | |
916c5919 | 7745 | if (constructor_stack->replacement_value.value != 0) |
3e4093b6 RS |
7746 | { |
7747 | error_init ("excess elements in struct initializer"); | |
7748 | return; | |
e5e809f4 JL |
7749 | } |
7750 | ||
3e4093b6 RS |
7751 | /* Ignore elements of a brace group if it is entirely superfluous |
7752 | and has already been diagnosed. */ | |
7753 | if (constructor_type == 0) | |
7754 | return; | |
e5e809f4 | 7755 | |
3e4093b6 RS |
7756 | /* If we've exhausted any levels that didn't have braces, |
7757 | pop them now. */ | |
7758 | while (constructor_stack->implicit) | |
7759 | { | |
7760 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7761 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7762 | && constructor_fields == 0) | |
b295aee2 | 7763 | process_init_element (pop_init_level (1), true); |
53650abe AP |
7764 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE |
7765 | || TREE_CODE (constructor_type) == VECTOR_TYPE) | |
3e4093b6 RS |
7766 | && (constructor_max_index == 0 |
7767 | || tree_int_cst_lt (constructor_max_index, | |
7768 | constructor_index))) | |
b295aee2 | 7769 | process_init_element (pop_init_level (1), true); |
3e4093b6 RS |
7770 | else |
7771 | break; | |
7772 | } | |
e5e809f4 | 7773 | |
3e4093b6 RS |
7774 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
7775 | if (constructor_range_stack) | |
e5e809f4 | 7776 | { |
3e4093b6 RS |
7777 | /* If value is a compound literal and we'll be just using its |
7778 | content, don't put it into a SAVE_EXPR. */ | |
916c5919 | 7779 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
3e4093b6 RS |
7780 | || !require_constant_value |
7781 | || flag_isoc99) | |
8ce94e44 JM |
7782 | { |
7783 | tree semantic_type = NULL_TREE; | |
7784 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
7785 | { | |
7786 | semantic_type = TREE_TYPE (value.value); | |
7787 | value.value = TREE_OPERAND (value.value, 0); | |
7788 | } | |
7789 | value.value = c_save_expr (value.value); | |
7790 | if (semantic_type) | |
7791 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
7792 | value.value); | |
7793 | } | |
3e4093b6 | 7794 | } |
e5e809f4 | 7795 | |
3e4093b6 RS |
7796 | while (1) |
7797 | { | |
7798 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
e5e809f4 | 7799 | { |
3e4093b6 RS |
7800 | tree fieldtype; |
7801 | enum tree_code fieldcode; | |
e5e809f4 | 7802 | |
3e4093b6 RS |
7803 | if (constructor_fields == 0) |
7804 | { | |
509c9d60 MLI |
7805 | pedwarn_init (input_location, 0, |
7806 | "excess elements in struct initializer"); | |
3e4093b6 RS |
7807 | break; |
7808 | } | |
e5e809f4 | 7809 | |
3e4093b6 RS |
7810 | fieldtype = TREE_TYPE (constructor_fields); |
7811 | if (fieldtype != error_mark_node) | |
7812 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
7813 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 7814 | |
3e4093b6 RS |
7815 | /* Error for non-static initialization of a flexible array member. */ |
7816 | if (fieldcode == ARRAY_TYPE | |
7817 | && !require_constant_value | |
7818 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
7819 | && TREE_CHAIN (constructor_fields) == NULL_TREE) | |
7820 | { | |
7821 | error_init ("non-static initialization of a flexible array member"); | |
7822 | break; | |
7823 | } | |
e5e809f4 | 7824 | |
3e4093b6 | 7825 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 7826 | if (value.value != 0 |
3e4093b6 | 7827 | && fieldcode == ARRAY_TYPE |
197463ae | 7828 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 7829 | && string_flag) |
916c5919 | 7830 | value.value = orig_value; |
3e4093b6 RS |
7831 | /* Otherwise, if we have come to a subaggregate, |
7832 | and we don't have an element of its type, push into it. */ | |
0953878d | 7833 | else if (value.value != 0 |
916c5919 JM |
7834 | && value.value != error_mark_node |
7835 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 7836 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 7837 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 RS |
7838 | { |
7839 | push_init_level (1); | |
7840 | continue; | |
7841 | } | |
e5e809f4 | 7842 | |
916c5919 | 7843 | if (value.value) |
3e4093b6 RS |
7844 | { |
7845 | push_member_name (constructor_fields); | |
bbbbb16a ILT |
7846 | output_init_element (value.value, value.original_type, |
7847 | strict_string, fieldtype, | |
7848 | constructor_fields, 1, implicit); | |
3e4093b6 | 7849 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
7850 | } |
7851 | else | |
3e4093b6 RS |
7852 | /* Do the bookkeeping for an element that was |
7853 | directly output as a constructor. */ | |
e5e809f4 | 7854 | { |
3e4093b6 RS |
7855 | /* For a record, keep track of end position of last field. */ |
7856 | if (DECL_SIZE (constructor_fields)) | |
c22cacf3 | 7857 | constructor_bit_index |
db3927fb AH |
7858 | = size_binop_loc (input_location, PLUS_EXPR, |
7859 | bit_position (constructor_fields), | |
7860 | DECL_SIZE (constructor_fields)); | |
3e4093b6 RS |
7861 | |
7862 | /* If the current field was the first one not yet written out, | |
7863 | it isn't now, so update. */ | |
7864 | if (constructor_unfilled_fields == constructor_fields) | |
7865 | { | |
7866 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
7867 | /* Skip any nameless bit fields. */ | |
7868 | while (constructor_unfilled_fields != 0 | |
7869 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7870 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7871 | constructor_unfilled_fields = | |
7872 | TREE_CHAIN (constructor_unfilled_fields); | |
7873 | } | |
e5e809f4 | 7874 | } |
3e4093b6 RS |
7875 | |
7876 | constructor_fields = TREE_CHAIN (constructor_fields); | |
7877 | /* Skip any nameless bit fields at the beginning. */ | |
7878 | while (constructor_fields != 0 | |
7879 | && DECL_C_BIT_FIELD (constructor_fields) | |
7880 | && DECL_NAME (constructor_fields) == 0) | |
7881 | constructor_fields = TREE_CHAIN (constructor_fields); | |
e5e809f4 | 7882 | } |
3e4093b6 | 7883 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
e5e809f4 | 7884 | { |
3e4093b6 RS |
7885 | tree fieldtype; |
7886 | enum tree_code fieldcode; | |
e5e809f4 | 7887 | |
3e4093b6 RS |
7888 | if (constructor_fields == 0) |
7889 | { | |
509c9d60 MLI |
7890 | pedwarn_init (input_location, 0, |
7891 | "excess elements in union initializer"); | |
3e4093b6 RS |
7892 | break; |
7893 | } | |
e5e809f4 | 7894 | |
3e4093b6 RS |
7895 | fieldtype = TREE_TYPE (constructor_fields); |
7896 | if (fieldtype != error_mark_node) | |
7897 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
7898 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 7899 | |
3e4093b6 RS |
7900 | /* Warn that traditional C rejects initialization of unions. |
7901 | We skip the warning if the value is zero. This is done | |
7902 | under the assumption that the zero initializer in user | |
7903 | code appears conditioned on e.g. __STDC__ to avoid | |
7904 | "missing initializer" warnings and relies on default | |
7905 | initialization to zero in the traditional C case. | |
7906 | We also skip the warning if the initializer is designated, | |
7907 | again on the assumption that this must be conditional on | |
7908 | __STDC__ anyway (and we've already complained about the | |
7909 | member-designator already). */ | |
3176a0c2 | 7910 | if (!in_system_header && !constructor_designated |
916c5919 JM |
7911 | && !(value.value && (integer_zerop (value.value) |
7912 | || real_zerop (value.value)))) | |
3176a0c2 DD |
7913 | warning (OPT_Wtraditional, "traditional C rejects initialization " |
7914 | "of unions"); | |
e5e809f4 | 7915 | |
3e4093b6 | 7916 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 7917 | if (value.value != 0 |
3e4093b6 | 7918 | && fieldcode == ARRAY_TYPE |
197463ae | 7919 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 7920 | && string_flag) |
916c5919 | 7921 | value.value = orig_value; |
3e4093b6 RS |
7922 | /* Otherwise, if we have come to a subaggregate, |
7923 | and we don't have an element of its type, push into it. */ | |
0953878d | 7924 | else if (value.value != 0 |
916c5919 JM |
7925 | && value.value != error_mark_node |
7926 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 7927 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 7928 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 RS |
7929 | { |
7930 | push_init_level (1); | |
7931 | continue; | |
7932 | } | |
e5e809f4 | 7933 | |
916c5919 | 7934 | if (value.value) |
3e4093b6 RS |
7935 | { |
7936 | push_member_name (constructor_fields); | |
bbbbb16a ILT |
7937 | output_init_element (value.value, value.original_type, |
7938 | strict_string, fieldtype, | |
7939 | constructor_fields, 1, implicit); | |
3e4093b6 | 7940 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
7941 | } |
7942 | else | |
3e4093b6 RS |
7943 | /* Do the bookkeeping for an element that was |
7944 | directly output as a constructor. */ | |
e5e809f4 | 7945 | { |
3e4093b6 RS |
7946 | constructor_bit_index = DECL_SIZE (constructor_fields); |
7947 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
e5e809f4 | 7948 | } |
e5e809f4 | 7949 | |
3e4093b6 RS |
7950 | constructor_fields = 0; |
7951 | } | |
7952 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7953 | { | |
7954 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
7955 | enum tree_code eltcode = TREE_CODE (elttype); | |
e5e809f4 | 7956 | |
3e4093b6 | 7957 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 7958 | if (value.value != 0 |
3e4093b6 | 7959 | && eltcode == ARRAY_TYPE |
197463ae | 7960 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
3e4093b6 | 7961 | && string_flag) |
916c5919 | 7962 | value.value = orig_value; |
3e4093b6 RS |
7963 | /* Otherwise, if we have come to a subaggregate, |
7964 | and we don't have an element of its type, push into it. */ | |
0953878d | 7965 | else if (value.value != 0 |
916c5919 JM |
7966 | && value.value != error_mark_node |
7967 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
3e4093b6 | 7968 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
53650abe | 7969 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) |
3e4093b6 RS |
7970 | { |
7971 | push_init_level (1); | |
7972 | continue; | |
7973 | } | |
8b6a5902 | 7974 | |
3e4093b6 RS |
7975 | if (constructor_max_index != 0 |
7976 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
7977 | || integer_all_onesp (constructor_max_index))) | |
7978 | { | |
509c9d60 MLI |
7979 | pedwarn_init (input_location, 0, |
7980 | "excess elements in array initializer"); | |
3e4093b6 RS |
7981 | break; |
7982 | } | |
8b6a5902 | 7983 | |
3e4093b6 | 7984 | /* Now output the actual element. */ |
916c5919 | 7985 | if (value.value) |
3e4093b6 | 7986 | { |
a0f0ab9f | 7987 | push_array_bounds (tree_low_cst (constructor_index, 1)); |
bbbbb16a ILT |
7988 | output_init_element (value.value, value.original_type, |
7989 | strict_string, elttype, | |
7990 | constructor_index, 1, implicit); | |
3e4093b6 RS |
7991 | RESTORE_SPELLING_DEPTH (constructor_depth); |
7992 | } | |
2f6e4e97 | 7993 | |
3e4093b6 | 7994 | constructor_index |
db3927fb AH |
7995 | = size_binop_loc (input_location, PLUS_EXPR, |
7996 | constructor_index, bitsize_one_node); | |
8b6a5902 | 7997 | |
916c5919 | 7998 | if (!value.value) |
3e4093b6 RS |
7999 | /* If we are doing the bookkeeping for an element that was |
8000 | directly output as a constructor, we must update | |
8001 | constructor_unfilled_index. */ | |
8002 | constructor_unfilled_index = constructor_index; | |
8003 | } | |
8004 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8005 | { | |
8006 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8b6a5902 | 8007 | |
c22cacf3 MS |
8008 | /* Do a basic check of initializer size. Note that vectors |
8009 | always have a fixed size derived from their type. */ | |
3e4093b6 RS |
8010 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) |
8011 | { | |
509c9d60 MLI |
8012 | pedwarn_init (input_location, 0, |
8013 | "excess elements in vector initializer"); | |
3e4093b6 RS |
8014 | break; |
8015 | } | |
8b6a5902 | 8016 | |
3e4093b6 | 8017 | /* Now output the actual element. */ |
916c5919 | 8018 | if (value.value) |
53650abe AP |
8019 | { |
8020 | if (TREE_CODE (value.value) == VECTOR_CST) | |
8021 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
8022 | output_init_element (value.value, value.original_type, | |
8023 | strict_string, elttype, | |
8024 | constructor_index, 1, implicit); | |
8025 | } | |
8b6a5902 | 8026 | |
3e4093b6 | 8027 | constructor_index |
db3927fb AH |
8028 | = size_binop_loc (input_location, |
8029 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 8030 | |
916c5919 | 8031 | if (!value.value) |
3e4093b6 RS |
8032 | /* If we are doing the bookkeeping for an element that was |
8033 | directly output as a constructor, we must update | |
8034 | constructor_unfilled_index. */ | |
8035 | constructor_unfilled_index = constructor_index; | |
8036 | } | |
8b6a5902 | 8037 | |
3e4093b6 RS |
8038 | /* Handle the sole element allowed in a braced initializer |
8039 | for a scalar variable. */ | |
b4519d39 SB |
8040 | else if (constructor_type != error_mark_node |
8041 | && constructor_fields == 0) | |
8b6a5902 | 8042 | { |
509c9d60 MLI |
8043 | pedwarn_init (input_location, 0, |
8044 | "excess elements in scalar initializer"); | |
3e4093b6 | 8045 | break; |
8b6a5902 JJ |
8046 | } |
8047 | else | |
8048 | { | |
916c5919 | 8049 | if (value.value) |
bbbbb16a ILT |
8050 | output_init_element (value.value, value.original_type, |
8051 | strict_string, constructor_type, | |
8052 | NULL_TREE, 1, implicit); | |
3e4093b6 | 8053 | constructor_fields = 0; |
8b6a5902 JJ |
8054 | } |
8055 | ||
3e4093b6 RS |
8056 | /* Handle range initializers either at this level or anywhere higher |
8057 | in the designator stack. */ | |
8058 | if (constructor_range_stack) | |
8b6a5902 | 8059 | { |
3e4093b6 RS |
8060 | struct constructor_range_stack *p, *range_stack; |
8061 | int finish = 0; | |
8062 | ||
8063 | range_stack = constructor_range_stack; | |
8064 | constructor_range_stack = 0; | |
8065 | while (constructor_stack != range_stack->stack) | |
8b6a5902 | 8066 | { |
366de0ce | 8067 | gcc_assert (constructor_stack->implicit); |
b295aee2 | 8068 | process_init_element (pop_init_level (1), true); |
8b6a5902 | 8069 | } |
3e4093b6 RS |
8070 | for (p = range_stack; |
8071 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
8072 | p = p->prev) | |
8b6a5902 | 8073 | { |
366de0ce | 8074 | gcc_assert (constructor_stack->implicit); |
b295aee2 | 8075 | process_init_element (pop_init_level (1), true); |
8b6a5902 | 8076 | } |
3e4093b6 | 8077 | |
db3927fb AH |
8078 | p->index = size_binop_loc (input_location, |
8079 | PLUS_EXPR, p->index, bitsize_one_node); | |
3e4093b6 RS |
8080 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) |
8081 | finish = 1; | |
8082 | ||
8083 | while (1) | |
8084 | { | |
8085 | constructor_index = p->index; | |
8086 | constructor_fields = p->fields; | |
8087 | if (finish && p->range_end && p->index == p->range_start) | |
8088 | { | |
8089 | finish = 0; | |
8090 | p->prev = 0; | |
8091 | } | |
8092 | p = p->next; | |
8093 | if (!p) | |
8094 | break; | |
8095 | push_init_level (2); | |
8096 | p->stack = constructor_stack; | |
8097 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
8098 | p->index = p->range_start; | |
8099 | } | |
8100 | ||
8101 | if (!finish) | |
8102 | constructor_range_stack = range_stack; | |
8103 | continue; | |
8b6a5902 JJ |
8104 | } |
8105 | ||
3e4093b6 | 8106 | break; |
8b6a5902 JJ |
8107 | } |
8108 | ||
3e4093b6 RS |
8109 | constructor_range_stack = 0; |
8110 | } | |
8111 | \f | |
9f0e2d86 ZW |
8112 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
8113 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
e130a54b | 8114 | an ASM_EXPR node). */ |
3e4093b6 | 8115 | tree |
9f0e2d86 | 8116 | build_asm_stmt (tree cv_qualifier, tree args) |
3e4093b6 | 8117 | { |
6de9cd9a DN |
8118 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
8119 | ASM_VOLATILE_P (args) = 1; | |
9f0e2d86 | 8120 | return add_stmt (args); |
8b6a5902 JJ |
8121 | } |
8122 | ||
9f0e2d86 ZW |
8123 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
8124 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
8125 | SIMPLE indicates whether there was anything at all after the | |
8126 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
e130a54b | 8127 | are subtly different. We use a ASM_EXPR node to represent this. */ |
3e4093b6 | 8128 | tree |
c2255bc4 | 8129 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, |
1c384bf1 | 8130 | tree clobbers, tree labels, bool simple) |
e5e809f4 | 8131 | { |
3e4093b6 | 8132 | tree tail; |
9f0e2d86 | 8133 | tree args; |
6de9cd9a DN |
8134 | int i; |
8135 | const char *constraint; | |
74f0c611 | 8136 | const char **oconstraints; |
6de9cd9a | 8137 | bool allows_mem, allows_reg, is_inout; |
74f0c611 | 8138 | int ninputs, noutputs; |
6de9cd9a DN |
8139 | |
8140 | ninputs = list_length (inputs); | |
8141 | noutputs = list_length (outputs); | |
74f0c611 RH |
8142 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); |
8143 | ||
1c384bf1 | 8144 | string = resolve_asm_operand_names (string, outputs, inputs, labels); |
3e4093b6 | 8145 | |
6de9cd9a DN |
8146 | /* Remove output conversions that change the type but not the mode. */ |
8147 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
e5e809f4 | 8148 | { |
3e4093b6 | 8149 | tree output = TREE_VALUE (tail); |
74f0c611 RH |
8150 | |
8151 | /* ??? Really, this should not be here. Users should be using a | |
8152 | proper lvalue, dammit. But there's a long history of using casts | |
8153 | in the output operands. In cases like longlong.h, this becomes a | |
8154 | primitive form of typechecking -- if the cast can be removed, then | |
8155 | the output operand had a type of the proper width; otherwise we'll | |
8156 | get an error. Gross, but ... */ | |
3e4093b6 | 8157 | STRIP_NOPS (output); |
74f0c611 RH |
8158 | |
8159 | if (!lvalue_or_else (output, lv_asm)) | |
8160 | output = error_mark_node; | |
8b6a5902 | 8161 | |
5544530a PB |
8162 | if (output != error_mark_node |
8163 | && (TREE_READONLY (output) | |
8164 | || TYPE_READONLY (TREE_TYPE (output)) | |
8165 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
8166 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
8167 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
8168 | readonly_error (output, lv_asm); | |
8169 | ||
6de9cd9a | 8170 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
74f0c611 RH |
8171 | oconstraints[i] = constraint; |
8172 | ||
8173 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
8174 | &allows_mem, &allows_reg, &is_inout)) | |
8175 | { | |
8176 | /* If the operand is going to end up in memory, | |
8177 | mark it addressable. */ | |
8178 | if (!allows_reg && !c_mark_addressable (output)) | |
8179 | output = error_mark_node; | |
8180 | } | |
8181 | else | |
c22cacf3 | 8182 | output = error_mark_node; |
3e4093b6 | 8183 | |
74f0c611 | 8184 | TREE_VALUE (tail) = output; |
8b6a5902 | 8185 | } |
3e4093b6 | 8186 | |
74f0c611 RH |
8187 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) |
8188 | { | |
8189 | tree input; | |
8190 | ||
8191 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8192 | input = TREE_VALUE (tail); | |
8193 | ||
74f0c611 RH |
8194 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
8195 | oconstraints, &allows_mem, &allows_reg)) | |
8196 | { | |
8197 | /* If the operand is going to end up in memory, | |
8198 | mark it addressable. */ | |
b4c33883 AP |
8199 | if (!allows_reg && allows_mem) |
8200 | { | |
8201 | /* Strip the nops as we allow this case. FIXME, this really | |
8202 | should be rejected or made deprecated. */ | |
8203 | STRIP_NOPS (input); | |
8204 | if (!c_mark_addressable (input)) | |
8205 | input = error_mark_node; | |
8206 | } | |
74f0c611 RH |
8207 | } |
8208 | else | |
8209 | input = error_mark_node; | |
8210 | ||
8211 | TREE_VALUE (tail) = input; | |
8212 | } | |
3e4093b6 | 8213 | |
1c384bf1 RH |
8214 | /* ASMs with labels cannot have outputs. This should have been |
8215 | enforced by the parser. */ | |
8216 | gcc_assert (outputs == NULL || labels == NULL); | |
8217 | ||
8218 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
9f0e2d86 | 8219 | |
5544530a PB |
8220 | /* asm statements without outputs, including simple ones, are treated |
8221 | as volatile. */ | |
8222 | ASM_INPUT_P (args) = simple; | |
8223 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
74f0c611 | 8224 | |
9f0e2d86 | 8225 | return args; |
e5e809f4 | 8226 | } |
3e4093b6 | 8227 | \f |
c2255bc4 AH |
8228 | /* Generate a goto statement to LABEL. LOC is the location of the |
8229 | GOTO. */ | |
506e2710 RH |
8230 | |
8231 | tree | |
c2255bc4 | 8232 | c_finish_goto_label (location_t loc, tree label) |
506e2710 | 8233 | { |
e1b7793c | 8234 | tree decl = lookup_label_for_goto (loc, label); |
506e2710 RH |
8235 | if (!decl) |
8236 | return NULL_TREE; | |
506e2710 | 8237 | TREE_USED (decl) = 1; |
c2255bc4 AH |
8238 | { |
8239 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
8240 | SET_EXPR_LOCATION (t, loc); | |
8241 | return add_stmt (t); | |
8242 | } | |
506e2710 RH |
8243 | } |
8244 | ||
c2255bc4 AH |
8245 | /* Generate a computed goto statement to EXPR. LOC is the location of |
8246 | the GOTO. */ | |
506e2710 RH |
8247 | |
8248 | tree | |
c2255bc4 | 8249 | c_finish_goto_ptr (location_t loc, tree expr) |
506e2710 | 8250 | { |
c2255bc4 AH |
8251 | tree t; |
8252 | pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>"); | |
928c19bb | 8253 | expr = c_fully_fold (expr, false, NULL); |
506e2710 | 8254 | expr = convert (ptr_type_node, expr); |
c2255bc4 AH |
8255 | t = build1 (GOTO_EXPR, void_type_node, expr); |
8256 | SET_EXPR_LOCATION (t, loc); | |
8257 | return add_stmt (t); | |
506e2710 RH |
8258 | } |
8259 | ||
5088b058 | 8260 | /* Generate a C `return' statement. RETVAL is the expression for what |
c2255bc4 AH |
8261 | to return, or a null pointer for `return;' with no value. LOC is |
8262 | the location of the return statement. If ORIGTYPE is not NULL_TREE, it | |
8263 | is the original type of RETVAL. */ | |
de520661 | 8264 | |
506e2710 | 8265 | tree |
c2255bc4 | 8266 | c_finish_return (location_t loc, tree retval, tree origtype) |
3e4093b6 | 8267 | { |
0c9b182b JJ |
8268 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; |
8269 | bool no_warning = false; | |
928c19bb | 8270 | bool npc = false; |
3e4093b6 RS |
8271 | |
8272 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
c2255bc4 AH |
8273 | warning_at (loc, 0, |
8274 | "function declared %<noreturn%> has a %<return%> statement"); | |
3e4093b6 | 8275 | |
928c19bb JM |
8276 | if (retval) |
8277 | { | |
8ce94e44 | 8278 | tree semantic_type = NULL_TREE; |
928c19bb | 8279 | npc = null_pointer_constant_p (retval); |
8ce94e44 JM |
8280 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) |
8281 | { | |
8282 | semantic_type = TREE_TYPE (retval); | |
8283 | retval = TREE_OPERAND (retval, 0); | |
8284 | } | |
928c19bb | 8285 | retval = c_fully_fold (retval, false, NULL); |
8ce94e44 JM |
8286 | if (semantic_type) |
8287 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
928c19bb JM |
8288 | } |
8289 | ||
3e4093b6 | 8290 | if (!retval) |
de520661 | 8291 | { |
3e4093b6 RS |
8292 | current_function_returns_null = 1; |
8293 | if ((warn_return_type || flag_isoc99) | |
8294 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
0c9b182b | 8295 | { |
b8698a0f | 8296 | pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type, |
fcf73884 | 8297 | "%<return%> with no value, in " |
0c9b182b JJ |
8298 | "function returning non-void"); |
8299 | no_warning = true; | |
8300 | } | |
400fbf9f | 8301 | } |
3e4093b6 | 8302 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
de520661 | 8303 | { |
3e4093b6 | 8304 | current_function_returns_null = 1; |
2397c575 | 8305 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) |
b8698a0f | 8306 | pedwarn (loc, 0, |
509c9d60 | 8307 | "%<return%> with a value, in function returning void"); |
b8698a0f | 8308 | else |
c2255bc4 | 8309 | pedwarn (loc, OPT_pedantic, "ISO C forbids " |
fcf73884 | 8310 | "%<return%> with expression, in function returning void"); |
de520661 | 8311 | } |
3e4093b6 | 8312 | else |
de520661 | 8313 | { |
c2255bc4 AH |
8314 | tree t = convert_for_assignment (loc, valtype, retval, origtype, |
8315 | ic_return, | |
8316 | npc, NULL_TREE, NULL_TREE, 0); | |
3e4093b6 RS |
8317 | tree res = DECL_RESULT (current_function_decl); |
8318 | tree inner; | |
8319 | ||
8320 | current_function_returns_value = 1; | |
8321 | if (t == error_mark_node) | |
506e2710 | 8322 | return NULL_TREE; |
3e4093b6 RS |
8323 | |
8324 | inner = t = convert (TREE_TYPE (res), t); | |
8325 | ||
8326 | /* Strip any conversions, additions, and subtractions, and see if | |
8327 | we are returning the address of a local variable. Warn if so. */ | |
8328 | while (1) | |
8b6a5902 | 8329 | { |
3e4093b6 | 8330 | switch (TREE_CODE (inner)) |
8b6a5902 | 8331 | { |
849421a3 JJ |
8332 | CASE_CONVERT: |
8333 | case NON_LVALUE_EXPR: | |
3e4093b6 | 8334 | case PLUS_EXPR: |
849421a3 | 8335 | case POINTER_PLUS_EXPR: |
3e4093b6 RS |
8336 | inner = TREE_OPERAND (inner, 0); |
8337 | continue; | |
8338 | ||
8339 | case MINUS_EXPR: | |
8340 | /* If the second operand of the MINUS_EXPR has a pointer | |
8341 | type (or is converted from it), this may be valid, so | |
8342 | don't give a warning. */ | |
8343 | { | |
8344 | tree op1 = TREE_OPERAND (inner, 1); | |
8b6a5902 | 8345 | |
3f75a254 | 8346 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) |
1043771b TB |
8347 | && (CONVERT_EXPR_P (op1) |
8348 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
3e4093b6 | 8349 | op1 = TREE_OPERAND (op1, 0); |
8b6a5902 | 8350 | |
3e4093b6 RS |
8351 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
8352 | break; | |
8b6a5902 | 8353 | |
3e4093b6 RS |
8354 | inner = TREE_OPERAND (inner, 0); |
8355 | continue; | |
8356 | } | |
400fbf9f | 8357 | |
3e4093b6 RS |
8358 | case ADDR_EXPR: |
8359 | inner = TREE_OPERAND (inner, 0); | |
c2f4acb7 | 8360 | |
6615c446 | 8361 | while (REFERENCE_CLASS_P (inner) |
c22cacf3 | 8362 | && TREE_CODE (inner) != INDIRECT_REF) |
3e4093b6 | 8363 | inner = TREE_OPERAND (inner, 0); |
8b6a5902 | 8364 | |
a2f1f4c3 | 8365 | if (DECL_P (inner) |
3f75a254 JM |
8366 | && !DECL_EXTERNAL (inner) |
8367 | && !TREE_STATIC (inner) | |
3e4093b6 | 8368 | && DECL_CONTEXT (inner) == current_function_decl) |
c2255bc4 AH |
8369 | warning_at (loc, |
8370 | 0, "function returns address of local variable"); | |
3e4093b6 | 8371 | break; |
8b6a5902 | 8372 | |
3e4093b6 RS |
8373 | default: |
8374 | break; | |
8375 | } | |
de520661 | 8376 | |
3e4093b6 RS |
8377 | break; |
8378 | } | |
8379 | ||
53fb4de3 | 8380 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
c2255bc4 | 8381 | SET_EXPR_LOCATION (retval, loc); |
ca085fd7 MLI |
8382 | |
8383 | if (warn_sequence_point) | |
8384 | verify_sequence_points (retval); | |
de520661 | 8385 | } |
8b6a5902 | 8386 | |
c2255bc4 | 8387 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); |
0c9b182b JJ |
8388 | TREE_NO_WARNING (ret_stmt) |= no_warning; |
8389 | return add_stmt (ret_stmt); | |
de520661 | 8390 | } |
3e4093b6 RS |
8391 | \f |
8392 | struct c_switch { | |
604f5adf ILT |
8393 | /* The SWITCH_EXPR being built. */ |
8394 | tree switch_expr; | |
a6c0a76c | 8395 | |
89dbed81 | 8396 | /* The original type of the testing expression, i.e. before the |
a6c0a76c SB |
8397 | default conversion is applied. */ |
8398 | tree orig_type; | |
8399 | ||
3e4093b6 RS |
8400 | /* A splay-tree mapping the low element of a case range to the high |
8401 | element, or NULL_TREE if there is no high element. Used to | |
8402 | determine whether or not a new case label duplicates an old case | |
8403 | label. We need a tree, rather than simply a hash table, because | |
8404 | of the GNU case range extension. */ | |
8405 | splay_tree cases; | |
a6c0a76c | 8406 | |
e1b7793c ILT |
8407 | /* The bindings at the point of the switch. This is used for |
8408 | warnings crossing decls when branching to a case label. */ | |
8409 | struct c_spot_bindings *bindings; | |
187230a7 | 8410 | |
3e4093b6 RS |
8411 | /* The next node on the stack. */ |
8412 | struct c_switch *next; | |
8413 | }; | |
400fbf9f | 8414 | |
3e4093b6 RS |
8415 | /* A stack of the currently active switch statements. The innermost |
8416 | switch statement is on the top of the stack. There is no need to | |
8417 | mark the stack for garbage collection because it is only active | |
8418 | during the processing of the body of a function, and we never | |
8419 | collect at that point. */ | |
de520661 | 8420 | |
506e2710 | 8421 | struct c_switch *c_switch_stack; |
de520661 | 8422 | |
3e4093b6 | 8423 | /* Start a C switch statement, testing expression EXP. Return the new |
c2255bc4 AH |
8424 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. |
8425 | SWITCH_COND_LOC is the location of the switch's condition. */ | |
de520661 | 8426 | |
3e4093b6 | 8427 | tree |
c2255bc4 AH |
8428 | c_start_case (location_t switch_loc, |
8429 | location_t switch_cond_loc, | |
8430 | tree exp) | |
de520661 | 8431 | { |
c58e8676 | 8432 | tree orig_type = error_mark_node; |
3e4093b6 | 8433 | struct c_switch *cs; |
2f6e4e97 | 8434 | |
3e4093b6 | 8435 | if (exp != error_mark_node) |
de520661 | 8436 | { |
3e4093b6 RS |
8437 | orig_type = TREE_TYPE (exp); |
8438 | ||
c58e8676 | 8439 | if (!INTEGRAL_TYPE_P (orig_type)) |
de520661 | 8440 | { |
c58e8676 VR |
8441 | if (orig_type != error_mark_node) |
8442 | { | |
c2255bc4 | 8443 | error_at (switch_cond_loc, "switch quantity not an integer"); |
c58e8676 VR |
8444 | orig_type = error_mark_node; |
8445 | } | |
3e4093b6 | 8446 | exp = integer_zero_node; |
de520661 | 8447 | } |
3e4093b6 | 8448 | else |
de520661 | 8449 | { |
c58e8676 | 8450 | tree type = TYPE_MAIN_VARIANT (orig_type); |
8b6a5902 | 8451 | |
3176a0c2 | 8452 | if (!in_system_header |
3e4093b6 RS |
8453 | && (type == long_integer_type_node |
8454 | || type == long_unsigned_type_node)) | |
c2255bc4 AH |
8455 | warning_at (switch_cond_loc, |
8456 | OPT_Wtraditional, "%<long%> switch expression not " | |
8457 | "converted to %<int%> in ISO C"); | |
8b6a5902 | 8458 | |
928c19bb | 8459 | exp = c_fully_fold (exp, false, NULL); |
3e4093b6 | 8460 | exp = default_conversion (exp); |
ca085fd7 MLI |
8461 | |
8462 | if (warn_sequence_point) | |
8463 | verify_sequence_points (exp); | |
3e4093b6 RS |
8464 | } |
8465 | } | |
8466 | ||
604f5adf | 8467 | /* Add this new SWITCH_EXPR to the stack. */ |
5d038c4c | 8468 | cs = XNEW (struct c_switch); |
604f5adf | 8469 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); |
c2255bc4 | 8470 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); |
a6c0a76c | 8471 | cs->orig_type = orig_type; |
3e4093b6 | 8472 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
e1b7793c | 8473 | cs->bindings = c_get_switch_bindings (); |
506e2710 RH |
8474 | cs->next = c_switch_stack; |
8475 | c_switch_stack = cs; | |
3e4093b6 | 8476 | |
604f5adf | 8477 | return add_stmt (cs->switch_expr); |
3e4093b6 RS |
8478 | } |
8479 | ||
c2255bc4 | 8480 | /* Process a case label at location LOC. */ |
3e4093b6 RS |
8481 | |
8482 | tree | |
c2255bc4 | 8483 | do_case (location_t loc, tree low_value, tree high_value) |
3e4093b6 RS |
8484 | { |
8485 | tree label = NULL_TREE; | |
8486 | ||
17cede2e JM |
8487 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) |
8488 | { | |
8489 | low_value = c_fully_fold (low_value, false, NULL); | |
8490 | if (TREE_CODE (low_value) == INTEGER_CST) | |
8491 | pedwarn (input_location, OPT_pedantic, | |
8492 | "case label is not an integer constant expression"); | |
8493 | } | |
8494 | ||
8495 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
8496 | { | |
8497 | high_value = c_fully_fold (high_value, false, NULL); | |
8498 | if (TREE_CODE (high_value) == INTEGER_CST) | |
8499 | pedwarn (input_location, OPT_pedantic, | |
8500 | "case label is not an integer constant expression"); | |
8501 | } | |
8502 | ||
e1b7793c | 8503 | if (c_switch_stack == NULL) |
187230a7 JM |
8504 | { |
8505 | if (low_value) | |
e1b7793c | 8506 | error_at (loc, "case label not within a switch statement"); |
187230a7 | 8507 | else |
e1b7793c ILT |
8508 | error_at (loc, "%<default%> label not within a switch statement"); |
8509 | return NULL_TREE; | |
187230a7 | 8510 | } |
de520661 | 8511 | |
e1b7793c ILT |
8512 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, |
8513 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
8514 | loc)) | |
8515 | return NULL_TREE; | |
8516 | ||
8517 | label = c_add_case_label (loc, c_switch_stack->cases, | |
8518 | SWITCH_COND (c_switch_stack->switch_expr), | |
8519 | c_switch_stack->orig_type, | |
8520 | low_value, high_value); | |
8521 | if (label == error_mark_node) | |
8522 | label = NULL_TREE; | |
3e4093b6 RS |
8523 | return label; |
8524 | } | |
de520661 | 8525 | |
3e4093b6 | 8526 | /* Finish the switch statement. */ |
de520661 | 8527 | |
3e4093b6 | 8528 | void |
325c3691 | 8529 | c_finish_case (tree body) |
3e4093b6 | 8530 | { |
506e2710 | 8531 | struct c_switch *cs = c_switch_stack; |
fbc315db | 8532 | location_t switch_location; |
3e4093b6 | 8533 | |
604f5adf | 8534 | SWITCH_BODY (cs->switch_expr) = body; |
325c3691 | 8535 | |
6de9cd9a | 8536 | /* Emit warnings as needed. */ |
c2255bc4 | 8537 | switch_location = EXPR_LOCATION (cs->switch_expr); |
fbc315db ILT |
8538 | c_do_switch_warnings (cs->cases, switch_location, |
8539 | TREE_TYPE (cs->switch_expr), | |
8540 | SWITCH_COND (cs->switch_expr)); | |
6de9cd9a | 8541 | |
3e4093b6 | 8542 | /* Pop the stack. */ |
506e2710 | 8543 | c_switch_stack = cs->next; |
3e4093b6 | 8544 | splay_tree_delete (cs->cases); |
e1b7793c | 8545 | c_release_switch_bindings (cs->bindings); |
5d038c4c | 8546 | XDELETE (cs); |
de520661 | 8547 | } |
325c3691 | 8548 | \f |
506e2710 RH |
8549 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
8550 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
8551 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
8552 | statement, and was not surrounded with parenthesis. */ | |
325c3691 | 8553 | |
9e51cf9d | 8554 | void |
506e2710 RH |
8555 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
8556 | tree else_block, bool nested_if) | |
325c3691 | 8557 | { |
506e2710 | 8558 | tree stmt; |
325c3691 | 8559 | |
506e2710 RH |
8560 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
8561 | if (warn_parentheses && nested_if && else_block == NULL) | |
325c3691 | 8562 | { |
506e2710 | 8563 | tree inner_if = then_block; |
16865eaa | 8564 | |
61ada8ae | 8565 | /* We know from the grammar productions that there is an IF nested |
506e2710 RH |
8566 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
8567 | it might not be exactly THEN_BLOCK, but should be the last | |
8568 | non-container statement within. */ | |
8569 | while (1) | |
8570 | switch (TREE_CODE (inner_if)) | |
8571 | { | |
8572 | case COND_EXPR: | |
8573 | goto found; | |
8574 | case BIND_EXPR: | |
8575 | inner_if = BIND_EXPR_BODY (inner_if); | |
8576 | break; | |
8577 | case STATEMENT_LIST: | |
8578 | inner_if = expr_last (then_block); | |
8579 | break; | |
8580 | case TRY_FINALLY_EXPR: | |
8581 | case TRY_CATCH_EXPR: | |
8582 | inner_if = TREE_OPERAND (inner_if, 0); | |
8583 | break; | |
8584 | default: | |
366de0ce | 8585 | gcc_unreachable (); |
506e2710 RH |
8586 | } |
8587 | found: | |
16865eaa | 8588 | |
506e2710 | 8589 | if (COND_EXPR_ELSE (inner_if)) |
fab922b1 MLI |
8590 | warning_at (if_locus, OPT_Wparentheses, |
8591 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
506e2710 | 8592 | } |
16865eaa | 8593 | |
2214de30 | 8594 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); |
a281759f | 8595 | SET_EXPR_LOCATION (stmt, if_locus); |
506e2710 | 8596 | add_stmt (stmt); |
325c3691 RH |
8597 | } |
8598 | ||
506e2710 RH |
8599 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
8600 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
8601 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
61ada8ae | 8602 | the statement controlled by the loop. BLAB is the break label. CLAB is |
506e2710 | 8603 | the continue label. Everything is allowed to be NULL. */ |
325c3691 RH |
8604 | |
8605 | void | |
506e2710 RH |
8606 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
8607 | tree blab, tree clab, bool cond_is_first) | |
325c3691 | 8608 | { |
506e2710 RH |
8609 | tree entry = NULL, exit = NULL, t; |
8610 | ||
28af952a RS |
8611 | /* If the condition is zero don't generate a loop construct. */ |
8612 | if (cond && integer_zerop (cond)) | |
8613 | { | |
8614 | if (cond_is_first) | |
8615 | { | |
8616 | t = build_and_jump (&blab); | |
8617 | SET_EXPR_LOCATION (t, start_locus); | |
8618 | add_stmt (t); | |
8619 | } | |
8620 | } | |
8621 | else | |
506e2710 RH |
8622 | { |
8623 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
c22cacf3 | 8624 | |
506e2710 | 8625 | /* If we have an exit condition, then we build an IF with gotos either |
c22cacf3 MS |
8626 | out of the loop, or to the top of it. If there's no exit condition, |
8627 | then we just build a jump back to the top. */ | |
506e2710 | 8628 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); |
c22cacf3 | 8629 | |
28af952a | 8630 | if (cond && !integer_nonzerop (cond)) |
c22cacf3 MS |
8631 | { |
8632 | /* Canonicalize the loop condition to the end. This means | |
8633 | generating a branch to the loop condition. Reuse the | |
8634 | continue label, if possible. */ | |
8635 | if (cond_is_first) | |
8636 | { | |
8637 | if (incr || !clab) | |
8638 | { | |
8639 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
8640 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
8641 | } | |
8642 | else | |
8643 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
a281759f | 8644 | SET_EXPR_LOCATION (t, start_locus); |
c22cacf3 MS |
8645 | add_stmt (t); |
8646 | } | |
8647 | ||
506e2710 | 8648 | t = build_and_jump (&blab); |
506e2710 | 8649 | if (cond_is_first) |
db3927fb AH |
8650 | exit = fold_build3_loc (start_locus, |
8651 | COND_EXPR, void_type_node, cond, exit, t); | |
506e2710 | 8652 | else |
db3927fb AH |
8653 | exit = fold_build3_loc (input_location, |
8654 | COND_EXPR, void_type_node, cond, exit, t); | |
c22cacf3 MS |
8655 | } |
8656 | ||
506e2710 RH |
8657 | add_stmt (top); |
8658 | } | |
c22cacf3 | 8659 | |
506e2710 RH |
8660 | if (body) |
8661 | add_stmt (body); | |
8662 | if (clab) | |
8663 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
8664 | if (incr) | |
8665 | add_stmt (incr); | |
8666 | if (entry) | |
8667 | add_stmt (entry); | |
8668 | if (exit) | |
8669 | add_stmt (exit); | |
8670 | if (blab) | |
8671 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
325c3691 | 8672 | } |
325c3691 RH |
8673 | |
8674 | tree | |
c2255bc4 | 8675 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) |
325c3691 | 8676 | { |
089efaa4 | 8677 | bool skip; |
506e2710 | 8678 | tree label = *label_p; |
325c3691 | 8679 | |
089efaa4 ILT |
8680 | /* In switch statements break is sometimes stylistically used after |
8681 | a return statement. This can lead to spurious warnings about | |
8682 | control reaching the end of a non-void function when it is | |
8683 | inlined. Note that we are calling block_may_fallthru with | |
8684 | language specific tree nodes; this works because | |
8685 | block_may_fallthru returns true when given something it does not | |
8686 | understand. */ | |
8687 | skip = !block_may_fallthru (cur_stmt_list); | |
8688 | ||
506e2710 | 8689 | if (!label) |
089efaa4 ILT |
8690 | { |
8691 | if (!skip) | |
c2255bc4 | 8692 | *label_p = label = create_artificial_label (loc); |
089efaa4 | 8693 | } |
953ff289 DN |
8694 | else if (TREE_CODE (label) == LABEL_DECL) |
8695 | ; | |
8696 | else switch (TREE_INT_CST_LOW (label)) | |
506e2710 | 8697 | { |
953ff289 | 8698 | case 0: |
506e2710 | 8699 | if (is_break) |
c2255bc4 | 8700 | error_at (loc, "break statement not within loop or switch"); |
506e2710 | 8701 | else |
c2255bc4 | 8702 | error_at (loc, "continue statement not within a loop"); |
506e2710 | 8703 | return NULL_TREE; |
953ff289 DN |
8704 | |
8705 | case 1: | |
8706 | gcc_assert (is_break); | |
c2255bc4 | 8707 | error_at (loc, "break statement used with OpenMP for loop"); |
953ff289 DN |
8708 | return NULL_TREE; |
8709 | ||
8710 | default: | |
8711 | gcc_unreachable (); | |
506e2710 | 8712 | } |
325c3691 | 8713 | |
089efaa4 ILT |
8714 | if (skip) |
8715 | return NULL_TREE; | |
8716 | ||
2e28e797 JH |
8717 | if (!is_break) |
8718 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
8719 | ||
53fb4de3 | 8720 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
325c3691 RH |
8721 | } |
8722 | ||
506e2710 | 8723 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
3a5b9284 RH |
8724 | |
8725 | static void | |
c2255bc4 | 8726 | emit_side_effect_warnings (location_t loc, tree expr) |
3a5b9284 | 8727 | { |
e6b5a630 RH |
8728 | if (expr == error_mark_node) |
8729 | ; | |
8730 | else if (!TREE_SIDE_EFFECTS (expr)) | |
3a5b9284 RH |
8731 | { |
8732 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
c2255bc4 | 8733 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); |
3a5b9284 | 8734 | } |
27f33b15 | 8735 | else |
c2255bc4 | 8736 | warn_if_unused_value (expr, loc); |
3a5b9284 RH |
8737 | } |
8738 | ||
506e2710 | 8739 | /* Process an expression as if it were a complete statement. Emit |
c2255bc4 AH |
8740 | diagnostics, but do not call ADD_STMT. LOC is the location of the |
8741 | statement. */ | |
3a5b9284 | 8742 | |
506e2710 | 8743 | tree |
c2255bc4 | 8744 | c_process_expr_stmt (location_t loc, tree expr) |
3a5b9284 RH |
8745 | { |
8746 | if (!expr) | |
506e2710 | 8747 | return NULL_TREE; |
3a5b9284 | 8748 | |
928c19bb JM |
8749 | expr = c_fully_fold (expr, false, NULL); |
8750 | ||
3a5b9284 RH |
8751 | if (warn_sequence_point) |
8752 | verify_sequence_points (expr); | |
8753 | ||
8754 | if (TREE_TYPE (expr) != error_mark_node | |
8755 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
8756 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
c2255bc4 | 8757 | error_at (loc, "expression statement has incomplete type"); |
3a5b9284 RH |
8758 | |
8759 | /* If we're not processing a statement expression, warn about unused values. | |
8760 | Warnings for statement expressions will be emitted later, once we figure | |
8761 | out which is the result. */ | |
8762 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
27f33b15 | 8763 | && warn_unused_value) |
c2255bc4 | 8764 | emit_side_effect_warnings (loc, expr); |
3a5b9284 RH |
8765 | |
8766 | /* If the expression is not of a type to which we cannot assign a line | |
8767 | number, wrap the thing in a no-op NOP_EXPR. */ | |
6615c446 | 8768 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) |
c2255bc4 AH |
8769 | { |
8770 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
8771 | SET_EXPR_LOCATION (expr, loc); | |
8772 | } | |
506e2710 RH |
8773 | |
8774 | return expr; | |
8775 | } | |
8776 | ||
c2255bc4 AH |
8777 | /* Emit an expression as a statement. LOC is the location of the |
8778 | expression. */ | |
506e2710 RH |
8779 | |
8780 | tree | |
c2255bc4 | 8781 | c_finish_expr_stmt (location_t loc, tree expr) |
506e2710 RH |
8782 | { |
8783 | if (expr) | |
c2255bc4 | 8784 | return add_stmt (c_process_expr_stmt (loc, expr)); |
506e2710 RH |
8785 | else |
8786 | return NULL; | |
3a5b9284 RH |
8787 | } |
8788 | ||
8789 | /* Do the opposite and emit a statement as an expression. To begin, | |
8790 | create a new binding level and return it. */ | |
325c3691 RH |
8791 | |
8792 | tree | |
8793 | c_begin_stmt_expr (void) | |
8794 | { | |
8795 | tree ret; | |
8796 | ||
8797 | /* We must force a BLOCK for this level so that, if it is not expanded | |
8798 | later, there is a way to turn off the entire subtree of blocks that | |
8799 | are contained in it. */ | |
8800 | keep_next_level (); | |
8801 | ret = c_begin_compound_stmt (true); | |
e1b7793c ILT |
8802 | |
8803 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
8804 | ? NULL | |
8805 | : c_switch_stack->bindings); | |
325c3691 RH |
8806 | |
8807 | /* Mark the current statement list as belonging to a statement list. */ | |
8808 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
8809 | ||
8810 | return ret; | |
8811 | } | |
8812 | ||
c2255bc4 AH |
8813 | /* LOC is the location of the compound statement to which this body |
8814 | belongs. */ | |
8815 | ||
325c3691 | 8816 | tree |
c2255bc4 | 8817 | c_finish_stmt_expr (location_t loc, tree body) |
325c3691 | 8818 | { |
3a5b9284 | 8819 | tree last, type, tmp, val; |
325c3691 RH |
8820 | tree *last_p; |
8821 | ||
c2255bc4 | 8822 | body = c_end_compound_stmt (loc, body, true); |
e1b7793c ILT |
8823 | |
8824 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
8825 | ? NULL | |
8826 | : c_switch_stack->bindings); | |
325c3691 | 8827 | |
3a5b9284 RH |
8828 | /* Locate the last statement in BODY. See c_end_compound_stmt |
8829 | about always returning a BIND_EXPR. */ | |
8830 | last_p = &BIND_EXPR_BODY (body); | |
8831 | last = BIND_EXPR_BODY (body); | |
8832 | ||
8833 | continue_searching: | |
325c3691 RH |
8834 | if (TREE_CODE (last) == STATEMENT_LIST) |
8835 | { | |
3a5b9284 RH |
8836 | tree_stmt_iterator i; |
8837 | ||
8838 | /* This can happen with degenerate cases like ({ }). No value. */ | |
8839 | if (!TREE_SIDE_EFFECTS (last)) | |
8840 | return body; | |
8841 | ||
8842 | /* If we're supposed to generate side effects warnings, process | |
8843 | all of the statements except the last. */ | |
27f33b15 | 8844 | if (warn_unused_value) |
325c3691 | 8845 | { |
3a5b9284 | 8846 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
c2255bc4 AH |
8847 | { |
8848 | location_t tloc; | |
8849 | tree t = tsi_stmt (i); | |
8850 | ||
8851 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
8852 | emit_side_effect_warnings (tloc, t); | |
8853 | } | |
325c3691 RH |
8854 | } |
8855 | else | |
3a5b9284 RH |
8856 | i = tsi_last (last); |
8857 | last_p = tsi_stmt_ptr (i); | |
8858 | last = *last_p; | |
325c3691 RH |
8859 | } |
8860 | ||
3a5b9284 RH |
8861 | /* If the end of the list is exception related, then the list was split |
8862 | by a call to push_cleanup. Continue searching. */ | |
8863 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
8864 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
8865 | { | |
8866 | last_p = &TREE_OPERAND (last, 0); | |
8867 | last = *last_p; | |
8868 | goto continue_searching; | |
8869 | } | |
8870 | ||
26d8af35 JM |
8871 | if (last == error_mark_node) |
8872 | return last; | |
8873 | ||
3a5b9284 RH |
8874 | /* In the case that the BIND_EXPR is not necessary, return the |
8875 | expression out from inside it. */ | |
26d8af35 JM |
8876 | if (last == BIND_EXPR_BODY (body) |
8877 | && BIND_EXPR_VARS (body) == NULL) | |
591baeb0 | 8878 | { |
928c19bb JM |
8879 | /* Even if this looks constant, do not allow it in a constant |
8880 | expression. */ | |
e5a94231 | 8881 | last = c_wrap_maybe_const (last, true); |
591baeb0 JM |
8882 | /* Do not warn if the return value of a statement expression is |
8883 | unused. */ | |
928c19bb | 8884 | TREE_NO_WARNING (last) = 1; |
591baeb0 JM |
8885 | return last; |
8886 | } | |
325c3691 RH |
8887 | |
8888 | /* Extract the type of said expression. */ | |
8889 | type = TREE_TYPE (last); | |
325c3691 | 8890 | |
3a5b9284 RH |
8891 | /* If we're not returning a value at all, then the BIND_EXPR that |
8892 | we already have is a fine expression to return. */ | |
8893 | if (!type || VOID_TYPE_P (type)) | |
8894 | return body; | |
8895 | ||
8896 | /* Now that we've located the expression containing the value, it seems | |
8897 | silly to make voidify_wrapper_expr repeat the process. Create a | |
8898 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
8899 | tmp = create_tmp_var_raw (type, NULL); | |
8900 | ||
8901 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
8902 | tree_expr_nonnegative_p giving up immediately. */ | |
8903 | val = last; | |
8904 | if (TREE_CODE (val) == NOP_EXPR | |
8905 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
8906 | val = TREE_OPERAND (val, 0); | |
8907 | ||
53fb4de3 | 8908 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
5e278028 | 8909 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); |
3a5b9284 | 8910 | |
c2255bc4 AH |
8911 | { |
8912 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
8913 | SET_EXPR_LOCATION (t, loc); | |
8914 | return t; | |
8915 | } | |
325c3691 RH |
8916 | } |
8917 | \f | |
8918 | /* Begin and end compound statements. This is as simple as pushing | |
8919 | and popping new statement lists from the tree. */ | |
8920 | ||
8921 | tree | |
8922 | c_begin_compound_stmt (bool do_scope) | |
8923 | { | |
8924 | tree stmt = push_stmt_list (); | |
8925 | if (do_scope) | |
4dfa0342 | 8926 | push_scope (); |
325c3691 RH |
8927 | return stmt; |
8928 | } | |
8929 | ||
c2255bc4 AH |
8930 | /* End a compound statement. STMT is the statement. LOC is the |
8931 | location of the compound statement-- this is usually the location | |
8932 | of the opening brace. */ | |
8933 | ||
325c3691 | 8934 | tree |
c2255bc4 | 8935 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) |
325c3691 RH |
8936 | { |
8937 | tree block = NULL; | |
8938 | ||
8939 | if (do_scope) | |
8940 | { | |
8941 | if (c_dialect_objc ()) | |
8942 | objc_clear_super_receiver (); | |
8943 | block = pop_scope (); | |
8944 | } | |
8945 | ||
8946 | stmt = pop_stmt_list (stmt); | |
c2255bc4 | 8947 | stmt = c_build_bind_expr (loc, block, stmt); |
325c3691 RH |
8948 | |
8949 | /* If this compound statement is nested immediately inside a statement | |
8950 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
8951 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
8952 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
8953 | was really last. */ | |
8954 | if (cur_stmt_list | |
8955 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
8956 | && TREE_CODE (stmt) != BIND_EXPR) | |
8957 | { | |
53fb4de3 | 8958 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
325c3691 | 8959 | TREE_SIDE_EFFECTS (stmt) = 1; |
c2255bc4 | 8960 | SET_EXPR_LOCATION (stmt, loc); |
325c3691 RH |
8961 | } |
8962 | ||
8963 | return stmt; | |
8964 | } | |
5a508662 RH |
8965 | |
8966 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
8967 | when the current scope is exited. EH_ONLY is true when this is not | |
8968 | meant to apply to normal control flow transfer. */ | |
8969 | ||
8970 | void | |
c2255bc4 | 8971 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
5a508662 | 8972 | { |
3a5b9284 RH |
8973 | enum tree_code code; |
8974 | tree stmt, list; | |
8975 | bool stmt_expr; | |
8976 | ||
8977 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
c2255bc4 | 8978 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); |
5a508662 | 8979 | add_stmt (stmt); |
3a5b9284 RH |
8980 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
8981 | list = push_stmt_list (); | |
8982 | TREE_OPERAND (stmt, 0) = list; | |
8983 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
5a508662 | 8984 | } |
325c3691 | 8985 | \f |
3e4093b6 RS |
8986 | /* Build a binary-operation expression without default conversions. |
8987 | CODE is the kind of expression to build. | |
ba47d38d | 8988 | LOCATION is the operator's location. |
3e4093b6 RS |
8989 | This function differs from `build' in several ways: |
8990 | the data type of the result is computed and recorded in it, | |
8991 | warnings are generated if arg data types are invalid, | |
8992 | special handling for addition and subtraction of pointers is known, | |
8993 | and some optimization is done (operations on narrow ints | |
8994 | are done in the narrower type when that gives the same result). | |
8995 | Constant folding is also done before the result is returned. | |
de520661 | 8996 | |
3e4093b6 RS |
8997 | Note that the operands will never have enumeral types, or function |
8998 | or array types, because either they will have the default conversions | |
8999 | performed or they have both just been converted to some other type in which | |
9000 | the arithmetic is to be done. */ | |
9001 | ||
9002 | tree | |
ba47d38d AH |
9003 | build_binary_op (location_t location, enum tree_code code, |
9004 | tree orig_op0, tree orig_op1, int convert_p) | |
de520661 | 9005 | { |
8ce94e44 JM |
9006 | tree type0, type1, orig_type0, orig_type1; |
9007 | tree eptype; | |
3e4093b6 RS |
9008 | enum tree_code code0, code1; |
9009 | tree op0, op1; | |
c9f9eb5d | 9010 | tree ret = error_mark_node; |
4de67c26 | 9011 | const char *invalid_op_diag; |
4d84fe7c | 9012 | bool op0_int_operands, op1_int_operands; |
928c19bb | 9013 | bool int_const, int_const_or_overflow, int_operands; |
b62acd60 | 9014 | |
3e4093b6 RS |
9015 | /* Expression code to give to the expression when it is built. |
9016 | Normally this is CODE, which is what the caller asked for, | |
9017 | but in some special cases we change it. */ | |
9018 | enum tree_code resultcode = code; | |
8b6a5902 | 9019 | |
3e4093b6 RS |
9020 | /* Data type in which the computation is to be performed. |
9021 | In the simplest cases this is the common type of the arguments. */ | |
9022 | tree result_type = NULL; | |
9023 | ||
8ce94e44 JM |
9024 | /* When the computation is in excess precision, the type of the |
9025 | final EXCESS_PRECISION_EXPR. */ | |
9026 | tree real_result_type = NULL; | |
9027 | ||
3e4093b6 RS |
9028 | /* Nonzero means operands have already been type-converted |
9029 | in whatever way is necessary. | |
9030 | Zero means they need to be converted to RESULT_TYPE. */ | |
9031 | int converted = 0; | |
9032 | ||
9033 | /* Nonzero means create the expression with this type, rather than | |
9034 | RESULT_TYPE. */ | |
9035 | tree build_type = 0; | |
9036 | ||
9037 | /* Nonzero means after finally constructing the expression | |
9038 | convert it to this type. */ | |
9039 | tree final_type = 0; | |
9040 | ||
9041 | /* Nonzero if this is an operation like MIN or MAX which can | |
9042 | safely be computed in short if both args are promoted shorts. | |
9043 | Also implies COMMON. | |
9044 | -1 indicates a bitwise operation; this makes a difference | |
9045 | in the exact conditions for when it is safe to do the operation | |
9046 | in a narrower mode. */ | |
9047 | int shorten = 0; | |
9048 | ||
9049 | /* Nonzero if this is a comparison operation; | |
9050 | if both args are promoted shorts, compare the original shorts. | |
9051 | Also implies COMMON. */ | |
9052 | int short_compare = 0; | |
9053 | ||
9054 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
9055 | original short and then promoted if the operand is a promoted short. */ | |
9056 | int short_shift = 0; | |
9057 | ||
9058 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
9059 | int common = 0; | |
9060 | ||
58393038 ZL |
9061 | /* True means types are compatible as far as ObjC is concerned. */ |
9062 | bool objc_ok; | |
9063 | ||
8ce94e44 JM |
9064 | /* True means this is an arithmetic operation that may need excess |
9065 | precision. */ | |
9066 | bool may_need_excess_precision; | |
9067 | ||
ba47d38d AH |
9068 | if (location == UNKNOWN_LOCATION) |
9069 | location = input_location; | |
9070 | ||
4d84fe7c JM |
9071 | op0 = orig_op0; |
9072 | op1 = orig_op1; | |
9073 | ||
9074 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
9075 | if (op0_int_operands) | |
9076 | op0 = remove_c_maybe_const_expr (op0); | |
9077 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
9078 | if (op1_int_operands) | |
9079 | op1 = remove_c_maybe_const_expr (op1); | |
9080 | int_operands = (op0_int_operands && op1_int_operands); | |
928c19bb JM |
9081 | if (int_operands) |
9082 | { | |
9083 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
9084 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
9085 | int_const = (int_const_or_overflow | |
9086 | && !TREE_OVERFLOW (orig_op0) | |
9087 | && !TREE_OVERFLOW (orig_op1)); | |
9088 | } | |
9089 | else | |
9090 | int_const = int_const_or_overflow = false; | |
9091 | ||
3e4093b6 | 9092 | if (convert_p) |
790e9490 | 9093 | { |
4d84fe7c JM |
9094 | op0 = default_conversion (op0); |
9095 | op1 = default_conversion (op1); | |
790e9490 RS |
9096 | } |
9097 | ||
8ce94e44 JM |
9098 | orig_type0 = type0 = TREE_TYPE (op0); |
9099 | orig_type1 = type1 = TREE_TYPE (op1); | |
91fa3c30 | 9100 | |
3e4093b6 RS |
9101 | /* The expression codes of the data types of the arguments tell us |
9102 | whether the arguments are integers, floating, pointers, etc. */ | |
9103 | code0 = TREE_CODE (type0); | |
9104 | code1 = TREE_CODE (type1); | |
9105 | ||
9106 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
9107 | STRIP_TYPE_NOPS (op0); | |
9108 | STRIP_TYPE_NOPS (op1); | |
9109 | ||
9110 | /* If an error was already reported for one of the arguments, | |
9111 | avoid reporting another error. */ | |
9112 | ||
9113 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
9114 | return error_mark_node; | |
9115 | ||
4de67c26 JM |
9116 | if ((invalid_op_diag |
9117 | = targetm.invalid_binary_op (code, type0, type1))) | |
9118 | { | |
ba47d38d | 9119 | error_at (location, invalid_op_diag); |
4de67c26 JM |
9120 | return error_mark_node; |
9121 | } | |
9122 | ||
8ce94e44 JM |
9123 | switch (code) |
9124 | { | |
9125 | case PLUS_EXPR: | |
9126 | case MINUS_EXPR: | |
9127 | case MULT_EXPR: | |
9128 | case TRUNC_DIV_EXPR: | |
9129 | case CEIL_DIV_EXPR: | |
9130 | case FLOOR_DIV_EXPR: | |
9131 | case ROUND_DIV_EXPR: | |
9132 | case EXACT_DIV_EXPR: | |
9133 | may_need_excess_precision = true; | |
9134 | break; | |
9135 | default: | |
9136 | may_need_excess_precision = false; | |
9137 | break; | |
9138 | } | |
9139 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
9140 | { | |
9141 | op0 = TREE_OPERAND (op0, 0); | |
9142 | type0 = TREE_TYPE (op0); | |
9143 | } | |
9144 | else if (may_need_excess_precision | |
9145 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
9146 | { | |
9147 | type0 = eptype; | |
9148 | op0 = convert (eptype, op0); | |
9149 | } | |
9150 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
9151 | { | |
9152 | op1 = TREE_OPERAND (op1, 0); | |
9153 | type1 = TREE_TYPE (op1); | |
9154 | } | |
9155 | else if (may_need_excess_precision | |
9156 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
9157 | { | |
9158 | type1 = eptype; | |
9159 | op1 = convert (eptype, op1); | |
9160 | } | |
9161 | ||
58393038 ZL |
9162 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); |
9163 | ||
3e4093b6 | 9164 | switch (code) |
de520661 | 9165 | { |
3e4093b6 RS |
9166 | case PLUS_EXPR: |
9167 | /* Handle the pointer + int case. */ | |
9168 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 9169 | { |
db3927fb | 9170 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); |
c9f9eb5d AH |
9171 | goto return_build_binary_op; |
9172 | } | |
3e4093b6 | 9173 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) |
c9f9eb5d | 9174 | { |
db3927fb | 9175 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); |
c9f9eb5d AH |
9176 | goto return_build_binary_op; |
9177 | } | |
fe67cf58 | 9178 | else |
3e4093b6 RS |
9179 | common = 1; |
9180 | break; | |
400fbf9f | 9181 | |
3e4093b6 RS |
9182 | case MINUS_EXPR: |
9183 | /* Subtraction of two similar pointers. | |
9184 | We must subtract them as integers, then divide by object size. */ | |
9185 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
744aa42f | 9186 | && comp_target_types (location, type0, type1)) |
c9f9eb5d | 9187 | { |
db3927fb | 9188 | ret = pointer_diff (location, op0, op1); |
c9f9eb5d AH |
9189 | goto return_build_binary_op; |
9190 | } | |
3e4093b6 RS |
9191 | /* Handle pointer minus int. Just like pointer plus int. */ |
9192 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 9193 | { |
db3927fb | 9194 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); |
c9f9eb5d AH |
9195 | goto return_build_binary_op; |
9196 | } | |
3e4093b6 RS |
9197 | else |
9198 | common = 1; | |
9199 | break; | |
8b6a5902 | 9200 | |
3e4093b6 RS |
9201 | case MULT_EXPR: |
9202 | common = 1; | |
9203 | break; | |
9204 | ||
9205 | case TRUNC_DIV_EXPR: | |
9206 | case CEIL_DIV_EXPR: | |
9207 | case FLOOR_DIV_EXPR: | |
9208 | case ROUND_DIV_EXPR: | |
9209 | case EXACT_DIV_EXPR: | |
c9f9eb5d | 9210 | warn_for_div_by_zero (location, op1); |
3e4093b6 RS |
9211 | |
9212 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 9213 | || code0 == FIXED_POINT_TYPE |
3e4093b6 RS |
9214 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
9215 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
ab22c1fa | 9216 | || code1 == FIXED_POINT_TYPE |
3e4093b6 | 9217 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 9218 | { |
5bed876a AH |
9219 | enum tree_code tcode0 = code0, tcode1 = code1; |
9220 | ||
3a021db2 | 9221 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
5bed876a | 9222 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
3a021db2 | 9223 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) |
5bed876a | 9224 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
3a021db2 | 9225 | |
ab22c1fa CF |
9226 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) |
9227 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
3e4093b6 RS |
9228 | resultcode = RDIV_EXPR; |
9229 | else | |
9230 | /* Although it would be tempting to shorten always here, that | |
9231 | loses on some targets, since the modulo instruction is | |
9232 | undefined if the quotient can't be represented in the | |
9233 | computation mode. We shorten only if unsigned or if | |
9234 | dividing by something we know != -1. */ | |
8df83eae | 9235 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 9236 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 9237 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
9238 | common = 1; |
9239 | } | |
9240 | break; | |
de520661 | 9241 | |
3e4093b6 | 9242 | case BIT_AND_EXPR: |
3e4093b6 RS |
9243 | case BIT_IOR_EXPR: |
9244 | case BIT_XOR_EXPR: | |
9245 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
9246 | shorten = -1; | |
9ef0c8d9 AP |
9247 | /* Allow vector types which are not floating point types. */ |
9248 | else if (code0 == VECTOR_TYPE | |
9249 | && code1 == VECTOR_TYPE | |
9250 | && !VECTOR_FLOAT_TYPE_P (type0) | |
9251 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
3e4093b6 RS |
9252 | common = 1; |
9253 | break; | |
9254 | ||
9255 | case TRUNC_MOD_EXPR: | |
9256 | case FLOOR_MOD_EXPR: | |
c9f9eb5d | 9257 | warn_for_div_by_zero (location, op1); |
de520661 | 9258 | |
5cfd5d9b AP |
9259 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
9260 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9261 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
9262 | common = 1; | |
9263 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3e4093b6 RS |
9264 | { |
9265 | /* Although it would be tempting to shorten always here, that loses | |
9266 | on some targets, since the modulo instruction is undefined if the | |
9267 | quotient can't be represented in the computation mode. We shorten | |
9268 | only if unsigned or if dividing by something we know != -1. */ | |
8df83eae | 9269 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 9270 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 9271 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
9272 | common = 1; |
9273 | } | |
9274 | break; | |
de520661 | 9275 | |
3e4093b6 RS |
9276 | case TRUTH_ANDIF_EXPR: |
9277 | case TRUTH_ORIF_EXPR: | |
9278 | case TRUTH_AND_EXPR: | |
9279 | case TRUTH_OR_EXPR: | |
9280 | case TRUTH_XOR_EXPR: | |
9281 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
ab22c1fa CF |
9282 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
9283 | || code0 == FIXED_POINT_TYPE) | |
3e4093b6 | 9284 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE |
ab22c1fa CF |
9285 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE |
9286 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
9287 | { |
9288 | /* Result of these operations is always an int, | |
9289 | but that does not mean the operands should be | |
9290 | converted to ints! */ | |
9291 | result_type = integer_type_node; | |
ba47d38d AH |
9292 | op0 = c_common_truthvalue_conversion (location, op0); |
9293 | op1 = c_common_truthvalue_conversion (location, op1); | |
3e4093b6 RS |
9294 | converted = 1; |
9295 | } | |
928c19bb JM |
9296 | if (code == TRUTH_ANDIF_EXPR) |
9297 | { | |
9298 | int_const_or_overflow = (int_operands | |
9299 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9300 | && (op0 == truthvalue_false_node | |
9301 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9302 | int_const = (int_const_or_overflow | |
9303 | && !TREE_OVERFLOW (orig_op0) | |
9304 | && (op0 == truthvalue_false_node | |
9305 | || !TREE_OVERFLOW (orig_op1))); | |
9306 | } | |
9307 | else if (code == TRUTH_ORIF_EXPR) | |
9308 | { | |
9309 | int_const_or_overflow = (int_operands | |
9310 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9311 | && (op0 == truthvalue_true_node | |
9312 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9313 | int_const = (int_const_or_overflow | |
9314 | && !TREE_OVERFLOW (orig_op0) | |
9315 | && (op0 == truthvalue_true_node | |
9316 | || !TREE_OVERFLOW (orig_op1))); | |
9317 | } | |
3e4093b6 | 9318 | break; |
eba80994 | 9319 | |
3e4093b6 RS |
9320 | /* Shift operations: result has same type as first operand; |
9321 | always convert second operand to int. | |
9322 | Also set SHORT_SHIFT if shifting rightward. */ | |
de520661 | 9323 | |
3e4093b6 | 9324 | case RSHIFT_EXPR: |
ab22c1fa CF |
9325 | if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) |
9326 | && code1 == INTEGER_TYPE) | |
3e4093b6 | 9327 | { |
928c19bb | 9328 | if (TREE_CODE (op1) == INTEGER_CST) |
b62acd60 | 9329 | { |
3e4093b6 | 9330 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
9331 | { |
9332 | int_const = false; | |
7d882b83 | 9333 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9334 | warning (0, "right shift count is negative"); |
9335 | } | |
3e4093b6 | 9336 | else |
bbb818c6 | 9337 | { |
3f75a254 | 9338 | if (!integer_zerop (op1)) |
3e4093b6 RS |
9339 | short_shift = 1; |
9340 | ||
9341 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
928c19bb JM |
9342 | { |
9343 | int_const = false; | |
7d882b83 | 9344 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9345 | warning (0, "right shift count >= width of type"); |
9346 | } | |
bbb818c6 | 9347 | } |
b62acd60 | 9348 | } |
de520661 | 9349 | |
3e4093b6 RS |
9350 | /* Use the type of the value to be shifted. */ |
9351 | result_type = type0; | |
9352 | /* Convert the shift-count to an integer, regardless of size | |
9353 | of value being shifted. */ | |
9354 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
9355 | op1 = convert (integer_type_node, op1); | |
9356 | /* Avoid converting op1 to result_type later. */ | |
9357 | converted = 1; | |
400fbf9f | 9358 | } |
3e4093b6 | 9359 | break; |
253b6b82 | 9360 | |
3e4093b6 | 9361 | case LSHIFT_EXPR: |
ab22c1fa CF |
9362 | if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) |
9363 | && code1 == INTEGER_TYPE) | |
3e4093b6 | 9364 | { |
928c19bb | 9365 | if (TREE_CODE (op1) == INTEGER_CST) |
de520661 | 9366 | { |
3e4093b6 | 9367 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
9368 | { |
9369 | int_const = false; | |
7d882b83 | 9370 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9371 | warning (0, "left shift count is negative"); |
9372 | } | |
de520661 | 9373 | |
3e4093b6 | 9374 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
928c19bb JM |
9375 | { |
9376 | int_const = false; | |
7d882b83 | 9377 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9378 | warning (0, "left shift count >= width of type"); |
9379 | } | |
94ba5069 | 9380 | } |
de520661 | 9381 | |
3e4093b6 RS |
9382 | /* Use the type of the value to be shifted. */ |
9383 | result_type = type0; | |
9384 | /* Convert the shift-count to an integer, regardless of size | |
9385 | of value being shifted. */ | |
9386 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
9387 | op1 = convert (integer_type_node, op1); | |
9388 | /* Avoid converting op1 to result_type later. */ | |
9389 | converted = 1; | |
400fbf9f | 9390 | } |
3e4093b6 | 9391 | break; |
de520661 | 9392 | |
3e4093b6 RS |
9393 | case EQ_EXPR: |
9394 | case NE_EXPR: | |
ae311566 | 9395 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) |
ba47d38d AH |
9396 | warning_at (location, |
9397 | OPT_Wfloat_equal, | |
9398 | "comparing floating point with == or != is unsafe"); | |
3e4093b6 RS |
9399 | /* Result of comparison is always int, |
9400 | but don't convert the args to int! */ | |
9401 | build_type = integer_type_node; | |
9402 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 9403 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) |
3e4093b6 | 9404 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
ab22c1fa | 9405 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) |
3e4093b6 RS |
9406 | short_compare = 1; |
9407 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
9408 | { | |
9409 | tree tt0 = TREE_TYPE (type0); | |
9410 | tree tt1 = TREE_TYPE (type1); | |
36c5e70a BE |
9411 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); |
9412 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
9413 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
9414 | ||
3e4093b6 RS |
9415 | /* Anything compares with void *. void * compares with anything. |
9416 | Otherwise, the targets must be compatible | |
9417 | and both must be object or both incomplete. */ | |
744aa42f | 9418 | if (comp_target_types (location, type0, type1)) |
10bc1b1b | 9419 | result_type = common_pointer_type (type0, type1); |
36c5e70a BE |
9420 | else if (null_pointer_constant_p (orig_op0)) |
9421 | result_type = type1; | |
9422 | else if (null_pointer_constant_p (orig_op1)) | |
9423 | result_type = type0; | |
9424 | else if (!addr_space_superset (as0, as1, &as_common)) | |
9425 | { | |
9426 | error_at (location, "comparison of pointers to " | |
9427 | "disjoint address spaces"); | |
9428 | return error_mark_node; | |
9429 | } | |
3e4093b6 | 9430 | else if (VOID_TYPE_P (tt0)) |
ee2990e7 | 9431 | { |
36c5e70a | 9432 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) |
ba47d38d | 9433 | pedwarn (location, OPT_pedantic, "ISO C forbids " |
fcf73884 | 9434 | "comparison of %<void *%> with function pointer"); |
ee2990e7 | 9435 | } |
3e4093b6 | 9436 | else if (VOID_TYPE_P (tt1)) |
e6834654 | 9437 | { |
36c5e70a | 9438 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) |
ba47d38d | 9439 | pedwarn (location, OPT_pedantic, "ISO C forbids " |
fcf73884 | 9440 | "comparison of %<void *%> with function pointer"); |
e6834654 | 9441 | } |
3e4093b6 | 9442 | else |
58393038 ZL |
9443 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
9444 | if (!objc_ok) | |
ba47d38d | 9445 | pedwarn (location, 0, |
509c9d60 | 9446 | "comparison of distinct pointer types lacks a cast"); |
e6834654 | 9447 | |
3e4093b6 | 9448 | if (result_type == NULL_TREE) |
36c5e70a BE |
9449 | { |
9450 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
9451 | result_type = build_pointer_type | |
9452 | (build_qualified_type (void_type_node, qual)); | |
9453 | } | |
e6834654 | 9454 | } |
6aa3c60d | 9455 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
690a704a BE |
9456 | { |
9457 | if (TREE_CODE (op0) == ADDR_EXPR | |
b3c6d2ea | 9458 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) |
ba47d38d AH |
9459 | warning_at (location, |
9460 | OPT_Waddress, "the address of %qD will never be NULL", | |
9461 | TREE_OPERAND (op0, 0)); | |
690a704a BE |
9462 | result_type = type0; |
9463 | } | |
6aa3c60d | 9464 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
690a704a | 9465 | { |
c22cacf3 | 9466 | if (TREE_CODE (op1) == ADDR_EXPR |
b3c6d2ea | 9467 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) |
ba47d38d AH |
9468 | warning_at (location, |
9469 | OPT_Waddress, "the address of %qD will never be NULL", | |
9470 | TREE_OPERAND (op1, 0)); | |
690a704a BE |
9471 | result_type = type1; |
9472 | } | |
3e4093b6 | 9473 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
de520661 | 9474 | { |
3e4093b6 | 9475 | result_type = type0; |
ba47d38d | 9476 | pedwarn (location, 0, "comparison between pointer and integer"); |
de520661 | 9477 | } |
3e4093b6 | 9478 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
8b6a5902 | 9479 | { |
3e4093b6 | 9480 | result_type = type1; |
ba47d38d | 9481 | pedwarn (location, 0, "comparison between pointer and integer"); |
8b6a5902 | 9482 | } |
3e4093b6 | 9483 | break; |
8b6a5902 | 9484 | |
3e4093b6 RS |
9485 | case LE_EXPR: |
9486 | case GE_EXPR: | |
9487 | case LT_EXPR: | |
9488 | case GT_EXPR: | |
9489 | build_type = integer_type_node; | |
ab22c1fa CF |
9490 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
9491 | || code0 == FIXED_POINT_TYPE) | |
9492 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
9493 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
9494 | short_compare = 1; |
9495 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
9496 | { | |
36c5e70a BE |
9497 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); |
9498 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
9499 | addr_space_t as_common; | |
9500 | ||
744aa42f | 9501 | if (comp_target_types (location, type0, type1)) |
3e4093b6 | 9502 | { |
10bc1b1b | 9503 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
9504 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
9505 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
ba47d38d | 9506 | pedwarn (location, 0, |
509c9d60 | 9507 | "comparison of complete and incomplete pointers"); |
fcf73884 | 9508 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) |
ba47d38d | 9509 | pedwarn (location, OPT_pedantic, "ISO C forbids " |
fcf73884 | 9510 | "ordered comparisons of pointers to functions"); |
3e4093b6 | 9511 | } |
36c5e70a BE |
9512 | else if (!addr_space_superset (as0, as1, &as_common)) |
9513 | { | |
9514 | error_at (location, "comparison of pointers to " | |
9515 | "disjoint address spaces"); | |
9516 | return error_mark_node; | |
9517 | } | |
3e4093b6 RS |
9518 | else |
9519 | { | |
36c5e70a BE |
9520 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
9521 | result_type = build_pointer_type | |
9522 | (build_qualified_type (void_type_node, qual)); | |
ba47d38d | 9523 | pedwarn (location, 0, |
509c9d60 | 9524 | "comparison of distinct pointer types lacks a cast"); |
3e4093b6 RS |
9525 | } |
9526 | } | |
6aa3c60d | 9527 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
3e4093b6 RS |
9528 | { |
9529 | result_type = type0; | |
fcf73884 | 9530 | if (pedantic) |
b8698a0f | 9531 | pedwarn (location, OPT_pedantic, |
fcf73884 MLI |
9532 | "ordered comparison of pointer with integer zero"); |
9533 | else if (extra_warnings) | |
ba47d38d | 9534 | warning_at (location, OPT_Wextra, |
fcf73884 | 9535 | "ordered comparison of pointer with integer zero"); |
3e4093b6 | 9536 | } |
6aa3c60d | 9537 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
3e4093b6 RS |
9538 | { |
9539 | result_type = type1; | |
b8698a0f | 9540 | pedwarn (location, OPT_pedantic, |
fcf73884 | 9541 | "ordered comparison of pointer with integer zero"); |
3e4093b6 RS |
9542 | } |
9543 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
9544 | { | |
9545 | result_type = type0; | |
ba47d38d | 9546 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
9547 | } |
9548 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
9549 | { | |
9550 | result_type = type1; | |
ba47d38d | 9551 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
9552 | } |
9553 | break; | |
64094f6a | 9554 | |
3e4093b6 | 9555 | default: |
37b2f290 | 9556 | gcc_unreachable (); |
c9fe6f9f | 9557 | } |
8f17b5c5 | 9558 | |
e57e265b PB |
9559 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
9560 | return error_mark_node; | |
9561 | ||
5bed876a AH |
9562 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
9563 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
9564 | || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0), | |
9565 | TREE_TYPE (type1)))) | |
9566 | { | |
ba47d38d | 9567 | binary_op_error (location, code, type0, type1); |
5bed876a AH |
9568 | return error_mark_node; |
9569 | } | |
9570 | ||
3e4093b6 | 9571 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
ab22c1fa | 9572 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) |
3e4093b6 RS |
9573 | && |
9574 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
ab22c1fa | 9575 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 9576 | { |
2ca862e9 JM |
9577 | bool first_complex = (code0 == COMPLEX_TYPE); |
9578 | bool second_complex = (code1 == COMPLEX_TYPE); | |
9579 | int none_complex = (!first_complex && !second_complex); | |
39b726dd | 9580 | |
3e4093b6 | 9581 | if (shorten || common || short_compare) |
3bf6bfcc JJ |
9582 | { |
9583 | result_type = c_common_type (type0, type1); | |
9584 | if (result_type == error_mark_node) | |
9585 | return error_mark_node; | |
9586 | } | |
400fbf9f | 9587 | |
2ca862e9 JM |
9588 | if (first_complex != second_complex |
9589 | && (code == PLUS_EXPR | |
9590 | || code == MINUS_EXPR | |
9591 | || code == MULT_EXPR | |
9592 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
9593 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
9594 | && flag_signed_zeros) | |
9595 | { | |
9596 | /* An operation on mixed real/complex operands must be | |
9597 | handled specially, but the language-independent code can | |
9598 | more easily optimize the plain complex arithmetic if | |
9599 | -fno-signed-zeros. */ | |
9600 | tree real_type = TREE_TYPE (result_type); | |
9601 | tree real, imag; | |
9602 | if (type0 != orig_type0 || type1 != orig_type1) | |
9603 | { | |
9604 | gcc_assert (may_need_excess_precision && common); | |
9605 | real_result_type = c_common_type (orig_type0, orig_type1); | |
9606 | } | |
9607 | if (first_complex) | |
9608 | { | |
9609 | if (TREE_TYPE (op0) != result_type) | |
9610 | op0 = convert_and_check (result_type, op0); | |
9611 | if (TREE_TYPE (op1) != real_type) | |
9612 | op1 = convert_and_check (real_type, op1); | |
9613 | } | |
9614 | else | |
9615 | { | |
9616 | if (TREE_TYPE (op0) != real_type) | |
9617 | op0 = convert_and_check (real_type, op0); | |
9618 | if (TREE_TYPE (op1) != result_type) | |
9619 | op1 = convert_and_check (result_type, op1); | |
9620 | } | |
9621 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
9622 | return error_mark_node; | |
9623 | if (first_complex) | |
9624 | { | |
9625 | op0 = c_save_expr (op0); | |
9626 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
9627 | op0, 1); | |
9628 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
9629 | op0, 1); | |
9630 | switch (code) | |
9631 | { | |
9632 | case MULT_EXPR: | |
9633 | case TRUNC_DIV_EXPR: | |
9634 | imag = build2 (resultcode, real_type, imag, op1); | |
9635 | /* Fall through. */ | |
9636 | case PLUS_EXPR: | |
9637 | case MINUS_EXPR: | |
9638 | real = build2 (resultcode, real_type, real, op1); | |
9639 | break; | |
9640 | default: | |
9641 | gcc_unreachable(); | |
9642 | } | |
9643 | } | |
9644 | else | |
9645 | { | |
9646 | op1 = c_save_expr (op1); | |
9647 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
9648 | op1, 1); | |
9649 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
9650 | op1, 1); | |
9651 | switch (code) | |
9652 | { | |
9653 | case MULT_EXPR: | |
9654 | imag = build2 (resultcode, real_type, op0, imag); | |
9655 | /* Fall through. */ | |
9656 | case PLUS_EXPR: | |
9657 | real = build2 (resultcode, real_type, op0, real); | |
9658 | break; | |
9659 | case MINUS_EXPR: | |
9660 | real = build2 (resultcode, real_type, op0, real); | |
9661 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
9662 | break; | |
9663 | default: | |
9664 | gcc_unreachable(); | |
9665 | } | |
9666 | } | |
9667 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
9668 | goto return_build_binary_op; | |
9669 | } | |
9670 | ||
3e4093b6 RS |
9671 | /* For certain operations (which identify themselves by shorten != 0) |
9672 | if both args were extended from the same smaller type, | |
9673 | do the arithmetic in that type and then extend. | |
400fbf9f | 9674 | |
3e4093b6 RS |
9675 | shorten !=0 and !=1 indicates a bitwise operation. |
9676 | For them, this optimization is safe only if | |
9677 | both args are zero-extended or both are sign-extended. | |
9678 | Otherwise, we might change the result. | |
9679 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
9680 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
400fbf9f | 9681 | |
3e4093b6 RS |
9682 | if (shorten && none_complex) |
9683 | { | |
3e4093b6 | 9684 | final_type = result_type; |
b8698a0f | 9685 | result_type = shorten_binary_op (result_type, op0, op1, |
6715192c | 9686 | shorten == -1); |
3e4093b6 | 9687 | } |
88a3dbc1 | 9688 | |
3e4093b6 | 9689 | /* Shifts can be shortened if shifting right. */ |
2f6e4e97 | 9690 | |
3e4093b6 RS |
9691 | if (short_shift) |
9692 | { | |
9693 | int unsigned_arg; | |
9694 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
88a3dbc1 | 9695 | |
3e4093b6 | 9696 | final_type = result_type; |
abe80e6d | 9697 | |
3e4093b6 | 9698 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
8df83eae | 9699 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
e9a25f70 | 9700 | |
3e4093b6 | 9701 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
be123439 | 9702 | && tree_int_cst_sgn (op1) > 0 |
3e4093b6 RS |
9703 | /* We can shorten only if the shift count is less than the |
9704 | number of bits in the smaller type size. */ | |
9705 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
9706 | /* We cannot drop an unsigned shift after sign-extension. */ | |
8df83eae | 9707 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
3e4093b6 RS |
9708 | { |
9709 | /* Do an unsigned shift if the operand was zero-extended. */ | |
9710 | result_type | |
9711 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
9712 | TREE_TYPE (arg0)); | |
9713 | /* Convert value-to-be-shifted to that type. */ | |
9714 | if (TREE_TYPE (op0) != result_type) | |
9715 | op0 = convert (result_type, op0); | |
9716 | converted = 1; | |
abe80e6d | 9717 | } |
88a3dbc1 RK |
9718 | } |
9719 | ||
3e4093b6 RS |
9720 | /* Comparison operations are shortened too but differently. |
9721 | They identify themselves by setting short_compare = 1. */ | |
56cb9733 | 9722 | |
3e4093b6 RS |
9723 | if (short_compare) |
9724 | { | |
9725 | /* Don't write &op0, etc., because that would prevent op0 | |
9726 | from being kept in a register. | |
9727 | Instead, make copies of the our local variables and | |
9728 | pass the copies by reference, then copy them back afterward. */ | |
9729 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
9730 | enum tree_code xresultcode = resultcode; | |
9731 | tree val | |
9732 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
8f17b5c5 | 9733 | |
3e4093b6 | 9734 | if (val != 0) |
c9f9eb5d AH |
9735 | { |
9736 | ret = val; | |
9737 | goto return_build_binary_op; | |
9738 | } | |
8f17b5c5 | 9739 | |
3e4093b6 RS |
9740 | op0 = xop0, op1 = xop1; |
9741 | converted = 1; | |
9742 | resultcode = xresultcode; | |
8f17b5c5 | 9743 | |
7d882b83 | 9744 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9745 | { |
9746 | bool op0_maybe_const = true; | |
9747 | bool op1_maybe_const = true; | |
9748 | tree orig_op0_folded, orig_op1_folded; | |
9749 | ||
9750 | if (in_late_binary_op) | |
9751 | { | |
9752 | orig_op0_folded = orig_op0; | |
9753 | orig_op1_folded = orig_op1; | |
9754 | } | |
9755 | else | |
9756 | { | |
9757 | /* Fold for the sake of possible warnings, as in | |
9758 | build_conditional_expr. This requires the | |
9759 | "original" values to be folded, not just op0 and | |
9760 | op1. */ | |
f5178456 | 9761 | c_inhibit_evaluation_warnings++; |
928c19bb JM |
9762 | op0 = c_fully_fold (op0, require_constant_value, |
9763 | &op0_maybe_const); | |
9764 | op1 = c_fully_fold (op1, require_constant_value, | |
9765 | &op1_maybe_const); | |
f5178456 | 9766 | c_inhibit_evaluation_warnings--; |
928c19bb JM |
9767 | orig_op0_folded = c_fully_fold (orig_op0, |
9768 | require_constant_value, | |
9769 | NULL); | |
9770 | orig_op1_folded = c_fully_fold (orig_op1, | |
9771 | require_constant_value, | |
9772 | NULL); | |
9773 | } | |
9774 | ||
9775 | if (warn_sign_compare) | |
9776 | warn_for_sign_compare (location, orig_op0_folded, | |
9777 | orig_op1_folded, op0, op1, | |
9778 | result_type, resultcode); | |
9779 | if (!in_late_binary_op) | |
9780 | { | |
9781 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
e5a94231 | 9782 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); |
928c19bb | 9783 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) |
e5a94231 | 9784 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 9785 | } |
3e4093b6 | 9786 | } |
2ad1815d | 9787 | } |
64094f6a | 9788 | } |
64094f6a | 9789 | |
3e4093b6 RS |
9790 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
9791 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
9792 | Then the expression will be built. | |
9793 | It will be given type FINAL_TYPE if that is nonzero; | |
9794 | otherwise, it will be given type RESULT_TYPE. */ | |
400fbf9f | 9795 | |
3e4093b6 RS |
9796 | if (!result_type) |
9797 | { | |
ba47d38d | 9798 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); |
3e4093b6 RS |
9799 | return error_mark_node; |
9800 | } | |
400fbf9f | 9801 | |
3f75a254 | 9802 | if (!converted) |
3e4093b6 RS |
9803 | { |
9804 | if (TREE_TYPE (op0) != result_type) | |
0f57299d | 9805 | op0 = convert_and_check (result_type, op0); |
3e4093b6 | 9806 | if (TREE_TYPE (op1) != result_type) |
0f57299d | 9807 | op1 = convert_and_check (result_type, op1); |
d97c6333 JW |
9808 | |
9809 | /* This can happen if one operand has a vector type, and the other | |
9810 | has a different type. */ | |
9811 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
9812 | return error_mark_node; | |
3e4093b6 | 9813 | } |
400fbf9f | 9814 | |
3e4093b6 | 9815 | if (build_type == NULL_TREE) |
8ce94e44 JM |
9816 | { |
9817 | build_type = result_type; | |
9818 | if (type0 != orig_type0 || type1 != orig_type1) | |
9819 | { | |
9820 | gcc_assert (may_need_excess_precision && common); | |
9821 | real_result_type = c_common_type (orig_type0, orig_type1); | |
9822 | } | |
9823 | } | |
400fbf9f | 9824 | |
c9f9eb5d | 9825 | /* Treat expressions in initializers specially as they can't trap. */ |
928c19bb JM |
9826 | if (int_const_or_overflow) |
9827 | ret = (require_constant_value | |
db3927fb AH |
9828 | ? fold_build2_initializer_loc (location, resultcode, build_type, |
9829 | op0, op1) | |
9830 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
928c19bb JM |
9831 | else |
9832 | ret = build2 (resultcode, build_type, op0, op1); | |
c9f9eb5d AH |
9833 | if (final_type != 0) |
9834 | ret = convert (final_type, ret); | |
9835 | ||
9836 | return_build_binary_op: | |
9837 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
9838 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) |
9839 | ret = (int_operands | |
9840 | ? note_integer_operands (ret) | |
9841 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
9842 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
9843 | && !in_late_binary_op) | |
9844 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
9845 | if (real_result_type) |
9846 | ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret); | |
c9f9eb5d AH |
9847 | protected_set_expr_location (ret, location); |
9848 | return ret; | |
400fbf9f | 9849 | } |
85498824 JM |
9850 | |
9851 | ||
9852 | /* Convert EXPR to be a truth-value, validating its type for this | |
ba47d38d | 9853 | purpose. LOCATION is the source location for the expression. */ |
85498824 JM |
9854 | |
9855 | tree | |
ba47d38d | 9856 | c_objc_common_truthvalue_conversion (location_t location, tree expr) |
85498824 | 9857 | { |
928c19bb JM |
9858 | bool int_const, int_operands; |
9859 | ||
85498824 JM |
9860 | switch (TREE_CODE (TREE_TYPE (expr))) |
9861 | { | |
9862 | case ARRAY_TYPE: | |
ba47d38d | 9863 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); |
85498824 JM |
9864 | return error_mark_node; |
9865 | ||
9866 | case RECORD_TYPE: | |
ba47d38d | 9867 | error_at (location, "used struct type value where scalar is required"); |
85498824 JM |
9868 | return error_mark_node; |
9869 | ||
9870 | case UNION_TYPE: | |
ba47d38d | 9871 | error_at (location, "used union type value where scalar is required"); |
85498824 JM |
9872 | return error_mark_node; |
9873 | ||
46bdb9cf JM |
9874 | case FUNCTION_TYPE: |
9875 | gcc_unreachable (); | |
9876 | ||
85498824 JM |
9877 | default: |
9878 | break; | |
9879 | } | |
9880 | ||
928c19bb JM |
9881 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); |
9882 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
4d84fe7c JM |
9883 | if (int_operands) |
9884 | expr = remove_c_maybe_const_expr (expr); | |
928c19bb | 9885 | |
85498824 JM |
9886 | /* ??? Should we also give an error for void and vectors rather than |
9887 | leaving those to give errors later? */ | |
928c19bb JM |
9888 | expr = c_common_truthvalue_conversion (location, expr); |
9889 | ||
9890 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
9891 | { | |
9892 | if (TREE_OVERFLOW (expr)) | |
9893 | return expr; | |
9894 | else | |
9895 | return note_integer_operands (expr); | |
9896 | } | |
9897 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
9898 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
9899 | return expr; | |
85498824 | 9900 | } |
73f397d4 JM |
9901 | \f |
9902 | ||
9903 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
9904 | required. */ | |
9905 | ||
9906 | tree | |
51eed280 | 9907 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) |
73f397d4 JM |
9908 | { |
9909 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
9910 | { | |
9911 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
9912 | /* Executing a compound literal inside a function reinitializes | |
9913 | it. */ | |
9914 | if (!TREE_STATIC (decl)) | |
9915 | *se = true; | |
9916 | return decl; | |
9917 | } | |
9918 | else | |
9919 | return expr; | |
9920 | } | |
953ff289 | 9921 | \f |
c0220ea4 | 9922 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
953ff289 DN |
9923 | |
9924 | tree | |
9925 | c_begin_omp_parallel (void) | |
9926 | { | |
9927 | tree block; | |
9928 | ||
9929 | keep_next_level (); | |
9930 | block = c_begin_compound_stmt (true); | |
9931 | ||
9932 | return block; | |
9933 | } | |
9934 | ||
c2255bc4 AH |
9935 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound |
9936 | statement. LOC is the location of the OMP_PARALLEL. */ | |
a68ab351 | 9937 | |
953ff289 | 9938 | tree |
c2255bc4 | 9939 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) |
953ff289 DN |
9940 | { |
9941 | tree stmt; | |
9942 | ||
c2255bc4 | 9943 | block = c_end_compound_stmt (loc, block, true); |
953ff289 DN |
9944 | |
9945 | stmt = make_node (OMP_PARALLEL); | |
9946 | TREE_TYPE (stmt) = void_type_node; | |
9947 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
9948 | OMP_PARALLEL_BODY (stmt) = block; | |
c2255bc4 | 9949 | SET_EXPR_LOCATION (stmt, loc); |
953ff289 DN |
9950 | |
9951 | return add_stmt (stmt); | |
9952 | } | |
9953 | ||
a68ab351 JJ |
9954 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
9955 | ||
9956 | tree | |
9957 | c_begin_omp_task (void) | |
9958 | { | |
9959 | tree block; | |
9960 | ||
9961 | keep_next_level (); | |
9962 | block = c_begin_compound_stmt (true); | |
9963 | ||
9964 | return block; | |
9965 | } | |
9966 | ||
c2255bc4 AH |
9967 | /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound |
9968 | statement. LOC is the location of the #pragma. */ | |
a68ab351 JJ |
9969 | |
9970 | tree | |
c2255bc4 | 9971 | c_finish_omp_task (location_t loc, tree clauses, tree block) |
a68ab351 JJ |
9972 | { |
9973 | tree stmt; | |
9974 | ||
c2255bc4 | 9975 | block = c_end_compound_stmt (loc, block, true); |
a68ab351 JJ |
9976 | |
9977 | stmt = make_node (OMP_TASK); | |
9978 | TREE_TYPE (stmt) = void_type_node; | |
9979 | OMP_TASK_CLAUSES (stmt) = clauses; | |
9980 | OMP_TASK_BODY (stmt) = block; | |
c2255bc4 | 9981 | SET_EXPR_LOCATION (stmt, loc); |
a68ab351 JJ |
9982 | |
9983 | return add_stmt (stmt); | |
9984 | } | |
9985 | ||
953ff289 DN |
9986 | /* For all elements of CLAUSES, validate them vs OpenMP constraints. |
9987 | Remove any elements from the list that are invalid. */ | |
9988 | ||
9989 | tree | |
9990 | c_finish_omp_clauses (tree clauses) | |
9991 | { | |
9992 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
9993 | tree c, t, *pc = &clauses; | |
9994 | const char *name; | |
9995 | ||
9996 | bitmap_obstack_initialize (NULL); | |
9997 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
9998 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
9999 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
10000 | ||
10001 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
10002 | { | |
10003 | bool remove = false; | |
10004 | bool need_complete = false; | |
10005 | bool need_implicitly_determined = false; | |
10006 | ||
aaf46ef9 | 10007 | switch (OMP_CLAUSE_CODE (c)) |
953ff289 DN |
10008 | { |
10009 | case OMP_CLAUSE_SHARED: | |
10010 | name = "shared"; | |
10011 | need_implicitly_determined = true; | |
10012 | goto check_dup_generic; | |
10013 | ||
10014 | case OMP_CLAUSE_PRIVATE: | |
10015 | name = "private"; | |
10016 | need_complete = true; | |
10017 | need_implicitly_determined = true; | |
10018 | goto check_dup_generic; | |
10019 | ||
10020 | case OMP_CLAUSE_REDUCTION: | |
10021 | name = "reduction"; | |
10022 | need_implicitly_determined = true; | |
10023 | t = OMP_CLAUSE_DECL (c); | |
10024 | if (AGGREGATE_TYPE_P (TREE_TYPE (t)) | |
10025 | || POINTER_TYPE_P (TREE_TYPE (t))) | |
10026 | { | |
c2255bc4 AH |
10027 | error_at (OMP_CLAUSE_LOCATION (c), |
10028 | "%qE has invalid type for %<reduction%>", t); | |
953ff289 DN |
10029 | remove = true; |
10030 | } | |
10031 | else if (FLOAT_TYPE_P (TREE_TYPE (t))) | |
10032 | { | |
10033 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
10034 | const char *r_name = NULL; | |
10035 | ||
10036 | switch (r_code) | |
10037 | { | |
10038 | case PLUS_EXPR: | |
10039 | case MULT_EXPR: | |
10040 | case MINUS_EXPR: | |
10041 | break; | |
10042 | case BIT_AND_EXPR: | |
10043 | r_name = "&"; | |
10044 | break; | |
10045 | case BIT_XOR_EXPR: | |
10046 | r_name = "^"; | |
10047 | break; | |
10048 | case BIT_IOR_EXPR: | |
10049 | r_name = "|"; | |
10050 | break; | |
10051 | case TRUTH_ANDIF_EXPR: | |
10052 | r_name = "&&"; | |
10053 | break; | |
10054 | case TRUTH_ORIF_EXPR: | |
10055 | r_name = "||"; | |
10056 | break; | |
10057 | default: | |
10058 | gcc_unreachable (); | |
10059 | } | |
10060 | if (r_name) | |
10061 | { | |
c2255bc4 AH |
10062 | error_at (OMP_CLAUSE_LOCATION (c), |
10063 | "%qE has invalid type for %<reduction(%s)%>", | |
10064 | t, r_name); | |
953ff289 DN |
10065 | remove = true; |
10066 | } | |
10067 | } | |
10068 | goto check_dup_generic; | |
10069 | ||
10070 | case OMP_CLAUSE_COPYPRIVATE: | |
10071 | name = "copyprivate"; | |
10072 | goto check_dup_generic; | |
10073 | ||
10074 | case OMP_CLAUSE_COPYIN: | |
10075 | name = "copyin"; | |
10076 | t = OMP_CLAUSE_DECL (c); | |
10077 | if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) | |
10078 | { | |
c2255bc4 AH |
10079 | error_at (OMP_CLAUSE_LOCATION (c), |
10080 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
953ff289 DN |
10081 | remove = true; |
10082 | } | |
10083 | goto check_dup_generic; | |
10084 | ||
10085 | check_dup_generic: | |
10086 | t = OMP_CLAUSE_DECL (c); | |
10087 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10088 | { | |
c2255bc4 AH |
10089 | error_at (OMP_CLAUSE_LOCATION (c), |
10090 | "%qE is not a variable in clause %qs", t, name); | |
953ff289 DN |
10091 | remove = true; |
10092 | } | |
10093 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10094 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
10095 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10096 | { | |
c2255bc4 AH |
10097 | error_at (OMP_CLAUSE_LOCATION (c), |
10098 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10099 | remove = true; |
10100 | } | |
10101 | else | |
10102 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
10103 | break; | |
10104 | ||
10105 | case OMP_CLAUSE_FIRSTPRIVATE: | |
10106 | name = "firstprivate"; | |
10107 | t = OMP_CLAUSE_DECL (c); | |
10108 | need_complete = true; | |
10109 | need_implicitly_determined = true; | |
10110 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10111 | { | |
c2255bc4 AH |
10112 | error_at (OMP_CLAUSE_LOCATION (c), |
10113 | "%qE is not a variable in clause %<firstprivate%>", t); | |
953ff289 DN |
10114 | remove = true; |
10115 | } | |
10116 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10117 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
10118 | { | |
c2255bc4 AH |
10119 | error_at (OMP_CLAUSE_LOCATION (c), |
10120 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10121 | remove = true; |
10122 | } | |
10123 | else | |
10124 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
10125 | break; | |
10126 | ||
10127 | case OMP_CLAUSE_LASTPRIVATE: | |
10128 | name = "lastprivate"; | |
10129 | t = OMP_CLAUSE_DECL (c); | |
10130 | need_complete = true; | |
10131 | need_implicitly_determined = true; | |
10132 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10133 | { | |
c2255bc4 AH |
10134 | error_at (OMP_CLAUSE_LOCATION (c), |
10135 | "%qE is not a variable in clause %<lastprivate%>", t); | |
953ff289 DN |
10136 | remove = true; |
10137 | } | |
10138 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10139 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10140 | { | |
c2255bc4 AH |
10141 | error_at (OMP_CLAUSE_LOCATION (c), |
10142 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10143 | remove = true; |
10144 | } | |
10145 | else | |
10146 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
10147 | break; | |
10148 | ||
10149 | case OMP_CLAUSE_IF: | |
10150 | case OMP_CLAUSE_NUM_THREADS: | |
10151 | case OMP_CLAUSE_SCHEDULE: | |
10152 | case OMP_CLAUSE_NOWAIT: | |
10153 | case OMP_CLAUSE_ORDERED: | |
10154 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 JJ |
10155 | case OMP_CLAUSE_UNTIED: |
10156 | case OMP_CLAUSE_COLLAPSE: | |
953ff289 DN |
10157 | pc = &OMP_CLAUSE_CHAIN (c); |
10158 | continue; | |
10159 | ||
10160 | default: | |
10161 | gcc_unreachable (); | |
10162 | } | |
10163 | ||
10164 | if (!remove) | |
10165 | { | |
10166 | t = OMP_CLAUSE_DECL (c); | |
10167 | ||
10168 | if (need_complete) | |
10169 | { | |
10170 | t = require_complete_type (t); | |
10171 | if (t == error_mark_node) | |
10172 | remove = true; | |
10173 | } | |
10174 | ||
10175 | if (need_implicitly_determined) | |
10176 | { | |
10177 | const char *share_name = NULL; | |
10178 | ||
10179 | if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
10180 | share_name = "threadprivate"; | |
10181 | else switch (c_omp_predetermined_sharing (t)) | |
10182 | { | |
10183 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
10184 | break; | |
10185 | case OMP_CLAUSE_DEFAULT_SHARED: | |
10186 | share_name = "shared"; | |
10187 | break; | |
10188 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
10189 | share_name = "private"; | |
10190 | break; | |
10191 | default: | |
10192 | gcc_unreachable (); | |
10193 | } | |
10194 | if (share_name) | |
10195 | { | |
c2255bc4 AH |
10196 | error_at (OMP_CLAUSE_LOCATION (c), |
10197 | "%qE is predetermined %qs for %qs", | |
10198 | t, share_name, name); | |
953ff289 DN |
10199 | remove = true; |
10200 | } | |
10201 | } | |
10202 | } | |
10203 | ||
10204 | if (remove) | |
10205 | *pc = OMP_CLAUSE_CHAIN (c); | |
10206 | else | |
10207 | pc = &OMP_CLAUSE_CHAIN (c); | |
10208 | } | |
10209 | ||
10210 | bitmap_obstack_release (NULL); | |
10211 | return clauses; | |
10212 | } | |
9ae165a0 DG |
10213 | |
10214 | /* Make a variant type in the proper way for C/C++, propagating qualifiers | |
10215 | down to the element type of an array. */ | |
10216 | ||
10217 | tree | |
10218 | c_build_qualified_type (tree type, int type_quals) | |
10219 | { | |
10220 | if (type == error_mark_node) | |
10221 | return type; | |
10222 | ||
10223 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10224 | { | |
10225 | tree t; | |
10226 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
10227 | type_quals); | |
10228 | ||
10229 | /* See if we already have an identically qualified type. */ | |
10230 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
10231 | { | |
10232 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
10233 | && TYPE_NAME (t) == TYPE_NAME (type) | |
10234 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
10235 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
10236 | TYPE_ATTRIBUTES (type))) | |
10237 | break; | |
10238 | } | |
10239 | if (!t) | |
10240 | { | |
10241 | tree domain = TYPE_DOMAIN (type); | |
10242 | ||
10243 | t = build_variant_type_copy (type); | |
10244 | TREE_TYPE (t) = element_type; | |
10245 | ||
10246 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
10247 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
10248 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
10249 | else if (TYPE_CANONICAL (element_type) != element_type | |
10250 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
10251 | { | |
b8698a0f | 10252 | tree unqualified_canon |
9ae165a0 | 10253 | = build_array_type (TYPE_CANONICAL (element_type), |
b8698a0f | 10254 | domain? TYPE_CANONICAL (domain) |
9ae165a0 | 10255 | : NULL_TREE); |
b8698a0f | 10256 | TYPE_CANONICAL (t) |
9ae165a0 DG |
10257 | = c_build_qualified_type (unqualified_canon, type_quals); |
10258 | } | |
10259 | else | |
10260 | TYPE_CANONICAL (t) = t; | |
10261 | } | |
10262 | return t; | |
10263 | } | |
10264 | ||
10265 | /* A restrict-qualified pointer type must be a pointer to object or | |
10266 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
10267 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
10268 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
10269 | && (!POINTER_TYPE_P (type) | |
10270 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
10271 | { | |
10272 | error ("invalid use of %<restrict%>"); | |
10273 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
10274 | } | |
10275 | ||
10276 | return build_qualified_type (type, type_quals); | |
10277 | } | |
72b5577d ILT |
10278 | |
10279 | /* Build a VA_ARG_EXPR for the C parser. */ | |
10280 | ||
10281 | tree | |
c2255bc4 | 10282 | c_build_va_arg (location_t loc, tree expr, tree type) |
72b5577d ILT |
10283 | { |
10284 | if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) | |
10285 | warning_at (loc, OPT_Wc___compat, | |
10286 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
c2255bc4 | 10287 | return build_va_arg (loc, expr, type); |
72b5577d | 10288 | } |