Debian Codegraph @

gcc/d/dmd/dimport.d

raw
1 /**
2 * A `Dsymbol` representing a renamed import.
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/dimport.d, _dimport.d)
8 * Documentation: https://dlang.org/phobos/dmd_dimport.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/dimport.d
10 */
11
12 module dmd.dimport;
13
14 import dmd.arraytypes;
15 import dmd.dmodule;
16 import dmd.dsymbol;
17 import dmd.identifier;
18 import dmd.location;
19 import dmd.visitor;
20
21 /***********************************************************
22 */
23 extern (C++) final class Import : Dsymbol
24 {
25 /* static import aliasId = pkg1.pkg2.id : alias1 = name1, alias2 = name2;
26 */
27 Identifier[] packages; // array of Identifier's representing packages
28 Identifier id; // module Identifier
29 Identifier aliasId;
30 int isstatic; // !=0 if static import
31 Visibility visibility;
32
33 // Pairs of alias=name to bind into current namespace
34 Identifiers names;
35 Identifiers aliases;
36
37 Module mod;
38 Package pkg; // leftmost package/module
39
40 // corresponding AliasDeclarations for alias=name pairs
41 AliasDeclarations aliasdecls;
42
43 extern (D) this(Loc loc, Identifier[] packages, Identifier id, Identifier aliasId, int isstatic)
44 {
45 Identifier selectIdent()
46 {
47 // select Dsymbol identifier (bracketed)
48 if (aliasId)
49 {
50 // import [aliasId] = std.stdio;
51 return aliasId;
52 }
53 if (packages.length > 0)
54 {
55 // import [std].stdio;
56 return packages[0];
57 }
58 // import [id];
59 return id;
60 }
61
62 super(DSYM.import_, loc, selectIdent());
63
64 assert(id);
65 version (none)
66 {
67 import core.stdc.stdio;
68
69 printf("Import::Import(");
70 foreach (id; packages)
71 {
72 printf("%s.", id.toChars());
73 }
74 printf("%s)\n", id.toChars());
75 }
76 this.packages = packages;
77 this.id = id;
78 this.aliasId = aliasId;
79 this.isstatic = isstatic;
80 this.visibility = Visibility.Kind.private_; // default to private
81 }
82
83 override const(char)* kind() const
84 {
85 return isstatic ? "static import" : "import";
86 }
87
88 override Visibility visible() pure nothrow @nogc @safe
89 {
90 return visibility;
91 }
92
93 // copy only syntax trees
94 override Import syntaxCopy(Dsymbol s)
95 {
96 assert(!s);
97 auto si = new Import(loc, packages, id, aliasId, isstatic);
98 si.comment = comment;
99 assert(!(isstatic && names.length));
100 if (names.length && !si.aliasId)
101 si.ident = null;
102 for (size_t i = 0; i < names.length; i++)
103 {
104 si.names.push(names[i]);
105 si.aliases.push(aliases[i]);
106 }
107 return si;
108 }
109
110 /*******************************
111 * Mark the imported packages as accessible from the current
112 * scope. This access check is necessary when using FQN b/c
113 * we're using a single global package tree.
114 * https://issues.dlang.org/show_bug.cgi?id=313
115 */
116 extern (D) void addPackageAccess(ScopeDsymbol scopesym)
117 {
118 //printf("Import::addPackageAccess('%s') %p\n", toPrettyChars(), this);
119 if (packages.length > 0)
120 {
121 // import a.b.c.d;
122 auto p = pkg; // a
123 scopesym.addAccessiblePackage(p, visibility);
124 foreach (id; packages[1 .. $]) // [b, c]
125 {
126 auto sym = p.symtab.lookup(id);
127 // https://issues.dlang.org/show_bug.cgi?id=17991
128 // An import of truly empty file/package can happen
129 // https://issues.dlang.org/show_bug.cgi?id=20151
130 // Package in the path conflicts with a module name
131 if (sym is null)
132 break;
133 // https://issues.dlang.org/show_bug.cgi?id=23327
134 // Package conflicts with symbol of the same name
135 p = sym.isPackage();
136 if (p is null)
137 break;
138 scopesym.addAccessiblePackage(p, visibility);
139 }
140 }
141 scopesym.addAccessiblePackage(mod, visibility); // d
142 }
143
144 override Dsymbol toAlias()
145 {
146 if (aliasId)
147 return mod;
148 return this;
149 }
150
151 override bool overloadInsert(Dsymbol s)
152 {
153 /* Allow multiple imports with the same package base, but disallow
154 * alias collisions
155 * https://issues.dlang.org/show_bug.cgi?id=5412
156 */
157 assert(ident && ident == s.ident);
158 if (aliasId)
159 return false;
160 const imp = s.isImport();
161 return imp && !imp.aliasId;
162 }
163
164 override void accept(Visitor v)
165 {
166 v.visit(this);
167 }
168 }