Skip to content

Commit 17f0965

Browse files
committed
[TBDGen] NFC: move TBDGenVisitor to its own header.
1 parent dfa8501 commit 17f0965

File tree

2 files changed

+138
-87
lines changed

2 files changed

+138
-87
lines changed

lib/TBDGen/TBDGen.cpp

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -28,101 +28,17 @@
2828
#include "swift/SIL/TypeLowering.h"
2929
#include "llvm/ADT/StringSet.h"
3030

31+
#include "TBDGenVisitor.h"
32+
3133
using namespace swift;
3234
using namespace swift::irgen;
35+
using namespace swift::tbdgen;
3336
using StringSet = llvm::StringSet<>;
3437

3538
static bool isPrivateDecl(ValueDecl *VD) {
3639
return getDeclLinkage(VD) != FormalLinkage::PublicUnique;
3740
}
3841

39-
namespace {
40-
class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
41-
StringSet &Symbols;
42-
const UniversalLinkageInfo &UniversalLinkInfo;
43-
ModuleDecl *SwiftModule;
44-
bool FileHasEntryPoint;
45-
bool SILSerializeWitnessTables;
46-
47-
bool InsideAbstractStorageDecl = false;
48-
49-
void addSymbol(StringRef name) {
50-
auto isNewValue = Symbols.insert(name).second;
51-
(void)isNewValue;
52-
assert(isNewValue && "already inserted");
53-
}
54-
55-
void addSymbol(SILDeclRef declRef);
56-
57-
void addSymbol(LinkEntity entity) {
58-
auto linkage =
59-
LinkInfo::get(UniversalLinkInfo, SwiftModule, entity, ForDefinition);
60-
61-
auto externallyVisible =
62-
llvm::GlobalValue::isExternalLinkage(linkage.getLinkage()) &&
63-
linkage.getVisibility() != llvm::GlobalValue::HiddenVisibility;
64-
65-
if (externallyVisible)
66-
addSymbol(linkage.getName());
67-
}
68-
69-
void addConformances(DeclContext *DC);
70-
71-
public:
72-
TBDGenVisitor(StringSet &symbols,
73-
const UniversalLinkageInfo &universalLinkInfo,
74-
ModuleDecl *swiftModule, bool fileHasEntryPoint,
75-
bool silSerializeWitnessTables)
76-
: Symbols(symbols), UniversalLinkInfo(universalLinkInfo),
77-
SwiftModule(swiftModule), FileHasEntryPoint(fileHasEntryPoint),
78-
SILSerializeWitnessTables(silSerializeWitnessTables) {}
79-
80-
void visitMembers(Decl *D) {
81-
SmallVector<Decl *, 4> members;
82-
auto addMembers = [&](DeclRange range) {
83-
for (auto member : range)
84-
members.push_back(member);
85-
};
86-
if (auto ED = dyn_cast<ExtensionDecl>(D))
87-
addMembers(ED->getMembers());
88-
else if (auto NTD = dyn_cast<NominalTypeDecl>(D))
89-
addMembers(NTD->getMembers());
90-
else if (auto ASD = dyn_cast<AbstractStorageDecl>(D))
91-
ASD->getAllAccessorFunctions(members);
92-
93-
for (auto member : members) {
94-
ASTVisitor::visit(member);
95-
}
96-
}
97-
98-
void visitPatternBindingDecl(PatternBindingDecl *PBD);
99-
100-
void visitValueDecl(ValueDecl *VD);
101-
102-
void visitAbstractFunctionDecl(AbstractFunctionDecl *AFD);
103-
104-
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
105-
// any information here is encoded elsewhere
106-
}
107-
108-
void visitNominalTypeDecl(NominalTypeDecl *NTD);
109-
110-
void visitClassDecl(ClassDecl *CD);
111-
112-
void visitConstructorDecl(ConstructorDecl *CD);
113-
114-
void visitExtensionDecl(ExtensionDecl *ED);
115-
116-
void visitProtocolDecl(ProtocolDecl *PD);
117-
118-
void visitAbstractStorageDecl(AbstractStorageDecl *ASD);
119-
120-
void visitVarDecl(VarDecl *VD);
121-
122-
void visitDecl(Decl *D) { visitMembers(D); }
123-
};
124-
} // end anonymous namespace
125-
12642
static bool isGlobalOrStaticVar(VarDecl *VD) {
12743
return VD->isStatic() || VD->getDeclContext()->isModuleScopeContext();
12844
}

