1 //===-- runtime/edit-input.cpp ----------------------------------*- 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 #include "edit-input.h" 10 #include "flang/Common/real.h" 11 #include "flang/Common/uint128.h" 12 #include <algorithm> 13 14 namespace Fortran::runtime::io { 15 16 // For fixed-width fields, initialize the number of remaining characters. 17 // Skip over leading blanks, then return the first non-blank character (if any). 18 static std::optional<char32_t> PrepareInput( 19 IoStatementState &io, const DataEdit &edit, std::optional<int> &remaining) { 20 remaining.reset(); 21 if (edit.descriptor == DataEdit::ListDirected) { 22 io.GetNextNonBlank(); 23 } else { 24 if (edit.width.value_or(0) > 0) { 25 remaining = *edit.width; 26 } 27 io.SkipSpaces(remaining); 28 } 29 return io.NextInField(remaining); 30 } 31 32 static bool EditBOZInput(IoStatementState &io, const DataEdit &edit, void *n, 33 int base, int totalBitSize) { 34 std::optional<int> remaining; 35 std::optional<char32_t> next{PrepareInput(io, edit, remaining)}; 36 common::UnsignedInt128 value{0}; 37 for (; next; next = io.NextInField(remaining)) { 38 char32_t ch{*next}; 39 if (ch == ' ' || ch == '\t') { 40 continue; 41 } 42 int digit{0}; 43 if (ch >= '0' && ch <= '1') { 44 digit = ch - '0'; 45 } else if (base >= 8 && ch >= '2' && ch <= '7') { 46 digit = ch - '0'; 47 } else if (base >= 10 && ch >= '8' && ch <= '9') { 48 digit = ch - '0'; 49 } else if (base == 16 && ch >= 'A' && ch <= 'Z') { 50 digit = ch + 10 - 'A'; 51 } else if (base == 16 && ch >= 'a' && ch <= 'z') { 52 digit = ch + 10 - 'a'; 53 } else { 54 io.GetIoErrorHandler().SignalError( 55 "Bad character '%lc' in B/O/Z input field", ch); 56 return false; 57 } 58 value *= base; 59 value += digit; 60 } 61 // TODO: check for overflow 62 std::memcpy(n, &value, totalBitSize >> 3); 63 return true; 64 } 65 66 // Prepares input from a field, and consumes the sign, if any. 67 // Returns true if there's a '-' sign. 68 static bool ScanNumericPrefix(IoStatementState &io, const DataEdit &edit, 69 std::optional<char32_t> &next, std::optional<int> &remaining) { 70 next = PrepareInput(io, edit, remaining); 71 bool negative{false}; 72 if (next) { 73 negative = *next == '-'; 74 if (negative || *next == '+') { 75 io.SkipSpaces(remaining); 76 next = io.NextInField(remaining); 77 } 78 } 79 return negative; 80 } 81 82 bool EditIntegerInput( 83 IoStatementState &io, const DataEdit &edit, void *n, int kind) { 84 RUNTIME_CHECK(io.GetIoErrorHandler(), kind >= 1 && !(kind & (kind - 1))); 85 switch (edit.descriptor) { 86 case DataEdit::ListDirected: 87 case 'G': 88 case 'I': 89 break; 90 case 'B': 91 return EditBOZInput(io, edit, n, 2, kind << 3); 92 case 'O': 93 return EditBOZInput(io, edit, n, 8, kind << 3); 94 case 'Z': 95 return EditBOZInput(io, edit, n, 16, kind << 3); 96 default: 97 io.GetIoErrorHandler().SignalError(IostatErrorInFormat, 98 "Data edit descriptor '%c' may not be used with an INTEGER data item", 99 edit.descriptor); 100 return false; 101 } 102 std::optional<int> remaining; 103 std::optional<char32_t> next; 104 bool negate{ScanNumericPrefix(io, edit, next, remaining)}; 105 common::UnsignedInt128 value; 106 for (; next; next = io.NextInField(remaining)) { 107 char32_t ch{*next}; 108 if (ch == ' ' || ch == '\t') { 109 if (edit.modes.editingFlags & blankZero) { 110 ch = '0'; // BZ mode - treat blank as if it were zero 111 } else { 112 continue; 113 } 114 } 115 int digit{0}; 116 if (ch >= '0' && ch <= '9') { 117 digit = ch - '0'; 118 } else { 119 io.GetIoErrorHandler().SignalError( 120 "Bad character '%lc' in INTEGER input field", ch); 121 return false; 122 } 123 value *= 10; 124 value += digit; 125 } 126 if (negate) { 127 value = -value; 128 } 129 std::memcpy(n, &value, kind); 130 return true; 131 } 132 133 // Parses a REAL input number from the input source as a normalized 134 // fraction into a supplied buffer -- there's an optional '-', a 135 // decimal point, and at least one digit. The adjusted exponent value 136 // is returned in a reference argument. The returned value is the number 137 // of characters that (should) have been written to the buffer -- this can 138 // be larger than the buffer size and can indicate overflow. Replaces 139 // blanks with zeroes if appropriate. 140 static int ScanRealInput(char *buffer, int bufferSize, IoStatementState &io, 141 const DataEdit &edit, int &exponent) { 142 std::optional<int> remaining; 143 std::optional<char32_t> next; 144 int got{0}; 145 std::optional<int> decimalPoint; 146 auto Put{[&](char ch) -> void { 147 if (got < bufferSize) { 148 buffer[got] = ch; 149 } 150 ++got; 151 }}; 152 if (ScanNumericPrefix(io, edit, next, remaining)) { 153 Put('-'); 154 } 155 if (!next) { // empty field means zero 156 Put('0'); 157 return got; 158 } 159 char32_t decimal = edit.modes.editingFlags & decimalComma ? ',' : '.'; 160 char32_t first{*next >= 'a' && *next <= 'z' ? *next + 'A' - 'a' : *next}; 161 if (first == 'N' || first == 'I') { 162 // NaN or infinity - convert to upper case 163 // Subtle: a blank field of digits could be followed by 'E' or 'D', 164 for (; next && 165 ((*next >= 'a' && *next <= 'z') || (*next >= 'A' && *next <= 'Z')); 166 next = io.NextInField(remaining)) { 167 if (*next >= 'a' && *next <= 'z') { 168 Put(*next - 'a' + 'A'); 169 } else { 170 Put(*next); 171 } 172 } 173 if (next && *next == '(') { // NaN(...) 174 while (next && *next != ')') { 175 next = io.NextInField(remaining); 176 } 177 } 178 exponent = 0; 179 } else if (first == decimal || (first >= '0' && first <= '9') || 180 first == 'E' || first == 'D' || first == 'Q') { 181 Put('.'); // input field is normalized to a fraction 182 auto start{got}; 183 bool bzMode{(edit.modes.editingFlags & blankZero) != 0}; 184 for (; next; next = io.NextInField(remaining)) { 185 char32_t ch{*next}; 186 if (ch == ' ' || ch == '\t') { 187 if (bzMode) { 188 ch = '0'; // BZ mode - treat blank as if it were zero 189 } else { 190 continue; 191 } 192 } 193 if (ch == '0' && got == start && !decimalPoint) { 194 // omit leading zeroes before the decimal 195 } else if (ch >= '0' && ch <= '9') { 196 Put(ch); 197 } else if (ch == decimal && !decimalPoint) { 198 // the decimal point is *not* copied to the buffer 199 decimalPoint = got - start; // # of digits before the decimal point 200 } else { 201 break; 202 } 203 } 204 if (got == start) { 205 Put('0'); // emit at least one digit 206 } 207 if (next && 208 (*next == 'e' || *next == 'E' || *next == 'd' || *next == 'D' || 209 *next == 'q' || *next == 'Q')) { 210 // Optional exponent letter. Blanks are allowed between the 211 // optional exponent letter and the exponent value. 212 io.SkipSpaces(remaining); 213 next = io.NextInField(remaining); 214 } 215 // The default exponent is -kP, but the scale factor doesn't affect 216 // an explicit exponent. 217 exponent = -edit.modes.scale; 218 if (next && 219 (*next == '-' || *next == '+' || (*next >= '0' && *next <= '9') || 220 (bzMode && (*next == ' ' || *next == '\t')))) { 221 bool negExpo{*next == '-'}; 222 if (negExpo || *next == '+') { 223 next = io.NextInField(remaining); 224 } 225 for (exponent = 0; next; next = io.NextInField(remaining)) { 226 if (*next >= '0' && *next <= '9') { 227 exponent = 10 * exponent + *next - '0'; 228 } else if (bzMode && (*next == ' ' || *next == '\t')) { 229 exponent = 10 * exponent; 230 } else { 231 break; 232 } 233 } 234 if (negExpo) { 235 exponent = -exponent; 236 } 237 } 238 if (decimalPoint) { 239 exponent += *decimalPoint; 240 } else { 241 // When no decimal point (or comma) appears in the value, the 'd' 242 // part of the edit descriptor must be interpreted as the number of 243 // digits in the value to be interpreted as being to the *right* of 244 // the assumed decimal point (13.7.2.3.2) 245 exponent += got - start - edit.digits.value_or(0); 246 } 247 } else { 248 // TODO: hex FP input 249 exponent = 0; 250 return 0; 251 } 252 if (remaining) { 253 while (next && (*next == ' ' || *next == '\t')) { 254 next = io.NextInField(remaining); 255 } 256 if (next) { 257 return 0; // error: unused nonblank character in fixed-width field 258 } 259 } 260 return got; 261 } 262 263 template <int binaryPrecision> 264 bool EditCommonRealInput(IoStatementState &io, const DataEdit &edit, void *n) { 265 static constexpr int maxDigits{ 266 common::MaxDecimalConversionDigits(binaryPrecision)}; 267 static constexpr int bufferSize{maxDigits + 18}; 268 char buffer[bufferSize]; 269 int exponent{0}; 270 int got{ScanRealInput(buffer, maxDigits + 2, io, edit, exponent)}; 271 if (got >= maxDigits + 2) { 272 io.GetIoErrorHandler().Crash("EditCommonRealInput: buffer was too small"); 273 return false; 274 } 275 if (got == 0) { 276 io.GetIoErrorHandler().SignalError("Bad REAL input value"); 277 return false; 278 } 279 bool hadExtra{got > maxDigits}; 280 if (exponent != 0) { 281 got += std::snprintf(&buffer[got], bufferSize - got, "e%d", exponent); 282 } 283 buffer[got] = '\0'; 284 const char *p{buffer}; 285 decimal::ConversionToBinaryResult<binaryPrecision> converted{ 286 decimal::ConvertToBinary<binaryPrecision>(p, edit.modes.round)}; 287 if (hadExtra) { 288 converted.flags = static_cast<enum decimal::ConversionResultFlags>( 289 converted.flags | decimal::Inexact); 290 } 291 // TODO: raise converted.flags as exceptions? 292 *reinterpret_cast<decimal::BinaryFloatingPointNumber<binaryPrecision> *>(n) = 293 converted.binary; 294 return true; 295 } 296 297 template <int binaryPrecision> 298 bool EditRealInput(IoStatementState &io, const DataEdit &edit, void *n) { 299 switch (edit.descriptor) { 300 case DataEdit::ListDirected: 301 case DataEdit::ListDirectedRealPart: 302 case DataEdit::ListDirectedImaginaryPart: 303 case 'F': 304 case 'E': // incl. EN, ES, & EX 305 case 'D': 306 case 'G': 307 return EditCommonRealInput<binaryPrecision>(io, edit, n); 308 case 'B': 309 return EditBOZInput( 310 io, edit, n, 2, common::BitsForBinaryPrecision(binaryPrecision)); 311 case 'O': 312 return EditBOZInput( 313 io, edit, n, 8, common::BitsForBinaryPrecision(binaryPrecision)); 314 case 'Z': 315 return EditBOZInput( 316 io, edit, n, 16, common::BitsForBinaryPrecision(binaryPrecision)); 317 default: 318 io.GetIoErrorHandler().SignalError(IostatErrorInFormat, 319 "Data edit descriptor '%c' may not be used for REAL input", 320 edit.descriptor); 321 return false; 322 } 323 } 324 325 // 13.7.3 in Fortran 2018 326 bool EditLogicalInput(IoStatementState &io, const DataEdit &edit, bool &x) { 327 switch (edit.descriptor) { 328 case DataEdit::ListDirected: 329 case 'L': 330 case 'G': 331 break; 332 default: 333 io.GetIoErrorHandler().SignalError(IostatErrorInFormat, 334 "Data edit descriptor '%c' may not be used for LOGICAL input", 335 edit.descriptor); 336 return false; 337 } 338 std::optional<int> remaining; 339 std::optional<char32_t> next{PrepareInput(io, edit, remaining)}; 340 if (next && *next == '.') { // skip optional period 341 next = io.NextInField(remaining); 342 } 343 if (!next) { 344 io.GetIoErrorHandler().SignalError("Empty LOGICAL input field"); 345 return false; 346 } 347 switch (*next) { 348 case 'T': 349 case 't': 350 x = true; 351 break; 352 case 'F': 353 case 'f': 354 x = false; 355 break; 356 default: 357 io.GetIoErrorHandler().SignalError( 358 "Bad character '%lc' in LOGICAL input field", *next); 359 return false; 360 } 361 if (remaining) { // ignore the rest of the field 362 io.HandleRelativePosition(*remaining); 363 } else if (edit.descriptor == DataEdit::ListDirected) { 364 while (io.NextInField(remaining)) { // discard rest of field 365 } 366 } 367 return true; 368 } 369 370 // See 13.10.3.1 paragraphs 7-9 in Fortran 2018 371 static bool EditDelimitedCharacterInput( 372 IoStatementState &io, char *x, std::size_t length, char32_t delimiter) { 373 while (true) { 374 if (auto ch{io.GetCurrentChar()}) { 375 io.HandleRelativePosition(1); 376 if (*ch == delimiter) { 377 ch = io.GetCurrentChar(); 378 if (ch && *ch == delimiter) { 379 // Repeated delimiter: use as character value. Can't straddle a 380 // record boundary. 381 io.HandleRelativePosition(1); 382 } else { 383 std::fill_n(x, length, ' '); 384 return true; 385 } 386 } 387 if (length > 0) { 388 *x++ = *ch; 389 --length; 390 } 391 } else if (!io.AdvanceRecord()) { // EOF 392 std::fill_n(x, length, ' '); 393 return false; 394 } 395 } 396 } 397 398 static bool EditListDirectedDefaultCharacterInput( 399 IoStatementState &io, char *x, std::size_t length) { 400 auto ch{io.GetCurrentChar()}; 401 if (ch && (*ch == '\'' || *ch == '"')) { 402 io.HandleRelativePosition(1); 403 return EditDelimitedCharacterInput(io, x, length, *ch); 404 } 405 // Undelimited list-directed character input: stop at a value separator 406 // or the end of the current record. 407 std::optional<int> remaining{length}; 408 for (std::optional<char32_t> next{io.NextInField(remaining)}; next; 409 next = io.NextInField(remaining)) { 410 switch (*next) { 411 case ' ': 412 case '\t': 413 case ',': 414 case ';': 415 case '/': 416 remaining = 0; // value separator: stop 417 break; 418 default: 419 *x++ = *next; 420 --length; 421 } 422 } 423 std::fill_n(x, length, ' '); 424 return true; 425 } 426 427 bool EditDefaultCharacterInput( 428 IoStatementState &io, const DataEdit &edit, char *x, std::size_t length) { 429 switch (edit.descriptor) { 430 case DataEdit::ListDirected: 431 return EditListDirectedDefaultCharacterInput(io, x, length); 432 case 'A': 433 case 'G': 434 break; 435 default: 436 io.GetIoErrorHandler().SignalError(IostatErrorInFormat, 437 "Data edit descriptor '%c' may not be used with a CHARACTER data item", 438 edit.descriptor); 439 return false; 440 } 441 std::optional<int> remaining{length}; 442 if (edit.width && *edit.width > 0) { 443 remaining = *edit.width; 444 } 445 // When the field is wider than the variable, we drop the leading 446 // characters. When the variable is wider than the field, there's 447 // trailing padding. 448 std::int64_t skip{*remaining - static_cast<std::int64_t>(length)}; 449 for (std::optional<char32_t> next{io.NextInField(remaining)}; next; 450 next = io.NextInField(remaining)) { 451 if (skip > 0) { 452 --skip; 453 } else { 454 *x++ = *next; 455 --length; 456 } 457 } 458 std::fill_n(x, length, ' '); 459 return true; 460 } 461 462 template bool EditRealInput<8>(IoStatementState &, const DataEdit &, void *); 463 template bool EditRealInput<11>(IoStatementState &, const DataEdit &, void *); 464 template bool EditRealInput<24>(IoStatementState &, const DataEdit &, void *); 465 template bool EditRealInput<53>(IoStatementState &, const DataEdit &, void *); 466 template bool EditRealInput<64>(IoStatementState &, const DataEdit &, void *); 467 template bool EditRealInput<113>(IoStatementState &, const DataEdit &, void *); 468 } // namespace Fortran::runtime::io 469