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