1 //===-- lib/Semantics/data-to-inits.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 // DATA statement object/value checking and conversion to static 10 // initializers 11 // - Applies specific checks to each scalar element initialization with a 12 // constant value or pointer target with class DataInitializationCompiler; 13 // - Collects the elemental initializations for each symbol and converts them 14 // into a single init() expression with member function 15 // DataChecker::ConstructInitializer(). 16 17 #include "data-to-inits.h" 18 #include "pointer-assignment.h" 19 #include "flang/Evaluate/fold-designator.h" 20 #include "flang/Evaluate/tools.h" 21 #include "flang/Semantics/tools.h" 22 23 // The job of generating explicit static initializers for objects that don't 24 // have them in order to implement default component initialization is now being 25 // done in lowering, so don't do it here in semantics; but the code remains here 26 // in case we change our minds. 27 static constexpr bool makeDefaultInitializationExplicit{false}; 28 29 // Whether to delete the original "init()" initializers from storage-associated 30 // objects and pointers. 31 static constexpr bool removeOriginalInits{false}; 32 33 namespace Fortran::semantics { 34 35 // Steps through a list of values in a DATA statement set; implements 36 // repetition. 37 template <typename DSV = parser::DataStmtValue> class ValueListIterator { 38 public: 39 ValueListIterator(SemanticsContext &context, const std::list<DSV> &list) 40 : context_{context}, end_{list.end()}, at_{list.begin()} { 41 SetRepetitionCount(); 42 } 43 bool hasFatalError() const { return hasFatalError_; } 44 bool IsAtEnd() const { return at_ == end_; } 45 const SomeExpr *operator*() const { return GetExpr(context_, GetConstant()); } 46 std::optional<parser::CharBlock> LocateSource() const { 47 if (!hasFatalError_) { 48 return GetConstant().source; 49 } 50 return {}; 51 } 52 ValueListIterator &operator++() { 53 if (repetitionsRemaining_ > 0) { 54 --repetitionsRemaining_; 55 } else if (at_ != end_) { 56 ++at_; 57 SetRepetitionCount(); 58 } 59 return *this; 60 } 61 62 private: 63 using listIterator = typename std::list<DSV>::const_iterator; 64 void SetRepetitionCount(); 65 const parser::DataStmtValue &GetValue() const { 66 return DEREF(common::Unwrap<const parser::DataStmtValue>(*at_)); 67 } 68 const parser::DataStmtConstant &GetConstant() const { 69 return std::get<parser::DataStmtConstant>(GetValue().t); 70 } 71 72 SemanticsContext &context_; 73 listIterator end_, at_; 74 ConstantSubscript repetitionsRemaining_{0}; 75 bool hasFatalError_{false}; 76 }; 77 78 template <typename DSV> void ValueListIterator<DSV>::SetRepetitionCount() { 79 for (repetitionsRemaining_ = 1; at_ != end_; ++at_) { 80 auto repetitions{GetValue().repetitions}; 81 if (repetitions < 0) { 82 hasFatalError_ = true; 83 } else if (repetitions > 0) { 84 repetitionsRemaining_ = repetitions - 1; 85 return; 86 } 87 } 88 repetitionsRemaining_ = 0; 89 } 90 91 // Collects all of the elemental initializations from DATA statements 92 // into a single image for each symbol that appears in any DATA. 93 // Expands the implied DO loops and array references. 94 // Applies checks that validate each distinct elemental initialization 95 // of the variables in a data-stmt-set, as well as those that apply 96 // to the corresponding values being used to initialize each element. 97 template <typename DSV = parser::DataStmtValue> 98 class DataInitializationCompiler { 99 public: 100 DataInitializationCompiler(DataInitializations &inits, 101 evaluate::ExpressionAnalyzer &a, const std::list<DSV> &list) 102 : inits_{inits}, exprAnalyzer_{a}, values_{a.context(), list} {} 103 const DataInitializations &inits() const { return inits_; } 104 bool HasSurplusValues() const { return !values_.IsAtEnd(); } 105 bool Scan(const parser::DataStmtObject &); 106 // Initializes all elements of whole variable or component 107 bool Scan(const Symbol &); 108 109 private: 110 bool Scan(const parser::Variable &); 111 bool Scan(const parser::Designator &); 112 bool Scan(const parser::DataImpliedDo &); 113 bool Scan(const parser::DataIDoObject &); 114 115 // Initializes all elements of a designator, which can be an array or section. 116 bool InitDesignator(const SomeExpr &); 117 // Initializes a single scalar object. 118 bool InitElement(const evaluate::OffsetSymbol &, const SomeExpr &designator); 119 // If the returned flag is true, emit a warning about CHARACTER misusage. 120 std::optional<std::pair<SomeExpr, bool>> ConvertElement( 121 const SomeExpr &, const evaluate::DynamicType &); 122 123 DataInitializations &inits_; 124 evaluate::ExpressionAnalyzer &exprAnalyzer_; 125 ValueListIterator<DSV> values_; 126 }; 127 128 template <typename DSV> 129 bool DataInitializationCompiler<DSV>::Scan( 130 const parser::DataStmtObject &object) { 131 return common::visit( 132 common::visitors{ 133 [&](const common::Indirection<parser::Variable> &var) { 134 return Scan(var.value()); 135 }, 136 [&](const parser::DataImpliedDo &ido) { return Scan(ido); }, 137 }, 138 object.u); 139 } 140 141 template <typename DSV> 142 bool DataInitializationCompiler<DSV>::Scan(const parser::Variable &var) { 143 if (const auto *expr{GetExpr(exprAnalyzer_.context(), var)}) { 144 exprAnalyzer_.GetFoldingContext().messages().SetLocation(var.GetSource()); 145 if (InitDesignator(*expr)) { 146 return true; 147 } 148 } 149 return false; 150 } 151 152 template <typename DSV> 153 bool DataInitializationCompiler<DSV>::Scan( 154 const parser::Designator &designator) { 155 if (auto expr{exprAnalyzer_.Analyze(designator)}) { 156 exprAnalyzer_.GetFoldingContext().messages().SetLocation( 157 parser::FindSourceLocation(designator)); 158 if (InitDesignator(*expr)) { 159 return true; 160 } 161 } 162 return false; 163 } 164 165 template <typename DSV> 166 bool DataInitializationCompiler<DSV>::Scan(const parser::DataImpliedDo &ido) { 167 const auto &bounds{std::get<parser::DataImpliedDo::Bounds>(ido.t)}; 168 auto name{bounds.name.thing.thing}; 169 const auto *lowerExpr{ 170 GetExpr(exprAnalyzer_.context(), bounds.lower.thing.thing)}; 171 const auto *upperExpr{ 172 GetExpr(exprAnalyzer_.context(), bounds.upper.thing.thing)}; 173 const auto *stepExpr{bounds.step 174 ? GetExpr(exprAnalyzer_.context(), bounds.step->thing.thing) 175 : nullptr}; 176 if (lowerExpr && upperExpr) { 177 // Fold the bounds expressions (again) in case any of them depend 178 // on outer implied DO loops. 179 evaluate::FoldingContext &context{exprAnalyzer_.GetFoldingContext()}; 180 std::int64_t stepVal{1}; 181 if (stepExpr) { 182 auto foldedStep{evaluate::Fold(context, SomeExpr{*stepExpr})}; 183 stepVal = ToInt64(foldedStep).value_or(1); 184 if (stepVal == 0) { 185 exprAnalyzer_.Say(name.source, 186 "DATA statement implied DO loop has a step value of zero"_err_en_US); 187 return false; 188 } 189 } 190 auto foldedLower{evaluate::Fold(context, SomeExpr{*lowerExpr})}; 191 auto lower{ToInt64(foldedLower)}; 192 auto foldedUpper{evaluate::Fold(context, SomeExpr{*upperExpr})}; 193 auto upper{ToInt64(foldedUpper)}; 194 if (lower && upper) { 195 int kind{evaluate::ResultType<evaluate::ImpliedDoIndex>::kind}; 196 if (const auto dynamicType{evaluate::DynamicType::From(*name.symbol)}) { 197 if (dynamicType->category() == TypeCategory::Integer) { 198 kind = dynamicType->kind(); 199 } 200 } 201 if (exprAnalyzer_.AddImpliedDo(name.source, kind)) { 202 auto &value{context.StartImpliedDo(name.source, *lower)}; 203 bool result{true}; 204 for (auto n{(*upper - value + stepVal) / stepVal}; n > 0; 205 --n, value += stepVal) { 206 for (const auto &object : 207 std::get<std::list<parser::DataIDoObject>>(ido.t)) { 208 if (!Scan(object)) { 209 result = false; 210 break; 211 } 212 } 213 } 214 context.EndImpliedDo(name.source); 215 exprAnalyzer_.RemoveImpliedDo(name.source); 216 return result; 217 } 218 } 219 } 220 return false; 221 } 222 223 template <typename DSV> 224 bool DataInitializationCompiler<DSV>::Scan( 225 const parser::DataIDoObject &object) { 226 return common::visit( 227 common::visitors{ 228 [&](const parser::Scalar<common::Indirection<parser::Designator>> 229 &var) { return Scan(var.thing.value()); }, 230 [&](const common::Indirection<parser::DataImpliedDo> &ido) { 231 return Scan(ido.value()); 232 }, 233 }, 234 object.u); 235 } 236 237 template <typename DSV> 238 bool DataInitializationCompiler<DSV>::Scan(const Symbol &symbol) { 239 auto designator{exprAnalyzer_.Designate(evaluate::DataRef{symbol})}; 240 CHECK(designator.has_value()); 241 return InitDesignator(*designator); 242 } 243 244 template <typename DSV> 245 bool DataInitializationCompiler<DSV>::InitDesignator( 246 const SomeExpr &designator) { 247 evaluate::FoldingContext &context{exprAnalyzer_.GetFoldingContext()}; 248 evaluate::DesignatorFolder folder{context}; 249 while (auto offsetSymbol{folder.FoldDesignator(designator)}) { 250 if (folder.isOutOfRange()) { 251 if (auto bad{evaluate::OffsetToDesignator(context, *offsetSymbol)}) { 252 exprAnalyzer_.context().Say( 253 "DATA statement designator '%s' is out of range"_err_en_US, 254 bad->AsFortran()); 255 } else { 256 exprAnalyzer_.context().Say( 257 "DATA statement designator '%s' is out of range"_err_en_US, 258 designator.AsFortran()); 259 } 260 return false; 261 } else if (!InitElement(*offsetSymbol, designator)) { 262 return false; 263 } else { 264 ++values_; 265 } 266 } 267 return folder.isEmpty(); 268 } 269 270 template <typename DSV> 271 std::optional<std::pair<SomeExpr, bool>> 272 DataInitializationCompiler<DSV>::ConvertElement( 273 const SomeExpr &expr, const evaluate::DynamicType &type) { 274 if (auto converted{evaluate::ConvertToType(type, SomeExpr{expr})}) { 275 return {std::make_pair(std::move(*converted), false)}; 276 } 277 // Allow DATA initialization with Hollerith and kind=1 CHARACTER like 278 // (most) other Fortran compilers do. 279 if (auto converted{evaluate::HollerithToBOZ( 280 exprAnalyzer_.GetFoldingContext(), expr, type)}) { 281 return {std::make_pair(std::move(*converted), true)}; 282 } 283 SemanticsContext &context{exprAnalyzer_.context()}; 284 if (context.IsEnabled(common::LanguageFeature::LogicalIntegerAssignment)) { 285 if (MaybeExpr converted{evaluate::DataConstantConversionExtension( 286 exprAnalyzer_.GetFoldingContext(), type, expr)}) { 287 if (context.ShouldWarn( 288 common::LanguageFeature::LogicalIntegerAssignment)) { 289 context.Say( 290 "nonstandard usage: initialization of %s with %s"_port_en_US, 291 type.AsFortran(), expr.GetType().value().AsFortran()); 292 } 293 return {std::make_pair(std::move(*converted), false)}; 294 } 295 } 296 return std::nullopt; 297 } 298 299 template <typename DSV> 300 bool DataInitializationCompiler<DSV>::InitElement( 301 const evaluate::OffsetSymbol &offsetSymbol, const SomeExpr &designator) { 302 const Symbol &symbol{offsetSymbol.symbol()}; 303 const Symbol *lastSymbol{GetLastSymbol(designator)}; 304 bool isPointer{lastSymbol && IsPointer(*lastSymbol)}; 305 bool isProcPointer{lastSymbol && IsProcedurePointer(*lastSymbol)}; 306 evaluate::FoldingContext &context{exprAnalyzer_.GetFoldingContext()}; 307 auto &messages{context.messages()}; 308 auto restorer{ 309 messages.SetLocation(values_.LocateSource().value_or(messages.at()))}; 310 311 const auto DescribeElement{[&]() { 312 if (auto badDesignator{ 313 evaluate::OffsetToDesignator(context, offsetSymbol)}) { 314 return badDesignator->AsFortran(); 315 } else { 316 // Error recovery 317 std::string buf; 318 llvm::raw_string_ostream ss{buf}; 319 ss << offsetSymbol.symbol().name() << " offset " << offsetSymbol.offset() 320 << " bytes for " << offsetSymbol.size() << " bytes"; 321 return ss.str(); 322 } 323 }}; 324 const auto GetImage{[&]() -> evaluate::InitialImage & { 325 auto iter{inits_.emplace(&symbol, symbol.size())}; 326 auto &symbolInit{iter.first->second}; 327 symbolInit.initializedRanges.emplace_back( 328 offsetSymbol.offset(), offsetSymbol.size()); 329 return symbolInit.image; 330 }}; 331 const auto OutOfRangeError{[&]() { 332 evaluate::AttachDeclaration( 333 exprAnalyzer_.context().Say( 334 "DATA statement designator '%s' is out of range for its variable '%s'"_err_en_US, 335 DescribeElement(), symbol.name()), 336 symbol); 337 }}; 338 339 if (values_.hasFatalError()) { 340 return false; 341 } else if (values_.IsAtEnd()) { 342 exprAnalyzer_.context().Say( 343 "DATA statement set has no value for '%s'"_err_en_US, 344 DescribeElement()); 345 return false; 346 } else if (static_cast<std::size_t>( 347 offsetSymbol.offset() + offsetSymbol.size()) > symbol.size()) { 348 OutOfRangeError(); 349 return false; 350 } 351 352 const SomeExpr *expr{*values_}; 353 if (!expr) { 354 CHECK(exprAnalyzer_.context().AnyFatalError()); 355 } else if (isPointer) { 356 if (static_cast<std::size_t>(offsetSymbol.offset() + offsetSymbol.size()) > 357 symbol.size()) { 358 OutOfRangeError(); 359 } else if (evaluate::IsNullPointer(*expr)) { 360 // nothing to do; rely on zero initialization 361 return true; 362 } else if (isProcPointer) { 363 if (evaluate::IsProcedure(*expr)) { 364 if (CheckPointerAssignment(context, designator, *expr)) { 365 if (lastSymbol->has<ProcEntityDetails>()) { 366 GetImage().AddPointer(offsetSymbol.offset(), *expr); 367 return true; 368 } else { 369 evaluate::AttachDeclaration( 370 exprAnalyzer_.context().Say( 371 "DATA statement initialization of procedure pointer '%s' declared using a POINTER statement and an INTERFACE instead of a PROCEDURE statement"_todo_en_US, 372 DescribeElement()), 373 *lastSymbol); 374 } 375 } 376 } else { 377 exprAnalyzer_.Say( 378 "Data object '%s' may not be used to initialize '%s', which is a procedure pointer"_err_en_US, 379 expr->AsFortran(), DescribeElement()); 380 } 381 } else if (evaluate::IsProcedure(*expr)) { 382 exprAnalyzer_.Say( 383 "Procedure '%s' may not be used to initialize '%s', which is not a procedure pointer"_err_en_US, 384 expr->AsFortran(), DescribeElement()); 385 } else if (CheckInitialTarget(context, designator, *expr)) { 386 GetImage().AddPointer(offsetSymbol.offset(), *expr); 387 return true; 388 } 389 } else if (evaluate::IsNullPointer(*expr)) { 390 exprAnalyzer_.Say("Initializer for '%s' must not be a pointer"_err_en_US, 391 DescribeElement()); 392 } else if (evaluate::IsProcedure(*expr)) { 393 exprAnalyzer_.Say("Initializer for '%s' must not be a procedure"_err_en_US, 394 DescribeElement()); 395 } else if (auto designatorType{designator.GetType()}) { 396 if (expr->Rank() > 0) { 397 // Because initial-data-target is ambiguous with scalar-constant and 398 // scalar-constant-subobject at parse time, enforcement of scalar-* 399 // must be deferred to here. 400 exprAnalyzer_.Say( 401 "DATA statement value initializes '%s' with an array"_err_en_US, 402 DescribeElement()); 403 } else if (auto converted{ConvertElement(*expr, *designatorType)}) { 404 // value non-pointer initialization 405 if (IsBOZLiteral(*expr) && 406 designatorType->category() != TypeCategory::Integer) { // 8.6.7(11) 407 exprAnalyzer_.Say( 408 "BOZ literal should appear in a DATA statement only as a value for an integer object, but '%s' is '%s'"_port_en_US, 409 DescribeElement(), designatorType->AsFortran()); 410 } else if (converted->second) { 411 exprAnalyzer_.context().Say( 412 "DATA statement value initializes '%s' of type '%s' with CHARACTER"_port_en_US, 413 DescribeElement(), designatorType->AsFortran()); 414 } 415 auto folded{evaluate::Fold(context, std::move(converted->first))}; 416 switch (GetImage().Add( 417 offsetSymbol.offset(), offsetSymbol.size(), folded, context)) { 418 case evaluate::InitialImage::Ok: 419 return true; 420 case evaluate::InitialImage::NotAConstant: 421 exprAnalyzer_.Say( 422 "DATA statement value '%s' for '%s' is not a constant"_err_en_US, 423 folded.AsFortran(), DescribeElement()); 424 break; 425 case evaluate::InitialImage::OutOfRange: 426 OutOfRangeError(); 427 break; 428 default: 429 CHECK(exprAnalyzer_.context().AnyFatalError()); 430 break; 431 } 432 } else { 433 exprAnalyzer_.context().Say( 434 "DATA statement value could not be converted to the type '%s' of the object '%s'"_err_en_US, 435 designatorType->AsFortran(), DescribeElement()); 436 } 437 } else { 438 CHECK(exprAnalyzer_.context().AnyFatalError()); 439 } 440 return false; 441 } 442 443 void AccumulateDataInitializations(DataInitializations &inits, 444 evaluate::ExpressionAnalyzer &exprAnalyzer, 445 const parser::DataStmtSet &set) { 446 DataInitializationCompiler scanner{ 447 inits, exprAnalyzer, std::get<std::list<parser::DataStmtValue>>(set.t)}; 448 for (const auto &object : 449 std::get<std::list<parser::DataStmtObject>>(set.t)) { 450 if (!scanner.Scan(object)) { 451 return; 452 } 453 } 454 if (scanner.HasSurplusValues()) { 455 exprAnalyzer.context().Say( 456 "DATA statement set has more values than objects"_err_en_US); 457 } 458 } 459 460 void AccumulateDataInitializations(DataInitializations &inits, 461 evaluate::ExpressionAnalyzer &exprAnalyzer, const Symbol &symbol, 462 const std::list<common::Indirection<parser::DataStmtValue>> &list) { 463 DataInitializationCompiler<common::Indirection<parser::DataStmtValue>> 464 scanner{inits, exprAnalyzer, list}; 465 if (scanner.Scan(symbol) && scanner.HasSurplusValues()) { 466 exprAnalyzer.context().Say( 467 "DATA statement set has more values than objects"_err_en_US); 468 } 469 } 470 471 // Looks for default derived type component initialization -- but 472 // *not* allocatables. 473 static const DerivedTypeSpec *HasDefaultInitialization(const Symbol &symbol) { 474 if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) { 475 if (object->init().has_value()) { 476 return nullptr; // init is explicit, not default 477 } else if (!object->isDummy() && object->type()) { 478 if (const DerivedTypeSpec * derived{object->type()->AsDerived()}) { 479 DirectComponentIterator directs{*derived}; 480 if (std::find_if( 481 directs.begin(), directs.end(), [](const Symbol &component) { 482 return !IsAllocatable(component) && 483 HasDeclarationInitializer(component); 484 })) { 485 return derived; 486 } 487 } 488 } 489 } 490 return nullptr; 491 } 492 493 // PopulateWithComponentDefaults() adds initializations to an instance 494 // of SymbolDataInitialization containing all of the default component 495 // initializers 496 497 static void PopulateWithComponentDefaults(SymbolDataInitialization &init, 498 std::size_t offset, const DerivedTypeSpec &derived, 499 evaluate::FoldingContext &foldingContext); 500 501 static void PopulateWithComponentDefaults(SymbolDataInitialization &init, 502 std::size_t offset, const DerivedTypeSpec &derived, 503 evaluate::FoldingContext &foldingContext, const Symbol &symbol) { 504 if (auto extents{evaluate::GetConstantExtents(foldingContext, symbol)}) { 505 const Scope &scope{derived.scope() ? *derived.scope() 506 : DEREF(derived.typeSymbol().scope())}; 507 std::size_t stride{scope.size()}; 508 if (std::size_t alignment{scope.alignment().value_or(0)}) { 509 stride = ((stride + alignment - 1) / alignment) * alignment; 510 } 511 for (auto elements{evaluate::GetSize(*extents)}; elements-- > 0; 512 offset += stride) { 513 PopulateWithComponentDefaults(init, offset, derived, foldingContext); 514 } 515 } 516 } 517 518 // F'2018 19.5.3(10) allows storage-associated default component initialization 519 // when the values are identical. 520 static void PopulateWithComponentDefaults(SymbolDataInitialization &init, 521 std::size_t offset, const DerivedTypeSpec &derived, 522 evaluate::FoldingContext &foldingContext) { 523 const Scope &scope{ 524 derived.scope() ? *derived.scope() : DEREF(derived.typeSymbol().scope())}; 525 for (const auto &pair : scope) { 526 const Symbol &component{*pair.second}; 527 std::size_t componentOffset{offset + component.offset()}; 528 if (const auto *object{component.detailsIf<ObjectEntityDetails>()}) { 529 if (!IsAllocatable(component) && !IsAutomatic(component)) { 530 bool initialized{false}; 531 if (object->init()) { 532 initialized = true; 533 if (IsPointer(component)) { 534 if (auto extant{init.image.AsConstantPointer(componentOffset)}) { 535 initialized = !(*extant == *object->init()); 536 } 537 if (initialized) { 538 init.image.AddPointer(componentOffset, *object->init()); 539 } 540 } else { // data, not pointer 541 if (auto dyType{evaluate::DynamicType::From(component)}) { 542 if (auto extents{evaluate::GetConstantExtents( 543 foldingContext, component)}) { 544 if (auto extant{init.image.AsConstant( 545 foldingContext, *dyType, *extents, componentOffset)}) { 546 initialized = !(*extant == *object->init()); 547 } 548 } 549 } 550 if (initialized) { 551 init.image.Add(componentOffset, component.size(), *object->init(), 552 foldingContext); 553 } 554 } 555 } else if (const DeclTypeSpec * type{component.GetType()}) { 556 if (const DerivedTypeSpec * componentDerived{type->AsDerived()}) { 557 PopulateWithComponentDefaults(init, componentOffset, 558 *componentDerived, foldingContext, component); 559 } 560 } 561 if (initialized) { 562 init.initializedRanges.emplace_back( 563 componentOffset, component.size()); 564 } 565 } 566 } else if (const auto *proc{component.detailsIf<ProcEntityDetails>()}) { 567 if (proc->init() && *proc->init()) { 568 SomeExpr procPtrInit{evaluate::ProcedureDesignator{**proc->init()}}; 569 auto extant{init.image.AsConstantPointer(componentOffset)}; 570 if (!extant || !(*extant == procPtrInit)) { 571 init.initializedRanges.emplace_back( 572 componentOffset, component.size()); 573 init.image.AddPointer(componentOffset, std::move(procPtrInit)); 574 } 575 } 576 } 577 } 578 } 579 580 static bool CheckForOverlappingInitialization( 581 const std::list<SymbolRef> &symbols, 582 SymbolDataInitialization &initialization, 583 evaluate::ExpressionAnalyzer &exprAnalyzer, const std::string &what) { 584 bool result{true}; 585 auto &context{exprAnalyzer.GetFoldingContext()}; 586 initialization.initializedRanges.sort(); 587 ConstantSubscript next{0}; 588 for (const auto &range : initialization.initializedRanges) { 589 if (range.start() < next) { 590 result = false; // error: overlap 591 bool hit{false}; 592 for (const Symbol &symbol : symbols) { 593 auto offset{range.start() - 594 static_cast<ConstantSubscript>( 595 symbol.offset() - symbols.front()->offset())}; 596 if (offset >= 0) { 597 if (auto badDesignator{evaluate::OffsetToDesignator( 598 context, symbol, offset, range.size())}) { 599 hit = true; 600 exprAnalyzer.Say(symbol.name(), 601 "%s affect '%s' more than once"_err_en_US, what, 602 badDesignator->AsFortran()); 603 } 604 } 605 } 606 CHECK(hit); 607 } 608 next = range.start() + range.size(); 609 CHECK(next <= static_cast<ConstantSubscript>(initialization.image.size())); 610 } 611 return result; 612 } 613 614 static void IncorporateExplicitInitialization( 615 SymbolDataInitialization &combined, DataInitializations &inits, 616 const Symbol &symbol, ConstantSubscript firstOffset, 617 evaluate::FoldingContext &foldingContext) { 618 auto iter{inits.find(&symbol)}; 619 const auto offset{symbol.offset() - firstOffset}; 620 if (iter != inits.end()) { // DATA statement initialization 621 for (const auto &range : iter->second.initializedRanges) { 622 auto at{offset + range.start()}; 623 combined.initializedRanges.emplace_back(at, range.size()); 624 combined.image.Incorporate( 625 at, iter->second.image, range.start(), range.size()); 626 } 627 if (removeOriginalInits) { 628 inits.erase(iter); 629 } 630 } else { // Declaration initialization 631 Symbol &mutableSymbol{const_cast<Symbol &>(symbol)}; 632 if (IsPointer(mutableSymbol)) { 633 if (auto *object{mutableSymbol.detailsIf<ObjectEntityDetails>()}) { 634 if (object->init()) { 635 combined.initializedRanges.emplace_back(offset, mutableSymbol.size()); 636 combined.image.AddPointer(offset, *object->init()); 637 if (removeOriginalInits) { 638 object->init().reset(); 639 } 640 } 641 } else if (auto *proc{mutableSymbol.detailsIf<ProcEntityDetails>()}) { 642 if (proc->init() && *proc->init()) { 643 combined.initializedRanges.emplace_back(offset, mutableSymbol.size()); 644 combined.image.AddPointer( 645 offset, SomeExpr{evaluate::ProcedureDesignator{**proc->init()}}); 646 if (removeOriginalInits) { 647 proc->init().reset(); 648 } 649 } 650 } 651 } else if (auto *object{mutableSymbol.detailsIf<ObjectEntityDetails>()}) { 652 if (!IsNamedConstant(mutableSymbol) && object->init()) { 653 combined.initializedRanges.emplace_back(offset, mutableSymbol.size()); 654 combined.image.Add( 655 offset, mutableSymbol.size(), *object->init(), foldingContext); 656 if (removeOriginalInits) { 657 object->init().reset(); 658 } 659 } 660 } 661 } 662 } 663 664 // Finds the size of the smallest element type in a list of 665 // storage-associated objects. 666 static std::size_t ComputeMinElementBytes( 667 const std::list<SymbolRef> &associated, 668 evaluate::FoldingContext &foldingContext) { 669 std::size_t minElementBytes{1}; 670 const Symbol &first{*associated.front()}; 671 for (const Symbol &s : associated) { 672 if (auto dyType{evaluate::DynamicType::From(s)}) { 673 auto size{static_cast<std::size_t>( 674 evaluate::ToInt64(dyType->MeasureSizeInBytes(foldingContext, true)) 675 .value_or(1))}; 676 if (std::size_t alignment{dyType->GetAlignment(foldingContext)}) { 677 size = ((size + alignment - 1) / alignment) * alignment; 678 } 679 if (&s == &first) { 680 minElementBytes = size; 681 } else { 682 minElementBytes = std::min(minElementBytes, size); 683 } 684 } else { 685 minElementBytes = 1; 686 } 687 } 688 return minElementBytes; 689 } 690 691 // Checks for overlapping initialization errors in a list of 692 // storage-associated objects. Default component initializations 693 // are allowed to be overridden by explicit initializations. 694 // If the objects are static, save the combined initializer as 695 // a compiler-created object that covers all of them. 696 static bool CombineEquivalencedInitialization( 697 const std::list<SymbolRef> &associated, 698 evaluate::ExpressionAnalyzer &exprAnalyzer, DataInitializations &inits) { 699 // Compute the minimum common granularity and total size 700 const Symbol &first{*associated.front()}; 701 std::size_t maxLimit{0}; 702 for (const Symbol &s : associated) { 703 CHECK(s.offset() >= first.offset()); 704 auto limit{s.offset() + s.size()}; 705 if (limit > maxLimit) { 706 maxLimit = limit; 707 } 708 } 709 auto bytes{static_cast<common::ConstantSubscript>(maxLimit - first.offset())}; 710 Scope &scope{const_cast<Scope &>(first.owner())}; 711 // Combine the initializations of the associated objects. 712 // Apply all default initializations first. 713 SymbolDataInitialization combined{static_cast<std::size_t>(bytes)}; 714 auto &foldingContext{exprAnalyzer.GetFoldingContext()}; 715 for (const Symbol &s : associated) { 716 if (!IsNamedConstant(s)) { 717 if (const auto *derived{HasDefaultInitialization(s)}) { 718 PopulateWithComponentDefaults( 719 combined, s.offset() - first.offset(), *derived, foldingContext, s); 720 } 721 } 722 } 723 if (!CheckForOverlappingInitialization(associated, combined, exprAnalyzer, 724 "Distinct default component initializations of equivalenced objects"s)) { 725 return false; 726 } 727 // Don't complain about overlap between explicit initializations and 728 // default initializations. 729 combined.initializedRanges.clear(); 730 // Now overlay all explicit initializations from DATA statements and 731 // from initializers in declarations. 732 for (const Symbol &symbol : associated) { 733 IncorporateExplicitInitialization( 734 combined, inits, symbol, first.offset(), foldingContext); 735 } 736 if (!CheckForOverlappingInitialization(associated, combined, exprAnalyzer, 737 "Explicit initializations of equivalenced objects"s)) { 738 return false; 739 } 740 // If the items are in static storage, save the final initialization. 741 if (std::find_if(associated.begin(), associated.end(), 742 [](SymbolRef ref) { return IsSaved(*ref); }) != associated.end()) { 743 // Create a compiler array temp that overlaps all the items. 744 SourceName name{exprAnalyzer.context().GetTempName(scope)}; 745 auto emplaced{ 746 scope.try_emplace(name, Attrs{Attr::SAVE}, ObjectEntityDetails{})}; 747 CHECK(emplaced.second); 748 Symbol &combinedSymbol{*emplaced.first->second}; 749 combinedSymbol.set(Symbol::Flag::CompilerCreated); 750 inits.emplace(&combinedSymbol, std::move(combined)); 751 auto &details{combinedSymbol.get<ObjectEntityDetails>()}; 752 combinedSymbol.set_offset(first.offset()); 753 combinedSymbol.set_size(bytes); 754 std::size_t minElementBytes{ 755 ComputeMinElementBytes(associated, foldingContext)}; 756 if (!evaluate::IsValidKindOfIntrinsicType( 757 TypeCategory::Integer, minElementBytes) || 758 (bytes % minElementBytes) != 0) { 759 minElementBytes = 1; 760 } 761 const DeclTypeSpec &typeSpec{scope.MakeNumericType( 762 TypeCategory::Integer, KindExpr{minElementBytes})}; 763 details.set_type(typeSpec); 764 ArraySpec arraySpec; 765 arraySpec.emplace_back(ShapeSpec::MakeExplicit(Bound{ 766 bytes / static_cast<common::ConstantSubscript>(minElementBytes)})); 767 details.set_shape(arraySpec); 768 if (const auto *commonBlock{FindCommonBlockContaining(first)}) { 769 details.set_commonBlock(*commonBlock); 770 } 771 // Add an EQUIVALENCE set to the scope so that the new object appears in 772 // the results of GetStorageAssociations(). 773 auto &newSet{scope.equivalenceSets().emplace_back()}; 774 newSet.emplace_back(combinedSymbol); 775 newSet.emplace_back(const_cast<Symbol &>(first)); 776 } 777 return true; 778 } 779 780 // When a statically-allocated derived type variable has no explicit 781 // initialization, but its type has at least one nonallocatable ultimate 782 // component with default initialization, make its initialization explicit. 783 [[maybe_unused]] static void MakeDefaultInitializationExplicit( 784 const Scope &scope, const std::list<std::list<SymbolRef>> &associations, 785 evaluate::FoldingContext &foldingContext, DataInitializations &inits) { 786 UnorderedSymbolSet equivalenced; 787 for (const std::list<SymbolRef> &association : associations) { 788 for (const Symbol &symbol : association) { 789 equivalenced.emplace(symbol); 790 } 791 } 792 for (const auto &pair : scope) { 793 const Symbol &symbol{*pair.second}; 794 if (!symbol.test(Symbol::Flag::InDataStmt) && 795 !HasDeclarationInitializer(symbol) && IsSaved(symbol) && 796 equivalenced.find(symbol) == equivalenced.end()) { 797 // Static object, no local storage association, no explicit initialization 798 if (const DerivedTypeSpec * derived{HasDefaultInitialization(symbol)}) { 799 auto newInitIter{inits.emplace(&symbol, symbol.size())}; 800 CHECK(newInitIter.second); 801 auto &newInit{newInitIter.first->second}; 802 PopulateWithComponentDefaults( 803 newInit, 0, *derived, foldingContext, symbol); 804 } 805 } 806 } 807 } 808 809 // Traverses the Scopes to: 810 // 1) combine initialization of equivalenced objects, & 811 // 2) optionally make initialization explicit for otherwise uninitialized static 812 // objects of derived types with default component initialization 813 // Returns false on error. 814 static bool ProcessScopes(const Scope &scope, 815 evaluate::ExpressionAnalyzer &exprAnalyzer, DataInitializations &inits) { 816 bool result{true}; // no error 817 switch (scope.kind()) { 818 case Scope::Kind::Global: 819 case Scope::Kind::Module: 820 case Scope::Kind::MainProgram: 821 case Scope::Kind::Subprogram: 822 case Scope::Kind::BlockData: 823 case Scope::Kind::Block: { 824 std::list<std::list<SymbolRef>> associations{GetStorageAssociations(scope)}; 825 for (const std::list<SymbolRef> &associated : associations) { 826 if (std::find_if(associated.begin(), associated.end(), [](SymbolRef ref) { 827 return IsInitialized(*ref); 828 }) != associated.end()) { 829 result &= 830 CombineEquivalencedInitialization(associated, exprAnalyzer, inits); 831 } 832 } 833 if constexpr (makeDefaultInitializationExplicit) { 834 MakeDefaultInitializationExplicit( 835 scope, associations, exprAnalyzer.GetFoldingContext(), inits); 836 } 837 for (const Scope &child : scope.children()) { 838 result &= ProcessScopes(child, exprAnalyzer, inits); 839 } 840 } break; 841 default:; 842 } 843 return result; 844 } 845 846 // Converts the static initialization image for a single symbol with 847 // one or more DATA statement appearances. 848 void ConstructInitializer(const Symbol &symbol, 849 SymbolDataInitialization &initialization, 850 evaluate::ExpressionAnalyzer &exprAnalyzer) { 851 std::list<SymbolRef> symbols{symbol}; 852 CheckForOverlappingInitialization( 853 symbols, initialization, exprAnalyzer, "DATA statement initializations"s); 854 auto &context{exprAnalyzer.GetFoldingContext()}; 855 if (const auto *proc{symbol.detailsIf<ProcEntityDetails>()}) { 856 CHECK(IsProcedurePointer(symbol)); 857 auto &mutableProc{const_cast<ProcEntityDetails &>(*proc)}; 858 if (MaybeExpr expr{initialization.image.AsConstantPointer()}) { 859 if (const auto *procDesignator{ 860 std::get_if<evaluate::ProcedureDesignator>(&expr->u)}) { 861 CHECK(!procDesignator->GetComponent()); 862 mutableProc.set_init(DEREF(procDesignator->GetSymbol())); 863 } else { 864 CHECK(evaluate::IsNullPointer(*expr)); 865 mutableProc.set_init(nullptr); 866 } 867 } else { 868 mutableProc.set_init(nullptr); 869 } 870 } else if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) { 871 auto &mutableObject{const_cast<ObjectEntityDetails &>(*object)}; 872 if (IsPointer(symbol)) { 873 if (auto ptr{initialization.image.AsConstantPointer()}) { 874 mutableObject.set_init(*ptr); 875 } else { 876 mutableObject.set_init(SomeExpr{evaluate::NullPointer{}}); 877 } 878 } else if (auto symbolType{evaluate::DynamicType::From(symbol)}) { 879 if (auto extents{evaluate::GetConstantExtents(context, symbol)}) { 880 mutableObject.set_init( 881 initialization.image.AsConstant(context, *symbolType, *extents)); 882 } else { 883 exprAnalyzer.Say(symbol.name(), 884 "internal: unknown shape for '%s' while constructing initializer from DATA"_err_en_US, 885 symbol.name()); 886 return; 887 } 888 } else { 889 exprAnalyzer.Say(symbol.name(), 890 "internal: no type for '%s' while constructing initializer from DATA"_err_en_US, 891 symbol.name()); 892 return; 893 } 894 if (!object->init()) { 895 exprAnalyzer.Say(symbol.name(), 896 "internal: could not construct an initializer from DATA statements for '%s'"_err_en_US, 897 symbol.name()); 898 } 899 } else { 900 CHECK(exprAnalyzer.context().AnyFatalError()); 901 } 902 } 903 904 void ConvertToInitializers( 905 DataInitializations &inits, evaluate::ExpressionAnalyzer &exprAnalyzer) { 906 if (ProcessScopes( 907 exprAnalyzer.context().globalScope(), exprAnalyzer, inits)) { 908 for (auto &[symbolPtr, initialization] : inits) { 909 ConstructInitializer(*symbolPtr, initialization, exprAnalyzer); 910 } 911 } 912 } 913 } // namespace Fortran::semantics 914