11 #include "llvm/ADT/Sequence.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/Debug.h"
21 return (type.empty() || type.ends_with(
"&") || type.ends_with(
"*")) ?
""
34 os <<
" = " << defaultValue;
48 llvm::interleaveComma(parameters, os,
49 [&os](
auto ¶m) { param.writeDeclTo(os); });
52 llvm::interleaveComma(parameters, os,
53 [&os](
auto ¶m) { param.writeDefTo(os); });
59 if (parameters.size() < other.parameters.size())
62 other.parameters.begin(), other.parameters.end(), parameters.begin(),
63 [](
auto &lhs,
auto &rhs) { return lhs.getType() == rhs.getType(); }))
70 return parameters.size() == other.parameters.size() ||
71 parameters[other.parameters.size()].hasDefaultValue();
79 return methodName == other.methodName &&
80 parameters.
subsumes(other.parameters);
90 StringRef namePrefix)
const {
92 << (namePrefix.empty() ?
"" :
"::") << methodName <<
"(";
99 if (templateParams.empty())
103 llvm::interleaveComma(templateParams, os,
104 [&](StringRef param) { os <<
"typename " << param; });
113 : declOnly(declOnly), stringOs(body), os(stringOs) {}
116 auto bodyRef = StringRef(body).ltrim(
'\n');
120 if (bodyRef.back() !=
'\n')
131 os <<
"[[deprecated(\"";
178 if (!initializers.empty())
180 llvm::interleaveComma(initializers, os,
181 [&](
auto &initializer) { initializer.writeTo(os); });
182 if (!initializers.empty())
190 StringRef namePrefix)
const {
197 if (!initializers.empty())
199 llvm::interleaveComma(initializers, os,
200 [&](
auto &initializer) { initializer.writeTo(os); });
201 if (!initializers.empty())
209 os << name <<
'(' << value <<
')';
219 switch (visibility) {
221 return os <<
"public";
223 return os <<
"protected";
225 return os <<
"private";
237 os << visibility <<
' ' << name;
238 if (!templateParams.empty()) {
239 auto scope = os.
scope(
"<",
">",
false);
240 llvm::interleaveComma(templateParams, os,
241 [&](
auto ¶m) { os << param; });
250 if (!templateParams.empty()) {
252 llvm::interleaveComma(templateParams, os, [&](StringRef paramName) {
253 os <<
"typename " << paramName;
257 os <<
"using " << name;
259 os <<
" = " << value;
268 os << type <<
' ' << name <<
";\n";
277 os << visibility <<
":\n";
290 StringRef namePrefix)
const {
299 parents.push_back(std::move(parent));
300 return parents.back();
304 if (!templateParams.empty()) {
306 llvm::interleaveComma(templateParams, os,
307 [&](StringRef param) { os <<
"typename " << param; });
312 os << (isStruct ?
"struct" :
"class") <<
' ' << className <<
' ';
315 if (!parents.empty()) {
317 llvm::interleaveComma(parents, os,
318 [&](
auto &parent) { parent.
writeTo(os); });
321 auto classScope = os.
scope(
"{\n",
"};\n",
true);
324 for (
auto &decl : declarations)
325 decl->writeDeclTo(os);
330 for (
auto &decl : declarations)
331 decl->writeDefTo(os, className);
338 for (
auto &method : methods) {
339 if (method->isPrivate())
340 privateMethods.push_back(std::move(method));
342 publicMethods.push_back(std::move(method));
350 for (
auto &method : publicMethods)
351 declarations.push_back(std::move(method));
357 for (
auto &method : privateMethods)
358 declarations.push_back(std::move(method));
365 for (
auto &field : fields)
366 declare<Field>(std::move(field));
371 auto reverseDecls = llvm::reverse(declarations);
372 auto it = llvm::find_if(reverseDecls, llvm::IsaPred<VisibilityDeclaration>);
373 return it == reverseDecls.end()
375 : cast<VisibilityDeclaration>(**it).getVisibility();
378 Method *insertAndPruneMethods(std::vector<std::unique_ptr<Method>> &methods,
379 std::unique_ptr<Method> newMethod) {
380 if (llvm::any_of(methods, [&](
auto &method) {
381 return method->makesRedundant(*newMethod);
385 llvm::erase_if(methods, [&](
auto &method) {
386 return newMethod->makesRedundant(*method);
388 methods.push_back(std::move(newMethod));
389 return methods.back().get();
393 return insertAndPruneMethods(methods,
394 std::make_unique<Method>(std::move(newMethod)));
398 return dyn_cast_or_null<Constructor>(insertAndPruneMethods(
399 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.
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 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.