aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/TargetFunctionState.cpp
blob: 56ff3635614c4069b5161ea0693285f41fe47309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/TargetFunctionState.h"

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>

namespace rcc {

TargetFunctionState::TargetFunctionState(TargetFunctionState &&) noexcept = default;
TargetFunctionState &TargetFunctionState::operator=(TargetFunctionState &&) noexcept = default;
TargetFunctionState::~TargetFunctionState() = default;

std::optional<TargetFunctionStateTypeID> TargetFunctionState::typeID() const noexcept {
  if (Storage == nullptr)
    return std::nullopt;
  return Storage->typeID();
}

std::optional<std::uint32_t> TargetFunctionState::schemaVersion() const noexcept {
  if (Storage == nullptr)
    return std::nullopt;
  return Storage->schemaVersion();
}

Expected<void> TargetFunctionState::verify() const {
  if (Storage == nullptr)
    return makeUnexpected(ErrorCode::VerificationError, "target function state is empty");
  if (Storage->typeID().value() == 0 || Storage->schemaVersion() == 0) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "target function state identity or schema version is invalid");
  }
  return Storage->verify();
}

Expected<TargetFunctionState>
TargetFunctionState::clone(const MachineFunctionCloneContext &CloneContext) const {
  auto Verification = verify();
  if (!Verification)
    return std::unexpected(Verification.error());
  auto ClonedStorage = Storage->cloneStorage(CloneContext);
  if (!ClonedStorage)
    return std::unexpected(ClonedStorage.error());
  TargetFunctionState Result(std::move(*ClonedStorage));
  auto CloneVerification = Result.verify();
  if (!CloneVerification)
    return std::unexpected(CloneVerification.error());
  return Result;
}

Expected<std::string> TargetFunctionState::print() const {
  auto Verification = verify();
  if (!Verification)
    return std::unexpected(Verification.error());
  return Storage->print();
}

Expected<EncodedTargetFunctionState> TargetFunctionState::encode() const {
  auto Verification = verify();
  if (!Verification)
    return std::unexpected(Verification.error());
  auto Payload = Storage->encodePayload();
  if (!Payload)
    return std::unexpected(Payload.error());
  return EncodedTargetFunctionState{Storage->typeID(), Storage->schemaVersion(),
                                    std::move(*Payload)};
}

bool TargetFunctionState::equals(const TargetFunctionState &Other) const noexcept {
  if (Storage == nullptr || Other.Storage == nullptr)
    return Storage == nullptr && Other.Storage == nullptr;
  if (Storage->typeID() != Other.Storage->typeID() ||
      Storage->localTypeToken() != Other.Storage->localTypeToken()) {
    return false;
  }
  return Storage->equals(*Other.Storage);
}

} // namespace rcc