1515#include " flang/Parser/unparse.h"
1616#include " flang/Semantics/scope.h"
1717#include " flang/Semantics/semantics.h"
18+ #include " flang/Semantics/symbol-dependence.h"
1819#include " flang/Semantics/symbol.h"
1920#include " flang/Semantics/tools.h"
2021#include " llvm/Support/FileSystem.h"
@@ -223,72 +224,13 @@ std::string ModFileWriter::GetAsString(const Symbol &symbol) {
223224// Collect symbols from constant and specification expressions that are being
224225// referenced directly from other modules; they may require new USE
225226// associations.
226- static void HarvestSymbolsNeededFromOtherModules (
227- SourceOrderedSymbolSet &, const Scope &);
228- static void HarvestSymbolsNeededFromOtherModules (
229- SourceOrderedSymbolSet &set, const Symbol &symbol, const Scope &scope) {
230- auto HarvestBound{[&](const Bound &bound) {
231- if (const auto &expr{bound.GetExplicit ()}) {
232- for (SymbolRef ref : evaluate::CollectSymbols (*expr)) {
233- set.emplace (*ref);
234- }
235- }
236- }};
237- auto HarvestShapeSpec{[&](const ShapeSpec &shapeSpec) {
238- HarvestBound (shapeSpec.lbound ());
239- HarvestBound (shapeSpec.ubound ());
240- }};
241- auto HarvestArraySpec{[&](const ArraySpec &arraySpec) {
242- for (const auto &shapeSpec : arraySpec) {
243- HarvestShapeSpec (shapeSpec);
244- }
245- }};
246-
247- if (symbol.has <DerivedTypeDetails>()) {
248- if (symbol.scope ()) {
249- HarvestSymbolsNeededFromOtherModules (set, *symbol.scope ());
250- }
251- } else if (const auto &generic{symbol.detailsIf <GenericDetails>()};
252- generic && generic->derivedType ()) {
253- const Symbol &dtSym{*generic->derivedType ()};
254- if (dtSym.has <DerivedTypeDetails>()) {
255- if (dtSym.scope ()) {
256- HarvestSymbolsNeededFromOtherModules (set, *dtSym.scope ());
257- }
258- } else {
259- CHECK (dtSym.has <UseDetails>() || dtSym.has <UseErrorDetails>());
260- }
261- } else if (const auto *object{symbol.detailsIf <ObjectEntityDetails>()}) {
262- HarvestArraySpec (object->shape ());
263- HarvestArraySpec (object->coshape ());
264- if (IsNamedConstant (symbol) || scope.IsDerivedType ()) {
265- if (object->init ()) {
266- for (SymbolRef ref : evaluate::CollectSymbols (*object->init ())) {
267- set.emplace (*ref);
268- }
269- }
270- }
271- } else if (const auto *proc{symbol.detailsIf <ProcEntityDetails>()}) {
272- if (proc->init () && *proc->init () && scope.IsDerivedType ()) {
273- set.emplace (**proc->init ());
274- }
275- } else if (const auto *subp{symbol.detailsIf <SubprogramDetails>()}) {
276- for (const Symbol *dummy : subp->dummyArgs ()) {
277- if (dummy) {
278- HarvestSymbolsNeededFromOtherModules (set, *dummy, scope);
279- }
280- }
281- if (subp->isFunction ()) {
282- HarvestSymbolsNeededFromOtherModules (set, subp->result (), scope);
283- }
284- }
285- }
286-
287- static void HarvestSymbolsNeededFromOtherModules (
288- SourceOrderedSymbolSet &set, const Scope &scope) {
289- for (const auto &[_, symbol] : scope) {
290- HarvestSymbolsNeededFromOtherModules (set, *symbol, scope);
227+ static SourceOrderedSymbolSet HarvestSymbolsNeededFromOtherModules (
228+ const Scope &scope) {
229+ SourceOrderedSymbolSet set;
230+ for (const Symbol &symbol : CollectAllDependences (scope)) {
231+ set.insert (symbol);
291232 }
233+ return set;
292234}
293235
294236void ModFileWriter::PrepareRenamings (const Scope &scope) {
@@ -304,8 +246,8 @@ void ModFileWriter::PrepareRenamings(const Scope &scope) {
304246 }
305247 }
306248 // Collect symbols needed from other modules
307- SourceOrderedSymbolSet symbolsNeeded;
308- HarvestSymbolsNeededFromOtherModules (symbolsNeeded, scope);
249+ SourceOrderedSymbolSet symbolsNeeded{
250+ HarvestSymbolsNeededFromOtherModules (scope)} ;
309251 // Establish any necessary renamings of symbols in other modules
310252 // to their names in this scope, creating those new names when needed.
311253 auto &renamings{context_.moduleFileOutputRenamings ()};
0 commit comments