| 1 |
//===-- lib/Semantics/semantics.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 "flang/Semantics/semantics.h" |
| 10 |
#include "assignment.h" |
| 11 |
#include "canonicalize-acc.h" |
| 12 |
#include "canonicalize-directives.h" |
| 13 |
#include "canonicalize-do.h" |
| 14 |
#include "canonicalize-omp.h" |
| 15 |
#include "check-acc-structure.h" |
| 16 |
#include "check-allocate.h" |
| 17 |
#include "check-arithmeticif.h" |
| 18 |
#include "check-case.h" |
| 19 |
#include "check-coarray.h" |
| 20 |
#include "check-cuda.h" |
| 21 |
#include "check-data.h" |
| 22 |
#include "check-deallocate.h" |
| 23 |
#include "check-declarations.h" |
| 24 |
#include "check-do-forall.h" |
| 25 |
#include "check-if-stmt.h" |
| 26 |
#include "check-io.h" |
| 27 |
#include "check-namelist.h" |
| 28 |
#include "check-nullify.h" |
| 29 |
#include "check-omp-structure.h" |
| 30 |
#include "check-purity.h" |
| 31 |
#include "check-return.h" |
| 32 |
#include "check-select-rank.h" |
| 33 |
#include "check-select-type.h" |
| 34 |
#include "check-stop.h" |
| 35 |
#include "compute-offsets.h" |
| 36 |
#include "mod-file.h" |
| 37 |
#include "resolve-labels.h" |
| 38 |
#include "resolve-names.h" |
| 39 |
#include "rewrite-parse-tree.h" |
| 40 |
#include "flang/Common/default-kinds.h" |
| 41 |
#include "flang/Parser/parse-tree-visitor.h" |
| 42 |
#include "flang/Parser/tools.h" |
| 43 |
#include "flang/Semantics/expression.h" |
| 44 |
#include "flang/Semantics/scope.h" |
| 45 |
#include "flang/Semantics/symbol.h" |
| 46 |
#include "llvm/Support/raw_ostream.h" |
| 47 |
#include "llvm/TargetParser/Host.h" |
| 48 |
#include "llvm/TargetParser/Triple.h" |
| 49 |
|
| 50 |
namespace Fortran::semantics { |
| 51 |
|
| 52 |
using NameToSymbolMap = std::multimap<parser::CharBlock, SymbolRef>; |
| 53 |
static void DoDumpSymbols(llvm::raw_ostream &, const Scope &, int indent = 0); |
| 54 |
static void PutIndent(llvm::raw_ostream &, int indent); |
| 55 |
|
| 56 |
static void GetSymbolNames(const Scope &scope, NameToSymbolMap &symbols) { |
| 57 |
// Finds all symbol names in the scope without collecting duplicates. |
| 58 |
for (const auto &pair : scope) { |
| 59 |
symbols.emplace(pair.second->name(), *pair.second); |
| 60 |
} |
| 61 |
for (const auto &pair : scope.commonBlocks()) { |
| 62 |
symbols.emplace(pair.second->name(), *pair.second); |
| 63 |
} |
| 64 |
for (const auto &child : scope.children()) { |
| 65 |
GetSymbolNames(child, symbols); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// A parse tree visitor that calls Enter/Leave functions from each checker |
| 70 |
// class C supplied as template parameters. Enter is called before the node's |
| 71 |
// children are visited, Leave is called after. No two checkers may have the |
| 72 |
// same Enter or Leave function. Each checker must be constructible from |
| 73 |
// SemanticsContext and have BaseChecker as a virtual base class. |
| 74 |
template <typename... C> |
| 75 |
class SemanticsVisitor : public virtual BaseChecker, public virtual C... { |
| 76 |
public: |
| 77 |
using BaseChecker::Enter; |
| 78 |
using BaseChecker::Leave; |
| 79 |
using C::Enter...; |
| 80 |
using C::Leave...; |
| 81 |
SemanticsVisitor(SemanticsContext &context) |
| 82 |
: C{context}..., context_{context} {} |
| 83 |
|
| 84 |
template <typename N> bool Pre(const N &node) { |
| 85 |
if constexpr (common::HasMember<const N *, ConstructNode>) { |
| 86 |
context_.PushConstruct(node); |
| 87 |
} |
| 88 |
Enter(node); |
| 89 |
return true; |
| 90 |
} |
| 91 |
template <typename N> void Post(const N &node) { |
| 92 |
Leave(node); |
| 93 |
if constexpr (common::HasMember<const N *, ConstructNode>) { |
| 94 |
context_.PopConstruct(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
template <typename T> bool Pre(const parser::Statement<T> &node) { |
| 99 |
context_.set_location(node.source); |
| 100 |
Enter(node); |
| 101 |
return true; |
| 102 |
} |
| 103 |
template <typename T> bool Pre(const parser::UnlabeledStatement<T> &node) { |
| 104 |
context_.set_location(node.source); |
| 105 |
Enter(node); |
| 106 |
return true; |
| 107 |
} |
| 108 |
template <typename T> void Post(const parser::Statement<T> &node) { |
| 109 |
Leave(node); |
| 110 |
context_.set_location(std::nullopt); |
| 111 |
} |
| 112 |
template <typename T> void Post(const parser::UnlabeledStatement<T> &node) { |
| 113 |
Leave(node); |
| 114 |
context_.set_location(std::nullopt); |
| 115 |
} |
| 116 |
|
| 117 |
bool Walk(const parser::Program &program) { |
| 118 |
parser::Walk(program, *this); |
| 119 |
return !context_.AnyFatalError(); |
| 120 |
} |
| 121 |
|
| 122 |
private: |
| 123 |
SemanticsContext &context_; |
| 124 |
}; |
| 125 |
|
| 126 |
class MiscChecker : public virtual BaseChecker { |
| 127 |
public: |
| 128 |
explicit MiscChecker(SemanticsContext &context) : context_{context} {} |
| 129 |
void Leave(const parser::EntryStmt &) { |
| 130 |
if (!context_.constructStack().empty()) { // C1571 |
| 131 |
context_.Say("ENTRY may not appear in an executable construct"_err_en_US); |
| 132 |
} |
| 133 |
} |
| 134 |
void Leave(const parser::AssignStmt &stmt) { |
| 135 |
CheckAssignGotoName(std::get<parser::Name>(stmt.t)); |
| 136 |
} |
| 137 |
void Leave(const parser::AssignedGotoStmt &stmt) { |
| 138 |
CheckAssignGotoName(std::get<parser::Name>(stmt.t)); |
| 139 |
} |
| 140 |
|
| 141 |
private: |
| 142 |
void CheckAssignGotoName(const parser::Name &name) { |
| 143 |
if (context_.HasError(name.symbol)) { |
| 144 |
return; |
| 145 |
} |
| 146 |
const Symbol &symbol{DEREF(name.symbol)}; |
| 147 |
auto type{evaluate::DynamicType::From(symbol)}; |
| 148 |
if (!IsVariableName(symbol) || symbol.Rank() != 0 || !type || |
| 149 |
type->category() != TypeCategory::Integer || |
| 150 |
type->kind() != |
| 151 |
context_.defaultKinds().GetDefaultKind(TypeCategory::Integer)) { |
| 152 |
context_ |
| 153 |
.Say(name.source, |
| 154 |
"'%s' must be a default integer scalar variable"_err_en_US, |
| 155 |
name.source) |
| 156 |
.Attach(symbol.name(), "Declaration of '%s'"_en_US, symbol.name()); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
SemanticsContext &context_; |
| 161 |
}; |
| 162 |
|
| 163 |
using StatementSemanticsPass1 = ExprChecker; |
| 164 |
using StatementSemanticsPass2 = SemanticsVisitor<AllocateChecker, |
| 165 |
ArithmeticIfStmtChecker, AssignmentChecker, CaseChecker, CoarrayChecker, |
| 166 |
DataChecker, DeallocateChecker, DoForallChecker, IfStmtChecker, IoChecker, |
| 167 |
MiscChecker, NamelistChecker, NullifyChecker, PurityChecker, |
| 168 |
ReturnStmtChecker, SelectRankConstructChecker, SelectTypeChecker, |
| 169 |
StopChecker>; |
| 170 |
|
| 171 |
static bool PerformStatementSemantics( |
| 172 |
SemanticsContext &context, parser::Program &program) { |
| 173 |
ResolveNames(context, program, context.globalScope()); |
| 174 |
RewriteParseTree(context, program); |
| 175 |
ComputeOffsets(context, context.globalScope()); |
| 176 |
CheckDeclarations(context); |
| 177 |
StatementSemanticsPass1{context}.Walk(program); |
| 178 |
StatementSemanticsPass2 pass2{context}; |
| 179 |
pass2.Walk(program); |
| 180 |
if (context.languageFeatures().IsEnabled(common::LanguageFeature::OpenACC)) { |
| 181 |
SemanticsVisitor<AccStructureChecker>{context}.Walk(program); |
| 182 |
} |
| 183 |
if (context.languageFeatures().IsEnabled(common::LanguageFeature::OpenMP)) { |
| 184 |
SemanticsVisitor<OmpStructureChecker>{context}.Walk(program); |
| 185 |
} |
| 186 |
if (context.languageFeatures().IsEnabled(common::LanguageFeature::CUDA)) { |
| 187 |
SemanticsVisitor<CUDAChecker>{context}.Walk(program); |
| 188 |
} |
| 189 |
if (!context.AnyFatalError()) { |
| 190 |
pass2.CompileDataInitializationsIntoInitializers(); |
| 191 |
} |
| 192 |
return !context.AnyFatalError(); |
| 193 |
} |
| 194 |
|
| 195 |
/// This class keeps track of the common block appearances with the biggest size |
| 196 |
/// and with an initial value (if any) in a program. This allows reporting |
| 197 |
/// conflicting initialization and warning about appearances of a same |
| 198 |
/// named common block with different sizes. The biggest common block size and |
| 199 |
/// initialization (if any) can later be provided so that lowering can generate |
| 200 |
/// the correct symbol size and initial values, even when named common blocks |
| 201 |
/// appears with different sizes and are initialized outside of block data. |
| 202 |
class CommonBlockMap { |
| 203 |
private: |
| 204 |
struct CommonBlockInfo { |
| 205 |
// Common block symbol for the appearance with the biggest size. |
| 206 |
SymbolRef biggestSize; |
| 207 |
// Common block symbol for the appearance with the initialized members (if |
| 208 |
// any). |
| 209 |
std::optional<SymbolRef> initialization; |
| 210 |
}; |
| 211 |
|
| 212 |
public: |
| 213 |
void MapCommonBlockAndCheckConflicts( |
| 214 |
SemanticsContext &context, const Symbol &common) { |
| 215 |
const Symbol *isInitialized{CommonBlockIsInitialized(common)}; |
| 216 |
// Merge common according to the name they will have in the object files. |
| 217 |
// This allows merging BIND(C) and non BIND(C) common block instead of |
| 218 |
// later crashing. This "merge" matches what ifort/gfortran/nvfortran are |
| 219 |
// doing and what a linker would do if the definition were in distinct |
| 220 |
// files. |
| 221 |
std::string commonName{ |
| 222 |
GetCommonBlockObjectName(common, context.underscoring())}; |
| 223 |
auto [it, firstAppearance] = commonBlocks_.insert({commonName, |
| 224 |
isInitialized ? CommonBlockInfo{common, common} |
| 225 |
: CommonBlockInfo{common, std::nullopt}}); |
| 226 |
if (!firstAppearance) { |
| 227 |
CommonBlockInfo &info{it->second}; |
| 228 |
if (isInitialized) { |
| 229 |
if (info.initialization.has_value() && |
| 230 |
&**info.initialization != &common) { |
| 231 |
// Use the location of the initialization in the error message because |
| 232 |
// common block symbols may have no location if they are blank |
| 233 |
// commons. |
| 234 |
const Symbol &previousInit{ |
| 235 |
DEREF(CommonBlockIsInitialized(**info.initialization))}; |
| 236 |
context |
| 237 |
.Say(isInitialized->name(), |
| 238 |
"Multiple initialization of COMMON block /%s/"_err_en_US, |
| 239 |
common.name()) |
| 240 |
.Attach(previousInit.name(), |
| 241 |
"Previous initialization of COMMON block /%s/"_en_US, |
| 242 |
common.name()); |
| 243 |
} else { |
| 244 |
info.initialization = common; |
| 245 |
} |
| 246 |
} |
| 247 |
if (common.size() != info.biggestSize->size() && !common.name().empty() && |
| 248 |
context.ShouldWarn(common::LanguageFeature::DistinctCommonSizes)) { |
| 249 |
context |
| 250 |
.Say(common.name(), |
| 251 |
"A named COMMON block should have the same size everywhere it appears (%zd bytes here)"_port_en_US, |
| 252 |
common.size()) |
| 253 |
.Attach(info.biggestSize->name(), |
| 254 |
"Previously defined with a size of %zd bytes"_en_US, |
| 255 |
info.biggestSize->size()); |
| 256 |
} |
| 257 |
if (common.size() > info.biggestSize->size()) { |
| 258 |
info.biggestSize = common; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
CommonBlockList GetCommonBlocks() const { |
| 264 |
CommonBlockList result; |
| 265 |
for (const auto &[_, blockInfo] : commonBlocks_) { |
| 266 |
result.emplace_back( |
| 267 |
std::make_pair(blockInfo.initialization ? *blockInfo.initialization |
| 268 |
: blockInfo.biggestSize, |
| 269 |
blockInfo.biggestSize->size())); |
| 270 |
} |
| 271 |
return result; |
| 272 |
} |
| 273 |
|
| 274 |
private: |
| 275 |
/// Return the symbol of an initialized member if a COMMON block |
| 276 |
/// is initalized. Otherwise, return nullptr. |
| 277 |
static Symbol *CommonBlockIsInitialized(const Symbol &common) { |
| 278 |
const auto &commonDetails = |
| 279 |
common.get<Fortran::semantics::CommonBlockDetails>(); |
| 280 |
|
| 281 |
for (const auto &member : commonDetails.objects()) { |
| 282 |
if (IsInitialized(*member)) { |
| 283 |
return &*member; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// Common block may be initialized via initialized variables that are in an |
| 288 |
// equivalence with the common block members. |
| 289 |
for (const Fortran::semantics::EquivalenceSet &set : |
| 290 |
common.owner().equivalenceSets()) { |
| 291 |
for (const Fortran::semantics::EquivalenceObject &obj : set) { |
| 292 |
if (!obj.symbol.test( |
| 293 |
Fortran::semantics::Symbol::Flag::CompilerCreated)) { |
| 294 |
if (FindCommonBlockContaining(obj.symbol) == &common && |
| 295 |
IsInitialized(obj.symbol)) { |
| 296 |
return &obj.symbol; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
return nullptr; |
| 302 |
} |
| 303 |
|
| 304 |
std::map<std::string, CommonBlockInfo> commonBlocks_; |
| 305 |
}; |
| 306 |
|
| 307 |
SemanticsContext::SemanticsContext( |
| 308 |
const common::IntrinsicTypeDefaultKinds &defaultKinds, |
| 309 |
const common::LanguageFeatureControl &languageFeatures, |
| 310 |
parser::AllCookedSources &allCookedSources) |
| 311 |
: defaultKinds_{defaultKinds}, languageFeatures_{languageFeatures}, |
| 312 |
allCookedSources_{allCookedSources}, |
| 313 |
intrinsics_{evaluate::IntrinsicProcTable::Configure(defaultKinds_)}, |
| 314 |
globalScope_{*this}, intrinsicModulesScope_{globalScope_.MakeScope( |
| 315 |
Scope::Kind::IntrinsicModules, nullptr)}, |
| 316 |
foldingContext_{parser::ContextualMessages{&messages_}, defaultKinds_, |
| 317 |
intrinsics_, targetCharacteristics_, languageFeatures_, tempNames_} {} |
| 318 |
|
| 319 |
SemanticsContext::~SemanticsContext() {} |
| 320 |
|
| 321 |
int SemanticsContext::GetDefaultKind(TypeCategory category) const { |
| 322 |
return defaultKinds_.GetDefaultKind(category); |
| 323 |
} |
| 324 |
|
| 325 |
const DeclTypeSpec &SemanticsContext::MakeNumericType( |
| 326 |
TypeCategory category, int kind) { |
| 327 |
if (kind == 0) { |
| 328 |
kind = GetDefaultKind(category); |
| 329 |
} |
| 330 |
return globalScope_.MakeNumericType(category, KindExpr{kind}); |
| 331 |
} |
| 332 |
const DeclTypeSpec &SemanticsContext::MakeLogicalType(int kind) { |
| 333 |
if (kind == 0) { |
| 334 |
kind = GetDefaultKind(TypeCategory::Logical); |
| 335 |
} |
| 336 |
return globalScope_.MakeLogicalType(KindExpr{kind}); |
| 337 |
} |
| 338 |
|
| 339 |
bool SemanticsContext::AnyFatalError() const { |
| 340 |
return !messages_.empty() && |
| 341 |
(warningsAreErrors_ || messages_.AnyFatalError()); |
| 342 |
} |
| 343 |
bool SemanticsContext::HasError(const Symbol &symbol) { |
| 344 |
return errorSymbols_.count(symbol) > 0; |
| 345 |
} |
| 346 |
bool SemanticsContext::HasError(const Symbol *symbol) { |
| 347 |
return !symbol || HasError(*symbol); |
| 348 |
} |
| 349 |
bool SemanticsContext::HasError(const parser::Name &name) { |
| 350 |
return HasError(name.symbol); |
| 351 |
} |
| 352 |
void SemanticsContext::SetError(const Symbol &symbol, bool value) { |
| 353 |
if (value) { |
| 354 |
CheckError(symbol); |
| 355 |
errorSymbols_.emplace(symbol); |
| 356 |
} |
| 357 |
} |
| 358 |
void SemanticsContext::CheckError(const Symbol &symbol) { |
| 359 |
if (!AnyFatalError()) { |
| 360 |
std::string buf; |
| 361 |
llvm::raw_string_ostream ss{buf}; |
| 362 |
ss << symbol; |
| 363 |
common::die( |
| 364 |
"No error was reported but setting error on: %s", ss.str().c_str()); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
bool SemanticsContext::ScopeIndexComparator::operator()( |
| 369 |
parser::CharBlock x, parser::CharBlock y) const { |
| 370 |
return x.begin() < y.begin() || |
| 371 |
(x.begin() == y.begin() && x.size() > y.size()); |
| 372 |
} |
| 373 |
|
| 374 |
auto SemanticsContext::SearchScopeIndex(parser::CharBlock source) |
| 375 |
-> ScopeIndex::iterator { |
| 376 |
if (!scopeIndex_.empty()) { |
| 377 |
auto iter{scopeIndex_.upper_bound(source)}; |
| 378 |
auto begin{scopeIndex_.begin()}; |
| 379 |
do { |
| 380 |
--iter; |
| 381 |
if (iter->first.Contains(source)) { |
| 382 |
return iter; |
| 383 |
} |
| 384 |
} while (iter != begin); |
| 385 |
} |
| 386 |
return scopeIndex_.end(); |
| 387 |
} |
| 388 |
|
| 389 |
const Scope &SemanticsContext::FindScope(parser::CharBlock source) const { |
| 390 |
return const_cast<SemanticsContext *>(this)->FindScope(source); |
| 391 |
} |
| 392 |
|
| 393 |
Scope &SemanticsContext::FindScope(parser::CharBlock source) { |
| 394 |
if (auto iter{SearchScopeIndex(source)}; iter != scopeIndex_.end()) { |
| 395 |
return iter->second; |
| 396 |
} else { |
| 397 |
common::die( |
| 398 |
"SemanticsContext::FindScope(): invalid source location for '%s'", |
| 399 |
source.ToString().c_str()); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
void SemanticsContext::UpdateScopeIndex( |
| 404 |
Scope &scope, parser::CharBlock newSource) { |
| 405 |
if (scope.sourceRange().empty()) { |
| 406 |
scopeIndex_.emplace(newSource, scope); |
| 407 |
} else if (!scope.sourceRange().Contains(newSource)) { |
| 408 |
auto iter{SearchScopeIndex(scope.sourceRange())}; |
| 409 |
CHECK(iter != scopeIndex_.end()); |
| 410 |
while (&iter->second != &scope) { |
| 411 |
CHECK(iter != scopeIndex_.begin()); |
| 412 |
--iter; |
| 413 |
} |
| 414 |
scopeIndex_.erase(iter); |
| 415 |
scopeIndex_.emplace(newSource, scope); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
bool SemanticsContext::IsInModuleFile(parser::CharBlock source) const { |
| 420 |
for (const Scope *scope{&FindScope(source)}; !scope->IsGlobal(); |
| 421 |
scope = &scope->parent()) { |
| 422 |
if (scope->IsModuleFile()) { |
| 423 |
return true; |
| 424 |
} |
| 425 |
} |
| 426 |
return false; |
| 427 |
} |
| 428 |
|
| 429 |
void SemanticsContext::PopConstruct() { |
| 430 |
CHECK(!constructStack_.empty()); |
| 431 |
constructStack_.pop_back(); |
| 432 |
} |
| 433 |
|
| 434 |
void SemanticsContext::CheckIndexVarRedefine(const parser::CharBlock &location, |
| 435 |
const Symbol &variable, parser::MessageFixedText &&message) { |
| 436 |
const Symbol &symbol{ResolveAssociations(variable)}; |
| 437 |
auto it{activeIndexVars_.find(symbol)}; |
| 438 |
if (it != activeIndexVars_.end()) { |
| 439 |
std::string kind{EnumToString(it->second.kind)}; |
| 440 |
Say(location, std::move(message), kind, symbol.name()) |
| 441 |
.Attach(it->second.location, "Enclosing %s construct"_en_US, kind); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
void SemanticsContext::WarnIndexVarRedefine( |
| 446 |
const parser::CharBlock &location, const Symbol &variable) { |
| 447 |
if (ShouldWarn(common::UsageWarning::IndexVarRedefinition)) { |
| 448 |
CheckIndexVarRedefine(location, variable, |
| 449 |
"Possible redefinition of %s variable '%s'"_warn_en_US); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
void SemanticsContext::CheckIndexVarRedefine( |
| 454 |
const parser::CharBlock &location, const Symbol &variable) { |
| 455 |
CheckIndexVarRedefine( |
| 456 |
location, variable, "Cannot redefine %s variable '%s'"_err_en_US); |
| 457 |
} |
| 458 |
|
| 459 |
void SemanticsContext::CheckIndexVarRedefine(const parser::Variable &variable) { |
| 460 |
if (const Symbol * entity{GetLastName(variable).symbol}) { |
| 461 |
CheckIndexVarRedefine(variable.GetSource(), *entity); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
void SemanticsContext::CheckIndexVarRedefine(const parser::Name &name) { |
| 466 |
if (const Symbol * entity{name.symbol}) { |
| 467 |
CheckIndexVarRedefine(name.source, *entity); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
void SemanticsContext::ActivateIndexVar( |
| 472 |
const parser::Name &name, IndexVarKind kind) { |
| 473 |
CheckIndexVarRedefine(name); |
| 474 |
if (const Symbol * indexVar{name.symbol}) { |
| 475 |
activeIndexVars_.emplace( |
| 476 |
ResolveAssociations(*indexVar), IndexVarInfo{name.source, kind}); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
void SemanticsContext::DeactivateIndexVar(const parser::Name &name) { |
| 481 |
if (Symbol * indexVar{name.symbol}) { |
| 482 |
auto it{activeIndexVars_.find(ResolveAssociations(*indexVar))}; |
| 483 |
if (it != activeIndexVars_.end() && it->second.location == name.source) { |
| 484 |
activeIndexVars_.erase(it); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
SymbolVector SemanticsContext::GetIndexVars(IndexVarKind kind) { |
| 490 |
SymbolVector result; |
| 491 |
for (const auto &[symbol, info] : activeIndexVars_) { |
| 492 |
if (info.kind == kind) { |
| 493 |
result.push_back(symbol); |
| 494 |
} |
| 495 |
} |
| 496 |
return result; |
| 497 |
} |
| 498 |
|
| 499 |
SourceName SemanticsContext::SaveTempName(std::string &&name) { |
| 500 |
return {*tempNames_.emplace(std::move(name)).first}; |
| 501 |
} |
| 502 |
|
| 503 |
SourceName SemanticsContext::GetTempName(const Scope &scope) { |
| 504 |
for (const auto &str : tempNames_) { |
| 505 |
if (IsTempName(str)) { |
| 506 |
SourceName name{str}; |
| 507 |
if (scope.find(name) == scope.end()) { |
| 508 |
return name; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
return SaveTempName(".F18."s + std::to_string(tempNames_.size())); |
| 513 |
} |
| 514 |
|
| 515 |
bool SemanticsContext::IsTempName(const std::string &name) { |
| 516 |
return name.size() > 5 && name.substr(0, 5) == ".F18."; |
| 517 |
} |
| 518 |
|
| 519 |
Scope *SemanticsContext::GetBuiltinModule(const char *name) { |
| 520 |
return ModFileReader{*this}.Read(SourceName{name, std::strlen(name)}, |
| 521 |
true /*intrinsic*/, nullptr, /*silent=*/true); |
| 522 |
} |
| 523 |
|
| 524 |
void SemanticsContext::UseFortranBuiltinsModule() { |
| 525 |
if (builtinsScope_ == nullptr) { |
| 526 |
builtinsScope_ = GetBuiltinModule("__fortran_builtins"); |
| 527 |
if (builtinsScope_) { |
| 528 |
intrinsics_.SupplyBuiltins(*builtinsScope_); |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
void SemanticsContext::UsePPCBuiltinTypesModule() { |
| 534 |
if (ppcBuiltinTypesScope_ == nullptr) { |
| 535 |
ppcBuiltinTypesScope_ = GetBuiltinModule("__ppc_types"); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
const Scope &SemanticsContext::GetCUDABuiltinsScope() { |
| 540 |
if (!cudaBuiltinsScope_) { |
| 541 |
cudaBuiltinsScope_ = GetBuiltinModule("__cuda_builtins"); |
| 542 |
CHECK(cudaBuiltinsScope_.value() != nullptr); |
| 543 |
} |
| 544 |
return **cudaBuiltinsScope_; |
| 545 |
} |
| 546 |
|
| 547 |
const Scope &SemanticsContext::GetCUDADeviceScope() { |
| 548 |
if (!cudaDeviceScope_) { |
| 549 |
cudaDeviceScope_ = GetBuiltinModule("cudadevice"); |
| 550 |
CHECK(cudaDeviceScope_.value() != nullptr); |
| 551 |
} |
| 552 |
return **cudaDeviceScope_; |
| 553 |
} |
| 554 |
|
| 555 |
void SemanticsContext::UsePPCBuiltinsModule() { |
| 556 |
if (ppcBuiltinsScope_ == nullptr) { |
| 557 |
ppcBuiltinsScope_ = GetBuiltinModule("__ppc_intrinsics"); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
parser::Program &SemanticsContext::SaveParseTree(parser::Program &&tree) { |
| 562 |
return modFileParseTrees_.emplace_back(std::move(tree)); |
| 563 |
} |
| 564 |
|
| 565 |
bool Semantics::Perform() { |
| 566 |
// Implicitly USE the __Fortran_builtins module so that special types |
| 567 |
// (e.g., __builtin_team_type) are available to semantics, esp. for |
| 568 |
// intrinsic checking. |
| 569 |
if (!program_.v.empty()) { |
| 570 |
const auto *frontModule{std::get_if<common::Indirection<parser::Module>>( |
| 571 |
&program_.v.front().u)}; |
| 572 |
if (frontModule && |
| 573 |
(std::get<parser::Statement<parser::ModuleStmt>>(frontModule->value().t) |
| 574 |
.statement.v.source == "__fortran_builtins" || |
| 575 |
std::get<parser::Statement<parser::ModuleStmt>>( |
| 576 |
frontModule->value().t) |
| 577 |
.statement.v.source == "__ppc_types")) { |
| 578 |
// Don't try to read the builtins module when we're actually building it. |
| 579 |
} else if (frontModule && |
| 580 |
(std::get<parser::Statement<parser::ModuleStmt>>(frontModule->value().t) |
| 581 |
.statement.v.source == "__ppc_intrinsics" || |
| 582 |
std::get<parser::Statement<parser::ModuleStmt>>( |
| 583 |
frontModule->value().t) |
| 584 |
.statement.v.source == "mma")) { |
| 585 |
// The derived type definition for the vectors is needed. |
| 586 |
context_.UsePPCBuiltinTypesModule(); |
| 587 |
} else { |
| 588 |
context_.UseFortranBuiltinsModule(); |
| 589 |
llvm::Triple targetTriple{llvm::Triple( |
| 590 |
llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple()))}; |
| 591 |
// Only use __ppc_intrinsics module when targetting PowerPC arch |
| 592 |
if (context_.targetCharacteristics().isPPC()) { |
| 593 |
context_.UsePPCBuiltinTypesModule(); |
| 594 |
context_.UsePPCBuiltinsModule(); |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
return ValidateLabels(context_, program_) && |
| 599 |
parser::CanonicalizeDo(program_) && // force line break |
| 600 |
CanonicalizeAcc(context_.messages(), program_) && |
| 601 |
CanonicalizeOmp(context_.messages(), program_) && |
| 602 |
CanonicalizeCUDA(program_) && |
| 603 |
CanonicalizeDirectives(context_.messages(), program_) && |
| 604 |
PerformStatementSemantics(context_, program_) && |
| 605 |
ModFileWriter{context_} |
| 606 |
.set_hermeticModuleFileOutput(hermeticModuleFileOutput_) |
| 607 |
.WriteAll(); |
| 608 |
} |
| 609 |
|
| 610 |
void Semantics::EmitMessages(llvm::raw_ostream &os) { |
| 611 |
// Resolve the CharBlock locations of the Messages to ProvenanceRanges |
| 612 |
// so messages from parsing and semantics are intermixed in source order. |
| 613 |
context_.messages().ResolveProvenances(context_.allCookedSources()); |
| 614 |
context_.messages().Emit(os, context_.allCookedSources()); |
| 615 |
} |
| 616 |
|
| 617 |
void Semantics::DumpSymbols(llvm::raw_ostream &os) { |
| 618 |
DoDumpSymbols(os, context_.globalScope()); |
| 619 |
} |
| 620 |
|
| 621 |
void Semantics::DumpSymbolsSources(llvm::raw_ostream &os) const { |
| 622 |
NameToSymbolMap symbols; |
| 623 |
GetSymbolNames(context_.globalScope(), symbols); |
| 624 |
const parser::AllCookedSources &allCooked{context_.allCookedSources()}; |
| 625 |
for (const auto &pair : symbols) { |
| 626 |
const Symbol &symbol{pair.second}; |
| 627 |
if (auto sourceInfo{allCooked.GetSourcePositionRange(symbol.name())}) { |
| 628 |
os << symbol.name().ToString() << ": " << sourceInfo->first.path << ", " |
| 629 |
<< sourceInfo->first.line << ", " << sourceInfo->first.column << "-" |
| 630 |
<< sourceInfo->second.column << "\n"; |
| 631 |
} else if (symbol.has<semantics::UseDetails>()) { |
| 632 |
os << symbol.name().ToString() << ": " |
| 633 |
<< symbol.GetUltimate().owner().symbol()->name().ToString() << "\n"; |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
void DoDumpSymbols(llvm::raw_ostream &os, const Scope &scope, int indent) { |
| 639 |
PutIndent(os, indent); |
| 640 |
os << Scope::EnumToString(scope.kind()) << " scope:"; |
| 641 |
if (const auto *symbol{scope.symbol()}) { |
| 642 |
os << ' ' << symbol->name(); |
| 643 |
} |
| 644 |
if (scope.alignment().has_value()) { |
| 645 |
os << " size=" << scope.size() << " alignment=" << *scope.alignment(); |
| 646 |
} |
| 647 |
if (scope.derivedTypeSpec()) { |
| 648 |
os << " instantiation of " << *scope.derivedTypeSpec(); |
| 649 |
} |
| 650 |
os << " sourceRange=" << scope.sourceRange().size() << " bytes\n"; |
| 651 |
++indent; |
| 652 |
for (const auto &pair : scope) { |
| 653 |
const auto &symbol{*pair.second}; |
| 654 |
PutIndent(os, indent); |
| 655 |
os << symbol << '\n'; |
| 656 |
if (const auto *details{symbol.detailsIf<GenericDetails>()}) { |
| 657 |
if (const auto &type{details->derivedType()}) { |
| 658 |
PutIndent(os, indent); |
| 659 |
os << *type << '\n'; |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
if (!scope.equivalenceSets().empty()) { |
| 664 |
PutIndent(os, indent); |
| 665 |
os << "Equivalence Sets:"; |
| 666 |
for (const auto &set : scope.equivalenceSets()) { |
| 667 |
os << ' '; |
| 668 |
char sep = '('; |
| 669 |
for (const auto &object : set) { |
| 670 |
os << sep << object.AsFortran(); |
| 671 |
sep = ','; |
| 672 |
} |
| 673 |
os << ')'; |
| 674 |
} |
| 675 |
os << '\n'; |
| 676 |
} |
| 677 |
if (!scope.crayPointers().empty()) { |
| 678 |
PutIndent(os, indent); |
| 679 |
os << "Cray Pointers:"; |
| 680 |
for (const auto &[pointee, pointer] : scope.crayPointers()) { |
| 681 |
os << " (" << pointer->name() << ',' << pointee << ')'; |
| 682 |
} |
| 683 |
} |
| 684 |
for (const auto &pair : scope.commonBlocks()) { |
| 685 |
const auto &symbol{*pair.second}; |
| 686 |
PutIndent(os, indent); |
| 687 |
os << symbol << '\n'; |
| 688 |
} |
| 689 |
for (const auto &child : scope.children()) { |
| 690 |
DoDumpSymbols(os, child, indent); |
| 691 |
} |
| 692 |
--indent; |
| 693 |
} |
| 694 |
|
| 695 |
static void PutIndent(llvm::raw_ostream &os, int indent) { |
| 696 |
for (int i = 0; i < indent; ++i) { |
| 697 |
os << " "; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
void SemanticsContext::MapCommonBlockAndCheckConflicts(const Symbol &common) { |
| 702 |
if (!commonBlockMap_) { |
| 703 |
commonBlockMap_ = std::make_unique<CommonBlockMap>(); |
| 704 |
} |
| 705 |
commonBlockMap_->MapCommonBlockAndCheckConflicts(*this, common); |
| 706 |
} |
| 707 |
|
| 708 |
CommonBlockList SemanticsContext::GetCommonBlocks() const { |
| 709 |
if (commonBlockMap_) { |
| 710 |
return commonBlockMap_->GetCommonBlocks(); |
| 711 |
} |
| 712 |
return {}; |
| 713 |
} |
| 714 |
|
| 715 |
} // namespace Fortran::semantics |