9#ifndef FORTRAN_RUNTIME_ISO_FORTRAN_UTIL_H_
10#define FORTRAN_RUNTIME_ISO_FORTRAN_UTIL_H_
14#include "terminator.h"
15#include "flang/ISO_Fortran_binding_wrapper.h"
16#include "flang/Runtime/descriptor.h"
17#include "flang/Runtime/type-code.h"
20namespace Fortran::ISO {
21static inline constexpr RT_API_ATTRS
bool IsCharacterType(CFI_type_t ty) {
22 return ty == CFI_type_char || ty == CFI_type_char16_t ||
23 ty == CFI_type_char32_t;
25static inline constexpr RT_API_ATTRS
bool IsAssumedSize(
const CFI_cdesc_t *dv) {
26 return dv->rank > 0 && dv->dim[dv->rank - 1].extent == -1;
29static inline RT_API_ATTRS std::size_t MinElemLen(CFI_type_t type) {
34 "not yet implemented: CFI_type_t=%d",
static_cast<int>(type));
37 return Fortran::runtime::Descriptor::BytesFor(
38 typeParams->first, typeParams->second);
41static inline RT_API_ATTRS
int VerifyEstablishParameters(
42 CFI_cdesc_t *descriptor,
void *base_addr, CFI_attribute_t attribute,
43 CFI_type_t type, std::size_t elem_len, CFI_rank_t rank,
44 const CFI_index_t extents[],
bool external) {
45 if (attribute != CFI_attribute_other && attribute != CFI_attribute_pointer &&
46 attribute != CFI_attribute_allocatable) {
47 return CFI_INVALID_ATTRIBUTE;
49 if (rank > CFI_MAX_RANK) {
50 return CFI_INVALID_RANK;
52 if (base_addr && attribute == CFI_attribute_allocatable) {
53 return CFI_ERROR_BASE_ADDR_NOT_NULL;
55 if (rank > 0 && base_addr && !extents) {
56 return CFI_INVALID_EXTENT;
58 if (type < CFI_type_signed_char || type > CFI_TYPE_LAST) {
59 return CFI_INVALID_TYPE;
62 return CFI_INVALID_DESCRIPTOR;
65 if (type == CFI_type_struct || type == CFI_type_other ||
66 IsCharacterType(type)) {
68 return CFI_INVALID_ELEM_LEN;
73 if (type == CFI_type_other) {
74 return CFI_INVALID_TYPE;
80static inline RT_API_ATTRS
void EstablishDescriptor(
CFI_cdesc_t *descriptor,
81 void *base_addr, CFI_attribute_t attribute, CFI_type_t type,
82 std::size_t elem_len, CFI_rank_t rank,
const CFI_index_t extents[]) {
83 descriptor->base_addr = base_addr;
84 descriptor->elem_len = elem_len;
85 descriptor->version = CFI_VERSION;
86 descriptor->rank = rank;
87 descriptor->type = type;
88 descriptor->attribute = attribute;
89 descriptor->extra = 0;
90 std::size_t byteSize{elem_len};
91 constexpr std::size_t lower_bound{0};
93 for (std::size_t j{0}; j < rank; ++j) {
94 descriptor->dim[j].lower_bound = lower_bound;
95 descriptor->dim[j].extent = extents[j];
96 descriptor->dim[j].sm = byteSize;
97 byteSize *= extents[j];
Definition: terminator.h:23
Definition: type-code.h:21
Definition: ISO_Fortran_binding.h:158