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