2021-09-02 16:14:01 +08:00
|
|
|
//===-- runtime/edit-input.cpp --------------------------------------------===//
|
2020-02-14 06:41:56 +08:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "edit-input.h"
|
2021-10-21 04:56:47 +08:00
|
|
|
#include "namelist.h"
|
2020-02-14 06:41:56 +08:00
|
|
|
#include "flang/Common/real.h"
|
|
|
|
#include "flang/Common/uint128.h"
|
2020-03-13 01:52:29 +08:00
|
|
|
#include <algorithm>
|
2020-02-14 06:41:56 +08:00
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
|
|
|
static bool EditBOZInput(IoStatementState &io, const DataEdit &edit, void *n,
|
|
|
|
int base, int totalBitSize) {
|
|
|
|
std::optional<int> remaining;
|
2021-05-06 02:37:49 +08:00
|
|
|
std::optional<char32_t> next{io.PrepareInput(edit, remaining)};
|
2020-02-14 06:41:56 +08:00
|
|
|
common::UnsignedInt128 value{0};
|
|
|
|
for (; next; next = io.NextInField(remaining)) {
|
|
|
|
char32_t ch{*next};
|
2020-07-18 07:40:49 +08:00
|
|
|
if (ch == ' ' || ch == '\t') {
|
2020-02-14 06:41:56 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int digit{0};
|
|
|
|
if (ch >= '0' && ch <= '1') {
|
|
|
|
digit = ch - '0';
|
|
|
|
} else if (base >= 8 && ch >= '2' && ch <= '7') {
|
|
|
|
digit = ch - '0';
|
|
|
|
} else if (base >= 10 && ch >= '8' && ch <= '9') {
|
|
|
|
digit = ch - '0';
|
|
|
|
} else if (base == 16 && ch >= 'A' && ch <= 'Z') {
|
|
|
|
digit = ch + 10 - 'A';
|
|
|
|
} else if (base == 16 && ch >= 'a' && ch <= 'z') {
|
|
|
|
digit = ch + 10 - 'a';
|
|
|
|
} else {
|
|
|
|
io.GetIoErrorHandler().SignalError(
|
|
|
|
"Bad character '%lc' in B/O/Z input field", ch);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value *= base;
|
|
|
|
value += digit;
|
|
|
|
}
|
|
|
|
// TODO: check for overflow
|
|
|
|
std::memcpy(n, &value, totalBitSize >> 3);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-04 02:20:35 +08:00
|
|
|
// Prepares input from a field, and consumes the sign, if any.
|
|
|
|
// Returns true if there's a '-' sign.
|
2020-02-14 06:41:56 +08:00
|
|
|
static bool ScanNumericPrefix(IoStatementState &io, const DataEdit &edit,
|
|
|
|
std::optional<char32_t> &next, std::optional<int> &remaining) {
|
2021-05-06 02:37:49 +08:00
|
|
|
next = io.PrepareInput(edit, remaining);
|
2020-02-14 06:41:56 +08:00
|
|
|
bool negative{false};
|
|
|
|
if (next) {
|
|
|
|
negative = *next == '-';
|
|
|
|
if (negative || *next == '+') {
|
2021-09-21 01:52:39 +08:00
|
|
|
io.GotChar();
|
2020-08-04 02:20:35 +08:00
|
|
|
io.SkipSpaces(remaining);
|
2020-02-14 06:41:56 +08:00
|
|
|
next = io.NextInField(remaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return negative;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditIntegerInput(
|
|
|
|
IoStatementState &io, const DataEdit &edit, void *n, int kind) {
|
|
|
|
RUNTIME_CHECK(io.GetIoErrorHandler(), kind >= 1 && !(kind & (kind - 1)));
|
|
|
|
switch (edit.descriptor) {
|
|
|
|
case DataEdit::ListDirected:
|
2021-10-21 04:56:47 +08:00
|
|
|
if (IsNamelistName(io)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2020-02-14 06:41:56 +08:00
|
|
|
case 'G':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'I':
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
return EditBOZInput(io, edit, n, 2, kind << 3);
|
|
|
|
case 'O':
|
|
|
|
return EditBOZInput(io, edit, n, 8, kind << 3);
|
|
|
|
case 'Z':
|
|
|
|
return EditBOZInput(io, edit, n, 16, kind << 3);
|
2021-10-22 04:33:07 +08:00
|
|
|
case 'A': // legacy extension
|
|
|
|
return EditDefaultCharacterInput(
|
|
|
|
io, edit, reinterpret_cast<char *>(n), kind);
|
2020-02-14 06:41:56 +08:00
|
|
|
default:
|
|
|
|
io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
|
|
|
|
"Data edit descriptor '%c' may not be used with an INTEGER data item",
|
|
|
|
edit.descriptor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::optional<int> remaining;
|
|
|
|
std::optional<char32_t> next;
|
|
|
|
bool negate{ScanNumericPrefix(io, edit, next, remaining)};
|
|
|
|
common::UnsignedInt128 value;
|
2022-01-08 02:29:23 +08:00
|
|
|
bool any{false};
|
2020-02-14 06:41:56 +08:00
|
|
|
for (; next; next = io.NextInField(remaining)) {
|
|
|
|
char32_t ch{*next};
|
2020-07-18 07:40:49 +08:00
|
|
|
if (ch == ' ' || ch == '\t') {
|
2020-02-14 06:41:56 +08:00
|
|
|
if (edit.modes.editingFlags & blankZero) {
|
2020-03-29 12:00:16 +08:00
|
|
|
ch = '0'; // BZ mode - treat blank as if it were zero
|
2020-02-14 06:41:56 +08:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int digit{0};
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
digit = ch - '0';
|
|
|
|
} else {
|
|
|
|
io.GetIoErrorHandler().SignalError(
|
|
|
|
"Bad character '%lc' in INTEGER input field", ch);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value *= 10;
|
|
|
|
value += digit;
|
2022-01-08 02:29:23 +08:00
|
|
|
any = true;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2022-01-08 02:29:23 +08:00
|
|
|
if (any) {
|
|
|
|
if (negate) {
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
std::memcpy(n, &value, kind);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2022-01-08 02:29:23 +08:00
|
|
|
return any;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
|
2020-08-04 02:20:35 +08:00
|
|
|
// Parses a REAL input number from the input source as a normalized
|
|
|
|
// fraction into a supplied buffer -- there's an optional '-', a
|
|
|
|
// decimal point, and at least one digit. The adjusted exponent value
|
|
|
|
// is returned in a reference argument. The returned value is the number
|
|
|
|
// of characters that (should) have been written to the buffer -- this can
|
|
|
|
// be larger than the buffer size and can indicate overflow. Replaces
|
|
|
|
// blanks with zeroes if appropriate.
|
2020-02-14 06:41:56 +08:00
|
|
|
static int ScanRealInput(char *buffer, int bufferSize, IoStatementState &io,
|
|
|
|
const DataEdit &edit, int &exponent) {
|
|
|
|
std::optional<int> remaining;
|
|
|
|
std::optional<char32_t> next;
|
|
|
|
int got{0};
|
|
|
|
std::optional<int> decimalPoint;
|
2020-08-04 02:20:35 +08:00
|
|
|
auto Put{[&](char ch) -> void {
|
2020-02-14 06:41:56 +08:00
|
|
|
if (got < bufferSize) {
|
2020-08-04 02:20:35 +08:00
|
|
|
buffer[got] = ch;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2020-08-04 02:20:35 +08:00
|
|
|
++got;
|
|
|
|
}};
|
|
|
|
if (ScanNumericPrefix(io, edit, next, remaining)) {
|
|
|
|
Put('-');
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2021-09-30 01:19:21 +08:00
|
|
|
if (next.value_or(' ') == ' ') { // empty/blank field means zero
|
|
|
|
remaining.reset();
|
2020-08-04 02:20:35 +08:00
|
|
|
Put('0');
|
2020-02-14 06:41:56 +08:00
|
|
|
return got;
|
|
|
|
}
|
|
|
|
char32_t decimal = edit.modes.editingFlags & decimalComma ? ',' : '.';
|
2020-08-04 02:20:35 +08:00
|
|
|
char32_t first{*next >= 'a' && *next <= 'z' ? *next + 'A' - 'a' : *next};
|
|
|
|
if (first == 'N' || first == 'I') {
|
2020-02-14 06:41:56 +08:00
|
|
|
// NaN or infinity - convert to upper case
|
2020-08-04 02:20:35 +08:00
|
|
|
// Subtle: a blank field of digits could be followed by 'E' or 'D',
|
2020-02-14 06:41:56 +08:00
|
|
|
for (; next &&
|
|
|
|
((*next >= 'a' && *next <= 'z') || (*next >= 'A' && *next <= 'Z'));
|
|
|
|
next = io.NextInField(remaining)) {
|
2020-08-04 02:20:35 +08:00
|
|
|
if (*next >= 'a' && *next <= 'z') {
|
|
|
|
Put(*next - 'a' + 'A');
|
|
|
|
} else {
|
|
|
|
Put(*next);
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
if (next && *next == '(') { // NaN(...)
|
2020-02-14 06:41:56 +08:00
|
|
|
while (next && *next != ')') {
|
|
|
|
next = io.NextInField(remaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exponent = 0;
|
2020-08-04 02:20:35 +08:00
|
|
|
} else if (first == decimal || (first >= '0' && first <= '9') ||
|
|
|
|
first == 'E' || first == 'D' || first == 'Q') {
|
|
|
|
Put('.'); // input field is normalized to a fraction
|
|
|
|
auto start{got};
|
2020-09-15 07:11:45 +08:00
|
|
|
bool bzMode{(edit.modes.editingFlags & blankZero) != 0};
|
2020-02-14 06:41:56 +08:00
|
|
|
for (; next; next = io.NextInField(remaining)) {
|
|
|
|
char32_t ch{*next};
|
2020-07-18 07:40:49 +08:00
|
|
|
if (ch == ' ' || ch == '\t') {
|
2020-09-15 07:11:45 +08:00
|
|
|
if (bzMode) {
|
2020-03-29 12:00:16 +08:00
|
|
|
ch = '0'; // BZ mode - treat blank as if it were zero
|
2020-02-14 06:41:56 +08:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 08:21:08 +08:00
|
|
|
if (ch == '0' && got == start && !decimalPoint) {
|
|
|
|
// omit leading zeroes before the decimal
|
2020-02-14 06:41:56 +08:00
|
|
|
} else if (ch >= '0' && ch <= '9') {
|
2020-08-04 02:20:35 +08:00
|
|
|
Put(ch);
|
2020-02-14 06:41:56 +08:00
|
|
|
} else if (ch == decimal && !decimalPoint) {
|
|
|
|
// the decimal point is *not* copied to the buffer
|
2020-03-29 12:00:16 +08:00
|
|
|
decimalPoint = got - start; // # of digits before the decimal point
|
2020-02-14 06:41:56 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 02:20:35 +08:00
|
|
|
if (got == start) {
|
|
|
|
Put('0'); // emit at least one digit
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
if (next &&
|
|
|
|
(*next == 'e' || *next == 'E' || *next == 'd' || *next == 'D' ||
|
|
|
|
*next == 'q' || *next == 'Q')) {
|
2020-09-15 07:11:45 +08:00
|
|
|
// Optional exponent letter. Blanks are allowed between the
|
|
|
|
// optional exponent letter and the exponent value.
|
2020-02-14 06:41:56 +08:00
|
|
|
io.SkipSpaces(remaining);
|
|
|
|
next = io.NextInField(remaining);
|
|
|
|
}
|
2020-09-15 07:11:45 +08:00
|
|
|
// The default exponent is -kP, but the scale factor doesn't affect
|
|
|
|
// an explicit exponent.
|
|
|
|
exponent = -edit.modes.scale;
|
2020-02-14 06:41:56 +08:00
|
|
|
if (next &&
|
2020-09-15 07:11:45 +08:00
|
|
|
(*next == '-' || *next == '+' || (*next >= '0' && *next <= '9') ||
|
|
|
|
(bzMode && (*next == ' ' || *next == '\t')))) {
|
2020-02-14 06:41:56 +08:00
|
|
|
bool negExpo{*next == '-'};
|
|
|
|
if (negExpo || *next == '+') {
|
|
|
|
next = io.NextInField(remaining);
|
|
|
|
}
|
2020-09-15 07:11:45 +08:00
|
|
|
for (exponent = 0; next; next = io.NextInField(remaining)) {
|
|
|
|
if (*next >= '0' && *next <= '9') {
|
|
|
|
exponent = 10 * exponent + *next - '0';
|
|
|
|
} else if (bzMode && (*next == ' ' || *next == '\t')) {
|
|
|
|
exponent = 10 * exponent;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
if (negExpo) {
|
|
|
|
exponent = -exponent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (decimalPoint) {
|
|
|
|
exponent += *decimalPoint;
|
|
|
|
} else {
|
|
|
|
// When no decimal point (or comma) appears in the value, the 'd'
|
|
|
|
// part of the edit descriptor must be interpreted as the number of
|
|
|
|
// digits in the value to be interpreted as being to the *right* of
|
|
|
|
// the assumed decimal point (13.7.2.3.2)
|
|
|
|
exponent += got - start - edit.digits.value_or(0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: hex FP input
|
|
|
|
exponent = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-06 02:37:49 +08:00
|
|
|
// Consume the trailing ')' of a list-directed or NAMELIST complex
|
|
|
|
// input value.
|
|
|
|
if (edit.descriptor == DataEdit::ListDirectedImaginaryPart) {
|
|
|
|
if (next && (*next == ' ' || *next == '\t')) {
|
|
|
|
next = io.NextInField(remaining);
|
|
|
|
}
|
|
|
|
if (!next) { // NextInField fails on separators like ')'
|
|
|
|
next = io.GetCurrentChar();
|
|
|
|
if (next && *next == ')') {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (remaining) {
|
2020-07-18 07:40:49 +08:00
|
|
|
while (next && (*next == ' ' || *next == '\t')) {
|
2020-02-14 06:41:56 +08:00
|
|
|
next = io.NextInField(remaining);
|
|
|
|
}
|
|
|
|
if (next) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return 0; // error: unused nonblank character in fixed-width field
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
2021-11-03 04:01:38 +08:00
|
|
|
// If no special modes are in effect and the form of the input value
|
|
|
|
// that's present in the input stream is acceptable to the decimal->binary
|
|
|
|
// converter without modification, this fast path for real input
|
|
|
|
// saves time by avoiding memory copies and reformatting of the exponent.
|
|
|
|
template <int PRECISION>
|
|
|
|
static bool TryFastPathRealInput(
|
|
|
|
IoStatementState &io, const DataEdit &edit, void *n) {
|
|
|
|
if (edit.modes.editingFlags & (blankZero | decimalComma)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (edit.modes.scale != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const char *str{nullptr};
|
|
|
|
std::size_t got{io.GetNextInputBytes(str)};
|
|
|
|
if (got == 0 || str == nullptr ||
|
|
|
|
!io.GetConnectionState().recordLength.has_value()) {
|
|
|
|
return false; // could not access reliably-terminated input stream
|
|
|
|
}
|
|
|
|
const char *p{str};
|
|
|
|
std::int64_t maxConsume{
|
|
|
|
std::min<std::int64_t>(got, edit.width.value_or(got))};
|
|
|
|
const char *limit{str + maxConsume};
|
|
|
|
decimal::ConversionToBinaryResult<PRECISION> converted{
|
|
|
|
decimal::ConvertToBinary<PRECISION>(p, edit.modes.round, limit)};
|
|
|
|
if (converted.flags & decimal::Invalid) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (edit.digits.value_or(0) != 0 &&
|
|
|
|
std::memchr(str, '.', p - str) == nullptr) {
|
|
|
|
// No explicit decimal point, and edit descriptor is Fw.d (or other)
|
|
|
|
// with d != 0, which implies scaling.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (; p < limit && (*p == ' ' || *p == '\t'); ++p) {
|
|
|
|
}
|
|
|
|
if (edit.descriptor == DataEdit::ListDirectedImaginaryPart) {
|
2021-11-20 07:49:16 +08:00
|
|
|
// Need to consume a trailing ')' and any white space after
|
2021-11-03 04:01:38 +08:00
|
|
|
if (p >= limit || *p != ')') {
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-20 07:49:16 +08:00
|
|
|
for (++p; p < limit && (*p == ' ' || *p == '\t'); ++p) {
|
2021-11-03 04:01:38 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-20 07:49:16 +08:00
|
|
|
if (edit.width && p < str + *edit.width) {
|
|
|
|
return false; // unconverted characters remain in fixed width field
|
2021-11-03 04:01:38 +08:00
|
|
|
}
|
|
|
|
// Success on the fast path!
|
|
|
|
// TODO: raise converted.flags as exceptions?
|
|
|
|
*reinterpret_cast<decimal::BinaryFloatingPointNumber<PRECISION> *>(n) =
|
|
|
|
converted.binary;
|
|
|
|
io.HandleRelativePosition(p - str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-03 03:39:05 +08:00
|
|
|
template <int KIND>
|
2020-02-14 06:41:56 +08:00
|
|
|
bool EditCommonRealInput(IoStatementState &io, const DataEdit &edit, void *n) {
|
2020-10-03 03:39:05 +08:00
|
|
|
constexpr int binaryPrecision{common::PrecisionOfRealKind(KIND)};
|
2021-11-03 04:01:38 +08:00
|
|
|
if (TryFastPathRealInput<binaryPrecision>(io, edit, n)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Fast path wasn't available or didn't work; go the more general route
|
2020-02-14 06:41:56 +08:00
|
|
|
static constexpr int maxDigits{
|
|
|
|
common::MaxDecimalConversionDigits(binaryPrecision)};
|
|
|
|
static constexpr int bufferSize{maxDigits + 18};
|
|
|
|
char buffer[bufferSize];
|
|
|
|
int exponent{0};
|
|
|
|
int got{ScanRealInput(buffer, maxDigits + 2, io, edit, exponent)};
|
|
|
|
if (got >= maxDigits + 2) {
|
2020-07-23 08:01:22 +08:00
|
|
|
io.GetIoErrorHandler().Crash("EditCommonRealInput: buffer was too small");
|
2020-02-14 06:41:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (got == 0) {
|
|
|
|
io.GetIoErrorHandler().SignalError("Bad REAL input value");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool hadExtra{got > maxDigits};
|
|
|
|
if (exponent != 0) {
|
2021-11-03 04:01:38 +08:00
|
|
|
buffer[got++] = 'e';
|
|
|
|
if (exponent < 0) {
|
|
|
|
buffer[got++] = '-';
|
|
|
|
exponent = -exponent;
|
|
|
|
}
|
|
|
|
if (exponent > 9999) {
|
|
|
|
exponent = 9999; // will convert to +/-Inf
|
|
|
|
}
|
|
|
|
if (exponent > 999) {
|
|
|
|
int dig{exponent / 1000};
|
|
|
|
buffer[got++] = '0' + dig;
|
|
|
|
int rest{exponent - 1000 * dig};
|
|
|
|
dig = rest / 100;
|
|
|
|
buffer[got++] = '0' + dig;
|
|
|
|
rest -= 100 * dig;
|
|
|
|
dig = rest / 10;
|
|
|
|
buffer[got++] = '0' + dig;
|
|
|
|
buffer[got++] = '0' + (rest - 10 * dig);
|
|
|
|
} else if (exponent > 99) {
|
|
|
|
int dig{exponent / 100};
|
|
|
|
buffer[got++] = '0' + dig;
|
|
|
|
int rest{exponent - 100 * dig};
|
|
|
|
dig = rest / 10;
|
|
|
|
buffer[got++] = '0' + dig;
|
|
|
|
buffer[got++] = '0' + (rest - 10 * dig);
|
|
|
|
} else if (exponent > 9) {
|
|
|
|
int dig{exponent / 10};
|
|
|
|
buffer[got++] = '0' + dig;
|
|
|
|
buffer[got++] = '0' + (exponent - 10 * dig);
|
|
|
|
} else {
|
|
|
|
buffer[got++] = '0' + exponent;
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
buffer[got] = '\0';
|
|
|
|
const char *p{buffer};
|
|
|
|
decimal::ConversionToBinaryResult<binaryPrecision> converted{
|
|
|
|
decimal::ConvertToBinary<binaryPrecision>(p, edit.modes.round)};
|
|
|
|
if (hadExtra) {
|
|
|
|
converted.flags = static_cast<enum decimal::ConversionResultFlags>(
|
|
|
|
converted.flags | decimal::Inexact);
|
|
|
|
}
|
|
|
|
// TODO: raise converted.flags as exceptions?
|
|
|
|
*reinterpret_cast<decimal::BinaryFloatingPointNumber<binaryPrecision> *>(n) =
|
|
|
|
converted.binary;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-03 03:39:05 +08:00
|
|
|
template <int KIND>
|
2020-02-14 06:41:56 +08:00
|
|
|
bool EditRealInput(IoStatementState &io, const DataEdit &edit, void *n) {
|
2020-10-03 03:39:05 +08:00
|
|
|
constexpr int binaryPrecision{common::PrecisionOfRealKind(KIND)};
|
2020-02-14 06:41:56 +08:00
|
|
|
switch (edit.descriptor) {
|
|
|
|
case DataEdit::ListDirected:
|
2021-10-21 04:56:47 +08:00
|
|
|
if (IsNamelistName(io)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return EditCommonRealInput<KIND>(io, edit, n);
|
2020-07-23 08:01:22 +08:00
|
|
|
case DataEdit::ListDirectedRealPart:
|
|
|
|
case DataEdit::ListDirectedImaginaryPart:
|
2020-02-14 06:41:56 +08:00
|
|
|
case 'F':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'E': // incl. EN, ES, & EX
|
2020-02-14 06:41:56 +08:00
|
|
|
case 'D':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'G':
|
2020-10-03 03:39:05 +08:00
|
|
|
return EditCommonRealInput<KIND>(io, edit, n);
|
2020-02-14 06:41:56 +08:00
|
|
|
case 'B':
|
|
|
|
return EditBOZInput(
|
|
|
|
io, edit, n, 2, common::BitsForBinaryPrecision(binaryPrecision));
|
|
|
|
case 'O':
|
|
|
|
return EditBOZInput(
|
|
|
|
io, edit, n, 8, common::BitsForBinaryPrecision(binaryPrecision));
|
|
|
|
case 'Z':
|
|
|
|
return EditBOZInput(
|
|
|
|
io, edit, n, 16, common::BitsForBinaryPrecision(binaryPrecision));
|
2021-10-22 04:33:07 +08:00
|
|
|
case 'A': // legacy extension
|
|
|
|
return EditDefaultCharacterInput(
|
|
|
|
io, edit, reinterpret_cast<char *>(n), KIND);
|
2020-02-14 06:41:56 +08:00
|
|
|
default:
|
|
|
|
io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
|
|
|
|
"Data edit descriptor '%c' may not be used for REAL input",
|
|
|
|
edit.descriptor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 13.7.3 in Fortran 2018
|
|
|
|
bool EditLogicalInput(IoStatementState &io, const DataEdit &edit, bool &x) {
|
|
|
|
switch (edit.descriptor) {
|
|
|
|
case DataEdit::ListDirected:
|
2021-10-21 04:56:47 +08:00
|
|
|
if (IsNamelistName(io)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2020-02-14 06:41:56 +08:00
|
|
|
case 'L':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'G':
|
|
|
|
break;
|
2020-02-14 06:41:56 +08:00
|
|
|
default:
|
|
|
|
io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
|
|
|
|
"Data edit descriptor '%c' may not be used for LOGICAL input",
|
|
|
|
edit.descriptor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::optional<int> remaining;
|
2021-05-06 02:37:49 +08:00
|
|
|
std::optional<char32_t> next{io.PrepareInput(edit, remaining)};
|
2020-03-29 12:00:16 +08:00
|
|
|
if (next && *next == '.') { // skip optional period
|
2020-02-14 06:41:56 +08:00
|
|
|
next = io.NextInField(remaining);
|
|
|
|
}
|
|
|
|
if (!next) {
|
|
|
|
io.GetIoErrorHandler().SignalError("Empty LOGICAL input field");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (*next) {
|
|
|
|
case 'T':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 't':
|
|
|
|
x = true;
|
|
|
|
break;
|
2020-02-14 06:41:56 +08:00
|
|
|
case 'F':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'f':
|
|
|
|
x = false;
|
|
|
|
break;
|
2020-02-14 06:41:56 +08:00
|
|
|
default:
|
|
|
|
io.GetIoErrorHandler().SignalError(
|
|
|
|
"Bad character '%lc' in LOGICAL input field", *next);
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
if (remaining) { // ignore the rest of the field
|
2020-02-14 06:41:56 +08:00
|
|
|
io.HandleRelativePosition(*remaining);
|
2020-07-15 03:31:16 +08:00
|
|
|
} else if (edit.descriptor == DataEdit::ListDirected) {
|
|
|
|
while (io.NextInField(remaining)) { // discard rest of field
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See 13.10.3.1 paragraphs 7-9 in Fortran 2018
|
|
|
|
static bool EditDelimitedCharacterInput(
|
|
|
|
IoStatementState &io, char *x, std::size_t length, char32_t delimiter) {
|
2021-05-06 02:37:49 +08:00
|
|
|
bool result{true};
|
2020-02-14 06:41:56 +08:00
|
|
|
while (true) {
|
2021-05-06 02:37:49 +08:00
|
|
|
auto ch{io.GetCurrentChar()};
|
|
|
|
if (!ch) {
|
|
|
|
if (io.AdvanceRecord()) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
result = false; // EOF in character value
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
if (*ch == delimiter) {
|
[flang] runtime: fix problems with I/O around EOF & delimited characters
When a WRITE overwrites an endfile record, we need to forget
that there was an endfile record. When doing a BACKSPACE
after an explicit ENDFILE statement, the position afterwards
must be upon the endfile record.
Attempts to join list-directed delimited character input across
record boundaries was due to a bad reading of the standard
and has been deleted, now that the requirements are better understood.
This problem would cause a read attempt past EOF if a delimited
character input value was at the end of a record.
It turns out that delimited list-directed (and NAMELIST) character
output is required to emit contiguous doubled instances of the
delimiter character when it appears in the output value. When
fixed-size records are being emitted, as is the case with internal
output, this is not possible when the problematic character falls
on the last position of a record. No two other Fortran compilers
do the same thing in this situation so there is no good precedent
to follow.
Because it seems least wrong, with this patch we now emit one copy
of the delimiter as the last character of the current record and
another as the first character of the next record. (The
second-least-wrong alternative might be to flag a runtime error,
but that seems harsh since it's not an explicit error in the standard,
and the output may not have to be usable later as input anyway.)
Consequently, the output is not suitable for use as list-directed or
NAMELIST input.
If a later standard were to clarify this case, this behavior will of
course change as needed to conform.
Differential Revision: https://reviews.llvm.org/D106695
2021-07-23 00:47:37 +08:00
|
|
|
auto next{io.GetCurrentChar()};
|
|
|
|
if (next && *next == delimiter) {
|
|
|
|
// Repeated delimiter: use as character value
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
} else {
|
|
|
|
break; // closing delimiter
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
2021-05-06 02:37:49 +08:00
|
|
|
}
|
|
|
|
if (length > 0) {
|
|
|
|
*x++ = *ch;
|
|
|
|
--length;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 02:37:49 +08:00
|
|
|
std::fill_n(x, length, ' ');
|
|
|
|
return result;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool EditListDirectedDefaultCharacterInput(
|
|
|
|
IoStatementState &io, char *x, std::size_t length) {
|
|
|
|
auto ch{io.GetCurrentChar()};
|
|
|
|
if (ch && (*ch == '\'' || *ch == '"')) {
|
|
|
|
io.HandleRelativePosition(1);
|
|
|
|
return EditDelimitedCharacterInput(io, x, length, *ch);
|
|
|
|
}
|
2021-10-21 04:56:47 +08:00
|
|
|
if (IsNamelistName(io)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
// Undelimited list-directed character input: stop at a value separator
|
|
|
|
// or the end of the current record.
|
|
|
|
std::optional<int> remaining{length};
|
|
|
|
for (std::optional<char32_t> next{io.NextInField(remaining)}; next;
|
|
|
|
next = io.NextInField(remaining)) {
|
|
|
|
switch (*next) {
|
|
|
|
case ' ':
|
2020-07-18 07:40:49 +08:00
|
|
|
case '\t':
|
2020-02-14 06:41:56 +08:00
|
|
|
case ',':
|
|
|
|
case ';':
|
|
|
|
case '/':
|
2020-03-29 12:00:16 +08:00
|
|
|
remaining = 0; // value separator: stop
|
2020-02-14 06:41:56 +08:00
|
|
|
break;
|
2020-03-29 12:00:16 +08:00
|
|
|
default:
|
|
|
|
*x++ = *next;
|
|
|
|
--length;
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::fill_n(x, length, ' ');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditDefaultCharacterInput(
|
|
|
|
IoStatementState &io, const DataEdit &edit, char *x, std::size_t length) {
|
|
|
|
switch (edit.descriptor) {
|
|
|
|
case DataEdit::ListDirected:
|
|
|
|
return EditListDirectedDefaultCharacterInput(io, x, length);
|
|
|
|
case 'A':
|
2020-03-29 12:00:16 +08:00
|
|
|
case 'G':
|
|
|
|
break;
|
2020-02-14 06:41:56 +08:00
|
|
|
default:
|
|
|
|
io.GetIoErrorHandler().SignalError(IostatErrorInFormat,
|
|
|
|
"Data edit descriptor '%c' may not be used with a CHARACTER data item",
|
|
|
|
edit.descriptor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::optional<int> remaining{length};
|
|
|
|
if (edit.width && *edit.width > 0) {
|
|
|
|
remaining = *edit.width;
|
|
|
|
}
|
|
|
|
// When the field is wider than the variable, we drop the leading
|
|
|
|
// characters. When the variable is wider than the field, there's
|
|
|
|
// trailing padding.
|
|
|
|
std::int64_t skip{*remaining - static_cast<std::int64_t>(length)};
|
|
|
|
for (std::optional<char32_t> next{io.NextInField(remaining)}; next;
|
|
|
|
next = io.NextInField(remaining)) {
|
|
|
|
if (skip > 0) {
|
|
|
|
--skip;
|
2021-09-21 01:52:39 +08:00
|
|
|
io.GotChar(-1);
|
2020-02-14 06:41:56 +08:00
|
|
|
} else {
|
|
|
|
*x++ = *next;
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::fill_n(x, length, ' ');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-03 03:39:05 +08:00
|
|
|
template bool EditRealInput<2>(IoStatementState &, const DataEdit &, void *);
|
|
|
|
template bool EditRealInput<3>(IoStatementState &, const DataEdit &, void *);
|
|
|
|
template bool EditRealInput<4>(IoStatementState &, const DataEdit &, void *);
|
2020-02-14 06:41:56 +08:00
|
|
|
template bool EditRealInput<8>(IoStatementState &, const DataEdit &, void *);
|
2020-10-03 03:39:05 +08:00
|
|
|
template bool EditRealInput<10>(IoStatementState &, const DataEdit &, void *);
|
|
|
|
// TODO: double/double
|
|
|
|
template bool EditRealInput<16>(IoStatementState &, const DataEdit &, void *);
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|