2020-01-28 10:18:45 +08:00
|
|
|
//===-- runtime/ISO_Fortran_binding.cpp -----------------------------------===//
|
2018-05-17 01:22:33 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// 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
|
2018-05-17 01:22:33 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-05-17 01:22:33 +08:00
|
|
|
|
|
|
|
// Implements the required interoperability API from ISO_Fortran_binding.h
|
|
|
|
// as specified in section 18.5.5 of Fortran 2018.
|
|
|
|
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/ISO_Fortran_binding.h"
|
|
|
|
#include "flang/Runtime/descriptor.h"
|
2020-02-14 06:41:56 +08:00
|
|
|
#include <cstdlib>
|
2018-05-17 01:22:33 +08:00
|
|
|
|
|
|
|
namespace Fortran::ISO {
|
|
|
|
extern "C" {
|
|
|
|
|
2018-11-29 04:17:34 +08:00
|
|
|
static inline constexpr bool IsCharacterType(CFI_type_t ty) {
|
[flang] More Fortran runtime support for CHARACTER operations
Summary:
- Remove C++ library dependence from lock.h
- Implement LEN_TRIM, REPEAT, ADJUSTL, ADJUSTR, MAX/MIN
intrinsic functions for CHARACTER
Reviewers: tskeith, PeteSteinfeld, sscalpone, schweitz, DavidTruby
Reviewed By: PeteSteinfeld
Subscribers: llvm-commits, flang-commits
Tags: #flang, #llvm
Differential Revision: https://reviews.llvm.org/D82054
2020-06-18 04:17:24 +08:00
|
|
|
return ty == CFI_type_char || ty == CFI_type_char16_t ||
|
|
|
|
ty == CFI_type_char32_t;
|
2018-11-29 04:17:34 +08:00
|
|
|
}
|
|
|
|
static inline constexpr bool IsAssumedSize(const CFI_cdesc_t *dv) {
|
|
|
|
return dv->rank > 0 && dv->dim[dv->rank - 1].extent == -1;
|
|
|
|
}
|
|
|
|
|
2018-05-17 01:31:35 +08:00
|
|
|
void *CFI_address(
|
|
|
|
const CFI_cdesc_t *descriptor, const CFI_index_t subscripts[]) {
|
2018-11-30 05:17:53 +08:00
|
|
|
char *p{static_cast<char *>(descriptor->base_addr)};
|
|
|
|
const CFI_rank_t rank{descriptor->rank};
|
2018-05-17 01:22:33 +08:00
|
|
|
const CFI_dim_t *dim{descriptor->dim};
|
2018-11-30 05:17:53 +08:00
|
|
|
for (CFI_rank_t j{0}; j < rank; ++j, ++dim) {
|
2018-05-17 01:22:33 +08:00
|
|
|
p += (subscripts[j] - dim->lower_bound) * dim->sm;
|
|
|
|
}
|
2018-11-30 05:17:53 +08:00
|
|
|
return p;
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFI_allocate(CFI_cdesc_t *descriptor, const CFI_index_t lower_bounds[],
|
2018-05-17 01:31:35 +08:00
|
|
|
const CFI_index_t upper_bounds[], std::size_t elem_len) {
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!descriptor) {
|
2018-11-28 08:07:21 +08:00
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2018-05-17 01:22:33 +08:00
|
|
|
if (descriptor->version != CFI_VERSION) {
|
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2018-08-03 08:04:31 +08:00
|
|
|
if (descriptor->attribute != CFI_attribute_allocatable &&
|
|
|
|
descriptor->attribute != CFI_attribute_pointer) {
|
2018-05-17 01:22:33 +08:00
|
|
|
// Non-interoperable object
|
2018-11-17 00:00:41 +08:00
|
|
|
return CFI_INVALID_ATTRIBUTE;
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
2018-08-03 08:04:31 +08:00
|
|
|
if (descriptor->attribute == CFI_attribute_allocatable &&
|
2019-11-10 01:29:31 +08:00
|
|
|
descriptor->base_addr) {
|
2018-05-17 01:22:33 +08:00
|
|
|
return CFI_ERROR_BASE_ADDR_NOT_NULL;
|
|
|
|
}
|
|
|
|
if (descriptor->rank > CFI_MAX_RANK) {
|
|
|
|
return CFI_INVALID_RANK;
|
|
|
|
}
|
2018-07-27 07:07:50 +08:00
|
|
|
if (descriptor->type < CFI_type_signed_char ||
|
2021-05-01 01:21:56 +08:00
|
|
|
descriptor->type > CFI_TYPE_LAST) {
|
2018-07-27 07:07:50 +08:00
|
|
|
return CFI_INVALID_TYPE;
|
|
|
|
}
|
2018-11-29 04:17:34 +08:00
|
|
|
if (!IsCharacterType(descriptor->type)) {
|
2018-05-17 01:22:33 +08:00
|
|
|
elem_len = descriptor->elem_len;
|
|
|
|
if (elem_len <= 0) {
|
|
|
|
return CFI_INVALID_ELEM_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::size_t rank{descriptor->rank};
|
|
|
|
CFI_dim_t *dim{descriptor->dim};
|
|
|
|
std::size_t byteSize{elem_len};
|
|
|
|
for (std::size_t j{0}; j < rank; ++j, ++dim) {
|
|
|
|
CFI_index_t lb{lower_bounds[j]};
|
|
|
|
CFI_index_t ub{upper_bounds[j]};
|
|
|
|
CFI_index_t extent{ub >= lb ? ub - lb + 1 : 0};
|
|
|
|
dim->lower_bound = lb;
|
|
|
|
dim->extent = extent;
|
|
|
|
dim->sm = byteSize;
|
|
|
|
byteSize *= extent;
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
void *p{std::malloc(byteSize)};
|
2020-11-11 07:13:02 +08:00
|
|
|
if (!p && byteSize) {
|
2018-05-17 01:22:33 +08:00
|
|
|
return CFI_ERROR_MEM_ALLOCATION;
|
|
|
|
}
|
|
|
|
descriptor->base_addr = p;
|
2018-07-27 07:07:50 +08:00
|
|
|
descriptor->elem_len = elem_len;
|
2018-05-17 01:22:33 +08:00
|
|
|
return CFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CFI_deallocate(CFI_cdesc_t *descriptor) {
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!descriptor) {
|
2018-11-28 08:07:21 +08:00
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2018-05-17 01:22:33 +08:00
|
|
|
if (descriptor->version != CFI_VERSION) {
|
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2018-08-03 08:04:31 +08:00
|
|
|
if (descriptor->attribute != CFI_attribute_allocatable &&
|
|
|
|
descriptor->attribute != CFI_attribute_pointer) {
|
2018-05-17 01:22:33 +08:00
|
|
|
// Non-interoperable object
|
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!descriptor->base_addr) {
|
2018-05-17 01:22:33 +08:00
|
|
|
return CFI_ERROR_BASE_ADDR_NULL;
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
std::free(descriptor->base_addr);
|
2018-05-17 01:22:33 +08:00
|
|
|
descriptor->base_addr = nullptr;
|
|
|
|
return CFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:07:50 +08:00
|
|
|
static constexpr std::size_t MinElemLen(CFI_type_t type) {
|
|
|
|
std::size_t minElemLen{0};
|
|
|
|
switch (type) {
|
2020-03-29 12:00:16 +08:00
|
|
|
case CFI_type_signed_char:
|
|
|
|
minElemLen = sizeof(signed char);
|
|
|
|
break;
|
|
|
|
case CFI_type_short:
|
|
|
|
minElemLen = sizeof(short);
|
|
|
|
break;
|
|
|
|
case CFI_type_int:
|
|
|
|
minElemLen = sizeof(int);
|
|
|
|
break;
|
|
|
|
case CFI_type_long:
|
|
|
|
minElemLen = sizeof(long);
|
|
|
|
break;
|
|
|
|
case CFI_type_long_long:
|
|
|
|
minElemLen = sizeof(long long);
|
|
|
|
break;
|
|
|
|
case CFI_type_size_t:
|
|
|
|
minElemLen = sizeof(std::size_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int8_t:
|
|
|
|
minElemLen = sizeof(std::int8_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int16_t:
|
|
|
|
minElemLen = sizeof(std::int16_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int32_t:
|
|
|
|
minElemLen = sizeof(std::int32_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int64_t:
|
|
|
|
minElemLen = sizeof(std::int64_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int128_t:
|
|
|
|
minElemLen = 2 * sizeof(std::int64_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int_least8_t:
|
|
|
|
minElemLen = sizeof(std::int_least8_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int_least16_t:
|
|
|
|
minElemLen = sizeof(std::int_least16_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int_least32_t:
|
|
|
|
minElemLen = sizeof(std::int_least32_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int_least64_t:
|
|
|
|
minElemLen = sizeof(std::int_least64_t);
|
|
|
|
break;
|
2018-07-27 07:07:50 +08:00
|
|
|
case CFI_type_int_least128_t:
|
|
|
|
minElemLen = 2 * sizeof(std::int_least64_t);
|
|
|
|
break;
|
2020-03-29 12:00:16 +08:00
|
|
|
case CFI_type_int_fast8_t:
|
|
|
|
minElemLen = sizeof(std::int_fast8_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int_fast16_t:
|
|
|
|
minElemLen = sizeof(std::int_fast16_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int_fast32_t:
|
|
|
|
minElemLen = sizeof(std::int_fast32_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_int_fast64_t:
|
|
|
|
minElemLen = sizeof(std::int_fast64_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_intmax_t:
|
|
|
|
minElemLen = sizeof(std::intmax_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_intptr_t:
|
|
|
|
minElemLen = sizeof(std::intptr_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_ptrdiff_t:
|
|
|
|
minElemLen = sizeof(std::ptrdiff_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_float:
|
|
|
|
minElemLen = sizeof(float);
|
|
|
|
break;
|
|
|
|
case CFI_type_double:
|
|
|
|
minElemLen = sizeof(double);
|
|
|
|
break;
|
|
|
|
case CFI_type_long_double:
|
|
|
|
minElemLen = sizeof(long double);
|
|
|
|
break;
|
|
|
|
case CFI_type_float_Complex:
|
|
|
|
minElemLen = 2 * sizeof(float);
|
|
|
|
break;
|
|
|
|
case CFI_type_double_Complex:
|
|
|
|
minElemLen = 2 * sizeof(double);
|
|
|
|
break;
|
2018-07-27 07:07:50 +08:00
|
|
|
case CFI_type_long_double_Complex:
|
|
|
|
minElemLen = 2 * sizeof(long double);
|
|
|
|
break;
|
2020-03-29 12:00:16 +08:00
|
|
|
case CFI_type_Bool:
|
|
|
|
minElemLen = 1;
|
|
|
|
break;
|
|
|
|
case CFI_type_cptr:
|
|
|
|
minElemLen = sizeof(void *);
|
|
|
|
break;
|
[flang] More Fortran runtime support for CHARACTER operations
Summary:
- Remove C++ library dependence from lock.h
- Implement LEN_TRIM, REPEAT, ADJUSTL, ADJUSTR, MAX/MIN
intrinsic functions for CHARACTER
Reviewers: tskeith, PeteSteinfeld, sscalpone, schweitz, DavidTruby
Reviewed By: PeteSteinfeld
Subscribers: llvm-commits, flang-commits
Tags: #flang, #llvm
Differential Revision: https://reviews.llvm.org/D82054
2020-06-18 04:17:24 +08:00
|
|
|
case CFI_type_char16_t:
|
|
|
|
minElemLen = sizeof(char16_t);
|
|
|
|
break;
|
|
|
|
case CFI_type_char32_t:
|
|
|
|
minElemLen = sizeof(char32_t);
|
|
|
|
break;
|
2018-07-27 07:07:50 +08:00
|
|
|
}
|
|
|
|
return minElemLen;
|
|
|
|
}
|
|
|
|
|
2018-05-17 01:22:33 +08:00
|
|
|
int CFI_establish(CFI_cdesc_t *descriptor, void *base_addr,
|
2018-05-17 01:31:35 +08:00
|
|
|
CFI_attribute_t attribute, CFI_type_t type, std::size_t elem_len,
|
|
|
|
CFI_rank_t rank, const CFI_index_t extents[]) {
|
2018-08-03 08:04:31 +08:00
|
|
|
if (attribute != CFI_attribute_other && attribute != CFI_attribute_pointer &&
|
|
|
|
attribute != CFI_attribute_allocatable) {
|
2018-05-17 01:22:33 +08:00
|
|
|
return CFI_INVALID_ATTRIBUTE;
|
|
|
|
}
|
|
|
|
if (rank > CFI_MAX_RANK) {
|
|
|
|
return CFI_INVALID_RANK;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (base_addr && attribute == CFI_attribute_allocatable) {
|
2018-08-03 08:04:31 +08:00
|
|
|
return CFI_ERROR_BASE_ADDR_NOT_NULL;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (rank > 0 && base_addr && !extents) {
|
2018-05-17 01:22:33 +08:00
|
|
|
return CFI_INVALID_EXTENT;
|
|
|
|
}
|
2021-05-01 01:21:56 +08:00
|
|
|
if (type < CFI_type_signed_char || type > CFI_TYPE_LAST) {
|
2018-07-27 07:07:50 +08:00
|
|
|
return CFI_INVALID_TYPE;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!descriptor) {
|
2018-11-28 08:07:21 +08:00
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2021-05-01 02:22:01 +08:00
|
|
|
if (type == CFI_type_struct || type == CFI_type_other ||
|
|
|
|
IsCharacterType(type)) {
|
2021-06-11 17:07:23 +08:00
|
|
|
if (elem_len <= 0) {
|
2021-05-01 02:22:01 +08:00
|
|
|
return CFI_INVALID_ELEM_LEN;
|
2021-06-11 17:07:23 +08:00
|
|
|
}
|
2021-05-01 02:22:01 +08:00
|
|
|
} else {
|
|
|
|
elem_len = MinElemLen(type);
|
|
|
|
assert(elem_len > 0 && "Unknown element length for type");
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
|
|
|
descriptor->base_addr = base_addr;
|
|
|
|
descriptor->elem_len = elem_len;
|
|
|
|
descriptor->version = CFI_VERSION;
|
|
|
|
descriptor->rank = rank;
|
2018-08-03 02:45:11 +08:00
|
|
|
descriptor->type = type;
|
2018-05-17 01:22:33 +08:00
|
|
|
descriptor->attribute = attribute;
|
2018-08-03 02:45:11 +08:00
|
|
|
descriptor->f18Addendum = 0;
|
2018-05-17 01:22:33 +08:00
|
|
|
std::size_t byteSize{elem_len};
|
2018-11-30 05:17:53 +08:00
|
|
|
constexpr std::size_t lower_bound{0};
|
2019-11-10 01:29:31 +08:00
|
|
|
if (base_addr) {
|
2018-11-05 23:07:18 +08:00
|
|
|
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];
|
|
|
|
}
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
|
|
|
return CFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CFI_is_contiguous(const CFI_cdesc_t *descriptor) {
|
2018-08-04 06:18:04 +08:00
|
|
|
CFI_index_t bytes = descriptor->elem_len;
|
2018-08-03 08:04:31 +08:00
|
|
|
for (int j{0}; j < descriptor->rank; ++j) {
|
|
|
|
if (bytes != descriptor->dim[j].sm) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
bytes *= descriptor->dim[j].extent;
|
|
|
|
}
|
|
|
|
return 1;
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFI_section(CFI_cdesc_t *result, const CFI_cdesc_t *source,
|
2018-05-17 01:31:35 +08:00
|
|
|
const CFI_index_t lower_bounds[], const CFI_index_t upper_bounds[],
|
|
|
|
const CFI_index_t strides[]) {
|
2018-11-16 23:10:04 +08:00
|
|
|
CFI_index_t extent[CFI_MAX_RANK];
|
|
|
|
CFI_index_t actualStride[CFI_MAX_RANK];
|
|
|
|
CFI_rank_t resRank{0};
|
|
|
|
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!result || !source) {
|
2018-11-28 08:07:21 +08:00
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2018-11-16 23:10:04 +08:00
|
|
|
if (source->rank == 0) {
|
|
|
|
return CFI_INVALID_RANK;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (IsAssumedSize(source) && !upper_bounds) {
|
2018-11-16 23:10:04 +08:00
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
|
|
|
if ((result->type != source->type) ||
|
|
|
|
(result->elem_len != source->elem_len)) {
|
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
|
|
|
if (result->attribute == CFI_attribute_allocatable) {
|
|
|
|
return CFI_INVALID_ATTRIBUTE;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!source->base_addr) {
|
2018-11-16 23:10:04 +08:00
|
|
|
return CFI_ERROR_BASE_ADDR_NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-28 08:07:21 +08:00
|
|
|
char *shiftedBaseAddr{static_cast<char *>(source->base_addr)};
|
2018-11-16 23:10:04 +08:00
|
|
|
bool isZeroSized{false};
|
|
|
|
for (int j{0}; j < source->rank; ++j) {
|
2018-11-28 06:13:21 +08:00
|
|
|
const CFI_dim_t &dim{source->dim[j]};
|
|
|
|
const CFI_index_t srcLB{dim.lower_bound};
|
|
|
|
const CFI_index_t srcUB{srcLB + dim.extent - 1};
|
2019-11-10 01:29:31 +08:00
|
|
|
const CFI_index_t lb{lower_bounds ? lower_bounds[j] : srcLB};
|
|
|
|
const CFI_index_t ub{upper_bounds ? upper_bounds[j] : srcUB};
|
|
|
|
const CFI_index_t stride{strides ? strides[j] : 1};
|
2018-11-16 23:10:04 +08:00
|
|
|
|
|
|
|
if (stride == 0 && lb != ub) {
|
|
|
|
return CFI_ERROR_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
if ((lb <= ub && stride >= 0) || (lb >= ub && stride < 0)) {
|
|
|
|
if ((lb < srcLB) || (lb > srcUB) || (ub < srcLB) || (ub > srcUB)) {
|
|
|
|
return CFI_ERROR_OUT_OF_BOUNDS;
|
|
|
|
}
|
|
|
|
shiftedBaseAddr += (lb - srcLB) * dim.sm;
|
2018-11-28 06:13:21 +08:00
|
|
|
extent[j] = stride != 0 ? 1 + (ub - lb) / stride : 1;
|
2018-11-16 23:10:04 +08:00
|
|
|
} else {
|
|
|
|
isZeroSized = true;
|
|
|
|
extent[j] = 0;
|
|
|
|
}
|
|
|
|
actualStride[j] = stride;
|
|
|
|
resRank += (stride != 0);
|
|
|
|
}
|
|
|
|
if (resRank != result->rank) {
|
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
|
|
|
|
2018-11-20 00:54:58 +08:00
|
|
|
// For zero-sized arrays, base_addr is processor-dependent (see 18.5.3).
|
2018-11-16 23:10:04 +08:00
|
|
|
// We keep it on the source base_addr
|
2018-11-20 01:10:08 +08:00
|
|
|
result->base_addr = isZeroSized ? source->base_addr : shiftedBaseAddr;
|
2018-11-16 23:10:04 +08:00
|
|
|
resRank = 0;
|
|
|
|
for (int j{0}; j < source->rank; ++j) {
|
|
|
|
if (actualStride[j] != 0) {
|
2018-11-19 23:50:30 +08:00
|
|
|
result->dim[resRank].lower_bound = 0;
|
2018-11-16 23:10:04 +08:00
|
|
|
result->dim[resRank].extent = extent[j];
|
|
|
|
result->dim[resRank].sm = actualStride[j] * source->dim[j].sm;
|
|
|
|
++resRank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CFI_SUCCESS;
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFI_select_part(CFI_cdesc_t *result, const CFI_cdesc_t *source,
|
2018-05-17 01:31:35 +08:00
|
|
|
std::size_t displacement, std::size_t elem_len) {
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!result || !source) {
|
2018-11-28 08:07:21 +08:00
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2018-11-16 23:10:04 +08:00
|
|
|
if (result->rank != source->rank) {
|
|
|
|
return CFI_INVALID_RANK;
|
|
|
|
}
|
|
|
|
if (result->attribute == CFI_attribute_allocatable) {
|
|
|
|
return CFI_INVALID_ATTRIBUTE;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!source->base_addr) {
|
2018-11-16 23:10:04 +08:00
|
|
|
return CFI_ERROR_BASE_ADDR_NULL;
|
|
|
|
}
|
|
|
|
if (IsAssumedSize(source)) {
|
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
|
|
|
|
2018-11-29 04:17:34 +08:00
|
|
|
if (!IsCharacterType(result->type)) {
|
2018-11-16 23:10:04 +08:00
|
|
|
elem_len = result->elem_len;
|
|
|
|
}
|
|
|
|
if (displacement + elem_len > source->elem_len) {
|
|
|
|
return CFI_INVALID_ELEM_LEN;
|
|
|
|
}
|
|
|
|
|
2018-11-30 05:17:53 +08:00
|
|
|
result->base_addr = displacement + static_cast<char *>(source->base_addr);
|
2018-11-16 23:10:04 +08:00
|
|
|
result->elem_len = elem_len;
|
|
|
|
for (int j{0}; j < source->rank; ++j) {
|
2018-11-19 23:50:30 +08:00
|
|
|
result->dim[j].lower_bound = 0;
|
|
|
|
result->dim[j].extent = source->dim[j].extent;
|
|
|
|
result->dim[j].sm = source->dim[j].sm;
|
2018-11-16 23:10:04 +08:00
|
|
|
}
|
|
|
|
return CFI_SUCCESS;
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CFI_setpointer(CFI_cdesc_t *result, const CFI_cdesc_t *source,
|
2018-05-17 01:31:35 +08:00
|
|
|
const CFI_index_t lower_bounds[]) {
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!result) {
|
2018-11-28 08:07:21 +08:00
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
2018-11-16 23:10:04 +08:00
|
|
|
if (result->attribute != CFI_attribute_pointer) {
|
|
|
|
return CFI_INVALID_ATTRIBUTE;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!source) {
|
2018-11-16 23:10:04 +08:00
|
|
|
result->base_addr = nullptr;
|
|
|
|
return CFI_SUCCESS;
|
|
|
|
}
|
|
|
|
if (source->rank != result->rank) {
|
|
|
|
return CFI_INVALID_RANK;
|
|
|
|
}
|
|
|
|
if (source->type != result->type) {
|
|
|
|
return CFI_INVALID_TYPE;
|
|
|
|
}
|
|
|
|
if (source->elem_len != result->elem_len) {
|
|
|
|
return CFI_INVALID_ELEM_LEN;
|
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!source->base_addr && source->attribute != CFI_attribute_pointer) {
|
2018-11-16 23:10:04 +08:00
|
|
|
return CFI_ERROR_BASE_ADDR_NULL;
|
|
|
|
}
|
|
|
|
if (IsAssumedSize(source)) {
|
|
|
|
return CFI_INVALID_DESCRIPTOR;
|
|
|
|
}
|
|
|
|
|
2019-11-10 01:29:31 +08:00
|
|
|
const bool copySrcLB{!lower_bounds};
|
2018-11-16 23:10:04 +08:00
|
|
|
result->base_addr = source->base_addr;
|
2019-11-10 01:29:31 +08:00
|
|
|
if (source->base_addr) {
|
2018-11-16 23:10:04 +08:00
|
|
|
for (int j{0}; j < result->rank; ++j) {
|
|
|
|
result->dim[j].extent = source->dim[j].extent;
|
|
|
|
result->dim[j].sm = source->dim[j].sm;
|
|
|
|
result->dim[j].lower_bound =
|
|
|
|
copySrcLB ? source->dim[j].lower_bound : lower_bounds[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CFI_SUCCESS;
|
2018-05-17 01:22:33 +08:00
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} // extern "C"
|
|
|
|
} // namespace Fortran::ISO
|