FLANG
descriptor-consts.h
1//===-- include/flang/Runtime/descriptor-consts.h ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_
10#define FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_
11
12#include "flang/Common/api-attrs.h"
13#include "flang/ISO_Fortran_binding_wrapper.h"
14#include <cstddef>
15#include <cstdint>
16
17// Value of the addendum presence flag.
18#define _CFI_ADDENDUM_FLAG 1
19// Number of bits needed to be shifted when manipulating the allocator index.
20#define _CFI_ALLOCATOR_IDX_SHIFT 1
21// Allocator index mask.
22#define _CFI_ALLOCATOR_IDX_MASK 0b00001110
23
24namespace Fortran::runtime::typeInfo {
25using TypeParameterValue = std::int64_t;
26class DerivedType;
27} // namespace Fortran::runtime::typeInfo
28
29namespace Fortran::runtime {
30class Descriptor;
31using SubscriptValue = ISO::CFI_index_t;
32
36static constexpr RT_API_ATTRS std::size_t MaxDescriptorSizeInBytes(
37 int rank, bool addendum = false, int lengthTypeParameters = 0) {
38 // Layout:
39 //
40 // fortran::runtime::Descriptor {
41 // ISO::CFI_cdesc_t {
42 // void *base_addr; (pointer -> up to 8 bytes)
43 // size_t elem_len; (up to 8 bytes)
44 // int version; (up to 4 bytes)
45 // CFI_rank_t rank; (unsigned char -> 1 byte)
46 // CFI_type_t type; (signed char -> 1 byte)
47 // CFI_attribute_t attribute; (unsigned char -> 1 byte)
48 // unsigned char extra; (1 byte)
49 // }
50 // }
51 // fortran::runtime::Dimension[rank] {
52 // ISO::CFI_dim_t {
53 // CFI_index_t lower_bound; (ptrdiff_t -> up to 8 bytes)
54 // CFI_index_t extent; (ptrdiff_t -> up to 8 bytes)
55 // CFI_index_t sm; (ptrdiff_t -> up to 8 bytes)
56 // }
57 // }
58 // fortran::runtime::DescriptorAddendum {
59 // const typeInfo::DerivedType *derivedType_; (pointer -> up to 8
60 // bytes) typeInfo::TypeParameterValue len_[lenParameters]; (int64_t -> 8
61 // bytes)
62 // }
63 std::size_t bytes{24u + rank * 24u};
64 if (addendum || lengthTypeParameters > 0) {
65 if (lengthTypeParameters < 1)
66 lengthTypeParameters = 1;
67 bytes += 8u + static_cast<std::size_t>(lengthTypeParameters) * 8u;
68 }
69 return bytes;
70}
71
72} // namespace Fortran::runtime
73
74#endif /* FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_ */