1 //===-- runtime/io-api.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 // Implements the I/O statement API
10 
11 #include "flang/Runtime/io-api.h"
12 #include "descriptor-io.h"
13 #include "edit-input.h"
14 #include "edit-output.h"
15 #include "environment.h"
16 #include "format.h"
17 #include "io-stmt.h"
18 #include "terminator.h"
19 #include "tools.h"
20 #include "unit.h"
21 #include "flang/Runtime/descriptor.h"
22 #include "flang/Runtime/memory.h"
23 #include <cstdlib>
24 #include <memory>
25 
26 namespace Fortran::runtime::io {
27 
28 const char *InquiryKeywordHashDecode(
29     char *buffer, std::size_t n, InquiryKeywordHash hash) {
30   if (n < 1) {
31     return nullptr;
32   }
33   char *p{buffer + n};
34   *--p = '\0';
35   while (hash > 1) {
36     if (p < buffer) {
37       return nullptr;
38     }
39     *--p = 'A' + (hash % 26);
40     hash /= 26;
41   }
42   return hash == 1 ? p : nullptr;
43 }
44 
45 template <Direction DIR>
46 Cookie BeginInternalArrayListIO(const Descriptor &descriptor,
47     void ** /*scratchArea*/, std::size_t /*scratchBytes*/,
48     const char *sourceFile, int sourceLine) {
49   Terminator oom{sourceFile, sourceLine};
50   return &New<InternalListIoStatementState<DIR>>{oom}(
51       descriptor, sourceFile, sourceLine)
52               .release()
53               ->ioStatementState();
54 }
55 
56 Cookie IONAME(BeginInternalArrayListOutput)(const Descriptor &descriptor,
57     void **scratchArea, std::size_t scratchBytes, const char *sourceFile,
58     int sourceLine) {
59   return BeginInternalArrayListIO<Direction::Output>(
60       descriptor, scratchArea, scratchBytes, sourceFile, sourceLine);
61 }
62 
63 Cookie IONAME(BeginInternalArrayListInput)(const Descriptor &descriptor,
64     void **scratchArea, std::size_t scratchBytes, const char *sourceFile,
65     int sourceLine) {
66   return BeginInternalArrayListIO<Direction::Input>(
67       descriptor, scratchArea, scratchBytes, sourceFile, sourceLine);
68 }
69 
70 template <Direction DIR>
71 Cookie BeginInternalArrayFormattedIO(const Descriptor &descriptor,
72     const char *format, std::size_t formatLength, void ** /*scratchArea*/,
73     std::size_t /*scratchBytes*/, const char *sourceFile, int sourceLine) {
74   Terminator oom{sourceFile, sourceLine};
75   return &New<InternalFormattedIoStatementState<DIR>>{oom}(
76       descriptor, format, formatLength, sourceFile, sourceLine)
77               .release()
78               ->ioStatementState();
79 }
80 
81 Cookie IONAME(BeginInternalArrayFormattedOutput)(const Descriptor &descriptor,
82     const char *format, std::size_t formatLength, void **scratchArea,
83     std::size_t scratchBytes, const char *sourceFile, int sourceLine) {
84   return BeginInternalArrayFormattedIO<Direction::Output>(descriptor, format,
85       formatLength, scratchArea, scratchBytes, sourceFile, sourceLine);
86 }
87 
88 Cookie IONAME(BeginInternalArrayFormattedInput)(const Descriptor &descriptor,
89     const char *format, std::size_t formatLength, void **scratchArea,
90     std::size_t scratchBytes, const char *sourceFile, int sourceLine) {
91   return BeginInternalArrayFormattedIO<Direction::Input>(descriptor, format,
92       formatLength, scratchArea, scratchBytes, sourceFile, sourceLine);
93 }
94 
95 template <Direction DIR>
96 Cookie BeginInternalListIO(
97     std::conditional_t<DIR == Direction::Input, const char, char> *internal,
98     std::size_t internalLength, void ** /*scratchArea*/,
99     std::size_t /*scratchBytes*/, const char *sourceFile, int sourceLine) {
100   Terminator oom{sourceFile, sourceLine};
101   return &New<InternalListIoStatementState<DIR>>{oom}(
102       internal, internalLength, sourceFile, sourceLine)
103               .release()
104               ->ioStatementState();
105 }
106 
107 Cookie IONAME(BeginInternalListOutput)(char *internal,
108     std::size_t internalLength, void **scratchArea, std::size_t scratchBytes,
109     const char *sourceFile, int sourceLine) {
110   return BeginInternalListIO<Direction::Output>(internal, internalLength,
111       scratchArea, scratchBytes, sourceFile, sourceLine);
112 }
113 
114 Cookie IONAME(BeginInternalListInput)(const char *internal,
115     std::size_t internalLength, void **scratchArea, std::size_t scratchBytes,
116     const char *sourceFile, int sourceLine) {
117   return BeginInternalListIO<Direction::Input>(internal, internalLength,
118       scratchArea, scratchBytes, sourceFile, sourceLine);
119 }
120 
121 template <Direction DIR>
122 Cookie BeginInternalFormattedIO(
123     std::conditional_t<DIR == Direction::Input, const char, char> *internal,
124     std::size_t internalLength, const char *format, std::size_t formatLength,
125     void ** /*scratchArea*/, std::size_t /*scratchBytes*/,
126     const char *sourceFile, int sourceLine) {
127   Terminator oom{sourceFile, sourceLine};
128   return &New<InternalFormattedIoStatementState<DIR>>{oom}(
129       internal, internalLength, format, formatLength, sourceFile, sourceLine)
130               .release()
131               ->ioStatementState();
132 }
133 
134 Cookie IONAME(BeginInternalFormattedOutput)(char *internal,
135     std::size_t internalLength, const char *format, std::size_t formatLength,
136     void **scratchArea, std::size_t scratchBytes, const char *sourceFile,
137     int sourceLine) {
138   return BeginInternalFormattedIO<Direction::Output>(internal, internalLength,
139       format, formatLength, scratchArea, scratchBytes, sourceFile, sourceLine);
140 }
141 
142 Cookie IONAME(BeginInternalFormattedInput)(const char *internal,
143     std::size_t internalLength, const char *format, std::size_t formatLength,
144     void **scratchArea, std::size_t scratchBytes, const char *sourceFile,
145     int sourceLine) {
146   return BeginInternalFormattedIO<Direction::Input>(internal, internalLength,
147       format, formatLength, scratchArea, scratchBytes, sourceFile, sourceLine);
148 }
149 
150 template <Direction DIR, template <Direction> class STATE, typename... A>
151 Cookie BeginExternalListIO(const char *what, int unitNumber,
152     const char *sourceFile, int sourceLine, A &&...xs) {
153   Terminator terminator{sourceFile, sourceLine};
154   if (unitNumber == DefaultUnit) {
155     unitNumber = DIR == Direction::Input ? 5 : 6;
156   }
157   ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous(
158       unitNumber, DIR, false /*!unformatted*/, terminator)};
159   if (!unit.isUnformatted.has_value()) {
160     unit.isUnformatted = false;
161   }
162   if (*unit.isUnformatted) {
163     terminator.Crash("%s attempted on unformatted file", what);
164     return nullptr;
165   }
166   if (ChildIo * child{unit.GetChildIo()}) {
167     return child->CheckFormattingAndDirection(terminator, what, false, DIR)
168         ? &child->BeginIoStatement<ChildListIoStatementState<DIR>>(
169               *child, sourceFile, sourceLine)
170         : nullptr;
171   } else {
172     if (unit.access == Access::Direct) {
173       terminator.Crash("%s attempted on direct access file", what);
174       return nullptr;
175     }
176     IoErrorHandler handler{terminator};
177     unit.SetDirection(DIR, handler);
178     IoStatementState &io{unit.BeginIoStatement<STATE<DIR>>(
179         std::forward<A>(xs)..., unit, sourceFile, sourceLine)};
180     return &io;
181   }
182 }
183 
184 Cookie IONAME(BeginExternalListOutput)(
185     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
186   return BeginExternalListIO<Direction::Output, ExternalListIoStatementState>(
187       "List-directed output", unitNumber, sourceFile, sourceLine);
188 }
189 
190 Cookie IONAME(BeginExternalListInput)(
191     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
192   return BeginExternalListIO<Direction::Input, ExternalListIoStatementState>(
193       "List-directed input", unitNumber, sourceFile, sourceLine);
194 }
195 
196 template <Direction DIR>
197 Cookie BeginExternalFormattedIO(const char *format, std::size_t formatLength,
198     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
199   Terminator terminator{sourceFile, sourceLine};
200   if (unitNumber == DefaultUnit) {
201     unitNumber = DIR == Direction::Input ? 5 : 6;
202   }
203   ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous(
204       unitNumber, DIR, false /*!unformatted*/, terminator)};
205   if (!unit.isUnformatted.has_value()) {
206     unit.isUnformatted = false;
207   }
208   if (*unit.isUnformatted) {
209     terminator.Crash("Formatted I/O attempted on unformatted file");
210     return nullptr;
211   }
212   if (ChildIo * child{unit.GetChildIo()}) {
213     return child->CheckFormattingAndDirection(terminator,
214                DIR == Direction::Output ? "formatted output"
215                                         : "formatted input",
216                false, DIR)
217         ? &child->BeginIoStatement<ChildFormattedIoStatementState<DIR>>(
218               *child, format, formatLength, sourceFile, sourceLine)
219         : nullptr;
220   } else {
221     IoErrorHandler handler{terminator};
222     unit.SetDirection(DIR, handler);
223     IoStatementState &io{
224         unit.BeginIoStatement<ExternalFormattedIoStatementState<DIR>>(
225             unit, format, formatLength, sourceFile, sourceLine)};
226     return &io;
227   }
228 }
229 
230 Cookie IONAME(BeginExternalFormattedOutput)(const char *format,
231     std::size_t formatLength, ExternalUnit unitNumber, const char *sourceFile,
232     int sourceLine) {
233   return BeginExternalFormattedIO<Direction::Output>(
234       format, formatLength, unitNumber, sourceFile, sourceLine);
235 }
236 
237 Cookie IONAME(BeginExternalFormattedInput)(const char *format,
238     std::size_t formatLength, ExternalUnit unitNumber, const char *sourceFile,
239     int sourceLine) {
240   return BeginExternalFormattedIO<Direction::Input>(
241       format, formatLength, unitNumber, sourceFile, sourceLine);
242 }
243 
244 template <Direction DIR>
245 Cookie BeginUnformattedIO(
246     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
247   Terminator terminator{sourceFile, sourceLine};
248   ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous(
249       unitNumber, DIR, true /*unformatted*/, terminator)};
250   if (!unit.isUnformatted.has_value()) {
251     unit.isUnformatted = true;
252   }
253   if (!*unit.isUnformatted) {
254     terminator.Crash("Unformatted I/O attempted on formatted file");
255   }
256   if (ChildIo * child{unit.GetChildIo()}) {
257     return child->CheckFormattingAndDirection(terminator,
258                DIR == Direction::Output ? "unformatted output"
259                                         : "unformatted input",
260                true, DIR)
261         ? &child->BeginIoStatement<ChildUnformattedIoStatementState<DIR>>(
262               *child, sourceFile, sourceLine)
263         : nullptr;
264   } else {
265     IoStatementState &io{
266         unit.BeginIoStatement<ExternalUnformattedIoStatementState<DIR>>(
267             unit, sourceFile, sourceLine)};
268     IoErrorHandler handler{terminator};
269     unit.SetDirection(DIR, handler);
270     if constexpr (DIR == Direction::Output) {
271       if (unit.access == Access::Sequential) {
272         // Create space for (sub)record header to be completed by
273         // ExternalFileUnit::AdvanceRecord()
274         unit.recordLength.reset(); // in case of prior BACKSPACE
275         io.Emit("\0\0\0\0", 4); // placeholder for record length header
276       }
277     }
278     return &io;
279   }
280 }
281 
282 Cookie IONAME(BeginUnformattedOutput)(
283     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
284   return BeginUnformattedIO<Direction::Output>(
285       unitNumber, sourceFile, sourceLine);
286 }
287 
288 Cookie IONAME(BeginUnformattedInput)(
289     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
290   return BeginUnformattedIO<Direction::Input>(
291       unitNumber, sourceFile, sourceLine);
292 }
293 
294 Cookie IONAME(BeginOpenUnit)( // OPEN(without NEWUNIT=)
295     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
296   bool wasExtant{false};
297   Terminator terminator{sourceFile, sourceLine};
298   ExternalFileUnit &unit{
299       ExternalFileUnit::LookUpOrCreate(unitNumber, terminator, wasExtant)};
300   return &unit.BeginIoStatement<OpenStatementState>(
301       unit, wasExtant, sourceFile, sourceLine);
302 }
303 
304 Cookie IONAME(BeginOpenNewUnit)( // OPEN(NEWUNIT=j)
305     const char *sourceFile, int sourceLine) {
306   Terminator terminator{sourceFile, sourceLine};
307   ExternalFileUnit &unit{
308       ExternalFileUnit::NewUnit(terminator, false /*not child I/O*/)};
309   return &unit.BeginIoStatement<OpenStatementState>(
310       unit, false /*was an existing file*/, sourceFile, sourceLine);
311 }
312 
313 Cookie IONAME(BeginClose)(
314     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
315   if (ExternalFileUnit * unit{ExternalFileUnit::LookUpForClose(unitNumber)}) {
316     return &unit->BeginIoStatement<CloseStatementState>(
317         *unit, sourceFile, sourceLine);
318   } else {
319     // CLOSE(UNIT=bad unit) is just a no-op
320     Terminator oom{sourceFile, sourceLine};
321     return &New<NoopStatementState>{oom}(sourceFile, sourceLine)
322                 .release()
323                 ->ioStatementState();
324   }
325 }
326 
327 Cookie IONAME(BeginFlush)(
328     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
329   if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) {
330     return &unit->BeginIoStatement<ExternalMiscIoStatementState>(
331         *unit, ExternalMiscIoStatementState::Flush, sourceFile, sourceLine);
332   } else {
333     // FLUSH(UNIT=unknown) is a no-op
334     Terminator oom{sourceFile, sourceLine};
335     return &New<NoopStatementState>{oom}(sourceFile, sourceLine)
336                 .release()
337                 ->ioStatementState();
338   }
339 }
340 
341 Cookie IONAME(BeginBackspace)(
342     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
343   Terminator terminator{sourceFile, sourceLine};
344   ExternalFileUnit &unit{
345       ExternalFileUnit::LookUpOrCrash(unitNumber, terminator)};
346   return &unit.BeginIoStatement<ExternalMiscIoStatementState>(
347       unit, ExternalMiscIoStatementState::Backspace, sourceFile, sourceLine);
348 }
349 
350 Cookie IONAME(BeginEndfile)(
351     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
352   Terminator terminator{sourceFile, sourceLine};
353   ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous(
354       unitNumber, Direction::Output, std::nullopt, terminator)};
355   return &unit.BeginIoStatement<ExternalMiscIoStatementState>(
356       unit, ExternalMiscIoStatementState::Endfile, sourceFile, sourceLine);
357 }
358 
359 Cookie IONAME(BeginRewind)(
360     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
361   Terminator terminator{sourceFile, sourceLine};
362   ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous(
363       unitNumber, Direction::Input, std::nullopt, terminator)};
364   return &unit.BeginIoStatement<ExternalMiscIoStatementState>(
365       unit, ExternalMiscIoStatementState::Rewind, sourceFile, sourceLine);
366 }
367 
368 Cookie IONAME(BeginInquireUnit)(
369     ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
370   if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) {
371     if (ChildIo * child{unit->GetChildIo()}) {
372       return &child->BeginIoStatement<InquireUnitState>(
373           *unit, sourceFile, sourceLine);
374     } else {
375       return &unit->BeginIoStatement<InquireUnitState>(
376           *unit, sourceFile, sourceLine);
377     }
378   } else {
379     // INQUIRE(UNIT=unrecognized unit)
380     Terminator oom{sourceFile, sourceLine};
381     return &New<InquireNoUnitState>{oom}(sourceFile, sourceLine)
382                 .release()
383                 ->ioStatementState();
384   }
385 }
386 
387 Cookie IONAME(BeginInquireFile)(const char *path, std::size_t pathLength,
388     const char *sourceFile, int sourceLine) {
389   Terminator oom{sourceFile, sourceLine};
390   auto trimmed{
391       SaveDefaultCharacter(path, TrimTrailingSpaces(path, pathLength), oom)};
392   if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(trimmed.get())}) {
393     // INQUIRE(FILE=) to a connected unit
394     return &unit->BeginIoStatement<InquireUnitState>(
395         *unit, sourceFile, sourceLine);
396   } else {
397     return &New<InquireUnconnectedFileState>{oom}(
398         std::move(trimmed), sourceFile, sourceLine)
399                 .release()
400                 ->ioStatementState();
401   }
402 }
403 
404 Cookie IONAME(BeginInquireIoLength)(const char *sourceFile, int sourceLine) {
405   Terminator oom{sourceFile, sourceLine};
406   return &New<InquireIOLengthState>{oom}(sourceFile, sourceLine)
407               .release()
408               ->ioStatementState();
409 }
410 
411 // Control list items
412 
413 void IONAME(EnableHandlers)(Cookie cookie, bool hasIoStat, bool hasErr,
414     bool hasEnd, bool hasEor, bool hasIoMsg) {
415   IoErrorHandler &handler{cookie->GetIoErrorHandler()};
416   if (hasIoStat) {
417     handler.HasIoStat();
418   }
419   if (hasErr) {
420     handler.HasErrLabel();
421   }
422   if (hasEnd) {
423     handler.HasEndLabel();
424   }
425   if (hasEor) {
426     handler.HasEorLabel();
427   }
428   if (hasIoMsg) {
429     handler.HasIoMsg();
430   }
431 }
432 
433 static bool YesOrNo(const char *keyword, std::size_t length, const char *what,
434     IoErrorHandler &handler) {
435   static const char *keywords[]{"YES", "NO", nullptr};
436   switch (IdentifyValue(keyword, length, keywords)) {
437   case 0:
438     return true;
439   case 1:
440     return false;
441   default:
442     handler.SignalError(IostatErrorInKeyword, "Invalid %s='%.*s'", what,
443         static_cast<int>(length), keyword);
444     return false;
445   }
446 }
447 
448 bool IONAME(SetAdvance)(
449     Cookie cookie, const char *keyword, std::size_t length) {
450   IoStatementState &io{*cookie};
451   bool nonAdvancing{
452       !YesOrNo(keyword, length, "ADVANCE", io.GetIoErrorHandler())};
453   if (nonAdvancing && io.GetConnectionState().access == Access::Direct) {
454     io.GetIoErrorHandler().SignalError(
455         "Non-advancing I/O attempted on direct access file");
456   } else {
457     io.mutableModes().nonAdvancing = nonAdvancing;
458   }
459   return true;
460 }
461 
462 bool IONAME(SetBlank)(Cookie cookie, const char *keyword, std::size_t length) {
463   IoStatementState &io{*cookie};
464   ConnectionState &connection{io.GetConnectionState()};
465   static const char *keywords[]{"NULL", "ZERO", nullptr};
466   switch (IdentifyValue(keyword, length, keywords)) {
467   case 0:
468     connection.modes.editingFlags &= ~blankZero;
469     return true;
470   case 1:
471     connection.modes.editingFlags |= blankZero;
472     return true;
473   default:
474     io.GetIoErrorHandler().SignalError(IostatErrorInKeyword,
475         "Invalid BLANK='%.*s'", static_cast<int>(length), keyword);
476     return false;
477   }
478 }
479 
480 bool IONAME(SetDecimal)(
481     Cookie cookie, const char *keyword, std::size_t length) {
482   IoStatementState &io{*cookie};
483   ConnectionState &connection{io.GetConnectionState()};
484   static const char *keywords[]{"COMMA", "POINT", nullptr};
485   switch (IdentifyValue(keyword, length, keywords)) {
486   case 0:
487     connection.modes.editingFlags |= decimalComma;
488     return true;
489   case 1:
490     connection.modes.editingFlags &= ~decimalComma;
491     return true;
492   default:
493     io.GetIoErrorHandler().SignalError(IostatErrorInKeyword,
494         "Invalid DECIMAL='%.*s'", static_cast<int>(length), keyword);
495     return false;
496   }
497 }
498 
499 bool IONAME(SetDelim)(Cookie cookie, const char *keyword, std::size_t length) {
500   IoStatementState &io{*cookie};
501   ConnectionState &connection{io.GetConnectionState()};
502   static const char *keywords[]{"APOSTROPHE", "QUOTE", "NONE", nullptr};
503   switch (IdentifyValue(keyword, length, keywords)) {
504   case 0:
505     connection.modes.delim = '\'';
506     return true;
507   case 1:
508     connection.modes.delim = '"';
509     return true;
510   case 2:
511     connection.modes.delim = '\0';
512     return true;
513   default:
514     io.GetIoErrorHandler().SignalError(IostatErrorInKeyword,
515         "Invalid DELIM='%.*s'", static_cast<int>(length), keyword);
516     return false;
517   }
518 }
519 
520 bool IONAME(SetPad)(Cookie cookie, const char *keyword, std::size_t length) {
521   IoStatementState &io{*cookie};
522   ConnectionState &connection{io.GetConnectionState()};
523   connection.modes.pad =
524       YesOrNo(keyword, length, "PAD", io.GetIoErrorHandler());
525   return true;
526 }
527 
528 bool IONAME(SetPos)(Cookie cookie, std::int64_t pos) {
529   IoStatementState &io{*cookie};
530   ConnectionState &connection{io.GetConnectionState()};
531   IoErrorHandler &handler{io.GetIoErrorHandler()};
532   if (connection.access != Access::Stream) {
533     handler.SignalError("POS= may not appear unless ACCESS='STREAM'");
534     return false;
535   }
536   if (pos < 1) { // POS=1 is beginning of file (12.6.2.11)
537     handler.SignalError("POS=%zd is invalid", static_cast<std::intmax_t>(pos));
538     return false;
539   }
540   if (auto *unit{io.GetExternalFileUnit()}) {
541     unit->SetPosition(pos - 1, handler);
542     return true;
543   }
544   io.GetIoErrorHandler().Crash("SetPos() on internal unit");
545   return false;
546 }
547 
548 bool IONAME(SetRec)(Cookie cookie, std::int64_t rec) {
549   IoStatementState &io{*cookie};
550   ConnectionState &connection{io.GetConnectionState()};
551   IoErrorHandler &handler{io.GetIoErrorHandler()};
552   if (connection.access != Access::Direct) {
553     handler.SignalError("REC= may not appear unless ACCESS='DIRECT'");
554     return false;
555   }
556   if (!connection.openRecl) {
557     handler.SignalError("RECL= was not specified");
558     return false;
559   }
560   if (rec < 1) {
561     handler.SignalError("REC=%zd is invalid", static_cast<std::intmax_t>(rec));
562     return false;
563   }
564   connection.currentRecordNumber = rec;
565   if (auto *unit{io.GetExternalFileUnit()}) {
566     unit->SetPosition((rec - 1) * *connection.openRecl, handler);
567   }
568   return true;
569 }
570 
571 bool IONAME(SetRound)(Cookie cookie, const char *keyword, std::size_t length) {
572   IoStatementState &io{*cookie};
573   ConnectionState &connection{io.GetConnectionState()};
574   static const char *keywords[]{"UP", "DOWN", "ZERO", "NEAREST", "COMPATIBLE",
575       "PROCESSOR_DEFINED", nullptr};
576   switch (IdentifyValue(keyword, length, keywords)) {
577   case 0:
578     connection.modes.round = decimal::RoundUp;
579     return true;
580   case 1:
581     connection.modes.round = decimal::RoundDown;
582     return true;
583   case 2:
584     connection.modes.round = decimal::RoundToZero;
585     return true;
586   case 3:
587     connection.modes.round = decimal::RoundNearest;
588     return true;
589   case 4:
590     connection.modes.round = decimal::RoundCompatible;
591     return true;
592   case 5:
593     connection.modes.round = executionEnvironment.defaultOutputRoundingMode;
594     return true;
595   default:
596     io.GetIoErrorHandler().SignalError(IostatErrorInKeyword,
597         "Invalid ROUND='%.*s'", static_cast<int>(length), keyword);
598     return false;
599   }
600 }
601 
602 bool IONAME(SetSign)(Cookie cookie, const char *keyword, std::size_t length) {
603   IoStatementState &io{*cookie};
604   ConnectionState &connection{io.GetConnectionState()};
605   static const char *keywords[]{
606       "PLUS", "SUPPRESS", "PROCESSOR_DEFINED", nullptr};
607   switch (IdentifyValue(keyword, length, keywords)) {
608   case 0:
609     connection.modes.editingFlags |= signPlus;
610     return true;
611   case 1:
612   case 2: // processor default is SS
613     connection.modes.editingFlags &= ~signPlus;
614     return true;
615   default:
616     io.GetIoErrorHandler().SignalError(IostatErrorInKeyword,
617         "Invalid SIGN='%.*s'", static_cast<int>(length), keyword);
618     return false;
619   }
620 }
621 
622 bool IONAME(SetAccess)(Cookie cookie, const char *keyword, std::size_t length) {
623   IoStatementState &io{*cookie};
624   auto *open{io.get_if<OpenStatementState>()};
625   if (!open) {
626     io.GetIoErrorHandler().Crash(
627         "SetAccess() called when not in an OPEN statement");
628   }
629   static const char *keywords[]{
630       "SEQUENTIAL", "DIRECT", "STREAM", "APPEND", nullptr};
631   switch (IdentifyValue(keyword, length, keywords)) {
632   case 0:
633     open->set_access(Access::Sequential);
634     break;
635   case 1:
636     open->set_access(Access::Direct);
637     break;
638   case 2:
639     open->set_access(Access::Stream);
640     break;
641   case 3: // Sun Fortran extension ACCESS=APPEND: treat as if POSITION=APPEND
642     open->set_position(Position::Append);
643     break;
644   default:
645     open->SignalError(IostatErrorInKeyword, "Invalid ACCESS='%.*s'",
646         static_cast<int>(length), keyword);
647   }
648   return true;
649 }
650 
651 bool IONAME(SetAction)(Cookie cookie, const char *keyword, std::size_t length) {
652   IoStatementState &io{*cookie};
653   auto *open{io.get_if<OpenStatementState>()};
654   if (!open) {
655     io.GetIoErrorHandler().Crash(
656         "SetAction() called when not in an OPEN statement");
657   }
658   std::optional<Action> action;
659   static const char *keywords[]{"READ", "WRITE", "READWRITE", nullptr};
660   switch (IdentifyValue(keyword, length, keywords)) {
661   case 0:
662     action = Action::Read;
663     break;
664   case 1:
665     action = Action::Write;
666     break;
667   case 2:
668     action = Action::ReadWrite;
669     break;
670   default:
671     open->SignalError(IostatErrorInKeyword, "Invalid ACTION='%.*s'",
672         static_cast<int>(length), keyword);
673     return false;
674   }
675   RUNTIME_CHECK(io.GetIoErrorHandler(), action.has_value());
676   if (open->wasExtant()) {
677     if ((*action != Action::Write) != open->unit().mayRead() ||
678         (*action != Action::Read) != open->unit().mayWrite()) {
679       open->SignalError("ACTION= may not be changed on an open unit");
680     }
681   }
682   open->set_action(*action);
683   return true;
684 }
685 
686 bool IONAME(SetAsynchronous)(
687     Cookie cookie, const char *keyword, std::size_t length) {
688   IoStatementState &io{*cookie};
689   auto *open{io.get_if<OpenStatementState>()};
690   if (!open) {
691     io.GetIoErrorHandler().Crash(
692         "SetAsynchronous() called when not in an OPEN statement");
693   }
694   static const char *keywords[]{"YES", "NO", nullptr};
695   switch (IdentifyValue(keyword, length, keywords)) {
696   case 0:
697     open->unit().set_mayAsynchronous(true);
698     return true;
699   case 1:
700     open->unit().set_mayAsynchronous(false);
701     return true;
702   default:
703     open->SignalError(IostatErrorInKeyword, "Invalid ASYNCHRONOUS='%.*s'",
704         static_cast<int>(length), keyword);
705     return false;
706   }
707 }
708 
709 bool IONAME(SetCarriagecontrol)(
710     Cookie cookie, const char *keyword, std::size_t length) {
711   IoStatementState &io{*cookie};
712   auto *open{io.get_if<OpenStatementState>()};
713   if (!open) {
714     io.GetIoErrorHandler().Crash(
715         "SetCarriageControl() called when not in an OPEN statement");
716   }
717   static const char *keywords[]{"LIST", "FORTRAN", "NONE", nullptr};
718   switch (IdentifyValue(keyword, length, keywords)) {
719   case 0:
720     return true;
721   case 1:
722   case 2:
723     open->SignalError(IostatErrorInKeyword,
724         "Unimplemented CARRIAGECONTROL='%.*s'", static_cast<int>(length),
725         keyword);
726     return false;
727   default:
728     open->SignalError(IostatErrorInKeyword, "Invalid CARRIAGECONTROL='%.*s'",
729         static_cast<int>(length), keyword);
730     return false;
731   }
732 }
733 
734 bool IONAME(SetConvert)(
735     Cookie cookie, const char *keyword, std::size_t length) {
736   IoStatementState &io{*cookie};
737   auto *open{io.get_if<OpenStatementState>()};
738   if (!open) {
739     io.GetIoErrorHandler().Crash(
740         "SetConvert() called when not in an OPEN statement");
741   }
742   if (auto convert{GetConvertFromString(keyword, length)}) {
743     open->set_convert(*convert);
744     return true;
745   } else {
746     open->SignalError(IostatErrorInKeyword, "Invalid CONVERT='%.*s'",
747         static_cast<int>(length), keyword);
748     return false;
749   }
750 }
751 
752 bool IONAME(SetEncoding)(
753     Cookie cookie, const char *keyword, std::size_t length) {
754   IoStatementState &io{*cookie};
755   auto *open{io.get_if<OpenStatementState>()};
756   if (!open) {
757     io.GetIoErrorHandler().Crash(
758         "SetEncoding() called when not in an OPEN statement");
759   }
760   bool isUTF8{false};
761   static const char *keywords[]{"UTF-8", "DEFAULT", nullptr};
762   switch (IdentifyValue(keyword, length, keywords)) {
763   case 0:
764     isUTF8 = true;
765     break;
766   case 1:
767     isUTF8 = false;
768     break;
769   default:
770     open->SignalError(IostatErrorInKeyword, "Invalid ENCODING='%.*s'",
771         static_cast<int>(length), keyword);
772   }
773   if (isUTF8 != open->unit().isUTF8) {
774     if (open->wasExtant()) {
775       open->SignalError("ENCODING= may not be changed on an open unit");
776     }
777     open->unit().isUTF8 = isUTF8;
778   }
779   return true;
780 }
781 
782 bool IONAME(SetForm)(Cookie cookie, const char *keyword, std::size_t length) {
783   IoStatementState &io{*cookie};
784   auto *open{io.get_if<OpenStatementState>()};
785   if (!open) {
786     io.GetIoErrorHandler().Crash(
787         "SetForm() called when not in an OPEN statement");
788   }
789   static const char *keywords[]{"FORMATTED", "UNFORMATTED", nullptr};
790   switch (IdentifyValue(keyword, length, keywords)) {
791   case 0:
792     open->set_isUnformatted(false);
793     break;
794   case 1:
795     open->set_isUnformatted(true);
796     break;
797   default:
798     open->SignalError(IostatErrorInKeyword, "Invalid FORM='%.*s'",
799         static_cast<int>(length), keyword);
800   }
801   return true;
802 }
803 
804 bool IONAME(SetPosition)(
805     Cookie cookie, const char *keyword, std::size_t length) {
806   IoStatementState &io{*cookie};
807   auto *open{io.get_if<OpenStatementState>()};
808   if (!open) {
809     io.GetIoErrorHandler().Crash(
810         "SetPosition() called when not in an OPEN statement");
811   }
812   static const char *positions[]{"ASIS", "REWIND", "APPEND", nullptr};
813   switch (IdentifyValue(keyword, length, positions)) {
814   case 0:
815     open->set_position(Position::AsIs);
816     return true;
817   case 1:
818     open->set_position(Position::Rewind);
819     return true;
820   case 2:
821     open->set_position(Position::Append);
822     return true;
823   default:
824     io.GetIoErrorHandler().SignalError(IostatErrorInKeyword,
825         "Invalid POSITION='%.*s'", static_cast<int>(length), keyword);
826   }
827   return true;
828 }
829 
830 bool IONAME(SetRecl)(Cookie cookie, std::size_t n) {
831   IoStatementState &io{*cookie};
832   auto *open{io.get_if<OpenStatementState>()};
833   if (!open) {
834     io.GetIoErrorHandler().Crash(
835         "SetRecl() called when not in an OPEN statement");
836   }
837   if (n <= 0) {
838     io.GetIoErrorHandler().SignalError("RECL= must be greater than zero");
839     return false;
840   } else if (open->wasExtant() &&
841       open->unit().openRecl.value_or(0) != static_cast<std::int64_t>(n)) {
842     open->SignalError("RECL= may not be changed for an open unit");
843     return false;
844   } else {
845     open->unit().openRecl = n;
846     return true;
847   }
848 }
849 
850 bool IONAME(SetStatus)(Cookie cookie, const char *keyword, std::size_t length) {
851   IoStatementState &io{*cookie};
852   if (auto *open{io.get_if<OpenStatementState>()}) {
853     static const char *statuses[]{
854         "OLD", "NEW", "SCRATCH", "REPLACE", "UNKNOWN", nullptr};
855     switch (IdentifyValue(keyword, length, statuses)) {
856     case 0:
857       open->set_status(OpenStatus::Old);
858       return true;
859     case 1:
860       open->set_status(OpenStatus::New);
861       return true;
862     case 2:
863       open->set_status(OpenStatus::Scratch);
864       return true;
865     case 3:
866       open->set_status(OpenStatus::Replace);
867       return true;
868     case 4:
869       open->set_status(OpenStatus::Unknown);
870       return true;
871     default:
872       io.GetIoErrorHandler().SignalError(IostatErrorInKeyword,
873           "Invalid STATUS='%.*s'", static_cast<int>(length), keyword);
874     }
875     return false;
876   }
877   if (auto *close{io.get_if<CloseStatementState>()}) {
878     static const char *statuses[]{"KEEP", "DELETE", nullptr};
879     switch (IdentifyValue(keyword, length, statuses)) {
880     case 0:
881       close->set_status(CloseStatus::Keep);
882       return true;
883     case 1:
884       close->set_status(CloseStatus::Delete);
885       return true;
886     default:
887       io.GetIoErrorHandler().SignalError(IostatErrorInKeyword,
888           "Invalid STATUS='%.*s'", static_cast<int>(length), keyword);
889     }
890     return false;
891   }
892   if (io.get_if<NoopStatementState>()) {
893     return true; // don't bother validating STATUS= in a no-op CLOSE
894   }
895   io.GetIoErrorHandler().Crash(
896       "SetStatus() called when not in an OPEN or CLOSE statement");
897 }
898 
899 bool IONAME(SetFile)(Cookie cookie, const char *path, std::size_t chars) {
900   IoStatementState &io{*cookie};
901   if (auto *open{io.get_if<OpenStatementState>()}) {
902     open->set_path(path, chars);
903     return true;
904   }
905   io.GetIoErrorHandler().Crash(
906       "SetFile() called when not in an OPEN statement");
907   return false;
908 }
909 
910 bool IONAME(GetNewUnit)(Cookie cookie, int &unit, int kind) {
911   IoStatementState &io{*cookie};
912   auto *open{io.get_if<OpenStatementState>()};
913   if (!open) {
914     io.GetIoErrorHandler().Crash(
915         "GetNewUnit() called when not in an OPEN statement");
916   }
917   if (!SetInteger(unit, kind, open->unit().unitNumber())) {
918     open->SignalError("GetNewUnit(): Bad INTEGER kind(%d) for result");
919   }
920   return true;
921 }
922 
923 // Data transfers
924 
925 bool IONAME(OutputDescriptor)(Cookie cookie, const Descriptor &descriptor) {
926   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
927 }
928 
929 bool IONAME(InputDescriptor)(Cookie cookie, const Descriptor &descriptor) {
930   return descr::DescriptorIO<Direction::Input>(*cookie, descriptor);
931 }
932 
933 bool IONAME(OutputUnformattedBlock)(Cookie cookie, const char *x,
934     std::size_t length, std::size_t elementBytes) {
935   IoStatementState &io{*cookie};
936   if (auto *unf{io.get_if<
937           ExternalUnformattedIoStatementState<Direction::Output>>()}) {
938     return unf->Emit(x, length, elementBytes);
939   } else if (auto *inq{io.get_if<InquireIOLengthState>()}) {
940     return inq->Emit(x, length, elementBytes);
941   }
942   io.GetIoErrorHandler().Crash("OutputUnformattedBlock() called for an I/O "
943                                "statement that is not unformatted output");
944   return false;
945 }
946 
947 bool IONAME(InputUnformattedBlock)(
948     Cookie cookie, char *x, std::size_t length, std::size_t elementBytes) {
949   IoStatementState &io{*cookie};
950   io.BeginReadingRecord();
951   if (io.GetIoErrorHandler().InError()) {
952     return false;
953   }
954   if (auto *unf{
955           io.get_if<ExternalUnformattedIoStatementState<Direction::Input>>()}) {
956     return unf->Receive(x, length, elementBytes);
957   }
958   io.GetIoErrorHandler().Crash("InputUnformattedBlock() called for an I/O "
959                                "statement that is not unformatted output");
960   return false;
961 }
962 
963 bool IONAME(OutputInteger8)(Cookie cookie, std::int8_t n) {
964   cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger8");
965   StaticDescriptor staticDescriptor;
966   Descriptor &descriptor{staticDescriptor.descriptor()};
967   descriptor.Establish(
968       TypeCategory::Integer, 1, reinterpret_cast<void *>(&n), 0);
969   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
970 }
971 
972 bool IONAME(OutputInteger16)(Cookie cookie, std::int16_t n) {
973   cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger16");
974   StaticDescriptor staticDescriptor;
975   Descriptor &descriptor{staticDescriptor.descriptor()};
976   descriptor.Establish(
977       TypeCategory::Integer, 2, reinterpret_cast<void *>(&n), 0);
978   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
979 }
980 
981 bool IONAME(OutputInteger32)(Cookie cookie, std::int32_t n) {
982   cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger32");
983   StaticDescriptor staticDescriptor;
984   Descriptor &descriptor{staticDescriptor.descriptor()};
985   descriptor.Establish(
986       TypeCategory::Integer, 4, reinterpret_cast<void *>(&n), 0);
987   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
988 }
989 
990 bool IONAME(OutputInteger64)(Cookie cookie, std::int64_t n) {
991   cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger64");
992   StaticDescriptor staticDescriptor;
993   Descriptor &descriptor{staticDescriptor.descriptor()};
994   descriptor.Establish(
995       TypeCategory::Integer, 8, reinterpret_cast<void *>(&n), 0);
996   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
997 }
998 
999 #ifdef __SIZEOF_INT128__
1000 bool IONAME(OutputInteger128)(Cookie cookie, common::int128_t n) {
1001   cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger128");
1002   StaticDescriptor staticDescriptor;
1003   Descriptor &descriptor{staticDescriptor.descriptor()};
1004   descriptor.Establish(
1005       TypeCategory::Integer, 16, reinterpret_cast<void *>(&n), 0);
1006   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
1007 }
1008 #endif
1009 
1010 bool IONAME(InputInteger)(Cookie cookie, std::int64_t &n, int kind) {
1011   cookie->CheckFormattedStmtType<Direction::Input>("InputInteger");
1012   StaticDescriptor staticDescriptor;
1013   Descriptor &descriptor{staticDescriptor.descriptor()};
1014   descriptor.Establish(
1015       TypeCategory::Integer, kind, reinterpret_cast<void *>(&n), 0);
1016   return descr::DescriptorIO<Direction::Input>(*cookie, descriptor);
1017 }
1018 
1019 bool IONAME(OutputReal32)(Cookie cookie, float x) {
1020   cookie->CheckFormattedStmtType<Direction::Output>("OutputReal32");
1021   StaticDescriptor staticDescriptor;
1022   Descriptor &descriptor{staticDescriptor.descriptor()};
1023   descriptor.Establish(TypeCategory::Real, 4, reinterpret_cast<void *>(&x), 0);
1024   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
1025 }
1026 
1027 bool IONAME(OutputReal64)(Cookie cookie, double x) {
1028   cookie->CheckFormattedStmtType<Direction::Output>("OutputReal64");
1029   StaticDescriptor staticDescriptor;
1030   Descriptor &descriptor{staticDescriptor.descriptor()};
1031   descriptor.Establish(TypeCategory::Real, 8, reinterpret_cast<void *>(&x), 0);
1032   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
1033 }
1034 
1035 bool IONAME(InputReal32)(Cookie cookie, float &x) {
1036   cookie->CheckFormattedStmtType<Direction::Input>("InputReal32");
1037   StaticDescriptor staticDescriptor;
1038   Descriptor &descriptor{staticDescriptor.descriptor()};
1039   descriptor.Establish(TypeCategory::Real, 4, reinterpret_cast<void *>(&x), 0);
1040   return descr::DescriptorIO<Direction::Input>(*cookie, descriptor);
1041 }
1042 
1043 bool IONAME(InputReal64)(Cookie cookie, double &x) {
1044   cookie->CheckFormattedStmtType<Direction::Input>("InputReal64");
1045   StaticDescriptor staticDescriptor;
1046   Descriptor &descriptor{staticDescriptor.descriptor()};
1047   descriptor.Establish(TypeCategory::Real, 8, reinterpret_cast<void *>(&x), 0);
1048   return descr::DescriptorIO<Direction::Input>(*cookie, descriptor);
1049 }
1050 
1051 bool IONAME(OutputComplex32)(Cookie cookie, float r, float i) {
1052   cookie->CheckFormattedStmtType<Direction::Output>("OutputComplex32");
1053   float z[2]{r, i};
1054   StaticDescriptor staticDescriptor;
1055   Descriptor &descriptor{staticDescriptor.descriptor()};
1056   descriptor.Establish(
1057       TypeCategory::Complex, 4, reinterpret_cast<void *>(&z), 0);
1058   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
1059 }
1060 
1061 bool IONAME(OutputComplex64)(Cookie cookie, double r, double i) {
1062   cookie->CheckFormattedStmtType<Direction::Output>("OutputComplex64");
1063   double z[2]{r, i};
1064   StaticDescriptor staticDescriptor;
1065   Descriptor &descriptor{staticDescriptor.descriptor()};
1066   descriptor.Establish(
1067       TypeCategory::Complex, 8, reinterpret_cast<void *>(&z), 0);
1068   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
1069 }
1070 
1071 bool IONAME(InputComplex32)(Cookie cookie, float z[2]) {
1072   cookie->CheckFormattedStmtType<Direction::Input>("InputComplex32");
1073   StaticDescriptor staticDescriptor;
1074   Descriptor &descriptor{staticDescriptor.descriptor()};
1075   descriptor.Establish(
1076       TypeCategory::Complex, 4, reinterpret_cast<void *>(z), 0);
1077   return descr::DescriptorIO<Direction::Input>(*cookie, descriptor);
1078 }
1079 
1080 bool IONAME(InputComplex64)(Cookie cookie, double z[2]) {
1081   cookie->CheckFormattedStmtType<Direction::Input>("InputComplex64");
1082   StaticDescriptor staticDescriptor;
1083   Descriptor &descriptor{staticDescriptor.descriptor()};
1084   descriptor.Establish(
1085       TypeCategory::Complex, 8, reinterpret_cast<void *>(z), 0);
1086   return descr::DescriptorIO<Direction::Input>(*cookie, descriptor);
1087 }
1088 
1089 bool IONAME(OutputCharacter)(
1090     Cookie cookie, const char *x, std::size_t length, int kind) {
1091   cookie->CheckFormattedStmtType<Direction::Output>("OutputCharacter");
1092   StaticDescriptor staticDescriptor;
1093   Descriptor &descriptor{staticDescriptor.descriptor()};
1094   descriptor.Establish(
1095       kind, length, reinterpret_cast<void *>(const_cast<char *>(x)), 0);
1096   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
1097 }
1098 
1099 bool IONAME(OutputAscii)(Cookie cookie, const char *x, std::size_t length) {
1100   return IONAME(OutputCharacter(cookie, x, length, 1));
1101 }
1102 
1103 bool IONAME(InputCharacter)(
1104     Cookie cookie, char *x, std::size_t length, int kind) {
1105   cookie->CheckFormattedStmtType<Direction::Input>("InputCharacter");
1106   StaticDescriptor staticDescriptor;
1107   Descriptor &descriptor{staticDescriptor.descriptor()};
1108   descriptor.Establish(kind, length, reinterpret_cast<void *>(x), 0);
1109   return descr::DescriptorIO<Direction::Input>(*cookie, descriptor);
1110 }
1111 
1112 bool IONAME(InputAscii)(Cookie cookie, char *x, std::size_t length) {
1113   return IONAME(InputCharacter(cookie, x, length, 1));
1114 }
1115 
1116 bool IONAME(OutputLogical)(Cookie cookie, bool truth) {
1117   cookie->CheckFormattedStmtType<Direction::Output>("OutputLogical");
1118   StaticDescriptor staticDescriptor;
1119   Descriptor &descriptor{staticDescriptor.descriptor()};
1120   descriptor.Establish(
1121       TypeCategory::Logical, sizeof truth, reinterpret_cast<void *>(&truth), 0);
1122   return descr::DescriptorIO<Direction::Output>(*cookie, descriptor);
1123 }
1124 
1125 bool IONAME(InputLogical)(Cookie cookie, bool &truth) {
1126   cookie->CheckFormattedStmtType<Direction::Input>("InputLogical");
1127   StaticDescriptor staticDescriptor;
1128   Descriptor &descriptor{staticDescriptor.descriptor()};
1129   descriptor.Establish(
1130       TypeCategory::Logical, sizeof truth, reinterpret_cast<void *>(&truth), 0);
1131   return descr::DescriptorIO<Direction::Input>(*cookie, descriptor);
1132 }
1133 
1134 std::size_t IONAME(GetSize)(Cookie cookie) {
1135   IoStatementState &io{*cookie};
1136   if (const auto *formatted{
1137           io.get_if<FormattedIoStatementState<Direction::Input>>()}) {
1138     return formatted->GetEditDescriptorChars();
1139   }
1140   io.GetIoErrorHandler().Crash(
1141       "GetIoSize() called for an I/O statement that is not a formatted READ()");
1142   return 0;
1143 }
1144 
1145 std::size_t IONAME(GetIoLength)(Cookie cookie) {
1146   IoStatementState &io{*cookie};
1147   if (const auto *inq{io.get_if<InquireIOLengthState>()}) {
1148     return inq->bytes();
1149   }
1150   io.GetIoErrorHandler().Crash("GetIoLength() called for an I/O statement that "
1151                                "is not INQUIRE(IOLENGTH=)");
1152   return 0;
1153 }
1154 
1155 void IONAME(GetIoMsg)(Cookie cookie, char *msg, std::size_t length) {
1156   IoErrorHandler &handler{cookie->GetIoErrorHandler()};
1157   if (handler.InError()) { // leave "msg" alone when no error
1158     handler.GetIoMsg(msg, length);
1159   }
1160 }
1161 
1162 bool IONAME(InquireCharacter)(Cookie cookie, InquiryKeywordHash inquiry,
1163     char *result, std::size_t length) {
1164   IoStatementState &io{*cookie};
1165   return io.Inquire(inquiry, result, length);
1166 }
1167 
1168 bool IONAME(InquireLogical)(
1169     Cookie cookie, InquiryKeywordHash inquiry, bool &result) {
1170   IoStatementState &io{*cookie};
1171   return io.Inquire(inquiry, result);
1172 }
1173 
1174 bool IONAME(InquirePendingId)(Cookie cookie, std::int64_t id, bool &result) {
1175   IoStatementState &io{*cookie};
1176   return io.Inquire(HashInquiryKeyword("PENDING"), id, result);
1177 }
1178 
1179 bool IONAME(InquireInteger64)(
1180     Cookie cookie, InquiryKeywordHash inquiry, std::int64_t &result, int kind) {
1181   IoStatementState &io{*cookie};
1182   std::int64_t n;
1183   if (io.Inquire(inquiry, n)) {
1184     SetInteger(result, kind, n);
1185     return true;
1186   }
1187   return false;
1188 }
1189 
1190 enum Iostat IONAME(EndIoStatement)(Cookie cookie) {
1191   IoStatementState &io{*cookie};
1192   return static_cast<enum Iostat>(io.EndIoStatement());
1193 }
1194 } // namespace Fortran::runtime::io
1195