MLIR  19.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/Twine.h"
16 #include "llvm/ADT/TypeSwitch.h"
17 #include "llvm/TableGen/Record.h"
18 
19 using namespace mlir;
20 using namespace mlir::tblgen;
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 llvm::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++ class name for this type (which may just be ::mlir::Type).
63 std::string TypeConstraint::getCPPClassName() const {
64  StringRef className = def->getValueAsString("cppClassName");
65 
66  // If the class name is already namespace resolved, use it.
67  if (className.contains("::"))
68  return className.str();
69 
70  // Otherwise, check to see if there is a namespace from a dialect to prepend.
71  if (const llvm::RecordVal *value = def->getValue("dialect")) {
72  Dialect dialect(cast<const llvm::DefInit>(value->getValue())->getDef());
73  return (dialect.getCppNamespace() + "::" + className).str();
74  }
75  return className.str();
76 }
77 
78 Type::Type(const llvm::Record *record) : TypeConstraint(record) {}
79 
80 Dialect Type::getDialect() const {
81  return Dialect(def->getValueAsDef("dialect"));
82 }
const llvm::Record * def
Definition: Constraint.h:79
StringRef getCppNamespace() const
Definition: Dialect.cpp:28
TypeConstraint(const llvm::DefInit *record)
Definition: Type.cpp:22
StringRef getVariadicOfVariadicSegmentSizeAttr() const
Definition: Type.cpp:37
bool isVariadic() const
Definition: Type.cpp:29
bool isOptional() const
Definition: Type.cpp:25
bool isVariableLength() const
Definition: Type.h:53
std::string getCPPClassName() const
Definition: Type.cpp:63
std::optional< StringRef > getBuilderCall() const
Definition: Type.cpp:44
bool isVariadicOfVariadic() const
Definition: Type.cpp:33
Dialect getDialect() const
Type(const llvm::Record *record)
Definition: Type.cpp:78
Include the generated interface declarations.