-
-
Notifications
You must be signed in to change notification settings - Fork 593
/
init.d
1068 lines (1005 loc) · 32.7 KB
/
init.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 D programming language
// Copyright (c) 1999-2015 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
module ddmd.init;
import ddmd.aggregate;
import ddmd.arraytypes;
import ddmd.dcast;
import ddmd.declaration;
import ddmd.dscope;
import ddmd.dstruct;
import ddmd.dsymbol;
import ddmd.dtemplate;
import ddmd.errors;
import ddmd.expression;
import ddmd.func;
import ddmd.globals;
import ddmd.hdrgen;
import ddmd.id;
import ddmd.identifier;
import ddmd.mtype;
import ddmd.root.outbuffer;
import ddmd.root.rootobject;
import ddmd.statement;
import ddmd.tokens;
import ddmd.visitor;
enum NeedInterpret : int
{
INITnointerpret,
INITinterpret,
}
alias INITnointerpret = NeedInterpret.INITnointerpret;
alias INITinterpret = NeedInterpret.INITinterpret;
/***********************************************************
*/
extern (C++) class Initializer : RootObject
{
public:
Loc loc;
final extern (D) this(Loc loc)
{
this.loc = loc;
}
abstract Initializer syntaxCopy();
final static Initializers* arraySyntaxCopy(Initializers* ai)
{
Initializers* a = null;
if (ai)
{
a = new Initializers();
a.setDim(ai.dim);
for (size_t i = 0; i < a.dim; i++)
(*a)[i] = (*ai)[i].syntaxCopy();
}
return a;
}
/* Translates to an expression to infer type.
* Returns ExpInitializer or ErrorInitializer.
*/
abstract Initializer inferType(Scope* sc);
// needInterpret is INITinterpret if must be a manifest constant, 0 if not.
abstract Initializer semantic(Scope* sc, Type t, NeedInterpret needInterpret);
abstract Expression toExpression(Type t = null);
override final char* toChars()
{
OutBuffer buf;
HdrGenState hgs;
.toCBuffer(this, &buf, &hgs);
return buf.extractString();
}
ErrorInitializer isErrorInitializer()
{
return null;
}
VoidInitializer isVoidInitializer()
{
return null;
}
StructInitializer isStructInitializer()
{
return null;
}
ArrayInitializer isArrayInitializer()
{
return null;
}
ExpInitializer isExpInitializer()
{
return null;
}
void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class VoidInitializer : Initializer
{
public:
Type type; // type that this will initialize to
extern (D) this(Loc loc)
{
super(loc);
}
override Initializer syntaxCopy()
{
return new VoidInitializer(loc);
}
override Initializer inferType(Scope* sc)
{
error(loc, "cannot infer type from void initializer");
return new ErrorInitializer();
}
override Initializer semantic(Scope* sc, Type t, NeedInterpret needInterpret)
{
//printf("VoidInitializer::semantic(t = %p)\n", t);
type = t;
return this;
}
override Expression toExpression(Type t = null)
{
return null;
}
override VoidInitializer isVoidInitializer()
{
return this;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class ErrorInitializer : Initializer
{
public:
extern (D) this()
{
super(Loc());
}
override Initializer syntaxCopy()
{
return this;
}
override Initializer inferType(Scope* sc)
{
return this;
}
override Initializer semantic(Scope* sc, Type t, NeedInterpret needInterpret)
{
//printf("ErrorInitializer::semantic(t = %p)\n", t);
return this;
}
override Expression toExpression(Type t = null)
{
return new ErrorExp();
}
override ErrorInitializer isErrorInitializer()
{
return this;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class StructInitializer : Initializer
{
public:
Identifiers field; // of Identifier *'s
Initializers value; // parallel array of Initializer *'s
extern (D) this(Loc loc)
{
super(loc);
}
override Initializer syntaxCopy()
{
auto ai = new StructInitializer(loc);
assert(field.dim == value.dim);
ai.field.setDim(field.dim);
ai.value.setDim(value.dim);
for (size_t i = 0; i < field.dim; i++)
{
ai.field[i] = field[i];
ai.value[i] = value[i].syntaxCopy();
}
return ai;
}
void addInit(Identifier field, Initializer value)
{
//printf("StructInitializer::addInit(field = %p, value = %p)\n", field, value);
this.field.push(field);
this.value.push(value);
}
override Initializer inferType(Scope* sc)
{
error(loc, "cannot infer type from struct initializer");
return new ErrorInitializer();
}
override Initializer semantic(Scope* sc, Type t, NeedInterpret needInterpret)
{
//printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
t = t.toBasetype();
if (t.ty == Tsarray && t.nextOf().toBasetype().ty == Tstruct)
t = t.nextOf().toBasetype();
if (t.ty == Tstruct)
{
StructDeclaration sd = (cast(TypeStruct)t).sym;
if (sd.ctor)
{
error(loc, "%s %s has constructors, cannot use { initializers }, use %s( initializers ) instead", sd.kind(), sd.toChars(), sd.toChars());
return new ErrorInitializer();
}
sd.size(loc);
if (sd.sizeok != SIZEOKdone)
return new ErrorInitializer();
size_t nfields = sd.fields.dim - sd.isNested();
//expandTuples for non-identity arguments?
auto elements = new Expressions();
elements.setDim(nfields);
for (size_t i = 0; i < elements.dim; i++)
(*elements)[i] = null;
// Run semantic for explicitly given initializers
// TODO: this part is slightly different from StructLiteralExp::semantic.
bool errors = false;
for (size_t fieldi = 0, i = 0; i < field.dim; i++)
{
if (Identifier id = field[i])
{
Dsymbol s = sd.search(loc, id);
if (!s)
{
s = sd.search_correct(id);
if (s)
error(loc, "'%s' is not a member of '%s', did you mean %s '%s'?", id.toChars(), sd.toChars(), s.kind(), s.toChars());
else
error(loc, "'%s' is not a member of '%s'", id.toChars(), sd.toChars());
return new ErrorInitializer();
}
s = s.toAlias();
// Find out which field index it is
for (fieldi = 0; 1; fieldi++)
{
if (fieldi >= nfields)
{
error(loc, "%s.%s is not a per-instance initializable field", sd.toChars(), s.toChars());
return new ErrorInitializer();
}
if (s == sd.fields[fieldi])
break;
}
}
else if (fieldi >= nfields)
{
error(loc, "too many initializers for %s", sd.toChars());
return new ErrorInitializer();
}
VarDeclaration vd = sd.fields[fieldi];
if ((*elements)[fieldi])
{
error(loc, "duplicate initializer for field '%s'", vd.toChars());
errors = true;
continue;
}
for (size_t j = 0; j < nfields; j++)
{
VarDeclaration v2 = sd.fields[j];
if (vd.isOverlappedWith(v2) && (*elements)[j])
{
error(loc, "overlapping initialization for field %s and %s", v2.toChars(), vd.toChars());
errors = true;
continue;
}
}
assert(sc);
Initializer iz = value[i];
iz = iz.semantic(sc, vd.type.addMod(t.mod), needInterpret);
Expression ex = iz.toExpression();
if (ex.op == TOKerror)
{
errors = true;
continue;
}
value[i] = iz;
(*elements)[fieldi] = ex;
++fieldi;
}
if (errors)
return new ErrorInitializer();
auto sle = new StructLiteralExp(loc, sd, elements, t);
if (!sd.fill(loc, elements, false))
return new ErrorInitializer();
sle.type = t;
auto ie = new ExpInitializer(loc, sle);
return ie.semantic(sc, t, needInterpret);
}
else if ((t.ty == Tdelegate || t.ty == Tpointer && t.nextOf().ty == Tfunction) && value.dim == 0)
{
TOK tok = (t.ty == Tdelegate) ? TOKdelegate : TOKfunction;
/* Rewrite as empty delegate literal { }
*/
auto parameters = new Parameters();
Type tf = new TypeFunction(parameters, null, 0, LINKd);
auto fd = new FuncLiteralDeclaration(loc, Loc(), tf, tok, null);
fd.fbody = new CompoundStatement(loc, new Statements());
fd.endloc = loc;
Expression e = new FuncExp(loc, fd);
auto ie = new ExpInitializer(loc, e);
return ie.semantic(sc, t, needInterpret);
}
error(loc, "a struct is not a valid initializer for a %s", t.toChars());
return new ErrorInitializer();
}
/***************************************
* This works by transforming a struct initializer into
* a struct literal. In the future, the two should be the
* same thing.
*/
override Expression toExpression(Type t = null)
{
// cannot convert to an expression without target 'ad'
return null;
}
override StructInitializer isStructInitializer()
{
return this;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class ArrayInitializer : Initializer
{
public:
Expressions index; // indices
Initializers value; // of Initializer *'s
size_t dim; // length of array being initialized
Type type; // type that array will be used to initialize
bool sem; // true if semantic() is run
extern (D) this(Loc loc)
{
super(loc);
}
override Initializer syntaxCopy()
{
//printf("ArrayInitializer::syntaxCopy()\n");
auto ai = new ArrayInitializer(loc);
assert(index.dim == value.dim);
ai.index.setDim(index.dim);
ai.value.setDim(value.dim);
for (size_t i = 0; i < ai.value.dim; i++)
{
ai.index[i] = index[i] ? index[i].syntaxCopy() : null;
ai.value[i] = value[i].syntaxCopy();
}
return ai;
}
void addInit(Expression index, Initializer value)
{
this.index.push(index);
this.value.push(value);
dim = 0;
type = null;
}
bool isAssociativeArray()
{
for (size_t i = 0; i < value.dim; i++)
{
if (index[i])
return true;
}
return false;
}
override Initializer inferType(Scope* sc)
{
//printf("ArrayInitializer::inferType() %s\n", toChars());
Expressions* keys = null;
Expressions* values;
if (isAssociativeArray())
{
keys = new Expressions();
keys.setDim(value.dim);
values = new Expressions();
values.setDim(value.dim);
for (size_t i = 0; i < value.dim; i++)
{
Expression e = index[i];
if (!e)
goto Lno;
(*keys)[i] = e;
Initializer iz = value[i];
if (!iz)
goto Lno;
iz = iz.inferType(sc);
if (iz.isErrorInitializer())
return iz;
assert(iz.isExpInitializer());
(*values)[i] = (cast(ExpInitializer)iz).exp;
assert((*values)[i].op != TOKerror);
}
Expression e = new AssocArrayLiteralExp(loc, keys, values);
auto ei = new ExpInitializer(loc, e);
return ei.inferType(sc);
}
else
{
auto elements = new Expressions();
elements.setDim(value.dim);
elements.zero();
for (size_t i = 0; i < value.dim; i++)
{
assert(!index[i]); // already asserted by isAssociativeArray()
Initializer iz = value[i];
if (!iz)
goto Lno;
iz = iz.inferType(sc);
if (iz.isErrorInitializer())
return iz;
assert(iz.isExpInitializer());
(*elements)[i] = (cast(ExpInitializer)iz).exp;
assert((*elements)[i].op != TOKerror);
}
Expression e = new ArrayLiteralExp(loc, elements);
auto ei = new ExpInitializer(loc, e);
return ei.inferType(sc);
}
Lno:
if (keys)
{
error(loc, "not an associative array initializer");
}
else
{
error(loc, "cannot infer type from array initializer");
}
return new ErrorInitializer();
}
override Initializer semantic(Scope* sc, Type t, NeedInterpret needInterpret)
{
size_t length;
const(uint) amax = 0x80000000;
bool errors = false;
//printf("ArrayInitializer::semantic(%s)\n", t->toChars());
if (sem) // if semantic() already run
return this;
sem = true;
t = t.toBasetype();
switch (t.ty)
{
case Tsarray:
case Tarray:
break;
case Tvector:
t = (cast(TypeVector)t).basetype;
break;
case Taarray:
case Tstruct:
// consider implicit constructor call
{
Expression e;
// note: MyStruct foo = [1:2, 3:4] is correct code if MyStruct has a this(int[int])
if (t.ty == Taarray || isAssociativeArray())
e = toAssocArrayLiteral();
else
e = toExpression();
if (!e) // Bugzilla 13987
{
error(loc, "cannot use array to initialize %s", t.toChars());
goto Lerr;
}
auto ei = new ExpInitializer(e.loc, e);
return ei.semantic(sc, t, needInterpret);
}
case Tpointer:
if (t.nextOf().ty != Tfunction)
break;
default:
error(loc, "cannot use array to initialize %s", t.toChars());
goto Lerr;
}
type = t;
length = 0;
for (size_t i = 0; i < index.dim; i++)
{
Expression idx = index[i];
if (idx)
{
sc = sc.startCTFE();
idx = idx.semantic(sc);
sc = sc.endCTFE();
idx = idx.ctfeInterpret();
index[i] = idx;
length = cast(size_t)idx.toInteger();
if (idx.op == TOKerror)
errors = true;
}
Initializer val = value[i];
ExpInitializer ei = val.isExpInitializer();
if (ei && !idx)
ei.expandTuples = true;
val = val.semantic(sc, t.nextOf(), needInterpret);
if (val.isErrorInitializer())
errors = true;
ei = val.isExpInitializer();
// found a tuple, expand it
if (ei && ei.exp.op == TOKtuple)
{
TupleExp te = cast(TupleExp)ei.exp;
index.remove(i);
value.remove(i);
for (size_t j = 0; j < te.exps.dim; ++j)
{
Expression e = (*te.exps)[j];
index.insert(i + j, cast(Expression)null);
value.insert(i + j, new ExpInitializer(e.loc, e));
}
i--;
continue;
}
else
{
value[i] = val;
}
length++;
if (length == 0)
{
error(loc, "array dimension overflow");
goto Lerr;
}
if (length > dim)
dim = length;
}
if (t.ty == Tsarray)
{
dinteger_t edim = (cast(TypeSArray)t).dim.toInteger();
if (dim > edim)
{
error(loc, "array initializer has %u elements, but array length is %lld", dim, edim);
goto Lerr;
}
}
if (errors)
goto Lerr;
if (cast(uinteger_t)dim * t.nextOf().size() >= amax)
{
error(loc, "array dimension %u exceeds max of %u", cast(uint)dim, cast(uint)(amax / t.nextOf().size()));
goto Lerr;
}
return this;
Lerr:
return new ErrorInitializer();
}
/********************************
* If possible, convert array initializer to array literal.
* Otherwise return NULL.
*/
override Expression toExpression(Type tx = null)
{
//printf("ArrayInitializer::toExpression(), dim = %d\n", dim);
//static int i; if (++i == 2) assert(0);
Expressions* elements;
size_t edim;
Type t = null;
if (type)
{
if (type == Type.terror)
return new ErrorExp();
t = type.toBasetype();
switch (t.ty)
{
case Tsarray:
edim = cast(size_t)(cast(TypeSArray)t).dim.toInteger();
break;
case Tvector:
t = (cast(TypeVector)t).basetype;
edim = cast(size_t)(cast(TypeSArray)t).dim.toInteger();
break;
case Tpointer:
case Tarray:
edim = dim;
break;
default:
assert(0);
}
}
else
{
edim = value.dim;
for (size_t i = 0, j = 0; i < value.dim; i++, j++)
{
if (index[i])
{
if (index[i].op == TOKint64)
j = cast(size_t)index[i].toInteger();
else
goto Lno;
}
if (j >= edim)
edim = j + 1;
}
}
elements = new Expressions();
elements.setDim(edim);
elements.zero();
for (size_t i = 0, j = 0; i < value.dim; i++, j++)
{
if (index[i])
j = cast(size_t)index[i].toInteger();
assert(j < edim);
Initializer iz = value[i];
if (!iz)
goto Lno;
Expression ex = iz.toExpression();
if (!ex)
{
goto Lno;
}
(*elements)[j] = ex;
}
/* Fill in any missing elements with the default initializer
*/
{
Expression _init = null;
for (size_t i = 0; i < edim; i++)
{
if (!(*elements)[i])
{
if (!type)
goto Lno;
if (!_init)
_init = (cast(TypeNext)t).next.defaultInit();
(*elements)[i] = _init;
}
}
for (size_t i = 0; i < edim; i++)
{
Expression e = (*elements)[i];
if (e.op == TOKerror)
return e;
}
Expression e = new ArrayLiteralExp(loc, elements);
e.type = type;
return e;
}
Lno:
return null;
}
/********************************
* If possible, convert array initializer to associative array initializer.
*/
Expression toAssocArrayLiteral()
{
Expression e;
//printf("ArrayInitializer::toAssocArrayInitializer()\n");
//static int i; if (++i == 2) assert(0);
auto keys = new Expressions();
keys.setDim(value.dim);
auto values = new Expressions();
values.setDim(value.dim);
for (size_t i = 0; i < value.dim; i++)
{
e = index[i];
if (!e)
goto Lno;
(*keys)[i] = e;
Initializer iz = value[i];
if (!iz)
goto Lno;
e = iz.toExpression();
if (!e)
goto Lno;
(*values)[i] = e;
}
e = new AssocArrayLiteralExp(loc, keys, values);
return e;
Lno:
error(loc, "not an associative array initializer");
return new ErrorExp();
}
override ArrayInitializer isArrayInitializer()
{
return this;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class ExpInitializer : Initializer
{
public:
Expression exp;
bool expandTuples;
extern (D) this(Loc loc, Expression exp)
{
super(loc);
this.exp = exp;
}
override Initializer syntaxCopy()
{
return new ExpInitializer(loc, exp.syntaxCopy());
}
override Initializer inferType(Scope* sc)
{
//printf("ExpInitializer::inferType() %s\n", toChars());
exp = exp.semantic(sc);
exp = resolveProperties(sc, exp);
if (exp.op == TOKimport)
{
ScopeExp se = cast(ScopeExp)exp;
TemplateInstance ti = se.sds.isTemplateInstance();
if (ti && ti.semanticRun == PASSsemantic && !ti.aliasdecl)
se.error("cannot infer type from %s %s, possible circular dependency", se.sds.kind(), se.toChars());
else
se.error("cannot infer type from %s %s", se.sds.kind(), se.toChars());
return new ErrorInitializer();
}
// Give error for overloaded function addresses
if (exp.op == TOKsymoff)
{
SymOffExp se = cast(SymOffExp)exp;
if (se.hasOverloads && !se.var.isFuncDeclaration().isUnique())
{
exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
return new ErrorInitializer();
}
}
if (exp.op == TOKdelegate)
{
DelegateExp se = cast(DelegateExp)exp;
if (se.hasOverloads && se.func.isFuncDeclaration() && !se.func.isFuncDeclaration().isUnique())
{
exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
return new ErrorInitializer();
}
}
if (exp.op == TOKaddress)
{
AddrExp ae = cast(AddrExp)exp;
if (ae.e1.op == TOKoverloadset)
{
exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
return new ErrorInitializer();
}
}
if (exp.op == TOKerror)
return new ErrorInitializer();
if (!exp.type)
return new ErrorInitializer();
return this;
}
override Initializer semantic(Scope* sc, Type t, NeedInterpret needInterpret)
{
//printf("ExpInitializer::semantic(%s), type = %s\n", exp->toChars(), t->toChars());
if (needInterpret)
sc = sc.startCTFE();
exp = exp.semantic(sc);
exp = resolveProperties(sc, exp);
if (needInterpret)
sc = sc.endCTFE();
if (exp.op == TOKerror)
return new ErrorInitializer();
uint olderrors = global.errors;
if (needInterpret)
{
// If the result will be implicitly cast, move the cast into CTFE
// to avoid premature truncation of polysemous types.
// eg real [] x = [1.1, 2.2]; should use real precision.
if (exp.implicitConvTo(t))
{
exp = exp.implicitCastTo(sc, t);
}
exp = exp.ctfeInterpret();
}
else
{
exp = exp.optimize(WANTvalue);
}
if (!global.gag && olderrors != global.errors)
return this; // Failed, suppress duplicate error messages
if (exp.type.ty == Ttuple && (cast(TypeTuple)exp.type).arguments.dim == 0)
{
Type et = exp.type;
exp = new TupleExp(exp.loc, new Expressions());
exp.type = et;
}
if (exp.op == TOKtype)
{
exp.error("initializer must be an expression, not '%s'", exp.toChars());
return new ErrorInitializer();
}
// Make sure all pointers are constants
if (needInterpret && hasNonConstPointers(exp))
{
exp.error("cannot use non-constant CTFE pointer in an initializer '%s'", exp.toChars());
return new ErrorInitializer();
}
Type tb = t.toBasetype();
Type ti = exp.type.toBasetype();
if (exp.op == TOKtuple && expandTuples && !exp.implicitConvTo(t))
return new ExpInitializer(loc, exp);
/* Look for case of initializing a static array with a too-short
* string literal, such as:
* char[5] foo = "abc";
* Allow this by doing an explicit cast, which will lengthen the string
* literal.
*/
if (exp.op == TOKstring && tb.ty == Tsarray)
{
StringExp se = cast(StringExp)exp;
Type typeb = se.type.toBasetype();
TY tynto = tb.nextOf().ty;
if (!se.committed &&
(typeb.ty == Tarray || typeb.ty == Tsarray) &&
(tynto == Tchar || tynto == Twchar || tynto == Tdchar) &&
se.numberOfCodeUnits(tynto) < (cast(TypeSArray)tb).dim.toInteger())
{
exp = se.castTo(sc, t);
goto L1;
}
}
// Look for implicit constructor call
if (tb.ty == Tstruct && !(ti.ty == Tstruct && tb.toDsymbol(sc) == ti.toDsymbol(sc)) && !exp.implicitConvTo(t))
{
StructDeclaration sd = (cast(TypeStruct)tb).sym;
if (sd.ctor)
{
// Rewrite as S().ctor(exp)
Expression e;
e = new StructLiteralExp(loc, sd, null);
e = new DotIdExp(loc, e, Id.ctor);
e = new CallExp(loc, e, exp);
e = e.semantic(sc);
if (needInterpret)
exp = e.ctfeInterpret();
else
exp = e.optimize(WANTvalue);
}
}
// Look for the case of statically initializing an array
// with a single member.
if (tb.ty == Tsarray && !tb.nextOf().equals(ti.toBasetype().nextOf()) && exp.implicitConvTo(tb.nextOf()))
{
/* If the variable is not actually used in compile time, array creation is
* redundant. So delay it until invocation of toExpression() or toDt().
*/
t = tb.nextOf();
}
if (exp.implicitConvTo(t))
{
exp = exp.implicitCastTo(sc, t);
}
else
{
// Look for mismatch of compile-time known length to emit
// better diagnostic message, as same as AssignExp::semantic.
if (tb.ty == Tsarray && exp.implicitConvTo(tb.nextOf().arrayOf()) > MATCHnomatch)
{
uinteger_t dim1 = (cast(TypeSArray)tb).dim.toInteger();
uinteger_t dim2 = dim1;
if (exp.op == TOKarrayliteral)
{
ArrayLiteralExp ale = cast(ArrayLiteralExp)exp;
dim2 = ale.elements ? ale.elements.dim : 0;
}
else if (exp.op == TOKslice)
{
Type tx = toStaticArrayType(cast(SliceExp)exp);
if (tx)
dim2 = (cast(TypeSArray)tx).dim.toInteger();
}
if (dim1 != dim2)
{
exp.error("mismatched array lengths, %d and %d", cast(int)dim1, cast(int)dim2);
exp = new ErrorExp();
}
}
exp = exp.implicitCastTo(sc, t);
}
L1:
if (exp.op == TOKerror)
return this;
if (needInterpret)
exp = exp.ctfeInterpret();
else
exp = exp.optimize(WANTvalue);
//printf("-ExpInitializer::semantic(): "); exp->print();
return this;
}
override Expression toExpression(Type t = null)
{
if (t)
{
//printf("ExpInitializer::toExpression(t = %s) exp = %s\n", t->toChars(), exp->toChars());
Type tb = t.toBasetype();
Expression e = (exp.op == TOKconstruct || exp.op == TOKblit) ? (cast(AssignExp)exp).e2 : exp;
if (tb.ty == Tsarray && e.implicitConvTo(tb.nextOf()))
{
TypeSArray tsa = cast(TypeSArray)tb;
size_t d = cast(size_t)tsa.dim.toInteger();
auto elements = new Expressions();
elements.setDim(d);
for (size_t i = 0; i < d; i++)
(*elements)[i] = e;
auto ae = new ArrayLiteralExp(e.loc, elements);
ae.type = t;
return ae;
}
}
return exp;
}
override ExpInitializer isExpInitializer()
{
return this;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
version (all)
{
extern (C++) bool hasNonConstPointers(Expression e)
{
if (e.type.ty == Terror)
return false;