xref: /llvm-project-15.0.7/flang/runtime/unit.h (revision fbdcfcd4)
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 "file.h"
17 #include "format.h"
18 #include "io-error.h"
19 #include "io-stmt.h"
20 #include "lock.h"
21 #include "memory.h"
22 #include "terminator.h"
23 #include <cstdlib>
24 #include <cstring>
25 #include <optional>
26 #include <variant>
27 
28 namespace Fortran::runtime::io {
29 
30 class UnitMap;
31 
32 class ExternalFileUnit : public ConnectionState,
33                          public OpenFile,
34                          public FileFrame<ExternalFileUnit> {
35 public:
36   explicit ExternalFileUnit(int unitNumber) : unitNumber_{unitNumber} {}
37   int unitNumber() const { return unitNumber_; }
38 
39   static ExternalFileUnit *LookUp(int unit);
40   static ExternalFileUnit &LookUpOrCrash(int unit, const Terminator &);
41   static ExternalFileUnit &LookUpOrCreate(
42       int unit, const Terminator &, bool *wasExtant = nullptr);
43   static ExternalFileUnit *LookUpForClose(int unit);
44   static int NewUnit(const Terminator &);
45   static void CloseAll(IoErrorHandler &);
46 
47   void OpenUnit(OpenStatus, Position, OwningPtr<char> &&path,
48       std::size_t pathLength, IoErrorHandler &);
49   void CloseUnit(CloseStatus, IoErrorHandler &);
50   void DestroyClosed();
51 
52   template <typename A, typename... X>
53   IoStatementState &BeginIoStatement(X &&... xs) {
54     // TODO: Child data transfer statements vs. locking
55     lock_.Take(); // dropped in EndIoStatement()
56     A &state{u_.emplace<A>(std::forward<X>(xs)...)};
57     if constexpr (!std::is_same_v<A, OpenStatementState>) {
58       state.mutableModes() = ConnectionState::modes;
59     }
60     io_.emplace(state);
61     return *io_;
62   }
63 
64   bool Emit(const char *, std::size_t bytes, IoErrorHandler &);
65   std::optional<char32_t> GetCurrentChar(IoErrorHandler &);
66   void SetLeftTabLimit();
67   bool AdvanceRecord(IoErrorHandler &);
68   void BackspaceRecord(IoErrorHandler &);
69   void FlushIfTerminal(IoErrorHandler &);
70   void EndIoStatement();
71   void SetPosition(std::int64_t pos) {
72     frameOffsetInFile_ = pos;
73     recordOffsetInFrame_ = 0;
74   }
75 
76 private:
77   static UnitMap &GetUnitMap();
78   void NextSequentialUnformattedInputRecord(IoErrorHandler &);
79   void NextSequentialFormattedInputRecord(IoErrorHandler &);
80   void BackspaceSequentialUnformattedRecord(IoErrorHandler &);
81   void BackspaceSequentialFormattedRecord(IoErrorHandler &);
82 
83   int unitNumber_{-1};
84   bool isReading_{false};
85 
86   Lock lock_;
87 
88   // When an I/O statement is in progress on this unit, holds its state.
89   std::variant<std::monostate, OpenStatementState, CloseStatementState,
90       ExternalFormattedIoStatementState<Direction::Output>,
91       ExternalFormattedIoStatementState<Direction::Input>,
92       ExternalListIoStatementState<Direction::Output>,
93       ExternalListIoStatementState<Direction::Input>,
94       UnformattedIoStatementState<Direction::Output>,
95       UnformattedIoStatementState<Direction::Input>>
96       u_;
97 
98   // Points to the active alternative (if any) in u_ for use as a Cookie
99   std::optional<IoStatementState> io_;
100 
101   // Subtle: The beginning of the frame can't be allowed to advance
102   // during a single list-directed READ due to the possibility of a
103   // multi-record CHARACTER value with a "r*" repeat count.
104   std::int64_t frameOffsetInFile_{0};
105   std::int64_t recordOffsetInFrame_{0}; // of currentRecordNumber
106 };
107 
108 } // namespace Fortran::runtime::io
109 #endif // FORTRAN_RUNTIME_IO_UNIT_H_
110