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