|
| 1 | +//===--- GenClangDecl.cpp - Swift IRGen for imported Clang declarations ---===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "IRGenModule.h" |
| 14 | +#include "clang/AST/Decl.h" |
| 15 | +#include "clang/AST/DeclGroup.h" |
| 16 | +#include "clang/AST/DataRecursiveASTVisitor.h" |
| 17 | +#include "clang/CodeGen/ModuleBuilder.h" |
| 18 | +#include "llvm/ADT/SmallPtrSet.h" |
| 19 | + |
| 20 | +using namespace swift; |
| 21 | +using namespace irgen; |
| 22 | + |
| 23 | +namespace { |
| 24 | +class ClangDeclRefFinder |
| 25 | + : public clang::DataRecursiveASTVisitor<ClangDeclRefFinder> { |
| 26 | + std::function<void(const clang::DeclRefExpr *)> callback; |
| 27 | +public: |
| 28 | + template <typename Fn> |
| 29 | + explicit ClangDeclRefFinder(Fn fn) : callback(fn) {} |
| 30 | + |
| 31 | + bool VisitDeclRefExpr(clang::DeclRefExpr *DRE) { |
| 32 | + callback(DRE); |
| 33 | + return true; |
| 34 | + } |
| 35 | +}; |
| 36 | +} // end anonymous namespace |
| 37 | + |
| 38 | +void IRGenModule::emitLocalDecls(clang::Decl *decl) { |
| 39 | + auto valueDecl = dyn_cast<clang::ValueDecl>(decl); |
| 40 | + if (!valueDecl || valueDecl->isExternallyVisible()) { |
| 41 | + ClangCodeGen->HandleTopLevelDecl(clang::DeclGroupRef(decl)); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (!GlobalClangDecls.insert(decl).second) |
| 46 | + return; |
| 47 | + SmallVector<const clang::Decl *, 8> stack; |
| 48 | + stack.push_back(decl); |
| 49 | + |
| 50 | + ClangDeclRefFinder refFinder([&](const clang::DeclRefExpr *DRE) { |
| 51 | + const clang::ValueDecl *D = DRE->getDecl(); |
| 52 | + if (!D->hasLinkage() || D->isExternallyVisible()) |
| 53 | + return; |
| 54 | + if (!GlobalClangDecls.insert(D->getCanonicalDecl()).second) |
| 55 | + return; |
| 56 | + stack.push_back(D); |
| 57 | + }); |
| 58 | + |
| 59 | + while (!stack.empty()) { |
| 60 | + auto *next = const_cast<clang::Decl *>(stack.pop_back_val()); |
| 61 | + if (auto fn = dyn_cast<clang::FunctionDecl>(next)) { |
| 62 | + const clang::FunctionDecl *definition; |
| 63 | + if (fn->hasBody(definition)) |
| 64 | + refFinder.TraverseDecl(const_cast<clang::FunctionDecl *>(definition)); |
| 65 | + } |
| 66 | + ClangCodeGen->HandleTopLevelDecl(clang::DeclGroupRef(next)); |
| 67 | + } |
| 68 | +} |
| 69 | + |
0 commit comments