2019-03-04 10:13:12 -08:00
|
|
|
// Copyright (c) 2019, 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.
|
|
|
|
|
2019-03-26 00:33:03 -07:00
|
|
|
#include "tools.h"
|
2019-03-26 18:03:33 -07:00
|
|
|
#include "scope.h"
|
2019-04-12 16:30:03 -07:00
|
|
|
#include "symbol.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "../common/indirection.h"
|
|
|
|
#include "../parser/message.h"
|
|
|
|
#include "../parser/parse-tree.h"
|
2019-03-04 10:13:12 -08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
namespace Fortran::semantics {
|
|
|
|
|
|
|
|
static const Symbol *FindCommonBlockInScope(
|
|
|
|
const Scope &scope, const Symbol &object) {
|
|
|
|
for (const auto &pair : scope.commonBlocks()) {
|
|
|
|
const Symbol &block{*pair.second};
|
|
|
|
if (IsCommonBlockContaining(block, object)) {
|
|
|
|
return █
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Symbol *FindCommonBlockContaining(const Symbol &object) {
|
|
|
|
for (const Scope *scope{&object.owner()};
|
|
|
|
scope->kind() != Scope::Kind::Global; scope = &scope->parent()) {
|
|
|
|
if (const Symbol * block{FindCommonBlockInScope(*scope, object)}) {
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Scope *FindProgramUnitContaining(const Scope &start) {
|
|
|
|
const Scope *scope{&start};
|
|
|
|
while (scope != nullptr) {
|
|
|
|
switch (scope->kind()) {
|
|
|
|
case Scope::Kind::Module:
|
|
|
|
case Scope::Kind::MainProgram:
|
|
|
|
case Scope::Kind::Subprogram: return scope;
|
|
|
|
case Scope::Kind::Global:
|
|
|
|
case Scope::Kind::System: return nullptr;
|
|
|
|
case Scope::Kind::DerivedType:
|
|
|
|
case Scope::Kind::Block:
|
|
|
|
case Scope::Kind::Forall:
|
|
|
|
case Scope::Kind::ImpliedDos: scope = &scope->parent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Scope *FindProgramUnitContaining(const Symbol &symbol) {
|
|
|
|
return FindProgramUnitContaining(symbol.owner());
|
|
|
|
}
|
|
|
|
|
|
|
|
const Scope *FindPureFunctionContaining(const Scope *scope) {
|
|
|
|
scope = FindProgramUnitContaining(*scope);
|
|
|
|
while (scope != nullptr) {
|
|
|
|
if (IsPureFunction(*scope)) {
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
scope = FindProgramUnitContaining(scope->parent());
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCommonBlockContaining(const Symbol &block, const Symbol &object) {
|
|
|
|
const auto &objects{block.get<CommonBlockDetails>().objects()};
|
|
|
|
auto found{std::find(objects.begin(), objects.end(), &object)};
|
|
|
|
return found != objects.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsUseAssociated(const Symbol &symbol, const Scope &scope) {
|
|
|
|
const Scope *owner{FindProgramUnitContaining(symbol.GetUltimate().owner())};
|
|
|
|
return owner != nullptr && owner->kind() == Scope::Kind::Module &&
|
|
|
|
owner != FindProgramUnitContaining(scope);
|
|
|
|
}
|
|
|
|
|
2019-03-26 00:33:03 -07:00
|
|
|
bool DoesScopeContain(
|
|
|
|
const Scope *maybeAncestor, const Scope &maybeDescendent) {
|
2019-03-05 13:11:57 -08:00
|
|
|
if (maybeAncestor != nullptr) {
|
|
|
|
const Scope *scope{&maybeDescendent};
|
|
|
|
while (scope->kind() != Scope::Kind::Global) {
|
|
|
|
scope = &scope->parent();
|
|
|
|
if (scope == maybeAncestor) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:11:57 -08:00
|
|
|
bool DoesScopeContain(const Scope *maybeAncestor, const Symbol &symbol) {
|
|
|
|
return DoesScopeContain(maybeAncestor, symbol.owner());
|
|
|
|
}
|
|
|
|
|
2019-03-04 10:13:12 -08:00
|
|
|
bool IsHostAssociated(const Symbol &symbol, const Scope &scope) {
|
2019-03-05 13:11:57 -08:00
|
|
|
return DoesScopeContain(FindProgramUnitContaining(symbol), scope);
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDummy(const Symbol &symbol) {
|
|
|
|
if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
|
|
|
return details->isDummy();
|
|
|
|
} else if (const auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
|
|
|
|
return details->isDummy();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 11:29:48 -07:00
|
|
|
bool IsPointer(const Symbol &symbol) {
|
|
|
|
return symbol.attrs().test(Attr::POINTER);
|
|
|
|
}
|
|
|
|
|
2019-04-10 22:17:44 -07:00
|
|
|
bool IsAllocatable(const Symbol &symbol) {
|
|
|
|
return symbol.attrs().test(Attr::ALLOCATABLE);
|
|
|
|
}
|
|
|
|
|
2019-03-04 10:13:12 -08:00
|
|
|
bool IsPointerDummy(const Symbol &symbol) {
|
2019-04-07 11:29:48 -07:00
|
|
|
return IsPointer(symbol) && IsDummy(symbol);
|
|
|
|
}
|
|
|
|
|
2019-04-10 22:17:44 -07:00
|
|
|
bool IsAllocatableOrPointer(const Symbol &symbol) {
|
|
|
|
return IsPointer(symbol) || IsAllocatable(symbol);
|
|
|
|
}
|
|
|
|
|
2019-04-07 11:29:48 -07:00
|
|
|
bool IsParameter(const Symbol &symbol) {
|
|
|
|
return symbol.attrs().test(Attr::PARAMETER);
|
|
|
|
}
|
|
|
|
|
|
|
|
// variable-name
|
|
|
|
bool IsVariableName(const Symbol &symbol) {
|
|
|
|
return symbol.has<ObjectEntityDetails>() && !IsParameter(symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
// proc-name
|
|
|
|
bool IsProcName(const Symbol &symbol) {
|
|
|
|
return symbol.has<ProcEntityDetails>();
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFunction(const Symbol &symbol) {
|
|
|
|
if (const auto *procDetails{symbol.detailsIf<ProcEntityDetails>()}) {
|
|
|
|
return procDetails->interface().type() != nullptr ||
|
|
|
|
(procDetails->interface().symbol() != nullptr &&
|
|
|
|
IsFunction(*procDetails->interface().symbol()));
|
|
|
|
} else if (const auto *subprogram{symbol.detailsIf<SubprogramDetails>()}) {
|
|
|
|
return subprogram->isFunction();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPureFunction(const Symbol &symbol) {
|
|
|
|
return symbol.attrs().test(Attr::PURE) && IsFunction(symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPureFunction(const Scope &scope) {
|
|
|
|
if (const Symbol * symbol{scope.GetSymbol()}) {
|
|
|
|
return IsPureFunction(*symbol);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:28:35 -08:00
|
|
|
static const Symbol *FindPointerComponent(
|
2019-03-04 10:13:12 -08:00
|
|
|
const Scope &scope, std::set<const Scope *> &visited) {
|
|
|
|
if (scope.kind() != Scope::Kind::DerivedType) {
|
2019-03-04 16:28:35 -08:00
|
|
|
return nullptr;
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
if (!visited.insert(&scope).second) {
|
2019-03-04 16:28:35 -08:00
|
|
|
return nullptr;
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
2019-03-04 16:28:35 -08:00
|
|
|
// If there's a top-level pointer component, return it for clearer error
|
|
|
|
// messaging.
|
2019-03-04 10:13:12 -08:00
|
|
|
for (const auto &pair : scope) {
|
|
|
|
const Symbol &symbol{*pair.second};
|
|
|
|
if (symbol.attrs().test(Attr::POINTER)) {
|
2019-03-04 16:28:35 -08:00
|
|
|
return &symbol;
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
2019-03-04 16:28:35 -08:00
|
|
|
}
|
|
|
|
for (const auto &pair : scope) {
|
|
|
|
const Symbol &symbol{*pair.second};
|
2019-03-04 10:13:12 -08:00
|
|
|
if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
|
|
|
|
if (const DeclTypeSpec * type{details->type()}) {
|
|
|
|
if (const DerivedTypeSpec * derived{type->AsDerived()}) {
|
|
|
|
if (const Scope * nested{derived->scope()}) {
|
2019-03-04 16:28:35 -08:00
|
|
|
if (const Symbol *
|
|
|
|
pointer{FindPointerComponent(*nested, visited)}) {
|
|
|
|
return pointer;
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 16:28:35 -08:00
|
|
|
return nullptr;
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:28:35 -08:00
|
|
|
const Symbol *FindPointerComponent(const Scope &scope) {
|
2019-03-04 10:13:12 -08:00
|
|
|
std::set<const Scope *> visited;
|
2019-03-04 16:28:35 -08:00
|
|
|
return FindPointerComponent(scope, visited);
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:28:35 -08:00
|
|
|
const Symbol *FindPointerComponent(const DerivedTypeSpec &derived) {
|
2019-03-04 10:13:12 -08:00
|
|
|
if (const Scope * scope{derived.scope()}) {
|
2019-03-04 16:28:35 -08:00
|
|
|
return FindPointerComponent(*scope);
|
2019-03-04 10:13:12 -08:00
|
|
|
} else {
|
2019-03-04 16:28:35 -08:00
|
|
|
return nullptr;
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:28:35 -08:00
|
|
|
const Symbol *FindPointerComponent(const DeclTypeSpec &type) {
|
2019-03-04 10:13:12 -08:00
|
|
|
if (const DerivedTypeSpec * derived{type.AsDerived()}) {
|
2019-03-04 16:28:35 -08:00
|
|
|
return FindPointerComponent(*derived);
|
2019-03-04 10:13:12 -08:00
|
|
|
} else {
|
2019-03-04 16:28:35 -08:00
|
|
|
return nullptr;
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:28:35 -08:00
|
|
|
const Symbol *FindPointerComponent(const DeclTypeSpec *type) {
|
|
|
|
return type ? FindPointerComponent(*type) : nullptr;
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:28:35 -08:00
|
|
|
const Symbol *FindPointerComponent(const Symbol &symbol) {
|
|
|
|
return symbol.attrs().test(Attr::POINTER)
|
|
|
|
? &symbol
|
|
|
|
: FindPointerComponent(symbol.GetType());
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// C1594 specifies several ways by which an object might be globally visible.
|
2019-03-04 16:28:35 -08:00
|
|
|
const Symbol *FindExternallyVisibleObject(
|
|
|
|
const Symbol &object, const Scope &scope) {
|
|
|
|
// TODO: Storage association with any object for which this predicate holds,
|
|
|
|
// once EQUIVALENCE is supported.
|
|
|
|
if (IsUseAssociated(object, scope) || IsHostAssociated(object, scope) ||
|
2019-03-04 10:13:12 -08:00
|
|
|
(IsPureFunction(scope) && IsPointerDummy(object)) ||
|
2019-03-04 16:28:35 -08:00
|
|
|
(object.attrs().test(Attr::INTENT_IN) && IsDummy(object))) {
|
|
|
|
return &object;
|
|
|
|
} else if (const Symbol * block{FindCommonBlockContaining(object)}) {
|
|
|
|
return block;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|
2019-03-26 00:33:03 -07:00
|
|
|
|
|
|
|
bool ExprHasTypeCategory(const evaluate::GenericExprWrapper &expr,
|
|
|
|
const common::TypeCategory &type) {
|
|
|
|
auto dynamicType{expr.v.GetType()};
|
|
|
|
return dynamicType.has_value() && dynamicType->category == type;
|
|
|
|
}
|
2019-03-04 10:13:12 -08:00
|
|
|
}
|