[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / mlir / unittests / IR / TypeTest.cpp
blob1bb9d077d8ed596950017676422edc5eb44ebcd6
1 //===- TypeTest.cpp - Type API unit tests ---------------------------------===//
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 //===----------------------------------------------------------------------===//
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"
15 using namespace mlir;
17 /// Mock implementations of a Type hierarchy
18 struct LeafType;
20 struct MiddleType : Type::TypeBase<MiddleType, Type, TypeStorage> {
21 using Base::Base;
22 static bool classof(Type ty) {
23 return ty.getTypeID() == TypeID::get<LeafType>() || Base::classof(ty);
27 struct LeafType : Type::TypeBase<LeafType, MiddleType, TypeStorage> {
28 using Base::Base;
31 struct FakeDialect : Dialect {
32 FakeDialect(MLIRContext *context)
33 : Dialect(getDialectNamespace(), context, TypeID::get<FakeDialect>()) {
34 addTypes<MiddleType, LeafType>();
36 static constexpr ::llvm::StringLiteral getDialectNamespace() {
37 return ::llvm::StringLiteral("fake");
41 TEST(Type, Casting) {
42 MLIRContext ctx;
43 ctx.loadDialect<FakeDialect>();
45 Type intTy = IntegerType::get(&ctx, 8);
46 Type nullTy;
47 MiddleType middleTy = MiddleType::get(&ctx);
48 MiddleType leafTy = LeafType::get(&ctx);
49 Type leaf2Ty = LeafType::get(&ctx);
51 EXPECT_TRUE(isa<IntegerType>(intTy));
52 EXPECT_FALSE(isa<FunctionType>(intTy));
53 EXPECT_FALSE(isa_and_present<IntegerType>(nullTy));
54 EXPECT_TRUE(isa<MiddleType>(middleTy));
55 EXPECT_FALSE(isa<LeafType>(middleTy));
56 EXPECT_TRUE(isa<MiddleType>(leafTy));
57 EXPECT_TRUE(isa<LeafType>(leaf2Ty));
58 EXPECT_TRUE(isa<LeafType>(leafTy));
60 EXPECT_TRUE(static_cast<bool>(dyn_cast<IntegerType>(intTy)));
61 EXPECT_FALSE(static_cast<bool>(dyn_cast<FunctionType>(intTy)));
62 EXPECT_FALSE(static_cast<bool>(cast_if_present<FunctionType>(nullTy)));
63 EXPECT_FALSE(static_cast<bool>(dyn_cast_if_present<IntegerType>(nullTy)));
65 EXPECT_EQ(8u, cast<IntegerType>(intTy).getWidth());