FLANG
pointer.h
1//===-- include/flang/Runtime/pointer.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// Defines APIs for Fortran runtime library support of code generated
10// to manipulate and query data pointers.
11
12#ifndef FORTRAN_RUNTIME_POINTER_H_
13#define FORTRAN_RUNTIME_POINTER_H_
14
15#include "flang/Runtime/descriptor-consts.h"
16#include "flang/Runtime/entry-names.h"
17
18namespace Fortran::runtime {
19extern "C" {
20
21// Data pointer initialization for NULLIFY(), "p=>NULL()`, & for ALLOCATE().
22
23// Initializes a pointer to a disassociated state for NULLIFY() or "p=>NULL()".
24void RTDECL(PointerNullifyIntrinsic)(
25 Descriptor &, TypeCategory, int kind, int rank = 0, int corank = 0);
26void RTDECL(PointerNullifyCharacter)(Descriptor &, SubscriptValue length = 0,
27 int kind = 1, int rank = 0, int corank = 0);
28void RTDECL(PointerNullifyDerived)(
29 Descriptor &, const typeInfo::DerivedType &, int rank = 0, int corank = 0);
30
31// Explicitly sets the bounds of an initialized disassociated pointer.
32// The upper cobound is ignored for the last codimension.
33void RTDECL(PointerSetBounds)(
34 Descriptor &, int zeroBasedDim, SubscriptValue lower, SubscriptValue upper);
35void RTDECL(PointerSetCoBounds)(Descriptor &, int zeroBasedCoDim,
36 SubscriptValue lower, SubscriptValue upper = 0);
37
38// Length type parameters are indexed in declaration order; i.e., 0 is the
39// first length type parameter in the deepest base type. (Not for use
40// with CHARACTER; see above.)
41void RTDECL(PointerSetDerivedLength)(Descriptor &, int which, SubscriptValue);
42
43// For MOLD= allocation: acquires information from another descriptor
44// to initialize a null data pointer.
45void RTDECL(PointerApplyMold)(
46 Descriptor &, const Descriptor &mold, int rank = 0);
47
48// Data pointer association for "p=>TARGET"
49
50// Associates a scalar pointer with a simple scalar target.
51void RTDECL(PointerAssociateScalar)(Descriptor &, void *);
52
53// Associates a pointer with a target of the same rank, possibly with new lower
54// bounds, which are passed in a vector whose length must equal the rank.
55void RTDECL(PointerAssociate)(Descriptor &, const Descriptor &target);
56void RTDECL(PointerAssociateLowerBounds)(
57 Descriptor &, const Descriptor &target, const Descriptor &lowerBounds);
58
59// Associates a pointer with a target with bounds remapping. The target must be
60// simply contiguous &/or of rank 1. The bounds constitute a [2,newRank]
61// integer array whose columns are [lower bound, upper bound] on each dimension.
62// Use the Monomorphic form if the pointer's type shouldn't change and
63// the target is polymorphic.
64void RTDECL(PointerAssociateRemapping)(Descriptor &, const Descriptor &target,
65 const Descriptor &bounds, const char *sourceFile = nullptr,
66 int sourceLine = 0);
67void RTDECL(PointerAssociateRemappingMonomorphic)(Descriptor &,
68 const Descriptor &target, const Descriptor &bounds,
69 const char *sourceFile = nullptr, int sourceLine = 0);
70
71// Data pointer allocation and deallocation
72
73// When an explicit type-spec appears in an ALLOCATE statement for an
74// pointer with an explicit (non-deferred) length type paramater for
75// a derived type or CHARACTER value, the explicit value has to match
76// the length type parameter's value. This API checks that requirement.
77// Returns 0 for success, or the STAT= value on failure with hasStat==true.
78int RTDECL(PointerCheckLengthParameter)(Descriptor &,
79 int which /* 0 for CHARACTER length */, SubscriptValue other,
80 bool hasStat = false, const Descriptor *errMsg = nullptr,
81 const char *sourceFile = nullptr, int sourceLine = 0);
82
83// Allocates a data pointer. Its descriptor must have been initialized
84// and its bounds and length type parameters set. It need not be disassociated.
85// On failure, if hasStat is true, returns a nonzero error code for
86// STAT= and (if present) fills in errMsg; if hasStat is false, the
87// image is terminated. On success, leaves errMsg alone and returns zero.
88// Successfully allocated memory is initialized if the pointer has a
89// derived type, and is always initialized by PointerAllocateSource().
90// Performs all necessary coarray synchronization and validation actions.
91int RTDECL(PointerAllocate)(Descriptor &, bool hasStat = false,
92 const Descriptor *errMsg = nullptr, const char *sourceFile = nullptr,
93 int sourceLine = 0);
94int RTDECL(PointerAllocateSource)(Descriptor &, const Descriptor &source,
95 bool hasStat = false, const Descriptor *errMsg = nullptr,
96 const char *sourceFile = nullptr, int sourceLine = 0);
97
98// Deallocates a data pointer, which must have been allocated by
99// PointerAllocate(), possibly copied with PointerAssociate().
100// Finalizes elements &/or components as needed. The pointer is left
101// in an initialized disassociated state suitable for reallocation
102// with the same bounds, cobounds, and length type parameters.
103int RTDECL(PointerDeallocate)(Descriptor &, bool hasStat = false,
104 const Descriptor *errMsg = nullptr, const char *sourceFile = nullptr,
105 int sourceLine = 0);
106
107// Same as PointerDeallocate but also set the dynamic type as the declared type
108// as mentioned in 7.3.2.3 note 7.
109int RTDECL(PointerDeallocatePolymorphic)(Descriptor &,
110 const typeInfo::DerivedType *, bool hasStat = false,
111 const Descriptor *errMsg = nullptr, const char *sourceFile = nullptr,
112 int sourceLine = 0);
113
114// Association inquiries for ASSOCIATED()
115
116// True when the pointer is not disassociated.
117bool RTDECL(PointerIsAssociated)(const Descriptor &);
118
119// True when the pointer is associated with a specific target.
120bool RTDECL(PointerIsAssociatedWith)(
121 const Descriptor &, const Descriptor *target);
122
123// Fortran POINTERs are allocated with an extra validation word after their
124// payloads in order to detect erroneous deallocations later.
125RT_API_ATTRS void *AllocateValidatedPointerPayload(
126 std::size_t, int allocatorIdx = 0);
127RT_API_ATTRS bool ValidatePointerPayload(const ISO::CFI_cdesc_t &);
128
129} // extern "C"
130} // namespace Fortran::runtime
131#endif // FORTRAN_RUNTIME_POINTER_H_