2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Semantics/scope.cpp -------------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2019-12-21 04: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-05-02 03:50:34 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Semantics/scope.h"
|
|
|
|
#include "flang/Parser/characters.h"
|
|
|
|
#include "flang/Semantics/symbol.h"
|
|
|
|
#include "flang/Semantics/type.h"
|
2020-02-28 23:11:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-12-12 06:51:08 +08:00
|
|
|
#include <algorithm>
|
2018-03-23 08:08:20 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2018-03-24 03:24:29 +08:00
|
|
|
namespace Fortran::semantics {
|
2018-03-23 08:08:20 +08:00
|
|
|
|
2018-06-20 07:06:41 +08:00
|
|
|
Symbols<1024> Scope::allSymbols;
|
|
|
|
|
2019-06-12 09:26:48 +08:00
|
|
|
bool EquivalenceObject::operator==(const EquivalenceObject &that) const {
|
2019-06-13 03:38:04 +08:00
|
|
|
return symbol == that.symbol && subscripts == that.subscripts &&
|
|
|
|
substringStart == that.substringStart;
|
2019-06-12 09:26:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EquivalenceObject::operator<(const EquivalenceObject &that) const {
|
|
|
|
return &symbol < &that.symbol ||
|
2019-06-13 03:38:04 +08:00
|
|
|
(&symbol == &that.symbol &&
|
|
|
|
(subscripts < that.subscripts ||
|
|
|
|
(subscripts == that.subscripts &&
|
|
|
|
substringStart < that.substringStart)));
|
2019-06-12 09:26:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string EquivalenceObject::AsFortran() const {
|
2020-02-28 23:11:03 +08:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream ss{buf};
|
2019-06-12 09:26:48 +08:00
|
|
|
ss << symbol.name().ToString();
|
|
|
|
if (!subscripts.empty()) {
|
|
|
|
char sep{'('};
|
|
|
|
for (auto subscript : subscripts) {
|
|
|
|
ss << sep << subscript;
|
|
|
|
sep = ',';
|
|
|
|
}
|
|
|
|
ss << ')';
|
|
|
|
}
|
2019-06-13 03:38:04 +08:00
|
|
|
if (substringStart) {
|
|
|
|
ss << '(' << *substringStart << ":)";
|
|
|
|
}
|
2019-06-12 09:26:48 +08:00
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2018-05-15 04:51:13 +08:00
|
|
|
Scope &Scope::MakeScope(Kind kind, Symbol *symbol) {
|
2021-03-17 05:32:45 +08:00
|
|
|
return children_.emplace_back(*this, kind, symbol, context_);
|
2018-03-23 08:08:20 +08:00
|
|
|
}
|
|
|
|
|
2020-04-23 06:39:24 +08:00
|
|
|
template <typename T>
|
|
|
|
static std::vector<common::Reference<T>> GetSortedSymbols(
|
2020-05-07 03:20:07 +08:00
|
|
|
std::map<SourceName, MutableSymbolRef> symbols) {
|
2020-04-23 06:39:24 +08:00
|
|
|
std::vector<common::Reference<T>> result;
|
|
|
|
result.reserve(symbols.size());
|
|
|
|
for (auto &pair : symbols) {
|
|
|
|
result.push_back(*pair.second);
|
|
|
|
}
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-07 03:20:07 +08:00
|
|
|
MutableSymbolVector Scope::GetSymbols() {
|
2020-04-23 06:39:24 +08:00
|
|
|
return GetSortedSymbols<Symbol>(symbols_);
|
|
|
|
}
|
|
|
|
SymbolVector Scope::GetSymbols() const {
|
|
|
|
return GetSortedSymbols<const Symbol>(symbols_);
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:21:19 +08:00
|
|
|
Scope::iterator Scope::find(const SourceName &name) {
|
2018-11-17 04:43:08 +08:00
|
|
|
return symbols_.find(name);
|
2018-06-22 23:21:19 +08:00
|
|
|
}
|
|
|
|
Scope::size_type Scope::erase(const SourceName &name) {
|
2018-07-17 22:02:30 +08:00
|
|
|
auto it{symbols_.find(name)};
|
2018-06-22 23:21:19 +08:00
|
|
|
if (it != end()) {
|
|
|
|
symbols_.erase(it);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 04:43:08 +08:00
|
|
|
Symbol *Scope::FindSymbol(const SourceName &name) const {
|
2019-02-13 09:24:43 +08:00
|
|
|
auto it{find(name)};
|
2018-08-23 07:05:06 +08:00
|
|
|
if (it != end()) {
|
2019-10-23 07:53:29 +08:00
|
|
|
return &*it->second;
|
2018-08-23 07:05:06 +08:00
|
|
|
} else if (CanImport(name)) {
|
|
|
|
return parent_.FindSymbol(name);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 09:26:48 +08:00
|
|
|
|
2019-11-16 06:26:10 +08:00
|
|
|
Symbol *Scope::FindComponent(SourceName name) const {
|
|
|
|
CHECK(IsDerivedType());
|
|
|
|
auto found{find(name)};
|
|
|
|
if (found != end()) {
|
|
|
|
return &*found->second;
|
|
|
|
} else if (const Scope * parent{GetDerivedTypeParent()}) {
|
|
|
|
return parent->FindComponent(name);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 06:31:02 +08:00
|
|
|
bool Scope::Contains(const Scope &that) const {
|
|
|
|
for (const Scope *scope{&that};; scope = &scope->parent()) {
|
|
|
|
if (*scope == *this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (scope->IsGlobal()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 05:28:08 +08:00
|
|
|
Symbol *Scope::CopySymbol(const Symbol &symbol) {
|
|
|
|
auto pair{try_emplace(symbol.name(), symbol.attrs())};
|
|
|
|
if (!pair.second) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return nullptr; // already exists
|
2020-03-18 05:28:08 +08:00
|
|
|
} else {
|
|
|
|
Symbol &result{*pair.first->second};
|
|
|
|
result.flags() = symbol.flags();
|
|
|
|
result.set_details(common::Clone(symbol.details()));
|
|
|
|
return &result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 09:26:48 +08:00
|
|
|
void Scope::add_equivalenceSet(EquivalenceSet &&set) {
|
|
|
|
equivalenceSets_.emplace_back(std::move(set));
|
|
|
|
}
|
|
|
|
|
2019-09-05 07:55:08 +08:00
|
|
|
void Scope::add_crayPointer(const SourceName &name, Symbol &pointer) {
|
|
|
|
CHECK(pointer.test(Symbol::Flag::CrayPointer));
|
2019-10-23 07:53:29 +08:00
|
|
|
crayPointers_.emplace(name, pointer);
|
2019-09-05 07:55:08 +08:00
|
|
|
}
|
|
|
|
|
2019-02-19 03:39:46 +08:00
|
|
|
Symbol &Scope::MakeCommonBlock(const SourceName &name) {
|
|
|
|
const auto it{commonBlocks_.find(name)};
|
|
|
|
if (it != commonBlocks_.end()) {
|
|
|
|
return *it->second;
|
|
|
|
} else {
|
|
|
|
Symbol &symbol{MakeSymbol(name, Attrs{}, CommonBlockDetails{})};
|
2019-10-23 07:53:29 +08:00
|
|
|
commonBlocks_.emplace(name, symbol);
|
2019-02-19 03:39:46 +08:00
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 05:38:01 +08:00
|
|
|
Symbol *Scope::FindCommonBlock(const SourceName &name) const {
|
2019-02-19 03:39:46 +08:00
|
|
|
const auto it{commonBlocks_.find(name)};
|
2019-10-23 07:53:29 +08:00
|
|
|
return it != commonBlocks_.end() ? &*it->second : nullptr;
|
2019-02-19 03:39:46 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 07:21:27 +08:00
|
|
|
Scope *Scope::FindSubmodule(const SourceName &name) const {
|
|
|
|
auto it{submodules_.find(name)};
|
|
|
|
if (it == submodules_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
2019-10-23 07:53:29 +08:00
|
|
|
return &*it->second;
|
2018-08-03 07:21:27 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 02:24:12 +08:00
|
|
|
bool Scope::AddSubmodule(const SourceName &name, Scope &submodule) {
|
2019-10-23 07:53:29 +08:00
|
|
|
return submodules_.emplace(name, submodule).second;
|
2018-08-03 07:21:27 +08:00
|
|
|
}
|
2018-12-18 04:41:43 +08:00
|
|
|
|
2019-07-11 09:20:27 +08:00
|
|
|
const DeclTypeSpec *Scope::FindType(const DeclTypeSpec &type) const {
|
|
|
|
auto it{std::find(declTypeSpecs_.begin(), declTypeSpecs_.end(), type)};
|
|
|
|
return it != declTypeSpecs_.end() ? &*it : nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-18 05:52:10 +08:00
|
|
|
const DeclTypeSpec &Scope::MakeNumericType(
|
|
|
|
TypeCategory category, KindExpr &&kind) {
|
2018-12-05 02:55:32 +08:00
|
|
|
return MakeLengthlessType(NumericTypeSpec{category, std::move(kind)});
|
2018-12-18 04:41:43 +08:00
|
|
|
}
|
2018-12-05 02:55:32 +08:00
|
|
|
const DeclTypeSpec &Scope::MakeLogicalType(KindExpr &&kind) {
|
|
|
|
return MakeLengthlessType(LogicalTypeSpec{std::move(kind)});
|
2018-12-18 04:41:43 +08:00
|
|
|
}
|
2019-01-16 08:59:20 +08:00
|
|
|
const DeclTypeSpec &Scope::MakeTypeStarType() {
|
2018-12-18 04:41:43 +08:00
|
|
|
return MakeLengthlessType(DeclTypeSpec{DeclTypeSpec::TypeStar});
|
|
|
|
}
|
2019-01-16 08:59:20 +08:00
|
|
|
const DeclTypeSpec &Scope::MakeClassStarType() {
|
2018-12-18 04:41:43 +08:00
|
|
|
return MakeLengthlessType(DeclTypeSpec{DeclTypeSpec::ClassStar});
|
|
|
|
}
|
|
|
|
// Types that can't have length parameters can be reused without having to
|
|
|
|
// compare length expressions. They are stored in the global scope.
|
2018-12-05 02:55:32 +08:00
|
|
|
const DeclTypeSpec &Scope::MakeLengthlessType(DeclTypeSpec &&type) {
|
2019-07-11 09:20:27 +08:00
|
|
|
const auto *found{FindType(type)};
|
|
|
|
return found ? *found : declTypeSpecs_.emplace_back(std::move(type));
|
2018-12-12 06:51:08 +08:00
|
|
|
}
|
2018-12-18 04:41:43 +08:00
|
|
|
|
2019-01-18 05:52:10 +08:00
|
|
|
const DeclTypeSpec &Scope::MakeCharacterType(
|
|
|
|
ParamValue &&length, KindExpr &&kind) {
|
2018-12-05 02:55:32 +08:00
|
|
|
return declTypeSpecs_.emplace_back(
|
|
|
|
CharacterTypeSpec{std::move(length), std::move(kind)});
|
2018-12-15 06:04:15 +08:00
|
|
|
}
|
|
|
|
|
2018-12-05 02:55:32 +08:00
|
|
|
DeclTypeSpec &Scope::MakeDerivedType(
|
2019-09-12 09:21:07 +08:00
|
|
|
DeclTypeSpec::Category category, DerivedTypeSpec &&spec) {
|
|
|
|
return declTypeSpecs_.emplace_back(category, std::move(spec));
|
2018-12-05 02:55:32 +08:00
|
|
|
}
|
|
|
|
|
2021-01-30 05:34:22 +08:00
|
|
|
const DeclTypeSpec *Scope::GetType(const SomeExpr &expr) {
|
|
|
|
if (auto dyType{expr.GetType()}) {
|
|
|
|
if (dyType->IsAssumedType()) {
|
|
|
|
return &MakeTypeStarType();
|
|
|
|
} else if (dyType->IsUnlimitedPolymorphic()) {
|
|
|
|
return &MakeClassStarType();
|
|
|
|
} else {
|
|
|
|
switch (dyType->category()) {
|
|
|
|
case TypeCategory::Integer:
|
|
|
|
case TypeCategory::Real:
|
|
|
|
case TypeCategory::Complex:
|
|
|
|
return &MakeNumericType(dyType->category(), KindExpr{dyType->kind()});
|
|
|
|
case TypeCategory::Character:
|
|
|
|
if (const ParamValue * lenParam{dyType->charLength()}) {
|
|
|
|
return &MakeCharacterType(
|
|
|
|
ParamValue{*lenParam}, KindExpr{dyType->kind()});
|
|
|
|
} else {
|
|
|
|
auto lenExpr{dyType->GetCharLength()};
|
|
|
|
if (!lenExpr) {
|
|
|
|
lenExpr =
|
|
|
|
std::get<evaluate::Expr<evaluate::SomeCharacter>>(expr.u).LEN();
|
|
|
|
}
|
|
|
|
if (lenExpr) {
|
|
|
|
return &MakeCharacterType(
|
|
|
|
ParamValue{SomeIntExpr{std::move(*lenExpr)},
|
|
|
|
common::TypeParamAttr::Len},
|
|
|
|
KindExpr{dyType->kind()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TypeCategory::Logical:
|
|
|
|
return &MakeLogicalType(KindExpr{dyType->kind()});
|
|
|
|
case TypeCategory::Derived:
|
|
|
|
return &MakeDerivedType(dyType->IsPolymorphic()
|
|
|
|
? DeclTypeSpec::ClassDerived
|
|
|
|
: DeclTypeSpec::TypeDerived,
|
|
|
|
DerivedTypeSpec{dyType->GetDerivedTypeSpec()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-24 02:45:49 +08:00
|
|
|
Scope::ImportKind Scope::GetImportKind() const {
|
2018-08-23 07:05:06 +08:00
|
|
|
if (importKind_) {
|
|
|
|
return *importKind_;
|
|
|
|
}
|
2019-02-27 06:52:39 +08:00
|
|
|
if (symbol_ && !symbol_->attrs().test(Attr::MODULE)) {
|
2018-08-23 07:05:06 +08:00
|
|
|
if (auto *details{symbol_->detailsIf<SubprogramDetails>()}) {
|
|
|
|
if (details->isInterface()) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return ImportKind::None; // default for non-mod-proc interface body
|
2018-08-23 07:05:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ImportKind::Default;
|
|
|
|
}
|
|
|
|
|
2018-08-24 02:24:12 +08:00
|
|
|
std::optional<parser::MessageFixedText> Scope::SetImportKind(ImportKind kind) {
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!importKind_) {
|
2018-08-23 07:05:06 +08:00
|
|
|
importKind_ = kind;
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
bool hasNone{kind == ImportKind::None || *importKind_ == ImportKind::None};
|
|
|
|
bool hasAll{kind == ImportKind::All || *importKind_ == ImportKind::All};
|
2018-08-24 02:24:12 +08:00
|
|
|
// Check C8100 and C898: constraints on multiple IMPORT statements
|
2018-08-23 07:05:06 +08:00
|
|
|
if (hasNone || hasAll) {
|
|
|
|
return hasNone
|
|
|
|
? "IMPORT,NONE must be the only IMPORT statement in a scope"_err_en_US
|
|
|
|
: "IMPORT,ALL must be the only IMPORT statement in a scope"_err_en_US;
|
|
|
|
} else if (kind != *importKind_ &&
|
|
|
|
(kind != ImportKind::Only || kind != ImportKind::Only)) {
|
|
|
|
return "Every IMPORT must have ONLY specifier if one of them does"_err_en_US;
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 04:43:08 +08:00
|
|
|
void Scope::add_importName(const SourceName &name) {
|
2018-08-23 07:05:06 +08:00
|
|
|
importNames_.insert(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// true if name can be imported or host-associated from parent scope.
|
|
|
|
bool Scope::CanImport(const SourceName &name) const {
|
2019-08-09 06:06:51 +08:00
|
|
|
if (IsGlobal() || parent_.IsGlobal()) {
|
2018-08-23 07:05:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-24 02:45:49 +08:00
|
|
|
switch (GetImportKind()) {
|
2019-10-03 06:48:20 +08:00
|
|
|
SWITCH_COVERS_ALL_CASES
|
2020-03-29 12:00:16 +08:00
|
|
|
case ImportKind::None:
|
|
|
|
return false;
|
2018-08-23 07:05:06 +08:00
|
|
|
case ImportKind::All:
|
2020-03-29 12:00:16 +08:00
|
|
|
case ImportKind::Default:
|
|
|
|
return true;
|
|
|
|
case ImportKind::Only:
|
|
|
|
return importNames_.count(name) > 0;
|
2018-08-23 07:05:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 01:58:26 +08:00
|
|
|
const Scope *Scope::FindScope(parser::CharBlock source) const {
|
2019-04-03 02:56:19 +08:00
|
|
|
return const_cast<Scope *>(this)->FindScope(source);
|
2019-04-03 01:58:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Scope *Scope::FindScope(parser::CharBlock source) {
|
2019-06-01 07:37:00 +08:00
|
|
|
bool isContained{sourceRange_.Contains(source)};
|
2019-07-12 00:27:25 +08:00
|
|
|
if (!isContained && !IsGlobal() && !IsModuleFile()) {
|
2018-11-29 07:55:55 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-04-03 01:58:26 +08:00
|
|
|
for (auto &child : children_) {
|
|
|
|
if (auto *scope{child.FindScope(source)}) {
|
2018-11-29 07:55:55 +08:00
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
}
|
2019-06-01 07:37:00 +08:00
|
|
|
return isContained ? this : nullptr;
|
2018-11-29 07:55:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scope::AddSourceRange(const parser::CharBlock &source) {
|
2020-03-11 06:31:02 +08:00
|
|
|
for (auto *scope = this; !scope->IsGlobal(); scope = &scope->parent()) {
|
[flang] [OpenMP] Name Resolution for OpenMP constructs (flang-compiler/f18#940)
This is an extended framework based on the previous work that addresses
the NR on OpenMP directives/clauses (b2ea520). In this change:
* New `OmpVisitor` is created (ResolveNamesVisitor derives from it) to
create necessary scopes for certain OpenMP constructs. This is along
with the regular Fortran NR process.
* Old `OmpVisitor` is adjusted and converted to a standalone visitor--
`OmpAttributeVisitor`. This is used to walk through the OpenMP constructs
and do the NR for variables on the OpenMP directives or data references
within the OpenMP constructs. "Do the NR" here means that based on the NR
results of the regular Fortran NR, fix the symbols of `Names` related
to the OpenMP constructs. Note that there is an `OmpContext` in this
visitor (similar to the one in `OmpStructureChecker`), this is necessary
when dealing with the nested OpenMP constructs in the future.
Given an OpenMP code:
```
real*8 a, b
a = 1.
b = 2.
!$omp parallel private(a)
a = 3.
b = 4.
!$omp end parallel
print *, a, b
end
```
w/o -fopenmp:
```
real*8 a, b
!REF: /MainProgram1/a
a = 1.
!REF: /MainProgram1/b
b = 2.
!!!! OMP parallel
!REF: /MainProgram1/a
a = 3.
!REF: /MainProgram1/b
b = 4.
!!!! OMP end parallel
!REF: /MainProgram1/a
!REF: /MainProgram1/b
print *, a, b
end
```
w/ -fopenmp:
```
real*8 a, b
!REF: /MainProgram1/a
a = 1.
!REF: /MainProgram1/b
b = 2.
!$omp parallel private(a) <-- new Symbol for 'a' created
!DEF: /MainProgram1/Block1/a (OmpPrivate) HostAssoc REAL(8)
a = 3. <-- fix the old symbol with new Symbol in parallel scope
!REF: /MainProgram1/b
b = 4. <-- do nothing because by default it is shared in this scope
!$omp end parallel
!REF: /MainProgram1/a
!REF: /MainProgram1/b
print *, a, b
end
```
Please note that this is a framework update, there are still many
things on the TODO list for finishing the NR for OpenMP (based on
the `OpenMP-semantics.md` design doc), which will be on top of this
framework.
Some TODO items:
- Create a generic function to go through all the rules for deciding
`predetermined`, `explicitly determined`, and `implicitly determined`
data-sharing attributes. (This is the next biggest part)
- Handle `Array Sections` and `Array or Structure Element`.
- Take association into consideration for example Pointer association,
`ASSOCIATE` construct, and etc.
- Handle all the name resolution for directives/clauses that have
`parser::Name`.
* N.B. Extend `AddSourceRange` to apply to current and parent scopes
- motivated by a few cases that need to call `AddSourceRange`
for current & parent scopes; the extension should be safe
- global scope is not included
Original-commit: flang-compiler/f18@0c3c39d30e3f166a6a1303337c5fd7eead720fd0
Reviewed-on: https://github.com/flang-compiler/f18/pull/940
2020-01-29 04:51:35 +08:00
|
|
|
scope->sourceRange_.ExtendToCover(source);
|
|
|
|
}
|
2018-11-29 07:55:55 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Scope &scope) {
|
2018-05-04 06:57:56 +08:00
|
|
|
os << Scope::EnumToString(scope.kind()) << " scope: ";
|
2018-07-17 22:02:30 +08:00
|
|
|
if (auto *symbol{scope.symbol()}) {
|
2018-05-04 06:57:56 +08:00
|
|
|
os << *symbol << ' ';
|
|
|
|
}
|
2020-04-23 06:39:24 +08:00
|
|
|
if (scope.derivedTypeSpec_) {
|
|
|
|
os << "instantiation of " << *scope.derivedTypeSpec_ << ' ';
|
|
|
|
}
|
2018-05-04 06:57:56 +08:00
|
|
|
os << scope.children_.size() << " children\n";
|
2018-06-22 23:21:19 +08:00
|
|
|
for (const auto &pair : scope.symbols_) {
|
2019-10-23 07:53:29 +08:00
|
|
|
const Symbol &symbol{*pair.second};
|
|
|
|
os << " " << symbol << '\n';
|
2018-03-23 08:08:20 +08:00
|
|
|
}
|
2019-06-12 09:26:48 +08:00
|
|
|
if (!scope.equivalenceSets_.empty()) {
|
|
|
|
os << " Equivalence Sets:\n";
|
|
|
|
for (const auto &set : scope.equivalenceSets_) {
|
|
|
|
os << " ";
|
|
|
|
for (const auto &object : set) {
|
|
|
|
os << ' ' << object.AsFortran();
|
|
|
|
}
|
|
|
|
os << '\n';
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 03:39:46 +08:00
|
|
|
for (const auto &pair : scope.commonBlocks_) {
|
2019-10-23 07:53:29 +08:00
|
|
|
const Symbol &symbol{*pair.second};
|
|
|
|
os << " " << symbol << '\n';
|
2019-02-19 03:39:46 +08:00
|
|
|
}
|
2018-03-23 08:08:20 +08:00
|
|
|
return os;
|
|
|
|
}
|
2018-12-05 02:55:32 +08:00
|
|
|
|
2020-07-27 03:13:36 +08:00
|
|
|
bool Scope::IsStmtFunction() const {
|
|
|
|
return symbol_ && symbol_->test(Symbol::Flag::StmtFunction);
|
|
|
|
}
|
|
|
|
|
2018-12-05 02:55:32 +08:00
|
|
|
bool Scope::IsParameterizedDerivedType() const {
|
2019-07-12 00:27:25 +08:00
|
|
|
if (!IsDerivedType()) {
|
2018-12-05 02:55:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (const Scope * parent{GetDerivedTypeParent()}) {
|
|
|
|
if (parent->IsParameterizedDerivedType()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto &pair : symbols_) {
|
|
|
|
if (pair.second->has<TypeParamDetails>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeclTypeSpec *Scope::FindInstantiatedDerivedType(
|
|
|
|
const DerivedTypeSpec &spec, DeclTypeSpec::Category category) const {
|
|
|
|
DeclTypeSpec type{category, spec};
|
2019-07-11 09:20:27 +08:00
|
|
|
if (const auto *result{FindType(type)}) {
|
|
|
|
return result;
|
2019-07-12 00:27:25 +08:00
|
|
|
} else if (IsGlobal()) {
|
2019-02-20 07:38:55 +08:00
|
|
|
return nullptr;
|
2019-07-11 09:20:27 +08:00
|
|
|
} else {
|
|
|
|
return parent().FindInstantiatedDerivedType(spec, category);
|
2018-12-05 02:55:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Scope *Scope::GetDerivedTypeParent() const {
|
|
|
|
if (const Symbol * symbol{GetSymbol()}) {
|
|
|
|
if (const DerivedTypeSpec * parent{symbol->GetParentTypeSpec(this)}) {
|
|
|
|
return parent->scope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-23 00:15:02 +08:00
|
|
|
|
2020-03-31 08:42:50 +08:00
|
|
|
const Scope &Scope::GetDerivedTypeBase() const {
|
|
|
|
const Scope *child{this};
|
|
|
|
for (const Scope *parent{GetDerivedTypeParent()}; parent != nullptr;
|
|
|
|
parent = child->GetDerivedTypeParent()) {
|
|
|
|
child = parent;
|
|
|
|
}
|
|
|
|
return *child;
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:32:45 +08:00
|
|
|
void Scope::InstantiateDerivedTypes() {
|
2019-11-23 00:15:02 +08:00
|
|
|
for (DeclTypeSpec &type : declTypeSpecs_) {
|
|
|
|
if (type.category() == DeclTypeSpec::TypeDerived ||
|
|
|
|
type.category() == DeclTypeSpec::ClassDerived) {
|
2021-03-17 05:32:45 +08:00
|
|
|
type.derivedTypeSpec().Instantiate(*this, context_);
|
2019-11-23 00:15:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::semantics
|