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"
|
2022-03-17 03:32:03 +08:00
|
|
|
#include "environment.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 "terminator.h"
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/memory.h"
|
2020-01-24 08:59:27 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <optional>
|
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
class UnitMap;
|
2021-06-26 01:40:08 +08:00
|
|
|
class ChildIo;
|
2020-02-14 06:41:56 +08:00
|
|
|
|
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:
|
2022-03-17 03:32:03 +08:00
|
|
|
explicit ExternalFileUnit(int unitNumber) : unitNumber_{unitNumber} {
|
|
|
|
isUTF8 = executionEnvironment.defaultUTF8;
|
|
|
|
}
|
2021-11-25 08:05:37 +08:00
|
|
|
~ExternalFileUnit() {}
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
int unitNumber() const { return unitNumber_; }
|
2020-08-04 02:35:29 +08:00
|
|
|
bool swapEndianness() const { return swapEndianness_; }
|
2021-06-26 01:40:08 +08:00
|
|
|
bool createdForInternalChildIo() const { return createdForInternalChildIo_; }
|
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);
|
2022-01-27 01:53:12 +08:00
|
|
|
static ExternalFileUnit &NewUnit(const Terminator &, bool forChildIo);
|
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
|
|
|
|
2022-02-17 06:55:19 +08:00
|
|
|
Iostat SetDirection(Direction);
|
2020-07-04 03:38:22 +08:00
|
|
|
|
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-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;
|
|
|
|
}
|
2022-02-18 09:22:09 +08:00
|
|
|
directAccessRecWasSet_ = false;
|
2020-02-05 08:55:45 +08:00
|
|
|
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 &);
|
2021-11-03 04:01:38 +08:00
|
|
|
std::size_t GetNextInputBytes(const char *&, IoErrorHandler &);
|
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 &);
|
2021-06-29 02:41:04 +08:00
|
|
|
void FlushOutput(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();
|
2022-01-29 07:34:28 +08:00
|
|
|
void SetPosition(std::int64_t, IoErrorHandler &); // zero-based
|
|
|
|
std::int64_t InquirePos() const {
|
|
|
|
// 12.6.2.11 defines POS=1 as the beginning of file
|
|
|
|
return frameOffsetInFile_ + 1;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2021-06-26 01:40:08 +08:00
|
|
|
ChildIo *GetChildIo() { return child_.get(); }
|
|
|
|
ChildIo &PushChildIo(IoStatementState &);
|
|
|
|
void PopChildIo(ChildIo &);
|
|
|
|
|
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 &);
|
2022-01-29 07:34:28 +08:00
|
|
|
void BeginVariableFormattedInputRecord(IoErrorHandler &);
|
2020-07-04 03:38:22 +08:00
|
|
|
void BackspaceFixedRecord(IoErrorHandler &);
|
|
|
|
void BackspaceVariableUnformattedRecord(IoErrorHandler &);
|
|
|
|
void BackspaceVariableFormattedRecord(IoErrorHandler &);
|
2022-01-29 07:34:28 +08:00
|
|
|
bool SetVariableFormattedRecordLength();
|
2020-07-04 03:38:22 +08:00
|
|
|
void DoImpliedEndfile(IoErrorHandler &);
|
|
|
|
void DoEndfile(IoErrorHandler &);
|
2021-06-29 02:41:04 +08:00
|
|
|
void CommitWrites();
|
2022-02-18 09:22:09 +08:00
|
|
|
bool CheckDirectAccess(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};
|
2022-01-29 07:34:28 +08:00
|
|
|
bool impliedEndfile_{false}; // sequential/stream output has taken place
|
2020-10-01 03:53:00 +08:00
|
|
|
bool beganReadingRecord_{false};
|
2022-02-18 09:22:09 +08:00
|
|
|
bool directAccessRecWasSet_{false}; // REC= appeared
|
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>,
|
2021-06-26 01:40:08 +08:00
|
|
|
ExternalUnformattedIoStatementState<Direction::Output>,
|
|
|
|
ExternalUnformattedIoStatementState<Direction::Input>, InquireUnitState,
|
2022-02-17 06:55:19 +08:00
|
|
|
ExternalMiscIoStatementState, ErroneousIoStatementState>
|
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};
|
2021-06-26 01:40:08 +08:00
|
|
|
|
|
|
|
bool createdForInternalChildIo_{false};
|
|
|
|
|
|
|
|
// A stack of child I/O pseudo-units for user-defined derived type
|
|
|
|
// I/O that have this unit number.
|
|
|
|
OwningPtr<ChildIo> child_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A pseudo-unit for child I/O statements in user-defined derived type
|
|
|
|
// I/O subroutines; it forwards operations to the parent I/O statement,
|
|
|
|
// which can also be a child I/O statement.
|
|
|
|
class ChildIo {
|
|
|
|
public:
|
|
|
|
ChildIo(IoStatementState &parent, OwningPtr<ChildIo> &&previous)
|
|
|
|
: parent_{parent}, previous_{std::move(previous)} {}
|
|
|
|
|
|
|
|
IoStatementState &parent() const { return parent_; }
|
|
|
|
|
|
|
|
void EndIoStatement();
|
|
|
|
|
|
|
|
template <typename A, typename... X>
|
|
|
|
IoStatementState &BeginIoStatement(X &&...xs) {
|
|
|
|
A &state{u_.emplace<A>(std::forward<X>(xs)...)};
|
|
|
|
io_.emplace(state);
|
|
|
|
return *io_;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwningPtr<ChildIo> AcquirePrevious() { return std::move(previous_); }
|
|
|
|
|
2022-02-17 06:55:19 +08:00
|
|
|
Iostat CheckFormattingAndDirection(bool unformatted, Direction);
|
2021-06-26 01:40:08 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
IoStatementState &parent_;
|
|
|
|
OwningPtr<ChildIo> previous_;
|
|
|
|
std::variant<std::monostate,
|
|
|
|
ChildFormattedIoStatementState<Direction::Output>,
|
|
|
|
ChildFormattedIoStatementState<Direction::Input>,
|
|
|
|
ChildListIoStatementState<Direction::Output>,
|
|
|
|
ChildListIoStatementState<Direction::Input>,
|
|
|
|
ChildUnformattedIoStatementState<Direction::Output>,
|
2022-02-17 06:55:19 +08:00
|
|
|
ChildUnformattedIoStatementState<Direction::Input>, InquireUnitState,
|
|
|
|
ErroneousIoStatementState>
|
2021-06-26 01:40:08 +08:00
|
|
|
u_;
|
|
|
|
std::optional<IoStatementState> io_;
|
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_
|