Fix a -Wpessimizing-move warning.
[oota-llvm.git] / unittests / ExecutionEngine / Orc / OrcTestCommon.h
index 1b2859b51d094e30a1f0fa6d7e6e8c80f4d2484a..875db202a20e69cc01240f36716f9306de268a24 100644 (file)
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/TypeBuilder.h"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "llvm/ExecutionEngine/Orc/JITSymbol.h"
+#include "llvm/Support/TargetSelect.h"
 #include <memory>
 
 namespace llvm {
 
-  class ModuleBuilder {
-  public:
-    ModuleBuilder(LLVMContext &Context, StringRef Triple,
-                  StringRef Name);
+// Base class for Orc tests that will execute code.
+class OrcExecutionTest {
+public:
 
-    template <typename FuncType>
-    Function* createFunctionDecl(Module *M, StringRef Name) {
-      return Function::Create(
-               TypeBuilder<FuncType, false>::get(M->getContext()),
-               GlobalValue::ExternalLinkage, Name, M);
+  OrcExecutionTest() {
+    if (!NativeTargetInitialized) {
+      InitializeNativeTarget();
+      InitializeNativeTargetAsmParser();
+      InitializeNativeTargetAsmPrinter();
+      NativeTargetInitialized = true;
     }
+  };
 
-    Module* getModule() { return M.get(); }
-    const Module* getModule() const { return M.get(); }
-    std::unique_ptr<Module> takeModule() { return std::move(M); }
+  // Get a target machine for the host if it supports JIT execution.
+  std::unique_ptr<TargetMachine> getHostTargetMachineIfSupported() {
+    std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget());
 
-  private:
-    std::unique_ptr<Module> M;
-    IRBuilder<> Builder;
-  };
+    const Triple& TT = TM->getTargetTriple();
 
-  // Dummy struct type.
-  struct DummyStruct {
-    int X[256];
-  };
+    if (TT.getArch() == Triple::x86_64 && TT.isOSDarwin())
+      return TM;
 
-  // TypeBuilder specialization for DummyStruct.
-  template <bool XCompile>
-  class TypeBuilder<DummyStruct, XCompile> {
-  public:
-    static StructType *get(LLVMContext &Context) {
-      return StructType::get(
-          TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
-    }
-  };
+    return nullptr;
+  }
+
+private:
+  static bool NativeTargetInitialized;
+};
+
+class ModuleBuilder {
+public:
+  ModuleBuilder(LLVMContext &Context, StringRef Triple,
+                StringRef Name);
+
+  template <typename FuncType>
+  Function* createFunctionDecl(StringRef Name) {
+    return Function::Create(
+             TypeBuilder<FuncType, false>::get(M->getContext()),
+             GlobalValue::ExternalLinkage, Name, M.get());
+  }
+
+  Module* getModule() { return M.get(); }
+  const Module* getModule() const { return M.get(); }
+  std::unique_ptr<Module> takeModule() { return std::move(M); }
+
+private:
+  std::unique_ptr<Module> M;
+  IRBuilder<> Builder;
+};
+
+// Dummy struct type.
+struct DummyStruct {
+  int X[256];
+};
+
+// TypeBuilder specialization for DummyStruct.
+template <bool XCompile>
+class TypeBuilder<DummyStruct, XCompile> {
+public:
+  static StructType *get(LLVMContext &Context) {
+    return StructType::get(
+      TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
+  }
+};
+
+template <typename HandleT,
+          typename AddModuleSetFtor,
+          typename RemoveModuleSetFtor,
+          typename FindSymbolFtor,
+          typename FindSymbolInFtor>
+class MockBaseLayer {
+public:
+
+  typedef HandleT ModuleSetHandleT;
+
+  MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
+                RemoveModuleSetFtor &&RemoveModuleSet,
+                FindSymbolFtor &&FindSymbol,
+                FindSymbolInFtor &&FindSymbolIn)
+      : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
+        FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
+  {}
+
+  template <typename ModuleSetT, typename MemoryManagerPtrT,
+            typename SymbolResolverPtrT>
+  ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
+                                SymbolResolverPtrT Resolver) {
+    return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
+  }
+
+  void removeModuleSet(ModuleSetHandleT H) {
+    RemoveModuleSet(H);
+  }
+
+  orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
+    return FindSymbol(Name, ExportedSymbolsOnly);
+  }
+
+  orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
+                         bool ExportedSymbolsOnly) {
+    return FindSymbolIn(H, Name, ExportedSymbolsOnly);
+  }
+
+private:
+  AddModuleSetFtor AddModuleSet;
+  RemoveModuleSetFtor RemoveModuleSet;
+  FindSymbolFtor FindSymbol;
+  FindSymbolInFtor FindSymbolIn;
+};
+
+template <typename ModuleSetHandleT,
+          typename AddModuleSetFtor,
+          typename RemoveModuleSetFtor,
+          typename FindSymbolFtor,
+          typename FindSymbolInFtor>
+MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
+              FindSymbolFtor, FindSymbolInFtor>
+createMockBaseLayer(AddModuleSetFtor &&AddModuleSet,
+                    RemoveModuleSetFtor &&RemoveModuleSet,
+                    FindSymbolFtor &&FindSymbol,
+                    FindSymbolInFtor &&FindSymbolIn) {
+  return MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
+                       FindSymbolFtor, FindSymbolInFtor>(
+                         std::forward<AddModuleSetFtor>(AddModuleSet),
+                         std::forward<RemoveModuleSetFtor>(RemoveModuleSet),
+                         std::forward<FindSymbolFtor>(FindSymbol),
+                         std::forward<FindSymbolInFtor>(FindSymbolIn));
+}
+
+template <typename ReturnT>
+class DoNothingAndReturn {
+public:
+  DoNothingAndReturn(ReturnT Val) : Val(Val) {}
+
+  template <typename... Args>
+  ReturnT operator()(Args...) const { return Val; }
+private:
+  ReturnT Val;
+};
 
+template <>
+class DoNothingAndReturn<void> {
+public:
+  template <typename... Args>
+  void operator()(Args...) const { }
+};
 
 } // namespace llvm