10#include "llvm/ADT/StringExtras.h"
11#include "llvm/ADT/TypeSwitch.h"
12#include "llvm/Support/SaveAndRestore.h"
13#include "llvm/Support/ScopedPrinter.h"
26 NodePrinter(raw_ostream &os) : os(os) {}
29 void print(Type type);
36 template <
typename RangeT,
37 std::enable_if_t<!std::is_convertible<RangeT, const Node *>::value>
39 void printChildren(RangeT &&range) {
44 auto it = std::begin(range);
45 for (
unsigned i = 0, e = llvm::size(range) - 1; i < e; ++i, ++it)
49 elementIndentStack.back() =
true;
52 template <
typename RangeT,
typename... OthersT,
53 std::enable_if_t<std::is_convertible<RangeT, const Node *>::value>
55 void printChildren(RangeT &&range, OthersT &&...others) {
56 printChildren(ArrayRef<const Node *>({range, others...}));
60 template <
typename RangeT>
61 void printChildren(StringRef label, RangeT &&range) {
64 elementIndentStack.reserve(elementIndentStack.size() + 1);
65 llvm::SaveAndRestore lastElement(elementIndentStack.back(),
true);
69 elementIndentStack.push_back(
false);
70 printChildren(std::forward<RangeT>(range));
71 elementIndentStack.pop_back();
75 void printImpl(
const CompoundStmt *stmt);
76 void printImpl(
const EraseStmt *stmt);
77 void printImpl(
const LetStmt *stmt);
78 void printImpl(
const ReplaceStmt *stmt);
79 void printImpl(
const ReturnStmt *stmt);
80 void printImpl(
const RewriteStmt *stmt);
82 void printImpl(
const AttributeExpr *expr);
83 void printImpl(
const CallExpr *expr);
84 void printImpl(
const DeclRefExpr *expr);
85 void printImpl(
const MemberAccessExpr *expr);
86 void printImpl(
const OperationExpr *expr);
87 void printImpl(
const RangeExpr *expr);
88 void printImpl(
const TupleExpr *expr);
89 void printImpl(
const TypeExpr *expr);
91 void printImpl(
const AttrConstraintDecl *decl);
92 void printImpl(
const OpConstraintDecl *decl);
93 void printImpl(
const TypeConstraintDecl *decl);
94 void printImpl(
const TypeRangeConstraintDecl *decl);
95 void printImpl(
const UserConstraintDecl *decl);
96 void printImpl(
const ValueConstraintDecl *decl);
97 void printImpl(
const ValueRangeConstraintDecl *decl);
98 void printImpl(
const NamedAttributeDecl *decl);
99 void printImpl(
const OpNameDecl *decl);
100 void printImpl(
const PatternDecl *decl);
101 void printImpl(
const UserRewriteDecl *decl);
102 void printImpl(
const VariableDecl *decl);
103 void printImpl(
const Module *module);
107 if (elementIndentStack.empty())
110 for (
bool isLastElt : llvm::ArrayRef(elementIndentStack).drop_back())
111 os << (isLastElt ?
" " :
" |");
112 os << (elementIndentStack.back() ?
" `" :
" |");
120 SmallVector<bool> elementIndentStack;
124void NodePrinter::print(
Type type) {
132 .Case([&](AttributeType) { os <<
"Attr"; })
133 .Case([&](ConstraintType) { os <<
"Constraint"; })
134 .Case([&](OperationType type) {
136 if (std::optional<StringRef> name = type.
getName())
137 os <<
"<" << *name <<
">";
139 .Case([&](RangeType type) {
143 .Case([&](RewriteType) { os <<
"Rewrite"; })
144 .Case([&](TupleType type) {
146 llvm::interleaveComma(
149 if (!std::get<0>(it).empty())
150 os << std::get<0>(it) <<
": ";
151 this->print(std::get<1>(it));
155 .Case([&](TypeType) { os <<
"Type"; })
156 .Case([&](ValueType) { os <<
"Value"; })
157 .DefaultUnreachable(
"unknown AST type");
160void NodePrinter::print(
const Node *node) {
164 elementIndentStack.push_back(
false);
168 const CompoundStmt,
const EraseStmt,
const LetStmt,
const ReplaceStmt,
169 const ReturnStmt,
const RewriteStmt,
172 const AttributeExpr,
const CallExpr,
const DeclRefExpr,
173 const MemberAccessExpr,
const OperationExpr,
const RangeExpr,
174 const TupleExpr,
const TypeExpr,
177 const AttrConstraintDecl,
const OpConstraintDecl,
178 const TypeConstraintDecl,
const TypeRangeConstraintDecl,
179 const UserConstraintDecl,
const ValueConstraintDecl,
180 const ValueRangeConstraintDecl,
const NamedAttributeDecl,
181 const OpNameDecl,
const PatternDecl,
const UserRewriteDecl,
184 const Module>([&](
auto derivedNode) { this->printImpl(derivedNode); })
185 .DefaultUnreachable(
"unknown AST node");
186 elementIndentStack.pop_back();
189void NodePrinter::printImpl(
const CompoundStmt *stmt) {
190 os <<
"CompoundStmt " << stmt <<
"\n";
194void NodePrinter::printImpl(
const EraseStmt *stmt) {
195 os <<
"EraseStmt " << stmt <<
"\n";
199void NodePrinter::printImpl(
const LetStmt *stmt) {
200 os <<
"LetStmt " << stmt <<
"\n";
204void NodePrinter::printImpl(
const ReplaceStmt *stmt) {
205 os <<
"ReplaceStmt " << stmt <<
"\n";
210void NodePrinter::printImpl(
const ReturnStmt *stmt) {
211 os <<
"ReturnStmt " << stmt <<
"\n";
215void NodePrinter::printImpl(
const RewriteStmt *stmt) {
216 os <<
"RewriteStmt " << stmt <<
"\n";
220void NodePrinter::printImpl(
const AttributeExpr *expr) {
221 os <<
"AttributeExpr " << expr <<
" Value<\"" << expr->
getValue() <<
"\">\n";
224void NodePrinter::printImpl(
const CallExpr *expr) {
225 os <<
"CallExpr " << expr <<
" Type<";
235void NodePrinter::printImpl(
const DeclRefExpr *expr) {
236 os <<
"DeclRefExpr " << expr <<
" Type<";
239 printChildren(expr->
getDecl());
242void NodePrinter::printImpl(
const MemberAccessExpr *expr) {
243 os <<
"MemberAccessExpr " << expr <<
" Member<" << expr->
getMemberName()
250void NodePrinter::printImpl(
const OperationExpr *expr) {
251 os <<
"OperationExpr " << expr <<
" Type<";
261void NodePrinter::printImpl(
const RangeExpr *expr) {
262 os <<
"RangeExpr " << expr <<
" Type<";
269void NodePrinter::printImpl(
const TupleExpr *expr) {
270 os <<
"TupleExpr " << expr <<
" Type<";
277void NodePrinter::printImpl(
const TypeExpr *expr) {
278 os <<
"TypeExpr " << expr <<
" Value<\"" << expr->
getValue() <<
"\">\n";
281void NodePrinter::printImpl(
const AttrConstraintDecl *decl) {
282 os <<
"AttrConstraintDecl " << decl <<
"\n";
284 printChildren(typeExpr);
287void NodePrinter::printImpl(
const OpConstraintDecl *decl) {
288 os <<
"OpConstraintDecl " << decl <<
"\n";
292void NodePrinter::printImpl(
const TypeConstraintDecl *decl) {
293 os <<
"TypeConstraintDecl " << decl <<
"\n";
296void NodePrinter::printImpl(
const TypeRangeConstraintDecl *decl) {
297 os <<
"TypeRangeConstraintDecl " << decl <<
"\n";
300void NodePrinter::printImpl(
const UserConstraintDecl *decl) {
301 os <<
"UserConstraintDecl " << decl <<
" Name<" << decl->
getName().
getName()
303 if (std::optional<StringRef> codeBlock = decl->
getCodeBlock()) {
305 llvm::printEscapedString(*codeBlock, os);
309 printChildren(
"Inputs", decl->
getInputs());
311 if (
const CompoundStmt *body = decl->
getBody())
315void NodePrinter::printImpl(
const ValueConstraintDecl *decl) {
316 os <<
"ValueConstraintDecl " << decl <<
"\n";
318 printChildren(typeExpr);
321void NodePrinter::printImpl(
const ValueRangeConstraintDecl *decl) {
322 os <<
"ValueRangeConstraintDecl " << decl <<
"\n";
324 printChildren(typeExpr);
327void NodePrinter::printImpl(
const NamedAttributeDecl *decl) {
328 os <<
"NamedAttributeDecl " << decl <<
" Name<" << decl->
getName().
getName()
333void NodePrinter::printImpl(
const OpNameDecl *decl) {
334 os <<
"OpNameDecl " << decl;
335 if (std::optional<StringRef> name = decl->
getName())
336 os <<
" Name<" << *name <<
">";
340void NodePrinter::printImpl(
const PatternDecl *decl) {
341 os <<
"PatternDecl " << decl;
342 if (
const Name *name = decl->
getName())
343 os <<
" Name<" << name->getName() <<
">";
344 if (std::optional<uint16_t> benefit = decl->
getBenefit())
345 os <<
" Benefit<" << *benefit <<
">";
350 printChildren(decl->
getBody());
353void NodePrinter::printImpl(
const UserRewriteDecl *decl) {
354 os <<
"UserRewriteDecl " << decl <<
" Name<" << decl->
getName().
getName()
356 if (std::optional<StringRef> codeBlock = decl->
getCodeBlock()) {
358 llvm::printEscapedString(*codeBlock, os);
362 printChildren(
"Inputs", decl->
getInputs());
364 if (
const CompoundStmt *body = decl->
getBody())
368void NodePrinter::printImpl(
const VariableDecl *decl) {
369 os <<
"VariableDecl " << decl <<
" Name<" << decl->
getName().
getName()
374 printChildren(initExpr);
378 [](
const ConstraintRef &ref) { return ref.constraint; });
379 printChildren(
"Constraints", constraints);
382void NodePrinter::printImpl(
const Module *module) {
383 os <<
"Module " << module <<
"\n";
391void Node::print(
raw_ostream &os)
const { NodePrinter(os).print(
this); }
MemRefDependenceGraph::Node Node
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Expr * getTypeExpr()
Return the optional type the attribute is constrained to.
StringRef getValue() const
Get the raw value of this expression.
Expr * getCallableExpr() const
Return the callable of this call.
MutableArrayRef< Expr * > getArguments()
Return the arguments of this call.
bool getIsNegated() const
Returns whether the result of this call is to be negated.
MutableArrayRef< Stmt * > getChildren()
Return the children of this compound statement.
Decl * getDecl() const
Get the decl referenced by this expression.
const Name * getName() const
Return the name of the decl, or nullptr if it doesn't have one.
Type getType() const
Return the type of this expression.
VariableDecl * getVarDecl() const
Return the variable defined by this statement.
const Expr * getParentExpr() const
Get the parent expression of this access.
StringRef getMemberName() const
Return the name of the member being accessed.
MutableArrayRef< Decl * > getChildren()
Return the children of this module.
const Name & getName() const
Return the name of the attribute.
Expr * getValue() const
Return value of the attribute.
const OpNameDecl * getNameDecl() const
Return the declaration of the operation name.
std::optional< StringRef > getName() const
Return the name of this operation, or std::nullopt if the name is unknown.
Expr * getRootOpExpr() const
Return the root operation of this rewrite.
MutableArrayRef< Expr * > getOperands()
Return the operands of this operation.
MutableArrayRef< NamedAttributeDecl * > getAttributes()
Return the attributes of this operation.
const OpNameDecl * getNameDecl() const
Return the declaration of the operation name.
MutableArrayRef< Expr * > getResultTypes()
Return the result types of this operation.
std::optional< StringRef > getName() const
Return the name of this operation type, or std::nullopt if it doesn't have on.
const CompoundStmt * getBody() const
Return the body of this pattern.
std::optional< uint16_t > getBenefit() const
Return the benefit of this pattern if specified, or std::nullopt.
bool hasBoundedRewriteRecursion() const
Return if this pattern has bounded rewrite recursion.
RangeType getType() const
Return the range result type of this expression.
MutableArrayRef< Expr * > getElements()
Return the element expressions of this range.
Type getElementType() const
Return the element type of this range.
MutableArrayRef< Expr * > getReplExprs()
Return the replacement values of this statement.
Expr * getResultExpr()
Return the result expression of this statement.
CompoundStmt * getRewriteBody() const
Return the compound rewrite body.
TupleType getType() const
Return the tuple result type of this expression.
MutableArrayRef< Expr * > getElements()
Return the element expressions of this tuple.
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
ArrayRef< Type > getElementTypes() const
Return the element types of this tuple.
StringRef getValue() const
Get the raw value of this expression.
void print(raw_ostream &os) const
Print this type to the given stream.
const Name & getName() const
Return the name of the constraint.
const CompoundStmt * getBody() const
Return the body of this constraint if this constraint is a PDLL constraint, otherwise returns nullptr...
MutableArrayRef< VariableDecl * > getResults()
Return the explicit results of the constraint declaration.
MutableArrayRef< VariableDecl * > getInputs()
Return the input arguments of this constraint.
Type getResultType() const
Return the result type of this constraint.
std::optional< StringRef > getCodeBlock() const
Return the optional code block of this constraint, if this is a native constraint with a provided imp...
std::optional< StringRef > getCodeBlock() const
Return the optional code block of this rewrite, if this is a native rewrite with a provided implement...
const Name & getName() const
Return the name of the rewrite.
const CompoundStmt * getBody() const
Return the body of this rewrite if this rewrite is a PDLL rewrite, otherwise returns nullptr.
Type getResultType() const
Return the result type of this rewrite.
MutableArrayRef< VariableDecl * > getResults()
Return the explicit results of the rewrite declaration.
MutableArrayRef< VariableDecl * > getInputs()
Return the input arguments of this rewrite.
Expr * getTypeExpr()
Return the optional type the value is constrained to.
Expr * getTypeExpr()
Return the optional type the value range is constrained to.
MutableArrayRef< ConstraintRef > getConstraints()
Return the constraints of this variable.
Expr * getInitExpr() const
Return the initializer expression of this statement, or nullptr if there was no initializer.
const Name & getName() const
Return the name of the decl.
Type getType() const
Return the type of the decl.
Include the generated interface declarations.
llvm::TypeSwitch< T, ResultT > TypeSwitch
StringRef getName() const
Return the raw string name.