2020-02-25 15:11:52 +00:00
|
|
|
//===-- lib/Semantics/semantics.cpp ---------------------------------------===//
|
2018-09-14 15:04:50 -07:00
|
|
|
//
|
2019-12-20 12:52:07 -08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-09-14 15:04:50 -07:00
|
|
|
//
|
2020-01-10 12:12:03 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-09-14 15:04:50 -07:00
|
|
|
|
2020-02-25 15:11:52 +00:00
|
|
|
#include "flang/Semantics/semantics.h"
|
2018-12-04 10:55:32 -08:00
|
|
|
#include "assignment.h"
|
2018-09-19 14:26:02 -07:00
|
|
|
#include "canonicalize-do.h"
|
2019-08-22 10:34:15 -07:00
|
|
|
#include "canonicalize-omp.h"
|
2019-04-18 09:17:31 -07:00
|
|
|
#include "check-allocate.h"
|
2019-03-26 00:33:03 -07:00
|
|
|
#include "check-arithmeticif.h"
|
2020-03-26 12:25:29 -07:00
|
|
|
#include "check-case.h"
|
2019-04-15 10:26:20 -07:00
|
|
|
#include "check-coarray.h"
|
2020-02-21 11:49:14 +05:30
|
|
|
#include "check-data.h"
|
2019-04-10 22:17:44 -07:00
|
|
|
#include "check-deallocate.h"
|
2019-09-10 17:08:18 -07:00
|
|
|
#include "check-declarations.h"
|
2020-02-19 13:28:19 -08:00
|
|
|
#include "check-do-forall.h"
|
2019-03-26 00:33:03 -07:00
|
|
|
#include "check-if-stmt.h"
|
2019-04-30 11:28:16 -07:00
|
|
|
#include "check-io.h"
|
2020-02-25 18:01:23 -08:00
|
|
|
#include "check-namelist.h"
|
2019-04-07 11:29:48 -07:00
|
|
|
#include "check-nullify.h"
|
2019-06-25 16:18:51 -07:00
|
|
|
#include "check-omp-structure.h"
|
2019-11-12 15:43:09 -08:00
|
|
|
#include "check-purity.h"
|
2019-04-13 20:58:25 -07:00
|
|
|
#include "check-return.h"
|
2019-03-18 16:19:41 +00:00
|
|
|
#include "check-stop.h"
|
2018-09-14 15:04:50 -07:00
|
|
|
#include "mod-file.h"
|
|
|
|
|
#include "resolve-labels.h"
|
|
|
|
|
#include "resolve-names.h"
|
|
|
|
|
#include "rewrite-parse-tree.h"
|
2020-02-25 15:11:52 +00:00
|
|
|
#include "flang/Common/default-kinds.h"
|
|
|
|
|
#include "flang/Parser/parse-tree-visitor.h"
|
|
|
|
|
#include "flang/Parser/tools.h"
|
|
|
|
|
#include "flang/Semantics/expression.h"
|
|
|
|
|
#include "flang/Semantics/scope.h"
|
|
|
|
|
#include "flang/Semantics/symbol.h"
|
2020-02-28 15:11:03 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-09-14 15:04:50 -07:00
|
|
|
|
|
|
|
|
namespace Fortran::semantics {
|
|
|
|
|
|
2019-10-22 16:53:29 -07:00
|
|
|
using NameToSymbolMap = std::map<const char *, SymbolRef>;
|
2020-02-28 15:11:03 +00:00
|
|
|
static void DoDumpSymbols(llvm::raw_ostream &, const Scope &, int indent = 0);
|
|
|
|
|
static void PutIndent(llvm::raw_ostream &, int indent);
|
2018-09-14 15:04:50 -07:00
|
|
|
|
2019-09-05 10:05:45 -07:00
|
|
|
static void GetSymbolNames(const Scope &scope, NameToSymbolMap &symbols) {
|
|
|
|
|
// Finds all symbol names in the scope without collecting duplicates.
|
|
|
|
|
for (const auto &pair : scope) {
|
2019-10-22 16:53:29 -07:00
|
|
|
symbols.emplace(pair.second->name().begin(), *pair.second);
|
2019-09-05 10:05:45 -07:00
|
|
|
}
|
|
|
|
|
for (const auto &pair : scope.commonBlocks()) {
|
2019-10-22 16:53:29 -07:00
|
|
|
symbols.emplace(pair.second->name().begin(), *pair.second);
|
2019-09-05 10:05:45 -07:00
|
|
|
}
|
|
|
|
|
for (const auto &child : scope.children()) {
|
|
|
|
|
GetSymbolNames(child, symbols);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 16:52:50 -08:00
|
|
|
// A parse tree visitor that calls Enter/Leave functions from each checker
|
|
|
|
|
// class C supplied as template parameters. Enter is called before the node's
|
|
|
|
|
// children are visited, Leave is called after. No two checkers may have the
|
|
|
|
|
// same Enter or Leave function. Each checker must be constructible from
|
|
|
|
|
// SemanticsContext and have BaseChecker as a virtual base class.
|
2020-03-26 12:25:29 -07:00
|
|
|
template <typename... C> class SemanticsVisitor : public virtual C... {
|
2019-03-06 17:07:25 -08:00
|
|
|
public:
|
2019-03-05 16:52:50 -08:00
|
|
|
using C::Enter...;
|
|
|
|
|
using C::Leave...;
|
|
|
|
|
using BaseChecker::Enter;
|
|
|
|
|
using BaseChecker::Leave;
|
2019-03-06 17:07:25 -08:00
|
|
|
SemanticsVisitor(SemanticsContext &context)
|
2020-03-26 12:25:29 -07:00
|
|
|
: C{context}..., context_{context} {}
|
2019-03-26 12:40:13 -07:00
|
|
|
|
2020-03-26 12:25:29 -07:00
|
|
|
template <typename N> bool Pre(const N &node) {
|
2019-09-10 14:42:34 -07:00
|
|
|
if constexpr (common::HasMember<const N *, ConstructNode>) {
|
|
|
|
|
context_.PushConstruct(node);
|
|
|
|
|
}
|
2019-03-05 16:52:50 -08:00
|
|
|
Enter(node);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-03-26 12:25:29 -07:00
|
|
|
template <typename N> void Post(const N &node) {
|
2019-09-10 14:42:34 -07:00
|
|
|
Leave(node);
|
|
|
|
|
if constexpr (common::HasMember<const N *, ConstructNode>) {
|
|
|
|
|
context_.PopConstruct();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-26 12:40:13 -07:00
|
|
|
|
2020-03-26 12:25:29 -07:00
|
|
|
template <typename T> bool Pre(const parser::Statement<T> &node) {
|
2019-08-21 05:33:03 -07:00
|
|
|
context_.set_location(node.source);
|
2019-03-26 12:40:13 -07:00
|
|
|
Enter(node);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-03-26 12:25:29 -07:00
|
|
|
template <typename T> bool Pre(const parser::UnlabeledStatement<T> &node) {
|
2019-08-26 16:26:24 -07:00
|
|
|
context_.set_location(node.source);
|
|
|
|
|
Enter(node);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-03-26 12:25:29 -07:00
|
|
|
template <typename T> void Post(const parser::Statement<T> &node) {
|
2019-03-26 12:40:13 -07:00
|
|
|
Leave(node);
|
2019-08-21 05:33:03 -07:00
|
|
|
context_.set_location(std::nullopt);
|
2019-03-26 12:40:13 -07:00
|
|
|
}
|
2020-03-26 12:25:29 -07:00
|
|
|
template <typename T> void Post(const parser::UnlabeledStatement<T> &node) {
|
2019-08-26 16:26:24 -07:00
|
|
|
Leave(node);
|
|
|
|
|
context_.set_location(std::nullopt);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 17:07:25 -08:00
|
|
|
bool Walk(const parser::Program &program) {
|
|
|
|
|
parser::Walk(program, *this);
|
|
|
|
|
return !context_.AnyFatalError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
SemanticsContext &context_;
|
2019-03-05 16:52:50 -08:00
|
|
|
};
|
|
|
|
|
|
2020-04-03 12:05:03 -07:00
|
|
|
class MiscChecker : public virtual BaseChecker {
|
2020-03-19 16:31:10 -07:00
|
|
|
public:
|
2020-04-03 12:05:03 -07:00
|
|
|
explicit MiscChecker(SemanticsContext &context) : context_{context} {}
|
2020-03-19 16:31:10 -07:00
|
|
|
void Leave(const parser::EntryStmt &) {
|
2020-03-26 12:25:29 -07:00
|
|
|
if (!context_.constructStack().empty()) { // C1571
|
2020-03-19 16:31:10 -07:00
|
|
|
context_.Say("ENTRY may not appear in an executable construct"_err_en_US);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-03 12:05:03 -07:00
|
|
|
void Leave(const parser::AssignStmt &stmt) {
|
|
|
|
|
CheckAssignGotoName(std::get<parser::Name>(stmt.t));
|
|
|
|
|
}
|
|
|
|
|
void Leave(const parser::AssignedGotoStmt &stmt) {
|
|
|
|
|
CheckAssignGotoName(std::get<parser::Name>(stmt.t));
|
|
|
|
|
}
|
2020-03-19 16:31:10 -07:00
|
|
|
|
|
|
|
|
private:
|
2020-04-03 12:05:03 -07:00
|
|
|
void CheckAssignGotoName(const parser::Name &name) {
|
|
|
|
|
if (context_.HasError(name.symbol)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const Symbol &symbol{DEREF(name.symbol)};
|
|
|
|
|
auto type{evaluate::DynamicType::From(symbol)};
|
|
|
|
|
if (!IsVariableName(symbol) || symbol.Rank() != 0 || !type ||
|
|
|
|
|
type->category() != TypeCategory::Integer ||
|
|
|
|
|
type->kind() !=
|
|
|
|
|
context_.defaultKinds().GetDefaultKind(TypeCategory::Integer)) {
|
|
|
|
|
context_
|
|
|
|
|
.Say(name.source,
|
|
|
|
|
"'%s' must be a default integer scalar variable"_err_en_US,
|
|
|
|
|
name.source)
|
|
|
|
|
.Attach(symbol.name(), "Declaration of '%s'"_en_US, symbol.name());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 16:31:10 -07:00
|
|
|
SemanticsContext &context_;
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-11 17:22:16 -07:00
|
|
|
using StatementSemanticsPass1 = ExprChecker;
|
2020-02-25 18:01:23 -08:00
|
|
|
using StatementSemanticsPass2 = SemanticsVisitor<AllocateChecker,
|
2020-03-26 12:25:29 -07:00
|
|
|
ArithmeticIfStmtChecker, AssignmentChecker, CaseChecker, CoarrayChecker,
|
2020-04-03 12:05:03 -07:00
|
|
|
DataChecker, DeallocateChecker, DoForallChecker, IfStmtChecker, IoChecker,
|
|
|
|
|
MiscChecker, NamelistChecker, NullifyChecker, OmpStructureChecker,
|
|
|
|
|
PurityChecker, ReturnStmtChecker, StopChecker>;
|
2019-03-05 16:52:50 -08:00
|
|
|
|
2019-04-19 09:21:12 -07:00
|
|
|
static bool PerformStatementSemantics(
|
2019-04-25 13:18:33 -07:00
|
|
|
SemanticsContext &context, parser::Program &program) {
|
|
|
|
|
ResolveNames(context, program);
|
|
|
|
|
RewriteParseTree(context, program);
|
2019-09-10 15:51:46 -07:00
|
|
|
CheckDeclarations(context);
|
2019-04-19 09:21:12 -07:00
|
|
|
StatementSemanticsPass1{context}.Walk(program);
|
2020-02-20 14:54:46 -08:00
|
|
|
StatementSemanticsPass2{context}.Walk(program);
|
|
|
|
|
return !context.AnyFatalError();
|
2019-04-19 09:21:12 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-26 00:33:03 -07:00
|
|
|
SemanticsContext::SemanticsContext(
|
|
|
|
|
const common::IntrinsicTypeDefaultKinds &defaultKinds,
|
2019-11-06 11:15:03 -08:00
|
|
|
const common::LanguageFeatureControl &languageFeatures,
|
2019-05-31 16:37:00 -07:00
|
|
|
parser::AllSources &allSources)
|
2020-03-26 12:25:29 -07:00
|
|
|
: defaultKinds_{defaultKinds}, languageFeatures_{languageFeatures},
|
|
|
|
|
allSources_{allSources},
|
|
|
|
|
intrinsics_{evaluate::IntrinsicProcTable::Configure(defaultKinds_)},
|
|
|
|
|
foldingContext_{
|
|
|
|
|
parser::ContextualMessages{&messages_}, defaultKinds_, intrinsics_} {}
|
2018-10-22 16:41:26 -07:00
|
|
|
|
2019-05-31 16:37:00 -07:00
|
|
|
SemanticsContext::~SemanticsContext() {}
|
|
|
|
|
|
2019-06-11 18:26:48 -07:00
|
|
|
int SemanticsContext::GetDefaultKind(TypeCategory category) const {
|
|
|
|
|
return defaultKinds_.GetDefaultKind(category);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 11:15:03 -08:00
|
|
|
bool SemanticsContext::IsEnabled(common::LanguageFeature feature) const {
|
2019-03-18 11:48:02 -07:00
|
|
|
return languageFeatures_.IsEnabled(feature);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 11:15:03 -08:00
|
|
|
bool SemanticsContext::ShouldWarn(common::LanguageFeature feature) const {
|
2019-03-18 11:48:02 -07:00
|
|
|
return languageFeatures_.ShouldWarn(feature);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec &SemanticsContext::MakeNumericType(
|
2018-12-17 12:41:43 -08:00
|
|
|
TypeCategory category, int kind) {
|
|
|
|
|
if (kind == 0) {
|
2019-06-11 18:26:48 -07:00
|
|
|
kind = GetDefaultKind(category);
|
2018-12-17 12:41:43 -08:00
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
return globalScope_.MakeNumericType(category, KindExpr{kind});
|
2018-12-17 12:41:43 -08:00
|
|
|
}
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec &SemanticsContext::MakeLogicalType(int kind) {
|
2018-12-17 12:41:43 -08:00
|
|
|
if (kind == 0) {
|
2019-06-11 18:26:48 -07:00
|
|
|
kind = GetDefaultKind(TypeCategory::Logical);
|
2018-12-17 12:41:43 -08:00
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
return globalScope_.MakeLogicalType(KindExpr{kind});
|
2018-12-17 12:41:43 -08:00
|
|
|
}
|
|
|
|
|
|
2018-10-22 07:37:38 -07:00
|
|
|
bool SemanticsContext::AnyFatalError() const {
|
|
|
|
|
return !messages_.empty() &&
|
|
|
|
|
(warningsAreErrors_ || messages_.AnyFatalError());
|
2018-09-14 15:04:50 -07:00
|
|
|
}
|
2019-04-25 14:47:39 -07:00
|
|
|
bool SemanticsContext::HasError(const Symbol &symbol) {
|
|
|
|
|
return CheckError(symbol.test(Symbol::Flag::Error));
|
|
|
|
|
}
|
|
|
|
|
bool SemanticsContext::HasError(const Symbol *symbol) {
|
|
|
|
|
return CheckError(!symbol || HasError(*symbol));
|
|
|
|
|
}
|
|
|
|
|
bool SemanticsContext::HasError(const parser::Name &name) {
|
|
|
|
|
return HasError(name.symbol);
|
|
|
|
|
}
|
|
|
|
|
void SemanticsContext::SetError(Symbol &symbol, bool value) {
|
|
|
|
|
if (value) {
|
|
|
|
|
CHECK(AnyFatalError());
|
|
|
|
|
symbol.set(Symbol::Flag::Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bool SemanticsContext::CheckError(bool error) {
|
|
|
|
|
CHECK(!error || AnyFatalError());
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2018-09-14 15:04:50 -07:00
|
|
|
|
2019-04-02 10:58:26 -07:00
|
|
|
const Scope &SemanticsContext::FindScope(parser::CharBlock source) const {
|
2019-04-02 11:56:19 -07:00
|
|
|
return const_cast<SemanticsContext *>(this)->FindScope(source);
|
2019-04-02 10:58:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scope &SemanticsContext::FindScope(parser::CharBlock source) {
|
|
|
|
|
if (auto *scope{globalScope_.FindScope(source)}) {
|
2019-03-01 17:33:20 -08:00
|
|
|
return *scope;
|
|
|
|
|
} else {
|
2020-01-02 09:55:03 -08:00
|
|
|
common::die("SemanticsContext::FindScope(): invalid source location");
|
2019-03-01 17:33:20 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 16:02:37 -07:00
|
|
|
void SemanticsContext::PopConstruct() {
|
|
|
|
|
CHECK(!constructStack_.empty());
|
|
|
|
|
constructStack_.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 17:14:24 -08:00
|
|
|
void SemanticsContext::CheckIndexVarRedefine(const parser::CharBlock &location,
|
2020-01-02 12:26:47 -08:00
|
|
|
const Symbol &variable, parser::MessageFixedText &&message) {
|
|
|
|
|
if (const Symbol * root{GetAssociationRoot(variable)}) {
|
2020-02-18 17:14:24 -08:00
|
|
|
auto it{activeIndexVars_.find(*root)};
|
|
|
|
|
if (it != activeIndexVars_.end()) {
|
|
|
|
|
std::string kind{EnumToString(it->second.kind)};
|
|
|
|
|
Say(location, std::move(message), kind, root->name())
|
|
|
|
|
.Attach(it->second.location, "Enclosing %s construct"_en_US, kind);
|
2020-01-02 12:26:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 17:14:24 -08:00
|
|
|
void SemanticsContext::WarnIndexVarRedefine(
|
2019-12-06 19:59:54 -08:00
|
|
|
const parser::CharBlock &location, const Symbol &variable) {
|
2020-02-18 17:14:24 -08:00
|
|
|
CheckIndexVarRedefine(
|
|
|
|
|
location, variable, "Possible redefinition of %s variable '%s'"_en_US);
|
2019-12-06 19:59:54 -08:00
|
|
|
}
|
|
|
|
|
|
2020-02-18 17:14:24 -08:00
|
|
|
void SemanticsContext::CheckIndexVarRedefine(
|
2020-01-02 12:26:47 -08:00
|
|
|
const parser::CharBlock &location, const Symbol &variable) {
|
2020-02-18 17:14:24 -08:00
|
|
|
CheckIndexVarRedefine(
|
|
|
|
|
location, variable, "Cannot redefine %s variable '%s'"_err_en_US);
|
2019-12-06 19:59:54 -08:00
|
|
|
}
|
|
|
|
|
|
2020-02-18 17:14:24 -08:00
|
|
|
void SemanticsContext::CheckIndexVarRedefine(const parser::Variable &variable) {
|
2019-12-06 19:59:54 -08:00
|
|
|
if (const Symbol * entity{GetLastName(variable).symbol}) {
|
2020-02-18 17:14:24 -08:00
|
|
|
CheckIndexVarRedefine(variable.GetSource(), *entity);
|
2019-12-06 19:59:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 17:14:24 -08:00
|
|
|
void SemanticsContext::CheckIndexVarRedefine(const parser::Name &name) {
|
2019-12-06 19:59:54 -08:00
|
|
|
if (const Symbol * entity{name.symbol}) {
|
2020-02-18 17:14:24 -08:00
|
|
|
CheckIndexVarRedefine(name.source, *entity);
|
2019-12-06 19:59:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 17:14:24 -08:00
|
|
|
void SemanticsContext::ActivateIndexVar(
|
|
|
|
|
const parser::Name &name, IndexVarKind kind) {
|
|
|
|
|
CheckIndexVarRedefine(name);
|
|
|
|
|
if (const Symbol * indexVar{name.symbol}) {
|
|
|
|
|
if (const Symbol * root{GetAssociationRoot(*indexVar)}) {
|
|
|
|
|
activeIndexVars_.emplace(*root, IndexVarInfo{name.source, kind});
|
2019-12-06 19:59:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 17:14:24 -08:00
|
|
|
void SemanticsContext::DeactivateIndexVar(const parser::Name &name) {
|
|
|
|
|
if (Symbol * indexVar{name.symbol}) {
|
|
|
|
|
if (const Symbol * root{GetAssociationRoot(*indexVar)}) {
|
|
|
|
|
auto it{activeIndexVars_.find(*root)};
|
|
|
|
|
if (it != activeIndexVars_.end() && it->second.location == name.source) {
|
|
|
|
|
activeIndexVars_.erase(it);
|
2019-12-06 19:59:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 14:54:46 -08:00
|
|
|
SymbolVector SemanticsContext::GetIndexVars(IndexVarKind kind) {
|
|
|
|
|
SymbolVector result;
|
|
|
|
|
for (const auto &[symbol, info] : activeIndexVars_) {
|
|
|
|
|
if (info.kind == kind) {
|
|
|
|
|
result.push_back(symbol);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 07:37:38 -07:00
|
|
|
bool Semantics::Perform() {
|
2019-07-09 11:54:40 -07:00
|
|
|
return ValidateLabels(context_, program_) &&
|
2020-03-26 12:25:29 -07:00
|
|
|
parser::CanonicalizeDo(program_) && // force line break
|
2019-08-22 10:34:15 -07:00
|
|
|
CanonicalizeOmp(context_.messages(), program_) &&
|
2019-04-19 09:21:12 -07:00
|
|
|
PerformStatementSemantics(context_, program_) &&
|
2019-03-06 17:07:25 -08:00
|
|
|
ModFileWriter{context_}.WriteAll();
|
2018-09-14 15:04:50 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 15:11:03 +00:00
|
|
|
void Semantics::EmitMessages(llvm::raw_ostream &os) const {
|
2018-10-22 07:37:38 -07:00
|
|
|
context_.messages().Emit(os, cooked_);
|
2018-09-14 15:04:50 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 15:11:03 +00:00
|
|
|
void Semantics::DumpSymbols(llvm::raw_ostream &os) {
|
2018-10-22 07:37:38 -07:00
|
|
|
DoDumpSymbols(os, context_.globalScope());
|
2018-09-14 15:04:50 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 15:11:03 +00:00
|
|
|
void Semantics::DumpSymbolsSources(llvm::raw_ostream &os) const {
|
2019-09-05 10:05:45 -07:00
|
|
|
NameToSymbolMap symbols;
|
|
|
|
|
GetSymbolNames(context_.globalScope(), symbols);
|
2020-02-06 03:27:36 -08:00
|
|
|
for (const auto &pair : symbols) {
|
2019-10-22 16:53:29 -07:00
|
|
|
const Symbol &symbol{pair.second};
|
2019-09-09 11:36:42 -07:00
|
|
|
if (auto sourceInfo{cooked_.GetSourcePositionRange(symbol.name())}) {
|
2019-09-05 10:05:45 -07:00
|
|
|
os << symbol.name().ToString() << ": " << sourceInfo->first.file.path()
|
|
|
|
|
<< ", " << sourceInfo->first.line << ", " << sourceInfo->first.column
|
|
|
|
|
<< "-" << sourceInfo->second.column << "\n";
|
|
|
|
|
} else if (symbol.has<semantics::UseDetails>()) {
|
|
|
|
|
os << symbol.name().ToString() << ": "
|
|
|
|
|
<< symbol.GetUltimate().owner().symbol()->name().ToString() << "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 15:11:03 +00:00
|
|
|
void DoDumpSymbols(llvm::raw_ostream &os, const Scope &scope, int indent) {
|
2018-09-14 15:04:50 -07:00
|
|
|
PutIndent(os, indent);
|
|
|
|
|
os << Scope::EnumToString(scope.kind()) << " scope:";
|
|
|
|
|
if (const auto *symbol{scope.symbol()}) {
|
2019-09-04 16:55:08 -07:00
|
|
|
os << ' ' << symbol->name();
|
2018-09-14 15:04:50 -07:00
|
|
|
}
|
|
|
|
|
os << '\n';
|
|
|
|
|
++indent;
|
|
|
|
|
for (const auto &pair : scope) {
|
|
|
|
|
const auto &symbol{*pair.second};
|
|
|
|
|
PutIndent(os, indent);
|
|
|
|
|
os << symbol << '\n';
|
|
|
|
|
if (const auto *details{symbol.detailsIf<GenericDetails>()}) {
|
|
|
|
|
if (const auto &type{details->derivedType()}) {
|
|
|
|
|
PutIndent(os, indent);
|
|
|
|
|
os << *type << '\n';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-11 18:26:48 -07:00
|
|
|
if (!scope.equivalenceSets().empty()) {
|
|
|
|
|
PutIndent(os, indent);
|
|
|
|
|
os << "Equivalence Sets:";
|
|
|
|
|
for (const auto &set : scope.equivalenceSets()) {
|
|
|
|
|
os << ' ';
|
|
|
|
|
char sep = '(';
|
|
|
|
|
for (const auto &object : set) {
|
|
|
|
|
os << sep << object.AsFortran();
|
|
|
|
|
sep = ',';
|
|
|
|
|
}
|
|
|
|
|
os << ')';
|
|
|
|
|
}
|
|
|
|
|
os << '\n';
|
|
|
|
|
}
|
2019-09-04 16:55:08 -07:00
|
|
|
if (!scope.crayPointers().empty()) {
|
|
|
|
|
PutIndent(os, indent);
|
|
|
|
|
os << "Cray Pointers:";
|
|
|
|
|
for (const auto &[pointee, pointer] : scope.crayPointers()) {
|
|
|
|
|
os << " (" << pointer->name() << ',' << pointee << ')';
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-18 11:39:46 -08:00
|
|
|
for (const auto &pair : scope.commonBlocks()) {
|
|
|
|
|
const auto &symbol{*pair.second};
|
|
|
|
|
PutIndent(os, indent);
|
|
|
|
|
os << symbol << '\n';
|
|
|
|
|
}
|
2018-09-14 15:04:50 -07:00
|
|
|
for (const auto &child : scope.children()) {
|
|
|
|
|
DoDumpSymbols(os, child, indent);
|
|
|
|
|
}
|
|
|
|
|
--indent;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 15:11:03 +00:00
|
|
|
static void PutIndent(llvm::raw_ostream &os, int indent) {
|
2018-09-14 15:04:50 -07:00
|
|
|
for (int i = 0; i < indent; ++i) {
|
|
|
|
|
os << " ";
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-26 12:25:29 -07:00
|
|
|
} // namespace Fortran::semantics
|