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