llvm/flang/lib/semantics/unparse-with-symbols.cc

95 lines
3.2 KiB
C++
Raw Normal View History

// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "unparse-with-symbols.h"
#include "symbol.h"
#include "../parser/parse-tree-visitor.h"
#include "../parser/parse-tree.h"
#include "../parser/unparse.h"
#include <map>
#include <ostream>
#include <set>
namespace Fortran::semantics {
// Walk the parse tree and collection information about which statements
// reference symbols. Then PrintSymbols outputs information by statement.
// The first reference to a symbol is treated as its definition and more
// information is included.
class SymbolDumpVisitor {
public:
// Write out symbols referenced at this statement.
void PrintSymbols(const parser::CharBlock &, std::ostream &, int);
template<typename T> bool Pre(const T &) { return true; }
template<typename T> void Post(const T &) {}
template<typename T> bool Pre(const parser::Statement<T> &stmt) {
currStmt_ = &stmt.source;
return true;
}
template<typename T> void Post(const parser::Statement<T> &) {
currStmt_ = nullptr;
}
void Post(const parser::Name &name);
private:
const SourceName *currStmt_{nullptr}; // current statement we are processing
std::multimap<const char *, const Symbol *> symbols_; // location to symbol
std::set<const Symbol *> symbolsDefined_; // symbols that have been processed
void Indent(std::ostream &, int) const;
};
void SymbolDumpVisitor::PrintSymbols(
const parser::CharBlock &location, std::ostream &out, int indent) {
std::set<const Symbol *> done; // prevent duplicates on this line
auto range{symbols_.equal_range(location.begin())};
for (auto it{range.first}; it != range.second; ++it) {
const auto *symbol{it->second};
if (done.insert(symbol).second) {
bool firstTime{symbolsDefined_.insert(symbol).second};
Indent(out, indent);
out << '!' << (firstTime ? "DEF"s : "REF"s) << ": ";
DumpForUnparse(out, *symbol, firstTime);
out << '\n';
}
}
}
void SymbolDumpVisitor::Indent(std::ostream &out, int indent) const {
for (int i{0}; i < indent; ++i) {
out << ' ';
}
}
void SymbolDumpVisitor::Post(const parser::Name &name) {
if (const auto *symbol{name.symbol}) {
[flang] Change when symbol is set in parser::Name Rework how `parser::Name` is resolved to contain a `Symbol`. so that constants in types can be evaluated. For example: ``` integer, parameter :: k = 8 integer(k) :: i ``` The old approach of collecting the symbols at the end of name resolution and filling in the `parser::Name` does not work because the type of `i` needs to be set in the symbol table. The symbol field in `parser::Name` is now mutable so that we can set it during name resolution. `RewriteParseTree` no longer needs to do that (it still warns about unresolved ones), so it does not need to collect symbols and fill them in. Consequently, we can eliminate "occurrences" from symbols -- we just need the name where each is first defined. This requires a lot of refactoring in `resolve-names.cc` to pass around `parser::Name` rather than `SourceName` so that we can resolve the name to a symbol. Fix some bugs where we stored `SourceName *` instead of `SourceName` in the symbol table. The pointers were into the parse tree, so they were only valid as long as the parse tree was around. The symbol table needs to remain valid longer than that, so the names need to be copied. `parser::Name` is not used in the symbol table. Eliminate `GenericSpec`. Currently all we need to do is to resolve the kinds of GenericSpec that contain names. Add `ScopeName` kind of `MiscDetails` for when we need a symbol in the scope to match the name of the scope. For example, `module m` cannot contain a declaration of a new `m`. Subprograms need real details because they can be called recursively. Fix output of partially resolved modules where we know it is a submodule but have not yet resolved the ancestor. Original-commit: flang-compiler/f18@5c1a4b99d2421f5b32e83426488d3fdf7951cfba Reviewed-on: https://github.com/flang-compiler/f18/pull/238 Tree-same-pre-rewrite: false
2018-11-16 21:43:08 +01:00
if (!symbol->has<MiscDetails>()) {
CHECK(currStmt_);
symbols_.emplace(currStmt_->begin(), symbol);
}
}
}
void UnparseWithSymbols(std::ostream &out, const parser::Program &program,
parser::Encoding encoding) {
SymbolDumpVisitor visitor;
parser::Walk(program, visitor);
parser::preStatementType preStatement{
[&](const parser::CharBlock &location, std::ostream &out, int indent) {
visitor.PrintSymbols(location, out, indent);
}};
parser::Unparse(out, program, encoding, false, true, &preStatement);
}
}