FLANG
array-constructor-consts.h
1//===-- include/flang/Runtime/array-constructor-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_ARRAY_CONSTRUCTOR_CONSTS_H_
10#define FORTRAN_RUNTIME_ARRAY_CONSTRUCTOR_CONSTS_H_
11
12#include "flang/Runtime/descriptor-consts.h"
13#include "flang/Runtime/entry-names.h"
14
15namespace Fortran::runtime {
16struct ArrayConstructorVector;
17
18// Max sizeof(ArrayConstructorVector) and sizeof(ArrayConstructorVector) for any
19// target.
20// TODO: Use target-specific size/alignment instead of overapproximation.
21constexpr std::size_t MaxArrayConstructorVectorSizeInBytes = 2 * 40;
22constexpr std::size_t MaxArrayConstructorVectorAlignInBytes = 8;
23
24// This file defines an API to "push" an evaluated array constructor value
25// "from" into some storage "to" of an array constructor. It can be seen as a
26// form of std::vector::push_back() implementation for Fortran array
27// constructors. In the APIs and ArrayConstructorVector struct above:
28//
29// - "to" is a ranked-1 descriptor whose declared type is already set to the
30// array constructor derived type. It may be already allocated, even before the
31// first call to this API, or it may be unallocated. "to" extent is increased
32// every time a "from" is pushed past its current extent. At this end of the
33// API calls, its extent is the extent of the array constructor. If "to" is
34// unallocated and its extent is not null, it is assumed this is the final array
35// constructor extent value, and the first allocation already "reserves" storage
36// space accordingly to avoid reallocations.
37// - "from" is a scalar or array descriptor for the evaluated array
38// constructor value that must be copied into the storage of "to" at
39// "nextValuePosition".
40// - "useValueLengthParameters" must be set to true if the array constructor
41// has length parameters and no type spec. If it is true and "to" is
42// unallocated, "to" will take the length parameters of "from". If it is true
43// and "to" is an allocated character array constructor, it will be checked
44// that "from" length matches the one from "to". When it is false, the
45// character length must already be set in "to" before the first call to this
46// API and "from" character lengths are allowed to mismatch from "to".
47// - "nextValuePosition" is the zero based sequence position of "from" in the
48// array constructor. It is updated after this call by the number of "from"
49// elements. It should be set to zero by the caller of this API before the first
50// call.
51// - "actualAllocationSize" is the current allocation size of "to" storage. It
52// may be bigger than "to" extent for reallocation optimization purposes, but
53// should never be smaller, unless this is the first call and "to" is
54// unallocated. It is updated by the runtime after each successful allocation or
55// reallocation. It should be set to "to" extent if "to" is allocated before the
56// first call of this API, and can be left undefined otherwise.
57//
58// Note that this API can be used with "to" being a variable (that can be
59// discontiguous). This can be done when the variable is the left hand side of
60// an assignment from an array constructor as long as:
61// - none of the ac-value overlaps with the variable,
62// - this is an intrinsic assignment that is not a whole allocatable
63// assignment, *and* for a type that has no components requiring user defined
64// assignments,
65// - the variable is properly finalized before using this API if its need to,
66// - "useValueLengthParameters" should be set to false in this case, even if
67// the array constructor has no type-spec, since the variable may have a
68// different character length than the array constructor values.
69
70extern "C" {
71// API to initialize an ArrayConstructorVector before any values are pushed to
72// it. Inlined code is only expected to allocate the "ArrayConstructorVector"
73// class instance storage with sufficient size
74// (MaxArrayConstructorVectorSizeInBytes is expected to be large enough for all
75// supported targets). This avoids the need for the runtime to maintain a state,
76// or to use dynamic allocation for it.
77void RTDECL(InitArrayConstructorVector)(ArrayConstructorVector &vector,
78 Descriptor &to, bool useValueLengthParameters,
79 const char *sourceFile = nullptr, int sourceLine = 0);
80
81// Generic API to push any kind of entity into the array constructor (any
82// Fortran type and any rank).
83void RTDECL(PushArrayConstructorValue)(
84 ArrayConstructorVector &vector, const Descriptor &from);
85
86// API to push scalar array constructor value of:
87// - a numerical or logical type,
88// - or a derived type that has no length parameters, and no allocatable
89// component (that would require deep copies).
90// It requires no descriptor for the value that is passed via its base address.
91void RTDECL(PushArrayConstructorSimpleScalar)(
92 ArrayConstructorVector &vector, void *from);
93} // extern "C"
94} // namespace Fortran::runtime
95
96#endif /* FORTRAN_RUNTIME_ARRAY_CONSTRUCTOR_CONSTS_H_ */