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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
//===-- lib/Semantics/definable.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 "definable.h"
#include "flang/Evaluate/tools.h"
#include "flang/Semantics/tools.h"
using namespace Fortran::parser::literals;
namespace Fortran::semantics {
template <typename... A>
static parser::Message BlameSymbol(parser::CharBlock at,
const parser::MessageFixedText &text, const Symbol &original, A &&...x) {
parser::Message message{at, text, original.name(), std::forward<A>(x)...};
message.set_severity(parser::Severity::Error);
evaluate::AttachDeclaration(message, original);
return message;
}
static bool IsPointerDummyOfPureFunction(const Symbol &x) {
return IsPointerDummy(x) && FindPureProcedureContaining(x.owner()) &&
x.owner().symbol() && IsFunction(*x.owner().symbol());
}
// See C1594, first paragraph. These conditions enable checks on both
// left-hand and right-hand sides in various circumstances.
const char *WhyBaseObjectIsSuspicious(const Symbol &x, const Scope &scope) {
if (IsHostAssociatedIntoSubprogram(x, scope)) {
return "host-associated";
} else if (IsUseAssociated(x, scope)) {
return "USE-associated";
} else if (IsPointerDummyOfPureFunction(x)) {
return "a POINTER dummy argument of a pure function";
} else if (IsIntentIn(x)) {
return "an INTENT(IN) dummy argument";
} else if (FindCommonBlockContaining(x)) {
return "in a COMMON block";
} else {
return nullptr;
}
}
// Checks C1594(1,2); false if check fails
static std::optional<parser::Message> CheckDefinabilityInPureScope(
SourceName at, const Symbol &original, const Symbol &ultimate,
const Scope &context, const Scope &pure) {
if (pure.symbol()) {
if (const char *why{WhyBaseObjectIsSuspicious(ultimate, context)}) {
return BlameSymbol(at,
"'%s' may not be defined in pure subprogram '%s' because it is %s"_en_US,
original, pure.symbol()->name(), why);
}
}
return std::nullopt;
}
// True when the object being defined is not a subobject of the base
// object, e.g. X%PTR = 1., X%PTR%PTR2 => T (but not X%PTR => T).
// F'2023 9.4.2p5
static bool DefinesComponentPointerTarget(
const evaluate::DataRef &dataRef, DefinabilityFlags flags) {
if (const evaluate::Component *
component{common::visit(
common::visitors{
[](const SymbolRef &) -> const evaluate::Component * {
return nullptr;
},
[](const evaluate::Component &component) { return &component; },
[](const evaluate::ArrayRef &aRef) {
return aRef.base().UnwrapComponent();
},
[](const evaluate::CoarrayRef &aRef)
-> const evaluate::Component * { return nullptr; },
},
dataRef.u)}) {
const Symbol &compSym{component->GetLastSymbol()};
if (IsPointer(compSym) ||
(flags.test(DefinabilityFlag::AcceptAllocatable) &&
IsAllocatable(compSym))) {
if (!flags.test(DefinabilityFlag::PointerDefinition)) {
return true;
}
}
flags.reset(DefinabilityFlag::PointerDefinition);
return DefinesComponentPointerTarget(component->base(), flags);
} else {
return false;
}
}
// Check the leftmost (or only) symbol from a data-ref or expression.
static std::optional<parser::Message> WhyNotDefinableBase(parser::CharBlock at,
const Scope &scope, DefinabilityFlags flags, const Symbol &original,
bool isWholeSymbol, bool isComponentPointerTarget) {
const Symbol &ultimate{original.GetUltimate()};
bool isPointerDefinition{flags.test(DefinabilityFlag::PointerDefinition)};
bool acceptAllocatable{flags.test(DefinabilityFlag::AcceptAllocatable)};
bool isTargetDefinition{!isPointerDefinition && IsPointer(ultimate)};
if (const auto *association{ultimate.detailsIf<AssocEntityDetails>()}) {
if (!IsVariable(association->expr())) {
return BlameSymbol(at,
"'%s' is construct associated with an expression"_en_US, original);
} else if (evaluate::HasVectorSubscript(association->expr().value())) {
return BlameSymbol(at,
"Construct association '%s' has a vector subscript"_en_US, original);
} else if (auto dataRef{evaluate::ExtractDataRef(
*association->expr(), true, true)}) {
return WhyNotDefinableBase(at, scope, flags, dataRef->GetFirstSymbol(),
isWholeSymbol &&
std::holds_alternative<evaluate::SymbolRef>(dataRef->u),
isComponentPointerTarget ||
DefinesComponentPointerTarget(*dataRef, flags));
}
}
if (isTargetDefinition || isComponentPointerTarget) {
} else if (!isPointerDefinition && !IsVariableName(ultimate)) {
return BlameSymbol(at, "'%s' is not a variable"_en_US, original);
} else if (IsProtected(ultimate) && IsUseAssociated(original, scope)) {
return BlameSymbol(at, "'%s' is protected in this scope"_en_US, original);
} else if (IsIntentIn(ultimate) &&
(!IsPointer(ultimate) || (isWholeSymbol && isPointerDefinition))) {
return BlameSymbol(
at, "'%s' is an INTENT(IN) dummy argument"_en_US, original);
}
if (const Scope * pure{FindPureProcedureContaining(scope)}) {
// Additional checking for pure subprograms.
if (!isTargetDefinition || isComponentPointerTarget) {
if (auto msg{CheckDefinabilityInPureScope(
at, original, ultimate, scope, *pure)}) {
return msg;
}
}
if (const Symbol *
visible{FindExternallyVisibleObject(
ultimate, *pure, isPointerDefinition)}) {
return BlameSymbol(at,
"'%s' is externally visible via '%s' and not definable in a pure subprogram"_en_US,
original, visible->name());
}
}
if (const Scope * deviceContext{FindCUDADeviceContext(&scope)}) {
bool isOwnedByDeviceCode{deviceContext->Contains(ultimate.owner())};
if (isPointerDefinition && !acceptAllocatable) {
return BlameSymbol(at,
"'%s' is a pointer and may not be associated in a device subprogram"_err_en_US,
original);
} else if (auto cudaDataAttr{GetCUDADataAttr(&ultimate)}) {
if (*cudaDataAttr == common::CUDADataAttr::Constant) {
return BlameSymbol(at,
"'%s' has ATTRIBUTES(CONSTANT) and is not definable in a device subprogram"_err_en_US,
original);
} else if (acceptAllocatable && !isOwnedByDeviceCode) {
return BlameSymbol(at,
"'%s' is a host-associated allocatable and is not definable in a device subprogram"_err_en_US,
original);
} else if (*cudaDataAttr != common::CUDADataAttr::Device &&
*cudaDataAttr != common::CUDADataAttr::Managed &&
*cudaDataAttr != common::CUDADataAttr::Shared) {
return BlameSymbol(at,
"'%s' is not device or managed or shared data and is not definable in a device subprogram"_err_en_US,
original);
}
} else if (!isOwnedByDeviceCode) {
return BlameSymbol(at,
"'%s' is a host variable and is not definable in a device subprogram"_err_en_US,
original);
}
}
return std::nullopt;
}
static std::optional<parser::Message> WhyNotDefinableLast(parser::CharBlock at,
const Scope &scope, DefinabilityFlags flags, const Symbol &original) {
const Symbol &ultimate{original.GetUltimate()};
if (const auto *association{ultimate.detailsIf<AssocEntityDetails>()};
association &&
(association->rank().has_value() ||
!flags.test(DefinabilityFlag::PointerDefinition))) {
if (auto dataRef{
evaluate::ExtractDataRef(*association->expr(), true, true)}) {
return WhyNotDefinableLast(at, scope, flags, dataRef->GetLastSymbol());
}
}
if (flags.test(DefinabilityFlag::PointerDefinition)) {
if (flags.test(DefinabilityFlag::AcceptAllocatable)) {
if (!IsAllocatableOrObjectPointer(&ultimate)) {
return BlameSymbol(
at, "'%s' is neither a pointer nor an allocatable"_en_US, original);
}
} else if (!IsPointer(ultimate)) {
return BlameSymbol(at, "'%s' is not a pointer"_en_US, original);
}
return std::nullopt; // pointer assignment - skip following checks
}
if (IsOrContainsEventOrLockComponent(ultimate)) {
return BlameSymbol(at,
"'%s' is an entity with either an EVENT_TYPE or LOCK_TYPE"_en_US,
original);
}
if (FindPureProcedureContaining(scope)) {
if (auto dyType{evaluate::DynamicType::From(ultimate)}) {
if (!flags.test(DefinabilityFlag::PolymorphicOkInPure)) {
if (dyType->IsPolymorphic()) { // C1596
return BlameSymbol(
at, "'%s' is polymorphic in a pure subprogram"_en_US, original);
}
}
if (const Symbol * impure{HasImpureFinal(ultimate)}) {
return BlameSymbol(at, "'%s' has an impure FINAL procedure '%s'"_en_US,
original, impure->name());
}
if (const DerivedTypeSpec * derived{GetDerivedTypeSpec(dyType)}) {
if (!flags.test(DefinabilityFlag::PolymorphicOkInPure)) {
if (auto bad{FindPolymorphicAllocatableUltimateComponent(*derived)}) {
return BlameSymbol(at,
"'%s' has polymorphic component '%s' in a pure subprogram"_en_US,
original, bad.BuildResultDesignatorName());
}
}
}
}
}
return std::nullopt;
}
// Checks a data-ref
static std::optional<parser::Message> WhyNotDefinable(parser::CharBlock at,
const Scope &scope, DefinabilityFlags flags,
const evaluate::DataRef &dataRef) {
auto whyNotBase{
WhyNotDefinableBase(at, scope, flags, dataRef.GetFirstSymbol(),
std::holds_alternative<evaluate::SymbolRef>(dataRef.u),
DefinesComponentPointerTarget(dataRef, flags))};
if (!whyNotBase || !whyNotBase->IsFatal()) {
if (auto whyNotLast{
WhyNotDefinableLast(at, scope, flags, dataRef.GetLastSymbol())}) {
if (whyNotLast->IsFatal() || !whyNotBase) {
return whyNotLast;
}
}
}
return whyNotBase;
}
std::optional<parser::Message> WhyNotDefinable(parser::CharBlock at,
const Scope &scope, DefinabilityFlags flags, const Symbol &original) {
auto whyNotBase{WhyNotDefinableBase(at, scope, flags, original,
/*isWholeSymbol=*/true, /*isComponentPointerTarget=*/false)};
if (!whyNotBase || !whyNotBase->IsFatal()) {
if (auto whyNotLast{WhyNotDefinableLast(at, scope, flags, original)}) {
if (whyNotLast->IsFatal() || !whyNotBase) {
return whyNotLast;
}
}
}
return whyNotBase;
}
class DuplicatedSubscriptFinder
: public evaluate::AnyTraverse<DuplicatedSubscriptFinder, bool> {
using Base = evaluate::AnyTraverse<DuplicatedSubscriptFinder, bool>;
public:
explicit DuplicatedSubscriptFinder(evaluate::FoldingContext &foldingContext)
: Base{*this}, foldingContext_{foldingContext} {}
using Base::operator();
bool operator()(const evaluate::ActualArgument &) {
return false; // don't descend into argument expressions
}
bool operator()(const evaluate::ArrayRef &aRef) {
bool anyVector{false};
for (const auto &ss : aRef.subscript()) {
if (ss.Rank() > 0) {
anyVector = true;
if (const auto *vecExpr{
std::get_if<evaluate::IndirectSubscriptIntegerExpr>(&ss.u)}) {
auto folded{evaluate::Fold(foldingContext_,
evaluate::Expr<evaluate::SubscriptInteger>{vecExpr->value()})};
if (const auto *con{
evaluate::UnwrapConstantValue<evaluate::SubscriptInteger>(
folded)}) {
std::set<std::int64_t> values;
for (const auto &j : con->values()) {
if (auto pair{values.emplace(j.ToInt64())}; !pair.second) {
return true; // duplicate
}
}
}
return false;
}
}
}
return anyVector ? false : (*this)(aRef.base());
}
private:
evaluate::FoldingContext &foldingContext_;
};
std::optional<parser::Message> WhyNotDefinable(parser::CharBlock at,
const Scope &scope, DefinabilityFlags flags,
const evaluate::Expr<evaluate::SomeType> &expr) {
std::optional<parser::Message> portabilityWarning;
if (auto dataRef{evaluate::ExtractDataRef(expr, true, true)}) {
if (evaluate::HasVectorSubscript(expr)) {
if (flags.test(DefinabilityFlag::VectorSubscriptIsOk)) {
if (auto type{expr.GetType()}) {
if (!type->IsUnlimitedPolymorphic() &&
type->category() == TypeCategory::Derived) {
// Seek the FINAL subroutine that should but cannot be called
// for this definition of an array with a vector-valued subscript.
// If there's an elemental FINAL subroutine, all is well; otherwise,
// if there is a FINAL subroutine with a matching or assumed rank
// dummy argument, there's no way to call it.
int rank{expr.Rank()};
const DerivedTypeSpec *spec{&type->GetDerivedTypeSpec()};
while (spec) {
bool anyElemental{false};
const Symbol *anyRankMatch{nullptr};
for (auto ref : FinalsForDerivedTypeInstantiation(*spec)) {
const Symbol &ultimate{ref->GetUltimate()};
anyElemental |= ultimate.attrs().test(Attr::ELEMENTAL);
if (const auto *subp{ultimate.detailsIf<SubprogramDetails>()}) {
if (!subp->dummyArgs().empty()) {
if (const Symbol * arg{subp->dummyArgs()[0]}) {
const auto *object{arg->detailsIf<ObjectEntityDetails>()};
if (arg->Rank() == rank ||
(object && object->IsAssumedRank())) {
anyRankMatch = &*ref;
}
}
}
}
}
if (anyRankMatch && !anyElemental) {
if (!portabilityWarning &&
scope.context().languageFeatures().ShouldWarn(
common::UsageWarning::VectorSubscriptFinalization)) {
portabilityWarning = parser::Message{at,
"Variable '%s' has a vector subscript and will be finalized by non-elemental subroutine '%s'"_port_en_US,
expr.AsFortran(), anyRankMatch->name()};
}
break;
}
const auto *parent{FindParentTypeSpec(*spec)};
spec = parent ? parent->AsDerived() : nullptr;
}
}
}
if (!flags.test(DefinabilityFlag::DuplicatesAreOk) &&
DuplicatedSubscriptFinder{scope.context().foldingContext()}(expr)) {
return parser::Message{at,
"Variable has a vector subscript with a duplicated element"_err_en_US};
}
} else {
return parser::Message{at,
"Variable '%s' has a vector subscript"_err_en_US, expr.AsFortran()};
}
}
if (FindPureProcedureContaining(scope) &&
evaluate::ExtractCoarrayRef(expr)) {
return parser::Message(at,
"A pure subprogram may not define the coindexed object '%s'"_err_en_US,
expr.AsFortran());
}
if (auto whyNotDataRef{WhyNotDefinable(at, scope, flags, *dataRef)}) {
return whyNotDataRef;
}
} else if (evaluate::IsNullPointer(expr)) {
return parser::Message{
at, "'%s' is a null pointer"_err_en_US, expr.AsFortran()};
} else if (flags.test(DefinabilityFlag::PointerDefinition)) {
if (const auto *procDesignator{
std::get_if<evaluate::ProcedureDesignator>(&expr.u)}) {
// Defining a procedure pointer
if (const Symbol * procSym{procDesignator->GetSymbol()}) {
if (evaluate::ExtractCoarrayRef(expr)) { // C1027
return BlameSymbol(at,
"Procedure pointer '%s' may not be a coindexed object"_err_en_US,
*procSym, expr.AsFortran());
}
if (const auto *component{procDesignator->GetComponent()}) {
flags.reset(DefinabilityFlag::PointerDefinition);
return WhyNotDefinableBase(at, scope, flags,
component->base().GetFirstSymbol(), false,
DefinesComponentPointerTarget(component->base(), flags));
} else {
return WhyNotDefinable(at, scope, flags, *procSym);
}
}
}
return parser::Message{
at, "'%s' is not a definable pointer"_err_en_US, expr.AsFortran()};
} else if (!evaluate::IsVariable(expr)) {
return parser::Message{
at, "'%s' is not a variable or pointer"_err_en_US, expr.AsFortran()};
}
return portabilityWarning;
}
} // namespace Fortran::semantics
|