//===- TypeTest.cpp - Type API unit tests ---------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "mlir/IR/BuiltinTypes.h" #include "mlir/IR/Dialect.h" #include "mlir/IR/Types.h" #include "mlir/IR/Value.h" #include "gtest/gtest.h" using namespace mlir; /// Mock implementations of a Type hierarchy struct LeafType; struct MiddleType : Type::TypeBase { using Base::Base; static constexpr StringLiteral name = "test.middle"; static bool classof(Type ty) { return ty.getTypeID() == TypeID::get() || Base::classof(ty); } }; struct LeafType : Type::TypeBase { using Base::Base; static constexpr StringLiteral name = "test.leaf"; }; struct FakeDialect : Dialect { FakeDialect(MLIRContext *context) : Dialect(getDialectNamespace(), context, TypeID::get()) { addTypes(); } static constexpr ::llvm::StringLiteral getDialectNamespace() { return ::llvm::StringLiteral("fake"); } }; TEST(Type, Casting) { MLIRContext ctx; ctx.loadDialect(); Type intTy = IntegerType::get(&ctx, 8); Type nullTy; MiddleType middleTy = MiddleType::get(&ctx); MiddleType leafTy = LeafType::get(&ctx); Type leaf2Ty = LeafType::get(&ctx); EXPECT_TRUE(isa(intTy)); EXPECT_FALSE(isa(intTy)); EXPECT_FALSE(isa_and_present(nullTy)); EXPECT_TRUE(isa(middleTy)); EXPECT_FALSE(isa(middleTy)); EXPECT_TRUE(isa(leafTy)); EXPECT_TRUE(isa(leaf2Ty)); EXPECT_TRUE(isa(leafTy)); EXPECT_TRUE(static_cast(dyn_cast(intTy))); EXPECT_FALSE(static_cast(dyn_cast(intTy))); EXPECT_FALSE(static_cast(cast_if_present(nullTy))); EXPECT_FALSE(static_cast(dyn_cast_if_present(nullTy))); EXPECT_EQ(8u, cast(intTy).getWidth()); }