| 1 |
//===-- runtime/pseudo-unit.cpp -------------------------------------------===// |
| 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 |
// Implemenation of ExternalFileUnit and PseudoOpenFile for |
| 10 |
// RT_USE_PSEUDO_FILE_UNIT=1. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "io-error.h" |
| 15 |
#include "tools.h" |
| 16 |
#include "unit.h" |
| 17 |
|
| 18 |
// NOTE: the header files above may define OpenMP declare target |
| 19 |
// variables, so they have to be included unconditionally |
| 20 |
// so that the offload entries are consistent between host and device. |
| 21 |
#if defined(RT_USE_PSEUDO_FILE_UNIT) |
| 22 |
#include <cstdio> |
| 23 |
|
| 24 |
namespace Fortran::runtime::io { |
| 25 |
|
| 26 |
void FlushOutputOnCrash(const Terminator &) {} |
| 27 |
|
| 28 |
ExternalFileUnit *ExternalFileUnit::LookUp(int) { |
| 29 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 30 |
} |
| 31 |
|
| 32 |
ExternalFileUnit *ExternalFileUnit::LookUpOrCreate( |
| 33 |
int, const Terminator &, bool &) { |
| 34 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 35 |
} |
| 36 |
|
| 37 |
ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit, |
| 38 |
Direction direction, Fortran::common::optional<bool>, |
| 39 |
IoErrorHandler &handler) { |
| 40 |
if (direction != Direction::Output) { |
| 41 |
handler.Crash("ExternalFileUnit only supports output IO"); |
| 42 |
} |
| 43 |
return New<ExternalFileUnit>{handler}(unit).release(); |
| 44 |
} |
| 45 |
|
| 46 |
ExternalFileUnit *ExternalFileUnit::LookUp(const char *, std::size_t) { |
| 47 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 48 |
} |
| 49 |
|
| 50 |
ExternalFileUnit &ExternalFileUnit::CreateNew(int, const Terminator &) { |
| 51 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 52 |
} |
| 53 |
|
| 54 |
ExternalFileUnit *ExternalFileUnit::LookUpForClose(int) { |
| 55 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 56 |
} |
| 57 |
|
| 58 |
ExternalFileUnit &ExternalFileUnit::NewUnit(const Terminator &, bool) { |
| 59 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 60 |
} |
| 61 |
|
| 62 |
bool ExternalFileUnit::OpenUnit(Fortran::common::optional<OpenStatus> status, |
| 63 |
Fortran::common::optional<Action>, Position, OwningPtr<char> &&, |
| 64 |
std::size_t, Convert, IoErrorHandler &handler) { |
| 65 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 66 |
} |
| 67 |
|
| 68 |
bool ExternalFileUnit::OpenAnonymousUnit(Fortran::common::optional<OpenStatus>, |
| 69 |
Fortran::common::optional<Action>, Position, Convert convert, |
| 70 |
IoErrorHandler &handler) { |
| 71 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 72 |
} |
| 73 |
|
| 74 |
void ExternalFileUnit::CloseUnit(CloseStatus, IoErrorHandler &handler) { |
| 75 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 76 |
} |
| 77 |
|
| 78 |
void ExternalFileUnit::DestroyClosed() { |
| 79 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 80 |
} |
| 81 |
|
| 82 |
Iostat ExternalFileUnit::SetDirection(Direction direction) { |
| 83 |
if (direction != Direction::Output) { |
| 84 |
return IostatReadFromWriteOnly; |
| 85 |
} |
| 86 |
direction_ = direction; |
| 87 |
return IostatOk; |
| 88 |
} |
| 89 |
|
| 90 |
void ExternalFileUnit::CloseAll(IoErrorHandler &) {} |
| 91 |
|
| 92 |
void ExternalFileUnit::FlushAll(IoErrorHandler &) {} |
| 93 |
|
| 94 |
int ExternalFileUnit::GetAsynchronousId(IoErrorHandler &handler) { |
| 95 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 96 |
} |
| 97 |
|
| 98 |
bool ExternalFileUnit::Wait(int) { |
| 99 |
Terminator{__FILE__, __LINE__}.Crash("unsupported"); |
| 100 |
} |
| 101 |
|
| 102 |
void PseudoOpenFile::set_mayAsynchronous(bool yes) { |
| 103 |
if (yes) { |
| 104 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
Fortran::common::optional<PseudoOpenFile::FileOffset> |
| 109 |
PseudoOpenFile::knownSize() const { |
| 110 |
Terminator{__FILE__, __LINE__}.Crash("unsupported"); |
| 111 |
} |
| 112 |
|
| 113 |
void PseudoOpenFile::Open(OpenStatus, Fortran::common::optional<Action>, |
| 114 |
Position, IoErrorHandler &handler) { |
| 115 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 116 |
} |
| 117 |
|
| 118 |
void PseudoOpenFile::Close(CloseStatus, IoErrorHandler &handler) { |
| 119 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 120 |
} |
| 121 |
|
| 122 |
std::size_t PseudoOpenFile::Read( |
| 123 |
FileOffset, char *, std::size_t, std::size_t, IoErrorHandler &handler) { |
| 124 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 125 |
} |
| 126 |
|
| 127 |
std::size_t PseudoOpenFile::Write(FileOffset at, const char *buffer, |
| 128 |
std::size_t bytes, IoErrorHandler &handler) { |
| 129 |
if (at) { |
| 130 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 131 |
} |
| 132 |
// TODO: use persistent string buffer that can be reallocated |
| 133 |
// as needed, and only freed at destruction of *this. |
| 134 |
auto string{SizedNew<char>{handler}(bytes + 1)}; |
| 135 |
std::memcpy(string.get(), buffer, bytes); |
| 136 |
string.get()[bytes] = '\0'; |
| 137 |
std::printf("%s", string.get()); |
| 138 |
return bytes; |
| 139 |
} |
| 140 |
|
| 141 |
void PseudoOpenFile::Truncate(FileOffset, IoErrorHandler &handler) { |
| 142 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 143 |
} |
| 144 |
|
| 145 |
int PseudoOpenFile::ReadAsynchronously( |
| 146 |
FileOffset, char *, std::size_t, IoErrorHandler &handler) { |
| 147 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 148 |
} |
| 149 |
|
| 150 |
int PseudoOpenFile::WriteAsynchronously( |
| 151 |
FileOffset, const char *, std::size_t, IoErrorHandler &handler) { |
| 152 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 153 |
} |
| 154 |
|
| 155 |
void PseudoOpenFile::Wait(int, IoErrorHandler &handler) { |
| 156 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 157 |
} |
| 158 |
|
| 159 |
void PseudoOpenFile::WaitAll(IoErrorHandler &handler) { |
| 160 |
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 161 |
} |
| 162 |
|
| 163 |
Position PseudoOpenFile::InquirePosition() const { |
| 164 |
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION); |
| 165 |
} |
| 166 |
|
| 167 |
} // namespace Fortran::runtime::io |
| 168 |
|
| 169 |
#endif // defined(RT_USE_PSEUDO_FILE_UNIT) |