-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathexpressionsem.d
17701 lines (15942 loc) · 597 KB
/
expressionsem.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
/**
* Semantic analysis of expressions.
*
* Specification: ($LINK2 https://dlang.org/spec/expression.html, Expressions)
*
* 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/expressionsem.d, _expressionsem.d)
* Documentation: https://dlang.org/phobos/dmd_expressionsem.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/expressionsem.d
*/
module dmd.expressionsem;
import core.stdc.stdio;
import dmd.access;
import dmd.aggregate;
import dmd.aliasthis;
import dmd.arrayop;
import dmd.arraytypes;
import dmd.attrib;
import dmd.astcodegen;
import dmd.astenums;
import dmd.canthrow;
import dmd.chkformat;
import dmd.cond;
import dmd.ctorflow;
import dmd.dscope;
import dmd.dsymbol;
import dmd.declaration;
import dmd.dclass;
import dmd.dcast;
import dmd.delegatize;
import dmd.denum;
import dmd.deps;
import dmd.dimport;
import dmd.dinterpret;
import dmd.dmodule;
import dmd.dstruct;
import dmd.dsymbolsem;
import dmd.dtemplate;
import dmd.errors;
import dmd.errorsink;
import dmd.enumsem;
import dmd.escape;
import dmd.expression;
import dmd.file_manager;
import dmd.func;
import dmd.funcsem;
import dmd.globals;
import dmd.hdrgen;
import dmd.id;
import dmd.identifier;
import dmd.imphint;
import dmd.importc;
import dmd.init;
import dmd.initsem;
import dmd.inline;
import dmd.intrange;
import dmd.location;
import dmd.mangle;
import dmd.mtype;
import dmd.mustuse;
import dmd.nspace;
import dmd.nogc;
import dmd.objc;
import dmd.opover;
import dmd.optimize;
import dmd.parse;
import dmd.printast;
import dmd.root.array;
import dmd.root.ctfloat;
import dmd.root.filename;
import dmd.common.outbuffer;
import dmd.rootobject;
import dmd.root.string;
import dmd.root.utf;
import dmd.semantic2;
import dmd.semantic3;
import dmd.sideeffect;
import dmd.safe;
import dmd.target;
import dmd.templatesem : matchWithInstance;
import dmd.tokens;
import dmd.traits;
import dmd.typesem;
import dmd.typinf;
import dmd.utils;
import dmd.utils : arrayCastBigEndian;
import dmd.visitor;
import dmd.visitor.postorder;
enum LOGSEMANTIC = false;
/***********************************
* Determine if a `this` is needed to access `d`.
* Params:
* sc = context
* d = declaration to check
* Returns:
* true means a `this` is needed
*/
private bool isNeedThisScope(Scope* sc, Declaration d)
{
if (sc.intypeof == 1)
return false;
AggregateDeclaration ad = d.isThis();
if (!ad)
return false;
//printf("d = %s, ad = %s\n", d.toChars(), ad.toChars());
for (Dsymbol s = sc.parent; s; s = s.toParentLocal())
{
//printf("\ts = %s %s, toParent2() = %p\n", s.kind(), s.toChars(), s.toParent2());
if (AggregateDeclaration ad2 = s.isAggregateDeclaration())
{
if (ad2 == ad)
return false;
if (ad2.isNested())
continue;
return true;
}
if (FuncDeclaration f = s.isFuncDeclaration())
{
if (f.isMemberLocal())
break;
}
}
return true;
}
/********************************************************
* Perform semantic analysis and CTFE on expressions to produce
* a string.
* Params:
* buf = append generated string to buffer
* sc = context
* exps = array of Expressions
* loc = location of the pragma / mixin where this conversion was requested, for supplemental error
* fmt = format string for supplemental error. May contain 1 `%s` which prints the faulty expression
* expandTuples = whether tuples should be expanded rather than printed as tuple syntax
* Returns:
* true on error
*/
bool expressionsToString(ref OutBuffer buf, Scope* sc, Expressions* exps,
Loc loc, const(char)* fmt, bool expandTuples)
{
if (!exps)
return false;
foreach (ex; *exps)
{
bool error()
{
if (loc != Loc.initial && fmt)
errorSupplemental(loc, fmt, ex.toChars());
return true;
}
if (!ex)
continue;
auto sc2 = sc.startCTFE();
sc2.tinst = null;
sc2.minst = null; // prevents emission of any instantiated templates to object file
auto e2 = ex.expressionSemantic(sc2);
auto e3 = resolveProperties(sc2, e2);
sc2.endCTFE();
// allowed to contain types as well as expressions
auto e4 = ctfeInterpretForPragmaMsg(e3);
if (!e4 || e4.op == EXP.error)
return error();
// expand tuple
if (expandTuples)
if (auto te = e4.isTupleExp())
{
if (expressionsToString(buf, sc, te.exps, loc, fmt, true))
return error();
continue;
}
// char literals exp `.toStringExp` return `null` but we cant override it
// because in most contexts we don't want the conversion to succeed.
IntegerExp ie = e4.isIntegerExp();
const ty = (ie && ie.type) ? ie.type.ty : Terror;
if (ty.isSomeChar)
{
auto tsa = new TypeSArray(ie.type, IntegerExp.literal!1);
e4 = new ArrayLiteralExp(ex.loc, tsa, ie);
}
StringExp se = e4.toStringExp();
if (se && se.type.nextOf().ty.isSomeChar)
buf.writestring(se.toUTF8(sc).peekString());
else if (!(se && se.len == 0)) // don't print empty array literal `[]`
buf.writestring(e4.toString());
}
return false;
}
/*****************************************
* Determine if `this` is available by walking up the enclosing
* scopes until a function is found.
*
* Params:
* sc = where to start looking for the enclosing function
* Returns:
* Found function if it satisfies `isThis()`, otherwise `null`
*/
FuncDeclaration hasThis(Scope* sc)
{
//printf("hasThis()\n");
Dsymbol p = sc.parent;
while (p && p.isTemplateMixin())
p = p.parent;
FuncDeclaration fdthis = p ? p.isFuncDeclaration() : null;
//printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis.toChars() : "");
// Go upwards until we find the enclosing member function
FuncDeclaration fd = fdthis;
while (1)
{
if (!fd)
{
return null;
}
if (!fd.isNested() || fd.isThis() || (fd.hasDualContext() && fd.isMember2()))
break;
Dsymbol parent = fd.parent;
while (1)
{
if (!parent)
return null;
TemplateInstance ti = parent.isTemplateInstance();
if (ti)
parent = ti.parent;
else
break;
}
fd = parent.isFuncDeclaration();
}
if (!fd.isThis() && !(fd.hasDualContext() && fd.isMember2()))
{
return null;
}
assert(fd.vthis);
return fd;
}
extern (D) bool findTempDecl(DotTemplateInstanceExp exp, Scope* sc)
{
auto ti = exp.ti;
auto e1 = exp.e1;
static if (LOGSEMANTIC)
{
printf("DotTemplateInstanceExp::findTempDecl('%s')\n", exp.toChars());
}
if (ti.tempdecl)
return true;
Expression e = new DotIdExp(exp.loc, e1, ti.name);
e = e.expressionSemantic(sc);
if (e.op == EXP.dot)
e = (cast(DotExp)e).e2;
Dsymbol s = null;
switch (e.op)
{
case EXP.overloadSet:
s = (cast(OverExp)e).vars;
break;
case EXP.dotTemplateDeclaration:
s = (cast(DotTemplateExp)e).td;
break;
case EXP.scope_:
s = (cast(ScopeExp)e).sds;
break;
case EXP.dotVariable:
s = (cast(DotVarExp)e).var;
break;
case EXP.variable:
s = (cast(VarExp)e).var;
break;
default:
return false;
}
return ti.updateTempDecl(sc, s);
}
/***********************************************************
* Resolve `exp` as a compile-time known string.
* Params:
* sc = scope
* exp = Expression which expected as a string
* s = What the string is expected for, will be used in error diagnostic.
* Returns:
* String literal, or `null` if error happens.
*/
StringExp semanticString(Scope* sc, Expression exp, const char* s)
{
sc = sc.startCTFE();
exp = exp.expressionSemantic(sc);
exp = resolveProperties(sc, exp);
sc = sc.endCTFE();
if (exp.op == EXP.error)
return null;
auto e = exp;
if (exp.type.isString())
{
e = e.ctfeInterpret();
if (e.op == EXP.error)
return null;
}
if (auto se = e.toStringExp())
return se;
error(exp.loc, "`string` expected for %s, not `(%s)` of type `%s`",
s, exp.toChars(), exp.type.toChars());
return null;
}
/****************************************
* Convert string to char[].
*/
StringExp toUTF8(StringExp se, Scope* sc)
{
if (se.sz == 1)
return se;
// Convert to UTF-8 string
se.committed = false;
Expression e = castTo(se, sc, Type.tchar.arrayOf());
e = e.optimize(WANTvalue);
auto result = e.isStringExp();
assert(result);
assert(result.sz == 1);
return result;
}
/********************************
* The type for a unary expression is incompatible.
* Print error message.
* Returns:
* ErrorExp
*/
private Expression incompatibleTypes(UnaExp e)
{
if (e.e1.type.toBasetype() == Type.terror)
return e.e1;
if (e.e1.op == EXP.type)
{
error(e.loc, "incompatible type for `%s(%s)`: cannot use `%s` with types", EXPtoString(e.op).ptr, e.e1.toErrMsg(), EXPtoString(e.op).ptr);
}
else
{
error(e.loc, "incompatible type for `%s(%s)`: `%s`", EXPtoString(e.op).ptr, e.e1.toErrMsg(), e.e1.type.toChars());
}
return ErrorExp.get();
}
/********************************
* The types for a binary expression are incompatible.
* Print error message.
* Returns:
* ErrorExp
*/
extern (D) Expression incompatibleTypes(BinExp e, Scope* sc = null)
{
if (e.e1.type.toBasetype() == Type.terror)
return e.e1;
if (e.e2.type.toBasetype() == Type.terror)
return e.e2;
// CondExp uses 'a ? b : c' but we're comparing 'b : c'
const(char)* thisOp = (e.op == EXP.question) ? ":" : EXPtoString(e.op).ptr;
if (sc && suggestBinaryOverloads(e, sc))
return ErrorExp.get();
if (e.e1.op == EXP.type || e.e2.op == EXP.type)
{
error(e.loc, "incompatible types for `(%s) %s (%s)`: cannot use `%s` with types",
e.e1.toErrMsg(), thisOp, e.e2.toErrMsg(), EXPtoString(e.op).ptr);
}
else if (e.e1.type.equals(e.e2.type))
{
error(e.loc, "incompatible types for `(%s) %s (%s)`: both operands are of type `%s`",
e.e1.toErrMsg(), thisOp, e.e2.toErrMsg(), e.e1.type.toChars());
}
else
{
auto ts = toAutoQualChars(e.e1.type, e.e2.type);
error(e.loc, "incompatible types for `(%s) %s (%s)`: `%s` and `%s`",
e.e1.toErrMsg(), thisOp, e.e2.toErrMsg(), ts[0], ts[1]);
}
return ErrorExp.get();
}
private Expression reorderSettingAAElem(BinExp exp, Scope* sc)
{
BinExp be = exp;
auto ie = be.e1.isIndexExp();
if (!ie)
return be;
if (ie.e1.type.toBasetype().ty != Taarray)
return be;
/* Fix evaluation order of setting AA element
* https://issues.dlang.org/show_bug.cgi?id=3825
* Rewrite:
* aa[k1][k2][k3] op= val;
* as:
* auto ref __aatmp = aa;
* auto ref __aakey3 = k1, __aakey2 = k2, __aakey1 = k3;
* auto ref __aaval = val;
* __aatmp[__aakey3][__aakey2][__aakey1] op= __aaval; // assignment
*/
Expression e0;
while (1)
{
Expression de;
ie.e2 = extractSideEffect(sc, "__aakey", de, ie.e2);
e0 = Expression.combine(de, e0);
auto ie1 = ie.e1.isIndexExp();
if (!ie1 ||
ie1.e1.type.toBasetype().ty != Taarray)
{
break;
}
ie = ie1;
}
assert(ie.e1.type.toBasetype().ty == Taarray);
Expression de;
ie.e1 = extractSideEffect(sc, "__aatmp", de, ie.e1);
e0 = Expression.combine(de, e0);
be.e2 = extractSideEffect(sc, "__aaval", e0, be.e2, true);
//printf("-e0 = %s, be = %s\n", e0.toChars(), be.toChars());
return Expression.combine(e0, be);
}
private Expression checkOpAssignTypes(BinExp binExp, Scope* sc)
{
auto e1 = binExp.e1;
auto e2 = binExp.e2;
auto op = binExp.op;
auto type = binExp.type;
auto loc = binExp.loc;
// At that point t1 and t2 are the merged types. type is the original type of the lhs.
Type t1 = e1.type;
Type t2 = e2.type;
// T opAssign floating yields a floating. Prevent truncating conversions (float to int).
// See https://issues.dlang.org/show_bug.cgi?id=3841.
// Should we also prevent double to float (type.isFloating() && type.size() < t2.size()) ?
if (op == EXP.addAssign || op == EXP.minAssign ||
op == EXP.mulAssign || op == EXP.divAssign || op == EXP.modAssign ||
op == EXP.powAssign)
{
if ((type.isIntegral() && t2.isFloating()))
{
warning(loc, "`%s %s %s` is performing truncating conversion", type.toChars(), EXPtoString(op).ptr, t2.toChars());
}
}
// generate an error if this is a nonsensical *=,/=, or %=, eg real *= imaginary
if (op == EXP.mulAssign || op == EXP.divAssign || op == EXP.modAssign)
{
// Any multiplication by an imaginary or complex number yields a complex result.
// r *= c, i*=c, r*=i, i*=i are all forbidden operations.
const(char)* opstr = EXPtoString(op).ptr;
if (t1.isReal() && t2.isComplex())
{
error(loc, "`%s %s %s` is undefined. Did you mean `%s %s %s.re`?", t1.toChars(), opstr, t2.toChars(), t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
else if (t1.isImaginary() && t2.isComplex())
{
error(loc, "`%s %s %s` is undefined. Did you mean `%s %s %s.im`?", t1.toChars(), opstr, t2.toChars(), t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
else if ((t1.isReal() || t1.isImaginary()) && t2.isImaginary())
{
error(loc, "`%s %s %s` is an undefined operation", t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
}
// generate an error if this is a nonsensical += or -=, eg real += imaginary
if (op == EXP.addAssign || op == EXP.minAssign)
{
// Addition or subtraction of a real and an imaginary is a complex result.
// Thus, r+=i, r+=c, i+=r, i+=c are all forbidden operations.
if ((t1.isReal() && (t2.isImaginary() || t2.isComplex())) || (t1.isImaginary() && (t2.isReal() || t2.isComplex())))
{
error(loc, "`%s %s %s` is undefined (result is complex)", t1.toChars(), EXPtoString(op).ptr, t2.toChars());
return ErrorExp.get();
}
if (type.isReal() || type.isImaginary())
{
assert(global.errors || t2.isFloating());
e2 = e2.castTo(sc, t1);
}
}
if (op == EXP.mulAssign && t2.isFloating())
{
if (t1.isReal())
{
if (t2.isImaginary() || t2.isComplex())
{
e2 = e2.castTo(sc, t1);
}
}
else if (t1.isImaginary())
{
if (t2.isImaginary() || t2.isComplex())
{
switch (t1.ty)
{
case Timaginary32:
t2 = Type.tfloat32;
break;
case Timaginary64:
t2 = Type.tfloat64;
break;
case Timaginary80:
t2 = Type.tfloat80;
break;
default:
assert(0);
}
e2 = e2.castTo(sc, t2);
}
}
}
else if (op == EXP.divAssign && t2.isImaginary())
{
if (t1.isReal())
{
// x/iv = i(-x/v)
// Therefore, the result is 0
e2 = new CommaExp(loc, e2, new RealExp(loc, CTFloat.zero, t1));
e2.type = t1;
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;
}
else if (t1.isImaginary())
{
Type t3;
switch (t1.ty)
{
case Timaginary32:
t3 = Type.tfloat32;
break;
case Timaginary64:
t3 = Type.tfloat64;
break;
case Timaginary80:
t3 = Type.tfloat80;
break;
default:
assert(0);
}
e2 = e2.castTo(sc, t3);
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;
}
}
else if (op == EXP.modAssign)
{
if (t2.isComplex())
{
error(loc, "cannot perform modulo complex arithmetic");
return ErrorExp.get();
}
}
return binExp;
}
private Expression extractOpDollarSideEffect(Scope* sc, UnaExp ue)
{
Expression e0;
Expression e1 = Expression.extractLast(ue.e1, e0);
// https://issues.dlang.org/show_bug.cgi?id=12585
// Extract the side effect part if ue.e1 is comma.
if (sc.ctfe ? hasSideEffect(e1) : !isTrivialExp(e1)) // match logic in extractSideEffect()
{
/* Even if opDollar is needed, 'e1' should be evaluate only once. So
* Rewrite:
* e1.opIndex( ... use of $ ... )
* e1.opSlice( ... use of $ ... )
* as:
* (ref __dop = e1, __dop).opIndex( ... __dop.opDollar ...)
* (ref __dop = e1, __dop).opSlice( ... __dop.opDollar ...)
*/
e1 = extractSideEffect(sc, "__dop", e0, e1, false);
assert(e1.isVarExp());
e1.isVarExp().var.storage_class |= STC.exptemp; // lifetime limited to expression
}
ue.e1 = e1;
return e0;
}
/****************************************
* Expand alias this tuples.
*/
TupleDeclaration isAliasThisTuple(Expression e)
{
if (!e.type)
return null;
Type t = e.type.toBasetype();
while (true)
{
if (Dsymbol s = t.toDsymbol(null))
{
if (auto ad = s.isAggregateDeclaration())
{
s = ad.aliasthis ? ad.aliasthis.sym : null;
if (s && s.isVarDeclaration())
{
TupleDeclaration td = s.isVarDeclaration().toAlias().isTupleDeclaration();
if (td && td.isexp)
return td;
}
if (Type att = t.aliasthisOf())
{
t = att;
continue;
}
}
}
return null;
}
}
/**************************************
* Runs semantic on ae.arguments. Declares temporary variables
* if '$' was used.
*/
Expression resolveOpDollar(Scope* sc, ArrayExp ae, out Expression pe0)
{
assert(!ae.lengthVar);
AggregateDeclaration ad = isAggregate(ae.e1.type);
Dsymbol slice = search_function(ad, Id.opSlice);
//printf("slice = %s %s\n", slice.kind(), slice.toChars());
Expression fallback()
{
if (ae.arguments.length == 1)
return null;
error(ae.loc, "multi-dimensional slicing requires template `opSlice`");
return ErrorExp.get();
}
foreach (i, e; *ae.arguments)
{
if (i == 0)
pe0 = extractOpDollarSideEffect(sc, ae);
if (e.op == EXP.interval && !(slice && slice.isTemplateDeclaration()))
{
return fallback();
}
//printf("[%d] e = %s\n", i, e.toChars());
// Create scope for '$' variable for this dimension
auto sym = new ArrayScopeSymbol(sc, ae);
sym.parent = sc.scopesym;
sc = sc.push(sym);
ae.lengthVar = null; // Create it only if required
ae.currentDimension = i; // Dimension for $, if required
e = e.expressionSemantic(sc);
e = resolveProperties(sc, e);
if (ae.lengthVar && sc.func)
{
// If $ was used, declare it now
Expression de = new DeclarationExp(ae.loc, ae.lengthVar);
de = de.expressionSemantic(sc);
pe0 = Expression.combine(pe0, de);
}
sc = sc.pop();
if (auto ie = e.isIntervalExp())
{
auto tiargs = new Objects();
Expression edim = new IntegerExp(ae.loc, i, Type.tsize_t);
edim = edim.expressionSemantic(sc);
tiargs.push(edim);
auto fargs = new Expressions(2);
(*fargs)[0] = ie.lwr;
(*fargs)[1] = ie.upr;
const xerrors = global.startGagging();
sc = sc.push();
FuncDeclaration fslice = resolveFuncCall(ae.loc, sc, slice, tiargs, ae.e1.type, ArgumentList(fargs), FuncResolveFlag.quiet);
sc = sc.pop();
global.endGagging(xerrors);
if (!fslice)
return fallback();
e = new DotTemplateInstanceExp(ae.loc, ae.e1, Id.opSlice, tiargs);
e = new CallExp(ae.loc, e, fargs);
e = e.expressionSemantic(sc);
}
if (!e.type)
{
error(ae.loc, "`%s` has no value", e.toErrMsg());
e = ErrorExp.get();
}
if (e.op == EXP.error)
return e;
(*ae.arguments)[i] = e;
}
return ae;
}
/**************************************
* Runs semantic on se.lwr and se.upr. Declares a temporary variable
* if '$' was used.
* Returns:
* ae, or ErrorExp if errors occurred
*/
Expression resolveOpDollar(Scope* sc, ArrayExp ae, IntervalExp ie, ref Expression pe0)
{
//assert(!ae.lengthVar);
if (!ie)
return ae;
VarDeclaration lengthVar = ae.lengthVar;
bool errors = false;
// create scope for '$'
auto sym = new ArrayScopeSymbol(sc, ae);
sym.parent = sc.scopesym;
sc = sc.push(sym);
Expression sem(Expression e)
{
e = e.expressionSemantic(sc);
e = resolveProperties(sc, e);
if (!e.type)
{
error(ae.loc, "`%s` has no value", e.toErrMsg());
errors = true;
}
return e;
}
ie.lwr = sem(ie.lwr);
ie.upr = sem(ie.upr);
if (ie.lwr.isErrorExp() || ie.upr.isErrorExp())
errors = true;
if (lengthVar != ae.lengthVar && sc.func)
{
// If $ was used, declare it now
Expression de = new DeclarationExp(ae.loc, ae.lengthVar);
de = de.expressionSemantic(sc);
pe0 = Expression.combine(pe0, de);
}
sc = sc.pop();
return errors ? ErrorExp.get() : ae;
}
/******************************
* Perform semantic() on an array of Expressions.
*/
extern(D) bool arrayExpressionSemantic(
Expression[] exps, Scope* sc, bool preserveErrors = false)
{
bool err = false;
foreach (ref e; exps)
{
if (e is null) continue;
auto e2 = e.expressionSemantic(sc);
if (e2.op == EXP.error)
err = true;
if (preserveErrors || e2.op != EXP.error)
e = e2;
}
return err;
}
/************************************************
* Handle the postblit call on lvalue, or the move of rvalue.
*
* Params:
* sc = the scope where the expression is encountered
* e = the expression the needs to be moved or copied (source)
* t = if the struct defines a copy constructor, the type of the destination (can be NULL)
* nrvo = true if the generated copy can be treated as NRVO
* move = true to allow a move constructor to be used, false to prevent infinite recursion
* Returns:
* The expression that copy constructs or moves the value.
*/
extern (D) Expression doCopyOrMove(Scope* sc, Expression e, Type t, bool nrvo, bool move = false)
{
//printf("doCopyOrMove() %s\n", toChars(e));
StructDeclaration sd;
if (t)
{
if (auto ts = t.isTypeStruct())
sd = ts.sym;
}
if (auto ce = e.isCondExp())
{
ce.e1 = doCopyOrMove(sc, ce.e1, null, nrvo);
ce.e2 = doCopyOrMove(sc, ce.e2, null, nrvo);
}
else if (e.isLvalue())
{
e = callCpCtor(sc, e, t, nrvo);
}
else if (move && sd && sd.hasMoveCtor && !e.isCallExp() && !e.isStructLiteralExp())
{
// #move
/* Rewrite as:
* S __copyrvalue;
* __copyrvalue.moveCtor(e);
* __copyrvalue;
*/
VarDeclaration vd = new VarDeclaration(e.loc, e.type, Identifier.generateId("__copyrvalue"), null);
if (nrvo)
vd.nrvo = true;
vd.storage_class |= STC.nodtor;
vd.dsymbolSemantic(sc);
Expression de = new DeclarationExp(e.loc, vd);
Expression ve = new VarExp(e.loc, vd);
Expression er;
er = new DotIdExp(e.loc, ve, Id.ctor); // ve.ctor
er = new CallExp(e.loc, er, e); // ve.ctor(e)
er = new CommaExp(e.loc, er, new VarExp(e.loc, vd)); // ve.ctor(e),vd
er = Expression.combine(de, er); // de,ve.ctor(e),vd
e = er.expressionSemantic(sc);
}
else
{
e = valueNoDtor(e);
}
return e;
}
/*********************************************
* If e is an instance of a struct, and that struct has a copy constructor,
* rewrite e as:
* (tmp = e),tmp
* Params:
* sc = just used to specify the scope of created temporary variable
* destinationType = the type of the object on which the copy constructor is called;
* may be null if the struct defines a postblit
* nrvo = true if the generated copy can be treated as NRVO
*/
private Expression callCpCtor(Scope* sc, Expression e, Type destinationType, bool nrvo)
{
//printf("callCpCtor(e: %s et: %s destinationType: %s\n", toChars(e), toChars(e.type), toChars(destinationType));
auto ts = e.type.baseElemOf().isTypeStruct();
if (!ts)
return e;
StructDeclaration sd = ts.sym;
if (!sd.postblit && !sd.hasCopyCtor)
return e;
/* Create a variable tmp, and replace the argument e with:
* (tmp = e),tmp
* and let AssignExp() handle the construction.
* This is not the most efficient, ideally tmp would be constructed
* directly onto the stack.
*/
VarDeclaration tmp = copyToTemp(STC.rvalue, "__copytmp", e);
if (nrvo)
tmp.nrvo = true;
if (sd.hasCopyCtor && destinationType)
{
// https://issues.dlang.org/show_bug.cgi?id=22619
// If the destination type is inout we can preserve it
// only if inside an inout function; if we are not inside
// an inout function, then we will preserve the type of
// the source
if (destinationType.hasWild && !(sc.func.storage_class & STC.wild))
tmp.type = e.type;
else
tmp.type = destinationType;
}
tmp.storage_class |= STC.nodtor;
tmp.dsymbolSemantic(sc);
Expression de = new DeclarationExp(e.loc, tmp);
Expression ve = new VarExp(e.loc, tmp);
de.type = Type.tvoid;
ve.type = e.type;
return Expression.combine(de, ve);
}
/************************************************
* If we want the value of this expression, but do not want to call
* the destructor on it.
*/
Expression valueNoDtor(Expression e)
{
//printf("valueNoDtor() %s\n", toChars(e));
auto ex = lastComma(e);
if (auto ce = ex.isCallExp())
{
/* The struct value returned from the function is transferred
* so do not call the destructor on it.
* Recognize:
* ((S _ctmp = S.init), _ctmp).this(...)
* and make sure the destructor is not called on _ctmp
* BUG: if ex is a CommaExp, we should go down the right side.
*/
if (auto dve = ce.e1.isDotVarExp())
{
if (dve.var.isCtorDeclaration())
{
// It's a constructor call
if (auto comma = dve.e1.isCommaExp())
{
if (auto ve = comma.e2.isVarExp())
{
VarDeclaration ctmp = ve.var.isVarDeclaration();
if (ctmp)
{
ctmp.storage_class |= STC.nodtor;
assert(!ce.isLvalue());
}
}
}
}
}
}
else if (auto ve = ex.isVarExp())
{
auto vtmp = ve.var.isVarDeclaration();
if (vtmp && (vtmp.storage_class & STC.rvalue))
{
vtmp.storage_class |= STC.nodtor;
}
}
return e;
}
/*
Checks if `exp` contains a direct access to a `noreturn`
variable. If that is the case, an `assert(0)` expression
is generated and returned. This function should be called
only after semantic analysis has been performed on `exp`.
Params:
exp = expression that is checked
Returns:
An `assert(0)` expression if `exp` contains a `noreturn`
variable access, `exp` otherwise.
*/
Expression checkNoreturnVarAccess(Expression exp)
{
assert(exp.type);
Expression result = exp;