| 1 |
//===-- HLFIRDialect.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/HLFIR/HLFIRDialect.h" |
| 14 |
#include "flang/Optimizer/HLFIR/HLFIROps.h" |
| 15 |
#include "mlir/IR/Builders.h" |
| 16 |
#include "mlir/IR/BuiltinTypes.h" |
| 17 |
#include "mlir/IR/DialectImplementation.h" |
| 18 |
#include "mlir/IR/Matchers.h" |
| 19 |
#include "mlir/IR/OpImplementation.h" |
| 20 |
#include "llvm/ADT/TypeSwitch.h" |
| 21 |
|
| 22 |
#include "flang/Optimizer/HLFIR/HLFIRDialect.cpp.inc" |
| 23 |
|
| 24 |
#define GET_TYPEDEF_CLASSES |
| 25 |
#include "flang/Optimizer/HLFIR/HLFIRTypes.cpp.inc" |
| 26 |
|
| 27 |
#define GET_ATTRDEF_CLASSES |
| 28 |
#include "flang/Optimizer/HLFIR/HLFIRAttributes.cpp.inc" |
| 29 |
|
| 30 |
void hlfir::hlfirDialect::initialize() { |
| 31 |
addTypes< |
| 32 |
#define GET_TYPEDEF_LIST |
| 33 |
#include "flang/Optimizer/HLFIR/HLFIRTypes.cpp.inc" |
| 34 |
>(); |
| 35 |
addOperations< |
| 36 |
#define GET_OP_LIST |
| 37 |
#include "flang/Optimizer/HLFIR/HLFIROps.cpp.inc" |
| 38 |
>(); |
| 39 |
} |
| 40 |
|
| 41 |
// `expr` `<` `*` | bounds (`x` bounds)* `:` type [`?`] `>` |
| 42 |
// bounds ::= `?` | int-lit |
| 43 |
mlir::Type hlfir::ExprType::parse(mlir::AsmParser &parser) { |
| 44 |
if (parser.parseLess()) |
| 45 |
return {}; |
| 46 |
ExprType::Shape shape; |
| 47 |
if (parser.parseOptionalStar()) { |
| 48 |
if (parser.parseDimensionList(shape, /*allowDynamic=*/true)) |
| 49 |
return {}; |
| 50 |
} else if (parser.parseColon()) { |
| 51 |
return {}; |
| 52 |
} |
| 53 |
mlir::Type eleTy; |
| 54 |
if (parser.parseType(eleTy)) |
| 55 |
return {}; |
| 56 |
const bool polymorphic = mlir::succeeded(parser.parseOptionalQuestion()); |
| 57 |
if (parser.parseGreater()) |
| 58 |
return {}; |
| 59 |
return ExprType::get(parser.getContext(), shape, eleTy, polymorphic); |
| 60 |
} |
| 61 |
|
| 62 |
void hlfir::ExprType::print(mlir::AsmPrinter &printer) const { |
| 63 |
auto shape = getShape(); |
| 64 |
printer << '<'; |
| 65 |
if (shape.size()) { |
| 66 |
for (const auto &b : shape) { |
| 67 |
if (b >= 0) |
| 68 |
printer << b << 'x'; |
| 69 |
else |
| 70 |
printer << "?x"; |
| 71 |
} |
| 72 |
} |
| 73 |
printer << getEleTy(); |
| 74 |
if (isPolymorphic()) |
| 75 |
printer << '?'; |
| 76 |
printer << '>'; |
| 77 |
} |
| 78 |
|
| 79 |
bool hlfir::isFortranVariableType(mlir::Type type) { |
| 80 |
return llvm::TypeSwitch<mlir::Type, bool>(type) |
| 81 |
.Case<fir::ReferenceType, fir::PointerType, fir::HeapType>([](auto p) { |
| 82 |
mlir::Type eleType = p.getEleTy(); |
| 83 |
return eleType.isa<fir::BaseBoxType>() || !fir::hasDynamicSize(eleType); |
| 84 |
}) |
| 85 |
.Case<fir::BaseBoxType, fir::BoxCharType>([](auto) { return true; }) |
| 86 |
.Default([](mlir::Type) { return false; }); |
| 87 |
} |
| 88 |
|
| 89 |
bool hlfir::isFortranScalarCharacterType(mlir::Type type) { |
| 90 |
return isFortranScalarCharacterExprType(type) || |
| 91 |
type.isa<fir::BoxCharType>() || |
| 92 |
fir::unwrapPassByRefType(fir::unwrapRefType(type)) |
| 93 |
.isa<fir::CharacterType>(); |
| 94 |
} |
| 95 |
|
| 96 |
bool hlfir::isFortranScalarCharacterExprType(mlir::Type type) { |
| 97 |
if (auto exprType = type.dyn_cast<hlfir::ExprType>()) |
| 98 |
return exprType.isScalar() && |
| 99 |
exprType.getElementType().isa<fir::CharacterType>(); |
| 100 |
return false; |
| 101 |
} |