| 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/symbol.h" |
| 15 |
#include "llvm/Support/raw_ostream.h" |
| 16 |
#include <map> |
| 17 |
#include <set> |
| 18 |
|
| 19 |
namespace Fortran::semantics { |
| 20 |
|
| 21 |
// Walk the parse tree and collection information about which statements |
| 22 |
// reference symbols. Then PrintSymbols outputs information by statement. |
| 23 |
// The first reference to a symbol is treated as its definition and more |
| 24 |
// information is included. |
| 25 |
class SymbolDumpVisitor { |
| 26 |
public: |
| 27 |
// Write out symbols referenced at this statement. |
| 28 |
void PrintSymbols(const parser::CharBlock &, llvm::raw_ostream &, int); |
| 29 |
|
| 30 |
template <typename T> bool Pre(const T &) { return true; } |
| 31 |
template <typename T> void Post(const T &) {} |
| 32 |
template <typename T> bool Pre(const parser::Statement<T> &stmt) { |
| 33 |
currStmt_ = stmt.source; |
| 34 |
return true; |
| 35 |
} |
| 36 |
template <typename T> void Post(const parser::Statement<T> &) { |
| 37 |
currStmt_ = std::nullopt; |
| 38 |
} |
| 39 |
bool Pre(const parser::AccClause &clause) { |
| 40 |
currStmt_ = clause.source; |
| 41 |
return true; |
| 42 |
} |
| 43 |
void Post(const parser::AccClause &) { currStmt_ = std::nullopt; } |
| 44 |
bool Pre(const parser::OmpClause &clause) { |
| 45 |
currStmt_ = clause.source; |
| 46 |
return true; |
| 47 |
} |
| 48 |
void Post(const parser::OmpClause &) { currStmt_ = std::nullopt; } |
| 49 |
bool Pre(const parser::OpenMPThreadprivate &dir) { |
| 50 |
currStmt_ = dir.source; |
| 51 |
return true; |
| 52 |
} |
| 53 |
void Post(const parser::OpenMPThreadprivate &) { currStmt_ = std::nullopt; } |
| 54 |
void Post(const parser::Name &name); |
| 55 |
|
| 56 |
private: |
| 57 |
std::optional<SourceName> currStmt_; // current statement we are processing |
| 58 |
std::multimap<const char *, const Symbol *> symbols_; // location to symbol |
| 59 |
std::set<const Symbol *> symbolsDefined_; // symbols that have been processed |
| 60 |
void Indent(llvm::raw_ostream &, int) const; |
| 61 |
}; |
| 62 |
|
| 63 |
void SymbolDumpVisitor::PrintSymbols( |
| 64 |
const parser::CharBlock &location, llvm::raw_ostream &out, int indent) { |
| 65 |
std::set<const Symbol *> done; // prevent duplicates on this line |
| 66 |
auto range{symbols_.equal_range(location.begin())}; |
| 67 |
for (auto it{range.first}; it != range.second; ++it) { |
| 68 |
const auto *symbol{it->second}; |
| 69 |
if (done.insert(symbol).second) { |
| 70 |
bool firstTime{symbolsDefined_.insert(symbol).second}; |
| 71 |
Indent(out, indent); |
| 72 |
out << '!' << (firstTime ? "DEF"s : "REF"s) << ": "; |
| 73 |
DumpForUnparse(out, *symbol, firstTime); |
| 74 |
out << '\n'; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void SymbolDumpVisitor::Indent(llvm::raw_ostream &out, int indent) const { |
| 80 |
for (int i{0}; i < indent; ++i) { |
| 81 |
out << ' '; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void SymbolDumpVisitor::Post(const parser::Name &name) { |
| 86 |
if (const auto *symbol{name.symbol}) { |
| 87 |
if (!symbol->has<MiscDetails>()) { |
| 88 |
symbols_.emplace(currStmt_.value().begin(), symbol); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void UnparseWithSymbols(llvm::raw_ostream &out, const parser::Program &program, |
| 94 |
parser::Encoding encoding) { |
| 95 |
SymbolDumpVisitor visitor; |
| 96 |
parser::Walk(program, visitor); |
| 97 |
parser::preStatementType preStatement{ |
| 98 |
[&](const parser::CharBlock &location, llvm::raw_ostream &out, |
| 99 |
int indent) { visitor.PrintSymbols(location, out, indent); }}; |
| 100 |
parser::Unparse(out, program, encoding, false, true, &preStatement); |
| 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 |
} |
| 139 |
} // namespace Fortran::semantics |