1 //===- LambdaResolverMM - Redirect symbol lookup via a functor --*- 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 //   Defines a RuntimeDyld::SymbolResolver subclass that uses a user-supplied
10 // functor for symbol resolution.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
15 #define LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
19 #include "llvm/ExecutionEngine/OrcV1Deprecation.h"
20 #include <memory>
21 
22 namespace llvm {
23 namespace orc {
24 
25 template <typename DylibLookupFtorT, typename ExternalLookupFtorT>
26 class LambdaResolver : public LegacyJITSymbolResolver {
27 public:
28   LLVM_ATTRIBUTE_DEPRECATED(
29       LambdaResolver(DylibLookupFtorT DylibLookupFtor,
30                      ExternalLookupFtorT ExternalLookupFtor),
31       "ORCv1 utilities (including resolvers) are deprecated and will be "
32       "removed "
33       "in the next release. Please use ORCv2 (see docs/ORCv2.rst)");
34 
LambdaResolver(ORCv1DeprecationAcknowledgement,DylibLookupFtorT DylibLookupFtor,ExternalLookupFtorT ExternalLookupFtor)35   LambdaResolver(ORCv1DeprecationAcknowledgement,
36                  DylibLookupFtorT DylibLookupFtor,
37                  ExternalLookupFtorT ExternalLookupFtor)
38       : DylibLookupFtor(DylibLookupFtor),
39         ExternalLookupFtor(ExternalLookupFtor) {}
40 
findSymbolInLogicalDylib(const std::string & Name)41   JITSymbol findSymbolInLogicalDylib(const std::string &Name) final {
42     return DylibLookupFtor(Name);
43   }
44 
findSymbol(const std::string & Name)45   JITSymbol findSymbol(const std::string &Name) final {
46     return ExternalLookupFtor(Name);
47   }
48 
49 private:
50   DylibLookupFtorT DylibLookupFtor;
51   ExternalLookupFtorT ExternalLookupFtor;
52 };
53 
54 template <typename DylibLookupFtorT, typename ExternalLookupFtorT>
LambdaResolver(DylibLookupFtorT DylibLookupFtor,ExternalLookupFtorT ExternalLookupFtor)55 LambdaResolver<DylibLookupFtorT, ExternalLookupFtorT>::LambdaResolver(
56     DylibLookupFtorT DylibLookupFtor, ExternalLookupFtorT ExternalLookupFtor)
57     : DylibLookupFtor(DylibLookupFtor), ExternalLookupFtor(ExternalLookupFtor) {
58 }
59 
60 template <typename DylibLookupFtorT,
61           typename ExternalLookupFtorT>
62 std::shared_ptr<LambdaResolver<DylibLookupFtorT, ExternalLookupFtorT>>
createLambdaResolver(DylibLookupFtorT DylibLookupFtor,ExternalLookupFtorT ExternalLookupFtor)63 createLambdaResolver(DylibLookupFtorT DylibLookupFtor,
64                      ExternalLookupFtorT ExternalLookupFtor) {
65   using LR = LambdaResolver<DylibLookupFtorT, ExternalLookupFtorT>;
66   return std::make_unique<LR>(std::move(DylibLookupFtor),
67                          std::move(ExternalLookupFtor));
68 }
69 
70 template <typename DylibLookupFtorT, typename ExternalLookupFtorT>
71 std::shared_ptr<LambdaResolver<DylibLookupFtorT, ExternalLookupFtorT>>
createLambdaResolver(ORCv1DeprecationAcknowledgement,DylibLookupFtorT DylibLookupFtor,ExternalLookupFtorT ExternalLookupFtor)72 createLambdaResolver(ORCv1DeprecationAcknowledgement,
73                      DylibLookupFtorT DylibLookupFtor,
74                      ExternalLookupFtorT ExternalLookupFtor) {
75   using LR = LambdaResolver<DylibLookupFtorT, ExternalLookupFtorT>;
76   return std::make_unique<LR>(AcknowledgeORCv1Deprecation,
77                          std::move(DylibLookupFtor),
78                          std::move(ExternalLookupFtor));
79 }
80 
81 } // end namespace orc
82 } // end namespace llvm
83 
84 #endif // LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H
85