FLANG
initial-image.h
1//===-------include/flang/Evaluate/initial-image.h ------------------------===//
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#ifndef FORTRAN_EVALUATE_INITIAL_IMAGE_H_
10#define FORTRAN_EVALUATE_INITIAL_IMAGE_H_
11
12// Represents the initialized storage of an object during DATA statement
13// processing, including the conversion of that image to a constant
14// initializer for a symbol.
15
16#include "expression.h"
17#include <map>
18#include <optional>
19#include <vector>
20
21namespace Fortran::evaluate {
22
23template <typename SCALAR>
24inline void StoreSerialValues(char *dst, llvm::ArrayRef<SCALAR> values,
25 size_t elementSize, bool *changed = nullptr) {
26 for (auto [i, v] : llvm::enumerate(values)) {
27 v.StoreRawBytes(dst + i * elementSize, elementSize, changed);
28 }
29}
30
31template <typename SCALAR>
32inline void LoadSerialValues(int kind, const char *src,
33 llvm::MutableArrayRef<SCALAR> values, size_t stride) {
34 for (auto it : llvm::enumerate(values)) {
35 it.value() = SCALAR::FromRawBytes(
36 kind, src + stride * it.index(), SCALAR::bytesStored(kind));
37 }
38}
39
40class InitialImage {
41public:
42 enum Result {
43 Ok,
44 OkNoChange,
45 NotAConstant,
46 OutOfRange,
47 SizeMismatch,
48 LengthMismatch,
49 TooManyElems,
50 };
51
52 explicit InitialImage(std::size_t bytes) : data_(bytes) {}
53 InitialImage(InitialImage &&that) = default;
54
55 std::size_t size() const { return data_.size(); }
56
57 template <typename A>
58 Result Add(ConstantSubscript, std::size_t, const A &, FoldingContext &) {
59 return NotAConstant;
60 }
61 template <typename T>
62 Result Add(ConstantSubscript offset, std::size_t bytes, const Constant<T> &x,
63 FoldingContext &context) {
64 if (offset < 0 || offset + bytes > data_.size()) {
65 return OutOfRange;
66 } else {
67 auto elementBytes{ToInt64(x.GetType().MeasureSizeInBytes(context, true))};
68 if (!elementBytes ||
69 bytes !=
70 x.values().size() * static_cast<std::size_t>(*elementBytes)) {
71 return SizeMismatch;
72 } else if (bytes == 0) {
73 return OkNoChange;
74 } else {
75 // TODO endianness
76 bool changed{false};
77 StoreSerialValues<Scalar<T>>(&data_.at(offset),
78 llvm::ArrayRef<Scalar<T>>(x.values()), *elementBytes, &changed);
79 return changed ? Ok : OkNoChange;
80 }
81 }
82 }
83 Result Add(ConstantSubscript, std::size_t,
85 Result Add(ConstantSubscript, std::size_t,
87 template <typename T>
88 Result Add(ConstantSubscript offset, std::size_t bytes, const Expr<T> &x,
89 FoldingContext &c) {
90 return common::visit(
91 [&](const auto &y) { return Add(offset, bytes, y, c); }, x.u);
92 }
93
94 Result AddPointer(ConstantSubscript, const Expr<SomeType> &);
95
96 // Returns true if anything changes
97 bool Incorporate(ConstantSubscript toOffset, const InitialImage &from,
98 ConstantSubscript fromOffset, ConstantSubscript bytes);
99
100 // Conversions to constant initializers
101 std::optional<Expr<SomeType>> AsConstant(FoldingContext &,
102 const DynamicType &, std::optional<std::int64_t> charLength,
103 const ConstantSubscripts &, bool padWithZero = false,
104 ConstantSubscript offset = 0) const;
105 std::optional<Expr<SomeType>> AsConstantPointer(
106 ConstantSubscript offset = 0) const;
107
108 friend class AsConstantHelper;
109
110private:
111 std::vector<char> data_;
112 std::map<ConstantSubscript, Expr<SomeType>> pointers_;
113};
114
115} // namespace Fortran::evaluate
116#endif // FORTRAN_EVALUATE_INITIAL_IMAGE_H_
Definition constant.h:181
Definition type.h:101
Definition common.h:222
Definition common.h:224
Definition type.h:61
Definition FIRType.h:106
Definition call.h:34