llvm-project
795 строк · 26.9 Кб
1//===-- runtime/unit.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// Implementation of ExternalFileUnit common for both
10// RT_USE_PSEUDO_FILE_UNIT=0 and RT_USE_PSEUDO_FILE_UNIT=1.
11//
12//===----------------------------------------------------------------------===//
13#include "unit.h"
14#include "io-error.h"
15#include "lock.h"
16#include "tools.h"
17#include <limits>
18#include <utility>
19
20namespace Fortran::runtime::io {
21
22#ifndef FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS
23RT_OFFLOAD_VAR_GROUP_BEGIN
24RT_VAR_ATTRS ExternalFileUnit *defaultInput{nullptr}; // unit 5
25RT_VAR_ATTRS ExternalFileUnit *defaultOutput{nullptr}; // unit 6
26RT_VAR_ATTRS ExternalFileUnit *errorOutput{nullptr}; // unit 0 extension
27RT_OFFLOAD_VAR_GROUP_END
28#endif // FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS
29
30RT_OFFLOAD_API_GROUP_BEGIN
31
32static inline RT_API_ATTRS void SwapEndianness(
33char *data, std::size_t bytes, std::size_t elementBytes) {
34if (elementBytes > 1) {
35auto half{elementBytes >> 1};
36for (std::size_t j{0}; j + elementBytes <= bytes; j += elementBytes) {
37for (std::size_t k{0}; k < half; ++k) {
38RT_DIAG_PUSH
39RT_DIAG_DISABLE_CALL_HOST_FROM_DEVICE_WARN
40std::swap(data[j + k], data[j + elementBytes - 1 - k]);
41RT_DIAG_POP
42}
43}
44}
45}
46
47bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
48std::size_t elementBytes, IoErrorHandler &handler) {
49auto furthestAfter{std::max(furthestPositionInRecord,
50positionInRecord + static_cast<std::int64_t>(bytes))};
51if (openRecl) {
52// Check for fixed-length record overrun, but allow for
53// sequential record termination.
54int extra{0};
55int header{0};
56if (access == Access::Sequential) {
57if (isUnformatted.value_or(false)) {
58// record header + footer
59header = static_cast<int>(sizeof(std::uint32_t));
60extra = 2 * header;
61} else {
62#ifdef _WIN32
63if (!isWindowsTextFile()) {
64++extra; // carriage return (CR)
65}
66#endif
67++extra; // newline (LF)
68}
69}
70if (furthestAfter > extra + *openRecl) {
71handler.SignalError(IostatRecordWriteOverrun,
72"Attempt to write %zd bytes to position %jd in a fixed-size record "
73"of %jd bytes",
74bytes, static_cast<std::intmax_t>(positionInRecord - header),
75static_cast<std::intmax_t>(*openRecl));
76return false;
77}
78}
79if (recordLength) {
80// It is possible for recordLength to have a value now for a
81// variable-length output record if the previous operation
82// was a BACKSPACE or non advancing input statement.
83recordLength.reset();
84beganReadingRecord_ = false;
85}
86if (IsAfterEndfile()) {
87handler.SignalError(IostatWriteAfterEndfile);
88return false;
89}
90CheckDirectAccess(handler);
91WriteFrame(frameOffsetInFile_, recordOffsetInFrame_ + furthestAfter, handler);
92if (positionInRecord > furthestPositionInRecord) {
93std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord, ' ',
94positionInRecord - furthestPositionInRecord);
95}
96char *to{Frame() + recordOffsetInFrame_ + positionInRecord};
97std::memcpy(to, data, bytes);
98if (swapEndianness_) {
99SwapEndianness(to, bytes, elementBytes);
100}
101positionInRecord += bytes;
102furthestPositionInRecord = furthestAfter;
103anyWriteSinceLastPositioning_ = true;
104return true;
105}
106
107bool ExternalFileUnit::Receive(char *data, std::size_t bytes,
108std::size_t elementBytes, IoErrorHandler &handler) {
109RUNTIME_CHECK(handler, direction_ == Direction::Input);
110auto furthestAfter{std::max(furthestPositionInRecord,
111positionInRecord + static_cast<std::int64_t>(bytes))};
112if (furthestAfter > recordLength.value_or(furthestAfter)) {
113handler.SignalError(IostatRecordReadOverrun,
114"Attempt to read %zd bytes at position %jd in a record of %jd bytes",
115bytes, static_cast<std::intmax_t>(positionInRecord),
116static_cast<std::intmax_t>(*recordLength));
117return false;
118}
119auto need{recordOffsetInFrame_ + furthestAfter};
120auto got{ReadFrame(frameOffsetInFile_, need, handler)};
121if (got >= need) {
122std::memcpy(data, Frame() + recordOffsetInFrame_ + positionInRecord, bytes);
123if (swapEndianness_) {
124SwapEndianness(data, bytes, elementBytes);
125}
126positionInRecord += bytes;
127furthestPositionInRecord = furthestAfter;
128return true;
129} else {
130HitEndOnRead(handler);
131return false;
132}
133}
134
135std::size_t ExternalFileUnit::GetNextInputBytes(
136const char *&p, IoErrorHandler &handler) {
137RUNTIME_CHECK(handler, direction_ == Direction::Input);
138std::size_t length{1};
139if (auto recl{EffectiveRecordLength()}) {
140if (positionInRecord < *recl) {
141length = *recl - positionInRecord;
142} else {
143p = nullptr;
144return 0;
145}
146}
147p = FrameNextInput(handler, length);
148return p ? length : 0;
149}
150
151const char *ExternalFileUnit::FrameNextInput(
152IoErrorHandler &handler, std::size_t bytes) {
153RUNTIME_CHECK(handler, isUnformatted.has_value() && !*isUnformatted);
154if (static_cast<std::int64_t>(positionInRecord + bytes) <=
155recordLength.value_or(positionInRecord + bytes)) {
156auto at{recordOffsetInFrame_ + positionInRecord};
157auto need{static_cast<std::size_t>(at + bytes)};
158auto got{ReadFrame(frameOffsetInFile_, need, handler)};
159SetVariableFormattedRecordLength();
160if (got >= need) {
161return Frame() + at;
162}
163HitEndOnRead(handler);
164}
165return nullptr;
166}
167
168bool ExternalFileUnit::SetVariableFormattedRecordLength() {
169if (recordLength || access == Access::Direct) {
170return true;
171} else if (FrameLength() > recordOffsetInFrame_) {
172const char *record{Frame() + recordOffsetInFrame_};
173std::size_t bytes{FrameLength() - recordOffsetInFrame_};
174if (const char *nl{FindCharacter(record, '\n', bytes)}) {
175recordLength = nl - record;
176if (*recordLength > 0 && record[*recordLength - 1] == '\r') {
177--*recordLength;
178}
179return true;
180}
181}
182return false;
183}
184
185bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
186RUNTIME_CHECK(handler, direction_ == Direction::Input);
187if (!beganReadingRecord_) {
188beganReadingRecord_ = true;
189// Don't use IsAtEOF() to check for an EOF condition here, just detect
190// it from a failed or short read from the file. IsAtEOF() could be
191// wrong for formatted input if actual newline characters had been
192// written in-band by previous WRITEs before a REWIND. In fact,
193// now that we know that the unit is being used for input (again),
194// it's best to reset endfileRecordNumber and ensure IsAtEOF() will
195// now be true on return only if it gets set by HitEndOnRead().
196endfileRecordNumber.reset();
197if (access == Access::Direct) {
198CheckDirectAccess(handler);
199auto need{static_cast<std::size_t>(recordOffsetInFrame_ + *openRecl)};
200auto got{ReadFrame(frameOffsetInFile_, need, handler)};
201if (got >= need) {
202recordLength = openRecl;
203} else {
204recordLength.reset();
205HitEndOnRead(handler);
206}
207} else {
208if (anyWriteSinceLastPositioning_ && access == Access::Sequential) {
209// Most Fortran implementations allow a READ after a WRITE;
210// the read then just hits an EOF.
211DoEndfile<false, Direction::Input>(handler);
212}
213recordLength.reset();
214RUNTIME_CHECK(handler, isUnformatted.has_value());
215if (*isUnformatted) {
216if (access == Access::Sequential) {
217BeginSequentialVariableUnformattedInputRecord(handler);
218}
219} else { // formatted sequential or stream
220BeginVariableFormattedInputRecord(handler);
221}
222}
223}
224RUNTIME_CHECK(handler,
225recordLength.has_value() || !IsRecordFile() || handler.InError());
226return !handler.InError();
227}
228
229void ExternalFileUnit::FinishReadingRecord(IoErrorHandler &handler) {
230RUNTIME_CHECK(handler, direction_ == Direction::Input && beganReadingRecord_);
231beganReadingRecord_ = false;
232if (handler.GetIoStat() == IostatEnd ||
233(IsRecordFile() && !recordLength.has_value())) {
234// Avoid bogus crashes in END/ERR circumstances; but
235// still increment the current record number so that
236// an attempted read of an endfile record, followed by
237// a BACKSPACE, will still be at EOF.
238++currentRecordNumber;
239} else if (IsRecordFile()) {
240recordOffsetInFrame_ += *recordLength;
241if (access != Access::Direct) {
242RUNTIME_CHECK(handler, isUnformatted.has_value());
243recordLength.reset();
244if (isUnformatted.value_or(false)) {
245// Retain footer in frame for more efficient BACKSPACE
246frameOffsetInFile_ += recordOffsetInFrame_;
247recordOffsetInFrame_ = sizeof(std::uint32_t);
248} else { // formatted
249if (FrameLength() > recordOffsetInFrame_ &&
250Frame()[recordOffsetInFrame_] == '\r') {
251++recordOffsetInFrame_;
252}
253if (FrameLength() > recordOffsetInFrame_ &&
254Frame()[recordOffsetInFrame_] == '\n') {
255++recordOffsetInFrame_;
256}
257if (!pinnedFrame || mayPosition()) {
258frameOffsetInFile_ += recordOffsetInFrame_;
259recordOffsetInFrame_ = 0;
260}
261}
262}
263++currentRecordNumber;
264} else { // unformatted stream
265furthestPositionInRecord =
266std::max(furthestPositionInRecord, positionInRecord);
267frameOffsetInFile_ += recordOffsetInFrame_ + furthestPositionInRecord;
268recordOffsetInFrame_ = 0;
269}
270BeginRecord();
271}
272
273bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
274if (direction_ == Direction::Input) {
275FinishReadingRecord(handler);
276return BeginReadingRecord(handler);
277} else { // Direction::Output
278bool ok{true};
279RUNTIME_CHECK(handler, isUnformatted.has_value());
280positionInRecord = furthestPositionInRecord;
281if (access == Access::Direct) {
282if (furthestPositionInRecord <
283openRecl.value_or(furthestPositionInRecord)) {
284// Pad remainder of fixed length record
285WriteFrame(
286frameOffsetInFile_, recordOffsetInFrame_ + *openRecl, handler);
287std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
288isUnformatted.value_or(false) ? 0 : ' ',
289*openRecl - furthestPositionInRecord);
290furthestPositionInRecord = *openRecl;
291}
292} else if (*isUnformatted) {
293if (access == Access::Sequential) {
294// Append the length of a sequential unformatted variable-length record
295// as its footer, then overwrite the reserved first four bytes of the
296// record with its length as its header. These four bytes were skipped
297// over in BeginUnformattedIO<Output>().
298// TODO: Break very large records up into subrecords with negative
299// headers &/or footers
300std::uint32_t length;
301length = furthestPositionInRecord - sizeof length;
302ok = ok &&
303Emit(reinterpret_cast<const char *>(&length), sizeof length,
304sizeof length, handler);
305positionInRecord = 0;
306ok = ok &&
307Emit(reinterpret_cast<const char *>(&length), sizeof length,
308sizeof length, handler);
309} else {
310// Unformatted stream: nothing to do
311}
312} else if (handler.GetIoStat() != IostatOk &&
313furthestPositionInRecord == 0) {
314// Error in formatted variable length record, and no output yet; do
315// nothing, like most other Fortran compilers do.
316return true;
317} else {
318// Terminate formatted variable length record
319const char *lineEnding{"\n"};
320std::size_t lineEndingBytes{1};
321#ifdef _WIN32
322if (!isWindowsTextFile()) {
323lineEnding = "\r\n";
324lineEndingBytes = 2;
325}
326#endif
327ok = ok && Emit(lineEnding, lineEndingBytes, 1, handler);
328}
329leftTabLimit.reset();
330if (IsAfterEndfile()) {
331return false;
332}
333CommitWrites();
334++currentRecordNumber;
335if (access != Access::Direct) {
336impliedEndfile_ = IsRecordFile();
337if (IsAtEOF()) {
338endfileRecordNumber.reset();
339}
340}
341return ok;
342}
343}
344
345void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
346if (access == Access::Direct || !IsRecordFile()) {
347handler.SignalError(IostatBackspaceNonSequential,
348"BACKSPACE(UNIT=%d) on direct-access file or unformatted stream",
349unitNumber());
350} else {
351if (IsAfterEndfile()) {
352// BACKSPACE after explicit ENDFILE
353currentRecordNumber = *endfileRecordNumber;
354} else if (leftTabLimit && direction_ == Direction::Input) {
355// BACKSPACE after non-advancing input
356leftTabLimit.reset();
357} else {
358DoImpliedEndfile(handler);
359if (frameOffsetInFile_ + recordOffsetInFrame_ > 0) {
360--currentRecordNumber;
361if (openRecl && access == Access::Direct) {
362BackspaceFixedRecord(handler);
363} else {
364RUNTIME_CHECK(handler, isUnformatted.has_value());
365if (isUnformatted.value_or(false)) {
366BackspaceVariableUnformattedRecord(handler);
367} else {
368BackspaceVariableFormattedRecord(handler);
369}
370}
371}
372}
373BeginRecord();
374anyWriteSinceLastPositioning_ = false;
375}
376}
377
378void ExternalFileUnit::FlushOutput(IoErrorHandler &handler) {
379if (!mayPosition()) {
380auto frameAt{FrameAt()};
381if (frameOffsetInFile_ >= frameAt &&
382frameOffsetInFile_ <
383static_cast<std::int64_t>(frameAt + FrameLength())) {
384// A Flush() that's about to happen to a non-positionable file
385// needs to advance frameOffsetInFile_ to prevent attempts at
386// impossible seeks
387CommitWrites();
388leftTabLimit.reset();
389}
390}
391Flush(handler);
392}
393
394void ExternalFileUnit::FlushIfTerminal(IoErrorHandler &handler) {
395if (isTerminal()) {
396FlushOutput(handler);
397}
398}
399
400void ExternalFileUnit::Endfile(IoErrorHandler &handler) {
401if (access == Access::Direct) {
402handler.SignalError(IostatEndfileDirect,
403"ENDFILE(UNIT=%d) on direct-access file", unitNumber());
404} else if (!mayWrite()) {
405handler.SignalError(IostatEndfileUnwritable,
406"ENDFILE(UNIT=%d) on read-only file", unitNumber());
407} else if (IsAfterEndfile()) {
408// ENDFILE after ENDFILE
409} else {
410DoEndfile(handler);
411if (IsRecordFile() && access != Access::Direct) {
412// Explicit ENDFILE leaves position *after* the endfile record
413RUNTIME_CHECK(handler, endfileRecordNumber.has_value());
414currentRecordNumber = *endfileRecordNumber + 1;
415}
416}
417}
418
419void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
420if (access == Access::Direct) {
421handler.SignalError(IostatRewindNonSequential,
422"REWIND(UNIT=%d) on non-sequential file", unitNumber());
423} else {
424DoImpliedEndfile(handler);
425SetPosition(0, handler);
426currentRecordNumber = 1;
427leftTabLimit.reset();
428anyWriteSinceLastPositioning_ = false;
429}
430}
431
432void ExternalFileUnit::SetPosition(std::int64_t pos, IoErrorHandler &handler) {
433frameOffsetInFile_ = pos;
434recordOffsetInFrame_ = 0;
435if (access == Access::Direct) {
436directAccessRecWasSet_ = true;
437}
438BeginRecord();
439}
440
441bool ExternalFileUnit::SetStreamPos(
442std::int64_t oneBasedPos, IoErrorHandler &handler) {
443if (access != Access::Stream) {
444handler.SignalError("POS= may not appear unless ACCESS='STREAM'");
445return false;
446}
447if (oneBasedPos < 1) { // POS=1 is beginning of file (12.6.2.11)
448handler.SignalError(
449"POS=%zd is invalid", static_cast<std::intmax_t>(oneBasedPos));
450return false;
451}
452// A backwards POS= implies truncation after writing, at least in
453// Intel and NAG.
454if (static_cast<std::size_t>(oneBasedPos - 1) <
455frameOffsetInFile_ + recordOffsetInFrame_) {
456DoImpliedEndfile(handler);
457}
458SetPosition(oneBasedPos - 1, handler);
459// We no longer know which record we're in. Set currentRecordNumber to
460// a large value from whence we can both advance and backspace.
461currentRecordNumber = std::numeric_limits<std::int64_t>::max() / 2;
462endfileRecordNumber.reset();
463return true;
464}
465
466bool ExternalFileUnit::SetDirectRec(
467std::int64_t oneBasedRec, IoErrorHandler &handler) {
468if (access != Access::Direct) {
469handler.SignalError("REC= may not appear unless ACCESS='DIRECT'");
470return false;
471}
472if (!openRecl) {
473handler.SignalError("RECL= was not specified");
474return false;
475}
476if (oneBasedRec < 1) {
477handler.SignalError(
478"REC=%zd is invalid", static_cast<std::intmax_t>(oneBasedRec));
479return false;
480}
481currentRecordNumber = oneBasedRec;
482SetPosition((oneBasedRec - 1) * *openRecl, handler);
483return true;
484}
485
486void ExternalFileUnit::EndIoStatement() {
487io_.reset();
488u_.emplace<std::monostate>();
489lock_.Drop();
490}
491
492void ExternalFileUnit::BeginSequentialVariableUnformattedInputRecord(
493IoErrorHandler &handler) {
494RUNTIME_CHECK(handler, access == Access::Sequential);
495std::int32_t header{0}, footer{0};
496std::size_t need{recordOffsetInFrame_ + sizeof header};
497std::size_t got{ReadFrame(frameOffsetInFile_, need, handler)};
498// Try to emit informative errors to help debug corrupted files.
499const char *error{nullptr};
500if (got < need) {
501if (got == recordOffsetInFrame_) {
502HitEndOnRead(handler);
503} else {
504error = "Unformatted variable-length sequential file input failed at "
505"record #%jd (file offset %jd): truncated record header";
506}
507} else {
508header = ReadHeaderOrFooter(recordOffsetInFrame_);
509recordLength = sizeof header + header; // does not include footer
510need = recordOffsetInFrame_ + *recordLength + sizeof footer;
511got = ReadFrame(frameOffsetInFile_, need, handler);
512if (got < need) {
513error = "Unformatted variable-length sequential file input failed at "
514"record #%jd (file offset %jd): hit EOF reading record with "
515"length %jd bytes";
516} else {
517footer = ReadHeaderOrFooter(recordOffsetInFrame_ + *recordLength);
518if (footer != header) {
519error = "Unformatted variable-length sequential file input failed at "
520"record #%jd (file offset %jd): record header has length %jd "
521"that does not match record footer (%jd)";
522}
523}
524}
525if (error) {
526handler.SignalError(error, static_cast<std::intmax_t>(currentRecordNumber),
527static_cast<std::intmax_t>(frameOffsetInFile_),
528static_cast<std::intmax_t>(header), static_cast<std::intmax_t>(footer));
529// TODO: error recovery
530}
531positionInRecord = sizeof header;
532}
533
534void ExternalFileUnit::BeginVariableFormattedInputRecord(
535IoErrorHandler &handler) {
536if (this == defaultInput) {
537if (defaultOutput) {
538defaultOutput->FlushOutput(handler);
539}
540if (errorOutput) {
541errorOutput->FlushOutput(handler);
542}
543}
544std::size_t length{0};
545do {
546std::size_t need{length + 1};
547length =
548ReadFrame(frameOffsetInFile_, recordOffsetInFrame_ + need, handler) -
549recordOffsetInFrame_;
550if (length < need) {
551if (length > 0) {
552// final record w/o \n
553recordLength = length;
554unterminatedRecord = true;
555} else {
556HitEndOnRead(handler);
557}
558break;
559}
560} while (!SetVariableFormattedRecordLength());
561}
562
563void ExternalFileUnit::BackspaceFixedRecord(IoErrorHandler &handler) {
564RUNTIME_CHECK(handler, openRecl.has_value());
565if (frameOffsetInFile_ < *openRecl) {
566handler.SignalError(IostatBackspaceAtFirstRecord);
567} else {
568frameOffsetInFile_ -= *openRecl;
569}
570}
571
572void ExternalFileUnit::BackspaceVariableUnformattedRecord(
573IoErrorHandler &handler) {
574std::int32_t header{0};
575auto headerBytes{static_cast<std::int64_t>(sizeof header)};
576frameOffsetInFile_ += recordOffsetInFrame_;
577recordOffsetInFrame_ = 0;
578if (frameOffsetInFile_ <= headerBytes) {
579handler.SignalError(IostatBackspaceAtFirstRecord);
580return;
581}
582// Error conditions here cause crashes, not file format errors, because the
583// validity of the file structure before the current record will have been
584// checked informatively in NextSequentialVariableUnformattedInputRecord().
585std::size_t got{
586ReadFrame(frameOffsetInFile_ - headerBytes, headerBytes, handler)};
587if (static_cast<std::int64_t>(got) < headerBytes) {
588handler.SignalError(IostatShortRead);
589return;
590}
591recordLength = ReadHeaderOrFooter(0);
592if (frameOffsetInFile_ < *recordLength + 2 * headerBytes) {
593handler.SignalError(IostatBadUnformattedRecord);
594return;
595}
596frameOffsetInFile_ -= *recordLength + 2 * headerBytes;
597auto need{static_cast<std::size_t>(
598recordOffsetInFrame_ + sizeof header + *recordLength)};
599got = ReadFrame(frameOffsetInFile_, need, handler);
600if (got < need) {
601handler.SignalError(IostatShortRead);
602return;
603}
604header = ReadHeaderOrFooter(recordOffsetInFrame_);
605if (header != *recordLength) {
606handler.SignalError(IostatBadUnformattedRecord);
607return;
608}
609}
610
611// There's no portable memrchr(), unfortunately, and strrchr() would
612// fail on a record with a NUL, so we have to do it the hard way.
613static RT_API_ATTRS const char *FindLastNewline(
614const char *str, std::size_t length) {
615for (const char *p{str + length}; p >= str; p--) {
616if (*p == '\n') {
617return p;
618}
619}
620return nullptr;
621}
622
623void ExternalFileUnit::BackspaceVariableFormattedRecord(
624IoErrorHandler &handler) {
625// File offset of previous record's newline
626auto prevNL{
627frameOffsetInFile_ + static_cast<std::int64_t>(recordOffsetInFrame_) - 1};
628if (prevNL < 0) {
629handler.SignalError(IostatBackspaceAtFirstRecord);
630return;
631}
632while (true) {
633if (frameOffsetInFile_ < prevNL) {
634if (const char *p{
635FindLastNewline(Frame(), prevNL - 1 - frameOffsetInFile_)}) {
636recordOffsetInFrame_ = p - Frame() + 1;
637recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
638break;
639}
640}
641if (frameOffsetInFile_ == 0) {
642recordOffsetInFrame_ = 0;
643recordLength = prevNL;
644break;
645}
646frameOffsetInFile_ -= std::min<std::int64_t>(frameOffsetInFile_, 1024);
647auto need{static_cast<std::size_t>(prevNL + 1 - frameOffsetInFile_)};
648auto got{ReadFrame(frameOffsetInFile_, need, handler)};
649if (got < need) {
650handler.SignalError(IostatShortRead);
651return;
652}
653}
654if (Frame()[recordOffsetInFrame_ + *recordLength] != '\n') {
655handler.SignalError(IostatMissingTerminator);
656return;
657}
658if (*recordLength > 0 &&
659Frame()[recordOffsetInFrame_ + *recordLength - 1] == '\r') {
660--*recordLength;
661}
662}
663
664void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
665if (access != Access::Direct) {
666if (!impliedEndfile_ && leftTabLimit && direction_ == Direction::Output) {
667// Flush a partial record after non-advancing output
668impliedEndfile_ = true;
669}
670if (impliedEndfile_ && mayPosition()) {
671DoEndfile(handler);
672}
673}
674impliedEndfile_ = false;
675}
676
677template <bool ANY_DIR, Direction DIR>
678void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
679if (IsRecordFile() && access != Access::Direct) {
680furthestPositionInRecord =
681std::max(positionInRecord, furthestPositionInRecord);
682if (leftTabLimit) { // last I/O was non-advancing
683if (access == Access::Sequential && direction_ == Direction::Output) {
684if constexpr (ANY_DIR || DIR == Direction::Output) {
685// When DoEndfile() is called from BeginReadingRecord(),
686// this call to AdvanceRecord() may appear as a recursion
687// though it may never happen. Expose the call only
688// under the constexpr direction check.
689AdvanceRecord(handler);
690} else {
691// This check always fails if we are here.
692RUNTIME_CHECK(handler, direction_ != Direction::Output);
693}
694} else { // Access::Stream or input
695leftTabLimit.reset();
696++currentRecordNumber;
697}
698}
699endfileRecordNumber = currentRecordNumber;
700}
701frameOffsetInFile_ += recordOffsetInFrame_ + furthestPositionInRecord;
702recordOffsetInFrame_ = 0;
703FlushOutput(handler);
704Truncate(frameOffsetInFile_, handler);
705TruncateFrame(frameOffsetInFile_, handler);
706BeginRecord();
707impliedEndfile_ = false;
708anyWriteSinceLastPositioning_ = false;
709}
710
711template void ExternalFileUnit::DoEndfile(IoErrorHandler &handler);
712template void ExternalFileUnit::DoEndfile<false, Direction::Output>(
713IoErrorHandler &handler);
714template void ExternalFileUnit::DoEndfile<false, Direction::Input>(
715IoErrorHandler &handler);
716
717void ExternalFileUnit::CommitWrites() {
718frameOffsetInFile_ +=
719recordOffsetInFrame_ + recordLength.value_or(furthestPositionInRecord);
720recordOffsetInFrame_ = 0;
721BeginRecord();
722}
723
724bool ExternalFileUnit::CheckDirectAccess(IoErrorHandler &handler) {
725if (access == Access::Direct) {
726RUNTIME_CHECK(handler, openRecl);
727if (!directAccessRecWasSet_) {
728handler.SignalError(
729"No REC= was specified for a data transfer with ACCESS='DIRECT'");
730return false;
731}
732}
733return true;
734}
735
736void ExternalFileUnit::HitEndOnRead(IoErrorHandler &handler) {
737handler.SignalEnd();
738if (IsRecordFile() && access != Access::Direct) {
739endfileRecordNumber = currentRecordNumber;
740}
741}
742
743ChildIo &ExternalFileUnit::PushChildIo(IoStatementState &parent) {
744OwningPtr<ChildIo> current{std::move(child_)};
745Terminator &terminator{parent.GetIoErrorHandler()};
746OwningPtr<ChildIo> next{New<ChildIo>{terminator}(parent, std::move(current))};
747child_.reset(next.release());
748return *child_;
749}
750
751void ExternalFileUnit::PopChildIo(ChildIo &child) {
752if (child_.get() != &child) {
753child.parent().GetIoErrorHandler().Crash(
754"ChildIo being popped is not top of stack");
755}
756child_.reset(child.AcquirePrevious().release()); // deletes top child
757}
758
759std::int32_t ExternalFileUnit::ReadHeaderOrFooter(std::int64_t frameOffset) {
760std::int32_t word;
761char *wordPtr{reinterpret_cast<char *>(&word)};
762std::memcpy(wordPtr, Frame() + frameOffset, sizeof word);
763if (swapEndianness_) {
764SwapEndianness(wordPtr, sizeof word, sizeof word);
765}
766return word;
767}
768
769void ChildIo::EndIoStatement() {
770io_.reset();
771u_.emplace<std::monostate>();
772}
773
774Iostat ChildIo::CheckFormattingAndDirection(
775bool unformatted, Direction direction) {
776bool parentIsInput{!parent_.get_if<IoDirectionState<Direction::Output>>()};
777bool parentIsFormatted{parentIsInput
778? parent_.get_if<FormattedIoStatementState<Direction::Input>>() !=
779nullptr
780: parent_.get_if<FormattedIoStatementState<Direction::Output>>() !=
781nullptr};
782bool parentIsUnformatted{!parentIsFormatted};
783if (unformatted != parentIsUnformatted) {
784return unformatted ? IostatUnformattedChildOnFormattedParent
785: IostatFormattedChildOnUnformattedParent;
786} else if (parentIsInput != (direction == Direction::Input)) {
787return parentIsInput ? IostatChildOutputToInputParent
788: IostatChildInputFromOutputParent;
789} else {
790return IostatOk;
791}
792}
793
794RT_OFFLOAD_API_GROUP_END
795} // namespace Fortran::runtime::io
796