| 1 | //===-- Utils.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 | // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "flang/Optimizer/Support/Utils.h" |
| 14 | #include "flang/Optimizer/Dialect/FIROps.h" |
| 15 | #include "flang/Optimizer/Dialect/FIRType.h" |
| 16 | #include "flang/Optimizer/Support/InternalNames.h" |
| 17 | |
| 18 | fir::TypeInfoOp fir::lookupTypeInfoOp(fir::RecordType recordType, |
| 19 | mlir::ModuleOp module, |
| 20 | const mlir::SymbolTable *symbolTable) { |
| 21 | // fir.type_info was created with the mangled name of the derived type. |
| 22 | // It is the same as the name in the related fir.type, except when a pass |
| 23 | // lowered the fir.type (e.g., when lowering fir.boxproc type if the type has |
| 24 | // pointer procedure components), in which case suffix may have been added to |
| 25 | // the fir.type name. Get rid of them when looking up for the fir.type_info. |
| 26 | llvm::StringRef originalMangledTypeName = |
| 27 | fir::NameUniquer::dropTypeConversionMarkers(recordType.getName()); |
| 28 | return fir::lookupTypeInfoOp(originalMangledTypeName, module, symbolTable); |
| 29 | } |
| 30 | |
| 31 | fir::TypeInfoOp fir::lookupTypeInfoOp(llvm::StringRef name, |
| 32 | mlir::ModuleOp module, |
| 33 | const mlir::SymbolTable *symbolTable) { |
| 34 | if (symbolTable) |
| 35 | if (auto typeInfo = symbolTable->lookup<fir::TypeInfoOp>(name)) |
| 36 | return typeInfo; |
| 37 | return module.lookupSymbol<fir::TypeInfoOp>(name); |
| 38 | } |
| 39 | |
| 40 | std::optional<llvm::ArrayRef<int64_t>> fir::getComponentLowerBoundsIfNonDefault( |
| 41 | fir::RecordType recordType, llvm::StringRef component, |
| 42 | mlir::ModuleOp module, const mlir::SymbolTable *symbolTable) { |
| 43 | fir::TypeInfoOp typeInfo = |
| 44 | fir::lookupTypeInfoOp(recordType, module, symbolTable); |
| 45 | if (!typeInfo || typeInfo.getComponentInfo().empty()) |
| 46 | return std::nullopt; |
| 47 | for (auto componentInfo : |
| 48 | typeInfo.getComponentInfo().getOps<fir::DTComponentOp>()) |
| 49 | if (componentInfo.getName() == component) |
| 50 | return componentInfo.getLowerBounds(); |
| 51 | return std::nullopt; |
| 52 | } |
| 53 | |