1 //===-- runtime/io-stmt.cpp -------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "io-stmt.h"
10 #include "connection.h"
11 #include "format.h"
12 #include "memory.h"
13 #include "tools.h"
14 #include "unit.h"
15 #include <algorithm>
16 #include <cstdio>
17 #include <cstring>
18 #include <limits>
19 
20 namespace Fortran::runtime::io {
21 
22 int IoStatementBase::EndIoStatement() { return GetIoStat(); }
23 
24 bool IoStatementBase::Emit(const char *, std::size_t, std::size_t) {
25   return false;
26 }
27 
28 bool IoStatementBase::Emit(const char *, std::size_t) {
29   return false;
30 }
31 
32 bool IoStatementBase::Emit(const char16_t *, std::size_t) {
33   return false;
34 }
35 
36 bool IoStatementBase::Emit(const char32_t *, std::size_t) {
37   return false;
38 }
39 
40 std::optional<char32_t> IoStatementBase::GetCurrentChar() {
41   return std::nullopt;
42 }
43 
44 bool IoStatementBase::AdvanceRecord(int) { return false; }
45 
46 void IoStatementBase::BackspaceRecord() {}
47 
48 bool IoStatementBase::Receive(char *, std::size_t, std::size_t) {
49   return false;
50 }
51 
52 std::optional<DataEdit> IoStatementBase::GetNextDataEdit(
53     IoStatementState &, int) {
54   return std::nullopt;
55 }
56 
57 ExternalFileUnit *IoStatementBase::GetExternalFileUnit() const {
58   return nullptr;
59 }
60 
61 bool IoStatementBase::BeginReadingRecord() { return true; }
62 
63 void IoStatementBase::FinishReadingRecord() {}
64 
65 void IoStatementBase::HandleAbsolutePosition(std::int64_t) {}
66 
67 void IoStatementBase::HandleRelativePosition(std::int64_t) {}
68 
69 bool IoStatementBase::Inquire(InquiryKeywordHash, char *, std::size_t) {
70   return false;
71 }
72 
73 bool IoStatementBase::Inquire(InquiryKeywordHash, bool &) {
74   return false;
75 }
76 
77 bool IoStatementBase::Inquire(InquiryKeywordHash, std::int64_t, bool &) {
78   return false;
79 }
80 
81 bool IoStatementBase::Inquire(InquiryKeywordHash, std::int64_t &) {
82   return false;
83 }
84 
85 void IoStatementBase::BadInquiryKeywordHashCrash(InquiryKeywordHash inquiry) {
86   char buffer[16];
87   const char *decode{InquiryKeywordHashDecode(buffer, sizeof buffer, inquiry)};
88   Crash("bad InquiryKeywordHash 0x%x (%s)", inquiry,
89       decode ? decode : "(cannot decode)");
90 }
91 
92 template <Direction DIR, typename CHAR>
93 InternalIoStatementState<DIR, CHAR>::InternalIoStatementState(
94     Buffer scalar, std::size_t length, const char *sourceFile, int sourceLine)
95     : IoStatementBase{sourceFile, sourceLine}, unit_{scalar, length} {}
96 
97 template <Direction DIR, typename CHAR>
98 InternalIoStatementState<DIR, CHAR>::InternalIoStatementState(
99     const Descriptor &d, const char *sourceFile, int sourceLine)
100     : IoStatementBase{sourceFile, sourceLine}, unit_{d, *this} {}
101 
102 template <Direction DIR, typename CHAR>
103 bool InternalIoStatementState<DIR, CHAR>::Emit(
104     const CharType *data, std::size_t chars) {
105   if constexpr (DIR == Direction::Input) {
106     Crash("InternalIoStatementState<Direction::Input>::Emit() called");
107     return false;
108   }
109   return unit_.Emit(data, chars * sizeof(CharType), *this);
110 }
111 
112 template <Direction DIR, typename CHAR>
113 std::optional<char32_t> InternalIoStatementState<DIR, CHAR>::GetCurrentChar() {
114   if constexpr (DIR == Direction::Output) {
115     Crash(
116         "InternalIoStatementState<Direction::Output>::GetCurrentChar() called");
117     return std::nullopt;
118   }
119   return unit_.GetCurrentChar(*this);
120 }
121 
122 template <Direction DIR, typename CHAR>
123 bool InternalIoStatementState<DIR, CHAR>::AdvanceRecord(int n) {
124   while (n-- > 0) {
125     if (!unit_.AdvanceRecord(*this)) {
126       return false;
127     }
128   }
129   return true;
130 }
131 
132 template <Direction DIR, typename CHAR>
133 void InternalIoStatementState<DIR, CHAR>::BackspaceRecord() {
134   unit_.BackspaceRecord(*this);
135 }
136 
137 template <Direction DIR, typename CHAR>
138 int InternalIoStatementState<DIR, CHAR>::EndIoStatement() {
139   if constexpr (DIR == Direction::Output) {
140     unit_.EndIoStatement(); // fill
141   }
142   auto result{IoStatementBase::EndIoStatement()};
143   if (free_) {
144     FreeMemory(this);
145   }
146   return result;
147 }
148 
149 template <Direction DIR, typename CHAR>
150 void InternalIoStatementState<DIR, CHAR>::HandleAbsolutePosition(
151     std::int64_t n) {
152   return unit_.HandleAbsolutePosition(n);
153 }
154 
155 template <Direction DIR, typename CHAR>
156 void InternalIoStatementState<DIR, CHAR>::HandleRelativePosition(
157     std::int64_t n) {
158   return unit_.HandleRelativePosition(n);
159 }
160 
161 template <Direction DIR, typename CHAR>
162 InternalFormattedIoStatementState<DIR, CHAR>::InternalFormattedIoStatementState(
163     Buffer buffer, std::size_t length, const CHAR *format,
164     std::size_t formatLength, const char *sourceFile, int sourceLine)
165     : InternalIoStatementState<DIR, CHAR>{buffer, length, sourceFile,
166           sourceLine},
167       ioStatementState_{*this}, format_{*this, format, formatLength} {}
168 
169 template <Direction DIR, typename CHAR>
170 InternalFormattedIoStatementState<DIR, CHAR>::InternalFormattedIoStatementState(
171     const Descriptor &d, const CHAR *format, std::size_t formatLength,
172     const char *sourceFile, int sourceLine)
173     : InternalIoStatementState<DIR, CHAR>{d, sourceFile, sourceLine},
174       ioStatementState_{*this}, format_{*this, format, formatLength} {}
175 
176 template <Direction DIR, typename CHAR>
177 int InternalFormattedIoStatementState<DIR, CHAR>::EndIoStatement() {
178   if constexpr (DIR == Direction::Output) {
179     format_.Finish(*this); // ignore any remaining input positioning actions
180   }
181   return InternalIoStatementState<DIR, CHAR>::EndIoStatement();
182 }
183 
184 template <Direction DIR, typename CHAR>
185 InternalListIoStatementState<DIR, CHAR>::InternalListIoStatementState(
186     Buffer buffer, std::size_t length, const char *sourceFile, int sourceLine)
187     : InternalIoStatementState<DIR, CharType>{buffer, length, sourceFile,
188           sourceLine},
189       ioStatementState_{*this} {}
190 
191 template <Direction DIR, typename CHAR>
192 InternalListIoStatementState<DIR, CHAR>::InternalListIoStatementState(
193     const Descriptor &d, const char *sourceFile, int sourceLine)
194     : InternalIoStatementState<DIR, CharType>{d, sourceFile, sourceLine},
195       ioStatementState_{*this} {}
196 
197 ExternalIoStatementBase::ExternalIoStatementBase(
198     ExternalFileUnit &unit, const char *sourceFile, int sourceLine)
199     : IoStatementBase{sourceFile, sourceLine}, unit_{unit} {}
200 
201 MutableModes &ExternalIoStatementBase::mutableModes() { return unit_.modes; }
202 
203 ConnectionState &ExternalIoStatementBase::GetConnectionState() { return unit_; }
204 
205 int ExternalIoStatementBase::EndIoStatement() {
206   if (mutableModes().nonAdvancing) {
207     unit_.leftTabLimit = unit_.furthestPositionInRecord;
208   } else {
209     unit_.leftTabLimit.reset();
210   }
211   auto result{IoStatementBase::EndIoStatement()};
212   unit_.EndIoStatement(); // annihilates *this in unit_.u_
213   return result;
214 }
215 
216 void OpenStatementState::set_path(const char *path, std::size_t length) {
217   pathLength_ = TrimTrailingSpaces(path, length);
218   path_ = SaveDefaultCharacter(path, pathLength_, *this);
219 }
220 
221 int OpenStatementState::EndIoStatement() {
222   if (path_.get() || wasExtant_ ||
223       (status_ && *status_ == OpenStatus::Scratch)) {
224     unit().OpenUnit(status_, action_, position_, std::move(path_), pathLength_,
225         convert_, *this);
226   } else {
227     unit().OpenAnonymousUnit(status_, action_, position_, convert_, *this);
228   }
229   if (access_) {
230     if (*access_ != unit().access) {
231       if (wasExtant_) {
232         SignalError("ACCESS= may not be changed on an open unit");
233       }
234     }
235     unit().access = *access_;
236   }
237   if (!unit().isUnformatted) {
238     unit().isUnformatted = isUnformatted_;
239   }
240   if (isUnformatted_ && *isUnformatted_ != *unit().isUnformatted) {
241     if (wasExtant_) {
242       SignalError("FORM= may not be changed on an open unit");
243     }
244     unit().isUnformatted = *isUnformatted_;
245   }
246   return ExternalIoStatementBase::EndIoStatement();
247 }
248 
249 int CloseStatementState::EndIoStatement() {
250   int result{ExternalIoStatementBase::EndIoStatement()};
251   unit().CloseUnit(status_, *this);
252   unit().DestroyClosed();
253   return result;
254 }
255 
256 int NoUnitIoStatementState::EndIoStatement() {
257   auto result{IoStatementBase::EndIoStatement()};
258   FreeMemory(this);
259   return result;
260 }
261 
262 template <Direction DIR>
263 ExternalIoStatementState<DIR>::ExternalIoStatementState(
264     ExternalFileUnit &unit, const char *sourceFile, int sourceLine)
265     : ExternalIoStatementBase{unit, sourceFile, sourceLine}, mutableModes_{
266                                                                  unit.modes} {}
267 
268 template <Direction DIR> int ExternalIoStatementState<DIR>::EndIoStatement() {
269   if constexpr (DIR == Direction::Input) {
270     BeginReadingRecord(); // in case there were no I/O items
271     if (!mutableModes().nonAdvancing) {
272       FinishReadingRecord();
273     }
274   } else {
275     if (!mutableModes().nonAdvancing) {
276       unit().AdvanceRecord(*this);
277     }
278     unit().FlushIfTerminal(*this);
279   }
280   return ExternalIoStatementBase::EndIoStatement();
281 }
282 
283 template <Direction DIR>
284 bool ExternalIoStatementState<DIR>::Emit(
285     const char *data, std::size_t bytes, std::size_t elementBytes) {
286   if constexpr (DIR == Direction::Input) {
287     Crash("ExternalIoStatementState::Emit(char) called for input statement");
288   }
289   return unit().Emit(data, bytes, elementBytes, *this);
290 }
291 
292 template <Direction DIR>
293 bool ExternalIoStatementState<DIR>::Emit(const char *data, std::size_t bytes) {
294   if constexpr (DIR == Direction::Input) {
295     Crash("ExternalIoStatementState::Emit(char) called for input statement");
296   }
297   return unit().Emit(data, bytes, 0, *this);
298 }
299 
300 template <Direction DIR>
301 bool ExternalIoStatementState<DIR>::Emit(
302     const char16_t *data, std::size_t chars) {
303   if constexpr (DIR == Direction::Input) {
304     Crash(
305         "ExternalIoStatementState::Emit(char16_t) called for input statement");
306   }
307   // TODO: UTF-8 encoding
308   return unit().Emit(reinterpret_cast<const char *>(data), chars * sizeof *data,
309       sizeof *data, *this);
310 }
311 
312 template <Direction DIR>
313 bool ExternalIoStatementState<DIR>::Emit(
314     const char32_t *data, std::size_t chars) {
315   if constexpr (DIR == Direction::Input) {
316     Crash(
317         "ExternalIoStatementState::Emit(char32_t) called for input statement");
318   }
319   // TODO: UTF-8 encoding
320   return unit().Emit(reinterpret_cast<const char *>(data), chars * sizeof *data,
321       sizeof *data, *this);
322 }
323 
324 template <Direction DIR>
325 std::optional<char32_t> ExternalIoStatementState<DIR>::GetCurrentChar() {
326   if constexpr (DIR == Direction::Output) {
327     Crash(
328         "ExternalIoStatementState<Direction::Output>::GetCurrentChar() called");
329   }
330   return unit().GetCurrentChar(*this);
331 }
332 
333 template <Direction DIR>
334 bool ExternalIoStatementState<DIR>::AdvanceRecord(int n) {
335   while (n-- > 0) {
336     if (!unit().AdvanceRecord(*this)) {
337       return false;
338     }
339   }
340   return true;
341 }
342 
343 template <Direction DIR> void ExternalIoStatementState<DIR>::BackspaceRecord() {
344   unit().BackspaceRecord(*this);
345 }
346 
347 template <Direction DIR>
348 void ExternalIoStatementState<DIR>::HandleAbsolutePosition(std::int64_t n) {
349   return unit().HandleAbsolutePosition(n);
350 }
351 
352 template <Direction DIR>
353 void ExternalIoStatementState<DIR>::HandleRelativePosition(std::int64_t n) {
354   return unit().HandleRelativePosition(n);
355 }
356 
357 template <Direction DIR>
358 bool ExternalIoStatementState<DIR>::BeginReadingRecord() {
359   if constexpr (DIR == Direction::Input) {
360     return unit().BeginReadingRecord(*this);
361   } else {
362     Crash("ExternalIoStatementState<Direction::Output>::BeginReadingRecord() "
363           "called");
364     return false;
365   }
366 }
367 
368 template <Direction DIR>
369 void ExternalIoStatementState<DIR>::FinishReadingRecord() {
370   if constexpr (DIR == Direction::Input) {
371     unit().FinishReadingRecord(*this);
372   } else {
373     Crash("ExternalIoStatementState<Direction::Output>::FinishReadingRecord() "
374           "called");
375   }
376 }
377 
378 template <Direction DIR, typename CHAR>
379 ExternalFormattedIoStatementState<DIR, CHAR>::ExternalFormattedIoStatementState(
380     ExternalFileUnit &unit, const CHAR *format, std::size_t formatLength,
381     const char *sourceFile, int sourceLine)
382     : ExternalIoStatementState<DIR>{unit, sourceFile, sourceLine},
383       format_{*this, format, formatLength} {}
384 
385 template <Direction DIR, typename CHAR>
386 int ExternalFormattedIoStatementState<DIR, CHAR>::EndIoStatement() {
387   format_.Finish(*this);
388   return ExternalIoStatementState<DIR>::EndIoStatement();
389 }
390 
391 std::optional<DataEdit> IoStatementState::GetNextDataEdit(int n) {
392   return std::visit(
393       [&](auto &x) { return x.get().GetNextDataEdit(*this, n); }, u_);
394 }
395 
396 bool IoStatementState::Emit(
397     const char *data, std::size_t n, std::size_t elementBytes) {
398   return std::visit(
399       [=](auto &x) { return x.get().Emit(data, n, elementBytes); }, u_);
400 }
401 
402 bool IoStatementState::Emit(const char *data, std::size_t n) {
403   return std::visit([=](auto &x) { return x.get().Emit(data, n); }, u_);
404 }
405 
406 bool IoStatementState::Emit(const char16_t *data, std::size_t chars) {
407   return std::visit([=](auto &x) { return x.get().Emit(data, chars); }, u_);
408 }
409 
410 bool IoStatementState::Emit(const char32_t *data, std::size_t chars) {
411   return std::visit([=](auto &x) { return x.get().Emit(data, chars); }, u_);
412 }
413 
414 bool IoStatementState::Receive(
415     char *data, std::size_t n, std::size_t elementBytes) {
416   return std::visit(
417       [=](auto &x) { return x.get().Receive(data, n, elementBytes); }, u_);
418 }
419 
420 std::optional<char32_t> IoStatementState::GetCurrentChar() {
421   return std::visit([&](auto &x) { return x.get().GetCurrentChar(); }, u_);
422 }
423 
424 bool IoStatementState::AdvanceRecord(int n) {
425   return std::visit([=](auto &x) { return x.get().AdvanceRecord(n); }, u_);
426 }
427 
428 void IoStatementState::BackspaceRecord() {
429   std::visit([](auto &x) { x.get().BackspaceRecord(); }, u_);
430 }
431 
432 void IoStatementState::HandleRelativePosition(std::int64_t n) {
433   std::visit([=](auto &x) { x.get().HandleRelativePosition(n); }, u_);
434 }
435 
436 void IoStatementState::HandleAbsolutePosition(std::int64_t n) {
437   std::visit([=](auto &x) { x.get().HandleAbsolutePosition(n); }, u_);
438 }
439 
440 int IoStatementState::EndIoStatement() {
441   return std::visit([](auto &x) { return x.get().EndIoStatement(); }, u_);
442 }
443 
444 ConnectionState &IoStatementState::GetConnectionState() {
445   return std::visit(
446       [](auto &x) -> ConnectionState & { return x.get().GetConnectionState(); },
447       u_);
448 }
449 
450 MutableModes &IoStatementState::mutableModes() {
451   return std::visit(
452       [](auto &x) -> MutableModes & { return x.get().mutableModes(); }, u_);
453 }
454 
455 bool IoStatementState::BeginReadingRecord() {
456   return std::visit([](auto &x) { return x.get().BeginReadingRecord(); }, u_);
457 }
458 
459 IoErrorHandler &IoStatementState::GetIoErrorHandler() const {
460   return std::visit(
461       [](auto &x) -> IoErrorHandler & {
462         return static_cast<IoErrorHandler &>(x.get());
463       },
464       u_);
465 }
466 
467 ExternalFileUnit *IoStatementState::GetExternalFileUnit() const {
468   return std::visit([](auto &x) { return x.get().GetExternalFileUnit(); }, u_);
469 }
470 
471 bool IoStatementState::EmitRepeated(char ch, std::size_t n) {
472   return std::visit(
473       [=](auto &x) {
474         for (std::size_t j{0}; j < n; ++j) {
475           if (!x.get().Emit(&ch, 1)) {
476             return false;
477           }
478         }
479         return true;
480       },
481       u_);
482 }
483 
484 bool IoStatementState::EmitField(
485     const char *p, std::size_t length, std::size_t width) {
486   if (width <= 0) {
487     width = static_cast<int>(length);
488   }
489   if (length > static_cast<std::size_t>(width)) {
490     return EmitRepeated('*', width);
491   } else {
492     return EmitRepeated(' ', static_cast<int>(width - length)) &&
493         Emit(p, length);
494   }
495 }
496 
497 std::optional<char32_t> IoStatementState::PrepareInput(
498     const DataEdit &edit, std::optional<int> &remaining) {
499   remaining.reset();
500   if (edit.descriptor == DataEdit::ListDirected) {
501     GetNextNonBlank();
502   } else {
503     if (edit.width.value_or(0) > 0) {
504       remaining = *edit.width;
505     }
506     SkipSpaces(remaining);
507   }
508   return NextInField(remaining);
509 }
510 
511 std::optional<char32_t> IoStatementState::SkipSpaces(
512     std::optional<int> &remaining) {
513   while (!remaining || *remaining > 0) {
514     if (auto ch{GetCurrentChar()}) {
515       if (*ch != ' ' && *ch != '\t') {
516         return ch;
517       }
518       HandleRelativePosition(1);
519       if (remaining) {
520         --*remaining;
521       }
522     } else {
523       break;
524     }
525   }
526   return std::nullopt;
527 }
528 
529 std::optional<char32_t> IoStatementState::NextInField(
530     std::optional<int> &remaining) {
531   if (!remaining) { // list-directed or NAMELIST: check for separators
532     if (auto next{GetCurrentChar()}) {
533       switch (*next) {
534       case ' ':
535       case '\t':
536       case ',':
537       case ';':
538       case '/':
539       case '(':
540       case ')':
541       case '\'':
542       case '"':
543       case '*':
544       case '\n': // for stream access
545         break;
546       default:
547         HandleRelativePosition(1);
548         return next;
549       }
550     }
551   } else if (*remaining > 0) {
552     if (auto next{GetCurrentChar()}) {
553       --*remaining;
554       HandleRelativePosition(1);
555       return next;
556     }
557     const ConnectionState &connection{GetConnectionState()};
558     if (!connection.IsAtEOF() && connection.isFixedRecordLength &&
559         connection.recordLength &&
560         connection.positionInRecord >= *connection.recordLength) {
561       if (connection.modes.pad) { // PAD='YES'
562         --*remaining;
563         return std::optional<char32_t>{' '};
564       }
565       IoErrorHandler &handler{GetIoErrorHandler()};
566       if (mutableModes().nonAdvancing) {
567         handler.SignalEor();
568       } else {
569         handler.SignalError(IostatRecordReadOverrun);
570       }
571     }
572   }
573   return std::nullopt;
574 }
575 
576 std::optional<char32_t> IoStatementState::GetNextNonBlank() {
577   auto ch{GetCurrentChar()};
578   bool inNamelist{GetConnectionState().modes.inNamelist};
579   while (!ch || *ch == ' ' || *ch == '\t' || (inNamelist && *ch == '!')) {
580     if (ch && (*ch == ' ' || *ch == '\t')) {
581       HandleRelativePosition(1);
582     } else if (!AdvanceRecord()) {
583       return std::nullopt;
584     }
585     ch = GetCurrentChar();
586   }
587   return ch;
588 }
589 
590 bool IoStatementState::Inquire(
591     InquiryKeywordHash inquiry, char *out, std::size_t chars) {
592   return std::visit(
593       [&](auto &x) { return x.get().Inquire(inquiry, out, chars); }, u_);
594 }
595 
596 bool IoStatementState::Inquire(InquiryKeywordHash inquiry, bool &out) {
597   return std::visit([&](auto &x) { return x.get().Inquire(inquiry, out); }, u_);
598 }
599 
600 bool IoStatementState::Inquire(
601     InquiryKeywordHash inquiry, std::int64_t id, bool &out) {
602   return std::visit(
603       [&](auto &x) { return x.get().Inquire(inquiry, id, out); }, u_);
604 }
605 
606 bool IoStatementState::Inquire(InquiryKeywordHash inquiry, std::int64_t &n) {
607   return std::visit([&](auto &x) { return x.get().Inquire(inquiry, n); }, u_);
608 }
609 
610 bool ListDirectedStatementState<Direction::Output>::EmitLeadingSpaceOrAdvance(
611     IoStatementState &io, std::size_t length, bool isCharacter) {
612   if (length == 0) {
613     return true;
614   }
615   const ConnectionState &connection{io.GetConnectionState()};
616   int space{connection.positionInRecord == 0 ||
617       !(isCharacter && lastWasUndelimitedCharacter())};
618   set_lastWasUndelimitedCharacter(false);
619   if (connection.NeedAdvance(space + length)) {
620     return io.AdvanceRecord();
621   }
622   if (space) {
623     return io.Emit(" ", 1);
624   }
625   return true;
626 }
627 
628 std::optional<DataEdit>
629 ListDirectedStatementState<Direction::Output>::GetNextDataEdit(
630     IoStatementState &io, int maxRepeat) {
631   DataEdit edit;
632   edit.descriptor = DataEdit::ListDirected;
633   edit.repeat = maxRepeat;
634   edit.modes = io.mutableModes();
635   return edit;
636 }
637 
638 std::optional<DataEdit>
639 ListDirectedStatementState<Direction::Input>::GetNextDataEdit(
640     IoStatementState &io, int maxRepeat) {
641   // N.B. list-directed transfers cannot be nonadvancing (C1221)
642   ConnectionState &connection{io.GetConnectionState()};
643   DataEdit edit;
644   edit.descriptor = DataEdit::ListDirected;
645   edit.repeat = 1; // may be overridden below
646   edit.modes = connection.modes;
647   if (hitSlash_) { // everything after '/' is nullified
648     edit.descriptor = DataEdit::ListDirectedNullValue;
649     return edit;
650   }
651   char32_t comma{','};
652   if (io.mutableModes().editingFlags & decimalComma) {
653     comma = ';';
654   }
655   if (remaining_ > 0 && !realPart_) { // "r*c" repetition in progress
656     while (connection.currentRecordNumber > repeatRecordNumber_) {
657       io.BackspaceRecord();
658     }
659     connection.HandleAbsolutePosition(repeatPositionInRecord_);
660     if (!imaginaryPart_) {
661       edit.repeat = std::min<int>(remaining_, maxRepeat);
662       auto ch{io.GetCurrentChar()};
663       if (!ch || *ch == ' ' || *ch == '\t' || *ch == comma) {
664         // "r*" repeated null
665         edit.descriptor = DataEdit::ListDirectedNullValue;
666       }
667     }
668     remaining_ -= edit.repeat;
669     return edit;
670   }
671   // Skip separators, handle a "r*c" repeat count; see 13.10.2 in Fortran 2018
672   if (imaginaryPart_) {
673     imaginaryPart_ = false;
674   } else if (realPart_) {
675     realPart_ = false;
676     imaginaryPart_ = true;
677     edit.descriptor = DataEdit::ListDirectedImaginaryPart;
678   }
679   auto ch{io.GetNextNonBlank()};
680   if (ch && *ch == comma && eatComma_) {
681     // Consume comma & whitespace after previous item.
682     // This includes the comma between real and imaginary components
683     // in list-directed/NAMELIST complex input.
684     io.HandleRelativePosition(1);
685     ch = io.GetNextNonBlank();
686   }
687   eatComma_ = true;
688   if (!ch) {
689     return std::nullopt;
690   }
691   if (*ch == '/') {
692     hitSlash_ = true;
693     edit.descriptor = DataEdit::ListDirectedNullValue;
694     return edit;
695   }
696   if (*ch == comma) { // separator: null value
697     edit.descriptor = DataEdit::ListDirectedNullValue;
698     return edit;
699   }
700   if (imaginaryPart_) { // can't repeat components
701     return edit;
702   }
703   if (*ch >= '0' && *ch <= '9') { // look for "r*" repetition count
704     auto start{connection.positionInRecord};
705     int r{0};
706     do {
707       static auto constexpr clamp{(std::numeric_limits<int>::max() - '9') / 10};
708       if (r >= clamp) {
709         r = 0;
710         break;
711       }
712       r = 10 * r + (*ch - '0');
713       io.HandleRelativePosition(1);
714       ch = io.GetCurrentChar();
715     } while (ch && *ch >= '0' && *ch <= '9');
716     if (r > 0 && ch && *ch == '*') { // subtle: r must be nonzero
717       io.HandleRelativePosition(1);
718       ch = io.GetCurrentChar();
719       if (ch && *ch == '/') { // r*/
720         hitSlash_ = true;
721         edit.descriptor = DataEdit::ListDirectedNullValue;
722         return edit;
723       }
724       if (!ch || *ch == ' ' || *ch == '\t' || *ch == comma) { // "r*" null
725         edit.descriptor = DataEdit::ListDirectedNullValue;
726       }
727       edit.repeat = std::min<int>(r, maxRepeat);
728       remaining_ = r - edit.repeat;
729       repeatRecordNumber_ = connection.currentRecordNumber;
730       repeatPositionInRecord_ = connection.positionInRecord;
731     } else { // not a repetition count, just an integer value; rewind
732       connection.positionInRecord = start;
733     }
734   }
735   if (!imaginaryPart_ && ch && *ch == '(') {
736     realPart_ = true;
737     io.HandleRelativePosition(1);
738     edit.descriptor = DataEdit::ListDirectedRealPart;
739   }
740   return edit;
741 }
742 
743 template <Direction DIR>
744 bool ExternalUnformattedIoStatementState<DIR>::Receive(
745     char *data, std::size_t bytes, std::size_t elementBytes) {
746   if constexpr (DIR == Direction::Output) {
747     this->Crash("ExternalUnformattedIoStatementState::Receive() called for "
748                 "output statement");
749   }
750   return this->unit().Receive(data, bytes, elementBytes, *this);
751 }
752 
753 template <Direction DIR>
754 ChildIoStatementState<DIR>::ChildIoStatementState(
755     ChildIo &child, const char *sourceFile, int sourceLine)
756     : IoStatementBase{sourceFile, sourceLine}, child_{child} {}
757 
758 template <Direction DIR>
759 MutableModes &ChildIoStatementState<DIR>::mutableModes() {
760   return child_.parent().mutableModes();
761 }
762 
763 template <Direction DIR>
764 ConnectionState &ChildIoStatementState<DIR>::GetConnectionState() {
765   return child_.parent().GetConnectionState();
766 }
767 
768 template <Direction DIR>
769 ExternalFileUnit *ChildIoStatementState<DIR>::GetExternalFileUnit() const {
770   return child_.parent().GetExternalFileUnit();
771 }
772 
773 template <Direction DIR> int ChildIoStatementState<DIR>::EndIoStatement() {
774   auto result{IoStatementBase::EndIoStatement()};
775   child_.EndIoStatement(); // annihilates *this in child_.u_
776   return result;
777 }
778 
779 template <Direction DIR>
780 bool ChildIoStatementState<DIR>::Emit(
781     const char *data, std::size_t bytes, std::size_t elementBytes) {
782   return child_.parent().Emit(data, bytes, elementBytes);
783 }
784 
785 template <Direction DIR>
786 bool ChildIoStatementState<DIR>::Emit(const char *data, std::size_t bytes) {
787   return child_.parent().Emit(data, bytes);
788 }
789 
790 template <Direction DIR>
791 bool ChildIoStatementState<DIR>::Emit(const char16_t *data, std::size_t chars) {
792   return child_.parent().Emit(data, chars);
793 }
794 
795 template <Direction DIR>
796 bool ChildIoStatementState<DIR>::Emit(const char32_t *data, std::size_t chars) {
797   return child_.parent().Emit(data, chars);
798 }
799 
800 template <Direction DIR>
801 std::optional<char32_t> ChildIoStatementState<DIR>::GetCurrentChar() {
802   return child_.parent().GetCurrentChar();
803 }
804 
805 template <Direction DIR>
806 void ChildIoStatementState<DIR>::HandleAbsolutePosition(std::int64_t n) {
807   return child_.parent().HandleAbsolutePosition(n);
808 }
809 
810 template <Direction DIR>
811 void ChildIoStatementState<DIR>::HandleRelativePosition(std::int64_t n) {
812   return child_.parent().HandleRelativePosition(n);
813 }
814 
815 template <Direction DIR, typename CHAR>
816 ChildFormattedIoStatementState<DIR, CHAR>::ChildFormattedIoStatementState(
817     ChildIo &child, const CHAR *format, std::size_t formatLength,
818     const char *sourceFile, int sourceLine)
819     : ChildIoStatementState<DIR>{child, sourceFile, sourceLine},
820       mutableModes_{child.parent().mutableModes()}, format_{*this, format,
821                                                         formatLength} {}
822 
823 template <Direction DIR, typename CHAR>
824 int ChildFormattedIoStatementState<DIR, CHAR>::EndIoStatement() {
825   format_.Finish(*this);
826   return ChildIoStatementState<DIR>::EndIoStatement();
827 }
828 
829 template <Direction DIR, typename CHAR>
830 bool ChildFormattedIoStatementState<DIR, CHAR>::AdvanceRecord(int) {
831   return false; // no can do in a child I/O
832 }
833 
834 template <Direction DIR>
835 bool ChildUnformattedIoStatementState<DIR>::Receive(
836     char *data, std::size_t bytes, std::size_t elementBytes) {
837   return this->child().parent().Receive(data, bytes, elementBytes);
838 }
839 
840 template class InternalIoStatementState<Direction::Output>;
841 template class InternalIoStatementState<Direction::Input>;
842 template class InternalFormattedIoStatementState<Direction::Output>;
843 template class InternalFormattedIoStatementState<Direction::Input>;
844 template class InternalListIoStatementState<Direction::Output>;
845 template class InternalListIoStatementState<Direction::Input>;
846 template class ExternalIoStatementState<Direction::Output>;
847 template class ExternalIoStatementState<Direction::Input>;
848 template class ExternalFormattedIoStatementState<Direction::Output>;
849 template class ExternalFormattedIoStatementState<Direction::Input>;
850 template class ExternalListIoStatementState<Direction::Output>;
851 template class ExternalListIoStatementState<Direction::Input>;
852 template class ExternalUnformattedIoStatementState<Direction::Output>;
853 template class ExternalUnformattedIoStatementState<Direction::Input>;
854 template class ChildIoStatementState<Direction::Output>;
855 template class ChildIoStatementState<Direction::Input>;
856 template class ChildFormattedIoStatementState<Direction::Output>;
857 template class ChildFormattedIoStatementState<Direction::Input>;
858 template class ChildListIoStatementState<Direction::Output>;
859 template class ChildListIoStatementState<Direction::Input>;
860 template class ChildUnformattedIoStatementState<Direction::Output>;
861 template class ChildUnformattedIoStatementState<Direction::Input>;
862 
863 int ExternalMiscIoStatementState::EndIoStatement() {
864   ExternalFileUnit &ext{unit()};
865   switch (which_) {
866   case Flush:
867     ext.FlushOutput(*this);
868     std::fflush(nullptr); // flushes C stdio output streams (12.9(2))
869     break;
870   case Backspace:
871     ext.BackspaceRecord(*this);
872     break;
873   case Endfile:
874     ext.Endfile(*this);
875     break;
876   case Rewind:
877     ext.Rewind(*this);
878     break;
879   }
880   return ExternalIoStatementBase::EndIoStatement();
881 }
882 
883 InquireUnitState::InquireUnitState(
884     ExternalFileUnit &unit, const char *sourceFile, int sourceLine)
885     : ExternalIoStatementBase{unit, sourceFile, sourceLine} {}
886 
887 bool InquireUnitState::Inquire(
888     InquiryKeywordHash inquiry, char *result, std::size_t length) {
889   if (unit().createdForInternalChildIo()) {
890     SignalError(IostatInquireInternalUnit,
891         "INQUIRE of unit created for defined derived type I/O of an internal "
892         "unit");
893     return false;
894   }
895   const char *str{nullptr};
896   switch (inquiry) {
897   case HashInquiryKeyword("ACCESS"):
898     switch (unit().access) {
899     case Access::Sequential:
900       str = "SEQUENTIAL";
901       break;
902     case Access::Direct:
903       str = "DIRECT";
904       break;
905     case Access::Stream:
906       str = "STREAM";
907       break;
908     }
909     break;
910   case HashInquiryKeyword("ACTION"):
911     str = unit().mayWrite() ? unit().mayRead() ? "READWRITE" : "WRITE" : "READ";
912     break;
913   case HashInquiryKeyword("ASYNCHRONOUS"):
914     str = unit().mayAsynchronous() ? "YES" : "NO";
915     break;
916   case HashInquiryKeyword("BLANK"):
917     str = unit().isUnformatted.value_or(true)   ? "UNDEFINED"
918         : unit().modes.editingFlags & blankZero ? "ZERO"
919                                                 : "NULL";
920     break;
921   case HashInquiryKeyword("CARRIAGECONTROL"):
922     str = "LIST";
923     break;
924   case HashInquiryKeyword("CONVERT"):
925     str = unit().swapEndianness() ? "SWAP" : "NATIVE";
926     break;
927   case HashInquiryKeyword("DECIMAL"):
928     str = unit().isUnformatted.value_or(true)      ? "UNDEFINED"
929         : unit().modes.editingFlags & decimalComma ? "COMMA"
930                                                    : "POINT";
931     break;
932   case HashInquiryKeyword("DELIM"):
933     if (unit().isUnformatted.value_or(true)) {
934       str = "UNDEFINED";
935     } else {
936       switch (unit().modes.delim) {
937       case '\'':
938         str = "APOSTROPHE";
939         break;
940       case '"':
941         str = "QUOTE";
942         break;
943       default:
944         str = "NONE";
945         break;
946       }
947     }
948     break;
949   case HashInquiryKeyword("DIRECT"):
950     str = unit().access == Access::Direct ||
951             (unit().mayPosition() && unit().isFixedRecordLength)
952         ? "YES"
953         : "NO";
954     break;
955   case HashInquiryKeyword("ENCODING"):
956     str = unit().isUnformatted.value_or(true) ? "UNDEFINED"
957         : unit().isUTF8                       ? "UTF-8"
958                                               : "ASCII";
959     break;
960   case HashInquiryKeyword("FORM"):
961     str = !unit().isUnformatted ? "UNKNOWN"
962         : *unit().isUnformatted ? "UNFORMATTED"
963                                 : "FORMATTED";
964     break;
965   case HashInquiryKeyword("FORMATTED"):
966     str = !unit().isUnformatted ? "UNKNOWN"
967         : *unit().isUnformatted ? "NO"
968                                 : "YES";
969     break;
970   case HashInquiryKeyword("NAME"):
971     str = unit().path();
972     if (!str) {
973       return true; // result is undefined
974     }
975     break;
976   case HashInquiryKeyword("PAD"):
977     str = unit().isUnformatted.value_or(true) ? "UNDEFINED"
978         : unit().modes.pad                    ? "YES"
979                                               : "NO";
980     break;
981   case HashInquiryKeyword("POSITION"):
982     if (unit().access == Access::Direct) {
983       str = "UNDEFINED";
984     } else {
985       auto size{unit().knownSize()};
986       auto pos{unit().position()};
987       if (pos == size.value_or(pos + 1)) {
988         str = "APPEND";
989       } else if (pos == 0) {
990         str = "REWIND";
991       } else {
992         str = "ASIS"; // processor-dependent & no common behavior
993       }
994     }
995     break;
996   case HashInquiryKeyword("READ"):
997     str = unit().mayRead() ? "YES" : "NO";
998     break;
999   case HashInquiryKeyword("READWRITE"):
1000     str = unit().mayRead() && unit().mayWrite() ? "YES" : "NO";
1001     break;
1002   case HashInquiryKeyword("ROUND"):
1003     if (unit().isUnformatted.value_or(true)) {
1004       str = "UNDEFINED";
1005     } else {
1006       switch (unit().modes.round) {
1007       case decimal::FortranRounding::RoundNearest:
1008         str = "NEAREST";
1009         break;
1010       case decimal::FortranRounding::RoundUp:
1011         str = "UP";
1012         break;
1013       case decimal::FortranRounding::RoundDown:
1014         str = "DOWN";
1015         break;
1016       case decimal::FortranRounding::RoundToZero:
1017         str = "ZERO";
1018         break;
1019       case decimal::FortranRounding::RoundCompatible:
1020         str = "COMPATIBLE";
1021         break;
1022       }
1023     }
1024     break;
1025   case HashInquiryKeyword("SEQUENTIAL"):
1026     // "NO" for Direct, since Sequential would not work if
1027     // the unit were reopened without RECL=.
1028     str = unit().access == Access::Sequential ? "YES" : "NO";
1029     break;
1030   case HashInquiryKeyword("SIGN"):
1031     str = unit().isUnformatted.value_or(true)  ? "UNDEFINED"
1032         : unit().modes.editingFlags & signPlus ? "PLUS"
1033                                                : "SUPPRESS";
1034     break;
1035   case HashInquiryKeyword("STREAM"):
1036     str = unit().access == Access::Stream ? "YES" : "NO";
1037     break;
1038   case HashInquiryKeyword("WRITE"):
1039     str = unit().mayWrite() ? "YES" : "NO";
1040     break;
1041   case HashInquiryKeyword("UNFORMATTED"):
1042     str = !unit().isUnformatted ? "UNKNOWN"
1043         : *unit().isUnformatted ? "YES"
1044                                 : "NO";
1045     break;
1046   }
1047   if (str) {
1048     ToFortranDefaultCharacter(result, length, str);
1049     return true;
1050   } else {
1051     BadInquiryKeywordHashCrash(inquiry);
1052     return false;
1053   }
1054 }
1055 
1056 bool InquireUnitState::Inquire(InquiryKeywordHash inquiry, bool &result) {
1057   switch (inquiry) {
1058   case HashInquiryKeyword("EXIST"):
1059     result = true;
1060     return true;
1061   case HashInquiryKeyword("NAMED"):
1062     result = unit().path() != nullptr;
1063     return true;
1064   case HashInquiryKeyword("OPENED"):
1065     result = true;
1066     return true;
1067   case HashInquiryKeyword("PENDING"):
1068     result = false; // asynchronous I/O is not implemented
1069     return true;
1070   default:
1071     BadInquiryKeywordHashCrash(inquiry);
1072     return false;
1073   }
1074 }
1075 
1076 bool InquireUnitState::Inquire(
1077     InquiryKeywordHash inquiry, std::int64_t, bool &result) {
1078   switch (inquiry) {
1079   case HashInquiryKeyword("PENDING"):
1080     result = false; // asynchronous I/O is not implemented
1081     return true;
1082   default:
1083     BadInquiryKeywordHashCrash(inquiry);
1084     return false;
1085   }
1086 }
1087 
1088 bool InquireUnitState::Inquire(
1089     InquiryKeywordHash inquiry, std::int64_t &result) {
1090   switch (inquiry) {
1091   case HashInquiryKeyword("NEXTREC"):
1092     if (unit().access == Access::Direct) {
1093       result = unit().currentRecordNumber;
1094     }
1095     return true;
1096   case HashInquiryKeyword("NUMBER"):
1097     result = unit().unitNumber();
1098     return true;
1099   case HashInquiryKeyword("POS"):
1100     result = unit().position();
1101     return true;
1102   case HashInquiryKeyword("RECL"):
1103     if (unit().access == Access::Stream) {
1104       result = -2;
1105     } else if (unit().isFixedRecordLength && unit().recordLength) {
1106       result = *unit().recordLength;
1107     } else {
1108       result = std::numeric_limits<std::uint32_t>::max();
1109     }
1110     return true;
1111   case HashInquiryKeyword("SIZE"):
1112     if (auto size{unit().knownSize()}) {
1113       result = *size;
1114     } else {
1115       result = -1;
1116     }
1117     return true;
1118   default:
1119     BadInquiryKeywordHashCrash(inquiry);
1120     return false;
1121   }
1122 }
1123 
1124 InquireNoUnitState::InquireNoUnitState(const char *sourceFile, int sourceLine)
1125     : NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
1126 
1127 bool InquireNoUnitState::Inquire(
1128     InquiryKeywordHash inquiry, char *result, std::size_t length) {
1129   switch (inquiry) {
1130   case HashInquiryKeyword("ACCESS"):
1131   case HashInquiryKeyword("ACTION"):
1132   case HashInquiryKeyword("ASYNCHRONOUS"):
1133   case HashInquiryKeyword("BLANK"):
1134   case HashInquiryKeyword("CARRIAGECONTROL"):
1135   case HashInquiryKeyword("CONVERT"):
1136   case HashInquiryKeyword("DECIMAL"):
1137   case HashInquiryKeyword("DELIM"):
1138   case HashInquiryKeyword("FORM"):
1139   case HashInquiryKeyword("NAME"):
1140   case HashInquiryKeyword("PAD"):
1141   case HashInquiryKeyword("POSITION"):
1142   case HashInquiryKeyword("ROUND"):
1143   case HashInquiryKeyword("SIGN"):
1144     ToFortranDefaultCharacter(result, length, "UNDEFINED");
1145     return true;
1146   case HashInquiryKeyword("DIRECT"):
1147   case HashInquiryKeyword("ENCODING"):
1148   case HashInquiryKeyword("FORMATTED"):
1149   case HashInquiryKeyword("READ"):
1150   case HashInquiryKeyword("READWRITE"):
1151   case HashInquiryKeyword("SEQUENTIAL"):
1152   case HashInquiryKeyword("STREAM"):
1153   case HashInquiryKeyword("WRITE"):
1154   case HashInquiryKeyword("UNFORMATTED"):
1155     ToFortranDefaultCharacter(result, length, "UNKNONN");
1156     return true;
1157   default:
1158     BadInquiryKeywordHashCrash(inquiry);
1159     return false;
1160   }
1161 }
1162 
1163 bool InquireNoUnitState::Inquire(InquiryKeywordHash inquiry, bool &result) {
1164   switch (inquiry) {
1165   case HashInquiryKeyword("EXIST"):
1166     result = true;
1167     return true;
1168   case HashInquiryKeyword("NAMED"):
1169   case HashInquiryKeyword("OPENED"):
1170   case HashInquiryKeyword("PENDING"):
1171     result = false;
1172     return true;
1173   default:
1174     BadInquiryKeywordHashCrash(inquiry);
1175     return false;
1176   }
1177 }
1178 
1179 bool InquireNoUnitState::Inquire(
1180     InquiryKeywordHash inquiry, std::int64_t, bool &result) {
1181   switch (inquiry) {
1182   case HashInquiryKeyword("PENDING"):
1183     result = false;
1184     return true;
1185   default:
1186     BadInquiryKeywordHashCrash(inquiry);
1187     return false;
1188   }
1189 }
1190 
1191 bool InquireNoUnitState::Inquire(
1192     InquiryKeywordHash inquiry, std::int64_t &result) {
1193   switch (inquiry) {
1194   case HashInquiryKeyword("NEXTREC"):
1195   case HashInquiryKeyword("NUMBER"):
1196   case HashInquiryKeyword("POS"):
1197   case HashInquiryKeyword("RECL"):
1198   case HashInquiryKeyword("SIZE"):
1199     result = -1;
1200     return true;
1201   default:
1202     BadInquiryKeywordHashCrash(inquiry);
1203     return false;
1204   }
1205 }
1206 
1207 InquireUnconnectedFileState::InquireUnconnectedFileState(
1208     OwningPtr<char> &&path, const char *sourceFile, int sourceLine)
1209     : NoUnitIoStatementState{sourceFile, sourceLine, *this}, path_{std::move(
1210                                                                  path)} {}
1211 
1212 bool InquireUnconnectedFileState::Inquire(
1213     InquiryKeywordHash inquiry, char *result, std::size_t length) {
1214   const char *str{nullptr};
1215   switch (inquiry) {
1216   case HashInquiryKeyword("ACCESS"):
1217   case HashInquiryKeyword("ACTION"):
1218   case HashInquiryKeyword("ASYNCHRONOUS"):
1219   case HashInquiryKeyword("BLANK"):
1220   case HashInquiryKeyword("CARRIAGECONTROL"):
1221   case HashInquiryKeyword("CONVERT"):
1222   case HashInquiryKeyword("DECIMAL"):
1223   case HashInquiryKeyword("DELIM"):
1224   case HashInquiryKeyword("FORM"):
1225   case HashInquiryKeyword("PAD"):
1226   case HashInquiryKeyword("POSITION"):
1227   case HashInquiryKeyword("ROUND"):
1228   case HashInquiryKeyword("SIGN"):
1229     str = "UNDEFINED";
1230     break;
1231   case HashInquiryKeyword("DIRECT"):
1232   case HashInquiryKeyword("ENCODING"):
1233   case HashInquiryKeyword("FORMATTED"):
1234   case HashInquiryKeyword("SEQUENTIAL"):
1235   case HashInquiryKeyword("STREAM"):
1236   case HashInquiryKeyword("UNFORMATTED"):
1237     str = "UNKNONN";
1238     break;
1239   case HashInquiryKeyword("READ"):
1240     str = MayRead(path_.get()) ? "YES" : "NO";
1241     break;
1242   case HashInquiryKeyword("READWRITE"):
1243     str = MayReadAndWrite(path_.get()) ? "YES" : "NO";
1244     break;
1245   case HashInquiryKeyword("WRITE"):
1246     str = MayWrite(path_.get()) ? "YES" : "NO";
1247     break;
1248   case HashInquiryKeyword("NAME"):
1249     str = path_.get();
1250     return true;
1251   }
1252   if (str) {
1253     ToFortranDefaultCharacter(result, length, str);
1254     return true;
1255   } else {
1256     BadInquiryKeywordHashCrash(inquiry);
1257     return false;
1258   }
1259 }
1260 
1261 bool InquireUnconnectedFileState::Inquire(
1262     InquiryKeywordHash inquiry, bool &result) {
1263   switch (inquiry) {
1264   case HashInquiryKeyword("EXIST"):
1265     result = IsExtant(path_.get());
1266     return true;
1267   case HashInquiryKeyword("NAMED"):
1268     result = true;
1269     return true;
1270   case HashInquiryKeyword("OPENED"):
1271     result = false;
1272     return true;
1273   case HashInquiryKeyword("PENDING"):
1274     result = false;
1275     return true;
1276   default:
1277     BadInquiryKeywordHashCrash(inquiry);
1278     return false;
1279   }
1280 }
1281 
1282 bool InquireUnconnectedFileState::Inquire(
1283     InquiryKeywordHash inquiry, std::int64_t, bool &result) {
1284   switch (inquiry) {
1285   case HashInquiryKeyword("PENDING"):
1286     result = false;
1287     return true;
1288   default:
1289     BadInquiryKeywordHashCrash(inquiry);
1290     return false;
1291   }
1292 }
1293 
1294 bool InquireUnconnectedFileState::Inquire(
1295     InquiryKeywordHash inquiry, std::int64_t &result) {
1296   switch (inquiry) {
1297   case HashInquiryKeyword("NEXTREC"):
1298   case HashInquiryKeyword("NUMBER"):
1299   case HashInquiryKeyword("POS"):
1300   case HashInquiryKeyword("RECL"):
1301   case HashInquiryKeyword("SIZE"):
1302     result = -1;
1303     return true;
1304   default:
1305     BadInquiryKeywordHashCrash(inquiry);
1306     return false;
1307   }
1308 }
1309 
1310 InquireIOLengthState::InquireIOLengthState(
1311     const char *sourceFile, int sourceLine)
1312     : NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
1313 
1314 } // namespace Fortran::runtime::io
1315