blob: 99bae55815ddd757b3386680af36602003a0fd9f [file] [log] [blame]
Lang Hamesdc4260d2015-04-20 20:41:45 +00001//===------ 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"
Lang Hamesc0056562015-10-20 04:35:02 +000023#include "llvm/ExecutionEngine/Orc/JITSymbol.h"
Lang Hamesdc4260d2015-04-20 20:41:45 +000024#include <memory>
25
26namespace 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) {
Reid Klecknerab9d97c2015-04-21 15:56:21 +000059 return StructType::get(
60 TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
Lang Hamesdc4260d2015-04-20 20:41:45 +000061 }
62 };
63
Lang Hamesc0056562015-10-20 04:35:02 +000064template <typename HandleT,
65 typename AddModuleSetFtor,
66 typename RemoveModuleSetFtor,
67 typename FindSymbolFtor,
68 typename FindSymbolInFtor>
69class MockBaseLayer {
70public:
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
102private:
103 AddModuleSetFtor AddModuleSet;
104 RemoveModuleSetFtor RemoveModuleSet;
105 FindSymbolFtor FindSymbol;
106 FindSymbolInFtor FindSymbolIn;
107};
108
109template <typename ModuleSetHandleT,
110 typename AddModuleSetFtor,
111 typename RemoveModuleSetFtor,
112 typename FindSymbolFtor,
113 typename FindSymbolInFtor>
114MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
115 FindSymbolFtor, FindSymbolInFtor>
116createMockBaseLayer(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
128template <typename ReturnT>
129class DoNothingAndReturn {
130public:
131 DoNothingAndReturn(ReturnT Val) : Val(Val) {}
132
133 template <typename... Args>
134 ReturnT operator()(Args...) const { return Val; }
135private:
136 ReturnT Val;
137};
138
139template <>
140class DoNothingAndReturn<void> {
141public:
142 template <typename... Args>
143 void operator()(Args...) const { }
144};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000145
146} // namespace llvm
147
148#endif