2021-11-12 09:17:05 +08:00
|
|
|
//===- Class.cpp - Helper classes for Op C++ code emission --------------===//
|
2020-01-11 00:18:08 +08:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-12 09:17:05 +08:00
|
|
|
#include "mlir/TableGen/Class.h"
|
2020-01-11 00:18:08 +08:00
|
|
|
#include "mlir/TableGen/Format.h"
|
2020-09-18 04:18:09 +08:00
|
|
|
#include "llvm/ADT/Sequence.h"
|
2020-01-11 00:18:08 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2020-09-18 04:18:09 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2020-01-11 00:18:08 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
2020-08-12 08:47:07 +08:00
|
|
|
using namespace mlir::tblgen;
|
2020-01-11 00:18:08 +08:00
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
/// Returns space to be emitted after the given C++ `type`. return "" if the
|
|
|
|
/// ends with '&' or '*', or is empty, else returns " ".
|
2021-11-12 09:17:05 +08:00
|
|
|
static StringRef getSpaceAfterType(StringRef type) {
|
2020-09-18 04:18:09 +08:00
|
|
|
return (type.empty() || type.endswith("&") || type.endswith("*")) ? "" : " ";
|
|
|
|
}
|
|
|
|
|
2020-01-11 00:18:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-11-12 09:17:05 +08:00
|
|
|
// MethodParameter definitions
|
2020-01-11 00:18:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void MethodParameter::writeDeclTo(raw_indented_ostream &os) const {
|
2021-11-12 09:17:05 +08:00
|
|
|
if (optional)
|
2020-09-18 04:18:09 +08:00
|
|
|
os << "/*optional*/";
|
|
|
|
os << type << getSpaceAfterType(type) << name;
|
2021-11-30 22:09:00 +08:00
|
|
|
if (hasDefaultValue())
|
2020-09-18 04:18:09 +08:00
|
|
|
os << " = " << defaultValue;
|
|
|
|
}
|
2020-01-11 00:18:08 +08:00
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void MethodParameter::writeDefTo(raw_indented_ostream &os) const {
|
|
|
|
if (optional)
|
|
|
|
os << "/*optional*/";
|
|
|
|
os << type << getSpaceAfterType(type) << name;
|
|
|
|
}
|
|
|
|
|
2020-09-18 04:18:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-11-12 09:17:05 +08:00
|
|
|
// MethodParameters definitions
|
2020-09-18 04:18:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void MethodParameters::writeDeclTo(raw_indented_ostream &os) const {
|
2021-11-12 09:17:05 +08:00
|
|
|
llvm::interleaveComma(parameters, os,
|
|
|
|
[&os](auto ¶m) { param.writeDeclTo(os); });
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|
2021-11-30 22:09:00 +08:00
|
|
|
void MethodParameters::writeDefTo(raw_indented_ostream &os) const {
|
2021-11-12 09:17:05 +08:00
|
|
|
llvm::interleaveComma(parameters, os,
|
|
|
|
[&os](auto ¶m) { param.writeDefTo(os); });
|
2020-09-18 04:18:09 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:17:05 +08:00
|
|
|
bool MethodParameters::subsumes(const MethodParameters &other) const {
|
|
|
|
// These parameters do not subsume the others if there are fewer parameters
|
|
|
|
// or their types do not match.
|
|
|
|
if (parameters.size() < other.parameters.size())
|
|
|
|
return false;
|
|
|
|
if (!std::equal(
|
|
|
|
other.parameters.begin(), other.parameters.end(), parameters.begin(),
|
|
|
|
[](auto &lhs, auto &rhs) { return lhs.getType() == rhs.getType(); }))
|
2020-09-18 04:18:09 +08:00
|
|
|
return false;
|
2020-01-11 00:18:08 +08:00
|
|
|
|
2021-11-12 09:17:05 +08:00
|
|
|
// If all the common parameters have the same type, we can elide the other
|
|
|
|
// method if this method has the same number of parameters as other or if the
|
|
|
|
// first paramater after the common parameters has a default value (and, as
|
|
|
|
// required by C++, subsequent parameters will have default values too).
|
|
|
|
return parameters.size() == other.parameters.size() ||
|
|
|
|
parameters[other.parameters.size()].hasDefaultValue();
|
2020-09-18 04:18:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-11-12 09:17:05 +08:00
|
|
|
// MethodSignature definitions
|
2020-09-18 04:18:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-12 09:17:05 +08:00
|
|
|
bool MethodSignature::makesRedundant(const MethodSignature &other) const {
|
|
|
|
return methodName == other.methodName &&
|
|
|
|
parameters.subsumes(other.parameters);
|
2020-09-18 04:18:09 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void MethodSignature::writeDeclTo(raw_indented_ostream &os) const {
|
2020-09-18 04:18:09 +08:00
|
|
|
os << returnType << getSpaceAfterType(returnType) << methodName << "(";
|
2021-11-12 09:17:05 +08:00
|
|
|
parameters.writeDeclTo(os);
|
2020-09-18 04:18:09 +08:00
|
|
|
os << ")";
|
|
|
|
}
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void MethodSignature::writeDefTo(raw_indented_ostream &os,
|
|
|
|
StringRef namePrefix) const {
|
2020-09-18 04:18:09 +08:00
|
|
|
os << returnType << getSpaceAfterType(returnType) << namePrefix
|
|
|
|
<< (namePrefix.empty() ? "" : "::") << methodName << "(";
|
2021-11-12 09:17:05 +08:00
|
|
|
parameters.writeDefTo(os);
|
2020-09-18 04:18:09 +08:00
|
|
|
os << ")";
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-11-12 09:17:05 +08:00
|
|
|
// MethodBody definitions
|
2020-01-11 00:18:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
MethodBody::MethodBody(bool declOnly)
|
|
|
|
: declOnly(declOnly), stringOs(body), os(stringOs) {}
|
2020-01-11 00:18:08 +08:00
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void MethodBody::writeTo(raw_indented_ostream &os) const {
|
2020-01-11 00:18:08 +08:00
|
|
|
auto bodyRef = StringRef(body).drop_while([](char c) { return c == '\n'; });
|
|
|
|
os << bodyRef;
|
2021-11-30 22:09:00 +08:00
|
|
|
if (bodyRef.empty())
|
|
|
|
return;
|
|
|
|
if (bodyRef.back() != '\n')
|
2020-01-11 00:18:08 +08:00
|
|
|
os << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-11-12 09:17:05 +08:00
|
|
|
// Method definitions
|
2020-01-11 00:18:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void Method::writeDeclTo(raw_indented_ostream &os) const {
|
2020-01-11 00:18:08 +08:00
|
|
|
if (isStatic())
|
|
|
|
os << "static ";
|
2021-11-30 22:09:00 +08:00
|
|
|
if (properties & ConstexprValue)
|
2021-02-05 01:12:15 +08:00
|
|
|
os << "constexpr ";
|
2020-01-11 00:18:08 +08:00
|
|
|
methodSignature.writeDeclTo(os);
|
2021-11-30 22:09:00 +08:00
|
|
|
if (isConst())
|
|
|
|
os << " const";
|
2021-06-23 03:13:35 +08:00
|
|
|
if (!isInline()) {
|
2021-11-30 22:09:00 +08:00
|
|
|
os << ";\n";
|
|
|
|
return;
|
2021-02-05 01:12:15 +08:00
|
|
|
}
|
2021-11-30 22:09:00 +08:00
|
|
|
os << " {\n";
|
|
|
|
methodBody.writeTo(os);
|
|
|
|
os << "}\n\n";
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void Method::writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const {
|
|
|
|
// The method has no definition to write if it is declaration only or inline.
|
|
|
|
if (properties & Declaration || isInline())
|
2021-02-05 01:12:15 +08:00
|
|
|
return;
|
2021-11-30 22:09:00 +08:00
|
|
|
|
2020-01-11 00:18:08 +08:00
|
|
|
methodSignature.writeDefTo(os, namePrefix);
|
2021-11-30 22:09:00 +08:00
|
|
|
if (isConst())
|
|
|
|
os << " const";
|
2020-01-11 00:18:08 +08:00
|
|
|
os << " {\n";
|
|
|
|
methodBody.writeTo(os);
|
2021-11-30 22:09:00 +08:00
|
|
|
os << "}\n\n";
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 00:05:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-11-12 09:17:05 +08:00
|
|
|
// Constructor definitions
|
2020-05-29 00:05:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void Constructor::writeDeclTo(raw_indented_ostream &os) const {
|
|
|
|
if (properties & ConstexprValue)
|
|
|
|
os << "constexpr ";
|
|
|
|
methodSignature.writeDeclTo(os);
|
|
|
|
if (!isInline()) {
|
|
|
|
os << ";\n\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
os << ' ';
|
|
|
|
if (!initializers.empty())
|
|
|
|
os << ": ";
|
|
|
|
llvm::interleaveComma(initializers, os,
|
|
|
|
[&](auto &initializer) { initializer.writeTo(os); });
|
|
|
|
if (!initializers.empty())
|
|
|
|
os << ' ';
|
|
|
|
os << "{";
|
|
|
|
methodBody.writeTo(os);
|
|
|
|
os << "}\n\n";
|
2020-05-29 00:05:24 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void Constructor::writeDefTo(raw_indented_ostream &os,
|
|
|
|
StringRef namePrefix) const {
|
|
|
|
// The method has no definition to write if it is declaration only or inline.
|
|
|
|
if (properties & Declaration || isInline())
|
2020-05-29 00:05:24 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
methodSignature.writeDefTo(os, namePrefix);
|
2021-11-30 22:09:00 +08:00
|
|
|
os << ' ';
|
|
|
|
if (!initializers.empty())
|
|
|
|
os << ": ";
|
|
|
|
llvm::interleaveComma(initializers, os,
|
|
|
|
[&](auto &initializer) { initializer.writeTo(os); });
|
|
|
|
if (!initializers.empty())
|
|
|
|
os << ' ';
|
|
|
|
os << "{";
|
2020-05-29 00:05:24 +08:00
|
|
|
methodBody.writeTo(os);
|
2021-11-30 22:09:00 +08:00
|
|
|
os << "}\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Constructor::MemberInitializer::writeTo(raw_indented_ostream &os) const {
|
|
|
|
os << name << '(' << value << ')';
|
2020-05-29 00:05:24 +08:00
|
|
|
}
|
|
|
|
|
2020-01-11 00:18:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-11-30 22:09:00 +08:00
|
|
|
// Visibility definitions
|
2020-01-11 00:18:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
namespace mlir {
|
|
|
|
namespace tblgen {
|
|
|
|
raw_ostream &operator<<(raw_ostream &os, Visibility visibility) {
|
|
|
|
switch (visibility) {
|
|
|
|
case Visibility::Public:
|
|
|
|
return os << "public";
|
|
|
|
case Visibility::Protected:
|
|
|
|
return os << "protected";
|
|
|
|
case Visibility::Private:
|
|
|
|
return os << "private";
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
2021-12-08 02:27:58 +08:00
|
|
|
} // namespace tblgen
|
|
|
|
} // namespace mlir
|
2020-01-11 00:18:08 +08:00
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ParentClass definitions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ParentClass::writeTo(raw_indented_ostream &os) const {
|
|
|
|
os << visibility << ' ' << name;
|
|
|
|
if (!templateParams.empty()) {
|
|
|
|
auto scope = os.scope("<", ">", /*indent=*/false);
|
|
|
|
llvm::interleaveComma(templateParams, os,
|
|
|
|
[&](auto ¶m) { os << param; });
|
|
|
|
}
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|
2021-11-12 09:17:05 +08:00
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UsingDeclaration definitions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void UsingDeclaration::writeDeclTo(raw_indented_ostream &os) const {
|
|
|
|
os << "using " << name;
|
|
|
|
if (!value.empty())
|
|
|
|
os << " = " << value;
|
|
|
|
os << ";\n";
|
|
|
|
}
|
2020-09-18 04:18:09 +08:00
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Field definitions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void Field::writeDeclTo(raw_indented_ostream &os) const {
|
|
|
|
os << type << ' ' << name << ";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VisibilityDeclaration definitions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void VisibilityDeclaration::writeDeclTo(raw_indented_ostream &os) const {
|
|
|
|
os.unindent();
|
|
|
|
os << visibility << ":\n";
|
|
|
|
os.indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExtraClassDeclaration definitions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ExtraClassDeclaration::writeDeclTo(raw_indented_ostream &os) const {
|
|
|
|
os.printReindented(extraClassDeclaration);
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:42:12 +08:00
|
|
|
void ExtraClassDeclaration::writeDefTo(raw_indented_ostream &os,
|
|
|
|
StringRef namePrefix) const {
|
|
|
|
os.printReindented(extraClassDefinition);
|
|
|
|
}
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Class definitions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ParentClass &Class::addParent(ParentClass parent) {
|
|
|
|
parents.push_back(std::move(parent));
|
|
|
|
return parents.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Class::writeDeclTo(raw_indented_ostream &os) const {
|
|
|
|
// Declare the class.
|
|
|
|
os << (isStruct ? "struct" : "class") << ' ' << className << ' ';
|
|
|
|
|
|
|
|
// Declare the parent classes, if any.
|
|
|
|
if (!parents.empty()) {
|
|
|
|
os << ": ";
|
|
|
|
llvm::interleaveComma(parents, os,
|
|
|
|
[&](auto &parent) { parent.writeTo(os); });
|
|
|
|
os << ' ';
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|
2021-11-30 22:09:00 +08:00
|
|
|
auto classScope = os.scope("{\n", "};\n", /*indent=*/true);
|
2020-09-18 04:18:09 +08:00
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
// Print all the class declarations.
|
|
|
|
for (auto &decl : declarations)
|
|
|
|
decl->writeDeclTo(os);
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
void Class::writeDefTo(raw_indented_ostream &os) const {
|
|
|
|
// Print all the definitions.
|
|
|
|
for (auto &decl : declarations)
|
|
|
|
decl->writeDefTo(os, className);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Class::finalize() {
|
|
|
|
// Sort the methods by public and private. Remove them from the pending list
|
|
|
|
// of methods.
|
|
|
|
SmallVector<std::unique_ptr<Method>> publicMethods, privateMethods;
|
|
|
|
for (auto &method : methods) {
|
|
|
|
if (method->isPrivate())
|
|
|
|
privateMethods.push_back(std::move(method));
|
|
|
|
else
|
|
|
|
publicMethods.push_back(std::move(method));
|
|
|
|
}
|
|
|
|
methods.clear();
|
|
|
|
|
|
|
|
// If the last visibility declaration wasn't `public`, add one that is. Then,
|
|
|
|
// declare the public methods.
|
|
|
|
if (!publicMethods.empty() && getLastVisibilityDecl() != Visibility::Public)
|
|
|
|
declare<VisibilityDeclaration>(Visibility::Public);
|
|
|
|
for (auto &method : publicMethods)
|
|
|
|
declarations.push_back(std::move(method));
|
|
|
|
|
|
|
|
// If the last visibility declaration wasn't `private`, add one that is. Then,
|
|
|
|
// declare the private methods.
|
|
|
|
if (!privateMethods.empty() && getLastVisibilityDecl() != Visibility::Private)
|
|
|
|
declare<VisibilityDeclaration>(Visibility::Private);
|
|
|
|
for (auto &method : privateMethods)
|
|
|
|
declarations.push_back(std::move(method));
|
|
|
|
|
|
|
|
// All fields added to the pending list are private and declared at the bottom
|
|
|
|
// of the class. If the last visibility declaration wasn't `private`, add one
|
|
|
|
// that is, then declare the fields.
|
|
|
|
if (!fields.empty() && getLastVisibilityDecl() != Visibility::Private)
|
|
|
|
declare<VisibilityDeclaration>(Visibility::Private);
|
|
|
|
for (auto &field : fields)
|
|
|
|
declare<Field>(std::move(field));
|
|
|
|
fields.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility Class::getLastVisibilityDecl() const {
|
|
|
|
auto reverseDecls = llvm::reverse(declarations);
|
|
|
|
auto it = llvm::find_if(reverseDecls, [](auto &decl) {
|
|
|
|
return isa<VisibilityDeclaration>(decl);
|
2020-09-18 04:18:09 +08:00
|
|
|
});
|
2021-11-30 22:09:00 +08:00
|
|
|
return it == reverseDecls.end()
|
|
|
|
? (isStruct ? Visibility::Public : Visibility::Private)
|
|
|
|
: cast<VisibilityDeclaration>(*it).getVisibility();
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
Method *insertAndPruneMethods(std::vector<std::unique_ptr<Method>> &methods,
|
|
|
|
std::unique_ptr<Method> newMethod) {
|
2021-11-12 09:17:05 +08:00
|
|
|
if (llvm::any_of(methods, [&](auto &method) {
|
2021-11-30 22:09:00 +08:00
|
|
|
return method->makesRedundant(*newMethod);
|
2021-11-12 09:17:05 +08:00
|
|
|
}))
|
|
|
|
return nullptr;
|
|
|
|
|
2021-11-30 22:09:00 +08:00
|
|
|
llvm::erase_if(methods, [&](auto &method) {
|
|
|
|
return newMethod->makesRedundant(*method);
|
|
|
|
});
|
2021-11-12 09:17:05 +08:00
|
|
|
methods.push_back(std::move(newMethod));
|
2021-11-30 22:09:00 +08:00
|
|
|
return methods.back().get();
|
2021-11-12 09:17:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Method *Class::addMethodAndPrune(Method &&newMethod) {
|
2021-11-30 22:09:00 +08:00
|
|
|
return insertAndPruneMethods(methods,
|
|
|
|
std::make_unique<Method>(std::move(newMethod)));
|
2021-11-12 09:17:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Constructor *Class::addConstructorAndPrune(Constructor &&newCtor) {
|
2021-11-30 22:09:00 +08:00
|
|
|
return dyn_cast_or_null<Constructor>(insertAndPruneMethods(
|
|
|
|
methods, std::make_unique<Constructor>(std::move(newCtor))));
|
2020-01-11 00:18:08 +08:00
|
|
|
}
|