MLIR  14.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 None otherwise.
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 llvm::None;
53  return TypeSwitch<llvm::Init *, Optional<StringRef>>(builderCall->getValue())
54  .Case<llvm::StringInit>([&](auto *init) {
55  StringRef value = init->getValue();
56  return value.empty() ? Optional<StringRef>() : value;
57  })
58  .Default([](auto *) { return llvm::None; });
59 }
60 
61 // Return the C++ class name for this type (which may just be ::mlir::Type).
62 std::string TypeConstraint::getCPPClassName() const {
63  StringRef className = def->getValueAsString("cppClassName");
64 
65  // If the class name is already namespace resolved, use it.
66  if (className.contains("::"))
67  return className.str();
68 
69  // Otherwise, check to see if there is a namespace from a dialect to prepend.
70  if (const llvm::RecordVal *value = def->getValue("dialect")) {
71  Dialect dialect(cast<const llvm::DefInit>(value->getValue())->getDef());
72  return (dialect.getCppNamespace() + "::" + className).str();
73  }
74  return className.str();
75 }
76 
77 Type::Type(const llvm::Record *record) : TypeConstraint(record) {}
78 
79 StringRef Type::getDescription() const {
80  return def->getValueAsString("description");
81 }
82 
83 Dialect Type::getDialect() const {
84  return Dialect(def->getValueAsDef("dialect"));
85 }
Include the generated interface declarations.
bool isVariadicOfVariadic() const
Definition: Type.cpp:33
Type(const llvm::Record *record)
Definition: Type.cpp:77
static constexpr const bool value
StringRef getVariadicOfVariadicSegmentSizeAttr() const
Definition: Type.cpp:37
Optional< StringRef > getBuilderCall() const
Definition: Type.cpp:44
bool isVariadic() const
Definition: Type.cpp:29
Dialect getDialect() const
bool isOptional() const
Definition: Type.cpp:25
StringRef getDescription() const
Definition: Type.cpp:79
TypeConstraint(const llvm::DefInit *record)
Definition: Type.cpp:22
const llvm::Record * def
Definition: Constraint.h:61
bool isVariableLength() const
Definition: Type.h:53
std::string getCPPClassName() const
Definition: Type.cpp:62