2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Semantics/check-data.cpp --------------------------------------===//
|
2020-02-21 14:19:14 +08:00
|
|
|
//
|
|
|
|
// 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"
|
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 04:54:11 +08:00
|
|
|
#include "flang/Evaluate/traverse.h"
|
|
|
|
#include "flang/Semantics/expression.h"
|
2020-02-21 14:19:14 +08:00
|
|
|
|
|
|
|
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)}) {
|
2020-03-29 12:00:16 +08:00
|
|
|
if (!evaluate::IsConstantExpr(*expr)) { // C884
|
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 04:54:11 +08:00
|
|
|
exprAnalyzer_.Say(parsedExpr.source,
|
2020-02-21 14:19:14 +08:00
|
|
|
"Structure constructor in data value must be a constant expression"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 04:54:11 +08:00
|
|
|
// 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};
|
|
|
|
};
|
|
|
|
|
2020-04-18 14:09:34 +08:00
|
|
|
// TODO: C876, C877, C879
|
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 04:54:11 +08:00
|
|
|
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);
|
2020-04-18 14:09:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataChecker::Leave(const parser::DataStmtObject &dataObject) {
|
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 04:54:11 +08:00
|
|
|
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);
|
2020-04-18 14:09:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 14:19:14 +08:00
|
|
|
|
|
|
|
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)}) {
|
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 04:54:11 +08:00
|
|
|
if (MaybeExpr checked{exprAnalyzer_.Analyze(*dataRef)}) {
|
|
|
|
auto expr{evaluate::Fold(
|
|
|
|
exprAnalyzer_.GetFoldingContext(), std::move(checked))};
|
2020-02-21 14:19:14 +08:00
|
|
|
if (auto i64{ToInt64(expr)}) {
|
2020-03-29 12:00:16 +08:00
|
|
|
if (*i64 < 0) { // C882
|
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 04:54:11 +08:00
|
|
|
exprAnalyzer_.Say(designator->source,
|
2020-02-21 14:19:14 +08:00
|
|
|
"Repeat count for data value must not be negative"_err_en_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::semantics
|