[Orc] Fix syntax error in LazyEmittingLayer::removeModuleSet.
[oota-llvm.git] / include / llvm / ExecutionEngine / Orc / LookasideRTDyldMM.h
1 //===- LookasideRTDyldMM - Redirect symbol lookup via a functor -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //   Defines an adapter for RuntimeDyldMM that allows lookups for external
11 // symbols to go via a functor.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H
16 #define LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H
17
18 #include <memory>
19 #include <vector>
20
21 namespace llvm {
22
23 /// @brief Defines an adapter for RuntimeDyldMM that allows lookups for external
24 ///        symbols to go via a functor, before falling back to the lookup logic
25 ///        provided by the underlying RuntimeDyldMM instance.
26 ///
27 ///   This class is useful for redirecting symbol lookup back to various layers
28 /// of a JIT component stack, e.g. to enable lazy module emission.
29 ///
30 template <typename BaseRTDyldMM, typename ExternalLookupFtor,
31           typename DylibLookupFtor>
32 class LookasideRTDyldMM : public BaseRTDyldMM {
33 public:
34   /// @brief Create a LookasideRTDyldMM intance.
35   LookasideRTDyldMM(ExternalLookupFtor ExternalLookup,
36                     DylibLookupFtor DylibLookup)
37       : ExternalLookup(std::move(ExternalLookup)),
38         DylibLookup(std::move(DylibLookup)) {}
39
40   /// @brief Look up the given symbol address, first via the functor this
41   ///        instance was created with, then (if the symbol isn't found)
42   ///        via the underlying RuntimeDyldMM.
43   uint64_t getSymbolAddress(const std::string &Name) override {
44     if (uint64_t Addr = ExternalLookup(Name))
45       return Addr;
46     return BaseRTDyldMM::getSymbolAddress(Name);
47   }
48
49   uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) override {
50     if (uint64_t Addr = DylibLookup(Name))
51       return Addr;
52     return BaseRTDyldMM::getSymbolAddressInLogicalDylib(Name);
53   };
54
55   /// @brief Get a reference to the ExternalLookup functor.
56   ExternalLookupFtor &getExternalLookup() { return ExternalLookup; }
57
58   /// @brief Get a const-reference to the ExternalLookup functor.
59   const ExternalLookupFtor &getExternalLookup() const { return ExternalLookup; }
60
61   /// @brief Get a reference to the DylibLookup functor.
62   DylibLookupFtor &getDylibLookup() { return DylibLookup; }
63
64   /// @brief Get a const-reference to the DylibLookup functor.
65   const DylibLookupFtor &getDylibLookup() const { return DylibLookup; }
66
67 private:
68   ExternalLookupFtor ExternalLookup;
69   DylibLookupFtor DylibLookup;
70 };
71
72 /// @brief Create a LookasideRTDyldMM from a base memory manager type, an
73 ///        external lookup functor, and a dylib lookup functor.
74 template <typename BaseRTDyldMM, typename ExternalLookupFtor,
75           typename DylibLookupFtor>
76 std::unique_ptr<
77     LookasideRTDyldMM<BaseRTDyldMM, ExternalLookupFtor, DylibLookupFtor>>
78 createLookasideRTDyldMM(ExternalLookupFtor &&ExternalLookup,
79                         DylibLookupFtor &&DylibLookup) {
80   typedef LookasideRTDyldMM<BaseRTDyldMM, ExternalLookupFtor, DylibLookupFtor>
81       ThisLookasideMM;
82   return llvm::make_unique<ThisLookasideMM>(
83       std::forward<ExternalLookupFtor>(ExternalLookup),
84       std::forward<DylibLookupFtor>(DylibLookup));
85 }
86 }
87
88 #endif // LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H