1 //===-- LambdaResolverMM - 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 a RuntimeDyld::SymbolResolver subclass that uses a user-supplied
11 // functor for symbol resolution.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
16 #define LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
17 
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #include <memory>
21 #include <vector>
22 
23 namespace llvm {
24 namespace orc {
25 
26 template <typename ExternalLookupFtorT, typename DylibLookupFtorT>
27 class LambdaResolver : public RuntimeDyld::SymbolResolver {
28 public:
29 
LambdaResolver(ExternalLookupFtorT ExternalLookupFtor,DylibLookupFtorT DylibLookupFtor)30   LambdaResolver(ExternalLookupFtorT ExternalLookupFtor,
31                  DylibLookupFtorT DylibLookupFtor)
32       : ExternalLookupFtor(ExternalLookupFtor),
33         DylibLookupFtor(DylibLookupFtor) {}
34 
findSymbol(const std::string & Name)35   RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) final {
36     return ExternalLookupFtor(Name);
37   }
38 
39   RuntimeDyld::SymbolInfo
findSymbolInLogicalDylib(const std::string & Name)40   findSymbolInLogicalDylib(const std::string &Name) final {
41     return DylibLookupFtor(Name);
42   }
43 
44 private:
45   ExternalLookupFtorT ExternalLookupFtor;
46   DylibLookupFtorT DylibLookupFtor;
47 };
48 
49 template <typename ExternalLookupFtorT,
50           typename DylibLookupFtorT>
51 std::unique_ptr<LambdaResolver<ExternalLookupFtorT, DylibLookupFtorT>>
createLambdaResolver(ExternalLookupFtorT ExternalLookupFtor,DylibLookupFtorT DylibLookupFtor)52 createLambdaResolver(ExternalLookupFtorT ExternalLookupFtor,
53                      DylibLookupFtorT DylibLookupFtor) {
54   typedef LambdaResolver<ExternalLookupFtorT, DylibLookupFtorT> LR;
55   return make_unique<LR>(std::move(ExternalLookupFtor),
56                          std::move(DylibLookupFtor));
57 }
58 
59 } // End namespace orc.
60 } // End namespace llvm.
61 
62 #endif // LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
63