1 //===-- runtime/connection.h ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // Fortran I/O connection state (abstracted over internal & external units)
10 
11 #ifndef FORTRAN_RUNTIME_IO_CONNECTION_H_
12 #define FORTRAN_RUNTIME_IO_CONNECTION_H_
13 
14 #include "format.h"
15 #include <cinttypes>
16 #include <optional>
17 
18 namespace Fortran::runtime::io {
19 
20 class IoStatementState;
21 
22 enum class Direction { Output, Input };
23 enum class Access { Sequential, Direct, Stream };
24 
25 // These characteristics of a connection are immutable after being
26 // established in an OPEN statement.
27 struct ConnectionAttributes {
28   Access access{Access::Sequential}; // ACCESS='SEQUENTIAL', 'DIRECT', 'STREAM'
29   std::optional<bool> isUnformatted; // FORM='UNFORMATTED' if true
30   bool isUTF8{false}; // ENCODING='UTF-8'
31   std::optional<std::int64_t> openRecl; // RECL= on OPEN
32 
IsRecordFileConnectionAttributes33   bool IsRecordFile() const {
34     // Formatted stream files are viewed as having records, at least on input
35     return access != Access::Stream || !isUnformatted.value_or(true);
36   }
37 
useUTF8ConnectionAttributes38   template <typename CHAR = char> constexpr bool useUTF8() const {
39     // For wide CHARACTER kinds, always use UTF-8 for formatted I/O.
40     // For single-byte CHARACTER, encode characters >= 0x80 with
41     // UTF-8 iff the mode is set.
42     return sizeof(CHAR) > 1 || isUTF8;
43   }
44 };
45 
46 struct ConnectionState : public ConnectionAttributes {
47   bool IsAtEOF() const; // true when read has hit EOF or endfile record
48   bool IsAfterEndfile() const; // true after ENDFILE until repositioned
49   std::size_t RemainingSpaceInRecord() const;
50   bool NeedAdvance(std::size_t) const;
51   void HandleAbsolutePosition(std::int64_t);
52   void HandleRelativePosition(std::int64_t);
53 
BeginRecordConnectionState54   void BeginRecord() {
55     positionInRecord = 0;
56     furthestPositionInRecord = 0;
57     unterminatedRecord = false;
58   }
59 
EffectiveRecordLengthConnectionState60   std::optional<std::int64_t> EffectiveRecordLength() const {
61     // When an input record is longer than an explicit RECL= from OPEN
62     // it is effectively truncated on input.
63     return openRecl && recordLength && *openRecl < *recordLength ? openRecl
64                                                                  : recordLength;
65   }
66 
67   std::optional<std::int64_t> recordLength;
68 
69   std::int64_t currentRecordNumber{1}; // 1 is first
70 
71   // positionInRecord is the 0-based offset in the current recurd to/from
72   // which the next data transfer will occur.  It can be past
73   // furthestPositionInRecord if moved by an X or T or TR control edit
74   // descriptor.
75   std::int64_t positionInRecord{0};
76 
77   // furthestPositionInRecord is the 0-based offset of the greatest
78   // position in the current record to/from which any data transfer has
79   // occurred, plus one.  It can be viewed as a count of bytes processed.
80   std::int64_t furthestPositionInRecord{0}; // max(position+bytes)
81 
82   // Set at end of non-advancing I/O data transfer
83   std::optional<std::int64_t> leftTabLimit; // offset in current record
84 
85   // currentRecordNumber value captured after ENDFILE/REWIND/BACKSPACE statement
86   // or an end-of-file READ condition on a sequential access file
87   std::optional<std::int64_t> endfileRecordNumber;
88 
89   // Mutable modes set at OPEN() that can be overridden in READ/WRITE & FORMAT
90   MutableModes modes; // BLANK=, DECIMAL=, SIGN=, ROUND=, PAD=, DELIM=, kP
91 
92   // Set when processing repeated items during list-directed & NAMELIST input
93   // in order to keep a span of records in frame on a non-positionable file,
94   // so that backspacing to the beginning of the repeated item doesn't require
95   // repositioning the external storage medium when that's impossible.
96   bool pinnedFrame{false};
97 
98   // Set when the last record of a file is not properly terminated
99   // so that a non-advancing READ will not signal EOR.
100   bool unterminatedRecord{false};
101 };
102 
103 // Utility class for capturing and restoring a position in an input stream.
104 class SavedPosition {
105 public:
106   explicit SavedPosition(IoStatementState &);
107   ~SavedPosition();
108 
109 private:
110   IoStatementState &io_;
111   ConnectionState saved_;
112 };
113 
114 } // namespace Fortran::runtime::io
115 #endif // FORTRAN_RUNTIME_IO_CONNECTION_H_
116