1//===---------------- OrcError.cpp - Error codes for ORC ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Error codes for ORC.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ExecutionEngine/Orc/OrcError.h"
14#include "llvm/Support/ErrorHandling.h"
15#include "llvm/Support/ManagedStatic.h"
16
17#include <type_traits>
18
19using namespace llvm;
20using namespace llvm::orc;
21
22namespace {
23
24// FIXME: This class is only here to support the transition to llvm::Error. It
25// will be removed once this transition is complete. Clients should prefer to
26// deal with the Error value directly, rather than converting to error_code.
27class OrcErrorCategory : public std::error_category {
28public:
29  const char *name() const noexcept override { return "orc"; }
30
31  std::string message(int condition) const override {
32    switch (static_cast<OrcErrorCode>(condition)) {
33    case OrcErrorCode::UnknownORCError:
34      return "Unknown ORC error";
35    case OrcErrorCode::DuplicateDefinition:
36      return "Duplicate symbol definition";
37    case OrcErrorCode::JITSymbolNotFound:
38      return "JIT symbol not found";
39    case OrcErrorCode::RemoteAllocatorDoesNotExist:
40      return "Remote allocator does not exist";
41    case OrcErrorCode::RemoteAllocatorIdAlreadyInUse:
42      return "Remote allocator Id already in use";
43    case OrcErrorCode::RemoteMProtectAddrUnrecognized:
44      return "Remote mprotect call references unallocated memory";
45    case OrcErrorCode::RemoteIndirectStubsOwnerDoesNotExist:
46      return "Remote indirect stubs owner does not exist";
47    case OrcErrorCode::RemoteIndirectStubsOwnerIdAlreadyInUse:
48      return "Remote indirect stubs owner Id already in use";
49    case OrcErrorCode::RPCConnectionClosed:
50      return "RPC connection closed";
51    case OrcErrorCode::RPCCouldNotNegotiateFunction:
52      return "Could not negotiate RPC function";
53    case OrcErrorCode::RPCResponseAbandoned:
54      return "RPC response abandoned";
55    case OrcErrorCode::UnexpectedRPCCall:
56      return "Unexpected RPC call";
57    case OrcErrorCode::UnexpectedRPCResponse:
58      return "Unexpected RPC response";
59    case OrcErrorCode::UnknownErrorCodeFromRemote:
60      return "Unknown error returned from remote RPC function "
61             "(Use StringError to get error message)";
62    case OrcErrorCode::UnknownResourceHandle:
63      return "Unknown resource handle";
64    case OrcErrorCode::MissingSymbolDefinitions:
65      return "MissingSymbolsDefinitions";
66    case OrcErrorCode::UnexpectedSymbolDefinitions:
67      return "UnexpectedSymbolDefinitions";
68    }
69    llvm_unreachable("Unhandled error code");
70  }
71};
72
73static ManagedStatic<OrcErrorCategory> OrcErrCat;
74}
75
76namespace llvm {
77namespace orc {
78
79char DuplicateDefinition::ID = 0;
80char JITSymbolNotFound::ID = 0;
81
82std::error_code orcError(OrcErrorCode ErrCode) {
83  typedef std::underlying_type<OrcErrorCode>::type UT;
84  return std::error_code(static_cast<UT>(ErrCode), *OrcErrCat);
85}
86
87
88DuplicateDefinition::DuplicateDefinition(std::string SymbolName)
89  : SymbolName(std::move(SymbolName)) {}
90
91std::error_code DuplicateDefinition::convertToErrorCode() const {
92  return orcError(OrcErrorCode::DuplicateDefinition);
93}
94
95void DuplicateDefinition::log(raw_ostream &OS) const {
96  OS << "Duplicate definition of symbol '" << SymbolName << "'";
97}
98
99const std::string &DuplicateDefinition::getSymbolName() const {
100  return SymbolName;
101}
102
103JITSymbolNotFound::JITSymbolNotFound(std::string SymbolName)
104  : SymbolName(std::move(SymbolName)) {}
105
106std::error_code JITSymbolNotFound::convertToErrorCode() const {
107  typedef std::underlying_type<OrcErrorCode>::type UT;
108  return std::error_code(static_cast<UT>(OrcErrorCode::JITSymbolNotFound),
109                         *OrcErrCat);
110}
111
112void JITSymbolNotFound::log(raw_ostream &OS) const {
113  OS << "Could not find symbol '" << SymbolName << "'";
114}
115
116const std::string &JITSymbolNotFound::getSymbolName() const {
117  return SymbolName;
118}
119
120}
121}
122