blob: 1e2a5c6728b76938ddc9b889782a6a187836756e [file] [log] [blame]
CarolineConcatto64ab3302020-02-25 15:11:521//===-- 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 Keith3780d3e2020-07-13 19:19:1711#include "flang/Common/restorer.h"
CarolineConcatto64ab3302020-02-25 15:11:5212#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 Truby0855c452020-02-25 15:59:5019#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/raw_ostream.h"
CarolineConcatto64ab3302020-02-25 15:11:5222#include <algorithm>
CarolineConcatto64ab3302020-02-25 15:11:5223#include <fstream>
CarolineConcatto64ab3302020-02-25 15:11:5224#include <set>
25#include <string_view>
CarolineConcatto64ab3302020-02-25 15:11:5226#include <vector>
27
28namespace Fortran::semantics {
29
30using 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.
36struct 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
45static std::optional<SourceName> GetSubmoduleParent(const parser::Program &);
Tim Keith86f59de2020-12-02 23:13:4946static void CollectSymbols(const Scope &, SymbolVector &, SymbolVector &);
Caroline Concatto8670e492020-02-28 15:11:0347static void PutEntity(llvm::raw_ostream &, const Symbol &);
48static void PutObjectEntity(llvm::raw_ostream &, const Symbol &);
49static void PutProcEntity(llvm::raw_ostream &, const Symbol &);
50static void PutPassName(llvm::raw_ostream &, const std::optional<SourceName> &);
51static void PutTypeParam(llvm::raw_ostream &, const Symbol &);
CarolineConcatto64ab3302020-02-25 15:11:5252static void PutEntity(
Caroline Concatto8670e492020-02-28 15:11:0353 llvm::raw_ostream &, const Symbol &, std::function<void()>, Attrs);
54static void PutInit(llvm::raw_ostream &, const Symbol &, const MaybeExpr &);
55static void PutInit(llvm::raw_ostream &, const MaybeIntExpr &);
56static void PutBound(llvm::raw_ostream &, const Bound &);
57static llvm::raw_ostream &PutAttrs(llvm::raw_ostream &, Attrs,
CarolineConcatto64ab3302020-02-25 15:11:5258 const MaybeExpr & = std::nullopt, std::string before = ","s,
59 std::string after = ""s);
Caroline Concatto8670e492020-02-28 15:11:0360
61static llvm::raw_ostream &PutAttr(llvm::raw_ostream &, Attr);
62static llvm::raw_ostream &PutType(llvm::raw_ostream &, const DeclTypeSpec &);
63static llvm::raw_ostream &PutLower(llvm::raw_ostream &, const std::string &);
David Truby0855c452020-02-25 15:59:5064static std::error_code WriteFile(
65 const std::string &, const std::string &, bool = true);
CarolineConcatto64ab3302020-02-25 15:11:5266static bool FileContentsMatch(
67 const std::string &, const std::string &, const std::string &);
CarolineConcatto64ab3302020-02-25 15:11:5268static std::string CheckSum(const std::string_view &);
69
70// Collect symbols needed for a subprogram interface
71class SubprogramSymbolCollector {
72public:
peter klauslerc42f6312020-03-19 23:31:1073 SubprogramSymbolCollector(const Symbol &symbol, const Scope &scope)
Tim Keith1f879002020-03-29 04:00:1674 : symbol_{symbol}, scope_{scope} {}
CarolineConcatto64ab3302020-02-25 15:11:5275 const SymbolVector &symbols() const { return need_; }
76 const std::set<SourceName> &imports() const { return imports_; }
77 void Collect();
78
79private:
80 const Symbol &symbol_;
81 const Scope &scope_;
82 bool isInterface_{false};
Tim Keith1f879002020-03-29 04:00:1683 SymbolVector need_; // symbols that are needed
peter klausler0d8331c2021-03-18 17:26:2384 UnorderedSymbolSet needSet_; // symbols already in need_
85 UnorderedSymbolSet useSet_; // use-associations that might be needed
Tim Keith1f879002020-03-29 04:00:1686 std::set<SourceName> imports_; // imports from host that are needed
CarolineConcatto64ab3302020-02-25 15:11:5287
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 Keith1f879002020-03-29 04:00:1695 template <typename T> void DoExpr(evaluate::Expr<T> expr) {
CarolineConcatto64ab3302020-02-25 15:11:5296 for (const Symbol &symbol : evaluate::CollectSymbols(expr)) {
97 DoSymbol(symbol);
98 }
99 }
100};
101
102bool ModFileWriter::WriteAll() {
Tim Keith3780d3e2020-07-13 19:19:17103 // this flag affects character literals: force it to be consistent
104 auto restorer{
105 common::ScopedSet(parser::useHexadecimalEscapeSequences, false)};
CarolineConcatto64ab3302020-02-25 15:11:52106 WriteAll(context_.globalScope());
107 return !context_.AnyFatalError();
108}
109
110void ModFileWriter::WriteAll(const Scope &scope) {
111 for (const auto &child : scope.children()) {
112 WriteOne(child);
113 }
114}
115
116void 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 Keith1f879002020-03-29 04:00:16122 WriteAll(scope); // write out submodules
CarolineConcatto64ab3302020-02-25 15:11:52123 }
124}
125
126// Construct the name of a module file. Non-empty ancestorName means submodule.
127static 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.
134void 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 Truby0855c452020-02-25 15:59:50140 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());
CarolineConcatto64ab3302020-02-25 15:11:52144 }
145}
146
147// Return the entire body of the module file
148// and clear saved uses, decls, and contains.
149std::string ModFileWriter::GetAsString(const Symbol &symbol) {
Caroline Concatto8670e492020-02-28 15:11:03150 std::string buf;
151 llvm::raw_string_ostream all{buf};
CarolineConcatto64ab3302020-02-25 15:11:52152 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 Concatto8670e492020-02-28 15:11:03165 uses_.str().clear();
CarolineConcatto64ab3302020-02-25 15:11:52166 all << useExtraAttrs_.str();
Caroline Concatto8670e492020-02-28 15:11:03167 useExtraAttrs_.str().clear();
CarolineConcatto64ab3302020-02-25 15:11:52168 all << decls_.str();
Caroline Concatto8670e492020-02-28 15:11:03169 decls_.str().clear();
CarolineConcatto64ab3302020-02-25 15:11:52170 auto str{contains_.str()};
Caroline Concatto8670e492020-02-28 15:11:03171 contains_.str().clear();
CarolineConcatto64ab3302020-02-25 15:11:52172 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 klausler37b2e2b2020-09-30 20:34:23180bool ModFileWriter::PutSymbols(const Scope &scope) {
Tim Keith86f59de2020-12-02 23:13:49181 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) {
CarolineConcatto64ab3302020-02-25 15:11:52187 PutSymbol(typeBindings, symbol);
188 }
Tim Keith86f59de2020-12-02 23:13:49189 for (const Symbol &symbol : uses) {
190 PutUse(symbol);
191 }
CarolineConcatto64ab3302020-02-25 15:11:52192 if (auto str{typeBindings.str()}; !str.empty()) {
193 CHECK(scope.IsDerivedType());
194 decls_ << "contains\n" << str;
peter klausler37b2e2b2020-09-30 20:34:23195 return true;
196 } else {
197 return false;
CarolineConcatto64ab3302020-02-25 15:11:52198 }
199}
200
peter klausler4864d9f2021-01-13 22:12:23201static 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
CarolineConcatto64ab3302020-02-25 15:11:52210// 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.
212void ModFileWriter::PutSymbol(
Caroline Concatto8670e492020-02-28 15:11:03213 llvm::raw_ostream &typeBindings, const Symbol &symbol) {
Tim Keith1f879002020-03-29 04:00:16214 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 klausler4864d9f2021-01-13 22:12:23222 PutGenericName(typeBindings << "generic::", symbol)
223 << "=>" << proc.name() << '\n';
Tim Keith1f879002020-03-29 04:00:16224 }
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 Keithd5c05ce2020-05-06 19:20:07267 for (const auto &object : x.objects()) {
268 decls_ << sep << object->name();
Tim Keith1f879002020-03-29 04:00:16269 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 Keith1f879002020-03-29 04:00:16277 [](const HostAssocDetails &) {},
278 [](const MiscDetails &) {},
279 [&](const auto &) { PutEntity(decls_, symbol); },
280 },
CarolineConcatto64ab3302020-02-25 15:11:52281 symbol.details());
282}
283
284void 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 klausler37b2e2b2020-09-30 20:34:23304 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 }
CarolineConcatto64ab3302020-02-25 15:11:52315 decls_ << "end type\n";
316}
317
318// Attributes that may be in a subprogram prefix
319static const Attrs subprogramPrefixAttrs{Attr::ELEMENTAL, Attr::IMPURE,
320 Attr::MODULE, Attr::NON_RECURSIVE, Attr::PURE, Attr::RECURSIVE};
321
322void 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 Keithd55627d2020-12-28 16:50:30331 bool isAbstract{attrs.test(Attr::ABSTRACT)};
332 if (isAbstract) {
333 attrs.set(Attr::ABSTRACT, false);
334 }
CarolineConcatto64ab3302020-02-25 15:11:52335 Attrs prefixAttrs{subprogramPrefixAttrs & attrs};
336 // emit any non-prefix attributes in an attribute statement
337 attrs &= ~subprogramPrefixAttrs;
Caroline Concatto8670e492020-02-28 15:11:03338 std::string ssBuf;
339 llvm::raw_string_ostream ss{ssBuf};
CarolineConcatto64ab3302020-02-25 15:11:52340 PutAttrs(ss, attrs);
341 if (!ss.str().empty()) {
342 decls_ << ss.str().substr(1) << "::" << symbol.name() << '\n';
343 }
344 bool isInterface{details.isInterface()};
Caroline Concatto8670e492020-02-28 15:11:03345 llvm::raw_ostream &os{isInterface ? decls_ : contains_};
CarolineConcatto64ab3302020-02-25 15:11:52346 if (isInterface) {
Tim Keithd55627d2020-12-28 16:50:30347 os << (isAbstract ? "abstract " : "") << "interface\n";
CarolineConcatto64ab3302020-02-25 15:11:52348 }
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 Steinfeld3ed29092020-06-18 14:05:08357 if (dummy) {
358 os << dummy->name();
359 } else {
360 os << "*";
361 }
CarolineConcatto64ab3302020-02-25 15:11:52362 }
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 klauslerc42f6312020-03-19 23:31:10373 // 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 Concatto8670e492020-02-28 15:11:03378 std::string typeBindingsBuf;
379 llvm::raw_string_ostream typeBindings{typeBindingsBuf};
peter klauslerc42f6312020-03-19 23:31:10380 ModFileWriter writer{context_};
CarolineConcatto64ab3302020-02-25 15:11:52381 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
396static 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
CarolineConcatto64ab3302020-02-25 15:11:52404void ModFileWriter::PutGeneric(const Symbol &symbol) {
Tim Keith86f59de2020-12-02 23:13:49405 const auto &genericOwner{symbol.owner()};
CarolineConcatto64ab3302020-02-25 15:11:52406 auto &details{symbol.get<GenericDetails>()};
407 PutGenericName(decls_ << "interface ", symbol) << '\n';
408 for (const Symbol &specific : details.specificProcs()) {
Tim Keith86f59de2020-12-02 23:13:49409 if (specific.owner() == genericOwner) {
410 decls_ << "procedure::" << specific.name() << '\n';
411 }
CarolineConcatto64ab3302020-02-25 15:11:52412 }
413 decls_ << "end interface\n";
414 if (symbol.attrs().test(Attr::PRIVATE)) {
415 PutGenericName(decls_ << "private::", symbol) << '\n';
416 }
417}
418
419void ModFileWriter::PutUse(const Symbol &symbol) {
420 auto &details{symbol.get<UseDetails>()};
421 auto &use{details.symbol()};
Tim Keith14f49592020-05-29 23:39:13422 uses_ << "use " << GetUsedModule(details).name();
CarolineConcatto64ab3302020-02-25 15:11:52423 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 klausler4864d9f2021-01-13 22:12:23432 if (symbol.attrs().test(Attr::PRIVATE)) {
433 PutGenericName(useExtraAttrs_ << "private::", symbol) << '\n';
434 }
CarolineConcatto64ab3302020-02-25 15:11:52435}
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.
439void 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 klausler4864d9f2021-01-13 22:12:23447// 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.)
454static 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
CarolineConcatto64ab3302020-02-25 15:11:52472// 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 Keith86f59de2020-12-02 23:13:49474void CollectSymbols(
475 const Scope &scope, SymbolVector &sorted, SymbolVector &uses) {
CarolineConcatto64ab3302020-02-25 15:11:52476 SymbolVector namelist;
Tim Keithc353ebb2020-04-22 22:39:24477 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);
CarolineConcatto64ab3302020-02-25 15:11:52486 }
Tim Keith86f59de2020-12-02 23:13:49487 if (const auto *details{symbol->detailsIf<GenericDetails>()}) {
488 uses.insert(uses.end(), details->uses().begin(), details->uses().end());
489 }
CarolineConcatto64ab3302020-02-25 15:11:52490 }
491 }
Tim Keithd55627d2020-12-28 16:50:30492 // 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 klausler4864d9f2021-01-13 22:12:23495 return NameInModuleFile(x).begin() < NameInModuleFile(y).begin();
Tim Keithd55627d2020-12-28 16:50:30496 });
Tim Keithc353ebb2020-04-22 22:39:24497 sorted.insert(sorted.end(), namelist.begin(), namelist.end());
CarolineConcatto64ab3302020-02-25 15:11:52498 for (const auto &pair : scope.commonBlocks()) {
Tim Keithc353ebb2020-04-22 22:39:24499 sorted.push_back(*pair.second);
CarolineConcatto64ab3302020-02-25 15:11:52500 }
peter klausler0d8331c2021-03-18 17:26:23501 std::sort(
502 sorted.end() - commonSize, sorted.end(), SymbolSourcePositionCompare{});
CarolineConcatto64ab3302020-02-25 15:11:52503}
504
Caroline Concatto8670e492020-02-28 15:11:03505void PutEntity(llvm::raw_ostream &os, const Symbol &symbol) {
CarolineConcatto64ab3302020-02-25 15:11:52506 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 Concatto8670e492020-02-28 15:11:03519void PutShapeSpec(llvm::raw_ostream &os, const ShapeSpec &x) {
CarolineConcatto64ab3302020-02-25 15:11:52520 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 Concatto8670e492020-02-28 15:11:03533void PutShape(
534 llvm::raw_ostream &os, const ArraySpec &shape, char open, char close) {
CarolineConcatto64ab3302020-02-25 15:11:52535 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 Concatto8670e492020-02-28 15:11:03550void PutObjectEntity(llvm::raw_ostream &os, const Symbol &symbol) {
CarolineConcatto64ab3302020-02-25 15:11:52551 auto &details{symbol.get<ObjectEntityDetails>()};
Tim Keith1f879002020-03-29 04:00:16552 PutEntity(
553 os, symbol, [&]() { PutType(os, DEREF(symbol.GetType())); },
CarolineConcatto64ab3302020-02-25 15:11:52554 symbol.attrs());
555 PutShape(os, details.shape(), '(', ')');
556 PutShape(os, details.coshape(), '[', ']');
557 PutInit(os, symbol, details.init());
558 os << '\n';
559}
560
Caroline Concatto8670e492020-02-28 15:11:03561void PutProcEntity(llvm::raw_ostream &os, const Symbol &symbol) {
CarolineConcatto64ab3302020-02-25 15:11:52562 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 Keith1f879002020-03-29 04:00:16572 PutEntity(
573 os, symbol,
CarolineConcatto64ab3302020-02-25 15:11:52574 [&]() {
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 Concatto8670e492020-02-28 15:11:03588void PutPassName(
589 llvm::raw_ostream &os, const std::optional<SourceName> &passName) {
CarolineConcatto64ab3302020-02-25 15:11:52590 if (passName) {
591 os << ",pass(" << *passName << ')';
592 }
593}
Caroline Concatto8670e492020-02-28 15:11:03594void PutTypeParam(llvm::raw_ostream &os, const Symbol &symbol) {
CarolineConcatto64ab3302020-02-25 15:11:52595 auto &details{symbol.get<TypeParamDetails>()};
Tim Keith1f879002020-03-29 04:00:16596 PutEntity(
597 os, symbol,
CarolineConcatto64ab3302020-02-25 15:11:52598 [&]() {
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 Concatto8670e492020-02-28 15:11:03607void PutInit(
608 llvm::raw_ostream &os, const Symbol &symbol, const MaybeExpr &init) {
CarolineConcatto64ab3302020-02-25 15:11:52609 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 Concatto8670e492020-02-28 15:11:03618void PutInit(llvm::raw_ostream &os, const MaybeIntExpr &init) {
CarolineConcatto64ab3302020-02-25 15:11:52619 if (init) {
620 init->AsFortran(os << '=');
621 }
622}
623
Caroline Concatto8670e492020-02-28 15:11:03624void PutBound(llvm::raw_ostream &os, const Bound &x) {
CarolineConcatto64ab3302020-02-25 15:11:52625 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 Concatto8670e492020-02-28 15:11:03636void PutEntity(llvm::raw_ostream &os, const Symbol &symbol,
CarolineConcatto64ab3302020-02-25 15:11:52637 std::function<void()> writeType, Attrs attrs) {
638 writeType();
639 MaybeExpr bindName;
Tim Keith1f879002020-03-29 04:00:16640 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 },
CarolineConcatto64ab3302020-02-25 15:11:52646 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 Concatto8670e492020-02-28 15:11:03653llvm::raw_ostream &PutAttrs(llvm::raw_ostream &os, Attrs attrs,
654 const MaybeExpr &bindName, std::string before, std::string after) {
Tim Keith1f879002020-03-29 04:00:16655 attrs.set(Attr::PUBLIC, false); // no need to write PUBLIC
656 attrs.set(Attr::EXTERNAL, false); // no need to write EXTERNAL
CarolineConcatto64ab3302020-02-25 15:11:52657 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 Concatto8670e492020-02-28 15:11:03670llvm::raw_ostream &PutAttr(llvm::raw_ostream &os, Attr attr) {
CarolineConcatto64ab3302020-02-25 15:11:52671 return PutLower(os, AttrToString(attr));
672}
673
Caroline Concatto8670e492020-02-28 15:11:03674llvm::raw_ostream &PutType(llvm::raw_ostream &os, const DeclTypeSpec &type) {
CarolineConcatto64ab3302020-02-25 15:11:52675 return PutLower(os, type.AsFortran());
676}
677
Caroline Concatto8670e492020-02-28 15:11:03678llvm::raw_ostream &PutLower(llvm::raw_ostream &os, const std::string &str) {
CarolineConcatto64ab3302020-02-25 15:11:52679 for (char c : str) {
680 os << parser::ToLowerCaseLetter(c);
681 }
682 return os;
683}
684
685struct Temp {
Steve Scalpone61106302020-03-05 15:09:29686 Temp(int fd, std::string path) : fd{fd}, path{path} {}
David Truby0855c452020-02-25 15:59:50687 Temp(Temp &&t) : fd{std::exchange(t.fd, -1)}, path{std::move(t.path)} {}
CarolineConcatto64ab3302020-02-25 15:11:52688 ~Temp() {
David Truby0855c452020-02-25 15:59:50689 if (fd >= 0) {
Steve Scalpone61106302020-03-05 15:09:29690 llvm::sys::fs::file_t native{llvm::sys::fs::convertFDToNativeFile(fd)};
691 llvm::sys::fs::closeFile(native);
David Truby0855c452020-02-25 15:59:50692 llvm::sys::fs::remove(path.c_str());
693 }
CarolineConcatto64ab3302020-02-25 15:11:52694 }
Steve Scalpone61106302020-03-05 15:09:29695 int fd;
CarolineConcatto64ab3302020-02-25 15:11:52696 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 Truby0855c452020-02-25 15:59:50701static llvm::ErrorOr<Temp> MkTemp(const std::string &path) {
CarolineConcatto64ab3302020-02-25 15:11:52702 auto length{path.length()};
703 auto dot{path.find_last_of("./")};
David Truby0855c452020-02-25 15:59:50704 std::string suffix{
705 dot < length && path[dot] == '.' ? path.substr(dot + 1) : ""};
CarolineConcatto64ab3302020-02-25 15:11:52706 CHECK(length > suffix.length() &&
707 path.substr(length - suffix.length()) == suffix);
David Truby0855c452020-02-25 15:59:50708 auto prefix{path.substr(0, length - suffix.length())};
Steve Scalpone61106302020-03-05 15:09:29709 int fd;
David Truby0855c452020-02-25 15:59:50710 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()};
CarolineConcatto64ab3302020-02-25 15:11:52716}
717
718// Write the module file at path, prepending header. If an error occurs,
719// return errno, otherwise 0.
David Truby0855c452020-02-25 15:59:50720static std::error_code WriteFile(
721 const std::string &path, const std::string &contents, bool debug) {
CarolineConcatto64ab3302020-02-25 15:11:52722 auto header{std::string{ModHeader::bom} + ModHeader::magic +
723 CheckSum(contents) + ModHeader::terminator};
David Truby0855c452020-02-25 15:59:50724 if (debug) {
725 llvm::dbgs() << "Processing module " << path << ": ";
726 }
CarolineConcatto64ab3302020-02-25 15:11:52727 if (FileContentsMatch(path, header, contents)) {
David Truby0855c452020-02-25 15:59:50728 if (debug) {
729 llvm::dbgs() << "module unchanged, not writing\n";
730 }
731 return {};
CarolineConcatto64ab3302020-02-25 15:11:52732 }
David Truby0855c452020-02-25 15:59:50733 llvm::ErrorOr<Temp> temp{MkTemp(path)};
734 if (!temp) {
735 return temp.getError();
CarolineConcatto64ab3302020-02-25 15:11:52736 }
David Truby0855c452020-02-25 15:59:50737 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();
CarolineConcatto64ab3302020-02-25 15:11:52743 }
David Truby0855c452020-02-25 15:59:50744 if (debug) {
745 llvm::dbgs() << "module written\n";
CarolineConcatto64ab3302020-02-25 15:11:52746 }
David Truby0855c452020-02-25 15:59:50747 return llvm::sys::fs::rename(temp->path, path);
CarolineConcatto64ab3302020-02-25 15:11:52748}
749
750// Return true if the stream matches what we would write for the mod file.
751static 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 Truby0855c452020-02-25 15:59:50755 auto buf_or{llvm::MemoryBuffer::getFile(path)};
756 if (!buf_or) {
CarolineConcatto64ab3302020-02-25 15:11:52757 return false;
758 }
David Truby0855c452020-02-25 15:59:50759 auto buf = std::move(buf_or.get());
760 if (buf->getBufferSize() != hsize + csize) {
CarolineConcatto64ab3302020-02-25 15:11:52761 return false;
762 }
David Truby0855c452020-02-25 15:59:50763 if (!std::equal(header.begin(), header.end(), buf->getBufferStart(),
764 buf->getBufferStart() + hsize)) {
765 return false;
CarolineConcatto64ab3302020-02-25 15:11:52766 }
David Truby0855c452020-02-25 15:59:50767
768 return std::equal(contents.begin(), contents.end(),
769 buf->getBufferStart() + hsize, buf->getBufferEnd());
CarolineConcatto64ab3302020-02-25 15:11:52770}
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.
775static 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 Truby13ea73e2020-02-27 13:42:56789static bool VerifyHeader(llvm::ArrayRef<char> content) {
790 std::string_view sv{content.data(), content.size()};
CarolineConcatto64ab3302020-02-25 15:11:52791 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
CarolineConcatto64ab3302020-02-25 15:11:52799Scope *ModFileReader::Read(const SourceName &name, Scope *ancestor) {
Tim Keith1f879002020-03-29 04:00:16800 std::string ancestorName; // empty for module
CarolineConcatto64ab3302020-02-25 15:11:52801 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 klausler92a54192020-08-31 19:22:24812 parser::Parsing parsing{context_.allCookedSources()};
CarolineConcatto64ab3302020-02-25 15:11:52813 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 Truby13ea73e2020-02-27 13:42:56828 if (!VerifyHeader(sourceFile->content())) {
CarolineConcatto64ab3302020-02-25 15:11:52829 Say(name, ancestorName, "File has invalid checksum: %s"_en_US,
830 sourceFile->path());
831 return nullptr;
832 }
Caroline Concatto8670e492020-02-28 15:11:03833 llvm::raw_null_ostream NullStream;
834 parsing.Parse(NullStream);
CarolineConcatto64ab3302020-02-25 15:11:52835 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 Keith1f879002020-03-29 04:00:16842 Scope *parentScope; // the scope this module/submodule goes into
CarolineConcatto64ab3302020-02-25 15:11:52843 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 klausler4864d9f2021-01-13 22:12:23850 auto pair{parentScope->try_emplace(name, UnknownDetails{})};
851 if (!pair.second) {
CarolineConcatto64ab3302020-02-25 15:11:52852 return nullptr;
853 }
peter klausler4864d9f2021-01-13 22:12:23854 Symbol &modSymbol{*pair.first->second};
CarolineConcatto64ab3302020-02-25 15:11:52855 modSymbol.set(Symbol::Flag::ModFile);
peter klausler4864d9f2021-01-13 22:12:23856 ResolveNames(context_, *parseTree);
857 CHECK(modSymbol.has<ModuleDetails>());
858 CHECK(modSymbol.test(Symbol::Flag::ModFile));
CarolineConcatto64ab3302020-02-25 15:11:52859 return modSymbol.scope();
860}
861
862parser::Message &ModFileReader::Say(const SourceName &name,
863 const std::string &ancestor, parser::MessageFixedText &&msg,
864 const std::string &arg) {
peter klausler0bfa4ac2021-02-11 00:20:59865 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());
CarolineConcatto64ab3302020-02-25 15:11:52872}
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.
876static 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
891void SubprogramSymbolCollector::Collect() {
892 const auto &details{symbol_.get<SubprogramDetails>()};
893 isInterface_ = details.isInterface();
894 for (const Symbol *dummyArg : details.dummyArgs()) {
Pete Steinfeld3ed29092020-06-18 14:05:08895 if (dummyArg) {
896 DoSymbol(*dummyArg);
897 }
CarolineConcatto64ab3302020-02-25 15:11:52898 }
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 Keith6f300102020-05-11 20:28:05905 if (useSet_.count(useDetails->symbol().GetUltimate()) > 0) {
CarolineConcatto64ab3302020-02-25 15:11:52906 need_.push_back(symbol);
907 }
908 }
909 }
910}
911
912void SubprogramSymbolCollector::DoSymbol(const Symbol &symbol) {
913 DoSymbol(symbol.name(), symbol);
914}
915
916// Do symbols this one depends on; then add to need_
917void 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 Keith1f879002020-03-29 04:00:16930 return; // already done
CarolineConcatto64ab3302020-02-25 15:11:52931 }
Tim Keith1f879002020-03-29 04:00:16932 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 Keithd5c05ce2020-05-06 19:20:07947 for (const auto &object : details.objects()) {
948 DoSymbol(*object);
Tim Keith1f879002020-03-29 04:00:16949 }
950 },
951 [](const auto &) {},
952 },
CarolineConcatto64ab3302020-02-25 15:11:52953 symbol.details());
954 if (!symbol.has<UseDetails>()) {
955 DoType(symbol.GetType());
956 }
957 if (!scope.IsDerivedType()) {
958 need_.push_back(symbol);
959 }
960}
961
962void SubprogramSymbolCollector::DoType(const DeclTypeSpec *type) {
963 if (!type) {
964 return;
965 }
966 switch (type->category()) {
967 case DeclTypeSpec::Numeric:
Tim Keith1f879002020-03-29 04:00:16968 case DeclTypeSpec::Logical:
969 break; // nothing to do
CarolineConcatto64ab3302020-02-25 15:11:52970 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
991void SubprogramSymbolCollector::DoBound(const Bound &bound) {
992 if (const MaybeSubscriptIntExpr & expr{bound.GetExplicit()}) {
993 DoExpr(*expr);
994 }
995}
996void SubprogramSymbolCollector::DoParamValue(const ParamValue &paramValue) {
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?
1003bool SubprogramSymbolCollector::NeedImport(
1004 const SourceName &name, const Symbol &symbol) {
1005 if (!isInterface_) {
1006 return false;
peter klausler4864d9f2021-01-13 22:12:231007 } else if (symbol.owner().Contains(scope_)) {
CarolineConcatto64ab3302020-02-25 15:11:521008 return true;
peter klausler4864d9f2021-01-13 22:12:231009 } 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;
CarolineConcatto64ab3302020-02-25 15:11:521017 }
1018}
1019
Tim Keith1f879002020-03-29 04:00:161020} // namespace Fortran::semantics