1 //===- TypeTest.cpp - Type API unit tests ---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "mlir/IR/BuiltinTypes.h"
10 #include "mlir/IR/Dialect.h"
11 #include "mlir/IR/Types.h"
12 #include "mlir/IR/Value.h"
13 #include "gtest/gtest.h"
17 /// Mock implementations of a Type hierarchy
20 struct MiddleType
: Type::TypeBase
<MiddleType
, Type
, TypeStorage
> {
23 static constexpr StringLiteral name
= "test.middle";
25 static bool classof(Type ty
) {
26 return ty
.getTypeID() == TypeID::get
<LeafType
>() || Base::classof(ty
);
30 struct LeafType
: Type::TypeBase
<LeafType
, MiddleType
, TypeStorage
> {
33 static constexpr StringLiteral name
= "test.leaf";
36 struct FakeDialect
: Dialect
{
37 FakeDialect(MLIRContext
*context
)
38 : Dialect(getDialectNamespace(), context
, TypeID::get
<FakeDialect
>()) {
39 addTypes
<MiddleType
, LeafType
>();
41 static constexpr ::llvm::StringLiteral
getDialectNamespace() {
42 return ::llvm::StringLiteral("fake");
48 ctx
.loadDialect
<FakeDialect
>();
50 Type intTy
= IntegerType::get(&ctx
, 8);
52 MiddleType middleTy
= MiddleType::get(&ctx
);
53 MiddleType leafTy
= LeafType::get(&ctx
);
54 Type leaf2Ty
= LeafType::get(&ctx
);
56 EXPECT_TRUE(isa
<IntegerType
>(intTy
));
57 EXPECT_FALSE(isa
<FunctionType
>(intTy
));
58 EXPECT_FALSE(isa_and_present
<IntegerType
>(nullTy
));
59 EXPECT_TRUE(isa
<MiddleType
>(middleTy
));
60 EXPECT_FALSE(isa
<LeafType
>(middleTy
));
61 EXPECT_TRUE(isa
<MiddleType
>(leafTy
));
62 EXPECT_TRUE(isa
<LeafType
>(leaf2Ty
));
63 EXPECT_TRUE(isa
<LeafType
>(leafTy
));
65 EXPECT_TRUE(static_cast<bool>(dyn_cast
<IntegerType
>(intTy
)));
66 EXPECT_FALSE(static_cast<bool>(dyn_cast
<FunctionType
>(intTy
)));
67 EXPECT_FALSE(static_cast<bool>(cast_if_present
<FunctionType
>(nullTy
)));
68 EXPECT_FALSE(static_cast<bool>(dyn_cast_if_present
<IntegerType
>(nullTy
)));
70 EXPECT_EQ(8u, cast
<IntegerType
>(intTy
).getWidth());