2018-05-01 12:50:34 -07:00
|
|
|
// 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.
|
|
|
|
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "scope.h"
|
|
|
|
#include "symbol.h"
|
2018-12-11 14:51:08 -08:00
|
|
|
#include <algorithm>
|
2018-03-22 17:08:20 -07:00
|
|
|
#include <memory>
|
|
|
|
|
2018-03-23 12:24:29 -07:00
|
|
|
namespace Fortran::semantics {
|
2018-03-22 17:08:20 -07:00
|
|
|
|
2018-06-19 16:06:41 -07:00
|
|
|
Symbols<1024> Scope::allSymbols;
|
|
|
|
|
2018-09-07 09:06:27 -07:00
|
|
|
bool Scope::IsModule() const {
|
|
|
|
return kind_ == Kind::Module && !symbol_->get<ModuleDetails>().isSubmodule();
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:51:13 -07:00
|
|
|
Scope &Scope::MakeScope(Kind kind, Symbol *symbol) {
|
2018-04-18 15:06:35 -07:00
|
|
|
children_.emplace_back(*this, kind, symbol);
|
2018-03-22 17:08:20 -07:00
|
|
|
return children_.back();
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:21:19 -07:00
|
|
|
Scope::iterator Scope::find(const SourceName &name) {
|
2018-11-16 12:43:08 -08:00
|
|
|
return symbols_.find(name);
|
2018-06-22 08:21:19 -07:00
|
|
|
}
|
|
|
|
Scope::const_iterator Scope::find(const SourceName &name) const {
|
|
|
|
return symbols_.find(name);
|
|
|
|
}
|
|
|
|
Scope::size_type Scope::erase(const SourceName &name) {
|
2018-07-17 07:02:30 -07:00
|
|
|
auto it{symbols_.find(name)};
|
2018-06-22 08:21:19 -07:00
|
|
|
if (it != end()) {
|
|
|
|
symbols_.erase(it);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:43:08 -08:00
|
|
|
Symbol *Scope::FindSymbol(const SourceName &name) const {
|
2018-08-27 11:48:49 -07:00
|
|
|
if (kind() == Kind::DerivedType) {
|
|
|
|
return parent_.FindSymbol(name);
|
|
|
|
}
|
2018-08-22 16:05:06 -07:00
|
|
|
const auto it{find(name)};
|
|
|
|
if (it != end()) {
|
|
|
|
return it->second;
|
|
|
|
} else if (CanImport(name)) {
|
|
|
|
return parent_.FindSymbol(name);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 16:21:27 -07:00
|
|
|
Scope *Scope::FindSubmodule(const SourceName &name) const {
|
|
|
|
auto it{submodules_.find(name)};
|
|
|
|
if (it == submodules_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 11:24:12 -07:00
|
|
|
bool Scope::AddSubmodule(const SourceName &name, Scope &submodule) {
|
|
|
|
return submodules_.emplace(name, &submodule).second;
|
2018-08-02 16:21:27 -07:00
|
|
|
}
|
2018-12-11 14:51:08 -08:00
|
|
|
DeclTypeSpec &Scope::MakeDeclTypeSpec(TypeCategory category, int kind) {
|
2018-12-14 14:04:15 -08:00
|
|
|
CHECK(category != TypeCategory::Character);
|
2018-12-11 14:51:08 -08:00
|
|
|
DeclTypeSpec type{IntrinsicTypeSpec{category, kind}};
|
|
|
|
auto it{std::find(declTypeSpecs_.begin(), declTypeSpecs_.end(), type)};
|
|
|
|
if (it != declTypeSpecs_.end()) {
|
|
|
|
return *it;
|
|
|
|
} else {
|
|
|
|
declTypeSpecs_.push_back(type);
|
|
|
|
return declTypeSpecs_.back();
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 14:04:15 -08:00
|
|
|
DeclTypeSpec &Scope::MakeDeclTypeSpec(ParamValue &&length, int kind) {
|
|
|
|
characterTypeSpecs_.emplace_back(std::move(length), kind);
|
|
|
|
declTypeSpecs_.emplace_back(characterTypeSpecs_.back());
|
|
|
|
return declTypeSpecs_.back();
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:51:08 -08:00
|
|
|
DeclTypeSpec &Scope::MakeDeclTypeSpec(
|
|
|
|
DeclTypeSpec::Category category, const SourceName &name) {
|
|
|
|
CHECK(category == DeclTypeSpec::TypeDerived ||
|
|
|
|
category == DeclTypeSpec::ClassDerived);
|
|
|
|
derivedTypeSpecs_.emplace_back(name);
|
|
|
|
declTypeSpecs_.emplace_back(category, derivedTypeSpecs_.back());
|
|
|
|
return declTypeSpecs_.back();
|
|
|
|
}
|
|
|
|
DeclTypeSpec &Scope::MakeDeclTypeSpec(DeclTypeSpec::Category category) {
|
|
|
|
CHECK(category == DeclTypeSpec::TypeStar ||
|
|
|
|
category == DeclTypeSpec::ClassStar);
|
|
|
|
declTypeSpecs_.emplace_back(category);
|
|
|
|
return declTypeSpecs_.back();
|
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
DerivedTypeSpec &Scope::MakeDerivedTypeSpec(const SourceName &name) {
|
|
|
|
derivedTypeSpecs_.emplace_back(name);
|
|
|
|
return derivedTypeSpecs_.back();
|
|
|
|
}
|
|
|
|
|
2018-08-23 11:45:49 -07:00
|
|
|
Scope::ImportKind Scope::GetImportKind() const {
|
2018-08-22 16:05:06 -07:00
|
|
|
if (importKind_) {
|
|
|
|
return *importKind_;
|
|
|
|
}
|
|
|
|
if (symbol_) {
|
|
|
|
if (auto *details{symbol_->detailsIf<SubprogramDetails>()}) {
|
|
|
|
if (details->isInterface()) {
|
|
|
|
return ImportKind::None; // default for interface body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ImportKind::Default;
|
|
|
|
}
|
|
|
|
|
2018-08-23 11:24:12 -07:00
|
|
|
std::optional<parser::MessageFixedText> Scope::SetImportKind(ImportKind kind) {
|
2018-08-22 16:05:06 -07:00
|
|
|
if (!importKind_.has_value()) {
|
|
|
|
importKind_ = kind;
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
bool hasNone{kind == ImportKind::None || *importKind_ == ImportKind::None};
|
|
|
|
bool hasAll{kind == ImportKind::All || *importKind_ == ImportKind::All};
|
2018-08-23 11:24:12 -07:00
|
|
|
// Check C8100 and C898: constraints on multiple IMPORT statements
|
2018-08-22 16:05:06 -07: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-16 12:43:08 -08:00
|
|
|
void Scope::add_importName(const SourceName &name) {
|
2018-08-22 16:05:06 -07:00
|
|
|
importNames_.insert(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// true if name can be imported or host-associated from parent scope.
|
|
|
|
bool Scope::CanImport(const SourceName &name) const {
|
|
|
|
if (kind_ == Kind::Global) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-23 11:45:49 -07:00
|
|
|
switch (GetImportKind()) {
|
2018-08-22 16:05:06 -07:00
|
|
|
case ImportKind::None: return false;
|
|
|
|
case ImportKind::All:
|
|
|
|
case ImportKind::Default: return true;
|
|
|
|
case ImportKind::Only: return importNames_.count(name) > 0;
|
|
|
|
default: CRASH_NO_CASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 15:55:55 -08:00
|
|
|
const Scope *Scope::FindScope(const parser::CharBlock &source) const {
|
|
|
|
if (!sourceRange_.Contains(source)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (const auto &child : children_) {
|
|
|
|
if (const auto *scope{child.FindScope(source)}) {
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scope::AddSourceRange(const parser::CharBlock &source) {
|
2018-11-29 09:10:19 -08:00
|
|
|
sourceRange_.ExtendToCover(source);
|
2018-11-28 15:55:55 -08:00
|
|
|
}
|
|
|
|
|
2018-03-22 17:08:20 -07:00
|
|
|
std::ostream &operator<<(std::ostream &os, const Scope &scope) {
|
2018-05-03 15:57:56 -07:00
|
|
|
os << Scope::EnumToString(scope.kind()) << " scope: ";
|
2018-07-17 07:02:30 -07:00
|
|
|
if (auto *symbol{scope.symbol()}) {
|
2018-05-03 15:57:56 -07:00
|
|
|
os << *symbol << ' ';
|
|
|
|
}
|
|
|
|
os << scope.children_.size() << " children\n";
|
2018-06-22 08:21:19 -07:00
|
|
|
for (const auto &pair : scope.symbols_) {
|
2018-07-25 06:55:11 -07:00
|
|
|
const auto *symbol{pair.second};
|
|
|
|
os << " " << *symbol << '\n';
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
2018-10-25 05:55:23 -07:00
|
|
|
}
|