llvm/flang/lib/semantics/tools.h
2019-03-04 16:30:25 -08:00

74 lines
2.7 KiB
C++

// 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.
#ifndef FORTRAN_SEMANTICS_TOOLS_H_
#define FORTRAN_SEMANTICS_TOOLS_H_
// Simple predicates and look-up functions that are best defined
// canonically for use in semantic checking.
#include "scope.h"
#include "symbol.h"
#include "type.h"
#include "../evaluate/variable.h"
namespace Fortran::semantics {
const Symbol *FindCommonBlockContaining(const Symbol &object);
const Scope *FindProgramUnitContaining(const Scope &);
const Scope *FindProgramUnitContaining(const Symbol &);
const Scope *FindPureFunctionContaining(const Scope *);
bool IsCommonBlockContaining(const Symbol &block, const Symbol &object);
bool IsAncestor(const Scope *maybeAncestor, const Scope &maybeDescendent);
bool IsUseAssociated(const Symbol *, const Scope &);
bool IsHostAssociated(const Symbol &, const Scope &);
bool IsDummy(const Symbol &);
bool IsPointerDummy(const Symbol &);
bool IsFunction(const Symbol &);
bool IsPureFunction(const Symbol &);
bool IsPureFunction(const Scope &);
bool HasPointerComponent(const Scope &);
bool HasPointerComponent(const DerivedTypeSpec &);
bool HasPointerComponent(const DeclTypeSpec &);
bool IsOrHasPointerComponent(const Symbol &);
// Determines whether an object might be visible outside a
// PURE function (C1594)
bool IsExternallyVisibleObject(const Symbol &, const Scope &);
template<typename A> bool IsExternallyVisibleObject(const A &, const Scope &) {
return false; // default base case
}
template<typename T>
bool IsExternallyVisibleObject(
const evaluate::Designator<T> &designator, const Scope &scope) {
if (const Symbol * symbol{designator.GetBaseObject().symbol()}) {
return IsExternallyVisibleObject(*symbol, scope);
} else {
// Coindexed values are visible even if their image-local objects are not.
return std::holds_alternative<evaluate::CoarrayRef>(designator.u);
}
}
template<typename T>
bool IsExternallyVisibleObject(
const evaluate::Expr<T> &expr, const Scope &scope) {
return std::visit(
[&](const auto &x) { return IsExternallyVisibleObject(x, scope); },
expr.u);
}
}
#endif // FORTRAN_SEMANTICS_TOOLS_H_