/
dsymbolsem.d
5586 lines (5017 loc) · 198 KB
/
dsymbolsem.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-2018 by The D Language Foundation, 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: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/dsymbolsem.d, _dsymbolsem.d)
* Documentation: https://dlang.org/phobos/dmd_dsymbolsem.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/dsymbolsem.d
*/
module dmd.dsymbolsem;
import core.stdc.stdio;
import core.stdc.string;
import dmd.aggregate;
import dmd.aliasthis;
import dmd.arraytypes;
import dmd.astcodegen;
import dmd.attrib;
import dmd.blockexit;
import dmd.clone;
import dmd.dcast;
import dmd.dclass;
import dmd.declaration;
import dmd.denum;
import dmd.dimport;
import dmd.dinterpret;
import dmd.dmodule;
import dmd.dscope;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.dversion;
import dmd.errors;
import dmd.escape;
import dmd.expression;
import dmd.expressionsem;
import dmd.func;
import dmd.globals;
import dmd.gluelayer;
import dmd.id;
import dmd.identifier;
import dmd.init;
import dmd.initsem;
import dmd.hdrgen;
import dmd.mars;
import dmd.mtype;
import dmd.nogc;
import dmd.nspace;
import dmd.objc;
import dmd.opover;
import dmd.parse;
import dmd.root.filename;
import dmd.root.outbuffer;
import dmd.root.rmem;
import dmd.root.rootobject;
import dmd.semantic2;
import dmd.semantic3;
import dmd.sideeffect;
import dmd.statementsem;
import dmd.staticassert;
import dmd.tokens;
import dmd.utf;
import dmd.utils;
import dmd.statement;
import dmd.target;
import dmd.templateparamsem;
import dmd.typesem;
import dmd.visitor;
enum LOG = false;
private uint setMangleOverride(Dsymbol s, char* sym)
{
AttribDeclaration ad = s.isAttribDeclaration();
if (ad)
{
Dsymbols* decls = ad.include(null);
uint nestedCount = 0;
if (decls && decls.dim)
for (size_t i = 0; i < decls.dim; ++i)
nestedCount += setMangleOverride((*decls)[i], sym);
return nestedCount;
}
else if (s.isFuncDeclaration() || s.isVarDeclaration())
{
s.isDeclaration().mangleOverride = sym;
return 1;
}
else
return 0;
}
/*************************************
* Does semantic analysis on the public face of declarations.
*/
extern(C++) void dsymbolSemantic(Dsymbol dsym, Scope* sc)
{
scope v = new DsymbolSemanticVisitor(sc);
dsym.accept(v);
}
structalign_t getAlignment(AlignDeclaration ad, Scope* sc)
{
if (ad.salign != ad.UNKNOWN)
return ad.salign;
if (!ad.ealign)
return ad.salign = STRUCTALIGN_DEFAULT;
sc = sc.startCTFE();
ad.ealign = ad.ealign.expressionSemantic(sc);
ad.ealign = resolveProperties(sc, ad.ealign);
sc = sc.endCTFE();
ad.ealign = ad.ealign.ctfeInterpret();
if (ad.ealign.op == TOK.error)
return ad.salign = STRUCTALIGN_DEFAULT;
Type tb = ad.ealign.type.toBasetype();
auto n = ad.ealign.toInteger();
if (n < 1 || n & (n - 1) || structalign_t.max < n || !tb.isintegral())
{
error(ad.loc, "alignment must be an integer positive power of 2, not %s", ad.ealign.toChars());
return ad.salign = STRUCTALIGN_DEFAULT;
}
return ad.salign = cast(structalign_t)n;
}
const(char)* getMessage(DeprecatedDeclaration dd)
{
if (auto sc = dd._scope)
{
dd._scope = null;
sc = sc.startCTFE();
dd.msg = dd.msg.expressionSemantic(sc);
dd.msg = resolveProperties(sc, dd.msg);
sc = sc.endCTFE();
dd.msg = dd.msg.ctfeInterpret();
if (auto se = dd.msg.toStringExp())
dd.msgstr = se.toStringz().ptr;
else
dd.msg.error("compile time constant expected, not `%s`", dd.msg.toChars());
}
return dd.msgstr;
}
// Returns true if a contract can appear without a function body.
package bool allowsContractWithoutBody(FuncDeclaration funcdecl)
{
assert(!funcdecl.fbody);
/* Contracts can only appear without a body when they are virtual
* interface functions or abstract.
*/
Dsymbol parent = funcdecl.toParent();
InterfaceDeclaration id = parent.isInterfaceDeclaration();
if (!funcdecl.isAbstract() &&
(funcdecl.fensure || funcdecl.frequire) &&
!(id && funcdecl.isVirtual()))
{
auto cd = parent.isClassDeclaration();
if (!(cd && cd.isAbstract()))
return false;
}
return true;
}
private extern(C++) final class DsymbolSemanticVisitor : Visitor
{
alias visit = Visitor.visit;
Scope* sc;
this(Scope* sc)
{
this.sc = sc;
}
override void visit(Dsymbol dsym)
{
dsym.error("%p has no semantic routine", dsym);
}
override void visit(ScopeDsymbol) { }
override void visit(Declaration) { }
override void visit(AliasThis dsym)
{
if (dsym.semanticRun != PASS.init)
return;
if (dsym._scope)
{
sc = dsym._scope;
dsym._scope = null;
}
if (!sc)
return;
dsym.semanticRun = PASS.semantic;
Dsymbol p = sc.parent.pastMixin();
AggregateDeclaration ad = p.isAggregateDeclaration();
if (!ad)
{
error(dsym.loc, "alias this can only be a member of aggregate, not %s `%s`", p.kind(), p.toChars());
return;
}
assert(ad.members);
Dsymbol s = ad.search(dsym.loc, dsym.ident);
if (!s)
{
s = sc.search(dsym.loc, dsym.ident, null);
if (s)
error(dsym.loc, "`%s` is not a member of `%s`", s.toChars(), ad.toChars());
else
error(dsym.loc, "undefined identifier `%s`", dsym.ident.toChars());
return;
}
if (ad.aliasthis && s != ad.aliasthis)
{
error(dsym.loc, "there can be only one alias this");
return;
}
/* disable the alias this conversion so the implicit conversion check
* doesn't use it.
*/
ad.aliasthis = null;
Dsymbol sx = s;
if (sx.isAliasDeclaration())
sx = sx.toAlias();
Declaration d = sx.isDeclaration();
if (d && !d.isTupleDeclaration())
{
/* https://issues.dlang.org/show_bug.cgi?id=18429
*
* If the identifier in the AliasThis declaration
* is defined later and is a voldemort type, we must
* perform semantic on the declaration to deduce the type.
*/
if (!d.type)
d.dsymbolSemantic(sc);
Type t = d.type;
assert(t);
if (ad.type.implicitConvTo(t) > MATCH.nomatch)
{
error(dsym.loc, "alias this is not reachable as `%s` already converts to `%s`", ad.toChars(), t.toChars());
}
}
ad.aliasthis = s;
dsym.semanticRun = PASS.semanticdone;
}
override void visit(AliasDeclaration dsym)
{
if (dsym.semanticRun >= PASS.semanticdone)
return;
assert(dsym.semanticRun <= PASS.semantic);
dsym.storage_class |= sc.stc & STC.deprecated_;
dsym.protection = sc.protection;
dsym.userAttribDecl = sc.userAttribDecl;
if (!sc.func && dsym.inNonRoot())
return;
aliasSemantic(dsym, sc);
}
override void visit(VarDeclaration dsym)
{
version (none)
{
printf("VarDeclaration::semantic('%s', parent = '%s') sem = %d\n", toChars(), sc.parent ? sc.parent.toChars() : null, sem);
printf(" type = %s\n", type ? type.toChars() : "null");
printf(" stc = x%x\n", sc.stc);
printf(" storage_class = x%llx\n", storage_class);
printf("linkage = %d\n", sc.linkage);
//if (strcmp(toChars(), "mul") == 0) assert(0);
}
//if (semanticRun > PASS.init)
// return;
//semanticRun = PSSsemantic;
if (dsym.semanticRun >= PASS.semanticdone)
return;
Scope* scx = null;
if (dsym._scope)
{
sc = dsym._scope;
scx = sc;
dsym._scope = null;
}
if (!sc)
return;
dsym.semanticRun = PASS.semantic;
/* Pick up storage classes from context, but except synchronized,
* override, abstract, and final.
*/
dsym.storage_class |= (sc.stc & ~(STC.synchronized_ | STC.override_ | STC.abstract_ | STC.final_));
if (dsym.storage_class & STC.extern_ && dsym._init)
dsym.error("extern symbols cannot have initializers");
dsym.userAttribDecl = sc.userAttribDecl;
AggregateDeclaration ad = dsym.isThis();
if (ad)
dsym.storage_class |= ad.storage_class & STC.TYPECTOR;
/* If auto type inference, do the inference
*/
int inferred = 0;
if (!dsym.type)
{
dsym.inuse++;
// Infering the type requires running semantic,
// so mark the scope as ctfe if required
bool needctfe = (dsym.storage_class & (STC.manifest | STC.static_)) != 0;
if (needctfe)
sc = sc.startCTFE();
//printf("inferring type for %s with init %s\n", toChars(), _init.toChars());
dsym._init = dsym._init.inferType(sc);
dsym.type = dsym._init.initializerToExpression().type;
if (needctfe)
sc = sc.endCTFE();
dsym.inuse--;
inferred = 1;
/* This is a kludge to support the existing syntax for RAII
* declarations.
*/
dsym.storage_class &= ~STC.auto_;
dsym.originalType = dsym.type.syntaxCopy();
}
else
{
if (!dsym.originalType)
dsym.originalType = dsym.type.syntaxCopy();
/* Prefix function attributes of variable declaration can affect
* its type:
* pure nothrow void function() fp;
* static assert(is(typeof(fp) == void function() pure nothrow));
*/
Scope* sc2 = sc.push();
sc2.stc |= (dsym.storage_class & STC.FUNCATTR);
dsym.inuse++;
dsym.type = dsym.type.typeSemantic(dsym.loc, sc2);
dsym.inuse--;
sc2.pop();
}
//printf(" semantic type = %s\n", type ? type.toChars() : "null");
if (dsym.type.ty == Terror)
dsym.errors = true;
dsym.type.checkDeprecated(dsym.loc, sc);
dsym.linkage = sc.linkage;
dsym.parent = sc.parent;
//printf("this = %p, parent = %p, '%s'\n", this, parent, parent.toChars());
dsym.protection = sc.protection;
/* If scope's alignment is the default, use the type's alignment,
* otherwise the scope overrrides.
*/
dsym.alignment = sc.alignment();
if (dsym.alignment == STRUCTALIGN_DEFAULT)
dsym.alignment = dsym.type.alignment(); // use type's alignment
//printf("sc.stc = %x\n", sc.stc);
//printf("storage_class = x%x\n", storage_class);
if (global.params.vcomplex)
dsym.type.checkComplexTransition(dsym.loc, sc);
// Calculate type size + safety checks
if (sc.func && !sc.intypeof)
{
if (dsym.storage_class & STC.gshared && !dsym.isMember())
{
if (sc.func.setUnsafe())
dsym.error("__gshared not allowed in safe functions; use shared");
}
}
Dsymbol parent = dsym.toParent();
Type tb = dsym.type.toBasetype();
Type tbn = tb.baseElemOf();
if (tb.ty == Tvoid && !(dsym.storage_class & STC.lazy_))
{
if (inferred)
{
dsym.error("type `%s` is inferred from initializer `%s`, and variables cannot be of type `void`", dsym.type.toChars(), dsym._init.toChars());
}
else
dsym.error("variables cannot be of type `void`");
dsym.type = Type.terror;
tb = dsym.type;
}
if (tb.ty == Tfunction)
{
dsym.error("cannot be declared to be a function");
dsym.type = Type.terror;
tb = dsym.type;
}
if (tb.ty == Tstruct)
{
TypeStruct ts = cast(TypeStruct)tb;
if (!ts.sym.members)
{
dsym.error("no definition of struct `%s`", ts.toChars());
}
}
if ((dsym.storage_class & STC.auto_) && !inferred)
dsym.error("storage class `auto` has no effect if type is not inferred, did you mean `scope`?");
if (tb.ty == Ttuple)
{
/* Instead, declare variables for each of the tuple elements
* and add those.
*/
TypeTuple tt = cast(TypeTuple)tb;
size_t nelems = Parameter.dim(tt.arguments);
Expression ie = (dsym._init && !dsym._init.isVoidInitializer()) ? dsym._init.initializerToExpression() : null;
if (ie)
ie = ie.expressionSemantic(sc);
if (nelems > 0 && ie)
{
auto iexps = new Expressions();
iexps.push(ie);
auto exps = new Expressions();
for (size_t pos = 0; pos < iexps.dim; pos++)
{
Lexpand1:
Expression e = (*iexps)[pos];
Parameter arg = Parameter.getNth(tt.arguments, pos);
arg.type = arg.type.typeSemantic(dsym.loc, sc);
//printf("[%d] iexps.dim = %d, ", pos, iexps.dim);
//printf("e = (%s %s, %s), ", Token::tochars[e.op], e.toChars(), e.type.toChars());
//printf("arg = (%s, %s)\n", arg.toChars(), arg.type.toChars());
if (e != ie)
{
if (iexps.dim > nelems)
goto Lnomatch;
if (e.type.implicitConvTo(arg.type))
continue;
}
if (e.op == TOK.tuple)
{
TupleExp te = cast(TupleExp)e;
if (iexps.dim - 1 + te.exps.dim > nelems)
goto Lnomatch;
iexps.remove(pos);
iexps.insert(pos, te.exps);
(*iexps)[pos] = Expression.combine(te.e0, (*iexps)[pos]);
goto Lexpand1;
}
else if (isAliasThisTuple(e))
{
auto v = copyToTemp(0, "__tup", e);
v.dsymbolSemantic(sc);
auto ve = new VarExp(dsym.loc, v);
ve.type = e.type;
exps.setDim(1);
(*exps)[0] = ve;
expandAliasThisTuples(exps, 0);
for (size_t u = 0; u < exps.dim; u++)
{
Lexpand2:
Expression ee = (*exps)[u];
arg = Parameter.getNth(tt.arguments, pos + u);
arg.type = arg.type.typeSemantic(dsym.loc, sc);
//printf("[%d+%d] exps.dim = %d, ", pos, u, exps.dim);
//printf("ee = (%s %s, %s), ", Token::tochars[ee.op], ee.toChars(), ee.type.toChars());
//printf("arg = (%s, %s)\n", arg.toChars(), arg.type.toChars());
size_t iexps_dim = iexps.dim - 1 + exps.dim;
if (iexps_dim > nelems)
goto Lnomatch;
if (ee.type.implicitConvTo(arg.type))
continue;
if (expandAliasThisTuples(exps, u) != -1)
goto Lexpand2;
}
if ((*exps)[0] != ve)
{
Expression e0 = (*exps)[0];
(*exps)[0] = new CommaExp(dsym.loc, new DeclarationExp(dsym.loc, v), e0);
(*exps)[0].type = e0.type;
iexps.remove(pos);
iexps.insert(pos, exps);
goto Lexpand1;
}
}
}
if (iexps.dim < nelems)
goto Lnomatch;
ie = new TupleExp(dsym._init.loc, iexps);
}
Lnomatch:
if (ie && ie.op == TOK.tuple)
{
TupleExp te = cast(TupleExp)ie;
size_t tedim = te.exps.dim;
if (tedim != nelems)
{
error(dsym.loc, "tuple of %d elements cannot be assigned to tuple of %d elements", cast(int)tedim, cast(int)nelems);
for (size_t u = tedim; u < nelems; u++) // fill dummy expression
te.exps.push(new ErrorExp());
}
}
auto exps = new Objects();
exps.setDim(nelems);
for (size_t i = 0; i < nelems; i++)
{
Parameter arg = Parameter.getNth(tt.arguments, i);
OutBuffer buf;
buf.printf("__%s_field_%llu", dsym.ident.toChars(), cast(ulong)i);
auto id = Identifier.idPool(buf.peekSlice());
Initializer ti;
if (ie)
{
Expression einit = ie;
if (ie.op == TOK.tuple)
{
TupleExp te = cast(TupleExp)ie;
einit = (*te.exps)[i];
if (i == 0)
einit = Expression.combine(te.e0, einit);
}
ti = new ExpInitializer(einit.loc, einit);
}
else
ti = dsym._init ? dsym._init.syntaxCopy() : null;
StorageClass storage_class = STC.temp | dsym.storage_class;
if (arg.storageClass & STC.parameter)
storage_class |= arg.storageClass;
auto v = new VarDeclaration(dsym.loc, arg.type, id, ti, storage_class);
//printf("declaring field %s of type %s\n", v.toChars(), v.type.toChars());
v.dsymbolSemantic(sc);
if (sc.scopesym)
{
//printf("adding %s to %s\n", v.toChars(), sc.scopesym.toChars());
if (sc.scopesym.members)
// Note this prevents using foreach() over members, because the limits can change
sc.scopesym.members.push(v);
}
Expression e = new DsymbolExp(dsym.loc, v);
(*exps)[i] = e;
}
auto v2 = new TupleDeclaration(dsym.loc, dsym.ident, exps);
v2.parent = dsym.parent;
v2.isexp = true;
dsym.aliassym = v2;
dsym.semanticRun = PASS.semanticdone;
return;
}
/* Storage class can modify the type
*/
dsym.type = dsym.type.addStorageClass(dsym.storage_class);
/* Adjust storage class to reflect type
*/
if (dsym.type.isConst())
{
dsym.storage_class |= STC.const_;
if (dsym.type.isShared())
dsym.storage_class |= STC.shared_;
}
else if (dsym.type.isImmutable())
dsym.storage_class |= STC.immutable_;
else if (dsym.type.isShared())
dsym.storage_class |= STC.shared_;
else if (dsym.type.isWild())
dsym.storage_class |= STC.wild;
if (StorageClass stc = dsym.storage_class & (STC.synchronized_ | STC.override_ | STC.abstract_ | STC.final_))
{
if (stc == STC.final_)
dsym.error("cannot be `final`, perhaps you meant `const`?");
else
{
OutBuffer buf;
stcToBuffer(&buf, stc);
dsym.error("cannot be `%s`", buf.peekString());
}
dsym.storage_class &= ~stc; // strip off
}
if (dsym.storage_class & STC.scope_)
{
StorageClass stc = dsym.storage_class & (STC.static_ | STC.extern_ | STC.manifest | STC.tls | STC.gshared);
if (stc)
{
OutBuffer buf;
stcToBuffer(&buf, stc);
dsym.error("cannot be `scope` and `%s`", buf.peekString());
}
else if (dsym.isMember())
{
dsym.error("field cannot be `scope`");
}
else if (!dsym.type.hasPointers())
{
dsym.storage_class &= ~STC.scope_; // silently ignore; may occur in generic code
}
}
if (dsym.storage_class & (STC.static_ | STC.extern_ | STC.manifest | STC.templateparameter | STC.tls | STC.gshared | STC.ctfe))
{
}
else
{
AggregateDeclaration aad = parent.isAggregateDeclaration();
if (aad)
{
if (global.params.vfield && dsym.storage_class & (STC.const_ | STC.immutable_) && dsym._init && !dsym._init.isVoidInitializer())
{
const(char)* p = dsym.loc.toChars();
const(char)* s = (dsym.storage_class & STC.immutable_) ? "immutable" : "const";
message("%s: %s.%s is %s field", p ? p : "", ad.toPrettyChars(), dsym.toChars(), s);
}
dsym.storage_class |= STC.field;
if (tbn.ty == Tstruct && (cast(TypeStruct)tbn).sym.noDefaultCtor)
{
if (!dsym.isThisDeclaration() && !dsym._init)
aad.noDefaultCtor = true;
}
}
InterfaceDeclaration id = parent.isInterfaceDeclaration();
if (id)
{
dsym.error("field not allowed in interface");
}
else if (aad && aad.sizeok == Sizeok.done)
{
dsym.error("cannot be further field because it will change the determined %s size", aad.toChars());
}
/* Templates cannot add fields to aggregates
*/
TemplateInstance ti = parent.isTemplateInstance();
if (ti)
{
// Take care of nested templates
while (1)
{
TemplateInstance ti2 = ti.tempdecl.parent.isTemplateInstance();
if (!ti2)
break;
ti = ti2;
}
// If it's a member template
AggregateDeclaration ad2 = ti.tempdecl.isMember();
if (ad2 && dsym.storage_class != STC.undefined_)
{
dsym.error("cannot use template to add field to aggregate `%s`", ad2.toChars());
}
}
}
if ((dsym.storage_class & (STC.ref_ | STC.parameter | STC.foreach_ | STC.temp | STC.result)) == STC.ref_ && dsym.ident != Id.This)
{
dsym.error("only parameters or `foreach` declarations can be `ref`");
}
if (dsym.type.hasWild())
{
if (dsym.storage_class & (STC.static_ | STC.extern_ | STC.tls | STC.gshared | STC.manifest | STC.field) || dsym.isDataseg())
{
dsym.error("only parameters or stack based variables can be `inout`");
}
FuncDeclaration func = sc.func;
if (func)
{
if (func.fes)
func = func.fes.func;
bool isWild = false;
for (FuncDeclaration fd = func; fd; fd = fd.toParent2().isFuncDeclaration())
{
if ((cast(TypeFunction)fd.type).iswild)
{
isWild = true;
break;
}
}
if (!isWild)
{
dsym.error("`inout` variables can only be declared inside `inout` functions");
}
}
}
if (!(dsym.storage_class & (STC.ctfe | STC.ref_ | STC.result)) && tbn.ty == Tstruct && (cast(TypeStruct)tbn).sym.noDefaultCtor)
{
if (!dsym._init)
{
if (dsym.isField())
{
/* For fields, we'll check the constructor later to make sure it is initialized
*/
dsym.storage_class |= STC.nodefaultctor;
}
else if (dsym.storage_class & STC.parameter)
{
}
else
dsym.error("default construction is disabled for type `%s`", dsym.type.toChars());
}
}
FuncDeclaration fd = parent.isFuncDeclaration();
if (dsym.type.isscope() && !(dsym.storage_class & STC.nodtor))
{
if (dsym.storage_class & (STC.field | STC.out_ | STC.ref_ | STC.static_ | STC.manifest | STC.tls | STC.gshared) || !fd)
{
dsym.error("globals, statics, fields, manifest constants, ref and out parameters cannot be `scope`");
}
if (!(dsym.storage_class & STC.scope_))
{
if (!(dsym.storage_class & STC.parameter) && dsym.ident != Id.withSym)
dsym.error("reference to `scope class` must be `scope`");
}
}
// Calculate type size + safety checks
if (sc.func && !sc.intypeof)
{
if (dsym._init && dsym._init.isVoidInitializer() && dsym.type.hasPointers()) // get type size
{
if (sc.func.setUnsafe())
dsym.error("`void` initializers for pointers not allowed in safe functions");
}
else if (!dsym._init &&
!(dsym.storage_class & (STC.static_ | STC.extern_ | STC.tls | STC.gshared | STC.manifest | STC.field | STC.parameter)) &&
dsym.type.hasVoidInitPointers())
{
if (sc.func.setUnsafe())
dsym.error("`void` initializers for pointers not allowed in safe functions");
}
}
if (!dsym._init && !fd)
{
// If not mutable, initializable by constructor only
dsym.storage_class |= STC.ctorinit;
}
if (dsym._init)
dsym.storage_class |= STC.init; // remember we had an explicit initializer
else if (dsym.storage_class & STC.manifest)
dsym.error("manifest constants must have initializers");
bool isBlit = false;
d_uns64 sz;
if (!dsym._init &&
!(dsym.storage_class & (STC.static_ | STC.gshared | STC.extern_)) &&
fd &&
(!(dsym.storage_class & (STC.field | STC.in_ | STC.foreach_ | STC.parameter | STC.result)) ||
(dsym.storage_class & STC.out_)) &&
(sz = dsym.type.size()) != 0)
{
// Provide a default initializer
//printf("Providing default initializer for '%s'\n", toChars());
if (sz == SIZE_INVALID && dsym.type.ty != Terror)
dsym.error("size of type `%s` is invalid", dsym.type.toChars());
Type tv = dsym.type;
while (tv.ty == Tsarray) // Don't skip Tenum
tv = tv.nextOf();
if (tv.needsNested())
{
/* Nested struct requires valid enclosing frame pointer.
* In StructLiteralExp::toElem(), it's calculated.
*/
assert(tbn.ty == Tstruct);
checkFrameAccess(dsym.loc, sc, (cast(TypeStruct)tbn).sym);
Expression e = tv.defaultInitLiteral(dsym.loc);
e = new BlitExp(dsym.loc, new VarExp(dsym.loc, dsym), e);
e = e.expressionSemantic(sc);
dsym._init = new ExpInitializer(dsym.loc, e);
goto Ldtor;
}
if (tv.ty == Tstruct && (cast(TypeStruct)tv).sym.zeroInit == 1)
{
/* If a struct is all zeros, as a special case
* set it's initializer to the integer 0.
* In AssignExp::toElem(), we check for this and issue
* a memset() to initialize the struct.
* Must do same check in interpreter.
*/
Expression e = new IntegerExp(dsym.loc, 0, Type.tint32);
e = new BlitExp(dsym.loc, new VarExp(dsym.loc, dsym), e);
e.type = dsym.type; // don't type check this, it would fail
dsym._init = new ExpInitializer(dsym.loc, e);
goto Ldtor;
}
if (dsym.type.baseElemOf().ty == Tvoid)
{
dsym.error("`%s` does not have a default initializer", dsym.type.toChars());
}
else if (auto e = dsym.type.defaultInit(dsym.loc))
{
dsym._init = new ExpInitializer(dsym.loc, e);
}
// Default initializer is always a blit
isBlit = true;
}
if (dsym._init)
{
sc = sc.push();
sc.stc &= ~(STC.TYPECTOR | STC.pure_ | STC.nothrow_ | STC.nogc | STC.ref_ | STC.disable);
ExpInitializer ei = dsym._init.isExpInitializer();
if (ei) // https://issues.dlang.org/show_bug.cgi?id=13424
// Preset the required type to fail in FuncLiteralDeclaration::semantic3
ei.exp = inferType(ei.exp, dsym.type);
// If inside function, there is no semantic3() call
if (sc.func || sc.intypeof == 1)
{
// If local variable, use AssignExp to handle all the various
// possibilities.
if (fd && !(dsym.storage_class & (STC.manifest | STC.static_ | STC.tls | STC.gshared | STC.extern_)) && !dsym._init.isVoidInitializer())
{
//printf("fd = '%s', var = '%s'\n", fd.toChars(), toChars());
if (!ei)
{
ArrayInitializer ai = dsym._init.isArrayInitializer();
Expression e;
if (ai && tb.ty == Taarray)
e = ai.toAssocArrayLiteral();
else
e = dsym._init.initializerToExpression();
if (!e)
{
// Run semantic, but don't need to interpret
dsym._init = dsym._init.initializerSemantic(sc, dsym.type, INITnointerpret);
e = dsym._init.initializerToExpression();
if (!e)
{
dsym.error("is not a static and cannot have static initializer");
e = new ErrorExp();
}
}
ei = new ExpInitializer(dsym._init.loc, e);
dsym._init = ei;
}
Expression exp = ei.exp;
Expression e1 = new VarExp(dsym.loc, dsym);
if (isBlit)
exp = new BlitExp(dsym.loc, e1, exp);
else
exp = new ConstructExp(dsym.loc, e1, exp);
dsym.canassign++;
exp = exp.expressionSemantic(sc);
dsym.canassign--;
exp = exp.optimize(WANTvalue);
if (exp.op == TOK.error)
{
dsym._init = new ErrorInitializer();
ei = null;
}
else
ei.exp = exp;
if (ei && dsym.isScope())
{
Expression ex = ei.exp;
while (ex.op == TOK.comma)
ex = (cast(CommaExp)ex).e2;
if (ex.op == TOK.blit || ex.op == TOK.construct)
ex = (cast(AssignExp)ex).e2;
if (ex.op == TOK.new_)
{
// See if initializer is a NewExp that can be allocated on the stack
NewExp ne = cast(NewExp)ex;
if (dsym.type.toBasetype().ty == Tclass)
{
if (ne.newargs && ne.newargs.dim > 1)
{
dsym.mynew = true;
}
else
{
ne.onstack = 1;
dsym.onstack = true;
}
}
}
else if (ex.op == TOK.function_)
{
// or a delegate that doesn't escape a reference to the function
FuncDeclaration f = (cast(FuncExp)ex).fd;
f.tookAddressOf--;
}
}
}
else
{
// https://issues.dlang.org/show_bug.cgi?id=14166
// Don't run CTFE for the temporary variables inside typeof
dsym._init = dsym._init.initializerSemantic(sc, dsym.type, sc.intypeof == 1 ? INITnointerpret : INITinterpret);
}
}
else if (parent.isAggregateDeclaration())
{
dsym._scope = scx ? scx : sc.copy();
dsym._scope.setNoFree();
}
else if (dsym.storage_class & (STC.const_ | STC.immutable_ | STC.manifest) || dsym.type.isConst() || dsym.type.isImmutable())
{
/* Because we may need the results of a const declaration in a
* subsequent type, such as an array dimension, before semantic2()
* gets ordinarily run, try to run semantic2() now.
* Ignore failure.
*/
if (!inferred)
{
uint errors = global.errors;
dsym.inuse++;
if (ei)
{
Expression exp = ei.exp.syntaxCopy();
bool needctfe = dsym.isDataseg() || (dsym.storage_class & STC.manifest);
if (needctfe)
sc = sc.startCTFE();
exp = exp.expressionSemantic(sc);
exp = resolveProperties(sc, exp);
if (needctfe)
sc = sc.endCTFE();
Type tb2 = dsym.type.toBasetype();
Type ti = exp.type.toBasetype();
/* The problem is the following code:
* struct CopyTest {
* double x;
* this(double a) { x = a * 10.0;}
* this(this) { x += 2.0; }
* }
* const CopyTest z = CopyTest(5.3); // ok
* const CopyTest w = z; // not ok, postblit not run
* static assert(w.x == 55.0);
* because the postblit doesn't get run on the initialization of w.
*/
if (ti.ty == Tstruct)
{
StructDeclaration sd = (cast(TypeStruct)ti).sym;
/* Look to see if initializer involves a copy constructor
* (which implies a postblit)
*/
// there is a copy constructor
// and exp is the same struct
if (sd.postblit && tb2.toDsymbol(null) == sd)