-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathinline.d
2457 lines (2207 loc) · 74.1 KB
/
inline.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
/**
* Performs inlining, which is an optimization pass enabled with the `-inline` flag.
*
* The AST is traversed, and every function call is considered for inlining using `inlinecost.d`.
* The function call is then inlined if this cost is below a threshold.
*
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/inline.d, _inline.d)
* Documentation: https://dlang.org/phobos/dmd_inline.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/inline.d
*/
module dmd.inline;
import core.stdc.stdio;
import core.stdc.string;
import dmd.aggregate;
import dmd.arraytypes;
import dmd.astenums;
import dmd.attrib;
import dmd.declaration;
import dmd.dmodule;
import dmd.dscope;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dsymbolsem : include;
import dmd.dtemplate;
import dmd.expression;
import dmd.expressionsem : semanticTypeInfo;
import dmd.errors;
import dmd.func;
import dmd.funcsem;
import dmd.globals;
import dmd.hdrgen;
import dmd.id;
import dmd.identifier;
import dmd.init;
import dmd.initsem;
import dmd.location;
import dmd.mtype;
import dmd.opover;
import dmd.printast;
import dmd.statement;
import dmd.tokens;
import dmd.typesem : pointerTo, sarrayOf;
import dmd.visitor;
import dmd.visitor.postorder;
import dmd.inlinecost;
/***********************************************************
* Scan function implementations in Module m looking for functions that can be inlined,
* and inline them in situ.
*
* Params:
* m = module to scan
*/
public void inlineScanModule(Module m)
{
if (m.semanticRun != PASS.semantic3done)
return;
m.semanticRun = PASS.inline;
// Note that modules get their own scope, from scratch.
// This is so regardless of where in the syntax a module
// gets imported, it is unaffected by context.
//printf("Module = %p\n", m.sc.scopesym);
foreach (i; 0 .. m.members.length)
{
Dsymbol s = (*m.members)[i];
//if (global.params.v.verbose)
// message("inline scan symbol %s", s.toChars());
inlineScanDsymbol(s);
}
m.semanticRun = PASS.inlinedone;
}
private void inlineScanDsymbol(Dsymbol s)
{
scope InlineScanVisitorDsymbol v = new InlineScanVisitorDsymbol();
s.accept(v);
}
/***********************************************************
* Perform the "inline copying" of a default argument for a function parameter.
*
* Todo:
* The hack for https://issues.dlang.org/show_bug.cgi?id=4820 case is still questionable.
* Perhaps would have to handle a delegate expression with 'null' context properly in front-end.
*/
public Expression inlineCopy(Expression e, Scope* sc)
{
/* See https://issues.dlang.org/show_bug.cgi?id=2935
* for explanation of why just a copy() is broken
*/
//return e.copy();
if (auto de = e.isDelegateExp())
{
if (de.func.isNested())
{
/* https://issues.dlang.org/show_bug.cgi?id=4820
* Defer checking until later if we actually need the 'this' pointer
*/
return de.copy();
}
}
const cost = inlineCostExpression(e);
if (cost >= COST_MAX)
{
error(e.loc, "cannot inline default argument `%s`", e.toChars());
return ErrorExp.get();
}
scope ids = new InlineDoState(sc.parent, null);
return doInlineAs!Expression(e, ids);
}
private:
enum LOG = false;
enum CANINLINE_LOG = false;
enum EXPANDINLINE_LOG = false;
/***********************************************************
* Represent a context to inline statements and expressions.
*
* Todo:
* It would be better to make foundReturn an instance field of DoInlineAs visitor class,
* like as DoInlineAs!Result.result field, because it's one another result of inlining.
* The best would be to return a pair of result Expression and a bool value as foundReturn
* from doInlineAs function.
*/
private final class InlineDoState
{
// inline context
VarDeclaration vthis;
Dsymbols from; // old Dsymbols
Dsymbols to; // parallel array of new Dsymbols
Dsymbol parent; // new parent
FuncDeclaration fd; // function being inlined (old parent)
// inline result
bool foundReturn;
this(Dsymbol parent, FuncDeclaration fd) scope
{
this.parent = parent;
this.fd = fd;
}
}
/***********************************************************
* Perform the inlining from (Statement or Expression) to (Statement or Expression).
*
* Inlining is done by:
* - Converting to an Expression
* - Copying the trees of the function to be inlined
* - Renaming the variables
*/
private extern (C++) final class DoInlineAs(Result) : Visitor
if (is(Result == Statement) || is(Result == Expression))
{
alias visit = Visitor.visit;
public:
InlineDoState ids;
Result result;
enum asStatements = is(Result == Statement);
extern (D) this(InlineDoState ids) scope
{
this.ids = ids;
}
// Statement -> (Statement | Expression)
override void visit(Statement s)
{
printf("Statement.doInlineAs!%s()\n%s\n", Result.stringof.ptr, s.toChars());
fflush(stdout);
assert(0); // default is we can't inline it
}
override void visit(ExpStatement s)
{
static if (LOG)
{
if (s.exp)
printf("ExpStatement.doInlineAs!%s() '%s'\n", Result.stringof.ptr, s.exp.toChars());
}
auto exp = doInlineAs!Expression(s.exp, ids);
static if (asStatements)
result = new ExpStatement(s.loc, exp);
else
result = exp;
}
override void visit(CompoundStatement s)
{
//printf("CompoundStatement.doInlineAs!%s() %d\n", Result.stringof.ptr, s.statements.length);
static if (asStatements)
{
auto as = new Statements();
as.reserve(s.statements.length);
}
foreach (i, sx; *s.statements)
{
if (!sx)
continue;
static if (asStatements)
{
as.push(doInlineAs!Statement(sx, ids));
}
else
{
/* Specifically allow:
* if (condition)
* return exp1;
* return exp2;
*/
IfStatement ifs;
Statement s3;
if ((ifs = sx.isIfStatement()) !is null &&
ifs.ifbody &&
ifs.ifbody.endsWithReturnStatement() &&
!ifs.elsebody &&
i + 1 < s.statements.length &&
(s3 = (*s.statements)[i + 1]) !is null &&
s3.endsWithReturnStatement()
)
{
/* Rewrite as ?:
*/
auto econd = doInlineAs!Expression(ifs.condition, ids);
assert(econd);
auto e1 = doInlineAs!Expression(ifs.ifbody, ids);
assert(ids.foundReturn);
auto e2 = doInlineAs!Expression(s3, ids);
assert(e2);
Expression e = new CondExp(econd.loc, econd, e1, e2);
e.type = e1.type;
if (e.type.ty == Ttuple)
{
e1.type = Type.tvoid;
e2.type = Type.tvoid;
e.type = Type.tvoid;
}
result = Expression.combine(result, e);
}
else
{
ids.foundReturn = false;
auto e = doInlineAs!Expression(sx, ids);
result = Expression.combine(result, e);
}
}
if (ids.foundReturn)
break;
}
static if (asStatements)
result = new CompoundStatement(s.loc, as);
}
override void visit(UnrolledLoopStatement s)
{
//printf("UnrolledLoopStatement.doInlineAs!%s() %d\n", Result.stringof.ptr, s.statements.length);
static if (asStatements)
{
auto as = new Statements();
as.reserve(s.statements.length);
}
foreach (sx; *s.statements)
{
if (!sx)
continue;
auto r = doInlineAs!Result(sx, ids);
static if (asStatements)
as.push(r);
else
result = Expression.combine(result, r);
if (ids.foundReturn)
break;
}
static if (asStatements)
result = new UnrolledLoopStatement(s.loc, as);
}
override void visit(ScopeStatement s)
{
//printf("ScopeStatement.doInlineAs!%s() %d\n", Result.stringof.ptr, s.statement.length);
auto r = doInlineAs!Result(s.statement, ids);
static if (asStatements)
result = new ScopeStatement(s.loc, r, s.endloc);
else
result = r;
}
override void visit(IfStatement s)
{
assert(!s.param);
auto econd = doInlineAs!Expression(s.condition, ids);
assert(econd);
auto ifbody = doInlineAs!Result(s.ifbody, ids);
bool bodyReturn = ids.foundReturn;
ids.foundReturn = false;
auto elsebody = doInlineAs!Result(s.elsebody, ids);
static if (asStatements)
{
result = new IfStatement(s.loc, s.param, econd, ifbody, elsebody, s.endloc);
}
else
{
alias e1 = ifbody;
alias e2 = elsebody;
if (e1 && e2)
{
result = new CondExp(econd.loc, econd, e1, e2);
result.type = e1.type;
if (result.type.ty == Ttuple)
{
e1.type = Type.tvoid;
e2.type = Type.tvoid;
result.type = Type.tvoid;
}
}
else if (e1)
{
result = new LogicalExp(econd.loc, EXP.andAnd, econd, e1);
result.type = Type.tvoid;
}
else if (e2)
{
result = new LogicalExp(econd.loc, EXP.orOr, econd, e2);
result.type = Type.tvoid;
}
else
{
result = econd;
}
}
ids.foundReturn = ids.foundReturn && bodyReturn;
}
override void visit(ReturnStatement s)
{
//printf("ReturnStatement.doInlineAs!%s() '%s'\n", Result.stringof.ptr, s.exp ? s.exp.toChars() : "");
ids.foundReturn = true;
auto exp = doInlineAs!Expression(s.exp, ids);
if (!exp) // https://issues.dlang.org/show_bug.cgi?id=14560
// 'return' must not leave in the expand result
return;
static if (asStatements)
{
/* Any return statement should be the last statement in the function being
* inlined, otherwise things shouldn't have gotten this far. Since the
* return value is being ignored (otherwise it wouldn't be inlined as a statement)
* we only need to evaluate `exp` for side effects.
* Already disallowed this if `exp` produces an object that needs destruction -
* an enhancement would be to do the destruction here.
*/
result = new ExpStatement(s.loc, exp);
}
else
result = exp;
}
override void visit(ImportStatement s)
{
//printf("ImportStatement.doInlineAs!%s()\n", Result.stringof.ptr);
}
override void visit(ForStatement s)
{
//printf("ForStatement.doInlineAs!%s()\n", Result.stringof.ptr);
static if (asStatements)
{
auto sinit = doInlineAs!Statement(s._init, ids);
auto scond = doInlineAs!Expression(s.condition, ids);
auto sincr = doInlineAs!Expression(s.increment, ids);
auto sbody = doInlineAs!Statement(s._body, ids);
result = new ForStatement(s.loc, sinit, scond, sincr, sbody, s.endloc);
}
else
result = null; // cannot be inlined as an Expression
}
override void visit(ThrowStatement s)
{
//printf("ThrowStatement.doInlineAs!%s() '%s'\n", Result.stringof.ptr, s.exp.toChars());
static if (asStatements)
result = new ThrowStatement(s.loc, doInlineAs!Expression(s.exp, ids));
else
result = null; // cannot be inlined as an Expression
}
// Expression -> (Statement | Expression)
static if (asStatements)
{
override void visit(Expression e)
{
result = new ExpStatement(e.loc, doInlineAs!Expression(e, ids));
}
}
else
{
/******************************
* Perform doInlineAs() on an array of Expressions.
*/
Expressions* arrayExpressionDoInline(Expressions* a)
{
if (!a)
return null;
auto newa = new Expressions(a.length);
foreach (i; 0 .. a.length)
{
(*newa)[i] = doInlineAs!Expression((*a)[i], ids);
}
return newa;
}
override void visit(Expression e)
{
//printf("Expression.doInlineAs!%s(%s): %s\n", Result.stringof.ptr, EXPtoString(e.op).ptr, e.toChars());
result = e.copy();
}
override void visit(SymOffExp e)
{
//printf("SymOffExp.doInlineAs!%s(%s)\n", Result.stringof.ptr, e.toChars());
foreach (i; 0 .. ids.from.length)
{
if (e.var != ids.from[i])
continue;
auto se = e.copy().isSymOffExp();
se.var = ids.to[i].isDeclaration();
result = se;
return;
}
result = e;
}
override void visit(VarExp e)
{
//printf("VarExp.doInlineAs!%s(%s)\n", Result.stringof.ptr, e.toChars());
foreach (i; 0 .. ids.from.length)
{
if (e.var != ids.from[i])
continue;
auto ve = e.copy().isVarExp();
ve.var = ids.to[i].isDeclaration();
result = ve;
return;
}
if (ids.fd && e.var == ids.fd.vthis)
{
result = new VarExp(e.loc, ids.vthis);
if (ids.fd.hasDualContext())
result = new AddrExp(e.loc, result);
result.type = e.type;
return;
}
/* Inlining context pointer access for nested referenced variables.
* For example:
* auto fun() {
* int i = 40;
* auto foo() {
* int g = 2;
* struct Result {
* auto bar() { return i + g; }
* }
* return Result();
* }
* return foo();
* }
* auto t = fun();
* 'i' and 'g' are nested referenced variables in Result.bar(), so:
* auto x = t.bar();
* should be inlined to:
* auto x = *(t.vthis.vthis + i.voffset) + *(t.vthis + g.voffset)
*/
auto v = e.var.isVarDeclaration();
if (v && v.nestedrefs.length && ids.vthis)
{
Dsymbol s = ids.fd;
auto fdv = v.toParent().isFuncDeclaration();
assert(fdv);
result = new VarExp(e.loc, ids.vthis);
result.type = ids.vthis.type;
if (ids.fd.hasDualContext())
{
// &__this
result = new AddrExp(e.loc, result);
result.type = ids.vthis.type.pointerTo();
}
while (s != fdv)
{
auto f = s.isFuncDeclaration();
AggregateDeclaration ad;
if (f && f.hasDualContext())
{
if (f.hasNestedFrameRefs())
{
result = new DotVarExp(e.loc, result, f.vthis);
result.type = f.vthis.type;
}
// (*__this)[i]
uint i = f.followInstantiationContext(fdv);
if (i == 1 && f == ids.fd)
{
auto ve = e.copy().isVarExp();
ve.originalScope = ids.fd;
result = ve;
return;
}
result = new PtrExp(e.loc, result);
result.type = Type.tvoidptr.sarrayOf(2);
auto ie = new IndexExp(e.loc, result, new IntegerExp(i));
ie.indexIsInBounds = true; // no runtime bounds checking
result = ie;
result.type = Type.tvoidptr;
s = f.toParentP(fdv);
ad = s.isAggregateDeclaration();
if (ad)
goto Lad;
continue;
}
else if ((ad = s.isThis()) !is null)
{
Lad:
while (ad)
{
assert(ad.vthis);
bool i = ad.followInstantiationContext(fdv);
auto vthis = i ? ad.vthis2 : ad.vthis;
result = new DotVarExp(e.loc, result, vthis);
result.type = vthis.type;
s = ad.toParentP(fdv);
ad = s.isAggregateDeclaration();
}
}
else if (f && f.isNested())
{
assert(f.vthis);
if (f.hasNestedFrameRefs())
{
result = new DotVarExp(e.loc, result, f.vthis);
result.type = f.vthis.type;
}
s = f.toParent2();
}
else
assert(0);
assert(s);
}
result = new DotVarExp(e.loc, result, v);
result.type = v.type;
//printf("\t==> result = %s, type = %s\n", result.toChars(), result.type.toChars());
return;
}
else if (v && v.nestedrefs.length)
{
auto ve = e.copy().isVarExp();
ve.originalScope = ids.fd;
result = ve;
return;
}
result = e;
}
override void visit(ThisExp e)
{
//if (!ids.vthis)
// e.error("no `this` when inlining `%s`", ids.parent.toChars());
if (!ids.vthis)
{
result = e;
return;
}
result = new VarExp(e.loc, ids.vthis);
if (ids.fd.hasDualContext())
{
// __this[0]
result.type = ids.vthis.type;
auto ie = new IndexExp(e.loc, result, IntegerExp.literal!0);
ie.indexIsInBounds = true; // no runtime bounds checking
result = ie;
if (e.type.ty == Tstruct)
{
result.type = e.type.pointerTo();
result = new PtrExp(e.loc, result);
}
}
result.type = e.type;
}
override void visit(SuperExp e)
{
assert(ids.vthis);
result = new VarExp(e.loc, ids.vthis);
if (ids.fd.hasDualContext())
{
// __this[0]
result.type = ids.vthis.type;
auto ie = new IndexExp(e.loc, result, IntegerExp.literal!0);
ie.indexIsInBounds = true; // no runtime bounds checking
result = ie;
}
result.type = e.type;
}
override void visit(DeclarationExp e)
{
//printf("DeclarationExp.doInlineAs!%s(%s)\n", Result.stringof.ptr, e.toChars());
if (auto vd = e.declaration.isVarDeclaration())
{
version (none)
{
// Need to figure this out before inlining can work for tuples
if (auto tup = vd.toAlias().isTupleDeclaration())
{
tup.foreachVar((s) { s; });
result = st.objects.length;
return;
}
}
if (vd.isStatic())
return;
if (ids.fd && vd == ids.fd.nrvo_var)
{
foreach (i; 0 .. ids.from.length)
{
if (vd != ids.from[i])
continue;
if (vd._init && !vd._init.isVoidInitializer())
{
result = vd._init.initializerToExpression();
assert(result);
result = doInlineAs!Expression(result, ids);
}
else
result = IntegerExp.literal!0;
return;
}
}
auto vto = new VarDeclaration(vd.loc, vd.type, vd.ident, vd._init);
memcpy(cast(void*)vto, cast(void*)vd, __traits(classInstanceSize, VarDeclaration));
vto.parent = ids.parent;
version (IN_LLVM) {} else
{
vto.csym = null;
}
ids.from.push(vd);
ids.to.push(vto);
if (vd._init)
{
if (vd._init.isVoidInitializer())
{
vto._init = new VoidInitializer(vd._init.loc);
}
else
{
auto ei = vd._init.initializerToExpression();
assert(ei);
vto._init = new ExpInitializer(ei.loc, doInlineAs!Expression(ei, ids));
}
}
if (vd.edtor)
{
vto.edtor = doInlineAs!Expression(vd.edtor, ids);
}
auto de = e.copy().isDeclarationExp();
de.declaration = vto;
result = de;
return;
}
// Prevent the copy of the aggregates allowed in inlineable funcs
if (isInlinableNestedAggregate(e))
return;
/* This needs work, like DeclarationExp.toElem(), if we are
* to handle TemplateMixin's. For now, we just don't inline them.
*/
visit(cast(Expression)e);
}
override void visit(TypeidExp e)
{
//printf("TypeidExp.doInlineAs!%s(): %s\n", Result.stringof.ptr, e.toChars());
auto te = e.copy().isTypeidExp();
if (auto ex = isExpression(te.obj))
{
te.obj = doInlineAs!Expression(ex, ids);
}
else
assert(isType(te.obj));
result = te;
}
override void visit(NewExp e)
{
//printf("NewExp.doInlineAs!%s(): %s\n", Result.stringof.ptr, e.toChars());
auto ne = e.copy().isNewExp();
auto lowering = ne.lowering;
if (lowering)
if (auto ce = lowering.isCallExp())
if (ce.f.ident == Id._d_newarrayT || ce.f.ident == Id._d_newarraymTX)
{
ne.lowering = doInlineAs!Expression(lowering, ids);
goto LhasLowering;
}
ne.placement = doInlineAs!Expression(e.placement, ids);
ne.thisexp = doInlineAs!Expression(e.thisexp, ids);
ne.argprefix = doInlineAs!Expression(e.argprefix, ids);
ne.arguments = arrayExpressionDoInline(e.arguments);
LhasLowering:
result = ne;
semanticTypeInfo(null, e.type);
}
override void visit(UnaExp e)
{
auto ue = cast(UnaExp)e.copy();
ue.e1 = doInlineAs!Expression(e.e1, ids);
result = ue;
}
override void visit(AssertExp e)
{
auto ae = e.copy().isAssertExp();
ae.e1 = doInlineAs!Expression(e.e1, ids);
ae.msg = doInlineAs!Expression(e.msg, ids);
result = ae;
}
override void visit(CatExp e)
{
auto ce = e.copy().isCatExp();
if (auto lowering = ce.lowering)
ce.lowering = doInlineAs!Expression(lowering, ids);
else
{
ce.e1 = doInlineAs!Expression(e.e1, ids);
ce.e2 = doInlineAs!Expression(e.e2, ids);
}
result = ce;
}
override void visit(CatAssignExp e)
{
auto cae = cast(CatAssignExp) e.copy();
if (auto lowering = cae.lowering)
cae.lowering = doInlineAs!Expression(cae.lowering, ids);
else
{
cae.e1 = doInlineAs!Expression(e.e1, ids);
cae.e2 = doInlineAs!Expression(e.e2, ids);
}
result = cae;
}
override void visit(BinExp e)
{
auto be = cast(BinExp)e.copy();
be.e1 = doInlineAs!Expression(e.e1, ids);
be.e2 = doInlineAs!Expression(e.e2, ids);
result = be;
}
override void visit(CallExp e)
{
auto ce = e.copy().isCallExp();
ce.e1 = doInlineAs!Expression(e.e1, ids);
ce.arguments = arrayExpressionDoInline(e.arguments);
result = ce;
}
override void visit(AssignExp e)
{
visit(cast(BinExp)e);
}
override void visit(LoweredAssignExp e)
{
result = doInlineAs!Expression(e.lowering, ids);
}
override void visit(EqualExp e)
{
visit(cast(BinExp)e);
Type t1 = e.e1.type.toBasetype();
if (t1.isStaticOrDynamicArray())
{
Type t = t1.nextOf().toBasetype();
while (t.toBasetype().nextOf())
t = t.nextOf().toBasetype();
if (t.ty == Tstruct)
semanticTypeInfo(null, t);
}
else if (t1.ty == Taarray)
{
semanticTypeInfo(null, t1);
}
}
override void visit(IndexExp e)
{
auto are = e.copy().isIndexExp();
are.e1 = doInlineAs!Expression(e.e1, ids);
if (e.lengthVar)
{
//printf("lengthVar\n");
auto vd = e.lengthVar;
auto vto = new VarDeclaration(vd.loc, vd.type, vd.ident, vd._init);
memcpy(cast(void*)vto, cast(void*)vd, __traits(classInstanceSize, VarDeclaration));
vto.parent = ids.parent;
version (IN_LLVM) {} else
{
vto.csym = null;
}
ids.from.push(vd);
ids.to.push(vto);
if (vd._init && !vd._init.isVoidInitializer())
{
auto ie = vd._init.isExpInitializer();
assert(ie);
vto._init = new ExpInitializer(ie.loc, doInlineAs!Expression(ie.exp, ids));
}
are.lengthVar = vto;
}
are.e2 = doInlineAs!Expression(e.e2, ids);
result = are;
}
override void visit(SliceExp e)
{
auto are = e.copy().isSliceExp();
are.e1 = doInlineAs!Expression(e.e1, ids);
if (e.lengthVar)
{
//printf("lengthVar\n");
auto vd = e.lengthVar;
auto vto = new VarDeclaration(vd.loc, vd.type, vd.ident, vd._init);
memcpy(cast(void*)vto, cast(void*)vd, __traits(classInstanceSize, VarDeclaration));
vto.parent = ids.parent;
version (IN_LLVM) {} else
{
vto.csym = null;
}
ids.from.push(vd);
ids.to.push(vto);
if (vd._init && !vd._init.isVoidInitializer())
{
auto ie = vd._init.isExpInitializer();
assert(ie);
vto._init = new ExpInitializer(ie.loc, doInlineAs!Expression(ie.exp, ids));
}
are.lengthVar = vto;
}
are.lwr = doInlineAs!Expression(e.lwr, ids);
are.upr = doInlineAs!Expression(e.upr, ids);
result = are;
}
override void visit(TupleExp e)
{
auto ce = e.copy().isTupleExp();
ce.e0 = doInlineAs!Expression(e.e0, ids);
ce.exps = arrayExpressionDoInline(e.exps);
result = ce;
}
override void visit(ArrayLiteralExp e)
{
auto ce = e.copy().isArrayLiteralExp();
ce.basis = doInlineAs!Expression(e.basis, ids);
ce.elements = arrayExpressionDoInline(e.elements);
result = ce;
semanticTypeInfo(null, e.type);
}
override void visit(AssocArrayLiteralExp e)
{
auto ce = e.copy().isAssocArrayLiteralExp();
ce.keys = arrayExpressionDoInline(e.keys);
ce.values = arrayExpressionDoInline(e.values);
result = ce;
semanticTypeInfo(null, e.type);
}
override void visit(StructLiteralExp e)
{
if (e.inlinecopy)
{
result = e.inlinecopy;
return;
}
auto ce = e.copy().isStructLiteralExp();
e.inlinecopy = ce;
ce.elements = arrayExpressionDoInline(e.elements);
e.inlinecopy = null;
result = ce;
}
override void visit(ArrayExp e)
{
assert(0); // this should have been lowered to something else
}
override void visit(CondExp e)
{
auto ce = e.copy().isCondExp();
ce.econd = doInlineAs!Expression(e.econd, ids);
ce.e1 = doInlineAs!Expression(e.e1, ids);
ce.e2 = doInlineAs!Expression(e.e2, ids);
result = ce;
}
}
}
/// ditto
private Result doInlineAs(Result)(Statement s, InlineDoState ids)
{
if (!s)
return null;
scope DoInlineAs!Result v = new DoInlineAs!Result(ids);
s.accept(v);
return v.result;
}
/// ditto
private Result doInlineAs(Result)(Expression e, InlineDoState ids)
{
if (!e)
return null;
scope DoInlineAs!Result v = new DoInlineAs!Result(ids);
e.accept(v);
return v.result;
}
/***********************************************************
* Walk the trees, looking for functions to inline.
* Inline any that can be.
*/
private extern (C++) final class InlineScanVisitor : Visitor
{
alias visit = Visitor.visit;
public:
FuncDeclaration parent; // function being scanned
// As the visit method cannot return a value, these variables
// are used to pass the result from 'visit' back to 'inlineScan'
Statement sresult;
Expression eresult;