-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcall.c
6402 lines (5566 loc) · 179 KB
/
call.c
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
/* Functions related to invoking methods and overloaded functions.
Copyright (C) 1987, 92-97, 1998 Free Software Foundation, Inc.
Contributed by Michael Tiemann ([email protected]) and
modified by Brendan Kehoe ([email protected]).
This file is part of GNU CC.
GNU CC 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 2, or (at your option)
any later version.
GNU CC 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 GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* High-level class interface. */
#include "config.h"
#include "tree.h"
#include <stdio.h>
#include "cp-tree.h"
#include "class.h"
#include "output.h"
#include "flags.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "obstack.h"
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
extern int inhibit_warnings;
extern tree ctor_label, dtor_label;
/* Compute the ease with which a conversion can be performed
between an expected and the given type. */
static struct harshness_code convert_harshness PROTO((register tree, register tree, tree));
static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
static int rank_for_ideal PROTO((struct candidate *,
struct candidate *));
static int user_harshness PROTO((tree, tree));
static int strictly_better PROTO((unsigned int, unsigned int));
static struct candidate * ideal_candidate PROTO((struct candidate *,
int, int));
static int may_be_remote PROTO((tree));
static tree build_field_call PROTO((tree, tree, tree, tree));
static tree find_scoped_type PROTO((tree, tree, tree));
static void print_candidates PROTO((tree));
static struct z_candidate * tourney PROTO((struct z_candidate *));
static int joust PROTO((struct z_candidate *, struct z_candidate *));
static int compare_qual PROTO((tree, tree));
static int compare_ics PROTO((tree, tree));
static tree build_over_call PROTO((tree, tree, tree, int));
static tree convert_default_arg PROTO((tree, tree));
static void enforce_access PROTO((tree, tree));
static tree convert_like PROTO((tree, tree));
static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
tree, char *));
static tree build_object_call PROTO((tree, tree));
static tree resolve_args PROTO((tree));
static struct z_candidate * build_user_type_conversion_1
PROTO ((tree, tree, int));
static void print_z_candidates PROTO((struct z_candidate *));
static tree build_this PROTO((tree));
static struct z_candidate * splice_viable PROTO((struct z_candidate *));
static int any_viable PROTO((struct z_candidate *));
static struct z_candidate * add_template_candidate
PROTO((struct z_candidate *, tree, tree, tree, tree, int));
static struct z_candidate * add_template_conv_candidate
PROTO((struct z_candidate *, tree, tree, tree, tree));
static struct z_candidate * add_builtin_candidates
PROTO((struct z_candidate *, enum tree_code, enum tree_code,
tree, tree *, int));
static struct z_candidate * add_builtin_candidate
PROTO((struct z_candidate *, enum tree_code, enum tree_code,
tree, tree, tree, tree *, tree *, int));
static int is_complete PROTO((tree));
static struct z_candidate * build_builtin_candidate
PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
int));
static struct z_candidate * add_conv_candidate
PROTO((struct z_candidate *, tree, tree, tree));
static struct z_candidate * add_function_candidate
PROTO((struct z_candidate *, tree, tree, int));
static tree implicit_conversion PROTO((tree, tree, tree, int));
static tree standard_conversion PROTO((tree, tree, tree));
static tree reference_binding PROTO((tree, tree, tree, int));
static tree strip_top_quals PROTO((tree));
static tree non_reference PROTO((tree));
static tree build_conv PROTO((enum tree_code, tree, tree));
static void print_n_candidates PROTO((struct candidate *, int));
static tree default_parm_conversions PROTO((tree, tree *));
static int is_subseq PROTO((tree, tree));
#define EVIL_RETURN(ARG) ((ARG).code = EVIL_CODE, (ARG))
#define STD_RETURN(ARG) ((ARG).code = STD_CODE, (ARG))
#define QUAL_RETURN(ARG) ((ARG).code = QUAL_CODE, (ARG))
#define TRIVIAL_RETURN(ARG) ((ARG).code = TRIVIAL_CODE, (ARG))
#define ZERO_RETURN(ARG) ((ARG).code = 0, (ARG))
/* Ordering function for overload resolution. Compare two candidates
by gross quality. */
int
rank_for_overload (x, y)
struct candidate *x, *y;
{
if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
return y->h.code - x->h.code;
if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
return -1;
/* This is set by compute_conversion_costs, for calling a non-const
member function from a const member function. */
if ((y->harshness[0].code & CONST_CODE) ^ (x->harshness[0].code & CONST_CODE))
return y->harshness[0].code - x->harshness[0].code;
if (y->h.code & STD_CODE)
{
if (x->h.code & STD_CODE)
return y->h.distance - x->h.distance;
return 1;
}
if (x->h.code & STD_CODE)
return -1;
return y->h.code - x->h.code;
}
/* Compare two candidates, argument by argument. */
static int
rank_for_ideal (x, y)
struct candidate *x, *y;
{
int i;
if (x->h_len != y->h_len)
abort ();
for (i = 0; i < x->h_len; i++)
{
if (y->harshness[i].code - x->harshness[i].code)
return y->harshness[i].code - x->harshness[i].code;
if ((y->harshness[i].code & STD_CODE)
&& (y->harshness[i].distance - x->harshness[i].distance))
return y->harshness[i].distance - x->harshness[i].distance;
/* They're both the same code. Now see if we're dealing with an
integral promotion that needs a finer grain of accuracy. */
if (y->harshness[0].code & PROMO_CODE
&& (y->harshness[i].int_penalty ^ x->harshness[i].int_penalty))
return y->harshness[i].int_penalty - x->harshness[i].int_penalty;
}
return 0;
}
/* TYPE is the type we wish to convert to. PARM is the parameter
we have to work with. We use a somewhat arbitrary cost function
to measure this conversion. */
static struct harshness_code
convert_harshness (type, parmtype, parm)
register tree type, parmtype;
tree parm;
{
struct harshness_code h;
register enum tree_code codel;
register enum tree_code coder;
int lvalue;
h.code = 0;
h.distance = 0;
h.int_penalty = 0;
#ifdef GATHER_STATISTICS
n_convert_harshness++;
#endif
if (TREE_CODE (parmtype) == REFERENCE_TYPE)
{
if (parm)
parm = convert_from_reference (parm);
parmtype = TREE_TYPE (parmtype);
lvalue = 1;
}
else if (parm)
lvalue = lvalue_p (parm);
else
lvalue = 0;
if (TYPE_PTRMEMFUNC_P (type))
type = TYPE_PTRMEMFUNC_FN_TYPE (type);
if (TYPE_PTRMEMFUNC_P (parmtype))
parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
codel = TREE_CODE (type);
coder = TREE_CODE (parmtype);
if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
return ZERO_RETURN (h);
if (coder == ERROR_MARK)
return EVIL_RETURN (h);
if (codel == REFERENCE_TYPE)
{
tree ttl, ttr;
int constp = parm ? TREE_READONLY (parm) : TYPE_READONLY (parmtype);
int volatilep = (parm ? TREE_THIS_VOLATILE (parm)
: TYPE_VOLATILE (parmtype));
register tree intype = TYPE_MAIN_VARIANT (parmtype);
register enum tree_code form = TREE_CODE (intype);
int penalty = 0;
ttl = TREE_TYPE (type);
/* Only allow const reference binding if we were given a parm to deal
with, since it isn't really a conversion. This is a hack to
prevent build_type_conversion from finding this conversion, but
still allow overloading to find it. */
if (! lvalue && ! (parm && TYPE_READONLY (ttl)))
return EVIL_RETURN (h);
if ((TYPE_READONLY (ttl) < constp)
|| (TYPE_VOLATILE (ttl) < volatilep))
return EVIL_RETURN (h);
/* When passing a non-const argument into a const reference, dig it a
little, so a non-const reference is preferred over this one. */
penalty = ((TYPE_READONLY (ttl) > constp)
+ (TYPE_VOLATILE (ttl) > volatilep));
ttl = TYPE_MAIN_VARIANT (ttl);
if (form == OFFSET_TYPE)
{
intype = TREE_TYPE (intype);
form = TREE_CODE (intype);
}
ttr = intype;
if (TREE_CODE (ttl) == ARRAY_TYPE && TREE_CODE (ttr) == ARRAY_TYPE)
{
if (comptypes (ttl, ttr, 1))
return ZERO_RETURN (h);
return EVIL_RETURN (h);
}
h = convert_harshness (ttl, ttr, NULL_TREE);
if (penalty && h.code == 0)
{
h.code = QUAL_CODE;
h.int_penalty = penalty;
}
return h;
}
if (codel == POINTER_TYPE && fntype_p (parmtype))
{
tree p1, p2;
struct harshness_code h1, h2;
/* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
type = TREE_TYPE (type);
if (coder == POINTER_TYPE)
{
parmtype = TREE_TYPE (parmtype);
coder = TREE_CODE (parmtype);
}
if (coder != TREE_CODE (type))
return EVIL_RETURN (h);
if (type != parmtype && coder == METHOD_TYPE)
{
tree ttl = TYPE_METHOD_BASETYPE (type);
tree ttr = TYPE_METHOD_BASETYPE (parmtype);
int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
if (b_or_d < 0)
{
b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
if (b_or_d < 0)
return EVIL_RETURN (h);
h.distance = -b_or_d;
}
else
h.distance = b_or_d;
h.code = STD_CODE;
type = build_function_type
(TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
parmtype = build_function_type
(TREE_TYPE (parmtype), TREE_CHAIN (TYPE_ARG_TYPES (parmtype)));
}
/* We allow the default conversion between function type
and pointer-to-function type for free. */
if (comptypes (type, parmtype, 1))
return h;
if (pedantic)
return EVIL_RETURN (h);
/* Compare return types. */
p1 = TREE_TYPE (type);
p2 = TREE_TYPE (parmtype);
h2 = convert_harshness (p1, p2, NULL_TREE);
if (h2.code & EVIL_CODE)
return h2;
h1.code = TRIVIAL_CODE;
h1.distance = 0;
if (h2.distance != 0)
{
tree binfo;
/* This only works for pointers. */
if (TREE_CODE (p1) != POINTER_TYPE
&& TREE_CODE (p1) != REFERENCE_TYPE)
return EVIL_RETURN (h);
p1 = TREE_TYPE (p1);
p2 = TREE_TYPE (p2);
/* Don't die if we happen to be dealing with void*. */
if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
return EVIL_RETURN (h);
if (h2.distance < 0)
binfo = get_binfo (p2, p1, 0);
else
binfo = get_binfo (p1, p2, 0);
if (! BINFO_OFFSET_ZEROP (binfo))
{
#if 0
static int explained = 0;
if (h2.distance < 0)
message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p2, p1);
else
message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p1, p2);
if (! explained++)
sorry ("(because pointer values change during conversion)");
#endif
return EVIL_RETURN (h);
}
}
h1.code |= h2.code;
if (h2.distance > h1.distance)
h1.distance = h2.distance;
p1 = TYPE_ARG_TYPES (type);
p2 = TYPE_ARG_TYPES (parmtype);
while (p1 && TREE_VALUE (p1) != void_type_node
&& p2 && TREE_VALUE (p2) != void_type_node)
{
h2 = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2),
NULL_TREE);
if (h2.code & EVIL_CODE)
return h2;
if (h2.distance)
{
/* This only works for pointers and references. */
if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
&& TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
return EVIL_RETURN (h);
h2.distance = - h2.distance;
}
h1.code |= h2.code;
if (h2.distance > h1.distance)
h1.distance = h2.distance;
p1 = TREE_CHAIN (p1);
p2 = TREE_CHAIN (p2);
}
if (p1 == p2)
return h1;
if (p2)
{
if (p1)
return EVIL_RETURN (h);
h1.code |= ELLIPSIS_CODE;
return h1;
}
if (p1)
{
if (TREE_PURPOSE (p1) == NULL_TREE)
h1.code |= EVIL_CODE;
return h1;
}
}
else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
{
tree ttl, ttr;
/* Get to the OFFSET_TYPE that this might be. */
type = TREE_TYPE (type);
if (coder != TREE_CODE (type))
return EVIL_RETURN (h);
ttl = TYPE_OFFSET_BASETYPE (type);
ttr = TYPE_OFFSET_BASETYPE (parmtype);
if (ttl == ttr)
h.code = 0;
else
{
int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
if (b_or_d < 0)
{
b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
if (b_or_d < 0)
return EVIL_RETURN (h);
h.distance = -b_or_d;
}
else
h.distance = b_or_d;
h.code = STD_CODE;
}
/* Now test the OFFSET_TYPE's target compatibility. */
type = TREE_TYPE (type);
parmtype = TREE_TYPE (parmtype);
}
if (coder == UNKNOWN_TYPE)
{
if (codel == FUNCTION_TYPE
|| codel == METHOD_TYPE
|| (codel == POINTER_TYPE
&& (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
|| TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
return TRIVIAL_RETURN (h);
return EVIL_RETURN (h);
}
if (coder == VOID_TYPE)
return EVIL_RETURN (h);
if (codel == BOOLEAN_TYPE)
{
if (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE)
return STD_RETURN (h);
else if (coder == POINTER_TYPE || coder == OFFSET_TYPE)
{
/* Make this worse than any conversion to another pointer.
FIXME this is how I think the language should work, but it may not
end up being how the language is standardized (jason 1/30/95). */
h.distance = 32767;
return STD_RETURN (h);
}
return EVIL_RETURN (h);
}
if (INTEGRAL_CODE_P (codel))
{
/* Control equivalence of ints an enums. */
if (codel == ENUMERAL_TYPE
&& flag_int_enum_equivalence == 0)
{
/* Enums can be converted to ints, but not vice-versa. */
if (coder != ENUMERAL_TYPE
|| TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
return EVIL_RETURN (h);
}
/* else enums and ints (almost) freely interconvert. */
if (INTEGRAL_CODE_P (coder))
{
if (TYPE_MAIN_VARIANT (type)
== TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
{
h.code = PROMO_CODE;
}
else
h.code = STD_CODE;
return h;
}
else if (coder == REAL_TYPE)
{
h.code = STD_CODE;
h.distance = 0;
return h;
}
}
if (codel == REAL_TYPE)
{
if (coder == REAL_TYPE)
{
if (TYPE_MAIN_VARIANT (type)
== TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
h.code = PROMO_CODE;
else
h.code = STD_CODE;
return h;
}
else if (INTEGRAL_CODE_P (coder))
{
h.code = STD_CODE;
h.distance = 0;
return h;
}
}
/* Convert arrays which have not previously been converted. */
if (coder == ARRAY_TYPE)
{
coder = POINTER_TYPE;
if (parm)
{
parm = decay_conversion (parm);
parmtype = TREE_TYPE (parm);
}
else
parmtype = build_pointer_type (TREE_TYPE (parmtype));
}
/* Conversions among pointers */
if (codel == POINTER_TYPE && coder == POINTER_TYPE)
{
register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
int penalty = 4 * (ttl != ttr);
/* Anything converts to void *. Since this may be `const void *'
(etc.) use VOID_TYPE instead of void_type_node. Otherwise, the
targets must be the same, except that we do allow (at some cost)
conversion between signed and unsigned pointer types. */
if ((TREE_CODE (ttl) == METHOD_TYPE
|| TREE_CODE (ttl) == FUNCTION_TYPE)
&& TREE_CODE (ttl) == TREE_CODE (ttr))
{
if (comptypes (ttl, ttr, -1))
{
h.code = penalty ? STD_CODE : 0;
h.distance = 0;
}
else
h.code = EVIL_CODE;
return h;
}
#if 1
if (TREE_CODE (ttl) != VOID_TYPE
&& (TREE_CODE (ttr) != VOID_TYPE || !parm || !null_ptr_cst_p (parm)))
{
if (comp_target_types (type, parmtype, 1) <= 0)
return EVIL_RETURN (h);
}
#else
if (!(TREE_CODE (ttl) == VOID_TYPE
|| TREE_CODE (ttr) == VOID_TYPE
|| (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
&& (ttl = unsigned_type (ttl),
ttr = unsigned_type (ttr),
penalty = 10, 0))
|| (comp_target_types (ttl, ttr, 0) > 0)))
return EVIL_RETURN (h);
#endif
if (ttr == ttl)
{
tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
h.code = 0;
/* Note conversion from `T*' to `const T*',
or `T*' to `volatile T*'. */
if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
|| (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
h.code = EVIL_CODE;
else if ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
|| (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2)))
h.code |= QUAL_CODE;
h.distance = 0;
return h;
}
if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
{
int b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
if (b_or_d < 0)
{
b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
if (b_or_d < 0)
return EVIL_RETURN (h);
h.distance = -b_or_d;
}
else
h.distance = b_or_d;
h.code = STD_CODE;
return h;
}
/* If converting from a `class*' to a `void*', make it
less favorable than any inheritance relationship. */
if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
{
h.code = STD_CODE;
h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
return h;
}
h.code = penalty ? STD_CODE : PROMO_CODE;
/* Catch things like `const char *' -> `const void *'
vs `const char *' -> `void *'. */
if (ttl != ttr)
{
tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
|| (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
h.code = EVIL_CODE;
else if ((TYPE_READONLY (tmp1) > TREE_READONLY (tmp2))
|| (TYPE_VOLATILE (tmp1) > TYPE_VOLATILE (tmp2)))
h.code |= QUAL_CODE;
}
return h;
}
if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
{
/* This is not a bad match, but don't let it beat
integer-enum combinations. */
if (parm && integer_zerop (parm))
{
h.code = STD_CODE;
h.distance = 0;
return h;
}
}
/* C++: Since the `this' parameter of a signature member function
is represented as a signature pointer to handle default implementations
correctly, we can have the case that `type' is a signature pointer
while `parmtype' is a pointer to a signature table. We don't really
do any conversions in this case, so just return 0. */
if (codel == RECORD_TYPE && coder == POINTER_TYPE
&& IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
return ZERO_RETURN (h);
if (codel == RECORD_TYPE && coder == RECORD_TYPE)
{
int b_or_d = get_base_distance (type, parmtype, 0, (tree*)0);
if (b_or_d < 0)
{
b_or_d = get_base_distance (parmtype, type, 0, (tree*)0);
if (b_or_d < 0)
return EVIL_RETURN (h);
h.distance = -b_or_d;
}
else
h.distance = b_or_d;
h.code = STD_CODE;
return h;
}
return EVIL_RETURN (h);
}
/* A clone of build_type_conversion for checking user-defined conversions in
overload resolution. */
static int
user_harshness (type, parmtype)
register tree type, parmtype;
{
tree conv;
tree winner = NULL_TREE;
int code;
{
tree typename = build_typename_overload (type);
if (lookup_fnfields (TYPE_BINFO (parmtype), typename, 0))
return 0;
}
for (conv = lookup_conversions (parmtype); conv; conv = TREE_CHAIN (conv))
{
struct harshness_code tmp;
tree cand = TREE_VALUE (conv);
if (winner && winner == cand)
continue;
tmp = convert_harshness (type, TREE_TYPE (TREE_TYPE (cand)), NULL_TREE);
if ((tmp.code < USER_CODE) && (tmp.distance >= 0))
{
if (winner)
return EVIL_CODE;
else
{
winner = cand;
code = tmp.code;
}
}
}
if (winner)
return code;
return -1;
}
#ifdef DEBUG_MATCHING
static char *
print_harshness (h)
struct harshness_code *h;
{
static char buf[1024];
char tmp[1024];
bzero (buf, 1024 * sizeof (char));
strcat (buf, "codes=[");
if (h->code & EVIL_CODE)
strcat (buf, "EVIL");
if (h->code & CONST_CODE)
strcat (buf, " CONST");
if (h->code & ELLIPSIS_CODE)
strcat (buf, " ELLIPSIS");
if (h->code & USER_CODE)
strcat (buf, " USER");
if (h->code & STD_CODE)
strcat (buf, " STD");
if (h->code & PROMO_CODE)
strcat (buf, " PROMO");
if (h->code & QUAL_CODE)
strcat (buf, " QUAL");
if (h->code & TRIVIAL_CODE)
strcat (buf, " TRIVIAL");
if (buf[0] == '\0')
strcat (buf, "0");
sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
strcat (buf, tmp);
return buf;
}
#endif
/* Algorithm: For each argument, calculate how difficult it is to
make FUNCTION accept that argument. If we can easily tell that
FUNCTION won't be acceptable to one of the arguments, then we
don't need to compute the ease of converting the other arguments,
since it will never show up in the intersection of all arguments'
favorite functions.
Conversions between builtin and user-defined types are allowed, but
no function involving such a conversion is preferred to one which
does not require such a conversion. Furthermore, such conversions
must be unique. */
void
compute_conversion_costs (function, tta_in, cp, arglen)
tree function;
tree tta_in;
struct candidate *cp;
int arglen;
{
tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
tree ttf = ttf_in;
tree tta = tta_in;
/* Start out with no strikes against. */
int evil_strikes = 0;
int ellipsis_strikes = 0;
int user_strikes = 0;
int b_or_d_strikes = 0;
int easy_strikes = 0;
int strike_index = 0, win;
struct harshness_code lose;
extern int cp_silent;
#ifdef GATHER_STATISTICS
n_compute_conversion_costs++;
#endif
#ifndef DEBUG_MATCHING
/* We don't emit any warnings or errors while trying out each candidate. */
cp_silent = 1;
#endif
cp->function = function;
cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
cp->u.bad_arg = 0; /* optimistic! */
cp->h.code = 0;
cp->h.distance = 0;
cp->h.int_penalty = 0;
bzero ((char *) cp->harshness,
(cp->h_len + 1) * sizeof (struct harshness_code));
while (ttf && tta)
{
struct harshness_code h;
if (ttf == void_list_node)
break;
if (type_unknown_p (TREE_VALUE (tta)))
{
/* Must perform some instantiation here. */
tree rhs = TREE_VALUE (tta);
tree lhstype = TREE_VALUE (ttf);
/* Keep quiet about possible contravariance violations. */
int old_inhibit_warnings = inhibit_warnings;
inhibit_warnings = 1;
/* @@ This is to undo what `grokdeclarator' does to
parameter types. It really should go through
something more general. */
TREE_TYPE (tta) = unknown_type_node;
rhs = instantiate_type (lhstype, rhs, 0);
inhibit_warnings = old_inhibit_warnings;
if (TREE_CODE (rhs) == ERROR_MARK)
h.code = EVIL_CODE;
else
h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
}
else
{
#ifdef DEBUG_MATCHING
static tree old_function = NULL_TREE;
if (!old_function || function != old_function)
{
cp_error ("trying %D", function);
old_function = function;
}
cp_error (" doing (%T) %E against arg %T",
TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
TREE_VALUE (ttf));
#endif
h = convert_harshness (TREE_VALUE (ttf),
TREE_TYPE (TREE_VALUE (tta)),
TREE_VALUE (tta));
#ifdef DEBUG_MATCHING
cp_error (" evaluated %s", print_harshness (&h));
#endif
}
cp->harshness[strike_index] = h;
if ((h.code & EVIL_CODE)
|| ((h.code & STD_CODE) && h.distance < 0))
{
cp->u.bad_arg = strike_index;
evil_strikes = 1;
}
else if (h.code & ELLIPSIS_CODE)
ellipsis_strikes += 1;
#if 0
/* This is never set by `convert_harshness'. */
else if (h.code & USER_CODE)
{
user_strikes += 1;
}
#endif
else
{
if ((h.code & STD_CODE) && h.distance)
{
if (h.distance > b_or_d_strikes)
b_or_d_strikes = h.distance;
}
else
easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
cp->h.code |= h.code;
/* Make sure we communicate this. */
cp->h.int_penalty += h.int_penalty;
}
ttf = TREE_CHAIN (ttf);
tta = TREE_CHAIN (tta);
strike_index += 1;
}
if (tta)
{
/* ran out of formals, and parmlist is fixed size. */
if (ttf /* == void_type_node */)
{
cp->h.code = EVIL_CODE;
cp->u.bad_arg = -1;
cp_silent = 0;
return;
}
else
{
struct harshness_code h;
int l = list_length (tta);
ellipsis_strikes += l;
h.code = ELLIPSIS_CODE;
h.distance = 0;
h.int_penalty = 0;
for (; l; --l)
cp->harshness[strike_index++] = h;
}
}
else if (ttf && ttf != void_list_node)
{
/* ran out of actuals, and no defaults. */
if (TREE_PURPOSE (ttf) == NULL_TREE)
{
cp->h.code = EVIL_CODE;
cp->u.bad_arg = -2;
cp_silent = 0;
return;
}
/* Store index of first default. */
cp->harshness[arglen].distance = strike_index+1;
}
else
cp->harshness[arglen].distance = 0;
/* Argument list lengths work out, so don't need to check them again. */
if (evil_strikes)
{
/* We do not check for derived->base conversions here, since in
no case would they give evil strike counts, unless such conversions
are somehow ambiguous. */
/* See if any user-defined conversions apply.
But make sure that we do not loop. */
static int dont_convert_types = 0;
if (dont_convert_types)
{
cp->h.code = EVIL_CODE;
cp_silent = 0;
return;
}
win = 0; /* Only get one chance to win. */
ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
tta = tta_in;
strike_index = 0;
evil_strikes = 0;
while (ttf && tta)
{
if (ttf == void_list_node)
break;
lose = cp->harshness[strike_index];
if ((lose.code & EVIL_CODE)
|| ((lose.code & STD_CODE) && lose.distance < 0))
{
tree actual_type = TREE_TYPE (TREE_VALUE (tta));
tree formal_type = TREE_VALUE (ttf);
int extra_conversions = 0;
dont_convert_types = 1;
if (TREE_CODE (formal_type) == REFERENCE_TYPE)
formal_type = TREE_TYPE (formal_type);
if (TREE_CODE (actual_type) == REFERENCE_TYPE)
actual_type = TREE_TYPE (actual_type);
if (formal_type != error_mark_node
&& actual_type != error_mark_node)
{
formal_type = complete_type (TYPE_MAIN_VARIANT (formal_type));
actual_type = complete_type (TYPE_MAIN_VARIANT (actual_type));
if (TYPE_HAS_CONSTRUCTOR (formal_type))
{
/* If it has a constructor for this type,