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