2020-01-24 08:59:27 +08:00
|
|
|
//===-- runtime/unit.h ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
// Fortran external I/O units
|
2020-01-24 08:59:27 +08:00
|
|
|
|
|
|
|
#ifndef FORTRAN_RUNTIME_IO_UNIT_H_
|
|
|
|
#define FORTRAN_RUNTIME_IO_UNIT_H_
|
|
|
|
|
|
|
|
#include "buffer.h"
|
2020-02-05 08:55:45 +08:00
|
|
|
#include "connection.h"
|
2020-01-24 08:59:27 +08:00
|
|
|
#include "file.h"
|
|
|
|
#include "format.h"
|
|
|
|
#include "io-error.h"
|
|
|
|
#include "io-stmt.h"
|
|
|
|
#include "lock.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "terminator.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <optional>
|
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
class UnitMap;
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
class ExternalFileUnit : public ConnectionState,
|
|
|
|
public OpenFile,
|
|
|
|
public FileFrame<ExternalFileUnit> {
|
2020-01-24 08:59:27 +08:00
|
|
|
public:
|
2020-02-05 08:55:45 +08:00
|
|
|
explicit ExternalFileUnit(int unitNumber) : unitNumber_{unitNumber} {}
|
|
|
|
int unitNumber() const { return unitNumber_; }
|
2020-08-04 02:35:29 +08:00
|
|
|
bool swapEndianness() const { return swapEndianness_; }
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
static ExternalFileUnit *LookUp(int unit);
|
|
|
|
static ExternalFileUnit &LookUpOrCrash(int unit, const Terminator &);
|
2020-02-14 06:41:56 +08:00
|
|
|
static ExternalFileUnit &LookUpOrCreate(
|
2020-07-15 02:28:03 +08:00
|
|
|
int unit, const Terminator &, bool &wasExtant);
|
2021-05-06 02:33:00 +08:00
|
|
|
static ExternalFileUnit &LookUpOrCreateAnonymous(int unit, Direction,
|
|
|
|
std::optional<bool> isUnformatted, const Terminator &);
|
2020-08-04 02:35:29 +08:00
|
|
|
static ExternalFileUnit *LookUp(const char *path);
|
2020-07-15 02:28:03 +08:00
|
|
|
static ExternalFileUnit &CreateNew(int unit, const Terminator &);
|
2020-02-14 06:41:56 +08:00
|
|
|
static ExternalFileUnit *LookUpForClose(int unit);
|
|
|
|
static int NewUnit(const Terminator &);
|
2020-01-24 08:59:27 +08:00
|
|
|
static void CloseAll(IoErrorHandler &);
|
2020-07-04 03:38:22 +08:00
|
|
|
static void FlushAll(IoErrorHandler &);
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2021-04-13 08:05:05 +08:00
|
|
|
void OpenUnit(std::optional<OpenStatus>, std::optional<Action>, Position,
|
2020-07-22 08:37:35 +08:00
|
|
|
OwningPtr<char> &&path, std::size_t pathLength, Convert,
|
|
|
|
IoErrorHandler &);
|
2021-04-13 08:05:05 +08:00
|
|
|
void OpenAnonymousUnit(std::optional<OpenStatus>, std::optional<Action>,
|
|
|
|
Position, Convert, IoErrorHandler &);
|
2020-02-05 08:55:45 +08:00
|
|
|
void CloseUnit(CloseStatus, IoErrorHandler &);
|
2020-02-14 06:41:56 +08:00
|
|
|
void DestroyClosed();
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-07-04 03:38:22 +08:00
|
|
|
bool SetDirection(Direction, IoErrorHandler &);
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A, typename... X>
|
2020-08-04 02:35:29 +08:00
|
|
|
IoStatementState &BeginIoStatement(X &&...xs) {
|
2020-02-14 06:41:56 +08:00
|
|
|
// TODO: Child data transfer statements vs. locking
|
2020-03-29 12:00:16 +08:00
|
|
|
lock_.Take(); // dropped in EndIoStatement()
|
2020-02-05 08:55:45 +08:00
|
|
|
A &state{u_.emplace<A>(std::forward<X>(xs)...)};
|
|
|
|
if constexpr (!std::is_same_v<A, OpenStatementState>) {
|
|
|
|
state.mutableModes() = ConnectionState::modes;
|
|
|
|
}
|
|
|
|
io_.emplace(state);
|
|
|
|
return *io_;
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 08:37:35 +08:00
|
|
|
bool Emit(
|
|
|
|
const char *, std::size_t, std::size_t elementBytes, IoErrorHandler &);
|
|
|
|
bool Receive(char *, std::size_t, std::size_t elementBytes, IoErrorHandler &);
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<char32_t> GetCurrentChar(IoErrorHandler &);
|
2020-01-24 08:59:27 +08:00
|
|
|
void SetLeftTabLimit();
|
2021-04-14 07:07:58 +08:00
|
|
|
bool BeginReadingRecord(IoErrorHandler &);
|
2020-10-01 03:53:00 +08:00
|
|
|
void FinishReadingRecord(IoErrorHandler &);
|
2020-02-05 08:55:45 +08:00
|
|
|
bool AdvanceRecord(IoErrorHandler &);
|
2020-02-14 06:41:56 +08:00
|
|
|
void BackspaceRecord(IoErrorHandler &);
|
2020-02-05 08:55:45 +08:00
|
|
|
void FlushIfTerminal(IoErrorHandler &);
|
2020-07-04 03:38:22 +08:00
|
|
|
void Endfile(IoErrorHandler &);
|
|
|
|
void Rewind(IoErrorHandler &);
|
2020-02-05 08:55:45 +08:00
|
|
|
void EndIoStatement();
|
2020-02-14 06:41:56 +08:00
|
|
|
void SetPosition(std::int64_t pos) {
|
|
|
|
frameOffsetInFile_ = pos;
|
|
|
|
recordOffsetInFrame_ = 0;
|
2020-07-04 03:38:22 +08:00
|
|
|
BeginRecord();
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-01-24 08:59:27 +08:00
|
|
|
private:
|
2020-02-14 06:41:56 +08:00
|
|
|
static UnitMap &GetUnitMap();
|
2020-07-04 03:38:22 +08:00
|
|
|
const char *FrameNextInput(IoErrorHandler &, std::size_t);
|
|
|
|
void BeginSequentialVariableUnformattedInputRecord(IoErrorHandler &);
|
|
|
|
void BeginSequentialVariableFormattedInputRecord(IoErrorHandler &);
|
|
|
|
void BackspaceFixedRecord(IoErrorHandler &);
|
|
|
|
void BackspaceVariableUnformattedRecord(IoErrorHandler &);
|
|
|
|
void BackspaceVariableFormattedRecord(IoErrorHandler &);
|
|
|
|
bool SetSequentialVariableFormattedRecordLength();
|
|
|
|
void DoImpliedEndfile(IoErrorHandler &);
|
|
|
|
void DoEndfile(IoErrorHandler &);
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-01-24 08:59:27 +08:00
|
|
|
int unitNumber_{-1};
|
2020-07-04 03:38:22 +08:00
|
|
|
Direction direction_{Direction::Output};
|
|
|
|
bool impliedEndfile_{false}; // seq. output has taken place
|
2020-10-01 03:53:00 +08:00
|
|
|
bool beganReadingRecord_{false};
|
2020-02-14 06:41:56 +08:00
|
|
|
|
|
|
|
Lock lock_;
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
// When an I/O statement is in progress on this unit, holds its state.
|
|
|
|
std::variant<std::monostate, OpenStatementState, CloseStatementState,
|
2020-02-14 06:41:56 +08:00
|
|
|
ExternalFormattedIoStatementState<Direction::Output>,
|
|
|
|
ExternalFormattedIoStatementState<Direction::Input>,
|
|
|
|
ExternalListIoStatementState<Direction::Output>,
|
|
|
|
ExternalListIoStatementState<Direction::Input>,
|
|
|
|
UnformattedIoStatementState<Direction::Output>,
|
2020-08-04 02:35:29 +08:00
|
|
|
UnformattedIoStatementState<Direction::Input>, InquireUnitState,
|
2020-07-04 08:57:28 +08:00
|
|
|
ExternalMiscIoStatementState>
|
2020-02-05 08:55:45 +08:00
|
|
|
u_;
|
2020-02-14 06:41:56 +08:00
|
|
|
|
|
|
|
// Points to the active alternative (if any) in u_ for use as a Cookie
|
2020-02-05 08:55:45 +08:00
|
|
|
std::optional<IoStatementState> io_;
|
2020-02-14 06:41:56 +08:00
|
|
|
|
|
|
|
// Subtle: The beginning of the frame can't be allowed to advance
|
|
|
|
// during a single list-directed READ due to the possibility of a
|
2020-07-04 03:38:22 +08:00
|
|
|
// multi-record CHARACTER value with a "r*" repeat count. So we
|
|
|
|
// manage the frame and the current record therein separately.
|
2020-02-14 06:41:56 +08:00
|
|
|
std::int64_t frameOffsetInFile_{0};
|
2020-07-04 03:38:22 +08:00
|
|
|
std::size_t recordOffsetInFrame_{0}; // of currentRecordNumber
|
2020-07-22 08:37:35 +08:00
|
|
|
|
|
|
|
bool swapEndianness_{false};
|
2020-01-24 08:59:27 +08:00
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|
|
|
|
#endif // FORTRAN_RUNTIME_IO_UNIT_H_
|