2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Evaluate/expression.cpp ---------------------------------------===//
|
2018-06-21 02:55:10 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-06-21 02:55:10 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-21 02:55:10 +08:00
|
|
|
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Evaluate/expression.h"
|
2018-07-26 04:46:13 +08:00
|
|
|
#include "int-power.h"
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Common/idioms.h"
|
|
|
|
#include "flang/Evaluate/common.h"
|
|
|
|
#include "flang/Evaluate/tools.h"
|
|
|
|
#include "flang/Evaluate/variable.h"
|
2020-09-04 23:44:52 +08:00
|
|
|
#include "flang/Parser/char-block.h"
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Parser/message.h"
|
2020-09-04 23:44:52 +08:00
|
|
|
#include "flang/Semantics/scope.h"
|
|
|
|
#include "flang/Semantics/symbol.h"
|
|
|
|
#include "flang/Semantics/tools.h"
|
|
|
|
#include "flang/Semantics/type.h"
|
2020-02-28 23:11:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-06-21 02:55:10 +08:00
|
|
|
#include <string>
|
2018-06-21 07:50:27 +08:00
|
|
|
#include <type_traits>
|
2018-06-21 02:55:10 +08:00
|
|
|
|
2018-06-23 05:51:15 +08:00
|
|
|
using namespace Fortran::parser::literals;
|
|
|
|
|
2018-06-21 02:55:10 +08:00
|
|
|
namespace Fortran::evaluate {
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-07-04 01:25:43 +08:00
|
|
|
std::optional<Expr<SubscriptInteger>>
|
|
|
|
Expr<Type<TypeCategory::Character, KIND>>::LEN() const {
|
|
|
|
using T = std::optional<Expr<SubscriptInteger>>;
|
2018-07-20 01:12:25 +08:00
|
|
|
return std::visit(
|
2018-11-30 01:27:34 +08:00
|
|
|
common::visitors{
|
2019-07-04 01:25:43 +08:00
|
|
|
[](const Constant<Result> &c) -> T {
|
2019-02-08 04:05:27 +08:00
|
|
|
return AsExpr(Constant<SubscriptInteger>{c.LEN()});
|
2018-11-30 01:27:34 +08:00
|
|
|
},
|
2019-07-04 01:25:43 +08:00
|
|
|
[](const ArrayConstructor<Result> &a) -> T { return a.LEN(); },
|
2018-09-18 02:31:38 +08:00
|
|
|
[](const Parentheses<Result> &x) { return x.left().LEN(); },
|
2019-02-20 07:38:55 +08:00
|
|
|
[](const Convert<Result> &x) {
|
|
|
|
return std::visit(
|
|
|
|
[&](const auto &kx) { return kx.LEN(); }, x.left().u);
|
|
|
|
},
|
2019-07-04 01:25:43 +08:00
|
|
|
[](const Concat<KIND> &c) -> T {
|
|
|
|
if (auto llen{c.left().LEN()}) {
|
|
|
|
if (auto rlen{c.right().LEN()}) {
|
|
|
|
return *std::move(llen) + *std::move(rlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2018-08-09 07:30:58 +08:00
|
|
|
},
|
2019-07-04 01:25:43 +08:00
|
|
|
[](const Extremum<Result> &c) -> T {
|
|
|
|
if (auto llen{c.left().LEN()}) {
|
|
|
|
if (auto rlen{c.right().LEN()}) {
|
|
|
|
return Expr<SubscriptInteger>{Extremum<SubscriptInteger>{
|
2019-10-31 23:03:16 +08:00
|
|
|
Ordering::Greater, *std::move(llen), *std::move(rlen)}};
|
2019-07-04 01:25:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2018-08-18 06:38:37 +08:00
|
|
|
},
|
2018-09-15 06:48:40 +08:00
|
|
|
[](const Designator<Result> &dr) { return dr.LEN(); },
|
2018-11-30 01:27:34 +08:00
|
|
|
[](const FunctionRef<Result> &fr) { return fr.LEN(); },
|
2019-07-04 01:25:43 +08:00
|
|
|
[](const SetLength<KIND> &x) -> T { return x.right(); },
|
2018-11-30 01:27:34 +08:00
|
|
|
},
|
2018-07-21 03:19:09 +08:00
|
|
|
u);
|
2018-07-20 00:53:42 +08:00
|
|
|
}
|
|
|
|
|
2019-03-21 02:38:45 +08:00
|
|
|
Expr<SomeType>::~Expr() = default;
|
2018-09-08 01:33:32 +08:00
|
|
|
|
2019-01-04 00:45:27 +08:00
|
|
|
#if defined(__APPLE__) && defined(__GNUC__)
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A>
|
2018-12-21 01:39:29 +08:00
|
|
|
typename ExpressionBase<A>::Derived &ExpressionBase<A>::derived() {
|
|
|
|
return *static_cast<Derived *>(this);
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A>
|
2018-12-21 01:39:29 +08:00
|
|
|
const typename ExpressionBase<A>::Derived &ExpressionBase<A>::derived() const {
|
|
|
|
return *static_cast<const Derived *>(this);
|
|
|
|
}
|
2019-01-04 00:45:27 +08:00
|
|
|
#endif
|
2018-12-21 01:39:29 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A>
|
2018-10-09 06:35:19 +08:00
|
|
|
std::optional<DynamicType> ExpressionBase<A>::GetType() const {
|
2019-01-05 06:05:53 +08:00
|
|
|
if constexpr (IsLengthlessIntrinsicType<Result>) {
|
2018-10-10 03:07:29 +08:00
|
|
|
return Result::GetType();
|
2018-10-09 06:35:19 +08:00
|
|
|
} else {
|
|
|
|
return std::visit(
|
2019-08-02 03:32:17 +08:00
|
|
|
[&](const auto &x) -> std::optional<DynamicType> {
|
2019-05-04 02:29:15 +08:00
|
|
|
if constexpr (!common::HasMember<decltype(x), TypelessExpression>) {
|
2018-10-09 06:35:19 +08:00
|
|
|
return x.GetType();
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
return std::nullopt; // w/o "else" to dodge bogus g++ 8.1 warning
|
2018-10-09 06:35:19 +08:00
|
|
|
},
|
|
|
|
derived().u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> int ExpressionBase<A>::Rank() const {
|
2018-09-19 00:34:59 +08:00
|
|
|
return std::visit(
|
2018-10-02 05:36:31 +08:00
|
|
|
[](const auto &x) {
|
2019-05-04 02:29:15 +08:00
|
|
|
if constexpr (common::HasMember<decltype(x), TypelessExpression>) {
|
2018-10-02 05:36:31 +08:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return x.Rank();
|
|
|
|
}
|
|
|
|
},
|
2018-09-19 00:34:59 +08:00
|
|
|
derived().u);
|
|
|
|
}
|
|
|
|
|
2021-09-23 07:49:09 +08:00
|
|
|
DynamicType Parentheses<SomeDerived>::GetType() const {
|
|
|
|
return left().GetType().value();
|
|
|
|
}
|
|
|
|
|
2021-10-01 05:16:29 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
template <typename A> LLVM_DUMP_METHOD void ExpressionBase<A>::dump() const {
|
|
|
|
llvm::errs() << "Expr is <{" << AsFortran() << "}>\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-11 01:28:36 +08:00
|
|
|
// Equality testing
|
2019-01-05 06:05:53 +08:00
|
|
|
|
2019-01-23 08:30:32 +08:00
|
|
|
bool ImpliedDoIndex::operator==(const ImpliedDoIndex &that) const {
|
|
|
|
return name == that.name;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename T>
|
2019-01-23 08:30:32 +08:00
|
|
|
bool ImpliedDo<T>::operator==(const ImpliedDo<T> &that) const {
|
2019-03-06 04:28:08 +08:00
|
|
|
return name_ == that.name_ && lower_ == that.lower_ &&
|
|
|
|
upper_ == that.upper_ && stride_ == that.stride_ &&
|
|
|
|
values_ == that.values_;
|
2019-01-05 06:05:53 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename T>
|
2020-03-11 01:28:36 +08:00
|
|
|
bool ArrayConstructorValue<T>::operator==(
|
|
|
|
const ArrayConstructorValue<T> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename R>
|
2019-01-05 06:05:53 +08:00
|
|
|
bool ArrayConstructorValues<R>::operator==(
|
|
|
|
const ArrayConstructorValues<R> &that) const {
|
2019-02-09 08:35:02 +08:00
|
|
|
return values_ == that.values_;
|
2019-01-23 08:30:32 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-01-23 08:30:32 +08:00
|
|
|
bool ArrayConstructor<Type<TypeCategory::Character, KIND>>::operator==(
|
2019-02-09 08:35:02 +08:00
|
|
|
const ArrayConstructor &that) const {
|
|
|
|
return length_ == that.length_ &&
|
|
|
|
static_cast<const Base &>(*this) == static_cast<const Base &>(that);
|
2019-01-05 06:05:53 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 02:39:10 +08:00
|
|
|
bool ArrayConstructor<SomeDerived>::operator==(
|
2019-02-09 08:35:02 +08:00
|
|
|
const ArrayConstructor &that) const {
|
2019-05-14 00:33:18 +08:00
|
|
|
return result_ == that.result_ &&
|
2019-02-09 08:35:02 +08:00
|
|
|
static_cast<const Base &>(*this) == static_cast<const Base &>(that);
|
|
|
|
;
|
2019-02-09 02:39:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StructureConstructor::StructureConstructor(
|
|
|
|
const semantics::DerivedTypeSpec &spec,
|
|
|
|
const StructureConstructorValues &values)
|
2020-03-29 12:00:16 +08:00
|
|
|
: result_{spec}, values_{values} {}
|
2019-02-09 02:39:10 +08:00
|
|
|
StructureConstructor::StructureConstructor(
|
|
|
|
const semantics::DerivedTypeSpec &spec, StructureConstructorValues &&values)
|
2020-03-29 12:00:16 +08:00
|
|
|
: result_{spec}, values_{std::move(values)} {}
|
2019-02-09 02:39:10 +08:00
|
|
|
|
|
|
|
bool StructureConstructor::operator==(const StructureConstructor &that) const {
|
2019-05-14 00:33:18 +08:00
|
|
|
return result_ == that.result_ && values_ == that.values_;
|
2019-02-09 02:39:10 +08:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:28:36 +08:00
|
|
|
bool Relational<SomeType>::operator==(const Relational<SomeType> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2020-03-11 01:28:36 +08:00
|
|
|
bool Expr<Type<TypeCategory::Integer, KIND>>::operator==(
|
|
|
|
const Expr<Type<TypeCategory::Integer, KIND>> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2020-03-11 01:28:36 +08:00
|
|
|
bool Expr<Type<TypeCategory::Real, KIND>>::operator==(
|
|
|
|
const Expr<Type<TypeCategory::Real, KIND>> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2020-03-11 01:28:36 +08:00
|
|
|
bool Expr<Type<TypeCategory::Complex, KIND>>::operator==(
|
|
|
|
const Expr<Type<TypeCategory::Complex, KIND>> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2020-03-11 01:28:36 +08:00
|
|
|
bool Expr<Type<TypeCategory::Logical, KIND>>::operator==(
|
|
|
|
const Expr<Type<TypeCategory::Logical, KIND>> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2020-03-11 01:28:36 +08:00
|
|
|
bool Expr<Type<TypeCategory::Character, KIND>>::operator==(
|
|
|
|
const Expr<Type<TypeCategory::Character, KIND>> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <TypeCategory CAT>
|
2020-03-11 01:28:36 +08:00
|
|
|
bool Expr<SomeKind<CAT>>::operator==(const Expr<SomeKind<CAT>> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr<SomeDerived>::operator==(const Expr<SomeDerived> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr<SomeCharacter>::operator==(const Expr<SomeCharacter> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr<SomeType>::operator==(const Expr<SomeType> &that) const {
|
|
|
|
return u == that.u;
|
|
|
|
}
|
|
|
|
|
2019-05-14 00:33:18 +08:00
|
|
|
DynamicType StructureConstructor::GetType() const { return result_.GetType(); }
|
2019-02-09 02:39:10 +08:00
|
|
|
|
2020-09-04 23:44:52 +08:00
|
|
|
std::optional<Expr<SomeType>> StructureConstructor::CreateParentComponent(
|
|
|
|
const Symbol &component) const {
|
|
|
|
if (const semantics::DerivedTypeSpec *
|
|
|
|
parentSpec{GetParentTypeSpec(derivedTypeSpec())}) {
|
|
|
|
StructureConstructor structureConstructor{*parentSpec};
|
|
|
|
if (const auto *parentDetails{
|
|
|
|
component.detailsIf<semantics::DerivedTypeDetails>()}) {
|
|
|
|
auto parentIter{parentDetails->componentNames().begin()};
|
|
|
|
for (const auto &childIter : values_) {
|
|
|
|
if (parentIter == parentDetails->componentNames().end()) {
|
|
|
|
break; // There are more components in the child
|
|
|
|
}
|
|
|
|
SymbolRef componentSymbol{childIter.first};
|
|
|
|
structureConstructor.Add(
|
|
|
|
*componentSymbol, common::Clone(childIter.second.value()));
|
|
|
|
++parentIter;
|
|
|
|
}
|
|
|
|
Constant<SomeDerived> constResult{std::move(structureConstructor)};
|
|
|
|
Expr<SomeDerived> result{std::move(constResult)};
|
|
|
|
return std::optional<Expr<SomeType>>{result};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Symbol *GetParentComponentSymbol(const Symbol &symbol) {
|
|
|
|
if (symbol.test(Symbol::Flag::ParentComp)) {
|
|
|
|
// we have a created parent component
|
|
|
|
const auto &compObject{symbol.get<semantics::ObjectEntityDetails>()};
|
|
|
|
if (const semantics::DeclTypeSpec * compType{compObject.type()}) {
|
|
|
|
const semantics::DerivedTypeSpec &dtSpec{compType->derivedTypeSpec()};
|
|
|
|
const semantics::Symbol &compTypeSymbol{dtSpec.typeSymbol()};
|
|
|
|
return &compTypeSymbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (symbol.detailsIf<semantics::DerivedTypeDetails>()) {
|
|
|
|
// we have an implicit parent type component
|
|
|
|
return &symbol;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Expr<SomeType>> StructureConstructor::Find(
|
2019-10-23 07:53:29 +08:00
|
|
|
const Symbol &component) const {
|
2019-05-22 07:58:46 +08:00
|
|
|
if (auto iter{values_.find(component)}; iter != values_.end()) {
|
2020-09-04 23:44:52 +08:00
|
|
|
return iter->second.value();
|
|
|
|
}
|
|
|
|
// The component wasn't there directly, see if we're looking for the parent
|
|
|
|
// component of an extended type
|
|
|
|
if (const Symbol * typeSymbol{GetParentComponentSymbol(component)}) {
|
|
|
|
return CreateParentComponent(*typeSymbol);
|
|
|
|
}
|
|
|
|
// Look for the component in the parent type component. The parent type
|
|
|
|
// component is always the first one
|
|
|
|
if (!values_.empty()) {
|
|
|
|
const Expr<SomeType> *parentExpr{&values_.begin()->second.value()};
|
|
|
|
if (const Expr<SomeDerived> *derivedExpr{
|
|
|
|
std::get_if<Expr<SomeDerived>>(&parentExpr->u)}) {
|
|
|
|
if (const Constant<SomeDerived> *constExpr{
|
|
|
|
std::get_if<Constant<SomeDerived>>(&derivedExpr->u)}) {
|
|
|
|
if (std::optional<StructureConstructor> parentComponentValue{
|
|
|
|
constExpr->GetScalarValue()}) {
|
|
|
|
// Try to find the component in the parent structure constructor
|
|
|
|
return parentComponentValue->Find(component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 07:58:46 +08:00
|
|
|
}
|
2020-09-04 23:44:52 +08:00
|
|
|
return std::nullopt;
|
2019-05-22 07:58:46 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 02:39:10 +08:00
|
|
|
StructureConstructor &StructureConstructor::Add(
|
|
|
|
const Symbol &symbol, Expr<SomeType> &&expr) {
|
2019-10-23 07:53:29 +08:00
|
|
|
values_.emplace(symbol, std::move(expr));
|
2019-02-09 02:39:10 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-12-18 02:53:20 +08:00
|
|
|
GenericExprWrapper::~GenericExprWrapper() {}
|
2019-03-21 02:38:45 +08:00
|
|
|
|
2020-07-10 02:08:41 +08:00
|
|
|
void GenericExprWrapper::Deleter(GenericExprWrapper *p) { delete p; }
|
|
|
|
|
2019-12-18 02:53:20 +08:00
|
|
|
GenericAssignmentWrapper::~GenericAssignmentWrapper() {}
|
2019-11-23 08:04:56 +08:00
|
|
|
|
2020-07-10 02:08:41 +08:00
|
|
|
void GenericAssignmentWrapper::Deleter(GenericAssignmentWrapper *p) {
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <TypeCategory CAT> int Expr<SomeKind<CAT>>::GetKind() const {
|
2019-03-21 02:38:45 +08:00
|
|
|
return std::visit(
|
|
|
|
[](const auto &kx) { return std::decay_t<decltype(kx)>::Result::kind; },
|
|
|
|
u);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Expr<SomeCharacter>::GetKind() const {
|
|
|
|
return std::visit(
|
|
|
|
[](const auto &kx) { return std::decay_t<decltype(kx)>::Result::kind; },
|
|
|
|
u);
|
|
|
|
}
|
|
|
|
|
2019-07-04 01:25:43 +08:00
|
|
|
std::optional<Expr<SubscriptInteger>> Expr<SomeCharacter>::LEN() const {
|
2019-03-21 02:38:45 +08:00
|
|
|
return std::visit([](const auto &kx) { return kx.LEN(); }, u);
|
|
|
|
}
|
|
|
|
|
2019-03-28 06:27:33 +08:00
|
|
|
INSTANTIATE_EXPRESSION_TEMPLATES
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::evaluate
|