forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcast.d
3821 lines (3455 loc) · 126 KB
/
dcast.d
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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (c) 1999-2017 by Digital Mars, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(DMDSRC _dcast.d)
*/
module ddmd.dcast;
import core.stdc.stdio;
import core.stdc.string;
import ddmd.aggregate;
import ddmd.aliasthis;
import ddmd.arrayop;
import ddmd.arraytypes;
import ddmd.dclass;
import ddmd.declaration;
import ddmd.dscope;
import ddmd.dstruct;
import ddmd.dsymbol;
import ddmd.errors;
import ddmd.expression;
import ddmd.func;
import ddmd.globals;
import ddmd.impcnvtab;
import ddmd.init;
import ddmd.intrange;
import ddmd.mtype;
import ddmd.opover;
import ddmd.root.ctfloat;
import ddmd.root.outbuffer;
import ddmd.root.rmem;
import ddmd.tokens;
import ddmd.utf;
import ddmd.visitor;
enum LOG = false;
/**************************************
* Do an implicit cast.
* Issue error if it can't be done.
*/
extern (C++) Expression implicitCastTo(Expression e, Scope* sc, Type t)
{
extern (C++) final class ImplicitCastTo : Visitor
{
alias visit = super.visit;
public:
Type t;
Scope* sc;
Expression result;
extern (D) this(Scope* sc, Type t)
{
this.sc = sc;
this.t = t;
}
override void visit(Expression e)
{
//printf("Expression.implicitCastTo(%s of type %s) => %s\n", e.toChars(), e.type.toChars(), t.toChars());
MATCH match = e.implicitConvTo(t);
if (match)
{
if (match == MATCHconst && (e.type.constConv(t) || !e.isLvalue() && e.type.equivalent(t)))
{
/* Do not emit CastExp for const conversions and
* unique conversions on rvalue.
*/
result = e.copy();
result.type = t;
return;
}
result = e.castTo(sc, t);
return;
}
result = e.optimize(WANTvalue);
if (result != e)
{
result.accept(this);
return;
}
if (t.ty != Terror && e.type.ty != Terror)
{
if (!t.deco)
{
e.error("forward reference to type %s", t.toChars());
}
else
{
//printf("type %p ty %d deco %p\n", type, type.ty, type.deco);
//type = type.semantic(loc, sc);
//printf("type %s t %s\n", type.deco, t.deco);
e.error("cannot implicitly convert expression (%s) of type %s to %s",
e.toChars(), e.type.toChars(), t.toChars());
}
}
result = new ErrorExp();
}
override void visit(StringExp e)
{
//printf("StringExp::implicitCastTo(%s of type %s) => %s\n", e.toChars(), e.type.toChars(), t.toChars());
visit(cast(Expression)e);
if (result.op == TOKstring)
{
// Retain polysemous nature if it started out that way
(cast(StringExp)result).committed = e.committed;
}
}
override void visit(ErrorExp e)
{
result = e;
}
override void visit(FuncExp e)
{
//printf("FuncExp::implicitCastTo type = %p %s, t = %s\n", e.type, e.type ? e.type.toChars() : NULL, t.toChars());
FuncExp fe;
if (e.matchType(t, sc, &fe) > MATCHnomatch)
{
result = fe;
return;
}
visit(cast(Expression)e);
}
override void visit(ArrayLiteralExp e)
{
visit(cast(Expression)e);
Type tb = result.type.toBasetype();
if (tb.ty == Tarray)
semanticTypeInfo(sc, (cast(TypeDArray)tb).next);
}
override void visit(SliceExp e)
{
visit(cast(Expression)e);
if (result.op != TOKslice)
return;
e = cast(SliceExp)result;
if (e.e1.op == TOKarrayliteral)
{
ArrayLiteralExp ale = cast(ArrayLiteralExp)e.e1;
Type tb = t.toBasetype();
Type tx;
if (tb.ty == Tsarray)
tx = tb.nextOf().sarrayOf(ale.elements ? ale.elements.dim : 0);
else
tx = tb.nextOf().arrayOf();
e.e1 = ale.implicitCastTo(sc, tx);
}
}
}
scope ImplicitCastTo v = new ImplicitCastTo(sc, t);
e.accept(v);
return v.result;
}
/*******************************************
* Return MATCH level of implicitly converting e to type t.
* Don't do the actual cast; don't change e.
*/
extern (C++) MATCH implicitConvTo(Expression e, Type t)
{
extern (C++) final class ImplicitConvTo : Visitor
{
alias visit = super.visit;
public:
Type t;
MATCH result;
extern (D) this(Type t)
{
this.t = t;
result = MATCHnomatch;
}
override void visit(Expression e)
{
version (none)
{
printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
//static int nest; if (++nest == 10) assert(0);
if (t == Type.terror)
return;
if (!e.type)
{
e.error("%s is not an expression", e.toChars());
e.type = Type.terror;
}
Expression ex = e.optimize(WANTvalue);
if (ex.type.equals(t))
{
result = MATCHexact;
return;
}
if (ex != e)
{
//printf("\toptimized to %s of type %s\n", e.toChars(), e.type.toChars());
result = ex.implicitConvTo(t);
return;
}
MATCH match = e.type.implicitConvTo(t);
if (match != MATCHnomatch)
{
result = match;
return;
}
/* See if we can do integral narrowing conversions
*/
if (e.type.isintegral() && t.isintegral() && e.type.isTypeBasic() && t.isTypeBasic())
{
IntRange src = getIntRange(e);
IntRange target = IntRange.fromType(t);
if (target.contains(src))
{
result = MATCHconvert;
return;
}
}
}
/******
* Given expression e of type t, see if we can implicitly convert e
* to type tprime, where tprime is type t with mod bits added.
* Returns:
* match level
*/
static MATCH implicitMod(Expression e, Type t, MOD mod)
{
Type tprime;
if (t.ty == Tpointer)
tprime = t.nextOf().castMod(mod).pointerTo();
else if (t.ty == Tarray)
tprime = t.nextOf().castMod(mod).arrayOf();
else if (t.ty == Tsarray)
tprime = t.nextOf().castMod(mod).sarrayOf(t.size() / t.nextOf().size());
else
tprime = t.castMod(mod);
return e.implicitConvTo(tprime);
}
static MATCH implicitConvToAddMin(BinExp e, Type t)
{
/* Is this (ptr +- offset)? If so, then ask ptr
* if the conversion can be done.
* This is to support doing things like implicitly converting a mutable unique
* pointer to an immutable pointer.
*/
Type tb = t.toBasetype();
Type typeb = e.type.toBasetype();
if (typeb.ty != Tpointer || tb.ty != Tpointer)
return MATCHnomatch;
Type t1b = e.e1.type.toBasetype();
Type t2b = e.e2.type.toBasetype();
if (t1b.ty == Tpointer && t2b.isintegral() && t1b.equivalent(tb))
{
// ptr + offset
// ptr - offset
MATCH m = e.e1.implicitConvTo(t);
return (m > MATCHconst) ? MATCHconst : m;
}
if (t2b.ty == Tpointer && t1b.isintegral() && t2b.equivalent(tb))
{
// offset + ptr
MATCH m = e.e2.implicitConvTo(t);
return (m > MATCHconst) ? MATCHconst : m;
}
return MATCHnomatch;
}
override void visit(AddExp e)
{
version (none)
{
printf("AddExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
visit(cast(Expression)e);
if (result == MATCHnomatch)
result = implicitConvToAddMin(e, t);
}
override void visit(MinExp e)
{
version (none)
{
printf("MinExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
visit(cast(Expression)e);
if (result == MATCHnomatch)
result = implicitConvToAddMin(e, t);
}
override void visit(IntegerExp e)
{
version (none)
{
printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
MATCH m = e.type.implicitConvTo(t);
if (m >= MATCHconst)
{
result = m;
return;
}
TY ty = e.type.toBasetype().ty;
TY toty = t.toBasetype().ty;
TY oldty = ty;
if (m == MATCHnomatch && t.ty == Tenum)
return;
if (t.ty == Tvector)
{
TypeVector tv = cast(TypeVector)t;
TypeBasic tb = tv.elementType();
if (tb.ty == Tvoid)
return;
toty = tb.ty;
}
switch (ty)
{
case Tbool:
case Tint8:
case Tchar:
case Tuns8:
case Tint16:
case Tuns16:
case Twchar:
ty = Tint32;
break;
case Tdchar:
ty = Tuns32;
break;
default:
break;
}
// Only allow conversion if no change in value
dinteger_t value = e.toInteger();
switch (toty)
{
case Tbool:
if ((value & 1) != value)
return;
break;
case Tint8:
if (ty == Tuns64 && value & ~0x7FU)
return;
else if (cast(byte)value != value)
return;
break;
case Tchar:
if ((oldty == Twchar || oldty == Tdchar) && value > 0x7F)
return;
goto case Tuns8;
case Tuns8:
//printf("value = %llu %llu\n", (dinteger_t)(unsigned char)value, value);
if (cast(ubyte)value != value)
return;
break;
case Tint16:
if (ty == Tuns64 && value & ~0x7FFFU)
return;
else if (cast(short)value != value)
return;
break;
case Twchar:
if (oldty == Tdchar && value > 0xD7FF && value < 0xE000)
return;
goto case Tuns16;
case Tuns16:
if (cast(ushort)value != value)
return;
break;
case Tint32:
if (ty == Tuns32)
{
}
else if (ty == Tuns64 && value & ~0x7FFFFFFFU)
return;
else if (cast(int)value != value)
return;
break;
case Tuns32:
if (ty == Tint32)
{
}
else if (cast(uint)value != value)
return;
break;
case Tdchar:
if (value > 0x10FFFFU)
return;
break;
case Tfloat32:
{
float f;
if (e.type.isunsigned())
{
f = cast(float)value;
if (f != value)
return;
}
else
{
f = cast(float)cast(sinteger_t)value;
if (f != cast(sinteger_t)value)
return;
}
break;
}
case Tfloat64:
{
double f;
if (e.type.isunsigned())
{
f = cast(double)value;
if (f != value)
return;
}
else
{
f = cast(double)cast(sinteger_t)value;
if (f != cast(sinteger_t)value)
return;
}
break;
}
case Tfloat80:
{
if (e.type.isunsigned())
{
const f = real_t(value);
if (cast(dinteger_t)f != value) // isn't this a noop, because the compiler prefers ld
return;
}
else
{
const f = real_t(cast(sinteger_t)value);
if (cast(sinteger_t)f != cast(sinteger_t)value)
return;
}
break;
}
case Tpointer:
//printf("type = %s\n", type.toBasetype()->toChars());
//printf("t = %s\n", t.toBasetype()->toChars());
if (ty == Tpointer && e.type.toBasetype().nextOf().ty == t.toBasetype().nextOf().ty)
{
/* Allow things like:
* const char* P = cast(char *)3;
* char* q = P;
*/
break;
}
goto default;
default:
visit(cast(Expression)e);
return;
}
//printf("MATCHconvert\n");
result = MATCHconvert;
}
override void visit(ErrorExp e)
{
// no match
}
override void visit(NullExp e)
{
version (none)
{
printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s, committed = %d)\n", e.toChars(), e.type.toChars(), t.toChars(), e.committed);
}
if (e.type.equals(t))
{
result = MATCHexact;
return;
}
/* Allow implicit conversions from immutable to mutable|const,
* and mutable to immutable. It works because, after all, a null
* doesn't actually point to anything.
*/
if (t.equivalent(e.type))
{
result = MATCHconst;
return;
}
visit(cast(Expression)e);
}
override void visit(StructLiteralExp e)
{
version (none)
{
printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
visit(cast(Expression)e);
if (result != MATCHnomatch)
return;
if (e.type.ty == t.ty && e.type.ty == Tstruct && (cast(TypeStruct)e.type).sym == (cast(TypeStruct)t).sym)
{
result = MATCHconst;
for (size_t i = 0; i < e.elements.dim; i++)
{
Expression el = (*e.elements)[i];
if (!el)
continue;
Type te = el.type;
te = e.sd.fields[i].type.addMod(t.mod);
MATCH m2 = el.implicitConvTo(te);
//printf("\t%s => %s, match = %d\n", el.toChars(), te.toChars(), m2);
if (m2 < result)
result = m2;
}
}
}
override void visit(StringExp e)
{
version (none)
{
printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n", e.toChars(), e.committed, e.type.toChars(), t.toChars());
}
if (!e.committed && t.ty == Tpointer && t.nextOf().ty == Tvoid)
return;
if (e.type.ty == Tsarray || e.type.ty == Tarray || e.type.ty == Tpointer)
{
TY tyn = e.type.nextOf().ty;
if (tyn == Tchar || tyn == Twchar || tyn == Tdchar)
{
switch (t.ty)
{
case Tsarray:
if (e.type.ty == Tsarray)
{
TY tynto = t.nextOf().ty;
if (tynto == tyn)
{
if ((cast(TypeSArray)e.type).dim.toInteger() == (cast(TypeSArray)t).dim.toInteger())
{
result = MATCHexact;
}
return;
}
if (tynto == Tchar || tynto == Twchar || tynto == Tdchar)
{
if (e.committed && tynto != tyn)
return;
size_t fromlen = e.numberOfCodeUnits(tynto);
size_t tolen = cast(size_t)(cast(TypeSArray)t).dim.toInteger();
if (tolen < fromlen)
return;
if (tolen != fromlen)
{
// implicit length extending
result = MATCHconvert;
return;
}
}
if (!e.committed && (tynto == Tchar || tynto == Twchar || tynto == Tdchar))
{
result = MATCHexact;
return;
}
}
else if (e.type.ty == Tarray)
{
TY tynto = t.nextOf().ty;
if (tynto == Tchar || tynto == Twchar || tynto == Tdchar)
{
if (e.committed && tynto != tyn)
return;
size_t fromlen = e.numberOfCodeUnits(tynto);
size_t tolen = cast(size_t)(cast(TypeSArray)t).dim.toInteger();
if (tolen < fromlen)
return;
if (tolen != fromlen)
{
// implicit length extending
result = MATCHconvert;
return;
}
}
if (tynto == tyn)
{
result = MATCHexact;
return;
}
if (!e.committed && (tynto == Tchar || tynto == Twchar || tynto == Tdchar))
{
result = MATCHexact;
return;
}
}
goto case; /+ fall through +/
case Tarray:
case Tpointer:
Type tn = t.nextOf();
MATCH m = MATCHexact;
if (e.type.nextOf().mod != tn.mod)
{
if (!tn.isConst())
return;
m = MATCHconst;
}
if (!e.committed)
{
switch (tn.ty)
{
case Tchar:
if (e.postfix == 'w' || e.postfix == 'd')
m = MATCHconvert;
result = m;
return;
case Twchar:
if (e.postfix != 'w')
m = MATCHconvert;
result = m;
return;
case Tdchar:
if (e.postfix != 'd')
m = MATCHconvert;
result = m;
return;
default:
break;
}
}
break;
default:
break;
}
}
}
visit(cast(Expression)e);
}
override void visit(ArrayLiteralExp e)
{
version (none)
{
printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
Type tb = t.toBasetype();
Type typeb = e.type.toBasetype();
if ((tb.ty == Tarray || tb.ty == Tsarray) &&
(typeb.ty == Tarray || typeb.ty == Tsarray))
{
result = MATCHexact;
Type typen = typeb.nextOf().toBasetype();
if (tb.ty == Tsarray)
{
TypeSArray tsa = cast(TypeSArray)tb;
if (e.elements.dim != tsa.dim.toInteger())
result = MATCHnomatch;
}
Type telement = tb.nextOf();
if (!e.elements.dim)
{
if (typen.ty != Tvoid)
result = typen.implicitConvTo(telement);
}
else
{
if (e.basis)
{
MATCH m = e.basis.implicitConvTo(telement);
if (m < result)
result = m;
}
for (size_t i = 0; i < e.elements.dim; i++)
{
Expression el = (*e.elements)[i];
if (result == MATCHnomatch)
break;
if (!el)
continue;
MATCH m = el.implicitConvTo(telement);
if (m < result)
result = m; // remember worst match
}
}
if (!result)
result = e.type.implicitConvTo(t);
return;
}
else if (tb.ty == Tvector && (typeb.ty == Tarray || typeb.ty == Tsarray))
{
result = MATCHexact;
// Convert array literal to vector type
TypeVector tv = cast(TypeVector)tb;
TypeSArray tbase = cast(TypeSArray)tv.basetype;
assert(tbase.ty == Tsarray);
const edim = e.elements.dim;
const tbasedim = tbase.dim.toInteger();
if (edim > tbasedim)
{
result = MATCHnomatch;
return;
}
Type telement = tv.elementType();
if (edim < tbasedim)
{
Expression el = typeb.nextOf.defaultInitLiteral(e.loc);
MATCH m = el.implicitConvTo(telement);
if (m < result)
result = m; // remember worst match
}
foreach (i; 0 .. edim)
{
Expression el = (*e.elements)[i];
MATCH m = el.implicitConvTo(telement);
if (m < result)
result = m; // remember worst match
if (result == MATCHnomatch)
break; // no need to check for worse
}
return;
}
visit(cast(Expression)e);
}
override void visit(AssocArrayLiteralExp e)
{
Type tb = t.toBasetype();
Type typeb = e.type.toBasetype();
if (tb.ty == Taarray && typeb.ty == Taarray)
{
result = MATCHexact;
for (size_t i = 0; i < e.keys.dim; i++)
{
Expression el = (*e.keys)[i];
MATCH m = el.implicitConvTo((cast(TypeAArray)tb).index);
if (m < result)
result = m; // remember worst match
if (result == MATCHnomatch)
break; // no need to check for worse
el = (*e.values)[i];
m = el.implicitConvTo(tb.nextOf());
if (m < result)
result = m; // remember worst match
if (result == MATCHnomatch)
break; // no need to check for worse
}
return;
}
else
visit(cast(Expression)e);
}
override void visit(CallExp e)
{
enum LOG = 0;
static if (LOG)
{
printf("CallExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
visit(cast(Expression)e);
if (result != MATCHnomatch)
return;
/* Allow the result of strongly pure functions to
* convert to immutable
*/
if (e.f && e.f.isolateReturn())
{
result = e.type.immutableOf().implicitConvTo(t);
if (result > MATCHconst) // Match level is MATCHconst at best.
result = MATCHconst;
return;
}
/* Conversion is 'const' conversion if:
* 1. function is pure (weakly pure is ok)
* 2. implicit conversion only fails because of mod bits
* 3. each function parameter can be implicitly converted to the mod bits
*/
Type tx = e.f ? e.f.type : e.e1.type;
tx = tx.toBasetype();
if (tx.ty != Tfunction)
return;
TypeFunction tf = cast(TypeFunction)tx;
if (tf.purity == PUREimpure)
return;
if (e.f && e.f.isNested())
return;
/* See if fail only because of mod bits.
*
* Bugzilla 14155: All pure functions can access global immutable data.
* So the returned pointer may refer an immutable global data,
* and then the returned pointer that points non-mutable object
* cannot be unique pointer.
*
* Example:
* immutable g;
* static this() { g = 1; }
* const(int*) foo() pure { return &g; }
* void test() {
* immutable(int*) ip = foo(); // OK
* int* mp = foo(); // should be disallowed
* }
*/
if (e.type.immutableOf().implicitConvTo(t) < MATCHconst && e.type.addMod(MODshared).implicitConvTo(t) < MATCHconst && e.type.implicitConvTo(t.addMod(MODshared)) < MATCHconst)
{
return;
}
// Allow a conversion to immutable type, or
// conversions of mutable types between thread-local and shared.
/* Get mod bits of what we're converting to
*/
Type tb = t.toBasetype();
MOD mod = tb.mod;
if (tf.isref)
{
}
else
{
Type ti = getIndirection(t);
if (ti)
mod = ti.mod;
}
static if (LOG)
{
printf("mod = x%x\n", mod);
}
if (mod & MODwild)
return; // not sure what to do with this
/* Apply mod bits to each function parameter,
* and see if we can convert the function argument to the modded type
*/
size_t nparams = Parameter.dim(tf.parameters);
size_t j = (tf.linkage == LINKd && tf.varargs == 1); // if TypeInfoArray was prepended
if (e.e1.op == TOKdotvar)
{
/* Treat 'this' as just another function argument
*/
DotVarExp dve = cast(DotVarExp)e.e1;
Type targ = dve.e1.type;
if (targ.constConv(targ.castMod(mod)) == MATCHnomatch)
return;
}
for (size_t i = j; i < e.arguments.dim; ++i)
{
Expression earg = (*e.arguments)[i];
Type targ = earg.type.toBasetype();
static if (LOG)
{
printf("[%d] earg: %s, targ: %s\n", cast(int)i, earg.toChars(), targ.toChars());
}
if (i - j < nparams)
{
Parameter fparam = Parameter.getNth(tf.parameters, i - j);
if (fparam.storageClass & STClazy)
return; // not sure what to do with this
Type tparam = fparam.type;
if (!tparam)
continue;
if (fparam.storageClass & (STCout | STCref))
{
if (targ.constConv(tparam.castMod(mod)) == MATCHnomatch)
return;
continue;
}
}
static if (LOG)
{
printf("[%d] earg: %s, targm: %s\n", cast(int)i, earg.toChars(), targ.addMod(mod).toChars());
}
if (implicitMod(earg, targ, mod) == MATCHnomatch)
return;
}
/* Success
*/
result = MATCHconst;
}
override void visit(AddrExp e)
{
version (none)
{
printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
result = e.type.implicitConvTo(t);
//printf("\tresult = %d\n", result);
if (result != MATCHnomatch)
return;
Type tb = t.toBasetype();
Type typeb = e.type.toBasetype();
// Look for pointers to functions where the functions are overloaded.
if (e.e1.op == TOKoverloadset &&
(tb.ty == Tpointer || tb.ty == Tdelegate) && tb.nextOf().ty == Tfunction)
{
OverExp eo = cast(OverExp)e.e1;
FuncDeclaration f = null;
for (size_t i = 0; i < eo.vars.a.dim; i++)
{
Dsymbol s = eo.vars.a[i];
FuncDeclaration f2 = s.isFuncDeclaration();
assert(f2);
if (f2.overloadExactMatch(tb.nextOf()))
{
if (f)
{
/* Error if match in more than one overload set,
* even if one is a 'better' match than the other.
*/
ScopeDsymbol.multiplyDefined(e.loc, f, f2);
}
else
f = f2;
result = MATCHexact;
}
}
}
if (e.e1.op == TOKvar &&
typeb.ty == Tpointer && typeb.nextOf().ty == Tfunction &&
tb.ty == Tpointer && tb.nextOf().ty == Tfunction)
{
/* I don't think this can ever happen -
* it should have been
* converted to a SymOffExp.
*/
assert(0);
}
//printf("\tresult = %d\n", result);
}
override void visit(SymOffExp e)
{
version (none)
{
printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
}
result = e.type.implicitConvTo(t);
//printf("\tresult = %d\n", result);