f58c089acb44be061a0966cf69ca4f595a4d78ff
[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 #include "llvm/ADT/iterator.h"
18 #include "llvm/ADT/Optional.h"
19
20 namespace llvm {
21 namespace orc {
22
23 template <typename BaseLayerT,
24           typename LogicalModuleResources,
25           typename LogicalDylibResources>
26 class LogicalDylib {
27 public:
28   typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;
29 private:
30
31   typedef std::vector<BaseLayerModuleSetHandleT> BaseLayerHandleList;
32
33   struct LogicalModule {
34     LogicalModuleResources Resources;
35     BaseLayerHandleList BaseLayerHandles;
36   };
37   typedef std::vector<LogicalModule> LogicalModuleList;
38
39 public:
40
41   typedef typename BaseLayerHandleList::iterator BaseLayerHandleIterator;
42   typedef typename LogicalModuleList::iterator LogicalModuleHandle;
43
44   LogicalDylib(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
45
46   ~LogicalDylib() {
47     for (auto &LM : LogicalModules)
48       for (auto BLH : LM.BaseLayerHandles)
49         BaseLayer.removeModuleSet(BLH);
50   }
51
52   LogicalModuleHandle createLogicalModule() {
53     LogicalModules.push_back(LogicalModule());
54     return std::prev(LogicalModules.end());
55   }
56
57   void addToLogicalModule(LogicalModuleHandle LMH,
58                           BaseLayerModuleSetHandleT BaseLayerHandle) {
59     LMH->BaseLayerHandles.push_back(BaseLayerHandle);
60   }
61
62   LogicalModuleResources& getLogicalModuleResources(LogicalModuleHandle LMH) {
63     return LMH->Resources;
64   }
65
66   BaseLayerHandleIterator moduleHandlesBegin(LogicalModuleHandle LMH) {
67     return LMH->BaseLayerHandles.begin();
68   }
69
70   BaseLayerHandleIterator moduleHandlesEnd(LogicalModuleHandle LMH) {
71     return LMH->BaseLayerHandles.end();
72   }
73
74   JITSymbol findSymbolInLogicalModule(LogicalModuleHandle LMH,
75                                       const std::string &Name) {
76     for (auto BLH : LMH->BaseLayerHandles)
77       if (auto Symbol = BaseLayer.findSymbolIn(BLH, Name, false))
78         return Symbol;
79     return nullptr;
80   }
81
82   JITSymbol findSymbolInternally(LogicalModuleHandle LMH,
83                                  const std::string &Name) {
84     if (auto Symbol = findSymbolInLogicalModule(LMH, Name))
85       return Symbol;
86
87     for (auto LMI = LogicalModules.begin(), LME = LogicalModules.end();
88            LMI != LME; ++LMI) {
89       if (LMI != LMH)
90         if (auto Symbol = findSymbolInLogicalModule(LMI, Name))
91           return Symbol;
92     }
93
94     return nullptr;
95   }
96
97   JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
98     for (auto &LM : LogicalModules)
99       for (auto BLH : LM.BaseLayerHandles)
100         if (auto Symbol =
101             BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
102           return Symbol;
103     return nullptr;
104   }
105
106   LogicalDylibResources& getDylibResources() { return DylibResources; }
107
108 protected:
109   BaseLayerT BaseLayer;
110   LogicalModuleList LogicalModules;
111   LogicalDylibResources DylibResources;
112
113 };
114
115 } // End namespace orc.
116 } // End namespace llvm.
117
118 #endif // LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H