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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
//===-- lib/Semantics/pointer-assignment.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 "pointer-assignment.h"
#include "definable.h"
#include "flang/Common/idioms.h"
#include "flang/Common/restorer.h"
#include "flang/Evaluate/characteristics.h"
#include "flang/Evaluate/expression.h"
#include "flang/Evaluate/fold.h"
#include "flang/Evaluate/tools.h"
#include "flang/Parser/message.h"
#include "flang/Parser/parse-tree-visitor.h"
#include "flang/Parser/parse-tree.h"
#include "flang/Semantics/expression.h"
#include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
#include <set>
#include <string>
#include <type_traits>
// Semantic checks for pointer assignment.
namespace Fortran::semantics {
using namespace parser::literals;
using evaluate::characteristics::DummyDataObject;
using evaluate::characteristics::FunctionResult;
using evaluate::characteristics::Procedure;
using evaluate::characteristics::TypeAndShape;
using parser::MessageFixedText;
using parser::MessageFormattedText;
class PointerAssignmentChecker {
public:
PointerAssignmentChecker(SemanticsContext &context, const Scope &scope,
parser::CharBlock source, const std::string &description)
: context_{context}, scope_{scope}, source_{source}, description_{
description} {}
PointerAssignmentChecker(
SemanticsContext &context, const Scope &scope, const Symbol &lhs)
: context_{context}, scope_{scope}, source_{lhs.name()},
description_{"pointer '"s + lhs.name().ToString() + '\''}, lhs_{&lhs} {
set_lhsType(TypeAndShape::Characterize(lhs, foldingContext_));
set_isContiguous(lhs.attrs().test(Attr::CONTIGUOUS));
set_isVolatile(lhs.attrs().test(Attr::VOLATILE));
}
PointerAssignmentChecker &set_lhsType(std::optional<TypeAndShape> &&);
PointerAssignmentChecker &set_isContiguous(bool);
PointerAssignmentChecker &set_isVolatile(bool);
PointerAssignmentChecker &set_isBoundsRemapping(bool);
PointerAssignmentChecker &set_pointerComponentLHS(const Symbol *);
bool CheckLeftHandSide(const SomeExpr &);
bool Check(const SomeExpr &);
private:
bool CharacterizeProcedure();
template <typename T> bool Check(const T &);
template <typename T> bool Check(const evaluate::Expr<T> &);
template <typename T> bool Check(const evaluate::FunctionRef<T> &);
template <typename T> bool Check(const evaluate::Designator<T> &);
bool Check(const evaluate::NullPointer &);
bool Check(const evaluate::ProcedureDesignator &);
bool Check(const evaluate::ProcedureRef &);
// Target is a procedure
bool Check(parser::CharBlock rhsName, bool isCall,
const Procedure * = nullptr,
const evaluate::SpecificIntrinsic *specific = nullptr);
bool LhsOkForUnlimitedPoly() const;
template <typename... A> parser::Message *Say(A &&...);
SemanticsContext &context_;
evaluate::FoldingContext &foldingContext_{context_.foldingContext()};
const Scope &scope_;
const parser::CharBlock source_;
const std::string description_;
const Symbol *lhs_{nullptr};
std::optional<TypeAndShape> lhsType_;
std::optional<Procedure> procedure_;
bool characterizedProcedure_{false};
bool isContiguous_{false};
bool isVolatile_{false};
bool isBoundsRemapping_{false};
const Symbol *pointerComponentLHS_{nullptr};
};
PointerAssignmentChecker &PointerAssignmentChecker::set_lhsType(
std::optional<TypeAndShape> &&lhsType) {
lhsType_ = std::move(lhsType);
return *this;
}
PointerAssignmentChecker &PointerAssignmentChecker::set_isContiguous(
bool isContiguous) {
isContiguous_ = isContiguous;
return *this;
}
PointerAssignmentChecker &PointerAssignmentChecker::set_isVolatile(
bool isVolatile) {
isVolatile_ = isVolatile;
return *this;
}
PointerAssignmentChecker &PointerAssignmentChecker::set_isBoundsRemapping(
bool isBoundsRemapping) {
isBoundsRemapping_ = isBoundsRemapping;
return *this;
}
PointerAssignmentChecker &PointerAssignmentChecker::set_pointerComponentLHS(
const Symbol *symbol) {
pointerComponentLHS_ = symbol;
return *this;
}
bool PointerAssignmentChecker::CharacterizeProcedure() {
if (!characterizedProcedure_) {
characterizedProcedure_ = true;
if (lhs_ && IsProcedure(*lhs_)) {
procedure_ = Procedure::Characterize(*lhs_, foldingContext_);
}
}
return procedure_.has_value();
}
bool PointerAssignmentChecker::CheckLeftHandSide(const SomeExpr &lhs) {
if (auto whyNot{WhyNotDefinable(foldingContext_.messages().at(), scope_,
DefinabilityFlags{DefinabilityFlag::PointerDefinition}, lhs)}) {
if (auto *msg{Say(
"The left-hand side of a pointer assignment is not definable"_err_en_US)}) {
msg->Attach(std::move(*whyNot));
}
return false;
} else {
return true;
}
}
template <typename T> bool PointerAssignmentChecker::Check(const T &) {
// Catch-all case for really bad target expression
Say("Target associated with %s must be a designator or a call to a"
" pointer-valued function"_err_en_US,
description_);
return false;
}
template <typename T>
bool PointerAssignmentChecker::Check(const evaluate::Expr<T> &x) {
return common::visit([&](const auto &x) { return Check(x); }, x.u);
}
bool PointerAssignmentChecker::Check(const SomeExpr &rhs) {
if (HasVectorSubscript(rhs)) { // C1025
Say("An array section with a vector subscript may not be a pointer target"_err_en_US);
return false;
}
if (ExtractCoarrayRef(rhs)) { // C1026
Say("A coindexed object may not be a pointer target"_err_en_US);
return false;
}
if (!common::visit([&](const auto &x) { return Check(x); }, rhs.u)) {
return false;
}
if (IsNullPointer(rhs)) {
return true;
}
if (lhs_ && IsProcedure(*lhs_)) {
return true;
}
if (const auto *pureProc{FindPureProcedureContaining(scope_)}) {
if (pointerComponentLHS_) { // C1594(4) is a hard error
if (const Symbol * object{FindExternallyVisibleObject(rhs, *pureProc)}) {
if (auto *msg{Say(
"Externally visible object '%s' may not be associated with pointer component '%s' in a pure procedure"_err_en_US,
object->name(), pointerComponentLHS_->name())}) {
msg->Attach(object->name(), "Object declaration"_en_US)
.Attach(
pointerComponentLHS_->name(), "Pointer declaration"_en_US);
}
return false;
}
} else if (const Symbol * base{GetFirstSymbol(rhs)}) {
if (const char *why{WhyBaseObjectIsSuspicious(
base->GetUltimate(), scope_)}) { // C1594(3)
evaluate::SayWithDeclaration(foldingContext_.messages(), *base,
"A pure subprogram may not use '%s' as the target of pointer assignment because it is %s"_err_en_US,
base->name(), why);
return false;
}
}
}
if (isContiguous_) {
if (auto contiguous{evaluate::IsContiguous(rhs, foldingContext_)}) {
if (!*contiguous) {
Say("CONTIGUOUS pointer may not be associated with a discontiguous target"_err_en_US);
return false;
}
} else if (context_.ShouldWarn(
common::UsageWarning::PointerToPossibleNoncontiguous)) {
Say("Target of CONTIGUOUS pointer association is not known to be contiguous"_warn_en_US);
}
}
// Warn about undefinable data targets
if (context_.ShouldWarn(common::UsageWarning::PointerToUndefinable)) {
if (auto because{WhyNotDefinable(
foldingContext_.messages().at(), scope_, {}, rhs)}) {
if (auto *msg{
Say("Pointer target is not a definable variable"_warn_en_US)}) {
msg->Attach(std::move(*because));
}
return false;
}
}
return true;
}
bool PointerAssignmentChecker::Check(const evaluate::NullPointer &) {
return true; // P => NULL() without MOLD=; always OK
}
template <typename T>
bool PointerAssignmentChecker::Check(const evaluate::FunctionRef<T> &f) {
std::string funcName;
const auto *symbol{f.proc().GetSymbol()};
if (symbol) {
funcName = symbol->name().ToString();
} else if (const auto *intrinsic{f.proc().GetSpecificIntrinsic()}) {
funcName = intrinsic->name;
}
auto proc{Procedure::Characterize(f.proc(), foldingContext_)};
if (!proc) {
return false;
}
std::optional<MessageFixedText> msg;
const auto &funcResult{proc->functionResult}; // C1025
if (!funcResult) {
msg = "%s is associated with the non-existent result of reference to"
" procedure"_err_en_US;
} else if (CharacterizeProcedure()) {
// Shouldn't be here in this function unless lhs is an object pointer.
msg = "Procedure %s is associated with the result of a reference to"
" function '%s' that does not return a procedure pointer"_err_en_US;
} else if (funcResult->IsProcedurePointer()) {
msg = "Object %s is associated with the result of a reference to"
" function '%s' that is a procedure pointer"_err_en_US;
} else if (!funcResult->attrs.test(FunctionResult::Attr::Pointer)) {
msg = "%s is associated with the result of a reference to function '%s'"
" that is a not a pointer"_err_en_US;
} else if (isContiguous_ &&
!funcResult->attrs.test(FunctionResult::Attr::Contiguous)) {
msg = "CONTIGUOUS %s is associated with the result of reference to"
" function '%s' that is not contiguous"_err_en_US;
} else if (lhsType_) {
const auto *frTypeAndShape{funcResult->GetTypeAndShape()};
CHECK(frTypeAndShape);
if (!lhsType_->IsCompatibleWith(foldingContext_.messages(), *frTypeAndShape,
"pointer", "function result",
isBoundsRemapping_ /*omit shape check*/,
evaluate::CheckConformanceFlags::BothDeferredShape)) {
return false; // IsCompatibleWith() emitted message
}
}
if (msg) {
auto restorer{common::ScopedSet(lhs_, symbol)};
Say(*msg, description_, funcName);
return false;
}
return true;
}
template <typename T>
bool PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) {
const Symbol *last{d.GetLastSymbol()};
const Symbol *base{d.GetBaseObject().symbol()};
if (!last || !base) {
// P => "character literal"(1:3)
Say("Pointer target is not a named entity"_err_en_US);
return false;
}
std::optional<std::variant<MessageFixedText, MessageFormattedText>> msg;
if (CharacterizeProcedure()) {
// Shouldn't be here in this function unless lhs is an object pointer.
msg = "In assignment to procedure %s, the target is not a procedure or"
" procedure pointer"_err_en_US;
} else if (!evaluate::GetLastTarget(GetSymbolVector(d))) { // C1025
msg = "In assignment to object %s, the target '%s' is not an object with"
" POINTER or TARGET attributes"_err_en_US;
} else if (auto rhsType{TypeAndShape::Characterize(d, foldingContext_)}) {
if (!lhsType_) {
msg = "%s associated with object '%s' with incompatible type or"
" shape"_err_en_US;
} else if (rhsType->corank() > 0 &&
(isVolatile_ != last->attrs().test(Attr::VOLATILE))) { // C1020
// TODO: what if A is VOLATILE in A%B%C? need a better test here
if (isVolatile_) {
msg = "Pointer may not be VOLATILE when target is a"
" non-VOLATILE coarray"_err_en_US;
} else {
msg = "Pointer must be VOLATILE when target is a"
" VOLATILE coarray"_err_en_US;
}
} else if (rhsType->type().IsUnlimitedPolymorphic()) {
if (!LhsOkForUnlimitedPoly()) {
msg = "Pointer type must be unlimited polymorphic or non-extensible"
" derived type when target is unlimited polymorphic"_err_en_US;
}
} else {
if (!lhsType_->type().IsTkLenCompatibleWith(rhsType->type())) {
msg = MessageFormattedText{
"Target type %s is not compatible with pointer type %s"_err_en_US,
rhsType->type().AsFortran(), lhsType_->type().AsFortran()};
} else if (!isBoundsRemapping_ &&
!lhsType_->attrs().test(TypeAndShape::Attr::AssumedRank)) {
int lhsRank{evaluate::GetRank(lhsType_->shape())};
int rhsRank{evaluate::GetRank(rhsType->shape())};
if (lhsRank != rhsRank) {
msg = MessageFormattedText{
"Pointer has rank %d but target has rank %d"_err_en_US, lhsRank,
rhsRank};
}
}
}
}
if (msg) {
auto restorer{common::ScopedSet(lhs_, last)};
if (auto *m{std::get_if<MessageFixedText>(&*msg)}) {
std::string buf;
llvm::raw_string_ostream ss{buf};
d.AsFortran(ss);
Say(*m, description_, ss.str());
} else {
Say(std::get<MessageFormattedText>(*msg));
}
return false;
}
return true;
}
// Common handling for procedure pointer right-hand sides
bool PointerAssignmentChecker::Check(parser::CharBlock rhsName, bool isCall,
const Procedure *rhsProcedure,
const evaluate::SpecificIntrinsic *specific) {
std::string whyNot;
CharacterizeProcedure();
if (std::optional<MessageFixedText> msg{evaluate::CheckProcCompatibility(
isCall, procedure_, rhsProcedure, specific, whyNot)}) {
Say(std::move(*msg), description_, rhsName, whyNot);
return false;
}
return true;
}
bool PointerAssignmentChecker::Check(const evaluate::ProcedureDesignator &d) {
if (const Symbol * symbol{d.GetSymbol()}) {
if (const auto *subp{
symbol->GetUltimate().detailsIf<SubprogramDetails>()}) {
if (subp->stmtFunction()) {
evaluate::SayWithDeclaration(foldingContext_.messages(), *symbol,
"Statement function '%s' may not be the target of a pointer assignment"_err_en_US,
symbol->name());
return false;
}
} else if (symbol->has<ProcBindingDetails>() &&
context_.ShouldWarn(common::UsageWarning::Portability)) {
evaluate::SayWithDeclaration(foldingContext_.messages(), *symbol,
"Procedure binding '%s' used as target of a pointer assignment"_port_en_US,
symbol->name());
}
}
if (auto chars{Procedure::Characterize(d, foldingContext_)}) {
return Check(d.GetName(), false, &*chars, d.GetSpecificIntrinsic());
} else {
return Check(d.GetName(), false);
}
}
bool PointerAssignmentChecker::Check(const evaluate::ProcedureRef &ref) {
if (auto chars{Procedure::Characterize(ref, foldingContext_)}) {
if (chars->functionResult) {
if (const auto *proc{chars->functionResult->IsProcedurePointer()}) {
return Check(ref.proc().GetName(), true, proc);
}
}
return Check(ref.proc().GetName(), true, &*chars);
} else {
return Check(ref.proc().GetName(), true, nullptr);
}
}
// The target can be unlimited polymorphic if the pointer is, or if it is
// a non-extensible derived type.
bool PointerAssignmentChecker::LhsOkForUnlimitedPoly() const {
const auto &type{lhsType_->type()};
if (type.category() != TypeCategory::Derived || type.IsAssumedType()) {
return false;
} else if (type.IsUnlimitedPolymorphic()) {
return true;
} else {
return !IsExtensibleType(&type.GetDerivedTypeSpec());
}
}
template <typename... A>
parser::Message *PointerAssignmentChecker::Say(A &&...x) {
auto *msg{foldingContext_.messages().Say(std::forward<A>(x)...)};
if (msg) {
if (lhs_) {
return evaluate::AttachDeclaration(msg, *lhs_);
}
if (!source_.empty()) {
msg->Attach(source_, "Declaration of %s"_en_US, description_);
}
}
return msg;
}
// Verify that any bounds on the LHS of a pointer assignment are valid.
// Return true if it is a bound-remapping so we can perform further checks.
static bool CheckPointerBounds(
evaluate::FoldingContext &context, const evaluate::Assignment &assignment) {
auto &messages{context.messages()};
const SomeExpr &lhs{assignment.lhs};
const SomeExpr &rhs{assignment.rhs};
bool isBoundsRemapping{false};
std::size_t numBounds{common::visit(
common::visitors{
[&](const evaluate::Assignment::BoundsSpec &bounds) {
return bounds.size();
},
[&](const evaluate::Assignment::BoundsRemapping &bounds) {
isBoundsRemapping = true;
evaluate::ExtentExpr lhsSizeExpr{1};
for (const auto &bound : bounds) {
lhsSizeExpr = std::move(lhsSizeExpr) *
(common::Clone(bound.second) - common::Clone(bound.first) +
evaluate::ExtentExpr{1});
}
if (std::optional<std::int64_t> lhsSize{evaluate::ToInt64(
evaluate::Fold(context, std::move(lhsSizeExpr)))}) {
if (auto shape{evaluate::GetShape(context, rhs)}) {
if (std::optional<std::int64_t> rhsSize{
evaluate::ToInt64(evaluate::Fold(
context, evaluate::GetSize(std::move(*shape))))}) {
if (*lhsSize > *rhsSize) {
messages.Say(
"Pointer bounds require %d elements but target has"
" only %d"_err_en_US,
*lhsSize, *rhsSize); // 10.2.2.3(9)
}
}
}
}
return bounds.size();
},
[](const auto &) -> std::size_t {
DIE("not valid for pointer assignment");
},
},
assignment.u)};
if (numBounds > 0) {
if (lhs.Rank() != static_cast<int>(numBounds)) {
messages.Say("Pointer '%s' has rank %d but the number of bounds specified"
" is %d"_err_en_US,
lhs.AsFortran(), lhs.Rank(), numBounds); // C1018
}
}
if (isBoundsRemapping && rhs.Rank() != 1 &&
!evaluate::IsSimplyContiguous(rhs, context)) {
messages.Say("Pointer bounds remapping target must have rank 1 or be"
" simply contiguous"_err_en_US); // 10.2.2.3(9)
}
return isBoundsRemapping;
}
bool CheckPointerAssignment(SemanticsContext &context,
const evaluate::Assignment &assignment, const Scope &scope) {
return CheckPointerAssignment(context, assignment.lhs, assignment.rhs, scope,
CheckPointerBounds(context.foldingContext(), assignment));
}
bool CheckPointerAssignment(SemanticsContext &context, const SomeExpr &lhs,
const SomeExpr &rhs, const Scope &scope, bool isBoundsRemapping) {
const Symbol *pointer{GetLastSymbol(lhs)};
if (!pointer) {
return false; // error was reported
}
PointerAssignmentChecker checker{context, scope, *pointer};
checker.set_isBoundsRemapping(isBoundsRemapping);
bool lhsOk{checker.CheckLeftHandSide(lhs)};
bool rhsOk{checker.Check(rhs)};
return lhsOk && rhsOk; // don't short-circuit
}
bool CheckStructConstructorPointerComponent(SemanticsContext &context,
const Symbol &lhs, const SomeExpr &rhs, const Scope &scope) {
return PointerAssignmentChecker{context, scope, lhs}
.set_pointerComponentLHS(&lhs)
.Check(rhs);
}
bool CheckPointerAssignment(SemanticsContext &context, parser::CharBlock source,
const std::string &description, const DummyDataObject &lhs,
const SomeExpr &rhs, const Scope &scope) {
return PointerAssignmentChecker{context, scope, source, description}
.set_lhsType(common::Clone(lhs.type))
.set_isContiguous(lhs.attrs.test(DummyDataObject::Attr::Contiguous))
.set_isVolatile(lhs.attrs.test(DummyDataObject::Attr::Volatile))
.Check(rhs);
}
bool CheckInitialTarget(SemanticsContext &context, const SomeExpr &pointer,
const SomeExpr &init, const Scope &scope) {
return evaluate::IsInitialDataTarget(
init, &context.foldingContext().messages()) &&
CheckPointerAssignment(context, pointer, init, scope);
}
} // namespace Fortran::semantics
|