lib/TBDGen/TBDGenVisitor.h

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//===--- TBDGenVisitor.h - AST Visitor for TBD generation -----------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the visitor that finds all symbols in a swift AST.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
#ifndef SWIFT_TBDGEN_TBDGENVISITOR_H
17+
#define SWIFT_TBDGEN_TBDGENVISITOR_H
18+
19+
#include "swift/AST/ASTMangler.h"
20+
#include "swift/AST/ASTVisitor.h"
21+
#include "swift/AST/Module.h"
22+
#include "swift/AST/ParameterList.h"
23+
#include "swift/Basic/LLVM.h"
24+
#include "swift/IRGen/Linking.h"
25+
#include "swift/SIL/FormalLinkage.h"
26+
#include "swift/SIL/SILDeclRef.h"
27+
#include "swift/SIL/SILWitnessTable.h"
28+
#include "swift/SIL/TypeLowering.h"
29+
#include "llvm/ADT/StringSet.h"
30+
#include "llvm/ADT/Triple.h"
31+
32+
using namespace swift::irgen;
33+
using StringSet = llvm::StringSet<>;
34+
35+
namespace swift {
36+
namespace tbdgen {
37+
38+
class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
39+
public:
40+
StringSet &Symbols;
41+
const llvm::Triple &Triple;
42+
const UniversalLinkageInfo &UniversalLinkInfo;
43+
ModuleDecl *SwiftModule;
44+
45+
private:
46+
bool FileHasEntryPoint = false;
47+
bool SILSerializeWitnessTables;
48+
49+
bool InsideAbstractStorageDecl = false;
50+
51+
void addSymbol(StringRef name) {
52+
auto isNewValue = Symbols.insert(name).second;
53+
(void)isNewValue;
54+
assert(isNewValue && "already inserted");
55+
}
56+
57+
void addSymbol(SILDeclRef declRef);
58+
59+
void addSymbol(LinkEntity entity) {
60+
auto linkage =
61+
LinkInfo::get(UniversalLinkInfo, SwiftModule, entity, ForDefinition);
62+
63+
auto externallyVisible =
64+
llvm::GlobalValue::isExternalLinkage(linkage.getLinkage()) &&
65+
linkage.getVisibility() != llvm::GlobalValue::HiddenVisibility;
66+
67+
if (externallyVisible)
68+
addSymbol(linkage.getName());
69+
}
70+
71+
void addConformances(DeclContext *DC);
72+
73+
public:
74+
TBDGenVisitor(StringSet &symbols, const llvm::Triple &triple,
75+
const UniversalLinkageInfo &universalLinkInfo,
76+
ModuleDecl *swiftModule, bool silSerializeWitnessTables)
77+
: Symbols(symbols), Triple(triple), UniversalLinkInfo(universalLinkInfo),
78+
SwiftModule(swiftModule),
79+
SILSerializeWitnessTables(silSerializeWitnessTables) {}
80+
81+
void setFileHasEntryPoint(bool hasEntryPoint) {
82+
FileHasEntryPoint = hasEntryPoint;
83+
84+
if (hasEntryPoint)
85+
addSymbol("main");
86+
}
87+
88+
void visitMembers(Decl *D) {
89+
SmallVector<Decl *, 4> members;
90+
auto addMembers = [&](DeclRange range) {
91+
for (auto member : range)
92+
members.push_back(member);
93+
};
94+
if (auto ED = dyn_cast<ExtensionDecl>(D))
95+
addMembers(ED->getMembers());
96+
else if (auto NTD = dyn_cast<NominalTypeDecl>(D))
97+
addMembers(NTD->getMembers());
98+
else if (auto ASD = dyn_cast<AbstractStorageDecl>(D))
99+
ASD->getAllAccessorFunctions(members);
100+
101+
for (auto member : members) {
102+
ASTVisitor::visit(member);
103+
}
104+
}
105+
106+
void visitPatternBindingDecl(PatternBindingDecl *PBD);
107+
108+
void visitValueDecl(ValueDecl *VD);
109+
110+
void visitAbstractFunctionDecl(AbstractFunctionDecl *AFD);
111+
112+
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
113+
// any information here is encoded elsewhere
114+
}
115+
116+
void visitNominalTypeDecl(NominalTypeDecl *NTD);
117+
118+
void visitClassDecl(ClassDecl *CD);
119+
120+
void visitConstructorDecl(ConstructorDecl *CD);
121+
122+
void visitExtensionDecl(ExtensionDecl *ED);
123+
124+
void visitProtocolDecl(ProtocolDecl *PD);
125+
126+
void visitAbstractStorageDecl(AbstractStorageDecl *ASD);
127+
128+
void visitVarDecl(VarDecl *VD);
129+
130+
void visitDecl(Decl *D) { visitMembers(D); }
131+
};
132+
} // end namespace tbdgen
133+
} // end namespace swift
134+
135+
#endif

0 commit comments

Comments
 (0)