|
| 1 | +//===- Visitor.cpp ---------------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "clang/InstallAPI/Visitor.h" |
| 10 | +#include "clang/AST/Availability.h" |
| 11 | +#include "clang/Basic/Linkage.h" |
| 12 | +#include "llvm/ADT/SmallString.h" |
| 13 | +#include "llvm/ADT/StringRef.h" |
| 14 | +#include "llvm/IR/DataLayout.h" |
| 15 | +#include "llvm/IR/Mangler.h" |
| 16 | + |
| 17 | +using namespace llvm; |
| 18 | +using namespace llvm::MachO; |
| 19 | + |
| 20 | +namespace clang::installapi { |
| 21 | + |
| 22 | +// Exported NamedDecl needs to have externally visibiliy linkage and |
| 23 | +// default visibility from LinkageComputer. |
| 24 | +static bool isExported(const NamedDecl *D) { |
| 25 | + auto LV = D->getLinkageAndVisibility(); |
| 26 | + return isExternallyVisible(LV.getLinkage()) && |
| 27 | + (LV.getVisibility() == DefaultVisibility); |
| 28 | +} |
| 29 | + |
| 30 | +static SymbolFlags getFlags(bool WeakDef, bool ThreadLocal) { |
| 31 | + SymbolFlags Result = SymbolFlags::None; |
| 32 | + if (WeakDef) |
| 33 | + Result |= SymbolFlags::WeakDefined; |
| 34 | + if (ThreadLocal) |
| 35 | + Result |= SymbolFlags::ThreadLocalValue; |
| 36 | + |
| 37 | + return Result; |
| 38 | +} |
| 39 | + |
| 40 | +void InstallAPIVisitor::HandleTranslationUnit(ASTContext &ASTCtx) { |
| 41 | + if (ASTCtx.getDiagnostics().hasErrorOccurred()) |
| 42 | + return; |
| 43 | + |
| 44 | + auto *D = ASTCtx.getTranslationUnitDecl(); |
| 45 | + TraverseDecl(D); |
| 46 | +} |
| 47 | + |
| 48 | +std::string InstallAPIVisitor::getMangledName(const NamedDecl *D) const { |
| 49 | + SmallString<256> Name; |
| 50 | + if (MC->shouldMangleDeclName(D)) { |
| 51 | + raw_svector_ostream NStream(Name); |
| 52 | + MC->mangleName(D, NStream); |
| 53 | + } else |
| 54 | + Name += D->getNameAsString(); |
| 55 | + |
| 56 | + return getBackendMangledName(Name); |
| 57 | +} |
| 58 | + |
| 59 | +std::string InstallAPIVisitor::getBackendMangledName(Twine Name) const { |
| 60 | + SmallString<256> FinalName; |
| 61 | + Mangler::getNameWithPrefix(FinalName, Name, DataLayout(Layout)); |
| 62 | + return std::string(FinalName); |
| 63 | +} |
| 64 | + |
| 65 | +/// Collect all global variables. |
| 66 | +bool InstallAPIVisitor::VisitVarDecl(const VarDecl *D) { |
| 67 | + // Skip function parameters. |
| 68 | + if (isa<ParmVarDecl>(D)) |
| 69 | + return true; |
| 70 | + |
| 71 | + // Skip variables in records. They are handled seperately for C++. |
| 72 | + if (D->getDeclContext()->isRecord()) |
| 73 | + return true; |
| 74 | + |
| 75 | + // Skip anything inside functions or methods. |
| 76 | + if (!D->isDefinedOutsideFunctionOrMethod()) |
| 77 | + return true; |
| 78 | + |
| 79 | + // If this is a template but not specialization or instantiation, skip. |
| 80 | + if (D->getASTContext().getTemplateOrSpecializationInfo(D) && |
| 81 | + D->getTemplateSpecializationKind() == TSK_Undeclared) |
| 82 | + return true; |
| 83 | + |
| 84 | + // TODO: Capture SourceLocation & Availability for Decls. |
| 85 | + const RecordLinkage Linkage = |
| 86 | + isExported(D) ? RecordLinkage::Exported : RecordLinkage::Internal; |
| 87 | + const bool WeakDef = D->hasAttr<WeakAttr>(); |
| 88 | + const bool ThreadLocal = D->getTLSKind() != VarDecl::TLS_None; |
| 89 | + Slice.addGlobal(getMangledName(D), Linkage, GlobalRecord::Kind::Variable, |
| 90 | + getFlags(WeakDef, ThreadLocal)); |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace clang::installapi |
0 commit comments