2 * A `Dsymbol` representing a renamed import.
4 * Copyright: Copyright (C) 1999-2024 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/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/src/dmd/dimport.d
14 import dmd
.arraytypes
;
18 import dmd
.identifier
;
22 /***********************************************************
24 extern (C
++) final class Import
: Dsymbol
26 /* static import aliasId = pkg1.pkg2.id : alias1 = name1, alias2 = name2;
28 Identifier
[] packages
; // array of Identifier's representing packages
29 Identifier id
; // module Identifier
31 int isstatic
; // !=0 if static import
32 Visibility visibility
;
34 // Pairs of alias=name to bind into current namespace
39 Package pkg
; // leftmost package/module
41 // corresponding AliasDeclarations for alias=name pairs
42 AliasDeclarations aliasdecls
;
44 extern (D
) this(const ref Loc loc
, Identifier
[] packages
, Identifier id
, Identifier aliasId
, int isstatic
)
46 Identifier
selectIdent()
48 // select Dsymbol identifier (bracketed)
51 // import [aliasId] = std.stdio;
54 else if (packages
.length
> 0)
56 // import [std].stdio;
66 super(loc
, selectIdent());
71 import core
.stdc
.stdio
;
73 printf("Import::Import(");
74 foreach (id
; packages
)
76 printf("%s.", id
.toChars());
78 printf("%s)\n", id
.toChars());
80 this.packages
= packages
;
82 this.aliasId
= aliasId
;
83 this.isstatic
= isstatic
;
84 this.visibility
= Visibility
.Kind
.private_
; // default to private
87 extern (D
) void addAlias(Identifier name
, Identifier _alias
)
90 .error(loc
, "%s `%s` cannot have an import bind list", kind
, toPrettyChars
);
92 this.ident
= null; // make it an anonymous import
97 override const(char)* kind() const
99 return isstatic ?
"static import" : "import";
102 override Visibility
visible() pure nothrow @nogc @safe
107 // copy only syntax trees
108 override Import
syntaxCopy(Dsymbol s
)
111 auto si
= new Import(loc
, packages
, id
, aliasId
, isstatic
);
112 si
.comment
= comment
;
113 for (size_t i
= 0; i
< names
.length
; i
++)
115 si
.addAlias(names
[i
], aliases
[i
]);
120 /*******************************
121 * Mark the imported packages as accessible from the current
122 * scope. This access check is necessary when using FQN b/c
123 * we're using a single global package tree.
124 * https://issues.dlang.org/show_bug.cgi?id=313
126 extern (D
) void addPackageAccess(ScopeDsymbol scopesym
)
128 //printf("Import::addPackageAccess('%s') %p\n", toPrettyChars(), this);
129 if (packages
.length
> 0)
133 scopesym
.addAccessiblePackage(p
, visibility
);
134 foreach (id
; packages
[1 .. $]) // [b, c]
136 auto sym
= p
.symtab
.lookup(id
);
137 // https://issues.dlang.org/show_bug.cgi?id=17991
138 // An import of truly empty file/package can happen
139 // https://issues.dlang.org/show_bug.cgi?id=20151
140 // Package in the path conflicts with a module name
143 // https://issues.dlang.org/show_bug.cgi?id=23327
144 // Package conflicts with symbol of the same name
148 scopesym
.addAccessiblePackage(p
, visibility
);
151 scopesym
.addAccessiblePackage(mod
, visibility
); // d
154 override Dsymbol
toAlias()
161 override bool overloadInsert(Dsymbol s
)
163 /* Allow multiple imports with the same package base, but disallow
165 * https://issues.dlang.org/show_bug.cgi?id=5412
167 assert(ident
&& ident
== s
.ident
);
169 if (!aliasId
&& (imp
= s
.isImport()) !is null && !imp
.aliasId
)
175 override inout(Import
) isImport() inout
180 override void accept(Visitor v
)