4be2e1967072382806606dd69b762faf12780dba
[oota-llvm.git] / unittests / ExecutionEngine / Orc / OrcTestCommon.h
1 //===------ OrcTestCommon.h - Utilities for Orc Unit Tests ------*- 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 // Common utilities for the Orc unit tests.
11 //
12 //===----------------------------------------------------------------------===//
13
14
15 #ifndef LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
16 #define LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
17
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/TypeBuilder.h"
23 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
24 #include <memory>
25
26 namespace llvm {
27
28 class ModuleBuilder {
29 public:
30   ModuleBuilder(LLVMContext &Context, StringRef Triple,
31                 StringRef Name);
32
33   template <typename FuncType>
34   Function* createFunctionDecl(Module *M, StringRef Name) {
35     return Function::Create(
36              TypeBuilder<FuncType, false>::get(M->getContext()),
37              GlobalValue::ExternalLinkage, Name, M);
38   }
39
40   Module* getModule() { return M.get(); }
41   const Module* getModule() const { return M.get(); }
42   std::unique_ptr<Module> takeModule() { return std::move(M); }
43
44 private:
45   std::unique_ptr<Module> M;
46   IRBuilder<> Builder;
47 };
48
49 // Dummy struct type.
50 struct DummyStruct {
51   int X[256];
52 };
53
54 // TypeBuilder specialization for DummyStruct.
55 template <bool XCompile>
56 class TypeBuilder<DummyStruct, XCompile> {
57 public:
58   static StructType *get(LLVMContext &Context) {
59     return StructType::get(
60       TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
61   }
62 };
63
64 template <typename HandleT,
65           typename AddModuleSetFtor,
66           typename RemoveModuleSetFtor,
67           typename FindSymbolFtor,
68           typename FindSymbolInFtor>
69 class MockBaseLayer {
70 public:
71
72   typedef HandleT ModuleSetHandleT;
73
74   MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
75                 RemoveModuleSetFtor &&RemoveModuleSet,
76                 FindSymbolFtor &&FindSymbol,
77                 FindSymbolInFtor &&FindSymbolIn)
78       : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
79         FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
80   {}
81
82   template <typename ModuleSetT, typename MemoryManagerPtrT,
83             typename SymbolResolverPtrT>
84   ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
85                                 SymbolResolverPtrT Resolver) {
86     return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
87   }
88
89   void removeModuleSet(ModuleSetHandleT H) {
90     RemoveModuleSet(H);
91   }
92
93   orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
94     return FindSymbol(Name, ExportedSymbolsOnly);
95   }
96
97   orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
98                          bool ExportedSymbolsOnly) {
99     return FindSymbolIn(H, Name, ExportedSymbolsOnly);
100   }
101
102 private:
103   AddModuleSetFtor AddModuleSet;
104   RemoveModuleSetFtor RemoveModuleSet;
105   FindSymbolFtor FindSymbol;
106   FindSymbolInFtor FindSymbolIn;
107 };
108
109 template <typename ModuleSetHandleT,
110           typename AddModuleSetFtor,
111           typename RemoveModuleSetFtor,
112           typename FindSymbolFtor,
113           typename FindSymbolInFtor>
114 MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
115               FindSymbolFtor, FindSymbolInFtor>
116 createMockBaseLayer(AddModuleSetFtor &&AddModuleSet,
117                     RemoveModuleSetFtor &&RemoveModuleSet,
118                     FindSymbolFtor &&FindSymbol,
119                     FindSymbolInFtor &&FindSymbolIn) {
120   return MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
121                        FindSymbolFtor, FindSymbolInFtor>(
122                          std::forward<AddModuleSetFtor>(AddModuleSet),
123                          std::forward<RemoveModuleSetFtor>(RemoveModuleSet),
124                          std::forward<FindSymbolFtor>(FindSymbol),
125                          std::forward<FindSymbolInFtor>(FindSymbolIn));
126 }
127
128 template <typename ReturnT>
129 class DoNothingAndReturn {
130 public:
131   DoNothingAndReturn(ReturnT Val) : Val(Val) {}
132
133   template <typename... Args>
134   ReturnT operator()(Args...) const { return Val; }
135 private:
136   ReturnT Val;
137 };
138
139 template <>
140 class DoNothingAndReturn<void> {
141 public:
142   template <typename... Args>
143   void operator()(Args...) const { }
144 };
145
146 } // namespace llvm
147
148 #endif