1 //===-- runtime/io-api.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 // Implements the I/O statement API 10 11 #include "flang/Runtime/io-api.h" 12 #include "descriptor-io.h" 13 #include "edit-input.h" 14 #include "edit-output.h" 15 #include "environment.h" 16 #include "format.h" 17 #include "io-stmt.h" 18 #include "terminator.h" 19 #include "tools.h" 20 #include "unit.h" 21 #include "flang/Runtime/descriptor.h" 22 #include "flang/Runtime/memory.h" 23 #include <cstdlib> 24 #include <memory> 25 26 namespace Fortran::runtime::io { 27 28 const char *InquiryKeywordHashDecode( 29 char *buffer, std::size_t n, InquiryKeywordHash hash) { 30 if (n < 1) { 31 return nullptr; 32 } 33 char *p{buffer + n}; 34 *--p = '\0'; 35 while (hash > 1) { 36 if (p < buffer) { 37 return nullptr; 38 } 39 *--p = 'A' + (hash % 26); 40 hash /= 26; 41 } 42 return hash == 1 ? p : nullptr; 43 } 44 45 template <Direction DIR> 46 Cookie BeginInternalArrayListIO(const Descriptor &descriptor, 47 void ** /*scratchArea*/, std::size_t /*scratchBytes*/, 48 const char *sourceFile, int sourceLine) { 49 Terminator oom{sourceFile, sourceLine}; 50 return &New<InternalListIoStatementState<DIR>>{oom}( 51 descriptor, sourceFile, sourceLine) 52 .release() 53 ->ioStatementState(); 54 } 55 56 Cookie IONAME(BeginInternalArrayListOutput)(const Descriptor &descriptor, 57 void **scratchArea, std::size_t scratchBytes, const char *sourceFile, 58 int sourceLine) { 59 return BeginInternalArrayListIO<Direction::Output>( 60 descriptor, scratchArea, scratchBytes, sourceFile, sourceLine); 61 } 62 63 Cookie IONAME(BeginInternalArrayListInput)(const Descriptor &descriptor, 64 void **scratchArea, std::size_t scratchBytes, const char *sourceFile, 65 int sourceLine) { 66 return BeginInternalArrayListIO<Direction::Input>( 67 descriptor, scratchArea, scratchBytes, sourceFile, sourceLine); 68 } 69 70 template <Direction DIR> 71 Cookie BeginInternalArrayFormattedIO(const Descriptor &descriptor, 72 const char *format, std::size_t formatLength, void ** /*scratchArea*/, 73 std::size_t /*scratchBytes*/, const char *sourceFile, int sourceLine) { 74 Terminator oom{sourceFile, sourceLine}; 75 return &New<InternalFormattedIoStatementState<DIR>>{oom}( 76 descriptor, format, formatLength, sourceFile, sourceLine) 77 .release() 78 ->ioStatementState(); 79 } 80 81 Cookie IONAME(BeginInternalArrayFormattedOutput)(const Descriptor &descriptor, 82 const char *format, std::size_t formatLength, void **scratchArea, 83 std::size_t scratchBytes, const char *sourceFile, int sourceLine) { 84 return BeginInternalArrayFormattedIO<Direction::Output>(descriptor, format, 85 formatLength, scratchArea, scratchBytes, sourceFile, sourceLine); 86 } 87 88 Cookie IONAME(BeginInternalArrayFormattedInput)(const Descriptor &descriptor, 89 const char *format, std::size_t formatLength, void **scratchArea, 90 std::size_t scratchBytes, const char *sourceFile, int sourceLine) { 91 return BeginInternalArrayFormattedIO<Direction::Input>(descriptor, format, 92 formatLength, scratchArea, scratchBytes, sourceFile, sourceLine); 93 } 94 95 template <Direction DIR> 96 Cookie BeginInternalListIO( 97 std::conditional_t<DIR == Direction::Input, const char, char> *internal, 98 std::size_t internalLength, void ** /*scratchArea*/, 99 std::size_t /*scratchBytes*/, const char *sourceFile, int sourceLine) { 100 Terminator oom{sourceFile, sourceLine}; 101 return &New<InternalListIoStatementState<DIR>>{oom}( 102 internal, internalLength, sourceFile, sourceLine) 103 .release() 104 ->ioStatementState(); 105 } 106 107 Cookie IONAME(BeginInternalListOutput)(char *internal, 108 std::size_t internalLength, void **scratchArea, std::size_t scratchBytes, 109 const char *sourceFile, int sourceLine) { 110 return BeginInternalListIO<Direction::Output>(internal, internalLength, 111 scratchArea, scratchBytes, sourceFile, sourceLine); 112 } 113 114 Cookie IONAME(BeginInternalListInput)(const char *internal, 115 std::size_t internalLength, void **scratchArea, std::size_t scratchBytes, 116 const char *sourceFile, int sourceLine) { 117 return BeginInternalListIO<Direction::Input>(internal, internalLength, 118 scratchArea, scratchBytes, sourceFile, sourceLine); 119 } 120 121 template <Direction DIR> 122 Cookie BeginInternalFormattedIO( 123 std::conditional_t<DIR == Direction::Input, const char, char> *internal, 124 std::size_t internalLength, const char *format, std::size_t formatLength, 125 void ** /*scratchArea*/, std::size_t /*scratchBytes*/, 126 const char *sourceFile, int sourceLine) { 127 Terminator oom{sourceFile, sourceLine}; 128 return &New<InternalFormattedIoStatementState<DIR>>{oom}( 129 internal, internalLength, format, formatLength, sourceFile, sourceLine) 130 .release() 131 ->ioStatementState(); 132 } 133 134 Cookie IONAME(BeginInternalFormattedOutput)(char *internal, 135 std::size_t internalLength, const char *format, std::size_t formatLength, 136 void **scratchArea, std::size_t scratchBytes, const char *sourceFile, 137 int sourceLine) { 138 return BeginInternalFormattedIO<Direction::Output>(internal, internalLength, 139 format, formatLength, scratchArea, scratchBytes, sourceFile, sourceLine); 140 } 141 142 Cookie IONAME(BeginInternalFormattedInput)(const char *internal, 143 std::size_t internalLength, const char *format, std::size_t formatLength, 144 void **scratchArea, std::size_t scratchBytes, const char *sourceFile, 145 int sourceLine) { 146 return BeginInternalFormattedIO<Direction::Input>(internal, internalLength, 147 format, formatLength, scratchArea, scratchBytes, sourceFile, sourceLine); 148 } 149 150 template <Direction DIR, template <Direction> class STATE, typename... A> 151 Cookie BeginExternalListIO( 152 int unitNumber, const char *sourceFile, int sourceLine, A &&...xs) { 153 Terminator terminator{sourceFile, sourceLine}; 154 if (unitNumber == DefaultUnit) { 155 unitNumber = DIR == Direction::Input ? 5 : 6; 156 } 157 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 158 unitNumber, DIR, false /*!unformatted*/, terminator)}; 159 if (!unit.isUnformatted.has_value()) { 160 unit.isUnformatted = false; 161 } 162 Iostat iostat{IostatOk}; 163 if (*unit.isUnformatted) { 164 iostat = IostatFormattedIoOnUnformattedUnit; 165 } 166 if (ChildIo * child{unit.GetChildIo()}) { 167 if (iostat == IostatOk) { 168 iostat = child->CheckFormattingAndDirection(false, DIR); 169 } 170 if (iostat == IostatOk) { 171 return &child->BeginIoStatement<ChildListIoStatementState<DIR>>( 172 *child, sourceFile, sourceLine); 173 } else { 174 return &child->BeginIoStatement<ErroneousIoStatementState>( 175 iostat, sourceFile, sourceLine); 176 } 177 } else { 178 if (iostat == IostatOk && unit.access == Access::Direct) { 179 iostat = IostatListIoOnDirectAccessUnit; 180 } 181 if (iostat == IostatOk) { 182 iostat = unit.SetDirection(DIR); 183 } 184 if (iostat == IostatOk) { 185 return &unit.BeginIoStatement<STATE<DIR>>( 186 std::forward<A>(xs)..., unit, sourceFile, sourceLine); 187 } else { 188 return &unit.BeginIoStatement<ErroneousIoStatementState>( 189 iostat, sourceFile, sourceLine); 190 } 191 } 192 } 193 194 Cookie IONAME(BeginExternalListOutput)( 195 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 196 return BeginExternalListIO<Direction::Output, ExternalListIoStatementState>( 197 unitNumber, sourceFile, sourceLine); 198 } 199 200 Cookie IONAME(BeginExternalListInput)( 201 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 202 return BeginExternalListIO<Direction::Input, ExternalListIoStatementState>( 203 unitNumber, sourceFile, sourceLine); 204 } 205 206 template <Direction DIR> 207 Cookie BeginExternalFormattedIO(const char *format, std::size_t formatLength, 208 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 209 Terminator terminator{sourceFile, sourceLine}; 210 if (unitNumber == DefaultUnit) { 211 unitNumber = DIR == Direction::Input ? 5 : 6; 212 } 213 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 214 unitNumber, DIR, false /*!unformatted*/, terminator)}; 215 Iostat iostat{IostatOk}; 216 if (!unit.isUnformatted.has_value()) { 217 unit.isUnformatted = false; 218 } 219 if (*unit.isUnformatted) { 220 iostat = IostatFormattedIoOnUnformattedUnit; 221 } 222 if (ChildIo * child{unit.GetChildIo()}) { 223 if (iostat == IostatOk) { 224 iostat = child->CheckFormattingAndDirection(false, DIR); 225 } 226 if (iostat == IostatOk) { 227 return &child->BeginIoStatement<ChildFormattedIoStatementState<DIR>>( 228 *child, format, formatLength, sourceFile, sourceLine); 229 } else { 230 return &child->BeginIoStatement<ErroneousIoStatementState>( 231 iostat, sourceFile, sourceLine); 232 } 233 } else { 234 if (iostat == IostatOk) { 235 iostat = unit.SetDirection(DIR); 236 } 237 if (iostat == IostatOk) { 238 return &unit.BeginIoStatement<ExternalFormattedIoStatementState<DIR>>( 239 unit, format, formatLength, sourceFile, sourceLine); 240 } else { 241 return &unit.BeginIoStatement<ErroneousIoStatementState>( 242 iostat, sourceFile, sourceLine); 243 } 244 } 245 } 246 247 Cookie IONAME(BeginExternalFormattedOutput)(const char *format, 248 std::size_t formatLength, ExternalUnit unitNumber, const char *sourceFile, 249 int sourceLine) { 250 return BeginExternalFormattedIO<Direction::Output>( 251 format, formatLength, unitNumber, sourceFile, sourceLine); 252 } 253 254 Cookie IONAME(BeginExternalFormattedInput)(const char *format, 255 std::size_t formatLength, ExternalUnit unitNumber, const char *sourceFile, 256 int sourceLine) { 257 return BeginExternalFormattedIO<Direction::Input>( 258 format, formatLength, unitNumber, sourceFile, sourceLine); 259 } 260 261 template <Direction DIR> 262 Cookie BeginUnformattedIO( 263 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 264 Terminator terminator{sourceFile, sourceLine}; 265 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 266 unitNumber, DIR, true /*unformatted*/, terminator)}; 267 Iostat iostat{IostatOk}; 268 if (!unit.isUnformatted.has_value()) { 269 unit.isUnformatted = true; 270 } 271 if (!*unit.isUnformatted) { 272 iostat = IostatUnformattedIoOnFormattedUnit; 273 } 274 if (ChildIo * child{unit.GetChildIo()}) { 275 if (iostat == IostatOk) { 276 iostat = child->CheckFormattingAndDirection(true, DIR); 277 } 278 if (iostat == IostatOk) { 279 return &child->BeginIoStatement<ChildUnformattedIoStatementState<DIR>>( 280 *child, sourceFile, sourceLine); 281 } else { 282 return &child->BeginIoStatement<ErroneousIoStatementState>( 283 iostat, sourceFile, sourceLine); 284 } 285 } else { 286 if (iostat == IostatOk) { 287 iostat = unit.SetDirection(DIR); 288 } 289 if (iostat == IostatOk) { 290 IoStatementState &io{ 291 unit.BeginIoStatement<ExternalUnformattedIoStatementState<DIR>>( 292 unit, sourceFile, sourceLine)}; 293 if constexpr (DIR == Direction::Output) { 294 if (unit.access == Access::Sequential) { 295 // Create space for (sub)record header to be completed by 296 // ExternalFileUnit::AdvanceRecord() 297 unit.recordLength.reset(); // in case of prior BACKSPACE 298 io.Emit("\0\0\0\0", 4); // placeholder for record length header 299 } 300 } 301 return &io; 302 } else { 303 return &unit.BeginIoStatement<ErroneousIoStatementState>( 304 iostat, sourceFile, sourceLine); 305 } 306 } 307 } 308 309 Cookie IONAME(BeginUnformattedOutput)( 310 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 311 return BeginUnformattedIO<Direction::Output>( 312 unitNumber, sourceFile, sourceLine); 313 } 314 315 Cookie IONAME(BeginUnformattedInput)( 316 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 317 return BeginUnformattedIO<Direction::Input>( 318 unitNumber, sourceFile, sourceLine); 319 } 320 321 Cookie IONAME(BeginOpenUnit)( // OPEN(without NEWUNIT=) 322 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 323 bool wasExtant{false}; 324 Terminator terminator{sourceFile, sourceLine}; 325 ExternalFileUnit &unit{ 326 ExternalFileUnit::LookUpOrCreate(unitNumber, terminator, wasExtant)}; 327 return &unit.BeginIoStatement<OpenStatementState>( 328 unit, wasExtant, sourceFile, sourceLine); 329 } 330 331 Cookie IONAME(BeginOpenNewUnit)( // OPEN(NEWUNIT=j) 332 const char *sourceFile, int sourceLine) { 333 Terminator terminator{sourceFile, sourceLine}; 334 ExternalFileUnit &unit{ 335 ExternalFileUnit::NewUnit(terminator, false /*not child I/O*/)}; 336 return &unit.BeginIoStatement<OpenStatementState>( 337 unit, false /*was an existing file*/, sourceFile, sourceLine); 338 } 339 340 Cookie IONAME(BeginClose)( 341 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 342 if (ExternalFileUnit * unit{ExternalFileUnit::LookUpForClose(unitNumber)}) { 343 return &unit->BeginIoStatement<CloseStatementState>( 344 *unit, sourceFile, sourceLine); 345 } else { 346 // CLOSE(UNIT=bad unit) is just a no-op 347 Terminator oom{sourceFile, sourceLine}; 348 return &New<NoopStatementState>{oom}(sourceFile, sourceLine) 349 .release() 350 ->ioStatementState(); 351 } 352 } 353 354 Cookie IONAME(BeginFlush)( 355 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 356 if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) { 357 return &unit->BeginIoStatement<ExternalMiscIoStatementState>( 358 *unit, ExternalMiscIoStatementState::Flush, sourceFile, sourceLine); 359 } else { 360 // FLUSH(UNIT=unknown) is a no-op 361 Terminator oom{sourceFile, sourceLine}; 362 return &New<NoopStatementState>{oom}(sourceFile, sourceLine) 363 .release() 364 ->ioStatementState(); 365 } 366 } 367 368 Cookie IONAME(BeginBackspace)( 369 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 370 Terminator terminator{sourceFile, sourceLine}; 371 ExternalFileUnit &unit{ 372 ExternalFileUnit::LookUpOrCrash(unitNumber, terminator)}; 373 return &unit.BeginIoStatement<ExternalMiscIoStatementState>( 374 unit, ExternalMiscIoStatementState::Backspace, sourceFile, sourceLine); 375 } 376 377 Cookie IONAME(BeginEndfile)( 378 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 379 Terminator terminator{sourceFile, sourceLine}; 380 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 381 unitNumber, Direction::Output, std::nullopt, terminator)}; 382 return &unit.BeginIoStatement<ExternalMiscIoStatementState>( 383 unit, ExternalMiscIoStatementState::Endfile, sourceFile, sourceLine); 384 } 385 386 Cookie IONAME(BeginRewind)( 387 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 388 Terminator terminator{sourceFile, sourceLine}; 389 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 390 unitNumber, Direction::Input, std::nullopt, terminator)}; 391 return &unit.BeginIoStatement<ExternalMiscIoStatementState>( 392 unit, ExternalMiscIoStatementState::Rewind, sourceFile, sourceLine); 393 } 394 395 Cookie IONAME(BeginInquireUnit)( 396 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 397 if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) { 398 if (ChildIo * child{unit->GetChildIo()}) { 399 return &child->BeginIoStatement<InquireUnitState>( 400 *unit, sourceFile, sourceLine); 401 } else { 402 return &unit->BeginIoStatement<InquireUnitState>( 403 *unit, sourceFile, sourceLine); 404 } 405 } else { 406 // INQUIRE(UNIT=unrecognized unit) 407 Terminator oom{sourceFile, sourceLine}; 408 return &New<InquireNoUnitState>{oom}(sourceFile, sourceLine) 409 .release() 410 ->ioStatementState(); 411 } 412 } 413 414 Cookie IONAME(BeginInquireFile)(const char *path, std::size_t pathLength, 415 const char *sourceFile, int sourceLine) { 416 Terminator oom{sourceFile, sourceLine}; 417 auto trimmed{ 418 SaveDefaultCharacter(path, TrimTrailingSpaces(path, pathLength), oom)}; 419 if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(trimmed.get())}) { 420 // INQUIRE(FILE=) to a connected unit 421 return &unit->BeginIoStatement<InquireUnitState>( 422 *unit, sourceFile, sourceLine); 423 } else { 424 return &New<InquireUnconnectedFileState>{oom}( 425 std::move(trimmed), sourceFile, sourceLine) 426 .release() 427 ->ioStatementState(); 428 } 429 } 430 431 Cookie IONAME(BeginInquireIoLength)(const char *sourceFile, int sourceLine) { 432 Terminator oom{sourceFile, sourceLine}; 433 return &New<InquireIOLengthState>{oom}(sourceFile, sourceLine) 434 .release() 435 ->ioStatementState(); 436 } 437 438 // Control list items 439 440 void IONAME(EnableHandlers)(Cookie cookie, bool hasIoStat, bool hasErr, 441 bool hasEnd, bool hasEor, bool hasIoMsg) { 442 IoErrorHandler &handler{cookie->GetIoErrorHandler()}; 443 if (hasIoStat) { 444 handler.HasIoStat(); 445 } 446 if (hasErr) { 447 handler.HasErrLabel(); 448 } 449 if (hasEnd) { 450 handler.HasEndLabel(); 451 } 452 if (hasEor) { 453 handler.HasEorLabel(); 454 } 455 if (hasIoMsg) { 456 handler.HasIoMsg(); 457 } 458 } 459 460 static bool YesOrNo(const char *keyword, std::size_t length, const char *what, 461 IoErrorHandler &handler) { 462 static const char *keywords[]{"YES", "NO", nullptr}; 463 switch (IdentifyValue(keyword, length, keywords)) { 464 case 0: 465 return true; 466 case 1: 467 return false; 468 default: 469 handler.SignalError(IostatErrorInKeyword, "Invalid %s='%.*s'", what, 470 static_cast<int>(length), keyword); 471 return false; 472 } 473 } 474 475 bool IONAME(SetAdvance)( 476 Cookie cookie, const char *keyword, std::size_t length) { 477 IoStatementState &io{*cookie}; 478 bool nonAdvancing{ 479 !YesOrNo(keyword, length, "ADVANCE", io.GetIoErrorHandler())}; 480 if (nonAdvancing && io.GetConnectionState().access == Access::Direct) { 481 io.GetIoErrorHandler().SignalError( 482 "Non-advancing I/O attempted on direct access file"); 483 } else { 484 io.mutableModes().nonAdvancing = nonAdvancing; 485 } 486 return true; 487 } 488 489 bool IONAME(SetBlank)(Cookie cookie, const char *keyword, std::size_t length) { 490 IoStatementState &io{*cookie}; 491 static const char *keywords[]{"NULL", "ZERO", nullptr}; 492 switch (IdentifyValue(keyword, length, keywords)) { 493 case 0: 494 io.mutableModes().editingFlags &= ~blankZero; 495 return true; 496 case 1: 497 io.mutableModes().editingFlags |= blankZero; 498 return true; 499 default: 500 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 501 "Invalid BLANK='%.*s'", static_cast<int>(length), keyword); 502 return false; 503 } 504 } 505 506 bool IONAME(SetDecimal)( 507 Cookie cookie, const char *keyword, std::size_t length) { 508 IoStatementState &io{*cookie}; 509 static const char *keywords[]{"COMMA", "POINT", nullptr}; 510 switch (IdentifyValue(keyword, length, keywords)) { 511 case 0: 512 io.mutableModes().editingFlags |= decimalComma; 513 return true; 514 case 1: 515 io.mutableModes().editingFlags &= ~decimalComma; 516 return true; 517 default: 518 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 519 "Invalid DECIMAL='%.*s'", static_cast<int>(length), keyword); 520 return false; 521 } 522 } 523 524 bool IONAME(SetDelim)(Cookie cookie, const char *keyword, std::size_t length) { 525 IoStatementState &io{*cookie}; 526 static const char *keywords[]{"APOSTROPHE", "QUOTE", "NONE", nullptr}; 527 switch (IdentifyValue(keyword, length, keywords)) { 528 case 0: 529 io.mutableModes().delim = '\''; 530 return true; 531 case 1: 532 io.mutableModes().delim = '"'; 533 return true; 534 case 2: 535 io.mutableModes().delim = '\0'; 536 return true; 537 default: 538 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 539 "Invalid DELIM='%.*s'", static_cast<int>(length), keyword); 540 return false; 541 } 542 } 543 544 bool IONAME(SetPad)(Cookie cookie, const char *keyword, std::size_t length) { 545 IoStatementState &io{*cookie}; 546 io.mutableModes().pad = 547 YesOrNo(keyword, length, "PAD", io.GetIoErrorHandler()); 548 return true; 549 } 550 551 bool IONAME(SetPos)(Cookie cookie, std::int64_t pos) { 552 IoStatementState &io{*cookie}; 553 ConnectionState &connection{io.GetConnectionState()}; 554 IoErrorHandler &handler{io.GetIoErrorHandler()}; 555 if (connection.access != Access::Stream) { 556 handler.SignalError("POS= may not appear unless ACCESS='STREAM'"); 557 return false; 558 } 559 if (pos < 1) { // POS=1 is beginning of file (12.6.2.11) 560 handler.SignalError("POS=%zd is invalid", static_cast<std::intmax_t>(pos)); 561 return false; 562 } 563 if (auto *unit{io.GetExternalFileUnit()}) { 564 unit->SetPosition(pos - 1, handler); 565 return true; 566 } 567 io.GetIoErrorHandler().Crash("SetPos() on internal unit"); 568 return false; 569 } 570 571 bool IONAME(SetRec)(Cookie cookie, std::int64_t rec) { 572 IoStatementState &io{*cookie}; 573 ConnectionState &connection{io.GetConnectionState()}; 574 IoErrorHandler &handler{io.GetIoErrorHandler()}; 575 if (connection.access != Access::Direct) { 576 handler.SignalError("REC= may not appear unless ACCESS='DIRECT'"); 577 return false; 578 } 579 if (!connection.openRecl) { 580 handler.SignalError("RECL= was not specified"); 581 return false; 582 } 583 if (rec < 1) { 584 handler.SignalError("REC=%zd is invalid", static_cast<std::intmax_t>(rec)); 585 return false; 586 } 587 connection.currentRecordNumber = rec; 588 if (auto *unit{io.GetExternalFileUnit()}) { 589 unit->SetPosition((rec - 1) * *connection.openRecl, handler); 590 } 591 return true; 592 } 593 594 bool IONAME(SetRound)(Cookie cookie, const char *keyword, std::size_t length) { 595 IoStatementState &io{*cookie}; 596 static const char *keywords[]{"UP", "DOWN", "ZERO", "NEAREST", "COMPATIBLE", 597 "PROCESSOR_DEFINED", nullptr}; 598 switch (IdentifyValue(keyword, length, keywords)) { 599 case 0: 600 io.mutableModes().round = decimal::RoundUp; 601 return true; 602 case 1: 603 io.mutableModes().round = decimal::RoundDown; 604 return true; 605 case 2: 606 io.mutableModes().round = decimal::RoundToZero; 607 return true; 608 case 3: 609 io.mutableModes().round = decimal::RoundNearest; 610 return true; 611 case 4: 612 io.mutableModes().round = decimal::RoundCompatible; 613 return true; 614 case 5: 615 io.mutableModes().round = executionEnvironment.defaultOutputRoundingMode; 616 return true; 617 default: 618 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 619 "Invalid ROUND='%.*s'", static_cast<int>(length), keyword); 620 return false; 621 } 622 } 623 624 bool IONAME(SetSign)(Cookie cookie, const char *keyword, std::size_t length) { 625 IoStatementState &io{*cookie}; 626 static const char *keywords[]{ 627 "PLUS", "SUPPRESS", "PROCESSOR_DEFINED", nullptr}; 628 switch (IdentifyValue(keyword, length, keywords)) { 629 case 0: 630 io.mutableModes().editingFlags |= signPlus; 631 return true; 632 case 1: 633 case 2: // processor default is SS 634 io.mutableModes().editingFlags &= ~signPlus; 635 return true; 636 default: 637 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 638 "Invalid SIGN='%.*s'", static_cast<int>(length), keyword); 639 return false; 640 } 641 } 642 643 bool IONAME(SetAccess)(Cookie cookie, const char *keyword, std::size_t length) { 644 IoStatementState &io{*cookie}; 645 auto *open{io.get_if<OpenStatementState>()}; 646 if (!open) { 647 io.GetIoErrorHandler().Crash( 648 "SetAccess() called when not in an OPEN statement"); 649 } 650 static const char *keywords[]{ 651 "SEQUENTIAL", "DIRECT", "STREAM", "APPEND", nullptr}; 652 switch (IdentifyValue(keyword, length, keywords)) { 653 case 0: 654 open->set_access(Access::Sequential); 655 break; 656 case 1: 657 open->set_access(Access::Direct); 658 break; 659 case 2: 660 open->set_access(Access::Stream); 661 break; 662 case 3: // Sun Fortran extension ACCESS=APPEND: treat as if POSITION=APPEND 663 open->set_position(Position::Append); 664 break; 665 default: 666 open->SignalError(IostatErrorInKeyword, "Invalid ACCESS='%.*s'", 667 static_cast<int>(length), keyword); 668 } 669 return true; 670 } 671 672 bool IONAME(SetAction)(Cookie cookie, const char *keyword, std::size_t length) { 673 IoStatementState &io{*cookie}; 674 auto *open{io.get_if<OpenStatementState>()}; 675 if (!open) { 676 io.GetIoErrorHandler().Crash( 677 "SetAction() called when not in an OPEN statement"); 678 } 679 std::optional<Action> action; 680 static const char *keywords[]{"READ", "WRITE", "READWRITE", nullptr}; 681 switch (IdentifyValue(keyword, length, keywords)) { 682 case 0: 683 action = Action::Read; 684 break; 685 case 1: 686 action = Action::Write; 687 break; 688 case 2: 689 action = Action::ReadWrite; 690 break; 691 default: 692 open->SignalError(IostatErrorInKeyword, "Invalid ACTION='%.*s'", 693 static_cast<int>(length), keyword); 694 return false; 695 } 696 RUNTIME_CHECK(io.GetIoErrorHandler(), action.has_value()); 697 if (open->wasExtant()) { 698 if ((*action != Action::Write) != open->unit().mayRead() || 699 (*action != Action::Read) != open->unit().mayWrite()) { 700 open->SignalError("ACTION= may not be changed on an open unit"); 701 } 702 } 703 open->set_action(*action); 704 return true; 705 } 706 707 bool IONAME(SetAsynchronous)( 708 Cookie cookie, const char *keyword, std::size_t length) { 709 IoStatementState &io{*cookie}; 710 auto *open{io.get_if<OpenStatementState>()}; 711 if (!open) { 712 io.GetIoErrorHandler().Crash( 713 "SetAsynchronous() called when not in an OPEN statement"); 714 } 715 static const char *keywords[]{"YES", "NO", nullptr}; 716 switch (IdentifyValue(keyword, length, keywords)) { 717 case 0: 718 open->unit().set_mayAsynchronous(true); 719 return true; 720 case 1: 721 open->unit().set_mayAsynchronous(false); 722 return true; 723 default: 724 open->SignalError(IostatErrorInKeyword, "Invalid ASYNCHRONOUS='%.*s'", 725 static_cast<int>(length), keyword); 726 return false; 727 } 728 } 729 730 bool IONAME(SetCarriagecontrol)( 731 Cookie cookie, const char *keyword, std::size_t length) { 732 IoStatementState &io{*cookie}; 733 auto *open{io.get_if<OpenStatementState>()}; 734 if (!open) { 735 io.GetIoErrorHandler().Crash( 736 "SetCarriageControl() called when not in an OPEN statement"); 737 } 738 static const char *keywords[]{"LIST", "FORTRAN", "NONE", nullptr}; 739 switch (IdentifyValue(keyword, length, keywords)) { 740 case 0: 741 return true; 742 case 1: 743 case 2: 744 open->SignalError(IostatErrorInKeyword, 745 "Unimplemented CARRIAGECONTROL='%.*s'", static_cast<int>(length), 746 keyword); 747 return false; 748 default: 749 open->SignalError(IostatErrorInKeyword, "Invalid CARRIAGECONTROL='%.*s'", 750 static_cast<int>(length), keyword); 751 return false; 752 } 753 } 754 755 bool IONAME(SetConvert)( 756 Cookie cookie, const char *keyword, std::size_t length) { 757 IoStatementState &io{*cookie}; 758 auto *open{io.get_if<OpenStatementState>()}; 759 if (!open) { 760 io.GetIoErrorHandler().Crash( 761 "SetConvert() called when not in an OPEN statement"); 762 } 763 if (auto convert{GetConvertFromString(keyword, length)}) { 764 open->set_convert(*convert); 765 return true; 766 } else { 767 open->SignalError(IostatErrorInKeyword, "Invalid CONVERT='%.*s'", 768 static_cast<int>(length), keyword); 769 return false; 770 } 771 } 772 773 bool IONAME(SetEncoding)( 774 Cookie cookie, const char *keyword, std::size_t length) { 775 IoStatementState &io{*cookie}; 776 auto *open{io.get_if<OpenStatementState>()}; 777 if (!open) { 778 io.GetIoErrorHandler().Crash( 779 "SetEncoding() called when not in an OPEN statement"); 780 } 781 bool isUTF8{false}; 782 static const char *keywords[]{"UTF-8", "DEFAULT", nullptr}; 783 switch (IdentifyValue(keyword, length, keywords)) { 784 case 0: 785 isUTF8 = true; 786 break; 787 case 1: 788 isUTF8 = false; 789 break; 790 default: 791 open->SignalError(IostatErrorInKeyword, "Invalid ENCODING='%.*s'", 792 static_cast<int>(length), keyword); 793 } 794 if (isUTF8 != open->unit().isUTF8) { 795 if (open->wasExtant()) { 796 open->SignalError("ENCODING= may not be changed on an open unit"); 797 } 798 open->unit().isUTF8 = isUTF8; 799 } 800 return true; 801 } 802 803 bool IONAME(SetForm)(Cookie cookie, const char *keyword, std::size_t length) { 804 IoStatementState &io{*cookie}; 805 auto *open{io.get_if<OpenStatementState>()}; 806 if (!open) { 807 io.GetIoErrorHandler().Crash( 808 "SetForm() called when not in an OPEN statement"); 809 } 810 static const char *keywords[]{"FORMATTED", "UNFORMATTED", nullptr}; 811 switch (IdentifyValue(keyword, length, keywords)) { 812 case 0: 813 open->set_isUnformatted(false); 814 break; 815 case 1: 816 open->set_isUnformatted(true); 817 break; 818 default: 819 open->SignalError(IostatErrorInKeyword, "Invalid FORM='%.*s'", 820 static_cast<int>(length), keyword); 821 } 822 return true; 823 } 824 825 bool IONAME(SetPosition)( 826 Cookie cookie, const char *keyword, std::size_t length) { 827 IoStatementState &io{*cookie}; 828 auto *open{io.get_if<OpenStatementState>()}; 829 if (!open) { 830 io.GetIoErrorHandler().Crash( 831 "SetPosition() called when not in an OPEN statement"); 832 } 833 static const char *positions[]{"ASIS", "REWIND", "APPEND", nullptr}; 834 switch (IdentifyValue(keyword, length, positions)) { 835 case 0: 836 open->set_position(Position::AsIs); 837 return true; 838 case 1: 839 open->set_position(Position::Rewind); 840 return true; 841 case 2: 842 open->set_position(Position::Append); 843 return true; 844 default: 845 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 846 "Invalid POSITION='%.*s'", static_cast<int>(length), keyword); 847 } 848 return true; 849 } 850 851 bool IONAME(SetRecl)(Cookie cookie, std::size_t n) { 852 IoStatementState &io{*cookie}; 853 auto *open{io.get_if<OpenStatementState>()}; 854 if (!open) { 855 io.GetIoErrorHandler().Crash( 856 "SetRecl() called when not in an OPEN statement"); 857 } 858 if (n <= 0) { 859 io.GetIoErrorHandler().SignalError("RECL= must be greater than zero"); 860 return false; 861 } else if (open->wasExtant() && 862 open->unit().openRecl.value_or(0) != static_cast<std::int64_t>(n)) { 863 open->SignalError("RECL= may not be changed for an open unit"); 864 return false; 865 } else { 866 open->unit().openRecl = n; 867 return true; 868 } 869 } 870 871 bool IONAME(SetStatus)(Cookie cookie, const char *keyword, std::size_t length) { 872 IoStatementState &io{*cookie}; 873 if (auto *open{io.get_if<OpenStatementState>()}) { 874 static const char *statuses[]{ 875 "OLD", "NEW", "SCRATCH", "REPLACE", "UNKNOWN", nullptr}; 876 switch (IdentifyValue(keyword, length, statuses)) { 877 case 0: 878 open->set_status(OpenStatus::Old); 879 return true; 880 case 1: 881 open->set_status(OpenStatus::New); 882 return true; 883 case 2: 884 open->set_status(OpenStatus::Scratch); 885 return true; 886 case 3: 887 open->set_status(OpenStatus::Replace); 888 return true; 889 case 4: 890 open->set_status(OpenStatus::Unknown); 891 return true; 892 default: 893 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 894 "Invalid STATUS='%.*s'", static_cast<int>(length), keyword); 895 } 896 return false; 897 } 898 if (auto *close{io.get_if<CloseStatementState>()}) { 899 static const char *statuses[]{"KEEP", "DELETE", nullptr}; 900 switch (IdentifyValue(keyword, length, statuses)) { 901 case 0: 902 close->set_status(CloseStatus::Keep); 903 return true; 904 case 1: 905 close->set_status(CloseStatus::Delete); 906 return true; 907 default: 908 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 909 "Invalid STATUS='%.*s'", static_cast<int>(length), keyword); 910 } 911 return false; 912 } 913 if (io.get_if<NoopStatementState>()) { 914 return true; // don't bother validating STATUS= in a no-op CLOSE 915 } 916 io.GetIoErrorHandler().Crash( 917 "SetStatus() called when not in an OPEN or CLOSE statement"); 918 } 919 920 bool IONAME(SetFile)(Cookie cookie, const char *path, std::size_t chars) { 921 IoStatementState &io{*cookie}; 922 if (auto *open{io.get_if<OpenStatementState>()}) { 923 open->set_path(path, chars); 924 return true; 925 } 926 io.GetIoErrorHandler().Crash( 927 "SetFile() called when not in an OPEN statement"); 928 return false; 929 } 930 931 bool IONAME(GetNewUnit)(Cookie cookie, int &unit, int kind) { 932 IoStatementState &io{*cookie}; 933 auto *open{io.get_if<OpenStatementState>()}; 934 if (!open) { 935 io.GetIoErrorHandler().Crash( 936 "GetNewUnit() called when not in an OPEN statement"); 937 } 938 std::int64_t result{open->unit().unitNumber()}; 939 if (!SetInteger(unit, kind, result)) { 940 open->SignalError("GetNewUnit(): Bad INTEGER kind(%d) or out-of-range " 941 "value(%jd) for result", 942 kind, static_cast<std::intmax_t>(result)); 943 } 944 return true; 945 } 946 947 // Data transfers 948 949 bool IONAME(OutputDescriptor)(Cookie cookie, const Descriptor &descriptor) { 950 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 951 } 952 953 bool IONAME(InputDescriptor)(Cookie cookie, const Descriptor &descriptor) { 954 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 955 } 956 957 bool IONAME(OutputUnformattedBlock)(Cookie cookie, const char *x, 958 std::size_t length, std::size_t elementBytes) { 959 IoStatementState &io{*cookie}; 960 if (auto *unf{io.get_if< 961 ExternalUnformattedIoStatementState<Direction::Output>>()}) { 962 return unf->Emit(x, length, elementBytes); 963 } else if (auto *inq{io.get_if<InquireIOLengthState>()}) { 964 return inq->Emit(x, length, elementBytes); 965 } 966 io.GetIoErrorHandler().Crash("OutputUnformattedBlock() called for an I/O " 967 "statement that is not unformatted output"); 968 return false; 969 } 970 971 bool IONAME(InputUnformattedBlock)( 972 Cookie cookie, char *x, std::size_t length, std::size_t elementBytes) { 973 IoStatementState &io{*cookie}; 974 io.BeginReadingRecord(); 975 if (io.GetIoErrorHandler().InError()) { 976 return false; 977 } 978 if (auto *unf{ 979 io.get_if<ExternalUnformattedIoStatementState<Direction::Input>>()}) { 980 return unf->Receive(x, length, elementBytes); 981 } 982 io.GetIoErrorHandler().Crash("InputUnformattedBlock() called for an I/O " 983 "statement that is not unformatted output"); 984 return false; 985 } 986 987 bool IONAME(OutputInteger8)(Cookie cookie, std::int8_t n) { 988 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger8"); 989 StaticDescriptor staticDescriptor; 990 Descriptor &descriptor{staticDescriptor.descriptor()}; 991 descriptor.Establish( 992 TypeCategory::Integer, 1, reinterpret_cast<void *>(&n), 0); 993 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 994 } 995 996 bool IONAME(OutputInteger16)(Cookie cookie, std::int16_t n) { 997 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger16"); 998 StaticDescriptor staticDescriptor; 999 Descriptor &descriptor{staticDescriptor.descriptor()}; 1000 descriptor.Establish( 1001 TypeCategory::Integer, 2, reinterpret_cast<void *>(&n), 0); 1002 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1003 } 1004 1005 bool IONAME(OutputInteger32)(Cookie cookie, std::int32_t n) { 1006 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger32"); 1007 StaticDescriptor staticDescriptor; 1008 Descriptor &descriptor{staticDescriptor.descriptor()}; 1009 descriptor.Establish( 1010 TypeCategory::Integer, 4, reinterpret_cast<void *>(&n), 0); 1011 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1012 } 1013 1014 bool IONAME(OutputInteger64)(Cookie cookie, std::int64_t n) { 1015 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger64"); 1016 StaticDescriptor staticDescriptor; 1017 Descriptor &descriptor{staticDescriptor.descriptor()}; 1018 descriptor.Establish( 1019 TypeCategory::Integer, 8, reinterpret_cast<void *>(&n), 0); 1020 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1021 } 1022 1023 #ifdef __SIZEOF_INT128__ 1024 bool IONAME(OutputInteger128)(Cookie cookie, common::int128_t n) { 1025 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger128"); 1026 StaticDescriptor staticDescriptor; 1027 Descriptor &descriptor{staticDescriptor.descriptor()}; 1028 descriptor.Establish( 1029 TypeCategory::Integer, 16, reinterpret_cast<void *>(&n), 0); 1030 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1031 } 1032 #endif 1033 1034 bool IONAME(InputInteger)(Cookie cookie, std::int64_t &n, int kind) { 1035 cookie->CheckFormattedStmtType<Direction::Input>("InputInteger"); 1036 StaticDescriptor staticDescriptor; 1037 Descriptor &descriptor{staticDescriptor.descriptor()}; 1038 descriptor.Establish( 1039 TypeCategory::Integer, kind, reinterpret_cast<void *>(&n), 0); 1040 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1041 } 1042 1043 bool IONAME(OutputReal32)(Cookie cookie, float x) { 1044 cookie->CheckFormattedStmtType<Direction::Output>("OutputReal32"); 1045 StaticDescriptor staticDescriptor; 1046 Descriptor &descriptor{staticDescriptor.descriptor()}; 1047 descriptor.Establish(TypeCategory::Real, 4, reinterpret_cast<void *>(&x), 0); 1048 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1049 } 1050 1051 bool IONAME(OutputReal64)(Cookie cookie, double x) { 1052 cookie->CheckFormattedStmtType<Direction::Output>("OutputReal64"); 1053 StaticDescriptor staticDescriptor; 1054 Descriptor &descriptor{staticDescriptor.descriptor()}; 1055 descriptor.Establish(TypeCategory::Real, 8, reinterpret_cast<void *>(&x), 0); 1056 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1057 } 1058 1059 bool IONAME(InputReal32)(Cookie cookie, float &x) { 1060 cookie->CheckFormattedStmtType<Direction::Input>("InputReal32"); 1061 StaticDescriptor staticDescriptor; 1062 Descriptor &descriptor{staticDescriptor.descriptor()}; 1063 descriptor.Establish(TypeCategory::Real, 4, reinterpret_cast<void *>(&x), 0); 1064 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1065 } 1066 1067 bool IONAME(InputReal64)(Cookie cookie, double &x) { 1068 cookie->CheckFormattedStmtType<Direction::Input>("InputReal64"); 1069 StaticDescriptor staticDescriptor; 1070 Descriptor &descriptor{staticDescriptor.descriptor()}; 1071 descriptor.Establish(TypeCategory::Real, 8, reinterpret_cast<void *>(&x), 0); 1072 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1073 } 1074 1075 bool IONAME(OutputComplex32)(Cookie cookie, float r, float i) { 1076 cookie->CheckFormattedStmtType<Direction::Output>("OutputComplex32"); 1077 float z[2]{r, i}; 1078 StaticDescriptor staticDescriptor; 1079 Descriptor &descriptor{staticDescriptor.descriptor()}; 1080 descriptor.Establish( 1081 TypeCategory::Complex, 4, reinterpret_cast<void *>(&z), 0); 1082 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1083 } 1084 1085 bool IONAME(OutputComplex64)(Cookie cookie, double r, double i) { 1086 cookie->CheckFormattedStmtType<Direction::Output>("OutputComplex64"); 1087 double z[2]{r, i}; 1088 StaticDescriptor staticDescriptor; 1089 Descriptor &descriptor{staticDescriptor.descriptor()}; 1090 descriptor.Establish( 1091 TypeCategory::Complex, 8, reinterpret_cast<void *>(&z), 0); 1092 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1093 } 1094 1095 bool IONAME(InputComplex32)(Cookie cookie, float z[2]) { 1096 cookie->CheckFormattedStmtType<Direction::Input>("InputComplex32"); 1097 StaticDescriptor staticDescriptor; 1098 Descriptor &descriptor{staticDescriptor.descriptor()}; 1099 descriptor.Establish( 1100 TypeCategory::Complex, 4, reinterpret_cast<void *>(z), 0); 1101 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1102 } 1103 1104 bool IONAME(InputComplex64)(Cookie cookie, double z[2]) { 1105 cookie->CheckFormattedStmtType<Direction::Input>("InputComplex64"); 1106 StaticDescriptor staticDescriptor; 1107 Descriptor &descriptor{staticDescriptor.descriptor()}; 1108 descriptor.Establish( 1109 TypeCategory::Complex, 8, reinterpret_cast<void *>(z), 0); 1110 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1111 } 1112 1113 bool IONAME(OutputCharacter)( 1114 Cookie cookie, const char *x, std::size_t length, int kind) { 1115 cookie->CheckFormattedStmtType<Direction::Output>("OutputCharacter"); 1116 StaticDescriptor staticDescriptor; 1117 Descriptor &descriptor{staticDescriptor.descriptor()}; 1118 descriptor.Establish( 1119 kind, length, reinterpret_cast<void *>(const_cast<char *>(x)), 0); 1120 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1121 } 1122 1123 bool IONAME(OutputAscii)(Cookie cookie, const char *x, std::size_t length) { 1124 return IONAME(OutputCharacter(cookie, x, length, 1)); 1125 } 1126 1127 bool IONAME(InputCharacter)( 1128 Cookie cookie, char *x, std::size_t length, int kind) { 1129 cookie->CheckFormattedStmtType<Direction::Input>("InputCharacter"); 1130 StaticDescriptor staticDescriptor; 1131 Descriptor &descriptor{staticDescriptor.descriptor()}; 1132 descriptor.Establish(kind, length, reinterpret_cast<void *>(x), 0); 1133 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1134 } 1135 1136 bool IONAME(InputAscii)(Cookie cookie, char *x, std::size_t length) { 1137 return IONAME(InputCharacter(cookie, x, length, 1)); 1138 } 1139 1140 bool IONAME(OutputLogical)(Cookie cookie, bool truth) { 1141 cookie->CheckFormattedStmtType<Direction::Output>("OutputLogical"); 1142 StaticDescriptor staticDescriptor; 1143 Descriptor &descriptor{staticDescriptor.descriptor()}; 1144 descriptor.Establish( 1145 TypeCategory::Logical, sizeof truth, reinterpret_cast<void *>(&truth), 0); 1146 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1147 } 1148 1149 bool IONAME(InputLogical)(Cookie cookie, bool &truth) { 1150 cookie->CheckFormattedStmtType<Direction::Input>("InputLogical"); 1151 StaticDescriptor staticDescriptor; 1152 Descriptor &descriptor{staticDescriptor.descriptor()}; 1153 descriptor.Establish( 1154 TypeCategory::Logical, sizeof truth, reinterpret_cast<void *>(&truth), 0); 1155 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1156 } 1157 1158 std::size_t IONAME(GetSize)(Cookie cookie) { 1159 IoStatementState &io{*cookie}; 1160 if (const auto *formatted{ 1161 io.get_if<FormattedIoStatementState<Direction::Input>>()}) { 1162 return formatted->GetEditDescriptorChars(); 1163 } 1164 io.GetIoErrorHandler().Crash( 1165 "GetIoSize() called for an I/O statement that is not a formatted READ()"); 1166 return 0; 1167 } 1168 1169 std::size_t IONAME(GetIoLength)(Cookie cookie) { 1170 IoStatementState &io{*cookie}; 1171 if (const auto *inq{io.get_if<InquireIOLengthState>()}) { 1172 return inq->bytes(); 1173 } 1174 io.GetIoErrorHandler().Crash("GetIoLength() called for an I/O statement that " 1175 "is not INQUIRE(IOLENGTH=)"); 1176 return 0; 1177 } 1178 1179 void IONAME(GetIoMsg)(Cookie cookie, char *msg, std::size_t length) { 1180 IoErrorHandler &handler{cookie->GetIoErrorHandler()}; 1181 if (handler.InError()) { // leave "msg" alone when no error 1182 handler.GetIoMsg(msg, length); 1183 } 1184 } 1185 1186 bool IONAME(InquireCharacter)(Cookie cookie, InquiryKeywordHash inquiry, 1187 char *result, std::size_t length) { 1188 IoStatementState &io{*cookie}; 1189 return io.Inquire(inquiry, result, length); 1190 } 1191 1192 bool IONAME(InquireLogical)( 1193 Cookie cookie, InquiryKeywordHash inquiry, bool &result) { 1194 IoStatementState &io{*cookie}; 1195 return io.Inquire(inquiry, result); 1196 } 1197 1198 bool IONAME(InquirePendingId)(Cookie cookie, std::int64_t id, bool &result) { 1199 IoStatementState &io{*cookie}; 1200 return io.Inquire(HashInquiryKeyword("PENDING"), id, result); 1201 } 1202 1203 bool IONAME(InquireInteger64)( 1204 Cookie cookie, InquiryKeywordHash inquiry, std::int64_t &result, int kind) { 1205 IoStatementState &io{*cookie}; 1206 std::int64_t n; 1207 if (io.Inquire(inquiry, n)) { 1208 if (SetInteger(result, kind, n)) { 1209 return true; 1210 } 1211 io.GetIoErrorHandler().SignalError( 1212 "InquireInteger64(): Bad INTEGER kind(%d) or out-of-range value(%jd) " 1213 "for result", 1214 kind, static_cast<std::intmax_t>(n)); 1215 } 1216 return false; 1217 } 1218 1219 enum Iostat IONAME(EndIoStatement)(Cookie cookie) { 1220 IoStatementState &io{*cookie}; 1221 return static_cast<enum Iostat>(io.EndIoStatement()); 1222 } 1223 } // namespace Fortran::runtime::io 1224