mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
typeck.cc
11921 lines (10461 loc) · 366 KB
/
typeck.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Build expressions with type checking for C++ compiler.
Copyright (C) 1987-2023 Free Software Foundation, Inc.
Hacked by Michael Tiemann (tiemann@cygnus.com)
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
/* This file is part of the C++ front end.
It contains routines to build C++ expressions given their operands,
including computing the types of the result, C and C++ specific error
checks, and some optimization. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "cp-tree.h"
#include "stor-layout.h"
#include "varasm.h"
#include "intl.h"
#include "convert.h"
#include "c-family/c-objc.h"
#include "c-family/c-ubsan.h"
#include "gcc-rich-location.h"
#include "stringpool.h"
#include "attribs.h"
#include "asan.h"
#include "gimplify.h"
static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
static tree cp_build_function_call (tree, tree, tsubst_flags_t);
static tree pfn_from_ptrmemfunc (tree);
static tree delta_from_ptrmemfunc (tree);
static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
tsubst_flags_t, int);
static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
tsubst_flags_t);
static tree rationalize_conditional_expr (enum tree_code, tree,
tsubst_flags_t);
static bool comp_ptr_ttypes_real (tree, tree, int);
static bool comp_except_types (tree, tree, bool);
static bool comp_array_types (const_tree, const_tree, compare_bounds_t, bool);
static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
static bool casts_away_constness (tree, tree, tsubst_flags_t);
static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKNOWN_LOCATION);
static void error_args_num (location_t, tree, bool);
static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
tsubst_flags_t);
static bool is_std_move_p (tree);
static bool is_std_forward_p (tree);
/* Do `exp = require_complete_type (exp);' to make sure exp
does not have an incomplete type. (That includes void types.)
Returns error_mark_node if the VALUE does not have
complete type when this function returns. */
tree
require_complete_type (tree value,
tsubst_flags_t complain /* = tf_warning_or_error */)
{
tree type;
if (processing_template_decl || value == error_mark_node)
return value;
if (TREE_CODE (value) == OVERLOAD)
type = unknown_type_node;
else
type = TREE_TYPE (value);
if (type == error_mark_node)
return error_mark_node;
/* First, detect a valid value with a complete type. */
if (COMPLETE_TYPE_P (type))
return value;
if (complete_type_or_maybe_complain (type, value, complain))
return value;
else
return error_mark_node;
}
/* Try to complete TYPE, if it is incomplete. For example, if TYPE is
a template instantiation, do the instantiation. Returns TYPE,
whether or not it could be completed, unless something goes
horribly wrong, in which case the error_mark_node is returned. */
tree
complete_type (tree type)
{
if (type == NULL_TREE)
/* Rather than crash, we return something sure to cause an error
at some point. */
return error_mark_node;
if (type == error_mark_node || COMPLETE_TYPE_P (type))
;
else if (TREE_CODE (type) == ARRAY_TYPE)
{
tree t = complete_type (TREE_TYPE (type));
unsigned int needs_constructing, has_nontrivial_dtor;
if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
layout_type (type);
needs_constructing
= TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
has_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
{
TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
}
}
else if (CLASS_TYPE_P (type))
{
if (modules_p ())
/* TYPE could be a class member we've not loaded the definition of. */
lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
instantiate_class_template (TYPE_MAIN_VARIANT (type));
}
return type;
}
/* Like complete_type, but issue an error if the TYPE cannot be completed.
VALUE is used for informative diagnostics.
Returns NULL_TREE if the type cannot be made complete. */
tree
complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
{
type = complete_type (type);
if (type == error_mark_node)
/* We already issued an error. */
return NULL_TREE;
else if (!COMPLETE_TYPE_P (type))
{
if (complain & tf_error)
cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
note_failed_type_completion_for_satisfaction (type);
return NULL_TREE;
}
else
return type;
}
tree
complete_type_or_else (tree type, tree value)
{
return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
}
/* Return the common type of two parameter lists.
We assume that comptypes has already been done and returned 1;
if that isn't so, this may crash.
As an optimization, free the space we allocate if the parameter
lists are already common. */
static tree
commonparms (tree p1, tree p2)
{
tree oldargs = p1, newargs, n;
int i, len;
int any_change = 0;
len = list_length (p1);
newargs = tree_last (p1);
if (newargs == void_list_node)
i = 1;
else
{
i = 0;
newargs = 0;
}
for (; i < len; i++)
newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
n = newargs;
for (i = 0; p1;
p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
{
if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
{
TREE_PURPOSE (n) = TREE_PURPOSE (p1);
any_change = 1;
}
else if (! TREE_PURPOSE (p1))
{
if (TREE_PURPOSE (p2))
{
TREE_PURPOSE (n) = TREE_PURPOSE (p2);
any_change = 1;
}
}
else
{
if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
any_change = 1;
TREE_PURPOSE (n) = TREE_PURPOSE (p2);
}
if (TREE_VALUE (p1) != TREE_VALUE (p2))
{
any_change = 1;
TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
}
else
TREE_VALUE (n) = TREE_VALUE (p1);
}
if (! any_change)
return oldargs;
return newargs;
}
/* Given a type, perhaps copied for a typedef,
find the "original" version of it. */
static tree
original_type (tree t)
{
int quals = cp_type_quals (t);
while (t != error_mark_node
&& TYPE_NAME (t) != NULL_TREE)
{
tree x = TYPE_NAME (t);
if (TREE_CODE (x) != TYPE_DECL)
break;
x = DECL_ORIGINAL_TYPE (x);
if (x == NULL_TREE)
break;
t = x;
}
return cp_build_qualified_type (t, quals);
}
/* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE
and return a variant of TYPE with the merged attributes. */
static tree
merge_type_attributes_from (tree type, tree other_type)
{
tree attrs = targetm.merge_type_attributes (type, other_type);
attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
return cp_build_type_attribute_variant (type, attrs);
}
/* Compare floating point conversion ranks and subranks of T1 and T2
types. If T1 and T2 have unordered conversion ranks, return 3.
If T1 has greater conversion rank than T2, return 2.
If T2 has greater conversion rank than T1, return -2.
If T1 has equal conversion rank as T2, return -1, 0 or 1 depending
on if T1 has smaller, equal or greater conversion subrank than
T2. */
int
cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
{
tree mv1 = TYPE_MAIN_VARIANT (t1);
tree mv2 = TYPE_MAIN_VARIANT (t2);
int extended1 = 0;
int extended2 = 0;
if (mv1 == mv2)
return 0;
for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
{
if (mv1 == FLOATN_NX_TYPE_NODE (i))
extended1 = i + 1;
if (mv2 == FLOATN_NX_TYPE_NODE (i))
extended2 = i + 1;
}
if (mv1 == bfloat16_type_node)
extended1 = true;
if (mv2 == bfloat16_type_node)
extended2 = true;
if (extended2 && !extended1)
{
int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
return ret == 3 ? 3 : -ret;
}
const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
gcc_assert (fmt1->b == 2 && fmt2->b == 2);
/* For {ibm,mips}_extended_format formats, the type has variable
precision up to ~2150 bits when the first double is around maximum
representable double and second double is subnormal minimum.
So, e.g. for __ibm128 vs. std::float128_t, they have unordered
ranks. */
int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
/* The rank of a floating point type T is greater than the rank of
any floating-point type whose set of values is a proper subset
of the set of values of T. */
if ((p1 > p2 && fmt1->emax >= fmt2->emax)
|| (p1 == p2 && fmt1->emax > fmt2->emax))
return 2;
if ((p1 < p2 && fmt1->emax <= fmt2->emax)
|| (p1 == p2 && fmt1->emax < fmt2->emax))
return -2;
if ((p1 > p2 && fmt1->emax < fmt2->emax)
|| (p1 < p2 && fmt1->emax > fmt2->emax))
return 3;
if (!extended1 && !extended2)
{
/* The rank of long double is greater than the rank of double, which
is greater than the rank of float. */
if (t1 == long_double_type_node)
return 2;
else if (t2 == long_double_type_node)
return -2;
if (t1 == double_type_node)
return 2;
else if (t2 == double_type_node)
return -2;
if (t1 == float_type_node)
return 2;
else if (t2 == float_type_node)
return -2;
return 0;
}
/* Two extended floating-point types with the same set of values have equal
ranks. */
if (extended1 && extended2)
{
if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
{
/* Prefer higher extendedN value. */
if (extended1 > extended2)
return 1;
else if (extended1 < extended2)
return -1;
else
return 0;
}
else if (extended1 <= NUM_FLOATN_TYPES)
/* Prefer _FloatN type over _FloatMx type. */
return 1;
else if (extended2 <= NUM_FLOATN_TYPES)
return -1;
else
return 0;
}
/* gcc_assert (extended1 && !extended2); */
tree *p;
int cnt = 0;
for (p = &float_type_node; p <= &long_double_type_node; ++p)
{
const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
gcc_assert (fmt3->b == 2);
int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
if (p1 == p3 && fmt1->emax == fmt3->emax)
++cnt;
}
/* An extended floating-point type with the same set of values
as exactly one cv-unqualified standard floating-point type
has a rank equal to the rank of that standard floating-point
type.
An extended floating-point type with the same set of values
as more than one cv-unqualified standard floating-point type
has a rank equal to the rank of double.
Thus, if the latter is true and t2 is long double, t2
has higher rank. */
if (cnt > 1 && mv2 == long_double_type_node)
return -2;
/* Otherwise, they have equal rank, but extended types
(other than std::bfloat16_t) have higher subrank.
std::bfloat16_t shouldn't have equal rank to any standard
floating point type. */
return 1;
}
/* Return the common type for two arithmetic types T1 and T2 under the
usual arithmetic conversions. The default conversions have already
been applied, and enumerated types converted to their compatible
integer types. */
static tree
cp_common_type (tree t1, tree t2)
{
enum tree_code code1 = TREE_CODE (t1);
enum tree_code code2 = TREE_CODE (t2);
tree attributes;
int i;
/* In what follows, we slightly generalize the rules given in [expr] so
as to deal with `long long' and `complex'. First, merge the
attributes. */
attributes = (*targetm.merge_type_attributes) (t1, t2);
if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
{
if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
return build_type_attribute_variant (t1, attributes);
else
return NULL_TREE;
}
/* FIXME: Attributes. */
gcc_assert (ARITHMETIC_TYPE_P (t1)
|| VECTOR_TYPE_P (t1)
|| UNSCOPED_ENUM_P (t1));
gcc_assert (ARITHMETIC_TYPE_P (t2)
|| VECTOR_TYPE_P (t2)
|| UNSCOPED_ENUM_P (t2));
/* If one type is complex, form the common type of the non-complex
components, then make that complex. Use T1 or T2 if it is the
required type. */
if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
{
tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
tree subtype
= type_after_usual_arithmetic_conversions (subtype1, subtype2);
if (subtype == error_mark_node)
return subtype;
if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
return build_type_attribute_variant (t1, attributes);
else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
return build_type_attribute_variant (t2, attributes);
else
return build_type_attribute_variant (build_complex_type (subtype),
attributes);
}
if (code1 == VECTOR_TYPE)
{
/* When we get here we should have two vectors of the same size.
Just prefer the unsigned one if present. */
if (TYPE_UNSIGNED (t1))
return merge_type_attributes_from (t1, t2);
else
return merge_type_attributes_from (t2, t1);
}
/* If only one is real, use it as the result. */
if (code1 == REAL_TYPE && code2 != REAL_TYPE)
return build_type_attribute_variant (t1, attributes);
if (code2 == REAL_TYPE && code1 != REAL_TYPE)
return build_type_attribute_variant (t2, attributes);
if (code1 == REAL_TYPE
&& (extended_float_type_p (t1) || extended_float_type_p (t2)))
{
tree mv1 = TYPE_MAIN_VARIANT (t1);
tree mv2 = TYPE_MAIN_VARIANT (t2);
if (mv1 == mv2)
return build_type_attribute_variant (t1, attributes);
int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
if (cmpret == 3)
return error_mark_node;
else if (cmpret >= 0)
return build_type_attribute_variant (t1, attributes);
else
return build_type_attribute_variant (t2, attributes);
}
/* Both real or both integers; use the one with greater precision. */
if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
return build_type_attribute_variant (t1, attributes);
else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
return build_type_attribute_variant (t2, attributes);
/* The types are the same; no need to do anything fancy. */
if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
return build_type_attribute_variant (t1, attributes);
if (code1 != REAL_TYPE)
{
/* If one is unsigned long long, then convert the other to unsigned
long long. */
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
return build_type_attribute_variant (long_long_unsigned_type_node,
attributes);
/* If one is a long long, and the other is an unsigned long, and
long long can represent all the values of an unsigned long, then
convert to a long long. Otherwise, convert to an unsigned long
long. Otherwise, if either operand is long long, convert the
other to long long.
Since we're here, we know the TYPE_PRECISION is the same;
therefore converting to long long cannot represent all the values
of an unsigned long, so we choose unsigned long long in that
case. */
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
{
tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
? long_long_unsigned_type_node
: long_long_integer_type_node);
return build_type_attribute_variant (t, attributes);
}
/* Go through the same procedure, but for longs. */
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
return build_type_attribute_variant (long_unsigned_type_node,
attributes);
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
{
tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
? long_unsigned_type_node : long_integer_type_node);
return build_type_attribute_variant (t, attributes);
}
/* For __intN types, either the type is __int128 (and is lower
priority than the types checked above, but higher than other
128-bit types) or it's known to not be the same size as other
types (enforced in toplev.cc). Prefer the unsigned type. */
for (i = 0; i < NUM_INT_N_ENTS; i ++)
{
if (int_n_enabled_p [i]
&& (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
|| same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
|| same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
|| same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
{
tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
? int_n_trees[i].unsigned_type
: int_n_trees[i].signed_type);
return build_type_attribute_variant (t, attributes);
}
}
/* Otherwise prefer the unsigned one. */
if (TYPE_UNSIGNED (t1))
return build_type_attribute_variant (t1, attributes);
else
return build_type_attribute_variant (t2, attributes);
}
else
{
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
return build_type_attribute_variant (long_double_type_node,
attributes);
if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
return build_type_attribute_variant (double_type_node,
attributes);
if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
return build_type_attribute_variant (float_type_node,
attributes);
/* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
the standard C++ floating-point types. Logic earlier in this
function has already eliminated the possibility that
TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
compelling reason to choose one or the other. */
return build_type_attribute_variant (t1, attributes);
}
}
/* T1 and T2 are arithmetic or enumeration types. Return the type
that will result from the "usual arithmetic conversions" on T1 and
T2 as described in [expr]. */
tree
type_after_usual_arithmetic_conversions (tree t1, tree t2)
{
gcc_assert (ARITHMETIC_TYPE_P (t1)
|| VECTOR_TYPE_P (t1)
|| UNSCOPED_ENUM_P (t1));
gcc_assert (ARITHMETIC_TYPE_P (t2)
|| VECTOR_TYPE_P (t2)
|| UNSCOPED_ENUM_P (t2));
/* Perform the integral promotions. We do not promote real types here. */
if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
&& INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
{
t1 = type_promotes_to (t1);
t2 = type_promotes_to (t2);
}
return cp_common_type (t1, t2);
}
static void
composite_pointer_error (const op_location_t &location,
diagnostic_t kind, tree t1, tree t2,
composite_pointer_operation operation)
{
switch (operation)
{
case CPO_COMPARISON:
emit_diagnostic (kind, location, 0,
"comparison between "
"distinct pointer types %qT and %qT lacks a cast",
t1, t2);
break;
case CPO_CONVERSION:
emit_diagnostic (kind, location, 0,
"conversion between "
"distinct pointer types %qT and %qT lacks a cast",
t1, t2);
break;
case CPO_CONDITIONAL_EXPR:
emit_diagnostic (kind, location, 0,
"conditional expression between "
"distinct pointer types %qT and %qT lacks a cast",
t1, t2);
break;
default:
gcc_unreachable ();
}
}
/* Subroutine of composite_pointer_type to implement the recursive
case. See that function for documentation of the parameters. And ADD_CONST
is used to track adding "const" where needed. */
static tree
composite_pointer_type_r (const op_location_t &location,
tree t1, tree t2, bool *add_const,
composite_pointer_operation operation,
tsubst_flags_t complain)
{
tree pointee1;
tree pointee2;
tree result_type;
tree attributes;
/* Determine the types pointed to by T1 and T2. */
if (TYPE_PTR_P (t1))
{
pointee1 = TREE_TYPE (t1);
pointee2 = TREE_TYPE (t2);
}
else
{
pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
}
/* [expr.type]
If T1 and T2 are similar types, the result is the cv-combined type of
T1 and T2. */
if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
result_type = pointee1;
else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
|| (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
{
result_type = composite_pointer_type_r (location, pointee1, pointee2,
add_const, operation, complain);
if (result_type == error_mark_node)
return error_mark_node;
}
else
{
if (complain & tf_error)
composite_pointer_error (location, DK_PERMERROR,
t1, t2, operation);
else
return error_mark_node;
result_type = void_type_node;
}
const int q1 = cp_type_quals (pointee1);
const int q2 = cp_type_quals (pointee2);
const int quals = q1 | q2;
result_type = cp_build_qualified_type (result_type,
(quals | (*add_const
? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED)));
/* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
the TLQ). The reason is that both T1 and T2 can then be converted to the
cv-combined type of T1 and T2. */
if (quals != q1 || quals != q2)
*add_const = true;
/* If the original types were pointers to members, so is the
result. */
if (TYPE_PTRMEM_P (t1))
{
if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
TYPE_PTRMEM_CLASS_TYPE (t2)))
{
if (complain & tf_error)
composite_pointer_error (location, DK_PERMERROR,
t1, t2, operation);
else
return error_mark_node;
}
result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
result_type);
}
else
result_type = build_pointer_type (result_type);
/* Merge the attributes. */
attributes = (*targetm.merge_type_attributes) (t1, t2);
return build_type_attribute_variant (result_type, attributes);
}
/* Return the composite pointer type (see [expr.type]) for T1 and T2.
ARG1 and ARG2 are the values with those types. The OPERATION is to
describe the operation between the pointer types,
in case an error occurs.
This routine also implements the computation of a common type for
pointers-to-members as per [expr.eq]. */
tree
composite_pointer_type (const op_location_t &location,
tree t1, tree t2, tree arg1, tree arg2,
composite_pointer_operation operation,
tsubst_flags_t complain)
{
tree class1;
tree class2;
/* [expr.type]
If one operand is a null pointer constant, the composite pointer
type is the type of the other operand. */
if (null_ptr_cst_p (arg1))
return t2;
if (null_ptr_cst_p (arg2))
return t1;
/* We have:
[expr.type]
If one of the operands has type "pointer to cv1 void", then
the other has type "pointer to cv2 T", and the composite pointer
type is "pointer to cv12 void", where cv12 is the union of cv1
and cv2.
If either type is a pointer to void, make sure it is T1. */
if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
std::swap (t1, t2);
/* Now, if T1 is a pointer to void, merge the qualifiers. */
if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
{
tree attributes;
tree result_type;
if (TYPE_PTRFN_P (t2))
{
if (complain & tf_error)
{
switch (operation)
{
case CPO_COMPARISON:
pedwarn (location, OPT_Wpedantic,
"ISO C++ forbids comparison between pointer "
"of type %<void *%> and pointer-to-function");
break;
case CPO_CONVERSION:
pedwarn (location, OPT_Wpedantic,
"ISO C++ forbids conversion between pointer "
"of type %<void *%> and pointer-to-function");
break;
case CPO_CONDITIONAL_EXPR:
pedwarn (location, OPT_Wpedantic,
"ISO C++ forbids conditional expression between "
"pointer of type %<void *%> and "
"pointer-to-function");
break;
default:
gcc_unreachable ();
}
}
else
return error_mark_node;
}
result_type
= cp_build_qualified_type (void_type_node,
(cp_type_quals (TREE_TYPE (t1))
| cp_type_quals (TREE_TYPE (t2))));
result_type = build_pointer_type (result_type);
/* Merge the attributes. */
attributes = (*targetm.merge_type_attributes) (t1, t2);
return build_type_attribute_variant (result_type, attributes);
}
if (c_dialect_objc () && TYPE_PTR_P (t1)
&& TYPE_PTR_P (t2))
{
if (objc_have_common_type (t1, t2, -3, NULL_TREE))
return objc_common_type (t1, t2);
}
/* if T1 or T2 is "pointer to noexcept function" and the other type is
"pointer to function", where the function types are otherwise the same,
"pointer to function" */
if (fnptr_conv_p (t1, t2))
return t1;
if (fnptr_conv_p (t2, t1))
return t2;
/* [expr.eq] permits the application of a pointer conversion to
bring the pointers to a common type. */
if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
&& CLASS_TYPE_P (TREE_TYPE (t1))
&& CLASS_TYPE_P (TREE_TYPE (t2))
&& !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
TREE_TYPE (t2)))
{
class1 = TREE_TYPE (t1);
class2 = TREE_TYPE (t2);
if (DERIVED_FROM_P (class1, class2))
t2 = (build_pointer_type
(cp_build_qualified_type (class1, cp_type_quals (class2))));
else if (DERIVED_FROM_P (class2, class1))
t1 = (build_pointer_type
(cp_build_qualified_type (class2, cp_type_quals (class1))));
else
{
if (complain & tf_error)
composite_pointer_error (location, DK_ERROR, t1, t2, operation);
return error_mark_node;
}
}
/* [expr.eq] permits the application of a pointer-to-member
conversion to change the class type of one of the types. */
else if (TYPE_PTRMEM_P (t1)
&& !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
TYPE_PTRMEM_CLASS_TYPE (t2)))
{
class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
if (DERIVED_FROM_P (class1, class2))
t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
else if (DERIVED_FROM_P (class2, class1))
t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
else
{
if (complain & tf_error)
switch (operation)
{
case CPO_COMPARISON:
error_at (location, "comparison between distinct "
"pointer-to-member types %qT and %qT lacks a cast",
t1, t2);
break;
case CPO_CONVERSION:
error_at (location, "conversion between distinct "
"pointer-to-member types %qT and %qT lacks a cast",
t1, t2);
break;
case CPO_CONDITIONAL_EXPR:
error_at (location, "conditional expression between distinct "
"pointer-to-member types %qT and %qT lacks a cast",
t1, t2);
break;
default:
gcc_unreachable ();
}
return error_mark_node;
}
}
bool add_const = false;
return composite_pointer_type_r (location, t1, t2, &add_const, operation,
complain);
}
/* Return the merged type of two types.
We assume that comptypes has already been done and returned 1;
if that isn't so, this may crash.
This just combines attributes and default arguments; any other
differences would cause the two types to compare unalike. */
tree
merge_types (tree t1, tree t2)
{
enum tree_code code1;
enum tree_code code2;
tree attributes;
/* Save time if the two types are the same. */
if (t1 == t2)
return t1;
if (original_type (t1) == original_type (t2))
return t1;
/* If one type is nonsense, use the other. */
if (t1 == error_mark_node)
return t2;
if (t2 == error_mark_node)
return t1;
/* Handle merging an auto redeclaration with a previous deduced
return type. */
if (is_auto (t1))
return t2;
/* Merge the attributes. */
attributes = (*targetm.merge_type_attributes) (t1, t2);
if (TYPE_PTRMEMFUNC_P (t1))
t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
if (TYPE_PTRMEMFUNC_P (t2))
t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
code1 = TREE_CODE (t1);
code2 = TREE_CODE (t2);
if (code1 != code2)
{
gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
if (code1 == TYPENAME_TYPE)
{
t1 = resolve_typename_type (t1, /*only_current_p=*/true);
code1 = TREE_CODE (t1);
}
else
{
t2 = resolve_typename_type (t2, /*only_current_p=*/true);
code2 = TREE_CODE (t2);
}
}
switch (code1)
{
case POINTER_TYPE:
case REFERENCE_TYPE:
/* For two pointers, do this recursively on the target type. */
{
tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
int quals = cp_type_quals (t1);
if (code1 == POINTER_TYPE)
{
t1 = build_pointer_type (target);
if (TREE_CODE (target) == METHOD_TYPE)
t1 = build_ptrmemfunc_type (t1);
}
else
t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
t1 = build_type_attribute_variant (t1, attributes);
t1 = cp_build_qualified_type (t1, quals);
return t1;
}
case OFFSET_TYPE:
{
int quals;
tree pointee;
quals = cp_type_quals (t1);
pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
TYPE_PTRMEM_POINTED_TO_TYPE (t2));
t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
pointee);
t1 = cp_build_qualified_type (t1, quals);
break;
}
case ARRAY_TYPE:
{
tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
/* Save space: see if the result is identical to one of the args. */
if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
return build_type_attribute_variant (t1, attributes);
if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
return build_type_attribute_variant (t2, attributes);
/* Merge the element types, and have a size if either arg has one. */