1 //===- TypeAttrNamesTest.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 // This file test the lookup of AbstractType / AbstractAttribute by name.
11 //===----------------------------------------------------------------------===//
13 #include "mlir/IR/Attributes.h"
14 #include "mlir/IR/BuiltinTypes.h"
15 #include "mlir/IR/Dialect.h"
16 #include "mlir/IR/Types.h"
17 #include "mlir/IR/Value.h"
18 #include "mlir/Support/TypeID.h"
19 #include "gtest/gtest.h"
24 struct FooType
: Type::TypeBase
<FooType
, Type
, TypeStorage
> {
27 static constexpr StringLiteral name
= "fake.foo";
29 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FooType
)
32 struct BarAttr
: Attribute::AttrBase
<BarAttr
, Attribute
, AttributeStorage
> {
35 static constexpr StringLiteral name
= "fake.bar";
37 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(BarAttr
)
40 struct FakeDialect
: Dialect
{
41 FakeDialect(MLIRContext
*context
)
42 : Dialect(getDialectNamespace(), context
, TypeID::get
<FakeDialect
>()) {
44 addAttributes
<BarAttr
>();
47 static constexpr ::llvm::StringLiteral
getDialectNamespace() {
48 return ::llvm::StringLiteral("fake");
51 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FakeDialect
)
55 TEST(AbstractType
, LookupWithString
) {
57 ctx
.loadDialect
<FakeDialect
>();
59 // Check that we can lookup an abstract type by name.
60 auto fooAbstractType
= AbstractType::lookup("fake.foo", &ctx
);
61 EXPECT_TRUE(fooAbstractType
.has_value());
62 EXPECT_TRUE(fooAbstractType
->get().getName() == "fake.foo");
64 // Check that the abstract type is the same as the one used by the type.
65 auto fooType
= FooType::get(&ctx
);
66 EXPECT_TRUE(&fooType
.getAbstractType() == &fooAbstractType
->get());
68 // Check that lookups of non-existing types returns nullopt.
69 // Even if an attribute with the same name exists.
70 EXPECT_FALSE(AbstractType::lookup("fake.bar", &ctx
).has_value());
73 TEST(AbstractAttribute
, LookupWithString
) {
75 ctx
.loadDialect
<FakeDialect
>();
77 // Check that we can lookup an abstract type by name.
78 auto barAbstractAttr
= AbstractAttribute::lookup("fake.bar", &ctx
);
79 EXPECT_TRUE(barAbstractAttr
.has_value());
80 EXPECT_TRUE(barAbstractAttr
->get().getName() == "fake.bar");
82 // Check that the abstract Attribute is the same as the one used by the
84 auto barAttr
= BarAttr::get(&ctx
);
85 EXPECT_TRUE(&barAttr
.getAbstractAttribute() == &barAbstractAttr
->get());
87 // Check that lookups of non-existing Attributes returns nullopt.
88 // Even if an attribute with the same name exists.
89 EXPECT_FALSE(AbstractAttribute::lookup("fake.foo", &ctx
).has_value());