Skip to content

Commit 38779f3

Browse files
committed
SILGen/IRGen: Delegate decision to skip @_silgen_name functions to TBDGenVisitor.
This allows SILGen for `#_hasSymbol` conditions to visit them and emit a declaration of the function appropriately.
1 parent 2847ee9 commit 38779f3

File tree

7 files changed

+18
-14
lines changed

7 files changed

+18
-14
lines changed

include/swift/IRGen/IRSymbolVisitor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class IRSymbolVisitor {
5858
const IRSymbolVisitorContext &ctx);
5959

6060
/// Override to prepare for enumeration of the symbols for a specific decl.
61-
virtual void willVisitDecl(Decl *D) {}
61+
/// Return \c true to proceed with visiting the decl or \c false to skip it.
62+
virtual bool willVisitDecl(Decl *D) { return true; }
6263

6364
/// Override to clean up after enumeration of the symbols for a specific decl.
6465
virtual void didVisitDecl(Decl *D) {}

include/swift/SIL/SILSymbolVisitor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class SILSymbolVisitor {
7676
const SILSymbolVisitorContext &ctx);
7777

7878
/// Override to prepare for enumeration of the symbols for a specific decl.
79-
virtual void willVisitDecl(Decl *D) {}
79+
/// Return \c true to proceed with visiting the decl or \c false to skip it.
80+
virtual bool willVisitDecl(Decl *D) { return true; }
8081

8182
/// Override to clean up after enumeration of the symbols for a specific decl.
8283
virtual void didVisitDecl(Decl *D) {}

lib/IRGen/IRSymbolVisitor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class IRSymbolVisitorImpl : public SILSymbolVisitor {
7272
: Visitor{Visitor}, Ctx{Ctx},
7373
PublicSymbolsOnly{Ctx.getSILCtx().getOpts().PublicSymbolsOnly} {}
7474

75-
void willVisitDecl(Decl *D) override {
76-
Visitor.willVisitDecl(D);
75+
bool willVisitDecl(Decl *D) override {
76+
return Visitor.willVisitDecl(D);
7777
}
7878

7979
void didVisitDecl(Decl *D) override {

lib/IRGen/TBDGen.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,15 @@ void TBDGenVisitor::addSymbol(StringRef name, SymbolSource source,
400400
}
401401
}
402402

403-
void TBDGenVisitor::willVisitDecl(Decl *D) {
403+
bool TBDGenVisitor::willVisitDecl(Decl *D) {
404+
// A @_silgen_name("...") function without a body only exists to
405+
// forward-declare a symbol from another library.
406+
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D))
407+
if (!AFD->hasBody() && AFD->getAttrs().hasAttribute<SILGenNameAttr>())
408+
return false;
409+
404410
DeclStack.push_back(D);
411+
return true;
405412
}
406413

407414
void TBDGenVisitor::didVisitDecl(Decl *D) {

lib/IRGen/TBDGenVisitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class TBDGenVisitor : public IRSymbolVisitor {
139139

140140
// --- IRSymbolVisitor ---
141141

142-
void willVisitDecl(Decl *D) override;
142+
bool willVisitDecl(Decl *D) override;
143143
void didVisitDecl(Decl *D) override;
144144

145145
void addFunction(SILDeclRef declRef) override;

lib/SIL/IR/SILSymbolVisitor.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
362362
: Visitor{Visitor}, Ctx{Ctx} {}
363363

364364
void visit(Decl *D) {
365-
Visitor.willVisitDecl(D);
365+
if (!Visitor.willVisitDecl(D))
366+
return;
366367
ASTVisitor::visit(D);
367368
Visitor.didVisitDecl(D);
368369
}
@@ -408,12 +409,6 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
408409
}
409410

410411
void visitAbstractFunctionDecl(AbstractFunctionDecl *AFD) {
411-
// A @_silgen_name("...") function without a body only exists to
412-
// forward-declare a symbol from another library.
413-
if (!AFD->hasBody() && AFD->getAttrs().hasAttribute<SILGenNameAttr>()) {
414-
return;
415-
}
416-
417412
// Add exported prespecialized symbols.
418413
for (auto *attr : AFD->getAttrs().getAttributes<SpecializeAttr>()) {
419414
if (!attr->isExported())

test/SILGen/has_symbol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func testGlobalFunctions() {
4141

4242
// --- forwardDeclaredFunc() ---
4343
// CHECK: sil hidden_external @$s17has_symbol_helper19forwardDeclaredFuncyyFTwS : $@convention(thin) () -> Builtin.Int1
44-
// FIXME: missing forward declared function
44+
// CHECK: sil @forward_declared_func : $@convention(thin) () -> ()
4545

4646

4747
// CHECK: sil hidden [ossa] @$s4test0A4VarsyyF : $@convention(thin) () -> ()

0 commit comments

Comments
 (0)