MLIR  22.0.0git
Type.cpp
Go to the documentation of this file.
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 
18 using namespace mlir;
19 using namespace mlir::tblgen;
20 using llvm::Record;
21 
22 TypeConstraint::TypeConstraint(const llvm::DefInit *init)
23  : TypeConstraint(init->getDef()) {}
24 
26  return def->isSubClassOf("Optional");
27 }
28 
30  return def->isSubClassOf("Variadic");
31 }
32 
34  return def->isSubClassOf("VariadicOfVariadic");
35 }
36 
38  assert(isVariadicOfVariadic());
39  return def->getValueAsString("segmentAttrName");
40 }
41 
42 // Returns the builder call for this constraint if this is a buildable type,
43 // returns std::nullopt otherwise.
44 std::optional<StringRef> TypeConstraint::getBuilderCall() const {
45  const Record *baseType = def;
46  if (isVariableLength())
47  baseType = baseType->getValueAsDef("baseType");
48 
49  // Check to see if this type constraint has a builder call.
50  const llvm::RecordVal *builderCall = baseType->getValue("builderCall");
51  if (!builderCall || !builderCall->getValue())
52  return std::nullopt;
54  builderCall->getValue())
55  .Case<llvm::StringInit>([&](auto *init) {
56  StringRef value = init->getValue();
57  return value.empty() ? std::optional<StringRef>() : value;
58  })
59  .Default([](auto *) { return std::nullopt; });
60 }
61 
62 // Return the C++ type for this type (which may just be ::mlir::Type).
63 StringRef TypeConstraint::getCppType() const {
64  return def->getValueAsString("cppType");
65 }
66 
67 Type::Type(const Record *record) : TypeConstraint(record) {}
68 
69 Dialect Type::getDialect() const {
70  return Dialect(def->getValueAsDef("dialect"));
71 }
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
const llvm::Record * def
Definition: Constraint.h:90
TypeConstraint(const llvm::DefInit *record)
Definition: Type.cpp:22
StringRef getVariadicOfVariadicSegmentSizeAttr() const
Definition: Type.cpp:37
bool isVariadic() const
Definition: Type.cpp:29
StringRef getCppType() const
Definition: Type.cpp:63
bool isOptional() const
Definition: Type.cpp:25
bool isVariableLength() const
Definition: Type.h:53
std::optional< StringRef > getBuilderCall() const
Definition: Type.cpp:44
bool isVariadicOfVariadic() const
Definition: Type.cpp:33
Dialect getDialect() const
Type(const llvm::Record *record)
Include the generated interface declarations.