2021-05-20 10:37:03 -07:00
|
|
|
//===-- runtime/allocatable.cpp -------------------------------------------===//
|
2020-03-30 16:37:30 -07:00
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2021-09-01 16:00:53 -07:00
|
|
|
#include "flang/Runtime/allocatable.h"
|
2023-01-13 20:40:51 +08:00
|
|
|
#include "assign.h"
|
2021-07-19 11:53:20 -07:00
|
|
|
#include "derived.h"
|
2020-11-10 15:13:02 -08:00
|
|
|
#include "stat.h"
|
2020-03-30 16:37:30 -07:00
|
|
|
#include "terminator.h"
|
2021-07-19 11:53:20 -07:00
|
|
|
#include "type-info.h"
|
2023-01-09 15:13:58 +00:00
|
|
|
#include "flang/ISO_Fortran_binding.h"
|
|
|
|
|
#include "flang/Runtime/assign.h"
|
|
|
|
|
#include "flang/Runtime/descriptor.h"
|
2020-03-30 16:37:30 -07:00
|
|
|
|
|
|
|
|
namespace Fortran::runtime {
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
2020-11-10 15:13:02 -08:00
|
|
|
void RTNAME(AllocatableInitIntrinsic)(Descriptor &descriptor,
|
|
|
|
|
TypeCategory category, int kind, int rank, int corank) {
|
|
|
|
|
INTERNAL_CHECK(corank == 0);
|
|
|
|
|
descriptor.Establish(TypeCode{category, kind},
|
|
|
|
|
Descriptor::BytesFor(category, kind), nullptr, rank, nullptr,
|
|
|
|
|
CFI_attribute_allocatable);
|
2020-03-30 16:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-10 15:13:02 -08:00
|
|
|
void RTNAME(AllocatableInitCharacter)(Descriptor &descriptor,
|
|
|
|
|
SubscriptValue length, int kind, int rank, int corank) {
|
|
|
|
|
INTERNAL_CHECK(corank == 0);
|
|
|
|
|
descriptor.Establish(
|
|
|
|
|
kind, length, nullptr, rank, nullptr, CFI_attribute_allocatable);
|
2020-03-30 16:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-10 15:13:02 -08:00
|
|
|
void RTNAME(AllocatableInitDerived)(Descriptor &descriptor,
|
2020-12-07 14:46:24 -08:00
|
|
|
const typeInfo::DerivedType &derivedType, int rank, int corank) {
|
2020-11-10 15:13:02 -08:00
|
|
|
INTERNAL_CHECK(corank == 0);
|
|
|
|
|
descriptor.Establish(
|
|
|
|
|
derivedType, nullptr, rank, nullptr, CFI_attribute_allocatable);
|
2020-03-30 16:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-09 15:13:58 +00:00
|
|
|
std::int32_t RTNAME(MoveAlloc)(Descriptor &to, Descriptor &from, bool hasStat,
|
|
|
|
|
const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
|
|
|
|
|
Terminator terminator{sourceFile, sourceLine};
|
|
|
|
|
// Should be handled by semantic analysis
|
|
|
|
|
RUNTIME_CHECK(terminator, to.type() == from.type());
|
|
|
|
|
RUNTIME_CHECK(terminator, to.IsAllocatable() && from.IsAllocatable());
|
|
|
|
|
|
|
|
|
|
// If to and from are the same allocatable they must not be allocated
|
|
|
|
|
// and nothing should be done.
|
|
|
|
|
if (from.raw().base_addr == to.raw().base_addr && from.IsAllocated()) {
|
|
|
|
|
return ReturnError(terminator, StatInvalidDescriptor, errMsg, hasStat);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (to.IsAllocated()) {
|
|
|
|
|
int stat{to.Destroy(/*finalize=*/true)};
|
|
|
|
|
if (stat != StatOk) {
|
|
|
|
|
return ReturnError(terminator, stat, errMsg, hasStat);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If from isn't allocated, the standard defines that nothing should be done.
|
|
|
|
|
if (from.IsAllocated()) {
|
|
|
|
|
to = from;
|
|
|
|
|
from.raw().base_addr = nullptr;
|
|
|
|
|
}
|
2020-11-10 15:13:02 -08:00
|
|
|
return StatOk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTNAME(AllocatableSetBounds)(Descriptor &descriptor, int zeroBasedDim,
|
|
|
|
|
SubscriptValue lower, SubscriptValue upper) {
|
|
|
|
|
INTERNAL_CHECK(zeroBasedDim >= 0 && zeroBasedDim < descriptor.rank());
|
|
|
|
|
descriptor.GetDimension(zeroBasedDim).SetBounds(lower, upper);
|
|
|
|
|
// The byte strides are computed when the object is allocated.
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 10:42:17 -07:00
|
|
|
void RTNAME(AllocatableSetDerivedLength)(
|
|
|
|
|
Descriptor &descriptor, int which, SubscriptValue x) {
|
|
|
|
|
DescriptorAddendum *addendum{descriptor.Addendum()};
|
|
|
|
|
INTERNAL_CHECK(addendum != nullptr);
|
|
|
|
|
addendum->SetLenParameterValue(which, x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTNAME(AllocatableApplyMold)(
|
|
|
|
|
Descriptor &descriptor, const Descriptor &mold) {
|
2023-01-17 15:51:04 +01:00
|
|
|
if (descriptor.IsAllocated()) {
|
|
|
|
|
// 9.7.1.3 Return so the error can be emitted by AllocatableAllocate.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-16 10:42:17 -07:00
|
|
|
descriptor = mold;
|
|
|
|
|
descriptor.set_base_addr(nullptr);
|
|
|
|
|
descriptor.raw().attribute = CFI_attribute_allocatable;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 15:13:02 -08:00
|
|
|
int RTNAME(AllocatableAllocate)(Descriptor &descriptor, bool hasStat,
|
2021-03-12 13:33:50 -08:00
|
|
|
const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
|
2020-11-10 15:13:02 -08:00
|
|
|
Terminator terminator{sourceFile, sourceLine};
|
|
|
|
|
if (!descriptor.IsAllocatable()) {
|
|
|
|
|
return ReturnError(terminator, StatInvalidDescriptor, errMsg, hasStat);
|
|
|
|
|
}
|
|
|
|
|
if (descriptor.IsAllocated()) {
|
|
|
|
|
return ReturnError(terminator, StatBaseNotNull, errMsg, hasStat);
|
|
|
|
|
}
|
2021-07-19 11:53:20 -07:00
|
|
|
int stat{ReturnError(terminator, descriptor.Allocate(), errMsg, hasStat)};
|
|
|
|
|
if (stat == StatOk) {
|
|
|
|
|
if (const DescriptorAddendum * addendum{descriptor.Addendum()}) {
|
|
|
|
|
if (const auto *derived{addendum->derivedType()}) {
|
|
|
|
|
if (!derived->noInitializationNeeded()) {
|
|
|
|
|
stat = Initialize(descriptor, *derived, terminator, hasStat, errMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return stat;
|
2020-03-30 16:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-13 20:40:51 +08:00
|
|
|
int RTNAME(AllocatableAllocateSource)(Descriptor &alloc,
|
|
|
|
|
const Descriptor &source, bool hasStat, const Descriptor *errMsg,
|
|
|
|
|
const char *sourceFile, int sourceLine) {
|
|
|
|
|
if (alloc.Elements() == 0) {
|
|
|
|
|
return StatOk;
|
|
|
|
|
}
|
|
|
|
|
int stat{RTNAME(AllocatableAllocate)(
|
|
|
|
|
alloc, hasStat, errMsg, sourceFile, sourceLine)};
|
|
|
|
|
if (stat == StatOk) {
|
|
|
|
|
Terminator terminator{sourceFile, sourceLine};
|
|
|
|
|
// 9.7.1.2(7)
|
|
|
|
|
Assign(alloc, source, terminator, /*skipRealloc=*/true);
|
|
|
|
|
}
|
|
|
|
|
return stat;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 15:13:02 -08:00
|
|
|
int RTNAME(AllocatableDeallocate)(Descriptor &descriptor, bool hasStat,
|
2021-03-12 13:33:50 -08:00
|
|
|
const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
|
2020-11-10 15:13:02 -08:00
|
|
|
Terminator terminator{sourceFile, sourceLine};
|
|
|
|
|
if (!descriptor.IsAllocatable()) {
|
|
|
|
|
return ReturnError(terminator, StatInvalidDescriptor, errMsg, hasStat);
|
|
|
|
|
}
|
|
|
|
|
if (!descriptor.IsAllocated()) {
|
|
|
|
|
return ReturnError(terminator, StatBaseNull, errMsg, hasStat);
|
|
|
|
|
}
|
2021-07-19 11:53:20 -07:00
|
|
|
return ReturnError(terminator, descriptor.Destroy(true), errMsg, hasStat);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 11:12:00 +01:00
|
|
|
int RTNAME(AllocatableDeallocatePolymorphic)(Descriptor &descriptor,
|
|
|
|
|
const typeInfo::DerivedType *derivedType, bool hasStat,
|
|
|
|
|
const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
|
|
|
|
|
int stat{RTNAME(AllocatableDeallocate)(
|
|
|
|
|
descriptor, hasStat, errMsg, sourceFile, sourceLine)};
|
|
|
|
|
if (stat == StatOk) {
|
|
|
|
|
DescriptorAddendum *addendum{descriptor.Addendum()};
|
|
|
|
|
INTERNAL_CHECK(addendum != nullptr);
|
|
|
|
|
addendum->set_derivedType(derivedType);
|
|
|
|
|
}
|
|
|
|
|
return stat;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 11:53:20 -07:00
|
|
|
void RTNAME(AllocatableDeallocateNoFinal)(
|
|
|
|
|
Descriptor &descriptor, const char *sourceFile, int sourceLine) {
|
|
|
|
|
Terminator terminator{sourceFile, sourceLine};
|
|
|
|
|
if (!descriptor.IsAllocatable()) {
|
|
|
|
|
ReturnError(terminator, StatInvalidDescriptor);
|
|
|
|
|
} else if (!descriptor.IsAllocated()) {
|
|
|
|
|
ReturnError(terminator, StatBaseNull);
|
|
|
|
|
} else {
|
|
|
|
|
ReturnError(terminator, descriptor.Destroy(false));
|
|
|
|
|
}
|
2020-03-30 16:37:30 -07:00
|
|
|
}
|
2021-07-16 10:42:17 -07:00
|
|
|
|
2023-01-13 20:40:51 +08:00
|
|
|
// TODO: AllocatableCheckLengthParameter
|
2020-03-30 16:37:30 -07:00
|
|
|
}
|
|
|
|
|
} // namespace Fortran::runtime
|