Files
clang-p2996/flang-rt/lib/runtime/ISO_Fortran_util.h
Michael Kruse 54f37133b7 [Flang][NFC] Move runtime library files to flang-rt (#110298)
Mostly mechanical changes in preparation of extracting the Flang-RT
"subproject" in #110217. This PR intends to only move pre-existing files
to the new folder structure, with no behavioral change. Common files
(headers, testing, cmake) shared by Flang-RT and Flang remain in
`flang/`.

Some cosmetic changes and files paths were necessary:
* Relative paths to the new path for the source files and
`add_subdirectory`.
 * Add the new location's include directory to `include_directories`
* The unittest/Evaluate directory has unitests for flang-rt and Flang. A
new `CMakeLists.txt` was introduced for the flang-rt tests.
 * Change the `#include` paths relative to the include directive
 * clang-format on the `#include` directives
* Since the paths are part if the copyright header and include guards, a
script was used to canonicalize those
* `test/Runtime` and runtime tests in `test/Driver` are moved, but the
lit.cfg.py mechanism to execute the will only be added in #110217.
2025-02-16 13:25:31 +01:00

103 lines
3.4 KiB
C++

//===-- lib/runtime/ISO_Fortran_util.h --------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef FLANG_RT_RUNTIME_ISO_FORTRAN_UTIL_H_
#define FLANG_RT_RUNTIME_ISO_FORTRAN_UTIL_H_
// Internal utils for establishing CFI_cdesc_t descriptors.
#include "flang-rt/runtime/descriptor.h"
#include "flang-rt/runtime/terminator.h"
#include "flang-rt/runtime/type-code.h"
#include "flang/Common/ISO_Fortran_binding_wrapper.h"
#include <cstdlib>
namespace Fortran::ISO {
static inline constexpr RT_API_ATTRS bool IsCharacterType(CFI_type_t ty) {
return ty == CFI_type_char || ty == CFI_type_char16_t ||
ty == CFI_type_char32_t;
}
static inline constexpr RT_API_ATTRS bool IsAssumedSize(const CFI_cdesc_t *dv) {
return dv->rank > 0 && dv->dim[dv->rank - 1].extent == -1;
}
static inline RT_API_ATTRS std::size_t MinElemLen(CFI_type_t type) {
auto typeParams{Fortran::runtime::TypeCode{type}.GetCategoryAndKind()};
if (!typeParams) {
Fortran::runtime::Terminator terminator{__FILE__, __LINE__};
terminator.Crash(
"not yet implemented: CFI_type_t=%d", static_cast<int>(type));
}
return Fortran::runtime::Descriptor::BytesFor(
typeParams->first, typeParams->second);
}
static inline RT_API_ATTRS int VerifyEstablishParameters(
CFI_cdesc_t *descriptor, void *base_addr, CFI_attribute_t attribute,
CFI_type_t type, std::size_t elem_len, CFI_rank_t rank,
const CFI_index_t extents[], bool external) {
if (attribute != CFI_attribute_other && attribute != CFI_attribute_pointer &&
attribute != CFI_attribute_allocatable) {
return CFI_INVALID_ATTRIBUTE;
}
if (rank > CFI_MAX_RANK) {
return CFI_INVALID_RANK;
}
if (base_addr && attribute == CFI_attribute_allocatable) {
return CFI_ERROR_BASE_ADDR_NOT_NULL;
}
if (rank > 0 && base_addr && !extents) {
return CFI_INVALID_EXTENT;
}
if (type < CFI_type_signed_char || type > CFI_TYPE_LAST) {
return CFI_INVALID_TYPE;
}
if (!descriptor) {
return CFI_INVALID_DESCRIPTOR;
}
if (external) {
if (type == CFI_type_struct || type == CFI_type_other ||
IsCharacterType(type)) {
if (elem_len <= 0) {
return CFI_INVALID_ELEM_LEN;
}
}
} else {
// We do not expect CFI_type_other for internal invocations.
if (type == CFI_type_other) {
return CFI_INVALID_TYPE;
}
}
return CFI_SUCCESS;
}
static inline RT_API_ATTRS void EstablishDescriptor(CFI_cdesc_t *descriptor,
void *base_addr, CFI_attribute_t attribute, CFI_type_t type,
std::size_t elem_len, CFI_rank_t rank, const CFI_index_t extents[]) {
descriptor->base_addr = base_addr;
descriptor->elem_len = elem_len;
descriptor->version = CFI_VERSION;
descriptor->rank = rank;
descriptor->type = type;
descriptor->attribute = attribute;
descriptor->extra = 0;
std::size_t byteSize{elem_len};
constexpr std::size_t lower_bound{0};
if (base_addr) {
for (std::size_t j{0}; j < rank; ++j) {
descriptor->dim[j].lower_bound = lower_bound;
descriptor->dim[j].extent = extents[j];
descriptor->dim[j].sm = byteSize;
byteSize *= extents[j];
}
}
}
} // namespace Fortran::ISO
#endif // FLANG_RT_RUNTIME_ISO_FORTRAN_UTIL_H_