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/ADT/TypeSwitch.h"
15 #include "llvm/TableGen/Record.h"
16 
17 using namespace mlir;
18 using namespace mlir::tblgen;
19 
20 TypeConstraint::TypeConstraint(const llvm::Record *record)
21     : Constraint(Constraint::CK_Type, record) {
22   assert(def->isSubClassOf("TypeConstraint") &&
23          "must be subclass of TableGen 'TypeConstraint' class");
24 }
25 
26 TypeConstraint::TypeConstraint(const llvm::DefInit *init)
27     : TypeConstraint(init->getDef()) {}
28 
29 bool TypeConstraint::isVariadic() const {
30   return def->isSubClassOf("Variadic");
31 }
32 
33 // Returns the builder call for this constraint if this is a buildable type,
34 // returns None otherwise.
35 Optional<StringRef> TypeConstraint::getBuilderCall() const {
36   const llvm::Record *baseType = def;
37   if (isVariadic())
38     baseType = baseType->getValueAsDef("baseType");
39 
40   // Check to see if this type constraint has a builder call.
41   const llvm::RecordVal *builderCall = baseType->getValue("builderCall");
42   if (!builderCall || !builderCall->getValue())
43     return llvm::None;
44   return TypeSwitch<llvm::Init *, Optional<StringRef>>(builderCall->getValue())
45       .Case<llvm::StringInit, llvm::CodeInit>([&](auto *init) {
46         StringRef value = init->getValue();
47         return value.empty() ? Optional<StringRef>() : value;
48       })
49       .Default([](auto *) { return llvm::None; });
50 }
51 
52 Type::Type(const llvm::Record *record) : TypeConstraint(record) {}
53 
54 StringRef Type::getTypeDescription() const {
55   return def->getValueAsString("typeDescription");
56 }
57 
58 Dialect Type::getDialect() const {
59   return Dialect(def->getValueAsDef("dialect"));
60 }
61