| 1 |
//===-- ConvertType.cpp ---------------------------------------------------===// |
| 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 |
#include "flang/Lower/ConvertType.h" |
| 10 |
#include "flang/Lower/AbstractConverter.h" |
| 11 |
#include "flang/Lower/CallInterface.h" |
| 12 |
#include "flang/Lower/ConvertVariable.h" |
| 13 |
#include "flang/Lower/Mangler.h" |
| 14 |
#include "flang/Lower/PFTBuilder.h" |
| 15 |
#include "flang/Lower/Support/Utils.h" |
| 16 |
#include "flang/Optimizer/Builder/Todo.h" |
| 17 |
#include "flang/Optimizer/Dialect/FIRType.h" |
| 18 |
#include "flang/Semantics/tools.h" |
| 19 |
#include "flang/Semantics/type.h" |
| 20 |
#include "mlir/IR/Builders.h" |
| 21 |
#include "mlir/IR/BuiltinTypes.h" |
| 22 |
#include "llvm/Support/Debug.h" |
| 23 |
|
| 24 |
#define DEBUG_TYPE "flang-lower-type" |
| 25 |
|
| 26 |
using Fortran::common::VectorElementCategory; |
| 27 |
|
| 28 |
//===--------------------------------------------------------------------===// |
| 29 |
// Intrinsic type translation helpers |
| 30 |
//===--------------------------------------------------------------------===// |
| 31 |
|
| 32 |
static mlir::Type genRealType(mlir::MLIRContext *context, int kind) { |
| 33 |
if (Fortran::evaluate::IsValidKindOfIntrinsicType( |
| 34 |
Fortran::common::TypeCategory::Real, kind)) { |
| 35 |
switch (kind) { |
| 36 |
case 2: |
| 37 |
return mlir::FloatType::getF16(context); |
| 38 |
case 3: |
| 39 |
return mlir::FloatType::getBF16(context); |
| 40 |
case 4: |
| 41 |
return mlir::FloatType::getF32(context); |
| 42 |
case 8: |
| 43 |
return mlir::FloatType::getF64(context); |
| 44 |
case 10: |
| 45 |
return mlir::FloatType::getF80(context); |
| 46 |
case 16: |
| 47 |
return mlir::FloatType::getF128(context); |
| 48 |
} |
| 49 |
} |
| 50 |
llvm_unreachable("REAL type translation not implemented"); |
| 51 |
} |
| 52 |
|
| 53 |
template <int KIND> |
| 54 |
int getIntegerBits() { |
| 55 |
return Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer, |
| 56 |
KIND>::Scalar::bits; |
| 57 |
} |
| 58 |
static mlir::Type genIntegerType(mlir::MLIRContext *context, int kind, |
| 59 |
bool isUnsigned = false) { |
| 60 |
if (Fortran::evaluate::IsValidKindOfIntrinsicType( |
| 61 |
Fortran::common::TypeCategory::Integer, kind)) { |
| 62 |
mlir::IntegerType::SignednessSemantics signedness = |
| 63 |
(isUnsigned ? mlir::IntegerType::SignednessSemantics::Unsigned |
| 64 |
: mlir::IntegerType::SignednessSemantics::Signless); |
| 65 |
|
| 66 |
switch (kind) { |
| 67 |
case 1: |
| 68 |
return mlir::IntegerType::get(context, getIntegerBits<1>(), signedness); |
| 69 |
case 2: |
| 70 |
return mlir::IntegerType::get(context, getIntegerBits<2>(), signedness); |
| 71 |
case 4: |
| 72 |
return mlir::IntegerType::get(context, getIntegerBits<4>(), signedness); |
| 73 |
case 8: |
| 74 |
return mlir::IntegerType::get(context, getIntegerBits<8>(), signedness); |
| 75 |
case 16: |
| 76 |
return mlir::IntegerType::get(context, getIntegerBits<16>(), signedness); |
| 77 |
} |
| 78 |
} |
| 79 |
llvm_unreachable("INTEGER kind not translated"); |
| 80 |
} |
| 81 |
|
| 82 |
static mlir::Type genLogicalType(mlir::MLIRContext *context, int KIND) { |
| 83 |
if (Fortran::evaluate::IsValidKindOfIntrinsicType( |
| 84 |
Fortran::common::TypeCategory::Logical, KIND)) |
| 85 |
return fir::LogicalType::get(context, KIND); |
| 86 |
return {}; |
| 87 |
} |
| 88 |
|
| 89 |
static mlir::Type genCharacterType( |
| 90 |
mlir::MLIRContext *context, int KIND, |
| 91 |
Fortran::lower::LenParameterTy len = fir::CharacterType::unknownLen()) { |
| 92 |
if (Fortran::evaluate::IsValidKindOfIntrinsicType( |
| 93 |
Fortran::common::TypeCategory::Character, KIND)) |
| 94 |
return fir::CharacterType::get(context, KIND, len); |
| 95 |
return {}; |
| 96 |
} |
| 97 |
|
| 98 |
static mlir::Type genComplexType(mlir::MLIRContext *context, int KIND) { |
| 99 |
if (Fortran::evaluate::IsValidKindOfIntrinsicType( |
| 100 |
Fortran::common::TypeCategory::Complex, KIND)) |
| 101 |
return fir::ComplexType::get(context, KIND); |
| 102 |
return {}; |
| 103 |
} |
| 104 |
|
| 105 |
static mlir::Type |
| 106 |
genFIRType(mlir::MLIRContext *context, Fortran::common::TypeCategory tc, |
| 107 |
int kind, |
| 108 |
llvm::ArrayRef<Fortran::lower::LenParameterTy> lenParameters) { |
| 109 |
switch (tc) { |
| 110 |
case Fortran::common::TypeCategory::Real: |
| 111 |
return genRealType(context, kind); |
| 112 |
case Fortran::common::TypeCategory::Integer: |
| 113 |
return genIntegerType(context, kind); |
| 114 |
case Fortran::common::TypeCategory::Complex: |
| 115 |
return genComplexType(context, kind); |
| 116 |
case Fortran::common::TypeCategory::Logical: |
| 117 |
return genLogicalType(context, kind); |
| 118 |
case Fortran::common::TypeCategory::Character: |
| 119 |
if (!lenParameters.empty()) |
| 120 |
return genCharacterType(context, kind, lenParameters[0]); |
| 121 |
return genCharacterType(context, kind); |
| 122 |
default: |
| 123 |
break; |
| 124 |
} |
| 125 |
llvm_unreachable("unhandled type category"); |
| 126 |
} |
| 127 |
|
| 128 |
//===--------------------------------------------------------------------===// |
| 129 |
// Symbol and expression type translation |
| 130 |
//===--------------------------------------------------------------------===// |
| 131 |
|
| 132 |
/// TypeBuilderImpl translates expression and symbol type taking into account |
| 133 |
/// their shape and length parameters. For symbols, attributes such as |
| 134 |
/// ALLOCATABLE or POINTER are reflected in the fir type. |
| 135 |
/// It uses evaluate::DynamicType and evaluate::Shape when possible to |
| 136 |
/// avoid re-implementing type/shape analysis here. |
| 137 |
/// Do not use the FirOpBuilder from the AbstractConverter to get fir/mlir types |
| 138 |
/// since it is not guaranteed to exist yet when we lower types. |
| 139 |
namespace { |
| 140 |
struct TypeBuilderImpl { |
| 141 |
|
| 142 |
TypeBuilderImpl(Fortran::lower::AbstractConverter &converter) |
| 143 |
: derivedTypeInConstruction{converter.getTypeConstructionStack()}, |
| 144 |
converter{converter}, context{&converter.getMLIRContext()} {} |
| 145 |
|
| 146 |
template <typename A> |
| 147 |
mlir::Type genExprType(const A &expr) { |
| 148 |
std::optional<Fortran::evaluate::DynamicType> dynamicType = expr.GetType(); |
| 149 |
if (!dynamicType) |
| 150 |
return genTypelessExprType(expr); |
| 151 |
Fortran::common::TypeCategory category = dynamicType->category(); |
| 152 |
|
| 153 |
mlir::Type baseType; |
| 154 |
bool isPolymorphic = (dynamicType->IsPolymorphic() || |
| 155 |
dynamicType->IsUnlimitedPolymorphic()) && |
| 156 |
!dynamicType->IsAssumedType(); |
| 157 |
if (dynamicType->IsUnlimitedPolymorphic()) { |
| 158 |
baseType = mlir::NoneType::get(context); |
| 159 |
} else if (category == Fortran::common::TypeCategory::Derived) { |
| 160 |
baseType = genDerivedType(dynamicType->GetDerivedTypeSpec()); |
| 161 |
} else { |
| 162 |
// LOGICAL, INTEGER, REAL, COMPLEX, CHARACTER |
| 163 |
llvm::SmallVector<Fortran::lower::LenParameterTy> params; |
| 164 |
translateLenParameters(params, category, expr); |
| 165 |
baseType = genFIRType(context, category, dynamicType->kind(), params); |
| 166 |
} |
| 167 |
std::optional<Fortran::evaluate::Shape> shapeExpr = |
| 168 |
Fortran::evaluate::GetShape(converter.getFoldingContext(), expr); |
| 169 |
fir::SequenceType::Shape shape; |
| 170 |
if (shapeExpr) { |
| 171 |
translateShape(shape, std::move(*shapeExpr)); |
| 172 |
} else { |
| 173 |
// Shape static analysis cannot return something useful for the shape. |
| 174 |
// Use unknown extents. |
| 175 |
int rank = expr.Rank(); |
| 176 |
if (rank < 0) |
| 177 |
TODO(converter.getCurrentLocation(), "assumed rank expression types"); |
| 178 |
for (int dim = 0; dim < rank; ++dim) |
| 179 |
shape.emplace_back(fir::SequenceType::getUnknownExtent()); |
| 180 |
} |
| 181 |
|
| 182 |
if (!shape.empty()) { |
| 183 |
if (isPolymorphic) |
| 184 |
return fir::ClassType::get(fir::SequenceType::get(shape, baseType)); |
| 185 |
return fir::SequenceType::get(shape, baseType); |
| 186 |
} |
| 187 |
if (isPolymorphic) |
| 188 |
return fir::ClassType::get(baseType); |
| 189 |
return baseType; |
| 190 |
} |
| 191 |
|
| 192 |
template <typename A> |
| 193 |
void translateShape(A &shape, Fortran::evaluate::Shape &&shapeExpr) { |
| 194 |
for (Fortran::evaluate::MaybeExtentExpr extentExpr : shapeExpr) { |
| 195 |
fir::SequenceType::Extent extent = fir::SequenceType::getUnknownExtent(); |
| 196 |
if (std::optional<std::int64_t> constantExtent = |
| 197 |
toInt64(std::move(extentExpr))) |
| 198 |
extent = *constantExtent; |
| 199 |
shape.push_back(extent); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
template <typename A> |
| 204 |
std::optional<std::int64_t> toInt64(A &&expr) { |
| 205 |
return Fortran::evaluate::ToInt64(Fortran::evaluate::Fold( |
| 206 |
converter.getFoldingContext(), std::move(expr))); |
| 207 |
} |
| 208 |
|
| 209 |
template <typename A> |
| 210 |
mlir::Type genTypelessExprType(const A &expr) { |
| 211 |
fir::emitFatalError(converter.getCurrentLocation(), "not a typeless expr"); |
| 212 |
} |
| 213 |
|
| 214 |
mlir::Type genTypelessExprType(const Fortran::lower::SomeExpr &expr) { |
| 215 |
return Fortran::common::visit( |
| 216 |
Fortran::common::visitors{ |
| 217 |
[&](const Fortran::evaluate::BOZLiteralConstant &) -> mlir::Type { |
| 218 |
return mlir::NoneType::get(context); |
| 219 |
}, |
| 220 |
[&](const Fortran::evaluate::NullPointer &) -> mlir::Type { |
| 221 |
return fir::ReferenceType::get(mlir::NoneType::get(context)); |
| 222 |
}, |
| 223 |
[&](const Fortran::evaluate::ProcedureDesignator &proc) |
| 224 |
-> mlir::Type { |
| 225 |
return Fortran::lower::translateSignature(proc, converter); |
| 226 |
}, |
| 227 |
[&](const Fortran::evaluate::ProcedureRef &) -> mlir::Type { |
| 228 |
return mlir::NoneType::get(context); |
| 229 |
}, |
| 230 |
[](const auto &x) -> mlir::Type { |
| 231 |
using T = std::decay_t<decltype(x)>; |
| 232 |
static_assert(!Fortran::common::HasMember< |
| 233 |
T, Fortran::evaluate::TypelessExpression>, |
| 234 |
"missing typeless expr handling"); |
| 235 |
llvm::report_fatal_error("not a typeless expression"); |
| 236 |
}, |
| 237 |
}, |
| 238 |
expr.u); |
| 239 |
} |
| 240 |
|
| 241 |
mlir::Type genSymbolType(const Fortran::semantics::Symbol &symbol, |
| 242 |
bool isAlloc = false, bool isPtr = false) { |
| 243 |
mlir::Location loc = converter.genLocation(symbol.name()); |
| 244 |
mlir::Type ty; |
| 245 |
// If the symbol is not the same as the ultimate one (i.e, it is host or use |
| 246 |
// associated), all the symbol properties are the ones of the ultimate |
| 247 |
// symbol but the volatile and asynchronous attributes that may differ. To |
| 248 |
// avoid issues with helper functions that would not follow association |
| 249 |
// links, the fir type is built based on the ultimate symbol. This relies |
| 250 |
// on the fact volatile and asynchronous are not reflected in fir types. |
| 251 |
const Fortran::semantics::Symbol &ultimate = symbol.GetUltimate(); |
| 252 |
|
| 253 |
if (Fortran::semantics::IsProcedurePointer(ultimate)) { |
| 254 |
Fortran::evaluate::ProcedureDesignator proc(ultimate); |
| 255 |
auto procTy{Fortran::lower::translateSignature(proc, converter)}; |
| 256 |
return fir::BoxProcType::get(context, procTy); |
| 257 |
} |
| 258 |
|
| 259 |
if (const Fortran::semantics::DeclTypeSpec *type = ultimate.GetType()) { |
| 260 |
if (const Fortran::semantics::IntrinsicTypeSpec *tySpec = |
| 261 |
type->AsIntrinsic()) { |
| 262 |
int kind = toInt64(Fortran::common::Clone(tySpec->kind())).value(); |
| 263 |
llvm::SmallVector<Fortran::lower::LenParameterTy> params; |
| 264 |
translateLenParameters(params, tySpec->category(), ultimate); |
| 265 |
ty = genFIRType(context, tySpec->category(), kind, params); |
| 266 |
} else if (type->IsUnlimitedPolymorphic()) { |
| 267 |
ty = mlir::NoneType::get(context); |
| 268 |
} else if (const Fortran::semantics::DerivedTypeSpec *tySpec = |
| 269 |
type->AsDerived()) { |
| 270 |
ty = genDerivedType(*tySpec); |
| 271 |
} else { |
| 272 |
fir::emitFatalError(loc, "symbol's type must have a type spec"); |
| 273 |
} |
| 274 |
} else { |
| 275 |
fir::emitFatalError(loc, "symbol must have a type"); |
| 276 |
} |
| 277 |
bool isPolymorphic = (Fortran::semantics::IsPolymorphic(symbol) || |
| 278 |
Fortran::semantics::IsUnlimitedPolymorphic(symbol)) && |
| 279 |
!Fortran::semantics::IsAssumedType(symbol); |
| 280 |
if (ultimate.IsObjectArray()) { |
| 281 |
auto shapeExpr = |
| 282 |
Fortran::evaluate::GetShape(converter.getFoldingContext(), ultimate); |
| 283 |
fir::SequenceType::Shape shape; |
| 284 |
// If there is no shapExpr, this is an assumed-rank, and the empty shape |
| 285 |
// will build the desired fir.array<*:T> type. |
| 286 |
if (shapeExpr) |
| 287 |
translateShape(shape, std::move(*shapeExpr)); |
| 288 |
ty = fir::SequenceType::get(shape, ty); |
| 289 |
} |
| 290 |
if (Fortran::semantics::IsPointer(symbol)) |
| 291 |
return fir::wrapInClassOrBoxType(fir::PointerType::get(ty), |
| 292 |
isPolymorphic); |
| 293 |
if (Fortran::semantics::IsAllocatable(symbol)) |
| 294 |
return fir::wrapInClassOrBoxType(fir::HeapType::get(ty), isPolymorphic); |
| 295 |
// isPtr and isAlloc are variable that were promoted to be on the |
| 296 |
// heap or to be pointers, but they do not have Fortran allocatable |
| 297 |
// or pointer semantics, so do not use box for them. |
| 298 |
if (isPtr) |
| 299 |
return fir::PointerType::get(ty); |
| 300 |
if (isAlloc) |
| 301 |
return fir::HeapType::get(ty); |
| 302 |
if (isPolymorphic) |
| 303 |
return fir::ClassType::get(ty); |
| 304 |
return ty; |
| 305 |
} |
| 306 |
|
| 307 |
/// Does \p component has non deferred lower bounds that are not compile time |
| 308 |
/// constant 1. |
| 309 |
static bool componentHasNonDefaultLowerBounds( |
| 310 |
const Fortran::semantics::Symbol &component) { |
| 311 |
if (const auto *objDetails = |
| 312 |
component.detailsIf<Fortran::semantics::ObjectEntityDetails>()) |
| 313 |
for (const Fortran::semantics::ShapeSpec &bounds : objDetails->shape()) |
| 314 |
if (auto lb = bounds.lbound().GetExplicit()) |
| 315 |
if (auto constant = Fortran::evaluate::ToInt64(*lb)) |
| 316 |
if (!constant || *constant != 1) |
| 317 |
return true; |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
mlir::Type genVectorType(const Fortran::semantics::DerivedTypeSpec &tySpec) { |
| 322 |
assert(tySpec.scope() && "Missing scope for Vector type"); |
| 323 |
auto vectorSize{tySpec.scope()->size()}; |
| 324 |
switch (tySpec.category()) { |
| 325 |
SWITCH_COVERS_ALL_CASES |
| 326 |
case (Fortran::semantics::DerivedTypeSpec::Category::IntrinsicVector): { |
| 327 |
int64_t vecElemKind; |
| 328 |
int64_t vecElemCategory; |
| 329 |
|
| 330 |
for (const auto &pair : tySpec.parameters()) { |
| 331 |
if (pair.first == "element_category") { |
| 332 |
vecElemCategory = |
| 333 |
Fortran::evaluate::ToInt64(pair.second.GetExplicit()) |
| 334 |
.value_or(-1); |
| 335 |
} else if (pair.first == "element_kind") { |
| 336 |
vecElemKind = |
| 337 |
Fortran::evaluate::ToInt64(pair.second.GetExplicit()).value_or(0); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
assert((vecElemCategory >= 0 && |
| 342 |
static_cast<size_t>(vecElemCategory) < |
| 343 |
Fortran::common::VectorElementCategory_enumSize) && |
| 344 |
"Vector element type is not specified"); |
| 345 |
assert(vecElemKind && "Vector element kind is not specified"); |
| 346 |
|
| 347 |
int64_t numOfElements = vectorSize / vecElemKind; |
| 348 |
switch (static_cast<VectorElementCategory>(vecElemCategory)) { |
| 349 |
SWITCH_COVERS_ALL_CASES |
| 350 |
case VectorElementCategory::Integer: |
| 351 |
return fir::VectorType::get(numOfElements, |
| 352 |
genIntegerType(context, vecElemKind)); |
| 353 |
case VectorElementCategory::Unsigned: |
| 354 |
return fir::VectorType::get(numOfElements, |
| 355 |
genIntegerType(context, vecElemKind, true)); |
| 356 |
case VectorElementCategory::Real: |
| 357 |
return fir::VectorType::get(numOfElements, |
| 358 |
genRealType(context, vecElemKind)); |
| 359 |
} |
| 360 |
break; |
| 361 |
} |
| 362 |
case (Fortran::semantics::DerivedTypeSpec::Category::PairVector): |
| 363 |
case (Fortran::semantics::DerivedTypeSpec::Category::QuadVector): |
| 364 |
return fir::VectorType::get(vectorSize * 8, |
| 365 |
mlir::IntegerType::get(context, 1)); |
| 366 |
case (Fortran::semantics::DerivedTypeSpec::Category::DerivedType): |
| 367 |
Fortran::common::die("Vector element type not implemented"); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
mlir::Type genDerivedType(const Fortran::semantics::DerivedTypeSpec &tySpec) { |
| 372 |
std::vector<std::pair<std::string, mlir::Type>> ps; |
| 373 |
std::vector<std::pair<std::string, mlir::Type>> cs; |
| 374 |
if (tySpec.IsVectorType()) { |
| 375 |
return genVectorType(tySpec); |
| 376 |
} |
| 377 |
|
| 378 |
const Fortran::semantics::Symbol &typeSymbol = tySpec.typeSymbol(); |
| 379 |
const Fortran::semantics::Scope &derivedScope = DEREF(tySpec.GetScope()); |
| 380 |
if (mlir::Type ty = getTypeIfDerivedAlreadyInConstruction(derivedScope)) |
| 381 |
return ty; |
| 382 |
|
| 383 |
auto rec = fir::RecordType::get(context, converter.mangleName(tySpec)); |
| 384 |
// Maintain the stack of types for recursive references and to speed-up |
| 385 |
// the derived type constructions that can be expensive for derived type |
| 386 |
// with dozens of components/parents (modern Fortran). |
| 387 |
derivedTypeInConstruction.try_emplace(&derivedScope, rec); |
| 388 |
|
| 389 |
// Gather the record type fields. |
| 390 |
// (1) The data components. |
| 391 |
if (converter.getLoweringOptions().getLowerToHighLevelFIR()) { |
| 392 |
// In HLFIR the parent component is the first fir.type component. |
| 393 |
for (const auto &componentName : |
| 394 |
typeSymbol.get<Fortran::semantics::DerivedTypeDetails>() |
| 395 |
.componentNames()) { |
| 396 |
auto scopeIter = derivedScope.find(componentName); |
| 397 |
assert(scopeIter != derivedScope.cend() && |
| 398 |
"failed to find derived type component symbol"); |
| 399 |
const Fortran::semantics::Symbol &component = scopeIter->second.get(); |
| 400 |
mlir::Type ty = genSymbolType(component); |
| 401 |
cs.emplace_back(converter.getRecordTypeFieldName(component), ty); |
| 402 |
} |
| 403 |
} else { |
| 404 |
for (const auto &component : |
| 405 |
Fortran::semantics::OrderedComponentIterator(tySpec)) { |
| 406 |
// In the lowering to FIR the parent component does not appear in the |
| 407 |
// fir.type and its components are inlined at the beginning of the |
| 408 |
// fir.type<>. |
| 409 |
// FIXME: this strategy leads to bugs because padding should be inserted |
| 410 |
// after the component of the parents so that the next components do not |
| 411 |
// end-up in the parent storage if the sum of the parent's component |
| 412 |
// storage size is not a multiple of the parent type storage alignment. |
| 413 |
|
| 414 |
// Lowering is assuming non deferred component lower bounds are |
| 415 |
// always 1. Catch any situations where this is not true for now. |
| 416 |
if (componentHasNonDefaultLowerBounds(component)) |
| 417 |
TODO(converter.genLocation(component.name()), |
| 418 |
"derived type components with non default lower bounds"); |
| 419 |
if (IsProcedure(component)) |
| 420 |
TODO(converter.genLocation(component.name()), "procedure components"); |
| 421 |
mlir::Type ty = genSymbolType(component); |
| 422 |
// Do not add the parent component (component of the parents are |
| 423 |
// added and should be sufficient, the parent component would |
| 424 |
// duplicate the fields). Note that genSymbolType must be called above |
| 425 |
// on it so that the dispatch table for the parent type still gets |
| 426 |
// emitted as needed. |
| 427 |
if (component.test(Fortran::semantics::Symbol::Flag::ParentComp)) |
| 428 |
continue; |
| 429 |
cs.emplace_back(converter.getRecordTypeFieldName(component), ty); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
mlir::Location loc = converter.genLocation(typeSymbol.name()); |
| 434 |
// (2) The LEN type parameters. |
| 435 |
for (const auto ¶m : |
| 436 |
Fortran::semantics::OrderParameterDeclarations(typeSymbol)) |
| 437 |
if (param->get<Fortran::semantics::TypeParamDetails>().attr() == |
| 438 |
Fortran::common::TypeParamAttr::Len) { |
| 439 |
TODO(loc, "parameterized derived types"); |
| 440 |
// TODO: emplace in ps. Beware that param is the symbol in the type |
| 441 |
// declaration, not instantiation: its kind may not be a constant. |
| 442 |
// The instantiated symbol in tySpec.scope should be used instead. |
| 443 |
ps.emplace_back(param->name().ToString(), genSymbolType(*param)); |
| 444 |
} |
| 445 |
|
| 446 |
rec.finalize(ps, cs); |
| 447 |
|
| 448 |
if (!ps.empty()) { |
| 449 |
// TODO: this type is a PDT (parametric derived type) with length |
| 450 |
// parameter. Create the functions to use for allocation, dereferencing, |
| 451 |
// and address arithmetic here. |
| 452 |
} |
| 453 |
LLVM_DEBUG(llvm::dbgs() << "derived type: " << rec << '\n'); |
| 454 |
|
| 455 |
// Generate the type descriptor object if any |
| 456 |
if (const Fortran::semantics::Symbol *typeInfoSym = |
| 457 |
derivedScope.runtimeDerivedTypeDescription()) |
| 458 |
converter.registerTypeInfo(loc, *typeInfoSym, tySpec, rec); |
| 459 |
return rec; |
| 460 |
} |
| 461 |
|
| 462 |
// To get the character length from a symbol, make an fold a designator for |
| 463 |
// the symbol to cover the case where the symbol is an assumed length named |
| 464 |
// constant and its length comes from its init expression length. |
| 465 |
template <int Kind> |
| 466 |
fir::SequenceType::Extent |
| 467 |
getCharacterLengthHelper(const Fortran::semantics::Symbol &symbol) { |
| 468 |
using TC = |
| 469 |
Fortran::evaluate::Type<Fortran::common::TypeCategory::Character, Kind>; |
| 470 |
auto designator = Fortran::evaluate::Fold( |
| 471 |
converter.getFoldingContext(), |
| 472 |
Fortran::evaluate::Expr<TC>{Fortran::evaluate::Designator<TC>{symbol}}); |
| 473 |
if (auto len = toInt64(std::move(designator.LEN()))) |
| 474 |
return *len; |
| 475 |
return fir::SequenceType::getUnknownExtent(); |
| 476 |
} |
| 477 |
|
| 478 |
template <typename T> |
| 479 |
void translateLenParameters( |
| 480 |
llvm::SmallVectorImpl<Fortran::lower::LenParameterTy> ¶ms, |
| 481 |
Fortran::common::TypeCategory category, const T &exprOrSym) { |
| 482 |
if (category == Fortran::common::TypeCategory::Character) |
| 483 |
params.push_back(getCharacterLength(exprOrSym)); |
| 484 |
else if (category == Fortran::common::TypeCategory::Derived) |
| 485 |
TODO(converter.getCurrentLocation(), "derived type length parameters"); |
| 486 |
} |
| 487 |
Fortran::lower::LenParameterTy |
| 488 |
getCharacterLength(const Fortran::semantics::Symbol &symbol) { |
| 489 |
const Fortran::semantics::DeclTypeSpec *type = symbol.GetType(); |
| 490 |
if (!type || |
| 491 |
type->category() != Fortran::semantics::DeclTypeSpec::Character || |
| 492 |
!type->AsIntrinsic()) |
| 493 |
llvm::report_fatal_error("not a character symbol"); |
| 494 |
int kind = |
| 495 |
toInt64(Fortran::common::Clone(type->AsIntrinsic()->kind())).value(); |
| 496 |
switch (kind) { |
| 497 |
case 1: |
| 498 |
return getCharacterLengthHelper<1>(symbol); |
| 499 |
case 2: |
| 500 |
return getCharacterLengthHelper<2>(symbol); |
| 501 |
case 4: |
| 502 |
return getCharacterLengthHelper<4>(symbol); |
| 503 |
} |
| 504 |
llvm_unreachable("unknown character kind"); |
| 505 |
} |
| 506 |
|
| 507 |
template <typename A> |
| 508 |
Fortran::lower::LenParameterTy getCharacterLength(const A &expr) { |
| 509 |
return fir::SequenceType::getUnknownExtent(); |
| 510 |
} |
| 511 |
|
| 512 |
template <typename T> |
| 513 |
Fortran::lower::LenParameterTy |
| 514 |
getCharacterLength(const Fortran::evaluate::FunctionRef<T> &funcRef) { |
| 515 |
if (auto constantLen = toInt64(funcRef.LEN())) |
| 516 |
return *constantLen; |
| 517 |
return fir::SequenceType::getUnknownExtent(); |
| 518 |
} |
| 519 |
|
| 520 |
Fortran::lower::LenParameterTy |
| 521 |
getCharacterLength(const Fortran::lower::SomeExpr &expr) { |
| 522 |
// Do not use dynamic type length here. We would miss constant |
| 523 |
// lengths opportunities because dynamic type only has the length |
| 524 |
// if it comes from a declaration. |
| 525 |
if (const auto *charExpr = std::get_if< |
| 526 |
Fortran::evaluate::Expr<Fortran::evaluate::SomeCharacter>>( |
| 527 |
&expr.u)) { |
| 528 |
if (auto constantLen = toInt64(charExpr->LEN())) |
| 529 |
return *constantLen; |
| 530 |
} else if (auto dynamicType = expr.GetType()) { |
| 531 |
// When generating derived type type descriptor as structure constructor, |
| 532 |
// semantics wraps designators to data component initialization into |
| 533 |
// CLASS(*), regardless of their actual type. |
| 534 |
// GetType() will recover the actual symbol type as the dynamic type, so |
| 535 |
// getCharacterLength may be reached even if expr is packaged as an |
| 536 |
// Expr<SomeDerived> instead of an Expr<SomeChar>. |
| 537 |
// Just use the dynamic type here again to retrieve the length. |
| 538 |
if (auto constantLen = toInt64(dynamicType->GetCharLength())) |
| 539 |
return *constantLen; |
| 540 |
} |
| 541 |
return fir::SequenceType::getUnknownExtent(); |
| 542 |
} |
| 543 |
|
| 544 |
mlir::Type genVariableType(const Fortran::lower::pft::Variable &var) { |
| 545 |
return genSymbolType(var.getSymbol(), var.isHeapAlloc(), var.isPointer()); |
| 546 |
} |
| 547 |
|
| 548 |
/// Derived type can be recursive. That is, pointer components of a derived |
| 549 |
/// type `t` have type `t`. This helper returns `t` if it is already being |
| 550 |
/// lowered to avoid infinite loops. |
| 551 |
mlir::Type getTypeIfDerivedAlreadyInConstruction( |
| 552 |
const Fortran::semantics::Scope &derivedScope) const { |
| 553 |
return derivedTypeInConstruction.lookup(&derivedScope); |
| 554 |
} |
| 555 |
|
| 556 |
/// Stack derived type being processed to avoid infinite loops in case of |
| 557 |
/// recursive derived types. The depth of derived types is expected to be |
| 558 |
/// shallow (<10), so a SmallVector is sufficient. |
| 559 |
Fortran::lower::TypeConstructionStack &derivedTypeInConstruction; |
| 560 |
Fortran::lower::AbstractConverter &converter; |
| 561 |
mlir::MLIRContext *context; |
| 562 |
}; |
| 563 |
} // namespace |
| 564 |
|
| 565 |
mlir::Type Fortran::lower::getFIRType(mlir::MLIRContext *context, |
| 566 |
Fortran::common::TypeCategory tc, |
| 567 |
int kind, |
| 568 |
llvm::ArrayRef<LenParameterTy> params) { |
| 569 |
return genFIRType(context, tc, kind, params); |
| 570 |
} |
| 571 |
|
| 572 |
mlir::Type Fortran::lower::translateDerivedTypeToFIRType( |
| 573 |
Fortran::lower::AbstractConverter &converter, |
| 574 |
const Fortran::semantics::DerivedTypeSpec &tySpec) { |
| 575 |
return TypeBuilderImpl{converter}.genDerivedType(tySpec); |
| 576 |
} |
| 577 |
|
| 578 |
mlir::Type Fortran::lower::translateSomeExprToFIRType( |
| 579 |
Fortran::lower::AbstractConverter &converter, const SomeExpr &expr) { |
| 580 |
return TypeBuilderImpl{converter}.genExprType(expr); |
| 581 |
} |
| 582 |
|
| 583 |
mlir::Type Fortran::lower::translateSymbolToFIRType( |
| 584 |
Fortran::lower::AbstractConverter &converter, const SymbolRef symbol) { |
| 585 |
return TypeBuilderImpl{converter}.genSymbolType(symbol); |
| 586 |
} |
| 587 |
|
| 588 |
mlir::Type Fortran::lower::translateVariableToFIRType( |
| 589 |
Fortran::lower::AbstractConverter &converter, |
| 590 |
const Fortran::lower::pft::Variable &var) { |
| 591 |
return TypeBuilderImpl{converter}.genVariableType(var); |
| 592 |
} |
| 593 |
|
| 594 |
mlir::Type Fortran::lower::convertReal(mlir::MLIRContext *context, int kind) { |
| 595 |
return genRealType(context, kind); |
| 596 |
} |
| 597 |
|
| 598 |
bool Fortran::lower::isDerivedTypeWithLenParameters( |
| 599 |
const Fortran::semantics::Symbol &sym) { |
| 600 |
if (const Fortran::semantics::DeclTypeSpec *declTy = sym.GetType()) |
| 601 |
if (const Fortran::semantics::DerivedTypeSpec *derived = |
| 602 |
declTy->AsDerived()) |
| 603 |
return Fortran::semantics::CountLenParameters(*derived) > 0; |
| 604 |
return false; |
| 605 |
} |
| 606 |
|
| 607 |
template <typename T> |
| 608 |
mlir::Type Fortran::lower::TypeBuilder<T>::genType( |
| 609 |
Fortran::lower::AbstractConverter &converter, |
| 610 |
const Fortran::evaluate::FunctionRef<T> &funcRef) { |
| 611 |
return TypeBuilderImpl{converter}.genExprType(funcRef); |
| 612 |
} |
| 613 |
|
| 614 |
const Fortran::semantics::DerivedTypeSpec & |
| 615 |
Fortran::lower::ComponentReverseIterator::advanceToParentType() { |
| 616 |
const Fortran::semantics::Scope *scope = currentParentType->GetScope(); |
| 617 |
auto parentComp = |
| 618 |
DEREF(scope).find(currentTypeDetails->GetParentComponentName().value()); |
| 619 |
assert(parentComp != scope->cend() && "failed to get parent component"); |
| 620 |
setCurrentType(parentComp->second->GetType()->derivedTypeSpec()); |
| 621 |
return *currentParentType; |
| 622 |
} |
| 623 |
|
| 624 |
void Fortran::lower::ComponentReverseIterator::setCurrentType( |
| 625 |
const Fortran::semantics::DerivedTypeSpec &derived) { |
| 626 |
currentParentType = &derived; |
| 627 |
currentTypeDetails = ¤tParentType->typeSymbol() |
| 628 |
.get<Fortran::semantics::DerivedTypeDetails>(); |
| 629 |
componentIt = currentTypeDetails->componentNames().crbegin(); |
| 630 |
componentItEnd = currentTypeDetails->componentNames().crend(); |
| 631 |
} |
| 632 |
|
| 633 |
using namespace Fortran::evaluate; |
| 634 |
using namespace Fortran::common; |
| 635 |
FOR_EACH_SPECIFIC_TYPE(template class Fortran::lower::TypeBuilder, ) |