1 //===-- runtime/internal-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 #include "internal-unit.h"
10 #include "io-error.h"
11 #include "flang/Runtime/descriptor.h"
12 #include <algorithm>
13 #include <type_traits>
14 
15 namespace Fortran::runtime::io {
16 
17 template <Direction DIR>
18 InternalDescriptorUnit<DIR>::InternalDescriptorUnit(
19     Scalar scalar, std::size_t length) {
20   isFixedRecordLength = true;
21   recordLength = length;
22   endfileRecordNumber = 2;
23   void *pointer{reinterpret_cast<void *>(const_cast<char *>(scalar))};
24   descriptor().Establish(TypeCode{CFI_type_char}, length, pointer, 0, nullptr,
25       CFI_attribute_pointer);
26 }
27 
28 template <Direction DIR>
29 InternalDescriptorUnit<DIR>::InternalDescriptorUnit(
30     const Descriptor &that, const Terminator &terminator) {
31   RUNTIME_CHECK(terminator, that.type().IsCharacter());
32   Descriptor &d{descriptor()};
33   RUNTIME_CHECK(
34       terminator, that.SizeInBytes() <= d.SizeInBytes(maxRank, true, 0));
35   new (&d) Descriptor{that};
36   d.Check();
37   isFixedRecordLength = true;
38   recordLength = d.ElementBytes();
39   endfileRecordNumber = d.Elements() + 1;
40 }
41 
42 template <Direction DIR> void InternalDescriptorUnit<DIR>::EndIoStatement() {
43   if constexpr (DIR == Direction::Output) { // blank fill
44     while (char *record{CurrentRecord()}) {
45       if (furthestPositionInRecord <
46           recordLength.value_or(furthestPositionInRecord)) {
47         std::fill_n(record + furthestPositionInRecord,
48             *recordLength - furthestPositionInRecord, ' ');
49       }
50       furthestPositionInRecord = 0;
51       ++currentRecordNumber;
52     }
53   }
54 }
55 
56 template <Direction DIR>
57 bool InternalDescriptorUnit<DIR>::Emit(
58     const char *data, std::size_t bytes, IoErrorHandler &handler) {
59   if constexpr (DIR == Direction::Input) {
60     handler.Crash("InternalDescriptorUnit<Direction::Input>::Emit() called");
61     return false && data[bytes] != 0; // bogus compare silences GCC warning
62   } else {
63     if (bytes <= 0) {
64       return true;
65     }
66     char *record{CurrentRecord()};
67     if (!record) {
68       handler.SignalError(IostatInternalWriteOverrun);
69       return false;
70     }
71     auto furthestAfter{std::max(furthestPositionInRecord,
72         positionInRecord + static_cast<std::int64_t>(bytes))};
73     bool ok{true};
74     if (furthestAfter > static_cast<std::int64_t>(recordLength.value_or(0))) {
75       handler.SignalError(IostatRecordWriteOverrun);
76       furthestAfter = recordLength.value_or(0);
77       bytes = std::max(std::int64_t{0}, furthestAfter - positionInRecord);
78       ok = false;
79     } else if (positionInRecord > furthestPositionInRecord) {
80       std::fill_n(record + furthestPositionInRecord,
81           positionInRecord - furthestPositionInRecord, ' ');
82     }
83     std::memcpy(record + positionInRecord, data, bytes);
84     positionInRecord += bytes;
85     furthestPositionInRecord = furthestAfter;
86     return ok;
87   }
88 }
89 
90 template <Direction DIR>
91 std::size_t InternalDescriptorUnit<DIR>::GetNextInputBytes(
92     const char *&p, IoErrorHandler &handler) {
93   if constexpr (DIR == Direction::Output) {
94     handler.Crash("InternalDescriptorUnit<Direction::Output>::"
95                   "GetNextInputBytes() called");
96     return 0;
97   } else {
98     const char *record{CurrentRecord()};
99     if (!record) {
100       handler.SignalEnd();
101       return 0;
102     } else if (positionInRecord >= recordLength.value_or(positionInRecord)) {
103       return 0;
104     } else {
105       p = &record[positionInRecord];
106       return *recordLength - positionInRecord;
107     }
108   }
109 }
110 
111 template <Direction DIR>
112 std::optional<char32_t> InternalDescriptorUnit<DIR>::GetCurrentChar(
113     IoErrorHandler &handler) {
114   const char *p{nullptr};
115   std::size_t bytes{GetNextInputBytes(p, handler)};
116   if (bytes == 0) {
117     return std::nullopt;
118   } else {
119     if (isUTF8) {
120       // TODO: UTF-8 decoding
121     }
122     return *p;
123   }
124 }
125 
126 template <Direction DIR>
127 bool InternalDescriptorUnit<DIR>::AdvanceRecord(IoErrorHandler &handler) {
128   if (currentRecordNumber >= endfileRecordNumber.value_or(0)) {
129     handler.SignalEnd();
130     return false;
131   }
132   if constexpr (DIR == Direction::Output) { // blank fill
133     if (furthestPositionInRecord <
134         recordLength.value_or(furthestPositionInRecord)) {
135       char *record{CurrentRecord()};
136       RUNTIME_CHECK(handler, record != nullptr);
137       std::fill_n(record + furthestPositionInRecord,
138           *recordLength - furthestPositionInRecord, ' ');
139     }
140   }
141   ++currentRecordNumber;
142   BeginRecord();
143   return true;
144 }
145 
146 template <Direction DIR>
147 void InternalDescriptorUnit<DIR>::BackspaceRecord(IoErrorHandler &handler) {
148   RUNTIME_CHECK(handler, currentRecordNumber > 1);
149   --currentRecordNumber;
150   BeginRecord();
151 }
152 
153 template class InternalDescriptorUnit<Direction::Output>;
154 template class InternalDescriptorUnit<Direction::Input>;
155 } // namespace Fortran::runtime::io
156