brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.7 KiB · 8fb8c34 Raw
104 lines · cpp
1//===- Trait.cpp ----------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// Trait wrapper to simplify using TableGen Record defining a MLIR Trait.10//11//===----------------------------------------------------------------------===//12 13#include "mlir/TableGen/Trait.h"14#include "mlir/TableGen/Interfaces.h"15#include "mlir/TableGen/Predicate.h"16#include "llvm/TableGen/Error.h"17#include "llvm/TableGen/Record.h"18 19using namespace mlir;20using namespace mlir::tblgen;21 22//===----------------------------------------------------------------------===//23// Trait24//===----------------------------------------------------------------------===//25 26Trait Trait::create(const llvm::Init *init) {27  auto *def = cast<llvm::DefInit>(init)->getDef();28  if (def->isSubClassOf("PredTrait"))29    return Trait(Kind::Pred, def);30  if (def->isSubClassOf("GenInternalTrait"))31    return Trait(Kind::Internal, def);32  if (def->isSubClassOf("InterfaceTrait"))33    return Trait(Kind::Interface, def);34  assert(def->isSubClassOf("NativeTrait"));35  return Trait(Kind::Native, def);36}37 38Trait::Trait(Kind kind, const llvm::Record *def) : def(def), kind(kind) {}39 40//===----------------------------------------------------------------------===//41// NativeTrait42//===----------------------------------------------------------------------===//43 44std::string NativeTrait::getFullyQualifiedTraitName() const {45  llvm::StringRef trait = def->getValueAsString("trait");46  llvm::StringRef cppNamespace = def->getValueAsString("cppNamespace");47  return cppNamespace.empty() ? trait.str()48                              : (cppNamespace + "::" + trait).str();49}50 51bool NativeTrait::isStructuralOpTrait() const {52  return def->isSubClassOf("StructuralOpTrait");53}54 55StringRef NativeTrait::getExtraConcreteClassDeclaration() const {56  return def->getValueAsString("extraConcreteClassDeclaration");57}58 59StringRef NativeTrait::getExtraConcreteClassDefinition() const {60  return def->getValueAsString("extraConcreteClassDefinition");61}62 63//===----------------------------------------------------------------------===//64// InternalTrait65//===----------------------------------------------------------------------===//66 67llvm::StringRef InternalTrait::getFullyQualifiedTraitName() const {68  return def->getValueAsString("trait");69}70 71//===----------------------------------------------------------------------===//72// PredTrait73//===----------------------------------------------------------------------===//74 75std::string PredTrait::getPredTemplate() const {76  auto pred = Pred(def->getValueInit("predicate"));77  return pred.getCondition();78}79 80llvm::StringRef PredTrait::getSummary() const {81  return def->getValueAsString("summary");82}83 84//===----------------------------------------------------------------------===//85// InterfaceTrait86//===----------------------------------------------------------------------===//87 88Interface InterfaceTrait::getInterface() const { return Interface(def); }89 90std::string InterfaceTrait::getFullyQualifiedTraitName() const {91  llvm::StringRef trait = def->getValueAsString("trait");92  llvm::StringRef cppNamespace = def->getValueAsString("cppNamespace");93  return cppNamespace.empty() ? trait.str()94                              : (cppNamespace + "::" + trait).str();95}96 97bool InterfaceTrait::shouldDeclareMethods() const {98  return def->isSubClassOf("DeclareInterfaceMethods");99}100 101std::vector<StringRef> InterfaceTrait::getAlwaysDeclaredMethods() const {102  return def->getValueAsListOfStrings("alwaysOverriddenMethods");103}104