llvm-project
156 строк · 6.5 Кб
1//===-- runtime/descriptor-io.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#include "descriptor-io.h"
10#include "flang/Common/restorer.h"
11#include "flang/Runtime/freestanding-tools.h"
12
13namespace Fortran::runtime::io::descr {
14RT_OFFLOAD_API_GROUP_BEGIN
15
16// Defined formatted I/O (maybe)
17Fortran::common::optional<bool> DefinedFormattedIo(IoStatementState &io,
18const Descriptor &descriptor, const typeInfo::DerivedType &derived,
19const typeInfo::SpecialBinding &special,
20const SubscriptValue subscripts[]) {
21Fortran::common::optional<DataEdit> peek{
22io.GetNextDataEdit(0 /*to peek at it*/)};
23if (peek &&
24(peek->descriptor == DataEdit::DefinedDerivedType ||
25peek->descriptor == DataEdit::ListDirected)) {
26// Defined formatting
27IoErrorHandler &handler{io.GetIoErrorHandler()};
28DataEdit edit{*io.GetNextDataEdit(1)}; // now consume it; no repeats
29RUNTIME_CHECK(handler, edit.descriptor == peek->descriptor);
30char ioType[2 + edit.maxIoTypeChars];
31auto ioTypeLen{std::size_t{2} /*"DT"*/ + edit.ioTypeChars};
32if (edit.descriptor == DataEdit::DefinedDerivedType) {
33ioType[0] = 'D';
34ioType[1] = 'T';
35std::memcpy(ioType + 2, edit.ioType, edit.ioTypeChars);
36} else {
37runtime::strcpy(
38ioType, io.mutableModes().inNamelist ? "NAMELIST" : "LISTDIRECTED");
39ioTypeLen = runtime::strlen(ioType);
40}
41StaticDescriptor<1, true> vListStatDesc;
42Descriptor &vListDesc{vListStatDesc.descriptor()};
43vListDesc.Establish(TypeCategory::Integer, sizeof(int), nullptr, 1);
44vListDesc.set_base_addr(edit.vList);
45vListDesc.GetDimension(0).SetBounds(1, edit.vListEntries);
46vListDesc.GetDimension(0).SetByteStride(
47static_cast<SubscriptValue>(sizeof(int)));
48ExternalFileUnit *actualExternal{io.GetExternalFileUnit()};
49ExternalFileUnit *external{actualExternal};
50if (!external) {
51// Create a new unit to service defined I/O for an
52// internal I/O parent.
53external = &ExternalFileUnit::NewUnit(handler, true);
54}
55ChildIo &child{external->PushChildIo(io)};
56// Child formatted I/O is nonadvancing by definition (F'2018 12.6.2.4).
57auto restorer{common::ScopedSet(io.mutableModes().nonAdvancing, true)};
58int unit{external->unitNumber()};
59int ioStat{IostatOk};
60char ioMsg[100];
61Fortran::common::optional<std::int64_t> startPos;
62if (edit.descriptor == DataEdit::DefinedDerivedType &&
63special.which() == typeInfo::SpecialBinding::Which::ReadFormatted) {
64// DT is an edit descriptor so everything that the child
65// I/O subroutine reads counts towards READ(SIZE=).
66startPos = io.InquirePos();
67}
68if (special.IsArgDescriptor(0)) {
69// "dtv" argument is "class(t)", pass a descriptor
70auto *p{special.GetProc<void (*)(const Descriptor &, int &, char *,
71const Descriptor &, int &, char *, std::size_t, std::size_t)>()};
72StaticDescriptor<1, true, 10 /*?*/> elementStatDesc;
73Descriptor &elementDesc{elementStatDesc.descriptor()};
74elementDesc.Establish(
75derived, nullptr, 0, nullptr, CFI_attribute_pointer);
76elementDesc.set_base_addr(descriptor.Element<char>(subscripts));
77p(elementDesc, unit, ioType, vListDesc, ioStat, ioMsg, ioTypeLen,
78sizeof ioMsg);
79} else {
80// "dtv" argument is "type(t)", pass a raw pointer
81auto *p{special.GetProc<void (*)(const void *, int &, char *,
82const Descriptor &, int &, char *, std::size_t, std::size_t)>()};
83p(descriptor.Element<char>(subscripts), unit, ioType, vListDesc, ioStat,
84ioMsg, ioTypeLen, sizeof ioMsg);
85}
86handler.Forward(ioStat, ioMsg, sizeof ioMsg);
87external->PopChildIo(child);
88if (!actualExternal) {
89// Close unit created for internal I/O above.
90auto *closing{external->LookUpForClose(external->unitNumber())};
91RUNTIME_CHECK(handler, external == closing);
92external->DestroyClosed();
93}
94if (startPos) {
95io.GotChar(io.InquirePos() - *startPos);
96}
97return handler.GetIoStat() == IostatOk;
98} else {
99// There's a defined I/O subroutine, but there's a FORMAT present and
100// it does not have a DT data edit descriptor, so apply default formatting
101// to the components of the derived type as usual.
102return Fortran::common::nullopt;
103}
104}
105
106// Defined unformatted I/O
107bool DefinedUnformattedIo(IoStatementState &io, const Descriptor &descriptor,
108const typeInfo::DerivedType &derived,
109const typeInfo::SpecialBinding &special) {
110// Unformatted I/O must have an external unit (or child thereof).
111IoErrorHandler &handler{io.GetIoErrorHandler()};
112ExternalFileUnit *external{io.GetExternalFileUnit()};
113if (!external) { // INQUIRE(IOLENGTH=)
114handler.SignalError(IostatNonExternalDefinedUnformattedIo);
115return false;
116}
117ChildIo &child{external->PushChildIo(io)};
118int unit{external->unitNumber()};
119int ioStat{IostatOk};
120char ioMsg[100];
121std::size_t numElements{descriptor.Elements()};
122SubscriptValue subscripts[maxRank];
123descriptor.GetLowerBounds(subscripts);
124if (special.IsArgDescriptor(0)) {
125// "dtv" argument is "class(t)", pass a descriptor
126auto *p{special.GetProc<void (*)(
127const Descriptor &, int &, int &, char *, std::size_t)>()};
128StaticDescriptor<1, true, 10 /*?*/> elementStatDesc;
129Descriptor &elementDesc{elementStatDesc.descriptor()};
130elementDesc.Establish(derived, nullptr, 0, nullptr, CFI_attribute_pointer);
131for (; numElements-- > 0; descriptor.IncrementSubscripts(subscripts)) {
132elementDesc.set_base_addr(descriptor.Element<char>(subscripts));
133p(elementDesc, unit, ioStat, ioMsg, sizeof ioMsg);
134if (ioStat != IostatOk) {
135break;
136}
137}
138} else {
139// "dtv" argument is "type(t)", pass a raw pointer
140auto *p{special.GetProc<void (*)(
141const void *, int &, int &, char *, std::size_t)>()};
142for (; numElements-- > 0; descriptor.IncrementSubscripts(subscripts)) {
143p(descriptor.Element<char>(subscripts), unit, ioStat, ioMsg,
144sizeof ioMsg);
145if (ioStat != IostatOk) {
146break;
147}
148}
149}
150handler.Forward(ioStat, ioMsg, sizeof ioMsg);
151external->PopChildIo(child);
152return handler.GetIoStat() == IostatOk;
153}
154
155RT_OFFLOAD_API_GROUP_END
156} // namespace Fortran::runtime::io::descr
157