]>
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)); | |
509c9d60 | 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)); | |
509c9d60 | 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 | |
1625 | || TYPE_TRANSPARENT_UNION (a1)) | |
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 | |
1646 | || TYPE_TRANSPARENT_UNION (a2)) | |
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 | |
c9f9eb5d | 2110 | build_indirect_ref (location_t loc, tree ptr, const char *errorstring) |
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) |
c9f9eb5d AH |
2168 | error_at (loc, |
2169 | "invalid type argument of %qs (have %qT)", errorstring, type); | |
400fbf9f JW |
2170 | return error_mark_node; |
2171 | } | |
2172 | ||
2173 | /* This handles expressions of the form "a[i]", which denotes | |
2174 | an array reference. | |
2175 | ||
2176 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2177 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2178 | This avoids forcing the array out of registers, and can work on | |
2179 | arrays that are not lvalues (for example, members of structures returned | |
6a3799eb AH |
2180 | by functions). |
2181 | ||
2182 | LOC is the location to use for the returned expression. */ | |
400fbf9f JW |
2183 | |
2184 | tree | |
c2255bc4 | 2185 | build_array_ref (location_t loc, tree array, tree index) |
400fbf9f | 2186 | { |
6a3799eb | 2187 | tree ret; |
a4ab7973 | 2188 | bool swapped = false; |
400fbf9f JW |
2189 | if (TREE_TYPE (array) == error_mark_node |
2190 | || TREE_TYPE (index) == error_mark_node) | |
2191 | return error_mark_node; | |
2192 | ||
a4ab7973 JM |
2193 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE |
2194 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE) | |
400fbf9f | 2195 | { |
a4ab7973 JM |
2196 | tree temp; |
2197 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE | |
2198 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
fdeefd49 | 2199 | { |
a63068b6 | 2200 | error_at (loc, "subscripted value is neither array nor pointer"); |
fdeefd49 RS |
2201 | return error_mark_node; |
2202 | } | |
a4ab7973 JM |
2203 | temp = array; |
2204 | array = index; | |
2205 | index = temp; | |
2206 | swapped = true; | |
2207 | } | |
2208 | ||
2209 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2210 | { | |
a63068b6 | 2211 | error_at (loc, "array subscript is not an integer"); |
a4ab7973 JM |
2212 | return error_mark_node; |
2213 | } | |
2214 | ||
2215 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2216 | { | |
a63068b6 | 2217 | error_at (loc, "subscripted value is pointer to function"); |
a4ab7973 JM |
2218 | return error_mark_node; |
2219 | } | |
2220 | ||
ff6b6641 GDR |
2221 | /* ??? Existing practice has been to warn only when the char |
2222 | index is syntactically the index, not for char[array]. */ | |
2223 | if (!swapped) | |
2224 | warn_array_subscript_with_type_char (index); | |
a4ab7973 JM |
2225 | |
2226 | /* Apply default promotions *after* noticing character types. */ | |
2227 | index = default_conversion (index); | |
2228 | ||
2229 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
2230 | ||
2231 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2232 | { | |
e4d35515 | 2233 | tree rval, type; |
fdeefd49 | 2234 | |
400fbf9f JW |
2235 | /* An array that is indexed by a non-constant |
2236 | cannot be stored in a register; we must be able to do | |
2237 | address arithmetic on its address. | |
2238 | Likewise an array of elements of variable size. */ | |
2239 | if (TREE_CODE (index) != INTEGER_CST | |
d0f062fb | 2240 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) |
400fbf9f JW |
2241 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) |
2242 | { | |
dffd7eb6 | 2243 | if (!c_mark_addressable (array)) |
400fbf9f JW |
2244 | return error_mark_node; |
2245 | } | |
e6d52559 JW |
2246 | /* An array that is indexed by a constant value which is not within |
2247 | the array bounds cannot be stored in a register either; because we | |
2248 | would get a crash in store_bit_field/extract_bit_field when trying | |
2249 | to access a non-existent part of the register. */ | |
2250 | if (TREE_CODE (index) == INTEGER_CST | |
eb34af89 | 2251 | && TYPE_DOMAIN (TREE_TYPE (array)) |
3f75a254 | 2252 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) |
e6d52559 | 2253 | { |
dffd7eb6 | 2254 | if (!c_mark_addressable (array)) |
e6d52559 JW |
2255 | return error_mark_node; |
2256 | } | |
400fbf9f | 2257 | |
400fbf9f JW |
2258 | if (pedantic) |
2259 | { | |
2260 | tree foo = array; | |
2261 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2262 | foo = TREE_OPERAND (foo, 0); | |
5baeaac0 | 2263 | if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) |
a63068b6 | 2264 | pedwarn (loc, OPT_pedantic, |
fcf73884 | 2265 | "ISO C forbids subscripting %<register%> array"); |
3f75a254 | 2266 | else if (!flag_isoc99 && !lvalue_p (foo)) |
a63068b6 | 2267 | pedwarn (loc, OPT_pedantic, |
fcf73884 | 2268 | "ISO C90 forbids subscripting non-lvalue array"); |
400fbf9f JW |
2269 | } |
2270 | ||
46df2823 | 2271 | type = TREE_TYPE (TREE_TYPE (array)); |
53fb4de3 | 2272 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); |
400fbf9f | 2273 | /* Array ref is const/volatile if the array elements are |
c22cacf3 | 2274 | or if the array is. */ |
400fbf9f JW |
2275 | TREE_READONLY (rval) |
2276 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2277 | | TREE_READONLY (array)); | |
2278 | TREE_SIDE_EFFECTS (rval) | |
2279 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2280 | | TREE_SIDE_EFFECTS (array)); | |
2281 | TREE_THIS_VOLATILE (rval) | |
2282 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2283 | /* This was added by rms on 16 Nov 91. | |
2f6e4e97 | 2284 | It fixes vol struct foo *a; a->elts[1] |
400fbf9f JW |
2285 | in an inline function. |
2286 | Hope it doesn't break something else. */ | |
2287 | | TREE_THIS_VOLATILE (array)); | |
928c19bb | 2288 | ret = require_complete_type (rval); |
6a3799eb AH |
2289 | protected_set_expr_location (ret, loc); |
2290 | return ret; | |
400fbf9f | 2291 | } |
a4ab7973 JM |
2292 | else |
2293 | { | |
2294 | tree ar = default_conversion (array); | |
400fbf9f | 2295 | |
a4ab7973 JM |
2296 | if (ar == error_mark_node) |
2297 | return ar; | |
400fbf9f | 2298 | |
a4ab7973 JM |
2299 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); |
2300 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
400fbf9f | 2301 | |
ba47d38d | 2302 | return build_indirect_ref |
c9f9eb5d AH |
2303 | (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0), |
2304 | "array indexing"); | |
a4ab7973 | 2305 | } |
400fbf9f JW |
2306 | } |
2307 | \f | |
7e585d16 | 2308 | /* Build an external reference to identifier ID. FUN indicates |
766beb40 | 2309 | whether this will be used for a function call. LOC is the source |
6866c6e8 ILT |
2310 | location of the identifier. This sets *TYPE to the type of the |
2311 | identifier, which is not the same as the type of the returned value | |
2312 | for CONST_DECLs defined as enum constants. If the type of the | |
2313 | identifier is not available, *TYPE is set to NULL. */ | |
7e585d16 | 2314 | tree |
c2255bc4 | 2315 | build_external_ref (location_t loc, tree id, int fun, tree *type) |
7e585d16 ZW |
2316 | { |
2317 | tree ref; | |
2318 | tree decl = lookup_name (id); | |
16b34ad6 ZL |
2319 | |
2320 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2321 | whatever lookup_name() found. */ | |
2322 | decl = objc_lookup_ivar (decl, id); | |
7e585d16 | 2323 | |
6866c6e8 | 2324 | *type = NULL; |
339a28b9 | 2325 | if (decl && decl != error_mark_node) |
6866c6e8 ILT |
2326 | { |
2327 | ref = decl; | |
2328 | *type = TREE_TYPE (ref); | |
2329 | } | |
339a28b9 ZW |
2330 | else if (fun) |
2331 | /* Implicit function declaration. */ | |
c2255bc4 | 2332 | ref = implicitly_declare (loc, id); |
339a28b9 ZW |
2333 | else if (decl == error_mark_node) |
2334 | /* Don't complain about something that's already been | |
2335 | complained about. */ | |
2336 | return error_mark_node; | |
2337 | else | |
2338 | { | |
c2255bc4 | 2339 | undeclared_variable (loc, id); |
339a28b9 ZW |
2340 | return error_mark_node; |
2341 | } | |
7e585d16 ZW |
2342 | |
2343 | if (TREE_TYPE (ref) == error_mark_node) | |
2344 | return error_mark_node; | |
2345 | ||
339a28b9 | 2346 | if (TREE_DEPRECATED (ref)) |
9b86d6bb | 2347 | warn_deprecated_use (ref, NULL_TREE); |
339a28b9 | 2348 | |
ad960f56 MLI |
2349 | /* Recursive call does not count as usage. */ |
2350 | if (ref != current_function_decl) | |
2351 | { | |
ad960f56 MLI |
2352 | TREE_USED (ref) = 1; |
2353 | } | |
7e585d16 | 2354 | |
bc4b653b JM |
2355 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) |
2356 | { | |
2357 | if (!in_sizeof && !in_typeof) | |
2358 | C_DECL_USED (ref) = 1; | |
2359 | else if (DECL_INITIAL (ref) == 0 | |
2360 | && DECL_EXTERNAL (ref) | |
2361 | && !TREE_PUBLIC (ref)) | |
2362 | record_maybe_used_decl (ref); | |
2363 | } | |
2364 | ||
7e585d16 ZW |
2365 | if (TREE_CODE (ref) == CONST_DECL) |
2366 | { | |
6193b8b7 | 2367 | used_types_insert (TREE_TYPE (ref)); |
24b97832 ILT |
2368 | |
2369 | if (warn_cxx_compat | |
2370 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2371 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2372 | { | |
2373 | warning_at (loc, OPT_Wc___compat, | |
2374 | ("enum constant defined in struct or union " | |
2375 | "is not visible in C++")); | |
2376 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2377 | } | |
2378 | ||
7e585d16 ZW |
2379 | ref = DECL_INITIAL (ref); |
2380 | TREE_CONSTANT (ref) = 1; | |
2381 | } | |
6a29edea | 2382 | else if (current_function_decl != 0 |
4b1e44be | 2383 | && !DECL_FILE_SCOPE_P (current_function_decl) |
6a29edea EB |
2384 | && (TREE_CODE (ref) == VAR_DECL |
2385 | || TREE_CODE (ref) == PARM_DECL | |
2386 | || TREE_CODE (ref) == FUNCTION_DECL)) | |
2387 | { | |
2388 | tree context = decl_function_context (ref); | |
2f6e4e97 | 2389 | |
6a29edea EB |
2390 | if (context != 0 && context != current_function_decl) |
2391 | DECL_NONLOCAL (ref) = 1; | |
2392 | } | |
71113fcd GK |
2393 | /* C99 6.7.4p3: An inline definition of a function with external |
2394 | linkage ... shall not contain a reference to an identifier with | |
2395 | internal linkage. */ | |
2396 | else if (current_function_decl != 0 | |
2397 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2398 | && DECL_EXTERNAL (current_function_decl) | |
2399 | && VAR_OR_FUNCTION_DECL_P (ref) | |
2400 | && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref)) | |
1033ffa8 JJ |
2401 | && ! TREE_PUBLIC (ref) |
2402 | && DECL_CONTEXT (ref) != current_function_decl) | |
991d6621 JM |
2403 | record_inline_static (loc, current_function_decl, ref, |
2404 | csi_internal); | |
7e585d16 ZW |
2405 | |
2406 | return ref; | |
2407 | } | |
2408 | ||
bc4b653b JM |
2409 | /* Record details of decls possibly used inside sizeof or typeof. */ |
2410 | struct maybe_used_decl | |
2411 | { | |
2412 | /* The decl. */ | |
2413 | tree decl; | |
2414 | /* The level seen at (in_sizeof + in_typeof). */ | |
2415 | int level; | |
2416 | /* The next one at this level or above, or NULL. */ | |
2417 | struct maybe_used_decl *next; | |
2418 | }; | |
2419 | ||
2420 | static struct maybe_used_decl *maybe_used_decls; | |
2421 | ||
2422 | /* Record that DECL, an undefined static function reference seen | |
2423 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2424 | a VLA type or the operand of typeof is a variably modified | |
2425 | type. */ | |
2426 | ||
4e2fb7de | 2427 | static void |
bc4b653b JM |
2428 | record_maybe_used_decl (tree decl) |
2429 | { | |
2430 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2431 | t->decl = decl; | |
2432 | t->level = in_sizeof + in_typeof; | |
2433 | t->next = maybe_used_decls; | |
2434 | maybe_used_decls = t; | |
2435 | } | |
2436 | ||
2437 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2438 | USED is false, just discard them. If it is true, mark them used | |
2439 | (if no longer inside sizeof or typeof) or move them to the next | |
2440 | level up (if still inside sizeof or typeof). */ | |
2441 | ||
2442 | void | |
2443 | pop_maybe_used (bool used) | |
2444 | { | |
2445 | struct maybe_used_decl *p = maybe_used_decls; | |
2446 | int cur_level = in_sizeof + in_typeof; | |
2447 | while (p && p->level > cur_level) | |
2448 | { | |
2449 | if (used) | |
2450 | { | |
2451 | if (cur_level == 0) | |
2452 | C_DECL_USED (p->decl) = 1; | |
2453 | else | |
2454 | p->level = cur_level; | |
2455 | } | |
2456 | p = p->next; | |
2457 | } | |
2458 | if (!used || cur_level == 0) | |
2459 | maybe_used_decls = p; | |
2460 | } | |
2461 | ||
2462 | /* Return the result of sizeof applied to EXPR. */ | |
2463 | ||
2464 | struct c_expr | |
c2255bc4 | 2465 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) |
bc4b653b JM |
2466 | { |
2467 | struct c_expr ret; | |
ad97f4be JM |
2468 | if (expr.value == error_mark_node) |
2469 | { | |
2470 | ret.value = error_mark_node; | |
2471 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 2472 | ret.original_type = NULL; |
ad97f4be JM |
2473 | pop_maybe_used (false); |
2474 | } | |
2475 | else | |
2476 | { | |
928c19bb JM |
2477 | bool expr_const_operands = true; |
2478 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, | |
2479 | &expr_const_operands); | |
c2255bc4 | 2480 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); |
ad97f4be | 2481 | ret.original_code = ERROR_MARK; |
6866c6e8 | 2482 | ret.original_type = NULL; |
928c19bb | 2483 | if (c_vla_type_p (TREE_TYPE (folded_expr))) |
52ffd86e MS |
2484 | { |
2485 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
928c19bb JM |
2486 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2487 | folded_expr, ret.value); | |
2488 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
c2255bc4 | 2489 | SET_EXPR_LOCATION (ret.value, loc); |
52ffd86e | 2490 | } |
928c19bb | 2491 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); |
ad97f4be | 2492 | } |
bc4b653b JM |
2493 | return ret; |
2494 | } | |
2495 | ||
2496 | /* Return the result of sizeof applied to T, a structure for the type | |
c2255bc4 AH |
2497 | name passed to sizeof (rather than the type itself). LOC is the |
2498 | location of the original expression. */ | |
bc4b653b JM |
2499 | |
2500 | struct c_expr | |
c2255bc4 | 2501 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) |
bc4b653b JM |
2502 | { |
2503 | tree type; | |
2504 | struct c_expr ret; | |
928c19bb JM |
2505 | tree type_expr = NULL_TREE; |
2506 | bool type_expr_const = true; | |
2507 | type = groktypename (t, &type_expr, &type_expr_const); | |
c2255bc4 | 2508 | ret.value = c_sizeof (loc, type); |
bc4b653b | 2509 | ret.original_code = ERROR_MARK; |
6866c6e8 | 2510 | ret.original_type = NULL; |
24070fcb JM |
2511 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) |
2512 | && c_vla_type_p (type)) | |
928c19bb | 2513 | { |
24070fcb JM |
2514 | /* If the type is a [*] array, it is a VLA but is represented as |
2515 | having a size of zero. In such a case we must ensure that | |
2516 | the result of sizeof does not get folded to a constant by | |
2517 | c_fully_fold, because if the size is evaluated the result is | |
2518 | not constant and so constraints on zero or negative size | |
2519 | arrays must not be applied when this sizeof call is inside | |
2520 | another array declarator. */ | |
2521 | if (!type_expr) | |
2522 | type_expr = integer_zero_node; | |
928c19bb JM |
2523 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), |
2524 | type_expr, ret.value); | |
2525 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2526 | } | |
16464cc1 VR |
2527 | pop_maybe_used (type != error_mark_node |
2528 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
bc4b653b JM |
2529 | return ret; |
2530 | } | |
2531 | ||
400fbf9f | 2532 | /* Build a function call to function FUNCTION with parameters PARAMS. |
c2255bc4 | 2533 | The function call is at LOC. |
400fbf9f JW |
2534 | PARAMS is a list--a chain of TREE_LIST nodes--in which the |
2535 | TREE_VALUE of each node is a parameter-expression. | |
2536 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2537 | ||
2538 | tree | |
c2255bc4 | 2539 | build_function_call (location_t loc, tree function, tree params) |
bbbbb16a ILT |
2540 | { |
2541 | VEC(tree,gc) *vec; | |
2542 | tree ret; | |
2543 | ||
2544 | vec = VEC_alloc (tree, gc, list_length (params)); | |
2545 | for (; params; params = TREE_CHAIN (params)) | |
2546 | VEC_quick_push (tree, vec, TREE_VALUE (params)); | |
c2255bc4 | 2547 | ret = build_function_call_vec (loc, function, vec, NULL); |
bbbbb16a ILT |
2548 | VEC_free (tree, gc, vec); |
2549 | return ret; | |
2550 | } | |
2551 | ||
2552 | /* Build a function call to function FUNCTION with parameters PARAMS. | |
2553 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2554 | either NULL or the original type of the corresponding element in | |
2555 | PARAMS. The original type may differ from TREE_TYPE of the | |
2556 | parameter for enums. FUNCTION's data type may be a function type | |
2557 | or pointer-to-function. This function changes the elements of | |
2558 | PARAMS. */ | |
2559 | ||
2560 | tree | |
c2255bc4 | 2561 | build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params, |
bbbbb16a | 2562 | VEC(tree,gc) *origtypes) |
400fbf9f | 2563 | { |
b3694847 | 2564 | tree fntype, fundecl = 0; |
4977bab6 | 2565 | tree name = NULL_TREE, result; |
c96f4f73 | 2566 | tree tem; |
94a0dd7b SL |
2567 | int nargs; |
2568 | tree *argarray; | |
2569 | ||
400fbf9f | 2570 | |
fc76e425 | 2571 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ |
a7d53fce | 2572 | STRIP_TYPE_NOPS (function); |
400fbf9f JW |
2573 | |
2574 | /* Convert anything with function type to a pointer-to-function. */ | |
2575 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2576 | { | |
58646b77 PB |
2577 | /* Implement type-directed function overloading for builtins. |
2578 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
2579 | handle all the type checking. The result is a complete expression | |
2580 | that implements this function call. */ | |
c2255bc4 | 2581 | tem = resolve_overloaded_builtin (loc, function, params); |
58646b77 PB |
2582 | if (tem) |
2583 | return tem; | |
48ae6c13 | 2584 | |
400fbf9f | 2585 | name = DECL_NAME (function); |
a5eadacc | 2586 | fundecl = function; |
400fbf9f | 2587 | } |
f2a71bbc | 2588 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) |
c2255bc4 | 2589 | function = function_to_pointer_conversion (loc, function); |
400fbf9f | 2590 | |
6e955430 ZL |
2591 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF |
2592 | expressions, like those used for ObjC messenger dispatches. */ | |
bbbbb16a ILT |
2593 | if (!VEC_empty (tree, params)) |
2594 | function = objc_rewrite_function_call (function, | |
2595 | VEC_index (tree, params, 0)); | |
6e955430 | 2596 | |
928c19bb JM |
2597 | function = c_fully_fold (function, false, NULL); |
2598 | ||
400fbf9f JW |
2599 | fntype = TREE_TYPE (function); |
2600 | ||
2601 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2602 | return error_mark_node; | |
2603 | ||
2604 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2605 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2606 | { | |
c2255bc4 | 2607 | error_at (loc, "called object %qE is not a function", function); |
400fbf9f JW |
2608 | return error_mark_node; |
2609 | } | |
2610 | ||
5ce89b2e JM |
2611 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) |
2612 | current_function_returns_abnormally = 1; | |
2613 | ||
400fbf9f JW |
2614 | /* fntype now gets the type of function pointed to. */ |
2615 | fntype = TREE_TYPE (fntype); | |
2616 | ||
ab4194da JM |
2617 | /* Convert the parameters to the types declared in the |
2618 | function prototype, or apply default promotions. */ | |
2619 | ||
bbbbb16a ILT |
2620 | nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes, |
2621 | function, fundecl); | |
ab4194da JM |
2622 | if (nargs < 0) |
2623 | return error_mark_node; | |
2624 | ||
c96f4f73 EB |
2625 | /* Check that the function is called through a compatible prototype. |
2626 | If it is not, replace the call by a trap, wrapped up in a compound | |
2627 | expression if necessary. This has the nice side-effect to prevent | |
2628 | the tree-inliner from generating invalid assignment trees which may | |
58393038 | 2629 | blow up in the RTL expander later. */ |
1043771b | 2630 | if (CONVERT_EXPR_P (function) |
c96f4f73 EB |
2631 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR |
2632 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
3f75a254 | 2633 | && !comptypes (fntype, TREE_TYPE (tem))) |
c96f4f73 EB |
2634 | { |
2635 | tree return_type = TREE_TYPE (fntype); | |
c2255bc4 | 2636 | tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP], |
c96f4f73 | 2637 | NULL_TREE); |
ab4194da | 2638 | int i; |
c96f4f73 EB |
2639 | |
2640 | /* This situation leads to run-time undefined behavior. We can't, | |
2641 | therefore, simply error unless we can prove that all possible | |
2642 | executions of the program must execute the code. */ | |
c2255bc4 | 2643 | if (warning_at (loc, 0, "function called through a non-compatible type")) |
71205d17 MLI |
2644 | /* We can, however, treat "undefined" any way we please. |
2645 | Call abort to encourage the user to fix the program. */ | |
c2255bc4 | 2646 | inform (loc, "if this code is reached, the program will abort"); |
ab4194da JM |
2647 | /* Before the abort, allow the function arguments to exit or |
2648 | call longjmp. */ | |
2649 | for (i = 0; i < nargs; i++) | |
bbbbb16a ILT |
2650 | trap = build2 (COMPOUND_EXPR, void_type_node, |
2651 | VEC_index (tree, params, i), trap); | |
bba745c1 | 2652 | |
c96f4f73 | 2653 | if (VOID_TYPE_P (return_type)) |
3ce62965 JM |
2654 | { |
2655 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
db3927fb | 2656 | pedwarn (loc, 0, |
3ce62965 JM |
2657 | "function with qualified void return type called"); |
2658 | return trap; | |
2659 | } | |
c96f4f73 EB |
2660 | else |
2661 | { | |
2662 | tree rhs; | |
2663 | ||
2664 | if (AGGREGATE_TYPE_P (return_type)) | |
c2255bc4 | 2665 | rhs = build_compound_literal (loc, return_type, |
928c19bb JM |
2666 | build_constructor (return_type, 0), |
2667 | false); | |
c96f4f73 | 2668 | else |
db3927fb | 2669 | rhs = fold_convert_loc (loc, return_type, integer_zero_node); |
c96f4f73 | 2670 | |
3ce62965 JM |
2671 | return require_complete_type (build2 (COMPOUND_EXPR, return_type, |
2672 | trap, rhs)); | |
c96f4f73 EB |
2673 | } |
2674 | } | |
2675 | ||
bbbbb16a ILT |
2676 | argarray = VEC_address (tree, params); |
2677 | ||
83322951 RG |
2678 | /* Check that arguments to builtin functions match the expectations. */ |
2679 | if (fundecl | |
2680 | && DECL_BUILT_IN (fundecl) | |
2681 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2682 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2683 | return error_mark_node; | |
400fbf9f | 2684 | |
83322951 | 2685 | /* Check that the arguments to the function are valid. */ |
94a0dd7b | 2686 | check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray, |
10a22b11 | 2687 | TYPE_ARG_TYPES (fntype)); |
400fbf9f | 2688 | |
928c19bb JM |
2689 | if (name != NULL_TREE |
2690 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
bf730f15 | 2691 | { |
928c19bb | 2692 | if (require_constant_value) |
db3927fb AH |
2693 | result = |
2694 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), | |
2695 | function, nargs, argarray); | |
928c19bb | 2696 | else |
db3927fb AH |
2697 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), |
2698 | function, nargs, argarray); | |
928c19bb JM |
2699 | if (TREE_CODE (result) == NOP_EXPR |
2700 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2701 | STRIP_TYPE_NOPS (result); | |
bf730f15 RS |
2702 | } |
2703 | else | |
db3927fb AH |
2704 | result = build_call_array_loc (loc, TREE_TYPE (fntype), |
2705 | function, nargs, argarray); | |
b0b3afb2 | 2706 | |
71653180 | 2707 | if (VOID_TYPE_P (TREE_TYPE (result))) |
3ce62965 JM |
2708 | { |
2709 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
db3927fb | 2710 | pedwarn (loc, 0, |
3ce62965 JM |
2711 | "function with qualified void return type called"); |
2712 | return result; | |
2713 | } | |
1eb8759b | 2714 | return require_complete_type (result); |
400fbf9f JW |
2715 | } |
2716 | \f | |
bbbbb16a ILT |
2717 | /* Convert the argument expressions in the vector VALUES |
2718 | to the types in the list TYPELIST. | |
400fbf9f JW |
2719 | |
2720 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
2721 | perform the default conversions. | |
2722 | ||
bbbbb16a ILT |
2723 | ORIGTYPES is the original types of the expressions in VALUES. This |
2724 | holds the type of enum values which have been converted to integral | |
2725 | types. It may be NULL. | |
400fbf9f | 2726 | |
03dafa61 JM |
2727 | FUNCTION is a tree for the called function. It is used only for |
2728 | error messages, where it is formatted with %qE. | |
400fbf9f JW |
2729 | |
2730 | This is also where warnings about wrong number of args are generated. | |
2731 | ||
94a0dd7b | 2732 | Returns the actual number of arguments processed (which may be less |
bbbbb16a ILT |
2733 | than the length of VALUES in some error situations), or -1 on |
2734 | failure. */ | |
94a0dd7b SL |
2735 | |
2736 | static int | |
bbbbb16a ILT |
2737 | convert_arguments (tree typelist, VEC(tree,gc) *values, |
2738 | VEC(tree,gc) *origtypes, tree function, tree fundecl) | |
400fbf9f | 2739 | { |
bbbbb16a ILT |
2740 | tree typetail, val; |
2741 | unsigned int parmnum; | |
b5d32c25 KG |
2742 | const bool type_generic = fundecl |
2743 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl))); | |
8ce94e44 | 2744 | bool type_generic_remove_excess_precision = false; |
03dafa61 | 2745 | tree selector; |
03dafa61 | 2746 | |
2ac2f164 JM |
2747 | /* Change pointer to function to the function itself for |
2748 | diagnostics. */ | |
03dafa61 JM |
2749 | if (TREE_CODE (function) == ADDR_EXPR |
2750 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
2ac2f164 | 2751 | function = TREE_OPERAND (function, 0); |
03dafa61 JM |
2752 | |
2753 | /* Handle an ObjC selector specially for diagnostics. */ | |
2754 | selector = objc_message_selector (); | |
400fbf9f | 2755 | |
8ce94e44 JM |
2756 | /* For type-generic built-in functions, determine whether excess |
2757 | precision should be removed (classification) or not | |
2758 | (comparison). */ | |
2759 | if (type_generic | |
2760 | && DECL_BUILT_IN (fundecl) | |
2761 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
2762 | { | |
2763 | switch (DECL_FUNCTION_CODE (fundecl)) | |
2764 | { | |
2765 | case BUILT_IN_ISFINITE: | |
2766 | case BUILT_IN_ISINF: | |
2767 | case BUILT_IN_ISINF_SIGN: | |
2768 | case BUILT_IN_ISNAN: | |
2769 | case BUILT_IN_ISNORMAL: | |
2770 | case BUILT_IN_FPCLASSIFY: | |
2771 | type_generic_remove_excess_precision = true; | |
2772 | break; | |
2773 | ||
2774 | default: | |
2775 | type_generic_remove_excess_precision = false; | |
2776 | break; | |
2777 | } | |
2778 | } | |
2779 | ||
400fbf9f | 2780 | /* Scan the given expressions and types, producing individual |
bbbbb16a | 2781 | converted arguments. */ |
400fbf9f | 2782 | |
bbbbb16a ILT |
2783 | for (typetail = typelist, parmnum = 0; |
2784 | VEC_iterate (tree, values, parmnum, val); | |
2785 | ++parmnum) | |
400fbf9f | 2786 | { |
b3694847 | 2787 | tree type = typetail ? TREE_VALUE (typetail) : 0; |
8ce94e44 | 2788 | tree valtype = TREE_TYPE (val); |
03dafa61 JM |
2789 | tree rname = function; |
2790 | int argnum = parmnum + 1; | |
4d3e6fae | 2791 | const char *invalid_func_diag; |
8ce94e44 | 2792 | bool excess_precision = false; |
928c19bb | 2793 | bool npc; |
bbbbb16a | 2794 | tree parmval; |
400fbf9f JW |
2795 | |
2796 | if (type == void_type_node) | |
2797 | { | |
03dafa61 | 2798 | error ("too many arguments to function %qE", function); |
94a0dd7b | 2799 | return parmnum; |
400fbf9f JW |
2800 | } |
2801 | ||
03dafa61 JM |
2802 | if (selector && argnum > 2) |
2803 | { | |
2804 | rname = selector; | |
2805 | argnum -= 2; | |
2806 | } | |
2807 | ||
928c19bb | 2808 | npc = null_pointer_constant_p (val); |
8ce94e44 JM |
2809 | |
2810 | /* If there is excess precision and a prototype, convert once to | |
2811 | the required type rather than converting via the semantic | |
2812 | type. Likewise without a prototype a float value represented | |
2813 | as long double should be converted once to double. But for | |
2814 | type-generic classification functions excess precision must | |
2815 | be removed here. */ | |
2816 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
2817 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
2818 | { | |
2819 | val = TREE_OPERAND (val, 0); | |
2820 | excess_precision = true; | |
2821 | } | |
928c19bb | 2822 | val = c_fully_fold (val, false, NULL); |
ed248cf7 | 2823 | STRIP_TYPE_NOPS (val); |
400fbf9f | 2824 | |
400fbf9f JW |
2825 | val = require_complete_type (val); |
2826 | ||
2827 | if (type != 0) | |
2828 | { | |
2829 | /* Formal parm type is specified by a function prototype. */ | |
400fbf9f | 2830 | |
20913689 | 2831 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) |
400fbf9f JW |
2832 | { |
2833 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
2834 | parmval = val; | |
2835 | } | |
2836 | else | |
2837 | { | |
bbbbb16a ILT |
2838 | tree origtype; |
2839 | ||
d45cf215 RS |
2840 | /* Optionally warn about conversions that |
2841 | differ from the default conversions. */ | |
05170031 | 2842 | if (warn_traditional_conversion || warn_traditional) |
400fbf9f | 2843 | { |
e3a64162 | 2844 | unsigned int formal_prec = TYPE_PRECISION (type); |
400fbf9f | 2845 | |
aae43c5f | 2846 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 2847 | && TREE_CODE (valtype) == REAL_TYPE) |
d4ee4d25 | 2848 | warning (0, "passing argument %d of %qE as integer " |
03dafa61 JM |
2849 | "rather than floating due to prototype", |
2850 | argnum, rname); | |
03829ad2 | 2851 | if (INTEGRAL_TYPE_P (type) |
8ce94e44 | 2852 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
d4ee4d25 | 2853 | warning (0, "passing argument %d of %qE as integer " |
03dafa61 JM |
2854 | "rather than complex due to prototype", |
2855 | argnum, rname); | |
aae43c5f | 2856 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 2857 | && TREE_CODE (valtype) == REAL_TYPE) |
d4ee4d25 | 2858 | warning (0, "passing argument %d of %qE as complex " |
03dafa61 JM |
2859 | "rather than floating due to prototype", |
2860 | argnum, rname); | |
400fbf9f | 2861 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 2862 | && INTEGRAL_TYPE_P (valtype)) |
d4ee4d25 | 2863 | warning (0, "passing argument %d of %qE as floating " |
03dafa61 JM |
2864 | "rather than integer due to prototype", |
2865 | argnum, rname); | |
03829ad2 | 2866 | else if (TREE_CODE (type) == COMPLEX_TYPE |
8ce94e44 | 2867 | && INTEGRAL_TYPE_P (valtype)) |
d4ee4d25 | 2868 | warning (0, "passing argument %d of %qE as complex " |
03dafa61 JM |
2869 | "rather than integer due to prototype", |
2870 | argnum, rname); | |
aae43c5f | 2871 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 2872 | && TREE_CODE (valtype) == COMPLEX_TYPE) |
d4ee4d25 | 2873 | warning (0, "passing argument %d of %qE as floating " |
03dafa61 JM |
2874 | "rather than complex due to prototype", |
2875 | argnum, rname); | |
aae43c5f RK |
2876 | /* ??? At some point, messages should be written about |
2877 | conversions between complex types, but that's too messy | |
2878 | to do now. */ | |
d45cf215 | 2879 | else if (TREE_CODE (type) == REAL_TYPE |
8ce94e44 | 2880 | && TREE_CODE (valtype) == REAL_TYPE) |
d45cf215 RS |
2881 | { |
2882 | /* Warn if any argument is passed as `float', | |
047de90b | 2883 | since without a prototype it would be `double'. */ |
9a8ce21f JG |
2884 | if (formal_prec == TYPE_PRECISION (float_type_node) |
2885 | && type != dfloat32_type_node) | |
d4ee4d25 | 2886 | warning (0, "passing argument %d of %qE as %<float%> " |
03dafa61 JM |
2887 | "rather than %<double%> due to prototype", |
2888 | argnum, rname); | |
9a8ce21f JG |
2889 | |
2890 | /* Warn if mismatch between argument and prototype | |
2891 | for decimal float types. Warn of conversions with | |
2892 | binary float types and of precision narrowing due to | |
2893 | prototype. */ | |
8ce94e44 | 2894 | else if (type != valtype |
9a8ce21f JG |
2895 | && (type == dfloat32_type_node |
2896 | || type == dfloat64_type_node | |
c22cacf3 | 2897 | || type == dfloat128_type_node |
8ce94e44 JM |
2898 | || valtype == dfloat32_type_node |
2899 | || valtype == dfloat64_type_node | |
2900 | || valtype == dfloat128_type_node) | |
c22cacf3 | 2901 | && (formal_prec |
8ce94e44 | 2902 | <= TYPE_PRECISION (valtype) |
9a8ce21f | 2903 | || (type == dfloat128_type_node |
8ce94e44 | 2904 | && (valtype |
c22cacf3 | 2905 | != dfloat64_type_node |
8ce94e44 | 2906 | && (valtype |
9a8ce21f JG |
2907 | != dfloat32_type_node))) |
2908 | || (type == dfloat64_type_node | |
8ce94e44 | 2909 | && (valtype |
9a8ce21f JG |
2910 | != dfloat32_type_node)))) |
2911 | warning (0, "passing argument %d of %qE as %qT " | |
2912 | "rather than %qT due to prototype", | |
8ce94e44 | 2913 | argnum, rname, type, valtype); |
9a8ce21f | 2914 | |
d45cf215 | 2915 | } |
3ed56f8a KG |
2916 | /* Detect integer changing in width or signedness. |
2917 | These warnings are only activated with | |
05170031 MLI |
2918 | -Wtraditional-conversion, not with -Wtraditional. */ |
2919 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
8ce94e44 | 2920 | && INTEGRAL_TYPE_P (valtype)) |
400fbf9f | 2921 | { |
d45cf215 RS |
2922 | tree would_have_been = default_conversion (val); |
2923 | tree type1 = TREE_TYPE (would_have_been); | |
2924 | ||
754a4d82 | 2925 | if (TREE_CODE (type) == ENUMERAL_TYPE |
a38b987a | 2926 | && (TYPE_MAIN_VARIANT (type) |
8ce94e44 | 2927 | == TYPE_MAIN_VARIANT (valtype))) |
754a4d82 RS |
2928 | /* No warning if function asks for enum |
2929 | and the actual arg is that enum type. */ | |
2930 | ; | |
2931 | else if (formal_prec != TYPE_PRECISION (type1)) | |
c2255bc4 AH |
2932 | warning (OPT_Wtraditional_conversion, |
2933 | "passing argument %d of %qE " | |
3176a0c2 DD |
2934 | "with different width due to prototype", |
2935 | argnum, rname); | |
8df83eae | 2936 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) |
d45cf215 | 2937 | ; |
800cd3b9 RS |
2938 | /* Don't complain if the formal parameter type |
2939 | is an enum, because we can't tell now whether | |
2940 | the value was an enum--even the same enum. */ | |
2941 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
2942 | ; | |
400fbf9f JW |
2943 | else if (TREE_CODE (val) == INTEGER_CST |
2944 | && int_fits_type_p (val, type)) | |
2945 | /* Change in signedness doesn't matter | |
2946 | if a constant value is unaffected. */ | |
2947 | ; | |
ce9895ae RS |
2948 | /* If the value is extended from a narrower |
2949 | unsigned type, it doesn't matter whether we | |
2950 | pass it as signed or unsigned; the value | |
2951 | certainly is the same either way. */ | |
8ce94e44 JM |
2952 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) |
2953 | && TYPE_UNSIGNED (valtype)) | |
ce9895ae | 2954 | ; |
8df83eae | 2955 | else if (TYPE_UNSIGNED (type)) |
c2255bc4 AH |
2956 | warning (OPT_Wtraditional_conversion, |
2957 | "passing argument %d of %qE " | |
3176a0c2 DD |
2958 | "as unsigned due to prototype", |
2959 | argnum, rname); | |
3ed56f8a | 2960 | else |
c2255bc4 AH |
2961 | warning (OPT_Wtraditional_conversion, |
2962 | "passing argument %d of %qE " | |
3176a0c2 | 2963 | "as signed due to prototype", argnum, rname); |
400fbf9f JW |
2964 | } |
2965 | } | |
2966 | ||
8ce94e44 JM |
2967 | /* Possibly restore an EXCESS_PRECISION_EXPR for the |
2968 | sake of better warnings from convert_and_check. */ | |
2969 | if (excess_precision) | |
2970 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
bbbbb16a ILT |
2971 | origtype = (origtypes == NULL |
2972 | ? NULL_TREE | |
2973 | : VEC_index (tree, origtypes, parmnum)); | |
744aa42f ILT |
2974 | parmval = convert_for_assignment (input_location, type, val, |
2975 | origtype, ic_argpass, npc, | |
2ac2f164 JM |
2976 | fundecl, function, |
2977 | parmnum + 1); | |
2f6e4e97 | 2978 | |
61f71b34 | 2979 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) |
b6d6aa84 | 2980 | && INTEGRAL_TYPE_P (type) |
400fbf9f JW |
2981 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) |
2982 | parmval = default_conversion (parmval); | |
400fbf9f | 2983 | } |
400fbf9f | 2984 | } |
8ce94e44 JM |
2985 | else if (TREE_CODE (valtype) == REAL_TYPE |
2986 | && (TYPE_PRECISION (valtype) | |
c22cacf3 | 2987 | < TYPE_PRECISION (double_type_node)) |
8ce94e44 | 2988 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) |
b5d32c25 KG |
2989 | { |
2990 | if (type_generic) | |
bbbbb16a | 2991 | parmval = val; |
b5d32c25 KG |
2992 | else |
2993 | /* Convert `float' to `double'. */ | |
bbbbb16a | 2994 | parmval = convert (double_type_node, val); |
b5d32c25 | 2995 | } |
8ce94e44 JM |
2996 | else if (excess_precision && !type_generic) |
2997 | /* A "double" argument with excess precision being passed | |
2998 | without a prototype or in variable arguments. */ | |
bbbbb16a | 2999 | parmval = convert (valtype, val); |
c22cacf3 MS |
3000 | else if ((invalid_func_diag = |
3001 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
4d3e6fae FJ |
3002 | { |
3003 | error (invalid_func_diag); | |
94a0dd7b | 3004 | return -1; |
4d3e6fae | 3005 | } |
400fbf9f JW |
3006 | else |
3007 | /* Convert `short' and `char' to full-size `int'. */ | |
bbbbb16a ILT |
3008 | parmval = default_conversion (val); |
3009 | ||
3010 | VEC_replace (tree, values, parmnum, parmval); | |
400fbf9f JW |
3011 | |
3012 | if (typetail) | |
3013 | typetail = TREE_CHAIN (typetail); | |
3014 | } | |
3015 | ||
bbbbb16a | 3016 | gcc_assert (parmnum == VEC_length (tree, values)); |
94a0dd7b | 3017 | |
400fbf9f | 3018 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) |
3789b316 JM |
3019 | { |
3020 | error ("too few arguments to function %qE", function); | |
94a0dd7b | 3021 | return -1; |
3789b316 | 3022 | } |
400fbf9f | 3023 | |
94a0dd7b | 3024 | return parmnum; |
400fbf9f JW |
3025 | } |
3026 | \f | |
43f6dfd3 RS |
3027 | /* This is the entry point used by the parser to build unary operators |
3028 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3029 | ARG is the operand. For unary plus, the C parser currently uses | |
6a3799eb AH |
3030 | CONVERT_EXPR for code. |
3031 | ||
3032 | LOC is the location to use for the tree generated. | |
3033 | */ | |
43f6dfd3 RS |
3034 | |
3035 | struct c_expr | |
c2255bc4 | 3036 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) |
43f6dfd3 RS |
3037 | { |
3038 | struct c_expr result; | |
3039 | ||
c9f9eb5d | 3040 | result.value = build_unary_op (loc, code, arg.value, 0); |
100d537d | 3041 | result.original_code = code; |
6866c6e8 ILT |
3042 | result.original_type = NULL; |
3043 | ||
59c0753d | 3044 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) |
c2255bc4 | 3045 | overflow_warning (loc, result.value); |
59c0753d | 3046 | |
43f6dfd3 RS |
3047 | return result; |
3048 | } | |
3049 | ||
3050 | /* This is the entry point used by the parser to build binary operators | |
3051 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3052 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3053 | expression, we check for operands that were written with other binary | |
ba47d38d AH |
3054 | operators in a way that is likely to confuse the user. |
3055 | ||
3056 | LOCATION is the location of the binary operator. */ | |
edc7c4ec | 3057 | |
487a92fe | 3058 | struct c_expr |
ba47d38d AH |
3059 | parser_build_binary_op (location_t location, enum tree_code code, |
3060 | struct c_expr arg1, struct c_expr arg2) | |
400fbf9f | 3061 | { |
487a92fe | 3062 | struct c_expr result; |
400fbf9f | 3063 | |
487a92fe JM |
3064 | enum tree_code code1 = arg1.original_code; |
3065 | enum tree_code code2 = arg2.original_code; | |
6866c6e8 ILT |
3066 | tree type1 = (arg1.original_type |
3067 | ? arg1.original_type | |
3068 | : TREE_TYPE (arg1.value)); | |
3069 | tree type2 = (arg2.original_type | |
3070 | ? arg2.original_type | |
3071 | : TREE_TYPE (arg2.value)); | |
400fbf9f | 3072 | |
ba47d38d AH |
3073 | result.value = build_binary_op (location, code, |
3074 | arg1.value, arg2.value, 1); | |
487a92fe | 3075 | result.original_code = code; |
6866c6e8 | 3076 | result.original_type = NULL; |
58bf601b | 3077 | |
487a92fe JM |
3078 | if (TREE_CODE (result.value) == ERROR_MARK) |
3079 | return result; | |
400fbf9f | 3080 | |
ba47d38d AH |
3081 | if (location != UNKNOWN_LOCATION) |
3082 | protected_set_expr_location (result.value, location); | |
3083 | ||
400fbf9f | 3084 | /* Check for cases such as x+y<<z which users are likely |
487a92fe | 3085 | to misinterpret. */ |
400fbf9f | 3086 | if (warn_parentheses) |
100d537d | 3087 | warn_about_parentheses (code, code1, arg1.value, code2, arg2.value); |
001af587 | 3088 | |
ca409efd | 3089 | if (warn_logical_op) |
a243fb4a | 3090 | warn_logical_operator (input_location, code, TREE_TYPE (result.value), |
ca409efd | 3091 | code1, arg1.value, code2, arg2.value); |
63a08740 | 3092 | |
e994a705 RS |
3093 | /* Warn about comparisons against string literals, with the exception |
3094 | of testing for equality or inequality of a string literal with NULL. */ | |
3095 | if (code == EQ_EXPR || code == NE_EXPR) | |
3096 | { | |
3097 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3098 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
c2255bc4 AH |
3099 | warning_at (location, OPT_Waddress, |
3100 | "comparison with string literal results in unspecified behavior"); | |
e994a705 RS |
3101 | } |
3102 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3103 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
c2255bc4 AH |
3104 | warning_at (location, OPT_Waddress, |
3105 | "comparison with string literal results in unspecified behavior"); | |
e994a705 | 3106 | |
59c0753d MLI |
3107 | if (TREE_OVERFLOW_P (result.value) |
3108 | && !TREE_OVERFLOW_P (arg1.value) | |
3109 | && !TREE_OVERFLOW_P (arg2.value)) | |
c2255bc4 | 3110 | overflow_warning (location, result.value); |
400fbf9f | 3111 | |
6866c6e8 ILT |
3112 | /* Warn about comparisons of different enum types. */ |
3113 | if (warn_enum_compare | |
3114 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3115 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3116 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3117 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3118 | warning_at (location, OPT_Wenum_compare, | |
3119 | "comparison between %qT and %qT", | |
3120 | type1, type2); | |
3121 | ||
400fbf9f JW |
3122 | return result; |
3123 | } | |
3e4093b6 | 3124 | \f |
3e4093b6 RS |
3125 | /* Return a tree for the difference of pointers OP0 and OP1. |
3126 | The resulting tree has type int. */ | |
293c9fdd | 3127 | |
3e4093b6 | 3128 | static tree |
db3927fb | 3129 | pointer_diff (location_t loc, tree op0, tree op1) |
3e4093b6 | 3130 | { |
3e4093b6 | 3131 | tree restype = ptrdiff_type_node; |
36c5e70a | 3132 | tree result, inttype; |
400fbf9f | 3133 | |
36c5e70a BE |
3134 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); |
3135 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
3e4093b6 RS |
3136 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); |
3137 | tree con0, con1, lit0, lit1; | |
3138 | tree orig_op1 = op1; | |
400fbf9f | 3139 | |
36c5e70a BE |
3140 | /* If the operands point into different address spaces, we need to |
3141 | explicitly convert them to pointers into the common address space | |
3142 | before we can subtract the numerical address values. */ | |
3143 | if (as0 != as1) | |
3144 | { | |
3145 | addr_space_t as_common; | |
3146 | tree common_type; | |
3147 | ||
3148 | /* Determine the common superset address space. This is guaranteed | |
3149 | to exist because the caller verified that comp_target_types | |
3150 | returned non-zero. */ | |
3151 | if (!addr_space_superset (as0, as1, &as_common)) | |
3152 | gcc_unreachable (); | |
3153 | ||
3154 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3155 | op0 = convert (common_type, op0); | |
3156 | op1 = convert (common_type, op1); | |
3157 | } | |
3158 | ||
3159 | /* Determine integer type to perform computations in. This will usually | |
3160 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3161 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3162 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
3163 | inttype = lang_hooks.types.type_for_size | |
3164 | (TYPE_PRECISION (TREE_TYPE (op0)), 0); | |
3165 | else | |
3166 | inttype = restype; | |
3167 | ||
3168 | ||
fcf73884 | 3169 | if (TREE_CODE (target_type) == VOID_TYPE) |
db3927fb | 3170 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 MLI |
3171 | "pointer of type %<void *%> used in subtraction"); |
3172 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
db3927fb | 3173 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 | 3174 | "pointer to a function used in subtraction"); |
400fbf9f | 3175 | |
3e4093b6 RS |
3176 | /* If the conversion to ptrdiff_type does anything like widening or |
3177 | converting a partial to an integral mode, we get a convert_expression | |
3178 | that is in the way to do any simplifications. | |
3179 | (fold-const.c doesn't know that the extra bits won't be needed. | |
3180 | split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a | |
3181 | different mode in place.) | |
3182 | So first try to find a common term here 'by hand'; we want to cover | |
3183 | at least the cases that occur in legal static initializers. */ | |
1043771b | 3184 | if (CONVERT_EXPR_P (op0) |
1344f9a3 JM |
3185 | && (TYPE_PRECISION (TREE_TYPE (op0)) |
3186 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))) | |
3187 | con0 = TREE_OPERAND (op0, 0); | |
3188 | else | |
3189 | con0 = op0; | |
1043771b | 3190 | if (CONVERT_EXPR_P (op1) |
1344f9a3 JM |
3191 | && (TYPE_PRECISION (TREE_TYPE (op1)) |
3192 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))) | |
3193 | con1 = TREE_OPERAND (op1, 0); | |
3194 | else | |
3195 | con1 = op1; | |
400fbf9f | 3196 | |
3e4093b6 RS |
3197 | if (TREE_CODE (con0) == PLUS_EXPR) |
3198 | { | |
3199 | lit0 = TREE_OPERAND (con0, 1); | |
3200 | con0 = TREE_OPERAND (con0, 0); | |
3201 | } | |
3202 | else | |
3203 | lit0 = integer_zero_node; | |
400fbf9f | 3204 | |
3e4093b6 | 3205 | if (TREE_CODE (con1) == PLUS_EXPR) |
400fbf9f | 3206 | { |
3e4093b6 RS |
3207 | lit1 = TREE_OPERAND (con1, 1); |
3208 | con1 = TREE_OPERAND (con1, 0); | |
400fbf9f JW |
3209 | } |
3210 | else | |
3e4093b6 RS |
3211 | lit1 = integer_zero_node; |
3212 | ||
3213 | if (operand_equal_p (con0, con1, 0)) | |
400fbf9f | 3214 | { |
3e4093b6 RS |
3215 | op0 = lit0; |
3216 | op1 = lit1; | |
400fbf9f JW |
3217 | } |
3218 | ||
400fbf9f | 3219 | |
3e4093b6 RS |
3220 | /* First do the subtraction as integers; |
3221 | then drop through to build the divide operator. | |
3222 | Do not do default conversions on the minus operator | |
3223 | in case restype is a short type. */ | |
400fbf9f | 3224 | |
db3927fb | 3225 | op0 = build_binary_op (loc, |
36c5e70a BE |
3226 | MINUS_EXPR, convert (inttype, op0), |
3227 | convert (inttype, op1), 0); | |
3e4093b6 RS |
3228 | /* This generates an error if op1 is pointer to incomplete type. */ |
3229 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
db3927fb | 3230 | error_at (loc, "arithmetic on pointer to an incomplete type"); |
400fbf9f | 3231 | |
3e4093b6 RS |
3232 | /* This generates an error if op0 is pointer to incomplete type. */ |
3233 | op1 = c_size_in_bytes (target_type); | |
400fbf9f | 3234 | |
3e4093b6 | 3235 | /* Divide by the size, in easiest possible way. */ |
36c5e70a BE |
3236 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, |
3237 | op0, convert (inttype, op1)); | |
3238 | ||
3239 | /* Convert to final result type if necessary. */ | |
3240 | return convert (restype, result); | |
3e4093b6 RS |
3241 | } |
3242 | \f | |
3243 | /* Construct and perhaps optimize a tree representation | |
3244 | for a unary operation. CODE, a tree_code, specifies the operation | |
3245 | and XARG is the operand. | |
3246 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3247 | the default promotions (such as from short to int). | |
3248 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3249 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
c9f9eb5d AH |
3250 | arrays to pointers in C99. |
3251 | ||
3252 | LOCATION is the location of the operator. */ | |
400fbf9f | 3253 | |
3e4093b6 | 3254 | tree |
c9f9eb5d AH |
3255 | build_unary_op (location_t location, |
3256 | enum tree_code code, tree xarg, int flag) | |
3e4093b6 RS |
3257 | { |
3258 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3259 | tree arg = xarg; | |
3260 | tree argtype = 0; | |
808d6eaa | 3261 | enum tree_code typecode; |
3e4093b6 | 3262 | tree val; |
c9f9eb5d | 3263 | tree ret = error_mark_node; |
8ce94e44 | 3264 | tree eptype = NULL_TREE; |
3e4093b6 | 3265 | int noconvert = flag; |
4de67c26 | 3266 | const char *invalid_op_diag; |
928c19bb JM |
3267 | bool int_operands; |
3268 | ||
3269 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
4d84fe7c JM |
3270 | if (int_operands) |
3271 | arg = remove_c_maybe_const_expr (arg); | |
400fbf9f | 3272 | |
808d6eaa JM |
3273 | if (code != ADDR_EXPR) |
3274 | arg = require_complete_type (arg); | |
3275 | ||
3276 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
3e4093b6 RS |
3277 | if (typecode == ERROR_MARK) |
3278 | return error_mark_node; | |
3279 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3280 | typecode = INTEGER_TYPE; | |
6c36d76b | 3281 | |
4de67c26 JM |
3282 | if ((invalid_op_diag |
3283 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3284 | { | |
c9f9eb5d | 3285 | error_at (location, invalid_op_diag); |
4de67c26 JM |
3286 | return error_mark_node; |
3287 | } | |
3288 | ||
8ce94e44 JM |
3289 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
3290 | { | |
3291 | eptype = TREE_TYPE (arg); | |
3292 | arg = TREE_OPERAND (arg, 0); | |
3293 | } | |
3294 | ||
3e4093b6 RS |
3295 | switch (code) |
3296 | { | |
3297 | case CONVERT_EXPR: | |
3298 | /* This is used for unary plus, because a CONVERT_EXPR | |
3299 | is enough to prevent anybody from looking inside for | |
3300 | associativity, but won't generate any code. */ | |
3301 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3302 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
8a2cee38 | 3303 | || typecode == VECTOR_TYPE)) |
400fbf9f | 3304 | { |
c9f9eb5d | 3305 | error_at (location, "wrong type argument to unary plus"); |
3e4093b6 | 3306 | return error_mark_node; |
400fbf9f | 3307 | } |
3e4093b6 RS |
3308 | else if (!noconvert) |
3309 | arg = default_conversion (arg); | |
db3927fb | 3310 | arg = non_lvalue_loc (location, arg); |
400fbf9f JW |
3311 | break; |
3312 | ||
3e4093b6 RS |
3313 | case NEGATE_EXPR: |
3314 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
ab22c1fa | 3315 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE |
3e4093b6 RS |
3316 | || typecode == VECTOR_TYPE)) |
3317 | { | |
c9f9eb5d | 3318 | error_at (location, "wrong type argument to unary minus"); |
3e4093b6 RS |
3319 | return error_mark_node; |
3320 | } | |
3321 | else if (!noconvert) | |
3322 | arg = default_conversion (arg); | |
400fbf9f JW |
3323 | break; |
3324 | ||
3e4093b6 | 3325 | case BIT_NOT_EXPR: |
462643f0 AP |
3326 | /* ~ works on integer types and non float vectors. */ |
3327 | if (typecode == INTEGER_TYPE | |
3328 | || (typecode == VECTOR_TYPE | |
3329 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
03d5b1f5 | 3330 | { |
3e4093b6 RS |
3331 | if (!noconvert) |
3332 | arg = default_conversion (arg); | |
03d5b1f5 | 3333 | } |
3e4093b6 | 3334 | else if (typecode == COMPLEX_TYPE) |
400fbf9f | 3335 | { |
3e4093b6 | 3336 | code = CONJ_EXPR; |
c9f9eb5d | 3337 | pedwarn (location, OPT_pedantic, |
fcf73884 | 3338 | "ISO C does not support %<~%> for complex conjugation"); |
3e4093b6 RS |
3339 | if (!noconvert) |
3340 | arg = default_conversion (arg); | |
3341 | } | |
3342 | else | |
3343 | { | |
c9f9eb5d | 3344 | error_at (location, "wrong type argument to bit-complement"); |
3e4093b6 | 3345 | return error_mark_node; |
400fbf9f JW |
3346 | } |
3347 | break; | |
3348 | ||
3e4093b6 | 3349 | case ABS_EXPR: |
11017cc7 | 3350 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) |
400fbf9f | 3351 | { |
c9f9eb5d | 3352 | error_at (location, "wrong type argument to abs"); |
3e4093b6 | 3353 | return error_mark_node; |
400fbf9f | 3354 | } |
3e4093b6 RS |
3355 | else if (!noconvert) |
3356 | arg = default_conversion (arg); | |
400fbf9f JW |
3357 | break; |
3358 | ||
3e4093b6 RS |
3359 | case CONJ_EXPR: |
3360 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3361 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3362 | || typecode == COMPLEX_TYPE)) | |
400fbf9f | 3363 | { |
c9f9eb5d | 3364 | error_at (location, "wrong type argument to conjugation"); |
3e4093b6 | 3365 | return error_mark_node; |
400fbf9f | 3366 | } |
3e4093b6 RS |
3367 | else if (!noconvert) |
3368 | arg = default_conversion (arg); | |
400fbf9f JW |
3369 | break; |
3370 | ||
3e4093b6 | 3371 | case TRUTH_NOT_EXPR: |
ab22c1fa | 3372 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3373 | && typecode != REAL_TYPE && typecode != POINTER_TYPE |
46bdb9cf | 3374 | && typecode != COMPLEX_TYPE) |
400fbf9f | 3375 | { |
c9f9eb5d AH |
3376 | error_at (location, |
3377 | "wrong type argument to unary exclamation mark"); | |
3e4093b6 | 3378 | return error_mark_node; |
400fbf9f | 3379 | } |
c9f9eb5d | 3380 | arg = c_objc_common_truthvalue_conversion (location, arg); |
db3927fb | 3381 | ret = invert_truthvalue_loc (location, arg); |
ca80e52b EB |
3382 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ |
3383 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3384 | location = EXPR_LOCATION (ret); | |
c9f9eb5d | 3385 | goto return_build_unary_op; |
3e4093b6 | 3386 | |
3e4093b6 RS |
3387 | case REALPART_EXPR: |
3388 | if (TREE_CODE (arg) == COMPLEX_CST) | |
c9f9eb5d | 3389 | ret = TREE_REALPART (arg); |
3e4093b6 | 3390 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
db3927fb AH |
3391 | ret = fold_build1_loc (location, |
3392 | REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg); | |
3e4093b6 | 3393 | else |
c9f9eb5d | 3394 | ret = arg; |
8ce94e44 JM |
3395 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3396 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3397 | goto return_build_unary_op; |
605a99f6 | 3398 | |
3e4093b6 RS |
3399 | case IMAGPART_EXPR: |
3400 | if (TREE_CODE (arg) == COMPLEX_CST) | |
c9f9eb5d | 3401 | ret = TREE_IMAGPART (arg); |
3e4093b6 | 3402 | else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE) |
db3927fb AH |
3403 | ret = fold_build1_loc (location, |
3404 | IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg); | |
3e4093b6 | 3405 | else |
db3927fb AH |
3406 | ret = omit_one_operand_loc (location, TREE_TYPE (arg), |
3407 | integer_zero_node, arg); | |
8ce94e44 JM |
3408 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) |
3409 | eptype = TREE_TYPE (eptype); | |
c9f9eb5d | 3410 | goto return_build_unary_op; |
3e4093b6 RS |
3411 | |
3412 | case PREINCREMENT_EXPR: | |
3413 | case POSTINCREMENT_EXPR: | |
3414 | case PREDECREMENT_EXPR: | |
3415 | case POSTDECREMENT_EXPR: | |
3e4093b6 | 3416 | |
928c19bb JM |
3417 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) |
3418 | { | |
3419 | tree inner = build_unary_op (location, code, | |
3420 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3421 | if (inner == error_mark_node) | |
3422 | return error_mark_node; | |
3423 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3424 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3425 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3426 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3427 | goto return_build_unary_op; | |
3428 | } | |
3429 | ||
3430 | /* Complain about anything that is not a true lvalue. */ | |
3431 | if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR | |
3432 | || code == POSTINCREMENT_EXPR) | |
3433 | ? lv_increment | |
3434 | : lv_decrement))) | |
3435 | return error_mark_node; | |
3436 | ||
09639a83 ILT |
3437 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) |
3438 | { | |
3439 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3440 | warning_at (location, OPT_Wc___compat, | |
3441 | "increment of enumeration value is invalid in C++"); | |
3442 | else | |
3443 | warning_at (location, OPT_Wc___compat, | |
3444 | "decrement of enumeration value is invalid in C++"); | |
3445 | } | |
3446 | ||
928c19bb JM |
3447 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ |
3448 | arg = c_fully_fold (arg, false, NULL); | |
3449 | ||
3e4093b6 RS |
3450 | /* Increment or decrement the real part of the value, |
3451 | and don't change the imaginary part. */ | |
3452 | if (typecode == COMPLEX_TYPE) | |
400fbf9f | 3453 | { |
3e4093b6 RS |
3454 | tree real, imag; |
3455 | ||
c9f9eb5d | 3456 | pedwarn (location, OPT_pedantic, |
509c9d60 | 3457 | "ISO C does not support %<++%> and %<--%> on complex types"); |
3e4093b6 RS |
3458 | |
3459 | arg = stabilize_reference (arg); | |
c9f9eb5d AH |
3460 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); |
3461 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
3462 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
a87db577 AP |
3463 | if (real == error_mark_node || imag == error_mark_node) |
3464 | return error_mark_node; | |
c9f9eb5d AH |
3465 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), |
3466 | real, imag); | |
3467 | goto return_build_unary_op; | |
400fbf9f | 3468 | } |
3e4093b6 RS |
3469 | |
3470 | /* Report invalid types. */ | |
3471 | ||
ab22c1fa | 3472 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE |
3e4093b6 | 3473 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE) |
400fbf9f | 3474 | { |
3e4093b6 | 3475 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c9f9eb5d | 3476 | error_at (location, "wrong type argument to increment"); |
c22cacf3 | 3477 | else |
c9f9eb5d | 3478 | error_at (location, "wrong type argument to decrement"); |
3e4093b6 RS |
3479 | |
3480 | return error_mark_node; | |
400fbf9f | 3481 | } |
400fbf9f | 3482 | |
3e4093b6 RS |
3483 | { |
3484 | tree inc; | |
400fbf9f | 3485 | |
3e4093b6 RS |
3486 | argtype = TREE_TYPE (arg); |
3487 | ||
3488 | /* Compute the increment. */ | |
3489 | ||
3490 | if (typecode == POINTER_TYPE) | |
3491 | { | |
3492 | /* If pointer target is an undefined struct, | |
3493 | we just cannot know how to do the arithmetic. */ | |
b70cef5d | 3494 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) |
3e4093b6 RS |
3495 | { |
3496 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
c9f9eb5d AH |
3497 | error_at (location, |
3498 | "increment of pointer to unknown structure"); | |
3e4093b6 | 3499 | else |
c9f9eb5d AH |
3500 | error_at (location, |
3501 | "decrement of pointer to unknown structure"); | |
3e4093b6 | 3502 | } |
b70cef5d JJ |
3503 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE |
3504 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
c22cacf3 | 3505 | { |
3e4093b6 | 3506 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) |
c9f9eb5d | 3507 | pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 | 3508 | "wrong type argument to increment"); |
3e4093b6 | 3509 | else |
c9f9eb5d | 3510 | pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
fcf73884 | 3511 | "wrong type argument to decrement"); |
3e4093b6 RS |
3512 | } |
3513 | ||
b70cef5d | 3514 | inc = c_size_in_bytes (TREE_TYPE (argtype)); |
db3927fb | 3515 | inc = fold_convert_loc (location, sizetype, inc); |
3e4093b6 | 3516 | } |
b70cef5d | 3517 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) |
ab22c1fa CF |
3518 | { |
3519 | /* For signed fract types, we invert ++ to -- or | |
3520 | -- to ++, and change inc from 1 to -1, because | |
3521 | it is not possible to represent 1 in signed fract constants. | |
3522 | For unsigned fract types, the result always overflows and | |
3523 | we get an undefined (original) or the maximum value. */ | |
3524 | if (code == PREINCREMENT_EXPR) | |
3525 | code = PREDECREMENT_EXPR; | |
3526 | else if (code == PREDECREMENT_EXPR) | |
3527 | code = PREINCREMENT_EXPR; | |
3528 | else if (code == POSTINCREMENT_EXPR) | |
3529 | code = POSTDECREMENT_EXPR; | |
3530 | else /* code == POSTDECREMENT_EXPR */ | |
3531 | code = POSTINCREMENT_EXPR; | |
3532 | ||
3533 | inc = integer_minus_one_node; | |
3534 | inc = convert (argtype, inc); | |
3535 | } | |
3e4093b6 | 3536 | else |
5be014d5 AP |
3537 | { |
3538 | inc = integer_one_node; | |
3539 | inc = convert (argtype, inc); | |
3540 | } | |
3e4093b6 | 3541 | |
3e4093b6 | 3542 | /* Report a read-only lvalue. */ |
f37acdf9 | 3543 | if (TYPE_READONLY (argtype)) |
953ff289 DN |
3544 | { |
3545 | readonly_error (arg, | |
3546 | ((code == PREINCREMENT_EXPR | |
3547 | || code == POSTINCREMENT_EXPR) | |
3548 | ? lv_increment : lv_decrement)); | |
3549 | return error_mark_node; | |
3550 | } | |
f37acdf9 JM |
3551 | else if (TREE_READONLY (arg)) |
3552 | readonly_warning (arg, | |
3553 | ((code == PREINCREMENT_EXPR | |
3554 | || code == POSTINCREMENT_EXPR) | |
3555 | ? lv_increment : lv_decrement)); | |
3e4093b6 RS |
3556 | |
3557 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) | |
3558 | val = boolean_increment (code, arg); | |
3559 | else | |
53fb4de3 | 3560 | val = build2 (code, TREE_TYPE (arg), arg, inc); |
3e4093b6 | 3561 | TREE_SIDE_EFFECTS (val) = 1; |
3e4093b6 | 3562 | if (TREE_CODE (val) != code) |
6de9cd9a | 3563 | TREE_NO_WARNING (val) = 1; |
c9f9eb5d AH |
3564 | ret = val; |
3565 | goto return_build_unary_op; | |
3e4093b6 RS |
3566 | } |
3567 | ||
3568 | case ADDR_EXPR: | |
3569 | /* Note that this operation never does default_conversion. */ | |
3570 | ||
2b4b7036 JM |
3571 | /* The operand of unary '&' must be an lvalue (which excludes |
3572 | expressions of type void), or, in C99, the result of a [] or | |
3573 | unary '*' operator. */ | |
3574 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
3575 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
3576 | && (TREE_CODE (arg) != INDIRECT_REF | |
3577 | || !flag_isoc99)) | |
3578 | pedwarn (location, 0, "taking address of expression of type %<void%>"); | |
3579 | ||
3e4093b6 RS |
3580 | /* Let &* cancel out to simplify resulting code. */ |
3581 | if (TREE_CODE (arg) == INDIRECT_REF) | |
400fbf9f | 3582 | { |
3e4093b6 RS |
3583 | /* Don't let this be an lvalue. */ |
3584 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
db3927fb | 3585 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); |
c9f9eb5d AH |
3586 | ret = TREE_OPERAND (arg, 0); |
3587 | goto return_build_unary_op; | |
400fbf9f | 3588 | } |
1eb8759b | 3589 | |
7c672dfc | 3590 | /* For &x[y], return x+y */ |
3e4093b6 | 3591 | if (TREE_CODE (arg) == ARRAY_REF) |
1eb8759b | 3592 | { |
f2a71bbc JM |
3593 | tree op0 = TREE_OPERAND (arg, 0); |
3594 | if (!c_mark_addressable (op0)) | |
3e4093b6 | 3595 | return error_mark_node; |
c9f9eb5d | 3596 | return build_binary_op (location, PLUS_EXPR, |
f2a71bbc | 3597 | (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE |
c2255bc4 AH |
3598 | ? array_to_pointer_conversion (location, |
3599 | op0) | |
f2a71bbc | 3600 | : op0), |
7c672dfc | 3601 | TREE_OPERAND (arg, 1), 1); |
1eb8759b | 3602 | } |
1eb8759b | 3603 | |
3e4093b6 RS |
3604 | /* Anything not already handled and not a true memory reference |
3605 | or a non-lvalue array is an error. */ | |
3606 | else if (typecode != FUNCTION_TYPE && !flag | |
9bf24266 | 3607 | && !lvalue_or_else (arg, lv_addressof)) |
3e4093b6 | 3608 | return error_mark_node; |
b6a10c9f | 3609 | |
928c19bb JM |
3610 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify |
3611 | folding later. */ | |
3612 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
3613 | { | |
3614 | tree inner = build_unary_op (location, code, | |
3615 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3616 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3617 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3618 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3619 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
3620 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
3621 | goto return_build_unary_op; | |
3622 | } | |
3623 | ||
3e4093b6 RS |
3624 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ |
3625 | argtype = TREE_TYPE (arg); | |
400fbf9f | 3626 | |
3e4093b6 | 3627 | /* If the lvalue is const or volatile, merge that into the type |
c22cacf3 | 3628 | to which the address will point. Note that you can't get a |
3e4093b6 RS |
3629 | restricted pointer by taking the address of something, so we |
3630 | only have to deal with `const' and `volatile' here. */ | |
6615c446 | 3631 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) |
3e4093b6 RS |
3632 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))) |
3633 | argtype = c_build_type_variant (argtype, | |
3634 | TREE_READONLY (arg), | |
3635 | TREE_THIS_VOLATILE (arg)); | |
400fbf9f | 3636 | |
3e4093b6 RS |
3637 | if (!c_mark_addressable (arg)) |
3638 | return error_mark_node; | |
400fbf9f | 3639 | |
abb54d14 JM |
3640 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF |
3641 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
400fbf9f | 3642 | |
5cc200fc | 3643 | argtype = build_pointer_type (argtype); |
5e55f99d RH |
3644 | |
3645 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
3646 | when we have proper support for integer constant expressions. */ | |
3647 | val = get_base_address (arg); | |
3648 | if (val && TREE_CODE (val) == INDIRECT_REF | |
3aa2ddb8 JJ |
3649 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) |
3650 | { | |
db3927fb AH |
3651 | tree op0 = fold_convert_loc (location, sizetype, |
3652 | fold_offsetof (arg, val)), op1; | |
3aa2ddb8 | 3653 | |
db3927fb AH |
3654 | op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0)); |
3655 | ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0); | |
c9f9eb5d | 3656 | goto return_build_unary_op; |
3aa2ddb8 | 3657 | } |
5e55f99d | 3658 | |
5cc200fc | 3659 | val = build1 (ADDR_EXPR, argtype, arg); |
400fbf9f | 3660 | |
c9f9eb5d AH |
3661 | ret = val; |
3662 | goto return_build_unary_op; | |
400fbf9f | 3663 | |
3e4093b6 | 3664 | default: |
1344f9a3 | 3665 | gcc_unreachable (); |
3e4093b6 | 3666 | } |
400fbf9f | 3667 | |
3e4093b6 RS |
3668 | if (argtype == 0) |
3669 | argtype = TREE_TYPE (arg); | |
928c19bb JM |
3670 | if (TREE_CODE (arg) == INTEGER_CST) |
3671 | ret = (require_constant_value | |
db3927fb AH |
3672 | ? fold_build1_initializer_loc (location, code, argtype, arg) |
3673 | : fold_build1_loc (location, code, argtype, arg)); | |
928c19bb JM |
3674 | else |
3675 | ret = build1 (code, argtype, arg); | |
c9f9eb5d AH |
3676 | return_build_unary_op: |
3677 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
3678 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) |
3679 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
3680 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
3681 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
3682 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
3683 | if (eptype) |
3684 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
c9f9eb5d AH |
3685 | protected_set_expr_location (ret, location); |
3686 | return ret; | |
3e4093b6 | 3687 | } |
400fbf9f | 3688 | |
3e4093b6 RS |
3689 | /* Return nonzero if REF is an lvalue valid for this language. |
3690 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
5baeaac0 | 3691 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ |
400fbf9f | 3692 | |
1e4ae551 | 3693 | bool |
58f9752a | 3694 | lvalue_p (const_tree ref) |
3e4093b6 | 3695 | { |
58f9752a | 3696 | const enum tree_code code = TREE_CODE (ref); |
400fbf9f | 3697 | |
3e4093b6 RS |
3698 | switch (code) |
3699 | { | |
3700 | case REALPART_EXPR: | |
3701 | case IMAGPART_EXPR: | |
3702 | case COMPONENT_REF: | |
3703 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
400fbf9f | 3704 | |
928c19bb JM |
3705 | case C_MAYBE_CONST_EXPR: |
3706 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
3707 | ||
3e4093b6 RS |
3708 | case COMPOUND_LITERAL_EXPR: |
3709 | case STRING_CST: | |
3710 | return 1; | |
400fbf9f | 3711 | |
3e4093b6 RS |
3712 | case INDIRECT_REF: |
3713 | case ARRAY_REF: | |
3714 | case VAR_DECL: | |
3715 | case PARM_DECL: | |
3716 | case RESULT_DECL: | |
3717 | case ERROR_MARK: | |
3718 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
3719 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
665f2503 | 3720 | |
3e4093b6 | 3721 | case BIND_EXPR: |
3e4093b6 | 3722 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; |
665f2503 | 3723 | |
3e4093b6 RS |
3724 | default: |
3725 | return 0; | |
3726 | } | |
3727 | } | |
400fbf9f | 3728 | \f |
9bf24266 | 3729 | /* Give an error for storing in something that is 'const'. */ |
54c93c30 | 3730 | |
9bf24266 JM |
3731 | static void |
3732 | readonly_error (tree arg, enum lvalue_use use) | |
54c93c30 | 3733 | { |
5544530a PB |
3734 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement |
3735 | || use == lv_asm); | |
9bf24266 JM |
3736 | /* Using this macro rather than (for example) arrays of messages |
3737 | ensures that all the format strings are checked at compile | |
3738 | time. */ | |
5544530a | 3739 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ |
c22cacf3 | 3740 | : (use == lv_increment ? (I) \ |
5544530a | 3741 | : (use == lv_decrement ? (D) : (AS)))) |
3e4093b6 | 3742 | if (TREE_CODE (arg) == COMPONENT_REF) |
54c93c30 | 3743 | { |
3e4093b6 | 3744 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) |
9bf24266 | 3745 | readonly_error (TREE_OPERAND (arg, 0), use); |
3e4093b6 | 3746 | else |
4b794eaf JJ |
3747 | error (READONLY_MSG (G_("assignment of read-only member %qD"), |
3748 | G_("increment of read-only member %qD"), | |
5544530a PB |
3749 | G_("decrement of read-only member %qD"), |
3750 | G_("read-only member %qD used as %<asm%> output")), | |
c51a1ba9 | 3751 | TREE_OPERAND (arg, 1)); |
54c93c30 | 3752 | } |
3e4093b6 | 3753 | else if (TREE_CODE (arg) == VAR_DECL) |
4b794eaf JJ |
3754 | error (READONLY_MSG (G_("assignment of read-only variable %qD"), |
3755 | G_("increment of read-only variable %qD"), | |
5544530a PB |
3756 | G_("decrement of read-only variable %qD"), |
3757 | G_("read-only variable %qD used as %<asm%> output")), | |
c51a1ba9 | 3758 | arg); |
3e4093b6 | 3759 | else |
d7705934 DB |
3760 | error (READONLY_MSG (G_("assignment of read-only location %qE"), |
3761 | G_("increment of read-only location %qE"), | |
3762 | G_("decrement of read-only location %qE"), | |
3763 | G_("read-only location %qE used as %<asm%> output")), | |
3764 | arg); | |
54c93c30 | 3765 | } |
37dc0d8d | 3766 | |
f37acdf9 JM |
3767 | /* Give a warning for storing in something that is read-only in GCC |
3768 | terms but not const in ISO C terms. */ | |
3769 | ||
3770 | static void | |
3771 | readonly_warning (tree arg, enum lvalue_use use) | |
3772 | { | |
3773 | switch (use) | |
3774 | { | |
3775 | case lv_assign: | |
3776 | warning (0, "assignment of read-only location %qE", arg); | |
3777 | break; | |
3778 | case lv_increment: | |
3779 | warning (0, "increment of read-only location %qE", arg); | |
3780 | break; | |
3781 | case lv_decrement: | |
3782 | warning (0, "decrement of read-only location %qE", arg); | |
3783 | break; | |
3784 | default: | |
3785 | gcc_unreachable (); | |
3786 | } | |
3787 | return; | |
3788 | } | |
3789 | ||
37dc0d8d JM |
3790 | |
3791 | /* Return nonzero if REF is an lvalue valid for this language; | |
3792 | otherwise, print an error message and return zero. USE says | |
3793 | how the lvalue is being used and so selects the error message. */ | |
3794 | ||
3795 | static int | |
58f9752a | 3796 | lvalue_or_else (const_tree ref, enum lvalue_use use) |
37dc0d8d JM |
3797 | { |
3798 | int win = lvalue_p (ref); | |
3799 | ||
3800 | if (!win) | |
3801 | lvalue_error (use); | |
3802 | ||
3803 | return win; | |
3804 | } | |
3e4093b6 RS |
3805 | \f |
3806 | /* Mark EXP saying that we need to be able to take the | |
3807 | address of it; it should not be allocated in a register. | |
3808 | Returns true if successful. */ | |
54c93c30 | 3809 | |
3e4093b6 RS |
3810 | bool |
3811 | c_mark_addressable (tree exp) | |
400fbf9f | 3812 | { |
3e4093b6 | 3813 | tree x = exp; |
95602da1 | 3814 | |
3e4093b6 RS |
3815 | while (1) |
3816 | switch (TREE_CODE (x)) | |
3817 | { | |
3818 | case COMPONENT_REF: | |
3819 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
3820 | { | |
0039fa55 AN |
3821 | error |
3822 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
3e4093b6 RS |
3823 | return false; |
3824 | } | |
95602da1 | 3825 | |
3e4093b6 | 3826 | /* ... fall through ... */ |
95602da1 | 3827 | |
3e4093b6 RS |
3828 | case ADDR_EXPR: |
3829 | case ARRAY_REF: | |
3830 | case REALPART_EXPR: | |
3831 | case IMAGPART_EXPR: | |
3832 | x = TREE_OPERAND (x, 0); | |
3833 | break; | |
95602da1 | 3834 | |
3e4093b6 RS |
3835 | case COMPOUND_LITERAL_EXPR: |
3836 | case CONSTRUCTOR: | |
3837 | TREE_ADDRESSABLE (x) = 1; | |
3838 | return true; | |
95602da1 | 3839 | |
3e4093b6 RS |
3840 | case VAR_DECL: |
3841 | case CONST_DECL: | |
3842 | case PARM_DECL: | |
3843 | case RESULT_DECL: | |
5baeaac0 | 3844 | if (C_DECL_REGISTER (x) |
3e4093b6 RS |
3845 | && DECL_NONLOCAL (x)) |
3846 | { | |
e697b20f | 3847 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
3e4093b6 | 3848 | { |
0039fa55 AN |
3849 | error |
3850 | ("global register variable %qD used in nested function", x); | |
3e4093b6 RS |
3851 | return false; |
3852 | } | |
509c9d60 | 3853 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); |
3e4093b6 | 3854 | } |
5baeaac0 | 3855 | else if (C_DECL_REGISTER (x)) |
3e4093b6 | 3856 | { |
e697b20f | 3857 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) |
0039fa55 AN |
3858 | error ("address of global register variable %qD requested", x); |
3859 | else | |
3860 | error ("address of register variable %qD requested", x); | |
3861 | return false; | |
3e4093b6 | 3862 | } |
400fbf9f | 3863 | |
3e4093b6 RS |
3864 | /* drops in */ |
3865 | case FUNCTION_DECL: | |
3866 | TREE_ADDRESSABLE (x) = 1; | |
3867 | /* drops out */ | |
3868 | default: | |
3869 | return true; | |
3870 | } | |
3871 | } | |
3872 | \f | |
928c19bb JM |
3873 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If |
3874 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
3875 | if folded to an integer constant then the unselected half may | |
3876 | contain arbitrary operations not normally permitted in constant | |
c2255bc4 | 3877 | expressions. Set the location of the expression to LOC. */ |
400fbf9f JW |
3878 | |
3879 | tree | |
744aa42f | 3880 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, |
d130ae11 ILT |
3881 | tree op1, tree op1_original_type, tree op2, |
3882 | tree op2_original_type) | |
400fbf9f | 3883 | { |
3e4093b6 RS |
3884 | tree type1; |
3885 | tree type2; | |
3886 | enum tree_code code1; | |
3887 | enum tree_code code2; | |
3888 | tree result_type = NULL; | |
8ce94e44 | 3889 | tree ep_result_type = NULL; |
3e4093b6 | 3890 | tree orig_op1 = op1, orig_op2 = op2; |
928c19bb | 3891 | bool int_const, op1_int_operands, op2_int_operands, int_operands; |
4d84fe7c | 3892 | bool ifexp_int_operands; |
928c19bb | 3893 | tree ret; |
4ea80a41 | 3894 | bool objc_ok; |
400fbf9f | 3895 | |
4d84fe7c JM |
3896 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); |
3897 | if (op1_int_operands) | |
3898 | op1 = remove_c_maybe_const_expr (op1); | |
3899 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
3900 | if (op2_int_operands) | |
3901 | op2 = remove_c_maybe_const_expr (op2); | |
3902 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
3903 | if (ifexp_int_operands) | |
3904 | ifexp = remove_c_maybe_const_expr (ifexp); | |
3905 | ||
3e4093b6 RS |
3906 | /* Promote both alternatives. */ |
3907 | ||
3908 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
3909 | op1 = default_conversion (op1); | |
3910 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
3911 | op2 = default_conversion (op2); | |
3912 | ||
3913 | if (TREE_CODE (ifexp) == ERROR_MARK | |
3914 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
3915 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
400fbf9f | 3916 | return error_mark_node; |
400fbf9f | 3917 | |
3e4093b6 RS |
3918 | type1 = TREE_TYPE (op1); |
3919 | code1 = TREE_CODE (type1); | |
3920 | type2 = TREE_TYPE (op2); | |
3921 | code2 = TREE_CODE (type2); | |
3922 | ||
b1adf557 JM |
3923 | /* C90 does not permit non-lvalue arrays in conditional expressions. |
3924 | In C99 they will be pointers by now. */ | |
3925 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
3926 | { | |
744aa42f | 3927 | error_at (colon_loc, "non-lvalue array in conditional expression"); |
b1adf557 JM |
3928 | return error_mark_node; |
3929 | } | |
3930 | ||
4ea80a41 DA |
3931 | objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE); |
3932 | ||
8ce94e44 JM |
3933 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR |
3934 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
3935 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
3936 | || code1 == COMPLEX_TYPE) | |
3937 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
3938 | || code2 == COMPLEX_TYPE)) | |
3939 | { | |
3940 | ep_result_type = c_common_type (type1, type2); | |
3941 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
3942 | { | |
3943 | op1 = TREE_OPERAND (op1, 0); | |
3944 | type1 = TREE_TYPE (op1); | |
3945 | gcc_assert (TREE_CODE (type1) == code1); | |
3946 | } | |
3947 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
3948 | { | |
3949 | op2 = TREE_OPERAND (op2, 0); | |
3950 | type2 = TREE_TYPE (op2); | |
3951 | gcc_assert (TREE_CODE (type2) == code2); | |
3952 | } | |
3953 | } | |
3954 | ||
d130ae11 ILT |
3955 | if (warn_cxx_compat) |
3956 | { | |
3957 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
3958 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
3959 | ||
3960 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
3961 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
3962 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
3963 | warning_at (colon_loc, OPT_Wc___compat, | |
3964 | ("different enum types in conditional is " | |
3965 | "invalid in C++: %qT vs %qT"), | |
3966 | t1, t2); | |
3967 | } | |
3968 | ||
3e4093b6 RS |
3969 | /* Quickly detect the usual case where op1 and op2 have the same type |
3970 | after promotion. */ | |
3971 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
400fbf9f | 3972 | { |
3e4093b6 RS |
3973 | if (type1 == type2) |
3974 | result_type = type1; | |
3975 | else | |
3976 | result_type = TYPE_MAIN_VARIANT (type1); | |
3977 | } | |
3978 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
c22cacf3 MS |
3979 | || code1 == COMPLEX_TYPE) |
3980 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
3981 | || code2 == COMPLEX_TYPE)) | |
3e4093b6 | 3982 | { |
ccf7f880 | 3983 | result_type = c_common_type (type1, type2); |
400fbf9f | 3984 | |
3e4093b6 RS |
3985 | /* If -Wsign-compare, warn here if type1 and type2 have |
3986 | different signedness. We'll promote the signed to unsigned | |
3987 | and later code won't know it used to be different. | |
3988 | Do this check on the original types, so that explicit casts | |
3989 | will be considered, but default promotions won't. */ | |
7d882b83 | 3990 | if (c_inhibit_evaluation_warnings == 0) |
ab87f8c8 | 3991 | { |
8df83eae RK |
3992 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); |
3993 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
400fbf9f | 3994 | |
3e4093b6 RS |
3995 | if (unsigned_op1 ^ unsigned_op2) |
3996 | { | |
6ac01510 ILT |
3997 | bool ovf; |
3998 | ||
3e4093b6 RS |
3999 | /* Do not warn if the result type is signed, since the |
4000 | signed type will only be chosen if it can represent | |
4001 | all the values of the unsigned type. */ | |
3f75a254 | 4002 | if (!TYPE_UNSIGNED (result_type)) |
3e4093b6 | 4003 | /* OK */; |
3e4093b6 | 4004 | else |
928c19bb JM |
4005 | { |
4006 | bool op1_maybe_const = true; | |
4007 | bool op2_maybe_const = true; | |
4008 | ||
4009 | /* Do not warn if the signed quantity is an | |
4010 | unsuffixed integer literal (or some static | |
4011 | constant expression involving such literals) and | |
4012 | it is non-negative. This warning requires the | |
4013 | operands to be folded for best results, so do | |
4014 | that folding in this case even without | |
4015 | warn_sign_compare to avoid warning options | |
4016 | possibly affecting code generation. */ | |
f5178456 RS |
4017 | c_inhibit_evaluation_warnings |
4018 | += (ifexp == truthvalue_false_node); | |
928c19bb JM |
4019 | op1 = c_fully_fold (op1, require_constant_value, |
4020 | &op1_maybe_const); | |
f5178456 RS |
4021 | c_inhibit_evaluation_warnings |
4022 | -= (ifexp == truthvalue_false_node); | |
4023 | ||
4024 | c_inhibit_evaluation_warnings | |
4025 | += (ifexp == truthvalue_true_node); | |
928c19bb JM |
4026 | op2 = c_fully_fold (op2, require_constant_value, |
4027 | &op2_maybe_const); | |
f5178456 RS |
4028 | c_inhibit_evaluation_warnings |
4029 | -= (ifexp == truthvalue_true_node); | |
928c19bb JM |
4030 | |
4031 | if (warn_sign_compare) | |
4032 | { | |
4033 | if ((unsigned_op2 | |
4034 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4035 | || (unsigned_op1 | |
4036 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4037 | /* OK */; | |
4038 | else | |
744aa42f ILT |
4039 | warning_at (colon_loc, OPT_Wsign_compare, |
4040 | ("signed and unsigned type in " | |
4041 | "conditional expression")); | |
928c19bb JM |
4042 | } |
4043 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
e5a94231 | 4044 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 4045 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) |
e5a94231 | 4046 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); |
928c19bb | 4047 | } |
3e4093b6 RS |
4048 | } |
4049 | } | |
4050 | } | |
4051 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4052 | { | |
fcf73884 | 4053 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) |
744aa42f | 4054 | pedwarn (colon_loc, OPT_pedantic, |
fcf73884 | 4055 | "ISO C forbids conditional expr with only one void side"); |
3e4093b6 RS |
4056 | result_type = void_type_node; |
4057 | } | |
4058 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4059 | { | |
36c5e70a BE |
4060 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); |
4061 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4062 | addr_space_t as_common; | |
4063 | ||
744aa42f | 4064 | if (comp_target_types (colon_loc, type1, type2)) |
10bc1b1b | 4065 | result_type = common_pointer_type (type1, type2); |
6aa3c60d | 4066 | else if (null_pointer_constant_p (orig_op1)) |
36c5e70a | 4067 | result_type = type2; |
6aa3c60d | 4068 | else if (null_pointer_constant_p (orig_op2)) |
36c5e70a BE |
4069 | result_type = type1; |
4070 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4071 | { | |
4072 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4073 | "used in conditional expression"); | |
4074 | return error_mark_node; | |
4075 | } | |
3e4093b6 | 4076 | else if (VOID_TYPE_P (TREE_TYPE (type1))) |
34a80643 | 4077 | { |
fcf73884 | 4078 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) |
744aa42f | 4079 | pedwarn (colon_loc, OPT_pedantic, |
509c9d60 | 4080 | "ISO C forbids conditional expr between " |
bda67431 | 4081 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4082 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), |
4083 | TREE_TYPE (type2))); | |
34a80643 | 4084 | } |
3e4093b6 | 4085 | else if (VOID_TYPE_P (TREE_TYPE (type2))) |
1c2a9b35 | 4086 | { |
fcf73884 | 4087 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) |
744aa42f | 4088 | pedwarn (colon_loc, OPT_pedantic, |
509c9d60 | 4089 | "ISO C forbids conditional expr between " |
bda67431 | 4090 | "%<void *%> and function pointer"); |
3e4093b6 RS |
4091 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), |
4092 | TREE_TYPE (type1))); | |
1c2a9b35 | 4093 | } |
34a80643 | 4094 | else |
ab87f8c8 | 4095 | { |
36c5e70a BE |
4096 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
4097 | ||
4ea80a41 | 4098 | if (!objc_ok) |
744aa42f | 4099 | pedwarn (colon_loc, 0, |
4ea80a41 | 4100 | "pointer type mismatch in conditional expression"); |
36c5e70a BE |
4101 | result_type = build_pointer_type |
4102 | (build_qualified_type (void_type_node, qual)); | |
ab87f8c8 | 4103 | } |
3e4093b6 RS |
4104 | } |
4105 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4106 | { | |
6aa3c60d | 4107 | if (!null_pointer_constant_p (orig_op2)) |
744aa42f | 4108 | pedwarn (colon_loc, 0, |
509c9d60 | 4109 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4110 | else |
ab87f8c8 | 4111 | { |
3e4093b6 | 4112 | op2 = null_pointer_node; |
ab87f8c8 | 4113 | } |
3e4093b6 RS |
4114 | result_type = type1; |
4115 | } | |
4116 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4117 | { | |
6aa3c60d | 4118 | if (!null_pointer_constant_p (orig_op1)) |
744aa42f | 4119 | pedwarn (colon_loc, 0, |
509c9d60 | 4120 | "pointer/integer type mismatch in conditional expression"); |
3e4093b6 | 4121 | else |
ab87f8c8 | 4122 | { |
3e4093b6 | 4123 | op1 = null_pointer_node; |
ab87f8c8 | 4124 | } |
3e4093b6 RS |
4125 | result_type = type2; |
4126 | } | |
1c2a9b35 | 4127 | |
3e4093b6 RS |
4128 | if (!result_type) |
4129 | { | |
4130 | if (flag_cond_mismatch) | |
4131 | result_type = void_type_node; | |
4132 | else | |
400fbf9f | 4133 | { |
c2255bc4 | 4134 | error_at (colon_loc, "type mismatch in conditional expression"); |
ab87f8c8 | 4135 | return error_mark_node; |
400fbf9f | 4136 | } |
3e4093b6 | 4137 | } |
400fbf9f | 4138 | |
3e4093b6 RS |
4139 | /* Merge const and volatile flags of the incoming types. */ |
4140 | result_type | |
4141 | = build_type_variant (result_type, | |
afbd0665 AS |
4142 | TYPE_READONLY (type1) || TYPE_READONLY (type2), |
4143 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
b6a10c9f | 4144 | |
afbd0665 | 4145 | if (result_type != type1) |
3e4093b6 | 4146 | op1 = convert_and_check (result_type, op1); |
afbd0665 | 4147 | if (result_type != type2) |
3e4093b6 | 4148 | op2 = convert_and_check (result_type, op2); |
b6a10c9f | 4149 | |
928c19bb JM |
4150 | if (ifexp_bcp && ifexp == truthvalue_true_node) |
4151 | { | |
4152 | op2_int_operands = true; | |
4153 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4154 | } | |
4155 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4156 | { | |
4157 | op1_int_operands = true; | |
4158 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4159 | } | |
4d84fe7c | 4160 | int_const = int_operands = (ifexp_int_operands |
928c19bb JM |
4161 | && op1_int_operands |
4162 | && op2_int_operands); | |
4163 | if (int_operands) | |
4164 | { | |
4165 | int_const = ((ifexp == truthvalue_true_node | |
4166 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4167 | && !TREE_OVERFLOW (orig_op1)) | |
4168 | || (ifexp == truthvalue_false_node | |
4169 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4170 | && !TREE_OVERFLOW (orig_op2))); | |
4171 | } | |
4172 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) | |
db3927fb | 4173 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); |
928c19bb JM |
4174 | else |
4175 | { | |
4176 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); | |
4177 | if (int_operands) | |
4178 | ret = note_integer_operands (ret); | |
4179 | } | |
8ce94e44 JM |
4180 | if (ep_result_type) |
4181 | ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret); | |
928c19bb | 4182 | |
c2255bc4 | 4183 | protected_set_expr_location (ret, colon_loc); |
928c19bb | 4184 | return ret; |
3e4093b6 RS |
4185 | } |
4186 | \f | |
487a92fe | 4187 | /* Return a compound expression that performs two expressions and |
c2255bc4 AH |
4188 | returns the value of the second of them. |
4189 | ||
4190 | LOC is the location of the COMPOUND_EXPR. */ | |
400fbf9f | 4191 | |
3e4093b6 | 4192 | tree |
c2255bc4 | 4193 | build_compound_expr (location_t loc, tree expr1, tree expr2) |
3e4093b6 | 4194 | { |
4d84fe7c | 4195 | bool expr1_int_operands, expr2_int_operands; |
8ce94e44 | 4196 | tree eptype = NULL_TREE; |
928c19bb JM |
4197 | tree ret; |
4198 | ||
4d84fe7c JM |
4199 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); |
4200 | if (expr1_int_operands) | |
4201 | expr1 = remove_c_maybe_const_expr (expr1); | |
4202 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4203 | if (expr2_int_operands) | |
4204 | expr2 = remove_c_maybe_const_expr (expr2); | |
4205 | ||
8ce94e44 JM |
4206 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) |
4207 | expr1 = TREE_OPERAND (expr1, 0); | |
4208 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4209 | { | |
4210 | eptype = TREE_TYPE (expr2); | |
4211 | expr2 = TREE_OPERAND (expr2, 0); | |
4212 | } | |
4213 | ||
3f75a254 | 4214 | if (!TREE_SIDE_EFFECTS (expr1)) |
3e4093b6 RS |
4215 | { |
4216 | /* The left-hand operand of a comma expression is like an expression | |
c5409249 | 4217 | statement: with -Wunused, we should warn if it doesn't have |
3e4093b6 | 4218 | any side-effects, unless it was explicitly cast to (void). */ |
e14a6540 | 4219 | if (warn_unused_value) |
47aecf47 | 4220 | { |
e14a6540 | 4221 | if (VOID_TYPE_P (TREE_TYPE (expr1)) |
1043771b | 4222 | && CONVERT_EXPR_P (expr1)) |
47aecf47 | 4223 | ; /* (void) a, b */ |
e14a6540 JM |
4224 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) |
4225 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
1043771b | 4226 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) |
47aecf47 JM |
4227 | ; /* (void) a, (void) b, c */ |
4228 | else | |
c2255bc4 AH |
4229 | warning_at (loc, OPT_Wunused_value, |
4230 | "left-hand operand of comma expression has no effect"); | |
47aecf47 | 4231 | } |
3e4093b6 | 4232 | } |
400fbf9f | 4233 | |
3e4093b6 RS |
4234 | /* With -Wunused, we should also warn if the left-hand operand does have |
4235 | side-effects, but computes a value which is not used. For example, in | |
4236 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4237 | so we should issue a warning. */ | |
4238 | else if (warn_unused_value) | |
c2255bc4 | 4239 | warn_if_unused_value (expr1, loc); |
400fbf9f | 4240 | |
e63d6886 AP |
4241 | if (expr2 == error_mark_node) |
4242 | return error_mark_node; | |
4243 | ||
928c19bb JM |
4244 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); |
4245 | ||
4246 | if (flag_isoc99 | |
4d84fe7c JM |
4247 | && expr1_int_operands |
4248 | && expr2_int_operands) | |
928c19bb JM |
4249 | ret = note_integer_operands (ret); |
4250 | ||
8ce94e44 JM |
4251 | if (eptype) |
4252 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4253 | ||
c2255bc4 | 4254 | protected_set_expr_location (ret, loc); |
928c19bb | 4255 | return ret; |
3e4093b6 | 4256 | } |
400fbf9f | 4257 | |
67165eb3 ILT |
4258 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to |
4259 | which we are casting. OTYPE is the type of the expression being | |
4260 | cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared | |
36c5e70a BE |
4261 | on the command line. Named address space qualifiers are not handled |
4262 | here, because they result in different warnings. */ | |
67165eb3 ILT |
4263 | |
4264 | static void | |
4265 | handle_warn_cast_qual (tree type, tree otype) | |
4266 | { | |
4267 | tree in_type = type; | |
4268 | tree in_otype = otype; | |
4269 | int added = 0; | |
4270 | int discarded = 0; | |
4271 | bool is_const; | |
4272 | ||
4273 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4274 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4275 | nodes is uninteresting and we stop as soon as we hit a | |
4276 | non-POINTER_TYPE node on either type. */ | |
4277 | do | |
4278 | { | |
4279 | in_otype = TREE_TYPE (in_otype); | |
4280 | in_type = TREE_TYPE (in_type); | |
4281 | ||
4282 | /* GNU C allows cv-qualified function types. 'const' means the | |
4283 | function is very pure, 'volatile' means it can't return. We | |
4284 | need to warn when such qualifiers are added, not when they're | |
4285 | taken away. */ | |
4286 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4287 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
36c5e70a BE |
4288 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) |
4289 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
67165eb3 | 4290 | else |
36c5e70a BE |
4291 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) |
4292 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
67165eb3 ILT |
4293 | } |
4294 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4295 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4296 | ||
4297 | if (added) | |
4298 | warning (OPT_Wcast_qual, "cast adds new qualifiers to function type"); | |
4299 | ||
4300 | if (discarded) | |
4301 | /* There are qualifiers present in IN_OTYPE that are not present | |
4302 | in IN_TYPE. */ | |
4303 | warning (OPT_Wcast_qual, | |
4304 | "cast discards qualifiers from pointer target type"); | |
4305 | ||
4306 | if (added || discarded) | |
4307 | return; | |
4308 | ||
4309 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4310 | const value to be changed with no additional warning. We only | |
4311 | issue this warning if T is the same on both sides, and we only | |
4312 | issue the warning if there are the same number of pointers on | |
4313 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4314 | cast is unsafe when a qualifier is added at one level and const | |
4315 | is not present at all outer levels. | |
4316 | ||
4317 | To issue this warning, we check at each level whether the cast | |
4318 | adds new qualifiers not already seen. We don't need to special | |
4319 | case function types, as they won't have the same | |
4320 | TYPE_MAIN_VARIANT. */ | |
4321 | ||
4322 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4323 | return; | |
4324 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4325 | return; | |
4326 | ||
4327 | in_type = type; | |
4328 | in_otype = otype; | |
4329 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4330 | do | |
4331 | { | |
4332 | in_type = TREE_TYPE (in_type); | |
4333 | in_otype = TREE_TYPE (in_otype); | |
4334 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4335 | && !is_const) | |
4336 | { | |
4337 | warning (OPT_Wcast_qual, | |
4338 | ("new qualifiers in middle of multi-level non-const cast " | |
4339 | "are unsafe")); | |
4340 | break; | |
4341 | } | |
4342 | if (is_const) | |
4343 | is_const = TYPE_READONLY (in_type); | |
4344 | } | |
4345 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4346 | } | |
4347 | ||
c2255bc4 AH |
4348 | /* Build an expression representing a cast to type TYPE of expression EXPR. |
4349 | LOC is the location of the cast-- typically the open paren of the cast. */ | |
400fbf9f | 4350 | |
3e4093b6 | 4351 | tree |
c2255bc4 | 4352 | build_c_cast (location_t loc, tree type, tree expr) |
3e4093b6 | 4353 | { |
8ce94e44 JM |
4354 | tree value; |
4355 | ||
4356 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
4357 | expr = TREE_OPERAND (expr, 0); | |
4358 | ||
4359 | value = expr; | |
400fbf9f | 4360 | |
3e4093b6 RS |
4361 | if (type == error_mark_node || expr == error_mark_node) |
4362 | return error_mark_node; | |
400fbf9f | 4363 | |
3e4093b6 RS |
4364 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing |
4365 | only in <protocol> qualifications. But when constructing cast expressions, | |
4366 | the protocols do matter and must be kept around. */ | |
700686fa ZL |
4367 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) |
4368 | return build1 (NOP_EXPR, type, expr); | |
4369 | ||
4370 | type = TYPE_MAIN_VARIANT (type); | |
400fbf9f | 4371 | |
3e4093b6 RS |
4372 | if (TREE_CODE (type) == ARRAY_TYPE) |
4373 | { | |
c2255bc4 | 4374 | error_at (loc, "cast specifies array type"); |
3e4093b6 RS |
4375 | return error_mark_node; |
4376 | } | |
400fbf9f | 4377 | |
3e4093b6 RS |
4378 | if (TREE_CODE (type) == FUNCTION_TYPE) |
4379 | { | |
c2255bc4 | 4380 | error_at (loc, "cast specifies function type"); |
3e4093b6 RS |
4381 | return error_mark_node; |
4382 | } | |
400fbf9f | 4383 | |
808d6eaa JM |
4384 | if (!VOID_TYPE_P (type)) |
4385 | { | |
4386 | value = require_complete_type (value); | |
4387 | if (value == error_mark_node) | |
4388 | return error_mark_node; | |
4389 | } | |
4390 | ||
3e4093b6 RS |
4391 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) |
4392 | { | |
fcf73884 MLI |
4393 | if (TREE_CODE (type) == RECORD_TYPE |
4394 | || TREE_CODE (type) == UNION_TYPE) | |
c2255bc4 | 4395 | pedwarn (loc, OPT_pedantic, |
fcf73884 | 4396 | "ISO C forbids casting nonscalar to the same type"); |
3e4093b6 RS |
4397 | } |
4398 | else if (TREE_CODE (type) == UNION_TYPE) | |
4399 | { | |
4400 | tree field; | |
400fbf9f | 4401 | |
3e4093b6 | 4402 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
39ffbac9 VR |
4403 | if (TREE_TYPE (field) != error_mark_node |
4404 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
4405 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
3e4093b6 RS |
4406 | break; |
4407 | ||
4408 | if (field) | |
400fbf9f | 4409 | { |
3e4093b6 RS |
4410 | tree t; |
4411 | ||
c2255bc4 AH |
4412 | pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type"); |
4413 | t = digest_init (loc, type, | |
4038c495 | 4414 | build_constructor_single (type, field, value), |
bbbbb16a | 4415 | NULL_TREE, false, true, 0); |
3e4093b6 RS |
4416 | TREE_CONSTANT (t) = TREE_CONSTANT (value); |
4417 | return t; | |
400fbf9f | 4418 | } |
c2255bc4 | 4419 | error_at (loc, "cast to union type from type not present in union"); |
3e4093b6 RS |
4420 | return error_mark_node; |
4421 | } | |
4422 | else | |
4423 | { | |
4424 | tree otype, ovalue; | |
400fbf9f | 4425 | |
3e4093b6 | 4426 | if (type == void_type_node) |
c2255bc4 AH |
4427 | { |
4428 | tree t = build1 (CONVERT_EXPR, type, value); | |
4429 | SET_EXPR_LOCATION (t, loc); | |
4430 | return t; | |
4431 | } | |
400fbf9f | 4432 | |
3e4093b6 | 4433 | otype = TREE_TYPE (value); |
400fbf9f | 4434 | |
3e4093b6 | 4435 | /* Optionally warn about potentially worrisome casts. */ |
3e4093b6 RS |
4436 | if (warn_cast_qual |
4437 | && TREE_CODE (type) == POINTER_TYPE | |
4438 | && TREE_CODE (otype) == POINTER_TYPE) | |
67165eb3 | 4439 | handle_warn_cast_qual (type, otype); |
400fbf9f | 4440 | |
36c5e70a BE |
4441 | /* Warn about conversions between pointers to disjoint |
4442 | address spaces. */ | |
4443 | if (TREE_CODE (type) == POINTER_TYPE | |
4444 | && TREE_CODE (otype) == POINTER_TYPE | |
4445 | && !null_pointer_constant_p (value)) | |
4446 | { | |
4447 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
4448 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
4449 | addr_space_t as_common; | |
4450 | ||
4451 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
4452 | { | |
4453 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
4454 | warning_at (loc, 0, "cast to %s address space pointer " | |
4455 | "from disjoint generic address space pointer", | |
4456 | c_addr_space_name (as_to)); | |
4457 | ||
4458 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
4459 | warning_at (loc, 0, "cast to generic address space pointer " | |
4460 | "from disjoint %s address space pointer", | |
4461 | c_addr_space_name (as_from)); | |
4462 | ||
4463 | else | |
4464 | warning_at (loc, 0, "cast to %s address space pointer " | |
4465 | "from disjoint %s address space pointer", | |
4466 | c_addr_space_name (as_to), | |
4467 | c_addr_space_name (as_from)); | |
4468 | } | |
4469 | } | |
4470 | ||
3e4093b6 | 4471 | /* Warn about possible alignment problems. */ |
3176a0c2 | 4472 | if (STRICT_ALIGNMENT |
3e4093b6 RS |
4473 | && TREE_CODE (type) == POINTER_TYPE |
4474 | && TREE_CODE (otype) == POINTER_TYPE | |
4475 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
4476 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
4477 | /* Don't warn about opaque types, where the actual alignment | |
4478 | restriction is unknown. */ | |
4479 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
4480 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
4481 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
4482 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
c2255bc4 AH |
4483 | warning_at (loc, OPT_Wcast_align, |
4484 | "cast increases required alignment of target type"); | |
e9a25f70 | 4485 | |
3176a0c2 | 4486 | if (TREE_CODE (type) == INTEGER_TYPE |
3e4093b6 | 4487 | && TREE_CODE (otype) == POINTER_TYPE |
8d1c7aef | 4488 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) |
c22cacf3 MS |
4489 | /* Unlike conversion of integers to pointers, where the |
4490 | warning is disabled for converting constants because | |
4491 | of cases such as SIG_*, warn about converting constant | |
4492 | pointers to integers. In some cases it may cause unwanted | |
8d1c7aef | 4493 | sign extension, and a warning is appropriate. */ |
c2255bc4 AH |
4494 | warning_at (loc, OPT_Wpointer_to_int_cast, |
4495 | "cast from pointer to integer of different size"); | |
400fbf9f | 4496 | |
3176a0c2 | 4497 | if (TREE_CODE (value) == CALL_EXPR |
3e4093b6 | 4498 | && TREE_CODE (type) != TREE_CODE (otype)) |
c2255bc4 AH |
4499 | warning_at (loc, OPT_Wbad_function_cast, |
4500 | "cast from function call of type %qT " | |
4501 | "to non-matching type %qT", otype, type); | |
400fbf9f | 4502 | |
3176a0c2 | 4503 | if (TREE_CODE (type) == POINTER_TYPE |
3e4093b6 RS |
4504 | && TREE_CODE (otype) == INTEGER_TYPE |
4505 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
4506 | /* Don't warn about converting any constant. */ | |
4507 | && !TREE_CONSTANT (value)) | |
c2255bc4 AH |
4508 | warning_at (loc, |
4509 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
4510 | "of different size"); | |
400fbf9f | 4511 | |
79bedddc SR |
4512 | if (warn_strict_aliasing <= 2) |
4513 | strict_aliasing_warning (otype, type, expr); | |
400fbf9f | 4514 | |
3897f229 JM |
4515 | /* If pedantic, warn for conversions between function and object |
4516 | pointer types, except for converting a null pointer constant | |
4517 | to function pointer type. */ | |
4518 | if (pedantic | |
4519 | && TREE_CODE (type) == POINTER_TYPE | |
4520 | && TREE_CODE (otype) == POINTER_TYPE | |
4521 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
4522 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
c2255bc4 | 4523 | pedwarn (loc, OPT_pedantic, "ISO C forbids " |
fcf73884 | 4524 | "conversion of function pointer to object pointer type"); |
3897f229 JM |
4525 | |
4526 | if (pedantic | |
4527 | && TREE_CODE (type) == POINTER_TYPE | |
4528 | && TREE_CODE (otype) == POINTER_TYPE | |
4529 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
4530 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
6aa3c60d | 4531 | && !null_pointer_constant_p (value)) |
c2255bc4 | 4532 | pedwarn (loc, OPT_pedantic, "ISO C forbids " |
fcf73884 | 4533 | "conversion of object pointer to function pointer type"); |
3897f229 | 4534 | |
3e4093b6 | 4535 | ovalue = value; |
3e4093b6 | 4536 | value = convert (type, value); |
400fbf9f | 4537 | |
3e4093b6 | 4538 | /* Ignore any integer overflow caused by the cast. */ |
928c19bb | 4539 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) |
3e4093b6 | 4540 | { |
8bcd6380 | 4541 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) |
6414bad6 | 4542 | { |
8bcd6380 RS |
4543 | if (!TREE_OVERFLOW (value)) |
4544 | { | |
4545 | /* Avoid clobbering a shared constant. */ | |
4546 | value = copy_node (value); | |
4547 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
4548 | } | |
6414bad6 | 4549 | } |
8bcd6380 | 4550 | else if (TREE_OVERFLOW (value)) |
d8e1f97b PB |
4551 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ |
4552 | value = build_int_cst_wide (TREE_TYPE (value), | |
4553 | TREE_INT_CST_LOW (value), | |
4554 | TREE_INT_CST_HIGH (value)); | |
3e4093b6 RS |
4555 | } |
4556 | } | |
400fbf9f | 4557 | |
53cd18ec JM |
4558 | /* Don't let a cast be an lvalue. */ |
4559 | if (value == expr) | |
db3927fb | 4560 | value = non_lvalue_loc (loc, value); |
e9a25f70 | 4561 | |
928c19bb JM |
4562 | /* Don't allow the results of casting to floating-point or complex |
4563 | types be confused with actual constants, or casts involving | |
4564 | integer and pointer types other than direct integer-to-integer | |
4565 | and integer-to-pointer be confused with integer constant | |
4566 | expressions and null pointer constants. */ | |
4567 | if (TREE_CODE (value) == REAL_CST | |
4568 | || TREE_CODE (value) == COMPLEX_CST | |
4569 | || (TREE_CODE (value) == INTEGER_CST | |
4570 | && !((TREE_CODE (expr) == INTEGER_CST | |
4571 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
4572 | || TREE_CODE (expr) == REAL_CST | |
4573 | || TREE_CODE (expr) == COMPLEX_CST))) | |
4574 | value = build1 (NOP_EXPR, type, value); | |
4575 | ||
c2255bc4 AH |
4576 | if (CAN_HAVE_LOCATION_P (value)) |
4577 | SET_EXPR_LOCATION (value, loc); | |
3e4093b6 | 4578 | return value; |
400fbf9f JW |
4579 | } |
4580 | ||
c2255bc4 AH |
4581 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the |
4582 | location of the open paren of the cast, or the position of the cast | |
4583 | expr. */ | |
3e4093b6 | 4584 | tree |
c2255bc4 | 4585 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) |
400fbf9f | 4586 | { |
f8893e47 | 4587 | tree type; |
928c19bb JM |
4588 | tree type_expr = NULL_TREE; |
4589 | bool type_expr_const = true; | |
4590 | tree ret; | |
3e4093b6 | 4591 | int saved_wsp = warn_strict_prototypes; |
c5c76735 | 4592 | |
3e4093b6 RS |
4593 | /* This avoids warnings about unprototyped casts on |
4594 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
4595 | if (TREE_CODE (expr) == INTEGER_CST) | |
4596 | warn_strict_prototypes = 0; | |
928c19bb | 4597 | type = groktypename (type_name, &type_expr, &type_expr_const); |
3e4093b6 | 4598 | warn_strict_prototypes = saved_wsp; |
c5c76735 | 4599 | |
c2255bc4 | 4600 | ret = build_c_cast (loc, type, expr); |
928c19bb JM |
4601 | if (type_expr) |
4602 | { | |
4603 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); | |
4604 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const; | |
c2255bc4 | 4605 | SET_EXPR_LOCATION (ret, loc); |
928c19bb | 4606 | } |
24b97832 ILT |
4607 | |
4608 | if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) | |
4609 | SET_EXPR_LOCATION (ret, loc); | |
4610 | ||
4611 | /* C++ does not permits types to be defined in a cast. */ | |
4612 | if (warn_cxx_compat && type_name->specs->tag_defined_p) | |
4613 | warning_at (loc, OPT_Wc___compat, | |
4614 | "defining a type in a cast is invalid in C++"); | |
4615 | ||
928c19bb | 4616 | return ret; |
400fbf9f | 4617 | } |
3e4093b6 RS |
4618 | \f |
4619 | /* Build an assignment expression of lvalue LHS from value RHS. | |
32e8bb8e ILT |
4620 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which |
4621 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
3e4093b6 RS |
4622 | MODIFYCODE is the code for a binary operator that we use |
4623 | to combine the old value of LHS with RHS to get the new value. | |
c9f9eb5d | 4624 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. |
bbbbb16a ILT |
4625 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, |
4626 | which may differ from TREE_TYPE (RHS) for an enum value. | |
c9f9eb5d | 4627 | |
c2255bc4 AH |
4628 | LOCATION is the location of the MODIFYCODE operator. |
4629 | RHS_LOC is the location of the RHS. */ | |
2f6e4e97 | 4630 | |
3e4093b6 | 4631 | tree |
32e8bb8e | 4632 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, |
c2255bc4 AH |
4633 | enum tree_code modifycode, |
4634 | location_t rhs_loc, tree rhs, tree rhs_origtype) | |
400fbf9f | 4635 | { |
3e4093b6 RS |
4636 | tree result; |
4637 | tree newrhs; | |
8ce94e44 | 4638 | tree rhs_semantic_type = NULL_TREE; |
3e4093b6 RS |
4639 | tree lhstype = TREE_TYPE (lhs); |
4640 | tree olhstype = lhstype; | |
928c19bb | 4641 | bool npc; |
e9a25f70 | 4642 | |
3e4093b6 RS |
4643 | /* Types that aren't fully specified cannot be used in assignments. */ |
4644 | lhs = require_complete_type (lhs); | |
e9a25f70 | 4645 | |
3e4093b6 RS |
4646 | /* Avoid duplicate error messages from operands that had errors. */ |
4647 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
4648 | return error_mark_node; | |
400fbf9f | 4649 | |
c0bcacec VR |
4650 | if (!lvalue_or_else (lhs, lv_assign)) |
4651 | return error_mark_node; | |
4652 | ||
8ce94e44 JM |
4653 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
4654 | { | |
4655 | rhs_semantic_type = TREE_TYPE (rhs); | |
4656 | rhs = TREE_OPERAND (rhs, 0); | |
4657 | } | |
4658 | ||
3e4093b6 | 4659 | newrhs = rhs; |
400fbf9f | 4660 | |
928c19bb JM |
4661 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) |
4662 | { | |
4663 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
c2255bc4 | 4664 | lhs_origtype, modifycode, rhs_loc, rhs, |
32e8bb8e | 4665 | rhs_origtype); |
928c19bb JM |
4666 | if (inner == error_mark_node) |
4667 | return error_mark_node; | |
4668 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4669 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
4670 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
4671 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
4672 | protected_set_expr_location (result, location); | |
4673 | return result; | |
4674 | } | |
4675 | ||
3e4093b6 RS |
4676 | /* If a binary op has been requested, combine the old LHS value with the RHS |
4677 | producing the value we should actually store into the LHS. */ | |
4678 | ||
4679 | if (modifycode != NOP_EXPR) | |
400fbf9f | 4680 | { |
928c19bb | 4681 | lhs = c_fully_fold (lhs, false, NULL); |
3e4093b6 | 4682 | lhs = stabilize_reference (lhs); |
c9f9eb5d | 4683 | newrhs = build_binary_op (location, |
ba47d38d | 4684 | modifycode, lhs, rhs, 1); |
bbbbb16a ILT |
4685 | |
4686 | /* The original type of the right hand side is no longer | |
4687 | meaningful. */ | |
4688 | rhs_origtype = NULL_TREE; | |
400fbf9f | 4689 | } |
400fbf9f | 4690 | |
9bf24266 | 4691 | /* Give an error for storing in something that is 'const'. */ |
bbbd6700 | 4692 | |
f37acdf9 | 4693 | if (TYPE_READONLY (lhstype) |
3e4093b6 RS |
4694 | || ((TREE_CODE (lhstype) == RECORD_TYPE |
4695 | || TREE_CODE (lhstype) == UNION_TYPE) | |
4696 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
953ff289 DN |
4697 | { |
4698 | readonly_error (lhs, lv_assign); | |
4699 | return error_mark_node; | |
4700 | } | |
f37acdf9 JM |
4701 | else if (TREE_READONLY (lhs)) |
4702 | readonly_warning (lhs, lv_assign); | |
bbbd6700 | 4703 | |
3e4093b6 RS |
4704 | /* If storing into a structure or union member, |
4705 | it has probably been given type `int'. | |
4706 | Compute the type that would go with | |
4707 | the actual amount of storage the member occupies. */ | |
bbbd6700 | 4708 | |
3e4093b6 RS |
4709 | if (TREE_CODE (lhs) == COMPONENT_REF |
4710 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
4711 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
4712 | || TREE_CODE (lhstype) == REAL_TYPE | |
4713 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
4714 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
400fbf9f | 4715 | |
3e4093b6 RS |
4716 | /* If storing in a field that is in actuality a short or narrower than one, |
4717 | we must store in the field in its actual type. */ | |
4718 | ||
4719 | if (lhstype != TREE_TYPE (lhs)) | |
4720 | { | |
4721 | lhs = copy_node (lhs); | |
4722 | TREE_TYPE (lhs) = lhstype; | |
400fbf9f | 4723 | } |
400fbf9f | 4724 | |
32e8bb8e ILT |
4725 | /* Issue -Wc++-compat warnings about an assignment to an enum type |
4726 | when LHS does not have its original type. This happens for, | |
4727 | e.g., an enum bitfield in a struct. */ | |
4728 | if (warn_cxx_compat | |
4729 | && lhs_origtype != NULL_TREE | |
4730 | && lhs_origtype != lhstype | |
4731 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
4732 | { | |
4733 | tree checktype = (rhs_origtype != NULL_TREE | |
4734 | ? rhs_origtype | |
4735 | : TREE_TYPE (rhs)); | |
4736 | if (checktype != error_mark_node | |
4737 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)) | |
4738 | warning_at (location, OPT_Wc___compat, | |
4739 | "enum conversion in assignment is invalid in C++"); | |
4740 | } | |
4741 | ||
8ce94e44 JM |
4742 | /* Convert new value to destination type. Fold it first, then |
4743 | restore any excess precision information, for the sake of | |
4744 | conversion warnings. */ | |
400fbf9f | 4745 | |
928c19bb JM |
4746 | npc = null_pointer_constant_p (newrhs); |
4747 | newrhs = c_fully_fold (newrhs, false, NULL); | |
8ce94e44 JM |
4748 | if (rhs_semantic_type) |
4749 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
744aa42f ILT |
4750 | newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype, |
4751 | ic_assign, npc, NULL_TREE, NULL_TREE, 0); | |
3e4093b6 RS |
4752 | if (TREE_CODE (newrhs) == ERROR_MARK) |
4753 | return error_mark_node; | |
400fbf9f | 4754 | |
6e955430 ZL |
4755 | /* Emit ObjC write barrier, if necessary. */ |
4756 | if (c_dialect_objc () && flag_objc_gc) | |
4757 | { | |
4758 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
4759 | if (result) | |
c9f9eb5d AH |
4760 | { |
4761 | protected_set_expr_location (result, location); | |
4762 | return result; | |
4763 | } | |
6e955430 ZL |
4764 | } |
4765 | ||
ea4b7848 | 4766 | /* Scan operands. */ |
400fbf9f | 4767 | |
53fb4de3 | 4768 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); |
3e4093b6 | 4769 | TREE_SIDE_EFFECTS (result) = 1; |
c9f9eb5d | 4770 | protected_set_expr_location (result, location); |
400fbf9f | 4771 | |
3e4093b6 RS |
4772 | /* If we got the LHS in a different type for storing in, |
4773 | convert the result back to the nominal type of LHS | |
4774 | so that the value we return always has the same type | |
4775 | as the LHS argument. */ | |
e855c5ce | 4776 | |
3e4093b6 RS |
4777 | if (olhstype == TREE_TYPE (result)) |
4778 | return result; | |
c9f9eb5d | 4779 | |
744aa42f ILT |
4780 | result = convert_for_assignment (location, olhstype, result, rhs_origtype, |
4781 | ic_assign, false, NULL_TREE, NULL_TREE, 0); | |
c9f9eb5d AH |
4782 | protected_set_expr_location (result, location); |
4783 | return result; | |
3e4093b6 RS |
4784 | } |
4785 | \f | |
bbbbb16a ILT |
4786 | /* Convert value RHS to type TYPE as preparation for an assignment to |
4787 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
4788 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
4789 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
4790 | constant before any folding. | |
3e4093b6 RS |
4791 | The real work of conversion is done by `convert'. |
4792 | The purpose of this function is to generate error messages | |
4793 | for assignments that are not allowed in C. | |
2ac2f164 JM |
4794 | ERRTYPE says whether it is argument passing, assignment, |
4795 | initialization or return. | |
2f6e4e97 | 4796 | |
c2255bc4 | 4797 | LOCATION is the location of the RHS. |
2ac2f164 | 4798 | FUNCTION is a tree for the function being called. |
3e4093b6 | 4799 | PARMNUM is the number of the argument, for printing in error messages. */ |
cb3ca04e | 4800 | |
3e4093b6 | 4801 | static tree |
744aa42f ILT |
4802 | convert_for_assignment (location_t location, tree type, tree rhs, |
4803 | tree origtype, enum impl_conv errtype, | |
4804 | bool null_pointer_constant, tree fundecl, | |
4805 | tree function, int parmnum) | |
3e4093b6 RS |
4806 | { |
4807 | enum tree_code codel = TREE_CODE (type); | |
8ce94e44 | 4808 | tree orig_rhs = rhs; |
3e4093b6 RS |
4809 | tree rhstype; |
4810 | enum tree_code coder; | |
2ac2f164 | 4811 | tree rname = NULL_TREE; |
58393038 | 4812 | bool objc_ok = false; |
2ac2f164 | 4813 | |
6b4ef5c1 | 4814 | if (errtype == ic_argpass) |
2ac2f164 JM |
4815 | { |
4816 | tree selector; | |
4817 | /* Change pointer to function to the function itself for | |
4818 | diagnostics. */ | |
4819 | if (TREE_CODE (function) == ADDR_EXPR | |
4820 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
4821 | function = TREE_OPERAND (function, 0); | |
4822 | ||
4823 | /* Handle an ObjC selector specially for diagnostics. */ | |
4824 | selector = objc_message_selector (); | |
4825 | rname = function; | |
4826 | if (selector && parmnum > 2) | |
4827 | { | |
4828 | rname = selector; | |
4829 | parmnum -= 2; | |
4830 | } | |
4831 | } | |
4832 | ||
4833 | /* This macro is used to emit diagnostics to ensure that all format | |
4834 | strings are complete sentences, visible to gettext and checked at | |
4835 | compile time. */ | |
c2255bc4 | 4836 | #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \ |
1e053dfe MLI |
4837 | do { \ |
4838 | switch (errtype) \ | |
4839 | { \ | |
4840 | case ic_argpass: \ | |
4841 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \ | |
c2255bc4 AH |
4842 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ |
4843 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
1e053dfe MLI |
4844 | "expected %qT but argument is of type %qT", \ |
4845 | type, rhstype); \ | |
4846 | break; \ | |
1e053dfe MLI |
4847 | case ic_assign: \ |
4848 | pedwarn (LOCATION, OPT, AS); \ | |
4849 | break; \ | |
4850 | case ic_init: \ | |
4851 | pedwarn (LOCATION, OPT, IN); \ | |
4852 | break; \ | |
4853 | case ic_return: \ | |
c2255bc4 | 4854 | pedwarn (LOCATION, OPT, RE); \ |
1e053dfe MLI |
4855 | break; \ |
4856 | default: \ | |
4857 | gcc_unreachable (); \ | |
4858 | } \ | |
2ac2f164 | 4859 | } while (0) |
cb3ca04e | 4860 | |
8ce94e44 JM |
4861 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) |
4862 | rhs = TREE_OPERAND (rhs, 0); | |
4863 | ||
3e4093b6 RS |
4864 | rhstype = TREE_TYPE (rhs); |
4865 | coder = TREE_CODE (rhstype); | |
4866 | ||
4867 | if (coder == ERROR_MARK) | |
4868 | return error_mark_node; | |
4869 | ||
58393038 ZL |
4870 | if (c_dialect_objc ()) |
4871 | { | |
4872 | int parmno; | |
4873 | ||
4874 | switch (errtype) | |
4875 | { | |
4876 | case ic_return: | |
4877 | parmno = 0; | |
4878 | break; | |
4879 | ||
4880 | case ic_assign: | |
4881 | parmno = -1; | |
4882 | break; | |
4883 | ||
4884 | case ic_init: | |
4885 | parmno = -2; | |
4886 | break; | |
4887 | ||
4888 | default: | |
4889 | parmno = parmnum; | |
4890 | break; | |
4891 | } | |
4892 | ||
4893 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
4894 | } | |
4895 | ||
bbbbb16a ILT |
4896 | if (warn_cxx_compat) |
4897 | { | |
4898 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
4899 | if (checktype != error_mark_node | |
4900 | && TREE_CODE (type) == ENUMERAL_TYPE | |
4901 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
4902 | { | |
81f40b79 ILT |
4903 | WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat, |
4904 | G_("enum conversion when passing argument " | |
4905 | "%d of %qE is invalid in C++"), | |
4906 | G_("enum conversion in assignment is " | |
4907 | "invalid in C++"), | |
4908 | G_("enum conversion in initialization is " | |
4909 | "invalid in C++"), | |
4910 | G_("enum conversion in return is " | |
4911 | "invalid in C++")); | |
bbbbb16a ILT |
4912 | } |
4913 | } | |
4914 | ||
3e4093b6 | 4915 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) |
59c0753d | 4916 | return rhs; |
3e4093b6 RS |
4917 | |
4918 | if (coder == VOID_TYPE) | |
400fbf9f | 4919 | { |
6dcc04b0 JM |
4920 | /* Except for passing an argument to an unprototyped function, |
4921 | this is a constraint violation. When passing an argument to | |
4922 | an unprototyped function, it is compile-time undefined; | |
4923 | making it a constraint in that case was rejected in | |
4924 | DR#252. */ | |
c2255bc4 | 4925 | error_at (location, "void value not ignored as it ought to be"); |
3e4093b6 | 4926 | return error_mark_node; |
400fbf9f | 4927 | } |
808d6eaa JM |
4928 | rhs = require_complete_type (rhs); |
4929 | if (rhs == error_mark_node) | |
4930 | return error_mark_node; | |
3e4093b6 RS |
4931 | /* A type converts to a reference to it. |
4932 | This code doesn't fully support references, it's just for the | |
4933 | special case of va_start and va_copy. */ | |
4934 | if (codel == REFERENCE_TYPE | |
132da1a5 | 4935 | && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1) |
400fbf9f | 4936 | { |
3e4093b6 | 4937 | if (!lvalue_p (rhs)) |
400fbf9f | 4938 | { |
c2255bc4 | 4939 | error_at (location, "cannot pass rvalue to reference parameter"); |
3e4093b6 | 4940 | return error_mark_node; |
400fbf9f | 4941 | } |
3e4093b6 RS |
4942 | if (!c_mark_addressable (rhs)) |
4943 | return error_mark_node; | |
4944 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
c2255bc4 | 4945 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 RS |
4946 | |
4947 | /* We already know that these two types are compatible, but they | |
4948 | may not be exactly identical. In fact, `TREE_TYPE (type)' is | |
4949 | likely to be __builtin_va_list and `TREE_TYPE (rhs)' is | |
4950 | likely to be va_list, a typedef to __builtin_va_list, which | |
4951 | is different enough that it will cause problems later. */ | |
4952 | if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type)) | |
c2255bc4 AH |
4953 | { |
4954 | rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs); | |
4955 | SET_EXPR_LOCATION (rhs, location); | |
4956 | } | |
3e4093b6 RS |
4957 | |
4958 | rhs = build1 (NOP_EXPR, type, rhs); | |
c2255bc4 | 4959 | SET_EXPR_LOCATION (rhs, location); |
3e4093b6 | 4960 | return rhs; |
400fbf9f | 4961 | } |
3e4093b6 | 4962 | /* Some types can interconvert without explicit casts. */ |
3274deff | 4963 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE |
00c8e9f6 | 4964 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) |
3e4093b6 RS |
4965 | return convert (type, rhs); |
4966 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
4967 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
ab22c1fa | 4968 | || codel == FIXED_POINT_TYPE |
3e4093b6 RS |
4969 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE |
4970 | || codel == BOOLEAN_TYPE) | |
4971 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
ab22c1fa | 4972 | || coder == FIXED_POINT_TYPE |
3e4093b6 RS |
4973 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE |
4974 | || coder == BOOLEAN_TYPE)) | |
928c19bb JM |
4975 | { |
4976 | tree ret; | |
4977 | bool save = in_late_binary_op; | |
4978 | if (codel == BOOLEAN_TYPE) | |
4979 | in_late_binary_op = true; | |
8ce94e44 | 4980 | ret = convert_and_check (type, orig_rhs); |
928c19bb JM |
4981 | if (codel == BOOLEAN_TYPE) |
4982 | in_late_binary_op = save; | |
4983 | return ret; | |
4984 | } | |
400fbf9f | 4985 | |
79077aea JJ |
4986 | /* Aggregates in different TUs might need conversion. */ |
4987 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
4988 | && codel == coder | |
4989 | && comptypes (type, rhstype)) | |
4990 | return convert_and_check (type, rhs); | |
4991 | ||
3e4093b6 RS |
4992 | /* Conversion to a transparent union from its member types. |
4993 | This applies only to function arguments. */ | |
79077aea | 4994 | if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) |
6b4ef5c1 | 4995 | && errtype == ic_argpass) |
400fbf9f | 4996 | { |
0257e383 | 4997 | tree memb, marginal_memb = NULL_TREE; |
3e4093b6 | 4998 | |
0257e383 | 4999 | for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb)) |
400fbf9f | 5000 | { |
0257e383 | 5001 | tree memb_type = TREE_TYPE (memb); |
400fbf9f | 5002 | |
3e4093b6 | 5003 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), |
132da1a5 | 5004 | TYPE_MAIN_VARIANT (rhstype))) |
3e4093b6 | 5005 | break; |
e58cd767 | 5006 | |
3e4093b6 RS |
5007 | if (TREE_CODE (memb_type) != POINTER_TYPE) |
5008 | continue; | |
2f6e4e97 | 5009 | |
3e4093b6 RS |
5010 | if (coder == POINTER_TYPE) |
5011 | { | |
5012 | tree ttl = TREE_TYPE (memb_type); | |
5013 | tree ttr = TREE_TYPE (rhstype); | |
400fbf9f | 5014 | |
3e4093b6 RS |
5015 | /* Any non-function converts to a [const][volatile] void * |
5016 | and vice versa; otherwise, targets must be the same. | |
5017 | Meanwhile, the lhs target must have all the qualifiers of | |
5018 | the rhs. */ | |
5019 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
744aa42f | 5020 | || comp_target_types (location, memb_type, rhstype)) |
3e4093b6 RS |
5021 | { |
5022 | /* If this type won't generate any warnings, use it. */ | |
5023 | if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr) | |
5024 | || ((TREE_CODE (ttr) == FUNCTION_TYPE | |
5025 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5026 | ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5027 | == TYPE_QUALS (ttr)) | |
5028 | : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5029 | == TYPE_QUALS (ttl)))) | |
5030 | break; | |
400fbf9f | 5031 | |
3e4093b6 | 5032 | /* Keep looking for a better type, but remember this one. */ |
0257e383 RH |
5033 | if (!marginal_memb) |
5034 | marginal_memb = memb; | |
3e4093b6 RS |
5035 | } |
5036 | } | |
82bde854 | 5037 | |
3e4093b6 | 5038 | /* Can convert integer zero to any pointer type. */ |
928c19bb | 5039 | if (null_pointer_constant) |
3e4093b6 RS |
5040 | { |
5041 | rhs = null_pointer_node; | |
5042 | break; | |
5043 | } | |
5044 | } | |
400fbf9f | 5045 | |
0257e383 | 5046 | if (memb || marginal_memb) |
3e4093b6 | 5047 | { |
0257e383 | 5048 | if (!memb) |
3e4093b6 RS |
5049 | { |
5050 | /* We have only a marginally acceptable member type; | |
5051 | it needs a warning. */ | |
0257e383 | 5052 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); |
3e4093b6 | 5053 | tree ttr = TREE_TYPE (rhstype); |
714a0864 | 5054 | |
3e4093b6 RS |
5055 | /* Const and volatile mean something different for function |
5056 | types, so the usual warnings are not appropriate. */ | |
5057 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
5058 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5059 | { | |
5060 | /* Because const and volatile on functions are | |
5061 | restrictions that say the function will not do | |
5062 | certain things, it is okay to use a const or volatile | |
5063 | function where an ordinary one is wanted, but not | |
5064 | vice-versa. */ | |
36c5e70a BE |
5065 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5066 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
c2255bc4 | 5067 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5068 | G_("passing argument %d of %qE " |
2ac2f164 JM |
5069 | "makes qualified function " |
5070 | "pointer from unqualified"), | |
4b794eaf | 5071 | G_("assignment makes qualified " |
2ac2f164 JM |
5072 | "function pointer from " |
5073 | "unqualified"), | |
4b794eaf | 5074 | G_("initialization makes qualified " |
2ac2f164 JM |
5075 | "function pointer from " |
5076 | "unqualified"), | |
4b794eaf | 5077 | G_("return makes qualified function " |
2ac2f164 | 5078 | "pointer from unqualified")); |
3e4093b6 | 5079 | } |
36c5e70a BE |
5080 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5081 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
c2255bc4 | 5082 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5083 | G_("passing argument %d of %qE discards " |
2ac2f164 | 5084 | "qualifiers from pointer target type"), |
4b794eaf | 5085 | G_("assignment discards qualifiers " |
2ac2f164 | 5086 | "from pointer target type"), |
4b794eaf | 5087 | G_("initialization discards qualifiers " |
2ac2f164 | 5088 | "from pointer target type"), |
4b794eaf | 5089 | G_("return discards qualifiers from " |
2ac2f164 | 5090 | "pointer target type")); |
0257e383 RH |
5091 | |
5092 | memb = marginal_memb; | |
3e4093b6 | 5093 | } |
400fbf9f | 5094 | |
fcf73884 | 5095 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) |
c2255bc4 | 5096 | pedwarn (location, OPT_pedantic, |
fcf73884 | 5097 | "ISO C prohibits argument conversion to union type"); |
0e7c47fa | 5098 | |
db3927fb | 5099 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); |
0257e383 | 5100 | return build_constructor_single (type, memb, rhs); |
3e4093b6 | 5101 | } |
0e7c47fa RK |
5102 | } |
5103 | ||
3e4093b6 RS |
5104 | /* Conversions among pointers */ |
5105 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
5106 | && (coder == codel)) | |
400fbf9f | 5107 | { |
3e4093b6 RS |
5108 | tree ttl = TREE_TYPE (type); |
5109 | tree ttr = TREE_TYPE (rhstype); | |
46df2823 JM |
5110 | tree mvl = ttl; |
5111 | tree mvr = ttr; | |
3e4093b6 | 5112 | bool is_opaque_pointer; |
264fa2db | 5113 | int target_cmp = 0; /* Cache comp_target_types () result. */ |
36c5e70a BE |
5114 | addr_space_t asl; |
5115 | addr_space_t asr; | |
400fbf9f | 5116 | |
46df2823 JM |
5117 | if (TREE_CODE (mvl) != ARRAY_TYPE) |
5118 | mvl = TYPE_MAIN_VARIANT (mvl); | |
5119 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
5120 | mvr = TYPE_MAIN_VARIANT (mvr); | |
3e4093b6 | 5121 | /* Opaque pointers are treated like void pointers. */ |
f83c7f63 | 5122 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); |
c22cacf3 | 5123 | |
b7e20b53 | 5124 | /* C++ does not allow the implicit conversion void* -> T*. However, |
c22cacf3 MS |
5125 | for the purpose of reducing the number of false positives, we |
5126 | tolerate the special case of | |
b7e20b53 | 5127 | |
c22cacf3 | 5128 | int *p = NULL; |
b7e20b53 | 5129 | |
c22cacf3 | 5130 | where NULL is typically defined in C to be '(void *) 0'. */ |
c6bdf92e | 5131 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) |
c2255bc4 AH |
5132 | warning_at (location, OPT_Wc___compat, |
5133 | "request for implicit conversion " | |
5134 | "from %qT to %qT not permitted in C++", rhstype, type); | |
400fbf9f | 5135 | |
36c5e70a BE |
5136 | /* See if the pointers point to incompatible address spaces. */ |
5137 | asl = TYPE_ADDR_SPACE (ttl); | |
5138 | asr = TYPE_ADDR_SPACE (ttr); | |
5139 | if (!null_pointer_constant_p (rhs) | |
5140 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
5141 | { | |
5142 | switch (errtype) | |
5143 | { | |
5144 | case ic_argpass: | |
5145 | error_at (location, "passing argument %d of %qE from pointer to " | |
5146 | "non-enclosed address space", parmnum, rname); | |
5147 | break; | |
5148 | case ic_assign: | |
5149 | error_at (location, "assignment from pointer to " | |
5150 | "non-enclosed address space"); | |
5151 | break; | |
5152 | case ic_init: | |
5153 | error_at (location, "initialization from pointer to " | |
5154 | "non-enclosed address space"); | |
5155 | break; | |
5156 | case ic_return: | |
5157 | error_at (location, "return from pointer to " | |
5158 | "non-enclosed address space"); | |
5159 | break; | |
5160 | default: | |
5161 | gcc_unreachable (); | |
5162 | } | |
5163 | return error_mark_node; | |
5164 | } | |
5165 | ||
7876a414 KG |
5166 | /* Check if the right-hand side has a format attribute but the |
5167 | left-hand side doesn't. */ | |
104f8784 KG |
5168 | if (warn_missing_format_attribute |
5169 | && check_missing_format_attribute (type, rhstype)) | |
c22cacf3 | 5170 | { |
104f8784 KG |
5171 | switch (errtype) |
5172 | { | |
5173 | case ic_argpass: | |
c2255bc4 AH |
5174 | warning_at (location, OPT_Wmissing_format_attribute, |
5175 | "argument %d of %qE might be " | |
5176 | "a candidate for a format attribute", | |
5177 | parmnum, rname); | |
104f8784 KG |
5178 | break; |
5179 | case ic_assign: | |
c2255bc4 AH |
5180 | warning_at (location, OPT_Wmissing_format_attribute, |
5181 | "assignment left-hand side might be " | |
5182 | "a candidate for a format attribute"); | |
104f8784 KG |
5183 | break; |
5184 | case ic_init: | |
c2255bc4 AH |
5185 | warning_at (location, OPT_Wmissing_format_attribute, |
5186 | "initialization left-hand side might be " | |
5187 | "a candidate for a format attribute"); | |
104f8784 KG |
5188 | break; |
5189 | case ic_return: | |
c2255bc4 AH |
5190 | warning_at (location, OPT_Wmissing_format_attribute, |
5191 | "return type might be " | |
5192 | "a candidate for a format attribute"); | |
104f8784 KG |
5193 | break; |
5194 | default: | |
5195 | gcc_unreachable (); | |
5196 | } | |
7876a414 | 5197 | } |
c22cacf3 | 5198 | |
3e4093b6 RS |
5199 | /* Any non-function converts to a [const][volatile] void * |
5200 | and vice versa; otherwise, targets must be the same. | |
5201 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
5202 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
744aa42f | 5203 | || (target_cmp = comp_target_types (location, type, rhstype)) |
3e4093b6 | 5204 | || is_opaque_pointer |
12753674 RE |
5205 | || (c_common_unsigned_type (mvl) |
5206 | == c_common_unsigned_type (mvr))) | |
3e4093b6 RS |
5207 | { |
5208 | if (pedantic | |
5209 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5210 | || | |
5211 | (VOID_TYPE_P (ttr) | |
928c19bb | 5212 | && !null_pointer_constant |
3e4093b6 | 5213 | && TREE_CODE (ttl) == FUNCTION_TYPE))) |
c2255bc4 | 5214 | WARN_FOR_ASSIGNMENT (location, OPT_pedantic, |
509c9d60 | 5215 | G_("ISO C forbids passing argument %d of " |
2ac2f164 JM |
5216 | "%qE between function pointer " |
5217 | "and %<void *%>"), | |
4b794eaf | 5218 | G_("ISO C forbids assignment between " |
2ac2f164 | 5219 | "function pointer and %<void *%>"), |
4b794eaf | 5220 | G_("ISO C forbids initialization between " |
2ac2f164 | 5221 | "function pointer and %<void *%>"), |
4b794eaf | 5222 | G_("ISO C forbids return between function " |
2ac2f164 | 5223 | "pointer and %<void *%>")); |
3e4093b6 RS |
5224 | /* Const and volatile mean something different for function types, |
5225 | so the usual warnings are not appropriate. */ | |
5226 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
5227 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
5228 | { | |
36c5e70a BE |
5229 | if (TYPE_QUALS_NO_ADDR_SPACE (ttr) |
5230 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
58393038 ZL |
5231 | { |
5232 | /* Types differing only by the presence of the 'volatile' | |
5233 | qualifier are acceptable if the 'volatile' has been added | |
5234 | in by the Objective-C EH machinery. */ | |
5235 | if (!objc_type_quals_match (ttl, ttr)) | |
c2255bc4 | 5236 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5237 | G_("passing argument %d of %qE discards " |
58393038 | 5238 | "qualifiers from pointer target type"), |
4b794eaf | 5239 | G_("assignment discards qualifiers " |
58393038 | 5240 | "from pointer target type"), |
4b794eaf | 5241 | G_("initialization discards qualifiers " |
58393038 | 5242 | "from pointer target type"), |
4b794eaf | 5243 | G_("return discards qualifiers from " |
58393038 ZL |
5244 | "pointer target type")); |
5245 | } | |
3e4093b6 RS |
5246 | /* If this is not a case of ignoring a mismatch in signedness, |
5247 | no warning. */ | |
5248 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
264fa2db | 5249 | || target_cmp) |
3e4093b6 RS |
5250 | ; |
5251 | /* If there is a mismatch, do warn. */ | |
f2fd3821 | 5252 | else if (warn_pointer_sign) |
c2255bc4 | 5253 | WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign, |
509c9d60 | 5254 | G_("pointer targets in passing argument " |
2ac2f164 | 5255 | "%d of %qE differ in signedness"), |
4b794eaf | 5256 | G_("pointer targets in assignment " |
2ac2f164 | 5257 | "differ in signedness"), |
4b794eaf | 5258 | G_("pointer targets in initialization " |
2ac2f164 | 5259 | "differ in signedness"), |
4b794eaf | 5260 | G_("pointer targets in return differ " |
2ac2f164 | 5261 | "in signedness")); |
3e4093b6 RS |
5262 | } |
5263 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
5264 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5265 | { | |
5266 | /* Because const and volatile on functions are restrictions | |
5267 | that say the function will not do certain things, | |
5268 | it is okay to use a const or volatile function | |
5269 | where an ordinary one is wanted, but not vice-versa. */ | |
36c5e70a BE |
5270 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) |
5271 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
c2255bc4 | 5272 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5273 | G_("passing argument %d of %qE makes " |
2ac2f164 JM |
5274 | "qualified function pointer " |
5275 | "from unqualified"), | |
4b794eaf | 5276 | G_("assignment makes qualified function " |
2ac2f164 | 5277 | "pointer from unqualified"), |
4b794eaf | 5278 | G_("initialization makes qualified " |
2ac2f164 | 5279 | "function pointer from unqualified"), |
4b794eaf | 5280 | G_("return makes qualified function " |
2ac2f164 | 5281 | "pointer from unqualified")); |
3e4093b6 RS |
5282 | } |
5283 | } | |
5284 | else | |
58393038 ZL |
5285 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
5286 | if (!objc_ok) | |
c2255bc4 | 5287 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5288 | G_("passing argument %d of %qE from " |
58393038 | 5289 | "incompatible pointer type"), |
4b794eaf JJ |
5290 | G_("assignment from incompatible pointer type"), |
5291 | G_("initialization from incompatible " | |
58393038 | 5292 | "pointer type"), |
4b794eaf | 5293 | G_("return from incompatible pointer type")); |
58393038 | 5294 | |
3e4093b6 RS |
5295 | return convert (type, rhs); |
5296 | } | |
b494fd98 EB |
5297 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) |
5298 | { | |
6dcc04b0 JM |
5299 | /* ??? This should not be an error when inlining calls to |
5300 | unprototyped functions. */ | |
c2255bc4 | 5301 | error_at (location, "invalid use of non-lvalue array"); |
b494fd98 EB |
5302 | return error_mark_node; |
5303 | } | |
3e4093b6 | 5304 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) |
400fbf9f | 5305 | { |
3e4093b6 RS |
5306 | /* An explicit constant 0 can convert to a pointer, |
5307 | or one that results from arithmetic, even including | |
5308 | a cast to integer type. */ | |
928c19bb | 5309 | if (!null_pointer_constant) |
c2255bc4 | 5310 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5311 | G_("passing argument %d of %qE makes " |
2ac2f164 | 5312 | "pointer from integer without a cast"), |
4b794eaf | 5313 | G_("assignment makes pointer from integer " |
2ac2f164 | 5314 | "without a cast"), |
4b794eaf | 5315 | G_("initialization makes pointer from " |
2ac2f164 | 5316 | "integer without a cast"), |
4b794eaf | 5317 | G_("return makes pointer from integer " |
2ac2f164 | 5318 | "without a cast")); |
b3006337 EB |
5319 | |
5320 | return convert (type, rhs); | |
400fbf9f | 5321 | } |
3e4093b6 | 5322 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) |
400fbf9f | 5323 | { |
c2255bc4 | 5324 | WARN_FOR_ASSIGNMENT (location, 0, |
509c9d60 | 5325 | G_("passing argument %d of %qE makes integer " |
2ac2f164 | 5326 | "from pointer without a cast"), |
4b794eaf | 5327 | G_("assignment makes integer from pointer " |
2ac2f164 | 5328 | "without a cast"), |
4b794eaf | 5329 | G_("initialization makes integer from pointer " |
2ac2f164 | 5330 | "without a cast"), |
4b794eaf | 5331 | G_("return makes integer from pointer " |
2ac2f164 | 5332 | "without a cast")); |
3e4093b6 RS |
5333 | return convert (type, rhs); |
5334 | } | |
5335 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
928c19bb JM |
5336 | { |
5337 | tree ret; | |
5338 | bool save = in_late_binary_op; | |
5339 | in_late_binary_op = true; | |
5340 | ret = convert (type, rhs); | |
5341 | in_late_binary_op = save; | |
5342 | return ret; | |
5343 | } | |
400fbf9f | 5344 | |
2ac2f164 | 5345 | switch (errtype) |
3e4093b6 | 5346 | { |
2ac2f164 | 5347 | case ic_argpass: |
c2255bc4 | 5348 | error_at (location, "incompatible type for argument %d of %qE", parmnum, rname); |
a7da8b42 MLI |
5349 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) |
5350 | ? DECL_SOURCE_LOCATION (fundecl) : input_location, | |
5351 | "expected %qT but argument is of type %qT", type, rhstype); | |
2ac2f164 JM |
5352 | break; |
5353 | case ic_assign: | |
c2255bc4 AH |
5354 | error_at (location, "incompatible types when assigning to type %qT from " |
5355 | "type %qT", type, rhstype); | |
2ac2f164 JM |
5356 | break; |
5357 | case ic_init: | |
c2255bc4 AH |
5358 | error_at (location, |
5359 | "incompatible types when initializing type %qT using type %qT", | |
5360 | type, rhstype); | |
2ac2f164 JM |
5361 | break; |
5362 | case ic_return: | |
c2255bc4 AH |
5363 | error_at (location, |
5364 | "incompatible types when returning type %qT but %qT was " | |
5365 | "expected", rhstype, type); | |
2ac2f164 JM |
5366 | break; |
5367 | default: | |
5368 | gcc_unreachable (); | |
400fbf9f | 5369 | } |
53b01f59 | 5370 | |
3e4093b6 RS |
5371 | return error_mark_node; |
5372 | } | |
3e4093b6 RS |
5373 | \f |
5374 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
5375 | return its value. Otherwise, return error_mark_node. | |
15b732b2 | 5376 | |
3e4093b6 RS |
5377 | This is for handling COMPOUND_EXPRs as initializer elements |
5378 | which is allowed with a warning when -pedantic is specified. */ | |
15b732b2 | 5379 | |
3e4093b6 RS |
5380 | static tree |
5381 | valid_compound_expr_initializer (tree value, tree endtype) | |
5382 | { | |
5383 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
5384 | { | |
5385 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
5386 | == error_mark_node) | |
5387 | return error_mark_node; | |
5388 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
5389 | endtype); | |
5390 | } | |
116df786 | 5391 | else if (!initializer_constant_valid_p (value, endtype)) |
3e4093b6 RS |
5392 | return error_mark_node; |
5393 | else | |
5394 | return value; | |
15b732b2 | 5395 | } |
400fbf9f | 5396 | \f |
3e4093b6 RS |
5397 | /* Perform appropriate conversions on the initial value of a variable, |
5398 | store it in the declaration DECL, | |
5399 | and print any error messages that are appropriate. | |
bbbbb16a | 5400 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
c2255bc4 AH |
5401 | If the init is invalid, store an ERROR_MARK. |
5402 | ||
5403 | INIT_LOC is the location of the initial value. */ | |
400fbf9f | 5404 | |
3e4093b6 | 5405 | void |
c2255bc4 | 5406 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) |
400fbf9f | 5407 | { |
3e4093b6 | 5408 | tree value, type; |
928c19bb | 5409 | bool npc = false; |
400fbf9f | 5410 | |
3e4093b6 | 5411 | /* If variable's type was invalidly declared, just ignore it. */ |
400fbf9f | 5412 | |
3e4093b6 RS |
5413 | type = TREE_TYPE (decl); |
5414 | if (TREE_CODE (type) == ERROR_MARK) | |
5415 | return; | |
400fbf9f | 5416 | |
3e4093b6 | 5417 | /* Digest the specified initializer into an expression. */ |
400fbf9f | 5418 | |
928c19bb JM |
5419 | if (init) |
5420 | npc = null_pointer_constant_p (init); | |
c2255bc4 AH |
5421 | value = digest_init (init_loc, type, init, origtype, npc, |
5422 | true, TREE_STATIC (decl)); | |
400fbf9f | 5423 | |
3e4093b6 | 5424 | /* Store the expression if valid; else report error. */ |
400fbf9f | 5425 | |
3176a0c2 | 5426 | if (!in_system_header |
3f75a254 | 5427 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) |
3176a0c2 DD |
5428 | warning (OPT_Wtraditional, "traditional C rejects automatic " |
5429 | "aggregate initialization"); | |
2f6e4e97 | 5430 | |
3e4093b6 | 5431 | DECL_INITIAL (decl) = value; |
400fbf9f | 5432 | |
3e4093b6 RS |
5433 | /* ANSI wants warnings about out-of-range constant initializers. */ |
5434 | STRIP_TYPE_NOPS (value); | |
c2658540 MLI |
5435 | if (TREE_STATIC (decl)) |
5436 | constant_expression_warning (value); | |
400fbf9f | 5437 | |
3e4093b6 RS |
5438 | /* Check if we need to set array size from compound literal size. */ |
5439 | if (TREE_CODE (type) == ARRAY_TYPE | |
5440 | && TYPE_DOMAIN (type) == 0 | |
5441 | && value != error_mark_node) | |
400fbf9f | 5442 | { |
3e4093b6 RS |
5443 | tree inside_init = init; |
5444 | ||
ed248cf7 | 5445 | STRIP_TYPE_NOPS (inside_init); |
3e4093b6 RS |
5446 | inside_init = fold (inside_init); |
5447 | ||
5448 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
5449 | { | |
8d9f82d5 | 5450 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
3e4093b6 | 5451 | |
8d9f82d5 | 5452 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) |
3e4093b6 RS |
5453 | { |
5454 | /* For int foo[] = (int [3]){1}; we need to set array size | |
5455 | now since later on array initializer will be just the | |
5456 | brace enclosed list of the compound literal. */ | |
8d9f82d5 AO |
5457 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); |
5458 | TREE_TYPE (decl) = type; | |
5459 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); | |
3e4093b6 | 5460 | layout_type (type); |
8d9f82d5 | 5461 | layout_decl (cldecl, 0); |
3e4093b6 RS |
5462 | } |
5463 | } | |
400fbf9f | 5464 | } |
3e4093b6 RS |
5465 | } |
5466 | \f | |
5467 | /* Methods for storing and printing names for error messages. */ | |
400fbf9f | 5468 | |
3e4093b6 RS |
5469 | /* Implement a spelling stack that allows components of a name to be pushed |
5470 | and popped. Each element on the stack is this structure. */ | |
400fbf9f | 5471 | |
3e4093b6 RS |
5472 | struct spelling |
5473 | { | |
5474 | int kind; | |
5475 | union | |
400fbf9f | 5476 | { |
a0f0ab9f | 5477 | unsigned HOST_WIDE_INT i; |
3e4093b6 RS |
5478 | const char *s; |
5479 | } u; | |
5480 | }; | |
2f6e4e97 | 5481 | |
3e4093b6 RS |
5482 | #define SPELLING_STRING 1 |
5483 | #define SPELLING_MEMBER 2 | |
5484 | #define SPELLING_BOUNDS 3 | |
400fbf9f | 5485 | |
3e4093b6 RS |
5486 | static struct spelling *spelling; /* Next stack element (unused). */ |
5487 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
5488 | static int spelling_size; /* Size of the spelling stack. */ | |
400fbf9f | 5489 | |
3e4093b6 RS |
5490 | /* Macros to save and restore the spelling stack around push_... functions. |
5491 | Alternative to SAVE_SPELLING_STACK. */ | |
400fbf9f | 5492 | |
3e4093b6 RS |
5493 | #define SPELLING_DEPTH() (spelling - spelling_base) |
5494 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
400fbf9f | 5495 | |
3e4093b6 RS |
5496 | /* Push an element on the spelling stack with type KIND and assign VALUE |
5497 | to MEMBER. */ | |
400fbf9f | 5498 | |
3e4093b6 RS |
5499 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ |
5500 | { \ | |
5501 | int depth = SPELLING_DEPTH (); \ | |
5502 | \ | |
5503 | if (depth >= spelling_size) \ | |
5504 | { \ | |
5505 | spelling_size += 10; \ | |
cca8ead2 BI |
5506 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ |
5507 | spelling_size); \ | |
3e4093b6 RS |
5508 | RESTORE_SPELLING_DEPTH (depth); \ |
5509 | } \ | |
5510 | \ | |
5511 | spelling->kind = (KIND); \ | |
5512 | spelling->MEMBER = (VALUE); \ | |
5513 | spelling++; \ | |
5514 | } | |
400fbf9f | 5515 | |
3e4093b6 | 5516 | /* Push STRING on the stack. Printed literally. */ |
400fbf9f | 5517 | |
3e4093b6 RS |
5518 | static void |
5519 | push_string (const char *string) | |
5520 | { | |
5521 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
5522 | } | |
400fbf9f | 5523 | |
3e4093b6 | 5524 | /* Push a member name on the stack. Printed as '.' STRING. */ |
400fbf9f | 5525 | |
3e4093b6 RS |
5526 | static void |
5527 | push_member_name (tree decl) | |
5528 | { | |
5529 | const char *const string | |
88388a52 JM |
5530 | = (DECL_NAME (decl) |
5531 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
5532 | : _("<anonymous>")); | |
3e4093b6 RS |
5533 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); |
5534 | } | |
400fbf9f | 5535 | |
3e4093b6 | 5536 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ |
400fbf9f | 5537 | |
3e4093b6 | 5538 | static void |
a0f0ab9f | 5539 | push_array_bounds (unsigned HOST_WIDE_INT bounds) |
3e4093b6 RS |
5540 | { |
5541 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
5542 | } | |
bb58bec5 | 5543 | |
3e4093b6 | 5544 | /* Compute the maximum size in bytes of the printed spelling. */ |
400fbf9f | 5545 | |
3e4093b6 RS |
5546 | static int |
5547 | spelling_length (void) | |
5548 | { | |
5549 | int size = 0; | |
5550 | struct spelling *p; | |
400fbf9f | 5551 | |
3e4093b6 RS |
5552 | for (p = spelling_base; p < spelling; p++) |
5553 | { | |
5554 | if (p->kind == SPELLING_BOUNDS) | |
5555 | size += 25; | |
5556 | else | |
5557 | size += strlen (p->u.s) + 1; | |
5558 | } | |
5559 | ||
5560 | return size; | |
400fbf9f | 5561 | } |
400fbf9f | 5562 | |
3e4093b6 | 5563 | /* Print the spelling to BUFFER and return it. */ |
400fbf9f | 5564 | |
3e4093b6 RS |
5565 | static char * |
5566 | print_spelling (char *buffer) | |
400fbf9f | 5567 | { |
3e4093b6 RS |
5568 | char *d = buffer; |
5569 | struct spelling *p; | |
400fbf9f | 5570 | |
3e4093b6 RS |
5571 | for (p = spelling_base; p < spelling; p++) |
5572 | if (p->kind == SPELLING_BOUNDS) | |
5573 | { | |
a0f0ab9f | 5574 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); |
3e4093b6 RS |
5575 | d += strlen (d); |
5576 | } | |
5577 | else | |
5578 | { | |
5579 | const char *s; | |
5580 | if (p->kind == SPELLING_MEMBER) | |
5581 | *d++ = '.'; | |
5582 | for (s = p->u.s; (*d = *s++); d++) | |
5583 | ; | |
5584 | } | |
5585 | *d++ = '\0'; | |
5586 | return buffer; | |
5587 | } | |
400fbf9f | 5588 | |
3e4093b6 RS |
5589 | /* Issue an error message for a bad initializer component. |
5590 | MSGID identifies the message. | |
5591 | The component name is taken from the spelling stack. */ | |
400fbf9f | 5592 | |
3e4093b6 RS |
5593 | void |
5594 | error_init (const char *msgid) | |
5595 | { | |
5596 | char *ofwhat; | |
400fbf9f | 5597 | |
3e4093b6 | 5598 | error ("%s", _(msgid)); |
28dab132 | 5599 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5600 | if (*ofwhat) |
bda67431 | 5601 | error ("(near initialization for %qs)", ofwhat); |
3e4093b6 | 5602 | } |
400fbf9f | 5603 | |
fcf73884 MLI |
5604 | /* Issue a pedantic warning for a bad initializer component. OPT is |
5605 | the option OPT_* (from options.h) controlling this warning or 0 if | |
5606 | it is unconditionally given. MSGID identifies the message. The | |
5607 | component name is taken from the spelling stack. */ | |
400fbf9f | 5608 | |
3e4093b6 | 5609 | void |
509c9d60 | 5610 | pedwarn_init (location_t location, int opt, const char *msgid) |
3e4093b6 RS |
5611 | { |
5612 | char *ofwhat; | |
9f720c3e | 5613 | |
509c9d60 | 5614 | pedwarn (location, opt, "%s", _(msgid)); |
28dab132 | 5615 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5616 | if (*ofwhat) |
509c9d60 | 5617 | pedwarn (location, opt, "(near initialization for %qs)", ofwhat); |
3e4093b6 | 5618 | } |
9f720c3e | 5619 | |
683d6ff9 MLI |
5620 | /* Issue a warning for a bad initializer component. |
5621 | ||
5622 | OPT is the OPT_W* value corresponding to the warning option that | |
5623 | controls this warning. MSGID identifies the message. The | |
5624 | component name is taken from the spelling stack. */ | |
61179109 | 5625 | |
3e4093b6 | 5626 | static void |
683d6ff9 | 5627 | warning_init (int opt, const char *msgid) |
3e4093b6 RS |
5628 | { |
5629 | char *ofwhat; | |
7e842ef8 | 5630 | |
683d6ff9 | 5631 | warning (opt, "%s", _(msgid)); |
28dab132 | 5632 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); |
3e4093b6 | 5633 | if (*ofwhat) |
683d6ff9 | 5634 | warning (opt, "(near initialization for %qs)", ofwhat); |
3e4093b6 RS |
5635 | } |
5636 | \f | |
916c5919 JM |
5637 | /* If TYPE is an array type and EXPR is a parenthesized string |
5638 | constant, warn if pedantic that EXPR is being used to initialize an | |
5639 | object of type TYPE. */ | |
5640 | ||
5641 | void | |
5642 | maybe_warn_string_init (tree type, struct c_expr expr) | |
5643 | { | |
5644 | if (pedantic | |
5645 | && TREE_CODE (type) == ARRAY_TYPE | |
5646 | && TREE_CODE (expr.value) == STRING_CST | |
5647 | && expr.original_code != STRING_CST) | |
509c9d60 | 5648 | pedwarn_init (input_location, OPT_pedantic, |
fcf73884 | 5649 | "array initialized from parenthesized string constant"); |
916c5919 JM |
5650 | } |
5651 | ||
3e4093b6 RS |
5652 | /* Digest the parser output INIT as an initializer for type TYPE. |
5653 | Return a C expression of type TYPE to represent the initial value. | |
7e842ef8 | 5654 | |
bbbbb16a ILT |
5655 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. |
5656 | ||
928c19bb JM |
5657 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. |
5658 | ||
916c5919 JM |
5659 | If INIT is a string constant, STRICT_STRING is true if it is |
5660 | unparenthesized or we should not warn here for it being parenthesized. | |
5661 | For other types of INIT, STRICT_STRING is not used. | |
5662 | ||
c2255bc4 AH |
5663 | INIT_LOC is the location of the INIT. |
5664 | ||
3e4093b6 RS |
5665 | REQUIRE_CONSTANT requests an error if non-constant initializers or |
5666 | elements are seen. */ | |
7e842ef8 | 5667 | |
3e4093b6 | 5668 | static tree |
c2255bc4 AH |
5669 | digest_init (location_t init_loc, tree type, tree init, tree origtype, |
5670 | bool null_pointer_constant, bool strict_string, | |
5671 | int require_constant) | |
3e4093b6 RS |
5672 | { |
5673 | enum tree_code code = TREE_CODE (type); | |
5674 | tree inside_init = init; | |
8ce94e44 | 5675 | tree semantic_type = NULL_TREE; |
928c19bb | 5676 | bool maybe_const = true; |
7e842ef8 | 5677 | |
3e4093b6 | 5678 | if (type == error_mark_node |
f01da1a5 | 5679 | || !init |
3e4093b6 RS |
5680 | || init == error_mark_node |
5681 | || TREE_TYPE (init) == error_mark_node) | |
5682 | return error_mark_node; | |
7e842ef8 | 5683 | |
ed248cf7 | 5684 | STRIP_TYPE_NOPS (inside_init); |
7e842ef8 | 5685 | |
8ce94e44 JM |
5686 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) |
5687 | { | |
5688 | semantic_type = TREE_TYPE (inside_init); | |
5689 | inside_init = TREE_OPERAND (inside_init, 0); | |
5690 | } | |
928c19bb JM |
5691 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); |
5692 | inside_init = decl_constant_value_for_optimization (inside_init); | |
7e842ef8 | 5693 | |
3e4093b6 RS |
5694 | /* Initialization of an array of chars from a string constant |
5695 | optionally enclosed in braces. */ | |
7e842ef8 | 5696 | |
197463ae JM |
5697 | if (code == ARRAY_TYPE && inside_init |
5698 | && TREE_CODE (inside_init) == STRING_CST) | |
3e4093b6 RS |
5699 | { |
5700 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
197463ae JM |
5701 | /* Note that an array could be both an array of character type |
5702 | and an array of wchar_t if wchar_t is signed char or unsigned | |
5703 | char. */ | |
5704 | bool char_array = (typ1 == char_type_node | |
5705 | || typ1 == signed_char_type_node | |
5706 | || typ1 == unsigned_char_type_node); | |
5707 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
c466b2cd KVH |
5708 | bool char16_array = !!comptypes (typ1, char16_type_node); |
5709 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
5710 | ||
5711 | if (char_array || wchar_array || char16_array || char32_array) | |
7e842ef8 | 5712 | { |
916c5919 | 5713 | struct c_expr expr; |
c466b2cd | 5714 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); |
916c5919 JM |
5715 | expr.value = inside_init; |
5716 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
6866c6e8 | 5717 | expr.original_type = NULL; |
916c5919 JM |
5718 | maybe_warn_string_init (type, expr); |
5719 | ||
a45e580b | 5720 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
c2255bc4 | 5721 | pedwarn_init (init_loc, OPT_pedantic, |
a45e580b JM |
5722 | "initialization of a flexible array member"); |
5723 | ||
3e4093b6 | 5724 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), |
132da1a5 | 5725 | TYPE_MAIN_VARIANT (type))) |
3e4093b6 | 5726 | return inside_init; |
7e842ef8 | 5727 | |
c466b2cd | 5728 | if (char_array) |
3e4093b6 | 5729 | { |
c466b2cd KVH |
5730 | if (typ2 != char_type_node) |
5731 | { | |
5732 | error_init ("char-array initialized from wide string"); | |
5733 | return error_mark_node; | |
5734 | } | |
3e4093b6 | 5735 | } |
c466b2cd | 5736 | else |
3e4093b6 | 5737 | { |
c466b2cd KVH |
5738 | if (typ2 == char_type_node) |
5739 | { | |
5740 | error_init ("wide character array initialized from non-wide " | |
5741 | "string"); | |
5742 | return error_mark_node; | |
5743 | } | |
5744 | else if (!comptypes(typ1, typ2)) | |
5745 | { | |
5746 | error_init ("wide character array initialized from " | |
5747 | "incompatible wide string"); | |
5748 | return error_mark_node; | |
5749 | } | |
7e842ef8 | 5750 | } |
2f6e4e97 | 5751 | |
3e4093b6 RS |
5752 | TREE_TYPE (inside_init) = type; |
5753 | if (TYPE_DOMAIN (type) != 0 | |
5754 | && TYPE_SIZE (type) != 0 | |
5eb4df45 ILT |
5755 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) |
5756 | { | |
5757 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
5758 | ||
c466b2cd | 5759 | /* Subtract the size of a single (possibly wide) character |
3e4093b6 RS |
5760 | because it's ok to ignore the terminating null char |
5761 | that is counted in the length of the constant. */ | |
5eb4df45 ILT |
5762 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), |
5763 | (len | |
5764 | - (TYPE_PRECISION (typ1) | |
5765 | / BITS_PER_UNIT)))) | |
5766 | pedwarn_init (init_loc, 0, | |
5767 | ("initializer-string for array of chars " | |
5768 | "is too long")); | |
5769 | else if (warn_cxx_compat | |
5770 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
5771 | warning_at (init_loc, OPT_Wc___compat, | |
5772 | ("initializer-string for array chars " | |
5773 | "is too long for C++")); | |
5774 | } | |
7e842ef8 | 5775 | |
3e4093b6 | 5776 | return inside_init; |
7e842ef8 | 5777 | } |
197463ae JM |
5778 | else if (INTEGRAL_TYPE_P (typ1)) |
5779 | { | |
5780 | error_init ("array of inappropriate type initialized " | |
5781 | "from string constant"); | |
5782 | return error_mark_node; | |
5783 | } | |
7e842ef8 PE |
5784 | } |
5785 | ||
3e4093b6 RS |
5786 | /* Build a VECTOR_CST from a *constant* vector constructor. If the |
5787 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
5788 | below and handle as a constructor. */ | |
e89be13b JJ |
5789 | if (code == VECTOR_TYPE |
5790 | && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE | |
00c8e9f6 | 5791 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) |
e89be13b JJ |
5792 | && TREE_CONSTANT (inside_init)) |
5793 | { | |
5794 | if (TREE_CODE (inside_init) == VECTOR_CST | |
5795 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
5796 | TYPE_MAIN_VARIANT (type))) | |
5797 | return inside_init; | |
5798 | ||
5799 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
5800 | { | |
4038c495 GB |
5801 | unsigned HOST_WIDE_INT ix; |
5802 | tree value; | |
5803 | bool constant_p = true; | |
e89be13b JJ |
5804 | |
5805 | /* Iterate through elements and check if all constructor | |
5806 | elements are *_CSTs. */ | |
4038c495 GB |
5807 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) |
5808 | if (!CONSTANT_CLASS_P (value)) | |
5809 | { | |
5810 | constant_p = false; | |
5811 | break; | |
5812 | } | |
e89be13b | 5813 | |
4038c495 GB |
5814 | if (constant_p) |
5815 | return build_vector_from_ctor (type, | |
5816 | CONSTRUCTOR_ELTS (inside_init)); | |
e89be13b JJ |
5817 | } |
5818 | } | |
6035d635 | 5819 | |
ca085fd7 MLI |
5820 | if (warn_sequence_point) |
5821 | verify_sequence_points (inside_init); | |
5822 | ||
3e4093b6 RS |
5823 | /* Any type can be initialized |
5824 | from an expression of the same type, optionally with braces. */ | |
400fbf9f | 5825 | |
3e4093b6 RS |
5826 | if (inside_init && TREE_TYPE (inside_init) != 0 |
5827 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
132da1a5 | 5828 | TYPE_MAIN_VARIANT (type)) |
3e4093b6 | 5829 | || (code == ARRAY_TYPE |
132da1a5 | 5830 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 5831 | || (code == VECTOR_TYPE |
132da1a5 | 5832 | && comptypes (TREE_TYPE (inside_init), type)) |
3e4093b6 | 5833 | || (code == POINTER_TYPE |
3897f229 | 5834 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE |
3e4093b6 | 5835 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), |
132da1a5 | 5836 | TREE_TYPE (type))))) |
3e4093b6 RS |
5837 | { |
5838 | if (code == POINTER_TYPE) | |
b494fd98 | 5839 | { |
b494fd98 EB |
5840 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) |
5841 | { | |
f2a71bbc JM |
5842 | if (TREE_CODE (inside_init) == STRING_CST |
5843 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
c2255bc4 AH |
5844 | inside_init = array_to_pointer_conversion |
5845 | (init_loc, inside_init); | |
f2a71bbc JM |
5846 | else |
5847 | { | |
5848 | error_init ("invalid use of non-lvalue array"); | |
5849 | return error_mark_node; | |
5850 | } | |
b494fd98 | 5851 | } |
f2a71bbc | 5852 | } |
b494fd98 | 5853 | |
bae39a73 NS |
5854 | if (code == VECTOR_TYPE) |
5855 | /* Although the types are compatible, we may require a | |
5856 | conversion. */ | |
5857 | inside_init = convert (type, inside_init); | |
3e4093b6 | 5858 | |
ca58211b PB |
5859 | if (require_constant |
5860 | && (code == VECTOR_TYPE || !flag_isoc99) | |
3e4093b6 | 5861 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) |
400fbf9f | 5862 | { |
3e4093b6 RS |
5863 | /* As an extension, allow initializing objects with static storage |
5864 | duration with compound literals (which are then treated just as | |
ca58211b PB |
5865 | the brace enclosed list they contain). Also allow this for |
5866 | vectors, as we can only assign them with compound literals. */ | |
3e4093b6 RS |
5867 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); |
5868 | inside_init = DECL_INITIAL (decl); | |
400fbf9f | 5869 | } |
3e4093b6 RS |
5870 | |
5871 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
5872 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
400fbf9f | 5873 | { |
3e4093b6 RS |
5874 | error_init ("array initialized from non-constant array expression"); |
5875 | return error_mark_node; | |
400fbf9f | 5876 | } |
400fbf9f | 5877 | |
3e4093b6 RS |
5878 | /* Compound expressions can only occur here if -pedantic or |
5879 | -pedantic-errors is specified. In the later case, we always want | |
5880 | an error. In the former case, we simply want a warning. */ | |
5881 | if (require_constant && pedantic | |
5882 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
5883 | { | |
5884 | inside_init | |
5885 | = valid_compound_expr_initializer (inside_init, | |
5886 | TREE_TYPE (inside_init)); | |
5887 | if (inside_init == error_mark_node) | |
5888 | error_init ("initializer element is not constant"); | |
2f6e4e97 | 5889 | else |
c2255bc4 | 5890 | pedwarn_init (init_loc, OPT_pedantic, |
509c9d60 | 5891 | "initializer element is not constant"); |
3e4093b6 RS |
5892 | if (flag_pedantic_errors) |
5893 | inside_init = error_mark_node; | |
5894 | } | |
5895 | else if (require_constant | |
116df786 RH |
5896 | && !initializer_constant_valid_p (inside_init, |
5897 | TREE_TYPE (inside_init))) | |
3e4093b6 RS |
5898 | { |
5899 | error_init ("initializer element is not constant"); | |
5900 | inside_init = error_mark_node; | |
8b40563c | 5901 | } |
928c19bb | 5902 | else if (require_constant && !maybe_const) |
c2255bc4 | 5903 | pedwarn_init (init_loc, 0, |
928c19bb | 5904 | "initializer element is not a constant expression"); |
f735a153 | 5905 | |
8fcef540 KG |
5906 | /* Added to enable additional -Wmissing-format-attribute warnings. */ |
5907 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) | |
c2255bc4 AH |
5908 | inside_init = convert_for_assignment (init_loc, type, inside_init, |
5909 | origtype, | |
bbbbb16a | 5910 | ic_init, null_pointer_constant, |
928c19bb | 5911 | NULL_TREE, NULL_TREE, 0); |
3e4093b6 RS |
5912 | return inside_init; |
5913 | } | |
f735a153 | 5914 | |
3e4093b6 | 5915 | /* Handle scalar types, including conversions. */ |
400fbf9f | 5916 | |
ab22c1fa CF |
5917 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE |
5918 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
5919 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
400fbf9f | 5920 | { |
f2a71bbc JM |
5921 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE |
5922 | && (TREE_CODE (init) == STRING_CST | |
5923 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
c2255bc4 | 5924 | inside_init = init = array_to_pointer_conversion (init_loc, init); |
8ce94e44 JM |
5925 | if (semantic_type) |
5926 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
5927 | inside_init); | |
3e4093b6 | 5928 | inside_init |
c2255bc4 AH |
5929 | = convert_for_assignment (init_loc, type, inside_init, origtype, |
5930 | ic_init, null_pointer_constant, | |
3e4093b6 | 5931 | NULL_TREE, NULL_TREE, 0); |
2f6e4e97 | 5932 | |
3274deff JW |
5933 | /* Check to see if we have already given an error message. */ |
5934 | if (inside_init == error_mark_node) | |
5935 | ; | |
3f75a254 | 5936 | else if (require_constant && !TREE_CONSTANT (inside_init)) |
400fbf9f | 5937 | { |
3e4093b6 RS |
5938 | error_init ("initializer element is not constant"); |
5939 | inside_init = error_mark_node; | |
400fbf9f | 5940 | } |
3e4093b6 | 5941 | else if (require_constant |
116df786 RH |
5942 | && !initializer_constant_valid_p (inside_init, |
5943 | TREE_TYPE (inside_init))) | |
400fbf9f | 5944 | { |
3e4093b6 RS |
5945 | error_init ("initializer element is not computable at load time"); |
5946 | inside_init = error_mark_node; | |
400fbf9f | 5947 | } |
928c19bb | 5948 | else if (require_constant && !maybe_const) |
c2255bc4 | 5949 | pedwarn_init (init_loc, 0, |
928c19bb | 5950 | "initializer element is not a constant expression"); |
3e4093b6 RS |
5951 | |
5952 | return inside_init; | |
400fbf9f | 5953 | } |
d9fc6069 | 5954 | |
3e4093b6 | 5955 | /* Come here only for records and arrays. */ |
d9fc6069 | 5956 | |
3e4093b6 | 5957 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) |
d9fc6069 | 5958 | { |
3e4093b6 RS |
5959 | error_init ("variable-sized object may not be initialized"); |
5960 | return error_mark_node; | |
d9fc6069 | 5961 | } |
3e4093b6 RS |
5962 | |
5963 | error_init ("invalid initializer"); | |
5964 | return error_mark_node; | |
d9fc6069 | 5965 | } |
400fbf9f | 5966 | \f |
3e4093b6 | 5967 | /* Handle initializers that use braces. */ |
400fbf9f | 5968 | |
3e4093b6 RS |
5969 | /* Type of object we are accumulating a constructor for. |
5970 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
5971 | static tree constructor_type; | |
400fbf9f | 5972 | |
3e4093b6 RS |
5973 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields |
5974 | left to fill. */ | |
5975 | static tree constructor_fields; | |
400fbf9f | 5976 | |
3e4093b6 RS |
5977 | /* For an ARRAY_TYPE, this is the specified index |
5978 | at which to store the next element we get. */ | |
5979 | static tree constructor_index; | |
400fbf9f | 5980 | |
3e4093b6 RS |
5981 | /* For an ARRAY_TYPE, this is the maximum index. */ |
5982 | static tree constructor_max_index; | |
400fbf9f | 5983 | |
3e4093b6 RS |
5984 | /* For a RECORD_TYPE, this is the first field not yet written out. */ |
5985 | static tree constructor_unfilled_fields; | |
400fbf9f | 5986 | |
3e4093b6 RS |
5987 | /* For an ARRAY_TYPE, this is the index of the first element |
5988 | not yet written out. */ | |
5989 | static tree constructor_unfilled_index; | |
895ea614 | 5990 | |
3e4093b6 RS |
5991 | /* In a RECORD_TYPE, the byte index of the next consecutive field. |
5992 | This is so we can generate gaps between fields, when appropriate. */ | |
5993 | static tree constructor_bit_index; | |
10d5caec | 5994 | |
3e4093b6 RS |
5995 | /* If we are saving up the elements rather than allocating them, |
5996 | this is the list of elements so far (in reverse order, | |
5997 | most recent first). */ | |
4038c495 | 5998 | static VEC(constructor_elt,gc) *constructor_elements; |
ad47f1e5 | 5999 | |
3e4093b6 RS |
6000 | /* 1 if constructor should be incrementally stored into a constructor chain, |
6001 | 0 if all the elements should be kept in AVL tree. */ | |
6002 | static int constructor_incremental; | |
ad47f1e5 | 6003 | |
3e4093b6 RS |
6004 | /* 1 if so far this constructor's elements are all compile-time constants. */ |
6005 | static int constructor_constant; | |
ad47f1e5 | 6006 | |
3e4093b6 RS |
6007 | /* 1 if so far this constructor's elements are all valid address constants. */ |
6008 | static int constructor_simple; | |
ad47f1e5 | 6009 | |
928c19bb JM |
6010 | /* 1 if this constructor has an element that cannot be part of a |
6011 | constant expression. */ | |
6012 | static int constructor_nonconst; | |
6013 | ||
3e4093b6 RS |
6014 | /* 1 if this constructor is erroneous so far. */ |
6015 | static int constructor_erroneous; | |
d45cf215 | 6016 | |
3e4093b6 RS |
6017 | /* Structure for managing pending initializer elements, organized as an |
6018 | AVL tree. */ | |
d45cf215 | 6019 | |
3e4093b6 | 6020 | struct init_node |
d45cf215 | 6021 | { |
3e4093b6 RS |
6022 | struct init_node *left, *right; |
6023 | struct init_node *parent; | |
6024 | int balance; | |
6025 | tree purpose; | |
6026 | tree value; | |
bbbbb16a | 6027 | tree origtype; |
d45cf215 RS |
6028 | }; |
6029 | ||
3e4093b6 RS |
6030 | /* Tree of pending elements at this constructor level. |
6031 | These are elements encountered out of order | |
6032 | which belong at places we haven't reached yet in actually | |
6033 | writing the output. | |
6034 | Will never hold tree nodes across GC runs. */ | |
6035 | static struct init_node *constructor_pending_elts; | |
d45cf215 | 6036 | |
3e4093b6 RS |
6037 | /* The SPELLING_DEPTH of this constructor. */ |
6038 | static int constructor_depth; | |
d45cf215 | 6039 | |
3e4093b6 RS |
6040 | /* DECL node for which an initializer is being read. |
6041 | 0 means we are reading a constructor expression | |
6042 | such as (struct foo) {...}. */ | |
6043 | static tree constructor_decl; | |
d45cf215 | 6044 | |
3e4093b6 RS |
6045 | /* Nonzero if this is an initializer for a top-level decl. */ |
6046 | static int constructor_top_level; | |
d45cf215 | 6047 | |
3e4093b6 RS |
6048 | /* Nonzero if there were any member designators in this initializer. */ |
6049 | static int constructor_designated; | |
d45cf215 | 6050 | |
3e4093b6 RS |
6051 | /* Nesting depth of designator list. */ |
6052 | static int designator_depth; | |
d45cf215 | 6053 | |
3e4093b6 | 6054 | /* Nonzero if there were diagnosed errors in this designator list. */ |
b06df647 | 6055 | static int designator_erroneous; |
d45cf215 | 6056 | |
3e4093b6 RS |
6057 | \f |
6058 | /* This stack has a level for each implicit or explicit level of | |
6059 | structuring in the initializer, including the outermost one. It | |
6060 | saves the values of most of the variables above. */ | |
d45cf215 | 6061 | |
3e4093b6 RS |
6062 | struct constructor_range_stack; |
6063 | ||
6064 | struct constructor_stack | |
d45cf215 | 6065 | { |
3e4093b6 RS |
6066 | struct constructor_stack *next; |
6067 | tree type; | |
6068 | tree fields; | |
6069 | tree index; | |
6070 | tree max_index; | |
6071 | tree unfilled_index; | |
6072 | tree unfilled_fields; | |
6073 | tree bit_index; | |
4038c495 | 6074 | VEC(constructor_elt,gc) *elements; |
3e4093b6 RS |
6075 | struct init_node *pending_elts; |
6076 | int offset; | |
6077 | int depth; | |
916c5919 | 6078 | /* If value nonzero, this value should replace the entire |
3e4093b6 | 6079 | constructor at this level. */ |
916c5919 | 6080 | struct c_expr replacement_value; |
3e4093b6 RS |
6081 | struct constructor_range_stack *range_stack; |
6082 | char constant; | |
6083 | char simple; | |
928c19bb | 6084 | char nonconst; |
3e4093b6 RS |
6085 | char implicit; |
6086 | char erroneous; | |
6087 | char outer; | |
6088 | char incremental; | |
6089 | char designated; | |
6090 | }; | |
d45cf215 | 6091 | |
802415d1 | 6092 | static struct constructor_stack *constructor_stack; |
d45cf215 | 6093 | |
3e4093b6 RS |
6094 | /* This stack represents designators from some range designator up to |
6095 | the last designator in the list. */ | |
d45cf215 | 6096 | |
3e4093b6 RS |
6097 | struct constructor_range_stack |
6098 | { | |
6099 | struct constructor_range_stack *next, *prev; | |
6100 | struct constructor_stack *stack; | |
6101 | tree range_start; | |
6102 | tree index; | |
6103 | tree range_end; | |
6104 | tree fields; | |
6105 | }; | |
d45cf215 | 6106 | |
802415d1 | 6107 | static struct constructor_range_stack *constructor_range_stack; |
d45cf215 | 6108 | |
3e4093b6 RS |
6109 | /* This stack records separate initializers that are nested. |
6110 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
6111 | in cases like { ... (struct foo) { ... } ... }. */ | |
d45cf215 | 6112 | |
3e4093b6 | 6113 | struct initializer_stack |
d45cf215 | 6114 | { |
3e4093b6 RS |
6115 | struct initializer_stack *next; |
6116 | tree decl; | |
3e4093b6 RS |
6117 | struct constructor_stack *constructor_stack; |
6118 | struct constructor_range_stack *constructor_range_stack; | |
4038c495 | 6119 | VEC(constructor_elt,gc) *elements; |
3e4093b6 RS |
6120 | struct spelling *spelling; |
6121 | struct spelling *spelling_base; | |
6122 | int spelling_size; | |
6123 | char top_level; | |
6124 | char require_constant_value; | |
6125 | char require_constant_elements; | |
6126 | }; | |
d45cf215 | 6127 | |
802415d1 | 6128 | static struct initializer_stack *initializer_stack; |
3e4093b6 RS |
6129 | \f |
6130 | /* Prepare to parse and output the initializer for variable DECL. */ | |
400fbf9f JW |
6131 | |
6132 | void | |
a396f8ae | 6133 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) |
400fbf9f | 6134 | { |
3e4093b6 | 6135 | const char *locus; |
cceb1885 | 6136 | struct initializer_stack *p = XNEW (struct initializer_stack); |
400fbf9f | 6137 | |
3e4093b6 | 6138 | p->decl = constructor_decl; |
3e4093b6 RS |
6139 | p->require_constant_value = require_constant_value; |
6140 | p->require_constant_elements = require_constant_elements; | |
6141 | p->constructor_stack = constructor_stack; | |
6142 | p->constructor_range_stack = constructor_range_stack; | |
6143 | p->elements = constructor_elements; | |
6144 | p->spelling = spelling; | |
6145 | p->spelling_base = spelling_base; | |
6146 | p->spelling_size = spelling_size; | |
6147 | p->top_level = constructor_top_level; | |
6148 | p->next = initializer_stack; | |
6149 | initializer_stack = p; | |
400fbf9f | 6150 | |
3e4093b6 | 6151 | constructor_decl = decl; |
3e4093b6 RS |
6152 | constructor_designated = 0; |
6153 | constructor_top_level = top_level; | |
400fbf9f | 6154 | |
6f17bbcf | 6155 | if (decl != 0 && decl != error_mark_node) |
3e4093b6 RS |
6156 | { |
6157 | require_constant_value = TREE_STATIC (decl); | |
6158 | require_constant_elements | |
6159 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
6160 | /* For a scalar, you can always use any value to initialize, | |
6161 | even within braces. */ | |
6162 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
6163 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
6164 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
6165 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
88388a52 | 6166 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); |
3e4093b6 RS |
6167 | } |
6168 | else | |
6169 | { | |
6170 | require_constant_value = 0; | |
6171 | require_constant_elements = 0; | |
88388a52 | 6172 | locus = _("(anonymous)"); |
3e4093b6 | 6173 | } |
b71c7f8a | 6174 | |
3e4093b6 RS |
6175 | constructor_stack = 0; |
6176 | constructor_range_stack = 0; | |
b71c7f8a | 6177 | |
3e4093b6 RS |
6178 | missing_braces_mentioned = 0; |
6179 | ||
6180 | spelling_base = 0; | |
6181 | spelling_size = 0; | |
6182 | RESTORE_SPELLING_DEPTH (0); | |
6183 | ||
6184 | if (locus) | |
6185 | push_string (locus); | |
6186 | } | |
6187 | ||
6188 | void | |
6189 | finish_init (void) | |
b71c7f8a | 6190 | { |
3e4093b6 | 6191 | struct initializer_stack *p = initializer_stack; |
b71c7f8a | 6192 | |
3e4093b6 RS |
6193 | /* Free the whole constructor stack of this initializer. */ |
6194 | while (constructor_stack) | |
6195 | { | |
6196 | struct constructor_stack *q = constructor_stack; | |
6197 | constructor_stack = q->next; | |
6198 | free (q); | |
6199 | } | |
6200 | ||
366de0ce | 6201 | gcc_assert (!constructor_range_stack); |
3e4093b6 RS |
6202 | |
6203 | /* Pop back to the data of the outer initializer (if any). */ | |
36579663 | 6204 | free (spelling_base); |
3aeb3655 | 6205 | |
3e4093b6 | 6206 | constructor_decl = p->decl; |
3e4093b6 RS |
6207 | require_constant_value = p->require_constant_value; |
6208 | require_constant_elements = p->require_constant_elements; | |
6209 | constructor_stack = p->constructor_stack; | |
6210 | constructor_range_stack = p->constructor_range_stack; | |
6211 | constructor_elements = p->elements; | |
6212 | spelling = p->spelling; | |
6213 | spelling_base = p->spelling_base; | |
6214 | spelling_size = p->spelling_size; | |
6215 | constructor_top_level = p->top_level; | |
6216 | initializer_stack = p->next; | |
6217 | free (p); | |
b71c7f8a | 6218 | } |
400fbf9f | 6219 | \f |
3e4093b6 RS |
6220 | /* Call here when we see the initializer is surrounded by braces. |
6221 | This is instead of a call to push_init_level; | |
6222 | it is matched by a call to pop_init_level. | |
400fbf9f | 6223 | |
3e4093b6 RS |
6224 | TYPE is the type to initialize, for a constructor expression. |
6225 | For an initializer for a decl, TYPE is zero. */ | |
400fbf9f | 6226 | |
3e4093b6 RS |
6227 | void |
6228 | really_start_incremental_init (tree type) | |
400fbf9f | 6229 | { |
5d038c4c | 6230 | struct constructor_stack *p = XNEW (struct constructor_stack); |
400fbf9f | 6231 | |
3e4093b6 RS |
6232 | if (type == 0) |
6233 | type = TREE_TYPE (constructor_decl); | |
400fbf9f | 6234 | |
b6fc2cdb PB |
6235 | if (TREE_CODE (type) == VECTOR_TYPE |
6236 | && TYPE_VECTOR_OPAQUE (type)) | |
3e4093b6 | 6237 | error ("opaque vector types cannot be initialized"); |
400fbf9f | 6238 | |
3e4093b6 RS |
6239 | p->type = constructor_type; |
6240 | p->fields = constructor_fields; | |
6241 | p->index = constructor_index; | |
6242 | p->max_index = constructor_max_index; | |
6243 | p->unfilled_index = constructor_unfilled_index; | |
6244 | p->unfilled_fields = constructor_unfilled_fields; | |
6245 | p->bit_index = constructor_bit_index; | |
6246 | p->elements = constructor_elements; | |
6247 | p->constant = constructor_constant; | |
6248 | p->simple = constructor_simple; | |
928c19bb | 6249 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
6250 | p->erroneous = constructor_erroneous; |
6251 | p->pending_elts = constructor_pending_elts; | |
6252 | p->depth = constructor_depth; | |
916c5919 JM |
6253 | p->replacement_value.value = 0; |
6254 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 6255 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
6256 | p->implicit = 0; |
6257 | p->range_stack = 0; | |
6258 | p->outer = 0; | |
6259 | p->incremental = constructor_incremental; | |
6260 | p->designated = constructor_designated; | |
6261 | p->next = 0; | |
6262 | constructor_stack = p; | |
b13aca19 | 6263 | |
3e4093b6 RS |
6264 | constructor_constant = 1; |
6265 | constructor_simple = 1; | |
928c19bb | 6266 | constructor_nonconst = 0; |
3e4093b6 RS |
6267 | constructor_depth = SPELLING_DEPTH (); |
6268 | constructor_elements = 0; | |
6269 | constructor_pending_elts = 0; | |
6270 | constructor_type = type; | |
6271 | constructor_incremental = 1; | |
6272 | constructor_designated = 0; | |
6273 | designator_depth = 0; | |
b06df647 | 6274 | designator_erroneous = 0; |
400fbf9f | 6275 | |
3e4093b6 RS |
6276 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6277 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
400fbf9f | 6278 | { |
3e4093b6 RS |
6279 | constructor_fields = TYPE_FIELDS (constructor_type); |
6280 | /* Skip any nameless bit fields at the beginning. */ | |
6281 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6282 | && DECL_NAME (constructor_fields) == 0) | |
6283 | constructor_fields = TREE_CHAIN (constructor_fields); | |
05bccae2 | 6284 | |
3e4093b6 RS |
6285 | constructor_unfilled_fields = constructor_fields; |
6286 | constructor_bit_index = bitsize_zero_node; | |
400fbf9f | 6287 | } |
3e4093b6 RS |
6288 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
6289 | { | |
6290 | if (TYPE_DOMAIN (constructor_type)) | |
6291 | { | |
6292 | constructor_max_index | |
6293 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 6294 | |
3e4093b6 RS |
6295 | /* Detect non-empty initializations of zero-length arrays. */ |
6296 | if (constructor_max_index == NULL_TREE | |
6297 | && TYPE_SIZE (constructor_type)) | |
7d60be94 | 6298 | constructor_max_index = build_int_cst (NULL_TREE, -1); |
400fbf9f | 6299 | |
3e4093b6 RS |
6300 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
6301 | to initialize VLAs will cause a proper error; avoid tree | |
6302 | checking errors as well by setting a safe value. */ | |
6303 | if (constructor_max_index | |
6304 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
7d60be94 | 6305 | constructor_max_index = build_int_cst (NULL_TREE, -1); |
59c83dbf | 6306 | |
3e4093b6 RS |
6307 | constructor_index |
6308 | = convert (bitsizetype, | |
6309 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
59c83dbf | 6310 | } |
3e4093b6 | 6311 | else |
493179da JM |
6312 | { |
6313 | constructor_index = bitsize_zero_node; | |
6314 | constructor_max_index = NULL_TREE; | |
6315 | } | |
59c83dbf | 6316 | |
3e4093b6 RS |
6317 | constructor_unfilled_index = constructor_index; |
6318 | } | |
6319 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6320 | { | |
6321 | /* Vectors are like simple fixed-size arrays. */ | |
6322 | constructor_max_index = | |
7d60be94 | 6323 | build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
a0f0ab9f | 6324 | constructor_index = bitsize_zero_node; |
3e4093b6 RS |
6325 | constructor_unfilled_index = constructor_index; |
6326 | } | |
6327 | else | |
6328 | { | |
6329 | /* Handle the case of int x = {5}; */ | |
6330 | constructor_fields = constructor_type; | |
6331 | constructor_unfilled_fields = constructor_type; | |
6332 | } | |
6333 | } | |
6334 | \f | |
6335 | /* Push down into a subobject, for initialization. | |
6336 | If this is for an explicit set of braces, IMPLICIT is 0. | |
6337 | If it is because the next element belongs at a lower level, | |
6338 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
400fbf9f | 6339 | |
3e4093b6 RS |
6340 | void |
6341 | push_init_level (int implicit) | |
6342 | { | |
6343 | struct constructor_stack *p; | |
6344 | tree value = NULL_TREE; | |
400fbf9f | 6345 | |
3e4093b6 | 6346 | /* If we've exhausted any levels that didn't have braces, |
472d98b4 JM |
6347 | pop them now. If implicit == 1, this will have been done in |
6348 | process_init_element; do not repeat it here because in the case | |
6349 | of excess initializers for an empty aggregate this leads to an | |
6350 | infinite cycle of popping a level and immediately recreating | |
6351 | it. */ | |
6352 | if (implicit != 1) | |
3e4093b6 | 6353 | { |
472d98b4 JM |
6354 | while (constructor_stack->implicit) |
6355 | { | |
6356 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6357 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6358 | && constructor_fields == 0) | |
b295aee2 | 6359 | process_init_element (pop_init_level (1), true); |
472d98b4 JM |
6360 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE |
6361 | && constructor_max_index | |
6362 | && tree_int_cst_lt (constructor_max_index, | |
6363 | constructor_index)) | |
b295aee2 | 6364 | process_init_element (pop_init_level (1), true); |
472d98b4 JM |
6365 | else |
6366 | break; | |
6367 | } | |
3e4093b6 | 6368 | } |
400fbf9f | 6369 | |
3e4093b6 RS |
6370 | /* Unless this is an explicit brace, we need to preserve previous |
6371 | content if any. */ | |
6372 | if (implicit) | |
6373 | { | |
6374 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6375 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6376 | && constructor_fields) | |
6377 | value = find_init_member (constructor_fields); | |
6378 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6379 | value = find_init_member (constructor_index); | |
400fbf9f JW |
6380 | } |
6381 | ||
5d038c4c | 6382 | p = XNEW (struct constructor_stack); |
3e4093b6 RS |
6383 | p->type = constructor_type; |
6384 | p->fields = constructor_fields; | |
6385 | p->index = constructor_index; | |
6386 | p->max_index = constructor_max_index; | |
6387 | p->unfilled_index = constructor_unfilled_index; | |
6388 | p->unfilled_fields = constructor_unfilled_fields; | |
6389 | p->bit_index = constructor_bit_index; | |
6390 | p->elements = constructor_elements; | |
6391 | p->constant = constructor_constant; | |
6392 | p->simple = constructor_simple; | |
928c19bb | 6393 | p->nonconst = constructor_nonconst; |
3e4093b6 RS |
6394 | p->erroneous = constructor_erroneous; |
6395 | p->pending_elts = constructor_pending_elts; | |
6396 | p->depth = constructor_depth; | |
916c5919 JM |
6397 | p->replacement_value.value = 0; |
6398 | p->replacement_value.original_code = ERROR_MARK; | |
6866c6e8 | 6399 | p->replacement_value.original_type = NULL; |
3e4093b6 RS |
6400 | p->implicit = implicit; |
6401 | p->outer = 0; | |
6402 | p->incremental = constructor_incremental; | |
6403 | p->designated = constructor_designated; | |
6404 | p->next = constructor_stack; | |
6405 | p->range_stack = 0; | |
6406 | constructor_stack = p; | |
400fbf9f | 6407 | |
3e4093b6 RS |
6408 | constructor_constant = 1; |
6409 | constructor_simple = 1; | |
928c19bb | 6410 | constructor_nonconst = 0; |
3e4093b6 RS |
6411 | constructor_depth = SPELLING_DEPTH (); |
6412 | constructor_elements = 0; | |
6413 | constructor_incremental = 1; | |
6414 | constructor_designated = 0; | |
6415 | constructor_pending_elts = 0; | |
6416 | if (!implicit) | |
400fbf9f | 6417 | { |
3e4093b6 RS |
6418 | p->range_stack = constructor_range_stack; |
6419 | constructor_range_stack = 0; | |
6420 | designator_depth = 0; | |
b06df647 | 6421 | designator_erroneous = 0; |
3e4093b6 | 6422 | } |
400fbf9f | 6423 | |
3e4093b6 RS |
6424 | /* Don't die if an entire brace-pair level is superfluous |
6425 | in the containing level. */ | |
6426 | if (constructor_type == 0) | |
6427 | ; | |
6428 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6429 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6430 | { | |
6431 | /* Don't die if there are extra init elts at the end. */ | |
6432 | if (constructor_fields == 0) | |
6433 | constructor_type = 0; | |
6434 | else | |
400fbf9f | 6435 | { |
3e4093b6 RS |
6436 | constructor_type = TREE_TYPE (constructor_fields); |
6437 | push_member_name (constructor_fields); | |
6438 | constructor_depth++; | |
400fbf9f | 6439 | } |
3e4093b6 RS |
6440 | } |
6441 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6442 | { | |
6443 | constructor_type = TREE_TYPE (constructor_type); | |
a0f0ab9f | 6444 | push_array_bounds (tree_low_cst (constructor_index, 1)); |
3e4093b6 | 6445 | constructor_depth++; |
400fbf9f JW |
6446 | } |
6447 | ||
3e4093b6 | 6448 | if (constructor_type == 0) |
400fbf9f | 6449 | { |
3e4093b6 RS |
6450 | error_init ("extra brace group at end of initializer"); |
6451 | constructor_fields = 0; | |
6452 | constructor_unfilled_fields = 0; | |
6453 | return; | |
400fbf9f JW |
6454 | } |
6455 | ||
3e4093b6 RS |
6456 | if (value && TREE_CODE (value) == CONSTRUCTOR) |
6457 | { | |
6458 | constructor_constant = TREE_CONSTANT (value); | |
6459 | constructor_simple = TREE_STATIC (value); | |
928c19bb | 6460 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); |
3e4093b6 | 6461 | constructor_elements = CONSTRUCTOR_ELTS (value); |
4038c495 | 6462 | if (!VEC_empty (constructor_elt, constructor_elements) |
3e4093b6 RS |
6463 | && (TREE_CODE (constructor_type) == RECORD_TYPE |
6464 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
6465 | set_nonincremental_init (); | |
6466 | } | |
400fbf9f | 6467 | |
3e4093b6 RS |
6468 | if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) |
6469 | { | |
6470 | missing_braces_mentioned = 1; | |
683d6ff9 | 6471 | warning_init (OPT_Wmissing_braces, "missing braces around initializer"); |
3e4093b6 | 6472 | } |
400fbf9f | 6473 | |
3e4093b6 RS |
6474 | if (TREE_CODE (constructor_type) == RECORD_TYPE |
6475 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6476 | { | |
6477 | constructor_fields = TYPE_FIELDS (constructor_type); | |
6478 | /* Skip any nameless bit fields at the beginning. */ | |
6479 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6480 | && DECL_NAME (constructor_fields) == 0) | |
6481 | constructor_fields = TREE_CHAIN (constructor_fields); | |
103b7b17 | 6482 | |
3e4093b6 RS |
6483 | constructor_unfilled_fields = constructor_fields; |
6484 | constructor_bit_index = bitsize_zero_node; | |
6485 | } | |
6486 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6487 | { | |
6488 | /* Vectors are like simple fixed-size arrays. */ | |
6489 | constructor_max_index = | |
7d60be94 | 6490 | build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1); |
3e4093b6 RS |
6491 | constructor_index = convert (bitsizetype, integer_zero_node); |
6492 | constructor_unfilled_index = constructor_index; | |
6493 | } | |
6494 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6495 | { | |
6496 | if (TYPE_DOMAIN (constructor_type)) | |
6497 | { | |
6498 | constructor_max_index | |
6499 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
400fbf9f | 6500 | |
3e4093b6 RS |
6501 | /* Detect non-empty initializations of zero-length arrays. */ |
6502 | if (constructor_max_index == NULL_TREE | |
6503 | && TYPE_SIZE (constructor_type)) | |
7d60be94 | 6504 | constructor_max_index = build_int_cst (NULL_TREE, -1); |
de520661 | 6505 | |
3e4093b6 RS |
6506 | /* constructor_max_index needs to be an INTEGER_CST. Attempts |
6507 | to initialize VLAs will cause a proper error; avoid tree | |
6508 | checking errors as well by setting a safe value. */ | |
6509 | if (constructor_max_index | |
6510 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
7d60be94 | 6511 | constructor_max_index = build_int_cst (NULL_TREE, -1); |
b62acd60 | 6512 | |
3e4093b6 RS |
6513 | constructor_index |
6514 | = convert (bitsizetype, | |
6515 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
6516 | } | |
6517 | else | |
6518 | constructor_index = bitsize_zero_node; | |
de520661 | 6519 | |
3e4093b6 RS |
6520 | constructor_unfilled_index = constructor_index; |
6521 | if (value && TREE_CODE (value) == STRING_CST) | |
6522 | { | |
6523 | /* We need to split the char/wchar array into individual | |
6524 | characters, so that we don't have to special case it | |
6525 | everywhere. */ | |
6526 | set_nonincremental_init_from_string (value); | |
6527 | } | |
6528 | } | |
6529 | else | |
6530 | { | |
b4519d39 | 6531 | if (constructor_type != error_mark_node) |
683d6ff9 | 6532 | warning_init (0, "braces around scalar initializer"); |
3e4093b6 RS |
6533 | constructor_fields = constructor_type; |
6534 | constructor_unfilled_fields = constructor_type; | |
6535 | } | |
6536 | } | |
8b6a5902 | 6537 | |
3e4093b6 | 6538 | /* At the end of an implicit or explicit brace level, |
916c5919 JM |
6539 | finish up that level of constructor. If a single expression |
6540 | with redundant braces initialized that level, return the | |
6541 | c_expr structure for that expression. Otherwise, the original_code | |
6542 | element is set to ERROR_MARK. | |
6543 | If we were outputting the elements as they are read, return 0 as the value | |
3e4093b6 | 6544 | from inner levels (process_init_element ignores that), |
916c5919 | 6545 | but return error_mark_node as the value from the outermost level |
3e4093b6 | 6546 | (that's what we want to put in DECL_INITIAL). |
916c5919 | 6547 | Otherwise, return a CONSTRUCTOR expression as the value. */ |
de520661 | 6548 | |
916c5919 | 6549 | struct c_expr |
3e4093b6 RS |
6550 | pop_init_level (int implicit) |
6551 | { | |
6552 | struct constructor_stack *p; | |
916c5919 JM |
6553 | struct c_expr ret; |
6554 | ret.value = 0; | |
6555 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 6556 | ret.original_type = NULL; |
de520661 | 6557 | |
3e4093b6 RS |
6558 | if (implicit == 0) |
6559 | { | |
6560 | /* When we come to an explicit close brace, | |
6561 | pop any inner levels that didn't have explicit braces. */ | |
6562 | while (constructor_stack->implicit) | |
b295aee2 | 6563 | process_init_element (pop_init_level (1), true); |
de520661 | 6564 | |
366de0ce | 6565 | gcc_assert (!constructor_range_stack); |
3e4093b6 | 6566 | } |
e5e809f4 | 6567 | |
0066ef9c RH |
6568 | /* Now output all pending elements. */ |
6569 | constructor_incremental = 1; | |
6570 | output_pending_init_elements (1); | |
6571 | ||
3e4093b6 | 6572 | p = constructor_stack; |
e5e809f4 | 6573 | |
3e4093b6 RS |
6574 | /* Error for initializing a flexible array member, or a zero-length |
6575 | array member in an inappropriate context. */ | |
6576 | if (constructor_type && constructor_fields | |
6577 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
6578 | && TYPE_DOMAIN (constructor_type) | |
3f75a254 | 6579 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) |
3e4093b6 RS |
6580 | { |
6581 | /* Silently discard empty initializations. The parser will | |
6582 | already have pedwarned for empty brackets. */ | |
6583 | if (integer_zerop (constructor_unfilled_index)) | |
6584 | constructor_type = NULL_TREE; | |
366de0ce | 6585 | else |
3e4093b6 | 6586 | { |
366de0ce | 6587 | gcc_assert (!TYPE_SIZE (constructor_type)); |
c22cacf3 | 6588 | |
3e4093b6 RS |
6589 | if (constructor_depth > 2) |
6590 | error_init ("initialization of flexible array member in a nested context"); | |
fcf73884 | 6591 | else |
509c9d60 MLI |
6592 | pedwarn_init (input_location, OPT_pedantic, |
6593 | "initialization of a flexible array member"); | |
de520661 | 6594 | |
3e4093b6 RS |
6595 | /* We have already issued an error message for the existence |
6596 | of a flexible array member not at the end of the structure. | |
535a42b1 | 6597 | Discard the initializer so that we do not die later. */ |
3e4093b6 RS |
6598 | if (TREE_CHAIN (constructor_fields) != NULL_TREE) |
6599 | constructor_type = NULL_TREE; | |
6600 | } | |
3e4093b6 | 6601 | } |
de520661 | 6602 | |
3e4093b6 | 6603 | /* Warn when some struct elements are implicitly initialized to zero. */ |
eaac4679 | 6604 | if (warn_missing_field_initializers |
3e4093b6 RS |
6605 | && constructor_type |
6606 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
6607 | && constructor_unfilled_fields) | |
6608 | { | |
6609 | /* Do not warn for flexible array members or zero-length arrays. */ | |
6610 | while (constructor_unfilled_fields | |
3f75a254 | 6611 | && (!DECL_SIZE (constructor_unfilled_fields) |
3e4093b6 RS |
6612 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) |
6613 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
cc77d4d5 | 6614 | |
3e4093b6 RS |
6615 | /* Do not warn if this level of the initializer uses member |
6616 | designators; it is likely to be deliberate. */ | |
6617 | if (constructor_unfilled_fields && !constructor_designated) | |
6618 | { | |
6619 | push_member_name (constructor_unfilled_fields); | |
683d6ff9 MLI |
6620 | warning_init (OPT_Wmissing_field_initializers, |
6621 | "missing initializer"); | |
3e4093b6 RS |
6622 | RESTORE_SPELLING_DEPTH (constructor_depth); |
6623 | } | |
6624 | } | |
de520661 | 6625 | |
3e4093b6 | 6626 | /* Pad out the end of the structure. */ |
916c5919 | 6627 | if (p->replacement_value.value) |
3e4093b6 RS |
6628 | /* If this closes a superfluous brace pair, |
6629 | just pass out the element between them. */ | |
916c5919 | 6630 | ret = p->replacement_value; |
3e4093b6 RS |
6631 | else if (constructor_type == 0) |
6632 | ; | |
6633 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
6634 | && TREE_CODE (constructor_type) != UNION_TYPE | |
6635 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
6636 | && TREE_CODE (constructor_type) != VECTOR_TYPE) | |
6637 | { | |
6638 | /* A nonincremental scalar initializer--just return | |
6639 | the element, after verifying there is just one. */ | |
4038c495 | 6640 | if (VEC_empty (constructor_elt,constructor_elements)) |
3e4093b6 RS |
6641 | { |
6642 | if (!constructor_erroneous) | |
6643 | error_init ("empty scalar initializer"); | |
916c5919 | 6644 | ret.value = error_mark_node; |
3e4093b6 | 6645 | } |
4038c495 | 6646 | else if (VEC_length (constructor_elt,constructor_elements) != 1) |
3e4093b6 RS |
6647 | { |
6648 | error_init ("extra elements in scalar initializer"); | |
4038c495 | 6649 | ret.value = VEC_index (constructor_elt,constructor_elements,0)->value; |
3e4093b6 RS |
6650 | } |
6651 | else | |
4038c495 | 6652 | ret.value = VEC_index (constructor_elt,constructor_elements,0)->value; |
3e4093b6 RS |
6653 | } |
6654 | else | |
6655 | { | |
6656 | if (constructor_erroneous) | |
916c5919 | 6657 | ret.value = error_mark_node; |
3e4093b6 RS |
6658 | else |
6659 | { | |
916c5919 | 6660 | ret.value = build_constructor (constructor_type, |
4038c495 | 6661 | constructor_elements); |
3e4093b6 | 6662 | if (constructor_constant) |
51eed280 | 6663 | TREE_CONSTANT (ret.value) = 1; |
3e4093b6 | 6664 | if (constructor_constant && constructor_simple) |
916c5919 | 6665 | TREE_STATIC (ret.value) = 1; |
928c19bb JM |
6666 | if (constructor_nonconst) |
6667 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
3e4093b6 RS |
6668 | } |
6669 | } | |
de520661 | 6670 | |
928c19bb JM |
6671 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) |
6672 | { | |
6673 | if (constructor_nonconst) | |
6674 | ret.original_code = C_MAYBE_CONST_EXPR; | |
6675 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
6676 | ret.original_code = ERROR_MARK; | |
6677 | } | |
6678 | ||
3e4093b6 RS |
6679 | constructor_type = p->type; |
6680 | constructor_fields = p->fields; | |
6681 | constructor_index = p->index; | |
6682 | constructor_max_index = p->max_index; | |
6683 | constructor_unfilled_index = p->unfilled_index; | |
6684 | constructor_unfilled_fields = p->unfilled_fields; | |
6685 | constructor_bit_index = p->bit_index; | |
6686 | constructor_elements = p->elements; | |
6687 | constructor_constant = p->constant; | |
6688 | constructor_simple = p->simple; | |
928c19bb | 6689 | constructor_nonconst = p->nonconst; |
3e4093b6 RS |
6690 | constructor_erroneous = p->erroneous; |
6691 | constructor_incremental = p->incremental; | |
6692 | constructor_designated = p->designated; | |
6693 | constructor_pending_elts = p->pending_elts; | |
6694 | constructor_depth = p->depth; | |
6695 | if (!p->implicit) | |
6696 | constructor_range_stack = p->range_stack; | |
6697 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
de520661 | 6698 | |
3e4093b6 RS |
6699 | constructor_stack = p->next; |
6700 | free (p); | |
b621a4dd | 6701 | |
5d5e98dc VR |
6702 | if (ret.value == 0 && constructor_stack == 0) |
6703 | ret.value = error_mark_node; | |
916c5919 | 6704 | return ret; |
3e4093b6 | 6705 | } |
8b6a5902 | 6706 | |
3e4093b6 RS |
6707 | /* Common handling for both array range and field name designators. |
6708 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
400fbf9f | 6709 | |
3e4093b6 RS |
6710 | static int |
6711 | set_designator (int array) | |
de520661 | 6712 | { |
3e4093b6 RS |
6713 | tree subtype; |
6714 | enum tree_code subcode; | |
de520661 | 6715 | |
3e4093b6 RS |
6716 | /* Don't die if an entire brace-pair level is superfluous |
6717 | in the containing level. */ | |
6718 | if (constructor_type == 0) | |
6719 | return 1; | |
de520661 | 6720 | |
366de0ce NS |
6721 | /* If there were errors in this designator list already, bail out |
6722 | silently. */ | |
b06df647 | 6723 | if (designator_erroneous) |
3e4093b6 | 6724 | return 1; |
e28cae4f | 6725 | |
3e4093b6 RS |
6726 | if (!designator_depth) |
6727 | { | |
366de0ce | 6728 | gcc_assert (!constructor_range_stack); |
de520661 | 6729 | |
3e4093b6 RS |
6730 | /* Designator list starts at the level of closest explicit |
6731 | braces. */ | |
6732 | while (constructor_stack->implicit) | |
b295aee2 | 6733 | process_init_element (pop_init_level (1), true); |
3e4093b6 RS |
6734 | constructor_designated = 1; |
6735 | return 0; | |
6736 | } | |
de520661 | 6737 | |
366de0ce | 6738 | switch (TREE_CODE (constructor_type)) |
3c3fa147 | 6739 | { |
366de0ce NS |
6740 | case RECORD_TYPE: |
6741 | case UNION_TYPE: | |
3e4093b6 RS |
6742 | subtype = TREE_TYPE (constructor_fields); |
6743 | if (subtype != error_mark_node) | |
6744 | subtype = TYPE_MAIN_VARIANT (subtype); | |
366de0ce NS |
6745 | break; |
6746 | case ARRAY_TYPE: | |
3e4093b6 | 6747 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); |
366de0ce NS |
6748 | break; |
6749 | default: | |
6750 | gcc_unreachable (); | |
de520661 | 6751 | } |
400fbf9f | 6752 | |
3e4093b6 RS |
6753 | subcode = TREE_CODE (subtype); |
6754 | if (array && subcode != ARRAY_TYPE) | |
6755 | { | |
6756 | error_init ("array index in non-array initializer"); | |
6757 | return 1; | |
6758 | } | |
6759 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
6760 | { | |
6761 | error_init ("field name not in record or union initializer"); | |
6762 | return 1; | |
6763 | } | |
d45cf215 | 6764 | |
3e4093b6 RS |
6765 | constructor_designated = 1; |
6766 | push_init_level (2); | |
6767 | return 0; | |
de520661 | 6768 | } |
400fbf9f | 6769 | |
3e4093b6 RS |
6770 | /* If there are range designators in designator list, push a new designator |
6771 | to constructor_range_stack. RANGE_END is end of such stack range or | |
6772 | NULL_TREE if there is no range designator at this level. */ | |
400fbf9f | 6773 | |
3e4093b6 RS |
6774 | static void |
6775 | push_range_stack (tree range_end) | |
6776 | { | |
6777 | struct constructor_range_stack *p; | |
400fbf9f | 6778 | |
5d038c4c | 6779 | p = GGC_NEW (struct constructor_range_stack); |
3e4093b6 RS |
6780 | p->prev = constructor_range_stack; |
6781 | p->next = 0; | |
6782 | p->fields = constructor_fields; | |
6783 | p->range_start = constructor_index; | |
6784 | p->index = constructor_index; | |
6785 | p->stack = constructor_stack; | |
6786 | p->range_end = range_end; | |
8b6a5902 | 6787 | if (constructor_range_stack) |
3e4093b6 RS |
6788 | constructor_range_stack->next = p; |
6789 | constructor_range_stack = p; | |
de520661 | 6790 | } |
400fbf9f | 6791 | |
3e4093b6 RS |
6792 | /* Within an array initializer, specify the next index to be initialized. |
6793 | FIRST is that index. If LAST is nonzero, then initialize a range | |
6794 | of indices, running from FIRST through LAST. */ | |
5a7ec9d9 | 6795 | |
de520661 | 6796 | void |
3e4093b6 | 6797 | set_init_index (tree first, tree last) |
de520661 | 6798 | { |
3e4093b6 RS |
6799 | if (set_designator (1)) |
6800 | return; | |
de520661 | 6801 | |
b06df647 | 6802 | designator_erroneous = 1; |
de520661 | 6803 | |
3ea8cd06 JM |
6804 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) |
6805 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
6806 | { | |
6807 | error_init ("array index in initializer not of integer type"); | |
6808 | return; | |
6809 | } | |
6810 | ||
2b6da65c JM |
6811 | if (TREE_CODE (first) != INTEGER_CST) |
6812 | { | |
6813 | first = c_fully_fold (first, false, NULL); | |
6814 | if (TREE_CODE (first) == INTEGER_CST) | |
6815 | pedwarn_init (input_location, OPT_pedantic, | |
6816 | "array index in initializer is not " | |
6817 | "an integer constant expression"); | |
6818 | } | |
6819 | ||
6820 | if (last && TREE_CODE (last) != INTEGER_CST) | |
6821 | { | |
6822 | last = c_fully_fold (last, false, NULL); | |
6823 | if (TREE_CODE (last) == INTEGER_CST) | |
6824 | pedwarn_init (input_location, OPT_pedantic, | |
6825 | "array index in initializer is not " | |
6826 | "an integer constant expression"); | |
6827 | } | |
6828 | ||
3e4093b6 RS |
6829 | if (TREE_CODE (first) != INTEGER_CST) |
6830 | error_init ("nonconstant array index in initializer"); | |
6831 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) | |
6832 | error_init ("nonconstant array index in initializer"); | |
6833 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) | |
6834 | error_init ("array index in non-array initializer"); | |
622adc7e MK |
6835 | else if (tree_int_cst_sgn (first) == -1) |
6836 | error_init ("array index in initializer exceeds array bounds"); | |
3e4093b6 RS |
6837 | else if (constructor_max_index |
6838 | && tree_int_cst_lt (constructor_max_index, first)) | |
6839 | error_init ("array index in initializer exceeds array bounds"); | |
6840 | else | |
de520661 | 6841 | { |
928c19bb JM |
6842 | constant_expression_warning (first); |
6843 | if (last) | |
6844 | constant_expression_warning (last); | |
3e4093b6 | 6845 | constructor_index = convert (bitsizetype, first); |
665f2503 | 6846 | |
3e4093b6 | 6847 | if (last) |
2bede729 | 6848 | { |
3e4093b6 RS |
6849 | if (tree_int_cst_equal (first, last)) |
6850 | last = 0; | |
6851 | else if (tree_int_cst_lt (last, first)) | |
6852 | { | |
6853 | error_init ("empty index range in initializer"); | |
6854 | last = 0; | |
6855 | } | |
6856 | else | |
6857 | { | |
6858 | last = convert (bitsizetype, last); | |
6859 | if (constructor_max_index != 0 | |
6860 | && tree_int_cst_lt (constructor_max_index, last)) | |
6861 | { | |
6862 | error_init ("array index range in initializer exceeds array bounds"); | |
6863 | last = 0; | |
6864 | } | |
6865 | } | |
2bede729 | 6866 | } |
fed3cef0 | 6867 | |
3e4093b6 | 6868 | designator_depth++; |
b06df647 | 6869 | designator_erroneous = 0; |
3e4093b6 RS |
6870 | if (constructor_range_stack || last) |
6871 | push_range_stack (last); | |
de520661 | 6872 | } |
de520661 | 6873 | } |
3e4093b6 RS |
6874 | |
6875 | /* Within a struct initializer, specify the next field to be initialized. */ | |
400fbf9f | 6876 | |
de520661 | 6877 | void |
3e4093b6 | 6878 | set_init_label (tree fieldname) |
de520661 | 6879 | { |
3e4093b6 | 6880 | tree tail; |
94ba5069 | 6881 | |
3e4093b6 RS |
6882 | if (set_designator (0)) |
6883 | return; | |
6884 | ||
b06df647 | 6885 | designator_erroneous = 1; |
3e4093b6 RS |
6886 | |
6887 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
6888 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
94ba5069 | 6889 | { |
3e4093b6 RS |
6890 | error_init ("field name not in record or union initializer"); |
6891 | return; | |
94ba5069 RS |
6892 | } |
6893 | ||
3e4093b6 RS |
6894 | for (tail = TYPE_FIELDS (constructor_type); tail; |
6895 | tail = TREE_CHAIN (tail)) | |
8b6a5902 | 6896 | { |
3e4093b6 RS |
6897 | if (DECL_NAME (tail) == fieldname) |
6898 | break; | |
8b6a5902 JJ |
6899 | } |
6900 | ||
3e4093b6 | 6901 | if (tail == 0) |
c51a1ba9 | 6902 | error ("unknown field %qE specified in initializer", fieldname); |
3e4093b6 | 6903 | else |
8b6a5902 | 6904 | { |
3e4093b6 RS |
6905 | constructor_fields = tail; |
6906 | designator_depth++; | |
b06df647 | 6907 | designator_erroneous = 0; |
3e4093b6 RS |
6908 | if (constructor_range_stack) |
6909 | push_range_stack (NULL_TREE); | |
8b6a5902 | 6910 | } |
3e4093b6 RS |
6911 | } |
6912 | \f | |
6913 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
6914 | identifies the initializer, either array index or field in a structure. | |
bbbbb16a ILT |
6915 | VALUE is the value of that index or field. If ORIGTYPE is not |
6916 | NULL_TREE, it is the original type of VALUE. | |
b295aee2 JJ |
6917 | |
6918 | IMPLICIT is true if value comes from pop_init_level (1), | |
6919 | the new initializer has been merged with the existing one | |
6920 | and thus no warnings should be emitted about overriding an | |
6921 | existing initializer. */ | |
de520661 | 6922 | |
3e4093b6 | 6923 | static void |
bbbbb16a | 6924 | add_pending_init (tree purpose, tree value, tree origtype, bool implicit) |
3e4093b6 RS |
6925 | { |
6926 | struct init_node *p, **q, *r; | |
6927 | ||
6928 | q = &constructor_pending_elts; | |
6929 | p = 0; | |
6930 | ||
6931 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
de520661 | 6932 | { |
3e4093b6 | 6933 | while (*q != 0) |
91fa3c30 | 6934 | { |
3e4093b6 RS |
6935 | p = *q; |
6936 | if (tree_int_cst_lt (purpose, p->purpose)) | |
6937 | q = &p->left; | |
6938 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
6939 | q = &p->right; | |
6940 | else | |
6941 | { | |
b295aee2 JJ |
6942 | if (!implicit) |
6943 | { | |
6944 | if (TREE_SIDE_EFFECTS (p->value)) | |
6945 | warning_init (0, "initialized field with side-effects overwritten"); | |
6946 | else if (warn_override_init) | |
6947 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
6948 | } | |
3e4093b6 | 6949 | p->value = value; |
bbbbb16a | 6950 | p->origtype = origtype; |
3e4093b6 RS |
6951 | return; |
6952 | } | |
91fa3c30 | 6953 | } |
de520661 | 6954 | } |
3e4093b6 | 6955 | else |
de520661 | 6956 | { |
3e4093b6 | 6957 | tree bitpos; |
400fbf9f | 6958 | |
3e4093b6 RS |
6959 | bitpos = bit_position (purpose); |
6960 | while (*q != NULL) | |
6961 | { | |
6962 | p = *q; | |
6963 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
6964 | q = &p->left; | |
6965 | else if (p->purpose != purpose) | |
6966 | q = &p->right; | |
6967 | else | |
6968 | { | |
b295aee2 JJ |
6969 | if (!implicit) |
6970 | { | |
6971 | if (TREE_SIDE_EFFECTS (p->value)) | |
6972 | warning_init (0, "initialized field with side-effects overwritten"); | |
6973 | else if (warn_override_init) | |
6974 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
6975 | } | |
3e4093b6 | 6976 | p->value = value; |
bbbbb16a | 6977 | p->origtype = origtype; |
3e4093b6 RS |
6978 | return; |
6979 | } | |
6980 | } | |
91fa3c30 | 6981 | } |
b71c7f8a | 6982 | |
5d038c4c | 6983 | r = GGC_NEW (struct init_node); |
3e4093b6 RS |
6984 | r->purpose = purpose; |
6985 | r->value = value; | |
bbbbb16a | 6986 | r->origtype = origtype; |
8b6a5902 | 6987 | |
3e4093b6 RS |
6988 | *q = r; |
6989 | r->parent = p; | |
6990 | r->left = 0; | |
6991 | r->right = 0; | |
6992 | r->balance = 0; | |
b71c7f8a | 6993 | |
3e4093b6 | 6994 | while (p) |
de520661 | 6995 | { |
3e4093b6 | 6996 | struct init_node *s; |
665f2503 | 6997 | |
3e4093b6 | 6998 | if (r == p->left) |
2bede729 | 6999 | { |
3e4093b6 RS |
7000 | if (p->balance == 0) |
7001 | p->balance = -1; | |
7002 | else if (p->balance < 0) | |
7003 | { | |
7004 | if (r->balance < 0) | |
7005 | { | |
7006 | /* L rotation. */ | |
7007 | p->left = r->right; | |
7008 | if (p->left) | |
7009 | p->left->parent = p; | |
7010 | r->right = p; | |
e7b6a0ee | 7011 | |
3e4093b6 RS |
7012 | p->balance = 0; |
7013 | r->balance = 0; | |
39bc99c2 | 7014 | |
3e4093b6 RS |
7015 | s = p->parent; |
7016 | p->parent = r; | |
7017 | r->parent = s; | |
7018 | if (s) | |
7019 | { | |
7020 | if (s->left == p) | |
7021 | s->left = r; | |
7022 | else | |
7023 | s->right = r; | |
7024 | } | |
7025 | else | |
7026 | constructor_pending_elts = r; | |
7027 | } | |
7028 | else | |
7029 | { | |
7030 | /* LR rotation. */ | |
7031 | struct init_node *t = r->right; | |
e7b6a0ee | 7032 | |
3e4093b6 RS |
7033 | r->right = t->left; |
7034 | if (r->right) | |
7035 | r->right->parent = r; | |
7036 | t->left = r; | |
7037 | ||
7038 | p->left = t->right; | |
7039 | if (p->left) | |
7040 | p->left->parent = p; | |
7041 | t->right = p; | |
7042 | ||
7043 | p->balance = t->balance < 0; | |
7044 | r->balance = -(t->balance > 0); | |
7045 | t->balance = 0; | |
7046 | ||
7047 | s = p->parent; | |
7048 | p->parent = t; | |
7049 | r->parent = t; | |
7050 | t->parent = s; | |
7051 | if (s) | |
7052 | { | |
7053 | if (s->left == p) | |
7054 | s->left = t; | |
7055 | else | |
7056 | s->right = t; | |
7057 | } | |
7058 | else | |
7059 | constructor_pending_elts = t; | |
7060 | } | |
7061 | break; | |
7062 | } | |
7063 | else | |
7064 | { | |
7065 | /* p->balance == +1; growth of left side balances the node. */ | |
7066 | p->balance = 0; | |
7067 | break; | |
7068 | } | |
2bede729 | 7069 | } |
3e4093b6 RS |
7070 | else /* r == p->right */ |
7071 | { | |
7072 | if (p->balance == 0) | |
7073 | /* Growth propagation from right side. */ | |
7074 | p->balance++; | |
7075 | else if (p->balance > 0) | |
7076 | { | |
7077 | if (r->balance > 0) | |
7078 | { | |
7079 | /* R rotation. */ | |
7080 | p->right = r->left; | |
7081 | if (p->right) | |
7082 | p->right->parent = p; | |
7083 | r->left = p; | |
7084 | ||
7085 | p->balance = 0; | |
7086 | r->balance = 0; | |
7087 | ||
7088 | s = p->parent; | |
7089 | p->parent = r; | |
7090 | r->parent = s; | |
7091 | if (s) | |
7092 | { | |
7093 | if (s->left == p) | |
7094 | s->left = r; | |
7095 | else | |
7096 | s->right = r; | |
7097 | } | |
7098 | else | |
7099 | constructor_pending_elts = r; | |
7100 | } | |
7101 | else /* r->balance == -1 */ | |
7102 | { | |
7103 | /* RL rotation */ | |
7104 | struct init_node *t = r->left; | |
7105 | ||
7106 | r->left = t->right; | |
7107 | if (r->left) | |
7108 | r->left->parent = r; | |
7109 | t->right = r; | |
7110 | ||
7111 | p->right = t->left; | |
7112 | if (p->right) | |
7113 | p->right->parent = p; | |
7114 | t->left = p; | |
7115 | ||
7116 | r->balance = (t->balance < 0); | |
7117 | p->balance = -(t->balance > 0); | |
7118 | t->balance = 0; | |
7119 | ||
7120 | s = p->parent; | |
7121 | p->parent = t; | |
7122 | r->parent = t; | |
7123 | t->parent = s; | |
7124 | if (s) | |
7125 | { | |
7126 | if (s->left == p) | |
7127 | s->left = t; | |
7128 | else | |
7129 | s->right = t; | |
7130 | } | |
7131 | else | |
7132 | constructor_pending_elts = t; | |
7133 | } | |
7134 | break; | |
7135 | } | |
7136 | else | |
7137 | { | |
7138 | /* p->balance == -1; growth of right side balances the node. */ | |
7139 | p->balance = 0; | |
7140 | break; | |
7141 | } | |
7142 | } | |
7143 | ||
7144 | r = p; | |
7145 | p = p->parent; | |
7146 | } | |
7147 | } | |
7148 | ||
7149 | /* Build AVL tree from a sorted chain. */ | |
7150 | ||
7151 | static void | |
7152 | set_nonincremental_init (void) | |
7153 | { | |
4038c495 GB |
7154 | unsigned HOST_WIDE_INT ix; |
7155 | tree index, value; | |
3e4093b6 RS |
7156 | |
7157 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7158 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7159 | return; | |
7160 | ||
4038c495 | 7161 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) |
bbbbb16a | 7162 | add_pending_init (index, value, NULL_TREE, false); |
3e4093b6 RS |
7163 | constructor_elements = 0; |
7164 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
7165 | { | |
7166 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
7167 | /* Skip any nameless bit fields at the beginning. */ | |
7168 | while (constructor_unfilled_fields != 0 | |
7169 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7170 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7171 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
fed3cef0 | 7172 | |
de520661 | 7173 | } |
3e4093b6 | 7174 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) |
de520661 | 7175 | { |
3e4093b6 RS |
7176 | if (TYPE_DOMAIN (constructor_type)) |
7177 | constructor_unfilled_index | |
7178 | = convert (bitsizetype, | |
7179 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7180 | else | |
7181 | constructor_unfilled_index = bitsize_zero_node; | |
de520661 | 7182 | } |
3e4093b6 | 7183 | constructor_incremental = 0; |
de520661 | 7184 | } |
400fbf9f | 7185 | |
3e4093b6 | 7186 | /* Build AVL tree from a string constant. */ |
de520661 | 7187 | |
3e4093b6 RS |
7188 | static void |
7189 | set_nonincremental_init_from_string (tree str) | |
de520661 | 7190 | { |
3e4093b6 RS |
7191 | tree value, purpose, type; |
7192 | HOST_WIDE_INT val[2]; | |
7193 | const char *p, *end; | |
7194 | int byte, wchar_bytes, charwidth, bitpos; | |
de520661 | 7195 | |
366de0ce | 7196 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); |
940ff66d | 7197 | |
c466b2cd | 7198 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; |
3e4093b6 RS |
7199 | charwidth = TYPE_PRECISION (char_type_node); |
7200 | type = TREE_TYPE (constructor_type); | |
7201 | p = TREE_STRING_POINTER (str); | |
7202 | end = p + TREE_STRING_LENGTH (str); | |
91fa3c30 | 7203 | |
3e4093b6 RS |
7204 | for (purpose = bitsize_zero_node; |
7205 | p < end && !tree_int_cst_lt (constructor_max_index, purpose); | |
7206 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) | |
584ef5fe | 7207 | { |
3e4093b6 | 7208 | if (wchar_bytes == 1) |
ffc5c6a9 | 7209 | { |
3e4093b6 RS |
7210 | val[1] = (unsigned char) *p++; |
7211 | val[0] = 0; | |
ffc5c6a9 RH |
7212 | } |
7213 | else | |
3e4093b6 RS |
7214 | { |
7215 | val[0] = 0; | |
7216 | val[1] = 0; | |
7217 | for (byte = 0; byte < wchar_bytes; byte++) | |
7218 | { | |
7219 | if (BYTES_BIG_ENDIAN) | |
7220 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
7221 | else | |
7222 | bitpos = byte * charwidth; | |
7223 | val[bitpos < HOST_BITS_PER_WIDE_INT] | |
7224 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) | |
7225 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
7226 | } | |
7227 | } | |
584ef5fe | 7228 | |
8df83eae | 7229 | if (!TYPE_UNSIGNED (type)) |
3e4093b6 RS |
7230 | { |
7231 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
7232 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
7233 | { | |
7234 | if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) | |
7235 | { | |
7236 | val[1] |= ((HOST_WIDE_INT) -1) << bitpos; | |
7237 | val[0] = -1; | |
7238 | } | |
7239 | } | |
7240 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
7241 | { | |
7242 | if (val[1] < 0) | |
c22cacf3 | 7243 | val[0] = -1; |
3e4093b6 RS |
7244 | } |
7245 | else if (val[0] & (((HOST_WIDE_INT) 1) | |
7246 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) | |
7247 | val[0] |= ((HOST_WIDE_INT) -1) | |
7248 | << (bitpos - HOST_BITS_PER_WIDE_INT); | |
7249 | } | |
ffc5c6a9 | 7250 | |
7d60be94 | 7251 | value = build_int_cst_wide (type, val[1], val[0]); |
bbbbb16a | 7252 | add_pending_init (purpose, value, NULL_TREE, false); |
9dfcc8db BH |
7253 | } |
7254 | ||
3e4093b6 RS |
7255 | constructor_incremental = 0; |
7256 | } | |
de520661 | 7257 | |
3e4093b6 RS |
7258 | /* Return value of FIELD in pending initializer or zero if the field was |
7259 | not initialized yet. */ | |
7260 | ||
7261 | static tree | |
7262 | find_init_member (tree field) | |
7263 | { | |
7264 | struct init_node *p; | |
7265 | ||
7266 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
19d76e60 | 7267 | { |
3e4093b6 RS |
7268 | if (constructor_incremental |
7269 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
7270 | set_nonincremental_init (); | |
7271 | ||
7272 | p = constructor_pending_elts; | |
7273 | while (p) | |
19d76e60 | 7274 | { |
3e4093b6 RS |
7275 | if (tree_int_cst_lt (field, p->purpose)) |
7276 | p = p->left; | |
7277 | else if (tree_int_cst_lt (p->purpose, field)) | |
7278 | p = p->right; | |
7279 | else | |
7280 | return p->value; | |
19d76e60 | 7281 | } |
19d76e60 | 7282 | } |
3e4093b6 | 7283 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
de520661 | 7284 | { |
3e4093b6 | 7285 | tree bitpos = bit_position (field); |
de520661 | 7286 | |
3e4093b6 RS |
7287 | if (constructor_incremental |
7288 | && (!constructor_unfilled_fields | |
7289 | || tree_int_cst_lt (bitpos, | |
7290 | bit_position (constructor_unfilled_fields)))) | |
7291 | set_nonincremental_init (); | |
de520661 | 7292 | |
3e4093b6 RS |
7293 | p = constructor_pending_elts; |
7294 | while (p) | |
7295 | { | |
7296 | if (field == p->purpose) | |
7297 | return p->value; | |
7298 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7299 | p = p->left; | |
7300 | else | |
7301 | p = p->right; | |
7302 | } | |
7303 | } | |
7304 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
de520661 | 7305 | { |
4038c495 GB |
7306 | if (!VEC_empty (constructor_elt, constructor_elements) |
7307 | && (VEC_last (constructor_elt, constructor_elements)->index | |
7308 | == field)) | |
7309 | return VEC_last (constructor_elt, constructor_elements)->value; | |
de520661 | 7310 | } |
3e4093b6 | 7311 | return 0; |
de520661 RS |
7312 | } |
7313 | ||
3e4093b6 RS |
7314 | /* "Output" the next constructor element. |
7315 | At top level, really output it to assembler code now. | |
7316 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
bbbbb16a | 7317 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. |
3e4093b6 RS |
7318 | TYPE is the data type that the containing data type wants here. |
7319 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
916c5919 JM |
7320 | If VALUE is a string constant, STRICT_STRING is true if it is |
7321 | unparenthesized or we should not warn here for it being parenthesized. | |
7322 | For other types of VALUE, STRICT_STRING is not used. | |
8b6a5902 | 7323 | |
3e4093b6 RS |
7324 | PENDING if non-nil means output pending elements that belong |
7325 | right after this element. (PENDING is normally 1; | |
b295aee2 JJ |
7326 | it is 0 while outputting pending elements, to avoid recursion.) |
7327 | ||
7328 | IMPLICIT is true if value comes from pop_init_level (1), | |
7329 | the new initializer has been merged with the existing one | |
7330 | and thus no warnings should be emitted about overriding an | |
7331 | existing initializer. */ | |
8b6a5902 | 7332 | |
3e4093b6 | 7333 | static void |
bbbbb16a ILT |
7334 | output_init_element (tree value, tree origtype, bool strict_string, tree type, |
7335 | tree field, int pending, bool implicit) | |
3e4093b6 | 7336 | { |
8ce94e44 | 7337 | tree semantic_type = NULL_TREE; |
4038c495 | 7338 | constructor_elt *celt; |
928c19bb JM |
7339 | bool maybe_const = true; |
7340 | bool npc; | |
4038c495 | 7341 | |
0a880880 | 7342 | if (type == error_mark_node || value == error_mark_node) |
8b6a5902 | 7343 | { |
3e4093b6 RS |
7344 | constructor_erroneous = 1; |
7345 | return; | |
8b6a5902 | 7346 | } |
46bdb9cf JM |
7347 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
7348 | && (TREE_CODE (value) == STRING_CST | |
7349 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
7350 | && !(TREE_CODE (value) == STRING_CST | |
7351 | && TREE_CODE (type) == ARRAY_TYPE | |
7352 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
7353 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
7354 | TYPE_MAIN_VARIANT (type))) | |
c2255bc4 | 7355 | value = array_to_pointer_conversion (input_location, value); |
8b6a5902 | 7356 | |
3e4093b6 RS |
7357 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR |
7358 | && require_constant_value && !flag_isoc99 && pending) | |
8b6a5902 | 7359 | { |
3e4093b6 RS |
7360 | /* As an extension, allow initializing objects with static storage |
7361 | duration with compound literals (which are then treated just as | |
7362 | the brace enclosed list they contain). */ | |
7363 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); | |
7364 | value = DECL_INITIAL (decl); | |
8b6a5902 JJ |
7365 | } |
7366 | ||
928c19bb | 7367 | npc = null_pointer_constant_p (value); |
8ce94e44 JM |
7368 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) |
7369 | { | |
7370 | semantic_type = TREE_TYPE (value); | |
7371 | value = TREE_OPERAND (value, 0); | |
7372 | } | |
928c19bb JM |
7373 | value = c_fully_fold (value, require_constant_value, &maybe_const); |
7374 | ||
3e4093b6 RS |
7375 | if (value == error_mark_node) |
7376 | constructor_erroneous = 1; | |
7377 | else if (!TREE_CONSTANT (value)) | |
7378 | constructor_constant = 0; | |
116df786 | 7379 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) |
3e4093b6 RS |
7380 | || ((TREE_CODE (constructor_type) == RECORD_TYPE |
7381 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7382 | && DECL_C_BIT_FIELD (field) | |
7383 | && TREE_CODE (value) != INTEGER_CST)) | |
7384 | constructor_simple = 0; | |
928c19bb JM |
7385 | if (!maybe_const) |
7386 | constructor_nonconst = 1; | |
3e4093b6 | 7387 | |
116df786 | 7388 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) |
8b6a5902 | 7389 | { |
116df786 RH |
7390 | if (require_constant_value) |
7391 | { | |
7392 | error_init ("initializer element is not constant"); | |
7393 | value = error_mark_node; | |
7394 | } | |
7395 | else if (require_constant_elements) | |
509c9d60 MLI |
7396 | pedwarn (input_location, 0, |
7397 | "initializer element is not computable at load time"); | |
8b6a5902 | 7398 | } |
928c19bb JM |
7399 | else if (!maybe_const |
7400 | && (require_constant_value || require_constant_elements)) | |
7401 | pedwarn_init (input_location, 0, | |
7402 | "initializer element is not a constant expression"); | |
3e4093b6 | 7403 | |
81f40b79 ILT |
7404 | /* Issue -Wc++-compat warnings about initializing a bitfield with |
7405 | enum type. */ | |
7406 | if (warn_cxx_compat | |
7407 | && field != NULL_TREE | |
7408 | && TREE_CODE (field) == FIELD_DECL | |
7409 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
7410 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
7411 | != TYPE_MAIN_VARIANT (type)) | |
7412 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
7413 | { | |
7414 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
7415 | if (checktype != error_mark_node | |
7416 | && (TYPE_MAIN_VARIANT (checktype) | |
7417 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
7418 | warning_init (OPT_Wc___compat, | |
7419 | "enum conversion in initialization is invalid in C++"); | |
7420 | } | |
7421 | ||
3e4093b6 RS |
7422 | /* If this field is empty (and not at the end of structure), |
7423 | don't do anything other than checking the initializer. */ | |
7424 | if (field | |
7425 | && (TREE_TYPE (field) == error_mark_node | |
7426 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
7427 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
7428 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
7429 | || TREE_CHAIN (field))))) | |
7430 | return; | |
7431 | ||
8ce94e44 JM |
7432 | if (semantic_type) |
7433 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
c2255bc4 AH |
7434 | value = digest_init (input_location, type, value, origtype, npc, |
7435 | strict_string, require_constant_value); | |
3e4093b6 | 7436 | if (value == error_mark_node) |
8b6a5902 | 7437 | { |
3e4093b6 RS |
7438 | constructor_erroneous = 1; |
7439 | return; | |
8b6a5902 | 7440 | } |
928c19bb JM |
7441 | if (require_constant_value || require_constant_elements) |
7442 | constant_expression_warning (value); | |
8b6a5902 | 7443 | |
3e4093b6 RS |
7444 | /* If this element doesn't come next in sequence, |
7445 | put it on constructor_pending_elts. */ | |
7446 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
7447 | && (!constructor_incremental | |
7448 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
8b6a5902 | 7449 | { |
3e4093b6 RS |
7450 | if (constructor_incremental |
7451 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
7452 | set_nonincremental_init (); | |
7453 | ||
bbbbb16a | 7454 | add_pending_init (field, value, origtype, implicit); |
3e4093b6 | 7455 | return; |
8b6a5902 | 7456 | } |
3e4093b6 RS |
7457 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
7458 | && (!constructor_incremental | |
7459 | || field != constructor_unfilled_fields)) | |
8b6a5902 | 7460 | { |
3e4093b6 RS |
7461 | /* We do this for records but not for unions. In a union, |
7462 | no matter which field is specified, it can be initialized | |
7463 | right away since it starts at the beginning of the union. */ | |
7464 | if (constructor_incremental) | |
7465 | { | |
7466 | if (!constructor_unfilled_fields) | |
7467 | set_nonincremental_init (); | |
7468 | else | |
7469 | { | |
7470 | tree bitpos, unfillpos; | |
7471 | ||
7472 | bitpos = bit_position (field); | |
7473 | unfillpos = bit_position (constructor_unfilled_fields); | |
7474 | ||
7475 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
7476 | set_nonincremental_init (); | |
7477 | } | |
7478 | } | |
7479 | ||
bbbbb16a | 7480 | add_pending_init (field, value, origtype, implicit); |
3e4093b6 | 7481 | return; |
8b6a5902 | 7482 | } |
3e4093b6 | 7483 | else if (TREE_CODE (constructor_type) == UNION_TYPE |
4038c495 | 7484 | && !VEC_empty (constructor_elt, constructor_elements)) |
3e4093b6 | 7485 | { |
b295aee2 JJ |
7486 | if (!implicit) |
7487 | { | |
7488 | if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt, | |
7489 | constructor_elements)->value)) | |
7490 | warning_init (0, | |
7491 | "initialized field with side-effects overwritten"); | |
7492 | else if (warn_override_init) | |
7493 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7494 | } | |
8b6a5902 | 7495 | |
3e4093b6 RS |
7496 | /* We can have just one union field set. */ |
7497 | constructor_elements = 0; | |
7498 | } | |
8b6a5902 | 7499 | |
3e4093b6 RS |
7500 | /* Otherwise, output this element either to |
7501 | constructor_elements or to the assembler file. */ | |
8b6a5902 | 7502 | |
4038c495 GB |
7503 | celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL); |
7504 | celt->index = field; | |
7505 | celt->value = value; | |
8b6a5902 | 7506 | |
3e4093b6 RS |
7507 | /* Advance the variable that indicates sequential elements output. */ |
7508 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7509 | constructor_unfilled_index | |
db3927fb AH |
7510 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, |
7511 | bitsize_one_node); | |
3e4093b6 RS |
7512 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) |
7513 | { | |
7514 | constructor_unfilled_fields | |
7515 | = TREE_CHAIN (constructor_unfilled_fields); | |
8b6a5902 | 7516 | |
3e4093b6 RS |
7517 | /* Skip any nameless bit fields. */ |
7518 | while (constructor_unfilled_fields != 0 | |
7519 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7520 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7521 | constructor_unfilled_fields = | |
7522 | TREE_CHAIN (constructor_unfilled_fields); | |
7523 | } | |
7524 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
7525 | constructor_unfilled_fields = 0; | |
de520661 | 7526 | |
3e4093b6 RS |
7527 | /* Now output any pending elements which have become next. */ |
7528 | if (pending) | |
7529 | output_pending_init_elements (0); | |
7530 | } | |
8b6a5902 | 7531 | |
3e4093b6 RS |
7532 | /* Output any pending elements which have become next. |
7533 | As we output elements, constructor_unfilled_{fields,index} | |
7534 | advances, which may cause other elements to become next; | |
7535 | if so, they too are output. | |
8b6a5902 | 7536 | |
3e4093b6 RS |
7537 | If ALL is 0, we return when there are |
7538 | no more pending elements to output now. | |
665f2503 | 7539 | |
3e4093b6 RS |
7540 | If ALL is 1, we output space as necessary so that |
7541 | we can output all the pending elements. */ | |
19d76e60 | 7542 | |
3e4093b6 RS |
7543 | static void |
7544 | output_pending_init_elements (int all) | |
7545 | { | |
7546 | struct init_node *elt = constructor_pending_elts; | |
7547 | tree next; | |
de520661 | 7548 | |
3e4093b6 RS |
7549 | retry: |
7550 | ||
ba228239 | 7551 | /* Look through the whole pending tree. |
3e4093b6 RS |
7552 | If we find an element that should be output now, |
7553 | output it. Otherwise, set NEXT to the element | |
7554 | that comes first among those still pending. */ | |
7555 | ||
7556 | next = 0; | |
7557 | while (elt) | |
7558 | { | |
7559 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8b6a5902 | 7560 | { |
3e4093b6 RS |
7561 | if (tree_int_cst_equal (elt->purpose, |
7562 | constructor_unfilled_index)) | |
bbbbb16a | 7563 | output_init_element (elt->value, elt->origtype, true, |
3e4093b6 | 7564 | TREE_TYPE (constructor_type), |
b295aee2 | 7565 | constructor_unfilled_index, 0, false); |
3e4093b6 RS |
7566 | else if (tree_int_cst_lt (constructor_unfilled_index, |
7567 | elt->purpose)) | |
8b6a5902 | 7568 | { |
3e4093b6 RS |
7569 | /* Advance to the next smaller node. */ |
7570 | if (elt->left) | |
7571 | elt = elt->left; | |
7572 | else | |
7573 | { | |
7574 | /* We have reached the smallest node bigger than the | |
7575 | current unfilled index. Fill the space first. */ | |
7576 | next = elt->purpose; | |
7577 | break; | |
7578 | } | |
8b6a5902 | 7579 | } |
ce662d4c JJ |
7580 | else |
7581 | { | |
3e4093b6 RS |
7582 | /* Advance to the next bigger node. */ |
7583 | if (elt->right) | |
7584 | elt = elt->right; | |
7585 | else | |
ce662d4c | 7586 | { |
3e4093b6 RS |
7587 | /* We have reached the biggest node in a subtree. Find |
7588 | the parent of it, which is the next bigger node. */ | |
7589 | while (elt->parent && elt->parent->right == elt) | |
7590 | elt = elt->parent; | |
7591 | elt = elt->parent; | |
7592 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
7593 | elt->purpose)) | |
7594 | { | |
7595 | next = elt->purpose; | |
7596 | break; | |
7597 | } | |
ce662d4c JJ |
7598 | } |
7599 | } | |
8b6a5902 | 7600 | } |
3e4093b6 RS |
7601 | else if (TREE_CODE (constructor_type) == RECORD_TYPE |
7602 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7603 | { | |
7604 | tree ctor_unfilled_bitpos, elt_bitpos; | |
ce662d4c | 7605 | |
3e4093b6 RS |
7606 | /* If the current record is complete we are done. */ |
7607 | if (constructor_unfilled_fields == 0) | |
7608 | break; | |
de520661 | 7609 | |
3e4093b6 RS |
7610 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); |
7611 | elt_bitpos = bit_position (elt->purpose); | |
7612 | /* We can't compare fields here because there might be empty | |
7613 | fields in between. */ | |
7614 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
7615 | { | |
7616 | constructor_unfilled_fields = elt->purpose; | |
bbbbb16a ILT |
7617 | output_init_element (elt->value, elt->origtype, true, |
7618 | TREE_TYPE (elt->purpose), | |
b295aee2 | 7619 | elt->purpose, 0, false); |
3e4093b6 RS |
7620 | } |
7621 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
7622 | { | |
7623 | /* Advance to the next smaller node. */ | |
7624 | if (elt->left) | |
7625 | elt = elt->left; | |
7626 | else | |
7627 | { | |
7628 | /* We have reached the smallest node bigger than the | |
7629 | current unfilled field. Fill the space first. */ | |
7630 | next = elt->purpose; | |
7631 | break; | |
7632 | } | |
7633 | } | |
7634 | else | |
7635 | { | |
7636 | /* Advance to the next bigger node. */ | |
7637 | if (elt->right) | |
7638 | elt = elt->right; | |
7639 | else | |
7640 | { | |
7641 | /* We have reached the biggest node in a subtree. Find | |
7642 | the parent of it, which is the next bigger node. */ | |
7643 | while (elt->parent && elt->parent->right == elt) | |
7644 | elt = elt->parent; | |
7645 | elt = elt->parent; | |
7646 | if (elt | |
7647 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
7648 | bit_position (elt->purpose)))) | |
7649 | { | |
7650 | next = elt->purpose; | |
7651 | break; | |
7652 | } | |
7653 | } | |
7654 | } | |
7655 | } | |
7656 | } | |
de520661 | 7657 | |
3e4093b6 RS |
7658 | /* Ordinarily return, but not if we want to output all |
7659 | and there are elements left. */ | |
3f75a254 | 7660 | if (!(all && next != 0)) |
e5cfb88f RK |
7661 | return; |
7662 | ||
3e4093b6 RS |
7663 | /* If it's not incremental, just skip over the gap, so that after |
7664 | jumping to retry we will output the next successive element. */ | |
7665 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
7666 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7667 | constructor_unfilled_fields = next; | |
7668 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7669 | constructor_unfilled_index = next; | |
de520661 | 7670 | |
3e4093b6 RS |
7671 | /* ELT now points to the node in the pending tree with the next |
7672 | initializer to output. */ | |
7673 | goto retry; | |
de520661 RS |
7674 | } |
7675 | \f | |
3e4093b6 RS |
7676 | /* Add one non-braced element to the current constructor level. |
7677 | This adjusts the current position within the constructor's type. | |
7678 | This may also start or terminate implicit levels | |
7679 | to handle a partly-braced initializer. | |
e5e809f4 | 7680 | |
3e4093b6 | 7681 | Once this has found the correct level for the new element, |
b295aee2 JJ |
7682 | it calls output_init_element. |
7683 | ||
7684 | IMPLICIT is true if value comes from pop_init_level (1), | |
7685 | the new initializer has been merged with the existing one | |
7686 | and thus no warnings should be emitted about overriding an | |
7687 | existing initializer. */ | |
3e4093b6 RS |
7688 | |
7689 | void | |
b295aee2 | 7690 | process_init_element (struct c_expr value, bool implicit) |
e5e809f4 | 7691 | { |
916c5919 JM |
7692 | tree orig_value = value.value; |
7693 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
7694 | bool strict_string = value.original_code == STRING_CST; | |
e5e809f4 | 7695 | |
3e4093b6 | 7696 | designator_depth = 0; |
b06df647 | 7697 | designator_erroneous = 0; |
e5e809f4 | 7698 | |
3e4093b6 RS |
7699 | /* Handle superfluous braces around string cst as in |
7700 | char x[] = {"foo"}; */ | |
7701 | if (string_flag | |
7702 | && constructor_type | |
7703 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
197463ae | 7704 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) |
3e4093b6 | 7705 | && integer_zerop (constructor_unfilled_index)) |
e5e809f4 | 7706 | { |
916c5919 | 7707 | if (constructor_stack->replacement_value.value) |
c22cacf3 | 7708 | error_init ("excess elements in char array initializer"); |
3e4093b6 RS |
7709 | constructor_stack->replacement_value = value; |
7710 | return; | |
e5e809f4 | 7711 | } |
8b6a5902 | 7712 | |
916c5919 | 7713 | if (constructor_stack->replacement_value.value != 0) |
3e4093b6 RS |
7714 | { |
7715 | error_init ("excess elements in struct initializer"); | |
7716 | return; | |
e5e809f4 JL |
7717 | } |
7718 | ||
3e4093b6 RS |
7719 | /* Ignore elements of a brace group if it is entirely superfluous |
7720 | and has already been diagnosed. */ | |
7721 | if (constructor_type == 0) | |
7722 | return; | |
e5e809f4 | 7723 | |
3e4093b6 RS |
7724 | /* If we've exhausted any levels that didn't have braces, |
7725 | pop them now. */ | |
7726 | while (constructor_stack->implicit) | |
7727 | { | |
7728 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7729 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7730 | && constructor_fields == 0) | |
b295aee2 | 7731 | process_init_element (pop_init_level (1), true); |
53650abe AP |
7732 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE |
7733 | || TREE_CODE (constructor_type) == VECTOR_TYPE) | |
3e4093b6 RS |
7734 | && (constructor_max_index == 0 |
7735 | || tree_int_cst_lt (constructor_max_index, | |
7736 | constructor_index))) | |
b295aee2 | 7737 | process_init_element (pop_init_level (1), true); |
3e4093b6 RS |
7738 | else |
7739 | break; | |
7740 | } | |
e5e809f4 | 7741 | |
3e4093b6 RS |
7742 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ |
7743 | if (constructor_range_stack) | |
e5e809f4 | 7744 | { |
3e4093b6 RS |
7745 | /* If value is a compound literal and we'll be just using its |
7746 | content, don't put it into a SAVE_EXPR. */ | |
916c5919 | 7747 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR |
3e4093b6 RS |
7748 | || !require_constant_value |
7749 | || flag_isoc99) | |
8ce94e44 JM |
7750 | { |
7751 | tree semantic_type = NULL_TREE; | |
7752 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
7753 | { | |
7754 | semantic_type = TREE_TYPE (value.value); | |
7755 | value.value = TREE_OPERAND (value.value, 0); | |
7756 | } | |
7757 | value.value = c_save_expr (value.value); | |
7758 | if (semantic_type) | |
7759 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
7760 | value.value); | |
7761 | } | |
3e4093b6 | 7762 | } |
e5e809f4 | 7763 | |
3e4093b6 RS |
7764 | while (1) |
7765 | { | |
7766 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
e5e809f4 | 7767 | { |
3e4093b6 RS |
7768 | tree fieldtype; |
7769 | enum tree_code fieldcode; | |
e5e809f4 | 7770 | |
3e4093b6 RS |
7771 | if (constructor_fields == 0) |
7772 | { | |
509c9d60 MLI |
7773 | pedwarn_init (input_location, 0, |
7774 | "excess elements in struct initializer"); | |
3e4093b6 RS |
7775 | break; |
7776 | } | |
e5e809f4 | 7777 | |
3e4093b6 RS |
7778 | fieldtype = TREE_TYPE (constructor_fields); |
7779 | if (fieldtype != error_mark_node) | |
7780 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
7781 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 7782 | |
3e4093b6 RS |
7783 | /* Error for non-static initialization of a flexible array member. */ |
7784 | if (fieldcode == ARRAY_TYPE | |
7785 | && !require_constant_value | |
7786 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
7787 | && TREE_CHAIN (constructor_fields) == NULL_TREE) | |
7788 | { | |
7789 | error_init ("non-static initialization of a flexible array member"); | |
7790 | break; | |
7791 | } | |
e5e809f4 | 7792 | |
3e4093b6 | 7793 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 7794 | if (value.value != 0 |
3e4093b6 | 7795 | && fieldcode == ARRAY_TYPE |
197463ae | 7796 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 7797 | && string_flag) |
916c5919 | 7798 | value.value = orig_value; |
3e4093b6 RS |
7799 | /* Otherwise, if we have come to a subaggregate, |
7800 | and we don't have an element of its type, push into it. */ | |
0953878d | 7801 | else if (value.value != 0 |
916c5919 JM |
7802 | && value.value != error_mark_node |
7803 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 7804 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 7805 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 RS |
7806 | { |
7807 | push_init_level (1); | |
7808 | continue; | |
7809 | } | |
e5e809f4 | 7810 | |
916c5919 | 7811 | if (value.value) |
3e4093b6 RS |
7812 | { |
7813 | push_member_name (constructor_fields); | |
bbbbb16a ILT |
7814 | output_init_element (value.value, value.original_type, |
7815 | strict_string, fieldtype, | |
7816 | constructor_fields, 1, implicit); | |
3e4093b6 | 7817 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
7818 | } |
7819 | else | |
3e4093b6 RS |
7820 | /* Do the bookkeeping for an element that was |
7821 | directly output as a constructor. */ | |
e5e809f4 | 7822 | { |
3e4093b6 RS |
7823 | /* For a record, keep track of end position of last field. */ |
7824 | if (DECL_SIZE (constructor_fields)) | |
c22cacf3 | 7825 | constructor_bit_index |
db3927fb AH |
7826 | = size_binop_loc (input_location, PLUS_EXPR, |
7827 | bit_position (constructor_fields), | |
7828 | DECL_SIZE (constructor_fields)); | |
3e4093b6 RS |
7829 | |
7830 | /* If the current field was the first one not yet written out, | |
7831 | it isn't now, so update. */ | |
7832 | if (constructor_unfilled_fields == constructor_fields) | |
7833 | { | |
7834 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
7835 | /* Skip any nameless bit fields. */ | |
7836 | while (constructor_unfilled_fields != 0 | |
7837 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7838 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7839 | constructor_unfilled_fields = | |
7840 | TREE_CHAIN (constructor_unfilled_fields); | |
7841 | } | |
e5e809f4 | 7842 | } |
3e4093b6 RS |
7843 | |
7844 | constructor_fields = TREE_CHAIN (constructor_fields); | |
7845 | /* Skip any nameless bit fields at the beginning. */ | |
7846 | while (constructor_fields != 0 | |
7847 | && DECL_C_BIT_FIELD (constructor_fields) | |
7848 | && DECL_NAME (constructor_fields) == 0) | |
7849 | constructor_fields = TREE_CHAIN (constructor_fields); | |
e5e809f4 | 7850 | } |
3e4093b6 | 7851 | else if (TREE_CODE (constructor_type) == UNION_TYPE) |
e5e809f4 | 7852 | { |
3e4093b6 RS |
7853 | tree fieldtype; |
7854 | enum tree_code fieldcode; | |
e5e809f4 | 7855 | |
3e4093b6 RS |
7856 | if (constructor_fields == 0) |
7857 | { | |
509c9d60 MLI |
7858 | pedwarn_init (input_location, 0, |
7859 | "excess elements in union initializer"); | |
3e4093b6 RS |
7860 | break; |
7861 | } | |
e5e809f4 | 7862 | |
3e4093b6 RS |
7863 | fieldtype = TREE_TYPE (constructor_fields); |
7864 | if (fieldtype != error_mark_node) | |
7865 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
7866 | fieldcode = TREE_CODE (fieldtype); | |
e5e809f4 | 7867 | |
3e4093b6 RS |
7868 | /* Warn that traditional C rejects initialization of unions. |
7869 | We skip the warning if the value is zero. This is done | |
7870 | under the assumption that the zero initializer in user | |
7871 | code appears conditioned on e.g. __STDC__ to avoid | |
7872 | "missing initializer" warnings and relies on default | |
7873 | initialization to zero in the traditional C case. | |
7874 | We also skip the warning if the initializer is designated, | |
7875 | again on the assumption that this must be conditional on | |
7876 | __STDC__ anyway (and we've already complained about the | |
7877 | member-designator already). */ | |
3176a0c2 | 7878 | if (!in_system_header && !constructor_designated |
916c5919 JM |
7879 | && !(value.value && (integer_zerop (value.value) |
7880 | || real_zerop (value.value)))) | |
3176a0c2 DD |
7881 | warning (OPT_Wtraditional, "traditional C rejects initialization " |
7882 | "of unions"); | |
e5e809f4 | 7883 | |
3e4093b6 | 7884 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 7885 | if (value.value != 0 |
3e4093b6 | 7886 | && fieldcode == ARRAY_TYPE |
197463ae | 7887 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) |
3e4093b6 | 7888 | && string_flag) |
916c5919 | 7889 | value.value = orig_value; |
3e4093b6 RS |
7890 | /* Otherwise, if we have come to a subaggregate, |
7891 | and we don't have an element of its type, push into it. */ | |
0953878d | 7892 | else if (value.value != 0 |
916c5919 JM |
7893 | && value.value != error_mark_node |
7894 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
3e4093b6 | 7895 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE |
53650abe | 7896 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) |
3e4093b6 RS |
7897 | { |
7898 | push_init_level (1); | |
7899 | continue; | |
7900 | } | |
e5e809f4 | 7901 | |
916c5919 | 7902 | if (value.value) |
3e4093b6 RS |
7903 | { |
7904 | push_member_name (constructor_fields); | |
bbbbb16a ILT |
7905 | output_init_element (value.value, value.original_type, |
7906 | strict_string, fieldtype, | |
7907 | constructor_fields, 1, implicit); | |
3e4093b6 | 7908 | RESTORE_SPELLING_DEPTH (constructor_depth); |
e5e809f4 JL |
7909 | } |
7910 | else | |
3e4093b6 RS |
7911 | /* Do the bookkeeping for an element that was |
7912 | directly output as a constructor. */ | |
e5e809f4 | 7913 | { |
3e4093b6 RS |
7914 | constructor_bit_index = DECL_SIZE (constructor_fields); |
7915 | constructor_unfilled_fields = TREE_CHAIN (constructor_fields); | |
e5e809f4 | 7916 | } |
e5e809f4 | 7917 | |
3e4093b6 RS |
7918 | constructor_fields = 0; |
7919 | } | |
7920 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7921 | { | |
7922 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
7923 | enum tree_code eltcode = TREE_CODE (elttype); | |
e5e809f4 | 7924 | |
3e4093b6 | 7925 | /* Accept a string constant to initialize a subarray. */ |
916c5919 | 7926 | if (value.value != 0 |
3e4093b6 | 7927 | && eltcode == ARRAY_TYPE |
197463ae | 7928 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) |
3e4093b6 | 7929 | && string_flag) |
916c5919 | 7930 | value.value = orig_value; |
3e4093b6 RS |
7931 | /* Otherwise, if we have come to a subaggregate, |
7932 | and we don't have an element of its type, push into it. */ | |
0953878d | 7933 | else if (value.value != 0 |
916c5919 JM |
7934 | && value.value != error_mark_node |
7935 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
3e4093b6 | 7936 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE |
53650abe | 7937 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) |
3e4093b6 RS |
7938 | { |
7939 | push_init_level (1); | |
7940 | continue; | |
7941 | } | |
8b6a5902 | 7942 | |
3e4093b6 RS |
7943 | if (constructor_max_index != 0 |
7944 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
7945 | || integer_all_onesp (constructor_max_index))) | |
7946 | { | |
509c9d60 MLI |
7947 | pedwarn_init (input_location, 0, |
7948 | "excess elements in array initializer"); | |
3e4093b6 RS |
7949 | break; |
7950 | } | |
8b6a5902 | 7951 | |
3e4093b6 | 7952 | /* Now output the actual element. */ |
916c5919 | 7953 | if (value.value) |
3e4093b6 | 7954 | { |
a0f0ab9f | 7955 | push_array_bounds (tree_low_cst (constructor_index, 1)); |
bbbbb16a ILT |
7956 | output_init_element (value.value, value.original_type, |
7957 | strict_string, elttype, | |
7958 | constructor_index, 1, implicit); | |
3e4093b6 RS |
7959 | RESTORE_SPELLING_DEPTH (constructor_depth); |
7960 | } | |
2f6e4e97 | 7961 | |
3e4093b6 | 7962 | constructor_index |
db3927fb AH |
7963 | = size_binop_loc (input_location, PLUS_EXPR, |
7964 | constructor_index, bitsize_one_node); | |
8b6a5902 | 7965 | |
916c5919 | 7966 | if (!value.value) |
3e4093b6 RS |
7967 | /* If we are doing the bookkeeping for an element that was |
7968 | directly output as a constructor, we must update | |
7969 | constructor_unfilled_index. */ | |
7970 | constructor_unfilled_index = constructor_index; | |
7971 | } | |
7972 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
7973 | { | |
7974 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8b6a5902 | 7975 | |
c22cacf3 MS |
7976 | /* Do a basic check of initializer size. Note that vectors |
7977 | always have a fixed size derived from their type. */ | |
3e4093b6 RS |
7978 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) |
7979 | { | |
509c9d60 MLI |
7980 | pedwarn_init (input_location, 0, |
7981 | "excess elements in vector initializer"); | |
3e4093b6 RS |
7982 | break; |
7983 | } | |
8b6a5902 | 7984 | |
3e4093b6 | 7985 | /* Now output the actual element. */ |
916c5919 | 7986 | if (value.value) |
53650abe AP |
7987 | { |
7988 | if (TREE_CODE (value.value) == VECTOR_CST) | |
7989 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
7990 | output_init_element (value.value, value.original_type, | |
7991 | strict_string, elttype, | |
7992 | constructor_index, 1, implicit); | |
7993 | } | |
8b6a5902 | 7994 | |
3e4093b6 | 7995 | constructor_index |
db3927fb AH |
7996 | = size_binop_loc (input_location, |
7997 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
8b6a5902 | 7998 | |
916c5919 | 7999 | if (!value.value) |
3e4093b6 RS |
8000 | /* If we are doing the bookkeeping for an element that was |
8001 | directly output as a constructor, we must update | |
8002 | constructor_unfilled_index. */ | |
8003 | constructor_unfilled_index = constructor_index; | |
8004 | } | |
8b6a5902 | 8005 | |
3e4093b6 RS |
8006 | /* Handle the sole element allowed in a braced initializer |
8007 | for a scalar variable. */ | |
b4519d39 SB |
8008 | else if (constructor_type != error_mark_node |
8009 | && constructor_fields == 0) | |
8b6a5902 | 8010 | { |
509c9d60 MLI |
8011 | pedwarn_init (input_location, 0, |
8012 | "excess elements in scalar initializer"); | |
3e4093b6 | 8013 | break; |
8b6a5902 JJ |
8014 | } |
8015 | else | |
8016 | { | |
916c5919 | 8017 | if (value.value) |
bbbbb16a ILT |
8018 | output_init_element (value.value, value.original_type, |
8019 | strict_string, constructor_type, | |
8020 | NULL_TREE, 1, implicit); | |
3e4093b6 | 8021 | constructor_fields = 0; |
8b6a5902 JJ |
8022 | } |
8023 | ||
3e4093b6 RS |
8024 | /* Handle range initializers either at this level or anywhere higher |
8025 | in the designator stack. */ | |
8026 | if (constructor_range_stack) | |
8b6a5902 | 8027 | { |
3e4093b6 RS |
8028 | struct constructor_range_stack *p, *range_stack; |
8029 | int finish = 0; | |
8030 | ||
8031 | range_stack = constructor_range_stack; | |
8032 | constructor_range_stack = 0; | |
8033 | while (constructor_stack != range_stack->stack) | |
8b6a5902 | 8034 | { |
366de0ce | 8035 | gcc_assert (constructor_stack->implicit); |
b295aee2 | 8036 | process_init_element (pop_init_level (1), true); |
8b6a5902 | 8037 | } |
3e4093b6 RS |
8038 | for (p = range_stack; |
8039 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
8040 | p = p->prev) | |
8b6a5902 | 8041 | { |
366de0ce | 8042 | gcc_assert (constructor_stack->implicit); |
b295aee2 | 8043 | process_init_element (pop_init_level (1), true); |
8b6a5902 | 8044 | } |
3e4093b6 | 8045 | |
db3927fb AH |
8046 | p->index = size_binop_loc (input_location, |
8047 | PLUS_EXPR, p->index, bitsize_one_node); | |
3e4093b6 RS |
8048 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) |
8049 | finish = 1; | |
8050 | ||
8051 | while (1) | |
8052 | { | |
8053 | constructor_index = p->index; | |
8054 | constructor_fields = p->fields; | |
8055 | if (finish && p->range_end && p->index == p->range_start) | |
8056 | { | |
8057 | finish = 0; | |
8058 | p->prev = 0; | |
8059 | } | |
8060 | p = p->next; | |
8061 | if (!p) | |
8062 | break; | |
8063 | push_init_level (2); | |
8064 | p->stack = constructor_stack; | |
8065 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
8066 | p->index = p->range_start; | |
8067 | } | |
8068 | ||
8069 | if (!finish) | |
8070 | constructor_range_stack = range_stack; | |
8071 | continue; | |
8b6a5902 JJ |
8072 | } |
8073 | ||
3e4093b6 | 8074 | break; |
8b6a5902 JJ |
8075 | } |
8076 | ||
3e4093b6 RS |
8077 | constructor_range_stack = 0; |
8078 | } | |
8079 | \f | |
9f0e2d86 ZW |
8080 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER |
8081 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
e130a54b | 8082 | an ASM_EXPR node). */ |
3e4093b6 | 8083 | tree |
9f0e2d86 | 8084 | build_asm_stmt (tree cv_qualifier, tree args) |
3e4093b6 | 8085 | { |
6de9cd9a DN |
8086 | if (!ASM_VOLATILE_P (args) && cv_qualifier) |
8087 | ASM_VOLATILE_P (args) = 1; | |
9f0e2d86 | 8088 | return add_stmt (args); |
8b6a5902 JJ |
8089 | } |
8090 | ||
9f0e2d86 ZW |
8091 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, |
8092 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
8093 | SIMPLE indicates whether there was anything at all after the | |
8094 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
e130a54b | 8095 | are subtly different. We use a ASM_EXPR node to represent this. */ |
3e4093b6 | 8096 | tree |
c2255bc4 | 8097 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, |
1c384bf1 | 8098 | tree clobbers, tree labels, bool simple) |
e5e809f4 | 8099 | { |
3e4093b6 | 8100 | tree tail; |
9f0e2d86 | 8101 | tree args; |
6de9cd9a DN |
8102 | int i; |
8103 | const char *constraint; | |
74f0c611 | 8104 | const char **oconstraints; |
6de9cd9a | 8105 | bool allows_mem, allows_reg, is_inout; |
74f0c611 | 8106 | int ninputs, noutputs; |
6de9cd9a DN |
8107 | |
8108 | ninputs = list_length (inputs); | |
8109 | noutputs = list_length (outputs); | |
74f0c611 RH |
8110 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); |
8111 | ||
1c384bf1 | 8112 | string = resolve_asm_operand_names (string, outputs, inputs, labels); |
3e4093b6 | 8113 | |
6de9cd9a DN |
8114 | /* Remove output conversions that change the type but not the mode. */ |
8115 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
e5e809f4 | 8116 | { |
3e4093b6 | 8117 | tree output = TREE_VALUE (tail); |
74f0c611 RH |
8118 | |
8119 | /* ??? Really, this should not be here. Users should be using a | |
8120 | proper lvalue, dammit. But there's a long history of using casts | |
8121 | in the output operands. In cases like longlong.h, this becomes a | |
8122 | primitive form of typechecking -- if the cast can be removed, then | |
8123 | the output operand had a type of the proper width; otherwise we'll | |
8124 | get an error. Gross, but ... */ | |
3e4093b6 | 8125 | STRIP_NOPS (output); |
74f0c611 RH |
8126 | |
8127 | if (!lvalue_or_else (output, lv_asm)) | |
8128 | output = error_mark_node; | |
8b6a5902 | 8129 | |
5544530a PB |
8130 | if (output != error_mark_node |
8131 | && (TREE_READONLY (output) | |
8132 | || TYPE_READONLY (TREE_TYPE (output)) | |
8133 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
8134 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
8135 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
8136 | readonly_error (output, lv_asm); | |
8137 | ||
6de9cd9a | 8138 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); |
74f0c611 RH |
8139 | oconstraints[i] = constraint; |
8140 | ||
8141 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
8142 | &allows_mem, &allows_reg, &is_inout)) | |
8143 | { | |
8144 | /* If the operand is going to end up in memory, | |
8145 | mark it addressable. */ | |
8146 | if (!allows_reg && !c_mark_addressable (output)) | |
8147 | output = error_mark_node; | |
8148 | } | |
8149 | else | |
c22cacf3 | 8150 | output = error_mark_node; |
3e4093b6 | 8151 | |
74f0c611 | 8152 | TREE_VALUE (tail) = output; |
8b6a5902 | 8153 | } |
3e4093b6 | 8154 | |
74f0c611 RH |
8155 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) |
8156 | { | |
8157 | tree input; | |
8158 | ||
8159 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8160 | input = TREE_VALUE (tail); | |
8161 | ||
74f0c611 RH |
8162 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
8163 | oconstraints, &allows_mem, &allows_reg)) | |
8164 | { | |
8165 | /* If the operand is going to end up in memory, | |
8166 | mark it addressable. */ | |
b4c33883 AP |
8167 | if (!allows_reg && allows_mem) |
8168 | { | |
8169 | /* Strip the nops as we allow this case. FIXME, this really | |
8170 | should be rejected or made deprecated. */ | |
8171 | STRIP_NOPS (input); | |
8172 | if (!c_mark_addressable (input)) | |
8173 | input = error_mark_node; | |
8174 | } | |
74f0c611 RH |
8175 | } |
8176 | else | |
8177 | input = error_mark_node; | |
8178 | ||
8179 | TREE_VALUE (tail) = input; | |
8180 | } | |
3e4093b6 | 8181 | |
1c384bf1 RH |
8182 | /* ASMs with labels cannot have outputs. This should have been |
8183 | enforced by the parser. */ | |
8184 | gcc_assert (outputs == NULL || labels == NULL); | |
8185 | ||
8186 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
9f0e2d86 | 8187 | |
5544530a PB |
8188 | /* asm statements without outputs, including simple ones, are treated |
8189 | as volatile. */ | |
8190 | ASM_INPUT_P (args) = simple; | |
8191 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
74f0c611 | 8192 | |
9f0e2d86 | 8193 | return args; |
e5e809f4 | 8194 | } |
3e4093b6 | 8195 | \f |
c2255bc4 AH |
8196 | /* Generate a goto statement to LABEL. LOC is the location of the |
8197 | GOTO. */ | |
506e2710 RH |
8198 | |
8199 | tree | |
c2255bc4 | 8200 | c_finish_goto_label (location_t loc, tree label) |
506e2710 | 8201 | { |
e1b7793c | 8202 | tree decl = lookup_label_for_goto (loc, label); |
506e2710 RH |
8203 | if (!decl) |
8204 | return NULL_TREE; | |
506e2710 | 8205 | TREE_USED (decl) = 1; |
c2255bc4 AH |
8206 | { |
8207 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
8208 | SET_EXPR_LOCATION (t, loc); | |
8209 | return add_stmt (t); | |
8210 | } | |
506e2710 RH |
8211 | } |
8212 | ||
c2255bc4 AH |
8213 | /* Generate a computed goto statement to EXPR. LOC is the location of |
8214 | the GOTO. */ | |
506e2710 RH |
8215 | |
8216 | tree | |
c2255bc4 | 8217 | c_finish_goto_ptr (location_t loc, tree expr) |
506e2710 | 8218 | { |
c2255bc4 AH |
8219 | tree t; |
8220 | pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>"); | |
928c19bb | 8221 | expr = c_fully_fold (expr, false, NULL); |
506e2710 | 8222 | expr = convert (ptr_type_node, expr); |
c2255bc4 AH |
8223 | t = build1 (GOTO_EXPR, void_type_node, expr); |
8224 | SET_EXPR_LOCATION (t, loc); | |
8225 | return add_stmt (t); | |
506e2710 RH |
8226 | } |
8227 | ||
5088b058 | 8228 | /* Generate a C `return' statement. RETVAL is the expression for what |
c2255bc4 AH |
8229 | to return, or a null pointer for `return;' with no value. LOC is |
8230 | the location of the return statement. If ORIGTYPE is not NULL_TREE, it | |
8231 | is the original type of RETVAL. */ | |
de520661 | 8232 | |
506e2710 | 8233 | tree |
c2255bc4 | 8234 | c_finish_return (location_t loc, tree retval, tree origtype) |
3e4093b6 | 8235 | { |
0c9b182b JJ |
8236 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; |
8237 | bool no_warning = false; | |
928c19bb | 8238 | bool npc = false; |
3e4093b6 RS |
8239 | |
8240 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
c2255bc4 AH |
8241 | warning_at (loc, 0, |
8242 | "function declared %<noreturn%> has a %<return%> statement"); | |
3e4093b6 | 8243 | |
928c19bb JM |
8244 | if (retval) |
8245 | { | |
8ce94e44 | 8246 | tree semantic_type = NULL_TREE; |
928c19bb | 8247 | npc = null_pointer_constant_p (retval); |
8ce94e44 JM |
8248 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) |
8249 | { | |
8250 | semantic_type = TREE_TYPE (retval); | |
8251 | retval = TREE_OPERAND (retval, 0); | |
8252 | } | |
928c19bb | 8253 | retval = c_fully_fold (retval, false, NULL); |
8ce94e44 JM |
8254 | if (semantic_type) |
8255 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
928c19bb JM |
8256 | } |
8257 | ||
3e4093b6 | 8258 | if (!retval) |
de520661 | 8259 | { |
3e4093b6 RS |
8260 | current_function_returns_null = 1; |
8261 | if ((warn_return_type || flag_isoc99) | |
8262 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
0c9b182b | 8263 | { |
c2255bc4 | 8264 | pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type, |
fcf73884 | 8265 | "%<return%> with no value, in " |
0c9b182b JJ |
8266 | "function returning non-void"); |
8267 | no_warning = true; | |
8268 | } | |
400fbf9f | 8269 | } |
3e4093b6 | 8270 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) |
de520661 | 8271 | { |
3e4093b6 | 8272 | current_function_returns_null = 1; |
2397c575 | 8273 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) |
c2255bc4 | 8274 | pedwarn (loc, 0, |
509c9d60 | 8275 | "%<return%> with a value, in function returning void"); |
fcf73884 | 8276 | else |
c2255bc4 | 8277 | pedwarn (loc, OPT_pedantic, "ISO C forbids " |
fcf73884 | 8278 | "%<return%> with expression, in function returning void"); |
de520661 | 8279 | } |
3e4093b6 | 8280 | else |
de520661 | 8281 | { |
c2255bc4 AH |
8282 | tree t = convert_for_assignment (loc, valtype, retval, origtype, |
8283 | ic_return, | |
8284 | npc, NULL_TREE, NULL_TREE, 0); | |
3e4093b6 RS |
8285 | tree res = DECL_RESULT (current_function_decl); |
8286 | tree inner; | |
8287 | ||
8288 | current_function_returns_value = 1; | |
8289 | if (t == error_mark_node) | |
506e2710 | 8290 | return NULL_TREE; |
3e4093b6 RS |
8291 | |
8292 | inner = t = convert (TREE_TYPE (res), t); | |
8293 | ||
8294 | /* Strip any conversions, additions, and subtractions, and see if | |
8295 | we are returning the address of a local variable. Warn if so. */ | |
8296 | while (1) | |
8b6a5902 | 8297 | { |
3e4093b6 | 8298 | switch (TREE_CODE (inner)) |
8b6a5902 | 8299 | { |
849421a3 JJ |
8300 | CASE_CONVERT: |
8301 | case NON_LVALUE_EXPR: | |
3e4093b6 | 8302 | case PLUS_EXPR: |
849421a3 | 8303 | case POINTER_PLUS_EXPR: |
3e4093b6 RS |
8304 | inner = TREE_OPERAND (inner, 0); |
8305 | continue; | |
8306 | ||
8307 | case MINUS_EXPR: | |
8308 | /* If the second operand of the MINUS_EXPR has a pointer | |
8309 | type (or is converted from it), this may be valid, so | |
8310 | don't give a warning. */ | |
8311 | { | |
8312 | tree op1 = TREE_OPERAND (inner, 1); | |
8b6a5902 | 8313 | |
3f75a254 | 8314 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) |
1043771b TB |
8315 | && (CONVERT_EXPR_P (op1) |
8316 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
3e4093b6 | 8317 | op1 = TREE_OPERAND (op1, 0); |
8b6a5902 | 8318 | |
3e4093b6 RS |
8319 | if (POINTER_TYPE_P (TREE_TYPE (op1))) |
8320 | break; | |
8b6a5902 | 8321 | |
3e4093b6 RS |
8322 | inner = TREE_OPERAND (inner, 0); |
8323 | continue; | |
8324 | } | |
400fbf9f | 8325 | |
3e4093b6 RS |
8326 | case ADDR_EXPR: |
8327 | inner = TREE_OPERAND (inner, 0); | |
c2f4acb7 | 8328 | |
6615c446 | 8329 | while (REFERENCE_CLASS_P (inner) |
c22cacf3 | 8330 | && TREE_CODE (inner) != INDIRECT_REF) |
3e4093b6 | 8331 | inner = TREE_OPERAND (inner, 0); |
8b6a5902 | 8332 | |
a2f1f4c3 | 8333 | if (DECL_P (inner) |
3f75a254 JM |
8334 | && !DECL_EXTERNAL (inner) |
8335 | && !TREE_STATIC (inner) | |
3e4093b6 | 8336 | && DECL_CONTEXT (inner) == current_function_decl) |
c2255bc4 AH |
8337 | warning_at (loc, |
8338 | 0, "function returns address of local variable"); | |
3e4093b6 | 8339 | break; |
8b6a5902 | 8340 | |
3e4093b6 RS |
8341 | default: |
8342 | break; | |
8343 | } | |
de520661 | 8344 | |
3e4093b6 RS |
8345 | break; |
8346 | } | |
8347 | ||
53fb4de3 | 8348 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); |
c2255bc4 | 8349 | SET_EXPR_LOCATION (retval, loc); |
ca085fd7 MLI |
8350 | |
8351 | if (warn_sequence_point) | |
8352 | verify_sequence_points (retval); | |
de520661 | 8353 | } |
8b6a5902 | 8354 | |
c2255bc4 | 8355 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); |
0c9b182b JJ |
8356 | TREE_NO_WARNING (ret_stmt) |= no_warning; |
8357 | return add_stmt (ret_stmt); | |
de520661 | 8358 | } |
3e4093b6 RS |
8359 | \f |
8360 | struct c_switch { | |
604f5adf ILT |
8361 | /* The SWITCH_EXPR being built. */ |
8362 | tree switch_expr; | |
a6c0a76c | 8363 | |
89dbed81 | 8364 | /* The original type of the testing expression, i.e. before the |
a6c0a76c SB |
8365 | default conversion is applied. */ |
8366 | tree orig_type; | |
8367 | ||
3e4093b6 RS |
8368 | /* A splay-tree mapping the low element of a case range to the high |
8369 | element, or NULL_TREE if there is no high element. Used to | |
8370 | determine whether or not a new case label duplicates an old case | |
8371 | label. We need a tree, rather than simply a hash table, because | |
8372 | of the GNU case range extension. */ | |
8373 | splay_tree cases; | |
a6c0a76c | 8374 | |
e1b7793c ILT |
8375 | /* The bindings at the point of the switch. This is used for |
8376 | warnings crossing decls when branching to a case label. */ | |
8377 | struct c_spot_bindings *bindings; | |
187230a7 | 8378 | |
3e4093b6 RS |
8379 | /* The next node on the stack. */ |
8380 | struct c_switch *next; | |
8381 | }; | |
400fbf9f | 8382 | |
3e4093b6 RS |
8383 | /* A stack of the currently active switch statements. The innermost |
8384 | switch statement is on the top of the stack. There is no need to | |
8385 | mark the stack for garbage collection because it is only active | |
8386 | during the processing of the body of a function, and we never | |
8387 | collect at that point. */ | |
de520661 | 8388 | |
506e2710 | 8389 | struct c_switch *c_switch_stack; |
de520661 | 8390 | |
3e4093b6 | 8391 | /* Start a C switch statement, testing expression EXP. Return the new |
c2255bc4 AH |
8392 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. |
8393 | SWITCH_COND_LOC is the location of the switch's condition. */ | |
de520661 | 8394 | |
3e4093b6 | 8395 | tree |
c2255bc4 AH |
8396 | c_start_case (location_t switch_loc, |
8397 | location_t switch_cond_loc, | |
8398 | tree exp) | |
de520661 | 8399 | { |
c58e8676 | 8400 | tree orig_type = error_mark_node; |
3e4093b6 | 8401 | struct c_switch *cs; |
2f6e4e97 | 8402 | |
3e4093b6 | 8403 | if (exp != error_mark_node) |
de520661 | 8404 | { |
3e4093b6 RS |
8405 | orig_type = TREE_TYPE (exp); |
8406 | ||
c58e8676 | 8407 | if (!INTEGRAL_TYPE_P (orig_type)) |
de520661 | 8408 | { |
c58e8676 VR |
8409 | if (orig_type != error_mark_node) |
8410 | { | |
c2255bc4 | 8411 | error_at (switch_cond_loc, "switch quantity not an integer"); |
c58e8676 VR |
8412 | orig_type = error_mark_node; |
8413 | } | |
3e4093b6 | 8414 | exp = integer_zero_node; |
de520661 | 8415 | } |
3e4093b6 | 8416 | else |
de520661 | 8417 | { |
c58e8676 | 8418 | tree type = TYPE_MAIN_VARIANT (orig_type); |
8b6a5902 | 8419 | |
3176a0c2 | 8420 | if (!in_system_header |
3e4093b6 RS |
8421 | && (type == long_integer_type_node |
8422 | || type == long_unsigned_type_node)) | |
c2255bc4 AH |
8423 | warning_at (switch_cond_loc, |
8424 | OPT_Wtraditional, "%<long%> switch expression not " | |
8425 | "converted to %<int%> in ISO C"); | |
8b6a5902 | 8426 | |
928c19bb | 8427 | exp = c_fully_fold (exp, false, NULL); |
3e4093b6 | 8428 | exp = default_conversion (exp); |
ca085fd7 MLI |
8429 | |
8430 | if (warn_sequence_point) | |
8431 | verify_sequence_points (exp); | |
3e4093b6 RS |
8432 | } |
8433 | } | |
8434 | ||
604f5adf | 8435 | /* Add this new SWITCH_EXPR to the stack. */ |
5d038c4c | 8436 | cs = XNEW (struct c_switch); |
604f5adf | 8437 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); |
c2255bc4 | 8438 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); |
a6c0a76c | 8439 | cs->orig_type = orig_type; |
3e4093b6 | 8440 | cs->cases = splay_tree_new (case_compare, NULL, NULL); |
e1b7793c | 8441 | cs->bindings = c_get_switch_bindings (); |
506e2710 RH |
8442 | cs->next = c_switch_stack; |
8443 | c_switch_stack = cs; | |
3e4093b6 | 8444 | |
604f5adf | 8445 | return add_stmt (cs->switch_expr); |
3e4093b6 RS |
8446 | } |
8447 | ||
c2255bc4 | 8448 | /* Process a case label at location LOC. */ |
3e4093b6 RS |
8449 | |
8450 | tree | |
c2255bc4 | 8451 | do_case (location_t loc, tree low_value, tree high_value) |
3e4093b6 RS |
8452 | { |
8453 | tree label = NULL_TREE; | |
8454 | ||
17cede2e JM |
8455 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) |
8456 | { | |
8457 | low_value = c_fully_fold (low_value, false, NULL); | |
8458 | if (TREE_CODE (low_value) == INTEGER_CST) | |
8459 | pedwarn (input_location, OPT_pedantic, | |
8460 | "case label is not an integer constant expression"); | |
8461 | } | |
8462 | ||
8463 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
8464 | { | |
8465 | high_value = c_fully_fold (high_value, false, NULL); | |
8466 | if (TREE_CODE (high_value) == INTEGER_CST) | |
8467 | pedwarn (input_location, OPT_pedantic, | |
8468 | "case label is not an integer constant expression"); | |
8469 | } | |
8470 | ||
e1b7793c | 8471 | if (c_switch_stack == NULL) |
187230a7 JM |
8472 | { |
8473 | if (low_value) | |
e1b7793c | 8474 | error_at (loc, "case label not within a switch statement"); |
187230a7 | 8475 | else |
e1b7793c ILT |
8476 | error_at (loc, "%<default%> label not within a switch statement"); |
8477 | return NULL_TREE; | |
187230a7 | 8478 | } |
de520661 | 8479 | |
e1b7793c ILT |
8480 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, |
8481 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
8482 | loc)) | |
8483 | return NULL_TREE; | |
8484 | ||
8485 | label = c_add_case_label (loc, c_switch_stack->cases, | |
8486 | SWITCH_COND (c_switch_stack->switch_expr), | |
8487 | c_switch_stack->orig_type, | |
8488 | low_value, high_value); | |
8489 | if (label == error_mark_node) | |
8490 | label = NULL_TREE; | |
3e4093b6 RS |
8491 | return label; |
8492 | } | |
de520661 | 8493 | |
3e4093b6 | 8494 | /* Finish the switch statement. */ |
de520661 | 8495 | |
3e4093b6 | 8496 | void |
325c3691 | 8497 | c_finish_case (tree body) |
3e4093b6 | 8498 | { |
506e2710 | 8499 | struct c_switch *cs = c_switch_stack; |
fbc315db | 8500 | location_t switch_location; |
3e4093b6 | 8501 | |
604f5adf | 8502 | SWITCH_BODY (cs->switch_expr) = body; |
325c3691 | 8503 | |
6de9cd9a | 8504 | /* Emit warnings as needed. */ |
c2255bc4 | 8505 | switch_location = EXPR_LOCATION (cs->switch_expr); |
fbc315db ILT |
8506 | c_do_switch_warnings (cs->cases, switch_location, |
8507 | TREE_TYPE (cs->switch_expr), | |
8508 | SWITCH_COND (cs->switch_expr)); | |
6de9cd9a | 8509 | |
3e4093b6 | 8510 | /* Pop the stack. */ |
506e2710 | 8511 | c_switch_stack = cs->next; |
3e4093b6 | 8512 | splay_tree_delete (cs->cases); |
e1b7793c | 8513 | c_release_switch_bindings (cs->bindings); |
5d038c4c | 8514 | XDELETE (cs); |
de520661 | 8515 | } |
325c3691 | 8516 | \f |
506e2710 RH |
8517 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, |
8518 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
8519 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
8520 | statement, and was not surrounded with parenthesis. */ | |
325c3691 | 8521 | |
9e51cf9d | 8522 | void |
506e2710 RH |
8523 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, |
8524 | tree else_block, bool nested_if) | |
325c3691 | 8525 | { |
506e2710 | 8526 | tree stmt; |
325c3691 | 8527 | |
506e2710 RH |
8528 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ |
8529 | if (warn_parentheses && nested_if && else_block == NULL) | |
325c3691 | 8530 | { |
506e2710 | 8531 | tree inner_if = then_block; |
16865eaa | 8532 | |
61ada8ae | 8533 | /* We know from the grammar productions that there is an IF nested |
506e2710 RH |
8534 | within THEN_BLOCK. Due to labels and c99 conditional declarations, |
8535 | it might not be exactly THEN_BLOCK, but should be the last | |
8536 | non-container statement within. */ | |
8537 | while (1) | |
8538 | switch (TREE_CODE (inner_if)) | |
8539 | { | |
8540 | case COND_EXPR: | |
8541 | goto found; | |
8542 | case BIND_EXPR: | |
8543 | inner_if = BIND_EXPR_BODY (inner_if); | |
8544 | break; | |
8545 | case STATEMENT_LIST: | |
8546 | inner_if = expr_last (then_block); | |
8547 | break; | |
8548 | case TRY_FINALLY_EXPR: | |
8549 | case TRY_CATCH_EXPR: | |
8550 | inner_if = TREE_OPERAND (inner_if, 0); | |
8551 | break; | |
8552 | default: | |
366de0ce | 8553 | gcc_unreachable (); |
506e2710 RH |
8554 | } |
8555 | found: | |
16865eaa | 8556 | |
506e2710 | 8557 | if (COND_EXPR_ELSE (inner_if)) |
fab922b1 MLI |
8558 | warning_at (if_locus, OPT_Wparentheses, |
8559 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
506e2710 | 8560 | } |
16865eaa | 8561 | |
2214de30 | 8562 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); |
a281759f | 8563 | SET_EXPR_LOCATION (stmt, if_locus); |
506e2710 | 8564 | add_stmt (stmt); |
325c3691 RH |
8565 | } |
8566 | ||
506e2710 RH |
8567 | /* Emit a general-purpose loop construct. START_LOCUS is the location of |
8568 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
8569 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
61ada8ae | 8570 | the statement controlled by the loop. BLAB is the break label. CLAB is |
506e2710 | 8571 | the continue label. Everything is allowed to be NULL. */ |
325c3691 RH |
8572 | |
8573 | void | |
506e2710 RH |
8574 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, |
8575 | tree blab, tree clab, bool cond_is_first) | |
325c3691 | 8576 | { |
506e2710 RH |
8577 | tree entry = NULL, exit = NULL, t; |
8578 | ||
28af952a RS |
8579 | /* If the condition is zero don't generate a loop construct. */ |
8580 | if (cond && integer_zerop (cond)) | |
8581 | { | |
8582 | if (cond_is_first) | |
8583 | { | |
8584 | t = build_and_jump (&blab); | |
8585 | SET_EXPR_LOCATION (t, start_locus); | |
8586 | add_stmt (t); | |
8587 | } | |
8588 | } | |
8589 | else | |
506e2710 RH |
8590 | { |
8591 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
c22cacf3 | 8592 | |
506e2710 | 8593 | /* If we have an exit condition, then we build an IF with gotos either |
c22cacf3 MS |
8594 | out of the loop, or to the top of it. If there's no exit condition, |
8595 | then we just build a jump back to the top. */ | |
506e2710 | 8596 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); |
c22cacf3 | 8597 | |
28af952a | 8598 | if (cond && !integer_nonzerop (cond)) |
c22cacf3 MS |
8599 | { |
8600 | /* Canonicalize the loop condition to the end. This means | |
8601 | generating a branch to the loop condition. Reuse the | |
8602 | continue label, if possible. */ | |
8603 | if (cond_is_first) | |
8604 | { | |
8605 | if (incr || !clab) | |
8606 | { | |
8607 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
8608 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
8609 | } | |
8610 | else | |
8611 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
a281759f | 8612 | SET_EXPR_LOCATION (t, start_locus); |
c22cacf3 MS |
8613 | add_stmt (t); |
8614 | } | |
8615 | ||
506e2710 | 8616 | t = build_and_jump (&blab); |
506e2710 | 8617 | if (cond_is_first) |
db3927fb AH |
8618 | exit = fold_build3_loc (start_locus, |
8619 | COND_EXPR, void_type_node, cond, exit, t); | |
506e2710 | 8620 | else |
db3927fb AH |
8621 | exit = fold_build3_loc (input_location, |
8622 | COND_EXPR, void_type_node, cond, exit, t); | |
c22cacf3 MS |
8623 | } |
8624 | ||
506e2710 RH |
8625 | add_stmt (top); |
8626 | } | |
c22cacf3 | 8627 | |
506e2710 RH |
8628 | if (body) |
8629 | add_stmt (body); | |
8630 | if (clab) | |
8631 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
8632 | if (incr) | |
8633 | add_stmt (incr); | |
8634 | if (entry) | |
8635 | add_stmt (entry); | |
8636 | if (exit) | |
8637 | add_stmt (exit); | |
8638 | if (blab) | |
8639 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
325c3691 | 8640 | } |
325c3691 RH |
8641 | |
8642 | tree | |
c2255bc4 | 8643 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) |
325c3691 | 8644 | { |
089efaa4 | 8645 | bool skip; |
506e2710 | 8646 | tree label = *label_p; |
325c3691 | 8647 | |
089efaa4 ILT |
8648 | /* In switch statements break is sometimes stylistically used after |
8649 | a return statement. This can lead to spurious warnings about | |
8650 | control reaching the end of a non-void function when it is | |
8651 | inlined. Note that we are calling block_may_fallthru with | |
8652 | language specific tree nodes; this works because | |
8653 | block_may_fallthru returns true when given something it does not | |
8654 | understand. */ | |
8655 | skip = !block_may_fallthru (cur_stmt_list); | |
8656 | ||
506e2710 | 8657 | if (!label) |
089efaa4 ILT |
8658 | { |
8659 | if (!skip) | |
c2255bc4 | 8660 | *label_p = label = create_artificial_label (loc); |
089efaa4 | 8661 | } |
953ff289 DN |
8662 | else if (TREE_CODE (label) == LABEL_DECL) |
8663 | ; | |
8664 | else switch (TREE_INT_CST_LOW (label)) | |
506e2710 | 8665 | { |
953ff289 | 8666 | case 0: |
506e2710 | 8667 | if (is_break) |
c2255bc4 | 8668 | error_at (loc, "break statement not within loop or switch"); |
506e2710 | 8669 | else |
c2255bc4 | 8670 | error_at (loc, "continue statement not within a loop"); |
506e2710 | 8671 | return NULL_TREE; |
953ff289 DN |
8672 | |
8673 | case 1: | |
8674 | gcc_assert (is_break); | |
c2255bc4 | 8675 | error_at (loc, "break statement used with OpenMP for loop"); |
953ff289 DN |
8676 | return NULL_TREE; |
8677 | ||
8678 | default: | |
8679 | gcc_unreachable (); | |
506e2710 | 8680 | } |
325c3691 | 8681 | |
089efaa4 ILT |
8682 | if (skip) |
8683 | return NULL_TREE; | |
8684 | ||
2e28e797 JH |
8685 | if (!is_break) |
8686 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
8687 | ||
53fb4de3 | 8688 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); |
325c3691 RH |
8689 | } |
8690 | ||
506e2710 | 8691 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ |
3a5b9284 RH |
8692 | |
8693 | static void | |
c2255bc4 | 8694 | emit_side_effect_warnings (location_t loc, tree expr) |
3a5b9284 | 8695 | { |
e6b5a630 RH |
8696 | if (expr == error_mark_node) |
8697 | ; | |
8698 | else if (!TREE_SIDE_EFFECTS (expr)) | |
3a5b9284 RH |
8699 | { |
8700 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
c2255bc4 | 8701 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); |
3a5b9284 | 8702 | } |
27f33b15 | 8703 | else |
c2255bc4 | 8704 | warn_if_unused_value (expr, loc); |
3a5b9284 RH |
8705 | } |
8706 | ||
506e2710 | 8707 | /* Process an expression as if it were a complete statement. Emit |
c2255bc4 AH |
8708 | diagnostics, but do not call ADD_STMT. LOC is the location of the |
8709 | statement. */ | |
3a5b9284 | 8710 | |
506e2710 | 8711 | tree |
c2255bc4 | 8712 | c_process_expr_stmt (location_t loc, tree expr) |
3a5b9284 RH |
8713 | { |
8714 | if (!expr) | |
506e2710 | 8715 | return NULL_TREE; |
3a5b9284 | 8716 | |
928c19bb JM |
8717 | expr = c_fully_fold (expr, false, NULL); |
8718 | ||
3a5b9284 RH |
8719 | if (warn_sequence_point) |
8720 | verify_sequence_points (expr); | |
8721 | ||
8722 | if (TREE_TYPE (expr) != error_mark_node | |
8723 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
8724 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
c2255bc4 | 8725 | error_at (loc, "expression statement has incomplete type"); |
3a5b9284 RH |
8726 | |
8727 | /* If we're not processing a statement expression, warn about unused values. | |
8728 | Warnings for statement expressions will be emitted later, once we figure | |
8729 | out which is the result. */ | |
8730 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
27f33b15 | 8731 | && warn_unused_value) |
c2255bc4 | 8732 | emit_side_effect_warnings (loc, expr); |
3a5b9284 RH |
8733 | |
8734 | /* If the expression is not of a type to which we cannot assign a line | |
8735 | number, wrap the thing in a no-op NOP_EXPR. */ | |
6615c446 | 8736 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) |
c2255bc4 AH |
8737 | { |
8738 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
8739 | SET_EXPR_LOCATION (expr, loc); | |
8740 | } | |
506e2710 RH |
8741 | |
8742 | return expr; | |
8743 | } | |
8744 | ||
c2255bc4 AH |
8745 | /* Emit an expression as a statement. LOC is the location of the |
8746 | expression. */ | |
506e2710 RH |
8747 | |
8748 | tree | |
c2255bc4 | 8749 | c_finish_expr_stmt (location_t loc, tree expr) |
506e2710 RH |
8750 | { |
8751 | if (expr) | |
c2255bc4 | 8752 | return add_stmt (c_process_expr_stmt (loc, expr)); |
506e2710 RH |
8753 | else |
8754 | return NULL; | |
3a5b9284 RH |
8755 | } |
8756 | ||
8757 | /* Do the opposite and emit a statement as an expression. To begin, | |
8758 | create a new binding level and return it. */ | |
325c3691 RH |
8759 | |
8760 | tree | |
8761 | c_begin_stmt_expr (void) | |
8762 | { | |
8763 | tree ret; | |
8764 | ||
8765 | /* We must force a BLOCK for this level so that, if it is not expanded | |
8766 | later, there is a way to turn off the entire subtree of blocks that | |
8767 | are contained in it. */ | |
8768 | keep_next_level (); | |
8769 | ret = c_begin_compound_stmt (true); | |
e1b7793c ILT |
8770 | |
8771 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
8772 | ? NULL | |
8773 | : c_switch_stack->bindings); | |
325c3691 RH |
8774 | |
8775 | /* Mark the current statement list as belonging to a statement list. */ | |
8776 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
8777 | ||
8778 | return ret; | |
8779 | } | |
8780 | ||
c2255bc4 AH |
8781 | /* LOC is the location of the compound statement to which this body |
8782 | belongs. */ | |
8783 | ||
325c3691 | 8784 | tree |
c2255bc4 | 8785 | c_finish_stmt_expr (location_t loc, tree body) |
325c3691 | 8786 | { |
3a5b9284 | 8787 | tree last, type, tmp, val; |
325c3691 RH |
8788 | tree *last_p; |
8789 | ||
c2255bc4 | 8790 | body = c_end_compound_stmt (loc, body, true); |
e1b7793c ILT |
8791 | |
8792 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
8793 | ? NULL | |
8794 | : c_switch_stack->bindings); | |
325c3691 | 8795 | |
3a5b9284 RH |
8796 | /* Locate the last statement in BODY. See c_end_compound_stmt |
8797 | about always returning a BIND_EXPR. */ | |
8798 | last_p = &BIND_EXPR_BODY (body); | |
8799 | last = BIND_EXPR_BODY (body); | |
8800 | ||
8801 | continue_searching: | |
325c3691 RH |
8802 | if (TREE_CODE (last) == STATEMENT_LIST) |
8803 | { | |
3a5b9284 RH |
8804 | tree_stmt_iterator i; |
8805 | ||
8806 | /* This can happen with degenerate cases like ({ }). No value. */ | |
8807 | if (!TREE_SIDE_EFFECTS (last)) | |
8808 | return body; | |
8809 | ||
8810 | /* If we're supposed to generate side effects warnings, process | |
8811 | all of the statements except the last. */ | |
27f33b15 | 8812 | if (warn_unused_value) |
325c3691 | 8813 | { |
3a5b9284 | 8814 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) |
c2255bc4 AH |
8815 | { |
8816 | location_t tloc; | |
8817 | tree t = tsi_stmt (i); | |
8818 | ||
8819 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
8820 | emit_side_effect_warnings (tloc, t); | |
8821 | } | |
325c3691 RH |
8822 | } |
8823 | else | |
3a5b9284 RH |
8824 | i = tsi_last (last); |
8825 | last_p = tsi_stmt_ptr (i); | |
8826 | last = *last_p; | |
325c3691 RH |
8827 | } |
8828 | ||
3a5b9284 RH |
8829 | /* If the end of the list is exception related, then the list was split |
8830 | by a call to push_cleanup. Continue searching. */ | |
8831 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
8832 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
8833 | { | |
8834 | last_p = &TREE_OPERAND (last, 0); | |
8835 | last = *last_p; | |
8836 | goto continue_searching; | |
8837 | } | |
8838 | ||
26d8af35 JM |
8839 | if (last == error_mark_node) |
8840 | return last; | |
8841 | ||
3a5b9284 RH |
8842 | /* In the case that the BIND_EXPR is not necessary, return the |
8843 | expression out from inside it. */ | |
26d8af35 JM |
8844 | if (last == BIND_EXPR_BODY (body) |
8845 | && BIND_EXPR_VARS (body) == NULL) | |
591baeb0 | 8846 | { |
928c19bb JM |
8847 | /* Even if this looks constant, do not allow it in a constant |
8848 | expression. */ | |
e5a94231 | 8849 | last = c_wrap_maybe_const (last, true); |
591baeb0 JM |
8850 | /* Do not warn if the return value of a statement expression is |
8851 | unused. */ | |
928c19bb | 8852 | TREE_NO_WARNING (last) = 1; |
591baeb0 JM |
8853 | return last; |
8854 | } | |
325c3691 RH |
8855 | |
8856 | /* Extract the type of said expression. */ | |
8857 | type = TREE_TYPE (last); | |
325c3691 | 8858 | |
3a5b9284 RH |
8859 | /* If we're not returning a value at all, then the BIND_EXPR that |
8860 | we already have is a fine expression to return. */ | |
8861 | if (!type || VOID_TYPE_P (type)) | |
8862 | return body; | |
8863 | ||
8864 | /* Now that we've located the expression containing the value, it seems | |
8865 | silly to make voidify_wrapper_expr repeat the process. Create a | |
8866 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
8867 | tmp = create_tmp_var_raw (type, NULL); | |
8868 | ||
8869 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
8870 | tree_expr_nonnegative_p giving up immediately. */ | |
8871 | val = last; | |
8872 | if (TREE_CODE (val) == NOP_EXPR | |
8873 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
8874 | val = TREE_OPERAND (val, 0); | |
8875 | ||
53fb4de3 | 8876 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); |
5e278028 | 8877 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); |
3a5b9284 | 8878 | |
c2255bc4 AH |
8879 | { |
8880 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
8881 | SET_EXPR_LOCATION (t, loc); | |
8882 | return t; | |
8883 | } | |
325c3691 RH |
8884 | } |
8885 | \f | |
8886 | /* Begin and end compound statements. This is as simple as pushing | |
8887 | and popping new statement lists from the tree. */ | |
8888 | ||
8889 | tree | |
8890 | c_begin_compound_stmt (bool do_scope) | |
8891 | { | |
8892 | tree stmt = push_stmt_list (); | |
8893 | if (do_scope) | |
4dfa0342 | 8894 | push_scope (); |
325c3691 RH |
8895 | return stmt; |
8896 | } | |
8897 | ||
c2255bc4 AH |
8898 | /* End a compound statement. STMT is the statement. LOC is the |
8899 | location of the compound statement-- this is usually the location | |
8900 | of the opening brace. */ | |
8901 | ||
325c3691 | 8902 | tree |
c2255bc4 | 8903 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) |
325c3691 RH |
8904 | { |
8905 | tree block = NULL; | |
8906 | ||
8907 | if (do_scope) | |
8908 | { | |
8909 | if (c_dialect_objc ()) | |
8910 | objc_clear_super_receiver (); | |
8911 | block = pop_scope (); | |
8912 | } | |
8913 | ||
8914 | stmt = pop_stmt_list (stmt); | |
c2255bc4 | 8915 | stmt = c_build_bind_expr (loc, block, stmt); |
325c3691 RH |
8916 | |
8917 | /* If this compound statement is nested immediately inside a statement | |
8918 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
8919 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
8920 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
8921 | was really last. */ | |
8922 | if (cur_stmt_list | |
8923 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
8924 | && TREE_CODE (stmt) != BIND_EXPR) | |
8925 | { | |
53fb4de3 | 8926 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); |
325c3691 | 8927 | TREE_SIDE_EFFECTS (stmt) = 1; |
c2255bc4 | 8928 | SET_EXPR_LOCATION (stmt, loc); |
325c3691 RH |
8929 | } |
8930 | ||
8931 | return stmt; | |
8932 | } | |
5a508662 RH |
8933 | |
8934 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
8935 | when the current scope is exited. EH_ONLY is true when this is not | |
8936 | meant to apply to normal control flow transfer. */ | |
8937 | ||
8938 | void | |
c2255bc4 | 8939 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
5a508662 | 8940 | { |
3a5b9284 RH |
8941 | enum tree_code code; |
8942 | tree stmt, list; | |
8943 | bool stmt_expr; | |
8944 | ||
8945 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
c2255bc4 | 8946 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); |
5a508662 | 8947 | add_stmt (stmt); |
3a5b9284 RH |
8948 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); |
8949 | list = push_stmt_list (); | |
8950 | TREE_OPERAND (stmt, 0) = list; | |
8951 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
5a508662 | 8952 | } |
325c3691 | 8953 | \f |
3e4093b6 RS |
8954 | /* Build a binary-operation expression without default conversions. |
8955 | CODE is the kind of expression to build. | |
ba47d38d | 8956 | LOCATION is the operator's location. |
3e4093b6 RS |
8957 | This function differs from `build' in several ways: |
8958 | the data type of the result is computed and recorded in it, | |
8959 | warnings are generated if arg data types are invalid, | |
8960 | special handling for addition and subtraction of pointers is known, | |
8961 | and some optimization is done (operations on narrow ints | |
8962 | are done in the narrower type when that gives the same result). | |
8963 | Constant folding is also done before the result is returned. | |
de520661 | 8964 | |
3e4093b6 RS |
8965 | Note that the operands will never have enumeral types, or function |
8966 | or array types, because either they will have the default conversions | |
8967 | performed or they have both just been converted to some other type in which | |
8968 | the arithmetic is to be done. */ | |
8969 | ||
8970 | tree | |
ba47d38d AH |
8971 | build_binary_op (location_t location, enum tree_code code, |
8972 | tree orig_op0, tree orig_op1, int convert_p) | |
de520661 | 8973 | { |
8ce94e44 JM |
8974 | tree type0, type1, orig_type0, orig_type1; |
8975 | tree eptype; | |
3e4093b6 RS |
8976 | enum tree_code code0, code1; |
8977 | tree op0, op1; | |
c9f9eb5d | 8978 | tree ret = error_mark_node; |
4de67c26 | 8979 | const char *invalid_op_diag; |
4d84fe7c | 8980 | bool op0_int_operands, op1_int_operands; |
928c19bb | 8981 | bool int_const, int_const_or_overflow, int_operands; |
b62acd60 | 8982 | |
3e4093b6 RS |
8983 | /* Expression code to give to the expression when it is built. |
8984 | Normally this is CODE, which is what the caller asked for, | |
8985 | but in some special cases we change it. */ | |
8986 | enum tree_code resultcode = code; | |
8b6a5902 | 8987 | |
3e4093b6 RS |
8988 | /* Data type in which the computation is to be performed. |
8989 | In the simplest cases this is the common type of the arguments. */ | |
8990 | tree result_type = NULL; | |
8991 | ||
8ce94e44 JM |
8992 | /* When the computation is in excess precision, the type of the |
8993 | final EXCESS_PRECISION_EXPR. */ | |
8994 | tree real_result_type = NULL; | |
8995 | ||
3e4093b6 RS |
8996 | /* Nonzero means operands have already been type-converted |
8997 | in whatever way is necessary. | |
8998 | Zero means they need to be converted to RESULT_TYPE. */ | |
8999 | int converted = 0; | |
9000 | ||
9001 | /* Nonzero means create the expression with this type, rather than | |
9002 | RESULT_TYPE. */ | |
9003 | tree build_type = 0; | |
9004 | ||
9005 | /* Nonzero means after finally constructing the expression | |
9006 | convert it to this type. */ | |
9007 | tree final_type = 0; | |
9008 | ||
9009 | /* Nonzero if this is an operation like MIN or MAX which can | |
9010 | safely be computed in short if both args are promoted shorts. | |
9011 | Also implies COMMON. | |
9012 | -1 indicates a bitwise operation; this makes a difference | |
9013 | in the exact conditions for when it is safe to do the operation | |
9014 | in a narrower mode. */ | |
9015 | int shorten = 0; | |
9016 | ||
9017 | /* Nonzero if this is a comparison operation; | |
9018 | if both args are promoted shorts, compare the original shorts. | |
9019 | Also implies COMMON. */ | |
9020 | int short_compare = 0; | |
9021 | ||
9022 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
9023 | original short and then promoted if the operand is a promoted short. */ | |
9024 | int short_shift = 0; | |
9025 | ||
9026 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
9027 | int common = 0; | |
9028 | ||
58393038 ZL |
9029 | /* True means types are compatible as far as ObjC is concerned. */ |
9030 | bool objc_ok; | |
9031 | ||
8ce94e44 JM |
9032 | /* True means this is an arithmetic operation that may need excess |
9033 | precision. */ | |
9034 | bool may_need_excess_precision; | |
9035 | ||
ba47d38d AH |
9036 | if (location == UNKNOWN_LOCATION) |
9037 | location = input_location; | |
9038 | ||
4d84fe7c JM |
9039 | op0 = orig_op0; |
9040 | op1 = orig_op1; | |
9041 | ||
9042 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
9043 | if (op0_int_operands) | |
9044 | op0 = remove_c_maybe_const_expr (op0); | |
9045 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
9046 | if (op1_int_operands) | |
9047 | op1 = remove_c_maybe_const_expr (op1); | |
9048 | int_operands = (op0_int_operands && op1_int_operands); | |
928c19bb JM |
9049 | if (int_operands) |
9050 | { | |
9051 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
9052 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
9053 | int_const = (int_const_or_overflow | |
9054 | && !TREE_OVERFLOW (orig_op0) | |
9055 | && !TREE_OVERFLOW (orig_op1)); | |
9056 | } | |
9057 | else | |
9058 | int_const = int_const_or_overflow = false; | |
9059 | ||
3e4093b6 | 9060 | if (convert_p) |
790e9490 | 9061 | { |
4d84fe7c JM |
9062 | op0 = default_conversion (op0); |
9063 | op1 = default_conversion (op1); | |
790e9490 RS |
9064 | } |
9065 | ||
8ce94e44 JM |
9066 | orig_type0 = type0 = TREE_TYPE (op0); |
9067 | orig_type1 = type1 = TREE_TYPE (op1); | |
91fa3c30 | 9068 | |
3e4093b6 RS |
9069 | /* The expression codes of the data types of the arguments tell us |
9070 | whether the arguments are integers, floating, pointers, etc. */ | |
9071 | code0 = TREE_CODE (type0); | |
9072 | code1 = TREE_CODE (type1); | |
9073 | ||
9074 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
9075 | STRIP_TYPE_NOPS (op0); | |
9076 | STRIP_TYPE_NOPS (op1); | |
9077 | ||
9078 | /* If an error was already reported for one of the arguments, | |
9079 | avoid reporting another error. */ | |
9080 | ||
9081 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
9082 | return error_mark_node; | |
9083 | ||
4de67c26 JM |
9084 | if ((invalid_op_diag |
9085 | = targetm.invalid_binary_op (code, type0, type1))) | |
9086 | { | |
ba47d38d | 9087 | error_at (location, invalid_op_diag); |
4de67c26 JM |
9088 | return error_mark_node; |
9089 | } | |
9090 | ||
8ce94e44 JM |
9091 | switch (code) |
9092 | { | |
9093 | case PLUS_EXPR: | |
9094 | case MINUS_EXPR: | |
9095 | case MULT_EXPR: | |
9096 | case TRUNC_DIV_EXPR: | |
9097 | case CEIL_DIV_EXPR: | |
9098 | case FLOOR_DIV_EXPR: | |
9099 | case ROUND_DIV_EXPR: | |
9100 | case EXACT_DIV_EXPR: | |
9101 | may_need_excess_precision = true; | |
9102 | break; | |
9103 | default: | |
9104 | may_need_excess_precision = false; | |
9105 | break; | |
9106 | } | |
9107 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
9108 | { | |
9109 | op0 = TREE_OPERAND (op0, 0); | |
9110 | type0 = TREE_TYPE (op0); | |
9111 | } | |
9112 | else if (may_need_excess_precision | |
9113 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
9114 | { | |
9115 | type0 = eptype; | |
9116 | op0 = convert (eptype, op0); | |
9117 | } | |
9118 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
9119 | { | |
9120 | op1 = TREE_OPERAND (op1, 0); | |
9121 | type1 = TREE_TYPE (op1); | |
9122 | } | |
9123 | else if (may_need_excess_precision | |
9124 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
9125 | { | |
9126 | type1 = eptype; | |
9127 | op1 = convert (eptype, op1); | |
9128 | } | |
9129 | ||
58393038 ZL |
9130 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); |
9131 | ||
3e4093b6 | 9132 | switch (code) |
de520661 | 9133 | { |
3e4093b6 RS |
9134 | case PLUS_EXPR: |
9135 | /* Handle the pointer + int case. */ | |
9136 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 9137 | { |
db3927fb | 9138 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); |
c9f9eb5d AH |
9139 | goto return_build_binary_op; |
9140 | } | |
3e4093b6 | 9141 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) |
c9f9eb5d | 9142 | { |
db3927fb | 9143 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); |
c9f9eb5d AH |
9144 | goto return_build_binary_op; |
9145 | } | |
fe67cf58 | 9146 | else |
3e4093b6 RS |
9147 | common = 1; |
9148 | break; | |
400fbf9f | 9149 | |
3e4093b6 RS |
9150 | case MINUS_EXPR: |
9151 | /* Subtraction of two similar pointers. | |
9152 | We must subtract them as integers, then divide by object size. */ | |
9153 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
744aa42f | 9154 | && comp_target_types (location, type0, type1)) |
c9f9eb5d | 9155 | { |
db3927fb | 9156 | ret = pointer_diff (location, op0, op1); |
c9f9eb5d AH |
9157 | goto return_build_binary_op; |
9158 | } | |
3e4093b6 RS |
9159 | /* Handle pointer minus int. Just like pointer plus int. */ |
9160 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
c9f9eb5d | 9161 | { |
db3927fb | 9162 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); |
c9f9eb5d AH |
9163 | goto return_build_binary_op; |
9164 | } | |
3e4093b6 RS |
9165 | else |
9166 | common = 1; | |
9167 | break; | |
8b6a5902 | 9168 | |
3e4093b6 RS |
9169 | case MULT_EXPR: |
9170 | common = 1; | |
9171 | break; | |
9172 | ||
9173 | case TRUNC_DIV_EXPR: | |
9174 | case CEIL_DIV_EXPR: | |
9175 | case FLOOR_DIV_EXPR: | |
9176 | case ROUND_DIV_EXPR: | |
9177 | case EXACT_DIV_EXPR: | |
c9f9eb5d | 9178 | warn_for_div_by_zero (location, op1); |
3e4093b6 RS |
9179 | |
9180 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 9181 | || code0 == FIXED_POINT_TYPE |
3e4093b6 RS |
9182 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
9183 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
ab22c1fa | 9184 | || code1 == FIXED_POINT_TYPE |
3e4093b6 | 9185 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 9186 | { |
5bed876a AH |
9187 | enum tree_code tcode0 = code0, tcode1 = code1; |
9188 | ||
3a021db2 | 9189 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) |
5bed876a | 9190 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); |
3a021db2 | 9191 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) |
5bed876a | 9192 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); |
3a021db2 | 9193 | |
ab22c1fa CF |
9194 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) |
9195 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
3e4093b6 RS |
9196 | resultcode = RDIV_EXPR; |
9197 | else | |
9198 | /* Although it would be tempting to shorten always here, that | |
9199 | loses on some targets, since the modulo instruction is | |
9200 | undefined if the quotient can't be represented in the | |
9201 | computation mode. We shorten only if unsigned or if | |
9202 | dividing by something we know != -1. */ | |
8df83eae | 9203 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 9204 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 9205 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
9206 | common = 1; |
9207 | } | |
9208 | break; | |
de520661 | 9209 | |
3e4093b6 | 9210 | case BIT_AND_EXPR: |
3e4093b6 RS |
9211 | case BIT_IOR_EXPR: |
9212 | case BIT_XOR_EXPR: | |
9213 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
9214 | shorten = -1; | |
9ef0c8d9 AP |
9215 | /* Allow vector types which are not floating point types. */ |
9216 | else if (code0 == VECTOR_TYPE | |
9217 | && code1 == VECTOR_TYPE | |
9218 | && !VECTOR_FLOAT_TYPE_P (type0) | |
9219 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
3e4093b6 RS |
9220 | common = 1; |
9221 | break; | |
9222 | ||
9223 | case TRUNC_MOD_EXPR: | |
9224 | case FLOOR_MOD_EXPR: | |
c9f9eb5d | 9225 | warn_for_div_by_zero (location, op1); |
de520661 | 9226 | |
5cfd5d9b AP |
9227 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
9228 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9229 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
9230 | common = 1; | |
9231 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
3e4093b6 RS |
9232 | { |
9233 | /* Although it would be tempting to shorten always here, that loses | |
9234 | on some targets, since the modulo instruction is undefined if the | |
9235 | quotient can't be represented in the computation mode. We shorten | |
9236 | only if unsigned or if dividing by something we know != -1. */ | |
8df83eae | 9237 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) |
3e4093b6 | 9238 | || (TREE_CODE (op1) == INTEGER_CST |
3f75a254 | 9239 | && !integer_all_onesp (op1))); |
3e4093b6 RS |
9240 | common = 1; |
9241 | } | |
9242 | break; | |
de520661 | 9243 | |
3e4093b6 RS |
9244 | case TRUTH_ANDIF_EXPR: |
9245 | case TRUTH_ORIF_EXPR: | |
9246 | case TRUTH_AND_EXPR: | |
9247 | case TRUTH_OR_EXPR: | |
9248 | case TRUTH_XOR_EXPR: | |
9249 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
ab22c1fa CF |
9250 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
9251 | || code0 == FIXED_POINT_TYPE) | |
3e4093b6 | 9252 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE |
ab22c1fa CF |
9253 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE |
9254 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
9255 | { |
9256 | /* Result of these operations is always an int, | |
9257 | but that does not mean the operands should be | |
9258 | converted to ints! */ | |
9259 | result_type = integer_type_node; | |
ba47d38d AH |
9260 | op0 = c_common_truthvalue_conversion (location, op0); |
9261 | op1 = c_common_truthvalue_conversion (location, op1); | |
3e4093b6 RS |
9262 | converted = 1; |
9263 | } | |
928c19bb JM |
9264 | if (code == TRUTH_ANDIF_EXPR) |
9265 | { | |
9266 | int_const_or_overflow = (int_operands | |
9267 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9268 | && (op0 == truthvalue_false_node | |
9269 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9270 | int_const = (int_const_or_overflow | |
9271 | && !TREE_OVERFLOW (orig_op0) | |
9272 | && (op0 == truthvalue_false_node | |
9273 | || !TREE_OVERFLOW (orig_op1))); | |
9274 | } | |
9275 | else if (code == TRUTH_ORIF_EXPR) | |
9276 | { | |
9277 | int_const_or_overflow = (int_operands | |
9278 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9279 | && (op0 == truthvalue_true_node | |
9280 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9281 | int_const = (int_const_or_overflow | |
9282 | && !TREE_OVERFLOW (orig_op0) | |
9283 | && (op0 == truthvalue_true_node | |
9284 | || !TREE_OVERFLOW (orig_op1))); | |
9285 | } | |
3e4093b6 | 9286 | break; |
eba80994 | 9287 | |
3e4093b6 RS |
9288 | /* Shift operations: result has same type as first operand; |
9289 | always convert second operand to int. | |
9290 | Also set SHORT_SHIFT if shifting rightward. */ | |
de520661 | 9291 | |
3e4093b6 | 9292 | case RSHIFT_EXPR: |
ab22c1fa CF |
9293 | if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) |
9294 | && code1 == INTEGER_TYPE) | |
3e4093b6 | 9295 | { |
928c19bb | 9296 | if (TREE_CODE (op1) == INTEGER_CST) |
b62acd60 | 9297 | { |
3e4093b6 | 9298 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
9299 | { |
9300 | int_const = false; | |
7d882b83 | 9301 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9302 | warning (0, "right shift count is negative"); |
9303 | } | |
3e4093b6 | 9304 | else |
bbb818c6 | 9305 | { |
3f75a254 | 9306 | if (!integer_zerop (op1)) |
3e4093b6 RS |
9307 | short_shift = 1; |
9308 | ||
9309 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
928c19bb JM |
9310 | { |
9311 | int_const = false; | |
7d882b83 | 9312 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9313 | warning (0, "right shift count >= width of type"); |
9314 | } | |
bbb818c6 | 9315 | } |
b62acd60 | 9316 | } |
de520661 | 9317 | |
3e4093b6 RS |
9318 | /* Use the type of the value to be shifted. */ |
9319 | result_type = type0; | |
9320 | /* Convert the shift-count to an integer, regardless of size | |
9321 | of value being shifted. */ | |
9322 | if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
9323 | op1 = convert (integer_type_node, op1); | |
9324 | /* Avoid converting op1 to result_type later. */ | |
9325 | converted = 1; | |
400fbf9f | 9326 | } |
3e4093b6 | 9327 | break; |
253b6b82 | 9328 | |
3e4093b6 | 9329 | case LSHIFT_EXPR: |
ab22c1fa CF |
9330 | if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) |
9331 | && code1 == INTEGER_TYPE) | |
3e4093b6 | 9332 | { |
928c19bb | 9333 | if (TREE_CODE (op1) == INTEGER_CST) |
de520661 | 9334 | { |
3e4093b6 | 9335 | if (tree_int_cst_sgn (op1) < 0) |
928c19bb JM |
9336 | { |
9337 | int_const = false; | |
7d882b83 | 9338 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9339 | warning (0, "left shift count is negative"); |
9340 | } | |
de520661 | 9341 | |
3e4093b6 | 9342 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) |
928c19bb JM |
9343 | { |
9344 | int_const = false; | |
7d882b83 | 9345 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9346 | warning (0, "left shift count >= width of type"); |
9347 | } | |
94ba5069 | 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; |
de520661 | 9360 | |
3e4093b6 RS |
9361 | case EQ_EXPR: |
9362 | case NE_EXPR: | |
ae311566 | 9363 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) |
ba47d38d AH |
9364 | warning_at (location, |
9365 | OPT_Wfloat_equal, | |
9366 | "comparing floating point with == or != is unsafe"); | |
3e4093b6 RS |
9367 | /* Result of comparison is always int, |
9368 | but don't convert the args to int! */ | |
9369 | build_type = integer_type_node; | |
9370 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
ab22c1fa | 9371 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) |
3e4093b6 | 9372 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE |
ab22c1fa | 9373 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) |
3e4093b6 RS |
9374 | short_compare = 1; |
9375 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
9376 | { | |
9377 | tree tt0 = TREE_TYPE (type0); | |
9378 | tree tt1 = TREE_TYPE (type1); | |
36c5e70a BE |
9379 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); |
9380 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
9381 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
9382 | ||
3e4093b6 RS |
9383 | /* Anything compares with void *. void * compares with anything. |
9384 | Otherwise, the targets must be compatible | |
9385 | and both must be object or both incomplete. */ | |
744aa42f | 9386 | if (comp_target_types (location, type0, type1)) |
10bc1b1b | 9387 | result_type = common_pointer_type (type0, type1); |
36c5e70a BE |
9388 | else if (null_pointer_constant_p (orig_op0)) |
9389 | result_type = type1; | |
9390 | else if (null_pointer_constant_p (orig_op1)) | |
9391 | result_type = type0; | |
9392 | else if (!addr_space_superset (as0, as1, &as_common)) | |
9393 | { | |
9394 | error_at (location, "comparison of pointers to " | |
9395 | "disjoint address spaces"); | |
9396 | return error_mark_node; | |
9397 | } | |
3e4093b6 | 9398 | else if (VOID_TYPE_P (tt0)) |
ee2990e7 | 9399 | { |
36c5e70a | 9400 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) |
ba47d38d | 9401 | pedwarn (location, OPT_pedantic, "ISO C forbids " |
fcf73884 | 9402 | "comparison of %<void *%> with function pointer"); |
ee2990e7 | 9403 | } |
3e4093b6 | 9404 | else if (VOID_TYPE_P (tt1)) |
e6834654 | 9405 | { |
36c5e70a | 9406 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) |
ba47d38d | 9407 | pedwarn (location, OPT_pedantic, "ISO C forbids " |
fcf73884 | 9408 | "comparison of %<void *%> with function pointer"); |
e6834654 | 9409 | } |
3e4093b6 | 9410 | else |
58393038 ZL |
9411 | /* Avoid warning about the volatile ObjC EH puts on decls. */ |
9412 | if (!objc_ok) | |
ba47d38d | 9413 | pedwarn (location, 0, |
509c9d60 | 9414 | "comparison of distinct pointer types lacks a cast"); |
e6834654 | 9415 | |
3e4093b6 | 9416 | if (result_type == NULL_TREE) |
36c5e70a BE |
9417 | { |
9418 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
9419 | result_type = build_pointer_type | |
9420 | (build_qualified_type (void_type_node, qual)); | |
9421 | } | |
e6834654 | 9422 | } |
6aa3c60d | 9423 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
690a704a BE |
9424 | { |
9425 | if (TREE_CODE (op0) == ADDR_EXPR | |
b3c6d2ea | 9426 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) |
ba47d38d AH |
9427 | warning_at (location, |
9428 | OPT_Waddress, "the address of %qD will never be NULL", | |
9429 | TREE_OPERAND (op0, 0)); | |
690a704a BE |
9430 | result_type = type0; |
9431 | } | |
6aa3c60d | 9432 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
690a704a | 9433 | { |
c22cacf3 | 9434 | if (TREE_CODE (op1) == ADDR_EXPR |
b3c6d2ea | 9435 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) |
ba47d38d AH |
9436 | warning_at (location, |
9437 | OPT_Waddress, "the address of %qD will never be NULL", | |
9438 | TREE_OPERAND (op1, 0)); | |
690a704a BE |
9439 | result_type = type1; |
9440 | } | |
3e4093b6 | 9441 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) |
de520661 | 9442 | { |
3e4093b6 | 9443 | result_type = type0; |
ba47d38d | 9444 | pedwarn (location, 0, "comparison between pointer and integer"); |
de520661 | 9445 | } |
3e4093b6 | 9446 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) |
8b6a5902 | 9447 | { |
3e4093b6 | 9448 | result_type = type1; |
ba47d38d | 9449 | pedwarn (location, 0, "comparison between pointer and integer"); |
8b6a5902 | 9450 | } |
3e4093b6 | 9451 | break; |
8b6a5902 | 9452 | |
3e4093b6 RS |
9453 | case LE_EXPR: |
9454 | case GE_EXPR: | |
9455 | case LT_EXPR: | |
9456 | case GT_EXPR: | |
9457 | build_type = integer_type_node; | |
ab22c1fa CF |
9458 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE |
9459 | || code0 == FIXED_POINT_TYPE) | |
9460 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
9461 | || code1 == FIXED_POINT_TYPE)) | |
3e4093b6 RS |
9462 | short_compare = 1; |
9463 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
9464 | { | |
36c5e70a BE |
9465 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); |
9466 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
9467 | addr_space_t as_common; | |
9468 | ||
744aa42f | 9469 | if (comp_target_types (location, type0, type1)) |
3e4093b6 | 9470 | { |
10bc1b1b | 9471 | result_type = common_pointer_type (type0, type1); |
3e4093b6 RS |
9472 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) |
9473 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
ba47d38d | 9474 | pedwarn (location, 0, |
509c9d60 | 9475 | "comparison of complete and incomplete pointers"); |
fcf73884 | 9476 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) |
ba47d38d | 9477 | pedwarn (location, OPT_pedantic, "ISO C forbids " |
fcf73884 | 9478 | "ordered comparisons of pointers to functions"); |
3e4093b6 | 9479 | } |
36c5e70a BE |
9480 | else if (!addr_space_superset (as0, as1, &as_common)) |
9481 | { | |
9482 | error_at (location, "comparison of pointers to " | |
9483 | "disjoint address spaces"); | |
9484 | return error_mark_node; | |
9485 | } | |
3e4093b6 RS |
9486 | else |
9487 | { | |
36c5e70a BE |
9488 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); |
9489 | result_type = build_pointer_type | |
9490 | (build_qualified_type (void_type_node, qual)); | |
ba47d38d | 9491 | pedwarn (location, 0, |
509c9d60 | 9492 | "comparison of distinct pointer types lacks a cast"); |
3e4093b6 RS |
9493 | } |
9494 | } | |
6aa3c60d | 9495 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |
3e4093b6 RS |
9496 | { |
9497 | result_type = type0; | |
fcf73884 | 9498 | if (pedantic) |
ba47d38d | 9499 | pedwarn (location, OPT_pedantic, |
fcf73884 MLI |
9500 | "ordered comparison of pointer with integer zero"); |
9501 | else if (extra_warnings) | |
ba47d38d | 9502 | warning_at (location, OPT_Wextra, |
fcf73884 | 9503 | "ordered comparison of pointer with integer zero"); |
3e4093b6 | 9504 | } |
6aa3c60d | 9505 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) |
3e4093b6 RS |
9506 | { |
9507 | result_type = type1; | |
ba47d38d | 9508 | pedwarn (location, OPT_pedantic, |
fcf73884 | 9509 | "ordered comparison of pointer with integer zero"); |
3e4093b6 RS |
9510 | } |
9511 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
9512 | { | |
9513 | result_type = type0; | |
ba47d38d | 9514 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
9515 | } |
9516 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
9517 | { | |
9518 | result_type = type1; | |
ba47d38d | 9519 | pedwarn (location, 0, "comparison between pointer and integer"); |
3e4093b6 RS |
9520 | } |
9521 | break; | |
64094f6a | 9522 | |
3e4093b6 | 9523 | default: |
37b2f290 | 9524 | gcc_unreachable (); |
c9fe6f9f | 9525 | } |
8f17b5c5 | 9526 | |
e57e265b PB |
9527 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) |
9528 | return error_mark_node; | |
9529 | ||
5bed876a AH |
9530 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE |
9531 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
9532 | || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0), | |
9533 | TREE_TYPE (type1)))) | |
9534 | { | |
ba47d38d | 9535 | binary_op_error (location, code, type0, type1); |
5bed876a AH |
9536 | return error_mark_node; |
9537 | } | |
9538 | ||
3e4093b6 | 9539 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE |
ab22c1fa | 9540 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) |
3e4093b6 RS |
9541 | && |
9542 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
ab22c1fa | 9543 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) |
400fbf9f | 9544 | { |
2ca862e9 JM |
9545 | bool first_complex = (code0 == COMPLEX_TYPE); |
9546 | bool second_complex = (code1 == COMPLEX_TYPE); | |
9547 | int none_complex = (!first_complex && !second_complex); | |
39b726dd | 9548 | |
3e4093b6 | 9549 | if (shorten || common || short_compare) |
3bf6bfcc JJ |
9550 | { |
9551 | result_type = c_common_type (type0, type1); | |
9552 | if (result_type == error_mark_node) | |
9553 | return error_mark_node; | |
9554 | } | |
400fbf9f | 9555 | |
2ca862e9 JM |
9556 | if (first_complex != second_complex |
9557 | && (code == PLUS_EXPR | |
9558 | || code == MINUS_EXPR | |
9559 | || code == MULT_EXPR | |
9560 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
9561 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
9562 | && flag_signed_zeros) | |
9563 | { | |
9564 | /* An operation on mixed real/complex operands must be | |
9565 | handled specially, but the language-independent code can | |
9566 | more easily optimize the plain complex arithmetic if | |
9567 | -fno-signed-zeros. */ | |
9568 | tree real_type = TREE_TYPE (result_type); | |
9569 | tree real, imag; | |
9570 | if (type0 != orig_type0 || type1 != orig_type1) | |
9571 | { | |
9572 | gcc_assert (may_need_excess_precision && common); | |
9573 | real_result_type = c_common_type (orig_type0, orig_type1); | |
9574 | } | |
9575 | if (first_complex) | |
9576 | { | |
9577 | if (TREE_TYPE (op0) != result_type) | |
9578 | op0 = convert_and_check (result_type, op0); | |
9579 | if (TREE_TYPE (op1) != real_type) | |
9580 | op1 = convert_and_check (real_type, op1); | |
9581 | } | |
9582 | else | |
9583 | { | |
9584 | if (TREE_TYPE (op0) != real_type) | |
9585 | op0 = convert_and_check (real_type, op0); | |
9586 | if (TREE_TYPE (op1) != result_type) | |
9587 | op1 = convert_and_check (result_type, op1); | |
9588 | } | |
9589 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
9590 | return error_mark_node; | |
9591 | if (first_complex) | |
9592 | { | |
9593 | op0 = c_save_expr (op0); | |
9594 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
9595 | op0, 1); | |
9596 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
9597 | op0, 1); | |
9598 | switch (code) | |
9599 | { | |
9600 | case MULT_EXPR: | |
9601 | case TRUNC_DIV_EXPR: | |
9602 | imag = build2 (resultcode, real_type, imag, op1); | |
9603 | /* Fall through. */ | |
9604 | case PLUS_EXPR: | |
9605 | case MINUS_EXPR: | |
9606 | real = build2 (resultcode, real_type, real, op1); | |
9607 | break; | |
9608 | default: | |
9609 | gcc_unreachable(); | |
9610 | } | |
9611 | } | |
9612 | else | |
9613 | { | |
9614 | op1 = c_save_expr (op1); | |
9615 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
9616 | op1, 1); | |
9617 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
9618 | op1, 1); | |
9619 | switch (code) | |
9620 | { | |
9621 | case MULT_EXPR: | |
9622 | imag = build2 (resultcode, real_type, op0, imag); | |
9623 | /* Fall through. */ | |
9624 | case PLUS_EXPR: | |
9625 | real = build2 (resultcode, real_type, op0, real); | |
9626 | break; | |
9627 | case MINUS_EXPR: | |
9628 | real = build2 (resultcode, real_type, op0, real); | |
9629 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
9630 | break; | |
9631 | default: | |
9632 | gcc_unreachable(); | |
9633 | } | |
9634 | } | |
9635 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
9636 | goto return_build_binary_op; | |
9637 | } | |
9638 | ||
3e4093b6 RS |
9639 | /* For certain operations (which identify themselves by shorten != 0) |
9640 | if both args were extended from the same smaller type, | |
9641 | do the arithmetic in that type and then extend. | |
400fbf9f | 9642 | |
3e4093b6 RS |
9643 | shorten !=0 and !=1 indicates a bitwise operation. |
9644 | For them, this optimization is safe only if | |
9645 | both args are zero-extended or both are sign-extended. | |
9646 | Otherwise, we might change the result. | |
9647 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
9648 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
400fbf9f | 9649 | |
3e4093b6 RS |
9650 | if (shorten && none_complex) |
9651 | { | |
3e4093b6 | 9652 | final_type = result_type; |
6715192c MLI |
9653 | result_type = shorten_binary_op (result_type, op0, op1, |
9654 | shorten == -1); | |
3e4093b6 | 9655 | } |
88a3dbc1 | 9656 | |
3e4093b6 | 9657 | /* Shifts can be shortened if shifting right. */ |
2f6e4e97 | 9658 | |
3e4093b6 RS |
9659 | if (short_shift) |
9660 | { | |
9661 | int unsigned_arg; | |
9662 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
88a3dbc1 | 9663 | |
3e4093b6 | 9664 | final_type = result_type; |
abe80e6d | 9665 | |
3e4093b6 | 9666 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) |
8df83eae | 9667 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); |
e9a25f70 | 9668 | |
3e4093b6 | 9669 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) |
be123439 | 9670 | && tree_int_cst_sgn (op1) > 0 |
3e4093b6 RS |
9671 | /* We can shorten only if the shift count is less than the |
9672 | number of bits in the smaller type size. */ | |
9673 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
9674 | /* We cannot drop an unsigned shift after sign-extension. */ | |
8df83eae | 9675 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) |
3e4093b6 RS |
9676 | { |
9677 | /* Do an unsigned shift if the operand was zero-extended. */ | |
9678 | result_type | |
9679 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
9680 | TREE_TYPE (arg0)); | |
9681 | /* Convert value-to-be-shifted to that type. */ | |
9682 | if (TREE_TYPE (op0) != result_type) | |
9683 | op0 = convert (result_type, op0); | |
9684 | converted = 1; | |
abe80e6d | 9685 | } |
88a3dbc1 RK |
9686 | } |
9687 | ||
3e4093b6 RS |
9688 | /* Comparison operations are shortened too but differently. |
9689 | They identify themselves by setting short_compare = 1. */ | |
56cb9733 | 9690 | |
3e4093b6 RS |
9691 | if (short_compare) |
9692 | { | |
9693 | /* Don't write &op0, etc., because that would prevent op0 | |
9694 | from being kept in a register. | |
9695 | Instead, make copies of the our local variables and | |
9696 | pass the copies by reference, then copy them back afterward. */ | |
9697 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
9698 | enum tree_code xresultcode = resultcode; | |
9699 | tree val | |
9700 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
8f17b5c5 | 9701 | |
3e4093b6 | 9702 | if (val != 0) |
c9f9eb5d AH |
9703 | { |
9704 | ret = val; | |
9705 | goto return_build_binary_op; | |
9706 | } | |
8f17b5c5 | 9707 | |
3e4093b6 RS |
9708 | op0 = xop0, op1 = xop1; |
9709 | converted = 1; | |
9710 | resultcode = xresultcode; | |
8f17b5c5 | 9711 | |
7d882b83 | 9712 | if (c_inhibit_evaluation_warnings == 0) |
928c19bb JM |
9713 | { |
9714 | bool op0_maybe_const = true; | |
9715 | bool op1_maybe_const = true; | |
9716 | tree orig_op0_folded, orig_op1_folded; | |
9717 | ||
9718 | if (in_late_binary_op) | |
9719 | { | |
9720 | orig_op0_folded = orig_op0; | |
9721 | orig_op1_folded = orig_op1; | |
9722 | } | |
9723 | else | |
9724 | { | |
9725 | /* Fold for the sake of possible warnings, as in | |
9726 | build_conditional_expr. This requires the | |
9727 | "original" values to be folded, not just op0 and | |
9728 | op1. */ | |
f5178456 | 9729 | c_inhibit_evaluation_warnings++; |
928c19bb JM |
9730 | op0 = c_fully_fold (op0, require_constant_value, |
9731 | &op0_maybe_const); | |
9732 | op1 = c_fully_fold (op1, require_constant_value, | |
9733 | &op1_maybe_const); | |
f5178456 | 9734 | c_inhibit_evaluation_warnings--; |
928c19bb JM |
9735 | orig_op0_folded = c_fully_fold (orig_op0, |
9736 | require_constant_value, | |
9737 | NULL); | |
9738 | orig_op1_folded = c_fully_fold (orig_op1, | |
9739 | require_constant_value, | |
9740 | NULL); | |
9741 | } | |
9742 | ||
9743 | if (warn_sign_compare) | |
9744 | warn_for_sign_compare (location, orig_op0_folded, | |
9745 | orig_op1_folded, op0, op1, | |
9746 | result_type, resultcode); | |
9747 | if (!in_late_binary_op) | |
9748 | { | |
9749 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
e5a94231 | 9750 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); |
928c19bb | 9751 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) |
e5a94231 | 9752 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); |
928c19bb | 9753 | } |
3e4093b6 | 9754 | } |
2ad1815d | 9755 | } |
64094f6a | 9756 | } |
64094f6a | 9757 | |
3e4093b6 RS |
9758 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. |
9759 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
9760 | Then the expression will be built. | |
9761 | It will be given type FINAL_TYPE if that is nonzero; | |
9762 | otherwise, it will be given type RESULT_TYPE. */ | |
400fbf9f | 9763 | |
3e4093b6 RS |
9764 | if (!result_type) |
9765 | { | |
ba47d38d | 9766 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); |
3e4093b6 RS |
9767 | return error_mark_node; |
9768 | } | |
400fbf9f | 9769 | |
3f75a254 | 9770 | if (!converted) |
3e4093b6 RS |
9771 | { |
9772 | if (TREE_TYPE (op0) != result_type) | |
0f57299d | 9773 | op0 = convert_and_check (result_type, op0); |
3e4093b6 | 9774 | if (TREE_TYPE (op1) != result_type) |
0f57299d | 9775 | op1 = convert_and_check (result_type, op1); |
d97c6333 JW |
9776 | |
9777 | /* This can happen if one operand has a vector type, and the other | |
9778 | has a different type. */ | |
9779 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
9780 | return error_mark_node; | |
3e4093b6 | 9781 | } |
400fbf9f | 9782 | |
3e4093b6 | 9783 | if (build_type == NULL_TREE) |
8ce94e44 JM |
9784 | { |
9785 | build_type = result_type; | |
9786 | if (type0 != orig_type0 || type1 != orig_type1) | |
9787 | { | |
9788 | gcc_assert (may_need_excess_precision && common); | |
9789 | real_result_type = c_common_type (orig_type0, orig_type1); | |
9790 | } | |
9791 | } | |
400fbf9f | 9792 | |
c9f9eb5d | 9793 | /* Treat expressions in initializers specially as they can't trap. */ |
928c19bb JM |
9794 | if (int_const_or_overflow) |
9795 | ret = (require_constant_value | |
db3927fb AH |
9796 | ? fold_build2_initializer_loc (location, resultcode, build_type, |
9797 | op0, op1) | |
9798 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
928c19bb JM |
9799 | else |
9800 | ret = build2 (resultcode, build_type, op0, op1); | |
c9f9eb5d AH |
9801 | if (final_type != 0) |
9802 | ret = convert (final_type, ret); | |
9803 | ||
9804 | return_build_binary_op: | |
9805 | gcc_assert (ret != error_mark_node); | |
928c19bb JM |
9806 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) |
9807 | ret = (int_operands | |
9808 | ? note_integer_operands (ret) | |
9809 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
9810 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
9811 | && !in_late_binary_op) | |
9812 | ret = note_integer_operands (ret); | |
8ce94e44 JM |
9813 | if (real_result_type) |
9814 | ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret); | |
c9f9eb5d AH |
9815 | protected_set_expr_location (ret, location); |
9816 | return ret; | |
400fbf9f | 9817 | } |
85498824 JM |
9818 | |
9819 | ||
9820 | /* Convert EXPR to be a truth-value, validating its type for this | |
ba47d38d | 9821 | purpose. LOCATION is the source location for the expression. */ |
85498824 JM |
9822 | |
9823 | tree | |
ba47d38d | 9824 | c_objc_common_truthvalue_conversion (location_t location, tree expr) |
85498824 | 9825 | { |
928c19bb JM |
9826 | bool int_const, int_operands; |
9827 | ||
85498824 JM |
9828 | switch (TREE_CODE (TREE_TYPE (expr))) |
9829 | { | |
9830 | case ARRAY_TYPE: | |
ba47d38d | 9831 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); |
85498824 JM |
9832 | return error_mark_node; |
9833 | ||
9834 | case RECORD_TYPE: | |
ba47d38d | 9835 | error_at (location, "used struct type value where scalar is required"); |
85498824 JM |
9836 | return error_mark_node; |
9837 | ||
9838 | case UNION_TYPE: | |
ba47d38d | 9839 | error_at (location, "used union type value where scalar is required"); |
85498824 JM |
9840 | return error_mark_node; |
9841 | ||
46bdb9cf JM |
9842 | case FUNCTION_TYPE: |
9843 | gcc_unreachable (); | |
9844 | ||
85498824 JM |
9845 | default: |
9846 | break; | |
9847 | } | |
9848 | ||
928c19bb JM |
9849 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); |
9850 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
4d84fe7c JM |
9851 | if (int_operands) |
9852 | expr = remove_c_maybe_const_expr (expr); | |
928c19bb | 9853 | |
85498824 JM |
9854 | /* ??? Should we also give an error for void and vectors rather than |
9855 | leaving those to give errors later? */ | |
928c19bb JM |
9856 | expr = c_common_truthvalue_conversion (location, expr); |
9857 | ||
9858 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
9859 | { | |
9860 | if (TREE_OVERFLOW (expr)) | |
9861 | return expr; | |
9862 | else | |
9863 | return note_integer_operands (expr); | |
9864 | } | |
9865 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
9866 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
9867 | return expr; | |
85498824 | 9868 | } |
73f397d4 JM |
9869 | \f |
9870 | ||
9871 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
9872 | required. */ | |
9873 | ||
9874 | tree | |
51eed280 | 9875 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) |
73f397d4 JM |
9876 | { |
9877 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
9878 | { | |
9879 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
9880 | /* Executing a compound literal inside a function reinitializes | |
9881 | it. */ | |
9882 | if (!TREE_STATIC (decl)) | |
9883 | *se = true; | |
9884 | return decl; | |
9885 | } | |
9886 | else | |
9887 | return expr; | |
9888 | } | |
953ff289 | 9889 | \f |
c0220ea4 | 9890 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
953ff289 DN |
9891 | |
9892 | tree | |
9893 | c_begin_omp_parallel (void) | |
9894 | { | |
9895 | tree block; | |
9896 | ||
9897 | keep_next_level (); | |
9898 | block = c_begin_compound_stmt (true); | |
9899 | ||
9900 | return block; | |
9901 | } | |
9902 | ||
c2255bc4 AH |
9903 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound |
9904 | statement. LOC is the location of the OMP_PARALLEL. */ | |
a68ab351 | 9905 | |
953ff289 | 9906 | tree |
c2255bc4 | 9907 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) |
953ff289 DN |
9908 | { |
9909 | tree stmt; | |
9910 | ||
c2255bc4 | 9911 | block = c_end_compound_stmt (loc, block, true); |
953ff289 DN |
9912 | |
9913 | stmt = make_node (OMP_PARALLEL); | |
9914 | TREE_TYPE (stmt) = void_type_node; | |
9915 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
9916 | OMP_PARALLEL_BODY (stmt) = block; | |
c2255bc4 | 9917 | SET_EXPR_LOCATION (stmt, loc); |
953ff289 DN |
9918 | |
9919 | return add_stmt (stmt); | |
9920 | } | |
9921 | ||
a68ab351 JJ |
9922 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ |
9923 | ||
9924 | tree | |
9925 | c_begin_omp_task (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_TASK, with CLAUSES and BLOCK as its compound |
9936 | statement. LOC is the location of the #pragma. */ | |
a68ab351 JJ |
9937 | |
9938 | tree | |
c2255bc4 | 9939 | c_finish_omp_task (location_t loc, tree clauses, tree block) |
a68ab351 JJ |
9940 | { |
9941 | tree stmt; | |
9942 | ||
c2255bc4 | 9943 | block = c_end_compound_stmt (loc, block, true); |
a68ab351 JJ |
9944 | |
9945 | stmt = make_node (OMP_TASK); | |
9946 | TREE_TYPE (stmt) = void_type_node; | |
9947 | OMP_TASK_CLAUSES (stmt) = clauses; | |
9948 | OMP_TASK_BODY (stmt) = block; | |
c2255bc4 | 9949 | SET_EXPR_LOCATION (stmt, loc); |
a68ab351 JJ |
9950 | |
9951 | return add_stmt (stmt); | |
9952 | } | |
9953 | ||
953ff289 DN |
9954 | /* For all elements of CLAUSES, validate them vs OpenMP constraints. |
9955 | Remove any elements from the list that are invalid. */ | |
9956 | ||
9957 | tree | |
9958 | c_finish_omp_clauses (tree clauses) | |
9959 | { | |
9960 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
9961 | tree c, t, *pc = &clauses; | |
9962 | const char *name; | |
9963 | ||
9964 | bitmap_obstack_initialize (NULL); | |
9965 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
9966 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
9967 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
9968 | ||
9969 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
9970 | { | |
9971 | bool remove = false; | |
9972 | bool need_complete = false; | |
9973 | bool need_implicitly_determined = false; | |
9974 | ||
aaf46ef9 | 9975 | switch (OMP_CLAUSE_CODE (c)) |
953ff289 DN |
9976 | { |
9977 | case OMP_CLAUSE_SHARED: | |
9978 | name = "shared"; | |
9979 | need_implicitly_determined = true; | |
9980 | goto check_dup_generic; | |
9981 | ||
9982 | case OMP_CLAUSE_PRIVATE: | |
9983 | name = "private"; | |
9984 | need_complete = true; | |
9985 | need_implicitly_determined = true; | |
9986 | goto check_dup_generic; | |
9987 | ||
9988 | case OMP_CLAUSE_REDUCTION: | |
9989 | name = "reduction"; | |
9990 | need_implicitly_determined = true; | |
9991 | t = OMP_CLAUSE_DECL (c); | |
9992 | if (AGGREGATE_TYPE_P (TREE_TYPE (t)) | |
9993 | || POINTER_TYPE_P (TREE_TYPE (t))) | |
9994 | { | |
c2255bc4 AH |
9995 | error_at (OMP_CLAUSE_LOCATION (c), |
9996 | "%qE has invalid type for %<reduction%>", t); | |
953ff289 DN |
9997 | remove = true; |
9998 | } | |
9999 | else if (FLOAT_TYPE_P (TREE_TYPE (t))) | |
10000 | { | |
10001 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
10002 | const char *r_name = NULL; | |
10003 | ||
10004 | switch (r_code) | |
10005 | { | |
10006 | case PLUS_EXPR: | |
10007 | case MULT_EXPR: | |
10008 | case MINUS_EXPR: | |
10009 | break; | |
10010 | case BIT_AND_EXPR: | |
10011 | r_name = "&"; | |
10012 | break; | |
10013 | case BIT_XOR_EXPR: | |
10014 | r_name = "^"; | |
10015 | break; | |
10016 | case BIT_IOR_EXPR: | |
10017 | r_name = "|"; | |
10018 | break; | |
10019 | case TRUTH_ANDIF_EXPR: | |
10020 | r_name = "&&"; | |
10021 | break; | |
10022 | case TRUTH_ORIF_EXPR: | |
10023 | r_name = "||"; | |
10024 | break; | |
10025 | default: | |
10026 | gcc_unreachable (); | |
10027 | } | |
10028 | if (r_name) | |
10029 | { | |
c2255bc4 AH |
10030 | error_at (OMP_CLAUSE_LOCATION (c), |
10031 | "%qE has invalid type for %<reduction(%s)%>", | |
10032 | t, r_name); | |
953ff289 DN |
10033 | remove = true; |
10034 | } | |
10035 | } | |
10036 | goto check_dup_generic; | |
10037 | ||
10038 | case OMP_CLAUSE_COPYPRIVATE: | |
10039 | name = "copyprivate"; | |
10040 | goto check_dup_generic; | |
10041 | ||
10042 | case OMP_CLAUSE_COPYIN: | |
10043 | name = "copyin"; | |
10044 | t = OMP_CLAUSE_DECL (c); | |
10045 | if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) | |
10046 | { | |
c2255bc4 AH |
10047 | error_at (OMP_CLAUSE_LOCATION (c), |
10048 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
953ff289 DN |
10049 | remove = true; |
10050 | } | |
10051 | goto check_dup_generic; | |
10052 | ||
10053 | check_dup_generic: | |
10054 | t = OMP_CLAUSE_DECL (c); | |
10055 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10056 | { | |
c2255bc4 AH |
10057 | error_at (OMP_CLAUSE_LOCATION (c), |
10058 | "%qE is not a variable in clause %qs", t, name); | |
953ff289 DN |
10059 | remove = true; |
10060 | } | |
10061 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10062 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
10063 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10064 | { | |
c2255bc4 AH |
10065 | error_at (OMP_CLAUSE_LOCATION (c), |
10066 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10067 | remove = true; |
10068 | } | |
10069 | else | |
10070 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
10071 | break; | |
10072 | ||
10073 | case OMP_CLAUSE_FIRSTPRIVATE: | |
10074 | name = "firstprivate"; | |
10075 | t = OMP_CLAUSE_DECL (c); | |
10076 | need_complete = true; | |
10077 | need_implicitly_determined = true; | |
10078 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10079 | { | |
c2255bc4 AH |
10080 | error_at (OMP_CLAUSE_LOCATION (c), |
10081 | "%qE is not a variable in clause %<firstprivate%>", t); | |
953ff289 DN |
10082 | remove = true; |
10083 | } | |
10084 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10085 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
10086 | { | |
c2255bc4 AH |
10087 | error_at (OMP_CLAUSE_LOCATION (c), |
10088 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10089 | remove = true; |
10090 | } | |
10091 | else | |
10092 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
10093 | break; | |
10094 | ||
10095 | case OMP_CLAUSE_LASTPRIVATE: | |
10096 | name = "lastprivate"; | |
10097 | t = OMP_CLAUSE_DECL (c); | |
10098 | need_complete = true; | |
10099 | need_implicitly_determined = true; | |
10100 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10101 | { | |
c2255bc4 AH |
10102 | error_at (OMP_CLAUSE_LOCATION (c), |
10103 | "%qE is not a variable in clause %<lastprivate%>", t); | |
953ff289 DN |
10104 | remove = true; |
10105 | } | |
10106 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10107 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10108 | { | |
c2255bc4 AH |
10109 | error_at (OMP_CLAUSE_LOCATION (c), |
10110 | "%qE appears more than once in data clauses", t); | |
953ff289 DN |
10111 | remove = true; |
10112 | } | |
10113 | else | |
10114 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
10115 | break; | |
10116 | ||
10117 | case OMP_CLAUSE_IF: | |
10118 | case OMP_CLAUSE_NUM_THREADS: | |
10119 | case OMP_CLAUSE_SCHEDULE: | |
10120 | case OMP_CLAUSE_NOWAIT: | |
10121 | case OMP_CLAUSE_ORDERED: | |
10122 | case OMP_CLAUSE_DEFAULT: | |
a68ab351 JJ |
10123 | case OMP_CLAUSE_UNTIED: |
10124 | case OMP_CLAUSE_COLLAPSE: | |
953ff289 DN |
10125 | pc = &OMP_CLAUSE_CHAIN (c); |
10126 | continue; | |
10127 | ||
10128 | default: | |
10129 | gcc_unreachable (); | |
10130 | } | |
10131 | ||
10132 | if (!remove) | |
10133 | { | |
10134 | t = OMP_CLAUSE_DECL (c); | |
10135 | ||
10136 | if (need_complete) | |
10137 | { | |
10138 | t = require_complete_type (t); | |
10139 | if (t == error_mark_node) | |
10140 | remove = true; | |
10141 | } | |
10142 | ||
10143 | if (need_implicitly_determined) | |
10144 | { | |
10145 | const char *share_name = NULL; | |
10146 | ||
10147 | if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
10148 | share_name = "threadprivate"; | |
10149 | else switch (c_omp_predetermined_sharing (t)) | |
10150 | { | |
10151 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
10152 | break; | |
10153 | case OMP_CLAUSE_DEFAULT_SHARED: | |
10154 | share_name = "shared"; | |
10155 | break; | |
10156 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
10157 | share_name = "private"; | |
10158 | break; | |
10159 | default: | |
10160 | gcc_unreachable (); | |
10161 | } | |
10162 | if (share_name) | |
10163 | { | |
c2255bc4 AH |
10164 | error_at (OMP_CLAUSE_LOCATION (c), |
10165 | "%qE is predetermined %qs for %qs", | |
10166 | t, share_name, name); | |
953ff289 DN |
10167 | remove = true; |
10168 | } | |
10169 | } | |
10170 | } | |
10171 | ||
10172 | if (remove) | |
10173 | *pc = OMP_CLAUSE_CHAIN (c); | |
10174 | else | |
10175 | pc = &OMP_CLAUSE_CHAIN (c); | |
10176 | } | |
10177 | ||
10178 | bitmap_obstack_release (NULL); | |
10179 | return clauses; | |
10180 | } | |
9ae165a0 DG |
10181 | |
10182 | /* Make a variant type in the proper way for C/C++, propagating qualifiers | |
10183 | down to the element type of an array. */ | |
10184 | ||
10185 | tree | |
10186 | c_build_qualified_type (tree type, int type_quals) | |
10187 | { | |
10188 | if (type == error_mark_node) | |
10189 | return type; | |
10190 | ||
10191 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10192 | { | |
10193 | tree t; | |
10194 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
10195 | type_quals); | |
10196 | ||
10197 | /* See if we already have an identically qualified type. */ | |
10198 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
10199 | { | |
10200 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
10201 | && TYPE_NAME (t) == TYPE_NAME (type) | |
10202 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
10203 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
10204 | TYPE_ATTRIBUTES (type))) | |
10205 | break; | |
10206 | } | |
10207 | if (!t) | |
10208 | { | |
10209 | tree domain = TYPE_DOMAIN (type); | |
10210 | ||
10211 | t = build_variant_type_copy (type); | |
10212 | TREE_TYPE (t) = element_type; | |
10213 | ||
10214 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
10215 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
10216 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
10217 | else if (TYPE_CANONICAL (element_type) != element_type | |
10218 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
10219 | { | |
10220 | tree unqualified_canon | |
10221 | = build_array_type (TYPE_CANONICAL (element_type), | |
10222 | domain? TYPE_CANONICAL (domain) | |
10223 | : NULL_TREE); | |
10224 | TYPE_CANONICAL (t) | |
10225 | = c_build_qualified_type (unqualified_canon, type_quals); | |
10226 | } | |
10227 | else | |
10228 | TYPE_CANONICAL (t) = t; | |
10229 | } | |
10230 | return t; | |
10231 | } | |
10232 | ||
10233 | /* A restrict-qualified pointer type must be a pointer to object or | |
10234 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
10235 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
10236 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
10237 | && (!POINTER_TYPE_P (type) | |
10238 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
10239 | { | |
10240 | error ("invalid use of %<restrict%>"); | |
10241 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
10242 | } | |
10243 | ||
10244 | return build_qualified_type (type, type_quals); | |
10245 | } | |
72b5577d ILT |
10246 | |
10247 | /* Build a VA_ARG_EXPR for the C parser. */ | |
10248 | ||
10249 | tree | |
c2255bc4 | 10250 | c_build_va_arg (location_t loc, tree expr, tree type) |
72b5577d ILT |
10251 | { |
10252 | if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) | |
10253 | warning_at (loc, OPT_Wc___compat, | |
10254 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
c2255bc4 | 10255 | return build_va_arg (loc, expr, type); |
72b5577d | 10256 | } |