Skip to content

Commit 09093e8

Browse files
committed
Pass in out param to ImportPath getters
Modify ASTDumper to print module real names Add doc comments
1 parent fd5aaa5 commit 09093e8

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

include/swift/AST/Decl.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,13 +1132,19 @@ class ImportDecl final : public Decl,
11321132

11331133
SourceLoc ImportLoc;
11341134
SourceLoc KindLoc;
1135+
// Used to store the real module name corresponding to this import decl
1136+
// in case module aliasing is used. For example if '-module-alias Foo=Bar'
1137+
// was passed and this decl is 'import Foo', the real name 'Bar' will be
1138+
// stored.
11351139
Identifier RealModuleName;
11361140

11371141
/// The resolved module.
11381142
ModuleDecl *Mod = nullptr;
11391143

11401144
ImportDecl(DeclContext *DC, SourceLoc ImportLoc, ImportKind K,
11411145
SourceLoc KindLoc, ImportPath Path);
1146+
// Sets the real module name corresponding to this import decl in
1147+
// case module aliasing is used. Called in \c ImportDecl::create.
11421148
void setRealModuleName(Identifier name) { RealModuleName = name; };
11431149
public:
11441150
static ImportDecl *create(ASTContext &C, DeclContext *DC,
@@ -1163,27 +1169,33 @@ class ImportDecl final : public Decl,
11631169
return static_cast<ImportKind>(Bits.ImportDecl.ImportKind);
11641170
}
11651171

