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 TypeConstraint(const llvm::DefInit * init)22TypeConstraint::TypeConstraint(const llvm::DefInit *init) 23 : TypeConstraint(init->getDef()) {} 24 isOptional() const25bool TypeConstraint::isOptional() const { 26 return def->isSubClassOf("Optional"); 27 } 28 isVariadic() const29bool TypeConstraint::isVariadic() const { 30 return def->isSubClassOf("Variadic"); 31 } 32 isVariadicOfVariadic() const33bool TypeConstraint::isVariadicOfVariadic() const { 34 return def->isSubClassOf("VariadicOfVariadic"); 35 } 36 getVariadicOfVariadicSegmentSizeAttr() const37StringRef TypeConstraint::getVariadicOfVariadicSegmentSizeAttr() const { 38 assert(isVariadicOfVariadic()); 39 return def->getValueAsString("segmentAttrName"); 40 } 41 42 // Returns the builder call for this constraint if this is a buildable type, 43 // returns None otherwise. getBuilderCall() const44Optional<StringRef> TypeConstraint::getBuilderCall() const { 45 const llvm::Record *baseType = def; 46 if (isVariableLength()) 47 baseType = baseType->getValueAsDef("baseType"); 48 49 // Check to see if this type constraint has a builder call. 50 const llvm::RecordVal *builderCall = baseType->getValue("builderCall"); 51 if (!builderCall || !builderCall->getValue()) 52 return llvm::None; 53 return TypeSwitch<llvm::Init *, Optional<StringRef>>(builderCall->getValue()) 54 .Case<llvm::StringInit>([&](auto *init) { 55 StringRef value = init->getValue(); 56 return value.empty() ? Optional<StringRef>() : value; 57 }) 58 .Default([](auto *) { return llvm::None; }); 59 } 60 61 // Return the C++ class name for this type (which may just be ::mlir::Type). getCPPClassName() const62std::string TypeConstraint::getCPPClassName() const { 63 StringRef className = def->getValueAsString("cppClassName"); 64 65 // If the class name is already namespace resolved, use it. 66 if (className.contains("::")) 67 return className.str(); 68 69 // Otherwise, check to see if there is a namespace from a dialect to prepend. 70 if (const llvm::RecordVal *value = def->getValue("dialect")) { 71 Dialect dialect(cast<const llvm::DefInit>(value->getValue())->getDef()); 72 return (dialect.getCppNamespace() + "::" + className).str(); 73 } 74 return className.str(); 75 } 76 Type(const llvm::Record * record)77Type::Type(const llvm::Record *record) : TypeConstraint(record) {} 78 getDialect() const79Dialect Type::getDialect() const { 80 return Dialect(def->getValueAsDef("dialect")); 81 } 82