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 | |
25 | bool TypeConstraint::isOptional() const { |
26 | return def->isSubClassOf(Name: "Optional"); |
27 | } |
28 | |
29 | bool TypeConstraint::isVariadic() const { |
30 | return def->isSubClassOf(Name: "Variadic"); |
31 | } |
32 | |
33 | bool TypeConstraint::isVariadicOfVariadic() const { |
34 | return def->isSubClassOf(Name: "VariadicOfVariadic"); |
35 | } |
36 | |
37 | StringRef 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. |
44 | std::optional<StringRef> TypeConstraint::getBuilderCall() const { |
45 | const llvm::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 || !builderCall->getValue()) |
52 | return std::nullopt; |
53 | return TypeSwitch<llvm::Init *, std::optional<StringRef>>( |
54 | builderCall->getValue()) |
55 | .Case<llvm::StringInit>(caseFn: [&](auto *init) { |
56 | StringRef value = init->getValue(); |
57 | return value.empty() ? std::optional<StringRef>() : value; |
58 | }) |
59 | .Default(defaultFn: [](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(FieldName: "cppClassName"); |
65 | |
66 | // If the class name is already namespace resolved, use it. |
67 | if (className.contains(Other: "::")) |
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(Name: "dialect")) { |
72 | Dialect dialect(cast<const llvm::DefInit>(Val: 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(FieldName: "dialect")); |
82 | } |
83 |