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
24namespace Fortran::runtime::io {
25
26void FlushOutputOnCrash(const Terminator &) {}
27
28ExternalFileUnit *ExternalFileUnit::LookUp(int) {
29 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
30}
31
32ExternalFileUnit *ExternalFileUnit::LookUpOrCreate(
33 int, const Terminator &, bool &) {
34 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
35}
36
37ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
38 Direction direction, Fortran::common::optional<bool>,
39 const Terminator &terminator) {
40 if (direction != Direction::Output) {
41 terminator.Crash("ExternalFileUnit only supports output IO");
42 }
43 return New<ExternalFileUnit>{terminator}(unit).release();
44}
45
46ExternalFileUnit *ExternalFileUnit::LookUp(const char *, std::size_t) {
47 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
48}
49
50ExternalFileUnit &ExternalFileUnit::CreateNew(int, const Terminator &) {
51 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
52}
53
54ExternalFileUnit *ExternalFileUnit::LookUpForClose(int) {
55 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
56}
57
58ExternalFileUnit &ExternalFileUnit::NewUnit(const Terminator &, bool) {
59 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
60}
61
62bool 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
68void 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
74void ExternalFileUnit::CloseUnit(CloseStatus, IoErrorHandler &handler) {
75 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
76}
77
78void ExternalFileUnit::DestroyClosed() {
79 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
80}
81
82Iostat ExternalFileUnit::SetDirection(Direction direction) {
83 if (direction != Direction::Output) {
84 return IostatReadFromWriteOnly;
85 }
86 direction_ = direction;
87 return IostatOk;
88}
89
90void ExternalFileUnit::CloseAll(IoErrorHandler &) {}
91
92void ExternalFileUnit::FlushAll(IoErrorHandler &) {}
93
94int ExternalFileUnit::GetAsynchronousId(IoErrorHandler &handler) {
95 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
96}
97
98bool ExternalFileUnit::Wait(int) {
99 Terminator{__FILE__, __LINE__}.Crash("unsupported");
100}
101
102void PseudoOpenFile::set_mayAsynchronous(bool yes) {
103 if (yes) {
104 Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
105 }
106}
107
108Fortran::common::optional<PseudoOpenFile::FileOffset>
109PseudoOpenFile::knownSize() const {
110 Terminator{__FILE__, __LINE__}.Crash("unsupported");
111}
112
113void PseudoOpenFile::Open(OpenStatus, Fortran::common::optional<Action>,
114 Position, IoErrorHandler &handler) {
115 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
116}
117
118void PseudoOpenFile::Close(CloseStatus, IoErrorHandler &handler) {
119 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
120}
121
122std::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
127std::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
141void PseudoOpenFile::Truncate(FileOffset, IoErrorHandler &handler) {
142 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
143}
144
145int PseudoOpenFile::ReadAsynchronously(
146 FileOffset, char *, std::size_t, IoErrorHandler &handler) {
147 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
148}
149
150int PseudoOpenFile::WriteAsynchronously(
151 FileOffset, const char *, std::size_t, IoErrorHandler &handler) {
152 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
153}
154
155void PseudoOpenFile::Wait(int, IoErrorHandler &handler) {
156 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
157}
158
159void PseudoOpenFile::WaitAll(IoErrorHandler &handler) {
160 handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
161}
162
163Position 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

source code of flang/runtime/pseudo-unit.cpp