12#ifndef FORTRAN_EVALUATE_SHAPE_H_
13#define FORTRAN_EVALUATE_SHAPE_H_
15#include "expression.h"
18#include "flang/Common/indirection.h"
19#include "flang/Evaluate/type.h"
31using ExtentType = SubscriptInteger;
33using MaybeExtentExpr = std::optional<ExtentExpr>;
34using Shape = std::vector<MaybeExtentExpr>;
36bool IsImpliedShape(
const Symbol &);
37bool IsExplicitShape(
const Symbol &);
40std::optional<ExtentExpr> AsExtentArrayExpr(
const Shape &);
42std::optional<Constant<ExtentType>> AsConstantShape(
49std::optional<ConstantSubscripts> AsConstantExtents(
51inline std::optional<ConstantSubscripts> AsConstantExtents(
52 FoldingContext &foldingContext,
const std::optional<Shape> &maybeShape) {
54 return AsConstantExtents(foldingContext, *maybeShape);
59Shape AsShape(
const ConstantSubscripts &);
60std::optional<Shape> AsShape(
const std::optional<ConstantSubscripts> &);
62inline int GetRank(
const Shape &s) {
return static_cast<int>(s.size()); }
65std::optional<Shape> Fold(
FoldingContext &, std::optional<Shape> &&);
71std::optional<Shape> GetShape(
74std::optional<Shape> GetShape(
77std::optional<Shape> GetShape(
const A &,
bool invariantOnly =
true);
89ExtentExpr GetRawLowerBound(
90 const NamedEntity &,
int dimension,
bool invariantOnly =
true);
92 int dimension,
bool invariantOnly =
true);
93MaybeExtentExpr GetLBOUND(
94 const NamedEntity &,
int dimension,
bool invariantOnly =
true);
96 bool invariantOnly =
true);
97MaybeExtentExpr GetRawUpperBound(
98 const NamedEntity &,
int dimension,
bool invariantOnly =
true);
100 int dimension,
bool invariantOnly =
true);
101MaybeExtentExpr GetUBOUND(
102 const NamedEntity &,
int dimension,
bool invariantOnly =
true);
104 bool invariantOnly =
true);
105MaybeExtentExpr ComputeUpperBound(ExtentExpr &&lower, MaybeExtentExpr &&extent);
106MaybeExtentExpr ComputeUpperBound(
108Shape GetRawLowerBounds(
const NamedEntity &,
bool invariantOnly =
true);
109Shape GetRawLowerBounds(
111Shape GetLBOUNDs(
const NamedEntity &,
bool invariantOnly =
true);
114Shape GetUBOUNDs(
const NamedEntity &,
bool invariantOnly =
true);
117MaybeExtentExpr GetExtent(
118 const NamedEntity &,
int dimension,
bool invariantOnly =
true);
120 bool invariantOnly =
true);
122 bool invariantOnly =
true);
124 const NamedEntity &,
int dimension,
bool invariantOnly =
true);
127MaybeExtentExpr GetLCOBOUND(
128 const Symbol &,
int dimension,
bool invariantOnly =
true);
129MaybeExtentExpr GetUCOBOUND(
130 const Symbol &,
int dimension,
bool invariantOnly =
true);
131Shape GetLCOBOUNDs(
const Symbol &,
bool invariantOnly =
true);
132Shape GetUCOBOUNDs(
const Symbol &,
bool invariantOnly =
true);
135ExtentExpr CountTrips(
136 ExtentExpr &&lower, ExtentExpr &&upper, ExtentExpr &&stride);
137ExtentExpr CountTrips(
138 const ExtentExpr &lower,
const ExtentExpr &upper,
const ExtentExpr &stride);
139MaybeExtentExpr CountTrips(
140 MaybeExtentExpr &&lower, MaybeExtentExpr &&upper, MaybeExtentExpr &&stride);
143MaybeExtentExpr GetSize(Shape &&);
144ConstantSubscript GetSize(
const ConstantSubscripts &);
145inline MaybeExtentExpr GetSize(
const std::optional<Shape> &maybeShape) {
147 return GetSize(Shape(*maybeShape));
153bool ContainsAnyImpliedDoIndex(
const ExtentExpr &);
158 :
public AnyTraverse<GetShapeHelper, std::optional<Shape>> {
160 using Result = std::optional<Shape>;
161 using Base = AnyTraverse<GetShapeHelper, Result>;
162 using Base::operator();
164 : Base{*
this}, context_{context}, invariantOnly_{invariantOnly} {}
166 Result operator()(
const ImpliedDoIndex &)
const {
return ScalarShape(); }
169 Result operator()(
const BOZLiteralConstant &)
const {
return ScalarShape(); }
170 Result operator()(
const StaticDataObject::Pointer &)
const {
171 return ScalarShape();
174 return ScalarShape();
177 template <
typename T> Result operator()(
const Constant<T> &c)
const {
178 return ConstantShape(c.SHAPE());
181 Result operator()(
const Symbol &)
const;
182 Result operator()(
const Component &)
const;
183 Result operator()(
const ArrayRef &)
const;
185 Result operator()(
const Substring &)
const;
188 template <
typename T>
190 return Shape{GetArrayConstructorExtent(aconst)};
192 template <
typename T>
196 int rank{conditional.thenValue().Rank()};
197 Result thenShape{(*this)(conditional.thenValue())};
199 return Shape(rank, std::nullopt);
201 Result elseShape{(*this)(conditional.elseValue())};
202 if (thenShape != elseShape) {
203 return Shape(rank, std::nullopt);
207 template <
typename D,
typename R,
typename LO,
typename RO>
209 if (
int rr{operation.right().Rank()}; rr > 0) {
210 if (
int lr{operation.left().Rank()}; lr == 0 || lr == rr) {
211 return (*
this)(operation.right());
216 return (*
this)(operation.left());
221 static Result ScalarShape() {
return Shape{}; }
223 Result AsShapeResult(ExtentExpr &&)
const;
226 template <
typename T>
227 MaybeExtentExpr GetArrayConstructorValueExtent(
229 return common::visit(
231 [&](
const Expr<T> &x) -> MaybeExtentExpr {
232 if (
auto xShape{(*this)(x)}) {
234 return GetSize(std::move(*xShape));
242 if (!ContainsAnyImpliedDoIndex(ido.lower()) &&
243 !ContainsAnyImpliedDoIndex(ido.upper()) &&
244 !ContainsAnyImpliedDoIndex(ido.stride())) {
245 if (
auto nValues{GetArrayConstructorExtent(ido.values())}) {
246 if (!ContainsAnyImpliedDoIndex(*nValues)) {
247 return std::move(*nValues) *
248 CountTrips(ido.lower(), ido.upper(), ido.stride());
258 template <
typename T>
259 MaybeExtentExpr GetArrayConstructorExtent(
261 ExtentExpr result{0};
262 for (
const auto &value : values) {
263 if (MaybeExtentExpr n{GetArrayConstructorValueExtent(value)}) {
264 AccumulateExtent(result, std::move(*n));
273 void AccumulateExtent(ExtentExpr &, ExtentExpr &&)
const;
276 mutable bool useResultSymbolShape_{
true};
279 bool invariantOnly_{
true};
283std::optional<Shape> GetShape(
287 return Fold(*context, std::move(shape));
297std::optional<Shape> GetShape(
299 return GetShape(&context, x, invariantOnly);
303std::optional<Shape> GetShape(
const A &x,
bool invariantOnly) {
304 return GetShape(
nullptr, x, invariantOnly);
308std::optional<Constant<ExtentType>> GetConstantShape(
310 if (
auto shape{GetShape(context, x,
true)}) {
311 return AsConstantShape(context, *shape);
319std::optional<ConstantSubscripts> GetConstantExtents(
321 if (
auto shape{GetShape(context, x,
true)}) {
322 if (
auto extents{AsConstantExtents(context, *shape)}) {
323 if (!HasNegativeExtent(*extents)) {
336std::optional<Shape> GetContextFreeShape(
FoldingContext &context,
const A &x) {
352 LeftScalarExpandable = 1,
353 RightScalarExpandable = 2,
354 LeftIsDeferredShape = 4,
355 RightIsDeferredShape = 8,
356 EitherScalarExpandable = LeftScalarExpandable | RightScalarExpandable,
357 BothDeferredShape = LeftIsDeferredShape | RightIsDeferredShape,
358 RightIsExpandableDeferred = RightScalarExpandable | RightIsDeferredShape,
362 const Shape &left,
const Shape &right,
363 CheckConformanceFlags::Flags flags = CheckConformanceFlags::None,
364 const char *leftIs =
"left operand",
const char *rightIs =
"right operand");
370bool IncrementSubscripts(
371 ConstantSubscripts &,
const ConstantSubscripts &extents);
Definition expression.h:466
Definition expression.h:494
Definition variable.h:205
Definition variable.h:243
Definition expression.h:395
Definition constant.h:147
Definition variable.h:413
Definition expression.h:432
Definition variable.h:101
Definition expression.h:114
Definition expression.h:769
Definition variable.h:304
Definition variable.h:136
Definition check-expression.h:19
Definition expression.h:460
Definition expression.h:424
Definition variable.h:191