35 explicit InitialImage(std::size_t bytes) : data_(bytes) {}
36 InitialImage(InitialImage &&that) =
default;
38 std::size_t size()
const {
return data_.size(); }
41 Result Add(ConstantSubscript, std::size_t,
const A &,
FoldingContext &) {
45 Result Add(ConstantSubscript offset, std::size_t bytes,
const Constant<T> &x,
47 if (offset < 0 || offset + bytes > data_.size()) {
50 auto elementBytes{ToInt64(x.GetType().MeasureSizeInBytes(context,
true))};
53 x.values().size() *
static_cast<std::size_t
>(*elementBytes)) {
55 }
else if (bytes == 0) {
59 auto *to{&data_.at(offset)};
60 const auto *from{&x.values().at(0)};
61 if (std::memcmp(to, from, bytes) == 0) {
64 std::memcpy(to, from, bytes);
71 Result Add(ConstantSubscript offset, std::size_t bytes,
74 if (offset < 0 || offset + bytes > data_.size()) {
77 auto optElements{TotalElementCount(x.shape())};
81 auto elements{*optElements};
82 auto elementBytes{bytes > 0 ? bytes / elements : 0};
83 if (elements * elementBytes != bytes) {
85 }
else if (bytes == 0) {
88 Result result{OkNoChange};
89 for (
auto at{x.lbounds()}; elements-- > 0; x.IncrementSubscripts(at)) {
90 auto scalar{x.At(at)};
91 auto scalarBytes{scalar.size() * KIND};
92 if (scalarBytes != elementBytes) {
93 result = LengthMismatch;
96 for (; scalarBytes < elementBytes; scalarBytes += KIND) {
100 auto *to{&data_.at(offset)};
101 const auto *from{scalar.data()};
102 if (std::memcmp(to, from, elementBytes) != 0) {
103 std::memcpy(to, from, elementBytes);
104 if (result == OkNoChange) {
108 offset += elementBytes;
116 template <
typename T>
117 Result Add(ConstantSubscript offset, std::size_t bytes,
const Expr<T> &x,
119 return common::visit(
120 [&](
const auto &y) {
return Add(offset, bytes, y, c); }, x.u);
126 bool Incorporate(ConstantSubscript toOffset,
const InitialImage &from,
127 ConstantSubscript fromOffset, ConstantSubscript bytes);
131 const DynamicType &, std::optional<std::int64_t> charLength,
132 const ConstantSubscripts &,
bool padWithZero =
false,
133 ConstantSubscript offset = 0)
const;
134 std::optional<Expr<SomeType>> AsConstantPointer(
135 ConstantSubscript offset = 0)
const;
137 friend class AsConstantHelper;
140 std::vector<char> data_;
141 std::map<ConstantSubscript, Expr<SomeType>> pointers_;