1/* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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 "memmodel.h"
30#include "target.h"
31#include "function.h"
32#include "bitmap.h"
33#include "c-tree.h"
34#include "gimple-expr.h"
35#include "predict.h"
36#include "stor-layout.h"
37#include "trans-mem.h"
38#include "varasm.h"
39#include "stmt.h"
40#include "langhooks.h"
41#include "c-lang.h"
42#include "intl.h"
43#include "tree-iterator.h"
44#include "gimplify.h"
45#include "tree-inline.h"
46#include "omp-general.h"
47#include "c-family/c-objc.h"
48#include "c-family/c-ubsan.h"
49#include "gomp-constants.h"
50#include "spellcheck-tree.h"
51#include "gcc-rich-location.h"
52#include "stringpool.h"
53#include "attribs.h"
54#include "asan.h"
55
56/* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
63};
64
65/* The level of nesting inside "__alignof__". */
66int in_alignof;
67
68/* The level of nesting inside "sizeof". */
69int in_sizeof;
70
71/* The level of nesting inside "typeof". */
72int in_typeof;
73
74/* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76tree c_last_sizeof_arg;
77location_t c_last_sizeof_loc;
78
79/* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81static int found_missing_braces;
82
83static int require_constant_value;
84static int require_constant_elements;
85
86static bool null_pointer_constant_p (const_tree);
87static tree qualify_type (tree, tree);
88static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90static int comp_target_types (location_t, tree, tree);
91static int function_types_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
94static tree lookup_field (tree, tree);
95static int convert_arguments (location_t, vec<location_t>, tree,
96 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 tree);
98static tree pointer_diff (location_t, tree, tree, tree *);
99static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
100 enum impl_conv, bool, tree, tree, int);
101static tree valid_compound_expr_initializer (tree, tree);
102static void push_string (const char *);
103static void push_member_name (tree);
104static int spelling_length (void);
105static char *print_spelling (char *);
106static void warning_init (location_t, int, const char *);
107static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
108static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
109 bool, struct obstack *);
110static void output_pending_init_elements (int, struct obstack *);
111static bool set_designator (location_t, bool, struct obstack *);
112static void push_range_stack (tree, struct obstack *);
113static void add_pending_init (location_t, tree, tree, tree, bool,
114 struct obstack *);
115static void set_nonincremental_init (struct obstack *);
116static void set_nonincremental_init_from_string (tree, struct obstack *);
117static tree find_init_member (tree, struct obstack *);
118static void readonly_warning (tree, enum lvalue_use);
119static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
120static void record_maybe_used_decl (tree);
121static int comptypes_internal (const_tree, const_tree, bool *, bool *);
122
123/* Return true if EXP is a null pointer constant, false otherwise. */
124
125static bool
126null_pointer_constant_p (const_tree expr)
127{
128 /* This should really operate on c_expr structures, but they aren't
129 yet available everywhere required. */
130 tree type = TREE_TYPE (expr);
131 return (TREE_CODE (expr) == INTEGER_CST
132 && !TREE_OVERFLOW (expr)
133 && integer_zerop (expr)
134 && (INTEGRAL_TYPE_P (type)
135 || (TREE_CODE (type) == POINTER_TYPE
136 && VOID_TYPE_P (TREE_TYPE (type))
137 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
138}
139
140/* EXPR may appear in an unevaluated part of an integer constant
141 expression, but not in an evaluated part. Wrap it in a
142 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
143 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
144
145static tree
146note_integer_operands (tree expr)
147{
148 tree ret;
149 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
150 {
151 ret = copy_node (expr);
152 TREE_OVERFLOW (ret) = 1;
153 }
154 else
155 {
156 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
157 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
158 }
159 return ret;
160}
161
162/* Having checked whether EXPR may appear in an unevaluated part of an
163 integer constant expression and found that it may, remove any
164 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 expression. */
166
167static inline tree
168remove_c_maybe_const_expr (tree expr)
169{
170 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
171 return C_MAYBE_CONST_EXPR_EXPR (expr);
172 else
173 return expr;
174}
175
176 /* This is a cache to hold if two types are compatible or not. */
177
178struct tagged_tu_seen_cache {
179 const struct tagged_tu_seen_cache * next;
180 const_tree t1;
181 const_tree t2;
182 /* The return value of tagged_types_tu_compatible_p if we had seen
183 these two types already. */
184 int val;
185};
186
187static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
188static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
189
190/* Do `exp = require_complete_type (loc, exp);' to make sure exp
191 does not have an incomplete type. (That includes void types.)
192 LOC is the location of the use. */
193
194tree
195require_complete_type (location_t loc, tree value)
196{
197 tree type = TREE_TYPE (value);
198
199 if (error_operand_p (value))
200 return error_mark_node;
201
202 /* First, detect a valid value with a complete type. */
203 if (COMPLETE_TYPE_P (type))
204 return value;
205
206 c_incomplete_type_error (loc, value, type);
207 return error_mark_node;
208}
209
210/* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
212 and TYPE is the type that was invalid. LOC is the location for
213 the error. */
214
215void
216c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
217{
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
221
222 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
223 error_at (loc, "%qD has an incomplete type %qT", value, type);
224 else
225 {
226 retry:
227 /* We must print an error message. Be clever about what it says. */
228
229 switch (TREE_CODE (type))
230 {
231 case RECORD_TYPE:
232 case UNION_TYPE:
233 case ENUMERAL_TYPE:
234 break;
235
236 case VOID_TYPE:
237 error_at (loc, "invalid use of void expression");
238 return;
239
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
242 {
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
244 {
245 error_at (loc, "invalid use of flexible array member");
246 return;
247 }
248 type = TREE_TYPE (type);
249 goto retry;
250 }
251 error_at (loc, "invalid use of array with unspecified bounds");
252 return;
253
254 default:
255 gcc_unreachable ();
256 }
257
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error_at (loc, "invalid use of undefined type %qT", type);
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error_at (loc, "invalid use of incomplete typedef %qT", type);
263 }
264}
265
266/* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
268
269tree
270c_type_promotes_to (tree type)
271{
272 tree ret = NULL_TREE;
273
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 ret = double_type_node;
276 else if (c_promoting_integer_type_p (type))
277 {
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 ret = unsigned_type_node;
282 else
283 ret = integer_type_node;
284 }
285
286 if (ret != NULL_TREE)
287 return (TYPE_ATOMIC (type)
288 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
289 : ret);
290
291 return type;
292}
293
294/* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
297
298static bool
299addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
300{
301 if (as1 == as2)
302 {
303 *common = as1;
304 return true;
305 }
306 else if (targetm.addr_space.subset_p (as1, as2))
307 {
308 *common = as2;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as2, as1))
312 {
313 *common = as1;
314 return true;
315 }
316 else
317 return false;
318}
319
320/* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
322
323static tree
324qualify_type (tree type, tree like)
325{
326 addr_space_t as_type = TYPE_ADDR_SPACE (type);
327 addr_space_t as_like = TYPE_ADDR_SPACE (like);
328 addr_space_t as_common;
329
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type, as_like, &as_common))
333 {
334 as_common = as_type;
335 error ("%qT and %qT are in disjoint named address spaces",
336 type, like);
337 }
338
339 return c_build_qualified_type (type,
340 TYPE_QUALS_NO_ADDR_SPACE (type)
341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
342 | ENCODE_QUAL_ADDR_SPACE (as_common));
343}
344
345/* Return true iff the given tree T is a variable length array. */
346
347bool
348c_vla_type_p (const_tree t)
349{
350 if (TREE_CODE (t) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t))
352 return true;
353 return false;
354}
355
356/* Return the composite type of two compatible types.
357
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
361
362tree
363composite_type (tree t1, tree t2)
364{
365 enum tree_code code1;
366 enum tree_code code2;
367 tree attributes;
368
369 /* Save time if the two types are the same. */
370
371 if (t1 == t2) return t1;
372
373 /* If one type is nonsense, use the other. */
374 if (t1 == error_mark_node)
375 return t2;
376 if (t2 == error_mark_node)
377 return t1;
378
379 code1 = TREE_CODE (t1);
380 code2 = TREE_CODE (t2);
381
382 /* Merge the attributes. */
383 attributes = targetm.merge_type_attributes (t1, t2);
384
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
389
390 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
391 return t1;
392 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
393 return t2;
394
395 gcc_assert (code1 == code2);
396
397 switch (code1)
398 {
399 case POINTER_TYPE:
400 /* For two pointers, do this recursively on the target type. */
401 {
402 tree pointed_to_1 = TREE_TYPE (t1);
403 tree pointed_to_2 = TREE_TYPE (t2);
404 tree target = composite_type (pointed_to_1, pointed_to_2);
405 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
406 t1 = build_type_attribute_variant (t1, attributes);
407 return qualify_type (t1, t2);
408 }
409
410 case ARRAY_TYPE:
411 {
412 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
413 int quals;
414 tree unqual_elt;
415 tree d1 = TYPE_DOMAIN (t1);
416 tree d2 = TYPE_DOMAIN (t2);
417 bool d1_variable, d2_variable;
418 bool d1_zero, d2_zero;
419 bool t1_complete, t2_complete;
420
421 /* We should not have any type quals on arrays at all. */
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
424
425 t1_complete = COMPLETE_TYPE_P (t1);
426 t2_complete = COMPLETE_TYPE_P (t2);
427
428 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
429 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
430
431 d1_variable = (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
434 d2_variable = (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
437 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
438 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
439
440 /* Save space: see if the result is identical to one of the args. */
441 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
442 && (d2_variable || d2_zero || !d1_variable))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
445 && (d1_variable || d1_zero || !d2_variable))
446 return build_type_attribute_variant (t2, attributes);
447
448 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
451 return build_type_attribute_variant (t2, attributes);
452
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
457 back at the end. */
458 quals = TYPE_QUALS (strip_array_types (elt));
459 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
460 t1 = build_array_type (unqual_elt,
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
462 && (d2_variable
463 || d2_zero
464 || !d1_variable))
465 ? t1
466 : t2));
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero && d2_zero
470 && (t1_complete || t2_complete)
471 && !COMPLETE_TYPE_P (t1))
472 {
473 TYPE_SIZE (t1) = bitsize_zero_node;
474 TYPE_SIZE_UNIT (t1) = size_zero_node;
475 }
476 t1 = c_build_qualified_type (t1, quals);
477 return build_type_attribute_variant (t1, attributes);
478 }
479
480 case ENUMERAL_TYPE:
481 case RECORD_TYPE:
482 case UNION_TYPE:
483 if (attributes != NULL)
484 {
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
487 return t1;
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
489 return t2;
490 }
491 return build_type_attribute_variant (t1, attributes);
492
493 case FUNCTION_TYPE:
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
496 {
497 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
498 tree p1 = TYPE_ARG_TYPES (t1);
499 tree p2 = TYPE_ARG_TYPES (t2);
500 int len;
501 tree newargs, n;
502 int i;
503
504 /* Save space: see if the result is identical to one of the args. */
505 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
506 return build_type_attribute_variant (t1, attributes);
507 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
508 return build_type_attribute_variant (t2, attributes);
509
510 /* Simple way if one arg fails to specify argument types. */
511 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
512 {
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
516 }
517 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
518 {
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
522 }
523
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
526
527 for (len = 0, newargs = p1;
528 newargs && newargs != void_list_node;
529 len++, newargs = TREE_CHAIN (newargs))
530 ;
531
532 for (i = 0; i < len; i++)
533 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
534
535 n = newargs;
536
537 for (; p1 && p1 != void_list_node;
538 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
539 {
540 /* A null type means arg type is not specified.
541 Take whatever the other function type has. */
542 if (TREE_VALUE (p1) == NULL_TREE)
543 {
544 TREE_VALUE (n) = TREE_VALUE (p2);
545 goto parm_done;
546 }
547 if (TREE_VALUE (p2) == NULL_TREE)
548 {
549 TREE_VALUE (n) = TREE_VALUE (p1);
550 goto parm_done;
551 }
552
553 /* Given wait (union {union wait *u; int *i} *)
554 and wait (union wait *),
555 prefer union wait * as type of parm. */
556 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
557 && TREE_VALUE (p1) != TREE_VALUE (p2))
558 {
559 tree memb;
560 tree mv2 = TREE_VALUE (p2);
561 if (mv2 && mv2 != error_mark_node
562 && TREE_CODE (mv2) != ARRAY_TYPE)
563 mv2 = TYPE_MAIN_VARIANT (mv2);
564 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
565 memb; memb = DECL_CHAIN (memb))
566 {
567 tree mv3 = TREE_TYPE (memb);
568 if (mv3 && mv3 != error_mark_node
569 && TREE_CODE (mv3) != ARRAY_TYPE)
570 mv3 = TYPE_MAIN_VARIANT (mv3);
571 if (comptypes (mv3, mv2))
572 {
573 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
574 TREE_VALUE (p2));
575 pedwarn (input_location, OPT_Wpedantic,
576 "function types not truly compatible in ISO C");
577 goto parm_done;
578 }
579 }
580 }
581 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
582 && TREE_VALUE (p2) != TREE_VALUE (p1))
583 {
584 tree memb;
585 tree mv1 = TREE_VALUE (p1);
586 if (mv1 && mv1 != error_mark_node
587 && TREE_CODE (mv1) != ARRAY_TYPE)
588 mv1 = TYPE_MAIN_VARIANT (mv1);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
590 memb; memb = DECL_CHAIN (memb))
591 {
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv1))
597 {
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p1));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
603 }
604 }
605 }
606 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
607 parm_done: ;
608 }
609
610 t1 = build_function_type (valtype, newargs);
611 t1 = qualify_type (t1, t2);
612 }
613 /* FALLTHRU */
614
615 default:
616 return build_type_attribute_variant (t1, attributes);
617 }
618
619}
620
621/* Return the type of a conditional expression between pointers to
622 possibly differently qualified versions of compatible types.
623
624 We assume that comp_target_types has already been done and returned
625 nonzero; if that isn't so, this may crash. */
626
627static tree
628common_pointer_type (tree t1, tree t2)
629{
630 tree attributes;
631 tree pointed_to_1, mv1;
632 tree pointed_to_2, mv2;
633 tree target;
634 unsigned target_quals;
635 addr_space_t as1, as2, as_common;
636 int quals1, quals2;
637
638 /* Save time if the two types are the same. */
639
640 if (t1 == t2) return t1;
641
642 /* If one type is nonsense, use the other. */
643 if (t1 == error_mark_node)
644 return t2;
645 if (t2 == error_mark_node)
646 return t1;
647
648 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
649 && TREE_CODE (t2) == POINTER_TYPE);
650
651 /* Merge the attributes. */
652 attributes = targetm.merge_type_attributes (t1, t2);
653
654 /* Find the composite type of the target types, and combine the
655 qualifiers of the two types' targets. Do not lose qualifiers on
656 array element types by taking the TYPE_MAIN_VARIANT. */
657 mv1 = pointed_to_1 = TREE_TYPE (t1);
658 mv2 = pointed_to_2 = TREE_TYPE (t2);
659 if (TREE_CODE (mv1) != ARRAY_TYPE)
660 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
661 if (TREE_CODE (mv2) != ARRAY_TYPE)
662 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
663 target = composite_type (mv1, mv2);
664
665 /* Strip array types to get correct qualifier for pointers to arrays */
666 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
667 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
668
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
673 target_quals = (quals1 & quals2);
674 else
675 target_quals = (quals1 | quals2);
676
677 /* If the two named address spaces are different, determine the common
678 superset address space. This is guaranteed to exist due to the
679 assumption that comp_target_type returned non-zero. */
680 as1 = TYPE_ADDR_SPACE (pointed_to_1);
681 as2 = TYPE_ADDR_SPACE (pointed_to_2);
682 if (!addr_space_superset (as1, as2, &as_common))
683 gcc_unreachable ();
684
685 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
686
687 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
688 return build_type_attribute_variant (t1, attributes);
689}
690
691/* Return the common type for two arithmetic types under the usual
692 arithmetic conversions. The default conversions have already been
693 applied, and enumerated types converted to their compatible integer
694 types. The resulting type is unqualified and has no attributes.
695
696 This is the type for the result of most arithmetic operations
697 if the operands have the given two types. */
698
699static tree
700c_common_type (tree t1, tree t2)
701{
702 enum tree_code code1;
703 enum tree_code code2;
704
705 /* If one type is nonsense, use the other. */
706 if (t1 == error_mark_node)
707 return t2;
708 if (t2 == error_mark_node)
709 return t1;
710
711 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
712 t1 = TYPE_MAIN_VARIANT (t1);
713
714 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
715 t2 = TYPE_MAIN_VARIANT (t2);
716
717 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
718 t1 = build_type_attribute_variant (t1, NULL_TREE);
719
720 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
721 t2 = build_type_attribute_variant (t2, NULL_TREE);
722
723 /* Save time if the two types are the same. */
724
725 if (t1 == t2) return t1;
726
727 code1 = TREE_CODE (t1);
728 code2 = TREE_CODE (t2);
729
730 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
731 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
732 || code1 == INTEGER_TYPE);
733 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
734 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
735 || code2 == INTEGER_TYPE);
736
737 /* When one operand is a decimal float type, the other operand cannot be
738 a generic float type or a complex type. We also disallow vector types
739 here. */
740 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
741 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
742 {
743 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
744 {
745 error ("can%'t mix operands of decimal float and vector types");
746 return error_mark_node;
747 }
748 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
749 {
750 error ("can%'t mix operands of decimal float and complex types");
751 return error_mark_node;
752 }
753 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
754 {
755 error ("can%'t mix operands of decimal float and other float types");
756 return error_mark_node;
757 }
758 }
759
760 /* If one type is a vector type, return that type. (How the usual
761 arithmetic conversions apply to the vector types extension is not
762 precisely specified.) */
763 if (code1 == VECTOR_TYPE)
764 return t1;
765
766 if (code2 == VECTOR_TYPE)
767 return t2;
768
769 /* If one type is complex, form the common type of the non-complex
770 components, then make that complex. Use T1 or T2 if it is the
771 required type. */
772 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
773 {
774 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
775 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
776 tree subtype = c_common_type (subtype1, subtype2);
777
778 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
779 return t1;
780 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
781 return t2;
782 else
783 return build_complex_type (subtype);
784 }
785
786 /* If only one is real, use it as the result. */
787
788 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
789 return t1;
790
791 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
792 return t2;
793
794 /* If both are real and either are decimal floating point types, use
795 the decimal floating point type with the greater precision. */
796
797 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
798 {
799 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
801 return dfloat128_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
804 return dfloat64_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
807 return dfloat32_type_node;
808 }
809
810 /* Deal with fixed-point types. */
811 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
812 {
813 unsigned int unsignedp = 0, satp = 0;
814 scalar_mode m1, m2;
815 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
816
817 m1 = SCALAR_TYPE_MODE (t1);
818 m2 = SCALAR_TYPE_MODE (t2);
819
820 /* If one input type is saturating, the result type is saturating. */
821 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
822 satp = 1;
823
824 /* If both fixed-point types are unsigned, the result type is unsigned.
825 When mixing fixed-point and integer types, follow the sign of the
826 fixed-point type.
827 Otherwise, the result type is signed. */
828 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
829 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
830 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t1))
832 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t2)))
834 unsignedp = 1;
835
836 /* The result type is signed. */
837 if (unsignedp == 0)
838 {
839 /* If the input type is unsigned, we need to convert to the
840 signed type. */
841 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
842 {
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
850 m1 = as_a <scalar_mode>
851 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
852 }
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
854 {
855 enum mode_class mclass = (enum mode_class) 0;
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
862 m2 = as_a <scalar_mode>
863 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
864 }
865 }
866
867 if (code1 == FIXED_POINT_TYPE)
868 {
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
871 }
872 else
873 {
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
877 }
878
879 if (code2 == FIXED_POINT_TYPE)
880 {
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
883 }
884 else
885 {
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
889 }
890
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
895 }
896
897 /* Both real or both integers; use the one with greater precision. */
898
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
903
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
907
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
911
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
914 {
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
918 return long_long_integer_type_node;
919 }
920
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
924
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
927 {
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
934 }
935
936 /* For floating types of the same TYPE_PRECISION (which we here
937 assume means either the same set of values, or sets of values
938 neither a subset of the other, with behavior being undefined in
939 the latter case), follow the rules from TS 18661-3: prefer
940 interchange types _FloatN, then standard types long double,
941 double, float, then extended types _FloatNx. For extended types,
942 check them starting with _Float128x as that seems most consistent
943 in spirit with preferring long double to double; for interchange
944 types, also check in that order for consistency although it's not
945 possible for more than one of them to have the same
946 precision. */
947 tree mv1 = TYPE_MAIN_VARIANT (t1);
948 tree mv2 = TYPE_MAIN_VARIANT (t2);
949
950 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
951 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
952 return FLOATN_TYPE_NODE (i);
953
954 /* Likewise, prefer long double to double even if same size. */
955 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
956 return long_double_type_node;
957
958 /* Likewise, prefer double to float even if same size.
959 We got a couple of embedded targets with 32 bit doubles, and the
960 pdp11 might have 64 bit floats. */
961 if (mv1 == double_type_node || mv2 == double_type_node)
962 return double_type_node;
963
964 if (mv1 == float_type_node || mv2 == float_type_node)
965 return float_type_node;
966
967 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
968 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
969 return FLOATNX_TYPE_NODE (i);
970
971 /* Otherwise prefer the unsigned one. */
972
973 if (TYPE_UNSIGNED (t1))
974 return t1;
975 else
976 return t2;
977}
978
979/* Wrapper around c_common_type that is used by c-common.c and other
980 front end optimizations that remove promotions. ENUMERAL_TYPEs
981 are allowed here and are converted to their compatible integer types.
982 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
983 preferably a non-Boolean type as the common type. */
984tree
985common_type (tree t1, tree t2)
986{
987 if (TREE_CODE (t1) == ENUMERAL_TYPE)
988 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
989 if (TREE_CODE (t2) == ENUMERAL_TYPE)
990 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
991
992 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE
994 && TREE_CODE (t2) == BOOLEAN_TYPE)
995 return boolean_type_node;
996
997 /* If either type is BOOLEAN_TYPE, then return the other. */
998 if (TREE_CODE (t1) == BOOLEAN_TYPE)
999 return t2;
1000 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1001 return t1;
1002
1003 return c_common_type (t1, t2);
1004}
1005
1006/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1007 or various other operations. Return 2 if they are compatible
1008 but a warning may be needed if you use them together. */
1009
1010int
1011comptypes (tree type1, tree type2)
1012{
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1015
1016 val = comptypes_internal (type1, type2, NULL, NULL);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1018
1019 return val;
1020}
1021
1022/* Like comptypes, but if it returns non-zero because enum and int are
1023 compatible, it sets *ENUM_AND_INT_P to true. */
1024
1025static int
1026comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1027{
1028 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1029 int val;
1030
1031 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1032 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1033
1034 return val;
1035}
1036
1037/* Like comptypes, but if it returns nonzero for different types, it
1038 sets *DIFFERENT_TYPES_P to true. */
1039
1040int
1041comptypes_check_different_types (tree type1, tree type2,
1042 bool *different_types_p)
1043{
1044 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1045 int val;
1046
1047 val = comptypes_internal (type1, type2, NULL, different_types_p);
1048 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1049
1050 return val;
1051}
1052
1053/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1054 or various other operations. Return 2 if they are compatible
1055 but a warning may be needed if you use them together. If
1056 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1057 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1058 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1059 NULL, and the types are compatible but different enough not to be
1060 permitted in C11 typedef redeclarations, then this sets
1061 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1062 false, but may or may not be set if the types are incompatible.
1063 This differs from comptypes, in that we don't free the seen
1064 types. */
1065
1066static int
1067comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1068 bool *different_types_p)
1069{
1070 const_tree t1 = type1;
1071 const_tree t2 = type2;
1072 int attrval, val;
1073
1074 /* Suppress errors caused by previously reported errors. */
1075
1076 if (t1 == t2 || !t1 || !t2
1077 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1078 return 1;
1079
1080 /* Enumerated types are compatible with integer types, but this is
1081 not transitive: two enumerated types in the same translation unit
1082 are compatible with each other only if they are the same type. */
1083
1084 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1085 {
1086 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1087 if (TREE_CODE (t2) != VOID_TYPE)
1088 {
1089 if (enum_and_int_p != NULL)
1090 *enum_and_int_p = true;
1091 if (different_types_p != NULL)
1092 *different_types_p = true;
1093 }
1094 }
1095 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1096 {
1097 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1098 if (TREE_CODE (t1) != VOID_TYPE)
1099 {
1100 if (enum_and_int_p != NULL)
1101 *enum_and_int_p = true;
1102 if (different_types_p != NULL)
1103 *different_types_p = true;
1104 }
1105 }
1106
1107 if (t1 == t2)
1108 return 1;
1109
1110 /* Different classes of types can't be compatible. */
1111
1112 if (TREE_CODE (t1) != TREE_CODE (t2))
1113 return 0;
1114
1115 /* Qualifiers must match. C99 6.7.3p9 */
1116
1117 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1118 return 0;
1119
1120 /* Allow for two different type nodes which have essentially the same
1121 definition. Note that we already checked for equality of the type
1122 qualifiers (just above). */
1123
1124 if (TREE_CODE (t1) != ARRAY_TYPE
1125 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1126 return 1;
1127
1128 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1129 if (!(attrval = comp_type_attributes (t1, t2)))
1130 return 0;
1131
1132 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1133 val = 0;
1134
1135 switch (TREE_CODE (t1))
1136 {
1137 case INTEGER_TYPE:
1138 case FIXED_POINT_TYPE:
1139 case REAL_TYPE:
1140 /* With these nodes, we can't determine type equivalence by
1141 looking at what is stored in the nodes themselves, because
1142 two nodes might have different TYPE_MAIN_VARIANTs but still
1143 represent the same type. For example, wchar_t and int could
1144 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1145 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1146 and are distinct types. On the other hand, int and the
1147 following typedef
1148
1149 typedef int INT __attribute((may_alias));
1150
1151 have identical properties, different TYPE_MAIN_VARIANTs, but
1152 represent the same type. The canonical type system keeps
1153 track of equivalence in this case, so we fall back on it. */
1154 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1155
1156 case POINTER_TYPE:
1157 /* Do not remove mode information. */
1158 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1159 break;
1160 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1161 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1162 enum_and_int_p, different_types_p));
1163 break;
1164
1165 case FUNCTION_TYPE:
1166 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1167 different_types_p);
1168 break;
1169
1170 case ARRAY_TYPE:
1171 {
1172 tree d1 = TYPE_DOMAIN (t1);
1173 tree d2 = TYPE_DOMAIN (t2);
1174 bool d1_variable, d2_variable;
1175 bool d1_zero, d2_zero;
1176 val = 1;
1177
1178 /* Target types must match incl. qualifiers. */
1179 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1180 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p,
1182 different_types_p)))
1183 return 0;
1184
1185 if (different_types_p != NULL
1186 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1187 *different_types_p = true;
1188 /* Sizes must match unless one is missing or variable. */
1189 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1190 break;
1191
1192 d1_zero = !TYPE_MAX_VALUE (d1);
1193 d2_zero = !TYPE_MAX_VALUE (d2);
1194
1195 d1_variable = (!d1_zero
1196 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1198 d2_variable = (!d2_zero
1199 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1200 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1201 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1202 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1203
1204 if (different_types_p != NULL
1205 && d1_variable != d2_variable)
1206 *different_types_p = true;
1207 if (d1_variable || d2_variable)
1208 break;
1209 if (d1_zero && d2_zero)
1210 break;
1211 if (d1_zero || d2_zero
1212 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1213 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1214 val = 0;
1215
1216 break;
1217 }
1218
1219 case ENUMERAL_TYPE:
1220 case RECORD_TYPE:
1221 case UNION_TYPE:
1222 if (val != 1 && !same_translation_unit_p (t1, t2))
1223 {
1224 tree a1 = TYPE_ATTRIBUTES (t1);
1225 tree a2 = TYPE_ATTRIBUTES (t2);
1226
1227 if (! attribute_list_contained (a1, a2)
1228 && ! attribute_list_contained (a2, a1))
1229 break;
1230
1231 if (attrval != 2)
1232 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1233 different_types_p);
1234 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1235 different_types_p);
1236 }
1237 break;
1238
1239 case VECTOR_TYPE:
1240 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1241 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1242 enum_and_int_p, different_types_p));
1243 break;
1244
1245 default:
1246 break;
1247 }
1248 return attrval == 2 && val == 1 ? 2 : val;
1249}
1250
1251/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1252 their qualifiers, except for named address spaces. If the pointers point to
1253 different named addresses, then we must determine if one address space is a
1254 subset of the other. */
1255
1256static int
1257comp_target_types (location_t location, tree ttl, tree ttr)
1258{
1259 int val;
1260 int val_ped;
1261 tree mvl = TREE_TYPE (ttl);
1262 tree mvr = TREE_TYPE (ttr);
1263 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1264 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1265 addr_space_t as_common;
1266 bool enum_and_int_p;
1267
1268 /* Fail if pointers point to incompatible address spaces. */
1269 if (!addr_space_superset (asl, asr, &as_common))
1270 return 0;
1271
1272 /* For pedantic record result of comptypes on arrays before losing
1273 qualifiers on the element type below. */
1274 val_ped = 1;
1275
1276 if (TREE_CODE (mvl) == ARRAY_TYPE
1277 && TREE_CODE (mvr) == ARRAY_TYPE)
1278 val_ped = comptypes (mvl, mvr);
1279
1280 /* Qualifiers on element types of array types that are
1281 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1282
1283 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1284 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1285 : TYPE_MAIN_VARIANT (mvl));
1286
1287 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1288 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1289 : TYPE_MAIN_VARIANT (mvr));
1290
1291 enum_and_int_p = false;
1292 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1293
1294 if (val == 1 && val_ped != 1)
1295 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1296 "are incompatible in ISO C");
1297
1298 if (val == 2)
1299 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1300
1301 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1302 warning_at (location, OPT_Wc___compat,
1303 "pointer target types incompatible in C++");
1304
1305 return val;
1306}
1307
1308/* Subroutines of `comptypes'. */
1309
1310/* Determine whether two trees derive from the same translation unit.
1311 If the CONTEXT chain ends in a null, that tree's context is still
1312 being parsed, so if two trees have context chains ending in null,
1313 they're in the same translation unit. */
1314
1315bool
1316same_translation_unit_p (const_tree t1, const_tree t2)
1317{
1318 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1319 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1320 {
1321 case tcc_declaration:
1322 t1 = DECL_CONTEXT (t1); break;
1323 case tcc_type:
1324 t1 = TYPE_CONTEXT (t1); break;
1325 case tcc_exceptional:
1326 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1327 default: gcc_unreachable ();
1328 }
1329
1330 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1331 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1332 {
1333 case tcc_declaration:
1334 t2 = DECL_CONTEXT (t2); break;
1335 case tcc_type:
1336 t2 = TYPE_CONTEXT (t2); break;
1337 case tcc_exceptional:
1338 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1339 default: gcc_unreachable ();
1340 }
1341
1342 return t1 == t2;
1343}
1344
1345/* Allocate the seen two types, assuming that they are compatible. */
1346
1347static struct tagged_tu_seen_cache *
1348alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1349{
1350 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1351 tu->next = tagged_tu_seen_base;
1352 tu->t1 = t1;
1353 tu->t2 = t2;
1354
1355 tagged_tu_seen_base = tu;
1356
1357 /* The C standard says that two structures in different translation
1358 units are compatible with each other only if the types of their
1359 fields are compatible (among other things). We assume that they
1360 are compatible until proven otherwise when building the cache.
1361 An example where this can occur is:
1362 struct a
1363 {
1364 struct a *next;
1365 };
1366 If we are comparing this against a similar struct in another TU,
1367 and did not assume they were compatible, we end up with an infinite
1368 loop. */
1369 tu->val = 1;
1370 return tu;
1371}
1372
1373/* Free the seen types until we get to TU_TIL. */
1374
1375static void
1376free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1377{
1378 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1379 while (tu != tu_til)
1380 {
1381 const struct tagged_tu_seen_cache *const tu1
1382 = (const struct tagged_tu_seen_cache *) tu;
1383 tu = tu1->next;
1384 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1385 }
1386 tagged_tu_seen_base = tu_til;
1387}
1388
1389/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1390 compatible. If the two types are not the same (which has been
1391 checked earlier), this can only happen when multiple translation
1392 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1393 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1394 comptypes_internal. */
1395
1396static int
1397tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1398 bool *enum_and_int_p, bool *different_types_p)
1399{
1400 tree s1, s2;
1401 bool needs_warning = false;
1402
1403 /* We have to verify that the tags of the types are the same. This
1404 is harder than it looks because this may be a typedef, so we have
1405 to go look at the original type. It may even be a typedef of a
1406 typedef...
1407 In the case of compiler-created builtin structs the TYPE_DECL
1408 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1409 while (TYPE_NAME (t1)
1410 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1411 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1412 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1413
1414 while (TYPE_NAME (t2)
1415 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1416 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1417 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1418
1419 /* C90 didn't have the requirement that the two tags be the same. */
1420 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1421 return 0;
1422
1423 /* C90 didn't say what happened if one or both of the types were
1424 incomplete; we choose to follow C99 rules here, which is that they
1425 are compatible. */
1426 if (TYPE_SIZE (t1) == NULL
1427 || TYPE_SIZE (t2) == NULL)
1428 return 1;
1429
1430 {
1431 const struct tagged_tu_seen_cache * tts_i;
1432 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1433 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1434 return tts_i->val;
1435 }
1436
1437 switch (TREE_CODE (t1))
1438 {
1439 case ENUMERAL_TYPE:
1440 {
1441 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1442 /* Speed up the case where the type values are in the same order. */
1443 tree tv1 = TYPE_VALUES (t1);
1444 tree tv2 = TYPE_VALUES (t2);
1445
1446 if (tv1 == tv2)
1447 {
1448 return 1;
1449 }
1450
1451 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1452 {
1453 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1454 break;
1455 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1456 {
1457 tu->val = 0;
1458 return 0;
1459 }
1460 }
1461
1462 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1463 {
1464 return 1;
1465 }
1466 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
1471
1472 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1473 {
1474 tu->val = 0;
1475 return 0;
1476 }
1477
1478 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1479 {
1480 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1481 if (s2 == NULL
1482 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1483 {
1484 tu->val = 0;
1485 return 0;
1486 }
1487 }
1488 return 1;
1489 }
1490
1491 case UNION_TYPE:
1492 {
1493 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1494 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1495 {
1496 tu->val = 0;
1497 return 0;
1498 }
1499
1500 /* Speed up the common case where the fields are in the same order. */
1501 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1503 {
1504 int result;
1505
1506 if (DECL_NAME (s1) != DECL_NAME (s2))
1507 break;
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1510
1511 if (result != 1 && !DECL_NAME (s1))
1512 break;
1513 if (result == 0)
1514 {
1515 tu->val = 0;
1516 return 0;
1517 }
1518 if (result == 2)
1519 needs_warning = true;
1520
1521 if (TREE_CODE (s1) == FIELD_DECL
1522 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1523 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1524 {
1525 tu->val = 0;
1526 return 0;
1527 }
1528 }
1529 if (!s1 && !s2)
1530 {
1531 tu->val = needs_warning ? 2 : 1;
1532 return tu->val;
1533 }
1534
1535 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1536 {
1537 bool ok = false;
1538
1539 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1540 if (DECL_NAME (s1) == DECL_NAME (s2))
1541 {
1542 int result;
1543
1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1545 enum_and_int_p,
1546 different_types_p);
1547
1548 if (result != 1 && !DECL_NAME (s1))
1549 continue;
1550 if (result == 0)
1551 {
1552 tu->val = 0;
1553 return 0;
1554 }
1555 if (result == 2)
1556 needs_warning = true;
1557
1558 if (TREE_CODE (s1) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 break;
1562
1563 ok = true;
1564 break;
1565 }
1566 if (!ok)
1567 {
1568 tu->val = 0;
1569 return 0;
1570 }
1571 }
1572 tu->val = needs_warning ? 2 : 10;
1573 return tu->val;
1574 }
1575
1576 case RECORD_TYPE:
1577 {
1578 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1579
1580 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1581 s1 && s2;
1582 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1583 {
1584 int result;
1585 if (TREE_CODE (s1) != TREE_CODE (s2)
1586 || DECL_NAME (s1) != DECL_NAME (s2))
1587 break;
1588 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1589 enum_and_int_p, different_types_p);
1590 if (result == 0)
1591 break;
1592 if (result == 2)
1593 needs_warning = true;
1594
1595 if (TREE_CODE (s1) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1598 break;
1599 }
1600 if (s1 && s2)
1601 tu->val = 0;
1602 else
1603 tu->val = needs_warning ? 2 : 1;
1604 return tu->val;
1605 }
1606
1607 default:
1608 gcc_unreachable ();
1609 }
1610}
1611
1612/* Return 1 if two function types F1 and F2 are compatible.
1613 If either type specifies no argument types,
1614 the other must specify a fixed number of self-promoting arg types.
1615 Otherwise, if one type specifies only the number of arguments,
1616 the other must specify that number of self-promoting arg types.
1617 Otherwise, the argument types must match.
1618 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1619
1620static int
1621function_types_compatible_p (const_tree f1, const_tree f2,
1622 bool *enum_and_int_p, bool *different_types_p)
1623{
1624 tree args1, args2;
1625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1626 int val = 1;
1627 int val1;
1628 tree ret1, ret2;
1629
1630 ret1 = TREE_TYPE (f1);
1631 ret2 = TREE_TYPE (f2);
1632
1633 /* 'volatile' qualifiers on a function's return type used to mean
1634 the function is noreturn. */
1635 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1636 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1637 if (TYPE_VOLATILE (ret1))
1638 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1639 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1640 if (TYPE_VOLATILE (ret2))
1641 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1642 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1643 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1644 if (val == 0)
1645 return 0;
1646
1647 args1 = TYPE_ARG_TYPES (f1);
1648 args2 = TYPE_ARG_TYPES (f2);
1649
1650 if (different_types_p != NULL
1651 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1652 *different_types_p = true;
1653
1654 /* An unspecified parmlist matches any specified parmlist
1655 whose argument types don't need default promotions. */
1656
1657 if (args1 == NULL_TREE)
1658 {
1659 if (!self_promoting_args_p (args2))
1660 return 0;
1661 /* If one of these types comes from a non-prototype fn definition,
1662 compare that with the other type's arglist.
1663 If they don't match, ask for a warning (but no error). */
1664 if (TYPE_ACTUAL_ARG_TYPES (f1)
1665 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1666 enum_and_int_p, different_types_p))
1667 val = 2;
1668 return val;
1669 }
1670 if (args2 == NULL_TREE)
1671 {
1672 if (!self_promoting_args_p (args1))
1673 return 0;
1674 if (TYPE_ACTUAL_ARG_TYPES (f2)
1675 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1676 enum_and_int_p, different_types_p))
1677 val = 2;
1678 return val;
1679 }
1680
1681 /* Both types have argument lists: compare them and propagate results. */
1682 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1683 different_types_p);
1684 return val1 != 1 ? val1 : val;
1685}
1686
1687/* Check two lists of types for compatibility, returning 0 for
1688 incompatible, 1 for compatible, or 2 for compatible with
1689 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1690 comptypes_internal. */
1691
1692static int
1693type_lists_compatible_p (const_tree args1, const_tree args2,
1694 bool *enum_and_int_p, bool *different_types_p)
1695{
1696 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1697 int val = 1;
1698 int newval = 0;
1699
1700 while (1)
1701 {
1702 tree a1, mv1, a2, mv2;
1703 if (args1 == NULL_TREE && args2 == NULL_TREE)
1704 return val;
1705 /* If one list is shorter than the other,
1706 they fail to match. */
1707 if (args1 == NULL_TREE || args2 == NULL_TREE)
1708 return 0;
1709 mv1 = a1 = TREE_VALUE (args1);
1710 mv2 = a2 = TREE_VALUE (args2);
1711 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1712 mv1 = (TYPE_ATOMIC (mv1)
1713 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1714 TYPE_QUAL_ATOMIC)
1715 : TYPE_MAIN_VARIANT (mv1));
1716 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1717 mv2 = (TYPE_ATOMIC (mv2)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1719 TYPE_QUAL_ATOMIC)
1720 : TYPE_MAIN_VARIANT (mv2));
1721 /* A null pointer instead of a type
1722 means there is supposed to be an argument
1723 but nothing is specified about what type it has.
1724 So match anything that self-promotes. */
1725 if (different_types_p != NULL
1726 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1727 *different_types_p = true;
1728 if (a1 == NULL_TREE)
1729 {
1730 if (c_type_promotes_to (a2) != a2)
1731 return 0;
1732 }
1733 else if (a2 == NULL_TREE)
1734 {
1735 if (c_type_promotes_to (a1) != a1)
1736 return 0;
1737 }
1738 /* If one of the lists has an error marker, ignore this arg. */
1739 else if (TREE_CODE (a1) == ERROR_MARK
1740 || TREE_CODE (a2) == ERROR_MARK)
1741 ;
1742 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1743 different_types_p)))
1744 {
1745 if (different_types_p != NULL)
1746 *different_types_p = true;
1747 /* Allow wait (union {union wait *u; int *i} *)
1748 and wait (union wait *) to be compatible. */
1749 if (TREE_CODE (a1) == UNION_TYPE
1750 && (TYPE_NAME (a1) == NULL_TREE
1751 || TYPE_TRANSPARENT_AGGR (a1))
1752 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1753 && tree_int_cst_equal (TYPE_SIZE (a1),
1754 TYPE_SIZE (a2)))
1755 {
1756 tree memb;
1757 for (memb = TYPE_FIELDS (a1);
1758 memb; memb = DECL_CHAIN (memb))
1759 {
1760 tree mv3 = TREE_TYPE (memb);
1761 if (mv3 && mv3 != error_mark_node
1762 && TREE_CODE (mv3) != ARRAY_TYPE)
1763 mv3 = (TYPE_ATOMIC (mv3)
1764 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1765 TYPE_QUAL_ATOMIC)
1766 : TYPE_MAIN_VARIANT (mv3));
1767 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1768 different_types_p))
1769 break;
1770 }
1771 if (memb == NULL_TREE)
1772 return 0;
1773 }
1774 else if (TREE_CODE (a2) == UNION_TYPE
1775 && (TYPE_NAME (a2) == NULL_TREE
1776 || TYPE_TRANSPARENT_AGGR (a2))
1777 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1778 && tree_int_cst_equal (TYPE_SIZE (a2),
1779 TYPE_SIZE (a1)))
1780 {
1781 tree memb;
1782 for (memb = TYPE_FIELDS (a2);
1783 memb; memb = DECL_CHAIN (memb))
1784 {
1785 tree mv3 = TREE_TYPE (memb);
1786 if (mv3 && mv3 != error_mark_node
1787 && TREE_CODE (mv3) != ARRAY_TYPE)
1788 mv3 = (TYPE_ATOMIC (mv3)
1789 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1790 TYPE_QUAL_ATOMIC)
1791 : TYPE_MAIN_VARIANT (mv3));
1792 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1793 different_types_p))
1794 break;
1795 }
1796 if (memb == NULL_TREE)
1797 return 0;
1798 }
1799 else
1800 return 0;
1801 }
1802
1803 /* comptypes said ok, but record if it said to warn. */
1804 if (newval > val)
1805 val = newval;
1806
1807 args1 = TREE_CHAIN (args1);
1808 args2 = TREE_CHAIN (args2);
1809 }
1810}
1811
1812/* Compute the size to increment a pointer by. When a function type or void
1813 type or incomplete type is passed, size_one_node is returned.
1814 This function does not emit any diagnostics; the caller is responsible
1815 for that. */
1816
1817static tree
1818c_size_in_bytes (const_tree type)
1819{
1820 enum tree_code code = TREE_CODE (type);
1821
1822 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1823 || !COMPLETE_TYPE_P (type))
1824 return size_one_node;
1825
1826 /* Convert in case a char is more than one unit. */
1827 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1828 size_int (TYPE_PRECISION (char_type_node)
1829 / BITS_PER_UNIT));
1830}
1831
1832/* Return either DECL or its known constant value (if it has one). */
1833
1834tree
1835decl_constant_value_1 (tree decl)
1836{
1837 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1838 TREE_CODE (decl) != PARM_DECL
1839 && !TREE_THIS_VOLATILE (decl)
1840 && TREE_READONLY (decl)
1841 && DECL_INITIAL (decl) != NULL_TREE
1842 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1843 /* This is invalid if initial value is not constant.
1844 If it has either a function call, a memory reference,
1845 or a variable, then re-evaluating it could give different results. */
1846 && TREE_CONSTANT (DECL_INITIAL (decl))
1847 /* Check for cases where this is sub-optimal, even though valid. */
1848 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1849 return DECL_INITIAL (decl);
1850 return decl;
1851}
1852
1853/* Return either DECL or its known constant value (if it has one).
1854 Like the above, but always return decl outside of functions. */
1855
1856tree
1857decl_constant_value (tree decl)
1858{
1859 /* Don't change a variable array bound or initial value to a constant
1860 in a place where a variable is invalid. */
1861 return current_function_decl ? decl_constant_value_1 (decl) : decl;
1862}
1863
1864/* Convert the array expression EXP to a pointer. */
1865static tree
1866array_to_pointer_conversion (location_t loc, tree exp)
1867{
1868 tree orig_exp = exp;
1869 tree type = TREE_TYPE (exp);
1870 tree adr;
1871 tree restype = TREE_TYPE (type);
1872 tree ptrtype;
1873
1874 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1875
1876 STRIP_TYPE_NOPS (exp);
1877
1878 if (TREE_NO_WARNING (orig_exp))
1879 TREE_NO_WARNING (exp) = 1;
1880
1881 ptrtype = build_pointer_type (restype);
1882
1883 if (INDIRECT_REF_P (exp))
1884 return convert (ptrtype, TREE_OPERAND (exp, 0));
1885
1886 /* In C++ array compound literals are temporary objects unless they are
1887 const or appear in namespace scope, so they are destroyed too soon
1888 to use them for much of anything (c++/53220). */
1889 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1890 {
1891 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1892 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1893 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1894 "converting an array compound literal to a pointer "
1895 "is ill-formed in C++");
1896 }
1897
1898 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1899 return convert (ptrtype, adr);
1900}
1901
1902/* Convert the function expression EXP to a pointer. */
1903static tree
1904function_to_pointer_conversion (location_t loc, tree exp)
1905{
1906 tree orig_exp = exp;
1907
1908 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1909
1910 STRIP_TYPE_NOPS (exp);
1911
1912 if (TREE_NO_WARNING (orig_exp))
1913 TREE_NO_WARNING (exp) = 1;
1914
1915 return build_unary_op (loc, ADDR_EXPR, exp, false);
1916}
1917
1918/* Mark EXP as read, not just set, for set but not used -Wunused
1919 warning purposes. */
1920
1921void
1922mark_exp_read (tree exp)
1923{
1924 switch (TREE_CODE (exp))
1925 {
1926 case VAR_DECL:
1927 case PARM_DECL:
1928 DECL_READ_P (exp) = 1;
1929 break;
1930 case ARRAY_REF:
1931 case COMPONENT_REF:
1932 case MODIFY_EXPR:
1933 case REALPART_EXPR:
1934 case IMAGPART_EXPR:
1935 CASE_CONVERT:
1936 case ADDR_EXPR:
1937 case VIEW_CONVERT_EXPR:
1938 mark_exp_read (TREE_OPERAND (exp, 0));
1939 break;
1940 case COMPOUND_EXPR:
1941 case C_MAYBE_CONST_EXPR:
1942 mark_exp_read (TREE_OPERAND (exp, 1));
1943 break;
1944 default:
1945 break;
1946 }
1947}
1948
1949/* Perform the default conversion of arrays and functions to pointers.
1950 Return the result of converting EXP. For any other expression, just
1951 return EXP.
1952
1953 LOC is the location of the expression. */
1954
1955struct c_expr
1956default_function_array_conversion (location_t loc, struct c_expr exp)
1957{
1958 tree orig_exp = exp.value;
1959 tree type = TREE_TYPE (exp.value);
1960 enum tree_code code = TREE_CODE (type);
1961
1962 switch (code)
1963 {
1964 case ARRAY_TYPE:
1965 {
1966 bool not_lvalue = false;
1967 bool lvalue_array_p;
1968
1969 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1970 || CONVERT_EXPR_P (exp.value))
1971 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1972 {
1973 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1974 not_lvalue = true;
1975 exp.value = TREE_OPERAND (exp.value, 0);
1976 }
1977
1978 if (TREE_NO_WARNING (orig_exp))
1979 TREE_NO_WARNING (exp.value) = 1;
1980
1981 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1982 if (!flag_isoc99 && !lvalue_array_p)
1983 {
1984 /* Before C99, non-lvalue arrays do not decay to pointers.
1985 Normally, using such an array would be invalid; but it can
1986 be used correctly inside sizeof or as a statement expression.
1987 Thus, do not give an error here; an error will result later. */
1988 return exp;
1989 }
1990
1991 exp.value = array_to_pointer_conversion (loc, exp.value);
1992 }
1993 break;
1994 case FUNCTION_TYPE:
1995 exp.value = function_to_pointer_conversion (loc, exp.value);
1996 break;
1997 default:
1998 break;
1999 }
2000
2001 return exp;
2002}
2003
2004struct c_expr
2005default_function_array_read_conversion (location_t loc, struct c_expr exp)
2006{
2007 mark_exp_read (exp.value);
2008 return default_function_array_conversion (loc, exp);
2009}
2010
2011/* Return whether EXPR should be treated as an atomic lvalue for the
2012 purposes of load and store handling. */
2013
2014static bool
2015really_atomic_lvalue (tree expr)
2016{
2017 if (error_operand_p (expr))
2018 return false;
2019 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2020 return false;
2021 if (!lvalue_p (expr))
2022 return false;
2023
2024 /* Ignore _Atomic on register variables, since their addresses can't
2025 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2026 sequences wouldn't work. Ignore _Atomic on structures containing
2027 bit-fields, since accessing elements of atomic structures or
2028 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2029 it's undefined at translation time or execution time, and the
2030 normal atomic sequences again wouldn't work. */
2031 while (handled_component_p (expr))
2032 {
2033 if (TREE_CODE (expr) == COMPONENT_REF
2034 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2035 return false;
2036 expr = TREE_OPERAND (expr, 0);
2037 }
2038 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2039 return false;
2040 return true;
2041}
2042
2043/* Convert expression EXP (location LOC) from lvalue to rvalue,
2044 including converting functions and arrays to pointers if CONVERT_P.
2045 If READ_P, also mark the expression as having been read. */
2046
2047struct c_expr
2048convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2049 bool convert_p, bool read_p)
2050{
2051 if (read_p)
2052 mark_exp_read (exp.value);
2053 if (convert_p)
2054 exp = default_function_array_conversion (loc, exp);
2055 if (really_atomic_lvalue (exp.value))
2056 {
2057 vec<tree, va_gc> *params;
2058 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2059 tree expr_type = TREE_TYPE (exp.value);
2060 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2061 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2062
2063 gcc_assert (TYPE_ATOMIC (expr_type));
2064
2065 /* Expansion of a generic atomic load may require an addition
2066 element, so allocate enough to prevent a resize. */
2067 vec_alloc (params, 4);
2068
2069 /* Remove the qualifiers for the rest of the expressions and
2070 create the VAL temp variable to hold the RHS. */
2071 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2072 tmp = create_tmp_var_raw (nonatomic_type);
2073 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2074 TREE_ADDRESSABLE (tmp) = 1;
2075 TREE_NO_WARNING (tmp) = 1;
2076
2077 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2078 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2079 params->quick_push (expr_addr);
2080 params->quick_push (tmp_addr);
2081 params->quick_push (seq_cst);
2082 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2083
2084 /* EXPR is always read. */
2085 mark_exp_read (exp.value);
2086
2087 /* Return tmp which contains the value loaded. */
2088 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2089 NULL_TREE, NULL_TREE);
2090 }
2091 return exp;
2092}
2093
2094/* EXP is an expression of integer type. Apply the integer promotions
2095 to it and return the promoted value. */
2096
2097tree
2098perform_integral_promotions (tree exp)
2099{
2100 tree type = TREE_TYPE (exp);
2101 enum tree_code code = TREE_CODE (type);
2102
2103 gcc_assert (INTEGRAL_TYPE_P (type));
2104
2105 /* Normally convert enums to int,
2106 but convert wide enums to something wider. */
2107 if (code == ENUMERAL_TYPE)
2108 {
2109 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2110 TYPE_PRECISION (integer_type_node)),
2111 ((TYPE_PRECISION (type)
2112 >= TYPE_PRECISION (integer_type_node))
2113 && TYPE_UNSIGNED (type)));
2114
2115 return convert (type, exp);
2116 }
2117
2118 /* ??? This should no longer be needed now bit-fields have their
2119 proper types. */
2120 if (TREE_CODE (exp) == COMPONENT_REF
2121 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2122 /* If it's thinner than an int, promote it like a
2123 c_promoting_integer_type_p, otherwise leave it alone. */
2124 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2125 TYPE_PRECISION (integer_type_node)))
2126 return convert (integer_type_node, exp);
2127
2128 if (c_promoting_integer_type_p (type))
2129 {
2130 /* Preserve unsignedness if not really getting any wider. */
2131 if (TYPE_UNSIGNED (type)
2132 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2133 return convert (unsigned_type_node, exp);
2134
2135 return convert (integer_type_node, exp);
2136 }
2137
2138 return exp;
2139}
2140
2141
2142/* Perform default promotions for C data used in expressions.
2143 Enumeral types or short or char are converted to int.
2144 In addition, manifest constants symbols are replaced by their values. */
2145
2146tree
2147default_conversion (tree exp)
2148{
2149 tree orig_exp;
2150 tree type = TREE_TYPE (exp);
2151 enum tree_code code = TREE_CODE (type);
2152 tree promoted_type;
2153
2154 mark_exp_read (exp);
2155
2156 /* Functions and arrays have been converted during parsing. */
2157 gcc_assert (code != FUNCTION_TYPE);
2158 if (code == ARRAY_TYPE)
2159 return exp;
2160
2161 /* Constants can be used directly unless they're not loadable. */
2162 if (TREE_CODE (exp) == CONST_DECL)
2163 exp = DECL_INITIAL (exp);
2164
2165 /* Strip no-op conversions. */
2166 orig_exp = exp;
2167 STRIP_TYPE_NOPS (exp);
2168
2169 if (TREE_NO_WARNING (orig_exp))
2170 TREE_NO_WARNING (exp) = 1;
2171
2172 if (code == VOID_TYPE)
2173 {
2174 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2175 "void value not ignored as it ought to be");
2176 return error_mark_node;
2177 }
2178
2179 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2180 if (exp == error_mark_node)
2181 return error_mark_node;
2182
2183 promoted_type = targetm.promoted_type (type);
2184 if (promoted_type)
2185 return convert (promoted_type, exp);
2186
2187 if (INTEGRAL_TYPE_P (type))
2188 return perform_integral_promotions (exp);
2189
2190 return exp;
2191}
2192
2193/* Look up COMPONENT in a structure or union TYPE.
2194
2195 If the component name is not found, returns NULL_TREE. Otherwise,
2196 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2197 stepping down the chain to the component, which is in the last
2198 TREE_VALUE of the list. Normally the list is of length one, but if
2199 the component is embedded within (nested) anonymous structures or
2200 unions, the list steps down the chain to the component. */
2201
2202static tree
2203lookup_field (tree type, tree component)
2204{
2205 tree field;
2206
2207 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2208 to the field elements. Use a binary search on this array to quickly
2209 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2210 will always be set for structures which have many elements. */
2211
2212 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2213 {
2214 int bot, top, half;
2215 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2216
2217 field = TYPE_FIELDS (type);
2218 bot = 0;
2219 top = TYPE_LANG_SPECIFIC (type)->s->len;
2220 while (top - bot > 1)
2221 {
2222 half = (top - bot + 1) >> 1;
2223 field = field_array[bot+half];
2224
2225 if (DECL_NAME (field) == NULL_TREE)
2226 {
2227 /* Step through all anon unions in linear fashion. */
2228 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2229 {
2230 field = field_array[bot++];
2231 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2232 {
2233 tree anon = lookup_field (TREE_TYPE (field), component);
2234
2235 if (anon)
2236 return tree_cons (NULL_TREE, field, anon);
2237
2238 /* The Plan 9 compiler permits referring
2239 directly to an anonymous struct/union field
2240 using a typedef name. */
2241 if (flag_plan9_extensions
2242 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2243 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2244 == TYPE_DECL)
2245 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2246 == component))
2247 break;
2248 }
2249 }
2250
2251 /* Entire record is only anon unions. */
2252 if (bot > top)
2253 return NULL_TREE;
2254
2255 /* Restart the binary search, with new lower bound. */
2256 continue;
2257 }
2258
2259 if (DECL_NAME (field) == component)
2260 break;
2261 if (DECL_NAME (field) < component)
2262 bot += half;
2263 else
2264 top = bot + half;
2265 }
2266
2267 if (DECL_NAME (field_array[bot]) == component)
2268 field = field_array[bot];
2269 else if (DECL_NAME (field) != component)
2270 return NULL_TREE;
2271 }
2272 else
2273 {
2274 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2275 {
2276 if (DECL_NAME (field) == NULL_TREE
2277 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2278 {
2279 tree anon = lookup_field (TREE_TYPE (field), component);
2280
2281 if (anon)
2282 return tree_cons (NULL_TREE, field, anon);
2283
2284 /* The Plan 9 compiler permits referring directly to an
2285 anonymous struct/union field using a typedef
2286 name. */
2287 if (flag_plan9_extensions
2288 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2289 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2290 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2291 == component))
2292 break;
2293 }
2294
2295 if (DECL_NAME (field) == component)
2296 break;
2297 }
2298
2299 if (field == NULL_TREE)
2300 return NULL_TREE;
2301 }
2302
2303 return tree_cons (NULL_TREE, field, NULL_TREE);
2304}
2305
2306/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2307
2308static void
2309lookup_field_fuzzy_find_candidates (tree type, tree component,
2310 vec<tree> *candidates)
2311{
2312 tree field;
2313 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2314 {
2315 if (DECL_NAME (field) == NULL_TREE
2316 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2317 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2318 candidates);
2319
2320 if (DECL_NAME (field))
2321 candidates->safe_push (DECL_NAME (field));
2322 }
2323}
2324
2325/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2326 rather than returning a TREE_LIST for an exact match. */
2327
2328static tree
2329lookup_field_fuzzy (tree type, tree component)
2330{
2331 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2332
2333 /* First, gather a list of candidates. */
2334 auto_vec <tree> candidates;
2335
2336 lookup_field_fuzzy_find_candidates (type, component,
2337 &candidates);
2338
2339 return find_closest_identifier (component, &candidates);
2340}
2341
2342/* Support function for build_component_ref's error-handling.
2343
2344 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2345 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2346
2347static bool
2348should_suggest_deref_p (tree datum_type)
2349{
2350 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2351 allows "." for ptrs; we could be handling a failed attempt
2352 to access a property. */
2353 if (c_dialect_objc ())
2354 return false;
2355
2356 /* Only suggest it for pointers... */
2357 if (TREE_CODE (datum_type) != POINTER_TYPE)
2358 return false;
2359
2360 /* ...to structs/unions. */
2361 tree underlying_type = TREE_TYPE (datum_type);
2362 enum tree_code code = TREE_CODE (underlying_type);
2363 if (code == RECORD_TYPE || code == UNION_TYPE)
2364 return true;
2365 else
2366 return false;
2367}
2368
2369/* Make an expression to refer to the COMPONENT field of structure or
2370 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2371 location of the COMPONENT_REF. COMPONENT_LOC is the location
2372 of COMPONENT. */
2373
2374tree
2375build_component_ref (location_t loc, tree datum, tree component,
2376 location_t component_loc)
2377{
2378 tree type = TREE_TYPE (datum);
2379 enum tree_code code = TREE_CODE (type);
2380 tree field = NULL;
2381 tree ref;
2382 bool datum_lvalue = lvalue_p (datum);
2383
2384 if (!objc_is_public (datum, component))
2385 return error_mark_node;
2386
2387 /* Detect Objective-C property syntax object.property. */
2388 if (c_dialect_objc ()
2389 && (ref = objc_maybe_build_component_ref (datum, component)))
2390 return ref;
2391
2392 /* See if there is a field or component with name COMPONENT. */
2393
2394 if (code == RECORD_TYPE || code == UNION_TYPE)
2395 {
2396 if (!COMPLETE_TYPE_P (type))
2397 {
2398 c_incomplete_type_error (loc, NULL_TREE, type);
2399 return error_mark_node;
2400 }
2401
2402 field = lookup_field (type, component);
2403
2404 if (!field)
2405 {
2406 tree guessed_id = lookup_field_fuzzy (type, component);
2407 if (guessed_id)
2408 {
2409 /* Attempt to provide a fixit replacement hint, if
2410 we have a valid range for the component. */
2411 location_t reported_loc
2412 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2413 gcc_rich_location rich_loc (reported_loc);
2414 if (component_loc != UNKNOWN_LOCATION)
2415 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2416 error_at (&rich_loc,
2417 "%qT has no member named %qE; did you mean %qE?",
2418 type, component, guessed_id);
2419 }
2420 else
2421 error_at (loc, "%qT has no member named %qE", type, component);
2422 return error_mark_node;
2423 }
2424
2425 /* Accessing elements of atomic structures or unions is undefined
2426 behavior (C11 6.5.2.3#5). */
2427 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2428 {
2429 if (code == RECORD_TYPE)
2430 warning_at (loc, 0, "accessing a member %qE of an atomic "
2431 "structure %qE", component, datum);
2432 else
2433 warning_at (loc, 0, "accessing a member %qE of an atomic "
2434 "union %qE", component, datum);
2435 }
2436
2437 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2438 This might be better solved in future the way the C++ front
2439 end does it - by giving the anonymous entities each a
2440 separate name and type, and then have build_component_ref
2441 recursively call itself. We can't do that here. */
2442 do
2443 {
2444 tree subdatum = TREE_VALUE (field);
2445 int quals;
2446 tree subtype;
2447 bool use_datum_quals;
2448
2449 if (TREE_TYPE (subdatum) == error_mark_node)
2450 return error_mark_node;
2451
2452 /* If this is an rvalue, it does not have qualifiers in C
2453 standard terms and we must avoid propagating such
2454 qualifiers down to a non-lvalue array that is then
2455 converted to a pointer. */
2456 use_datum_quals = (datum_lvalue
2457 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2458
2459 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2460 if (use_datum_quals)
2461 quals |= TYPE_QUALS (TREE_TYPE (datum));
2462 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2463
2464 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2465 NULL_TREE);
2466 SET_EXPR_LOCATION (ref, loc);
2467 if (TREE_READONLY (subdatum)
2468 || (use_datum_quals && TREE_READONLY (datum)))
2469 TREE_READONLY (ref) = 1;
2470 if (TREE_THIS_VOLATILE (subdatum)
2471 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2472 TREE_THIS_VOLATILE (ref) = 1;
2473
2474 if (TREE_DEPRECATED (subdatum))
2475 warn_deprecated_use (subdatum, NULL_TREE);
2476
2477 datum = ref;
2478
2479 field = TREE_CHAIN (field);
2480 }
2481 while (field);
2482
2483 return ref;
2484 }
2485 else if (should_suggest_deref_p (type))
2486 {
2487 /* Special-case the error message for "ptr.field" for the case
2488 where the user has confused "." vs "->". */
2489 rich_location richloc (line_table, loc);
2490 /* "loc" should be the "." token. */
2491 richloc.add_fixit_replace ("->");
2492 error_at (&richloc,
2493 "%qE is a pointer; did you mean to use %<->%>?",
2494 datum);
2495 return error_mark_node;
2496 }
2497 else if (code != ERROR_MARK)
2498 error_at (loc,
2499 "request for member %qE in something not a structure or union",
2500 component);
2501
2502 return error_mark_node;
2503}
2504
2505/* Given an expression PTR for a pointer, return an expression
2506 for the value pointed to.
2507 ERRORSTRING is the name of the operator to appear in error messages.
2508
2509 LOC is the location to use for the generated tree. */
2510
2511tree
2512build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2513{
2514 tree pointer = default_conversion (ptr);
2515 tree type = TREE_TYPE (pointer);
2516 tree ref;
2517
2518 if (TREE_CODE (type) == POINTER_TYPE)
2519 {
2520 if (CONVERT_EXPR_P (pointer)
2521 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2522 {
2523 /* If a warning is issued, mark it to avoid duplicates from
2524 the backend. This only needs to be done at
2525 warn_strict_aliasing > 2. */
2526 if (warn_strict_aliasing > 2)
2527 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2528 type, TREE_OPERAND (pointer, 0)))
2529 TREE_NO_WARNING (pointer) = 1;
2530 }
2531
2532 if (TREE_CODE (pointer) == ADDR_EXPR
2533 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2534 == TREE_TYPE (type)))
2535 {
2536 ref = TREE_OPERAND (pointer, 0);
2537 protected_set_expr_location (ref, loc);
2538 return ref;
2539 }
2540 else
2541 {
2542 tree t = TREE_TYPE (type);
2543
2544 ref = build1 (INDIRECT_REF, t, pointer);
2545
2546 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2547 {
2548 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2549 {
2550 error_at (loc, "dereferencing pointer to incomplete type "
2551 "%qT", t);
2552 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2553 }
2554 return error_mark_node;
2555 }
2556 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2557 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2558
2559 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2560 so that we get the proper error message if the result is used
2561 to assign to. Also, &* is supposed to be a no-op.
2562 And ANSI C seems to specify that the type of the result
2563 should be the const type. */
2564 /* A de-reference of a pointer to const is not a const. It is valid
2565 to change it via some other pointer. */
2566 TREE_READONLY (ref) = TYPE_READONLY (t);
2567 TREE_SIDE_EFFECTS (ref)
2568 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2569 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2570 protected_set_expr_location (ref, loc);
2571 return ref;
2572 }
2573 }
2574 else if (TREE_CODE (pointer) != ERROR_MARK)
2575 invalid_indirection_error (loc, type, errstring);
2576
2577 return error_mark_node;
2578}
2579
2580/* This handles expressions of the form "a[i]", which denotes
2581 an array reference.
2582
2583 This is logically equivalent in C to *(a+i), but we may do it differently.
2584 If A is a variable or a member, we generate a primitive ARRAY_REF.
2585 This avoids forcing the array out of registers, and can work on
2586 arrays that are not lvalues (for example, members of structures returned
2587 by functions).
2588
2589 For vector types, allow vector[i] but not i[vector], and create
2590 *(((type*)&vectortype) + i) for the expression.
2591
2592 LOC is the location to use for the returned expression. */
2593
2594tree
2595build_array_ref (location_t loc, tree array, tree index)
2596{
2597 tree ret;
2598 bool swapped = false;
2599 if (TREE_TYPE (array) == error_mark_node
2600 || TREE_TYPE (index) == error_mark_node)
2601 return error_mark_node;
2602
2603 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2604 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2605 /* Allow vector[index] but not index[vector]. */
2606 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2607 {
2608 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2610 {
2611 error_at (loc,
2612 "subscripted value is neither array nor pointer nor vector");
2613
2614 return error_mark_node;
2615 }
2616 std::swap (array, index);
2617 swapped = true;
2618 }
2619
2620 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2621 {
2622 error_at (loc, "array subscript is not an integer");
2623 return error_mark_node;
2624 }
2625
2626 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2627 {
2628 error_at (loc, "subscripted value is pointer to function");
2629 return error_mark_node;
2630 }
2631
2632 /* ??? Existing practice has been to warn only when the char
2633 index is syntactically the index, not for char[array]. */
2634 if (!swapped)
2635 warn_array_subscript_with_type_char (loc, index);
2636
2637 /* Apply default promotions *after* noticing character types. */
2638 index = default_conversion (index);
2639 if (index == error_mark_node)
2640 return error_mark_node;
2641
2642 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2643
2644 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2645 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2646
2647 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2648 {
2649 tree rval, type;
2650
2651 /* An array that is indexed by a non-constant
2652 cannot be stored in a register; we must be able to do
2653 address arithmetic on its address.
2654 Likewise an array of elements of variable size. */
2655 if (TREE_CODE (index) != INTEGER_CST
2656 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2657 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2658 {
2659 if (!c_mark_addressable (array, true))
2660 return error_mark_node;
2661 }
2662 /* An array that is indexed by a constant value which is not within
2663 the array bounds cannot be stored in a register either; because we
2664 would get a crash in store_bit_field/extract_bit_field when trying
2665 to access a non-existent part of the register. */
2666 if (TREE_CODE (index) == INTEGER_CST
2667 && TYPE_DOMAIN (TREE_TYPE (array))
2668 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2669 {
2670 if (!c_mark_addressable (array))
2671 return error_mark_node;
2672 }
2673
2674 if ((pedantic || warn_c90_c99_compat)
2675 && ! was_vector)
2676 {
2677 tree foo = array;
2678 while (TREE_CODE (foo) == COMPONENT_REF)
2679 foo = TREE_OPERAND (foo, 0);
2680 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2681 pedwarn (loc, OPT_Wpedantic,
2682 "ISO C forbids subscripting %<register%> array");
2683 else if (!lvalue_p (foo))
2684 pedwarn_c90 (loc, OPT_Wpedantic,
2685 "ISO C90 forbids subscripting non-lvalue "
2686 "array");
2687 }
2688
2689 type = TREE_TYPE (TREE_TYPE (array));
2690 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2691 /* Array ref is const/volatile if the array elements are
2692 or if the array is. */
2693 TREE_READONLY (rval)
2694 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2695 | TREE_READONLY (array));
2696 TREE_SIDE_EFFECTS (rval)
2697 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2698 | TREE_SIDE_EFFECTS (array));
2699 TREE_THIS_VOLATILE (rval)
2700 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2701 /* This was added by rms on 16 Nov 91.
2702 It fixes vol struct foo *a; a->elts[1]
2703 in an inline function.
2704 Hope it doesn't break something else. */
2705 | TREE_THIS_VOLATILE (array));
2706 ret = require_complete_type (loc, rval);
2707 protected_set_expr_location (ret, loc);
2708 if (non_lvalue)
2709 ret = non_lvalue_loc (loc, ret);
2710 return ret;
2711 }
2712 else
2713 {
2714 tree ar = default_conversion (array);
2715
2716 if (ar == error_mark_node)
2717 return ar;
2718
2719 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2720 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2721
2722 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2723 index, false),
2724 RO_ARRAY_INDEXING);
2725 if (non_lvalue)
2726 ret = non_lvalue_loc (loc, ret);
2727 return ret;
2728 }
2729}
2730
2731/* Build an external reference to identifier ID. FUN indicates
2732 whether this will be used for a function call. LOC is the source
2733 location of the identifier. This sets *TYPE to the type of the
2734 identifier, which is not the same as the type of the returned value
2735 for CONST_DECLs defined as enum constants. If the type of the
2736 identifier is not available, *TYPE is set to NULL. */
2737tree
2738build_external_ref (location_t loc, tree id, bool fun, tree *type)
2739{
2740 tree ref;
2741 tree decl = lookup_name (id);
2742
2743 /* In Objective-C, an instance variable (ivar) may be preferred to
2744 whatever lookup_name() found. */
2745 decl = objc_lookup_ivar (decl, id);
2746
2747 *type = NULL;
2748 if (decl && decl != error_mark_node)
2749 {
2750 ref = decl;
2751 *type = TREE_TYPE (ref);
2752 }
2753 else if (fun)
2754 /* Implicit function declaration. */
2755 ref = implicitly_declare (loc, id);
2756 else if (decl == error_mark_node)
2757 /* Don't complain about something that's already been
2758 complained about. */
2759 return error_mark_node;
2760 else
2761 {
2762 undeclared_variable (loc, id);
2763 return error_mark_node;
2764 }
2765
2766 if (TREE_TYPE (ref) == error_mark_node)
2767 return error_mark_node;
2768
2769 if (TREE_DEPRECATED (ref))
2770 warn_deprecated_use (ref, NULL_TREE);
2771
2772 /* Recursive call does not count as usage. */
2773 if (ref != current_function_decl)
2774 {
2775 TREE_USED (ref) = 1;
2776 }
2777
2778 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2779 {
2780 if (!in_sizeof && !in_typeof)
2781 C_DECL_USED (ref) = 1;
2782 else if (DECL_INITIAL (ref) == NULL_TREE
2783 && DECL_EXTERNAL (ref)
2784 && !TREE_PUBLIC (ref))
2785 record_maybe_used_decl (ref);
2786 }
2787
2788 if (TREE_CODE (ref) == CONST_DECL)
2789 {
2790 used_types_insert (TREE_TYPE (ref));
2791
2792 if (warn_cxx_compat
2793 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2794 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2795 {
2796 warning_at (loc, OPT_Wc___compat,
2797 ("enum constant defined in struct or union "
2798 "is not visible in C++"));
2799 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2800 }
2801
2802 ref = DECL_INITIAL (ref);
2803 TREE_CONSTANT (ref) = 1;
2804 }
2805 else if (current_function_decl != NULL_TREE
2806 && !DECL_FILE_SCOPE_P (current_function_decl)
2807 && (VAR_OR_FUNCTION_DECL_P (ref)
2808 || TREE_CODE (ref) == PARM_DECL))
2809 {
2810 tree context = decl_function_context (ref);
2811
2812 if (context != NULL_TREE && context != current_function_decl)
2813 DECL_NONLOCAL (ref) = 1;
2814 }
2815 /* C99 6.7.4p3: An inline definition of a function with external
2816 linkage ... shall not contain a reference to an identifier with
2817 internal linkage. */
2818 else if (current_function_decl != NULL_TREE
2819 && DECL_DECLARED_INLINE_P (current_function_decl)
2820 && DECL_EXTERNAL (current_function_decl)
2821 && VAR_OR_FUNCTION_DECL_P (ref)
2822 && (!VAR_P (ref) || TREE_STATIC (ref))
2823 && ! TREE_PUBLIC (ref)
2824 && DECL_CONTEXT (ref) != current_function_decl)
2825 record_inline_static (loc, current_function_decl, ref,
2826 csi_internal);
2827
2828 return ref;
2829}
2830
2831/* Record details of decls possibly used inside sizeof or typeof. */
2832struct maybe_used_decl
2833{
2834 /* The decl. */
2835 tree decl;
2836 /* The level seen at (in_sizeof + in_typeof). */
2837 int level;
2838 /* The next one at this level or above, or NULL. */
2839 struct maybe_used_decl *next;
2840};
2841
2842static struct maybe_used_decl *maybe_used_decls;
2843
2844/* Record that DECL, an undefined static function reference seen
2845 inside sizeof or typeof, might be used if the operand of sizeof is
2846 a VLA type or the operand of typeof is a variably modified
2847 type. */
2848
2849static void
2850record_maybe_used_decl (tree decl)
2851{
2852 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2853 t->decl = decl;
2854 t->level = in_sizeof + in_typeof;
2855 t->next = maybe_used_decls;
2856 maybe_used_decls = t;
2857}
2858
2859/* Pop the stack of decls possibly used inside sizeof or typeof. If
2860 USED is false, just discard them. If it is true, mark them used
2861 (if no longer inside sizeof or typeof) or move them to the next
2862 level up (if still inside sizeof or typeof). */
2863
2864void
2865pop_maybe_used (bool used)
2866{
2867 struct maybe_used_decl *p = maybe_used_decls;
2868 int cur_level = in_sizeof + in_typeof;
2869 while (p && p->level > cur_level)
2870 {
2871 if (used)
2872 {
2873 if (cur_level == 0)
2874 C_DECL_USED (p->decl) = 1;
2875 else
2876 p->level = cur_level;
2877 }
2878 p = p->next;
2879 }
2880 if (!used || cur_level == 0)
2881 maybe_used_decls = p;
2882}
2883
2884/* Return the result of sizeof applied to EXPR. */
2885
2886struct c_expr
2887c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2888{
2889 struct c_expr ret;
2890 if (expr.value == error_mark_node)
2891 {
2892 ret.value = error_mark_node;
2893 ret.original_code = ERROR_MARK;
2894 ret.original_type = NULL;
2895 pop_maybe_used (false);
2896 }
2897 else
2898 {
2899 bool expr_const_operands = true;
2900
2901 if (TREE_CODE (expr.value) == PARM_DECL
2902 && C_ARRAY_PARAMETER (expr.value))
2903 {
2904 if (warning_at (loc, OPT_Wsizeof_array_argument,
2905 "%<sizeof%> on array function parameter %qE will "
2906 "return size of %qT", expr.value,
2907 TREE_TYPE (expr.value)))
2908 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2909 }
2910 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2911 &expr_const_operands);
2912 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2913 c_last_sizeof_arg = expr.value;
2914 c_last_sizeof_loc = loc;
2915 ret.original_code = SIZEOF_EXPR;
2916 ret.original_type = NULL;
2917 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2918 {
2919 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2920 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2921 folded_expr, ret.value);
2922 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2923 SET_EXPR_LOCATION (ret.value, loc);
2924 }
2925 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2926 }
2927 return ret;
2928}
2929
2930/* Return the result of sizeof applied to T, a structure for the type
2931 name passed to sizeof (rather than the type itself). LOC is the
2932 location of the original expression. */
2933
2934struct c_expr
2935c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2936{
2937 tree type;
2938 struct c_expr ret;
2939 tree type_expr = NULL_TREE;
2940 bool type_expr_const = true;
2941 type = groktypename (t, &type_expr, &type_expr_const);
2942 ret.value = c_sizeof (loc, type);
2943 c_last_sizeof_arg = type;
2944 c_last_sizeof_loc = loc;
2945 ret.original_code = SIZEOF_EXPR;
2946 ret.original_type = NULL;
2947 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2948 && c_vla_type_p (type))
2949 {
2950 /* If the type is a [*] array, it is a VLA but is represented as
2951 having a size of zero. In such a case we must ensure that
2952 the result of sizeof does not get folded to a constant by
2953 c_fully_fold, because if the size is evaluated the result is
2954 not constant and so constraints on zero or negative size
2955 arrays must not be applied when this sizeof call is inside
2956 another array declarator. */
2957 if (!type_expr)
2958 type_expr = integer_zero_node;
2959 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2960 type_expr, ret.value);
2961 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2962 }
2963 pop_maybe_used (type != error_mark_node
2964 ? C_TYPE_VARIABLE_SIZE (type) : false);
2965 return ret;
2966}
2967
2968/* Build a function call to function FUNCTION with parameters PARAMS.
2969 The function call is at LOC.
2970 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2971 TREE_VALUE of each node is a parameter-expression.
2972 FUNCTION's data type may be a function type or a pointer-to-function. */
2973
2974tree
2975build_function_call (location_t loc, tree function, tree params)
2976{
2977 vec<tree, va_gc> *v;
2978 tree ret;
2979
2980 vec_alloc (v, list_length (params));
2981 for (; params; params = TREE_CHAIN (params))
2982 v->quick_push (TREE_VALUE (params));
2983 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2984 vec_free (v);
2985 return ret;
2986}
2987
2988/* Give a note about the location of the declaration of DECL. */
2989
2990static void
2991inform_declaration (tree decl)
2992{
2993 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2994 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2995}
2996
2997/* Build a function call to function FUNCTION with parameters PARAMS.
2998 ORIGTYPES, if not NULL, is a vector of types; each element is
2999 either NULL or the original type of the corresponding element in
3000 PARAMS. The original type may differ from TREE_TYPE of the
3001 parameter for enums. FUNCTION's data type may be a function type
3002 or pointer-to-function. This function changes the elements of
3003 PARAMS. */
3004
3005tree
3006build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3007 tree function, vec<tree, va_gc> *params,
3008 vec<tree, va_gc> *origtypes)
3009{
3010 tree fntype, fundecl = NULL_TREE;
3011 tree name = NULL_TREE, result;
3012 tree tem;
3013 int nargs;
3014 tree *argarray;
3015
3016
3017 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3018 STRIP_TYPE_NOPS (function);
3019
3020 /* Convert anything with function type to a pointer-to-function. */
3021 if (TREE_CODE (function) == FUNCTION_DECL)
3022 {
3023 name = DECL_NAME (function);
3024
3025 if (flag_tm)
3026 tm_malloc_replacement (function);
3027 fundecl = function;
3028 /* Atomic functions have type checking/casting already done. They are
3029 often rewritten and don't match the original parameter list. */
3030 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3031 origtypes = NULL;
3032 }
3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3034 function = function_to_pointer_conversion (loc, function);
3035
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
3040
3041 function = c_fully_fold (function, false, NULL);
3042
3043 fntype = TREE_TYPE (function);
3044
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3047
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3050 {
3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3056 {
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3061 }
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
3065 return error_mark_node;
3066 }
3067
3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3070
3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3073
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3076
3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
3079 if (nargs < 0)
3080 return error_mark_node;
3081
3082 /* Check that the function is called through a compatible prototype.
3083 If it is not, warn. */
3084 if (CONVERT_EXPR_P (function)
3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3087 && !comptypes (fntype, TREE_TYPE (tem)))
3088 {
3089 tree return_type = TREE_TYPE (fntype);
3090
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
3094 warning_at (loc, 0, "function called through a non-compatible type");
3095
3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3100 }
3101
3102 argarray = vec_safe_address (params);
3103
3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
3110 return error_mark_node;
3111
3112 /* Check that the arguments to the function are valid. */
3113 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3114 nargs, argarray, &arg_loc);
3115
3116 if (name != NULL_TREE
3117 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3118 {
3119 if (require_constant_value)
3120 result
3121 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3122 function, nargs, argarray);
3123 else
3124 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3125 function, nargs, argarray);
3126 if (TREE_CODE (result) == NOP_EXPR
3127 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3128 STRIP_TYPE_NOPS (result);
3129 }
3130 else
3131 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3132 function, nargs, argarray);
3133 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3134 later. */
3135 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3136 TREE_NO_WARNING (result) = 1;
3137
3138 /* In this improbable scenario, a nested function returns a VM type.
3139 Create a TARGET_EXPR so that the call always has a LHS, much as
3140 what the C++ FE does for functions returning non-PODs. */
3141 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3142 {
3143 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3144 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3145 NULL_TREE, NULL_TREE);
3146 }
3147
3148 if (VOID_TYPE_P (TREE_TYPE (result)))
3149 {
3150 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3151 pedwarn (loc, 0,
3152 "function with qualified void return type called");
3153 return result;
3154 }
3155 return require_complete_type (loc, result);
3156}
3157
3158/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3159
3160tree
3161c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3162 tree function, vec<tree, va_gc> *params,
3163 vec<tree, va_gc> *origtypes)
3164{
3165 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3166 STRIP_TYPE_NOPS (function);
3167
3168 /* Convert anything with function type to a pointer-to-function. */
3169 if (TREE_CODE (function) == FUNCTION_DECL)
3170 {
3171 /* Implement type-directed function overloading for builtins.
3172 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3173 handle all the type checking. The result is a complete expression
3174 that implements this function call. */
3175 tree tem = resolve_overloaded_builtin (loc, function, params);
3176 if (tem)
3177 return tem;
3178 }
3179 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3180}
3181
3182/* Convert the argument expressions in the vector VALUES
3183 to the types in the list TYPELIST.
3184
3185 If TYPELIST is exhausted, or when an element has NULL as its type,
3186 perform the default conversions.
3187
3188 ORIGTYPES is the original types of the expressions in VALUES. This
3189 holds the type of enum values which have been converted to integral
3190 types. It may be NULL.
3191
3192 FUNCTION is a tree for the called function. It is used only for
3193 error messages, where it is formatted with %qE.
3194
3195 This is also where warnings about wrong number of args are generated.
3196
3197 ARG_LOC are locations of function arguments (if any).
3198
3199 Returns the actual number of arguments processed (which may be less
3200 than the length of VALUES in some error situations), or -1 on
3201 failure. */
3202
3203static int
3204convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3205 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3206 tree function, tree fundecl)
3207{
3208 tree typetail, val;
3209 unsigned int parmnum;
3210 bool error_args = false;
3211 const bool type_generic = fundecl
3212 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3213 bool type_generic_remove_excess_precision = false;
3214 bool type_generic_overflow_p = false;
3215 tree selector;
3216
3217 /* Change pointer to function to the function itself for
3218 diagnostics. */
3219 if (TREE_CODE (function) == ADDR_EXPR
3220 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3221 function = TREE_OPERAND (function, 0);
3222
3223 /* Handle an ObjC selector specially for diagnostics. */
3224 selector = objc_message_selector ();
3225
3226 /* For type-generic built-in functions, determine whether excess
3227 precision should be removed (classification) or not
3228 (comparison). */
3229 if (type_generic
3230 && DECL_BUILT_IN (fundecl)
3231 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3232 {
3233 switch (DECL_FUNCTION_CODE (fundecl))
3234 {
3235 case BUILT_IN_ISFINITE:
3236 case BUILT_IN_ISINF:
3237 case BUILT_IN_ISINF_SIGN:
3238 case BUILT_IN_ISNAN:
3239 case BUILT_IN_ISNORMAL:
3240 case BUILT_IN_FPCLASSIFY:
3241 type_generic_remove_excess_precision = true;
3242 break;
3243
3244 case BUILT_IN_ADD_OVERFLOW_P:
3245 case BUILT_IN_SUB_OVERFLOW_P:
3246 case BUILT_IN_MUL_OVERFLOW_P:
3247 /* The last argument of these type-generic builtins
3248 should not be promoted. */
3249 type_generic_overflow_p = true;
3250 break;
3251
3252 default:
3253 break;
3254 }
3255 }
3256
3257 /* Scan the given expressions and types, producing individual
3258 converted arguments. */
3259
3260 for (typetail = typelist, parmnum = 0;
3261 values && values->iterate (parmnum, &val);
3262 ++parmnum)
3263 {
3264 tree type = typetail ? TREE_VALUE (typetail) : 0;
3265 tree valtype = TREE_TYPE (val);
3266 tree rname = function;
3267 int argnum = parmnum + 1;
3268 const char *invalid_func_diag;
3269 bool excess_precision = false;
3270 bool npc;
3271 tree parmval;
3272 /* Some __atomic_* builtins have additional hidden argument at
3273 position 0. */
3274 location_t ploc
3275 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3276 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3277 : input_location;
3278
3279 if (type == void_type_node)
3280 {
3281 if (selector)
3282 error_at (loc, "too many arguments to method %qE", selector);
3283 else
3284 error_at (loc, "too many arguments to function %qE", function);
3285 inform_declaration (fundecl);
3286 return error_args ? -1 : (int) parmnum;
3287 }
3288
3289 if (selector && argnum > 2)
3290 {
3291 rname = selector;
3292 argnum -= 2;
3293 }
3294
3295 npc = null_pointer_constant_p (val);
3296
3297 /* If there is excess precision and a prototype, convert once to
3298 the required type rather than converting via the semantic
3299 type. Likewise without a prototype a float value represented
3300 as long double should be converted once to double. But for
3301 type-generic classification functions excess precision must
3302 be removed here. */
3303 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3304 && (type || !type_generic || !type_generic_remove_excess_precision))
3305 {
3306 val = TREE_OPERAND (val, 0);
3307 excess_precision = true;
3308 }
3309 val = c_fully_fold (val, false, NULL);
3310 STRIP_TYPE_NOPS (val);
3311
3312 val = require_complete_type (ploc, val);
3313
3314 /* Some floating-point arguments must be promoted to double when
3315 no type is specified by a prototype. This applies to
3316 arguments of type float, and to architecture-specific types
3317 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3318 bool promote_float_arg = false;
3319 if (type == NULL_TREE
3320 && TREE_CODE (valtype) == REAL_TYPE
3321 && (TYPE_PRECISION (valtype)
3322 <= TYPE_PRECISION (double_type_node))
3323 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3324 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3325 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3326 {
3327 /* Promote this argument, unless it has a _FloatN or
3328 _FloatNx type. */
3329 promote_float_arg = true;
3330 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3331 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3332 {
3333 promote_float_arg = false;
3334 break;
3335 }
3336 }
3337
3338 if (type != NULL_TREE)
3339 {
3340 /* Formal parm type is specified by a function prototype. */
3341
3342 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3343 {
3344 error_at (ploc, "type of formal parameter %d is incomplete",
3345 parmnum + 1);
3346 parmval = val;
3347 }
3348 else
3349 {
3350 tree origtype;
3351
3352 /* Optionally warn about conversions that
3353 differ from the default conversions. */
3354 if (warn_traditional_conversion || warn_traditional)
3355 {
3356 unsigned int formal_prec = TYPE_PRECISION (type);
3357
3358 if (INTEGRAL_TYPE_P (type)
3359 && TREE_CODE (valtype) == REAL_TYPE)
3360 warning_at (ploc, OPT_Wtraditional_conversion,
3361 "passing argument %d of %qE as integer rather "
3362 "than floating due to prototype",
3363 argnum, rname);
3364 if (INTEGRAL_TYPE_P (type)
3365 && TREE_CODE (valtype) == COMPLEX_TYPE)
3366 warning_at (ploc, OPT_Wtraditional_conversion,
3367 "passing argument %d of %qE as integer rather "
3368 "than complex due to prototype",
3369 argnum, rname);
3370 else if (TREE_CODE (type) == COMPLEX_TYPE
3371 && TREE_CODE (valtype) == REAL_TYPE)
3372 warning_at (ploc, OPT_Wtraditional_conversion,
3373 "passing argument %d of %qE as complex rather "
3374 "than floating due to prototype",
3375 argnum, rname);
3376 else if (TREE_CODE (type) == REAL_TYPE
3377 && INTEGRAL_TYPE_P (valtype))
3378 warning_at (ploc, OPT_Wtraditional_conversion,
3379 "passing argument %d of %qE as floating rather "
3380 "than integer due to prototype",
3381 argnum, rname);
3382 else if (TREE_CODE (type) == COMPLEX_TYPE
3383 && INTEGRAL_TYPE_P (valtype))
3384 warning_at (ploc, OPT_Wtraditional_conversion,
3385 "passing argument %d of %qE as complex rather "
3386 "than integer due to prototype",
3387 argnum, rname);
3388 else if (TREE_CODE (type) == REAL_TYPE
3389 && TREE_CODE (valtype) == COMPLEX_TYPE)
3390 warning_at (ploc, OPT_Wtraditional_conversion,
3391 "passing argument %d of %qE as floating rather "
3392 "than complex due to prototype",
3393 argnum, rname);
3394 /* ??? At some point, messages should be written about
3395 conversions between complex types, but that's too messy
3396 to do now. */
3397 else if (TREE_CODE (type) == REAL_TYPE
3398 && TREE_CODE (valtype) == REAL_TYPE)
3399 {
3400 /* Warn if any argument is passed as `float',
3401 since without a prototype it would be `double'. */
3402 if (formal_prec == TYPE_PRECISION (float_type_node)
3403 && type != dfloat32_type_node)
3404 warning_at (ploc, 0,
3405 "passing argument %d of %qE as %<float%> "
3406 "rather than %<double%> due to prototype",
3407 argnum, rname);
3408
3409 /* Warn if mismatch between argument and prototype
3410 for decimal float types. Warn of conversions with
3411 binary float types and of precision narrowing due to
3412 prototype. */
3413 else if (type != valtype
3414 && (type == dfloat32_type_node
3415 || type == dfloat64_type_node
3416 || type == dfloat128_type_node
3417 || valtype == dfloat32_type_node
3418 || valtype == dfloat64_type_node
3419 || valtype == dfloat128_type_node)
3420 && (formal_prec
3421 <= TYPE_PRECISION (valtype)
3422 || (type == dfloat128_type_node
3423 && (valtype
3424 != dfloat64_type_node
3425 && (valtype
3426 != dfloat32_type_node)))
3427 || (type == dfloat64_type_node
3428 && (valtype
3429 != dfloat32_type_node))))
3430 warning_at (ploc, 0,
3431 "passing argument %d of %qE as %qT "
3432 "rather than %qT due to prototype",
3433 argnum, rname, type, valtype);
3434
3435 }
3436 /* Detect integer changing in width or signedness.
3437 These warnings are only activated with
3438 -Wtraditional-conversion, not with -Wtraditional. */
3439 else if (warn_traditional_conversion
3440 && INTEGRAL_TYPE_P (type)
3441 && INTEGRAL_TYPE_P (valtype))
3442 {
3443 tree would_have_been = default_conversion (val);
3444 tree type1 = TREE_TYPE (would_have_been);
3445
3446 if (val == error_mark_node)
3447 /* VAL could have been of incomplete type. */;
3448 else if (TREE_CODE (type) == ENUMERAL_TYPE
3449 && (TYPE_MAIN_VARIANT (type)
3450 == TYPE_MAIN_VARIANT (valtype)))
3451 /* No warning if function asks for enum
3452 and the actual arg is that enum type. */
3453 ;
3454 else if (formal_prec != TYPE_PRECISION (type1))
3455 warning_at (ploc, OPT_Wtraditional_conversion,
3456 "passing argument %d of %qE "
3457 "with different width due to prototype",
3458 argnum, rname);
3459 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3460 ;
3461 /* Don't complain if the formal parameter type
3462 is an enum, because we can't tell now whether
3463 the value was an enum--even the same enum. */
3464 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3465 ;
3466 else if (TREE_CODE (val) == INTEGER_CST
3467 && int_fits_type_p (val, type))
3468 /* Change in signedness doesn't matter
3469 if a constant value is unaffected. */
3470 ;
3471 /* If the value is extended from a narrower
3472 unsigned type, it doesn't matter whether we
3473 pass it as signed or unsigned; the value
3474 certainly is the same either way. */
3475 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3476 && TYPE_UNSIGNED (valtype))
3477 ;
3478 else if (TYPE_UNSIGNED (type))
3479 warning_at (ploc, OPT_Wtraditional_conversion,
3480 "passing argument %d of %qE "
3481 "as unsigned due to prototype",
3482 argnum, rname);
3483 else
3484 warning_at (ploc, OPT_Wtraditional_conversion,
3485 "passing argument %d of %qE "
3486 "as signed due to prototype",
3487 argnum, rname);
3488 }
3489 }
3490
3491 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3492 sake of better warnings from convert_and_check. */
3493 if (excess_precision)
3494 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3495 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3496 parmval = convert_for_assignment (loc, ploc, type,
3497 val, origtype, ic_argpass,
3498 npc, fundecl, function,
3499 parmnum + 1);
3500
3501 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3502 && INTEGRAL_TYPE_P (type)
3503 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3504 parmval = default_conversion (parmval);
3505 }
3506 }
3507 else if (promote_float_arg)
3508 {
3509 if (type_generic)
3510 parmval = val;
3511 else
3512 {
3513 /* Convert `float' to `double'. */
3514 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3515 warning_at (ploc, OPT_Wdouble_promotion,
3516 "implicit conversion from %qT to %qT when passing "
3517 "argument to function",
3518 valtype, double_type_node);
3519 parmval = convert (double_type_node, val);
3520 }
3521 }
3522 else if ((excess_precision && !type_generic)
3523 || (type_generic_overflow_p && parmnum == 2))
3524 /* A "double" argument with excess precision being passed
3525 without a prototype or in variable arguments.
3526 The last argument of __builtin_*_overflow_p should not be
3527 promoted. */
3528 parmval = convert (valtype, val);
3529 else if ((invalid_func_diag =
3530 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3531 {
3532 error (invalid_func_diag);
3533 return -1;
3534 }
3535 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3536 {
3537 return -1;
3538 }
3539 else
3540 /* Convert `short' and `char' to full-size `int'. */
3541 parmval = default_conversion (val);
3542
3543 (*values)[parmnum] = parmval;
3544 if (parmval == error_mark_node)
3545 error_args = true;
3546
3547 if (typetail)
3548 typetail = TREE_CHAIN (typetail);
3549 }
3550
3551 gcc_assert (parmnum == vec_safe_length (values));
3552
3553 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3554 {
3555 error_at (loc, "too few arguments to function %qE", function);
3556 inform_declaration (fundecl);
3557 return -1;
3558 }
3559
3560 return error_args ? -1 : (int) parmnum;
3561}
3562
3563/* This is the entry point used by the parser to build unary operators
3564 in the input. CODE, a tree_code, specifies the unary operator, and
3565 ARG is the operand. For unary plus, the C parser currently uses
3566 CONVERT_EXPR for code.
3567
3568 LOC is the location to use for the tree generated.
3569*/
3570
3571struct c_expr
3572parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3573{
3574 struct c_expr result;
3575
3576 result.original_code = code;
3577 result.original_type = NULL;
3578
3579 if (reject_gcc_builtin (arg.value))
3580 {
3581 result.value = error_mark_node;
3582 }
3583 else
3584 {
3585 result.value = build_unary_op (loc, code, arg.value, false);
3586
3587 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3588 overflow_warning (loc, result.value, arg.value);
3589 }
3590
3591 /* We are typically called when parsing a prefix token at LOC acting on
3592 ARG. Reflect this by updating the source range of the result to
3593 start at LOC and end at the end of ARG. */
3594 set_c_expr_source_range (&result,
3595 loc, arg.get_finish ());
3596
3597 return result;
3598}
3599
3600/* Returns true if TYPE is a character type, *not* including wchar_t. */
3601
3602static bool
3603char_type_p (tree type)
3604{
3605 return (type == char_type_node
3606 || type == unsigned_char_type_node
3607 || type == signed_char_type_node
3608 || type == char16_type_node
3609 || type == char32_type_node);
3610}
3611
3612/* This is the entry point used by the parser to build binary operators
3613 in the input. CODE, a tree_code, specifies the binary operator, and
3614 ARG1 and ARG2 are the operands. In addition to constructing the
3615 expression, we check for operands that were written with other binary
3616 operators in a way that is likely to confuse the user.
3617
3618 LOCATION is the location of the binary operator. */
3619
3620struct c_expr
3621parser_build_binary_op (location_t location, enum tree_code code,
3622 struct c_expr arg1, struct c_expr arg2)
3623{
3624 struct c_expr result;
3625
3626 enum tree_code code1 = arg1.original_code;
3627 enum tree_code code2 = arg2.original_code;
3628 tree type1 = (arg1.original_type
3629 ? arg1.original_type
3630 : TREE_TYPE (arg1.value));
3631 tree type2 = (arg2.original_type
3632 ? arg2.original_type
3633 : TREE_TYPE (arg2.value));
3634
3635 result.value = build_binary_op (location, code,
3636 arg1.value, arg2.value, true);
3637 result.original_code = code;
3638 result.original_type = NULL;
3639
3640 if (TREE_CODE (result.value) == ERROR_MARK)
3641 {
3642 set_c_expr_source_range (&result,
3643 arg1.get_start (),
3644 arg2.get_finish ());
3645 return result;
3646 }
3647
3648 if (location != UNKNOWN_LOCATION)
3649 protected_set_expr_location (result.value, location);
3650
3651 set_c_expr_source_range (&result,
3652 arg1.get_start (),
3653 arg2.get_finish ());
3654
3655 /* Check for cases such as x+y<<z which users are likely
3656 to misinterpret. */
3657 if (warn_parentheses)
3658 warn_about_parentheses (location, code, code1, arg1.value, code2,
3659 arg2.value);
3660
3661 if (warn_logical_op)
3662 warn_logical_operator (location, code, TREE_TYPE (result.value),
3663 code1, arg1.value, code2, arg2.value);
3664
3665 if (warn_tautological_compare)
3666 {
3667 tree lhs = arg1.value;
3668 tree rhs = arg2.value;
3669 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3670 {
3671 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3672 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3673 lhs = NULL_TREE;
3674 else
3675 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3676 }
3677 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3678 {
3679 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3680 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3681 rhs = NULL_TREE;
3682 else
3683 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3684 }
3685 if (lhs != NULL_TREE && rhs != NULL_TREE)
3686 warn_tautological_cmp (location, code, lhs, rhs);
3687 }
3688
3689 if (warn_logical_not_paren
3690 && TREE_CODE_CLASS (code) == tcc_comparison
3691 && code1 == TRUTH_NOT_EXPR
3692 && code2 != TRUTH_NOT_EXPR
3693 /* Avoid warning for !!x == y. */
3694 && (TREE_CODE (arg1.value) != NE_EXPR
3695 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3696 {
3697 /* Avoid warning for !b == y where b has _Bool type. */
3698 tree t = integer_zero_node;
3699 if (TREE_CODE (arg1.value) == EQ_EXPR
3700 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3701 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3702 {
3703 t = TREE_OPERAND (arg1.value, 0);
3704 do
3705 {
3706 if (TREE_TYPE (t) != integer_type_node)
3707 break;
3708 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3709 t = C_MAYBE_CONST_EXPR_EXPR (t);
3710 else if (CONVERT_EXPR_P (t))
3711 t = TREE_OPERAND (t, 0);
3712 else
3713 break;
3714 }
3715 while (1);
3716 }
3717 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3718 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3719 }
3720
3721 /* Warn about comparisons against string literals, with the exception
3722 of testing for equality or inequality of a string literal with NULL. */
3723 if (code == EQ_EXPR || code == NE_EXPR)
3724 {
3725 if ((code1 == STRING_CST
3726 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3727 || (code2 == STRING_CST
3728 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3729 warning_at (location, OPT_Waddress,
3730 "comparison with string literal results in unspecified behavior");
3731 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3732 if (POINTER_TYPE_P (type1)
3733 && null_pointer_constant_p (arg2.value)
3734 && char_type_p (type2)
3735 && warning_at (location, OPT_Wpointer_compare,
3736 "comparison between pointer and zero character "
3737 "constant"))
3738 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3739 else if (POINTER_TYPE_P (type2)
3740 && null_pointer_constant_p (arg1.value)
3741 && char_type_p (type1)
3742 && warning_at (location, OPT_Wpointer_compare,
3743 "comparison between pointer and zero character "
3744 "constant"))
3745 inform (arg2.get_start (), "did you mean to dereference the pointer?");
3746 }
3747 else if (TREE_CODE_CLASS (code) == tcc_comparison
3748 && (code1 == STRING_CST || code2 == STRING_CST))
3749 warning_at (location, OPT_Waddress,
3750 "comparison with string literal results in unspecified behavior");
3751
3752 if (TREE_OVERFLOW_P (result.value)
3753 && !TREE_OVERFLOW_P (arg1.value)
3754 && !TREE_OVERFLOW_P (arg2.value))
3755 overflow_warning (location, result.value);
3756
3757 /* Warn about comparisons of different enum types. */
3758 if (warn_enum_compare
3759 && TREE_CODE_CLASS (code) == tcc_comparison
3760 && TREE_CODE (type1) == ENUMERAL_TYPE
3761 && TREE_CODE (type2) == ENUMERAL_TYPE
3762 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3763 warning_at (location, OPT_Wenum_compare,
3764 "comparison between %qT and %qT",
3765 type1, type2);
3766
3767 return result;
3768}
3769
3770/* Return a tree for the difference of pointers OP0 and OP1.
3771 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3772 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3773
3774static tree
3775pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3776{
3777 tree restype = ptrdiff_type_node;
3778 tree result, inttype;
3779
3780 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3781 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3782 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3783 tree orig_op1 = op1;
3784
3785 /* If the operands point into different address spaces, we need to
3786 explicitly convert them to pointers into the common address space
3787 before we can subtract the numerical address values. */
3788 if (as0 != as1)
3789 {
3790 addr_space_t as_common;
3791 tree common_type;
3792
3793 /* Determine the common superset address space. This is guaranteed
3794 to exist because the caller verified that comp_target_types
3795 returned non-zero. */
3796 if (!addr_space_superset (as0, as1, &as_common))
3797 gcc_unreachable ();
3798
3799 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3800 op0 = convert (common_type, op0);
3801 op1 = convert (common_type, op1);
3802 }
3803
3804 /* Determine integer type result of the subtraction. This will usually
3805 be the same as the result type (ptrdiff_t), but may need to be a wider
3806 type if pointers for the address space are wider than ptrdiff_t. */
3807 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3808 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3809 else
3810 inttype = restype;
3811
3812 if (TREE_CODE (target_type) == VOID_TYPE)
3813 pedwarn (loc, OPT_Wpointer_arith,
3814 "pointer of type %<void *%> used in subtraction");
3815 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3816 pedwarn (loc, OPT_Wpointer_arith,
3817 "pointer to a function used in subtraction");
3818
3819 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3820 {
3821 gcc_assert (current_function_decl != NULL_TREE);
3822
3823 op0 = save_expr (op0);
3824 op1 = save_expr (op1);
3825
3826 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3827 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3828 }
3829
3830 /* First do the subtraction, then build the divide operator
3831 and only convert at the very end.
3832 Do not do default conversions in case restype is a short type. */
3833
3834 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3835 pointers. If some platform cannot provide that, or has a larger
3836 ptrdiff_type to support differences larger than half the address
3837 space, cast the pointers to some larger integer type and do the
3838 computations in that type. */
3839 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3840 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3841 convert (inttype, op1), false);
3842 else
3843 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3844
3845 /* This generates an error if op1 is pointer to incomplete type. */
3846 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3847 error_at (loc, "arithmetic on pointer to an incomplete type");
3848
3849 op1 = c_size_in_bytes (target_type);
3850
3851 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3852 error_at (loc, "arithmetic on pointer to an empty aggregate");
3853
3854 /* Divide by the size, in easiest possible way. */
3855 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3856 op0, convert (inttype, op1));
3857
3858 /* Convert to final result type if necessary. */
3859 return convert (restype, result);
3860}
3861
3862/* Expand atomic compound assignments into an appropriate sequence as
3863 specified by the C11 standard section 6.5.16.2.
3864
3865 _Atomic T1 E1
3866 T2 E2
3867 E1 op= E2
3868
3869 This sequence is used for all types for which these operations are
3870 supported.
3871
3872 In addition, built-in versions of the 'fe' prefixed routines may
3873 need to be invoked for floating point (real, complex or vector) when
3874 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3875
3876 T1 newval;
3877 T1 old;
3878 T1 *addr
3879 T2 val
3880 fenv_t fenv
3881
3882 addr = &E1;
3883 val = (E2);
3884 __atomic_load (addr, &old, SEQ_CST);
3885 feholdexcept (&fenv);
3886loop:
3887 newval = old op val;
3888 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3889 SEQ_CST))
3890 goto done;
3891 feclearexcept (FE_ALL_EXCEPT);
3892 goto loop:
3893done:
3894 feupdateenv (&fenv);
3895
3896 The compiler will issue the __atomic_fetch_* built-in when possible,
3897 otherwise it will generate the generic form of the atomic operations.
3898 This requires temp(s) and has their address taken. The atomic processing
3899 is smart enough to figure out when the size of an object can utilize
3900 a lock-free version, and convert the built-in call to the appropriate
3901 lock-free routine. The optimizers will then dispose of any temps that
3902 are no longer required, and lock-free implementations are utilized as
3903 long as there is target support for the required size.
3904
3905 If the operator is NOP_EXPR, then this is a simple assignment, and
3906 an __atomic_store is issued to perform the assignment rather than
3907 the above loop. */
3908
3909/* Build an atomic assignment at LOC, expanding into the proper
3910 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3911 the result of the operation, unless RETURN_OLD_P, in which case
3912 return the old value of LHS (this is only for postincrement and
3913 postdecrement). */
3914
3915static tree
3916build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3917 tree rhs, bool return_old_p)
3918{
3919 tree fndecl, func_call;
3920 vec<tree, va_gc> *params;
3921 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3922 tree old, old_addr;
3923 tree compound_stmt;
3924 tree stmt, goto_stmt;
3925 tree loop_label, loop_decl, done_label, done_decl;
3926
3927 tree lhs_type = TREE_TYPE (lhs);
3928 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3929 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3930 tree rhs_semantic_type = TREE_TYPE (rhs);
3931 tree nonatomic_rhs_semantic_type;
3932 tree rhs_type;
3933
3934 gcc_assert (TYPE_ATOMIC (lhs_type));
3935
3936 if (return_old_p)
3937 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3938
3939 /* Allocate enough vector items for a compare_exchange. */
3940 vec_alloc (params, 6);
3941
3942 /* Create a compound statement to hold the sequence of statements
3943 with a loop. */
3944 compound_stmt = c_begin_compound_stmt (false);
3945
3946 /* Remove any excess precision (which is only present here in the
3947 case of compound assignments). */
3948 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3949 {
3950 gcc_assert (modifycode != NOP_EXPR);
3951 rhs = TREE_OPERAND (rhs, 0);
3952 }
3953 rhs_type = TREE_TYPE (rhs);
3954
3955 /* Fold the RHS if it hasn't already been folded. */
3956 if (modifycode != NOP_EXPR)
3957 rhs = c_fully_fold (rhs, false, NULL);
3958
3959 /* Remove the qualifiers for the rest of the expressions and create
3960 the VAL temp variable to hold the RHS. */
3961 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3962 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3963 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3964 TYPE_UNQUALIFIED);
3965 val = create_tmp_var_raw (nonatomic_rhs_type);
3966 TREE_ADDRESSABLE (val) = 1;
3967 TREE_NO_WARNING (val) = 1;
3968 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3969 NULL_TREE);
3970 SET_EXPR_LOCATION (rhs, loc);
3971 add_stmt (rhs);
3972
3973 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3974 an atomic_store. */
3975 if (modifycode == NOP_EXPR)
3976 {
3977 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3978 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3979 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3980 params->quick_push (lhs_addr);
3981 params->quick_push (rhs);
3982 params->quick_push (seq_cst);
3983 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3984 add_stmt (func_call);
3985
3986 /* Finish the compound statement. */
3987 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3988
3989 /* VAL is the value which was stored, return a COMPOUND_STMT of
3990 the statement and that value. */
3991 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3992 }
3993
3994 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3995 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3996 isn't applicable for such builtins. ??? Do we want to handle enums? */
3997 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3998 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3999 {
4000 built_in_function fncode;
4001 switch (modifycode)
4002 {
4003 case PLUS_EXPR:
4004 case POINTER_PLUS_EXPR:
4005 fncode = (return_old_p
4006 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4007 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4008 break;
4009 case MINUS_EXPR:
4010 fncode = (return_old_p
4011 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4012 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4013 break;
4014 case BIT_AND_EXPR:
4015 fncode = (return_old_p
4016 ?