1 //===------ OrcTestCommon.h - Utilities for Orc Unit Tests ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common utilities for the Orc unit tests.
12 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
16 #define LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
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"
30 ModuleBuilder(LLVMContext &Context, StringRef Triple,
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);
40 Module* getModule() { return M.get(); }
41 const Module* getModule() const { return M.get(); }
42 std::unique_ptr<Module> takeModule() { return std::move(M); }
45 std::unique_ptr<Module> M;
54 // TypeBuilder specialization for DummyStruct.
55 template <bool XCompile>
56 class TypeBuilder<DummyStruct, XCompile> {
58 static StructType *get(LLVMContext &Context) {
59 return StructType::get(
60 TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
64 template <typename HandleT,
65 typename AddModuleSetFtor,
66 typename RemoveModuleSetFtor,
67 typename FindSymbolFtor,
68 typename FindSymbolInFtor>
72 typedef HandleT ModuleSetHandleT;
74 MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
75 RemoveModuleSetFtor &&RemoveModuleSet,
76 FindSymbolFtor &&FindSymbol,
77 FindSymbolInFtor &&FindSymbolIn)
78 : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
79 FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
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));
89 void removeModuleSet(ModuleSetHandleT H) {
93 orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
94 return FindSymbol(Name, ExportedSymbolsOnly);
97 orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
98 bool ExportedSymbolsOnly) {
99 return FindSymbolIn(H, Name, ExportedSymbolsOnly);
103 AddModuleSetFtor AddModuleSet;
104 RemoveModuleSetFtor RemoveModuleSet;
105 FindSymbolFtor FindSymbol;
106 FindSymbolInFtor FindSymbolIn;
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));
128 template <typename ReturnT>
129 class DoNothingAndReturn {
131 DoNothingAndReturn(ReturnT Val) : Val(Val) {}
133 template <typename... Args>
134 ReturnT operator()(Args...) const { return Val; }
140 class DoNothingAndReturn<void> {
142 template <typename... Args>
143 void operator()(Args...) const { }