1166-
ImportPath getImportPath(bool withRealModuleName = false) const {
1172+
/// Retrieves the full import path.
1173+
/// \param outRealModuleName An ImportPath builder to write the real module name to if module aliasing
1174+
/// was used. For example, if '-module-alias Foo=Bar' was passed, the real name 'Bar' will be written
1175+
/// to it, which corresponds to 'import Foo' in source file.
1176+
/// \returns An ImportPath corresponding to this import decl. If module aliasing was used, 'Foo' will be in the
1177+
/// returned path instead of 'Bar' using the above example.
1178+
ImportPath getImportPath(ImportPath::Builder *outRealModuleName = nullptr) const {
11671179
auto path = ImportPath({ getTrailingObjects<ImportPath::Element>(),
1168-
static_cast<size_t>(Bits.ImportDecl.NumPathElements) });
1169-
if (withRealModuleName && !RealModuleName.empty()) {
1170-
ImportPath::Builder realPath;
1180+
static_cast<size_t>(Bits.ImportDecl.NumPathElements) });;
1181+
1182+
if (outRealModuleName != nullptr && !RealModuleName.empty()) {
11711183
for (auto elem: path) {
1172-
if (realPath.empty()) {
1173-
realPath.push_back(RealModuleName);
1184+
if (outRealModuleName->empty()) {
1185+
// Add the real module name instead of its alias
1186+
outRealModuleName->push_back(RealModuleName);
11741187
} else {
1175-
realPath.push_back(elem.Item);
1188+
// Add the rest if any (access path elements)
1189+
outRealModuleName->push_back(elem.Item);
11761190
}
11771191
}
1178-
path = ImportPath(realPath.get().getRaw());
11791192
}
11801193
return path;
11811194
}
11821195

1183-
ImportPath::Module getModulePath(bool withRealName = false) const {
1184-
if (withRealName && !RealModuleName.empty()) {
1185-
ImportPath::Module::Builder builder(RealModuleName);
1186-
return builder.get();
1196+
ImportPath::Module getModulePath(ImportPath::Module::Builder *outRealModuleName = nullptr) const {
1197+
if (outRealModuleName != nullptr && !RealModuleName.empty()) {
1198+
outRealModuleName->push_back(RealModuleName);
11871199
}
11881200
return getImportPath().getModulePath(getImportKind());
11891201
}

lib/AST/ASTDumper.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,17 @@ namespace {
521521
OS << " kind=" << getImportKindString(ID->getImportKind());
522522

523523
OS << " '";
524-
ID->getImportPath(/*withRealModuleName=*/true).print(OS);
524+
// Check if module aliasing was used for the given imported module; for
525+
// example, if '-module-alias Foo=Bar' was passed and this module has
526+
// 'import Foo', its corresponding real module name 'Bar' should be printed.
527+
ImportPath::Builder builderWithRealModuleName;
528+
auto path = ID->getImportPath(/*outRealModuleName=*/&builderWithRealModuleName);
529+
if (!builderWithRealModuleName.get().empty()) {
530+
// Module aliasing was used, so print its real module name
531+
builderWithRealModuleName.get().print(OS);
532+
} else {
533+
path.print(OS);
534+
}
525535
OS << "')";
526536
}
527537

@@ -1223,7 +1233,7 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
12231233

12241234
switch (dc->getContextKind()) {
12251235
case DeclContextKind::Module:
1226-
printName(os, cast<ModuleDecl>(dc)->getName());
1236+
printName(os, cast<ModuleDecl>(dc)->getRealName());
12271237
break;
12281238

12291239
case DeclContextKind::FileUnit:
@@ -1304,11 +1314,13 @@ void ValueDecl::dumpRef(raw_ostream &os) const {
13041314
// Print the context.
13051315
printContext(os, getDeclContext());
13061316
os << ".";
1317+
// Print name.
1318+
getName().printPretty(os);
1319+
} else {
1320+
auto moduleName = cast<ModuleDecl>(this)->getRealName();
1321+
os << moduleName;
13071322
}
13081323

1309-
// Print name.
1310-
getName().printPretty(os);
1311-
13121324
// Print location.
13131325
auto &srcMgr = getASTContext().SourceMgr;
13141326
if (getLoc().isValid()) {

lib/FrontendTool/ImportedModules.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,20 @@ bool swift::emitImportedModules(ModuleDecl *mainModule,
6565
auto ID = dyn_cast<ImportDecl>(D);
6666
if (!ID)
6767
continue;
68-
auto modulePath = ID->getModulePath(/*withRealModuleName=*/true);
69-
// only the top-level name is needed (i.e. A in A.B.C)
70-
Modules.insert(modulePath[0].Item.str());
68+
69+
// Check if module aliasing was used for an imported module; for example,
70+
// if '-module-alias Foo=Bar' was passed and this module has 'import Foo',
71+
// its corresponding real module name 'Bar' should be emitted.
72+
ImportPath::Module::Builder builderWithRealModuleName;
73+
auto modulePath = ID->getModulePath(/*outRealModuleName=*/&builderWithRealModuleName);
74+
75+
if (!builderWithRealModuleName.get().empty()) {
76+
// Module aliasing was used, so use its real module name
77+
Modules.insert(builderWithRealModuleName.get().front().Item.str());
78+
} else {
79+
// only the top-level name is needed (i.e. A in A.B.C)
80+
Modules.insert(modulePath[0].Item.str());
81+
}
7182
}
7283

7384
// And now look in the C code we're possibly using.

test/Frontend/module-alias-dump-ast.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
// RUN: %target-swift-frontend -dump-ast %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t > %t/result-ast.output
1414

1515
// RUN: %FileCheck %s -input-file %t/result-ast.output -check-prefix CHECK-AST
16+
// CHECK-AST-NOT: bind=XLogging
17+
// CHECK-AST-NOT: module<XLogging>
18+
// CHECK-AST-NOT: decl=XLogging
19+
// CHECK-AST: component id='XLogging' bind=AppleLogging
1620
// CHECK-AST: module<AppleLogging>
17-
// RUN: not %FileCheck %s -input-file %t/result-ast.output -check-prefix CHECK-NOT-AST
18-
// CHECK-NOT-AST: XLogging
19-
21+
// CHECK-AST: decl=AppleLogging
2022

2123
// BEGIN FileLogging.swift
2224
public struct Logger {
2325
public init() {}
2426
}
25-
public func setup() -> XLogging.Logger? {
27+
public func setup() -> Logger? {
2628
return Logger()
2729
}
2830

0 commit comments

Comments
 (0)