1 //===-- runtime/unit.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 external I/O units 10 11 #ifndef FORTRAN_RUNTIME_IO_UNIT_H_ 12 #define FORTRAN_RUNTIME_IO_UNIT_H_ 13 14 #include "buffer.h" 15 #include "connection.h" 16 #include "environment.h" 17 #include "file.h" 18 #include "format.h" 19 #include "io-error.h" 20 #include "io-stmt.h" 21 #include "lock.h" 22 #include "terminator.h" 23 #include "flang/Runtime/memory.h" 24 #include <cstdlib> 25 #include <cstring> 26 #include <optional> 27 #include <variant> 28 29 namespace Fortran::runtime::io { 30 31 class UnitMap; 32 class ChildIo; 33 34 class ExternalFileUnit : public ConnectionState, 35 public OpenFile, 36 public FileFrame<ExternalFileUnit> { 37 public: 38 explicit ExternalFileUnit(int unitNumber) : unitNumber_{unitNumber} { 39 isUTF8 = executionEnvironment.defaultUTF8; 40 } 41 ~ExternalFileUnit() {} 42 43 int unitNumber() const { return unitNumber_; } 44 bool swapEndianness() const { return swapEndianness_; } 45 bool createdForInternalChildIo() const { return createdForInternalChildIo_; } 46 47 static ExternalFileUnit *LookUp(int unit); 48 static ExternalFileUnit &LookUpOrCrash(int unit, const Terminator &); 49 static ExternalFileUnit &LookUpOrCreate( 50 int unit, const Terminator &, bool &wasExtant); 51 static ExternalFileUnit &LookUpOrCreateAnonymous(int unit, Direction, 52 std::optional<bool> isUnformatted, const Terminator &); 53 static ExternalFileUnit *LookUp(const char *path); 54 static ExternalFileUnit &CreateNew(int unit, const Terminator &); 55 static ExternalFileUnit *LookUpForClose(int unit); 56 static ExternalFileUnit &NewUnit(const Terminator &, bool forChildIo); 57 static void CloseAll(IoErrorHandler &); 58 static void FlushAll(IoErrorHandler &); 59 60 void OpenUnit(std::optional<OpenStatus>, std::optional<Action>, Position, 61 OwningPtr<char> &&path, std::size_t pathLength, Convert, 62 IoErrorHandler &); 63 void OpenAnonymousUnit(std::optional<OpenStatus>, std::optional<Action>, 64 Position, Convert, IoErrorHandler &); 65 void CloseUnit(CloseStatus, IoErrorHandler &); 66 void DestroyClosed(); 67 68 Iostat SetDirection(Direction); 69 70 template <typename A, typename... X> 71 IoStatementState &BeginIoStatement(X &&...xs) { 72 lock_.Take(); // dropped in EndIoStatement() 73 A &state{u_.emplace<A>(std::forward<X>(xs)...)}; 74 if constexpr (!std::is_same_v<A, OpenStatementState>) { 75 state.mutableModes() = ConnectionState::modes; 76 } 77 directAccessRecWasSet_ = false; 78 io_.emplace(state); 79 return *io_; 80 } 81 82 bool Emit( 83 const char *, std::size_t, std::size_t elementBytes, IoErrorHandler &); 84 bool Receive(char *, std::size_t, std::size_t elementBytes, IoErrorHandler &); 85 std::size_t GetNextInputBytes(const char *&, IoErrorHandler &); 86 void SetLeftTabLimit(); 87 bool BeginReadingRecord(IoErrorHandler &); 88 void FinishReadingRecord(IoErrorHandler &); 89 bool AdvanceRecord(IoErrorHandler &); 90 void BackspaceRecord(IoErrorHandler &); 91 void FlushOutput(IoErrorHandler &); 92 void FlushIfTerminal(IoErrorHandler &); 93 void Endfile(IoErrorHandler &); 94 void Rewind(IoErrorHandler &); 95 void EndIoStatement(); 96 void SetPosition(std::int64_t, IoErrorHandler &); // zero-based 97 std::int64_t InquirePos() const { 98 // 12.6.2.11 defines POS=1 as the beginning of file 99 return frameOffsetInFile_ + 1; 100 } 101 102 ChildIo *GetChildIo() { return child_.get(); } 103 ChildIo &PushChildIo(IoStatementState &); 104 void PopChildIo(ChildIo &); 105 106 private: 107 static UnitMap &GetUnitMap(); 108 const char *FrameNextInput(IoErrorHandler &, std::size_t); 109 void BeginSequentialVariableUnformattedInputRecord(IoErrorHandler &); 110 void BeginVariableFormattedInputRecord(IoErrorHandler &); 111 void BackspaceFixedRecord(IoErrorHandler &); 112 void BackspaceVariableUnformattedRecord(IoErrorHandler &); 113 void BackspaceVariableFormattedRecord(IoErrorHandler &); 114 bool SetVariableFormattedRecordLength(); 115 void DoImpliedEndfile(IoErrorHandler &); 116 void DoEndfile(IoErrorHandler &); 117 void CommitWrites(); 118 bool CheckDirectAccess(IoErrorHandler &); 119 120 int unitNumber_{-1}; 121 Direction direction_{Direction::Output}; 122 bool impliedEndfile_{false}; // sequential/stream output has taken place 123 bool beganReadingRecord_{false}; 124 bool directAccessRecWasSet_{false}; // REC= appeared 125 126 Lock lock_; 127 128 // When an I/O statement is in progress on this unit, holds its state. 129 std::variant<std::monostate, OpenStatementState, CloseStatementState, 130 ExternalFormattedIoStatementState<Direction::Output>, 131 ExternalFormattedIoStatementState<Direction::Input>, 132 ExternalListIoStatementState<Direction::Output>, 133 ExternalListIoStatementState<Direction::Input>, 134 ExternalUnformattedIoStatementState<Direction::Output>, 135 ExternalUnformattedIoStatementState<Direction::Input>, InquireUnitState, 136 ExternalMiscIoStatementState, ErroneousIoStatementState> 137 u_; 138 139 // Points to the active alternative (if any) in u_ for use as a Cookie 140 std::optional<IoStatementState> io_; 141 142 // Subtle: The beginning of the frame can't be allowed to advance 143 // during a single list-directed READ due to the possibility of a 144 // multi-record CHARACTER value with a "r*" repeat count. So we 145 // manage the frame and the current record therein separately. 146 std::int64_t frameOffsetInFile_{0}; 147 std::size_t recordOffsetInFrame_{0}; // of currentRecordNumber 148 149 bool swapEndianness_{false}; 150 151 bool createdForInternalChildIo_{false}; 152 153 // A stack of child I/O pseudo-units for user-defined derived type 154 // I/O that have this unit number. 155 OwningPtr<ChildIo> child_; 156 }; 157 158 // A pseudo-unit for child I/O statements in user-defined derived type 159 // I/O subroutines; it forwards operations to the parent I/O statement, 160 // which can also be a child I/O statement. 161 class ChildIo { 162 public: 163 ChildIo(IoStatementState &parent, OwningPtr<ChildIo> &&previous) 164 : parent_{parent}, previous_{std::move(previous)} {} 165 166 IoStatementState &parent() const { return parent_; } 167 168 void EndIoStatement(); 169 170 template <typename A, typename... X> 171 IoStatementState &BeginIoStatement(X &&...xs) { 172 A &state{u_.emplace<A>(std::forward<X>(xs)...)}; 173 io_.emplace(state); 174 return *io_; 175 } 176 177 OwningPtr<ChildIo> AcquirePrevious() { return std::move(previous_); } 178 179 Iostat CheckFormattingAndDirection(bool unformatted, Direction); 180 181 private: 182 IoStatementState &parent_; 183 OwningPtr<ChildIo> previous_; 184 std::variant<std::monostate, 185 ChildFormattedIoStatementState<Direction::Output>, 186 ChildFormattedIoStatementState<Direction::Input>, 187 ChildListIoStatementState<Direction::Output>, 188 ChildListIoStatementState<Direction::Input>, 189 ChildUnformattedIoStatementState<Direction::Output>, 190 ChildUnformattedIoStatementState<Direction::Input>, InquireUnitState, 191 ErroneousIoStatementState> 192 u_; 193 std::optional<IoStatementState> io_; 194 }; 195 196 } // namespace Fortran::runtime::io 197 #endif // FORTRAN_RUNTIME_IO_UNIT_H_ 198