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
FlushOutputOnCrash(const Terminator &)26 void FlushOutputOnCrash(const Terminator &) {}
27
LookUp(int)28 ExternalFileUnit *ExternalFileUnit::LookUp(int) {
29 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
30 }
31
LookUpOrCreate(int,const Terminator &,bool &)32 ExternalFileUnit *ExternalFileUnit::LookUpOrCreate(
33 int, const Terminator &, bool &) {
34 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
35 }
36
LookUpOrCreateAnonymous(int unit,Direction direction,Fortran::common::optional<bool>,IoErrorHandler & handler)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
LookUp(const char *,std::size_t)46 ExternalFileUnit *ExternalFileUnit::LookUp(const char *, std::size_t) {
47 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
48 }
49
CreateNew(int,const Terminator &)50 ExternalFileUnit &ExternalFileUnit::CreateNew(int, const Terminator &) {
51 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
52 }
53
LookUpForClose(int)54 ExternalFileUnit *ExternalFileUnit::LookUpForClose(int) {
55 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
56 }
57
NewUnit(const Terminator &,bool)58 ExternalFileUnit &ExternalFileUnit::NewUnit(const Terminator &, bool) {
59 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
60 }
61
OpenUnit(Fortran::common::optional<OpenStatus> status,Fortran::common::optional<Action>,Position,OwningPtr<char> &&,std::size_t,Convert,IoErrorHandler & handler)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
OpenAnonymousUnit(Fortran::common::optional<OpenStatus>,Fortran::common::optional<Action>,Position,Convert convert,IoErrorHandler & handler)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
CloseUnit(CloseStatus,IoErrorHandler & handler)74 void ExternalFileUnit::CloseUnit(CloseStatus, IoErrorHandler &handler) {
75 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
76 }
77
DestroyClosed()78 void ExternalFileUnit::DestroyClosed() {
79 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
80 }
81
SetDirection(Direction direction)82 Iostat ExternalFileUnit::SetDirection(Direction direction) {
83 if (direction != Direction::Output) {
84 return IostatReadFromWriteOnly;
85 }
86 direction_ = direction;
87 return IostatOk;
88 }
89
CloseAll(IoErrorHandler &)90 void ExternalFileUnit::CloseAll(IoErrorHandler &) {}
91
FlushAll(IoErrorHandler &)92 void ExternalFileUnit::FlushAll(IoErrorHandler &) {}
93
GetAsynchronousId(IoErrorHandler & handler)94 int ExternalFileUnit::GetAsynchronousId(IoErrorHandler &handler) {
95 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
96 }
97
Wait(int)98 bool ExternalFileUnit::Wait(int) {
99 Terminator{__FILE__, __LINE__}.Crash("unsupported");
100 }
101
set_mayAsynchronous(bool yes)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>
knownSize() const109 PseudoOpenFile::knownSize() const {
110 Terminator{__FILE__, __LINE__}.Crash("unsupported");
111 }
112
Open(OpenStatus,Fortran::common::optional<Action>,Position,IoErrorHandler & handler)113 void PseudoOpenFile::Open(OpenStatus, Fortran::common::optional<Action>,
114 Position, IoErrorHandler &handler) {
115 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
116 }
117
Close(CloseStatus,IoErrorHandler & handler)118 void PseudoOpenFile::Close(CloseStatus, IoErrorHandler &handler) {
119 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
120 }
121
Read(FileOffset,char *,std::size_t,std::size_t,IoErrorHandler & handler)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
Write(FileOffset at,const char * buffer,std::size_t bytes,IoErrorHandler & handler)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
Truncate(FileOffset,IoErrorHandler & handler)141 void PseudoOpenFile::Truncate(FileOffset, IoErrorHandler &handler) {
142 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
143 }
144
ReadAsynchronously(FileOffset,char *,std::size_t,IoErrorHandler & handler)145 int PseudoOpenFile::ReadAsynchronously(
146 FileOffset, char *, std::size_t, IoErrorHandler &handler) {
147 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
148 }
149
WriteAsynchronously(FileOffset,const char *,std::size_t,IoErrorHandler & handler)150 int PseudoOpenFile::WriteAsynchronously(
151 FileOffset, const char *, std::size_t, IoErrorHandler &handler) {
152 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
153 }
154
Wait(int,IoErrorHandler & handler)155 void PseudoOpenFile::Wait(int, IoErrorHandler &handler) {
156 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
157 }
158
WaitAll(IoErrorHandler & handler)159 void PseudoOpenFile::WaitAll(IoErrorHandler &handler) {
160 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
161 }
162
InquirePosition() const163 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)
170