1 //===- Type.cpp - Type class ----------------------------------------------===//
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 // Type wrapper to simplify using TableGen Record defining a MLIR Type.
11 //===----------------------------------------------------------------------===//
13 #include "mlir/TableGen/Type.h"
14 #include "mlir/TableGen/Dialect.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/ADT/TypeSwitch.h"
17 #include "llvm/TableGen/Record.h"
20 using namespace mlir::tblgen
;
23 TypeConstraint::TypeConstraint(const llvm::DefInit
*init
)
24 : TypeConstraint(init
->getDef()) {}
26 bool TypeConstraint::isOptional() const {
27 return def
->isSubClassOf("Optional");
30 bool TypeConstraint::isVariadic() const {
31 return def
->isSubClassOf("Variadic");
34 bool TypeConstraint::isVariadicOfVariadic() const {
35 return def
->isSubClassOf("VariadicOfVariadic");
38 StringRef
TypeConstraint::getVariadicOfVariadicSegmentSizeAttr() const {
39 assert(isVariadicOfVariadic());
40 return def
->getValueAsString("segmentAttrName");
43 // Returns the builder call for this constraint if this is a buildable type,
44 // returns std::nullopt otherwise.
45 std::optional
<StringRef
> TypeConstraint::getBuilderCall() const {
46 const Record
*baseType
= def
;
47 if (isVariableLength())
48 baseType
= baseType
->getValueAsDef("baseType");
50 // Check to see if this type constraint has a builder call.
51 const llvm::RecordVal
*builderCall
= baseType
->getValue("builderCall");
52 if (!builderCall
|| !builderCall
->getValue())
54 return TypeSwitch
<const llvm::Init
*, std::optional
<StringRef
>>(
55 builderCall
->getValue())
56 .Case
<llvm::StringInit
>([&](auto *init
) {
57 StringRef value
= init
->getValue();
58 return value
.empty() ? std::optional
<StringRef
>() : value
;
60 .Default([](auto *) { return std::nullopt
; });
63 // Return the C++ type for this type (which may just be ::mlir::Type).
64 StringRef
TypeConstraint::getCppType() const {
65 return def
->getValueAsString("cppType");
68 Type::Type(const Record
*record
) : TypeConstraint(record
) {}
70 Dialect
Type::getDialect() const {
71 return Dialect(def
->getValueAsDef("dialect"));