Skip to content

Commit

Permalink
[flang][runtime] Handle incomplete NAMELIST input derived type compon… (
Browse files Browse the repository at this point in the history
#66831)

…ent list

When a derived type value appears in NAMELIST input, its components'
values appear in sequence. This sequence can be truncated by a NAME=
that begins the next NAMELIST input item, or by the terminal '/' that
ends the NAMELIST group. Extend the mechanism already in place for
truncated array item lists in NAMELIST input so that it also applies to
derived type component sequences, and rename things appropriately.
  • Loading branch information
klausler committed Oct 16, 2023
1 parent 6f41510 commit 750c8e3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 5 additions & 1 deletion flang/runtime/descriptor-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ static bool DefaultComponentwiseIO(IoStatementState &io,
*compArray.Element<typeInfo::Component>(at)};
if (!DefaultComponentIO<DIR>(
io, component, descriptor, subscripts, handler, table)) {
return false;
// Truncated nonempty namelist input sequence?
auto *listInput{
io.get_if<ListDirectedStatementState<Direction::Input>>()};
return DIR == Direction::Input && (j > 0 || k > 0) && listInput &&
listInput->inNamelistSequence();
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions flang/runtime/io-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ template <>
class ListDirectedStatementState<Direction::Input>
: public FormattedIoStatementState<Direction::Input> {
public:
bool inNamelistArray() const { return inNamelistArray_; }
void set_inNamelistArray(bool yes = true) { inNamelistArray_ = yes; }
bool inNamelistSequence() const { return inNamelistSequence_; }

// Skips value separators, handles repetition and null values.
// Vacant when '/' appears; present with descriptor == ListDirectedNullValue
Expand All @@ -308,11 +307,11 @@ class ListDirectedStatementState<Direction::Input>
// input statement. This member function resets some state so that
// repetition and null values work correctly for each successive
// NAMELIST input item.
void ResetForNextNamelistItem(bool inNamelistArray) {
void ResetForNextNamelistItem(bool inNamelistSequence) {
remaining_ = 0;
eatComma_ = false;
realPart_ = imaginaryPart_ = false;
inNamelistArray_ = inNamelistArray;
inNamelistSequence_ = inNamelistSequence;
}

private:
Expand All @@ -322,7 +321,7 @@ class ListDirectedStatementState<Direction::Input>
bool hitSlash_{false}; // once '/' is seen, nullify further items
bool realPart_{false};
bool imaginaryPart_{false};
bool inNamelistArray_{false};
bool inNamelistSequence_{false};
};

template <Direction DIR>
Expand Down
11 changes: 7 additions & 4 deletions flang/runtime/namelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,18 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
}
io.HandleRelativePosition(byteCount);
// Read the values into the descriptor. An array can be short.
listInput->ResetForNextNamelistItem(useDescriptor->rank() > 0);
if (const auto *addendum{useDescriptor->Addendum()};
addendum && addendum->derivedType()) {
const NonTbpDefinedIoTable *table{group.nonTbpDefinedIo};
listInput->ResetForNextNamelistItem(/*inNamelistSequence=*/true);
if (!IONAME(InputDerivedType)(cookie, *useDescriptor, table)) {
return false;
}
} else if (!descr::DescriptorIO<Direction::Input>(io, *useDescriptor)) {
return false;
} else {
listInput->ResetForNextNamelistItem(useDescriptor->rank() > 0);
if (!descr::DescriptorIO<Direction::Input>(io, *useDescriptor)) {
return false;
}
}
next = io.GetNextNonBlank(byteCount);
if (next && *next == comma) {
Expand All @@ -549,7 +552,7 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) {
bool IsNamelistNameOrSlash(IoStatementState &io) {
if (auto *listInput{
io.get_if<ListDirectedStatementState<Direction::Input>>()}) {
if (listInput->inNamelistArray()) {
if (listInput->inNamelistSequence()) {
SavedPosition savedPosition{io};
std::size_t byteCount{0};
if (auto ch{io.GetNextNonBlank(byteCount)}) {
Expand Down

0 comments on commit 750c8e3

Please sign in to comment.