|
| 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