|
| 1 | +//===-- runtime/io-api-common.h ---------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef FLANG_RUNTIME_IO_API_COMMON_H_ |
| 10 | +#define FLANG_RUNTIME_IO_API_COMMON_H_ |
| 11 | + |
| 12 | +#include "io-stmt.h" |
| 13 | +#include "terminator.h" |
| 14 | +#include "unit.h" |
| 15 | +#include "flang/Common/api-attrs.h" |
| 16 | +#include "flang/Common/optional.h" |
| 17 | +#include "flang/Runtime/io-api.h" |
| 18 | + |
| 19 | +namespace Fortran::runtime::io { |
| 20 | + |
| 21 | +static inline RT_API_ATTRS Cookie NoopUnit(const Terminator &terminator, |
| 22 | + int unitNumber, enum Iostat iostat = IostatOk) { |
| 23 | + Cookie cookie{&New<NoopStatementState>{terminator}( |
| 24 | + terminator.sourceFileName(), terminator.sourceLine(), unitNumber) |
| 25 | + .release() |
| 26 | + ->ioStatementState()}; |
| 27 | + if (iostat != IostatOk) { |
| 28 | + cookie->GetIoErrorHandler().SetPendingError(iostat); |
| 29 | + } |
| 30 | + return cookie; |
| 31 | +} |
| 32 | + |
| 33 | +static inline RT_API_ATTRS ExternalFileUnit *GetOrCreateUnit(int unitNumber, |
| 34 | + Direction direction, Fortran::common::optional<bool> isUnformatted, |
| 35 | + const Terminator &terminator, Cookie &errorCookie) { |
| 36 | + if (ExternalFileUnit * |
| 37 | + unit{ExternalFileUnit::LookUpOrCreateAnonymous( |
| 38 | + unitNumber, direction, isUnformatted, terminator)}) { |
| 39 | + errorCookie = nullptr; |
| 40 | + return unit; |
| 41 | + } else { |
| 42 | + errorCookie = NoopUnit(terminator, unitNumber, IostatBadUnitNumber); |
| 43 | + return nullptr; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +template <Direction DIR, template <Direction> class STATE, typename... A> |
| 48 | +RT_API_ATTRS Cookie BeginExternalListIO( |
| 49 | + int unitNumber, const char *sourceFile, int sourceLine, A &&...xs) { |
| 50 | + Terminator terminator{sourceFile, sourceLine}; |
| 51 | + Cookie errorCookie{nullptr}; |
| 52 | + ExternalFileUnit *unit{GetOrCreateUnit( |
| 53 | + unitNumber, DIR, false /*!unformatted*/, terminator, errorCookie)}; |
| 54 | + if (!unit) { |
| 55 | + return errorCookie; |
| 56 | + } |
| 57 | + if (!unit->isUnformatted.has_value()) { |
| 58 | + unit->isUnformatted = false; |
| 59 | + } |
| 60 | + Iostat iostat{IostatOk}; |
| 61 | + if (*unit->isUnformatted) { |
| 62 | + iostat = IostatFormattedIoOnUnformattedUnit; |
| 63 | + } |
| 64 | + if (ChildIo * child{unit->GetChildIo()}) { |
| 65 | + if (iostat == IostatOk) { |
| 66 | + iostat = child->CheckFormattingAndDirection(false, DIR); |
| 67 | + } |
| 68 | + if (iostat == IostatOk) { |
| 69 | + return &child->BeginIoStatement<ChildListIoStatementState<DIR>>( |
| 70 | + *child, sourceFile, sourceLine); |
| 71 | + } else { |
| 72 | + return &child->BeginIoStatement<ErroneousIoStatementState>( |
| 73 | + iostat, nullptr /* no unit */, sourceFile, sourceLine); |
| 74 | + } |
| 75 | + } else { |
| 76 | + if (iostat == IostatOk && unit->access == Access::Direct) { |
| 77 | + iostat = IostatListIoOnDirectAccessUnit; |
| 78 | + } |
| 79 | + if (iostat == IostatOk) { |
| 80 | + iostat = unit->SetDirection(DIR); |
| 81 | + } |
| 82 | + if (iostat == IostatOk) { |
| 83 | + return &unit->BeginIoStatement<STATE<DIR>>( |
| 84 | + terminator, std::forward<A>(xs)..., *unit, sourceFile, sourceLine); |
| 85 | + } else { |
| 86 | + return &unit->BeginIoStatement<ErroneousIoStatementState>( |
| 87 | + terminator, iostat, unit, sourceFile, sourceLine); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace Fortran::runtime::io |
| 93 | +#endif // FLANG_RUNTIME_IO_API_COMMON_H_ |
0 commit comments