1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
//===-- lib/Evaluate/call.cpp ---------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "flang/Evaluate/call.h"
#include "flang/Common/Fortran.h"
#include "flang/Common/idioms.h"
#include "flang/Evaluate/characteristics.h"
#include "flang/Evaluate/check-expression.h"
#include "flang/Evaluate/expression.h"
#include "flang/Evaluate/tools.h"
#include "flang/Semantics/symbol.h"
namespace Fortran::evaluate {
DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ActualArgument)
ActualArgument::ActualArgument(Expr<SomeType> &&x) : u_{std::move(x)} {}
ActualArgument::ActualArgument(common::CopyableIndirection<Expr<SomeType>> &&v)
: u_{std::move(v)} {}
ActualArgument::ActualArgument(AssumedType x) : u_{x} {}
ActualArgument::ActualArgument(common::Label x) : u_{x} {}
ActualArgument::~ActualArgument() {}
ActualArgument::AssumedType::AssumedType(const Symbol &symbol)
: symbol_{symbol} {
const semantics::DeclTypeSpec *type{symbol.GetType()};
CHECK(type && type->category() == semantics::DeclTypeSpec::TypeStar);
}
int ActualArgument::AssumedType::Rank() const { return symbol_->Rank(); }
ActualArgument &ActualArgument::operator=(Expr<SomeType> &&expr) {
u_ = std::move(expr);
return *this;
}
std::optional<DynamicType> ActualArgument::GetType() const {
if (const Expr<SomeType> *expr{UnwrapExpr()}) {
return expr->GetType();
} else if (std::holds_alternative<AssumedType>(u_)) {
return DynamicType::AssumedType();
} else {
return std::nullopt;
}
}
int ActualArgument::Rank() const {
if (const Expr<SomeType> *expr{UnwrapExpr()}) {
return expr->Rank();
} else {
return std::get<AssumedType>(u_).Rank();
}
}
bool ActualArgument::operator==(const ActualArgument &that) const {
return keyword_ == that.keyword_ && isPassedObject_ == that.isPassedObject_ &&
u_ == that.u_;
}
void ActualArgument::Parenthesize() {
u_ = evaluate::Parenthesize(std::move(DEREF(UnwrapExpr())));
}
SpecificIntrinsic::SpecificIntrinsic(
IntrinsicProcedure n, characteristics::Procedure &&chars)
: name{n}, characteristics{
new characteristics::Procedure{std::move(chars)}} {}
DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(SpecificIntrinsic)
SpecificIntrinsic::~SpecificIntrinsic() {}
bool SpecificIntrinsic::operator==(const SpecificIntrinsic &that) const {
return name == that.name && characteristics == that.characteristics;
}
ProcedureDesignator::ProcedureDesignator(Component &&c)
: u{common::CopyableIndirection<Component>::Make(std::move(c))} {}
bool ProcedureDesignator::operator==(const ProcedureDesignator &that) const {
return u == that.u;
}
std::optional<DynamicType> ProcedureDesignator::GetType() const {
if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&u)}) {
if (const auto &result{intrinsic->characteristics.value().functionResult}) {
if (const auto *typeAndShape{result->GetTypeAndShape()}) {
return typeAndShape->type();
}
}
} else {
return DynamicType::From(GetSymbol());
}
return std::nullopt;
}
int ProcedureDesignator::Rank() const {
if (const Symbol * symbol{GetSymbol()}) {
// Subtle: will be zero for functions returning procedure pointers
return symbol->Rank();
}
if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&u)}) {
if (const auto &result{intrinsic->characteristics.value().functionResult}) {
if (const auto *typeAndShape{result->GetTypeAndShape()}) {
CHECK(!typeAndShape->attrs().test(
characteristics::TypeAndShape::Attr::AssumedRank));
return typeAndShape->Rank();
}
// Otherwise, intrinsic returns a procedure pointer (e.g. NULL(MOLD=pptr))
}
}
return 0;
}
const Symbol *ProcedureDesignator::GetInterfaceSymbol() const {
if (const Symbol * symbol{GetSymbol()}) {
const Symbol &ultimate{symbol->GetUltimate()};
if (const auto *proc{ultimate.detailsIf<semantics::ProcEntityDetails>()}) {
return proc->procInterface();
} else if (const auto *binding{
ultimate.detailsIf<semantics::ProcBindingDetails>()}) {
return &binding->symbol();
} else if (ultimate.has<semantics::SubprogramDetails>()) {
return &ultimate;
}
}
return nullptr;
}
bool ProcedureDesignator::IsElemental() const {
if (const Symbol * interface{GetInterfaceSymbol()}) {
return IsElementalProcedure(*interface);
} else if (const Symbol * symbol{GetSymbol()}) {
return IsElementalProcedure(*symbol);
} else if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&u)}) {
return intrinsic->characteristics.value().attrs.test(
characteristics::Procedure::Attr::Elemental);
} else {
DIE("ProcedureDesignator::IsElemental(): no case");
}
return false;
}
bool ProcedureDesignator::IsPure() const {
if (const Symbol * interface{GetInterfaceSymbol()}) {
return IsPureProcedure(*interface);
} else if (const Symbol * symbol{GetSymbol()}) {
return IsPureProcedure(*symbol);
} else if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&u)}) {
return intrinsic->characteristics.value().attrs.test(
characteristics::Procedure::Attr::Pure);
} else {
DIE("ProcedureDesignator::IsPure(): no case");
}
return false;
}
const SpecificIntrinsic *ProcedureDesignator::GetSpecificIntrinsic() const {
return std::get_if<SpecificIntrinsic>(&u);
}
const Component *ProcedureDesignator::GetComponent() const {
if (auto *c{std::get_if<common::CopyableIndirection<Component>>(&u)}) {
return &c->value();
} else {
return nullptr;
}
}
const Symbol *ProcedureDesignator::GetSymbol() const {
return common::visit(
common::visitors{
[](SymbolRef symbol) { return &*symbol; },
[](const common::CopyableIndirection<Component> &c) {
return &c.value().GetLastSymbol();
},
[](const auto &) -> const Symbol * { return nullptr; },
},
u);
}
const SymbolRef *ProcedureDesignator::UnwrapSymbolRef() const {
return std::get_if<SymbolRef>(&u);
}
std::string ProcedureDesignator::GetName() const {
return common::visit(
common::visitors{
[](const SpecificIntrinsic &i) { return i.name; },
[](const Symbol &symbol) { return symbol.name().ToString(); },
[](const common::CopyableIndirection<Component> &c) {
return c.value().GetLastSymbol().name().ToString();
},
},
u);
}
std::optional<Expr<SubscriptInteger>> ProcedureRef::LEN() const {
if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&proc_.u)}) {
if (intrinsic->name == "repeat") {
// LEN(REPEAT(ch,n)) == LEN(ch) * n
CHECK(arguments_.size() == 2);
const auto *stringArg{
UnwrapExpr<Expr<SomeCharacter>>(arguments_[0].value())};
const auto *nCopiesArg{
UnwrapExpr<Expr<SomeInteger>>(arguments_[1].value())};
CHECK(stringArg && nCopiesArg);
if (auto stringLen{stringArg->LEN()}) {
auto converted{ConvertTo(*stringLen, common::Clone(*nCopiesArg))};
return *std::move(stringLen) * std::move(converted);
}
}
// Some other cases (e.g., LEN(CHAR(...))) are handled in
// ProcedureDesignator::LEN() because they're independent of the
// lengths of the actual arguments.
}
if (auto len{proc_.LEN()}) {
if (IsActuallyConstant(*len)) {
return len;
}
// TODO: Handle cases where the length of a function result is a
// safe expression in terms of actual argument values, after substituting
// actual argument expressions for INTENT(IN)/VALUE dummy arguments.
}
return std::nullopt;
}
int ProcedureRef::Rank() const {
if (IsElemental()) {
for (const auto &arg : arguments_) {
if (arg) {
if (int rank{arg->Rank()}; rank > 0) {
return rank;
}
}
}
return 0;
} else {
return proc_.Rank();
}
}
ProcedureRef::~ProcedureRef() {}
void ProcedureRef::Deleter(ProcedureRef *p) { delete p; }
} // namespace Fortran::evaluate
|