10 #include "llvm/ADT/Twine.h"
11 #include "llvm/Support/Debug.h"
19 return (type.empty() || type.ends_with(
"&") || type.ends_with(
"*")) ?
""
32 os <<
" = " << defaultValue;
46 llvm::interleaveComma(parameters, os,
47 [&os](
auto ¶m) { param.writeDeclTo(os); });
50 llvm::interleaveComma(parameters, os,
51 [&os](
auto ¶m) { param.writeDefTo(os); });
57 if (parameters.size() < other.parameters.size())
60 other.parameters.begin(), other.parameters.end(), parameters.begin(),
61 [](
auto &lhs,
auto &rhs) { return lhs.getType() == rhs.getType(); }))
68 return parameters.size() == other.parameters.size() ||
69 parameters[other.parameters.size()].hasDefaultValue();
77 return methodName == other.methodName &&
78 parameters.
subsumes(other.parameters);
88 StringRef namePrefix)
const {
90 << (namePrefix.empty() ?
"" :
"::") << methodName <<
"(";
97 if (templateParams.empty())
101 llvm::interleaveComma(templateParams, os,
102 [&](StringRef param) { os <<
"typename " << param; });
111 : declOnly(declOnly), stringOs(body), os(stringOs) {}
114 auto bodyRef = StringRef(body).ltrim(
'\n');
118 if (bodyRef.back() !=
'\n')
129 os <<
"[[deprecated(\"";
173 assert(
false &&
"constructor cannot be static");
177 assert(
false &&
"constructor cannot be const");
182 "declaration implies no definition and thus cannot be inline");
185 if (isDeclaration && isConstexprValue) {
187 "declaration implies no definition and thus cannot be constexpr");
208 if (!initializers.empty())
210 llvm::interleaveComma(initializers, os,
211 [&](
auto &initializer) { initializer.writeTo(os); });
212 if (!initializers.empty())
220 StringRef namePrefix)
const {
227 if (!initializers.empty())
229 llvm::interleaveComma(initializers, os,
230 [&](
auto &initializer) { initializer.writeTo(os); });
231 if (!initializers.empty())
239 os << name <<
'(' << value <<
')';
249 switch (visibility) {
251 return os <<
"public";
253 return os <<
"protected";
255 return os <<
"private";
267 os << visibility <<
' ' << name;
268 if (!templateParams.empty()) {
269 auto scope = os.
scope(
"<",
">",
false);
270 llvm::interleaveComma(templateParams, os,
271 [&](
auto ¶m) { os << param; });
280 if (!templateParams.empty()) {
282 llvm::interleaveComma(templateParams, os, [&](StringRef paramName) {
283 os <<
"typename " << paramName;
287 os <<
"using " << name;
289 os <<
" = " << value;
298 os << type <<
' ' << name <<
";\n";
307 os << visibility <<
":\n";
320 StringRef namePrefix)
const {
329 parents.push_back(std::move(parent));
330 return parents.back();
334 if (!templateParams.empty()) {
336 llvm::interleaveComma(templateParams, os,
337 [&](StringRef param) { os <<
"typename " << param; });
342 os << (isStruct ?
"struct" :
"class") <<
' ' << className <<
' ';
345 if (!parents.empty()) {
347 llvm::interleaveComma(parents, os,
348 [&](
auto &parent) { parent.
writeTo(os); });
351 auto classScope = os.
scope(
"{\n",
"};\n",
true);
354 for (
auto &decl : declarations)
355 decl->writeDeclTo(os);
360 for (
auto &decl : declarations)
361 decl->writeDefTo(os, className);
368 for (
auto &method : methods) {
369 if (method->isPrivate())
370 privateMethods.push_back(std::move(method));
372 publicMethods.push_back(std::move(method));
380 for (
auto &method : publicMethods)
381 declarations.push_back(std::move(method));
387 for (
auto &method : privateMethods)
388 declarations.push_back(std::move(method));
395 for (
auto &field : fields)
396 declare<Field>(std::move(field));
401 auto reverseDecls = llvm::reverse(declarations);
402 auto it = llvm::find_if(reverseDecls, llvm::IsaPred<VisibilityDeclaration>);
403 return it == reverseDecls.end()
405 : cast<VisibilityDeclaration>(**it).getVisibility();
408 Method *insertAndPruneMethods(std::vector<std::unique_ptr<Method>> &methods,
409 std::unique_ptr<Method> newMethod) {
410 if (llvm::any_of(methods, [&](
auto &method) {
411 return method->makesRedundant(*newMethod);
415 llvm::erase_if(methods, [&](
auto &method) {
416 return newMethod->makesRedundant(*method);
418 methods.push_back(std::move(newMethod));
419 return methods.back().get();
423 return insertAndPruneMethods(methods,
424 std::make_unique<Method>(std::move(newMethod)));
428 return dyn_cast_or_null<Constructor>(insertAndPruneMethods(
429 methods, std::make_unique<Constructor>(std::move(newCtor))));
static StringRef getSpaceAfterType(StringRef type)
Returns space to be emitted after the given C++ type.
raw_ostream subclass that simplifies indention a sequence of code.
raw_indented_ostream & unindent()
Decreases the indent and returning this raw_indented_ostream.
DelimitedScope scope(StringRef open="", StringRef close="", bool indent=true)
Returns DelimitedScope.
raw_indented_ostream & indent()
Increases the indent and returning this raw_indented_ostream.
raw_indented_ostream & printReindented(StringRef str, StringRef extraPrefix="")
Prints a string re-indented to the current indent.
ParentClass & addParent(ParentClass parent)
Add a parent class.
virtual void finalize()
The declaration of a class needs to be "finalized".
void writeDefTo(raw_ostream &rawOs) const
Write the definitions of thiss class's out-of-line constructors and methods.
Constructor * addConstructorAndPrune(Constructor &&newCtor)
Add a new constructor if it is not made redundant by any existing constructors and prune and existing...
Visibility getLastVisibilityDecl() const
Get the last visibility declaration.
void writeDeclTo(raw_ostream &rawOs) const
Write the declaration of this class, all declarations, and definitions of inline functions.
Method * addMethodAndPrune(Method &&newMethod)
Add a new method if it is not made redundant by any existing methods and prune and existing methods m...
void writeTo(raw_indented_ostream &os) const
Write the member initializer.
void writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const override
Write the definition of the constructor if it is not inline.
void writeDeclTo(raw_indented_ostream &os) const override
Write the declaration of the constructor, and its definition if inline.
void writeDeclTo(raw_indented_ostream &os) const override
Write the declaration of the field.
void writeTo(raw_indented_ostream &os) const
Write the method body to the output stream.
MethodBody(bool declOnly)
Create a method body, indicating whether it should be elided for methods that are declaration-only.
void writeDefTo(raw_indented_ostream &os) const
Write the parameter as part of a method definition.
bool hasDefaultValue() const
Returns true if the parameter has a default value.
void writeDeclTo(raw_indented_ostream &os) const
Write the parameter as part of a method declaration.
This class contains a list of method parameters for constructor, class methods, and method signatures...
void writeDefTo(raw_indented_ostream &os) const
Write the parameters as part of a method definition.
bool subsumes(const MethodParameters &other) const
Determine whether this list of parameters "subsumes" another, which occurs when this parameter list i...
void writeDeclTo(raw_indented_ostream &os) const
Write the parameters as part of a method declaration.
This class contains the signature of a C++ method, including the return type.
bool makesRedundant(const MethodSignature &other) const
Determine whether a method with this signature makes a method with other signature redundant.
void writeDeclTo(raw_indented_ostream &os) const
Write the signature as part of a method declaration.
void writeTemplateParamsTo(raw_indented_ostream &os) const
Write the template parameters of the signature.
void writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const
Write the signature as part of a method definition.
Class for holding an op's method for C++ code emission.
bool isStatic() const
Returns true if this is a static method.
MethodBody methodBody
The body of the method, if it has one.
static bool methodPropertiesAreCompatible(Properties properties)
Utility method to verify method properties correctness.
MethodSignature methodSignature
The signature of the method.
void writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const override
Write the method definition. This is a no-op for inline methods.
void writeDeclTo(raw_indented_ostream &os) const override
Write the method declaration, including the definition if inline.
Properties properties
A collection of method properties.
std::optional< std::string > deprecationMessage
Deprecation message if the method is deprecated.
bool isInline() const
Returns true if this is an inline method.
bool isConstructor() const
Returns true if this is a constructor.
bool isConst() const
Returns true if this class method is const.
This class describes a C++ parent class declaration.
void writeTo(raw_indented_ostream &os) const
Write the parent class declaration.
void writeDeclTo(raw_indented_ostream &os) const override
Write the using declaration.
void writeDeclTo(raw_indented_ostream &os) const override
Write the visibility declaration.
Visibility
This enum describes C++ inheritance visibility.
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, mlir::tblgen::Visibility visibility)
Write "public", "protected", or "private".
Include the generated interface declarations.