2019-01-07 15:42:36 -08:00
|
|
|
// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-05-01 12:50:34 -07:00
|
|
|
//
|
|
|
|
// 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"
|
2019-01-31 09:58:40 -08:00
|
|
|
#include "semantics.h"
|
2018-03-22 17:08:20 -07:00
|
|
|
#include "symbol.h"
|
2018-12-04 10:55:32 -08:00
|
|
|
#include "type.h"
|
|
|
|
#include "../evaluate/fold.h"
|
|
|
|
#include "../parser/characters.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-12-04 10:55:32 -08:00
|
|
|
return children_.emplace_back(*this, kind, symbol);
|
2018-03-22 17:08:20 -07:00
|
|
|
}
|
|
|
|
|
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::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-17 12:41:43 -08:00
|
|
|
|
2019-01-17 13:52:10 -08:00
|
|
|
const DeclTypeSpec &Scope::MakeNumericType(
|
|
|
|
TypeCategory category, KindExpr &&kind) {
|
2018-12-04 10:55:32 -08:00
|
|
|
return MakeLengthlessType(NumericTypeSpec{category, std::move(kind)});
|
2018-12-17 12:41:43 -08:00
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
const DeclTypeSpec &Scope::MakeLogicalType(KindExpr &&kind) {
|
|
|
|
return MakeLengthlessType(LogicalTypeSpec{std::move(kind)});
|
2018-12-17 12:41:43 -08:00
|
|
|
}
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec &Scope::MakeTypeStarType() {
|
2018-12-17 12:41:43 -08:00
|
|
|
return MakeLengthlessType(DeclTypeSpec{DeclTypeSpec::TypeStar});
|
|
|
|
}
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec &Scope::MakeClassStarType() {
|
2018-12-17 12: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-04 10:55:32 -08:00
|
|
|
const DeclTypeSpec &Scope::MakeLengthlessType(DeclTypeSpec &&type) {
|
2018-12-11 14:51:08 -08:00
|
|
|
auto it{std::find(declTypeSpecs_.begin(), declTypeSpecs_.end(), type)};
|
|
|
|
if (it != declTypeSpecs_.end()) {
|
|
|
|
return *it;
|
|
|
|
} else {
|
2018-12-04 10:55:32 -08:00
|
|
|
return declTypeSpecs_.emplace_back(std::move(type));
|
2018-12-11 14:51:08 -08:00
|
|
|
}
|
|
|
|
}
|
2018-12-17 12:41:43 -08:00
|
|
|
|
2019-01-17 13:52:10 -08:00
|
|
|
const DeclTypeSpec &Scope::MakeCharacterType(
|
|
|
|
ParamValue &&length, KindExpr &&kind) {
|
2018-12-04 10:55:32 -08:00
|
|
|
return declTypeSpecs_.emplace_back(
|
|
|
|
CharacterTypeSpec{std::move(length), std::move(kind)});
|
2018-12-14 14:04:15 -08:00
|
|
|
}
|
|
|
|
|
2019-01-15 16:59:20 -08:00
|
|
|
const DeclTypeSpec &Scope::MakeDerivedType(
|
2019-01-17 13:52:10 -08:00
|
|
|
DeclTypeSpec::Category category, DerivedTypeSpec &&spec) {
|
|
|
|
return MakeDerivedType(std::move(spec), category);
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeclTypeSpec &Scope::MakeDerivedType(DeclTypeSpec::Category category,
|
2019-01-31 09:58:40 -08:00
|
|
|
DerivedTypeSpec &&instance, SemanticsContext &semanticsContext) {
|
2019-01-17 13:52:10 -08:00
|
|
|
DeclTypeSpec &type{declTypeSpecs_.emplace_back(
|
|
|
|
category, DerivedTypeSpec{std::move(instance)})};
|
2019-01-31 09:58:40 -08:00
|
|
|
type.derivedTypeSpec().Instantiate(*this, semanticsContext);
|
2019-01-17 13:52:10 -08:00
|
|
|
return type;
|
2018-12-11 14:51:08 -08:00
|
|
|
}
|
2018-06-22 08:21:19 -07:00
|
|
|
|
2018-12-04 10:55:32 -08:00
|
|
|
DeclTypeSpec &Scope::MakeDerivedType(const Symbol &typeSymbol) {
|
|
|
|
CHECK(typeSymbol.has<DerivedTypeDetails>());
|
|
|
|
CHECK(typeSymbol.scope() != nullptr);
|
|
|
|
return MakeDerivedType(
|
|
|
|
DerivedTypeSpec{typeSymbol}, DeclTypeSpec::TypeDerived);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclTypeSpec &Scope::MakeDerivedType(
|
2019-01-17 13:52:10 -08:00
|
|
|
DerivedTypeSpec &&spec, DeclTypeSpec::Category category) {
|
2018-12-04 10:55:32 -08:00
|
|
|
return declTypeSpecs_.emplace_back(
|
2019-01-17 13:52:10 -08:00
|
|
|
category, DerivedTypeSpec{std::move(spec)});
|
2018-12-04 10:55:32 -08:00
|
|
|
}
|
|
|
|
|
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-12-04 10:55:32 -08:00
|
|
|
|
|
|
|
bool Scope::IsParameterizedDerivedType() const {
|
|
|
|
if (kind_ != Kind::DerivedType) {
|
|
|
|
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};
|
|
|
|
auto typeIter{std::find(declTypeSpecs_.begin(), declTypeSpecs_.end(), type)};
|
|
|
|
if (typeIter != declTypeSpecs_.end()) {
|
|
|
|
return &*typeIter;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeclTypeSpec &Scope::FindOrInstantiateDerivedType(DerivedTypeSpec &&spec,
|
2019-01-31 09:58:40 -08:00
|
|
|
DeclTypeSpec::Category category, SemanticsContext &semanticsContext) {
|
|
|
|
spec.FoldParameterExpressions(semanticsContext.foldingContext());
|
2018-12-04 10:55:32 -08:00
|
|
|
if (const DeclTypeSpec * type{FindInstantiatedDerivedType(spec, category)}) {
|
|
|
|
return *type;
|
|
|
|
}
|
|
|
|
// Create a new instantiation of this parameterized derived type
|
|
|
|
// for this particular distinct set of actual parameter values.
|
|
|
|
DeclTypeSpec &type{MakeDerivedType(std::move(spec), category)};
|
2019-01-31 09:58:40 -08:00
|
|
|
type.derivedTypeSpec().Instantiate(*this, semanticsContext);
|
2018-12-04 10:55:32 -08:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scope::InstantiateDerivedType(
|
2019-01-31 09:58:40 -08:00
|
|
|
Scope &clone, SemanticsContext &semanticsContext) const {
|
2019-01-31 11:44:13 -08:00
|
|
|
CHECK(kind_ == Kind::DerivedType);
|
2018-12-04 10:55:32 -08:00
|
|
|
clone.sourceRange_ = sourceRange_;
|
|
|
|
clone.chars_ = chars_;
|
|
|
|
for (const auto &pair : symbols_) {
|
2019-01-31 09:58:40 -08:00
|
|
|
pair.second->Instantiate(clone, semanticsContext);
|
2018-12-04 10:55:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeclTypeSpec &Scope::InstantiateIntrinsicType(
|
2019-01-31 09:58:40 -08:00
|
|
|
const DeclTypeSpec &spec, SemanticsContext &semanticsContext) {
|
2018-12-04 10:55:32 -08:00
|
|
|
const IntrinsicTypeSpec *intrinsic{spec.AsIntrinsic()};
|
|
|
|
CHECK(intrinsic != nullptr);
|
|
|
|
if (evaluate::ToInt64(intrinsic->kind()).has_value()) {
|
|
|
|
return spec; // KIND is already a known constant
|
|
|
|
}
|
|
|
|
// The expression was not originally constant, but now it must be so
|
|
|
|
// in the context of a parameterized derived type instantiation.
|
|
|
|
KindExpr copy{intrinsic->kind()};
|
2019-01-31 09:58:40 -08:00
|
|
|
evaluate::FoldingContext &foldingContext{semanticsContext.foldingContext()};
|
2018-12-04 10:55:32 -08:00
|
|
|
copy = evaluate::Fold(foldingContext, std::move(copy));
|
2019-01-31 09:58:40 -08:00
|
|
|
int kind{
|
|
|
|
semanticsContext.defaultKinds().GetDefaultKind(intrinsic->category())};
|
|
|
|
if (auto value{evaluate::ToInt64(copy)}) {
|
|
|
|
if (evaluate::IsValidKindOfIntrinsicType(intrinsic->category(), *value)) {
|
|
|
|
kind = *value;
|
|
|
|
} else {
|
2019-01-31 16:04:17 -08:00
|
|
|
foldingContext.messages().Say(
|
2019-01-31 11:44:13 -08:00
|
|
|
"KIND parameter value (%jd) of intrinsic type %s "
|
|
|
|
"did not resolve to a supported value"_err_en_US,
|
2019-01-31 09:58:40 -08:00
|
|
|
static_cast<std::intmax_t>(*value),
|
|
|
|
parser::ToUpperCaseLetters(
|
|
|
|
common::EnumToString(intrinsic->category()))
|
|
|
|
.data());
|
|
|
|
}
|
2018-12-04 10:55:32 -08:00
|
|
|
}
|
|
|
|
switch (spec.category()) {
|
|
|
|
case DeclTypeSpec::Numeric:
|
|
|
|
return declTypeSpecs_.emplace_back(
|
2019-01-31 09:58:40 -08:00
|
|
|
NumericTypeSpec{intrinsic->category(), KindExpr{kind}});
|
2018-12-04 10:55:32 -08:00
|
|
|
case DeclTypeSpec::Logical:
|
2019-01-31 09:58:40 -08:00
|
|
|
return declTypeSpecs_.emplace_back(LogicalTypeSpec{KindExpr{kind}});
|
2018-12-04 10:55:32 -08:00
|
|
|
case DeclTypeSpec::Character:
|
|
|
|
return declTypeSpecs_.emplace_back(CharacterTypeSpec{
|
2019-01-31 09:58:40 -08:00
|
|
|
ParamValue{spec.characterTypeSpec().length()}, KindExpr{kind}});
|
2018-12-04 10:55:32 -08:00
|
|
|
default: CRASH_NO_CASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Symbol *Scope::GetSymbol() const {
|
|
|
|
if (symbol_ != nullptr) {
|
|
|
|
return symbol_;
|
|
|
|
}
|
|
|
|
if (derivedTypeSpec_ != nullptr) {
|
|
|
|
return &derivedTypeSpec_->typeSymbol();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Scope *Scope::GetDerivedTypeParent() const {
|
|
|
|
if (const Symbol * symbol{GetSymbol()}) {
|
|
|
|
if (const DerivedTypeSpec * parent{symbol->GetParentTypeSpec(this)}) {
|
|
|
|
return parent->scope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-25 05:55:23 -07:00
|
|
|
}
|