[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.
This commit is contained in:
Peter Klausler
2025-09-19 08:09:39 -07:00
committed by GitHub
parent b21dd44dbc
commit 80fa3bddd0
5 changed files with 16 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ class OpenFile {
public:
using FileOffset = std::int64_t;
int fd() const { return fd_; }
const char *path() const { return path_.get(); }
std::size_t pathLength() const { return pathLength_; }
void set_path(OwningPtr<char> &&, std::size_t bytes);

View File

@@ -424,6 +424,15 @@ std::int64_t RTNAME(Ftell)(int unitNumber) {
return -1;
}
}
std::int32_t FORTRAN_PROCEDURE_NAME(fnum)(const int &unitNumber) {
if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) {
return unit->fd();
} else {
return -1;
}
}
} // namespace io
} // extern "C"

View File

@@ -59,6 +59,7 @@ class PseudoOpenFile {
public:
using FileOffset = std::int64_t;
RT_API_ATTRS int fd() const { return 1 /*stdout*/; }
RT_API_ATTRS const char *path() const { return nullptr; }
RT_API_ATTRS std::size_t pathLength() const { return 0; }
RT_API_ATTRS void set_path(OwningPtr<char> &&, std::size_t bytes) {}

View File

@@ -716,6 +716,7 @@ CALL BACKTRACE()
CALL FDATE(TIME)
CALL GETLOG(USRNAME)
CALL GETENV(NAME [, VALUE, LENGTH, STATUS, TRIM_NAME, ERRMSG ])
unixFD = FNUM(FORTRAN_UNIT)
```
## Intrinsic Procedure Name Resolution
@@ -778,6 +779,7 @@ This phase currently supports all the intrinsic procedures listed above but the
| Atomic intrinsic subroutines | ATOMIC_ADD |
| Collective intrinsic subroutines | CO_REDUCE |
| Library subroutines | BACKTRACE, FDATE, GETLOG, GETENV |
| Library functions | FNUM |
### Intrinsic Function Folding

View File

@@ -45,6 +45,9 @@ std::int32_t RTNAME(Fseek)(int unit, std::int64_t zeroBasedPos, int whence,
const char *sourceFileName, int lineNumber);
std::int64_t RTNAME(Ftell)(int unit);
// FNUM maps a Fortran unit number to its UNIX file descriptor
std::int32_t FORTRAN_PROCEDURE_NAME(fnum)(const int &unitNumber);
// GNU Fortran 77 compatibility function IARGC.
std::int32_t FORTRAN_PROCEDURE_NAME(iargc)();