]>
Commit | Line | Data |
---|---|---|
1 | /* Build expressions with type checking for C compiler. | |
2 | Copyright (C) 1987-2013 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | ||
21 | /* This file is part of the C front end. | |
22 | It contains routines to build C expressions given their operands, | |
23 | including computing the types of the result, C-specific error checks, | |
24 | and some optimization. */ | |
25 | ||
26 | #include "config.h" | |
27 | #include "system.h" | |
28 | #include "coretypes.h" | |
29 | #include "tm.h" | |
30 | #include "tree.h" | |
31 | #include "langhooks.h" | |
32 | #include "c-tree.h" | |
33 | #include "c-lang.h" | |
34 | #include "flags.h" | |
35 | #include "intl.h" | |
36 | #include "target.h" | |
37 | #include "tree-iterator.h" | |
38 | #include "bitmap.h" | |
39 | #include "gimple.h" | |
40 | #include "c-family/c-objc.h" | |
41 | #include "c-family/c-common.h" | |
42 | #include "c-family/c-ubsan.h" | |
43 | ||
44 | /* Possible cases of implicit bad conversions. Used to select | |
45 | diagnostic messages in convert_for_assignment. */ | |
46 | enum impl_conv { | |
47 | ic_argpass, | |
48 | ic_assign, | |
49 | ic_init, | |
50 | ic_return | |
51 | }; | |
52 | ||
53 | /* The level of nesting inside "__alignof__". */ | |
54 | int in_alignof; | |
55 | ||
56 | /* The level of nesting inside "sizeof". */ | |
57 | int in_sizeof; | |
58 | ||
59 | /* The level of nesting inside "typeof". */ | |
60 | int in_typeof; | |
61 | ||
62 | /* The argument of last parsed sizeof expression, only to be tested | |
63 | if expr.original_code == SIZEOF_EXPR. */ | |
64 | tree c_last_sizeof_arg; | |
65 | ||
66 | /* Nonzero if we've already printed a "missing braces around initializer" | |
67 | message within this initializer. */ | |
68 | static int missing_braces_mentioned; | |
69 | ||
70 | static int require_constant_value; | |
71 | static int require_constant_elements; | |
72 | ||
73 | static bool null_pointer_constant_p (const_tree); | |
74 | static tree qualify_type (tree, tree); | |
75 | static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *, | |
76 | bool *); | |
77 | static int comp_target_types (location_t, tree, tree); | |
78 | static int function_types_compatible_p (const_tree, const_tree, bool *, | |
79 | bool *); | |
80 | static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *); | |
81 | static tree lookup_field (tree, tree); | |
82 | static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *, | |
83 | tree, tree); | |
84 | static tree pointer_diff (location_t, tree, tree); | |
85 | static tree convert_for_assignment (location_t, tree, tree, tree, | |
86 | enum impl_conv, bool, tree, tree, int); | |
87 | static tree valid_compound_expr_initializer (tree, tree); | |
88 | static void push_string (const char *); | |
89 | static void push_member_name (tree); | |
90 | static int spelling_length (void); | |
91 | static char *print_spelling (char *); | |
92 | static void warning_init (int, const char *); | |
93 | static tree digest_init (location_t, tree, tree, tree, bool, bool, int); | |
94 | static void output_init_element (tree, tree, bool, tree, tree, int, bool, | |
95 | struct obstack *); | |
96 | static void output_pending_init_elements (int, struct obstack *); | |
97 | static int set_designator (int, struct obstack *); | |
98 | static void push_range_stack (tree, struct obstack *); | |
99 | static void add_pending_init (tree, tree, tree, bool, struct obstack *); | |
100 | static void set_nonincremental_init (struct obstack *); | |
101 | static void set_nonincremental_init_from_string (tree, struct obstack *); | |
102 | static tree find_init_member (tree, struct obstack *); | |
103 | static void readonly_warning (tree, enum lvalue_use); | |
104 | static int lvalue_or_else (location_t, const_tree, enum lvalue_use); | |
105 | static void record_maybe_used_decl (tree); | |
106 | static int comptypes_internal (const_tree, const_tree, bool *, bool *); | |
107 | \f | |
108 | /* Return true if EXP is a null pointer constant, false otherwise. */ | |
109 | ||
110 | static bool | |
111 | null_pointer_constant_p (const_tree expr) | |
112 | { | |
113 | /* This should really operate on c_expr structures, but they aren't | |
114 | yet available everywhere required. */ | |
115 | tree type = TREE_TYPE (expr); | |
116 | return (TREE_CODE (expr) == INTEGER_CST | |
117 | && !TREE_OVERFLOW (expr) | |
118 | && integer_zerop (expr) | |
119 | && (INTEGRAL_TYPE_P (type) | |
120 | || (TREE_CODE (type) == POINTER_TYPE | |
121 | && VOID_TYPE_P (TREE_TYPE (type)) | |
122 | && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))); | |
123 | } | |
124 | ||
125 | /* EXPR may appear in an unevaluated part of an integer constant | |
126 | expression, but not in an evaluated part. Wrap it in a | |
127 | C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an | |
128 | INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */ | |
129 | ||
130 | static tree | |
131 | note_integer_operands (tree expr) | |
132 | { | |
133 | tree ret; | |
134 | if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op) | |
135 | { | |
136 | ret = copy_node (expr); | |
137 | TREE_OVERFLOW (ret) = 1; | |
138 | } | |
139 | else | |
140 | { | |
141 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr); | |
142 | C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1; | |
143 | } | |
144 | return ret; | |
145 | } | |
146 | ||
147 | /* Having checked whether EXPR may appear in an unevaluated part of an | |
148 | integer constant expression and found that it may, remove any | |
149 | C_MAYBE_CONST_EXPR noting this fact and return the resulting | |
150 | expression. */ | |
151 | ||
152 | static inline tree | |
153 | remove_c_maybe_const_expr (tree expr) | |
154 | { | |
155 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
156 | return C_MAYBE_CONST_EXPR_EXPR (expr); | |
157 | else | |
158 | return expr; | |
159 | } | |
160 | ||
161 | \f/* This is a cache to hold if two types are compatible or not. */ | |
162 | ||
163 | struct tagged_tu_seen_cache { | |
164 | const struct tagged_tu_seen_cache * next; | |
165 | const_tree t1; | |
166 | const_tree t2; | |
167 | /* The return value of tagged_types_tu_compatible_p if we had seen | |
168 | these two types already. */ | |
169 | int val; | |
170 | }; | |
171 | ||
172 | static const struct tagged_tu_seen_cache * tagged_tu_seen_base; | |
173 | static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *); | |
174 | ||
175 | /* Do `exp = require_complete_type (exp);' to make sure exp | |
176 | does not have an incomplete type. (That includes void types.) */ | |
177 | ||
178 | tree | |
179 | require_complete_type (tree value) | |
180 | { | |
181 | tree type = TREE_TYPE (value); | |
182 | ||
183 | if (value == error_mark_node || type == error_mark_node) | |
184 | return error_mark_node; | |
185 | ||
186 | /* First, detect a valid value with a complete type. */ | |
187 | if (COMPLETE_TYPE_P (type)) | |
188 | return value; | |
189 | ||
190 | c_incomplete_type_error (value, type); | |
191 | return error_mark_node; | |
192 | } | |
193 | ||
194 | /* Print an error message for invalid use of an incomplete type. | |
195 | VALUE is the expression that was used (or 0 if that isn't known) | |
196 | and TYPE is the type that was invalid. */ | |
197 | ||
198 | void | |
199 | c_incomplete_type_error (const_tree value, const_tree type) | |
200 | { | |
201 | const char *type_code_string; | |
202 | ||
203 | /* Avoid duplicate error message. */ | |
204 | if (TREE_CODE (type) == ERROR_MARK) | |
205 | return; | |
206 | ||
207 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
208 | || TREE_CODE (value) == PARM_DECL)) | |
209 | error ("%qD has an incomplete type", value); | |
210 | else | |
211 | { | |
212 | retry: | |
213 | /* We must print an error message. Be clever about what it says. */ | |
214 | ||
215 | switch (TREE_CODE (type)) | |
216 | { | |
217 | case RECORD_TYPE: | |
218 | type_code_string = "struct"; | |
219 | break; | |
220 | ||
221 | case UNION_TYPE: | |
222 | type_code_string = "union"; | |
223 | break; | |
224 | ||
225 | case ENUMERAL_TYPE: | |
226 | type_code_string = "enum"; | |
227 | break; | |
228 | ||
229 | case VOID_TYPE: | |
230 | error ("invalid use of void expression"); | |
231 | return; | |
232 | ||
233 | case ARRAY_TYPE: | |
234 | if (TYPE_DOMAIN (type)) | |
235 | { | |
236 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL) | |
237 | { | |
238 | error ("invalid use of flexible array member"); | |
239 | return; | |
240 | } | |
241 | type = TREE_TYPE (type); | |
242 | goto retry; | |
243 | } | |
244 | error ("invalid use of array with unspecified bounds"); | |
245 | return; | |
246 | ||
247 | default: | |
248 | gcc_unreachable (); | |
249 | } | |
250 | ||
251 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
252 | error ("invalid use of undefined type %<%s %E%>", | |
253 | type_code_string, TYPE_NAME (type)); | |
254 | else | |
255 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ | |
256 | error ("invalid use of incomplete typedef %qD", TYPE_NAME (type)); | |
257 | } | |
258 | } | |
259 | ||
260 | /* Given a type, apply default promotions wrt unnamed function | |
261 | arguments and return the new type. */ | |
262 | ||
263 | tree | |
264 | c_type_promotes_to (tree type) | |
265 | { | |
266 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
267 | return double_type_node; | |
268 | ||
269 | if (c_promoting_integer_type_p (type)) | |
270 | { | |
271 | /* Preserve unsignedness if not really getting any wider. */ | |
272 | if (TYPE_UNSIGNED (type) | |
273 | && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) | |
274 | return unsigned_type_node; | |
275 | return integer_type_node; | |
276 | } | |
277 | ||
278 | return type; | |
279 | } | |
280 | ||
281 | /* Return true if between two named address spaces, whether there is a superset | |
282 | named address space that encompasses both address spaces. If there is a | |
283 | superset, return which address space is the superset. */ | |
284 | ||
285 | static bool | |
286 | addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common) | |
287 | { | |
288 | if (as1 == as2) | |
289 | { | |
290 | *common = as1; | |
291 | return true; | |
292 | } | |
293 | else if (targetm.addr_space.subset_p (as1, as2)) | |
294 | { | |
295 | *common = as2; | |
296 | return true; | |
297 | } | |
298 | else if (targetm.addr_space.subset_p (as2, as1)) | |
299 | { | |
300 | *common = as1; | |
301 | return true; | |
302 | } | |
303 | else | |
304 | return false; | |
305 | } | |
306 | ||
307 | /* Return a variant of TYPE which has all the type qualifiers of LIKE | |
308 | as well as those of TYPE. */ | |
309 | ||
310 | static tree | |
311 | qualify_type (tree type, tree like) | |
312 | { | |
313 | addr_space_t as_type = TYPE_ADDR_SPACE (type); | |
314 | addr_space_t as_like = TYPE_ADDR_SPACE (like); | |
315 | addr_space_t as_common; | |
316 | ||
317 | /* If the two named address spaces are different, determine the common | |
318 | superset address space. If there isn't one, raise an error. */ | |
319 | if (!addr_space_superset (as_type, as_like, &as_common)) | |
320 | { | |
321 | as_common = as_type; | |
322 | error ("%qT and %qT are in disjoint named address spaces", | |
323 | type, like); | |
324 | } | |
325 | ||
326 | return c_build_qualified_type (type, | |
327 | TYPE_QUALS_NO_ADDR_SPACE (type) | |
328 | | TYPE_QUALS_NO_ADDR_SPACE (like) | |
329 | | ENCODE_QUAL_ADDR_SPACE (as_common)); | |
330 | } | |
331 | ||
332 | /* Return true iff the given tree T is a variable length array. */ | |
333 | ||
334 | bool | |
335 | c_vla_type_p (const_tree t) | |
336 | { | |
337 | if (TREE_CODE (t) == ARRAY_TYPE | |
338 | && C_TYPE_VARIABLE_SIZE (t)) | |
339 | return true; | |
340 | return false; | |
341 | } | |
342 | \f | |
343 | /* Return the composite type of two compatible types. | |
344 | ||
345 | We assume that comptypes has already been done and returned | |
346 | nonzero; if that isn't so, this may crash. In particular, we | |
347 | assume that qualifiers match. */ | |
348 | ||
349 | tree | |
350 | composite_type (tree t1, tree t2) | |
351 | { | |
352 | enum tree_code code1; | |
353 | enum tree_code code2; | |
354 | tree attributes; | |
355 | ||
356 | /* Save time if the two types are the same. */ | |
357 | ||
358 | if (t1 == t2) return t1; | |
359 | ||
360 | /* If one type is nonsense, use the other. */ | |
361 | if (t1 == error_mark_node) | |
362 | return t2; | |
363 | if (t2 == error_mark_node) | |
364 | return t1; | |
365 | ||
366 | code1 = TREE_CODE (t1); | |
367 | code2 = TREE_CODE (t2); | |
368 | ||
369 | /* Merge the attributes. */ | |
370 | attributes = targetm.merge_type_attributes (t1, t2); | |
371 | ||
372 | /* If one is an enumerated type and the other is the compatible | |
373 | integer type, the composite type might be either of the two | |
374 | (DR#013 question 3). For consistency, use the enumerated type as | |
375 | the composite type. */ | |
376 | ||
377 | if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE) | |
378 | return t1; | |
379 | if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE) | |
380 | return t2; | |
381 | ||
382 | gcc_assert (code1 == code2); | |
383 | ||
384 | switch (code1) | |
385 | { | |
386 | case POINTER_TYPE: | |
387 | /* For two pointers, do this recursively on the target type. */ | |
388 | { | |
389 | tree pointed_to_1 = TREE_TYPE (t1); | |
390 | tree pointed_to_2 = TREE_TYPE (t2); | |
391 | tree target = composite_type (pointed_to_1, pointed_to_2); | |
392 | t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false); | |
393 | t1 = build_type_attribute_variant (t1, attributes); | |
394 | return qualify_type (t1, t2); | |
395 | } | |
396 | ||
397 | case ARRAY_TYPE: | |
398 | { | |
399 | tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); | |
400 | int quals; | |
401 | tree unqual_elt; | |
402 | tree d1 = TYPE_DOMAIN (t1); | |
403 | tree d2 = TYPE_DOMAIN (t2); | |
404 | bool d1_variable, d2_variable; | |
405 | bool d1_zero, d2_zero; | |
406 | bool t1_complete, t2_complete; | |
407 | ||
408 | /* We should not have any type quals on arrays at all. */ | |
409 | gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1) | |
410 | && !TYPE_QUALS_NO_ADDR_SPACE (t2)); | |
411 | ||
412 | t1_complete = COMPLETE_TYPE_P (t1); | |
413 | t2_complete = COMPLETE_TYPE_P (t2); | |
414 | ||
415 | d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1); | |
416 | d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2); | |
417 | ||
418 | d1_variable = (!d1_zero | |
419 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
420 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
421 | d2_variable = (!d2_zero | |
422 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
423 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
424 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); | |
425 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
426 | ||
427 | /* Save space: see if the result is identical to one of the args. */ | |
428 | if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1) | |
429 | && (d2_variable || d2_zero || !d1_variable)) | |
430 | return build_type_attribute_variant (t1, attributes); | |
431 | if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2) | |
432 | && (d1_variable || d1_zero || !d2_variable)) | |
433 | return build_type_attribute_variant (t2, attributes); | |
434 | ||
435 | if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
436 | return build_type_attribute_variant (t1, attributes); | |
437 | if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1)) | |
438 | return build_type_attribute_variant (t2, attributes); | |
439 | ||
440 | /* Merge the element types, and have a size if either arg has | |
441 | one. We may have qualifiers on the element types. To set | |
442 | up TYPE_MAIN_VARIANT correctly, we need to form the | |
443 | composite of the unqualified types and add the qualifiers | |
444 | back at the end. */ | |
445 | quals = TYPE_QUALS (strip_array_types (elt)); | |
446 | unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED); | |
447 | t1 = build_array_type (unqual_elt, | |
448 | TYPE_DOMAIN ((TYPE_DOMAIN (t1) | |
449 | && (d2_variable | |
450 | || d2_zero | |
451 | || !d1_variable)) | |
452 | ? t1 | |
453 | : t2)); | |
454 | /* Ensure a composite type involving a zero-length array type | |
455 | is a zero-length type not an incomplete type. */ | |
456 | if (d1_zero && d2_zero | |
457 | && (t1_complete || t2_complete) | |
458 | && !COMPLETE_TYPE_P (t1)) | |
459 | { | |
460 | TYPE_SIZE (t1) = bitsize_zero_node; | |
461 | TYPE_SIZE_UNIT (t1) = size_zero_node; | |
462 | } | |
463 | t1 = c_build_qualified_type (t1, quals); | |
464 | return build_type_attribute_variant (t1, attributes); | |
465 | } | |
466 | ||
467 | case ENUMERAL_TYPE: | |
468 | case RECORD_TYPE: | |
469 | case UNION_TYPE: | |
470 | if (attributes != NULL) | |
471 | { | |
472 | /* Try harder not to create a new aggregate type. */ | |
473 | if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes)) | |
474 | return t1; | |
475 | if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes)) | |
476 | return t2; | |
477 | } | |
478 | return build_type_attribute_variant (t1, attributes); | |
479 | ||
480 | case FUNCTION_TYPE: | |
481 | /* Function types: prefer the one that specified arg types. | |
482 | If both do, merge the arg types. Also merge the return types. */ | |
483 | { | |
484 | tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2)); | |
485 | tree p1 = TYPE_ARG_TYPES (t1); | |
486 | tree p2 = TYPE_ARG_TYPES (t2); | |
487 | int len; | |
488 | tree newargs, n; | |
489 | int i; | |
490 | ||
491 | /* Save space: see if the result is identical to one of the args. */ | |
492 | if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2)) | |
493 | return build_type_attribute_variant (t1, attributes); | |
494 | if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1)) | |
495 | return build_type_attribute_variant (t2, attributes); | |
496 | ||
497 | /* Simple way if one arg fails to specify argument types. */ | |
498 | if (TYPE_ARG_TYPES (t1) == 0) | |
499 | { | |
500 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2)); | |
501 | t1 = build_type_attribute_variant (t1, attributes); | |
502 | return qualify_type (t1, t2); | |
503 | } | |
504 | if (TYPE_ARG_TYPES (t2) == 0) | |
505 | { | |
506 | t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1)); | |
507 | t1 = build_type_attribute_variant (t1, attributes); | |
508 | return qualify_type (t1, t2); | |
509 | } | |
510 | ||
511 | /* If both args specify argument types, we must merge the two | |
512 | lists, argument by argument. */ | |
513 | ||
514 | len = list_length (p1); | |
515 | newargs = 0; | |
516 | ||
517 | for (i = 0; i < len; i++) | |
518 | newargs = tree_cons (NULL_TREE, NULL_TREE, newargs); | |
519 | ||
520 | n = newargs; | |
521 | ||
522 | for (; p1; | |
523 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n)) | |
524 | { | |
525 | /* A null type means arg type is not specified. | |
526 | Take whatever the other function type has. */ | |
527 | if (TREE_VALUE (p1) == 0) | |
528 | { | |
529 | TREE_VALUE (n) = TREE_VALUE (p2); | |
530 | goto parm_done; | |
531 | } | |
532 | if (TREE_VALUE (p2) == 0) | |
533 | { | |
534 | TREE_VALUE (n) = TREE_VALUE (p1); | |
535 | goto parm_done; | |
536 | } | |
537 | ||
538 | /* Given wait (union {union wait *u; int *i} *) | |
539 | and wait (union wait *), | |
540 | prefer union wait * as type of parm. */ | |
541 | if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE | |
542 | && TREE_VALUE (p1) != TREE_VALUE (p2)) | |
543 | { | |
544 | tree memb; | |
545 | tree mv2 = TREE_VALUE (p2); | |
546 | if (mv2 && mv2 != error_mark_node | |
547 | && TREE_CODE (mv2) != ARRAY_TYPE) | |
548 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
549 | for (memb = TYPE_FIELDS (TREE_VALUE (p1)); | |
550 | memb; memb = DECL_CHAIN (memb)) | |
551 | { | |
552 | tree mv3 = TREE_TYPE (memb); | |
553 | if (mv3 && mv3 != error_mark_node | |
554 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
555 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
556 | if (comptypes (mv3, mv2)) | |
557 | { | |
558 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
559 | TREE_VALUE (p2)); | |
560 | pedwarn (input_location, OPT_Wpedantic, | |
561 | "function types not truly compatible in ISO C"); | |
562 | goto parm_done; | |
563 | } | |
564 | } | |
565 | } | |
566 | if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE | |
567 | && TREE_VALUE (p2) != TREE_VALUE (p1)) | |
568 | { | |
569 | tree memb; | |
570 | tree mv1 = TREE_VALUE (p1); | |
571 | if (mv1 && mv1 != error_mark_node | |
572 | && TREE_CODE (mv1) != ARRAY_TYPE) | |
573 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
574 | for (memb = TYPE_FIELDS (TREE_VALUE (p2)); | |
575 | memb; memb = DECL_CHAIN (memb)) | |
576 | { | |
577 | tree mv3 = TREE_TYPE (memb); | |
578 | if (mv3 && mv3 != error_mark_node | |
579 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
580 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
581 | if (comptypes (mv3, mv1)) | |
582 | { | |
583 | TREE_VALUE (n) = composite_type (TREE_TYPE (memb), | |
584 | TREE_VALUE (p1)); | |
585 | pedwarn (input_location, OPT_Wpedantic, | |
586 | "function types not truly compatible in ISO C"); | |
587 | goto parm_done; | |
588 | } | |
589 | } | |
590 | } | |
591 | TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2)); | |
592 | parm_done: ; | |
593 | } | |
594 | ||
595 | t1 = build_function_type (valtype, newargs); | |
596 | t1 = qualify_type (t1, t2); | |
597 | /* ... falls through ... */ | |
598 | } | |
599 | ||
600 | default: | |
601 | return build_type_attribute_variant (t1, attributes); | |
602 | } | |
603 | ||
604 | } | |
605 | ||
606 | /* Return the type of a conditional expression between pointers to | |
607 | possibly differently qualified versions of compatible types. | |
608 | ||
609 | We assume that comp_target_types has already been done and returned | |
610 | nonzero; if that isn't so, this may crash. */ | |
611 | ||
612 | static tree | |
613 | common_pointer_type (tree t1, tree t2) | |
614 | { | |
615 | tree attributes; | |
616 | tree pointed_to_1, mv1; | |
617 | tree pointed_to_2, mv2; | |
618 | tree target; | |
619 | unsigned target_quals; | |
620 | addr_space_t as1, as2, as_common; | |
621 | int quals1, quals2; | |
622 | ||
623 | /* Save time if the two types are the same. */ | |
624 | ||
625 | if (t1 == t2) return t1; | |
626 | ||
627 | /* If one type is nonsense, use the other. */ | |
628 | if (t1 == error_mark_node) | |
629 | return t2; | |
630 | if (t2 == error_mark_node) | |
631 | return t1; | |
632 | ||
633 | gcc_assert (TREE_CODE (t1) == POINTER_TYPE | |
634 | && TREE_CODE (t2) == POINTER_TYPE); | |
635 | ||
636 | /* Merge the attributes. */ | |
637 | attributes = targetm.merge_type_attributes (t1, t2); | |
638 | ||
639 | /* Find the composite type of the target types, and combine the | |
640 | qualifiers of the two types' targets. Do not lose qualifiers on | |
641 | array element types by taking the TYPE_MAIN_VARIANT. */ | |
642 | mv1 = pointed_to_1 = TREE_TYPE (t1); | |
643 | mv2 = pointed_to_2 = TREE_TYPE (t2); | |
644 | if (TREE_CODE (mv1) != ARRAY_TYPE) | |
645 | mv1 = TYPE_MAIN_VARIANT (pointed_to_1); | |
646 | if (TREE_CODE (mv2) != ARRAY_TYPE) | |
647 | mv2 = TYPE_MAIN_VARIANT (pointed_to_2); | |
648 | target = composite_type (mv1, mv2); | |
649 | ||
650 | /* For function types do not merge const qualifiers, but drop them | |
651 | if used inconsistently. The middle-end uses these to mark const | |
652 | and noreturn functions. */ | |
653 | quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1); | |
654 | quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2); | |
655 | ||
656 | if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE) | |
657 | target_quals = (quals1 & quals2); | |
658 | else | |
659 | target_quals = (quals1 | quals2); | |
660 | ||
661 | /* If the two named address spaces are different, determine the common | |
662 | superset address space. This is guaranteed to exist due to the | |
663 | assumption that comp_target_type returned non-zero. */ | |
664 | as1 = TYPE_ADDR_SPACE (pointed_to_1); | |
665 | as2 = TYPE_ADDR_SPACE (pointed_to_2); | |
666 | if (!addr_space_superset (as1, as2, &as_common)) | |
667 | gcc_unreachable (); | |
668 | ||
669 | target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common); | |
670 | ||
671 | t1 = build_pointer_type (c_build_qualified_type (target, target_quals)); | |
672 | return build_type_attribute_variant (t1, attributes); | |
673 | } | |
674 | ||
675 | /* Return the common type for two arithmetic types under the usual | |
676 | arithmetic conversions. The default conversions have already been | |
677 | applied, and enumerated types converted to their compatible integer | |
678 | types. The resulting type is unqualified and has no attributes. | |
679 | ||
680 | This is the type for the result of most arithmetic operations | |
681 | if the operands have the given two types. */ | |
682 | ||
683 | static tree | |
684 | c_common_type (tree t1, tree t2) | |
685 | { | |
686 | enum tree_code code1; | |
687 | enum tree_code code2; | |
688 | ||
689 | /* If one type is nonsense, use the other. */ | |
690 | if (t1 == error_mark_node) | |
691 | return t2; | |
692 | if (t2 == error_mark_node) | |
693 | return t1; | |
694 | ||
695 | if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED) | |
696 | t1 = TYPE_MAIN_VARIANT (t1); | |
697 | ||
698 | if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED) | |
699 | t2 = TYPE_MAIN_VARIANT (t2); | |
700 | ||
701 | if (TYPE_ATTRIBUTES (t1) != NULL_TREE) | |
702 | t1 = build_type_attribute_variant (t1, NULL_TREE); | |
703 | ||
704 | if (TYPE_ATTRIBUTES (t2) != NULL_TREE) | |
705 | t2 = build_type_attribute_variant (t2, NULL_TREE); | |
706 | ||
707 | /* Save time if the two types are the same. */ | |
708 | ||
709 | if (t1 == t2) return t1; | |
710 | ||
711 | code1 = TREE_CODE (t1); | |
712 | code2 = TREE_CODE (t2); | |
713 | ||
714 | gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE | |
715 | || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE | |
716 | || code1 == INTEGER_TYPE); | |
717 | gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE | |
718 | || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE | |
719 | || code2 == INTEGER_TYPE); | |
720 | ||
721 | /* When one operand is a decimal float type, the other operand cannot be | |
722 | a generic float type or a complex type. We also disallow vector types | |
723 | here. */ | |
724 | if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2)) | |
725 | && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2))) | |
726 | { | |
727 | if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE) | |
728 | { | |
729 | error ("can%'t mix operands of decimal float and vector types"); | |
730 | return error_mark_node; | |
731 | } | |
732 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
733 | { | |
734 | error ("can%'t mix operands of decimal float and complex types"); | |
735 | return error_mark_node; | |
736 | } | |
737 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
738 | { | |
739 | error ("can%'t mix operands of decimal float and other float types"); | |
740 | return error_mark_node; | |
741 | } | |
742 | } | |
743 | ||
744 | /* If one type is a vector type, return that type. (How the usual | |
745 | arithmetic conversions apply to the vector types extension is not | |
746 | precisely specified.) */ | |
747 | if (code1 == VECTOR_TYPE) | |
748 | return t1; | |
749 | ||
750 | if (code2 == VECTOR_TYPE) | |
751 | return t2; | |
752 | ||
753 | /* If one type is complex, form the common type of the non-complex | |
754 | components, then make that complex. Use T1 or T2 if it is the | |
755 | required type. */ | |
756 | if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE) | |
757 | { | |
758 | tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1; | |
759 | tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2; | |
760 | tree subtype = c_common_type (subtype1, subtype2); | |
761 | ||
762 | if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype) | |
763 | return t1; | |
764 | else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype) | |
765 | return t2; | |
766 | else | |
767 | return build_complex_type (subtype); | |
768 | } | |
769 | ||
770 | /* If only one is real, use it as the result. */ | |
771 | ||
772 | if (code1 == REAL_TYPE && code2 != REAL_TYPE) | |
773 | return t1; | |
774 | ||
775 | if (code2 == REAL_TYPE && code1 != REAL_TYPE) | |
776 | return t2; | |
777 | ||
778 | /* If both are real and either are decimal floating point types, use | |
779 | the decimal floating point type with the greater precision. */ | |
780 | ||
781 | if (code1 == REAL_TYPE && code2 == REAL_TYPE) | |
782 | { | |
783 | if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node | |
784 | || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node) | |
785 | return dfloat128_type_node; | |
786 | else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node | |
787 | || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node) | |
788 | return dfloat64_type_node; | |
789 | else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node | |
790 | || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node) | |
791 | return dfloat32_type_node; | |
792 | } | |
793 | ||
794 | /* Deal with fixed-point types. */ | |
795 | if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE) | |
796 | { | |
797 | unsigned int unsignedp = 0, satp = 0; | |
798 | enum machine_mode m1, m2; | |
799 | unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit; | |
800 | ||
801 | m1 = TYPE_MODE (t1); | |
802 | m2 = TYPE_MODE (t2); | |
803 | ||
804 | /* If one input type is saturating, the result type is saturating. */ | |
805 | if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2)) | |
806 | satp = 1; | |
807 | ||
808 | /* If both fixed-point types are unsigned, the result type is unsigned. | |
809 | When mixing fixed-point and integer types, follow the sign of the | |
810 | fixed-point type. | |
811 | Otherwise, the result type is signed. */ | |
812 | if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2) | |
813 | && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE) | |
814 | || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE | |
815 | && TYPE_UNSIGNED (t1)) | |
816 | || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE | |
817 | && TYPE_UNSIGNED (t2))) | |
818 | unsignedp = 1; | |
819 | ||
820 | /* The result type is signed. */ | |
821 | if (unsignedp == 0) | |
822 | { | |
823 | /* If the input type is unsigned, we need to convert to the | |
824 | signed type. */ | |
825 | if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1)) | |
826 | { | |
827 | enum mode_class mclass = (enum mode_class) 0; | |
828 | if (GET_MODE_CLASS (m1) == MODE_UFRACT) | |
829 | mclass = MODE_FRACT; | |
830 | else if (GET_MODE_CLASS (m1) == MODE_UACCUM) | |
831 | mclass = MODE_ACCUM; | |
832 | else | |
833 | gcc_unreachable (); | |
834 | m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0); | |
835 | } | |
836 | if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2)) | |
837 | { | |
838 | enum mode_class mclass = (enum mode_class) 0; | |
839 | if (GET_MODE_CLASS (m2) == MODE_UFRACT) | |
840 | mclass = MODE_FRACT; | |
841 | else if (GET_MODE_CLASS (m2) == MODE_UACCUM) | |
842 | mclass = MODE_ACCUM; | |
843 | else | |
844 | gcc_unreachable (); | |
845 | m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0); | |
846 | } | |
847 | } | |
848 | ||
849 | if (code1 == FIXED_POINT_TYPE) | |
850 | { | |
851 | fbit1 = GET_MODE_FBIT (m1); | |
852 | ibit1 = GET_MODE_IBIT (m1); | |
853 | } | |
854 | else | |
855 | { | |
856 | fbit1 = 0; | |
857 | /* Signed integers need to subtract one sign bit. */ | |
858 | ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1)); | |
859 | } | |
860 | ||
861 | if (code2 == FIXED_POINT_TYPE) | |
862 | { | |
863 | fbit2 = GET_MODE_FBIT (m2); | |
864 | ibit2 = GET_MODE_IBIT (m2); | |
865 | } | |
866 | else | |
867 | { | |
868 | fbit2 = 0; | |
869 | /* Signed integers need to subtract one sign bit. */ | |
870 | ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2)); | |
871 | } | |
872 | ||
873 | max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2; | |
874 | max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2; | |
875 | return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp, | |
876 | satp); | |
877 | } | |
878 | ||
879 | /* Both real or both integers; use the one with greater precision. */ | |
880 | ||
881 | if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2)) | |
882 | return t1; | |
883 | else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1)) | |
884 | return t2; | |
885 | ||
886 | /* Same precision. Prefer long longs to longs to ints when the | |
887 | same precision, following the C99 rules on integer type rank | |
888 | (which are equivalent to the C90 rules for C90 types). */ | |
889 | ||
890 | if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node | |
891 | || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node) | |
892 | return long_long_unsigned_type_node; | |
893 | ||
894 | if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node | |
895 | || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node) | |
896 | { | |
897 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
898 | return long_long_unsigned_type_node; | |
899 | else | |
900 | return long_long_integer_type_node; | |
901 | } | |
902 | ||
903 | if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node | |
904 | || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node) | |
905 | return long_unsigned_type_node; | |
906 | ||
907 | if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node | |
908 | || TYPE_MAIN_VARIANT (t2) == long_integer_type_node) | |
909 | { | |
910 | /* But preserve unsignedness from the other type, | |
911 | since long cannot hold all the values of an unsigned int. */ | |
912 | if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2)) | |
913 | return long_unsigned_type_node; | |
914 | else | |
915 | return long_integer_type_node; | |
916 | } | |
917 | ||
918 | /* Likewise, prefer long double to double even if same size. */ | |
919 | if (TYPE_MAIN_VARIANT (t1) == long_double_type_node | |
920 | || TYPE_MAIN_VARIANT (t2) == long_double_type_node) | |
921 | return long_double_type_node; | |
922 | ||
923 | /* Likewise, prefer double to float even if same size. | |
924 | We got a couple of embedded targets with 32 bit doubles, and the | |
925 | pdp11 might have 64 bit floats. */ | |
926 | if (TYPE_MAIN_VARIANT (t1) == double_type_node | |
927 | || TYPE_MAIN_VARIANT (t2) == double_type_node) | |
928 | return double_type_node; | |
929 | ||
930 | /* Otherwise prefer the unsigned one. */ | |
931 | ||
932 | if (TYPE_UNSIGNED (t1)) | |
933 | return t1; | |
934 | else | |
935 | return t2; | |
936 | } | |
937 | \f | |
938 | /* Wrapper around c_common_type that is used by c-common.c and other | |
939 | front end optimizations that remove promotions. ENUMERAL_TYPEs | |
940 | are allowed here and are converted to their compatible integer types. | |
941 | BOOLEAN_TYPEs are allowed here and return either boolean_type_node or | |
942 | preferably a non-Boolean type as the common type. */ | |
943 | tree | |
944 | common_type (tree t1, tree t2) | |
945 | { | |
946 | if (TREE_CODE (t1) == ENUMERAL_TYPE) | |
947 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1); | |
948 | if (TREE_CODE (t2) == ENUMERAL_TYPE) | |
949 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1); | |
950 | ||
951 | /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */ | |
952 | if (TREE_CODE (t1) == BOOLEAN_TYPE | |
953 | && TREE_CODE (t2) == BOOLEAN_TYPE) | |
954 | return boolean_type_node; | |
955 | ||
956 | /* If either type is BOOLEAN_TYPE, then return the other. */ | |
957 | if (TREE_CODE (t1) == BOOLEAN_TYPE) | |
958 | return t2; | |
959 | if (TREE_CODE (t2) == BOOLEAN_TYPE) | |
960 | return t1; | |
961 | ||
962 | return c_common_type (t1, t2); | |
963 | } | |
964 | ||
965 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment | |
966 | or various other operations. Return 2 if they are compatible | |
967 | but a warning may be needed if you use them together. */ | |
968 | ||
969 | int | |
970 | comptypes (tree type1, tree type2) | |
971 | { | |
972 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
973 | int val; | |
974 | ||
975 | val = comptypes_internal (type1, type2, NULL, NULL); | |
976 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
977 | ||
978 | return val; | |
979 | } | |
980 | ||
981 | /* Like comptypes, but if it returns non-zero because enum and int are | |
982 | compatible, it sets *ENUM_AND_INT_P to true. */ | |
983 | ||
984 | static int | |
985 | comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p) | |
986 | { | |
987 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
988 | int val; | |
989 | ||
990 | val = comptypes_internal (type1, type2, enum_and_int_p, NULL); | |
991 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
992 | ||
993 | return val; | |
994 | } | |
995 | ||
996 | /* Like comptypes, but if it returns nonzero for different types, it | |
997 | sets *DIFFERENT_TYPES_P to true. */ | |
998 | ||
999 | int | |
1000 | comptypes_check_different_types (tree type1, tree type2, | |
1001 | bool *different_types_p) | |
1002 | { | |
1003 | const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base; | |
1004 | int val; | |
1005 | ||
1006 | val = comptypes_internal (type1, type2, NULL, different_types_p); | |
1007 | free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1); | |
1008 | ||
1009 | return val; | |
1010 | } | |
1011 | \f | |
1012 | /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment | |
1013 | or various other operations. Return 2 if they are compatible | |
1014 | but a warning may be needed if you use them together. If | |
1015 | ENUM_AND_INT_P is not NULL, and one type is an enum and the other a | |
1016 | compatible integer type, then this sets *ENUM_AND_INT_P to true; | |
1017 | *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not | |
1018 | NULL, and the types are compatible but different enough not to be | |
1019 | permitted in C11 typedef redeclarations, then this sets | |
1020 | *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to | |
1021 | false, but may or may not be set if the types are incompatible. | |
1022 | This differs from comptypes, in that we don't free the seen | |
1023 | types. */ | |
1024 | ||
1025 | static int | |
1026 | comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, | |
1027 | bool *different_types_p) | |
1028 | { | |
1029 | const_tree t1 = type1; | |
1030 | const_tree t2 = type2; | |
1031 | int attrval, val; | |
1032 | ||
1033 | /* Suppress errors caused by previously reported errors. */ | |
1034 | ||
1035 | if (t1 == t2 || !t1 || !t2 | |
1036 | || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK) | |
1037 | return 1; | |
1038 | ||
1039 | /* Enumerated types are compatible with integer types, but this is | |
1040 | not transitive: two enumerated types in the same translation unit | |
1041 | are compatible with each other only if they are the same type. */ | |
1042 | ||
1043 | if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) | |
1044 | { | |
1045 | t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); | |
1046 | if (TREE_CODE (t2) != VOID_TYPE) | |
1047 | { | |
1048 | if (enum_and_int_p != NULL) | |
1049 | *enum_and_int_p = true; | |
1050 | if (different_types_p != NULL) | |
1051 | *different_types_p = true; | |
1052 | } | |
1053 | } | |
1054 | else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) | |
1055 | { | |
1056 | t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); | |
1057 | if (TREE_CODE (t1) != VOID_TYPE) | |
1058 | { | |
1059 | if (enum_and_int_p != NULL) | |
1060 | *enum_and_int_p = true; | |
1061 | if (different_types_p != NULL) | |
1062 | *different_types_p = true; | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | if (t1 == t2) | |
1067 | return 1; | |
1068 | ||
1069 | /* Different classes of types can't be compatible. */ | |
1070 | ||
1071 | if (TREE_CODE (t1) != TREE_CODE (t2)) | |
1072 | return 0; | |
1073 | ||
1074 | /* Qualifiers must match. C99 6.7.3p9 */ | |
1075 | ||
1076 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) | |
1077 | return 0; | |
1078 | ||
1079 | /* Allow for two different type nodes which have essentially the same | |
1080 | definition. Note that we already checked for equality of the type | |
1081 | qualifiers (just above). */ | |
1082 | ||
1083 | if (TREE_CODE (t1) != ARRAY_TYPE | |
1084 | && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) | |
1085 | return 1; | |
1086 | ||
1087 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1088 | if (!(attrval = comp_type_attributes (t1, t2))) | |
1089 | return 0; | |
1090 | ||
1091 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1092 | val = 0; | |
1093 | ||
1094 | switch (TREE_CODE (t1)) | |
1095 | { | |
1096 | case POINTER_TYPE: | |
1097 | /* Do not remove mode or aliasing information. */ | |
1098 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1099 | || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)) | |
1100 | break; | |
1101 | val = (TREE_TYPE (t1) == TREE_TYPE (t2) | |
1102 | ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
1103 | enum_and_int_p, different_types_p)); | |
1104 | break; | |
1105 | ||
1106 | case FUNCTION_TYPE: | |
1107 | val = function_types_compatible_p (t1, t2, enum_and_int_p, | |
1108 | different_types_p); | |
1109 | break; | |
1110 | ||
1111 | case ARRAY_TYPE: | |
1112 | { | |
1113 | tree d1 = TYPE_DOMAIN (t1); | |
1114 | tree d2 = TYPE_DOMAIN (t2); | |
1115 | bool d1_variable, d2_variable; | |
1116 | bool d1_zero, d2_zero; | |
1117 | val = 1; | |
1118 | ||
1119 | /* Target types must match incl. qualifiers. */ | |
1120 | if (TREE_TYPE (t1) != TREE_TYPE (t2) | |
1121 | && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
1122 | enum_and_int_p, | |
1123 | different_types_p))) | |
1124 | return 0; | |
1125 | ||
1126 | if (different_types_p != NULL | |
1127 | && (d1 == 0) != (d2 == 0)) | |
1128 | *different_types_p = true; | |
1129 | /* Sizes must match unless one is missing or variable. */ | |
1130 | if (d1 == 0 || d2 == 0 || d1 == d2) | |
1131 | break; | |
1132 | ||
1133 | d1_zero = !TYPE_MAX_VALUE (d1); | |
1134 | d2_zero = !TYPE_MAX_VALUE (d2); | |
1135 | ||
1136 | d1_variable = (!d1_zero | |
1137 | && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST | |
1138 | || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST)); | |
1139 | d2_variable = (!d2_zero | |
1140 | && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST | |
1141 | || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)); | |
1142 | d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1)); | |
1143 | d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2)); | |
1144 | ||
1145 | if (different_types_p != NULL | |
1146 | && d1_variable != d2_variable) | |
1147 | *different_types_p = true; | |
1148 | if (d1_variable || d2_variable) | |
1149 | break; | |
1150 | if (d1_zero && d2_zero) | |
1151 | break; | |
1152 | if (d1_zero || d2_zero | |
1153 | || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)) | |
1154 | || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2))) | |
1155 | val = 0; | |
1156 | ||
1157 | break; | |
1158 | } | |
1159 | ||
1160 | case ENUMERAL_TYPE: | |
1161 | case RECORD_TYPE: | |
1162 | case UNION_TYPE: | |
1163 | if (val != 1 && !same_translation_unit_p (t1, t2)) | |
1164 | { | |
1165 | tree a1 = TYPE_ATTRIBUTES (t1); | |
1166 | tree a2 = TYPE_ATTRIBUTES (t2); | |
1167 | ||
1168 | if (! attribute_list_contained (a1, a2) | |
1169 | && ! attribute_list_contained (a2, a1)) | |
1170 | break; | |
1171 | ||
1172 | if (attrval != 2) | |
1173 | return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, | |
1174 | different_types_p); | |
1175 | val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p, | |
1176 | different_types_p); | |
1177 | } | |
1178 | break; | |
1179 | ||
1180 | case VECTOR_TYPE: | |
1181 | val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | |
1182 | && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), | |
1183 | enum_and_int_p, different_types_p)); | |
1184 | break; | |
1185 | ||
1186 | default: | |
1187 | break; | |
1188 | } | |
1189 | return attrval == 2 && val == 1 ? 2 : val; | |
1190 | } | |
1191 | ||
1192 | /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring | |
1193 | their qualifiers, except for named address spaces. If the pointers point to | |
1194 | different named addresses, then we must determine if one address space is a | |
1195 | subset of the other. */ | |
1196 | ||
1197 | static int | |
1198 | comp_target_types (location_t location, tree ttl, tree ttr) | |
1199 | { | |
1200 | int val; | |
1201 | tree mvl = TREE_TYPE (ttl); | |
1202 | tree mvr = TREE_TYPE (ttr); | |
1203 | addr_space_t asl = TYPE_ADDR_SPACE (mvl); | |
1204 | addr_space_t asr = TYPE_ADDR_SPACE (mvr); | |
1205 | addr_space_t as_common; | |
1206 | bool enum_and_int_p; | |
1207 | ||
1208 | /* Fail if pointers point to incompatible address spaces. */ | |
1209 | if (!addr_space_superset (asl, asr, &as_common)) | |
1210 | return 0; | |
1211 | ||
1212 | /* Do not lose qualifiers on element types of array types that are | |
1213 | pointer targets by taking their TYPE_MAIN_VARIANT. */ | |
1214 | if (TREE_CODE (mvl) != ARRAY_TYPE) | |
1215 | mvl = TYPE_MAIN_VARIANT (mvl); | |
1216 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
1217 | mvr = TYPE_MAIN_VARIANT (mvr); | |
1218 | enum_and_int_p = false; | |
1219 | val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p); | |
1220 | ||
1221 | if (val == 2) | |
1222 | pedwarn (location, OPT_Wpedantic, "types are not quite compatible"); | |
1223 | ||
1224 | if (val == 1 && enum_and_int_p && warn_cxx_compat) | |
1225 | warning_at (location, OPT_Wc___compat, | |
1226 | "pointer target types incompatible in C++"); | |
1227 | ||
1228 | return val; | |
1229 | } | |
1230 | \f | |
1231 | /* Subroutines of `comptypes'. */ | |
1232 | ||
1233 | /* Determine whether two trees derive from the same translation unit. | |
1234 | If the CONTEXT chain ends in a null, that tree's context is still | |
1235 | being parsed, so if two trees have context chains ending in null, | |
1236 | they're in the same translation unit. */ | |
1237 | int | |
1238 | same_translation_unit_p (const_tree t1, const_tree t2) | |
1239 | { | |
1240 | while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL) | |
1241 | switch (TREE_CODE_CLASS (TREE_CODE (t1))) | |
1242 | { | |
1243 | case tcc_declaration: | |
1244 | t1 = DECL_CONTEXT (t1); break; | |
1245 | case tcc_type: | |
1246 | t1 = TYPE_CONTEXT (t1); break; | |
1247 | case tcc_exceptional: | |
1248 | t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */ | |
1249 | default: gcc_unreachable (); | |
1250 | } | |
1251 | ||
1252 | while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL) | |
1253 | switch (TREE_CODE_CLASS (TREE_CODE (t2))) | |
1254 | { | |
1255 | case tcc_declaration: | |
1256 | t2 = DECL_CONTEXT (t2); break; | |
1257 | case tcc_type: | |
1258 | t2 = TYPE_CONTEXT (t2); break; | |
1259 | case tcc_exceptional: | |
1260 | t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */ | |
1261 | default: gcc_unreachable (); | |
1262 | } | |
1263 | ||
1264 | return t1 == t2; | |
1265 | } | |
1266 | ||
1267 | /* Allocate the seen two types, assuming that they are compatible. */ | |
1268 | ||
1269 | static struct tagged_tu_seen_cache * | |
1270 | alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2) | |
1271 | { | |
1272 | struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache); | |
1273 | tu->next = tagged_tu_seen_base; | |
1274 | tu->t1 = t1; | |
1275 | tu->t2 = t2; | |
1276 | ||
1277 | tagged_tu_seen_base = tu; | |
1278 | ||
1279 | /* The C standard says that two structures in different translation | |
1280 | units are compatible with each other only if the types of their | |
1281 | fields are compatible (among other things). We assume that they | |
1282 | are compatible until proven otherwise when building the cache. | |
1283 | An example where this can occur is: | |
1284 | struct a | |
1285 | { | |
1286 | struct a *next; | |
1287 | }; | |
1288 | If we are comparing this against a similar struct in another TU, | |
1289 | and did not assume they were compatible, we end up with an infinite | |
1290 | loop. */ | |
1291 | tu->val = 1; | |
1292 | return tu; | |
1293 | } | |
1294 | ||
1295 | /* Free the seen types until we get to TU_TIL. */ | |
1296 | ||
1297 | static void | |
1298 | free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til) | |
1299 | { | |
1300 | const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base; | |
1301 | while (tu != tu_til) | |
1302 | { | |
1303 | const struct tagged_tu_seen_cache *const tu1 | |
1304 | = (const struct tagged_tu_seen_cache *) tu; | |
1305 | tu = tu1->next; | |
1306 | free (CONST_CAST (struct tagged_tu_seen_cache *, tu1)); | |
1307 | } | |
1308 | tagged_tu_seen_base = tu_til; | |
1309 | } | |
1310 | ||
1311 | /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are | |
1312 | compatible. If the two types are not the same (which has been | |
1313 | checked earlier), this can only happen when multiple translation | |
1314 | units are being compiled. See C99 6.2.7 paragraph 1 for the exact | |
1315 | rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in | |
1316 | comptypes_internal. */ | |
1317 | ||
1318 | static int | |
1319 | tagged_types_tu_compatible_p (const_tree t1, const_tree t2, | |
1320 | bool *enum_and_int_p, bool *different_types_p) | |
1321 | { | |
1322 | tree s1, s2; | |
1323 | bool needs_warning = false; | |
1324 | ||
1325 | /* We have to verify that the tags of the types are the same. This | |
1326 | is harder than it looks because this may be a typedef, so we have | |
1327 | to go look at the original type. It may even be a typedef of a | |
1328 | typedef... | |
1329 | In the case of compiler-created builtin structs the TYPE_DECL | |
1330 | may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */ | |
1331 | while (TYPE_NAME (t1) | |
1332 | && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
1333 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t1))) | |
1334 | t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1)); | |
1335 | ||
1336 | while (TYPE_NAME (t2) | |
1337 | && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
1338 | && DECL_ORIGINAL_TYPE (TYPE_NAME (t2))) | |
1339 | t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2)); | |
1340 | ||
1341 | /* C90 didn't have the requirement that the two tags be the same. */ | |
1342 | if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2)) | |
1343 | return 0; | |
1344 | ||
1345 | /* C90 didn't say what happened if one or both of the types were | |
1346 | incomplete; we choose to follow C99 rules here, which is that they | |
1347 | are compatible. */ | |
1348 | if (TYPE_SIZE (t1) == NULL | |
1349 | || TYPE_SIZE (t2) == NULL) | |
1350 | return 1; | |
1351 | ||
1352 | { | |
1353 | const struct tagged_tu_seen_cache * tts_i; | |
1354 | for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next) | |
1355 | if (tts_i->t1 == t1 && tts_i->t2 == t2) | |
1356 | return tts_i->val; | |
1357 | } | |
1358 | ||
1359 | switch (TREE_CODE (t1)) | |
1360 | { | |
1361 | case ENUMERAL_TYPE: | |
1362 | { | |
1363 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); | |
1364 | /* Speed up the case where the type values are in the same order. */ | |
1365 | tree tv1 = TYPE_VALUES (t1); | |
1366 | tree tv2 = TYPE_VALUES (t2); | |
1367 | ||
1368 | if (tv1 == tv2) | |
1369 | { | |
1370 | return 1; | |
1371 | } | |
1372 | ||
1373 | for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2)) | |
1374 | { | |
1375 | if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2)) | |
1376 | break; | |
1377 | if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1) | |
1378 | { | |
1379 | tu->val = 0; | |
1380 | return 0; | |
1381 | } | |
1382 | } | |
1383 | ||
1384 | if (tv1 == NULL_TREE && tv2 == NULL_TREE) | |
1385 | { | |
1386 | return 1; | |
1387 | } | |
1388 | if (tv1 == NULL_TREE || tv2 == NULL_TREE) | |
1389 | { | |
1390 | tu->val = 0; | |
1391 | return 0; | |
1392 | } | |
1393 | ||
1394 | if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2))) | |
1395 | { | |
1396 | tu->val = 0; | |
1397 | return 0; | |
1398 | } | |
1399 | ||
1400 | for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1)) | |
1401 | { | |
1402 | s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2)); | |
1403 | if (s2 == NULL | |
1404 | || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1) | |
1405 | { | |
1406 | tu->val = 0; | |
1407 | return 0; | |
1408 | } | |
1409 | } | |
1410 | return 1; | |
1411 | } | |
1412 | ||
1413 | case UNION_TYPE: | |
1414 | { | |
1415 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); | |
1416 | if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2))) | |
1417 | { | |
1418 | tu->val = 0; | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | /* Speed up the common case where the fields are in the same order. */ | |
1423 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2; | |
1424 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) | |
1425 | { | |
1426 | int result; | |
1427 | ||
1428 | if (DECL_NAME (s1) != DECL_NAME (s2)) | |
1429 | break; | |
1430 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), | |
1431 | enum_and_int_p, different_types_p); | |
1432 | ||
1433 | if (result != 1 && !DECL_NAME (s1)) | |
1434 | break; | |
1435 | if (result == 0) | |
1436 | { | |
1437 | tu->val = 0; | |
1438 | return 0; | |
1439 | } | |
1440 | if (result == 2) | |
1441 | needs_warning = true; | |
1442 | ||
1443 | if (TREE_CODE (s1) == FIELD_DECL | |
1444 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1445 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1446 | { | |
1447 | tu->val = 0; | |
1448 | return 0; | |
1449 | } | |
1450 | } | |
1451 | if (!s1 && !s2) | |
1452 | { | |
1453 | tu->val = needs_warning ? 2 : 1; | |
1454 | return tu->val; | |
1455 | } | |
1456 | ||
1457 | for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1)) | |
1458 | { | |
1459 | bool ok = false; | |
1460 | ||
1461 | for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2)) | |
1462 | if (DECL_NAME (s1) == DECL_NAME (s2)) | |
1463 | { | |
1464 | int result; | |
1465 | ||
1466 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), | |
1467 | enum_and_int_p, | |
1468 | different_types_p); | |
1469 | ||
1470 | if (result != 1 && !DECL_NAME (s1)) | |
1471 | continue; | |
1472 | if (result == 0) | |
1473 | { | |
1474 | tu->val = 0; | |
1475 | return 0; | |
1476 | } | |
1477 | if (result == 2) | |
1478 | needs_warning = true; | |
1479 | ||
1480 | if (TREE_CODE (s1) == FIELD_DECL | |
1481 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1482 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1483 | break; | |
1484 | ||
1485 | ok = true; | |
1486 | break; | |
1487 | } | |
1488 | if (!ok) | |
1489 | { | |
1490 | tu->val = 0; | |
1491 | return 0; | |
1492 | } | |
1493 | } | |
1494 | tu->val = needs_warning ? 2 : 10; | |
1495 | return tu->val; | |
1496 | } | |
1497 | ||
1498 | case RECORD_TYPE: | |
1499 | { | |
1500 | struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2); | |
1501 | ||
1502 | for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); | |
1503 | s1 && s2; | |
1504 | s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) | |
1505 | { | |
1506 | int result; | |
1507 | if (TREE_CODE (s1) != TREE_CODE (s2) | |
1508 | || DECL_NAME (s1) != DECL_NAME (s2)) | |
1509 | break; | |
1510 | result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), | |
1511 | enum_and_int_p, different_types_p); | |
1512 | if (result == 0) | |
1513 | break; | |
1514 | if (result == 2) | |
1515 | needs_warning = true; | |
1516 | ||
1517 | if (TREE_CODE (s1) == FIELD_DECL | |
1518 | && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), | |
1519 | DECL_FIELD_BIT_OFFSET (s2)) != 1) | |
1520 | break; | |
1521 | } | |
1522 | if (s1 && s2) | |
1523 | tu->val = 0; | |
1524 | else | |
1525 | tu->val = needs_warning ? 2 : 1; | |
1526 | return tu->val; | |
1527 | } | |
1528 | ||
1529 | default: | |
1530 | gcc_unreachable (); | |
1531 | } | |
1532 | } | |
1533 | ||
1534 | /* Return 1 if two function types F1 and F2 are compatible. | |
1535 | If either type specifies no argument types, | |
1536 | the other must specify a fixed number of self-promoting arg types. | |
1537 | Otherwise, if one type specifies only the number of arguments, | |
1538 | the other must specify that number of self-promoting arg types. | |
1539 | Otherwise, the argument types must match. | |
1540 | ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */ | |
1541 | ||
1542 | static int | |
1543 | function_types_compatible_p (const_tree f1, const_tree f2, | |
1544 | bool *enum_and_int_p, bool *different_types_p) | |
1545 | { | |
1546 | tree args1, args2; | |
1547 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1548 | int val = 1; | |
1549 | int val1; | |
1550 | tree ret1, ret2; | |
1551 | ||
1552 | ret1 = TREE_TYPE (f1); | |
1553 | ret2 = TREE_TYPE (f2); | |
1554 | ||
1555 | /* 'volatile' qualifiers on a function's return type used to mean | |
1556 | the function is noreturn. */ | |
1557 | if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2)) | |
1558 | pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>"); | |
1559 | if (TYPE_VOLATILE (ret1)) | |
1560 | ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1), | |
1561 | TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE); | |
1562 | if (TYPE_VOLATILE (ret2)) | |
1563 | ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2), | |
1564 | TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE); | |
1565 | val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p); | |
1566 | if (val == 0) | |
1567 | return 0; | |
1568 | ||
1569 | args1 = TYPE_ARG_TYPES (f1); | |
1570 | args2 = TYPE_ARG_TYPES (f2); | |
1571 | ||
1572 | if (different_types_p != NULL | |
1573 | && (args1 == 0) != (args2 == 0)) | |
1574 | *different_types_p = true; | |
1575 | ||
1576 | /* An unspecified parmlist matches any specified parmlist | |
1577 | whose argument types don't need default promotions. */ | |
1578 | ||
1579 | if (args1 == 0) | |
1580 | { | |
1581 | if (!self_promoting_args_p (args2)) | |
1582 | return 0; | |
1583 | /* If one of these types comes from a non-prototype fn definition, | |
1584 | compare that with the other type's arglist. | |
1585 | If they don't match, ask for a warning (but no error). */ | |
1586 | if (TYPE_ACTUAL_ARG_TYPES (f1) | |
1587 | && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), | |
1588 | enum_and_int_p, different_types_p)) | |
1589 | val = 2; | |
1590 | return val; | |
1591 | } | |
1592 | if (args2 == 0) | |
1593 | { | |
1594 | if (!self_promoting_args_p (args1)) | |
1595 | return 0; | |
1596 | if (TYPE_ACTUAL_ARG_TYPES (f2) | |
1597 | && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), | |
1598 | enum_and_int_p, different_types_p)) | |
1599 | val = 2; | |
1600 | return val; | |
1601 | } | |
1602 | ||
1603 | /* Both types have argument lists: compare them and propagate results. */ | |
1604 | val1 = type_lists_compatible_p (args1, args2, enum_and_int_p, | |
1605 | different_types_p); | |
1606 | return val1 != 1 ? val1 : val; | |
1607 | } | |
1608 | ||
1609 | /* Check two lists of types for compatibility, returning 0 for | |
1610 | incompatible, 1 for compatible, or 2 for compatible with | |
1611 | warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in | |
1612 | comptypes_internal. */ | |
1613 | ||
1614 | static int | |
1615 | type_lists_compatible_p (const_tree args1, const_tree args2, | |
1616 | bool *enum_and_int_p, bool *different_types_p) | |
1617 | { | |
1618 | /* 1 if no need for warning yet, 2 if warning cause has been seen. */ | |
1619 | int val = 1; | |
1620 | int newval = 0; | |
1621 | ||
1622 | while (1) | |
1623 | { | |
1624 | tree a1, mv1, a2, mv2; | |
1625 | if (args1 == 0 && args2 == 0) | |
1626 | return val; | |
1627 | /* If one list is shorter than the other, | |
1628 | they fail to match. */ | |
1629 | if (args1 == 0 || args2 == 0) | |
1630 | return 0; | |
1631 | mv1 = a1 = TREE_VALUE (args1); | |
1632 | mv2 = a2 = TREE_VALUE (args2); | |
1633 | if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE) | |
1634 | mv1 = TYPE_MAIN_VARIANT (mv1); | |
1635 | if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE) | |
1636 | mv2 = TYPE_MAIN_VARIANT (mv2); | |
1637 | /* A null pointer instead of a type | |
1638 | means there is supposed to be an argument | |
1639 | but nothing is specified about what type it has. | |
1640 | So match anything that self-promotes. */ | |
1641 | if (different_types_p != NULL | |
1642 | && (a1 == 0) != (a2 == 0)) | |
1643 | *different_types_p = true; | |
1644 | if (a1 == 0) | |
1645 | { | |
1646 | if (c_type_promotes_to (a2) != a2) | |
1647 | return 0; | |
1648 | } | |
1649 | else if (a2 == 0) | |
1650 | { | |
1651 | if (c_type_promotes_to (a1) != a1) | |
1652 | return 0; | |
1653 | } | |
1654 | /* If one of the lists has an error marker, ignore this arg. */ | |
1655 | else if (TREE_CODE (a1) == ERROR_MARK | |
1656 | || TREE_CODE (a2) == ERROR_MARK) | |
1657 | ; | |
1658 | else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p, | |
1659 | different_types_p))) | |
1660 | { | |
1661 | if (different_types_p != NULL) | |
1662 | *different_types_p = true; | |
1663 | /* Allow wait (union {union wait *u; int *i} *) | |
1664 | and wait (union wait *) to be compatible. */ | |
1665 | if (TREE_CODE (a1) == UNION_TYPE | |
1666 | && (TYPE_NAME (a1) == 0 | |
1667 | || TYPE_TRANSPARENT_AGGR (a1)) | |
1668 | && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST | |
1669 | && tree_int_cst_equal (TYPE_SIZE (a1), | |
1670 | TYPE_SIZE (a2))) | |
1671 | { | |
1672 | tree memb; | |
1673 | for (memb = TYPE_FIELDS (a1); | |
1674 | memb; memb = DECL_CHAIN (memb)) | |
1675 | { | |
1676 | tree mv3 = TREE_TYPE (memb); | |
1677 | if (mv3 && mv3 != error_mark_node | |
1678 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
1679 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
1680 | if (comptypes_internal (mv3, mv2, enum_and_int_p, | |
1681 | different_types_p)) | |
1682 | break; | |
1683 | } | |
1684 | if (memb == 0) | |
1685 | return 0; | |
1686 | } | |
1687 | else if (TREE_CODE (a2) == UNION_TYPE | |
1688 | && (TYPE_NAME (a2) == 0 | |
1689 | || TYPE_TRANSPARENT_AGGR (a2)) | |
1690 | && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST | |
1691 | && tree_int_cst_equal (TYPE_SIZE (a2), | |
1692 | TYPE_SIZE (a1))) | |
1693 | { | |
1694 | tree memb; | |
1695 | for (memb = TYPE_FIELDS (a2); | |
1696 | memb; memb = DECL_CHAIN (memb)) | |
1697 | { | |
1698 | tree mv3 = TREE_TYPE (memb); | |
1699 | if (mv3 && mv3 != error_mark_node | |
1700 | && TREE_CODE (mv3) != ARRAY_TYPE) | |
1701 | mv3 = TYPE_MAIN_VARIANT (mv3); | |
1702 | if (comptypes_internal (mv3, mv1, enum_and_int_p, | |
1703 | different_types_p)) | |
1704 | break; | |
1705 | } | |
1706 | if (memb == 0) | |
1707 | return 0; | |
1708 | } | |
1709 | else | |
1710 | return 0; | |
1711 | } | |
1712 | ||
1713 | /* comptypes said ok, but record if it said to warn. */ | |
1714 | if (newval > val) | |
1715 | val = newval; | |
1716 | ||
1717 | args1 = TREE_CHAIN (args1); | |
1718 | args2 = TREE_CHAIN (args2); | |
1719 | } | |
1720 | } | |
1721 | \f | |
1722 | /* Compute the size to increment a pointer by. */ | |
1723 | ||
1724 | static tree | |
1725 | c_size_in_bytes (const_tree type) | |
1726 | { | |
1727 | enum tree_code code = TREE_CODE (type); | |
1728 | ||
1729 | if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) | |
1730 | return size_one_node; | |
1731 | ||
1732 | if (!COMPLETE_OR_VOID_TYPE_P (type)) | |
1733 | { | |
1734 | error ("arithmetic on pointer to an incomplete type"); | |
1735 | return size_one_node; | |
1736 | } | |
1737 | ||
1738 | /* Convert in case a char is more than one unit. */ | |
1739 | return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), | |
1740 | size_int (TYPE_PRECISION (char_type_node) | |
1741 | / BITS_PER_UNIT)); | |
1742 | } | |
1743 | \f | |
1744 | /* Return either DECL or its known constant value (if it has one). */ | |
1745 | ||
1746 | tree | |
1747 | decl_constant_value (tree decl) | |
1748 | { | |
1749 | if (/* Don't change a variable array bound or initial value to a constant | |
1750 | in a place where a variable is invalid. Note that DECL_INITIAL | |
1751 | isn't valid for a PARM_DECL. */ | |
1752 | current_function_decl != 0 | |
1753 | && TREE_CODE (decl) != PARM_DECL | |
1754 | && !TREE_THIS_VOLATILE (decl) | |
1755 | && TREE_READONLY (decl) | |
1756 | && DECL_INITIAL (decl) != 0 | |
1757 | && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK | |
1758 | /* This is invalid if initial value is not constant. | |
1759 | If it has either a function call, a memory reference, | |
1760 | or a variable, then re-evaluating it could give different results. */ | |
1761 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1762 | /* Check for cases where this is sub-optimal, even though valid. */ | |
1763 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) | |
1764 | return DECL_INITIAL (decl); | |
1765 | return decl; | |
1766 | } | |
1767 | ||
1768 | /* Convert the array expression EXP to a pointer. */ | |
1769 | static tree | |
1770 | array_to_pointer_conversion (location_t loc, tree exp) | |
1771 | { | |
1772 | tree orig_exp = exp; | |
1773 | tree type = TREE_TYPE (exp); | |
1774 | tree adr; | |
1775 | tree restype = TREE_TYPE (type); | |
1776 | tree ptrtype; | |
1777 | ||
1778 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
1779 | ||
1780 | STRIP_TYPE_NOPS (exp); | |
1781 | ||
1782 | if (TREE_NO_WARNING (orig_exp)) | |
1783 | TREE_NO_WARNING (exp) = 1; | |
1784 | ||
1785 | ptrtype = build_pointer_type (restype); | |
1786 | ||
1787 | if (TREE_CODE (exp) == INDIRECT_REF) | |
1788 | return convert (ptrtype, TREE_OPERAND (exp, 0)); | |
1789 | ||
1790 | /* In C++ array compound literals are temporary objects unless they are | |
1791 | const or appear in namespace scope, so they are destroyed too soon | |
1792 | to use them for much of anything (c++/53220). */ | |
1793 | if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) | |
1794 | { | |
1795 | tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
1796 | if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) | |
1797 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, | |
1798 | "converting an array compound literal to a pointer " | |
1799 | "is ill-formed in C++"); | |
1800 | } | |
1801 | ||
1802 | adr = build_unary_op (loc, ADDR_EXPR, exp, 1); | |
1803 | return convert (ptrtype, adr); | |
1804 | } | |
1805 | ||
1806 | /* Convert the function expression EXP to a pointer. */ | |
1807 | static tree | |
1808 | function_to_pointer_conversion (location_t loc, tree exp) | |
1809 | { | |
1810 | tree orig_exp = exp; | |
1811 | ||
1812 | gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE); | |
1813 | ||
1814 | STRIP_TYPE_NOPS (exp); | |
1815 | ||
1816 | if (TREE_NO_WARNING (orig_exp)) | |
1817 | TREE_NO_WARNING (exp) = 1; | |
1818 | ||
1819 | return build_unary_op (loc, ADDR_EXPR, exp, 0); | |
1820 | } | |
1821 | ||
1822 | /* Mark EXP as read, not just set, for set but not used -Wunused | |
1823 | warning purposes. */ | |
1824 | ||
1825 | void | |
1826 | mark_exp_read (tree exp) | |
1827 | { | |
1828 | switch (TREE_CODE (exp)) | |
1829 | { | |
1830 | case VAR_DECL: | |
1831 | case PARM_DECL: | |
1832 | DECL_READ_P (exp) = 1; | |
1833 | break; | |
1834 | case ARRAY_REF: | |
1835 | case COMPONENT_REF: | |
1836 | case MODIFY_EXPR: | |
1837 | case REALPART_EXPR: | |
1838 | case IMAGPART_EXPR: | |
1839 | CASE_CONVERT: | |
1840 | case ADDR_EXPR: | |
1841 | mark_exp_read (TREE_OPERAND (exp, 0)); | |
1842 | break; | |
1843 | case COMPOUND_EXPR: | |
1844 | case C_MAYBE_CONST_EXPR: | |
1845 | mark_exp_read (TREE_OPERAND (exp, 1)); | |
1846 | break; | |
1847 | default: | |
1848 | break; | |
1849 | } | |
1850 | } | |
1851 | ||
1852 | /* Perform the default conversion of arrays and functions to pointers. | |
1853 | Return the result of converting EXP. For any other expression, just | |
1854 | return EXP. | |
1855 | ||
1856 | LOC is the location of the expression. */ | |
1857 | ||
1858 | struct c_expr | |
1859 | default_function_array_conversion (location_t loc, struct c_expr exp) | |
1860 | { | |
1861 | tree orig_exp = exp.value; | |
1862 | tree type = TREE_TYPE (exp.value); | |
1863 | enum tree_code code = TREE_CODE (type); | |
1864 | ||
1865 | switch (code) | |
1866 | { | |
1867 | case ARRAY_TYPE: | |
1868 | { | |
1869 | bool not_lvalue = false; | |
1870 | bool lvalue_array_p; | |
1871 | ||
1872 | while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR | |
1873 | || CONVERT_EXPR_P (exp.value)) | |
1874 | && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type) | |
1875 | { | |
1876 | if (TREE_CODE (exp.value) == NON_LVALUE_EXPR) | |
1877 | not_lvalue = true; | |
1878 | exp.value = TREE_OPERAND (exp.value, 0); | |
1879 | } | |
1880 | ||
1881 | if (TREE_NO_WARNING (orig_exp)) | |
1882 | TREE_NO_WARNING (exp.value) = 1; | |
1883 | ||
1884 | lvalue_array_p = !not_lvalue && lvalue_p (exp.value); | |
1885 | if (!flag_isoc99 && !lvalue_array_p) | |
1886 | { | |
1887 | /* Before C99, non-lvalue arrays do not decay to pointers. | |
1888 | Normally, using such an array would be invalid; but it can | |
1889 | be used correctly inside sizeof or as a statement expression. | |
1890 | Thus, do not give an error here; an error will result later. */ | |
1891 | return exp; | |
1892 | } | |
1893 | ||
1894 | exp.value = array_to_pointer_conversion (loc, exp.value); | |
1895 | } | |
1896 | break; | |
1897 | case FUNCTION_TYPE: | |
1898 | exp.value = function_to_pointer_conversion (loc, exp.value); | |
1899 | break; | |
1900 | default: | |
1901 | break; | |
1902 | } | |
1903 | ||
1904 | return exp; | |
1905 | } | |
1906 | ||
1907 | struct c_expr | |
1908 | default_function_array_read_conversion (location_t loc, struct c_expr exp) | |
1909 | { | |
1910 | mark_exp_read (exp.value); | |
1911 | return default_function_array_conversion (loc, exp); | |
1912 | } | |
1913 | ||
1914 | /* EXP is an expression of integer type. Apply the integer promotions | |
1915 | to it and return the promoted value. */ | |
1916 | ||
1917 | tree | |
1918 | perform_integral_promotions (tree exp) | |
1919 | { | |
1920 | tree type = TREE_TYPE (exp); | |
1921 | enum tree_code code = TREE_CODE (type); | |
1922 | ||
1923 | gcc_assert (INTEGRAL_TYPE_P (type)); | |
1924 | ||
1925 | /* Normally convert enums to int, | |
1926 | but convert wide enums to something wider. */ | |
1927 | if (code == ENUMERAL_TYPE) | |
1928 | { | |
1929 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), | |
1930 | TYPE_PRECISION (integer_type_node)), | |
1931 | ((TYPE_PRECISION (type) | |
1932 | >= TYPE_PRECISION (integer_type_node)) | |
1933 | && TYPE_UNSIGNED (type))); | |
1934 | ||
1935 | return convert (type, exp); | |
1936 | } | |
1937 | ||
1938 | /* ??? This should no longer be needed now bit-fields have their | |
1939 | proper types. */ | |
1940 | if (TREE_CODE (exp) == COMPONENT_REF | |
1941 | && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)) | |
1942 | /* If it's thinner than an int, promote it like a | |
1943 | c_promoting_integer_type_p, otherwise leave it alone. */ | |
1944 | && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), | |
1945 | TYPE_PRECISION (integer_type_node))) | |
1946 | return convert (integer_type_node, exp); | |
1947 | ||
1948 | if (c_promoting_integer_type_p (type)) | |
1949 | { | |
1950 | /* Preserve unsignedness if not really getting any wider. */ | |
1951 | if (TYPE_UNSIGNED (type) | |
1952 | && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) | |
1953 | return convert (unsigned_type_node, exp); | |
1954 | ||
1955 | return convert (integer_type_node, exp); | |
1956 | } | |
1957 | ||
1958 | return exp; | |
1959 | } | |
1960 | ||
1961 | ||
1962 | /* Perform default promotions for C data used in expressions. | |
1963 | Enumeral types or short or char are converted to int. | |
1964 | In addition, manifest constants symbols are replaced by their values. */ | |
1965 | ||
1966 | tree | |
1967 | default_conversion (tree exp) | |
1968 | { | |
1969 | tree orig_exp; | |
1970 | tree type = TREE_TYPE (exp); | |
1971 | enum tree_code code = TREE_CODE (type); | |
1972 | tree promoted_type; | |
1973 | ||
1974 | mark_exp_read (exp); | |
1975 | ||
1976 | /* Functions and arrays have been converted during parsing. */ | |
1977 | gcc_assert (code != FUNCTION_TYPE); | |
1978 | if (code == ARRAY_TYPE) | |
1979 | return exp; | |
1980 | ||
1981 | /* Constants can be used directly unless they're not loadable. */ | |
1982 | if (TREE_CODE (exp) == CONST_DECL) | |
1983 | exp = DECL_INITIAL (exp); | |
1984 | ||
1985 | /* Strip no-op conversions. */ | |
1986 | orig_exp = exp; | |
1987 | STRIP_TYPE_NOPS (exp); | |
1988 | ||
1989 | if (TREE_NO_WARNING (orig_exp)) | |
1990 | TREE_NO_WARNING (exp) = 1; | |
1991 | ||
1992 | if (code == VOID_TYPE) | |
1993 | { | |
1994 | error ("void value not ignored as it ought to be"); | |
1995 | return error_mark_node; | |
1996 | } | |
1997 | ||
1998 | exp = require_complete_type (exp); | |
1999 | if (exp == error_mark_node) | |
2000 | return error_mark_node; | |
2001 | ||
2002 | promoted_type = targetm.promoted_type (type); | |
2003 | if (promoted_type) | |
2004 | return convert (promoted_type, exp); | |
2005 | ||
2006 | if (INTEGRAL_TYPE_P (type)) | |
2007 | return perform_integral_promotions (exp); | |
2008 | ||
2009 | return exp; | |
2010 | } | |
2011 | \f | |
2012 | /* Look up COMPONENT in a structure or union TYPE. | |
2013 | ||
2014 | If the component name is not found, returns NULL_TREE. Otherwise, | |
2015 | the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL | |
2016 | stepping down the chain to the component, which is in the last | |
2017 | TREE_VALUE of the list. Normally the list is of length one, but if | |
2018 | the component is embedded within (nested) anonymous structures or | |
2019 | unions, the list steps down the chain to the component. */ | |
2020 | ||
2021 | static tree | |
2022 | lookup_field (tree type, tree component) | |
2023 | { | |
2024 | tree field; | |
2025 | ||
2026 | /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers | |
2027 | to the field elements. Use a binary search on this array to quickly | |
2028 | find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC | |
2029 | will always be set for structures which have many elements. */ | |
2030 | ||
2031 | if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s) | |
2032 | { | |
2033 | int bot, top, half; | |
2034 | tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0]; | |
2035 | ||
2036 | field = TYPE_FIELDS (type); | |
2037 | bot = 0; | |
2038 | top = TYPE_LANG_SPECIFIC (type)->s->len; | |
2039 | while (top - bot > 1) | |
2040 | { | |
2041 | half = (top - bot + 1) >> 1; | |
2042 | field = field_array[bot+half]; | |
2043 | ||
2044 | if (DECL_NAME (field) == NULL_TREE) | |
2045 | { | |
2046 | /* Step through all anon unions in linear fashion. */ | |
2047 | while (DECL_NAME (field_array[bot]) == NULL_TREE) | |
2048 | { | |
2049 | field = field_array[bot++]; | |
2050 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
2051 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
2052 | { | |
2053 | tree anon = lookup_field (TREE_TYPE (field), component); | |
2054 | ||
2055 | if (anon) | |
2056 | return tree_cons (NULL_TREE, field, anon); | |
2057 | ||
2058 | /* The Plan 9 compiler permits referring | |
2059 | directly to an anonymous struct/union field | |
2060 | using a typedef name. */ | |
2061 | if (flag_plan9_extensions | |
2062 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2063 | && (TREE_CODE (TYPE_NAME (TREE_TYPE (field))) | |
2064 | == TYPE_DECL) | |
2065 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2066 | == component)) | |
2067 | break; | |
2068 | } | |
2069 | } | |
2070 | ||
2071 | /* Entire record is only anon unions. */ | |
2072 | if (bot > top) | |
2073 | return NULL_TREE; | |
2074 | ||
2075 | /* Restart the binary search, with new lower bound. */ | |
2076 | continue; | |
2077 | } | |
2078 | ||
2079 | if (DECL_NAME (field) == component) | |
2080 | break; | |
2081 | if (DECL_NAME (field) < component) | |
2082 | bot += half; | |
2083 | else | |
2084 | top = bot + half; | |
2085 | } | |
2086 | ||
2087 | if (DECL_NAME (field_array[bot]) == component) | |
2088 | field = field_array[bot]; | |
2089 | else if (DECL_NAME (field) != component) | |
2090 | return NULL_TREE; | |
2091 | } | |
2092 | else | |
2093 | { | |
2094 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
2095 | { | |
2096 | if (DECL_NAME (field) == NULL_TREE | |
2097 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
2098 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)) | |
2099 | { | |
2100 | tree anon = lookup_field (TREE_TYPE (field), component); | |
2101 | ||
2102 | if (anon) | |
2103 | return tree_cons (NULL_TREE, field, anon); | |
2104 | ||
2105 | /* The Plan 9 compiler permits referring directly to an | |
2106 | anonymous struct/union field using a typedef | |
2107 | name. */ | |
2108 | if (flag_plan9_extensions | |
2109 | && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE | |
2110 | && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL | |
2111 | && (DECL_NAME (TYPE_NAME (TREE_TYPE (field))) | |
2112 | == component)) | |
2113 | break; | |
2114 | } | |
2115 | ||
2116 | if (DECL_NAME (field) == component) | |
2117 | break; | |
2118 | } | |
2119 | ||
2120 | if (field == NULL_TREE) | |
2121 | return NULL_TREE; | |
2122 | } | |
2123 | ||
2124 | return tree_cons (NULL_TREE, field, NULL_TREE); | |
2125 | } | |
2126 | ||
2127 | /* Make an expression to refer to the COMPONENT field of structure or | |
2128 | union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the | |
2129 | location of the COMPONENT_REF. */ | |
2130 | ||
2131 | tree | |
2132 | build_component_ref (location_t loc, tree datum, tree component) | |
2133 | { | |
2134 | tree type = TREE_TYPE (datum); | |
2135 | enum tree_code code = TREE_CODE (type); | |
2136 | tree field = NULL; | |
2137 | tree ref; | |
2138 | bool datum_lvalue = lvalue_p (datum); | |
2139 | ||
2140 | if (!objc_is_public (datum, component)) | |
2141 | return error_mark_node; | |
2142 | ||
2143 | /* Detect Objective-C property syntax object.property. */ | |
2144 | if (c_dialect_objc () | |
2145 | && (ref = objc_maybe_build_component_ref (datum, component))) | |
2146 | return ref; | |
2147 | ||
2148 | /* See if there is a field or component with name COMPONENT. */ | |
2149 | ||
2150 | if (code == RECORD_TYPE || code == UNION_TYPE) | |
2151 | { | |
2152 | if (!COMPLETE_TYPE_P (type)) | |
2153 | { | |
2154 | c_incomplete_type_error (NULL_TREE, type); | |
2155 | return error_mark_node; | |
2156 | } | |
2157 | ||
2158 | field = lookup_field (type, component); | |
2159 | ||
2160 | if (!field) | |
2161 | { | |
2162 | error_at (loc, "%qT has no member named %qE", type, component); | |
2163 | return error_mark_node; | |
2164 | } | |
2165 | ||
2166 | /* Chain the COMPONENT_REFs if necessary down to the FIELD. | |
2167 | This might be better solved in future the way the C++ front | |
2168 | end does it - by giving the anonymous entities each a | |
2169 | separate name and type, and then have build_component_ref | |
2170 | recursively call itself. We can't do that here. */ | |
2171 | do | |
2172 | { | |
2173 | tree subdatum = TREE_VALUE (field); | |
2174 | int quals; | |
2175 | tree subtype; | |
2176 | bool use_datum_quals; | |
2177 | ||
2178 | if (TREE_TYPE (subdatum) == error_mark_node) | |
2179 | return error_mark_node; | |
2180 | ||
2181 | /* If this is an rvalue, it does not have qualifiers in C | |
2182 | standard terms and we must avoid propagating such | |
2183 | qualifiers down to a non-lvalue array that is then | |
2184 | converted to a pointer. */ | |
2185 | use_datum_quals = (datum_lvalue | |
2186 | || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE); | |
2187 | ||
2188 | quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum))); | |
2189 | if (use_datum_quals) | |
2190 | quals |= TYPE_QUALS (TREE_TYPE (datum)); | |
2191 | subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals); | |
2192 | ||
2193 | ref = build3 (COMPONENT_REF, subtype, datum, subdatum, | |
2194 | NULL_TREE); | |
2195 | SET_EXPR_LOCATION (ref, loc); | |
2196 | if (TREE_READONLY (subdatum) | |
2197 | || (use_datum_quals && TREE_READONLY (datum))) | |
2198 | TREE_READONLY (ref) = 1; | |
2199 | if (TREE_THIS_VOLATILE (subdatum) | |
2200 | || (use_datum_quals && TREE_THIS_VOLATILE (datum))) | |
2201 | TREE_THIS_VOLATILE (ref) = 1; | |
2202 | ||
2203 | if (TREE_DEPRECATED (subdatum)) | |
2204 | warn_deprecated_use (subdatum, NULL_TREE); | |
2205 | ||
2206 | datum = ref; | |
2207 | ||
2208 | field = TREE_CHAIN (field); | |
2209 | } | |
2210 | while (field); | |
2211 | ||
2212 | return ref; | |
2213 | } | |
2214 | else if (code != ERROR_MARK) | |
2215 | error_at (loc, | |
2216 | "request for member %qE in something not a structure or union", | |
2217 | component); | |
2218 | ||
2219 | return error_mark_node; | |
2220 | } | |
2221 | \f | |
2222 | /* Given an expression PTR for a pointer, return an expression | |
2223 | for the value pointed to. | |
2224 | ERRORSTRING is the name of the operator to appear in error messages. | |
2225 | ||
2226 | LOC is the location to use for the generated tree. */ | |
2227 | ||
2228 | tree | |
2229 | build_indirect_ref (location_t loc, tree ptr, ref_operator errstring) | |
2230 | { | |
2231 | tree pointer = default_conversion (ptr); | |
2232 | tree type = TREE_TYPE (pointer); | |
2233 | tree ref; | |
2234 | ||
2235 | if (TREE_CODE (type) == POINTER_TYPE) | |
2236 | { | |
2237 | if (CONVERT_EXPR_P (pointer) | |
2238 | || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) | |
2239 | { | |
2240 | /* If a warning is issued, mark it to avoid duplicates from | |
2241 | the backend. This only needs to be done at | |
2242 | warn_strict_aliasing > 2. */ | |
2243 | if (warn_strict_aliasing > 2) | |
2244 | if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), | |
2245 | type, TREE_OPERAND (pointer, 0))) | |
2246 | TREE_NO_WARNING (pointer) = 1; | |
2247 | } | |
2248 | ||
2249 | if (TREE_CODE (pointer) == ADDR_EXPR | |
2250 | && (TREE_TYPE (TREE_OPERAND (pointer, 0)) | |
2251 | == TREE_TYPE (type))) | |
2252 | { | |
2253 | ref = TREE_OPERAND (pointer, 0); | |
2254 | protected_set_expr_location (ref, loc); | |
2255 | return ref; | |
2256 | } | |
2257 | else | |
2258 | { | |
2259 | tree t = TREE_TYPE (type); | |
2260 | ||
2261 | ref = build1 (INDIRECT_REF, t, pointer); | |
2262 | ||
2263 | if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE) | |
2264 | { | |
2265 | error_at (loc, "dereferencing pointer to incomplete type"); | |
2266 | return error_mark_node; | |
2267 | } | |
2268 | if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0) | |
2269 | warning_at (loc, 0, "dereferencing %<void *%> pointer"); | |
2270 | ||
2271 | /* We *must* set TREE_READONLY when dereferencing a pointer to const, | |
2272 | so that we get the proper error message if the result is used | |
2273 | to assign to. Also, &* is supposed to be a no-op. | |
2274 | And ANSI C seems to specify that the type of the result | |
2275 | should be the const type. */ | |
2276 | /* A de-reference of a pointer to const is not a const. It is valid | |
2277 | to change it via some other pointer. */ | |
2278 | TREE_READONLY (ref) = TYPE_READONLY (t); | |
2279 | TREE_SIDE_EFFECTS (ref) | |
2280 | = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer); | |
2281 | TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t); | |
2282 | protected_set_expr_location (ref, loc); | |
2283 | return ref; | |
2284 | } | |
2285 | } | |
2286 | else if (TREE_CODE (pointer) != ERROR_MARK) | |
2287 | invalid_indirection_error (loc, type, errstring); | |
2288 | ||
2289 | return error_mark_node; | |
2290 | } | |
2291 | ||
2292 | /* This handles expressions of the form "a[i]", which denotes | |
2293 | an array reference. | |
2294 | ||
2295 | This is logically equivalent in C to *(a+i), but we may do it differently. | |
2296 | If A is a variable or a member, we generate a primitive ARRAY_REF. | |
2297 | This avoids forcing the array out of registers, and can work on | |
2298 | arrays that are not lvalues (for example, members of structures returned | |
2299 | by functions). | |
2300 | ||
2301 | For vector types, allow vector[i] but not i[vector], and create | |
2302 | *(((type*)&vectortype) + i) for the expression. | |
2303 | ||
2304 | LOC is the location to use for the returned expression. */ | |
2305 | ||
2306 | tree | |
2307 | build_array_ref (location_t loc, tree array, tree index) | |
2308 | { | |
2309 | tree ret; | |
2310 | bool swapped = false; | |
2311 | if (TREE_TYPE (array) == error_mark_node | |
2312 | || TREE_TYPE (index) == error_mark_node) | |
2313 | return error_mark_node; | |
2314 | ||
2315 | if (flag_enable_cilkplus && contains_array_notation_expr (index)) | |
2316 | { | |
2317 | size_t rank = 0; | |
2318 | if (!find_rank (loc, index, index, true, &rank)) | |
2319 | return error_mark_node; | |
2320 | if (rank > 1) | |
2321 | { | |
2322 | error_at (loc, "rank of the array's index is greater than 1"); | |
2323 | return error_mark_node; | |
2324 | } | |
2325 | } | |
2326 | if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE | |
2327 | && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE | |
2328 | /* Allow vector[index] but not index[vector]. */ | |
2329 | && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE) | |
2330 | { | |
2331 | tree temp; | |
2332 | if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE | |
2333 | && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE) | |
2334 | { | |
2335 | error_at (loc, | |
2336 | "subscripted value is neither array nor pointer nor vector"); | |
2337 | ||
2338 | return error_mark_node; | |
2339 | } | |
2340 | temp = array; | |
2341 | array = index; | |
2342 | index = temp; | |
2343 | swapped = true; | |
2344 | } | |
2345 | ||
2346 | if (!INTEGRAL_TYPE_P (TREE_TYPE (index))) | |
2347 | { | |
2348 | error_at (loc, "array subscript is not an integer"); | |
2349 | return error_mark_node; | |
2350 | } | |
2351 | ||
2352 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE) | |
2353 | { | |
2354 | error_at (loc, "subscripted value is pointer to function"); | |
2355 | return error_mark_node; | |
2356 | } | |
2357 | ||
2358 | /* ??? Existing practice has been to warn only when the char | |
2359 | index is syntactically the index, not for char[array]. */ | |
2360 | if (!swapped) | |
2361 | warn_array_subscript_with_type_char (index); | |
2362 | ||
2363 | /* Apply default promotions *after* noticing character types. */ | |
2364 | index = default_conversion (index); | |
2365 | ||
2366 | gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE); | |
2367 | ||
2368 | convert_vector_to_pointer_for_subscript (loc, &array, index); | |
2369 | ||
2370 | if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) | |
2371 | { | |
2372 | tree rval, type; | |
2373 | ||
2374 | /* An array that is indexed by a non-constant | |
2375 | cannot be stored in a register; we must be able to do | |
2376 | address arithmetic on its address. | |
2377 | Likewise an array of elements of variable size. */ | |
2378 | if (TREE_CODE (index) != INTEGER_CST | |
2379 | || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array))) | |
2380 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)) | |
2381 | { | |
2382 | if (!c_mark_addressable (array)) | |
2383 | return error_mark_node; | |
2384 | } | |
2385 | /* An array that is indexed by a constant value which is not within | |
2386 | the array bounds cannot be stored in a register either; because we | |
2387 | would get a crash in store_bit_field/extract_bit_field when trying | |
2388 | to access a non-existent part of the register. */ | |
2389 | if (TREE_CODE (index) == INTEGER_CST | |
2390 | && TYPE_DOMAIN (TREE_TYPE (array)) | |
2391 | && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array)))) | |
2392 | { | |
2393 | if (!c_mark_addressable (array)) | |
2394 | return error_mark_node; | |
2395 | } | |
2396 | ||
2397 | if (pedantic) | |
2398 | { | |
2399 | tree foo = array; | |
2400 | while (TREE_CODE (foo) == COMPONENT_REF) | |
2401 | foo = TREE_OPERAND (foo, 0); | |
2402 | if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo)) | |
2403 | pedwarn (loc, OPT_Wpedantic, | |
2404 | "ISO C forbids subscripting %<register%> array"); | |
2405 | else if (!flag_isoc99 && !lvalue_p (foo)) | |
2406 | pedwarn (loc, OPT_Wpedantic, | |
2407 | "ISO C90 forbids subscripting non-lvalue array"); | |
2408 | } | |
2409 | ||
2410 | type = TREE_TYPE (TREE_TYPE (array)); | |
2411 | rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); | |
2412 | /* Array ref is const/volatile if the array elements are | |
2413 | or if the array is. */ | |
2414 | TREE_READONLY (rval) | |
2415 | |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array))) | |
2416 | | TREE_READONLY (array)); | |
2417 | TREE_SIDE_EFFECTS (rval) | |
2418 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2419 | | TREE_SIDE_EFFECTS (array)); | |
2420 | TREE_THIS_VOLATILE (rval) | |
2421 | |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array))) | |
2422 | /* This was added by rms on 16 Nov 91. | |
2423 | It fixes vol struct foo *a; a->elts[1] | |
2424 | in an inline function. | |
2425 | Hope it doesn't break something else. */ | |
2426 | | TREE_THIS_VOLATILE (array)); | |
2427 | ret = require_complete_type (rval); | |
2428 | protected_set_expr_location (ret, loc); | |
2429 | return ret; | |
2430 | } | |
2431 | else | |
2432 | { | |
2433 | tree ar = default_conversion (array); | |
2434 | ||
2435 | if (ar == error_mark_node) | |
2436 | return ar; | |
2437 | ||
2438 | gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); | |
2439 | gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); | |
2440 | ||
2441 | return build_indirect_ref | |
2442 | (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0), | |
2443 | RO_ARRAY_INDEXING); | |
2444 | } | |
2445 | } | |
2446 | \f | |
2447 | /* Build an external reference to identifier ID. FUN indicates | |
2448 | whether this will be used for a function call. LOC is the source | |
2449 | location of the identifier. This sets *TYPE to the type of the | |
2450 | identifier, which is not the same as the type of the returned value | |
2451 | for CONST_DECLs defined as enum constants. If the type of the | |
2452 | identifier is not available, *TYPE is set to NULL. */ | |
2453 | tree | |
2454 | build_external_ref (location_t loc, tree id, int fun, tree *type) | |
2455 | { | |
2456 | tree ref; | |
2457 | tree decl = lookup_name (id); | |
2458 | ||
2459 | /* In Objective-C, an instance variable (ivar) may be preferred to | |
2460 | whatever lookup_name() found. */ | |
2461 | decl = objc_lookup_ivar (decl, id); | |
2462 | ||
2463 | *type = NULL; | |
2464 | if (decl && decl != error_mark_node) | |
2465 | { | |
2466 | ref = decl; | |
2467 | *type = TREE_TYPE (ref); | |
2468 | } | |
2469 | else if (fun) | |
2470 | /* Implicit function declaration. */ | |
2471 | ref = implicitly_declare (loc, id); | |
2472 | else if (decl == error_mark_node) | |
2473 | /* Don't complain about something that's already been | |
2474 | complained about. */ | |
2475 | return error_mark_node; | |
2476 | else | |
2477 | { | |
2478 | undeclared_variable (loc, id); | |
2479 | return error_mark_node; | |
2480 | } | |
2481 | ||
2482 | if (TREE_TYPE (ref) == error_mark_node) | |
2483 | return error_mark_node; | |
2484 | ||
2485 | if (TREE_DEPRECATED (ref)) | |
2486 | warn_deprecated_use (ref, NULL_TREE); | |
2487 | ||
2488 | /* Recursive call does not count as usage. */ | |
2489 | if (ref != current_function_decl) | |
2490 | { | |
2491 | TREE_USED (ref) = 1; | |
2492 | } | |
2493 | ||
2494 | if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) | |
2495 | { | |
2496 | if (!in_sizeof && !in_typeof) | |
2497 | C_DECL_USED (ref) = 1; | |
2498 | else if (DECL_INITIAL (ref) == 0 | |
2499 | && DECL_EXTERNAL (ref) | |
2500 | && !TREE_PUBLIC (ref)) | |
2501 | record_maybe_used_decl (ref); | |
2502 | } | |
2503 | ||
2504 | if (TREE_CODE (ref) == CONST_DECL) | |
2505 | { | |
2506 | used_types_insert (TREE_TYPE (ref)); | |
2507 | ||
2508 | if (warn_cxx_compat | |
2509 | && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE | |
2510 | && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref))) | |
2511 | { | |
2512 | warning_at (loc, OPT_Wc___compat, | |
2513 | ("enum constant defined in struct or union " | |
2514 | "is not visible in C++")); | |
2515 | inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here"); | |
2516 | } | |
2517 | ||
2518 | ref = DECL_INITIAL (ref); | |
2519 | TREE_CONSTANT (ref) = 1; | |
2520 | } | |
2521 | else if (current_function_decl != 0 | |
2522 | && !DECL_FILE_SCOPE_P (current_function_decl) | |
2523 | && (TREE_CODE (ref) == VAR_DECL | |
2524 | || TREE_CODE (ref) == PARM_DECL | |
2525 | || TREE_CODE (ref) == FUNCTION_DECL)) | |
2526 | { | |
2527 | tree context = decl_function_context (ref); | |
2528 | ||
2529 | if (context != 0 && context != current_function_decl) | |
2530 | DECL_NONLOCAL (ref) = 1; | |
2531 | } | |
2532 | /* C99 6.7.4p3: An inline definition of a function with external | |
2533 | linkage ... shall not contain a reference to an identifier with | |
2534 | internal linkage. */ | |
2535 | else if (current_function_decl != 0 | |
2536 | && DECL_DECLARED_INLINE_P (current_function_decl) | |
2537 | && DECL_EXTERNAL (current_function_decl) | |
2538 | && VAR_OR_FUNCTION_DECL_P (ref) | |
2539 | && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref)) | |
2540 | && ! TREE_PUBLIC (ref) | |
2541 | && DECL_CONTEXT (ref) != current_function_decl) | |
2542 | record_inline_static (loc, current_function_decl, ref, | |
2543 | csi_internal); | |
2544 | ||
2545 | return ref; | |
2546 | } | |
2547 | ||
2548 | /* Record details of decls possibly used inside sizeof or typeof. */ | |
2549 | struct maybe_used_decl | |
2550 | { | |
2551 | /* The decl. */ | |
2552 | tree decl; | |
2553 | /* The level seen at (in_sizeof + in_typeof). */ | |
2554 | int level; | |
2555 | /* The next one at this level or above, or NULL. */ | |
2556 | struct maybe_used_decl *next; | |
2557 | }; | |
2558 | ||
2559 | static struct maybe_used_decl *maybe_used_decls; | |
2560 | ||
2561 | /* Record that DECL, an undefined static function reference seen | |
2562 | inside sizeof or typeof, might be used if the operand of sizeof is | |
2563 | a VLA type or the operand of typeof is a variably modified | |
2564 | type. */ | |
2565 | ||
2566 | static void | |
2567 | record_maybe_used_decl (tree decl) | |
2568 | { | |
2569 | struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); | |
2570 | t->decl = decl; | |
2571 | t->level = in_sizeof + in_typeof; | |
2572 | t->next = maybe_used_decls; | |
2573 | maybe_used_decls = t; | |
2574 | } | |
2575 | ||
2576 | /* Pop the stack of decls possibly used inside sizeof or typeof. If | |
2577 | USED is false, just discard them. If it is true, mark them used | |
2578 | (if no longer inside sizeof or typeof) or move them to the next | |
2579 | level up (if still inside sizeof or typeof). */ | |
2580 | ||
2581 | void | |
2582 | pop_maybe_used (bool used) | |
2583 | { | |
2584 | struct maybe_used_decl *p = maybe_used_decls; | |
2585 | int cur_level = in_sizeof + in_typeof; | |
2586 | while (p && p->level > cur_level) | |
2587 | { | |
2588 | if (used) | |
2589 | { | |
2590 | if (cur_level == 0) | |
2591 | C_DECL_USED (p->decl) = 1; | |
2592 | else | |
2593 | p->level = cur_level; | |
2594 | } | |
2595 | p = p->next; | |
2596 | } | |
2597 | if (!used || cur_level == 0) | |
2598 | maybe_used_decls = p; | |
2599 | } | |
2600 | ||
2601 | /* Return the result of sizeof applied to EXPR. */ | |
2602 | ||
2603 | struct c_expr | |
2604 | c_expr_sizeof_expr (location_t loc, struct c_expr expr) | |
2605 | { | |
2606 | struct c_expr ret; | |
2607 | if (expr.value == error_mark_node) | |
2608 | { | |
2609 | ret.value = error_mark_node; | |
2610 | ret.original_code = ERROR_MARK; | |
2611 | ret.original_type = NULL; | |
2612 | pop_maybe_used (false); | |
2613 | } | |
2614 | else | |
2615 | { | |
2616 | bool expr_const_operands = true; | |
2617 | tree folded_expr = c_fully_fold (expr.value, require_constant_value, | |
2618 | &expr_const_operands); | |
2619 | ret.value = c_sizeof (loc, TREE_TYPE (folded_expr)); | |
2620 | c_last_sizeof_arg = expr.value; | |
2621 | ret.original_code = SIZEOF_EXPR; | |
2622 | ret.original_type = NULL; | |
2623 | if (c_vla_type_p (TREE_TYPE (folded_expr))) | |
2624 | { | |
2625 | /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */ | |
2626 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), | |
2627 | folded_expr, ret.value); | |
2628 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands; | |
2629 | SET_EXPR_LOCATION (ret.value, loc); | |
2630 | } | |
2631 | pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr))); | |
2632 | } | |
2633 | return ret; | |
2634 | } | |
2635 | ||
2636 | /* Return the result of sizeof applied to T, a structure for the type | |
2637 | name passed to sizeof (rather than the type itself). LOC is the | |
2638 | location of the original expression. */ | |
2639 | ||
2640 | struct c_expr | |
2641 | c_expr_sizeof_type (location_t loc, struct c_type_name *t) | |
2642 | { | |
2643 | tree type; | |
2644 | struct c_expr ret; | |
2645 | tree type_expr = NULL_TREE; | |
2646 | bool type_expr_const = true; | |
2647 | type = groktypename (t, &type_expr, &type_expr_const); | |
2648 | ret.value = c_sizeof (loc, type); | |
2649 | c_last_sizeof_arg = type; | |
2650 | ret.original_code = SIZEOF_EXPR; | |
2651 | ret.original_type = NULL; | |
2652 | if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) | |
2653 | && c_vla_type_p (type)) | |
2654 | { | |
2655 | /* If the type is a [*] array, it is a VLA but is represented as | |
2656 | having a size of zero. In such a case we must ensure that | |
2657 | the result of sizeof does not get folded to a constant by | |
2658 | c_fully_fold, because if the size is evaluated the result is | |
2659 | not constant and so constraints on zero or negative size | |
2660 | arrays must not be applied when this sizeof call is inside | |
2661 | another array declarator. */ | |
2662 | if (!type_expr) | |
2663 | type_expr = integer_zero_node; | |
2664 | ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), | |
2665 | type_expr, ret.value); | |
2666 | C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; | |
2667 | } | |
2668 | pop_maybe_used (type != error_mark_node | |
2669 | ? C_TYPE_VARIABLE_SIZE (type) : false); | |
2670 | return ret; | |
2671 | } | |
2672 | ||
2673 | /* Build a function call to function FUNCTION with parameters PARAMS. | |
2674 | The function call is at LOC. | |
2675 | PARAMS is a list--a chain of TREE_LIST nodes--in which the | |
2676 | TREE_VALUE of each node is a parameter-expression. | |
2677 | FUNCTION's data type may be a function type or a pointer-to-function. */ | |
2678 | ||
2679 | tree | |
2680 | build_function_call (location_t loc, tree function, tree params) | |
2681 | { | |
2682 | vec<tree, va_gc> *v; | |
2683 | tree ret; | |
2684 | ||
2685 | vec_alloc (v, list_length (params)); | |
2686 | for (; params; params = TREE_CHAIN (params)) | |
2687 | v->quick_push (TREE_VALUE (params)); | |
2688 | ret = build_function_call_vec (loc, function, v, NULL); | |
2689 | vec_free (v); | |
2690 | return ret; | |
2691 | } | |
2692 | ||
2693 | /* Give a note about the location of the declaration of DECL. */ | |
2694 | ||
2695 | static void inform_declaration (tree decl) | |
2696 | { | |
2697 | if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl))) | |
2698 | inform (DECL_SOURCE_LOCATION (decl), "declared here"); | |
2699 | } | |
2700 | ||
2701 | /* Build a function call to function FUNCTION with parameters PARAMS. | |
2702 | ORIGTYPES, if not NULL, is a vector of types; each element is | |
2703 | either NULL or the original type of the corresponding element in | |
2704 | PARAMS. The original type may differ from TREE_TYPE of the | |
2705 | parameter for enums. FUNCTION's data type may be a function type | |
2706 | or pointer-to-function. This function changes the elements of | |
2707 | PARAMS. */ | |
2708 | ||
2709 | tree | |
2710 | build_function_call_vec (location_t loc, tree function, | |
2711 | vec<tree, va_gc> *params, | |
2712 | vec<tree, va_gc> *origtypes) | |
2713 | { | |
2714 | tree fntype, fundecl = 0; | |
2715 | tree name = NULL_TREE, result; | |
2716 | tree tem; | |
2717 | int nargs; | |
2718 | tree *argarray; | |
2719 | ||
2720 | ||
2721 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
2722 | STRIP_TYPE_NOPS (function); | |
2723 | ||
2724 | /* Convert anything with function type to a pointer-to-function. */ | |
2725 | if (TREE_CODE (function) == FUNCTION_DECL) | |
2726 | { | |
2727 | /* Implement type-directed function overloading for builtins. | |
2728 | resolve_overloaded_builtin and targetm.resolve_overloaded_builtin | |
2729 | handle all the type checking. The result is a complete expression | |
2730 | that implements this function call. */ | |
2731 | tem = resolve_overloaded_builtin (loc, function, params); | |
2732 | if (tem) | |
2733 | return tem; | |
2734 | ||
2735 | name = DECL_NAME (function); | |
2736 | ||
2737 | if (flag_tm) | |
2738 | tm_malloc_replacement (function); | |
2739 | fundecl = function; | |
2740 | /* Atomic functions have type checking/casting already done. They are | |
2741 | often rewritten and don't match the original parameter list. */ | |
2742 | if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) | |
2743 | origtypes = NULL; | |
2744 | ||
2745 | if (flag_enable_cilkplus | |
2746 | && is_cilkplus_reduce_builtin (function)) | |
2747 | origtypes = NULL; | |
2748 | } | |
2749 | if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE) | |
2750 | function = function_to_pointer_conversion (loc, function); | |
2751 | ||
2752 | /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF | |
2753 | expressions, like those used for ObjC messenger dispatches. */ | |
2754 | if (params && !params->is_empty ()) | |
2755 | function = objc_rewrite_function_call (function, (*params)[0]); | |
2756 | ||
2757 | function = c_fully_fold (function, false, NULL); | |
2758 | ||
2759 | fntype = TREE_TYPE (function); | |
2760 | ||
2761 | if (TREE_CODE (fntype) == ERROR_MARK) | |
2762 | return error_mark_node; | |
2763 | ||
2764 | if (!(TREE_CODE (fntype) == POINTER_TYPE | |
2765 | && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)) | |
2766 | { | |
2767 | if (!flag_diagnostics_show_caret) | |
2768 | error_at (loc, | |
2769 | "called object %qE is not a function or function pointer", | |
2770 | function); | |
2771 | else if (DECL_P (function)) | |
2772 | { | |
2773 | error_at (loc, | |
2774 | "called object %qD is not a function or function pointer", | |
2775 | function); | |
2776 | inform_declaration (function); | |
2777 | } | |
2778 | else | |
2779 | error_at (loc, | |
2780 | "called object is not a function or function pointer"); | |
2781 | return error_mark_node; | |
2782 | } | |
2783 | ||
2784 | if (fundecl && TREE_THIS_VOLATILE (fundecl)) | |
2785 | current_function_returns_abnormally = 1; | |
2786 | ||
2787 | /* fntype now gets the type of function pointed to. */ | |
2788 | fntype = TREE_TYPE (fntype); | |
2789 | ||
2790 | /* Convert the parameters to the types declared in the | |
2791 | function prototype, or apply default promotions. */ | |
2792 | ||
2793 | nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes, | |
2794 | function, fundecl); | |
2795 | if (nargs < 0) | |
2796 | return error_mark_node; | |
2797 | ||
2798 | /* Check that the function is called through a compatible prototype. | |
2799 | If it is not, replace the call by a trap, wrapped up in a compound | |
2800 | expression if necessary. This has the nice side-effect to prevent | |
2801 | the tree-inliner from generating invalid assignment trees which may | |
2802 | blow up in the RTL expander later. */ | |
2803 | if (CONVERT_EXPR_P (function) | |
2804 | && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR | |
2805 | && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL | |
2806 | && !comptypes (fntype, TREE_TYPE (tem))) | |
2807 | { | |
2808 | tree return_type = TREE_TYPE (fntype); | |
2809 | tree trap = build_function_call (loc, | |
2810 | builtin_decl_explicit (BUILT_IN_TRAP), | |
2811 | NULL_TREE); | |
2812 | int i; | |
2813 | ||
2814 | /* This situation leads to run-time undefined behavior. We can't, | |
2815 | therefore, simply error unless we can prove that all possible | |
2816 | executions of the program must execute the code. */ | |
2817 | if (warning_at (loc, 0, "function called through a non-compatible type")) | |
2818 | /* We can, however, treat "undefined" any way we please. | |
2819 | Call abort to encourage the user to fix the program. */ | |
2820 | inform (loc, "if this code is reached, the program will abort"); | |
2821 | /* Before the abort, allow the function arguments to exit or | |
2822 | call longjmp. */ | |
2823 | for (i = 0; i < nargs; i++) | |
2824 | trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap); | |
2825 | ||
2826 | if (VOID_TYPE_P (return_type)) | |
2827 | { | |
2828 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED) | |
2829 | pedwarn (loc, 0, | |
2830 | "function with qualified void return type called"); | |
2831 | return trap; | |
2832 | } | |
2833 | else | |
2834 | { | |
2835 | tree rhs; | |
2836 | ||
2837 | if (AGGREGATE_TYPE_P (return_type)) | |
2838 | rhs = build_compound_literal (loc, return_type, | |
2839 | build_constructor (return_type, | |
2840 | NULL), | |
2841 | false); | |
2842 | else | |
2843 | rhs = build_zero_cst (return_type); | |
2844 | ||
2845 | return require_complete_type (build2 (COMPOUND_EXPR, return_type, | |
2846 | trap, rhs)); | |
2847 | } | |
2848 | } | |
2849 | ||
2850 | argarray = vec_safe_address (params); | |
2851 | ||
2852 | /* Check that arguments to builtin functions match the expectations. */ | |
2853 | if (fundecl | |
2854 | && DECL_BUILT_IN (fundecl) | |
2855 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL | |
2856 | && !check_builtin_function_arguments (fundecl, nargs, argarray)) | |
2857 | return error_mark_node; | |
2858 | ||
2859 | /* Check that the arguments to the function are valid. */ | |
2860 | check_function_arguments (fntype, nargs, argarray); | |
2861 | ||
2862 | if (name != NULL_TREE | |
2863 | && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10)) | |
2864 | { | |
2865 | if (require_constant_value) | |
2866 | result = | |
2867 | fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype), | |
2868 | function, nargs, argarray); | |
2869 | else | |
2870 | result = fold_build_call_array_loc (loc, TREE_TYPE (fntype), | |
2871 | function, nargs, argarray); | |
2872 | if (TREE_CODE (result) == NOP_EXPR | |
2873 | && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST) | |
2874 | STRIP_TYPE_NOPS (result); | |
2875 | } | |
2876 | else | |
2877 | result = build_call_array_loc (loc, TREE_TYPE (fntype), | |
2878 | function, nargs, argarray); | |
2879 | ||
2880 | if (VOID_TYPE_P (TREE_TYPE (result))) | |
2881 | { | |
2882 | if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED) | |
2883 | pedwarn (loc, 0, | |
2884 | "function with qualified void return type called"); | |
2885 | return result; | |
2886 | } | |
2887 | return require_complete_type (result); | |
2888 | } | |
2889 | \f | |
2890 | /* Convert the argument expressions in the vector VALUES | |
2891 | to the types in the list TYPELIST. | |
2892 | ||
2893 | If TYPELIST is exhausted, or when an element has NULL as its type, | |
2894 | perform the default conversions. | |
2895 | ||
2896 | ORIGTYPES is the original types of the expressions in VALUES. This | |
2897 | holds the type of enum values which have been converted to integral | |
2898 | types. It may be NULL. | |
2899 | ||
2900 | FUNCTION is a tree for the called function. It is used only for | |
2901 | error messages, where it is formatted with %qE. | |
2902 | ||
2903 | This is also where warnings about wrong number of args are generated. | |
2904 | ||
2905 | Returns the actual number of arguments processed (which may be less | |
2906 | than the length of VALUES in some error situations), or -1 on | |
2907 | failure. */ | |
2908 | ||
2909 | static int | |
2910 | convert_arguments (tree typelist, vec<tree, va_gc> *values, | |
2911 | vec<tree, va_gc> *origtypes, tree function, tree fundecl) | |
2912 | { | |
2913 | tree typetail, val; | |
2914 | unsigned int parmnum; | |
2915 | bool error_args = false; | |
2916 | const bool type_generic = fundecl | |
2917 | && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl))); | |
2918 | bool type_generic_remove_excess_precision = false; | |
2919 | tree selector; | |
2920 | ||
2921 | /* Change pointer to function to the function itself for | |
2922 | diagnostics. */ | |
2923 | if (TREE_CODE (function) == ADDR_EXPR | |
2924 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
2925 | function = TREE_OPERAND (function, 0); | |
2926 | ||
2927 | /* Handle an ObjC selector specially for diagnostics. */ | |
2928 | selector = objc_message_selector (); | |
2929 | ||
2930 | /* For type-generic built-in functions, determine whether excess | |
2931 | precision should be removed (classification) or not | |
2932 | (comparison). */ | |
2933 | if (type_generic | |
2934 | && DECL_BUILT_IN (fundecl) | |
2935 | && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL) | |
2936 | { | |
2937 | switch (DECL_FUNCTION_CODE (fundecl)) | |
2938 | { | |
2939 | case BUILT_IN_ISFINITE: | |
2940 | case BUILT_IN_ISINF: | |
2941 | case BUILT_IN_ISINF_SIGN: | |
2942 | case BUILT_IN_ISNAN: | |
2943 | case BUILT_IN_ISNORMAL: | |
2944 | case BUILT_IN_FPCLASSIFY: | |
2945 | type_generic_remove_excess_precision = true; | |
2946 | break; | |
2947 | ||
2948 | default: | |
2949 | type_generic_remove_excess_precision = false; | |
2950 | break; | |
2951 | } | |
2952 | } | |
2953 | if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl)) | |
2954 | return vec_safe_length (values); | |
2955 | ||
2956 | /* Scan the given expressions and types, producing individual | |
2957 | converted arguments. */ | |
2958 | ||
2959 | for (typetail = typelist, parmnum = 0; | |
2960 | values && values->iterate (parmnum, &val); | |
2961 | ++parmnum) | |
2962 | { | |
2963 | tree type = typetail ? TREE_VALUE (typetail) : 0; | |
2964 | tree valtype = TREE_TYPE (val); | |
2965 | tree rname = function; | |
2966 | int argnum = parmnum + 1; | |
2967 | const char *invalid_func_diag; | |
2968 | bool excess_precision = false; | |
2969 | bool npc; | |
2970 | tree parmval; | |
2971 | ||
2972 | if (type == void_type_node) | |
2973 | { | |
2974 | if (selector) | |
2975 | error_at (input_location, | |
2976 | "too many arguments to method %qE", selector); | |
2977 | else | |
2978 | error_at (input_location, | |
2979 | "too many arguments to function %qE", function); | |
2980 | inform_declaration (fundecl); | |
2981 | return parmnum; | |
2982 | } | |
2983 | ||
2984 | if (selector && argnum > 2) | |
2985 | { | |
2986 | rname = selector; | |
2987 | argnum -= 2; | |
2988 | } | |
2989 | ||
2990 | npc = null_pointer_constant_p (val); | |
2991 | ||
2992 | /* If there is excess precision and a prototype, convert once to | |
2993 | the required type rather than converting via the semantic | |
2994 | type. Likewise without a prototype a float value represented | |
2995 | as long double should be converted once to double. But for | |
2996 | type-generic classification functions excess precision must | |
2997 | be removed here. */ | |
2998 | if (TREE_CODE (val) == EXCESS_PRECISION_EXPR | |
2999 | && (type || !type_generic || !type_generic_remove_excess_precision)) | |
3000 | { | |
3001 | val = TREE_OPERAND (val, 0); | |
3002 | excess_precision = true; | |
3003 | } | |
3004 | val = c_fully_fold (val, false, NULL); | |
3005 | STRIP_TYPE_NOPS (val); | |
3006 | ||
3007 | val = require_complete_type (val); | |
3008 | ||
3009 | if (type != 0) | |
3010 | { | |
3011 | /* Formal parm type is specified by a function prototype. */ | |
3012 | ||
3013 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) | |
3014 | { | |
3015 | error ("type of formal parameter %d is incomplete", parmnum + 1); | |
3016 | parmval = val; | |
3017 | } | |
3018 | else | |
3019 | { | |
3020 | tree origtype; | |
3021 | ||
3022 | /* Optionally warn about conversions that | |
3023 | differ from the default conversions. */ | |
3024 | if (warn_traditional_conversion || warn_traditional) | |
3025 | { | |
3026 | unsigned int formal_prec = TYPE_PRECISION (type); | |
3027 | ||
3028 | if (INTEGRAL_TYPE_P (type) | |
3029 | && TREE_CODE (valtype) == REAL_TYPE) | |
3030 | warning (0, "passing argument %d of %qE as integer " | |
3031 | "rather than floating due to prototype", | |
3032 | argnum, rname); | |
3033 | if (INTEGRAL_TYPE_P (type) | |
3034 | && TREE_CODE (valtype) == COMPLEX_TYPE) | |
3035 | warning (0, "passing argument %d of %qE as integer " | |
3036 | "rather than complex due to prototype", | |
3037 | argnum, rname); | |
3038 | else if (TREE_CODE (type) == COMPLEX_TYPE | |
3039 | && TREE_CODE (valtype) == REAL_TYPE) | |
3040 | warning (0, "passing argument %d of %qE as complex " | |
3041 | "rather than floating due to prototype", | |
3042 | argnum, rname); | |
3043 | else if (TREE_CODE (type) == REAL_TYPE | |
3044 | && INTEGRAL_TYPE_P (valtype)) | |
3045 | warning (0, "passing argument %d of %qE as floating " | |
3046 | "rather than integer due to prototype", | |
3047 | argnum, rname); | |
3048 | else if (TREE_CODE (type) == COMPLEX_TYPE | |
3049 | && INTEGRAL_TYPE_P (valtype)) | |
3050 | warning (0, "passing argument %d of %qE as complex " | |
3051 | "rather than integer due to prototype", | |
3052 | argnum, rname); | |
3053 | else if (TREE_CODE (type) == REAL_TYPE | |
3054 | && TREE_CODE (valtype) == COMPLEX_TYPE) | |
3055 | warning (0, "passing argument %d of %qE as floating " | |
3056 | "rather than complex due to prototype", | |
3057 | argnum, rname); | |
3058 | /* ??? At some point, messages should be written about | |
3059 | conversions between complex types, but that's too messy | |
3060 | to do now. */ | |
3061 | else if (TREE_CODE (type) == REAL_TYPE | |
3062 | && TREE_CODE (valtype) == REAL_TYPE) | |
3063 | { | |
3064 | /* Warn if any argument is passed as `float', | |
3065 | since without a prototype it would be `double'. */ | |
3066 | if (formal_prec == TYPE_PRECISION (float_type_node) | |
3067 | && type != dfloat32_type_node) | |
3068 | warning (0, "passing argument %d of %qE as %<float%> " | |
3069 | "rather than %<double%> due to prototype", | |
3070 | argnum, rname); | |
3071 | ||
3072 | /* Warn if mismatch between argument and prototype | |
3073 | for decimal float types. Warn of conversions with | |
3074 | binary float types and of precision narrowing due to | |
3075 | prototype. */ | |
3076 | else if (type != valtype | |
3077 | && (type == dfloat32_type_node | |
3078 | || type == dfloat64_type_node | |
3079 | || type == dfloat128_type_node | |
3080 | || valtype == dfloat32_type_node | |
3081 | || valtype == dfloat64_type_node | |
3082 | || valtype == dfloat128_type_node) | |
3083 | && (formal_prec | |
3084 | <= TYPE_PRECISION (valtype) | |
3085 | || (type == dfloat128_type_node | |
3086 | && (valtype | |
3087 | != dfloat64_type_node | |
3088 | && (valtype | |
3089 | != dfloat32_type_node))) | |
3090 | || (type == dfloat64_type_node | |
3091 | && (valtype | |
3092 | != dfloat32_type_node)))) | |
3093 | warning (0, "passing argument %d of %qE as %qT " | |
3094 | "rather than %qT due to prototype", | |
3095 | argnum, rname, type, valtype); | |
3096 | ||
3097 | } | |
3098 | /* Detect integer changing in width or signedness. | |
3099 | These warnings are only activated with | |
3100 | -Wtraditional-conversion, not with -Wtraditional. */ | |
3101 | else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type) | |
3102 | && INTEGRAL_TYPE_P (valtype)) | |
3103 | { | |
3104 | tree would_have_been = default_conversion (val); | |
3105 | tree type1 = TREE_TYPE (would_have_been); | |
3106 | ||
3107 | if (TREE_CODE (type) == ENUMERAL_TYPE | |
3108 | && (TYPE_MAIN_VARIANT (type) | |
3109 | == TYPE_MAIN_VARIANT (valtype))) | |
3110 | /* No warning if function asks for enum | |
3111 | and the actual arg is that enum type. */ | |
3112 | ; | |
3113 | else if (formal_prec != TYPE_PRECISION (type1)) | |
3114 | warning (OPT_Wtraditional_conversion, | |
3115 | "passing argument %d of %qE " | |
3116 | "with different width due to prototype", | |
3117 | argnum, rname); | |
3118 | else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1)) | |
3119 | ; | |
3120 | /* Don't complain if the formal parameter type | |
3121 | is an enum, because we can't tell now whether | |
3122 | the value was an enum--even the same enum. */ | |
3123 | else if (TREE_CODE (type) == ENUMERAL_TYPE) | |
3124 | ; | |
3125 | else if (TREE_CODE (val) == INTEGER_CST | |
3126 | && int_fits_type_p (val, type)) | |
3127 | /* Change in signedness doesn't matter | |
3128 | if a constant value is unaffected. */ | |
3129 | ; | |
3130 | /* If the value is extended from a narrower | |
3131 | unsigned type, it doesn't matter whether we | |
3132 | pass it as signed or unsigned; the value | |
3133 | certainly is the same either way. */ | |
3134 | else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type) | |
3135 | && TYPE_UNSIGNED (valtype)) | |
3136 | ; | |
3137 | else if (TYPE_UNSIGNED (type)) | |
3138 | warning (OPT_Wtraditional_conversion, | |
3139 | "passing argument %d of %qE " | |
3140 | "as unsigned due to prototype", | |
3141 | argnum, rname); | |
3142 | else | |
3143 | warning (OPT_Wtraditional_conversion, | |
3144 | "passing argument %d of %qE " | |
3145 | "as signed due to prototype", argnum, rname); | |
3146 | } | |
3147 | } | |
3148 | ||
3149 | /* Possibly restore an EXCESS_PRECISION_EXPR for the | |
3150 | sake of better warnings from convert_and_check. */ | |
3151 | if (excess_precision) | |
3152 | val = build1 (EXCESS_PRECISION_EXPR, valtype, val); | |
3153 | origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum]; | |
3154 | parmval = convert_for_assignment (input_location, type, val, | |
3155 | origtype, ic_argpass, npc, | |
3156 | fundecl, function, | |
3157 | parmnum + 1); | |
3158 | ||
3159 | if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0) | |
3160 | && INTEGRAL_TYPE_P (type) | |
3161 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))) | |
3162 | parmval = default_conversion (parmval); | |
3163 | } | |
3164 | } | |
3165 | else if (TREE_CODE (valtype) == REAL_TYPE | |
3166 | && (TYPE_PRECISION (valtype) | |
3167 | <= TYPE_PRECISION (double_type_node)) | |
3168 | && TYPE_MAIN_VARIANT (valtype) != double_type_node | |
3169 | && TYPE_MAIN_VARIANT (valtype) != long_double_type_node | |
3170 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype))) | |
3171 | { | |
3172 | if (type_generic) | |
3173 | parmval = val; | |
3174 | else | |
3175 | { | |
3176 | /* Convert `float' to `double'. */ | |
3177 | if (warn_double_promotion && !c_inhibit_evaluation_warnings) | |
3178 | warning (OPT_Wdouble_promotion, | |
3179 | "implicit conversion from %qT to %qT when passing " | |
3180 | "argument to function", | |
3181 | valtype, double_type_node); | |
3182 | parmval = convert (double_type_node, val); | |
3183 | } | |
3184 | } | |
3185 | else if (excess_precision && !type_generic) | |
3186 | /* A "double" argument with excess precision being passed | |
3187 | without a prototype or in variable arguments. */ | |
3188 | parmval = convert (valtype, val); | |
3189 | else if ((invalid_func_diag = | |
3190 | targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val))) | |
3191 | { | |
3192 | error (invalid_func_diag); | |
3193 | return -1; | |
3194 | } | |
3195 | else | |
3196 | /* Convert `short' and `char' to full-size `int'. */ | |
3197 | parmval = default_conversion (val); | |
3198 | ||
3199 | (*values)[parmnum] = parmval; | |
3200 | if (parmval == error_mark_node) | |
3201 | error_args = true; | |
3202 | ||
3203 | if (typetail) | |
3204 | typetail = TREE_CHAIN (typetail); | |
3205 | } | |
3206 | ||
3207 | gcc_assert (parmnum == vec_safe_length (values)); | |
3208 | ||
3209 | if (typetail != 0 && TREE_VALUE (typetail) != void_type_node) | |
3210 | { | |
3211 | error_at (input_location, | |
3212 | "too few arguments to function %qE", function); | |
3213 | inform_declaration (fundecl); | |
3214 | return -1; | |
3215 | } | |
3216 | ||
3217 | return error_args ? -1 : (int) parmnum; | |
3218 | } | |
3219 | \f | |
3220 | /* This is the entry point used by the parser to build unary operators | |
3221 | in the input. CODE, a tree_code, specifies the unary operator, and | |
3222 | ARG is the operand. For unary plus, the C parser currently uses | |
3223 | CONVERT_EXPR for code. | |
3224 | ||
3225 | LOC is the location to use for the tree generated. | |
3226 | */ | |
3227 | ||
3228 | struct c_expr | |
3229 | parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg) | |
3230 | { | |
3231 | struct c_expr result; | |
3232 | ||
3233 | result.value = build_unary_op (loc, code, arg.value, 0); | |
3234 | result.original_code = code; | |
3235 | result.original_type = NULL; | |
3236 | ||
3237 | if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value)) | |
3238 | overflow_warning (loc, result.value); | |
3239 | ||
3240 | return result; | |
3241 | } | |
3242 | ||
3243 | /* This is the entry point used by the parser to build binary operators | |
3244 | in the input. CODE, a tree_code, specifies the binary operator, and | |
3245 | ARG1 and ARG2 are the operands. In addition to constructing the | |
3246 | expression, we check for operands that were written with other binary | |
3247 | operators in a way that is likely to confuse the user. | |
3248 | ||
3249 | LOCATION is the location of the binary operator. */ | |
3250 | ||
3251 | struct c_expr | |
3252 | parser_build_binary_op (location_t location, enum tree_code code, | |
3253 | struct c_expr arg1, struct c_expr arg2) | |
3254 | { | |
3255 | struct c_expr result; | |
3256 | ||
3257 | enum tree_code code1 = arg1.original_code; | |
3258 | enum tree_code code2 = arg2.original_code; | |
3259 | tree type1 = (arg1.original_type | |
3260 | ? arg1.original_type | |
3261 | : TREE_TYPE (arg1.value)); | |
3262 | tree type2 = (arg2.original_type | |
3263 | ? arg2.original_type | |
3264 | : TREE_TYPE (arg2.value)); | |
3265 | ||
3266 | result.value = build_binary_op (location, code, | |
3267 | arg1.value, arg2.value, 1); | |
3268 | result.original_code = code; | |
3269 | result.original_type = NULL; | |
3270 | ||
3271 | if (TREE_CODE (result.value) == ERROR_MARK) | |
3272 | return result; | |
3273 | ||
3274 | if (location != UNKNOWN_LOCATION) | |
3275 | protected_set_expr_location (result.value, location); | |
3276 | ||
3277 | /* Check for cases such as x+y<<z which users are likely | |
3278 | to misinterpret. */ | |
3279 | if (warn_parentheses) | |
3280 | warn_about_parentheses (input_location, code, | |
3281 | code1, arg1.value, code2, arg2.value); | |
3282 | ||
3283 | if (warn_logical_op) | |
3284 | warn_logical_operator (input_location, code, TREE_TYPE (result.value), | |
3285 | code1, arg1.value, code2, arg2.value); | |
3286 | ||
3287 | /* Warn about comparisons against string literals, with the exception | |
3288 | of testing for equality or inequality of a string literal with NULL. */ | |
3289 | if (code == EQ_EXPR || code == NE_EXPR) | |
3290 | { | |
3291 | if ((code1 == STRING_CST && !integer_zerop (arg2.value)) | |
3292 | || (code2 == STRING_CST && !integer_zerop (arg1.value))) | |
3293 | warning_at (location, OPT_Waddress, | |
3294 | "comparison with string literal results in unspecified behavior"); | |
3295 | } | |
3296 | else if (TREE_CODE_CLASS (code) == tcc_comparison | |
3297 | && (code1 == STRING_CST || code2 == STRING_CST)) | |
3298 | warning_at (location, OPT_Waddress, | |
3299 | "comparison with string literal results in unspecified behavior"); | |
3300 | ||
3301 | if (TREE_OVERFLOW_P (result.value) | |
3302 | && !TREE_OVERFLOW_P (arg1.value) | |
3303 | && !TREE_OVERFLOW_P (arg2.value)) | |
3304 | overflow_warning (location, result.value); | |
3305 | ||
3306 | /* Warn about comparisons of different enum types. */ | |
3307 | if (warn_enum_compare | |
3308 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3309 | && TREE_CODE (type1) == ENUMERAL_TYPE | |
3310 | && TREE_CODE (type2) == ENUMERAL_TYPE | |
3311 | && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2)) | |
3312 | warning_at (location, OPT_Wenum_compare, | |
3313 | "comparison between %qT and %qT", | |
3314 | type1, type2); | |
3315 | ||
3316 | return result; | |
3317 | } | |
3318 | \f | |
3319 | /* Return a tree for the difference of pointers OP0 and OP1. | |
3320 | The resulting tree has type int. */ | |
3321 | ||
3322 | static tree | |
3323 | pointer_diff (location_t loc, tree op0, tree op1) | |
3324 | { | |
3325 | tree restype = ptrdiff_type_node; | |
3326 | tree result, inttype; | |
3327 | ||
3328 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0))); | |
3329 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1))); | |
3330 | tree target_type = TREE_TYPE (TREE_TYPE (op0)); | |
3331 | tree con0, con1, lit0, lit1; | |
3332 | tree orig_op1 = op1; | |
3333 | ||
3334 | /* If the operands point into different address spaces, we need to | |
3335 | explicitly convert them to pointers into the common address space | |
3336 | before we can subtract the numerical address values. */ | |
3337 | if (as0 != as1) | |
3338 | { | |
3339 | addr_space_t as_common; | |
3340 | tree common_type; | |
3341 | ||
3342 | /* Determine the common superset address space. This is guaranteed | |
3343 | to exist because the caller verified that comp_target_types | |
3344 | returned non-zero. */ | |
3345 | if (!addr_space_superset (as0, as1, &as_common)) | |
3346 | gcc_unreachable (); | |
3347 | ||
3348 | common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1)); | |
3349 | op0 = convert (common_type, op0); | |
3350 | op1 = convert (common_type, op1); | |
3351 | } | |
3352 | ||
3353 | /* Determine integer type to perform computations in. This will usually | |
3354 | be the same as the result type (ptrdiff_t), but may need to be a wider | |
3355 | type if pointers for the address space are wider than ptrdiff_t. */ | |
3356 | if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0))) | |
3357 | inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0); | |
3358 | else | |
3359 | inttype = restype; | |
3360 | ||
3361 | ||
3362 | if (TREE_CODE (target_type) == VOID_TYPE) | |
3363 | pedwarn (loc, OPT_Wpointer_arith, | |
3364 | "pointer of type %<void *%> used in subtraction"); | |
3365 | if (TREE_CODE (target_type) == FUNCTION_TYPE) | |
3366 | pedwarn (loc, OPT_Wpointer_arith, | |
3367 | "pointer to a function used in subtraction"); | |
3368 | ||
3369 | /* If the conversion to ptrdiff_type does anything like widening or | |
3370 | converting a partial to an integral mode, we get a convert_expression | |
3371 | that is in the way to do any simplifications. | |
3372 | (fold-const.c doesn't know that the extra bits won't be needed. | |
3373 | split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a | |
3374 | different mode in place.) | |
3375 | So first try to find a common term here 'by hand'; we want to cover | |
3376 | at least the cases that occur in legal static initializers. */ | |
3377 | if (CONVERT_EXPR_P (op0) | |
3378 | && (TYPE_PRECISION (TREE_TYPE (op0)) | |
3379 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))) | |
3380 | con0 = TREE_OPERAND (op0, 0); | |
3381 | else | |
3382 | con0 = op0; | |
3383 | if (CONVERT_EXPR_P (op1) | |
3384 | && (TYPE_PRECISION (TREE_TYPE (op1)) | |
3385 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))) | |
3386 | con1 = TREE_OPERAND (op1, 0); | |
3387 | else | |
3388 | con1 = op1; | |
3389 | ||
3390 | if (TREE_CODE (con0) == POINTER_PLUS_EXPR) | |
3391 | { | |
3392 | lit0 = TREE_OPERAND (con0, 1); | |
3393 | con0 = TREE_OPERAND (con0, 0); | |
3394 | } | |
3395 | else | |
3396 | lit0 = integer_zero_node; | |
3397 | ||
3398 | if (TREE_CODE (con1) == POINTER_PLUS_EXPR) | |
3399 | { | |
3400 | lit1 = TREE_OPERAND (con1, 1); | |
3401 | con1 = TREE_OPERAND (con1, 0); | |
3402 | } | |
3403 | else | |
3404 | lit1 = integer_zero_node; | |
3405 | ||
3406 | if (operand_equal_p (con0, con1, 0)) | |
3407 | { | |
3408 | op0 = lit0; | |
3409 | op1 = lit1; | |
3410 | } | |
3411 | ||
3412 | ||
3413 | /* First do the subtraction as integers; | |
3414 | then drop through to build the divide operator. | |
3415 | Do not do default conversions on the minus operator | |
3416 | in case restype is a short type. */ | |
3417 | ||
3418 | op0 = build_binary_op (loc, | |
3419 | MINUS_EXPR, convert (inttype, op0), | |
3420 | convert (inttype, op1), 0); | |
3421 | /* This generates an error if op1 is pointer to incomplete type. */ | |
3422 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) | |
3423 | error_at (loc, "arithmetic on pointer to an incomplete type"); | |
3424 | ||
3425 | /* This generates an error if op0 is pointer to incomplete type. */ | |
3426 | op1 = c_size_in_bytes (target_type); | |
3427 | ||
3428 | /* Divide by the size, in easiest possible way. */ | |
3429 | result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype, | |
3430 | op0, convert (inttype, op1)); | |
3431 | ||
3432 | /* Convert to final result type if necessary. */ | |
3433 | return convert (restype, result); | |
3434 | } | |
3435 | \f | |
3436 | /* Construct and perhaps optimize a tree representation | |
3437 | for a unary operation. CODE, a tree_code, specifies the operation | |
3438 | and XARG is the operand. | |
3439 | For any CODE other than ADDR_EXPR, FLAG nonzero suppresses | |
3440 | the default promotions (such as from short to int). | |
3441 | For ADDR_EXPR, the default promotions are not applied; FLAG nonzero | |
3442 | allows non-lvalues; this is only used to handle conversion of non-lvalue | |
3443 | arrays to pointers in C99. | |
3444 | ||
3445 | LOCATION is the location of the operator. */ | |
3446 | ||
3447 | tree | |
3448 | build_unary_op (location_t location, | |
3449 | enum tree_code code, tree xarg, int flag) | |
3450 | { | |
3451 | /* No default_conversion here. It causes trouble for ADDR_EXPR. */ | |
3452 | tree arg = xarg; | |
3453 | tree argtype = 0; | |
3454 | enum tree_code typecode; | |
3455 | tree val; | |
3456 | tree ret = error_mark_node; | |
3457 | tree eptype = NULL_TREE; | |
3458 | int noconvert = flag; | |
3459 | const char *invalid_op_diag; | |
3460 | bool int_operands; | |
3461 | ||
3462 | int_operands = EXPR_INT_CONST_OPERANDS (xarg); | |
3463 | if (int_operands) | |
3464 | arg = remove_c_maybe_const_expr (arg); | |
3465 | ||
3466 | if (code != ADDR_EXPR) | |
3467 | arg = require_complete_type (arg); | |
3468 | ||
3469 | typecode = TREE_CODE (TREE_TYPE (arg)); | |
3470 | if (typecode == ERROR_MARK) | |
3471 | return error_mark_node; | |
3472 | if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE) | |
3473 | typecode = INTEGER_TYPE; | |
3474 | ||
3475 | if ((invalid_op_diag | |
3476 | = targetm.invalid_unary_op (code, TREE_TYPE (xarg)))) | |
3477 | { | |
3478 | error_at (location, invalid_op_diag); | |
3479 | return error_mark_node; | |
3480 | } | |
3481 | ||
3482 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) | |
3483 | { | |
3484 | eptype = TREE_TYPE (arg); | |
3485 | arg = TREE_OPERAND (arg, 0); | |
3486 | } | |
3487 | ||
3488 | switch (code) | |
3489 | { | |
3490 | case CONVERT_EXPR: | |
3491 | /* This is used for unary plus, because a CONVERT_EXPR | |
3492 | is enough to prevent anybody from looking inside for | |
3493 | associativity, but won't generate any code. */ | |
3494 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3495 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE | |
3496 | || typecode == VECTOR_TYPE)) | |
3497 | { | |
3498 | error_at (location, "wrong type argument to unary plus"); | |
3499 | return error_mark_node; | |
3500 | } | |
3501 | else if (!noconvert) | |
3502 | arg = default_conversion (arg); | |
3503 | arg = non_lvalue_loc (location, arg); | |
3504 | break; | |
3505 | ||
3506 | case NEGATE_EXPR: | |
3507 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3508 | || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE | |
3509 | || typecode == VECTOR_TYPE)) | |
3510 | { | |
3511 | error_at (location, "wrong type argument to unary minus"); | |
3512 | return error_mark_node; | |
3513 | } | |
3514 | else if (!noconvert) | |
3515 | arg = default_conversion (arg); | |
3516 | break; | |
3517 | ||
3518 | case BIT_NOT_EXPR: | |
3519 | /* ~ works on integer types and non float vectors. */ | |
3520 | if (typecode == INTEGER_TYPE | |
3521 | || (typecode == VECTOR_TYPE | |
3522 | && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg)))) | |
3523 | { | |
3524 | if (!noconvert) | |
3525 | arg = default_conversion (arg); | |
3526 | } | |
3527 | else if (typecode == COMPLEX_TYPE) | |
3528 | { | |
3529 | code = CONJ_EXPR; | |
3530 | pedwarn (location, OPT_Wpedantic, | |
3531 | "ISO C does not support %<~%> for complex conjugation"); | |
3532 | if (!noconvert) | |
3533 | arg = default_conversion (arg); | |
3534 | } | |
3535 | else | |
3536 | { | |
3537 | error_at (location, "wrong type argument to bit-complement"); | |
3538 | return error_mark_node; | |
3539 | } | |
3540 | break; | |
3541 | ||
3542 | case ABS_EXPR: | |
3543 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE)) | |
3544 | { | |
3545 | error_at (location, "wrong type argument to abs"); | |
3546 | return error_mark_node; | |
3547 | } | |
3548 | else if (!noconvert) | |
3549 | arg = default_conversion (arg); | |
3550 | break; | |
3551 | ||
3552 | case CONJ_EXPR: | |
3553 | /* Conjugating a real value is a no-op, but allow it anyway. */ | |
3554 | if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE | |
3555 | || typecode == COMPLEX_TYPE)) | |
3556 | { | |
3557 | error_at (location, "wrong type argument to conjugation"); | |
3558 | return error_mark_node; | |
3559 | } | |
3560 | else if (!noconvert) | |
3561 | arg = default_conversion (arg); | |
3562 | break; | |
3563 | ||
3564 | case TRUTH_NOT_EXPR: | |
3565 | if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE | |
3566 | && typecode != REAL_TYPE && typecode != POINTER_TYPE | |
3567 | && typecode != COMPLEX_TYPE) | |
3568 | { | |
3569 | error_at (location, | |
3570 | "wrong type argument to unary exclamation mark"); | |
3571 | return error_mark_node; | |
3572 | } | |
3573 | if (int_operands) | |
3574 | { | |
3575 | arg = c_objc_common_truthvalue_conversion (location, xarg); | |
3576 | arg = remove_c_maybe_const_expr (arg); | |
3577 | } | |
3578 | else | |
3579 | arg = c_objc_common_truthvalue_conversion (location, arg); | |
3580 | ret = invert_truthvalue_loc (location, arg); | |
3581 | /* If the TRUTH_NOT_EXPR has been folded, reset the location. */ | |
3582 | if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret)) | |
3583 | location = EXPR_LOCATION (ret); | |
3584 | goto return_build_unary_op; | |
3585 | ||
3586 | case REALPART_EXPR: | |
3587 | case IMAGPART_EXPR: | |
3588 | ret = build_real_imag_expr (location, code, arg); | |
3589 | if (ret == error_mark_node) | |
3590 | return error_mark_node; | |
3591 | if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE) | |
3592 | eptype = TREE_TYPE (eptype); | |
3593 | goto return_build_unary_op; | |
3594 | ||
3595 | case PREINCREMENT_EXPR: | |
3596 | case POSTINCREMENT_EXPR: | |
3597 | case PREDECREMENT_EXPR: | |
3598 | case POSTDECREMENT_EXPR: | |
3599 | ||
3600 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
3601 | { | |
3602 | tree inner = build_unary_op (location, code, | |
3603 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3604 | if (inner == error_mark_node) | |
3605 | return error_mark_node; | |
3606 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3607 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3608 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3609 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1; | |
3610 | goto return_build_unary_op; | |
3611 | } | |
3612 | ||
3613 | /* Complain about anything that is not a true lvalue. In | |
3614 | Objective-C, skip this check for property_refs. */ | |
3615 | if (!objc_is_property_ref (arg) | |
3616 | && !lvalue_or_else (location, | |
3617 | arg, ((code == PREINCREMENT_EXPR | |
3618 | || code == POSTINCREMENT_EXPR) | |
3619 | ? lv_increment | |
3620 | : lv_decrement))) | |
3621 | return error_mark_node; | |
3622 | ||
3623 | if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE) | |
3624 | { | |
3625 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3626 | warning_at (location, OPT_Wc___compat, | |
3627 | "increment of enumeration value is invalid in C++"); | |
3628 | else | |
3629 | warning_at (location, OPT_Wc___compat, | |
3630 | "decrement of enumeration value is invalid in C++"); | |
3631 | } | |
3632 | ||
3633 | /* Ensure the argument is fully folded inside any SAVE_EXPR. */ | |
3634 | arg = c_fully_fold (arg, false, NULL); | |
3635 | ||
3636 | /* Increment or decrement the real part of the value, | |
3637 | and don't change the imaginary part. */ | |
3638 | if (typecode == COMPLEX_TYPE) | |
3639 | { | |
3640 | tree real, imag; | |
3641 | ||
3642 | pedwarn (location, OPT_Wpedantic, | |
3643 | "ISO C does not support %<++%> and %<--%> on complex types"); | |
3644 | ||
3645 | arg = stabilize_reference (arg); | |
3646 | real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1); | |
3647 | imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1); | |
3648 | real = build_unary_op (EXPR_LOCATION (arg), code, real, 1); | |
3649 | if (real == error_mark_node || imag == error_mark_node) | |
3650 | return error_mark_node; | |
3651 | ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg), | |
3652 | real, imag); | |
3653 | goto return_build_unary_op; | |
3654 | } | |
3655 | ||
3656 | /* Report invalid types. */ | |
3657 | ||
3658 | if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE | |
3659 | && typecode != INTEGER_TYPE && typecode != REAL_TYPE) | |
3660 | { | |
3661 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3662 | error_at (location, "wrong type argument to increment"); | |
3663 | else | |
3664 | error_at (location, "wrong type argument to decrement"); | |
3665 | ||
3666 | return error_mark_node; | |
3667 | } | |
3668 | ||
3669 | { | |
3670 | tree inc; | |
3671 | ||
3672 | argtype = TREE_TYPE (arg); | |
3673 | ||
3674 | /* Compute the increment. */ | |
3675 | ||
3676 | if (typecode == POINTER_TYPE) | |
3677 | { | |
3678 | /* If pointer target is an undefined struct, | |
3679 | we just cannot know how to do the arithmetic. */ | |
3680 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) | |
3681 | { | |
3682 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3683 | error_at (location, | |
3684 | "increment of pointer to unknown structure"); | |
3685 | else | |
3686 | error_at (location, | |
3687 | "decrement of pointer to unknown structure"); | |
3688 | } | |
3689 | else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE | |
3690 | || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) | |
3691 | { | |
3692 | if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) | |
3693 | pedwarn (location, OPT_Wpointer_arith, | |
3694 | "wrong type argument to increment"); | |
3695 | else | |
3696 | pedwarn (location, OPT_Wpointer_arith, | |
3697 | "wrong type argument to decrement"); | |
3698 | } | |
3699 | ||
3700 | inc = c_size_in_bytes (TREE_TYPE (argtype)); | |
3701 | inc = convert_to_ptrofftype_loc (location, inc); | |
3702 | } | |
3703 | else if (FRACT_MODE_P (TYPE_MODE (argtype))) | |
3704 | { | |
3705 | /* For signed fract types, we invert ++ to -- or | |
3706 | -- to ++, and change inc from 1 to -1, because | |
3707 | it is not possible to represent 1 in signed fract constants. | |
3708 | For unsigned fract types, the result always overflows and | |
3709 | we get an undefined (original) or the maximum value. */ | |
3710 | if (code == PREINCREMENT_EXPR) | |
3711 | code = PREDECREMENT_EXPR; | |
3712 | else if (code == PREDECREMENT_EXPR) | |
3713 | code = PREINCREMENT_EXPR; | |
3714 | else if (code == POSTINCREMENT_EXPR) | |
3715 | code = POSTDECREMENT_EXPR; | |
3716 | else /* code == POSTDECREMENT_EXPR */ | |
3717 | code = POSTINCREMENT_EXPR; | |
3718 | ||
3719 | inc = integer_minus_one_node; | |
3720 | inc = convert (argtype, inc); | |
3721 | } | |
3722 | else | |
3723 | { | |
3724 | inc = integer_one_node; | |
3725 | inc = convert (argtype, inc); | |
3726 | } | |
3727 | ||
3728 | /* If 'arg' is an Objective-C PROPERTY_REF expression, then we | |
3729 | need to ask Objective-C to build the increment or decrement | |
3730 | expression for it. */ | |
3731 | if (objc_is_property_ref (arg)) | |
3732 | return objc_build_incr_expr_for_property_ref (location, code, | |
3733 | arg, inc); | |
3734 | ||
3735 | /* Report a read-only lvalue. */ | |
3736 | if (TYPE_READONLY (argtype)) | |
3737 | { | |
3738 | readonly_error (arg, | |
3739 | ((code == PREINCREMENT_EXPR | |
3740 | || code == POSTINCREMENT_EXPR) | |
3741 | ? lv_increment : lv_decrement)); | |
3742 | return error_mark_node; | |
3743 | } | |
3744 | else if (TREE_READONLY (arg)) | |
3745 | readonly_warning (arg, | |
3746 | ((code == PREINCREMENT_EXPR | |
3747 | || code == POSTINCREMENT_EXPR) | |
3748 | ? lv_increment : lv_decrement)); | |
3749 | ||
3750 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) | |
3751 | val = boolean_increment (code, arg); | |
3752 | else | |
3753 | val = build2 (code, TREE_TYPE (arg), arg, inc); | |
3754 | TREE_SIDE_EFFECTS (val) = 1; | |
3755 | if (TREE_CODE (val) != code) | |
3756 | TREE_NO_WARNING (val) = 1; | |
3757 | ret = val; | |
3758 | goto return_build_unary_op; | |
3759 | } | |
3760 | ||
3761 | case ADDR_EXPR: | |
3762 | /* Note that this operation never does default_conversion. */ | |
3763 | ||
3764 | /* The operand of unary '&' must be an lvalue (which excludes | |
3765 | expressions of type void), or, in C99, the result of a [] or | |
3766 | unary '*' operator. */ | |
3767 | if (VOID_TYPE_P (TREE_TYPE (arg)) | |
3768 | && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED | |
3769 | && (TREE_CODE (arg) != INDIRECT_REF | |
3770 | || !flag_isoc99)) | |
3771 | pedwarn (location, 0, "taking address of expression of type %<void%>"); | |
3772 | ||
3773 | /* Let &* cancel out to simplify resulting code. */ | |
3774 | if (TREE_CODE (arg) == INDIRECT_REF) | |
3775 | { | |
3776 | /* Don't let this be an lvalue. */ | |
3777 | if (lvalue_p (TREE_OPERAND (arg, 0))) | |
3778 | return non_lvalue_loc (location, TREE_OPERAND (arg, 0)); | |
3779 | ret = TREE_OPERAND (arg, 0); | |
3780 | goto return_build_unary_op; | |
3781 | } | |
3782 | ||
3783 | /* For &x[y], return x+y */ | |
3784 | if (TREE_CODE (arg) == ARRAY_REF) | |
3785 | { | |
3786 | tree op0 = TREE_OPERAND (arg, 0); | |
3787 | if (!c_mark_addressable (op0)) | |
3788 | return error_mark_node; | |
3789 | } | |
3790 | ||
3791 | /* Anything not already handled and not a true memory reference | |
3792 | or a non-lvalue array is an error. */ | |
3793 | else if (typecode != FUNCTION_TYPE && !flag | |
3794 | && !lvalue_or_else (location, arg, lv_addressof)) | |
3795 | return error_mark_node; | |
3796 | ||
3797 | /* Move address operations inside C_MAYBE_CONST_EXPR to simplify | |
3798 | folding later. */ | |
3799 | if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR) | |
3800 | { | |
3801 | tree inner = build_unary_op (location, code, | |
3802 | C_MAYBE_CONST_EXPR_EXPR (arg), flag); | |
3803 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
3804 | C_MAYBE_CONST_EXPR_PRE (arg), inner); | |
3805 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg)); | |
3806 | C_MAYBE_CONST_EXPR_NON_CONST (ret) | |
3807 | = C_MAYBE_CONST_EXPR_NON_CONST (arg); | |
3808 | goto return_build_unary_op; | |
3809 | } | |
3810 | ||
3811 | /* Ordinary case; arg is a COMPONENT_REF or a decl. */ | |
3812 | argtype = TREE_TYPE (arg); | |
3813 | ||
3814 | /* If the lvalue is const or volatile, merge that into the type | |
3815 | to which the address will point. This is only needed | |
3816 | for function types. */ | |
3817 | if ((DECL_P (arg) || REFERENCE_CLASS_P (arg)) | |
3818 | && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)) | |
3819 | && TREE_CODE (argtype) == FUNCTION_TYPE) | |
3820 | { | |
3821 | int orig_quals = TYPE_QUALS (strip_array_types (argtype)); | |
3822 | int quals = orig_quals; | |
3823 | ||
3824 | if (TREE_READONLY (arg)) | |
3825 | quals |= TYPE_QUAL_CONST; | |
3826 | if (TREE_THIS_VOLATILE (arg)) | |
3827 | quals |= TYPE_QUAL_VOLATILE; | |
3828 | ||
3829 | argtype = c_build_qualified_type (argtype, quals); | |
3830 | } | |
3831 | ||
3832 | if (!c_mark_addressable (arg)) | |
3833 | return error_mark_node; | |
3834 | ||
3835 | gcc_assert (TREE_CODE (arg) != COMPONENT_REF | |
3836 | || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); | |
3837 | ||
3838 | argtype = build_pointer_type (argtype); | |
3839 | ||
3840 | /* ??? Cope with user tricks that amount to offsetof. Delete this | |
3841 | when we have proper support for integer constant expressions. */ | |
3842 | val = get_base_address (arg); | |
3843 | if (val && TREE_CODE (val) == INDIRECT_REF | |
3844 | && TREE_CONSTANT (TREE_OPERAND (val, 0))) | |
3845 | { | |
3846 | ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg)); | |
3847 | goto return_build_unary_op; | |
3848 | } | |
3849 | ||
3850 | val = build1 (ADDR_EXPR, argtype, arg); | |
3851 | ||
3852 | ret = val; | |
3853 | goto return_build_unary_op; | |
3854 | ||
3855 | default: | |
3856 | gcc_unreachable (); | |
3857 | } | |
3858 | ||
3859 | if (argtype == 0) | |
3860 | argtype = TREE_TYPE (arg); | |
3861 | if (TREE_CODE (arg) == INTEGER_CST) | |
3862 | ret = (require_constant_value | |
3863 | ? fold_build1_initializer_loc (location, code, argtype, arg) | |
3864 | : fold_build1_loc (location, code, argtype, arg)); | |
3865 | else | |
3866 | ret = build1 (code, argtype, arg); | |
3867 | return_build_unary_op: | |
3868 | gcc_assert (ret != error_mark_node); | |
3869 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) | |
3870 | && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg))) | |
3871 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
3872 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands) | |
3873 | ret = note_integer_operands (ret); | |
3874 | if (eptype) | |
3875 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
3876 | protected_set_expr_location (ret, location); | |
3877 | return ret; | |
3878 | } | |
3879 | ||
3880 | /* Return nonzero if REF is an lvalue valid for this language. | |
3881 | Lvalues can be assigned, unless their type has TYPE_READONLY. | |
3882 | Lvalues can have their address taken, unless they have C_DECL_REGISTER. */ | |
3883 | ||
3884 | bool | |
3885 | lvalue_p (const_tree ref) | |
3886 | { | |
3887 | const enum tree_code code = TREE_CODE (ref); | |
3888 | ||
3889 | switch (code) | |
3890 | { | |
3891 | case REALPART_EXPR: | |
3892 | case IMAGPART_EXPR: | |
3893 | case COMPONENT_REF: | |
3894 | return lvalue_p (TREE_OPERAND (ref, 0)); | |
3895 | ||
3896 | case C_MAYBE_CONST_EXPR: | |
3897 | return lvalue_p (TREE_OPERAND (ref, 1)); | |
3898 | ||
3899 | case COMPOUND_LITERAL_EXPR: | |
3900 | case STRING_CST: | |
3901 | return 1; | |
3902 | ||
3903 | case INDIRECT_REF: | |
3904 | case ARRAY_REF: | |
3905 | case ARRAY_NOTATION_REF: | |
3906 | case VAR_DECL: | |
3907 | case PARM_DECL: | |
3908 | case RESULT_DECL: | |
3909 | case ERROR_MARK: | |
3910 | return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE | |
3911 | && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE); | |
3912 | ||
3913 | case BIND_EXPR: | |
3914 | return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; | |
3915 | ||
3916 | default: | |
3917 | return 0; | |
3918 | } | |
3919 | } | |
3920 | \f | |
3921 | /* Give a warning for storing in something that is read-only in GCC | |
3922 | terms but not const in ISO C terms. */ | |
3923 | ||
3924 | static void | |
3925 | readonly_warning (tree arg, enum lvalue_use use) | |
3926 | { | |
3927 | switch (use) | |
3928 | { | |
3929 | case lv_assign: | |
3930 | warning (0, "assignment of read-only location %qE", arg); | |
3931 | break; | |
3932 | case lv_increment: | |
3933 | warning (0, "increment of read-only location %qE", arg); | |
3934 | break; | |
3935 | case lv_decrement: | |
3936 | warning (0, "decrement of read-only location %qE", arg); | |
3937 | break; | |
3938 | default: | |
3939 | gcc_unreachable (); | |
3940 | } | |
3941 | return; | |
3942 | } | |
3943 | ||
3944 | ||
3945 | /* Return nonzero if REF is an lvalue valid for this language; | |
3946 | otherwise, print an error message and return zero. USE says | |
3947 | how the lvalue is being used and so selects the error message. | |
3948 | LOCATION is the location at which any error should be reported. */ | |
3949 | ||
3950 | static int | |
3951 | lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) | |
3952 | { | |
3953 | int win = lvalue_p (ref); | |
3954 | ||
3955 | if (!win) | |
3956 | lvalue_error (loc, use); | |
3957 | ||
3958 | return win; | |
3959 | } | |
3960 | \f | |
3961 | /* Mark EXP saying that we need to be able to take the | |
3962 | address of it; it should not be allocated in a register. | |
3963 | Returns true if successful. */ | |
3964 | ||
3965 | bool | |
3966 | c_mark_addressable (tree exp) | |
3967 | { | |
3968 | tree x = exp; | |
3969 | ||
3970 | while (1) | |
3971 | switch (TREE_CODE (x)) | |
3972 | { | |
3973 | case COMPONENT_REF: | |
3974 | if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1))) | |
3975 | { | |
3976 | error | |
3977 | ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1)); | |
3978 | return false; | |
3979 | } | |
3980 | ||
3981 | /* ... fall through ... */ | |
3982 | ||
3983 | case ADDR_EXPR: | |
3984 | case ARRAY_REF: | |
3985 | case REALPART_EXPR: | |
3986 | case IMAGPART_EXPR: | |
3987 | x = TREE_OPERAND (x, 0); | |
3988 | break; | |
3989 | ||
3990 | case COMPOUND_LITERAL_EXPR: | |
3991 | case CONSTRUCTOR: | |
3992 | TREE_ADDRESSABLE (x) = 1; | |
3993 | return true; | |
3994 | ||
3995 | case VAR_DECL: | |
3996 | case CONST_DECL: | |
3997 | case PARM_DECL: | |
3998 | case RESULT_DECL: | |
3999 | if (C_DECL_REGISTER (x) | |
4000 | && DECL_NONLOCAL (x)) | |
4001 | { | |
4002 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) | |
4003 | { | |
4004 | error | |
4005 | ("global register variable %qD used in nested function", x); | |
4006 | return false; | |
4007 | } | |
4008 | pedwarn (input_location, 0, "register variable %qD used in nested function", x); | |
4009 | } | |
4010 | else if (C_DECL_REGISTER (x)) | |
4011 | { | |
4012 | if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x)) | |
4013 | error ("address of global register variable %qD requested", x); | |
4014 | else | |
4015 | error ("address of register variable %qD requested", x); | |
4016 | return false; | |
4017 | } | |
4018 | ||
4019 | /* drops in */ | |
4020 | case FUNCTION_DECL: | |
4021 | TREE_ADDRESSABLE (x) = 1; | |
4022 | /* drops out */ | |
4023 | default: | |
4024 | return true; | |
4025 | } | |
4026 | } | |
4027 | \f | |
4028 | /* Convert EXPR to TYPE, warning about conversion problems with | |
4029 | constants. SEMANTIC_TYPE is the type this conversion would use | |
4030 | without excess precision. If SEMANTIC_TYPE is NULL, this function | |
4031 | is equivalent to convert_and_check. This function is a wrapper that | |
4032 | handles conversions that may be different than | |
4033 | the usual ones because of excess precision. */ | |
4034 | ||
4035 | static tree | |
4036 | ep_convert_and_check (tree type, tree expr, tree semantic_type) | |
4037 | { | |
4038 | if (TREE_TYPE (expr) == type) | |
4039 | return expr; | |
4040 | ||
4041 | if (!semantic_type) | |
4042 | return convert_and_check (type, expr); | |
4043 | ||
4044 | if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
4045 | && TREE_TYPE (expr) != semantic_type) | |
4046 | { | |
4047 | /* For integers, we need to check the real conversion, not | |
4048 | the conversion to the excess precision type. */ | |
4049 | expr = convert_and_check (semantic_type, expr); | |
4050 | } | |
4051 | /* Result type is the excess precision type, which should be | |
4052 | large enough, so do not check. */ | |
4053 | return convert (type, expr); | |
4054 | } | |
4055 | ||
4056 | /* Build and return a conditional expression IFEXP ? OP1 : OP2. If | |
4057 | IFEXP_BCP then the condition is a call to __builtin_constant_p, and | |
4058 | if folded to an integer constant then the unselected half may | |
4059 | contain arbitrary operations not normally permitted in constant | |
4060 | expressions. Set the location of the expression to LOC. */ | |
4061 | ||
4062 | tree | |
4063 | build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, | |
4064 | tree op1, tree op1_original_type, tree op2, | |
4065 | tree op2_original_type) | |
4066 | { | |
4067 | tree type1; | |
4068 | tree type2; | |
4069 | enum tree_code code1; | |
4070 | enum tree_code code2; | |
4071 | tree result_type = NULL; | |
4072 | tree semantic_result_type = NULL; | |
4073 | tree orig_op1 = op1, orig_op2 = op2; | |
4074 | bool int_const, op1_int_operands, op2_int_operands, int_operands; | |
4075 | bool ifexp_int_operands; | |
4076 | tree ret; | |
4077 | ||
4078 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
4079 | if (op1_int_operands) | |
4080 | op1 = remove_c_maybe_const_expr (op1); | |
4081 | op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2); | |
4082 | if (op2_int_operands) | |
4083 | op2 = remove_c_maybe_const_expr (op2); | |
4084 | ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp); | |
4085 | if (ifexp_int_operands) | |
4086 | ifexp = remove_c_maybe_const_expr (ifexp); | |
4087 | ||
4088 | /* Promote both alternatives. */ | |
4089 | ||
4090 | if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE) | |
4091 | op1 = default_conversion (op1); | |
4092 | if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE) | |
4093 | op2 = default_conversion (op2); | |
4094 | ||
4095 | if (TREE_CODE (ifexp) == ERROR_MARK | |
4096 | || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK | |
4097 | || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK) | |
4098 | return error_mark_node; | |
4099 | ||
4100 | type1 = TREE_TYPE (op1); | |
4101 | code1 = TREE_CODE (type1); | |
4102 | type2 = TREE_TYPE (op2); | |
4103 | code2 = TREE_CODE (type2); | |
4104 | ||
4105 | /* C90 does not permit non-lvalue arrays in conditional expressions. | |
4106 | In C99 they will be pointers by now. */ | |
4107 | if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE) | |
4108 | { | |
4109 | error_at (colon_loc, "non-lvalue array in conditional expression"); | |
4110 | return error_mark_node; | |
4111 | } | |
4112 | ||
4113 | if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR | |
4114 | || TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4115 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4116 | || code1 == COMPLEX_TYPE) | |
4117 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4118 | || code2 == COMPLEX_TYPE)) | |
4119 | { | |
4120 | semantic_result_type = c_common_type (type1, type2); | |
4121 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
4122 | { | |
4123 | op1 = TREE_OPERAND (op1, 0); | |
4124 | type1 = TREE_TYPE (op1); | |
4125 | gcc_assert (TREE_CODE (type1) == code1); | |
4126 | } | |
4127 | if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR) | |
4128 | { | |
4129 | op2 = TREE_OPERAND (op2, 0); | |
4130 | type2 = TREE_TYPE (op2); | |
4131 | gcc_assert (TREE_CODE (type2) == code2); | |
4132 | } | |
4133 | } | |
4134 | ||
4135 | if (warn_cxx_compat) | |
4136 | { | |
4137 | tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1); | |
4138 | tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2); | |
4139 | ||
4140 | if (TREE_CODE (t1) == ENUMERAL_TYPE | |
4141 | && TREE_CODE (t2) == ENUMERAL_TYPE | |
4142 | && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2)) | |
4143 | warning_at (colon_loc, OPT_Wc___compat, | |
4144 | ("different enum types in conditional is " | |
4145 | "invalid in C++: %qT vs %qT"), | |
4146 | t1, t2); | |
4147 | } | |
4148 | ||
4149 | /* Quickly detect the usual case where op1 and op2 have the same type | |
4150 | after promotion. */ | |
4151 | if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)) | |
4152 | { | |
4153 | if (type1 == type2) | |
4154 | result_type = type1; | |
4155 | else | |
4156 | result_type = TYPE_MAIN_VARIANT (type1); | |
4157 | } | |
4158 | else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
4159 | || code1 == COMPLEX_TYPE) | |
4160 | && (code2 == INTEGER_TYPE || code2 == REAL_TYPE | |
4161 | || code2 == COMPLEX_TYPE)) | |
4162 | { | |
4163 | result_type = c_common_type (type1, type2); | |
4164 | do_warn_double_promotion (result_type, type1, type2, | |
4165 | "implicit conversion from %qT to %qT to " | |
4166 | "match other result of conditional", | |
4167 | colon_loc); | |
4168 | ||
4169 | /* If -Wsign-compare, warn here if type1 and type2 have | |
4170 | different signedness. We'll promote the signed to unsigned | |
4171 | and later code won't know it used to be different. | |
4172 | Do this check on the original types, so that explicit casts | |
4173 | will be considered, but default promotions won't. */ | |
4174 | if (c_inhibit_evaluation_warnings == 0) | |
4175 | { | |
4176 | int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
4177 | int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2)); | |
4178 | ||
4179 | if (unsigned_op1 ^ unsigned_op2) | |
4180 | { | |
4181 | bool ovf; | |
4182 | ||
4183 | /* Do not warn if the result type is signed, since the | |
4184 | signed type will only be chosen if it can represent | |
4185 | all the values of the unsigned type. */ | |
4186 | if (!TYPE_UNSIGNED (result_type)) | |
4187 | /* OK */; | |
4188 | else | |
4189 | { | |
4190 | bool op1_maybe_const = true; | |
4191 | bool op2_maybe_const = true; | |
4192 | ||
4193 | /* Do not warn if the signed quantity is an | |
4194 | unsuffixed integer literal (or some static | |
4195 | constant expression involving such literals) and | |
4196 | it is non-negative. This warning requires the | |
4197 | operands to be folded for best results, so do | |
4198 | that folding in this case even without | |
4199 | warn_sign_compare to avoid warning options | |
4200 | possibly affecting code generation. */ | |
4201 | c_inhibit_evaluation_warnings | |
4202 | += (ifexp == truthvalue_false_node); | |
4203 | op1 = c_fully_fold (op1, require_constant_value, | |
4204 | &op1_maybe_const); | |
4205 | c_inhibit_evaluation_warnings | |
4206 | -= (ifexp == truthvalue_false_node); | |
4207 | ||
4208 | c_inhibit_evaluation_warnings | |
4209 | += (ifexp == truthvalue_true_node); | |
4210 | op2 = c_fully_fold (op2, require_constant_value, | |
4211 | &op2_maybe_const); | |
4212 | c_inhibit_evaluation_warnings | |
4213 | -= (ifexp == truthvalue_true_node); | |
4214 | ||
4215 | if (warn_sign_compare) | |
4216 | { | |
4217 | if ((unsigned_op2 | |
4218 | && tree_expr_nonnegative_warnv_p (op1, &ovf)) | |
4219 | || (unsigned_op1 | |
4220 | && tree_expr_nonnegative_warnv_p (op2, &ovf))) | |
4221 | /* OK */; | |
4222 | else | |
4223 | warning_at (colon_loc, OPT_Wsign_compare, | |
4224 | ("signed and unsigned type in " | |
4225 | "conditional expression")); | |
4226 | } | |
4227 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
4228 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); | |
4229 | if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST) | |
4230 | op2 = c_wrap_maybe_const (op2, !op2_maybe_const); | |
4231 | } | |
4232 | } | |
4233 | } | |
4234 | } | |
4235 | else if (code1 == VOID_TYPE || code2 == VOID_TYPE) | |
4236 | { | |
4237 | if (code1 != VOID_TYPE || code2 != VOID_TYPE) | |
4238 | pedwarn (colon_loc, OPT_Wpedantic, | |
4239 | "ISO C forbids conditional expr with only one void side"); | |
4240 | result_type = void_type_node; | |
4241 | } | |
4242 | else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE) | |
4243 | { | |
4244 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
4245 | addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2)); | |
4246 | addr_space_t as_common; | |
4247 | ||
4248 | if (comp_target_types (colon_loc, type1, type2)) | |
4249 | result_type = common_pointer_type (type1, type2); | |
4250 | else if (null_pointer_constant_p (orig_op1)) | |
4251 | result_type = type2; | |
4252 | else if (null_pointer_constant_p (orig_op2)) | |
4253 | result_type = type1; | |
4254 | else if (!addr_space_superset (as1, as2, &as_common)) | |
4255 | { | |
4256 | error_at (colon_loc, "pointers to disjoint address spaces " | |
4257 | "used in conditional expression"); | |
4258 | return error_mark_node; | |
4259 | } | |
4260 | else if (VOID_TYPE_P (TREE_TYPE (type1))) | |
4261 | { | |
4262 | if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE) | |
4263 | pedwarn (colon_loc, OPT_Wpedantic, | |
4264 | "ISO C forbids conditional expr between " | |
4265 | "%<void *%> and function pointer"); | |
4266 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type1), | |
4267 | TREE_TYPE (type2))); | |
4268 | } | |
4269 | else if (VOID_TYPE_P (TREE_TYPE (type2))) | |
4270 | { | |
4271 | if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE) | |
4272 | pedwarn (colon_loc, OPT_Wpedantic, | |
4273 | "ISO C forbids conditional expr between " | |
4274 | "%<void *%> and function pointer"); | |
4275 | result_type = build_pointer_type (qualify_type (TREE_TYPE (type2), | |
4276 | TREE_TYPE (type1))); | |
4277 | } | |
4278 | /* Objective-C pointer comparisons are a bit more lenient. */ | |
4279 | else if (objc_have_common_type (type1, type2, -3, NULL_TREE)) | |
4280 | result_type = objc_common_type (type1, type2); | |
4281 | else | |
4282 | { | |
4283 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
4284 | ||
4285 | pedwarn (colon_loc, 0, | |
4286 | "pointer type mismatch in conditional expression"); | |
4287 | result_type = build_pointer_type | |
4288 | (build_qualified_type (void_type_node, qual)); | |
4289 | } | |
4290 | } | |
4291 | else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE) | |
4292 | { | |
4293 | if (!null_pointer_constant_p (orig_op2)) | |
4294 | pedwarn (colon_loc, 0, | |
4295 | "pointer/integer type mismatch in conditional expression"); | |
4296 | else | |
4297 | { | |
4298 | op2 = null_pointer_node; | |
4299 | } | |
4300 | result_type = type1; | |
4301 | } | |
4302 | else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
4303 | { | |
4304 | if (!null_pointer_constant_p (orig_op1)) | |
4305 | pedwarn (colon_loc, 0, | |
4306 | "pointer/integer type mismatch in conditional expression"); | |
4307 | else | |
4308 | { | |
4309 | op1 = null_pointer_node; | |
4310 | } | |
4311 | result_type = type2; | |
4312 | } | |
4313 | ||
4314 | if (!result_type) | |
4315 | { | |
4316 | if (flag_cond_mismatch) | |
4317 | result_type = void_type_node; | |
4318 | else | |
4319 | { | |
4320 | error_at (colon_loc, "type mismatch in conditional expression"); | |
4321 | return error_mark_node; | |
4322 | } | |
4323 | } | |
4324 | ||
4325 | /* Merge const and volatile flags of the incoming types. */ | |
4326 | result_type | |
4327 | = build_type_variant (result_type, | |
4328 | TYPE_READONLY (type1) || TYPE_READONLY (type2), | |
4329 | TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2)); | |
4330 | ||
4331 | op1 = ep_convert_and_check (result_type, op1, semantic_result_type); | |
4332 | op2 = ep_convert_and_check (result_type, op2, semantic_result_type); | |
4333 | ||
4334 | if (ifexp_bcp && ifexp == truthvalue_true_node) | |
4335 | { | |
4336 | op2_int_operands = true; | |
4337 | op1 = c_fully_fold (op1, require_constant_value, NULL); | |
4338 | } | |
4339 | if (ifexp_bcp && ifexp == truthvalue_false_node) | |
4340 | { | |
4341 | op1_int_operands = true; | |
4342 | op2 = c_fully_fold (op2, require_constant_value, NULL); | |
4343 | } | |
4344 | int_const = int_operands = (ifexp_int_operands | |
4345 | && op1_int_operands | |
4346 | && op2_int_operands); | |
4347 | if (int_operands) | |
4348 | { | |
4349 | int_const = ((ifexp == truthvalue_true_node | |
4350 | && TREE_CODE (orig_op1) == INTEGER_CST | |
4351 | && !TREE_OVERFLOW (orig_op1)) | |
4352 | || (ifexp == truthvalue_false_node | |
4353 | && TREE_CODE (orig_op2) == INTEGER_CST | |
4354 | && !TREE_OVERFLOW (orig_op2))); | |
4355 | } | |
4356 | if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST)) | |
4357 | ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2); | |
4358 | else | |
4359 | { | |
4360 | if (int_operands) | |
4361 | { | |
4362 | op1 = remove_c_maybe_const_expr (op1); | |
4363 | op2 = remove_c_maybe_const_expr (op2); | |
4364 | } | |
4365 | ret = build3 (COND_EXPR, result_type, ifexp, op1, op2); | |
4366 | if (int_operands) | |
4367 | ret = note_integer_operands (ret); | |
4368 | } | |
4369 | if (semantic_result_type) | |
4370 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
4371 | ||
4372 | protected_set_expr_location (ret, colon_loc); | |
4373 | return ret; | |
4374 | } | |
4375 | \f | |
4376 | /* Return a compound expression that performs two expressions and | |
4377 | returns the value of the second of them. | |
4378 | ||
4379 | LOC is the location of the COMPOUND_EXPR. */ | |
4380 | ||
4381 | tree | |
4382 | build_compound_expr (location_t loc, tree expr1, tree expr2) | |
4383 | { | |
4384 | bool expr1_int_operands, expr2_int_operands; | |
4385 | tree eptype = NULL_TREE; | |
4386 | tree ret; | |
4387 | ||
4388 | expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1); | |
4389 | if (expr1_int_operands) | |
4390 | expr1 = remove_c_maybe_const_expr (expr1); | |
4391 | expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2); | |
4392 | if (expr2_int_operands) | |
4393 | expr2 = remove_c_maybe_const_expr (expr2); | |
4394 | ||
4395 | if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR) | |
4396 | expr1 = TREE_OPERAND (expr1, 0); | |
4397 | if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR) | |
4398 | { | |
4399 | eptype = TREE_TYPE (expr2); | |
4400 | expr2 = TREE_OPERAND (expr2, 0); | |
4401 | } | |
4402 | ||
4403 | if (!TREE_SIDE_EFFECTS (expr1)) | |
4404 | { | |
4405 | /* The left-hand operand of a comma expression is like an expression | |
4406 | statement: with -Wunused, we should warn if it doesn't have | |
4407 | any side-effects, unless it was explicitly cast to (void). */ | |
4408 | if (warn_unused_value) | |
4409 | { | |
4410 | if (VOID_TYPE_P (TREE_TYPE (expr1)) | |
4411 | && CONVERT_EXPR_P (expr1)) | |
4412 | ; /* (void) a, b */ | |
4413 | else if (VOID_TYPE_P (TREE_TYPE (expr1)) | |
4414 | && TREE_CODE (expr1) == COMPOUND_EXPR | |
4415 | && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1))) | |
4416 | ; /* (void) a, (void) b, c */ | |
4417 | else | |
4418 | warning_at (loc, OPT_Wunused_value, | |
4419 | "left-hand operand of comma expression has no effect"); | |
4420 | } | |
4421 | } | |
4422 | ||
4423 | /* With -Wunused, we should also warn if the left-hand operand does have | |
4424 | side-effects, but computes a value which is not used. For example, in | |
4425 | `foo() + bar(), baz()' the result of the `+' operator is not used, | |
4426 | so we should issue a warning. */ | |
4427 | else if (warn_unused_value) | |
4428 | warn_if_unused_value (expr1, loc); | |
4429 | ||
4430 | if (expr2 == error_mark_node) | |
4431 | return error_mark_node; | |
4432 | ||
4433 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2); | |
4434 | ||
4435 | if (flag_isoc99 | |
4436 | && expr1_int_operands | |
4437 | && expr2_int_operands) | |
4438 | ret = note_integer_operands (ret); | |
4439 | ||
4440 | if (eptype) | |
4441 | ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret); | |
4442 | ||
4443 | protected_set_expr_location (ret, loc); | |
4444 | return ret; | |
4445 | } | |
4446 | ||
4447 | /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to | |
4448 | which we are casting. OTYPE is the type of the expression being | |
4449 | cast. Both TYPE and OTYPE are pointer types. LOC is the location | |
4450 | of the cast. -Wcast-qual appeared on the command line. Named | |
4451 | address space qualifiers are not handled here, because they result | |
4452 | in different warnings. */ | |
4453 | ||
4454 | static void | |
4455 | handle_warn_cast_qual (location_t loc, tree type, tree otype) | |
4456 | { | |
4457 | tree in_type = type; | |
4458 | tree in_otype = otype; | |
4459 | int added = 0; | |
4460 | int discarded = 0; | |
4461 | bool is_const; | |
4462 | ||
4463 | /* Check that the qualifiers on IN_TYPE are a superset of the | |
4464 | qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE | |
4465 | nodes is uninteresting and we stop as soon as we hit a | |
4466 | non-POINTER_TYPE node on either type. */ | |
4467 | do | |
4468 | { | |
4469 | in_otype = TREE_TYPE (in_otype); | |
4470 | in_type = TREE_TYPE (in_type); | |
4471 | ||
4472 | /* GNU C allows cv-qualified function types. 'const' means the | |
4473 | function is very pure, 'volatile' means it can't return. We | |
4474 | need to warn when such qualifiers are added, not when they're | |
4475 | taken away. */ | |
4476 | if (TREE_CODE (in_otype) == FUNCTION_TYPE | |
4477 | && TREE_CODE (in_type) == FUNCTION_TYPE) | |
4478 | added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type) | |
4479 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype)); | |
4480 | else | |
4481 | discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype) | |
4482 | & ~TYPE_QUALS_NO_ADDR_SPACE (in_type)); | |
4483 | } | |
4484 | while (TREE_CODE (in_type) == POINTER_TYPE | |
4485 | && TREE_CODE (in_otype) == POINTER_TYPE); | |
4486 | ||
4487 | if (added) | |
4488 | warning_at (loc, OPT_Wcast_qual, | |
4489 | "cast adds %q#v qualifier to function type", added); | |
4490 | ||
4491 | if (discarded) | |
4492 | /* There are qualifiers present in IN_OTYPE that are not present | |
4493 | in IN_TYPE. */ | |
4494 | warning_at (loc, OPT_Wcast_qual, | |
4495 | "cast discards %q#v qualifier from pointer target type", | |
4496 | discarded); | |
4497 | ||
4498 | if (added || discarded) | |
4499 | return; | |
4500 | ||
4501 | /* A cast from **T to const **T is unsafe, because it can cause a | |
4502 | const value to be changed with no additional warning. We only | |
4503 | issue this warning if T is the same on both sides, and we only | |
4504 | issue the warning if there are the same number of pointers on | |
4505 | both sides, as otherwise the cast is clearly unsafe anyhow. A | |
4506 | cast is unsafe when a qualifier is added at one level and const | |
4507 | is not present at all outer levels. | |
4508 | ||
4509 | To issue this warning, we check at each level whether the cast | |
4510 | adds new qualifiers not already seen. We don't need to special | |
4511 | case function types, as they won't have the same | |
4512 | TYPE_MAIN_VARIANT. */ | |
4513 | ||
4514 | if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype)) | |
4515 | return; | |
4516 | if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE) | |
4517 | return; | |
4518 | ||
4519 | in_type = type; | |
4520 | in_otype = otype; | |
4521 | is_const = TYPE_READONLY (TREE_TYPE (in_type)); | |
4522 | do | |
4523 | { | |
4524 | in_type = TREE_TYPE (in_type); | |
4525 | in_otype = TREE_TYPE (in_otype); | |
4526 | if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0 | |
4527 | && !is_const) | |
4528 | { | |
4529 | warning_at (loc, OPT_Wcast_qual, | |
4530 | "to be safe all intermediate pointers in cast from " | |
4531 | "%qT to %qT must be %<const%> qualified", | |
4532 | otype, type); | |
4533 | break; | |
4534 | } | |
4535 | if (is_const) | |
4536 | is_const = TYPE_READONLY (in_type); | |
4537 | } | |
4538 | while (TREE_CODE (in_type) == POINTER_TYPE); | |
4539 | } | |
4540 | ||
4541 | /* Build an expression representing a cast to type TYPE of expression EXPR. | |
4542 | LOC is the location of the cast-- typically the open paren of the cast. */ | |
4543 | ||
4544 | tree | |
4545 | build_c_cast (location_t loc, tree type, tree expr) | |
4546 | { | |
4547 | tree value; | |
4548 | ||
4549 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
4550 | expr = TREE_OPERAND (expr, 0); | |
4551 | ||
4552 | value = expr; | |
4553 | ||
4554 | if (type == error_mark_node || expr == error_mark_node) | |
4555 | return error_mark_node; | |
4556 | ||
4557 | /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing | |
4558 | only in <protocol> qualifications. But when constructing cast expressions, | |
4559 | the protocols do matter and must be kept around. */ | |
4560 | if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr))) | |
4561 | return build1 (NOP_EXPR, type, expr); | |
4562 | ||
4563 | type = TYPE_MAIN_VARIANT (type); | |
4564 | ||
4565 | if (TREE_CODE (type) == ARRAY_TYPE) | |
4566 | { | |
4567 | error_at (loc, "cast specifies array type"); | |
4568 | return error_mark_node; | |
4569 | } | |
4570 | ||
4571 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
4572 | { | |
4573 | error_at (loc, "cast specifies function type"); | |
4574 | return error_mark_node; | |
4575 | } | |
4576 | ||
4577 | if (!VOID_TYPE_P (type)) | |
4578 | { | |
4579 | value = require_complete_type (value); | |
4580 | if (value == error_mark_node) | |
4581 | return error_mark_node; | |
4582 | } | |
4583 | ||
4584 | if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) | |
4585 | { | |
4586 | if (TREE_CODE (type) == RECORD_TYPE | |
4587 | || TREE_CODE (type) == UNION_TYPE) | |
4588 | pedwarn (loc, OPT_Wpedantic, | |
4589 | "ISO C forbids casting nonscalar to the same type"); | |
4590 | } | |
4591 | else if (TREE_CODE (type) == UNION_TYPE) | |
4592 | { | |
4593 | tree field; | |
4594 | ||
4595 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
4596 | if (TREE_TYPE (field) != error_mark_node | |
4597 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)), | |
4598 | TYPE_MAIN_VARIANT (TREE_TYPE (value)))) | |
4599 | break; | |
4600 | ||
4601 | if (field) | |
4602 | { | |
4603 | tree t; | |
4604 | bool maybe_const = true; | |
4605 | ||
4606 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type"); | |
4607 | t = c_fully_fold (value, false, &maybe_const); | |
4608 | t = build_constructor_single (type, field, t); | |
4609 | if (!maybe_const) | |
4610 | t = c_wrap_maybe_const (t, true); | |
4611 | t = digest_init (loc, type, t, | |
4612 | NULL_TREE, false, true, 0); | |
4613 | TREE_CONSTANT (t) = TREE_CONSTANT (value); | |
4614 | return t; | |
4615 | } | |
4616 | error_at (loc, "cast to union type from type not present in union"); | |
4617 | return error_mark_node; | |
4618 | } | |
4619 | else | |
4620 | { | |
4621 | tree otype, ovalue; | |
4622 | ||
4623 | if (type == void_type_node) | |
4624 | { | |
4625 | tree t = build1 (CONVERT_EXPR, type, value); | |
4626 | SET_EXPR_LOCATION (t, loc); | |
4627 | return t; | |
4628 | } | |
4629 | ||
4630 | otype = TREE_TYPE (value); | |
4631 | ||
4632 | /* Optionally warn about potentially worrisome casts. */ | |
4633 | if (warn_cast_qual | |
4634 | && TREE_CODE (type) == POINTER_TYPE | |
4635 | && TREE_CODE (otype) == POINTER_TYPE) | |
4636 | handle_warn_cast_qual (loc, type, otype); | |
4637 | ||
4638 | /* Warn about conversions between pointers to disjoint | |
4639 | address spaces. */ | |
4640 | if (TREE_CODE (type) == POINTER_TYPE | |
4641 | && TREE_CODE (otype) == POINTER_TYPE | |
4642 | && !null_pointer_constant_p (value)) | |
4643 | { | |
4644 | addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); | |
4645 | addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype)); | |
4646 | addr_space_t as_common; | |
4647 | ||
4648 | if (!addr_space_superset (as_to, as_from, &as_common)) | |
4649 | { | |
4650 | if (ADDR_SPACE_GENERIC_P (as_from)) | |
4651 | warning_at (loc, 0, "cast to %s address space pointer " | |
4652 | "from disjoint generic address space pointer", | |
4653 | c_addr_space_name (as_to)); | |
4654 | ||
4655 | else if (ADDR_SPACE_GENERIC_P (as_to)) | |
4656 | warning_at (loc, 0, "cast to generic address space pointer " | |
4657 | "from disjoint %s address space pointer", | |
4658 | c_addr_space_name (as_from)); | |
4659 | ||
4660 | else | |
4661 | warning_at (loc, 0, "cast to %s address space pointer " | |
4662 | "from disjoint %s address space pointer", | |
4663 | c_addr_space_name (as_to), | |
4664 | c_addr_space_name (as_from)); | |
4665 | } | |
4666 | } | |
4667 | ||
4668 | /* Warn about possible alignment problems. */ | |
4669 | if (STRICT_ALIGNMENT | |
4670 | && TREE_CODE (type) == POINTER_TYPE | |
4671 | && TREE_CODE (otype) == POINTER_TYPE | |
4672 | && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE | |
4673 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
4674 | /* Don't warn about opaque types, where the actual alignment | |
4675 | restriction is unknown. */ | |
4676 | && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE | |
4677 | || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE) | |
4678 | && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode) | |
4679 | && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype))) | |
4680 | warning_at (loc, OPT_Wcast_align, | |
4681 | "cast increases required alignment of target type"); | |
4682 | ||
4683 | if (TREE_CODE (type) == INTEGER_TYPE | |
4684 | && TREE_CODE (otype) == POINTER_TYPE | |
4685 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype)) | |
4686 | /* Unlike conversion of integers to pointers, where the | |
4687 | warning is disabled for converting constants because | |
4688 | of cases such as SIG_*, warn about converting constant | |
4689 | pointers to integers. In some cases it may cause unwanted | |
4690 | sign extension, and a warning is appropriate. */ | |
4691 | warning_at (loc, OPT_Wpointer_to_int_cast, | |
4692 | "cast from pointer to integer of different size"); | |
4693 | ||
4694 | if (TREE_CODE (value) == CALL_EXPR | |
4695 | && TREE_CODE (type) != TREE_CODE (otype)) | |
4696 | warning_at (loc, OPT_Wbad_function_cast, | |
4697 | "cast from function call of type %qT " | |
4698 | "to non-matching type %qT", otype, type); | |
4699 | ||
4700 | if (TREE_CODE (type) == POINTER_TYPE | |
4701 | && TREE_CODE (otype) == INTEGER_TYPE | |
4702 | && TYPE_PRECISION (type) != TYPE_PRECISION (otype) | |
4703 | /* Don't warn about converting any constant. */ | |
4704 | && !TREE_CONSTANT (value)) | |
4705 | warning_at (loc, | |
4706 | OPT_Wint_to_pointer_cast, "cast to pointer from integer " | |
4707 | "of different size"); | |
4708 | ||
4709 | if (warn_strict_aliasing <= 2) | |
4710 | strict_aliasing_warning (otype, type, expr); | |
4711 | ||
4712 | /* If pedantic, warn for conversions between function and object | |
4713 | pointer types, except for converting a null pointer constant | |
4714 | to function pointer type. */ | |
4715 | if (pedantic | |
4716 | && TREE_CODE (type) == POINTER_TYPE | |
4717 | && TREE_CODE (otype) == POINTER_TYPE | |
4718 | && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE | |
4719 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
4720 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " | |
4721 | "conversion of function pointer to object pointer type"); | |
4722 | ||
4723 | if (pedantic | |
4724 | && TREE_CODE (type) == POINTER_TYPE | |
4725 | && TREE_CODE (otype) == POINTER_TYPE | |
4726 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE | |
4727 | && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE | |
4728 | && !null_pointer_constant_p (value)) | |
4729 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " | |
4730 | "conversion of object pointer to function pointer type"); | |
4731 | ||
4732 | ovalue = value; | |
4733 | value = convert (type, value); | |
4734 | ||
4735 | /* Ignore any integer overflow caused by the cast. */ | |
4736 | if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype)) | |
4737 | { | |
4738 | if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue)) | |
4739 | { | |
4740 | if (!TREE_OVERFLOW (value)) | |
4741 | { | |
4742 | /* Avoid clobbering a shared constant. */ | |
4743 | value = copy_node (value); | |
4744 | TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue); | |
4745 | } | |
4746 | } | |
4747 | else if (TREE_OVERFLOW (value)) | |
4748 | /* Reset VALUE's overflow flags, ensuring constant sharing. */ | |
4749 | value = build_int_cst_wide (TREE_TYPE (value), | |
4750 | TREE_INT_CST_LOW (value), | |
4751 | TREE_INT_CST_HIGH (value)); | |
4752 | } | |
4753 | } | |
4754 | ||
4755 | /* Don't let a cast be an lvalue. */ | |
4756 | if (value == expr) | |
4757 | value = non_lvalue_loc (loc, value); | |
4758 | ||
4759 | /* Don't allow the results of casting to floating-point or complex | |
4760 | types be confused with actual constants, or casts involving | |
4761 | integer and pointer types other than direct integer-to-integer | |
4762 | and integer-to-pointer be confused with integer constant | |
4763 | expressions and null pointer constants. */ | |
4764 | if (TREE_CODE (value) == REAL_CST | |
4765 | || TREE_CODE (value) == COMPLEX_CST | |
4766 | || (TREE_CODE (value) == INTEGER_CST | |
4767 | && !((TREE_CODE (expr) == INTEGER_CST | |
4768 | && INTEGRAL_TYPE_P (TREE_TYPE (expr))) | |
4769 | || TREE_CODE (expr) == REAL_CST | |
4770 | || TREE_CODE (expr) == COMPLEX_CST))) | |
4771 | value = build1 (NOP_EXPR, type, value); | |
4772 | ||
4773 | if (CAN_HAVE_LOCATION_P (value)) | |
4774 | SET_EXPR_LOCATION (value, loc); | |
4775 | return value; | |
4776 | } | |
4777 | ||
4778 | /* Interpret a cast of expression EXPR to type TYPE. LOC is the | |
4779 | location of the open paren of the cast, or the position of the cast | |
4780 | expr. */ | |
4781 | tree | |
4782 | c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr) | |
4783 | { | |
4784 | tree type; | |
4785 | tree type_expr = NULL_TREE; | |
4786 | bool type_expr_const = true; | |
4787 | tree ret; | |
4788 | int saved_wsp = warn_strict_prototypes; | |
4789 | ||
4790 | /* This avoids warnings about unprototyped casts on | |
4791 | integers. E.g. "#define SIG_DFL (void(*)())0". */ | |
4792 | if (TREE_CODE (expr) == INTEGER_CST) | |
4793 | warn_strict_prototypes = 0; | |
4794 | type = groktypename (type_name, &type_expr, &type_expr_const); | |
4795 | warn_strict_prototypes = saved_wsp; | |
4796 | ||
4797 | ret = build_c_cast (loc, type, expr); | |
4798 | if (type_expr) | |
4799 | { | |
4800 | bool inner_expr_const = true; | |
4801 | ret = c_fully_fold (ret, require_constant_value, &inner_expr_const); | |
4802 | ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret); | |
4803 | C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const | |
4804 | && inner_expr_const); | |
4805 | SET_EXPR_LOCATION (ret, loc); | |
4806 | } | |
4807 | ||
4808 | if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret)) | |
4809 | SET_EXPR_LOCATION (ret, loc); | |
4810 | ||
4811 | /* C++ does not permits types to be defined in a cast, but it | |
4812 | allows references to incomplete types. */ | |
4813 | if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef) | |
4814 | warning_at (loc, OPT_Wc___compat, | |
4815 | "defining a type in a cast is invalid in C++"); | |
4816 | ||
4817 | return ret; | |
4818 | } | |
4819 | \f | |
4820 | /* Build an assignment expression of lvalue LHS from value RHS. | |
4821 | If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which | |
4822 | may differ from TREE_TYPE (LHS) for an enum bitfield. | |
4823 | MODIFYCODE is the code for a binary operator that we use | |
4824 | to combine the old value of LHS with RHS to get the new value. | |
4825 | Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. | |
4826 | If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS, | |
4827 | which may differ from TREE_TYPE (RHS) for an enum value. | |
4828 | ||
4829 | LOCATION is the location of the MODIFYCODE operator. | |
4830 | RHS_LOC is the location of the RHS. */ | |
4831 | ||
4832 | tree | |
4833 | build_modify_expr (location_t location, tree lhs, tree lhs_origtype, | |
4834 | enum tree_code modifycode, | |
4835 | location_t rhs_loc, tree rhs, tree rhs_origtype) | |
4836 | { | |
4837 | tree result; | |
4838 | tree newrhs; | |
4839 | tree rhs_semantic_type = NULL_TREE; | |
4840 | tree lhstype = TREE_TYPE (lhs); | |
4841 | tree olhstype = lhstype; | |
4842 | bool npc; | |
4843 | ||
4844 | /* Types that aren't fully specified cannot be used in assignments. */ | |
4845 | lhs = require_complete_type (lhs); | |
4846 | ||
4847 | /* Avoid duplicate error messages from operands that had errors. */ | |
4848 | if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK) | |
4849 | return error_mark_node; | |
4850 | ||
4851 | /* For ObjC properties, defer this check. */ | |
4852 | if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) | |
4853 | return error_mark_node; | |
4854 | ||
4855 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) | |
4856 | { | |
4857 | rhs_semantic_type = TREE_TYPE (rhs); | |
4858 | rhs = TREE_OPERAND (rhs, 0); | |
4859 | } | |
4860 | ||
4861 | newrhs = rhs; | |
4862 | ||
4863 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) | |
4864 | { | |
4865 | tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs), | |
4866 | lhs_origtype, modifycode, rhs_loc, rhs, | |
4867 | rhs_origtype); | |
4868 | if (inner == error_mark_node) | |
4869 | return error_mark_node; | |
4870 | result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner), | |
4871 | C_MAYBE_CONST_EXPR_PRE (lhs), inner); | |
4872 | gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs)); | |
4873 | C_MAYBE_CONST_EXPR_NON_CONST (result) = 1; | |
4874 | protected_set_expr_location (result, location); | |
4875 | return result; | |
4876 | } | |
4877 | ||
4878 | /* If a binary op has been requested, combine the old LHS value with the RHS | |
4879 | producing the value we should actually store into the LHS. */ | |
4880 | ||
4881 | if (modifycode != NOP_EXPR) | |
4882 | { | |
4883 | lhs = c_fully_fold (lhs, false, NULL); | |
4884 | lhs = stabilize_reference (lhs); | |
4885 | newrhs = build_binary_op (location, | |
4886 | modifycode, lhs, rhs, 1); | |
4887 | ||
4888 | /* The original type of the right hand side is no longer | |
4889 | meaningful. */ | |
4890 | rhs_origtype = NULL_TREE; | |
4891 | } | |
4892 | ||
4893 | if (c_dialect_objc ()) | |
4894 | { | |
4895 | /* Check if we are modifying an Objective-C property reference; | |
4896 | if so, we need to generate setter calls. */ | |
4897 | result = objc_maybe_build_modify_expr (lhs, newrhs); | |
4898 | if (result) | |
4899 | return result; | |
4900 | ||
4901 | /* Else, do the check that we postponed for Objective-C. */ | |
4902 | if (!lvalue_or_else (location, lhs, lv_assign)) | |
4903 | return error_mark_node; | |
4904 | } | |
4905 | ||
4906 | /* Give an error for storing in something that is 'const'. */ | |
4907 | ||
4908 | if (TYPE_READONLY (lhstype) | |
4909 | || ((TREE_CODE (lhstype) == RECORD_TYPE | |
4910 | || TREE_CODE (lhstype) == UNION_TYPE) | |
4911 | && C_TYPE_FIELDS_READONLY (lhstype))) | |
4912 | { | |
4913 | readonly_error (lhs, lv_assign); | |
4914 | return error_mark_node; | |
4915 | } | |
4916 | else if (TREE_READONLY (lhs)) | |
4917 | readonly_warning (lhs, lv_assign); | |
4918 | ||
4919 | /* If storing into a structure or union member, | |
4920 | it has probably been given type `int'. | |
4921 | Compute the type that would go with | |
4922 | the actual amount of storage the member occupies. */ | |
4923 | ||
4924 | if (TREE_CODE (lhs) == COMPONENT_REF | |
4925 | && (TREE_CODE (lhstype) == INTEGER_TYPE | |
4926 | || TREE_CODE (lhstype) == BOOLEAN_TYPE | |
4927 | || TREE_CODE (lhstype) == REAL_TYPE | |
4928 | || TREE_CODE (lhstype) == ENUMERAL_TYPE)) | |
4929 | lhstype = TREE_TYPE (get_unwidened (lhs, 0)); | |
4930 | ||
4931 | /* If storing in a field that is in actuality a short or narrower than one, | |
4932 | we must store in the field in its actual type. */ | |
4933 | ||
4934 | if (lhstype != TREE_TYPE (lhs)) | |
4935 | { | |
4936 | lhs = copy_node (lhs); | |
4937 | TREE_TYPE (lhs) = lhstype; | |
4938 | } | |
4939 | ||
4940 | /* Issue -Wc++-compat warnings about an assignment to an enum type | |
4941 | when LHS does not have its original type. This happens for, | |
4942 | e.g., an enum bitfield in a struct. */ | |
4943 | if (warn_cxx_compat | |
4944 | && lhs_origtype != NULL_TREE | |
4945 | && lhs_origtype != lhstype | |
4946 | && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE) | |
4947 | { | |
4948 | tree checktype = (rhs_origtype != NULL_TREE | |
4949 | ? rhs_origtype | |
4950 | : TREE_TYPE (rhs)); | |
4951 | if (checktype != error_mark_node | |
4952 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)) | |
4953 | warning_at (location, OPT_Wc___compat, | |
4954 | "enum conversion in assignment is invalid in C++"); | |
4955 | } | |
4956 | ||
4957 | /* Convert new value to destination type. Fold it first, then | |
4958 | restore any excess precision information, for the sake of | |
4959 | conversion warnings. */ | |
4960 | ||
4961 | npc = null_pointer_constant_p (newrhs); | |
4962 | newrhs = c_fully_fold (newrhs, false, NULL); | |
4963 | if (rhs_semantic_type) | |
4964 | newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs); | |
4965 | newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype, | |
4966 | ic_assign, npc, NULL_TREE, NULL_TREE, 0); | |
4967 | if (TREE_CODE (newrhs) == ERROR_MARK) | |
4968 | return error_mark_node; | |
4969 | ||
4970 | /* Emit ObjC write barrier, if necessary. */ | |
4971 | if (c_dialect_objc () && flag_objc_gc) | |
4972 | { | |
4973 | result = objc_generate_write_barrier (lhs, modifycode, newrhs); | |
4974 | if (result) | |
4975 | { | |
4976 | protected_set_expr_location (result, location); | |
4977 | return result; | |
4978 | } | |
4979 | } | |
4980 | ||
4981 | /* Scan operands. */ | |
4982 | ||
4983 | result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs); | |
4984 | TREE_SIDE_EFFECTS (result) = 1; | |
4985 | protected_set_expr_location (result, location); | |
4986 | ||
4987 | /* If we got the LHS in a different type for storing in, | |
4988 | convert the result back to the nominal type of LHS | |
4989 | so that the value we return always has the same type | |
4990 | as the LHS argument. */ | |
4991 | ||
4992 | if (olhstype == TREE_TYPE (result)) | |
4993 | return result; | |
4994 | ||
4995 | result = convert_for_assignment (location, olhstype, result, rhs_origtype, | |
4996 | ic_assign, false, NULL_TREE, NULL_TREE, 0); | |
4997 | protected_set_expr_location (result, location); | |
4998 | return result; | |
4999 | } | |
5000 | \f | |
5001 | /* Return whether STRUCT_TYPE has an anonymous field with type TYPE. | |
5002 | This is used to implement -fplan9-extensions. */ | |
5003 | ||
5004 | static bool | |
5005 | find_anonymous_field_with_type (tree struct_type, tree type) | |
5006 | { | |
5007 | tree field; | |
5008 | bool found; | |
5009 | ||
5010 | gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE | |
5011 | || TREE_CODE (struct_type) == UNION_TYPE); | |
5012 | found = false; | |
5013 | for (field = TYPE_FIELDS (struct_type); | |
5014 | field != NULL_TREE; | |
5015 | field = TREE_CHAIN (field)) | |
5016 | { | |
5017 | if (DECL_NAME (field) == NULL | |
5018 | && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field)))) | |
5019 | { | |
5020 | if (found) | |
5021 | return false; | |
5022 | found = true; | |
5023 | } | |
5024 | else if (DECL_NAME (field) == NULL | |
5025 | && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE | |
5026 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
5027 | && find_anonymous_field_with_type (TREE_TYPE (field), type)) | |
5028 | { | |
5029 | if (found) | |
5030 | return false; | |
5031 | found = true; | |
5032 | } | |
5033 | } | |
5034 | return found; | |
5035 | } | |
5036 | ||
5037 | /* RHS is an expression whose type is pointer to struct. If there is | |
5038 | an anonymous field in RHS with type TYPE, then return a pointer to | |
5039 | that field in RHS. This is used with -fplan9-extensions. This | |
5040 | returns NULL if no conversion could be found. */ | |
5041 | ||
5042 | static tree | |
5043 | convert_to_anonymous_field (location_t location, tree type, tree rhs) | |
5044 | { | |
5045 | tree rhs_struct_type, lhs_main_type; | |
5046 | tree field, found_field; | |
5047 | bool found_sub_field; | |
5048 | tree ret; | |
5049 | ||
5050 | gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs))); | |
5051 | rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs)); | |
5052 | gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE | |
5053 | || TREE_CODE (rhs_struct_type) == UNION_TYPE); | |
5054 | ||
5055 | gcc_assert (POINTER_TYPE_P (type)); | |
5056 | lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
5057 | ||
5058 | found_field = NULL_TREE; | |
5059 | found_sub_field = false; | |
5060 | for (field = TYPE_FIELDS (rhs_struct_type); | |
5061 | field != NULL_TREE; | |
5062 | field = TREE_CHAIN (field)) | |
5063 | { | |
5064 | if (DECL_NAME (field) != NULL_TREE | |
5065 | || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE | |
5066 | && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)) | |
5067 | continue; | |
5068 | if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field)))) | |
5069 | { | |
5070 | if (found_field != NULL_TREE) | |
5071 | return NULL_TREE; | |
5072 | found_field = field; | |
5073 | } | |
5074 | else if (find_anonymous_field_with_type (TREE_TYPE (field), | |
5075 | lhs_main_type)) | |
5076 | { | |
5077 | if (found_field != NULL_TREE) | |
5078 | return NULL_TREE; | |
5079 | found_field = field; | |
5080 | found_sub_field = true; | |
5081 | } | |
5082 | } | |
5083 | ||
5084 | if (found_field == NULL_TREE) | |
5085 | return NULL_TREE; | |
5086 | ||
5087 | ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field), | |
5088 | build_fold_indirect_ref (rhs), found_field, | |
5089 | NULL_TREE); | |
5090 | ret = build_fold_addr_expr_loc (location, ret); | |
5091 | ||
5092 | if (found_sub_field) | |
5093 | { | |
5094 | ret = convert_to_anonymous_field (location, type, ret); | |
5095 | gcc_assert (ret != NULL_TREE); | |
5096 | } | |
5097 | ||
5098 | return ret; | |
5099 | } | |
5100 | ||
5101 | /* Convert value RHS to type TYPE as preparation for an assignment to | |
5102 | an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the | |
5103 | original type of RHS; this differs from TREE_TYPE (RHS) for enum | |
5104 | types. NULL_POINTER_CONSTANT says whether RHS was a null pointer | |
5105 | constant before any folding. | |
5106 | The real work of conversion is done by `convert'. | |
5107 | The purpose of this function is to generate error messages | |
5108 | for assignments that are not allowed in C. | |
5109 | ERRTYPE says whether it is argument passing, assignment, | |
5110 | initialization or return. | |
5111 | ||
5112 | LOCATION is the location of the RHS. | |
5113 | FUNCTION is a tree for the function being called. | |
5114 | PARMNUM is the number of the argument, for printing in error messages. */ | |
5115 | ||
5116 | static tree | |
5117 | convert_for_assignment (location_t location, tree type, tree rhs, | |
5118 | tree origtype, enum impl_conv errtype, | |
5119 | bool null_pointer_constant, tree fundecl, | |
5120 | tree function, int parmnum) | |
5121 | { | |
5122 | enum tree_code codel = TREE_CODE (type); | |
5123 | tree orig_rhs = rhs; | |
5124 | tree rhstype; | |
5125 | enum tree_code coder; | |
5126 | tree rname = NULL_TREE; | |
5127 | bool objc_ok = false; | |
5128 | ||
5129 | if (errtype == ic_argpass) | |
5130 | { | |
5131 | tree selector; | |
5132 | /* Change pointer to function to the function itself for | |
5133 | diagnostics. */ | |
5134 | if (TREE_CODE (function) == ADDR_EXPR | |
5135 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) | |
5136 | function = TREE_OPERAND (function, 0); | |
5137 | ||
5138 | /* Handle an ObjC selector specially for diagnostics. */ | |
5139 | selector = objc_message_selector (); | |
5140 | rname = function; | |
5141 | if (selector && parmnum > 2) | |
5142 | { | |
5143 | rname = selector; | |
5144 | parmnum -= 2; | |
5145 | } | |
5146 | } | |
5147 | ||
5148 | /* This macro is used to emit diagnostics to ensure that all format | |
5149 | strings are complete sentences, visible to gettext and checked at | |
5150 | compile time. */ | |
5151 | #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \ | |
5152 | do { \ | |
5153 | switch (errtype) \ | |
5154 | { \ | |
5155 | case ic_argpass: \ | |
5156 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \ | |
5157 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ | |
5158 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
5159 | "expected %qT but argument is of type %qT", \ | |
5160 | type, rhstype); \ | |
5161 | break; \ | |
5162 | case ic_assign: \ | |
5163 | pedwarn (LOCATION, OPT, AS); \ | |
5164 | break; \ | |
5165 | case ic_init: \ | |
5166 | pedwarn_init (LOCATION, OPT, IN); \ | |
5167 | break; \ | |
5168 | case ic_return: \ | |
5169 | pedwarn (LOCATION, OPT, RE); \ | |
5170 | break; \ | |
5171 | default: \ | |
5172 | gcc_unreachable (); \ | |
5173 | } \ | |
5174 | } while (0) | |
5175 | ||
5176 | /* This macro is used to emit diagnostics to ensure that all format | |
5177 | strings are complete sentences, visible to gettext and checked at | |
5178 | compile time. It is the same as WARN_FOR_ASSIGNMENT but with an | |
5179 | extra parameter to enumerate qualifiers. */ | |
5180 | ||
5181 | #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \ | |
5182 | do { \ | |
5183 | switch (errtype) \ | |
5184 | { \ | |
5185 | case ic_argpass: \ | |
5186 | if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \ | |
5187 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \ | |
5188 | ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \ | |
5189 | "expected %qT but argument is of type %qT", \ | |
5190 | type, rhstype); \ | |
5191 | break; \ | |
5192 | case ic_assign: \ | |
5193 | pedwarn (LOCATION, OPT, AS, QUALS); \ | |
5194 | break; \ | |
5195 | case ic_init: \ | |
5196 | pedwarn (LOCATION, OPT, IN, QUALS); \ | |
5197 | break; \ | |
5198 | case ic_return: \ | |
5199 | pedwarn (LOCATION, OPT, RE, QUALS); \ | |
5200 | break; \ | |
5201 | default: \ | |
5202 | gcc_unreachable (); \ | |
5203 | } \ | |
5204 | } while (0) | |
5205 | ||
5206 | if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) | |
5207 | rhs = TREE_OPERAND (rhs, 0); | |
5208 | ||
5209 | rhstype = TREE_TYPE (rhs); | |
5210 | coder = TREE_CODE (rhstype); | |
5211 | ||
5212 | if (coder == ERROR_MARK) | |
5213 | return error_mark_node; | |
5214 | ||
5215 | if (c_dialect_objc ()) | |
5216 | { | |
5217 | int parmno; | |
5218 | ||
5219 | switch (errtype) | |
5220 | { | |
5221 | case ic_return: | |
5222 | parmno = 0; | |
5223 | break; | |
5224 | ||
5225 | case ic_assign: | |
5226 | parmno = -1; | |
5227 | break; | |
5228 | ||
5229 | case ic_init: | |
5230 | parmno = -2; | |
5231 | break; | |
5232 | ||
5233 | default: | |
5234 | parmno = parmnum; | |
5235 | break; | |
5236 | } | |
5237 | ||
5238 | objc_ok = objc_compare_types (type, rhstype, parmno, rname); | |
5239 | } | |
5240 | ||
5241 | if (warn_cxx_compat) | |
5242 | { | |
5243 | tree checktype = origtype != NULL_TREE ? origtype : rhstype; | |
5244 | if (checktype != error_mark_node | |
5245 | && TREE_CODE (type) == ENUMERAL_TYPE | |
5246 | && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) | |
5247 | { | |
5248 | WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat, | |
5249 | G_("enum conversion when passing argument " | |
5250 | "%d of %qE is invalid in C++"), | |
5251 | G_("enum conversion in assignment is " | |
5252 | "invalid in C++"), | |
5253 | G_("enum conversion in initialization is " | |
5254 | "invalid in C++"), | |
5255 | G_("enum conversion in return is " | |
5256 | "invalid in C++")); | |
5257 | } | |
5258 | } | |
5259 | ||
5260 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) | |
5261 | return rhs; | |
5262 | ||
5263 | if (coder == VOID_TYPE) | |
5264 | { | |
5265 | /* Except for passing an argument to an unprototyped function, | |
5266 | this is a constraint violation. When passing an argument to | |
5267 | an unprototyped function, it is compile-time undefined; | |
5268 | making it a constraint in that case was rejected in | |
5269 | DR#252. */ | |
5270 | error_at (location, "void value not ignored as it ought to be"); | |
5271 | return error_mark_node; | |
5272 | } | |
5273 | rhs = require_complete_type (rhs); | |
5274 | if (rhs == error_mark_node) | |
5275 | return error_mark_node; | |
5276 | /* A non-reference type can convert to a reference. This handles | |
5277 | va_start, va_copy and possibly port built-ins. */ | |
5278 | if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) | |
5279 | { | |
5280 | if (!lvalue_p (rhs)) | |
5281 | { | |
5282 | error_at (location, "cannot pass rvalue to reference parameter"); | |
5283 | return error_mark_node; | |
5284 | } | |
5285 | if (!c_mark_addressable (rhs)) | |
5286 | return error_mark_node; | |
5287 | rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); | |
5288 | SET_EXPR_LOCATION (rhs, location); | |
5289 | ||
5290 | rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)), | |
5291 | rhs, origtype, errtype, null_pointer_constant, | |
5292 | fundecl, function, parmnum); | |
5293 | if (rhs == error_mark_node) | |
5294 | return error_mark_node; | |
5295 | ||
5296 | rhs = build1 (NOP_EXPR, type, rhs); | |
5297 | SET_EXPR_LOCATION (rhs, location); | |
5298 | return rhs; | |
5299 | } | |
5300 | /* Some types can interconvert without explicit casts. */ | |
5301 | else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE | |
5302 | && vector_types_convertible_p (type, TREE_TYPE (rhs), true)) | |
5303 | return convert (type, rhs); | |
5304 | /* Arithmetic types all interconvert, and enum is treated like int. */ | |
5305 | else if ((codel == INTEGER_TYPE || codel == REAL_TYPE | |
5306 | || codel == FIXED_POINT_TYPE | |
5307 | || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE | |
5308 | || codel == BOOLEAN_TYPE) | |
5309 | && (coder == INTEGER_TYPE || coder == REAL_TYPE | |
5310 | || coder == FIXED_POINT_TYPE | |
5311 | || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE | |
5312 | || coder == BOOLEAN_TYPE)) | |
5313 | { | |
5314 | tree ret; | |
5315 | bool save = in_late_binary_op; | |
5316 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) | |
5317 | in_late_binary_op = true; | |
5318 | ret = convert_and_check (type, orig_rhs); | |
5319 | if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) | |
5320 | in_late_binary_op = save; | |
5321 | return ret; | |
5322 | } | |
5323 | ||
5324 | /* Aggregates in different TUs might need conversion. */ | |
5325 | if ((codel == RECORD_TYPE || codel == UNION_TYPE) | |
5326 | && codel == coder | |
5327 | && comptypes (type, rhstype)) | |
5328 | return convert_and_check (type, rhs); | |
5329 | ||
5330 | /* Conversion to a transparent union or record from its member types. | |
5331 | This applies only to function arguments. */ | |
5332 | if (((codel == UNION_TYPE || codel == RECORD_TYPE) | |
5333 | && TYPE_TRANSPARENT_AGGR (type)) | |
5334 | && errtype == ic_argpass) | |
5335 | { | |
5336 | tree memb, marginal_memb = NULL_TREE; | |
5337 | ||
5338 | for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb)) | |
5339 | { | |
5340 | tree memb_type = TREE_TYPE (memb); | |
5341 | ||
5342 | if (comptypes (TYPE_MAIN_VARIANT (memb_type), | |
5343 | TYPE_MAIN_VARIANT (rhstype))) | |
5344 | break; | |
5345 | ||
5346 | if (TREE_CODE (memb_type) != POINTER_TYPE) | |
5347 | continue; | |
5348 | ||
5349 | if (coder == POINTER_TYPE) | |
5350 | { | |
5351 | tree ttl = TREE_TYPE (memb_type); | |
5352 | tree ttr = TREE_TYPE (rhstype); | |
5353 | ||
5354 | /* Any non-function converts to a [const][volatile] void * | |
5355 | and vice versa; otherwise, targets must be the same. | |
5356 | Meanwhile, the lhs target must have all the qualifiers of | |
5357 | the rhs. */ | |
5358 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
5359 | || comp_target_types (location, memb_type, rhstype)) | |
5360 | { | |
5361 | /* If this type won't generate any warnings, use it. */ | |
5362 | if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr) | |
5363 | || ((TREE_CODE (ttr) == FUNCTION_TYPE | |
5364 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5365 | ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5366 | == TYPE_QUALS (ttr)) | |
5367 | : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr)) | |
5368 | == TYPE_QUALS (ttl)))) | |
5369 | break; | |
5370 | ||
5371 | /* Keep looking for a better type, but remember this one. */ | |
5372 | if (!marginal_memb) | |
5373 | marginal_memb = memb; | |
5374 | } | |
5375 | } | |
5376 | ||
5377 | /* Can convert integer zero to any pointer type. */ | |
5378 | if (null_pointer_constant) | |
5379 | { | |
5380 | rhs = null_pointer_node; | |
5381 | break; | |
5382 | } | |
5383 | } | |
5384 | ||
5385 | if (memb || marginal_memb) | |
5386 | { | |
5387 | if (!memb) | |
5388 | { | |
5389 | /* We have only a marginally acceptable member type; | |
5390 | it needs a warning. */ | |
5391 | tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb)); | |
5392 | tree ttr = TREE_TYPE (rhstype); | |
5393 | ||
5394 | /* Const and volatile mean something different for function | |
5395 | types, so the usual warnings are not appropriate. */ | |
5396 | if (TREE_CODE (ttr) == FUNCTION_TYPE | |
5397 | && TREE_CODE (ttl) == FUNCTION_TYPE) | |
5398 | { | |
5399 | /* Because const and volatile on functions are | |
5400 | restrictions that say the function will not do | |
5401 | certain things, it is okay to use a const or volatile | |
5402 | function where an ordinary one is wanted, but not | |
5403 | vice-versa. */ | |
5404 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) | |
5405 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
5406 | WARN_FOR_QUALIFIERS (location, 0, | |
5407 | G_("passing argument %d of %qE " | |
5408 | "makes %q#v qualified function " | |
5409 | "pointer from unqualified"), | |
5410 | G_("assignment makes %q#v qualified " | |
5411 | "function pointer from " | |
5412 | "unqualified"), | |
5413 | G_("initialization makes %q#v qualified " | |
5414 | "function pointer from " | |
5415 | "unqualified"), | |
5416 | G_("return makes %q#v qualified function " | |
5417 | "pointer from unqualified"), | |
5418 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
5419 | } | |
5420 | else if (TYPE_QUALS_NO_ADDR_SPACE (ttr) | |
5421 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
5422 | WARN_FOR_QUALIFIERS (location, 0, | |
5423 | G_("passing argument %d of %qE discards " | |
5424 | "%qv qualifier from pointer target type"), | |
5425 | G_("assignment discards %qv qualifier " | |
5426 | "from pointer target type"), | |
5427 | G_("initialization discards %qv qualifier " | |
5428 | "from pointer target type"), | |
5429 | G_("return discards %qv qualifier from " | |
5430 | "pointer target type"), | |
5431 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
5432 | ||
5433 | memb = marginal_memb; | |
5434 | } | |
5435 | ||
5436 | if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)) | |
5437 | pedwarn (location, OPT_Wpedantic, | |
5438 | "ISO C prohibits argument conversion to union type"); | |
5439 | ||
5440 | rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs); | |
5441 | return build_constructor_single (type, memb, rhs); | |
5442 | } | |
5443 | } | |
5444 | ||
5445 | /* Conversions among pointers */ | |
5446 | else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) | |
5447 | && (coder == codel)) | |
5448 | { | |
5449 | tree ttl = TREE_TYPE (type); | |
5450 | tree ttr = TREE_TYPE (rhstype); | |
5451 | tree mvl = ttl; | |
5452 | tree mvr = ttr; | |
5453 | bool is_opaque_pointer; | |
5454 | int target_cmp = 0; /* Cache comp_target_types () result. */ | |
5455 | addr_space_t asl; | |
5456 | addr_space_t asr; | |
5457 | ||
5458 | if (TREE_CODE (mvl) != ARRAY_TYPE) | |
5459 | mvl = TYPE_MAIN_VARIANT (mvl); | |
5460 | if (TREE_CODE (mvr) != ARRAY_TYPE) | |
5461 | mvr = TYPE_MAIN_VARIANT (mvr); | |
5462 | /* Opaque pointers are treated like void pointers. */ | |
5463 | is_opaque_pointer = vector_targets_convertible_p (ttl, ttr); | |
5464 | ||
5465 | /* The Plan 9 compiler permits a pointer to a struct to be | |
5466 | automatically converted into a pointer to an anonymous field | |
5467 | within the struct. */ | |
5468 | if (flag_plan9_extensions | |
5469 | && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE) | |
5470 | && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE) | |
5471 | && mvl != mvr) | |
5472 | { | |
5473 | tree new_rhs = convert_to_anonymous_field (location, type, rhs); | |
5474 | if (new_rhs != NULL_TREE) | |
5475 | { | |
5476 | rhs = new_rhs; | |
5477 | rhstype = TREE_TYPE (rhs); | |
5478 | coder = TREE_CODE (rhstype); | |
5479 | ttr = TREE_TYPE (rhstype); | |
5480 | mvr = TYPE_MAIN_VARIANT (ttr); | |
5481 | } | |
5482 | } | |
5483 | ||
5484 | /* C++ does not allow the implicit conversion void* -> T*. However, | |
5485 | for the purpose of reducing the number of false positives, we | |
5486 | tolerate the special case of | |
5487 | ||
5488 | int *p = NULL; | |
5489 | ||
5490 | where NULL is typically defined in C to be '(void *) 0'. */ | |
5491 | if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl)) | |
5492 | warning_at (location, OPT_Wc___compat, | |
5493 | "request for implicit conversion " | |
5494 | "from %qT to %qT not permitted in C++", rhstype, type); | |
5495 | ||
5496 | /* See if the pointers point to incompatible address spaces. */ | |
5497 | asl = TYPE_ADDR_SPACE (ttl); | |
5498 | asr = TYPE_ADDR_SPACE (ttr); | |
5499 | if (!null_pointer_constant_p (rhs) | |
5500 | && asr != asl && !targetm.addr_space.subset_p (asr, asl)) | |
5501 | { | |
5502 | switch (errtype) | |
5503 | { | |
5504 | case ic_argpass: | |
5505 | error_at (location, "passing argument %d of %qE from pointer to " | |
5506 | "non-enclosed address space", parmnum, rname); | |
5507 | break; | |
5508 | case ic_assign: | |
5509 | error_at (location, "assignment from pointer to " | |
5510 | "non-enclosed address space"); | |
5511 | break; | |
5512 | case ic_init: | |
5513 | error_at (location, "initialization from pointer to " | |
5514 | "non-enclosed address space"); | |
5515 | break; | |
5516 | case ic_return: | |
5517 | error_at (location, "return from pointer to " | |
5518 | "non-enclosed address space"); | |
5519 | break; | |
5520 | default: | |
5521 | gcc_unreachable (); | |
5522 | } | |
5523 | return error_mark_node; | |
5524 | } | |
5525 | ||
5526 | /* Check if the right-hand side has a format attribute but the | |
5527 | left-hand side doesn't. */ | |
5528 | if (warn_suggest_attribute_format | |
5529 | && check_missing_format_attribute (type, rhstype)) | |
5530 | { | |
5531 | switch (errtype) | |
5532 | { | |
5533 | case ic_argpass: | |
5534 | warning_at (location, OPT_Wsuggest_attribute_format, | |
5535 | "argument %d of %qE might be " | |
5536 | "a candidate for a format attribute", | |
5537 | parmnum, rname); | |
5538 | break; | |
5539 | case ic_assign: | |
5540 | warning_at (location, OPT_Wsuggest_attribute_format, | |
5541 | "assignment left-hand side might be " | |
5542 | "a candidate for a format attribute"); | |
5543 | break; | |
5544 | case ic_init: | |
5545 | warning_at (location, OPT_Wsuggest_attribute_format, | |
5546 | "initialization left-hand side might be " | |
5547 | "a candidate for a format attribute"); | |
5548 | break; | |
5549 | case ic_return: | |
5550 | warning_at (location, OPT_Wsuggest_attribute_format, | |
5551 | "return type might be " | |
5552 | "a candidate for a format attribute"); | |
5553 | break; | |
5554 | default: | |
5555 | gcc_unreachable (); | |
5556 | } | |
5557 | } | |
5558 | ||
5559 | /* Any non-function converts to a [const][volatile] void * | |
5560 | and vice versa; otherwise, targets must be the same. | |
5561 | Meanwhile, the lhs target must have all the qualifiers of the rhs. */ | |
5562 | if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
5563 | || (target_cmp = comp_target_types (location, type, rhstype)) | |
5564 | || is_opaque_pointer | |
5565 | || ((c_common_unsigned_type (mvl) | |
5566 | == c_common_unsigned_type (mvr)) | |
5567 | && c_common_signed_type (mvl) | |
5568 | == c_common_signed_type (mvr))) | |
5569 | { | |
5570 | if (pedantic | |
5571 | && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5572 | || | |
5573 | (VOID_TYPE_P (ttr) | |
5574 | && !null_pointer_constant | |
5575 | && TREE_CODE (ttl) == FUNCTION_TYPE))) | |
5576 | WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic, | |
5577 | G_("ISO C forbids passing argument %d of " | |
5578 | "%qE between function pointer " | |
5579 | "and %<void *%>"), | |
5580 | G_("ISO C forbids assignment between " | |
5581 | "function pointer and %<void *%>"), | |
5582 | G_("ISO C forbids initialization between " | |
5583 | "function pointer and %<void *%>"), | |
5584 | G_("ISO C forbids return between function " | |
5585 | "pointer and %<void *%>")); | |
5586 | /* Const and volatile mean something different for function types, | |
5587 | so the usual warnings are not appropriate. */ | |
5588 | else if (TREE_CODE (ttr) != FUNCTION_TYPE | |
5589 | && TREE_CODE (ttl) != FUNCTION_TYPE) | |
5590 | { | |
5591 | if (TYPE_QUALS_NO_ADDR_SPACE (ttr) | |
5592 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttl)) | |
5593 | { | |
5594 | WARN_FOR_QUALIFIERS (location, 0, | |
5595 | G_("passing argument %d of %qE discards " | |
5596 | "%qv qualifier from pointer target type"), | |
5597 | G_("assignment discards %qv qualifier " | |
5598 | "from pointer target type"), | |
5599 | G_("initialization discards %qv qualifier " | |
5600 | "from pointer target type"), | |
5601 | G_("return discards %qv qualifier from " | |
5602 | "pointer target type"), | |
5603 | TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl)); | |
5604 | } | |
5605 | /* If this is not a case of ignoring a mismatch in signedness, | |
5606 | no warning. */ | |
5607 | else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr) | |
5608 | || target_cmp) | |
5609 | ; | |
5610 | /* If there is a mismatch, do warn. */ | |
5611 | else if (warn_pointer_sign) | |
5612 | WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign, | |
5613 | G_("pointer targets in passing argument " | |
5614 | "%d of %qE differ in signedness"), | |
5615 | G_("pointer targets in assignment " | |
5616 | "differ in signedness"), | |
5617 | G_("pointer targets in initialization " | |
5618 | "differ in signedness"), | |
5619 | G_("pointer targets in return differ " | |
5620 | "in signedness")); | |
5621 | } | |
5622 | else if (TREE_CODE (ttl) == FUNCTION_TYPE | |
5623 | && TREE_CODE (ttr) == FUNCTION_TYPE) | |
5624 | { | |
5625 | /* Because const and volatile on functions are restrictions | |
5626 | that say the function will not do certain things, | |
5627 | it is okay to use a const or volatile function | |
5628 | where an ordinary one is wanted, but not vice-versa. */ | |
5629 | if (TYPE_QUALS_NO_ADDR_SPACE (ttl) | |
5630 | & ~TYPE_QUALS_NO_ADDR_SPACE (ttr)) | |
5631 | WARN_FOR_QUALIFIERS (location, 0, | |
5632 | G_("passing argument %d of %qE makes " | |
5633 | "%q#v qualified function pointer " | |
5634 | "from unqualified"), | |
5635 | G_("assignment makes %q#v qualified function " | |
5636 | "pointer from unqualified"), | |
5637 | G_("initialization makes %q#v qualified " | |
5638 | "function pointer from unqualified"), | |
5639 | G_("return makes %q#v qualified function " | |
5640 | "pointer from unqualified"), | |
5641 | TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr)); | |
5642 | } | |
5643 | } | |
5644 | else | |
5645 | /* Avoid warning about the volatile ObjC EH puts on decls. */ | |
5646 | if (!objc_ok) | |
5647 | WARN_FOR_ASSIGNMENT (location, 0, | |
5648 | G_("passing argument %d of %qE from " | |
5649 | "incompatible pointer type"), | |
5650 | G_("assignment from incompatible pointer type"), | |
5651 | G_("initialization from incompatible " | |
5652 | "pointer type"), | |
5653 | G_("return from incompatible pointer type")); | |
5654 | ||
5655 | return convert (type, rhs); | |
5656 | } | |
5657 | else if (codel == POINTER_TYPE && coder == ARRAY_TYPE) | |
5658 | { | |
5659 | /* ??? This should not be an error when inlining calls to | |
5660 | unprototyped functions. */ | |
5661 | error_at (location, "invalid use of non-lvalue array"); | |
5662 | return error_mark_node; | |
5663 | } | |
5664 | else if (codel == POINTER_TYPE && coder == INTEGER_TYPE) | |
5665 | { | |
5666 | /* An explicit constant 0 can convert to a pointer, | |
5667 | or one that results from arithmetic, even including | |
5668 | a cast to integer type. */ | |
5669 | if (!null_pointer_constant) | |
5670 | WARN_FOR_ASSIGNMENT (location, 0, | |
5671 | G_("passing argument %d of %qE makes " | |
5672 | "pointer from integer without a cast"), | |
5673 | G_("assignment makes pointer from integer " | |
5674 | "without a cast"), | |
5675 | G_("initialization makes pointer from " | |
5676 | "integer without a cast"), | |
5677 | G_("return makes pointer from integer " | |
5678 | "without a cast")); | |
5679 | ||
5680 | return convert (type, rhs); | |
5681 | } | |
5682 | else if (codel == INTEGER_TYPE && coder == POINTER_TYPE) | |
5683 | { | |
5684 | WARN_FOR_ASSIGNMENT (location, 0, | |
5685 | G_("passing argument %d of %qE makes integer " | |
5686 | "from pointer without a cast"), | |
5687 | G_("assignment makes integer from pointer " | |
5688 | "without a cast"), | |
5689 | G_("initialization makes integer from pointer " | |
5690 | "without a cast"), | |
5691 | G_("return makes integer from pointer " | |
5692 | "without a cast")); | |
5693 | return convert (type, rhs); | |
5694 | } | |
5695 | else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE) | |
5696 | { | |
5697 | tree ret; | |
5698 | bool save = in_late_binary_op; | |
5699 | in_late_binary_op = true; | |
5700 | ret = convert (type, rhs); | |
5701 | in_late_binary_op = save; | |
5702 | return ret; | |
5703 | } | |
5704 | ||
5705 | switch (errtype) | |
5706 | { | |
5707 | case ic_argpass: | |
5708 | error_at (location, "incompatible type for argument %d of %qE", parmnum, rname); | |
5709 | inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) | |
5710 | ? DECL_SOURCE_LOCATION (fundecl) : input_location, | |
5711 | "expected %qT but argument is of type %qT", type, rhstype); | |
5712 | break; | |
5713 | case ic_assign: | |
5714 | error_at (location, "incompatible types when assigning to type %qT from " | |
5715 | "type %qT", type, rhstype); | |
5716 | break; | |
5717 | case ic_init: | |
5718 | error_at (location, | |
5719 | "incompatible types when initializing type %qT using type %qT", | |
5720 | type, rhstype); | |
5721 | break; | |
5722 | case ic_return: | |
5723 | error_at (location, | |
5724 | "incompatible types when returning type %qT but %qT was " | |
5725 | "expected", rhstype, type); | |
5726 | break; | |
5727 | default: | |
5728 | gcc_unreachable (); | |
5729 | } | |
5730 | ||
5731 | return error_mark_node; | |
5732 | } | |
5733 | \f | |
5734 | /* If VALUE is a compound expr all of whose expressions are constant, then | |
5735 | return its value. Otherwise, return error_mark_node. | |
5736 | ||
5737 | This is for handling COMPOUND_EXPRs as initializer elements | |
5738 | which is allowed with a warning when -pedantic is specified. */ | |
5739 | ||
5740 | static tree | |
5741 | valid_compound_expr_initializer (tree value, tree endtype) | |
5742 | { | |
5743 | if (TREE_CODE (value) == COMPOUND_EXPR) | |
5744 | { | |
5745 | if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype) | |
5746 | == error_mark_node) | |
5747 | return error_mark_node; | |
5748 | return valid_compound_expr_initializer (TREE_OPERAND (value, 1), | |
5749 | endtype); | |
5750 | } | |
5751 | else if (!initializer_constant_valid_p (value, endtype)) | |
5752 | return error_mark_node; | |
5753 | else | |
5754 | return value; | |
5755 | } | |
5756 | \f | |
5757 | /* Perform appropriate conversions on the initial value of a variable, | |
5758 | store it in the declaration DECL, | |
5759 | and print any error messages that are appropriate. | |
5760 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. | |
5761 | If the init is invalid, store an ERROR_MARK. | |
5762 | ||
5763 | INIT_LOC is the location of the initial value. */ | |
5764 | ||
5765 | void | |
5766 | store_init_value (location_t init_loc, tree decl, tree init, tree origtype) | |
5767 | { | |
5768 | tree value, type; | |
5769 | bool npc = false; | |
5770 | ||
5771 | /* If variable's type was invalidly declared, just ignore it. */ | |
5772 | ||
5773 | type = TREE_TYPE (decl); | |
5774 | if (TREE_CODE (type) == ERROR_MARK) | |
5775 | return; | |
5776 | ||
5777 | /* Digest the specified initializer into an expression. */ | |
5778 | ||
5779 | if (init) | |
5780 | npc = null_pointer_constant_p (init); | |
5781 | value = digest_init (init_loc, type, init, origtype, npc, | |
5782 | true, TREE_STATIC (decl)); | |
5783 | ||
5784 | /* Store the expression if valid; else report error. */ | |
5785 | ||
5786 | if (!in_system_header | |
5787 | && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl)) | |
5788 | warning (OPT_Wtraditional, "traditional C rejects automatic " | |
5789 | "aggregate initialization"); | |
5790 | ||
5791 | DECL_INITIAL (decl) = value; | |
5792 | ||
5793 | /* ANSI wants warnings about out-of-range constant initializers. */ | |
5794 | STRIP_TYPE_NOPS (value); | |
5795 | if (TREE_STATIC (decl)) | |
5796 | constant_expression_warning (value); | |
5797 | ||
5798 | /* Check if we need to set array size from compound literal size. */ | |
5799 | if (TREE_CODE (type) == ARRAY_TYPE | |
5800 | && TYPE_DOMAIN (type) == 0 | |
5801 | && value != error_mark_node) | |
5802 | { | |
5803 | tree inside_init = init; | |
5804 | ||
5805 | STRIP_TYPE_NOPS (inside_init); | |
5806 | inside_init = fold (inside_init); | |
5807 | ||
5808 | if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
5809 | { | |
5810 | tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init); | |
5811 | ||
5812 | if (TYPE_DOMAIN (TREE_TYPE (cldecl))) | |
5813 | { | |
5814 | /* For int foo[] = (int [3]){1}; we need to set array size | |
5815 | now since later on array initializer will be just the | |
5816 | brace enclosed list of the compound literal. */ | |
5817 | tree etype = strip_array_types (TREE_TYPE (decl)); | |
5818 | type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); | |
5819 | TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl)); | |
5820 | layout_type (type); | |
5821 | layout_decl (cldecl, 0); | |
5822 | TREE_TYPE (decl) | |
5823 | = c_build_qualified_type (type, TYPE_QUALS (etype)); | |
5824 | } | |
5825 | } | |
5826 | } | |
5827 | } | |
5828 | \f | |
5829 | /* Methods for storing and printing names for error messages. */ | |
5830 | ||
5831 | /* Implement a spelling stack that allows components of a name to be pushed | |
5832 | and popped. Each element on the stack is this structure. */ | |
5833 | ||
5834 | struct spelling | |
5835 | { | |
5836 | int kind; | |
5837 | union | |
5838 | { | |
5839 | unsigned HOST_WIDE_INT i; | |
5840 | const char *s; | |
5841 | } u; | |
5842 | }; | |
5843 | ||
5844 | #define SPELLING_STRING 1 | |
5845 | #define SPELLING_MEMBER 2 | |
5846 | #define SPELLING_BOUNDS 3 | |
5847 | ||
5848 | static struct spelling *spelling; /* Next stack element (unused). */ | |
5849 | static struct spelling *spelling_base; /* Spelling stack base. */ | |
5850 | static int spelling_size; /* Size of the spelling stack. */ | |
5851 | ||
5852 | /* Macros to save and restore the spelling stack around push_... functions. | |
5853 | Alternative to SAVE_SPELLING_STACK. */ | |
5854 | ||
5855 | #define SPELLING_DEPTH() (spelling - spelling_base) | |
5856 | #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH)) | |
5857 | ||
5858 | /* Push an element on the spelling stack with type KIND and assign VALUE | |
5859 | to MEMBER. */ | |
5860 | ||
5861 | #define PUSH_SPELLING(KIND, VALUE, MEMBER) \ | |
5862 | { \ | |
5863 | int depth = SPELLING_DEPTH (); \ | |
5864 | \ | |
5865 | if (depth >= spelling_size) \ | |
5866 | { \ | |
5867 | spelling_size += 10; \ | |
5868 | spelling_base = XRESIZEVEC (struct spelling, spelling_base, \ | |
5869 | spelling_size); \ | |
5870 | RESTORE_SPELLING_DEPTH (depth); \ | |
5871 | } \ | |
5872 | \ | |
5873 | spelling->kind = (KIND); \ | |
5874 | spelling->MEMBER = (VALUE); \ | |
5875 | spelling++; \ | |
5876 | } | |
5877 | ||
5878 | /* Push STRING on the stack. Printed literally. */ | |
5879 | ||
5880 | static void | |
5881 | push_string (const char *string) | |
5882 | { | |
5883 | PUSH_SPELLING (SPELLING_STRING, string, u.s); | |
5884 | } | |
5885 | ||
5886 | /* Push a member name on the stack. Printed as '.' STRING. */ | |
5887 | ||
5888 | static void | |
5889 | push_member_name (tree decl) | |
5890 | { | |
5891 | const char *const string | |
5892 | = (DECL_NAME (decl) | |
5893 | ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))) | |
5894 | : _("<anonymous>")); | |
5895 | PUSH_SPELLING (SPELLING_MEMBER, string, u.s); | |
5896 | } | |
5897 | ||
5898 | /* Push an array bounds on the stack. Printed as [BOUNDS]. */ | |
5899 | ||
5900 | static void | |
5901 | push_array_bounds (unsigned HOST_WIDE_INT bounds) | |
5902 | { | |
5903 | PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i); | |
5904 | } | |
5905 | ||
5906 | /* Compute the maximum size in bytes of the printed spelling. */ | |
5907 | ||
5908 | static int | |
5909 | spelling_length (void) | |
5910 | { | |
5911 | int size = 0; | |
5912 | struct spelling *p; | |
5913 | ||
5914 | for (p = spelling_base; p < spelling; p++) | |
5915 | { | |
5916 | if (p->kind == SPELLING_BOUNDS) | |
5917 | size += 25; | |
5918 | else | |
5919 | size += strlen (p->u.s) + 1; | |
5920 | } | |
5921 | ||
5922 | return size; | |
5923 | } | |
5924 | ||
5925 | /* Print the spelling to BUFFER and return it. */ | |
5926 | ||
5927 | static char * | |
5928 | print_spelling (char *buffer) | |
5929 | { | |
5930 | char *d = buffer; | |
5931 | struct spelling *p; | |
5932 | ||
5933 | for (p = spelling_base; p < spelling; p++) | |
5934 | if (p->kind == SPELLING_BOUNDS) | |
5935 | { | |
5936 | sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i); | |
5937 | d += strlen (d); | |
5938 | } | |
5939 | else | |
5940 | { | |
5941 | const char *s; | |
5942 | if (p->kind == SPELLING_MEMBER) | |
5943 | *d++ = '.'; | |
5944 | for (s = p->u.s; (*d = *s++); d++) | |
5945 | ; | |
5946 | } | |
5947 | *d++ = '\0'; | |
5948 | return buffer; | |
5949 | } | |
5950 | ||
5951 | /* Issue an error message for a bad initializer component. | |
5952 | GMSGID identifies the message. | |
5953 | The component name is taken from the spelling stack. */ | |
5954 | ||
5955 | void | |
5956 | error_init (const char *gmsgid) | |
5957 | { | |
5958 | char *ofwhat; | |
5959 | ||
5960 | /* The gmsgid may be a format string with %< and %>. */ | |
5961 | error (gmsgid); | |
5962 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); | |
5963 | if (*ofwhat) | |
5964 | error ("(near initialization for %qs)", ofwhat); | |
5965 | } | |
5966 | ||
5967 | /* Issue a pedantic warning for a bad initializer component. OPT is | |
5968 | the option OPT_* (from options.h) controlling this warning or 0 if | |
5969 | it is unconditionally given. GMSGID identifies the message. The | |
5970 | component name is taken from the spelling stack. */ | |
5971 | ||
5972 | void | |
5973 | pedwarn_init (location_t location, int opt, const char *gmsgid) | |
5974 | { | |
5975 | char *ofwhat; | |
5976 | ||
5977 | /* The gmsgid may be a format string with %< and %>. */ | |
5978 | pedwarn (location, opt, gmsgid); | |
5979 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); | |
5980 | if (*ofwhat) | |
5981 | pedwarn (location, opt, "(near initialization for %qs)", ofwhat); | |
5982 | } | |
5983 | ||
5984 | /* Issue a warning for a bad initializer component. | |
5985 | ||
5986 | OPT is the OPT_W* value corresponding to the warning option that | |
5987 | controls this warning. GMSGID identifies the message. The | |
5988 | component name is taken from the spelling stack. */ | |
5989 | ||
5990 | static void | |
5991 | warning_init (int opt, const char *gmsgid) | |
5992 | { | |
5993 | char *ofwhat; | |
5994 | ||
5995 | /* The gmsgid may be a format string with %< and %>. */ | |
5996 | warning (opt, gmsgid); | |
5997 | ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); | |
5998 | if (*ofwhat) | |
5999 | warning (opt, "(near initialization for %qs)", ofwhat); | |
6000 | } | |
6001 | \f | |
6002 | /* If TYPE is an array type and EXPR is a parenthesized string | |
6003 | constant, warn if pedantic that EXPR is being used to initialize an | |
6004 | object of type TYPE. */ | |
6005 | ||
6006 | void | |
6007 | maybe_warn_string_init (tree type, struct c_expr expr) | |
6008 | { | |
6009 | if (pedantic | |
6010 | && TREE_CODE (type) == ARRAY_TYPE | |
6011 | && TREE_CODE (expr.value) == STRING_CST | |
6012 | && expr.original_code != STRING_CST) | |
6013 | pedwarn_init (input_location, OPT_Wpedantic, | |
6014 | "array initialized from parenthesized string constant"); | |
6015 | } | |
6016 | ||
6017 | /* Digest the parser output INIT as an initializer for type TYPE. | |
6018 | Return a C expression of type TYPE to represent the initial value. | |
6019 | ||
6020 | If ORIGTYPE is not NULL_TREE, it is the original type of INIT. | |
6021 | ||
6022 | NULL_POINTER_CONSTANT is true if INIT is a null pointer constant. | |
6023 | ||
6024 | If INIT is a string constant, STRICT_STRING is true if it is | |
6025 | unparenthesized or we should not warn here for it being parenthesized. | |
6026 | For other types of INIT, STRICT_STRING is not used. | |
6027 | ||
6028 | INIT_LOC is the location of the INIT. | |
6029 | ||
6030 | REQUIRE_CONSTANT requests an error if non-constant initializers or | |
6031 | elements are seen. */ | |
6032 | ||
6033 | static tree | |
6034 | digest_init (location_t init_loc, tree type, tree init, tree origtype, | |
6035 | bool null_pointer_constant, bool strict_string, | |
6036 | int require_constant) | |
6037 | { | |
6038 | enum tree_code code = TREE_CODE (type); | |
6039 | tree inside_init = init; | |
6040 | tree semantic_type = NULL_TREE; | |
6041 | bool maybe_const = true; | |
6042 | ||
6043 | if (type == error_mark_node | |
6044 | || !init | |
6045 | || init == error_mark_node | |
6046 | || TREE_TYPE (init) == error_mark_node) | |
6047 | return error_mark_node; | |
6048 | ||
6049 | STRIP_TYPE_NOPS (inside_init); | |
6050 | ||
6051 | if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR) | |
6052 | { | |
6053 | semantic_type = TREE_TYPE (inside_init); | |
6054 | inside_init = TREE_OPERAND (inside_init, 0); | |
6055 | } | |
6056 | inside_init = c_fully_fold (inside_init, require_constant, &maybe_const); | |
6057 | inside_init = decl_constant_value_for_optimization (inside_init); | |
6058 | ||
6059 | /* Initialization of an array of chars from a string constant | |
6060 | optionally enclosed in braces. */ | |
6061 | ||
6062 | if (code == ARRAY_TYPE && inside_init | |
6063 | && TREE_CODE (inside_init) == STRING_CST) | |
6064 | { | |
6065 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
6066 | /* Note that an array could be both an array of character type | |
6067 | and an array of wchar_t if wchar_t is signed char or unsigned | |
6068 | char. */ | |
6069 | bool char_array = (typ1 == char_type_node | |
6070 | || typ1 == signed_char_type_node | |
6071 | || typ1 == unsigned_char_type_node); | |
6072 | bool wchar_array = !!comptypes (typ1, wchar_type_node); | |
6073 | bool char16_array = !!comptypes (typ1, char16_type_node); | |
6074 | bool char32_array = !!comptypes (typ1, char32_type_node); | |
6075 | ||
6076 | if (char_array || wchar_array || char16_array || char32_array) | |
6077 | { | |
6078 | struct c_expr expr; | |
6079 | tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); | |
6080 | expr.value = inside_init; | |
6081 | expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); | |
6082 | expr.original_type = NULL; | |
6083 | maybe_warn_string_init (type, expr); | |
6084 | ||
6085 | if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type))) | |
6086 | pedwarn_init (init_loc, OPT_Wpedantic, | |
6087 | "initialization of a flexible array member"); | |
6088 | ||
6089 | if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6090 | TYPE_MAIN_VARIANT (type))) | |
6091 | return inside_init; | |
6092 | ||
6093 | if (char_array) | |
6094 | { | |
6095 | if (typ2 != char_type_node) | |
6096 | { | |
6097 | error_init ("char-array initialized from wide string"); | |
6098 | return error_mark_node; | |
6099 | } | |
6100 | } | |
6101 | else | |
6102 | { | |
6103 | if (typ2 == char_type_node) | |
6104 | { | |
6105 | error_init ("wide character array initialized from non-wide " | |
6106 | "string"); | |
6107 | return error_mark_node; | |
6108 | } | |
6109 | else if (!comptypes(typ1, typ2)) | |
6110 | { | |
6111 | error_init ("wide character array initialized from " | |
6112 | "incompatible wide string"); | |
6113 | return error_mark_node; | |
6114 | } | |
6115 | } | |
6116 | ||
6117 | TREE_TYPE (inside_init) = type; | |
6118 | if (TYPE_DOMAIN (type) != 0 | |
6119 | && TYPE_SIZE (type) != 0 | |
6120 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) | |
6121 | { | |
6122 | unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); | |
6123 | ||
6124 | /* Subtract the size of a single (possibly wide) character | |
6125 | because it's ok to ignore the terminating null char | |
6126 | that is counted in the length of the constant. */ | |
6127 | if (0 > compare_tree_int (TYPE_SIZE_UNIT (type), | |
6128 | (len | |
6129 | - (TYPE_PRECISION (typ1) | |
6130 | / BITS_PER_UNIT)))) | |
6131 | pedwarn_init (init_loc, 0, | |
6132 | ("initializer-string for array of chars " | |
6133 | "is too long")); | |
6134 | else if (warn_cxx_compat | |
6135 | && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len)) | |
6136 | warning_at (init_loc, OPT_Wc___compat, | |
6137 | ("initializer-string for array chars " | |
6138 | "is too long for C++")); | |
6139 | } | |
6140 | ||
6141 | return inside_init; | |
6142 | } | |
6143 | else if (INTEGRAL_TYPE_P (typ1)) | |
6144 | { | |
6145 | error_init ("array of inappropriate type initialized " | |
6146 | "from string constant"); | |
6147 | return error_mark_node; | |
6148 | } | |
6149 | } | |
6150 | ||
6151 | /* Build a VECTOR_CST from a *constant* vector constructor. If the | |
6152 | vector constructor is not constant (e.g. {1,2,3,foo()}) then punt | |
6153 | below and handle as a constructor. */ | |
6154 | if (code == VECTOR_TYPE | |
6155 | && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE | |
6156 | && vector_types_convertible_p (TREE_TYPE (inside_init), type, true) | |
6157 | && TREE_CONSTANT (inside_init)) | |
6158 | { | |
6159 | if (TREE_CODE (inside_init) == VECTOR_CST | |
6160 | && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6161 | TYPE_MAIN_VARIANT (type))) | |
6162 | return inside_init; | |
6163 | ||
6164 | if (TREE_CODE (inside_init) == CONSTRUCTOR) | |
6165 | { | |
6166 | unsigned HOST_WIDE_INT ix; | |
6167 | tree value; | |
6168 | bool constant_p = true; | |
6169 | ||
6170 | /* Iterate through elements and check if all constructor | |
6171 | elements are *_CSTs. */ | |
6172 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value) | |
6173 | if (!CONSTANT_CLASS_P (value)) | |
6174 | { | |
6175 | constant_p = false; | |
6176 | break; | |
6177 | } | |
6178 | ||
6179 | if (constant_p) | |
6180 | return build_vector_from_ctor (type, | |
6181 | CONSTRUCTOR_ELTS (inside_init)); | |
6182 | } | |
6183 | } | |
6184 | ||
6185 | if (warn_sequence_point) | |
6186 | verify_sequence_points (inside_init); | |
6187 | ||
6188 | /* Any type can be initialized | |
6189 | from an expression of the same type, optionally with braces. */ | |
6190 | ||
6191 | if (inside_init && TREE_TYPE (inside_init) != 0 | |
6192 | && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)), | |
6193 | TYPE_MAIN_VARIANT (type)) | |
6194 | || (code == ARRAY_TYPE | |
6195 | && comptypes (TREE_TYPE (inside_init), type)) | |
6196 | || (code == VECTOR_TYPE | |
6197 | && comptypes (TREE_TYPE (inside_init), type)) | |
6198 | || (code == POINTER_TYPE | |
6199 | && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE | |
6200 | && comptypes (TREE_TYPE (TREE_TYPE (inside_init)), | |
6201 | TREE_TYPE (type))))) | |
6202 | { | |
6203 | if (code == POINTER_TYPE) | |
6204 | { | |
6205 | if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE) | |
6206 | { | |
6207 | if (TREE_CODE (inside_init) == STRING_CST | |
6208 | || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
6209 | inside_init = array_to_pointer_conversion | |
6210 | (init_loc, inside_init); | |
6211 | else | |
6212 | { | |
6213 | error_init ("invalid use of non-lvalue array"); | |
6214 | return error_mark_node; | |
6215 | } | |
6216 | } | |
6217 | } | |
6218 | ||
6219 | if (code == VECTOR_TYPE) | |
6220 | /* Although the types are compatible, we may require a | |
6221 | conversion. */ | |
6222 | inside_init = convert (type, inside_init); | |
6223 | ||
6224 | if (require_constant | |
6225 | && (code == VECTOR_TYPE || !flag_isoc99) | |
6226 | && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR) | |
6227 | { | |
6228 | /* As an extension, allow initializing objects with static storage | |
6229 | duration with compound literals (which are then treated just as | |
6230 | the brace enclosed list they contain). Also allow this for | |
6231 | vectors, as we can only assign them with compound literals. */ | |
6232 | tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init); | |
6233 | inside_init = DECL_INITIAL (decl); | |
6234 | } | |
6235 | ||
6236 | if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST | |
6237 | && TREE_CODE (inside_init) != CONSTRUCTOR) | |
6238 | { | |
6239 | error_init ("array initialized from non-constant array expression"); | |
6240 | return error_mark_node; | |
6241 | } | |
6242 | ||
6243 | /* Compound expressions can only occur here if -Wpedantic or | |
6244 | -pedantic-errors is specified. In the later case, we always want | |
6245 | an error. In the former case, we simply want a warning. */ | |
6246 | if (require_constant && pedantic | |
6247 | && TREE_CODE (inside_init) == COMPOUND_EXPR) | |
6248 | { | |
6249 | inside_init | |
6250 | = valid_compound_expr_initializer (inside_init, | |
6251 | TREE_TYPE (inside_init)); | |
6252 | if (inside_init == error_mark_node) | |
6253 | error_init ("initializer element is not constant"); | |
6254 | else | |
6255 | pedwarn_init (init_loc, OPT_Wpedantic, | |
6256 | "initializer element is not constant"); | |
6257 | if (flag_pedantic_errors) | |
6258 | inside_init = error_mark_node; | |
6259 | } | |
6260 | else if (require_constant | |
6261 | && !initializer_constant_valid_p (inside_init, | |
6262 | TREE_TYPE (inside_init))) | |
6263 | { | |
6264 | error_init ("initializer element is not constant"); | |
6265 | inside_init = error_mark_node; | |
6266 | } | |
6267 | else if (require_constant && !maybe_const) | |
6268 | pedwarn_init (init_loc, 0, | |
6269 | "initializer element is not a constant expression"); | |
6270 | ||
6271 | /* Added to enable additional -Wsuggest-attribute=format warnings. */ | |
6272 | if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE) | |
6273 | inside_init = convert_for_assignment (init_loc, type, inside_init, | |
6274 | origtype, | |
6275 | ic_init, null_pointer_constant, | |
6276 | NULL_TREE, NULL_TREE, 0); | |
6277 | return inside_init; | |
6278 | } | |
6279 | ||
6280 | /* Handle scalar types, including conversions. */ | |
6281 | ||
6282 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE | |
6283 | || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE | |
6284 | || code == COMPLEX_TYPE || code == VECTOR_TYPE) | |
6285 | { | |
6286 | if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE | |
6287 | && (TREE_CODE (init) == STRING_CST | |
6288 | || TREE_CODE (init) == COMPOUND_LITERAL_EXPR)) | |
6289 | inside_init = init = array_to_pointer_conversion (init_loc, init); | |
6290 | if (semantic_type) | |
6291 | inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
6292 | inside_init); | |
6293 | inside_init | |
6294 | = convert_for_assignment (init_loc, type, inside_init, origtype, | |
6295 | ic_init, null_pointer_constant, | |
6296 | NULL_TREE, NULL_TREE, 0); | |
6297 | ||
6298 | /* Check to see if we have already given an error message. */ | |
6299 | if (inside_init == error_mark_node) | |
6300 | ; | |
6301 | else if (require_constant && !TREE_CONSTANT (inside_init)) | |
6302 | { | |
6303 | error_init ("initializer element is not constant"); | |
6304 | inside_init = error_mark_node; | |
6305 | } | |
6306 | else if (require_constant | |
6307 | && !initializer_constant_valid_p (inside_init, | |
6308 | TREE_TYPE (inside_init))) | |
6309 | { | |
6310 | error_init ("initializer element is not computable at load time"); | |
6311 | inside_init = error_mark_node; | |
6312 | } | |
6313 | else if (require_constant && !maybe_const) | |
6314 | pedwarn_init (init_loc, 0, | |
6315 | "initializer element is not a constant expression"); | |
6316 | ||
6317 | return inside_init; | |
6318 | } | |
6319 | ||
6320 | /* Come here only for records and arrays. */ | |
6321 | ||
6322 | if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) | |
6323 | { | |
6324 | error_init ("variable-sized object may not be initialized"); | |
6325 | return error_mark_node; | |
6326 | } | |
6327 | ||
6328 | error_init ("invalid initializer"); | |
6329 | return error_mark_node; | |
6330 | } | |
6331 | \f | |
6332 | /* Handle initializers that use braces. */ | |
6333 | ||
6334 | /* Type of object we are accumulating a constructor for. | |
6335 | This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */ | |
6336 | static tree constructor_type; | |
6337 | ||
6338 | /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields | |
6339 | left to fill. */ | |
6340 | static tree constructor_fields; | |
6341 | ||
6342 | /* For an ARRAY_TYPE, this is the specified index | |
6343 | at which to store the next element we get. */ | |
6344 | static tree constructor_index; | |
6345 | ||
6346 | /* For an ARRAY_TYPE, this is the maximum index. */ | |
6347 | static tree constructor_max_index; | |
6348 | ||
6349 | /* For a RECORD_TYPE, this is the first field not yet written out. */ | |
6350 | static tree constructor_unfilled_fields; | |
6351 | ||
6352 | /* For an ARRAY_TYPE, this is the index of the first element | |
6353 | not yet written out. */ | |
6354 | static tree constructor_unfilled_index; | |
6355 | ||
6356 | /* In a RECORD_TYPE, the byte index of the next consecutive field. | |
6357 | This is so we can generate gaps between fields, when appropriate. */ | |
6358 | static tree constructor_bit_index; | |
6359 | ||
6360 | /* If we are saving up the elements rather than allocating them, | |
6361 | this is the list of elements so far (in reverse order, | |
6362 | most recent first). */ | |
6363 | static vec<constructor_elt, va_gc> *constructor_elements; | |
6364 | ||
6365 | /* 1 if constructor should be incrementally stored into a constructor chain, | |
6366 | 0 if all the elements should be kept in AVL tree. */ | |
6367 | static int constructor_incremental; | |
6368 | ||
6369 | /* 1 if so far this constructor's elements are all compile-time constants. */ | |
6370 | static int constructor_constant; | |
6371 | ||
6372 | /* 1 if so far this constructor's elements are all valid address constants. */ | |
6373 | static int constructor_simple; | |
6374 | ||
6375 | /* 1 if this constructor has an element that cannot be part of a | |
6376 | constant expression. */ | |
6377 | static int constructor_nonconst; | |
6378 | ||
6379 | /* 1 if this constructor is erroneous so far. */ | |
6380 | static int constructor_erroneous; | |
6381 | ||
6382 | /* Structure for managing pending initializer elements, organized as an | |
6383 | AVL tree. */ | |
6384 | ||
6385 | struct init_node | |
6386 | { | |
6387 | struct init_node *left, *right; | |
6388 | struct init_node *parent; | |
6389 | int balance; | |
6390 | tree purpose; | |
6391 | tree value; | |
6392 | tree origtype; | |
6393 | }; | |
6394 | ||
6395 | /* Tree of pending elements at this constructor level. | |
6396 | These are elements encountered out of order | |
6397 | which belong at places we haven't reached yet in actually | |
6398 | writing the output. | |
6399 | Will never hold tree nodes across GC runs. */ | |
6400 | static struct init_node *constructor_pending_elts; | |
6401 | ||
6402 | /* The SPELLING_DEPTH of this constructor. */ | |
6403 | static int constructor_depth; | |
6404 | ||
6405 | /* DECL node for which an initializer is being read. | |
6406 | 0 means we are reading a constructor expression | |
6407 | such as (struct foo) {...}. */ | |
6408 | static tree constructor_decl; | |
6409 | ||
6410 | /* Nonzero if this is an initializer for a top-level decl. */ | |
6411 | static int constructor_top_level; | |
6412 | ||
6413 | /* Nonzero if there were any member designators in this initializer. */ | |
6414 | static int constructor_designated; | |
6415 | ||
6416 | /* Nesting depth of designator list. */ | |
6417 | static int designator_depth; | |
6418 | ||
6419 | /* Nonzero if there were diagnosed errors in this designator list. */ | |
6420 | static int designator_erroneous; | |
6421 | ||
6422 | \f | |
6423 | /* This stack has a level for each implicit or explicit level of | |
6424 | structuring in the initializer, including the outermost one. It | |
6425 | saves the values of most of the variables above. */ | |
6426 | ||
6427 | struct constructor_range_stack; | |
6428 | ||
6429 | struct constructor_stack | |
6430 | { | |
6431 | struct constructor_stack *next; | |
6432 | tree type; | |
6433 | tree fields; | |
6434 | tree index; | |
6435 | tree max_index; | |
6436 | tree unfilled_index; | |
6437 | tree unfilled_fields; | |
6438 | tree bit_index; | |
6439 | vec<constructor_elt, va_gc> *elements; | |
6440 | struct init_node *pending_elts; | |
6441 | int offset; | |
6442 | int depth; | |
6443 | /* If value nonzero, this value should replace the entire | |
6444 | constructor at this level. */ | |
6445 | struct c_expr replacement_value; | |
6446 | struct constructor_range_stack *range_stack; | |
6447 | char constant; | |
6448 | char simple; | |
6449 | char nonconst; | |
6450 | char implicit; | |
6451 | char erroneous; | |
6452 | char outer; | |
6453 | char incremental; | |
6454 | char designated; | |
6455 | }; | |
6456 | ||
6457 | static struct constructor_stack *constructor_stack; | |
6458 | ||
6459 | /* This stack represents designators from some range designator up to | |
6460 | the last designator in the list. */ | |
6461 | ||
6462 | struct constructor_range_stack | |
6463 | { | |
6464 | struct constructor_range_stack *next, *prev; | |
6465 | struct constructor_stack *stack; | |
6466 | tree range_start; | |
6467 | tree index; | |
6468 | tree range_end; | |
6469 | tree fields; | |
6470 | }; | |
6471 | ||
6472 | static struct constructor_range_stack *constructor_range_stack; | |
6473 | ||
6474 | /* This stack records separate initializers that are nested. | |
6475 | Nested initializers can't happen in ANSI C, but GNU C allows them | |
6476 | in cases like { ... (struct foo) { ... } ... }. */ | |
6477 | ||
6478 | struct initializer_stack | |
6479 | { | |
6480 | struct initializer_stack *next; | |
6481 | tree decl; | |
6482 | struct constructor_stack *constructor_stack; | |
6483 | struct constructor_range_stack *constructor_range_stack; | |
6484 | vec<constructor_elt, va_gc> *elements; | |
6485 | struct spelling *spelling; | |
6486 | struct spelling *spelling_base; | |
6487 | int spelling_size; | |
6488 | char top_level; | |
6489 | char require_constant_value; | |
6490 | char require_constant_elements; | |
6491 | }; | |
6492 | ||
6493 | static struct initializer_stack *initializer_stack; | |
6494 | \f | |
6495 | /* Prepare to parse and output the initializer for variable DECL. */ | |
6496 | ||
6497 | void | |
6498 | start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level) | |
6499 | { | |
6500 | const char *locus; | |
6501 | struct initializer_stack *p = XNEW (struct initializer_stack); | |
6502 | ||
6503 | p->decl = constructor_decl; | |
6504 | p->require_constant_value = require_constant_value; | |
6505 | p->require_constant_elements = require_constant_elements; | |
6506 | p->constructor_stack = constructor_stack; | |
6507 | p->constructor_range_stack = constructor_range_stack; | |
6508 | p->elements = constructor_elements; | |
6509 | p->spelling = spelling; | |
6510 | p->spelling_base = spelling_base; | |
6511 | p->spelling_size = spelling_size; | |
6512 | p->top_level = constructor_top_level; | |
6513 | p->next = initializer_stack; | |
6514 | initializer_stack = p; | |
6515 | ||
6516 | constructor_decl = decl; | |
6517 | constructor_designated = 0; | |
6518 | constructor_top_level = top_level; | |
6519 | ||
6520 | if (decl != 0 && decl != error_mark_node) | |
6521 | { | |
6522 | require_constant_value = TREE_STATIC (decl); | |
6523 | require_constant_elements | |
6524 | = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99)) | |
6525 | /* For a scalar, you can always use any value to initialize, | |
6526 | even within braces. */ | |
6527 | && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
6528 | || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
6529 | || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE | |
6530 | || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE)); | |
6531 | locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))); | |
6532 | } | |
6533 | else | |
6534 | { | |
6535 | require_constant_value = 0; | |
6536 | require_constant_elements = 0; | |
6537 | locus = _("(anonymous)"); | |
6538 | } | |
6539 | ||
6540 | constructor_stack = 0; | |
6541 | constructor_range_stack = 0; | |
6542 | ||
6543 | missing_braces_mentioned = 0; | |
6544 | ||
6545 | spelling_base = 0; | |
6546 | spelling_size = 0; | |
6547 | RESTORE_SPELLING_DEPTH (0); | |
6548 | ||
6549 | if (locus) | |
6550 | push_string (locus); | |
6551 | } | |
6552 | ||
6553 | void | |
6554 | finish_init (void) | |
6555 | { | |
6556 | struct initializer_stack *p = initializer_stack; | |
6557 | ||
6558 | /* Free the whole constructor stack of this initializer. */ | |
6559 | while (constructor_stack) | |
6560 | { | |
6561 | struct constructor_stack *q = constructor_stack; | |
6562 | constructor_stack = q->next; | |
6563 | free (q); | |
6564 | } | |
6565 | ||
6566 | gcc_assert (!constructor_range_stack); | |
6567 | ||
6568 | /* Pop back to the data of the outer initializer (if any). */ | |
6569 | free (spelling_base); | |
6570 | ||
6571 | constructor_decl = p->decl; | |
6572 | require_constant_value = p->require_constant_value; | |
6573 | require_constant_elements = p->require_constant_elements; | |
6574 | constructor_stack = p->constructor_stack; | |
6575 | constructor_range_stack = p->constructor_range_stack; | |
6576 | constructor_elements = p->elements; | |
6577 | spelling = p->spelling; | |
6578 | spelling_base = p->spelling_base; | |
6579 | spelling_size = p->spelling_size; | |
6580 | constructor_top_level = p->top_level; | |
6581 | initializer_stack = p->next; | |
6582 | free (p); | |
6583 | } | |
6584 | \f | |
6585 | /* Call here when we see the initializer is surrounded by braces. | |
6586 | This is instead of a call to push_init_level; | |
6587 | it is matched by a call to pop_init_level. | |
6588 | ||
6589 | TYPE is the type to initialize, for a constructor expression. | |
6590 | For an initializer for a decl, TYPE is zero. */ | |
6591 | ||
6592 | void | |
6593 | really_start_incremental_init (tree type) | |
6594 | { | |
6595 | struct constructor_stack *p = XNEW (struct constructor_stack); | |
6596 | ||
6597 | if (type == 0) | |
6598 | type = TREE_TYPE (constructor_decl); | |
6599 | ||
6600 | if (TREE_CODE (type) == VECTOR_TYPE | |
6601 | && TYPE_VECTOR_OPAQUE (type)) | |
6602 | error ("opaque vector types cannot be initialized"); | |
6603 | ||
6604 | p->type = constructor_type; | |
6605 | p->fields = constructor_fields; | |
6606 | p->index = constructor_index; | |
6607 | p->max_index = constructor_max_index; | |
6608 | p->unfilled_index = constructor_unfilled_index; | |
6609 | p->unfilled_fields = constructor_unfilled_fields; | |
6610 | p->bit_index = constructor_bit_index; | |
6611 | p->elements = constructor_elements; | |
6612 | p->constant = constructor_constant; | |
6613 | p->simple = constructor_simple; | |
6614 | p->nonconst = constructor_nonconst; | |
6615 | p->erroneous = constructor_erroneous; | |
6616 | p->pending_elts = constructor_pending_elts; | |
6617 | p->depth = constructor_depth; | |
6618 | p->replacement_value.value = 0; | |
6619 | p->replacement_value.original_code = ERROR_MARK; | |
6620 | p->replacement_value.original_type = NULL; | |
6621 | p->implicit = 0; | |
6622 | p->range_stack = 0; | |
6623 | p->outer = 0; | |
6624 | p->incremental = constructor_incremental; | |
6625 | p->designated = constructor_designated; | |
6626 | p->next = 0; | |
6627 | constructor_stack = p; | |
6628 | ||
6629 | constructor_constant = 1; | |
6630 | constructor_simple = 1; | |
6631 | constructor_nonconst = 0; | |
6632 | constructor_depth = SPELLING_DEPTH (); | |
6633 | constructor_elements = NULL; | |
6634 | constructor_pending_elts = 0; | |
6635 | constructor_type = type; | |
6636 | constructor_incremental = 1; | |
6637 | constructor_designated = 0; | |
6638 | designator_depth = 0; | |
6639 | designator_erroneous = 0; | |
6640 | ||
6641 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6642 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6643 | { | |
6644 | constructor_fields = TYPE_FIELDS (constructor_type); | |
6645 | /* Skip any nameless bit fields at the beginning. */ | |
6646 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6647 | && DECL_NAME (constructor_fields) == 0) | |
6648 | constructor_fields = DECL_CHAIN (constructor_fields); | |
6649 | ||
6650 | constructor_unfilled_fields = constructor_fields; | |
6651 | constructor_bit_index = bitsize_zero_node; | |
6652 | } | |
6653 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6654 | { | |
6655 | if (TYPE_DOMAIN (constructor_type)) | |
6656 | { | |
6657 | constructor_max_index | |
6658 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
6659 | ||
6660 | /* Detect non-empty initializations of zero-length arrays. */ | |
6661 | if (constructor_max_index == NULL_TREE | |
6662 | && TYPE_SIZE (constructor_type)) | |
6663 | constructor_max_index = integer_minus_one_node; | |
6664 | ||
6665 | /* constructor_max_index needs to be an INTEGER_CST. Attempts | |
6666 | to initialize VLAs will cause a proper error; avoid tree | |
6667 | checking errors as well by setting a safe value. */ | |
6668 | if (constructor_max_index | |
6669 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
6670 | constructor_max_index = integer_minus_one_node; | |
6671 | ||
6672 | constructor_index | |
6673 | = convert (bitsizetype, | |
6674 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
6675 | } | |
6676 | else | |
6677 | { | |
6678 | constructor_index = bitsize_zero_node; | |
6679 | constructor_max_index = NULL_TREE; | |
6680 | } | |
6681 | ||
6682 | constructor_unfilled_index = constructor_index; | |
6683 | } | |
6684 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6685 | { | |
6686 | /* Vectors are like simple fixed-size arrays. */ | |
6687 | constructor_max_index = | |
6688 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); | |
6689 | constructor_index = bitsize_zero_node; | |
6690 | constructor_unfilled_index = constructor_index; | |
6691 | } | |
6692 | else | |
6693 | { | |
6694 | /* Handle the case of int x = {5}; */ | |
6695 | constructor_fields = constructor_type; | |
6696 | constructor_unfilled_fields = constructor_type; | |
6697 | } | |
6698 | } | |
6699 | \f | |
6700 | /* Push down into a subobject, for initialization. | |
6701 | If this is for an explicit set of braces, IMPLICIT is 0. | |
6702 | If it is because the next element belongs at a lower level, | |
6703 | IMPLICIT is 1 (or 2 if the push is because of designator list). */ | |
6704 | ||
6705 | void | |
6706 | push_init_level (int implicit, struct obstack * braced_init_obstack) | |
6707 | { | |
6708 | struct constructor_stack *p; | |
6709 | tree value = NULL_TREE; | |
6710 | ||
6711 | /* If we've exhausted any levels that didn't have braces, | |
6712 | pop them now. If implicit == 1, this will have been done in | |
6713 | process_init_element; do not repeat it here because in the case | |
6714 | of excess initializers for an empty aggregate this leads to an | |
6715 | infinite cycle of popping a level and immediately recreating | |
6716 | it. */ | |
6717 | if (implicit != 1) | |
6718 | { | |
6719 | while (constructor_stack->implicit) | |
6720 | { | |
6721 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6722 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6723 | && constructor_fields == 0) | |
6724 | process_init_element (pop_init_level (1, braced_init_obstack), | |
6725 | true, braced_init_obstack); | |
6726 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
6727 | && constructor_max_index | |
6728 | && tree_int_cst_lt (constructor_max_index, | |
6729 | constructor_index)) | |
6730 | process_init_element (pop_init_level (1, braced_init_obstack), | |
6731 | true, braced_init_obstack); | |
6732 | else | |
6733 | break; | |
6734 | } | |
6735 | } | |
6736 | ||
6737 | /* Unless this is an explicit brace, we need to preserve previous | |
6738 | content if any. */ | |
6739 | if (implicit) | |
6740 | { | |
6741 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
6742 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6743 | && constructor_fields) | |
6744 | value = find_init_member (constructor_fields, braced_init_obstack); | |
6745 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6746 | value = find_init_member (constructor_index, braced_init_obstack); | |
6747 | } | |
6748 | ||
6749 | p = XNEW (struct constructor_stack); | |
6750 | p->type = constructor_type; | |
6751 | p->fields = constructor_fields; | |
6752 | p->index = constructor_index; | |
6753 | p->max_index = constructor_max_index; | |
6754 | p->unfilled_index = constructor_unfilled_index; | |
6755 | p->unfilled_fields = constructor_unfilled_fields; | |
6756 | p->bit_index = constructor_bit_index; | |
6757 | p->elements = constructor_elements; | |
6758 | p->constant = constructor_constant; | |
6759 | p->simple = constructor_simple; | |
6760 | p->nonconst = constructor_nonconst; | |
6761 | p->erroneous = constructor_erroneous; | |
6762 | p->pending_elts = constructor_pending_elts; | |
6763 | p->depth = constructor_depth; | |
6764 | p->replacement_value.value = 0; | |
6765 | p->replacement_value.original_code = ERROR_MARK; | |
6766 | p->replacement_value.original_type = NULL; | |
6767 | p->implicit = implicit; | |
6768 | p->outer = 0; | |
6769 | p->incremental = constructor_incremental; | |
6770 | p->designated = constructor_designated; | |
6771 | p->next = constructor_stack; | |
6772 | p->range_stack = 0; | |
6773 | constructor_stack = p; | |
6774 | ||
6775 | constructor_constant = 1; | |
6776 | constructor_simple = 1; | |
6777 | constructor_nonconst = 0; | |
6778 | constructor_depth = SPELLING_DEPTH (); | |
6779 | constructor_elements = NULL; | |
6780 | constructor_incremental = 1; | |
6781 | constructor_designated = 0; | |
6782 | constructor_pending_elts = 0; | |
6783 | if (!implicit) | |
6784 | { | |
6785 | p->range_stack = constructor_range_stack; | |
6786 | constructor_range_stack = 0; | |
6787 | designator_depth = 0; | |
6788 | designator_erroneous = 0; | |
6789 | } | |
6790 | ||
6791 | /* Don't die if an entire brace-pair level is superfluous | |
6792 | in the containing level. */ | |
6793 | if (constructor_type == 0) | |
6794 | ; | |
6795 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6796 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6797 | { | |
6798 | /* Don't die if there are extra init elts at the end. */ | |
6799 | if (constructor_fields == 0) | |
6800 | constructor_type = 0; | |
6801 | else | |
6802 | { | |
6803 | constructor_type = TREE_TYPE (constructor_fields); | |
6804 | push_member_name (constructor_fields); | |
6805 | constructor_depth++; | |
6806 | } | |
6807 | } | |
6808 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6809 | { | |
6810 | constructor_type = TREE_TYPE (constructor_type); | |
6811 | push_array_bounds (tree_low_cst (constructor_index, 1)); | |
6812 | constructor_depth++; | |
6813 | } | |
6814 | ||
6815 | if (constructor_type == 0) | |
6816 | { | |
6817 | error_init ("extra brace group at end of initializer"); | |
6818 | constructor_fields = 0; | |
6819 | constructor_unfilled_fields = 0; | |
6820 | return; | |
6821 | } | |
6822 | ||
6823 | if (value && TREE_CODE (value) == CONSTRUCTOR) | |
6824 | { | |
6825 | constructor_constant = TREE_CONSTANT (value); | |
6826 | constructor_simple = TREE_STATIC (value); | |
6827 | constructor_nonconst = CONSTRUCTOR_NON_CONST (value); | |
6828 | constructor_elements = CONSTRUCTOR_ELTS (value); | |
6829 | if (!vec_safe_is_empty (constructor_elements) | |
6830 | && (TREE_CODE (constructor_type) == RECORD_TYPE | |
6831 | || TREE_CODE (constructor_type) == ARRAY_TYPE)) | |
6832 | set_nonincremental_init (braced_init_obstack); | |
6833 | } | |
6834 | ||
6835 | if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) | |
6836 | { | |
6837 | missing_braces_mentioned = 1; | |
6838 | warning_init (OPT_Wmissing_braces, "missing braces around initializer"); | |
6839 | } | |
6840 | ||
6841 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
6842 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
6843 | { | |
6844 | constructor_fields = TYPE_FIELDS (constructor_type); | |
6845 | /* Skip any nameless bit fields at the beginning. */ | |
6846 | while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields) | |
6847 | && DECL_NAME (constructor_fields) == 0) | |
6848 | constructor_fields = DECL_CHAIN (constructor_fields); | |
6849 | ||
6850 | constructor_unfilled_fields = constructor_fields; | |
6851 | constructor_bit_index = bitsize_zero_node; | |
6852 | } | |
6853 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
6854 | { | |
6855 | /* Vectors are like simple fixed-size arrays. */ | |
6856 | constructor_max_index = | |
6857 | bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1); | |
6858 | constructor_index = bitsize_int (0); | |
6859 | constructor_unfilled_index = constructor_index; | |
6860 | } | |
6861 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
6862 | { | |
6863 | if (TYPE_DOMAIN (constructor_type)) | |
6864 | { | |
6865 | constructor_max_index | |
6866 | = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)); | |
6867 | ||
6868 | /* Detect non-empty initializations of zero-length arrays. */ | |
6869 | if (constructor_max_index == NULL_TREE | |
6870 | && TYPE_SIZE (constructor_type)) | |
6871 | constructor_max_index = integer_minus_one_node; | |
6872 | ||
6873 | /* constructor_max_index needs to be an INTEGER_CST. Attempts | |
6874 | to initialize VLAs will cause a proper error; avoid tree | |
6875 | checking errors as well by setting a safe value. */ | |
6876 | if (constructor_max_index | |
6877 | && TREE_CODE (constructor_max_index) != INTEGER_CST) | |
6878 | constructor_max_index = integer_minus_one_node; | |
6879 | ||
6880 | constructor_index | |
6881 | = convert (bitsizetype, | |
6882 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
6883 | } | |
6884 | else | |
6885 | constructor_index = bitsize_zero_node; | |
6886 | ||
6887 | constructor_unfilled_index = constructor_index; | |
6888 | if (value && TREE_CODE (value) == STRING_CST) | |
6889 | { | |
6890 | /* We need to split the char/wchar array into individual | |
6891 | characters, so that we don't have to special case it | |
6892 | everywhere. */ | |
6893 | set_nonincremental_init_from_string (value, braced_init_obstack); | |
6894 | } | |
6895 | } | |
6896 | else | |
6897 | { | |
6898 | if (constructor_type != error_mark_node) | |
6899 | warning_init (0, "braces around scalar initializer"); | |
6900 | constructor_fields = constructor_type; | |
6901 | constructor_unfilled_fields = constructor_type; | |
6902 | } | |
6903 | } | |
6904 | ||
6905 | /* At the end of an implicit or explicit brace level, | |
6906 | finish up that level of constructor. If a single expression | |
6907 | with redundant braces initialized that level, return the | |
6908 | c_expr structure for that expression. Otherwise, the original_code | |
6909 | element is set to ERROR_MARK. | |
6910 | If we were outputting the elements as they are read, return 0 as the value | |
6911 | from inner levels (process_init_element ignores that), | |
6912 | but return error_mark_node as the value from the outermost level | |
6913 | (that's what we want to put in DECL_INITIAL). | |
6914 | Otherwise, return a CONSTRUCTOR expression as the value. */ | |
6915 | ||
6916 | struct c_expr | |
6917 | pop_init_level (int implicit, struct obstack * braced_init_obstack) | |
6918 | { | |
6919 | struct constructor_stack *p; | |
6920 | struct c_expr ret; | |
6921 | ret.value = 0; | |
6922 | ret.original_code = ERROR_MARK; | |
6923 | ret.original_type = NULL; | |
6924 | ||
6925 | if (implicit == 0) | |
6926 | { | |
6927 | /* When we come to an explicit close brace, | |
6928 | pop any inner levels that didn't have explicit braces. */ | |
6929 | while (constructor_stack->implicit) | |
6930 | { | |
6931 | process_init_element (pop_init_level (1, braced_init_obstack), | |
6932 | true, braced_init_obstack); | |
6933 | } | |
6934 | gcc_assert (!constructor_range_stack); | |
6935 | } | |
6936 | ||
6937 | /* Now output all pending elements. */ | |
6938 | constructor_incremental = 1; | |
6939 | output_pending_init_elements (1, braced_init_obstack); | |
6940 | ||
6941 | p = constructor_stack; | |
6942 | ||
6943 | /* Error for initializing a flexible array member, or a zero-length | |
6944 | array member in an inappropriate context. */ | |
6945 | if (constructor_type && constructor_fields | |
6946 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
6947 | && TYPE_DOMAIN (constructor_type) | |
6948 | && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type))) | |
6949 | { | |
6950 | /* Silently discard empty initializations. The parser will | |
6951 | already have pedwarned for empty brackets. */ | |
6952 | if (integer_zerop (constructor_unfilled_index)) | |
6953 | constructor_type = NULL_TREE; | |
6954 | else | |
6955 | { | |
6956 | gcc_assert (!TYPE_SIZE (constructor_type)); | |
6957 | ||
6958 | if (constructor_depth > 2) | |
6959 | error_init ("initialization of flexible array member in a nested context"); | |
6960 | else | |
6961 | pedwarn_init (input_location, OPT_Wpedantic, | |
6962 | "initialization of a flexible array member"); | |
6963 | ||
6964 | /* We have already issued an error message for the existence | |
6965 | of a flexible array member not at the end of the structure. | |
6966 | Discard the initializer so that we do not die later. */ | |
6967 | if (DECL_CHAIN (constructor_fields) != NULL_TREE) | |
6968 | constructor_type = NULL_TREE; | |
6969 | } | |
6970 | } | |
6971 | ||
6972 | /* Warn when some struct elements are implicitly initialized to zero. */ | |
6973 | if (warn_missing_field_initializers | |
6974 | && constructor_type | |
6975 | && TREE_CODE (constructor_type) == RECORD_TYPE | |
6976 | && constructor_unfilled_fields) | |
6977 | { | |
6978 | bool constructor_zeroinit = | |
6979 | (vec_safe_length (constructor_elements) == 1 | |
6980 | && integer_zerop ((*constructor_elements)[0].value)); | |
6981 | ||
6982 | /* Do not warn for flexible array members or zero-length arrays. */ | |
6983 | while (constructor_unfilled_fields | |
6984 | && (!DECL_SIZE (constructor_unfilled_fields) | |
6985 | || integer_zerop (DECL_SIZE (constructor_unfilled_fields)))) | |
6986 | constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields); | |
6987 | ||
6988 | if (constructor_unfilled_fields | |
6989 | /* Do not warn if this level of the initializer uses member | |
6990 | designators; it is likely to be deliberate. */ | |
6991 | && !constructor_designated | |
6992 | /* Do not warn about initializing with ` = {0}'. */ | |
6993 | && !constructor_zeroinit) | |
6994 | { | |
6995 | if (warning_at (input_location, OPT_Wmissing_field_initializers, | |
6996 | "missing initializer for field %qD of %qT", | |
6997 | constructor_unfilled_fields, | |
6998 | constructor_type)) | |
6999 | inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields), | |
7000 | "%qD declared here", constructor_unfilled_fields); | |
7001 | } | |
7002 | } | |
7003 | ||
7004 | /* Pad out the end of the structure. */ | |
7005 | if (p->replacement_value.value) | |
7006 | /* If this closes a superfluous brace pair, | |
7007 | just pass out the element between them. */ | |
7008 | ret = p->replacement_value; | |
7009 | else if (constructor_type == 0) | |
7010 | ; | |
7011 | else if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7012 | && TREE_CODE (constructor_type) != UNION_TYPE | |
7013 | && TREE_CODE (constructor_type) != ARRAY_TYPE | |
7014 | && TREE_CODE (constructor_type) != VECTOR_TYPE) | |
7015 | { | |
7016 | /* A nonincremental scalar initializer--just return | |
7017 | the element, after verifying there is just one. */ | |
7018 | if (vec_safe_is_empty (constructor_elements)) | |
7019 | { | |
7020 | if (!constructor_erroneous) | |
7021 | error_init ("empty scalar initializer"); | |
7022 | ret.value = error_mark_node; | |
7023 | } | |
7024 | else if (vec_safe_length (constructor_elements) != 1) | |
7025 | { | |
7026 | error_init ("extra elements in scalar initializer"); | |
7027 | ret.value = (*constructor_elements)[0].value; | |
7028 | } | |
7029 | else | |
7030 | ret.value = (*constructor_elements)[0].value; | |
7031 | } | |
7032 | else | |
7033 | { | |
7034 | if (constructor_erroneous) | |
7035 | ret.value = error_mark_node; | |
7036 | else | |
7037 | { | |
7038 | ret.value = build_constructor (constructor_type, | |
7039 | constructor_elements); | |
7040 | if (constructor_constant) | |
7041 | TREE_CONSTANT (ret.value) = 1; | |
7042 | if (constructor_constant && constructor_simple) | |
7043 | TREE_STATIC (ret.value) = 1; | |
7044 | if (constructor_nonconst) | |
7045 | CONSTRUCTOR_NON_CONST (ret.value) = 1; | |
7046 | } | |
7047 | } | |
7048 | ||
7049 | if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR) | |
7050 | { | |
7051 | if (constructor_nonconst) | |
7052 | ret.original_code = C_MAYBE_CONST_EXPR; | |
7053 | else if (ret.original_code == C_MAYBE_CONST_EXPR) | |
7054 | ret.original_code = ERROR_MARK; | |
7055 | } | |
7056 | ||
7057 | constructor_type = p->type; | |
7058 | constructor_fields = p->fields; | |
7059 | constructor_index = p->index; | |
7060 | constructor_max_index = p->max_index; | |
7061 | constructor_unfilled_index = p->unfilled_index; | |
7062 | constructor_unfilled_fields = p->unfilled_fields; | |
7063 | constructor_bit_index = p->bit_index; | |
7064 | constructor_elements = p->elements; | |
7065 | constructor_constant = p->constant; | |
7066 | constructor_simple = p->simple; | |
7067 | constructor_nonconst = p->nonconst; | |
7068 | constructor_erroneous = p->erroneous; | |
7069 | constructor_incremental = p->incremental; | |
7070 | constructor_designated = p->designated; | |
7071 | constructor_pending_elts = p->pending_elts; | |
7072 | constructor_depth = p->depth; | |
7073 | if (!p->implicit) | |
7074 | constructor_range_stack = p->range_stack; | |
7075 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
7076 | ||
7077 | constructor_stack = p->next; | |
7078 | free (p); | |
7079 | ||
7080 | if (ret.value == 0 && constructor_stack == 0) | |
7081 | ret.value = error_mark_node; | |
7082 | return ret; | |
7083 | } | |
7084 | ||
7085 | /* Common handling for both array range and field name designators. | |
7086 | ARRAY argument is nonzero for array ranges. Returns zero for success. */ | |
7087 | ||
7088 | static int | |
7089 | set_designator (int array, struct obstack * braced_init_obstack) | |
7090 | { | |
7091 | tree subtype; | |
7092 | enum tree_code subcode; | |
7093 | ||
7094 | /* Don't die if an entire brace-pair level is superfluous | |
7095 | in the containing level. */ | |
7096 | if (constructor_type == 0) | |
7097 | return 1; | |
7098 | ||
7099 | /* If there were errors in this designator list already, bail out | |
7100 | silently. */ | |
7101 | if (designator_erroneous) | |
7102 | return 1; | |
7103 | ||
7104 | if (!designator_depth) | |
7105 | { | |
7106 | gcc_assert (!constructor_range_stack); | |
7107 | ||
7108 | /* Designator list starts at the level of closest explicit | |
7109 | braces. */ | |
7110 | while (constructor_stack->implicit) | |
7111 | { | |
7112 | process_init_element (pop_init_level (1, braced_init_obstack), | |
7113 | true, braced_init_obstack); | |
7114 | } | |
7115 | constructor_designated = 1; | |
7116 | return 0; | |
7117 | } | |
7118 | ||
7119 | switch (TREE_CODE (constructor_type)) | |
7120 | { | |
7121 | case RECORD_TYPE: | |
7122 | case UNION_TYPE: | |
7123 | subtype = TREE_TYPE (constructor_fields); | |
7124 | if (subtype != error_mark_node) | |
7125 | subtype = TYPE_MAIN_VARIANT (subtype); | |
7126 | break; | |
7127 | case ARRAY_TYPE: | |
7128 | subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
7129 | break; | |
7130 | default: | |
7131 | gcc_unreachable (); | |
7132 | } | |
7133 | ||
7134 | subcode = TREE_CODE (subtype); | |
7135 | if (array && subcode != ARRAY_TYPE) | |
7136 | { | |
7137 | error_init ("array index in non-array initializer"); | |
7138 | return 1; | |
7139 | } | |
7140 | else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE) | |
7141 | { | |
7142 | error_init ("field name not in record or union initializer"); | |
7143 | return 1; | |
7144 | } | |
7145 | ||
7146 | constructor_designated = 1; | |
7147 | push_init_level (2, braced_init_obstack); | |
7148 | return 0; | |
7149 | } | |
7150 | ||
7151 | /* If there are range designators in designator list, push a new designator | |
7152 | to constructor_range_stack. RANGE_END is end of such stack range or | |
7153 | NULL_TREE if there is no range designator at this level. */ | |
7154 | ||
7155 | static void | |
7156 | push_range_stack (tree range_end, struct obstack * braced_init_obstack) | |
7157 | { | |
7158 | struct constructor_range_stack *p; | |
7159 | ||
7160 | p = (struct constructor_range_stack *) | |
7161 | obstack_alloc (braced_init_obstack, | |
7162 | sizeof (struct constructor_range_stack)); | |
7163 | p->prev = constructor_range_stack; | |
7164 | p->next = 0; | |
7165 | p->fields = constructor_fields; | |
7166 | p->range_start = constructor_index; | |
7167 | p->index = constructor_index; | |
7168 | p->stack = constructor_stack; | |
7169 | p->range_end = range_end; | |
7170 | if (constructor_range_stack) | |
7171 | constructor_range_stack->next = p; | |
7172 | constructor_range_stack = p; | |
7173 | } | |
7174 | ||
7175 | /* Within an array initializer, specify the next index to be initialized. | |
7176 | FIRST is that index. If LAST is nonzero, then initialize a range | |
7177 | of indices, running from FIRST through LAST. */ | |
7178 | ||
7179 | void | |
7180 | set_init_index (tree first, tree last, | |
7181 | struct obstack * braced_init_obstack) | |
7182 | { | |
7183 | if (set_designator (1, braced_init_obstack)) | |
7184 | return; | |
7185 | ||
7186 | designator_erroneous = 1; | |
7187 | ||
7188 | if (!INTEGRAL_TYPE_P (TREE_TYPE (first)) | |
7189 | || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last)))) | |
7190 | { | |
7191 | error_init ("array index in initializer not of integer type"); | |
7192 | return; | |
7193 | } | |
7194 | ||
7195 | if (TREE_CODE (first) != INTEGER_CST) | |
7196 | { | |
7197 | first = c_fully_fold (first, false, NULL); | |
7198 | if (TREE_CODE (first) == INTEGER_CST) | |
7199 | pedwarn_init (input_location, OPT_Wpedantic, | |
7200 | "array index in initializer is not " | |
7201 | "an integer constant expression"); | |
7202 | } | |
7203 | ||
7204 | if (last && TREE_CODE (last) != INTEGER_CST) | |
7205 | { | |
7206 | last = c_fully_fold (last, false, NULL); | |
7207 | if (TREE_CODE (last) == INTEGER_CST) | |
7208 | pedwarn_init (input_location, OPT_Wpedantic, | |
7209 | "array index in initializer is not " | |
7210 | "an integer constant expression"); | |
7211 | } | |
7212 | ||
7213 | if (TREE_CODE (first) != INTEGER_CST) | |
7214 | error_init ("nonconstant array index in initializer"); | |
7215 | else if (last != 0 && TREE_CODE (last) != INTEGER_CST) | |
7216 | error_init ("nonconstant array index in initializer"); | |
7217 | else if (TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7218 | error_init ("array index in non-array initializer"); | |
7219 | else if (tree_int_cst_sgn (first) == -1) | |
7220 | error_init ("array index in initializer exceeds array bounds"); | |
7221 | else if (constructor_max_index | |
7222 | && tree_int_cst_lt (constructor_max_index, first)) | |
7223 | error_init ("array index in initializer exceeds array bounds"); | |
7224 | else | |
7225 | { | |
7226 | constant_expression_warning (first); | |
7227 | if (last) | |
7228 | constant_expression_warning (last); | |
7229 | constructor_index = convert (bitsizetype, first); | |
7230 | if (tree_int_cst_lt (constructor_index, first)) | |
7231 | { | |
7232 | constructor_index = copy_node (constructor_index); | |
7233 | TREE_OVERFLOW (constructor_index) = 1; | |
7234 | } | |
7235 | ||
7236 | if (last) | |
7237 | { | |
7238 | if (tree_int_cst_equal (first, last)) | |
7239 | last = 0; | |
7240 | else if (tree_int_cst_lt (last, first)) | |
7241 | { | |
7242 | error_init ("empty index range in initializer"); | |
7243 | last = 0; | |
7244 | } | |
7245 | else | |
7246 | { | |
7247 | last = convert (bitsizetype, last); | |
7248 | if (constructor_max_index != 0 | |
7249 | && tree_int_cst_lt (constructor_max_index, last)) | |
7250 | { | |
7251 | error_init ("array index range in initializer exceeds array bounds"); | |
7252 | last = 0; | |
7253 | } | |
7254 | } | |
7255 | } | |
7256 | ||
7257 | designator_depth++; | |
7258 | designator_erroneous = 0; | |
7259 | if (constructor_range_stack || last) | |
7260 | push_range_stack (last, braced_init_obstack); | |
7261 | } | |
7262 | } | |
7263 | ||
7264 | /* Within a struct initializer, specify the next field to be initialized. */ | |
7265 | ||
7266 | void | |
7267 | set_init_label (tree fieldname, struct obstack * braced_init_obstack) | |
7268 | { | |
7269 | tree field; | |
7270 | ||
7271 | if (set_designator (0, braced_init_obstack)) | |
7272 | return; | |
7273 | ||
7274 | designator_erroneous = 1; | |
7275 | ||
7276 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7277 | && TREE_CODE (constructor_type) != UNION_TYPE) | |
7278 | { | |
7279 | error_init ("field name not in record or union initializer"); | |
7280 | return; | |
7281 | } | |
7282 | ||
7283 | field = lookup_field (constructor_type, fieldname); | |
7284 | ||
7285 | if (field == 0) | |
7286 | error ("unknown field %qE specified in initializer", fieldname); | |
7287 | else | |
7288 | do | |
7289 | { | |
7290 | constructor_fields = TREE_VALUE (field); | |
7291 | designator_depth++; | |
7292 | designator_erroneous = 0; | |
7293 | if (constructor_range_stack) | |
7294 | push_range_stack (NULL_TREE, braced_init_obstack); | |
7295 | field = TREE_CHAIN (field); | |
7296 | if (field) | |
7297 | { | |
7298 | if (set_designator (0, braced_init_obstack)) | |
7299 | return; | |
7300 | } | |
7301 | } | |
7302 | while (field != NULL_TREE); | |
7303 | } | |
7304 | \f | |
7305 | /* Add a new initializer to the tree of pending initializers. PURPOSE | |
7306 | identifies the initializer, either array index or field in a structure. | |
7307 | VALUE is the value of that index or field. If ORIGTYPE is not | |
7308 | NULL_TREE, it is the original type of VALUE. | |
7309 | ||
7310 | IMPLICIT is true if value comes from pop_init_level (1), | |
7311 | the new initializer has been merged with the existing one | |
7312 | and thus no warnings should be emitted about overriding an | |
7313 | existing initializer. */ | |
7314 | ||
7315 | static void | |
7316 | add_pending_init (tree purpose, tree value, tree origtype, bool implicit, | |
7317 | struct obstack * braced_init_obstack) | |
7318 | { | |
7319 | struct init_node *p, **q, *r; | |
7320 | ||
7321 | q = &constructor_pending_elts; | |
7322 | p = 0; | |
7323 | ||
7324 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7325 | { | |
7326 | while (*q != 0) | |
7327 | { | |
7328 | p = *q; | |
7329 | if (tree_int_cst_lt (purpose, p->purpose)) | |
7330 | q = &p->left; | |
7331 | else if (tree_int_cst_lt (p->purpose, purpose)) | |
7332 | q = &p->right; | |
7333 | else | |
7334 | { | |
7335 | if (!implicit) | |
7336 | { | |
7337 | if (TREE_SIDE_EFFECTS (p->value)) | |
7338 | warning_init (0, "initialized field with side-effects overwritten"); | |
7339 | else if (warn_override_init) | |
7340 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7341 | } | |
7342 | p->value = value; | |
7343 | p->origtype = origtype; | |
7344 | return; | |
7345 | } | |
7346 | } | |
7347 | } | |
7348 | else | |
7349 | { | |
7350 | tree bitpos; | |
7351 | ||
7352 | bitpos = bit_position (purpose); | |
7353 | while (*q != NULL) | |
7354 | { | |
7355 | p = *q; | |
7356 | if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7357 | q = &p->left; | |
7358 | else if (p->purpose != purpose) | |
7359 | q = &p->right; | |
7360 | else | |
7361 | { | |
7362 | if (!implicit) | |
7363 | { | |
7364 | if (TREE_SIDE_EFFECTS (p->value)) | |
7365 | warning_init (0, "initialized field with side-effects overwritten"); | |
7366 | else if (warn_override_init) | |
7367 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7368 | } | |
7369 | p->value = value; | |
7370 | p->origtype = origtype; | |
7371 | return; | |
7372 | } | |
7373 | } | |
7374 | } | |
7375 | ||
7376 | r = (struct init_node *) obstack_alloc (braced_init_obstack, | |
7377 | sizeof (struct init_node)); | |
7378 | r->purpose = purpose; | |
7379 | r->value = value; | |
7380 | r->origtype = origtype; | |
7381 | ||
7382 | *q = r; | |
7383 | r->parent = p; | |
7384 | r->left = 0; | |
7385 | r->right = 0; | |
7386 | r->balance = 0; | |
7387 | ||
7388 | while (p) | |
7389 | { | |
7390 | struct init_node *s; | |
7391 | ||
7392 | if (r == p->left) | |
7393 | { | |
7394 | if (p->balance == 0) | |
7395 | p->balance = -1; | |
7396 | else if (p->balance < 0) | |
7397 | { | |
7398 | if (r->balance < 0) | |
7399 | { | |
7400 | /* L rotation. */ | |
7401 | p->left = r->right; | |
7402 | if (p->left) | |
7403 | p->left->parent = p; | |
7404 | r->right = p; | |
7405 | ||
7406 | p->balance = 0; | |
7407 | r->balance = 0; | |
7408 | ||
7409 | s = p->parent; | |
7410 | p->parent = r; | |
7411 | r->parent = s; | |
7412 | if (s) | |
7413 | { | |
7414 | if (s->left == p) | |
7415 | s->left = r; | |
7416 | else | |
7417 | s->right = r; | |
7418 | } | |
7419 | else | |
7420 | constructor_pending_elts = r; | |
7421 | } | |
7422 | else | |
7423 | { | |
7424 | /* LR rotation. */ | |
7425 | struct init_node *t = r->right; | |
7426 | ||
7427 | r->right = t->left; | |
7428 | if (r->right) | |
7429 | r->right->parent = r; | |
7430 | t->left = r; | |
7431 | ||
7432 | p->left = t->right; | |
7433 | if (p->left) | |
7434 | p->left->parent = p; | |
7435 | t->right = p; | |
7436 | ||
7437 | p->balance = t->balance < 0; | |
7438 | r->balance = -(t->balance > 0); | |
7439 | t->balance = 0; | |
7440 | ||
7441 | s = p->parent; | |
7442 | p->parent = t; | |
7443 | r->parent = t; | |
7444 | t->parent = s; | |
7445 | if (s) | |
7446 | { | |
7447 | if (s->left == p) | |
7448 | s->left = t; | |
7449 | else | |
7450 | s->right = t; | |
7451 | } | |
7452 | else | |
7453 | constructor_pending_elts = t; | |
7454 | } | |
7455 | break; | |
7456 | } | |
7457 | else | |
7458 | { | |
7459 | /* p->balance == +1; growth of left side balances the node. */ | |
7460 | p->balance = 0; | |
7461 | break; | |
7462 | } | |
7463 | } | |
7464 | else /* r == p->right */ | |
7465 | { | |
7466 | if (p->balance == 0) | |
7467 | /* Growth propagation from right side. */ | |
7468 | p->balance++; | |
7469 | else if (p->balance > 0) | |
7470 | { | |
7471 | if (r->balance > 0) | |
7472 | { | |
7473 | /* R rotation. */ | |
7474 | p->right = r->left; | |
7475 | if (p->right) | |
7476 | p->right->parent = p; | |
7477 | r->left = p; | |
7478 | ||
7479 | p->balance = 0; | |
7480 | r->balance = 0; | |
7481 | ||
7482 | s = p->parent; | |
7483 | p->parent = r; | |
7484 | r->parent = s; | |
7485 | if (s) | |
7486 | { | |
7487 | if (s->left == p) | |
7488 | s->left = r; | |
7489 | else | |
7490 | s->right = r; | |
7491 | } | |
7492 | else | |
7493 | constructor_pending_elts = r; | |
7494 | } | |
7495 | else /* r->balance == -1 */ | |
7496 | { | |
7497 | /* RL rotation */ | |
7498 | struct init_node *t = r->left; | |
7499 | ||
7500 | r->left = t->right; | |
7501 | if (r->left) | |
7502 | r->left->parent = r; | |
7503 | t->right = r; | |
7504 | ||
7505 | p->right = t->left; | |
7506 | if (p->right) | |
7507 | p->right->parent = p; | |
7508 | t->left = p; | |
7509 | ||
7510 | r->balance = (t->balance < 0); | |
7511 | p->balance = -(t->balance > 0); | |
7512 | t->balance = 0; | |
7513 | ||
7514 | s = p->parent; | |
7515 | p->parent = t; | |
7516 | r->parent = t; | |
7517 | t->parent = s; | |
7518 | if (s) | |
7519 | { | |
7520 | if (s->left == p) | |
7521 | s->left = t; | |
7522 | else | |
7523 | s->right = t; | |
7524 | } | |
7525 | else | |
7526 | constructor_pending_elts = t; | |
7527 | } | |
7528 | break; | |
7529 | } | |
7530 | else | |
7531 | { | |
7532 | /* p->balance == -1; growth of right side balances the node. */ | |
7533 | p->balance = 0; | |
7534 | break; | |
7535 | } | |
7536 | } | |
7537 | ||
7538 | r = p; | |
7539 | p = p->parent; | |
7540 | } | |
7541 | } | |
7542 | ||
7543 | /* Build AVL tree from a sorted chain. */ | |
7544 | ||
7545 | static void | |
7546 | set_nonincremental_init (struct obstack * braced_init_obstack) | |
7547 | { | |
7548 | unsigned HOST_WIDE_INT ix; | |
7549 | tree index, value; | |
7550 | ||
7551 | if (TREE_CODE (constructor_type) != RECORD_TYPE | |
7552 | && TREE_CODE (constructor_type) != ARRAY_TYPE) | |
7553 | return; | |
7554 | ||
7555 | FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value) | |
7556 | { | |
7557 | add_pending_init (index, value, NULL_TREE, true, | |
7558 | braced_init_obstack); | |
7559 | } | |
7560 | constructor_elements = NULL; | |
7561 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
7562 | { | |
7563 | constructor_unfilled_fields = TYPE_FIELDS (constructor_type); | |
7564 | /* Skip any nameless bit fields at the beginning. */ | |
7565 | while (constructor_unfilled_fields != 0 | |
7566 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7567 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7568 | constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields); | |
7569 | ||
7570 | } | |
7571 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7572 | { | |
7573 | if (TYPE_DOMAIN (constructor_type)) | |
7574 | constructor_unfilled_index | |
7575 | = convert (bitsizetype, | |
7576 | TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type))); | |
7577 | else | |
7578 | constructor_unfilled_index = bitsize_zero_node; | |
7579 | } | |
7580 | constructor_incremental = 0; | |
7581 | } | |
7582 | ||
7583 | /* Build AVL tree from a string constant. */ | |
7584 | ||
7585 | static void | |
7586 | set_nonincremental_init_from_string (tree str, | |
7587 | struct obstack * braced_init_obstack) | |
7588 | { | |
7589 | tree value, purpose, type; | |
7590 | HOST_WIDE_INT val[2]; | |
7591 | const char *p, *end; | |
7592 | int byte, wchar_bytes, charwidth, bitpos; | |
7593 | ||
7594 | gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE); | |
7595 | ||
7596 | wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT; | |
7597 | charwidth = TYPE_PRECISION (char_type_node); | |
7598 | type = TREE_TYPE (constructor_type); | |
7599 | p = TREE_STRING_POINTER (str); | |
7600 | end = p + TREE_STRING_LENGTH (str); | |
7601 | ||
7602 | for (purpose = bitsize_zero_node; | |
7603 | p < end | |
7604 | && !(constructor_max_index | |
7605 | && tree_int_cst_lt (constructor_max_index, purpose)); | |
7606 | purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node)) | |
7607 | { | |
7608 | if (wchar_bytes == 1) | |
7609 | { | |
7610 | val[1] = (unsigned char) *p++; | |
7611 | val[0] = 0; | |
7612 | } | |
7613 | else | |
7614 | { | |
7615 | val[0] = 0; | |
7616 | val[1] = 0; | |
7617 | for (byte = 0; byte < wchar_bytes; byte++) | |
7618 | { | |
7619 | if (BYTES_BIG_ENDIAN) | |
7620 | bitpos = (wchar_bytes - byte - 1) * charwidth; | |
7621 | else | |
7622 | bitpos = byte * charwidth; | |
7623 | val[bitpos < HOST_BITS_PER_WIDE_INT] | |
7624 | |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++)) | |
7625 | << (bitpos % HOST_BITS_PER_WIDE_INT); | |
7626 | } | |
7627 | } | |
7628 | ||
7629 | if (!TYPE_UNSIGNED (type)) | |
7630 | { | |
7631 | bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR; | |
7632 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
7633 | { | |
7634 | if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1))) | |
7635 | { | |
7636 | val[1] |= ((HOST_WIDE_INT) -1) << bitpos; | |
7637 | val[0] = -1; | |
7638 | } | |
7639 | } | |
7640 | else if (bitpos == HOST_BITS_PER_WIDE_INT) | |
7641 | { | |
7642 | if (val[1] < 0) | |
7643 | val[0] = -1; | |
7644 | } | |
7645 | else if (val[0] & (((HOST_WIDE_INT) 1) | |
7646 | << (bitpos - 1 - HOST_BITS_PER_WIDE_INT))) | |
7647 | val[0] |= ((HOST_WIDE_INT) -1) | |
7648 | << (bitpos - HOST_BITS_PER_WIDE_INT); | |
7649 | } | |
7650 | ||
7651 | value = build_int_cst_wide (type, val[1], val[0]); | |
7652 | add_pending_init (purpose, value, NULL_TREE, true, | |
7653 | braced_init_obstack); | |
7654 | } | |
7655 | ||
7656 | constructor_incremental = 0; | |
7657 | } | |
7658 | ||
7659 | /* Return value of FIELD in pending initializer or zero if the field was | |
7660 | not initialized yet. */ | |
7661 | ||
7662 | static tree | |
7663 | find_init_member (tree field, struct obstack * braced_init_obstack) | |
7664 | { | |
7665 | struct init_node *p; | |
7666 | ||
7667 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7668 | { | |
7669 | if (constructor_incremental | |
7670 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
7671 | set_nonincremental_init (braced_init_obstack); | |
7672 | ||
7673 | p = constructor_pending_elts; | |
7674 | while (p) | |
7675 | { | |
7676 | if (tree_int_cst_lt (field, p->purpose)) | |
7677 | p = p->left; | |
7678 | else if (tree_int_cst_lt (p->purpose, field)) | |
7679 | p = p->right; | |
7680 | else | |
7681 | return p->value; | |
7682 | } | |
7683 | } | |
7684 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
7685 | { | |
7686 | tree bitpos = bit_position (field); | |
7687 | ||
7688 | if (constructor_incremental | |
7689 | && (!constructor_unfilled_fields | |
7690 | || tree_int_cst_lt (bitpos, | |
7691 | bit_position (constructor_unfilled_fields)))) | |
7692 | set_nonincremental_init (braced_init_obstack); | |
7693 | ||
7694 | p = constructor_pending_elts; | |
7695 | while (p) | |
7696 | { | |
7697 | if (field == p->purpose) | |
7698 | return p->value; | |
7699 | else if (tree_int_cst_lt (bitpos, bit_position (p->purpose))) | |
7700 | p = p->left; | |
7701 | else | |
7702 | p = p->right; | |
7703 | } | |
7704 | } | |
7705 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
7706 | { | |
7707 | if (!vec_safe_is_empty (constructor_elements) | |
7708 | && (constructor_elements->last ().index == field)) | |
7709 | return constructor_elements->last ().value; | |
7710 | } | |
7711 | return 0; | |
7712 | } | |
7713 | ||
7714 | /* "Output" the next constructor element. | |
7715 | At top level, really output it to assembler code now. | |
7716 | Otherwise, collect it in a list from which we will make a CONSTRUCTOR. | |
7717 | If ORIGTYPE is not NULL_TREE, it is the original type of VALUE. | |
7718 | TYPE is the data type that the containing data type wants here. | |
7719 | FIELD is the field (a FIELD_DECL) or the index that this element fills. | |
7720 | If VALUE is a string constant, STRICT_STRING is true if it is | |
7721 | unparenthesized or we should not warn here for it being parenthesized. | |
7722 | For other types of VALUE, STRICT_STRING is not used. | |
7723 | ||
7724 | PENDING if non-nil means output pending elements that belong | |
7725 | right after this element. (PENDING is normally 1; | |
7726 | it is 0 while outputting pending elements, to avoid recursion.) | |
7727 | ||
7728 | IMPLICIT is true if value comes from pop_init_level (1), | |
7729 | the new initializer has been merged with the existing one | |
7730 | and thus no warnings should be emitted about overriding an | |
7731 | existing initializer. */ | |
7732 | ||
7733 | static void | |
7734 | output_init_element (tree value, tree origtype, bool strict_string, tree type, | |
7735 | tree field, int pending, bool implicit, | |
7736 | struct obstack * braced_init_obstack) | |
7737 | { | |
7738 | tree semantic_type = NULL_TREE; | |
7739 | bool maybe_const = true; | |
7740 | bool npc; | |
7741 | ||
7742 | if (type == error_mark_node || value == error_mark_node) | |
7743 | { | |
7744 | constructor_erroneous = 1; | |
7745 | return; | |
7746 | } | |
7747 | if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE | |
7748 | && (TREE_CODE (value) == STRING_CST | |
7749 | || TREE_CODE (value) == COMPOUND_LITERAL_EXPR) | |
7750 | && !(TREE_CODE (value) == STRING_CST | |
7751 | && TREE_CODE (type) == ARRAY_TYPE | |
7752 | && INTEGRAL_TYPE_P (TREE_TYPE (type))) | |
7753 | && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)), | |
7754 | TYPE_MAIN_VARIANT (type))) | |
7755 | value = array_to_pointer_conversion (input_location, value); | |
7756 | ||
7757 | if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR | |
7758 | && require_constant_value && !flag_isoc99 && pending) | |
7759 | { | |
7760 | /* As an extension, allow initializing objects with static storage | |
7761 | duration with compound literals (which are then treated just as | |
7762 | the brace enclosed list they contain). */ | |
7763 | tree decl = COMPOUND_LITERAL_EXPR_DECL (value); | |
7764 | value = DECL_INITIAL (decl); | |
7765 | } | |
7766 | ||
7767 | npc = null_pointer_constant_p (value); | |
7768 | if (TREE_CODE (value) == EXCESS_PRECISION_EXPR) | |
7769 | { | |
7770 | semantic_type = TREE_TYPE (value); | |
7771 | value = TREE_OPERAND (value, 0); | |
7772 | } | |
7773 | value = c_fully_fold (value, require_constant_value, &maybe_const); | |
7774 | ||
7775 | if (value == error_mark_node) | |
7776 | constructor_erroneous = 1; | |
7777 | else if (!TREE_CONSTANT (value)) | |
7778 | constructor_constant = 0; | |
7779 | else if (!initializer_constant_valid_p (value, TREE_TYPE (value)) | |
7780 | || ((TREE_CODE (constructor_type) == RECORD_TYPE | |
7781 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
7782 | && DECL_C_BIT_FIELD (field) | |
7783 | && TREE_CODE (value) != INTEGER_CST)) | |
7784 | constructor_simple = 0; | |
7785 | if (!maybe_const) | |
7786 | constructor_nonconst = 1; | |
7787 | ||
7788 | if (!initializer_constant_valid_p (value, TREE_TYPE (value))) | |
7789 | { | |
7790 | if (require_constant_value) | |
7791 | { | |
7792 | error_init ("initializer element is not constant"); | |
7793 | value = error_mark_node; | |
7794 | } | |
7795 | else if (require_constant_elements) | |
7796 | pedwarn (input_location, 0, | |
7797 | "initializer element is not computable at load time"); | |
7798 | } | |
7799 | else if (!maybe_const | |
7800 | && (require_constant_value || require_constant_elements)) | |
7801 | pedwarn_init (input_location, 0, | |
7802 | "initializer element is not a constant expression"); | |
7803 | ||
7804 | /* Issue -Wc++-compat warnings about initializing a bitfield with | |
7805 | enum type. */ | |
7806 | if (warn_cxx_compat | |
7807 | && field != NULL_TREE | |
7808 | && TREE_CODE (field) == FIELD_DECL | |
7809 | && DECL_BIT_FIELD_TYPE (field) != NULL_TREE | |
7810 | && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)) | |
7811 | != TYPE_MAIN_VARIANT (type)) | |
7812 | && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE) | |
7813 | { | |
7814 | tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value); | |
7815 | if (checktype != error_mark_node | |
7816 | && (TYPE_MAIN_VARIANT (checktype) | |
7817 | != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field)))) | |
7818 | warning_init (OPT_Wc___compat, | |
7819 | "enum conversion in initialization is invalid in C++"); | |
7820 | } | |
7821 | ||
7822 | /* If this field is empty (and not at the end of structure), | |
7823 | don't do anything other than checking the initializer. */ | |
7824 | if (field | |
7825 | && (TREE_TYPE (field) == error_mark_node | |
7826 | || (COMPLETE_TYPE_P (TREE_TYPE (field)) | |
7827 | && integer_zerop (TYPE_SIZE (TREE_TYPE (field))) | |
7828 | && (TREE_CODE (constructor_type) == ARRAY_TYPE | |
7829 | || DECL_CHAIN (field))))) | |
7830 | return; | |
7831 | ||
7832 | if (semantic_type) | |
7833 | value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value); | |
7834 | value = digest_init (input_location, type, value, origtype, npc, | |
7835 | strict_string, require_constant_value); | |
7836 | if (value == error_mark_node) | |
7837 | { | |
7838 | constructor_erroneous = 1; | |
7839 | return; | |
7840 | } | |
7841 | if (require_constant_value || require_constant_elements) | |
7842 | constant_expression_warning (value); | |
7843 | ||
7844 | /* If this element doesn't come next in sequence, | |
7845 | put it on constructor_pending_elts. */ | |
7846 | if (TREE_CODE (constructor_type) == ARRAY_TYPE | |
7847 | && (!constructor_incremental | |
7848 | || !tree_int_cst_equal (field, constructor_unfilled_index))) | |
7849 | { | |
7850 | if (constructor_incremental | |
7851 | && tree_int_cst_lt (field, constructor_unfilled_index)) | |
7852 | set_nonincremental_init (braced_init_obstack); | |
7853 | ||
7854 | add_pending_init (field, value, origtype, implicit, | |
7855 | braced_init_obstack); | |
7856 | return; | |
7857 | } | |
7858 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
7859 | && (!constructor_incremental | |
7860 | || field != constructor_unfilled_fields)) | |
7861 | { | |
7862 | /* We do this for records but not for unions. In a union, | |
7863 | no matter which field is specified, it can be initialized | |
7864 | right away since it starts at the beginning of the union. */ | |
7865 | if (constructor_incremental) | |
7866 | { | |
7867 | if (!constructor_unfilled_fields) | |
7868 | set_nonincremental_init (braced_init_obstack); | |
7869 | else | |
7870 | { | |
7871 | tree bitpos, unfillpos; | |
7872 | ||
7873 | bitpos = bit_position (field); | |
7874 | unfillpos = bit_position (constructor_unfilled_fields); | |
7875 | ||
7876 | if (tree_int_cst_lt (bitpos, unfillpos)) | |
7877 | set_nonincremental_init (braced_init_obstack); | |
7878 | } | |
7879 | } | |
7880 | ||
7881 | add_pending_init (field, value, origtype, implicit, | |
7882 | braced_init_obstack); | |
7883 | return; | |
7884 | } | |
7885 | else if (TREE_CODE (constructor_type) == UNION_TYPE | |
7886 | && !vec_safe_is_empty (constructor_elements)) | |
7887 | { | |
7888 | if (!implicit) | |
7889 | { | |
7890 | if (TREE_SIDE_EFFECTS (constructor_elements->last ().value)) | |
7891 | warning_init (0, | |
7892 | "initialized field with side-effects overwritten"); | |
7893 | else if (warn_override_init) | |
7894 | warning_init (OPT_Woverride_init, "initialized field overwritten"); | |
7895 | } | |
7896 | ||
7897 | /* We can have just one union field set. */ | |
7898 | constructor_elements = NULL; | |
7899 | } | |
7900 | ||
7901 | /* Otherwise, output this element either to | |
7902 | constructor_elements or to the assembler file. */ | |
7903 | ||
7904 | constructor_elt celt = {field, value}; | |
7905 | vec_safe_push (constructor_elements, celt); | |
7906 | ||
7907 | /* Advance the variable that indicates sequential elements output. */ | |
7908 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7909 | constructor_unfilled_index | |
7910 | = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index, | |
7911 | bitsize_one_node); | |
7912 | else if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
7913 | { | |
7914 | constructor_unfilled_fields | |
7915 | = DECL_CHAIN (constructor_unfilled_fields); | |
7916 | ||
7917 | /* Skip any nameless bit fields. */ | |
7918 | while (constructor_unfilled_fields != 0 | |
7919 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
7920 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
7921 | constructor_unfilled_fields = | |
7922 | DECL_CHAIN (constructor_unfilled_fields); | |
7923 | } | |
7924 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
7925 | constructor_unfilled_fields = 0; | |
7926 | ||
7927 | /* Now output any pending elements which have become next. */ | |
7928 | if (pending) | |
7929 | output_pending_init_elements (0, braced_init_obstack); | |
7930 | } | |
7931 | ||
7932 | /* Output any pending elements which have become next. | |
7933 | As we output elements, constructor_unfilled_{fields,index} | |
7934 | advances, which may cause other elements to become next; | |
7935 | if so, they too are output. | |
7936 | ||
7937 | If ALL is 0, we return when there are | |
7938 | no more pending elements to output now. | |
7939 | ||
7940 | If ALL is 1, we output space as necessary so that | |
7941 | we can output all the pending elements. */ | |
7942 | static void | |
7943 | output_pending_init_elements (int all, struct obstack * braced_init_obstack) | |
7944 | { | |
7945 | struct init_node *elt = constructor_pending_elts; | |
7946 | tree next; | |
7947 | ||
7948 | retry: | |
7949 | ||
7950 | /* Look through the whole pending tree. | |
7951 | If we find an element that should be output now, | |
7952 | output it. Otherwise, set NEXT to the element | |
7953 | that comes first among those still pending. */ | |
7954 | ||
7955 | next = 0; | |
7956 | while (elt) | |
7957 | { | |
7958 | if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
7959 | { | |
7960 | if (tree_int_cst_equal (elt->purpose, | |
7961 | constructor_unfilled_index)) | |
7962 | output_init_element (elt->value, elt->origtype, true, | |
7963 | TREE_TYPE (constructor_type), | |
7964 | constructor_unfilled_index, 0, false, | |
7965 | braced_init_obstack); | |
7966 | else if (tree_int_cst_lt (constructor_unfilled_index, | |
7967 | elt->purpose)) | |
7968 | { | |
7969 | /* Advance to the next smaller node. */ | |
7970 | if (elt->left) | |
7971 | elt = elt->left; | |
7972 | else | |
7973 | { | |
7974 | /* We have reached the smallest node bigger than the | |
7975 | current unfilled index. Fill the space first. */ | |
7976 | next = elt->purpose; | |
7977 | break; | |
7978 | } | |
7979 | } | |
7980 | else | |
7981 | { | |
7982 | /* Advance to the next bigger node. */ | |
7983 | if (elt->right) | |
7984 | elt = elt->right; | |
7985 | else | |
7986 | { | |
7987 | /* We have reached the biggest node in a subtree. Find | |
7988 | the parent of it, which is the next bigger node. */ | |
7989 | while (elt->parent && elt->parent->right == elt) | |
7990 | elt = elt->parent; | |
7991 | elt = elt->parent; | |
7992 | if (elt && tree_int_cst_lt (constructor_unfilled_index, | |
7993 | elt->purpose)) | |
7994 | { | |
7995 | next = elt->purpose; | |
7996 | break; | |
7997 | } | |
7998 | } | |
7999 | } | |
8000 | } | |
8001 | else if (TREE_CODE (constructor_type) == RECORD_TYPE | |
8002 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8003 | { | |
8004 | tree ctor_unfilled_bitpos, elt_bitpos; | |
8005 | ||
8006 | /* If the current record is complete we are done. */ | |
8007 | if (constructor_unfilled_fields == 0) | |
8008 | break; | |
8009 | ||
8010 | ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields); | |
8011 | elt_bitpos = bit_position (elt->purpose); | |
8012 | /* We can't compare fields here because there might be empty | |
8013 | fields in between. */ | |
8014 | if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos)) | |
8015 | { | |
8016 | constructor_unfilled_fields = elt->purpose; | |
8017 | output_init_element (elt->value, elt->origtype, true, | |
8018 | TREE_TYPE (elt->purpose), | |
8019 | elt->purpose, 0, false, | |
8020 | braced_init_obstack); | |
8021 | } | |
8022 | else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos)) | |
8023 | { | |
8024 | /* Advance to the next smaller node. */ | |
8025 | if (elt->left) | |
8026 | elt = elt->left; | |
8027 | else | |
8028 | { | |
8029 | /* We have reached the smallest node bigger than the | |
8030 | current unfilled field. Fill the space first. */ | |
8031 | next = elt->purpose; | |
8032 | break; | |
8033 | } | |
8034 | } | |
8035 | else | |
8036 | { | |
8037 | /* Advance to the next bigger node. */ | |
8038 | if (elt->right) | |
8039 | elt = elt->right; | |
8040 | else | |
8041 | { | |
8042 | /* We have reached the biggest node in a subtree. Find | |
8043 | the parent of it, which is the next bigger node. */ | |
8044 | while (elt->parent && elt->parent->right == elt) | |
8045 | elt = elt->parent; | |
8046 | elt = elt->parent; | |
8047 | if (elt | |
8048 | && (tree_int_cst_lt (ctor_unfilled_bitpos, | |
8049 | bit_position (elt->purpose)))) | |
8050 | { | |
8051 | next = elt->purpose; | |
8052 | break; | |
8053 | } | |
8054 | } | |
8055 | } | |
8056 | } | |
8057 | } | |
8058 | ||
8059 | /* Ordinarily return, but not if we want to output all | |
8060 | and there are elements left. */ | |
8061 | if (!(all && next != 0)) | |
8062 | return; | |
8063 | ||
8064 | /* If it's not incremental, just skip over the gap, so that after | |
8065 | jumping to retry we will output the next successive element. */ | |
8066 | if (TREE_CODE (constructor_type) == RECORD_TYPE | |
8067 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8068 | constructor_unfilled_fields = next; | |
8069 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8070 | constructor_unfilled_index = next; | |
8071 | ||
8072 | /* ELT now points to the node in the pending tree with the next | |
8073 | initializer to output. */ | |
8074 | goto retry; | |
8075 | } | |
8076 | \f | |
8077 | /* Add one non-braced element to the current constructor level. | |
8078 | This adjusts the current position within the constructor's type. | |
8079 | This may also start or terminate implicit levels | |
8080 | to handle a partly-braced initializer. | |
8081 | ||
8082 | Once this has found the correct level for the new element, | |
8083 | it calls output_init_element. | |
8084 | ||
8085 | IMPLICIT is true if value comes from pop_init_level (1), | |
8086 | the new initializer has been merged with the existing one | |
8087 | and thus no warnings should be emitted about overriding an | |
8088 | existing initializer. */ | |
8089 | ||
8090 | void | |
8091 | process_init_element (struct c_expr value, bool implicit, | |
8092 | struct obstack * braced_init_obstack) | |
8093 | { | |
8094 | tree orig_value = value.value; | |
8095 | int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST; | |
8096 | bool strict_string = value.original_code == STRING_CST; | |
8097 | ||
8098 | designator_depth = 0; | |
8099 | designator_erroneous = 0; | |
8100 | ||
8101 | /* Handle superfluous braces around string cst as in | |
8102 | char x[] = {"foo"}; */ | |
8103 | if (string_flag | |
8104 | && constructor_type | |
8105 | && TREE_CODE (constructor_type) == ARRAY_TYPE | |
8106 | && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type)) | |
8107 | && integer_zerop (constructor_unfilled_index)) | |
8108 | { | |
8109 | if (constructor_stack->replacement_value.value) | |
8110 | error_init ("excess elements in char array initializer"); | |
8111 | constructor_stack->replacement_value = value; | |
8112 | return; | |
8113 | } | |
8114 | ||
8115 | if (constructor_stack->replacement_value.value != 0) | |
8116 | { | |
8117 | error_init ("excess elements in struct initializer"); | |
8118 | return; | |
8119 | } | |
8120 | ||
8121 | /* Ignore elements of a brace group if it is entirely superfluous | |
8122 | and has already been diagnosed. */ | |
8123 | if (constructor_type == 0) | |
8124 | return; | |
8125 | ||
8126 | /* If we've exhausted any levels that didn't have braces, | |
8127 | pop them now. */ | |
8128 | while (constructor_stack->implicit) | |
8129 | { | |
8130 | if ((TREE_CODE (constructor_type) == RECORD_TYPE | |
8131 | || TREE_CODE (constructor_type) == UNION_TYPE) | |
8132 | && constructor_fields == 0) | |
8133 | process_init_element (pop_init_level (1, braced_init_obstack), | |
8134 | true, braced_init_obstack); | |
8135 | else if ((TREE_CODE (constructor_type) == ARRAY_TYPE | |
8136 | || TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8137 | && constructor_max_index | |
8138 | && tree_int_cst_lt (constructor_max_index, | |
8139 | constructor_index)) | |
8140 | process_init_element (pop_init_level (1, braced_init_obstack), | |
8141 | true, braced_init_obstack); | |
8142 | else | |
8143 | break; | |
8144 | } | |
8145 | ||
8146 | /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */ | |
8147 | if (constructor_range_stack) | |
8148 | { | |
8149 | /* If value is a compound literal and we'll be just using its | |
8150 | content, don't put it into a SAVE_EXPR. */ | |
8151 | if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR | |
8152 | || !require_constant_value | |
8153 | || flag_isoc99) | |
8154 | { | |
8155 | tree semantic_type = NULL_TREE; | |
8156 | if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR) | |
8157 | { | |
8158 | semantic_type = TREE_TYPE (value.value); | |
8159 | value.value = TREE_OPERAND (value.value, 0); | |
8160 | } | |
8161 | value.value = c_save_expr (value.value); | |
8162 | if (semantic_type) | |
8163 | value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type, | |
8164 | value.value); | |
8165 | } | |
8166 | } | |
8167 | ||
8168 | while (1) | |
8169 | { | |
8170 | if (TREE_CODE (constructor_type) == RECORD_TYPE) | |
8171 | { | |
8172 | tree fieldtype; | |
8173 | enum tree_code fieldcode; | |
8174 | ||
8175 | if (constructor_fields == 0) | |
8176 | { | |
8177 | pedwarn_init (input_location, 0, | |
8178 | "excess elements in struct initializer"); | |
8179 | break; | |
8180 | } | |
8181 | ||
8182 | fieldtype = TREE_TYPE (constructor_fields); | |
8183 | if (fieldtype != error_mark_node) | |
8184 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8185 | fieldcode = TREE_CODE (fieldtype); | |
8186 | ||
8187 | /* Error for non-static initialization of a flexible array member. */ | |
8188 | if (fieldcode == ARRAY_TYPE | |
8189 | && !require_constant_value | |
8190 | && TYPE_SIZE (fieldtype) == NULL_TREE | |
8191 | && DECL_CHAIN (constructor_fields) == NULL_TREE) | |
8192 | { | |
8193 | error_init ("non-static initialization of a flexible array member"); | |
8194 | break; | |
8195 | } | |
8196 | ||
8197 | /* Accept a string constant to initialize a subarray. */ | |
8198 | if (value.value != 0 | |
8199 | && fieldcode == ARRAY_TYPE | |
8200 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) | |
8201 | && string_flag) | |
8202 | value.value = orig_value; | |
8203 | /* Otherwise, if we have come to a subaggregate, | |
8204 | and we don't have an element of its type, push into it. */ | |
8205 | else if (value.value != 0 | |
8206 | && value.value != error_mark_node | |
8207 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
8208 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE | |
8209 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) | |
8210 | { | |
8211 | push_init_level (1, braced_init_obstack); | |
8212 | continue; | |
8213 | } | |
8214 | ||
8215 | if (value.value) | |
8216 | { | |
8217 | push_member_name (constructor_fields); | |
8218 | output_init_element (value.value, value.original_type, | |
8219 | strict_string, fieldtype, | |
8220 | constructor_fields, 1, implicit, | |
8221 | braced_init_obstack); | |
8222 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
8223 | } | |
8224 | else | |
8225 | /* Do the bookkeeping for an element that was | |
8226 | directly output as a constructor. */ | |
8227 | { | |
8228 | /* For a record, keep track of end position of last field. */ | |
8229 | if (DECL_SIZE (constructor_fields)) | |
8230 | constructor_bit_index | |
8231 | = size_binop_loc (input_location, PLUS_EXPR, | |
8232 | bit_position (constructor_fields), | |
8233 | DECL_SIZE (constructor_fields)); | |
8234 | ||
8235 | /* If the current field was the first one not yet written out, | |
8236 | it isn't now, so update. */ | |
8237 | if (constructor_unfilled_fields == constructor_fields) | |
8238 | { | |
8239 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); | |
8240 | /* Skip any nameless bit fields. */ | |
8241 | while (constructor_unfilled_fields != 0 | |
8242 | && DECL_C_BIT_FIELD (constructor_unfilled_fields) | |
8243 | && DECL_NAME (constructor_unfilled_fields) == 0) | |
8244 | constructor_unfilled_fields = | |
8245 | DECL_CHAIN (constructor_unfilled_fields); | |
8246 | } | |
8247 | } | |
8248 | ||
8249 | constructor_fields = DECL_CHAIN (constructor_fields); | |
8250 | /* Skip any nameless bit fields at the beginning. */ | |
8251 | while (constructor_fields != 0 | |
8252 | && DECL_C_BIT_FIELD (constructor_fields) | |
8253 | && DECL_NAME (constructor_fields) == 0) | |
8254 | constructor_fields = DECL_CHAIN (constructor_fields); | |
8255 | } | |
8256 | else if (TREE_CODE (constructor_type) == UNION_TYPE) | |
8257 | { | |
8258 | tree fieldtype; | |
8259 | enum tree_code fieldcode; | |
8260 | ||
8261 | if (constructor_fields == 0) | |
8262 | { | |
8263 | pedwarn_init (input_location, 0, | |
8264 | "excess elements in union initializer"); | |
8265 | break; | |
8266 | } | |
8267 | ||
8268 | fieldtype = TREE_TYPE (constructor_fields); | |
8269 | if (fieldtype != error_mark_node) | |
8270 | fieldtype = TYPE_MAIN_VARIANT (fieldtype); | |
8271 | fieldcode = TREE_CODE (fieldtype); | |
8272 | ||
8273 | /* Warn that traditional C rejects initialization of unions. | |
8274 | We skip the warning if the value is zero. This is done | |
8275 | under the assumption that the zero initializer in user | |
8276 | code appears conditioned on e.g. __STDC__ to avoid | |
8277 | "missing initializer" warnings and relies on default | |
8278 | initialization to zero in the traditional C case. | |
8279 | We also skip the warning if the initializer is designated, | |
8280 | again on the assumption that this must be conditional on | |
8281 | __STDC__ anyway (and we've already complained about the | |
8282 | member-designator already). */ | |
8283 | if (!in_system_header && !constructor_designated | |
8284 | && !(value.value && (integer_zerop (value.value) | |
8285 | || real_zerop (value.value)))) | |
8286 | warning (OPT_Wtraditional, "traditional C rejects initialization " | |
8287 | "of unions"); | |
8288 | ||
8289 | /* Accept a string constant to initialize a subarray. */ | |
8290 | if (value.value != 0 | |
8291 | && fieldcode == ARRAY_TYPE | |
8292 | && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype)) | |
8293 | && string_flag) | |
8294 | value.value = orig_value; | |
8295 | /* Otherwise, if we have come to a subaggregate, | |
8296 | and we don't have an element of its type, push into it. */ | |
8297 | else if (value.value != 0 | |
8298 | && value.value != error_mark_node | |
8299 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype | |
8300 | && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE | |
8301 | || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE)) | |
8302 | { | |
8303 | push_init_level (1, braced_init_obstack); | |
8304 | continue; | |
8305 | } | |
8306 | ||
8307 | if (value.value) | |
8308 | { | |
8309 | push_member_name (constructor_fields); | |
8310 | output_init_element (value.value, value.original_type, | |
8311 | strict_string, fieldtype, | |
8312 | constructor_fields, 1, implicit, | |
8313 | braced_init_obstack); | |
8314 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
8315 | } | |
8316 | else | |
8317 | /* Do the bookkeeping for an element that was | |
8318 | directly output as a constructor. */ | |
8319 | { | |
8320 | constructor_bit_index = DECL_SIZE (constructor_fields); | |
8321 | constructor_unfilled_fields = DECL_CHAIN (constructor_fields); | |
8322 | } | |
8323 | ||
8324 | constructor_fields = 0; | |
8325 | } | |
8326 | else if (TREE_CODE (constructor_type) == ARRAY_TYPE) | |
8327 | { | |
8328 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8329 | enum tree_code eltcode = TREE_CODE (elttype); | |
8330 | ||
8331 | /* Accept a string constant to initialize a subarray. */ | |
8332 | if (value.value != 0 | |
8333 | && eltcode == ARRAY_TYPE | |
8334 | && INTEGRAL_TYPE_P (TREE_TYPE (elttype)) | |
8335 | && string_flag) | |
8336 | value.value = orig_value; | |
8337 | /* Otherwise, if we have come to a subaggregate, | |
8338 | and we don't have an element of its type, push into it. */ | |
8339 | else if (value.value != 0 | |
8340 | && value.value != error_mark_node | |
8341 | && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype | |
8342 | && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE | |
8343 | || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE)) | |
8344 | { | |
8345 | push_init_level (1, braced_init_obstack); | |
8346 | continue; | |
8347 | } | |
8348 | ||
8349 | if (constructor_max_index != 0 | |
8350 | && (tree_int_cst_lt (constructor_max_index, constructor_index) | |
8351 | || integer_all_onesp (constructor_max_index))) | |
8352 | { | |
8353 | pedwarn_init (input_location, 0, | |
8354 | "excess elements in array initializer"); | |
8355 | break; | |
8356 | } | |
8357 | ||
8358 | /* Now output the actual element. */ | |
8359 | if (value.value) | |
8360 | { | |
8361 | push_array_bounds (tree_low_cst (constructor_index, 1)); | |
8362 | output_init_element (value.value, value.original_type, | |
8363 | strict_string, elttype, | |
8364 | constructor_index, 1, implicit, | |
8365 | braced_init_obstack); | |
8366 | RESTORE_SPELLING_DEPTH (constructor_depth); | |
8367 | } | |
8368 | ||
8369 | constructor_index | |
8370 | = size_binop_loc (input_location, PLUS_EXPR, | |
8371 | constructor_index, bitsize_one_node); | |
8372 | ||
8373 | if (!value.value) | |
8374 | /* If we are doing the bookkeeping for an element that was | |
8375 | directly output as a constructor, we must update | |
8376 | constructor_unfilled_index. */ | |
8377 | constructor_unfilled_index = constructor_index; | |
8378 | } | |
8379 | else if (TREE_CODE (constructor_type) == VECTOR_TYPE) | |
8380 | { | |
8381 | tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type)); | |
8382 | ||
8383 | /* Do a basic check of initializer size. Note that vectors | |
8384 | always have a fixed size derived from their type. */ | |
8385 | if (tree_int_cst_lt (constructor_max_index, constructor_index)) | |
8386 | { | |
8387 | pedwarn_init (input_location, 0, | |
8388 | "excess elements in vector initializer"); | |
8389 | break; | |
8390 | } | |
8391 | ||
8392 | /* Now output the actual element. */ | |
8393 | if (value.value) | |
8394 | { | |
8395 | if (TREE_CODE (value.value) == VECTOR_CST) | |
8396 | elttype = TYPE_MAIN_VARIANT (constructor_type); | |
8397 | output_init_element (value.value, value.original_type, | |
8398 | strict_string, elttype, | |
8399 | constructor_index, 1, implicit, | |
8400 | braced_init_obstack); | |
8401 | } | |
8402 | ||
8403 | constructor_index | |
8404 | = size_binop_loc (input_location, | |
8405 | PLUS_EXPR, constructor_index, bitsize_one_node); | |
8406 | ||
8407 | if (!value.value) | |
8408 | /* If we are doing the bookkeeping for an element that was | |
8409 | directly output as a constructor, we must update | |
8410 | constructor_unfilled_index. */ | |
8411 | constructor_unfilled_index = constructor_index; | |
8412 | } | |
8413 | ||
8414 | /* Handle the sole element allowed in a braced initializer | |
8415 | for a scalar variable. */ | |
8416 | else if (constructor_type != error_mark_node | |
8417 | && constructor_fields == 0) | |
8418 | { | |
8419 | pedwarn_init (input_location, 0, | |
8420 | "excess elements in scalar initializer"); | |
8421 | break; | |
8422 | } | |
8423 | else | |
8424 | { | |
8425 | if (value.value) | |
8426 | output_init_element (value.value, value.original_type, | |
8427 | strict_string, constructor_type, | |
8428 | NULL_TREE, 1, implicit, | |
8429 | braced_init_obstack); | |
8430 | constructor_fields = 0; | |
8431 | } | |
8432 | ||
8433 | /* Handle range initializers either at this level or anywhere higher | |
8434 | in the designator stack. */ | |
8435 | if (constructor_range_stack) | |
8436 | { | |
8437 | struct constructor_range_stack *p, *range_stack; | |
8438 | int finish = 0; | |
8439 | ||
8440 | range_stack = constructor_range_stack; | |
8441 | constructor_range_stack = 0; | |
8442 | while (constructor_stack != range_stack->stack) | |
8443 | { | |
8444 | gcc_assert (constructor_stack->implicit); | |
8445 | process_init_element (pop_init_level (1, | |
8446 | braced_init_obstack), | |
8447 | true, braced_init_obstack); | |
8448 | } | |
8449 | for (p = range_stack; | |
8450 | !p->range_end || tree_int_cst_equal (p->index, p->range_end); | |
8451 | p = p->prev) | |
8452 | { | |
8453 | gcc_assert (constructor_stack->implicit); | |
8454 | process_init_element (pop_init_level (1, braced_init_obstack), | |
8455 | true, braced_init_obstack); | |
8456 | } | |
8457 | ||
8458 | p->index = size_binop_loc (input_location, | |
8459 | PLUS_EXPR, p->index, bitsize_one_node); | |
8460 | if (tree_int_cst_equal (p->index, p->range_end) && !p->prev) | |
8461 | finish = 1; | |
8462 | ||
8463 | while (1) | |
8464 | { | |
8465 | constructor_index = p->index; | |
8466 | constructor_fields = p->fields; | |
8467 | if (finish && p->range_end && p->index == p->range_start) | |
8468 | { | |
8469 | finish = 0; | |
8470 | p->prev = 0; | |
8471 | } | |
8472 | p = p->next; | |
8473 | if (!p) | |
8474 | break; | |
8475 | push_init_level (2, braced_init_obstack); | |
8476 | p->stack = constructor_stack; | |
8477 | if (p->range_end && tree_int_cst_equal (p->index, p->range_end)) | |
8478 | p->index = p->range_start; | |
8479 | } | |
8480 | ||
8481 | if (!finish) | |
8482 | constructor_range_stack = range_stack; | |
8483 | continue; | |
8484 | } | |
8485 | ||
8486 | break; | |
8487 | } | |
8488 | ||
8489 | constructor_range_stack = 0; | |
8490 | } | |
8491 | \f | |
8492 | /* Build a complete asm-statement, whose components are a CV_QUALIFIER | |
8493 | (guaranteed to be 'volatile' or null) and ARGS (represented using | |
8494 | an ASM_EXPR node). */ | |
8495 | tree | |
8496 | build_asm_stmt (tree cv_qualifier, tree args) | |
8497 | { | |
8498 | if (!ASM_VOLATILE_P (args) && cv_qualifier) | |
8499 | ASM_VOLATILE_P (args) = 1; | |
8500 | return add_stmt (args); | |
8501 | } | |
8502 | ||
8503 | /* Build an asm-expr, whose components are a STRING, some OUTPUTS, | |
8504 | some INPUTS, and some CLOBBERS. The latter three may be NULL. | |
8505 | SIMPLE indicates whether there was anything at all after the | |
8506 | string in the asm expression -- asm("blah") and asm("blah" : ) | |
8507 | are subtly different. We use a ASM_EXPR node to represent this. */ | |
8508 | tree | |
8509 | build_asm_expr (location_t loc, tree string, tree outputs, tree inputs, | |
8510 | tree clobbers, tree labels, bool simple) | |
8511 | { | |
8512 | tree tail; | |
8513 | tree args; | |
8514 | int i; | |
8515 | const char *constraint; | |
8516 | const char **oconstraints; | |
8517 | bool allows_mem, allows_reg, is_inout; | |
8518 | int ninputs, noutputs; | |
8519 | ||
8520 | ninputs = list_length (inputs); | |
8521 | noutputs = list_length (outputs); | |
8522 | oconstraints = (const char **) alloca (noutputs * sizeof (const char *)); | |
8523 | ||
8524 | string = resolve_asm_operand_names (string, outputs, inputs, labels); | |
8525 | ||
8526 | /* Remove output conversions that change the type but not the mode. */ | |
8527 | for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
8528 | { | |
8529 | tree output = TREE_VALUE (tail); | |
8530 | ||
8531 | output = c_fully_fold (output, false, NULL); | |
8532 | ||
8533 | /* ??? Really, this should not be here. Users should be using a | |
8534 | proper lvalue, dammit. But there's a long history of using casts | |
8535 | in the output operands. In cases like longlong.h, this becomes a | |
8536 | primitive form of typechecking -- if the cast can be removed, then | |
8537 | the output operand had a type of the proper width; otherwise we'll | |
8538 | get an error. Gross, but ... */ | |
8539 | STRIP_NOPS (output); | |
8540 | ||
8541 | if (!lvalue_or_else (loc, output, lv_asm)) | |
8542 | output = error_mark_node; | |
8543 | ||
8544 | if (output != error_mark_node | |
8545 | && (TREE_READONLY (output) | |
8546 | || TYPE_READONLY (TREE_TYPE (output)) | |
8547 | || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE | |
8548 | || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE) | |
8549 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (output))))) | |
8550 | readonly_error (output, lv_asm); | |
8551 | ||
8552 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8553 | oconstraints[i] = constraint; | |
8554 | ||
8555 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
8556 | &allows_mem, &allows_reg, &is_inout)) | |
8557 | { | |
8558 | /* If the operand is going to end up in memory, | |
8559 | mark it addressable. */ | |
8560 | if (!allows_reg && !c_mark_addressable (output)) | |
8561 | output = error_mark_node; | |
8562 | if (!(!allows_reg && allows_mem) | |
8563 | && output != error_mark_node | |
8564 | && VOID_TYPE_P (TREE_TYPE (output))) | |
8565 | { | |
8566 | error_at (loc, "invalid use of void expression"); | |
8567 | output = error_mark_node; | |
8568 | } | |
8569 | } | |
8570 | else | |
8571 | output = error_mark_node; | |
8572 | ||
8573 | TREE_VALUE (tail) = output; | |
8574 | } | |
8575 | ||
8576 | for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
8577 | { | |
8578 | tree input; | |
8579 | ||
8580 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
8581 | input = TREE_VALUE (tail); | |
8582 | ||
8583 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, | |
8584 | oconstraints, &allows_mem, &allows_reg)) | |
8585 | { | |
8586 | /* If the operand is going to end up in memory, | |
8587 | mark it addressable. */ | |
8588 | if (!allows_reg && allows_mem) | |
8589 | { | |
8590 | input = c_fully_fold (input, false, NULL); | |
8591 | ||
8592 | /* Strip the nops as we allow this case. FIXME, this really | |
8593 | should be rejected or made deprecated. */ | |
8594 | STRIP_NOPS (input); | |
8595 | if (!c_mark_addressable (input)) | |
8596 | input = error_mark_node; | |
8597 | } | |
8598 | else | |
8599 | { | |
8600 | struct c_expr expr; | |
8601 | memset (&expr, 0, sizeof (expr)); | |
8602 | expr.value = input; | |
8603 | expr = default_function_array_conversion (loc, expr); | |
8604 | input = c_fully_fold (expr.value, false, NULL); | |
8605 | ||
8606 | if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input))) | |
8607 | { | |
8608 | error_at (loc, "invalid use of void expression"); | |
8609 | input = error_mark_node; | |
8610 | } | |
8611 | } | |
8612 | } | |
8613 | else | |
8614 | input = error_mark_node; | |
8615 | ||
8616 | TREE_VALUE (tail) = input; | |
8617 | } | |
8618 | ||
8619 | /* ASMs with labels cannot have outputs. This should have been | |
8620 | enforced by the parser. */ | |
8621 | gcc_assert (outputs == NULL || labels == NULL); | |
8622 | ||
8623 | args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels); | |
8624 | ||
8625 | /* asm statements without outputs, including simple ones, are treated | |
8626 | as volatile. */ | |
8627 | ASM_INPUT_P (args) = simple; | |
8628 | ASM_VOLATILE_P (args) = (noutputs == 0); | |
8629 | ||
8630 | return args; | |
8631 | } | |
8632 | \f | |
8633 | /* Generate a goto statement to LABEL. LOC is the location of the | |
8634 | GOTO. */ | |
8635 | ||
8636 | tree | |
8637 | c_finish_goto_label (location_t loc, tree label) | |
8638 | { | |
8639 | tree decl = lookup_label_for_goto (loc, label); | |
8640 | if (!decl) | |
8641 | return NULL_TREE; | |
8642 | TREE_USED (decl) = 1; | |
8643 | { | |
8644 | tree t = build1 (GOTO_EXPR, void_type_node, decl); | |
8645 | SET_EXPR_LOCATION (t, loc); | |
8646 | return add_stmt (t); | |
8647 | } | |
8648 | } | |
8649 | ||
8650 | /* Generate a computed goto statement to EXPR. LOC is the location of | |
8651 | the GOTO. */ | |
8652 | ||
8653 | tree | |
8654 | c_finish_goto_ptr (location_t loc, tree expr) | |
8655 | { | |
8656 | tree t; | |
8657 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>"); | |
8658 | expr = c_fully_fold (expr, false, NULL); | |
8659 | expr = convert (ptr_type_node, expr); | |
8660 | t = build1 (GOTO_EXPR, void_type_node, expr); | |
8661 | SET_EXPR_LOCATION (t, loc); | |
8662 | return add_stmt (t); | |
8663 | } | |
8664 | ||
8665 | /* Generate a C `return' statement. RETVAL is the expression for what | |
8666 | to return, or a null pointer for `return;' with no value. LOC is | |
8667 | the location of the return statement. If ORIGTYPE is not NULL_TREE, it | |
8668 | is the original type of RETVAL. */ | |
8669 | ||
8670 | tree | |
8671 | c_finish_return (location_t loc, tree retval, tree origtype) | |
8672 | { | |
8673 | tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt; | |
8674 | bool no_warning = false; | |
8675 | bool npc = false; | |
8676 | size_t rank = 0; | |
8677 | ||
8678 | if (TREE_THIS_VOLATILE (current_function_decl)) | |
8679 | warning_at (loc, 0, | |
8680 | "function declared %<noreturn%> has a %<return%> statement"); | |
8681 | ||
8682 | if (flag_enable_cilkplus && contains_array_notation_expr (retval)) | |
8683 | { | |
8684 | /* Array notations are allowed in a return statement if it is inside a | |
8685 | built-in array notation reduction function. */ | |
8686 | if (!find_rank (loc, retval, retval, false, &rank)) | |
8687 | return error_mark_node; | |
8688 | if (rank >= 1) | |
8689 | { | |
8690 | error_at (loc, "array notation expression cannot be used as a " | |
8691 | "return value"); | |
8692 | return error_mark_node; | |
8693 | } | |
8694 | } | |
8695 | if (retval) | |
8696 | { | |
8697 | tree semantic_type = NULL_TREE; | |
8698 | npc = null_pointer_constant_p (retval); | |
8699 | if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR) | |
8700 | { | |
8701 | semantic_type = TREE_TYPE (retval); | |
8702 | retval = TREE_OPERAND (retval, 0); | |
8703 | } | |
8704 | retval = c_fully_fold (retval, false, NULL); | |
8705 | if (semantic_type) | |
8706 | retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval); | |
8707 | } | |
8708 | ||
8709 | if (!retval) | |
8710 | { | |
8711 | current_function_returns_null = 1; | |
8712 | if ((warn_return_type || flag_isoc99) | |
8713 | && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE) | |
8714 | { | |
8715 | pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type, | |
8716 | "%<return%> with no value, in " | |
8717 | "function returning non-void"); | |
8718 | no_warning = true; | |
8719 | } | |
8720 | } | |
8721 | else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE) | |
8722 | { | |
8723 | current_function_returns_null = 1; | |
8724 | if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) | |
8725 | pedwarn (loc, 0, | |
8726 | "%<return%> with a value, in function returning void"); | |
8727 | else | |
8728 | pedwarn (loc, OPT_Wpedantic, "ISO C forbids " | |
8729 | "%<return%> with expression, in function returning void"); | |
8730 | } | |
8731 | else | |
8732 | { | |
8733 | tree t = convert_for_assignment (loc, valtype, retval, origtype, | |
8734 | ic_return, | |
8735 | npc, NULL_TREE, NULL_TREE, 0); | |
8736 | tree res = DECL_RESULT (current_function_decl); | |
8737 | tree inner; | |
8738 | bool save; | |
8739 | ||
8740 | current_function_returns_value = 1; | |
8741 | if (t == error_mark_node) | |
8742 | return NULL_TREE; | |
8743 | ||
8744 | save = in_late_binary_op; | |
8745 | if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE | |
8746 | || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE) | |
8747 | in_late_binary_op = true; | |
8748 | inner = t = convert (TREE_TYPE (res), t); | |
8749 | in_late_binary_op = save; | |
8750 | ||
8751 | /* Strip any conversions, additions, and subtractions, and see if | |
8752 | we are returning the address of a local variable. Warn if so. */ | |
8753 | while (1) | |
8754 | { | |
8755 | switch (TREE_CODE (inner)) | |
8756 | { | |
8757 | CASE_CONVERT: | |
8758 | case NON_LVALUE_EXPR: | |
8759 | case PLUS_EXPR: | |
8760 | case POINTER_PLUS_EXPR: | |
8761 | inner = TREE_OPERAND (inner, 0); | |
8762 | continue; | |
8763 | ||
8764 | case MINUS_EXPR: | |
8765 | /* If the second operand of the MINUS_EXPR has a pointer | |
8766 | type (or is converted from it), this may be valid, so | |
8767 | don't give a warning. */ | |
8768 | { | |
8769 | tree op1 = TREE_OPERAND (inner, 1); | |
8770 | ||
8771 | while (!POINTER_TYPE_P (TREE_TYPE (op1)) | |
8772 | && (CONVERT_EXPR_P (op1) | |
8773 | || TREE_CODE (op1) == NON_LVALUE_EXPR)) | |
8774 | op1 = TREE_OPERAND (op1, 0); | |
8775 | ||
8776 | if (POINTER_TYPE_P (TREE_TYPE (op1))) | |
8777 | break; | |
8778 | ||
8779 | inner = TREE_OPERAND (inner, 0); | |
8780 | continue; | |
8781 | } | |
8782 | ||
8783 | case ADDR_EXPR: | |
8784 | inner = TREE_OPERAND (inner, 0); | |
8785 | ||
8786 | while (REFERENCE_CLASS_P (inner) | |
8787 | && TREE_CODE (inner) != INDIRECT_REF) | |
8788 | inner = TREE_OPERAND (inner, 0); | |
8789 | ||
8790 | if (DECL_P (inner) | |
8791 | && !DECL_EXTERNAL (inner) | |
8792 | && !TREE_STATIC (inner) | |
8793 | && DECL_CONTEXT (inner) == current_function_decl) | |
8794 | warning_at (loc, | |
8795 | OPT_Wreturn_local_addr, "function returns address " | |
8796 | "of local variable"); | |
8797 | break; | |
8798 | ||
8799 | default: | |
8800 | break; | |
8801 | } | |
8802 | ||
8803 | break; | |
8804 | } | |
8805 | ||
8806 | retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t); | |
8807 | SET_EXPR_LOCATION (retval, loc); | |
8808 | ||
8809 | if (warn_sequence_point) | |
8810 | verify_sequence_points (retval); | |
8811 | } | |
8812 | ||
8813 | ret_stmt = build_stmt (loc, RETURN_EXPR, retval); | |
8814 | TREE_NO_WARNING (ret_stmt) |= no_warning; | |
8815 | return add_stmt (ret_stmt); | |
8816 | } | |
8817 | \f | |
8818 | struct c_switch { | |
8819 | /* The SWITCH_EXPR being built. */ | |
8820 | tree switch_expr; | |
8821 | ||
8822 | /* The original type of the testing expression, i.e. before the | |
8823 | default conversion is applied. */ | |
8824 | tree orig_type; | |
8825 | ||
8826 | /* A splay-tree mapping the low element of a case range to the high | |
8827 | element, or NULL_TREE if there is no high element. Used to | |
8828 | determine whether or not a new case label duplicates an old case | |
8829 | label. We need a tree, rather than simply a hash table, because | |
8830 | of the GNU case range extension. */ | |
8831 | splay_tree cases; | |
8832 | ||
8833 | /* The bindings at the point of the switch. This is used for | |
8834 | warnings crossing decls when branching to a case label. */ | |
8835 | struct c_spot_bindings *bindings; | |
8836 | ||
8837 | /* The next node on the stack. */ | |
8838 | struct c_switch *next; | |
8839 | }; | |
8840 | ||
8841 | /* A stack of the currently active switch statements. The innermost | |
8842 | switch statement is on the top of the stack. There is no need to | |
8843 | mark the stack for garbage collection because it is only active | |
8844 | during the processing of the body of a function, and we never | |
8845 | collect at that point. */ | |
8846 | ||
8847 | struct c_switch *c_switch_stack; | |
8848 | ||
8849 | /* Start a C switch statement, testing expression EXP. Return the new | |
8850 | SWITCH_EXPR. SWITCH_LOC is the location of the `switch'. | |
8851 | SWITCH_COND_LOC is the location of the switch's condition. */ | |
8852 | ||
8853 | tree | |
8854 | c_start_case (location_t switch_loc, | |
8855 | location_t switch_cond_loc, | |
8856 | tree exp) | |
8857 | { | |
8858 | tree orig_type = error_mark_node; | |
8859 | struct c_switch *cs; | |
8860 | ||
8861 | if (exp != error_mark_node) | |
8862 | { | |
8863 | orig_type = TREE_TYPE (exp); | |
8864 | ||
8865 | if (!INTEGRAL_TYPE_P (orig_type)) | |
8866 | { | |
8867 | if (orig_type != error_mark_node) | |
8868 | { | |
8869 | error_at (switch_cond_loc, "switch quantity not an integer"); | |
8870 | orig_type = error_mark_node; | |
8871 | } | |
8872 | exp = integer_zero_node; | |
8873 | } | |
8874 | else | |
8875 | { | |
8876 | tree type = TYPE_MAIN_VARIANT (orig_type); | |
8877 | ||
8878 | if (!in_system_header | |
8879 | && (type == long_integer_type_node | |
8880 | || type == long_unsigned_type_node)) | |
8881 | warning_at (switch_cond_loc, | |
8882 | OPT_Wtraditional, "%<long%> switch expression not " | |
8883 | "converted to %<int%> in ISO C"); | |
8884 | ||
8885 | exp = c_fully_fold (exp, false, NULL); | |
8886 | exp = default_conversion (exp); | |
8887 | ||
8888 | if (warn_sequence_point) | |
8889 | verify_sequence_points (exp); | |
8890 | } | |
8891 | } | |
8892 | ||
8893 | /* Add this new SWITCH_EXPR to the stack. */ | |
8894 | cs = XNEW (struct c_switch); | |
8895 | cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE); | |
8896 | SET_EXPR_LOCATION (cs->switch_expr, switch_loc); | |
8897 | cs->orig_type = orig_type; | |
8898 | cs->cases = splay_tree_new (case_compare, NULL, NULL); | |
8899 | cs->bindings = c_get_switch_bindings (); | |
8900 | cs->next = c_switch_stack; | |
8901 | c_switch_stack = cs; | |
8902 | ||
8903 | return add_stmt (cs->switch_expr); | |
8904 | } | |
8905 | ||
8906 | /* Process a case label at location LOC. */ | |
8907 | ||
8908 | tree | |
8909 | do_case (location_t loc, tree low_value, tree high_value) | |
8910 | { | |
8911 | tree label = NULL_TREE; | |
8912 | ||
8913 | if (low_value && TREE_CODE (low_value) != INTEGER_CST) | |
8914 | { | |
8915 | low_value = c_fully_fold (low_value, false, NULL); | |
8916 | if (TREE_CODE (low_value) == INTEGER_CST) | |
8917 | pedwarn (input_location, OPT_Wpedantic, | |
8918 | "case label is not an integer constant expression"); | |
8919 | } | |
8920 | ||
8921 | if (high_value && TREE_CODE (high_value) != INTEGER_CST) | |
8922 | { | |
8923 | high_value = c_fully_fold (high_value, false, NULL); | |
8924 | if (TREE_CODE (high_value) == INTEGER_CST) | |
8925 | pedwarn (input_location, OPT_Wpedantic, | |
8926 | "case label is not an integer constant expression"); | |
8927 | } | |
8928 | ||
8929 | if (c_switch_stack == NULL) | |
8930 | { | |
8931 | if (low_value) | |
8932 | error_at (loc, "case label not within a switch statement"); | |
8933 | else | |
8934 | error_at (loc, "%<default%> label not within a switch statement"); | |
8935 | return NULL_TREE; | |
8936 | } | |
8937 | ||
8938 | if (c_check_switch_jump_warnings (c_switch_stack->bindings, | |
8939 | EXPR_LOCATION (c_switch_stack->switch_expr), | |
8940 | loc)) | |
8941 | return NULL_TREE; | |
8942 | ||
8943 | label = c_add_case_label (loc, c_switch_stack->cases, | |
8944 | SWITCH_COND (c_switch_stack->switch_expr), | |
8945 | c_switch_stack->orig_type, | |
8946 | low_value, high_value); | |
8947 | if (label == error_mark_node) | |
8948 | label = NULL_TREE; | |
8949 | return label; | |
8950 | } | |
8951 | ||
8952 | /* Finish the switch statement. */ | |
8953 | ||
8954 | void | |
8955 | c_finish_case (tree body) | |
8956 | { | |
8957 | struct c_switch *cs = c_switch_stack; | |
8958 | location_t switch_location; | |
8959 | ||
8960 | SWITCH_BODY (cs->switch_expr) = body; | |
8961 | ||
8962 | /* Emit warnings as needed. */ | |
8963 | switch_location = EXPR_LOCATION (cs->switch_expr); | |
8964 | c_do_switch_warnings (cs->cases, switch_location, | |
8965 | TREE_TYPE (cs->switch_expr), | |
8966 | SWITCH_COND (cs->switch_expr)); | |
8967 | ||
8968 | /* Pop the stack. */ | |
8969 | c_switch_stack = cs->next; | |
8970 | splay_tree_delete (cs->cases); | |
8971 | c_release_switch_bindings (cs->bindings); | |
8972 | XDELETE (cs); | |
8973 | } | |
8974 | \f | |
8975 | /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND, | |
8976 | THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK | |
8977 | may be null. NESTED_IF is true if THEN_BLOCK contains another IF | |
8978 | statement, and was not surrounded with parenthesis. */ | |
8979 | ||
8980 | void | |
8981 | c_finish_if_stmt (location_t if_locus, tree cond, tree then_block, | |
8982 | tree else_block, bool nested_if) | |
8983 | { | |
8984 | tree stmt; | |
8985 | ||
8986 | /* If the condition has array notations, then the rank of the then_block and | |
8987 | else_block must be either 0 or be equal to the rank of the condition. If | |
8988 | the condition does not have array notations then break them up as it is | |
8989 | broken up in a normal expression. */ | |
8990 | if (flag_enable_cilkplus && contains_array_notation_expr (cond)) | |
8991 | { | |
8992 | size_t then_rank = 0, cond_rank = 0, else_rank = 0; | |
8993 | if (!find_rank (if_locus, cond, cond, true, &cond_rank)) | |
8994 | return; | |
8995 | if (then_block | |
8996 | && !find_rank (if_locus, then_block, then_block, true, &then_rank)) | |
8997 | return; | |
8998 | if (else_block | |
8999 | && !find_rank (if_locus, else_block, else_block, true, &else_rank)) | |
9000 | return; | |
9001 | if (cond_rank != then_rank && then_rank != 0) | |
9002 | { | |
9003 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9004 | " and the then-block"); | |
9005 | return; | |
9006 | } | |
9007 | else if (cond_rank != else_rank && else_rank != 0) | |
9008 | { | |
9009 | error_at (if_locus, "rank-mismatch between if-statement%'s condition" | |
9010 | " and the else-block"); | |
9011 | return; | |
9012 | } | |
9013 | } | |
9014 | /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */ | |
9015 | if (warn_parentheses && nested_if && else_block == NULL) | |
9016 | { | |
9017 | tree inner_if = then_block; | |
9018 | ||
9019 | /* We know from the grammar productions that there is an IF nested | |
9020 | within THEN_BLOCK. Due to labels and c99 conditional declarations, | |
9021 | it might not be exactly THEN_BLOCK, but should be the last | |
9022 | non-container statement within. */ | |
9023 | while (1) | |
9024 | switch (TREE_CODE (inner_if)) | |
9025 | { | |
9026 | case COND_EXPR: | |
9027 | goto found; | |
9028 | case BIND_EXPR: | |
9029 | inner_if = BIND_EXPR_BODY (inner_if); | |
9030 | break; | |
9031 | case STATEMENT_LIST: | |
9032 | inner_if = expr_last (then_block); | |
9033 | break; | |
9034 | case TRY_FINALLY_EXPR: | |
9035 | case TRY_CATCH_EXPR: | |
9036 | inner_if = TREE_OPERAND (inner_if, 0); | |
9037 | break; | |
9038 | default: | |
9039 | gcc_unreachable (); | |
9040 | } | |
9041 | found: | |
9042 | ||
9043 | if (COND_EXPR_ELSE (inner_if)) | |
9044 | warning_at (if_locus, OPT_Wparentheses, | |
9045 | "suggest explicit braces to avoid ambiguous %<else%>"); | |
9046 | } | |
9047 | ||
9048 | stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block); | |
9049 | SET_EXPR_LOCATION (stmt, if_locus); | |
9050 | add_stmt (stmt); | |
9051 | } | |
9052 | ||
9053 | /* Emit a general-purpose loop construct. START_LOCUS is the location of | |
9054 | the beginning of the loop. COND is the loop condition. COND_IS_FIRST | |
9055 | is false for DO loops. INCR is the FOR increment expression. BODY is | |
9056 | the statement controlled by the loop. BLAB is the break label. CLAB is | |
9057 | the continue label. Everything is allowed to be NULL. */ | |
9058 | ||
9059 | void | |
9060 | c_finish_loop (location_t start_locus, tree cond, tree incr, tree body, | |
9061 | tree blab, tree clab, bool cond_is_first) | |
9062 | { | |
9063 | tree entry = NULL, exit = NULL, t; | |
9064 | ||
9065 | if (flag_enable_cilkplus && contains_array_notation_expr (cond)) | |
9066 | { | |
9067 | error_at (start_locus, "array notation expression cannot be used in a " | |
9068 | "loop%'s condition"); | |
9069 | return; | |
9070 | } | |
9071 | ||
9072 | /* If the condition is zero don't generate a loop construct. */ | |
9073 | if (cond && integer_zerop (cond)) | |
9074 | { | |
9075 | if (cond_is_first) | |
9076 | { | |
9077 | t = build_and_jump (&blab); | |
9078 | SET_EXPR_LOCATION (t, start_locus); | |
9079 | add_stmt (t); | |
9080 | } | |
9081 | } | |
9082 | else | |
9083 | { | |
9084 | tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
9085 | ||
9086 | /* If we have an exit condition, then we build an IF with gotos either | |
9087 | out of the loop, or to the top of it. If there's no exit condition, | |
9088 | then we just build a jump back to the top. */ | |
9089 | exit = build_and_jump (&LABEL_EXPR_LABEL (top)); | |
9090 | ||
9091 | if (cond && !integer_nonzerop (cond)) | |
9092 | { | |
9093 | /* Canonicalize the loop condition to the end. This means | |
9094 | generating a branch to the loop condition. Reuse the | |
9095 | continue label, if possible. */ | |
9096 | if (cond_is_first) | |
9097 | { | |
9098 | if (incr || !clab) | |
9099 | { | |
9100 | entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE); | |
9101 | t = build_and_jump (&LABEL_EXPR_LABEL (entry)); | |
9102 | } | |
9103 | else | |
9104 | t = build1 (GOTO_EXPR, void_type_node, clab); | |
9105 | SET_EXPR_LOCATION (t, start_locus); | |
9106 | add_stmt (t); | |
9107 | } | |
9108 | ||
9109 | t = build_and_jump (&blab); | |
9110 | if (cond_is_first) | |
9111 | exit = fold_build3_loc (start_locus, | |
9112 | COND_EXPR, void_type_node, cond, exit, t); | |
9113 | else | |
9114 | exit = fold_build3_loc (input_location, | |
9115 | COND_EXPR, void_type_node, cond, exit, t); | |
9116 | } | |
9117 | ||
9118 | add_stmt (top); | |
9119 | } | |
9120 | ||
9121 | if (body) | |
9122 | add_stmt (body); | |
9123 | if (clab) | |
9124 | add_stmt (build1 (LABEL_EXPR, void_type_node, clab)); | |
9125 | if (incr) | |
9126 | add_stmt (incr); | |
9127 | if (entry) | |
9128 | add_stmt (entry); | |
9129 | if (exit) | |
9130 | add_stmt (exit); | |
9131 | if (blab) | |
9132 | add_stmt (build1 (LABEL_EXPR, void_type_node, blab)); | |
9133 | } | |
9134 | ||
9135 | tree | |
9136 | c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break) | |
9137 | { | |
9138 | bool skip; | |
9139 | tree label = *label_p; | |
9140 | ||
9141 | /* In switch statements break is sometimes stylistically used after | |
9142 | a return statement. This can lead to spurious warnings about | |
9143 | control reaching the end of a non-void function when it is | |
9144 | inlined. Note that we are calling block_may_fallthru with | |
9145 | language specific tree nodes; this works because | |
9146 | block_may_fallthru returns true when given something it does not | |
9147 | understand. */ | |
9148 | skip = !block_may_fallthru (cur_stmt_list); | |
9149 | ||
9150 | if (!label) | |
9151 | { | |
9152 | if (!skip) | |
9153 | *label_p = label = create_artificial_label (loc); | |
9154 | } | |
9155 | else if (TREE_CODE (label) == LABEL_DECL) | |
9156 | ; | |
9157 | else switch (TREE_INT_CST_LOW (label)) | |
9158 | { | |
9159 | case 0: | |
9160 | if (is_break) | |
9161 | error_at (loc, "break statement not within loop or switch"); | |
9162 | else | |
9163 | error_at (loc, "continue statement not within a loop"); | |
9164 | return NULL_TREE; | |
9165 | ||
9166 | case 1: | |
9167 | gcc_assert (is_break); | |
9168 | error_at (loc, "break statement used with OpenMP for loop"); | |
9169 | return NULL_TREE; | |
9170 | ||
9171 | default: | |
9172 | gcc_unreachable (); | |
9173 | } | |
9174 | ||
9175 | if (skip) | |
9176 | return NULL_TREE; | |
9177 | ||
9178 | if (!is_break) | |
9179 | add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN)); | |
9180 | ||
9181 | return add_stmt (build1 (GOTO_EXPR, void_type_node, label)); | |
9182 | } | |
9183 | ||
9184 | /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */ | |
9185 | ||
9186 | static void | |
9187 | emit_side_effect_warnings (location_t loc, tree expr) | |
9188 | { | |
9189 | if (expr == error_mark_node) | |
9190 | ; | |
9191 | else if (!TREE_SIDE_EFFECTS (expr)) | |
9192 | { | |
9193 | if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr)) | |
9194 | warning_at (loc, OPT_Wunused_value, "statement with no effect"); | |
9195 | } | |
9196 | else | |
9197 | warn_if_unused_value (expr, loc); | |
9198 | } | |
9199 | ||
9200 | /* Process an expression as if it were a complete statement. Emit | |
9201 | diagnostics, but do not call ADD_STMT. LOC is the location of the | |
9202 | statement. */ | |
9203 | ||
9204 | tree | |
9205 | c_process_expr_stmt (location_t loc, tree expr) | |
9206 | { | |
9207 | tree exprv; | |
9208 | ||
9209 | if (!expr) | |
9210 | return NULL_TREE; | |
9211 | ||
9212 | expr = c_fully_fold (expr, false, NULL); | |
9213 | ||
9214 | if (warn_sequence_point) | |
9215 | verify_sequence_points (expr); | |
9216 | ||
9217 | if (TREE_TYPE (expr) != error_mark_node | |
9218 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) | |
9219 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) | |
9220 | error_at (loc, "expression statement has incomplete type"); | |
9221 | ||
9222 | /* If we're not processing a statement expression, warn about unused values. | |
9223 | Warnings for statement expressions will be emitted later, once we figure | |
9224 | out which is the result. */ | |
9225 | if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
9226 | && warn_unused_value) | |
9227 | emit_side_effect_warnings (loc, expr); | |
9228 | ||
9229 | exprv = expr; | |
9230 | while (TREE_CODE (exprv) == COMPOUND_EXPR) | |
9231 | exprv = TREE_OPERAND (exprv, 1); | |
9232 | while (CONVERT_EXPR_P (exprv)) | |
9233 | exprv = TREE_OPERAND (exprv, 0); | |
9234 | if (DECL_P (exprv) | |
9235 | || handled_component_p (exprv) | |
9236 | || TREE_CODE (exprv) == ADDR_EXPR) | |
9237 | mark_exp_read (exprv); | |
9238 | ||
9239 | /* If the expression is not of a type to which we cannot assign a line | |
9240 | number, wrap the thing in a no-op NOP_EXPR. */ | |
9241 | if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) | |
9242 | { | |
9243 | expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
9244 | SET_EXPR_LOCATION (expr, loc); | |
9245 | } | |
9246 | ||
9247 | return expr; | |
9248 | } | |
9249 | ||
9250 | /* Emit an expression as a statement. LOC is the location of the | |
9251 | expression. */ | |
9252 | ||
9253 | tree | |
9254 | c_finish_expr_stmt (location_t loc, tree expr) | |
9255 | { | |
9256 | if (expr) | |
9257 | return add_stmt (c_process_expr_stmt (loc, expr)); | |
9258 | else | |
9259 | return NULL; | |
9260 | } | |
9261 | ||
9262 | /* Do the opposite and emit a statement as an expression. To begin, | |
9263 | create a new binding level and return it. */ | |
9264 | ||
9265 | tree | |
9266 | c_begin_stmt_expr (void) | |
9267 | { | |
9268 | tree ret; | |
9269 | ||
9270 | /* We must force a BLOCK for this level so that, if it is not expanded | |
9271 | later, there is a way to turn off the entire subtree of blocks that | |
9272 | are contained in it. */ | |
9273 | keep_next_level (); | |
9274 | ret = c_begin_compound_stmt (true); | |
9275 | ||
9276 | c_bindings_start_stmt_expr (c_switch_stack == NULL | |
9277 | ? NULL | |
9278 | : c_switch_stack->bindings); | |
9279 | ||
9280 | /* Mark the current statement list as belonging to a statement list. */ | |
9281 | STATEMENT_LIST_STMT_EXPR (ret) = 1; | |
9282 | ||
9283 | return ret; | |
9284 | } | |
9285 | ||
9286 | /* LOC is the location of the compound statement to which this body | |
9287 | belongs. */ | |
9288 | ||
9289 | tree | |
9290 | c_finish_stmt_expr (location_t loc, tree body) | |
9291 | { | |
9292 | tree last, type, tmp, val; | |
9293 | tree *last_p; | |
9294 | ||
9295 | body = c_end_compound_stmt (loc, body, true); | |
9296 | ||
9297 | c_bindings_end_stmt_expr (c_switch_stack == NULL | |
9298 | ? NULL | |
9299 | : c_switch_stack->bindings); | |
9300 | ||
9301 | /* Locate the last statement in BODY. See c_end_compound_stmt | |
9302 | about always returning a BIND_EXPR. */ | |
9303 | last_p = &BIND_EXPR_BODY (body); | |
9304 | last = BIND_EXPR_BODY (body); | |
9305 | ||
9306 | continue_searching: | |
9307 | if (TREE_CODE (last) == STATEMENT_LIST) | |
9308 | { | |
9309 | tree_stmt_iterator i; | |
9310 | ||
9311 | /* This can happen with degenerate cases like ({ }). No value. */ | |
9312 | if (!TREE_SIDE_EFFECTS (last)) | |
9313 | return body; | |
9314 | ||
9315 | /* If we're supposed to generate side effects warnings, process | |
9316 | all of the statements except the last. */ | |
9317 | if (warn_unused_value) | |
9318 | { | |
9319 | for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i)) | |
9320 | { | |
9321 | location_t tloc; | |
9322 | tree t = tsi_stmt (i); | |
9323 | ||
9324 | tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc; | |
9325 | emit_side_effect_warnings (tloc, t); | |
9326 | } | |
9327 | } | |
9328 | else | |
9329 | i = tsi_last (last); | |
9330 | last_p = tsi_stmt_ptr (i); | |
9331 | last = *last_p; | |
9332 | } | |
9333 | ||
9334 | /* If the end of the list is exception related, then the list was split | |
9335 | by a call to push_cleanup. Continue searching. */ | |
9336 | if (TREE_CODE (last) == TRY_FINALLY_EXPR | |
9337 | || TREE_CODE (last) == TRY_CATCH_EXPR) | |
9338 | { | |
9339 | last_p = &TREE_OPERAND (last, 0); | |
9340 | last = *last_p; | |
9341 | goto continue_searching; | |
9342 | } | |
9343 | ||
9344 | if (last == error_mark_node) | |
9345 | return last; | |
9346 | ||
9347 | /* In the case that the BIND_EXPR is not necessary, return the | |
9348 | expression out from inside it. */ | |
9349 | if (last == BIND_EXPR_BODY (body) | |
9350 | && BIND_EXPR_VARS (body) == NULL) | |
9351 | { | |
9352 | /* Even if this looks constant, do not allow it in a constant | |
9353 | expression. */ | |
9354 | last = c_wrap_maybe_const (last, true); | |
9355 | /* Do not warn if the return value of a statement expression is | |
9356 | unused. */ | |
9357 | TREE_NO_WARNING (last) = 1; | |
9358 | return last; | |
9359 | } | |
9360 | ||
9361 | /* Extract the type of said expression. */ | |
9362 | type = TREE_TYPE (last); | |
9363 | ||
9364 | /* If we're not returning a value at all, then the BIND_EXPR that | |
9365 | we already have is a fine expression to return. */ | |
9366 | if (!type || VOID_TYPE_P (type)) | |
9367 | return body; | |
9368 | ||
9369 | /* Now that we've located the expression containing the value, it seems | |
9370 | silly to make voidify_wrapper_expr repeat the process. Create a | |
9371 | temporary of the appropriate type and stick it in a TARGET_EXPR. */ | |
9372 | tmp = create_tmp_var_raw (type, NULL); | |
9373 | ||
9374 | /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids | |
9375 | tree_expr_nonnegative_p giving up immediately. */ | |
9376 | val = last; | |
9377 | if (TREE_CODE (val) == NOP_EXPR | |
9378 | && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))) | |
9379 | val = TREE_OPERAND (val, 0); | |
9380 | ||
9381 | *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val); | |
9382 | SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last)); | |
9383 | ||
9384 | { | |
9385 | tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE); | |
9386 | SET_EXPR_LOCATION (t, loc); | |
9387 | return t; | |
9388 | } | |
9389 | } | |
9390 | \f | |
9391 | /* Begin and end compound statements. This is as simple as pushing | |
9392 | and popping new statement lists from the tree. */ | |
9393 | ||
9394 | tree | |
9395 | c_begin_compound_stmt (bool do_scope) | |
9396 | { | |
9397 | tree stmt = push_stmt_list (); | |
9398 | if (do_scope) | |
9399 | push_scope (); | |
9400 | return stmt; | |
9401 | } | |
9402 | ||
9403 | /* End a compound statement. STMT is the statement. LOC is the | |
9404 | location of the compound statement-- this is usually the location | |
9405 | of the opening brace. */ | |
9406 | ||
9407 | tree | |
9408 | c_end_compound_stmt (location_t loc, tree stmt, bool do_scope) | |
9409 | { | |
9410 | tree block = NULL; | |
9411 | ||
9412 | if (do_scope) | |
9413 | { | |
9414 | if (c_dialect_objc ()) | |
9415 | objc_clear_super_receiver (); | |
9416 | block = pop_scope (); | |
9417 | } | |
9418 | ||
9419 | stmt = pop_stmt_list (stmt); | |
9420 | stmt = c_build_bind_expr (loc, block, stmt); | |
9421 | ||
9422 | /* If this compound statement is nested immediately inside a statement | |
9423 | expression, then force a BIND_EXPR to be created. Otherwise we'll | |
9424 | do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular, | |
9425 | STATEMENT_LISTs merge, and thus we can lose track of what statement | |
9426 | was really last. */ | |
9427 | if (building_stmt_list_p () | |
9428 | && STATEMENT_LIST_STMT_EXPR (cur_stmt_list) | |
9429 | && TREE_CODE (stmt) != BIND_EXPR) | |
9430 | { | |
9431 | stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL); | |
9432 | TREE_SIDE_EFFECTS (stmt) = 1; | |
9433 | SET_EXPR_LOCATION (stmt, loc); | |
9434 | } | |
9435 | ||
9436 | return stmt; | |
9437 | } | |
9438 | ||
9439 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
9440 | when the current scope is exited. EH_ONLY is true when this is not | |
9441 | meant to apply to normal control flow transfer. */ | |
9442 | ||
9443 | void | |
9444 | push_cleanup (tree decl, tree cleanup, bool eh_only) | |
9445 | { | |
9446 | enum tree_code code; | |
9447 | tree stmt, list; | |
9448 | bool stmt_expr; | |
9449 | ||
9450 | code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR; | |
9451 | stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup); | |
9452 | add_stmt (stmt); | |
9453 | stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list); | |
9454 | list = push_stmt_list (); | |
9455 | TREE_OPERAND (stmt, 0) = list; | |
9456 | STATEMENT_LIST_STMT_EXPR (list) = stmt_expr; | |
9457 | } | |
9458 | \f | |
9459 | /* Build a binary-operation expression without default conversions. | |
9460 | CODE is the kind of expression to build. | |
9461 | LOCATION is the operator's location. | |
9462 | This function differs from `build' in several ways: | |
9463 | the data type of the result is computed and recorded in it, | |
9464 | warnings are generated if arg data types are invalid, | |
9465 | special handling for addition and subtraction of pointers is known, | |
9466 | and some optimization is done (operations on narrow ints | |
9467 | are done in the narrower type when that gives the same result). | |
9468 | Constant folding is also done before the result is returned. | |
9469 | ||
9470 | Note that the operands will never have enumeral types, or function | |
9471 | or array types, because either they will have the default conversions | |
9472 | performed or they have both just been converted to some other type in which | |
9473 | the arithmetic is to be done. */ | |
9474 | ||
9475 | tree | |
9476 | build_binary_op (location_t location, enum tree_code code, | |
9477 | tree orig_op0, tree orig_op1, int convert_p) | |
9478 | { | |
9479 | tree type0, type1, orig_type0, orig_type1; | |
9480 | tree eptype; | |
9481 | enum tree_code code0, code1; | |
9482 | tree op0, op1; | |
9483 | tree ret = error_mark_node; | |
9484 | const char *invalid_op_diag; | |
9485 | bool op0_int_operands, op1_int_operands; | |
9486 | bool int_const, int_const_or_overflow, int_operands; | |
9487 | ||
9488 | /* Expression code to give to the expression when it is built. | |
9489 | Normally this is CODE, which is what the caller asked for, | |
9490 | but in some special cases we change it. */ | |
9491 | enum tree_code resultcode = code; | |
9492 | ||
9493 | /* Data type in which the computation is to be performed. | |
9494 | In the simplest cases this is the common type of the arguments. */ | |
9495 | tree result_type = NULL; | |
9496 | ||
9497 | /* When the computation is in excess precision, the type of the | |
9498 | final EXCESS_PRECISION_EXPR. */ | |
9499 | tree semantic_result_type = NULL; | |
9500 | ||
9501 | /* Nonzero means operands have already been type-converted | |
9502 | in whatever way is necessary. | |
9503 | Zero means they need to be converted to RESULT_TYPE. */ | |
9504 | int converted = 0; | |
9505 | ||
9506 | /* Nonzero means create the expression with this type, rather than | |
9507 | RESULT_TYPE. */ | |
9508 | tree build_type = 0; | |
9509 | ||
9510 | /* Nonzero means after finally constructing the expression | |
9511 | convert it to this type. */ | |
9512 | tree final_type = 0; | |
9513 | ||
9514 | /* Nonzero if this is an operation like MIN or MAX which can | |
9515 | safely be computed in short if both args are promoted shorts. | |
9516 | Also implies COMMON. | |
9517 | -1 indicates a bitwise operation; this makes a difference | |
9518 | in the exact conditions for when it is safe to do the operation | |
9519 | in a narrower mode. */ | |
9520 | int shorten = 0; | |
9521 | ||
9522 | /* Nonzero if this is a comparison operation; | |
9523 | if both args are promoted shorts, compare the original shorts. | |
9524 | Also implies COMMON. */ | |
9525 | int short_compare = 0; | |
9526 | ||
9527 | /* Nonzero if this is a right-shift operation, which can be computed on the | |
9528 | original short and then promoted if the operand is a promoted short. */ | |
9529 | int short_shift = 0; | |
9530 | ||
9531 | /* Nonzero means set RESULT_TYPE to the common type of the args. */ | |
9532 | int common = 0; | |
9533 | ||
9534 | /* True means types are compatible as far as ObjC is concerned. */ | |
9535 | bool objc_ok; | |
9536 | ||
9537 | /* True means this is an arithmetic operation that may need excess | |
9538 | precision. */ | |
9539 | bool may_need_excess_precision; | |
9540 | ||
9541 | /* True means this is a boolean operation that converts both its | |
9542 | operands to truth-values. */ | |
9543 | bool boolean_op = false; | |
9544 | ||
9545 | /* Remember whether we're doing / or %. */ | |
9546 | bool doing_div_or_mod = false; | |
9547 | ||
9548 | /* Remember whether we're doing << or >>. */ | |
9549 | bool doing_shift = false; | |
9550 | ||
9551 | /* Tree holding instrumentation expression. */ | |
9552 | tree instrument_expr = NULL; | |
9553 | ||
9554 | if (location == UNKNOWN_LOCATION) | |
9555 | location = input_location; | |
9556 | ||
9557 | op0 = orig_op0; | |
9558 | op1 = orig_op1; | |
9559 | ||
9560 | op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0); | |
9561 | if (op0_int_operands) | |
9562 | op0 = remove_c_maybe_const_expr (op0); | |
9563 | op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1); | |
9564 | if (op1_int_operands) | |
9565 | op1 = remove_c_maybe_const_expr (op1); | |
9566 | int_operands = (op0_int_operands && op1_int_operands); | |
9567 | if (int_operands) | |
9568 | { | |
9569 | int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST | |
9570 | && TREE_CODE (orig_op1) == INTEGER_CST); | |
9571 | int_const = (int_const_or_overflow | |
9572 | && !TREE_OVERFLOW (orig_op0) | |
9573 | && !TREE_OVERFLOW (orig_op1)); | |
9574 | } | |
9575 | else | |
9576 | int_const = int_const_or_overflow = false; | |
9577 | ||
9578 | /* Do not apply default conversion in mixed vector/scalar expression. */ | |
9579 | if (convert_p | |
9580 | && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE) | |
9581 | != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE))) | |
9582 | { | |
9583 | op0 = default_conversion (op0); | |
9584 | op1 = default_conversion (op1); | |
9585 | } | |
9586 | ||
9587 | /* When Cilk Plus is enabled and there are array notations inside op0, then | |
9588 | we check to see if there are builtin array notation functions. If | |
9589 | so, then we take on the type of the array notation inside it. */ | |
9590 | if (flag_enable_cilkplus && contains_array_notation_expr (op0)) | |
9591 | orig_type0 = type0 = find_correct_array_notation_type (op0); | |
9592 | else | |
9593 | orig_type0 = type0 = TREE_TYPE (op0); | |
9594 | ||
9595 | if (flag_enable_cilkplus && contains_array_notation_expr (op1)) | |
9596 | orig_type1 = type1 = find_correct_array_notation_type (op1); | |
9597 | else | |
9598 | orig_type1 = type1 = TREE_TYPE (op1); | |
9599 | ||
9600 | /* The expression codes of the data types of the arguments tell us | |
9601 | whether the arguments are integers, floating, pointers, etc. */ | |
9602 | code0 = TREE_CODE (type0); | |
9603 | code1 = TREE_CODE (type1); | |
9604 | ||
9605 | /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */ | |
9606 | STRIP_TYPE_NOPS (op0); | |
9607 | STRIP_TYPE_NOPS (op1); | |
9608 | ||
9609 | /* If an error was already reported for one of the arguments, | |
9610 | avoid reporting another error. */ | |
9611 | ||
9612 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
9613 | return error_mark_node; | |
9614 | ||
9615 | if ((invalid_op_diag | |
9616 | = targetm.invalid_binary_op (code, type0, type1))) | |
9617 | { | |
9618 | error_at (location, invalid_op_diag); | |
9619 | return error_mark_node; | |
9620 | } | |
9621 | ||
9622 | switch (code) | |
9623 | { | |
9624 | case PLUS_EXPR: | |
9625 | case MINUS_EXPR: | |
9626 | case MULT_EXPR: | |
9627 | case TRUNC_DIV_EXPR: | |
9628 | case CEIL_DIV_EXPR: | |
9629 | case FLOOR_DIV_EXPR: | |
9630 | case ROUND_DIV_EXPR: | |
9631 | case EXACT_DIV_EXPR: | |
9632 | may_need_excess_precision = true; | |
9633 | break; | |
9634 | default: | |
9635 | may_need_excess_precision = false; | |
9636 | break; | |
9637 | } | |
9638 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) | |
9639 | { | |
9640 | op0 = TREE_OPERAND (op0, 0); | |
9641 | type0 = TREE_TYPE (op0); | |
9642 | } | |
9643 | else if (may_need_excess_precision | |
9644 | && (eptype = excess_precision_type (type0)) != NULL_TREE) | |
9645 | { | |
9646 | type0 = eptype; | |
9647 | op0 = convert (eptype, op0); | |
9648 | } | |
9649 | if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR) | |
9650 | { | |
9651 | op1 = TREE_OPERAND (op1, 0); | |
9652 | type1 = TREE_TYPE (op1); | |
9653 | } | |
9654 | else if (may_need_excess_precision | |
9655 | && (eptype = excess_precision_type (type1)) != NULL_TREE) | |
9656 | { | |
9657 | type1 = eptype; | |
9658 | op1 = convert (eptype, op1); | |
9659 | } | |
9660 | ||
9661 | objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE); | |
9662 | ||
9663 | /* In case when one of the operands of the binary operation is | |
9664 | a vector and another is a scalar -- convert scalar to vector. */ | |
9665 | if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE)) | |
9666 | { | |
9667 | enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1, | |
9668 | true); | |
9669 | ||
9670 | switch (convert_flag) | |
9671 | { | |
9672 | case stv_error: | |
9673 | return error_mark_node; | |
9674 | case stv_firstarg: | |
9675 | { | |
9676 | bool maybe_const = true; | |
9677 | tree sc; | |
9678 | sc = c_fully_fold (op0, false, &maybe_const); | |
9679 | sc = save_expr (sc); | |
9680 | sc = convert (TREE_TYPE (type1), sc); | |
9681 | op0 = build_vector_from_val (type1, sc); | |
9682 | if (!maybe_const) | |
9683 | op0 = c_wrap_maybe_const (op0, true); | |
9684 | orig_type0 = type0 = TREE_TYPE (op0); | |
9685 | code0 = TREE_CODE (type0); | |
9686 | converted = 1; | |
9687 | break; | |
9688 | } | |
9689 | case stv_secondarg: | |
9690 | { | |
9691 | bool maybe_const = true; | |
9692 | tree sc; | |
9693 | sc = c_fully_fold (op1, false, &maybe_const); | |
9694 | sc = save_expr (sc); | |
9695 | sc = convert (TREE_TYPE (type0), sc); | |
9696 | op1 = build_vector_from_val (type0, sc); | |
9697 | if (!maybe_const) | |
9698 | op1 = c_wrap_maybe_const (op1, true); | |
9699 | orig_type1 = type1 = TREE_TYPE (op1); | |
9700 | code1 = TREE_CODE (type1); | |
9701 | converted = 1; | |
9702 | break; | |
9703 | } | |
9704 | default: | |
9705 | break; | |
9706 | } | |
9707 | } | |
9708 | ||
9709 | switch (code) | |
9710 | { | |
9711 | case PLUS_EXPR: | |
9712 | /* Handle the pointer + int case. */ | |
9713 | if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
9714 | { | |
9715 | ret = pointer_int_sum (location, PLUS_EXPR, op0, op1); | |
9716 | goto return_build_binary_op; | |
9717 | } | |
9718 | else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE) | |
9719 | { | |
9720 | ret = pointer_int_sum (location, PLUS_EXPR, op1, op0); | |
9721 | goto return_build_binary_op; | |
9722 | } | |
9723 | else | |
9724 | common = 1; | |
9725 | break; | |
9726 | ||
9727 | case MINUS_EXPR: | |
9728 | /* Subtraction of two similar pointers. | |
9729 | We must subtract them as integers, then divide by object size. */ | |
9730 | if (code0 == POINTER_TYPE && code1 == POINTER_TYPE | |
9731 | && comp_target_types (location, type0, type1)) | |
9732 | { | |
9733 | ret = pointer_diff (location, op0, op1); | |
9734 | goto return_build_binary_op; | |
9735 | } | |
9736 | /* Handle pointer minus int. Just like pointer plus int. */ | |
9737 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
9738 | { | |
9739 | ret = pointer_int_sum (location, MINUS_EXPR, op0, op1); | |
9740 | goto return_build_binary_op; | |
9741 | } | |
9742 | else | |
9743 | common = 1; | |
9744 | break; | |
9745 | ||
9746 | case MULT_EXPR: | |
9747 | common = 1; | |
9748 | break; | |
9749 | ||
9750 | case TRUNC_DIV_EXPR: | |
9751 | case CEIL_DIV_EXPR: | |
9752 | case FLOOR_DIV_EXPR: | |
9753 | case ROUND_DIV_EXPR: | |
9754 | case EXACT_DIV_EXPR: | |
9755 | doing_div_or_mod = true; | |
9756 | warn_for_div_by_zero (location, op1); | |
9757 | ||
9758 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
9759 | || code0 == FIXED_POINT_TYPE | |
9760 | || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) | |
9761 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
9762 | || code1 == FIXED_POINT_TYPE | |
9763 | || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)) | |
9764 | { | |
9765 | enum tree_code tcode0 = code0, tcode1 = code1; | |
9766 | ||
9767 | if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE) | |
9768 | tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0))); | |
9769 | if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE) | |
9770 | tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1))); | |
9771 | ||
9772 | if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE) | |
9773 | || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE))) | |
9774 | resultcode = RDIV_EXPR; | |
9775 | else | |
9776 | /* Although it would be tempting to shorten always here, that | |
9777 | loses on some targets, since the modulo instruction is | |
9778 | undefined if the quotient can't be represented in the | |
9779 | computation mode. We shorten only if unsigned or if | |
9780 | dividing by something we know != -1. */ | |
9781 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) | |
9782 | || (TREE_CODE (op1) == INTEGER_CST | |
9783 | && !integer_all_onesp (op1))); | |
9784 | common = 1; | |
9785 | } | |
9786 | break; | |
9787 | ||
9788 | case BIT_AND_EXPR: | |
9789 | case BIT_IOR_EXPR: | |
9790 | case BIT_XOR_EXPR: | |
9791 | if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
9792 | shorten = -1; | |
9793 | /* Allow vector types which are not floating point types. */ | |
9794 | else if (code0 == VECTOR_TYPE | |
9795 | && code1 == VECTOR_TYPE | |
9796 | && !VECTOR_FLOAT_TYPE_P (type0) | |
9797 | && !VECTOR_FLOAT_TYPE_P (type1)) | |
9798 | common = 1; | |
9799 | break; | |
9800 | ||
9801 | case TRUNC_MOD_EXPR: | |
9802 | case FLOOR_MOD_EXPR: | |
9803 | doing_div_or_mod = true; | |
9804 | warn_for_div_by_zero (location, op1); | |
9805 | ||
9806 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
9807 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9808 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
9809 | common = 1; | |
9810 | else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE) | |
9811 | { | |
9812 | /* Although it would be tempting to shorten always here, that loses | |
9813 | on some targets, since the modulo instruction is undefined if the | |
9814 | quotient can't be represented in the computation mode. We shorten | |
9815 | only if unsigned or if dividing by something we know != -1. */ | |
9816 | shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0)) | |
9817 | || (TREE_CODE (op1) == INTEGER_CST | |
9818 | && !integer_all_onesp (op1))); | |
9819 | common = 1; | |
9820 | } | |
9821 | break; | |
9822 | ||
9823 | case TRUTH_ANDIF_EXPR: | |
9824 | case TRUTH_ORIF_EXPR: | |
9825 | case TRUTH_AND_EXPR: | |
9826 | case TRUTH_OR_EXPR: | |
9827 | case TRUTH_XOR_EXPR: | |
9828 | if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE | |
9829 | || code0 == REAL_TYPE || code0 == COMPLEX_TYPE | |
9830 | || code0 == FIXED_POINT_TYPE) | |
9831 | && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE | |
9832 | || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
9833 | || code1 == FIXED_POINT_TYPE)) | |
9834 | { | |
9835 | /* Result of these operations is always an int, | |
9836 | but that does not mean the operands should be | |
9837 | converted to ints! */ | |
9838 | result_type = integer_type_node; | |
9839 | if (op0_int_operands) | |
9840 | { | |
9841 | op0 = c_objc_common_truthvalue_conversion (location, orig_op0); | |
9842 | op0 = remove_c_maybe_const_expr (op0); | |
9843 | } | |
9844 | else | |
9845 | op0 = c_objc_common_truthvalue_conversion (location, op0); | |
9846 | if (op1_int_operands) | |
9847 | { | |
9848 | op1 = c_objc_common_truthvalue_conversion (location, orig_op1); | |
9849 | op1 = remove_c_maybe_const_expr (op1); | |
9850 | } | |
9851 | else | |
9852 | op1 = c_objc_common_truthvalue_conversion (location, op1); | |
9853 | converted = 1; | |
9854 | boolean_op = true; | |
9855 | } | |
9856 | if (code == TRUTH_ANDIF_EXPR) | |
9857 | { | |
9858 | int_const_or_overflow = (int_operands | |
9859 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9860 | && (op0 == truthvalue_false_node | |
9861 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9862 | int_const = (int_const_or_overflow | |
9863 | && !TREE_OVERFLOW (orig_op0) | |
9864 | && (op0 == truthvalue_false_node | |
9865 | || !TREE_OVERFLOW (orig_op1))); | |
9866 | } | |
9867 | else if (code == TRUTH_ORIF_EXPR) | |
9868 | { | |
9869 | int_const_or_overflow = (int_operands | |
9870 | && TREE_CODE (orig_op0) == INTEGER_CST | |
9871 | && (op0 == truthvalue_true_node | |
9872 | || TREE_CODE (orig_op1) == INTEGER_CST)); | |
9873 | int_const = (int_const_or_overflow | |
9874 | && !TREE_OVERFLOW (orig_op0) | |
9875 | && (op0 == truthvalue_true_node | |
9876 | || !TREE_OVERFLOW (orig_op1))); | |
9877 | } | |
9878 | break; | |
9879 | ||
9880 | /* Shift operations: result has same type as first operand; | |
9881 | always convert second operand to int. | |
9882 | Also set SHORT_SHIFT if shifting rightward. */ | |
9883 | ||
9884 | case RSHIFT_EXPR: | |
9885 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE | |
9886 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
9887 | { | |
9888 | result_type = type0; | |
9889 | converted = 1; | |
9890 | } | |
9891 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
9892 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9893 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
9894 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
9895 | { | |
9896 | result_type = type0; | |
9897 | converted = 1; | |
9898 | } | |
9899 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
9900 | && code1 == INTEGER_TYPE) | |
9901 | { | |
9902 | doing_shift = true; | |
9903 | if (TREE_CODE (op1) == INTEGER_CST) | |
9904 | { | |
9905 | if (tree_int_cst_sgn (op1) < 0) | |
9906 | { | |
9907 | int_const = false; | |
9908 | if (c_inhibit_evaluation_warnings == 0) | |
9909 | warning (0, "right shift count is negative"); | |
9910 | } | |
9911 | else | |
9912 | { | |
9913 | if (!integer_zerop (op1)) | |
9914 | short_shift = 1; | |
9915 | ||
9916 | if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
9917 | { | |
9918 | int_const = false; | |
9919 | if (c_inhibit_evaluation_warnings == 0) | |
9920 | warning (0, "right shift count >= width of type"); | |
9921 | } | |
9922 | } | |
9923 | } | |
9924 | ||
9925 | /* Use the type of the value to be shifted. */ | |
9926 | result_type = type0; | |
9927 | /* Convert the non vector shift-count to an integer, regardless | |
9928 | of size of value being shifted. */ | |
9929 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
9930 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
9931 | op1 = convert (integer_type_node, op1); | |
9932 | /* Avoid converting op1 to result_type later. */ | |
9933 | converted = 1; | |
9934 | } | |
9935 | break; | |
9936 | ||
9937 | case LSHIFT_EXPR: | |
9938 | if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE | |
9939 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE) | |
9940 | { | |
9941 | result_type = type0; | |
9942 | converted = 1; | |
9943 | } | |
9944 | else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
9945 | && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE | |
9946 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE | |
9947 | && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1)) | |
9948 | { | |
9949 | result_type = type0; | |
9950 | converted = 1; | |
9951 | } | |
9952 | else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE) | |
9953 | && code1 == INTEGER_TYPE) | |
9954 | { | |
9955 | doing_shift = true; | |
9956 | if (TREE_CODE (op1) == INTEGER_CST) | |
9957 | { | |
9958 | if (tree_int_cst_sgn (op1) < 0) | |
9959 | { | |
9960 | int_const = false; | |
9961 | if (c_inhibit_evaluation_warnings == 0) | |
9962 | warning (0, "left shift count is negative"); | |
9963 | } | |
9964 | ||
9965 | else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0) | |
9966 | { | |
9967 | int_const = false; | |
9968 | if (c_inhibit_evaluation_warnings == 0) | |
9969 | warning (0, "left shift count >= width of type"); | |
9970 | } | |
9971 | } | |
9972 | ||
9973 | /* Use the type of the value to be shifted. */ | |
9974 | result_type = type0; | |
9975 | /* Convert the non vector shift-count to an integer, regardless | |
9976 | of size of value being shifted. */ | |
9977 | if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE | |
9978 | && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node) | |
9979 | op1 = convert (integer_type_node, op1); | |
9980 | /* Avoid converting op1 to result_type later. */ | |
9981 | converted = 1; | |
9982 | } | |
9983 | break; | |
9984 | ||
9985 | case EQ_EXPR: | |
9986 | case NE_EXPR: | |
9987 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) | |
9988 | { | |
9989 | tree intt; | |
9990 | if (TREE_TYPE (type0) != TREE_TYPE (type1)) | |
9991 | { | |
9992 | error_at (location, "comparing vectors with different " | |
9993 | "element types"); | |
9994 | return error_mark_node; | |
9995 | } | |
9996 | ||
9997 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
9998 | { | |
9999 | error_at (location, "comparing vectors with different " | |
10000 | "number of elements"); | |
10001 | return error_mark_node; | |
10002 | } | |
10003 | ||
10004 | /* Always construct signed integer vector type. */ | |
10005 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10006 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10007 | result_type = build_opaque_vector_type (intt, | |
10008 | TYPE_VECTOR_SUBPARTS (type0)); | |
10009 | converted = 1; | |
10010 | break; | |
10011 | } | |
10012 | if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)) | |
10013 | warning_at (location, | |
10014 | OPT_Wfloat_equal, | |
10015 | "comparing floating point with == or != is unsafe"); | |
10016 | /* Result of comparison is always int, | |
10017 | but don't convert the args to int! */ | |
10018 | build_type = integer_type_node; | |
10019 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
10020 | || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE) | |
10021 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
10022 | || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) | |
10023 | short_compare = 1; | |
10024 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) | |
10025 | { | |
10026 | if (TREE_CODE (op0) == ADDR_EXPR | |
10027 | && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) | |
10028 | { | |
10029 | if (code == EQ_EXPR) | |
10030 | warning_at (location, | |
10031 | OPT_Waddress, | |
10032 | "the comparison will always evaluate as %<false%> " | |
10033 | "for the address of %qD will never be NULL", | |
10034 | TREE_OPERAND (op0, 0)); | |
10035 | else | |
10036 | warning_at (location, | |
10037 | OPT_Waddress, | |
10038 | "the comparison will always evaluate as %<true%> " | |
10039 | "for the address of %qD will never be NULL", | |
10040 | TREE_OPERAND (op0, 0)); | |
10041 | } | |
10042 | result_type = type0; | |
10043 | } | |
10044 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) | |
10045 | { | |
10046 | if (TREE_CODE (op1) == ADDR_EXPR | |
10047 | && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) | |
10048 | { | |
10049 | if (code == EQ_EXPR) | |
10050 | warning_at (location, | |
10051 | OPT_Waddress, | |
10052 | "the comparison will always evaluate as %<false%> " | |
10053 | "for the address of %qD will never be NULL", | |
10054 | TREE_OPERAND (op1, 0)); | |
10055 | else | |
10056 | warning_at (location, | |
10057 | OPT_Waddress, | |
10058 | "the comparison will always evaluate as %<true%> " | |
10059 | "for the address of %qD will never be NULL", | |
10060 | TREE_OPERAND (op1, 0)); | |
10061 | } | |
10062 | result_type = type1; | |
10063 | } | |
10064 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
10065 | { | |
10066 | tree tt0 = TREE_TYPE (type0); | |
10067 | tree tt1 = TREE_TYPE (type1); | |
10068 | addr_space_t as0 = TYPE_ADDR_SPACE (tt0); | |
10069 | addr_space_t as1 = TYPE_ADDR_SPACE (tt1); | |
10070 | addr_space_t as_common = ADDR_SPACE_GENERIC; | |
10071 | ||
10072 | /* Anything compares with void *. void * compares with anything. | |
10073 | Otherwise, the targets must be compatible | |
10074 | and both must be object or both incomplete. */ | |
10075 | if (comp_target_types (location, type0, type1)) | |
10076 | result_type = common_pointer_type (type0, type1); | |
10077 | else if (!addr_space_superset (as0, as1, &as_common)) | |
10078 | { | |
10079 | error_at (location, "comparison of pointers to " | |
10080 | "disjoint address spaces"); | |
10081 | return error_mark_node; | |
10082 | } | |
10083 | else if (VOID_TYPE_P (tt0)) | |
10084 | { | |
10085 | if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE) | |
10086 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " | |
10087 | "comparison of %<void *%> with function pointer"); | |
10088 | } | |
10089 | else if (VOID_TYPE_P (tt1)) | |
10090 | { | |
10091 | if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE) | |
10092 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " | |
10093 | "comparison of %<void *%> with function pointer"); | |
10094 | } | |
10095 | else | |
10096 | /* Avoid warning about the volatile ObjC EH puts on decls. */ | |
10097 | if (!objc_ok) | |
10098 | pedwarn (location, 0, | |
10099 | "comparison of distinct pointer types lacks a cast"); | |
10100 | ||
10101 | if (result_type == NULL_TREE) | |
10102 | { | |
10103 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
10104 | result_type = build_pointer_type | |
10105 | (build_qualified_type (void_type_node, qual)); | |
10106 | } | |
10107 | } | |
10108 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
10109 | { | |
10110 | result_type = type0; | |
10111 | pedwarn (location, 0, "comparison between pointer and integer"); | |
10112 | } | |
10113 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
10114 | { | |
10115 | result_type = type1; | |
10116 | pedwarn (location, 0, "comparison between pointer and integer"); | |
10117 | } | |
10118 | break; | |
10119 | ||
10120 | case LE_EXPR: | |
10121 | case GE_EXPR: | |
10122 | case LT_EXPR: | |
10123 | case GT_EXPR: | |
10124 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE) | |
10125 | { | |
10126 | tree intt; | |
10127 | if (TREE_TYPE (type0) != TREE_TYPE (type1)) | |
10128 | { | |
10129 | error_at (location, "comparing vectors with different " | |
10130 | "element types"); | |
10131 | return error_mark_node; | |
10132 | } | |
10133 | ||
10134 | if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1)) | |
10135 | { | |
10136 | error_at (location, "comparing vectors with different " | |
10137 | "number of elements"); | |
10138 | return error_mark_node; | |
10139 | } | |
10140 | ||
10141 | /* Always construct signed integer vector type. */ | |
10142 | intt = c_common_type_for_size (GET_MODE_BITSIZE | |
10143 | (TYPE_MODE (TREE_TYPE (type0))), 0); | |
10144 | result_type = build_opaque_vector_type (intt, | |
10145 | TYPE_VECTOR_SUBPARTS (type0)); | |
10146 | converted = 1; | |
10147 | break; | |
10148 | } | |
10149 | build_type = integer_type_node; | |
10150 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE | |
10151 | || code0 == FIXED_POINT_TYPE) | |
10152 | && (code1 == INTEGER_TYPE || code1 == REAL_TYPE | |
10153 | || code1 == FIXED_POINT_TYPE)) | |
10154 | short_compare = 1; | |
10155 | else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) | |
10156 | { | |
10157 | addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0)); | |
10158 | addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1)); | |
10159 | addr_space_t as_common; | |
10160 | ||
10161 | if (comp_target_types (location, type0, type1)) | |
10162 | { | |
10163 | result_type = common_pointer_type (type0, type1); | |
10164 | if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) | |
10165 | != !COMPLETE_TYPE_P (TREE_TYPE (type1))) | |
10166 | pedwarn (location, 0, | |
10167 | "comparison of complete and incomplete pointers"); | |
10168 | else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) | |
10169 | pedwarn (location, OPT_Wpedantic, "ISO C forbids " | |
10170 | "ordered comparisons of pointers to functions"); | |
10171 | else if (null_pointer_constant_p (orig_op0) | |
10172 | || null_pointer_constant_p (orig_op1)) | |
10173 | warning_at (location, OPT_Wextra, | |
10174 | "ordered comparison of pointer with null pointer"); | |
10175 | ||
10176 | } | |
10177 | else if (!addr_space_superset (as0, as1, &as_common)) | |
10178 | { | |
10179 | error_at (location, "comparison of pointers to " | |
10180 | "disjoint address spaces"); | |
10181 | return error_mark_node; | |
10182 | } | |
10183 | else | |
10184 | { | |
10185 | int qual = ENCODE_QUAL_ADDR_SPACE (as_common); | |
10186 | result_type = build_pointer_type | |
10187 | (build_qualified_type (void_type_node, qual)); | |
10188 | pedwarn (location, 0, | |
10189 | "comparison of distinct pointer types lacks a cast"); | |
10190 | } | |
10191 | } | |
10192 | else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) | |
10193 | { | |
10194 | result_type = type0; | |
10195 | if (pedantic) | |
10196 | pedwarn (location, OPT_Wpedantic, | |
10197 | "ordered comparison of pointer with integer zero"); | |
10198 | else if (extra_warnings) | |
10199 | warning_at (location, OPT_Wextra, | |
10200 | "ordered comparison of pointer with integer zero"); | |
10201 | } | |
10202 | else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) | |
10203 | { | |
10204 | result_type = type1; | |
10205 | if (pedantic) | |
10206 | pedwarn (location, OPT_Wpedantic, | |
10207 | "ordered comparison of pointer with integer zero"); | |
10208 | else if (extra_warnings) | |
10209 | warning_at (location, OPT_Wextra, | |
10210 | "ordered comparison of pointer with integer zero"); | |
10211 | } | |
10212 | else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) | |
10213 | { | |
10214 | result_type = type0; | |
10215 | pedwarn (location, 0, "comparison between pointer and integer"); | |
10216 | } | |
10217 | else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE) | |
10218 | { | |
10219 | result_type = type1; | |
10220 | pedwarn (location, 0, "comparison between pointer and integer"); | |
10221 | } | |
10222 | break; | |
10223 | ||
10224 | default: | |
10225 | gcc_unreachable (); | |
10226 | } | |
10227 | ||
10228 | if (code0 == ERROR_MARK || code1 == ERROR_MARK) | |
10229 | return error_mark_node; | |
10230 | ||
10231 | if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE | |
10232 | && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1)) | |
10233 | || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0), | |
10234 | TREE_TYPE (type1)))) | |
10235 | { | |
10236 | binary_op_error (location, code, type0, type1); | |
10237 | return error_mark_node; | |
10238 | } | |
10239 | ||
10240 | if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE | |
10241 | || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE) | |
10242 | && | |
10243 | (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE | |
10244 | || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE)) | |
10245 | { | |
10246 | bool first_complex = (code0 == COMPLEX_TYPE); | |
10247 | bool second_complex = (code1 == COMPLEX_TYPE); | |
10248 | int none_complex = (!first_complex && !second_complex); | |
10249 | ||
10250 | if (shorten || common || short_compare) | |
10251 | { | |
10252 | result_type = c_common_type (type0, type1); | |
10253 | do_warn_double_promotion (result_type, type0, type1, | |
10254 | "implicit conversion from %qT to %qT " | |
10255 | "to match other operand of binary " | |
10256 | "expression", | |
10257 | location); | |
10258 | if (result_type == error_mark_node) | |
10259 | return error_mark_node; | |
10260 | } | |
10261 | ||
10262 | if (first_complex != second_complex | |
10263 | && (code == PLUS_EXPR | |
10264 | || code == MINUS_EXPR | |
10265 | || code == MULT_EXPR | |
10266 | || (code == TRUNC_DIV_EXPR && first_complex)) | |
10267 | && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE | |
10268 | && flag_signed_zeros) | |
10269 | { | |
10270 | /* An operation on mixed real/complex operands must be | |
10271 | handled specially, but the language-independent code can | |
10272 | more easily optimize the plain complex arithmetic if | |
10273 | -fno-signed-zeros. */ | |
10274 | tree real_type = TREE_TYPE (result_type); | |
10275 | tree real, imag; | |
10276 | if (type0 != orig_type0 || type1 != orig_type1) | |
10277 | { | |
10278 | gcc_assert (may_need_excess_precision && common); | |
10279 | semantic_result_type = c_common_type (orig_type0, orig_type1); | |
10280 | } | |
10281 | if (first_complex) | |
10282 | { | |
10283 | if (TREE_TYPE (op0) != result_type) | |
10284 | op0 = convert_and_check (result_type, op0); | |
10285 | if (TREE_TYPE (op1) != real_type) | |
10286 | op1 = convert_and_check (real_type, op1); | |
10287 | } | |
10288 | else | |
10289 | { | |
10290 | if (TREE_TYPE (op0) != real_type) | |
10291 | op0 = convert_and_check (real_type, op0); | |
10292 | if (TREE_TYPE (op1) != result_type) | |
10293 | op1 = convert_and_check (result_type, op1); | |
10294 | } | |
10295 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10296 | return error_mark_node; | |
10297 | if (first_complex) | |
10298 | { | |
10299 | op0 = c_save_expr (op0); | |
10300 | real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR, | |
10301 | op0, 1); | |
10302 | imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR, | |
10303 | op0, 1); | |
10304 | switch (code) | |
10305 | { | |
10306 | case MULT_EXPR: | |
10307 | case TRUNC_DIV_EXPR: | |
10308 | op1 = c_save_expr (op1); | |
10309 | imag = build2 (resultcode, real_type, imag, op1); | |
10310 | /* Fall through. */ | |
10311 | case PLUS_EXPR: | |
10312 | case MINUS_EXPR: | |
10313 | real = build2 (resultcode, real_type, real, op1); | |
10314 | break; | |
10315 | default: | |
10316 | gcc_unreachable(); | |
10317 | } | |
10318 | } | |
10319 | else | |
10320 | { | |
10321 | op1 = c_save_expr (op1); | |
10322 | real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR, | |
10323 | op1, 1); | |
10324 | imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR, | |
10325 | op1, 1); | |
10326 | switch (code) | |
10327 | { | |
10328 | case MULT_EXPR: | |
10329 | op0 = c_save_expr (op0); | |
10330 | imag = build2 (resultcode, real_type, op0, imag); | |
10331 | /* Fall through. */ | |
10332 | case PLUS_EXPR: | |
10333 | real = build2 (resultcode, real_type, op0, real); | |
10334 | break; | |
10335 | case MINUS_EXPR: | |
10336 | real = build2 (resultcode, real_type, op0, real); | |
10337 | imag = build1 (NEGATE_EXPR, real_type, imag); | |
10338 | break; | |
10339 | default: | |
10340 | gcc_unreachable(); | |
10341 | } | |
10342 | } | |
10343 | ret = build2 (COMPLEX_EXPR, result_type, real, imag); | |
10344 | goto return_build_binary_op; | |
10345 | } | |
10346 | ||
10347 | /* For certain operations (which identify themselves by shorten != 0) | |
10348 | if both args were extended from the same smaller type, | |
10349 | do the arithmetic in that type and then extend. | |
10350 | ||
10351 | shorten !=0 and !=1 indicates a bitwise operation. | |
10352 | For them, this optimization is safe only if | |
10353 | both args are zero-extended or both are sign-extended. | |
10354 | Otherwise, we might change the result. | |
10355 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
10356 | but calculated in (unsigned short) it would be (unsigned short)-1. */ | |
10357 | ||
10358 | if (shorten && none_complex) | |
10359 | { | |
10360 | final_type = result_type; | |
10361 | result_type = shorten_binary_op (result_type, op0, op1, | |
10362 | shorten == -1); | |
10363 | } | |
10364 | ||
10365 | /* Shifts can be shortened if shifting right. */ | |
10366 | ||
10367 | if (short_shift) | |
10368 | { | |
10369 | int unsigned_arg; | |
10370 | tree arg0 = get_narrower (op0, &unsigned_arg); | |
10371 | ||
10372 | final_type = result_type; | |
10373 | ||
10374 | if (arg0 == op0 && final_type == TREE_TYPE (op0)) | |
10375 | unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0)); | |
10376 | ||
10377 | if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type) | |
10378 | && tree_int_cst_sgn (op1) > 0 | |
10379 | /* We can shorten only if the shift count is less than the | |
10380 | number of bits in the smaller type size. */ | |
10381 | && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0 | |
10382 | /* We cannot drop an unsigned shift after sign-extension. */ | |
10383 | && (!TYPE_UNSIGNED (final_type) || unsigned_arg)) | |
10384 | { | |
10385 | /* Do an unsigned shift if the operand was zero-extended. */ | |
10386 | result_type | |
10387 | = c_common_signed_or_unsigned_type (unsigned_arg, | |
10388 | TREE_TYPE (arg0)); | |
10389 | /* Convert value-to-be-shifted to that type. */ | |
10390 | if (TREE_TYPE (op0) != result_type) | |
10391 | op0 = convert (result_type, op0); | |
10392 | converted = 1; | |
10393 | } | |
10394 | } | |
10395 | ||
10396 | /* Comparison operations are shortened too but differently. | |
10397 | They identify themselves by setting short_compare = 1. */ | |
10398 | ||
10399 | if (short_compare) | |
10400 | { | |
10401 | /* Don't write &op0, etc., because that would prevent op0 | |
10402 | from being kept in a register. | |
10403 | Instead, make copies of the our local variables and | |
10404 | pass the copies by reference, then copy them back afterward. */ | |
10405 | tree xop0 = op0, xop1 = op1, xresult_type = result_type; | |
10406 | enum tree_code xresultcode = resultcode; | |
10407 | tree val | |
10408 | = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode); | |
10409 | ||
10410 | if (val != 0) | |
10411 | { | |
10412 | ret = val; | |
10413 | goto return_build_binary_op; | |
10414 | } | |
10415 | ||
10416 | op0 = xop0, op1 = xop1; | |
10417 | converted = 1; | |
10418 | resultcode = xresultcode; | |
10419 | ||
10420 | if (c_inhibit_evaluation_warnings == 0) | |
10421 | { | |
10422 | bool op0_maybe_const = true; | |
10423 | bool op1_maybe_const = true; | |
10424 | tree orig_op0_folded, orig_op1_folded; | |
10425 | ||
10426 | if (in_late_binary_op) | |
10427 | { | |
10428 | orig_op0_folded = orig_op0; | |
10429 | orig_op1_folded = orig_op1; | |
10430 | } | |
10431 | else | |
10432 | { | |
10433 | /* Fold for the sake of possible warnings, as in | |
10434 | build_conditional_expr. This requires the | |
10435 | "original" values to be folded, not just op0 and | |
10436 | op1. */ | |
10437 | c_inhibit_evaluation_warnings++; | |
10438 | op0 = c_fully_fold (op0, require_constant_value, | |
10439 | &op0_maybe_const); | |
10440 | op1 = c_fully_fold (op1, require_constant_value, | |
10441 | &op1_maybe_const); | |
10442 | c_inhibit_evaluation_warnings--; | |
10443 | orig_op0_folded = c_fully_fold (orig_op0, | |
10444 | require_constant_value, | |
10445 | NULL); | |
10446 | orig_op1_folded = c_fully_fold (orig_op1, | |
10447 | require_constant_value, | |
10448 | NULL); | |
10449 | } | |
10450 | ||
10451 | if (warn_sign_compare) | |
10452 | warn_for_sign_compare (location, orig_op0_folded, | |
10453 | orig_op1_folded, op0, op1, | |
10454 | result_type, resultcode); | |
10455 | if (!in_late_binary_op && !int_operands) | |
10456 | { | |
10457 | if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST) | |
10458 | op0 = c_wrap_maybe_const (op0, !op0_maybe_const); | |
10459 | if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST) | |
10460 | op1 = c_wrap_maybe_const (op1, !op1_maybe_const); | |
10461 | } | |
10462 | } | |
10463 | } | |
10464 | } | |
10465 | ||
10466 | /* At this point, RESULT_TYPE must be nonzero to avoid an error message. | |
10467 | If CONVERTED is zero, both args will be converted to type RESULT_TYPE. | |
10468 | Then the expression will be built. | |
10469 | It will be given type FINAL_TYPE if that is nonzero; | |
10470 | otherwise, it will be given type RESULT_TYPE. */ | |
10471 | ||
10472 | if (!result_type) | |
10473 | { | |
10474 | binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1)); | |
10475 | return error_mark_node; | |
10476 | } | |
10477 | ||
10478 | if (build_type == NULL_TREE) | |
10479 | { | |
10480 | build_type = result_type; | |
10481 | if ((type0 != orig_type0 || type1 != orig_type1) | |
10482 | && !boolean_op) | |
10483 | { | |
10484 | gcc_assert (may_need_excess_precision && common); | |
10485 | semantic_result_type = c_common_type (orig_type0, orig_type1); | |
10486 | } | |
10487 | } | |
10488 | ||
10489 | if (!converted) | |
10490 | { | |
10491 | op0 = ep_convert_and_check (result_type, op0, semantic_result_type); | |
10492 | op1 = ep_convert_and_check (result_type, op1, semantic_result_type); | |
10493 | ||
10494 | /* This can happen if one operand has a vector type, and the other | |
10495 | has a different type. */ | |
10496 | if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK) | |
10497 | return error_mark_node; | |
10498 | } | |
10499 | ||
10500 | if (flag_sanitize & SANITIZE_UNDEFINED | |
10501 | && current_function_decl != 0 | |
10502 | && (doing_div_or_mod || doing_shift)) | |
10503 | { | |
10504 | /* OP0 and/or OP1 might have side-effects. */ | |
10505 | op0 = c_save_expr (op0); | |
10506 | op1 = c_save_expr (op1); | |
10507 | op0 = c_fully_fold (op0, false, NULL); | |
10508 | op1 = c_fully_fold (op1, false, NULL); | |
10509 | if (doing_div_or_mod) | |
10510 | instrument_expr = ubsan_instrument_division (location, op0, op1); | |
10511 | else if (doing_shift) | |
10512 | instrument_expr = ubsan_instrument_shift (location, code, op0, op1); | |
10513 | } | |
10514 | ||
10515 | /* Treat expressions in initializers specially as they can't trap. */ | |
10516 | if (int_const_or_overflow) | |
10517 | ret = (require_constant_value | |
10518 | ? fold_build2_initializer_loc (location, resultcode, build_type, | |
10519 | op0, op1) | |
10520 | : fold_build2_loc (location, resultcode, build_type, op0, op1)); | |
10521 | else | |
10522 | ret = build2 (resultcode, build_type, op0, op1); | |
10523 | if (final_type != 0) | |
10524 | ret = convert (final_type, ret); | |
10525 | ||
10526 | return_build_binary_op: | |
10527 | gcc_assert (ret != error_mark_node); | |
10528 | if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const) | |
10529 | ret = (int_operands | |
10530 | ? note_integer_operands (ret) | |
10531 | : build1 (NOP_EXPR, TREE_TYPE (ret), ret)); | |
10532 | else if (TREE_CODE (ret) != INTEGER_CST && int_operands | |
10533 | && !in_late_binary_op) | |
10534 | ret = note_integer_operands (ret); | |
10535 | if (semantic_result_type) | |
10536 | ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret); | |
10537 | protected_set_expr_location (ret, location); | |
10538 | ||
10539 | if ((flag_sanitize & SANITIZE_UNDEFINED) && instrument_expr != NULL) | |
10540 | ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret), | |
10541 | instrument_expr, ret); | |
10542 | ||
10543 | return ret; | |
10544 | } | |
10545 | ||
10546 | ||
10547 | /* Convert EXPR to be a truth-value, validating its type for this | |
10548 | purpose. LOCATION is the source location for the expression. */ | |
10549 | ||
10550 | tree | |
10551 | c_objc_common_truthvalue_conversion (location_t location, tree expr) | |
10552 | { | |
10553 | bool int_const, int_operands; | |
10554 | ||
10555 | switch (TREE_CODE (TREE_TYPE (expr))) | |
10556 | { | |
10557 | case ARRAY_TYPE: | |
10558 | error_at (location, "used array that cannot be converted to pointer where scalar is required"); | |
10559 | return error_mark_node; | |
10560 | ||
10561 | case RECORD_TYPE: | |
10562 | error_at (location, "used struct type value where scalar is required"); | |
10563 | return error_mark_node; | |
10564 | ||
10565 | case UNION_TYPE: | |
10566 | error_at (location, "used union type value where scalar is required"); | |
10567 | return error_mark_node; | |
10568 | ||
10569 | case VOID_TYPE: | |
10570 | error_at (location, "void value not ignored as it ought to be"); | |
10571 | return error_mark_node; | |
10572 | ||
10573 | case FUNCTION_TYPE: | |
10574 | gcc_unreachable (); | |
10575 | ||
10576 | case VECTOR_TYPE: | |
10577 | error_at (location, "used vector type where scalar is required"); | |
10578 | return error_mark_node; | |
10579 | ||
10580 | default: | |
10581 | break; | |
10582 | } | |
10583 | ||
10584 | int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); | |
10585 | int_operands = EXPR_INT_CONST_OPERANDS (expr); | |
10586 | if (int_operands && TREE_CODE (expr) != INTEGER_CST) | |
10587 | { | |
10588 | expr = remove_c_maybe_const_expr (expr); | |
10589 | expr = build2 (NE_EXPR, integer_type_node, expr, | |
10590 | convert (TREE_TYPE (expr), integer_zero_node)); | |
10591 | expr = note_integer_operands (expr); | |
10592 | } | |
10593 | else | |
10594 | /* ??? Should we also give an error for vectors rather than leaving | |
10595 | those to give errors later? */ | |
10596 | expr = c_common_truthvalue_conversion (location, expr); | |
10597 | ||
10598 | if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) | |
10599 | { | |
10600 | if (TREE_OVERFLOW (expr)) | |
10601 | return expr; | |
10602 | else | |
10603 | return note_integer_operands (expr); | |
10604 | } | |
10605 | if (TREE_CODE (expr) == INTEGER_CST && !int_const) | |
10606 | return build1 (NOP_EXPR, TREE_TYPE (expr), expr); | |
10607 | return expr; | |
10608 | } | |
10609 | \f | |
10610 | ||
10611 | /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as | |
10612 | required. */ | |
10613 | ||
10614 | tree | |
10615 | c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se) | |
10616 | { | |
10617 | if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR) | |
10618 | { | |
10619 | tree decl = COMPOUND_LITERAL_EXPR_DECL (expr); | |
10620 | /* Executing a compound literal inside a function reinitializes | |
10621 | it. */ | |
10622 | if (!TREE_STATIC (decl)) | |
10623 | *se = true; | |
10624 | return decl; | |
10625 | } | |
10626 | else | |
10627 | return expr; | |
10628 | } | |
10629 | \f | |
10630 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ | |
10631 | ||
10632 | tree | |
10633 | c_begin_omp_parallel (void) | |
10634 | { | |
10635 | tree block; | |
10636 | ||
10637 | keep_next_level (); | |
10638 | block = c_begin_compound_stmt (true); | |
10639 | ||
10640 | return block; | |
10641 | } | |
10642 | ||
10643 | /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound | |
10644 | statement. LOC is the location of the OMP_PARALLEL. */ | |
10645 | ||
10646 | tree | |
10647 | c_finish_omp_parallel (location_t loc, tree clauses, tree block) | |
10648 | { | |
10649 | tree stmt; | |
10650 | ||
10651 | block = c_end_compound_stmt (loc, block, true); | |
10652 | ||
10653 | stmt = make_node (OMP_PARALLEL); | |
10654 | TREE_TYPE (stmt) = void_type_node; | |
10655 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
10656 | OMP_PARALLEL_BODY (stmt) = block; | |
10657 | SET_EXPR_LOCATION (stmt, loc); | |
10658 | ||
10659 | return add_stmt (stmt); | |
10660 | } | |
10661 | ||
10662 | /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */ | |
10663 | ||
10664 | tree | |
10665 | c_begin_omp_task (void) | |
10666 | { | |
10667 | tree block; | |
10668 | ||
10669 | keep_next_level (); | |
10670 | block = c_begin_compound_stmt (true); | |
10671 | ||
10672 | return block; | |
10673 | } | |
10674 | ||
10675 | /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound | |
10676 | statement. LOC is the location of the #pragma. */ | |
10677 | ||
10678 | tree | |
10679 | c_finish_omp_task (location_t loc, tree clauses, tree block) | |
10680 | { | |
10681 | tree stmt; | |
10682 | ||
10683 | block = c_end_compound_stmt (loc, block, true); | |
10684 | ||
10685 | stmt = make_node (OMP_TASK); | |
10686 | TREE_TYPE (stmt) = void_type_node; | |
10687 | OMP_TASK_CLAUSES (stmt) = clauses; | |
10688 | OMP_TASK_BODY (stmt) = block; | |
10689 | SET_EXPR_LOCATION (stmt, loc); | |
10690 | ||
10691 | return add_stmt (stmt); | |
10692 | } | |
10693 | ||
10694 | /* For all elements of CLAUSES, validate them vs OpenMP constraints. | |
10695 | Remove any elements from the list that are invalid. */ | |
10696 | ||
10697 | tree | |
10698 | c_finish_omp_clauses (tree clauses) | |
10699 | { | |
10700 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
10701 | tree c, t, *pc = &clauses; | |
10702 | const char *name; | |
10703 | ||
10704 | bitmap_obstack_initialize (NULL); | |
10705 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
10706 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
10707 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
10708 | ||
10709 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
10710 | { | |
10711 | bool remove = false; | |
10712 | bool need_complete = false; | |
10713 | bool need_implicitly_determined = false; | |
10714 | ||
10715 | switch (OMP_CLAUSE_CODE (c)) | |
10716 | { | |
10717 | case OMP_CLAUSE_SHARED: | |
10718 | name = "shared"; | |
10719 | need_implicitly_determined = true; | |
10720 | goto check_dup_generic; | |
10721 | ||
10722 | case OMP_CLAUSE_PRIVATE: | |
10723 | name = "private"; | |
10724 | need_complete = true; | |
10725 | need_implicitly_determined = true; | |
10726 | goto check_dup_generic; | |
10727 | ||
10728 | case OMP_CLAUSE_REDUCTION: | |
10729 | name = "reduction"; | |
10730 | need_implicitly_determined = true; | |
10731 | t = OMP_CLAUSE_DECL (c); | |
10732 | if (AGGREGATE_TYPE_P (TREE_TYPE (t)) | |
10733 | || POINTER_TYPE_P (TREE_TYPE (t))) | |
10734 | { | |
10735 | error_at (OMP_CLAUSE_LOCATION (c), | |
10736 | "%qE has invalid type for %<reduction%>", t); | |
10737 | remove = true; | |
10738 | } | |
10739 | else if (FLOAT_TYPE_P (TREE_TYPE (t))) | |
10740 | { | |
10741 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
10742 | const char *r_name = NULL; | |
10743 | ||
10744 | switch (r_code) | |
10745 | { | |
10746 | case PLUS_EXPR: | |
10747 | case MULT_EXPR: | |
10748 | case MINUS_EXPR: | |
10749 | case MIN_EXPR: | |
10750 | case MAX_EXPR: | |
10751 | break; | |
10752 | case BIT_AND_EXPR: | |
10753 | r_name = "&"; | |
10754 | break; | |
10755 | case BIT_XOR_EXPR: | |
10756 | r_name = "^"; | |
10757 | break; | |
10758 | case BIT_IOR_EXPR: | |
10759 | r_name = "|"; | |
10760 | break; | |
10761 | case TRUTH_ANDIF_EXPR: | |
10762 | r_name = "&&"; | |
10763 | break; | |
10764 | case TRUTH_ORIF_EXPR: | |
10765 | r_name = "||"; | |
10766 | break; | |
10767 | default: | |
10768 | gcc_unreachable (); | |
10769 | } | |
10770 | if (r_name) | |
10771 | { | |
10772 | error_at (OMP_CLAUSE_LOCATION (c), | |
10773 | "%qE has invalid type for %<reduction(%s)%>", | |
10774 | t, r_name); | |
10775 | remove = true; | |
10776 | } | |
10777 | } | |
10778 | goto check_dup_generic; | |
10779 | ||
10780 | case OMP_CLAUSE_COPYPRIVATE: | |
10781 | name = "copyprivate"; | |
10782 | goto check_dup_generic; | |
10783 | ||
10784 | case OMP_CLAUSE_COPYIN: | |
10785 | name = "copyin"; | |
10786 | t = OMP_CLAUSE_DECL (c); | |
10787 | if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) | |
10788 | { | |
10789 | error_at (OMP_CLAUSE_LOCATION (c), | |
10790 | "%qE must be %<threadprivate%> for %<copyin%>", t); | |
10791 | remove = true; | |
10792 | } | |
10793 | goto check_dup_generic; | |
10794 | ||
10795 | check_dup_generic: | |
10796 | t = OMP_CLAUSE_DECL (c); | |
10797 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10798 | { | |
10799 | error_at (OMP_CLAUSE_LOCATION (c), | |
10800 | "%qE is not a variable in clause %qs", t, name); | |
10801 | remove = true; | |
10802 | } | |
10803 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10804 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
10805 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10806 | { | |
10807 | error_at (OMP_CLAUSE_LOCATION (c), | |
10808 | "%qE appears more than once in data clauses", t); | |
10809 | remove = true; | |
10810 | } | |
10811 | else | |
10812 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
10813 | break; | |
10814 | ||
10815 | case OMP_CLAUSE_FIRSTPRIVATE: | |
10816 | name = "firstprivate"; | |
10817 | t = OMP_CLAUSE_DECL (c); | |
10818 | need_complete = true; | |
10819 | need_implicitly_determined = true; | |
10820 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10821 | { | |
10822 | error_at (OMP_CLAUSE_LOCATION (c), | |
10823 | "%qE is not a variable in clause %<firstprivate%>", t); | |
10824 | remove = true; | |
10825 | } | |
10826 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10827 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
10828 | { | |
10829 | error_at (OMP_CLAUSE_LOCATION (c), | |
10830 | "%qE appears more than once in data clauses", t); | |
10831 | remove = true; | |
10832 | } | |
10833 | else | |
10834 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
10835 | break; | |
10836 | ||
10837 | case OMP_CLAUSE_LASTPRIVATE: | |
10838 | name = "lastprivate"; | |
10839 | t = OMP_CLAUSE_DECL (c); | |
10840 | need_complete = true; | |
10841 | need_implicitly_determined = true; | |
10842 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10843 | { | |
10844 | error_at (OMP_CLAUSE_LOCATION (c), | |
10845 | "%qE is not a variable in clause %<lastprivate%>", t); | |
10846 | remove = true; | |
10847 | } | |
10848 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
10849 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
10850 | { | |
10851 | error_at (OMP_CLAUSE_LOCATION (c), | |
10852 | "%qE appears more than once in data clauses", t); | |
10853 | remove = true; | |
10854 | } | |
10855 | else | |
10856 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
10857 | break; | |
10858 | ||
10859 | case OMP_CLAUSE_IF: | |
10860 | case OMP_CLAUSE_NUM_THREADS: | |
10861 | case OMP_CLAUSE_SCHEDULE: | |
10862 | case OMP_CLAUSE_NOWAIT: | |
10863 | case OMP_CLAUSE_ORDERED: | |
10864 | case OMP_CLAUSE_DEFAULT: | |
10865 | case OMP_CLAUSE_UNTIED: | |
10866 | case OMP_CLAUSE_COLLAPSE: | |
10867 | case OMP_CLAUSE_FINAL: | |
10868 | case OMP_CLAUSE_MERGEABLE: | |
10869 | pc = &OMP_CLAUSE_CHAIN (c); | |
10870 | continue; | |
10871 | ||
10872 | default: | |
10873 | gcc_unreachable (); | |
10874 | } | |
10875 | ||
10876 | if (!remove) | |
10877 | { | |
10878 | t = OMP_CLAUSE_DECL (c); | |
10879 | ||
10880 | if (need_complete) | |
10881 | { | |
10882 | t = require_complete_type (t); | |
10883 | if (t == error_mark_node) | |
10884 | remove = true; | |
10885 | } | |
10886 | ||
10887 | if (need_implicitly_determined) | |
10888 | { | |
10889 | const char *share_name = NULL; | |
10890 | ||
10891 | if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
10892 | share_name = "threadprivate"; | |
10893 | else switch (c_omp_predetermined_sharing (t)) | |
10894 | { | |
10895 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
10896 | break; | |
10897 | case OMP_CLAUSE_DEFAULT_SHARED: | |
10898 | /* const vars may be specified in firstprivate clause. */ | |
10899 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
10900 | && TREE_READONLY (t)) | |
10901 | break; | |
10902 | share_name = "shared"; | |
10903 | break; | |
10904 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
10905 | share_name = "private"; | |
10906 | break; | |
10907 | default: | |
10908 | gcc_unreachable (); | |
10909 | } | |
10910 | if (share_name) | |
10911 | { | |
10912 | error_at (OMP_CLAUSE_LOCATION (c), | |
10913 | "%qE is predetermined %qs for %qs", | |
10914 | t, share_name, name); | |
10915 | remove = true; | |
10916 | } | |
10917 | } | |
10918 | } | |
10919 | ||
10920 | if (remove) | |
10921 | *pc = OMP_CLAUSE_CHAIN (c); | |
10922 | else | |
10923 | pc = &OMP_CLAUSE_CHAIN (c); | |
10924 | } | |
10925 | ||
10926 | bitmap_obstack_release (NULL); | |
10927 | return clauses; | |
10928 | } | |
10929 | ||
10930 | /* Create a transaction node. */ | |
10931 | ||
10932 | tree | |
10933 | c_finish_transaction (location_t loc, tree block, int flags) | |
10934 | { | |
10935 | tree stmt = build_stmt (loc, TRANSACTION_EXPR, block); | |
10936 | if (flags & TM_STMT_ATTR_OUTER) | |
10937 | TRANSACTION_EXPR_OUTER (stmt) = 1; | |
10938 | if (flags & TM_STMT_ATTR_RELAXED) | |
10939 | TRANSACTION_EXPR_RELAXED (stmt) = 1; | |
10940 | return add_stmt (stmt); | |
10941 | } | |
10942 | ||
10943 | /* Make a variant type in the proper way for C/C++, propagating qualifiers | |
10944 | down to the element type of an array. */ | |
10945 | ||
10946 | tree | |
10947 | c_build_qualified_type (tree type, int type_quals) | |
10948 | { | |
10949 | if (type == error_mark_node) | |
10950 | return type; | |
10951 | ||
10952 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10953 | { | |
10954 | tree t; | |
10955 | tree element_type = c_build_qualified_type (TREE_TYPE (type), | |
10956 | type_quals); | |
10957 | ||
10958 | /* See if we already have an identically qualified type. */ | |
10959 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) | |
10960 | { | |
10961 | if (TYPE_QUALS (strip_array_types (t)) == type_quals | |
10962 | && TYPE_NAME (t) == TYPE_NAME (type) | |
10963 | && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) | |
10964 | && attribute_list_equal (TYPE_ATTRIBUTES (t), | |
10965 | TYPE_ATTRIBUTES (type))) | |
10966 | break; | |
10967 | } | |
10968 | if (!t) | |
10969 | { | |
10970 | tree domain = TYPE_DOMAIN (type); | |
10971 | ||
10972 | t = build_variant_type_copy (type); | |
10973 | TREE_TYPE (t) = element_type; | |
10974 | ||
10975 | if (TYPE_STRUCTURAL_EQUALITY_P (element_type) | |
10976 | || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain))) | |
10977 | SET_TYPE_STRUCTURAL_EQUALITY (t); | |
10978 | else if (TYPE_CANONICAL (element_type) != element_type | |
10979 | || (domain && TYPE_CANONICAL (domain) != domain)) | |
10980 | { | |
10981 | tree unqualified_canon | |
10982 | = build_array_type (TYPE_CANONICAL (element_type), | |
10983 | domain? TYPE_CANONICAL (domain) | |
10984 | : NULL_TREE); | |
10985 | TYPE_CANONICAL (t) | |
10986 | = c_build_qualified_type (unqualified_canon, type_quals); | |
10987 | } | |
10988 | else | |
10989 | TYPE_CANONICAL (t) = t; | |
10990 | } | |
10991 | return t; | |
10992 | } | |
10993 | ||
10994 | /* A restrict-qualified pointer type must be a pointer to object or | |
10995 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
10996 | REFERENCE_TYPEs, which is appropriate for C++. */ | |
10997 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
10998 | && (!POINTER_TYPE_P (type) | |
10999 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
11000 | { | |
11001 | error ("invalid use of %<restrict%>"); | |
11002 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
11003 | } | |
11004 | ||
11005 | return build_qualified_type (type, type_quals); | |
11006 | } | |
11007 | ||
11008 | /* Build a VA_ARG_EXPR for the C parser. */ | |
11009 | ||
11010 | tree | |
11011 | c_build_va_arg (location_t loc, tree expr, tree type) | |
11012 | { | |
11013 | if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE) | |
11014 | warning_at (loc, OPT_Wc___compat, | |
11015 | "C++ requires promoted type, not enum type, in %<va_arg%>"); | |
11016 | return build_va_arg (loc, expr, type); | |
11017 | } |