Files
clang-p2996/flang/lib/Semantics/check-data.cpp
peter klausler 3a1afd8c3d Rework DATA statement semantics to use typed expressions
Summary:
Updates recent work on DATA statement semantic checking in
flang/lib/Semantics/check-data.{h,cpp} to use the compiler's
internal representation for typed expressions rather than working
on the raw parse tree.  Saves the analyzed expressions for DATA
statement values as parse tree decorations because they'll soon be
needed in lowering.  Corrects wording of some error messages.

Fixes a bug in constant expression checking: structure constructors
are not constant expressions if they set an allocatable component
to anything other than NULL.

Includes infrastructure changes to make this work, some renaming
to reflect the fact that the implied DO loop indices tracked by
expression analysis are not (just) from array constructors, remove
some dead code, and improve some comments.

Reviewers: tskeith, sscalpone, jdoerfert, DavidTruby, anchu-rajendran, schweitz

Reviewed By: tskeith, anchu-rajendran, schweitz

Subscribers: llvm-commits, flang-commits

Tags: #flang, #llvm

Differential Revision: https://reviews.llvm.org/D78834
2020-04-25 10:29:34 -07:00

162 lines
5.7 KiB
C++

//===-- lib/Semantics/check-data.cpp --------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "check-data.h"
#include "flang/Evaluate/traverse.h"
#include "flang/Semantics/expression.h"
namespace Fortran::semantics {
void DataChecker::Leave(const parser::DataStmtConstant &dataConst) {
if (auto *structure{
std::get_if<parser::StructureConstructor>(&dataConst.u)}) {
for (const auto &component :
std::get<std::list<parser::ComponentSpec>>(structure->t)) {
const parser::Expr &parsedExpr{
std::get<parser::ComponentDataSource>(component.t).v.value()};
if (const auto *expr{GetExpr(parsedExpr)}) {
if (!evaluate::IsConstantExpr(*expr)) { // C884
exprAnalyzer_.Say(parsedExpr.source,
"Structure constructor in data value must be a constant expression"_err_en_US);
}
}
}
}
}
// Ensures that references to an implied DO loop control variable are
// represented as such in the "body" of the implied DO loop.
void DataChecker::Enter(const parser::DataImpliedDo &x) {
auto name{std::get<parser::DataImpliedDo::Bounds>(x.t).name.thing.thing};
int kind{evaluate::ResultType<evaluate::ImpliedDoIndex>::kind};
if (const auto dynamicType{evaluate::DynamicType::From(*name.symbol)}) {
kind = dynamicType->kind();
}
exprAnalyzer_.AddImpliedDo(name.source, kind);
}
void DataChecker::Leave(const parser::DataImpliedDo &x) {
auto name{std::get<parser::DataImpliedDo::Bounds>(x.t).name.thing.thing};
exprAnalyzer_.RemoveImpliedDo(name.source);
}
class DataVarChecker : public evaluate::AllTraverse<DataVarChecker, true> {
public:
using Base = evaluate::AllTraverse<DataVarChecker, true>;
DataVarChecker(SemanticsContext &c, parser::CharBlock src)
: Base{*this}, context_{c}, source_{src} {}
using Base::operator();
bool HasComponentWithoutSubscripts() const {
return hasComponent_ && !hasSubscript_;
}
bool operator()(const evaluate::Component &component) {
hasComponent_ = true;
return (*this)(component.base());
}
bool operator()(const evaluate::Subscript &subs) {
hasSubscript_ = true;
return std::visit(
common::visitors{
[&](const evaluate::IndirectSubscriptIntegerExpr &expr) {
return CheckSubscriptExpr(expr);
},
[&](const evaluate::Triplet &triplet) {
return CheckSubscriptExpr(triplet.lower()) &&
CheckSubscriptExpr(triplet.upper()) &&
CheckSubscriptExpr(triplet.stride());
},
},
subs.u);
}
template <typename T>
bool operator()(const evaluate::FunctionRef<T> &) const { // C875
context_.Say(source_,
"Data object variable must not be a function reference"_err_en_US);
return false;
}
bool operator()(const evaluate::CoarrayRef &) const { // C874
context_.Say(
source_, "Data object must not be a coindexed variable"_err_en_US);
return false;
}
private:
bool CheckSubscriptExpr(
const std::optional<evaluate::IndirectSubscriptIntegerExpr> &x) const {
return !x || CheckSubscriptExpr(*x);
}
bool CheckSubscriptExpr(
const evaluate::IndirectSubscriptIntegerExpr &expr) const {
return CheckSubscriptExpr(expr.value());
}
bool CheckSubscriptExpr(
const evaluate::Expr<evaluate::SubscriptInteger> &expr) const {
if (!evaluate::IsConstantExpr(expr)) { // C875,C881
context_.Say(
source_, "Data object must have constant subscripts"_err_en_US);
return false;
} else {
return true;
}
}
SemanticsContext &context_;
parser::CharBlock source_;
bool hasComponent_{false};
bool hasSubscript_{false};
};
// TODO: C876, C877, C879
void DataChecker::Leave(const parser::DataIDoObject &object) {
if (const auto *designator{
std::get_if<parser::Scalar<common::Indirection<parser::Designator>>>(
&object.u)}) {
if (MaybeExpr expr{exprAnalyzer_.Analyze(*designator)}) {
auto source{designator->thing.value().source};
if (evaluate::IsConstantExpr(*expr)) { // C878
exprAnalyzer_.Say(
source, "Data implied do object must be a variable"_err_en_US);
} else {
DataVarChecker checker{exprAnalyzer_.context(), source};
if (checker(*expr) && checker.HasComponentWithoutSubscripts()) { // C880
exprAnalyzer_.Say(source,
"Data implied do structure component must be subscripted"_err_en_US);
}
}
}
}
}
void DataChecker::Leave(const parser::DataStmtObject &dataObject) {
if (const auto *var{
std::get_if<common::Indirection<parser::Variable>>(&dataObject.u)}) {
if (auto expr{exprAnalyzer_.Analyze(*var)}) {
DataVarChecker{exprAnalyzer_.context(),
parser::FindSourceLocation(dataObject)}(expr);
}
}
}
void DataChecker::Leave(const parser::DataStmtRepeat &dataRepeat) {
if (const auto *designator{parser::Unwrap<parser::Designator>(dataRepeat)}) {
if (auto *dataRef{std::get_if<parser::DataRef>(&designator->u)}) {
if (MaybeExpr checked{exprAnalyzer_.Analyze(*dataRef)}) {
auto expr{evaluate::Fold(
exprAnalyzer_.GetFoldingContext(), std::move(checked))};
if (auto i64{ToInt64(expr)}) {
if (*i64 < 0) { // C882
exprAnalyzer_.Say(designator->source,
"Repeat count for data value must not be negative"_err_en_US);
}
}
}
}
}
}
} // namespace Fortran::semantics