|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 | 9 | #include "flang/Semantics/unparse-with-symbols.h"
|
| 10 | +#include "mod-file.h" |
10 | 11 | #include "flang/Parser/parse-tree-visitor.h"
|
11 | 12 | #include "flang/Parser/parse-tree.h"
|
12 | 13 | #include "flang/Parser/unparse.h"
|
@@ -98,4 +99,41 @@ void UnparseWithSymbols(llvm::raw_ostream &out, const parser::Program &program,
|
98 | 99 | int indent) { visitor.PrintSymbols(location, out, indent); }};
|
99 | 100 | parser::Unparse(out, program, encoding, false, true, &preStatement);
|
100 | 101 | }
|
| 102 | + |
| 103 | +// UnparseWithModules() |
| 104 | + |
| 105 | +class UsedModuleVisitor { |
| 106 | +public: |
| 107 | + UnorderedSymbolSet &modulesUsed() { return modulesUsed_; } |
| 108 | + UnorderedSymbolSet &modulesDefined() { return modulesDefined_; } |
| 109 | + template <typename T> bool Pre(const T &) { return true; } |
| 110 | + template <typename T> void Post(const T &) {} |
| 111 | + void Post(const parser::ModuleStmt &module) { |
| 112 | + if (module.v.symbol) { |
| 113 | + modulesDefined_.insert(*module.v.symbol); |
| 114 | + } |
| 115 | + } |
| 116 | + void Post(const parser::UseStmt &use) { |
| 117 | + if (use.moduleName.symbol) { |
| 118 | + modulesUsed_.insert(*use.moduleName.symbol); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +private: |
| 123 | + UnorderedSymbolSet modulesUsed_; |
| 124 | + UnorderedSymbolSet modulesDefined_; |
| 125 | +}; |
| 126 | + |
| 127 | +void UnparseWithModules(llvm::raw_ostream &out, SemanticsContext &context, |
| 128 | + const parser::Program &program, parser::Encoding encoding) { |
| 129 | + UsedModuleVisitor visitor; |
| 130 | + parser::Walk(program, visitor); |
| 131 | + UnorderedSymbolSet nonIntrinsicModulesWritten{ |
| 132 | + std::move(visitor.modulesDefined())}; |
| 133 | + ModFileWriter writer{context}; |
| 134 | + for (SymbolRef moduleRef : visitor.modulesUsed()) { |
| 135 | + writer.WriteClosure(out, *moduleRef, nonIntrinsicModulesWritten); |
| 136 | + } |
| 137 | + parser::Unparse(out, program, encoding, false, true); |
| 138 | +} |
101 | 139 | } // namespace Fortran::semantics
|
0 commit comments