Fix an MSVC build break since it can't synthesize move ctors.
[oota-llvm.git] / include / llvm / ExecutionEngine / Orc / LogicalDylib.h
1 //===--- LogicalDylib.h - Simulates dylib-style symbol lookup ---*- 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 // Simulates symbol resolution inside a dylib.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
15 #define LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H
16
17 namespace llvm {
18 namespace orc {
19
20 template <typename BaseLayerT,
21           typename LogicalModuleResources,
22           typename LogicalDylibResources>
23 class LogicalDylib {
24 public:
25   typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;
26 private:
27
28   typedef std::vector<BaseLayerModuleSetHandleT> BaseLayerHandleList;
29
30   struct LogicalModule {
31     // Make this move-only to ensure they don't get duplicated across moves of
32     // LogicalDylib or anything like that.
33     LogicalModule(LogicalModule &&RHS)
34         : Resources(std::move(RHS.Resources)),
35           BaseLayerHandles(std::move(RHS.BaseLayerHandles)) {}
36     LogicalModule() = default;
37     LogicalModuleResources Resources;
38     BaseLayerHandleList BaseLayerHandles;
39   };
40   typedef std::vector<LogicalModule> LogicalModuleList;
41
42 public:
43
44   typedef typename BaseLayerHandleList::iterator BaseLayerHandleIterator;
45   typedef typename LogicalModuleList::iterator LogicalModuleHandle;
46
47   LogicalDylib(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
48
49   ~LogicalDylib() {
50     for (auto &LM : LogicalModules)
51       for (auto BLH : LM.BaseLayerHandles)
52         BaseLayer.removeModuleSet(BLH);
53   }
54
55   // If possible, remove this and ~LogicalDylib once the work in the dtor is
56   // moved to members (eg: self-unregistering base layer handles).
57   LogicalDylib(LogicalDylib &&RHS) = default;
58
59   LogicalModuleHandle createLogicalModule() {
60     LogicalModules.push_back(LogicalModule());
61     return std::prev(LogicalModules.end());
62   }
63
64   void addToLogicalModule(LogicalModuleHandle LMH,
65                           BaseLayerModuleSetHandleT BaseLayerHandle) {
66     LMH->BaseLayerHandles.push_back(BaseLayerHandle);
67   }
68
69   LogicalModuleResources& getLogicalModuleResources(LogicalModuleHandle LMH) {
70     return LMH->Resources;
71   }
72
73   BaseLayerHandleIterator moduleHandlesBegin(LogicalModuleHandle LMH) {
74     return LMH->BaseLayerHandles.begin();
75   }
76
77   BaseLayerHandleIterator moduleHandlesEnd(LogicalModuleHandle LMH) {
78     return LMH->BaseLayerHandles.end();
79   }
80
81   JITSymbol findSymbolInLogicalModule(LogicalModuleHandle LMH,
82                                       const std::string &Name) {
83     for (auto BLH : LMH->BaseLayerHandles)
84       if (auto Symbol = BaseLayer.findSymbolIn(BLH, Name, false))
85         return Symbol;
86     return nullptr;
87   }
88
89   JITSymbol findSymbolInternally(LogicalModuleHandle LMH,
90                                  const std::string &Name) {
91     if (auto Symbol = findSymbolInLogicalModule(LMH, Name))
92       return Symbol;
93
94     for (auto LMI = LogicalModules.begin(), LME = LogicalModules.end();
95            LMI != LME; ++LMI) {
96       if (LMI != LMH)
97         if (auto Symbol = findSymbolInLogicalModule(LMI, Name))
98           return Symbol;
99     }
100
101     return nullptr;
102   }
103
104   JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
105     for (auto &LM : LogicalModules)
106       for (auto BLH : LM.BaseLayerHandles)
107         if (auto Symbol =
108             BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
109           return Symbol;
110     return nullptr;
111   }
112
113   LogicalDylibResources& getDylibResources() { return DylibResources; }
114
115 protected:
116   BaseLayerT BaseLayer;
117   LogicalModuleList LogicalModules;
118   LogicalDylibResources DylibResources;
119
120 };
121
122 } // End namespace orc.
123 } // End namespace llvm.
124
125 #endif // LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H