Fix some formatting from a recent commit.
[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 &&) = default;
34     LogicalModule() = default;
35     LogicalModuleResources Resources;
36     BaseLayerHandleList BaseLayerHandles;
37   };
38   typedef std::vector<LogicalModule> LogicalModuleList;
39
40 public:
41
42   typedef typename BaseLayerHandleList::iterator BaseLayerHandleIterator;
43   typedef typename LogicalModuleList::iterator LogicalModuleHandle;
44
45   LogicalDylib(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
46
47   ~LogicalDylib() {
48     for (auto &LM : LogicalModules)
49       for (auto BLH : LM.BaseLayerHandles)
50         BaseLayer.removeModuleSet(BLH);
51   }
52
53   // If possible, remove this and ~LogicalDylib once the work in the dtor is
54   // moved to members (eg: self-unregistering base layer handles).
55   LogicalDylib(LogicalDylib &&RHS) = default;
56
57   LogicalModuleHandle createLogicalModule() {
58     LogicalModules.push_back(LogicalModule());
59     return std::prev(LogicalModules.end());
60   }
61
62   void addToLogicalModule(LogicalModuleHandle LMH,
63                           BaseLayerModuleSetHandleT BaseLayerHandle) {
64     LMH->BaseLayerHandles.push_back(BaseLayerHandle);
65   }
66
67   LogicalModuleResources& getLogicalModuleResources(LogicalModuleHandle LMH) {
68     return LMH->Resources;
69   }
70
71   BaseLayerHandleIterator moduleHandlesBegin(LogicalModuleHandle LMH) {
72     return LMH->BaseLayerHandles.begin();
73   }
74
75   BaseLayerHandleIterator moduleHandlesEnd(LogicalModuleHandle LMH) {
76     return LMH->BaseLayerHandles.end();
77   }
78
79   JITSymbol findSymbolInLogicalModule(LogicalModuleHandle LMH,
80                                       const std::string &Name) {
81     for (auto BLH : LMH->BaseLayerHandles)
82       if (auto Symbol = BaseLayer.findSymbolIn(BLH, Name, false))
83         return Symbol;
84     return nullptr;
85   }
86
87   JITSymbol findSymbolInternally(LogicalModuleHandle LMH,
88                                  const std::string &Name) {
89     if (auto Symbol = findSymbolInLogicalModule(LMH, Name))
90       return Symbol;
91
92     for (auto LMI = LogicalModules.begin(), LME = LogicalModules.end();
93            LMI != LME; ++LMI) {
94       if (LMI != LMH)
95         if (auto Symbol = findSymbolInLogicalModule(LMI, Name))
96           return Symbol;
97     }
98
99     return nullptr;
100   }
101
102   JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
103     for (auto &LM : LogicalModules)
104       for (auto BLH : LM.BaseLayerHandles)
105         if (auto Symbol =
106             BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
107           return Symbol;
108     return nullptr;
109   }
110
111   LogicalDylibResources& getDylibResources() { return DylibResources; }
112
113 protected:
114   BaseLayerT BaseLayer;
115   LogicalModuleList LogicalModules;
116   LogicalDylibResources DylibResources;
117
118 };
119
120 } // End namespace orc.
121 } // End namespace llvm.
122
123 #endif // LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H