blob: f7a97c2743c19072f9796d9f055f28322191dc42 [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,
Benjamin Krameradcd0262020-01-28 20:23:46 +010078 StringRef NL = "\n", const ASTContext *Context = nullptr)
Alex Lorenz36070ed2017-08-17 13:41:55 +000079 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
George Karpenkov64885ae2018-09-15 02:02:31 +000080 NL(NL), Context(Context) {}
Mike Stump11289f42009-09-09 15:08:12 +000081
Benjamin Krameradcd0262020-01-28 20:23:46 +010082 void PrintStmt(Stmt *S) { PrintStmt(S, Policy.Indentation); }
Douglas Gregor7de59662009-05-29 20:38:28 +000083
84 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000085 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000086 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000087 // If this is an expr used in a stmt context, indent and newline it.
88 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000089 Visit(S);
George Karpenkov64885ae2018-09-15 02:02:31 +000090 OS << ";" << NL;
Chris Lattner882f7882006-11-04 18:52:07 +000091 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000092 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000093 } else {
George Karpenkov64885ae2018-09-15 02:02:31 +000094 Indent() << "<<<NULL STATEMENT>>>" << NL;
Chris Lattner882f7882006-11-04 18:52:07 +000095 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000096 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000097 }
Eli Friedman15ea8802009-05-30 00:19:54 +000098
Richard Smith8baa5002018-09-28 18:44:09 +000099 void PrintInitStmt(Stmt *S, unsigned PrefixWidth) {
100 // FIXME: Cope better with odd prefix widths.
101 IndentLevel += (PrefixWidth + 1) / 2;
102 if (auto *DS = dyn_cast<DeclStmt>(S))
103 PrintRawDeclStmt(DS);
104 else
105 PrintExpr(cast<Expr>(S));
106 OS << "; ";
107 IndentLevel -= (PrefixWidth + 1) / 2;
108 }
109
110 void PrintControlledStmt(Stmt *S) {
111 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
112 OS << " ";
113 PrintRawCompoundStmt(CS);
114 OS << NL;
115 } else {
116 OS << NL;
117 PrintStmt(S);
118 }
119 }
120
Chris Lattner073926e2007-05-20 23:04:55 +0000121 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000122 void PrintRawDecl(Decl *D);
Eli Friedmanf86e5072012-10-16 23:45:15 +0000123 void PrintRawDeclStmt(const DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000124 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +0000125 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000126 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +0000127 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
128 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Alexey Bataev7828b252017-11-21 17:08:48 +0000129 void PrintOMPExecutableDirective(OMPExecutableDirective *S,
130 bool ForceNoStmt = false);
Mike Stump11289f42009-09-09 15:08:12 +0000131
Chris Lattner882f7882006-11-04 18:52:07 +0000132 void PrintExpr(Expr *E) {
133 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +0000134 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000135 else
Chris Lattner882f7882006-11-04 18:52:07 +0000136 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000137 }
Mike Stump11289f42009-09-09 15:08:12 +0000138
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000139 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +0000140 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
141 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000142 return OS;
143 }
Mike Stump11289f42009-09-09 15:08:12 +0000144
Mike Stump11289f42009-09-09 15:08:12 +0000145 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000146 if (Helper && Helper->handledStmt(S,OS))
147 return;
148 else StmtVisitor<StmtPrinter>::Visit(S);
149 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000150
Chandler Carruthb7967b92010-10-23 08:21:37 +0000151 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
George Karpenkov64885ae2018-09-15 02:02:31 +0000152 Indent() << "<<unknown stmt type>>" << NL;
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000153 }
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000154
Chandler Carruthb7967b92010-10-23 08:21:37 +0000155 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000156 OS << "<<unknown expr type>>";
157 }
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000158
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000159 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000160
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000161#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000162#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000163 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000164#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000165 };
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000166
167} // namespace
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000168
Chris Lattner71e23ce2006-11-04 20:18:38 +0000169//===----------------------------------------------------------------------===//
170// Stmt printing methods.
171//===----------------------------------------------------------------------===//
172
Chris Lattner073926e2007-05-20 23:04:55 +0000173/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
174/// with no newline after the }.
175void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000176 OS << "{" << NL;
Aaron Ballmanc7e4e212014-03-17 14:19:37 +0000177 for (auto *I : Node->body())
178 PrintStmt(I);
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattner073926e2007-05-20 23:04:55 +0000180 Indent() << "}";
181}
182
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000183void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000184 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000185}
186
Eli Friedmanf86e5072012-10-16 23:45:15 +0000187void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000188 SmallVector<Decl *, 2> Decls(S->decls());
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000189 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000190}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000191
192void StmtPrinter::VisitNullStmt(NullStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000193 Indent() << ";" << NL;
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000194}
195
196void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000197 Indent();
198 PrintRawDeclStmt(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000199 OS << ";" << NL;
Steve Naroff2a8ad182007-05-29 22:59:26 +0000200}
201
Chris Lattner073926e2007-05-20 23:04:55 +0000202void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
203 Indent();
204 PrintRawCompoundStmt(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000205 OS << "" << NL;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000206}
207
Chris Lattner6c0ff132006-11-05 00:19:50 +0000208void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000209 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000210 PrintExpr(Node->getLHS());
211 if (Node->getRHS()) {
212 OS << " ... ";
213 PrintExpr(Node->getRHS());
214 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000215 OS << ":" << NL;
Mike Stump11289f42009-09-09 15:08:12 +0000216
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000217 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000218}
219
220void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000221 Indent(-1) << "default:" << NL;
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000222 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000223}
224
225void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000226 Indent(-1) << Node->getName() << ":" << NL;
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000227 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000228}
229
Richard Smithc202b282012-04-14 00:33:13 +0000230void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000231 for (const auto *Attr : Node->getAttrs()) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +0000232 Attr->printPretty(OS, Policy);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000233 }
234
Richard Smithc202b282012-04-14 00:33:13 +0000235 PrintStmt(Node->getSubStmt(), 0);
236}
237
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000238void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000239 OS << "if (";
Richard Smith8baa5002018-09-28 18:44:09 +0000240 if (If->getInit())
241 PrintInitStmt(If->getInit(), 4);
Eli Friedmanf86e5072012-10-16 23:45:15 +0000242 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
243 PrintRawDeclStmt(DS);
244 else
245 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000246 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000247
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000248 if (auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
Chris Lattner073926e2007-05-20 23:04:55 +0000249 OS << ' ';
250 PrintRawCompoundStmt(CS);
George Karpenkov64885ae2018-09-15 02:02:31 +0000251 OS << (If->getElse() ? " " : NL);
Chris Lattner073926e2007-05-20 23:04:55 +0000252 } else {
George Karpenkov64885ae2018-09-15 02:02:31 +0000253 OS << NL;
Chris Lattner073926e2007-05-20 23:04:55 +0000254 PrintStmt(If->getThen());
255 if (If->getElse()) Indent();
256 }
Mike Stump11289f42009-09-09 15:08:12 +0000257
Chris Lattner073926e2007-05-20 23:04:55 +0000258 if (Stmt *Else = If->getElse()) {
259 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000260
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000261 if (auto *CS = dyn_cast<CompoundStmt>(Else)) {
Chris Lattner073926e2007-05-20 23:04:55 +0000262 OS << ' ';
263 PrintRawCompoundStmt(CS);
George Karpenkov64885ae2018-09-15 02:02:31 +0000264 OS << NL;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000265 } else if (auto *ElseIf = dyn_cast<IfStmt>(Else)) {
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000266 OS << ' ';
267 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000268 } else {
George Karpenkov64885ae2018-09-15 02:02:31 +0000269 OS << NL;
Chris Lattner073926e2007-05-20 23:04:55 +0000270 PrintStmt(If->getElse());
271 }
Chris Lattner882f7882006-11-04 18:52:07 +0000272 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000273}
274
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000275void StmtPrinter::VisitIfStmt(IfStmt *If) {
276 Indent();
277 PrintRawIfStmt(If);
278}
279
Chris Lattnerf2174b62006-11-04 20:59:27 +0000280void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
281 Indent() << "switch (";
Richard Smith8baa5002018-09-28 18:44:09 +0000282 if (Node->getInit())
283 PrintInitStmt(Node->getInit(), 8);
Eli Friedmanf86e5072012-10-16 23:45:15 +0000284 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
285 PrintRawDeclStmt(DS);
286 else
287 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000288 OS << ")";
Richard Smith8baa5002018-09-28 18:44:09 +0000289 PrintControlledStmt(Node->getBody());
Chris Lattnerf2174b62006-11-04 20:59:27 +0000290}
291
Chris Lattner85ed8732006-11-04 20:40:44 +0000292void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
293 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000294 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
295 PrintRawDeclStmt(DS);
296 else
297 PrintExpr(Node->getCond());
George Karpenkov64885ae2018-09-15 02:02:31 +0000298 OS << ")" << NL;
Chris Lattner85ed8732006-11-04 20:40:44 +0000299 PrintStmt(Node->getBody());
300}
301
302void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000303 Indent() << "do ";
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000304 if (auto *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000305 PrintRawCompoundStmt(CS);
306 OS << " ";
307 } else {
George Karpenkov64885ae2018-09-15 02:02:31 +0000308 OS << NL;
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000309 PrintStmt(Node->getBody());
310 Indent();
311 }
Mike Stump11289f42009-09-09 15:08:12 +0000312
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000313 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000314 PrintExpr(Node->getCond());
George Karpenkov64885ae2018-09-15 02:02:31 +0000315 OS << ");" << NL;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000316}
317
Chris Lattner71e23ce2006-11-04 20:18:38 +0000318void StmtPrinter::VisitForStmt(ForStmt *Node) {
319 Indent() << "for (";
Richard Smith8baa5002018-09-28 18:44:09 +0000320 if (Node->getInit())
321 PrintInitStmt(Node->getInit(), 5);
322 else
323 OS << (Node->getCond() ? "; " : ";");
324 if (Node->getCond())
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000325 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000326 OS << ";";
327 if (Node->getInc()) {
328 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000329 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000330 }
Richard Smith8baa5002018-09-28 18:44:09 +0000331 OS << ")";
332 PrintControlledStmt(Node->getBody());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000333}
334
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000335void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000336 Indent() << "for (";
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000337 if (auto *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000338 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000339 else
340 PrintExpr(cast<Expr>(Node->getElement()));
341 OS << " in ";
342 PrintExpr(Node->getCollection());
Richard Smith8baa5002018-09-28 18:44:09 +0000343 OS << ")";
344 PrintControlledStmt(Node->getBody());
Fariborz Jahanian83615522008-01-02 22:54:34 +0000345}
346
Richard Smith02e85f32011-04-14 22:09:26 +0000347void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
348 Indent() << "for (";
Richard Smith8baa5002018-09-28 18:44:09 +0000349 if (Node->getInit())
350 PrintInitStmt(Node->getInit(), 5);
Richard Smith02e85f32011-04-14 22:09:26 +0000351 PrintingPolicy SubPolicy(Policy);
352 SubPolicy.SuppressInitializers = true;
353 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
354 OS << " : ";
355 PrintExpr(Node->getRangeInit());
Richard Smith8baa5002018-09-28 18:44:09 +0000356 OS << ")";
357 PrintControlledStmt(Node->getBody());
Richard Smith02e85f32011-04-14 22:09:26 +0000358}
359
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000360void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
361 Indent();
362 if (Node->isIfExists())
363 OS << "__if_exists (";
364 else
365 OS << "__if_not_exists (";
Fangrui Song6907ce22018-07-30 19:24:48 +0000366
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000367 if (NestedNameSpecifier *Qualifier
368 = Node->getQualifierLoc().getNestedNameSpecifier())
369 Qualifier->print(OS, Policy);
Fangrui Song6907ce22018-07-30 19:24:48 +0000370
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000371 OS << Node->getNameInfo() << ") ";
Fangrui Song6907ce22018-07-30 19:24:48 +0000372
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000373 PrintRawCompoundStmt(Node->getSubStmt());
374}
375
Chris Lattner16976d32006-11-05 01:46:01 +0000376void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000377 Indent() << "goto " << Node->getLabel()->getName() << ";";
George Karpenkov64885ae2018-09-15 02:02:31 +0000378 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner16976d32006-11-05 01:46:01 +0000379}
380
381void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000382 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000383 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000384 OS << ";";
George Karpenkov64885ae2018-09-15 02:02:31 +0000385 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner16976d32006-11-05 01:46:01 +0000386}
387
388void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000389 Indent() << "continue;";
George Karpenkov64885ae2018-09-15 02:02:31 +0000390 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner16976d32006-11-05 01:46:01 +0000391}
392
393void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000394 Indent() << "break;";
George Karpenkov64885ae2018-09-15 02:02:31 +0000395 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner16976d32006-11-05 01:46:01 +0000396}
397
Chris Lattner882f7882006-11-04 18:52:07 +0000398void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
399 Indent() << "return";
400 if (Node->getRetValue()) {
401 OS << " ";
402 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000403 }
Ted Kremenek88446602013-12-11 23:44:02 +0000404 OS << ";";
George Karpenkov64885ae2018-09-15 02:02:31 +0000405 if (Policy.IncludeNewlines) OS << NL;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000406}
407
Chad Rosierde70e0e2012-08-25 00:11:56 +0000408void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000409 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000410
Anders Carlsson660bdd12007-11-23 23:12:25 +0000411 if (Node->isVolatile())
412 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000413
Jennifer Yub8fee672019-06-03 15:57:25 +0000414 if (Node->isAsmGoto())
415 OS << "goto ";
416
Anders Carlsson660bdd12007-11-23 23:12:25 +0000417 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000418 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000419
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000420 // Outputs
421 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
Jennifer Yub8fee672019-06-03 15:57:25 +0000422 Node->getNumClobbers() != 0 || Node->getNumLabels() != 0)
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000423 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000424
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000425 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
426 if (i != 0)
427 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000428
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000429 if (!Node->getOutputName(i).empty()) {
430 OS << '[';
431 OS << Node->getOutputName(i);
432 OS << "] ";
433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
Chris Lattner72bbf172009-03-10 04:59:06 +0000435 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000436 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000437 Visit(Node->getOutputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000438 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000439 }
Mike Stump11289f42009-09-09 15:08:12 +0000440
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000441 // Inputs
Jennifer Yub8fee672019-06-03 15:57:25 +0000442 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 ||
443 Node->getNumLabels() != 0)
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000444 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000445
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000446 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
447 if (i != 0)
448 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000449
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000450 if (!Node->getInputName(i).empty()) {
451 OS << '[';
452 OS << Node->getInputName(i);
453 OS << "] ";
454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455
Chris Lattner72bbf172009-03-10 04:59:06 +0000456 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000457 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000458 Visit(Node->getInputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000459 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000462 // Clobbers
Jennifer Yub8fee672019-06-03 15:57:25 +0000463 if (Node->getNumClobbers() != 0 || Node->getNumLabels())
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000464 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000465
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000466 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
467 if (i != 0)
468 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000470 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000471 }
Mike Stump11289f42009-09-09 15:08:12 +0000472
Jennifer Yub8fee672019-06-03 15:57:25 +0000473 // Labels
474 if (Node->getNumLabels() != 0)
475 OS << " : ";
476
477 for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) {
478 if (i != 0)
479 OS << ", ";
480 OS << Node->getLabelName(i);
481 }
482
Ted Kremenek88446602013-12-11 23:44:02 +0000483 OS << ");";
George Karpenkov64885ae2018-09-15 02:02:31 +0000484 if (Policy.IncludeNewlines) OS << NL;
Chris Lattner73c56c02007-10-29 04:04:16 +0000485}
486
Chad Rosier32503022012-06-11 20:47:18 +0000487void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
488 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000489 Indent() << "__asm ";
490 if (Node->hasBraces())
George Karpenkov64885ae2018-09-15 02:02:31 +0000491 OS << "{" << NL;
492 OS << Node->getAsmString() << NL;
Chad Rosierb6f46c12012-08-15 16:53:30 +0000493 if (Node->hasBraces())
George Karpenkov64885ae2018-09-15 02:02:31 +0000494 Indent() << "}" << NL;
Chad Rosier32503022012-06-11 20:47:18 +0000495}
496
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000497void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000498 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000499}
500
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000501void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000502 Indent() << "@try";
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000503 if (auto *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000504 PrintRawCompoundStmt(TS);
George Karpenkov64885ae2018-09-15 02:02:31 +0000505 OS << NL;
Fariborz Jahanian88157952007-11-02 18:16:07 +0000506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregor96c79492010-04-23 22:50:49 +0000508 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
509 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000510 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000511 if (catchStmt->getCatchParamDecl()) {
512 if (Decl *DS = catchStmt->getCatchParamDecl())
513 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000514 }
515 OS << ")";
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000516 if (auto *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
Mike Stump11289f42009-09-09 15:08:12 +0000517 PrintRawCompoundStmt(CS);
George Karpenkov64885ae2018-09-15 02:02:31 +0000518 OS << NL;
Mike Stump11289f42009-09-09 15:08:12 +0000519 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000522 if (auto *FS = static_cast<ObjCAtFinallyStmt *>(Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000523 Indent() << "@finally";
524 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
George Karpenkov64885ae2018-09-15 02:02:31 +0000525 OS << NL;
Mike Stump11289f42009-09-09 15:08:12 +0000526 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000527}
528
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000529void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000530}
531
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000532void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
George Karpenkov64885ae2018-09-15 02:02:31 +0000533 Indent() << "@catch (...) { /* todo */ } " << NL;
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000534}
535
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000536void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000537 Indent() << "@throw";
538 if (Node->getThrowExpr()) {
539 OS << " ";
540 PrintExpr(Node->getThrowExpr());
541 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000542 OS << ";" << NL;
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000543}
544
Erik Pilkington29099de2016-07-16 00:35:23 +0000545void StmtPrinter::VisitObjCAvailabilityCheckExpr(
546 ObjCAvailabilityCheckExpr *Node) {
547 OS << "@available(...)";
548}
549
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000550void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000551 Indent() << "@synchronized (";
552 PrintExpr(Node->getSynchExpr());
553 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000554 PrintRawCompoundStmt(Node->getSynchBody());
George Karpenkov64885ae2018-09-15 02:02:31 +0000555 OS << NL;
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000556}
557
John McCall31168b02011-06-15 23:02:42 +0000558void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
559 Indent() << "@autoreleasepool";
560 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
George Karpenkov64885ae2018-09-15 02:02:31 +0000561 OS << NL;
John McCall31168b02011-06-15 23:02:42 +0000562}
563
Sebastian Redl9b244a82008-12-22 21:35:02 +0000564void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
565 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000566 if (Decl *ExDecl = Node->getExceptionDecl())
567 PrintRawDecl(ExDecl);
568 else
569 OS << "...";
570 OS << ") ";
571 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000572}
573
574void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
575 Indent();
576 PrintRawCXXCatchStmt(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000577 OS << NL;
Sebastian Redl9b244a82008-12-22 21:35:02 +0000578}
579
580void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
581 Indent() << "try ";
582 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000583 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000584 OS << " ";
585 PrintRawCXXCatchStmt(Node->getHandler(i));
586 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000587 OS << NL;
Sebastian Redl54c04d42008-12-22 19:15:10 +0000588}
589
John Wiegley1c0675e2011-04-28 01:08:34 +0000590void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
591 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
592 PrintRawCompoundStmt(Node->getTryBlock());
593 SEHExceptStmt *E = Node->getExceptHandler();
594 SEHFinallyStmt *F = Node->getFinallyHandler();
595 if(E)
596 PrintRawSEHExceptHandler(E);
597 else {
598 assert(F && "Must have a finally block...");
599 PrintRawSEHFinallyStmt(F);
600 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000601 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000602}
603
604void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
605 OS << "__finally ";
606 PrintRawCompoundStmt(Node->getBlock());
George Karpenkov64885ae2018-09-15 02:02:31 +0000607 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000608}
609
610void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
611 OS << "__except (";
612 VisitExpr(Node->getFilterExpr());
George Karpenkov64885ae2018-09-15 02:02:31 +0000613 OS << ")" << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000614 PrintRawCompoundStmt(Node->getBlock());
George Karpenkov64885ae2018-09-15 02:02:31 +0000615 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000616}
617
618void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
619 Indent();
620 PrintRawSEHExceptHandler(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000621 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000622}
623
624void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
625 Indent();
626 PrintRawSEHFinallyStmt(Node);
George Karpenkov64885ae2018-09-15 02:02:31 +0000627 OS << NL;
John Wiegley1c0675e2011-04-28 01:08:34 +0000628}
629
Nico Weber9b982072014-07-07 00:12:30 +0000630void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
631 Indent() << "__leave;";
George Karpenkov64885ae2018-09-15 02:02:31 +0000632 if (Policy.IncludeNewlines) OS << NL;
Nico Weber9b982072014-07-07 00:12:30 +0000633}
634
Chris Lattner71e23ce2006-11-04 20:18:38 +0000635//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000636// OpenMP directives printing methods
637//===----------------------------------------------------------------------===//
638
Alexey Bataev7828b252017-11-21 17:08:48 +0000639void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
640 bool ForceNoStmt) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000641 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000642 ArrayRef<OMPClause *> Clauses = S->clauses();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000643 for (auto *Clause : Clauses)
644 if (Clause && !Clause->isImplicit()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000645 OS << ' ';
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000646 Printer.Visit(Clause);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000647 }
George Karpenkov64885ae2018-09-15 02:02:31 +0000648 OS << NL;
Alexey Bataev475a7442018-01-12 19:39:11 +0000649 if (!ForceNoStmt && S->hasAssociatedStmt())
650 PrintStmt(S->getInnermostCapturedStmt()->getCapturedStmt());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000651}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000652
653void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000654 Indent() << "#pragma omp parallel";
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000655 PrintOMPExecutableDirective(Node);
656}
657
658void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000659 Indent() << "#pragma omp simd";
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000660 PrintOMPExecutableDirective(Node);
661}
662
Alexey Bataevf29276e2014-06-18 04:14:57 +0000663void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000664 Indent() << "#pragma omp for";
Alexey Bataevf29276e2014-06-18 04:14:57 +0000665 PrintOMPExecutableDirective(Node);
666}
667
Alexander Musmanf82886e2014-09-18 05:12:34 +0000668void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000669 Indent() << "#pragma omp for simd";
Alexander Musmanf82886e2014-09-18 05:12:34 +0000670 PrintOMPExecutableDirective(Node);
671}
672
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000673void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000674 Indent() << "#pragma omp sections";
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000675 PrintOMPExecutableDirective(Node);
676}
677
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000678void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
679 Indent() << "#pragma omp section";
680 PrintOMPExecutableDirective(Node);
681}
682
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000683void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000684 Indent() << "#pragma omp single";
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000685 PrintOMPExecutableDirective(Node);
686}
687
Alexander Musman80c22892014-07-17 08:54:58 +0000688void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
689 Indent() << "#pragma omp master";
690 PrintOMPExecutableDirective(Node);
691}
692
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000693void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
694 Indent() << "#pragma omp critical";
695 if (Node->getDirectiveName().getName()) {
696 OS << " (";
Sam McCall575e09d2019-11-15 19:19:17 +0100697 Node->getDirectiveName().printName(OS, Policy);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000698 OS << ")";
699 }
700 PrintOMPExecutableDirective(Node);
701}
702
Alexey Bataev4acb8592014-07-07 13:01:15 +0000703void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000704 Indent() << "#pragma omp parallel for";
Alexey Bataev4acb8592014-07-07 13:01:15 +0000705 PrintOMPExecutableDirective(Node);
706}
707
Alexander Musmane4e893b2014-09-23 09:33:00 +0000708void StmtPrinter::VisitOMPParallelForSimdDirective(
709 OMPParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000710 Indent() << "#pragma omp parallel for simd";
Alexander Musmane4e893b2014-09-23 09:33:00 +0000711 PrintOMPExecutableDirective(Node);
712}
713
cchen47d60942019-12-05 13:43:48 -0500714void StmtPrinter::VisitOMPParallelMasterDirective(
715 OMPParallelMasterDirective *Node) {
716 Indent() << "#pragma omp parallel master";
717 PrintOMPExecutableDirective(Node);
718}
719
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000720void StmtPrinter::VisitOMPParallelSectionsDirective(
721 OMPParallelSectionsDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000722 Indent() << "#pragma omp parallel sections";
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000723 PrintOMPExecutableDirective(Node);
724}
725
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000726void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000727 Indent() << "#pragma omp task";
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 PrintOMPExecutableDirective(Node);
729}
730
Alexey Bataev68446b72014-07-18 07:47:19 +0000731void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
732 Indent() << "#pragma omp taskyield";
733 PrintOMPExecutableDirective(Node);
734}
735
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000736void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
737 Indent() << "#pragma omp barrier";
738 PrintOMPExecutableDirective(Node);
739}
740
Alexey Bataev2df347a2014-07-18 10:17:07 +0000741void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
742 Indent() << "#pragma omp taskwait";
743 PrintOMPExecutableDirective(Node);
744}
745
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000746void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000747 Indent() << "#pragma omp taskgroup";
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000748 PrintOMPExecutableDirective(Node);
749}
750
Alexey Bataev6125da92014-07-21 11:26:11 +0000751void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000752 Indent() << "#pragma omp flush";
Alexey Bataev6125da92014-07-21 11:26:11 +0000753 PrintOMPExecutableDirective(Node);
754}
755
Alexey Bataevc112e942020-02-28 09:52:15 -0500756void StmtPrinter::VisitOMPDepobjDirective(OMPDepobjDirective *Node) {
757 Indent() << "#pragma omp depobj";
758 PrintOMPExecutableDirective(Node);
759}
760
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000761void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000762 Indent() << "#pragma omp ordered";
Alexey Bataev475a7442018-01-12 19:39:11 +0000763 PrintOMPExecutableDirective(Node, Node->hasClausesOfKind<OMPDependClause>());
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000764}
765
Alexey Bataev0162e452014-07-22 10:10:35 +0000766void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000767 Indent() << "#pragma omp atomic";
Alexey Bataev0162e452014-07-22 10:10:35 +0000768 PrintOMPExecutableDirective(Node);
769}
770
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000771void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000772 Indent() << "#pragma omp target";
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000773 PrintOMPExecutableDirective(Node);
774}
775
Michael Wong65f367f2015-07-21 13:44:28 +0000776void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000777 Indent() << "#pragma omp target data";
Michael Wong65f367f2015-07-21 13:44:28 +0000778 PrintOMPExecutableDirective(Node);
779}
780
Samuel Antaodf67fc42016-01-19 19:15:56 +0000781void StmtPrinter::VisitOMPTargetEnterDataDirective(
782 OMPTargetEnterDataDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000783 Indent() << "#pragma omp target enter data";
Alexey Bataev7828b252017-11-21 17:08:48 +0000784 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antaodf67fc42016-01-19 19:15:56 +0000785}
786
Samuel Antao72590762016-01-19 20:04:50 +0000787void StmtPrinter::VisitOMPTargetExitDataDirective(
788 OMPTargetExitDataDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000789 Indent() << "#pragma omp target exit data";
Alexey Bataev7828b252017-11-21 17:08:48 +0000790 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antao72590762016-01-19 20:04:50 +0000791}
792
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000793void StmtPrinter::VisitOMPTargetParallelDirective(
794 OMPTargetParallelDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000795 Indent() << "#pragma omp target parallel";
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +0000796 PrintOMPExecutableDirective(Node);
797}
798
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000799void StmtPrinter::VisitOMPTargetParallelForDirective(
800 OMPTargetParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000801 Indent() << "#pragma omp target parallel for";
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +0000802 PrintOMPExecutableDirective(Node);
803}
804
Alexey Bataev13314bf2014-10-09 04:18:56 +0000805void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000806 Indent() << "#pragma omp teams";
Alexey Bataev13314bf2014-10-09 04:18:56 +0000807 PrintOMPExecutableDirective(Node);
808}
809
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000810void StmtPrinter::VisitOMPCancellationPointDirective(
811 OMPCancellationPointDirective *Node) {
812 Indent() << "#pragma omp cancellation point "
813 << getOpenMPDirectiveName(Node->getCancelRegion());
814 PrintOMPExecutableDirective(Node);
815}
Alexey Bataev80909872015-07-02 11:25:17 +0000816
817void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
818 Indent() << "#pragma omp cancel "
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000819 << getOpenMPDirectiveName(Node->getCancelRegion());
Alexey Bataev80909872015-07-02 11:25:17 +0000820 PrintOMPExecutableDirective(Node);
821}
Alexey Bataev49f6e782015-12-01 04:18:41 +0000822
823void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000824 Indent() << "#pragma omp taskloop";
Alexey Bataev49f6e782015-12-01 04:18:41 +0000825 PrintOMPExecutableDirective(Node);
826}
827
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000828void StmtPrinter::VisitOMPTaskLoopSimdDirective(
829 OMPTaskLoopSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000830 Indent() << "#pragma omp taskloop simd";
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000831 PrintOMPExecutableDirective(Node);
832}
833
Alexey Bataev60e51c42019-10-10 20:13:02 +0000834void StmtPrinter::VisitOMPMasterTaskLoopDirective(
835 OMPMasterTaskLoopDirective *Node) {
836 Indent() << "#pragma omp master taskloop";
837 PrintOMPExecutableDirective(Node);
838}
839
Alexey Bataevb8552ab2019-10-18 16:47:35 +0000840void StmtPrinter::VisitOMPMasterTaskLoopSimdDirective(
841 OMPMasterTaskLoopSimdDirective *Node) {
842 Indent() << "#pragma omp master taskloop simd";
843 PrintOMPExecutableDirective(Node);
844}
845
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000846void StmtPrinter::VisitOMPParallelMasterTaskLoopDirective(
847 OMPParallelMasterTaskLoopDirective *Node) {
848 Indent() << "#pragma omp parallel master taskloop";
849 PrintOMPExecutableDirective(Node);
850}
851
Alexey Bataev14a388f2019-10-25 10:27:13 -0400852void StmtPrinter::VisitOMPParallelMasterTaskLoopSimdDirective(
853 OMPParallelMasterTaskLoopSimdDirective *Node) {
854 Indent() << "#pragma omp parallel master taskloop simd";
855 PrintOMPExecutableDirective(Node);
856}
857
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000858void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000859 Indent() << "#pragma omp distribute";
Carlo Bertolli6200a3d2015-12-14 14:51:25 +0000860 PrintOMPExecutableDirective(Node);
861}
862
Samuel Antao686c70c2016-05-26 17:30:50 +0000863void StmtPrinter::VisitOMPTargetUpdateDirective(
864 OMPTargetUpdateDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000865 Indent() << "#pragma omp target update";
Alexey Bataev7828b252017-11-21 17:08:48 +0000866 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antao686c70c2016-05-26 17:30:50 +0000867}
868
Carlo Bertolli9925f152016-06-27 14:55:37 +0000869void StmtPrinter::VisitOMPDistributeParallelForDirective(
870 OMPDistributeParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000871 Indent() << "#pragma omp distribute parallel for";
Carlo Bertolli9925f152016-06-27 14:55:37 +0000872 PrintOMPExecutableDirective(Node);
873}
874
Kelvin Li4a39add2016-07-05 05:00:15 +0000875void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
876 OMPDistributeParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000877 Indent() << "#pragma omp distribute parallel for simd";
Kelvin Li4a39add2016-07-05 05:00:15 +0000878 PrintOMPExecutableDirective(Node);
879}
880
Kelvin Li787f3fc2016-07-06 04:45:38 +0000881void StmtPrinter::VisitOMPDistributeSimdDirective(
882 OMPDistributeSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000883 Indent() << "#pragma omp distribute simd";
Kelvin Li787f3fc2016-07-06 04:45:38 +0000884 PrintOMPExecutableDirective(Node);
885}
886
Kelvin Lia579b912016-07-14 02:54:56 +0000887void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
888 OMPTargetParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000889 Indent() << "#pragma omp target parallel for simd";
Kelvin Lia579b912016-07-14 02:54:56 +0000890 PrintOMPExecutableDirective(Node);
891}
892
Kelvin Li986330c2016-07-20 22:57:10 +0000893void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000894 Indent() << "#pragma omp target simd";
Kelvin Li986330c2016-07-20 22:57:10 +0000895 PrintOMPExecutableDirective(Node);
896}
897
Kelvin Li02532872016-08-05 14:37:37 +0000898void StmtPrinter::VisitOMPTeamsDistributeDirective(
899 OMPTeamsDistributeDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000900 Indent() << "#pragma omp teams distribute";
Kelvin Li02532872016-08-05 14:37:37 +0000901 PrintOMPExecutableDirective(Node);
902}
903
Kelvin Li4e325f72016-10-25 12:50:55 +0000904void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
905 OMPTeamsDistributeSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000906 Indent() << "#pragma omp teams distribute simd";
Kelvin Li4e325f72016-10-25 12:50:55 +0000907 PrintOMPExecutableDirective(Node);
908}
909
Kelvin Li579e41c2016-11-30 23:51:03 +0000910void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
911 OMPTeamsDistributeParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000912 Indent() << "#pragma omp teams distribute parallel for simd";
Kelvin Li579e41c2016-11-30 23:51:03 +0000913 PrintOMPExecutableDirective(Node);
914}
915
Kelvin Li7ade93f2016-12-09 03:24:30 +0000916void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
917 OMPTeamsDistributeParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000918 Indent() << "#pragma omp teams distribute parallel for";
Kelvin Li7ade93f2016-12-09 03:24:30 +0000919 PrintOMPExecutableDirective(Node);
920}
921
Kelvin Libf594a52016-12-17 05:48:59 +0000922void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000923 Indent() << "#pragma omp target teams";
Kelvin Libf594a52016-12-17 05:48:59 +0000924 PrintOMPExecutableDirective(Node);
925}
926
Kelvin Li83c451e2016-12-25 04:52:54 +0000927void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
928 OMPTargetTeamsDistributeDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000929 Indent() << "#pragma omp target teams distribute";
Kelvin Li83c451e2016-12-25 04:52:54 +0000930 PrintOMPExecutableDirective(Node);
931}
932
Kelvin Li80e8f562016-12-29 22:16:30 +0000933void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
934 OMPTargetTeamsDistributeParallelForDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000935 Indent() << "#pragma omp target teams distribute parallel for";
Kelvin Li80e8f562016-12-29 22:16:30 +0000936 PrintOMPExecutableDirective(Node);
937}
938
Kelvin Li1851df52017-01-03 05:23:48 +0000939void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
940 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000941 Indent() << "#pragma omp target teams distribute parallel for simd";
Kelvin Li1851df52017-01-03 05:23:48 +0000942 PrintOMPExecutableDirective(Node);
943}
944
Kelvin Lida681182017-01-10 18:08:18 +0000945void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
946 OMPTargetTeamsDistributeSimdDirective *Node) {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +0000947 Indent() << "#pragma omp target teams distribute simd";
Kelvin Lida681182017-01-10 18:08:18 +0000948 PrintOMPExecutableDirective(Node);
949}
950
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000951//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000952// Expr printing methods.
953//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000954
Eric Fiselier708afb52019-05-16 21:04:15 +0000955void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) {
956 OS << Node->getBuiltinStr() << "()";
957}
958
Bill Wendling7c44da22018-10-31 03:48:47 +0000959void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) {
960 PrintExpr(Node->getSubExpr());
961}
962
Chris Lattner882f7882006-11-04 18:52:07 +0000963void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000964 if (const auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +0000965 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
966 return;
967 }
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000968 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
969 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000970 if (Node->hasTemplateKeyword())
971 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000972 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000973 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +0000974 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000975}
976
John McCall8cd78132009-11-19 22:55:06 +0000977void StmtPrinter::VisitDependentScopeDeclRefExpr(
978 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000979 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
980 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000981 if (Node->hasTemplateKeyword())
982 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000983 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000984 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +0000985 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000986}
987
John McCalld14a8642009-11-21 08:51:07 +0000988void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000989 if (Node->getQualifier())
990 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000991 if (Node->hasTemplateKeyword())
992 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000993 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000994 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +0000995 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000996}
997
Alex Lorenzf7579612017-10-26 00:56:54 +0000998static bool isImplicitSelf(const Expr *E) {
999 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001000 if (const auto *PD = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
Alex Lorenzf7579612017-10-26 00:56:54 +00001001 if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001002 DRE->getBeginLoc().isInvalid())
Alex Lorenzf7579612017-10-26 00:56:54 +00001003 return true;
1004 }
1005 }
1006 return false;
1007}
1008
Steve Naroffe46504b2007-11-12 14:29:37 +00001009void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +00001010 if (Node->getBase()) {
Alex Lorenzf7579612017-10-26 00:56:54 +00001011 if (!Policy.SuppressImplicitBase ||
1012 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1013 PrintExpr(Node->getBase());
1014 OS << (Node->isArrow() ? "->" : ".");
1015 }
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +00001016 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001017 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +00001018}
1019
Steve Naroffec944032008-05-30 00:40:33 +00001020void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001021 if (Node->isSuperReceiver())
1022 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +00001023 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +00001024 PrintExpr(Node->getBase());
1025 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +00001026 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1027 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +00001028 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001029
Alex Lorenz5a5a6542018-05-23 00:52:20 +00001030 if (Node->isImplicitProperty()) {
1031 if (const auto *Getter = Node->getImplicitPropertyGetter())
1032 Getter->getSelector().print(OS);
1033 else
1034 OS << SelectorTable::getPropertyNameFromSetterSelector(
1035 Node->getImplicitPropertySetter()->getSelector());
1036 } else
John McCallb7bd14f2010-12-02 01:19:52 +00001037 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001038}
1039
Ted Kremeneke65b0862012-03-06 20:05:56 +00001040void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001041 PrintExpr(Node->getBaseExpr());
1042 OS << "[";
1043 PrintExpr(Node->getKeyExpr());
1044 OS << "]";
1045}
1046
Chris Lattner6307f192008-08-10 01:53:14 +00001047void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Bruno Ricci17ff0262018-10-27 19:21:19 +00001048 OS << PredefinedExpr::getIdentKindName(Node->getIdentKind());
Anders Carlsson625bfc82007-07-21 05:21:51 +00001049}
1050
Steve Naroffae4143e2007-04-26 20:39:23 +00001051void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001052 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +00001053
1054 switch (Node->getKind()) {
1055 case CharacterLiteral::Ascii: break; // no prefix.
1056 case CharacterLiteral::Wide: OS << 'L'; break;
Aaron Ballman9a17c852016-01-07 20:59:26 +00001057 case CharacterLiteral::UTF8: OS << "u8"; break;
Douglas Gregorfb65e592011-07-27 05:40:30 +00001058 case CharacterLiteral::UTF16: OS << 'u'; break;
1059 case CharacterLiteral::UTF32: OS << 'U'; break;
1060 }
1061
Chris Lattner666115c2007-07-13 23:58:20 +00001062 switch (value) {
1063 case '\\':
1064 OS << "'\\\\'";
1065 break;
1066 case '\'':
1067 OS << "'\\''";
1068 break;
1069 case '\a':
1070 // TODO: K&R: the meaning of '\\a' is different in traditional C
1071 OS << "'\\a'";
1072 break;
1073 case '\b':
1074 OS << "'\\b'";
1075 break;
1076 // Nonstandard escape sequence.
1077 /*case '\e':
1078 OS << "'\\e'";
1079 break;*/
1080 case '\f':
1081 OS << "'\\f'";
1082 break;
1083 case '\n':
1084 OS << "'\\n'";
1085 break;
1086 case '\r':
1087 OS << "'\\r'";
1088 break;
1089 case '\t':
1090 OS << "'\\t'";
1091 break;
1092 case '\v':
1093 OS << "'\\v'";
1094 break;
1095 default:
Steven Watanabee43ae192016-02-13 02:31:28 +00001096 // A character literal might be sign-extended, which
1097 // would result in an invalid \U escape sequence.
1098 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1099 // are not correctly handled.
1100 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1101 value &= 0xFFu;
Jordan Rose00d1b592013-02-08 22:30:27 +00001102 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +00001103 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +00001104 else if (value < 256)
1105 OS << "'\\x" << llvm::format("%02x", value) << "'";
1106 else if (value <= 0xFFFF)
1107 OS << "'\\u" << llvm::format("%04x", value) << "'";
1108 else
1109 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001110 }
Steve Naroffae4143e2007-04-26 20:39:23 +00001111}
1112
Alex Lorenz36070ed2017-08-17 13:41:55 +00001113/// Prints the given expression using the original source text. Returns true on
1114/// success, false otherwise.
1115static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1116 const ASTContext *Context) {
1117 if (!Context)
1118 return false;
1119 bool Invalid = false;
1120 StringRef Source = Lexer::getSourceText(
1121 CharSourceRange::getTokenRange(E->getSourceRange()),
1122 Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1123 if (!Invalid) {
1124 OS << Source;
1125 return true;
1126 }
1127 return false;
1128}
1129
Steve Naroffdf7855b2007-02-21 23:46:25 +00001130void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Alex Lorenz36070ed2017-08-17 13:41:55 +00001131 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1132 return;
Chris Lattner06430412007-05-21 05:45:03 +00001133 bool isSigned = Node->getType()->isSignedIntegerType();
1134 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +00001135
Chris Lattner06430412007-05-21 05:45:03 +00001136 // Emit suffixes. Integer literals are always a builtin integer type.
Simon Pilgrim3459a4c2019-10-07 13:58:05 +00001137 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001138 default: llvm_unreachable("Unexpected type for integer literal!");
David Majnemerbe09e8e2015-03-06 18:04:22 +00001139 case BuiltinType::Char_S:
1140 case BuiltinType::Char_U: OS << "i8"; break;
David Majnemer65a407c2014-06-21 18:46:07 +00001141 case BuiltinType::UChar: OS << "Ui8"; break;
1142 case BuiltinType::Short: OS << "i16"; break;
1143 case BuiltinType::UShort: OS << "Ui16"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001144 case BuiltinType::Int: break; // no suffix.
1145 case BuiltinType::UInt: OS << 'U'; break;
1146 case BuiltinType::Long: OS << 'L'; break;
1147 case BuiltinType::ULong: OS << "UL"; break;
1148 case BuiltinType::LongLong: OS << "LL"; break;
1149 case BuiltinType::ULongLong: OS << "ULL"; break;
1150 }
Chris Lattner882f7882006-11-04 18:52:07 +00001151}
Benjamin Kramer8a526762012-09-20 14:07:17 +00001152
Leonard Chandb01c3a2018-06-20 17:19:40 +00001153void StmtPrinter::VisitFixedPointLiteral(FixedPointLiteral *Node) {
1154 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1155 return;
1156 OS << Node->getValueAsString(/*Radix=*/10);
1157
Simon Pilgrim3459a4c2019-10-07 13:58:05 +00001158 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
Leonard Chandb01c3a2018-06-20 17:19:40 +00001159 default: llvm_unreachable("Unexpected type for fixed point literal!");
1160 case BuiltinType::ShortFract: OS << "hr"; break;
1161 case BuiltinType::ShortAccum: OS << "hk"; break;
1162 case BuiltinType::UShortFract: OS << "uhr"; break;
1163 case BuiltinType::UShortAccum: OS << "uhk"; break;
1164 case BuiltinType::Fract: OS << "r"; break;
1165 case BuiltinType::Accum: OS << "k"; break;
1166 case BuiltinType::UFract: OS << "ur"; break;
1167 case BuiltinType::UAccum: OS << "uk"; break;
1168 case BuiltinType::LongFract: OS << "lr"; break;
1169 case BuiltinType::LongAccum: OS << "lk"; break;
1170 case BuiltinType::ULongFract: OS << "ulr"; break;
1171 case BuiltinType::ULongAccum: OS << "ulk"; break;
1172 }
1173}
1174
Benjamin Kramer8a526762012-09-20 14:07:17 +00001175static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1176 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001177 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +00001178 Node->getValue().toString(Str);
1179 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001180 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1181 OS << '.'; // Trailing dot in order to separate from ints.
1182
1183 if (!PrintSuffix)
1184 return;
1185
1186 // Emit suffixes. Float literals are always a builtin float type.
Simon Pilgrim3459a4c2019-10-07 13:58:05 +00001187 switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
Benjamin Kramer8a526762012-09-20 14:07:17 +00001188 default: llvm_unreachable("Unexpected type for float literal!");
1189 case BuiltinType::Half: break; // FIXME: suffix?
1190 case BuiltinType::Double: break; // no suffix.
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00001191 case BuiltinType::Float16: OS << "F16"; break;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001192 case BuiltinType::Float: OS << 'F'; break;
1193 case BuiltinType::LongDouble: OS << 'L'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001194 case BuiltinType::Float128: OS << 'Q'; break;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001195 }
1196}
1197
1198void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Alex Lorenz36070ed2017-08-17 13:41:55 +00001199 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1200 return;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001201 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +00001202}
Chris Lattner1c20a172007-08-26 03:42:43 +00001203
1204void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1205 PrintExpr(Node->getSubExpr());
1206 OS << "i";
1207}
1208
Steve Naroffdf7855b2007-02-21 23:46:25 +00001209void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +00001210 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +00001211}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001212
Chris Lattner882f7882006-11-04 18:52:07 +00001213void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1214 OS << "(";
1215 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +00001216 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +00001217}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001218
Chris Lattner882f7882006-11-04 18:52:07 +00001219void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +00001220 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +00001221 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +00001222
Eli Friedman1cf25362009-06-14 22:39:26 +00001223 // Print a space if this is an "identifier operator" like __real, or if
1224 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +00001225 switch (Node->getOpcode()) {
1226 default: break;
John McCalle3027922010-08-25 11:45:40 +00001227 case UO_Real:
1228 case UO_Imag:
1229 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +00001230 OS << ' ';
1231 break;
John McCalle3027922010-08-25 11:45:40 +00001232 case UO_Plus:
1233 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +00001234 if (isa<UnaryOperator>(Node->getSubExpr()))
1235 OS << ' ';
1236 break;
Chris Lattnere5b60442007-08-23 21:46:40 +00001237 }
1238 }
Chris Lattner882f7882006-11-04 18:52:07 +00001239 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001240
Chris Lattner15768702006-11-05 23:54:51 +00001241 if (Node->isPostfix())
1242 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +00001243}
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001244
Douglas Gregor882211c2010-04-28 22:16:22 +00001245void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1246 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001247 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1248 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +00001249 bool PrintedSomething = false;
1250 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
James Y Knight7281c352015-12-29 22:31:18 +00001251 OffsetOfNode ON = Node->getComponent(i);
1252 if (ON.getKind() == OffsetOfNode::Array) {
Douglas Gregor882211c2010-04-28 22:16:22 +00001253 // Array node
1254 OS << "[";
1255 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1256 OS << "]";
1257 PrintedSomething = true;
1258 continue;
1259 }
Douglas Gregord1702062010-04-29 00:18:15 +00001260
1261 // Skip implicit base indirections.
James Y Knight7281c352015-12-29 22:31:18 +00001262 if (ON.getKind() == OffsetOfNode::Base)
Douglas Gregord1702062010-04-29 00:18:15 +00001263 continue;
1264
Douglas Gregor882211c2010-04-28 22:16:22 +00001265 // Field or identifier node.
1266 IdentifierInfo *Id = ON.getFieldName();
1267 if (!Id)
1268 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001269
Douglas Gregor882211c2010-04-28 22:16:22 +00001270 if (PrintedSomething)
1271 OS << ".";
1272 else
1273 PrintedSomething = true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001274 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +00001275 }
1276 OS << ")";
1277}
1278
Peter Collingbournee190dee2011-03-11 19:24:49 +00001279void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1280 switch(Node->getKind()) {
1281 case UETT_SizeOf:
1282 OS << "sizeof";
1283 break;
1284 case UETT_AlignOf:
Richard Smith301bc212016-05-19 01:39:10 +00001285 if (Policy.Alignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001286 OS << "alignof";
Richard Smith301bc212016-05-19 01:39:10 +00001287 else if (Policy.UnderscoreAlignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001288 OS << "_Alignof";
1289 else
1290 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001291 break;
Richard Smith6822bd72018-10-26 19:26:45 +00001292 case UETT_PreferredAlignOf:
1293 OS << "__alignof";
1294 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001295 case UETT_VecStep:
1296 OS << "vec_step";
1297 break;
Alexey Bataev00396512015-07-02 03:40:19 +00001298 case UETT_OpenMPRequiredSimdAlign:
1299 OS << "__builtin_omp_required_simd_align";
1300 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001301 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001302 if (Node->isArgumentType()) {
1303 OS << '(';
1304 Node->getArgumentType().print(OS, Policy);
1305 OS << ')';
1306 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +00001307 OS << " ";
1308 PrintExpr(Node->getArgumentExpr());
1309 }
Chris Lattner882f7882006-11-04 18:52:07 +00001310}
Peter Collingbourne91147592011-04-15 00:35:48 +00001311
1312void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1313 OS << "_Generic(";
1314 PrintExpr(Node->getControllingExpr());
Mark de Wever21490282019-11-12 20:46:19 +01001315 for (const GenericSelectionExpr::Association Assoc : Node->associations()) {
Peter Collingbourne91147592011-04-15 00:35:48 +00001316 OS << ", ";
Bruno Ricci1ec7fd32019-01-29 12:57:11 +00001317 QualType T = Assoc.getType();
Peter Collingbourne91147592011-04-15 00:35:48 +00001318 if (T.isNull())
1319 OS << "default";
1320 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001321 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001322 OS << ": ";
Bruno Ricci1ec7fd32019-01-29 12:57:11 +00001323 PrintExpr(Assoc.getAssociationExpr());
Peter Collingbourne91147592011-04-15 00:35:48 +00001324 }
1325 OS << ")";
1326}
1327
Chris Lattner882f7882006-11-04 18:52:07 +00001328void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001329 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001330 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001331 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001332 OS << "]";
1333}
1334
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001335void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1336 PrintExpr(Node->getBase());
1337 OS << "[";
1338 if (Node->getLowerBound())
1339 PrintExpr(Node->getLowerBound());
Alexey Bataeved5fb672015-09-11 04:54:28 +00001340 if (Node->getColonLoc().isValid()) {
1341 OS << ":";
1342 if (Node->getLength())
1343 PrintExpr(Node->getLength());
1344 }
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001345 OS << "]";
1346}
1347
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001348void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001349 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001350 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1351 // Don't print any defaulted arguments
1352 break;
1353 }
1354
Chris Lattner882f7882006-11-04 18:52:07 +00001355 if (i) OS << ", ";
1356 PrintExpr(Call->getArg(i));
1357 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001358}
1359
1360void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1361 PrintExpr(Call->getCallee());
1362 OS << "(";
1363 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001364 OS << ")";
1365}
Alex Lorenzf7579612017-10-26 00:56:54 +00001366
1367static bool isImplicitThis(const Expr *E) {
1368 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1369 return TE->isImplicit();
1370 return false;
1371}
1372
Chris Lattner882f7882006-11-04 18:52:07 +00001373void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Alex Lorenzf7579612017-10-26 00:56:54 +00001374 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1375 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001376
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001377 auto *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
Alex Lorenzf7579612017-10-26 00:56:54 +00001378 FieldDecl *ParentDecl =
1379 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1380 : nullptr;
David Blaikie8fab8e52012-11-12 19:12:12 +00001381
Alex Lorenzf7579612017-10-26 00:56:54 +00001382 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1383 OS << (Node->isArrow() ? "->" : ".");
1384 }
David Blaikie8fab8e52012-11-12 19:12:12 +00001385
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001386 if (auto *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001387 if (FD->isAnonymousStructOrUnion())
1388 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001389
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001390 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1391 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001392 if (Node->hasTemplateKeyword())
1393 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001394 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001395 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00001396 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001397}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001398
Steve Naroffe87026a2009-07-24 17:54:45 +00001399void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1400 PrintExpr(Node->getBase());
1401 OS << (Node->isArrow() ? "->isa" : ".isa");
1402}
1403
Nate Begemance4d7fc2008-04-18 23:10:10 +00001404void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001405 PrintExpr(Node->getBase());
1406 OS << ".";
1407 OS << Node->getAccessor().getName();
1408}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001409
Douglas Gregorf19b2312008-10-28 15:36:24 +00001410void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001411 OS << '(';
1412 Node->getTypeAsWritten().print(OS, Policy);
1413 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001414 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001415}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001416
Steve Naroff57eb2c52007-07-19 21:32:11 +00001417void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001418 OS << '(';
1419 Node->getType().print(OS, Policy);
1420 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001421 PrintExpr(Node->getInitializer());
1422}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001423
Steve Naroff7a5af782007-07-13 16:58:59 +00001424void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001425 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001426 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001427}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001428
Chris Lattner882f7882006-11-04 18:52:07 +00001429void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1430 PrintExpr(Node->getLHS());
1431 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1432 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001433}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001434
Chris Lattner86928112007-08-25 02:00:02 +00001435void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1436 PrintExpr(Node->getLHS());
1437 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1438 PrintExpr(Node->getRHS());
1439}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001440
Chris Lattner882f7882006-11-04 18:52:07 +00001441void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1442 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001443 OS << " ? ";
1444 PrintExpr(Node->getLHS());
1445 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001446 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001447}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001448
Chris Lattnereefa10e2007-05-28 06:56:27 +00001449// GNU extensions.
1450
John McCallc07a0c72011-02-17 10:25:35 +00001451void
1452StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1453 PrintExpr(Node->getCommon());
1454 OS << " ?: ";
1455 PrintExpr(Node->getFalseExpr());
1456}
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001457
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001458void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001459 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001460}
1461
Chris Lattner366727f2007-07-24 16:58:17 +00001462void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1463 OS << "(";
1464 PrintRawCompoundStmt(E->getSubStmt());
1465 OS << ")";
1466}
1467
Steve Naroff9efdabc2007-08-03 21:21:27 +00001468void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1469 OS << "__builtin_choose_expr(";
1470 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001471 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001472 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001473 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001474 PrintExpr(Node->getRHS());
1475 OS << ")";
1476}
Chris Lattner366727f2007-07-24 16:58:17 +00001477
Douglas Gregor3be4b122008-11-29 04:51:27 +00001478void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1479 OS << "__null";
1480}
1481
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001482void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1483 OS << "__builtin_shufflevector(";
1484 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1485 if (i) OS << ", ";
1486 PrintExpr(Node->getExpr(i));
1487 }
1488 OS << ")";
1489}
1490
Hal Finkelc4d7c822013-09-18 03:29:45 +00001491void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1492 OS << "__builtin_convertvector(";
1493 PrintExpr(Node->getSrcExpr());
1494 OS << ", ";
1495 Node->getType().print(OS, Policy);
1496 OS << ")";
1497}
1498
Anders Carlsson4692db02007-08-31 04:56:16 +00001499void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001500 if (Node->getSyntacticForm()) {
1501 Visit(Node->getSyntacticForm());
1502 return;
1503 }
1504
Richard Smithd0e102f2015-01-30 02:04:26 +00001505 OS << "{";
Anders Carlsson4692db02007-08-31 04:56:16 +00001506 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1507 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001508 if (Node->getInit(i))
1509 PrintExpr(Node->getInit(i));
1510 else
Richard Smithd0e102f2015-01-30 02:04:26 +00001511 OS << "{}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001512 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001513 OS << "}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001514}
1515
Richard Smith410306b2016-12-12 02:53:20 +00001516void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1517 // There's no way to express this expression in any of our supported
1518 // languages, so just emit something terse and (hopefully) clear.
1519 OS << "{";
1520 PrintExpr(Node->getSubExpr());
1521 OS << "}";
1522}
1523
1524void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1525 OS << "*";
1526}
1527
Nate Begeman5ec4b312009-08-10 23:49:36 +00001528void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
Richard Smithd0e102f2015-01-30 02:04:26 +00001529 OS << "(";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001530 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1531 if (i) OS << ", ";
1532 PrintExpr(Node->getExpr(i));
1533 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001534 OS << ")";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001535}
1536
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001537void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Justin Bognercb337032015-05-28 22:19:36 +00001538 bool NeedsEquals = true;
David Majnemerf7e36092016-06-23 00:15:04 +00001539 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1540 if (D.isFieldDesignator()) {
1541 if (D.getDotLoc().isInvalid()) {
1542 if (IdentifierInfo *II = D.getFieldName()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001543 OS << II->getName() << ":";
Justin Bognercb337032015-05-28 22:19:36 +00001544 NeedsEquals = false;
1545 }
Serge Pavloveb57aa62014-06-20 17:08:28 +00001546 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001547 OS << "." << D.getFieldName()->getName();
Serge Pavloveb57aa62014-06-20 17:08:28 +00001548 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001549 } else {
1550 OS << "[";
David Majnemerf7e36092016-06-23 00:15:04 +00001551 if (D.isArrayDesignator()) {
1552 PrintExpr(Node->getArrayIndex(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001553 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001554 PrintExpr(Node->getArrayRangeStart(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001555 OS << " ... ";
David Majnemerf7e36092016-06-23 00:15:04 +00001556 PrintExpr(Node->getArrayRangeEnd(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001557 }
1558 OS << "]";
1559 }
1560 }
1561
Justin Bognercb337032015-05-28 22:19:36 +00001562 if (NeedsEquals)
1563 OS << " = ";
1564 else
1565 OS << " ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001566 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001567}
1568
Yunzhong Gaocb779302015-06-10 00:27:52 +00001569void StmtPrinter::VisitDesignatedInitUpdateExpr(
1570 DesignatedInitUpdateExpr *Node) {
1571 OS << "{";
1572 OS << "/*base*/";
1573 PrintExpr(Node->getBase());
1574 OS << ", ";
1575
1576 OS << "/*updater*/";
1577 PrintExpr(Node->getUpdater());
1578 OS << "}";
1579}
1580
1581void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1582 OS << "/*no init*/";
1583}
1584
Douglas Gregor0202cb42009-01-29 17:44:32 +00001585void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Richard Smith301bc212016-05-19 01:39:10 +00001586 if (Node->getType()->getAsCXXRecordDecl()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001587 OS << "/*implicit*/";
1588 Node->getType().print(OS, Policy);
1589 OS << "()";
1590 } else {
1591 OS << "/*implicit*/(";
1592 Node->getType().print(OS, Policy);
1593 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001594 if (Node->getType()->isRecordType())
1595 OS << "{}";
1596 else
1597 OS << 0;
1598 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001599}
1600
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001601void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001602 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001603 PrintExpr(Node->getSubExpr());
1604 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001605 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001606 OS << ")";
1607}
1608
John McCallfe96e0b2011-11-06 09:01:30 +00001609void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1610 PrintExpr(Node->getSyntacticForm());
1611}
1612
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001613void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001614 const char *Name = nullptr;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001615 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001616#define BUILTIN(ID, TYPE, ATTRS)
1617#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1618 case AtomicExpr::AO ## ID: \
1619 Name = #ID "("; \
1620 break;
1621#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001622 }
1623 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001624
1625 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001626 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001627 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
Yaxun Liu39195062017-08-04 18:16:31 +00001628 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1629 Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001630 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001631 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001632 }
Richard Smithfeea8832012-04-12 05:08:17 +00001633 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1634 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001635 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001636 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001637 }
Richard Smithfeea8832012-04-12 05:08:17 +00001638 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1639 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001640 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001641 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001642 }
Yaxun Liu39195062017-08-04 18:16:31 +00001643 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1644 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
Richard Smithdf6bee82013-05-01 19:02:43 +00001645 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001646 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001647 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001648 if (Node->isCmpXChg()) {
1649 OS << ", ";
1650 PrintExpr(Node->getOrderFail());
1651 }
1652 OS << ")";
1653}
1654
Chris Lattnereefa10e2007-05-28 06:56:27 +00001655// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001656void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
Douglas Gregor993603d2008-11-14 16:09:21 +00001657 OverloadedOperatorKind Kind = Node->getOperator();
1658 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1659 if (Node->getNumArgs() == 1) {
Richard Smith7fa2b742019-06-14 20:01:51 +00001660 OS << getOperatorSpelling(Kind) << ' ';
Douglas Gregor993603d2008-11-14 16:09:21 +00001661 PrintExpr(Node->getArg(0));
1662 } else {
1663 PrintExpr(Node->getArg(0));
Richard Smith7fa2b742019-06-14 20:01:51 +00001664 OS << ' ' << getOperatorSpelling(Kind);
Douglas Gregor993603d2008-11-14 16:09:21 +00001665 }
Eli Friedman8e236422012-10-12 22:45:14 +00001666 } else if (Kind == OO_Arrow) {
1667 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001668 } else if (Kind == OO_Call) {
1669 PrintExpr(Node->getArg(0));
1670 OS << '(';
1671 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1672 if (ArgIdx > 1)
1673 OS << ", ";
1674 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1675 PrintExpr(Node->getArg(ArgIdx));
1676 }
1677 OS << ')';
1678 } else if (Kind == OO_Subscript) {
1679 PrintExpr(Node->getArg(0));
1680 OS << '[';
1681 PrintExpr(Node->getArg(1));
1682 OS << ']';
1683 } else if (Node->getNumArgs() == 1) {
Richard Smith7fa2b742019-06-14 20:01:51 +00001684 OS << getOperatorSpelling(Kind) << ' ';
Douglas Gregor993603d2008-11-14 16:09:21 +00001685 PrintExpr(Node->getArg(0));
1686 } else if (Node->getNumArgs() == 2) {
1687 PrintExpr(Node->getArg(0));
Richard Smith7fa2b742019-06-14 20:01:51 +00001688 OS << ' ' << getOperatorSpelling(Kind) << ' ';
Douglas Gregor993603d2008-11-14 16:09:21 +00001689 PrintExpr(Node->getArg(1));
1690 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001691 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001692 }
1693}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001694
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001695void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001696 // If we have a conversion operator call only print the argument.
1697 CXXMethodDecl *MD = Node->getMethodDecl();
1698 if (MD && isa<CXXConversionDecl>(MD)) {
1699 PrintExpr(Node->getImplicitObjectArgument());
1700 return;
1701 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001702 VisitCallExpr(cast<CallExpr>(Node));
1703}
1704
Peter Collingbourne41f85462011-02-09 21:07:24 +00001705void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1706 PrintExpr(Node->getCallee());
1707 OS << "<<<";
1708 PrintCallArgs(Node->getConfig());
1709 OS << ">>>(";
1710 PrintCallArgs(Node);
1711 OS << ")";
1712}
1713
Richard Smith778dc0f2019-10-19 00:04:38 +00001714void StmtPrinter::VisitCXXRewrittenBinaryOperator(
1715 CXXRewrittenBinaryOperator *Node) {
1716 CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
1717 Node->getDecomposedForm();
1718 PrintExpr(const_cast<Expr*>(Decomposed.LHS));
1719 OS << ' ' << BinaryOperator::getOpcodeStr(Decomposed.Opcode) << ' ';
1720 PrintExpr(const_cast<Expr*>(Decomposed.RHS));
1721}
1722
Douglas Gregore200adc2008-10-27 19:41:14 +00001723void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1724 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001725 Node->getTypeAsWritten().print(OS, Policy);
1726 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001727 PrintExpr(Node->getSubExpr());
1728 OS << ")";
1729}
1730
Douglas Gregore200adc2008-10-27 19:41:14 +00001731void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1732 VisitCXXNamedCastExpr(Node);
1733}
1734
1735void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1736 VisitCXXNamedCastExpr(Node);
1737}
1738
1739void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1740 VisitCXXNamedCastExpr(Node);
1741}
1742
1743void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1744 VisitCXXNamedCastExpr(Node);
1745}
1746
Erik Pilkingtoneee944e2019-07-02 18:28:13 +00001747void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) {
1748 OS << "__builtin_bit_cast(";
1749 Node->getTypeInfoAsWritten()->getType().print(OS, Policy);
1750 OS << ", ";
1751 PrintExpr(Node->getSubExpr());
1752 OS << ")";
1753}
1754
Sebastian Redlc4704762008-11-11 11:37:55 +00001755void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1756 OS << "typeid(";
1757 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001758 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001759 } else {
1760 PrintExpr(Node->getExprOperand());
1761 }
1762 OS << ")";
1763}
1764
Francois Pichet9f4f2072010-09-08 12:20:18 +00001765void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1766 OS << "__uuidof(";
1767 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001768 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001769 } else {
1770 PrintExpr(Node->getExprOperand());
1771 }
1772 OS << ")";
1773}
1774
John McCall5e77d762013-04-16 07:28:30 +00001775void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1776 PrintExpr(Node->getBaseExpr());
1777 if (Node->isArrow())
1778 OS << "->";
1779 else
1780 OS << ".";
1781 if (NestedNameSpecifier *Qualifier =
1782 Node->getQualifierLoc().getNestedNameSpecifier())
1783 Qualifier->print(OS, Policy);
1784 OS << Node->getPropertyDecl()->getDeclName();
1785}
1786
Alexey Bataevf7630272015-11-25 12:01:00 +00001787void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1788 PrintExpr(Node->getBase());
1789 OS << "[";
1790 PrintExpr(Node->getIdx());
1791 OS << "]";
1792}
1793
Richard Smithc67fdd42012-03-07 08:35:16 +00001794void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1795 switch (Node->getLiteralOperatorKind()) {
1796 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001797 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001798 break;
1799 case UserDefinedLiteral::LOK_Template: {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001800 const auto *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
Richard Smith29e95952012-03-09 10:10:02 +00001801 const TemplateArgumentList *Args =
1802 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1803 assert(Args);
David Majnemer314b72f2015-04-05 05:32:54 +00001804
1805 if (Args->size() != 1) {
Richard Smithe87aeb32015-10-08 00:17:59 +00001806 OS << "operator\"\"" << Node->getUDSuffix()->getName();
Serge Pavlov03e672c2017-11-28 16:14:14 +00001807 printTemplateArgumentList(OS, Args->asArray(), Policy);
David Majnemer314b72f2015-04-05 05:32:54 +00001808 OS << "()";
1809 return;
1810 }
1811
Richard Smith29e95952012-03-09 10:10:02 +00001812 const TemplateArgument &Pack = Args->get(0);
Aaron Ballman2a89e852014-07-15 21:32:31 +00001813 for (const auto &P : Pack.pack_elements()) {
1814 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001815 OS << C;
1816 }
1817 break;
1818 }
Richard Smith75025ba2012-03-08 09:02:38 +00001819 case UserDefinedLiteral::LOK_Integer: {
1820 // Print integer literal without suffix.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001821 const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
Richard Smith75025ba2012-03-08 09:02:38 +00001822 OS << Int->getValue().toString(10, /*isSigned*/false);
1823 break;
1824 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001825 case UserDefinedLiteral::LOK_Floating: {
1826 // Print floating literal without suffix.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001827 auto *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
Benjamin Kramer8a526762012-09-20 14:07:17 +00001828 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1829 break;
1830 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001831 case UserDefinedLiteral::LOK_String:
1832 case UserDefinedLiteral::LOK_Character:
1833 PrintExpr(Node->getCookedLiteral());
1834 break;
1835 }
1836 OS << Node->getUDSuffix()->getName();
1837}
1838
Chris Lattnereefa10e2007-05-28 06:56:27 +00001839void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1840 OS << (Node->getValue() ? "true" : "false");
1841}
1842
Sebastian Redl576fd422009-05-10 18:38:11 +00001843void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1844 OS << "nullptr";
1845}
1846
Douglas Gregor97a9c812008-11-04 14:32:21 +00001847void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1848 OS << "this";
1849}
1850
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001851void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001852 if (!Node->getSubExpr())
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001853 OS << "throw";
1854 else {
1855 OS << "throw ";
1856 PrintExpr(Node->getSubExpr());
1857 }
1858}
1859
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001860void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001861 // Nothing to print: we picked up the default argument.
1862}
1863
1864void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1865 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001866}
1867
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001868void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001869 Node->getType().print(OS, Policy);
Richard Smith1ae689c2015-01-28 22:06:01 +00001870 // If there are no parens, this is list-initialization, and the braces are
1871 // part of the syntax of the inner construct.
1872 if (Node->getLParenLoc().isValid())
1873 OS << "(";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001874 PrintExpr(Node->getSubExpr());
Richard Smith1ae689c2015-01-28 22:06:01 +00001875 if (Node->getLParenLoc().isValid())
1876 OS << ")";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001877}
1878
Anders Carlsson993a4b32009-05-30 20:03:25 +00001879void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1880 PrintExpr(Node->getSubExpr());
1881}
1882
Douglas Gregordd04d332009-01-16 18:33:17 +00001883void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001884 Node->getType().print(OS, Policy);
Richard Smithed83ebd2015-02-05 07:02:11 +00001885 if (Node->isStdInitListInitialization())
1886 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1887 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001888 OS << "{";
1889 else
1890 OS << "(";
Douglas Gregordd04d332009-01-16 18:33:17 +00001891 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001892 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001893 Arg != ArgEnd; ++Arg) {
Benjamin Kramerf48ee442015-07-18 14:35:53 +00001894 if ((*Arg)->isDefaultArgument())
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001895 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001896 if (Arg != Node->arg_begin())
1897 OS << ", ";
1898 PrintExpr(*Arg);
1899 }
Richard Smithed83ebd2015-02-05 07:02:11 +00001900 if (Node->isStdInitListInitialization())
1901 /* See above. */;
1902 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001903 OS << "}";
1904 else
1905 OS << ")";
Douglas Gregordd04d332009-01-16 18:33:17 +00001906}
1907
Douglas Gregore31e6062012-02-07 10:09:13 +00001908void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1909 OS << '[';
1910 bool NeedComma = false;
1911 switch (Node->getCaptureDefault()) {
1912 case LCD_None:
1913 break;
1914
1915 case LCD_ByCopy:
1916 OS << '=';
1917 NeedComma = true;
1918 break;
1919
1920 case LCD_ByRef:
1921 OS << '&';
1922 NeedComma = true;
1923 break;
1924 }
1925 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1926 CEnd = Node->explicit_capture_end();
1927 C != CEnd;
1928 ++C) {
Richard Trieu931638e2017-11-11 00:54:25 +00001929 if (C->capturesVLAType())
1930 continue;
1931
Douglas Gregore31e6062012-02-07 10:09:13 +00001932 if (NeedComma)
1933 OS << ", ";
1934 NeedComma = true;
1935
1936 switch (C->getCaptureKind()) {
1937 case LCK_This:
1938 OS << "this";
1939 break;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001940
Faisal Validc6b5962016-03-21 09:25:37 +00001941 case LCK_StarThis:
1942 OS << "*this";
1943 break;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001944
Douglas Gregore31e6062012-02-07 10:09:13 +00001945 case LCK_ByRef:
James Dennettdd2ffea22015-05-07 18:48:18 +00001946 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
Douglas Gregore31e6062012-02-07 10:09:13 +00001947 OS << '&';
1948 OS << C->getCapturedVar()->getName();
1949 break;
1950
1951 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001952 OS << C->getCapturedVar()->getName();
1953 break;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001954
Alexey Bataev39c81e22014-08-28 04:28:19 +00001955 case LCK_VLAType:
1956 llvm_unreachable("VLA type in explicit captures.");
Douglas Gregore31e6062012-02-07 10:09:13 +00001957 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001958
Nicolas Lesser5125a022019-05-05 12:35:12 +00001959 if (C->isPackExpansion())
1960 OS << "...";
1961
James Dennettdd2ffea22015-05-07 18:48:18 +00001962 if (Node->isInitCapture(C))
Richard Smithbb13c9a2013-09-28 04:02:39 +00001963 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001964 }
1965 OS << ']';
1966
Hamza Sood8205a812019-05-04 10:49:46 +00001967 if (!Node->getExplicitTemplateParameters().empty()) {
1968 Node->getTemplateParameterList()->print(
1969 OS, Node->getLambdaClass()->getASTContext(),
1970 /*OmitTemplateKW*/true);
1971 }
1972
Douglas Gregore31e6062012-02-07 10:09:13 +00001973 if (Node->hasExplicitParameters()) {
Hamza Sood8205a812019-05-04 10:49:46 +00001974 OS << '(';
Douglas Gregore31e6062012-02-07 10:09:13 +00001975 CXXMethodDecl *Method = Node->getCallOperator();
1976 NeedComma = false;
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001977 for (const auto *P : Method->parameters()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001978 if (NeedComma) {
1979 OS << ", ";
1980 } else {
1981 NeedComma = true;
1982 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00001983 std::string ParamStr = P->getNameAsString();
1984 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001985 }
1986 if (Method->isVariadic()) {
1987 if (NeedComma)
1988 OS << ", ";
1989 OS << "...";
1990 }
1991 OS << ')';
1992
1993 if (Node->isMutable())
1994 OS << " mutable";
1995
Simon Pilgrim3459a4c2019-10-07 13:58:05 +00001996 auto *Proto = Method->getType()->castAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001997 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001998
Bill Wendling44426052012-12-20 19:22:21 +00001999 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00002000
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00002001 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002002 if (Node->hasExplicitResultType()) {
2003 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00002004 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002005 }
Douglas Gregore31e6062012-02-07 10:09:13 +00002006 }
2007
2008 // Print the body.
Douglas Gregore31e6062012-02-07 10:09:13 +00002009 OS << ' ';
Kadir Cetinkaya20c3c4f2019-05-27 16:20:45 +00002010 if (Policy.TerseOutput)
2011 OS << "{}";
2012 else
2013 PrintRawCompoundStmt(Node->getBody());
Douglas Gregore31e6062012-02-07 10:09:13 +00002014}
2015
Douglas Gregor747eb782010-07-08 06:14:04 +00002016void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00002017 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002018 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00002019 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002020 Node->getType().print(OS, Policy);
2021 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002022}
2023
Sebastian Redlbd150f42008-11-21 19:14:01 +00002024void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2025 if (E->isGlobalNew())
2026 OS << "::";
2027 OS << "new ";
2028 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002029 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002030 OS << "(";
2031 PrintExpr(E->getPlacementArg(0));
2032 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002033 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2034 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002035 OS << ", ";
2036 PrintExpr(E->getPlacementArg(i));
2037 }
2038 OS << ") ";
2039 }
2040 if (E->isParenTypeId())
2041 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002042 std::string TypeS;
Richard Smithb9fb1212019-05-06 03:47:15 +00002043 if (Optional<Expr *> Size = E->getArraySize()) {
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002044 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002045 s << '[';
Richard Smithb9fb1212019-05-06 03:47:15 +00002046 if (*Size)
2047 (*Size)->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002048 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002049 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002050 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002051 if (E->isParenTypeId())
2052 OS << ")";
2053
Sebastian Redl6047f072012-02-16 12:22:20 +00002054 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2055 if (InitStyle) {
2056 if (InitStyle == CXXNewExpr::CallInit)
2057 OS << "(";
2058 PrintExpr(E->getInitializer());
2059 if (InitStyle == CXXNewExpr::CallInit)
2060 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00002061 }
2062}
2063
2064void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2065 if (E->isGlobalDelete())
2066 OS << "::";
2067 OS << "delete ";
2068 if (E->isArrayForm())
2069 OS << "[] ";
2070 PrintExpr(E->getArgument());
2071}
2072
Douglas Gregorad8a3362009-09-04 17:36:40 +00002073void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2074 PrintExpr(E->getBase());
2075 if (E->isArrow())
2076 OS << "->";
2077 else
2078 OS << '.';
2079 if (E->getQualifier())
2080 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00002081 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00002082
Douglas Gregor678f90d2010-02-25 01:56:36 +00002083 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2084 OS << II->getName();
2085 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002086 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00002087}
2088
Anders Carlsson0781ce72009-04-23 02:32:43 +00002089void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithed83ebd2015-02-05 07:02:11 +00002090 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002091 OS << "{";
Richard Smithd59b8322012-12-19 01:39:02 +00002092
Douglas Gregorbaba85d2011-01-24 17:25:03 +00002093 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2094 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2095 // Don't print any defaulted arguments
2096 break;
2097 }
2098
2099 if (i) OS << ", ";
2100 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00002101 }
Richard Smithd59b8322012-12-19 01:39:02 +00002102
Richard Smithed83ebd2015-02-05 07:02:11 +00002103 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002104 OS << "}";
Anders Carlsson0781ce72009-04-23 02:32:43 +00002105}
2106
Richard Smith5179eb72016-06-28 19:03:57 +00002107void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2108 // Parens are printed by the surrounding context.
2109 OS << "<forwarded>";
2110}
2111
Richard Smithcc1b96d2013-06-12 22:31:48 +00002112void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2113 PrintExpr(E->getSubExpr());
2114}
2115
John McCall5d413782010-12-06 08:20:24 +00002116void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00002117 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00002118 PrintExpr(E->getSubExpr());
2119}
2120
Mike Stump11289f42009-09-09 15:08:12 +00002121void
Douglas Gregorce934142009-05-20 18:46:25 +00002122StmtPrinter::VisitCXXUnresolvedConstructExpr(
2123 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002124 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00002125 OS << "(";
2126 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00002127 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00002128 Arg != ArgEnd; ++Arg) {
2129 if (Arg != Node->arg_begin())
2130 OS << ", ";
2131 PrintExpr(*Arg);
2132 }
2133 OS << ")";
2134}
2135
John McCall8cd78132009-11-19 22:55:06 +00002136void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2137 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002138 if (!Node->isImplicitAccess()) {
2139 PrintExpr(Node->getBase());
2140 OS << (Node->isArrow() ? "->" : ".");
2141 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00002142 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2143 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002144 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00002145 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002146 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002147 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00002148 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00002149}
2150
John McCall10eae182009-11-30 22:42:35 +00002151void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002152 if (!Node->isImplicitAccess()) {
2153 PrintExpr(Node->getBase());
2154 OS << (Node->isArrow() ? "->" : ".");
2155 }
John McCall10eae182009-11-30 22:42:35 +00002156 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2157 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002158 if (Node->hasTemplateKeyword())
2159 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002160 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002161 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00002162 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
John McCall10eae182009-11-30 22:42:35 +00002163}
2164
Douglas Gregor29c42f22012-02-24 07:38:34 +00002165static const char *getTypeTraitName(TypeTrait TT) {
2166 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00002167#define TYPE_TRAIT_1(Spelling, Name, Key) \
2168case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00002169#define TYPE_TRAIT_2(Spelling, Name, Key) \
2170case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00002171#define TYPE_TRAIT_N(Spelling, Name, Key) \
2172 case clang::TT_##Name: return #Spelling;
2173#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002174 }
2175 llvm_unreachable("Type trait not covered by switch");
2176}
2177
John Wiegley6242b6a2011-04-28 00:16:57 +00002178static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2179 switch (ATT) {
2180 case ATT_ArrayRank: return "__array_rank";
2181 case ATT_ArrayExtent: return "__array_extent";
2182 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002183 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00002184}
2185
John Wiegleyf9f65842011-04-25 06:54:41 +00002186static const char *getExpressionTraitName(ExpressionTrait ET) {
2187 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00002188 case ET_IsLValueExpr: return "__is_lvalue_expr";
2189 case ET_IsRValueExpr: return "__is_rvalue_expr";
2190 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002191 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00002192}
2193
Douglas Gregor29c42f22012-02-24 07:38:34 +00002194void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2195 OS << getTypeTraitName(E->getTrait()) << "(";
2196 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2197 if (I > 0)
2198 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002199 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00002200 }
2201 OS << ")";
2202}
2203
John Wiegley6242b6a2011-04-28 00:16:57 +00002204void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002205 OS << getTypeTraitName(E->getTrait()) << '(';
2206 E->getQueriedType().print(OS, Policy);
2207 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00002208}
2209
John Wiegleyf9f65842011-04-25 06:54:41 +00002210void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002211 OS << getExpressionTraitName(E->getTrait()) << '(';
2212 PrintExpr(E->getQueriedExpression());
2213 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00002214}
2215
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002216void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2217 OS << "noexcept(";
2218 PrintExpr(E->getOperand());
2219 OS << ")";
2220}
2221
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002222void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002223 PrintExpr(E->getPattern());
2224 OS << "...";
2225}
2226
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002227void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002228 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002229}
2230
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002231void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2232 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002233 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002234}
2235
John McCall7c454bb2011-07-15 05:09:51 +00002236void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2237 SubstNonTypeTemplateParmExpr *Node) {
2238 Visit(Node->getReplacement());
2239}
2240
Richard Smithb15fe3a2012-09-12 00:56:43 +00002241void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2242 OS << *E->getParameterPack();
2243}
2244
Douglas Gregorfe314812011-06-21 17:03:29 +00002245void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
Tykerb0561b32019-11-17 11:41:55 +01002246 PrintExpr(Node->getSubExpr());
Douglas Gregorfe314812011-06-21 17:03:29 +00002247}
2248
Richard Smith0f0af192014-11-08 05:07:16 +00002249void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2250 OS << "(";
2251 if (E->getLHS()) {
2252 PrintExpr(E->getLHS());
2253 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2254 }
2255 OS << "...";
2256 if (E->getRHS()) {
2257 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2258 PrintExpr(E->getRHS());
2259 }
2260 OS << ")";
2261}
2262
Saar Raz5d98ba62019-10-15 15:24:26 +00002263void StmtPrinter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
2264 NestedNameSpecifierLoc NNS = E->getNestedNameSpecifierLoc();
2265 if (NNS)
2266 NNS.getNestedNameSpecifier()->print(OS, Policy);
2267 if (E->getTemplateKWLoc().isValid())
2268 OS << "template ";
2269 OS << E->getFoundDecl()->getName();
2270 printTemplateArgumentList(OS, E->getTemplateArgsAsWritten()->arguments(),
2271 Policy);
2272}
2273
Saar Raza0f50d72020-01-18 09:11:43 +02002274void StmtPrinter::VisitRequiresExpr(RequiresExpr *E) {
2275 OS << "requires ";
2276 auto LocalParameters = E->getLocalParameters();
2277 if (!LocalParameters.empty()) {
2278 OS << "(";
2279 for (ParmVarDecl *LocalParam : LocalParameters) {
2280 PrintRawDecl(LocalParam);
2281 if (LocalParam != LocalParameters.back())
2282 OS << ", ";
2283 }
2284
2285 OS << ") ";
2286 }
2287 OS << "{ ";
2288 auto Requirements = E->getRequirements();
2289 for (concepts::Requirement *Req : Requirements) {
2290 if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) {
2291 if (TypeReq->isSubstitutionFailure())
2292 OS << "<<error-type>>";
2293 else
2294 TypeReq->getType()->getType().print(OS, Policy);
2295 } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) {
2296 if (ExprReq->isCompound())
2297 OS << "{ ";
2298 if (ExprReq->isExprSubstitutionFailure())
2299 OS << "<<error-expression>>";
2300 else
2301 PrintExpr(ExprReq->getExpr());
2302 if (ExprReq->isCompound()) {
2303 OS << " }";
2304 if (ExprReq->getNoexceptLoc().isValid())
2305 OS << " noexcept";
2306 const auto &RetReq = ExprReq->getReturnTypeRequirement();
2307 if (!RetReq.isEmpty()) {
2308 OS << " -> ";
2309 if (RetReq.isSubstitutionFailure())
2310 OS << "<<error-type>>";
2311 else if (RetReq.isTypeConstraint())
2312 RetReq.getTypeConstraint()->print(OS, Policy);
2313 }
2314 }
2315 } else {
2316 auto *NestedReq = cast<concepts::NestedRequirement>(Req);
2317 OS << "requires ";
2318 if (NestedReq->isSubstitutionFailure())
2319 OS << "<<error-expression>>";
2320 else
2321 PrintExpr(NestedReq->getConstraintExpr());
2322 }
2323 OS << "; ";
2324 }
2325 OS << "}";
2326}
2327
Richard Smith9f690bd2015-10-27 06:02:45 +00002328// C++ Coroutines TS
2329
2330void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2331 Visit(S->getBody());
2332}
2333
2334void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2335 OS << "co_return";
2336 if (S->getOperand()) {
2337 OS << " ";
2338 Visit(S->getOperand());
2339 }
2340 OS << ";";
2341}
2342
2343void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2344 OS << "co_await ";
2345 PrintExpr(S->getOperand());
2346}
2347
Eric Fiselier20f25cb2017-03-06 23:38:15 +00002348void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2349 OS << "co_await ";
2350 PrintExpr(S->getOperand());
2351}
2352
Richard Smith9f690bd2015-10-27 06:02:45 +00002353void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2354 OS << "co_yield ";
2355 PrintExpr(S->getOperand());
2356}
2357
Mike Stump11289f42009-09-09 15:08:12 +00002358// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00002359
2360void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2361 OS << "@";
2362 VisitStringLiteral(Node->getString());
2363}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002364
Patrick Beard0caa3942012-04-19 00:25:12 +00002365void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00002366 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00002367 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002368}
2369
2370void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2371 OS << "@[ ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002372 ObjCArrayLiteral::child_range Ch = E->children();
2373 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2374 if (I != Ch.begin())
Ted Kremeneke65b0862012-03-06 20:05:56 +00002375 OS << ", ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002376 Visit(*I);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002377 }
2378 OS << " ]";
2379}
2380
2381void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2382 OS << "@{ ";
2383 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2384 if (I > 0)
2385 OS << ", ";
Fangrui Song6907ce22018-07-30 19:24:48 +00002386
Ted Kremeneke65b0862012-03-06 20:05:56 +00002387 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2388 Visit(Element.Key);
2389 OS << " : ";
2390 Visit(Element.Value);
2391 if (Element.isPackExpansion())
2392 OS << "...";
2393 }
2394 OS << " }";
2395}
2396
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002397void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002398 OS << "@encode(";
2399 Node->getEncodedType().print(OS, Policy);
2400 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002401}
2402
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002403void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00002404 OS << "@selector(";
2405 Node->getSelector().print(OS);
2406 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002407}
2408
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002409void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002410 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002411}
2412
Steve Naroffd54978b2007-09-18 23:55:05 +00002413void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2414 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00002415 switch (Mess->getReceiverKind()) {
2416 case ObjCMessageExpr::Instance:
2417 PrintExpr(Mess->getInstanceReceiver());
2418 break;
2419
2420 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002421 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00002422 break;
2423
2424 case ObjCMessageExpr::SuperInstance:
2425 case ObjCMessageExpr::SuperClass:
2426 OS << "Super";
2427 break;
2428 }
2429
Ted Kremeneka06e7122008-05-02 17:32:38 +00002430 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00002431 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00002432 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002433 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002434 } else {
2435 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00002436 if (i < selector.getNumArgs()) {
2437 if (i > 0) OS << ' ';
2438 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002439 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00002440 else
2441 OS << ":";
2442 }
2443 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00002444
Steve Naroffc6814ea2007-10-02 20:01:56 +00002445 PrintExpr(Mess->getArg(i));
2446 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002447 }
2448 OS << "]";
2449}
2450
Ted Kremeneke65b0862012-03-06 20:05:56 +00002451void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2452 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2453}
2454
John McCall31168b02011-06-15 23:02:42 +00002455void
2456StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2457 PrintExpr(E->getSubExpr());
2458}
2459
2460void
2461StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002462 OS << '(' << E->getBridgeKindName();
2463 E->getType().print(OS, Policy);
2464 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00002465 PrintExpr(E->getSubExpr());
2466}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002467
Steve Naroffc540d662008-09-03 18:15:37 +00002468void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002469 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00002470 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00002471
Steve Naroffc540d662008-09-03 18:15:37 +00002472 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002473
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002474 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00002475 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002476 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00002477 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00002478 for (BlockDecl::param_iterator AI = BD->param_begin(),
2479 E = BD->param_end(); AI != E; ++AI) {
2480 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002481 std::string ParamStr = (*AI)->getNameAsString();
2482 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00002483 }
Mike Stump11289f42009-09-09 15:08:12 +00002484
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00002485 const auto *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00002486 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002487 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00002488 OS << "...";
2489 }
2490 OS << ')';
2491 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00002492 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00002493}
2494
Fangrui Song6907ce22018-07-30 19:24:48 +00002495void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
Ted Kremenekf551f322011-11-30 22:08:08 +00002496 PrintExpr(Node->getSourceExpr());
2497}
John McCall8d69a212010-11-15 23:31:06 +00002498
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002499void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2500 // TODO: Print something reasonable for a TypoExpr, if necessary.
Davide Italiano04839a52016-01-30 08:03:54 +00002501 llvm_unreachable("Cannot print TypoExpr nodes");
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002502}
2503
Tanya Lattner55808c12011-06-04 00:47:47 +00002504void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2505 OS << "__builtin_astype(";
2506 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002507 OS << ", ";
2508 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00002509 OS << ")";
2510}
2511
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002512//===----------------------------------------------------------------------===//
2513// Stmt method implementations
2514//===----------------------------------------------------------------------===//
2515
Craig Topperc571c812013-08-22 06:02:26 +00002516void Stmt::dumpPretty(const ASTContext &Context) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002517 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002518}
2519
Csaba Dabis9ee26c82019-05-29 18:17:18 +00002520void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
Alex Lorenz36070ed2017-08-17 13:41:55 +00002521 const PrintingPolicy &Policy, unsigned Indentation,
Csaba Dabis9ee26c82019-05-29 18:17:18 +00002522 StringRef NL, const ASTContext *Context) const {
2523 StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
2524 P.Visit(const_cast<Stmt *>(this));
2525}
2526
2527void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
2528 const PrintingPolicy &Policy, bool AddQuotes) const {
2529 std::string Buf;
2530 llvm::raw_string_ostream TempOut(Buf);
2531
2532 printPretty(TempOut, Helper, Policy);
2533
2534 Out << JsonFormat(TempOut.str(), AddQuotes);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002535}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002536
2537//===----------------------------------------------------------------------===//
2538// PrinterHelper
2539//===----------------------------------------------------------------------===//
2540
2541// Implement virtual destructor.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00002542PrinterHelper::~PrinterHelper() = default;