11#ifndef FORTRAN_RUNTIME_IO_API_H_
12#define FORTRAN_RUNTIME_IO_API_H_
14#include "flang/Common/uint128.h"
15#include "flang/Runtime/entry-names.h"
16#include "flang/Runtime/iostat-consts.h"
17#include "flang/Runtime/magic-numbers.h"
21namespace Fortran::runtime {
25namespace Fortran::runtime::io {
27struct NonTbpDefinedIoTable;
29class IoStatementState;
30using Cookie = IoStatementState *;
31using ExternalUnit = int;
32using AsynchronousId = int;
34static constexpr ExternalUnit DefaultOutputUnit{FORTRAN_DEFAULT_OUTPUT_UNIT};
35static constexpr ExternalUnit DefaultInputUnit{FORTRAN_DEFAULT_INPUT_UNIT};
39using InquiryKeywordHash = std::uint64_t;
40constexpr InquiryKeywordHash HashInquiryKeyword(
const char *p) {
41 InquiryKeywordHash hash{1};
42 while (
char ch{*p++}) {
43 std::uint64_t letter{0};
44 if (ch >=
'a' && ch <=
'z') {
49 hash = 26 * hash + letter;
56#define IONAME(name) RTNAME(io##name)
59#define IODECL(name) RT_API_ATTRS IONAME(name)
63#define IODEF(name) RT_API_ATTRS IONAME(name)
81constexpr std::size_t RecommendedInternalIoScratchAreaBytes(
82 int maxFormatParenthesesNestingDepth) {
83 return 32 + 8 * maxFormatParenthesesNestingDepth;
92Cookie IODECL(BeginInternalArrayListOutput)(
const Descriptor &,
93 void **scratchArea =
nullptr, std::size_t scratchBytes = 0,
94 const char *sourceFile =
nullptr,
int sourceLine = 0);
95Cookie IODECL(BeginInternalArrayListInput)(
const Descriptor &,
96 void **scratchArea =
nullptr, std::size_t scratchBytes = 0,
97 const char *sourceFile =
nullptr,
int sourceLine = 0);
98Cookie IODECL(BeginInternalArrayFormattedOutput)(
const Descriptor &,
99 const char *format, std::size_t formatLength,
100 const Descriptor *formatDescriptor =
nullptr,
void **scratchArea =
nullptr,
101 std::size_t scratchBytes = 0,
const char *sourceFile =
nullptr,
103Cookie IODECL(BeginInternalArrayFormattedInput)(
const Descriptor &,
104 const char *format, std::size_t formatLength,
105 const Descriptor *formatDescriptor =
nullptr,
void **scratchArea =
nullptr,
106 std::size_t scratchBytes = 0,
const char *sourceFile =
nullptr,
111Cookie IODECL(BeginInternalListOutput)(
char *internal,
112 std::size_t internalLength,
void **scratchArea =
nullptr,
113 std::size_t scratchBytes = 0,
const char *sourceFile =
nullptr,
115Cookie IODECL(BeginInternalListInput)(
const char *internal,
116 std::size_t internalLength,
void **scratchArea =
nullptr,
117 std::size_t scratchBytes = 0,
const char *sourceFile =
nullptr,
119Cookie IODECL(BeginInternalFormattedOutput)(
char *internal,
120 std::size_t internalLength,
const char *format, std::size_t formatLength,
121 const Descriptor *formatDescriptor =
nullptr,
void **scratchArea =
nullptr,
122 std::size_t scratchBytes = 0,
const char *sourceFile =
nullptr,
124Cookie IODECL(BeginInternalFormattedInput)(
const char *internal,
125 std::size_t internalLength,
const char *format, std::size_t formatLength,
126 const Descriptor *formatDescriptor =
nullptr,
void **scratchArea =
nullptr,
127 std::size_t scratchBytes = 0,
const char *sourceFile =
nullptr,
139enum Iostat IODECL(CheckUnitNumberInRange64)(std::int64_t unit,
140 bool handleError,
char *ioMsg =
nullptr, std::size_t ioMsgLength = 0,
141 const char *sourceFile =
nullptr,
int sourceLine = 0);
142enum Iostat IODECL(CheckUnitNumberInRange128)(common::int128_t unit,
143 bool handleError,
char *ioMsg =
nullptr, std::size_t ioMsgLength = 0,
144 const char *sourceFile =
nullptr,
int sourceLine = 0);
147Cookie IODECL(BeginExternalListOutput)(ExternalUnit = DefaultOutputUnit,
148 const char *sourceFile =
nullptr,
int sourceLine = 0);
149Cookie IODECL(BeginExternalListInput)(ExternalUnit = DefaultInputUnit,
150 const char *sourceFile =
nullptr,
int sourceLine = 0);
151Cookie IODECL(BeginExternalFormattedOutput)(
const char *format, std::size_t,
152 const Descriptor *formatDescriptor =
nullptr,
153 ExternalUnit = DefaultOutputUnit,
const char *sourceFile =
nullptr,
155Cookie IODECL(BeginExternalFormattedInput)(
const char *format, std::size_t,
156 const Descriptor *formatDescriptor =
nullptr,
157 ExternalUnit = DefaultInputUnit,
const char *sourceFile =
nullptr,
159Cookie IODECL(BeginUnformattedOutput)(ExternalUnit = DefaultOutputUnit,
160 const char *sourceFile =
nullptr,
int sourceLine = 0);
161Cookie IODECL(BeginUnformattedInput)(ExternalUnit = DefaultInputUnit,
162 const char *sourceFile =
nullptr,
int sourceLine = 0);
165Cookie IODECL(BeginWait)(ExternalUnit, AsynchronousId,
166 const char *sourceFile =
nullptr,
int sourceLine = 0);
168Cookie IODECL(BeginWaitAll)(
169 ExternalUnit,
const char *sourceFile =
nullptr,
int sourceLine = 0);
172Cookie IODECL(BeginClose)(
173 ExternalUnit,
const char *sourceFile =
nullptr,
int sourceLine = 0);
174Cookie IODECL(BeginFlush)(
175 ExternalUnit,
const char *sourceFile =
nullptr,
int sourceLine = 0);
176Cookie IODECL(BeginBackspace)(
177 ExternalUnit,
const char *sourceFile =
nullptr,
int sourceLine = 0);
178Cookie IODECL(BeginEndfile)(
179 ExternalUnit,
const char *sourceFile =
nullptr,
int sourceLine = 0);
180Cookie IODECL(BeginRewind)(
181 ExternalUnit,
const char *sourceFile =
nullptr,
int sourceLine = 0);
184Cookie IODECL(BeginOpenUnit)(
185 ExternalUnit,
const char *sourceFile =
nullptr,
int sourceLine = 0);
186Cookie IODECL(BeginOpenNewUnit)(
187 const char *sourceFile =
nullptr,
int sourceLine = 0);
191Cookie IODECL(BeginInquireUnit)(
192 ExternalUnit,
const char *sourceFile =
nullptr,
int sourceLine = 0);
193Cookie IODECL(BeginInquireFile)(
const char *, std::size_t,
194 const char *sourceFile =
nullptr,
int sourceLine = 0);
195Cookie IODECL(BeginInquireIoLength)(
196 const char *sourceFile =
nullptr,
int sourceLine = 0);
214void IODECL(EnableHandlers)(Cookie,
bool hasIoStat =
false,
bool hasErr =
false,
215 bool hasEnd =
false,
bool hasEor =
false,
bool hasIoMsg =
false);
219bool IODECL(SetAsynchronous)(Cookie,
const char *, std::size_t);
226bool IODECL(SetAdvance)(Cookie,
const char *, std::size_t);
228bool IODECL(SetBlank)(Cookie,
const char *, std::size_t);
230bool IODECL(SetDecimal)(Cookie,
const char *, std::size_t);
232bool IODECL(SetDelim)(Cookie,
const char *, std::size_t);
234bool IODECL(SetPad)(Cookie,
const char *, std::size_t);
235bool IODECL(SetPos)(Cookie, std::int64_t);
236bool IODECL(SetRec)(Cookie, std::int64_t);
238bool IODECL(SetRound)(Cookie,
const char *, std::size_t);
240bool IODECL(SetSign)(Cookie,
const char *, std::size_t);
256bool IODECL(OutputDescriptor)(Cookie,
const Descriptor &);
257bool IODECL(InputDescriptor)(Cookie,
const Descriptor &);
259bool IODECL(OutputInteger8)(Cookie, std::int8_t);
260bool IODECL(OutputInteger16)(Cookie, std::int16_t);
261bool IODECL(OutputInteger32)(Cookie, std::int32_t);
262bool IODECL(OutputInteger64)(Cookie, std::int64_t);
263bool IODECL(OutputInteger128)(Cookie, common::int128_t);
264bool IODECL(InputInteger)(Cookie, std::int64_t &,
int kind = 8);
265bool IODECL(OutputReal32)(Cookie, float);
266bool IODECL(InputReal32)(Cookie,
float &);
267bool IODECL(OutputReal64)(Cookie, double);
268bool IODECL(InputReal64)(Cookie,
double &);
269bool IODECL(OutputComplex32)(Cookie, float, float);
270bool IODECL(InputComplex32)(Cookie,
float[2]);
271bool IODECL(OutputComplex64)(Cookie, double, double);
272bool IODECL(InputComplex64)(Cookie,
double[2]);
273bool IODECL(OutputCharacter)(Cookie,
const char *, std::size_t,
int kind = 1);
274bool IODECL(OutputAscii)(Cookie,
const char *, std::size_t);
275bool IODECL(InputCharacter)(Cookie,
char *, std::size_t,
int kind = 1);
276bool IODECL(InputAscii)(Cookie,
char *, std::size_t);
277bool IODECL(OutputLogical)(Cookie, bool);
278bool IODECL(InputLogical)(Cookie,
bool &);
282bool IODECL(OutputNamelist)(Cookie,
const NamelistGroup &);
283bool IODECL(InputNamelist)(Cookie,
const NamelistGroup &);
294bool IODECL(OutputDerivedType)(
295 Cookie,
const Descriptor &,
const NonTbpDefinedIoTable *);
296bool IODECL(InputDerivedType)(
297 Cookie,
const Descriptor &,
const NonTbpDefinedIoTable *);
304bool IODECL(SetAccess)(Cookie,
const char *, std::size_t);
306bool IODECL(SetAction)(Cookie,
const char *, std::size_t);
308bool IODECL(SetCarriagecontrol)(Cookie,
const char *, std::size_t);
310bool IODECL(SetConvert)(Cookie,
const char *, std::size_t);
312bool IODECL(SetEncoding)(Cookie,
const char *, std::size_t);
314bool IODECL(SetForm)(Cookie,
const char *, std::size_t);
316bool IODECL(SetPosition)(Cookie,
const char *, std::size_t);
317bool IODECL(SetRecl)(Cookie, std::size_t);
322bool IODECL(SetStatus)(Cookie,
const char *, std::size_t);
324bool IODECL(SetFile)(Cookie,
const char *, std::size_t chars);
327bool IODECL(GetNewUnit)(Cookie,
int &,
int kind = 4);
330std::size_t IODECL(GetSize)(Cookie);
333std::size_t IODECL(GetIoLength)(Cookie);
337void IODECL(GetIoMsg)(Cookie,
char *, std::size_t);
340AsynchronousId IODECL(GetAsynchronousId)(Cookie);
347bool IODECL(InquireCharacter)(Cookie, InquiryKeywordHash,
char *, std::size_t);
349bool IODECL(InquireLogical)(Cookie, InquiryKeywordHash,
bool &);
351bool IODECL(InquirePendingId)(Cookie, AsynchronousId,
bool &);
353bool IODECL(InquireInteger64)(
354 Cookie, InquiryKeywordHash, std::int64_t &,
int kind = 8);
365enum Iostat IODECL(EndIoStatement)(Cookie);