From 5e65aaa921f18cb3d9ff3ac11dc0a568f5e3d10e Mon Sep 17 00:00:00 2001 From: Tim Keith Date: Thu, 22 Aug 2019 13:57:18 -0700 Subject: [PATCH] [flang] Fix .mod file bug with IMPORT of USEd name If a symbol (derived type, for example) was use-associated into a scope and then imported into a nested interface block, we were not including the correct IMPORT statement in the .mod file. This fixes refines the test for when the IMPORT is needed. Fixes flang-compiler/f18#657. Original-commit: flang-compiler/f18@8383de47ecb1b6901e2dd6f290df28fe8a470908 Reviewed-on: https://github.com/flang-compiler/f18/pull/675 Tree-same-pre-rewrite: false --- flang/lib/semantics/mod-file.cc | 17 ++++++++++++++++- flang/test/semantics/modfile23.f90 | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/flang/lib/semantics/mod-file.cc b/flang/lib/semantics/mod-file.cc index 7845b24..711caae 100644 --- a/flang/lib/semantics/mod-file.cc +++ b/flang/lib/semantics/mod-file.cc @@ -86,6 +86,7 @@ private: void DoType(const DeclTypeSpec *); void DoBound(const Bound &); void DoParamValue(const ParamValue &); + bool NeedImport(const Symbol &); struct SymbolVisitor : public virtual evaluate::VisitorBase { using Result = SymbolVector; @@ -824,7 +825,8 @@ void SubprogramSymbolCollector::DoSymbol(const Symbol &symbol) { if (scope != scope_ && !scope.IsDerivedType()) { if (scope != scope_.parent()) { useSet_.insert(&symbol); - } else if (isInterface_) { + } + if (NeedImport(symbol)) { imports_.insert(&symbol); } return; @@ -902,4 +904,17 @@ void SubprogramSymbolCollector::DoParamValue(const ParamValue ¶mValue) { } } +// Do we need a IMPORT of this symbol into an interface block? +bool SubprogramSymbolCollector::NeedImport(const Symbol &symbol) { + if (!isInterface_) { + return false; + } else if (symbol.owner() != scope_.parent()) { + // detect import from parent of use-associated symbol + const auto *found{scope_.FindSymbol(symbol.name())}; + return DEREF(found).has() && found->owner() != scope_; + } else { + return true; + } +} + } diff --git a/flang/test/semantics/modfile23.f90 b/flang/test/semantics/modfile23.f90 index ca24a6a..8e651d4 100644 --- a/flang/test/semantics/modfile23.f90 +++ b/flang/test/semantics/modfile23.f90 @@ -184,3 +184,34 @@ end ! character(i,1)::x ! end !end + +module m8 + use m1, only: t1, t2 + interface + subroutine s1(x) + import + type(t1) :: x + end subroutine + subroutine s2(x) + import :: t2 + type(t2) :: x + end subroutine + end interface +end +!Expect: m8.mod +!module m8 +! use m1,only:t1 +! use m1,only:t2 +! interface +! subroutine s1(x) +! import::t1 +! type(t1)::x +! end +! end interface +! interface +! subroutine s2(x) +! import::t2 +! type(t2)::x +! end +! end interface +!end -- 2.7.4