| 1 | //===-- lib/Semantics/unparse-with-symbols.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 "flang/Semantics/unparse-with-symbols.h" |
| 10 | #include "mod-file.h" |
| 11 | #include "flang/Parser/parse-tree-visitor.h" |
| 12 | #include "flang/Parser/parse-tree.h" |
| 13 | #include "flang/Parser/unparse.h" |
| 14 | #include "flang/Semantics/semantics.h" |
| 15 | #include "flang/Semantics/symbol.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include <map> |
| 18 | #include <set> |
| 19 | |
| 20 | namespace Fortran::semantics { |
| 21 | |
| 22 | // Walk the parse tree and collection information about which statements |
| 23 | // reference symbols. Then PrintSymbols outputs information by statement. |
| 24 | // The first reference to a symbol is treated as its definition and more |
| 25 | // information is included. |
| 26 | class SymbolDumpVisitor { |
| 27 | public: |
| 28 | // Write out symbols referenced at this statement. |
| 29 | void PrintSymbols(const parser::CharBlock &, llvm::raw_ostream &, int); |
| 30 | |
| 31 | template <typename T> bool Pre(const T &) { return true; } |
| 32 | template <typename T> void Post(const T &) {} |
| 33 | template <typename T> bool Pre(const parser::Statement<T> &stmt) { |
| 34 | currStmt_ = stmt.source; |
| 35 | return true; |
| 36 | } |
| 37 | template <typename T> void Post(const parser::Statement<T> &) { |
| 38 | currStmt_ = std::nullopt; |
| 39 | } |
| 40 | bool Pre(const parser::AccClause &clause) { |
| 41 | currStmt_ = clause.source; |
| 42 | return true; |
| 43 | } |
| 44 | void Post(const parser::AccClause &) { currStmt_ = std::nullopt; } |
| 45 | bool Pre(const parser::OmpClause &clause) { |
| 46 | currStmt_ = clause.source; |
| 47 | return true; |
| 48 | } |
| 49 | void Post(const parser::OmpClause &) { currStmt_ = std::nullopt; } |
| 50 | bool Pre(const parser::OpenMPThreadprivate &dir) { |
| 51 | currStmt_ = dir.source; |
| 52 | return true; |
| 53 | } |
| 54 | void Post(const parser::OpenMPThreadprivate &) { currStmt_ = std::nullopt; } |
| 55 | void Post(const parser::Name &name); |
| 56 | |
| 57 | bool Pre(const parser::OpenMPDeclareMapperConstruct &x) { |
| 58 | currStmt_ = x.source; |
| 59 | return true; |
| 60 | } |
| 61 | void Post(const parser::OpenMPDeclareMapperConstruct &) { |
| 62 | currStmt_ = std::nullopt; |
| 63 | } |
| 64 | |
| 65 | bool Pre(const parser::OpenMPDeclareTargetConstruct &x) { |
| 66 | currStmt_ = x.source; |
| 67 | return true; |
| 68 | } |
| 69 | void Post(const parser::OpenMPDeclareTargetConstruct &) { |
| 70 | currStmt_ = std::nullopt; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | std::optional<SourceName> currStmt_; // current statement we are processing |
| 75 | std::multimap<const char *, const Symbol *> symbols_; // location to symbol |
| 76 | std::set<const Symbol *> symbolsDefined_; // symbols that have been processed |
| 77 | void Indent(llvm::raw_ostream &, int) const; |
| 78 | }; |
| 79 | |
| 80 | void SymbolDumpVisitor::PrintSymbols( |
| 81 | const parser::CharBlock &location, llvm::raw_ostream &out, int indent) { |
| 82 | std::set<const Symbol *> done; // prevent duplicates on this line |
| 83 | auto range{symbols_.equal_range(location.begin())}; |
| 84 | for (auto it{range.first}; it != range.second; ++it) { |
| 85 | const auto *symbol{it->second}; |
| 86 | if (done.insert(symbol).second) { |
| 87 | bool firstTime{symbolsDefined_.insert(symbol).second}; |
| 88 | Indent(out, indent); |
| 89 | out << '!' << (firstTime ? "DEF"s : "REF"s ) << ": " ; |
| 90 | DumpForUnparse(out, *symbol, firstTime); |
| 91 | out << '\n'; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void SymbolDumpVisitor::Indent(llvm::raw_ostream &out, int indent) const { |
| 97 | for (int i{0}; i < indent; ++i) { |
| 98 | out << ' '; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void SymbolDumpVisitor::Post(const parser::Name &name) { |
| 103 | if (const auto *symbol{name.symbol}) { |
| 104 | if (!symbol->has<MiscDetails>()) { |
| 105 | symbols_.emplace(currStmt_.value().begin(), symbol); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void UnparseWithSymbols(llvm::raw_ostream &out, const parser::Program &program, |
| 111 | const common::LangOptions &langOpts, parser::Encoding encoding) { |
| 112 | SymbolDumpVisitor visitor; |
| 113 | parser::Walk(program, visitor); |
| 114 | parser::preStatementType preStatement{ |
| 115 | [&](const parser::CharBlock &location, llvm::raw_ostream &out, |
| 116 | int indent) { visitor.PrintSymbols(location, out, indent); }}; |
| 117 | parser::Unparse(out, program, langOpts, encoding, false, true, &preStatement); |
| 118 | } |
| 119 | |
| 120 | // UnparseWithModules() |
| 121 | |
| 122 | class UsedModuleVisitor { |
| 123 | public: |
| 124 | UnorderedSymbolSet &modulesUsed() { return modulesUsed_; } |
| 125 | UnorderedSymbolSet &modulesDefined() { return modulesDefined_; } |
| 126 | template <typename T> bool Pre(const T &) { return true; } |
| 127 | template <typename T> void Post(const T &) {} |
| 128 | void Post(const parser::ModuleStmt &module) { |
| 129 | if (module.v.symbol) { |
| 130 | modulesDefined_.insert(*module.v.symbol); |
| 131 | } |
| 132 | } |
| 133 | void Post(const parser::UseStmt &use) { |
| 134 | if (use.moduleName.symbol) { |
| 135 | modulesUsed_.insert(*use.moduleName.symbol); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | UnorderedSymbolSet modulesUsed_; |
| 141 | UnorderedSymbolSet modulesDefined_; |
| 142 | }; |
| 143 | |
| 144 | void UnparseWithModules(llvm::raw_ostream &out, SemanticsContext &context, |
| 145 | const parser::Program &program, parser::Encoding encoding) { |
| 146 | UsedModuleVisitor visitor; |
| 147 | parser::Walk(program, visitor); |
| 148 | UnorderedSymbolSet nonIntrinsicModulesWritten{ |
| 149 | std::move(visitor.modulesDefined())}; |
| 150 | ModFileWriter writer{context}; |
| 151 | for (SymbolRef moduleRef : visitor.modulesUsed()) { |
| 152 | writer.WriteClosure(out, *moduleRef, nonIntrinsicModulesWritten); |
| 153 | } |
| 154 | parser::Unparse(out, program, context.langOptions(), encoding, false, true); |
| 155 | } |
| 156 | } // namespace Fortran::semantics |
| 157 | |