• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- TypeDef.cpp - TypeDef wrapper 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 // TypeDef wrapper to simplify using TableGen Record defining a MLIR dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/TableGen/TypeDef.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/TableGen/Error.h"
16 #include "llvm/TableGen/Record.h"
17 
18 using namespace mlir;
19 using namespace mlir::tblgen;
20 
getDialect() const21 Dialect TypeDef::getDialect() const {
22   auto *dialectDef =
23       dyn_cast<llvm::DefInit>(def->getValue("dialect")->getValue());
24   if (dialectDef == nullptr)
25     return Dialect(nullptr);
26   return Dialect(dialectDef->getDef());
27 }
28 
getName() const29 StringRef TypeDef::getName() const { return def->getName(); }
getCppClassName() const30 StringRef TypeDef::getCppClassName() const {
31   return def->getValueAsString("cppClassName");
32 }
33 
hasDescription() const34 bool TypeDef::hasDescription() const {
35   const llvm::RecordVal *s = def->getValue("description");
36   return s != nullptr && isa<llvm::StringInit>(s->getValue());
37 }
38 
getDescription() const39 StringRef TypeDef::getDescription() const {
40   return def->getValueAsString("description");
41 }
42 
hasSummary() const43 bool TypeDef::hasSummary() const {
44   const llvm::RecordVal *s = def->getValue("summary");
45   return s != nullptr && isa<llvm::StringInit>(s->getValue());
46 }
47 
getSummary() const48 StringRef TypeDef::getSummary() const {
49   return def->getValueAsString("summary");
50 }
51 
getStorageClassName() const52 StringRef TypeDef::getStorageClassName() const {
53   return def->getValueAsString("storageClass");
54 }
getStorageNamespace() const55 StringRef TypeDef::getStorageNamespace() const {
56   return def->getValueAsString("storageNamespace");
57 }
58 
genStorageClass() const59 bool TypeDef::genStorageClass() const {
60   return def->getValueAsBit("genStorageClass");
61 }
hasStorageCustomConstructor() const62 bool TypeDef::hasStorageCustomConstructor() const {
63   return def->getValueAsBit("hasStorageCustomConstructor");
64 }
getParameters(SmallVectorImpl<TypeParameter> & parameters) const65 void TypeDef::getParameters(SmallVectorImpl<TypeParameter> &parameters) const {
66   auto *parametersDag = def->getValueAsDag("parameters");
67   if (parametersDag != nullptr) {
68     size_t numParams = parametersDag->getNumArgs();
69     for (unsigned i = 0; i < numParams; i++)
70       parameters.push_back(TypeParameter(parametersDag, i));
71   }
72 }
getNumParameters() const73 unsigned TypeDef::getNumParameters() const {
74   auto *parametersDag = def->getValueAsDag("parameters");
75   return parametersDag ? parametersDag->getNumArgs() : 0;
76 }
getMnemonic() const77 llvm::Optional<StringRef> TypeDef::getMnemonic() const {
78   return def->getValueAsOptionalString("mnemonic");
79 }
getPrinterCode() const80 llvm::Optional<StringRef> TypeDef::getPrinterCode() const {
81   return def->getValueAsOptionalString("printer");
82 }
getParserCode() const83 llvm::Optional<StringRef> TypeDef::getParserCode() const {
84   return def->getValueAsOptionalString("parser");
85 }
genAccessors() const86 bool TypeDef::genAccessors() const {
87   return def->getValueAsBit("genAccessors");
88 }
genVerifyInvariantsDecl() const89 bool TypeDef::genVerifyInvariantsDecl() const {
90   return def->getValueAsBit("genVerifyInvariantsDecl");
91 }
getExtraDecls() const92 llvm::Optional<StringRef> TypeDef::getExtraDecls() const {
93   auto value = def->getValueAsString("extraClassDeclaration");
94   return value.empty() ? llvm::Optional<StringRef>() : value;
95 }
getLoc() const96 llvm::ArrayRef<llvm::SMLoc> TypeDef::getLoc() const { return def->getLoc(); }
operator ==(const TypeDef & other) const97 bool TypeDef::operator==(const TypeDef &other) const {
98   return def == other.def;
99 }
100 
operator <(const TypeDef & other) const101 bool TypeDef::operator<(const TypeDef &other) const {
102   return getName() < other.getName();
103 }
104 
getName() const105 StringRef TypeParameter::getName() const {
106   return def->getArgName(num)->getValue();
107 }
getAllocator() const108 llvm::Optional<StringRef> TypeParameter::getAllocator() const {
109   llvm::Init *parameterType = def->getArg(num);
110   if (isa<llvm::StringInit>(parameterType))
111     return llvm::Optional<StringRef>();
112 
113   if (auto *typeParameter = dyn_cast<llvm::DefInit>(parameterType)) {
114     llvm::RecordVal *code = typeParameter->getDef()->getValue("allocator");
115     if (!code)
116       return llvm::Optional<StringRef>();
117     if (llvm::StringInit *ci = dyn_cast<llvm::StringInit>(code->getValue()))
118       return ci->getValue();
119     if (isa<llvm::UnsetInit>(code->getValue()))
120       return llvm::Optional<StringRef>();
121 
122     llvm::PrintFatalError(
123         typeParameter->getDef()->getLoc(),
124         "Record `" + def->getArgName(num)->getValue() +
125             "', field `printer' does not have a code initializer!");
126   }
127 
128   llvm::PrintFatalError("Parameters DAG arguments must be either strings or "
129                         "defs which inherit from TypeParameter\n");
130 }
getCppType() const131 StringRef TypeParameter::getCppType() const {
132   auto *parameterType = def->getArg(num);
133   if (auto *stringType = dyn_cast<llvm::StringInit>(parameterType))
134     return stringType->getValue();
135   if (auto *typeParameter = dyn_cast<llvm::DefInit>(parameterType))
136     return typeParameter->getDef()->getValueAsString("cppType");
137   llvm::PrintFatalError(
138       "Parameters DAG arguments must be either strings or defs "
139       "which inherit from TypeParameter\n");
140 }
getDescription() const141 llvm::Optional<StringRef> TypeParameter::getDescription() const {
142   auto *parameterType = def->getArg(num);
143   if (auto *typeParameter = dyn_cast<llvm::DefInit>(parameterType)) {
144     const auto *desc = typeParameter->getDef()->getValue("description");
145     if (llvm::StringInit *ci = dyn_cast<llvm::StringInit>(desc->getValue()))
146       return ci->getValue();
147   }
148   return llvm::Optional<StringRef>();
149 }
getSyntax() const150 StringRef TypeParameter::getSyntax() const {
151   auto *parameterType = def->getArg(num);
152   if (auto *stringType = dyn_cast<llvm::StringInit>(parameterType))
153     return stringType->getValue();
154   if (auto *typeParameter = dyn_cast<llvm::DefInit>(parameterType)) {
155     const auto *syntax = typeParameter->getDef()->getValue("syntax");
156     if (syntax && isa<llvm::StringInit>(syntax->getValue()))
157       return dyn_cast<llvm::StringInit>(syntax->getValue())->getValue();
158     return getCppType();
159   }
160   llvm::PrintFatalError("Parameters DAG arguments must be either strings or "
161                         "defs which inherit from TypeParameter");
162 }
163