1 //===-- lib/Semantics/mod-file.cpp ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "resolve-names.h"
11 #include "flang/Evaluate/tools.h"
12 #include "flang/Parser/message.h"
13 #include "flang/Parser/parsing.h"
14 #include "flang/Semantics/scope.h"
15 #include "flang/Semantics/semantics.h"
16 #include "flang/Semantics/symbol.h"
17 #include "flang/Semantics/tools.h"
23 #include <string_view>
26 #include <sys/types.h>
30 namespace Fortran::semantics {
32 using namespace parser::literals;
34 // The first line of a file that identifies it as a .mod file.
35 // The first three bytes are a Unicode byte order mark that ensures
36 // that the module file is decoded as UTF-8 even if source files
37 // are using another encoding.
39 static constexpr const char bom[3 + 1]{"\xef\xbb\xbf"};
40 static constexpr int magicLen{13};
41 static constexpr int sumLen{16};
42 static constexpr const char magic[magicLen + 1]{"!mod$ v1 sum:"};
43 static constexpr char terminator{'\n'};
44 static constexpr int len{magicLen + 1 + sumLen};
47 static std::optional<SourceName> GetSubmoduleParent(const parser::Program &);
48 static SymbolVector CollectSymbols(const Scope &);
49 static void PutEntity(std::ostream &, const Symbol &);
50 static void PutObjectEntity(std::ostream &, const Symbol &);
51 static void PutProcEntity(std::ostream &, const Symbol &);
52 static void PutPassName(std::ostream &, const std::optional<SourceName> &);
53 static void PutTypeParam(std::ostream &, const Symbol &);
54 static void PutEntity(
55 std::ostream &, const Symbol &, std::function<void()>, Attrs);
56 static void PutInit(std::ostream &, const Symbol &, const MaybeExpr &);
57 static void PutInit(std::ostream &, const MaybeIntExpr &);
58 static void PutBound(std::ostream &, const Bound &);
59 static std::ostream &PutAttrs(std::ostream &, Attrs,
60 const MaybeExpr & = std::nullopt, std::string before = ","s,
61 std::string after = ""s);
62 static std::ostream &PutAttr(std::ostream &, Attr);
63 static std::ostream &PutType(std::ostream &, const DeclTypeSpec &);
64 static std::ostream &PutLower(std::ostream &, const std::string &);
65 static int WriteFile(const std::string &, const std::string &);
66 static bool FileContentsMatch(
67 const std::string &, const std::string &, const std::string &);
68 static std::size_t GetFileSize(const std::string &);
69 static std::string CheckSum(const std::string_view &);
71 // Collect symbols needed for a subprogram interface
72 class SubprogramSymbolCollector {
74 SubprogramSymbolCollector(const Symbol &symbol)
75 : symbol_{symbol}, scope_{DEREF(symbol.scope())} {}
76 const SymbolVector &symbols() const { return need_; }
77 const std::set<SourceName> &imports() const { return imports_; }
81 const Symbol &symbol_;
83 bool isInterface_{false};
84 SymbolVector need_; // symbols that are needed
85 SymbolSet needSet_; // symbols already in need_
86 SymbolSet useSet_; // use-associations that might be needed
87 std::set<SourceName> imports_; // imports from host that are needed
89 void DoSymbol(const Symbol &);
90 void DoSymbol(const SourceName &, const Symbol &);
91 void DoType(const DeclTypeSpec *);
92 void DoBound(const Bound &);
93 void DoParamValue(const ParamValue &);
94 bool NeedImport(const SourceName &, const Symbol &);
96 template<typename T> void DoExpr(evaluate::Expr<T> expr) {
97 for (const Symbol &symbol : evaluate::CollectSymbols(expr)) {
103 bool ModFileWriter::WriteAll() {
104 WriteAll(context_.globalScope());
105 return !context_.AnyFatalError();
108 void ModFileWriter::WriteAll(const Scope &scope) {
109 for (const auto &child : scope.children()) {
114 void ModFileWriter::WriteOne(const Scope &scope) {
115 if (scope.kind() == Scope::Kind::Module) {
116 auto *symbol{scope.symbol()};
117 if (!symbol->test(Symbol::Flag::ModFile)) {
120 WriteAll(scope); // write out submodules
124 // Construct the name of a module file. Non-empty ancestorName means submodule.
125 static std::string ModFileName(const SourceName &name,
126 const std::string &ancestorName, const std::string &suffix) {
127 std::string result{name.ToString() + suffix};
128 return ancestorName.empty() ? result : ancestorName + '-' + result;
131 // Write the module file for symbol, which must be a module or submodule.
132 void ModFileWriter::Write(const Symbol &symbol) {
133 auto *ancestor{symbol.get<ModuleDetails>().ancestor()};
134 auto ancestorName{ancestor ? ancestor->GetName().value().ToString() : ""s};
135 auto path{context_.moduleDirectory() + '/' +
136 ModFileName(symbol.name(), ancestorName, context_.moduleFileSuffix())};
137 PutSymbols(DEREF(symbol.scope()));
138 if (int error{WriteFile(path, GetAsString(symbol))}) {
139 context_.Say(symbol.name(), "Error writing %s: %s"_err_en_US, path,
140 std::strerror(error));
144 // Return the entire body of the module file
145 // and clear saved uses, decls, and contains.
146 std::string ModFileWriter::GetAsString(const Symbol &symbol) {
147 std::stringstream all;
148 auto &details{symbol.get<ModuleDetails>()};
149 if (!details.isSubmodule()) {
150 all << "module " << symbol.name();
152 auto *parent{details.parent()->symbol()};
153 auto *ancestor{details.ancestor()->symbol()};
154 all << "submodule(" << ancestor->name();
155 if (parent != ancestor) {
156 all << ':' << parent->name();
158 all << ") " << symbol.name();
160 all << '\n' << uses_.str();
162 all << useExtraAttrs_.str();
163 useExtraAttrs_.str(""s);
166 auto str{contains_.str()};
169 all << "contains\n" << str;
175 // Put out the visible symbols from scope.
176 void ModFileWriter::PutSymbols(const Scope &scope) {
177 std::stringstream typeBindings; // stuff after CONTAINS in derived type
178 for (const Symbol &symbol : CollectSymbols(scope)) {
179 PutSymbol(typeBindings, symbol);
181 if (auto str{typeBindings.str()}; !str.empty()) {
182 CHECK(scope.IsDerivedType());
183 decls_ << "contains\n" << str;
187 // Emit a symbol to decls_, except for bindings in a derived type (type-bound
188 // procedures, type-bound generics, final procedures) which go to typeBindings.
189 void ModFileWriter::PutSymbol(
190 std::stringstream &typeBindings, const Symbol &symbol) {
193 [&](const ModuleDetails &) { /* should be current module */ },
194 [&](const DerivedTypeDetails &) { PutDerivedType(symbol); },
195 [&](const SubprogramDetails &) { PutSubprogram(symbol); },
196 [&](const GenericDetails &x) {
197 if (symbol.owner().IsDerivedType()) {
199 for (const Symbol &proc : x.specificProcs()) {
200 typeBindings << "generic::" << symbol.name() << "=>"
201 << proc.name() << '\n';
206 PutSymbol(typeBindings, *x.specific());
208 if (x.derivedType()) {
209 PutSymbol(typeBindings, *x.derivedType());
213 [&](const UseDetails &) { PutUse(symbol); },
214 [](const UseErrorDetails &) {},
215 [&](const ProcBindingDetails &x) {
216 bool deferred{symbol.attrs().test(Attr::DEFERRED)};
217 typeBindings << "procedure";
219 typeBindings << '(' << x.symbol().name() << ')';
221 PutPassName(typeBindings, x.passName());
222 auto attrs{symbol.attrs()};
224 attrs.reset(Attr::PASS);
226 PutAttrs(typeBindings, attrs);
227 typeBindings << "::" << symbol.name();
228 if (!deferred && x.symbol().name() != symbol.name()) {
229 typeBindings << "=>" << x.symbol().name();
231 typeBindings << '\n';
233 [&](const NamelistDetails &x) {
234 decls_ << "namelist/" << symbol.name();
236 for (const Symbol &object : x.objects()) {
237 decls_ << sep << object.name();
242 [&](const CommonBlockDetails &x) {
243 decls_ << "common/" << symbol.name();
245 for (const Symbol &object : x.objects()) {
246 decls_ << sep << object.name();
250 if (symbol.attrs().test(Attr::BIND_C)) {
251 PutAttrs(decls_, symbol.attrs(), x.bindName(), ""s);
252 decls_ << "::/" << symbol.name() << "/\n";
255 [&](const FinalProcDetails &) {
256 typeBindings << "final::" << symbol.name() << '\n';
258 [](const HostAssocDetails &) {},
259 [](const MiscDetails &) {},
260 [&](const auto &) { PutEntity(decls_, symbol); },
265 void ModFileWriter::PutDerivedType(const Symbol &typeSymbol) {
266 auto &details{typeSymbol.get<DerivedTypeDetails>()};
267 PutAttrs(decls_ << "type", typeSymbol.attrs());
268 if (const DerivedTypeSpec * extends{typeSymbol.GetParentTypeSpec()}) {
269 decls_ << ",extends(" << extends->name() << ')';
271 decls_ << "::" << typeSymbol.name();
272 auto &typeScope{*typeSymbol.scope()};
273 if (!details.paramNames().empty()) {
275 for (const auto &name : details.paramNames()) {
276 decls_ << sep << name;
282 if (details.sequence()) {
283 decls_ << "sequence\n";
285 PutSymbols(typeScope);
286 decls_ << "end type\n";
289 // Attributes that may be in a subprogram prefix
290 static const Attrs subprogramPrefixAttrs{Attr::ELEMENTAL, Attr::IMPURE,
291 Attr::MODULE, Attr::NON_RECURSIVE, Attr::PURE, Attr::RECURSIVE};
293 void ModFileWriter::PutSubprogram(const Symbol &symbol) {
294 auto attrs{symbol.attrs()};
295 auto &details{symbol.get<SubprogramDetails>()};
297 if (attrs.test(Attr::BIND_C)) {
298 // bind(c) is a suffix, not prefix
299 bindAttrs.set(Attr::BIND_C, true);
300 attrs.set(Attr::BIND_C, false);
302 Attrs prefixAttrs{subprogramPrefixAttrs & attrs};
303 // emit any non-prefix attributes in an attribute statement
304 attrs &= ~subprogramPrefixAttrs;
305 std::stringstream ss;
307 if (!ss.str().empty()) {
308 decls_ << ss.str().substr(1) << "::" << symbol.name() << '\n';
310 bool isInterface{details.isInterface()};
311 std::ostream &os{isInterface ? decls_ : contains_};
315 PutAttrs(os, prefixAttrs, std::nullopt, ""s, " "s);
316 os << (details.isFunction() ? "function " : "subroutine ");
317 os << symbol.name() << '(';
319 for (const auto &dummy : details.dummyArgs()) {
326 PutAttrs(os, bindAttrs, details.bindName(), " "s, ""s);
327 if (details.isFunction()) {
328 const Symbol &result{details.result()};
329 if (result.name() != symbol.name()) {
330 os << " result(" << result.name() << ')';
335 // walk symbols, collect ones needed
336 ModFileWriter writer{context_};
337 std::stringstream typeBindings;
338 SubprogramSymbolCollector collector{symbol};
340 for (const Symbol &need : collector.symbols()) {
341 writer.PutSymbol(typeBindings, need);
343 CHECK(typeBindings.str().empty());
344 os << writer.uses_.str();
345 for (const SourceName &import : collector.imports()) {
346 decls_ << "import::" << import << "\n";
348 os << writer.decls_.str();
351 os << "end interface\n";
355 static bool IsIntrinsicOp(const Symbol &symbol) {
356 if (const auto *details{symbol.GetUltimate().detailsIf<GenericDetails>()}) {
357 return details->kind().IsIntrinsicOperator();
363 static std::ostream &PutGenericName(std::ostream &os, const Symbol &symbol) {
364 if (IsGenericDefinedOp(symbol)) {
365 return os << "operator(" << symbol.name() << ')';
367 return os << symbol.name();
371 void ModFileWriter::PutGeneric(const Symbol &symbol) {
372 auto &details{symbol.get<GenericDetails>()};
373 PutGenericName(decls_ << "interface ", symbol) << '\n';
374 for (const Symbol &specific : details.specificProcs()) {
375 decls_ << "procedure::" << specific.name() << '\n';
377 decls_ << "end interface\n";
378 if (symbol.attrs().test(Attr::PRIVATE)) {
379 PutGenericName(decls_ << "private::", symbol) << '\n';
383 void ModFileWriter::PutUse(const Symbol &symbol) {
384 auto &details{symbol.get<UseDetails>()};
385 auto &use{details.symbol()};
386 uses_ << "use " << details.module().name();
387 PutGenericName(uses_ << ",only:", symbol);
388 // Can have intrinsic op with different local-name and use-name
389 // (e.g. `operator(<)` and `operator(.lt.)`) but rename is not allowed
390 if (!IsIntrinsicOp(symbol) && use.name() != symbol.name()) {
391 PutGenericName(uses_ << "=>", use);
394 PutUseExtraAttr(Attr::VOLATILE, symbol, use);
395 PutUseExtraAttr(Attr::ASYNCHRONOUS, symbol, use);
398 // We have "USE local => use" in this module. If attr was added locally
399 // (i.e. on local but not on use), also write it out in the mod file.
400 void ModFileWriter::PutUseExtraAttr(
401 Attr attr, const Symbol &local, const Symbol &use) {
402 if (local.attrs().test(attr) && !use.attrs().test(attr)) {
403 PutAttr(useExtraAttrs_, attr) << "::";
404 useExtraAttrs_ << local.name() << '\n';
408 // Collect the symbols of this scope sorted by their original order, not name.
409 // Namelists are an exception: they are sorted after other symbols.
410 SymbolVector CollectSymbols(const Scope &scope) {
411 SymbolSet symbols; // to prevent duplicates
413 SymbolVector namelist;
415 sorted.reserve(scope.size() + scope.commonBlocks().size());
416 for (const auto &pair : scope) {
417 const Symbol &symbol{*pair.second};
418 if (!symbol.test(Symbol::Flag::ParentComp)) {
419 if (symbols.insert(symbol).second) {
420 if (symbol.has<NamelistDetails>()) {
421 namelist.push_back(symbol);
423 sorted.push_back(symbol);
428 for (const auto &pair : scope.commonBlocks()) {
429 const Symbol &symbol{*pair.second};
430 if (symbols.insert(symbol).second) {
431 common.push_back(symbol);
434 // sort normal symbols, then namelists, then common blocks:
435 auto cursor{sorted.begin()};
436 std::sort(cursor, sorted.end());
437 cursor = sorted.insert(sorted.end(), namelist.begin(), namelist.end());
438 std::sort(cursor, sorted.end());
439 cursor = sorted.insert(sorted.end(), common.begin(), common.end());
440 std::sort(cursor, sorted.end());
444 void PutEntity(std::ostream &os, const Symbol &symbol) {
447 [&](const ObjectEntityDetails &) { PutObjectEntity(os, symbol); },
448 [&](const ProcEntityDetails &) { PutProcEntity(os, symbol); },
449 [&](const TypeParamDetails &) { PutTypeParam(os, symbol); },
451 common::die("PutEntity: unexpected details: %s",
452 DetailsToString(symbol.details()).c_str());
458 void PutShapeSpec(std::ostream &os, const ShapeSpec &x) {
459 if (x.lbound().isAssumed()) {
460 CHECK(x.ubound().isAssumed());
463 if (!x.lbound().isDeferred()) {
464 PutBound(os, x.lbound());
467 if (!x.ubound().isDeferred()) {
468 PutBound(os, x.ubound());
472 void PutShape(std::ostream &os, const ArraySpec &shape, char open, char close) {
473 if (!shape.empty()) {
476 for (const auto &shapeSpec : shape) {
482 PutShapeSpec(os, shapeSpec);
488 void PutObjectEntity(std::ostream &os, const Symbol &symbol) {
489 auto &details{symbol.get<ObjectEntityDetails>()};
490 PutEntity(os, symbol, [&]() { PutType(os, DEREF(symbol.GetType())); },
492 PutShape(os, details.shape(), '(', ')');
493 PutShape(os, details.coshape(), '[', ']');
494 PutInit(os, symbol, details.init());
498 void PutProcEntity(std::ostream &os, const Symbol &symbol) {
499 if (symbol.attrs().test(Attr::INTRINSIC)) {
500 os << "intrinsic::" << symbol.name() << '\n';
503 const auto &details{symbol.get<ProcEntityDetails>()};
504 const ProcInterface &interface{details.interface()};
505 Attrs attrs{symbol.attrs()};
506 if (details.passName()) {
507 attrs.reset(Attr::PASS);
509 PutEntity(os, symbol,
512 if (interface.symbol()) {
513 os << interface.symbol()->name();
514 } else if (interface.type()) {
515 PutType(os, *interface.type());
518 PutPassName(os, details.passName());
524 void PutPassName(std::ostream &os, const std::optional<SourceName> &passName) {
526 os << ",pass(" << *passName << ')';
530 void PutTypeParam(std::ostream &os, const Symbol &symbol) {
531 auto &details{symbol.get<TypeParamDetails>()};
532 PutEntity(os, symbol,
534 PutType(os, DEREF(symbol.GetType()));
535 PutLower(os << ',', common::EnumToString(details.attr()));
538 PutInit(os, details.init());
542 void PutInit(std::ostream &os, const Symbol &symbol, const MaybeExpr &init) {
544 if (symbol.attrs().test(Attr::PARAMETER) ||
545 symbol.owner().IsDerivedType()) {
546 os << (symbol.attrs().test(Attr::POINTER) ? "=>" : "=");
552 void PutInit(std::ostream &os, const MaybeIntExpr &init) {
554 init->AsFortran(os << '=');
558 void PutBound(std::ostream &os, const Bound &x) {
561 } else if (x.isDeferred()) {
564 x.GetExplicit()->AsFortran(os);
568 // Write an entity (object or procedure) declaration.
569 // writeType is called to write out the type.
570 void PutEntity(std::ostream &os, const Symbol &symbol,
571 std::function<void()> writeType, Attrs attrs) {
576 [&](const SubprogramDetails &x) { bindName = x.bindName(); },
577 [&](const ObjectEntityDetails &x) { bindName = x.bindName(); },
578 [&](const ProcEntityDetails &x) { bindName = x.bindName(); },
579 [&](const auto &) {},
582 PutAttrs(os, attrs, bindName);
583 os << "::" << symbol.name();
586 // Put out each attribute to os, surrounded by `before` and `after` and
587 // mapped to lower case.
588 std::ostream &PutAttrs(std::ostream &os, Attrs attrs, const MaybeExpr &bindName,
589 std::string before, std::string after) {
590 attrs.set(Attr::PUBLIC, false); // no need to write PUBLIC
591 attrs.set(Attr::EXTERNAL, false); // no need to write EXTERNAL
593 bindName->AsFortran(os << before << "bind(c, name=") << ')' << after;
594 attrs.set(Attr::BIND_C, false);
596 for (std::size_t i{0}; i < Attr_enumSize; ++i) {
597 Attr attr{static_cast<Attr>(i)};
598 if (attrs.test(attr)) {
599 PutAttr(os << before, attr) << after;
605 std::ostream &PutAttr(std::ostream &os, Attr attr) {
606 return PutLower(os, AttrToString(attr));
609 std::ostream &PutType(std::ostream &os, const DeclTypeSpec &type) {
610 return PutLower(os, type.AsFortran());
613 std::ostream &PutLower(std::ostream &os, const std::string &str) {
615 os << parser::ToLowerCaseLetter(c);
624 unlink(path.c_str());
630 // Create a temp file in the same directory and with the same suffix as path.
631 // Return an open file descriptor and its path.
632 static Temp MkTemp(const std::string &path) {
633 auto length{path.length()};
634 auto dot{path.find_last_of("./")};
635 std::string suffix{dot < length && path[dot] == '.' ? path.substr(dot) : ""};
636 CHECK(length > suffix.length() &&
637 path.substr(length - suffix.length()) == suffix);
638 auto tempPath{path.substr(0, length - suffix.length()) + "XXXXXX" + suffix};
639 int fd{mkstemps(&tempPath[0], suffix.length())};
640 auto mask{umask(0777)};
642 chmod(tempPath.c_str(), 0666 & ~mask); // temp is created with mode 0600
643 return Temp{fd, tempPath};
646 // Write the module file at path, prepending header. If an error occurs,
647 // return errno, otherwise 0.
648 static int WriteFile(const std::string &path, const std::string &contents) {
649 auto header{std::string{ModHeader::bom} + ModHeader::magic +
650 CheckSum(contents) + ModHeader::terminator};
651 if (FileContentsMatch(path, header, contents)) {
654 Temp temp{MkTemp(path)};
658 if (write(temp.fd, header.c_str(), header.size()) !=
659 static_cast<ssize_t>(header.size()) ||
660 write(temp.fd, contents.c_str(), contents.size()) !=
661 static_cast<ssize_t>(contents.size())) {
664 if (std::rename(temp.path.c_str(), path.c_str()) == -1) {
670 // Return true if the stream matches what we would write for the mod file.
671 static bool FileContentsMatch(const std::string &path,
672 const std::string &header, const std::string &contents) {
673 std::size_t hsize{header.size()};
674 std::size_t csize{contents.size()};
675 if (GetFileSize(path) != hsize + csize) {
678 int fd{open(path.c_str(), O_RDONLY)};
682 constexpr std::size_t bufSize{4096};
683 std::string buffer(bufSize, '\0');
684 if (read(fd, &buffer[0], hsize) != static_cast<ssize_t>(hsize) ||
685 std::memcmp(&buffer[0], &header[0], hsize) != 0) {
687 return false; // header doesn't match
689 for (auto remaining{csize};;) {
690 auto bytes{std::min(bufSize, remaining)};
691 auto got{read(fd, &buffer[0], bytes)};
692 if (got != static_cast<ssize_t>(bytes) ||
693 std::memcmp(&buffer[0], &contents[csize - remaining], bytes) != 0) {
697 if (bytes == 0 && remaining == 0) {
705 // Compute a simple hash of the contents of a module file and
706 // return it as a string of hex digits.
707 // This uses the Fowler-Noll-Vo hash function.
708 static std::string CheckSum(const std::string_view &contents) {
709 std::uint64_t hash{0xcbf29ce484222325ull};
710 for (char c : contents) {
712 hash *= 0x100000001b3;
714 static const char *digits = "0123456789abcdef";
715 std::string result(ModHeader::sumLen, '0');
716 for (size_t i{ModHeader::sumLen}; hash != 0; hash >>= 4) {
717 result[--i] = digits[hash & 0xf];
722 static bool VerifyHeader(const char *content, std::size_t len) {
723 std::string_view sv{content, len};
724 if (sv.substr(0, ModHeader::magicLen) != ModHeader::magic) {
727 std::string_view expectSum{sv.substr(ModHeader::magicLen, ModHeader::sumLen)};
728 std::string actualSum{CheckSum(sv.substr(ModHeader::len))};
729 return expectSum == actualSum;
732 static std::size_t GetFileSize(const std::string &path) {
734 if (stat(path.c_str(), &statbuf) == 0) {
735 return static_cast<std::size_t>(statbuf.st_size);
741 Scope *ModFileReader::Read(const SourceName &name, Scope *ancestor) {
742 std::string ancestorName; // empty for module
744 if (auto *scope{ancestor->FindSubmodule(name)}) {
747 ancestorName = ancestor->GetName().value().ToString();
749 auto it{context_.globalScope().find(name)};
750 if (it != context_.globalScope().end()) {
751 return it->second->scope();
754 parser::Parsing parsing{context_.allSources()};
755 parser::Options options;
756 options.isModuleFile = true;
757 options.features.Enable(common::LanguageFeature::BackslashEscapes);
758 options.searchDirectories = context_.searchDirectories();
759 auto path{ModFileName(name, ancestorName, context_.moduleFileSuffix())};
760 const auto *sourceFile{parsing.Prescan(path, options)};
761 if (parsing.messages().AnyFatalError()) {
762 for (auto &msg : parsing.messages().messages()) {
763 std::string str{msg.ToString()};
764 Say(name, ancestorName, parser::MessageFixedText{str.c_str(), str.size()},
770 if (!VerifyHeader(sourceFile->content(), sourceFile->bytes())) {
771 Say(name, ancestorName, "File has invalid checksum: %s"_en_US,
776 parsing.Parse(nullptr);
777 auto &parseTree{parsing.parseTree()};
778 if (!parsing.messages().empty() || !parsing.consumedWholeFile() ||
780 Say(name, ancestorName, "Module file is corrupt: %s"_err_en_US,
784 Scope *parentScope; // the scope this module/submodule goes into
786 parentScope = &context_.globalScope();
787 } else if (std::optional<SourceName> parent{GetSubmoduleParent(*parseTree)}) {
788 parentScope = Read(*parent, ancestor);
790 parentScope = ancestor;
792 ResolveNames(context_, *parseTree);
793 const auto &it{parentScope->find(name)};
794 if (it == parentScope->end()) {
797 auto &modSymbol{*it->second};
798 modSymbol.set(Symbol::Flag::ModFile);
799 modSymbol.scope()->set_chars(parsing.cooked());
800 return modSymbol.scope();
803 parser::Message &ModFileReader::Say(const SourceName &name,
804 const std::string &ancestor, parser::MessageFixedText &&msg,
805 const std::string &arg) {
809 ? "Error reading module file for module '%s'"_err_en_US
810 : "Error reading module file for submodule '%s' of module '%s'"_err_en_US,
812 .Attach(name, std::move(msg), arg);
815 // program was read from a .mod file for a submodule; return the name of the
816 // submodule's parent submodule, nullptr if none.
817 static std::optional<SourceName> GetSubmoduleParent(
818 const parser::Program &program) {
819 CHECK(program.v.size() == 1);
820 auto &unit{program.v.front()};
821 auto &submod{std::get<common::Indirection<parser::Submodule>>(unit.u)};
823 std::get<parser::Statement<parser::SubmoduleStmt>>(submod.value().t)};
824 auto &parentId{std::get<parser::ParentIdentifier>(stmt.statement.t)};
825 if (auto &parent{std::get<std::optional<parser::Name>>(parentId.t)}) {
826 return parent->source;
832 void SubprogramSymbolCollector::Collect() {
833 const auto &details{symbol_.get<SubprogramDetails>()};
834 isInterface_ = details.isInterface();
835 for (const Symbol *dummyArg : details.dummyArgs()) {
836 DoSymbol(DEREF(dummyArg));
838 if (details.isFunction()) {
839 DoSymbol(details.result());
841 for (const auto &pair : scope_) {
842 const Symbol &symbol{*pair.second};
843 if (const auto *useDetails{symbol.detailsIf<UseDetails>()}) {
844 if (useSet_.count(useDetails->symbol()) > 0) {
845 need_.push_back(symbol);
851 void SubprogramSymbolCollector::DoSymbol(const Symbol &symbol) {
852 DoSymbol(symbol.name(), symbol);
855 // Do symbols this one depends on; then add to need_
856 void SubprogramSymbolCollector::DoSymbol(
857 const SourceName &name, const Symbol &symbol) {
858 const auto &scope{symbol.owner()};
859 if (scope != scope_ && !scope.IsDerivedType()) {
860 if (scope != scope_.parent()) {
861 useSet_.insert(symbol);
863 if (NeedImport(name, symbol)) {
864 imports_.insert(name);
868 if (!needSet_.insert(symbol).second) {
869 return; // already done
873 [this](const ObjectEntityDetails &details) {
874 for (const ShapeSpec &spec : details.shape()) {
875 DoBound(spec.lbound());
876 DoBound(spec.ubound());
878 for (const ShapeSpec &spec : details.coshape()) {
879 DoBound(spec.lbound());
880 DoBound(spec.ubound());
882 if (const Symbol * commonBlock{details.commonBlock()}) {
883 DoSymbol(*commonBlock);
886 [this](const CommonBlockDetails &details) {
887 for (const Symbol &object : details.objects()) {
894 if (!symbol.has<UseDetails>()) {
895 DoType(symbol.GetType());
897 if (!scope.IsDerivedType()) {
898 need_.push_back(symbol);
902 void SubprogramSymbolCollector::DoType(const DeclTypeSpec *type) {
906 switch (type->category()) {
907 case DeclTypeSpec::Numeric:
908 case DeclTypeSpec::Logical: break; // nothing to do
909 case DeclTypeSpec::Character:
910 DoParamValue(type->characterTypeSpec().length());
913 if (const DerivedTypeSpec * derived{type->AsDerived()}) {
914 const auto &typeSymbol{derived->typeSymbol()};
915 if (const DerivedTypeSpec * extends{typeSymbol.GetParentTypeSpec()}) {
916 DoSymbol(extends->name(), extends->typeSymbol());
918 for (const auto &pair : derived->parameters()) {
919 DoParamValue(pair.second);
921 for (const auto &pair : *typeSymbol.scope()) {
922 const Symbol &comp{*pair.second};
925 DoSymbol(derived->name(), derived->typeSymbol());
930 void SubprogramSymbolCollector::DoBound(const Bound &bound) {
931 if (const MaybeSubscriptIntExpr & expr{bound.GetExplicit()}) {
935 void SubprogramSymbolCollector::DoParamValue(const ParamValue ¶mValue) {
936 if (const auto &expr{paramValue.GetExplicit()}) {
941 // Do we need a IMPORT of this symbol into an interface block?
942 bool SubprogramSymbolCollector::NeedImport(
943 const SourceName &name, const Symbol &symbol) {
946 } else if (symbol.owner() != scope_.parent()) {
947 // detect import from parent of use-associated symbol
948 // can be null in the case of a use-associated derived type's parent type
949 const auto *found{scope_.FindSymbol(name)};
950 CHECK(found || symbol.has<DerivedTypeDetails>());
951 return found && found->has<UseDetails>() && found->owner() != scope_;