2020-01-17 05:51:25 +08:00
|
|
|
//===-- runtime/io-stmt.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
|
|
|
// Representations of the state of an I/O statement in progress
|
2020-01-17 05:51:25 +08:00
|
|
|
|
|
|
|
#ifndef FORTRAN_RUNTIME_IO_STMT_H_
|
|
|
|
#define FORTRAN_RUNTIME_IO_STMT_H_
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
#include "connection.h"
|
2020-01-17 05:51:25 +08:00
|
|
|
#include "descriptor.h"
|
2020-02-05 08:55:45 +08:00
|
|
|
#include "file.h"
|
2020-01-17 05:51:25 +08:00
|
|
|
#include "format.h"
|
2020-02-05 08:55:45 +08:00
|
|
|
#include "internal-unit.h"
|
2020-08-04 02:35:29 +08:00
|
|
|
#include "io-api.h"
|
2020-01-17 05:51:25 +08:00
|
|
|
#include "io-error.h"
|
2020-02-05 08:55:45 +08:00
|
|
|
#include <functional>
|
2020-01-17 05:51:25 +08:00
|
|
|
#include <type_traits>
|
2020-02-05 08:55:45 +08:00
|
|
|
#include <variant>
|
2020-01-17 05:51:25 +08:00
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
class ExternalFileUnit;
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
class OpenStatementState;
|
2020-08-04 02:35:29 +08:00
|
|
|
class InquireUnitState;
|
|
|
|
class InquireNoUnitState;
|
|
|
|
class InquireUnconnectedFileState;
|
|
|
|
class InquireIOLengthState;
|
|
|
|
class ExternalMiscIoStatementState;
|
2020-02-05 08:55:45 +08:00
|
|
|
class CloseStatementState;
|
|
|
|
class NoopCloseStatementState;
|
2020-02-14 06:41:56 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction, typename CHAR = char>
|
2020-02-05 08:55:45 +08:00
|
|
|
class InternalFormattedIoStatementState;
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction, typename CHAR = char> class InternalListIoStatementState;
|
|
|
|
template <Direction, typename CHAR = char>
|
2020-02-05 08:55:45 +08:00
|
|
|
class ExternalFormattedIoStatementState;
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction> class ExternalListIoStatementState;
|
|
|
|
template <Direction> class UnformattedIoStatementState;
|
2020-02-05 08:55:45 +08:00
|
|
|
|
2020-10-02 00:32:48 +08:00
|
|
|
struct InputStatementState {};
|
|
|
|
struct OutputStatementState {};
|
|
|
|
template <Direction D>
|
|
|
|
using IoDirectionState = std::conditional_t<D == Direction::Input,
|
|
|
|
InputStatementState, OutputStatementState>;
|
|
|
|
struct FormattedIoStatementState {};
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
// The Cookie type in the I/O API is a pointer (for C) to this class.
|
|
|
|
class IoStatementState {
|
2020-01-17 05:51:25 +08:00
|
|
|
public:
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> explicit IoStatementState(A &x) : u_{x} {}
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
// These member functions each project themselves into the active alternative.
|
2020-02-14 06:41:56 +08:00
|
|
|
// They're used by per-data-item routines in the I/O API (e.g., OutputReal64)
|
2020-02-05 08:55:45 +08:00
|
|
|
// to interact with the state of the I/O statement in progress.
|
|
|
|
// This design avoids virtual member functions and function pointers,
|
2020-02-14 06:41:56 +08:00
|
|
|
// which may not have good support in some runtime environments.
|
|
|
|
std::optional<DataEdit> GetNextDataEdit(int = 1);
|
2020-07-22 08:37:35 +08:00
|
|
|
bool Emit(const char *, std::size_t, std::size_t elementBytes = 0);
|
2020-03-29 12:00:16 +08:00
|
|
|
std::optional<char32_t> GetCurrentChar(); // vacant after end of record
|
2020-02-05 08:55:45 +08:00
|
|
|
bool AdvanceRecord(int = 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
void BackspaceRecord();
|
|
|
|
void HandleRelativePosition(std::int64_t);
|
2020-02-05 08:55:45 +08:00
|
|
|
int EndIoStatement();
|
|
|
|
ConnectionState &GetConnectionState();
|
2020-02-14 06:41:56 +08:00
|
|
|
IoErrorHandler &GetIoErrorHandler() const;
|
2020-03-29 12:00:16 +08:00
|
|
|
ExternalFileUnit *GetExternalFileUnit() const; // null if internal unit
|
2020-02-05 08:55:45 +08:00
|
|
|
MutableModes &mutableModes();
|
2020-08-04 02:31:13 +08:00
|
|
|
void BeginReadingRecord();
|
2020-10-01 03:53:00 +08:00
|
|
|
void FinishReadingRecord();
|
2020-08-04 02:35:29 +08:00
|
|
|
bool Inquire(InquiryKeywordHash, char *, std::size_t);
|
|
|
|
bool Inquire(InquiryKeywordHash, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t, bool &); // PENDING=
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t &);
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
// N.B.: this also works with base classes
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> A *get_if() const {
|
2020-02-05 08:55:45 +08:00
|
|
|
return std::visit(
|
|
|
|
[](auto &x) -> A * {
|
|
|
|
if constexpr (std::is_convertible_v<decltype(x.get()), A &>) {
|
|
|
|
return &x.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
},
|
|
|
|
u_);
|
|
|
|
}
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
bool EmitRepeated(char, std::size_t);
|
|
|
|
bool EmitField(const char *, std::size_t length, std::size_t width);
|
2020-07-04 08:57:28 +08:00
|
|
|
|
|
|
|
std::optional<char32_t> SkipSpaces(std::optional<int> &remaining);
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<char32_t> NextInField(std::optional<int> &remaining);
|
2020-03-29 12:00:16 +08:00
|
|
|
std::optional<char32_t> GetNextNonBlank(); // can advance record
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-10-02 00:32:48 +08:00
|
|
|
template <Direction D> void CheckFormattedStmtType(const char *name) {
|
|
|
|
if (!get_if<FormattedIoStatementState>() ||
|
|
|
|
!get_if<IoDirectionState<D>>()) {
|
|
|
|
GetIoErrorHandler().Crash(
|
|
|
|
"%s called for I/O statement that is not formatted %s", name,
|
|
|
|
D == Direction::Output ? "output" : "input");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:59:27 +08:00
|
|
|
private:
|
2020-02-05 08:55:45 +08:00
|
|
|
std::variant<std::reference_wrapper<OpenStatementState>,
|
|
|
|
std::reference_wrapper<CloseStatementState>,
|
|
|
|
std::reference_wrapper<NoopCloseStatementState>,
|
2020-02-14 06:41:56 +08:00
|
|
|
std::reference_wrapper<
|
|
|
|
InternalFormattedIoStatementState<Direction::Output>>,
|
|
|
|
std::reference_wrapper<
|
|
|
|
InternalFormattedIoStatementState<Direction::Input>>,
|
|
|
|
std::reference_wrapper<InternalListIoStatementState<Direction::Output>>,
|
|
|
|
std::reference_wrapper<InternalListIoStatementState<Direction::Input>>,
|
|
|
|
std::reference_wrapper<
|
|
|
|
ExternalFormattedIoStatementState<Direction::Output>>,
|
|
|
|
std::reference_wrapper<
|
|
|
|
ExternalFormattedIoStatementState<Direction::Input>>,
|
|
|
|
std::reference_wrapper<ExternalListIoStatementState<Direction::Output>>,
|
|
|
|
std::reference_wrapper<ExternalListIoStatementState<Direction::Input>>,
|
|
|
|
std::reference_wrapper<UnformattedIoStatementState<Direction::Output>>,
|
2020-07-04 08:57:28 +08:00
|
|
|
std::reference_wrapper<UnformattedIoStatementState<Direction::Input>>,
|
2020-08-04 02:35:29 +08:00
|
|
|
std::reference_wrapper<InquireUnitState>,
|
|
|
|
std::reference_wrapper<InquireNoUnitState>,
|
|
|
|
std::reference_wrapper<InquireUnconnectedFileState>,
|
|
|
|
std::reference_wrapper<InquireIOLengthState>,
|
2020-07-04 08:57:28 +08:00
|
|
|
std::reference_wrapper<ExternalMiscIoStatementState>>
|
2020-02-05 08:55:45 +08:00
|
|
|
u_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Base class for all per-I/O statement state classes.
|
|
|
|
// Inherits IoErrorHandler from its base.
|
|
|
|
struct IoStatementBase : public DefaultFormatControlCallbacks {
|
|
|
|
using DefaultFormatControlCallbacks::DefaultFormatControlCallbacks;
|
|
|
|
int EndIoStatement();
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<DataEdit> GetNextDataEdit(IoStatementState &, int = 1);
|
|
|
|
ExternalFileUnit *GetExternalFileUnit() const { return nullptr; }
|
2020-08-04 02:31:13 +08:00
|
|
|
void BeginReadingRecord() {}
|
2020-10-01 03:53:00 +08:00
|
|
|
void FinishReadingRecord() {}
|
2020-08-04 02:35:29 +08:00
|
|
|
bool Inquire(InquiryKeywordHash, char *, std::size_t);
|
|
|
|
bool Inquire(InquiryKeywordHash, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t &);
|
|
|
|
void BadInquiryKeywordHashCrash(InquiryKeywordHash);
|
2020-02-05 08:55:45 +08:00
|
|
|
};
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
// Common state for list-directed internal & external I/O
|
2020-10-03 04:08:49 +08:00
|
|
|
template <Direction> class ListDirectedStatementState;
|
2020-10-02 00:32:48 +08:00
|
|
|
template <>
|
2020-10-03 04:08:49 +08:00
|
|
|
class ListDirectedStatementState<Direction::Output>
|
2020-10-02 00:32:48 +08:00
|
|
|
: public FormattedIoStatementState {
|
2020-10-03 04:08:49 +08:00
|
|
|
public:
|
2020-02-05 08:55:45 +08:00
|
|
|
static std::size_t RemainingSpaceInRecord(const ConnectionState &);
|
|
|
|
bool NeedAdvance(const ConnectionState &, std::size_t) const;
|
|
|
|
bool EmitLeadingSpaceOrAdvance(
|
|
|
|
IoStatementState &, std::size_t, bool isCharacter = false);
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<DataEdit> GetNextDataEdit(
|
|
|
|
IoStatementState &, int maxRepeat = 1);
|
2020-02-05 08:55:45 +08:00
|
|
|
bool lastWasUndelimitedCharacter{false};
|
2020-01-17 05:51:25 +08:00
|
|
|
};
|
2020-10-02 00:32:48 +08:00
|
|
|
template <>
|
|
|
|
class ListDirectedStatementState<Direction::Input>
|
|
|
|
: public FormattedIoStatementState {
|
2020-02-14 06:41:56 +08:00
|
|
|
public:
|
|
|
|
// Skips value separators, handles repetition and null values.
|
|
|
|
// Vacant when '/' appears; present with descriptor == ListDirectedNullValue
|
|
|
|
// when a null value appears.
|
|
|
|
std::optional<DataEdit> GetNextDataEdit(
|
|
|
|
IoStatementState &, int maxRepeat = 1);
|
|
|
|
|
|
|
|
private:
|
2020-03-29 12:00:16 +08:00
|
|
|
int remaining_{0}; // for "r*" repetition
|
2020-02-14 06:41:56 +08:00
|
|
|
std::int64_t initialRecordNumber_;
|
|
|
|
std::int64_t initialPositionInRecord_;
|
2020-03-29 12:00:16 +08:00
|
|
|
bool isFirstItem_{true}; // leading separator implies null first item
|
|
|
|
bool hitSlash_{false}; // once '/' is seen, nullify further items
|
2020-02-14 06:41:56 +08:00
|
|
|
bool realPart_{false};
|
|
|
|
bool imaginaryPart_{false};
|
|
|
|
};
|
2020-01-17 05:51:25 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR = char>
|
2020-02-05 08:55:45 +08:00
|
|
|
class InternalIoStatementState : public IoStatementBase,
|
2020-02-14 06:41:56 +08:00
|
|
|
public IoDirectionState<DIR> {
|
2020-01-17 05:51:25 +08:00
|
|
|
public:
|
2020-02-05 08:55:45 +08:00
|
|
|
using CharType = CHAR;
|
2020-02-14 06:41:56 +08:00
|
|
|
using Buffer =
|
|
|
|
std::conditional_t<DIR == Direction::Input, const CharType *, CharType *>;
|
2020-01-24 08:59:27 +08:00
|
|
|
InternalIoStatementState(Buffer, std::size_t,
|
|
|
|
const char *sourceFile = nullptr, int sourceLine = 0);
|
2020-02-05 08:55:45 +08:00
|
|
|
InternalIoStatementState(
|
|
|
|
const Descriptor &, const char *sourceFile = nullptr, int sourceLine = 0);
|
|
|
|
int EndIoStatement();
|
2020-07-22 08:37:35 +08:00
|
|
|
bool Emit(const CharType *, std::size_t chars /* not necessarily bytes */,
|
|
|
|
std::size_t elementBytes = 0);
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<char32_t> GetCurrentChar();
|
2020-02-05 08:55:45 +08:00
|
|
|
bool AdvanceRecord(int = 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
void BackspaceRecord();
|
2020-02-05 08:55:45 +08:00
|
|
|
ConnectionState &GetConnectionState() { return unit_; }
|
|
|
|
MutableModes &mutableModes() { return unit_.modes; }
|
2020-02-14 06:41:56 +08:00
|
|
|
void HandleRelativePosition(std::int64_t);
|
|
|
|
void HandleAbsolutePosition(std::int64_t);
|
2020-01-17 05:51:25 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool free_{true};
|
2020-02-14 06:41:56 +08:00
|
|
|
InternalDescriptorUnit<DIR> unit_;
|
2020-01-17 05:51:25 +08:00
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-01-24 08:59:27 +08:00
|
|
|
class InternalFormattedIoStatementState
|
2020-03-29 12:00:16 +08:00
|
|
|
: public InternalIoStatementState<DIR, CHAR>,
|
2020-10-02 00:32:48 +08:00
|
|
|
public FormattedIoStatementState {
|
2020-01-17 05:51:25 +08:00
|
|
|
public:
|
2020-02-05 08:55:45 +08:00
|
|
|
using CharType = CHAR;
|
2020-02-14 06:41:56 +08:00
|
|
|
using typename InternalIoStatementState<DIR, CharType>::Buffer;
|
2020-01-17 05:51:25 +08:00
|
|
|
InternalFormattedIoStatementState(Buffer internal, std::size_t internalLength,
|
2020-02-05 08:55:45 +08:00
|
|
|
const CharType *format, std::size_t formatLength,
|
2020-01-17 05:51:25 +08:00
|
|
|
const char *sourceFile = nullptr, int sourceLine = 0);
|
2020-02-05 08:55:45 +08:00
|
|
|
InternalFormattedIoStatementState(const Descriptor &, const CharType *format,
|
|
|
|
std::size_t formatLength, const char *sourceFile = nullptr,
|
|
|
|
int sourceLine = 0);
|
|
|
|
IoStatementState &ioStatementState() { return ioStatementState_; }
|
|
|
|
int EndIoStatement();
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<DataEdit> GetNextDataEdit(
|
|
|
|
IoStatementState &, int maxRepeat = 1) {
|
2020-02-05 08:55:45 +08:00
|
|
|
return format_.GetNextDataEdit(*this, maxRepeat);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
|
|
|
private:
|
2020-03-29 12:00:16 +08:00
|
|
|
IoStatementState ioStatementState_; // points to *this
|
2020-02-14 06:41:56 +08:00
|
|
|
using InternalIoStatementState<DIR, CharType>::unit_;
|
2020-02-05 08:55:45 +08:00
|
|
|
// format_ *must* be last; it may be partial someday
|
|
|
|
FormatControl<InternalFormattedIoStatementState> format_;
|
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
class InternalListIoStatementState : public InternalIoStatementState<DIR, CHAR>,
|
|
|
|
public ListDirectedStatementState<DIR> {
|
2020-02-05 08:55:45 +08:00
|
|
|
public:
|
|
|
|
using CharType = CHAR;
|
2020-02-14 06:41:56 +08:00
|
|
|
using typename InternalIoStatementState<DIR, CharType>::Buffer;
|
2020-02-05 08:55:45 +08:00
|
|
|
InternalListIoStatementState(Buffer internal, std::size_t internalLength,
|
|
|
|
const char *sourceFile = nullptr, int sourceLine = 0);
|
|
|
|
InternalListIoStatementState(
|
|
|
|
const Descriptor &, const char *sourceFile = nullptr, int sourceLine = 0);
|
|
|
|
IoStatementState &ioStatementState() { return ioStatementState_; }
|
2020-02-14 06:41:56 +08:00
|
|
|
using ListDirectedStatementState<DIR>::GetNextDataEdit;
|
2020-02-05 08:55:45 +08:00
|
|
|
|
|
|
|
private:
|
2020-03-29 12:00:16 +08:00
|
|
|
IoStatementState ioStatementState_; // points to *this
|
2020-02-14 06:41:56 +08:00
|
|
|
using InternalIoStatementState<DIR, CharType>::unit_;
|
2020-02-05 08:55:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ExternalIoStatementBase : public IoStatementBase {
|
|
|
|
public:
|
|
|
|
ExternalIoStatementBase(
|
|
|
|
ExternalFileUnit &, const char *sourceFile = nullptr, int sourceLine = 0);
|
|
|
|
ExternalFileUnit &unit() { return unit_; }
|
|
|
|
MutableModes &mutableModes();
|
|
|
|
ConnectionState &GetConnectionState();
|
2020-01-17 05:51:25 +08:00
|
|
|
int EndIoStatement();
|
2020-02-14 06:41:56 +08:00
|
|
|
ExternalFileUnit *GetExternalFileUnit() { return &unit_; }
|
2020-01-17 05:51:25 +08:00
|
|
|
|
|
|
|
private:
|
2020-02-05 08:55:45 +08:00
|
|
|
ExternalFileUnit &unit_;
|
2020-01-17 05:51:25 +08:00
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-02-05 08:55:45 +08:00
|
|
|
class ExternalIoStatementState : public ExternalIoStatementBase,
|
2020-02-14 06:41:56 +08:00
|
|
|
public IoDirectionState<DIR> {
|
2020-01-24 08:59:27 +08:00
|
|
|
public:
|
2020-02-05 08:55:45 +08:00
|
|
|
using ExternalIoStatementBase::ExternalIoStatementBase;
|
|
|
|
int EndIoStatement();
|
2020-07-22 08:37:35 +08:00
|
|
|
bool Emit(const char *, std::size_t, std::size_t elementBytes = 0);
|
2020-02-05 08:55:45 +08:00
|
|
|
bool Emit(const char16_t *, std::size_t chars /* not bytes */);
|
|
|
|
bool Emit(const char32_t *, std::size_t chars /* not bytes */);
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<char32_t> GetCurrentChar();
|
2020-02-05 08:55:45 +08:00
|
|
|
bool AdvanceRecord(int = 1);
|
2020-02-14 06:41:56 +08:00
|
|
|
void BackspaceRecord();
|
|
|
|
void HandleRelativePosition(std::int64_t);
|
|
|
|
void HandleAbsolutePosition(std::int64_t);
|
2020-08-04 02:31:13 +08:00
|
|
|
void BeginReadingRecord();
|
2020-10-01 03:53:00 +08:00
|
|
|
void FinishReadingRecord();
|
2020-02-05 08:55:45 +08:00
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR, typename CHAR>
|
2020-02-14 06:41:56 +08:00
|
|
|
class ExternalFormattedIoStatementState : public ExternalIoStatementState<DIR>,
|
2020-10-02 00:32:48 +08:00
|
|
|
public FormattedIoStatementState {
|
2020-02-05 08:55:45 +08:00
|
|
|
public:
|
|
|
|
using CharType = CHAR;
|
|
|
|
ExternalFormattedIoStatementState(ExternalFileUnit &, const CharType *format,
|
2020-01-24 08:59:27 +08:00
|
|
|
std::size_t formatLength, const char *sourceFile = nullptr,
|
|
|
|
int sourceLine = 0);
|
2020-02-05 08:55:45 +08:00
|
|
|
MutableModes &mutableModes() { return mutableModes_; }
|
|
|
|
int EndIoStatement();
|
2020-02-14 06:41:56 +08:00
|
|
|
std::optional<DataEdit> GetNextDataEdit(
|
|
|
|
IoStatementState &, int maxRepeat = 1) {
|
2020-02-05 08:55:45 +08:00
|
|
|
return format_.GetNextDataEdit(*this, maxRepeat);
|
2020-01-24 08:59:27 +08:00
|
|
|
}
|
2020-02-05 08:55:45 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// These are forked from ConnectionState's modes at the beginning
|
|
|
|
// of each formatted I/O statement so they may be overridden by control
|
|
|
|
// edit descriptors during the statement.
|
|
|
|
MutableModes mutableModes_;
|
|
|
|
FormatControl<ExternalFormattedIoStatementState> format_;
|
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-02-14 06:41:56 +08:00
|
|
|
class ExternalListIoStatementState : public ExternalIoStatementState<DIR>,
|
|
|
|
public ListDirectedStatementState<DIR> {
|
2020-02-05 08:55:45 +08:00
|
|
|
public:
|
2020-02-14 06:41:56 +08:00
|
|
|
using ExternalIoStatementState<DIR>::ExternalIoStatementState;
|
|
|
|
using ListDirectedStatementState<DIR>::GetNextDataEdit;
|
2020-02-05 08:55:45 +08:00
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <Direction DIR>
|
2020-02-14 06:41:56 +08:00
|
|
|
class UnformattedIoStatementState : public ExternalIoStatementState<DIR> {
|
2020-02-05 08:55:45 +08:00
|
|
|
public:
|
2020-02-14 06:41:56 +08:00
|
|
|
using ExternalIoStatementState<DIR>::ExternalIoStatementState;
|
2020-07-22 08:37:35 +08:00
|
|
|
bool Receive(char *, std::size_t, std::size_t elementBytes = 0);
|
|
|
|
bool Emit(const char *, std::size_t, std::size_t elementBytes = 0);
|
2020-02-05 08:55:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class OpenStatementState : public ExternalIoStatementBase {
|
|
|
|
public:
|
|
|
|
OpenStatementState(ExternalFileUnit &unit, bool wasExtant,
|
|
|
|
const char *sourceFile = nullptr, int sourceLine = 0)
|
2020-03-29 12:00:16 +08:00
|
|
|
: ExternalIoStatementBase{unit, sourceFile, sourceLine}, wasExtant_{
|
|
|
|
wasExtant} {}
|
2020-02-05 08:55:45 +08:00
|
|
|
bool wasExtant() const { return wasExtant_; }
|
2020-07-18 02:24:29 +08:00
|
|
|
void set_status(OpenStatus status) { status_ = status; } // STATUS=
|
2020-08-04 02:35:29 +08:00
|
|
|
void set_path(const char *, std::size_t); // FILE=
|
2020-03-29 12:00:16 +08:00
|
|
|
void set_position(Position position) { position_ = position; } // POSITION=
|
2020-07-18 02:24:29 +08:00
|
|
|
void set_action(Action action) { action_ = action; } // ACTION=
|
2020-07-22 08:37:35 +08:00
|
|
|
void set_convert(Convert convert) { convert_ = convert; } // CONVERT=
|
2020-08-04 02:35:29 +08:00
|
|
|
void set_access(Access access) { access_ = access; } // ACCESS=
|
|
|
|
void set_isUnformatted(bool yes = true) { isUnformatted_ = yes; } // FORM=
|
2020-02-05 08:55:45 +08:00
|
|
|
int EndIoStatement();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool wasExtant_;
|
2020-07-18 08:02:47 +08:00
|
|
|
std::optional<OpenStatus> status_;
|
2020-02-05 08:55:45 +08:00
|
|
|
Position position_{Position::AsIs};
|
2020-07-18 02:24:29 +08:00
|
|
|
std::optional<Action> action_;
|
2020-07-22 08:37:35 +08:00
|
|
|
Convert convert_{Convert::Native};
|
2020-02-05 08:55:45 +08:00
|
|
|
OwningPtr<char> path_;
|
|
|
|
std::size_t pathLength_;
|
2020-08-04 02:35:29 +08:00
|
|
|
std::optional<bool> isUnformatted_;
|
|
|
|
std::optional<Access> access_;
|
2020-02-05 08:55:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CloseStatementState : public ExternalIoStatementBase {
|
|
|
|
public:
|
|
|
|
CloseStatementState(ExternalFileUnit &unit, const char *sourceFile = nullptr,
|
|
|
|
int sourceLine = 0)
|
2020-03-29 12:00:16 +08:00
|
|
|
: ExternalIoStatementBase{unit, sourceFile, sourceLine} {}
|
2020-02-05 08:55:45 +08:00
|
|
|
void set_status(CloseStatus status) { status_ = status; }
|
|
|
|
int EndIoStatement();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CloseStatus status_{CloseStatus::Keep};
|
|
|
|
};
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
// For CLOSE(bad unit) and INQUIRE(unconnected unit)
|
|
|
|
class NoUnitIoStatementState : public IoStatementBase {
|
2020-02-05 08:55:45 +08:00
|
|
|
public:
|
|
|
|
IoStatementState &ioStatementState() { return ioStatementState_; }
|
|
|
|
MutableModes &mutableModes() { return connection_.modes; }
|
|
|
|
ConnectionState &GetConnectionState() { return connection_; }
|
2020-01-24 08:59:27 +08:00
|
|
|
int EndIoStatement();
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
protected:
|
|
|
|
template <typename A>
|
|
|
|
NoUnitIoStatementState(const char *sourceFile, int sourceLine, A &stmt)
|
|
|
|
: IoStatementBase{sourceFile, sourceLine}, ioStatementState_{stmt} {}
|
|
|
|
|
2020-01-24 08:59:27 +08:00
|
|
|
private:
|
2020-03-29 12:00:16 +08:00
|
|
|
IoStatementState ioStatementState_; // points to *this
|
2020-02-05 08:55:45 +08:00
|
|
|
ConnectionState connection_;
|
2020-01-24 08:59:27 +08:00
|
|
|
};
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
class NoopCloseStatementState : public NoUnitIoStatementState {
|
|
|
|
public:
|
|
|
|
NoopCloseStatementState(const char *sourceFile, int sourceLine)
|
|
|
|
: NoUnitIoStatementState{sourceFile, sourceLine, *this} {}
|
|
|
|
void set_status(CloseStatus) {} // discards
|
|
|
|
};
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
extern template class InternalIoStatementState<Direction::Output>;
|
|
|
|
extern template class InternalIoStatementState<Direction::Input>;
|
|
|
|
extern template class InternalFormattedIoStatementState<Direction::Output>;
|
|
|
|
extern template class InternalFormattedIoStatementState<Direction::Input>;
|
|
|
|
extern template class InternalListIoStatementState<Direction::Output>;
|
|
|
|
extern template class InternalListIoStatementState<Direction::Input>;
|
|
|
|
extern template class ExternalIoStatementState<Direction::Output>;
|
|
|
|
extern template class ExternalIoStatementState<Direction::Input>;
|
|
|
|
extern template class ExternalFormattedIoStatementState<Direction::Output>;
|
|
|
|
extern template class ExternalFormattedIoStatementState<Direction::Input>;
|
|
|
|
extern template class ExternalListIoStatementState<Direction::Output>;
|
|
|
|
extern template class ExternalListIoStatementState<Direction::Input>;
|
|
|
|
extern template class UnformattedIoStatementState<Direction::Output>;
|
|
|
|
extern template class UnformattedIoStatementState<Direction::Input>;
|
|
|
|
extern template class FormatControl<
|
|
|
|
InternalFormattedIoStatementState<Direction::Output>>;
|
|
|
|
extern template class FormatControl<
|
|
|
|
InternalFormattedIoStatementState<Direction::Input>>;
|
|
|
|
extern template class FormatControl<
|
|
|
|
ExternalFormattedIoStatementState<Direction::Output>>;
|
|
|
|
extern template class FormatControl<
|
|
|
|
ExternalFormattedIoStatementState<Direction::Input>>;
|
2020-01-17 05:51:25 +08:00
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
class InquireUnitState : public ExternalIoStatementBase {
|
|
|
|
public:
|
|
|
|
InquireUnitState(ExternalFileUnit &unit, const char *sourceFile = nullptr,
|
|
|
|
int sourceLine = 0);
|
|
|
|
bool Inquire(InquiryKeywordHash, char *, std::size_t);
|
|
|
|
bool Inquire(InquiryKeywordHash, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t &);
|
|
|
|
};
|
|
|
|
|
|
|
|
class InquireNoUnitState : public NoUnitIoStatementState {
|
|
|
|
public:
|
|
|
|
InquireNoUnitState(const char *sourceFile = nullptr, int sourceLine = 0);
|
|
|
|
bool Inquire(InquiryKeywordHash, char *, std::size_t);
|
|
|
|
bool Inquire(InquiryKeywordHash, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t &);
|
|
|
|
};
|
|
|
|
|
|
|
|
class InquireUnconnectedFileState : public NoUnitIoStatementState {
|
|
|
|
public:
|
|
|
|
InquireUnconnectedFileState(OwningPtr<char> &&path,
|
|
|
|
const char *sourceFile = nullptr, int sourceLine = 0);
|
|
|
|
bool Inquire(InquiryKeywordHash, char *, std::size_t);
|
|
|
|
bool Inquire(InquiryKeywordHash, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t, bool &);
|
|
|
|
bool Inquire(InquiryKeywordHash, std::int64_t &);
|
|
|
|
|
|
|
|
private:
|
|
|
|
OwningPtr<char> path_; // trimmed and NUL terminated
|
|
|
|
};
|
|
|
|
|
|
|
|
class InquireIOLengthState : public NoUnitIoStatementState,
|
|
|
|
public OutputStatementState {
|
|
|
|
public:
|
|
|
|
InquireIOLengthState(const char *sourceFile = nullptr, int sourceLine = 0);
|
|
|
|
std::size_t bytes() const { return bytes_; }
|
|
|
|
bool Emit(const char *, std::size_t, std::size_t elementBytes = 0);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::size_t bytes_{0};
|
|
|
|
};
|
|
|
|
|
2020-07-04 08:57:28 +08:00
|
|
|
class ExternalMiscIoStatementState : public ExternalIoStatementBase {
|
|
|
|
public:
|
|
|
|
enum Which { Flush, Backspace, Endfile, Rewind };
|
|
|
|
ExternalMiscIoStatementState(ExternalFileUnit &unit, Which which,
|
|
|
|
const char *sourceFile = nullptr, int sourceLine = 0)
|
|
|
|
: ExternalIoStatementBase{unit, sourceFile, sourceLine}, which_{which} {}
|
|
|
|
int EndIoStatement();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Which which_;
|
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|
|
|
|
#endif // FORTRAN_RUNTIME_IO_STMT_H_
|