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/Fortran-consts.h"
13#include "flang/Common/ISO_Fortran_binding_wrapper.h"
14#include "flang/Common/api-attrs.h"
15#include <cstddef>
16#include <cstdint>
17
18// Value of the addendum presence flag.
19#define _CFI_ADDENDUM_FLAG 1
20// Number of bits needed to be shifted when manipulating the allocator index.
21#define _CFI_ALLOCATOR_IDX_SHIFT 1
22// Allocator index mask.
23#define _CFI_ALLOCATOR_IDX_MASK 0b00001110
24
25namespace Fortran::runtime::typeInfo {
26using TypeParameterValue = std::int64_t;
27class DerivedType;
28} // namespace Fortran::runtime::typeInfo
29
30namespace Fortran::runtime {
31class Descriptor;
32using SubscriptValue = ISO::CFI_index_t;
33using common::TypeCategory;
34
38static constexpr RT_API_ATTRS std::size_t MaxDescriptorSizeInBytes(
39 int rank, bool addendum = false, int lengthTypeParameters = 0) {
40 // Layout:
41 //
42 // fortran::runtime::Descriptor {
43 // ISO::CFI_cdesc_t {
44 // void *base_addr; (pointer -> up to 8 bytes)
45 // size_t elem_len; (up to 8 bytes)
46 // int version; (up to 4 bytes)
47 // CFI_rank_t rank; (unsigned char -> 1 byte)
48 // CFI_type_t type; (signed char -> 1 byte)
49 // CFI_attribute_t attribute; (unsigned char -> 1 byte)
50 // unsigned char extra; (1 byte)
51 // }
52 // }
53 // fortran::runtime::Dimension[rank] {
54 // ISO::CFI_dim_t {
55 // CFI_index_t lower_bound; (ptrdiff_t -> up to 8 bytes)
56 // CFI_index_t extent; (ptrdiff_t -> up to 8 bytes)
57 // CFI_index_t sm; (ptrdiff_t -> up to 8 bytes)
58 // }
59 // }
60 // fortran::runtime::DescriptorAddendum {
61 // const typeInfo::DerivedType *derivedType_; (pointer -> up to 8
62 // bytes) typeInfo::TypeParameterValue len_[lenParameters]; (int64_t -> 8
63 // bytes)
64 // }
65 std::size_t bytes{24u + rank * 24u};
66 if (addendum || lengthTypeParameters > 0) {
67 if (lengthTypeParameters < 1)
68 lengthTypeParameters = 1;
69 bytes += 8u + static_cast<std::size_t>(lengthTypeParameters) * 8u;
70 }
71 return bytes;
72}
73
74} // namespace Fortran::runtime
75
76#endif /* FORTRAN_RUNTIME_DESCRIPTOR_CONSTS_H_ */