Skip to content

[flang] Accept intrinsic functions in DATA statement variables #66229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions flang/lib/Evaluate/check-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ bool IsConstantExprHelper<INVARIANT>::operator()(
// LBOUND, UBOUND, and SIZE with truly constant DIM= arguments will have
// been rewritten into DescriptorInquiry operations.
if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&call.proc().u)}) {
const characteristics::Procedure &proc{intrinsic->characteristics.value()};
if (intrinsic->name == "kind" ||
intrinsic->name == IntrinsicProcTable::InvalidName ||
call.arguments().empty() || !call.arguments()[0]) {
Expand All @@ -129,6 +130,16 @@ bool IsConstantExprHelper<INVARIANT>::operator()(
} else if (intrinsic->name == "shape" || intrinsic->name == "size") {
auto shape{GetShape(call.arguments()[0]->UnwrapExpr())};
return shape && IsConstantExprShape(*shape);
} else if (proc.IsPure()) {
for (const auto &arg : call.arguments()) {
if (!arg) {
return false;
} else if (const auto *expr{arg->UnwrapExpr()};
!expr || !(*this)(*expr)) {
return false;
}
}
return true;
}
// TODO: STORAGE_SIZE
}
Expand Down
56 changes: 30 additions & 26 deletions flang/lib/Semantics/check-data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ class DataVarChecker : public evaluate::AllTraverse<DataVarChecker, true> {
lastSymbol.name().ToString());
return false;
}
RestrictPointer();
auto restorer{common::ScopedSet(isPointerAllowed_, false)};
return (*this)(component.base()) && (*this)(lastSymbol);
} else if (IsPointer(lastSymbol)) { // C877
context_.Say(source_,
"Data object must not contain pointer '%s' as a non-rightmost part"_err_en_US,
lastSymbol.name().ToString());
return false;
} else {
if (IsPointer(lastSymbol)) { // C877
context_.Say(source_,
"Data object must not contain pointer '%s' as a non-rightmost part"_err_en_US,
lastSymbol.name().ToString());
return false;
}
return (*this)(component.base()) && (*this)(lastSymbol);
}
return (*this)(component.base()) && (*this)(lastSymbol);
}
bool operator()(const evaluate::ArrayRef &arrayRef) {
hasSubscript_ = true;
Expand All @@ -128,29 +128,32 @@ class DataVarChecker : public evaluate::AllTraverse<DataVarChecker, true> {
return false;
}
bool operator()(const evaluate::Subscript &subs) {
DataVarChecker subscriptChecker{context_, source_};
subscriptChecker.RestrictPointer();
auto restorer1{common::ScopedSet(isPointerAllowed_, false)};
auto restorer2{common::ScopedSet(isFunctionAllowed_, true)};
return common::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) &&
subscriptChecker(subs.u);
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;
if (isFunctionAllowed_) {
// Must have been validated as a constant expression
return true;
} else {
context_.Say(source_,
"Data object variable must not be a function reference"_err_en_US);
return false;
}
}
void RestrictPointer() { isPointerAllowed_ = false; }

private:
bool CheckSubscriptExpr(
Expand Down Expand Up @@ -178,6 +181,7 @@ class DataVarChecker : public evaluate::AllTraverse<DataVarChecker, true> {
bool hasSubscript_{false};
bool isPointerAllowed_{true};
bool isFirstSymbol_{true};
bool isFunctionAllowed_{false};
};

static bool IsValidDataObject(const SomeExpr &expr) { // C878, C879
Expand Down
4 changes: 4 additions & 0 deletions flang/test/Semantics/data05.f90
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ subroutine s13
integer j(2)
data j(2:1), j(1:2) /1,2/ ! CHECK: j (InDataStmt) size=8 offset=0: ObjectEntity type: INTEGER(4) shape: 1_8:2_8 init:[INTEGER(4)::1_4,2_4]
end subroutine
subroutine s14
integer j(0:1)
data (j(modulo(k,2)),k=1,2) /3,4/ ! CHECK: j (InDataStmt) size=8 offset=0: ObjectEntity type: INTEGER(4) shape: 0_8:1_8 init:[INTEGER(4)::4_4,3_4]
end subroutine
end module