2019-04-02 00:22:45 +02:00
|
|
|
// Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// GetShape() analyzes an expression and determines its shape, if possible,
|
|
|
|
// representing the result as a vector of scalar integer expressions.
|
|
|
|
|
|
|
|
#ifndef FORTRAN_EVALUATE_SHAPE_H_
|
|
|
|
#define FORTRAN_EVALUATE_SHAPE_H_
|
|
|
|
|
|
|
|
#include "expression.h"
|
2019-04-04 22:12:21 +02:00
|
|
|
#include "tools.h"
|
2019-04-02 00:22:45 +02:00
|
|
|
#include "type.h"
|
2019-06-25 22:07:32 +02:00
|
|
|
#include "variable.h"
|
2019-04-02 00:22:45 +02:00
|
|
|
#include "../common/indirection.h"
|
|
|
|
#include <optional>
|
|
|
|
#include <variant>
|
|
|
|
|
2019-05-03 23:41:59 +02:00
|
|
|
namespace Fortran::parser {
|
|
|
|
class ContextualMessages;
|
|
|
|
}
|
|
|
|
|
2019-04-02 00:22:45 +02:00
|
|
|
namespace Fortran::evaluate {
|
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
class FoldingContext;
|
|
|
|
|
2019-04-04 22:12:21 +02:00
|
|
|
using ExtentType = SubscriptInteger;
|
|
|
|
using ExtentExpr = Expr<ExtentType>;
|
2019-05-22 01:58:46 +02:00
|
|
|
using MaybeExtentExpr = std::optional<ExtentExpr>;
|
|
|
|
using Shape = std::vector<MaybeExtentExpr>;
|
|
|
|
|
|
|
|
bool IsImpliedShape(const Symbol &);
|
|
|
|
bool IsExplicitShape(const Symbol &);
|
2019-04-04 22:12:21 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
// Conversions between various representations of shapes.
|
2019-05-22 01:58:46 +02:00
|
|
|
Shape AsShape(const Constant<ExtentType> &);
|
|
|
|
std::optional<Shape> AsShape(FoldingContext &, ExtentExpr &&);
|
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
std::optional<ExtentExpr> AsExtentArrayExpr(const Shape &);
|
2019-05-22 01:58:46 +02:00
|
|
|
|
2019-04-04 22:12:21 +02:00
|
|
|
std::optional<Constant<ExtentType>> AsConstantShape(const Shape &);
|
2019-05-22 01:58:46 +02:00
|
|
|
Constant<ExtentType> AsConstantShape(const ConstantSubscripts &);
|
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
ConstantSubscripts AsConstantExtents(const Constant<ExtentType> &);
|
|
|
|
std::optional<ConstantSubscripts> AsConstantExtents(const Shape &);
|
2019-04-04 22:12:21 +02:00
|
|
|
|
2019-05-22 01:58:46 +02:00
|
|
|
inline int GetRank(const Shape &s) { return static_cast<int>(s.size()); }
|
|
|
|
|
|
|
|
// The dimension here is zero-based, unlike DIM= arguments to many intrinsics.
|
2019-06-25 22:07:32 +02:00
|
|
|
MaybeExtentExpr GetLowerBound(
|
|
|
|
FoldingContext &, const NamedEntity &, int dimension);
|
|
|
|
Shape GetLowerBounds(FoldingContext &, const NamedEntity &);
|
|
|
|
MaybeExtentExpr GetExtent(FoldingContext &, const NamedEntity &, int dimension);
|
|
|
|
MaybeExtentExpr GetExtent(
|
|
|
|
FoldingContext &, const Subscript &, const NamedEntity &, int dimension);
|
2019-05-22 01:58:46 +02:00
|
|
|
MaybeExtentExpr GetUpperBound(
|
|
|
|
FoldingContext &, MaybeExtentExpr &&lower, MaybeExtentExpr &&extent);
|
|
|
|
|
2019-04-04 22:58:46 +02:00
|
|
|
// Compute an element count for a triplet or trip count for a DO.
|
2019-04-04 22:12:21 +02:00
|
|
|
ExtentExpr CountTrips(
|
|
|
|
ExtentExpr &&lower, ExtentExpr &&upper, ExtentExpr &&stride);
|
|
|
|
ExtentExpr CountTrips(
|
|
|
|
const ExtentExpr &lower, const ExtentExpr &upper, const ExtentExpr &stride);
|
2019-05-22 01:58:46 +02:00
|
|
|
MaybeExtentExpr CountTrips(
|
|
|
|
MaybeExtentExpr &&lower, MaybeExtentExpr &&upper, MaybeExtentExpr &&stride);
|
2019-04-04 22:12:21 +02:00
|
|
|
|
|
|
|
// Computes SIZE() == PRODUCT(shape)
|
2019-05-22 01:58:46 +02:00
|
|
|
MaybeExtentExpr GetSize(Shape &&);
|
2019-04-02 00:22:45 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
// Utility predicate: does an expression reference any implied DO index?
|
|
|
|
bool ContainsAnyImpliedDoIndex(const ExtentExpr &);
|
2019-04-02 00:22:45 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
// Compilation-time shape conformance checking, when corresponding extents
|
|
|
|
// are known.
|
2019-05-03 20:29:15 +02:00
|
|
|
bool CheckConformance(parser::ContextualMessages &, const Shape &,
|
|
|
|
const Shape &, const char * = "left operand",
|
|
|
|
const char * = "right operand");
|
2019-04-02 00:22:45 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
// The implementation of GetShape() is wrapped in a helper class
|
|
|
|
// so that the member functions may mutually recurse without prototypes.
|
|
|
|
class GetShapeHelper {
|
|
|
|
public:
|
|
|
|
explicit GetShapeHelper(FoldingContext &context) : context_{context} {}
|
2019-04-04 22:12:21 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
template<typename T> std::optional<Shape> GetShape(const Expr<T> &expr) {
|
|
|
|
return GetShape(expr.u);
|
|
|
|
}
|
2019-04-02 00:22:45 +02:00
|
|
|
|
2019-06-25 22:07:32 +02:00
|
|
|
std::optional<Shape> GetShape(const Symbol &);
|
2019-04-18 23:11:15 +02:00
|
|
|
std::optional<Shape> GetShape(const Symbol *);
|
|
|
|
std::optional<Shape> GetShape(const Component &);
|
2019-06-25 22:07:32 +02:00
|
|
|
std::optional<Shape> GetShape(const NamedEntity &);
|
|
|
|
std::optional<Shape> GetShape(const BaseObject &);
|
2019-04-18 23:11:15 +02:00
|
|
|
std::optional<Shape> GetShape(const ArrayRef &);
|
|
|
|
std::optional<Shape> GetShape(const CoarrayRef &);
|
|
|
|
std::optional<Shape> GetShape(const DataRef &);
|
|
|
|
std::optional<Shape> GetShape(const Substring &);
|
|
|
|
std::optional<Shape> GetShape(const ComplexPart &);
|
|
|
|
std::optional<Shape> GetShape(const ActualArgument &);
|
2019-05-03 20:29:15 +02:00
|
|
|
std::optional<Shape> GetShape(const ProcedureDesignator &);
|
2019-04-18 23:11:15 +02:00
|
|
|
std::optional<Shape> GetShape(const ProcedureRef &);
|
|
|
|
std::optional<Shape> GetShape(const ImpliedDoIndex &);
|
|
|
|
std::optional<Shape> GetShape(const Relational<SomeType> &);
|
|
|
|
std::optional<Shape> GetShape(const StructureConstructor &);
|
|
|
|
std::optional<Shape> GetShape(const DescriptorInquiry &);
|
|
|
|
std::optional<Shape> GetShape(const BOZLiteralConstant &);
|
|
|
|
std::optional<Shape> GetShape(const NullPointer &);
|
|
|
|
|
|
|
|
template<typename T> std::optional<Shape> GetShape(const Constant<T> &c) {
|
|
|
|
Constant<ExtentType> shape{c.SHAPE()};
|
|
|
|
return AsShape(shape);
|
|
|
|
}
|
2019-04-04 01:04:13 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
template<typename T>
|
|
|
|
std::optional<Shape> GetShape(const Designator<T> &designator) {
|
|
|
|
return GetShape(designator.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::optional<Shape> GetShape(const Variable<T> &variable) {
|
|
|
|
return GetShape(variable.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename D, typename R, typename... O>
|
|
|
|
std::optional<Shape> GetShape(const Operation<D, R, O...> &operation) {
|
|
|
|
if constexpr (sizeof...(O) > 1) {
|
|
|
|
if (operation.right().Rank() > 0) {
|
|
|
|
return GetShape(operation.right());
|
|
|
|
}
|
2019-04-04 01:04:13 +02:00
|
|
|
}
|
2019-04-18 23:11:15 +02:00
|
|
|
return GetShape(operation.left());
|
2019-04-04 01:04:13 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
template<int KIND>
|
|
|
|
std::optional<Shape> GetShape(const TypeParamInquiry<KIND> &) {
|
|
|
|
return Shape{}; // always scalar, even when applied to an array
|
|
|
|
}
|
2019-04-04 22:12:21 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
template<typename T>
|
|
|
|
std::optional<Shape> GetShape(const ArrayConstructor<T> &aconst) {
|
2019-05-22 01:58:46 +02:00
|
|
|
return Shape{GetArrayConstructorExtent(aconst)};
|
2019-04-18 23:11:15 +02:00
|
|
|
}
|
2019-04-04 22:12:21 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
template<typename... A>
|
|
|
|
std::optional<Shape> GetShape(const std::variant<A...> &u) {
|
|
|
|
return std::visit([&](const auto &x) { return GetShape(x); }, u);
|
|
|
|
}
|
2019-04-04 22:12:21 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
template<typename A, bool COPY>
|
|
|
|
std::optional<Shape> GetShape(const common::Indirection<A, COPY> &p) {
|
|
|
|
return GetShape(p.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename A>
|
|
|
|
std::optional<Shape> GetShape(const std::optional<A> &x) {
|
|
|
|
if (x.has_value()) {
|
|
|
|
return GetShape(*x);
|
2019-04-04 22:12:21 +02:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 01:04:13 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
private:
|
|
|
|
template<typename T>
|
2019-05-22 01:58:46 +02:00
|
|
|
MaybeExtentExpr GetArrayConstructorValueExtent(
|
|
|
|
const ArrayConstructorValue<T> &value) {
|
2019-04-18 23:11:15 +02:00
|
|
|
return std::visit(
|
|
|
|
common::visitors{
|
2019-05-22 01:58:46 +02:00
|
|
|
[&](const Expr<T> &x) -> MaybeExtentExpr {
|
2019-04-18 23:11:15 +02:00
|
|
|
if (std::optional<Shape> xShape{GetShape(x)}) {
|
|
|
|
// Array values in array constructors get linearized.
|
|
|
|
return GetSize(std::move(*xShape));
|
2019-05-13 18:33:18 +02:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
2019-04-18 23:11:15 +02:00
|
|
|
}
|
|
|
|
},
|
2019-05-22 01:58:46 +02:00
|
|
|
[&](const ImpliedDo<T> &ido) -> MaybeExtentExpr {
|
2019-04-18 23:11:15 +02:00
|
|
|
// Don't be heroic and try to figure out triangular implied DO
|
|
|
|
// nests.
|
|
|
|
if (!ContainsAnyImpliedDoIndex(ido.lower()) &&
|
|
|
|
!ContainsAnyImpliedDoIndex(ido.upper()) &&
|
|
|
|
!ContainsAnyImpliedDoIndex(ido.stride())) {
|
2019-05-22 01:58:46 +02:00
|
|
|
if (auto nValues{GetArrayConstructorExtent(ido.values())}) {
|
2019-04-18 23:11:15 +02:00
|
|
|
return std::move(*nValues) *
|
|
|
|
CountTrips(ido.lower(), ido.upper(), ido.stride());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
value.u);
|
|
|
|
}
|
2019-04-04 01:04:13 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
template<typename T>
|
2019-05-22 01:58:46 +02:00
|
|
|
MaybeExtentExpr GetArrayConstructorExtent(
|
|
|
|
const ArrayConstructorValues<T> &values) {
|
2019-04-18 23:11:15 +02:00
|
|
|
ExtentExpr result{0};
|
|
|
|
for (const auto &value : values) {
|
2019-05-22 01:58:46 +02:00
|
|
|
if (MaybeExtentExpr n{GetArrayConstructorValueExtent(value)}) {
|
2019-04-18 23:11:15 +02:00
|
|
|
result = std::move(result) + std::move(*n);
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-04-04 01:04:13 +02:00
|
|
|
|
2019-04-18 23:11:15 +02:00
|
|
|
FoldingContext &context_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A>
|
|
|
|
std::optional<Shape> GetShape(FoldingContext &context, const A &x) {
|
|
|
|
return GetShapeHelper{context}.GetShape(x);
|
2019-04-02 00:22:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // FORTRAN_EVALUATE_SHAPE_H_
|