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