peter klausler | 95696d5 | 2020-02-05 00:55:45 | [diff] [blame] | 1 | //===-- runtime/internal-unit.h ---------------------------------*- C++ -*-===// |
| 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 | // Fortran internal I/O "units" |
| 10 | |
| 11 | #ifndef FORTRAN_RUNTIME_IO_INTERNAL_UNIT_H_ |
| 12 | #define FORTRAN_RUNTIME_IO_INTERNAL_UNIT_H_ |
| 13 | |
| 14 | #include "connection.h" |
Peter Klausler | 830c0b9 | 2021-09-01 23:00:53 | [diff] [blame] | 15 | #include "flang/Runtime/descriptor.h" |
peter klausler | 95696d5 | 2020-02-05 00:55:45 | [diff] [blame] | 16 | #include <cinttypes> |
| 17 | #include <type_traits> |
| 18 | |
| 19 | namespace Fortran::runtime::io { |
| 20 | |
| 21 | class IoErrorHandler; |
| 22 | |
| 23 | // Points to (but does not own) a CHARACTER scalar or array for internal I/O. |
| 24 | // Does not buffer. |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 25 | template <Direction DIR> class InternalDescriptorUnit : public ConnectionState { |
peter klausler | 95696d5 | 2020-02-05 00:55:45 | [diff] [blame] | 26 | public: |
peter klausler | 3b63571 | 2020-02-13 22:41:56 | [diff] [blame] | 27 | using Scalar = |
| 28 | std::conditional_t<DIR == Direction::Input, const char *, char *>; |
peter klausler | 95696d5 | 2020-02-05 00:55:45 | [diff] [blame] | 29 | InternalDescriptorUnit(Scalar, std::size_t); |
| 30 | InternalDescriptorUnit(const Descriptor &, const Terminator &); |
| 31 | void EndIoStatement(); |
| 32 | |
peter klausler | 3b63571 | 2020-02-13 22:41:56 | [diff] [blame] | 33 | bool Emit(const char *, std::size_t, IoErrorHandler &); |
Peter Klausler | da25f96 | 2021-11-02 20:01:38 | [diff] [blame] | 34 | std::size_t GetNextInputBytes(const char *&, IoErrorHandler &); |
peter klausler | 3b63571 | 2020-02-13 22:41:56 | [diff] [blame] | 35 | std::optional<char32_t> GetCurrentChar(IoErrorHandler &); |
peter klausler | 95696d5 | 2020-02-05 00:55:45 | [diff] [blame] | 36 | bool AdvanceRecord(IoErrorHandler &); |
peter klausler | 3b63571 | 2020-02-13 22:41:56 | [diff] [blame] | 37 | void BackspaceRecord(IoErrorHandler &); |
peter klausler | 95696d5 | 2020-02-05 00:55:45 | [diff] [blame] | 38 | |
| 39 | private: |
| 40 | Descriptor &descriptor() { return staticDescriptor_.descriptor(); } |
peter klausler | 3b63571 | 2020-02-13 22:41:56 | [diff] [blame] | 41 | const Descriptor &descriptor() const { |
| 42 | return staticDescriptor_.descriptor(); |
| 43 | } |
| 44 | Scalar CurrentRecord() const { |
| 45 | return descriptor().template ZeroBasedIndexedElement<char>( |
| 46 | currentRecordNumber - 1); |
| 47 | } |
Peter Klausler | b77fd01 | 2022-01-05 19:42:18 | [diff] [blame] | 48 | void BlankFillOutputRecord(); |
| 49 | |
peter klausler | 95696d5 | 2020-02-05 00:55:45 | [diff] [blame] | 50 | StaticDescriptor<maxRank, true /*addendum*/> staticDescriptor_; |
peter klausler | 95696d5 | 2020-02-05 00:55:45 | [diff] [blame] | 51 | }; |
| 52 | |
peter klausler | 3b63571 | 2020-02-13 22:41:56 | [diff] [blame] | 53 | extern template class InternalDescriptorUnit<Direction::Output>; |
| 54 | extern template class InternalDescriptorUnit<Direction::Input>; |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 55 | } // namespace Fortran::runtime::io |
| 56 | #endif // FORTRAN_RUNTIME_IO_INTERNAL_UNIT_H_ |