| 1 |
//===- FIRTypesTest.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 "gtest/gtest.h" |
| 10 |
#include "flang/Optimizer/Dialect/FIRType.h" |
| 11 |
#include "flang/Optimizer/Support/InitFIR.h" |
| 12 |
|
| 13 |
struct FIRTypesTest : public testing::Test { |
| 14 |
public: |
| 15 |
void SetUp() { fir::support::loadDialects(context); } |
| 16 |
|
| 17 |
mlir::MLIRContext context; |
| 18 |
}; |
| 19 |
|
| 20 |
// Test fir::isPolymorphicType from flang/Optimizer/Dialect/FIRType.h. |
| 21 |
TEST_F(FIRTypesTest, isPolymorphicTypeTest) { |
| 22 |
mlir::Type noneTy = mlir::NoneType::get(&context); |
| 23 |
mlir::Type seqNoneTy = |
| 24 |
fir::SequenceType::get({fir::SequenceType::getUnknownExtent()}, noneTy); |
| 25 |
mlir::Type recTy = fir::RecordType::get(&context, "dt"); |
| 26 |
mlir::Type seqRecTy = |
| 27 |
fir::SequenceType::get({fir::SequenceType::getUnknownExtent()}, recTy); |
| 28 |
|
| 29 |
// CLASS(T) |
| 30 |
mlir::Type ty = fir::ClassType::get(recTy); |
| 31 |
EXPECT_TRUE(fir::isPolymorphicType(ty)); |
| 32 |
EXPECT_TRUE(fir::isPolymorphicType(fir::ReferenceType::get(ty))); |
| 33 |
|
| 34 |
// CLASS(T), DIMENSION(10) |
| 35 |
ty = fir::ClassType::get(fir::SequenceType::get({10}, recTy)); |
| 36 |
EXPECT_TRUE(fir::isPolymorphicType(ty)); |
| 37 |
|
| 38 |
// CLASS(T), DIMENSION(:) |
| 39 |
ty = fir::ClassType::get(seqRecTy); |
| 40 |
EXPECT_TRUE(fir::isPolymorphicType(ty)); |
| 41 |
|
| 42 |
// CLASS(T), ALLOCATABLE |
| 43 |
ty = fir::ClassType::get(fir::HeapType::get(recTy)); |
| 44 |
EXPECT_TRUE(fir::isPolymorphicType(ty)); |
| 45 |
|
| 46 |
// CLASS(T), ALLOCATABLE, DIMENSION(:) |
| 47 |
ty = fir::ClassType::get(fir::HeapType::get(seqRecTy)); |
| 48 |
EXPECT_TRUE(fir::isPolymorphicType(ty)); |
| 49 |
|
| 50 |
// CLASS(T), POINTER |
| 51 |
ty = fir::ClassType::get(fir::PointerType::get(recTy)); |
| 52 |
EXPECT_TRUE(fir::isPolymorphicType(ty)); |
| 53 |
|
| 54 |
// CLASS(T), POINTER, DIMENSIONS(:) |
| 55 |
ty = fir::ClassType::get(fir::PointerType::get(seqRecTy)); |
| 56 |
EXPECT_TRUE(fir::isPolymorphicType(ty)); |
| 57 |
|
| 58 |
// CLASS(*) |
| 59 |
ty = fir::ClassType::get(noneTy); |
| 60 |
EXPECT_TRUE(fir::isPolymorphicType(ty)); |
| 61 |
EXPECT_TRUE(fir::isPolymorphicType(fir::ReferenceType::get(ty))); |
| 62 |
|
| 63 |
// TYPE(*) |
| 64 |
EXPECT_TRUE(fir::isPolymorphicType(fir::BoxType::get(noneTy))); |
| 65 |
|
| 66 |
// TYPE(*), DIMENSION(:) |
| 67 |
EXPECT_TRUE(fir::isPolymorphicType(fir::BoxType::get(seqNoneTy))); |
| 68 |
|
| 69 |
// false tests |
| 70 |
EXPECT_FALSE(fir::isPolymorphicType(noneTy)); |
| 71 |
EXPECT_FALSE(fir::isPolymorphicType(seqNoneTy)); |
| 72 |
} |
| 73 |
|
| 74 |
// Test fir::isUnlimitedPolymorphicType from flang/Optimizer/Dialect/FIRType.h. |
| 75 |
TEST_F(FIRTypesTest, isUnlimitedPolymorphicTypeTest) { |
| 76 |
mlir::Type noneTy = mlir::NoneType::get(&context); |
| 77 |
mlir::Type seqNoneTy = |
| 78 |
fir::SequenceType::get({fir::SequenceType::getUnknownExtent()}, noneTy); |
| 79 |
|
| 80 |
// CLASS(*) |
| 81 |
mlir::Type ty = fir::ClassType::get(noneTy); |
| 82 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(ty)); |
| 83 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(fir::ReferenceType::get(ty))); |
| 84 |
|
| 85 |
// CLASS(*), DIMENSION(10) |
| 86 |
ty = fir::ClassType::get(fir::SequenceType::get({10}, noneTy)); |
| 87 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(ty)); |
| 88 |
|
| 89 |
// CLASS(*), DIMENSION(:) |
| 90 |
ty = fir::ClassType::get( |
| 91 |
fir::SequenceType::get({fir::SequenceType::getUnknownExtent()}, noneTy)); |
| 92 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(ty)); |
| 93 |
|
| 94 |
// CLASS(*), ALLOCATABLE |
| 95 |
ty = fir::ClassType::get(fir::HeapType::get(noneTy)); |
| 96 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(ty)); |
| 97 |
|
| 98 |
// CLASS(*), ALLOCATABLE, DIMENSION(:) |
| 99 |
ty = fir::ClassType::get(fir::HeapType::get(seqNoneTy)); |
| 100 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(ty)); |
| 101 |
|
| 102 |
// CLASS(*), POINTER |
| 103 |
ty = fir::ClassType::get(fir::PointerType::get(noneTy)); |
| 104 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(ty)); |
| 105 |
|
| 106 |
// CLASS(*), POINTER, DIMENSIONS(:) |
| 107 |
ty = fir::ClassType::get(fir::PointerType::get(seqNoneTy)); |
| 108 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(ty)); |
| 109 |
|
| 110 |
// TYPE(*) |
| 111 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(fir::BoxType::get(noneTy))); |
| 112 |
|
| 113 |
// TYPE(*), DIMENSION(:) |
| 114 |
EXPECT_TRUE(fir::isUnlimitedPolymorphicType(fir::BoxType::get(seqNoneTy))); |
| 115 |
|
| 116 |
// false tests |
| 117 |
EXPECT_FALSE(fir::isUnlimitedPolymorphicType(noneTy)); |
| 118 |
EXPECT_FALSE(fir::isUnlimitedPolymorphicType(seqNoneTy)); |
| 119 |
} |
| 120 |
|
| 121 |
// Test fir::isBoxedRecordType from flang/Optimizer/Dialect/FIRType.h. |
| 122 |
TEST_F(FIRTypesTest, isBoxedRecordType) { |
| 123 |
mlir::Type recTy = fir::RecordType::get(&context, "dt"); |
| 124 |
mlir::Type seqRecTy = |
| 125 |
fir::SequenceType::get({fir::SequenceType::getUnknownExtent()}, recTy); |
| 126 |
mlir::Type ty = fir::BoxType::get(recTy); |
| 127 |
EXPECT_TRUE(fir::isBoxedRecordType(ty)); |
| 128 |
EXPECT_TRUE(fir::isBoxedRecordType(fir::ReferenceType::get(ty))); |
| 129 |
|
| 130 |
// TYPE(T), ALLOCATABLE |
| 131 |
ty = fir::BoxType::get(fir::HeapType::get(recTy)); |
| 132 |
EXPECT_TRUE(fir::isBoxedRecordType(ty)); |
| 133 |
|
| 134 |
// TYPE(T), POINTER |
| 135 |
ty = fir::BoxType::get(fir::PointerType::get(recTy)); |
| 136 |
EXPECT_TRUE(fir::isBoxedRecordType(ty)); |
| 137 |
|
| 138 |
// TYPE(T), DIMENSION(10) |
| 139 |
ty = fir::BoxType::get(fir::SequenceType::get({10}, recTy)); |
| 140 |
EXPECT_TRUE(fir::isBoxedRecordType(ty)); |
| 141 |
|
| 142 |
// TYPE(T), DIMENSION(:) |
| 143 |
ty = fir::BoxType::get(seqRecTy); |
| 144 |
EXPECT_TRUE(fir::isBoxedRecordType(ty)); |
| 145 |
|
| 146 |
EXPECT_FALSE(fir::isBoxedRecordType(fir::BoxType::get( |
| 147 |
fir::ReferenceType::get(mlir::IntegerType::get(&context, 32))))); |
| 148 |
} |