1 //===-- Allocatable.h -- Allocatable statements lowering ------------------===// 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 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef FORTRAN_LOWER_ALLOCATABLE_H 14 #define FORTRAN_LOWER_ALLOCATABLE_H 15 16 #include "flang/Lower/AbstractConverter.h" 17 #include "flang/Optimizer/Builder/MutableBox.h" 18 #include "flang/Runtime/allocator-registry-consts.h" 19 #include "llvm/ADT/StringRef.h" 20 21 namespace mlir { 22 class Value; 23 class ValueRange; 24 class Location; 25 } // namespace mlir 26 27 namespace fir { 28 class FirOpBuilder; 29 } // namespace fir 30 31 namespace Fortran { 32 namespace parser { 33 struct AllocateStmt; 34 struct DeallocateStmt; 35 } // namespace parser 36 37 namespace semantics { 38 class Symbol; 39 class DerivedTypeSpec; 40 } // namespace semantics 41 42 namespace lower { 43 struct SymbolBox; 44 45 class StatementContext; 46 47 bool isArraySectionWithoutVectorSubscript(const SomeExpr &expr); 48 49 /// Lower an allocate statement to fir. 50 void genAllocateStmt(AbstractConverter &converter, 51 const parser::AllocateStmt &stmt, mlir::Location loc); 52 53 /// Lower a deallocate statement to fir. 54 void genDeallocateStmt(AbstractConverter &converter, 55 const parser::DeallocateStmt &stmt, mlir::Location loc); 56 57 void genDeallocateBox(AbstractConverter &converter, 58 const fir::MutableBoxValue &box, mlir::Location loc, 59 const Fortran::semantics::Symbol *sym = nullptr, 60 mlir::Value declaredTypeDesc = {}); 61 62 /// Deallocate an allocatable if it is allocated at the end of its lifetime. 63 void genDeallocateIfAllocated(AbstractConverter &converter, 64 const fir::MutableBoxValue &box, 65 mlir::Location loc, 66 const Fortran::semantics::Symbol *sym = nullptr); 67 68 /// Create a MutableBoxValue for an allocatable or pointer entity. 69 /// If the variables is a local variable that is not a dummy, it will be 70 /// initialized to unallocated/diassociated status. 71 fir::MutableBoxValue 72 createMutableBox(AbstractConverter &converter, mlir::Location loc, 73 const pft::Variable &var, mlir::Value boxAddr, 74 mlir::ValueRange nonDeferredParams, bool alwaysUseBox, 75 unsigned allocator = kDefaultAllocator); 76 77 /// Assign a boxed value to a boxed variable, \p box (known as a 78 /// MutableBoxValue). Expression \p source will be lowered to build the 79 /// assignment. If \p lbounds is not empty, it is used to define the result's 80 /// lower bounds. Otherwise, the lower bounds from \p source will be used. 81 void associateMutableBox(AbstractConverter &converter, mlir::Location loc, 82 const fir::MutableBoxValue &box, 83 const SomeExpr &source, mlir::ValueRange lbounds, 84 StatementContext &stmtCtx); 85 86 /// Is \p expr a reference to an entity with the ALLOCATABLE attribute? 87 bool isWholeAllocatable(const SomeExpr &expr); 88 89 /// Is \p expr a reference to an entity with the POINTER attribute? 90 bool isWholePointer(const SomeExpr &expr); 91 92 /// Read the length from \p box for an assumed length character allocatable or 93 /// pointer dummy argument given by \p sym. 94 mlir::Value getAssumedCharAllocatableOrPointerLen( 95 fir::FirOpBuilder &builder, mlir::Location loc, 96 const Fortran::semantics::Symbol &sym, mlir::Value box); 97 98 /// Retrieve the address of a type descriptor from its derived type spec. 99 mlir::Value 100 getTypeDescAddr(AbstractConverter &converter, mlir::Location loc, 101 const Fortran::semantics::DerivedTypeSpec &typeSpec); 102 103 } // namespace lower 104 } // namespace Fortran 105 106 #endif // FORTRAN_LOWER_ALLOCATABLE_H 107