1bb63d249STyker //===- TypeTest.cpp - Type API unit tests ---------------------------------===//
2bb63d249STyker //
3bb63d249STyker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bb63d249STyker // See https://llvm.org/LICENSE.txt for license information.
5bb63d249STyker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bb63d249STyker //
7bb63d249STyker //===----------------------------------------------------------------------===//
8bb63d249STyker
90b7f05e1SMehdi Amini #include "mlir/IR/BuiltinTypes.h"
10bb63d249STyker #include "mlir/IR/Dialect.h"
11bb63d249STyker #include "mlir/IR/Types.h"
12bb63d249STyker #include "mlir/IR/Value.h"
13bb63d249STyker #include "gtest/gtest.h"
14bb63d249STyker
15bb63d249STyker using namespace mlir;
16bb63d249STyker
17bb63d249STyker /// Mock implementations of a Type hierarchy
18bb63d249STyker struct LeafType;
19bb63d249STyker
20bb63d249STyker struct MiddleType : Type::TypeBase<MiddleType, Type, TypeStorage> {
21bb63d249STyker using Base::Base;
22*3dbac2c0SFehr Mathieu
23*3dbac2c0SFehr Mathieu static constexpr StringLiteral name = "test.middle";
24*3dbac2c0SFehr Mathieu
classofMiddleType25bb63d249STyker static bool classof(Type ty) {
26bb63d249STyker return ty.getTypeID() == TypeID::get<LeafType>() || Base::classof(ty);
27bb63d249STyker }
28bb63d249STyker };
29bb63d249STyker
30bb63d249STyker struct LeafType : Type::TypeBase<LeafType, MiddleType, TypeStorage> {
31bb63d249STyker using Base::Base;
32*3dbac2c0SFehr Mathieu
33*3dbac2c0SFehr Mathieu static constexpr StringLiteral name = "test.leaf";
34bb63d249STyker };
35bb63d249STyker
36bb63d249STyker struct FakeDialect : Dialect {
FakeDialectFakeDialect37bb63d249STyker FakeDialect(MLIRContext *context)
38bb63d249STyker : Dialect(getDialectNamespace(), context, TypeID::get<FakeDialect>()) {
39bb63d249STyker addTypes<MiddleType, LeafType>();
40bb63d249STyker }
getDialectNamespaceFakeDialect41bb63d249STyker static constexpr ::llvm::StringLiteral getDialectNamespace() {
42bb63d249STyker return ::llvm::StringLiteral("fake");
43bb63d249STyker }
44bb63d249STyker };
45bb63d249STyker
TEST(Type,Casting)46bb63d249STyker TEST(Type, Casting) {
47bb63d249STyker MLIRContext ctx;
48bb63d249STyker ctx.loadDialect<FakeDialect>();
49bb63d249STyker
50bb63d249STyker Type intTy = IntegerType::get(&ctx, 8);
51bb63d249STyker Type nullTy;
52bb63d249STyker MiddleType middleTy = MiddleType::get(&ctx);
53bb63d249STyker MiddleType leafTy = LeafType::get(&ctx);
54bb63d249STyker Type leaf2Ty = LeafType::get(&ctx);
55bb63d249STyker
56bb63d249STyker EXPECT_TRUE(isa<IntegerType>(intTy));
57bb63d249STyker EXPECT_FALSE(isa<FunctionType>(intTy));
58923230d4SRahul Kayaith EXPECT_FALSE(isa_and_present<IntegerType>(nullTy));
59bb63d249STyker EXPECT_TRUE(isa<MiddleType>(middleTy));
60bb63d249STyker EXPECT_FALSE(isa<LeafType>(middleTy));
61bb63d249STyker EXPECT_TRUE(isa<MiddleType>(leafTy));
62bb63d249STyker EXPECT_TRUE(isa<LeafType>(leaf2Ty));
63bb63d249STyker EXPECT_TRUE(isa<LeafType>(leafTy));
64bb63d249STyker
65bb63d249STyker EXPECT_TRUE(static_cast<bool>(dyn_cast<IntegerType>(intTy)));
66bb63d249STyker EXPECT_FALSE(static_cast<bool>(dyn_cast<FunctionType>(intTy)));
67923230d4SRahul Kayaith EXPECT_FALSE(static_cast<bool>(cast_if_present<FunctionType>(nullTy)));
68923230d4SRahul Kayaith EXPECT_FALSE(static_cast<bool>(dyn_cast_if_present<IntegerType>(nullTy)));
69bb63d249STyker
70bb63d249STyker EXPECT_EQ(8u, cast<IntegerType>(intTy).getWidth());
71bb63d249STyker }
72