| 1 |
//===-- lib/Semantics/check-do-forall.cpp ---------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "check-do-forall.h" |
| 10 |
#include "definable.h" |
| 11 |
#include "flang/Common/template.h" |
| 12 |
#include "flang/Evaluate/call.h" |
| 13 |
#include "flang/Evaluate/expression.h" |
| 14 |
#include "flang/Evaluate/tools.h" |
| 15 |
#include "flang/Evaluate/traverse.h" |
| 16 |
#include "flang/Parser/message.h" |
| 17 |
#include "flang/Parser/parse-tree-visitor.h" |
| 18 |
#include "flang/Parser/tools.h" |
| 19 |
#include "flang/Semantics/attr.h" |
| 20 |
#include "flang/Semantics/scope.h" |
| 21 |
#include "flang/Semantics/semantics.h" |
| 22 |
#include "flang/Semantics/symbol.h" |
| 23 |
#include "flang/Semantics/tools.h" |
| 24 |
#include "flang/Semantics/type.h" |
| 25 |
|
| 26 |
namespace Fortran::evaluate { |
| 27 |
using ActualArgumentRef = common::Reference<const ActualArgument>; |
| 28 |
|
| 29 |
inline bool operator<(ActualArgumentRef x, ActualArgumentRef y) { |
| 30 |
return &*x < &*y; |
| 31 |
} |
| 32 |
} // namespace Fortran::evaluate |
| 33 |
|
| 34 |
namespace Fortran::semantics { |
| 35 |
|
| 36 |
using namespace parser::literals; |
| 37 |
|
| 38 |
using Bounds = parser::LoopControl::Bounds; |
| 39 |
using IndexVarKind = SemanticsContext::IndexVarKind; |
| 40 |
|
| 41 |
static const parser::ConcurrentHeader &GetConcurrentHeader( |
| 42 |
const parser::LoopControl &loopControl) { |
| 43 |
const auto &concurrent{ |
| 44 |
std::get<parser::LoopControl::Concurrent>(loopControl.u)}; |
| 45 |
return std::get<parser::ConcurrentHeader>(concurrent.t); |
| 46 |
} |
| 47 |
static const parser::ConcurrentHeader &GetConcurrentHeader( |
| 48 |
const parser::ForallConstruct &construct) { |
| 49 |
const auto &stmt{ |
| 50 |
std::get<parser::Statement<parser::ForallConstructStmt>>(construct.t)}; |
| 51 |
return std::get<common::Indirection<parser::ConcurrentHeader>>( |
| 52 |
stmt.statement.t) |
| 53 |
.value(); |
| 54 |
} |
| 55 |
static const parser::ConcurrentHeader &GetConcurrentHeader( |
| 56 |
const parser::ForallStmt &stmt) { |
| 57 |
return std::get<common::Indirection<parser::ConcurrentHeader>>(stmt.t) |
| 58 |
.value(); |
| 59 |
} |
| 60 |
template <typename T> |
| 61 |
static const std::list<parser::ConcurrentControl> &GetControls(const T &x) { |
| 62 |
return std::get<std::list<parser::ConcurrentControl>>( |
| 63 |
GetConcurrentHeader(x).t); |
| 64 |
} |
| 65 |
|
| 66 |
static const Bounds &GetBounds(const parser::DoConstruct &doConstruct) { |
| 67 |
auto &loopControl{doConstruct.GetLoopControl().value()}; |
| 68 |
return std::get<Bounds>(loopControl.u); |
| 69 |
} |
| 70 |
|
| 71 |
static const parser::Name &GetDoVariable( |
| 72 |
const parser::DoConstruct &doConstruct) { |
| 73 |
const Bounds &bounds{GetBounds(doConstruct)}; |
| 74 |
return bounds.name.thing; |
| 75 |
} |
| 76 |
|
| 77 |
static parser::MessageFixedText GetEnclosingDoMsg() { |
| 78 |
return "Enclosing DO CONCURRENT statement"_en_US; |
| 79 |
} |
| 80 |
|
| 81 |
static void SayWithDo(SemanticsContext &context, parser::CharBlock stmtLocation, |
| 82 |
parser::MessageFixedText &&message, parser::CharBlock doLocation) { |
| 83 |
context.Say(stmtLocation, message).Attach(doLocation, GetEnclosingDoMsg()); |
| 84 |
} |
| 85 |
|
| 86 |
// 11.1.7.5 - enforce semantics constraints on a DO CONCURRENT loop body |
| 87 |
class DoConcurrentBodyEnforce { |
| 88 |
public: |
| 89 |
DoConcurrentBodyEnforce( |
| 90 |
SemanticsContext &context, parser::CharBlock doConcurrentSourcePosition) |
| 91 |
: context_{context}, |
| 92 |
doConcurrentSourcePosition_{doConcurrentSourcePosition} {} |
| 93 |
std::set<parser::Label> labels() { return labels_; } |
| 94 |
template <typename T> bool Pre(const T &x) { |
| 95 |
if (const auto *expr{GetExpr(context_, x)}) { |
| 96 |
if (auto bad{FindImpureCall(context_.foldingContext(), *expr)}) { |
| 97 |
context_.Say(currentStatementSourcePosition_, |
| 98 |
"Impure procedure '%s' may not be referenced in DO CONCURRENT"_err_en_US, |
| 99 |
*bad); |
| 100 |
} |
| 101 |
} |
| 102 |
return true; |
| 103 |
} |
| 104 |
template <typename T> bool Pre(const parser::Statement<T> &statement) { |
| 105 |
currentStatementSourcePosition_ = statement.source; |
| 106 |
if (statement.label.has_value()) { |
| 107 |
labels_.insert(*statement.label); |
| 108 |
} |
| 109 |
return true; |
| 110 |
} |
| 111 |
template <typename T> bool Pre(const parser::UnlabeledStatement<T> &stmt) { |
| 112 |
currentStatementSourcePosition_ = stmt.source; |
| 113 |
return true; |
| 114 |
} |
| 115 |
bool Pre(const parser::CallStmt &x) { |
| 116 |
if (x.typedCall.get()) { |
| 117 |
if (auto bad{FindImpureCall(context_.foldingContext(), *x.typedCall)}) { |
| 118 |
context_.Say(currentStatementSourcePosition_, |
| 119 |
"Impure procedure '%s' may not be referenced in DO CONCURRENT"_err_en_US, |
| 120 |
*bad); |
| 121 |
} |
| 122 |
} |
| 123 |
return true; |
| 124 |
} |
| 125 |
template <typename T> void Post(const T &) {} |
| 126 |
|
| 127 |
// C1140 -- Can't deallocate a polymorphic entity in a DO CONCURRENT. |
| 128 |
// Deallocation can be caused by exiting a block that declares an allocatable |
| 129 |
// entity, assignment to an allocatable variable, or an actual DEALLOCATE |
| 130 |
// statement |
| 131 |
// |
| 132 |
// Note also that the deallocation of a derived type entity might cause the |
| 133 |
// invocation of an IMPURE final subroutine. (C1139) |
| 134 |
// |
| 135 |
|
| 136 |
// Predicate for deallocations caused by block exit and direct deallocation |
| 137 |
static bool DeallocateAll(const Symbol &) { return true; } |
| 138 |
|
| 139 |
// Predicate for deallocations caused by intrinsic assignment |
| 140 |
static bool DeallocateNonCoarray(const Symbol &component) { |
| 141 |
return !evaluate::IsCoarray(component); |
| 142 |
} |
| 143 |
|
| 144 |
static bool WillDeallocatePolymorphic(const Symbol &entity, |
| 145 |
const std::function<bool(const Symbol &)> &WillDeallocate) { |
| 146 |
return WillDeallocate(entity) && IsPolymorphicAllocatable(entity); |
| 147 |
} |
| 148 |
|
| 149 |
// Is it possible that we will we deallocate a polymorphic entity or one |
| 150 |
// of its components? |
| 151 |
static bool MightDeallocatePolymorphic(const Symbol &original, |
| 152 |
const std::function<bool(const Symbol &)> &WillDeallocate) { |
| 153 |
const Symbol &symbol{ResolveAssociations(original)}; |
| 154 |
// Check the entity itself, no coarray exception here |
| 155 |
if (IsPolymorphicAllocatable(symbol)) { |
| 156 |
return true; |
| 157 |
} |
| 158 |
// Check the components |
| 159 |
if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) { |
| 160 |
if (const DeclTypeSpec * entityType{details->type()}) { |
| 161 |
if (const DerivedTypeSpec * derivedType{entityType->AsDerived()}) { |
| 162 |
UltimateComponentIterator ultimates{*derivedType}; |
| 163 |
for (const auto &ultimate : ultimates) { |
| 164 |
if (WillDeallocatePolymorphic(ultimate, WillDeallocate)) { |
| 165 |
return true; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
void SayDeallocateWithImpureFinal( |
| 175 |
const Symbol &entity, const char *reason, const Symbol &impure) { |
| 176 |
context_.SayWithDecl(entity, currentStatementSourcePosition_, |
| 177 |
"Deallocation of an entity with an IMPURE FINAL procedure '%s' caused by %s not allowed in DO CONCURRENT"_err_en_US, |
| 178 |
impure.name(), reason); |
| 179 |
} |
| 180 |
|
| 181 |
void SayDeallocateOfPolymorph( |
| 182 |
parser::CharBlock location, const Symbol &entity, const char *reason) { |
| 183 |
context_.SayWithDecl(entity, location, |
| 184 |
"Deallocation of a polymorphic entity caused by %s" |
| 185 |
" not allowed in DO CONCURRENT"_err_en_US, |
| 186 |
reason); |
| 187 |
} |
| 188 |
|
| 189 |
// Deallocation caused by block exit |
| 190 |
// Allocatable entities and all of their allocatable subcomponents will be |
| 191 |
// deallocated. This test is different from the other two because it does |
| 192 |
// not deallocate in cases where the entity itself is not allocatable but |
| 193 |
// has allocatable polymorphic components |
| 194 |
void Post(const parser::BlockConstruct &blockConstruct) { |
| 195 |
const auto &endBlockStmt{ |
| 196 |
std::get<parser::Statement<parser::EndBlockStmt>>(blockConstruct.t)}; |
| 197 |
const Scope &blockScope{context_.FindScope(endBlockStmt.source)}; |
| 198 |
const Scope &doScope{context_.FindScope(doConcurrentSourcePosition_)}; |
| 199 |
if (DoesScopeContain(&doScope, blockScope)) { |
| 200 |
const char *reason{"block exit"}; |
| 201 |
for (auto &pair : blockScope) { |
| 202 |
const Symbol &entity{*pair.second}; |
| 203 |
if (IsAllocatable(entity) && !IsSaved(entity) && |
| 204 |
MightDeallocatePolymorphic(entity, DeallocateAll)) { |
| 205 |
SayDeallocateOfPolymorph(endBlockStmt.source, entity, reason); |
| 206 |
} |
| 207 |
if (const Symbol * impure{HasImpureFinal(entity)}) { |
| 208 |
SayDeallocateWithImpureFinal(entity, reason, *impure); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
// Deallocation caused by assignment |
| 215 |
// Note that this case does not cause deallocation of coarray components |
| 216 |
void Post(const parser::AssignmentStmt &stmt) { |
| 217 |
const auto &variable{std::get<parser::Variable>(stmt.t)}; |
| 218 |
if (const Symbol * entity{GetLastName(variable).symbol}) { |
| 219 |
const char *reason{"assignment"}; |
| 220 |
if (MightDeallocatePolymorphic(*entity, DeallocateNonCoarray)) { |
| 221 |
SayDeallocateOfPolymorph(variable.GetSource(), *entity, reason); |
| 222 |
} |
| 223 |
if (const auto *assignment{GetAssignment(stmt)}) { |
| 224 |
const auto &lhs{assignment->lhs}; |
| 225 |
if (const Symbol * impure{HasImpureFinal(*entity, lhs.Rank())}) { |
| 226 |
SayDeallocateWithImpureFinal(*entity, reason, *impure); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
if (const auto *assignment{GetAssignment(stmt)}) { |
| 231 |
if (const auto *call{ |
| 232 |
std::get_if<evaluate::ProcedureRef>(&assignment->u)}) { |
| 233 |
if (auto bad{FindImpureCall(context_.foldingContext(), *call)}) { |
| 234 |
context_.Say(currentStatementSourcePosition_, |
| 235 |
"The defined assignment subroutine '%s' is not pure"_err_en_US, |
| 236 |
*bad); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
// Deallocation from a DEALLOCATE statement |
| 243 |
// This case is different because DEALLOCATE statements deallocate both |
| 244 |
// ALLOCATABLE and POINTER entities |
| 245 |
void Post(const parser::DeallocateStmt &stmt) { |
| 246 |
const auto &allocateObjectList{ |
| 247 |
std::get<std::list<parser::AllocateObject>>(stmt.t)}; |
| 248 |
for (const auto &allocateObject : allocateObjectList) { |
| 249 |
const parser::Name &name{GetLastName(allocateObject)}; |
| 250 |
const char *reason{"a DEALLOCATE statement"}; |
| 251 |
if (name.symbol) { |
| 252 |
const Symbol &entity{*name.symbol}; |
| 253 |
const DeclTypeSpec *entityType{entity.GetType()}; |
| 254 |
if ((entityType && entityType->IsPolymorphic()) || // POINTER case |
| 255 |
MightDeallocatePolymorphic(entity, DeallocateAll)) { |
| 256 |
SayDeallocateOfPolymorph( |
| 257 |
currentStatementSourcePosition_, entity, reason); |
| 258 |
} |
| 259 |
if (const Symbol * impure{HasImpureFinal(entity)}) { |
| 260 |
SayDeallocateWithImpureFinal(entity, reason, *impure); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// C1137 -- No image control statements in a DO CONCURRENT |
| 267 |
void Post(const parser::ExecutableConstruct &construct) { |
| 268 |
if (IsImageControlStmt(construct)) { |
| 269 |
const parser::CharBlock statementLocation{ |
| 270 |
GetImageControlStmtLocation(construct)}; |
| 271 |
auto &msg{context_.Say(statementLocation, |
| 272 |
"An image control statement is not allowed in DO" |
| 273 |
" CONCURRENT"_err_en_US)}; |
| 274 |
if (auto coarrayMsg{GetImageControlStmtCoarrayMsg(construct)}) { |
| 275 |
msg.Attach(statementLocation, *coarrayMsg); |
| 276 |
} |
| 277 |
msg.Attach(doConcurrentSourcePosition_, GetEnclosingDoMsg()); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
// C1136 -- No RETURN statements in a DO CONCURRENT |
| 282 |
void Post(const parser::ReturnStmt &) { |
| 283 |
context_ |
| 284 |
.Say(currentStatementSourcePosition_, |
| 285 |
"RETURN is not allowed in DO CONCURRENT"_err_en_US) |
| 286 |
.Attach(doConcurrentSourcePosition_, GetEnclosingDoMsg()); |
| 287 |
} |
| 288 |
|
| 289 |
// C1139: call to impure procedure and ... |
| 290 |
// C1141: cannot call ieee_get_flag, ieee_[gs]et_halting_mode |
| 291 |
// It's not necessary to check the ieee_get* procedures because they're |
| 292 |
// not pure, and impure procedures are caught by checks for constraint C1139 |
| 293 |
void Post(const parser::ProcedureDesignator &procedureDesignator) { |
| 294 |
if (auto *name{std::get_if<parser::Name>(&procedureDesignator.u)}) { |
| 295 |
if (name->symbol && |
| 296 |
fromScope(*name->symbol, "__fortran_ieee_exceptions"s)) { |
| 297 |
if (name->source == "ieee_set_halting_mode") { |
| 298 |
SayWithDo(context_, currentStatementSourcePosition_, |
| 299 |
"IEEE_SET_HALTING_MODE is not allowed in DO " |
| 300 |
"CONCURRENT"_err_en_US, |
| 301 |
doConcurrentSourcePosition_); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
// 11.1.7.5, paragraph 5, no ADVANCE specifier in a DO CONCURRENT |
| 308 |
void Post(const parser::IoControlSpec &ioControlSpec) { |
| 309 |
if (auto *charExpr{ |
| 310 |
std::get_if<parser::IoControlSpec::CharExpr>(&ioControlSpec.u)}) { |
| 311 |
if (std::get<parser::IoControlSpec::CharExpr::Kind>(charExpr->t) == |
| 312 |
parser::IoControlSpec::CharExpr::Kind::Advance) { |
| 313 |
SayWithDo(context_, currentStatementSourcePosition_, |
| 314 |
"ADVANCE specifier is not allowed in DO" |
| 315 |
" CONCURRENT"_err_en_US, |
| 316 |
doConcurrentSourcePosition_); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
private: |
| 322 |
bool fromScope(const Symbol &symbol, const std::string &moduleName) { |
| 323 |
if (symbol.GetUltimate().owner().IsModule() && |
| 324 |
symbol.GetUltimate().owner().GetName().value().ToString() == |
| 325 |
moduleName) { |
| 326 |
return true; |
| 327 |
} |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
std::set<parser::Label> labels_; |
| 332 |
parser::CharBlock currentStatementSourcePosition_; |
| 333 |
SemanticsContext &context_; |
| 334 |
parser::CharBlock doConcurrentSourcePosition_; |
| 335 |
}; // class DoConcurrentBodyEnforce |
| 336 |
|
| 337 |
// Class for enforcing C1130 -- in a DO CONCURRENT with DEFAULT(NONE), |
| 338 |
// variables from enclosing scopes must have their locality specified |
| 339 |
class DoConcurrentVariableEnforce { |
| 340 |
public: |
| 341 |
DoConcurrentVariableEnforce( |
| 342 |
SemanticsContext &context, parser::CharBlock doConcurrentSourcePosition) |
| 343 |
: context_{context}, |
| 344 |
doConcurrentSourcePosition_{doConcurrentSourcePosition}, |
| 345 |
blockScope_{context.FindScope(doConcurrentSourcePosition_)} {} |
| 346 |
|
| 347 |
template <typename T> bool Pre(const T &) { return true; } |
| 348 |
template <typename T> void Post(const T &) {} |
| 349 |
|
| 350 |
// Check to see if the name is a variable from an enclosing scope |
| 351 |
void Post(const parser::Name &name) { |
| 352 |
if (const Symbol * symbol{name.symbol}) { |
| 353 |
if (IsVariableName(*symbol)) { |
| 354 |
const Scope &variableScope{symbol->owner()}; |
| 355 |
if (DoesScopeContain(&variableScope, blockScope_)) { |
| 356 |
context_.SayWithDecl(*symbol, name.source, |
| 357 |
"Variable '%s' from an enclosing scope referenced in DO " |
| 358 |
"CONCURRENT with DEFAULT(NONE) must appear in a " |
| 359 |
"locality-spec"_err_en_US, |
| 360 |
symbol->name()); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
private: |
| 367 |
SemanticsContext &context_; |
| 368 |
parser::CharBlock doConcurrentSourcePosition_; |
| 369 |
const Scope &blockScope_; |
| 370 |
}; // class DoConcurrentVariableEnforce |
| 371 |
|
| 372 |
// Find a DO or FORALL and enforce semantics checks on its body |
| 373 |
class DoContext { |
| 374 |
public: |
| 375 |
DoContext(SemanticsContext &context, IndexVarKind kind) |
| 376 |
: context_{context}, kind_{kind} {} |
| 377 |
|
| 378 |
// Mark this DO construct as a point of definition for the DO variables |
| 379 |
// or index-names it contains. If they're already defined, emit an error |
| 380 |
// message. We need to remember both the variable and the source location of |
| 381 |
// the variable in the DO construct so that we can remove it when we leave |
| 382 |
// the DO construct and use its location in error messages. |
| 383 |
void DefineDoVariables(const parser::DoConstruct &doConstruct) { |
| 384 |
if (doConstruct.IsDoNormal()) { |
| 385 |
context_.ActivateIndexVar(GetDoVariable(doConstruct), IndexVarKind::DO); |
| 386 |
} else if (doConstruct.IsDoConcurrent()) { |
| 387 |
if (const auto &loopControl{doConstruct.GetLoopControl()}) { |
| 388 |
ActivateIndexVars(GetControls(*loopControl)); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
// Called at the end of a DO construct to deactivate the DO construct |
| 394 |
void ResetDoVariables(const parser::DoConstruct &doConstruct) { |
| 395 |
if (doConstruct.IsDoNormal()) { |
| 396 |
context_.DeactivateIndexVar(GetDoVariable(doConstruct)); |
| 397 |
} else if (doConstruct.IsDoConcurrent()) { |
| 398 |
if (const auto &loopControl{doConstruct.GetLoopControl()}) { |
| 399 |
DeactivateIndexVars(GetControls(*loopControl)); |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
void ActivateIndexVars(const std::list<parser::ConcurrentControl> &controls) { |
| 405 |
for (const auto &control : controls) { |
| 406 |
context_.ActivateIndexVar(std::get<parser::Name>(control.t), kind_); |
| 407 |
} |
| 408 |
} |
| 409 |
void DeactivateIndexVars( |
| 410 |
const std::list<parser::ConcurrentControl> &controls) { |
| 411 |
for (const auto &control : controls) { |
| 412 |
context_.DeactivateIndexVar(std::get<parser::Name>(control.t)); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
void Check(const parser::DoConstruct &doConstruct) { |
| 417 |
if (doConstruct.IsDoConcurrent()) { |
| 418 |
CheckDoConcurrent(doConstruct); |
| 419 |
} else if (doConstruct.IsDoNormal()) { |
| 420 |
CheckDoNormal(doConstruct); |
| 421 |
} else { |
| 422 |
// TODO: handle the other cases |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
void Check(const parser::ForallStmt &stmt) { |
| 427 |
CheckConcurrentHeader(GetConcurrentHeader(stmt)); |
| 428 |
} |
| 429 |
void Check(const parser::ForallConstruct &construct) { |
| 430 |
CheckConcurrentHeader(GetConcurrentHeader(construct)); |
| 431 |
} |
| 432 |
|
| 433 |
void Check(const parser::ForallAssignmentStmt &stmt) { |
| 434 |
if (const evaluate::Assignment * |
| 435 |
assignment{common::visit( |
| 436 |
common::visitors{[&](const auto &x) { return GetAssignment(x); }}, |
| 437 |
stmt.u)}) { |
| 438 |
CheckForallIndexesUsed(*assignment); |
| 439 |
CheckForImpureCall(assignment->lhs); |
| 440 |
CheckForImpureCall(assignment->rhs); |
| 441 |
|
| 442 |
if (IsVariable(assignment->lhs)) { |
| 443 |
if (const Symbol * symbol{GetLastSymbol(assignment->lhs)}) { |
| 444 |
if (auto impureFinal{ |
| 445 |
HasImpureFinal(*symbol, assignment->lhs.Rank())}) { |
| 446 |
context_.SayWithDecl(*symbol, parser::FindSourceLocation(stmt), |
| 447 |
"Impure procedure '%s' is referenced by finalization in a %s"_err_en_US, |
| 448 |
impureFinal->name(), LoopKindName()); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
if (const auto *proc{ |
| 454 |
std::get_if<evaluate::ProcedureRef>(&assignment->u)}) { |
| 455 |
CheckForImpureCall(*proc); |
| 456 |
} |
| 457 |
common::visit( |
| 458 |
common::visitors{ |
| 459 |
[](const evaluate::Assignment::Intrinsic &) {}, |
| 460 |
[&](const evaluate::ProcedureRef &proc) { |
| 461 |
CheckForImpureCall(proc); |
| 462 |
}, |
| 463 |
[&](const evaluate::Assignment::BoundsSpec &bounds) { |
| 464 |
for (const auto &bound : bounds) { |
| 465 |
CheckForImpureCall(SomeExpr{bound}); |
| 466 |
} |
| 467 |
}, |
| 468 |
[&](const evaluate::Assignment::BoundsRemapping &bounds) { |
| 469 |
for (const auto &bound : bounds) { |
| 470 |
CheckForImpureCall(SomeExpr{bound.first}); |
| 471 |
CheckForImpureCall(SomeExpr{bound.second}); |
| 472 |
} |
| 473 |
}, |
| 474 |
}, |
| 475 |
assignment->u); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
private: |
| 480 |
void SayBadDoControl(parser::CharBlock sourceLocation) { |
| 481 |
context_.Say(sourceLocation, "DO controls should be INTEGER"_err_en_US); |
| 482 |
} |
| 483 |
|
| 484 |
void CheckDoControl(const parser::CharBlock &sourceLocation, bool isReal) { |
| 485 |
if (isReal) { |
| 486 |
if (context_.ShouldWarn(common::LanguageFeature::RealDoControls)) { |
| 487 |
context_.Say( |
| 488 |
sourceLocation, "DO controls should be INTEGER"_port_en_US); |
| 489 |
} |
| 490 |
} else { |
| 491 |
SayBadDoControl(sourceLocation); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
void CheckDoVariable(const parser::ScalarName &scalarName) { |
| 496 |
const parser::CharBlock &sourceLocation{scalarName.thing.source}; |
| 497 |
if (const Symbol * symbol{scalarName.thing.symbol}) { |
| 498 |
if (!IsVariableName(*symbol)) { |
| 499 |
context_.Say( |
| 500 |
sourceLocation, "DO control must be an INTEGER variable"_err_en_US); |
| 501 |
} else if (auto why{WhyNotDefinable(sourceLocation, |
| 502 |
context_.FindScope(sourceLocation), DefinabilityFlags{}, |
| 503 |
*symbol)}) { |
| 504 |
context_ |
| 505 |
.Say(sourceLocation, |
| 506 |
"'%s' may not be used as a DO variable"_err_en_US, |
| 507 |
symbol->name()) |
| 508 |
.Attach(std::move(why->set_severity(parser::Severity::Because))); |
| 509 |
} else { |
| 510 |
const DeclTypeSpec *symType{symbol->GetType()}; |
| 511 |
if (!symType) { |
| 512 |
SayBadDoControl(sourceLocation); |
| 513 |
} else { |
| 514 |
if (!symType->IsNumeric(TypeCategory::Integer)) { |
| 515 |
CheckDoControl( |
| 516 |
sourceLocation, symType->IsNumeric(TypeCategory::Real)); |
| 517 |
} |
| 518 |
} |
| 519 |
} // No messages for INTEGER |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
// Semantic checks for the limit and step expressions |
| 524 |
void CheckDoExpression(const parser::ScalarExpr &scalarExpression) { |
| 525 |
if (const SomeExpr * expr{GetExpr(context_, scalarExpression)}) { |
| 526 |
if (!ExprHasTypeCategory(*expr, TypeCategory::Integer)) { |
| 527 |
// No warnings or errors for type INTEGER |
| 528 |
const parser::CharBlock &loc{scalarExpression.thing.value().source}; |
| 529 |
CheckDoControl(loc, ExprHasTypeCategory(*expr, TypeCategory::Real)); |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
void CheckDoNormal(const parser::DoConstruct &doConstruct) { |
| 535 |
// C1120 -- types of DO variables must be INTEGER, extended by allowing |
| 536 |
// REAL and DOUBLE PRECISION |
| 537 |
const Bounds &bounds{GetBounds(doConstruct)}; |
| 538 |
CheckDoVariable(bounds.name); |
| 539 |
CheckDoExpression(bounds.lower); |
| 540 |
CheckDoExpression(bounds.upper); |
| 541 |
if (bounds.step) { |
| 542 |
CheckDoExpression(*bounds.step); |
| 543 |
if (IsZero(*bounds.step) && |
| 544 |
context_.ShouldWarn(common::UsageWarning::ZeroDoStep)) { |
| 545 |
context_.Say(bounds.step->thing.value().source, |
| 546 |
"DO step expression should not be zero"_warn_en_US); |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
void CheckDoConcurrent(const parser::DoConstruct &doConstruct) { |
| 552 |
auto &doStmt{ |
| 553 |
std::get<parser::Statement<parser::NonLabelDoStmt>>(doConstruct.t)}; |
| 554 |
currentStatementSourcePosition_ = doStmt.source; |
| 555 |
|
| 556 |
const parser::Block &block{std::get<parser::Block>(doConstruct.t)}; |
| 557 |
DoConcurrentBodyEnforce doConcurrentBodyEnforce{context_, doStmt.source}; |
| 558 |
parser::Walk(block, doConcurrentBodyEnforce); |
| 559 |
|
| 560 |
LabelEnforce doConcurrentLabelEnforce{context_, |
| 561 |
doConcurrentBodyEnforce.labels(), currentStatementSourcePosition_, |
| 562 |
"DO CONCURRENT"}; |
| 563 |
parser::Walk(block, doConcurrentLabelEnforce); |
| 564 |
|
| 565 |
const auto &loopControl{doConstruct.GetLoopControl()}; |
| 566 |
CheckConcurrentLoopControl(*loopControl); |
| 567 |
CheckLocalitySpecs(*loopControl, block); |
| 568 |
} |
| 569 |
|
| 570 |
// Return a set of symbols whose names are in a Local locality-spec. Look |
| 571 |
// the names up in the scope that encloses the DO construct to avoid getting |
| 572 |
// the local versions of them. Then follow the host-, use-, and |
| 573 |
// construct-associations to get the root symbols |
| 574 |
UnorderedSymbolSet GatherLocals( |
| 575 |
const std::list<parser::LocalitySpec> &localitySpecs) const { |
| 576 |
UnorderedSymbolSet symbols; |
| 577 |
const Scope &parentScope{ |
| 578 |
context_.FindScope(currentStatementSourcePosition_).parent()}; |
| 579 |
// Loop through the LocalitySpec::Local locality-specs |
| 580 |
for (const auto &ls : localitySpecs) { |
| 581 |
if (const auto *names{std::get_if<parser::LocalitySpec::Local>(&ls.u)}) { |
| 582 |
// Loop through the names in the Local locality-spec getting their |
| 583 |
// symbols |
| 584 |
for (const parser::Name &name : names->v) { |
| 585 |
if (const Symbol * symbol{parentScope.FindSymbol(name.source)}) { |
| 586 |
symbols.insert(ResolveAssociations(*symbol)); |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
return symbols; |
| 592 |
} |
| 593 |
|
| 594 |
UnorderedSymbolSet GatherSymbolsFromExpression( |
| 595 |
const parser::Expr &expression) const { |
| 596 |
UnorderedSymbolSet result; |
| 597 |
if (const auto *expr{GetExpr(context_, expression)}) { |
| 598 |
for (const Symbol &symbol : evaluate::CollectSymbols(*expr)) { |
| 599 |
result.insert(ResolveAssociations(symbol)); |
| 600 |
} |
| 601 |
} |
| 602 |
return result; |
| 603 |
} |
| 604 |
|
| 605 |
// C1121 - procedures in mask must be pure |
| 606 |
void CheckMaskIsPure(const parser::ScalarLogicalExpr &mask) const { |
| 607 |
UnorderedSymbolSet references{ |
| 608 |
GatherSymbolsFromExpression(mask.thing.thing.value())}; |
| 609 |
for (const Symbol &ref : OrderBySourcePosition(references)) { |
| 610 |
if (IsProcedure(ref) && !IsPureProcedure(ref)) { |
| 611 |
context_.SayWithDecl(ref, parser::Unwrap<parser::Expr>(mask)->source, |
| 612 |
"%s mask expression may not reference impure procedure '%s'"_err_en_US, |
| 613 |
LoopKindName(), ref.name()); |
| 614 |
return; |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
void CheckNoCollisions(const UnorderedSymbolSet &refs, |
| 620 |
const UnorderedSymbolSet &uses, parser::MessageFixedText &&errorMessage, |
| 621 |
const parser::CharBlock &refPosition) const { |
| 622 |
for (const Symbol &ref : OrderBySourcePosition(refs)) { |
| 623 |
if (uses.find(ref) != uses.end()) { |
| 624 |
context_.SayWithDecl(ref, refPosition, std::move(errorMessage), |
| 625 |
LoopKindName(), ref.name()); |
| 626 |
return; |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
void HasNoReferences(const UnorderedSymbolSet &indexNames, |
| 632 |
const parser::ScalarIntExpr &expr) const { |
| 633 |
CheckNoCollisions(GatherSymbolsFromExpression(expr.thing.thing.value()), |
| 634 |
indexNames, |
| 635 |
"%s limit expression may not reference index variable '%s'"_err_en_US, |
| 636 |
expr.thing.thing.value().source); |
| 637 |
} |
| 638 |
|
| 639 |
// C1129, names in local locality-specs can't be in mask expressions |
| 640 |
void CheckMaskDoesNotReferenceLocal(const parser::ScalarLogicalExpr &mask, |
| 641 |
const UnorderedSymbolSet &localVars) const { |
| 642 |
CheckNoCollisions(GatherSymbolsFromExpression(mask.thing.thing.value()), |
| 643 |
localVars, |
| 644 |
"%s mask expression references variable '%s'" |
| 645 |
" in LOCAL locality-spec"_err_en_US, |
| 646 |
mask.thing.thing.value().source); |
| 647 |
} |
| 648 |
|
| 649 |
// C1129, names in local locality-specs can't be in limit or step |
| 650 |
// expressions |
| 651 |
void CheckExprDoesNotReferenceLocal(const parser::ScalarIntExpr &expr, |
| 652 |
const UnorderedSymbolSet &localVars) const { |
| 653 |
CheckNoCollisions(GatherSymbolsFromExpression(expr.thing.thing.value()), |
| 654 |
localVars, |
| 655 |
"%s expression references variable '%s'" |
| 656 |
" in LOCAL locality-spec"_err_en_US, |
| 657 |
expr.thing.thing.value().source); |
| 658 |
} |
| 659 |
|
| 660 |
// C1130, DEFAULT(NONE) locality requires names to be in locality-specs to |
| 661 |
// be used in the body of the DO loop |
| 662 |
void CheckDefaultNoneImpliesExplicitLocality( |
| 663 |
const std::list<parser::LocalitySpec> &localitySpecs, |
| 664 |
const parser::Block &block) const { |
| 665 |
bool hasDefaultNone{false}; |
| 666 |
for (auto &ls : localitySpecs) { |
| 667 |
if (std::holds_alternative<parser::LocalitySpec::DefaultNone>(ls.u)) { |
| 668 |
if (hasDefaultNone) { |
| 669 |
// F'2023 C1129, you can only have one DEFAULT(NONE) |
| 670 |
if (context_.ShouldWarn(common::LanguageFeature::BenignRedundancy)) { |
| 671 |
context_.Say(currentStatementSourcePosition_, |
| 672 |
"Only one DEFAULT(NONE) may appear"_port_en_US); |
| 673 |
} |
| 674 |
break; |
| 675 |
} |
| 676 |
hasDefaultNone = true; |
| 677 |
} |
| 678 |
} |
| 679 |
if (hasDefaultNone) { |
| 680 |
DoConcurrentVariableEnforce doConcurrentVariableEnforce{ |
| 681 |
context_, currentStatementSourcePosition_}; |
| 682 |
parser::Walk(block, doConcurrentVariableEnforce); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
void CheckReduce(const parser::LocalitySpec::Reduce &reduce) const { |
| 687 |
const parser::ReductionOperator &reductionOperator{ |
| 688 |
std::get<parser::ReductionOperator>(reduce.t)}; |
| 689 |
// F'2023 C1132, reduction variables should have suitable intrinsic type |
| 690 |
for (const parser::Name &x : std::get<std::list<parser::Name>>(reduce.t)) { |
| 691 |
bool supportedIdentifier{false}; |
| 692 |
if (x.symbol && x.symbol->GetType()) { |
| 693 |
const auto *type{x.symbol->GetType()}; |
| 694 |
auto typeMismatch{[&](const char *suitable_types) { |
| 695 |
context_.Say(currentStatementSourcePosition_, |
| 696 |
"Reduction variable '%s' ('%s') does not have a suitable type ('%s')."_err_en_US, |
| 697 |
x.symbol->name(), type->AsFortran(), suitable_types); |
| 698 |
}}; |
| 699 |
supportedIdentifier = true; |
| 700 |
switch (reductionOperator.v) { |
| 701 |
case parser::ReductionOperator::Operator::Plus: |
| 702 |
case parser::ReductionOperator::Operator::Multiply: |
| 703 |
if (!(type->IsNumeric(TypeCategory::Complex) || |
| 704 |
type->IsNumeric(TypeCategory::Integer) || |
| 705 |
type->IsNumeric(TypeCategory::Real))) { |
| 706 |
typeMismatch("COMPLEX', 'INTEGER', or 'REAL"); |
| 707 |
} |
| 708 |
break; |
| 709 |
case parser::ReductionOperator::Operator::And: |
| 710 |
case parser::ReductionOperator::Operator::Or: |
| 711 |
case parser::ReductionOperator::Operator::Eqv: |
| 712 |
case parser::ReductionOperator::Operator::Neqv: |
| 713 |
if (type->category() != DeclTypeSpec::Category::Logical) { |
| 714 |
typeMismatch("LOGICAL"); |
| 715 |
} |
| 716 |
break; |
| 717 |
case parser::ReductionOperator::Operator::Max: |
| 718 |
case parser::ReductionOperator::Operator::Min: |
| 719 |
if (!(type->IsNumeric(TypeCategory::Integer) || |
| 720 |
type->IsNumeric(TypeCategory::Real))) { |
| 721 |
typeMismatch("INTEGER', or 'REAL"); |
| 722 |
} |
| 723 |
break; |
| 724 |
case parser::ReductionOperator::Operator::Iand: |
| 725 |
case parser::ReductionOperator::Operator::Ior: |
| 726 |
case parser::ReductionOperator::Operator::Ieor: |
| 727 |
if (!type->IsNumeric(TypeCategory::Integer)) { |
| 728 |
typeMismatch("INTEGER"); |
| 729 |
} |
| 730 |
break; |
| 731 |
} |
| 732 |
} |
| 733 |
if (!supportedIdentifier) { |
| 734 |
context_.Say(currentStatementSourcePosition_, |
| 735 |
"Invalid identifier in REDUCE clause."_err_en_US); |
| 736 |
} |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
// C1123, concurrent limit or step expressions can't reference index-names |
| 741 |
void CheckConcurrentHeader(const parser::ConcurrentHeader &header) const { |
| 742 |
if (const auto &mask{ |
| 743 |
std::get<std::optional<parser::ScalarLogicalExpr>>(header.t)}) { |
| 744 |
CheckMaskIsPure(*mask); |
| 745 |
} |
| 746 |
auto &controls{std::get<std::list<parser::ConcurrentControl>>(header.t)}; |
| 747 |
UnorderedSymbolSet indexNames; |
| 748 |
for (const parser::ConcurrentControl &control : controls) { |
| 749 |
const auto &indexName{std::get<parser::Name>(control.t)}; |
| 750 |
if (indexName.symbol) { |
| 751 |
indexNames.insert(*indexName.symbol); |
| 752 |
} |
| 753 |
} |
| 754 |
if (!indexNames.empty()) { |
| 755 |
for (const parser::ConcurrentControl &control : controls) { |
| 756 |
HasNoReferences(indexNames, std::get<1>(control.t)); |
| 757 |
HasNoReferences(indexNames, std::get<2>(control.t)); |
| 758 |
if (const auto &intExpr{ |
| 759 |
std::get<std::optional<parser::ScalarIntExpr>>(control.t)}) { |
| 760 |
const parser::Expr &expr{intExpr->thing.thing.value()}; |
| 761 |
CheckNoCollisions(GatherSymbolsFromExpression(expr), indexNames, |
| 762 |
"%s step expression may not reference index variable '%s'"_err_en_US, |
| 763 |
expr.source); |
| 764 |
if (IsZero(expr)) { |
| 765 |
context_.Say(expr.source, |
| 766 |
"%s step expression may not be zero"_err_en_US, LoopKindName()); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
void CheckLocalitySpecs( |
| 774 |
const parser::LoopControl &control, const parser::Block &block) const { |
| 775 |
const auto &concurrent{ |
| 776 |
std::get<parser::LoopControl::Concurrent>(control.u)}; |
| 777 |
const auto &header{std::get<parser::ConcurrentHeader>(concurrent.t)}; |
| 778 |
const auto &localitySpecs{ |
| 779 |
std::get<std::list<parser::LocalitySpec>>(concurrent.t)}; |
| 780 |
if (!localitySpecs.empty()) { |
| 781 |
const UnorderedSymbolSet &localVars{GatherLocals(localitySpecs)}; |
| 782 |
for (const auto &c : GetControls(control)) { |
| 783 |
CheckExprDoesNotReferenceLocal(std::get<1>(c.t), localVars); |
| 784 |
CheckExprDoesNotReferenceLocal(std::get<2>(c.t), localVars); |
| 785 |
if (const auto &expr{ |
| 786 |
std::get<std::optional<parser::ScalarIntExpr>>(c.t)}) { |
| 787 |
CheckExprDoesNotReferenceLocal(*expr, localVars); |
| 788 |
} |
| 789 |
} |
| 790 |
if (const auto &mask{ |
| 791 |
std::get<std::optional<parser::ScalarLogicalExpr>>(header.t)}) { |
| 792 |
CheckMaskDoesNotReferenceLocal(*mask, localVars); |
| 793 |
} |
| 794 |
for (auto &ls : localitySpecs) { |
| 795 |
if (const auto *reduce{ |
| 796 |
std::get_if<parser::LocalitySpec::Reduce>(&ls.u)}) { |
| 797 |
CheckReduce(*reduce); |
| 798 |
} |
| 799 |
} |
| 800 |
CheckDefaultNoneImpliesExplicitLocality(localitySpecs, block); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
// check constraints [C1121 .. C1130] |
| 805 |
void CheckConcurrentLoopControl(const parser::LoopControl &control) const { |
| 806 |
const auto &concurrent{ |
| 807 |
std::get<parser::LoopControl::Concurrent>(control.u)}; |
| 808 |
CheckConcurrentHeader(std::get<parser::ConcurrentHeader>(concurrent.t)); |
| 809 |
} |
| 810 |
|
| 811 |
template <typename T> void CheckForImpureCall(const T &x) { |
| 812 |
if (auto bad{FindImpureCall(context_.foldingContext(), x)}) { |
| 813 |
context_.Say( |
| 814 |
"Impure procedure '%s' may not be referenced in a %s"_err_en_US, *bad, |
| 815 |
LoopKindName()); |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
// Each index should be used on the LHS of each assignment in a FORALL |
| 820 |
void CheckForallIndexesUsed(const evaluate::Assignment &assignment) { |
| 821 |
SymbolVector indexVars{context_.GetIndexVars(IndexVarKind::FORALL)}; |
| 822 |
if (!indexVars.empty()) { |
| 823 |
UnorderedSymbolSet symbols{evaluate::CollectSymbols(assignment.lhs)}; |
| 824 |
common::visit( |
| 825 |
common::visitors{ |
| 826 |
[&](const evaluate::Assignment::BoundsSpec &spec) { |
| 827 |
for (const auto &bound : spec) { |
| 828 |
// TODO: this is working around missing std::set::merge in some versions of |
| 829 |
// clang that we are building with |
| 830 |
#ifdef __clang__ |
| 831 |
auto boundSymbols{evaluate::CollectSymbols(bound)}; |
| 832 |
symbols.insert(boundSymbols.begin(), boundSymbols.end()); |
| 833 |
#else |
| 834 |
symbols.merge(evaluate::CollectSymbols(bound)); |
| 835 |
#endif |
| 836 |
} |
| 837 |
}, |
| 838 |
[&](const evaluate::Assignment::BoundsRemapping &remapping) { |
| 839 |
for (const auto &bounds : remapping) { |
| 840 |
#ifdef __clang__ |
| 841 |
auto lbSymbols{evaluate::CollectSymbols(bounds.first)}; |
| 842 |
symbols.insert(lbSymbols.begin(), lbSymbols.end()); |
| 843 |
auto ubSymbols{evaluate::CollectSymbols(bounds.second)}; |
| 844 |
symbols.insert(ubSymbols.begin(), ubSymbols.end()); |
| 845 |
#else |
| 846 |
symbols.merge(evaluate::CollectSymbols(bounds.first)); |
| 847 |
symbols.merge(evaluate::CollectSymbols(bounds.second)); |
| 848 |
#endif |
| 849 |
} |
| 850 |
}, |
| 851 |
[](const auto &) {}, |
| 852 |
}, |
| 853 |
assignment.u); |
| 854 |
for (const Symbol &index : indexVars) { |
| 855 |
if (symbols.count(index) == 0 && |
| 856 |
context_.ShouldWarn(common::UsageWarning::UnusedForallIndex)) { |
| 857 |
context_.Say("FORALL index variable '%s' not used on left-hand side" |
| 858 |
" of assignment"_warn_en_US, |
| 859 |
index.name()); |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
// For messages where the DO loop must be DO CONCURRENT, make that explicit. |
| 866 |
const char *LoopKindName() const { |
| 867 |
return kind_ == IndexVarKind::DO ? "DO CONCURRENT" : "FORALL"; |
| 868 |
} |
| 869 |
|
| 870 |
SemanticsContext &context_; |
| 871 |
const IndexVarKind kind_; |
| 872 |
parser::CharBlock currentStatementSourcePosition_; |
| 873 |
}; // class DoContext |
| 874 |
|
| 875 |
void DoForallChecker::Enter(const parser::DoConstruct &doConstruct) { |
| 876 |
DoContext doContext{context_, IndexVarKind::DO}; |
| 877 |
doContext.DefineDoVariables(doConstruct); |
| 878 |
} |
| 879 |
|
| 880 |
void DoForallChecker::Leave(const parser::DoConstruct &doConstruct) { |
| 881 |
DoContext doContext{context_, IndexVarKind::DO}; |
| 882 |
doContext.Check(doConstruct); |
| 883 |
doContext.ResetDoVariables(doConstruct); |
| 884 |
} |
| 885 |
|
| 886 |
void DoForallChecker::Enter(const parser::ForallConstruct &construct) { |
| 887 |
DoContext doContext{context_, IndexVarKind::FORALL}; |
| 888 |
doContext.ActivateIndexVars(GetControls(construct)); |
| 889 |
} |
| 890 |
void DoForallChecker::Leave(const parser::ForallConstruct &construct) { |
| 891 |
DoContext doContext{context_, IndexVarKind::FORALL}; |
| 892 |
doContext.Check(construct); |
| 893 |
doContext.DeactivateIndexVars(GetControls(construct)); |
| 894 |
} |
| 895 |
|
| 896 |
void DoForallChecker::Enter(const parser::ForallStmt &stmt) { |
| 897 |
DoContext doContext{context_, IndexVarKind::FORALL}; |
| 898 |
doContext.ActivateIndexVars(GetControls(stmt)); |
| 899 |
} |
| 900 |
void DoForallChecker::Leave(const parser::ForallStmt &stmt) { |
| 901 |
DoContext doContext{context_, IndexVarKind::FORALL}; |
| 902 |
doContext.Check(stmt); |
| 903 |
doContext.DeactivateIndexVars(GetControls(stmt)); |
| 904 |
} |
| 905 |
void DoForallChecker::Leave(const parser::ForallAssignmentStmt &stmt) { |
| 906 |
DoContext doContext{context_, IndexVarKind::FORALL}; |
| 907 |
doContext.Check(stmt); |
| 908 |
} |
| 909 |
|
| 910 |
template <typename A> |
| 911 |
static parser::CharBlock GetConstructPosition(const A &a) { |
| 912 |
return std::get<0>(a.t).source; |
| 913 |
} |
| 914 |
|
| 915 |
static parser::CharBlock GetNodePosition(const ConstructNode &construct) { |
| 916 |
return common::visit( |
| 917 |
[&](const auto &x) { return GetConstructPosition(*x); }, construct); |
| 918 |
} |
| 919 |
|
| 920 |
void DoForallChecker::SayBadLeave(StmtType stmtType, |
| 921 |
const char *enclosingStmtName, const ConstructNode &construct) const { |
| 922 |
context_ |
| 923 |
.Say("%s must not leave a %s statement"_err_en_US, EnumToString(stmtType), |
| 924 |
enclosingStmtName) |
| 925 |
.Attach(GetNodePosition(construct), "The construct that was left"_en_US); |
| 926 |
} |
| 927 |
|
| 928 |
static const parser::DoConstruct *MaybeGetDoConstruct( |
| 929 |
const ConstructNode &construct) { |
| 930 |
if (const auto *doNode{ |
| 931 |
std::get_if<const parser::DoConstruct *>(&construct)}) { |
| 932 |
return *doNode; |
| 933 |
} else { |
| 934 |
return nullptr; |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
static bool ConstructIsDoConcurrent(const ConstructNode &construct) { |
| 939 |
const parser::DoConstruct *doConstruct{MaybeGetDoConstruct(construct)}; |
| 940 |
return doConstruct && doConstruct->IsDoConcurrent(); |
| 941 |
} |
| 942 |
|
| 943 |
// Check that CYCLE and EXIT statements do not cause flow of control to |
| 944 |
// leave DO CONCURRENT, CRITICAL, or CHANGE TEAM constructs. |
| 945 |
void DoForallChecker::CheckForBadLeave( |
| 946 |
StmtType stmtType, const ConstructNode &construct) const { |
| 947 |
common::visit(common::visitors{ |
| 948 |
[&](const parser::DoConstruct *doConstructPtr) { |
| 949 |
if (doConstructPtr->IsDoConcurrent()) { |
| 950 |
// C1135 and C1167 -- CYCLE and EXIT statements can't |
| 951 |
// leave a DO CONCURRENT |
| 952 |
SayBadLeave(stmtType, "DO CONCURRENT", construct); |
| 953 |
} |
| 954 |
}, |
| 955 |
[&](const parser::CriticalConstruct *) { |
| 956 |
// C1135 and C1168 -- similarly, for CRITICAL |
| 957 |
SayBadLeave(stmtType, "CRITICAL", construct); |
| 958 |
}, |
| 959 |
[&](const parser::ChangeTeamConstruct *) { |
| 960 |
// C1135 and C1168 -- similarly, for CHANGE TEAM |
| 961 |
SayBadLeave(stmtType, "CHANGE TEAM", construct); |
| 962 |
}, |
| 963 |
[](const auto *) {}, |
| 964 |
}, |
| 965 |
construct); |
| 966 |
} |
| 967 |
|
| 968 |
static bool StmtMatchesConstruct(const parser::Name *stmtName, |
| 969 |
StmtType stmtType, const std::optional<parser::Name> &constructName, |
| 970 |
const ConstructNode &construct) { |
| 971 |
bool inDoConstruct{MaybeGetDoConstruct(construct) != nullptr}; |
| 972 |
if (!stmtName) { |
| 973 |
return inDoConstruct; // Unlabeled statements match all DO constructs |
| 974 |
} else if (constructName && constructName->source == stmtName->source) { |
| 975 |
return stmtType == StmtType::EXIT || inDoConstruct; |
| 976 |
} else { |
| 977 |
return false; |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
// C1167 Can't EXIT from a DO CONCURRENT |
| 982 |
void DoForallChecker::CheckDoConcurrentExit( |
| 983 |
StmtType stmtType, const ConstructNode &construct) const { |
| 984 |
if (stmtType == StmtType::EXIT && ConstructIsDoConcurrent(construct)) { |
| 985 |
SayBadLeave(StmtType::EXIT, "DO CONCURRENT", construct); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
// Check nesting violations for a CYCLE or EXIT statement. Loop up the |
| 990 |
// nesting levels looking for a construct that matches the CYCLE or EXIT |
| 991 |
// statment. At every construct, check for a violation. If we find a match |
| 992 |
// without finding a violation, the check is complete. |
| 993 |
void DoForallChecker::CheckNesting( |
| 994 |
StmtType stmtType, const parser::Name *stmtName) const { |
| 995 |
const ConstructStack &stack{context_.constructStack()}; |
| 996 |
for (auto iter{stack.cend()}; iter-- != stack.cbegin();) { |
| 997 |
const ConstructNode &construct{*iter}; |
| 998 |
const std::optional<parser::Name> &constructName{ |
| 999 |
MaybeGetNodeName(construct)}; |
| 1000 |
if (StmtMatchesConstruct(stmtName, stmtType, constructName, construct)) { |
| 1001 |
CheckDoConcurrentExit(stmtType, construct); |
| 1002 |
return; // We got a match, so we're finished checking |
| 1003 |
} |
| 1004 |
CheckForBadLeave(stmtType, construct); |
| 1005 |
} |
| 1006 |
|
| 1007 |
// We haven't found a match in the enclosing constructs |
| 1008 |
if (stmtType == StmtType::EXIT) { |
| 1009 |
context_.Say("No matching construct for EXIT statement"_err_en_US); |
| 1010 |
} else { |
| 1011 |
context_.Say("No matching DO construct for CYCLE statement"_err_en_US); |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
// C1135 -- Nesting for CYCLE statements |
| 1016 |
void DoForallChecker::Enter(const parser::CycleStmt &cycleStmt) { |
| 1017 |
CheckNesting(StmtType::CYCLE, common::GetPtrFromOptional(cycleStmt.v)); |
| 1018 |
} |
| 1019 |
|
| 1020 |
// C1167 and C1168 -- Nesting for EXIT statements |
| 1021 |
void DoForallChecker::Enter(const parser::ExitStmt &exitStmt) { |
| 1022 |
CheckNesting(StmtType::EXIT, common::GetPtrFromOptional(exitStmt.v)); |
| 1023 |
} |
| 1024 |
|
| 1025 |
void DoForallChecker::Leave(const parser::AssignmentStmt &stmt) { |
| 1026 |
const auto &variable{std::get<parser::Variable>(stmt.t)}; |
| 1027 |
context_.CheckIndexVarRedefine(variable); |
| 1028 |
} |
| 1029 |
|
| 1030 |
static void CheckIfArgIsDoVar(const evaluate::ActualArgument &arg, |
| 1031 |
const parser::CharBlock location, SemanticsContext &context) { |
| 1032 |
common::Intent intent{arg.dummyIntent()}; |
| 1033 |
if (intent == common::Intent::Out || intent == common::Intent::InOut) { |
| 1034 |
if (const SomeExpr * argExpr{arg.UnwrapExpr()}) { |
| 1035 |
if (const Symbol * var{evaluate::UnwrapWholeSymbolDataRef(*argExpr)}) { |
| 1036 |
if (intent == common::Intent::Out) { |
| 1037 |
context.CheckIndexVarRedefine(location, *var); |
| 1038 |
} else { |
| 1039 |
context.WarnIndexVarRedefine(location, *var); // INTENT(INOUT) |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
// Check to see if a DO variable is being passed as an actual argument to a |
| 1047 |
// dummy argument whose intent is OUT or INOUT. To do this, we need to find |
| 1048 |
// the expressions for actual arguments which contain DO variables. We get the |
| 1049 |
// intents of the dummy arguments from the ProcedureRef in the "typedCall" |
| 1050 |
// field of the CallStmt which was filled in during expression checking. At |
| 1051 |
// the same time, we need to iterate over the parser::Expr versions of the |
| 1052 |
// actual arguments to get their source locations of the arguments for the |
| 1053 |
// messages. |
| 1054 |
void DoForallChecker::Leave(const parser::CallStmt &callStmt) { |
| 1055 |
if (const auto &typedCall{callStmt.typedCall}) { |
| 1056 |
const auto &parsedArgs{ |
| 1057 |
std::get<std::list<parser::ActualArgSpec>>(callStmt.call.t)}; |
| 1058 |
auto parsedArgIter{parsedArgs.begin()}; |
| 1059 |
const evaluate::ActualArguments &checkedArgs{typedCall->arguments()}; |
| 1060 |
for (const auto &checkedOptionalArg : checkedArgs) { |
| 1061 |
if (parsedArgIter == parsedArgs.end()) { |
| 1062 |
break; // No more parsed arguments, we're done. |
| 1063 |
} |
| 1064 |
const auto &parsedArg{std::get<parser::ActualArg>(parsedArgIter->t)}; |
| 1065 |
++parsedArgIter; |
| 1066 |
if (checkedOptionalArg) { |
| 1067 |
const evaluate::ActualArgument &checkedArg{*checkedOptionalArg}; |
| 1068 |
if (const auto *parsedExpr{ |
| 1069 |
std::get_if<common::Indirection<parser::Expr>>(&parsedArg.u)}) { |
| 1070 |
CheckIfArgIsDoVar(checkedArg, parsedExpr->value().source, context_); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
void DoForallChecker::Leave(const parser::ConnectSpec &connectSpec) { |
| 1078 |
const auto *newunit{ |
| 1079 |
std::get_if<parser::ConnectSpec::Newunit>(&connectSpec.u)}; |
| 1080 |
if (newunit) { |
| 1081 |
context_.CheckIndexVarRedefine(newunit->v.thing.thing); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
using ActualArgumentSet = std::set<evaluate::ActualArgumentRef>; |
| 1086 |
|
| 1087 |
struct CollectActualArgumentsHelper |
| 1088 |
: public evaluate::SetTraverse<CollectActualArgumentsHelper, |
| 1089 |
ActualArgumentSet> { |
| 1090 |
using Base = SetTraverse<CollectActualArgumentsHelper, ActualArgumentSet>; |
| 1091 |
CollectActualArgumentsHelper() : Base{*this} {} |
| 1092 |
using Base::operator(); |
| 1093 |
ActualArgumentSet operator()(const evaluate::ActualArgument &arg) const { |
| 1094 |
return Combine(ActualArgumentSet{arg}, |
| 1095 |
CollectActualArgumentsHelper{}(arg.UnwrapExpr())); |
| 1096 |
} |
| 1097 |
}; |
| 1098 |
|
| 1099 |
template <typename A> ActualArgumentSet CollectActualArguments(const A &x) { |
| 1100 |
return CollectActualArgumentsHelper{}(x); |
| 1101 |
} |
| 1102 |
|
| 1103 |
template ActualArgumentSet CollectActualArguments(const SomeExpr &); |
| 1104 |
|
| 1105 |
void DoForallChecker::Enter(const parser::Expr &parsedExpr) { ++exprDepth_; } |
| 1106 |
|
| 1107 |
void DoForallChecker::Leave(const parser::Expr &parsedExpr) { |
| 1108 |
CHECK(exprDepth_ > 0); |
| 1109 |
if (--exprDepth_ == 0) { // Only check top level expressions |
| 1110 |
if (const SomeExpr * expr{GetExpr(context_, parsedExpr)}) { |
| 1111 |
ActualArgumentSet argSet{CollectActualArguments(*expr)}; |
| 1112 |
for (const evaluate::ActualArgumentRef &argRef : argSet) { |
| 1113 |
CheckIfArgIsDoVar(*argRef, parsedExpr.source, context_); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
void DoForallChecker::Leave(const parser::InquireSpec &inquireSpec) { |
| 1120 |
const auto *intVar{std::get_if<parser::InquireSpec::IntVar>(&inquireSpec.u)}; |
| 1121 |
if (intVar) { |
| 1122 |
const auto &scalar{std::get<parser::ScalarIntVariable>(intVar->t)}; |
| 1123 |
context_.CheckIndexVarRedefine(scalar.thing.thing); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
void DoForallChecker::Leave(const parser::IoControlSpec &ioControlSpec) { |
| 1128 |
const auto *size{std::get_if<parser::IoControlSpec::Size>(&ioControlSpec.u)}; |
| 1129 |
if (size) { |
| 1130 |
context_.CheckIndexVarRedefine(size->v.thing.thing); |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
void DoForallChecker::Leave(const parser::OutputImpliedDo &outputImpliedDo) { |
| 1135 |
const auto &control{std::get<parser::IoImpliedDoControl>(outputImpliedDo.t)}; |
| 1136 |
const parser::Name &name{control.name.thing.thing}; |
| 1137 |
context_.CheckIndexVarRedefine(name.source, *name.symbol); |
| 1138 |
} |
| 1139 |
|
| 1140 |
void DoForallChecker::Leave(const parser::StatVariable &statVariable) { |
| 1141 |
context_.CheckIndexVarRedefine(statVariable.v.thing.thing); |
| 1142 |
} |
| 1143 |
|
| 1144 |
} // namespace Fortran::semantics |