blob: 46802d765e1f0ab8604e30d9afb324088e3414da [file] [log] [blame]
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001//===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattnercbe4f772007-08-08 22:51:59 +00009// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
10// pretty print the AST back out to C code.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000011//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000014#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
Douglas Gregorc7acfdf2009-01-06 05:10:23 +000018#include "clang/AST/DeclCXX.h"
Ted Kremenek5c84c012007-10-17 18:36:42 +000019#include "clang/AST/DeclObjC.h"
Alexey Bataev90c228f2016-02-08 09:29:13 +000020#include "clang/AST/DeclOpenMP.h"
Douglas Gregorcdbc5392011-01-15 01:15:58 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000022#include "clang/AST/Expr.h"
Douglas Gregor2b88c112010-09-08 00:15:04 +000023#include "clang/AST/ExprCXX.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000024#include "clang/AST/ExprObjC.h"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000025#include "clang/AST/ExprOpenMP.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000026#include "clang/AST/NestedNameSpecifier.h"
27#include "clang/AST/OpenMPClause.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000028#include "clang/AST/PrettyPrinter.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000029#include "clang/AST/Stmt.h"
30#include "clang/AST/StmtCXX.h"
31#include "clang/AST/StmtObjC.h"
32#include "clang/AST/StmtOpenMP.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000033#include "clang/AST/StmtVisitor.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000034#include "clang/AST/TemplateBase.h"
35#include "clang/AST/Type.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000036#include "clang/Basic/CharInfo.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000037#include "clang/Basic/ExpressionTraits.h"
38#include "clang/Basic/IdentifierTable.h"
Csaba Dabis9ee26c82019-05-29 18:17:18 +000039#include "clang/Basic/JsonSupport.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000040#include "clang/Basic/LLVM.h"
41#include "clang/Basic/Lambda.h"
42#include "clang/Basic/OpenMPKinds.h"
43#include "clang/Basic/OperatorKinds.h"
44#include "clang/Basic/SourceLocation.h"
45#include "clang/Basic/TypeTraits.h"
Alex Lorenz36070ed2017-08-17 13:41:55 +000046#include "clang/Lex/Lexer.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000047#include "llvm/ADT/ArrayRef.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000048#include "llvm/ADT/SmallString.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000049#include "llvm/ADT/SmallVector.h"
50#include "llvm/ADT/StringRef.h"
51#include "llvm/Support/Casting.h"
52#include "llvm/Support/Compiler.h"
53#include "llvm/Support/ErrorHandling.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000054#include "llvm/Support/Format.h"
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000055#include "llvm/Support/raw_ostream.h"
56#include <cassert>
57#include <string>
58
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000059using namespace clang;
60
61//===----------------------------------------------------------------------===//
62// StmtPrinter Visitor
63//===----------------------------------------------------------------------===//
64
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000065namespace {
66
Benjamin Kramer26222b62009-11-28 19:03:38 +000067 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000068 raw_ostream &OS;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000069 unsigned IndentLevel;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +000070 PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000071 PrintingPolicy Policy;
George Karpenkov64885ae2018-09-15 02:02:31 +000072 std::string NL;
Alex Lorenz36070ed2017-08-17 13:41:55 +000073 const ASTContext *Context;
Douglas Gregor7de59662009-05-29 20:38:28 +000074
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000075 public:
Alex Lorenz36070ed2017-08-17 13:41:55 +000076 StmtPrinter(raw_ostream &os, PrinterHelper *helper,
77 const PrintingPolicy &Policy, unsigned Indentation = 0,
George Karpenkov64885ae2018-09-15 02:02:31 +000078 StringRef NL = "\n",
Alex Lorenz36070ed2017-08-17 13:41:55 +000079 const ASTContext *Context = nullptr)
80 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
George Karpenkov64885ae2018-09-15 02:02:31 +000081 NL(NL), Context(Context) {}
Mike Stump11289f42009-09-09 15:08:12 +000082
Douglas Gregor7de59662009-05-29 20:38:28 +000083 void PrintStmt(Stmt *S) {
84 PrintStmt(S, Policy.Indentation);
85 }
86
87 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000088 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000089 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000090 // If this is an expr used in a stmt context, indent and newline it.
91 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000092 Visit(S);
George Karpenkov64885ae2018-09-15 02:02:31 +000093 OS << ";" << NL;
Chris Lattner882f7882006-11-04 18:52:07 +000094 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000095 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000096 } else {
George Karpenkov64885ae2018-09-15 02:02:31 +000097 Indent() << "<<<NULL STATEMENT>>>" << NL;
Chris Lattner882f7882006-11-04 18:52:07 +000098 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000099 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +0000100 }
Eli Friedman15ea8802009-05-30 00:19:54 +0000101
Richard Smith8baa5002018-09-28 18:44:09 +0000102 void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
103 // FIXME: Cope better with odd prefix widths.
104 IndentLevel += (PrefixWidth + 1) / 2;
105 if (auto *DS = dyn_cast<DeclStmt>(S))
106 PrintRawDeclStmt(DS);
107 else
108 PrintExpr(cast<Expr>(S));
109 OS << "; ";
110 IndentLevel -= (PrefixWidth + 1) / 2;
111 }
112
113 void PrintControlledStmt(Stmt *S) {
114 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
115 OS << " ";
116 PrintRawCompoundStmt(CS);
117 OS << NL;
118 } else {
119 OS << NL;
120 PrintStmt(S);
121 }
122 }
123
Chris Lattner073926e2007-05-20 23:04:55 +0000124 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000125 void PrintRawDecl(Decl *D);
Eli Friedmanf86e5072012-10-16 23:45:15 +0000126 void PrintRawDeclStmt(const DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000127 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +0000128 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000129 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +0000130 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
131 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Alexey Bataev7828b252017-11-21 17:08:48 +0000132 void PrintOMPExecutableDirective(OMPExecutableDirective *S,
133 bool ForceNoStmt = false);
Mike Stump11289f42009-09-09 15:08:12 +0000134
Chris Lattner882f7882006-11-04 18:52:07 +0000135 void PrintExpr(Expr *E) {
136 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +0000137 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000138 else
Chris Lattner882f7882006-11-04 18:52:07 +0000139 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000140 }
Mike Stump11289f42009-09-09 15:08:12 +0000141
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000142 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +0000143 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
144 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000145 return OS;
146 }
Mike Stump11289f42009-09-09 15:08:12 +0000147
Mike Stump11289f42009-09-09 15:08:12 +0000148 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000149 if (Helper && Helper->handledStmt(S,OS))
150 return;
151 else StmtVisitor<StmtPrinter>::Visit(S);
152 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000153
Chandler Carruthb7967b92010-10-23 08:21:37 +0000154 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
George Karpenkov64885ae2018-09-15 02:02:31 +0000155 Indent() << "<<unknown stmt type>>" << NL;
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000156 }
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000157
Chandler Carruthb7967b92010-10-23 08:21:37 +0000158 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000159 OS << "<<unknown expr type>>";
160 }
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000161
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000162 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000163
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000164#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000165#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000166 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000167#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000168 };
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000169
170} // namespace
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000171
Chris Lattner71e23ce2006-11-04 20:18:38 +0000172//===----------------------------------------------------------------------===//
173// Stmt printing methods.
174//===----------------------------------------------------------------------===//
175
Chris Lattner073926e2007-05-20 23:04:55 +0000176/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
177/// with no newline after the }.
178void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000179 OS << "{" << NL;
Aaron Ballmanc7e4e212014-03-17 14:19:37 +0000180 for (auto *I : Node->body())
181 PrintStmt(I);
Mike Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattner073926e2007-05-20 23:04:55 +0000183 Indent() << "}";
184}
185
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000186void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000187 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000188}
189
Eli Friedmanf86e5072012-10-16 23:45:15 +0000190void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000191 SmallVector<Decl *, 2> Decls(S->decls());
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000192 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000193}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000194
195void StmtPrinter::VisitNullStmt(NullStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000196 Indent() << ";" << NL;
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000197}
198
199void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000200 Indent();
201 PrintRawDeclStmt(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000202 OS << ";" << NL;
Steve Naroff2a8ad182007-05-29 22:59:26 +0000203}
204
Chris Lattner073926e2007-05-20 23:04:55 +0000205void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
206 Indent();
207 PrintRawCompoundStmt(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000208 OS << "" << NL;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000209}
210
Chris Lattner6c0ff132006-11-05 00:19:50 +0000211void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000212 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000213 PrintExpr(Node->getLHS());
214 if (Node->getRHS()) {
215 OS << " ... ";
216 PrintExpr(Node->getRHS());
217 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000218 OS << ":" << NL;
Mike Stump11289f42009-09-09 15:08:12 +0000219
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000220 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000221}
222
223void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000224 Indent(-1) << "default:" << NL;
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000225 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000226}
227
228void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000229 Indent(-1) << Node->getName() << ":" << NL;
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000230 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000231}
232
Richard Smithc202b282012-04-14 00:33:13 +0000233void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000234 for (const auto *Attr : Node->getAttrs()) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +0000235 Attr->printPretty(OS, Policy);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000236 }
237
Richard Smithc202b282012-04-14 00:33:13 +0000238 PrintStmt(Node->getSubStmt(), 0);
239}
240
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000241void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000242 OS << "if (";
Richard Smith8baa5002018-09-28 18:44:09 +0000243 if (If->getInit())
244 PrintInitStmt(If->getInit(), 4);
Eli Friedmanf86e5072012-10-16 23:45:15 +0000245 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
246 PrintRawDeclStmt(DS);
247 else
248 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000249 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000250
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000251 if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
Chris Lattner073926e2007-05-20 23:04:55 +0000252 OS << ' ';
253 PrintRawCompoundStmt(CS);
George Karpenkov64885ae2018-09-15 02:02:31 +0000254 OS << (If->getElse() ? " " : NL);
Chris Lattner073926e2007-05-20 23:04:55 +0000255 } else {
George Karpenkov64885ae2018-09-15 02:02:31 +0000256 OS << NL;
Chris Lattner073926e2007-05-20 23:04:55 +0000257 PrintStmt(If->getThen());
258 if (If->getElse()) Indent();
259 }
Mike Stump11289f42009-09-09 15:08:12 +0000260
Chris Lattner073926e2007-05-20 23:04:55 +0000261 if (Stmt *Else = If->getElse()) {
262 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000263
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000264 if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
Chris Lattner073926e2007-05-20 23:04:55 +0000265 OS << ' ';
266 PrintRawCompoundStmt(CS);
George Karpenkov64885ae2018-09-15 02:02:31 +0000267 OS << NL;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000268 } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000269 OS << ' ';
270 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000271 } else {
George Karpenkov64885ae2018-09-15 02:02:31 +0000272 OS << NL;
Chris Lattner073926e2007-05-20 23:04:55 +0000273 PrintStmt(If->getElse());
274 }
Chris Lattner882f7882006-11-04 18:52:07 +0000275 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000276}
277
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000278void StmtPrinter::VisitIfStmt(IfStmt *If) {
279 Indent();
280 PrintRawIfStmt(If);
281}
282
Chris Lattnerf2174b62006-11-04 20:59:27 +0000283void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
284 Indent() << "switch (";
Richard Smith8baa5002018-09-28 18:44:09 +0000285 if (Node->getInit())
286 PrintInitStmt(Node->getInit(), 8);
Eli Friedmanf86e5072012-10-16 23:45:15 +0000287 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
288 PrintRawDeclStmt(DS);
289 else
290 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000291 OS << ")";
Richard Smith8baa5002018-09-28 18:44:09 +0000292 PrintControlledStmt(Node->getBody());
Chris Lattnerf2174b62006-11-04 20:59:27 +0000293}
294
Chris Lattner85ed8732006-11-04 20:40:44 +0000295void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
296 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000297 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
298 PrintRawDeclStmt(DS);
299 else
300 PrintExpr(Node->getCond());
George Karpenkov64885ae2018-09-15 02:02:31 +0000301 OS << ")" << NL;
Chris Lattner85ed8732006-11-04 20:40:44 +0000302 PrintStmt(Node->getBody());
303}
304
305void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000306 Indent() << "do ";
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000307 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000308 PrintRawCompoundStmt(CS);
309 OS << " ";
310 } else {
George Karpenkov64885ae2018-09-15 02:02:31 +0000311 OS << NL;
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000312 PrintStmt(Node->getBody());
313 Indent();
314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000316 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000317 PrintExpr(Node->getCond());
George Karpenkov64885ae2018-09-15 02:02:31 +0000318 OS << ");" << NL;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000319}
320
Chris Lattner71e23ce2006-11-04 20:18:38 +0000321void StmtPrinter::VisitForStmt(ForStmt *Node) {
322 Indent() << "for (";
Richard Smith8baa5002018-09-28 18:44:09 +0000323 if (Node->getInit())
324 PrintInitStmt(Node->getInit(), 5);
325 else
326 OS << (Node->getCond() ? "; " : ";");
327 if (Node->getCond())
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000328 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000329 OS << ";";
330 if (Node->getInc()) {
331 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000332 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000333 }
Richard Smith8baa5002018-09-28 18:44:09 +0000334 OS << ")";
335 PrintControlledStmt(Node->getBody());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000336}
337
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000338void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000339 Indent() << "for (";
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000340 if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000341 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000342 else
343 PrintExpr(cast<Expr>(Node->getElement()));
344 OS << " in ";
345 PrintExpr(Node->getCollection());
Richard Smith8baa5002018-09-28 18:44:09 +0000346 OS << ")";
347 PrintControlledStmt(Node->getBody());
Fariborz Jahanian83615522008-01-02 22:54:34 +0000348}
349
Richard Smith02e85f32011-04-14 22:09:26 +0000350void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
351 Indent() << "for (";
Richard Smith8baa5002018-09-28 18:44:09 +0000352 if (Node->getInit())
353 PrintInitStmt(Node->getInit(), 5);
Richard Smith02e85f32011-04-14 22:09:26 +0000354 PrintingPolicy SubPolicy(Policy);
355 SubPolicy.SuppressInitializers = true;
356 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
357 OS << " : ";
358 PrintExpr(Node->getRangeInit());
Richard Smith8baa5002018-09-28 18:44:09 +0000359 OS << ")";
360 PrintControlledStmt(Node->getBody());
Richard Smith02e85f32011-04-14 22:09:26 +0000361}
362
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000363void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
364 Indent();
365 if (Node->isIfExists())
366 OS << "__if_exists (";
367 else
368 OS << "__if_not_exists (";
Fangrui Song6907ce22018-07-30 19:24:48 +0000369
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000370 if (NestedNameSpecifier *Qualifier
371 = Node->getQualifierLoc().getNestedNameSpecifier())
372 Qualifier->print(OS, Policy);
Fangrui Song6907ce22018-07-30 19:24:48 +0000373
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000374 OS << Node->getNameInfo() << ") ";
Fangrui Song6907ce22018-07-30 19:24:48 +0000375
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000376 PrintRawCompoundStmt(Node->getSubStmt());
377}
378
Chris Lattner16976d32006-11-05 01:46:01 +0000379void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000380 Indent() << "goto " << Node->getLabel()->getName() << ";";
George Karpenkov64885ae2018-09-15 02:02:31 +0000381 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner16976d32006-11-05 01:46:01 +0000382}
383
384void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000385 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000386 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000387 OS << ";";
George Karpenkov64885ae2018-09-15 02:02:31 +0000388 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner16976d32006-11-05 01:46:01 +0000389}
390
391void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000392 Indent() << "continue;";
George Karpenkov64885ae2018-09-15 02:02:31 +0000393 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner16976d32006-11-05 01:46:01 +0000394}
395
396void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000397 Indent() << "break;";
George Karpenkov64885ae2018-09-15 02:02:31 +0000398 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner16976d32006-11-05 01:46:01 +0000399}
400
Chris Lattner882f7882006-11-04 18:52:07 +0000401void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
402 Indent() << "return";
403 if (Node->getRetValue()) {
404 OS << " ";
405 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000406 }
Ted Kremenek88446602013-12-11 23:44:02 +0000407 OS << ";";
George Karpenkov64885ae2018-09-15 02:02:31 +0000408 if (Policy.IncludeNewlines) OS << NL;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000409}
410
Chad Rosierde70e0e2012-08-25 00:11:56 +0000411void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000412 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000413
Anders Carlsson660bdd12007-11-23 23:12:25 +0000414 if (Node->isVolatile())
415 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000416
Jennifer Yub8fee672019-06-03 15:57:25 +0000417 if (Node->isAsmGoto())
418 OS << "goto ";
419
Anders Carlsson660bdd12007-11-23 23:12:25 +0000420 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000421 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000422
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000423 // Outputs
424 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
Jennifer Yub8fee672019-06-03 15:57:25 +0000425 Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000426 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000427
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000428 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
429 if (i != 0)
430 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000431
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000432 if (!Node->getOutputName(i).empty()) {
433 OS << '[';
434 OS << Node->getOutputName(i);
435 OS << "] ";
436 }
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattner72bbf172009-03-10 04:59:06 +0000438 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000439 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000440 Visit(Node->getOutputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000441 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000444 // Inputs
Jennifer Yub8fee672019-06-03 15:57:25 +0000445 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
446 Node->getNumLabels() != 0)
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000447 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000448
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000449 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
450 if (i != 0)
451 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000452
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000453 if (!Node->getInputName(i).empty()) {
454 OS << '[';
455 OS << Node->getInputName(i);
456 OS << "] ";
457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Chris Lattner72bbf172009-03-10 04:59:06 +0000459 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000460 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000461 Visit(Node->getInputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000462 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000463 }
Mike Stump11289f42009-09-09 15:08:12 +0000464
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000465 // Clobbers
Jennifer Yub8fee672019-06-03 15:57:25 +0000466 if (Node->getNumClobbers() != 0 || Node->getNumLabels())
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000467 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000468
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000469 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
470 if (i != 0)
471 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000472
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000473 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000474 }
Mike Stump11289f42009-09-09 15:08:12 +0000475
Jennifer Yub8fee672019-06-03 15:57:25 +0000476 // Labels
477 if (Node->getNumLabels() != 0)
478 OS << " : ";
479
480 for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
481 if (i != 0)
482 OS << ", ";
483 OS << Node->getLabelName(i);
484 }
485
Ted Kremenek88446602013-12-11 23:44:02 +0000486 OS << ");";
George Karpenkov64885ae2018-09-15 02:02:31 +0000487 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner73c56c02007-10-29 04:04:16 +0000488}
489
Chad Rosier32503022012-06-11 20:47:18 +0000490void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
491 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000492 Indent() << "__asm ";
493 if (Node->hasBraces())
George Karpenkov64885ae2018-09-15 02:02:31 +0000494 OS << "{" << NL;
495 OS << Node->getAsmString() << NL;
Chad Rosierb6f46c12012-08-15 16:53:30 +0000496 if (Node->hasBraces())
George Karpenkov64885ae2018-09-15 02:02:31 +0000497 Indent() << "}" << NL;
Chad Rosier32503022012-06-11 20:47:18 +0000498}
499
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000500void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000501 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000502}
503
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000504void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000505 Indent() << "@try";
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000506 if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000507 PrintRawCompoundStmt(TS);
George Karpenkov64885ae2018-09-15 02:02:31 +0000508 OS << NL;
Fariborz Jahanian88157952007-11-02 18:16:07 +0000509 }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Douglas Gregor96c79492010-04-23 22:50:49 +0000511 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
512 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000513 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000514 if (catchStmt->getCatchParamDecl()) {
515 if (Decl *DS = catchStmt->getCatchParamDecl())
516 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000517 }
518 OS << ")";
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000519 if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
Mike Stump11289f42009-09-09 15:08:12 +0000520 PrintRawCompoundStmt(CS);
George Karpenkov64885ae2018-09-15 02:02:31 +0000521 OS << NL;
Mike Stump11289f42009-09-09 15:08:12 +0000522 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000523 }
Mike Stump11289f42009-09-09 15:08:12 +0000524
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000525 if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000526 Indent() << "@finally";
527 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
George Karpenkov64885ae2018-09-15 02:02:31 +0000528 OS << NL;
Mike Stump11289f42009-09-09 15:08:12 +0000529 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000530}
531
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000532void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000533}
534
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000535void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000536 Indent() << "@catch (...) { /* todo */ } " << NL;
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000537}
538
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000539void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000540 Indent() << "@throw";
541 if (Node->getThrowExpr()) {
542 OS << " ";
543 PrintExpr(Node->getThrowExpr());
544 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000545 OS << ";" << NL;
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000546}
547
Erik Pilkington29099de2016-07-16 00:35:23 +0000548void StmtPrinter::VisitObjCAvailabilityCheckExpr(
549 ObjCAvailabilityCheckExpr *Node) {
550 OS << "@available(...)";
551}
552
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000553void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000554 Indent() << "@synchronized (";
555 PrintExpr(Node->getSynchExpr());
556 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000557 PrintRawCompoundStmt(Node->getSynchBody());
George Karpenkov64885ae2018-09-15 02:02:31 +0000558 OS << NL;
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000559}
560
John McCall31168b02011-06-15 23:02:42 +0000561void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
562 Indent() << "@autoreleasepool";
563 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
George Karpenkov64885ae2018-09-15 02:02:31 +0000564 OS << NL;
John McCall31168b02011-06-15 23:02:42 +0000565}
566
Sebastian Redl9b244a82008-12-22 21:35:02 +0000567void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
568 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000569 if (Decl *ExDecl = Node->getExceptionDecl())
570 PrintRawDecl(ExDecl);
571 else
572 OS << "...";
573 OS << ") ";
574 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000575}
576
577void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
578 Indent();
579 PrintRawCXXCatchStmt(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000580 OS << NL;
Sebastian Redl9b244a82008-12-22 21:35:02 +0000581}
582
583void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
584 Indent() << "try ";
585 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000586 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000587 OS << " ";
588 PrintRawCXXCatchStmt(Node->getHandler(i));
589 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000590 OS << NL;
Sebastian Redl54c04d42008-12-22 19:15:10 +0000591}
592
John Wiegley1c0675e2011-04-28 01:08:34 +0000593void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
594 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
595 PrintRawCompoundStmt(Node->getTryBlock());
596 SEHExceptStmt *E = Node->getExceptHandler();
597 SEHFinallyStmt *F = Node->getFinallyHandler();
598 if(E)
599 PrintRawSEHExceptHandler(E);
600 else {
601 assert(F && "Must have a finally block...");
602 PrintRawSEHFinallyStmt(F);
603 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000604 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000605}
606
607void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
608 OS << "__finally ";
609 PrintRawCompoundStmt(Node->getBlock());
George Karpenkov64885ae2018-09-15 02:02:31 +0000610 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000611}
612
613void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
614 OS << "__except (";
615 VisitExpr(Node->getFilterExpr());
George Karpenkov64885ae2018-09-15 02:02:31 +0000616 OS << ")" << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000617 PrintRawCompoundStmt(Node->getBlock());
George Karpenkov64885ae2018-09-15 02:02:31 +0000618 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000619}
620
621void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
622 Indent();
623 PrintRawSEHExceptHandler(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000624 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000625}
626
627void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
628 Indent();
629 PrintRawSEHFinallyStmt(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000630 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000631}
632
Nico Weber9b982072014-07-07 00:12:30 +0000633void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
634 Indent() << "__leave;";
George Karpenkov64885ae2018-09-15 02:02:31 +0000635 if (Policy.IncludeNewlines) OS << NL;
Nico Weber9b982072014-07-07 00:12:30 +0000636}
637
Chris Lattner71e23ce2006-11-04 20:18:38 +0000638//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000639// OpenMP directives printing methods
640//===----------------------------------------------------------------------===//
641
Alexey Bataev7828b252017-11-21 17:08:48 +0000642void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
643 bool ForceNoStmt) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000644 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000645 ArrayRef<OMPClause *> Clauses = S->clauses();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000646 for (auto *Clause : Clauses)
647 if (Clause && !Clause->isImplicit()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000648 OS << ' ';
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000649 Printer.Visit(Clause);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000650 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000651 OS << NL;
Alexey Bataev475a7442018-01-12 19:39:11 +0000652 if (!ForceNoStmt && S->hasAssociatedStmt())
653 PrintStmt(S->getInnermostCapturedStmt()->getCapturedStmt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000654}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000655
656void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000657 Indent() << "#pragma omp parallel";
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000658 PrintOMPExecutableDirective(Node);
659}
660
661void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000662 Indent() << "#pragma omp simd";
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000663 PrintOMPExecutableDirective(Node);
664}
665
Alexey Bataevf29276e2014-06-18 04:14:57 +0000666void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000667 Indent() << "#pragma omp for";
Alexey Bataevf29276e2014-06-18 04:14:57 +0000668 PrintOMPExecutableDirective(Node);
669}
670
Alexander Musmanf82886e2014-09-18 05:12:34 +0000671void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000672 Indent() << "#pragma omp for simd";
Alexander Musmanf82886e2014-09-18 05:12:34 +0000673 PrintOMPExecutableDirective(Node);
674}
675
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000676void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000677 Indent() << "#pragma omp sections";
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000678 PrintOMPExecutableDirective(Node);
679}
680
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000681void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
682 Indent() << "#pragma omp section";
683 PrintOMPExecutableDirective(Node);
684}
685
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000686void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000687 Indent() << "#pragma omp single";
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000688 PrintOMPExecutableDirective(Node);
689}
690
Alexander Musman80c22892014-07-17 08:54:58 +0000691void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
692 Indent() << "#pragma omp master";
693 PrintOMPExecutableDirective(Node);
694}
695
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000696void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
697 Indent() << "#pragma omp critical";
698 if (Node->getDirectiveName().getName()) {
699 OS << " (";
700 Node->getDirectiveName().printName(OS);
701 OS << ")";
702 }
703 PrintOMPExecutableDirective(Node);
704}
705
Alexey Bataev4acb8592014-07-07 13:01:15 +0000706void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000707 Indent() << "#pragma omp parallel for";
Alexey Bataev4acb8592014-07-07 13:01:15 +0000708 PrintOMPExecutableDirective(Node);
709}
710
Alexander Musmane4e893b2014-09-23 09:33:00 +0000711void StmtPrinter::VisitOMPParallelForSimdDirective(
712 OMPParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000713 Indent() << "#pragma omp parallel for simd";
Alexander Musmane4e893b2014-09-23 09:33:00 +0000714 PrintOMPExecutableDirective(Node);
715}
716
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000717void StmtPrinter::VisitOMPParallelSectionsDirective(
718 OMPParallelSectionsDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000719 Indent() << "#pragma omp parallel sections";
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000720 PrintOMPExecutableDirective(Node);
721}
722
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000723void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000724 Indent() << "#pragma omp task";
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000725 PrintOMPExecutableDirective(Node);
726}
727
Alexey Bataev68446b72014-07-18 07:47:19 +0000728void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
729 Indent() << "#pragma omp taskyield";
730 PrintOMPExecutableDirective(Node);
731}
732
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000733void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
734 Indent() << "#pragma omp barrier";
735 PrintOMPExecutableDirective(Node);
736}
737
Alexey Bataev2df347a2014-07-18 10:17:07 +0000738void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
739 Indent() << "#pragma omp taskwait";
740 PrintOMPExecutableDirective(Node);
741}
742
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000743void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000744 Indent() << "#pragma omp taskgroup";
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000745 PrintOMPExecutableDirective(Node);
746}
747
Alexey Bataev6125da92014-07-21 11:26:11 +0000748void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000749 Indent() << "#pragma omp flush";
Alexey Bataev6125da92014-07-21 11:26:11 +0000750 PrintOMPExecutableDirective(Node);
751}
752
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000753void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000754 Indent() << "#pragma omp ordered";
Alexey Bataev475a7442018-01-12 19:39:11 +0000755 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000756}
757
Alexey Bataev0162e452014-07-22 10:10:35 +0000758void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000759 Indent() << "#pragma omp atomic";
Alexey Bataev0162e452014-07-22 10:10:35 +0000760 PrintOMPExecutableDirective(Node);
761}
762
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000763void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000764 Indent() << "#pragma omp target";
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000765 PrintOMPExecutableDirective(Node);
766}
767
Michael Wong65f367f2015-07-21 13:44:28 +0000768void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000769 Indent() << "#pragma omp target data";
Michael Wong65f367f2015-07-21 13:44:28 +0000770 PrintOMPExecutableDirective(Node);
771}
772
Samuel Antaodf67fc42016-01-19 19:15:56 +0000773void StmtPrinter::VisitOMPTargetEnterDataDirective(
774 OMPTargetEnterDataDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000775 Indent() << "#pragma omp target enter data";
Alexey Bataev7828b252017-11-21 17:08:48 +0000776 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antaodf67fc42016-01-19 19:15:56 +0000777}
778
Samuel Antao72590762016-01-19 20:04:50 +0000779void StmtPrinter::VisitOMPTargetExitDataDirective(
780 OMPTargetExitDataDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000781 Indent() << "#pragma omp target exit data";
Alexey Bataev7828b252017-11-21 17:08:48 +0000782 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antao72590762016-01-19 20:04:50 +0000783}
784
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000785void StmtPrinter::VisitOMPTargetParallelDirective(
786 OMPTargetParallelDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000787 Indent() << "#pragma omp target parallel";
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000788 PrintOMPExecutableDirective(Node);
789}
790
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000791void StmtPrinter::VisitOMPTargetParallelForDirective(
792 OMPTargetParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000793 Indent() << "#pragma omp target parallel for";
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000794 PrintOMPExecutableDirective(Node);
795}
796
Alexey Bataev13314bf2014-10-09 04:18:56 +0000797void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000798 Indent() << "#pragma omp teams";
Alexey Bataev13314bf2014-10-09 04:18:56 +0000799 PrintOMPExecutableDirective(Node);
800}
801
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000802void StmtPrinter::VisitOMPCancellationPointDirective(
803 OMPCancellationPointDirective *Node) {
804 Indent() << "#pragma omp cancellation point "
805 << getOpenMPDirectiveName(Node->getCancelRegion());
806 PrintOMPExecutableDirective(Node);
807}
Alexey Bataev80909872015-07-02 11:25:17 +0000808
809void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
810 Indent() << "#pragma omp cancel "
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000811 << getOpenMPDirectiveName(Node->getCancelRegion());
Alexey Bataev80909872015-07-02 11:25:17 +0000812 PrintOMPExecutableDirective(Node);
813}
Alexey Bataev49f6e782015-12-01 04:18:41 +0000814
815void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000816 Indent() << "#pragma omp taskloop";
Alexey Bataev49f6e782015-12-01 04:18:41 +0000817 PrintOMPExecutableDirective(Node);
818}
819
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000820void StmtPrinter::VisitOMPTaskLoopSimdDirective(
821 OMPTaskLoopSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000822 Indent() << "#pragma omp taskloop simd";
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000823 PrintOMPExecutableDirective(Node);
824}
825
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000826void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000827 Indent() << "#pragma omp distribute";
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000828 PrintOMPExecutableDirective(Node);
829}
830
Samuel Antao686c70c2016-05-26 17:30:50 +0000831void StmtPrinter::VisitOMPTargetUpdateDirective(
832 OMPTargetUpdateDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000833 Indent() << "#pragma omp target update";
Alexey Bataev7828b252017-11-21 17:08:48 +0000834 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antao686c70c2016-05-26 17:30:50 +0000835}
836
Carlo Bertolli9925f152016-06-27 14:55:37 +0000837void StmtPrinter::VisitOMPDistributeParallelForDirective(
838 OMPDistributeParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000839 Indent() << "#pragma omp distribute parallel for";
Carlo Bertolli9925f152016-06-27 14:55:37 +0000840 PrintOMPExecutableDirective(Node);
841}
842
Kelvin Li4a39add2016-07-05 05:00:15 +0000843void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
844 OMPDistributeParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000845 Indent() << "#pragma omp distribute parallel for simd";
Kelvin Li4a39add2016-07-05 05:00:15 +0000846 PrintOMPExecutableDirective(Node);
847}
848
Kelvin Li787f3fc2016-07-06 04:45:38 +0000849void StmtPrinter::VisitOMPDistributeSimdDirective(
850 OMPDistributeSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000851 Indent() << "#pragma omp distribute simd";
Kelvin Li787f3fc2016-07-06 04:45:38 +0000852 PrintOMPExecutableDirective(Node);
853}
854
Kelvin Lia579b912016-07-14 02:54:56 +0000855void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
856 OMPTargetParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000857 Indent() << "#pragma omp target parallel for simd";
Kelvin Lia579b912016-07-14 02:54:56 +0000858 PrintOMPExecutableDirective(Node);
859}
860
Kelvin Li986330c2016-07-20 22:57:10 +0000861void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000862 Indent() << "#pragma omp target simd";
Kelvin Li986330c2016-07-20 22:57:10 +0000863 PrintOMPExecutableDirective(Node);
864}
865
Kelvin Li02532872016-08-05 14:37:37 +0000866void StmtPrinter::VisitOMPTeamsDistributeDirective(
867 OMPTeamsDistributeDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000868 Indent() << "#pragma omp teams distribute";
Kelvin Li02532872016-08-05 14:37:37 +0000869 PrintOMPExecutableDirective(Node);
870}
871
Kelvin Li4e325f72016-10-25 12:50:55 +0000872void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
873 OMPTeamsDistributeSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000874 Indent() << "#pragma omp teams distribute simd";
Kelvin Li4e325f72016-10-25 12:50:55 +0000875 PrintOMPExecutableDirective(Node);
876}
877
Kelvin Li579e41c2016-11-30 23:51:03 +0000878void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
879 OMPTeamsDistributeParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000880 Indent() << "#pragma omp teams distribute parallel for simd";
Kelvin Li579e41c2016-11-30 23:51:03 +0000881 PrintOMPExecutableDirective(Node);
882}
883
Kelvin Li7ade93f2016-12-09 03:24:30 +0000884void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
885 OMPTeamsDistributeParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000886 Indent() << "#pragma omp teams distribute parallel for";
Kelvin Li7ade93f2016-12-09 03:24:30 +0000887 PrintOMPExecutableDirective(Node);
888}
889
Kelvin Libf594a52016-12-17 05:48:59 +0000890void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000891 Indent() << "#pragma omp target teams";
Kelvin Libf594a52016-12-17 05:48:59 +0000892 PrintOMPExecutableDirective(Node);
893}
894
Kelvin Li83c451e2016-12-25 04:52:54 +0000895void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
896 OMPTargetTeamsDistributeDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000897 Indent() << "#pragma omp target teams distribute";
Kelvin Li83c451e2016-12-25 04:52:54 +0000898 PrintOMPExecutableDirective(Node);
899}
900
Kelvin Li80e8f562016-12-29 22:16:30 +0000901void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
902 OMPTargetTeamsDistributeParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000903 Indent() << "#pragma omp target teams distribute parallel for";
Kelvin Li80e8f562016-12-29 22:16:30 +0000904 PrintOMPExecutableDirective(Node);
905}
906
Kelvin Li1851df52017-01-03 05:23:48 +0000907void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
908 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000909 Indent() << "#pragma omp target teams distribute parallel for simd";
Kelvin Li1851df52017-01-03 05:23:48 +0000910 PrintOMPExecutableDirective(Node);
911}
912
Kelvin Lida681182017-01-10 18:08:18 +0000913void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
914 OMPTargetTeamsDistributeSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000915 Indent() << "#pragma omp target teams distribute simd";
Kelvin Lida681182017-01-10 18:08:18 +0000916 PrintOMPExecutableDirective(Node);
917}
918
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000919//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000920// Expr printing methods.
921//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000922
Eric Fiselier708afb52019-05-16 21:04:15 +0000923void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
924 OS << Node->getBuiltinStr() << "()";
925}
926
Bill Wendling7c44da22018-10-31 03:48:47 +0000927void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
928 PrintExpr(Node->getSubExpr());
929}
930
Chris Lattner882f7882006-11-04 18:52:07 +0000931void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000932 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +0000933 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
934 return;
935 }
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000936 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
937 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000938 if (Node->hasTemplateKeyword())
939 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000940 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000941 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +0000942 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000943}
944
John McCall8cd78132009-11-19 22:55:06 +0000945void StmtPrinter::VisitDependentScopeDeclRefExpr(
946 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000947 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
948 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000949 if (Node->hasTemplateKeyword())
950 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000951 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000952 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +0000953 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000954}
955
John McCalld14a8642009-11-21 08:51:07 +0000956void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000957 if (Node->getQualifier())
958 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000959 if (Node->hasTemplateKeyword())
960 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000961 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000962 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +0000963 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000964}
965
Alex Lorenzf7579612017-10-26 00:56:54 +0000966static bool isImplicitSelf(const Expr *E) {
967 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000968 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
Alex Lorenzf7579612017-10-26 00:56:54 +0000969 if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000970 DRE->getBeginLoc().isInvalid())
Alex Lorenzf7579612017-10-26 00:56:54 +0000971 return true;
972 }
973 }
974 return false;
975}
976
Steve Naroffe46504b2007-11-12 14:29:37 +0000977void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000978 if (Node->getBase()) {
Alex Lorenzf7579612017-10-26 00:56:54 +0000979 if (!Policy.SuppressImplicitBase ||
980 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
981 PrintExpr(Node->getBase());
982 OS << (Node->isArrow() ? "->" : ".");
983 }
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000984 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000985 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000986}
987
Steve Naroffec944032008-05-30 00:40:33 +0000988void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000989 if (Node->isSuperReceiver())
990 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +0000991 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000992 PrintExpr(Node->getBase());
993 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +0000994 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
995 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +0000996 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000997
Alex Lorenz5a5a6542018-05-23 00:52:20 +0000998 if (Node->isImplicitProperty()) {
999 if (const auto *Getter = Node->getImplicitPropertyGetter())
1000 Getter->getSelector().print(OS);
1001 else
1002 OS << SelectorTable::getPropertyNameFromSetterSelector(
1003 Node->getImplicitPropertySetter()->getSelector());
1004 } else
John McCallb7bd14f2010-12-02 01:19:52 +00001005 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001006}
1007
Ted Kremeneke65b0862012-03-06 20:05:56 +00001008void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001009 PrintExpr(Node->getBaseExpr());
1010 OS << "[";
1011 PrintExpr(Node->getKeyExpr());
1012 OS << "]";
1013}
1014
Chris Lattner6307f192008-08-10 01:53:14 +00001015void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Bruno Ricci17ff0262018-10-27 19:21:19 +00001016 OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
Anders Carlsson625bfc82007-07-21 05:21:51 +00001017}
1018
Steve Naroffae4143e2007-04-26 20:39:23 +00001019void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001020 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +00001021
1022 switch (Node->getKind()) {
1023 case CharacterLiteral::Ascii: break; // no prefix.
1024 case CharacterLiteral::Wide: OS << 'L'; break;
Aaron Ballman9a17c852016-01-07 20:59:26 +00001025 case CharacterLiteral::UTF8: OS << "u8"; break;
Douglas Gregorfb65e592011-07-27 05:40:30 +00001026 case CharacterLiteral::UTF16: OS << 'u'; break;
1027 case CharacterLiteral::UTF32: OS << 'U'; break;
1028 }
1029
Chris Lattner666115c2007-07-13 23:58:20 +00001030 switch (value) {
1031 case '\\':
1032 OS << "'\\\\'";
1033 break;
1034 case '\'':
1035 OS << "'\\''";
1036 break;
1037 case '\a':
1038 // TODO: K&R: the meaning of '\\a' is different in traditional C
1039 OS << "'\\a'";
1040 break;
1041 case '\b':
1042 OS << "'\\b'";
1043 break;
1044 // Nonstandard escape sequence.
1045 /*case '\e':
1046 OS << "'\\e'";
1047 break;*/
1048 case '\f':
1049 OS << "'\\f'";
1050 break;
1051 case '\n':
1052 OS << "'\\n'";
1053 break;
1054 case '\r':
1055 OS << "'\\r'";
1056 break;
1057 case '\t':
1058 OS << "'\\t'";
1059 break;
1060 case '\v':
1061 OS << "'\\v'";
1062 break;
1063 default:
Steven Watanabee43ae192016-02-13 02:31:28 +00001064 // A character literal might be sign-extended, which
1065 // would result in an invalid \U escape sequence.
1066 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1067 // are not correctly handled.
1068 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1069 value &= 0xFFu;
Jordan Rose00d1b592013-02-08 22:30:27 +00001070 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +00001071 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +00001072 else if (value < 256)
1073 OS << "'\\x" << llvm::format("%02x", value) << "'";
1074 else if (value <= 0xFFFF)
1075 OS << "'\\u" << llvm::format("%04x", value) << "'";
1076 else
1077 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001078 }
Steve Naroffae4143e2007-04-26 20:39:23 +00001079}
1080
Alex Lorenz36070ed2017-08-17 13:41:55 +00001081/// Prints the given expression using the original source text. Returns true on
1082/// success, false otherwise.
1083static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1084 const ASTContext *Context) {
1085 if (!Context)
1086 return false;
1087 bool Invalid = false;
1088 StringRef Source = Lexer::getSourceText(
1089 CharSourceRange::getTokenRange(E->getSourceRange()),
1090 Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1091 if (!Invalid) {
1092 OS << Source;
1093 return true;
1094 }
1095 return false;
1096}
1097
Steve Naroffdf7855b2007-02-21 23:46:25 +00001098void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Alex Lorenz36070ed2017-08-17 13:41:55 +00001099 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1100 return;
Chris Lattner06430412007-05-21 05:45:03 +00001101 bool isSigned = Node->getType()->isSignedIntegerType();
1102 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +00001103
Chris Lattner06430412007-05-21 05:45:03 +00001104 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +00001105 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001106 default: llvm_unreachable("Unexpected type for integer literal!");
David Majnemerbe09e8e2015-03-06 18:04:22 +00001107 case BuiltinType::Char_S:
1108 case BuiltinType::Char_U: OS << "i8"; break;
David Majnemer65a407c2014-06-21 18:46:07 +00001109 case BuiltinType::UChar: OS << "Ui8"; break;
1110 case BuiltinType::Short: OS << "i16"; break;
1111 case BuiltinType::UShort: OS << "Ui16"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001112 case BuiltinType::Int: break; // no suffix.
1113 case BuiltinType::UInt: OS << 'U'; break;
1114 case BuiltinType::Long: OS << 'L'; break;
1115 case BuiltinType::ULong: OS << "UL"; break;
1116 case BuiltinType::LongLong: OS << "LL"; break;
1117 case BuiltinType::ULongLong: OS << "ULL"; break;
1118 }
Chris Lattner882f7882006-11-04 18:52:07 +00001119}
Benjamin Kramer8a526762012-09-20 14:07:17 +00001120
Leonard Chandb01c3a2018-06-20 17:19:40 +00001121void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1122 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1123 return;
1124 OS << Node->getValueAsString(/*Radix=*/10);
1125
1126 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1127 default: llvm_unreachable("Unexpected type for fixed point literal!");
1128 case BuiltinType::ShortFract: OS << "hr"; break;
1129 case BuiltinType::ShortAccum: OS << "hk"; break;
1130 case BuiltinType::UShortFract: OS << "uhr"; break;
1131 case BuiltinType::UShortAccum: OS << "uhk"; break;
1132 case BuiltinType::Fract: OS << "r"; break;
1133 case BuiltinType::Accum: OS << "k"; break;
1134 case BuiltinType::UFract: OS << "ur"; break;
1135 case BuiltinType::UAccum: OS << "uk"; break;
1136 case BuiltinType::LongFract: OS << "lr"; break;
1137 case BuiltinType::LongAccum: OS << "lk"; break;
1138 case BuiltinType::ULongFract: OS << "ulr"; break;
1139 case BuiltinType::ULongAccum: OS << "ulk"; break;
1140 }
1141}
1142
Benjamin Kramer8a526762012-09-20 14:07:17 +00001143static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1144 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001145 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +00001146 Node->getValue().toString(Str);
1147 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001148 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1149 OS << '.'; // Trailing dot in order to separate from ints.
1150
1151 if (!PrintSuffix)
1152 return;
1153
1154 // Emit suffixes. Float literals are always a builtin float type.
1155 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1156 default: llvm_unreachable("Unexpected type for float literal!");
1157 case BuiltinType::Half: break; // FIXME: suffix?
1158 case BuiltinType::Double: break; // no suffix.
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00001159 case BuiltinType::Float16: OS << "F16"; break;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001160 case BuiltinType::Float: OS << 'F'; break;
1161 case BuiltinType::LongDouble: OS << 'L'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001162 case BuiltinType::Float128: OS << 'Q'; break;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001163 }
1164}
1165
1166void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Alex Lorenz36070ed2017-08-17 13:41:55 +00001167 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1168 return;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001169 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +00001170}
Chris Lattner1c20a172007-08-26 03:42:43 +00001171
1172void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1173 PrintExpr(Node->getSubExpr());
1174 OS << "i";
1175}
1176
Steve Naroffdf7855b2007-02-21 23:46:25 +00001177void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +00001178 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +00001179}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001180
Chris Lattner882f7882006-11-04 18:52:07 +00001181void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1182 OS << "(";
1183 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +00001184 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +00001185}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001186
Chris Lattner882f7882006-11-04 18:52:07 +00001187void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +00001188 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +00001189 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +00001190
Eli Friedman1cf25362009-06-14 22:39:26 +00001191 // Print a space if this is an "identifier operator" like __real, or if
1192 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +00001193 switch (Node->getOpcode()) {
1194 default: break;
John McCalle3027922010-08-25 11:45:40 +00001195 case UO_Real:
1196 case UO_Imag:
1197 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +00001198 OS << ' ';
1199 break;
John McCalle3027922010-08-25 11:45:40 +00001200 case UO_Plus:
1201 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +00001202 if (isa<UnaryOperator>(Node->getSubExpr()))
1203 OS << ' ';
1204 break;
Chris Lattnere5b60442007-08-23 21:46:40 +00001205 }
1206 }
Chris Lattner882f7882006-11-04 18:52:07 +00001207 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattner15768702006-11-05 23:54:51 +00001209 if (Node->isPostfix())
1210 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +00001211}
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001212
Douglas Gregor882211c2010-04-28 22:16:22 +00001213void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1214 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001215 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1216 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +00001217 bool PrintedSomething = false;
1218 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
James Y Knight7281c352015-12-29 22:31:18 +00001219 OffsetOfNode ON = Node->getComponent(i);
1220 if (ON.getKind() == OffsetOfNode::Array) {
Douglas Gregor882211c2010-04-28 22:16:22 +00001221 // Array node
1222 OS << "[";
1223 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1224 OS << "]";
1225 PrintedSomething = true;
1226 continue;
1227 }
Douglas Gregord1702062010-04-29 00:18:15 +00001228
1229 // Skip implicit base indirections.
James Y Knight7281c352015-12-29 22:31:18 +00001230 if (ON.getKind() == OffsetOfNode::Base)
Douglas Gregord1702062010-04-29 00:18:15 +00001231 continue;
1232
Douglas Gregor882211c2010-04-28 22:16:22 +00001233 // Field or identifier node.
1234 IdentifierInfo *Id = ON.getFieldName();
1235 if (!Id)
1236 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001237
Douglas Gregor882211c2010-04-28 22:16:22 +00001238 if (PrintedSomething)
1239 OS << ".";
1240 else
1241 PrintedSomething = true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001242 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +00001243 }
1244 OS << ")";
1245}
1246
Peter Collingbournee190dee2011-03-11 19:24:49 +00001247void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1248 switch(Node->getKind()) {
1249 case UETT_SizeOf:
1250 OS << "sizeof";
1251 break;
1252 case UETT_AlignOf:
Richard Smith301bc212016-05-19 01:39:10 +00001253 if (Policy.Alignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001254 OS << "alignof";
Richard Smith301bc212016-05-19 01:39:10 +00001255 else if (Policy.UnderscoreAlignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001256 OS << "_Alignof";
1257 else
1258 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001259 break;
Richard Smith6822bd72018-10-26 19:26:45 +00001260 case UETT_PreferredAlignOf:
1261 OS << "__alignof";
1262 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001263 case UETT_VecStep:
1264 OS << "vec_step";
1265 break;
Alexey Bataev00396512015-07-02 03:40:19 +00001266 case UETT_OpenMPRequiredSimdAlign:
1267 OS << "__builtin_omp_required_simd_align";
1268 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001269 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001270 if (Node->isArgumentType()) {
1271 OS << '(';
1272 Node->getArgumentType().print(OS, Policy);
1273 OS << ')';
1274 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +00001275 OS << " ";
1276 PrintExpr(Node->getArgumentExpr());
1277 }
Chris Lattner882f7882006-11-04 18:52:07 +00001278}
Peter Collingbourne91147592011-04-15 00:35:48 +00001279
1280void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1281 OS << "_Generic(";
1282 PrintExpr(Node->getControllingExpr());
Bruno Ricci1ec7fd32019-01-29 12:57:11 +00001283 for (const GenericSelectionExpr::Association &Assoc : Node->associations()) {
Peter Collingbourne91147592011-04-15 00:35:48 +00001284 OS << ", ";
Bruno Ricci1ec7fd32019-01-29 12:57:11 +00001285 QualType T = Assoc.getType();
Peter Collingbourne91147592011-04-15 00:35:48 +00001286 if (T.isNull())
1287 OS << "default";
1288 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001289 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001290 OS << ": ";
Bruno Ricci1ec7fd32019-01-29 12:57:11 +00001291 PrintExpr(Assoc.getAssociationExpr());
Peter Collingbourne91147592011-04-15 00:35:48 +00001292 }
1293 OS << ")";
1294}
1295
Chris Lattner882f7882006-11-04 18:52:07 +00001296void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001297 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001298 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001299 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001300 OS << "]";
1301}
1302
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001303void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1304 PrintExpr(Node->getBase());
1305 OS << "[";
1306 if (Node->getLowerBound())
1307 PrintExpr(Node->getLowerBound());
Alexey Bataeved5fb672015-09-11 04:54:28 +00001308 if (Node->getColonLoc().isValid()) {
1309 OS << ":";
1310 if (Node->getLength())
1311 PrintExpr(Node->getLength());
1312 }
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001313 OS << "]";
1314}
1315
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001316void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001317 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001318 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1319 // Don't print any defaulted arguments
1320 break;
1321 }
1322
Chris Lattner882f7882006-11-04 18:52:07 +00001323 if (i) OS << ", ";
1324 PrintExpr(Call->getArg(i));
1325 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001326}
1327
1328void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1329 PrintExpr(Call->getCallee());
1330 OS << "(";
1331 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001332 OS << ")";
1333}
Alex Lorenzf7579612017-10-26 00:56:54 +00001334
1335static bool isImplicitThis(const Expr *E) {
1336 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1337 return TE->isImplicit();
1338 return false;
1339}
1340
Chris Lattner882f7882006-11-04 18:52:07 +00001341void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Alex Lorenzf7579612017-10-26 00:56:54 +00001342 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1343 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001344
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001345 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
Alex Lorenzf7579612017-10-26 00:56:54 +00001346 FieldDecl *ParentDecl =
1347 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1348 : nullptr;
David Blaikie8fab8e52012-11-12 19:12:12 +00001349
Alex Lorenzf7579612017-10-26 00:56:54 +00001350 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1351 OS << (Node->isArrow() ? "->" : ".");
1352 }
David Blaikie8fab8e52012-11-12 19:12:12 +00001353
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001354 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001355 if (FD->isAnonymousStructOrUnion())
1356 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001357
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001358 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1359 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001360 if (Node->hasTemplateKeyword())
1361 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001362 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001363 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00001364 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001365}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001366
Steve Naroffe87026a2009-07-24 17:54:45 +00001367void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1368 PrintExpr(Node->getBase());
1369 OS << (Node->isArrow() ? "->isa" : ".isa");
1370}
1371
Nate Begemance4d7fc2008-04-18 23:10:10 +00001372void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001373 PrintExpr(Node->getBase());
1374 OS << ".";
1375 OS << Node->getAccessor().getName();
1376}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001377
Douglas Gregorf19b2312008-10-28 15:36:24 +00001378void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001379 OS << '(';
1380 Node->getTypeAsWritten().print(OS, Policy);
1381 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001382 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001383}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001384
Steve Naroff57eb2c52007-07-19 21:32:11 +00001385void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001386 OS << '(';
1387 Node->getType().print(OS, Policy);
1388 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001389 PrintExpr(Node->getInitializer());
1390}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001391
Steve Naroff7a5af782007-07-13 16:58:59 +00001392void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001393 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001394 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001395}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001396
Chris Lattner882f7882006-11-04 18:52:07 +00001397void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1398 PrintExpr(Node->getLHS());
1399 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1400 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001401}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001402
Chris Lattner86928112007-08-25 02:00:02 +00001403void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1404 PrintExpr(Node->getLHS());
1405 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1406 PrintExpr(Node->getRHS());
1407}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001408
Chris Lattner882f7882006-11-04 18:52:07 +00001409void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1410 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001411 OS << " ? ";
1412 PrintExpr(Node->getLHS());
1413 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001414 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001415}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001416
Chris Lattnereefa10e2007-05-28 06:56:27 +00001417// GNU extensions.
1418
John McCallc07a0c72011-02-17 10:25:35 +00001419void
1420StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1421 PrintExpr(Node->getCommon());
1422 OS << " ?: ";
1423 PrintExpr(Node->getFalseExpr());
1424}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001425
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001426void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001427 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001428}
1429
Chris Lattner366727f2007-07-24 16:58:17 +00001430void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1431 OS << "(";
1432 PrintRawCompoundStmt(E->getSubStmt());
1433 OS << ")";
1434}
1435
Steve Naroff9efdabc2007-08-03 21:21:27 +00001436void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1437 OS << "__builtin_choose_expr(";
1438 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001439 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001440 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001441 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001442 PrintExpr(Node->getRHS());
1443 OS << ")";
1444}
Chris Lattner366727f2007-07-24 16:58:17 +00001445
Douglas Gregor3be4b122008-11-29 04:51:27 +00001446void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1447 OS << "__null";
1448}
1449
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001450void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1451 OS << "__builtin_shufflevector(";
1452 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1453 if (i) OS << ", ";
1454 PrintExpr(Node->getExpr(i));
1455 }
1456 OS << ")";
1457}
1458
Hal Finkelc4d7c822013-09-18 03:29:45 +00001459void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1460 OS << "__builtin_convertvector(";
1461 PrintExpr(Node->getSrcExpr());
1462 OS << ", ";
1463 Node->getType().print(OS, Policy);
1464 OS << ")";
1465}
1466
Anders Carlsson4692db02007-08-31 04:56:16 +00001467void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001468 if (Node->getSyntacticForm()) {
1469 Visit(Node->getSyntacticForm());
1470 return;
1471 }
1472
Richard Smithd0e102f2015-01-30 02:04:26 +00001473 OS << "{";
Anders Carlsson4692db02007-08-31 04:56:16 +00001474 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1475 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001476 if (Node->getInit(i))
1477 PrintExpr(Node->getInit(i));
1478 else
Richard Smithd0e102f2015-01-30 02:04:26 +00001479 OS << "{}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001480 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001481 OS << "}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001482}
1483
Richard Smith410306b2016-12-12 02:53:20 +00001484void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1485 // There's no way to express this expression in any of our supported
1486 // languages, so just emit something terse and (hopefully) clear.
1487 OS << "{";
1488 PrintExpr(Node->getSubExpr());
1489 OS << "}";
1490}
1491
1492void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1493 OS << "*";
1494}
1495
Nate Begeman5ec4b312009-08-10 23:49:36 +00001496void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
Richard Smithd0e102f2015-01-30 02:04:26 +00001497 OS << "(";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001498 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1499 if (i) OS << ", ";
1500 PrintExpr(Node->getExpr(i));
1501 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001502 OS << ")";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001503}
1504
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001505void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Justin Bognercb337032015-05-28 22:19:36 +00001506 bool NeedsEquals = true;
David Majnemerf7e36092016-06-23 00:15:04 +00001507 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1508 if (D.isFieldDesignator()) {
1509 if (D.getDotLoc().isInvalid()) {
1510 if (IdentifierInfo *II = D.getFieldName()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001511 OS << II->getName() << ":";
Justin Bognercb337032015-05-28 22:19:36 +00001512 NeedsEquals = false;
1513 }
Serge Pavloveb57aa62014-06-20 17:08:28 +00001514 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001515 OS << "." << D.getFieldName()->getName();
Serge Pavloveb57aa62014-06-20 17:08:28 +00001516 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001517 } else {
1518 OS << "[";
David Majnemerf7e36092016-06-23 00:15:04 +00001519 if (D.isArrayDesignator()) {
1520 PrintExpr(Node->getArrayIndex(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001521 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001522 PrintExpr(Node->getArrayRangeStart(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001523 OS << " ... ";
David Majnemerf7e36092016-06-23 00:15:04 +00001524 PrintExpr(Node->getArrayRangeEnd(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001525 }
1526 OS << "]";
1527 }
1528 }
1529
Justin Bognercb337032015-05-28 22:19:36 +00001530 if (NeedsEquals)
1531 OS << " = ";
1532 else
1533 OS << " ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001534 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001535}
1536
Yunzhong Gaocb779302015-06-10 00:27:52 +00001537void StmtPrinter::VisitDesignatedInitUpdateExpr(
1538 DesignatedInitUpdateExpr *Node) {
1539 OS << "{";
1540 OS << "/*base*/";
1541 PrintExpr(Node->getBase());
1542 OS << ", ";
1543
1544 OS << "/*updater*/";
1545 PrintExpr(Node->getUpdater());
1546 OS << "}";
1547}
1548
1549void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1550 OS << "/*no init*/";
1551}
1552
Douglas Gregor0202cb42009-01-29 17:44:32 +00001553void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Richard Smith301bc212016-05-19 01:39:10 +00001554 if (Node->getType()->getAsCXXRecordDecl()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001555 OS << "/*implicit*/";
1556 Node->getType().print(OS, Policy);
1557 OS << "()";
1558 } else {
1559 OS << "/*implicit*/(";
1560 Node->getType().print(OS, Policy);
1561 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001562 if (Node->getType()->isRecordType())
1563 OS << "{}";
1564 else
1565 OS << 0;
1566 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001567}
1568
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001569void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001570 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001571 PrintExpr(Node->getSubExpr());
1572 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001573 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001574 OS << ")";
1575}
1576
John McCallfe96e0b2011-11-06 09:01:30 +00001577void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1578 PrintExpr(Node->getSyntacticForm());
1579}
1580
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001581void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001582 const char *Name = nullptr;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001583 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001584#define BUILTIN(ID, TYPE, ATTRS)
1585#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1586 case AtomicExpr::AO ## ID: \
1587 Name = #ID "("; \
1588 break;
1589#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001590 }
1591 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001592
1593 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001594 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001595 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
Yaxun Liu39195062017-08-04 18:16:31 +00001596 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1597 Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001598 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001599 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001600 }
Richard Smithfeea8832012-04-12 05:08:17 +00001601 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1602 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001603 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001604 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001605 }
Richard Smithfeea8832012-04-12 05:08:17 +00001606 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1607 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001608 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001609 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001610 }
Yaxun Liu39195062017-08-04 18:16:31 +00001611 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1612 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
Richard Smithdf6bee82013-05-01 19:02:43 +00001613 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001614 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001615 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001616 if (Node->isCmpXChg()) {
1617 OS << ", ";
1618 PrintExpr(Node->getOrderFail());
1619 }
1620 OS << ")";
1621}
1622
Chris Lattnereefa10e2007-05-28 06:56:27 +00001623// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001624void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
Douglas Gregor993603d2008-11-14 16:09:21 +00001625 OverloadedOperatorKind Kind = Node->getOperator();
1626 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1627 if (Node->getNumArgs() == 1) {
Richard Smith7fa2b742019-06-14 20:01:51 +00001628 OS << getOperatorSpelling(Kind) << ' ';
Douglas Gregor993603d2008-11-14 16:09:21 +00001629 PrintExpr(Node->getArg(0));
1630 } else {
1631 PrintExpr(Node->getArg(0));
Richard Smith7fa2b742019-06-14 20:01:51 +00001632 OS << ' ' << getOperatorSpelling(Kind);
Douglas Gregor993603d2008-11-14 16:09:21 +00001633 }
Eli Friedman8e236422012-10-12 22:45:14 +00001634 } else if (Kind == OO_Arrow) {
1635 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001636 } else if (Kind == OO_Call) {
1637 PrintExpr(Node->getArg(0));
1638 OS << '(';
1639 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1640 if (ArgIdx > 1)
1641 OS << ", ";
1642 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1643 PrintExpr(Node->getArg(ArgIdx));
1644 }
1645 OS << ')';
1646 } else if (Kind == OO_Subscript) {
1647 PrintExpr(Node->getArg(0));
1648 OS << '[';
1649 PrintExpr(Node->getArg(1));
1650 OS << ']';
1651 } else if (Node->getNumArgs() == 1) {
Richard Smith7fa2b742019-06-14 20:01:51 +00001652 OS << getOperatorSpelling(Kind) << ' ';
Douglas Gregor993603d2008-11-14 16:09:21 +00001653 PrintExpr(Node->getArg(0));
1654 } else if (Node->getNumArgs() == 2) {
1655 PrintExpr(Node->getArg(0));
Richard Smith7fa2b742019-06-14 20:01:51 +00001656 OS << ' ' << getOperatorSpelling(Kind) << ' ';
Douglas Gregor993603d2008-11-14 16:09:21 +00001657 PrintExpr(Node->getArg(1));
1658 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001659 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001660 }
1661}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001662
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001663void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001664 // If we have a conversion operator call only print the argument.
1665 CXXMethodDecl *MD = Node->getMethodDecl();
1666 if (MD && isa<CXXConversionDecl>(MD)) {
1667 PrintExpr(Node->getImplicitObjectArgument());
1668 return;
1669 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001670 VisitCallExpr(cast<CallExpr>(Node));
1671}
1672
Peter Collingbourne41f85462011-02-09 21:07:24 +00001673void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1674 PrintExpr(Node->getCallee());
1675 OS << "<<<";
1676 PrintCallArgs(Node->getConfig());
1677 OS << ">>>(";
1678 PrintCallArgs(Node);
1679 OS << ")";
1680}
1681
Douglas Gregore200adc2008-10-27 19:41:14 +00001682void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1683 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001684 Node->getTypeAsWritten().print(OS, Policy);
1685 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001686 PrintExpr(Node->getSubExpr());
1687 OS << ")";
1688}
1689
Douglas Gregore200adc2008-10-27 19:41:14 +00001690void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1691 VisitCXXNamedCastExpr(Node);
1692}
1693
1694void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1695 VisitCXXNamedCastExpr(Node);
1696}
1697
1698void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1699 VisitCXXNamedCastExpr(Node);
1700}
1701
1702void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1703 VisitCXXNamedCastExpr(Node);
1704}
1705
Erik Pilkingtoneee944e2019-07-02 18:28:13 +00001706void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1707 OS << "__builtin_bit_cast(";
1708 Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1709 OS << ", ";
1710 PrintExpr(Node->getSubExpr());
1711 OS << ")";
1712}
1713
Sebastian Redlc4704762008-11-11 11:37:55 +00001714void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1715 OS << "typeid(";
1716 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001717 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001718 } else {
1719 PrintExpr(Node->getExprOperand());
1720 }
1721 OS << ")";
1722}
1723
Francois Pichet9f4f2072010-09-08 12:20:18 +00001724void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1725 OS << "__uuidof(";
1726 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001727 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001728 } else {
1729 PrintExpr(Node->getExprOperand());
1730 }
1731 OS << ")";
1732}
1733
John McCall5e77d762013-04-16 07:28:30 +00001734void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1735 PrintExpr(Node->getBaseExpr());
1736 if (Node->isArrow())
1737 OS << "->";
1738 else
1739 OS << ".";
1740 if (NestedNameSpecifier *Qualifier =
1741 Node->getQualifierLoc().getNestedNameSpecifier())
1742 Qualifier->print(OS, Policy);
1743 OS << Node->getPropertyDecl()->getDeclName();
1744}
1745
Alexey Bataevf7630272015-11-25 12:01:00 +00001746void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1747 PrintExpr(Node->getBase());
1748 OS << "[";
1749 PrintExpr(Node->getIdx());
1750 OS << "]";
1751}
1752
Richard Smithc67fdd42012-03-07 08:35:16 +00001753void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1754 switch (Node->getLiteralOperatorKind()) {
1755 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001756 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001757 break;
1758 case UserDefinedLiteral::LOK_Template: {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001759 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
Richard Smith29e95952012-03-09 10:10:02 +00001760 const TemplateArgumentList *Args =
1761 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1762 assert(Args);
David Majnemer314b72f2015-04-05 05:32:54 +00001763
1764 if (Args->size() != 1) {
Richard Smithe87aeb32015-10-08 00:17:59 +00001765 OS << "operator\"\"" << Node->getUDSuffix()->getName();
Serge Pavlov03e672c2017-11-28 16:14:14 +00001766 printTemplateArgumentList(OS, Args->asArray(), Policy);
David Majnemer314b72f2015-04-05 05:32:54 +00001767 OS << "()";
1768 return;
1769 }
1770
Richard Smith29e95952012-03-09 10:10:02 +00001771 const TemplateArgument &Pack = Args->get(0);
Aaron Ballman2a89e852014-07-15 21:32:31 +00001772 for (const auto &P : Pack.pack_elements()) {
1773 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001774 OS << C;
1775 }
1776 break;
1777 }
Richard Smith75025ba2012-03-08 09:02:38 +00001778 case UserDefinedLiteral::LOK_Integer: {
1779 // Print integer literal without suffix.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001780 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
Richard Smith75025ba2012-03-08 09:02:38 +00001781 OS << Int->getValue().toString(10, /*isSigned*/false);
1782 break;
1783 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001784 case UserDefinedLiteral::LOK_Floating: {
1785 // Print floating literal without suffix.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001786 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
Benjamin Kramer8a526762012-09-20 14:07:17 +00001787 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1788 break;
1789 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001790 case UserDefinedLiteral::LOK_String:
1791 case UserDefinedLiteral::LOK_Character:
1792 PrintExpr(Node->getCookedLiteral());
1793 break;
1794 }
1795 OS << Node->getUDSuffix()->getName();
1796}
1797
Chris Lattnereefa10e2007-05-28 06:56:27 +00001798void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1799 OS << (Node->getValue() ? "true" : "false");
1800}
1801
Sebastian Redl576fd422009-05-10 18:38:11 +00001802void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1803 OS << "nullptr";
1804}
1805
Douglas Gregor97a9c812008-11-04 14:32:21 +00001806void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1807 OS << "this";
1808}
1809
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001810void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001811 if (!Node->getSubExpr())
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001812 OS << "throw";
1813 else {
1814 OS << "throw ";
1815 PrintExpr(Node->getSubExpr());
1816 }
1817}
1818
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001819void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001820 // Nothing to print: we picked up the default argument.
1821}
1822
1823void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1824 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001825}
1826
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001827void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001828 Node->getType().print(OS, Policy);
Richard Smith1ae689c2015-01-28 22:06:01 +00001829 // If there are no parens, this is list-initialization, and the braces are
1830 // part of the syntax of the inner construct.
1831 if (Node->getLParenLoc().isValid())
1832 OS << "(";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001833 PrintExpr(Node->getSubExpr());
Richard Smith1ae689c2015-01-28 22:06:01 +00001834 if (Node->getLParenLoc().isValid())
1835 OS << ")";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001836}
1837
Anders Carlsson993a4b32009-05-30 20:03:25 +00001838void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1839 PrintExpr(Node->getSubExpr());
1840}
1841
Douglas Gregordd04d332009-01-16 18:33:17 +00001842void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001843 Node->getType().print(OS, Policy);
Richard Smithed83ebd2015-02-05 07:02:11 +00001844 if (Node->isStdInitListInitialization())
1845 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1846 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001847 OS << "{";
1848 else
1849 OS << "(";
Douglas Gregordd04d332009-01-16 18:33:17 +00001850 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001851 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001852 Arg != ArgEnd; ++Arg) {
Benjamin Kramerf48ee442015-07-18 14:35:53 +00001853 if ((*Arg)->isDefaultArgument())
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001854 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001855 if (Arg != Node->arg_begin())
1856 OS << ", ";
1857 PrintExpr(*Arg);
1858 }
Richard Smithed83ebd2015-02-05 07:02:11 +00001859 if (Node->isStdInitListInitialization())
1860 /* See above. */;
1861 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001862 OS << "}";
1863 else
1864 OS << ")";
Douglas Gregordd04d332009-01-16 18:33:17 +00001865}
1866
Douglas Gregore31e6062012-02-07 10:09:13 +00001867void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1868 OS << '[';
1869 bool NeedComma = false;
1870 switch (Node->getCaptureDefault()) {
1871 case LCD_None:
1872 break;
1873
1874 case LCD_ByCopy:
1875 OS << '=';
1876 NeedComma = true;
1877 break;
1878
1879 case LCD_ByRef:
1880 OS << '&';
1881 NeedComma = true;
1882 break;
1883 }
1884 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1885 CEnd = Node->explicit_capture_end();
1886 C != CEnd;
1887 ++C) {
Richard Trieu931638e2017-11-11 00:54:25 +00001888 if (C->capturesVLAType())
1889 continue;
1890
Douglas Gregore31e6062012-02-07 10:09:13 +00001891 if (NeedComma)
1892 OS << ", ";
1893 NeedComma = true;
1894
1895 switch (C->getCaptureKind()) {
1896 case LCK_This:
1897 OS << "this";
1898 break;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001899
Faisal Validc6b5962016-03-21 09:25:37 +00001900 case LCK_StarThis:
1901 OS << "*this";
1902 break;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001903
Douglas Gregore31e6062012-02-07 10:09:13 +00001904 case LCK_ByRef:
James Dennettdd2ffea22015-05-07 18:48:18 +00001905 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
Douglas Gregore31e6062012-02-07 10:09:13 +00001906 OS << '&';
1907 OS << C->getCapturedVar()->getName();
1908 break;
1909
1910 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001911 OS << C->getCapturedVar()->getName();
1912 break;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001913
Alexey Bataev39c81e22014-08-28 04:28:19 +00001914 case LCK_VLAType:
1915 llvm_unreachable("VLA type in explicit captures.");
Douglas Gregore31e6062012-02-07 10:09:13 +00001916 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001917
Nicolas Lesser5125a022019-05-05 12:35:12 +00001918 if (C->isPackExpansion())
1919 OS << "...";
1920
James Dennettdd2ffea22015-05-07 18:48:18 +00001921 if (Node->isInitCapture(C))
Richard Smithbb13c9a2013-09-28 04:02:39 +00001922 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001923 }
1924 OS << ']';
1925
Hamza Sood8205a812019-05-04 10:49:46 +00001926 if (!Node->getExplicitTemplateParameters().empty()) {
1927 Node->getTemplateParameterList()->print(
1928 OS, Node->getLambdaClass()->getASTContext(),
1929 /*OmitTemplateKW*/true);
1930 }
1931
Douglas Gregore31e6062012-02-07 10:09:13 +00001932 if (Node->hasExplicitParameters()) {
Hamza Sood8205a812019-05-04 10:49:46 +00001933 OS << '(';
Douglas Gregore31e6062012-02-07 10:09:13 +00001934 CXXMethodDecl *Method = Node->getCallOperator();
1935 NeedComma = false;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001936 for (const auto *P : Method->parameters()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001937 if (NeedComma) {
1938 OS << ", ";
1939 } else {
1940 NeedComma = true;
1941 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00001942 std::string ParamStr = P->getNameAsString();
1943 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001944 }
1945 if (Method->isVariadic()) {
1946 if (NeedComma)
1947 OS << ", ";
1948 OS << "...";
1949 }
1950 OS << ')';
1951
1952 if (Node->isMutable())
1953 OS << " mutable";
1954
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001955 auto *Proto = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001956 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001957
Bill Wendling44426052012-12-20 19:22:21 +00001958 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001959
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001960 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001961 if (Node->hasExplicitResultType()) {
1962 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00001963 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001964 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001965 }
1966
1967 // Print the body.
Douglas Gregore31e6062012-02-07 10:09:13 +00001968 OS << ' ';
Kadir Cetinkaya20c3c4f2019-05-27 16:20:45 +00001969 if (Policy.TerseOutput)
1970 OS << "{}";
1971 else
1972 PrintRawCompoundStmt(Node->getBody());
Douglas Gregore31e6062012-02-07 10:09:13 +00001973}
1974
Douglas Gregor747eb782010-07-08 06:14:04 +00001975void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001976 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001977 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001978 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001979 Node->getType().print(OS, Policy);
1980 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001981}
1982
Sebastian Redlbd150f42008-11-21 19:14:01 +00001983void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1984 if (E->isGlobalNew())
1985 OS << "::";
1986 OS << "new ";
1987 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001988 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001989 OS << "(";
1990 PrintExpr(E->getPlacementArg(0));
1991 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001992 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1993 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001994 OS << ", ";
1995 PrintExpr(E->getPlacementArg(i));
1996 }
1997 OS << ") ";
1998 }
1999 if (E->isParenTypeId())
2000 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002001 std::string TypeS;
Richard Smithb9fb1212019-05-06 03:47:15 +00002002 if (Optional<Expr *> Size = E->getArraySize()) {
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002003 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002004 s << '[';
Richard Smithb9fb1212019-05-06 03:47:15 +00002005 if (*Size)
2006 (*Size)->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002007 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002008 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002009 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002010 if (E->isParenTypeId())
2011 OS << ")";
2012
Sebastian Redl6047f072012-02-16 12:22:20 +00002013 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2014 if (InitStyle) {
2015 if (InitStyle == CXXNewExpr::CallInit)
2016 OS << "(";
2017 PrintExpr(E->getInitializer());
2018 if (InitStyle == CXXNewExpr::CallInit)
2019 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00002020 }
2021}
2022
2023void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2024 if (E->isGlobalDelete())
2025 OS << "::";
2026 OS << "delete ";
2027 if (E->isArrayForm())
2028 OS << "[] ";
2029 PrintExpr(E->getArgument());
2030}
2031
Douglas Gregorad8a3362009-09-04 17:36:40 +00002032void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2033 PrintExpr(E->getBase());
2034 if (E->isArrow())
2035 OS << "->";
2036 else
2037 OS << '.';
2038 if (E->getQualifier())
2039 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00002040 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00002041
Douglas Gregor678f90d2010-02-25 01:56:36 +00002042 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2043 OS << II->getName();
2044 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002045 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00002046}
2047
Anders Carlsson0781ce72009-04-23 02:32:43 +00002048void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithed83ebd2015-02-05 07:02:11 +00002049 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002050 OS << "{";
Richard Smithd59b8322012-12-19 01:39:02 +00002051
Douglas Gregorbaba85d2011-01-24 17:25:03 +00002052 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2053 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2054 // Don't print any defaulted arguments
2055 break;
2056 }
2057
2058 if (i) OS << ", ";
2059 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00002060 }
Richard Smithd59b8322012-12-19 01:39:02 +00002061
Richard Smithed83ebd2015-02-05 07:02:11 +00002062 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002063 OS << "}";
Anders Carlsson0781ce72009-04-23 02:32:43 +00002064}
2065
Richard Smith5179eb72016-06-28 19:03:57 +00002066void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2067 // Parens are printed by the surrounding context.
2068 OS << "<forwarded>";
2069}
2070
Richard Smithcc1b96d2013-06-12 22:31:48 +00002071void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2072 PrintExpr(E->getSubExpr());
2073}
2074
John McCall5d413782010-12-06 08:20:24 +00002075void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00002076 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00002077 PrintExpr(E->getSubExpr());
2078}
2079
Mike Stump11289f42009-09-09 15:08:12 +00002080void
Douglas Gregorce934142009-05-20 18:46:25 +00002081StmtPrinter::VisitCXXUnresolvedConstructExpr(
2082 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002083 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00002084 OS << "(";
2085 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00002086 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00002087 Arg != ArgEnd; ++Arg) {
2088 if (Arg != Node->arg_begin())
2089 OS << ", ";
2090 PrintExpr(*Arg);
2091 }
2092 OS << ")";
2093}
2094
John McCall8cd78132009-11-19 22:55:06 +00002095void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2096 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002097 if (!Node->isImplicitAccess()) {
2098 PrintExpr(Node->getBase());
2099 OS << (Node->isArrow() ? "->" : ".");
2100 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00002101 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2102 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002103 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00002104 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002105 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002106 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00002107 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00002108}
2109
John McCall10eae182009-11-30 22:42:35 +00002110void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002111 if (!Node->isImplicitAccess()) {
2112 PrintExpr(Node->getBase());
2113 OS << (Node->isArrow() ? "->" : ".");
2114 }
John McCall10eae182009-11-30 22:42:35 +00002115 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2116 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002117 if (Node->hasTemplateKeyword())
2118 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002119 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002120 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00002121 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
John McCall10eae182009-11-30 22:42:35 +00002122}
2123
Douglas Gregor29c42f22012-02-24 07:38:34 +00002124static const char *getTypeTraitName(TypeTrait TT) {
2125 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00002126#define TYPE_TRAIT_1(Spelling, Name, Key) \
2127case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00002128#define TYPE_TRAIT_2(Spelling, Name, Key) \
2129case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00002130#define TYPE_TRAIT_N(Spelling, Name, Key) \
2131 case clang::TT_##Name: return #Spelling;
2132#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002133 }
2134 llvm_unreachable("Type trait not covered by switch");
2135}
2136
John Wiegley6242b6a2011-04-28 00:16:57 +00002137static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2138 switch (ATT) {
2139 case ATT_ArrayRank: return "__array_rank";
2140 case ATT_ArrayExtent: return "__array_extent";
2141 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002142 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00002143}
2144
John Wiegleyf9f65842011-04-25 06:54:41 +00002145static const char *getExpressionTraitName(ExpressionTrait ET) {
2146 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00002147 case ET_IsLValueExpr: return "__is_lvalue_expr";
2148 case ET_IsRValueExpr: return "__is_rvalue_expr";
2149 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002150 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00002151}
2152
Douglas Gregor29c42f22012-02-24 07:38:34 +00002153void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2154 OS << getTypeTraitName(E->getTrait()) << "(";
2155 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2156 if (I > 0)
2157 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002158 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00002159 }
2160 OS << ")";
2161}
2162
John Wiegley6242b6a2011-04-28 00:16:57 +00002163void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002164 OS << getTypeTraitName(E->getTrait()) << '(';
2165 E->getQueriedType().print(OS, Policy);
2166 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00002167}
2168
John Wiegleyf9f65842011-04-25 06:54:41 +00002169void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002170 OS << getExpressionTraitName(E->getTrait()) << '(';
2171 PrintExpr(E->getQueriedExpression());
2172 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00002173}
2174
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002175void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2176 OS << "noexcept(";
2177 PrintExpr(E->getOperand());
2178 OS << ")";
2179}
2180
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002181void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002182 PrintExpr(E->getPattern());
2183 OS << "...";
2184}
2185
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002186void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002187 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002188}
2189
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002190void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2191 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002192 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002193}
2194
John McCall7c454bb2011-07-15 05:09:51 +00002195void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2196 SubstNonTypeTemplateParmExpr *Node) {
2197 Visit(Node->getReplacement());
2198}
2199
Richard Smithb15fe3a2012-09-12 00:56:43 +00002200void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2201 OS << *E->getParameterPack();
2202}
2203
Douglas Gregorfe314812011-06-21 17:03:29 +00002204void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2205 PrintExpr(Node->GetTemporaryExpr());
2206}
2207
Richard Smith0f0af192014-11-08 05:07:16 +00002208void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2209 OS << "(";
2210 if (E->getLHS()) {
2211 PrintExpr(E->getLHS());
2212 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2213 }
2214 OS << "...";
2215 if (E->getRHS()) {
2216 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2217 PrintExpr(E->getRHS());
2218 }
2219 OS << ")";
2220}
2221
Richard Smith9f690bd2015-10-27 06:02:45 +00002222// C++ Coroutines TS
2223
2224void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2225 Visit(S->getBody());
2226}
2227
2228void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2229 OS << "co_return";
2230 if (S->getOperand()) {
2231 OS << " ";
2232 Visit(S->getOperand());
2233 }
2234 OS << ";";
2235}
2236
2237void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2238 OS << "co_await ";
2239 PrintExpr(S->getOperand());
2240}
2241
Eric Fiselier20f25cb2017-03-06 23:38:15 +00002242void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2243 OS << "co_await ";
2244 PrintExpr(S->getOperand());
2245}
2246
Richard Smith9f690bd2015-10-27 06:02:45 +00002247void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2248 OS << "co_yield ";
2249 PrintExpr(S->getOperand());
2250}
2251
Mike Stump11289f42009-09-09 15:08:12 +00002252// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00002253
2254void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2255 OS << "@";
2256 VisitStringLiteral(Node->getString());
2257}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002258
Patrick Beard0caa3942012-04-19 00:25:12 +00002259void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00002260 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00002261 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002262}
2263
2264void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2265 OS << "@[ ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002266 ObjCArrayLiteral::child_range Ch = E->children();
2267 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2268 if (I != Ch.begin())
Ted Kremeneke65b0862012-03-06 20:05:56 +00002269 OS << ", ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002270 Visit(*I);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002271 }
2272 OS << " ]";
2273}
2274
2275void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2276 OS << "@{ ";
2277 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2278 if (I > 0)
2279 OS << ", ";
Fangrui Song6907ce22018-07-30 19:24:48 +00002280
Ted Kremeneke65b0862012-03-06 20:05:56 +00002281 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2282 Visit(Element.Key);
2283 OS << " : ";
2284 Visit(Element.Value);
2285 if (Element.isPackExpansion())
2286 OS << "...";
2287 }
2288 OS << " }";
2289}
2290
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002291void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002292 OS << "@encode(";
2293 Node->getEncodedType().print(OS, Policy);
2294 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002295}
2296
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002297void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00002298 OS << "@selector(";
2299 Node->getSelector().print(OS);
2300 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002301}
2302
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002303void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002304 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002305}
2306
Steve Naroffd54978b2007-09-18 23:55:05 +00002307void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2308 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00002309 switch (Mess->getReceiverKind()) {
2310 case ObjCMessageExpr::Instance:
2311 PrintExpr(Mess->getInstanceReceiver());
2312 break;
2313
2314 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002315 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00002316 break;
2317
2318 case ObjCMessageExpr::SuperInstance:
2319 case ObjCMessageExpr::SuperClass:
2320 OS << "Super";
2321 break;
2322 }
2323
Ted Kremeneka06e7122008-05-02 17:32:38 +00002324 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00002325 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00002326 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002327 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002328 } else {
2329 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00002330 if (i < selector.getNumArgs()) {
2331 if (i > 0) OS << ' ';
2332 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002333 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00002334 else
2335 OS << ":";
2336 }
2337 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00002338
Steve Naroffc6814ea2007-10-02 20:01:56 +00002339 PrintExpr(Mess->getArg(i));
2340 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002341 }
2342 OS << "]";
2343}
2344
Ted Kremeneke65b0862012-03-06 20:05:56 +00002345void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2346 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2347}
2348
John McCall31168b02011-06-15 23:02:42 +00002349void
2350StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2351 PrintExpr(E->getSubExpr());
2352}
2353
2354void
2355StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002356 OS << '(' << E->getBridgeKindName();
2357 E->getType().print(OS, Policy);
2358 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00002359 PrintExpr(E->getSubExpr());
2360}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002361
Steve Naroffc540d662008-09-03 18:15:37 +00002362void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002363 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00002364 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00002365
Steve Naroffc540d662008-09-03 18:15:37 +00002366 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002367
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002368 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00002369 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002370 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00002371 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00002372 for (BlockDecl::param_iterator AI = BD->param_begin(),
2373 E = BD->param_end(); AI != E; ++AI) {
2374 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002375 std::string ParamStr = (*AI)->getNameAsString();
2376 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00002377 }
Mike Stump11289f42009-09-09 15:08:12 +00002378
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00002379 const auto *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00002380 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002381 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00002382 OS << "...";
2383 }
2384 OS << ')';
2385 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00002386 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00002387}
2388
Fangrui Song6907ce22018-07-30 19:24:48 +00002389void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
Ted Kremenekf551f322011-11-30 22:08:08 +00002390 PrintExpr(Node->getSourceExpr());
2391}
John McCall8d69a212010-11-15 23:31:06 +00002392
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002393void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2394 // TODO: Print something reasonable for a TypoExpr, if necessary.
Davide Italiano04839a52016-01-30 08:03:54 +00002395 llvm_unreachable("Cannot print TypoExpr nodes");
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002396}
2397
Tanya Lattner55808c12011-06-04 00:47:47 +00002398void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2399 OS << "__builtin_astype(";
2400 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002401 OS << ", ";
2402 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00002403 OS << ")";
2404}
2405
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002406//===----------------------------------------------------------------------===//
2407// Stmt method implementations
2408//===----------------------------------------------------------------------===//
2409
Craig Topperc571c812013-08-22 06:02:26 +00002410void Stmt::dumpPretty(const ASTContext &Context) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002411 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002412}
2413
Csaba Dabis9ee26c82019-05-29 18:17:18 +00002414void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
Alex Lorenz36070ed2017-08-17 13:41:55 +00002415 const PrintingPolicy &Policy, unsigned Indentation,
Csaba Dabis9ee26c82019-05-29 18:17:18 +00002416 StringRef NL, const ASTContext *Context) const {
2417 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2418 P.Visit(const_cast<Stmt *>(this));
2419}
2420
2421void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2422 const PrintingPolicy &Policy, bool AddQuotes) const {
2423 std::string Buf;
2424 llvm::raw_string_ostream TempOut(Buf);
2425
2426 printPretty(TempOut, Helper, Policy);
2427
2428 Out << JsonFormat(TempOut.str(), AddQuotes);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002429}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002430
2431//===----------------------------------------------------------------------===//
2432// PrinterHelper
2433//===----------------------------------------------------------------------===//
2434
2435// Implement virtual destructor.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00002436PrinterHelper::~PrinterHelper() = default;