Skip to content

Commit 5196411

Browse files
klauslergithub-actions[bot]
authored andcommitted
Automerge: [flang] Implement FNUM() (#159433)
The GNU Fortran library function FNUM(u) returns the UNIX file descriptor that corresponds to an open Fortran unit number, if any; otherwise -1. This implementation is a library extension only, not an intrinsic.
2 parents 1e283bf + 80fa3bd commit 5196411

File tree

5 files changed

+16
-0
lines changed

5 files changed

+16
-0
lines changed

flang-rt/include/flang-rt/runtime/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class OpenFile {
2727
public:
2828
using FileOffset = std::int64_t;
2929

30+
int fd() const { return fd_; }
3031
const char *path() const { return path_.get(); }
3132
std::size_t pathLength() const { return pathLength_; }
3233
void set_path(OwningPtr<char> &&, std::size_t bytes);

flang-rt/lib/runtime/extensions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,15 @@ std::int64_t RTNAME(Ftell)(int unitNumber) {
424424
return -1;
425425
}
426426
}
427+
428+
std::int32_t FORTRAN_PROCEDURE_NAME(fnum)(const int &unitNumber) {
429+
if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) {
430+
return unit->fd();
431+
} else {
432+
return -1;
433+
}
434+
}
435+
427436
} // namespace io
428437

429438
} // extern "C"

flang-rt/lib/runtime/unit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class PseudoOpenFile {
5959
public:
6060
using FileOffset = std::int64_t;
6161

62+
RT_API_ATTRS int fd() const { return 1 /*stdout*/; }
6263
RT_API_ATTRS const char *path() const { return nullptr; }
6364
RT_API_ATTRS std::size_t pathLength() const { return 0; }
6465
RT_API_ATTRS void set_path(OwningPtr<char> &&, std::size_t bytes) {}

flang/docs/Intrinsics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ CALL BACKTRACE()
716716
CALL FDATE(TIME)
717717
CALL GETLOG(USRNAME)
718718
CALL GETENV(NAME [, VALUE, LENGTH, STATUS, TRIM_NAME, ERRMSG ])
719+
unixFD = FNUM(FORTRAN_UNIT)
719720
```
720721

721722
## Intrinsic Procedure Name Resolution
@@ -778,6 +779,7 @@ This phase currently supports all the intrinsic procedures listed above but the
778779
| Atomic intrinsic subroutines | ATOMIC_ADD |
779780
| Collective intrinsic subroutines | CO_REDUCE |
780781
| Library subroutines | BACKTRACE, FDATE, GETLOG, GETENV |
782+
| Library functions | FNUM |
781783

782784

783785
### Intrinsic Function Folding

flang/include/flang/Runtime/extensions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ std::int32_t RTNAME(Fseek)(int unit, std::int64_t zeroBasedPos, int whence,
4545
const char *sourceFileName, int lineNumber);
4646
std::int64_t RTNAME(Ftell)(int unit);
4747

48+
// FNUM maps a Fortran unit number to its UNIX file descriptor
49+
std::int32_t FORTRAN_PROCEDURE_NAME(fnum)(const int &unitNumber);
50+
4851
// GNU Fortran 77 compatibility function IARGC.
4952
std::int32_t FORTRAN_PROCEDURE_NAME(iargc)();
5053

0 commit comments

Comments
 (0)