Peter Klausler deb62f5ad6 [flang][runtime] Clean up asynchronous I/O APIs
Now that the requirements and implementation of asynchronous I/O are
better understood, adjust their I/O runtime APIs.  In particular:
1) Remove the BeginAsynchronousOutput/Input APIs; they're not needed,
   since any data transfer statement might have ASYNCHRONOUS= and
   (if ASYNCHRONOUS='YES') ID= control list specifiers that need to
   at least be checked.
2) Add implementations for BeginWait(All) to check for the error
   case of a bad unit number and nonzero ID=.
3) Rearrange and comment SetAsynchronous so that it's clear that
   it can be called for READ/WRITE as well as for OPEN.

The implementation remains completely synchronous, but should be conforming.
Where opportunities make sense for true asynchronous implementations of
some big block transfers without SIZE= in the future, we'll need to add
a GetAsynchronousId API to capture ID= on a READ or WRITE; add sourceFile
and sourceLine arguments to BeginWait(All) for good error reporting;
track pending operations in unit.h; and add code to force synchronization
to non-asynchronous I/O operations.

Lowering should call SetAsynchronous when ASYNCHRONOUS= appears as
a control list specifier.  It should also set ID=x variables to 0
until such time as we support asynchronous operations, if ever.
This patch only removes the removed APIs from lowering.

Differential Revision: https://reviews.llvm.org/D126143
2022-05-24 07:54:57 -07:00

78 lines
2.6 KiB
C++

//===-- runtime/io-error.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
//
//===----------------------------------------------------------------------===//
// Distinguishes I/O error conditions; fatal ones lead to termination,
// and those that the user program has chosen to handle are recorded
// so that the highest-priority one can be returned as IOSTAT=.
// IOSTAT error codes are raw errno values augmented with values for
// Fortran-specific errors.
#ifndef FORTRAN_RUNTIME_IO_ERROR_H_
#define FORTRAN_RUNTIME_IO_ERROR_H_
#include "terminator.h"
#include "flang/Runtime/iostat.h"
#include "flang/Runtime/memory.h"
#include <cinttypes>
namespace Fortran::runtime::io {
// See 12.11 in Fortran 2018
class IoErrorHandler : public Terminator {
public:
using Terminator::Terminator;
explicit IoErrorHandler(const Terminator &that) : Terminator{that} {}
void HasIoStat() { flags_ |= hasIoStat; }
void HasErrLabel() { flags_ |= hasErr; }
void HasEndLabel() { flags_ |= hasEnd; }
void HasEorLabel() { flags_ |= hasEor; }
void HasIoMsg() { flags_ |= hasIoMsg; }
bool InError() const {
return ioStat_ != IostatOk || pendingError_ != IostatOk;
}
// For I/O statements that detect fatal errors in their
// Begin...() API routines before it is known whether they
// have error handling control list items. Such statements
// have an ErroneousIoStatementState with a pending error.
void SetPendingError(int iostat) { pendingError_ = iostat; }
void SignalError(int iostatOrErrno, const char *msg, ...);
void SignalError(int iostatOrErrno);
template <typename... X> void SignalError(const char *msg, X &&...xs) {
SignalError(IostatGenericError, msg, std::forward<X>(xs)...);
}
void Forward(int iostatOrErrno, const char *, std::size_t);
void SignalErrno(); // SignalError(errno)
void SignalEnd(); // input only; EOF on internal write is an error
void SignalEor(); // non-advancing input only; EOR on write is an error
void SignalPendingError();
int GetIoStat() const { return ioStat_; }
bool GetIoMsg(char *, std::size_t);
private:
enum Flag : std::uint8_t {
hasIoStat = 1, // IOSTAT=
hasErr = 2, // ERR=
hasEnd = 4, // END=
hasEor = 8, // EOR=
hasIoMsg = 16, // IOMSG=
};
std::uint8_t flags_{0};
int ioStat_{IostatOk};
OwningPtr<char> ioMsg_;
int pendingError_{IostatOk};
};
} // namespace Fortran::runtime::io
#endif // FORTRAN_RUNTIME_IO_ERROR_H_