1 //===-- lib/Semantics/mod-file.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 "mod-file.h" 10 #include "resolve-names.h" 11 #include "flang/Common/restorer.h" 12 #include "flang/Evaluate/tools.h" 13 #include "flang/Parser/message.h" 14 #include "flang/Parser/parsing.h" 15 #include "flang/Semantics/scope.h" 16 #include "flang/Semantics/semantics.h" 17 #include "flang/Semantics/symbol.h" 18 #include "flang/Semantics/tools.h" 19 #include "llvm/Support/FileSystem.h" 20 #include "llvm/Support/MemoryBuffer.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include <algorithm> 23 #include <fstream> 24 #include <set> 25 #include <string_view> 26 #include <vector> 27 28 namespace Fortran::semantics { 29 30 using namespace parser::literals; 31 32 // The first line of a file that identifies it as a .mod file. 33 // The first three bytes are a Unicode byte order mark that ensures 34 // that the module file is decoded as UTF-8 even if source files 35 // are using another encoding. 36 struct ModHeader { 37 static constexpr const char bom[3 + 1]{"\xef\xbb\xbf"}; 38 static constexpr int magicLen{13}; 39 static constexpr int sumLen{16}; 40 static constexpr const char magic[magicLen + 1]{"!mod$ v1 sum:"}; 41 static constexpr char terminator{'\n'}; 42 static constexpr int len{magicLen + 1 + sumLen}; 43 }; 44 45 static std::optional<SourceName> GetSubmoduleParent(const parser::Program &); 46 static void CollectSymbols(const Scope &, SymbolVector &, SymbolVector &); 47 static void PutEntity(llvm::raw_ostream &, const Symbol &); 48 static void PutObjectEntity(llvm::raw_ostream &, const Symbol &); 49 static void PutProcEntity(llvm::raw_ostream &, const Symbol &); 50 static void PutPassName(llvm::raw_ostream &, const std::optional<SourceName> &); 51 static void PutTypeParam(llvm::raw_ostream &, const Symbol &); 52 static void PutEntity( 53 llvm::raw_ostream &, const Symbol &, std::function<void()>, Attrs); 54 static void PutInit(llvm::raw_ostream &, const Symbol &, const MaybeExpr &); 55 static void PutInit(llvm::raw_ostream &, const MaybeIntExpr &); 56 static void PutBound(llvm::raw_ostream &, const Bound &); 57 static llvm::raw_ostream &PutAttrs(llvm::raw_ostream &, Attrs, 58 const MaybeExpr & = std::nullopt, std::string before = ","s, 59 std::string after = ""s); 60 61 static llvm::raw_ostream &PutAttr(llvm::raw_ostream &, Attr); 62 static llvm::raw_ostream &PutType(llvm::raw_ostream &, const DeclTypeSpec &); 63 static llvm::raw_ostream &PutLower(llvm::raw_ostream &, const std::string &); 64 static std::error_code WriteFile( 65 const std::string &, const std::string &, bool = true); 66 static bool FileContentsMatch( 67 const std::string &, const std::string &, const std::string &); 68 static std::string CheckSum(const std::string_view &); 69 70 // Collect symbols needed for a subprogram interface 71 class SubprogramSymbolCollector { 72 public: 73 SubprogramSymbolCollector(const Symbol &symbol, const Scope &scope) 74 : symbol_{symbol}, scope_{scope} {} 75 const SymbolVector &symbols() const { return need_; } 76 const std::set<SourceName> &imports() const { return imports_; } 77 void Collect(); 78 79 private: 80 const Symbol &symbol_; 81 const Scope &scope_; 82 bool isInterface_{false}; 83 SymbolVector need_; // symbols that are needed 84 SymbolSet needSet_; // symbols already in need_ 85 SymbolSet useSet_; // use-associations that might be needed 86 std::set<SourceName> imports_; // imports from host that are needed 87 88 void DoSymbol(const Symbol &); 89 void DoSymbol(const SourceName &, const Symbol &); 90 void DoType(const DeclTypeSpec *); 91 void DoBound(const Bound &); 92 void DoParamValue(const ParamValue &); 93 bool NeedImport(const SourceName &, const Symbol &); 94 95 template <typename T> void DoExpr(evaluate::Expr<T> expr) { 96 for (const Symbol &symbol : evaluate::CollectSymbols(expr)) { 97 DoSymbol(symbol); 98 } 99 } 100 }; 101 102 bool ModFileWriter::WriteAll() { 103 // this flag affects character literals: force it to be consistent 104 auto restorer{ 105 common::ScopedSet(parser::useHexadecimalEscapeSequences, false)}; 106 WriteAll(context_.globalScope()); 107 return !context_.AnyFatalError(); 108 } 109 110 void ModFileWriter::WriteAll(const Scope &scope) { 111 for (const auto &child : scope.children()) { 112 WriteOne(child); 113 } 114 } 115 116 void ModFileWriter::WriteOne(const Scope &scope) { 117 if (scope.kind() == Scope::Kind::Module) { 118 auto *symbol{scope.symbol()}; 119 if (!symbol->test(Symbol::Flag::ModFile)) { 120 Write(*symbol); 121 } 122 WriteAll(scope); // write out submodules 123 } 124 } 125 126 // Construct the name of a module file. Non-empty ancestorName means submodule. 127 static std::string ModFileName(const SourceName &name, 128 const std::string &ancestorName, const std::string &suffix) { 129 std::string result{name.ToString() + suffix}; 130 return ancestorName.empty() ? result : ancestorName + '-' + result; 131 } 132 133 // Write the module file for symbol, which must be a module or submodule. 134 void ModFileWriter::Write(const Symbol &symbol) { 135 auto *ancestor{symbol.get<ModuleDetails>().ancestor()}; 136 auto ancestorName{ancestor ? ancestor->GetName().value().ToString() : ""s}; 137 auto path{context_.moduleDirectory() + '/' + 138 ModFileName(symbol.name(), ancestorName, context_.moduleFileSuffix())}; 139 PutSymbols(DEREF(symbol.scope())); 140 if (std::error_code error{ 141 WriteFile(path, GetAsString(symbol), context_.debugModuleWriter())}) { 142 context_.Say( 143 symbol.name(), "Error writing %s: %s"_err_en_US, path, error.message()); 144 } 145 } 146 147 // Return the entire body of the module file 148 // and clear saved uses, decls, and contains. 149 std::string ModFileWriter::GetAsString(const Symbol &symbol) { 150 std::string buf; 151 llvm::raw_string_ostream all{buf}; 152 auto &details{symbol.get<ModuleDetails>()}; 153 if (!details.isSubmodule()) { 154 all << "module " << symbol.name(); 155 } else { 156 auto *parent{details.parent()->symbol()}; 157 auto *ancestor{details.ancestor()->symbol()}; 158 all << "submodule(" << ancestor->name(); 159 if (parent != ancestor) { 160 all << ':' << parent->name(); 161 } 162 all << ") " << symbol.name(); 163 } 164 all << '\n' << uses_.str(); 165 uses_.str().clear(); 166 all << useExtraAttrs_.str(); 167 useExtraAttrs_.str().clear(); 168 all << decls_.str(); 169 decls_.str().clear(); 170 auto str{contains_.str()}; 171 contains_.str().clear(); 172 if (!str.empty()) { 173 all << "contains\n" << str; 174 } 175 all << "end\n"; 176 return all.str(); 177 } 178 179 // Put out the visible symbols from scope. 180 bool ModFileWriter::PutSymbols(const Scope &scope) { 181 SymbolVector sorted; 182 SymbolVector uses; 183 CollectSymbols(scope, sorted, uses); 184 std::string buf; // stuff after CONTAINS in derived type 185 llvm::raw_string_ostream typeBindings{buf}; 186 for (const Symbol &symbol : sorted) { 187 PutSymbol(typeBindings, symbol); 188 } 189 for (const Symbol &symbol : uses) { 190 PutUse(symbol); 191 } 192 if (auto str{typeBindings.str()}; !str.empty()) { 193 CHECK(scope.IsDerivedType()); 194 decls_ << "contains\n" << str; 195 return true; 196 } else { 197 return false; 198 } 199 } 200 201 // Emit a symbol to decls_, except for bindings in a derived type (type-bound 202 // procedures, type-bound generics, final procedures) which go to typeBindings. 203 void ModFileWriter::PutSymbol( 204 llvm::raw_ostream &typeBindings, const Symbol &symbol) { 205 std::visit(common::visitors{ 206 [&](const ModuleDetails &) { /* should be current module */ }, 207 [&](const DerivedTypeDetails &) { PutDerivedType(symbol); }, 208 [&](const SubprogramDetails &) { PutSubprogram(symbol); }, 209 [&](const GenericDetails &x) { 210 if (symbol.owner().IsDerivedType()) { 211 // generic binding 212 for (const Symbol &proc : x.specificProcs()) { 213 typeBindings << "generic::" << symbol.name() << "=>" 214 << proc.name() << '\n'; 215 } 216 } else { 217 PutGeneric(symbol); 218 if (x.specific()) { 219 PutSymbol(typeBindings, *x.specific()); 220 } 221 if (x.derivedType()) { 222 PutSymbol(typeBindings, *x.derivedType()); 223 } 224 } 225 }, 226 [&](const UseDetails &) { PutUse(symbol); }, 227 [](const UseErrorDetails &) {}, 228 [&](const ProcBindingDetails &x) { 229 bool deferred{symbol.attrs().test(Attr::DEFERRED)}; 230 typeBindings << "procedure"; 231 if (deferred) { 232 typeBindings << '(' << x.symbol().name() << ')'; 233 } 234 PutPassName(typeBindings, x.passName()); 235 auto attrs{symbol.attrs()}; 236 if (x.passName()) { 237 attrs.reset(Attr::PASS); 238 } 239 PutAttrs(typeBindings, attrs); 240 typeBindings << "::" << symbol.name(); 241 if (!deferred && x.symbol().name() != symbol.name()) { 242 typeBindings << "=>" << x.symbol().name(); 243 } 244 typeBindings << '\n'; 245 }, 246 [&](const NamelistDetails &x) { 247 decls_ << "namelist/" << symbol.name(); 248 char sep{'/'}; 249 for (const Symbol &object : x.objects()) { 250 decls_ << sep << object.name(); 251 sep = ','; 252 } 253 decls_ << '\n'; 254 }, 255 [&](const CommonBlockDetails &x) { 256 decls_ << "common/" << symbol.name(); 257 char sep = '/'; 258 for (const auto &object : x.objects()) { 259 decls_ << sep << object->name(); 260 sep = ','; 261 } 262 decls_ << '\n'; 263 if (symbol.attrs().test(Attr::BIND_C)) { 264 PutAttrs(decls_, symbol.attrs(), x.bindName(), ""s); 265 decls_ << "::/" << symbol.name() << "/\n"; 266 } 267 }, 268 [](const HostAssocDetails &) {}, 269 [](const MiscDetails &) {}, 270 [&](const auto &) { PutEntity(decls_, symbol); }, 271 }, 272 symbol.details()); 273 } 274 275 void ModFileWriter::PutDerivedType(const Symbol &typeSymbol) { 276 auto &details{typeSymbol.get<DerivedTypeDetails>()}; 277 PutAttrs(decls_ << "type", typeSymbol.attrs()); 278 if (const DerivedTypeSpec * extends{typeSymbol.GetParentTypeSpec()}) { 279 decls_ << ",extends(" << extends->name() << ')'; 280 } 281 decls_ << "::" << typeSymbol.name(); 282 auto &typeScope{*typeSymbol.scope()}; 283 if (!details.paramNames().empty()) { 284 char sep{'('}; 285 for (const auto &name : details.paramNames()) { 286 decls_ << sep << name; 287 sep = ','; 288 } 289 decls_ << ')'; 290 } 291 decls_ << '\n'; 292 if (details.sequence()) { 293 decls_ << "sequence\n"; 294 } 295 bool contains{PutSymbols(typeScope)}; 296 if (!details.finals().empty()) { 297 const char *sep{contains ? "final::" : "contains\nfinal::"}; 298 for (const auto &pair : details.finals()) { 299 decls_ << sep << pair.second->name(); 300 sep = ","; 301 } 302 if (*sep == ',') { 303 decls_ << '\n'; 304 } 305 } 306 decls_ << "end type\n"; 307 } 308 309 // Attributes that may be in a subprogram prefix 310 static const Attrs subprogramPrefixAttrs{Attr::ELEMENTAL, Attr::IMPURE, 311 Attr::MODULE, Attr::NON_RECURSIVE, Attr::PURE, Attr::RECURSIVE}; 312 313 void ModFileWriter::PutSubprogram(const Symbol &symbol) { 314 auto attrs{symbol.attrs()}; 315 auto &details{symbol.get<SubprogramDetails>()}; 316 Attrs bindAttrs{}; 317 if (attrs.test(Attr::BIND_C)) { 318 // bind(c) is a suffix, not prefix 319 bindAttrs.set(Attr::BIND_C, true); 320 attrs.set(Attr::BIND_C, false); 321 } 322 Attrs prefixAttrs{subprogramPrefixAttrs & attrs}; 323 // emit any non-prefix attributes in an attribute statement 324 attrs &= ~subprogramPrefixAttrs; 325 std::string ssBuf; 326 llvm::raw_string_ostream ss{ssBuf}; 327 PutAttrs(ss, attrs); 328 if (!ss.str().empty()) { 329 decls_ << ss.str().substr(1) << "::" << symbol.name() << '\n'; 330 } 331 bool isInterface{details.isInterface()}; 332 llvm::raw_ostream &os{isInterface ? decls_ : contains_}; 333 if (isInterface) { 334 os << "interface\n"; 335 } 336 PutAttrs(os, prefixAttrs, std::nullopt, ""s, " "s); 337 os << (details.isFunction() ? "function " : "subroutine "); 338 os << symbol.name() << '('; 339 int n = 0; 340 for (const auto &dummy : details.dummyArgs()) { 341 if (n++ > 0) { 342 os << ','; 343 } 344 if (dummy) { 345 os << dummy->name(); 346 } else { 347 os << "*"; 348 } 349 } 350 os << ')'; 351 PutAttrs(os, bindAttrs, details.bindName(), " "s, ""s); 352 if (details.isFunction()) { 353 const Symbol &result{details.result()}; 354 if (result.name() != symbol.name()) { 355 os << " result(" << result.name() << ')'; 356 } 357 } 358 os << '\n'; 359 360 // walk symbols, collect ones needed for interface 361 const Scope &scope{ 362 details.entryScope() ? *details.entryScope() : DEREF(symbol.scope())}; 363 SubprogramSymbolCollector collector{symbol, scope}; 364 collector.Collect(); 365 std::string typeBindingsBuf; 366 llvm::raw_string_ostream typeBindings{typeBindingsBuf}; 367 ModFileWriter writer{context_}; 368 for (const Symbol &need : collector.symbols()) { 369 writer.PutSymbol(typeBindings, need); 370 } 371 CHECK(typeBindings.str().empty()); 372 os << writer.uses_.str(); 373 for (const SourceName &import : collector.imports()) { 374 decls_ << "import::" << import << "\n"; 375 } 376 os << writer.decls_.str(); 377 os << "end\n"; 378 if (isInterface) { 379 os << "end interface\n"; 380 } 381 } 382 383 static bool IsIntrinsicOp(const Symbol &symbol) { 384 if (const auto *details{symbol.GetUltimate().detailsIf<GenericDetails>()}) { 385 return details->kind().IsIntrinsicOperator(); 386 } else { 387 return false; 388 } 389 } 390 391 static llvm::raw_ostream &PutGenericName( 392 llvm::raw_ostream &os, const Symbol &symbol) { 393 if (IsGenericDefinedOp(symbol)) { 394 return os << "operator(" << symbol.name() << ')'; 395 } else { 396 return os << symbol.name(); 397 } 398 } 399 400 void ModFileWriter::PutGeneric(const Symbol &symbol) { 401 const auto &genericOwner{symbol.owner()}; 402 auto &details{symbol.get<GenericDetails>()}; 403 PutGenericName(decls_ << "interface ", symbol) << '\n'; 404 for (const Symbol &specific : details.specificProcs()) { 405 if (specific.owner() == genericOwner) { 406 decls_ << "procedure::" << specific.name() << '\n'; 407 } 408 } 409 decls_ << "end interface\n"; 410 if (symbol.attrs().test(Attr::PRIVATE)) { 411 PutGenericName(decls_ << "private::", symbol) << '\n'; 412 } 413 } 414 415 void ModFileWriter::PutUse(const Symbol &symbol) { 416 auto &details{symbol.get<UseDetails>()}; 417 auto &use{details.symbol()}; 418 uses_ << "use " << GetUsedModule(details).name(); 419 PutGenericName(uses_ << ",only:", symbol); 420 // Can have intrinsic op with different local-name and use-name 421 // (e.g. `operator(<)` and `operator(.lt.)`) but rename is not allowed 422 if (!IsIntrinsicOp(symbol) && use.name() != symbol.name()) { 423 PutGenericName(uses_ << "=>", use); 424 } 425 uses_ << '\n'; 426 PutUseExtraAttr(Attr::VOLATILE, symbol, use); 427 PutUseExtraAttr(Attr::ASYNCHRONOUS, symbol, use); 428 } 429 430 // We have "USE local => use" in this module. If attr was added locally 431 // (i.e. on local but not on use), also write it out in the mod file. 432 void ModFileWriter::PutUseExtraAttr( 433 Attr attr, const Symbol &local, const Symbol &use) { 434 if (local.attrs().test(attr) && !use.attrs().test(attr)) { 435 PutAttr(useExtraAttrs_, attr) << "::"; 436 useExtraAttrs_ << local.name() << '\n'; 437 } 438 } 439 440 // Collect the symbols of this scope sorted by their original order, not name. 441 // Namelists are an exception: they are sorted after other symbols. 442 void CollectSymbols( 443 const Scope &scope, SymbolVector &sorted, SymbolVector &uses) { 444 SymbolVector namelist; 445 std::size_t commonSize{scope.commonBlocks().size()}; 446 auto symbols{scope.GetSymbols()}; 447 sorted.reserve(symbols.size() + commonSize); 448 for (SymbolRef symbol : symbols) { 449 if (!symbol->test(Symbol::Flag::ParentComp)) { 450 if (symbol->has<NamelistDetails>()) { 451 namelist.push_back(symbol); 452 } else { 453 sorted.push_back(symbol); 454 } 455 if (const auto *details{symbol->detailsIf<GenericDetails>()}) { 456 uses.insert(uses.end(), details->uses().begin(), details->uses().end()); 457 } 458 } 459 } 460 sorted.insert(sorted.end(), namelist.begin(), namelist.end()); 461 for (const auto &pair : scope.commonBlocks()) { 462 sorted.push_back(*pair.second); 463 } 464 std::sort(sorted.end() - commonSize, sorted.end()); 465 } 466 467 void PutEntity(llvm::raw_ostream &os, const Symbol &symbol) { 468 std::visit( 469 common::visitors{ 470 [&](const ObjectEntityDetails &) { PutObjectEntity(os, symbol); }, 471 [&](const ProcEntityDetails &) { PutProcEntity(os, symbol); }, 472 [&](const TypeParamDetails &) { PutTypeParam(os, symbol); }, 473 [&](const auto &) { 474 common::die("PutEntity: unexpected details: %s", 475 DetailsToString(symbol.details()).c_str()); 476 }, 477 }, 478 symbol.details()); 479 } 480 481 void PutShapeSpec(llvm::raw_ostream &os, const ShapeSpec &x) { 482 if (x.lbound().isAssumed()) { 483 CHECK(x.ubound().isAssumed()); 484 os << ".."; 485 } else { 486 if (!x.lbound().isDeferred()) { 487 PutBound(os, x.lbound()); 488 } 489 os << ':'; 490 if (!x.ubound().isDeferred()) { 491 PutBound(os, x.ubound()); 492 } 493 } 494 } 495 void PutShape( 496 llvm::raw_ostream &os, const ArraySpec &shape, char open, char close) { 497 if (!shape.empty()) { 498 os << open; 499 bool first{true}; 500 for (const auto &shapeSpec : shape) { 501 if (first) { 502 first = false; 503 } else { 504 os << ','; 505 } 506 PutShapeSpec(os, shapeSpec); 507 } 508 os << close; 509 } 510 } 511 512 void PutObjectEntity(llvm::raw_ostream &os, const Symbol &symbol) { 513 auto &details{symbol.get<ObjectEntityDetails>()}; 514 PutEntity( 515 os, symbol, [&]() { PutType(os, DEREF(symbol.GetType())); }, 516 symbol.attrs()); 517 PutShape(os, details.shape(), '(', ')'); 518 PutShape(os, details.coshape(), '[', ']'); 519 PutInit(os, symbol, details.init()); 520 os << '\n'; 521 } 522 523 void PutProcEntity(llvm::raw_ostream &os, const Symbol &symbol) { 524 if (symbol.attrs().test(Attr::INTRINSIC)) { 525 os << "intrinsic::" << symbol.name() << '\n'; 526 return; 527 } 528 const auto &details{symbol.get<ProcEntityDetails>()}; 529 const ProcInterface &interface{details.interface()}; 530 Attrs attrs{symbol.attrs()}; 531 if (details.passName()) { 532 attrs.reset(Attr::PASS); 533 } 534 PutEntity( 535 os, symbol, 536 [&]() { 537 os << "procedure("; 538 if (interface.symbol()) { 539 os << interface.symbol()->name(); 540 } else if (interface.type()) { 541 PutType(os, *interface.type()); 542 } 543 os << ')'; 544 PutPassName(os, details.passName()); 545 }, 546 attrs); 547 os << '\n'; 548 } 549 550 void PutPassName( 551 llvm::raw_ostream &os, const std::optional<SourceName> &passName) { 552 if (passName) { 553 os << ",pass(" << *passName << ')'; 554 } 555 } 556 void PutTypeParam(llvm::raw_ostream &os, const Symbol &symbol) { 557 auto &details{symbol.get<TypeParamDetails>()}; 558 PutEntity( 559 os, symbol, 560 [&]() { 561 PutType(os, DEREF(symbol.GetType())); 562 PutLower(os << ',', common::EnumToString(details.attr())); 563 }, 564 symbol.attrs()); 565 PutInit(os, details.init()); 566 os << '\n'; 567 } 568 569 void PutInit( 570 llvm::raw_ostream &os, const Symbol &symbol, const MaybeExpr &init) { 571 if (init) { 572 if (symbol.attrs().test(Attr::PARAMETER) || 573 symbol.owner().IsDerivedType()) { 574 os << (symbol.attrs().test(Attr::POINTER) ? "=>" : "="); 575 init->AsFortran(os); 576 } 577 } 578 } 579 580 void PutInit(llvm::raw_ostream &os, const MaybeIntExpr &init) { 581 if (init) { 582 init->AsFortran(os << '='); 583 } 584 } 585 586 void PutBound(llvm::raw_ostream &os, const Bound &x) { 587 if (x.isAssumed()) { 588 os << '*'; 589 } else if (x.isDeferred()) { 590 os << ':'; 591 } else { 592 x.GetExplicit()->AsFortran(os); 593 } 594 } 595 596 // Write an entity (object or procedure) declaration. 597 // writeType is called to write out the type. 598 void PutEntity(llvm::raw_ostream &os, const Symbol &symbol, 599 std::function<void()> writeType, Attrs attrs) { 600 writeType(); 601 MaybeExpr bindName; 602 std::visit(common::visitors{ 603 [&](const SubprogramDetails &x) { bindName = x.bindName(); }, 604 [&](const ObjectEntityDetails &x) { bindName = x.bindName(); }, 605 [&](const ProcEntityDetails &x) { bindName = x.bindName(); }, 606 [&](const auto &) {}, 607 }, 608 symbol.details()); 609 PutAttrs(os, attrs, bindName); 610 os << "::" << symbol.name(); 611 } 612 613 // Put out each attribute to os, surrounded by `before` and `after` and 614 // mapped to lower case. 615 llvm::raw_ostream &PutAttrs(llvm::raw_ostream &os, Attrs attrs, 616 const MaybeExpr &bindName, std::string before, std::string after) { 617 attrs.set(Attr::PUBLIC, false); // no need to write PUBLIC 618 attrs.set(Attr::EXTERNAL, false); // no need to write EXTERNAL 619 if (bindName) { 620 bindName->AsFortran(os << before << "bind(c, name=") << ')' << after; 621 attrs.set(Attr::BIND_C, false); 622 } 623 for (std::size_t i{0}; i < Attr_enumSize; ++i) { 624 Attr attr{static_cast<Attr>(i)}; 625 if (attrs.test(attr)) { 626 PutAttr(os << before, attr) << after; 627 } 628 } 629 return os; 630 } 631 632 llvm::raw_ostream &PutAttr(llvm::raw_ostream &os, Attr attr) { 633 return PutLower(os, AttrToString(attr)); 634 } 635 636 llvm::raw_ostream &PutType(llvm::raw_ostream &os, const DeclTypeSpec &type) { 637 return PutLower(os, type.AsFortran()); 638 } 639 640 llvm::raw_ostream &PutLower(llvm::raw_ostream &os, const std::string &str) { 641 for (char c : str) { 642 os << parser::ToLowerCaseLetter(c); 643 } 644 return os; 645 } 646 647 struct Temp { 648 Temp(int fd, std::string path) : fd{fd}, path{path} {} 649 Temp(Temp &&t) : fd{std::exchange(t.fd, -1)}, path{std::move(t.path)} {} 650 ~Temp() { 651 if (fd >= 0) { 652 llvm::sys::fs::file_t native{llvm::sys::fs::convertFDToNativeFile(fd)}; 653 llvm::sys::fs::closeFile(native); 654 llvm::sys::fs::remove(path.c_str()); 655 } 656 } 657 int fd; 658 std::string path; 659 }; 660 661 // Create a temp file in the same directory and with the same suffix as path. 662 // Return an open file descriptor and its path. 663 static llvm::ErrorOr<Temp> MkTemp(const std::string &path) { 664 auto length{path.length()}; 665 auto dot{path.find_last_of("./")}; 666 std::string suffix{ 667 dot < length && path[dot] == '.' ? path.substr(dot + 1) : ""}; 668 CHECK(length > suffix.length() && 669 path.substr(length - suffix.length()) == suffix); 670 auto prefix{path.substr(0, length - suffix.length())}; 671 int fd; 672 llvm::SmallString<16> tempPath; 673 if (std::error_code err{llvm::sys::fs::createUniqueFile( 674 prefix + "%%%%%%" + suffix, fd, tempPath)}) { 675 return err; 676 } 677 return Temp{fd, tempPath.c_str()}; 678 } 679 680 // Write the module file at path, prepending header. If an error occurs, 681 // return errno, otherwise 0. 682 static std::error_code WriteFile( 683 const std::string &path, const std::string &contents, bool debug) { 684 auto header{std::string{ModHeader::bom} + ModHeader::magic + 685 CheckSum(contents) + ModHeader::terminator}; 686 if (debug) { 687 llvm::dbgs() << "Processing module " << path << ": "; 688 } 689 if (FileContentsMatch(path, header, contents)) { 690 if (debug) { 691 llvm::dbgs() << "module unchanged, not writing\n"; 692 } 693 return {}; 694 } 695 llvm::ErrorOr<Temp> temp{MkTemp(path)}; 696 if (!temp) { 697 return temp.getError(); 698 } 699 llvm::raw_fd_ostream writer(temp->fd, /*shouldClose=*/false); 700 writer << header; 701 writer << contents; 702 writer.flush(); 703 if (writer.has_error()) { 704 return writer.error(); 705 } 706 if (debug) { 707 llvm::dbgs() << "module written\n"; 708 } 709 return llvm::sys::fs::rename(temp->path, path); 710 } 711 712 // Return true if the stream matches what we would write for the mod file. 713 static bool FileContentsMatch(const std::string &path, 714 const std::string &header, const std::string &contents) { 715 std::size_t hsize{header.size()}; 716 std::size_t csize{contents.size()}; 717 auto buf_or{llvm::MemoryBuffer::getFile(path)}; 718 if (!buf_or) { 719 return false; 720 } 721 auto buf = std::move(buf_or.get()); 722 if (buf->getBufferSize() != hsize + csize) { 723 return false; 724 } 725 if (!std::equal(header.begin(), header.end(), buf->getBufferStart(), 726 buf->getBufferStart() + hsize)) { 727 return false; 728 } 729 730 return std::equal(contents.begin(), contents.end(), 731 buf->getBufferStart() + hsize, buf->getBufferEnd()); 732 } 733 734 // Compute a simple hash of the contents of a module file and 735 // return it as a string of hex digits. 736 // This uses the Fowler-Noll-Vo hash function. 737 static std::string CheckSum(const std::string_view &contents) { 738 std::uint64_t hash{0xcbf29ce484222325ull}; 739 for (char c : contents) { 740 hash ^= c & 0xff; 741 hash *= 0x100000001b3; 742 } 743 static const char *digits = "0123456789abcdef"; 744 std::string result(ModHeader::sumLen, '0'); 745 for (size_t i{ModHeader::sumLen}; hash != 0; hash >>= 4) { 746 result[--i] = digits[hash & 0xf]; 747 } 748 return result; 749 } 750 751 static bool VerifyHeader(llvm::ArrayRef<char> content) { 752 std::string_view sv{content.data(), content.size()}; 753 if (sv.substr(0, ModHeader::magicLen) != ModHeader::magic) { 754 return false; 755 } 756 std::string_view expectSum{sv.substr(ModHeader::magicLen, ModHeader::sumLen)}; 757 std::string actualSum{CheckSum(sv.substr(ModHeader::len))}; 758 return expectSum == actualSum; 759 } 760 761 Scope *ModFileReader::Read(const SourceName &name, Scope *ancestor) { 762 std::string ancestorName; // empty for module 763 if (ancestor) { 764 if (auto *scope{ancestor->FindSubmodule(name)}) { 765 return scope; 766 } 767 ancestorName = ancestor->GetName().value().ToString(); 768 } else { 769 auto it{context_.globalScope().find(name)}; 770 if (it != context_.globalScope().end()) { 771 return it->second->scope(); 772 } 773 } 774 parser::Parsing parsing{context_.allCookedSources()}; 775 parser::Options options; 776 options.isModuleFile = true; 777 options.features.Enable(common::LanguageFeature::BackslashEscapes); 778 options.searchDirectories = context_.searchDirectories(); 779 auto path{ModFileName(name, ancestorName, context_.moduleFileSuffix())}; 780 const auto *sourceFile{parsing.Prescan(path, options)}; 781 if (parsing.messages().AnyFatalError()) { 782 for (auto &msg : parsing.messages().messages()) { 783 std::string str{msg.ToString()}; 784 Say(name, ancestorName, parser::MessageFixedText{str.c_str(), str.size()}, 785 path); 786 } 787 return nullptr; 788 } 789 CHECK(sourceFile); 790 if (!VerifyHeader(sourceFile->content())) { 791 Say(name, ancestorName, "File has invalid checksum: %s"_en_US, 792 sourceFile->path()); 793 return nullptr; 794 } 795 llvm::raw_null_ostream NullStream; 796 parsing.Parse(NullStream); 797 auto &parseTree{parsing.parseTree()}; 798 if (!parsing.messages().empty() || !parsing.consumedWholeFile() || 799 !parseTree) { 800 Say(name, ancestorName, "Module file is corrupt: %s"_err_en_US, 801 sourceFile->path()); 802 return nullptr; 803 } 804 Scope *parentScope; // the scope this module/submodule goes into 805 if (!ancestor) { 806 parentScope = &context_.globalScope(); 807 } else if (std::optional<SourceName> parent{GetSubmoduleParent(*parseTree)}) { 808 parentScope = Read(*parent, ancestor); 809 } else { 810 parentScope = ancestor; 811 } 812 ResolveNames(context_, *parseTree); 813 const auto &it{parentScope->find(name)}; 814 if (it == parentScope->end()) { 815 return nullptr; 816 } 817 auto &modSymbol{*it->second}; 818 modSymbol.set(Symbol::Flag::ModFile); 819 return modSymbol.scope(); 820 } 821 822 parser::Message &ModFileReader::Say(const SourceName &name, 823 const std::string &ancestor, parser::MessageFixedText &&msg, 824 const std::string &arg) { 825 return context_ 826 .Say(name, 827 ancestor.empty() 828 ? "Error reading module file for module '%s'"_err_en_US 829 : "Error reading module file for submodule '%s' of module '%s'"_err_en_US, 830 name, ancestor) 831 .Attach(name, std::move(msg), arg); 832 } 833 834 // program was read from a .mod file for a submodule; return the name of the 835 // submodule's parent submodule, nullptr if none. 836 static std::optional<SourceName> GetSubmoduleParent( 837 const parser::Program &program) { 838 CHECK(program.v.size() == 1); 839 auto &unit{program.v.front()}; 840 auto &submod{std::get<common::Indirection<parser::Submodule>>(unit.u)}; 841 auto &stmt{ 842 std::get<parser::Statement<parser::SubmoduleStmt>>(submod.value().t)}; 843 auto &parentId{std::get<parser::ParentIdentifier>(stmt.statement.t)}; 844 if (auto &parent{std::get<std::optional<parser::Name>>(parentId.t)}) { 845 return parent->source; 846 } else { 847 return std::nullopt; 848 } 849 } 850 851 void SubprogramSymbolCollector::Collect() { 852 const auto &details{symbol_.get<SubprogramDetails>()}; 853 isInterface_ = details.isInterface(); 854 for (const Symbol *dummyArg : details.dummyArgs()) { 855 if (dummyArg) { 856 DoSymbol(*dummyArg); 857 } 858 } 859 if (details.isFunction()) { 860 DoSymbol(details.result()); 861 } 862 for (const auto &pair : scope_) { 863 const Symbol &symbol{*pair.second}; 864 if (const auto *useDetails{symbol.detailsIf<UseDetails>()}) { 865 if (useSet_.count(useDetails->symbol().GetUltimate()) > 0) { 866 need_.push_back(symbol); 867 } 868 } 869 } 870 } 871 872 void SubprogramSymbolCollector::DoSymbol(const Symbol &symbol) { 873 DoSymbol(symbol.name(), symbol); 874 } 875 876 // Do symbols this one depends on; then add to need_ 877 void SubprogramSymbolCollector::DoSymbol( 878 const SourceName &name, const Symbol &symbol) { 879 const auto &scope{symbol.owner()}; 880 if (scope != scope_ && !scope.IsDerivedType()) { 881 if (scope != scope_.parent()) { 882 useSet_.insert(symbol); 883 } 884 if (NeedImport(name, symbol)) { 885 imports_.insert(name); 886 } 887 return; 888 } 889 if (!needSet_.insert(symbol).second) { 890 return; // already done 891 } 892 std::visit(common::visitors{ 893 [this](const ObjectEntityDetails &details) { 894 for (const ShapeSpec &spec : details.shape()) { 895 DoBound(spec.lbound()); 896 DoBound(spec.ubound()); 897 } 898 for (const ShapeSpec &spec : details.coshape()) { 899 DoBound(spec.lbound()); 900 DoBound(spec.ubound()); 901 } 902 if (const Symbol * commonBlock{details.commonBlock()}) { 903 DoSymbol(*commonBlock); 904 } 905 }, 906 [this](const CommonBlockDetails &details) { 907 for (const auto &object : details.objects()) { 908 DoSymbol(*object); 909 } 910 }, 911 [](const auto &) {}, 912 }, 913 symbol.details()); 914 if (!symbol.has<UseDetails>()) { 915 DoType(symbol.GetType()); 916 } 917 if (!scope.IsDerivedType()) { 918 need_.push_back(symbol); 919 } 920 } 921 922 void SubprogramSymbolCollector::DoType(const DeclTypeSpec *type) { 923 if (!type) { 924 return; 925 } 926 switch (type->category()) { 927 case DeclTypeSpec::Numeric: 928 case DeclTypeSpec::Logical: 929 break; // nothing to do 930 case DeclTypeSpec::Character: 931 DoParamValue(type->characterTypeSpec().length()); 932 break; 933 default: 934 if (const DerivedTypeSpec * derived{type->AsDerived()}) { 935 const auto &typeSymbol{derived->typeSymbol()}; 936 if (const DerivedTypeSpec * extends{typeSymbol.GetParentTypeSpec()}) { 937 DoSymbol(extends->name(), extends->typeSymbol()); 938 } 939 for (const auto &pair : derived->parameters()) { 940 DoParamValue(pair.second); 941 } 942 for (const auto &pair : *typeSymbol.scope()) { 943 const Symbol &comp{*pair.second}; 944 DoSymbol(comp); 945 } 946 DoSymbol(derived->name(), derived->typeSymbol()); 947 } 948 } 949 } 950 951 void SubprogramSymbolCollector::DoBound(const Bound &bound) { 952 if (const MaybeSubscriptIntExpr & expr{bound.GetExplicit()}) { 953 DoExpr(*expr); 954 } 955 } 956 void SubprogramSymbolCollector::DoParamValue(const ParamValue ¶mValue) { 957 if (const auto &expr{paramValue.GetExplicit()}) { 958 DoExpr(*expr); 959 } 960 } 961 962 // Do we need a IMPORT of this symbol into an interface block? 963 bool SubprogramSymbolCollector::NeedImport( 964 const SourceName &name, const Symbol &symbol) { 965 if (!isInterface_) { 966 return false; 967 } else if (symbol.owner() != scope_.parent()) { 968 // detect import from parent of use-associated symbol 969 // can be null in the case of a use-associated derived type's parent type 970 const auto *found{scope_.FindSymbol(name)}; 971 CHECK(found || symbol.has<DerivedTypeDetails>()); 972 return found && found->has<UseDetails>() && found->owner() != scope_; 973 } else { 974 return true; 975 } 976 } 977 978 } // namespace Fortran::semantics 979