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