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-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(
|
|
|
|
int unit, const Terminator &, bool *wasExtant = nullptr);
|
|
|
|
static ExternalFileUnit *LookUpForClose(int unit);
|
|
|
|
static int NewUnit(const Terminator &);
|
2020-01-24 08:59:27 +08:00
|
|
|
static void CloseAll(IoErrorHandler &);
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
void OpenUnit(OpenStatus, Position, OwningPtr<char> &&path,
|
|
|
|
std::size_t pathLength, IoErrorHandler &);
|
|
|
|
void CloseUnit(CloseStatus, IoErrorHandler &);
|
2020-02-14 06:41:56 +08:00
|
|
|
void DestroyClosed();
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
template<typename A, typename... X>
|
|
|
|
IoStatementState &BeginIoStatement(X &&... xs) {
|
2020-02-14 06:41:56 +08:00
|
|
|
// TODO: Child data transfer statements vs. locking
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool Emit(const char *, std::size_t bytes, 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();
|
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 &);
|
|
|
|
void EndIoStatement();
|
2020-02-14 06:41:56 +08:00
|
|
|
void SetPosition(std::int64_t pos) {
|
|
|
|
frameOffsetInFile_ = pos;
|
|
|
|
recordOffsetInFrame_ = 0;
|
|
|
|
}
|
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();
|
|
|
|
void NextSequentialUnformattedInputRecord(IoErrorHandler &);
|
|
|
|
void NextSequentialFormattedInputRecord(IoErrorHandler &);
|
|
|
|
void BackspaceSequentialUnformattedRecord(IoErrorHandler &);
|
|
|
|
void BackspaceSequentialFormattedRecord(IoErrorHandler &);
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-01-24 08:59:27 +08:00
|
|
|
int unitNumber_{-1};
|
|
|
|
bool isReading_{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>,
|
|
|
|
UnformattedIoStatementState<Direction::Input>>
|
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
|
|
|
|
// multi-record CHARACTER value with a "r*" repeat count.
|
|
|
|
std::int64_t frameOffsetInFile_{0};
|
|
|
|
std::int64_t recordOffsetInFrame_{0}; // of currentRecordNumber
|
2020-01-24 08:59:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // FORTRAN_RUNTIME_IO_UNIT_H_
|