Debian Codegraph @

gcc/d/dmd/dsymbol.d

raw
1 /**
2 * The base class for a D symbol, which can be a module, variable, function, enum, etc.
3 *
4 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
5 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
6 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/dsymbol.d, _dsymbol.d)
8 * Documentation: https://dlang.org/phobos/dmd_dsymbol.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/dsymbol.d
10 */
11
12 module dmd.dsymbol;
13
14 import core.stdc.stdarg;
15 import core.stdc.stdio;
16 import core.stdc.string;
17 import core.stdc.stdlib;
18
19 import dmd.aggregate;
20 import dmd.aliasthis;
21 import dmd.arraytypes;
22 import dmd.attrib;
23 import dmd.astenums;
24 import dmd.ast_node;
25 import dmd.gluelayer;
26 import dmd.dclass;
27 import dmd.declaration;
28 import dmd.denum;
29 import dmd.dimport;
30 import dmd.dmodule;
31 import dmd.dversion;
32 import dmd.dscope;
33 import dmd.dstruct;
34 import dmd.dtemplate;
35 import dmd.errors;
36 import dmd.expression;
37 import dmd.func;
38 import dmd.globals;
39 import dmd.id;
40 import dmd.identifier;
41 import dmd.init;
42 import dmd.lexer;
43 import dmd.location;
44 import dmd.mtype;
45 import dmd.nspace;
46 import dmd.root.aav;
47 import dmd.root.rmem;
48 import dmd.rootobject;
49 import dmd.root.string;
50 import dmd.statement;
51 import dmd.staticassert;
52 import dmd.tokens;
53 import dmd.visitor;
54 import dmd.dsymbolsem;
55
56 import dmd.common.outbuffer;
57
58 /***************************************
59 * Calls dg(Dsymbol* sym) for each Dsymbol.
60 * If dg returns !=0, stops and returns that value else returns 0.
61 * Params:
62 * symbols = Dsymbols
63 * dg = delegate to call for each Dsymbol
64 * Returns:
65 * last value returned by dg()
66 *
67 * See_Also: $(REF each, dmd, root, array)
68 */
69 int foreachDsymbol(Dsymbols* symbols, scope int delegate(Dsymbol) dg)
70 {
71 assert(dg);
72 if (symbols)
73 {
74 /* Do not use foreach, as the size of the array may expand during iteration
75 */
76 for (size_t i = 0; i < symbols.length; ++i)
77 {
78 Dsymbol s = (*symbols)[i];
79 const result = dg(s);
80 if (result)
81 return result;
82 }
83 }
84 return 0;
85 }
86
87 /***************************************
88 * Calls dg(Dsymbol* sym) for each Dsymbol.
89 * Params:
90 * symbols = Dsymbols
91 * dg = delegate to call for each Dsymbol
92 *
93 * See_Also: $(REF each, dmd, root, array)
94 */
95 void foreachDsymbol(Dsymbols* symbols, scope void delegate(Dsymbol) dg)
96 {
97 assert(dg);
98 if (symbols)
99 {
100 /* Do not use foreach, as the size of the array may expand during iteration
101 */
102 for (size_t i = 0; i < symbols.length; ++i)
103 {
104 Dsymbol s = (*symbols)[i];
105 dg(s);
106 }
107 }
108 }
109
110
111 struct Ungag
112 {
113 uint oldgag;
114
115 extern (D) this(uint old) nothrow @safe
116 {
117 this.oldgag = old;
118 }
119
120 extern (C++) ~this() nothrow
121 {
122 global.gag = oldgag;
123 }
124 }
125
126 struct Visibility
127 {
128 ///
129 enum Kind : ubyte
130 {
131 undefined,
132 none, // no access
133 private_,
134 package_,
135 protected_,
136 public_,
137 export_,
138 }
139
140 Kind kind;
141 Package pkg;
142
143 extern(C++) this(Visibility.Kind kind, Package pkg = null) pure nothrow @nogc @safe
144 {
145 this.kind = kind;
146 this.pkg = pkg;
147 }
148
149 extern (D):
150
151 /**
152 * Checks if `this` is less or more visible than `other`
153 *
154 * Params:
155 * other = Visibility to compare `this` to.
156 *
157 * Returns:
158 * A value `< 0` if `this` is less visible than `other`,
159 * a value `> 0` if `this` is more visible than `other`,
160 * and `0` if they are at the same level.
161 * Note that `package` visibility with different packages
162 * will also return `0`.
163 */
164 int opCmp(const Visibility other) const pure nothrow @nogc @safe
165 {
166 return this.kind - other.kind;
167 }
168
169 ///
170 unittest
171 {
172 assert(Visibility(Visibility.Kind.public_) > Visibility(Visibility.Kind.private_));
173 assert(Visibility(Visibility.Kind.private_) < Visibility(Visibility.Kind.protected_));
174 assert(Visibility(Visibility.Kind.package_) >= Visibility(Visibility.Kind.package_));
175 }
176
177 /**
178 * Checks if `this` is absolutely identical visibility attribute to `other`
179 */
180 bool opEquals(ref const Visibility other) const @safe
181 {
182 if (this.kind == other.kind)
183 {
184 if (this.kind == Visibility.Kind.package_)
185 return this.pkg == other.pkg;
186 return true;
187 }
188 return false;
189 }
190 }
191
192 enum PASS : ubyte
193 {
194 initial, // initial state
195 semantic, // semantic() started
196 semanticdone, // semantic() done
197 semantic2, // semantic2() started
198 semantic2done, // semantic2() done
199 semantic3, // semantic3() started
200 semantic3done, // semantic3() done
201 inline, // inline started
202 inlinedone, // inline done
203 obj, // toObjFile() run
204 }
205
206 // Search options
207 alias SearchOptFlags = uint;
208 enum SearchOpt : SearchOptFlags
209 {
210 all = 0x00, // search for all symbols
211 ignorePrivateImports = 0x01, // don't search private imports
212 ignoreErrors = 0x02, // don't give error messages
213 ignoreAmbiguous = 0x04, // return NULL if ambiguous
214 localsOnly = 0x08, // only look at locals (don't search imports)
215 importsOnly = 0x10, // only look in imports
216 unqualifiedModule = 0x20, // the module scope search is unqualified,
217 // meaning don't search imports in that scope,
218 // because qualified module searches search
219 // their imports
220 tagNameSpace = 0x40, // search ImportC tag symbol table
221 ignoreVisibility = 0x80, // also find private and package protected symbols
222 }
223
224 /***********************************************************
225 * Struct/Class/Union field state.
226 * Used for transitory information when setting field offsets, such
227 * as bit fields.
228 */
229 struct FieldState
230 {
231 uint offset; /// byte offset for next field
232
233 uint fieldOffset; /// byte offset for the start of the bit field
234 uint fieldSize; /// byte size of field
235 uint fieldAlign; /// byte alignment of field
236 uint bitOffset; /// bit offset for field
237
238 bool inFlight; /// bit field is in flight
239
240 void print() const
241 {
242 printf("FieldState.offset = %d bytes\n", offset);
243 printf(" .fieldOffset = %d bytes\n", fieldOffset);
244 printf(" .bitOffset = %d bits\n", bitOffset);
245 printf(" .fieldSize = %d bytes\n", fieldSize);
246 printf(" .inFlight = %d\n", inFlight);
247 }
248 }
249
250 // 99.9% of Dsymbols don't have attributes (at least in druntime and Phobos),
251 // so save memory by grouping them into a separate struct
252 private struct DsymbolAttributes
253 {
254 /// C++ namespace this symbol belongs to
255 CPPNamespaceDeclaration cppnamespace;
256 /// customized deprecation message
257 DeprecatedDeclaration depdecl_;
258 /// user defined attributes
259 UserAttributeDeclaration userAttribDecl;
260 }
261
262 enum DSYM : ubyte
263 {
264 none,
265 dsymbol,
266 linkDeclaration,
267 cppMangleDeclaration,
268 alignDeclaration,
269 pragmaDeclaration,
270 conditionalDeclaration,
271 staticForeachDeclaration,
272 userAttributeDeclaration,
273 labelDsymbol,
274 aliasThis,
275 package_,
276 module_,
277 enumMember,
278 templateDeclaration,
279 templateInstance,
280 templateMixin,
281 forwardingAttribDeclaration,
282 nspace,
283 declaration,
284 storageClassDeclaration,
285 expressionDsymbol,
286 aliasAssign,
287 thisDeclaration,
288 bitFieldDeclaration,
289 typeInfoDeclaration,
290 tupleDeclaration,
291 aliasDeclaration,
292 aggregateDeclaration,
293 funcDeclaration,
294 funcAliasDeclaration,
295 overDeclaration,
296 funcLiteralDeclaration,
297 ctorDeclaration,
298 postBlitDeclaration,
299 dtorDeclaration,
300 staticCtorDeclaration,
301 staticDtorDeclaration,
302 sharedStaticCtorDeclaration,
303 sharedStaticDtorDeclaration,
304 invariantDeclaration,
305 unitTestDeclaration,
306 newDeclaration,
307 varDeclaration,
308 versionSymbol,
309 debugSymbol,
310 classDeclaration,
311 structDeclaration,
312 unionDeclaration,
313 interfaceDeclaration,
314 scopeDsymbol,
315 forwardingScopeDsymbol,
316 withScopeSymbol,
317 arrayScopeSymbol,
318 import_,
319 enumDeclaration,
320 symbolDeclaration,
321 attribDeclaration,
322 anonDeclaration,
323 cppNamespaceDeclaration,
324 visibilityDeclaration,
325 overloadSet,
326 mixinDeclaration,
327 staticAssert,
328 staticIfDeclaration,
329 cAsmDeclaration
330 }
331
332 /***********************************************************
333 */
334 extern (C++) class Dsymbol : ASTNode
335 {
336 Identifier ident;
337 Dsymbol parent;
338 Symbol* csym; // symbol for code generator
339 Scope* _scope; // !=null means context to use for semantic()
340 private DsymbolAttributes* atts; /// attached attribute declarations
341 const Loc loc; // where defined
342 ushort localNum; /// perturb mangled name to avoid collisions with those in FuncDeclaration.localsymtab
343 static struct BitFields
344 {
345 bool errors; // this symbol failed to pass semantic()
346 PASS semanticRun = PASS.initial;
347 }
348 import dmd.common.bitfields;
349 mixin(generateBitFields!(BitFields, ubyte));
350 DSYM dsym;
351
352 final extern (D) this(DSYM tag) nothrow @safe
353 {
354 //printf("Dsymbol::Dsymbol(%p)\n", this);
355 this.dsym = tag;
356 loc = Loc.initial;
357 }
358
359 final extern (D) this(DSYM tag, Identifier ident) nothrow @safe
360 {
361 //printf("Dsymbol::Dsymbol(%p, ident)\n", this);
362 this.dsym = tag;
363 this.loc = Loc.initial;
364 this.ident = ident;
365 }
366
367 final extern (D) this(DSYM tag, Loc loc, Identifier ident) nothrow @safe
368 {
369 //printf("Dsymbol::Dsymbol(%p, ident)\n", this);
370 this.dsym = tag;
371 this.loc = loc;
372 this.ident = ident;
373 }
374
375 static Dsymbol create(Identifier ident) nothrow @safe
376 {
377 return new Dsymbol(DSYM.dsymbol, ident);
378 }
379
380 final override const(char)* toChars() const
381 {
382 import dmd.hdrgen : toChars;
383 return toChars(this);
384 }
385
386 // Getters / setters for fields stored in `DsymbolAttributes`
387 final nothrow pure @safe
388 {
389 private ref DsymbolAttributes getAtts()
390 {
391 if (!atts)
392 atts = new DsymbolAttributes();
393 return *atts;
394 }
395
396 inout(DeprecatedDeclaration) depdecl() inout { return atts ? atts.depdecl_ : null; }
397 inout(CPPNamespaceDeclaration) cppnamespace() inout { return atts ? atts.cppnamespace : null; }
398 inout(UserAttributeDeclaration) userAttribDecl() inout { return atts ? atts.userAttribDecl : null; }
399
400 DeprecatedDeclaration depdecl(DeprecatedDeclaration dd)
401 {
402 if (!dd && !atts)
403 return null;
404 return getAtts().depdecl_ = dd;
405 }
406
407 CPPNamespaceDeclaration cppnamespace(CPPNamespaceDeclaration ns)
408 {
409 if (!ns && !atts)
410 return null;
411 return getAtts().cppnamespace = ns;
412 }
413
414 UserAttributeDeclaration userAttribDecl(UserAttributeDeclaration uad)
415 {
416 if (!uad && !atts)
417 return null;
418 return getAtts().userAttribDecl = uad;
419 }
420 }
421
422 // helper to print fully qualified (template) arguments
423 const(char)* toPrettyCharsHelper()
424 {
425 return toChars();
426 }
427
428 override bool equals(const RootObject o) const
429 {
430 if (this == o)
431 return true;
432 const s = o.isDsymbol();
433 if (!s)
434 return false;
435 // Overload sets don't have an ident
436 // Function-local declarations may have identical names
437 // if they are declared in different scopes
438 if (s && ident && s.ident && ident.equals(s.ident) && localNum == s.localNum)
439 return true;
440 return false;
441 }
442
443 final bool isAnonymous() const
444 {
445 return ident is null || ident.isAnonymous;
446 }
447
448 extern(D) private const(char)[] prettyFormatHelper()
449 {
450 const cstr = toPrettyChars();
451 return '`' ~ cstr.toDString() ~ "`\0";
452 }
453
454 /**********************************
455 * Determine which Module a Dsymbol is in.
456 */
457 final Module getModule()
458 {
459 //printf("Dsymbol::getModule()\n");
460 if (TemplateInstance ti = isInstantiated())
461 return ti.tempdecl.getModule();
462 Dsymbol s = this;
463 while (s)
464 {
465 //printf("\ts = %s '%s'\n", s.kind(), s.toPrettyChars());
466 if (Module m = s.isModule())
467 return m;
468 s = s.parent;
469 }
470 return null;
471 }
472
473 /**************************************
474 * Does this Dsymbol come from a C file?
475 * Returns:
476 * true if it does
477 */
478 final bool isCsymbol()
479 {
480 if (Module m = getModule())
481 return m.filetype == FileType.c;
482 return false;
483 }
484
485 /**********************************
486 * Determine which Module a Dsymbol is in, as far as access rights go.
487 */
488 final Module getAccessModule()
489 {
490 //printf("Dsymbol::getAccessModule()\n");
491 if (TemplateInstance ti = isInstantiated())
492 return ti.tempdecl.getAccessModule();
493 Dsymbol s = this;
494 while (s)
495 {
496 //printf("\ts = %s '%s'\n", s.kind(), s.toPrettyChars());
497 if (Module m = s.isModule())
498 return m;
499 TemplateInstance ti = s.isTemplateInstance();
500 if (ti && ti.enclosing)
501 {
502 /* Because of local template instantiation, the parent isn't where the access
503 * rights come from - it's the template declaration
504 */
505 s = ti.tempdecl;
506 }
507 else
508 s = s.parent;
509 }
510 return null;
511 }
512
513 /**
514 * `pastMixin` returns the enclosing symbol if this is a template mixin.
515 *
516 * `pastMixinAndNspace` does likewise, additionally skipping over Nspaces that
517 * are mangleOnly.
518 *
519 * See also `parent`, `toParent` and `toParent2`.
520 */
521 final inout(Dsymbol) pastMixin() inout @safe
522 {
523 //printf("Dsymbol::pastMixin() %s\n", toChars());
524 if (!isTemplateMixin() && !isForwardingAttribDeclaration() && !isForwardingScopeDsymbol())
525 return this;
526 if (!parent)
527 return null;
528 return parent.pastMixin();
529 }
530
531 /**********************************
532 * `parent` field returns a lexically enclosing scope symbol this is a member of.
533 *
534 * `toParent()` returns a logically enclosing scope symbol this is a member of.
535 * It skips over TemplateMixin's.
536 *
537 * `toParent2()` returns an enclosing scope symbol this is living at runtime.
538 * It skips over both TemplateInstance's and TemplateMixin's.
539 * It's used when looking for the 'this' pointer of the enclosing function/class.
540 *
541 * `toParentDecl()` similar to `toParent2()` but always follows the template declaration scope
542 * instead of the instantiation scope.
543 *
544 * `toParentLocal()` similar to `toParentDecl()` but follows the instantiation scope
545 * if a template declaration is non-local i.e. global or static.
546 *
547 * Examples:
548 * ---
549 * module mod;
550 * template Foo(alias a) { mixin Bar!(); }
551 * mixin template Bar() {
552 * public { // VisibilityDeclaration
553 * void baz() { a = 2; }
554 * }
555 * }
556 * void test() {
557 * int v = 1;
558 * alias foo = Foo!(v);
559 * foo.baz();
560 * assert(v == 2);
561 * }
562 *
563 * // s == FuncDeclaration('mod.test.Foo!().Bar!().baz()')
564 * // s.parent == TemplateMixin('mod.test.Foo!().Bar!()')
565 * // s.toParent() == TemplateInstance('mod.test.Foo!()')
566 * // s.toParent2() == FuncDeclaration('mod.test')
567 * // s.toParentDecl() == Module('mod')
568 * // s.toParentLocal() == FuncDeclaration('mod.test')
569 * ---
570 */
571 final inout(Dsymbol) toParent() inout @safe
572 {
573 return parent ? parent.pastMixin() : null;
574 }
575
576 /// ditto
577 final inout(Dsymbol) toParent2() inout @safe
578 {
579 if (!parent || !parent.isTemplateInstance && !parent.isForwardingAttribDeclaration() && !parent.isForwardingScopeDsymbol())
580 return parent;
581 return parent.toParent2;
582 }
583
584 /// ditto
585 final inout(Dsymbol) toParentDecl() inout
586 {
587 return toParentDeclImpl(false);
588 }
589
590 /// ditto
591 final inout(Dsymbol) toParentLocal() inout
592 {
593 return toParentDeclImpl(true);
594 }
595
596 private inout(Dsymbol) toParentDeclImpl(bool localOnly) inout
597 {
598 auto p = toParent();
599 if (!p || !p.isTemplateInstance())
600 return p;
601 auto ti = p.isTemplateInstance();
602 if (ti.tempdecl && (!localOnly || !(cast(TemplateDeclaration)ti.tempdecl).isstatic))
603 return ti.tempdecl.toParentDeclImpl(localOnly);
604 return parent.toParentDeclImpl(localOnly);
605 }
606
607 /**
608 * Returns the declaration scope scope of `this` unless any of the symbols
609 * `p1` or `p2` resides in its enclosing instantiation scope then the
610 * latter is returned.
611 */
612 final Dsymbol toParentP(Dsymbol p1, Dsymbol p2 = null)
613 {
614 return followInstantiationContext(p1, p2) ? toParent2() : toParentLocal();
615 }
616
617 final inout(TemplateInstance) isInstantiated() inout
618 {
619 if (!parent)
620 return null;
621 auto ti = parent.isTemplateInstance();
622 if (ti && !ti.isTemplateMixin())
623 return ti;
624 return parent.isInstantiated();
625 }
626
627 /***
628 * Returns true if any of the symbols `p1` or `p2` resides in the enclosing
629 * instantiation scope of `this`.
630 */
631 final bool followInstantiationContext(Dsymbol p1, Dsymbol p2 = null)
632 {
633 static bool has2This(Dsymbol s)
634 {
635 if (auto f = s.isFuncDeclaration())
636 return f.hasDualContext();
637 if (auto ad = s.isAggregateDeclaration())
638 return ad.vthis2 !is null;
639 return false;
640 }
641
642 if (has2This(this))
643 {
644 assert(p1);
645 auto outer = toParent();
646 while (outer)
647 {
648 auto ti = outer.isTemplateInstance();
649 if (!ti)
650 break;
651 foreach (oarg; *ti.tiargs)
652 {
653 auto sa = getDsymbol(oarg);
654 if (!sa)
655 continue;
656 sa = sa.toAlias().toParent2();
657 if (!sa)
658 continue;
659 if (sa == p1)
660 return true;
661 if (p2 && sa == p2)
662 return true;
663 }
664 outer = ti.tempdecl.toParent();
665 }
666 return false;
667 }
668 return false;
669 }
670
671 // Check if this function is a member of a template which has only been
672 // instantiated speculatively, eg from inside is(typeof()).
673 // Return the speculative template instance it is part of,
674 // or NULL if not speculative.
675 final inout(TemplateInstance) isSpeculative() inout
676 {
677 if (!parent)
678 return null;
679 auto ti = parent.isTemplateInstance();
680 if (ti && ti.gagged)
681 return ti;
682 if (!parent.toParent())
683 return null;
684 return parent.isSpeculative();
685 }
686
687 final Ungag ungagSpeculative() const
688 {
689 const oldgag = global.gag;
690 if (global.gag && !isSpeculative() && !toParent2().isFuncDeclaration())
691 global.gag = 0;
692 return Ungag(oldgag);
693 }
694
695 // kludge for template.isSymbol()
696 override final DYNCAST dyncast() const
697 {
698 return DYNCAST.dsymbol;
699 }
700
701 /*************************************
702 * Do syntax copy of an array of Dsymbol's.
703 */
704 extern (D) static Dsymbols* arraySyntaxCopy(Dsymbols* a)
705 {
706 Dsymbols* b = null;
707 if (a)
708 {
709 b = a.copy();
710 for (size_t i = 0; i < b.length; i++)
711 {
712 (*b)[i] = (*b)[i].syntaxCopy(null);
713 }
714 }
715 return b;
716 }
717
718 Identifier getIdent()
719 {
720 return ident;
721 }
722
723 const(char)* toPrettyChars(bool QualifyTypes = false)
724 {
725 //printf("Dsymbol::toPrettyChars() '%s'\n", toChars());
726 if (!parent)
727 {
728 auto s = toChars();
729 return s;
730 }
731
732 OutBuffer buf;
733
734 void addQualifiers(Dsymbol p)
735 {
736 if (p.parent)
737 {
738 addQualifiers(p.parent);
739 buf.writeByte('.');
740 }
741 const s = QualifyTypes ? p.toPrettyCharsHelper() : p.toChars();
742 buf.writestring(s);
743 }
744
745 addQualifiers(this);
746 auto s = buf.extractSlice(true).ptr;
747
748 return s;
749 }
750
751 const(char)* kind() const pure nothrow @nogc @safe
752 {
753 return "symbol";
754 }
755
756 /*********************************
757 * If this symbol is really an alias for another,
758 * return that other.
759 * If needed, semantic() is invoked due to resolve forward reference.
760 */
761 Dsymbol toAlias()
762 {
763 return this;
764 }
765
766 /*********************************
767 * Resolve recursive tuple expansion in eponymous template.
768 */
769 Dsymbol toAlias2()
770 {
771 return toAlias();
772 }
773
774 bool overloadInsert(Dsymbol s)
775 {
776 //printf("Dsymbol::overloadInsert('%s')\n", s.toChars());
777 return false;
778 }
779
780 /*********************************
781 * Returns:
782 * SIZE_INVALID when the size cannot be determined
783 */
784 uinteger_t size(Loc loc)
785 {
786 .error(loc, "%s `%s` symbol `%s` has no size", kind, toPrettyChars, toChars());
787 return SIZE_INVALID;
788 }
789
790 bool isforwardRef()
791 {
792 return false;
793 }
794
795 // is a 'this' required to access the member
796 inout(AggregateDeclaration) isThis() inout
797 {
798 return null;
799 }
800
801 // is Dsymbol exported?
802 bool isExport() const
803 {
804 return false;
805 }
806
807 // is Dsymbol imported?
808 bool isImportedSymbol() const
809 {
810 return false;
811 }
812
813 // is Dsymbol deprecated?
814 bool isDeprecated() @safe @nogc pure nothrow const
815 {
816 return false;
817 }
818
819 bool isOverloadable() const
820 {
821 return false;
822 }
823
824 // is this a LabelDsymbol()?
825 LabelDsymbol isLabel()
826 {
827 return null;
828 }
829
830 /// Returns an AggregateDeclaration when toParent() is that.
831 final inout(AggregateDeclaration) isMember() inout
832 {
833 //printf("Dsymbol::isMember() %s\n", toChars());
834 auto p = toParent();
835 //printf("parent is %s %s\n", p.kind(), p.toChars());
836 return p ? p.isAggregateDeclaration() : null;
837 }
838
839 /// Returns an AggregateDeclaration when toParent2() is that.
840 final inout(AggregateDeclaration) isMember2() inout
841 {
842 //printf("Dsymbol::isMember2() '%s'\n", toChars());
843 auto p = toParent2();
844 //printf("parent is %s %s\n", p.kind(), p.toChars());
845 return p ? p.isAggregateDeclaration() : null;
846 }
847
848 /// Returns an AggregateDeclaration when toParentDecl() is that.
849 final inout(AggregateDeclaration) isMemberDecl() inout
850 {
851 //printf("Dsymbol::isMemberDecl() '%s'\n", toChars());
852 auto p = toParentDecl();
853 //printf("parent is %s %s\n", p.kind(), p.toChars());
854 return p ? p.isAggregateDeclaration() : null;
855 }
856
857 /// Returns an AggregateDeclaration when toParentLocal() is that.
858 final inout(AggregateDeclaration) isMemberLocal() inout
859 {
860 //printf("Dsymbol::isMemberLocal() '%s'\n", toChars());
861 auto p = toParentLocal();
862 //printf("parent is %s %s\n", p.kind(), p.toChars());
863 return p ? p.isAggregateDeclaration() : null;
864 }
865
866 // is this a member of a ClassDeclaration?
867 final ClassDeclaration isClassMember()
868 {
869 auto ad = isMember();
870 return ad ? ad.isClassDeclaration() : null;
871 }
872
873 // is this a type?
874 Type getType()
875 {
876 return null;
877 }
878
879 // need a 'this' pointer?
880 bool needThis()
881 {
882 return false;
883 }
884
885 /*************************************
886 */
887 Visibility visible() pure nothrow @nogc @safe
888 {
889 return Visibility(Visibility.Kind.public_);
890 }
891
892 /**************************************
893 * Copy the syntax.
894 * Used for template instantiations.
895 * If s is NULL, allocate the new object, otherwise fill it in.
896 */
897 Dsymbol syntaxCopy(Dsymbol s)
898 {
899 printf("%s %s\n", kind(), toChars());
900 assert(0);
901 }
902
903 /*****************************************
904 * Same as Dsymbol::oneMember(), but look at an array of Dsymbols.
905 */
906 extern (D) static bool oneMembers(Dsymbols* members, out Dsymbol ps, Identifier ident)
907 {
908 //printf("Dsymbol::oneMembers() %d\n", members ? members.length : 0);
909 Dsymbol s = null;
910 if (!members)
911 {
912 ps = null;
913 return true;
914 }
915
916 for (size_t i = 0; i < members.length; i++)
917 {
918 Dsymbol sx = (*members)[i];
919 bool x = sx.oneMember(ps, ident); //MYTODO: this temporarily creates a new dependency to dsymbolsem, will need to extract oneMembers() later
920 //printf("\t[%d] kind %s = %d, s = %p\n", i, sx.kind(), x, *ps);
921 if (!x)
922 {
923 //printf("\tfalse 1\n");
924 assert(ps is null);
925 return false;
926 }
927 if (ps)
928 {
929 assert(ident);
930 if (!ps.ident || !ps.ident.equals(ident))
931 continue;
932 if (!s)
933 s = ps;
934 else if (s.isOverloadable() && ps.isOverloadable())
935 {
936 // keep head of overload set
937 FuncDeclaration f1 = s.isFuncDeclaration();
938 FuncDeclaration f2 = ps.isFuncDeclaration();
939 if (f1 && f2)
940 {
941 assert(!f1.isFuncAliasDeclaration());
942 assert(!f2.isFuncAliasDeclaration());
943 for (; f1 != f2; f1 = f1.overnext0)
944 {
945 if (f1.overnext0 is null)
946 {
947 f1.overnext0 = f2;
948 break;
949 }
950 }
951 }
952 }
953 else // more than one symbol
954 {
955 ps = null;
956 //printf("\tfalse 2\n");
957 return false;
958 }
959 }
960 }
961 ps = s; // s is the one symbol, null if none
962 //printf("\ttrue\n");
963 return true;
964 }
965
966 /*****************************************
967 * Is Dsymbol a variable that contains pointers?
968 */
969 bool hasPointers()
970 {
971 //printf("Dsymbol::hasPointers() %s\n", toChars());
972 return false;
973 }
974
975 void addObjcSymbols(ClassDeclarations* classes, ClassDeclarations* categories)
976 {
977 }
978
979 /****************************************
980 * Add documentation comment to Dsymbol.
981 * Ignore NULL comments.
982 */
983 void addComment(const(char)* comment)
984 {
985 import dmd.dsymbolsem;
986 dmd.dsymbolsem.addComment(this, comment);
987 }
988
989 /// get documentation comment for this Dsymbol
990 final const(char)* comment()
991 {
992 //printf("getcomment: %p '%s'\n", this, this.toChars());
993 if (auto p = cast(void*)this in commentHashTable)
994 {
995 //printf("comment: '%s'\n", *p);
996 return *p;
997 }
998 return null;
999 }
1000
1001 /* Shell around addComment() to avoid disruption for the moment */
1002 final void comment(const(char)* comment) { addComment(comment); }
1003
1004 extern (D) __gshared const(char)*[void*] commentHashTable;
1005
1006
1007 /**********************************
1008 * Get ddoc unittest associated with this symbol.
1009 * (only use this with ddoc)
1010 * Returns: ddoc unittest, null if none
1011 */
1012 final UnitTestDeclaration ddocUnittest()
1013 {
1014 if (auto p = cast(void*)this in ddocUnittestHashTable)
1015 return *p;
1016 return null;
1017 }
1018
1019 /**********************************
1020 * Set ddoc unittest associated with this symbol.
1021 */
1022 final void ddocUnittest(UnitTestDeclaration utd)
1023 {
1024 ddocUnittestHashTable[cast(void*)this] = utd;
1025 }
1026
1027 private extern (D) __gshared UnitTestDeclaration[void*] ddocUnittestHashTable;
1028
1029
1030 /****************************************
1031 * Returns true if this symbol is defined in a non-root module without instantiation.
1032 */
1033 final bool inNonRoot()
1034 {
1035 Dsymbol s = parent;
1036 for (; s; s = s.toParent())
1037 {
1038 if (auto ti = s.isTemplateInstance())
1039 {
1040 return false;
1041 }
1042 if (auto m = s.isModule())
1043 {
1044 if (!m.isRoot())
1045 return true;
1046 break;
1047 }
1048 }
1049 return false;
1050 }
1051
1052 /**
1053 * Deinitializes the global state of the compiler.
1054 *
1055 * This can be used to restore the state set by `_init` to its original
1056 * state.
1057 */
1058 static void deinitialize()
1059 {
1060 commentHashTable = commentHashTable.init;
1061 ddocUnittestHashTable = ddocUnittestHashTable.init;
1062 }
1063
1064 /************
1065 */
1066 override void accept(Visitor v)
1067 {
1068 v.visit(this);
1069 }
1070
1071 pure nothrow @trusted @nogc final:
1072
1073 inout(Package) isPackage() inout { return (dsym == DSYM.package_ || dsym == DSYM.module_) ? cast(inout(Package)) cast(void*) this : null; }
1074 inout(Module) isModule() inout { return dsym == DSYM.module_ ? cast(inout(Module)) cast(void*) this : null; }
1075 inout(EnumMember) isEnumMember() inout { return dsym == DSYM.enumMember ? cast(inout(EnumMember)) cast(void*) this : null; }
1076 inout(TemplateDeclaration) isTemplateDeclaration() inout { return dsym == DSYM.templateDeclaration ? cast(inout(TemplateDeclaration)) cast(void*) this : null; }
1077 inout(TemplateInstance) isTemplateInstance() inout { return (dsym == DSYM.templateInstance || dsym == DSYM.templateMixin) ? cast(inout(TemplateInstance)) cast(void*) this : null; }
1078 inout(TemplateMixin) isTemplateMixin() inout { return dsym == DSYM.templateMixin ? cast(inout(TemplateMixin)) cast(void*) this : null; }
1079 inout(ForwardingAttribDeclaration) isForwardingAttribDeclaration() inout { return dsym == DSYM.forwardingAttribDeclaration ? cast(inout(ForwardingAttribDeclaration)) cast(void*) this : null; }
1080 inout(Nspace) isNspace() inout { return dsym == DSYM.nspace ? cast(inout(Nspace)) cast(void*) this : null; }
1081 inout(Declaration) isDeclaration() inout {
1082 switch (dsym)
1083 {
1084 case DSYM.tupleDeclaration:
1085 case DSYM.aliasDeclaration:
1086 case DSYM.overDeclaration:
1087 case DSYM.varDeclaration:
1088 case DSYM.bitFieldDeclaration:
1089 case DSYM.typeInfoDeclaration:
1090 case DSYM.thisDeclaration:
1091 case DSYM.enumMember:
1092 case DSYM.symbolDeclaration:
1093 case DSYM.funcDeclaration:
1094 case DSYM.funcAliasDeclaration:
1095 case DSYM.funcLiteralDeclaration:
1096 case DSYM.ctorDeclaration:
1097 case DSYM.postBlitDeclaration:
1098 case DSYM.dtorDeclaration:
1099 case DSYM.staticCtorDeclaration:
1100 case DSYM.sharedStaticCtorDeclaration:
1101 case DSYM.staticDtorDeclaration:
1102 case DSYM.sharedStaticDtorDeclaration:
1103 case DSYM.invariantDeclaration:
1104 case DSYM.unitTestDeclaration:
1105 case DSYM.newDeclaration:
1106 return cast(inout(Declaration)) cast(void*) this;
1107 default:
1108 return null;
1109 }
1110 }
1111 inout(StorageClassDeclaration) isStorageClassDeclaration() inout { return dsym == DSYM.storageClassDeclaration ? cast(inout(StorageClassDeclaration)) cast(void*) this : null; }
1112 inout(ExpressionDsymbol) isExpressionDsymbol() inout { return dsym == DSYM.expressionDsymbol ? cast(inout(ExpressionDsymbol)) cast(void*) this : null; }
1113 inout(AliasAssign) isAliasAssign() inout { return dsym == DSYM.aliasAssign ? cast(inout(AliasAssign)) cast(void*) this : null; }
1114 inout(ThisDeclaration) isThisDeclaration() inout { return dsym == DSYM.thisDeclaration ? cast(inout(ThisDeclaration)) cast(void*) this : null; }
1115 inout(BitFieldDeclaration) isBitFieldDeclaration() inout { return dsym == DSYM.bitFieldDeclaration ? cast(inout(BitFieldDeclaration)) cast(void*) this : null; }
1116 inout(TypeInfoDeclaration) isTypeInfoDeclaration() inout { return dsym == DSYM.typeInfoDeclaration ? cast(inout(TypeInfoDeclaration)) cast(void*) this : null; }
1117 inout(TupleDeclaration) isTupleDeclaration() inout { return dsym == DSYM.tupleDeclaration ? cast(inout(TupleDeclaration)) cast(void*) this : null; }
1118 inout(AliasDeclaration) isAliasDeclaration() inout { return dsym == DSYM.aliasDeclaration ? cast(inout(AliasDeclaration)) cast(void*) this : null; }
1119 inout(AggregateDeclaration) isAggregateDeclaration() inout {
1120 switch (dsym)
1121 {
1122 case DSYM.aggregateDeclaration:
1123 case DSYM.structDeclaration:
1124 case DSYM.unionDeclaration:
1125 case DSYM.classDeclaration:
1126 case DSYM.interfaceDeclaration:
1127 return cast(inout(AggregateDeclaration)) cast(void*) this;
1128 default:
1129 return null;
1130 }
1131 }
1132 inout(FuncDeclaration) isFuncDeclaration() inout {
1133 switch (dsym)
1134 {
1135 case DSYM.funcDeclaration:
1136 case DSYM.funcAliasDeclaration:
1137 case DSYM.funcLiteralDeclaration:
1138 case DSYM.ctorDeclaration:
1139 case DSYM.postBlitDeclaration:
1140 case DSYM.dtorDeclaration:
1141 case DSYM.staticCtorDeclaration:
1142 case DSYM.sharedStaticCtorDeclaration:
1143 case DSYM.staticDtorDeclaration:
1144 case DSYM.sharedStaticDtorDeclaration:
1145 case DSYM.invariantDeclaration:
1146 case DSYM.unitTestDeclaration:
1147 case DSYM.newDeclaration:
1148 return cast(inout(FuncDeclaration)) cast(void*) this;
1149 default:
1150 return null;
1151 }
1152 }
1153 inout(FuncAliasDeclaration) isFuncAliasDeclaration() inout { return dsym == DSYM.funcAliasDeclaration ? cast(inout(FuncAliasDeclaration)) cast(void*) this : null; }
1154 inout(OverDeclaration) isOverDeclaration() inout { return dsym == DSYM.overDeclaration ? cast(inout(OverDeclaration)) cast(void*) this : null; }
1155 inout(FuncLiteralDeclaration) isFuncLiteralDeclaration() inout { return dsym == DSYM.funcLiteralDeclaration ? cast(inout(FuncLiteralDeclaration)) cast(void*) this : null; }
1156 inout(CtorDeclaration) isCtorDeclaration() inout { return dsym == DSYM.ctorDeclaration ? cast(inout(CtorDeclaration)) cast(void*) this : null; }
1157 inout(PostBlitDeclaration) isPostBlitDeclaration() inout { return dsym == DSYM.postBlitDeclaration ? cast(inout(PostBlitDeclaration)) cast(void*) this : null; }
1158 inout(DtorDeclaration) isDtorDeclaration() inout { return dsym == DSYM.dtorDeclaration ? cast(inout(DtorDeclaration)) cast(void*) this : null; }
1159 inout(StaticCtorDeclaration) isStaticCtorDeclaration() inout { return (dsym == DSYM.staticCtorDeclaration || dsym == DSYM.sharedStaticCtorDeclaration) ? cast(inout(StaticCtorDeclaration)) cast(void*) this : null; }
1160 inout(StaticDtorDeclaration) isStaticDtorDeclaration() inout { return (dsym == DSYM.staticDtorDeclaration || dsym == DSYM.sharedStaticDtorDeclaration) ? cast(inout(StaticDtorDeclaration)) cast(void*) this : null; }
1161 inout(SharedStaticCtorDeclaration) isSharedStaticCtorDeclaration() inout { return dsym == DSYM.sharedStaticCtorDeclaration ? cast(inout(SharedStaticCtorDeclaration)) cast(void*) this : null; }
1162 inout(SharedStaticDtorDeclaration) isSharedStaticDtorDeclaration() inout { return dsym == DSYM.sharedStaticDtorDeclaration ? cast(inout(SharedStaticDtorDeclaration)) cast(void*) this : null; }
1163 inout(InvariantDeclaration) isInvariantDeclaration() inout { return dsym == DSYM.invariantDeclaration ? cast(inout(InvariantDeclaration)) cast(void*) this : null; }
1164 inout(UnitTestDeclaration) isUnitTestDeclaration() inout { return dsym == DSYM.unitTestDeclaration ? cast(inout(UnitTestDeclaration)) cast(void*) this : null; }
1165 inout(NewDeclaration) isNewDeclaration() inout { return dsym == DSYM.newDeclaration ? cast(inout(NewDeclaration)) cast(void*) this : null; }
1166 inout(VarDeclaration) isVarDeclaration() inout {
1167 switch (dsym)
1168 {
1169 case DSYM.varDeclaration:
1170 case DSYM.bitFieldDeclaration:
1171 case DSYM.typeInfoDeclaration:
1172 case DSYM.thisDeclaration:
1173 case DSYM.enumMember:
1174 return cast(inout(VarDeclaration)) cast(void*) this;
1175 default:
1176 return null;
1177 }
1178 }
1179 inout(VersionSymbol) isVersionSymbol() inout { return dsym == DSYM.versionSymbol ? cast(inout(VersionSymbol)) cast(void*) this : null; }
1180 inout(DebugSymbol) isDebugSymbol() inout { return dsym == DSYM.debugSymbol ? cast(inout(DebugSymbol)) cast(void*) this : null; }
1181 inout(ClassDeclaration) isClassDeclaration() inout { return (dsym == DSYM.classDeclaration || dsym == DSYM.interfaceDeclaration) ? cast(inout(ClassDeclaration)) cast(void*) this : null; }
1182 inout(StructDeclaration) isStructDeclaration() inout { return (dsym == DSYM.structDeclaration || dsym == DSYM.unionDeclaration) ? cast(inout(StructDeclaration)) cast(void*) this : null; }
1183 inout(UnionDeclaration) isUnionDeclaration() inout { return dsym == DSYM.unionDeclaration ? cast(inout(UnionDeclaration)) cast(void*) this : null; }
1184 inout(InterfaceDeclaration) isInterfaceDeclaration() inout { return dsym == DSYM.interfaceDeclaration ? cast(inout(InterfaceDeclaration)) cast(void*) this : null; }
1185 inout(ScopeDsymbol) isScopeDsymbol() inout {
1186 switch (dsym)
1187 {
1188 case DSYM.enumDeclaration:
1189 case DSYM.scopeDsymbol:
1190 case DSYM.package_:
1191 case DSYM.module_:
1192 case DSYM.nspace:
1193 case DSYM.templateInstance:
1194 case DSYM.templateMixin:
1195 case DSYM.templateDeclaration:
1196 case DSYM.aggregateDeclaration:
1197 case DSYM.structDeclaration:
1198 case DSYM.unionDeclaration:
1199 case DSYM.classDeclaration:
1200 case DSYM.interfaceDeclaration:
1201 case DSYM.withScopeSymbol:
1202 case DSYM.arrayScopeSymbol:
1203 case DSYM.forwardingScopeDsymbol:
1204 return cast(inout(ScopeDsymbol)) cast(void*) this;
1205 default:
1206 return null;
1207 }
1208 }
1209 inout(ForwardingScopeDsymbol) isForwardingScopeDsymbol() inout { return dsym == DSYM.forwardingScopeDsymbol ? cast(inout(ForwardingScopeDsymbol)) cast(void*) this : null; }
1210 inout(WithScopeSymbol) isWithScopeSymbol() inout { return dsym == DSYM.withScopeSymbol ? cast(inout(WithScopeSymbol)) cast(void*) this : null; }
1211 inout(ArrayScopeSymbol) isArrayScopeSymbol() inout { return dsym == DSYM.arrayScopeSymbol ? cast(inout(ArrayScopeSymbol)) cast(void*) this : null; }
1212 inout(Import) isImport() inout { return dsym == DSYM.import_ ? cast(inout(Import)) cast(void*) this : null; }
1213 inout(EnumDeclaration) isEnumDeclaration() inout { return dsym == DSYM.enumDeclaration ? cast(inout(EnumDeclaration)) cast(void*) this : null; }
1214 inout(SymbolDeclaration) isSymbolDeclaration() inout { return dsym == DSYM.symbolDeclaration ? cast(inout(SymbolDeclaration)) cast(void*) this : null; }
1215 inout(AttribDeclaration) isAttribDeclaration() inout {
1216 switch (dsym)
1217 {
1218 case DSYM.attribDeclaration:
1219 case DSYM.storageClassDeclaration:
1220 case DSYM.linkDeclaration:
1221 case DSYM.cppMangleDeclaration:
1222 case DSYM.cppNamespaceDeclaration:
1223 case DSYM.visibilityDeclaration:
1224 case DSYM.alignDeclaration:
1225 case DSYM.anonDeclaration:
1226 case DSYM.pragmaDeclaration:
1227 case DSYM.conditionalDeclaration:
1228 case DSYM.staticIfDeclaration:
1229 case DSYM.staticForeachDeclaration:
1230 case DSYM.forwardingAttribDeclaration:
1231 case DSYM.mixinDeclaration:
1232 case DSYM.userAttributeDeclaration:
1233 return cast(inout(AttribDeclaration)) cast(void*) this;
1234 default:
1235 return null;
1236 }
1237 }
1238 inout(AnonDeclaration) isAnonDeclaration() inout { return dsym == DSYM.anonDeclaration ? cast(inout(AnonDeclaration)) cast(void*) this : null; }
1239 inout(CPPNamespaceDeclaration) isCPPNamespaceDeclaration() inout { return dsym == DSYM.cppNamespaceDeclaration ? cast(inout(CPPNamespaceDeclaration)) cast(void*) this : null; }
1240 inout(VisibilityDeclaration) isVisibilityDeclaration() inout { return dsym == DSYM.visibilityDeclaration ? cast(inout(VisibilityDeclaration)) cast(void*) this : null; }
1241 inout(OverloadSet) isOverloadSet() inout { return dsym == DSYM.overloadSet ? cast(inout(OverloadSet)) cast(void*) this : null; }
1242 inout(MixinDeclaration) isMixinDeclaration() inout { return dsym == DSYM.mixinDeclaration ? cast(inout(MixinDeclaration)) cast(void*) this : null; }
1243 inout(StaticAssert) isStaticAssert() inout { return dsym == DSYM.staticAssert ? cast(inout(StaticAssert)) cast(void*) this : null; }
1244 inout(StaticIfDeclaration) isStaticIfDeclaration() inout { return dsym == DSYM.staticIfDeclaration ? cast(inout(StaticIfDeclaration)) cast(void*) this : null; }
1245 inout(CAsmDeclaration) isCAsmDeclaration() inout { return dsym == DSYM.cAsmDeclaration ? cast(inout(CAsmDeclaration)) cast(void*) this : null; }
1246 }
1247
1248 /***********************************************************
1249 * Dsymbol that generates a scope
1250 */
1251 extern (C++) class ScopeDsymbol : Dsymbol
1252 {
1253 Dsymbols* members; // all Dsymbol's in this scope
1254 DsymbolTable symtab; // members[] sorted into table
1255 uint endlinnum; // the linnumber of the statement after the scope (0 if unknown)
1256 /// symbols whose members have been imported, i.e. imported modules and template mixins
1257 Dsymbols* importedScopes;
1258 Visibility.Kind* visibilities; // array of Visibility.Kind, one for each import
1259
1260 private:
1261
1262 import dmd.root.bitarray;
1263 BitArray accessiblePackages, privateAccessiblePackages;// whitelists of accessible (imported) packages
1264
1265 public:
1266 final extern (D) this() nothrow @safe
1267 {
1268 super(DSYM.scopeDsymbol);
1269 }
1270
1271 final extern (D) this(Identifier ident) nothrow @safe
1272 {
1273 super(DSYM.scopeDsymbol, ident);
1274 }
1275
1276 final extern (D) this(Loc loc, Identifier ident) nothrow @safe
1277 {
1278 super(DSYM.scopeDsymbol, loc, ident);
1279 }
1280
1281 override ScopeDsymbol syntaxCopy(Dsymbol s)
1282 {
1283 //printf("ScopeDsymbol::syntaxCopy('%s')\n", toChars());
1284 ScopeDsymbol sds = s ? cast(ScopeDsymbol)s : new ScopeDsymbol(ident);
1285 sds.comment = comment;
1286 sds.members = arraySyntaxCopy(members);
1287 sds.endlinnum = endlinnum;
1288 return sds;
1289 }
1290
1291 extern (D) final OverloadSet mergeOverloadSet(Identifier ident, OverloadSet os, Dsymbol s)
1292 {
1293 if (!os)
1294 {
1295 os = new OverloadSet(ident);
1296 os.parent = this;
1297 }
1298 if (OverloadSet os2 = s.isOverloadSet())
1299 {
1300 // Merge the cross-module overload set 'os2' into 'os'
1301 if (os.a.length == 0)
1302 {
1303 os.a.setDim(os2.a.length);
1304 memcpy(os.a.tdata(), os2.a.tdata(), (os.a[0]).sizeof * os2.a.length);
1305 }
1306 else
1307 {
1308 for (size_t i = 0; i < os2.a.length; i++)
1309 {
1310 os = mergeOverloadSet(ident, os, os2.a[i]);
1311 }
1312 }
1313 }
1314 else
1315 {
1316 assert(s.isOverloadable());
1317 /* Don't add to os[] if s is alias of previous sym
1318 */
1319 for (size_t j = 0; j < os.a.length; j++)
1320 {
1321 Dsymbol s2 = os.a[j];
1322 if (s.toAlias() == s2.toAlias())
1323 {
1324 if (s2.isDeprecated() || (s2.visible() < s.visible() && s.visible().kind != Visibility.Kind.none))
1325 {
1326 os.a[j] = s;
1327 }
1328 goto Lcontinue;
1329 }
1330 }
1331 os.push(s);
1332 Lcontinue:
1333 }
1334 return os;
1335 }
1336
1337 void importScope(Dsymbol s, Visibility visibility) nothrow
1338 {
1339 //printf("%s.ScopeDsymbol::importScope(%s, %d)\n", toChars(), s.toChars(), visibility);
1340 // No circular or redundant import's
1341 if (s != this)
1342 {
1343 if (!importedScopes)
1344 importedScopes = new Dsymbols();
1345 else
1346 {
1347 for (size_t i = 0; i < importedScopes.length; i++)
1348 {
1349 Dsymbol ss = (*importedScopes)[i];
1350 if (ss == s) // if already imported
1351 {
1352 if (visibility.kind > visibilities[i])
1353 visibilities[i] = visibility.kind; // upgrade access
1354 return;
1355 }
1356 }
1357 }
1358 importedScopes.push(s);
1359 visibilities = cast(Visibility.Kind*)mem.xrealloc(visibilities, importedScopes.length * (visibilities[0]).sizeof);
1360 visibilities[importedScopes.length - 1] = visibility.kind;
1361 }
1362 }
1363
1364
1365 /*****************************************
1366 * Returns: the symbols whose members have been imported, i.e. imported modules
1367 * and template mixins.
1368 *
1369 * See_Also: importScope
1370 */
1371 extern (D) final Dsymbols* getImportedScopes() nothrow @nogc @safe pure
1372 {
1373 return importedScopes;
1374 }
1375
1376 /*****************************************
1377 * Returns: the array of visibilities associated with each imported scope. The
1378 * length of the array matches the imported scopes array.
1379 *
1380 * See_Also: getImportedScopes
1381 */
1382 extern (D) final Visibility.Kind[] getImportVisibilities() nothrow @nogc @safe pure
1383 {
1384 if (!importedScopes)
1385 return null;
1386
1387 return (() @trusted => visibilities[0 .. importedScopes.length])();
1388 }
1389
1390 extern (D) final void addAccessiblePackage(Package p, Visibility visibility) nothrow
1391 {
1392 auto pary = visibility.kind == Visibility.Kind.private_ ? &privateAccessiblePackages : &accessiblePackages;
1393 if (pary.length <= p.tag)
1394 pary.length = p.tag + 1;
1395 (*pary)[p.tag] = true;
1396 }
1397
1398 bool isPackageAccessible(Package p, Visibility visibility, SearchOptFlags flags = SearchOpt.all) nothrow
1399 {
1400 if (p.tag < accessiblePackages.length && accessiblePackages[p.tag] ||
1401 visibility.kind == Visibility.Kind.private_ && p.tag < privateAccessiblePackages.length && privateAccessiblePackages[p.tag])
1402 return true;
1403 foreach (i, ss; importedScopes ? (*importedScopes)[] : null)
1404 {
1405 // only search visible scopes && imported modules should ignore private imports
1406 if (visibility.kind <= visibilities[i] &&
1407 ss.isScopeDsymbol.isPackageAccessible(p, visibility, SearchOpt.ignorePrivateImports))
1408 return true;
1409 }
1410 return false;
1411 }
1412
1413 override final bool isforwardRef() nothrow
1414 {
1415 return (members is null);
1416 }
1417
1418 static void multiplyDefined(Loc loc, Dsymbol s1, Dsymbol s2)
1419 {
1420 version (none)
1421 {
1422 printf("ScopeDsymbol::multiplyDefined()\n");
1423 printf("s1 = %p, '%s' kind = '%s', parent = %s\n", s1, s1.toChars(), s1.kind(), s1.parent ? s1.parent.toChars() : "");
1424 printf("s2 = %p, '%s' kind = '%s', parent = %s\n", s2, s2.toChars(), s2.kind(), s2.parent ? s2.parent.toChars() : "");
1425 }
1426 if (loc.isValid())
1427 {
1428 .error(loc, "`%s` matches conflicting symbols:", s1.ident.toChars());
1429 errorSupplemental(s1.loc, "%s `%s`", s1.kind(), s1.toPrettyChars());
1430 errorSupplemental(s2.loc, "%s `%s`", s2.kind(), s2.toPrettyChars());
1431
1432 static if (0)
1433 {
1434 if (auto so = s1.isOverloadSet())
1435 {
1436 printf("first %p:\n", so);
1437 foreach (s; so.a[])
1438 {
1439 printf(" %p %s `%s` at %s\n", s, s.kind(), s.toPrettyChars(), s.locToChars());
1440 }
1441 }
1442 if (auto so = s2.isOverloadSet())
1443 {
1444 printf("second %p:\n", so);
1445 foreach (s; so.a[])
1446 {
1447 printf(" %p %s `%s` at %s\n", s, s.kind(), s.toPrettyChars(), s.locToChars());
1448 }
1449 }
1450 }
1451 }
1452 else
1453 {
1454 .error(s1.loc, "%s `%s` conflicts with %s `%s` at %s", s1.kind, s1.toPrettyChars, s2.kind(), s2.toPrettyChars(), s2.loc.toChars());
1455 }
1456 }
1457
1458 override const(char)* kind() const
1459 {
1460 return "ScopeDsymbol";
1461 }
1462
1463 /********************************
1464 * Insert Dsymbol in table.
1465 * Params:
1466 * s = symbol to add
1467 * Returns:
1468 * null if already in table, `s` if inserted
1469 */
1470 Dsymbol symtabInsert(Dsymbol s) nothrow
1471 {
1472 return symtab.insert(s);
1473 }
1474
1475 /****************************************
1476 * Look up identifier in symbol table.
1477 * Params:
1478 * s = symbol
1479 * id = identifier to look up
1480 * Returns:
1481 * Dsymbol if found, null if not
1482 */
1483 Dsymbol symtabLookup(Dsymbol s, Identifier id) nothrow
1484 {
1485 return symtab.lookup(id);
1486 }
1487
1488 override void accept(Visitor v)
1489 {
1490 v.visit(this);
1491 }
1492 }
1493
1494 /***********************************************************
1495 * With statement scope
1496 */
1497 extern (C++) final class WithScopeSymbol : ScopeDsymbol
1498 {
1499 WithStatement withstate;
1500
1501 extern (D) this(WithStatement withstate) nothrow @safe
1502 {
1503 this.withstate = withstate;
1504 this.dsym = DSYM.withScopeSymbol;
1505 }
1506
1507 override void accept(Visitor v)
1508 {
1509 v.visit(this);
1510 }
1511 }
1512
1513 /***********************************************************
1514 * Array Index/Slice scope
1515 */
1516 extern (C++) final class ArrayScopeSymbol : ScopeDsymbol
1517 {
1518 // either a SliceExp, an IndexExp, an ArrayExp, a TypeTuple or a TupleDeclaration.
1519 // Discriminated using DYNCAST and, for expressions, also EXP
1520 RootObject arrayContent;
1521
1522 extern (D) this(Scope* sc, Expression exp) nothrow @safe
1523 {
1524 super(exp.loc, null);
1525 assert(exp.op == EXP.index || exp.op == EXP.slice || exp.op == EXP.array);
1526 this._scope = sc;
1527 this.arrayContent = exp;
1528 this.dsym = DSYM.arrayScopeSymbol;
1529 }
1530
1531 extern (D) this(Scope* sc, TypeTuple type) nothrow @safe
1532 {
1533 this._scope = sc;
1534 this.arrayContent = type;
1535 }
1536
1537 extern (D) this(Scope* sc, TupleDeclaration td) nothrow @safe
1538 {
1539 this._scope = sc;
1540 this.arrayContent = td;
1541 }
1542
1543 override void accept(Visitor v)
1544 {
1545 v.visit(this);
1546 }
1547 }
1548
1549 /***********************************************************
1550 * Overload Sets
1551 */
1552 extern (C++) final class OverloadSet : Dsymbol
1553 {
1554 Dsymbols a; // array of Dsymbols
1555
1556 extern (D) this(Identifier ident, OverloadSet os = null) nothrow
1557 {
1558 super(DSYM.overloadSet, ident);
1559 if (os)
1560 {
1561 a.pushSlice(os.a[]);
1562 }
1563 }
1564
1565 void push(Dsymbol s) nothrow
1566 {
1567 a.push(s);
1568 }
1569
1570 override const(char)* kind() const
1571 {
1572 return "overloadset";
1573 }
1574
1575 override void accept(Visitor v)
1576 {
1577 v.visit(this);
1578 }
1579 }
1580
1581 /***********************************************************
1582 * Forwarding ScopeDsymbol. Used by ForwardingAttribDeclaration and
1583 * ForwardingScopeDeclaration to forward symbol insertions to another
1584 * scope. See `dmd.attrib.ForwardingAttribDeclaration` for more
1585 * details.
1586 */
1587 extern (C++) final class ForwardingScopeDsymbol : ScopeDsymbol
1588 {
1589 extern (D) this() nothrow @safe
1590 {
1591 super();
1592 this.dsym = DSYM.forwardingScopeDsymbol;
1593 }
1594
1595 override Dsymbol symtabInsert(Dsymbol s) nothrow
1596 {
1597 if (auto d = s.isDeclaration())
1598 {
1599 if (d.storage_class & STC.local)
1600 {
1601 // Symbols with storage class STC.local are not
1602 // forwarded, but stored in the local symbol
1603 // table. (Those are the `static foreach` variables.)
1604 if (!symtab)
1605 {
1606 symtab = new DsymbolTable();
1607 }
1608 return super.symtabInsert(s); // insert locally
1609 }
1610 }
1611 auto forward = parent.isScopeDsymbol();
1612 assert(forward);
1613 if (!forward.symtab)
1614 {
1615 forward.symtab = new DsymbolTable();
1616 }
1617 // Non-STC.local symbols are forwarded to `forward`.
1618 return forward.symtabInsert(s);
1619 }
1620
1621 /************************
1622 * This override handles the following two cases:
1623 * static foreach (i, i; [0]) { ... }
1624 * and
1625 * static foreach (i; [0]) { enum i = 2; }
1626 */
1627 override Dsymbol symtabLookup(Dsymbol s, Identifier id) nothrow
1628 {
1629 // correctly diagnose clashing foreach loop variables.
1630 if (auto d = s.isDeclaration())
1631 {
1632 if (d.storage_class & STC.local)
1633 {
1634 if (!symtab)
1635 {
1636 symtab = new DsymbolTable();
1637 }
1638 return super.symtabLookup(s,id);
1639 }
1640 }
1641 // Declarations within `static foreach` do not clash with
1642 // `static foreach` loop variables.
1643 auto forward = parent.isScopeDsymbol();
1644 assert(forward);
1645 if (!forward.symtab)
1646 {
1647 forward.symtab = new DsymbolTable();
1648 }
1649 return forward.symtabLookup(s,id);
1650 }
1651
1652 override void importScope(Dsymbol s, Visibility visibility)
1653 {
1654 auto forward = parent.isScopeDsymbol();
1655 assert(forward);
1656 forward.importScope(s, visibility);
1657 }
1658
1659 override const(char)* kind()const{ return "local scope"; }
1660
1661 }
1662
1663 /**
1664 * Class that holds an expression in a Dsymbol wrapper.
1665 * This is not an AST node, but a class used to pass
1666 * an expression as a function parameter of type Dsymbol.
1667 */
1668 extern (C++) final class ExpressionDsymbol : Dsymbol
1669 {
1670 Expression exp;
1671 this(Expression exp) nothrow @safe
1672 {
1673 super(DSYM.expressionDsymbol);
1674 this.exp = exp;
1675 }
1676 }
1677
1678 /**********************************************
1679 * Encapsulate assigning to an alias:
1680 * `identifier = type;`
1681 * `identifier = symbol;`
1682 * where `identifier` is an AliasDeclaration in scope.
1683 */
1684 extern (C++) final class AliasAssign : Dsymbol
1685 {
1686 Identifier ident; /// Dsymbol's ident will be null, as this class is anonymous
1687 Type type; /// replace previous RHS of AliasDeclaration with `type`
1688 Dsymbol aliassym; /// replace previous RHS of AliasDeclaration with `aliassym`
1689 /// only one of type and aliassym can be != null
1690
1691 extern (D) this(Loc loc, Identifier ident, Type type, Dsymbol aliassym) nothrow @safe
1692 {
1693 super(DSYM.aliasAssign, loc, null);
1694 this.ident = ident;
1695 this.type = type;
1696 this.aliassym = aliassym;
1697 }
1698
1699 override AliasAssign syntaxCopy(Dsymbol s)
1700 {
1701 assert(!s);
1702 AliasAssign aa = new AliasAssign(loc, ident,
1703 type ? type.syntaxCopy() : null,
1704 aliassym ? aliassym.syntaxCopy(null) : null);
1705 return aa;
1706 }
1707
1708 override const(char)* kind() const
1709 {
1710 return "alias assignment";
1711 }
1712
1713 override void accept(Visitor v)
1714 {
1715 v.visit(this);
1716 }
1717 }
1718
1719 /***********************************************************
1720 * Table of Dsymbol's
1721 */
1722 extern (C++) final class DsymbolTable : RootObject
1723 {
1724 AssocArray!(Identifier, Dsymbol) tab;
1725
1726 nothrow:
1727
1728 /***************************
1729 * Look up Identifier in symbol table
1730 * Params:
1731 * ident = identifer to look up
1732 * Returns:
1733 * Dsymbol if found, null if not
1734 */
1735 Dsymbol lookup(const Identifier ident)
1736 {
1737 //printf("DsymbolTable::lookup(%s)\n", ident.toChars());
1738 return tab[ident];
1739 }
1740
1741 /**********
1742 * Replace existing symbol in symbol table with `s`.
1743 * If it's not there, add it.
1744 * Params:
1745 * s = replacement symbol with same identifier
1746 */
1747 void update(Dsymbol s)
1748 {
1749 *tab.getLvalue(s.ident) = s;
1750 }
1751
1752 /**************************
1753 * Insert Dsymbol in table.
1754 * Params:
1755 * s = symbol to add
1756 * Returns:
1757 * null if already in table, `s` if inserted
1758 */
1759 Dsymbol insert(Dsymbol s)
1760 {
1761 return insert(s.ident, s);
1762 }
1763
1764 /**************************
1765 * Insert Dsymbol in table.
1766 * Params:
1767 * ident = identifier to serve as index
1768 * s = symbol to add
1769 * Returns:
1770 * null if already in table, `s` if inserted
1771 */
1772 Dsymbol insert(const Identifier ident, Dsymbol s)
1773 {
1774 //printf("DsymbolTable.insert(this = %p, '%s')\n", this, s.ident.toChars());
1775 Dsymbol* ps = tab.getLvalue(ident);
1776 if (*ps)
1777 return null; // already in table
1778 *ps = s;
1779 return s;
1780 }
1781
1782 /*****************
1783 * Returns:
1784 * number of symbols in symbol table
1785 */
1786 size_t length() const pure
1787 {
1788 return tab.length;
1789 }
1790 }
1791
1792 /**
1793 * ImportC global `asm` definition.
1794 */
1795 extern (C++) final class CAsmDeclaration : Dsymbol
1796 {
1797 Expression code;
1798 extern (D) this(Expression e) nothrow @safe
1799 {
1800 super(DSYM.cAsmDeclaration);
1801 this.code = e;
1802 }
1803
1804 override void accept(Visitor v)
1805 {
1806 v.visit(this);
1807 }
1808 }