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 "llvm/TableGen/Record.h"
15 
16 using namespace mlir;
17 using namespace mlir::tblgen;
18 
19 TypeConstraint::TypeConstraint(const llvm::Record *record)
20     : Constraint(Constraint::CK_Type, record) {
21   assert(def->isSubClassOf("TypeConstraint") &&
22          "must be subclass of TableGen 'TypeConstraint' class");
23 }
24 
25 TypeConstraint::TypeConstraint(const llvm::DefInit *init)
26     : TypeConstraint(init->getDef()) {}
27 
28 bool TypeConstraint::isVariadic() const {
29   return def->isSubClassOf("Variadic");
30 }
31 
32 // Returns the builder call for this constraint if this is a buildable type,
33 // returns None otherwise.
34 Optional<StringRef> TypeConstraint::getBuilderCall() const {
35   const llvm::Record *baseType = def;
36   if (isVariadic())
37     baseType = baseType->getValueAsDef("baseType");
38 
39   if (!baseType->isSubClassOf("BuildableType"))
40     return None;
41   return baseType->getValueAsString("builderCall");
42 }
43 
44 Type::Type(const llvm::Record *record) : TypeConstraint(record) {}
45 
46 StringRef Type::getTypeDescription() const {
47   return def->getValueAsString("typeDescription");
48 }
49 
50 Dialect Type::getDialect() const {
51   return Dialect(def->getValueAsDef("dialect"));
52 }
53