62 ConstantBounds() =
default;
63 explicit ConstantBounds(
const ConstantSubscripts &shape);
64 explicit ConstantBounds(ConstantSubscripts &&shape);
66 const ConstantSubscripts &shape()
const {
return shape_; }
67 int Rank()
const {
return GetRank(shape_); }
68 static constexpr int Corank() {
return 0; }
80 const ConstantSubscripts &lbounds()
const {
return lbounds_; }
81 ConstantSubscripts ComputeUbounds(std::optional<int> dim)
const;
82 void set_lbounds(ConstantSubscripts &&);
83 void SetLowerBoundsToOne();
84 bool HasNonDefaultLowerBound()
const;
91 bool IncrementSubscripts(
92 ConstantSubscripts &,
const std::vector<int> *dimOrder =
nullptr)
const;
95 ConstantSubscript SubscriptsToOffset(
const ConstantSubscripts &)
const;
98 ConstantSubscripts shape_;
99 ConstantSubscripts lbounds_;
107class ConstantBase :
public ConstantBounds {
108 static_assert(RESULT::category != TypeCategory::Character);
111 using Result = RESULT;
112 using Element = ELEMENT;
114 constexpr int kind()
const {
return kind_; }
118 template <
typename A>
119 ConstantBase(
int kind,
const A &x, Result res)
120 : kind_{kind}, result_{res}, values_{Element{kind, x}} {
121 CHECK_KIND(kind, RESULT);
122 if constexpr (IsLengthlessIntrinsicType<Result>) {
123 CHECK(res.kind() == kind);
126 ConstantBase(
int kind, ELEMENT &&x)
127 : kind_{kind}, result_{Result{kind}}, values_{std::move(x)} {
128 CHECK_KIND(kind, RESULT);
129 if constexpr (IsLengthlessIntrinsicType<Result>) {
130 CHECK(values_.front().kind() == kind);
134 template <
typename A>
135 ConstantBase(
int kind,
const A &x)
136 : kind_{kind}, result_{Result{kind}}, values_{Element{kind, x}} {
137 CHECK_KIND(kind, RESULT);
139 ConstantBase(
int kind, ELEMENT &&x, Result res)
140 : kind_{kind}, result_{res}, values_{std::move(x)} {
141 CHECK_KIND(kind, RESULT);
142 if constexpr (IsLengthlessIntrinsicType<Result>) {
143 CHECK(res.kind() == kind);
147 ConstantBase(
int kind, std::vector<Element> &&x, ConstantSubscripts &&sh)
148 : ConstantBase{kind, std::move(x), std::move(sh), Result{kind}} {}
150 int kind, std::vector<Element> &&, ConstantSubscripts &&, Result);
151 template <
typename A,
typename B,
typename C>
152 ConstantBase(
int kind,
const std::map<A, B, C> &x, Result res)
153 : kind_{kind}, result_{res}, values_{x} {
154 CHECK_KIND(kind, RESULT);
157 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ConstantBase)
160 bool operator==(
const ConstantBase &)
const;
161 bool empty()
const {
return values_.empty(); }
162 std::size_t size()
const {
return values_.size(); }
163 const std::vector<Element> &values()
const {
return values_; }
164 Result &result() {
return result_; }
165 const Result &result()
const {
return result_; }
167 constexpr DynamicType GetType()
const {
return result_.GetType(); }
168 llvm::raw_ostream &AsFortran(llvm::raw_ostream &)
const;
169 std::string AsFortran()
const;
172 std::vector<Element> Reshape(
const ConstantSubscripts &)
const;
173 std::size_t CopyFrom(
const ConstantBase &source, std::size_t count,
174 ConstantSubscripts &resultSubscripts,
const std::vector<int> *dimOrder);
178 std::vector<Element> values_;
181template <
typename T>
class Constant :
public ConstantBase<T> {
184 using Base = ConstantBase<T>;
185 using Element = Scalar<T>;
190 std::optional<Scalar<T>> GetScalarValue()
const {
191 if (ConstantBounds::Rank() == 0) {
192 return Base::values_.at(0);
200 Element At(
const ConstantSubscripts &)
const;
202 Constant Reshape(ConstantSubscripts &&)
const;
203 std::size_t CopyFrom(
const Constant &source, std::size_t count,
204 ConstantSubscripts &resultSubscripts,
const std::vector<int> *dimOrder);
208class Constant<
Type<TypeCategory::Character>> :
public ConstantBounds {
211 using Element = Scalar<Result>;
213 constexpr int kind()
const {
return kind_; }
215 CLASS_BOILERPLATE(Constant)
216 explicit Constant(
int kind,
const Scalar<Result> &);
217 explicit Constant(
int kind, Scalar<Result> &&);
218 Constant(
int kind, ConstantSubscript length, std::vector<Element> &&,
219 ConstantSubscripts &&);
222 bool operator==(
const Constant &that)
const {
223 return kind() == that.kind() && LEN() == that.LEN() &&
224 shape() == that.shape() && values_ == that.values_;
227 std::size_t size()
const;
229 const Scalar<Result> &values()
const {
return values_; }
230 ConstantSubscript LEN()
const {
return length_; }
231 bool wasHollerith()
const {
return wasHollerith_; }
232 void set_wasHollerith(
bool yes =
true) { wasHollerith_ = yes; }
234 std::optional<Scalar<Result>> GetScalarValue()
const {
243 Scalar<Result> At(
const ConstantSubscripts &)
const;
246 std::optional<Constant> Substring(ConstantSubscript, ConstantSubscript)
const;
248 Constant Reshape(ConstantSubscripts &&)
const;
249 llvm::raw_ostream &AsFortran(llvm::raw_ostream &)
const;
250 std::string AsFortran()
const;
251 DynamicType GetType()
const {
return {kind_, length_}; }
252 std::size_t CopyFrom(
const Constant &source, std::size_t count,
253 ConstantSubscripts &resultSubscripts,
const std::vector<int> *dimOrder);
257 Scalar<Result> values_;
258 ConstantSubscript length_;
259 bool wasHollerith_{
false};
270class Constant<
Type<TypeCategory::Derived>>
271 :
public ConstantBase<Type<TypeCategory::Derived>,
272 StructureConstructorValues> {
275 using Element = StructureConstructorValues;
277 ConstantBase<Type<TypeCategory::Derived>, StructureConstructorValues>;
288 std::vector<StructureConstructorValues> &&, ConstantSubscripts &&);
290 std::vector<StructureConstructor> &&, ConstantSubscripts &&);
291 CLASS_BOILERPLATE(Constant)
293 std::optional<StructureConstructor> GetScalarValue()
const;
296 bool operator==(
const Constant &)
const;
297 Constant Reshape(ConstantSubscripts &&)
const;
298 std::size_t CopyFrom(
const Constant &source, std::size_t count,
299 ConstantSubscripts &resultSubscripts,
const std::vector<int> *dimOrder);