18f5fa566SLei Zhang //===- Type.cpp - Type class ----------------------------------------------===// 2b2cc2c34SLei Zhang // 330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information. 556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6b2cc2c34SLei Zhang // 756222a06SMehdi Amini //===----------------------------------------------------------------------===// 8b2cc2c34SLei Zhang // 9b2cc2c34SLei Zhang // Type wrapper to simplify using TableGen Record defining a MLIR Type. 10b2cc2c34SLei Zhang // 11b2cc2c34SLei Zhang //===----------------------------------------------------------------------===// 12b2cc2c34SLei Zhang 13b2cc2c34SLei Zhang #include "mlir/TableGen/Type.h" 14c42cee0cSRiver Riddle #include "mlir/TableGen/Dialect.h" 15c42cee0cSRiver Riddle #include "llvm/ADT/Twine.h" 16ebf190fcSRiver Riddle #include "llvm/ADT/TypeSwitch.h" 17b2cc2c34SLei Zhang #include "llvm/TableGen/Record.h" 18b2cc2c34SLei Zhang 19b2cc2c34SLei Zhang using namespace mlir; 2027e8efedSJacques Pienaar using namespace mlir::tblgen; 21*659192b1SRahul Joshi using llvm::Record; 22b2cc2c34SLei Zhang 2327e8efedSJacques Pienaar TypeConstraint::TypeConstraint(const llvm::DefInit *init) 248f5fa566SLei Zhang : TypeConstraint(init->getDef()) {} 2544e9869fSAlex Zinenko 26aba1acc8SRiver Riddle bool TypeConstraint::isOptional() const { 27aba1acc8SRiver Riddle return def->isSubClassOf("Optional"); 28aba1acc8SRiver Riddle } 29aba1acc8SRiver Riddle 3027e8efedSJacques Pienaar bool TypeConstraint::isVariadic() const { 31509cd739SJacques Pienaar return def->isSubClassOf("Variadic"); 32509cd739SJacques Pienaar } 3327e8efedSJacques Pienaar 344e103a12SRiver Riddle bool TypeConstraint::isVariadicOfVariadic() const { 354e103a12SRiver Riddle return def->isSubClassOf("VariadicOfVariadic"); 364e103a12SRiver Riddle } 374e103a12SRiver Riddle 384e103a12SRiver Riddle StringRef TypeConstraint::getVariadicOfVariadicSegmentSizeAttr() const { 394e103a12SRiver Riddle assert(isVariadicOfVariadic()); 404e103a12SRiver Riddle return def->getValueAsString("segmentAttrName"); 414e103a12SRiver Riddle } 424e103a12SRiver Riddle 43b3a1d09cSRiver Riddle // Returns the builder call for this constraint if this is a buildable type, 4470c73d1bSKazu Hirata // returns std::nullopt otherwise. 453cfe412eSFangrui Song std::optional<StringRef> TypeConstraint::getBuilderCall() const { 46*659192b1SRahul Joshi const Record *baseType = def; 47aba1acc8SRiver Riddle if (isVariableLength()) 48b3a1d09cSRiver Riddle baseType = baseType->getValueAsDef("baseType"); 49b3a1d09cSRiver Riddle 50fbba6395SRiver Riddle // Check to see if this type constraint has a builder call. 51fbba6395SRiver Riddle const llvm::RecordVal *builderCall = baseType->getValue("builderCall"); 52fbba6395SRiver Riddle if (!builderCall || !builderCall->getValue()) 531a36588eSKazu Hirata return std::nullopt; 54e768b076SRahul Joshi return TypeSwitch<const llvm::Init *, std::optional<StringRef>>( 553cfe412eSFangrui Song builderCall->getValue()) 56415fab6fSPaul C. Anagnostopoulos .Case<llvm::StringInit>([&](auto *init) { 57fbba6395SRiver Riddle StringRef value = init->getValue(); 583cfe412eSFangrui Song return value.empty() ? std::optional<StringRef>() : value; 59fbba6395SRiver Riddle }) 601a36588eSKazu Hirata .Default([](auto *) { return std::nullopt; }); 61b3a1d09cSRiver Riddle } 62b3a1d09cSRiver Riddle 6335f55f53SMatthias Springer // Return the C++ type for this type (which may just be ::mlir::Type). 6435f55f53SMatthias Springer StringRef TypeConstraint::getCppType() const { 6535f55f53SMatthias Springer return def->getValueAsString("cppType"); 669eb3e564SChris Lattner } 679eb3e564SChris Lattner 68*659192b1SRahul Joshi Type::Type(const Record *record) : TypeConstraint(record) {} 6927e8efedSJacques Pienaar 7027e8efedSJacques Pienaar Dialect Type::getDialect() const { 7127e8efedSJacques Pienaar return Dialect(def->getValueAsDef("dialect")); 7227e8efedSJacques Pienaar } 73