diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp index 36b23410ab20..9a5a34fe4b93 100644 --- a/flang/lib/Semantics/mod-file.cpp +++ b/flang/lib/Semantics/mod-file.cpp @@ -53,9 +53,6 @@ static void PutBound(llvm::raw_ostream &, const Bound &); static void PutShapeSpec(llvm::raw_ostream &, const ShapeSpec &); static void PutShape( llvm::raw_ostream &, const ArraySpec &, char open, char close); -llvm::raw_ostream &PutAttrs(llvm::raw_ostream &, Attrs, - const std::string * = nullptr, std::string before = ","s, - std::string after = ""s); static llvm::raw_ostream &PutAttr(llvm::raw_ostream &, Attr); static llvm::raw_ostream &PutType(llvm::raw_ostream &, const DeclTypeSpec &); @@ -132,6 +129,7 @@ static std::string ModFileName(const SourceName &name, // Write the module file for symbol, which must be a module or submodule. void ModFileWriter::Write(const Symbol &symbol) { auto *ancestor{symbol.get().ancestor()}; + isSubmodule_ = ancestor != nullptr; auto ancestorName{ancestor ? ancestor->GetName().value().ToString() : ""s}; auto path{context_.moduleDirectory() + '/' + ModFileName(symbol.name(), ancestorName, context_.moduleFileSuffix())}; @@ -310,6 +308,9 @@ void ModFileWriter::PutSymbol( sep = ','; } decls_ << '\n'; + if (!isSubmodule_ && symbol.attrs().test(Attr::PRIVATE)) { + decls_ << "private::" << symbol.name() << '\n'; + } }, [&](const CommonBlockDetails &x) { decls_ << "common/" << symbol.name(); @@ -519,7 +520,7 @@ void ModFileWriter::PutGeneric(const Symbol &symbol) { } } decls_ << "end interface\n"; - if (symbol.attrs().test(Attr::PRIVATE)) { + if (!isSubmodule_ && symbol.attrs().test(Attr::PRIVATE)) { PutGenericName(decls_ << "private::", symbol) << '\n'; } } @@ -543,7 +544,7 @@ void ModFileWriter::PutUse(const Symbol &symbol) { uses_ << '\n'; PutUseExtraAttr(Attr::VOLATILE, symbol, use); PutUseExtraAttr(Attr::ASYNCHRONOUS, symbol, use); - if (symbol.attrs().test(Attr::PRIVATE)) { + if (!isSubmodule_ && symbol.attrs().test(Attr::PRIVATE)) { PutGenericName(useExtraAttrs_ << "private::", symbol) << '\n'; } } @@ -686,7 +687,7 @@ void ModFileWriter::PutObjectEntity( void ModFileWriter::PutProcEntity(llvm::raw_ostream &os, const Symbol &symbol) { if (symbol.attrs().test(Attr::INTRINSIC)) { os << "intrinsic::" << symbol.name() << '\n'; - if (symbol.attrs().test(Attr::PRIVATE)) { + if (!isSubmodule_ && symbol.attrs().test(Attr::PRIVATE)) { os << "private::" << symbol.name() << '\n'; } return; @@ -777,10 +778,13 @@ void ModFileWriter::PutEntity(llvm::raw_ostream &os, const Symbol &symbol, // Put out each attribute to os, surrounded by `before` and `after` and // mapped to lower case. -llvm::raw_ostream &PutAttrs(llvm::raw_ostream &os, Attrs attrs, - const std::string *bindName, std::string before, std::string after) { +llvm::raw_ostream &ModFileWriter::PutAttrs(llvm::raw_ostream &os, Attrs attrs, + const std::string *bindName, std::string before, std::string after) const { attrs.set(Attr::PUBLIC, false); // no need to write PUBLIC attrs.set(Attr::EXTERNAL, false); // no need to write EXTERNAL + if (isSubmodule_) { + attrs.set(Attr::PRIVATE, false); + } if (bindName) { os << before << "bind(c, name=\"" << *bindName << "\")" << after; attrs.set(Attr::BIND_C, false); diff --git a/flang/lib/Semantics/mod-file.h b/flang/lib/Semantics/mod-file.h index e732fdb9bcb6..04f6e06bb0b4 100644 --- a/flang/lib/Semantics/mod-file.h +++ b/flang/lib/Semantics/mod-file.h @@ -51,6 +51,7 @@ private: useExtraAttrsBuf_}; // attrs added to used entity llvm::raw_string_ostream decls_{declsBuf_}; llvm::raw_string_ostream contains_{containsBuf_}; + bool isSubmodule_{false}; void WriteAll(const Scope &); void WriteOne(const Scope &); @@ -72,6 +73,9 @@ private: void PutGeneric(const Symbol &); void PutUse(const Symbol &); void PutUseExtraAttr(Attr, const Symbol &, const Symbol &); + llvm::raw_ostream &PutAttrs(llvm::raw_ostream &, Attrs, + const std::string * = nullptr, std::string before = ","s, + std::string after = ""s) const; }; class ModFileReader {