-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
declaration.d
2165 lines (1916 loc) · 66.2 KB
/
declaration.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
/**
* Miscellaneous declarations, including typedef, alias, variable declarations including the
* implicit this declaration, type tuples, ClassInfo, ModuleInfo and various TypeInfos.
*
* Copyright: Copyright (C) 1999-2021 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/declaration.d, _declaration.d)
* Documentation: https://dlang.org/phobos/dmd_declaration.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/declaration.d
*/
module dmd.declaration;
import core.stdc.stdio;
import dmd.aggregate;
import dmd.arraytypes;
import dmd.ctorflow;
import dmd.dclass;
import dmd.delegatize;
import dmd.dscope;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dsymbolsem;
import dmd.dtemplate;
import dmd.errors;
import dmd.expression;
import dmd.func;
import dmd.globals;
import dmd.id;
import dmd.identifier;
import dmd.init;
import dmd.initsem;
import dmd.intrange;
import dmd.mtype;
import dmd.root.outbuffer;
import dmd.root.rootobject;
import dmd.target;
import dmd.tokens;
import dmd.typesem;
import dmd.visitor;
/************************************
* Check to see the aggregate type is nested and its context pointer is
* accessible from the current scope.
* Returns true if error occurs.
*/
bool checkFrameAccess(Loc loc, Scope* sc, AggregateDeclaration ad, size_t iStart = 0)
{
Dsymbol sparent = ad.toParentLocal();
Dsymbol sparent2 = ad.toParent2();
Dsymbol s = sc.func;
if (ad.isNested() && s)
{
//printf("ad = %p %s [%s], parent:%p\n", ad, ad.toChars(), ad.loc.toChars(), ad.parent);
//printf("sparent = %p %s [%s], parent: %s\n", sparent, sparent.toChars(), sparent.loc.toChars(), sparent.parent,toChars());
//printf("sparent2 = %p %s [%s], parent: %s\n", sparent2, sparent2.toChars(), sparent2.loc.toChars(), sparent2.parent,toChars());
if (!ensureStaticLinkTo(s, sparent) || sparent != sparent2 && !ensureStaticLinkTo(s, sparent2))
{
error(loc, "cannot access frame pointer of `%s`", ad.toPrettyChars());
return true;
}
}
bool result = false;
for (size_t i = iStart; i < ad.fields.dim; i++)
{
VarDeclaration vd = ad.fields[i];
Type tb = vd.type.baseElemOf();
if (tb.ty == Tstruct)
{
result |= checkFrameAccess(loc, sc, (cast(TypeStruct)tb).sym);
}
}
return result;
}
/***********************************************
* Mark variable v as modified if it is inside a constructor that var
* is a field in.
*/
bool modifyFieldVar(Loc loc, Scope* sc, VarDeclaration var, Expression e1)
{
//printf("modifyFieldVar(var = %s)\n", var.toChars());
Dsymbol s = sc.func;
while (1)
{
FuncDeclaration fd = null;
if (s)
fd = s.isFuncDeclaration();
if (fd &&
((fd.isCtorDeclaration() && var.isField()) ||
(fd.isStaticCtorDeclaration() && !var.isField())) &&
fd.toParentDecl() == var.toParent2() &&
(!e1 || e1.op == TOK.this_))
{
bool result = true;
var.ctorinit = true;
//printf("setting ctorinit\n");
if (var.isField() && sc.ctorflow.fieldinit.length && !sc.intypeof)
{
assert(e1);
auto mustInit = ((var.storage_class & STC.nodefaultctor) != 0 ||
var.type.needsNested());
const dim = sc.ctorflow.fieldinit.length;
auto ad = fd.isMemberDecl();
assert(ad);
size_t i;
for (i = 0; i < dim; i++) // same as findFieldIndexByName in ctfeexp.c ?
{
if (ad.fields[i] == var)
break;
}
assert(i < dim);
auto fieldInit = &sc.ctorflow.fieldinit[i];
const fi = fieldInit.csx;
if (fi & CSX.this_ctor)
{
if (var.type.isMutable() && e1.type.isMutable())
result = false;
else
{
const(char)* modStr = !var.type.isMutable() ? MODtoChars(var.type.mod) : MODtoChars(e1.type.mod);
.error(loc, "%s field `%s` initialized multiple times", modStr, var.toChars());
.errorSupplemental(fieldInit.loc, "Previous initialization is here.");
}
}
else if (sc.inLoop || (fi & CSX.label))
{
if (!mustInit && var.type.isMutable() && e1.type.isMutable())
result = false;
else
{
const(char)* modStr = !var.type.isMutable() ? MODtoChars(var.type.mod) : MODtoChars(e1.type.mod);
.error(loc, "%s field `%s` initialization is not allowed in loops or after labels", modStr, var.toChars());
}
}
fieldInit.csx |= CSX.this_ctor;
fieldInit.loc = e1.loc;
if (var.overlapped) // https://issues.dlang.org/show_bug.cgi?id=15258
{
foreach (j, v; ad.fields)
{
if (v is var || !var.isOverlappedWith(v))
continue;
v.ctorinit = true;
sc.ctorflow.fieldinit[j].csx = CSX.this_ctor;
}
}
}
else if (fd != sc.func)
{
if (var.type.isMutable())
result = false;
else if (sc.func.fes)
{
const(char)* p = var.isField() ? "field" : var.kind();
.error(loc, "%s %s `%s` initialization is not allowed in foreach loop",
MODtoChars(var.type.mod), p, var.toChars());
}
else
{
const(char)* p = var.isField() ? "field" : var.kind();
.error(loc, "%s %s `%s` initialization is not allowed in nested function `%s`",
MODtoChars(var.type.mod), p, var.toChars(), sc.func.toChars());
}
}
return result;
}
else
{
if (s)
{
s = s.toParentP(var.toParent2());
continue;
}
}
break;
}
return false;
}
/******************************************
*/
extern (C++) void ObjectNotFound(Identifier id)
{
error(Loc.initial, "`%s` not found. object.d may be incorrectly installed or corrupt.", id.toChars());
fatal();
}
enum STC : ulong
{
undefined_ = 0L,
static_ = (1L << 0),
extern_ = (1L << 1),
const_ = (1L << 2),
final_ = (1L << 3),
abstract_ = (1L << 4),
parameter = (1L << 5),
field = (1L << 6),
override_ = (1L << 7),
auto_ = (1L << 8),
synchronized_ = (1L << 9),
deprecated_ = (1L << 10),
in_ = (1L << 11), // in parameter
out_ = (1L << 12), // out parameter
lazy_ = (1L << 13), // lazy parameter
foreach_ = (1L << 14), // variable for foreach loop
//(1L << 15)
variadic = (1L << 16), // the 'variadic' parameter in: T foo(T a, U b, V variadic...)
ctorinit = (1L << 17), // can only be set inside constructor
templateparameter = (1L << 18), // template parameter
scope_ = (1L << 19),
immutable_ = (1L << 20),
ref_ = (1L << 21),
init = (1L << 22), // has explicit initializer
manifest = (1L << 23), // manifest constant
nodtor = (1L << 24), // don't run destructor
nothrow_ = (1L << 25), // never throws exceptions
pure_ = (1L << 26), // pure function
tls = (1L << 27), // thread local
alias_ = (1L << 28), // alias parameter
shared_ = (1L << 29), // accessible from multiple threads
gshared = (1L << 30), // accessible from multiple threads, but not typed as "shared"
wild = (1L << 31), // for "wild" type constructor
property = (1L << 32),
safe = (1L << 33),
trusted = (1L << 34),
system = (1L << 35),
ctfe = (1L << 36), // can be used in CTFE, even if it is static
disable = (1L << 37), // for functions that are not callable
result = (1L << 38), // for result variables passed to out contracts
nodefaultctor = (1L << 39), // must be set inside constructor
temp = (1L << 40), // temporary variable
rvalue = (1L << 41), // force rvalue for variables
nogc = (1L << 42), // @nogc
volatile_ = (1L << 43), // destined for volatile in the back end
return_ = (1L << 44), // 'return ref' or 'return scope' for function parameters
autoref = (1L << 45), // Mark for the already deduced 'auto ref' parameter
inference = (1L << 46), // do attribute inference
exptemp = (1L << 47), // temporary variable that has lifetime restricted to an expression
maybescope = (1L << 48), // parameter might be 'scope'
scopeinferred = (1L << 49), // 'scope' has been inferred and should not be part of mangling
future = (1L << 50), // introducing new base class function
local = (1L << 51), // do not forward (see dmd.dsymbol.ForwardingScopeDsymbol).
returninferred = (1L << 52), // 'return' has been inferred and should not be part of mangling
live = (1L << 53), // function @live attribute
// Group members are mutually exclusive (there can be only one)
safeGroup = STC.safe | STC.trusted | STC.system,
/// Group for `in` / `out` / `ref` storage classes on parameter
IOR = STC.in_ | STC.ref_ | STC.out_,
TYPECTOR = (STC.const_ | STC.immutable_ | STC.shared_ | STC.wild),
FUNCATTR = (STC.ref_ | STC.nothrow_ | STC.nogc | STC.pure_ | STC.property | STC.live |
STC.safeGroup),
}
enum STCStorageClass =
(STC.auto_ | STC.scope_ | STC.static_ | STC.extern_ | STC.const_ | STC.final_ | STC.abstract_ | STC.synchronized_ |
STC.deprecated_ | STC.future | STC.override_ | STC.lazy_ | STC.alias_ | STC.out_ | STC.in_ | STC.manifest |
STC.immutable_ | STC.shared_ | STC.wild | STC.nothrow_ | STC.nogc | STC.pure_ | STC.ref_ | STC.return_ | STC.tls | STC.gshared |
STC.property | STC.safeGroup | STC.disable | STC.local | STC.live);
/* These storage classes "flow through" to the inner scope of a Dsymbol
*/
enum STCFlowThruAggregate = STC.safeGroup; /// for an AggregateDeclaration
enum STCFlowThruFunction = ~(STC.auto_ | STC.scope_ | STC.static_ | STC.extern_ | STC.abstract_ | STC.deprecated_ | STC.override_ |
STC.TYPECTOR | STC.final_ | STC.tls | STC.gshared | STC.ref_ | STC.return_ | STC.property |
STC.nothrow_ | STC.pure_ | STC.safe | STC.trusted | STC.system); /// for a FuncDeclaration
/* Accumulator for successive matches.
*/
struct MatchAccumulator
{
int count; // number of matches found so far
MATCH last = MATCH.nomatch; // match level of lastf
FuncDeclaration lastf; // last matching function we found
FuncDeclaration nextf; // if ambiguous match, this is the "other" function
}
/***********************************************************
*/
extern (C++) abstract class Declaration : Dsymbol
{
Type type;
Type originalType; // before semantic analysis
StorageClass storage_class = STC.undefined_;
Visibility visibility;
LINK linkage = LINK.default_;
short inuse; // used to detect cycles
ubyte adFlags; // control re-assignment of AliasDeclaration (put here for packing reasons)
enum wasRead = 1; // set if AliasDeclaration was read
enum ignoreRead = 2; // ignore any reads of AliasDeclaration
// overridden symbol with pragma(mangle, "...")
const(char)[] mangleOverride;
final extern (D) this(Identifier ident)
{
super(ident);
visibility = Visibility(Visibility.Kind.undefined);
}
final extern (D) this(const ref Loc loc, Identifier ident)
{
super(loc, ident);
visibility = Visibility(Visibility.Kind.undefined);
}
override const(char)* kind() const
{
return "declaration";
}
override final d_uns64 size(const ref Loc loc)
{
assert(type);
return type.size();
}
/**
* Issue an error if an attempt to call a disabled method is made
*
* If the declaration is disabled but inside a disabled function,
* returns `true` but do not issue an error message.
*
* Params:
* loc = Location information of the call
* sc = Scope in which the call occurs
* isAliasedDeclaration = if `true` searches overload set
*
* Returns:
* `true` if this `Declaration` is `@disable`d, `false` otherwise.
*/
extern (D) final bool checkDisabled(Loc loc, Scope* sc, bool isAliasedDeclaration = false)
{
if (!(storage_class & STC.disable))
return false;
if (sc.func && sc.func.storage_class & STC.disable)
return true;
auto p = toParent();
if (p && isPostBlitDeclaration())
{
p.error(loc, "is not copyable because it is annotated with `@disable`");
return true;
}
// if the function is @disabled, maybe there
// is an overload in the overload set that isn't
if (isAliasedDeclaration)
{
FuncDeclaration fd = isFuncDeclaration();
if (fd)
{
for (FuncDeclaration ovl = fd; ovl; ovl = cast(FuncDeclaration)ovl.overnext)
if (!(ovl.storage_class & STC.disable))
return false;
}
}
if (auto ctor = isCtorDeclaration())
{
if (ctor.isCpCtor && ctor.generated)
{
.error(loc, "Generating an `inout` copy constructor for `struct %s` failed, therefore instances of it are uncopyable", parent.toPrettyChars());
return true;
}
}
error(loc, "cannot be used because it is annotated with `@disable`");
return true;
}
/*************************************
* Check to see if declaration can be modified in this context (sc).
* Issue error if not.
* Params:
* loc = location for error messages
* e1 = `null` or `this` expression when this declaration is a field
* sc = context
* flag = !=0 means do not issue error message for invalid modification
* Returns:
* Modifiable.yes or Modifiable.initialization
*/
extern (D) final Modifiable checkModify(Loc loc, Scope* sc, Expression e1, int flag)
{
VarDeclaration v = isVarDeclaration();
if (v && v.canassign)
return Modifiable.initialization;
if (isParameter() || isResult())
{
for (Scope* scx = sc; scx; scx = scx.enclosing)
{
if (scx.func == parent && (scx.flags & SCOPE.contract))
{
const(char)* s = isParameter() && parent.ident != Id.ensure ? "parameter" : "result";
if (!flag)
error(loc, "cannot modify %s `%s` in contract", s, toChars());
return Modifiable.initialization; // do not report type related errors
}
}
}
if (e1 && e1.op == TOK.this_ && isField())
{
VarDeclaration vthis = (cast(ThisExp)e1).var;
for (Scope* scx = sc; scx; scx = scx.enclosing)
{
if (scx.func == vthis.parent && (scx.flags & SCOPE.contract))
{
if (!flag)
error(loc, "cannot modify parameter `this` in contract");
return Modifiable.initialization; // do not report type related errors
}
}
}
if (v && (isCtorinit() || isField()))
{
// It's only modifiable if inside the right constructor
if ((storage_class & (STC.foreach_ | STC.ref_)) == (STC.foreach_ | STC.ref_))
return Modifiable.initialization;
return modifyFieldVar(loc, sc, v, e1)
? Modifiable.initialization : Modifiable.yes;
}
return Modifiable.yes;
}
override final Dsymbol search(const ref Loc loc, Identifier ident, int flags = SearchLocalsOnly)
{
Dsymbol s = Dsymbol.search(loc, ident, flags);
if (!s && type)
{
s = type.toDsymbol(_scope);
if (s)
s = s.search(loc, ident, flags);
}
return s;
}
final bool isStatic() const pure nothrow @nogc @safe
{
return (storage_class & STC.static_) != 0;
}
bool isDelete()
{
return false;
}
bool isDataseg()
{
return false;
}
bool isThreadlocal()
{
return false;
}
bool isCodeseg() const pure nothrow @nogc @safe
{
return false;
}
final bool isCtorinit() const pure nothrow @nogc @safe
{
return (storage_class & STC.ctorinit) != 0;
}
final bool isFinal() const pure nothrow @nogc @safe
{
return (storage_class & STC.final_) != 0;
}
bool isAbstract()
{
return (storage_class & STC.abstract_) != 0;
}
final bool isConst() const pure nothrow @nogc @safe
{
return (storage_class & STC.const_) != 0;
}
final bool isImmutable() const pure nothrow @nogc @safe
{
return (storage_class & STC.immutable_) != 0;
}
final bool isWild() const pure nothrow @nogc @safe
{
return (storage_class & STC.wild) != 0;
}
final bool isAuto() const pure nothrow @nogc @safe
{
return (storage_class & STC.auto_) != 0;
}
final bool isScope() const pure nothrow @nogc @safe
{
return (storage_class & STC.scope_) != 0;
}
final bool isSynchronized() const pure nothrow @nogc @safe
{
return (storage_class & STC.synchronized_) != 0;
}
final bool isParameter() const pure nothrow @nogc @safe
{
return (storage_class & STC.parameter) != 0;
}
override final bool isDeprecated() const pure nothrow @nogc @safe
{
return (storage_class & STC.deprecated_) != 0;
}
final bool isDisabled() const pure nothrow @nogc @safe
{
return (storage_class & STC.disable) != 0;
}
final bool isOverride() const pure nothrow @nogc @safe
{
return (storage_class & STC.override_) != 0;
}
final bool isResult() const pure nothrow @nogc @safe
{
return (storage_class & STC.result) != 0;
}
final bool isField() const pure nothrow @nogc @safe
{
return (storage_class & STC.field) != 0;
}
final bool isIn() const pure nothrow @nogc @safe
{
return (storage_class & STC.in_) != 0;
}
final bool isOut() const pure nothrow @nogc @safe
{
return (storage_class & STC.out_) != 0;
}
final bool isRef() const pure nothrow @nogc @safe
{
return (storage_class & STC.ref_) != 0;
}
final bool isFuture() const pure nothrow @nogc @safe
{
return (storage_class & STC.future) != 0;
}
override final Visibility visible() pure nothrow @nogc @safe
{
return visibility;
}
override final inout(Declaration) isDeclaration() inout
{
return this;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class TupleDeclaration : Declaration
{
Objects* objects;
bool isexp; // true: expression tuple
TypeTuple tupletype; // !=null if this is a type tuple
extern (D) this(const ref Loc loc, Identifier ident, Objects* objects)
{
super(loc, ident);
this.objects = objects;
}
override TupleDeclaration syntaxCopy(Dsymbol s)
{
assert(0);
}
override const(char)* kind() const
{
return "tuple";
}
override Type getType()
{
/* If this tuple represents a type, return that type
*/
//printf("TupleDeclaration::getType() %s\n", toChars());
if (isexp)
return null;
if (!tupletype)
{
/* It's only a type tuple if all the Object's are types
*/
for (size_t i = 0; i < objects.dim; i++)
{
RootObject o = (*objects)[i];
if (o.dyncast() != DYNCAST.type)
{
//printf("\tnot[%d], %p, %d\n", i, o, o.dyncast());
return null;
}
}
/* We know it's a type tuple, so build the TypeTuple
*/
Types* types = cast(Types*)objects;
auto args = new Parameters(objects.dim);
OutBuffer buf;
int hasdeco = 1;
for (size_t i = 0; i < types.dim; i++)
{
Type t = (*types)[i];
//printf("type = %s\n", t.toChars());
version (none)
{
buf.printf("_%s_%d", ident.toChars(), i);
const len = buf.offset;
const name = buf.extractSlice().ptr;
auto id = Identifier.idPool(name, len);
auto arg = new Parameter(STC.in_, t, id, null);
}
else
{
auto arg = new Parameter(0, t, null, null, null);
}
(*args)[i] = arg;
if (!t.deco)
hasdeco = 0;
}
tupletype = new TypeTuple(args);
if (hasdeco)
return tupletype.typeSemantic(Loc.initial, null);
}
return tupletype;
}
override Dsymbol toAlias2()
{
//printf("TupleDeclaration::toAlias2() '%s' objects = %s\n", toChars(), objects.toChars());
for (size_t i = 0; i < objects.dim; i++)
{
RootObject o = (*objects)[i];
if (Dsymbol s = isDsymbol(o))
{
s = s.toAlias2();
(*objects)[i] = s;
}
}
return this;
}
override bool needThis()
{
//printf("TupleDeclaration::needThis(%s)\n", toChars());
for (size_t i = 0; i < objects.dim; i++)
{
RootObject o = (*objects)[i];
if (o.dyncast() == DYNCAST.expression)
{
Expression e = cast(Expression)o;
if (e.op == TOK.dSymbol)
{
DsymbolExp ve = cast(DsymbolExp)e;
Declaration d = ve.s.isDeclaration();
if (d && d.needThis())
{
return true;
}
}
}
}
return false;
}
override inout(TupleDeclaration) isTupleDeclaration() inout
{
return this;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class AliasDeclaration : Declaration
{
Dsymbol aliassym;
Dsymbol overnext; // next in overload list
Dsymbol _import; // !=null if unresolved internal alias for selective import
extern (D) this(const ref Loc loc, Identifier ident, Type type)
{
super(loc, ident);
//printf("AliasDeclaration(id = '%s', type = %p)\n", id.toChars(), type);
//printf("type = '%s'\n", type.toChars());
this.type = type;
assert(type);
}
extern (D) this(const ref Loc loc, Identifier ident, Dsymbol s)
{
super(loc, ident);
//printf("AliasDeclaration(id = '%s', s = %p)\n", id.toChars(), s);
assert(s != this);
this.aliassym = s;
assert(s);
}
static AliasDeclaration create(Loc loc, Identifier id, Type type)
{
return new AliasDeclaration(loc, id, type);
}
override AliasDeclaration syntaxCopy(Dsymbol s)
{
//printf("AliasDeclaration::syntaxCopy()\n");
assert(!s);
AliasDeclaration sa = type ? new AliasDeclaration(loc, ident, type.syntaxCopy()) : new AliasDeclaration(loc, ident, aliassym.syntaxCopy(null));
sa.comment = comment;
sa.storage_class = storage_class;
return sa;
}
override bool overloadInsert(Dsymbol s)
{
//printf("[%s] AliasDeclaration::overloadInsert('%s') s = %s %s @ [%s]\n",
// loc.toChars(), toChars(), s.kind(), s.toChars(), s.loc.toChars());
/** Aliases aren't overloadable themselves, but if their Aliasee is
* overloadable they are converted to an overloadable Alias (either
* FuncAliasDeclaration or OverDeclaration).
*
* This is done by moving the Aliasee into such an overloadable alias
* which is then used to replace the existing Aliasee. The original
* Alias (_this_) remains a useless shell.
*
* This is a horrible mess. It was probably done to avoid replacing
* existing AST nodes and references, but it needs a major
* simplification b/c it's too complex to maintain.
*
* A simpler approach might be to merge any colliding symbols into a
* simple Overload class (an array) and then later have that resolve
* all collisions.
*/
if (semanticRun >= PASS.semanticdone)
{
/* Semantic analysis is already finished, and the aliased entity
* is not overloadable.
*/
if (type)
return false;
/* When s is added in member scope by static if, mixin("code") or others,
* aliassym is determined already. See the case in: test/compilable/test61.d
*/
auto sa = aliassym.toAlias();
if (auto td = s.toAlias().isTemplateDeclaration())
s = td.funcroot ? td.funcroot : td;
if (auto fd = sa.isFuncDeclaration())
{
auto fa = new FuncAliasDeclaration(ident, fd);
fa.visibility = visibility;
fa.parent = parent;
aliassym = fa;
return aliassym.overloadInsert(s);
}
if (auto td = sa.isTemplateDeclaration())
{
auto od = new OverDeclaration(ident, td.funcroot ? td.funcroot : td);
od.visibility = visibility;
od.parent = parent;
aliassym = od;
return aliassym.overloadInsert(s);
}
if (auto od = sa.isOverDeclaration())
{
if (sa.ident != ident || sa.parent != parent)
{
od = new OverDeclaration(ident, od);
od.visibility = visibility;
od.parent = parent;
aliassym = od;
}
return od.overloadInsert(s);
}
if (auto os = sa.isOverloadSet())
{
if (sa.ident != ident || sa.parent != parent)
{
os = new OverloadSet(ident, os);
// TODO: visibility is lost here b/c OverloadSets have no visibility attribute
// Might no be a practical issue, b/c the code below fails to resolve the overload anyhow.
// ----
// module os1;
// import a, b;
// private alias merged = foo; // private alias to overload set of a.foo and b.foo
// ----
// module os2;
// import a, b;
// public alias merged = bar; // public alias to overload set of a.bar and b.bar
// ----
// module bug;
// import os1, os2;
// void test() { merged(123); } // should only look at os2.merged
//
// os.visibility = visibility;
os.parent = parent;
aliassym = os;
}
os.push(s);
return true;
}
return false;
}
/* Don't know yet what the aliased symbol is, so assume it can
* be overloaded and check later for correctness.
*/
if (overnext)
return overnext.overloadInsert(s);
if (s is this)
return true;
overnext = s;
return true;
}
override const(char)* kind() const
{
return "alias";
}
override Type getType()
{
if (type)
return type;
return toAlias().getType();
}
override Dsymbol toAlias()
{
//printf("[%s] AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s', inuse = %d)\n",
// loc.toChars(), toChars(), this, aliassym, aliassym ? aliassym.kind() : "", inuse);
assert(this != aliassym);
//static int count; if (++count == 10) *(char*)0=0;
// Reading the AliasDeclaration
if (!(adFlags & ignoreRead))
adFlags |= wasRead; // can never assign to this AliasDeclaration again
if (inuse == 1 && type && _scope)
{
inuse = 2;
uint olderrors = global.errors;
Dsymbol s = type.toDsymbol(_scope);
//printf("[%s] type = %s, s = %p, this = %p\n", loc.toChars(), type.toChars(), s, this);
if (global.errors != olderrors)
goto Lerr;
if (s)
{
s = s.toAlias();
if (global.errors != olderrors)
goto Lerr;
aliassym = s;
inuse = 0;
}
else
{
Type t = type.typeSemantic(loc, _scope);
if (t.ty == Terror)
goto Lerr;
if (global.errors != olderrors)
goto Lerr;
//printf("t = %s\n", t.toChars());
inuse = 0;
}
}
if (inuse)
{
error("recursive alias declaration");
Lerr:
// Avoid breaking "recursive alias" state during errors gagged
if (global.gag)
return this;
aliassym = new AliasDeclaration(loc, ident, Type.terror);
type = Type.terror;
return aliassym;
}
if (semanticRun >= PASS.semanticdone)
{
// semantic is already done.
// Do not see aliassym !is null, because of lambda aliases.
// Do not see type.deco !is null, even so "alias T = const int;` needs
// semantic analysis to take the storage class `const` as type qualifier.
}
else
{
if (_import && _import._scope)
{
/* If this is an internal alias for selective/renamed import,
* load the module first.
*/
_import.dsymbolSemantic(null);
}
if (_scope)
{
aliasSemantic(this, _scope);
}
}
inuse = 1;
Dsymbol s = aliassym ? aliassym.toAlias() : this;
inuse = 0;
return s;
}
override Dsymbol toAlias2()
{
if (inuse)
{
error("recursive alias declaration");
return this;
}
inuse = 1;
Dsymbol s = aliassym ? aliassym.toAlias2() : this;
inuse = 0;
return s;
}
override bool isOverloadable() const
{
// assume overloadable until alias is resolved
return semanticRun < PASS.semanticdone ||
aliassym && aliassym.isOverloadable();
}
override inout(AliasDeclaration) isAliasDeclaration() inout
{
return this;
}
/** Returns: `true` if this instance was created to make a template parameter
visible in the scope of a template body, `false` otherwise */
extern (D) bool isAliasedTemplateParameter() const
{
return !!(storage_class & STC.templateparameter);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class OverDeclaration : Declaration
{
Dsymbol overnext; // next in overload list
Dsymbol aliassym;
extern (D) this(Identifier ident, Dsymbol s)